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())
Alist = list(map(int, input().split()))
Blist = list(map(int, input().split()))
beforeN = sum(Alist)
for i in range(N - 1, -1, -1):
    rest_power = Blist[i] - Alist[i + 1]
    if rest_power > 0:
        Alist[i + 1] = 0
        last_power = Alist[i] - rest_power
        if last_power <= 0:
            Alist[i] = 0
        else:
            Alist[i] = Alist[i] - rest_power
    else:
        Alist[i + 1] = Alist[i + 1] - Blist[i]
        continue
ans = beforeN - sum(Alist)
print(ans)

Solution (Python):
n  = int(input())
a  = list(map(int,input().split()))
a = a[::-1]
b  = list(map(int,input().split()))
b = b[::-1]
c  = 0

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

print(c)

Solution (Python):
n = int(input())
AL = list(map(int,input().split()))
BL = list(map(int,input().split()))
ans = 0
for i in range(n):
    if BL[i] <= AL[i]:
        ans += BL[i]
    elif BL[i] > AL[i] and BL[i] <= AL[i]+AL[i+1]:
        ans += BL[i]
        AL[i+1] = AL[i+1] - BL[i] + AL[i]
    else:
        ans += AL[i]+AL[i+1]
        AL[i+1] = 0
print(ans)

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
start = sum(A)
for i in range(N):
    A[i] = A[i] - B[i]
    if A[i] < 0:
        m = A[i] * (-1)
        A[i] = 0
        A[i+1] = max(A[i+1]-m,0)
end = sum(A)
print(start - end)

Solution (Python):

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)


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 += B[i]
    else:
        
        count += A[i]
        B[i] -= A[i]

        
        if A[i+1] >= B[i]:
            A[i+1] -= B[i]
            count += B[i]
        else:
            count += A[i+1]
            A[i+1] = 0


print(count)

Solution (Python):
import sys
input = sys.stdin.readline
N=int(input())
A=[int(x) for x in input().split()]
B=[int(x) for x in input().split()]
m=0
for i in range(0,N):
    d=B[i]-A[i]
    if d > 0:
        if A[i+1]-d <=0:
            B[i]=A[i]+A[i+1]
            A[i]=0
            A[i+1]=0
        else:
            A[i]=0
            A[i+1]=A[i+1]-d
    else:
        A[i]=-d
print(sum(B))

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

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

  if B[i] != 0:
    if B[i] < A[i+1]:
      result += B[i]
      A[i+1] -= B[i]
    elif B[i] >= A[i+1]:
      result += A[i+1]
      A[i+1] = 0

print(result)

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

ans=0
for i in range(len(b)):
  first=min(a[i],b[i])
  a[i]=a[i]-first
  b[i]=b[i]-first
  ans=ans+first
  if b[i]==0:
    continue
  
  second=min(a[i+1],b[i])
  a[i+1]=a[i+1]-second
  b[i]=b[i]-second
  ans=ans+second
  
print(ans)

Solution (Python):
n = int(input())
monsters = list(map(int, input().split()))
heroes = list(map(int, input().split()))

cnt = 0
for i in range(n):
  if heroes[i] <= monsters[i]:
    cnt += heroes[i]
  else:
    cnt += monsters[i]
    heroes[i] -= monsters[i]
    if heroes[i] <= monsters[i+1]:
      cnt += heroes[i]
      monsters[i+1] -= heroes[i]
    else:
      cnt += monsters[i+1]
      monsters[i+1] = 0
  
print(cnt)

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

a = list(map(int,input().split()))
b = list(map(int,input().split()))
count = 0
for i in range(num):
    if b[i] >= a[i]:
        cp=a[i]
        count += a[i]
        a[i]=0
        b[i] -=cp
        if b[i] >= a[i+1]:
            cp = a[i+1]
            count += cp
            a[i+1] = 0
        else:
            a[i+1] -=b[i]
            count += b[i]
    else:
        a[i] -=b[i]
        count += b[i]
print(count)

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

res = 0

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

    if d >= 1:
        res += A[i + 1]
        if A[i] >= d:
            A[i] -= d
            res += d
        else:
            res += A[i]
            A[i] = 0
    else:
        res += B[i]

print(res)

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

s = sum(a)
for i,v in enumerate(b):
    if a[i]>v:
        a[i] -= v
    elif a[i]+a[i+1]>v:
        a[i+1] -= (v-a[i])
        a[i]   = 0
    else:
        a[i]=a[i+1]=0
print(s-sum(a))

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
b.append(0)
result_L = []
yoryoku_L = [0]

for val in range(n+1):
    yoryoku = yoryoku_L[val] + b[val] - a[val]

    if yoryoku >= 0:
        result = a[val]

        if a[val] - yoryoku_L[val] >= 0:
            yoryoku_L.append(yoryoku)
        else:
            yoryoku_L.append(b[val])

        result_L.append(result)
    else:
        result = b[val] + yoryoku_L[val]
        yoryoku_L.append(0)
        result_L.append(result)


print(sum(result_L))

Solution (Python):
import heapq

N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
sum=0
for i in range(N):
  if A[i]>=B[i]:

    sum+=B[i]
    A[i]-=B[i]
  else:

    sum+=A[i]
    tmp=B[i]-A[i]
    A[i]=0
    if A[i+1]>=tmp:

      sum+=tmp
      A[i+1]-=tmp
    else:

      sum+=A[i+1]
      A[i+1]=0



print(sum)

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

cnt = 0
for i in range(N):
  v = min(A[-i-1],B[-i-1])
  cnt += v
  A[-i-1] -= v
  B[-i-1] -= v
  w = min(A[-i-2],B[-i-1])
  cnt += w
  A[-i-2] -= w
  B[-i-1] -= w
    
print(cnt)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
x = 0
for i in range(n):
    for j in range(2):
        xi = min(a[i + j], b[i])
        a[i + j] -= xi
        b[i] -= xi
        x += xi
print(x)

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

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

print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split(' ')))
b = list(map(int, input().split(' ')))
ctr = min(a[0], b[0])
b[0] -= ctr

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

ctr += min(a[n], b[n - 1])

print(ctr)

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
d = 0
c = A[0]

for i in range(N):
    d += max(0, c - B[i])
    c = max(0, A[i+1] + min(0, c - B[i]))
            
            
print(sum(A) - (d + c))

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

M = 0

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

print(M)

Solution (Python):
N=int(input())
A=list(map(int, input().split()))
B=list(map(int, input().split()))
c=A[0]
d=0
for i in range(N):
  d+=max(0,c-B[i])
  c=max(0,A[i+1]+min(0,c-B[i]))
print(sum(A)-(d+c))

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

print(bSum)

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

print(bSum)

Solution (Python):
n = int(input())
a, b = [list(map(int, input().split())) for _ in range(2)]
bSum = 0
for i in range(n):
    if a[i] >= b[i]:
        bSum += b[i]
        a[i] -= b[i]
        b[i] = 0
    elif a[i] + a[i + 1] >= b[i]:
        bSum += b[i]
        a[i + 1] -= b[i] - a[i]
        a[i] = 0
    elif a[i] + a[i + 1] <= b[i]:
        bSum += a[i] + a[i + 1]
        a[i + 1] = 0

print(bSum)

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

monster = []
for i in range(N):
    monster.append(min(A[i]+A[i+1],B[i]))
    A[i+1] = min((A[i]+A[i+1])-monster[i],A[i+1])
print(sum(monster))

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]
    elif a[i] + a[i+1] > b[i]:
        ans += b[i]
        a[i+1] = a[i] + a[i+1] - b[i]
    else:
        ans += a[i] + a[i+1]
        a[i+1] = 0
