結果

問題 No.951 【本日限定】1枚頼むともう1枚無料!
ユーザー tentententen
提出日時 2020-08-26 16:30:02
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,554 bytes
コンパイル時間 2,226 ms
コンパイル使用メモリ 77,920 KB
実行使用メモリ 241,100 KB
最終ジャッジ日時 2024-04-25 03:27:21
合計ジャッジ時間 15,399 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 113 ms
40,276 KB
testcase_01 AC 120 ms
40,944 KB
testcase_02 AC 137 ms
41,936 KB
testcase_03 AC 115 ms
40,208 KB
testcase_04 AC 138 ms
41,060 KB
testcase_05 AC 124 ms
40,992 KB
testcase_06 AC 133 ms
41,856 KB
testcase_07 AC 119 ms
40,168 KB
testcase_08 AC 134 ms
41,368 KB
testcase_09 AC 230 ms
48,024 KB
testcase_10 AC 199 ms
44,660 KB
testcase_11 AC 203 ms
47,692 KB
testcase_12 AC 480 ms
93,328 KB
testcase_13 AC 1,412 ms
225,296 KB
testcase_14 AC 374 ms
65,448 KB
testcase_15 AC 344 ms
72,228 KB
testcase_16 AC 414 ms
78,860 KB
testcase_17 AC 581 ms
104,528 KB
testcase_18 AC 122 ms
40,860 KB
testcase_19 AC 114 ms
40,432 KB
testcase_20 AC 111 ms
39,672 KB
testcase_21 AC 182 ms
44,316 KB
testcase_22 AC 179 ms
42,876 KB
testcase_23 AC 182 ms
43,280 KB
testcase_24 TLE -
testcase_25 AC 1,879 ms
240,648 KB
testcase_26 TLE -
testcase_27 TLE -
testcase_28 AC 1,367 ms
240,876 KB
testcase_29 AC 1,354 ms
240,732 KB
testcase_30 AC 125 ms
41,136 KB
testcase_31 AC 130 ms
41,452 KB
testcase_32 AC 168 ms
42,616 KB
testcase_33 AC 184 ms
43,868 KB
testcase_34 AC 177 ms
48,240 KB
testcase_35 TLE -
testcase_36 TLE -
testcase_37 AC 1,921 ms
240,800 KB
testcase_38 AC 1,847 ms
240,940 KB
testcase_39 AC 1,832 ms
239,576 KB
testcase_40 TLE -
testcase_41 TLE -
testcase_42 TLE -
testcase_43 TLE -
testcase_44 TLE -
testcase_45 TLE -
testcase_46 AC 1,373 ms
240,808 KB
testcase_47 AC 1,343 ms
240,824 KB
testcase_48 AC 1,390 ms
240,812 KB
testcase_49 AC 1,331 ms
239,496 KB
testcase_50 AC 1,350 ms
240,824 KB
testcase_51 AC 1,388 ms
240,896 KB
testcase_52 AC 1,600 ms
220,544 KB
testcase_53 AC 1,686 ms
240,500 KB
testcase_54 AC 580 ms
241,100 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static Pizza[] pizzas;
    static int[][][] dp;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        pizzas = new Pizza[n];
        for (int i = 0; i < n; i++) {
            pizzas[i] = new Pizza(sc.nextInt(), sc.nextInt());
        }
        Arrays.sort(pizzas);
        dp = new int[2][n][k + 1];
        for (int[][] arr1 : dp) {
            for (int[] arr : arr1) {
                Arrays.fill(arr, -1);
            }
        }
        System.out.println(dfw(0, n - 1, k));
    }
    
    static int dfw(int type, int idx, int cost) {
        if (cost < 0) {
            return Integer.MIN_VALUE;
        }
        if (idx < 0) {
            return 0;
        }
        if (dp[type][idx][cost] == -1) {
             if (type == 0) {
                dp[type][idx][cost] = Math.max(dfw(0, idx - 1, cost), dfw(1, idx - 1, cost - pizzas[idx].cost) + pizzas[idx].value);
            } else {
                dp[type][idx][cost] = Math.max(dfw(1, idx - 1, cost), dfw(0, idx - 1, cost) + pizzas[idx].value);
            }
        }
        return dp[type][idx][cost];
    }
    
    static class Pizza implements Comparable<Pizza> {
        int cost;
        int value;
        
        public Pizza(int cost, int value) {
            this.cost = cost;
            this.value = value;
        }
        
        public int compareTo(Pizza another) {
            return cost - another.cost;
        }
    }
}
0