Problem:
 Score : 300 points    Problem Statement There are N+1 towns. The i -th town is being attacked by A_i monsters.  We have N heroes. The i -th hero can defeat monsters attacking the i -th or (i+1) -th town, for a total of at most B_i monsters.  What is the maximum total number of monsters the heroes can cooperate to defeat?      Constraints  All values in input are integers.  1 \leq N \leq 10^5  1 \leq A_i \leq 10^9  1 \leq B_i \leq 10^9         Input Input is given from Standard Input in the following format:  N  A_1  A_2  ...  A_{N+1}  B_1  B_2  ...  B_N       Output Print the maximum total number of monsters the heroes can defeat.        Sample Input 1 2
3 5 2
4 5      Sample Output 1 9  If the heroes choose the monsters to defeat as follows, they can defeat nine monsters in total, which is the maximum result.   The first hero defeats two monsters attacking the first town and two monsters attacking the second town.  The second hero defeats three monsters attacking the second town and two monsters attacking the third town.        Sample Input 2 3
5 6 3 8
5 100 8      Sample Output 2 22       Sample Input 3 2
100 1 1
1 100      Sample Output 3 3

Solution (Python):
N = int( input() )
A, B = list( map(lambda x: int(x), input().split(" ")) ), list( map(lambda x: int(x), input().split(" ")) )


 
def kill_monsters(Ai, Bi):

    Ai -= Bi
    Bi = 0

    if Ai < 0:
        Bi += -1*Ai
        Ai = 0

    return (Ai, Bi)
 
def main(local_A, local_B, lim):
 
    for i in range(lim):
        local_A[i], local_B[i] = kill_monsters(local_A[i], local_B[i])
 
        if 0 < local_B[i]:
            local_A[i+1], local_B[i] = kill_monsters(local_A[i+1], local_B[i])
 
    return [local_A, local_B]

print( sum(A) - sum(main(A, B, N)[0]) )

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

ans = 0
for i in range(n):
    tmp = min(a[i], b[i])
    ans += tmp
    a[i] -= tmp
    b[i] -= tmp
    tmp = min(a[i+1], b[i])
    ans += tmp
    a[i+1] -= tmp
    b[i] -= tmp
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

def main():
	n = int(input().strip())
	a = list(map(int, input().strip().split()))
	b = list(map(int, input().strip().split()))
	s = 0
	for i in range(n):
		l = min(a[i], b[i])
		a[i] -= l
		b[i] -= l
		r = min(a[i+1], b[i])
		a[i+1] -= r
		b[i] -= r
		s += l + r
	print(s)

if __name__ == '__main__':
	main()

Solution (Python):
num_input = input()
listA_input = input().split(" ")
listB_input = input().split(" ")
N = int(num_input)
allkenemies = 0
A = []
B = []
for n_A in listA_input:
  A.append(int(n_A))
for n_B in listB_input:
  B.append(int(n_B))
for num in range(0,N):
  kenemies = min(A[num],B[num])
  allkenemies += kenemies
  A[num] -= kenemies
  B[num] -= kenemies
  kenemies = min(A[num+1],B[num])
  allkenemies += kenemies
  A[num+1] -= kenemies
print(allkenemies)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = sum(a)
for i in range(n):
    if b[i]>=a[i]+a[i+1]:
        a[i]=0
        a[i+1]=0
    elif b[i]>=a[i]:
        a[i+1]-=b[i]-a[i]
        a[i]=0
    else:
        a[i]-=b[i]
print(c-sum(a))

Solution (Python):



def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    ans = 0

    for i in range(n):
        left = min(a[i], b[i])
        ans += left
        b[i] -= left

        right = min(a[i + 1], b[i])
        ans += right
        a[i + 1] -= right

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
R = lambda: map(int, input().split())
input()
a = list(R())
def f():
    s = 0
    for x, y, z in zip(R(), a, a[1:]):
        t = min(x, y - s)
        s = min(z, x - t)
        yield s + t
print(sum(f()))

Solution (Python):
R = lambda: map(int, input().split())
input()
a = list(R())
def f():
    y = 0
    for u, v, w in zip(R(), a, a[1:]):
        x = min(v - y, u)
        y = min(w, u - x)
        yield x + y
