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):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline


def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, A, B):
    S = sum(B)

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

        ab = min(A[i+1], B[i])
        A[i+1] -= ab
        B[i] -= ab
    
    return S - sum(B)


def main():
    N = read_int()
    A = read_int_n()
    B = read_int_n()
    print(slv(N, A, B))


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

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 reversed(range(N)):
    ans += min(A[i + 1], B[i])
    A[i + 1], B[i] = max(0, A[i + 1] - B[i]), max(0, B[i] - A[i + 1])

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

print(ans)

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

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

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

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

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

    

ans = 0
for i in range(n + 1):
    ans = ans + (A2[i] - A[i])
    
    
print(ans)

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

defeated_monster = 0

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

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

monst=list(map(int,input().split()))
yusya=list(map(int,input().split()))
ans=0
for i in range(n):
  if(i==0):
    kill=min(yusya[0],monst[0])
    ans+=kill
    yusya[0]-=kill
    monst[0]-=kill
  else:
    kill=min(yusya[i-1],monst[i])
    ans+=kill
    yusya[i-1]-=kill
    monst[i]-=kill
    kill=min(yusya[i],monst[i])
    ans+=kill
    yusya[i]-=kill
    monst[i]-=kill
    
ans+=min(monst[-1],yusya[-1])

print(ans)

Solution (Python):

def minn(a,b):
  if a>b:
    return b
  return a
n=int(input())
a=list(map(int,input().split()))

b=list(map(int,input().split()))
sm=0
tmp=0

for i in range(n):
  tmp=minn(a[i],b[i])
  sm=sm+tmp
  a[i]=a[i]-tmp
  b[i]=b[i]-tmp
  tmp=minn(a[i+1],b[i])
  sm=sm+tmp
  a[i+1]=a[i+1]-tmp
  b[i]=b[i]-tmp
  
print(sm)

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

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

Solution (Python):



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

sum_hit_count = 0
next_knock_down = 0

for i in range(len(A)-1):
    remaining_enemy = A[i] - next_knock_down
    if remaining_enemy >= B[i]:
        sum_hit_count += B[i]
        next_knock_down = 0
        continue
    else:
        sum_hit_count += remaining_enemy
        remaining_capacity = B[i] - remaining_enemy
        if A[i+1] >= remaining_capacity:
            sum_hit_count += remaining_capacity
            next_knock_down = remaining_capacity
        else:
            sum_hit_count += A[i+1]
            next_knock_down = A[i+1]

print(sum_hit_count)

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

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

print(total)

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


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

for j in range(0, n+1):
    count += (before[j] - a[j])


print(count)

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

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

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

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

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

ans=0
for i in range(n):
    d=min(a[i],b[i])
    b[i]-=d
    ans+=d
    d=min(a[i+1],b[i])
    a[i+1]-=d
    ans+=d
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):
    aa=a[i]
    c+=min(a[i],b[i])
    a[i]=max(0,a[i]-b[i])
    if a[i]==0:
        c+=min(a[i+1],b[i]-aa)
        a[i+1]=max(0,a[i+1]+aa-b[i])
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(n):
    aa=a[i]
    c+=min(a[i],b[i])
    a[i]=max(0,a[i]-b[i])
    if a[i]==0:
        c+=min(a[i+1],b[i]-aa)
        a[i+1]=max(0,a[i+1]+aa-b[i])
print(c)

Solution (Python):
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def _I(): return int(sys.stdin.readline())
def _F(): return float(sys.stdin.readline())
def _pf(s): return print(s, flush=True)

N = _I()
A = LI()
B = LI()

"""
左からみていったときの不都合
特になし
"""
all_count = sum(A)
for bidx in range(len(B)):
    diff = min(A[bidx], B[bidx])
    A[bidx] -= diff
    B[bidx] -= diff
    diff = min(A[bidx+1], B[bidx])
    A[bidx+1] -= diff
    B[bidx] -= diff
final_count = sum(A)


print(all_count - final_count)

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

ans = 0

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

