問題一覧 > 通常問題

No.774 tatyam and Prime Daifugo

レベル : / AI推定難易度 : 1025 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 49
作問者 : tatyam / テスター : Shuz*
お気に入りにしたユーザー ProblemId : 2269 / 自分の提出
問題文最終更新日: 2026-10-20 09:30:16
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
Advent Calendar Contest 2018 (順位表) の他の問題:

Problem Statement

tatyam has split into multiple clones and is playing a card game called Prime Daifugo. In Prime Daifugo, several playing cards are dealt as a hand, and the person who gets rid of their hand wins.
The player whose turn it is may choose some cards from their hand, concatenate them in any order, and play them if the result is a prime number. tatyam is aiming to win in one go by using all the cards in the hand.
Find the largest prime number that can be made using all the cards in the hand.

Input

\(n\)
\(a_1\) \(a_2\) … \(a_n\)

All input values are integers.
The first line contains the number of cards dealt \(n\) (\(1 \leq n \leq 9\)).
The second line contains the dealt cards \(a_i\) (\(1 \leq a_1 \leq a_2 \leq … \leq a_n \leq 13\)).

Output

Output the largest prime number that can be made by rearranging and using all the cards in the hand.
If no prime number can be made, output -1.
Print a newline at the end.

Sample

Sample 1
Input
3
1 7 9
Output
971

Among the numbers that can be made by rearranging 1, 7, 9, the primes are 179, 197, 719, 971, so output the largest of them, 971.

Sample 2
Input
9
13 13 13 13 13 13 13 13 13
Output
-1

Any arrangement results in a multiple of 13.

Sample 3
Input
7
1 2 4 6 8 10 12
Output
862412101

This is a famous even-card-consuming prime.

Sample 4
Input
5
1 7 7 9 13
Output
971713

This is larger than 971371.

Sample 5
Input
6
8 13 13 13 13 13
Output
81313131313

Be careful of overflow.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。