結果

問題 No.1947 質より種類数
ユーザー tentententen
提出日時 2023-08-08 13:43:50
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,465 ms / 2,000 ms
コード長 2,543 bytes
コンパイル時間 2,308 ms
コンパイル使用メモリ 85,504 KB
実行使用メモリ 252,124 KB
最終ジャッジ日時 2024-04-26 06:28:15
合計ジャッジ時間 16,377 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
50,568 KB
testcase_01 AC 49 ms
50,180 KB
testcase_02 AC 50 ms
50,584 KB
testcase_03 AC 52 ms
50,492 KB
testcase_04 AC 80 ms
51,812 KB
testcase_05 AC 76 ms
51,608 KB
testcase_06 AC 75 ms
51,092 KB
testcase_07 AC 79 ms
51,844 KB
testcase_08 AC 80 ms
51,400 KB
testcase_09 AC 105 ms
57,696 KB
testcase_10 AC 87 ms
54,900 KB
testcase_11 AC 132 ms
62,032 KB
testcase_12 AC 115 ms
57,676 KB
testcase_13 AC 82 ms
54,764 KB
testcase_14 AC 365 ms
111,228 KB
testcase_15 AC 240 ms
85,704 KB
testcase_16 AC 504 ms
130,304 KB
testcase_17 AC 670 ms
186,076 KB
testcase_18 AC 1,138 ms
237,488 KB
testcase_19 AC 1,027 ms
227,416 KB
testcase_20 AC 728 ms
184,560 KB
testcase_21 AC 926 ms
228,620 KB
testcase_22 AC 124 ms
61,876 KB
testcase_23 AC 436 ms
131,636 KB
testcase_24 AC 85 ms
40,204 KB
testcase_25 AC 80 ms
39,604 KB
testcase_26 AC 60 ms
37,904 KB
testcase_27 AC 75 ms
38,960 KB
testcase_28 AC 75 ms
38,944 KB
testcase_29 AC 1,027 ms
238,280 KB
testcase_30 AC 1,134 ms
251,880 KB
testcase_31 AC 51 ms
50,504 KB
testcase_32 AC 50 ms
50,592 KB
testcase_33 AC 53 ms
51,172 KB
testcase_34 AC 1,465 ms
251,616 KB
testcase_35 AC 330 ms
248,832 KB
testcase_36 AC 1,109 ms
252,124 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static int[] values;
    static int[] weights;
    static int[][] dp;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int v = sc.nextInt();
        int c = sc.nextInt();
        values = new int[n * 2];
        weights = new int[n * 2];
        for (int i = 0; i < n; i++) {
            weights[i * 2] = sc.nextInt();
            weights[i * 2 + 1] = weights[i * 2];
            values[i * 2] = sc.nextInt();
            values[i * 2 + 1] = values[i * 2] + c;
        }
        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) {
            if (idx % 2 == 0) {
                dp[idx][v] = Math.max(dfw(idx - 1, v), dfw(idx, v - weights[idx]) + values[idx]);
            } else {
                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