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:
import sys

input = sys.stdin.readline

def main():
    ans = 0
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    for i in range(n):
        for j in range(i, i+2):
            if a[j] >= b[i]:
                a[j] -= b[i]
                ans += b[i]
                break
            else:
                ans += a[j]
                b[i] -=  a[j]
                a[j] = 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):
    tmp = 0
    if A[i] >= B[i]:
        A[i] -= B[i]
        ans += B[i]
    else:
        ans += A[i]
        tmp = B[i] - A[i]
        A[i] = 0
        if A[i+1] >= tmp:
            A[i+1] -= tmp
            ans += tmp
        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()))
total = 0
cnt = 0
total_l = []
cnt_l = []
for i in range(n):
    if a[i] - (b[i] - cnt) >= 0:
        total += b[i] - cnt
        cnt = 0
    else:
        total += a[i]
        cnt = max(-b[i], a[i] - (b[i] - cnt))

if cnt < 0:
    total += min(a[n], abs(cnt))

print(total)

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

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.buffer.readline
import os
from math import*

n=int(input())
arr=list(map(int,input().split()))
brr=list(map(int,input().split()))

ans=0
for i in range(n):
	if arr[i]>=brr[i]:
		ans+=brr[i]
	else:
		if arr[i]+arr[i+1]>=brr[i]:
			ans+=brr[i]
			arr[i+1]=arr[i]+arr[i+1]-brr[i]
		else:
			ans+=arr[i]+arr[i+1]
			arr[i+1]=0
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())
a = [*map(int, input().split())]
b = [*map(int, input().split())]
s = sum(a)
for i in range(n):
    p = a[i]
    a[i] = max(0, a[i] - b[i])
    if a[i] == 0:
        a[i + 1] = max(0, a[i + 1] + p - b[i])

print(s - sum(a))

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):
    b = min(A[i], B[i])
    ans += b
    A[i] -= b
    B[i] -= b
    a = min(A[i+1], B[i])
    ans += a
    A[i+1] -= a
    B[i] -= a
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] < B[i]:
    count += A[i]
    B[i] -= A[i]
    A[i] = 0
    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:
import sys

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

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

    if A[i] >= B[i]:
        ans += B[i]
    else:
        ans += A[i]
        plus = B[i] - A[i]
    # print(ans, plus)

if A[N] >= plus:
    ans += plus
else:
    ans += A[N]

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
alist=list(map(int,input().split()))
blist=list(map(int,input().split()))
ini=sum(alist)

for i in range(n):
    tmpa=alist[i]
    tmpb=blist[i]
    if tmpb >=tmpa:
        alist[i]=0
        tmpb-=tmpa
        alist[i+1]=max(0,alist[i+1]-tmpb)
    else:
        alist[i]-=tmpb
print(ini-sum(alist))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
al = list(map(int, input().split()))
bl = list(map(int, input().split()))

ans = 0
killed = 0
for i in range(n):
    rem_a = al[i] - killed
    if bl[i] >= rem_a:
        ans += rem_a
        rem_b = bl[i] - rem_a
        if rem_b >= al[i+1]:
            ans += al[i+1]
            killed = al[i+1]
        else:
            ans += rem_b
            killed = rem_b
    else:
        ans += bl[i]
        killed = 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(N):
    if A[i] > B[i]:
        cnt += B[i]
        A[i] -= B[i]
        B[i] = 0
        
    else:
        cnt += A[i]
        B[i] -= A[i]
        A[i] = 0
        
            
    if A[i+1] > B[i]:
        cnt += B[i]
        A[i+1] -= B[i]
        B[i] = 0
        
    else:
        cnt += A[i+1]
        B[i] -= A[i+1]
        A[i+1] = 0

    #print(A)
    #print(B)
    #print(cnt)
    #print('')
        
       
cnt += min(A[-1],B[-1]) 


print(cnt)

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    ans = 0
    for i in range(n):
        if b[i] >= a[i]:
            ans += a[i]
            b[i] -= a[i]
            if b[i] >= a[i+1]:
                ans += a[i+1]
                a[i+1] = 0
            else:
                ans += b[i]
                a[i+1] -= b[i]
        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()))
before = sum(A)

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

after = sum(A)
print(before - after)

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 = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]

Pre = sum(A)
for T in range(0,N):
    if A[T]<=B[T]:
        A[T+1] = max(A[T+1]-(B[T]-A[T]),0)
        A[T] = 0
    else:
        A[T] -= B[T]
Aft = sum(A)
print(Pre-Aft)

Submission status: Accepted

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

St = sum(A)
for T in range(0,N):
    if A[T]+A[T+1]<= B[T]:
        A[T] = 0
        A[T+1] = 0
    if A[T]<=B[T]<A[T]+A[T+1]:
        A[T+1] = A[T+1]-(B[T]-A[T])
        A[T] = 0
    if B[T]<A[T]:
        A[T] = A[T]-B[T] 