print(ans)

Solution (Python):
n = int(input())
a = [int(i) for i in input().split(" ")]
b = [int(i) for i in input().split(" ")]
c = min(a[0], b[0])
d = max(0, b[0]-a[0])
for i in range(n-1):
    c += min(a[i+1], d)
    d = max(0, a[i+1]-d)
    c += min(b[i+1], d)
    d = max(0, b[i+1]-d)
c += min(a[n], d)
print(c)

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

ans = 0

for i in reversed(range(N)):
    
    for j in reversed(range(2)):
    
        temp = min(A[i + j], B[i])
        ans += temp
        A[i + j] -= temp
        B[i] -= temp
        
print(ans)

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

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

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

print(sum)

Solution (Python):
N=int(input())
monster_num = list(map(int,input().split()))
maximum_kill = list(map(int,input().split()))

kill_num = [[0,0] for i in range(N)]

kill_num[0][0] = min(monster_num[0],maximum_kill[0])
kill_num[0][1] = min(monster_num[1], maximum_kill[0] - kill_num[0][0])
for i in range(1,N):
    kill_num[i][0] = min(monster_num[i] - kill_num[i-1][1], maximum_kill[i])
    kill_num[i][1] = min(monster_num[i+1], maximum_kill[i] - kill_num[i][0])

print(sum(sum(kill) for kill in kill_num))

Solution (Python):


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
d = 0
for i,n in enumerate(b):
    c = a[i]
    if n + d >= c:
        ans += c
        if c - d <= 0:
            d = n
        else:
            d = n - (c - d)
    else:
        ans += n + d
        d = 0
l = a[-1]
if l <= d:
    ans += l
else:
    ans += d
print(ans)

Solution (Python):
def main(): 

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

    cnt = 0

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

    print(cnt)



if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = m = 0
for i in range(N):
    m = min(A[i], B[i])
    A[i] -= m
    B[i] -= m
    ans += m
    m = min(A[i + 1], B[i])
    A[i + 1] -= m
    B[i] -= m
    ans += m
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):
    x = min(A[i], B[i])
    A[i] -= x
    B[i] -= x
    ans += x

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

print(ans)

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):
  if a[i] <= b[i]:
    ans += a[i]
    b[i] -= a[i]
    if a[i + 1] <= b[i]:
      ans += a[i + 1]
      a[i + 1] = 0
    else:
      ans += b[i]
      a[i + 1] -= b[i]
  else:
    ans += b[i] 
    
print(ans)

Solution (Python):
import math

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

ans = 0

for i in reversed(range(n)):
	if a[i+1] - b[i] <= 0:
		ans += a[i+1]
		b[i] -= a[i+1]
		a[i+1] = 0
		if a[i] - b[i] <= 0:
			ans += a[i]
			b[i] -= a[i]
			a[i] = 0
		else:
			ans += b[i]
			a[i] -= b[i]
			b[i] = 0
	else:
		ans += b[i]
		a[i+1] -= b[i]
		b[i] = 0

print(ans)

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

Solution (Python):
import copy

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

result = 0
rest_power = 0
for i in range(N):
    if B[i] > A[i]:
        rest_power = B[i] - A[i]
        result += A[i]
        
        mem = copy.copy(A[i + 1])
        A[i + 1] -= rest_power
        if A[i + 1] < 0:
            result += mem
            A[i + 1] = 0
        else:
            result += rest_power
    else:
        result += B[i]

print(result)

Solution (Python):

import sys


def solve(N: int, A: "List[int]", B: "List[int]"):
    s = 0
    for i, b in enumerate(B):
        if A[i] + A[i + 1] <= b:
            s += A[i] + A[i + 1]
            A[i] = 0
            A[i + 1] = 0
            B[i] = 0
    for i, b in enumerate(B):
        sub = min(A[i], B[i])
        A[i] -= sub
        B[i] -= sub
        s += sub
        sub = min(A[i + 1], B[i])
        A[i + 1] -= sub
        B[i] -= sub
        s += sub
    print(s)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N + 1)]  
    B = [int(next(tokens)) for _ in range(N)]  
    solve(N, A, B)

