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

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

"""
ans = 1
for x in range(N):
    ans *= A[x]
"""
ans = 1
for x in A:
    ans *= x

if ans > 1.0e+18:
    print(-1)
else:
    print(ans)



### FIXED CODE ###

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

"""
ans = 1
for x in range(N):
    ans *= A[x]
"""
A = sorted(A)

ans = 1
for x in A:
    ans *= x
    if ans == 0 or ans > 1.0e+18:
        break


if ans > 1.0e+18:
    print(-1)
else:
    print(ans)
### BUGGY CODE ###

import numpy as np
n = list(map(int,input().split()))
a = list(map(int,input().split()))
a = np.array(a)

for i in range(n[1]):
    a[a.argmax()] = a[a.argmax()]//2 
print(np.sum(a))


### FIXED CODE ###


N, M = map(int,input().split())
A = list(-int(i) for i in input().split())
import heapq

heapq.heapify(A)
for _ in range(M):
    a = heapq.heappop(A)//(-2)
    heapq.heappush(A,-a)

print(-sum(A))
### BUGGY CODE ###

import sys
input = sys.stdin.readline
n=int(input())
a=list(map(int,input().split()))

A=a.count(1)
B=a.count(2)
C=a.count(3)

dp=[[[0]*(n+1) for _ in range(n+1)] for _ in range(n+1)]

for k in range(C+1):
    for j in range(B+C+1-k):
        for i in range(n+1-k-j):
            if i==0 and j==0 and k==0:
                continue
            res=n
            if i>0:
                res+=i*dp[i-1][j][k]
            if j>0:
                res+=j*dp[i+1][j-1][k]
            if k>0:
                res+=k*dp[i][j+1][k-1]
            dp[i][j][k]=res/(i+j+k)
print(dp[A][B][C])

### FIXED CODE ###

import sys
input = sys.stdin.readline
n=int(input())
a=list(map(int,input().split()))

A=a.count(1)
B=a.count(2)
C=a.count(3)

dp=[[[0.0]*(n+1) for _ in range(n+1)] for _ in range(n+1)]
def c():
    for k in range(C+1):
        for j in range(B+C+1-k):
            for i in range(n+1-k-j):
                if i==0 and j==0 and k==0:
                    continue
                res=1.0*n
                if i>0:
                    res+=i*dp[i-1][j][k]
                if j>0:
                    res+=j*dp[i+1][j-1][k]
                if k>0:
                    res+=k*dp[i][j+1][k-1]
                dp[i][j][k]=res/(i+j+k)
    print(dp[A][B][C])
c()
### BUGGY CODE ###

N = int(input())
def Lucas(n):
    if n == 0:
        return 2
    elif n == 1:
        return 1
    else:
        return Lucas(n-1)+Lucas(n-2)
print(Lucas(N))

### FIXED CODE ###

from functools import lru_cache
N = int(input())
@lru_cache(maxsize=1000)
def luca(n):
    if n == 0:
        return 2
    elif n == 1:
        return 1
    else:
        return luca(n-1)+luca(n-2)
print(luca(N))
### BUGGY CODE ###

n,k = map(int,input().split())
p = list(map(int,input().split()))
walist=[]

for i in range(0,n-k+1):
    wa = 0
    for j in range(k):
        wa += p[i+j]
    walist += [wa]
print(max(walist)/2 + k/2)


### FIXED CODE ###

n,k = map(int,input().split())
p = list(map(int,input().split()))

pp = list(map(lambda x:(x+1)/2,p))

p_rui = [pp[0]]
for i in range(1,n):
    p_rui.append(p_rui[i-1]+pp[i])



walist=[p_rui[k-1]]
for i in range(0,n-k):
    walist += [p_rui[k+i]-p_rui[i]]

print(max(walist))
### BUGGY CODE ###

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

res = list()
for rank in range(1,n+1):
    for i,v in enumerate(a):
        if v == rank:
            res.append(i+1)