print(St-sum(A))

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

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

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

print(total)

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

Submission status: Accepted

Solution code:
def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    sumA = sum(A)
    
    for i in range(N):
        res = B[i]
        if A[i] > 0 and res != 0:
            A[i], res = max(A[i] - res, 0), max(res - A[i], 0)

        if A[i+1] > 0 and res != 0:
            A[i+1], res = max(A[i+1] - res, 0), max(res - A[i+1], 0)
    
    print(sumA - sum(A))
    return

resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

a = list(map(int,input().split()))

b = list(map(int,input().split()))

ans = 0
surplus = 0

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

tmp = min(a[n],surplus)
ans += tmp
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split())) 
    e = sum(B)
    for i in range(len(B)):
        attack = min(B[i],A[i])
        B[i] -= attack
        A[i] -= attack

        if i < N:
            attack = min(B[i],A[i+1])
            B[i] -= attack
            A[i+1] -= attack

    print(e-sum(B))



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(len(a)-1):
    #左倒す
    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
    b[i]-=right
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[N-i],B[N-1-i])
    if  B[N-i-1]>A[N-i]:
        ans+=min(A[N-i-1],B[N-1-i]-A[N-i])
        A[N-i-1]-=min(A[N-i-1],B[N-1-i]-A[N-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):
    amari =max(b[i]-a[i], 0)
    ans += min(b[i], a[i]+a[i+1])
    a[i+1] = max(0, -amari+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()))
s=0
c=0
for i in range(n):
    if b[i]<=a[i]:
        s+=b[i]
    else:
        s+=a[i]
        c=a[i+1]
        a[i+1]+=a[i]-b[i]
        if a[i+1]<0:
            a[i+1]=0
            s+=c
        else:
            s+=b[i]-a[i]
print(s)

Submission status: Accepted

Solution code:
n = int(input())
monster = list(map(int, input().split()))
brave = list(map(int, input().split()))
res = 0
for i in range(n):
  temp = min(monster[i], brave[i])
  brave[i] -= temp
  res += temp
  temp = min(monster[i + 1], brave[i])
  monster[i + 1] -= temp
  res += temp
print(res)

Submission status: Accepted

Solution code:
from collections import defaultdict
def readInt():
  return int(input())
def readInts():
  return list(map(int, input().split()))
def readChar():
  return input()
def readChars():
  return input().split()

n = readInt()
a = readInts()
b = readInts()
b.append(0)
b.append(0)

s = sum(a)

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

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

Submission status: Accepted

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

for i in range(N):
    i1_enemy = monster[i]
    i2_enemy = monster[i+1]
    atk = hero[i]
    if atk <= i1_enemy:
        ans += atk
    elif i1_enemy <= atk <= i1_enemy + i2_enemy:
        ans += atk
        monster[i+1] = i1_enemy + i2_enemy - atk
    else:
        ans += i1_enemy + i2_enemy
        monster[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+1] + a[i] > b[i]:
        ans += b[i]
        a[i+1] = min(a[i+1], a[i] + a[i+1] - b[i])
    else:
        ans += a[i] + a[i+1]
        a[i+1] = 0
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding:utf-8
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]:
            a[i + 1] -= b[i]
            ans += b[i]
        else:
            ans += a[i + 1]
            a[i + 1] = 0


print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(x) for x in input().rstrip().split()]
b=[int(x) for x in input().rstrip().split()]
ans=0
for i in range(len(b)):
  if a[i]+a[i+1]<=b[i]:
    ans+=a[i]+a[i+1]
    a[i]=0
    a[i+1]=0
  
  elif a[i]<=b[i]:
    ans+=a[i]
    ans+=(b[i]-a[i])
    a[i+1]=a[i+1]-(b[i]-a[i])
  else:
    a[i]-=b[i]
    ans+=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):
  if a[i] >= b[i]:
    ans += b[i]
  else:
    ans += a[i]
    b[i] -= a[i]
    if b[i] <= a[i+1]:
      a[i+1] -= b[i]
      ans += b[i]
    else:
      ans += a[i+1]
      a[i+1] = 0
      
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(input().split())
B = list(input().split())
#print (N, '\n', A, '\n', B)

def Levonas(a,b):
    if a-b < 0:
        return (0,b-a,a)
    else:
        return (a-b, 0, b)

i = 0
j = N-1
su = 0