print(sum(f()))

Solution (Python):
R = lambda: map(int, input().split())
input()
a = list(R())
r = y = 0
for u, v, w in zip(R(), a, a[1:]):
    x = min(v - y, u)
    y = min(w, u - x)
    r += x + y
print(r)

Solution (Python):
from itertools import tee

R = lambda: map(int, input().split())
input()
(a, b), c = tee(R()), R()
next(b)
r = y = 0
for u, v, w in zip(c, a, b):
    x = min(v - y, u)
    y = min(w, u - x)
    r += x + y
print(r)

Solution (Python):
R = lambda: map(int, input().split())
input()
a, b = list(R()), R()
r = y = 0
for u, v, w in zip(b, a, a[1:]):
    x = min(v - y, u)
    y = min(w, u - x)
    r += x + y
print(r)

Solution (Python):
R = lambda: map(int, input().split())
input()
a, b = list(R()), R()
r = 0
for i, u in enumerate(b):
    x = min(a[i], u)
    y = min(a[i + 1], u - x)
    a[i + 1] -= y
    r += x + y
print(r)

Solution (Python):
R = lambda: list(map(int, input().split()))
n = int(input())
a, b = R(), R()
r = 0
for i in range(n):
    x = min(a[i], b[i])
    y = min(a[i + 1], b[i] - x)
    a[i + 1] -= y
    r += x + y
print(r)

Solution (Python):
N=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))

ans=0

for i in range(N):
    if a[i] >= b[i]:
        ans += b[i]

    else:
        ans += a[i]
        yo = b[i]-a[i]

        if a[i+1]<= yo:
            ans += a[i+1]
            a[i+1]=0
        else:
            ans += yo
            a[i+1] -= yo
          
print(ans)

Solution (Python):

n=int(input())
mns=list(map(int,input().split()))
hrs=list(map(int,input().split()))
mk=0
for i in range(n):
  if(hrs[i]>=mns[i]+mns[i+1]):
    mk+=mns[i]+mns[i+1]
    mns[i]=0
    mns[i+1]=0
  else:
    if(hrs[i]<=mns[i]):
      mk+=hrs[i]
    else:
      mk+=hrs[i]
      mns[i+1]-=(hrs[i]-mns[i])
print(mk)

Solution (Python):
n=int(input())
t=[int(i) for i in input().split()]
a=[int(i) for i in input().split()]
c=0
for i in range(n):
    if a[i]<=t[i]+t[i+1]:
        c+=a[i]
    else:
        c+=t[i]+t[i+1]
    if a[i]>=t[i]:
        a[i]-=t[i]
        t[i]=0
    else:
        t[i]-=a[i]
        a[i]=0
    if a[i]>=t[i+1]:
        a[i]-=t[i+1]
        t[i+1]=0
    else:
        t[i+1]-=a[i]
        a[i]=0
print(c)

Solution (Python):


n = int(input())
A = [int(el) for el in input().split(' ')]
B = [int(el) for el in input().split(' ')]

total = 0
for i in range(n):
    if i == 0:
        f = min(A[0], B[0])
        g = min(A[1], B[0]-f)
        total += f + g
    else:
        f = min(A[i] - g, B[i])
        g = min(A[i+1], B[i] - f)
        total += f + g



print(total)

Solution (Python):
N = int(input())
m = list(map(int, input().split()))
b = list(map(int, input().split()))
s = 0
for i in range(N):
  l = min(m[i],b[i])
  s += l
  m[i] -= l
  b[i] -= l
  
  r = min(m[i+1],b[i])
  s += r
  m[i+1] -= r
print(s)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

ans = 0
for i in range(N):
    if B[i] <= A[i]:
        ans += B[i]
        A[i] -= B[i]
    else:
        ans += A[i] + min(B[i] - A[i], A[i + 1])
        A[i + 1] = max(A[i + 1] - (B[i] - A[i]), 0)
        A[i] = 0

print(ans)

Solution (Python):
class ABC135:
    def __init__(self):
        pass

    def solve(self, N):
        ans = 0
        A = list(map(int, input().split()))
        B = list(map(int, input().split()))
        for i in range(len(B)):
            diff = min(A[i], B[i]);
            A[i] -= diff
            B[i] -= diff
            ans += diff
            diff = min(A[i+1], B[i]);
            A[i+1] -= diff
            B[i] -= diff
            ans += diff
        print(ans)

