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):
from collections import deque
from sys import stdin


def main():
    lines = stdin.readlines()
    A = deque(map(int, lines[1].split()))
    B = deque(map(int, lines[2].split()))
    cnt = 0
    ans = 0
    while B:
        a = A.pop()
        b = B.pop()
        tmp = a if a < b else b
        ans += tmp
        if cnt % 2:
            A.append(a - tmp)
        else:
            B.append(b - tmp)
        cnt += 1
    print(ans)
    return


main()

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

C = [0] * (n+1)

ans = 0
for i in range(n):
    C[i+1] = min(max(B[i]+C[i]-A[i], 0),B[i])
    ans += min(A[i], B[i] + C[i])

ans += min(A[n], C[n])
print(ans)

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

cities = list(map(int, input().split()))
savers = list(map(int, input().split()))

result = 0

for i in range(N):
  left = min(cities[i], savers[i])
  cities[i] -= left
  savers[i] -= left
  result += left
  
  right = min(cities[i + 1], savers[i])
  cities[i + 1] -= right
  savers[i] -= right
  result += right

print(result)

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

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

B.append(0)
punitives = []

for i in range(1, N+1):
    v = 0
    if A[-i] >= B[-i]:
        A[-i] = A[-i]-B[-i]
        punitives.append(B[-i])
        B[-i] = 0
    else:
        punitives.append(A[-i])
        A[-i] = 0
        B[-i] = B[-i]-A[-i]

    if B[-i-1] >= A[-i]:
        v = A[-i]
        A[-i] = 0
        B[-i-1] = B[-i-1]-v
    else:
        v = B[-i-1]
        A[-i] = A[-i]-v
        B[-i-1] = 0
    punitives.append(v)

if A[0] >= B[0]:
    w = B[0]
else:
    w = A[0]

punitives.append(w)
print(sum(punitives))

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

c = 0
for i in range(n):
      ci = 0
      ci = min(a[i],b[i])
      c += ci
      b[i] -= ci
      ci1 = 0
      ci1 = min(a[i+1],b[i])
      c += ci1
      a[i+1] -= ci1

print(c)

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

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]

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

monster_count = 0
next_saver = 0

for i in range(N+1):
    if A[i] > B[i] + next_saver:
        monster_count += B[i] + next_saver
        next_saver = 0
    else:
        monster_count += A[i]
        if A[i] <= next_saver:
            next_saver = B[i]
        else:
            next_saver = B[i] - (A[i]-next_saver)
print(monster_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):
    if A[i] >= B[i]:
        ans += B[i]
    if A[i] < B[i]:
        ans += A[i]
        if A[i+1] >= (B[i] - A[i]):
            A[i+1] -= B[i] - A[i]
            ans += B[i] - A[i]
        else:
            ans += 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()))
cnt = 0
for i in range(n):
  if a[i] >= b[i]:
    a[i] = a[i] - b[i]
    cnt += b[i]
  elif a[i] + a[i + 1] >= b[i]:
    a[i + 1] = a[i] + a[i + 1] - b[i]
    a[i] = 0
    cnt += b[i]
  else:
    cnt += a[i] + a[i + 1]
    a[i] = 0
    a[i + 1] = 0
print(cnt)

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

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

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

res = 0
for i in range(n):
    res += min(A[i], B[i])
    if B[i] > A[i]:
        tmp = min(B[i] - A[i], A[i+1])
        res += tmp
        A[i+1] -= tmp
print(res)

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

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):
    c = min(A[i],B[i])
    ans += c
    B[i] -= c
    if(B[i] > 0):
        c = min(A[i+1], B[i])
        ans += c
        A[i+1] -= c
        B[i] -= c

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

    
print(ans)

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

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

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

print(defeated_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):
    ans += min(B[i], A[i]+A[i+1])
    A[i+1] = max(0, A[i+1] - max(0, (B[i]-A[i])))
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]
  else:
    
    c += a[i]
    
    d = b[i] - a[i]
    
    if a[i + 1] >= d:
      c += d
      a[i + 1] = a[i + 1] - d
    
    else:
      c += a[i + 1]
      a[i + 1] = 0
      
