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 = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
for i in range(n):
    
    beaten = min(a[i], b[i])
    b[i] -= beaten
    
    beaten2 = min(a[i + 1], b[i])
    a[i + 1] -= beaten2
    ans += beaten + beaten2
print(ans)

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(n):
    beaten = min(a[i], b[i])
    b[i] -= beaten
    beaten2 = min(a[i + 1], b[i])
    a[i + 1] -= beaten2
    ans += beaten + beaten2
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):
    a_m = min(a[i], b[i])
    c += a_m
    b[i] -= a_m
    b_m = min(a[i+1], b[i])
    a[i+1] -= b_m
    c += b_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):
    a = min(A[i], B[i])
    ans += a
    B[i] -= a
    b = min(A[i + 1], B[i])
    A[i + 1] -= b
    ans += b
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]
		B[i]-=A[i]+A[i+1]
		A[i]=0
		A[i+1]=0
	elif A[i]<=B[i] and A[i]+A[i+1]>B[i]:
		count+=B[i]
		B[i]-=A[i]
		A[i+1]-=B[i]
		A[i]=0
		B[i]=0
	else:
		count+=B[i]
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):
    remb = max(b[i] - a[i], 0)
    geta = min(b[i], a[i]) + min(a[i+1], remb)
    a[i+1] = max(a[i+1] - remb, 0)

    ans = ans + geta


print(ans)

Solution (Python):
N = int(input())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))
monster_count = 0

for i in range(N):
    a_1, a_2 = A_list[i], A_list[i+1]
    b = B_list[i]
    if b>=a_1:
        monster_count = monster_count + a_1
        b = b - a_1
    elif b<a_1:
        monster_count = monster_count + b
        continue

    if b>=a_2:
        monster_count = monster_count + a_2
        A_list[i+1] = 0
    elif b<a_2:
        monster_count = monster_count + b
        a_2 = a_2 - b
        A_list[i+1] = a_2

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

"""

ans = [0] * n 
if b[0] >= a[1] + a[0]:
    ans[0] = a[1] + a[0]
elif ans[0] <= b[0]:
    ans[0] = 
"""

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(n):
    beat = min(a[i], b[i])
    a[i] -= beat
    b[i] -= beat
    if i != n:
        beat2 = min(a[i + 1], b[i])
        a[i + 1] -= beat2
    ans += beat + beat2
print(ans)

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

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

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

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))

n = ni()
a = na()
b = na()

attack = 0
apre = a[0]
for ai, bi in zip(a[1:], b):
    attackleft = min(apre, bi)
    attack += attackleft
    bi -= attackleft
    attackright = min(ai, bi)
    attack += attackright
    apre = ai - attackright

print(attack)

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

print(cnt)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
sum1=sum(a)
for i in range(n):
    if a[i]>=b[i]:
        a[i]-=b[i]
        b[i]=0
    elif b[i]>a[i]:
        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(sum1-sum(a))

Solution (Python):
def main():
    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]:
            B[i] -= A[i]
            ans += 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]
            B[i] = 0
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    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]:
            if A[i+1]<=B[i]-A[i]:
                count+=A[i+1]+A[i]
                A[i+1]=0
            else:
                count+=B[i]
                A[i+1]=A[i+1]-(B[i]-A[i])
        if A[i]>B[i]:
            count+=B[i]
    print(count)
if __name__=="__main__":
    main()

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

ans = 0
for i, b in enumerate(B):
    l = min(A[i], b)
    b -= l
    r = min(A[i + 1], b)
    A[i] -= l
    A[i + 1] -= r

    ans += l + r

print(ans)

Solution (Python):
n = int(input())
a, b = [list(map(int, input().split())) for i in range(2)]

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

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

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

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