if __name__ == '__main__':
    N = int(input())
    problem = ABC135()
    problem.solve(N)

Solution (Python):
import sys
input = sys.stdin.readline

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

    tmp = 0
    ans = 0
    for i in range(n):
        mon = max(0, a[i] - tmp)

        if b[i] <= mon:
            ans += b[i] + tmp
            tmp = 0
        else:
            ans += a[i]
            tmp = b[i] - mon
            
    if tmp <= a[i + 1]:
        ans += tmp
    else:
        ans += a[i + 1]
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
monsters = list(map(int, input().split()))
savers = list(map(int, input().split()))
ko = 0

for i in range(n):
    left = min(monsters[i], savers[i])
    ko += left
    monsters[i] -= left
    savers[i] -= left

    right = min(monsters[i+1], savers[i])
    ko += right
    monsters[i+1] -= right
    savers[i] -= right

print(ko)

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
ans = 0
for i in range(n):
  if a[i]>=b[i]: ans+=b[i]
  else:
    ans+=min(b[i],a[i]+a[i+1])
    a[i+1] = max(0,a[i]+a[i+1]-b[i])
print(ans)

Solution (Python):
n = int(input())
enemy = list(map(int, input().split()))
beat = list(map(int, input().split()))
ans = 0 
for i in range(n):
  
  left = min([enemy[i], beat[i]])
  ans += left
  beat[i] -= left
  
  right = min([enemy[i+1], beat[i]])
  ans += right
  enemy[i + 1] -= right
  beat[i] -= right
  
print(ans)

Solution (Python):
n = int(input())
enemy = list(map(int, input().split()))
beat = list(map(int, input().split()))
ans = 0 
for i in range(n):
  
  left = min([enemy[i], beat[i]])
  ans += left
  enemy[i] -= left
  beat[i] -= left
  
  right = min([enemy[i+1], beat[i]])
  ans += right
  enemy[i + 1] -= right
  beat[i] -= right
  
print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=0
for i in range(n):
    if(a[i]>b[i]):
        a[i]=a[i]-b[i]
        c=c+b[i]
        b[i]=0
    else:
        b[i]=b[i]-a[i]
        if(a[i+1]>b[i]):
            a[i+1]=a[i+1]-b[i]
            c=c+b[i]
            b[i]=0
        else:
            b[i]=b[i]-a[i+1]
            c=c+a[i+1]
            a[i+1]=0
        c=c+a[i]
        a[i]=0
print(c)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
total = 0
before = 0
for i in range(n + 1):
    nowkill = 0
    if i != n:
        nowkill = b[i]
    nowneedkill = a[i]
    if nowneedkill <= before:
        before = nowkill
        total = total + nowneedkill
    else:
        nowkill = before + nowkill
        if nowkill > nowneedkill:
            before = nowkill - nowneedkill
            total = total + nowneedkill
        else:
            total = total + nowkill
            before = 0
print(total)

Solution (Python):
N = int(input())

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

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

ans = 0
for i in range(len(hero)):
	if mon[i]+mon[i+1] >= hero[i]:
		ans += hero[i]
		mon[i+1] = min(mon[i+1],mon[i]+mon[i+1]-hero[i])
	else:
		ans += mon[i]+mon[i+1]
		mon[i+1] = 0

print(ans)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))

z=0
for i in range(0,N):
  if A[i]>=B[i]:
    z=z+B[i]
  else:
    z=z+A[i]
    if B[i]-A[i]<=A[i+1]:
      z=z+B[i]-A[i]
      A[i+1]=A[i+1]-(B[i]-A[i])
    else:
      z=z+A[i+1]
      A[i+1]=0

print(z)

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

ans = 0
for i in range(n):
    left = min(a[i], b[i])
    ans += left
    a[i] -= left
    b[i] -= left
    right = min(a[i + 1], b[i])
    ans += right
    a[i + 1] -= right
    b[i] -= right

print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = 0

