Problem:
 Score : 300 points    Problem Statement There are N+1 towns. The i -th town is being attacked by A_i monsters.  We have N heroes. The i -th hero can defeat monsters attacking the i -th or (i+1) -th town, for a total of at most B_i monsters.  What is the maximum total number of monsters the heroes can cooperate to defeat?      Constraints  All values in input are integers.  1 \leq N \leq 10^5  1 \leq A_i \leq 10^9  1 \leq B_i \leq 10^9         Input Input is given from Standard Input in the following format:  N  A_1  A_2  ...  A_{N+1}  B_1  B_2  ...  B_N       Output Print the maximum total number of monsters the heroes can defeat.        Sample Input 1 2
3 5 2
4 5      Sample Output 1 9  If the heroes choose the monsters to defeat as follows, they can defeat nine monsters in total, which is the maximum result.   The first hero defeats two monsters attacking the first town and two monsters attacking the second town.  The second hero defeats three monsters attacking the second town and two monsters attacking the third town.        Sample Input 2 3
5 6 3 8
5 100 8      Sample Output 2 22       Sample Input 3 2
100 1 1
1 100      Sample Output 3 3

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


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

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = [0]*(n+1)
tmp = 0
tmp_2 = 0

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

    if tmp - a[n-i] > 0:
        c[n-i] = a[n-i]
        
    else:
        c[n-i] = tmp
        

print(sum(c))

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

Solution (Python):
n=int(input())
monster=list(map(int,input().split()))
yusha=list(map(int,input().split()))
moto=sum(monster)

for i in range(1, n+1):
    if yusha[-i]<=monster[-i]:
        monster[-i]-=yusha[-i]
    else:
        monster[-i-1]=monster[-i-1]-yusha[-i]+monster[-i]
        monster[-i]=0
        if monster[-i-1]<0:
            monster[-i-1]=0



ans=moto-sum(monster)

print(ans)

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

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

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

cnt = 0
for i in range(N):
    cnt += min(A[i], B[i])
    if A[i] < B[i]:
        if A[i+1] > (B[i] - A[i]):
            cnt += (B[i] - A[i])
            A[i+1] = A[i+1] - (B[i] - A[i])
        else:
            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()))
cnt=0
A.append(0)
for i in range(N):
    if B[i]<=A[i]:
        cnt+=B[i]
    else:
        cnt+=A[i]
        t=max(0,A[i+1]+A[i]-B[i])
        cnt+=A[i+1]-t
        A[i+1]=t
print(cnt)

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

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

Solution (Python):
N = int(input())
A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]
pow = 0
ans = 0
for a, b in zip(A, B):
    ans += a if pow >= a else pow
    a = 0 if pow >= a else a - pow
    ans += a if a <= b else b
    pow = b - a if b >= a else 0
ans += A[N] if pow >= A[N] else pow
print(ans)

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

Solution (Python):
N = int(input())
L = list(map(int,input().split()))
M = list(map(int,input().split()))
begin = sum(L)
for i in range(N):
    if M[i] > L[i]:
        L[i+1] -= M[i] - L[i]
        L[i] = 0
        L[i+1] = max(L[i+1],0)
    else:
        L[i] -= M[i]
end = sum(L)
print(begin - end)

Solution (Python):

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

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

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

c = 0
bb = 0
for i in range(N):
    a = a_list[i]
    b = b_list[i]

    aa = a - bb
    if aa <= 0:
        c += a
        aa = 0
    else:
        c += bb

    if b <= aa:
        c += b
        bb = 0
    else:
        c += aa
        bb = b - aa

aa = a_list[-1] - bb
if aa <= 0:
    c += a_list[-1]
    aa = 0
else:
    c += bb

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

Solution (Python):



def city_saver(a, b):
    monster = 0
    for i in range(len(b)):
        monster += min(a[i], b[i])
        if(a[i]<b[i]):
            monster += min(b[i]-a[i], a[i+1])
            a[i+1] -= min(b[i]-a[i], a[i+1])
    return monster

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

