Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Best Matched Pair

You are working for a worldwide game company as an engineer in Tokyo. This company holds an annual event for all the staff members of the company every summer. This year's event will take place in Tokyo. You will participate in the event on the side of the organizing staff. And you have been assigned to plan a recreation game which all the participants will play at the same time.

After you had thought out various ideas, you designed the rules of the game as below.

Each player is given a positive integer before the start of the game.

Each player attempts to make a pair with another player in this game, and formed pairs compete with each other by comparing the products of two integers.

Each player can change the partner any number of times before the end of the game, but cannot have two or more partners at the same time.

At the end of the game, the pair with the largest product wins the game.

In addition, regarding the given integers, the next condition must be satisfied for making a pair.

The sequence of digits obtained by considering the product of the two integers of a pair as a string must be increasing and consecutive from left to right. For example, 2, 23, and 56789 meet this condition, but 21, 334, 135 or 89012 do not.

Setting the rules as above, you noticed that multiple pairs may be the winners who have the same product depending on the situation. However, you can find out what is the largest product of two integers when a set of integers is given.

Your task is, given a set of distinct integers which will be assigned to the players, to compute the largest possible product of two integers, satisfying the rules of the game mentioned above.

Input

The input consists of a single test case formatted as follows.

$N$

$a_1$ $a_2$ ... $a_N$

The first line contains a positive integer $N$ which indicates the number of the players of the game. $N$ is an integer between 1 and 1,000. The second line has $N$ positive integers that indicate the numbers given to the players. For $i = 1, 2, ... , N - 1$, there is a space between $a_i$ and $a_{i+1}$. $a_i$ is between 1 and 10,000 for $i = 1, 2, ..., N$, and if $i \ne j$, then $a_i \ne a_j$.

Output

Print the largest possible product of the two integers satisfying the conditions for making a pair. If any two players cannot make a pair, print -1.

Sample Input 1

2
1 2

Output for the Sample Input 1

2

Sample Input 2

3
3 22 115

Output for the Sample Input 2

345

Sample Input 3

2
1 11

Output for the Sample Input 3

-1

Sample Input 4

2
5 27

Output for the Sample Input 4

-1

Sample Input 5

2
17 53

Output for the Sample Input 5

-1

Sample Input 6

10
53 43 36 96 99 2 27 86 93 23

Output for the Sample Input 6

3456

Solution in C++11 (judge verdict: Accepted)

#include <bits/stdc++.h>


//#define gc getchar unlocked
//#ifndef ONLINE JUDGE
#define gc getchar
//#endif // ONLINE JUDGE

//#define pc putchar_unlocked
//#ifndef ONLINE JUDGE
#define pc putchar
//#endif // ONLINE JUDGE

#define fRead           freopen("input.txt","r",stdin)
#define fWrite          freopen ("output.txt","w",stdout)

#define eps             1e-8
#define inf             0x3f3f3f3f
#define INF             2e18
#define LL              long long
#define ULL             unsigned long long
#define PI              acos(-1.0)
#define pb              push_back
#define mk              make_pair
#define pii             pair<int,int>
#define pLL             pair<LL,LL>
#define ff              first
#define ss              second
#define all(a)          a.begin(),a.end()
#define rall(a)         a.rbegin(),a.rend()
#define SQR(a)          ((a)*(a))
#define Unique(a)       sort(all(a)),a.erase(unique(all(a)),a.end())
#define min3(a,b,c)     min(a,min(b,c))
#define max3(a,b,c)     max(a,max(b,c))
#define min4(a,b,c,d)   min(min(a,b),min(c,d))
#define max4(a,b,c,d)   max(max(a,b),max(c,d))
#define max5(a,b,c,d,e) max(max3(a,b,c),max(d,e))
#define min5(a,b,c,d,e) min(min3(a,b,c),min(d,e))
#define Iterator(a)     __typeof__(a.begin())
#define rIterator(a)    __typeof__(a.rbegin())
#define FOR(a,it)       for(Iterator(a) it = a.begin();it != a.end(); it++)
#define rFOR(a,it)      for(rIterator(a) it = a.rbegin();it != a.rend(); it++)
#define FastRead        ios_base::sync_with_stdio(0);cin.tie(0)
#define CasePrint       pc('C'); pc('a'); pc('s'); pc('e'); pc(' '); write(qq++,false); pc(':'); pc(' ')

using namespace std;

const int MOD = 1e9 + 7;
int fx[] = {+1,-1,+0,+0,+1,+1,-1,-1,+0};
int fy[] = {+0,+0,+1,-1,+1,-1,+1,-1,+0};
template <typename T> inline T GCD (T a,T b ) {a = abs(a);b = abs(b); if(a < b) swap(a, b); while ( b ) { a = a % b; swap ( a, b ); } return a;}
template <typename T> inline T EGCD(T a,T b,T &x,T &y){if(a == 0) {x = 0;y = 1;return b;}T x1, y1;T d = EGCD(b % a, a, x1, y1);x = y1 - (b / a) * x1;y = x1;return d;}
template <typename T> inline T LCM(T x,T y){T tp = GCD(x,y);if( (x / tp) * 1. * y > 9e18) return 9e18;return (x / tp) * y;}
template <typename T> inline T BigMod(T A,T B,T M = MOD){T ret = 1;while(B){if(B & 1) ret = (ret * A) % M;A = (A * A) % M;B = B >> 1;}return ret;}
template <typename T> inline T InvMod (T A,T M = MOD){return BigMod(A,M-2,M);}
/*---------------------------fast I/O------------------------------------*/
#define scani2(a,b) scani(a) , scani(b)
#define scani3(a,b,c) scani(a), scani(b), scani(c)
#define scani4(a,b,c,d) scani(a), scani(b), scani(c), scani(d)
#define scani5(a,b,c,d,e) scani(a), scani(b), scani(c), scani(d) , scani(e)
template <typename T> T scani(T &n){n = 0;bool negative = false;char c = gc();while( c < '0' || c > '9'){if(c == '-') negative = true;c = gc();}while(c >= '0' && c <= '9'){n = n*10 + c-48;c = gc();}if(negative) n = ~(n-1);return n;}
template <typename T> void write(T n,int type = true){if(n<0) {pc('-');n = -n;}if(!n) {pc('0');if(type==32) pc(' '); else if(type) pc('\n'); return;}char buff[22];int len = 0;while(n) buff[len++] = n%10+48,n/=10;for(int i=len-1;i>=0;i--) pc(buff[i]);if(type==32) pc(' '); else if(type) pc('\n');}
int scans(char *a){int i=0;char c = 0;while(c < 33) c = gc();while(c > 33){a[i++] = c;c = gc();}a[i] = 0;return i;}
/*********************************************** End of template *********************************************/
const int N = 1000 + 10;

int n;
LL in[N];

bool ck(string s)
{
    for(int i=1;i<s.size();i++) if(s[i] != s[i - 1] + 1) return false;
    return true;
}

int main()
{
    LL mx = -1;
    scani(n);
    for(int i=0;i<n;i++) cin >> in[i];
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            LL m = in[i] * in[j];
            stringstream input ;
            input << m;
            string s = input.str();
            if(ck(s)){
                mx = max(mx, m);
            }
        }
    }
    write(mx);
//    cout << mx << endl;
    return 0;
}