This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
import java.io.*;
import java.util.StringTokenizer;
public class knapsack {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tokenizer = new StringTokenizer(in.readLine());
int s = Integer.parseInt(tokenizer.nextToken());
int n = Integer.parseInt(tokenizer.nextToken());
item[] items = new item[n+1];
for (int i = 1; i < n+1; i++) {
tokenizer = new StringTokenizer(in.readLine());
items[i] = new item(Integer.parseInt(tokenizer.nextToken()),Integer.parseInt(tokenizer.nextToken()),Integer.parseInt(tokenizer.nextToken()));
}
int[][] dp = new int[n+1][s+1];
for (int i = 0; i < n+1; i++) {
for (int j = 0; j < s+1; j++) {
dp[i][j] = -1;
}
}
dp[0][0] = 0;
for (int i = 0; i < n; i++) {
int v = items[i+1].v;
int w = items[i+1].w;
int k = items[i+1].k;
for (int j = 0; j < s+1; j++) {
if (dp[i][j] >= 0) {
for (int count = 0; count <= k; count++) {
if (j + w * count <= s) {
dp[i+1][j+w*count] = Math.max(dp[i+1][j+w*count], dp[i][j] + v*count);
}
else {
break;
}
}
}
}
}
int ans = 0;
for (int i = 1; i < n+1; i++) {
for (int j = 0; j < s+1; j++) {
ans = Math.max(ans, dp[i][j]);
}
}
System.out.println(ans);
}
static class item {
int v, w, k;
item(int v, int w, int k) {
this.v = v;
this.w = w;
this.k = k;
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |