結果

問題 No.771 しおり
ユーザー lam6er
提出日時 2025-03-20 20:18:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,098 ms / 2,000 ms
コード長 1,066 bytes
コンパイル時間 173 ms
コンパイル使用メモリ 82,440 KB
実行使用メモリ 181,200 KB
最終ジャッジ日時 2025-03-20 20:19:58
合計ジャッジ時間 15,657 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
A = []
B = []
for _ in range(n):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)

INF = float('inf')
dp = [[INF] * n for _ in range(1 << n)]

# Initialize for single book
for i in range(n):
    dp[1 << i][i] = 0

# Process all masks in increasing order of bits
for mask in range(1 << n):
    # Iterate over each possible last book in the mask
    for last in range(n):
        if not (mask & (1 << last)):
            continue  # last is not in the current mask
        current_max = dp[mask][last]
        if current_max == INF:
            continue
        # Try to add each possible next book
        for j in range(n):
            if mask & (1 << j):
                continue  # j is already in mask
            new_mask = mask | (1 << j)
            new_gap = B[last] - A[last] + A[j]
            new_max = max(current_max, new_gap)
            if new_max < dp[new_mask][j]:
                dp[new_mask][j] = new_max

full_mask = (1 << n) - 1
min_ugliness = min(dp[full_mask][last] for last in range(n))
print(min_ugliness)
0