while i <= j:
    te1 = Levonas(int(A[i]), int(B[i]))
    A[i] = te1[0]
    B[i] = te1[1]
    su = su + te1[2]
    te2 = Levonas(int(A[i+1]), int(B[i]))
    A[i+1] = te2[0]
    B[i] = te2[1]
    su = su + te2[2]
    i = i+1
    #print(i)
    #print("     ", A)
    #print("     ", B)
    te3 = Levonas(int(A[j+1]), int(B[j]))
    A[j+1] = te3[0]
    B[j] = te3[1]
    su = su + te3[2]
    te4 = Levonas(int(A[j]), int(B[j]))
    A[j] = te4[0]
    B[j] = te4[1]
    su = su + te4[2]
    j = j - 1
    #print(j)
    #print("     ", A)
    #print("     ", B)
    
while i <= j:
    if int(A[i])-int(B[i]) < 0:
        B[i] = int(B[i])-int(A[i])
        su = su + int(A[i])
        A[i] = 0
        if int(A[i+1])-int(B[i]) < 0:
            B[i] = int(B[i])-int(A[i+1])
            su = su + int(A[i+1])
            A[i+1] = 0
        else:
            su = su + int(B[i])
            A[i+1] = int(A[i+1])-int(B[i])
            B[i] = 0
    else:
        su = su + int(B[i])
        B[i] = 0
    i = i+1
    print(B)
    if int(A[j+1])-int(B[j]) < 0:
        B[j] = int(B[j])-int(A[j+1])
        su = su + int(A[j+1])
        A[j]=0
        if int(A[j])-int(B[j]) < 0:
            B[j] = int(B[j])-int(A[j])
            su = su + int(A[j])
            A[j] = 0
        else:
            su = su + int(B[j])
            A[j] = int(A[j])-int(B[j])
            B[j] = 0
        
    else:
        su = su + int(B[j])
        B[j] = 0
    j = j-1
    print(B)

print(su)

Submission status: Accepted

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

Submission status: Accepted

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

kills = 0
b_k = 0

for i in range(n):
    b_k = b[i]
    for j in range(2):
        if b_k - a[i+j] >= 0:
            kills += a[i+j]
            b_k -= a[i+j]
            a[i+j] = 0
        else:
            kills += b_k
            a[i+j] -= b_k
            b_k = 0

print(kills)

Submission status: Accepted

Solution code:
f=lambda:map(int,input().split())
input()
p,*A=f()
B=f()
s=0
for a,b in zip(A,B):
  t=min(b,a+p)
  p=a-max(0,t-p)
  s+=t