print(c)

Solution (Python):


N=int(input())

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

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

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

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

def calc():
    result = 0
    for i in range(N):
        x = min(A[i], B[i])
        A[i] -= x
        B[i] -= x
        result += x
        x = min(A[i + 1], B[i])
        A[i + 1] -= x
        B[i] -= x
        result += x
    return result

print(calc())

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

C = [0] * (N + 1)

B.append(0)
kill = 0

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

print(kill)

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

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

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

print(d_sum - sum(A))

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

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

print(total)

Solution (Python):
n = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
res = 0
for i in range(n):
    if A[i] >= B[i]:
        res += B[i]
    else:
        res += A[i]
        B[i] -= A[i]
        if A[i+1] >= B[i]:
            A[i+1] -= B[i]
            res += B[i]
        else:
            res += A[i+1]
            A[i+1] = 0
print(res)

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

L=[-1 for i in range(N)]
R=[-1 for i in range(N)]

L[0]=min(A[0],B[0])
R[0]=min(A[1],B[0]-L[0])

for i in range(1,N):
    L[i]=min(A[i]-R[i-1],B[i])
    R[i]=min(A[i+1],B[i]-L[i])
    
print(sum(L)+sum(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 B[i] < A[i]:
        ans += B[i]
        A[i] -= B[i]
    else:
        ans += A[i]
        B[i] -= A[i]
        if B[i] < A[i+1]:
            ans += B[i]
            A[i+1] -= B[i]
        else:
            ans += 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()))
count = 0
for i in range(n):
    if a[i] >= b[i]:
        count += b[i]
    elif a[i]+a[i+1] >= b[i]:
        count += b[i]
        a[i+1] -= b[i] - a[i]
    else:
        count += a[i] + a[i+1]
        a[i+1] = 0
print(count)

Solution (Python):
s=input()
t=list(map(int,input().split()))
u=list(map(int,input().split()))
list=[]
a=int(s)
c=t[0]
d=0
e=0
for i in range(a):
  if t[i]<=u[i]:
    if t[i+1]<=u[i]-t[i]:
      d+=t[i]+t[i+1]
      t[i+1]=0
    else:
      d+=u[i]
      t[i+1]=t[i+1]+t[i]-u[i]
  else:
    d+=u[i]
print(d)

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

print(count)

Solution (Python):
def main():
    n, *ab = map(int, open(0).read().split())
    a = ab[:n+1]
    b = ab[n+1:]

    init_a_sum = sum(a)
    for i, cur_b in enumerate(b):
        if a[i] > cur_b:
            a[i] -= cur_b
        else:
            a[i+1] = max(0, a[i+1] -  (cur_b - a[i]))
            a[i] = 0
    last_a_sum = sum(a)
    ans = init_a_sum - last_a_sum
    print(ans)
    return()

if __name__ == '__main__':
    main()

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

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

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
start = [0]*(N+1)
attack = 0
enemy = 0

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

for i in range(N):
    attack = min(A[i],B[i])
    A[i] = A[i] - attack
    B[i] = B[i] - attack
    attack = min(A[i+1],B[i])
    A[i+1] = A[i+1] - attack
    B[i] = B[i] - attack
    
for i in range(N+1):
    enemy = enemy + (start[i]-A[i])
    
print(enemy)

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


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

print(kill)

Solution (Python):
def main():

    N = int(input())
    A = [int(x) for x in input().split()]
    B = [int(x) for x in input().split()]
    cnt = 0

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

    print(cnt)

if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
monsters = list(map(int, input().split()))
yusha_power = list(map(int, input().split()))

