結果

問題 No.2364 Knapsack Problem
ユーザー 👑 kakel-sankakel-san
提出日時 2023-08-17 10:50:37
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 590 ms / 3,000 ms
コード長 1,804 bytes
コンパイル時間 1,693 ms
コンパイル使用メモリ 64,540 KB
実行使用メモリ 116,992 KB
最終ジャッジ日時 2023-08-17 10:50:47
合計ジャッジ時間 9,232 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
23,904 KB
testcase_01 AC 58 ms
23,832 KB
testcase_02 AC 59 ms
21,872 KB
testcase_03 AC 83 ms
19,800 KB
testcase_04 AC 60 ms
19,880 KB
testcase_05 AC 66 ms
23,900 KB
testcase_06 AC 59 ms
21,780 KB
testcase_07 AC 93 ms
31,964 KB
testcase_08 AC 58 ms
21,996 KB
testcase_09 AC 98 ms
35,808 KB
testcase_10 AC 69 ms
23,812 KB
testcase_11 AC 63 ms
23,948 KB
testcase_12 AC 541 ms
113,020 KB
testcase_13 AC 486 ms
114,936 KB
testcase_14 AC 518 ms
114,884 KB
testcase_15 AC 583 ms
114,856 KB
testcase_16 AC 514 ms
112,820 KB
testcase_17 AC 565 ms
114,924 KB
testcase_18 AC 481 ms
114,980 KB
testcase_19 AC 590 ms
114,924 KB
testcase_20 AC 570 ms
112,848 KB
testcase_21 AC 522 ms
116,992 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;
class Program
{
    static int NN => int.Parse(ReadLine());
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray();
    static string[] SList(long n) => Enumerable.Repeat(0, (int)n).Select(_ => ReadLine()).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var p = NList;
        var (n, m, w) = (p[0], p[1], p[2]);
        var a = NList;
        var b = NList;
        var c = NList;
        var d = NList;
        var bitmax = 1 << n + m;
        var dp = new long[bitmax][];
        for (var i = 0; i < dp.Length; ++i) dp[i] = Enumerable.Repeat(long.MinValue / 2, w + 1).ToArray();
        dp[0][0] = 0;
        for (var i = 0; i < dp.Length; ++i)
        {
            for (var j = 0; j < n + m; ++j)
            {
                var next = i | (1 << j);
                if (next == i) continue;
                for (var k = 0; k <= w; ++k)
                {
                    if (j < n)
                    {
                        if (k + a[j] > w) continue;
                        dp[next][k + a[j]] = Math.Max(dp[next][k + a[j]], dp[i][k] + b[j]);
                    }
                    else
                    {
                        if (k - c[j - n] < 0) continue;
                        dp[next][k - c[j - n]] = Math.Max(dp[next][k - c[j - n]], dp[i][k] - d[j - n]);
                    }
                }
            }
        }
        var ans = 0L;
        foreach (var dl in dp) foreach (var di in dl) ans = Math.Max(ans, di);
        WriteLine(ans);
    }
}
0