結果

問題 No.527 ナップサック容量問題
ユーザー schwarzahlschwarzahl
提出日時 2017-06-10 05:21:12
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,296 bytes
コンパイル時間 5,257 ms
コンパイル使用メモリ 79,460 KB
実行使用メモリ 77,000 KB
最終ジャッジ日時 2023-10-24 10:05:00
合計ジャッジ時間 17,362 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 140 ms
57,716 KB
testcase_01 WA -
testcase_02 AC 141 ms
57,484 KB
testcase_03 AC 143 ms
57,364 KB
testcase_04 WA -
testcase_05 AC 470 ms
71,740 KB
testcase_06 AC 1,244 ms
74,924 KB
testcase_07 AC 887 ms
73,736 KB
testcase_08 AC 397 ms
68,256 KB
testcase_09 AC 140 ms
57,576 KB
testcase_10 AC 1,219 ms
74,660 KB
testcase_11 AC 645 ms
72,188 KB
testcase_12 AC 488 ms
72,736 KB
testcase_13 AC 1,409 ms
75,176 KB
testcase_14 AC 396 ms
70,436 KB
testcase_15 AC 621 ms
72,252 KB
testcase_16 AC 140 ms
57,024 KB
testcase_17 AC 469 ms
71,792 KB
testcase_18 AC 541 ms
71,752 KB
testcase_19 AC 142 ms
57,392 KB
testcase_20 AC 382 ms
63,916 KB
testcase_21 AC 1,529 ms
75,308 KB
testcase_22 AC 911 ms
73,304 KB
testcase_23 AC 1,252 ms
73,980 KB
testcase_24 AC 246 ms
61,080 KB
testcase_25 WA -
testcase_26 AC 665 ms
73,324 KB
testcase_27 AC 735 ms
72,488 KB
testcase_28 WA -
testcase_29 AC 1,687 ms
77,000 KB
testcase_30 AC 145 ms
57,668 KB
testcase_31 AC 161 ms
57,728 KB
testcase_32 AC 166 ms
57,640 KB
testcase_33 AC 167 ms
57,620 KB
testcase_34 AC 176 ms
57,676 KB
testcase_35 AC 580 ms
72,180 KB
testcase_36 WA -
testcase_37 AC 455 ms
71,340 KB
testcase_38 AC 519 ms
71,756 KB
testcase_39 AC 487 ms
72,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

public class Main {
	public static void main(String[] args){
		Main main = new Main();
		main.solveC();
	}

	private void solveC() {
		Scanner sc = new Scanner(System.in);

		final int N = sc.nextInt();
		TreeMap<Integer, Integer> map = new TreeMap<>();
		map.put(0, 0);
		for (int i = 0; i < N; i++) {
			final int value = sc.nextInt();
			final int weight = sc.nextInt();
			TreeMap<Integer, Integer> nextMap = new TreeMap<>(map);
			for (Map.Entry<Integer, Integer> e : map.entrySet()) {
				final int prevWeight = e.getKey();
				final int prevValue = e.getValue();
				int nextWeight = prevWeight + weight;
				int nextValue = prevValue + value;
				if (!map.containsKey(nextWeight) || nextValue > map.get(nextWeight)) {
					nextMap.put(nextWeight, nextValue);
				}
			}
			map = nextMap;
		}
		final int V = sc.nextInt();

		Integer min = null;
		Integer max = null;
		for (Map.Entry<Integer, Integer> e : map.entrySet()) {
			final int weight = e.getKey();
			final int value = e.getValue();
			if (min == null && value == V) {
				min = weight;
			}
			if (value > V) {
				max = weight;
				break;
			}
		}

		System.out.println(min == null ? 1 : min);
		System.out.println(max == null ? "inf" : max - 1);
	}
}
0