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<bits/stdc++.h>
using namespace std;
using Int = long long;

template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

//INSERT ABOVE HERE
string calc(string s){
  //cout<<s<<endl;
  string res;
  Int n=s.size();
  if(s[0]=='a'){
    vector<Int> a(n*2);
    for(Int i=0,j=0;i<n;i++){
      if(s[i]=='b') continue;
      while(s[j]=='a') j++;
      a[i]=j++;
    }
    for(Int i=0;i<n;i++){
      if(s[i]=='x') continue;
      assert(s[i]=='a');
      res+="ab";
      for(Int j=i;j<a[i];j++)
	if(s[j]=='a') s[j]=s[a[j]]='x';
    }
  }else{
    for(Int k=0;k<n;k++){
      Int a=0,b=0;
      string tmp;
      for(Int i=0;i<n;i++){
	if(s[i]=='a'){
	  if(a>=k) tmp+=s[i];
	  a++;
	}else{
	  if(b>=k) tmp+=s[i];
	  b++;
	}
      }
      chmax(res,tmp);
    }
  }
  //cout<<res<<endl;
  return res;
}

signed main(){
  Int n;
  string s;
  cin>>n>>s;
  n*=2;
  vector<string> v;
  Int x[2]={1,-1};
  for(Int i=0;i<n;){
    Int j=i,k=x[s[j++]-'a'];
    while(j<n&&k!=0) k+=x[s[j++]-'a'];
    assert(k==0);
    v.emplace_back(calc(s.substr(i,j-i)));
    //cout<<i<<" "<<j<<endl;
    i=j;
  }
  reverse(v.begin(),v.end());
  string ans;
  for(string t:v)
    chmax(ans,t+ans);
  cout<<ans<<endl;
  return 0;
}