結果
問題 |
No.1163 I want to be a high achiever
|
ユーザー |
![]() |
提出日時 | 2024-07-22 16:54:51 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 150 ms / 2,000 ms |
コード長 | 2,313 bytes |
コンパイル時間 | 2,240 ms |
コンパイル使用メモリ | 82,036 KB |
実行使用メモリ | 48,168 KB |
最終ジャッジ日時 | 2024-07-22 16:54:58 |
合計ジャッジ時間 | 6,226 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 |
ソースコード
import java.io.*; import java.util.*; import java.util.function.*; import java.util.stream.*; public class Main { static List<Integer> values = new ArrayList<>(); static List<Integer> costs = new ArrayList<>(); static int[][] dp; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); int target = sc.nextInt(); int[] current = new int[n]; for (int i = 0; i < n; i++) { current[i] = target - sc.nextInt(); } int total = 0; for (int i = 0; i < n; i++) { int x = sc.nextInt(); if (current[i] <= 0) { total -= current[i]; } else { values.add(current[i]); costs.add(x); } } if (values.size() == n) { System.out.println(-1); return; } dp = new int[values.size()][total + 1]; for (int[] arr : dp) { Arrays.fill(arr, -1); } System.out.println(dfw(values.size() - 1, total)); } static int dfw(int idx, int v) { if (v < 0) { return Integer.MAX_VALUE / 2; } if (idx < 0) { return 0; } if (dp[idx][v] < 0) { dp[idx][v] = Math.min(dfw(idx - 1, v) + costs.get(idx), dfw(idx - 1, v - values.get(idx))); } return dp[idx][v]; } } class Scanner { BufferedReader br; StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); } catch (Exception e) { } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String next() { try { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } } catch (Exception e) { e.printStackTrace(); } finally { return st.nextToken(); } } }