結果

問題 No.4 おもりと天秤
ユーザー tenten
提出日時 2023-11-15 15:55:51
言語 Java
(openjdk 23)
結果
AC  
実行時間 78 ms / 5,000 ms
コード長 2,199 bytes
コンパイル時間 3,989 ms
コンパイル使用メモリ 91,880 KB
実行使用メモリ 52,604 KB
最終ジャッジ日時 2024-09-26 04:37:24
合計ジャッジ時間 4,972 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static int[] weights;
    static boolean[][] dp;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        weights = new int[n];
        int total = 0;
        for (int i = 0; i < n; i++) {
            weights[i] = sc.nextInt();
            total += weights[i];
        }
        if (total % 2 == 0) {
            dp = new boolean[n][total / 2 + 1];
            dfw(n - 1, total / 2);
        }
        System.out.println("impossible");
    }
    
    static boolean dfw(int idx, int v) {
        if (v == 0) {
            System.out.println("possible");
            System.exit(0);
        }
        if (v < 0 || idx < 0) {
            return true;
        }
        if (!dp[idx][v]) {
            dp[idx][v] = (dfw(idx - 1,  v) & dfw(idx - 1, v - weights[idx]));
        }
        return true;
    }
    
}
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