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

import copy

List_a = copy.copy(List_A)
List_b = copy.copy(List_B)
count = 0

for i in range(N+1):
    if i >= 1:
        if List_a[i] > List_b[i-1]:
            count += List_b[i-1]
            List_a[i] -= List_b[i-1]
            List_b[i-1] = 0
        else:
            count += List_a[i]
            List_b[i-1] -= List_a[i]
            List_a[i] = 0
        
    if i < N:
        if List_a[i] > List_b[i]:
            count += List_b[i]
            List_a[i] -= List_b[i]
            List_b[i] = 0
        else:
            count += List_a[i]
            List_b[i] -= List_a[i]
            List_a[i] = 0

print(count)

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

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

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

pass_on = 0
count = 0
for i, x in enumerate(b):
    warriors = x + pass_on
    enemies = a[i]
    if warriors >= enemies:
        if pass_on >= enemies:
            pass_on = x
        else:
            pass_on = x - (enemies - pass_on)
        count += enemies
    else:
        count += warriors
        pass_on = 0
        
last = a[i+1]

if pass_on >= last:
    count += last
else:
    count += pass_on
    
print(count)

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

print(count)

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
answer = 0
for i in range(len(B)):
    if(B[i] == A[i]):
        answer += A[i]
    elif(B[i] < A[i]):
        answer += B[i]
    else:
        value = B[i] - A[i] - A[i+1]
        if(value >= 0):
            answer += A[i] + A[i+1]
            A[i+1] = 0
        else:
            check = B[i] - A[i]
            answer += A[i] + check
            A[i+1] -= check
print(answer)

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

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

b.append(0)

ans = 0

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

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

count = 0
can_kill = 0

for i in range(N+1):
  target = max(A[i] - can_kill,0)
  if i != N:
    if target <= B[i]:
      count += A[i]
      can_kill = B[i] - target
    else:
      count += B[i]+can_kill
      can_kill = 0
  else:
    if A[i] > can_kill:
      count += can_kill
    else:
      count += A[i]
print(count)

Solution (Python):
heroes = int(input())
monsters = [int(i) for i in input().split()]
slay = [int(i) for i in input().split()]

res = 0

for i in range(heroes):
    temp = 0
    if slay[i] <= monsters[i]:
        res += slay[i]
    else:
        res += monsters[i]
        temp = slay[i] - monsters[i]
        if temp <= monsters[i+1]:
            res += temp
            monsters[i+1] -= temp
        else:
            res += monsters[i+1]
            monsters[i+1] = 0

print(res)

Solution (Python):
N = input()
city = list(map(int,input().split(' ')))
people = list(map(int,input().split(' ')))

res=0
for i in range(len(people)):
    if (city[len(city)-i-1]>=people[len(people)-i-1]):
        res+=people[len(people)-i-1]
    else:
        res+=city[len(city)-i-1]
        diff = people[len(people)-i-1] - city[len(city)-i-1]
        if (city[len(city)-i-2]>=diff):
            city[len(city)-i-2] = (city[len(city)-i-2] - diff)
            res += diff
        else:
            res += city[len(city)-i-2]
            city[len(city)-i-2] = 0
print(res)

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

Solution (Python):
import sys
input = sys.stdin.readline
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] = b[i] - a[i]
        if b[i] - a[i+1] >= 0:
            ans += a[i + 1]
            a[i+1] = 0
        else:
            ans += b[i]
            a[i + 1] = a[i + 1] - b[i]
    else:
        ans += b[i]

print(ans)

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

total_A = sum(A)




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




for i in range(1, N):
    if B[i-1] >= A[i]:
        B[i-1] -= A[i]
        A[i] = 0
        continue
    if B[i-1] > 0:
        A[i] -= B[i-1]
        B[i-1] = 0
    if A[i] > 0:
        if A[i] > B[i]:
            A[i] -= B[i]
            B[i] = 0
        elif B[i] >= A[i]:
            B[i] -= A[i]
            A[i] = 0
    
    
    

if (A[-1] > 0) & (B[-1] > 0):
    if A[-1] >= B[-1]:
        A[-1] -= B[-1]
        B[-1] = 0
    elif B[-1] > A[-1]:
        A[-1] = 0
        B[-1] -= A[-1]



