Harryxun/stf_run huggingface dataset:
# Acceptance of input

import sys

f_i = sys.stdin

n = int(f_i.readline())

edges = [[None] * n for i in range(n)]

for l_i in f_i:
    s, t, w = map(int, l_i.split())
    edges[s][t] = w
    edges[t][s] = w

# dfs tree walk 1

import collections

path = collections.deque()
path.append(0)

distance = 0
max_distance = 0

unvisited = [False] + [True] * (n - 1)

while True:
    u = path[-1]
    adj_e = edges[u]
    for v, e in enumerate(adj_e):
        if unvisited[v] and e != None:
            path.append(v)
            distance += e
            unvisited[v] = False
            if max_distance < distance:
                max_distance = distance
                end_point = v
            break
    if u == path[-1]:
        path.pop()
        if path:
            distance -= edges[u][path[-1]]
        else:
            break

# Measurement of diameter(dfs tree walk 2)

path.append(end_point)

distance = 0
diameter = 0

unvisited = [True] * n
unvisited[end_point] = False

while True:
    u = path[-1]
    adj_e = edges[u]
    for v, e in enumerate(adj_e):
        if unvisited[v] and e != None:
            path.append(v)
            distance += e
            unvisited[v] = False
            if diameter < distance:
                diameter = distance
                end_point = v
            break
    if u == path[-1]:
        path.pop()
        if path:
            distance -= edges[u][path[-1]]
        else:
            break

# output

print(diameter)

### FIXED CODE ###

# Acceptance of input

import sys

f_i = sys.stdin

n = int(f_i.readline())

edges = [[] for i in range(n)]

for l_i in f_i:
    s, t, w = map(int, l_i.split())
    edges[s].append((t, w))
    edges[t].append((s, w))

# Tree Walk function

import collections

def tree_walk(start, switch = 0):
    path = collections.deque()
    path.append(start)

    weight = collections.deque()
    weight.append(0)
    
    unvisited = [True] * n
    unvisited[start] = False
    
    rest = n - 1
    end_point = start
    diameter = 0
    
    while True:
        u = path[-1]
        for v, w in edges[u]:
            if unvisited[v]:
                path.append(v)
                unvisited[v] = False
                rest -= 1
                weight.append(w)
                break

        if u == path[-1]:
            distance = sum(weight)
            if diameter < distance:
                diameter = distance
                end_point = u
            if rest == 0:
                break
            path.pop()
            weight.pop()

    if switch:
        print(diameter)
    else:
        return end_point

# Output

tree_walk(tree_walk(0), 1)
# input
S = input()
T = input()

if T.startswith(S):
    print("Yes")
else;
    print("No")

### FIXED CODE ###

# input
S = input()
T = input()

if T.startswith(S):
    print("Yes")
else:
    print("No")
import math

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

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

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

now_lcm = a[0]
for i in range(n-1):
    now_lcm = lcm(now_lcm,a[i+1])

