Harryxun/stf_run huggingface dataset:
from collections import Counter
from itertools import accumulate
import sys
input = sys.stdin.readline
n,p = map(int,input().split())
s = input().rstrip()
if p == 2:
  ans = 0
  for i in range(n):
    if int(s[i])%2 == 0:
      ans += i+1
  print(ans)
elif p == 5:
  for i in range(n):
    if int(s[i])%5 == 0:
      ans += i+1
  print(ans)
else:
  ls = [0]
  prev = 0
  for i in range(n):
    x = int(s[n-i-1])
    y = (prev+x*pow(10,i,p))%p
    ls.append(y)
    prev = y
  c = Counter(ls)
  ans = 0
  for x in c.values():
    ans += x*(x-1)//2
  print(ans)

### FIXED CODE ###

from collections import Counter
from itertools import accumulate
import sys
input = sys.stdin.readline
n,p = map(int,input().split())
s = input().rstrip()
if p == 2:
  ans = 0
  for i in range(n):
    if int(s[i])%2 == 0:
      ans += i+1
  print(ans)
elif p == 5:
  ans = 0
  for i in range(n):
    if int(s[i])%5 == 0:
      ans += i+1
  print(ans)
else:
  ls = [0]
  prev = 0
  for i in range(n):
    x = int(s[n-i-1])
    y = (prev+x*pow(10,i,p))%p
    ls.append(y)
    prev = y
  c = Counter(ls)
  ans = 0
  for x in c.values():
    ans += x*(x-1)//2
  print(ans)
K,X = map(int,input().split())
mi = X-K+1
Ma = X+K-1
for i in range(mi,Ma+1):
    A.append(i)
print(*ans)

### FIXED CODE ###

K,X = map(int,input().split())
mi = X-K+1
Ma = X+K-1
A = []
for i in range(mi,Ma+1):
    A.append(i)
print(*A)
import math
def eratosthenes(n):
    prime_list = []
    num_list=[i for i in range(2, n+1)]
    limit = math.sqrt(n)
    while True:
        if limit <= num_list[0]:
            return prime_list + num_list
        prime_list.append(num_list[0])
        num_list = [e for e in num_list if e % num_list[0] != 0]

ls = eratosthenes(100000)
ls2 = []
count = 0
for i in ls:
    if (i+1)//2 in ls:
        count += 1
    ls2.append(count)

Q = int(input())
for i in range(Q):
    l, r = map(int, input().split())
    print(ls2[r]-ls2[l])

### FIXED CODE ###

q = int(input())
    
def is_prime(n):
    if n == 1:
        return False
    for i in range(2,int(n**0.5)+1):
        if n % i == 0:
            return False
    return True

ls = [0]

