結果

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

ソースコード

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