結果

問題 No.617 Nafmo、買い出しに行く
ユーザー hiro_metal_corehiro_metal_core
提出日時 2017-12-19 21:57:27
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 559 ms / 2,000 ms
コード長 1,191 bytes
コンパイル時間 377 ms
コンパイル使用メモリ 12,416 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2024-05-09 12:57:42
合計ジャッジ時間 5,367 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
10,752 KB
testcase_01 AC 30 ms
10,624 KB
testcase_02 AC 31 ms
10,624 KB
testcase_03 AC 63 ms
10,624 KB
testcase_04 AC 30 ms
10,752 KB
testcase_05 AC 99 ms
10,624 KB
testcase_06 AC 559 ms
10,752 KB
testcase_07 AC 559 ms
10,752 KB
testcase_08 AC 33 ms
10,624 KB
testcase_09 AC 30 ms
10,624 KB
testcase_10 AC 556 ms
10,624 KB
testcase_11 AC 557 ms
10,752 KB
testcase_12 AC 556 ms
10,752 KB
testcase_13 AC 473 ms
10,624 KB
testcase_14 AC 554 ms
10,624 KB
testcase_15 AC 30 ms
10,752 KB
testcase_16 AC 29 ms
10,624 KB
testcase_17 AC 30 ms
10,752 KB
testcase_18 AC 30 ms
10,752 KB
testcase_19 AC 29 ms
10,624 KB
testcase_20 AC 30 ms
10,624 KB
testcase_21 AC 30 ms
10,752 KB
権限があれば一括ダウンロードができます

ソースコード

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


# 以下は動的計画法で書いたアルゴリズム(なぜかREだった)
"""
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))
"""
0