結果
問題 |
No.2026 Yet Another Knapsack Problem
|
ユーザー |
![]() |
提出日時 | 2025-06-12 21:03:51 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,525 bytes |
コンパイル時間 | 192 ms |
コンパイル使用メモリ | 81,912 KB |
実行使用メモリ | 80,272 KB |
最終ジャッジ日時 | 2025-06-12 21:05:49 |
合計ジャッジ時間 | 34,031 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 35 TLE * 1 -- * 6 |
ソースコード
def main(): import sys input = sys.stdin.read().split() idx = 0 N = int(input[idx]) idx += 1 items = [] for i in range(1, N+1): c = int(input[idx]) v = int(input[idx+1]) idx += 2 items.append((i, c, v)) # Separate the first item (type 1) type1 = items[0] items = items[1:] # Initialize DP for other items INF = -10**18 dp_other = [[INF] * (N + 1) for _ in range(N + 1)] dp_other[0][0] = 0 for i in range(len(items)): item_i = items[i] weight_i = item_i[0] count_i = item_i[1] value_i = item_i[2] for k in range(N, -1, -1): for w in range(N, -1, -1): if dp_other[k][w] == INF: continue max_x = min(count_i, (N - w) // weight_i) for x in range(1, max_x + 1): new_k = k + x new_w = w + x * weight_i if new_k > N or new_w > N: break new_val = dp_other[k][w] + x * value_i if new_val > dp_other[new_k][new_w]: dp_other[new_k][new_w] = new_val # Precompute max_v_other max_v_other = [[INF] * (N + 1) for _ in range(N + 1)] for k in range(N+1): current_max = INF for w in range(N+1): if dp_other[k][w] > current_max: current_max = dp_other[k][w] max_v_other[k][w] = current_max c1, v1 = type1[1], type1[2] # Compute the answer for each k answers = [] for k in range(1, N+1): max_val = INF max_t = min(k, c1, N) for t in range(0, max_t + 1): k_remaining = k - t w_remaining = N - t if k_remaining < 0 or w_remaining < 0: continue if k_remaining > N or w_remaining > N: continue current_val = t * v1 if k_remaining > N: continue if w_remaining > N: w_remaining_clipped = N else: w_remaining_clipped = w_remaining if k_remaining <= N and w_remaining_clipped >= 0: current_val += max_v_other[k_remaining][w_remaining_clipped] if current_val > max_val: max_val = current_val answers.append(max_val) for ans in answers: print(ans) if __name__ == '__main__': main()