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

import copy
n, m = map(int, input().split())
ab_lis = []
for i in range(n):
    ab_lis.append(list(map(int, input().split())))
def rec(ab_lis, path, rew, r_day, ans):
    if r_day == 0:
        ans.append(rew)
        return
    for t_lis in ab_lis:
        if t_lis in path:
            continue
        elif t_lis[0] > r_day:
            ans.append(rew)
            continue
        path_c = copy.copy(path)
        path_c.append(t_lis)
        rec(ab_lis, path_c, rew + t_lis[1], r_day - 1, ans)   
ans = []
rec(ab_lis, [], 0, m, ans)        
print(max(ans))

### FIXED CODE ###

import collections
import heapq
n, m = map(int, input().split())
c = collections.defaultdict(list)
for i in range(n):
    a, b = map(int, input().split())
    if a > m:
        continue
    c[m - a].append(b)
h = []
heapq.heapify(h)    
ans = 0
for i in range(m - 1, -1, -1):
    for b in c[i]:
        heapq.heappush(h, -b)
    if len(h) >0:
        ans += -1 * heapq.heappop(h)
print(ans)
### BUGGY CODE ###

n,m = map(int, input().split())
ab = []
num = 0
for i in range(m):
    a,b = map(int, input().split())
    if a == 1 or b == n:
        num += 1
        ab.append((a,b))
ab.sort()
x = 'IMPOSSIBLE'
for h in range(num):
    if (ab[h][1],n) in ab:
        x = 'POSSIBLE'
        break
print(x)

### FIXED CODE ###

n,m = map(int, input().split())
b1 = []
b2 = []
for i in range(m):
    a,b = map(int, input().split())
    if a == 1:
        b1.append(b)
    if b == n:
        b2.append(a)
if len(b1) + len(b2) == len(list(set(b1+b2))):
    print('IMPOSSIBLE')
else:
    print('POSSIBLE')
### BUGGY CODE ###

n = int(input())
s = []


for i in range(n):
    x = list(str(input()))
    x.sort()
    s.append(''.join(x))

count = 0
for i in range(n):
    for j in range(i+1, n):
        if s[i] == s[j]:
            count += 1

            
print(count)

### FIXED CODE ###

n = int(input())
s = []


for i in range(n):
    x = list(str(input()))
    x.sort()
    s.append(''.join(x))

    
s.sort()
count = 0
ans = 0

import math
def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

import math
for i in range(n-1):
    if s[i] == s[i+1]:
        count += 1
        if i == n-2:
            ans += comb(count+1,2)
    else:
        if count != 0:
            ans += comb(count+1,2)
        count = 0

print(ans)
### BUGGY CODE ###

N = int(input())
primes = set([2])
for i in range(3, 55555+1):
    for p in primes:
        if i%p==0: break
    else:
        primes.add(i)

from itertools import combinations
p_list = sorted(list(primes))
for c in combinations(p_list, N):
    ok = True
    for c5 in combinations(c, 5):
        if sum(c5) in primes:
            ok = False
    if ok:
        print(*c)
        break

### FIXED CODE ###

N = int(input())
primes = set([2])
mod5_1 = []
for i in range(3, 55555+1):
    if len(mod5_1)==N: 
        break
    for p in primes:
        if i%p==0: 
            break
    else:
        primes.add(i)
        if i%5==1:
            mod5_1.append(i)
            
print(*mod5_1)
### BUGGY CODE ###

n=int(input())
a=list(input().split())
S=""
if n%2==0:
    for i in range(n):
        if i==0:
            S+=a[0]
        elif i%2==0:
            S=S+" "+a[i]
        else:
            S=a[i]+" "+S
else:
    for i in range(n):
        if i==0:
            S+=a[0]
        elif i%2==1:
            S=S+" "+a[i]
        else:
            S=a[i]+" "+S
print(S)


### FIXED CODE ###

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

l=a[0:n:2]
r=a[1:n:2]
if n%2==1:
    print(*l[::-1],*r)
else:
    print(*r[::-1],*l)
