結果

問題 No.1522 Unfairness
ユーザー tentententen
提出日時 2023-04-25 17:36:05
言語 Java21
(openjdk 21)
結果
AC  
実行時間 428 ms / 2,000 ms
コード長 2,283 bytes
コンパイル時間 3,043 ms
コンパイル使用メモリ 84,160 KB
実行使用メモリ 75,240 KB
最終ジャッジ日時 2024-04-26 22:08:06
合計ジャッジ時間 8,314 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
36,840 KB
testcase_01 AC 52 ms
37,168 KB
testcase_02 AC 52 ms
37,208 KB
testcase_03 AC 173 ms
51,836 KB
testcase_04 AC 114 ms
42,640 KB
testcase_05 AC 88 ms
39,196 KB
testcase_06 AC 96 ms
38,936 KB
testcase_07 AC 221 ms
55,952 KB
testcase_08 AC 222 ms
54,692 KB
testcase_09 AC 82 ms
38,780 KB
testcase_10 AC 134 ms
47,676 KB
testcase_11 AC 112 ms
42,160 KB
testcase_12 AC 146 ms
47,428 KB
testcase_13 AC 274 ms
66,244 KB
testcase_14 AC 90 ms
40,568 KB
testcase_15 AC 424 ms
74,960 KB
testcase_16 AC 428 ms
75,024 KB
testcase_17 AC 422 ms
74,976 KB
testcase_18 AC 425 ms
75,024 KB
testcase_19 AC 426 ms
75,100 KB
testcase_20 AC 161 ms
75,240 KB
testcase_21 AC 52 ms
37,264 KB
testcase_22 AC 51 ms
37,072 KB
testcase_23 AC 50 ms
37,352 KB
testcase_24 AC 51 ms
37,348 KB
testcase_25 AC 50 ms
37,040 KB
testcase_26 AC 51 ms
37,008 KB
testcase_27 AC 49 ms
36,968 KB
testcase_28 AC 52 ms
37,264 KB
testcase_29 AC 53 ms
37,116 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static int[] values;
    static int[][] dp;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int m = sc.nextInt();
        values = new int[n + 1];
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt();
        }
        values[n] = -10000000;
        Arrays.sort(values);
        dp = new int[n + 1][m + 1];
        for (int[] arr : dp) {
            Arrays.fill(arr, -1);
        }
        System.out.println(dfw(n, m));
    }
    
    static int dfw(int idx, int v) {
        if (v < 0) {
            return Integer.MIN_VALUE;
        }
        if (idx <= 1) {
            return 0;
        }
        if (dp[idx][v] < 0) {
            dp[idx][v] = Math.max(dfw(idx - 1, v), Math.max(dfw(idx - 2, v - (values[idx] - values[idx - 1])) + values[idx], dfw(idx - 3, v - (values[idx] - values[idx - 2])) + values[idx]));
        }
        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