#include using namespace std; #define rep(a) for(int i=0;isputn(d, len) != len) { dest.setstate(ios_base::badbit); } } return dest; } __uint128_t bigMul(__uint128_t a,__uint128_t b,__uint128_t m){ __uint128_t a_low=a&0xffffffffffffffff,a_high=a>>64,b_low=b&0xffffffffffffffff,b_high=b>>64,result; result=a_high*b_high%m; rep(64)result=result*2%m; result=(result+a_low*b_high+b_low*a_high)%m; rep(64)result=result*2%m; result=(result+a_low*b_low)%m; return result; } __uint128_t bigPowMod(__uint128_t n, __uint128_t p, __uint128_t m){ __uint128_t ans = 1, ln = n; if(p <= 0) return 1; while(p != 0){ if((p & 1) == 1) ans = bigMul(ans, ln, m); ln = bigMul(ln, ln, m); p = p >> 1; } return ans; } bool suspect(int a, int s, __uint128_t d, __uint128_t n) { __uint128_t x = bigPowMod(a, d, n); if (x == 1)return true; for (int r = 0; r < s; ++r) { if (x == n - 1)return true; x = bigMul(x, x, n); } return false; } const vectortest={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97}; bool MillerRabin(__uint128_t n){ if(n<=100){ for(auto&i:test)if(i==n)return true; return false; } if(n%2==0)return false; __uint128_t d = n - 1; int s = 0; while (d%2==0){ s++; d /= 2; } for(auto&i:test)if(!suspect(i, s, d, n))return false; return true; } priority_queue,bool>,vector,bool>>,greater,bool>>>q; set>>s; bool check(string num,__uint128_t num_i,vector& cards){ if(s.count({num,cards}))return false; s.insert({num,cards}); if(accumulate(cards.begin(),cards.begin()+10,0)==0){ if(accumulate(cards.begin()+10,cards.end(),0)==0)return true; if((int(num_i%11)-cards[10]+cards[12]+cards[13]*2+11)%11==0)return false; } bool flag=false; for(int i=1;i<14;i+=2)if(i!=5&&cards[i]){ flag=true; break; } return flag; } string to_s(int a){ string s=to_string(a); for(char&i:s)i='0'+('9'-i); return s; } signed main(){ int n; cin>>n; if(n==1){ int a; cin>>a; if(MillerRabin(a)){ cout<cards(14); vectora(n); rep(n)cin>>a[i]; if(accumulate(a.begin(),a.end(),0)%3==0)return puts("-1")&0; rep(n)cards[a[i]]++; q.push({"",0,cards,false}); while(!q.empty()){ string num=get<0>(q.top()); __uint128_t num_i=get<1>(q.top()); vectorcards=get<2>(q.top()); bool one=get<3>(q.top()),flag=true; q.pop(); for(int i=2;i<14;i++){ if(i==11){ if(cards[1]){ flag=false; if(one)continue; string num2=num; __uint128_t num_i2=num_i; int one=cards[1],eleven=cards[11]; while(cards[1]||cards[11]){ num_i2=num_i2*10+1; num2+=to_s(1); if(one!=cards[1]&&cards[11]){ cards[1]++; cards[11]--; } else cards[1]--; if(check(num2,num_i2,cards))q.push({num2,num_i2,cards,true}); } cards[1]=one; cards[11]=eleven; } else if(cards[11]){ flag=false; if(one)continue; cards[i]--; if(check(num+to_s(i),num_i*(i<10?10:100)+i,cards))q.push({num+to_s(i),num_i*(i<10?10:100)+i,cards,false}); cards[i]++; } } if(cards[i]){ flag=false; cards[i]--; if(check(num+to_s(i),num_i*(i<10?10:100)+i,cards))q.push({num+to_s(i),num_i*(i<10?10:100)+i,cards,false}); cards[i]++; } } if(flag&&MillerRabin(num_i)){ cout<