Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

N = int(input())
List = list(input())
ListR = []
ListG = []
ListB = []

for i in range(N):
  if List[i]=="R":
    ListR.append(i)
  elif List[i]=="G":
    ListG.append(i)
  else:
    ListB.append(i)
    
ans = len(ListR)*len(ListG)*len(ListB)
for a in ListR:
  for i in range(1,2000):
    if a+i in ListG:
      if a+2*i in ListB:
        ans -=1
      if a-i in ListB:
        ans -=1
    if a-i in ListG:
      if a-2*i in ListB:
        ans -=1
      if a+i in ListB:
        ans -=1
    if a+2*i in ListG:
      if a+i in ListB:
        ans -=1
    if a-2*i in ListG:
      if a-i in ListB:
        ans -=1
print(ans)



### FIXED CODE ###

N = int(input())
List = list(input())
ans = (List.count("R"))*(List.count("G"))*(List.count("B"))

for i in range(1,2000):
  for j in range(N-i*2):
    if {List[j], List[j+i], List[j+2*i]} == {"R","G","B"}:
      ans -=1
print(ans)
### BUGGY CODE ###

from collections import Counter
n,*a = map(int,open(0).read().split())
c = Counter(a)
d = sorted(c.keys(),reverse=True)
m = len(d)
ans = 0
for i in range(m):
  if c[d[i]] != 1:
    continue
  for j in range(i+1,m):
    if d[i]%d[j]==0:
      break
  else:
    ans+=1
print(ans)

### FIXED CODE ###

