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

Submission status: Accepted

Solution code:
#-*-coding:utf-8-*-
import sys
input=sys.stdin.readline
import numpy as np

def main():
    n = int(input())
    mob=np.array(list(map(int,input().split())),dtype=int)
    bra=np.array(list(map(int,input().split())),dtype=int)
    ans=0

    for i in range(n):
        tmp1=min(mob[i],bra[i])
        ans += tmp1
        tmp2=min(mob[i+1],bra[i]-tmp1)
        ans += tmp2
        mob[i+1]-=tmp2
    print(ans)


if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
#-*-coding:utf-8-*-
import sys
input=sys.stdin.readline
import numpy as np

def main():
    n = int(input())
    mob=np.array(list(map(int,input().split())),dtype=int)
    bra=np.array(list(map(int,input().split())),dtype=int)
    ans=0

    for i in range(n):
        ans += min(mob[i],bra[i])
        ans += min(mob[i+1],bra[i]-min(mob[i],bra[i]))
        mob[i+1]-=min(mob[i+1],bra[i]-min(mob[i],bra[i]))
    print(ans)


if __name__=="__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

assert len(A) == N + 1
assert len(B) == N

count = 0
for i in range(N-1, -1, -1):
  cost2 = min(A[i + 1], B[i])
  A[i+1] -= cost2
  cost1 = min(A[i], B[i] - cost2)
  A[i] -= cost1
  count += cost1 + cost2
print(count)

Submission status: Accepted

Solution code:
n = int(input())
la = list(map(int,input().split()))
lb = list(map(int,input().split()))
count = 0
for i in range(n):
  count += min(la[i],lb[i])
  if la[i]<lb[i]:
    if la[i+1] >= lb[i]-la[i]:
      count+=(lb[i]-la[i])
      la[i+1]-=(lb[i]-la[i])
    else:
      count+=la[i+1]
      la[i+1]=0
print(count)

Submission status: Accepted

Solution code:
N = int(input())
list_a = list(map(int,input().split()))
list_b = list(map(int,input().split()))
c = sum(list_a)

for i in range(1,len(list_a)):
    temp = list_a[i] - min((list_a[i-1] - list_b[i-1]),0)*(-1)
    if list_a[i-1] - list_b[i-1] <= 0:
        list_a[i-1] = 0
    else:
        list_a[i-1] -= list_b[i-1]
    if temp != list_a[i] and temp >= 0:
        list_a[i] = temp
    elif temp <= 0:
        list_a[i] = 0

del_c = sum(list_a)

ans = c - del_c
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(N):
    ans += min(A[i] + A[i+1], B[i])
    A[i+1] -= min(max(B[i] - A[i], 0), A[i+1])

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(N):
	if A[i]>=B[i]:
		ans+=B[i]
		A[i]-=B[i]
	else:
		temp=B[i]-A[i]
		ans+=A[i]
		if A[i+1]>=temp:
			ans+=temp
			A[i+1]-=temp
		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 i in range(n):
    if a[i] >= b[i]:
        ans += b[i]
    else:
        val = b[i] - a[i]
        if a[i+1] - val <= 0:
            ans += a[i] + a[i+1]
            a[i+1] = 0
        else:
            ans += b[i]
            a[i+1] = a[i+1] - val

print(ans)

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

if __name__ == "__main__":
    main()

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8


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

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

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

print(res)

Submission status: Accepted

Solution code:
# ABC 135 C
N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
num = sum(a)
for i in range(N):
  if a[i]<=b[i]:
    b[i] = b[i]-a[i]
    a[i]=0
    if a[i+1]<=b[i]:
      a[i+1]=0
    elif a[i+1]>b[i]:
      a[i+1] = a[i+1]-b[i]
  elif a[i]>b[i]:
    a[i] = a[i]-b[i]
#print(num,sum(a))
num = num-sum(a)
print(num)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def resolve():
    n = int(input())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    f = [0] * (n+1)
    f[0] += min(a[0],b[0])
    f[1] += min(a[1],b[0]-f[0])
    a[1] -= f[1]
    for i in range(1,n):
        f[i] += min(a[i],b[i])
        f[i+1] += min(a[i+1],b[i]-min(a[i],b[i]))
        a[i+1] -= f[i+1]
    print(sum(f))