res2 = [str(k) for k in res]
res2 = " ".join(res2)
print(res2)

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
ans = [0] * N
for i, a in enumerate(A):
    ans[a-1] = i+1
print(*ans)
### BUGGY CODE ###

#dt = {} for i in x: dt[i] = dt.get(i,0)+1
#dt = {k:v for k,v in sorted(x.items(), key=lambda i: i[1])}
ipnl = lambda n: [int(input()) for _ in range(n)]
inp = lambda :int(input())
ip = lambda :[int(w) for w in input().split()]

n,m = ip()
graph = {i:[] for i in range(1,n+1)}
for i in range(m):
    a,b = ip()
    graph[a].append(b)
    graph[b].append(a)

start = 1
nodes = list(graph.keys())
q = set()
d,parent,vis = {},{},{}
for i in nodes:
    d[i] = float('inf')
    parent[i] = -1
    q.add(i)
d[start] = 0

while len(q):
    mn = float('inf')
    for v in list(q):
        if d[v] < mn:
            mn = d[v]
            u = v
    q.remove(u)
    for v in graph[u]:
        if d[u]+1 < d[v] and v in q:
            d[v] = d[u] + 1
            parent[v] = u

if float('inf') in list(d.values()):
    print("No")
    exit()
print("Yes")
for i in range(2,n+1):
    print(parent[i])







### FIXED CODE ###

#dt = {} for i in x: dt[i] = dt.get(i,0)+1
#dt = {k:v for k,v in sorted(x.items(), key=lambda i: i[1])}
ipnl = lambda n: [int(input()) for _ in range(n)]
inp = lambda :int(input())
ip = lambda :[int(w) for w in input().split()]

n,m = ip()
graph = {i:[] for i in range(1,n+1)}
for i in range(m):
    a,b = ip()
    graph[a].append(b)
    graph[b].append(a)

parent = {i:-1 for i in range(1,n+1)}
parent[1] = 0
q = [1]
while len(q):
    u = q.pop(0)
    for v in graph[u]:
        if parent[v] == -1:
            q.append(v)
            parent[v] = u

if -1 in list(parent.values()):
    print("No")
    exit()
print("Yes")
for i in range(2,n+1):
    print(parent[i])
### BUGGY CODE ###

x, y, z, k = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
b = list(map(int, input().split(' ')))
c = list(map(int, input().split(' ')))
taste = []
tastexy = []
for i in set(range(x - k if x - k > 0 else 0, x)):
    for j in set(range(y - k if y - k > 0 else 0, y)):
        tastexy.append(a[i] + b[j])
tastexy.sort()
tindex = len(tastexy)-k if len(tastexy)-k > 0 else 0
tastez = tastexy[tindex:]
for t in tastez:
    for l in set(range(z - k if z - k > 0 else 0, z)):
        taste.append(t + c[l])
taste.sort()
for i in range(k):
    print(taste[-1-i])

### FIXED CODE ###

x,y,z,K=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
ans=[]
for i in range(x):
  for j in range(y):
    for k in range(z):
      if (i+1)*(j+1)*(k+1)<=K:
        ans.append(a[i]+b[j]+c[k])
      else:
        break
ans.sort(reverse=True)
for i in range(K):
  print(ans[i])
### BUGGY CODE ###

N = int(input())
S = list(input())
f=0
ijk = list()
for i in range(N):
    for j in range(N):
        for k in range(N):
            if(i<j<k):
                if(S[i]==S[j]):
                    pass
                elif(S[i]==S[k]):
                    pass
                elif(S[j]==S[k]):
                    pass
                else:
                    ijk.append([i+1,j+1,k+1])
# print(ijk)
for i in range(len(ijk)):
    if(ijk[i][1]-ijk[i][0] != ijk[i][2]-ijk[i][1]):
        f+=1
print(f)

### FIXED CODE ###

n=int(input())
s=input()
r=g=b=0
for i in s:
  if i=="R":r+=1
  elif i=="G":g+=1
  else:b+=1

