結果

問題 No.1163 I want to be a high achiever
ユーザー tenten
提出日時 2020-08-25 07:48:13
言語 Java
(openjdk 23)
結果
AC  
実行時間 275 ms / 2,000 ms
コード長 1,482 bytes
コンパイル時間 2,312 ms
コンパイル使用メモリ 77,864 KB
実行使用メモリ 64,168 KB
最終ジャッジ日時 2024-11-06 11:02:50
合計ジャッジ時間 10,617 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static ArrayList<Unit> list = new ArrayList<>();
    static int[][] dp;
	public static void main (String[] args) {
    	Scanner sc = new Scanner(System.in);
    	int n = sc.nextInt();
    	int x = sc.nextInt();
    	int[] aArr = new int[n];
    	for (int i = 0; i < n; i++) {
    	    aArr[i] = sc.nextInt();
    	}
    	int sum = 0;
    	for (int i = 0; i < n; i++) {
    	    int y = sc.nextInt();
    	    if (aArr[i] < x) {
    	        list.add(new Unit(x - aArr[i], y));
    	    } else {
    	        sum += aArr[i] - x;
    	    }
    	}
    	if (list.size() == n) {
    	    System.out.println(-1);
    	    return;
    	}
    	int length = list.size();
    	dp = new int[length][sum + 1];
    	for (int[] arr : dp) {
    	    Arrays.fill(arr, Integer.MAX_VALUE);
    	}
    	System.out.println(dfw(length - 1, sum));
    }
    
    static int dfw(int idx, int sum) {
        if (sum < 0) {
            return Integer.MAX_VALUE / 10;
        }
        if (idx < 0) {
            return 0;
        }
        if (dp[idx][sum] == Integer.MAX_VALUE) {
            dp[idx][sum] = Math.min(dfw(idx - 1, sum - list.get(idx).value), dfw(idx - 1, sum) + list.get(idx).cost);
        }
        return dp[idx][sum];
    }
    
    static class Unit {
        int value;
        int cost;
        
        public Unit(int value, int cost) {
            this.value = value;
            this.cost = cost;
        }
        
        
    }
}

0