count = 0
for yusha_index in range(len(yusha_power)):
    beaten = min(yusha_power[yusha_index], monsters[yusha_index])
    yusha_power[yusha_index] -= beaten
    monsters[yusha_index] -= beaten
    count += beaten

    beaten = min(yusha_power[yusha_index], monsters[yusha_index+1])
    yusha_power[yusha_index] -= beaten
    monsters[yusha_index+1] -= beaten
    count += beaten
print(count)

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

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

prev_monster = sum(A)

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

after_monster = sum(A)
ans = prev_monster - after_monster
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):
    if a[i] >= b[i]:
        ans += b[i]
    else:
        ans += a[i]
        b[i] -= a[i]
        if a[i+1] >= b[i]:
            ans += b[i]
            a[i+1] -= b[i]
        else:
            ans += 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()))
count=0
for i in range(N):
  if A[i]+A[i+1]<=B[i]:
    count+=A[i]+A[i+1]
    A[i+1]=0
  elif A[i]<=B[i]:
    count+=B[i]
    A[i+1]=A[i+1]-(B[i]-A[i])
  else:
    count+=B[i]
print(count)

Solution (Python):
N = int(input())
enemy = list(map(int ,input().split()))
brave = list(map(int, input().split()))
enemy = enemy[::-1]
brave = brave[::-1]
sum = 0
for i in range(N):
    star = min(enemy[i] , brave[i])
    brave[i] = brave[i] - enemy[i]
    if brave[i] > 0:
        add_star = min(enemy[i + 1], brave[i])
        enemy[i+1] = enemy[i + 1] - add_star
        star += add_star
    sum+= star
print(sum)

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

E = 0

for i in range(0,N):
	for j in range(i,i+2):

		if B[i] - A[j] > 0:
			E += A[j]
			B[i] = B[i] - A[j]
			A[j] = 0
		if B[i] - A[j] <= 0:
			E += B[i]
			A[j] = A[j] - B[i]
			B[i] = 0
			break
	
print(E)

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

count = 0

for i in range(0, N):
    a0 = A[i]
    b = B[i]
    
    if a0 < b:
        A[i] = 0
        count += a0
        b -= a0

        a1 = A[i+1]
        if a1 < b:
            A[i+1] = 0
            count += a1
        else:
            A[i+1] -= b
            count += b
    else:
        A[i] -= b
        count += b
print(count)

Solution (Python):
n = int(input())
*a, = map(int, input().split())
*b, = map(int, input().split())
count = 0
for i in range(n):
    tmp = min(a[i], b[i])
    count += tmp
    b[i] -= tmp
    tmp2 = min(a[i+1], b[i])
    count += tmp2
    a[i+1] -= tmp2

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

    if judge < 0:
        if (A[i+1]+judge) >= 0:
            count += -judge
            A[i+1] += judge
        else:
            count += A[i+1]
            A[i+1] = 0
print(count)

Solution (Python):
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] + a[i+1] >= b[i]:
        if a[i] >= b[i]:
            cnt += b[i]
        else:
            a[i+1] = a[i] + a[i+1] - b[i]
            cnt += b[i]
    else:
        cnt += a[i] + a[i+1]
        a[i+1] = a[i] = 0
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):
  if A[i]+A[i+1]<=B[i]:
    x+=A[i]+A[i+1]
    A[i+1]=0
  elif A[i]>=B[i]:
    x+=B[i]
  elif A[i]<B[i]:
    x+=B[i]
    A[i+1]=A[i+1]-(B[i]-A[i])
print(x)

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

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


ans = 0
last = 0
for i in range(N):
    b = B[i]
    a = A[i] - last
    if a < b:
        ans += a
        b -= a
        if A[i+1]<b:
            ans += A[i+1]
            b -= A[i+1]
            last = A[i+1]
        elif A[i+1]>=b:
            ans += b
            last = b        
    elif a >= b:
        last = 0
        ans += b
print(ans)

Solution (Python):

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

sup = 0
count = 0
for i in range(len(B)):
	b = B[i]
	m = A[i]
	if m - sup >=  b:
		count += sup + b
		sup = 0
	elif m-sup < b:
		count += m
		sup =  b - max(0, m - sup)

