atcoder#ARC136A. [ARC136A] A ↔ BB

[ARC136A] A ↔ BB

Score : 300300 points

Problem Statement

You are given a string SS of length NN consisting of A, B, C.

You can do the following two kinds of operations on SS any number of times in any order.

  • Choose A in SS, delete it, and insert BB at that position.
  • Choose two adjacent characters that are BB in SS, delete them, and insert A at that position.

Find the lexicographically smallest possible string that SS can become after your operations.

What is the lexicographical order?

Simply speaking, the lexicographical order is the order in which words are listed in a dictionary. As a more formal definition, here is the algorithm to determine the lexicographical order between different strings SS and TT.

Below, let SiS_i denote the ii-th character of SS. Also, if SS is lexicographically smaller than TT, we will denote that fact as S<TS \lt T; if SS is lexicographically larger than TT, we will denote that fact as S>TS \gt T.

  1. Let LL be the smaller of the lengths of SS and TT. For each i=1,2,,Li=1,2,\dots,L, we check whether SiS_i and TiT_i are the same.
  2. If there is an ii such that SiTiS_i \neq T_i, let jj be the smallest such ii. Then, we compare SjS_j and TjT_j. If SjS_j comes earlier than TjT_j in alphabetical order, we determine that S<TS \lt T and quit; if SjS_j comes later than TjT_j, we determine that S>TS \gt T and quit.
  3. If there is no ii such that SiTiS_i \neq T_i, we compare the lengths of SS and TT. If SS is shorter than TT, we determine that S<TS \lt T and quit; if SS is longer than TT, we determine that S>TS \gt T and quit.

Constraints

  • 1N2000001 \leq N \leq 200000
  • SS is a string of length NN consisting of A, B, C.

Input

Input is given from Standard Input in the following format:

NN

SS

Output

Print the answer.

4
CBAA
CAAB

We should do the following.

  • Initially, we have S=S=CBAA.
  • Delete the 33-rd character A and insert BB, making S=S=CBBBA.
  • Delete the 22-nd and 33-rd characters BB and insert A, making S=S=CABA.
  • Delete the 44-th character A and insert BB, making S=S=CABBB.
  • Delete the 33-rd and 44-th characters BB and insert A, making S=S=CAAB.

We cannot make SS lexicographically smaller than CAAB. Thus, the answer is CAAB.

1
A
A

We do no operation.

6
BBBCBB
ABCA