結果

問題 No.54 Happy Hallowe'en
ユーザー tentententen
提出日時 2021-03-03 19:59:11
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 1,304 bytes
コンパイル時間 2,431 ms
コンパイル使用メモリ 77,528 KB
実行使用メモリ 448,944 KB
最終ジャッジ日時 2024-04-14 11:40:11
合計ジャッジ時間 14,303 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 133 ms
54,092 KB
testcase_01 AC 136 ms
54,008 KB
testcase_02 AC 134 ms
54,104 KB
testcase_03 AC 133 ms
53,844 KB
testcase_04 AC 822 ms
185,612 KB
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 AC 131 ms
54,012 KB
testcase_11 AC 132 ms
54,256 KB
testcase_12 AC 3,892 ms
448,064 KB
testcase_13 RE -
testcase_14 AC 132 ms
53,824 KB
testcase_15 AC 134 ms
53,868 KB
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static House[] houses;
    static int[][] dp;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        houses = new House[n];
        for (int i = 0; i < n; i++) {
            houses[i] = new House(sc.nextInt(), sc.nextInt());
        }
        Arrays.sort(houses);
        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 (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