結果

問題 No.54 Happy Hallowe'en
ユーザー tentententen
提出日時 2021-03-03 20:07:45
言語 Java21
(openjdk 21)
結果
AC  
実行時間 4,550 ms / 5,000 ms
コード長 1,539 bytes
コンパイル時間 2,187 ms
コンパイル使用メモリ 78,576 KB
実行使用メモリ 511,152 KB
最終ジャッジ日時 2024-04-14 11:49:30
合計ジャッジ時間 25,820 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
50,264 KB
testcase_01 AC 53 ms
50,428 KB
testcase_02 AC 54 ms
50,252 KB
testcase_03 AC 53 ms
50,420 KB
testcase_04 AC 695 ms
185,620 KB
testcase_05 AC 1,185 ms
218,176 KB
testcase_06 AC 1,820 ms
334,080 KB
testcase_07 AC 2,727 ms
405,508 KB
testcase_08 AC 3,333 ms
412,992 KB
testcase_09 AC 4,211 ms
509,556 KB
testcase_10 AC 54 ms
50,496 KB
testcase_11 AC 53 ms
50,440 KB
testcase_12 AC 3,106 ms
511,152 KB
testcase_13 AC 4,550 ms
509,636 KB
testcase_14 AC 53 ms
50,404 KB
testcase_15 AC 54 ms
49,920 KB
testcase_16 AC 59 ms
50,416 KB
testcase_17 AC 58 ms
50,328 KB
testcase_18 AC 55 ms
50,460 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static House[] houses;
    static int[][] dp;
    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);
        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 > 10000) {
            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