print(count)

Solution (Python):


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

count = 0

for i in range(N):
    if i > 0:
        if (A[i] > 0) and (B[i-1] > 0):
            tmp = min(A[i],B[i-1])
            count += tmp
            A[i] -= tmp
            B[i-1] -= tmp            

    if (A[i] > 0) and (B[i] > 0):
        tmp = min(A[i],B[i])
        count += tmp
        A[i] -= tmp
        B[i] -= tmp

if (A[N] > 0) and (B[N-1] > 0):
    tmp = min(A[N],B[N-1])
    count += tmp
    A[N] -= tmp
    B[N-1] -= tmp 

print(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,0,-1):
  s = min(a[i],b[i-1])
  ans += s
  b[i-1] -= s
  t = min(a[i-1],b[i-1])
  ans += t
  a[i-1] -= t
  
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(0,N+1):
    A[i]=int(A[i])
for i in range(0,N):
    B[i]=int(B[i])

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

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

ans = 0
for i in range(N):
  if mon[i] >= hero[i]:
    ans += hero[i]
  else:
    tem = mon[i+1]
    mon[i+1] = max(0,mon[i+1]-(hero[i]-mon[i]))
    ans += mon[i]+tem-mon[i+1]
    
print(ans)

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

fii = []
fij = []

fii += [min(monsters[0], savers[0])]
fij += [min(monsters[1], savers[0]-fii[-1])]
for i in range(1,n):
    fii += [min(monsters[i]-fij[-1], savers[i])]
    fij += [min(monsters[i+1], savers[i]-fii[-1])]
print(sum(fii)+sum(fij))

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

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

    if A[i+1]<=B[i]:
        B[i]-=A[i+1]
        counter+=A[i+1]
        A[i+1]=0
    elif B[i]<A[i+1]:
        counter+=B[i]
        A[i+1]-=B[i]
        B[i]=0

print(counter)

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

        else:
            num += zan
            A[i + 1] -= zan

    else:
        num += B[i]




print(num)

Solution (Python):
n = int(raw_input())
a = map(int, raw_input().split()) 
b = map(int, raw_input().split()) 
m = 0
for i in xrange(n):
  p = min(a[i], b[i])
  a[i] = a[i] - p
  c = b[i] - p
  q = min(a[i+1], c) 
  a[i+1] = a[i+1] - q
  m += p + q
print m

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

sum=0
for i in range(n):
	sum+=s[i][0]+s[i][1]
print(sum)

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

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()))
ans = 0

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

print(ans)

Solution (Python):


def main():
    N, *AB = map(int, open(0).read().split())
    ans, a1 = 0, AB[0]
    for a2, b in zip(AB[1 : N + 1], AB[N + 1 :]):
        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, *AB = map(int, open(0).read().split())
    ans, a1 = 0, AB[0]
    for a2, b in zip(AB[1 : N + 1], AB[N + 1 :]):
        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):

N = int(input())
list_A = list(map(int, input().split()))
list_B = list(map(int, input().split()))
 

 
ans = 0
remaining_power = 0
 
if N>1:
    for i in range(N-1,-1,-1):
        
 
        if remaining_power>list_A[i+1]:
            
            remaining_power=list_B[i]
            ans += list_A[i+1]
            
        elif remaining_power + list_B[i] > list_A[i+1]:
            
            remaining_power = list_B[i] - (list_A[i+1] - remaining_power)
            ans += list_A[i+1]
            
        else:
            
            ans += remaining_power + list_B[i]
            remaining_power = 0
            
else:
    remaining_power = list_B[0] 
            
if remaining_power > list_A[0]:
    ans += list_A[0]
else:
    ans += remaining_power
        
print(ans)

Solution (Python):
n = int(input())
a_list = list(map(int,input().split()))
b_list = list(map(int,input().split()))