after_A = sum(A)

print(total_A - after_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 i != N-1:

    if A[i] >= B[i]:
      cnt += B[i]
    else:
      cnt += A[i]
      kantuu = B[i] - A[i]
      
      taosu = min(A[i+1], kantuu)
      cnt += taosu
      A[i+1] -= taosu
  else:
    cnt += min((A[i]+A[i+1]), B[i])
print(cnt)

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

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

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

cnt = 0
for i in range(N, 0, -1):
    if A[i] >= B[i-1]:
        cnt += B[i-1]
        A[i] -= B[i-1]
    elif B[i-1] > A[i]:
        cnt += A[i]
        B[i-1] -= A[i]
        if B[i-1] < A[i-1]:
            A[i-1] -= B[i-1]
            cnt += B[i-1]
        elif B[i-1] >= A[i-1]:
            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()))

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

print(s)

Solution (Python):
n = int(input())
a = [int(x) for x in input().split(' ')]
b = [int(x) for x in input().split(' ')]

b.append(0)
cnt = 0
y = 0

for c in range(n+1) :
    if(b[c] + y > a[c]):
        cnt += a[c]
        if(y>a[c]) :
            y = b[c]
        else :
            y = b[c] + y - a[c]
    else :
        cnt += b[c] + y
        y = 0
        

print(cnt)

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
L = 0
for i in range(len(B)):
    i_ = N-i-1
    if A[i_ +1]<= B[i_]:
        B_ = B[i_]-A[i_+1]
        A[i_+1]=0
        A[i_]=A[i_]-B_
        if A[i_]<0:
            L += A[i_]
            A[i_]=0
        L += B[i_]
    else:
        L += B[i_]
        A[i_+1]-=B[i_]
print(L)

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

c = 0

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

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

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

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

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

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

defeated = 0

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

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


A_list = A_list[::-1]
B_list = B_list[::-1]

count = 0
tmp = 0
for i, a in enumerate(A_list):
  
  if i < len(B_list):  
    if a <= B_list[i] + tmp:
      count += a
      if tmp > a:
        tmp = B_list[i]
      else:
        tmp = B_list[i] + tmp - a      
    else:
      count += B_list[i] + tmp
      tmp = 0            
  else:
    if a <= tmp:
      count += a
    else:
      count += tmp
    
print(count)

Solution (Python):

import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

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

r_monster = 0
monster_cnt = 0

if A[-1] > B[-1]:
    monster_cnt += B[-1]
    B[-1] = 0
else:
    monster_cnt += A[-1]
    B[-1] -= A[-1]
for a, b in zip(reversed(A[:-1]), reversed(B)):
    if a+r_monster > b:
        monster_cnt += b
        r_monster = min(a+r_monster - b, a)
    else:
        monster_cnt += a+r_monster
        r_monster = 0

print(monster_cnt)

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

monster_num = 0

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

    monster_num += min(a[i], b[i])
    if a[i] - b[i] < 0:
        a[i] = 0
    else:
        a[i] -= b[i]

print(monster_num)

Solution (Python):


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

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

    atk = B[i] if B[i] <= A[i+1] else A[i+1]
    A[i+1] = A[i+1] - atk
    B[i] = B[i] - atk
    res = res + atk

print(res)

Solution (Python):

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

count = 0
for i in reversed(range(n)):
    if a_list[i+1] >= b_list[i]:
       count += b_list[i]
       
    elif a_list[i+1] < b_list[i]:
        count += a_list[i+1]
        count += min(a_list[i], b_list[i]-a_list[i+1])
        a_list[i] = max(0,a_list[i] - (b_list[i] - a_list[i+1]))
        
    
print(count)

Solution (Python):
import sys

sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return list(map(int, sys.stdin.readline().split()))
def II(): return int(sys.stdin.readline())
def LS(): return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]

