結果
問題 | No.247 線形計画問題もどき |
ユーザー |
![]() |
提出日時 | 2021-12-03 08:20:50 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 231 ms / 2,000 ms |
コード長 | 1,775 bytes |
コンパイル時間 | 3,193 ms |
コンパイル使用メモリ | 77,708 KB |
実行使用メモリ | 90,896 KB |
最終ジャッジ日時 | 2024-07-05 12:24:42 |
合計ジャッジ時間 | 7,095 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 23 |
ソースコード
import java.io.*; import java.util.*; public class Main { static int[][] dp; static int[] values; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int c = sc.nextInt(); int n = sc.nextInt(); values = new int[n]; for (int i = 0; i < n; i++) { values[i] = sc.nextInt(); } dp = new int[n][c + 1]; for (int[] arr : dp) { Arrays.fill(arr, -1); } int ans = dfw(n - 1, c); if (ans >= Integer.MAX_VALUE / 2) { System.out.println(-1); } else { System.out.println(ans); } } static int dfw(int idx, int c) { if (c == 0) { return 0; } if (idx < 0) { return Integer.MAX_VALUE / 2; } if (dp[idx][c] < 0) { dp[idx][c] = dfw(idx - 1, c); if (c >= values[idx]) { dp[idx][c] = Math.min(dp[idx][c], dfw(idx, c - values[idx]) + 1); } } return dp[idx][c]; } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public String next() throws Exception { if (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }