제출 #467828

#제출 시각아이디문제언어결과실행 시간메모리
467828rainliofficialKnapsack (NOI18_knapsack)Java
73 / 100
1083 ms17668 KiB
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]; long[] pdp = new long[maxW+1]; for (int j=1; j<=n; j++){ for (int i=1; i<=maxW; i++){ dp[i] = pdp[i]; 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] = Math.max(dp[i], pdp[i-arr[j-1].weight*k] + (long)arr[j-1].val*k); } } } for (int k=0; k<=maxW; k++){ pdp[k] = dp[k]; dp[k] = 0; } } System.out.println(pdp[maxW]); } 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...