結果

問題 No.1583 Building Blocks
ユーザー lam6er
提出日時 2025-03-20 21:11:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 72 ms / 2,000 ms
コード長 755 bytes
コンパイル時間 199 ms
コンパイル使用メモリ 82,336 KB
実行使用メモリ 68,576 KB
最終ジャッジ日時 2025-03-20 21:13:18
合計ジャッジ時間 3,987 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
blocks = []
for _ in range(n):
    w, s = map(int, input().split())
    blocks.append((s + w, w, s))

# Sort blocks by the sum of s_i and w_i in ascending order
blocks.sort()

# Initialize DP array where dp[k] is the minimal cumulative weight for k blocks
dp = [float('inf')] * (n + 1)
dp[0] = 0

for sw, w, s in blocks:
    # Update in reverse to prevent using the same block multiple times
    for k in range(n, 0, -1):
        if dp[k-1] <= s:
            # Check if adding this block to k-1 blocks is better
            dp[k] = min(dp[k], dp[k-1] + w)

# Find the maximum number of blocks that can be stacked
max_blocks = 0
for k in range(n, 0, -1):
    if dp[k] < float('inf'):
        max_blocks = k
        break

print(max_blocks)
0