結果
問題 | No.914 Omiyage |
ユーザー |
![]() |
提出日時 | 2019-12-30 19:32:47 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 155 ms / 2,000 ms |
コード長 | 1,142 bytes |
コンパイル時間 | 2,035 ms |
コンパイル使用メモリ | 78,000 KB |
実行使用メモリ | 54,320 KB |
最終ジャッジ日時 | 2024-11-14 12:45:49 |
合計ジャッジ時間 | 5,977 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 18 |
ソースコード
import java.util.*; public class Main { static int[][] goods; static int[][] dp; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int k = sc.nextInt(); goods = new int[n][m]; dp = new int[n][k + 1]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { goods[i][j] = sc.nextInt(); } Arrays.fill(dp[i], -1); } int ans = dfw(n - 1, k); if (ans > k) { ans = -1; } System.out.println(ans); } static int dfw(int idx, int money) { if (money < 0) { return Integer.MAX_VALUE; } if (idx < 0) { return money; } if (dp[idx][money] != -1) { return dp[idx][money]; } dp[idx][money] = Integer.MAX_VALUE; for (int i = 0; i < goods[idx].length; i++) { dp[idx][money] = Math.min(dp[idx][money], dfw(idx - 1, money - goods[idx][i])); } return dp[idx][money]; } }