peterverebics/CodeNet_Python_428 huggingface dataset:
# Judge status: Accepted
n, m = [int(i) for i in input().split()]
sc = [list(map(int, input().split())) for _ in range(m)]


ans = -1
for i in range(10**(n-1) if n > 1 else 0, 10**n):
    for s, c in sc:
        if str(i)[s-1] != str(c):
            break
    else:
        ans = i
        break
print(ans)
# Judge status: Accepted
S = input()
s = list(S)
num = len(s)

cnt = 0
for i in range(num // 2):
    if s[i] != s[num - 1 - i]:
        cnt += 1
print(cnt)
# Judge status: Wrong Answer
n = int(input())
rate = list(map(int,input().split()))
ratecolor = 0
free = 0
ratecolor = 0
color = [0]*9
for i in range(n):
    if(rate[i] >= 3200):
        free += 1
        break
    for j in range(9):
        if(rate[i] < j*400 + 400):
            if(color[j] == 0):
                color[j] += 1
                ratecolor += 1
                break

print(ratecolor if ratecolor != 0 else 1,ratecolor+free)
# Judge status: Wrong Answer
import math

a, b, c = map(int, input().split())
ans="No"
if a**2+b**2+c**2>2*(a*b+b*c+c*a):
    ans="Yes"
print(ans)
# Judge status: Accepted
a,b,x=map(int,input().split())
c=a-1
max=b//x+1
min=c//x+1
if c==-1:
    min=0
else:
    pass
print(max-min)
# Judge status: Accepted
n=int(input())
p=list(map(int,input().split()))
ans=0
count=0
for i in range(n):
  if p[i] == i+1:
    ans+=1
    count+=1
  else:
    count=0
  if count==2:
    ans-=1
    count=0
print(ans)
# Judge status: Accepted
def main():
    N, M = map(int, input().split())
    X = [int(a) for a in input().split()]

    if N >= M:
        print(0)
    else:
        X.sort()
        dlist = list()
        for i in range(M-1):
            d = abs(X[i] - X[i+1])
            dlist.append(d)
        dlist.sort()
        print(sum(dlist[:len(dlist) - (N-1)]))
if __name__ == "__main__":
    main()
# Judge status: Accepted
S = input()[::-1]

kari = ['dream', 'dreamer', 'erase', 'eraser']
 
words = [i[::-1] for i in kari]
 
l = len(S)
cnt = ''

for i in range(l):
    cnt += S[i]
    if cnt in words:
        cnt = ''

if cnt!='':
    print('NO')
else:
    print('YES')
# Judge status: Wrong Answer
p,q,r = map(int,input().split())

print(min(p+q,p+r,r*q))
# Judge status: Accepted
A = input()
B = input()
if A[0] == B[2] and A[1] == B[1] and A[2] == B[0]:
  print("YES")
else:
  print("NO")
# Judge status: Accepted
import sys
inputs = list(map(int, sys.stdin))
ai = inputs[1]
for i in range(inputs[0]):
    if (ai == 2):
        print(i + 1)
        break
    ai = inputs[ai]
else:
    print(-1)
# Judge status: Accepted
n = int(input())
a = list(map(int, input().split()))

m = sum(a) // n
ans = 0
for x in a:
    ans += (x - m) ** 2
ansp = 0
for x in a:
    ansp += (x - m - 1) ** 2
print(min(ans, ansp))
# Judge status: Accepted
# https://atcoder.jp/contests/abc052/tasks/abc052_b

N = int(input())
S = input()

max_count = 0
curr_count = 0

for s in S:
    if s == 'I':
        curr_count += 1

    elif s == 'D':
        curr_count -= 1

    if curr_count > max_count:
        max_count = curr_count

print(max_count)
# Judge status: Accepted
n, x = map(int, input().split())
al = list(map(int, input().split()))

cnt = 0
for i in range(n-1):
    if al[i] + al[i+1] > x:
        if al[i+1] > (al[i] + al[i+1]) - x:
            cnt += al[i] + al[i+1] - x
            al[i+1] -= (al[i] + al[i+1] - x)
        else:
            cnt += al[i+1] + al[i] - x
            al[i+1] = 0
            al[i] = x

print(cnt)
# print(al)
# Judge status: Wrong Answer
import math
import fractions
a,b,c,d=map(int,input().split())
base=b-a+1
n_c=1+(b-c+1)//c-(a+c-1)//c+1
n_d=1+(b-d+1)//d-(a+d-1)//d+1
cd=c*d/fractions.gcd(c,d)
n_cd=1+(b-cd+1)//cd-(a+cd-1)//cd+1
print(base-n_c-n_d+n_cd)
# Judge status: Wrong Answer
import numpy

n = int(input())
A1 = list(map(int, input().split()))
A2 = list(map(int, input().split()))

ans = 0
for i in range(n):
    ans = max(ans, sum(A1[:i+1])+sum(A2[i:]), sum(A2[:i+1])+sum(A1[i:]))
print(ans)
# Judge status: Accepted
a=sorted(list(map(int,input().split())))
print(a[0]*a[1]//2)
# Judge status: Wrong Answer
n = int(input())
a = list(map(int, input().split()))

i = 0
m = 100
M = -100

while i < n:
    if a[i]<m:
        m = a[i]
    if a[i]>M:
        M = a[i]
    i+=1

s1 = (m + M)//2
s2 = s1 + 1

i = 0
x1 = 0
x2 = 0
while i < n:
    x1 += (s1-a[i])**2
    x2 += (s2-a[i])**2
    i+=1

if x1<x2:
    ans = x1
else:
    ans = x2
print(ans)
# Judge status: Wrong Answer
A, B, K = map(int, input().split())

cnt = 0
for i in range(A+1, 1, -1):
    if A % i == 0 and B % i == 0:
        cnt += 1
        if cnt == K:
            print(i)
            exit()
# Judge status: Accepted
k = int(input())
list = list(map(int, input().split()))
a = list[0]
b = list[1]
for i in range(a,b+1):
  if i % k ==0:
    print("OK")
    break
  if i==b:
    print("NG")
# Judge status: Wrong Answer
n,m,x,y=map(int,input().split())
l_x=sorted(list(map(int,input().split())))
l_y=sorted(list(map(int,input().split())))
print("War" if l_x[-1] >= l_y[0] else "No War")
# Judge status: Accepted
A=[]
B=[False]*9
for i in range(3):
    A+=[int(i) for i in input().split()]
n=int(input())
for _ in range(n):
    b=int(input())
    for i in range(9):
        if b==A[i]:
            B[i]=True
C=[[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9],[1,5,9],[3,5,7]]
ans=False
for a,b,c in C:
    if B[a-1] and B[b-1] and B[c-1]:
        ans=True
print("Yes" if ans else "No")
# Judge status: Accepted
def move_time(x1,y1,x2,y2):
    return abs(x1-x2)+abs(y1-y2)

n = int(input())
t1 = 0
x1 = 0
y1 = 0
isYes = True
for i in range(n):
    t2,x2,y2 = map(int,input().split())
    tm = move_time(x1,y1,x2,y2)
    #print(tm,t2,x2,y2)
    if tm>(t2-t1):
        isYes = False
    elif ((t2-t1)-tm)%2==1:
        isYes = False
    t1 = t2
    x1 = x2
    y1 = y2

if isYes:
    print('Yes')
else:
    print('No')
# Judge status: Accepted
N, M = map(int, input().split())

ol = []


def zeros(x):
    xt = str(x)
    k = 6 - len(xt)
    return "0" * k + xt

for i in range(M):
    p,y = map(int, input().split())
    ol.append((p,y))

l = sorted(ol)


res = {}

t=0
for i in range(M):
    if l[i][0] == l[i-1][0]:
        t+=1
    else:
        t=1
    res[l[i]] = zeros(l[i][0])+ zeros(t)

for i in ol:
    print(res[i])
# Judge status: Wrong Answer
import math
N = int(input())
cnt = 0
if N%2==1:
    print(0)
else :
    for i in range(1,30):
        cnt +=math.floor(N//5**i)
        if N < 5**i:
            break
    print(cnt)
# Judge status: Accepted
import math
N,D = list(map(int,input().split()))
print(math.ceil(N/(2*D+1)))
# Judge status: Accepted
x = 4
y = 7

a=int(input())

ans = 'No'

for i in range(20):
  for j in range(14):
    if (x * i) + (y * j) == a:
      ans = 'Yes'

print(ans)
# Judge status: Wrong Answer
from collections import deque

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

def bfs(u):
  queue=deque([u])
  d=[None]*n
  d[u]=0
  while queue:
    v=queue.popleft()
    for i in g[v]:
      if d[i]==None:
        d[i]=d[v]+1
        queue.append(i)
  return d

d=bfs(0)
if None in d:
  print("No")
else:
  print("Yes")
  for i in range(1,n):
    print(d[i])
# Judge status: Accepted
a, b = [int(i) for i in input().split()]
if b == 1:
    print(0)
    exit()
n = int((b-2) / (a-1))
print(n+1)
# Judge status: Accepted
from math import log, ceil
N = int(input())
A = list(map(int, input().split()))

#print(ceil(log(max(A)+1,2)))
keta = ceil(log(max(A)+1,2))

X = [0] * keta

for a in A:
  work = a
  i = 0
  while work > 0:
    X[i] += work % 2
    i += 1
    work = work // 2

ans = 0
mod = int(1e9+7)
for i in range(len(X)):
  ans += X[i] * (N-X[i]) * 2**i
  ans = ans % mod
  
print(ans)
# Judge status: Accepted
n,m = map(int,input().split())
line = sorted(list(map(int,input().split())),reverse = True)
sums = sum(line)
if sums % (4*m) != 0:
  least = sums//(4*m) + 1
else:
  least = sums//(4*m)
box = []
for i in line:
  if i >= least:
    box.append(i)
if len(box) >= m:
  print("Yes")
else:
  print("No")
# Judge status: Accepted
n = int(input())
c = 0
for i in range(1, n+1):
  if len(str(i)) % 2 == 1:
    c += 1
print(c)
# Judge status: Accepted
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = 10**6
a,b,c = inpl()
for i in range(1,n):
    if (a*i)%b == c:
        print('YES')
        break
else:
    print('NO')
# Judge status: Accepted
N = int(input())
stones = input()

total_count_R = stones.count('R')
left_count_R = stones[0:total_count_R].count('R')

answer = total_count_R - left_count_R

print(answer)
# Judge status: Accepted
n = int(input())
dic = []
for i in range(n):
    inp = input().split()
    dic.append([inp[0], 100-int(inp[1]), i+1])
    temp = ''
    for j in range(10):
        try:
            temp += str(ord(dic[i][0][j])-80)
        except:
            temp += '00'
    dic[i][0] = int(temp)
dic = sorted(dic)
for i in range(n):
    print(dic[i][2])
# Judge status: Accepted
N = int(input())
a = list(map(int, input().split()))

p1, p2, p4 = (0, 0, 0)
for ai in a:
	if ai % 4 == 0:
		p4 += 1
	elif ai % 2 == 0:
		p2 += 1
	else:
		p1 += 1
		
ans = 'Yes'
if p4 + 1 < p1 or (p4 + 1 == p1 and p2 != 0):
	ans = 'No'

print(ans)
# Judge status: Accepted
n = int(input())
ans = (n + 1) * n // 2
for i in range(2, n + 1):
    j = 1
    while i * j <= n:
        ans += i * j
        j += 1
print(ans)
# Judge status: Accepted
N,K = map(int,input().split())

B = [0]*N

for i in range(K):
    d = int(input())
    A = list(map(int,input().split()))
    for j in range(N):
        if j+1 in A:
            B[j] = 1

print(B.count(0))
# Judge status: Accepted
a,b,c = map(int,input().split())
if b-a==c-b:
  print("YES")
else:
  print("NO")
# Judge status: Accepted
s, w = map(int, input().split())

if w >= s:
    print('unsafe')
else:
    print('safe')
# Judge status: Accepted
import math
N = int(input())

for i in range(N+1):
  if math.floor(i*1.08) == N:
    print(i)
    exit()
print(':(')
# Judge status: Accepted
N, M = map(int, input().split())
ans = 0
pen = 0
w = [0]*(N+1)
ac = [False]*(N+1)
for _ in range(M):
    p, s = input().split()
    if s == "AC" and not ac[int(p)]:
        pen += w[int(p)]
        ac[int(p)] = True
        ans += 1
    elif s == "WA":
        w[int(p)] += 1
print(ans, pen)
# Judge status: Accepted
A,B=map(int,input().split())

if A>8 or B>8:
  print(':(')
else:  
  print('Yay!')
# Judge status: Wrong Answer
def f(x,y):
    A=0
    for i in range(len(x)):
        A+=(x[i]-y[i])*(x[i]-y[i])
    return A

M=20
N,D=map(int,input().split())

X=[]
for _ in range(N):
    X.append(tuple(map(int,input().split())))

S=set()
k=0
L=M*M*D
while k*k<=L:
    S.add(k*k)
    k+=1
    
K=0
for i in range(N):
    for j in range(i+1,N):
        K+=(f(X[i],X[j]) in S)
print(K)
# Judge status: Wrong Answer
x = int(input())


for i in range(1,10**5):
    ans = (i*(i+1)//2)
    if ans > x:
        print(i)
        break
# Judge status: Accepted
N,X = map(int,input().split())
x = list(map(int,input().split()))
x.append(X)
x.sort()
Mindif = x[1]-x[0]
import math
for i in range(1,N):
    Mindif = math.gcd(Mindif,x[i+1]-x[i])
print(Mindif)
# Judge status: Wrong Answer
N,M = map(int,input().split())

a = [list(map(int,input().split())) for x in range(N)]
c = [list(map(int,input().split())) for x in range(M)]

min_cpoint = -1
num_stack = 51

for i in range(N):
    for j in range(M):
        if num_stack > abs(a[i][0]-c[j][0]) + abs(a[i][1]-c[j][1]):
            num_stack = abs(a[i][0]-c[j][0]) + abs(a[i][1]-c[j][1])
            min_cpoint = j+1
    print(min_cpoint)
    min_cpoint = -1
    num_stack = 51
# Judge status: Wrong Answer
n,k=map(int,input().split())
a=[int(i)for i in input().split()]
d=[0 for i in range(max(a))]
for i in a:
    d[i-1]+=1
ans=sum(sorted(d)[:k-1])
print(ans)
# Judge status: Accepted
n = int(input())
li = [input() for i in range(n)]

print("AC x " + str(li.count("AC"))) 
print("WA x " + str(li.count("WA"))) 
print("TLE x " + str(li.count("TLE"))) 
print("RE x " + str(li.count("RE")))
# Judge status: Wrong Answer
N=int(input())
p=list(map(int,input().split()))
a=[]
for i in range(1,N):
    if p[i-1]>=p[i]:
     a.append(p[i])
c=set(a)

print(len(c)+1)
# Judge status: Wrong Answer
a = int(input())
if a < 3:
	print(0)
else:
	yy = a / 3
	print(yy**3)
# Judge status: Wrong Answer
n,m=map(int,input().split())
if n>=3 and m>=3:
  print(n*m-(n-2)*(m-2))
elif n==1 or m==1:
  print(2)
# Judge status: Wrong Answer
n, l = map(int, input().split())
d = {input() for _ in range(n)}
print(sorted(d))
# Judge status: Accepted
n = int(input())
a = list(map(int, input().split()))
b = sum(a)
c = 0
for i in range(n):
  c += a[i]
  if c >= b / 2:
    k = i
    break
cut1 = c - (b - c)
cut2 = b - c + a[k] - (c - a[k])
ans = min(cut1, cut2)
print(ans)
# Judge status: Accepted
N = int(input())
a = list(map(int, input().split()))


answer = 0
for i, e in enumerate(a):
    i += 1
    if i % 2 != 0 and e % 2 != 0:
        answer += 1 
print(answer)
# Judge status: Accepted
import collections
n = int(input())
a = [0] + list(map(int,input().split()))

a_pos = [a[i]+i for i in range(1,n+1)]
a_neg = [-a[i]+i for i in range(1,n+1)]
#print(a_pos)
#print(a_neg)

c_pos = collections.Counter(a_pos)
c_neg = collections.Counter(a_neg)
#print(c_pos)
#print(c_neg)

ans = 0
for c in c_pos:
    ans += c_pos[c] * c_neg[c]

print(ans)
# Judge status: Wrong Answer
n = int(input())
A = sorted(list(map(int, input().split())))
work = sum(A[1:])
temp = work % A[0]
result = temp if temp!=0 else A[0]
print(result)
# Judge status: Accepted
a,b,m=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
#print(a,b,m)
#print(A)
#print(B)

total = 10**5+10**5
#print(total)

for i in range(m):
    x,y,c=map(int,input().split())
    #print(x,y,c)
    D = (A[x-1] + B[y-1] - c)
    if total > D:
        total = D
    #print(D)
A.sort()
B.sort()
if total > (A[0] + B[0]):
    total = (A[0] + B[0])
print(total)
# Judge status: Accepted
N,M=map(int,input().split())
ans=[]
if N%2==1:
  for i in range(M):
    ans.append([i+1,N-i])
else:
  for i in range(M//2):
    ans.append([i+1,2*M-i+1])
  for j in range(M-M//2):
    ans.append([j+M//2+1,2*M-M//2-j])
for k in ans:
  print(*k,sep=' ')
# Judge status: Accepted
N,K = map(int,input().split())
A = list(map(int,input().split()))

while True:
  if K == 0:
    break
  imosu = [0 for i in range(N+2)]
  for i in range(N):
    l = max(0,i-A[i])
    r = min(N-1,i+A[i])
    imosu[l] += 1
    imosu[r+1] -= 1
  A = [imosu[0]]
  for i in imosu[1:N]:
    A.append(A[-1]+i)
  K -= 1
  if min(A) == N:
    break
  
print(*A)
# Judge status: Accepted
n = int(input())
a = list(map(int, input().split()))

a.sort(reverse=True)

n -= 1

idx = 0
score = a[idx]
n -= 1
idx += 1

while n > 0:
    if n > 1:
        score += a[idx] * 2
        n -= 2
        idx += 1
    else:
        score += a[idx]
        n -= 1

print(score)
# Judge status: Wrong Answer
data_a,data_b,data_c,data_d = [int(x) for x in input().split()]


length_1 = data_a - data_b
length_2 = data_b - data_c
length_3 = data_a - data_c

if length_3 <= data_d:
    print('Yes')
elif length_1 <= data_d and length_2 <= data_d:
    print('Yes')
else:
    print('No')
# Judge status: Wrong Answer
a,b=map(int,input().split())
if (a<=0 and b>=0) or (b<=0 and a>=0):
    print("Zero")
elif a>0 and b>0:
    print("Positive")
else:
    if b-a%2==1:
        print("Positive")
    else:
        print("Negative")
# Judge status: Wrong Answer
a=input()
b=list(map(int,input().split()))
b.sort()
if sum(b)-max(b)-max(b)<=0:
  print("Yes")
else:
  print("No")
# Judge status: Wrong Answer
N, X = map(int, input().split())
L = list(map(int, input().split()))
ans = 1
d = 0
for i in range(N):
  d += L[0]
  if d <= X:
    ans += 1
print(ans)
# Judge status: Accepted
# -*- coding: utf-8 -*-
import math

N = int(input())

ans =  (10**N) - 9**N - 9**N + 8**N
ans = ans % ((10**9)+7)
print(ans)
# Judge status: Accepted
A,B,C,D=map(int,input().split())
minX=max(A,C)
maxX=min(B,D)
if(maxX-minX<0):
    print(0)
else:
    print(maxX-minX)
# Judge status: Wrong Answer
n=int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

m=0
# 0で初期化すると最初が1の時にだめ
at=0
for i,ai in enumerate(a):
    m+=b[ai-1]
    if ai == at+1:
        m+=c[at-1]
    at=ai
print(m)
# Judge status: Accepted
n=int(input())
mod=10**9+7
ans=10**n-2*9**n+8**n
print(ans%mod)
# Judge status: Accepted
from math import gcd

k = int(input())

ans = 0
for x in range(1,k+1):
    for y in range(1,k+1):
        for z in range(1,k+1):
            ans += gcd(gcd(x,y),z)

print(ans)
# Judge status: Accepted
k,x = map(int,input().split())
if 500*k >= x:
    print("Yes")
else:
    print("No")
# Judge status: Wrong Answer
n,d=map(int,input().split())
l=[list(map(int,input().split())) for _ in range(n)]
cnt=0

for i in range(n-1):
  for i2 in range(i+1,n):
    distance = 0
    for j in range(d):
      distance+=(abs(l[i][j])-abs(l[i2][j]))**2
    
    if (distance**0.5).is_integer():
      cnt+=1
      
print(cnt)
# Judge status: Accepted
N = int(input())
A = [int(x) for x in input().split()]
count = [0 for _ in range(10**6+10)]
for a in A:
    if count[a] > 1:
        continue
    for x in range(0, 10**6+10, a):
        count[x] += 1
ans = 0
for a in A:
    if count[a] == 1:
        ans += count[a]
print(ans)
# Judge status: Accepted
X = int(input())
if(X == 7 or X == 5 or X == 3):
  print('YES')
else:
  print("NO")
# Judge status: Accepted
S = input()
lis = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"]

print(7 - lis.index(S))
# Judge status: Wrong Answer
N = int(input())
A = int(input())

nokori = N % 500

if nokori <= A:
    print("YES")
else:
    print("NO")
# Judge status: Wrong Answer
import sys
input=sys.stdin.buffer.readline
from math import*

n=int(input())
arr=list(map(int,input().split()))
ans=1
if n>2:
	ans=gcd(arr[0],arr[1])
	for i in range(2,n-1):
		ans=gcd(ans,arr[i])
else:
	ans=arr[0]
if arr[-1]%ans==0:
	print(ans)
else:
	print(arr[-1]%ans)
# Judge status: Accepted
import re
s=input()
S=re.sub(r'([0-9]{4})/([0-9]{2})/([0-9]{2})',r'\1\2\3',s)
if int(S)<=20190430:
    print('Heisei')
else:
    print('TBD')
# Judge status: Accepted
#!/usr/local/bin python
# -*- coding: utf-8 -*-
#
# ~/PycharmProjects/atcoder/C-RemainderMinimization.py
#
# import math
from fractions import gcd
a, b, c, d = map(int, input().split())
# cd_lcm = (c * d) // math.gcd(c, d)
cd_lcm = (c * d) // gcd(c, d)
ab_c = b//c - (a-1)//c
ab_d = b//d - (a-1)//d
ab_cd_both = b//cd_lcm - (a-1)//cd_lcm
ab_cd_either = ab_c + ab_d - ab_cd_both
ab_cd_neither = b-a+1 - ab_cd_either

print(ab_cd_neither)
# Judge status: Wrong Answer
A,B,C=map(int,input().split())
ans = 0
if A==B:
  ans = ans+1
if B==C:
  ans=ans+1
if A==C:
  ans=ans+1
if ans==3:
  print("Yes")
else:
  print("No")
# Judge status: Accepted
S = list(input())

S.sort()

flag = 1

if 'N' in S:
    if 'S' not in S:
        flag = 0

if 'S' in S:
    if 'N' not in S:
        flag = 0

if 'E' in S:
    if 'W' not in S:
        flag = 0

if 'W' in S:
    if 'E' not in S:
        flag = 0

if flag:
    print('Yes')

else:
    print('No')
# Judge status: Accepted
from collections import Counter
N=int(input())
A=[int(input()) for _ in range(N)]
l=[]
c=Counter(A)
values=list(c.values())
key=list(c.keys())
cnt=0
for i in range(len(key)):
    l.append([key[i],values[i]])

for i in range(len(key)):
    if l[i][1]%2==1:
        cnt=cnt+1
print(cnt)
# Judge status: Accepted
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

ans = 0
for i in range(N-1, -1, -1):
    if B[i] >= A[i+1]:
        B[i] -= A[i+1]
        ans += A[i+1]
        A[i+1] = 0
        if B[i] >= A[i]:
            ans += A[i]
            A[i] = 0
        else:
            ans += B[i]
            A[i] -= B[i]
    else:
        ans += B[i]
print(ans)
# Judge status: Accepted
a, b, c = map(int, input().split())
k = int(input())
cnt = 0
while a >= b:
    b *= 2
    cnt += 1
while b >= c:
    c *= 2
    cnt += 1

if cnt <= k:
    print("Yes")
else:
    print("No")
# Judge status: Accepted
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))

import collections

c = collections.Counter(A)

lis = list()
for i in c.most_common():
    lis.append(i[:][1])

lis.sort()

ans = 0
if len(c.most_common()) > K:
    ans += sum(lis[:(len(c.most_common())- K)])
print(ans)
# Judge status: Accepted
N=int(input())
S=list(input())
A = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
for i in range(len(S)):
  for j in range(26):
    if S[i]==A[j]:
      S[i]=A[(j+N)%26]
      break
print(''.join(S))
# Judge status: Accepted
n = int(input())
a = [int(i) for i in input().split(' ')]
result = 0
max_height = 0

for i in range(n):
  an = a[i]
  if i == 0:
    max_height = an
    max_height_idx = 0
    continue

  if an >= max_height:
    max_height = an
    max_height_idx = i
    continue

  result += (max_height - an)

print(result)
# Judge status: Accepted
n,x = map(int,input().split())
l = list(map(int,input().split()))
ans = 0
count = 1
for i in range(n):
  ans = ans + l[i]
  if ans > x:
    break
  count += 1
print(count)
# Judge status: Accepted
N = int(input())
cnt = [[0] * 10 for i in range(10)]
for n in range(1, N + 1):
    cnt[int(str(n)[0])][int(str(n)[-1])] += 1

ans = 0
for i in range(10):
    for j in range(10):
        ans += cnt[i][j] * cnt[j][i]

print(ans)
# Judge status: Accepted
import sys

input = sys.stdin.readline


def main():
    N = int(input())
    A = int(input())

    if N % 500 <= A:
        ans = "Yes"
    else:
        ans = "No"

    print(ans)


if __name__ == "__main__":
    main()
# Judge status: Accepted
n = int(input())
C = list(input())

if "R" not in C:
    print(0)
    exit()

W = C.count("W")
R = C.count("R")
w = 0
r = R
ans = float('inf')

for c in C:
    if c == "W":
        w += 1
    else:
        r -= 1
    ans = min(ans, max(w, r))
print(ans)
# Judge status: Accepted
def main():
    import sys

    def input(): return sys.stdin.readline().rstrip()

    n = int(input())
    s = input()
    cnt = [0]
    for i in range(n):
        cnt.append(cnt[-1])
        if s[i] == '#':
            cnt[-1] += 1
    
    m = cnt[-1]
    ans = 10**9
    for i in range(n+1):
        tmp = cnt[i] + (n-i)-(m-cnt[i])
        ans = min(tmp, ans)
    print(ans)


        
        
if __name__ == '__main__':
    main()
# Judge status: Accepted
n, m = map(int, input().split())
problem = [0] * n
ac = 0
penalty = 0
for i in range(m):
    p, s = map(str, input().split())
    if s == "AC":
        temp = problem[int(p) - 1]
        if temp >= 0:
            ac += 1
            penalty += temp
            problem[int(p) - 1] = -1
    else:
        if problem[int(p) - 1] >= 0:
            problem[int(p) - 1] += 1
print(ac, penalty)
# Judge status: Accepted
def solve(s, k):
    # 2^5000兆 >> 10^18 なので、sのk文字目までに1以外の数字xがあれば、k番目はxになる
    for i in range(min(k, len(s))):
        if s[i] != '1':
            return int(s[i])
    else:
        return 1

_s = input()
_k = int(input())
print(solve(_s, _k))
# Judge status: Wrong Answer
n = int(input())

for i in range(n+1):
    if i * 1.08 == n:
        print(i)
        exit()
print(":(")
# Judge status: Accepted
from fractions import gcd

MOD = 1000000007

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

lcm = 1
for i in A: lcm = lcm * i // gcd(lcm, i)

ans = 0
for i in A: ans += lcm // i

print(ans % MOD)
# Judge status: Wrong Answer
from collections import Counter

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

c = Counter(a)
list = c.most_common()
max = list[0][1]

ans = []
for i, j in list:
    if j == max:
        ans.append(i)
    else:
        break

ans.sort()
for i in range(len(ans)):
    print(ans[i])
# Judge status: Accepted
from itertools import accumulate

mod = 10 ** 9 + 7
input()
l = list(map(int, input().split()))
print(sum(a * b % mod for a, b in zip(list(accumulate(l))[:-1], l[1:])) % mod)