結果

問題 No.1861 Required Number
ユーザー tenten
提出日時 2022-03-07 11:15:19
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 2,414 bytes
コンパイル時間 3,773 ms
コンパイル使用メモリ 77,344 KB
実行使用メモリ 76,228 KB
最終ジャッジ日時 2024-07-22 04:43:29
合計ジャッジ時間 16,288 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2 WA * 1
other AC * 39 WA * 3 MLE * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] values = new int[n];
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt();
        }
        boolean[][] leftDP = new boolean[n][k + 1];
        leftDP[0][0] = true;
        leftDP[0][values[0]] = true;
        for (int i = 1; i < n; i++) {
            for (int j = 0; j <= k; j++) {
                if (leftDP[i - 1][j]) {
                    leftDP[i][j] = true;
                    if (j + values[i] <= k) {
                        leftDP[i][j + values[i]] = true;
                    }
                }
            }
        }
        boolean[][] rightDP = new boolean[n][k + 1];
        rightDP[n - 1][0] = true;
        rightDP[n - 1][values[n - 1]] = true;
        for (int i = n - 2; i >= 0; i--) {
            for (int j = 0; j <= k; j++) {
                if (rightDP[i + 1][j]) {
                    rightDP[i][j] = true;
                    if (j + values[i] <= k) {
                        rightDP[i][j + values[i]] = true;
                    }
                }
            }
        }
        int ans = 0;
        for (int i = 1; i < n - 1; i++) {
            boolean enable = false;
            for (int j = 0; j <= k && !enable; j++)  {
                enable = leftDP[i - 1][j] && rightDP[i + 1][k - j];
            }
            if (!enable) {
                ans++;
            }
        }
        if (!rightDP[1][k]) {
            ans++;
        }
        if (!leftDP[n - 2][k]) {
            ans++;
        }
        System.out.println(ans);
    }
}


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 next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0