ans=r*g*b
for i in range(n):
  for j in range(i+1,n):
    k_ = j+(j-i)
    if(k_ >=n):
      break
    ans -= int(s[i]!= s[j] and s[i] != s[k_] and s[j] != s[k_])

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

# 解説を見ながら
N = int(input())
A = list(map(int,input().split()))

# リストを作って，Amaxまでの数字について，aiの約数ならば足す，最終的には，要素が１のやつが求めるもの
# 一回検討した要素はsetに入れて次出たときは使わないようにする
import numpy as np
#L = np.zeros((10**6+10,))
MAX=10**6+1
L = [0]*(10**6+1)
used = set()
first = True
for a in A:
    if L[a] ==2:
        continue
    if a in used and first:
        first = False
        for i in range(0,MAX,a):
            L[i] += 1
        # L[::a] += 1　要素アクセスだから遅くなった?
        continue
    used.add(a)
    # L[1:][::a] += 1こういう書き方はリストではできないので注意
    for i in range(0,MAX,a):
        L[i] += 1
    #L[::a] += 1
count = 0
for a in A:
    if L[a] == 1:
        count += 1
print(count)







### FIXED CODE ###

# 解説を見ながら
N = int(input())
A = list(map(int,input().split()))

# リストを作って，Amaxまでの数字について，aiの約数ならば足す，最終的には，要素が１のやつが求めるもの
# 一回検討した要素はsetに入れて次出たときは使わないようにする
import numpy as np
#L = np.zeros((10**6+10,))
A.sort()
MAX=10**6+1
L = [0]*MAX
used = set()
#first = True  # firstのフラッグの使い回しをしていた．複数に分かれる条件分岐はしっかりとどのような分岐をするか書く癖をつけたほうがよい
for a in A:
    if L[a] == 2:
        continue
    if a in used:
        for i in range(0,MAX,a):
            L[i] = 2
        # L[::a] += 1　要素アクセスだから遅くなった?
        continue
    used.add(a)
    # L[1:][::a] += 1こういう書き方はリストではできないので注意
    L[a] = 1
    for i in range(2*a,MAX,a):
        L[i] = 2
    #L[::a] += 1
count = 0
for a in A:
    if L[a] == 1:
        count += 1
print(count)
### BUGGY CODE ###

N,M=map(int, input().split())
p=[]
for x in range(M):
    p.append(input())
A=[]
B=[]
for x in range(M):
    a,b=map(int,p[x].split())
    A.append(a)
    B.append(b)

f=0
for x in range(M):
    if A[x]==1:
        if "%s %s" % (B[x], N) in p:
            print("POSSIBLE")
        else:
            f=f+1
    else:
        f=f+1
if f==M:
    print("IMPOSSIBLE")
    


### FIXED CODE ###

N,M=map(int, input().split())
p=[]
for x in range(M):
    p.append(input())
A=[]
B=[]
for x in range(M):
    a,b=map(int,p[x].split())
    A.append(a)
    B.append(b)
    
isl=[]
for x in range(N):
    isl.append(0)
for x in range(M):
    if A[x]==1:
        isl[B[x]]=1
for x in range(M):
    if B[x]==N:
        isl[A[x]]=isl[A[x]]+1

if 2 in isl:
    print("POSSIBLE")
else:
    print("IMPOSSIBLE")
### BUGGY CODE ###

n = int(input())
a = list(map(lambda x: int(x),input().split()))
b = list(map(lambda x: int(x),input().split()))
c = list(map(lambda x: int(x),input().split()))

count = 0
for mid in b:
    upper_count = len(list(filter(lambda x: x<mid, a)))
    lower_count = len(list(filter(lambda x: x>mid, c)))
    count += upper_count * lower_count
print(count)

### FIXED CODE ###

import bisect

n = int(input())
a = sorted(list(map(lambda x: int(x),input().split())))
b = sorted(list(map(lambda x: int(x),input().split())))
c = sorted(list(map(lambda x: int(x),input().split())))