for i in range(N):
    a = min(A[i], B[i])
    b = min(A[i+1], B[i] - a)
    A[i+1] -= b
    ans += (a + b)

print(ans)

Solution (Python):
N = int(input())
M = list(map(int, input().split()))
H = list(map(int, input().split()))

sum_m = 0
rest = 0
for i in range(N):
  if M[i] >= H[i]+rest:
    sum_m += H[i]+rest
    rest = 0
  else:
    sum_m += M[i]
    rest = min(H[i], H[i]+rest-M[i])
sum_m += min(rest, M[-1])

print(sum_m)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

ans = 0
for i in range(n):
    left = min(a[i], b[i])
    ans += left
    a[i] -= left
    b[i] -= left

    right = min(a[i+1], b[i])
    ans += right
    a[i+1] -= right
    b[i] -= right


print(ans)

Solution (Python):
N = int(input())
M = list(map(int, input().split()))
H = list(map(int, input().split()))

sum_m = 0
killed = 0
rest = 0
for i in range(0, N):
  killed = min(H[i]+rest, M[i])
  rest = min(H[i]+rest-killed, H[i])
  sum_m += killed
sum_m += min(rest, M[-1])
print(sum_m)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
count = 0
for i in range(n-1,-1,-1):
    
    if a[i+1] < b[i]:
        count += a[i+1]
        b[i] -= a[i+1]
        a[i+1] = 0
    else:
        count += b[i]
        a[i+1] -= b[i]
        b[i] = 0
    
    if a[i] < b[i]:
        count += a[i]
        b[i] -= a[i]
        a[i] = 0
    else:
        count += b[i]
        a[i] -= b[i]
        b[i] = 0
print(count)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
count = 0
for i in range(n-1,-1,-1):
    
    if a[i+1] < b[i]:
        count += a[i+1]
        b[i] -= a[i+1]
        a[i+1] = 0
    else:
        count += b[i]
        a[i+1] -= b[i]
        b[i] = 0
    
    if a[i] < b[i]:
        count += a[i]
        b[i] -= a[i]
        a[i] = 0
    else:
        count += b[i]
        a[i] -= b[i]
        b[i] = 0
print(count)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
count = 0
for i in range(n):
    if a[i] < b[i]:
        count += a[i]
        b[i] -= a[i]
        a[i] = 0
    else:
        count += b[i]
        a[i] -= b[i]
        b[i] = 0
    if a[i+1] < b[i]:
        count += a[i+1]
        b[i] -= a[i+1]
        a[i+1] = 0
    else:
        count += b[i]
        a[i+1] -= b[i]
        b[i] = 0
print(count)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0

def f(n):
    nn = min(a[n], b[n])
    nnn = min(a[n+1], b[n] - nn)
    a[n+1] -= nnn
    return nn + nnn

for i in range(n):
    ans += f(i)

print(ans)

Solution (Python):
n=int(input())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]

k=0
for i,j in enumerate(b):
  if a[i]+a[i+1]<=j:
    k+=a[i]+a[i+1]
    a[i],a[i+1]=0,0
  elif a[i]<=j:
    k+=j
    a[i],a[i+1]=0,a[i]+a[i+1]-j
  else:
    k+=j
    a[i]-=j
print(k)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

initial = sum(A)
for i in range(1,N+1):
  diff1 = B[-i] - A[-i]
  if (diff1 >= 0):
    A[-i] = 0
    B[-i] = diff1
  else:
    A[-i] = - diff1
    B[-i] = 0
    
  diff2 = B[-i] - A[-i-1]
  if (diff2 >= 0):
    A[-i-1] = 0
    B[-i] = diff2
  else:
    A[-i-1] = - diff2
    B[-i] = 0
    
print(initial - sum(A))

Solution (Python):
N = int(input())
M = list(map(int, input().split()))
H = list(map(int, input().split()))

sum_m = 0
killed = 0
rest = 0
for i in range(0, N):
  killed = min(H[i]+rest, M[i])
  rest = max(0, min(H[i]+rest-killed, H[i]))
  sum_m += killed
sum_m += min(rest, M[-1])
print(sum_m)

Solution (Python):

