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 = input().split()
b = input().split()

count = 0

for i in range(n+1):
    city = n - i
    if (city != n):
        defeat = min(int(a[city]), int(b[city]))
        count = count + defeat
        a[city] = str(int(a[city]) - defeat)
    if (city != 0):
        defeat = min(int(a[city]), int(b[city-1]))
        count = count + defeat
        b[city-1] = str(int(b[city-1]) - defeat)

print(count)

Solution (Python):
sentence = input()
N = int(sentence)
sentence = input()
a_str = sentence.split()
a_list = [int(a) for a in a_str]
sentence = input()
b_str = sentence.split()
b_list = [int(b) for b in b_str]
a_list.reverse()
b_list.reverse()
remain = a_list.pop(0)
total_number = 0
for i, b in enumerate(b_list):
    if remain >= b:
        total_number += b
        remain = a_list[i]
    else:
        total_number += remain
        b_remain = b - remain
        monster_a = a_list[i]
        if monster_a >= b_remain:
            total_number += b_remain
            remain = monster_a - b_remain
        else:
            total_number += monster_a
            remain = 0
print(total_number)

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

total = 0
for i in range(N):
  b = B[i]
  a = A[i]

  if a >= b:
    total += b
    continue

  total += a
  b -= a

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

print(total)

Solution (Python):


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

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

if a[N] < n:
    t += a[N]
else:
    t += n

print(t)

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
last = 0
cnt = 0
for ind,i in enumerate(A):
    lmon = max(i-last,0)
    if(ind<N):
        lpow = B[ind]
    else:
        lpow = 0
    cnt += min(last+lpow,i)
    last = max(lpow-lmon,0)
print(cnt)

Solution (Python):
N = int(input())
AList = list(map(int, input().split()))
BList = list(map(int, input().split()))
BList.append(0)
CList = [0] 
Answer = 0
for i in range(0,N+1):
    if BList[i]+CList[i] > AList[i]:
        CList.append(min(BList[i]+CList[i]-AList[i],BList[i]))
        Answer += AList[i]
    else:
        CList.append(0)
        Answer += BList[i]+CList[i]
print(Answer)

Solution (Python):
import copy
N=int(input())
An=[int(x) for x in input().split()]
Bn=[int(x) for x in input().split()]
maxMon=sum(An)
for i,B in enumerate(Bn):
  An[i+1]=max(0,An[i+1]-(max(0,B-An[i])))
  An[i]=max(0,An[i]-B)

print(maxMon-sum(An))

Solution (Python):
N = int(input())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))
ans = 0
tmp = 0
for i in range(N):
    if a_list[i] >= b_list[i] + tmp:
        ans += b_list[i] + tmp
        tmp = 0
    else:
        ans += a_list[i]
        if (a_list[i] - tmp) <= 0:
            tmp = b_list[i]
        else:
            tmp = b_list[i] - (a_list[i] - tmp)


if tmp == 0:
    print(ans)
else:
    if tmp >= a_list[N]:
        print(ans + a_list[N])
    else:
        print(ans + tmp)

Solution (Python):
from copy import deepcopy


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

a = deepcopy(a_)
bef = 0
now = 0
ans = 0
for i in range(n):
    now = b[i]
    if bef >= a[i]:
        ans += a[i]
        bef = now
    else:
        ans += bef
        a[i] -= bef
        if now >= a[i]:
            ans += a[i]
            bef = now - a[i]
        else:
            ans += now
            bef = 0
if bef >= a[n]:
    ans += a[n]
else:
    ans += bef
ans1 = ans
a = deepcopy(a_)
a.reverse()
b.reverse()
bef = 0
now = 0
ans = 0
for i in range(n):
    now = b[i]
    if bef >= a[i]:
        ans += a[i]
        bef = now
    else:
        ans += bef
        a[i] -= bef
        if now >= a[i]:
            ans += a[i]
            bef = now - a[i]
        else:
            ans += now
            bef = 0
if bef >= a[n]:
    ans += a[n]
else:
    ans += bef
print(max(ans, ans1))

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

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

  
    
if kuriage > 0:
  sum += min(kuriage,int(a[n]))
    
print(sum)

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

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

monsters = [int(x) for x in input().split(' ')]
knights = [int(x) for x in input().split(' ')]

initial_num_monster = sum(monsters)

for pos, power in enumerate(knights):
    if monsters[pos] <= power:
        defeated = monsters[pos]
        monsters[pos] = 0
        rest_power = power - defeated
    else:
        monsters[pos] -= power
        continue

    monsters[pos+1] = max(monsters[pos+1] - rest_power, 0)

rest_monster = sum(monsters)
print(initial_num_monster - rest_monster)
exit(0)

