import java.io.*;
import java.util.*;
public class knapsack {
static int maxW, n;
static Item[] arr;
public static void main(String[] args) throws IOException{
BufferedReader file = new BufferedReader(new InputStreamReader(System.in));
//BufferedReader file = new BufferedReader(new FileReader("file.in"));
StringTokenizer st = new StringTokenizer(file.readLine());
maxW = Integer.parseInt(st.nextToken());
n = Integer.parseInt(st.nextToken());
arr = new Item[n];
for (int i=0; i<n; i++){
st = new StringTokenizer(file.readLine());
arr[i] = new Item(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
}
int[][] dp = new int[maxW+1][n+1];
for (int i=1; i<=maxW; i++){
for (int j=1; j<=n; j++){
for (int k=1; arr[j-1].weight*k<=i && k <= arr[j-1].k; k++){
if (i >= arr[j-1].weight*k){
dp[i][j] = Math.max(dp[i][j], dp[i-arr[j-1].weight*k][j-1] + arr[j-1].val*k);
}
}
}
}
System.out.println(dp[maxW][n]);
}
static class Item{
int val, weight, k;
public Item(int val, int weight, int k){
this.val = val;
this.weight = weight;
this.k = k;
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
8720 KB |
Output is correct |
2 |
Correct |
68 ms |
8360 KB |
Output is correct |
3 |
Correct |
83 ms |
9440 KB |
Output is correct |
4 |
Correct |
71 ms |
8476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
73 ms |
8060 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
73 ms |
8060 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
8720 KB |
Output is correct |
2 |
Correct |
68 ms |
8360 KB |
Output is correct |
3 |
Correct |
83 ms |
9440 KB |
Output is correct |
4 |
Correct |
71 ms |
8476 KB |
Output is correct |
5 |
Incorrect |
73 ms |
8060 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
8720 KB |
Output is correct |
2 |
Correct |
68 ms |
8360 KB |
Output is correct |
3 |
Correct |
83 ms |
9440 KB |
Output is correct |
4 |
Correct |
71 ms |
8476 KB |
Output is correct |
5 |
Incorrect |
73 ms |
8060 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |