Problem:
Score : 1400 points

Problem Statement

You are given P, a permutation of (1,\ 2,\ ...\ N).

A string S of length N consisting of 0 and 1 is a good string when it meets the following criterion:

The sequences X and Y are constructed as follows:

First, let X and Y be empty sequences.

For each i=1,\ 2,\ ...\ N, in this order, append P_i to the end of X if S_i= 0, and append it to the end of Y if S_i= 1.

If X and Y have the same number of high elements, S is a good string.
Here, the i-th element of a sequence is called high when that element is the largest among the elements from the 1-st to i-th element in the sequence.

Determine if there exists a good string. If it exists, find the lexicographically smallest such string.

Constraints

1 \leq N \leq 2 \times 10^5

1 \leq P_i \leq N

P_1,\ P_2,\ ...\ P_N are all distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_1 P_2 ... P_N

Output

If a good string does not exist, print -1.
If it exists, print the lexicographically smallest such string.

Sample Input 1

6
3 1 4 6 2 5

Sample Output 1

001001

Let S= 001001. Then, X=(3,\ 1,\ 6,\ 2) and Y=(4,\ 5).
The high elements in X is the first and third elements, and the high elements in Y is the first and second elements.
As they have the same number of high elements, 001001 is a good string.
There is no good string that is lexicographically smaller than this, so the answer is 001001.

Sample Input 2

5
1 2 3 4 5

Sample Output 2

-1

Sample Input 3

7
1 3 2 5 6 4 7

Sample Output 3

0001101

Sample Input 4

30
1 2 6 3 5 7 9 8 11 12 10 13 16 23 15 18 14 24 22 26 19 21 28 17 4 27 29 25 20 30

Sample Output 4

000000000001100101010010011101

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>
using namespace std;

typedef long long LL;
typedef vector<int> VI;

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)

template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
template<class Iter> void rprintf(const char *fmt, Iter begin, Iter end) {
    for (bool sp=0; begin!=end; ++begin) { if (sp) putchar(' '); else sp = true; printf(fmt, *begin); }
    putchar('\n');
}

template<class T> struct RXQ {
    static const int npos = -1;

    int n, m;
    vector<T> d;
    RXQ() {}
    RXQ(int n_, const T init=T()) : n(n_), m(2<<__lg(max(1, n))), d(2*m, init) {}
    template<class Iter> void build(Iter begin, Iter end, const T init=T()) {
	n = end - begin;
	m = 2<<__lg(max(1, n));
	d.resize(2*m);
	for (int i=0; begin!=end; ++begin, i++) d[i+m] = *begin;
	for (int i=n; i<m; i++) d[i+m] = init;
	for (int i=m; --i; ) d[i] = max(d[i*2+1], d[i*2]);
    };

    const T at(int i) const { return d[i+m]; }

    void modify(int i, const T v) {
	i += m; d[i] = v;
	for (i>>=1; i; i>>=1) d[i] = max(d[i*2+1], d[i*2]);
    }

    T max_v(int l, int r) const {
	l += m; r += m;
	T ret = d[l++];
	for (; l<r; l>>=1, r>>=1) {
	    if (l & 1) { amax(ret, d[l]); l++; }
	    if (r & 1) { r--; amax(ret, d[r]); }
	}
	return ret;
    }

    int max_i(int l, int r) const {
	return first_more_equeal(l, max_v(l, r));
    }

    // for i in [l .. n-1]:
    //   if at(i) > v: return i;
    // return npos;
    int first_more_than(int l, const T v) const {
	int r = 2*m;
	for (l+=m; l<r; l>>=1, r>>=1) {
	    if (l & 1) {
		if (v < d[l]) break;
		l++;
	    }
	}
	if (l >= r) return npos;
	for (; l<m;) {
	    if (v < d[l*2]) l = l*2;
	    else l = l*2+1;
	}
	return l - m;
    }

    // for i in [l .. n-1]:
    //   if v <= at(i): return i;
    // return npos;
    int first_more_equal(int l, const T v) const { 
	int r = 2*m;
	for (l+=m; l<r; l>>=1, r>>=1) {
	    if (l & 1) {
		if (v <= d[l]) break;
		l++;
	    }
	}
	if (l >= r) return npos;
	for (; l<m;) {
	    if (v <= d[l*2]) l = l*2;
	    else l = l*2+1;
	}
	return l - m;
    }

    // for i in [r-1 .. 0]:
    //   if v < at(i): return i;
    // return npos;
    int last_more_than(int r, const T v) const {
	if (r < m) {
	    for (r+=m; 1<r; r>>=1) {
		if (r & 1) {
		    r--;
		    if (v < d[r]) break;
		}
	    }
	    if (r <= 1) return npos;
	} else {
	    if (v < d[1]) r = 1;
	    else return npos;
	}
	for (; r<m; ) {
	    if (v < d[r*2+1]) r = r*2+1;
	    else r = r*2;
	}
	return r - m;
    }

    // for i in [r-1 .. 0]:
    //   if v <= at(i): return i;
    // return npos;
    int last_more_equal(int r, const T v) const { 
	if (r < m) { 
	    for (r+=m; 1<r; r>>=1) {
		if (r & 1) {
		    r--;
		    if (v <= d[r]) break;
		}
	    }
	    if (r <= 1) return npos;
	} else {
	    if (v <= d[1]) r = 1;
	    else return npos;
	}
	for (; r<m; ) {
	    if (v <= d[r*2+1]) r = r*2+1;
	    else r = r*2;
	}
	return r - m;
    }
};
int N;
int A[200011];

