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++14 (judge verdict: Accepted)

#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>

namespace Detail {
template <typename Container>
class BitChooseIterator {
public:
    using const_reference = typename Container::const_reference;

    BitChooseIterator(const Container& container, const std::vector<bool>& bits)
        : container_{container}
        , bits_{bits}
        , currentIndex_{nextIndex(0)}
    {
    }

    BitChooseIterator& operator++()
    {
        currentIndex_ = nextIndex(currentIndex_ + 1);
        return *this;
    }

    const BitChooseIterator operator++(int) { return operator++(); }

    bool operator==(const BitChooseIterator& rhs) const
    {
        return currentIndex_ == rhs.currentIndex_;
    }

    bool operator!=(const BitChooseIterator& rhs) const
    {
        return !(*this == rhs);
    }

    const_reference operator*() const { return container_[currentIndex_]; }

    BitChooseIterator end() const
    {
        auto it = *this;
        it.currentIndex_ = bits_.size();
        return it;
    }

private:
    const Container& container_;
    const std::vector<bool>& bits_;
    std::size_t currentIndex_;

    std::size_t nextIndex(std::size_t start)
    {
        for (std::size_t i = start; i < bits_.size(); ++i) {
            if (bits_[i]) {
                return i;
            }
        }
        return bits_.size();
    }
};

template <typename Container>
class BitChooseRange {
public:
    using Iterator = BitChooseIterator<Container>;

    BitChooseRange(const Container& container, const std::vector<bool>& bits)
        : begin_{container, bits}
    {
    }

    Iterator begin() { return begin_; }
    Iterator end() { return begin_.end(); }

private:
    Iterator begin_;
};

template <typename Container>
auto makeChooseRange(const Container& container, const std::vector<bool>& bits)
{
    return BitChooseRange<Container>(container, bits);
}

// TODO: more efficient implementation
template <typename Container>
class CombinationsIterator {
public:
    CombinationsIterator(const Container& container, std::size_t k)
        : container_{container}
        , n_{container.size()}
        , k_{k}
        , mask_(n_)
    {
        assert(n_ >= k_);
        std::fill_n(std::rbegin(mask_), k_, true);
    }

    CombinationsIterator& operator++()
    {
        std::next_permutation(std::begin(mask_), std::end(mask_));
        count_ += 1;
        return *this;
    }

    const CombinationsIterator operator++(int) { return operator++(); }

    bool operator==(const CombinationsIterator& rhs) const
    {
        return count_ == rhs.count_;
    }

    bool operator!=(const CombinationsIterator& rhs) const
    {
        return !(*this == rhs);
    }

    BitChooseRange<Container> operator*() const
    {
        return makeChooseRange(container_, mask_);
    }

    CombinationsIterator end() const
    {
        std::size_t p = 1;
        std::size_t q = 1;
        for (std::size_t i = k_; i != 0; --i) {
            p *= n_ - (k_ - i);
            q *= i;
        }

        CombinationsIterator it(container_, k_);
        it.count_ = p / q;
        return it;
    }

private:
    const Container& container_;
    const std::size_t n_;
    const std::size_t k_;
    std::size_t count_ = 0;
    std::vector<bool> mask_;
};

template <typename Container>
class CombinationsRange {
public:
    using Iterator = CombinationsIterator<Container>;

    CombinationsRange(const Container& container, std::size_t k)
        : begin_{container, k}
    {
    }

    Iterator begin() { return begin_; }
    Iterator end() { return begin_.end(); }

private:
    Iterator begin_;
};

template <typename Container>
auto makeCombinationsRange(const Container& container, std::size_t k)
{
    return CombinationsRange<Container>(container, k);
}
} // namespace Detail

template <typename Container>
auto combinations(const Container& container, std::size_t k)
{
    return Detail::makeCombinationsRange(container, k);
}

using namespace std;

int main()
{
    int n;
    cin >> n;

    vector<int> v;
    for (int i = 0; i < n; ++i) {
        int a;
        cin >> a;
        v.push_back(a);
    }
    sort(begin(v), end(v));
    auto check = [](int x) {
        auto s = to_string(x);
        for (size_t i = 0; i < s.size() - 1; ++i) {
            if (s[i] + 1 != s[i + 1]) {
                return false;
            }
        }
        return true;
    };
    int max_ = -1;
    for (auto combinationIter : combinations(v, 2)) {
        int p = 1;
        for (auto&& i : combinationIter) {
            p *= i;
        }
        if (check(p)) {
            max_ = max(max_, p);
        }
    }
    cout << max_ << endl;
}