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]"):
    counter = 0
    for i in range(N):
        if A[i] >= B[i]:
            counter += B[i]
            A[i] -= B[i]
            B[i] = 0
        else:
            counter += A[i]
            B[i] -= A[i]
            A[i] = 0
            if A[i+1] >= B[i]:
                counter += B[i]
                A[i+1] -= B[i]
                B[i] = 0
            else:
                counter += A[i+1]
                B[i] -= A[i+1]
                A[i+1] = 0
    counter += min(A[N-1] + A[N], B[N-1])
    print(counter)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = [ int(next(tokens)) for _ in range(N+1) ]  
    B = [ int(next(tokens)) for _ in range(N) ]  
    solve(N, A, B)

if __name__ == '__main__':
    main()

Solution (Python):
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
Max = sys.maxsize
def l(): 
	return list(map(int,input().split()))
def m(): 
	return map(int,input().split())
def onem(): 
	return int(input())
def s(x): 
	a = []
	aa = x[0]
	su = 1
	for i in range(len(x)-1):
		if aa == x[i+1]:
			a.append([aa,su])
			aa = x[i+1]
			su = 1
		else:
			su += 1
	a.append([aa,su])
	return a
def jo(x): 
	return " ".join(map(str,x))
def max2(x): 
	return max(map(max,x))

n = onem()

a = l()

b = l()

co = 0

if a[0] <= b[0]:
	co += a[0]
	nokori = b[0] - a[0]
	a[0] = 0
else:
	nokori = 0
	co = b[0]


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

if nokori >= a[-1]:
	co += a[-1]
else:
	co += nokori

print(co)

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

Solution (Python):
import sys, math, itertools, bisect, copy, re
from collections import Counter, deque, defaultdict






INF = float('inf')
MOD = 10 ** 9 + 7
EPS = 10 ** -7
sys.setrecursionlimit(1000000)








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

R1 = [0] * N
R2 = [0] * N
for i in range(N - 1, -1, -1):
    R1[i] = min(A[i + 1], B[i])
    B[i] -= R1[i]
    R2[i] = min(A[i], B[i])
    A[i] -= R2[i]

ans = sum(R1) + sum(R2)
print(ans)

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

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

temp=min(A[0],B[0])
kill+=temp
A[0]-=temp
B[0]-=temp

for i in range(1,N+1):
	temp=min(A[i],B[i-1])
	kill+=temp
	A[i]-=temp
	B[i-1]-=temp
	
	temp=min(A[i],B[i])
	kill+=temp
	A[i]-=temp
	B[i]-=temp

print(kill)

Solution (Python):
N = int(input())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))
count = 0
for i in range(N):
	m1 = min(a_list[i], b_list[i])
	count += m1
	b_list[i] -= m1
	m2 = min(a_list[i + 1], b_list[i])
	count += m2
	a_list[i + 1] -= m2
print(count)

Solution (Python):
n = int(input())
alst = [int(v) for v in input().split()]
blst = [int(v) for v in input().split()]

buster = 0
rage = 0
for a, b in zip(alst, blst):
    buster += min(a, rage)
    a -= min(a, rage)
    buster += min(a, b)
    if a < b:
        rage = b - a
    else:
        rage = 0
buster += min(alst[-1], rage)

print(buster)

Solution (Python):
def compute_max(n, xs, ys):
    rest_enemys = xs[:]
    rest_powers = ys[:]
    for i in range(n):
        
        removed_enemy = min(rest_enemys[i], rest_powers[i])
        rest_enemys[i] -= removed_enemy
        rest_powers[i] -= removed_enemy
        
        removed_enemy = min(rest_enemys[i + 1], rest_powers[i])
        rest_enemys[i + 1] -= removed_enemy
        rest_powers[i] -= removed_enemy
    total = sum(xs) - sum(rest_enemys)
    return total


def main():
    n = int(input())
    xs = [int(x) for x in input().split()]
    ys = [int(y) for y in input().split()]
    res = compute_max(n, xs, ys)
    print(res)


