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

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

Submission status: Accepted

Solution code:
s=input()
t=list(map(int,input().split()))
u=list(map(int,input().split()))
list=[]
a=int(s)
c=t[0]
d=0
e=0
for i in range(a):
  if t[i]<=u[i]:
    if t[i+1]<=u[i]-t[i]:
      d+=t[i]+t[i+1]
      t[i+1]=0
    else:
      d+=u[i]
      t[i+1]=t[i+1]+t[i]-u[i]
  else:
    d+=u[i]
print(d)

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

print(count)

Submission status: Accepted

Solution code:
def main():
    n, *ab = map(int, open(0).read().split())
    a = ab[:n+1]
    b = ab[n+1:]

    init_a_sum = sum(a)
    for i, cur_b in enumerate(b):
        if a[i] > cur_b:
            a[i] -= cur_b
        else:
            a[i+1] = max(0, a[i+1] -  (cur_b - a[i]))
            a[i] = 0
    last_a_sum = sum(a)
    ans = init_a_sum - last_a_sum
    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()))

a = a[::-1]
b = b[::-1]
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)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
start = [0]*(N+1)
attack = 0
enemy = 0

for i in range(N+1):
    start[i] = A[i]

for i in range(N):
    attack = min(A[i],B[i])
    A[i] = A[i] - attack
    B[i] = B[i] - attack
    attack = min(A[i+1],B[i])
    A[i+1] = A[i+1] - attack
    B[i] = B[i] - attack
    
for i in range(N+1):
    enemy = enemy + (start[i]-A[i])
    
print(enemy)

Submission status: Accepted

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


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

print(kill)

Submission status: Accepted

Solution code:
def main():

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

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

    print(cnt)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
monsters = list(map(int, input().split()))
yusha_power = list(map(int, input().split()))

count = 0
for yusha_index in range(len(yusha_power)):
    beaten = min(yusha_power[yusha_index], monsters[yusha_index])
    yusha_power[yusha_index] -= beaten
    monsters[yusha_index] -= beaten
    count += beaten

    beaten = min(yusha_power[yusha_index], monsters[yusha_index+1])
    yusha_power[yusha_index] -= beaten
    monsters[yusha_index+1] -= beaten
    count += beaten
print(count)

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

Submission status: Accepted

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

prev_monster = sum(A)
#勇者は左(i番目)を優先して倒していく方針で
for i in range(N):
  if(A[i] <= B[i]):#左を全滅できるなら
    if(A[i+1] <= B[i]-A[i]):#右も全滅できるなら
      A[i+1] = 0
    else:
      A[i+1] = A[i+1] - (B[i]-A[i])
    A[i] = 0
  else:
    A[i] = A[i]-B[i]

after_monster = sum(A)
ans = prev_monster - after_monster
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:
        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=list(map(int,input().split()))
B=list(map(int,input().split()))
count=0
for i in range(N):
  if A[i]+A[i+1]<=B[i]:
    count+=A[i]+A[i+1]
    A[i+1]=0
  elif A[i]<=B[i]:
    count+=B[i]
    A[i+1]=A[i+1]-(B[i]-A[i])
  else:
    count+=B[i]
print(count)

Submission status: Accepted

Solution code:
N = int(input())
enemy = list(map(int ,input().split()))
brave = list(map(int, input().split()))
enemy = enemy[::-1]
brave = brave[::-1]
sum = 0
for i in range(N):
    star = min(enemy[i] , brave[i])
    brave[i] = brave[i] - enemy[i]
    if brave[i] > 0:
        add_star = min(enemy[i + 1], brave[i])
        enemy[i+1] = enemy[i + 1] - add_star
        star += add_star
    sum+= star
print(sum)

Submission status: Accepted

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

E = 0

for i in range(0,N):#0-Nまで
	for j in range(i,i+2):#i+1-iまで

		if B[i] - A[j] > 0:
			E += A[j]
			B[i] = B[i] - A[j]
			A[j] = 0
		if B[i] - A[j] <= 0:
			E += B[i]
			A[j] = A[j] - B[i]
			B[i] = 0
			break
	
print(E)

Submission status: Accepted

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

count = 0

