結果
問題 |
No.2026 Yet Another Knapsack Problem
|
ユーザー |
![]() |
提出日時 | 2025-06-12 16:50:37 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,928 bytes |
コンパイル時間 | 223 ms |
コンパイル使用メモリ | 82,412 KB |
実行使用メモリ | 251,912 KB |
最終ジャッジ日時 | 2025-06-12 16:52:17 |
合計ジャッジ時間 | 33,664 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
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 groups = [] for i in range(1, N+1): c_i = int(input[idx]) v_i = int(input[idx+1]) idx += 2 groups.append((i, c_i, v_i)) # Separate group 1 (i=1) group1 = groups[0] i1, c1, v1 = group1 assert i1 == 1 and c1 == N other_groups = groups[1:] # Initialize DP[x][s] = max value using x items from other groups, sum (i-1)*t_i = s INF = -1 << 60 dp = [[INF] * (N + 1) for _ in range(N + 1)] dp[0][0] = 0 for i, c_i, v_i in other_groups: weight = i value_diff = v_i - v1 excess_per = weight - 1 # Process this group # We need to consider taking t items from this group, t from 0 to c_i # For each possible t, update the DP # To handle this efficiently, we can use a temporary array and iterate backwards temp = [row[:] for row in dp] for t in range(1, c_i + 1): t_excess = t * excess_per t_value = t * value_diff for x in range(N, t - 1, -1): for s in range(N, t_excess - 1, -1): if temp[x - t][s - t_excess] != INF: if dp[x][s] < temp[x - t][s - t_excess] + t_value: dp[x][s] = temp[x - t][s - t_excess] + t_value # Precompute the maximum for each x and s # For each k, the answer is k*v1 + max(dp[x][s] where x <=k and s <= N -k) for k in range(1, N + 1): max_extra = INF for x in range(0, k + 1): max_s = N - k if max_s < 0: continue for s in range(0, max_s + 1): if dp[x][s] > max_extra: max_extra = dp[x][s] print(max_extra + k * v1) if __name__ == '__main__': main()