if __name__ == "__main__":
    main()

Solution (Python):

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

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


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


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


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


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


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


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


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


def mt(f):
    import time

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

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

    return wrap


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


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

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


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


if __name__ == '__main__':
    main()

Solution (Python):
def main():
  n = int(input())
  alist = list(map(int, input().split()))
  blist = list(map(int, input().split()))
  ans = 0
  for i, b in enumerate(blist):
    if alist[i] < b:
      b -= alist[i]
      ans += alist[i]
      alist[i] = 0
    else:
      ans += b
      b = 0
    if alist[i+1] < b:
      ans += alist[i+1]
      alist[i+1] = 0
    else:
      ans += b
      alist[i+1] -= b
  print(ans)


if __name__ == '__main__':
  main()

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

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

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7

debug = True


def dprint(*objects):
    if debug == True:
        print(*objects)

def solve():
    N = II()
    a_list = LI()
    b_list = LI()

    ans = 0
    left = a_list[0]

    for i in range(N):
        power = b_list[i]
        if power >= left:
            
            ans += left
            power = power - left
        else:
            
            ans += power
            power = 0

        right = a_list[i+1]
        if power >= right:
            
            ans += right
            left = 0 
        else:
            
            ans += power
            left = right - power 

    print(ans)
solve()

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

        A[i] = A[i] - B[i]
        ans += B[i]

    else:

        B[i] = B[i] - A[i]
        ans += A[i]

        if A[i+1] >= B[i]:

            A[i+1] = A[i+1] - B[i]
            ans += B[i]

        else:

            B[i] = B[i] - A[i+1]
            ans += A[i+1]
            A[i+1] = 0

print (ans)

Solution (Python):
def main():
    n = int(input())
    monsters = list(map(int,input().split()))
    heroes = list(map(int,input().split()))
    total = 0
    
    for i in range(n):
        if heroes[i] >= monsters[i]:
            total += monsters[i]
            heroes[i] -= monsters[i]
        else:
            total += heroes[i]
            heroes[i] = 0

        if heroes[i] >= monsters[i+1]:
            total += monsters[i+1]
            heroes[i] -= monsters[i+1]
            monsters[i+1] = 0
        else:
            total += heroes[i]
            monsters[i+1] -= heroes[i]
            heroes[i] = 0

    print(total)

main()

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

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

print(ans)

Solution (Python):

import sys


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

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

print(init - sum(A))

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

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


killed = 0
for i in range(n):
    mn = min(B[i], A[i])
    killed += mn
    A[i] -= mn
    B[i] -= mn
    mn = min(B[i], A[i+1])
    killed += mn
    A[i+1] -= mn
    B[i] -= mn

print(killed)

Solution (Python):
import sys, math
from collections import deque

readline = sys.stdin.readline

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

ans = 0

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

print(ans)

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

Solution (Python):
def attack(i):
    attack = 0
    attack_i = A_list[i]
    if(B_list[i] > A_list[i]):
        B_list[i] = B_list[i] - A_list[i]
        attack = A_list[i]
        A_list[i] = 0 
        attack += min(B_list[i], A_list[i+1])
        A_list[i+1] = max(A_list[i+1] - B_list[i], 0)
    else:
        A_list[i] = A_list[i] -  B_list[i]
        attack += B_list[i]
    return attack

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

ans = 0
for i in range(N):
    ans += attack(i)
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(100000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *

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]
        continue
    else:
        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]
print(ans)

Solution (Python):
import sys

from collections import Counter
def inpl(): return list(map(int, input().split()))

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

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

print(ans)

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

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

ans=0

k = min(A[0],B[0])
A[0] -= k
B[0] -= k
ans +=k
for i in range(1, n+1):
    kk = min(B[i-1],A[i])
    A[i] -= kk
    B[i-1] -= kk
    ans += kk
    if i!=n:
        mm=min(B[i],A[i])
        A[i] -= mm
        B[i] -= mm
        ans += mm
    
    
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
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]>=0:
        cnt+=a[i]
        b[i]-=a[i]
    else:
        cnt+=b[i]
        b[i]=0
    if b[i]>0:
        if b[i]-a[i+1]>=0:
            cnt+=a[i+1]
            a[i+1]=0
        else:
            cnt+=b[i]
            a[i+1]-=b[i]
