結果

問題 No.914 Omiyage
ユーザー tentententen
提出日時 2020-08-28 00:57:13
言語 Java
(openjdk 23)
結果
AC  
実行時間 166 ms / 2,000 ms
コード長 1,177 bytes
コンパイル時間 2,324 ms
コンパイル使用メモリ 77,024 KB
実行使用メモリ 41,636 KB
最終ジャッジ日時 2024-11-09 00:57:35
合計ジャッジ時間 6,076 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

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