Problem:


Score : 1300 points

Problem Statement

Takahashi has a string S of length N consisting of lowercase English letters.
On this string, he will perform the following operation K times:

Let T be the string obtained by reversing S, and U be the string obtained by concatenating S and T in this order.

Let S' be some contiguous substring of U with length N, and replace S with S'.

Among the strings that can be the string S after the K operations, find the lexicographically smallest possible one.

Constraints

1 \leq N \leq 5000

1 \leq K \leq 10^9

|S|=N

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the lexicographically smallest possible string that can be the string S after the K operations.

Sample Input 1

5 1
bacba

Sample Output 1

aabca

When S=bacba, T=abcab, U=bacbaabcab, and the optimal choice of S' is S'=aabca.

Sample Input 2

10 2
bbaabbbaab

Sample Output 2

aaaabbaabb



Sample input:
5 1
bacba


Sample output:
aabca

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
u = s + s[::-1]
mi = "z" * n
for i in range(n + 1):
    if mi > u[i:i + n]:
        mi = u[i:i + n]
print(mi)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
m = min(s)
if k >= 20:
    print(m * n)
else:
    for p in range(k):
        u = s + s[::-1]
        l = []
        cnt = 0
        idx = 0
        for i in range(n * 2):
            if u[i] == m:
                if cnt == 0:
                    idx = i
                cnt += 1
            else:
                if cnt > 0:
                    l.append((cnt, idx))
                    cnt = 0
        if cnt > 0:
            l.append((cnt, idx))
        a, j = max(l)
        for c, i in l:
            if i <= n and c == a:
                s = min(s, u[i:i + n])
        if (k - p) % 2 == 0:
            s = s[::-1]
    print(s)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
S=list(input())
for i in range(n):
  S[i]=ord(S[i])-97
T=S[::-1]
U=S+T
f=0
for i in range(26):
  A=[]
  for j in range(2*n):
    if U[j]==i:
      f=1
      A.append(j)
  if f==1:
    c=i
    break
B=[]
for i in range(1,len(A)):
  a=1
  if A[i]==A[i-1]+1:
    a=a+1
    if A[i]>=n:
      B.append([a,A[i]])
  else:
    a=1
    if A[i]>=n:
      B.append([a,A[i]])
C=[]
b=max(B[i][0] for i in range(len(B)))
for i in range(len(B)):
  if B[i][0]==b:
    C.append(B[i][1])
if b*2**(k-1)>=n:
  for i in range(n):
    print(chr(97+c),end="")
  print()
  exit()
Ans=[]
d=n-b*2**(k-1)
for i in range(len(C)):
  AAns=U[C[i]-d-1:C[i]-1]
  Ans.append(AAns[::-1])
Ans.sort()
for i in range(b*2**(k-1)):
  print(chr(97+c),end="")
for i in range(n-b*2**(k-1)):
  print(chr(97+Ans[0][i]),end="")
print()

Submission status: Wrong Answer

Solution code:
from math import floor, log
inpl = lambda: list(map(int,input().split()))
N, K = inpl()
S = input()
U = S + S[::-1]
Sp = sorted([ U[i:i+N] for i in range(N+1)])[0]
n = 0
while Sp[n] == Sp[0]:
    n += 1

if K > int(floor(log(N/n)/log(2))):
    print(Sp[0]*N)
else:
    m = n * (2**(K-1))
    if m >= N:
        print(Sp[0]*N)
    else:
        print(Sp[0]*m + Sp[n:n+N-m])

Submission status: Wrong Answer

Solution code:
f=lambda s:min(s[i:i+n]for i in range(n+1))
n,k=map(int,input().split())
s=input()
if k<2:print(f(s+s[::-1]))
elif k>n:print(min(s)*n)
else:
  u=s+s[::-1]
  s,u=min((u[i],u[i+k-1:i+n-1])for i in range(n))
  u=s*~-k+u
  print(f(u[::-1]+u))

Submission status: Wrong Answer

Solution code:
f=lambda s:min(s[i:i+n]for i in range(n+1))
n,k=map(int,input().split())
s=input()
if k<2:print(f(s+s[::-1]))
elif k>n:print(min(s)*n)
else:
  u=s+s[::-1]
  s,u=min((u[i],u[i+n-1:i+k-1:-1])for i in range(n))
  u=s*k+u[::-1]
  print(f(u[::-1]+u))

Submission status: Wrong Answer