Solution (Python):
N = int(input())
A = [0]+list(map(int,input().split()))+[0]
B = [0]+list(map(int,input().split()))+[0]
ans = 0
for i in range(1, N+2):
    c = min(A[i], B[i-1])
    A[i] -= c
    ans += c
    c = min(A[i], B[i])
    A[i] -= c
    B[i] -= c
    ans += c
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 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]
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 b[i] >= a[i]:
        cnt += a[i]
        b[i] = b[i] - a[i]
        if b[i] >= a[i+1]:
            cnt += a[i+1]
            a[i+1] = 0
        else:
            a[i+1] = a[i+1]-b[i]
            cnt += b[i]
    else:
        cnt += b[i]
print(cnt)

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
c = 0
for i in range(0, n):
    c += min(A[i], B[i])
    if B[i] > A[i]:
        c += min(A[i+1], (B[i] - A[i]))
        A[i+1] -= min(A[i+1], (B[i] - A[i]))
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):
    if A[i] >= B[i]:
        count = count + B[i]
    else:
        count = count + min(A[i]+A[i+1], B[i])
        A[i+1] = A[i+1] - min(A[i+1], B[i]-A[i])
print(count)

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

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

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

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

res = 0

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

print(res)

Solution (Python):
import sys

n = int(sys.stdin.readline().rstrip())
*a, = map(int, sys.stdin.readline().split())
*b, = map(int, sys.stdin.readline().split())

def attack(i, j, cnt):
    if b[i] <= a[j]:
        a[j] -= b[i]
        cnt += b[i]
        b[i] = 0
    else:
        b[i] -= a[j]
        cnt += a[j]
        a[j] = 0
    return cnt

def main():
    cnt = 0
    for i in range(n):
        cnt = attack(i, i, cnt)
        cnt = attack(i, i+1, cnt)
    return cnt

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

Solution (Python):
import sys

n = int(sys.stdin.readline().rstrip())
*a, = map(int, sys.stdin.readline().split())
*b, = map(int, sys.stdin.readline().split())

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

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

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

res = 0
rem = 0

for i in range(N):
    mobs_rem = min(A[i], rem)
    A[i] -= mobs_rem
    mobs = min(A[i], B[i])
    rem = B[i] - mobs
    res += (mobs+mobs_rem)

res += min(A[N], rem)
print(res)

Solution (Python):
n = int(input())
data = list(map(int,input().split()))
init = sum(data)
datb = list(map(int,input().split()))
res = 0

for i in range(len(datb)):
    
    
    dmg = min(datb[i], data[i])
    data[i] = data[i] - min(datb[i], data[i])
    res += dmg

    datb[i] -= dmg
    datb[i] = 0 if datb[i] < 0 else datb[i]
    

    res += min(datb[i], data[i+1])
    data[i+1] = data[i+1] - min(datb[i], data[i+1])
    

print(init - sum(data))

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

Solution (Python):
N=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
i=0
kill=0
while i<N:
    if b[i]>a[i]:
        c=b[i]-a[i]
        if a[i+1]>=c:
            a[i+1]-=c
            kill+=b[i]
        else:
            kill+=a[i]+a[i+1]
            a[i+1]=0
    else:
        kill+=b[i]
    i+=1
print(kill)

Solution (Python):
n = int(input())
a_lst = list(map(int,input().split()))
b_lst = list(map(int,input().split()))
monster = 0

for i in range(n):
    a1 = a_lst[i]
    a2 = a_lst[i+1]
    b1 = b_lst[i]
    tmp = b1-a1

    monster += min(a1,b1)
    if tmp>0:
        if a2-tmp<=0:
            monster += a2
            a_lst[i+1] = 0
        else:
            monster += tmp
            a_lst[i+1] -= tmp

print(monster)

Solution (Python):
n = int(input())
A = [int(x) for x in input().split()][::-1]
B = [int(x) for x in input().split()][::-1]
beats = 0
carry = 0
for a, b in zip(A, B):
    beats = beats + min(b + carry, a)
    carry = max(b - max(a - carry, 0), 0)