count = 0
for mid in b:
    upper_count = bisect.bisect_left(a,mid)
    lower_count = len(c) - bisect.bisect_right(c,mid)
    count += upper_count * lower_count
print(count)
### BUGGY CODE ###

N,K = list(map(int,input().split()))
ans = 0
for i in range(N):
  a = i
  b = 1
  while a<K:
    a = a*2
    b = b*0.5
  ans += b*1/N
print(ans)
  
  

### FIXED CODE ###

N,K = list(map(int,input().split()))
ans = 0
for i in range(N):
  a = i+1
  b = 1
  while a<K:
    a = a*2
    b = b*0.5
  ans += b*1/N
print(ans)
### BUGGY CODE ###

K = int(input())
V = []
for i in range(1, 150):
    for j in range(15):
        val = str(i) + '9' * j
        V.append(int(val))
V = list(set(V))
V.sort()
# print(V)


# メモ化再起
from functools import lru_cache
@lru_cache(maxsize=10000000)
def f(n):
    s = sum([int(x) for x in list(str(n))])
    return n / s

ret = []
for n in range(len(V)):
    isOK = True
    for m in range(n + 1, len(V)):
        if f(V[n]) > f(V[m]):
            isOK = False
            break
    if isOK:
        ret.append(str(V[n]))

print(" ".join(ret[:K]))


### FIXED CODE ###

K = int(input())
V = []
for i in range(1, 150):
    for j in range(15):
        val = str(i) + '9' * j
        V.append(int(val))
V = list(set(V))
V.sort()
# print(V)


# メモ化再起
from functools import lru_cache
@lru_cache(maxsize=10000000)
def f(n):
    s = sum([int(x) for x in list(str(n))])
    return n / s

ret = []
for n in range(len(V)):
    isOK = True
    for m in range(n + 1, len(V)):
        if f(V[n]) > f(V[m]):
            isOK = False
            break
    if isOK:
        ret.append(str(V[n]))

print(" ".join(ret[:K]))
### BUGGY CODE ###

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

border = q-k+1
for i in range(1, n+1):
    if a.count(i) >= border:
        print('Yes')
    else:
        print('No')


### FIXED CODE ###

import sys
from collections import defaultdict
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

n, k, q = map(int, input().split())
dd = defaultdict(int)
for _ in range(q):
    dd[int(input())] += 1

border = q-k+1
for i in range(1, n+1):
    if dd[i] >= border:
        print('Yes')
    else:
        print('No')
### BUGGY CODE ###

n,k=map(int,input().split())
l=list(map(int,input().split()))
dp=[0]*n
dp[0]=0
for i in range(1,n):
    x=l[i]
    dp[i]=min([(dp[j]+abs(x-l[j])) for j in range(max(0,i-k),i)])
print(dp[n-1])

### FIXED CODE ###

n,k=map(int,input().split())
h=list(map(int, input().split()))
v=[0]*n
for i in range(1,n):
	u = h[i]
	v[i] = min([v[j] + abs(h[j]-u) for j in range(max(0, i-k), i)])
print(v[n-1])
### BUGGY CODE ###

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

A0=sorted(A)
i=0
L=0
while i<N:
    C=A0.count(A0[i])
    L+=int(C*(C-1)/2)
    i+=C

for i in range(N):
    L0=A.count(A[i])
    print(L-L0+1)

### FIXED CODE ###

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

L=0
C=[0]*N

for i in range(N):
    C[A[i]-1]+=1

for i in range(N):
    L+=int(C[i]*(C[i]-1)/2)
    
for i in A:
    print(L-C[i-1]+1)
### BUGGY CODE ###


import numpy as np

N = int(input())
P = input().split()
P = np.array([int(p) for p in P])

ans = N
for i, p in enumerate(P):
  ans -= int(((P[:i+1]<p).sum()>0))
print(ans)

### FIXED CODE ###

N = int(input())
P = input().split()
P = [int(p) for p in P]

ans = N
min_p = P[0]
for p in P[1:N]:
  if min_p > p:
    min_p = p
  else:
    ans -=1