resolve()

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

cnt = 0
for i in range(0,N):
    #隣の町で余った勇者を現在の町へ増援
    #更に隣の町へは増援できないことに注意
    if B[i] <= A[i]:
        cnt += B[i]
    elif B[i] > A[i] and B[i] <= A[i]+A[i+1]:
        cnt += B[i]
        A[i+1] = A[i+1] -B[i] + A[i]
    else:
        cnt += A[i]+A[i+1]
        A[i+1] = 0
        
print(cnt)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def main():
    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(len(B)-1, -1, -1):
        m = min(A[i+1], B[i])
        ans += m
        A[i+1] -= m
        B[i] -= m

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

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
num = 0
for i in range(n):
    min_ab = min(a[i],b[i])
    num += min_ab
    a[i] -= min_ab
    b[i] -= min_ab
    if b[i] != 0:
        min_ab = min(a[i + 1],b[i])
        num += min_ab
        a[i + 1] -= min_ab
        b[i] -= min_ab
print(num)

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

Submission status: Accepted

Solution code:
n = int(input())
alst = list(map(int, input().split()))
blst = list(map(int, input().split()))
ans = 0
car = 0
for i in range(n):
  plus = min(blst[i] + car, alst[i])
  ans += plus
  if plus == alst[i]:
    car = blst[i] + car - alst[i]
    car = min(car, blst[i])
  else:
    car = 0
ans += min(car, alst[-1])
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(B)):
    cB = B[i]
    if A[i] > cB:
        ans += cB
    else:
        ans += A[i]
        cB -= A[i]
        if cB > A[i+1]:
            ans += A[i+1]
        else:
            ans += cB
        A[i+1] = max(0, A[i+1]-cB)
print (ans)

Submission status: Accepted

Solution code:
n = int(input())
aa = list(map(int, input().split()))
bb = list(map(int, input().split()))

ans = 0
for i in range(n):
    t1 = min(aa[i], bb[i])
    aa[i] -= t1
    bb[i] -= t1
    ans += t1
    t2 = min(aa[i+1], bb[i])
    aa[i+1] -= t2
    ans += t2
print(ans)

Submission status: Accepted

Solution code:
#街の数はn+1個　aiはi番目の街を襲うモンスター
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))

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



print(count)

Submission status: Accepted

Solution code:
n = int(input())
aa = list(map(int, input().split()))
bb = list(map(int, input().split()))

ans = 0
for i in range(n):
    t1 = min(aa[i], bb[i])
    aa[i] -= t1
    bb[i] -= t1
    ans += t1
    t2 = min(aa[i+1], bb[i])
    aa[i+1] -= t2
    bb[i] -= t2
    ans += t2
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(N):
    if A[i] < B[i]:
        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]
    else:
        ans += B[i]
        
print(ans)

Submission status: Accepted

Solution code:
def abc135c():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    ans = 0
    for i in range(n):
        v = min(b[i], a[i])
        ans += v
        b[i] -= v
        vv = min(a[i+1], b[i])
        a[i+1] -= vv
        ans += vv
    print(ans)
abc135c()

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):
  left = min(A[i], B[i])
  ans += left
  A[i] -= left
  B[i] -= left

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from math import gcd
from itertools import combinations,permutations,accumulate, product # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
import math
import bisect
import heapq
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入g
#
# インデックス系
# int min_y = max(0, i - 2), max_y = min(h - 1, i + 2);
# int min_x = max(0, j - 2), max_x = min(w - 1, j + 2);
#
#
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
#mod = 998244353
INF = float('inf')
from sys import stdin
readline = stdin.readline
def readInts():
  return list(map(int,readline().split()))
def readTuples():
    return tuple(map(int,readline().split()))
def I():
    return int(readline())
n = I()
A = readInts()
B = readInts()
ans = 0
for i in range(n):
    v = B[i]
    # kill = A[i] + A[i+1]
    nya = min(v, A[i])
    A[i] -= nya
    v -= nya
    ans += nya
    nya = min(A[i+1], v)
    ans += nya
    A[i+1] -= nya
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque,defaultdict,Counter
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import product,permutations,combinations,combinations_with_replacement
from bisect import bisect_left,bisect_right
from math import sqrt,gcd,ceil,floor,factorial
# from fractions import gcd
from functools import reduce
from pprint import pprint
from statistics import mean,median,mode
import sys
sys.setrecursionlimit(10 ** 6)