for i in range(0, N):
    a0 = A[i]
    b = B[i]
    
    if a0 < b:
        A[i] = 0
        count += a0
        b -= a0

        a1 = A[i+1]
        if a1 < b:
            A[i+1] = 0
            count += a1
        else:
            A[i+1] -= b
            count += b
    else:
        A[i] -= b
        count += b
print(count)

Submission status: Accepted

Solution code:
n = int(input())
*a, = map(int, input().split())
*b, = map(int, input().split())
count = 0
for i in range(n):
    tmp = min(a[i], b[i])
    count += tmp
    b[i] -= tmp
    tmp2 = min(a[i+1], b[i])
    count += tmp2
    a[i+1] -= tmp2

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

    if judge < 0:
        if (A[i+1]+judge) >= 0:
            count += -judge
            A[i+1] += judge
        else:
            count += A[i+1]
            A[i+1] = 0
print(count)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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


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

Submission status: Accepted

Solution code:

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

sup = 0
count = 0
for i in range(len(B)):
	b = B[i]
	m = A[i]
	if m - sup >=  b:
		count += sup + b
		sup = 0
	elif m-sup < b:
		count += m
		sup =  b - max(0, m - sup)

print(count)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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

    if (A[i] > 0) and (B[i] > 0):
        tmp = min(A[i],B[i])
        count += tmp
        A[i] -= tmp
        B[i] -= tmp

if (A[N] > 0) and (B[N-1] > 0):
    tmp = min(A[N],B[N-1])
    count += tmp
    A[N] -= tmp
    B[N-1] -= tmp 

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

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

Submission status: Accepted

Solution code:
N = int(input())
mon = list(map(int,input().split()))
hero = list(map(int,input().split()))

ans = 0
for i in range(N):
  if mon[i] >= hero[i]:
    ans += hero[i]
  else:
    tem = mon[i+1]
    mon[i+1] = max(0,mon[i+1]-(hero[i]-mon[i]))
    ans += mon[i]+tem-mon[i+1]
    
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
monsters = list(map(int,input().split()))
savers = list(map(int,input().split()))

fii = []
fij = []

fii += [min(monsters[0], savers[0])]
fij += [min(monsters[1], savers[0]-fii[-1])]
for i in range(1,n):
    fii += [min(monsters[i]-fij[-1], savers[i])]
    fij += [min(monsters[i+1], savers[i]-fii[-1])]
print(sum(fii)+sum(fij))

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):
    if A[i]<=B[i]:
        B[i]-=A[i]
        counter+=A[i]
        A[i]=0
    elif B[i]<A[i]:
        counter+=B[i]
        A[i]-=B[i]
        B[i]=0

    if A[i+1]<=B[i]:
        B[i]-=A[i+1]
        counter+=A[i+1]
        A[i+1]=0
    elif B[i]<A[i+1]:
        counter+=B[i]
        A[i+1]-=B[i]
        B[i]=0

print(counter)

Submission status: Accepted

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

        else:
            num += zan
            A[i + 1] -= zan

    else:
        num += B[i]


#num += min(A[N], zan)

print(num)

Submission status: Accepted

Solution code:
n = int(raw_input())
a = map(int, raw_input().split()) # city
b = map(int, raw_input().split()) # attacker
m = 0
for i in xrange(n):
  p = min(a[i], b[i])
  a[i] = a[i] - p
  c = b[i] - p
  q = min(a[i+1], c) 
  a[i+1] = a[i+1] - q
  m += p + q
print m

Submission status: Accepted

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

sum=0
for i in range(n):
	sum+=s[i][0]+s[i][1]
print(sum)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
# ABC135C - City Savers
# greedy algorithm
def main():
    N, *AB = map(int, open(0).read().split())
    ans, a1 = 0, AB[0]
    for a2, b in zip(AB[1 : N + 1], AB[N + 1 :]):
        cnt1 = min(a1, b)
        cnt2 = min(a2, b - cnt1)
        ans += cnt1 + cnt2
        a1 = a2 - cnt2  # a2 becomes a1 in next turn
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC135C - City Savers
# greedy algorithm
def main():
    N, *AB = map(int, open(0).read().split())
    ans, a1 = 0, AB[0]
    for a2, b in zip(AB[1 : N + 1], AB[N + 1 :]):
        cnt1 = a1 if a1 < b else b
        x = b - cnt1
        cnt2 = a2 if a2 < x else x
        ans += cnt1 + cnt2
        a1 = a2 - cnt2  # a2 becomes a1 in next turn
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#135c
N = int(input())
list_A = list(map(int, input().split()))
list_B = list(map(int, input().split()))
 