Solution (Python):
n = int(input())
lia = list(map(int,input().split()))
lib = list(map(int,input().split()))
ans = 0
tmp = 0
for i in range(n):
  if tmp > lia[i]:
    ans += lia[i]
    tmp = lib[i]
  elif lia[i] <= lib[i] + tmp:
    ans += lia[i]
    tmp = lib[i] - lia[i] + tmp
  else:
    ans += lib[i] + tmp
    tmp = 0
ans += min(tmp,lia[n])
print(ans)

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

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

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

print(ans)

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

times = 0

for i in range(N):
    if As[i] - Bs[i] > 0:
        times += Bs[i]
    else:
        if Bs[i] - As[i] < As[i+1]:
            times += Bs[i]
            As[i+1] -= Bs[i] - As[i]
        else:
            times += As[i] + As[i+1]
            As[i+1] = 0

print(times)

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

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

goukei = 0
for i in range(N):
    hidari = min(A[i], B[i])
    nokori = max(0, B[i] - A[i])
    migi = min(A[i+1], nokori)
    A[i+1] = max(0, A[i+1] - nokori)
    goukei += hidari + migi


print(goukei)

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

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

print(teki)

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

count = 0

for i, b in enumerate(B):
    if(A[i] > b):
        count += b
    else:
        sum_monster = A[i] + A[i+1]
        killed_monster = min(b, A[i] + A[i+1])
        count += killed_monster
        A[i+1] = sum_monster - killed_monster
        
print(count)

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):
    for j in range(i,i+2):
        tmp = b[i]
        b[i] -= a[j]        
        if b[i]<=0:
            ans += tmp
            a[j] -= tmp
            break
        else:
            ans += a[j]
            a[j] = 0
            
print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
t = 0
left = 0
for i in range(N):
    a = A[i] - left
    b = B[i]
    if a>=b:
        t += b
        left = 0
    else:
        diff = b - a
        pos = A[i+1]
        if diff>=pos:
            t += pos
            left = pos
        else:
            t += diff
            left = diff
        t += a
print(t)

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

print(sum)

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

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

print(kill)

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

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

print(s-sum(a))

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

if res >= A[N]:
    sum_val += A[N]
else:
    sum_val += res

print(sum_val)

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

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

Solution (Python):
N = int(input())
ahoge = input().split()
bhoge = input().split()
a = []
b = []
for i in range(N):
  a.append(int(ahoge[i]))
  b.append(int(bhoge[i]))

a.append(int(ahoge[N]))
count = 0

for i in range(N-1,-1,-1):
  hoge = b[i] - a[i+1]
  if hoge > 0:
    count += a[i+1]
    huga = hoge - a[i]
    if huga > 0:
      count += a[i]
      a[i] = 0
    else:
      count += hoge
      a[i] = a[i] - hoge
  else:
    count += b[i]

print(count)

Solution (Python):
n = int(input())
monster = list(map(int,input().split()))
hero = list(map(int,input().split()))
s=0
for i in range(n):
    cur = hero[i]
    if cur>=monster[i]:
        cur-=monster[i]
        s+=monster[i]
        monster[i]=0
    else:
        monster[i]-=cur
        s+=cur
        cur=0
    if cur>0:
        if monster[i]>0:
            if cur >= monster[i]:
                cur -= monster[i]
                s += monster[i]
                monster[i] = 0
            else:
                monster[i] -= cur
                s+=cur
                cur = 0
        else:
            if i<=n-1:
                if cur >= monster[i+1]:
                    cur -= monster[i+1]
                    s += monster[i+1]
                    monster[i+1] = 0
                else:
                    monster[i+1] -= cur
                    s+=cur
                    cur = 0
print(s)

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

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

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


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

ans = maxans - ans

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


print(ans)

Solution (Python):
N = int(input())
towns = list(map(int, input().split()))
yusyas = list(map(int, input().split()))

ans = 0
restmons = towns[0]
for i, mons in enumerate(yusyas):
    if restmons > mons:  
        ans += mons
        restmons = towns[i + 1]
    else:
        if restmons + towns[i + 1] > mons:  
            ans += mons
            restmons = restmons - mons + towns[i + 1]
        else:  
            ans += restmons + towns[i + 1]
            restmons = 0
    

print(ans)

Solution (Python):
n = int(raw_input())
ms = map(int, raw_input().split(' '))
bs = map(int, raw_input().split(' '))
msorig = ms[:]

for i in range(len(bs)):
    if bs[i] >= ms[i]:
        ms[i+1] = max(0, ms[i+1] - (bs[i] - ms[i]))
    ms[i] = max(0, ms[i] - bs[i])

print sum(msorig) - sum(ms)

Solution (Python):



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

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

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

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
a_l = list(map(int, input().split()))
b_l = list(map(int, input().split()))