INF = float('inf')

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse=False):
    data.sort(key=lambda x:x[col],reverse=revese)
    return data

def mymax(data):
    M = -1*float('inf')
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float('inf')
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def mycount(ls,x):
    # lsはソート済みであること
    l = bisect_left(ls,x)
    r = bisect_right(ls,x)
    return (r-l)

def mydictvaluesort(dictionary):
    return sorted( dictionary.items(), key=lambda x:x[1] )

def mydictkeysort(dictionary):
    return sorted( dictionary.items(), key=lambda x:x[0] )

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(' ')
        elif type(ls[0])==str:
            print(' '.join(ls))
        elif type(ls[0])==int:
            print(' '.join(map(str,ls)))
        else:
            print('Output Error')
    else:
        if len(ls)==0:
            print('')
        elif type(ls[0])==str:
            print(''.join(ls))
        elif type(ls[0])==int:
            print(''.join(map(str,ls)))
        else:
            print('Output Error')

def I():
    return int(input())

def MI():
    return map(int,input().split())

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

def CI(n):
    return [ int(input()) for _ in range(n) ]

def LI(n):
    return [ list(map(int,input().split())) for _ in range(n) ]

def S():
    return input()

def MS():
    return input().split()

def RS():
    return list(input())

def CS(n):
    return [ input() for _ in range(n) ]

def LS(n):
    return [ list(input()) for _ in range(n) ]

# ddict = defaultdict(lambda: 0)
# ddict = defaultdict(lambda: 1)
# ddict = defaultdict(lambda: int())
# ddict = defaultdict(lambda: list())
# ddict = defaultdict(lambda: float())

mod = 10**9 + 7

n = I()
a = RI()
b = RI()
d = list(reversed(a))
e = list(reversed(b))

c1 = 0
for i in range(n):
    # print(f'i={i}')
    if a[i] >= b[i]:
        # print('A')
        c1 += b[i]
        a[i] -= b[i]
    elif (a[i]+a[i+1]) >= b[i]:
        # print('B')
        c1 += a[i]
        # a[i] = 0
        c1 += b[i] - a[i]
        a[i+1] -= (b[i] - a[i])
    elif (a[i]+a[i+1]) < b[i]:
        # print('C')
        c1 += a[i]
        # a[i] = 0
        c1 += a[i+1]
        a[i+1] = 0
    # print(c1,a)
print(c1)

# c2 = 0
# for i in range(n):
#     if d[i] >= e[i]:
#         c2 += e[i]
#     elif (d[i]+d[i+1]) >= e[i]:
#         c2 += e[i]
#         c2 += e[i] - d[i]
#         d[i+1] -= e[i] - d[i]
#     elif (d[i]+d[i+1]) < e[i]:
#         c2 += d[i]
#         c2 += d[i+1]
#         d[i+1] = 0
#     # print(c2,d)
# print(c2)

# ans = max(c1,c2)
# print(ans)

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 (0, N):
	if A[i] >= B[i]:
		count+=B[i]
	else:
		B[i]-=A[i]
		count+=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)

Submission status: Accepted

Solution code:
n = int(input())
al = list(map(int, input().split()))
bl = list(map(int, input().split())) + [0]
ans, tmp_b = 0, 0

for i in range(n+1):
    ans += min(al[i], tmp_b+bl[i])
    tmp_b = max(0, bl[i] - max(0, al[i]-tmp_b))
    # print(ans, tmp_b)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numba
from numba import njit, i8
import sys
import numpy as np
read = sys.stdin.readline


@njit((i8, i8[:], i8[:]), cache=True)
def main(n, a, b):
    ans = 0
    for i in range(n):
        d = min(a[i], b[i])
        ans += d
        b[i] -= d
        e = min(a[i + 1], b[i])
        ans += e
        a[i + 1] -= e
    print(ans)