#print("#################################################")
 
ans = 0
remaining_power = 0
 
if N>1:
    for i in range(N-1,-1,-1):
        #print(i,"… RP:",remaining_power,",A:",list_A[i+1],",B:",list_B[i])
 
        if remaining_power>list_A[i+1]:
            #余力のみで倒せる
            remaining_power=list_B[i]
            ans += list_A[i+1]
            #print("margin")
        elif remaining_power + list_B[i] > list_A[i+1]:
            #余力と勇者で倒せる
            remaining_power = list_B[i] - (list_A[i+1] - remaining_power)
            ans += list_A[i+1]
            #print("OK")
        else:
            #余力と勇者でも倒せない
            ans += remaining_power + list_B[i]
            remaining_power = 0
            #print("No")
else:
    remaining_power = list_B[0] 
            
if remaining_power > list_A[0]:
    ans += list_A[0]
else:
    ans += remaining_power
        
print(ans)

Submission status: Accepted

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

zan = 0
ans = 0
for i in range(n):
    temp = b_list[i] + zan
    s = a_list[i]
    if s >= temp:
        ans += temp
        zan = 0
    elif s < temp and s >= zan:
        ans += s
        zan = temp - s        
    else:
        ans += s
        zan = b_list[i]

if zan > a_list[n]:
    ans += a_list[n]
else:
    ans += zan
        
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

list_A = list(map(int, input().split()))
list_B = list(map(int, input().split()))

score = 0
for i in range(len(list_B)):
    #全滅
    if list_A[i] < list_B[i]:
        score = score + list_A[i]

        #次の街
        #次の街も全滅
        if list_A[i+1] < list_B[i]-list_A[i]:
            score = score + list_A[i+1]
            list_A[i+1] = 0
        #次の街を全滅させられない場合
        else:
            score = score + list_B[i]-list_A[i]
            list_A[i+1] = list_A[i+1] - (list_B[i]-list_A[i])

    else:
        score = score + list_B[i]

print(score)

Submission status: Accepted

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

cnt = 0
res = 0

for i in range(N):
    if A[i] >= res:
        cnt += res
        A[i] -= res
    else:
        cnt += A[i]
        A[i] = 0
        
    hit = min(A[i],B[i])
    cnt += hit
    res = B[i] - hit
    
cnt += min(A[N], res)

print(cnt)

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(0,n):
    zan = 0
    tres = 0
    ta = a[i]
    tb = b[i]

    if tb >= ta:
        res += ta
        zan = tb-ta
        tres = min(a[i+1],zan)
        a[i+1] -= tres
        res += tres

    else:
        res += tb
        
print(res)

Submission status: Accepted

Solution code:
N = int(input())

lstA = list(map(int,input().split()))
lstB = list(map(int,input().split()))

x = 0
ans = 0
for i in range(N):
    if ( lstB[i] >= (lstA[i] + lstA[i+1]) ):
        ans += (lstA[i] + lstA[i+1])
        lstA[i+1] = 0
    elif ( lstB[i] > lstA[i] ):
        ans += lstB[i]
        lstA[i+1] = lstA[i] + lstA[i+1] - lstB[i]
    else:
        ans += lstB[i]

print( ans )

Submission status: Accepted

