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 Java7 (OpenJDK 1.7.0) (judge verdict: Accepted)

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskE solver = new TaskE();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskE {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            String s = in.next();
            int[] balance = new int[2 * n + 1];
            for (int i = 0; i < 2 * n; ++i) {
                int w = s.charAt(i) - 'a';
                balance[i + 1] = balance[i] + 2 * w - 1;
            }
            int start = 0;
            List<String> sections = new ArrayList<>();

            for (int i = 1; i <= 2 * n; ++i)
                if (balance[i] == 0) {
                    String cur = solveSection(s.substring(start, i));
                    while (!sections.isEmpty() && (sections.get(sections.size() - 1) + cur).compareTo(cur) < 0) {
                        sections.remove(sections.size() - 1);
                    }
                    sections.add(cur);
                    start = i;
                }
            for (String x : sections) out.print(x);
            out.println();
        }

        private String solveSection(String s) {
            if (s.charAt(0) == 'a') {
                return solveASection(s);
            } else {
                return solveBSection(s);
            }
        }

        private String solveBSection(String s) {
            int n = s.length() / 2;
            int[] cnt = new int[2];
            int[] index = new int[2 * n];
            int[][] where = new int[2][n];
            for (int i = 0; i < 2 * n; ++i) {
                int w = s.charAt(i) - 'a';
                index[i] = cnt[w];
                where[w][cnt[w]] = i;
                ++cnt[w];
            }
            String res = "";
            for (int first = 0; first < n; ++first) {
                StringBuilder b = new StringBuilder();
                for (int i = 0; i < 2 * n; ++i) if (index[i] >= first) b.append(s.charAt(i));
                String t = b.toString();
                if (t.compareTo(res) > 0) res = t;
            }
            return res;
        }

        private String solveASection(String s) {
            int n = s.length() / 2;
            int[] cnt = new int[2];
            int[] index = new int[2 * n];
            int[][] where = new int[2][n];
            for (int i = 0; i < 2 * n; ++i) {
                int w = s.charAt(i) - 'a';
                index[i] = cnt[w];
                where[w][cnt[w]] = i;
                ++cnt[w];
            }
            StringBuilder res = new StringBuilder();
            int atleast = 0;
            for (int i = 0; i < 2 * n; ++i) {
                if (s.charAt(i) == 'b') {
                    int pa = where[0][index[i]];
                    if (pa >= atleast) {
                        res.append("ab");
                        atleast = i + 1;
                    }
                }
            }
            return res.toString();
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}