zan = 0
ans = 0
for i in range(n):
    temp = b_list[i] + zan
    s = a_list[i]
    if s >= temp:
        ans += temp
        zan = 0
    elif s < temp and s >= zan:
        ans += s
        zan = temp - s        
    else:
        ans += s
        zan = b_list[i]

if zan > a_list[n]:
    ans += a_list[n]
else:
    ans += zan
        
print(ans)

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

list_A = list(map(int, input().split()))
list_B = list(map(int, input().split()))

score = 0
for i in range(len(list_B)):
    
    if list_A[i] < list_B[i]:
        score = score + list_A[i]

        
        
        if list_A[i+1] < list_B[i]-list_A[i]:
            score = score + list_A[i+1]
            list_A[i+1] = 0
        
        else:
            score = score + list_B[i]-list_A[i]
            list_A[i+1] = list_A[i+1] - (list_B[i]-list_A[i])

    else:
        score = score + list_B[i]

print(score)

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

cnt = 0
res = 0

for i in range(N):
    if A[i] >= res:
        cnt += res
        A[i] -= res
    else:
        cnt += A[i]
        A[i] = 0
        
    hit = min(A[i],B[i])
    cnt += hit
    res = B[i] - hit
    
cnt += min(A[N], res)

print(cnt)

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

for i in range(0,n):
    zan = 0
    tres = 0
    ta = a[i]
    tb = b[i]

    if tb >= ta:
        res += ta
        zan = tb-ta
        tres = min(a[i+1],zan)
        a[i+1] -= tres
        res += tres

    else:
        res += tb
        
print(res)

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

lstA = list(map(int,input().split()))
lstB = list(map(int,input().split()))

x = 0
ans = 0
for i in range(N):
    if ( lstB[i] >= (lstA[i] + lstA[i+1]) ):
        ans += (lstA[i] + lstA[i+1])
        lstA[i+1] = 0
    elif ( lstB[i] > lstA[i] ):
        ans += lstB[i]
        lstA[i+1] = lstA[i] + lstA[i+1] - lstB[i]
    else:
        ans += lstB[i]

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

Solution (Python):
N = int(input())
A_list = list(map(int, input().split())) 
B_list = list(map(int, input().split())) 
ans = 0
before_num = 0
for i in range(N+1):
    if i < N:
        if A_list[i] >= before_num:
            ans += before_num
            A_list[i] -= before_num
            if A_list[i] >= B_list[i]:
                ans += B_list[i]
                A_list[i] -= B_list[i]
                B_list[i] = 0
                before_num = 0
            elif A_list[i] < B_list[i]:
                ans += A_list[i]
                B_list[i] -= A_list[i]
                A_list[i] = 0 
                before_num = B_list[i]
        elif A_list[i] < before_num:
            ans += A_list[i]
            before_num = B_list[i]
            A_list[i] = 0
    if i == N:
        if A_list[i] >= before_num:
            ans += before_num
            A_list[i] -= before_num
        elif A_list[i] < before_num:
            ans += A_list[i]
            A_list[i] = 0

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

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

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_list = list(map(int, input().split())) 
B_list = list(map(int, input().split())) 
ans = 0
before_num = 0
for i in range(N+1):
    if i < N:
        if A_list[i] >= before_num:
            ans += before_num
            A_list[i] -= before_num
            if A_list[i] >= B_list[i]:
                ans += B_list[i]
                A_list[i] -= B_list[i]
                B_list[i] = 0
                before_num = 0
            elif A_list[i] < B_list[i]:
                ans += A_list[i]
                B_list[i] -= A_list[i]
                A_list[i] = 0 
                before_num = B_list[i]
        elif A_list[i] < before_num:
            ans += A_list[i]
            before_num = B_list[i]
            A_list[i] = 0
    if i == N:
        if A_list[i] >= before_num:
            ans += before_num
            A_list[i] -= before_num
        elif A_list[i] < before_num:
            ans += A_list[i]
            A_list[i] = 0

print(ans)

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

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

    else:
        goukei += b[i]
print(goukei)