結果

問題 No.2542 Yokan for Two
ユーザー tentententen
提出日時 2023-12-13 14:03:22
言語 Java21
(openjdk 21)
結果
AC  
実行時間 126 ms / 2,000 ms
コード長 2,314 bytes
コンパイル時間 3,088 ms
コンパイル使用メモリ 91,480 KB
実行使用メモリ 64,312 KB
最終ジャッジ日時 2023-12-13 14:03:32
合計ジャッジ時間 9,123 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
53,992 KB
testcase_01 AC 55 ms
53,992 KB
testcase_02 AC 55 ms
53,988 KB
testcase_03 AC 76 ms
57,120 KB
testcase_04 AC 80 ms
57,308 KB
testcase_05 AC 86 ms
59,996 KB
testcase_06 AC 55 ms
53,996 KB
testcase_07 AC 95 ms
64,204 KB
testcase_08 AC 77 ms
57,224 KB
testcase_09 AC 95 ms
64,312 KB
testcase_10 AC 69 ms
54,360 KB
testcase_11 AC 76 ms
55,264 KB
testcase_12 AC 83 ms
57,228 KB
testcase_13 AC 72 ms
54,504 KB
testcase_14 AC 99 ms
64,220 KB
testcase_15 AC 99 ms
64,228 KB
testcase_16 AC 97 ms
64,216 KB
testcase_17 AC 98 ms
64,292 KB
testcase_18 AC 104 ms
64,264 KB
testcase_19 AC 91 ms
60,012 KB
testcase_20 AC 89 ms
60,020 KB
testcase_21 AC 89 ms
60,016 KB
testcase_22 AC 91 ms
60,004 KB
testcase_23 AC 90 ms
60,092 KB
testcase_24 AC 61 ms
54,004 KB
testcase_25 AC 64 ms
54,260 KB
testcase_26 AC 73 ms
54,888 KB
testcase_27 AC 77 ms
54,256 KB
testcase_28 AC 65 ms
54,272 KB
testcase_29 AC 78 ms
57,224 KB
testcase_30 AC 101 ms
64,252 KB
testcase_31 AC 104 ms
64,248 KB
testcase_32 AC 102 ms
64,288 KB
testcase_33 AC 102 ms
64,236 KB
testcase_34 AC 99 ms
64,240 KB
testcase_35 AC 101 ms
64,248 KB
testcase_36 AC 103 ms
64,252 KB
testcase_37 AC 98 ms
64,236 KB
testcase_38 AC 100 ms
64,248 KB
testcase_39 AC 98 ms
64,236 KB
testcase_40 AC 98 ms
64,236 KB
testcase_41 AC 126 ms
64,236 KB
testcase_42 AC 101 ms
62,348 KB
testcase_43 AC 97 ms
64,236 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int length = sc.nextInt();
        int n = sc.nextInt();
        int[] values = new int[n + 1];
        values[0] = sc.nextInt();
        int sum = values[0];
        for (int i = 1; i < n; i++) {
            values[i] = sc.nextInt() - sum;
            sum += values[i];
        }
        values[n] = length - sum;
        boolean[][] dp = new boolean[n][length];
        int max = Math.abs(values[n] - values[0]);
        dp[0][max] = true;
        for (int i = 1; i < n; i++) {
            for (int j = max; j >= 0; j--) {
                if (dp[i - 1][j]) {
                    dp[i][j + values[i]] = true;
                    dp[i][Math.abs(j - values[i])] = true;
                }
            }
            max += values[i];
        }
        for (int i = 0; ;i++) {
            if (dp[n - 1][i]) {
                System.out.println(i);
                return;
            }
        }
    }
}
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