結果

問題 No.617 Nafmo、買い出しに行く
ユーザー hiro_metal_corehiro_metal_core
提出日時 2017-12-19 01:12:52
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,163 bytes
コンパイル時間 106 ms
コンパイル使用メモリ 10,784 KB
実行使用メモリ 309,276 KB
最終ジャッジ日時 2023-08-22 06:09:56
合計ジャッジ時間 7,430 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,071 ms
98,740 KB
testcase_01 AC 20 ms
8,560 KB
testcase_02 AC 1,537 ms
121,332 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

# 以下は動的計画法で書いたアルゴリズム
dp = [[0 for i in range(K + 1)] for j in range(N + 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(ans_weight)
0