print(beats + min(A[-1], carry))

Solution (Python):


def main():
  count = int(input())
  monsters = list(map(int, input().split()))
  monsters_at_start = sum(monsters)
  heroes = list(map(int, input().split()))
  
  for i, hero in enumerate(heroes):
    if monsters[i] >= hero:
      monsters[i] -= hero
      
    elif monsters[i] < hero:
      hero -= monsters[i]
      monsters[i] = 0
      if monsters[i + 1] >= hero:
        monsters[i + 1] -= hero
        
      else:
        monsters[i + 1] = 0
        
  print(monsters_at_start - sum(monsters))
  
  return 0

if __name__== '__main__':
  main()

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

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

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

print(s1 - sum(A))

Solution (Python):
n = int(input())
aaa = list(map(int, input().split()))
sa = sum(aaa)
bbb = list(map(int, input().split()))
for i, b in enumerate(bbb):
    c = min(aaa[i], b)
    aaa[i] -= c
    aaa[i + 1] -= min(aaa[i + 1], b - c)
print(sa - sum(aaa))

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

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

print(cnt)

Solution (Python):
import sys
input = sys.stdin.buffer.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 += B[i]
    else:
        ans += A[i]
        tmp = min(B[i]-A[i], A[i+1])
        ans += tmp
        A[i+1] -= tmp
print(ans)

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

Solution (Python):



class Monster:
    def __init__(self, hp):
        self.hp = int(hp)


class Hero:
    def __init__(self, hp):
        self.hp = int(hp)

    def attack(self, monster_1st, monster_2nd):
        if self._attack(monster_1st):
            self._attack(monster_2nd)

    def _attack(self, monster):
        if monster.hp == self.hp:
            monster.hp = 0
            self.hp = 0
            return False
        elif monster.hp > self.hp:
            monster.hp -= self.hp
            self.hp = 0
            return False
        elif monster.hp < self.hp:
            self.hp -= monster.hp
            monster.hp = 0
            return True


def main():
    _ = int(input())
    monsters = list(map(Monster, input().split(' ')))
    monsters_at_start = sum(monster.hp for monster in monsters)
    heroes = list(map(Hero, input().split(' ')))

    for i, hero in enumerate(heroes):
        hero.attack(monsters[i], monsters[i + 1])

    print(monsters_at_start - sum(monster.hp for monster in monsters))

    return 0


if __name__ == '__main__':
    main()

Solution (Python):



def main():

    count = int(input())

    monsters = list(map(int, input().split(' ')))

    monsters_at_start = sum(monsters)

    heroes = list(map(int, input().split(' ')))

    for i, hero in enumerate(heroes):

        if monsters[i] >= hero:

            monsters[i] -= hero

        elif monsters[i] < hero:

            hero -= monsters[i]

            monsters[i] = 0

            if monsters[i + 1] >= hero:

                monsters[i + 1] -= hero

            else:

                monsters[i + 1] = 0


    print(monsters_at_start - sum(monsters))

    return 0


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque

N = int(input())
Monsters = deque(map(int, input().split()))
Fighters = deque(map(int, input().split()))

Knockouts = 0
prev_ftr = 0

while len(Monsters) > 0:
  mon = Monsters.popleft()
  ko1 = min(mon, prev_ftr)
  
  Knockouts += ko1
  mon -= ko1

  if len(Fighters) > 0:
    ftr = Fighters.popleft()
    ko2 = min(mon, ftr)
    
    Knockouts += ko2
    mon -= ko2
    prev_ftr = ftr - ko2

print(Knockouts)

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):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()
B = LIST()

dest = 0
nokori = 0
for i in reversed(range(N)):
	if A[i + 1] >= nokori:
		dest += nokori
		mons_hp = A[i + 1] - nokori
	else:
		dest += A[i + 1]
		mons_hp = 0

	if mons_hp > B[i]:
		nokori = 0
		dest += B[i]
	else:
		nokori = B[i] - mons_hp
		dest += mons_hp