print(ans)
### BUGGY CODE ###

NK = list(map(int,input().split()))
AB = []
for _ in range(NK[0]):
    I = list(map(int, input().split()))
    for _ in range(I[1]):
        AB.append(I[0])
print(sorted(AB)[NK[1]-1])

### FIXED CODE ###

from operator import itemgetter, attrgetter
NK = list(map(int,input().split()))
AB = sorted([tuple(map(int, input().split())) for _ in range(NK[0])],key = itemgetter(0))
for i,j in AB:
    NK[1] = NK[1] - j
    if NK[1]<=0:
        print(i)
        break
### BUGGY CODE ###

N, K = map(int, input().split())
S = []
T = []
A = []
for i in range(N):
    x, y = map(int, input().split())
    S.append(x)
    T.append(y)
    A.append([x, y])

ans = float("inf")
for i in S:
    for j in S:
        for k in T:
            for l in T:
                c = 0
                for x, y in A:
                    if i <= x <= j:
                        if k <= y <= l:
                            c += 1
                if c >= K:
                    ans = min(ans, (j - i)*(l - k))
print(ans)

### FIXED CODE ###

N, K = map(int, input().split())
S = []
T = []
A = []
for i in range(N):
    x, y = map(int, input().split())
    S.append(x)
    T.append(y)
    A.append([x, y])

S.sort()
T.sort()
ans = float("inf")
for i in range(N):
    for j in range(i+1, N):
        for k in range(N):
            for l in range(k+1, N):
                c = 0
                for x, y in A:
                    if S[i] <= x <= S[j]:
                        if T[k] <= y <= T[l]:
                            c += 1
                if c >= K:
                    ans = min(ans, (S[j] - S[i])*(T[l] - T[k]))
print(ans)
### BUGGY CODE ###

N = int(input())
lst = list(map(int, input().split()))
if (0 in lst):
  print(0)
else:
  product = 1
  for i in lst: #iというlist中の数字
    product *= i #これが(i + 1)番目

  if(product > 1000000000000000000):
      #これをfor文に書くことで掛け終える前に超えたらやめることができる（処理を短縮できる！）
    print(-1)
  else:
    print(product)

### FIXED CODE ###

N = int(input())
lst = list(map(int, input().split()))
if (0 in lst):
  print(0)
else:
  product = 1
  for i in lst: 
    product *= i

    if(product > 1000000000000000000):
      print(-1)
      break
  else:
    print(product)
### BUGGY CODE ###

N = int(input())
num_list = list(map(int, input().split()))
for i in range(N):
    if i == 0:
        c = num_list[i]
    else:
        c = c * num_list[i]

if c <= 10 ** 18:
    print(c)
else:
    print(-1)

### FIXED CODE ###

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

if 0 in A:
  ans = 0
  N = 0

for i in range(N):
  ans *= A[i]
  if ans > 10**18:
    ans = -1
    break

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


def main():
    S = input()
    Q = int(input())
    qi = [[0 for j in range(3)] for i in range(Q)]
    
    for i in range(Q):
        n = input()
        if n == 1:
            qi[i][0] = 1
        else:
            qi[i]= n.split()

        if qi[i][0] == "1":
            S = S[::-1]
        elif qi[i][0] == "2":
            if qi[i][1] == "1":
                S = str(qi[i][2]) + S
            elif qi[i][1] == "2":
                S = S + str(qi[i][2]) 
    print(S)

if __name__ == "__main__":
	main()

### FIXED CODE ###

from collections import deque

def main():
    S = deque(input())
    
    Q = int(input())
    count = 1

    for i in range(Q):
        n = input()
        if n == "1":
            #qi[i][0] = 1
            count += 1
        else:
            q= list(n.split())
            if (count % 2 == 1 and q[1] == "1") or(count % 2 == 0 and q[1] == "2"):
                S.appendleft(q[2])
            else:
                S.append(q[2]) 

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

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