if __name__ == "__main__":
    main()

Solution (Python):
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):
    for j in range(2):
        ans += min(b[i], a[i + j])
        b[i], a[i + j]= max(0, b[i] - a[i + j]), max(0, a[i + j] - b[i])
        
print(ans)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
b.append(0)
count = 0
for i in range(n+1):
  if a[i] >= b[i]:
    count += b[i]
  else:
    if a[i] + a[i+1] > b[i]:
      count += b[i]
      a[i+1] = 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_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))
ans = 0
for i, v in enumerate(b_list):
  enemy = a_list[i]
  if enemy >= b_list[i]:
    a_list[i] -= b_list[i]
    ans += b_list[i]
    b_list[i] = 0
  else:
    a_list[i] = 0
    b_list[i] = b_list[i] - enemy
    ans += enemy
    enemy = a_list[i + 1]
    if a_list[i + 1] >= b_list[i]:
      a_list[i + 1] -= b_list[i]
      ans += b_list[i]
      b_list[i] = 0
    else:
      a_list[i + 1] = 0
      b_list[i] = b_list[i] - enemy
      ans += enemy
print(ans)

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

n = int(input())
a_lst = list(map(int, input().split()))
b_lst = list(map(int, input().split()))
ans = 0

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

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

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

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

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

Solution (Python):
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
pre = a[0]
ans = 0
for i in range(n):
        if pre + a[i+1]-b[i] < 0:
                ans += pre + a[i+1]
                pre = 0
        elif 0<= pre + a[i+1]-b[i] <= a[i+1]:
                ans += b[i]
                pre = pre + a[i+1]-b[i]
        else:
                ans += b[i]
                pre = a[i+1]
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):
    if B[i] > A[i]:
        total += A[i]
        B[i] -= A[i]
        A[i] = 0
    else:
        total += B[i]
        A[i] -= B[i]
        B[i] = 0
    if B[i] > A[i+1]:
        total += A[i+1]
        B[i] -= A[i+1]
        A[i+1] = 0
    else:
        total += B[i]
        A[i+1] -= B[i]
        B[i] = 0
print(total)

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

ans = 0
x = 0

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

print(ans)

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

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

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

    d = min(a[i+1], b[i])
    a[i+1] -= d
    b[i] -= d
    cnt += d
print(cnt)

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

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

print(ans)

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

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

Solution (Python):
def getlist():
    return list(map(int, input().split()))

N = int(input())
A = getlist()
B = getlist()

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

print(ans)

Solution (Python):
n=int(input())
a = list(map(int, 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 a[i]+a[i+1]<b[i]:
            ans+=a[i+1]
            a[i+1]=0
        else:
            ans+=b[i]-a[i]
            a[i+1]=a[i+1]+a[i]-b[i]
print(ans)

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

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

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

Solution (Python):
if __name__ == '__main__':
	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 = ans + B[i]
			if A[i] > B[i]:
				pass
			else:
				A[i+1] = A[i+1] + A[i] - B[i]
		elif (A[i]+A[i+1]) <= B[i]:
			ans = 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()))

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

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

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

Solution (Python):
n=int(input())
T=list(map(int,input().split()))
AB=list(map(int,input().split()))
ans=0

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

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

pre_sum = sum(a)
limit = 0

for i in range(n):
  limit = b[i]
  if limit > a[i]:
    limit -= a[i]
    a[i] = 0
    if limit < a[i+1]:
      a[i+1] -= limit
      limit = 0
    else:
      a[i+1] = 0
      limit = 0
  else:
    a[i] -= limit
    limit = 0
    
result_sum = sum(a)
result = pre_sum - result_sum
print(result)

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

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

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

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

