結果

問題 No.951 【本日限定】1枚頼むともう1枚無料!
ユーザー tenten
提出日時 2020-08-26 16:30:02
言語 Java
(openjdk 23)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,554 bytes
コンパイル時間 2,366 ms
コンパイル使用メモリ 78,300 KB
実行使用メモリ 251,604 KB
最終ジャッジ日時 2024-11-07 13:10:01
合計ジャッジ時間 57,673 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 41 TLE * 11
権限があれば一括ダウンロードができます

ソースコード

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