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

maximum=0

for i in range(n)[::-1]:
    for j in range(2)[::-1]:
        alpha=min(a[i+j],b[i])
        a[i+j]-=alpha
        b[i]-=alpha
        maximum+=alpha
print(maximum)

Solution (Python):

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

busterd = 0

for i in range(N):
    if A[i] > B[i]:
        busterd += B[i]
    else:
        busterd += A[i]
        addition = B[i] - A[i]

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

        

        busterd += addition
        A[i+1] -= addition








































print(busterd)

Solution (Python):
n = int(input())
m = list(map(int,input().split()))
h = list(map(int,input().split()))
l = len(m)
s = 0
q=0
if(h[0] > m[0]):
    h[0] = h[0] - m[0]
    s = s+ m[0]
else:
    s = s + h[0]
    h[0]= 0
for i in range(1, n):
    if(i>l-1):
        break
    if(h[i-1]!=0):
        if(h[i-1] < m[i]):
            s = s+ h[i-1]
            m[i] = m[i] - h[i-1]
        else:
            s = s+ m[i]
            m[i] = 0
    if(h[i] > m[i]):
        h[i] = h[i] - m[i]
        s = s+ m[i]
        m[i] = 0
    else:
        s = s + h[i]
        m[i] = m[i] - h[i]
        h[i]= 0
if(l<=n):
    print(s)
else:
    if(m[n] > h[n-1]):
        print(s+ h[n-1])
    else:
        print(s+m[n])

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

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

Solution (Python):



import sys

input = sys.stdin.readline

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

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

    print(n)

main()

Solution (Python):
brave = int(input())
a_monster = list(map(int,input().split()))
b_monster = list(map(int,input().split()))
rem_power = 0
count = 0

for i in range(0, brave):
    rem_monster = a_monster[i] - rem_power
    if rem_monster <= 0:
        count += a_monster[i]
        rem_power = b_monster[i]
        continue
    rem_power += b_monster[i]
    if rem_power > a_monster[i]:
        count += a_monster[i]
        rem_power -= a_monster[i]
    else:
        count += rem_power
        rem_power = 0

last = a_monster[-1] - rem_power

if last > 0:
    print(count + rem_power)
else:
    print(count + a_monster[-1])

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

print(tot)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ans=0
p=0
l=0
k=0
A=a[n]
for i in range(n):
  B=a[n-i-1]
  C=b[n-i-1]
  if A>=C:
    ans+=C
    A=B
  elif A<C:
    ans+=A
    C=C-A
    if B>=C:
      ans+=C
      A=B-C
    else:
      ans+=B
      A=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(len(A)-1):
    if A[i]>=B[i]:
        ans += B[i]
        A[i]-=B[i]
        B[i]=0
    if B[i]>A[i]:
        ans+= A[i]
        B[i]-=A[i]
        A[i]=0
    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 bi in range(n-1, -1, -1):
    for ai in [bi+1, bi]:
        temp = a[ai] if a[ai] <= b[bi] else b[bi]
        ans += temp
        b[bi] -= temp
        a[ai] -= temp

print(ans)

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

print(ans-sum(a))

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

for x in range(n):
    if a[x] <= b[x]:
        b[x] -= a[x]
        c += a[x]
        a[x] = 0
    else:
        c += b[x]
        b[x] = 0

    if b[x] != 0:
        if a[x + 1] >= b[x]:
            a[x + 1] -= b[x]
            c += b[x]
        else:
            c += a[x + 1]
            a[x + 1] = 0

print(c)

Solution (Python):


import sys

sys.setrecursionlimit(10**7)


input = sys.stdin.readline


def read_h(typ=int):
    return list(map(typ, input().split()))


def read_v(n, m=1, typ=int):
    return [read_h(typ) if m > 1 else typ(input()) for _ in range(n)]


def main():
    n, = read_h()
    A = read_h()
    B = read_h()

    ans = 0
    rem_cap = 0
    for a, b in zip(A, B):
        cap = rem_cap + b
        if a >= cap:
            ans += cap
            rem_cap = 0
        elif rem_cap >= a:
            ans += a
            rem_cap = b
        else:
            ans += a
            rem_cap = cap - a

        
        

    if rem_cap >= A[-1]:
        ans += A[-1]
    else:
        ans += rem_cap

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):


