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 Rust (1.15.1) (judge verdict: Accepted)

#[allow(unused_imports)]
use std::cmp::{min,max};
#[allow(unused_imports)]
use std::collections::BTreeMap;
#[allow(unused_imports)]
use std::ops::*;
#[allow(unused_imports)]
use std::collections::BinaryHeap;

#[allow(unused_macros)]
macro_rules! tf {
    ($c:expr, $t:expr, $f:expr) => {{
        if $c { $t } else { $f }
    }};
}

fn main() {
    let n = read_i64() as usize;
    let ss: Vec<u8> = read_line().bytes().collect();
    let mut xs = vec![0; 2*n+1];
    for i in 0..2*n {
        xs[i+1] = xs[i] + tf!(ss[i] == b'a', 1, -1);
    }
    let ys = {
        let mut ys: Vec<Vec<u8>> = vec![];
        let mut buf = vec![];
        for i in 0..2*n+1 {
            if i == 2*n || xs[i] == 0 {
                if ! buf.is_empty() {
                    ys.push(buf.clone());
                    buf.clear();
                }
            }
            if i != 2 * n {
                buf.push(ss[i]);
            }
        }
        ys
    };
    let zs: Vec<Vec<u8>> = ys.iter().map(|y| {
        let mut ai = vec![];
        let mut bi = vec![];
        let mut si = vec![];
        for (i,&c) in y.iter().enumerate() {
            match c {
                b'a' => {
                    si.push(ai.len());
                    ai.push(i);
                }
                b'b' => {
                    si.push(bi.len());
                    bi.push(i);
                }
                _ => {
                    panic!();
                }
            }
        }
        let mut res: Vec<u8> = vec![];
        match y[0] {
            b'a' => {
                let mut prev = b'b';
                let mut valid: Vec<bool> = vec![true; y.len()];
                for (i,&c) in y.iter().enumerate() {
                    if ! valid[i] {
                        continue;
                    }
                    if c == prev {
                        match c {
                            b'a' => {
                                valid[bi[si[i]]] = false;
                            }
                            b'b' => {
                                valid[ai[si[i]]] = false;
                            }
                            _ => {
                                panic!();
                            }
                        }
                    }
                    else {
                        res.push(c);
                        prev = c;
                    }
                }
            }
            b'b' => {
                let mut valid = vec![true; y.len()];
                for i in 0..y.len() {
                    if ! valid[i] {
                        continue;
                    }
                    if y[i] == b'b' {
                        let mut buf = vec![];
                        for k in i..y.len() {
                            if ! valid[k] {
                                continue;
                            }
                            buf.push(y[k]);
                            if res < buf {
                                res = buf.clone();
                            }
                        }
                        valid[ai[si[i]]] = false;
                    }
                }
            }
            _ => {
                panic!();
            }
        }
        res
    }).collect();
    //eprintln!("{:?}", ys);
    //eprintln!("{:?}", zs);
    let mut res = vec![];
    for z in zs.iter().rev() {
        let mut buf = z.clone();
        for &c in &res {
            buf.push(c);
        }
        if res < buf {
            res = buf.clone();
        }
    }
    println!("{}", std::str::from_utf8(&res).unwrap());
}

#[allow(dead_code)]
fn read_line() -> String {
    let mut ret = String::new();
    std::io::stdin().read_line(&mut ret).ok();
    ret.pop();
    return ret;
}

#[allow(dead_code)]
fn read_i64() -> i64 {
    let ss = read_line();
    return ss.parse::<i64>().unwrap();
}

#[allow(dead_code)]
fn read_vec_i64() -> Vec<i64> {
    let mut res = vec![];
    let ss = read_line();
    for ts in ss.split_whitespace() {
        let x = ts.parse::<i64>().unwrap();
        res.push(x);
    }
    return res;
}

use std::fmt::Display;
#[allow(dead_code)]
fn write_vec<T: Display>(xs: &Vec<T>) {
    if xs.len() == 0 {
        println!("");
        return;
    }
    print!("{}", xs[0]);
    for i in 1..xs.len() {
        print!(" {}", xs[i]);
    }
    println!("");
}