atcoder#ARC148B. [ARC148B] dp

[ARC148B] dp

Score : 500500 points

Problem Statement

For a string TT of length LL consisting of d and p, let f(T)f(T) be TT rotated 180180 degrees. More formally, let f(T)f(T) be the string that satisfies the following conditions.

  • f(T)f(T) is a string of length LL consisting of d and p.
  • For every integer ii such that 1iL1 \leq i \leq L, the ii-th character of f(T)f(T) differs from the (L+1i)(L + 1 - i)-th character of TT.

For instance, if T=T = ddddd, f(T)=f(T) = ppppp; if T=T = dpdppp, f(T)=f(T)= dddpdp.

You are given a string SS of length NN consisting of d and p. You may perform the following operation zero or one time.

  • Choose a pair of integers (L,R)(L, R) such that 1LRN1 \leq L \leq R \leq N, and let TT be the substring formed by the LL-th through RR-th characters of SS. Then, replace the LL-th through RR-th characters of SS with f(T)f(T).

For instance, if S=S= dpdpp and (L,R)=(2,4)(L,R)=(2,4), we have T=T= pdp and f(T)=f(T)= dpd, so SS becomes ddpdp.

Print the lexicographically smallest string that SS can become.

What is lexicographical order?

A string S=S1S2SSS = S_1S_2\ldots S_{|S|} is said to be lexicographically smaller than a string T=T1T2TTT = T_1T_2\ldots T_{|T|} if one of the following 1. and 2. holds. Here, S|S| and T|T| denote the lengths of SS and TT, respectively.

  1. S<T|S| \lt |T| and S1S2SS=T1T2TSS_1S_2\ldots S_{|S|} = T_1T_2\ldots T_{|S|}.
  2. There is an integer 1imin{S,T}1 \leq i \leq \min\lbrace |S|, |T| \rbrace that satisfies the following two conditions:
    • S1S2Si1=T1T2Ti1S_1S_2\ldots S_{i-1} = T_1T_2\ldots T_{i-1}.
    • SiS_i is smaller than TiT_i in alphabetical order.

Constraints

  • 1N50001 \leq N \leq 5000
  • SS is a string of length NN consisting of d and p.
  • NN is an integer.

Input

The input is given from Standard Input in the following format:

NN

SS

Output

Print the answer.

6
dpdppd
dddpdd

Let (L,R)=(2,5)(L, R) = (2, 5). Then, we have T=T = pdpp and f(T)=f(T) = ddpd, so SS becomes dddpdd. This is the lexicographically smallest string that can be obtained, so print it.

3
ddd
ddd

It may be optimal to skip the operation.

11
ddpdpdppddp
ddddpdpdddp