Solution code:
# ABC136 C-City Savers
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 A[i] <= B[i] <= A[i] + A[i+1]:
        ans += B[i]
        A[i+1] -= (B[i] - A[i])
    elif A[i] + A[i+1] <B[i]:
        ans += A[i] + A[i+1]
        A[i+1] = 0
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A_list = list(map(int, input().split())) # len : N+1
B_list = list(map(int, input().split())) # len : N
ans = 0
before_num = 0
for i in range(N+1):
    if i < N:
        if A_list[i] >= before_num:
            ans += before_num
            A_list[i] -= before_num
            if A_list[i] >= B_list[i]:
                ans += B_list[i]
                A_list[i] -= B_list[i]
                B_list[i] = 0
                before_num = 0
            elif A_list[i] < B_list[i]:
                ans += A_list[i]
                B_list[i] -= A_list[i]
                A_list[i] = 0 
                before_num = B_list[i]
        elif A_list[i] < before_num:
            ans += A_list[i]
            before_num = B_list[i]
            A_list[i] = 0
    if i == N:
        if A_list[i] >= before_num:
            ans += before_num
            A_list[i] -= before_num
        elif A_list[i] < before_num:
            ans += A_list[i]
            A_list[i] = 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]:
    B[i]-=A[i]
    ans+=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()))
asum=sum(a)
b=list(map(int,input().split()))
for i in range(n):
    if a[n-i]<=b[n-i-1]:#勇者の次の街にいるモンスターが勇者が倒せる数より少ない場合
        b[n-i-1]-=a[n-i]
        a[n-i]=0
        if a[n-i-1]<=b[n-i-1]:#勇者の街にいるモンスターが勇者が倒せる数ののこりより少ない場合
            b[n-i-1]-=a[n-i-1]
            a[n-i-1]=0
        else:
            a[n-i-1]-=b[n-i-1]
            b[n-i-1]=0
    else:
        a[n-i]-=b[n-i-1]
        b[n-i-1]=0
answer=asum-sum(a)
print(answer)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

N = int(input())
A_list = list(map(int, input().split())) # len : N+1
B_list = list(map(int, input().split())) # len : N
ans = 0
before_num = 0
for i in range(N+1):
    if i < N:
        if A_list[i] >= before_num:
            ans += before_num
            A_list[i] -= before_num
            if A_list[i] >= B_list[i]:
                ans += B_list[i]
                A_list[i] -= B_list[i]
                B_list[i] = 0
                before_num = 0
            elif A_list[i] < B_list[i]:
                ans += A_list[i]
                B_list[i] -= A_list[i]
                A_list[i] = 0 
                before_num = B_list[i]
        elif A_list[i] < before_num:
            ans += A_list[i]
            before_num = B_list[i]
            A_list[i] = 0
    if i == N:
        if A_list[i] >= before_num:
            ans += before_num
            A_list[i] -= before_num
        elif A_list[i] < before_num:
            ans += A_list[i]
            A_list[i] = 0

print(ans)

Submission status: Accepted

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

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

    else:
        goukei += b[i]
print(goukei)

Submission status: Accepted

Solution code:
N = int(input())
Alist = list(map(int, input().split()))
Blist = list(map(int, input().split()))
beforeN = sum(Alist)
for i in range(N - 1, -1, -1):
    rest_power = Blist[i] - Alist[i + 1]
    if rest_power > 0:
        Alist[i + 1] = 0
        last_power = Alist[i] - rest_power
        if last_power <= 0:
            Alist[i] = 0
        else:
            Alist[i] = Alist[i] - rest_power
    else:
        Alist[i + 1] = Alist[i + 1] - Blist[i]
        continue
ans = beforeN - sum(Alist)
print(ans)

Submission status: Accepted

Solution code:
n  = int(input())
a  = list(map(int,input().split()))
a = a[::-1]
b  = list(map(int,input().split()))
b = b[::-1]
c  = 0

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

print(c)

Submission status: Accepted

Solution code:
n = int(input())
AL = list(map(int,input().split()))
BL = list(map(int,input().split()))
ans = 0
for i in range(n):
    if BL[i] <= AL[i]:
        ans += BL[i]
    elif BL[i] > AL[i] and BL[i] <= AL[i]+AL[i+1]:
        ans += BL[i]
        AL[i+1] = AL[i+1] - BL[i] + AL[i]
    else:
        ans += AL[i]+AL[i+1]
        AL[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()]
start = sum(A)
for i in range(N):
    A[i] = A[i] - B[i]
    if A[i] < 0:
        m = A[i] * (-1)
        A[i] = 0
        A[i+1] = max(A[i+1]-m,0)
end = sum(A)
print(start - end)

Submission status: Accepted

