結果
問題 | No.527 ナップサック容量問題 |
ユーザー |
|
提出日時 | 2017-09-09 15:09:22 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 186 ms / 2,000 ms |
コード長 | 1,180 bytes |
コンパイル時間 | 4,337 ms |
コンパイル使用メモリ | 78,760 KB |
実行使用メモリ | 42,476 KB |
最終ジャッジ日時 | 2024-11-07 09:32:21 |
合計ジャッジ時間 | 11,987 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 37 |
ソースコード
import java.io.*; import java.util.*; public class Main_yukicoder527 { private static Scanner sc; private static Printer pr; private static void solve() { final int INF = Integer.MAX_VALUE / 2; int n = sc.nextInt(); int[] v = new int[n]; int[] w = new int[n]; for (int i = 0; i < n; i++) { v[i] = sc.nextInt(); w[i] = sc.nextInt(); } int vv = sc.nextInt(); int[] dp = new int[100_000 + 1]; Arrays.fill(dp, INF); dp[0] = 0; for (int i = 0; i < n; i++) { for (int j = 100_000 - v[i]; j >= 0; j--) { if (dp[j] == INF) { continue; } dp[j + v[i]] = Math.min(dp[j + v[i]], dp[j] + w[i]); } } int max = INF; for (int i = vv + 1; i <= 100_000; i++) { max = Math.min(max, dp[i]); } pr.println(Math.max(1, dp[vv])); if (max == INF) { pr.println("inf"); } else { pr.println(max - 1); } } // --------------------------------------------------- public static void main(String[] args) { sc = new Scanner(System.in); pr = new Printer(System.out); solve(); pr.close(); sc.close(); } private static class Printer extends PrintWriter { Printer(PrintStream out) { super(out); } } }