from collections import Counter
n,*a = map(int,open(0).read().split())
s = set(a)
c = Counter(a)
m = max(s)
l = [True]*(m+1)
ans = 0
for i in range(1,m+1):
  if i in s and l[i]:
    if c[i] == 1:
      ans += 1
    for j in range(1,m//i+1):
      l[i*j] = False
print(ans)
### BUGGY CODE ###

s = input()
q = int(input())

count = 0
for i in range(q):
    q_i = input()
    if len(q_i) == 1:
        count += 1
    else:
        lis = [i for i in q_i.split()]
        x = count
        x += int(lis[1])
        if x % 2 == 0:
            s += lis[2]
        else:
            s = lis[2] + s

if count % 2 == 1:
    ans = ""
    for i in range(len(s)):
        ans += s[-i-1]
else:
    ans = s
print(ans)

### FIXED CODE ###

s = input()
q = int(input())
 
count = 0
s_f = ""
for i in range(q):
    q_i = input()
    if len(q_i) == 1:
        count += 1
    else:
        lis = [i for i in q_i.split()]
        x = count
        x += int(lis[1])
        if x % 2 == 0:
            s += lis[2]
        else:
            s_f += lis[2]

ans = ""
if count % 2 == 1:
    for i in range(len(s)):
        ans += s[-i-1]
    ans += s_f
else:
    s_f_ = ""
    for i in range(len(s_f)):
        s_f_ += s_f[-i-1]
    ans = s_f_ + s
print(ans)
### BUGGY CODE ###

import sys
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt, ceil, floor
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache, reduce
from operator import xor
from heapq import heappush, heappop
INF = float("inf")
sys.setrecursionlimit(10**7)

# 4近傍（右, 下, 左, 上）
dy4, dx4 = [0, -1, 0, 1], [1, 0, -1, 0]


def inside(y: int, x: int, H: int, W: int) -> bool: return 0 <= y < H and 0 <= x < W


def main():
    N = int(input())
    C, S, F = [], [], []
    for _ in range(N - 1):
        c, s, f = map(int, input().split())
        C.append(c)
        S.append(s)
        F.append(f)

    for i in range(N):
        now = 0
        for j in range(i, N - 1):
            t = 0
            while S[j] + F[j] * t < now:
                t += 1
            now += (S[j] + F[j] * t) - now
            now += C[j]
        print(now)


if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt, ceil, floor
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache, reduce
from operator import xor
from heapq import heappush, heappop
INF = float("inf")
sys.setrecursionlimit(10**7)

# 4近傍（右, 下, 左, 上）
dy4, dx4 = [0, -1, 0, 1], [1, 0, -1, 0]


def inside(y: int, x: int, H: int, W: int) -> bool: return 0 <= y < H and 0 <= x < W


def main():
    N = int(input())
    C, S, F = [], [], []
    for _ in range(N - 1):
        c, s, f = map(int, input().split())
        C.append(c)
        S.append(s)
        F.append(f)

    for i in range(N):
        now = 0
        for j in range(i, N - 1):
            if S[j] > now:
                now += S[j] - now

            t = ceil(now / F[j])
            now += (F[j] * t) - now
            now += C[j]
        print(now)


if __name__ == '__main__':
    main()
### BUGGY CODE ###

A,B,C,D = map(int,input().split())

Sum = 0

for i in range(A,B+1):
  if (i%C)!=0 and (i%D)!=0:
  	Sum += 1    

print(Sum)

### FIXED CODE ###

import fractions

A,B,C,D = map(int,input().split())

Min = (C * D) // fractions.gcd(C, D)

Sum_A = (A-1) - ((A-1)//C + (A-1)//D - (A-1)//(Min))
Sum_B = B - (B//C + B//D - B//(Min))
Sum = Sum_B - Sum_A

print(Sum)
### BUGGY CODE ###

N, M = list(map(int, input().split()))
S = input()
T = input()

def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x%y)

def lcm(a, b):
	return a * b // gcd (a, b)

ans = lcm(N, M)

i = 0
res = [0 for i in range(ans)]

for i in range(N):
    tmp_s = int(ans/N) * i
    res[tmp_s] = S[i]

for j in range(M):
    tmp_t = int(ans/M) * j
    if res[tmp_t] != 0:
        if T[j] != res[tmp_t]:
            ans = -1

print(ans)


### FIXED CODE ###

import fractions


def lcm(x, y):
    return int((x * y) // fractions.gcd(x, y))

n, m = [int(i) for i in input().split()]
s = input()
t = input()

span = fractions.gcd(n, m)
for i in range(span):
    if s[int(n/span)*i] != t[int(m/span)*i]:
        print("-1")
        exit()
print(lcm(n, m))
### BUGGY CODE ###

n,m=map(int,input().split())
h=list(map(int, input().split()))
a=[j for j in range(1,n+1)]
road=[]*m
for i in range(m):
    c=list(map(int,input().split()))
    road.append(c)
for i in range(m):
    d,e=road[i]
    if h[d-1]==h[e-1]:
        if d in a and e in a:
            a.remove(d)
            a.remove(e)
        elif d in a and e not in a:
            a.remove(d)
        elif d not in a and e in a:
            a.remove(e)
    elif h[d-1]>h[e-1]:
        if e in a:
            a.remove(e)
        else:
            pass
    elif h[d-1]<h[e-1]:
        if d in a:
            a.remove(d)
        else:
            pass
    
print(len(a))

### FIXED CODE ###

n,m=map(int,input().split())
h=[0]+list(map(int, input().split()))
roads=[[] for _ in range(n+1)]
cnt=0
for i in range(m):
    a,b=map(int,input().split())
    roads[a].append(b)
    roads[b].append(a)

for i in range(1,n+1):
    for j in roads[i]:
        if h[i]<=h[j]:
            break
    else:
         cnt+=1
    
print(cnt)
### BUGGY CODE ###

import sys
readline = sys.stdin.readline

N = int(readline())
P = list(map(float,readline().split()))

# dp[i][j] = i枚投げて、表がj枚出ている確率
dp = [[0] * (N + 1) for i in range(N + 1)]
dp[0][0] = 1
for i in range(N):
  for j in range(N):
    dp[i + 1][j] += dp[i][j] * (1 - P[i])
    dp[i + 1][j + 1] += dp[i][j] * P[i]
    
ans = 0
for i in range(N + 1):
  if i > N // 2:
    ans += dp[-1][i]

print(ans)

### FIXED CODE ###

n=int(input())
p=list(map(float,input().split()))
dp=[[0 for j in range(n+1)] for i in range(n+1)]

dp[0][0]=1
for i in range(n):
  for j in range(n):
    dp[i+1][j+1]+=dp[i][j]*p[i]
    dp[i+1][j]+=dp[i][j]*(1-p[i])
ans=0
for i in range(n,-1,-1):
  if i>n-i:
    ans+=dp[-1][i]
  else:
    break
print(ans)
### BUGGY CODE ###

K=int(input())
if K%2 == 0 or K%5 == 0:
  print(-1)
else:
  base = ''
  count = 1
  while(1):
    base += '7'
    if int(base)%K == 0:
      print(count)
      break
    else:
      count += 1
  


### FIXED CODE ###

K = int(input())
base = 0

for count in range(1, K+1):
  num = base*10 + 7
  if (num%K == 0):
    print(count)
    exit()
  else:
    base = num%K
print(-1)
### BUGGY CODE ###

def calc_k(p, ws):
    k = 1
    s = 0
    for w in ws:
        if s + w <= p:
            s += w
        else:
            k += 1
            s = w
    return k

def search_minimum_p_internal(lp, rp, k, ws):
    "?????????P????????????????????¢?´¢??????"
    cp = (lp + rp) // 2
    k1 = calc_k(cp, ws)
    if k1 == k:
        # ???????????¨????????????????°?????????¨???k??°????????????????????????
        return cp
    elif k1 < k:
        # ???????????¨???????????????k??°???????°?????????????????????§????????????????????????????????????
        return search_minimum_p_internal(lp, cp - 1, k, ws)
    else:
        # ???????????¨???????????????k??°????????????????????????????????????????????????????¶?????????????
        return search_minimum_p_internal(cp + 1, rp, k, ws)

def search_minimum_p(k, ws):
    """
    max(P) = max(wi) * max(n)
    ????????¨???????????????1??°?????¨????????????????????????
    min(P) = max(wi)
    ?°?????????¨??????????????????????????§????????????????????????????????????
    """
    maxp = 10000 * 100000
    minp = max(ws)
    p = search_minimum_p_internal(minp, maxp, k, ws)
    while True:
        k1 = calc_k(p - 1, ws)
        if k1 == k and p > minp:
            p -= 1
        else:
            return p

n, k = map(int, input().split())
ws = []
for i in range(n):
    ws.append(int(input()))

print(search_minimum_p(k, ws))

### FIXED CODE ###

def calc_k(p, ws):
    k = 1
    s = 0
    for w in ws:
        if w > p:
            return None
        elif s + w <= p:
            s += w
        else:
            k += 1
            s = w
    return k

def search_minimum_p(k, ws):
    """
    ?????????P????????????????????¢?´¢??????
    max(P) = max(wi) * max(n)
    ????????¨???????????????1??°?????¨????????????????????????
    min(P) = max(wi)
    ?°?????????¨??????????????????????????§????????????????????????????????????
    """
    lp = max(ws)
    rp = 10000 * 100000

    while lp <= rp:
        cp = (lp + rp) // 2
        k1 = calc_k(cp, ws)
        if k1 <= k:
            # ???????????¨???????????????k??°??\????????????????????§??????
            k2 = calc_k(cp - 1, ws)
            if k2 == None or k2 > k:
                # cp???k??°???????????????????????????p???????°??????§??????
                return cp
            else:
                # ???????°???????p????????¨??????
                rp = cp - 1
        else:
            # ???????????¨???????????????k??°????????????????????????????????????????????????????¶?????????????
            lp = cp + 1

    return None

n, k = map(int, input().split())
ws = []
for i in range(n):
    ws.append(int(input()))

print(search_minimum_p(k, ws))
### BUGGY CODE ###

n,m = map(int, input().split())
a = [int(input()) for _ in range(m)]
p = [0]*(n+1)
p[0] = 1
if 1 not in a:
  p[1] = 1

t = 1000000007

for i in range(2, n+1):
  if i in a:
    continue
  s = p[i-1] + p[i-2]
  s = s%t
  p[i] = s
  
print(p[n])  
  


### FIXED CODE ###

n,m = map(int, input().split())
a = set([int(input()) for _ in range(m)])
inf = 1000000007

dp = [0] * (n + 1)
dp[0] = 1
if 1 not in a:
  dp[1] = 1
else:
  dp[1] = 0
  
for i in range(2, n+1):
  if i in a:
    continue
  dp[i] = dp[i - 1] + dp[i - 2]
  dp[i] %= inf
  
print(dp[n])
### BUGGY CODE ###

s=input()
q=int(input())
for i in range(q):
  k=input()
  if k[0]=='1':
    s=s[::-1]
  else:
    if k[2]=='1':
      s=k[4]+s
    elif k[2]=='2':
      s+=k[4]
print(s)

### FIXED CODE ###

s=input()
q=int(input())
cnt=0
l=''
r=''
for i in range(q):
  k=input()
  if k[0]=='1':
    cnt+=1
  else:
    if k[2]=='1':
      if cnt%2==0:
        l=k[4]+l
      else:
        r+=k[4]
    elif k[2]=='2':
      if cnt%2!=0:
        l=k[4]+l
      else:
        r+=k[4]
if cnt%2==0:
  s=l+s+r
else:
  s=l+s+r
  s=s[::-1]
print(s)
### BUGGY CODE ###

N, M = map(int, input().split())
H = list(map(int, input().split()))
A = [0 for k in range(M)]
B = [0 for k in range(M)]

for k in range(M):
    A[k], B[k] = map(int, input().split())
    A[k] -= 1
    B[k] -=1
good = [1 for k in range(N)]
C = A.append(B)
for k in range(N):
    if good[k] ==1:

        for j in range(M):
            if k == A[j] and H[k]<=H[B[j]]:
                good[k] = 0
            if k == A[j] and H[k]>=H[B[j]]:
                good[B[j]] = 0
            if k == B[j] and H[k]<=H[A[j]]:
                good[k] = 0
            if k == B[j] and H[k]>=H[A[j]]:
                good[A[j]] = 0
        
        

print(sum(good))
        

### FIXED CODE ###

N, M = map(int, input().split())
H = list(map(int, input().split()))
A = [0 for k in range(M)]
B = [0 for k in range(M)]

for k in range(M):
    A[k], B[k] = map(int, input().split())
    A[k] -= 1
    B[k] -= 1
good = [1 for k in range(N)]

for k in range(M):
    if H[A[k]] <= H[B[k]]:
        good[A[k]] = 0
    if H[A[k]] >= H[B[k]]:
        good[B[k]] = 0   
        '''
for k in range(N):
    if good[k] ==1:
        if k in A:
            for j in range(M):
                if k == A[j] and H[k]<=H[B[j]]:
                    good[k] = 0
                if k == A[j] and H[k]>=H[B[j]]:
                    good[B[j]] = 0
        if k in B:
            for j in range(M):
                if k == B[j] and H[k]<=H[A[j]]:
                    good[k] = 0
                if k == B[j] and H[k]>=H[A[j]]:
                    good[A[j]] = 0
'''
print(sum(good))
### BUGGY CODE ###

from collections import deque

def main():
    n,k = map(int,input().split())

    a = [int(x) for x in input().split()]

    #ループを探す？

    checked_list = [1]
    que = deque([a[0]])

    counter = 0
    while True:
        counter += 1
        x = que.popleft()
        if counter == k:
            print(x)
            exit()
        if x in checked_list:
            startflg = checked_list.index(x)
            loop_length = counter - startflg
            loop_list = checked_list[startflg:counter]
            amari = (k-startflg) % loop_length
            print(loop_list[amari])
            exit()
        else:
            que.append(a[x-1])
            checked_list.append(x)

if __name__ == '__main__':
    main()

### FIXED CODE ###

from collections import deque
import sys

def main():
    input = sys.stdin.readline
    n,k = map(int,input().split())

    a = [int(x) for x in input().split()]

    #ループを探す？

    checked_dict = {1:0}
    que = deque([a[0]])

    counter = 0


    while True:
        counter += 1
        x = que.popleft()
        if counter == k:
            print(x)
            exit()
        if x in checked_dict:
            startflg = checked_dict[x]
            loop_length = counter - startflg

            inverse_dict = {v:k for k,v in checked_dict.items()}

            loop_list = [inverse_dict[i] for i in range(startflg,startflg+loop_length)]
            amari = (k-startflg) % loop_length
            print(loop_list[amari])
            exit()
        else:
            que.append(a[x-1])
            checked_dict[x] = counter

if __name__ == '__main__':
    main()
### BUGGY CODE ###

import sys
input = sys.stdin.readline

n = int(input())
a = list(map(int, input().split()))
b = 1

for i in range(n):
    b *= a[i]
    
if b > 10 ** 18:
    b = -1

print(b)

### FIXED CODE ###

import sys
input = sys.stdin.readline

n = int(input())
a = list(map(int, input().split()))
b = 1

for i in range(n):
    b *= a[i]
    if b > 10 ** 18:
        b = -1
        break

for i in range(n):
    if a[i] == 0:
        b = 0

print(b)
### BUGGY CODE ###

from bisect import bisect_left, bisect_right

N, K = map(int, input().split())
As = sorted(map(int, input().split()))

def check(t):
    "if the number of the pairs (i, j) that satisfies Ai * Aj <= t is < K, return True"
    count = 0
    for i, A in enumerate(As):
        if A > 0:
            index = bisect_right(As, t//A, lo = i+1) - (i+1)
        elif A < 0:
            index = N - bisect_left(As, -((-t)//A), lo = i+1)
        else:
            if t < 0:
                index = 0
            else:
                index = N - i - 1
        count += index
    return (count < K)
        


l = - (10 ** 18 + 10)
r = 10 ** 18 + 10
while r - l > 1:
    mid = (l + r) // 2
    if check(mid):
        l = mid
    else:
        r = mid

print(r)

### FIXED CODE ###

from bisect import bisect_left, bisect_right

N, K = map(int, input().split())
As = sorted(map(int, input().split()))
        
l = - (10 ** 18 + 10)
r = 10 ** 18 + 10
while True:
    mid = (l + r) // 2
    count = 0
    for i, A in enumerate(As):
        if A > 0:
            count += bisect_right(As, mid//A, lo = i+1) - (i+1)
        elif A < 0:
            count += N - bisect_left(As, -((-mid)//A), lo = i+1)
        else:
            if mid >= 0:
                count += N - i - 1
    if count < K:
        l = mid
    else:
        r = mid
    if r - l == 1:
        break

print(r)
### BUGGY CODE ###

from sys import stdin
input = stdin.readline


def main():
  N = int(input())
  G = [[] for _ in range(N+1)]
  for i in range(N-1):
    u, v, w = map(int, input().split())
    G[u].append((v, w))
    G[v].append((u, w))

  color = [-1]*(N+1)
  color[1] = 0
  S = [1]
  S_append = S.append
  S_pop = S.pop
  while len(S):
    now = S[-1]
    for next_, w in G[now]:
      if color[next_] != -1:
        continue
      S_append(next_)
      color[next_] = (w % 2) ^ color[now]
      break
    else:
      S_pop()

  for i in range(1, N+1):
    print(color[i])


if(__name__ == '__main__'):
  main()


### FIXED CODE ###

import sys
from collections import defaultdict, deque

def main():
    input = sys.stdin.readline
    N = int(input())
    d = defaultdict(list)
    for _ in range(N-1):
        u, v, w = map(int, input().split())
        d[u-1].append((v-1, w))
        d[v-1].append((u-1, w))

    dist = [0] * N
    q = deque()
    q.append((0, 0))
    visited = set()

    while len(q) > 0:
        q_ = deque()
        while len(q) > 0:
            node1, cost1 = q.popleft()

            dist[node1] = cost1
            visited.add(node1)

            for node2, cost2 in d[node1]:
                if node2 in visited:
                    continue

                q_.append((node2, cost1 + cost2))

        q = q_

    for d in dist:
        print(d % 2)


if __name__ == '__main__':
    main()
### BUGGY CODE ###

X, Y, Z, K = input().split()
X = int(X)
Y = int(Y)
Z = int(Z)
K = int(K)
A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]
C = [int(c) for c in input().split()]
AB = []
for a in range(min(K, len(A))):
    for b in range(min(K, len(B))):
        AB.append(A[a] + B[b])
AB = sorted(AB, reverse=True)
ABC = []
for ab in range(min(K, len(AB))):
    for c in range(min(K, len(C))):
        ABC.append(AB[ab] + C[c])
ABC = sorted(ABC, reverse=True)
for i in range(K):
    print(ABC[i])

### FIXED CODE ###

X, Y, Z, K = [int(x) for x in input().split()]
A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]
C = [int(c) for c in input().split()]
A = sorted(A, reverse=True)
B = sorted(B, reverse=True)
C = sorted(C, reverse=True)
ABC = []
for a in range(len(A)):
    if a+1 > K:
        break
    for b in range(len(B)):
        if b+1 > K:
            break
        elif (a+1)*(b+1) > K:
            break
        for c in range(len(C)):
            if c+1 > K:
                break
            elif (a+1)*(c+1) > K:
                break
            elif (b+1)*(c+1) > K:
                break
            elif (a+1)*(b+1)*(c+1) > K:
                break
            ABC.append(A[a] + B[b] + C[c])
ABC = sorted(ABC, reverse=True)
for i in range(K):
    print(ABC[i])
### BUGGY CODE ###

n, m = map(int, input().split())
a = list(map(int, input().split()))
a = list(reversed(sorted(a)))
ans = 0
minV = 10**15
cnt = 0

while True:
    if m <= 0:
        break
    a[0] = a[0] // 2
    a = list(reversed(sorted(a)))
    m -= 1

ans = sum(a)
print(ans)

### FIXED CODE ###

import heapq

n, m = map(int, input().split())
a = []
#a = list(map(int, input().split()))
#a = list(reversed(sorted(a)))
#heapq.heapify(a)
for i in input().split():
    heapq.heappush(a, -int(i))

ans = 0

for i in range(len(a)):
    while True:
        if m <= 0:
            break

        maxV = heapq.heappop(a)
#        print('maxV: ' + str(maxV))
        heapq.heappush(a, -(-maxV // 2))
        m -= 1
        # print('m: ' + str(m))
        # print('a: ' + str(a))
ans = -sum(a)
print(ans)
### BUGGY CODE ###

n=int(input())
a=[int(o) for o in input().split()]

li=[]
for i in a:
    li.append(i)
    li=li[::-1]
print(*li)

### FIXED CODE ###

n=int(input())
a=[int(i) for i in input().split()]

if n%2==0:
    li1=a[::2]
    li2=a[::-2]
    ans=li2+li1
    print(*ans)
else:
    li1=a[1::2]
    li2=a[::-2]
    ans=li2+li1
    print(*ans)
### BUGGY CODE ###

import numpy as np
n = int(input())
a = list(map(int,input().split()))
for i in range(n):
    print(int(np.median(a[0:i]+a[i+1:n])))

### FIXED CODE ###

n = int(input())
a = list(map(int,input().split()))
b = sorted(a)
for i in range(n):
    if b[int(n/2)] <= a[i]:
        print(b[int(n/2)-1])
    else:
        print(b[int(n/2)])
### BUGGY CODE ###

x,y = map(int,raw_input().split())
if x==y:
    print x
else:
    if y>x:
        tmp = x
        x = y
        y = tmp
    i=2
    ans = 1
    al = []
    while i<=x/2:
        if x % i ==0:
            if y % i==0:
                al.append(i)
                x = x/i
                y = y/i
                continue
        i+=1
    if len(al)==0:
        pass
    else:
        for j in al:
            ans *= j
    print ans

### FIXED CODE ###

x,y = map(int,raw_input().split())
if x==y:
    print x
else:
    if y>x:
        tmp = x
        x = y
        y = tmp
    i=2
    ans = 1
    al = []
    while i*i<=x:
        if x % i ==0:
            if y % i==0:
                al.append(i)
                x = x/i
                y = y/i
                continue
        i+=1
    if len(al)!=0:
        for j in al:
            ans *= j
    print ans
### BUGGY CODE ###

X = input()
while X.find("ST") != -1:
    X = X.replace("ST", "", 1)
print(len(X))

### FIXED CODE ###

X = input()
ans = 0

for c in X:
    if c == 'S':
        ans += 1
    if c == 'T' and ans > 0:
        ans -= 1
print(ans * 2)
### BUGGY CODE ###

import copy
from itertools import combinations
N,K = map(int,input().split())
A = list(map(int,input().split()))
ans = 10**9*15
for i in list(combinations(range(2,N+1),K-1)):#2番~N番からK-1個選ぶ
    B = copy.copy(A)
    count = 0
    C = sorted(i)#組み合わせをソート
    
    for j in C:
        while max(B[0:j-1]) >= B[j-1]:#シフト量+2番のビルが1番:B[0]~シフト量+1番:B[k]のビルより高く
            B[j-1] += 1#シフト量+2番目のビルを伸ばす
            count += 1#コストをプラス
    ans = min(ans,count)
print(ans)


### FIXED CODE ###


import copy
N,K = map(int,input().split())
A = list(map(int,input().split()))
ans = 10**9*15
for i in range(2**(N-1)):#2番~N番からK-1個選ぶ
    B = copy.copy(A)
    count = 0
    bit = 0
    for j in range(N-1):#A[1]~A[N-1]の内、選ばれるものの数
        bit = bit + (1&(i>>j))
    if(bit == K-1):#K-1個選ぶ
        for k in range(N-1):
            if(i>>k&1):
                if max(B[0:k+1]) >= B[k+1]:#シフト量+2番のビルが1番:B[0]~シフト量+1番:B[k]のビルより高く
                    dif =max(B[0:k+1])-B[k+1]+1
                    B[k+1] += dif#シフト量+2番目のビルを伸ばす
                    count += dif#コストをプラス
        ans = min(ans,count)
print(ans)
### BUGGY CODE ###

import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

#二分探索
N = ir()
S = sr()

def check(x):
    # x 文字
    bl = False
    for i in range(N):
        for j in range(i+x, N):
            if S[i:i+x] == S[j:j+x]:
                bl = True
    return bl

ok = 0; ng = N
while abs(ng-ok) > 1:
    mid = (ok+ng) // 2
    if check(mid):
        ok = mid
    else:
        ng = mid

print(ok)


### FIXED CODE ###

import sys
#import numpy as np

stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split())) # applies to numbers only
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces

N = ri()
S = rs()

def check(length):
    s = set()
    for i in range(N - 2*length + 1):
        s.add(S[i:i+length])
        if S[i+length:i+2*length] in s:
            return True
    return False

right = N // 2 + 1
left = 0
while right > left + 1:
    mid = (right + left) // 2
    if check(mid):
        left = mid
    else:
        right = mid

print(left)