Solution code:
f=lambda s:min(s[i:i+n]for i in range(n+1))
n,k=map(int,input().split())
s=input()
if k<2:print(f(s+s[::-1]))
elif k>n:print(min(s)*n)
else: 
  u=f(s+s[::-1])
  i=u.rfind(u[0])
  *u,=u
  for i in range(i,min(n,i+k-1)):u[i]=u[0]
  print(f(''.join(u[::-1]+u)))

Submission status: Wrong Answer

Solution code:
f=lambda s:min(s[i:i+n]for i in range(n+1))
n,k=map(int,input().split())
s=input()
if k<2:print(f(s+s[::-1]))
elif k>n:print(min(s)*n)
else: 
  u=f(s+s[::-1])
  i=u.rfind(u[0])
  *u,=u
  for i in range(i,min(n,i+k)):u[i]=u[0]
  print(f(''.join(u[::-1]+u)))

Submission status: Wrong Answer

Solution code:
f=lambda s:min(s[i:i+n]for i in range(n+1))
n,k=map(int,input().split())
s=input()
if k<2:print(f(s+s[::-1]))
elif k>n:print(min(s)*n)
else: 
  *u,=f(s+s[::-1])
  for i in range(k-1):u[i]=u[0]
  print(f(''.join(u[::-1]+u)))

Submission status: Wrong Answer

Solution code:
f=lambda s:min(s[i:i+n]for i in range(n+1))
n,k=map(int,input().split())
s=input()
if k<2:print(f(s+s[::-1]))
elif k>n:print(min(s)*n)
else: 
  *u,=f(s+s[::-1])
  for i in range(k):u[i]=u[0]
  print(f(''.join(u[::-1]+u)))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
j=1
cnt=1
while j<n:
    j*=2
    cnt+=1
if k>=cnt:print(min(s)*n)
else:
    if k==1:
        minstr=s+s[::-1]
    for i in range(k-1):
        s=s+s[::-1]
        minstr=s[:n]
        for j in range(n+1):
            if s[j:j+n]<minstr:
                minstr=s[j:j+n]
    if k>=2:
        minstr=minstr[::-1]+minstr
    ans=minstr[:n]
    for i in range(n+1):
        if ans > minstr[i:i+n]:
            ans=minstr[i:i+n]
    print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
t=s[::-1]
u="".join([s,t])
ls=[]
for i in range(n):
  ls.append(u[i:i+n])
ls.sort()
k-=1
x=ls[0]
cnt=1
for i in range(1,n):
  if x[0]==x[i]:
    cnt+=1
  else:
    break
if cnt*2**k<n:
  y=n-cnt*2**k
  print(x[0]*(cnt*2**k)+x[cnt:y+1])
else:
  print(x[0]*n)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
t=s[::-1]
u="".join([s,t])
ls=[]
for i in range(n):
  ls.append(u[i:i+n])
ls.sort()
k-=1
x=ls[0]
cnt=1
for i in range(1,n):
  if x[0]==x[i]:
    cnt+=1
  else:
    break
if cnt*2**k<n:
  y=n-cnt*2**k
  print(x[0]*(cnt*2**k)+x[1:y+1])
else:
  print(x[0]*n)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
t=s[::-1]
u="".join([s,t])
ls=[]
for i in range(n):
  ls.append(u[i:i+n])
ls.sort()
k-=1
x=ls[0]
if 2**k<n:
  y=n-2**k
  print(x[0]*(2**k)+x[1:y+1])
else:
  print(x[0]*n)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
S = input()

lex_min = "z"
for char in S:
  lex_min = min(char, lex_min)

if (1 << (K - 1)) > N:
  print(lex_min * N)
else:
  U = S + S[::-1]
  candidates = []
  for i in range(N, N << 1):
    if U[i] == lex_min:
      M = U[i-N+1:i+1]
      if K > 1:
        candidates.append((1 << (K - 1)) * lex_min + M[:N - (1 << (K - 1))])
      else:
        candidates.append(M)
  print(min(candidates))

Submission status: Wrong Answer

Solution code:
import sys

N,K=map(int,input().split())
S=list(input())
MIN=min(S)

def minimum(S):
    T=S+S[::-1]

    ANS=[]

    for i in range(N+1):
        ANS.append(T[i:i+N])

    return min(ANS)