def solve():
    N = int(input())
    A = [int(i) for i in input().split()]
    B = [int(i) for i in input().split()]
    count = 0
    for i in range(N):
        count += min(A[i], B[i])
        if A[i] < B[i]:
            b = B[i] - A[i]
            count += min(A[i + 1], b)
            A[i + 1] = max(0, A[i + 1] - b)
    print(count)


if __name__ == '__main__':
    solve()

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

count = 0
zan_A_i = A[N]

for i in range(N):
    zan_B_i = B[N-1-i] - zan_A_i

    if zan_B_i < 0:
        zan_B_i = 0
    zan_A_i = A[N-1-i] - zan_B_i
    if zan_A_i < 0:
        count += zan_A_i 
        zan_A_i = 0

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

print(count)

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

Solution (Python):


if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    ctr = 0

    for i in range(len(B)):
        if B[i] <= A[i]:
            ctr += B[i]
        else:
            ctr += A[i]
            rest = B[i] - A[i]

            ctr += min(A[i+1], rest)

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

    print(ctr)

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

print(ans)

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

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

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

sum_r = 0
pre_h_beat = 0
for i, b in enumerate(B):
    if i == 0:
        left_beat = min(b, A[i])
    else:
        left_beat = min(remain_mon, b)

    sum_r += left_beat

    cap_remain_beat = b - left_beat

    right_beat = min(cap_remain_beat, A[i + 1])

    sum_r += right_beat

    remain_mon = A[i + 1] - right_beat

print(sum_r)

Solution (Python):
n = int(input())
a_lst = list(map(int,input().split()))
b_lst = list(map(int,input().split()))

taoseru_nokori = 0
taoshita = 0
for i in range(n):
	monster_maenoga_taoshita = min(a_lst[i],taoseru_nokori)
	monster_nokori = a_lst[i] - monster_maenoga_taoshita
	monster_taoshita = min(b_lst[i],monster_nokori)
	taoseru_nokori = b_lst[i] - monster_taoshita
	
	taoshita += monster_maenoga_taoshita + monster_taoshita

taoshita += min(a_lst[n],taoseru_nokori)
print(taoshita)

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

B.append(0)
rest = 0
score = 0
for a,b in zip(A,B):
  x = min(a,rest)
  score += x
  a -= x
  if b < a:
    score += b
    rest = 0
    continue
  score += a
  rest = b - a
print(score)

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

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

n = int(input())
a = LI()
b = LI() + [0]

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

    diff2 = min(b[i], a[i+1])
    a[i+1] -= diff2
    count += diff2

print(count)

Solution (Python):
def INT_(n): return int(n)-1
def MI(): return map(int, input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_, input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n: int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split())) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')


def main():
    N = I()
    A = LI()
    B = LI()

    total = 0

    res = 0
    for i in range(N):
        monster = max(0, A[i] - res)
        if res + B[i] <= A[i]:
            total += B[i] + res
            res = 0
        else:
            total += A[i]
            res = B[i] - monster
    total += min(res, A[N])
    print(total)


if __name__ == '__main__':
    main()

Solution (Python):

def input_from_console():
    n = int(input())
    a_list = list(map(int, input().split()))
    b_list = list(map(int, input().split()))
    return n, a_list, b_list


def solve(n, a_list, b_list):
    sum_of_a = sum(a_list)
    for i in range(n):
        if a_list[i] - b_list[i] <= 0:
            b_list[i] -= a_list[i]
            a_list[i] = 0
            a_list[i + 1] -= min(b_list[i], a_list[i + 1])
        else:
            a_list[i] -= b_list[i]
    return sum_of_a - sum(a_list)


def main():
    n, a_list, b_list = input_from_console()
    print(solve(n, a_list, b_list))


if __name__ == "__main__":
    import sys

    if sys.gettrace():  
        check_cases()
    else:
        main()

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

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