def main():
    N = int(input())
    A = list(map(int,input().split()))
    B = list(map(int,input().split()))
    ans = 0
    for i in range(N):
        left = min(A[i],B[i])
        ans+=left
        A[i]-=left
        B[i]-=left
        right = min(A[i+1],B[i])
        ans+=right
        A[i+1]-=right
        B[i]-=right
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))

ans = 0
tmp = 0
for a,b in zip(As,Bs[:N]):
    
    if tmp > 0:
        if a >= tmp:
            ans += tmp
            a -= tmp
        else:
            ans += a
            a = 0
    
    if a >= b:
        ans += b
        tmp = 0
    else:
        ans += a
        tmp = b-a

a = As[N]
if a >= tmp:
    ans += tmp
else:
    ans += a

print(ans)

Solution (Python):

n = int(input())

a = list(map(int,input().split()))
b = list(map(int,input().split()))
cnt = 0
left = 0
for i in range(n):
    a1,b1 = a[i],b[i]
    if a1 <= b1 + left:
        cnt += a1
        left = min(b1 - a1 + left,b1)
    else:
        cnt += b1 + left
        left = 0
if a[n] <= left:
    cnt += a[n]
else:
    cnt += left
print(cnt)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
ans += min(a[0], b[0])
b[0] = max(0,b[0]-a[0])
ans += min(a[-1], b[-1])
b[-1] = max(0,b[-1]-a[-1])
i1 = 0
i2 = b[0]
for i in range(n-1):
  i1 = i2
  i2 = b[i+1]
  ai = a[i+1]
  if ai <= i1:
    ans += ai
  elif ai <= i1 + i2:
    ans += ai
    i2 -= ai - i1
  else:
    ans += i1 + i2
    i2 = 0
print(ans)

Solution (Python):
N = int(input())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))

sumA = sum(As)

for i, B in enumerate(Bs):
    d = min(As[i], B)
    As[i] -= d
    B -= d
    d = min(As[i+1], B)
    As[i+1] -= d
    B -= d

print(sumA - sum(As))

Solution (Python):
N=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))

ans=0
for i in range(N):
    if(a[i]>=b[i]):
        ans+=b[i]
    else:
        ans+=a[i]
        nokori=b[i]-a[i]
        if(a[i+1]>=nokori):
            a[i+1]-=nokori
            ans+=nokori
        else:
            ans+=a[i+1]
            a[i+1]=0
       

print(ans)

Solution (Python):
n,*a=map(int,open(0).read().split())
c=0
for i in range(2*n):j=i*-n%len(a);m=min(a[j],a[j-n]);c+=m;a[j-n]-=m
print(c)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))

ans = 0

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

print(ans)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
count = 0
flag = 0
i = 0
while(i<len(a)-1):
	if b[i]<a[i]+a[i+1]:
		count = count+b[i]
		if a[i]<b[i]:
			a[i+1] = a[i]+a[i+1]-b[i]
		flag = 0
		i += 1

	else:
		if flag == 0:
			count = count+a[i]+a[i+1]
			a[i+1] = 0
			flag = 1
			i = i + 1

		else:
			count = count + a[i+1]
			a[i+1] = 0
			i = i + 1

print(count)

Solution (Python):
n,x,*a=map(int,open(0).read().split())
c=0
for a,b in zip(a,a[n:]):c+=min(x+a,b);x=a-max(min(b-x,a),0)
print(c)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))

A=list(reversed(A))
B=list(reversed(B))
cnt=0
tmp=0
for i in range(N):
    if A[i]>=B[i]:
        cnt+=B[i]
        tmp=0
    else:
        cnt+=A[i]
        tmp=min(B[i]-A[i],A[i+1])
        cnt+=tmp
        A[i+1]-=tmp
print(cnt)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

d = [0]*(n+1)
for i in range(n):
  if a[i] >= b[i]:
    d[i] += b[i]
  else:
    d[i] += a[i]
    if a[i+1] < b[i] -a[i]:
      d[i+1] = a[i+1]
      a[i+1] = 0
    else:
      d[i+1] = b[i] - a[i]
      a[i+1] -= b[i] -a[i]
      
print(sum(d))

Solution (Python):
import sys

raw_input = sys.stdin.readline

f = lambda : raw_input().rstrip()    
f1 = lambda : f().split()
f2 = lambda t: map(t,f1())