def next(S):
    T=S+S[::-1]
    LIST=[]
    CL=[0]*2*N
    count=0

    for i in range(2*N):
        if T[i]==MIN:
            count+=1

        else:
            count=0

        CL[i]=count

    MAX=max(CL)
    LIST=[]

    for i in range(N-1,2*N):
        if CL[i]==MAX:
            LIST.append(T[i+1-N:i+1])

    return LIST,MAX

def next2(S,x):
    return S[x:]+[MIN]*x,2*x

if K==1:
    print("".join(minimum(S)))

else:
    CAN,MAX=next(S)

    if (MAX<<K)>=N:
        print(MIN*N)
        sys.exit()

    ANS=[]

    for c in CAN:
        x=MAX
        for j in range(K-2):
            c,x=next2(c,x)

            if max(c)==MIN:
                print("".join(c))
                sys.exit()

        ANS.append(minimum(c))

    print("".join(min(ANS)))

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
S  = str(input())

def get_kouho(SS):
  Scon = SS+SS[::-1]
  smallScon = [Scon[i:i+N] for i in range(N)]
  smallScon.sort()
  smallScon_rev = [s[::-1] for s in smallScon]
  smallScon_rev.sort()
  #print(smallScon)
  #print(smallScon_rev)
  kouho1 = smallScon[0]
  kouho2 = smallScon_rev[0]
  return kouho1, kouho2[::-1] #kouho2はrevしてるので元に戻す

kouho1, kouho2=get_kouho(S)
#print('kouhos',kouho1, kouho2)

for i in range(K-1):
  kouho11, kouho12=get_kouho(kouho1)
  kouho21, kouho22=get_kouho(kouho2)
  kouhos =kouho11, kouho12, kouho21, kouho22
  #print('kouhos',kouhos)
  kouho1 = sorted(kouhos)[0] # a****
  kouho2 = sorted([s[::-1] for s in kouhos])[0][::-1] # ****a

print(kouho1)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
K=min(K,20)
for i in range(K):
    X=[]
    U=S+S[::-1]
    if (K-i)%2==1:
        for j in range(N+1):
            X.append(U[j:N+j])
        S=min(X)
    else:
        for j in range(N+1):
            X.append(U[j:N+j][::-1])
        S=min(X)[::-1]

    #print(X,S)
print(S)

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]

n, k = map(int, input().split())
s = str(input())

u = s + s[::-1]
ss = s

times = min(n, k)
for i in range(times):
    for j in range(n):
        part = u[j:j+n]
        if part < ss:
            ss = part
        elif s == ss:
            print(s)
            exit()
    s = ss
    u = s[::-1] + s

print(s)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = str(input())


def get_kouho(SS):
    Scon = SS + SS[::-1]
    topchar = sorted(list(Scon))[0]
    smallScon = [Scon[i:i + N] for i in range(N) if Scon[i]==topchar]
    smallScon.sort()
    smallScon_rev = [Scon[i:i + N][::-1] for i in range(N) if Scon[i+-1]==topchar]
    smallScon_rev.sort()
    # print(smallScon)
    # print(smallScon_rev)
    kouho1 = smallScon[0]
    kouho2 = smallScon_rev[0]
    return kouho1, kouho2[::-1]  # kouho2はrevしてるので元に戻す


kouho1, kouho2 = get_kouho(S)
# print('kouhos',kouho1, kouho2)

for i in range(K - 1):
    kouho11, kouho12 = get_kouho(kouho1)
    kouho21, kouho22 = get_kouho(kouho2)
    kouhos = kouho11, kouho12, kouho21, kouho22
    print('kouhos', kouhos)
    kouho1 = sorted(kouhos)[0]
    kouho2 = sorted([s[::-1] for s in kouhos])[0][::-1]

print(sorted([kouho1, kouho2])[0])

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]

n, k = map(int, input().split())
s = str(input())

u = s + s[::-1]
ss = s

times = min(n, k)
for i in range(times):
    for j in range(n):
        part = u[j:j+n]
        if part < ss:
            ss = part
    if s == ss:
        print(s)
        exit()
    s = ss
    u = s[::-1] + s

print(s)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = str(input())


def get_kouho(SS):
    Scon = SS + SS[::-1]
    topchar = sorted(list(Scon))[0]
    smallScon = [Scon[i:i + N] for i in range(N) if Scon[i]==topchar]
    smallScon.sort()
    smallScon_rev = [Scon[i:i + N][::-1] for i in range(N) if Scon[i+N]==topchar]
    smallScon_rev.sort()
    # print(smallScon)
    # print(smallScon_rev)
    kouho1 = smallScon[0]
    kouho2 = smallScon_rev[0]
    return kouho1, kouho2[::-1]  # kouho2はrevしてるので元に戻す


