結果

問題 No.617 Nafmo、買い出しに行く
ユーザー hiro_metal_corehiro_metal_core
提出日時 2017-12-21 21:44:11
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 879 ms / 2,000 ms
コード長 1,225 bytes
コンパイル時間 165 ms
コンパイル使用メモリ 10,772 KB
実行使用メモリ 356,272 KB
最終ジャッジ日時 2023-08-22 20:53:35
合計ジャッジ時間 9,088 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 142 ms
53,116 KB
testcase_01 AC 137 ms
29,948 KB
testcase_02 AC 147 ms
63,180 KB
testcase_03 AC 236 ms
119,548 KB
testcase_04 AC 134 ms
36,264 KB
testcase_05 AC 349 ms
187,996 KB
testcase_06 AC 337 ms
356,272 KB
testcase_07 AC 879 ms
356,208 KB
testcase_08 AC 131 ms
29,244 KB
testcase_09 AC 130 ms
29,296 KB
testcase_10 AC 717 ms
263,916 KB
testcase_11 AC 852 ms
311,684 KB
testcase_12 AC 658 ms
180,196 KB
testcase_13 AC 644 ms
119,528 KB
testcase_14 AC 775 ms
148,392 KB
testcase_15 AC 134 ms
30,096 KB
testcase_16 AC 130 ms
30,572 KB
testcase_17 AC 128 ms
29,088 KB
testcase_18 AC 130 ms
29,300 KB
testcase_19 AC 130 ms
29,296 KB
testcase_20 AC 128 ms
29,132 KB
testcase_21 AC 130 ms
29,288 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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)))
0