結果
| 問題 | No.617 Nafmo、買い出しに行く |
| コンテスト | |
| ユーザー |
hiro_metal_core
|
| 提出日時 | 2017-12-21 21:44:11 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
AC
|
| 実行時間 | 1,302 ms / 2,000 ms |
| コード長 | 1,225 bytes |
| 記録 | |
| コンパイル時間 | 253 ms |
| コンパイル使用メモリ | 12,544 KB |
| 実行使用メモリ | 372,768 KB |
| 最終ジャッジ日時 | 2024-12-17 22:40:59 |
| 合計ジャッジ時間 | 16,974 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 20 |
ソースコード
N, K = list(map(int, input().split()))
A = [int(input()) for _ in range(N)]
# 以下は全探索のアルゴリズム(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
memo = -1*np.ones((N+1, K+1))
def do_dp_memo(item_index, rest_weight):
if memo[item_index, rest_weight] != -1:
return memo[item_index, rest_weight]
else:
if item_index == N:
res = 0
elif rest_weight < A[item_index]:
res = do_dp_memo(item_index + 1, rest_weight)
else:
res1 = do_dp_memo(item_index + 1, rest_weight)
res2 = do_dp_memo(item_index + 1, rest_weight - A[item_index]) + A[item_index]
res = max(res1, res2)
memo[item_index, rest_weight] = res
return res
print(int(do_dp_memo(0, K)))
hiro_metal_core