結果

問題 No.1163 I want to be a high achiever
ユーザー tentententen
提出日時 2021-11-05 18:42:35
言語 Java21
(openjdk 21)
結果
AC  
実行時間 303 ms / 2,000 ms
コード長 2,466 bytes
コンパイル時間 2,243 ms
コンパイル使用メモリ 77,580 KB
実行使用メモリ 101,624 KB
最終ジャッジ日時 2024-04-24 02:47:00
合計ジャッジ時間 8,458 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
37,280 KB
testcase_01 AC 52 ms
37,228 KB
testcase_02 AC 52 ms
37,284 KB
testcase_03 AC 55 ms
37,284 KB
testcase_04 AC 195 ms
66,296 KB
testcase_05 AC 248 ms
78,888 KB
testcase_06 AC 130 ms
47,040 KB
testcase_07 AC 294 ms
101,624 KB
testcase_08 AC 292 ms
100,392 KB
testcase_09 AC 55 ms
36,936 KB
testcase_10 AC 196 ms
66,292 KB
testcase_11 AC 250 ms
79,900 KB
testcase_12 AC 209 ms
75,348 KB
testcase_13 AC 133 ms
51,412 KB
testcase_14 AC 129 ms
51,316 KB
testcase_15 AC 283 ms
99,740 KB
testcase_16 AC 188 ms
66,448 KB
testcase_17 AC 219 ms
75,188 KB
testcase_18 AC 303 ms
100,824 KB
testcase_19 AC 143 ms
51,468 KB
testcase_20 AC 110 ms
42,988 KB
testcase_21 AC 56 ms
37,032 KB
testcase_22 AC 239 ms
75,612 KB
testcase_23 AC 221 ms
75,376 KB
testcase_24 AC 291 ms
100,024 KB
testcase_25 AC 184 ms
66,324 KB
testcase_26 AC 56 ms
37,248 KB
testcase_27 AC 54 ms
36,844 KB
testcase_28 AC 52 ms
36,804 KB
testcase_29 AC 59 ms
37,008 KB
testcase_30 AC 52 ms
37,196 KB
testcase_31 AC 53 ms
37,060 KB
testcase_32 AC 54 ms
36,796 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static Point[] points;
    static int[][] dp;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int x = sc.nextInt();
        points = new Point[n];
        int total = 0;
        boolean enable = false;
        for (int i = 0; i < n; i++) {
            points[i] = new Point(x - sc.nextInt());
            total += points[i].value;
            enable |= (points[i].value <= 0);
        }
        if (!enable) {
            System.out.println(-1);
            return;
        }
        if (total <= 0) {
            System.out.println(0);
            return;
        }
        for (int i = 0; i < n; i++) {
            points[i].cost = sc.nextInt();
        }
        Arrays.sort(points);
        dp = new int[n][total + 1];
        for (int[] arr : dp) {
            Arrays.fill(arr, -1);
        }
        System.out.println(dfw(n - 1, total));
    }
    
    static int dfw(int idx, int total) {
        if (total <= 0) {
            return 0;
        }
        if (idx < 0 || points[idx].value <= 0) {
            return Integer.MAX_VALUE / 2;
        }
        if (dp[idx][total] < 0) {
            dp[idx][total] = Math.min(dfw(idx - 1, total), dfw(idx - 1, total - points[idx].value) + points[idx].cost);
        }
        return dp[idx][total];
    }
    
    static class Point implements Comparable<Point> {
        int value;
        int cost;
        
        public Point(int value) {
            this.value = value;
        }
        
        public int compareTo(Point p) {
            return value - p.value;
        }
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    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 String nextLine() throws Exception {
        return br.readLine();
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0