結果

問題 No.951 【本日限定】1枚頼むともう1枚無料!
ユーザー tentententen
提出日時 2020-08-26 16:33:55
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,791 bytes
コンパイル時間 1,900 ms
コンパイル使用メモリ 78,040 KB
実行使用メモリ 237,800 KB
最終ジャッジ日時 2024-04-25 03:28:42
合計ジャッジ時間 45,347 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
37,056 KB
testcase_01 AC 45 ms
37,048 KB
testcase_02 AC 49 ms
37,020 KB
testcase_03 AC 45 ms
36,868 KB
testcase_04 AC 44 ms
36,884 KB
testcase_05 AC 45 ms
36,996 KB
testcase_06 AC 46 ms
37,020 KB
testcase_07 AC 45 ms
37,164 KB
testcase_08 AC 45 ms
36,900 KB
testcase_09 AC 119 ms
47,212 KB
testcase_10 AC 85 ms
41,428 KB
testcase_11 AC 92 ms
41,848 KB
testcase_12 AC 302 ms
78,740 KB
testcase_13 AC 1,091 ms
206,760 KB
testcase_14 AC 218 ms
66,820 KB
testcase_15 AC 164 ms
66,160 KB
testcase_16 AC 232 ms
75,520 KB
testcase_17 AC 358 ms
95,968 KB
testcase_18 AC 44 ms
37,000 KB
testcase_19 AC 45 ms
36,920 KB
testcase_20 AC 47 ms
37,020 KB
testcase_21 AC 81 ms
40,860 KB
testcase_22 AC 74 ms
38,676 KB
testcase_23 AC 77 ms
39,692 KB
testcase_24 TLE -
testcase_25 AC 1,572 ms
237,304 KB
testcase_26 AC 1,746 ms
237,600 KB
testcase_27 AC 1,678 ms
237,268 KB
testcase_28 AC 1,097 ms
237,188 KB
testcase_29 AC 1,111 ms
237,296 KB
testcase_30 AC 47 ms
37,136 KB
testcase_31 AC 48 ms
36,908 KB
testcase_32 AC 74 ms
39,004 KB
testcase_33 AC 79 ms
40,456 KB
testcase_34 AC 88 ms
46,208 KB
testcase_35 TLE -
testcase_36 TLE -
testcase_37 AC 1,581 ms
236,672 KB
testcase_38 AC 1,551 ms
236,696 KB
testcase_39 AC 1,561 ms
236,800 KB
testcase_40 AC 1,753 ms
237,660 KB
testcase_41 AC 1,710 ms
237,224 KB
testcase_42 AC 1,718 ms
237,308 KB
testcase_43 AC 1,736 ms
237,800 KB
testcase_44 AC 1,733 ms
237,352 KB
testcase_45 AC 1,767 ms
237,268 KB
testcase_46 AC 1,132 ms
237,600 KB
testcase_47 AC 1,143 ms
237,124 KB
testcase_48 AC 1,150 ms
237,124 KB
testcase_49 AC 1,097 ms
237,560 KB
testcase_50 AC 1,114 ms
237,652 KB
testcase_51 AC 1,148 ms
237,592 KB
testcase_52 AC 1,369 ms
207,848 KB
testcase_53 AC 1,415 ms
237,404 KB
testcase_54 AC 350 ms
236,852 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static Pizza[] pizzas;
    static int[][][] dp;
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] first = br.readLine().split(" ", 2);
        int n = Integer.parseInt(first[0]);
        int k = Integer.parseInt(first[1]);
        pizzas = new Pizza[n];
        for (int i = 0; i < n; i++) {
            String[] line = br.readLine().split(" ", 2);
            pizzas[i] = new Pizza(Integer.parseInt(line[0]), Integer.parseInt(line[1]));
        }
        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