def main1():
    a,b = f2(int)
    if (a+b)%2 == 0:
       print (a+b)/2
    elif a == b:
       print 0
    else:
       print "IMPOSSIBLE"

def main2():
    n = int(f())
    a = f2(int)
    b = sorted(a)
    m = 0
    for i in xrange(n):
        if a[i] != b[i]:
            m = m + 1
    if m<=2:
        print "YES"
    else:
        print "NO"

def main3():
    n = int(f())
    a = f2(int)
    b = f2(int)
    ans = 0
    for i in xrange(n):
        if a[i] >= b[i]:
            ans = ans + b[i]
        elif a[i] < b[i] :
            ans = ans + a[i]
            b[i] = b[i] - a[i]
            ans = ans + min(a[i+1], b[i])
            a[i+1] = max(0,a[i+1] - b[i])  
    print ans 
 
def main4():
    pass
    
    
if __name__ == "__main__":
    main3()

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))

ans=0
for i in range(n):
    if a[i]>=b[i]:
        ans+=b[i]
    else:
        b[i]-=a[i]
        ans+=a[i]
        num = min(b[i],a[i+1])
        a[i+1]-=num
        ans+=num
print(ans)

Solution (Python):
def c_city_savers(N, A, B):
    ans = 0
    for i in range(N):
        t1 = min(A[i], B[i])  
        B[i] -= t1  
        t2 = min(A[i + 1], B[i])  
        A[i + 1] -= t2
        ans += t1 + t2
    return ans

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

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
cnt = 0
for i in range(N):
    beat = min(A[i], B[i])
    cnt += beat
    A[i] -= beat
    B[i] -= beat
    beat = min(A[i+1], B[i])
    cnt += beat
    A[i+1] -= beat
    B[i] -= beat
print(cnt)

Solution (Python):


N = input()
N = int(N)

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


B = []
B = input().split()
B = [int(n) for n in B]
B.append(0)






monster = 0

for i in range(N) :
	if A[i] < B[i] :
		monster = monster + A[i]
		B[i] = B[i] - A[i]
		if A[i+1] < B[i] :
			monster = monster + A[i+1]
			A[i+1] = 0
		elif A[i+1] >= B[i] :
			monster = monster + B[i]
			A[i+1] = A[i+1] - B[i]
	elif A[i] >= B[i] :
		monster = monster + B[i]

print(monster)

Solution (Python):
N=int(input())
A=[int(i) for i in input().split()]
C=[int(i) for i in input().split()]
X=[A[i] for i in range(N+1)]
for i in range(N):
    if X[i]<C[i]:
        C[i]-=X[i]
        X[i]=0
        X[i+1]=max(0,X[i+1]-C[i])
    else:
        X[i]-=C[i]
        C[i]=0
print(sum(A)-sum(X))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

bfr = 0
ans = 0

for i in range(2*n):
  idx = i//2
  if(i%2 == 0):
    nxt = min(a[idx]-bfr, b[idx])
  else:
    nxt = min(b[idx]-bfr, a[idx+1])
  ans += nxt
  bfr = nxt
  
print(ans)

Solution (Python):


def main():
    n = int(input())
    A = tuple(map(int, input().split()))
    B = tuple(map(int, input().split()))
    ans, a1 = 0, A[0]
    for a2, b in zip(A[1:], B):
        cnt1 = a1 if a1 < b else b
        x = b - cnt1
        cnt2 = a2 if a2 < x else x
        ans += cnt1 + cnt2
        a1 = a2 - cnt2  
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):


def main():
    n = int(input())
    A = tuple(map(int, input().split()))
    B = tuple(map(int, input().split()))
    ans, a1 = 0, A[0]
    for a2, b in zip(A[1:], B):
        cnt1 = a1 if a1 < b else b
        cnt2 = a2 if a2 < b - cnt1 else b - cnt1
        ans += cnt1 + cnt2
        a1 = a2 - cnt2  
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):


def main():
    n = int(input())
    A = tuple(map(int, input().split()))
    B = tuple(map(int, input().split()))
    ans, a1 = 0, A[0]
    for a2, b in zip(A[1:], B):
        cnt1 = min(a1, b)
        cnt2 = min(a2, b - cnt1)
        ans += cnt1 + cnt2
        a1 = a2 - cnt2  
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):


