結果

問題 No.617 Nafmo、買い出しに行く
ユーザー hiro_metal_corehiro_metal_core
提出日時 2017-12-21 21:50:19
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,229 bytes
コンパイル時間 278 ms
コンパイル使用メモリ 10,940 KB
実行使用メモリ 336,276 KB
最終ジャッジ日時 2023-08-22 20:55:24
合計ジャッジ時間 14,894 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 163 ms
32,160 KB
testcase_01 AC 18 ms
8,340 KB
testcase_02 AC 223 ms
42,208 KB
testcase_03 AC 616 ms
100,324 KB
testcase_04 AC 56 ms
15,060 KB
testcase_05 AC 1,096 ms
170,624 KB
testcase_06 AC 1,979 ms
336,240 KB
testcase_07 AC 1,976 ms
336,276 KB
testcase_08 AC 15 ms
7,760 KB
testcase_09 AC 15 ms
7,792 KB
testcase_10 AC 1,778 ms
254,188 KB
testcase_11 TLE -
testcase_12 AC 1,272 ms
170,816 KB
testcase_13 AC 911 ms
109,188 KB
testcase_14 AC 1,170 ms
141,648 KB
testcase_15 AC 20 ms
9,068 KB
testcase_16 AC 24 ms
9,664 KB
testcase_17 AC 15 ms
7,964 KB
testcase_18 AC 16 ms
7,884 KB
testcase_19 AC 15 ms
7,824 KB
testcase_20 AC 16 ms
7,780 KB
testcase_21 AC 16 ms
7,836 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))
"""
# 以下は動的計画法(メモ化)のアルゴリズム
memo = [[-1 for _ in range(K+1)] for _ in range(N+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