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<cstring>
#define N 6006
char a[N],x[N];int b[N],i,j,n,o,p,q,t,y;
int main()
{
    scanf("%d%s",&n,a+1);
    for(*b=i=j=1;j<=n<<1;j++)if(a[j]==97?o++:p++,o==p)
    if(a[i]==97)
    {
        for(o=p=q=y=0;i<=j;i++)if(a[i]==97&&x[++o,y]!=97||a[i]==98&&++p==q&&x[y]!=98)x[++y]=a[i],q=o;
        memcpy(a+b[t],x+1,y),b[t+1]=b[t]+y,t++;
    }
    else
    {
        for(y=j-i+1,memcpy(x+1,a+i,y),memset(a+b[t],0,y);y;)
        {
            if(strncmp(a+b[t],x+1,y)<0)memcpy(a+b[t],x+1,y),b[t+1]=b[t]+y;
            for(i=1;x[i]==98;i++);
            for(y--;i<=y;i++)x[i]=x[i+1];
            for(i=1;x[i]==97;i++);
            for(y--;i<=y;i++)x[i]=x[i+1];
        }
        i=j+1,t++;
    }
    for(i=t,y=0;i--;)x[y+1]=a[b[i+1]]=0,strncmp(x+1,a+b[i],b[i+1]-b[i])<=0?memcpy(a+b[i+1],x+1,y),y+=b[i+1]-b[i],memcpy(x+1,a+b[i],y):0;
    return 0&(x[y+1]=0,printf("%s\n",x+1));
}