結果

問題 No.4 おもりと天秤
ユーザー CavasiroCavasiro
提出日時 2020-05-03 18:32:34
言語 Java21
(openjdk 21)
結果
AC  
実行時間 71 ms / 5,000 ms
コード長 4,061 bytes
コンパイル時間 2,458 ms
コンパイル使用メモリ 79,332 KB
実行使用メモリ 53,192 KB
最終ジャッジ日時 2023-09-03 23:32:15
合計ジャッジ時間 5,422 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
50,248 KB
testcase_01 AC 56 ms
50,820 KB
testcase_02 AC 57 ms
50,452 KB
testcase_03 AC 58 ms
50,228 KB
testcase_04 AC 57 ms
50,344 KB
testcase_05 AC 71 ms
53,192 KB
testcase_06 AC 56 ms
50,232 KB
testcase_07 AC 69 ms
52,632 KB
testcase_08 AC 59 ms
50,716 KB
testcase_09 AC 71 ms
52,368 KB
testcase_10 AC 70 ms
52,620 KB
testcase_11 AC 58 ms
50,452 KB
testcase_12 AC 60 ms
50,608 KB
testcase_13 AC 59 ms
50,364 KB
testcase_14 AC 58 ms
50,392 KB
testcase_15 AC 59 ms
50,388 KB
testcase_16 AC 59 ms
50,740 KB
testcase_17 AC 59 ms
50,344 KB
testcase_18 AC 70 ms
52,888 KB
testcase_19 AC 70 ms
52,668 KB
testcase_20 AC 69 ms
50,816 KB
testcase_21 AC 71 ms
52,888 KB
testcase_22 AC 69 ms
53,076 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.NoSuchElementException;

class Main implements Runnable {
	private final FastScanner sc;
	private final PrintWriter out;
	//private final long MOD = 1000000007;
	//private final int[] d8x = {-1,0,1,1,1,0,-1,-1};
	//private final int[] d8y = {-1,-1,-1,0,1,1,1,0};
	//private final int[] d4x = {0,1,0,-1};
	//private final int[] d4y = {-1,0,1,0};
	int n;
	int[] w;

	public static void main(String[] args) {
		new Thread(null, new Main(), "", 16L * 1024 * 1024).start();
	}

	Main() {
		sc = new FastScanner();
		out = new PrintWriter(System.out);
	}

	private void init() throws Exception {
		n = sc.nextInt();
		w = new int[n];
		for(int i=0;i<n;i++){
			w[i] = sc.nextInt();
		}
	}

	private void solve() throws Exception{
		boolean[][] dp;
		int sum = Arrays.stream(w).sum();
		dp = new boolean[n+1][sum+1];
		dp[0][0] = true;
		for(int i=1;i<=n;i++){
			for(int j=0;j<=sum;j++){
				if(dp[i-1][j] || (j-w[i-1]>=0 && dp[i-1][j-w[i-1]])){
					dp[i][j] = true;
				}
			}
		}
		if(sum%2==0 && dp[n][sum/2]){
			out.println("possible");
		} else {
			out.println("impossible");
		}
	}

	public void run() {
		try {
			init();
			sc.close();
			solve();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
		out.flush();
		out.close();
	}
}

class Data {
}

class ExIntVector {
	private int[] a;
	private int cnt;
	public ExIntVector(int max){
		a = new int[max];
		cnt = 0;
	}
	public int size(){
		return cnt;
	}
	public void add(int x){
		a[cnt++] = x;
	}
	public int get(int i){
		return a[i];
	}
	public int getLast(){
		return a[cnt-1];
	}
	public int[] toArray(){
		return a;
	}
}

class FastScanner {
	private final InputStream in = System.in;
	private final byte[] buffer = new byte[1024];
	private int ptr = 0;
	private int buflen = 0;

	private boolean read() {
		if (ptr < buflen) {
			return true;
		} else {
			ptr = 0;
			try {
				buflen = in.read(buffer);
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (buflen <= 0) {
				return false;
			}
			return true;
		}
	}

	private boolean hasNext() {
		while (read() && !isVisibleChar(buffer[ptr]))
			ptr++;
		return read();
	}

	private int getNextChar() {
		if (read()) {
			ptr++;
			return buffer[ptr - 1];
		} else {
			return -1;
		}
	}

	private static boolean isVisibleChar(int code) {
		return code >= 33 && code <= 126;
	}

	public String next() {
		if (!hasNext())
			throw new NoSuchElementException();
		StringBuilder sb = new StringBuilder();
		int b = getNextChar();
		while (isVisibleChar(b)) {
			sb.appendCodePoint(b);
			b = getNextChar();
		}
		return sb.toString();
	}

	public long nextLong() {
		if (!hasNext())
			throw new NoSuchElementException();
		long n = 0;
		boolean sign = true;
		int b = getNextChar();
		if (b == '-') {
			sign = false;
			b = getNextChar();
		}
		if (b < '0' || b > '9') {
			throw new NumberFormatException();
		}
		while (true) {
			if (b >= '0' && b <= '9') {
				n *= 10;
				n += b - '0';
			} else if (b == -1 || !isVisibleChar(b)) {
				return sign ? n : -n;
			} else {
				throw new NumberFormatException();
			}
			b = getNextChar();
		}
	}

	public int nextInt() {
		long n = nextLong();
		if (n < Integer.MIN_VALUE || n > Integer.MAX_VALUE)
			throw new NumberFormatException();
		return (int) n;
	}

	public double nextDouble() {
		return Double.parseDouble(next());
	}
	
	public char nextChar() {
		if (!hasNext())
			throw new NoSuchElementException();
		int b = getNextChar();
		while (!isVisibleChar(b)) {
			b = getNextChar();
		}
		return (char) b;
	}

	public void close() {
		try {
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

class IntQueue {
	protected int[] a;
	protected int head, tail;

	public IntQueue(int max) {
		a = new int[max];
	}

	public void offer(int x) {
		a[tail++] = x;
	}

	public int poll() {
		return a[head++];
	}

	public int peek() {
		return a[head];
	}
	
	public int size() {
		return tail - head;
	}
}
0