def main():
    n = II()
    a_s = LI()
    b_s = LI()
    b_s.append(0)
    ans = 0
    ans += min(a_s[0], b_s[0])
    for i in range(n):
        r = max(b_s[i] - a_s[i], 0)
        
        if r > a_s[i+1]:
            ans += a_s[i+1]
        else:
            ans += r
        a_s[i+1] = max(a_s[i+1] - r, 0)
        ans += min(a_s[i+1], b_s[i+1])
    print(ans)

if __name__ == '__main__':
    main()

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

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

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

        if a >= b:
            res += b
            A[i] = A[i] - b
        elif a < b:
            res += a
            A[i] = A[i] - a
            a_nx = A[i + 1]
            b -= a
            if a_nx >= b:
                res += b
                A[i + 1] = A[i + 1] - b
            elif a_nx < b:
                res += a_nx
                A[i + 1] = A[i + 1] - a_nx
    print(res)

if __name__ == '__main__':
    main()

Solution (Python):
import sys

n = int(sys.stdin.readline())
a_list = [int(x) for x in sys.stdin.readline().split()]
b_list = [int(x) for x in sys.stdin.readline().split()]
result = 0
for i in range(n):
    if b_list[i] <= a_list[i]:
        result += b_list[i]
    else:
        result += a_list[i]
        b_list[i] -= a_list[i]
        if b_list[i] <= a_list[i + 1]:
            a_list[i + 1] -= b_list[i]
            result += b_list[i]
        else:
            result += a_list[i + 1]
            a_list[i + 1] = 0
print(result)

Solution (Python):
import sys
import collections
import copy
input = sys.stdin.readline
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 B[i]-A[i] > 0:
		if B[i] < (A[i]+A[i+1]):
			ans += B[i]
			A[i+1] -= B[i] - A[i]
		else:
			ans += A[i] + A[i+1]
			A[i+1] = 0
	else:
		ans += B[i]
print(ans)

Solution (Python):

n = int(input())

an = list(map(int, input().split()) )
bn = list(map(int, input().split()) )


count = 0
for i in range(n):

    if an[i] + an[i+1] <= bn[i]:
        count += an[i] + an[i+1]
        an[i] = an[i+1] = 0

    else:
        
        dis = bn[i] - an[i]

    
        if dis > 0 :
            count += an[i] + dis
            an[i+1] -= dis
        else:
            count += bn[i]




print(count)

Solution (Python):
N = int(input())
ml = list(map(int,input().split()))
hl = list(map(int,input().split()))
ans = 0

for i,h in enumerate(hl):
    if(ml[i]<h):
        ans += ml[i]
        h -= ml[i]
        ml[i] = 0
        if(ml[i+1]<h):
            ans += ml[i+1]
            h -= ml[i+1]
            ml[i+1] = 0
        else:
            ans += h
            ml[i+1] -= h
            h = 0
    else:
        ans += h
        ml[i] -= h
        h = 0

print(ans)

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

new = old = 0
ans = 0
for monster, brave in zip(monsters, braves):
    new = brave
    m = monster

    damage = min(old, m)
    m -= damage
    ans += damage

    damage = min(new, m)
    new -= damage
    ans += damage

    old = new

print(ans)

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

nd0 = sum(a)

for i in reversed(range(n)):
    monster = a[i+1]
    yusha = b[i]
    if monster > yusha:
        b[i] = 0
        a[i+1] = monster - yusha
    else:
        b[i] = yusha - monster
        a[i+1] = 0
    yusha = b[i]
    monster = a[i]
    if monster > yusha:
        b[i] = 0
        a[i] = monster - yusha
    else:
        b[i] = yusha - monster
        a[i] = 0
        
ndend = sum(a)
print(nd0-ndend)

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

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

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

sum_ = 0
rest = 0

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

sum_ += min(a[n], rest)
print(sum_)

Solution (Python):
from collections import deque

N = int(input())
An = deque(map(int, input().split()))
Bn = deque(map(int, input().split()))

cnt = 0

while len(An) > 1:
    m = An.popleft()
    y = Bn.popleft()
    n = An[0]
    if y < m:
        cnt += y
    elif m <= y < m+n:
        cnt += y
        An[0] -= y-m
    else:
        cnt += m+n
        An[0] = 0
    
    
    

print(cnt)

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