spare_win = now_win = spare = win = 0
ans = 0
for i in range(N):
    spare_win = 0
    if spare:
        spare_win = min(A[i], spare)
        spare = 0
    now_win = min( (A[i] - spare_win), B[i])
    spare = B[i] - now_win
    ans += (now_win + spare_win)
ans += min(A[N], spare)
print(ans)

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

result = 0
for i in range(N):
  f = min(A[i], B[i])
  s = min(A[i+1], B[i]-f)
  result += s+f
  A[i+1] -= s
  
print(result)

Solution (Python):
N = int(input())
A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]
pow = 0
ans = 0
for i in range(N):
    ans += A[i] if pow >= A[i] else pow
    A[i] = 0 if pow >= A[i] else A[i] - pow
    ans += A[i] if A[i] <= B[i] else B[i]
    pow = B[i] - A[i] if B[i] >= A[i] else 0
ans += A[N] if pow >= A[N] else pow
print(ans)

Solution (Python):

N = int(input())
ai = list(map(int, input().split()))
bi = list(map(int, input().split()))

dair = {}
dbir = {}
for i in range(N):
    dair.setdefault(i, ai[N-i])
    dbir.setdefault(i, bi[N-1-i])

dair.setdefault(N, ai[0])



ans = 0
for i in range(N):
    diff = dbir[i] - dair[i]
    if diff >= 0:
        ans += dair[i]
        diff2 = dair[i+1] - diff
        if diff2 >= 0:
            dair[i+1]=diff2
            ans += diff
        else:
            ans += dair[i+1]
            dair[i+1]=0
    else:
        ans += dbir[i]
    

print(ans)

Solution (Python):
import sys

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

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

Solution (Python):
n = int(input())
a = list(map(lambda x : int(x),input().split(" ")))
b = list(map(lambda x : int(x),input().split(" ")))
num = 0

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

print(ans)

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

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

killed = 0
for i in range(N):
    kill_l = min(A[i], B[i])
    kill_r = min(A[i + 1], B[i] - kill_l)
    A[i + 1] -= kill_r
    killed += (kill_l + kill_r)

print(killed)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = 0
for i in range(0,n):
  if a[i] > b[i]:
    c = c + b[i]
  elif a[i] <= b[i]:
    c = c + a[i]
    b[i] = b[i] - a[i]
    if b[i] < a[i+1]:
      c = c + b[i]
      a[i+1] = a[i+1] - b[i]
    else:
      c = 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(' ')))
NUM = sum(A)
for i in range(0,N):
  if B[i] >= A[i] + A[i+1]:
    A[i] = 0
    A[i+1] = 0
  elif B[i] >= A[i] and A[i+1] > B[i] - A[i]:
    A[i+1] = A[i+1] - (B[i] - A[i])
    A[i] = 0
  else:
    A[i] -= B[i]
print(NUM - sum(A))

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

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

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

a_arr = list(map(int, input().split()))
b_arr = list(map(int, input().split()))

a_left = a_arr[0]
b = b_arr[0]

counter = 0

for i in range(n):
    a_right = a_arr[i + 1]
    b = b_arr[i]
    killed = b if a_left > b else a_left
    b -= killed
    counter += killed
    killed = b if a_right > b else a_right
    a_left = a_right - killed
    counter += killed

print(int(counter))

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

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

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

print(score)

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

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

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

    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

print(count)

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


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

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

c = 0

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

print(c)

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

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

As = list(map(int, input().split()))
Bs = list(map(int, input().split()))
a_ = 0
b_ = 0
count = 0

for i in range(n):
  a = As[i]
  b = Bs[i]
  
  if a >= b_:
    count += b_
    a = a - b_
  else:
    count += a
    a = 0
  
  if a >= b:
    count += b
    b_ = 0
  else:
    count += a
    b_ = b - a

count += min(As[-1], b_)
print(count)

