結果

問題 No.914 Omiyage
ユーザー tentententen
提出日時 2023-01-10 18:23:10
言語 Java21
(openjdk 21)
結果
AC  
実行時間 47 ms / 2,000 ms
コード長 2,374 bytes
コンパイル時間 2,466 ms
コンパイル使用メモリ 83,636 KB
実行使用メモリ 49,976 KB
最終ジャッジ日時 2023-08-23 10:16:06
合計ジャッジ時間 5,166 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
49,444 KB
testcase_01 AC 47 ms
49,912 KB
testcase_02 AC 46 ms
49,388 KB
testcase_03 AC 47 ms
49,208 KB
testcase_04 AC 46 ms
49,160 KB
testcase_05 AC 45 ms
49,332 KB
testcase_06 AC 43 ms
49,304 KB
testcase_07 AC 44 ms
49,456 KB
testcase_08 AC 45 ms
49,424 KB
testcase_09 AC 44 ms
49,200 KB
testcase_10 AC 45 ms
49,416 KB
testcase_11 AC 44 ms
49,564 KB
testcase_12 AC 46 ms
49,200 KB
testcase_13 AC 46 ms
49,208 KB
testcase_14 AC 47 ms
49,176 KB
testcase_15 AC 46 ms
49,976 KB
testcase_16 AC 44 ms
49,412 KB
testcase_17 AC 46 ms
49,480 KB
testcase_18 AC 44 ms
49,444 KB
testcase_19 AC 44 ms
49,240 KB
testcase_20 AC 44 ms
49,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    static int[][] lists;
    static int[][] dp;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int m = sc.nextInt();
        int k = sc.nextInt();
        lists = new int[n][m];
        dp = new int[n][k + 1];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                lists[i][j] = sc.nextInt();
            }
            Arrays.fill(dp[i], -1);
        }
        int ans = dfw(n - 1, k);
        if (ans >= k) {
            System.out.println(-1);
        } else {
            System.out.println(ans);
        }
    }
    
    static int dfw(int idx, int v) {
        if (v < 0) {
            return Integer.MAX_VALUE / 2;
        }
        if (idx < 0) {
            return v;
        }
        if (dp[idx][v] < 0) {
            dp[idx][v] = Integer.MAX_VALUE / 2;
            for (int x : lists[idx]) {
                dp[idx][v] = Math.min(dp[idx][v], dfw(idx - 1, v - x));
            }
        }
        return dp[idx][v];
    }
}

class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    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();
    }
    
}
0