結果

問題 No.951 【本日限定】1枚頼むともう1枚無料!
ユーザー tentententen
提出日時 2022-08-29 18:53:11
言語 Java21
(openjdk 21)
結果
MLE  
実行時間 -
コード長 2,340 bytes
コンパイル時間 2,541 ms
コンパイル使用メモリ 78,160 KB
実行使用メモリ 718,548 KB
最終ジャッジ日時 2024-04-24 09:03:47
合計ジャッジ時間 9,257 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
42,884 KB
testcase_01 AC 52 ms
37,112 KB
testcase_02 AC 55 ms
37,100 KB
testcase_03 AC 53 ms
37,188 KB
testcase_04 AC 56 ms
36,852 KB
testcase_05 AC 55 ms
37,176 KB
testcase_06 AC 54 ms
36,840 KB
testcase_07 AC 53 ms
36,952 KB
testcase_08 AC 55 ms
37,272 KB
testcase_09 AC 188 ms
55,744 KB
testcase_10 AC 149 ms
51,260 KB
testcase_11 AC 148 ms
51,540 KB
testcase_12 AC 1,016 ms
179,596 KB
testcase_13 MLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static Pizza[] pizzas;
    static int[][][] dp;
    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);
        dp = new int[n][k + 1][2];
        for (int[][] arr1 : dp) {
            for (int[] arr2 : arr1) {
                Arrays.fill(arr2, -1);
            }
        }
        System.out.println(dfw(n - 1, k, 0));
    }
    
    static int dfw(int idx, int value, int type) {
        if (value < 0) {
            return Integer.MIN_VALUE;
        }
        if (idx < 0) {
            return 0;
        }
        if (dp[idx][value][type] < 0) {
            if (type == 0) {
                dp[idx][value][type] = Math.max(dfw(idx - 1, value, type), dfw(idx - 1, value - pizzas[idx].price, 1) + pizzas[idx].value);
            } else {
                dp[idx][value][type] = Math.max(dfw(idx - 1, value, type), dfw(idx - 1, value, 0) + pizzas[idx].value);
            }
        }
        return dp[idx][value][type];
    }
    
    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 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 String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0