結果

問題 No.1594 Three Classes
ユーザー tentententen
提出日時 2024-04-24 12:55:37
言語 Java21
(openjdk 21)
結果
AC  
実行時間 54 ms / 2,000 ms
コード長 2,133 bytes
コンパイル時間 3,196 ms
コンパイル使用メモリ 78,904 KB
実行使用メモリ 37,096 KB
最終ジャッジ日時 2024-04-24 12:55:42
合計ジャッジ時間 4,633 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
36,676 KB
testcase_01 AC 52 ms
36,808 KB
testcase_02 AC 52 ms
36,672 KB
testcase_03 AC 53 ms
36,804 KB
testcase_04 AC 51 ms
37,068 KB
testcase_05 AC 52 ms
36,800 KB
testcase_06 AC 53 ms
36,676 KB
testcase_07 AC 54 ms
36,788 KB
testcase_08 AC 52 ms
37,088 KB
testcase_09 AC 53 ms
36,660 KB
testcase_10 AC 54 ms
37,088 KB
testcase_11 AC 52 ms
36,832 KB
testcase_12 AC 54 ms
36,780 KB
testcase_13 AC 53 ms
37,012 KB
testcase_14 AC 53 ms
36,784 KB
testcase_15 AC 52 ms
36,668 KB
testcase_16 AC 53 ms
36,620 KB
testcase_17 AC 53 ms
37,096 KB
testcase_18 AC 52 ms
36,908 KB
testcase_19 AC 51 ms
36,880 KB
testcase_20 AC 52 ms
37,028 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int total = 0;
        int[] values = new int[n];
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt();
            total += values[i];
        }
        if (total % 3 != 0) {
            System.out.println("No");
            return;
        }
        List<Integer> list = new ArrayList<>();
        int[] dp = new int[1 << n];
        for (int i = 1; i < (1 << n); i++) {
            for (int j = 0; j < n; j++) {
                if ((i & (1 << j)) == 0) {
                    continue;
                }
                dp[i] = dp[i ^ (1 << j)] + values[j];
                break;
            }
            if (dp[i] == total / 3) {
                list.add(i);
            }
        }
        for (int i = 0; i < list.size(); i++) {
            for (int j = i + 1; j < list.size(); j++) {
                if ((list.get(i) & list.get(j)) == 0) {
                    System.out.println("Yes");
                    return;
                }
            }
        }
        System.out.println("No");
    }
}
class Scanner {
    BufferedReader br;
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            
        }
    }
    
    public int nextInt() {
        return Integer.parseInt(next());
    }
    
    public long nextLong() {
        return Long.parseLong(next());
    }
    
    public double nextDouble() {
        return Double.parseDouble(next());
    }
    
    public String next() {
        try {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return st.nextToken();
        }
    }
    
}
0