kouho1, kouho2 = get_kouho(S)
# print('kouhos',kouho1, kouho2)

for i in range(K - 1):
    kouho11, kouho12 = get_kouho(kouho1)
    kouho21, kouho22 = get_kouho(kouho2)
    kouhos = kouho11, kouho12, kouho21, kouho22
    print('kouhos', kouhos)
    kouho1 = sorted(kouhos)[0]
    kouho2 = sorted([s[::-1] for s in kouhos])[0][::-1]

print(sorted([kouho1, kouho2])[0])

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
S  = str(input())

def get_kouho(SS):
  Scon = SS+SS[::-1]
  smallScon = [Scon[i:i+N] for i in range(N)]
  smallScon.sort()
  smallScon_rev = [s[::-1] for s in smallScon]
  smallScon_rev.sort()
  #print(smallScon)
  #print(smallScon_rev)
  kouho1 = smallScon[0]
  kouho2 = smallScon_rev[0]
  return kouho1, kouho2[::-1] #kouho2はrevしてるので元に戻す

kouho1, kouho2=get_kouho(S)
#print('kouhos',kouho1, kouho2)

for i in range(K-1):
  kouho11, kouho12=get_kouho(kouho1)
  kouho21, kouho22=get_kouho(kouho2)
  kouhos =kouho11, kouho12, kouho21, kouho22
  print('kouhos',kouhos)
  kouho1 = sorted(kouhos)[0]
  kouho2 = sorted([s[::-1] for s in kouhos])[0][::-1]

print(sorted([kouho1, kouho2])[0])

Submission status: Wrong Answer

Solution code:
#coding:utf-8
tp= input().split()
N = int(tp[0])
K = int(tp[1])
S = input()
Sd=""
for i in range(K):
  T=S[::-1]
  U=S+T
  rtp=[]
  for h in range(K):
    rtp.append(U[h:h+N])
  rtp.sort()
  Sd=rtp[0]
  S=Sd
print(Sd)

Submission status: Wrong Answer

Solution code:
def E(S, N):
    T = S[::-1]
    U = S+T
    return [U[i:N+i] for i in range(N)]

N, K = input().split()
N, K = int(N), int(K)
S = [input()]

for i in range(K):
    memory = []
    for s in S:
        memory += E(s, N)
        memory = sorted(memory)[:10]
    S = memory
    #S = sorted([s for i in range(len(memory)) for s in memory[i]])[:10]
print(sorted(S)[0])

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
S  = str(input())

def get_kouho(SS):
  Scon = SS+SS[::-1]
  smallScon = [Scon[i:i+N] for i in range(N)]
  smallScon_rev = [s[::-1] for s in smallScon]
  #print(smallScon)
  #print(smallScon_rev)
  kouho1 = sorted(smallScon)[0]
  kouho2 = sorted(smallScon_rev)[0]
  return kouho1, kouho2[::-1] #kouho2はrevしてるので元に戻す

kouho1, kouho2=get_kouho(S)
#print('kouhos',kouho1, kouho2)

for i in range(K-1):
  kouho11, kouho12=get_kouho(kouho1)
  kouho21, kouho22=get_kouho(kouho2[::-1])
  kouhos =kouho11, kouho12, kouho21, kouho22
  print('kouhos',kouhos)
  kouho1 = sorted(kouhos)[0]
  kouho2 = sorted([s[::-1] for s in kouhos])[0]

print(sorted([kouho1, kouho2])[0])

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
S  = str(input())

def get_kouho(SS):
  Scon = SS+SS[::-1]
  smallScon = [Scon[i:i+N] for i in range(N)]
  smallScon_rev = [s[::-1] for s in smallScon]
  #print(smallScon)
  #print(smallScon_rev)
  kouho1 = sorted(smallScon)[0]
  kouho2 = sorted(smallScon_rev)[0]
  return kouho1, kouho2[::-1] #kouho2はrevしてるので元に戻す

kouho1, kouho2=get_kouho(S)
#print('kouhos',kouho1, kouho2)

for i in range(K-1):
  kouho11, kouho12=get_kouho(kouho1)
  kouho21, kouho22=get_kouho(kouho2)
  kouhos =kouho11, kouho12, kouho21, kouho22
  print('kouhos',kouhos)
  kouho1 = sorted(kouhos)[0]
  kouho2 = sorted([s[::-1] for s in kouhos])[0][::-1] 