if __name__ == '__main__':
    main()

Solution (Python):
N = raw_input()
enemy = map(int, raw_input().split())
hero = map(int, raw_input().split())

counter = 0
enemyKill = 0

while counter < int(N):
  if hero[counter] > enemy[counter]:
    hero[counter] -= enemy[counter]
    enemyKill += enemy[counter]
    enemy[counter] = 0
    if hero[counter] > enemy[counter + 1]:
      hero[counter] -= enemy[counter + 1]
      enemyKill += enemy[counter + 1]
      enemy[counter + 1] = 0
    else:
      enemy[counter + 1] -= hero[counter]
      enemyKill += hero[counter]
      hero[counter] = 0
  else:
    enemy[counter] -= hero[counter]
    enemyKill += hero[counter]
    hero[counter] = 0
  counter += 1  
print enemyKill

Solution (Python):
N = int(input())
A = [int(i) for i in input().split(' ')]
B = [int(i) for i in input().split(' ')]
totalB = sum(B)

for i in range(N):
    if i == 0:
        B[i] = B[i] - A[i]
        if B[i]<0:
            B[i] = 0
    else:
        A[i] = A[i] - B[i-1]
        B[i-1] = 0
        if A[i] < 0:
            B[i-1] = abs(A[i])
            A[i] = 0
            
        B[i] = B[i] - A[i]
        if B[i] < 0:
            B[i] = 0


B[N-1] = B[N-1] - A[N]
if B[N-1] < 0:
    B[N-1] = 0

print(totalB - sum(B))

Solution (Python):
def solve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    result = 0
    
    for i in range(N):
        attack = min(A[i],B[i])
        B[i] -= attack
        A[i] -= attack
        result += attack
        if B[i] > 0:
            attack = min(A[i+1], B[i])
            B[i] -= attack
            A[i+1] -= attack
            result += attack
    
    print( result )
    
solve()

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

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

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):
    rg = 0
    if i == 0:
        rg = range(0, 1)
    elif i == n:
        rg = range(-1, 0)
    else:
        rg = range(-1, 1)

    for j in rg:
        if a[i] > b[i+j]:
            count += b[i+j]
            a[i] -= b[i+j]
            b[i+j] = 0
        else:
            count += a[i]
            b[i+j] -= a[i]
            a[i] = 0

print(str(count))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = 0
for i in range(N):
    temp = B[i] - A[i]
    if temp >= 0:
        ans += A[i]
        if A[i+1] >= temp:
            A[i+1] -= temp
            ans += temp
        else:
            ans += A[i+1]
            A[i+1] = 0
    else:
        ans += B[i]

print(ans)

Solution (Python):
N = int(input())
A_list = [int(i) for i in input().split()]
B_list = [int(i) for i in input().split()]

killed = 0
for i in range(N) :
    brave = B_list[i]
    monster = A_list[i]
    
    if (brave >= monster) :
        
        killed += monster
        brave -= monster
        monster = A_list[i+1]
        if (brave >= monster) :
            killed += monster
            brave -= monster
            monster = 0
        elif (brave < monster) :
            
            killed += brave
            monster -= brave 
        A_list[i+1] = monster
            
    elif (brave < monster) :
        
        killed += brave
        monster -= monster 

print(killed)

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
s = 0
for i in range(n):
  if a[i] + a[i+1] <= b[i]:
    s += a[i] + a[i+1]
    a[i+1] = 0
  elif a[i] + a[i+1] > b[i] and a[i] <= b[i]:
    s += b[i]
    a[i+1] -= b[i] - a[i]
  elif a[i] > b[i]:
    s += b[i]