cnt = 0
for i in range(N):
    if a_l[i] <= b_l[i]:
        cnt += a_l[i]
        tmp1 = b_l[i] - a_l[i]
        if a_l[i+1] >= tmp1:
            cnt += tmp1
            a_l[i+1] -= tmp1
        else:
            cnt += a_l[i+1]
            a_l[i+1] = 0
    else:
        cnt += b_l[i]

print(cnt)

Solution (Python):
import sys

def main():
    input = sys.stdin.readline
    N = int(input())
    A = list(map(int, input().split()))
    B = [0] + list(map(int, input().split()))

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

        ans += min(a + rest, b)
        rest = max(0, min(a + rest - b, a))

    return ans


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

Solution (Python):
n = int(input())
city = list(map(int, input().split()))
hero = list(map(int, input().split()))
get = 0

if city[0] <= hero[0]:
    get = get + city[0]
    hero[0] = hero[0] - city[0]
else:
    get = get + hero[0]
    hero[0] = 0


for i in range(1,n):
    if city[i] <= hero[i-1]:
        get = get + city[i]
    elif (city[i] > hero[i-1]) & (city[i] <= hero[i] + hero[i-1]):
        get = get + city[i]
        hero[i] = hero[i] - (city[i] - hero[i-1])
    else:
        get = get + (hero[i-1] + hero[i])
        hero[i] = 0

if city[n] <= hero[n-1]:
    get = get + city[n]
else:
    get = get + hero[n-1]

print(get)

Solution (Python):


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

defeat_sum = 0
for i, Bn in enumerate(reversed(B)):
    i = N-i-1

    defeat_num = min(Bn, A[i+1])
    Bn -= defeat_num
    A[i+1] -= defeat_num
    defeat_sum += defeat_num

    defeat_num = min(Bn, A[i])
    Bn -= defeat_num
    A[i] -= defeat_num
    defeat_sum += defeat_num

print(defeat_sum)

Solution (Python):
N = int(input())
monster_list = list(map(int, input().split()))
Hero = list(map(int, input().split()))
ans = 0
for i, monster in enumerate(monster_list):
    if i == 0:
        h = Hero[i]
        if h >= monster:
            ans += monster
            Hero[i] -= monster
        else:
            ans += h
            Hero[i] = 0
    elif i == N:
        h = Hero[i-1]
        if h >= monster:
            ans += monster
        else:
            ans += h
    else:
        h1 = Hero[i-1]
        h2 = Hero[i]

        if h1 >= monster:
            ans += monster
        else:
            ans += h1
            monster -= h1
            if h2 >= monster:
                ans += monster
                Hero[i] -= monster
            else:
                ans += h2
                Hero[i] = 0

print(ans)

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

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

Solution (Python):
import sys


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


def main():
    n = int(input())
    a = [int(i) for i in input().split()]
    b = [int(i) for i in input().split()]
    ans = sum(a)

    a[n], b[n-1] = fight(a[n], b[n-1])

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

    a[0], b[0] = fight(a[0], b[0])
    print(ans-sum(a))


def fight(m, h):
    if m <= h:
        return 0, h-m
    else:
        return m-h, 0


if __name__ == "__main__":
    main()

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

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

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
sA = sum(A)
for i in range(N):
    b = B[i]
    if A[i] != 0:
        x = min(b, A[i])
        A[i] -= x
        b -= x
    x = min(b, A[i+1])
    A[i+1] -= x

print(sA - sum(A))

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

print(ans)

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

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

Solution (Python):
n=int(input())
a_ls=list(map(int,input().split()))
b_ls=list(map(int,input().split()))
ans=0
for i in range(n):
    first_monster=a_ls[i]
    second_monster=a_ls[i+1]
    beats=b_ls[i]
    if first_monster>=beats:
        a_ls[i]=first_monster-beats
        ans+=beats
    elif first_monster+second_monster<beats:
        a_ls[i],a_ls[i+1]=0,0
        ans+=first_monster+second_monster
    else:
        rest=beats-first_monster
        a_ls[i]=0
        a_ls[i+1]-=rest
        ans+=beats
print(ans)

Solution (Python):


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

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

ans += min(A[N], yoryoku)
print(ans)

Solution (Python):
import sys,heapq,math,resource
from collections import deque,defaultdict
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True 
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

def setrlim():
  sys.setrecursionlimit(250000)
  soft,hard = resource.getrlimit(RLIMIT_STACK)
  
  

n = inn()
a = inl()
b = inl()
sum = 0
for i in range(n-1,-1,-1):
    x = min(a[i+1],b[i])
    sum += x
    b[i] -= x
    x = min(a[i],b[i])
    sum += x
    a[i] -= x
print(sum)

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

