結果

問題 No.4 おもりと天秤
ユーザー tentententen
提出日時 2021-12-02 12:50:19
言語 Java21
(openjdk 21)
結果
AC  
実行時間 133 ms / 5,000 ms
コード長 1,589 bytes
コンパイル時間 2,166 ms
コンパイル使用メモリ 75,644 KB
実行使用メモリ 55,332 KB
最終ジャッジ日時 2023-09-18 10:12:26
合計ジャッジ時間 4,837 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,476 KB
testcase_01 AC 42 ms
49,384 KB
testcase_02 AC 50 ms
50,408 KB
testcase_03 AC 43 ms
49,440 KB
testcase_04 AC 54 ms
50,452 KB
testcase_05 AC 127 ms
55,008 KB
testcase_06 AC 43 ms
49,468 KB
testcase_07 AC 128 ms
55,096 KB
testcase_08 AC 44 ms
49,372 KB
testcase_09 AC 63 ms
51,816 KB
testcase_10 AC 133 ms
55,248 KB
testcase_11 AC 43 ms
49,484 KB
testcase_12 AC 42 ms
49,440 KB
testcase_13 AC 42 ms
49,844 KB
testcase_14 AC 43 ms
49,404 KB
testcase_15 AC 43 ms
49,768 KB
testcase_16 AC 44 ms
49,476 KB
testcase_17 AC 44 ms
49,668 KB
testcase_18 AC 97 ms
52,220 KB
testcase_19 AC 127 ms
55,068 KB
testcase_20 AC 125 ms
55,288 KB
testcase_21 AC 123 ms
55,072 KB
testcase_22 AC 122 ms
55,332 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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();
    }
}
0