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

for i in range(N):
  num = min(A[i], B[i])
  A[i] -= num
  B[i] -= num
  
  if B[i]:
    num = min(A[i+1], B[i])
    A[i+1] -= num
    
print(sumA - 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, b in enumerate(B):
    if b <= A[i]:
        count += b
    else:
        count += A[i]
        buf = b - A[i]
        if A[i+1] >= buf:
            count += buf
            A[i+1] -= buf
        else:
            count += A[i+1]
            A[i+1] = 0

print(count)

Submission status: Accepted

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

    cnt = 0
    tmp = 0
    for i in range(len(b)):
        if a[i] - b[i] >= 0:
            cnt += b[i]
            a[i] -= b[i]
        else:
            tmp = b[i]-a[i]
            cnt += a[i]
            a[i] = 0
            if a[i+1] -tmp >= 0:
                cnt += tmp
                a[i+1] -= tmp
            else:
                cnt += a[i+1]
                a[i+1] = 0
    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
ai = list(map(int, input().split()))
bi = list(map(int, input().split()))

ans = 0
for i in range(n):
    ans += min(ai[i], bi[i])
    ans += min(ai[i+1], bi[i]-min(ai[i], bi[i]))
    ai[i+1] -= min(ai[i+1], bi[i]-min(ai[i], bi[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(A[i],B[i])
    if B[i]>A[i]:
        sarani = min(A[i+1],B[i]-A[i])
        A[i+1] -= sarani
        ans += sarani
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()]

count=0

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

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

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
sum=np.sum(b)
for i in reversed(range(n)):
  k = a[i+1]
  l = a[i]
  m = b[i]
 
  a[i+1] = max(k-m, 0)
  b[i] = max(m-k, 0)
 
  a[i] = max(l-b[i], 0)
  b[i] = max(b[i]-l, 0)
postsum=np.sum(b)
print(sum-postsum)

Submission status: Accepted

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

for i in range(n):
    if A[i] > B[i]:
        rs += B[i]
    else:
        t = min(B[i]-A[i], A[i+1])
        A[i+1] -= t
        rs += t + A[i]
print(rs)

Submission status: Accepted

Solution code:
#python3
import numpy as np
n=int(input())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
sum=np.sum(b)
for i in reversed(range(n)):
  k = a[i+1]
  l = a[i]
  m = b[i]

  a[i+1] = max(k-m, 0)
  b[i] = max(m-k, 0)

  a[i] = max(l-b[i], 0)
  b[i] = max(b[i]-l, 0)
postsum=np.sum(b)
print(sum-postsum)

Submission status: Accepted

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

Submission status: Accepted

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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
a = LI()
b = LI()
ans = 0
for i in range(n):
    cnt = b[i]
    if a[i] >= cnt:
        a[i] -= cnt
        ans += cnt
        continue
    else:
        ans += a[i]
        ans += min(a[i+1],(cnt - a[i]))
        a[i+1] = max(0,a[i+1] - (cnt - a[i]))
print(ans)

Submission status: Accepted

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

killed = 0
for i in range(n):
    now_kill = min(a[i],b[i])
    killed += now_kill
    a[i] -= now_kill
    b[i] -= now_kill
    if b[i] == 0:continue
    now_kill = min(a[i+1],b[i])
    killed += now_kill
    a[i+1] -= now_kill
    b[i] -= now_kill

print(killed)

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(0, N):
    a = min(A[i], B[i])
    ans += a
    # 使ったヒーローの力を減算
    B[i] -= a

    b = min(A[i + 1], B[i])
    ans += b
    # 倒した分だけ残りのモンスターを減算
    A[i + 1] -= b
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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

Submission status: Accepted

Solution code:
import sys

def main():
    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] -= min(A[i], B[i])

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

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

sm = sum(B)
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(sm - sum(B))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(fenum - sum(A))

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):
    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])
after = sum(A)

answer = before - after
print(answer)

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

    for i in range(N):
        if a[i] >= b[i]:
            ans += b[i]
        else:
            if a[i+1] >= (b[i] - a[i]):
                ans += b[i]
                a[i+1] = a[i+1] - (b[i] - a[i])
            else:
                ans += (a[i+1]+a[i])
                a[i+1] = 0
    return int(ans)
if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
n = int(input())
ls_a = list(map(int, input().split()))
ls_b = list(map(int, input().split()))

ans = 0
res = 0

for i in range(n):
	ans += min(ls_a[i], res + ls_b[i])
	if res >= ls_a[i]:
		res = ls_b[i]
	else:
		res = max(res + ls_b[i] - ls_a[i], 0)

if res >= ls_a[n]:
	print(ans + ls_a[n])
elif 0 <= res < ls_a[n]:
	print(ans + res)
else:
	print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

    count = 0
    for i in range(n):
        # print("-"*50)
        # print("A: {} A+1: {} B: {}".format(a_list[i], a_list[i+1], b_list[i]))
        if a_list[i] >= b_list[i]:
            a_list[i] -= b_list[i]
            count += b_list[i]
            b_list[i] = 0
        else:
            b_list[i] -= a_list[i]
            count += a_list[i]
            a_list[i] = 0

        # print("A: {} A+1: {} B: {}".format(a_list[i], a_list[i + 1], b_list[i]))

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

        # print("A: {} A+1: {} B: {}".format(a_list[i], a_list[i + 1], b_list[i]))

    print(count)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]

