結果
| 問題 |
No.4 おもりと天秤
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2021-12-02 12:50:19 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 149 ms / 5,000 ms |
| コード長 | 1,589 bytes |
| コンパイル時間 | 2,396 ms |
| コンパイル使用メモリ | 78,360 KB |
| 実行使用メモリ | 55,056 KB |
| 最終ジャッジ日時 | 2024-07-05 01:51:11 |
| 合計ジャッジ時間 | 5,267 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 23 |
ソースコード
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner();
int n = sc.nextInt();
int[] weights = new int[n];
int total = 0;
for (int i = 0; i < n; i++) {
weights[i] = sc.nextInt();
total += weights[i];
}
if (total % 2 == 1) {
System.out.println("impossible");
return;
}
total /= 2;
TreeSet<Integer> ans = new TreeSet<>();
ans.add(0);
for (int x : weights) {
Integer key = total - x + 1;
while ((key = ans.lower(key)) != null) {
ans.add(key + x);
}
}
if (ans.contains(total)) {
System.out.println("possible");
} else {
System.out.println("impossible");
}
}
}
class Scanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
public Scanner() throws Exception {
}
public int nextInt() throws Exception {
return Integer.parseInt(next());
}
public long nextLong() throws Exception {
return Long.parseLong(next());
}
public double nextDouble() throws Exception {
return Double.parseDouble(next());
}
public String next() throws Exception {
if (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
}
tenten