print(sorted([kouho1, kouho2])[0])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()

if k > 5000 * 26:
    k = 5000 * 26

for i in range(k):
    sList = []
    t = (s[::-1])
    u = s + t
    for j in range(n+1):
        sList.append(u[j:(j + n)])
    sList.sort()
    s = sList[0]

print(s)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
S  = str(input())

def get_kouho(SS):
  Scon = SS+SS[::-1]
  smallScon = [Scon[i:i+N] for i in range(N)]
  smallScon_rev = [s[::-1] for s in smallScon]
  #print(smallScon)
  #print(smallScon_rev)
  kouho1 = sorted(smallScon)[0]
  kouho2 = sorted(smallScon_rev)[0]
  return kouho1, kouho2[::-1] #kouho2はrevしてるので元に戻す

kouho1, kouho2=get_kouho(S)
#print('kouhos',kouho1, kouho2)

for i in range(K-1):
  kouho11, kouho12=get_kouho(kouho1)
  kouho21, kouho22=get_kouho(kouho2)
  kouhos =kouho11, kouho12, kouho21, kouho22
  #print('kouhos',kouhos)
  kouho1 = sorted(kouhos)[0]
  kouho2 = sorted([s[::-1] for s in kouhos])[0]

print(sorted([kouho1, kouho2])[0])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
d=s+s[::-1]
d=d[::-1]
r=d[:n]
e=d[:n]
for i in range(n):
    e=e[1:]+d[n+i]
    r=min(r,e)
t=1
qo=r[0]
for i in range(1,n):
    if qo==r[i]:
        t+=1
    else:
        break
r=r[::-1]
print(r)
for i in range(k-1):
    p=r+r[n-t:]
    p=p[::-1]
    p=p[:n]
    if r==p:
        break
    else:
        r=p[::-1]
        t+=t
print(r[::-1])

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
S  = str(input())

def get_kouho(S):
  Scon = S+S[::-1]
  smallScon = [Scon[i:i+N] for i in range(N)]
  kouho1 = sorted(smallScon)[0]
  kouho2 = sorted([s[::-1] for s in smallScon])[0]
  return kouho1, kouho2

kouho1, kouho2=get_kouho(S)

for i in range(K-1):
  kouho11, kouho12=get_kouho(kouho1)
  kouho21, kouho22=get_kouho(kouho2)
  kouhos =kouho11, kouho12, kouho21, kouho22
  kouho1 = sorted(kouhos)[0]
  kouho2 = sorted([s[::-1] for s in kouhos])[0]

print(sorted([kouho1, kouho2])[0])

Submission status: Wrong Answer

Solution code:
def E(S, N):
    T = S[::-1]
    U = S+T
    return sorted([U[i:N+i] for i in range(N)])[:10]

N, K = input().split()
N, K = int(N), int(K)
S = [input()]

for i in range(K):
    memory = []
    for s in S:
        memory.append(E(s, N))
    S = sorted([s for i in range(len(memory)) for s in memory[i]])[:10]
print(sorted(S)[0])

Submission status: Wrong Answer

Solution code:
def E(S, N):
    T = S[::-1]
    U = S+T
    return sorted([U[i:N+i] for i in range(N)])[:5]

N, K = input().split()
N, K = int(N), int(K)
S = [input()]

for i in range(K):
    memory = []
    for s in S:
        memory.append(E(s, N))
    S = sorted([s for i in range(len(memory)) for s in memory[i]])[:5]
print(sorted(S)[0])

Submission status: Wrong Answer

Solution code:
def E(S, N):
    T = S[::-1]
    U = S+T
    return sorted([U[i:N+i] for i in range(N)])[:3]

N, K = input().split()
N, K = int(N), int(K)
S = [input()]

for i in range(K):
    memory = []
    for s in S:
        memory.append(E(s, N))
    S = sorted([s for i in range(len(memory)) for s in memory[i]])[:3]
print(sorted(S)[0])

Submission status: Wrong Answer

Solution code:
n, k = (int(i) for i in input().split())
s1 = input()

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
S = input()
candidates = []
for i in range(K):
    S += S[::-1]
    for j in range(len(S)):
        if len(S)-(j+N) >= 0:
            candidates.append(S[j:j+N])
        else:
            remain = len(S[j:])
            S[:N-remain]
            candidates.append(S[j:] + S[:N-remain])
    candidates = sorted(candidates)
    S = candidates[0]
