結果
問題 | No.2167 Fibonacci Knapsack |
ユーザー |
![]() |
提出日時 | 2022-12-26 15:21:12 |
言語 | Java (openjdk 23) |
結果 |
MLE
|
実行時間 | - |
コード長 | 2,450 bytes |
コンパイル時間 | 2,873 ms |
コンパイル使用メモリ | 84,732 KB |
実行使用メモリ | 761,548 KB |
最終ジャッジ日時 | 2024-11-21 00:16:47 |
合計ジャッジ時間 | 91,262 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 MLE * 1 |
other | TLE * 2 MLE * 19 |
ソースコード
import java.io.*;import java.util.*;import java.util.stream.*;public class Main {static long[] weights;static long[] values;static ArrayList<HashMap<Long, Long>> dp;public static void main(String[] args) throws Exception {Scanner sc = new Scanner();int t = sc.nextInt();StringBuilder sb = new StringBuilder();while (t-- > 0) {int n = sc.nextInt();long w = sc.nextLong();weights = new long[n];values = new long[n];dp = new ArrayList<>();for (int i = 0; i < n; i++) {weights[i] = sc.nextLong();dp.add(new HashMap<>());}values[0] = 1;if (n > 1) {values[1] = 2;}for (int i = 2; i < n; i++) {values[i] = values[i - 1] + values[i - 2];}sb.append(dfw(n - 1, w)).append("\n");}System.out.print(sb);}static long dfw(int idx, long w) {if (w < 0) {return Long.MIN_VALUE;}if (idx < 0) {return 0;}if (!dp.get(idx).containsKey(w)) {dp.get(idx).put(w, Math.max(dfw(idx - 1, w), dfw(idx - 1, w - weights[idx]) + values[idx]));}return dp.get(idx).get(w);}}class Utilities {static String arrayToLineString(int[] arr) {return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));}}class Scanner {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));StringTokenizer st = new StringTokenizer("");StringBuilder sb = new StringBuilder();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 int[] nextIntArray() throws Exception {return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();}public String next() throws Exception {while (!st.hasMoreTokens()) {st = new StringTokenizer(br.readLine());}return st.nextToken();}}