結果

問題 No.1594 Three Classes
ユーザー merlinmerlin
提出日時 2021-07-09 21:38:36
言語 Java21
(openjdk 21)
結果
AC  
実行時間 69 ms / 2,000 ms
コード長 1,597 bytes
コンパイル時間 2,179 ms
コンパイル使用メモリ 78,516 KB
実行使用メモリ 37,492 KB
最終ジャッジ日時 2024-04-28 00:04:41
合計ジャッジ時間 4,136 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 66 ms
36,948 KB
testcase_01 AC 54 ms
37,068 KB
testcase_02 AC 53 ms
37,048 KB
testcase_03 AC 53 ms
37,304 KB
testcase_04 AC 53 ms
37,288 KB
testcase_05 AC 65 ms
37,468 KB
testcase_06 AC 53 ms
36,844 KB
testcase_07 AC 55 ms
37,160 KB
testcase_08 AC 53 ms
37,288 KB
testcase_09 AC 69 ms
37,492 KB
testcase_10 AC 66 ms
37,432 KB
testcase_11 AC 65 ms
36,924 KB
testcase_12 AC 64 ms
37,288 KB
testcase_13 AC 53 ms
37,152 KB
testcase_14 AC 53 ms
37,272 KB
testcase_15 AC 54 ms
36,956 KB
testcase_16 AC 53 ms
36,944 KB
testcase_17 AC 53 ms
37,304 KB
testcase_18 AC 53 ms
37,180 KB
testcase_19 AC 54 ms
36,996 KB
testcase_20 AC 55 ms
37,160 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