print(S)

Submission status: Wrong Answer

Solution code:
U = ""
arr = []
lines = input().split(" ")
N = int(lines[0])
K = int(lines[1])
S = input()
T = S[::-1]

for i in range(K):
    U += S
    U += T

num = len(U)

for j in range(num):
    if j + N <= num:
        arr.append(U[j : (j+ N)])
        
arr.sort()

print(arr[0])

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
S=input()
C=min(S)
for i in range(1,K+1):
  if N<=(1<<i):
    print(C*N)
    exit()
T=0
L=0
U=S
c=0
P='z'*N
for l in range(-1,1):
  for i in range(K-1):
    T=S
    T=T+S[::-1]
    L=[T[j:j+N] for j in range(N+1)]
    L.sort(key=lambda x:x[::-1])
    c=0
    for j in range(N):
      if L[0][-j-1]!=C:
        break
      c+=1
    while max(L[-1][-c:])!=C:
      del L[-1]
    S=L[l]
  T=S
  T=T+S[::-1]
  L=[T[j:j+N] for j in range(N+1)]
  L.sort()
  P=min(P,L[0])
  S=U
print(P)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
s = input()
rev_s = s + s[::-1]
mido = "z" * n
for i in range(n + 1):
    if mido > rev_s[i:i + n]:
        mido = rev_s[i:i + n]
ind = float("inf")
for i in range(n):
    if mido[i] != mido[0]:
        if i < ind:
            ind = i
ans = ind * (2 ** (k - 1))
print((mido[0] * ans + mido[ind:ind + n - ans])[:n])

Submission status: Time Limit Exceeded

Solution code:
import sys

N,K=map(int,input().split())
S=list(input())
MIN=min(S)

def minimum(S):
    T=S+S[::-1]

    ANS=[]

    for i in range(N+1):
        ANS.append(T[i:i+N])

    return min(ANS)

def next(S):
    T=S+S[::-1]
    LIST=[]
    CL=[0]*2*N
    count=0

    for i in range(2*N):
        if T[i]==MIN:
            count+=1

        else:
            count=0

        CL[i]=count

    MAX=max(CL)
    LIST=[]

    for i in range(N-1,2*N):
        if CL[i]==MAX:
            LIST.append(T[i+1-N:i+1])

    return LIST,MAX

def next2(S,x):
    return S[x:]+[MIN]*x,2*x

if K==1:
    print("".join(minimum(S)))

else:
    CAN,MAX=next(S)

    if (MAX<<(K-1))>=N:
        print(MIN*N)
        sys.exit()

    ANS=[]

    for c in CAN:
        x=MAX
        for j in range(K-2):
            c,x=next2(c,x)

            if max(c)==MIN:
                print("".join(c))
                sys.exit()

        ANS.append(minimum(c))

    print("".join(min(ANS)))

Submission status: Time Limit Exceeded

Solution code:
import sys

N,K=map(int,input().split())
S=list(input())
MIN=min(S)

def minimum(S):
    T=S+S[::-1]

    ANS=[]

    for i in range(N+1):
        ANS.append(T[i:i+N])

    return min(ANS)

def next(S):
    T=S+S[::-1]
    LIST=[]
    CL=[0]*2*N
    count=0

    for i in range(2*N):
        if T[i]==MIN:
            count+=1

        else:
            count=0

        CL[i]=count

    MAX=max(CL)
    LIST=[]

    for i in range(N-1,2*N):
        if CL[i]==MAX:
            LIST.append(T[i+1-N:i+1])

    return LIST,MAX

def next2(S,x):
    return S[x:]+[MIN]*x,2*x

if K==1:
    print("".join(minimum(S)))

else:
    CAN,MAX=next(S)

    if (MAX<<(K-1))>=N:
        print(MIN*N)
        sys.exit()

    ANS=[]

    for c in CAN:
        x=MAX
        for j in range(K-2):
            c,x=next2(c,x)

            if max(c)==MIN:
                print("".join(c))
                sys.exit()

        ANS.append(minimum(c))

    print("".join(min(ANS)))

Submission status: Time Limit Exceeded

Solution code:
import sys

N,K=map(int,input().split())
S=list(input())
MIN=min(S)

def minimum(S):
    T=S+S[::-1]

    ANS=[]

    for i in range(N+1):
        ANS.append(T[i:i+N])

    return min(ANS)

