結果

問題 No.1947 質より種類数
ユーザー tentententen
提出日時 2022-08-10 08:27:23
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,927 ms / 2,000 ms
コード長 2,011 bytes
コンパイル時間 5,748 ms
コンパイル使用メモリ 77,276 KB
実行使用メモリ 256,392 KB
最終ジャッジ日時 2023-10-20 17:18:44
合計ジャッジ時間 25,249 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
53,508 KB
testcase_01 AC 54 ms
53,516 KB
testcase_02 AC 54 ms
53,516 KB
testcase_03 AC 54 ms
53,516 KB
testcase_04 AC 92 ms
55,360 KB
testcase_05 AC 85 ms
54,272 KB
testcase_06 AC 84 ms
54,448 KB
testcase_07 AC 90 ms
55,412 KB
testcase_08 AC 93 ms
55,064 KB
testcase_09 AC 145 ms
61,648 KB
testcase_10 AC 132 ms
59,196 KB
testcase_11 AC 185 ms
66,144 KB
testcase_12 AC 167 ms
62,132 KB
testcase_13 AC 118 ms
58,384 KB
testcase_14 AC 595 ms
119,460 KB
testcase_15 AC 358 ms
90,372 KB
testcase_16 AC 780 ms
133,272 KB
testcase_17 AC 1,017 ms
189,504 KB
testcase_18 AC 1,927 ms
243,484 KB
testcase_19 AC 1,681 ms
231,676 KB
testcase_20 AC 1,155 ms
190,012 KB
testcase_21 AC 1,451 ms
231,364 KB
testcase_22 AC 179 ms
66,168 KB
testcase_23 AC 693 ms
134,408 KB
testcase_24 AC 103 ms
56,404 KB
testcase_25 AC 92 ms
55,128 KB
testcase_26 AC 73 ms
54,360 KB
testcase_27 AC 87 ms
54,936 KB
testcase_28 AC 91 ms
55,160 KB
testcase_29 AC 1,526 ms
252,580 KB
testcase_30 AC 1,637 ms
255,456 KB
testcase_31 AC 58 ms
53,524 KB
testcase_32 AC 55 ms
53,524 KB
testcase_33 AC 58 ms
54,256 KB
testcase_34 AC 1,117 ms
255,588 KB
testcase_35 AC 353 ms
252,136 KB
testcase_36 AC 1,605 ms
256,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static int c;
    static int[] costs;
    static int[] values;
    static int[][][] dp;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int v = sc.nextInt();
        c = sc.nextInt();
        costs = new int[n];
        values = new int[n];
        for (int i = 0; i < n; i++) {
            costs[i] = sc.nextInt();
            values[i] = sc.nextInt();
        }
        dp = new int[2][n][v + 1];
        for (int[][] arr1 : dp) {
            for (int[] arr2 : arr1) {
                Arrays.fill(arr2, -1);
            }
        }
        System.out.println(dfw(0, n - 1, v));
    }
    
    static int dfw(int times, int idx, int v) {
        if (v < 0) {
            return Integer.MIN_VALUE;
        }
        if (idx < 0) {
            return 0;
        }
        if (dp[times][idx][v] < 0) {
            dp[times][idx][v] = dfw(0, idx - 1, v);
            int add = 0;
            if (times == 0) {
                add = c;
            }
            dp[times][idx][v] = Math.max(dp[times][idx][v], Math.max(dfw(0, idx - 1, v - costs[idx]) + add + values[idx], dfw(1, idx, v - costs[idx]) + add + values[idx]));
        }
        return dp[times][idx][v];
    }
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    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 String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0