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: Wrong Answer)

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef pair<ll, ll> LP;
typedef vector<ll> vec;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-5;

bool used[6000];

int loc[6000];
int va[3000];
int vb[3000];

vector<int> z[6000];
typedef bitset<6000> bt;
bt bz[6000];
int n; string s;

bool chked[6000];
bt ans[6001];
bool valid[6000];

string trans(bt &b) {
	string ret;
	rep(i, 2 * n) {
		if (b[i]) {
			ret.push_back(s[i]);
		}
	}
	return ret;
}
int fre(int i) {
	int id = loc[i];
	if (s[i] == 'a')return vb[id];
	else return va[id];
}

void dfs(int le) {
	if (le == 2 * n)return;
	if (chked[le])return;
	chked[le] = true;
	int len = 0;
	Rep(i, le, 2 * n) {
		len = max(len, (int)z[i].size());
	}
	if (len == 0) {
		string ma = "";
		Rep(i, le, 2 * n) {
			if (valid[i]) {
				int to = i + 1;
				while (to < 2 * n && fre(to)<to)to++;
				dfs(to);
				bt nex = ans[to];
				nex[i] = 1; nex[vb[loc[i]]] = 1;
				string snex = trans(nex);
				if (ma < snex) {
					ma = snex;
					ans[le] = nex;
				}
			}
		}
		return;

		int to = le + 1;
		while (to < 2 * n && fre(to)<to)to++;
		dfs(to);
		ans[le] = ans[to];
		ans[le][le] = 1;
		if (s[le] == 'a') {
			int id = loc[le];
			ans[le][vb[id]] = 1;
		}
		else {
			int id = loc[le];
			ans[le][va[id]] = 1;
		}
	}
	else {
		string ma = "";
		Rep(i, le, 2 * n) {
			if (z[i].size() == len) {
				int to = z[i].back() + 1;
				while (to < 2 * n && fre(to)<to)to++;
				dfs(to);
				bt nex = bz[i]|ans[to];
				string snex = trans(nex);
				if (ma < snex) {
					ma = snex;
					ans[le] = nex;
				}
			}
		}
	}
	//cout << le << " " << trans(ans[le]) << endl;
}
void solve(){
	cin >> n >> s;
	int c[2] = {};
	rep(i, 2 * n) {
		if (s[i] == 'a') {
			va[c[0]] = i;
			loc[i] = c[0];
			c[0]++;
		}
		else {
			vb[c[1]] = i;
			loc[i] = c[1];
			c[1]++;
		}
	}
	fill(valid, valid + 2 * n, true);
	rep(i, 2 * n) {
		int id = loc[i];
		if (s[i] == 'a') {
			if (vb[id] < i)valid[i] = false;
		}
		else {
			if (va[id] < i)valid[i] = false;
		}
	}
	rep(i, 2 * n) {
		if (s[i] == 'b') {
			int id = loc[i];
			if (va[id] > i) {
				z[i].push_back(i);
				bz[i][i] = 1;
				bz[i][va[loc[i]]] = 1;
				int ri = va[id];
				Rep(j, i + 1, ri) {
					if (s[j] == 'b') {
						int iid = loc[j];
						if (va[iid] > j) {
							z[i].push_back(j);
							bz[i][j] = 1;
							bz[i][va[loc[j]]] = 1;
						}
					}
				}
			}
		}
	}
	dfs(0);
	cout << trans(ans[0]) << endl;
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(5);
	//init();
	solve();

	//cout << "finish" << endl;
	//stop
	return 0;
}