enemy = sum(a)
for i in range(len(b)-1, -1, -1):
    tmp = max(a[i + 1] - b[i], 0)
    b[i] -= a[i + 1] - tmp
    a[i + 1] = tmp
    tmp = max(a[i] - b[i], 0)
    b[i] -= a[i] - tmp
    a[i] = tmp
ans = enemy - sum(a)
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
a0 = np.array(list(map(int, input().split())))
a = a0.copy()
b = np.array(list(map(int, input().split())))
for i in range(n):
  c = a[i] - b[i]
  if c < 0:
    a[i] = 0
    a[i+1] = a[i+1] + c
    if a[i+1] < 0:
      a[i+1] = 0
  else:
    a[i] = a[i] - b[i]
print(sum(a0 - a))

Submission status: Accepted

Solution code:
N = int(input())
town = list(map(int,input().split(" ")))
town_before = sum(town)
brave = list(map(int,input().split(" ")))
for i in range(N):
    if town[i] < brave[i]:
        brave[i] -= town[i]
        town[i] = 0
        if town[i + 1] < brave[i]:
            town[i + 1] = 0
        else:
            town[i + 1] -= brave[i]
    else:
        town[i] -= brave[i]
print(town_before - sum(town))

Submission status: Accepted

Solution code:
#n = int(input())
#L = list(map(int, input().split()))
import math
import sys
import heapq
import fractions
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]:
        if B[i] >= A[i+1]+A[i]:
            ans += A[i]+A[i+1]
            A[i+1] = 0
        else:
            A[i+1] -= B[i]-A[i]
            ans += B[i]
    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()))
A.reverse()
B.reverse()
before = sum(A)
for i in range(N):
    if B[i]<=A[i]:
        A[i]=A[i]-B[i]
    elif A[i]<B[i]<=A[i]+A[i+1]:
        A[i+1]=A[i]+A[i+1]-B[i]
        A[i]=0
    else:
        A[i]=0
        A[i+1]=0
after = sum(A)
print(before-after)

Submission status: Accepted

Solution code:
n = int(input())
l1 = list(map(int, input().split()))
l2 = list(map(int, input().split()))
c = 0

for i in range(n):
    if i != n-1:
        if l2[i] >= l1[i]:
            c += l1[i]
            diff = l2[i] - l1[i]
            l2[i+1] += min(diff, l1[i+1])
        else:
            c += l2[i]
    else:
        if l2[i] >= l1[i] + l1[i+1]:
            c += l1[i] + l1[i+1]
        else:
            c += l2[i]

print(c)

Submission status: Accepted

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

z = 0
ans = 0
for a, b in zip(A, B):
    ans += min(a, b + z)
    z = max(0, b - max(0, a - z))

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):
    if A[i] >= B[i]:
        ans += B[i]
    elif A[i] + A[i+1] > B[i]:
        ans += B[i]
        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().strip())
A = list(map(int, input().strip().split()))
B = list(map(int, input().strip().split()))

def calc(a, b, c):
    if a < b:
        b -= a 
        c += a 
        a = 0 
    else:
        a -= b 
        c += b 
        b = 0
    return a, b, c 

c = 0
for i in reversed(range(N)):
    A[i+1], B[i], c = calc(A[i+1], B[i], c)
    A[i], B[i], c = calc(A[i], B[i], c)

print(c)

Submission status: Accepted

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

Submission status: Accepted

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

s = sum(B)
for i in range(n):
    diff = min(A[i], B[i])
    A[i] -= diff
    B[i] -= diff
    diff_next = min(A[i+1], B[i])
    A[i+1] -= diff_next
    B[i] -= diff_next

print(s - sum(B))

Submission status: Accepted

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

ans = 0
for i,b in enumerate(B):
    temp = min(b,A[i]+A[i+1])
    ans += temp
    A[i+1] -= max(0, temp-A[i]) #有り余った力を次の街で倒す
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):
    if A[i] >= B[i]:
        ans += B[i]
    elif A[i]+A[i+1] > B[i]:
        ans += B[i]
        A[i+1] = A[i]+A[i+1] -B[i]
    else: #B[i]が大きいとき，A[i+1]を消費済みにする
        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()))

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

print(monster)

Submission status: Accepted

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

count = 0

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

    count += kill

print(count)

Submission status: Accepted

Solution code:
# abc135 c city savers
n = int(input())
a_list = list(map(int,input().split())) # 魔物
b_list = list(map(int,input().split())) # 勇者

count = 0
for i in range(n):
    if a_list[i] <= b_list[i]: # i番目の魔物を全部倒せるとき
        #　a_list i+1の数をb_list[i] - a_list[i]分減らす
        yoryoku = b_list[i] - a_list[i]
        if yoryoku < a_list[i+1]: # 余力では次の魔物を全部倒せない時
            count += a_list[i] + yoryoku
            a_list[i+1] -= yoryoku
            a_list[i] = 0
        else: # 余力で次の魔物を全部倒せる時
            count += a_list[i] + a_list[i+1]
            a_list[i + 1] = 0
            a_list[i] = 0
    else:  # 全部倒せないとき
        count += b_list[i]
