knapsack - knapsack
Dữ liệu vào: Standard input
Dữ liệu ra: Standard output
Giới hạn thời gian: 1.0 giây
Giới hạn bộ nhớ: 128 megabyte
Đăng bởi: smurf

There is a housewife who recently won a prize to “shop for free as long as your shopping basket is not full” in a department store.

This housewife is given a shopping basket that can carry a maximal weight of S kilograms.

There are \(N\) item types in the department store and the \(i\)-th item is worth \(V_i\) VNĐ, weighs \(W_i\) kilograms, and there are \(K_i\) copies (of exactly same value and weight) of such item \(i\).

For example, there are \(N = 3\) item types: meat, milk, and bread; of which there are: \(1\) pack of meat, \(3\) bottles of milk, and \(4\) loaves of bread (see the last sample test case).

What items should the housewife take to maximize the total value of the items in her shopping basket?

Input:

The first line of input contains two positive integers, \(S\) and \(N\).

The next \(N\) lines of input will each contain three integers, where the \(i\)-th line contains \(V_i\)\(W_i\) and \(K_i\), the value in VNĐ, weight in kilograms and number of the \(i\)-th item respectively. 

Output:

Your program should print one integer, representing the maximum total value in VNĐ of the items that this housewife can take while ensuring the total weight does not exceed \(S\) kilograms.

Limit:

\(1 \leq S \leq 2000\)\(1 \leq V_i \leq 1000000\)\(1 \leq W_i \leq S\); \(1 \leq N \leq 100000\)\(1 \leq Ki \leq 10^9\)

Ví dụ

  • input
    15 5
    4 12 1
    2 1 1
    10 4 1
    1 1 1
    2 2 1
    output
    15
  • input
    20 3
    5000 15 1
    100 1 3
    50 1 4
    output
    5400

Sample Testcase 1:

The housewife can take one of items \(2, 3, 4, 5\) giving a total weight of \(1 + 4 + 1 + 2 = 8\) and a total value of \(2 + 10 + 1 + 2 = 15\).

Sample Testcase 2:

The housewife take one of item \(1\), three of item \(2\) and two of item \(3\) for a total weight of \(15 \times 1 + 1 \times 3 + 1 \times 2 = 20\) and a total value of \(5000 \times 1 + 100 \times 3 + 50 \times 2 = 5400\).

Back to Top