이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
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()));
}
long[][] dp = new long[maxW+1][n+1];
for (int i=1; i<=maxW; i++){
for (int j=1; j<=n; j++){
dp[i][j] = dp[i][j-1];
for (int k=1; arr[j-1].weight*k<=i && k <= arr[j-1].k; k++){
if (i >= (long)arr[j-1].weight*k){
dp[i][j] = Math.max(dp[i][j], dp[i-arr[j-1].weight*k][j-1] + (long)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;
}
}
}
# | 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... |