Solution (Python):
from collections import Counter
from collections import deque
from functools import reduce
from operator import mul
from pprint import pprint
import bisect
import copy
import fractions
import itertools
import math
import queue
import random
import sys
import time
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def MI(): return map(int, sys.stdin.readline().split())
def II(): return int(sys.stdin.readline())
def IS(): return input()
def C(x): return Counter(x)
def GCD_LIST(numbers): return reduce(fractions.gcd, numbers)
def LCM_LIST(numbers): return reduce(LCM, numbers)
def LCM(m, n): return (m * n // fractions.gcd(m, n))
def COMBINATION(n, r):
    if n < 0 or r < 0 or n < r:
        return 0
    r = min(n-r, r)
    if r == 0:
        return 1
    o = reduce(mul, range(n, n - r, -1))
    u = reduce(mul, range(1, r + 1))
    return o // u
class UnionFind():

    def __init__(self, n):
        self.n = n
        self.node = [i for i in range(n)]
        self.size = [1 for _ in range(n)]

    def same(self, x, y):
        return bool(self.root(x) == self.root(y))

    def root(self, x):
        if self.node[x] == x:
            return x
        else:
            self.node[x] = self.root(self.node[x])  
            return self.node[x]

    def unite(self, x, y):
        x, y = self.root(x), self.root(y)
        if x == y:
            return  
        if self.size[x] < self.size[y]:
            x, y = y, x
        self.node[y] = x
        self.size[x] += self.size[y]

    def return_size(self, x):
        return self.size[self.root(x)]


n = II()
a = LI()
b = LI()
cnt = 0
for i in range(len(b)):
    if a[n-i] >= b[n-1-i]:
        cnt += b[n-1-i]
        continue
    else:
        cnt += a[n-i]
        b[n-1-i] -= a[n-i]
        if a[n-1-i] >= b[n-1-i]:
            cnt += b[n-1-i]
            a[n-1-i] -= b[n-1-i]
        else:
            cnt += a[n-1-i]
            a[n-1-i] = 0
print(cnt)

Solution (Python):


import sys
import copy

sys.setrecursionlimit(1000000)


from collections import defaultdict



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

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

print(ans)

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

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

    ans = 0

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

    print(ans)


resolve()

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

print(count)

Solution (Python):
def calc(N, A, B):
    before = sum(A)
    for i in range(N - 1, -1, -1):
        if (B[i] >= A[i + 1]):
            B[i] -= A[i + 1]
            A[i] -= min(A[i], B[i])
            A[i + 1] = 0
        else:
            A[i + 1] -= B[i]
            B[i] = 0
    after = sum(A)
    return before - after


N = int(input())
A = [int(s) for s in input().split(' ')]
B = [int(s) for s in input().split(' ')]
print(calc(N, A, B))

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

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

Solution (Python):

N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split())) + [0]
sums = sum(A)
subs = 0
tmp = 0
for i in range(N+1):
    A[i] = max(0,A[i]-tmp)
    cmp = A[i] - B[i]
    if cmp > 0:
        subs += cmp
        tmp = 0
    else:
        tmp = -cmp

ans = sums - subs
print(ans)

Solution (Python):
n = int(input())
aa = list(map(int, input().split()))
num = sum(aa)
bb = list(map(int, input().split()))
for i in range(n):
    c = min(aa[i], bb[i])
    aa[i] -= c
    aa[i + 1] -= min(aa[i + 1], bb[i] - c)
print(num - sum(aa))

Solution (Python):

N = int(input())
AN = list(map(int, input().split()))
BN = list(map(int, input().split()))

ans = 0

for i in range(N):
    c1 = AN[i] if BN[i] >= AN[i] else BN[i]
    BN[i] -= c1
    AN[i] -= c1

    c2 = AN[i + 1] if BN[i] >= AN[i + 1] else BN[i]
    BN[i] -= c2
    AN[i + 1] -= c2

    ans += c1 + c2

print(ans)

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

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

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):
    defeated = min(A[i], B[i])
    count += defeated
    A[i] -= defeated
    B[i] -= defeated

    defeated = min(A[i+1], B[i])
    count += defeated
    A[i+1] -= defeated
    B[i] -= defeated

print(count)

Solution (Python):

import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import math






def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
 
def main():
    N = inputInt()
    A = inputList()
    B = inputList()
    
    ans = 0
    for i, val in enumerate(A):
        if i == N:
            break
        if B[i] <= val:
            ans += B[i]
        else:
            tmp = B[i] - val
            ans += val
            if i-1 <= N-1:
                if tmp <= A[i+1]:
                    ans += tmp
                    A[i+1] -= tmp
                else:
                    ans += A[i+1]
                    A[i+1] = 0
                    
    print(ans)
            	
