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 sys


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

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

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

Solution (Python):

n = int(input())

a = list(map(int,input().split()))
b = list(map(int,input().split()))
cnt = 0
left = 0
for i in range(n):
    a1,b1 = a[i],b[i]
    if a1 <= b1 + left:
        cnt += a1
        left = min(b1 - a1 + left,b1)
    else:
        cnt += b1 + left
        left = 0
if a[n] <= left:
    cnt += a[n]
else:
    cnt += left
print(cnt)

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

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

e = sum(A)

print(s-e)

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

Solution (Python):

n = int(input())
m = list(map(int, input().split()))
y = list(map(int, input().split()))

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

print(cnt)

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

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

Solution (Python):
import sys

n= int(sys.stdin.readline())
a= [ int(x) for x in sys.stdin.readline().split() ]
b= [ int(x) for x in sys.stdin.readline().split() ]

c= 0
r= a[0]
for i in range(n):
    m = r + a[i+1]
    bi= b[i]
    c+= min(m,bi)
    
    if bi<r: r= a[i+1]
    elif bi<m: r= m - bi
    else: r=0

print( c )

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

Solution (Python):
import sys


def main():
    n_list = [int(s) for s in sys.stdin.readline().strip().split()]
    n = n_list[0]
    a = [int(s) for s in sys.stdin.readline().strip().split()]
    b = [int(s) for s in sys.stdin.readline().strip().split()]

    ans = 0
    carry = 0
    for i in range(n):
        can_kill_max = carry + b[i]
        if a[i] < can_kill_max:
            ans += a[i]
            left_for_hero_i = a[i] - carry
            if left_for_hero_i > 0:
                carry = b[i] - left_for_hero_i
            else:
                carry = b[i]
        else:
            ans += can_kill_max
            carry = 0
    ans += min(carry, a[n])
    print(ans)


if __name__ == '__main__':
    main()

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

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

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

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

print(ans)

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

print(ans)

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

        if st <= b:
            A[i] -= st
            ans += st
            st = 0
        else:
            ans += A[i]
            st -= A[i]
            A[i] = 0

    print(ans)


if __name__ == "__main__":
    resolve()

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

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

print(max_cnt)

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


hoge=a[-1]+a[-2]

y=b[-1]

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

print(count)

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

cnt = 0
next = A[0]

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

print(cnt)

Solution (Python):
if __name__ == '__main__':
    n = int(input())
    a = [int(s) for s in input().split()]
    b = [int(s) for s in input().split()]
    ans = 0
    if b[0] < a[0]:
        ans += b[0]
        a[0] -= b[0]
        b[0] = 0
    else:
        ans += a[0]
        b[0] -= a[0]
        a[0] = 0
    for i in range(n - 1):
        if b[i] < a[i + 1]:
            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
        if b[i + 1] < a[i + 1]:
            ans += b[i + 1]
            a[i + 1] -= b[i + 1]
            b[i + 1] = 0
        else:
            ans += a[i + 1]
            b[i + 1] -= a[i + 1]
            a[i + 1] = 0
    if b[n - 1] < a[n]:
        ans += b[n - 1]
        a[n] -= b[n - 1]
        b[n - 1] = 0
    else:
        ans += a[n]
        b[n - 1] -= a[n]
        a[n] = 0
    print(ans)

Solution (Python):

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

killed = 0
remain = 0
b.insert(0, 0)
for monstar, hero in zip(a[::-1], b[::-1]):
    if monstar <= remain:
        killed += monstar
        monstar = 0
    else:
        killed += remain
        monstar -= remain

    if monstar <= hero:
        killed += monstar
        remain = hero - monstar
    else:
        killed += hero
        remain = 0

print(killed)

Solution (Python):
import sys

sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    [N] = string_to_int(inputs[0])
    A = string_to_int(inputs[1])
    B = string_to_int(inputs[2])

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

        if A[j] <= B[i]:
            B[i] -= A[j]
            count += A[j]
            A[j] = 0
        else:
            A[j] -= B[i]
            count += B[i]
            B[i] = 0

    return count


def string_to_int(string):
    return list(map(lambda x: int(x), string.split()))


if __name__ == "__main__":
    ret = solve(inputs(3))
    print(ret)

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

Solution (Python):
N = int(input())
A = []
B = []
num = 0

for a in input().split():
	A.append(int(a))
for b in input().split():
	B.append(int(b))

while len(B) > 0:
	a = A.pop()
	b = B.pop()
	if a >= b:
		num += b
	else:
		num += a
		b -= a
		if A[len(A)-1] >= b:
			A[len(A)-1] -= b
			num += b
		else:
			num += A[len(A)-1]
			A[len(A)-1] = 0
	