Solution (Python):
n = int(input())
towns = [int(s) for s in input().split()]
heros = [int(s) for s in input().split()]
p = 0
for i in range(n):
  rest = heros[i] - towns[i]
  if rest >= 0:
    p += towns[i]
    trest = towns[i + 1] - rest
    if trest < 0:
      p += towns[i + 1]
      towns[i + 1] = 0
    else:
      towns[i + 1] = trest
      p += rest
  else:
    p += heros[i]
print(p)

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

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

beaten_monster = sum(A) - sum(C)
print(beaten_monster)

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

cnt = 0
for i in reversed(range(n)):
    b = b_list[i]
    if a_list[i]+a_list[i+1] < b:
        cnt += a_list[i]+a_list[i+1]
        a_list[i+1] = 0
        a_list[i] = 0
    elif a_list[i+1] < b:
        cnt += b
        a_list[i] -= b - a_list[i+1]
        a_list[i+1] = 0
    else:
        cnt += b
        a_list[i+1] -= b
print(cnt)

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

j = 0
r = 0
for i in range(len(B)):
  cnt = i
  j = i
  while j <= cnt+1:

    if  j == len(A):
      break

    if B[i] >= A[j]:
      B[i] -= A[j]
      r += A[j]
      A[j] = 0
      j+=1
    else:
      A[j] -= B[i]
      r +=  B[i]
      break
print(r)

Solution (Python):
n=int(input())
T=list(map(int,input().split()))
AB=list(map(int,input().split()))
ans=0

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

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

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

        if power >= A[i]:
            count += A[i]
            A[i] = 0
        else:
            count += power
            A[i] -= power

print(count)

Solution (Python):
from sys import stdin
n = int(stdin.readline().rstrip())
A = list(map(int,stdin.readline().rstrip().split()))
B = list(map(int,stdin.readline().rstrip().split()))
point = 0
fa = min(A[0],B[0])
fb = min(A[1],B[0]-fa)
point += fb
point += fa
for i in range(1,n):
    fc = min(A[i]-fb,B[i])
    point += fc
    fb = min(A[i+1],B[i]-fc)
    point += fb
print(point)

Solution (Python):
city=int(input())
a=input().split(" ")
b=input().split(" ")
monster=[]
hero=[]
for p in a:
    monster.append(int(p))
for q in b:
    hero.append(int(q))
max=0
for s in range (0,city):
    if monster[s]-hero[s]>=0:
        max+=hero[s]
    elif monster[s+1]+monster[s]-hero[s]>0:
        max+=hero[s]
        monster[s+1]-=hero[s]-monster[s]
    else:
        max+=monster[s+1]+monster[s]
        monster[s+1]=0
print(max)

Solution (Python):

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

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

print(ans)

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


def attack(i, j):
    global a
    global b

    if a[i] >= b[j]:
        a[i] -= b[j]
        b[j] = 0
    else:
        b[j] -= a[i]
        a[i] = 0


for i in range(n):
    attack(i, i)
    attack(i + 1, i)

print(total - sum(a))

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

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

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

cnt = 0
for n in range(N):
    killed = min(B[n], A[n])
    res = B[n] - killed
    killed2 = min(res, A[n + 1])
    A[n + 1] -= killed2
    cnt += killed + killed2

print (cnt)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
result = 0
for k in range(N):
    if B[k] <= A[k]:
        result += B[k]
    else:
        remain = max([A[k+1] - (B[k] - A[k]) , 0])
        result += A[k] + A[k+1] - remain
        A[k+1] = remain
print(result)

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

print(count)

Solution (Python):
n = int(input())
mnstr = list(map(int,input().split()))
yshabl = list(map(int,input().split()))

cnt = 0

for i in range(-1,-1*len(yshabl)-1,-1):
	y = yshabl[i]
	m = mnstr[i]+mnstr[i-1]
	if m >= y:
		cnt += y
		if mnstr[i] >= y:
			pass
		else:
			mnstr[i-1] -= y - mnstr[i]
	else:
		cnt += m
		mnstr[i-1] = 0

print(cnt)