結果

問題 No.4 おもりと天秤
ユーザー kohaku_kohaku
提出日時 2016-11-14 20:23:18
言語 Java
(openjdk 23)
結果
AC  
実行時間 165 ms / 5,000 ms
コード長 939 bytes
コンパイル時間 2,035 ms
コンパイル使用メモリ 77,456 KB
実行使用メモリ 42,452 KB
最終ジャッジ日時 2024-06-26 10:04:37
合計ジャッジ時間 6,038 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int [] W = new int [N];
        int TW = 0;
        for(int i=0; i<N; i++){
            W[i] = sc.nextInt();
            TW = TW + W[i];
        }
        if(TW%2==1){
            System.out.println("impossible");
            return;
        }
        int HW = TW/2;
        boolean [][] PW = new boolean [N+2][TW];
        PW[0][W[0]-1]=true;
        for(int i=1; i<N; i++){
            for(int j=0; j<HW; j++){
                if(PW[i-1][HW-1-j]==true){
                    PW[i][HW-1-j+W[i]]=true;
                    PW[i][HW-1-j]=true;
                }
                if(PW[i][HW-1]==true){
                    System.out.println("possible");
                    return;
                }
            }
        }
        System.out.println("impossible");
    }
}
0