結果

問題 No.617 Nafmo、買い出しに行く
ユーザー hiro_metal_corehiro_metal_core
提出日時 2017-12-19 22:00:41
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 459 ms / 2,000 ms
コード長 1,232 bytes
コンパイル時間 172 ms
コンパイル使用メモリ 10,884 KB
実行使用メモリ 7,972 KB
最終ジャッジ日時 2023-08-22 07:04:17
合計ジャッジ時間 4,604 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
7,788 KB
testcase_01 AC 17 ms
7,800 KB
testcase_02 AC 17 ms
7,736 KB
testcase_03 AC 44 ms
7,812 KB
testcase_04 AC 15 ms
7,972 KB
testcase_05 AC 71 ms
7,820 KB
testcase_06 AC 459 ms
7,864 KB
testcase_07 AC 457 ms
7,840 KB
testcase_08 AC 16 ms
7,832 KB
testcase_09 AC 16 ms
7,868 KB
testcase_10 AC 454 ms
7,864 KB
testcase_11 AC 455 ms
7,936 KB
testcase_12 AC 453 ms
7,864 KB
testcase_13 AC 381 ms
7,804 KB
testcase_14 AC 452 ms
7,740 KB
testcase_15 AC 16 ms
7,836 KB
testcase_16 AC 16 ms
7,836 KB
testcase_17 AC 16 ms
7,760 KB
testcase_18 AC 16 ms
7,788 KB
testcase_19 AC 16 ms
7,756 KB
testcase_20 AC 16 ms
7,848 KB
testcase_21 AC 15 ms
7,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10000)

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