結果

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

ソースコード

diff #

import java.util.*;

//memo


class Yuki4_2{
	public static final int MAX_N = 100;
	public static final int MAX_W = 110000;

	public static boolean[][] dpChecker = new boolean[MAX_N+1][MAX_W+1];
	public static int[][] dpMemo = new int[MAX_N+1][MAX_W+1];
	public static int[] w = new int[MAX_N+1];

	static int myDP(int i,int j){
		//System.out.println(i+" "+j);
		if(dpChecker[i][j] == true) {return dpMemo[i][j];}
		int res;
		if(i==MAX_N){res = 0;}
		else if(j < w[i]){res = myDP(i+1,j);}
		else{
			if(myDP(i+1,j-w[i])+w[i] < myDP(i+1,j)){
				res = myDP(i+1,j);
			}else{
				res = myDP(i+1,j-w[i])+w[i];
			}
		}

		dpChecker[i][j] = true;
		dpMemo[i][j] = res;
		return res;
	}


	public static void main(String[] args){
		Scanner stdIn = new Scanner(System.in);
		int n = stdIn.nextInt();
		int sumW = 0;
		for(int i=0;i<n;i++){
			w[i] = stdIn.nextInt();
			sumW += w[i];
		}
		if(sumW%2 == 0){
			int answer = myDP(0,sumW/2);
			if(answer == sumW/2){
				System.out.println("possible");
			}else{
				//System.out.println(answer);
				System.out.println("impossible");
			}
		}else{
			System.out.println("impossible");
		}
	}
}
0