結果

問題 No.951 【本日限定】1枚頼むともう1枚無料!
ユーザー tentententen
提出日時 2023-01-31 19:52:59
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 3,009 bytes
コンパイル時間 4,986 ms
コンパイル使用メモリ 90,316 KB
実行使用メモリ 256,332 KB
最終ジャッジ日時 2023-09-13 12:19:26
合計ジャッジ時間 44,565 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,652 KB
testcase_01 AC 43 ms
49,716 KB
testcase_02 AC 44 ms
49,656 KB
testcase_03 AC 43 ms
48,024 KB
testcase_04 AC 44 ms
49,632 KB
testcase_05 AC 43 ms
49,932 KB
testcase_06 AC 46 ms
49,636 KB
testcase_07 AC 44 ms
50,116 KB
testcase_08 AC 45 ms
50,068 KB
testcase_09 AC 113 ms
58,660 KB
testcase_10 AC 90 ms
55,284 KB
testcase_11 AC 86 ms
56,048 KB
testcase_12 AC 316 ms
93,172 KB
testcase_13 AC 938 ms
224,520 KB
testcase_14 AC 249 ms
80,616 KB
testcase_15 AC 206 ms
80,564 KB
testcase_16 AC 270 ms
90,976 KB
testcase_17 AC 329 ms
111,680 KB
testcase_18 AC 44 ms
49,572 KB
testcase_19 AC 43 ms
49,652 KB
testcase_20 AC 43 ms
50,060 KB
testcase_21 AC 80 ms
55,020 KB
testcase_22 AC 61 ms
53,152 KB
testcase_23 AC 81 ms
52,364 KB
testcase_24 TLE -
testcase_25 AC 1,233 ms
251,944 KB
testcase_26 AC 1,492 ms
254,668 KB
testcase_27 AC 1,312 ms
251,636 KB
testcase_28 AC 880 ms
253,072 KB
testcase_29 AC 866 ms
253,688 KB
testcase_30 AC 44 ms
49,648 KB
testcase_31 AC 43 ms
49,812 KB
testcase_32 AC 73 ms
52,700 KB
testcase_33 AC 80 ms
55,284 KB
testcase_34 AC 90 ms
57,100 KB
testcase_35 TLE -
testcase_36 TLE -
testcase_37 AC 1,362 ms
253,776 KB
testcase_38 AC 1,279 ms
253,836 KB
testcase_39 AC 1,284 ms
253,360 KB
testcase_40 AC 1,429 ms
256,332 KB
testcase_41 AC 1,378 ms
254,304 KB
testcase_42 AC 1,281 ms
251,484 KB
testcase_43 AC 1,355 ms
251,452 KB
testcase_44 AC 1,315 ms
252,996 KB
testcase_45 AC 1,412 ms
254,324 KB
testcase_46 AC 871 ms
252,804 KB
testcase_47 AC 868 ms
253,292 KB
testcase_48 AC 876 ms
250,888 KB
testcase_49 AC 881 ms
251,120 KB
testcase_50 AC 871 ms
251,808 KB
testcase_51 AC 924 ms
251,892 KB
testcase_52 AC 1,270 ms
223,304 KB
testcase_53 AC 1,396 ms
254,084 KB
testcase_54 AC 455 ms
254,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static int[][] dp1;
    static int[][] dp2;
    static Pizza[] pizzas;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        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);
        dp1 = new int[n][k + 1];
        for (int[] arr : dp1) {
            Arrays.fill(arr, -1);
        }
        dp2 = new int[n][k + 1];
        for (int[] arr : dp2) {
            Arrays.fill(arr, -1);
        }
        System.out.println(dfw2(n - 1, k));
    }
    
    static int dfw2(int idx, int v) {
        if (v < 0) {
            return Integer.MIN_VALUE;
        }
        if (idx < 0) {
            return 0;
        }
        if (dp2[idx][v] < 0) {
            dp2[idx][v] = Math.max(dfw2(idx - 1, v), dfw1(idx - 1, v - pizzas[idx].price) + pizzas[idx].value);
        }
        return dp2[idx][v];
    }
    
    static int dfw1(int idx, int v) {
        if (v < 0) {
            return Integer.MIN_VALUE;
        }
        if (idx < 0) {
            return 0;
        }
        if (dp1[idx][v] < 0) {
            dp1[idx][v] = Math.max(dfw1(idx - 1, v), dfw2(idx - 1, v) + pizzas[idx].value);
        }
        return dp1[idx][v];
    }
    
    static class Pizza implements Comparable<Pizza> {
        int price;
        int value;
        
        public Pizza(int price, int value) {
            this.price = price;
            this.value = value;
        }
        
        public int compareTo(Pizza another) {
            return price - another.price;
        }
    }
}

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