if __name__ == "__main__":
	main()

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

count=0
for i in range(N):
    if a_list[i]<b_list[i]:
        c=b_list[i]-a_list[i]
        count+=a_list[i]
        a_list[i]=0
        if a_list[i+1]<c:
            count+=a_list[i+1]
            a_list[i+1]=0
        else:
            a_list[i+1]-=c
            count+=c
    else:
        a_list[i]-=b_list[i]
        count+=b_list[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):
    b = B[i]
    a0, a1 = A[i], A[i+1]

    taosu0 = min(a0, b)
    b -= taosu0

    taosu1 = min(a1, b)
    A[i+1] -= taosu1

    ans += taosu0 + taosu1

print(ans)

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

print(count)

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

total_monster = sum(A)
for n in range(N):
  A1 = A[n]
  A2 = A[n+1]
  B1 = B[n]
  if B1 < A1:
    A[n] = A1 - B1
    A[n+1] = A2
  elif A1 <= B1 < A1+A2:
    A[n] = 0
    A[n+1] = A1 + A2 - B1
  else:
    A[n] = 0
    A[n+1] = 0

remaining_monster = sum(A)
ans = total_monster - remaining_monster
print(ans)

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

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

f = [[0]*(N+1)]*(N+1)
f[0][0] = min(A[0],B[0])

sum_f = f[0][0]

for i in range(N):
    f[i][i+1] = min(A[i+1],B[i]-f[i][i])
    sum_f += f[i][i+1]
    if i == N-1:
        break
    f[i+1][i+1] = min(A[i+1]-f[i][i+1],B[i+1])
    sum_f += f[i+1][i+1]

print(sum_f)

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

counter =0
for i in range(0,len(B),1):
  if A[i] >= B[i]:
    counter +=B[i]
    A[i] -= B[i]
    B[i] = 0
    continue
  else:
    counter += A[i]
    B[i] -= A[i]
    A[i] =0
    if A[i+1] >= B[i]:
      counter +=B[i]
      A[i+1] -= B[i]
      B[i] =0
      continue
    else:
      counter += A[i+1]
      B[i] -= A[i+1]
      A[i+1] =0

print(counter)

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

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

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 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] = 0
    
print(cnt)

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

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


print(sum(A) - sum(e))

Solution (Python):


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

Ans = 0

for num in range(N):
    Ans += min(A[num], B[num])
    if A[num] >= B[num]:
        pass
    else: 
        B[num] -= A[num]
        Ans += min(A[num+1], B[num])
        if A[num+1] >= B[num]:
            A[num+1] -= B[num]
        else:
            A[num+1] = 0
print(Ans)

Solution (Python):

"""
Created on Mon Jul 29 02:16:51 2019

@author: matsui
"""

N=int(input())
A=input().split()
B=input().split()

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

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

xs = A[::]
last = 0
for i in range(N):
    if 0 < last:
        xs[i] = max(xs[i] - last, 0)
    if B[i] <= xs[i]:
        xs[i] -= B[i]
        last = 0
    else:
        last = B[i] - xs[i]
        xs[i] = 0
xs[N] = max(xs[N] - last, 0)
print(sum(A) - sum(xs))

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

Solution (Python):
from sys import stdin

n = int(stdin.readline().rstrip())
xs = [int(x) for x in stdin.readline().rstrip().split()]
ys = [int(x) for x in stdin.readline().rstrip().split()]

count = 0

for i in range(n):
   r = min(xs[i], ys[i])
   xs[i] -= r
   ys[i] -= r
   count += r
   r = min(xs[i+1], ys[i])
   xs[i+1] -= r
   count += r
   
print(count)

Solution (Python):
from sys import stdin

