結果

問題 No.771 しおり
ユーザー htensai
提出日時 2020-01-29 13:44:06
言語 Java
(openjdk 23)
結果
AC  
実行時間 455 ms / 2,000 ms
コード長 1,383 bytes
コンパイル時間 2,183 ms
コンパイル使用メモリ 77,716 KB
実行使用メモリ 72,784 KB
最終ジャッジ日時 2024-07-22 07:21:18
合計ジャッジ時間 10,146 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static int[] befores;
    static int[] afters;
    static int[][] dp;
    static int n;
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        n = Integer.parseInt(br.readLine());
        befores = new int[n];
        afters = new int[n];
        for (int i = 0; i < n; i++) {
            String[] line = br.readLine().split(" ", 2);
            befores[i] = Integer.parseInt(line[0]);
            afters[i] = Integer.parseInt(line[1]) - befores[i];
        }
        dp = new int[n + 1][1 << n];
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < n; i++) {
            min = Math.min(min, dfw(i, (1 << n) - 1));
        }
        System.out.println(min);
    }
    
    static int dfw(int idx, int key) {
        if (key == 1 << idx) {
            return 0;
        }
        if (dp[idx][key] != 0) {
            return dp[idx][key];
        }
        int min = Integer.MAX_VALUE;
        int next = key ^ (1 << idx);
        for (int i = 0; i < n; i++) {
            int x = 1 << i;
            if ((next & x) == 0) {
                continue;
            }
            min = Math.min(min, Math.max(dfw(i, next), afters[i] + befores[idx]));
        }
        dp[idx][key] = min;
        return min;
    }
}
0