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



Sample input:
2
3 5 2
4 5


Sample output:
9

Submission status: Accepted

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

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

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

    print(ans)

    return


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

if a[-1]>=overpower:
    print(ans+overpower)
else:
    print(ans+a[-1])

Submission status: Accepted

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

ans = 0

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

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
RA = [A[0]]

killed = []
for i in range(N):
    killed.append(min(RA[i], B[i]))
    RB = B[i] - killed[-1]
    killed[-1] += min(A[i+1], RB)
    RA.append(max(0, A[i+1]-RB))

print(sum(killed))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
sum_hunt = 0
old_hunt=0

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

print(sum_hunt)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
enemies = list(map(int, input().split()))
heros = list(map(int, input().split()))
total = 0
remain = enemies[0]
for i in range(len(heros)):
    if heros[i] < remain:
        remain = enemies[i+1]
        total += heros[i]
    else:
        e = remain + enemies[i+1]
        total += min(heros[i], e)
        remain = e - heros[i] if e > heros[i] else 0
print(total)

Submission status: Accepted

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

sum = 0
rest_hero = 0

for i in range(n+1):
    hero = b[i] + rest_hero
    if a[i] >= hero:
        sum += hero
        rest_hero = 0
    else:  # a[i] < hero
        sum += a[i]
        if a[i] > rest_hero:
            rest_hero = b[i] - (a[i] - rest_hero)
        else:
            rest_hero = b[i]

print(sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

cnt = 0
for i in range(N+1):
  if i == N:
    print(cnt, end='')
  elif A[i] <= B[i]:
    cnt += A[i]
    tmp = B[i]-A[i]
    if A[i+1] >= tmp:
      A[i+1] = A[i+1]-tmp
      cnt += tmp
    else:
      cnt += A[i+1]
      A[i+1] = 0
  elif A[i] > B[i]:
    cnt += B[i]

Submission status: Accepted

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

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

    else:
        c += (a[i]+a[i+1])
        a[i+1] = 0
print(c)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from collections import defaultdict, deque
import sys
import heapq
import bisect
import math
import itertools
import string
import queue
import copy
import time
import numpy as np
sys.setrecursionlimit(10**8)
input = sys.stdin.readline
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(input())
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())

n=inp()
a = inpl()
b = inpl()
ans=0
for i in range(n):
    tmp=a[i] - b[i]
    if tmp >= 0:
        ans += b[i]
    else:
        ans += a[i]
        tmp=-tmp
        temp = a[i + 1] - tmp
        if temp >= 0:
            ans += tmp
            a[i + 1] -= tmp
        else:
            ans += a[i + 1]
            a[i + 1] = 0
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

monsters = list(map(int, input().split()))
powers = list(map(int, input().split()))

# dp[i] = i番目の街のモンスターを最も多く倒せる数
dp = [0 for _ in range(N + 1)]

# 初期条件
# 勇者パワーの方が少ない場合その数しか倒せない
# モンスター数の方が少ない場合もその数しか倒せない
# print(f"powers: {powers}")
dp[N] = min(monsters[N], powers[N - 1])
# 勇者の火力を減らしておく
powers[N - 1] -= dp[N]
# Nから-1ずつ下がって0まで処理する
for i in range(N - 1, 0, -1):
    # print(f"powers: {powers}")
    # i番目の倒せる最大数を求める
    # i番目の勇者がi+1番目のモンスターを倒して残った火力 + i-1番目の勇者の火力と
    # i番目のモンスター数の内、少ない方が倒せる最大数になる
    dp[i] = min(powers[i] + powers[i - 1], monsters[i])
    # i-1番目の勇者の火力を減らしておく
    # ただしi番目の勇者で十分倒せる火力の場合何も減らさない
    powers[i - 1] -= max(dp[i] - powers[i], 0)

# print(f"powers: {powers}")
# 0番目の勇者の倒せる最大数を求める
dp[0] = min(powers[0], monsters[0])

print(sum(dp))

Submission status: Accepted

Solution code:
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])
    ans += t
    b[i] -= t
    t2 = min(b[i], a[i+1])
    a[i+1] -= t2
    ans += t2
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
arr = list(reversed(list(map(int,input().split()))))
brr = list(reversed(list(map(int,input().split()))))
brr.append(0)
cur=0
ans=0
for i in range(n+1):
    ans+=min(arr[i],cur)
    a=max(0,arr[i]-cur)
    ans+=min(a,brr[i])
    cur=max(brr[i]-a,0)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
