結果

問題 No.527 ナップサック容量問題
ユーザー mbanmban
提出日時 2017-06-10 22:53:10
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 54 ms / 2,000 ms
コード長 1,629 bytes
コンパイル時間 2,118 ms
コンパイル使用メモリ 111,248 KB
実行使用メモリ 25,360 KB
最終ジャッジ日時 2023-10-24 20:36:33
合計ジャッジ時間 4,808 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
25,348 KB
testcase_01 AC 31 ms
25,348 KB
testcase_02 AC 30 ms
25,352 KB
testcase_03 AC 30 ms
25,348 KB
testcase_04 AC 31 ms
25,348 KB
testcase_05 AC 37 ms
25,352 KB
testcase_06 AC 48 ms
25,352 KB
testcase_07 AC 43 ms
25,360 KB
testcase_08 AC 35 ms
25,352 KB
testcase_09 AC 31 ms
25,352 KB
testcase_10 AC 48 ms
25,348 KB
testcase_11 AC 41 ms
25,352 KB
testcase_12 AC 38 ms
25,352 KB
testcase_13 AC 50 ms
25,352 KB
testcase_14 AC 35 ms
25,348 KB
testcase_15 AC 40 ms
25,360 KB
testcase_16 AC 32 ms
25,348 KB
testcase_17 AC 37 ms
25,348 KB
testcase_18 AC 39 ms
25,348 KB
testcase_19 AC 30 ms
25,348 KB
testcase_20 AC 36 ms
25,348 KB
testcase_21 AC 52 ms
25,360 KB
testcase_22 AC 44 ms
25,360 KB
testcase_23 AC 51 ms
25,348 KB
testcase_24 AC 33 ms
25,348 KB
testcase_25 AC 42 ms
25,348 KB
testcase_26 AC 41 ms
25,360 KB
testcase_27 AC 42 ms
25,360 KB
testcase_28 AC 39 ms
25,360 KB
testcase_29 AC 54 ms
25,360 KB
testcase_30 AC 33 ms
25,352 KB
testcase_31 AC 36 ms
25,348 KB
testcase_32 AC 37 ms
25,348 KB
testcase_33 AC 35 ms
25,348 KB
testcase_34 AC 36 ms
25,348 KB
testcase_35 AC 40 ms
25,348 KB
testcase_36 AC 31 ms
25,348 KB
testcase_37 AC 37 ms
25,348 KB
testcase_38 AC 39 ms
25,348 KB
testcase_39 AC 41 ms
25,348 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;

class Program
{
    static void Main()
    {
        new Magatro().Solve();
    }
}

class Magatro
{
    private int N;
    private int[] v, w;
    private int V;
    const int Max = 100000;
    private void Scan()
    {
        N = int.Parse(Console.ReadLine());
        v = new int[N];
        w = new int[N];
        for (int i = 0; i < N; i++)
        {
            var line = Console.ReadLine().Split(' ');
            v[i] = int.Parse(line[0]);
            w[i] = int.Parse(line[1]);
        }
        V = int.Parse(Console.ReadLine());
    }

    public void Solve()
    {
        Scan();
        var dp = (new int[Max + 1]).Select(i => -1).ToArray();
        dp[0] = 0;
        for (int i = 0; i < N; i++)
        {
            for (int j = Max; j >= 0; j--)
            {
                if (dp[j] != -1)
                {
                    dp[j + w[i]] = Math.Max(dp[j + w[i]], dp[j] + v[i]);
                }
            }
        }
        int min = -1;
        int max = -1;
        for (int i = 0; i <= Max; i++)
        {
            if (dp[i] == V && min == -1)
            {
                min = i;
            }
            if(dp[i]>V&&max == -1)
            {
                max = i - 1;
            }
        }


        Console.WriteLine(Math.Max(1,min));
        if (max == -1)
        {
            Console.WriteLine("inf");
        }
        else
        {
            Console.WriteLine(max);
        }
    }
}
0