co = sum(a)

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

print(co - sum(a))

Solution (Python):
import numpy as np

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


N=int(input())
a=acinput()
b=acinput()

SA=np.sum(a)
res=0
for i in range(N):
    
    if a[i]>=b[i]:
        a[i]-=b[i]
        b[i]=0
    else:
        b[i]-=a[i]
        a[i]=0
        

        a[i+1]-=b[i]
        if a[i+1]<0:
            a[i+1]=0

res=0
for sa in a:
    res+=sa
print(SA-res)

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]
        B[i] = 0
    else:
        ans += A[i]
        B[i] -= A[i]
        if A[i+1] >= B[i]:
            ans += B[i]
            A[i+1] -= B[i]
            B[i] = 0
        else:
            ans += A[i+1]
            B[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
for i in range(N):
    ans += min(A[i] + A[i+1], B[i])
    A[i+1] = max(A[i+1]-max(B[i]-A[i], 0), 0)
    
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):
  c += min(A[i],B[i])
  if A[i] < B[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())
arrmon = list(map(int,input().split()))
arrhero = list(map(int,input().split()))

count = 0
i = 1
mon = arrmon[0]
hero = arrhero[0]
while i <= n:
  numin = min(mon,hero)
  count += numin
  hero -= numin
  mon = arrmon[i]
  
  numin = min(mon,hero)
  count += numin
  mon -= numin
  if i < n:
    hero = arrhero[i]
  
  i += 1
print(count)

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

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


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


print(cnt)

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

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

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

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

print(suma)

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

Solution (Python):

import sys


def solve(N: int, A: "List[int]", B: "List[int]"):
    count = 0
    for i in range(N-1, -1, -1):
        if A[i+1] > B[i]:
            count += B[i]
        else:
            count += A[i+1]
            B[i] = B[i] - A[i+1]
            count += min(A[i], B[i])
            A[i] = max(A[i] - B[i], 0)
    print(count)
    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):
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

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

Solution (Python):
N = int(input())
As = [int(i) for i in input().split(" ")]
Bs = [int(i) for i in input().split(" ")]
ans = 0
for i in range(N):
    a = As[i]
    b = Bs[i]
    if a - b >= 0:
      ans += b
      As[i] = a - b
    else:
      rest = b - a
      ans += a
      As[i] = 0
      a2 = As[i + 1]
      if a2 - rest >= 0:
         As[i + 1] -= rest
         ans += rest
      else:
         As[i + 1] = 0
         ans += a2
print(ans)

Solution (Python):
import sys

input = sys.stdin.readline

N = int(input().strip())
As = list(map(int, input().strip().split(' ')))
Bs = list(map(int, input().strip().split(' ')))

count = 0
for i, b in enumerate(Bs):
    if As[i] >= b:
        count += b
    elif As[i] < b:
        if As[i+1] >= b - As[i]:
            count += b
            As[i+1] -= b - As[i]
        else:
            count += As[i] + As[i+1]
            As[i + 1] = 0
print(count)

Solution (Python):


n=int(input())
a=input().rstrip().split(" ")
b=input().rstrip().split(" ")
a[n]=int(a[n])
for i in range(n):
    a[i]=int(a[i])
    b[i]=int(b[i])
ans=0
for i in range(n):
    if(a[i] >b[i]):
        a[i]=a[i]-b[i]
        ans=ans+b[i]
       
        b[i]=0
    else:
        ans=ans+a[i]
        
        
        b[i]=b[i]-a[i]
        a[i]=0
        if(b[i] < a[i+1]):
            ans=ans+b[i]
           
            a[i+1]=a[i+1]-b[i]
            b[i]=0
        else:
            ans=ans+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()]

ans = sum(A)

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

ans -= sum(A)

print(ans)

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

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

Solution (Python):


import sys


n = int(input())

tmp = input().split()
hoge = list(map(lambda a: int(a), tmp))
a = []
tm=0
for i in range(0,n+1):
	tm+=hoge[i]
	a.append(hoge[i])




tmp = input().split()
hoge = list(map(lambda a: int(a), tmp))
b = []
for i in range(0,n):
	b.append(hoge[i])




for i in range(0,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]=a[i+1]-b[i]
		else:
			a[i+1]=0

tm2 = 0
for i in range(0,n+1):
	tm2+=a[i]


print(tm-tm2)

Solution (Python):
import sys

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

    rest = 0
    count = 0
    for a,b in zip(A,B):
        if rest > 0:
            count += min(a,rest)
            a -= rest
            if a <= 0:
                rest = b
                continue
        if a < b:
            rest = b-a
            count += a
        else:
            count += b
            rest = 0
    count += min(A[-1],rest)
    print(count)

if __name__=='__main__':
    main()