n = int(stdin.readline().rstrip())
a = [int(x) for x in stdin.readline().rstrip().split()]
b = [int(x) for x in stdin.readline().rstrip().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:
            count += b[i]
            a[i+1] -= b[i]
    else:
        count += b[i]
        a[i] -= b[i]

print(count)

Solution (Python):
from sys import stdin


def main():
    N = int(input())
    A_lists = [int(x) for x in stdin.readline().rstrip().split()]
    B_lists = [int(x) for x in stdin.readline().rstrip().split()]

    kazyou = 0
    sum_ = 0

    for A, B in zip(A_lists, B_lists):
        if (A-kazyou) <= B:
            sum_ += A
            if A-kazyou < 0:
                kazyou = B
            else:
                kazyou = abs((A-kazyou)-B)
            
        else:
            sum_ += B + kazyou
            kazyou = 0
            

    if A_lists[N] <= kazyou:
        sum_ += A_lists[N]
    else:
        sum_ += kazyou

    print(sum_)


if __name__ == "__main__":
    main()

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

Solution (Python):
N = int(input())
monster = [int(i) for i in input().split()]
brave_power = [int(i) for i in input().split()]
toubatsu_count = 0

for i in range(N):
    if brave_power[i]>monster[i]:
        toubatsu_count+=monster[i]
        nokori_brave = brave_power[i] - monster[i]
        
        if nokori_brave > monster[i+1]:
            toubatsu_count+=monster[i+1]
            monster[i+1]=0
        else:
            toubatsu_count+=nokori_brave
            monster[i+1]-=nokori_brave
    else:
        toubatsu_count+=brave_power[i]

print(toubatsu_count)

Solution (Python):
N = int(input())
monster = [int(i) for i in input().split()]
brave_power = [int(i) for i in input().split()]
toubatsu_count = 0

for i in range(N):
    if brave_power[i]>monster[i]:
        toubatsu_count+=monster[i]
        nokori_brave = brave_power[i] - monster[i]
        
        if nokori_brave > monster[i+1]:
            toubatsu_count+=monster[i+1]
            monster[i+1]=0
        else:
            toubatsu_count+=nokori_brave
            monster[i+1]-=nokori_brave
    else:
        toubatsu_count+=brave_power[i]

print(toubatsu_count)

Solution (Python):
import sys
sys.setrecursionlimit(100000)
input=sys.stdin.readline
def intinput():return map(int,input().split())


n=int(input())
a=list(intinput())
b=list(intinput())
ans=0
for i in range(n):
    if a[i]+a[i+1]<b[i]:
        ans+=a[i]+a[i+1]
        a[i+1]=0
    else:
        ans+=b[i]
        a[i+1]-=0 if a[i]-b[i]>0 else b[i]-a[i]
print(ans)

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

total = 0
for i in range(len(B_list)):
    
    
    result = A_list[i] - B_list[i]
    if result > 0:
        total += B_list[i]
        A_list[i] = A_list[i] - B_list[i]
        B_list[i] = 0
        continue

    total += A_list[i]
    B_list[i] = B_list[i] - A_list[i]
    A_list[i] = 0

    result = A_list[i+1] - B_list[i]
    if result > 0:
        total += B_list[i]
        A_list[i+1] = A_list[i+1] - B_list[i]
        B_list[i] = 0
    else:
        total += A_list[i+1]
        A_list[i+1] = 0
        B_list[i] = B_list[i] - A_list[i+1]




print(total)

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

Solution (Python):
import sys

def solve(N, A, B):
    ans = 0
    for i in range(N):
        if A[i] != 0:
            d = min(A[i], B[i])
            A[i] -= d
            B[i] -= d
            ans += d

        if A[i+1] != 0 and B[i] != 0:
            d = min(A[i+1], B[i])
            A[i+1] -= d
            B[i] -= d
            ans += d
    
    return ans

def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    print(solve(N, A, B))

if __name__ == "__main__":
    main()

Solution (Python):
def main38():
    num = int(input())
    dataa = input().split()
    datab = input().split()
    numa = []
    numb = []
    for i in range(num):
        numa.append(int(dataa[i]))
        numb.append(int(datab[i]))
    numa.append(int(dataa[num]))
    result = 0
    for i in range(num):
        if numa[i] >= numb[i]:
            result = result + numb[i]
        else:
            result = result + numa[i]
            numb[i] = numb[i] - numa[i]
            if numa[i+1] >= numb[i]:
                result = result + numb[i]
                numa[i+1] = numa[i+1] - numb[i]
            else:
                result = result + numa[i+1]
                numa[i+1] = 0
    print(result)

if __name__ == "__main__":
    main38()

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

print(ans)

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

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

print(enemy_count)

Solution (Python):
def smaller(x, y):
	if x < y:
		return x
	return y

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
sum = 0
for i in range(0, N):
	m = smaller(A[i], B[i])
	sum += m
	A[i] -= m
	B[i] -= m
	m = smaller(A[i+1], B[i])
	sum += m
	A[i+1] -= m
	B[i] -= m
print(sum)