結果

問題 No.54 Happy Hallowe'en
ユーザー tentententen
提出日時 2021-03-03 20:14:02
言語 Java21
(openjdk 21)
結果
AC  
実行時間 3,996 ms / 5,000 ms
コード長 1,744 bytes
コンパイル時間 2,528 ms
コンパイル使用メモリ 77,948 KB
実行使用メモリ 511,540 KB
最終ジャッジ日時 2024-04-14 12:05:49
合計ジャッジ時間 22,986 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
50,452 KB
testcase_01 AC 53 ms
50,128 KB
testcase_02 AC 53 ms
50,248 KB
testcase_03 AC 53 ms
50,276 KB
testcase_04 AC 581 ms
183,304 KB
testcase_05 AC 987 ms
218,084 KB
testcase_06 AC 1,515 ms
334,272 KB
testcase_07 AC 2,284 ms
403,572 KB
testcase_08 AC 3,011 ms
407,208 KB
testcase_09 AC 3,996 ms
508,276 KB
testcase_10 AC 53 ms
50,132 KB
testcase_11 AC 53 ms
50,328 KB
testcase_12 AC 2,775 ms
511,540 KB
testcase_13 AC 3,729 ms
510,476 KB
testcase_14 AC 52 ms
50,248 KB
testcase_15 AC 52 ms
49,844 KB
testcase_16 AC 58 ms
50,224 KB
testcase_17 AC 58 ms
50,000 KB
testcase_18 AC 55 ms
50,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static House[] houses;
    static int[][] dp;
    static int[] maxes;
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        houses = new House[n];
        for (int i = 0; i < n; i++) {
            String[] line = br.readLine().split(" ", 2);
            houses[i] = new House(Integer.parseInt(line[0]), Integer.parseInt(line[1]));
        }
        Arrays.sort(houses);
        maxes = new int[n];
        maxes[0] = houses[0].limit;
        for (int i = 1; i < n; i++) {
            maxes[i] = Math.max(maxes[i - 1], houses[i].limit);
        }
        dp = new int[n][10001];
        for (int[] arr : dp) {
            Arrays.fill(arr, -1);
        }
        System.out.println(dfw(n - 1, 0));
    }
    
    static int dfw(int idx, int value) {
        if (idx < 0) {
            return 0;
        }
        if (value > maxes[idx]) {
            return 0;
        }
        if (dp[idx][value] < 0) {
            dp[idx][value] = dfw(idx - 1, value);
            if (houses[idx].limit > value) {
                dp[idx][value] = Math.max(dp[idx][value], dfw(idx - 1, value + houses[idx].gain) + houses[idx].gain);
            }
        }
        return dp[idx][value];
    }
    
    static class House implements Comparable<House> {
        int gain;
        int limit;
        
        public House(int gain, int limit) {
            this.gain = gain;
            this.limit = limit;
        }
        
        public int compareTo(House another) {
            return another.limit + another.gain - limit - gain;
        }
    }
}
0