結果

問題 No.1947 質より種類数
ユーザー tentententen
提出日時 2023-02-24 17:12:05
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,311 ms / 2,000 ms
コード長 2,487 bytes
コンパイル時間 2,629 ms
コンパイル使用メモリ 86,584 KB
実行使用メモリ 254,272 KB
最終ジャッジ日時 2023-10-11 02:30:32
合計ジャッジ時間 20,857 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
49,444 KB
testcase_01 AC 41 ms
49,504 KB
testcase_02 AC 42 ms
49,396 KB
testcase_03 AC 43 ms
49,920 KB
testcase_04 AC 73 ms
52,384 KB
testcase_05 AC 61 ms
52,544 KB
testcase_06 AC 62 ms
52,448 KB
testcase_07 AC 71 ms
52,448 KB
testcase_08 AC 77 ms
52,464 KB
testcase_09 AC 104 ms
57,880 KB
testcase_10 AC 89 ms
55,080 KB
testcase_11 AC 135 ms
61,948 KB
testcase_12 AC 117 ms
58,016 KB
testcase_13 AC 86 ms
55,512 KB
testcase_14 AC 420 ms
113,260 KB
testcase_15 AC 249 ms
86,036 KB
testcase_16 AC 599 ms
131,296 KB
testcase_17 AC 728 ms
187,580 KB
testcase_18 AC 1,273 ms
240,076 KB
testcase_19 AC 1,144 ms
228,456 KB
testcase_20 AC 871 ms
187,620 KB
testcase_21 AC 1,091 ms
228,228 KB
testcase_22 AC 128 ms
62,260 KB
testcase_23 AC 495 ms
130,132 KB
testcase_24 AC 73 ms
53,408 KB
testcase_25 AC 71 ms
52,588 KB
testcase_26 AC 57 ms
52,592 KB
testcase_27 AC 60 ms
52,724 KB
testcase_28 AC 76 ms
52,428 KB
testcase_29 AC 1,248 ms
252,568 KB
testcase_30 AC 1,166 ms
252,752 KB
testcase_31 AC 42 ms
49,464 KB
testcase_32 AC 42 ms
47,512 KB
testcase_33 AC 46 ms
50,048 KB
testcase_34 AC 1,311 ms
253,052 KB
testcase_35 AC 345 ms
252,820 KB
testcase_36 AC 1,263 ms
254,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static int c;
    static int[] prices;
    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();
        prices = new int[n];
        values = new int[n];
        for (int i = 0; i < n; i++) {
            prices[i] = sc.nextInt();
            values[i] = sc.nextInt();
        }
        dp = new int[n * 2][v + 1];
        for (int[] arr : dp) {
            Arrays.fill(arr, -1);
        }
        System.out.println(dfw(n * 2 - 1, v));
    }
    
    static int dfw(int idx, int v) {
        if (v < 0) {
            return Integer.MIN_VALUE;
        }
        if (idx < 0) {
            return 0;
        }
        if (dp[idx][v] < 0) {
            dp[idx][v] = dfw(idx - 1, v);
            if (idx % 2 == 1) {
                dp[idx][v] = Math.max(dp[idx][v], dfw(idx - 1, v - prices[idx / 2]) + c + values[idx / 2]);
            } else {
                dp[idx][v] = Math.max(dp[idx][v], dfw(idx, v - prices[idx / 2]) + values[idx / 2]);
            }
        }
        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