anscount=[0]*(n+1)
for i in range((n+1)//2):
    now=i
    teki=a[now]-anscount[now]
    if teki<=b[now]:
        b[now]-=teki
        anscount[now]+=teki
        if a[now+1]<=b[now]:
            anscount[now+1]+=a[now+1]
            b[now]-=a[now+1]
        else:
            anscount[now+1]+=b[now]
            b[now]=0
    else:
        anscount[now]+=b[now]
        b[now]=0


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


































print(sum(anscount))

Solution (Python):
N = int(input())
A = [int(k) for k in input().split()]
B = [int(j) for j in input().split()]
D = [0] * (N+1)
for i in range(N):
	if A[i] >= B[i]:
		D[i]+=B[i]
	else:
		D[i]+=A[i]
		C = B[i]-A[i]
		if A[i+1] >= C:
			D[i+1]+=C
			A[i+1]-=C
		else:
			D[i+1]+=A[i+1]
			A[i+1]=0

print(sum(D))

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
c = a[:]
ini = sum(c)
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]:
    a[i+1] = 0
  else:
    a[i+1] -= b[i]

print(ini-sum(a))

Solution (Python):
a = int(input())
b = input().split()
teki = [int(i) for i in b]
c = input().split()
yuusya = [int(i) for i in c]

count = 0

if yuusya[0] >= teki[0]:
    yuusya[0] -= teki[0]
    count += teki[0]
    teki[0] = 0
else:
    count += yuusya[0]
    teki[0] -= yuusya[0]
    yuusya[0] = 0

for i in range(1, len(yuusya)):
    if yuusya[i - 1] > 0:
        if yuusya[i - 1] > teki[i]:
            count += teki[i]
            yuusya[i - 1] = 0
            teki[i] = 0
            continue
        else:
            count += yuusya[i-1]
            teki[i]-=yuusya[i-1]
    if yuusya[i] >= teki[i]:
        yuusya[i] -= teki[i]
        count += teki[i]
        teki[i] = 0
    else:
        count += yuusya[i]
        teki[i] -= yuusya[i]
        yuusya[i] = 0

if len(teki) > len(yuusya) and len(teki) > 2:
    if yuusya[i] >= teki[i+1]:
        yuusya[i] -= teki[i+1]
        count += teki[i+1]
    else:
        count += yuusya[i]
        teki[i] -= yuusya[i]
        yuusya[i] = 0

print(count)

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

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

Solution (Python):
n = int(input())
t = list(map(int, input().split()))
h = list(map(int, input().split()))

ret = 0
for i in range(len(h)):
    a = h[i]
    if a > t[i]:
        ret += t[i]
        a -= t[i]
    else:
        ret += a
        a = 0

    if a > 0:
        if a > t[i + 1]:
            ret += t[i + 1]
            t[i + 1] = 0
        else:
            ret += a
            t[i + 1] -= a

print(ret)

Solution (Python):
from collections import defaultdict
from collections import deque
from string import ascii_uppercase
import sys, bisect, math, heapq

stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N = read_int()[0]
A = read_int()
B = read_int()


def solve():
    init_A = sum(A)
    
    for i in range(N):
        b_residual = max(B[i] - A[i], 0)
        if b_residual == 0:
            A[i] -= B[i]
            continue
        A[i] = 0
        A[i + 1] = max(A[i + 1] - b_residual, 0)
    return init_A - sum(A)

if __name__ == "__main__":
    print(solve())

Solution (Python):
import sys
N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
sum = 0
for i in range(N):
    if B[i] > A[i]: 
        B[i] -= A[i]
        sum += A[i]
        if B[i] > A[i+1]:
            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()))
sum=0
for i in range(N):
    if A[i]+A[i+1]<=B[i]:
     sum+=A[i]+A[i+1]
     A[i+1]-=B[i]-A[i]
     if A[i+1]<0:
         A[i+1]=0
    else:
        sum+=B[i]
        if A[i]<=B[i]:
            A[i+1]-=B[i]-A[i]
print(sum)

Solution (Python):


