結果

問題 No.3329 Only the Rightest Choice is Right!!!
コンテスト
ユーザー elphe
提出日時 2025-08-12 11:42:43
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,097 bytes
コンパイル時間 361 ms
コンパイル使用メモリ 12,416 KB
実行使用メモリ 363,776 KB
最終ジャッジ日時 2025-11-02 16:30:55
合計ジャッジ時間 35,349 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 72 TLE * 2
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys

def solve():
    data = sys.stdin.read().split()
    it = iter(data)
    N = int(next(it))
    W = int(next(it))
    v = [int(next(it)) for _ in range(N)]
    w = [int(next(it)) for _ in range(N)]

    # dp[i][j] = max value using items i..N-1 with capacity j
    dp = [[0] * (W + 1) for _ in range(N + 1)]

    for i in range(N - 1, -1, -1):
        wi = w[i]
        vi = v[i]
        cur = dp[i]
        nxt = dp[i + 1]

        # j < wi: can't take item i
        for j in range(wi):
            cur[j] = nxt[j]

        # j >= wi: decide take or skip
        for j in range(wi, W + 1):
            without = nxt[j]
            with_item = nxt[j - wi] + vi
            cur[j] = with_item if with_item > without else without

    # reconstruct chosen items
    ans = []
    j = W
    for i in range(N):
        if dp[i][j] != dp[i + 1][j]:
            ans.append(i + 1)  # 1-based index
            j -= w[i]

    # output
    out = [str(len(ans))]
    if ans:
        out.append(" ".join(map(str, ans)))
    sys.stdout.write("\n".join(out))


if __name__ == "__main__":
    solve()
0