print(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):
    if b[i] >= a[i]:
        ans += a[i]
        temp = b[i] - a[i]
        if temp >= a[i+1]:
            ans += a[i+1]
            a[i+1] = 0
        else:
            ans += temp
            a[i+1] -= temp
    else:
        ans += b[i]

print(ans)

Solution (Python):
def InputToInt():
    A = input().split()
    for i in range(len(A)):
        A[i] = int(A[i])
    return A

if __name__ == '__main__':
    N = int(input())
    A = InputToInt()
    B = InputToInt()

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

S = sum(A)

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

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

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

Solution (Python):
import bisect
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))
B = list(map(int, sys.stdin.readline().split()))

ans = 0
for i, b in enumerate(B):
    ans += min(A[i] + A[i + 1], b)
    b -= min(A[i], b)
    A[i + 1] -= min(A[i + 1], max(0, b))
print(ans)

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

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

Solution (Python):



import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline 

n = int(input())
a = [int(i) for i in readline().split()]
b = [int(i) for i in readline().split()] + [0]


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

    
print(ans)

Solution (Python):


import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
from heapq import heappush, heappop

def main():
    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])
        rest = b[i] - a[i]
        if rest > 0:
            ans += min(a[i+1], rest)
            a[i+1] = max(a[i+1] - rest, 0)

    print(ans)


if __name__ == '__main__':
    main()

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

ans = 0
for i in range(N):
    b = b_list[i]
    a1 = a_list[i]
    a2 = a_list[i+1]

    if b >= a1:
        tmp = a1
        ans += tmp
        b -= tmp
        if b > 0:
            if b >= a2:
                tmp2 = a2
                ans += tmp2
                a_list[i+1] -= tmp2
            else:
                ans += b
                a_list[i+1] -= b
    else:
        ans += b

print(ans)

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

killed = 0

for i in range(n):
    now_kill = min(a[i], b[i])
    killed += now_kill
    a[i] -= now_kill
    b[i] -= now_kill
    if b[i] == 0:continue
    now_kill = min(a[i+1], b[i])
    killed += now_kill
    a[i+1] -= now_kill
    b[i] -= now_kill

print(killed)

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

c = 0
for i in range(n):
    if b[i] >= a[i]:
        a[i+1] = max(0, a[i+1]-(b[i]-a[i]))
        a[i] = 0
    else:
        a[i] = 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()))
ans = 0
for i in range(N):
	b = B[i]
	a1,a2 = A[i], A[i+1]
	if b <= a1:
		ans += b
	else:
		ans += a1
		rest = b - a1
		if rest <= a2:
			ans += rest
			A[i+1] -= rest
		else:
			ans += a2
			A[i+1] = 0
print(ans)

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

cnt=0
for i in range(N):
    if A[i]>=B[i]:
        cnt+=B[i]
        A[i]-=B[i]
    else:
        cnt+=A[i]
        B[i]-=A[i]
        A[i]=0
        if A[i+1]>=B[i]:
            cnt+=B[i]
            A[i+1]-=B[i]
            B[i]=0
        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()))
ans=0
for i,B in enumerate(b):
    tmp=a[i]
    if tmp<=B:
        ans+=tmp
        B-=tmp
        if a[i+1]<=B:
            ans+=a[i+1]
            a[i+1]=0
        else:
            ans+=B
            a[i+1]-=B
    else:
        ans+=B
print(ans)

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

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

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

count=0

for i in range(N):
    count += min(A[i],B[i])
    nx = min(A[i+1],B[i]-min(B[i],A[i]))
    count += nx
    A[i+1]-=nx

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):
    num = min(a[i], b[i])
    a[i] -= num
    b[i] -= num
    cnt += num
    num2 = min(a[i+1], b[i])
    a[i+1] -= num2
    b[i] -= num2
    cnt += num2
    
