結果
| 問題 |
No.617 Nafmo、買い出しに行く
|
| コンテスト | |
| ユーザー |
hiro_metal_core
|
| 提出日時 | 2017-12-19 21:38:07 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,163 bytes |
| コンパイル時間 | 444 ms |
| コンパイル使用メモリ | 12,672 KB |
| 実行使用メモリ | 107,484 KB |
| 最終ジャッジ日時 | 2024-12-16 02:11:19 |
| 合計ジャッジ時間 | 44,859 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 9 TLE * 11 |
ソースコード
N, K = list(map(int, input().split()))
A = []
for n in range(N):
A.append(int(input()))
# 以下は全探索のアルゴリズム(TLEだった)
"""
def bin_search(item_index, rest_weight):
if item_index == N:
return 0
elif rest_weight < A[item_index]:
return bin_search(item_index + 1, rest_weight)
else:
res1 = bin_search(item_index + 1, rest_weight)
res2 = bin_search(item_index + 1, rest_weight - A[item_index]) + A[item_index]
return max(res1, res2)
print(bin_search(0, K))
"""
# 以下は動的計画法で書いたアルゴリズム
import numpy as np
dp = np.zeros((N + 1, K + 1))
def do_dp():
for item_index in range(N - 1, -1, -1):
for rest_weight in range(K + 1):
if rest_weight < A[item_index]:
dp[item_index][rest_weight] = dp[item_index + 1][rest_weight]
else:
dp[item_index][rest_weight] = \
max(dp[item_index + 1][rest_weight],
dp[item_index + 1][rest_weight - A[item_index]] + A[item_index])
return dp[0][K]
ans_weight = do_dp()
print(int(ans_weight))
hiro_metal_core