結果

問題 No.2232 Miser's Gift
ユーザー tentententen
提出日時 2023-04-21 11:58:12
言語 Java21
(openjdk 21)
結果
AC  
実行時間 525 ms / 2,000 ms
コード長 2,441 bytes
コンパイル時間 2,314 ms
コンパイル使用メモリ 85,004 KB
実行使用メモリ 131,620 KB
最終ジャッジ日時 2024-04-24 01:31:18
合計ジャッジ時間 20,535 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
50,440 KB
testcase_01 AC 48 ms
50,348 KB
testcase_02 AC 50 ms
50,112 KB
testcase_03 AC 382 ms
128,928 KB
testcase_04 AC 364 ms
129,860 KB
testcase_05 AC 131 ms
61,620 KB
testcase_06 AC 49 ms
50,408 KB
testcase_07 AC 133 ms
56,996 KB
testcase_08 AC 428 ms
131,596 KB
testcase_09 AC 429 ms
131,208 KB
testcase_10 AC 418 ms
131,260 KB
testcase_11 AC 428 ms
131,196 KB
testcase_12 AC 422 ms
131,304 KB
testcase_13 AC 370 ms
128,660 KB
testcase_14 AC 372 ms
128,968 KB
testcase_15 AC 373 ms
128,732 KB
testcase_16 AC 374 ms
130,868 KB
testcase_17 AC 373 ms
128,928 KB
testcase_18 AC 367 ms
128,872 KB
testcase_19 AC 372 ms
128,916 KB
testcase_20 AC 363 ms
129,012 KB
testcase_21 AC 362 ms
128,648 KB
testcase_22 AC 363 ms
128,908 KB
testcase_23 AC 525 ms
131,620 KB
testcase_24 AC 499 ms
131,044 KB
testcase_25 AC 511 ms
131,380 KB
testcase_26 AC 508 ms
131,328 KB
testcase_27 AC 501 ms
131,020 KB
testcase_28 AC 444 ms
131,372 KB
testcase_29 AC 440 ms
131,304 KB
testcase_30 AC 463 ms
131,252 KB
testcase_31 AC 459 ms
131,340 KB
testcase_32 AC 459 ms
131,264 KB
testcase_33 AC 381 ms
129,748 KB
testcase_34 AC 383 ms
129,368 KB
testcase_35 AC 417 ms
129,636 KB
testcase_36 AC 391 ms
129,620 KB
testcase_37 AC 422 ms
129,492 KB
testcase_38 AC 85 ms
51,484 KB
testcase_39 AC 85 ms
51,356 KB
testcase_40 AC 78 ms
51,372 KB
testcase_41 AC 80 ms
50,984 KB
testcase_42 AC 81 ms
51,428 KB
testcase_43 AC 77 ms
51,004 KB
testcase_44 AC 83 ms
51,672 KB
testcase_45 AC 79 ms
51,588 KB
testcase_46 AC 80 ms
51,584 KB
testcase_47 AC 80 ms
51,048 KB
testcase_48 AC 54 ms
50,256 KB
testcase_49 AC 54 ms
50,476 KB
testcase_50 AC 52 ms
50,584 KB
testcase_51 AC 51 ms
50,412 KB
testcase_52 AC 51 ms
50,400 KB
testcase_53 AC 52 ms
50,204 KB
testcase_54 AC 53 ms
50,000 KB
testcase_55 AC 52 ms
50,448 KB
testcase_56 AC 52 ms
50,368 KB
testcase_57 AC 53 ms
50,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    static int[] weights;
    static int[] values;
    static long[][] dp;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int w = sc.nextInt();
        weights = new int[n];
        values = new int[n];
        for (int i = 0; i < n; i++) {
            weights[i] = sc.nextInt();
            values[i] = sc.nextInt();
        }
        dp = new long[n][w + 1];
        for (long[] arr : dp) {
            Arrays.fill(arr, -1);
        }
        long base = dfw(n - 1, w);
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= w; i++) {
            long current = dfw(n - 1, w - i);
            sb.append(base - current + 1).append("\n");
        }
        System.out.print(sb);
    }
    
    static long dfw(int idx, int v) {
        if (v < 0) {
            return Long.MIN_VALUE;
        }
        if (idx < 0) {
            return 0;
        }
        if (dp[idx][v] < 0) {
            dp[idx][v] = Math.max(dfw(idx - 1, v), dfw(idx - 1, v - weights[idx]) + values[idx]);
        }
        return dp[idx][v];
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0