print(num)

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]=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]

print(count)

Solution (Python):
import numpy as np

N = int(input())
A = np.array(input().split(), dtype=np.int64)
B = np.array(input().split(), dtype=np.int64)
ans = 0

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

Solution (Python):


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

    n = 0
    b0 = 0

    for a, b in zip(A[:-1], B):
        n2 = min(a, b0)
        n += n2

        a -= n2
        n3 = min(a, b)
        n += n3

        b0 = b - n3

    n += min(b0, A[-1])
    print(n)




if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
a = [0]
at = list(map(int, input().split()))
a.extend(at)
b = [0]
bt = list(map(int, input().split()))
b.extend(bt)
b.append(0)
ans = 0

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

print(ans)

Solution (Python):
import io
import sys


def IN_S():
  return input()


def IN_I():
  return int(input())


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


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


def STR_SPLIT(s, n):
  for l in range(0, len(s), n):
    yield s[0 + l:n + l]


def T_IN():
  global test_str
  sys.stdin = io.StringIO(test_str[1:-1])


test_str = '''
2
100 1 1
1 100
'''


def MAIN():
  
  A()


def A():
  n = IN_I()
  al = IN_L_I()
  bl = IN_L_I()

  killed = 0
  for i, b in enumerate(bl):
    a1 = al[i] - b
    if a1 >= 0:
      killed += b
      continue

    killed += al[i]

    b = b - al[i]
    a2 = al[i + 1] - b
    if a2 < 0:
      killed += al[i + 1]
      al[i + 1] = 0
    else:
      killed += b
      al[i + 1] -= b

  print(killed)
  return None


def B():
  return None


def C():
  return None


MAIN()

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

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

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

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

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

print(ans)

Solution (Python):

import sys

def get_num():
    return int(input())


def get_nums(n):
    res = [0 for _ in range(n)]
    for i in range(n):
        res[i] = int(input())
    return res


def get_numbers():
    return map(int, input().split())


n = get_num()
a = list(reversed(list(get_numbers())))
b = list(reversed(list(get_numbers())))

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

    cost = min(a[i + 1], b[i])
    a[i + 1] -= cost
    ans += cost

print(ans)

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

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

sum = 0

for i in range(n):
    ind = n - i - 1
    if b[ind] <= anext:
        sum += b[ind]
        anext = a[ind]
        continue
    elif (b[ind] > anext) and (b[ind] < anext + a[ind]):
        sum += b[ind]
        anext = anext + a[ind] - b[ind]
        continue
    else:
        sum += anext + a[ind]
        anext = 0

print(sum)

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

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

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

counter = 0

for i in range(N):
    if B_list[i] <= A_list[i]:
        
        counter += B_list[i]
    
    else:
        if B_list[i] <= A_list[i] + A_list[i+1]:
            A_list[i+1] = A_list[i+1] - (B_list[i] - A_list[i])
            counter += B_list[i]

        else:
            counter += A_list[i] + A_list[i+1]
            A_list[i+1] = 0

print(counter)

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

def main():
    N = int(input())
    A = [0] + [int(a) for a in input().split()]
    B = [0] + [int(b) for b in input().split()] + [0] + [0]
    
    ans = 0
    
    for i in range(1,N+2):
        for j in range(-1,1):
            kill = min(A[i],B[i+j])
            ans += kill
            A[i] -= kill
            B[i+j] -= kill
    print(ans)
            
    
    
    
if __name__ == "__main__":
    main()

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

result = sum(A)

for i in range(N):
    beat = min(A[i], B[i])
    A[i] -= beat 
    B[i] -= beat
    A[i + 1] -= min(A[i + 1], B[i])
    
result -= sum(A)

print(result)

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

count = 0
remain = 0

for a, b in zip(As, Bs + [0]):
    if a >= remain:
        count += remain
        a -= remain
    else:
        count += a
        remain = b
        continue
    if a >= b:
        count += b
        remain = 0
    else:
        count += a
        remain = b - a

print(count)

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

initial_monsters = sum(a_list)

for index, kill in enumerate(b_list):
    if a_list[index] > kill:
        a_list[index] = a_list[index] - kill
    else:
        remain_kill = kill - a_list[index]
        a_list[index] = 0
        a_list[index + 1] = a_list[index + 1] - remain_kill
        if a_list[index + 1] < 0:
            a_list[index + 1] = 0

print(initial_monsters - sum(a_list))

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

