結果

問題 No.4 おもりと天秤
ユーザー mits58
提出日時 2015-12-12 00:06:28
言語 Java
(openjdk 23)
結果
AC  
実行時間 169 ms / 5,000 ms
コード長 1,025 bytes
コンパイル時間 2,192 ms
コンパイル使用メモリ 77,320 KB
実行使用メモリ 43,612 KB
最終ジャッジ日時 2024-06-26 09:21:30
合計ジャッジ時間 6,475 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main{
	static int n;
	static int[] w;
	static int[][] dp;
	static boolean res(int x,int h){
		if(h==0) return true;
		else if(h<0) return false;
		else if(x==n-1) return false;
		else if(dp[x][h]!=-1){
			if(dp[x][h]==0) return true;
			else return false;
		}
		else{
			if(res(x+1,h)||res(x+1,h-w[x])){
				dp[x][h]=0;
				return true;
			}
			else{
				dp[x][h]=1;
				return false;
			}
		}
	}
    public static void main(String[] args){
    	Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
        	n=sc.nextInt();
        	w=new int[n];
        	int sum=0;
        	for(int i=0;i<n;i++){
        		w[i]=sc.nextInt();
        		sum+=w[i];
        	}
        	if(sum%2!=0) System.out.println("impossible");
        	else{
        		dp=new int[n][sum/2+1];
        		for(int i=0;i<n;i++) for(int j=0;j<sum/2+1;j++) dp[i][j]=-1;
        		if(res(0,sum/2)) System.out.println("possible");
            	else System.out.println("impossible");
        	}
        }
    }
}
0