bool H[200011];
const int INF = 1<<29;
RXQ<int> S[2];

char ans[200011];

void MAIN() {
    scanf("%d", &N);
    REP (i, N) scanf("%d", A+i), A[i]--;

    int high = -1;
    REP (i, N) {
	if (A[i] > high) {
	    H[i] = true;
	    high = A[i];
	}
    }

    S[0] = RXQ<int>(N+2, -INF);
    S[1] = RXQ<int>(N+2, -INF);

    for (int i=N; i--;) {
	int tmp0 = S[0].max_v(A[i]+1, N);
	int tmp1 = S[1].max_v(A[i]+1, N);
	if (H[i]) {
	    S[0].modify(A[i], max(2, tmp0+2));
	    if (tmp1 >= 0) S[1].modify(A[i], tmp1 + 2);
	} else {
	    if (tmp1 >= 0) S[0].modify(A[i], tmp1 + 1);
	    S[1].modify(A[i], max(1, tmp0+1));
	}
    }

    int cntX = 0, cntY = 0, highX = -1, highY = -1, rest = 0;
    REP (i, N) if (H[i]) rest++;

    {
	bool ok = false;
	int target = cntX - cntY + rest;
	if (0 <= target && target <= S[target&1].max_v(0, N)) {
	    ok = true;
	}
	target = cntY - cntX + rest;
	if (!ok && 0 <= target && target <= S[target&1].max_v(0, N)) {
	    ok = true;
	}
	if (!ok) {
	    puts("-1");
	    return;
	}
    }

    REP (i, N) {
	S[0].modify(A[i], -INF);
	S[1].modify(A[i], -INF);

	int cx = cntX + (highX < A[i]? 1: 0);
	int hx = max(highX, A[i]);
	int re = rest - H[i];

	bool ok = false;
	if (!ok) {
	    int target = cx - cntY + re;
	    if (0 <= target && target <= S[target&1].max_v(highY+1, N)) {
		ok = true;
	    }
	}
	if (!ok) {
	    int target = cntY - cx + re;
	    if (0 <= target && target <= S[target&1].max_v(hx+1, N)) {
		ok = true;
	    }
	}

	if (ok) {
	    ans[i] = '0';
	    cntX += (highX < A[i]? 1: 0);
	    amax(highX, A[i]);
	    rest -= H[i];
	} else {
	    ans[i] = '1';
	    cntY += (highY < A[i]? 1: 0);
	    amax(highY, A[i]);
	    rest -= H[i];
	}
    }

    puts(ans);
}

int main() {
    int TC = 1;
//    scanf("%d", &TC);
    REP (tc, TC) MAIN();
    return 0;
}