if A[0] > nokori:
	dest += nokori
else:
	dest += A[0] 

print(dest)

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):
    if A[i]>B[i]:
        ans+=B[i]
        A[i]-=B[i]
    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]
        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()))
a.append(0)
b.append(0)
b.append(0)
ans = 0
for i in range(n+1):
    if a[i]-b[i] > 0:
        ans += b[i]
    elif a[i]-b[i] <= 0 and a[i] + a[i+1] >= b[i]:
        b[i+1] += b[i]-a[i]
        ans += a[i]
    else:
        b[i+1] += a[i+1]
        ans += a[i]
print(ans)

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

if __name__ == '__main__':
    main()

Solution (Python):
def main():
  import sys
  n = int(input())
  a = list(map(int,sys.stdin.readline().split()))
  b = list(map(int,sys.stdin.readline().split()))
  
  count=0
  buffer=0
  
  for c,h in zip(a,b):
    count += min(buffer,c)
    g = max(0,c-buffer)
    count += min(h,g)
    buffer=max(0,h-g)
  else:
    count+=min(buffer,a[-1])
    
    
  print(count)
  
if __name__=="__main__":
  main()

Solution (Python):
def main():
  import sys
  n = int(input())
  a = tuple(map(int,sys.stdin.readline().split()))
  b = tuple(map(int,sys.stdin.readline().split()))
  
  count=0
  buffer=0
  
  for c,h in zip(a,b):
    count += min(buffer,c)
    g = max(0,c-buffer)
    count += min(h,g)
    buffer=max(0,h-g)
  else:
    count+=min(buffer,a[-1])
    
    
  print(count)
  
if __name__=="__main__":
  main()

Solution (Python):
def main():
  import sys
  n = int(input())
  a = tuple(map(int,sys.stdin.readline().split()))
  b = tuple(map(int,sys.stdin.readline().split()))
  
  count=0
  buffer=0
  
  for i in range(n):
    c = a[i]
    h = b[i]
    count += min(buffer,c)
    g = max(0,c-buffer)
    count += min(h,g)
    buffer=max(0,h-g)
  else:
    count+=min(buffer,a[-1])
    
    
  print(count)
  
if __name__=="__main__":
  main()

Solution (Python):
def main():
  import sys
  n = int(input())
  a = tuple(map(int,sys.stdin.readline().split()))
  b = tuple(map(int,sys.stdin.readline().split()))
  
  count=0
  cNow=a[0]
  cNext=a[1]
  
  for i in range(n):
    cNext=a[i+1]
    h = b[i]
    count+=min(cNow,h)  
    if h>cNow:
        count+=min(h-cNow,cNext)
        cNow=max(0,cNext-(h-cNow))
        continue
    cNow=cNext
  print(count)
  
if __name__=="__main__":
  main()

Solution (Python):
def main():
  import sys
  n = int(input())
  a = list(map(int,sys.stdin.readline().split()))
  b = list(map(int,sys.stdin.readline().split()))
  
  count=0
  
  for i in range(n):
    c1 = a[i] 
    c2 = a[i+1]
    h = b[i]
    count+=min(c1,h)  
    if h>c1:
        count+=min(h-c1,c2)
        a[i+1]=max(0,c2-(h-c1))
 
  print(count)
  
if __name__=="__main__":
  main()

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

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


print(total)

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


ans = 0

if B[0] >= A[0]:
    ans += A[0]
    B[0] -= A[0]
else:
    ans += B[0]
    B[0] = 0
    
A = A[1:]
B.append(0)

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

Solution (Python):


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

total = 0
for i in range(len(a) - 1):
    s = min(a[i], b[i])
    a[i] = a[i] - s
    b[i] = b[i] - s

    t = min(a[i+1], b[i])
    a[i+1] = a[i+1] - t
    b[i] = b[i] - t
    total += s + t