n,q = map(int,input().split())
s = str(input())
ls = [list(map(int,input().split())) for _ in range(q)]
p = ""
k = 0
for i in range(q):
    for j in range(ls[i][0]-1,ls[i][1]-1):
        if s[j] == "A":
            if s[j+1] == "C":
                k += 1
    print(k)
    k = 0

### FIXED CODE ###

n,q = map(int,input().split())
s = str(input())
a = [0]
for i in range(1,n):
    if s[i-1] == "A" and s[i] == "C":
        a.append(a[-1]+1)
    else:
        a.append(a[-1])
for j in range(q):
    u,t = map(int,input().split())
    print(a[t-1]-a[u-1])
### BUGGY CODE ###

N = int(input())
A = [int(x) for x in input().split(' ')]
def score(A, b):
    s = 0
    for i, Ai in enumerate(A):
        s += abs(Ai - (b + i + 1))
    return s
s = float('inf')
for b in range(-max(A), max(A)):
    si = score(A, b)
    if si < s:
        s = si
print(s)

### FIXED CODE ###

import statistics
N = int(input())
A = [int(x) for x in input().split(' ')]
def score(C, b):
    s = 0
    for i, Ci in enumerate(C):
        s += abs(Ci - b)
    return int(s)

C = [Ai - (i+1) for i, Ai in enumerate(A)]
b = statistics.median(C)
b1 = int(b)
b2 = int(b) + 1
s1 = score(C, b1)
s2 = score(C, b2)
print(min(s1, s2))
### BUGGY CODE ###

def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]
[n,p]=i2()
s=input()

  
if p==2 or p==5:
   x1=0
   for i in range(n):
     if int(s[i])%p==0:
        x1+=i+1  
   print(x1)
else:
  t=0
  x2=0
  d=[0]
  a=1
  for i in range(n)[::-1]:
     t+=int(s[i])*a
     t%=p
     a*=10
     d.append(t)
  from collections import Counter
  c= Counter(d)
  x=0
  for i in c.values():
    if i>1:
      x+=i*(i-1)//2
  print(x)

### FIXED CODE ###

def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]
[n,p]=i2()
s=input()

  
if p==2 or p==5:
   x1=0
   for i in range(n):
     if int(s[i])%p==0:
        x1+=i+1  
   print(x1)
else:
  t=0
  x2=0
  d={0:1}
  a=1
  for i in range(n)[::-1]:
     t+=int(s[i])*a
     t%=p
     a*=10
     a%=p
     if t in d:
        x2+=d[t]
        d[t]+=1
     else:
        d[t]=1
  print(x2)
### BUGGY CODE ###

# coding: utf-8

import sys
import math

max = 11
prime = [2,3,5,7,11]

for line in sys.stdin:
    
    n = int(line)
    
    if n > max:
        for m in range(max + 1, n + 1):
            flag = 1
            
            if m % 2 == 0:
                flag = 0
            else:
                for p in prime:
                    if p > math.sqrt(m):
                        break
                    if m % p == 0:
                        flag = 0
                        break
            
            if flag == 1:
                prime.append(m)
        
        max = n
    
    count = 0
    for i in prime:
        if i > n:
            break
        count += 1
        
    print(count)

### FIXED CODE ###

# coding: utf-8
# Here your code !
import sys
import math

def primes(x):
    if x < 2: return []

    primes = [i for i in range(x)]
    primes[1] = 0 # 1????´???°??§?????????

    # ??¨??????????????????????????????
    for prime in primes:
        if prime > math.sqrt(x): break
        if prime == 0: continue
        for non_prime in range(2 * prime, x, prime): primes[non_prime] = 0

    return [prime for prime in primes if prime != 0]
    
prime_list = primes(1000000)

for line in sys.stdin:
    n = int(line)
    count = 0
    for p in prime_list:
        if p > n:
            break
        count += 1
    print(count)
### BUGGY CODE ###

import sys
input = sys.stdin.readline

R, G, B, N = (int(i) for i in input().split())
#print(R, G, B, N)