import sys
input = sys.stdin.readline

def main():
    n = int(input())
    A = tuple(map(int, input().rstrip().split()))
    B = tuple(map(int, input().rstrip().split()))
    ans, a1 = 0, A[0]
    for a2, b in zip(A[1:], B):
        cnt1 = min(a1, b)
        cnt2 = min(a2, b - cnt1)
        ans += cnt1 + cnt2
        a1 = a2 - cnt2  
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):


def main():
    n = int(input())
    A = tuple(map(int, input().rstrip().split()))
    B = tuple(map(int, input().rstrip().split()))
    ans, a1 = 0, A[0]
    for a2, b in zip(A[1:], B):
        cnt1 = min(a1, b)
        cnt2 = min(a2, b - cnt1)
        ans += cnt1 + cnt2
        a1 = a2 - cnt2  
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
input = sys.stdin.readline

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

    ans = 0
    for i in range(n):
        left = min(a[i], b[i])
        ans += left
        a[i] -= left
        b[i] -= left

        right = min(a[i + 1], b[i])
        ans += right
        a[i + 1] -= right
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
inpl = lambda: list(map(int,input().split()))
N = int(input())
A = inpl()
B = inpl()
ans = 0
for i in range(N):
    p = min(A[i],B[i])
    B[i] -= p
    q = min(B[i],A[i+1])
    A[i+1] -= q
    ans += p + q
print(ans)

Solution (Python):
N = int(input())
A = [int(_) for _ in input().split()]
B = [int(_) for _ in input().split()]
ans = 0

for i in range(N):
    left = min(A[i], B[i])
    A[i] -= left
    B[i] -= left
    ans += left

    right = min(A[i + 1], B[i])
    A[i + 1] -= right
    B[i] -= right
    ans += right

print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split(" ")))
B = list(map(int, input().split(" ")))

c = A[:]

point = 0
for i in range(N):
    p = min(c[i], B[i])
    c[i] -= p
    point += p
    p = min(c[i+1], B[i]-p)
    c[i+1] -= p
    point += p

print(point)

Solution (Python):



import sys

if __name__ == '__main__':
    inputN = int(input())
    inputMonster = list(map(int, input().split()))
    inputPower = list(map(int, input().split()))

    sumVictory = 0
    zanMonster = inputMonster[-1]
    
    for i in range(inputN):
        
        
        if inputPower[inputN - i - 1]  >= inputMonster[inputN - i - 1] + zanMonster:
            sumVictory += inputMonster[inputN - i - 1] + zanMonster
            zanMonster = 0
        
        else:
            sumVictory += inputPower[inputN - i - 1]
            if zanMonster > inputPower[inputN - i - 1]:
                zanMonster = inputMonster[inputN - i - 1]
            else:
                zanMonster = inputMonster[inputN - i - 1] + zanMonster - inputPower[inputN - i - 1]
        
    sys.stdout.write(str(sumVictory))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

C = A.copy()
for i in range(N):
  if C[i] < B[i]:
    if C[i] + C[i + 1] < B[i]:
      C[i], C[i + 1] = 0, 0
    else:
      C[i + 1] -= B[i] - C[i]
      C[i] = 0
  else:
    C[i] -= B[i]

print(sum(A) - sum(C))

Solution (Python):
n,*a=map(int,open(0).read().split())
c=0
for i in range(1,n+1):c+=min(a[i-1]+a[i],a[n+i]);a[i]-=max(min(a[n+i]-a[i-1],a[i]),0)
print(c)

Solution (Python):
n,*a=map(int,open(0).read().split())
c=0
for i in range(1,n+1):c+=min(a[i-1]+a[i],a[n+i]);a[i]-=max(min(a[n+i]-a[i-1],a[i]),0)
print(c)

Solution (Python):
n = int(input())
a = list(map(int, input().strip().split()))
b = list(map(int, input().strip().split()))
kill_count = 0
for i in range(n):
    kill_num = min(a[i],b[i])
    kill_count += kill_num
    b[i] -= kill_num
    a[i] -= kill_num

    kill_num_ex = min(a[i+1],b[i])
    kill_count += kill_num_ex
    a[i+1] -= kill_num_ex
    b[i] -= kill_num_ex