print(total)

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

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

Solution (Python):
def f(a, b, pb):
    return min(a, b + pb), max(0, b - max(a - pb, 0))

def g(A, B):
    ans, pb = 0, 0
    for a, b in zip(A, B):
        k, pb = f(a, b, pb)
        ans += k
    return ans

N = int(input())
A = tuple(map(int, input().split()))
B = tuple(map(int, input().split())) + (0,)
print(g(A, B))

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

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

if sum(a)>0:
    ans = sums-sum(a)
else:
    ans = sums
print(ans)

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

a_sum = sum(a)
aa = a[::-1]
bb = b[::-1]

for i in range(n):
    aa[i + 1] = max(0, aa[i + 1] - max(0, bb[i] - aa[i]))
    aa[i] = max(0, aa[i]-bb[i])

print(a_sum-sum(aa))

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

Solution (Python):
import sys
from collections import deque, Counter, defaultdict
from fractions import gcd
input = sys.stdin.readline

def main():
    n = int(input())
    As = list(map(int, input().split()))
    Bs = list(map(int, input().split()))

    c1 = sum(As)

    for i in range(n):
        
        if As[i]- Bs[i] > 0:
            As[i] -= Bs[i]
            Bs[i] = 0
        else:
            Bs[i] -= As[i]
            As[i] = 0

            if As[i+1] - Bs[i] > 0:
                As[i+1] -= Bs[i]
                Bs[i] = 0
            else:
                Bs[i] -= As[i]
                As[i+1] = 0
                
    c2 = sum(As)
    
    print(c1-c2)
        
    return

if __name__ == '__main__':
    main()

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

bef = sum(A)
for i, n in enumerate(B):
    if n>A[i]:
        n-=A[i]
        A[i]=0
        
        if n>A[i+1]:
            n-=A[i+1]
            A[i+1]=0
        else:
            A[i+1]-=n
            n=0
    else:
        A[i]-=n
        n=0

print(bef-sum(A))

Solution (Python):
n = int(input())
cities = list(map(int, input().split(' ')))
braves = list(map(int, input().split(' ')))
enemy_count = sum(cities)

for i, b in enumerate(braves):
  for _ in range(2):
    b = braves[i]
    if b <= 0:
      break
    if cities[i] > 0:
      if b >= cities[i]:
        braves[i] = b - cities[i]
        cities[i] = 0
      else:
        braves[i] = 0
        cities[i] = cities[i] - b
    elif cities[i] == 0:
      if b >= cities[i+1]:
        braves[i] = b - cities[i+1]
        cities[i+1] = 0
      else:
        braves[i] = 0
        cities[i+1] = cities[i+1] - b
    else:
      raise Exception

kill_count = enemy_count - sum(cities)
print(kill_count)

Solution (Python):
N = int(input())
A = [-1] + [int(a) for a in input().split()]
B = [-1] + [int(b) for b in input().split()]

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

print(count)

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 = list(map(int,input().split()))
b = list(map(int,input().split()))

ms = 0

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

    if a[i+1]>=b[i]:
        a[i+1] = a[i+1]-b[i]
        ms += b[i]
        b[i] = 0
    else:
        b[i] = b[i] - a[i+1]
        ms += a[i+1]
        a[i+1] = 0

print(ms)

Solution (Python):



def ri():
    return int(input())

def rl():
    return list(input().split())

def rli():
    return list(map(int, input().split()))

def main():
    n = ri()
    la = rli()
    lb = rli()
    ans = 0
    for i in range(n):
        if la[i] <= lb[i]:
            ans += la[i]
            lb[i] -= la[i]
        else:
            ans += lb[i]
            lb[i] = 0
        b2 = min(lb[i], la[i+1])
        ans += b2
        la[i+1] -= b2
    print(ans)


if __name__ == '__main__':
    main()

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