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

from operator import itemgetter

n = int(input())
lr = [list(map(int, input().split())) + [i] for i in range(n)]

l_sort = sorted(lr, key=itemgetter(0), reverse=True)
r_sort = sorted(lr, key=itemgetter(1))

ans = 0
used = []
now = 0
i = 0
j = 0

if abs(l_sort[0][0]) >= abs(r_sort[0][1]):
	while len(used) != n:
		while l_sort[i][2] in used:
			i += 1
		if not (l_sort[i][0] <= now and now <= l_sort[i][1]):
			ans += abs(now - l_sort[i][0])
			now = l_sort[i][0]
		used.append(l_sort[i][2])
		i += 1
		if len(used) == n:
			break
		while r_sort[j][2] in used:
			j += 1
		if not (r_sort[j][0] <= now and now <= r_sort[j][1]):
			ans += abs(now - r_sort[j][1])
			now = r_sort[j][1]
		used.append(r_sort[j][2])
		j += 1

	print(ans + abs(now))

else:
	while len(used) != n:
		while r_sort[j][2] in used:
			j += 1
		if not (r_sort[j][0] <= now and now <= r_sort[j][1]):
			ans += abs(now - r_sort[j][1])
			now = r_sort[j][1]
		used.append(r_sort[j][2])
		j += 1
		if len(used) == n:
			break
		while l_sort[i][2] in used:
			i += 1
		if not (l_sort[i][0] <= now and now <= l_sort[i][1]):
			ans += abs(now - l_sort[i][0])
			now = l_sort[i][0]
		used.append(l_sort[i][2])
		i += 1
		#print(now)

	print(ans + abs(now))

### FIXED CODE ###

from operator import itemgetter

n = int(input())
lr = [list(map(int, input().split())) for i in range(n)] + [[0, 0]]

l_sort = sorted(lr, key=itemgetter(0), reverse=True)
r_sort = sorted(lr, key=itemgetter(1))

ans = 0
sigma = 0

