結果

問題 No.774 tatyamと素数大富豪
ユーザー 37zigen37zigen
提出日時 2020-04-20 04:11:49
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,531 ms / 2,000 ms
コード長 1,472 bytes
コンパイル時間 2,599 ms
コンパイル使用メモリ 80,420 KB
実行使用メモリ 60,244 KB
最終ジャッジ日時 2024-04-16 00:31:21
合計ジャッジ時間 10,987 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,531 ms
60,244 KB
testcase_01 AC 134 ms
41,288 KB
testcase_02 AC 114 ms
40,228 KB
testcase_03 AC 261 ms
46,244 KB
testcase_04 AC 140 ms
40,552 KB
testcase_05 AC 135 ms
41,464 KB
testcase_06 AC 127 ms
41,180 KB
testcase_07 AC 129 ms
41,536 KB
testcase_08 AC 115 ms
40,232 KB
testcase_09 AC 131 ms
41,520 KB
testcase_10 AC 153 ms
41,764 KB
testcase_11 AC 269 ms
44,848 KB
testcase_12 AC 1,237 ms
54,364 KB
testcase_13 AC 912 ms
54,060 KB
testcase_14 AC 129 ms
41,196 KB
testcase_15 AC 329 ms
48,020 KB
testcase_16 AC 276 ms
45,556 KB
testcase_17 AC 203 ms
42,276 KB
testcase_18 AC 268 ms
44,660 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	void run() {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		String[] a=new String[n];
		ArrayList<String> b=new ArrayList<String>();
		long sum=0;
		for (int i=0;i<n;++i) {
			a[i]=sc.next();
			sum+=Integer.valueOf(a[i]);
		}
		Arrays.sort(a);
		int[] ord=new int[n];
		{
			int p=0;
			for (int i=0;i<n;++i) {
				b.add(a[i]);
				int j=i;
				while (j+1<n && a[i].equals(a[j+1])) {
					++j;
				}
				for (int k=i;k<=j;++k) {
					ord[k]=p;
				}
				i=j;
				++p;
			}
		}
		long ans=-1;
		if (sum%3!=0&&sum%9!=0) {
			do {
				StringBuilder v=new StringBuilder();
				for (int i=0;i<n;++i) v.append(b.get(ord[i]));
				if (ans>Long.valueOf(v.toString())) continue;
				if (new BigInteger(v.toString()).isProbablePrime(10)) ans=Math.max(ans, Long.valueOf(v.toString()));
			} while (nextPermutation(ord));
		}
		System.out.println(ans);
	}
	
	boolean nextPermutation(int[] a) {
		int n=a.length;
		int p=n-1;
		while (p>0 && a[p-1]>=a[p]) --p;
		if (p==0) return false;
		int q=p;
		while (q+1<n && a[p-1]<a[q+1]) ++q;
		a[p-1]^=a[q];a[q]^=a[p-1];a[p-1]^=a[q];
		int s=p,t=n-1;
		while (s<t) {
			a[s]^=a[t];a[t]^=a[s];a[s]^=a[t];
			++s;--t;
		}
		return true;
	}
	
	void tr(Object...objects) {System.out.println(Arrays.deepToString(objects));}
	
	public static void main(String[] args) {
    	new Main().run();
    }
}

0