print(count)

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

Submission status: Accepted

Solution code:
n = int(input())
monster = list(map(int, input().split()))
yusya = list(map(int, input().split()))
prev = 0
res = 0
for i in range(n):
  if yusya[i] <= monster[i]:
    res += yusya[i]
    continue
  else:
    res += monster[i]
    yusya[i] -= monster[i]
    if yusya[i] <= monster[i+1]:
      monster[i+1] -= yusya[i]
      res += yusya[i]
    else:
      res += monster[i+1]
      monster[i+1] = 0
print(res)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc135/tasks/abc135_c

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = 0
l = A[0]
for i in range(N):
    r = A[i + 1]
    cur = B[i]
    # print(l, r, cur)
    # ans += min(l + r, cur)
    # l = max(r - max(cur - l, 0), 0)  # めちゃくちゃ整理するとこの形の式になる

    # わかりやすくかきなおす
    ans += min(l, cur)
    rest_power = max(cur - l, 0)
    ans += min(rest_power, r)
    l = max(r - rest_power, 0)  # next l

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = 0
l = A[0]
for i in range(N):
    r = A[i + 1]
    cur = B[i]
    # print(l, r, cur)
    ans += min(l + r, cur)
    l = max(r - max(cur - l, 0), 0)  # めちゃくちゃ整理するとこの形の式になる
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):
    if A[i] + A[i+1] <= B[i]:
        c += A[i] + A[i+1]
        A[i] = 0
        A[i+1] = 0
    elif A[i]<=B[i]:
        c += A[i]
        A[i+1] = A[i+1] - (B[i] - A[i])
        c += (B[i] - A[i])
        A[i] = 0
    else:
        A[i] = A[i] - B[i]
        c += B[i]
print(c)

Submission status: Accepted

Solution code:
import sys
import itertools
import bisect
import math

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n = ni()
a = na()
b = na()

ans = 0

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

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = tuple(map(int, input().split()))

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

            aj = a[i+1]
            if aj >= bi:
                a[i+1] -= bi
                ans += bi
            else:
                a[i+1] = 0
                ans += aj
    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()))
sumA = sum(A)

for i in range(N, 0, -1):
    tmpA = A[i]
    tmpB = B[i-1]
    A[i] = max(0, tmpA-tmpB)
    B[i-1] = max(0, tmpB-tmpA)
    tmpA = A[i-1]
    tmpB = B[i-1]
    A[i-1] = max(0, tmpA-tmpB)
    B[i-1] = max(0, tmpB-tmpA)
print(sumA-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()]
A_sum = sum(A)

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

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = tuple(map(int, input().split()))

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

            aj = a[i+1]
            if aj >= bi:
                a[i+1] -= bi
                ans += bi
            else:
                a[i+1] = 0
                ans += aj
    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()))
s=sum(b)
for i in range(n):
    if b[i]>0:
        if b[i]>=a[i]:
            b[i]-=a[i]
        else:
            a[i]-=b[i]
            b[i]=0
    if b[i]>0:
        if b[i]>=a[i+1]:
            x=a[i+1]
            b[i]-=x
            a[i+1]-=x
        else:
            a[i+1]-=b[i]
            b[i]=0
print(s-sum(b))

Submission status: Accepted

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

Submission status: Accepted

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

def kill(saver, next, curr):
	killed = 0
	if saver >= next:
		saver -= next
		killed += next
		if saver >= curr:
			killed += curr
			return 0, killed
		else:
			killed += saver
			return curr-saver, killed 
	else:
		killed += saver
		return curr, killed

cnt = 0
res = A[-1]
for i in range(len(B)):
	ans = kill(B[len(B)-i-1], res, A[len(A)-i-2])
	res = ans[0]
	cnt += ans[1]
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

cnt = 0
#c_ = 0

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

        else:
            cnt += a[i+1]
            a[i+1] = 0

    else:
        cnt += b[i]
     # if i == n:
    #     c = c_ - a[i]
    #     if c > 0:
    #         cnt += a[i]
    #     else:
    #         cnt += c_
    #
    # else:
    #     c = c_ + b[i] - a[i]
    #     if c > 0:
    #         c_ = c
    #         cnt += a[i]
    #     else:
    #         cnt += b[i] + c_
    #         c_ = 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
for i in range(N):

    if B[i] >= A[i]:
        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

    else:
        ans += B[i]

print(ans)

Submission status: Accepted

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


def solve(N: int, A: "List[int]", B: "List[int]"):
    answer = 0

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

            if A[i+1]>=B[i]:
                answer += B[i]
                A[i+1]-=B[i]
            else:
                answer += A[i+1]
                A[i+1]=0
    print(answer)
        
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N + 1)]  # type: "List[int]"
    B = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A, B)

if __name__ == '__main__':
    main()