Solution code:

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

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

count = 0
for i in range(N):
    # 倒しきらずに途中で力尽きる or 相打ち
    if A[i] >= B[i]:
        count += B[i]
    else:
        # 余力あり
        count += A[i]
        B[i] -= A[i]

        # 残機でi+1を処理
        if A[i+1] >= B[i]:
            A[i+1] -= B[i]
            count += B[i]
        else:
            count += A[i+1]
            A[i+1] = 0


print(count)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N=int(input())
A=[int(x) for x in input().split()]
B=[int(x) for x in input().split()]
m=0
for i in range(0,N):
    d=B[i]-A[i]
    if d > 0:
        if A[i+1]-d <=0:
            B[i]=A[i]+A[i+1]
            A[i]=0
            A[i+1]=0
        else:
            A[i]=0
            A[i+1]=A[i+1]-d
    else:
        A[i]=-d
print(sum(B))

Submission status: Accepted

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

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

  if B[i] != 0:
    if B[i] < A[i+1]:
      result += B[i]
      A[i+1] -= B[i]
    elif B[i] >= A[i+1]:
      result += A[i+1]
      A[i+1] = 0

print(result)

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

Submission status: Accepted

Solution code:
n = int(input())
monsters = list(map(int, input().split()))
heroes = list(map(int, input().split()))

cnt = 0
for i in range(n):
  if heroes[i] <= monsters[i]:
    cnt += heroes[i]
  else:
    cnt += monsters[i]
    heroes[i] -= monsters[i]
    if heroes[i] <= monsters[i+1]:
      cnt += heroes[i]
      monsters[i+1] -= heroes[i]
    else:
      cnt += monsters[i+1]
      monsters[i+1] = 0
  
print(cnt)

Submission status: Accepted

Solution code:
num = int(input())

a = list(map(int,input().split()))
b = list(map(int,input().split()))
count = 0
for i in range(num):
    if b[i] >= a[i]:
        cp=a[i]
        count += a[i]
        a[i]=0
        b[i] -=cp
        if b[i] >= a[i+1]:
            cp = a[i+1]
            count += cp
            a[i+1] = 0
        else:
            a[i+1] -=b[i]
            count += b[i]
    else:
        a[i] -=b[i]
        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()))

res = 0

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

    if d >= 1:
        res += A[i + 1]
        if A[i] >= d:
            A[i] -= d
            res += d
        else:
            res += A[i]
            A[i] = 0
    else:
        res += B[i]

print(res)

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,v in enumerate(b):
    if a[i]>v:
        a[i] -= v
    elif a[i]+a[i+1]>v:
        a[i+1] -= (v-a[i])
        a[i]   = 0
    else:
        a[i]=a[i+1]=0
print(s-sum(a))

Submission status: Accepted

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

for val in range(n+1):
    yoryoku = yoryoku_L[val] + b[val] - a[val]

    if yoryoku >= 0:
        result = a[val]

        if a[val] - yoryoku_L[val] >= 0:
            yoryoku_L.append(yoryoku)
        else:
            yoryoku_L.append(b[val])

        result_L.append(result)
    else:
        result = b[val] + yoryoku_L[val]
        yoryoku_L.append(0)
        result_L.append(result)


print(sum(result_L))

Submission status: Accepted

Solution code:
import heapq

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

#print(A)
print(sum)

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):
  v = min(A[-i-1],B[-i-1])
  cnt += v
  A[-i-1] -= v
  B[-i-1] -= v
  w = min(A[-i-2],B[-i-1])
  cnt += w
  A[-i-2] -= w
  B[-i-1] -= w
    
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
x = 0
for i in range(n):
    for j in range(2):
        xi = min(a[i + j], b[i])
        a[i + j] -= xi
        b[i] -= xi
        x += xi
print(x)

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

for i in range(1, n):
    ctr += min(a[i], b[i - 1])
    a[i] -= min(a[i], b[i - 1])
    ctr += min(a[i], b[i])
    b[i] -= min(a[i], b[i])

ctr += min(a[n], b[n - 1])

print(ctr)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
d = 0
c = A[0]

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

Submission status: Accepted

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

M = 0

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

print(M)

Submission status: Accepted

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