結果
問題 |
No.2026 Yet Another Knapsack Problem
|
ユーザー |
![]() |
提出日時 | 2025-06-12 20:30:14 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,354 bytes |
コンパイル時間 | 237 ms |
コンパイル使用メモリ | 81,732 KB |
実行使用メモリ | 88,080 KB |
最終ジャッジ日時 | 2025-06-12 20:31:14 |
合計ジャッジ時間 | 58,301 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 TLE * 1 -- * 11 |
ソースコード
import sys import math from collections import defaultdict def main(): sys.setrecursionlimit(1 << 25) N = int(sys.stdin.readline()) items = [] c1, v1 = 0, 0 for i in range(N): c, v = map(int, sys.stdin.readline().split()) if i == 0: c1, v1 = c, v else: items.append((i+1, c, v)) # weight is i+1 (since i starts from 0 in 0-based) # Initialize other_dp[j][w]: max value with j items (excluding weight 1) and total weight w other_dp = [[-float('inf')] * (N + 1) for _ in range(N + 1)] other_dp[0][0] = 0 for weight, cnt, val in items: # Binary split k = 1 remaining = cnt while remaining > 0: take = min(k, remaining) s = take s_weight = s * weight s_val = s * val # Update other_dp in reverse for j in range(N, -1, -1): for w in range(N, -1, -1): if other_dp[j][w] == -float('inf'): continue new_j = j + s new_w = w + s_weight if new_j > N or new_w > N: continue if other_dp[new_j][new_w] < other_dp[j][w] + s_val: other_dp[new_j][new_w] = other_dp[j][w] + s_val remaining -= take k *= 2 # Preprocess max_other[j][t] = max{ other_dp[j][w] for w <= t } max_other = [[-float('inf')] * (N + 1) for _ in range(N + 1)] for j in range(N + 1): current_max = -float('inf') for t in range(N + 1): if other_dp[j][t] > current_max: current_max = other_dp[j][t] max_other[j][t] = current_max # Now handle the weight 1 items ans = [-float('inf')] * (N + 1) for k in range(1, N + 1): max_val = -float('inf') max_j = min(k, N) for j in range(0, max_j + 1): t = k - j if t < 0 or t > c1: continue t_max = N - t if t_max < 0: continue current_val = max_other[j][t_max] + t * v1 if current_val > max_val: max_val = current_val ans[k] = max_val for k in range(1, N + 1): print(ans[k]) if __name__ == '__main__': main()