print(s)

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 B[i] > A[i]:
        
        count += A[i]
        
        B[i] -= A[i]
        
        
        if B[i] > A[i+1]:
            
            count += A[i+1]
            A[i+1] = 0
        
        else:
            count += B[i]
            A[i+1]-=B[i]
    
    else:
        
        count += B[i]
        
        A[i] -= B[i]

print(count)

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

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

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

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]>=0:
        count+=B[i]
    else:
        if A[i+1]-abs(A[i]-B[i])>=0:
            count+=A[i]+abs(A[i]-B[i])
            A[i+1]=A[i+1]-abs(A[i]-B[i])
        else:
            count+=A[i]+A[i+1]
            A[i+1]=0

print(count)

Solution (Python):
N=input()
N=int(N)
A=list(map(int,input().split()))
B=list(map(int,input().split()))
P=0
p1=min(A[0],B[0])
P12=[]
P+=p1
for i in range(N):
    if i>=1:
        p1=min(A[i]-P12[i-1],B[i])
        P+=p1
    p12=min(A[i+1],B[i]-p1)
    P12.append(p12)
    P+=p12
print(P)

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

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

Solution (Python):
N = int(input())
monster = list(map(int, input().split()))
hero = list(map(int, input().split()))

count = 0
for i in range(N):
  k = min(monster[i], hero[i])
  l = min(monster[i+1], hero[i]-k)
  count += k+l
  monster[i+1] -= l
print(count)

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

ans = 0
for i, b in enumerate(B):
    defeated = min(A[i], b)
    A[i] -= defeated; b -= defeated
    ans += defeated
    if b > 0: defeated = min(A[i+1], b); A[i+1] -= defeated; ans += defeated
print(ans)

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

result = 0

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

print(result)

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
R = [0]*(N+1)
ans = 0
for i in range(N):
    if B[i] + R[i] < A[i]:
        ans += B[i]+R[i]
    else:
        if R[i] < A[i]:
            R[i+1] = B[i]+R[i]-A[i]
        else:
            R[i+1] = B[i]
        ans += A[i]
if R[N] < A[N]:
    ans += R[N]
else:
    ans += A[N]
print(ans)

Solution (Python):
if __name__ == '__main__':
    N = int(input())
    A, B = [list(map(int, input().split())) for l in range(2)]
    A_sum = sum(A)
    for i, brave in enumerate(B):
        A[i] -= brave
        if A[i] < 0:
            A[i+1] += A[i]
            if A[i+1] < 0:
                A[i+1] = 0
            A[i] = 0
            
    print(A_sum - sum(A))

Solution (Python):
from sys import exit, stderr, stdin
input = stdin.readline


def debug(var, name="hoge"):
    print(name +":" + str(type(var)) + " = " + repr(var), file=stderr)
    return


def main():
    N = int(input())
    A = [int(n) for n in input().split()]
    B = [int(n) for n in input().split()]
    ans = 0
    for i in range(N, -1, -1):
        
        
        if A[i] > 0:
            if i != N:
                beat = min(B[i],A[i])
                ans += beat
                A[i]-=beat
                B[i]-=beat
                if A[i] == 0:
                    continue
            beat = min(B[max(0,i-1)],A[i])
            ans += beat
            A[i]-=beat
            B[max(0,i-1)]-=beat
    
    
    print(ans)







if __name__ == "__main__":
    main()

Solution (Python):
from collections import defaultdict, deque
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate
import sys


