結果

問題 No.2364 Knapsack Problem
ユーザー MitarushiMitarushi
提出日時 2023-06-30 22:11:30
言語 Nim
(2.0.2)
結果
AC  
実行時間 5 ms / 3,000 ms
コード長 797 bytes
コンパイル時間 4,479 ms
コンパイル使用メモリ 72,820 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-21 16:14:25
合計ジャッジ時間 5,617 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 4 ms
4,380 KB
testcase_13 AC 5 ms
4,376 KB
testcase_14 AC 4 ms
4,376 KB
testcase_15 AC 5 ms
4,380 KB
testcase_16 AC 4 ms
4,380 KB
testcase_17 AC 4 ms
4,376 KB
testcase_18 AC 5 ms
4,376 KB
testcase_19 AC 5 ms
4,376 KB
testcase_20 AC 4 ms
4,376 KB
testcase_21 AC 4 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
/home/judge/data/code/Main.nim(1, 28) Warning: imported and not used: 'algorithm' [UnusedImport]

ソースコード

diff #

import strutils, sequtils, algorithm

var n, m, w: int
(n, m, w) = stdin.readLine.split.map(parseInt)
let k = n + m
var
    w_diff = newSeq[int]()
    v_diff = newSeq[int]()
w_diff.add(stdin.readLine.split.map(parseInt))
v_diff.add(stdin.readLine.split.map(parseInt))
w_diff.add(stdin.readLine.split.map(parseInt).mapIt(-it))
v_diff.add(stdin.readLine.split.map(parseInt).mapIt(-it))

let inf = 1 shl 30
var dp = newSeqWith[int](1 shl k, -inf)
dp[0] = 0

for i in 0..<(1 shl k):
    var w_sum = 0
    for j in 0..<k:
        if (i and (1 shl j)) != 0:
            w_sum += w_diff[j]
    for j in 0..<k:
        let next_w = w_sum + w_diff[j]
        if 0 <= next_w and next_w <= w and (i and (1 shl j)) == 0:
            dp[i or (1 shl j)] = max(dp[i or (1 shl j)], dp[i] + v_diff[j])

echo dp.max
0