if __name__ == '__main__':
    n = int(input())
    a = np.fromstring(read(), dtype=np.int64, sep=' ')
    b = np.fromstring(read(), dtype=np.int64, sep=' ')
    main(n, a, b)

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):
    tmp_l = min(A[i], B[i])
    ans += tmp_l
    A[i] -= tmp_l
    B[i] -= tmp_l
    tmp_r = min(A[i + 1], B[i])
    ans += tmp_r
    A[i + 1] -= tmp_r
    B[i] -= tmp_r
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(n):
    if a[i] <= b[i]:
        ans += a[i]
        b[i] -= a[i]
    else:
        ans += b[i]
        b[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]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

S_=sum(A)

print(S-S_)

Submission status: Accepted

Solution code:
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n = I()
    a = LI()
    b = LI()
    ans = 0

    for i in range(n):
        tem = a[i]
        a[i] = max(0,a[i]-b[i])
        attack = tem-a[i]
        b[i] -= attack
        ans += attack

        tem = a[i+1]
        a[i+1] = max(0,a[i+1]-b[i])
        attack = tem-a[i+1]
        ans += attack
    
    print(ans)


main()

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

Submission status: Accepted

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

ans = 0
carry = 0
for a, b in zip(A, B):
    a2 = max(0, a - carry)

    if a2 < b:
        ans += a
        carry = b - a2
    else:
        ans += carry + b
        carry = 0

ans += min(carry, A[-1])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

print(x - sum(a))

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

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int, input().split()))
b=list(map(int, input().split()))
ans=0
fii=min(a[0],b[0])
ans+=fii
fii1=min(a[1],b[0]-fii)
ans+=fii1
for i in range(1,n):
  fii=min(a[i]-fii1,b[i])
  ans+=fii
  fii1=min(a[i+1],b[i]-fii)
  ans+=fii1
print(ans)

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
 
# 勇者1...1番目・2番目の街で、B_1体まで
# 勇者2...2番目・3番目の街で、B_2体まで
# 勇者N...N番目・(N+1)番目の街で、B_N体まで

# 1番目の街は1番目の勇者が救う
# 1番目の勇者は1番目の街のモンスターを全力でmin(A_1,B_1) 体倒す
# 残った力で2番目の街のモンスターを全力でmin(A_2,B_1−min(A_1,B_1))体倒す
# 2番目の街に残ったモンスターは2番目の勇者が倒す
# これを繰り返す

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

# 初めのモンスター数 - 残ったモンスター数 = 勇者たちが倒した数
ans -= sum(A)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
 
# 勇者1...1番目・2番目の街で、B_1体まで
# 勇者2...2番目・3番目の街で、B_2体まで
# 勇者N...N番目・(N+1)番目の街で、B_N体まで
 
ans = sum(A)
for i in range(N):
  if A[i] >= B[i]: A[i] -= B[i]
  else:
    # A[i] < B[i]
    B[i] -= A[i]
    A[i] = 0
    A[i+1] = max(A[i+1]-B[i], 0) 

ans -= sum(A)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(x) for x in input().split()]
b=[int(x) for x in input().split()]
ans=0
for i in range(n):
    if b[i]<=a[i]:
        a[i]-=b[i]
        ans+=b[i]
        b[i]=0
    else:
        b[i]-=a[i]
        ans+=a[i]
        a[i]=0
        if b[i]<=a[i+1]:
            a[i+1]-=b[i]
            ans+=b[i]
            b[i]=0
        else:
            ans+=a[i+1]
            b[i]-=a[i+1]
            a[i+1]=0
print(ans)

Submission status: Accepted

Solution code:
import copy

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

ans = 0

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

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

Submission status: Accepted

Solution code:
n = int(input())
a_l = list(map(int, input().split()))
b_l = list(map(int, input().split()))

ans = 0
c = 0
for i in range(1, n+1):
    a = a_l[-i]
    if b_l[-i] + c >= a:
        ans += a
        d = c-a
        if d >=0:
            d=0
        c = b_l[-i] - abs(d)
    else:
        ans += b_l[-i] + c
        c = 0

if a_l[0] >= c:
    ans+= c
else:
    ans+= a_l[0]
print(ans)