### BUGGY CODE ###

n = int(input())
s = input()
pair = []
b_num = []
ans = 0
for i in range(n - 1):
    if s[i] == "B":
        b_num.append(i)
        continue
    for j in range(i + 1, n):
        if s[j] == "B" or s[j] == s[i]: continue
        pair.append((i, j))
if s[-1] == "B":
    b_num.append(n - 1)

for i, j in pair:
    cnt = 0
    if 2 * i - j in b_num:
        cnt += 1
    if (j - i) % 2 == 0 and (i + j) // 2 in b_num:
        cnt += 1
    if 2 * j - i in b_num:
        cnt += 1
    ans += len(b_num) - cnt
print(ans)

### FIXED CODE ###

n = int(input())
s = input()
bad = [0] * n
pair = 0
b_num = []
ans = 0
for i in range(n - 1):
    if s[i] == "B":
        b_num.append(i)
        continue
    for j in range(i + 1, n):
        if s[j] == "B" or s[j] == s[i]: continue
        pair += 1
        if 2 * i - j >= 0:
            bad[2 * i - j] += 1
        if (j - i) % 2 == 0:
            bad[(i + j) // 2] += 1
        if 2 * j - i < n:
            bad[2 * j - i] += 1
if s[-1] == "B":
    b_num.append(n - 1)

for k in b_num:
    ans += pair - bad[k]
print(ans)
### BUGGY CODE ###

def peak():
    # 入力
    N, M = map(int, input().split())
    H = list(map(int, input().split()))
    A = list()
    B = list()
    for _ in range(M):
        dummy = list(map(int, input().split()))
        A.append(dummy[0])
        B.append(dummy[1])
    # 初期処理
    is_good_list = list()
    # 各展望台が良い展望台か判別
    for ovservatory_num in range(1,N+1):
        is_good = True # 良い展望台 = True, 悪い展望台 = False
        for j in range(M):
            if A[j] == ovservatory_num:
                if H[A[j] - 1] > H[B[j] - 1]:
                    is_good = True
                else:
                    is_good = False
                    break
            elif B[j] == ovservatory_num:
                if H[B[j] - 1] > H[A[j] - 1]:
                    is_good = True
                else:
                    is_good = False
                    break
            else:
                pass
        is_good_list.append(is_good)
    # 良い展望台の個数
    return is_good_list.count(True)    

result = peak()
print(result)

### FIXED CODE ###

def peak():
    # 入力
    N, M = map(int, input().split())
    H = list(map(int, input().split()))
    A = list()
    B = list()
    for _ in range(M):
        dummy = list(map(int, input().split()))
        A.append(dummy[0])
        B.append(dummy[1])
    # 道がつながっている中で最大の展望台の高さを保存
    ovservatory_max_height = [0] * N
    for i in range(M):
        if H[B[i] - 1] > ovservatory_max_height[A[i] - 1]:
            ovservatory_max_height[A[i] - 1] = H[B[i] - 1]
        if H[A[i] - 1] > ovservatory_max_height[B[i] - 1]:
            ovservatory_max_height[B[i] - 1] = H[A[i] - 1]
    # つながっている展望台の最大長と比較
    is_good_list = list()
    for i in range(N):
        if H[i] > ovservatory_max_height[i] or ovservatory_max_height == 0:
            is_good_list.append(True)
        else:
            is_good_list.append(False)
    # 良い展望台の個数
    return is_good_list.count(True)    

result = peak()
print(result)
### BUGGY CODE ###

S = input()
S = S + '0'
mod = 2019
p = [0] * mod
r = 0
d = 1
for s in reversed(S):
  t = int(s)%mod
  r += t*d
  r %= mod
  d = d*10
  p[r] += 1

print(sum([i*(i-1)//2 for i in p]))

### FIXED CODE ###

S = input()
S = S + '0'
mod = 2019
p = [0] * mod
r = 0
d = 1
for s in reversed(S):
  t = int(s)%mod
  r += t*d
  r %= mod
  d = d*10%mod
  p[r] += 1

print(sum([i*(i-1)//2 for i in p]))
### BUGGY CODE ###

#ABC118C-MonstersBattleRoyale.py

from sys import stdin

input = stdin.readline

N = int(input())

A = list(map(int,input().split()))

A = set(A)
A = list(A)

a = 0

while len(A) != 1:
    A.sort()
    for i in range(1,len(A)):
        A[i] = A[i] - A[0]
    if A[0] == 0:
        A.pop(0)
    if len(A) <= N/2:
        break

i = 0
while True:
    if A[i] != 0:
        print(A[i])
        break
    i += 1


### FIXED CODE ###

#ABC118C-MonstersBattleRoyale.py

from sys import stdin
from functools import reduce
import fractions

input = stdin.readline

N = int(input())

A = list(set(map(int,input().split())))

answer = reduce(fractions.gcd, A)
print(answer)
### BUGGY CODE ###

N = int(input())
S = input()
ans = N
for i in range(N):
    left = S[:i]
    right = S[i + 1:]
    if left.count('W') + right.count('E') < ans:
        ans = left.count('W') + right.count('E')
print(ans)

### FIXED CODE ###

N = int(input())
S = input()
e_count = S.count('E')
w_count = 0
ans = e_count
for s in S:
    if s == 'E':
        e_count -= 1
    else:
        w_count += 1
    ans = min(ans, e_count + w_count)
print(ans)
### BUGGY CODE ###

from collections import Counter

N = int(input())
S = [Counter(input()) for _ in range(N)]

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

print(ans)


### FIXED CODE ###

from collections import Counter, defaultdict

N = int(input())
S = [Counter(input()) for _ in range(N)]

dd = defaultdict(int)

for i in S:
    p = 0
    for j in i:
        p += 26**(ord(j)-ord('a'))*(i[j])
    dd[p] += 1

ans = 0
a = dd.keys()
for i in a:
    ans += (dd[i]-1)*dd[i]//2
print(ans)
### BUGGY CODE ###

import random
D = int(input())
c = list(map(int, input().split()))
last = [0] * 26
ans = []
S = []
prev_score = 0
maxsi_list = []

for d in range(1, D+1):
    s = list(map(int, input().split()))
    S += [s]
    maxs = float('-inf')
    maxss = float('-inf')
    maxi = -1
    maxii = -1
    cumsump = 0
    for i in range(26):
        tmp = -c[i] * (d - last[i])
        cumsump += tmp
    for i in range(26):
        tmp = -c[i] * (d - last[i])
        tmp2 = s[i] + cumsump - tmp
        if tmp2 > maxs:
            maxs = tmp2
            maxi = i
        if s[i] > maxss:
            maxii = i
            maxss = s[i]
    ans += [maxi+1]
    last[maxi] = d
    prev_score += maxs
    maxsi_list += [maxii+1]
best_ans = ans
best_score = prev_score
mergin = 1300
# mergin = 0
# mergin = 13000
schedule = [i for i in range(D)]
for tt in range(31):
    random.shuffle(schedule)
    for dd in schedule:
        last = [0] * 26
        score = 0
        for d in range(1, D+1):
            if dd == (d-1):
                maxc = 0
                maxci = -1
                for i in range(26):
                    cost = c[i] * (d - last[i])
                    if maxc < cost:
                        maxc = cost
                        maxci = i+1
                prev_a = ans[dd]
                ans[dd] = maxci
            td = ans[d-1] - 1
            cumsump = 0
            for i in range(26):
                if td != i:
                    tmp = -c[i] * (d - last[i])
                    cumsump += tmp
            last[td] = d
            score += S[d-1][td]+cumsump
        if score <= prev_score-mergin:
        # if score <= best_score-mergin:
            ans[dd] = prev_a
        elif best_score < score:
            best_ans = ans
            best_score = score
            prev_score = score
        else:
            prev_score = score
    random.shuffle(schedule)
    for dd in schedule:
        last = [0] * 26
        score = 0
        prev_a = ans[dd]
        ans[dd] = maxsi_list[dd]
        for d in range(1, D+1):
            td = ans[d-1] - 1
            cumsump = 0
            for i in range(26):
                if td != i:
                    tmp = -c[i] * (d - last[i])
                    cumsump += tmp
            last[td] = d
            score += S[d-1][td]+cumsump
        if score <= prev_score-mergin:
        # if score <= best_score-mergin:
            ans[dd] = prev_a
        elif best_score < score:
            best_ans = ans
            best_score = score
            prev_score = score
        else:
            prev_score = score

for a in best_ans:
    print(a)

### FIXED CODE ###

D = int(input())
c = list(map(int, input().split()))
last = [0] * 26
ans = []
for d in range(1, D+1):
    s = list(map(int, input().split()))
    maxs = float('-inf')
    maxi = -1
    cumsump = 0
    for i in range(26):
        tmp = -c[i] * (d - last[i])
        cumsump += tmp
    for i in range(26):
        tmp = -c[i] * (d - last[i])
        tmp2 = s[i] + cumsump - tmp
        if tmp2 > maxs:
            maxs = tmp2
            maxi = i
    ans += [maxi+1]
    last[maxi] = d
        
for a in ans:
    print(a)
### BUGGY CODE ###

def main():
    n = int(input())
    alphabet = "abcdefghijklmnopqrstuvwxyz"
    d_alpha = {j: i for i, j in enumerate(alphabet)}
    s = input()
    s = [d_alpha[i] for i in s]
    m = 0
    for i in range(1, n):
        cnt_m = 0
        cnt = 0
        for j in range(i, n):
            if s[j-i] == s[j]:
                cnt = min(cnt+1, i)
            else:
                cnt_m = max(cnt, cnt_m)
                cnt = 0
        m = max(cnt, cnt_m, m)
    print(m)


main()


### FIXED CODE ###

def main():
    n=int(input())
    s=input()
    m=0
    for i in range(1,n):
        cnt_m=0
        cnt=0
        for j in range(i,n):
            if s[j-i]==s[j]:
                cnt=min(cnt+1,i)
            else:
                cnt_m=max(cnt,cnt_m)
                cnt=0
        m=max(cnt,cnt_m,m)
    print(m)

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

import bisect
n = int(input())
l = sorted(list(map(int,input().split())))
cou =0
for i in range(0,n-2):
  for j in range(i+1,n-1):
    #iがaの長さ、jがbの長さ
    m = l[i]
    u = l[j]
    l1 = l[j+1:]
    c =bisect.bisect_left(l1,m+u)
    cou += c
print(cou)
    

### FIXED CODE ###

import bisect
n = int(input())
l = sorted(list(map(int,input().split())))
cou =0
for i in range(0,n-2):
  for j in range(i+1,n-1):
    #iがaの長さ、jがbの長さ
    m = l[i]
    u = l[j]
    c =bisect.bisect_left(l,m+u)
    if c>j+1:
      cou += c-j-1
print(cou)
### BUGGY CODE ###

N, W = map(int, raw_input().split())
dp = [0]*(W+1)

for i in range(N):
    w, v = map(int, raw_input().split())
    for j in range(W, w-1, -1):
        x = dp[j-w] + v
        if x > dp[j]:
            dp[j] = x

print dp[-1]

### FIXED CODE ###

def main():

    N, W = map(int, raw_input().split())
    dp = [0]*(W+1)
 
    for i in xrange(N):
        w, v = map(int, raw_input().split())
        for j in xrange(W, w-1, -1):
            x = dp[j-w] + v
            if x > dp[j]:
                dp[j] = x
 
    print dp[-1]

main()
### BUGGY CODE ###

S = input()

dp = [[(0,0)]*13 for j in range(len(S)+1)]
mod = 10**9+7

dp[0][0] = (1,1)
k = 1

for s in S:
  if ('?' == s):
    L = list(range(10))
  else:
    L = [int(s)]

  for l in L:
    for i in range(13):
      if (dp[k-1][i][1] == 1):
        tmp = (i * 10) + l
        m = tmp % 13
        c = (tmp // 13)
        dp[k][m] = ((dp[k][m][0] + dp[k-1][i][0]) % mod, 1)
  k += 1
print(dp[k-1][5][0])


### FIXED CODE ###

S = input()
 
dp = [[0]*13 for j in range(len(S)+1)]
mod = 10**9+7
 
dp[0][0] = 1
k = 1
L0 = list(range(10))

for s in S:
  if ('?' == s):
    L = L0
  else:
    L = [int(s)]
 
  for l in L:
    for i in range(13):
      tmp = (i * 10) + l
      m = tmp % 13
      dp[k][m] = ((dp[k][m] + dp[k-1][i]) % mod)
  k += 1
print(dp[k-1][5])
### BUGGY CODE ###

def main():
    from collections import deque
    n, q = map(int, input().split())
    g = [[] for i in range(n+1)]
    point = [0 for i in range(n+1)]
    for i in range(n-1):
        a, b = map(int, input().split())
        g[a].append(b)
        g[b].append(a)
    for i in range(q):
        p, x = map(int, input().split())
        point[p] += x

    q = deque([[1,0]])
    q_append = q.append
    q_pop = q.pop
    for j in range(n):
        now = q_pop()
        point[now[0]] += point[now[1]]
        for i in range(len(g[now[0]])):
            if g[now[0]][i]==now[1]:
                continue
            q_append([g[now[0]][i],now[0]])
    for i in range(1, n+1):
        print(point[i], end='')
        if i!=n:
            print(' ', end='')

if __name__ == '__main__':
    main()


### FIXED CODE ###

def main():
    from collections import deque
    n, q = map(int, input().split())
    g = [[] for i in range(n+1)]
    point = [0 for i in range(n+1)]
    for i in range(n-1):
        a, b = map(int, input().split())
        g[a].append(b)
        g[b].append(a)
    for i in range(q):
        p, x = map(int, input().split())
        point[p] += x

    q = deque([[1,0]])
    for j in range(n):
        now = q.pop()
        point[now[0]] += point[now[1]]
        for i in g[now[0]]:
            if i==now[1]:
                continue
            q.append([i,now[0]])
    for i in range(1, n+1):
        print(point[i], end='')
        if i!=n:
            print(' ', end='')

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

n = int(input())
a = list(map(int,input().split()))
mod = 10**9 + 7
ans = 0
keta = 0
while a != [0]*n:
    b = list(map(lambda x: x % 2, a))
    a = list(map(lambda x: x//2, a))
    ans += b.count(0) * b.count(1) * 2**keta
    ans %= mod
    keta += 1
print(ans)


### FIXED CODE ###

n = int(input())
a = list(map(int,input().split()))
a = list(map(lambda x: (str(bin(x))[2:]).zfill(65), a))
mod = 10**9 + 7
ans = 0
for i in range(65):
    cnt = 0
    for j in a:
        if j[-1*(i+1)] == "1": cnt += 1
    ans += cnt * (n-cnt) * pow(2, i, mod) % mod
    ans %= mod
print(ans)
### BUGGY CODE ###

import heapq
import math
xs, ys, xt, yt = map(int, input().split())
N = int(input())

Coordinate = {-1: (xs, ys, 0), N:(xt, yt, 0)}
minRadiation = {int(i):10**20 for i in range(-1, N+1)}
ToVisit = {int(i) for i in range(-1, N+1)}
for i in range(N):
    x, y, r = map(int, input().split())
    Coordinate[i] = (x, y, r)

Q = [(0, -1)]
heapq.heapify(Q)
while Q:
    nowRadiation, index = heapq.heappop(Q)
    if index == N:
        print(nowRadiation)
        break
    elif index in ToVisit:
      ToVisit.difference_update({index})
      nowx, nowy, nowr = Coordinate[index]
      for nextIndex in ToVisit:
          nextx, nexty, nextr = Coordinate[nextIndex]
          tempTime = max(0, math.sqrt((nextx-nowx)**2 + (nexty-nowy)**2) - nowr - nextr) + nowRadiation
          if minRadiation[nextIndex] > tempTime:
              minRadiation[nextIndex] = tempTime
              heapq.heappush(Q, (tempTime, nextIndex))

### FIXED CODE ###

import heapq
import math
xs, ys, xt, yt = map(int, input().split())
N = int(input())

Coordinate = {-1: (xs, ys, 0), N:(xt, yt, 0)}
minRadiation = {int(i):10**20 for i in range(-1, N+1)}
ToVisit = {int(i) for i in range(-1, N+1)}
for i in range(N):
    x, y, r = map(int, input().split())
    Coordinate[i] = (x, y, r)

Q = [(0, -1)]
heapq.heapify(Q)
while Q:
    nowRadiation, index = heapq.heappop(Q)
    if index == N:
        print(nowRadiation)
        break
    elif index in ToVisit:
      ToVisit.difference_update({index})
      nowx, nowy, nowr = Coordinate[index]
      for nextIndex in ToVisit:
          nextx, nexty, nextr = Coordinate[nextIndex]
          tempTime = max(0, math.sqrt((nextx-nowx)**2 + (nexty-nowy)**2) - nowr - nextr) + nowRadiation
          if minRadiation[nextIndex] > tempTime:
              minRadiation[nextIndex] = tempTime
              heapq.heappush(Q, (tempTime, nextIndex))
### BUGGY CODE ###

n,m = map(int,input().split())

root = [[] for i in range(n)]
for _ in range(m):
    a, b = (int(x) for x in input().split())
    root[b-1].append(a-1)
    root[a-1].append(b-1) 

stack=[0]
check = [-1]*n
check[0] = 0

while len(stack)>0:
    v = stack.pop(0)
    for i in root[v]:
        if check[i] == -1:
            check[i]=v+1
            stack.append(i)

if -1 in check:
    print('No')
else:
    print('Yes')
    for i in range(1,n):
        print(check[i])

### FIXED CODE ###

N, M = map(int, input().split())
root = [[] for _ in range(N+1)]
 
for i in range(M):
    a, b = map(int, input().split())
    root[a].append(b)
    root[b].append(a)
 
ans = [10**9] * (N+1)
ans[0] = 0
ans[1] = 0
 
from collections import deque
q = deque([1])
 
while q:
    p = q.popleft()
    for i in root[p]:
        if ans[i] == 10**9:
            ans[i] = p
            q.append(i)
 
if 10**9 in set(ans):
    print('No')
else:
    print('Yes')
    for i in range(2, N+1):
        print(ans[i])
### BUGGY CODE ###

n = int(input())
x = list(map(int, input().split()))
X = sorted(x)
num = int(n/2)
ans = dict()
for i in range(n):
	target = X[:i] + X[i+1:]
		
	ans.setdefault(X[i], target[num - 1])
for i in x:
	print(ans[i])	

### FIXED CODE ###

n = int(input())
x = list(map(int, input().split()))
ans = dict()
x_copy = sorted(x[:])
right = x_copy[(n//2)-1]
left = x_copy[-(n//2)]

for i in range(n):
    if i < n//2:
        ans[x_copy[i]] = left
    else:
        ans[x_copy[i]] = right

for i in x:
    print(ans[i])
### BUGGY CODE ###

n, k = map(int, input().split(' '))
a = [int(tmp) for tmp in input().split(' ')]
point = 1

for i in range(k):
    point *= a[i]

for i in range(n - k):
    f_point = point // a[i] * a[k+i]
    if point < f_point:
        print('Yes')
    else:
        print('No')
    
    point = f_point


### FIXED CODE ###

n, k = map(int, input().split(' '))
a = [int(tmp) for tmp in input().split(' ')]
[print('Yes') if a < b else print('No') for a, b in zip(a[:n - k], a[k:])]
### BUGGY CODE ###

import numpy as np
N,K,Q=map(int,input().split())
score=np.array([K for i in range(N)])
loss=np.array([1 for i in range(N)])
for i in range(Q):
  A=int(input())
  score=score-loss
  score[A-1]+=1
for i in range(N):
  if(score[i]<=0):
    print("No")
  else:
    print("Yes")

### FIXED CODE ###

N,K,Q=map(int,input().split())
count=[0 for i in range(N)]
for i in range(Q):
  A=int(input())
  count[A-1]+=1
for i in range(N):
  if(count[i]>Q-K):
    print("Yes")
  else:
    print("No")
### BUGGY CODE ###

# DP
N, W = map(int, input().split())

dp = [-1] * (W + 1)
dp[0] = 0
max_w = 0
for i in range(N):
  w, v = map(int, input().split())
  for j in range(min(max_w, W - w), -1, -1):
    if dp[j] != -1:
      t = j + w
      dp[t] = max(dp[t], dp[j] + v)
      if t > max_w:
        max_w = t
print(max(dp))


### FIXED CODE ###

n, w = [int(e) for e in input().split()]
d = [[int(e) for e in input().split()] for i in range(n)]
t = {}
t[w] = 0
for i in range(n):
  keys = list(sorted(t.keys()))
  tw, tv = d[i]
  for j in keys:
    nw = j - tw
    if nw < 0:
      continue
    nv = t[j] + tv
    if nw in t:
      if nv > t[nw]:
        t[nw] = nv
    else:
      t[nw] = nv
print(max(t.values()))
### BUGGY CODE ###

import copy

N = int(input())

A = [int(input()) for i in range(N)]

for i in range(N):
    B = copy.copy(A)
    B[i] = 0
    print(max(B))

### FIXED CODE ###

import copy

N = int(input())

A = [int(input()) for i in range(N)]

B = copy.copy(A)
B.sort()

for i in range(N):
    if A[i] == B[-1]:
        print(B[-2])
    else:
        print(B[-1])
### BUGGY CODE ###

n = int(input())
nn = 0
i = 357

while i <= n:
  s = str(i)
  if ('0' in s or '1' in s or '2' in s or '4' in s or '6' in s or '8' in s or '9' in s) is False:
    if '3' in s:
      if '5' in s:
        if '7' in s:
          #print(i)
          nn += 1
  if i % 10 == 7:
    i += 6
  else:
    i +=2
print(nn)

### FIXED CODE ###

def nextn(n):
    if n == 0:
        return 3
    elif n % 10 == 3:
        return n+2
    elif n % 10== 5:
        return n+2
    elif n % 10 ==7:
        return nextn(n//10)*10 + 3

N = int(input())
n = 357
ans = 0
while n <= N:
    s = str(n)
    a = [False,False,False]
    for c in s:
        if c == '3':
            a[0] = True
        elif c == '5':
            a[1] = True
        elif c == '7':
            a[2] = True
        else:
            print(s)
    if all(a):
        ans += 1
    n = nextn(n) 
print(ans)
### BUGGY CODE ###

s = input()
k = int(input())

a = list(s)
a = [ord(i) for i in a]
b = sorted(a)
m = b[0]
n = 0
c = []
INF = 97+26
while True:
    a1 = [i for i in range(len(s)) if a[i] == m]

    for i in a1:
        j = 0
        while i+j < len(s) or j < k:
            if s[i:i+j+1] not in c:
                c.append(s[i:i+j+1])
                c.sort()
            if len(c) > 5:
                del c[-1]
            j += 1
    if len(c) >= k:
        break
    while b[n] == m:
        n += 1
    m = b[n]
print(c[k-1])


### FIXED CODE ###

s = input()
k = int(input())

a = list(s)
a = [ord(i) for i in a]
b = sorted(set(a))
c = []
INF = 97+26
for m in b:
    a1 = [i for i in range(len(s)) if a[i] == m]

    for i in a1:
        j = 0
        for j in range(k):
            if s[i:i+j+1] not in c:
                c.append(s[i:i+j+1])
                c.sort()
            if len(c) > 5:
                del c[-1]
            j += 1
            if i + j >= len(s):
                break
    if len(c) >= k:
        break

print(c[k-1])