이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
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];
int[] dp = new int[s+1];
dp[0] = 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 ans = 0;
for (int i = 1; i < n+1; i++) {
int v = items[i].v;
int w = items[i].w;
int k = items[i].k;
int[] nextDP = new int[s+1];
for (int j = 0; j < s+1; j++) {
for (int count = 0; count <= k; count++) {
if (j - w * count >= 0) {
if (dp[j - w * count] >= 1) {
nextDP[j] = Math.max(nextDP[j], dp[j-w*count] + v * count);
ans = Math.max(ans, nextDP[j]);
}
}
else {
break;
}
}
}
dp = nextDP;
}
System.out.println(ans-1);
}
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... |