monster = list(map(int, input().split()))
yuusya = list(map(int, input().split()))
sub = 0
for i in range(n,0,-1):
  #print("yuusya[i-1]",yuusya[i-1],"monster[i]",monster[i])
  if yuusya[i-1] > monster[i]:
    sub += monster[i]
    yuusya[i-1] -= monster[i]
    monster[i] = 0
  else:
    sub += yuusya[i-1]
    monster[i] -= yuusya[i-1]
    yuusya[i-1] = 0
  if yuusya[i-1] > monster[i-1]:
    sub += monster[i-1]
    yuusya[i-1] -= monster[i-1]
    monster[i-1] = 0
  else:
    sub += yuusya[i-1]
    monster[i-1] -= yuusya[i-1]
    yuusya[i-1] = 0
if yuusya[0] > monster[0]:
  sub+=monster[0]
else:
  sub+=yuusya[0]
print(sub)

Submission status: Accepted

Solution code:
n = int(input())
a_array = [int(x) for x in input().split()]
b_array = [int(x) for x in input().split()]

res = 0
amari = 0
amari0 = 0
amari1 = 0
for i in range(n):
    if i % 2 == 0:
        monster = max(0, a_array[i]-amari1)
        res += min(amari1, a_array[i])
        res += monster if b_array[i] >= monster else b_array[i]
        # amari = max(0, (b_array[i]+amari)-a_array[i])
        amari0 = 0 if b_array[i] <= monster else b_array[i]-monster
    else:
        monster = max(0, a_array[i]-amari0)
        res += min(amari0, a_array[i])
        res += monster if b_array[i] >= monster else b_array[i]
        # amari = max(0, (b_array[i]+amari)-a_array[i])
        amari1 = 0 if b_array[i] <= monster else b_array[i]-monster

if n % 2 == 0:
    monster = max(0, a_array[-1]-amari1)
    res += min(amari1, a_array[-1])
else:
    monster = max(0, a_array[-1]-amari0)
    res += min(amari0, a_array[-1])
print(res)

Submission status: Accepted

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

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

if suc >= a[n]:
    k += a[n]
else:
    k += suc
    
print(k)

Submission status: Accepted

Solution code:
N = int(input())
city = list(map(int, input().split()))
hero = list(map(int, input().split()))
ko = 0 

for i in range(0 ,N):
  if city [i] >= hero[i]:
    ko += hero[i]
  elif city [i] <= hero[i]:
    ko += city[i]
    extra = hero[i] - city[i]
    
    if extra >= city[i+1]:
      ko += city[i+1]
      city[i+1] = 0
    elif extra <= city[i+1]:
      ko += extra
      city[i+1] = city[i+1] - extra
print(ko)

Submission status: Accepted

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

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

print(count)

Submission status: Accepted

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

Submission status: Accepted

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

total_mons = 0
debt = 0

if B[N-1] < A[N]:
    total_mons += B[N-1]
    B[N - 1] = 0
else:
    total_mons += A[N]
    B[N - 1] = B[N - 1] - A[N]


for i in reversed(range(1, N)):

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


if B[0] < A[0]:
    total_mons += B[0]
else:
    total_mons += A[0]

print(total_mons)

Submission status: Accepted

Solution code:
while True:
    try:
        sum=0
        n=eval(input())
        l1=list(map(int,input().split()))
        l2=list(map(int,input().split()))
        for i in range(n):
            ss=1
            if(l2[i]-l1[i]>0):
                s=l2[i]-l1[i]
                if l1[i+1]-s<0:
                    sum+=l1[i+1]
                    l1[i+1]=0
                else:
                    l1[i+1]-=s
                    sum+=s
                    sum+=l1[i]
                    ss=0
                if(ss!=0):sum+=l1[i]

            elif(l2[i]-l1[i]<=0):
                sum+=l2[i]
        print(sum)
    except:break

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
Mon = 0
for i in range(N):
    a1 = A[i]
    a2 = A[i+1]
    b = B[i]
    mst = a1 + a2
    if b >= mst:
        Mon += mst
        A[i+1] = 0
    else:
        Mon += b
        if a1 <= b:
            A[i+1] = A[i+1] - (b-a1)
print(Mon)

Submission status: Accepted

Solution code:
N = int(input())
mnstr = list(map(int, input().split()))
yusha = list(map(int, input().split()))
sum = 0
killed = 0