print(cnt)

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-1, -1, -1):
    if(i < n-1):
        if(b_list[i+1] >= a_list[i+1]):
            counter += a_list[i+1]
            a_list[i+1] = 0
        else:
            counter += b_list[i+1]
            a_list[i+1] -= b_list[i+1]
    if(b_list[i] >= a_list[i+1]):
        counter += a_list[i+1]
        b_list[i] -= a_list[i+1]
    else:
        counter += b_list[i]
        b_list[i] = 0
    

counter += min(a_list[0], b_list[0])
print(counter)

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

Solution (Python):

n = int(input())

monster_list = [ int(v) for v in input().split() ]
aaaa_list = [ int(v) for v in input().split() ]

tottal_battle = 0
for i in range(n):
    battle = min(aaaa_list[i],monster_list[i])
    if aaaa_list[i] >= monster_list[i]:
        monster_list[i] = 0
        aaaa_list[i] -= battle
    else:
        aaaa_list[i] = 0
        monster_list[i] -= battle
    tottal_battle += battle
    battle = min(aaaa_list[i],monster_list[i+1])
    if aaaa_list[i] >= monster_list[i+1]:
        monster_list[i+1] = 0
        aaaa_list[i] -= battle
    else:
        aaaa_list[i] = 0
        monster_list[i+1] -= battle
    tottal_battle += battle

print(tottal_battle)

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

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

Solution (Python):

import sys
import math
from collections import defaultdict


sys.setrecursionlimit(10**7)
def input():
  return sys.stdin.readline()[:-1]


mod = 10**9 + 7


def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
  read_all = [tuple(map(int, input().split())) for _ in range(N)]
  return map(list,zip(*read_all))



N = I()
A = III()
B = III()

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

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

print(ans)

Solution (Python):
from heapq import heappush, heappop
from collections import deque
import itertools
from itertools import permutations
import sys
import bisect
sys.setrecursionlimit(10**6)
def MI():
    return map(int,input().split())
def I():
    return int(input())
def LI():
    return [int(i) for i in input().split()]
YN=['Yes','No']
YNeos='YNeos'
mo=10**9+7
imp='IMPOSSIBLE'


n=I()
a=LI()[::-1]
b=LI()[::-1]
ans=0
for i in range(n):
    d=min(a[i],b[i])
    ans+=d
    d2=min(a[i+1],b[i]-d)
    a[i+1]-=d2
    ans+=d2


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

    res += kill + kill2

print(res)

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

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

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

    cnt = min(A[i + 1], B[i])
    ans += cnt
    A[i + 1] -= cnt
    B[i] -= cnt
    
print(ans)

Solution (Python):
N = int(input())
lstA = [int(s) for s in input().split()]
lstB = [int(s) for s in input().split()]
count=0
for i in range(N):
  
  pow_i=min([lstA[i],lstB[i]])
  count=count+pow_i
  power_rest=lstB[i]-pow_i
  
  pow_isuc=min([lstA[i+1],power_rest])
  count=count+pow_isuc
  lstA[i+1]=lstA[i+1]-pow_isuc
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]:
        a[i] -= b[i]
    else:
        if b[i] - a[i] <= a[i+1]:
            a[i+1] -= b[i] - a[i]
        else:
            a[i+1] = 0
        a[i] = 0



print(s - sum(a))

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

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

    d = min(A[i+1], B[i])
    D += d
    A[i+1] -= d

print(D)

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

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

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

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

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

res = 0
for i in range(N):
    b = b_list[i]
    if b >= a_list[i]:
        b -= a_list[i]
        res += a_list[i]
        a_list[i] = 0
    else:
        res += b
        a_list[i] -= b
        b = 0

    if b >= a_list[i+1]:
        b -= a_list[i+1]
        res += a_list[i+1]
        a_list[i+1] = 0
    else:
        res += b
        a_list[i+1] -= b
        b = 0
print(res)

Solution (Python):


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