cnt = 0
for i in range(n):
  for j in range(i,i+1):
    x = b[j] - a[i]
    if x >= 0:
      b[j] = x
      cnt += a[i]
      y = b[j] - a[i+1]
      if y > 0:
        cnt += a[i+1]
        a[i+1] = 0
      elif y <= 0:
        cnt += b[j]
        a[i+1] = a[i+1] - b[j]
    elif x < 0:
      cnt += b[j]
      continue
print(cnt)

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

Solution (Python):
nh  = int(input())
m  = [int(i) for i in input().split()]
h  = [int(i) for i in input().split()]

h1 = sum(h)

for i in range(len(m)):
    if h[i-1] != 0 and i != 0:
        val = abs(m[i] - h[i-1])
        if m[i] > h[i-1]:
            m[i]  = val
            h[i-1]  = 0
        else:
            m[i]  = 0
            h[i-1]  = val        

    if m[i] != 0 and i < nh:
        val = abs(m[i] - h[i])
        if m[i] > h[i]:
            m[i]  = val
            h[i]  = 0
        else:
            m[i]  = 0
            h[i]  = val


print(h1 - sum(h))

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

for i in range(len(a)-2,-1,-1):
    b[i] = max(b[i]-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
    
print(c-sum(b))

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

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

print(cnt)

Solution (Python):
import sys
sys.setrecursionlimit(4100000)

import logging

logging.basicConfig(level=logging.DEBUG)

logger = logging.getLogger(__name__)


def resolve():
    N = [int(x) for x in sys.stdin.readline().split()][0]
    a_list = [int(x) for x in sys.stdin.readline().split()]
    b_list = [int(x) for x in sys.stdin.readline().split()]

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

            if a_list[i] >= b:
                a_list[i] -= b
                count += b
            else:
                count += a_list[i]
                a_list[i] = 0


    print(count)
            





if __name__ == "__main__":
    resolve()





import sys
from io import StringIO
import unittest

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """2
3 5 2
4 5"""
        output = """9"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """3
5 6 3 8
5 100 8"""
        output = """22"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """2
100 1 1
1 100"""
        output = """3"""
        self.assertIO(input, output)

    def test_入力例_4(self):
        input = """1
1 1
1"""
        output = """1"""
        self.assertIO(input, output)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=[0]
sum=0

for i in range(n):
  if b[i]+c[i]-a[i]>=0:
    if c[i]-a[i]>=0:
      c.append(b[i])
      
    else:
      c.append(b[i]+c[i]-a[i])
    sum+=a[i]
  else:
    c.append(0)
    sum+=b[i]+c[i]

if c[n]>a[n]:
  print(sum+a[n])
else:
  print(sum+c[n])

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

Solution (Python):
saver_num = int(input())
mons_num = list(map(int, input().split()))
power_num = list(map(int, input().split()))
count = 0

for i in range(saver_num):
    if power_num[i] <= mons_num[i]:
        count += power_num[i]
    else:
        count += mons_num[i]
        power = power_num[i] - mons_num[i]
        if power <= mons_num[i+1]:
            count += power
            mons_num[i+1] = mons_num[i+1] - power
        else:
            count += mons_num[i+1]
            mons_num[i+1] = 0

print(count)

Solution (Python):
N = int(input())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))

total = 0
for i in range(N):
    yusha_life = Bs[N-1-i]
    yusha_life -= As[N-i]
    if yusha_life <= 0:
        total += Bs[N-1-i]
        As[N-i] -= Bs[N-1-i]
    else:
        total += As[N-i]
        total += min(yusha_life, As[N-i-1])
        As[N-i-1] = 0 if As[N-i-1] <= yusha_life else As[N-i-1] - yusha_life

print(total)

Solution (Python):
import sys

input = sys.stdin.readline


def main():
    n = int(input())
    a_list = [int(i) for i in input().split()]
    b_list = [int(i) for i in input().split()]

    cnt = 0
    for i, b in enumerate(b_list):
        a = a_list[i]
        if a <= b:
            combo = a
            b = b - a
            a = a_list[i + 1]
            if a <= b:
                combo += a
                a = 0
            else:
                combo += b
                a -= b
            a_list[i + 1] = a
        else:
            combo = b

        cnt += combo
    print(cnt)


if __name__ == "__main__":
    main()

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

count = 0

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

print(count)

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

count=0
for i in range(N):
    c=B[i]-A[i]
    if c>=0:
        count+=A[i]

    if c>0 and A[i+1]>c:
        A[i+1]=A[i+1]-c
        count+=c


    elif c>0 and A[i+1]<=c:
        count+=A[i+1]
        A[i+1]=0


    if c<0:
        count+=B[i]

    
    
    
    
    
    








print(count)

Solution (Python):
n=int(input())
al=[int(i) for i in input().split()]
bl=[int(i) for i in input().split()]
acc=0
carry=0
for i in range(n):
    ai=al[i]; bi=bl[i]
    if ai <= carry:
        acc+=ai
        carry=bi
    elif ai-carry <= bi:
        acc+=ai
        carry=bi-(ai-carry)
    else:
        acc+=bi+carry
        carry=0
if not carry==0:
    if al[n] <= carry:
        acc+= al[n]
    else:
        acc+=carry
print(acc)

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


main()

Solution (Python):


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
b.insert(0, 0)
c = 0
z = 0
for i in range(n, -1, -1):
    if z >= a[i]:
        c += a[i]
        z = b[i]
    else:
        t = b[i]-(a[i]-z)
        if t > 0:
            c += a[i]
            z = t
        elif t <= 0:
            c += b[i]+z
            z = 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 += B[i]
  elif A[i+1]+A[i] <= B[i]:
      cnt += A[i+1]+A[i]
      A[i+1] = 0
  elif A[i]<B[i] and A[i+1]+A[i] >= B[i]:
      cnt +=B[i]
      A[i+1] = A[i+1]-(B[i]-A[i])


print(cnt)

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

print(count)

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

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

A_after_sum = sum(A)
print(A_sum- A_after_sum)

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


S = input()
S = S.split()
A = list(map(int, S))
A_copy = list(map(int, S))

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


remain = 0

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



print(sum(A_copy)-sum(A))

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

print(result)

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

ans = 0
for i in range(N):

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

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

print(ans)

Solution (Python):
N  = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
count = 0
monstarsum = sum(A)
for i in range(N):
  tmpwon = min(A[i],B[i])
  A[i] -= tmpwon
  A[i+1] -= min([A[i+1],B[i] - tmpwon])

ans = monstarsum - sum(A)
print(ans)

Solution (Python):
n = int(input())
mons = [int(i) for i in input().split()]
yusya = [int(i) for i in input().split()]
sum = 0
for i in range(n + 1):
    if i >= 1:
        if yusya[i - 1] < mons[i]:
            mons[i] -= yusya[i - 1]
            sum += yusya[i - 1]
            yusya[i - 1] = 0
        else:
            yusya[i - 1] -= mons[i]
            sum += mons[i]
            mons[i] = 0
    if i < n:
        if yusya[i] < mons[i]:
            mons[i] -= yusya[i]
            sum += yusya[i]
            yusya[i] = 0
        else:
            yusya[i] -= mons[i]
            sum += mons[i]
            mons[i] = 0

print(sum)

Solution (Python):


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

ans = 0 

ta = A[-1]

for a, b in list(zip(A[:-1], B))[::-1]:

    tb = b
    if ta >= b:
        ans += b
        ta = a
        continue
    else:
        ans += ta
        tb -= ta
        ta = 0
        

    if a >= tb:
        ans += tb
        ta = a - tb
    else:
        ans += a
        ta = 0

print(ans)

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

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

damagecost = 0
killmonster = 0
for i in range(n):
    if lb[i] <= la[i]:
        killmonster += lb[i]
    else:
        killmonster += la[i]
        damagecost = lb[i] - la[i]
        if damagecost >= la[i + 1]:
            killmonster += la[i + 1]
            la[i + 1] = 0
        else:
            killmonster += damagecost
            la[i + 1] = la[i + 1] - damagecost
print(killmonster)

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] != 0 and B[i] != 0:
        k = min(A[i],B[i])
        A[i] -= k
        B[i] -= k
        count += k
    if A[i+1] != 0 and B[i] != 0:
        l = min(A[i+1],B[i])
        A[i+1] -= l
        B[i] -= l
        count += l

print(count)

Solution (Python):
n = int(input())
list_a = list(map(int, input().split()))
list_b = list(map(int, input().split()))

count = 0
prev = list_a[0]

for i in range(n):
    if list_b[i] <= prev:
        count += list_b[i]
        prev = list_a[i + 1]
    else:
        count += prev
        if list_b[i] - prev >= list_a[i + 1]:
            count += list_a[i + 1]
            prev = 0
        else:
            count += list_b[i] - prev
            prev = list_a[i + 1] - list_b[i] + prev

print(count)

Solution (Python):
n = int(input())
a1 = list(map(int,input().split()))
b1 = list(map(int,input().split()))
a = []
b = []
for i in (reversed(a1)):
    a.append(i)
for i in (reversed(b1)):
    b.append(i)

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

Solution (Python):
n = int(raw_input())
a = [int(i) for i in raw_input().split(' ')]
b = [int(i) for i in raw_input().split(' ')]
ret = 0
for i in range(n):
    e = min(a[i], b[i])
    ret += e
    a[i] -= e
    b[i] -= e
    e = min(a[i + 1], b[i])
    ret += e
    a[i + 1] -= e
    b[i] -= e

print ret

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

a_rev = list(reversed(a))
b_rev = list(reversed(b))


kill_m_1 = 0

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

for i in range(n):
    if a_rev[i] <= b_rev[i]:
        kill_m_2 += a_rev[i]
        b_rev[i] = b_rev[i] - a_rev[i]
        a_rev[i] = 0
    
    else:
        a_rev[i] = a_rev[i] - b_rev[i]
        kill_m_2 += b_rev[i]
        b_rev[i] = 0
    
    if b_rev[i] != 0:
        
        if a_rev[i+1] <= b_rev[i]:
            kill_m_2 += a_rev[i+1]
            b_rev[i] = b_rev[i] - a_rev[i+1]
            a_rev[i+1] = 0
    
        else:
            a_rev[i+1] = a_rev[i+1] - b_rev[i]
            kill_m_2 += b_rev[i]
            b_rev[i] = 0
    
print(max(kill_m_1,kill_m_2))

Solution (Python):
N  = int(input())
mon_arr = list(map(int, input().split()))
hero_arr = list(map(int, input().split()))

nums = 0

for i in range(N):
  if (mon_arr[i] + mon_arr[i+1]) > hero_arr[i]:
    nums += hero_arr[i]
    if mon_arr[i+1] > (mon_arr[i] + mon_arr[i+1] - hero_arr[i]):
      mon_arr[i+1] = (mon_arr[i] + mon_arr[i+1] - hero_arr[i])
  else:
    nums += (mon_arr[i] + mon_arr[i+1])
    mon_arr[i+1] = 0
    
print(nums)

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

Solution (Python):
N = int(input())
Mons = list(map(int, input().split()))
Hero = list(map(int, input().split()))

ans = 0

for i in range(N):
    
    
    if Mons[i] >= Hero[i]:
        hunting_count = Hero[i]
        Mons[i] -= hunting_count
        ans += hunting_count
    else:
        hunting_count_part1 = Mons[i]
        Mons[i] -= hunting_count_part1
        Hero[i] -= hunting_count_part1
        ans += hunting_count_part1

        if Mons[i+1] >= Hero[i]:
            hunting_count_part2 = Hero[i]
            Mons[i+1] -= hunting_count_part2
            ans += hunting_count_part2
        else:
            hunting_count_part2 = Mons[i+1]
            Mons[i+1] -= hunting_count_part2
            Hero[i] -= hunting_count_part2 
            ans += hunting_count_part2

print(ans)