Problem:
Score : 1600 points

Problem Statement

You are given a string S of length 2N, containing N occurrences of a and N occurrences of b.

You will choose some of the characters in S. Here, for each i = 1,2,...,N, it is not allowed to choose exactly one of the following two: the i-th occurrence of a and the i-th occurrence of b. (That is, you can only choose both or neither.) Then, you will concatenate the chosen characters (without changing the order).

Find the lexicographically largest string that can be obtained in this way.

Constraints

1 \leq N \leq 3000

S is a string of length 2N containing N occurrences of a and N occurrences of b.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the lexicographically largest string that satisfies the condition.

Sample Input 1

3
aababb

Sample Output 1

abab

A subsequence of T obtained from taking the first, third, fourth and sixth characters in S, satisfies the condition.

Sample Input 2

3
bbabaa

Sample Output 2

bbabaa

You can choose all the characters.

Sample Input 3

6
bbbaabbabaaa

Sample Output 3

bbbabaaa

Sample Input 4

9
abbbaababaababbaba

Sample Output 4

bbaababababa

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

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 3008;
typedef vector<int> vec;
typedef vec::iterator it;

char s[N*2];
int d[N], c[N], *L[N], *R[N];

int Lyndon(int i) {
	while (d[i]) {
		int j = i, k = i + 1;
		for (; d[k] && d[j] >= d[k]; k++)
			if (d[j] > d[k]) j = i;
			else j++;
		if (!d[k]) return i;
		k -= j;
		while (i <= j) i += k;
	}
	return i;
}

void out(int *l, int *r) {
	int *t = c;
	while (l < r) {
		putchar('b');
		t[*l++]++;
		while (*t) putchar('a'), --*t;
		t++;
	}
}

int main() {
	scanf("%*d%s",s);
	int a = 0, b = 0, n = 0;
	for (int i = 0; s[i]; i++) {
		if (s[i] == 'b') b++;
		else d[a++] = b;
		if (a < b) n = b;
	}
	int l = 0, top = -1;
	for (int i = 0; i < n; i++)
		if (!(d[i] -= i + 1)) {
			int t = Lyndon(l); l = i + 1;
			while (~top && lexicographical_compare(L[top], R[top], d+t, d+l)) top--;
			L[++top] = d+t; R[top] = d+l;
		}
		else if (d[i] < 0) l = i + 1;
	for (int i = 0; i <= top; i++)
		out(L[i], R[i]);
	for (int i = n; i < b; i++)
		if (d[i] >= n) n = i+1, printf("ab");
	return 0;
}