print(kill_count)

Solution (Python):
n = int(input())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]

result = 0
for i in range(n):
    beated_l = min(a[i],b[i])
    beated_r = min(a[i+1],b[i]-beated_l)
    result += beated_l + beated_r
    a[i+1] -= beated_r
print(result)

Solution (Python):

def main():

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


    for i in range(N):
        if A[i] >= B[i]:
            ans += B[i]
            A[i] -= B[i]
            B[i] = 0
        else:
            ans += A[i]
            B[i] -= A[i]
            A[i] = 0
            if A[i+1] >= B[i]:
                ans += B[i]
                A[i+1] -= B[i]
                B[i] = 0
            else:
                ans += A[i+1]
                A[i+1] = 0
    print(ans)





if __name__== "__main__":
    main()

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

cnt = 0
flag = False
for i in range(n) :
  if a[i] > b[i] :
    a[i] -= b[i]
    cnt += b[i]
    b[i] = 0
  elif a[i] == b[i]:
    cnt += b[i]
    b[i] = 0
    a[i] = 0
  else :
    b[i] -= a[i]
    cnt += a[i]
    a[i] = 0
    flag = True

  if flag :
    if a[i+1] > b[i] :
      a[i+1] -= b[i]
      cnt += b[i]
      b[i] = 0
    elif a[i+1] == b[i]:
      cnt += b[i]
      b[i] = 0
      a[i+1] = 0
    else :
      b[i] -= a[i+1]
      cnt += a[i+1]
      a[i+1] = 0
    flag = False

print(cnt)

Solution (Python):

import copy
import random
import bisect 
import fractions 
import math
import sys
import bisect

mod = 10**9+7
sys.setrecursionlimit(mod) 

def LI(): return list(map(int, sys.stdin.readline().split()))

N = int(input())
A = LI()
B = LI()

ans = 0

right = 0
for i in range(N):
    left = min(A[i]-right,B[i])
    ans += left
    right = min((B[i]-left),A[i+1])
    ans += right

print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ans=0
t=0
for i in range(n):
	aa,bb=a[i],b[i]
	if t>0:
		ans+=min(t,aa)
		aa=aa-t
		if aa<0:
			aa=0
	ans+=min(aa,bb)
	t=bb-aa
if t>0:
	ans+=min(t,a[n])
print(ans)

Solution (Python):
i1 = int(input())
i2 = input().split(' ')
i3 = input().split(' ')

w = -1
s = 0

for i in range(i1):
  ii2 = int(i2[i])
  ii3 = int(i3[i])



  if w > -1:
    ii2 = w

  if ii2 > ii3 :
    
    s += ii3
    w = int(i2[i+1])
  else:
    s += ii2
    w = ii3 - ii2

    iii2 = int(i2[i+1])

    if iii2 > w:
      s += w
      w = iii2 - w
    else:
      s += iii2
      w = 0

print(s)

Solution (Python):
N = int(input())
A = [int(s) for s in input().split()]
B = [int(s) for s in input().split()]

ans = 0
for i in range(N):
    cnt0 = min(A[i], B[i])
    A[i] -= cnt0
    B[i] -= cnt0
    ans += cnt0
    cnt1 = min(A[i+1], B[i])
    A[i+1] -= cnt1
    B[i] -= cnt1
    ans += cnt1
print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=0
for i in range(n):
  if(a[i]>b[i]):
    c+=b[i]
  elif(b[i]>=a[i]+a[i+1]):
    c+=a[i]+a[i+1]
    a[i+1]=0
  else:
    c+=b[i]
    a[i+1]=a[i+1]+a[i]-b[i]
print('{0}'.format(c))

Solution (Python):
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; ansli = []; tmpli = []; candili = []
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]
def mi(n): return [wi() for _ in range(n)]
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]

n = i()
a = wi()
b = wi()
c = 0

for i in range(n):
    ans += min(c + b[i], a[i])
    c = max(0, c + b[i] - a[i])
    c = min(c, b[i])

ans += min(c, a[n])

print(ans)