print(s)

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 i%2==0:
        if A[i//2]>=B[i//2]:
            A[i//2]-=B[i//2]
            ans+=B[i//2]
            B[i//2]=0
        else:
            B[i//2]-=A[i//2]
            ans+=A[i//2]
            A[i//2]=0
        if A[i//2+1]>=B[i//2]:
            A[i//2+1]-=B[i//2]
            ans+=B[i//2]
            B[i//2]=0
        else:
            B[i//2]-=A[i//2+1]
            ans+=A[i//2+1]
            A[i//2+1]=0
    else:
        if A[N-i//2]>=B[N-i//2-1]:
            A[N-i//2]-=B[N-i//2-1]
            ans+=B[N-i//2-1]
            B[N-i//2-1]=0
        else:
            B[N-i//2-1]-=A[N-i//2]
            ans+=A[N-i//2]
            A[N-i//2]=0
        if A[N-i//2-1]>=B[N-i//2-1]:
            A[N-i//2-1]-=B[N-i//2-1]
            ans+=B[N-i//2-1]
            B[N-i//2-1]=0
        else:
            B[N-i//2-1]-=A[N-i//2-1]
            ans+=A[N-i//2-1]
            A[N-i//2-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 i%2==0:
        if A[int(i/2)]>=B[int(i/2)]:
            A[int(i/2)]-=B[int(i/2)]
            ans+=B[int(i/2)]
            B[int(i/2)]=0
        else:
            B[int(i/2)]-=A[int(i/2)]
            ans+=A[int(i/2)]
            A[int(i/2)]=0
        if A[int(i/2)+1]>=B[int(i/2)]:
            A[int(i/2)+1]-=B[int(i/2)]
            ans+=B[int(i/2)]
            B[int(i/2)]=0
        else:
            B[int(i/2)]-=A[int(i/2)+1]
            ans+=A[int(i/2)+1]
            A[int(i/2)+1]=0
    else:
        if A[N-int(i/2)]>=B[N-int(i/2)-1]:
            A[N-int(i/2)]-=B[N-int(i/2)-1]
            ans+=B[N-int(i/2)-1]
            B[N-int(i/2)-1]=0
        else:
            B[N-int(i/2)-1]-=A[N-int(i/2)]
            ans+=A[N-int(i/2)]
            A[N-int(i/2)]=0
        if A[N-int(i/2)-1]>=B[N-int(i/2)-1]:
            A[N-int(i/2)-1]-=B[N-int(i/2)-1]
            ans+=B[N-int(i/2)-1]
            B[N-int(i/2)-1]=0
        else:
            B[N-int(i/2)-1]-=A[N-int(i/2)-1]
            ans+=A[N-int(i/2)-1]
            A[N-int(i/2)-1]=0

print(ans)

Submission status: Accepted

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

# In[26]:


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


# In[28]:


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


# In[ ]:

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

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

print(count)

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *AB = map(int, read().split())
    A = AB[: N + 1]
    B = AB[N + 1 :]

    M = sum(A)

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

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

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

for i in range(N):
  if B[i]<=A[i]:
    A[i]-=B[i]
  else:
    A[i+1]=max(0, A[i+1]-B[i]+A[i])
    A[i]=0
print(pre-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):
  f_1 = min(a[i],b[i])
  f_2 = max(0,min(a[i+1],b[i]-a[i]))
  cnt += f_1+f_2
  a[i] -= f_1
  a[i+1] -= f_2
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
cnt = 0
beforeKill = 0
for i in range(n):
    kill1 = min(A[i] - beforeKill, B[i])
    kill2 = min(A[i+1], B[i] - kill1)
    cnt += kill1 + kill2
    beforeKill = kill2
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]
  elif b[i] > a[i] :
    if b[i] - a[i]  > a[i+1]:
      cnt += a[i] + a[i+1]
      a[i+1] = 0
    else:
      cnt += b[i]
      a[i+1] = a[i]+ a[i+1] - b[i]


print(cnt)

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:
import math

n = int(input())
aL = list(map(int, input().split(" ")))
bL = list(map(int, input().split(" ")))

aSum = sum(aL)

for i in range(n):
    dmg1 = min(aL[i], bL[i])
    aL[i] -= dmg1
    bL[i] -= dmg1

    dmg2 = min(aL[i + 1], bL[i])
    aL[i + 1] -= dmg2
    bL[i] -= dmg2

print(aSum - sum(aL))

Submission status: Accepted

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

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

print(monster - sum(A))

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

Submission status: Accepted

Solution code:
def main():
    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] >= b[i]:
            a[i] = a[i] - b[i]
            c += b[i]
            b[i] = 0
        else:
            c += a[i]
            b[i] = b[i] - a[i]
            a[i] = 0
            if a[i+1] >= b[i]:
                a[i+1] = a[i+1] - b[i]
                c += b[i]
                b[i] = 0
            else:
                c += a[i+1]
                b[i] = b[i] - a[i+1]
                a[i+1] = 0
    print(c) 
           
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))#モンスター数
b=list(map(int,input().split()))#勇者キャパ
sm=0
c=0
for i in range(n):
  if c >=a[i]:
    sm+=a[i]
    c=b[i]
  elif b[i]+c>=a[i]:
    sm+=a[i]
    c=-a[i]+c+b[i]
  else:
    sm+=b[i]+c
    c=0
sm+=min(c,a[-1])
print(sm)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# スペース区切りの整数の入力
#N, K = map(int, input().split())
# 文字列の入力
N = int(input())
#開業されたN個要素を受け付ける
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 = ans+A[i]
        B[i] = B[i] - A[i]
        A[i] = 0
        if A[i+1] <= B[i]:
            ans = ans+A[i+1]
            B[i] = B[i] - A[i+1]
            A[i+1] = 0
        else:
            ans = ans+B[i]
            A[i+1] = A[i+1] - B[i]
            B[i] = 0
    else:
        ans = ans+B[i]
        A[i] = A[i] - B[i]
        B[i] = 0
    #print(A)
    #print(B)
    #print(ans)
    #print()

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
enemy  = list(map(int,input().split()))
hero  = list(map(int,input().split()))

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))
answer_list = [0]*N

for i in range(N):
    temp = B_list[i]

    for j in range(i, i+2):
        if temp - A_list[j] <= 0:
            answer_list[i] += temp
            A_list[j] -= temp
            break
        else:
            answer_list[i] += A_list[j]
            temp -= A_list[j]
            A_list[j] -= A_list[j]

print(sum(answer_list))

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
t = 0
for i in range(n):
  t = a[i]
  if t >= b[i]:
    t -= b[i]
    ans += b[i]
  else:
    ans += t
    b[i] -= t
    t = 0
    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()))
sm=sum(a)

cnt=0
rest=0

for i in range(n):
    a[i]=max(0,a[i]-rest)
    rest=0
    rest=max(0,b[i]-a[i])
    a[i]=max(0,a[i]-b[i])
a[n]=max(0,a[n]-rest)

print(sm-sum(a))

Submission status: Accepted

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

i, j = 0, 0
count = sum(a)

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

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

print(count)