cnt=0
for i in range(N//R+1):
  for j in range(N//G+1):
    k=N-R*i-G*j
    if k >=0 and k%B==0:
      cnt+=1
      
print(cnt)

### FIXED CODE ###

import sys
input = sys.stdin.readline

R, G, B, N = (int(i) for i in input().split())
#print(R, G, B, N)

cnt=0
for i in range(N//R+1):
  for j in range(N//G+1):
    k=N-R*i-G*j
    if k >=0 and k%B==0:
      cnt+=1
      
print(cnt)
### BUGGY CODE ###

N = int(input())
d = []
for i in range(N):
    h, p = map(int, input().split())
    d.append((h, p))

d = sorted(d, key=lambda x: x[0] + x[1])
INF = float('inf')
dp = [[INF] * (N + 1) for i in range(N + 1)]
dp[0][0] = 0
for i in range(1, N + 1):
    for j in range(N + 1):
        dp[i][j] = dp[i - 1][j]
        if j == 0:
            continue
        if dp[i - 1][j - 1] > d[i - 1][0]:
            continue
        dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + d[i - 1][1])

for i in range(N, -1, -1):
    if dp[N][i] < INF:
        print(i)
        exit()


### FIXED CODE ###

N = int(input())
d = []
for i in range(N):
    h, p = map(int, input().split())
    d.append((h, p))

d = sorted(d, key=lambda x: x[0] + x[1])
INF = float('inf')
dp1 = [INF] * (N + 1)
dp2 = [INF] * (N + 1)
dp1[0] = 0
for i in range(1, N + 1):
    for j in range(N + 1):
        dp2[j] = dp1[j]
        if j == 0:
            continue
        if dp1[j - 1] > d[i - 1][0]:
            continue
        dp2[j] = min(dp2[j], dp1[j - 1] + d[i - 1][1])
    dp1, dp2 = dp2, dp1

for i in range(N, -1, -1):
    if dp1[i] < INF:
        print(i)
        exit()
### BUGGY CODE ###

base = input().split()
n = int(base[0])
k = int(base[1])
ans = []
def fnk(a):
  if a>=0:
    return a
  else:
    return a*(-1)
while fnk(n-k) not in ans:
  ans.append(fnk(n-k))
  n = fnk(n-k)
print(min(ans))

### FIXED CODE ###

base = input().split()
n = int(base[0])
k = int(base[1])
s = n%k
t = k-s
if s>t:
  print(t)
else:
  print(s)
### BUGGY CODE ###

#!/usr/bin/env python3
import sys

sys.setrecursionlimit(10 ** 8)
input = sys.stdin.readline


def dfs(now):
    seen[now] = True
    for next in branch[now]:
        if seen[next] is False:
            score[next] += score[now]
            dfs(next)


N, Q = map(int, input().split())
branch = [set() for _ in range(N)]
for _ in range(N - 1):
    a, b = map(int, input().split())
    branch[a - 1].add(b - 1)
    branch[b - 1].add(a - 1)
score = [0] * N
for _ in range(Q):
    p, x = map(int, input().split())
    score[p - 1] += x

seen = [False] * N
for i in range(N):
    if seen[i] is False:
        dfs(i)

print(*score)


### FIXED CODE ###

#!/usr/bin/env python3
from sys import setrecursionlimit

setrecursionlimit(10 ** 8)


def dfs(now):
    seen[now] = True
    score[now] += operation[now]
    for next in branch[now]:
        if seen[next] is False:
            score[next] += score[now]
            dfs(next)


N, Q = map(int, input().split())
branch = [set() for _ in range(N)]
for _ in range(N - 1):
    a, b = map(int, input().split())
    branch[a - 1].add(b - 1)
    branch[b - 1].add(a - 1)
operation = [0] * N
for _ in range(Q):
    p, x = map(int, input().split())
    operation[p - 1] += x

seen = [False] * N
score = [0] * N
for i in range(N):
    if seen[i] is False:
        dfs(i)

for ans in score:
    print(ans, end=' ')