結果

問題 No.771 しおり
ユーザー tenten
提出日時 2021-01-19 11:17:57
言語 Java
(openjdk 23)
結果
AC  
実行時間 728 ms / 2,000 ms
コード長 1,195 bytes
コンパイル時間 2,545 ms
コンパイル使用メモリ 77,380 KB
実行使用メモリ 62,308 KB
最終ジャッジ日時 2024-07-22 07:25:30
合計ジャッジ時間 18,274 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static int n;
    static int[] positions;
    static int[] sizes;
    static int[][] dp;
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		positions = new int[n];
		sizes = new int[n];
		for (int i = 0; i < n; i++) {
		    positions[i] = sc.nextInt();
		    sizes[i] = sc.nextInt();
		}
		dp = new int[n][1 << n];
		for (int[] arr : dp) {
		    Arrays.fill(arr, -1);
		}
		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 mask) {
	    if (dp[idx][mask] < 0) {
	        int next = ((1 << idx) ^ mask);
	        if (next == 0) {
	            dp[idx][mask] = 0;
	            return 0;
	        }
	        int min = Integer.MAX_VALUE;
	        for (int i = 0; i < n; i++) {
	            if (((1 << i) & next) == 0) {
	                continue;
	            }
	            int length = sizes[idx] - positions[idx] + positions[i];
	            min = Math.min(min, Math.max(length,dfw(i, next)));
	        }
	        dp[idx][mask] = min;
	    }
	    return dp[idx][mask];
	}
}
0