結果

問題 No.1594 Three Classes
ユーザー merlinmerlin
提出日時 2021-07-09 21:38:36
言語 Java21
(openjdk 21)
結果
AC  
実行時間 55 ms / 2,000 ms
コード長 1,597 bytes
コンパイル時間 2,348 ms
コンパイル使用メモリ 75,584 KB
実行使用メモリ 50,616 KB
最終ジャッジ日時 2023-08-10 06:46:17
合計ジャッジ時間 4,167 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
50,616 KB
testcase_01 AC 43 ms
49,340 KB
testcase_02 AC 42 ms
49,324 KB
testcase_03 AC 43 ms
49,336 KB
testcase_04 AC 42 ms
49,336 KB
testcase_05 AC 55 ms
50,144 KB
testcase_06 AC 43 ms
49,676 KB
testcase_07 AC 44 ms
49,368 KB
testcase_08 AC 45 ms
49,336 KB
testcase_09 AC 54 ms
50,220 KB
testcase_10 AC 54 ms
50,436 KB
testcase_11 AC 54 ms
50,360 KB
testcase_12 AC 53 ms
50,196 KB
testcase_13 AC 42 ms
47,248 KB
testcase_14 AC 43 ms
49,300 KB
testcase_15 AC 44 ms
49,416 KB
testcase_16 AC 43 ms
49,472 KB
testcase_17 AC 43 ms
49,412 KB
testcase_18 AC 42 ms
49,412 KB
testcase_19 AC 43 ms
49,260 KB
testcase_20 AC 42 ms
49,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

class Main
{
    public static void main(String args[])throws Exception
    {
        BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb=new StringBuilder();
        int n=Integer.parseInt(bu.readLine());
        int i,sum=0,ar[]=new int[n];
        ArrayList<Integer> a=new ArrayList<>();
        String s[]=bu.readLine().split(" ");
        for(i=0;i<n;i++)
        {
            ar[i]=Integer.parseInt(s[i]);
            a.add(i);
            sum+=ar[i];
        }
        if(sum%3!=0) {System.out.println("No"); return;}

        sum/=3;
        a=subset(a,ar,sum);
        boolean vis[]=new boolean[n];
        int sua=0,sub=0;
        for(int x:a)
        {
            vis[x]=true;
            sua+=ar[x];
        }

        ArrayList<Integer> b=new ArrayList<>();
        for(i=0;i<n;i++)
        if(!vis[i]) b.add(i);

        b=subset(b,ar,sum);
        for(int x:b) sub+=ar[x];
        if(sua==sub && sua==sum) sb.append("Yes\n");
        else sb.append("No\n");
        System.out.print(sb);
    }

    static ArrayList<Integer> subset(ArrayList<Integer> a,int ar[],int sum)
    {
        ArrayList<Integer> ans=new ArrayList<>();
        int i,n=a.size(),m=1<<n,j;
        for(i=0;i<m;i++)
        {
            int s=0;
            for(j=0;j<n;j++)
            if(((i>>j)&1)==1) s+=ar[a.get(j)];
            if(s==sum)
            {
                for(j=0;j<n;j++)
                if(((i>>j)&1)==1) ans.add(a.get(j));
                return ans;
            }
        }
        return ans;
    }
}
0