結果

問題 No.4 おもりと天秤
ユーザー mits58mits58
提出日時 2015-12-12 00:06:28
言語 Java21
(openjdk 21)
結果
AC  
実行時間 157 ms / 5,000 ms
コード長 1,025 bytes
コンパイル時間 2,040 ms
コンパイル使用メモリ 73,492 KB
実行使用メモリ 58,620 KB
最終ジャッジ日時 2023-09-08 16:29:32
合計ジャッジ時間 6,252 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
56,224 KB
testcase_01 AC 123 ms
55,736 KB
testcase_02 AC 125 ms
55,696 KB
testcase_03 AC 125 ms
55,816 KB
testcase_04 AC 124 ms
55,976 KB
testcase_05 AC 153 ms
57,516 KB
testcase_06 AC 123 ms
55,920 KB
testcase_07 AC 151 ms
58,620 KB
testcase_08 AC 131 ms
55,864 KB
testcase_09 AC 145 ms
57,796 KB
testcase_10 AC 149 ms
57,856 KB
testcase_11 AC 122 ms
55,844 KB
testcase_12 AC 122 ms
55,972 KB
testcase_13 AC 122 ms
56,208 KB
testcase_14 AC 121 ms
55,840 KB
testcase_15 AC 123 ms
56,072 KB
testcase_16 AC 124 ms
55,680 KB
testcase_17 AC 123 ms
55,980 KB
testcase_18 AC 157 ms
56,120 KB
testcase_19 AC 151 ms
56,284 KB
testcase_20 AC 149 ms
58,108 KB
testcase_21 AC 150 ms
55,372 KB
testcase_22 AC 148 ms
56,212 KB
権限があれば一括ダウンロードができます

ソースコード

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