結果

問題 No.1594 Three Classes
ユーザー merlinmerlin
提出日時 2021-07-09 21:38:36
言語 Java17
(openjdk 17.0.1)
結果
AC  
実行時間 76 ms / 2,000 ms
コード長 1,597 bytes
コンパイル時間 3,343 ms
使用メモリ 34,192 KB
最終ジャッジ日時 2022-12-17 01:43:14
合計ジャッジ時間 4,467 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 74 ms
34,192 KB
testcase_01 AC 52 ms
34,092 KB
testcase_02 AC 52 ms
34,160 KB
testcase_03 AC 52 ms
34,096 KB
testcase_04 AC 52 ms
33,988 KB
testcase_05 AC 76 ms
33,956 KB
testcase_06 AC 53 ms
34,108 KB
testcase_07 AC 52 ms
34,080 KB
testcase_08 AC 54 ms
34,180 KB
testcase_09 AC 75 ms
34,016 KB
testcase_10 AC 75 ms
34,136 KB
testcase_11 AC 75 ms
33,872 KB
testcase_12 AC 74 ms
34,136 KB
testcase_13 AC 52 ms
34,176 KB
testcase_14 AC 54 ms
33,928 KB
testcase_15 AC 54 ms
33,996 KB
testcase_16 AC 54 ms
34,084 KB
testcase_17 AC 53 ms
34,120 KB
testcase_18 AC 52 ms
34,116 KB
testcase_19 AC 52 ms
33,936 KB
testcase_20 AC 52 ms
33,868 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