def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LIM(): return list(map(lambda x:int(x) - 1, sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def LIRM(n): return [LIM() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
mod = 1000000007


def main():
    n = I()
    monster = LI()
    ret = 0
    ans = sum(monster)
    savor_lim = LI()
    for i in range(n - 1, -1, -1):
        k = min(savor_lim[i], monster[i + 1])
        monster[i + 1] -= k
        savor_lim[i] -= k
        monster[i] -= min(savor_lim[i], monster[i])
    return ans - sum(monster)



print(main())

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
d = sum(A)
for i in range(N):
  if A[i]>=B[i]:
    A[i] = A[i]-B[i]
  elif A[i]<B[i]:
    A[i+1] = A[i+1]-(B[i]-A[i]) if A[i+1]-(B[i]-A[i])>=0 else 0
    A[i] = 0
print(d-sum(A))

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

ans = 0
for i in range(len(B)-2, -1, -1):
    temp = min(A[i+1], B[i])
    ans += temp
    B[i] -= temp

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

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):
  am, ap = A[i], A[i+1]
  b = B[i]
  first = min(am, b)
  second = min(ap, b-first)
  A[i] -= first
  A[i+1] -= second
  c += first + second

print(c)

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

ini = sum(a)
ans = 0
remain = a[0]

for i in range(n):
    remain = max(0, b[i] - a[i])
    a[i] = max(0, a[i]-b[i])
    a[i+1] = max(0, a[i+1]-remain)

    
fin = sum(a)

print(ini-fin)

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
count = 0
left = 0
for i in range(n):
    if b[i] + left - a[i] >= 0:
        count += a[i]
        left = b[i] + left - a[i]
        if left > b[i]:
            left = b[i]
    else:
        count += b[i] + left
        left = 0

count += min(left, a[n])
print(count)

Solution (Python):
n = int(input())
A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]
ans = 0
for i in range(n):
    s1 = min(A[i], B[i])
    ans += s1
    B[i] -= s1
    A[i] -= s1
    s2 = min(A[i+1], B[i])
    ans += s2
    B[i] -= s2
    A[i+1] -= s2
    
print(ans)

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

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

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

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

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 B[i] >= A[i] and (B[i]-A[i]) >= A[i+1]:
        ans += A[i] + A[i+1]
        A[i] = 0
        A[i+1] = 0
    elif B[i] >= A[i] and (B[i]-A[i]) < A[i+1]:
        ans += B[i]
        A[i+1] -=  (B[i]-A[i])
        A[i] = 0
    else:
        ans += B[i]
        A[i] -= B[i]
        
print(ans)

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

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

print(ans)

Solution (Python):
n = int(input())
a_arr = list(map(int,input().split()))
b_arr = list(map(int,input().split()))
cnt = 0
for i in range(n):
    tmp = min(a_arr[i],b_arr[i])
    cnt += tmp
    a_arr[i] -= tmp
    cnt += min(b_arr[i]-tmp,a_arr[i+1])
    a_arr[i+1] -= min(b_arr[i]-tmp,a_arr[i+1])
print(cnt)

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

for i in range(n):
  if a[i] > 0:
    d = min(a[i], b[i])
    c += d
    a[i] -= d
    b[i] -= d

  if b[i] > 0:
    d = min(a[i+1], b[i])
    c += d
    a[i+1] -= d
    b[i] -= d

print(c)

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

s = 0
for i in range(n)[::-1]:
	if a[i + 1] - b[i] > 0:
		s += b[i]
		continue
	s += a[i + 1]
	rb = b[i] - a[i + 1]
	if a[i] - rb > 0:
		s += rb
		a[i] -= rb
		continue
	s += a[i]
	a[i] = 0
print(s)

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

mons = 0
tmp = 0

for i in range(n):
    if a[i] <= b[i]+tmp:
        mons += a[i]
        if a[i] >= tmp:
            tmp = b[i]-(a[i]-tmp)
        else:
            tmp = b[i]
    else:
        mons += b[i]+tmp
        tmp = 0

if a[n] >= tmp:
    mons += tmp
else:
    mons += a[n]

print(mons)

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

for i in range(n):
    t = min(a[i], b[i])
    c += t
    b[i] -= t
    if b[i]:
        t = min(a[i + 1], b[i])
        c += t
        a[i + 1] -= t
print(c)