mod = 1e9+7
ans = 0
for e in a:
    ans = ((ans%mod) + ((now_lcm//e)%mod))%mod

print(int(ans))


### FIXED CODE ###

import math

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

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

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

now_lcm = a[0]
for i in range(n):
    now_lcm = lcm(now_lcm,a[i])

ans = 0
for e in a:
    ans += now_lcm//e

print(int(ans%(10**9+7)))
buf = input().split(" ")
H=int(buf[0])
N=int(buf[1])

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

A=sorted(A, reverse=True)

for i in range(N):
  H=H-A

if H<=0:
  print("Yes")
else:
  print("No")



### FIXED CODE ###

buf = input().split(" ")
H=int(buf[0])
N=int(buf[1])
 
A = list(map(int, input().split())) 
 
A=sorted(A, reverse=True)
 
for i in range(N):
  if H<=0:
    break
  H=H-A[i]
 
if H<=0:
  print("Yes")
else:
  print("No")
N = input()
N = int(N)
str = input().split()
H = []
i = 0
j = 0
for i in range(N):
  H.append( int(str[i]) )
flag = []
i = 0
for i in range(N):
  for j in range(i):
    if H [i] < H[j]:
      flag.append(1)
    else:
      flag.append(0)
ans = 0
i = 0
for i in range(N):
  if flag[i] == 0:
    ans = ans+1
print(ans)

### FIXED CODE ###

N = input()
N = int(N)
str = input().split()
H = []
i = 0
j = 0
for i in range(N):
  H.append( int(str[i]) )
flag = 0
ans = 0
for i in range(N):
  for j in range(i):
    if H [i] < H[j]:
      flag =  1
      break
    else:
      flag = 0
  if flag == 0:
    ans = ans+1
  flag = 0

print(ans)
import math
N, K = map(int, input().split())
for i in range(1, K+1):
  ans = 0
  blue = math.factorial(K-1) // (math.factorial(i-1)*math.factorial(K-i))
  red = math.factorial(N-K+1) // (math.factorial(i)*math.factorial(N-K+1-i))
  ans = blue * red
  ans = ans % (10**9+7)
  print(int(ans))

### FIXED CODE ###

import math
N, K = map(int, input().split())
for i in range(1, K+1):
  ans = 0
  blue = math.factorial(K-1) // (math.factorial(i-1)*math.factorial(K-i))
  if N-K+1-i < 0:
    red = 0
  else:
    red = math.factorial(N-K+1) // (math.factorial(i)*math.factorial(N-K+1-i))
  ans = blue * red
  ans = ans % (10**9+7)
  print(int(ans))
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from heapq import heapify, heappop, heappush
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()
H = [INT() for i in range(N)]
H.sort()
ans = INF
for i in range(N-K+1):
    mi = H[i:i+K][0]
    ma = H[i:i+K][2]
    tmp = ma - mi
    ans = min(tmp, ans)
print(ans)


### FIXED CODE ###

n,k=map(int,input().split())
h=[int(input()) for i in range(n)]
h.sort()
#for i in range(n-k+1):
print(min(h[i+k-1]-h[i] for i in range(n-k+1)))
n = int(input())
A = int(input())
B = int(input())
if n == 1:
    print("Hello World")
elif n == 2:
    print(A + B)


### FIXED CODE ###

n = int(input())

if n == 1:
    print("Hello World")
if n == 2:
    a = int(input())
    b = int(input())
    print(a + b)
hoge = ["FizzBuzz" if i % 3 == 0 and i % 5 == 0 else("Fizz" if i % 3 == 0 else("Buzz" if i % 5 == 0 else str(i))) for i in range(10001)]

while True:
    m, n = map(int, input().split())
    if m == 0:
        break
    man = [str(i) for i in range(1, m+1)]
    a, b = divmod(n, m)
    man = a*man+man[:b]
    idx = 0
    for i in range(1, n+1):
        if hoge[i] == input():
            idx += 1
        else:
            v = man[idx]
            man = list(filter((v).__ne__, man))
            if len(man) == 1:
                break
    print(*sorted([int(v) for v in set(man)]))

### FIXED CODE ###

hoge = ["FizzBuzz" if i % 15 == 0 else("Fizz" if i % 3 == 0 else("Buzz" if i % 5 == 0 else str(i))) for i in range(10001)]

while True:
    m, n = map(int, input().split())
    if m == 0:
        break
    man = list(range(1, m+1))
    game = [input() for _ in range(n)]
    idx = 0
    for i in range(n):
        if game[i] != hoge[i+1]:
            del man[idx]
            if len(man) == 1:
                break
        else:
            idx += 1
        idx %= len(man)
    print(*man)
import sys
input = sys.stdin.readline

a, b = map(int, input().split())
if a <= 0:
    if b >= 0:
        ans = 'Zero'
    elif b < 0 and (b-a)%2 == 0:
        ans = 'Negative'
else:
    ans = 'Positive'
print(ans)

### FIXED CODE ###

import sys
input = sys.stdin.readline

a, b = map(int, input().split())
if a <= 0:
    if b >= 0:
        ans = 'Zero'
    elif b < 0 and (b-a)%2 == 0:
        ans = 'Negative'
    else:
        ans = 'Positive'
else:
    ans = 'Positive'
print(ans)
#D
from collections import deque
Str = str(input())
Q = int(input())
S = deque()
S.append(Str)
A = 0
B = []
C = []

for i in range(Q):
    a = list(map(str, input().split()))
    if a[0] == '1':
        if A%2==0:
            if len(B)>1:
                B_=B.reverse()
                print(B_)
                S.extendleft(B_)
                B = []
            else:
                S.extendleft(B)
                B = []
            S.extend(C)
            C = []
        else:
            if len(C)>1:
                C_=C.reverse()
                S.extendleft(C_)
                C = []
            else:
                S.extendleft(C)
                C = []
            S.extend(B)
            B = []
        A += 1
    elif a[1] == '1':
        B.append(a[2])
    else:
        C.append(a[2])

if a[0] != '1':
    if A%2==0:
        if len(B)>1:
            B_=B.reverse()
            print(B_)
            S.extendleft(B_)
            B = []
        else:
            S.extendleft(B)
            B = []
        S.extend(C)
        C = []
    else:
        if len(C)>1:
            C_=C.reverse()
            S.extendleft(C_)
            C = []
        else:
            S.extendleft(C)
            C = []
        S.extend(B)

if A%2 != 0:
    S.reverse()
print(''.join(S))

### FIXED CODE ###

#D
from collections import deque
Str = str(input())
Q = int(input())
S = deque(list(Str))
#S.append(list(Str))
#上の方法ではダメ
A = 0
B = []
C = []

for i in range(Q):
    a = list(map(str, input().split()))
    if a[0] == '1':
        if A%2==0:
            if len(B)>1:
                S.extendleft(B)
                B = []
            elif len(B) == 1:
                S.extendleft(B)
                B = []
            if len(C) > 0:
                S.extend(C)
                C = []
        else:
            if len(C)>1:
                S.extendleft(C)
                C = []
            elif len(C) == 1:
                S.extendleft(C)
                C = []
            if len(B) > 0:
                S.extend(B)
                B = []
        A += 1
    elif a[1] == '1':
        B.append(a[2])
    else:
        C.append(a[2])

if a[0] != '1':
    if A%2==0:
        if len(B)>1:
            S.extendleft(B)
            B = []
        elif len(B) == 1:
            S.extendleft(B)
            B = []
        if len(C) > 0:
            S.extend(C)
            C = []
    else:
        if len(C)>1:
            S.extendleft(C)
            C = []
        elif len(C) == 1:
            S.extendleft(C)
            C = []
        if len(B) > 0:
            S.extend(B)
            B = []

if A%2 != 0:
    S.reverse()
print(''.join(S))
n,m=map(int,input().split())
g=[[] for i in range(n)]
ro=[0]*n
for i in range(n+m-1):
    a,b=map(int,input().split())
    g[a-1].append(b)
    ro[b-1]+=1
for i in range(n):
    if ro[i]==0:
        r=i
ans=[0]*n
q=[r]
while len(q)>0:
    u=q.pop()
    for v in g[u]:
        ans[v-1]=u+1
        q.append(v-1)
for i in ans:
    print(i)


### FIXED CODE ###

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

ans=[0,0]

ans[0]=min(a,b)
ans[1]=max(0,a+b-n)
print(*ans)
n, q = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(n-1)]
PX = [list(map(int, input().split())) for _ in range(q)]

stack = [[] for _ in range(n+1)]
stack[0].append(1)
for i in range(n-1):
	stack[AB[i][0]].append(AB[i][1])

ans = [0] * n
pool = {}
def dfs(cur, num):
	ans[cur-1] += num
	if len(stack[cur]) <= 0:
		return
	for c in stack[cur]:
		dfs(c, num)

for px in PX:
	pool[px[0]] = pool.get(px[0], 0) + px[1]
for k, v in pool.items():
  	dfs(k, v)
print(*ans)

### FIXED CODE ###

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
n, q = map(int, input().split())
 
stack = [[] for _ in range(n)]
for i in range(n-1):
  a,b = map(int, input().split())
  a -= 1
  b -= 1
  stack[a].append(b)
  stack[b].append(a)
ans = [0] * n
for i in range(q):
  p, x = map(int, input().split())
  ans[p-1] += x
def dfs(cur, par):
    for c in stack[cur]:
      if c == par:
        continue
      ans[c] += ans[cur] 
      dfs(c, cur)
dfs(0, -1)
print(*ans)
N=int(input())
A=int(input())
B=int(input())
if N==1:
  print("Hello World")
else:
  print(A+B)
  

### FIXED CODE ###

N=int(input())
if N==1:
  print("Hello World")
else:
  A=int(input())
  B=int(input())
  print(A+B)
q,h,s,d = int(input().strip().split())
n = int(input())

h = min(h, q*2)
s = min(s, h*2)
d = min(d, s*2)

d_num, s_num = n//2, n%2
cost = d_num*d + s_num*s
print(cost)

### FIXED CODE ###

q,h,s,d = list(map(int, input().strip().split()))
n = int(input())

h = min(h, q*2)
s = min(s, h*2)
d = min(d, s*2)

d_num, s_num = n//2, n%2
cost = d_num*d + s_num*s
print(cost)
N=int(input())
a,b,c=map(int,input().split())
if N!=3:
  print("No")
elif a ^ b== c and a ^ c == b and b ^ c ==a:
  print("Yes")
else:
  print("No")


### FIXED CODE ###

import copy
N=int(input())
a_list=list(map(int,input().split()))
a=set(a_list)
a=list(a)
if 0 in a:
        if len(a) ==2:
            if a_list.count(0)==len(a_list)//3 or a_list.count(0)==len(a_list)//2:
                    print("Yes")
            else:
                    print("No")
        elif len(a)==1:
            print("Yes")
        else:
                print("No")
else:
        if len(a)==3:
            a1=a[0]
            a2=a[1]
            a3=a[2]
            if a1 ^ a2 ==a3 and a1^a3==a2 and a2^a3==a1:
                if a_list.count(a1) == a_list.count(a2) == a_list.count(a3):
                        print("Yes")
                else:
                        print("No")
            else:
                print("No")
        else:
            print("No")
class Robot:
    def __init__(self, x, l):
        self.x = x
        self.l = l
    
    def __lt__(self, other):
        return self.x < other.x

n = int(input().rstrip())
robos = [None] * n

for i in range(n):
    line = input().rstrip().split(' ')
    robos[i] = Robot(int(line[0]), int(line[1]))

robos.sort()

i = 0
ans = 0
while i < n:
    s = robos[i].x
    r = robos[i].l
    i += 1
    while i < n and robos[i].x <= s + r:
        i += 1
    
    p = robos[i - 1].x
    while i < n and robos[i].x <= p + robos[i].l:
        i += 1
    
    ans += 1

print(ans)

### FIXED CODE ###

h = int(input().rstrip())
w = int(input().rstrip())
n = int(input().rstrip())

if h > w:
    div = h
else:
    div = w

if n % div > 0:
    ans = int(n / div) + 1
else:
    ans = int(n / div)

print(ans)
n, k = map(int, input().split())
x = list(map(lambda x: int(x), input().split()))

for i in range(n-k+1):
    r = x[i+k-1]
    l = x[i]
    length = abs(r - l) + min(abs(l), abs(r))
    if length < ans:
        ans = length

print(ans)

### FIXED CODE ###

N, K   = map(int, input().split())
x = list(map(int, input().split()))
 
m = min(b - a + min(abs(a), abs(b)) for a, b in zip(x, x[K-1:]))
print(m)
# 答案
N,Q = map(int,input().split())
S = input()
l,r = 0,0
csum = [0]*(N+1)

# 累積和
for ctr in range(N):
    if S[ctr]=='A' and S[ctr+1]=='C':
        csum[ctr+1] = csum[ctr] + 1
    else:
        csum[ctr+1] = csum[ctr]

# 出力
for ctr in range(Q):
    l,r=map(int,input().split())
    print(csum[r-1] - csum[l-1])

### FIXED CODE ###

# 答案
N,Q = map(int,input().split())
S = input()
l,r = 0,0
csum = [0]*(N+1)

# 累積和
for ctr in range(N):
    if ctr == (N-1):
        csum[ctr+1] = csum[ctr]
    elif S[ctr]=='A' and S[ctr+1]=='C':
        csum[ctr+1] = csum[ctr] + 1
    else:
        csum[ctr+1] = csum[ctr]

# 出力
for ctr in range(Q):
    l,r=map(int,input().split())
    print(csum[r-1] - csum[l-1])
h, w = map(int, input().split())
a = [list(map(int, input().split())) for i in range(h)]
ans = 0
ansl = []
for i in range(h):
    for j in range(w - 1):
        if a[i][j] % 2 == 1 and a[i][j] >= 1:
            a[i][j + 1] += 1
            a[i][j] -= 1
            ans += 1
            ansl.append([i + 1, j + 1, i + 1, j + 2])
for k in range(h):
    if a[k][-1] % 2 == 1:
        a[i + 1][j] += 1
        a[i][j] -= 1
        ans += 1
        ansl.append([i + 1, j + 1, i + 2, j + 1])
print(ans)
for i in ansl:
    i = list(map(str, i))
    print(" ".join(i))

### FIXED CODE ###

h, w = map(int, input().split())
a = [list(map(int, input().split())) for i in range(h)]
ans = 0
ansl = []
for i in range(h):
    for j in range(w):
        if a[i][j] % 2 == 1 and a[i][j] >= 1:
            if j + 1 < w:
                a[i][j + 1] += 1
                a[i][j] -= 1
                ans += 1
                ansl.append([i + 1, j + 1, i + 1, j + 2])
for k in range(h):
    if a[k][-1] % 2 == 1:
        if k + 1 < h:
            a[k + 1][-1] += 1
            a[k][-1] -= 1
            ans += 1
            ansl.append([k + 1, w, k + 2, w])
print(ans)
for i in ansl:
    i = list(map(str, i))
    print(" ".join(i))
s = input()
t = input()

adjusted_t = t[:-1]

if s==adjuested_t:
  print("Yes")
else:
  print("No")
  

### FIXED CODE ###

s = input()
t = input()

adjusted_t = t[:-1]

if s==adjusted_t:
  print("Yes")
else:
  print("No")
S1 = input()
S2 = '2' + S1[0]
res = 0

if len(S1) == 1:
    print(0)

else:
    for s in S1[1::]:
        if S2[-1] == s:
            S2 += s
        else:
            S2 = S2[:-1:]
            res += 2

    print(res)


### FIXED CODE ###

S1 = input()
S2 = ''
res = 0

for s in S1:
    if len(S2) == 0 or S2[-1] == s:
        S2 += s
    else:
        S2 = S2[:-1:]
        res += 2

print(res)
n=int(input())
if n%2==1:
  print(0)
else:
  l=n//2
  k=0
  while l>=5:
    l/=5
    k+=1
  #print(k)
  c=0
  for i in range(1,k+1):
    c+=n//(2*5**i)
  print(c

### FIXED CODE ###

n=int(input())
if n%2==1 or n==0 or n==1:
  print(0)
else:
  l=n//2
  k=0
  while l>=5:
    l/=5
    k+=1
  c=0
  for i in range(1,k+1):
    c+=n//(2*5**i)
  print(c)
#include <bits/stdc++.h>
using namespace std;

int main() {
  long long int n, x, i;
  string result;
  cin >> n >> x;
  vector<string> burgers[51];
  burgers[0] = "P";
  burgers[1] = "BPPPB";
  burgers[2] = "BBPPPBPBPPPBB";
  i = 3;
  while (i<=50) {
    burgers[i] = "B" + burgers[i-1] + "P" + burgers[i-1] + "B";
    i+=1;
  }
  result = burgers[n][:x-1];
  cout << result;
  return 0;
}

/*
nx = input()
nx = nx.split()
n = int(nx[0])
x = int(nx[1])
burgers = [None] * 51
burgers[0] = "P"
burgers[1] = "BPPPB"
burgers[2] = "BBPPPBPBPPPBB"
i = 3
while i<=50:
  burgers[i] = "B" + burgers[i-1] + "P" + burgers[i-1] + "B"
  i+=1
result = burgers[n][:x-1]
print(result.count("P"))
*/

### FIXED CODE ###

def stack(layer, length):
  if layer==1:
    return "BPPPB"[:length].count("P")
  else:
    if length==(lengths[layer-1]+1)/2:
      return ps[layer-2]+1
    elif length<(lengths[layer-1]+1)/2:
      return stack(layer-1, length-1)
    else:
      return ps[layer-2]+1+stack(layer-1, length-lengths[layer-2]-2)
    
inp = input().split()
layer = int(inp[0])
length = int(inp[1])
lengths = [5]
ps = [3]
for i in range(49):
  lengths.append(lengths[i]*2+3)
  ps.append(ps[i]*2+1)
print(stack(layer, length))
def minToggle(arr, n): 
  
    zero =[0 for i in range(n + 1+1)] 
    zero[0] = 0
   
    # Fill entries in zero[] 
    # such that zero[i] 
    # stores count of zeroes 
    # to the left of i 
    # (exl 
    for i in range(1, n + 1): 
      
        # If zero found 
        # update zero[] array 
        if (arr[i-1] == 0): 
            zero[i] = zero[i-1] + 1
        else: 
            zero[i] = zero[i-1] 
   
    # Finding the minimum 
    # toggle required from 
    # every index(0 to n-1) 
    ans = n 
    for i in range(1, n + 1): 
        ans = min(ans, i - zero[i] + zero[n] - zero[i]) 
   
    return ans 
      
# Driver Program 
n=int(input())
s=input().rstrip()
arr=[]
ansi=min(s.count('.'),s.count('#'))
for i in s:
    if i=='.':
        arr.append(0)
    else:
        arr.append(1)
n = len(arr) 
  
p=minToggle(arr, n)) 
ansi=min(ans,p)
print(ans)
  
# This code is contributed 
# by Anant Agarwal. 

### FIXED CODE ###

def minToggle(arr, n): 
  
    zero =[0 for i in range(n + 1+1)] 
    zero[0] = 0
   
    # Fill entries in zero[] 
    # such that zero[i] 
    # stores count of zeroes 
    # to the left of i 
    # (exl 
    for i in range(1, n + 1): 
      
        # If zero found 
        # update zero[] array 
        if (arr[i-1] == 0): 
            zero[i] = zero[i-1] + 1
        else: 
            zero[i] = zero[i-1] 
   
    # Finding the minimum 
    # toggle required from 
    # every index(0 to n-1) 
    ans = n 
    for i in range(1, n + 1): 
        ans = min(ans, i - zero[i] + zero[n] - zero[i]) 
   
    return ans 
      
# Driver Program 
n=int(input())
s=input().rstrip()
arr=[]
ansi=min(s.count('.'),s.count('#'))
for i in s:
    if i=='.':
        arr.append(0)
    else:
        arr.append(1)
n = len(arr) 
  
p=minToggle(arr, n)
ansi=min(ansi,p)
print(ansi)
  
# This code is contributed 
# by Anant Agarwal.
n,m=map(int,input().split())
a=list(map(int,input().split()))
suma=sum(a)
l=suma/(4*m)
a.sort(reverse=True)
if a[m-1]>=l:
    print('Yes')
else:
    print('No')


### FIXED CODE ###

n,k=map(int,input().split())
print(min(n%k,k-n%k))
import sys
N,T =map(int,input().split())
c=[]
t=[]
t_count=0
for i in range(N):
    c_n,t_n=map(int,input().split())
    if t_n<=T:
        c.append(c_n)
        t.append(t_n)
        if i>len(t):
            break
        if t[i]>=T:
            t_count += 1
    

if t_count==N:
    print("TLE")
    sys.exit()

print(min(c))

### FIXED CODE ###

import sys
N,T =map(int,input().split())
c=[]
t=[]
t_count=0
for i in range(N):
    c_n,t_n=map(int,input().split())
    if t_n>T:
        t_count+=1
    if t_n<=T:
        c.append(c_n)
        t.append(t_n)
        #if i>len(t):
            #break
        
    

if t_count==N:
    print("TLE")
    sys.exit()

print(min(c))
n = input()

A = map(int, raw_input().split())

q = input()

m = map(int, raw_input().split())

for i in range(q):
  for j in range(n):
    if A[n-j] < m[i]:
      m[i] = m[i] - A[n-j]
    if m[i] <= 0:
      break
  if m[i] == 0:
    print "yes"
  else:
    print "no"

### FIXED CODE ###

n = input()

A = map(int, raw_input().split())
   
q = input()
   
M = map(int, raw_input().split())
  
def solve(i, m):
  if m == 0:
    return True
  if i >= n or m > sum(A):
    return False
  res = solve(i+1, m) or solve(i+1, m - A[i])
  return res
  
  
for m in M:
  if solve(0,m):
    print "yes"
  else:
    print "no"
import sys
input = sys.stdin.readline

def main():
    n, k = map(int, input().split())
    a = [0]
    ta = list(map(int, input().split()))
    a += ta
    kaiten = [0] * (n + 1)
    count = 0
    koko = 1
    kaitenkikan = 0
    while k > 0:
        koko = a[koko]
        count += 1
        k -= 1
        if kaiten[koko] > 0:
            kaitenkikan = abs(count - kaiten[koko])
            break
        else:
            kaiten[koko] = count
    k_remain = k % kaitenkikan
    if k_remain < 0:
        k_remain += kaitenkikan

    while k_remain > 0:
        koko = a[koko]
        k_remain -= 1

    print(koko)

if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys
input = sys.stdin.readline

def main():
    n, k = map(int, input().split())
    a = [0]
    ta = list(map(int, input().split()))
    a += ta
    kaiten = [0] * (n + 1)
    count = 0
    koko = 1
    kaitenkikan = 0
    while k > 0:
        koko = a[koko]
        count += 1
        k -= 1
        if kaiten[koko] > 0:
            kaitenkikan = abs(count - kaiten[koko])
            break
        else:
            kaiten[koko] = count
    if k == 0:
        print(koko)
        sys.exit()
    k_remain = k % kaitenkikan
    if k_remain < 0:
        k_remain += kaitenkikan

    while k_remain > 0:
        koko = a[koko]
        k_remain -= 1

    print(koko)

if __name__ == '__main__':
    main()
from heapq import*
(N,M),*t=[map(int,s.split())for s in open(0)]
q=[0]*M
v=[[]for _ in q]
z=0
for a,b in t:v[a-1]+=[b]*(a<=M)
for i in v:
	for j in i:heappush(q,-j)
	z-=heappop(q)
print(z)

### FIXED CODE ###

from collections import*
from heapq import*
n,m,*t=map(int,open(0).read().split())
d=defaultdict(list)
for a,b in zip(t[::2],t[1::2]):d[a].append(b)
q=[]
for i in sorted(d.keys())[::-1]:
    for b in sorted(d[i])[::-1][:m-i+1]:
        if len(q)<=m-i:heappush(q,b)
        elif q and q[0]<b:heappushpop(q,b)
print(sum(q))