for i in range(N):
  killedtemp1 = min(mnstr[i], yusha[i])#iの勇者が倒したモンスターiの数
  yusha[i] = max(0, yusha[i]-mnstr[i])
  killedtemp2 = min(mnstr[i+1], yusha[i])#iの勇者が倒したモンスターi+1の数
  mnstr[i+1] = mnstr[i+1] - killedtemp2
  killed += killedtemp1 + killedtemp2
  
print(killed)

Submission status: Accepted

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

pre = sum(a)

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

    elif b[i] < a[i]:
        a[i] -= b[i]
        b[i]

    elif b[i] == a[i]:
        b[i] = a[i] = 0

print(pre-sum(a))

Submission status: Accepted

Solution code:
import sys
p = [[int(c) for c in r.split()] for r in sys.stdin.read().split('\n') if r != '']
n = p[0][0]
a = p[1]
b = p[2]
c = 0
k = 0
for i in range(n):
    v = min(a[i], k)
    w = min(b[i], a[i] - v)
    c += v + w
    k = b[i] - w
v = min(a[-1], k)
print(c+v)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))

counter = 0
for i in range(n):
  power = B[-i-1]
  counter += min(power,A[-i-1])
  power -= min(power,A[-i-1])
  counter += min(power,A[-i-2])
  A[-i-2] = max(0, A[-i-2]-power)
print(counter)

Submission status: Accepted

Solution code:
n=input()
n=int(n)
c=0
a=list(map(int,input().split()))
b=list(map(int,input().split()))
m=0
for i in range(n):
  kill=min(a[i],b[i]+m)
  c+=kill
  m=min(b[i]+m-kill,b[i])
c+=min(m,a[n])
print(c)

Submission status: Accepted

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

Submission status: Accepted

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

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

    M += A[i]
    B[i] -= A[i]
    A[i] = 0

    if A[i+1] >= B[i]:
        M += B[i]
        A[i+1] -= B[i]
        B[i] = 0
        continue

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

print M

Submission status: Accepted

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

count=0

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

print(count)

Submission status: Accepted

Solution code:
N=int(input())
a=input().split()
b=input().split()
for i in range(0,N):
  a[i]=int(a[i])
  b[i]=int(b[i])
a[N]=int(a[N])
answer=sum(a)
for i in range(0,N):
    c=b[i]-a[i]
    if c<0:
        a[i]=-c
        b[i]=0
    else:
        a[i]=0
        b[i]=c
    d=b[i]-a[i+1]
    if d<0:
        a[i+1]=-d
        b[i]=0
    else:
        a[i+1]=0
        b[i]=d
print (answer-sum(a))

Submission status: Accepted

Solution code:
import copy
N = int(input())
liA = list(map(int,input().split()))
liB = list(map(int,input().split()))
liC = copy.deepcopy(liA)
for i in range(len(liB)):
    if liA[i] >= liB[i]:
        liA[i] = liA[i]-liB[i]
    elif liB[i] - (liA[i]+liA[i+1]) <= 0:
        liA[i+1] = liC[i+1] + liA[i] - liB[i]
        liA[i] = 0
    else:
        liA[i] = 0
        liA[i+1] = 0
print(sum(liC)-sum(liA))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:

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]

        # print(addition)

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


# for i in range(N):
#     vital = B[i]
#     first_monsters = A[i]
#     second_monsters = A[i+1]
#
#     dead = False
#
#     for d in range(first_monsters):
#         if first_monsters == 0:
#             break
#
#         first_monsters -= 1
#         vital -=1
#         busterd += 1
#
#         if vital == 0:
#             dead = True
#             break
#         if first_monsters == 0:
#             break
#
#     if dead:
#         break
#     else:
#         for z in range(second_monsters):
#             second_monsters -= 1
#             vital -= 1
#             busterd += 1
#
#             if vital == 0:
#                 A[i+1] = second_monsters
#                 dead = True
#                 break
#             if second_monsters == 0:
#                 A[i+1] = 0
#                 break


print(busterd)

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

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

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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]
        # print(ans)
    else:
        ans+=A[i+1]
        A[i+1]=0

print(ans)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

import sys

sys.setrecursionlimit(10**7)

# Override `input` function because `stdin.readline()` is 10x faster than built-in `input()`
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

        #  print('killed:', ans)
        #  print('remaining:', rem_cap)

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:


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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# coding=utf-8

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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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()