def solve(N, A, B):
    ans = sum(A)
    for i, attack in enumerate(B):
        delta = min(A[i], attack)
        A[i] -= delta
        attack -= delta
        delta = min(A[i+1], attack)
        A[i+1] -= delta
    return ans - sum(A)

N = int(raw_input())
A = map(int, raw_input().split())
B = map(int, raw_input().split())
print solve(N, A, B)

Solution (Python):

from __future__ import division, print_function

import os
import sys
from __builtin__ import xrange as range
from cStringIO import StringIO
from future_builtins import ascii, filter, hex, map, oct, zip
from io import IOBase

import __pypy__


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

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

    print(cnt)




BUFSIZE = 8192


class FastI(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = StringIO()
        self.newlines = 0

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count("\n") + (not b)
            ptr = self._buffer.tell()
            self._buffer.seek(0, 2), self._buffer.write(b), self._buffer.seek(ptr)
        self.newlines -= 1
        return self._buffer.readline()


class FastO(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = __pypy__.builders.StringBuilder()
        self.write = lambda s: self._buffer.append(s)

    def flush(self):
        os.write(self._fd, self._buffer.build())
        self._buffer = __pypy__.builders.StringBuilder()


def print(*args, **kwargs):
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


sys.stdin, sys.stdout = FastI(sys.stdin), FastO(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")



if __name__ == "__main__":
    main()

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

answer = 0

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

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

        else:
            answer += B[i-1]
            A[i] -= B[i-1]
            B[i-1] = 0

    else:
        if A[i] < B[i] + B[i-1]:
            answer += A[i]
            if A[i] < B[i-1]:
                B[i-1] -= A[i]
                A[i] = 0
            else:
                B[i] -= (A[i]-B[i-1])
                B[i-1] = 0
                A[i] = 0
        else:
            answer += (B[i]+B[i-1])
            A[i] -= (B[i]+B[i-1])
            B[i],B[i-1] = 0,0

print(answer)

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

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

Solution (Python):
N = int(input())
list_a = list(map(int, input().split()))
list_b = list(map(int,input().split()))
sum1 = sum(list_a)
for i in range(N):
    k = list_a[i]-list_b[i]
    if k <0:
       list_a[i] = 0
    else:
       list_a[i] = k
    if k>0:
      continue
    if(list_a[i+1]+k < 0):
        list_a[i+1] = 0
    else:
        list_a[i+1] = list_a[i+1]+k

print(sum1 - sum(list_a))

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

all_mons_n = sum(a_list)
mons_list = []

for i, b in enumerate(b_list):
    mons_n = a_list[i]
    next_mons_n = a_list[i+1]
    can_hit_n = b

    if mons_n <= can_hit_n:
        a_list[i] = 0
        can_hit_n = can_hit_n - mons_n
    elif mons_n > can_hit_n:
        a_list[i] -= can_hit_n
        can_hit_n = 0

    if can_hit_n > 0:
        if next_mons_n <= can_hit_n:
            a_list[i+1] = 0
            can_hit_n = can_hit_n - next_mons_n
        elif next_mons_n > can_hit_n:
            a_list[i+1] -= can_hit_n

total = all_mons_n - sum(a_list)
print(total)

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

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

print(ans)

Solution (Python):
n = int(input())
a = [int(_) for _ in input().split()]
b = [int(_) for _ in input().split()]
res = 0
for i in range(n):
    j = n-1-i
    t = min(a[j+1],b[j])
    res += t
    b[j]-= t
    s = min(a[j],b[j])
    res += s
    a[j]-= s
print(res)

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

ans = 0
for i in xrange(n):
    can = b[i]
    x1 = min(a[i], can)
    a[i] -= x1
    can -= x1
    ans += x1

    x2 = min(a[i + 1], can)
    a[i + 1] -= x2
    can -= x2
    ans += x2

print ans

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

Solution (Python):
n = int(input())
cities = list(map(int,input().split()))
braves = list(map(int,input().split()))
braves.append(0)
count = 0
for i in range(n+1):
  c = min(braves[i],cities[i])
  count += c
  if i != n and c != braves[i]:
    d = min(braves[i]-c, cities[i+1])
    count += d
    cities[i+1] -= d
print(count)

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

print(cnt)