for i in range(n // 2 + 1):
	sigma += 2 * (l_sort[i][0] - r_sort[i][1])
	if sigma > ans:
		ans = sigma

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

def r(x):
  while u[x]>=0:x=u[x]
  return x
(n,m),_,*e=[map(int,t.split())for t in open(0).readlines()]
u=[-1]*n
a=[n*~-n//2]
for x,y in e[::-1]:
  t=a[-1]
  x,y=r(x-1),r(y-1)
  if x!=y:
    t-=u[x]*u[y]
    u[x]+=u[y]
    u[y]=x
  a.append(t)
print(*a[::-1],sep='\n')

### FIXED CODE ###

class UnionFind:
  def __init__(self,n):
    *self.table,=range(n)
    self.rank=[1]*n
  def root(self,x):
    if self.table[x]==x:
      return x
    else:
      self.table[x]=self.root(self.table[x])
      return self.table[x]
  def unite(self,x,y):
    x=self.root(x)
    y=self.root(y)
    if x!=y:
      if self.rank[x]<self.rank[y]:
        x,y=y,x
      self.rank[x]+=self.rank[y]
      self.table[y]=x
  def same(self,x,y):
    return self.root(x)==self.root(y)
  def size(self,x):
    return self.rank[self.root(x)]
(n,m),_,*e=[map(int,t.split())for t in open(0).readlines()]
u=UnionFind(n)
a=[n*~-n//2]
for x,y in e[::-1]:
  x,y=x-1,y-1
  t=a[-1]
  if not u.same(x,y):
    t-=u.size(x)*u.size(y)
  a.append(t)
  u.unite(x,y)
print(*a[::-1],sep='\n')
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**7)

n, q = map(int, input().split())
g = [list() for i in range(n)]
node = [0] * n

for i in range(n-1):
    a, b = map(int, input().split())
    a -= 1; b -= 1
    g[a].append(b)
    g[b].append(a)

for i in range(q):
    p, x = map(int, input().split())
    node[p-1] += x

def dfs(v, p=-1):
    for u in g[v]:
        if u == p: continue
        node[u] += node[v]
        dfs(u, v)

dfs(0)
print(*node)

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

n, q = map(int, input().split())
g = [list() for i in range(n)]
node = [0] * n

for i in range(n-1):
    a, b = map(int, input().split())
    a -= 1; b -= 1
    g[a].append(b)
    g[b].append(a)

for i in range(q):
    p, x = map(int, input().split())
    node[p-1] += x

def dfs(v, p=-1):
    for u in g[v]:
        if u == p: continue
        node[u] += node[v]
        dfs(u, v)

dfs(0)
print(*node)
### BUGGY CODE ###

def gcd(x,y):
  return (y if x%y==0 else gcd(y,x%y))
def lcd(x,y):
  return x*y//gcd(x,y)
N=int(input())
A=list(map(int,input().split()))
l=1
for a in A:
  l=lcd(l,a%1000000007)
ans=0
for a in A:
  ans=(ans+l//a)
print(ans%1000000007)

### FIXED CODE ###

P=1000000007
def power(x,p):
  if(p==0):
    return 1;
  if(p%2):
    return power(x,p-1)*x%P;
  a=power(x,p//2);
  return a*a%P;
def inv(x):
  return power(x,P-2);
N=int(input());A=list(map(int,input().split()));
#N=10000;A=[1000000 for i in range(N)];
L=[0 for i in range(max(A)+1)]
for a in A:
  t=a;i=2;
  while(i**2<=a):
    c=0;
    while(t%i==0):
      t//=i;c+=1;
    L[i]=max(L[i],c)
    i+=1;
  if(t!=1):
    L[t]=max(L[t],1)
    
l=1;
for i in range(2,len(L)):
  l=l*power(i,L[i])%P;
b=0;
for a in A:
  b=(b+inv(a))%P
print(l*b%P)
### BUGGY CODE ###

L, R = map(int, input().split())
print(min([((i*j)%2019) for i in range(L,R) for j in range(i+1,R+1)]))

### FIXED CODE ###

L, R = map(int, input().split())
if R-L>=2019:
    print(0)
else:
    print(min([((i*j)%2019) for i in range(L,R) for j in range(i+1,R+1)]))
### BUGGY CODE ###

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

cnt = 0

for x in range(k+1):
    for y in range(k+1):
        for z in range(k+1):
            if s==x+y+z:
                cnt=cnt+1
            else:
                continue
    
print(cnt)
    

### FIXED CODE ###

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

cnt = 0

for x in range(k+1):
    for y in range(k+1):
        z=s-x-y
        if 0<=z<=k:
            cnt=cnt+1
        else:
            continue
    
print(cnt)
### BUGGY CODE ###

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

for i in range(N):
  if A.count(A[i]) > 1:
    print("NO")
    exit(0)
print("YES")

### FIXED CODE ###

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

A=sorted(A)

for i in range(0,N):
  if len(A) > i+1:
    if A[i] == A[i+1]:
      print("NO")
      exit(0)
print("YES")
### BUGGY CODE ###

N,M = map(int,input().split())
A = [0]*M
B = [0]*M
for i in range(M):
    A[i],B[i] = map(lambda x:int(x)-1,input().split())

def check_connect(connect,A,B):
    connect = connect.copy()
    a = -1
    b = -1
    for i,s in enumerate(connect):
        if A in s:
            a = i
        if B in s:
            b = i
        if a>=0 and b>=0:
            break
    if a == b:
        return connect,0
    tmp1 = len(connect[a])
    tmp2 = len(connect[b])
    connect[a] = connect[a] | connect[b]
    tmp3 = len(connect[a])
    connect.pop(b)
    return connect,(tmp3*(tmp3-1)-tmp1*(tmp1-1)-tmp2*(tmp2-1))//2

connect = [[] for _ in range(M+1)]
connect[M] = [{i}for i in range(N)]
dfubensa = [0]*M
for i in range(M-1,-1,-1):
    connect[i],dfubensa[i] = check_connect(connect[i+1],A[i],B[i])

fubensa = [dfubensa[0]]*M
print(fubensa[0])
for i in range(1,M):
    fubensa[i] = fubensa[i-1]+ dfubensa[i]
    print(fubensa[i])

### FIXED CODE ###

N,M = map(int,input().split())
A = [0]*M
B = [0]*M
for i in range(M):
    A[i],B[i] = map(lambda x:int(x)-1,input().split())

def check_connect(A,B):
    a = cnt[A]
    b = cnt[B]
    if a==-1 and b==-1:
        connect.append([A,B])
        cnt[A] = len(connect)-1
        cnt[B] = cnt[A]
        return 1
    if a == b:
        return 0
    if a == -1:
        a,b,A,B = b,a,B,A
    if b==-1:
        connect[a] += [B]
        cnt[B] = a
        return len(connect[a])-1
        
    tmp1 = len(connect[a])
    tmp2 = len(connect[b])
    if tmp2 > tmp1:
        a,b = b,a
    connect[a] += connect[b]
    for i in connect[b]:
        cnt[i] = a
    tmp3 = tmp1+tmp2
    connect[b] = []
    return (tmp3*(tmp3-1)-tmp1*(tmp1-1)-tmp2*(tmp2-1))//2

connect = []
cnt = [-1]*N
dfubensa = [0]*M
for i in range(M-1,-1,-1):
    dfubensa[i] = check_connect(A[i],B[i])

fubensa = dfubensa[0]
print(fubensa)
for i in range(1,M):
    fubensa += dfubensa[i]
    print(fubensa)
### BUGGY CODE ###

def parent(n):
    return (n+1)/2-1

def left(n):
    return (n+1)*2-1

def right(n):
    return (n+1)*2

def max_heapify(A, i):
    global pt
    l = left(i)
    r = right(i)
    largest = i
    if l <= pt and A[largest] < A[l]:
        largest = l
    if r <= pt and A[largest] < A[r]:
        largest = r

    if largest != i:
        A[largest],A[i] = A[i],A[largest]
        max_heapify(A,largest)

def build_max_heap(A):
    for i in reversed(xrange((len(A)-1)/2+1)):
        max_heapify(A,i)

def up_heap(A,i):
    p = parent(i)
    while p >= 0 and A[p] < A[i]:
        A[p],A[i] = A[i],A[p]
        i = p
        p = parent(i)

def extract_max(A):
    global pt
    ret = A[0]
    A[0] = A[pt]
    pt -= 1
    max_heapify(A,0)
    return ret


heap = [0]*2000000
pt = -1
while True:
    c = raw_input()
    if c[0] == 'i':
        n = int(c[7:])
        pt += 1
        heap[pt] = n
        up_heap(heap,pt)
    elif c[:2] == 'ex':
        print extract_max(heap)
    else:
        break

### FIXED CODE ###

from sys import stdin

def max_heapify(A, i):
    l = i*2
    r = i*2+1
    largest = i
    if l <= A[0] and A[i] < A[l]:
        largest = l
    else:
        largest = i
    if r <= A[0] and A[largest] < A[r]:
        largest = r

    if largest != i:
        A[largest],A[i] = A[i],A[largest]
        max_heapify(A,largest)

heap = [0]*2000000
for c in stdin:
    if c[0] == 'i':
        heap[0] += 1
        heap[heap[0]] = int(c[7:])
        i = heap[0]
        p = i/2
        while p > 0 and heap[p] < heap[i]:
            heap[p],heap[i] = heap[i],heap[p]
            i = p
            p = i/2
    elif c[1] == 'x':
        print heap[1]
        heap[1] = heap[heap[0]]
        heap[0] -= 1
        max_heapify(heap,1)
    else:
        break
### BUGGY CODE ###

n,k=map(int,input().split())
red = n-k
mod=10**9+7

for i in range(1,200320032003):
  a=4
nCk =[[0]*2005 for _ in range(2005)]
for i in range(2003):
    nCk[i][0] = 1

for i in range(1,2003):
    for j in range(1,i+1):
        nCk[i][j] = nCk[i-1][j]+nCk[i-1][j-1]

for i in range(1,k+1):
    print(nCk[k-1][i-1]*nCk[red+1][i]%mod)


### FIXED CODE ###

n,k=map(int,input().split())
red = n-k
mod=10**9+7

nCk =[[0]*2005 for _ in range(2005)]
for i in range(2003):
    nCk[i][0] = 1

for i in range(1,2003):
    for j in range(1,i+1):
        nCk[i][j] = nCk[i-1][j]+nCk[i-1][j-1]

for i in range(1,k+1):
    print(nCk[k-1][i-1]*nCk[red+1][i]%mod)
### BUGGY CODE ###

n = int(input())
nlist = list(map(int, input().split(" ")))

count = 0
for i, m in enumerate(nlist):
  count += max(nlist[:i+1])-m
  
print(count)


### FIXED CODE ###

n = int(input())
nlist = list(map(int, input().split(" ")))

count = 0
for i in range(n-1):
  if nlist[i]>nlist[i+1]:
    count += nlist[i]-nlist[i+1]
    nlist[i+1] = nlist[i]
print(count)
### BUGGY CODE ###

# ABC_129 C

def stairs(n, a):
    dp = [0 for _ in range(n+1)]
    dp[0] = 1
    if 1 not in a:
        dp[1] = 1
    for i in range(2, n+1):
        if i in a:
            continue
        else:
            dp[i] = dp[i-1]
            dp[i] += dp[i-2]

    result = dp[n] % 1000000007

    print(result)


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

if __name__ == '__main__':
    stairs(n, a)

### FIXED CODE ###

# ABC_129 C

def stairs(n, a):
    num_list = set(i for i in range(n+1) if i not in a)
    dp = [0 for _ in range(n+1)]
    dp[0] = 1
    if 1 not in a:
        dp[1] = 1
    for i in num_list:
        if i > 1:
            dp[i] = dp[i-1] + dp[i-2]
            
    result = dp[n] % 1000000007

    print(result)

    
n, m = list(map(int, input().split()))
a = set(int(input()) for _ in range(m))


if __name__ == '__main__':
    stairs(n, a)
### BUGGY CODE ###

n=int(input())
s=input()

r=s.count('R')
g=s.count('G')
b=s.count('B')

ans=r*g*b

for i in range(n):
    for d in range(n):
        j=i+d
        k=j+d
        if k<n:
            if s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
                ans-=1

print(ans)


### FIXED CODE ###

n=int(input())
s=input()

r_cnt=s.count('R')
g_cnt=s.count('G')
b_cnt=s.count('B')

ans=r_cnt*g_cnt*b_cnt

for i in range(n):
    for d in range(n):
        j=i+d
        k=j+d
        if k>=n:break
        if s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
            ans-=1
            
print(ans)
### BUGGY CODE ###

from collections import defaultdict

N, M = map(int, input().split())
S = list(map(int, input().split()))
T = list(map(int, input().split()))
mod = 10 ** 9 + 7
sum_dp = defaultdict(int)

for i in range(N):
    for j in range(M):
        temp = 0
        if(S[i] == T[j]):
            temp = sum_dp[(i-1, j-1)] + 1
        sum_dp[(i, j)] = sum_dp[(i-1, j)] + sum_dp[(i, j-1)] - sum_dp[(i-1, j-1)] + temp
        sum_dp[(i, j)] %= mod

print(sum_dp[(N-1, M-1)] + 1)


### FIXED CODE ###

from collections import defaultdict

N, M = map(int, input().split())
S = [0] + list(map(int, input().split()))
T = [0] + list(map(int, input().split()))
mod = 10 ** 9 + 7
sum_dp = []
for i in range(N+1):
    sum_dp.append([0] * (M+1))

temp = 0
for i in range(1, N+1):
    for j in range(1, M+1):
        temp = 0
        if(S[i] == T[j]):
            temp = sum_dp[i-1][j-1] + 1
        sum_dp[i][j] = (sum_dp[i-1][j] + sum_dp[i][j-1] - sum_dp[i-1][j-1] + temp) % mod

print(sum_dp[N][M] + 1)
### BUGGY CODE ###

s = input()
q = int(input())
flag = 1
for i in range(q):
    query = input().split()
    if query[0] == '1':
        flag *= -1
    elif query[1] == '1':
        if flag == 1:
            s = query[2] + s
        else:
            s = s + query[2]
    elif query[1] == '2':
        if flag == -1:
            s = query[2] + s
        else:
            s = s + query[2]
if flag == 1:
    print(s)
else:
    print(s[::-1])

### FIXED CODE ###

from collections import deque

s = deque(input())
q = int(input())
flag = 1
for i in range(q):
    query = input().split()
    if query[0] == '1':
        flag *= -1
    elif query[1] == '1':
        if flag == 1:
            s.appendleft(query[2])
        else:
            s.append(query[2])
    elif query[1] == '2':
        if flag == -1:
            s.appendleft(query[2])
        else:
            s.append(query[2])
if flag == 1:
    print(''.join(s))
else:
    print(''.join(list(s)[::-1]))
### BUGGY CODE ###

r, g, b, n = map(int,input().split())

ans = 0
for i in range(n//r+1):
    for j in range(n//g+1):
        bule_ball = (n - (r * i + g * j)) // b
        sum_ball = bule_ball * b + i * r + j * g
        #print(i,j,bule_ball)
        if bule_ball >= 0 and sum_ball == n:
            ans += 1

print(ans)

### FIXED CODE ###

r, g, b, n = map(int,input().split())

ans = 0
for i in range(n//r+1):
    for j in range(n//g+1):
        bule_ball = (n - (r * i + g * j)) // b
        sum_ball = bule_ball * b + i * r + j * g
        #print(i,j,bule_ball)
        if bule_ball >= 0 and sum_ball == n:
            ans += 1

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

import sys
import collections
ri = lambda: int(sys.stdin.readline())
rl = lambda: list(map(int, sys.stdin.readline().split()))

n = ri()
a = rl()
c = collections.Counter(a)
for i in a:
    ans = 0
    for k, v in c.items():
        ci = v
        if k == i:
            ci = max(0, v-1)
        ans += ci*(ci-1)//2
    print(ans)

### FIXED CODE ###

import sys
import collections
ri = lambda: int(sys.stdin.readline())
rl = lambda: list(map(int, sys.stdin.readline().split()))

n = ri()
a = rl()
c = collections.Counter(a)
t = 0
for v in c.values():
    t += v*(v-1)//2

for i in a:
    print(t-c[i]+1)
### BUGGY CODE ###

MAX = 10 ** 18

N = int(input())
liste = list(map(int, input().split()))
res = 1 


for i in range(N):
    #if(res/2 > MAX):
    #    print(-1)
    #    exit(0)
    res *= liste[i]

if(res > MAX):
    print(-1)
else: 
    print(res)


### FIXED CODE ###

MAX = 10 ** 18

N = int(input())
liste = list(map(int, input().split()))
res = 1 
for i in range(N):
    if(liste[i] == 0):
        print(0)
        exit(0)

for i in range(N):
    res *= liste[i]
    #print(res, liste[i])
    if(res > MAX):
        print(-1)
        exit(0)

if(res > MAX):
    print(-1)
else: 
    print(res)
### BUGGY CODE ###

#def make_divisors(n):
#    lower_divisors , upper_divisors = [], []
#    i = 1
#    while i*i <= n:
#        if n % i == 0:
#            lower_divisors.append(i)
#            if i != n // i:
#                upper_divisors.append(n//i)
#        i += 1
#    return lower_divisors + upper_divisors[::-1]
n=int(input())
#li=[]
#f=[len(make_divisors(i)) for i in range(1,n+1)]
#for i in range(1,n+1):
#    li.append(i*f[i-1])
#print(sum(li))

#L=[n//i*(i+n//i*i)//2 for i in range(1,n+1)]
#print(L)
#print(sum(L))
ans=0
for i in range(1,n+1):
    for j in range(1,n+1):
        if i%j==0:
            ans+=i
print(ans)


### FIXED CODE ###

n=int(input())

#L=[n//i*(i+n//i*i)//2 for i in range(1,n+1)]
#print(sum(L))
ans=0
#for i in range(1,n+1):
#    for j in range(1,n+1):
#        if i%j==0:
#            ans+=i
#print(ans)
g=lambda x:(x*(n//x)*(n//x+1))//2
print(sum(g(i) for i in range(1,n+1)))
### BUGGY CODE ###

def solve():
    N = int(raw_input())
    D = [int(raw_input()) for i in range(N)]
    if sum(D) % 2 > 0:
        print '-1'
        exit()
    Di = sorted([(di, i) for i, di in enumerate(D)], key = lambda x : x[0], reverse = True)
    d_to_i = {dd:i for dd, i in Di}
    # child = [[] for i in range(N)]
    ans = []
    n_child = [1] * N
    d_child = [0] * N
    for valD, node in Di:
        valD_par = valD - N + 2 * n_child[node]
        if valD_par in d_to_i.keys():
            node_par = d_to_i[valD_par]
            # child[node].append(node_par) ##
            # child[node_par].append(node)
            ans.append((node_par + 1, node + 1))
            n_child[node_par] += n_child[node]
            d_child[node_par] += n_child[node] + d_child[node]
            if n_child[node_par] == N:
                break
        else:
            print '-1'
            exit()

    # check if Di satisfied or not
    d_min, i_min = Di[-1]
    if d_child[i_min] != d_min:
        print '-1'
        exit()
    # for i in range(N):
    #     for j in child[i]:
    #         print str(i + 1) + ' ' + str(j + 1)
    for i,j in ans:
        print str(i) + ' ' + str(j)

if __name__ == '__main__':
    solve()


### FIXED CODE ###

n = int(input())
d = [int(input()) for i in range(n)]

if sum(d)%2 > 0:
    print(-1)
    exit()

d = sorted([(dd, i) for i, dd in enumerate(d)], reverse = True)
d_to_i = {dd:i for dd, i in d}
n_child = [1]*n
d_child = [0]*n
ans = []
for dd, i in d:
    d_i = dd+2*n_child[i]-n
    if d_i in d_to_i.keys():
        i_next = d_to_i[d_i]
        ans.append((i+1, i_next+1))
        n_child[i_next] += n_child[i]
        d_child[i_next] += d_child[i] + n_child[i]
        if n_child[i_next] == n:
            break
    else:
        print(-1)
        exit()

d_min, i_min = d[-1]
if d_min == d_child[i_min]:
    for a in ans:
        print(*a)
else:
     print(-1)
### BUGGY CODE ###

import sys
input = sys.stdin.readline
N,M=map(int, input().split())
L=[ [] for _ in range(N)]
X=[]
for _ in range(M):
    a,b = map(int,input().split())
    L[a-1].append(b)
    X.append({a:b})
Y=[ (sorted(L[_])) for _ in range(N) ]
for i in range(M) :
    for k,v in X[i].items():
        print('%06d%06d' % (k,(Y[k-1].index(v)+1)))

### FIXED CODE ###

N,M = map(int,input().split())
L = []
for i in range(M):
    l = list(map(int,input().split()))+[i]
    L.append(l)
cnt = 0
tmp = []
M = sorted(L,key = lambda x: (x[0],x[1]))
for x in M:
    if tmp == x[0]:
        x.append(cnt+1)
        cnt += 1
    else:
        tmp = x[0]
        cnt = 0
        x.append(cnt)
M = sorted(L,key = lambda x:x[2])
#print(M)
for x in M:
    st = str(x[0]).zfill(6)
    st += str(x[3]+1).zfill(6)
    print(st)
### BUGGY CODE ###

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

s = A[0]
for i in range(1, n):
  s *=A[i]

if s <= 1000000000000000000:
  print(s)
elif s > 1000000000000000000:
  print(-1)

### FIXED CODE ###

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

  if 0 in A:
    print(0)
    return

  p = 1
  for a in A:
    p *= a
    if p > 10 ** 18:
      print(-1)
      return

  print(p)
main()
### BUGGY CODE ###

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

s = input()

cnt = []

for i in range(1,len(s)+1):
    temp = s[0:i]
    #print(temp)
    cnt.append(temp.count('AC'))

#print(cnt)
for i in range(q):
    l, r  = map(int, input().split())
    print(cnt[r - 1] - cnt[l - 1])

### FIXED CODE ###

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

s = input()

cnt = [0]

for i in range(1,len(s)+1):
    temp = s[i-1:i+1]
    # print(temp)
    if temp == 'AC':
        cnt.append(cnt[i-1] + 1)
    else:
        cnt.append(cnt[i-1])

# print(cnt)
for i in range(q):
    l, r  = map(int, input().split())
    print(cnt[r - 1] - cnt[l - 1])
### BUGGY CODE ###

N, K  = map(int, input().split())
As = sorted(list(map(int, input().split())))
Fs = sorted(list(map(int, input().split())),reverse=True)
Xs = sorted([(a*f,i) for i,(a,f) in enumerate(zip(As,Fs))],reverse=True)
# Xs =[a*f for a,f in zip(As,Fs)]
# print(Xs)
# print(As)
# print(Fs)
import heapq
q = []
INF = 10**6
heapq.heapify(q)
for i,(a,f) in enumerate(zip(As,Fs)):
    num = -(a*f*INF+i)
    heapq.heappush(q,num)
if K >= sum(As):
    print(0)
    exit()
for k in range(K):
    num = heapq.heappop(q)
    i = (-num)%INF
    n = (-num)//INF
    # print(i,n,As[i],Fs[i])
    As[i] -= 1
    upd = -(As[i]*Fs[i]*INF+i)
    heapq.heappush(q,upd)
num = heapq.heappop(q)
ans = (-num)//INF
print(ans)


# x_min = min(Xs)
# ns = [max(0,A-x_min//F) for A,F in zip(As,Fs)]
# print(ns)


### FIXED CODE ###

N, K  = map(int, input().split())
As = sorted(list(map(int, input().split())))
Fs = sorted(list(map(int, input().split())),reverse=True)
Xs = [a*f for a,f in zip(As,Fs)]
X_M = max(Xs)

def f(x):
    K_tmp = sum([max(0,a-x//f) for a,f in zip(As,Fs)])
    return K_tmp <= K

def  binarySearch(l,r,target):
    while r - l > 1:
        mid = (l+r)//2
        """計算を入れる"""
        # print(mid, guess)
        if f(mid):
            r = mid
        else:
            l = mid
    return r

print(binarySearch(-1,X_M,K))
### BUGGY CODE ###

N = int(input())
L = []
for i in range(N) :
    L.append(int(input()))

A = []
for i in range(N+1) :
    if i == 0 :
        button = 1
        ans = 0
    elif button == 2 :
        break
    else :
        button = L[button-1]
        if button in A :
            ans = -1
            break
        else :
            A.append(button)
            ans += 1
print(ans)   


### FIXED CODE ###

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

flag = 0
now = 0
ans = 0
for i in range(N+1) :
    if now == 1 :
        flag = 1
        break
    else :
        now = L[now]-1
        ans += 1
if flag == 1 :
    print(ans)
else :
    print(-1)