def next(S):
    T=S+S[::-1]
    LIST=[]
    CL=[0]*2*N
    count=0

    for i in range(2*N):
        if T[i]==MIN:
            count+=1

        else:
            count=0

        CL[i]=count

    MAX=max(CL)
    LIST=[]

    for i in range(N-1,2*N):
        if CL[i]==MAX:
            LIST.append(T[i+1-N:i+1])

    return LIST

if K==1:
    print("".join(minimum(S)))

elif K>=13:
    print(MIN*N)

else:
    CAN=next(S)

    ANS=[]

    for c in CAN:
        for j in range(K-2):
            c=next(c)[0]

            if max(c)==MIN:
                print("".join(c))
                sys.exit()

        ANS.append(minimum(c))

    print("".join(min(ANS)))

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
S=list(input())
MIN=min(S)

def minimum(S):
    T=S+S[::-1]

    ANS=[]

    for i in range(N+1):
        ANS.append(T[i:i+N])

    return min(ANS)

def next(S):
    T=S+S[::-1]
    LIST=[]
    CL=[0]*2*N
    count=0

    for i in range(2*N):
        if T[i]==MIN:
            count+=1

        else:
            count=0

        CL[i]=count

    MAX=max(CL)
    LIST=[]

    for i in range(N-1,2*N):
        if CL[i]==MAX:
            LIST.append(T[i+1-N:i+1])

    return LIST

if K==1:
    print("".join(minimum(S)))

else:
    CAN=next(S)

    ANS=[]

    for c in CAN:
        for j in range(K-2):
            c=next(c)[0]

            if c==MIN*N:
                print("".join(c))
                sys.exit()

        ANS.append(minimum(c))

    print("".join(min(ANS)))

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
S=list(input())
MIN=min(S)

def minimum(S):
    T=S+S[::-1]

    ANS=[]

    for i in range(N+1):
        ANS.append(T[i:i+N])

    return min(ANS)

def next(S):
    T=S+S[::-1]
    LIST=[]
    CL=[0]*2*N
    count=0

    for i in range(2*N):
        if T[i]==MIN:
            count+=1

        else:
            count=0

        CL[i]=count

    MAX=max(CL)
    LIST=[]

    for i in range(N-1,2*N):
        if CL[i]==MAX:
            LIST.append(T[i+1-N:i+1])

    return LIST

if K==1:
    print("".join(minimum(S)))

else:
    CAN=next(S)

    ANS=[]

    for c in CAN:
        for j in range(K-2):
            c=next(c)[0]

            if c==MIN*N:
                print("".join(c))
                sys.exit()

        ANS.append(minimum(c))

    print("".join(min(ANS)))

Submission status: Time Limit Exceeded

Solution code:
def E(S, i):
    T = S[::-1]
    U = S+T
    S = U[:N]
    if i < K-1:
        for i in range(len(U)-N):
            if U[i+1:N+i+1][::-1] < S[::-1]:
                S = U[i+1:N+i+1]
    else:
        for i in range(len(U)-N):
            if U[i+1:N+i+1] < S:
                S = U[i+1:N+i+1]
    return S

N, K = input().split()
N, K = int(N), int(K)
S = input()

for i in range(K):
    S = E(S, i)
print(S)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
s=list(input())
l=0
c=0
for i in range(n):
    if s[i]==min(s):
        c+=1
        l=max(l,c)
    else:
        c=0
import sys
input=sys.stdin.readline
s.reverse()
if k>13:
    print(min(s)*n)
else:
    for i in range(min(k,13)):
        t=list(reversed(s))
        u=t+s
        uu=u[:n]
        for j in range(1,n+1):
            x=u[j:j+n]
            if uu>x:
                uu=x
        s=uu
    print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
s=list(input())
import sys
input=sys.stdin.readline
s.reverse()
if k>=20:
    print(min(s)*n)
else:
    for i in range(min(k,20)):
        t=list(reversed(s))
        u=t+s
        uu=u[:n]
        for j in range(1,n+1):
            x=u[j:j+n]
            if uu>x:
                uu=x
        s=uu
    print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
N,K = (int(i) for i in input().split())
S = input()

a = [S]
res = []

for i in range(K):
  res = []
  
  for k in a:
    Ss = k[::-1]
    SSs = k + Ss
    a = []
  
    for j in range(len(S)):
      a.append(SSs[j:len(S)+j])
      res.append(SSs[j:len(S)+j])
  