amari = 0
defeat = 0
for i in range(n):
    a_ = a[i]
    b_ = b[i]
    d1 = min(a_, amari)
    d2 = min(a_-d1, b_)
    defeat += (d1 + d2)
    amari = max(b_-d2, 0)

defeat += min(amari, a[-1])
print(defeat)

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

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

print(beated_monsters)

Solution (Python):
def resolve():
    n = int(input())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    mon=0
    for i,x in enumerate(b):
        mon = mon+min(a[i],x)
        tmp=x-a[i]
        if tmp>0:
            u = min(a[i+1],tmp)
            mon = mon+u
            a[i+1] = a[i+1]-u
    print(mon)


if __name__ == "__main__":
    resolve()

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

Solution (Python):
def c_city_savers(N, A, B):
    ans = 0
    for i in range(N):
        if A[i] >= B[i]:
            ans += B[i]
            A[i] -= B[i]
            B[i] = 0
            continue
        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]
                B[i] = 0
            else:
                ans += A[i + 1]
                B[i] -= A[i + 1]
                A[i + 1] = 0
    return ans

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

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

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

res = 0
for i in range(N):
    x = min(A[-1-i], B[-1-i])
    res += x
    A[-1-i] -= x
    y = min(A[-1-i], B[-2-i])
    res += y
    A[-1-i] -= y
    B[-2-i] -= y

x = min(A[0], B[0])
res += x
print(res)

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

count = 0
residual = A[0]

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

Solution (Python):
N = int(input())
As = [int(ai) for ai in input().split(' ')]
Bs = [int(bi) for bi in input().split(' ')]
S = sum(As)

As = As[::-1]
Bs = Bs[::-1]

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

print(S-sum(As))

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

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

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

print(cnt)

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

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

Solution (Python):
def main():
    n = int(input())
    ai = input().split(' ')
    bi = input().split(' ')

    count = 0
    for i in range(n):
        a0 = int(ai[i])
        b0 = int(bi[i])
        if b0 <= a0:
            count += b0
            continue
        else:
            count += a0
            a1 = int(ai[i+1])
            count += min(b0 - a0, a1)
            ai[i + 1] = str(max(a1 - (b0 - a0), 0))
    print(count)


if __name__ == '__main__':
    main()

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



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



print(defeated_num)

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

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

count = 0
for i in range(len(a)) : 
	count += _a[i] - a[i]
print(count)

Solution (Python):
import numpy as np
import math
import sys
sys.setrecursionlimit(10000000)

def main():
    N = int(input())
    A = [int(c) for c in input().split()]
    B = [int(c) for c in input().split()]
    cnt = 0
    for i in range(N):
        each_cnt = min(A[i], B[i])
        A[i] -= each_cnt
        B[i] -= each_cnt
        cnt += each_cnt
        if B[i] != 0:
            each_cnt = min(A[i + 1], B[i])
            A[i + 1] -= each_cnt
            B[i] -= each_cnt
            cnt += each_cnt
        else:
            continue
    print(cnt)
            


if __name__ == '__main__':
    main()

Solution (Python):
from sys import stdin
import math
import fractions
from collections import deque
from collections import Counter
import itertools

INF = 10 ** 10


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()]

ans = 0

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

print(ans)

Solution (Python):



import sys
import math
import bisect
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
mod = 10**9 + 7

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

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

print(ans)

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

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

    print(ans)

        
    





main()

Solution (Python):

from collections import defaultdict, deque
import math
from math import factorial
import fractions
import re

BIGNUM = 10 ** 9 + 7

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

    num = 0
    for i in range(N):
        idx = (N - 1) - i
        B = Bs[idx]
        A_prev = As[idx+1]
        A = As[idx]
        num_prev_monster = min([A_prev, B])
        B -= num_prev_monster
        num_monster = min([A, B])
        As[idx] -= num_monster
        num += num_prev_monster + num_monster

    print(num)

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

print(ans)

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

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

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

print(res)

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

A1, B1 = A[:], B[:]
ans1 = 0
for i in range(N):
    if A1[i] >= B1[i]:
        ans1 += B1[i]
    else:
        ans1 += A1[i] + min(A1[i + 1], B1[i] - A1[i])
        A1[i + 1] = max(0, A1[i + 1] - (B1[i] - A1[i]))

A2, B2 = A[:][::-1], B[:][::-1]
ans2 = 0
for i in range(N):
    if A2[i] >= B2[i]:
        ans2 += B2[i]
    else:
        ans2 += A2[i] + min(A2[i + 1], B2[i] - A2[i])
        A2[i + 1] = max(0, A2[i + 1] - (B2[i] - A2[i]))

print(max(ans1, ans2))