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 Ruby (2.3.3) (judge verdict: Time Limit Exceeded)

#
# File    : E.rb
# Author  : Kazune Takahashi
# Created : 2018-7-16 13:05:22
# Powered by Visual Studio Code
#

n = gets.to_i
s = gets.chomp

ary = []
while !(s == "")
  a = 0
  b = 0
  for i in 0...s.size()
    if s[i] == "a"
      a += 1
    else
      b += 1
    end
    if a == b
      ary << s[0..i]
      s = s[i+1..-1]
      break
    end
  end
end

ans = []

ary.each{|str|
  num = []
  a = 0
  b = 0
  str.chars{|c|
    if c == 'a'
      num << a
      a += 1
    else
      num << b
      b += 1
    end
  }
  if str[0] == "a"
    res = ""
    _a = true
    search = nil
    for i in 0..str.size
      if _a && str[i] == 'a'
        res += str[i]
        search = num[i]
        _a = false
      else (!_a) && str[i] == 'b'
        if num[i] == search
          res += str[i]
          _a = true
        end
      end
    end
    ans << res
  else
    res = []
    for k in 0..str.size/2
      temp = ""
      for i in 0...str.size
        if num[i] >= k
          temp += str[i]
        end
      end
      res << temp
    end
    ans << res.max
  end
}

res = ""

ans.reverse.each{|str|
  if res < str + res
    res = str + res
  end
}

puts res