Posted onSymbols count in article: 836Reading time ≈3 mins.
Problem
Synchronized Subsequence
Timelimit:2Sec Memorylimit:1024MB
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 ith occurrence of a and the ith 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≤N≤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: NS
Output
Print the lexicographically largest string that satisfies the condition.
Sample
Input #1
1 2
3 aababb
Output #1
1
abab
Explanation #1
A subsequence of T obtained from taking the first, third, fourth and sixth characters in S, satisfies the condition. Input #2
1 2
3 bbabaa
Output #2
1
bbabaa
Explanation #2
You can choose all the characters. Input #3