for i in range(1,100000):
    if is_prime(i) and is_prime((i+1)//2):
        ls.append(ls[i-1]+1)
    else:
        ls.append(ls[i-1])
        
for i in range(q):
    l,r = map(int,input().split())
    if l == r:
        if is_prime(l) and is_prime((l+1)//2):
            print(1)
        else:
          print(ls[r]-ls[l-1])  
    else:
        print(ls[r]-ls[l-1])
n,k = map(int,input().split())
c = [int(input()) for _ in range(n)]

c.sort()

for i in range(n-k+1):
    ans = min(ans,max(c[i:k+i])-min(c[i:k+i]))

print(ans)

### FIXED CODE ###

n,k = map(int,input().split())
c = [int(input()) for _ in range(n)]
c.sort()
ans = []
for i in range(n-k+1):
    ans.append(c[i+k-1]-c[i])
print(min(ans))
N = int(input())
A = list(map(int, input().split()))
print(max(A)-min(A))


### FIXED CODE ###

S = input()
T = input()

ans = False
for i in range(len(S)):
    R = S[i:] + S[:i]
    if T == R:
        ans = True
        break

print("Yes" if ans else "No")
n,*c=map(int,open(0).readlines())
l=[-1]*n
b=[]
for i in range(n):
  b.append(l[c[i]])
  l[c[i]]=i
dp=[1]+[0]*n
for i in range(1,n):
  dp[i]=dp[i-1]
  if c[i]!=c[i-1]and b[i]>-1:
    dp[i]=(dp[i]+dp[b[i]])%(10**9+7)
print(dp[-2])

### FIXED CODE ###

n,*c=map(int,open(0).readlines())
l=[-1]*2**18
b=[]
for i in range(n):
  b.append(l[c[i]])
  l[c[i]]=i
dp=[1]+[0]*n
for i in range(1,n):
  dp[i]=dp[i-1]
  if c[i]!=c[i-1]and b[i]>-1:
    dp[i]=(dp[i]+dp[b[i]])%(10**9+7)
print(dp[-2])
import numpy as np
def main():

    A, B = input().split()
    A = np.array(int(A), dtype=np.int64)
    B = np.array(float(B), dtype=np.float64)
    v = A * B
    return int(v)

if __name__ == '__main__':
    print(main())


### FIXED CODE ###

def main():

    A, B = input().split()
    B = "".join(B.split('.'))
    A = int(A)
    B = int(B)
    return (A * B) // 100

if __name__ == '__main__':
    print(main())
I = int(input())
A = input()
B = input()
C = input()

count = 0
for i in range(I):
  if not (A[i]==B[i]==C[i]):
    count += 1 if A[i]==B[i] or B[i]== C[i] or A[i]==C[i] else 2

print(count)

### FIXED CODE ###

N, A, B = [int(i) for i in input().split()]

X = A if A < B else B
Y = 0 if N > A+B else A+B-N

print("{0} {1}".format(X, Y))
A = input()
B = input()
C = input()

print(N[0] + N[1] + N[2])


### FIXED CODE ###

A = input()
B = input()
C = input()

print(A[0] + B[1] + C[2])
n, m = map(int, input().split())
p = list(map(int, input().split()))
xys = [list(map(int, input().split())) for _ in range(m)]
uf = {}

def dfs(cld):
    if cld not in uf:
        return []
    elif uf[cld] == None:
        return [cld]
    return dfs(uf[cld]) + [cld]

for xy in xys:
    xin = (xy[0] in uf)
    yin = (xy[1] in uf)
    xp = dfs(xy[0])
    yp = dfs(xy[1])
    if xin and yin:
        if len(xp) > len(yp):
            uf[xy[1]] = xp[0]
        else:
            uf[xy[0]] = yp[0]
    elif xin:
        uf[xy[1]] = xp[0]
    elif yin:
        uf[xy[0]] = yp[0]
    else:
        uf[xy[0]] = None
        uf[xy[1]] = xy[0]
count = 0
for i in range(n):
    num = i + 1
    pi = p[i]
    if sum(set(dfs(num)) & set(dfs(pi))) > 0:
        count += 1
    elif num == pi:
        count += 1
print(count)

### FIXED CODE ###

n, m = map(int, input().split())
p = list(map(int, input().split()))
xys = [list(map(int, input().split())) for _ in range(m)]
mapp = [[] for _ in range(n + 1)]
for xy in xys:
    mapp[xy[0]].append(xy[1])
    mapp[xy[1]].append(xy[0])

bfs = [0] * (n + 1)
count = 0
for i in range(1, n + 1):
    nexts = mapp[i]
    if not(bfs[i]):
        bfs[i] = i
    for j in nexts:
        if bfs[j]:
            continue
        bfs[j] = i
        nexts += mapp[j]
    if bfs[i] == bfs[p[i - 1]]:
        count += 1
print(count)
def odd_even():
    i = list(map(int, input().split()))
    if (i[0] * i[1]) % 2 == 0:
        print('Even')
    else:
        print('Odd')

if __name__=__main__:
    odd_even()

### FIXED CODE ###

def odd_even():
    i = list(map(int, input().split()))
    if (i[0] * i[1]) % 2 == 0:
        print('Even')
    else:
        print('Odd')

if __name__=='__main__':
    odd_even()
from collections import Counter

n = int(input())
vs = input().split()
even = []
odd = []
if not even == odd:
  for i, v in enumerate(vs):
  	if i % 2 == 0:
    	even.append(int(v))
    else:
    	odd.append(int(v))
  counter_even = Counter(even)
  counter_odd = Counter(odd)

  res = len(even) - counter_even.most_common()[0][1] + len(odd) - counter_odd.most_common()[0][1] 
else:
  res = len(even)
print(res)

### FIXED CODE ###

from collections import Counter

n = int(input())
vs = input().split()
even = []
odd = []

if len(list(set(vs))) == 1:
  res = int(len(vs) / 2)
else:
  for i, v in enumerate(vs):
    if i % 2 == 0:
      even.append(int(v))
    else:
      odd.append(int(v))
  counter_even = Counter(even)
  counter_odd = Counter(odd)
  if counter_even.most_common()[0][0] != counter_odd.most_common()[0][0]:
    res = len(even) - counter_even.most_common()[0][1]  + len(odd) - counter_odd.most_common()[0][1]
  else:
    res1 = len(even) - counter_even.most_common()[1][1]  + len(odd) - counter_odd.most_common()[0][1]
    res2 = len(even) - counter_even.most_common()[0][1]  + len(odd) - counter_odd.most_common()[1][1]
    res = min(res1, res2)
print(res)
n = int(input())
a = [int(input()) for _ in range(n)]
eva = float('inf')
for i in a:
  count = 0
  while i%2 == 0:
    i //= 2
    count += 1
  eva = min(count, eva)
print(eva)

### FIXED CODE ###

n = int(input())
a = map(int, input().split())
m = float('inf')
for i in a:
  count = 0
  while i%2 == 0:
    i = i//2
    count += 1
  m = min(m, count)
  if m==0:
    break
print(m)
n = int(input())
s = input()
t1 = [0]*n
t2 = [0]*n
for i in range(1, n):
  t1[i] = t1[i-1] + s[i-1] == 'W'
  t2[n-i-1] = t2[n-i] + s[n-i] == 'E'
ans = n
for i in range(n):
  ans = min(ans, t1[i] + t2[i])
print(ans)

### FIXED CODE ###

n = int(input())
s = input()
t1 = [0]*n
t2 = [0]*n
for i in range(1, n):
  t1[i] = t1[i-1] + (s[i-1] == 'W')
  t2[n-i-1] = t2[n-i] + (s[n-i] == 'E')
ans = n
for i in range(n):
  ans = min(ans, t1[i] + t2[i])
print(ans)
N = int(input())
a = [0]+list(map(int,input().split())+[0]
A =[abs(a[i]-a[i+1]) for i in range(N+1)]
total = sum(A)
             
for i in range(N):
    print(total+abs(a[i+2]-a[i])-A[i]-A[i+1])

### FIXED CODE ###

n = int(input())
a = [0] + list(map(int, input().split())) + [0]
A = [abs(a[i] - a[i + 1]) for i in range(n + 1)]
total = sum(A)

for i in range(n):
    print(total + abs(a[i + 2] - a[i]) - A[i] - A[i + 1])
import sys

MAX = 100002

sys.setrecursionlimit(MAX)

def add_num(nn, x):
    #print(nn)
    if nn == -1:
        return
    Tree[nn][0] += x
    for ni in Tree[nn][1]:
        add_num(ni, x)


ipt = input().split()
N = int(ipt[0])
Q = int(ipt[1])



Tree = []
for m in range(MAX):
    Tree.append([-1, [-1]])

nodes = set()
nodes.add(1)

for i in range(N-1):
    ipti = input().split()
    a = int(ipti[0])
    b = int(ipti[1])

    Tree[a][0] = 0
    Tree[a][1].insert(0, b)
    Tree[b][0] = 0

    nodes.add(b)




for i in range(Q):
    ipti = input().split()
    p = int(ipti[0])
    x = int(ipti[1])
    add_num(p, x)


for n in nodes:
    print(Tree[n][0])








### FIXED CODE ###

ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))
import sys

sys.setrecursionlimit(500000)

visited = set()

n,q = nm()

tval = [0]*n
t = [[] for _ in range(n)]

for i in range(n-1):
    a,b = nm()
    t[a-1].append(b-1)
    t[b-1].append(a-1)

for i in range(q):
    p,x = nm()
    tval[p-1] += x

ans = [0]*n
def dfs(num,val):
    visited.add(num)
    now = val+tval[num]
    ans[num] = now
    for nn in t[num]:
        if nn not in visited:
            dfs(nn,now)

dfs(0,0)

for i in ans:
    print(i)
from fractions import gcd
k=int(input())
ans=0
for i in range(1,k+1):
  for j in range(1,k+1):
    for l in range(1,k+1):
        a=(i,j,l)
        if len(a)==1:
            ans=ans+i
        elif len(a)==2:
            ans=ans+fractions.gcd(a[0],a[1])
        else:
            ans=ans+fractions.gcd(a[0],fractions.gcd(a[1],a[2]))
print(ans)

### FIXED CODE ###

from math import gcd
k=int(input())
ans=0
for i in range(1,k+1):
  for j in range(1,k+1):
    for l in range(1,k+1):
      ans=ans+gcd(gcd(i,j),l)
print(ans)
n, d = map(int, input().split())
count = 0
for i in range(n):
  a, b = map(int, input().split())
  if ((a+b)*(a-b))**(1/2) <= d:
    count += 1
print(count)

### FIXED CODE ###

n, d = map(int, input().split())
count = 0
for i in range(n):
  a, b = map(int, input().split())
  k = a**2 + b**2
  if k >= 0:
  	if (k)**(1/2) <= d:
  		count += 1
print(count)
from sys import stdin, stdout
n,m = map(int, stdin.readline().rstrip().split())
l = list(map(int, stdin.readline().rstrip().split()))
sum = sum(l)
k = sum/(4*m)
ans = "Yes"
l.sort(reverse = True)
for i in (0,m):
  if l[i]<k:
      ans="No"
      break
print(ans)

### FIXED CODE ###

n, m = map(int, input().split())
a = list(map(int, input().split()))
ans = "Yes"
 
a.sort(reverse=True)
 
for i in range(m):
    if a[i] < sum(a)*(1/(4*m)):
        ans = "No"
        break
 
print(ans)
#-*-ccoding:utf-8-*-
k = int(input())
a, b = map(int, input().split())
is_ok = False

for i in range(a,b+1):
  if i % k == 0:
    is_ok = true
    
if is_ok:
  print("OK")
else:
  print("NG")

### FIXED CODE ###

#-*-ccoding:utf-8-*-
k = int(input())
a, b = map(int, input().split())
is_ok = False
 
for i in range(a,b+1):
  if i % k == 0:
    is_ok = True
    
if is_ok:
  print("OK")
else:
  print("NG")
t=list(map(int,input().split()))
a=list(map(int,input().split()))
b=list(map(int,input().split()))
p,q=[0],[0]
w=[]
for i in range(4):
    p.append(p[-1]+a[i%2]*t[i%2])
    q.append(q[-1]+b[i%2]*t[i%2])
    w.append(p[i+1]-q[i+1])
    if w[-1]==0:
        print("infinity")
        exit()
    if i==3:
        if len(set(w))!=4:
            print("infinity")
            exit()
        chk=0
        if (w[0]>0 and w[1]>0) or (w[0]<0 and w[1]<0):
            print(0)
            exit()
        elif (w[0]>0 and w[1]<0 and w[2]<0) or (w[0]<0 and w[1]>0 and w[2]>0):
            print(1)
            exit()
        elif (w[0]>0 and w[1]<0 and w[2]>0 and w[3]>0) or (w[0]<0 and w[1]>0 and w[2]<0 and w[3]<0):
            print(2)
            exit()
print(w)
#print(max(abs(w[0]//(w[2]-w[0])),abs(w[1]//(w[3]-w[1])))*2-1)
x,y=(abs(w[0]//(w[2]-w[0])),abs(w[1]//(w[3]-w[1])))
if w[0]<w[2] and w[1]<w[3] and w[0]<0:
    print(x*2-1)
elif w[0]<w[2] and w[1]<w[3] and w[0]>0:
    print(x*2)
else:
    print(hhh)

### FIXED CODE ###

T1, T2 = map(int, input().split())
A1, A2 = map(int, input().split())
B1, B2 = map(int, input().split())
 
P = (A1 - B1) * T1
Q = (A2 - B2) * T2
if P > 0:
	P *= -1
	Q *= -1
 
if P + Q < 0:
	print(0)
elif P + Q == 0:
	print("infinity")
else:
	S = (-P) // (P + Q)
	T = (-P) % (P + Q)
	if T != 0:
		print(S * 2 + 1)
	else:
		print(S * 2)
import numpy as np


l, r = map(int, input().split())
#if l <= 2019 <= r:
#    print(0)
#    exit()
#elif r*(r-1) <= 2019:
#    print(l*(l+1))
#    exit()

lr = np.arange(l, r+1)
lr = np.reshape(lr, (1, len(lr)))
lr_t = lr.T
mod = (lr*lr_t) % 2019
mod = mod[np.triu_indices_from(mod, k=1)]

print(mod.min())

### FIXED CODE ###

l, r = map(int, input().split())
if l <= 2019 <= r:
    print(0)
    exit()
elif r*(r-1) <= 2019:
    print(l*(l+1))
    exit()

a = 2019
for i in range(l, r):
    for j in range(i+1, r+1):
        a = min(a, i*j%2019)
        if a == 0:
            print(0)
            exit()

print(a)
n, k = map(int, input().split())
print(n*((n-1)**(k-1)))


### FIXED CODE ###

a = set(list(map(int, input().split())))
print(len(a))
# -*- coding: utf-8 -*-
N= int(input())
A= list(map(int,input().split(' ')))
 
ans = 0
for i in range(len(A)-1):
  for j in range(i+1, len(A)):
    ans += A[i]*A[j]
 
print(ans)


### FIXED CODE ###

# -*- coding: utf-8 -*-
D, T, S= map(int,input().split(' '))
print('Yes' if D / S <= T else 'No')
weather = ["Sunny", "Cloudy", "Rainy", "Sunny"]
s = input()
for c, i in weather:
    if i == s:
        print(weather[c+1])
        break

### FIXED CODE ###

weather = ["Sunny", "Cloudy", "Rainy", "Sunny"]
s = input()
for c, i in enumerate(weather):
    if i == s:
        print(weather[c+1])
        break
N = int(input())
V = list(map(int, input().split()))
C = list(map(int, input().split()))

diff = [0 for i in range(N)]
for i in range(N):
  diff[i] = V[i] - C[i]
diff = list(reversed(sorted(diff)))
i, ans = 0, 0
while diff[i] > 0:
  ans += diff[i]
  i += 1
print(max(ans, 0))

### FIXED CODE ###

N = int(input())
V = list(map(int, input().split()))
C = list(map(int, input().split()))

ans = 0
for i in range(N):
  if V[i] > C[i]:
    ans += V[i] - C[i]
print(ans)
S = list(input())

Odd = S[0::2]
Even = S[1::2]

for i in range(len(Odd)):
    if 'L' == Odd[i]:
        c = 0
    else:
        for j in range(len(Even)):
            if 'R' == Even[j]:
                c = 0
            else:
                c = 1

if c == 1:
    print('Yes')
else:
    print('No')

### FIXED CODE ###

S = list(input())

Odd = S[0::2]
Even = S[1::2]

for i in range(len(Odd)):
    if 'L' == Odd[i]:
        l = 0
        break
    else:
        l = 1

r = 1
for j in range(len(Even)):
    if 'R' == Even[j]:
        r = 0
        break
    else:
        r = 1

if l+r == 2:
    print('Yes')
else:
    print('No')
d, g = (int(i) for i in input().split())
p = [[int(i) for i in input().split()] for i in range(n)]
ans = 1000

for i in range(1 << d): # 4
    score = 0
    num = 0
    rest = 0
    for j in range(d): # 2
        if ((i >> j) & 1) == 1: # 0 1  2=10
            score += p[j][0] * 100 * (j + 1) + p[j][1]
            num += p[j][0]
            print("num:",num)
        else:
            rest = j
            print("rest:",rest)
    if score < g:
        need = (g - score) // (100*(rest+1))
        if need > p[rest][0] - 1: # 0だったところの中で一番大きいところの問題数-1よりも必要手数が多かったら初めに戻る
            print("need:", need)
            continue
        else:
            num += need
            print("num2:",num)
    ans = min(ans, num)
    print("ans:", ans)
print(ans)

### FIXED CODE ###

d, g = (int(i) for i in input().split())
p = [[int(i) for i in input().split()] for i in range(d)]
ans = 1000

for i in range(1 << d): # 4
    score = 0
    num = 0
    rest = 0
    for j in range(d): # 2
        if ((i >> j) & 1) == 1: # 0 1  2=10
            score += p[j][0] * 100 * (j + 1) + p[j][1]
            num += p[j][0]
        else:
            rest = j
    if score < g:
        need = (g - score - 1) // (100*(rest+1)) + 1
        if need > p[rest][0] - 1: # 0だったところの中で一番大きいところの問題数-1よりも必要手数が多かったら初めに戻る
            continue
        else:
            num += need
    ans = min(ans, num)
print(ans)
n, m, k = map(int, input().split())

mod = 998244353

def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

N = 10**5
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )

ans = 0
for i in range(k+1):
    col = m * pow(m-1,n-i-1,mod)
    match = cmb(n-1,i,mod)
    ans += col * match

print(ans % mod)

### FIXED CODE ###

n, m, k = map(int, input().split())
 
mod = 998244353
 
def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod
 
N = 10**6
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル
 
for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )
 
ans = 0
for i in range(k+1):
    col = m * pow(m-1,n-i-1,mod)
    match = cmb(n-1,i,mod)
    ans += col * match
 
print(ans % mod)
inpl = lambda: map(int,input().split())
N = int(input())
if N == 1:
  print('Hello World')
else:
  A, B = inpl()
  print(A+B)

### FIXED CODE ###

N = int(input())
if N == 1:
  print('Hello World')
else:
  A = int(input())
  B = int(input())
  print(A+B)
import numpy as np
from operator import itemgetter

N = int(input())

list = []
for i in range(N):
    a,b=input().split()
    list.append([a, int(b),i+1])
sort1 = sorted(list, key=itemgetter(0))
if N == 1:
  print(1)
else:
  ans = []
  kari = []
  end = 1
  k = 1
  for i in range(0,N-1):
    if end == 1:
      kari.append(sort1[i])
      else:
        k = 0
        if sort1[i][0] == sort1[i+1][0]:  
          kari.append(sort1[i+1])
          end = 0
          if i+1 > N-2:
            kari.sort(key=itemgetter(1),reverse=True)
            ans.extend(kari)

            else:
              kari.sort(key=itemgetter(1),reverse=True)
              ans.extend(kari)
              if i+1 > N-2:
                ans.append(sort1[i+1])
                kari = []
                end = 1
  print(sort1)
  print(ans)
  for i in range(0,len(ans)):
      print(ans[i][2])

### FIXED CODE ###

import numpy as np
from operator import itemgetter
 
N = int(input())
 
list = []
for i in range(N):
    a,b=input().split()
    list.append([a, int(b),i+1])
sort1 = sorted(list, key=itemgetter(0))
if N == 1:
  	print(1)
else:
    ans = []
    kari = []
    end = 1
    k = 1
    for i in range(0,N-1):
        if end == 1:
            kari.append(sort1[i])
        else:
            k = 0
        if sort1[i][0] == sort1[i+1][0]:  
            kari.append(sort1[i+1])
            end = 0
            if i+1 > N-2:
            	kari.sort(key=itemgetter(1),reverse=True)
            	ans.extend(kari)

        else:
            kari.sort(key=itemgetter(1),reverse=True)
            ans.extend(kari)
            if i+1 > N-2:
            	ans.append(sort1[i+1])
            kari = []
            end = 1
    
    for i in range(0,len(ans)):
        print(ans[i][2])
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys

input = sys.stdin.readline

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

    def f(i):
        if i >= N:
            return 0

        n = 0
        m = min(A[i], B[i])
        n += m
        A[i] -= m
        B[i] -= m
        m = min(A[i+1], B[i])
        n += m
        A[i+1] -= m
        B[i] -= m
        return n + f(i+1)

    n = f(0)
    print(n)

main()


### FIXED CODE ###

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys

input = sys.stdin.readline

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

    n = 0
    for i in range(N):
        m = min(A[i], B[i])
        n += m
        A[i] -= m
        B[i] -= m
        m = min(A[i+1], B[i])
        n += m
        A[i+1] -= m
        B[i] -= m

    print(n)

main()
N=int(input())
import numpy as np
A=np.zeros(N)
B=np.zeros(N)
for i in range(0,N):
  A[i],B[i] = map(int,input().split())
  
W=np.zeros((N,np.max(B)))
for i in range(0,N):
  for j in range(int(B[i]-A[i]),int(B[i])):
    W[i][j]=1
    
for b in B:
  if np.sum(W[:,0:b]) > b:
    print('No')
    break
else:
  print('Yes')

    

### FIXED CODE ###

import sys
import bisect
import numpy as np
input = sys.stdin.readline
N=int(input())
A=[0]*N
B=[0]*N
for i in range(N):
    A[i],B[i]=[int(x) for x in input().split()]
matrix = np.vstack((np.array(A),np.array(B)))
matrix = matrix[:, np.argsort(matrix[1])]
time=0
capacity=0
for a,b in zip(matrix[0],matrix[1]):
    capacity += (b-time) - a
    time = b
    if capacity <0:
        print('No')
        sys.exit()
    #print(capacity,a,b)
print('Yes')
S = str(input())
if S = "ABC":
  print("ARC")
else:
  print("ABC")

### FIXED CODE ###

S = input()
if S == "ABC":
  print("ARC")
else:
  print("ABC")
from collections import defaultdict

N = int(input())
S = [0] * N
P = [0] * N

city_point = {}

for i in range(N):
    inp = input().split()
    S[i] = inp[0]
    P[i] = inp[1]
    city_point[i] = {'city': S[i], 'point': int(P[i])}
    
sorted_list = sorted(city_point.items(), key=lambda x: (x[1]['city'], x[1]['point']))

for key in sorted_list:
    print(key + 1)


### FIXED CODE ###

N = int(input())

cookbook = [0] * N

for n in range(N):
    line = input().split()
    s = line[0]
    p = int(line[1])
    cookbook[n] = (n + 1, s, p)

l = sorted(cookbook, key = lambda x: (x[1], -x[2]))
for item in l:
    print(item[0])
n=int(input())
a=list(map(int,input().split()))
checker=0
count=0
for i in range(len(a)):
  if a[i]%2==0:
    checker+=1
    if a[i]%3==0 or a[i]%5==0:
      count+=1
if checker==count:
  print("APPROVED")
else:
  print("DENIED)

### FIXED CODE ###

n=int(input())
a=list(map(int,input().split()))
checker=0
count=0
for i in range(len(a)):
  if a[i]%2==0:
    checker+=1
    if a[i]%3==0 or a[i]%5==0:
      count+=1
if checker==count:
  print("APPROVED")
else:
  print("DENIED")