print(min(res))

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
s=list(input())
import sys
input=sys.stdin.readline
s.reverse()
if k>=n:
    print(min(s)*n)
else:
    for i in range(min(k,n)):
        t=list(reversed(s))
        u=t+s
        uu=u[:n]
        for j in range(1,n+1):
            x=u[j:j+n]
            if uu>x:
                uu=x
        s=uu
    print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
N,K = (int(i) for i in input().split())
S = input()

a = [S]
res = []

for i in range(K):
  
  for k in a:
    Ss = k[::-1]
    SSs = k + Ss
    a = []
  
    for j in range(len(S)):
      a.append(SSs[j:len(S)+j])
      res.append(SSs[j:len(S)+j])
  
print(min(res))

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
s=list(input())
import sys
input=sys.stdin.readline
s.reverse()
if k>=n:
    print(min(s)*n)
else:
    for i in range(min(k,5000)):
        t=list(reversed(s))
        u=t+s
        uu=u[:n]
        for j in range(1,n+1):
            x=u[j:j+n]
            if uu>x:
                uu=x
        s=uu
    print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]

n, k = map(int, input().split())
s = str(input())

u = s + s[::-1]
ss = s

times = min(n, k)
for i in range(times):
    for j in range(n):
        part = u[j:j+n]
        if part < ss:
            ss = part
    s = ss
    u = s[::-1] + s

print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]

n, k = map(int, input().split())
s = str(input())

u = s + s[::-1]
ss = s

times = min(n, k)
for i in range(times):
    for j in range(n):
        part = u[j:j+n]
        if u[j:j+n] < ss:
            ss = part
    s = ss
    u = s[::-1] + s

print(s)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
s=list(input())
import sys
input=sys.stdin.readline
s.reverse()
if k>=n:
    print(min(s)*n)
else:
    for i in range(min(k,5000)):
        t=list(reversed(s))
        u=t+s
        uu=u[:n]
        for j in range(1,n+1):
            if uu>u[j:j+n]:
                uu=u[j:j+n]
        s=uu
    print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
def E(S, N):
    T = S[::-1]
    U = S+T
    return [U[i:N+i] for i in range(N)]

N, K = input().split()
N, K = int(N), int(K)
S = [input()]

for i in range(K):
    memory = []
    for s in S:
        memory += E(s, N)
        memory = sorted(memory)[:N//2]
    S = memory
    #S = sorted([s for i in range(len(memory)) for s in memory[i]])[:10]
print(sorted(S)[0])

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
s = str(input())

u = s + s[::-1]
ss = s

times = min(n, k)
for i in range(times):
    for j in range(n):
        part = u[j:j+n]
        if u[j:j+n] < ss:
            ss = part
    s = ss
    u = s[::-1] + s

print(s)

Submission status: Time Limit Exceeded

Solution code:
def E(S, N):
    T = S[::-1]
    U = S+T
    return [U[i:N+i] for i in range(N)]

N, K = input().split()
N, K = int(N), int(K)
S = [input()]

for i in range(K):
    memory = []
    for s in S:
        memory += sorted(E(s, N))[:N]
        memory = sorted(memory)[:N]
    S = memory
    #S = sorted([s for i in range(len(memory)) for s in memory[i]])[:10]
print(sorted(S)[0])

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
s=list(input())
s.reverse()
if k>=n:
    print(min(s)*n)
else:
    for i in range(min(k,5000)):
        t=list(reversed(s))
        u=t+s
        uu=[]
        for j in range(n+1):
            uu.append(u[j:j+n])
        uu.sort()
        s=uu[0]
    print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
s=list(input())
if k>=n:
    print(min(s)*n)
else:
    for i in range(min(k,5000)):
        t=list(reversed(s))
        u=s+t
        u.reverse()
        uu=[]
        for j in range(n+1):
            uu.append(u[j:j+n])
            uu.sort()
            s=list(reversed(uu[0]))
    print("".join(list(reversed(s))))

Submission status: Time Limit Exceeded

Solution code:
def E(S, N):
    T = S[::-1]
    U = S+T
    return [U[i:N+i] for i in range(N)]

N, K = input().split()
N, K = int(N), int(K)
S = [input()]

for i in range(K):
    memory = []
    for s in S:
        memory += E(s, N)
        memory = sorted(memory)[:N]
    S = memory
    #S = sorted([s for i in range(len(memory)) for s in memory[i]])[:10]
print(sorted(S)[0])