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

ans = 0

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

    else:
        ans += b[i]
        a[i] -= b[i]

print(ans)

Submission status: Accepted

Solution code:
#ABC135_C
#紙に書いて考える。具体例を考える。

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

ans=0
rem=A[0]

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

if __name__ == "__main__":
    sys.exit(main())

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def main():
    N = I()
    a = LI()
    b = LI()
    cnt = 0
    for i in range(len(b)):
        left = min(a[i], b[i])
        cnt += left
        b[i] -= left

        right = min(a[i+1], b[i])
        cnt += right
        b[i] -= right
        a[i+1] -= right
    print(cnt)
    """
    a = [0] + a
    b = [0] + b + [0]
    cnt = 0
    for i in range(1, N+2):
        if a[i] - b[i-1] >= 0:
            cnt += b[i-1]
            a[i] = a[i] - b[i-1]
            b[i-1] = 0
        else:
            cnt += a[i]
            b[i-1] = b[i-1] - a[i]
            a[i] = 0

        if a[i] - b[i] >= 0:
            cnt += b[i]
            a[i] = a[i] - b[i]
            b[i] = 0

        else:
            cnt += a[i]
            b[i] = b[i] - a[i]
            a[i] = 0
    print(cnt)
    """

main()

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def main():
    N = I()
    a = LI()
    b = LI()
    a = [0] + a
    b = [0] + b + [0]
    cnt = 0
    for i in range(1, N+2):
        if a[i] - b[i-1] >= 0:
            cnt += b[i-1]
            a[i] = a[i] - b[i-1]
            b[i-1] = 0
        else:
            cnt += a[i]
            b[i-1] = b[i-1] - a[i]
            a[i] = 0

        if a[i] - b[i] >= 0:
            cnt += b[i]
            a[i] = a[i] - b[i]
            b[i] = 0

        else:
            cnt += a[i]
            b[i] = b[i] - a[i]
            a[i] = 0
    print(cnt)

main()

Submission status: Accepted

Solution code:
from __future__ import print_function

import sys
input = sys.stdin.readline

def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return

# import math
# import string
# import fractions
# from fractions import Fraction
# from fractions import gcd

# def lcm(n,m):
#     return int(n*m/gcd(n,m))

# import re
# import array
# import copy
# import functools
# import operator

# import collections
# import itertools
# import bisect
# import heapq


# from heapq import heappush
# from heapq import heappop
# from heapq import heappushpop
# from heapq import heapify
# from heapq import heapreplace

# from queue import PriorityQueue as pq

# def reduce(p, q):
#     common = fractions.gcd(p, q)
#     return (p//common , q//common )
# # from itertools import accumulate
# # from collections import deque

# from operator import mul
# from functools import reduce

# def combinations_count(n, r):
#     r = min(r, n - r)
#     numer = reduce(mul, range(n, n - r, -1), 1)
#     denom = reduce(mul, range(1, r + 1), 1)
#     return numer // denom

# import random

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

    cnt=0;i=0
    while i<n:
        if b[i]==0 or (a[i]==0 and a[i+1]==0):
            i+=1
            
        else:
            temp=min(a[i],b[i]);a[i]-=temp;b[i]-=temp;cnt+=temp
            if b[i]==0:
                pass
            else:
                temp=min(b[i],a[i+1]);a[i+1]-=temp;b[i]-=temp;cnt+=temp
    print(cnt)
    
        
    return

if __name__ == '__main__':
    main()
'''
for given
n (1<=n<=10**5),
[a_i] (\forall i in [0...(n-1)].1<=a_i<=10**9 ),
[b_i] (\forall i in [0...(n-1)].1<=b_i<=10**9 )

find x_cnt such that
x_cnt:= sum(\for i in [0...(n-1)].b[i]    barahbarahbarah)


(a[i] + a[i+1] <= b[i] then )
min(a[i] + a[i+1] , b[i])

\forall i in [0...(n-1)]. a[i] - c[i]

'''

Submission status: Accepted

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

Submission status: Accepted

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

ans=0
for i in range(n):
    left=min(a[i],b[i])
    ans+=left
    a[i]-=left
    b[i]-=left
    right=min(a[i+1],b[i])
    ans+=right
    a[i+1]-=right
    b[i]-=right
print(ans)

Submission status: Accepted

Solution code:
#ABC-136-C
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
hunted_monsters = 0

for i in range(N):
    #勇者iが街iのモンスターを全て倒しても余力がある場合
    if A[i] < B[i]:
        B[i] -= A[i]
        hunted_monsters += A[i]
        A[i] = 0
        #街i+1のモンスターも全て倒せる場合
        if A[i+1] <= B[i]:
            B[i] -= A[i+1]
            hunted_monsters += A[i+1]
            A[i+1] = 0
        #街i+1のモンスターを一部倒せる場合
        else:
            A[i+1] -= B[i]
            hunted_monsters += B[i]
            B[i] = 0
    #勇者iが街iのモンスターの一部(もしくはぴったり全て)倒して余力がない場合
    else:
        A[i] -= B[i]
        hunted_monsters += B[i]
        B[i] = 0

print(hunted_monsters)

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

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
def main():
    n = int(input())
    a  = list(map(int, input().split()))
    b  = list(map(int, input().split()))
    #print(a,b)

    total = np.sum(a)
    for i in range(n):
        
        if b[i] >= a[i]:
            a[i+1] -= (b[i]-a[i])
            a[i] = 0
            #print(a[i+1])
            if a[i+1] < 0:
                a[i+1] = 0

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

    print(total-np.sum(a))

    
    
if __name__ == '__main__':
    main()

Submission status: Accepted

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

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]
        b[i] = 0
    elif a[i] == b[i]:
        count += a[i]
        b[i] = 0
    else:
        count += a[i]
        b[i] -= a[i]
        if a[i+1] > b[i]:
            count += b[i]
            a[i+1] -= b[i]
            b[i] = 0
        elif a[i+1] == b[i]:
            count += a[i+1]
            a[i+1] = 0
            b[i] = 0
        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()))
S=sum(A)
for i in range(N):
    if A[i]<B[i]:
        B[i]-=A[i]
        A[i]=0
        A[i+1]=max(0,A[i+1]-B[i])
    else:
        A[i]-=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()))
cnt = 0
for i in range(n):
    if a[i] >= b[i]:
        cnt += b[i]
    else:
        cnt += a[i]
        if a[i+1] > b[i]-a[i]:
            a[i+1] -= (b[i] - a[i])
            cnt += b[i]-a[i]
        else:
            cnt += a[i+1]
            a[i+1] = 0
print(cnt)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(buster)

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
for i in range(len(B)):
    #勇者が全滅させたら、隣町から余力をとる
    if B[i]>A[i]:
        mod=min(A[i+1],B[i]-A[i])
        A[i+1]-=mod
        #この街と隣町
        ans+=int(A[i]+mod)
   
    #敵が余るときは勇者は力の限り全部倒す
    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()))
m = 0
for i in range(N):
    p = min(A[i], B[i])
    B[i] -= p
    m += p
    q = min(A[i+1], B[i])
    A[i+1] -= q
    m += q
print(m)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
i = 0
j = 0
f = []
while (j < len(a)):
    f.append(a[j])
    j += 1

while (i < n):
    if (a[i]-b[i] < 0):
        b[i] -= a[i]
        a[i] = 0
        if (a[i+1] < b[i]):
            a[i+1] = 0
        else:
            a[i+1] -= b[i]
    else:
        a[i] -= b[i]
    i += 1
print (sum(f) - sum(a))

Submission status: Accepted

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

counter = 0
for i in range(len(b)):
    counter += min(a[i],b[i])
    if b[i] - a[i] >= 0:
        c = b[i]-a[i]
        if a[i + 1] - c >= 0:
            a[i + 1] -= c
            counter += c
        else:
            counter += a[i + 1]
            a[i + 1] = 0

print(counter)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
	n = int(input())
	mon = list(map(int,input().split()))
	b = list(map(int,input().split()))
	# print(n,mon,b)
	ans = 0
	for i in range(n):
		syori = 0
		if b[i]-mon[i] < 0:
			# print("b[i]-mon[i] < 0")
			ans += b[i]
			# b[i] -= mon[i]
			mon[i] -= b[i]
			syori = 1
			# print("mon[i] = {}".format(mon[i]))
			# print("b[i] = {}".format(b[i]))
		if b[i]-mon[i] >= 0 and syori == 0:
			# print("b[i]-mon[i] >= 0")
			ans += mon[i]
			b[i] -= mon[i]
			mon[i] = 0
			# print("mon[i] = {}".format(mon[i]))
			# print("b[i] = {}".format(b[i]))
			if b[i]-mon[i+1] <0:
				# print("if b[i]-mon[i+1] <0:")
				ans += b[i]
				mon[i+1] -= b[i]
				syori = 1
				# print("mon[i+1] = {}".format(mon[i+1]))
			if b[i] - mon[i+1] >= 0 and syori == 0:
			# else:
				# print("if b[i] - mon[i+1] >= 0:")
				ans += mon[i+1]
				mon[i+1] = 0
				# print("b[i] = {}".format(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()))

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
Ans = sum(B)
AT = []
for i in range(N//2+1):
  AT+=[[i,i],[i+1,i]]
  AT+=[[N-i,N-i-1],[N-i-1,N-i-1]]
AT = AT[:2*N]
for i in AT:
  X = min(A[i[0]],B[i[1]])
  A[i[0]]=A[i[0]]-X
  B[i[1]]=B[i[1]]-X
Ans = Ans - sum(B)
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):
  m=min(a[i],b[i])
  b[i]-=m
  r=min(a[i+1],b[i])
  a[i+1]-=r
  count+=m+r
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 reversed(range(len(B))):
#     # print(i)
#     if A[i+1] >= B[i]:
#         total += B[i]
#         B[i] = 0
#         continue
#     else:
#         total += A[i+1]
#         B[i] -= A[i+1]
#         if A[i] >= B[i]:
#             total += B[i]
#             A[i] -= B[i]
#         else:
#             total += A[i]
for i in range(len(B)):
    temp = min(A[i], B[i])
    A[i] -= temp
    B[i] -= temp
    total += temp
    temp = min(A[i+1], B[i])
    A[i+1] -= temp
    B[i] -= temp
    total += temp
print(total)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
from itertools import *
from math import *

INF = int(1e18)
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
before = sum(A)

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
before = sum(A)

for i in range(N):
    nokori = B[i]
    for j in range(i,i+2):
        if nokori >= A[j]:
            nokori -= A[j]
            A[j] = 0
        else:
            A[j] -= nokori
            nokori = 0
after = sum(A)

print(before - after)

Submission status: Accepted

Solution code:
def city_savers(N: int, A: list, B: list)->int:
    ans = 0
    for i in range(N):
        attacked = min(B[i], A[i])
        B[i] -= attacked
        A[i] -= attacked
        ans += attacked

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

    # print(A)
    # print(B)
    return ans


if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    ans = city_savers(N, A, B)
    print(ans)

Submission status: Accepted

Solution code:
def calculate(arr1,arr2):

    result = 0
    for i in range(len(arr2)):

        if (arr1[i] + arr1[i+1]) >= arr2[i]:
            result += arr2[i]
        else:
            result += arr1[i] + arr1[i+1]

        res1 = arr2[i] - arr1[i]

        if arr2[i] >= arr1[i]:
            arr1[i] = 0
        else:
            arr1[i] = arr1[i] - arr2[i]

        if res1 > 0:
            if arr1[i+1] >= res1:
                arr1[i+1] = arr1[i+1] - res1
            else:
                arr1[i+1] = 0


    return result


N = int(input())
arr1 = list(map(int, input().split()))
arr2 = list(map(int, input().split()))

result = calculate(arr1, arr2)

print(result)

Submission status: Accepted

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

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) :
    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
y = sum(b)
print(x-y)

Submission status: Accepted

Solution code:
import sys

if sys.platform =='ios':
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

N = INT()
A = MAP()
B = MAP()

ans = 0
for i in range(N):
	if A[i] >= B[i]:
		ans += B[i]
		A[i] -= B[i]
	else:
		#print(i+1,'th yuusya({})'.format(B[i]),A[i],A[i+1])
		# 残っているA[i]がいれば片付ける
		ans += A[i]
		B[i] -= A[i]
		A[i] = 0
		
		# 次の怪獣をできるかぎり倒しておく
		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, A)

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n=int(input())
p=list(map(int,input().split()))
p.append(0)
pp=list(map(int,input().split()))
c=0
for i in range(n):
    if p[i]>=pp[i]:
        c+=pp[i]
    elif p[i+1]>=pp[i]-p[i]:
        c=c+pp[i]
        pp[i] = pp[i]-p[i]
        p[i+1] = p[i+1]-pp[i]
    else:
        c=c+p[i]+p[i+1]
        p[i+1]=0
print(c)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    monsters = [int(x) for x in input().split()]
    heroes = [int(x) for x in input().split()]

    sum_monsters = sum(monsters)
    for i in range(n-1, -1, -1):
        if monsters[i+1] >= heroes[i]:
            monsters[i+1] -= heroes[i]
            heroes[i] = 0
        else:
            temp_m = monsters[i+1]
            monsters[i+1] = 0
            heroes[i] -= temp_m
            if monsters[i] >= heroes[i]:
                monsters[i] -= heroes[i]
                heroes[i] = 0
            else:
                temp_m = monsters[i]
                monsters[i] = 0
                heroes[i] -= temp_m

    ans = sum_monsters - sum(monsters)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

knock = 0
ex = 0
for i in range(n):
    if a[i] < b[i]:
        ex = b[i] - a[i]
        knock += a[i]
    else:
        ex = 0
        knock += b[i]

    if ex != 0:
        if a[i+1] - ex <= 0:
            knock += a[i+1]
            a[i+1] = 0
        else:
            knock += ex
            a[i+1] -= ex

print(knock)

Submission status: Accepted

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

ret = 0 # 倒した数

for i in range(N):
    k = A[i] - B[i]
    
    if k < 0: # 勇者の余力あり
        ret += A[i]

        if (A[i+1] + k) > 0:
            A[i+1] += k
            ret += abs(k)
        else:
            ret += A[i+1]
            A[i+1] = 0
    else:
        ret += B[i]
        
print(ret)

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]<b[i]:
        if a[i+1]>=b[i]-a[i]:
            ans+=b[i]
            a[i+1]-=b[i]-a[i]
        elif a[i+1]<b[i]-a[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()))
#print(a)
#print(b)
ans=0
for i in range(n):
    t=min(a[i],b[i])
    ans+=t
    a[i]-=t
    b[i]-=t
    t=min(a[i+1],b[i])
    ans+=t
    a[i+1]-=t
    b[i]-=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(n) :
  if b[i] >= a[i] :
    if a[i+1] >= (b[i]-a[i]) :
      a[i+1] = a[i+1] - (b[i]-a[i])
      ans += b[i]
    else :
      ans += a[i] + 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] :
    if a[i+1] >= (b[i]-a[i]) :
      a[i+1] = a[i+1] - (b[i]-a[i])
      ans += b[i]
    else :
      ans += a[i] + 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()))
s=0
for i in range(n-1,-1,-1):
  if a[i+1]>=b[i]:
    a[i+1]-=b[i]
    s+=b[i]
    continue
  elif a[i+1]<=b[i]<=a[i]+a[i+1]:
    s+=b[i]
    a[i]=a[i]+a[i+1]-b[i]
    continue
  elif b[i]>=a[i]+a[i+1]:
    s+=a[i]+a[i+1]
    a[i]=0
    continue
print(s)

Submission status: Accepted

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

point = 0
i = 0
while (i < N):
  if A[i] >= B[i]:
    point += B[i]
  else:
    if(A[i] + A[i+1]) > B[i]:
      point += B[i]
      A[i+1] -= (B[i] - A[i])
    else:
      point += (A[i] + A[i+1])
      A[i+1] = 0
  i += 1
print(point)

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]<0:
    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 = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]

m = sum(a)

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

print(m - sum(a))

Submission status: Accepted

Solution code:
#coding: utf-8
import math
import heapq
import bisect

MOD = 10**9+7

N = int(input())

# 複数個の数値を、intの配列として取得
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(0,B[i]-A[i])
    ans+=min(A[i+1],B[i])
    A[i+1]=max(0,A[i+1]-B[i])

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
ans = 0
for i in range(N):
  if A[i] > B[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 = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
ans = 0

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

if b[n-1] != 0:
    if a[n] > b[n-1]:
        ans += b[n-1]
    else:
        ans += a[n]

print(ans)

Submission status: Accepted

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

ans = sum(A)

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

print(ans - sum(A))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    readline = stdin.readline
    readline()
    *A, = map(int, readline().split())
    *B, = map(int, readline().split())
    ans = 0
    pre = 0
    for a, b in zip(A, B):
        brave = pre + b
        if brave < a:
            ans += brave
            pre = 0
        elif a < pre:
            ans += a
            pre = b
        else:
            ans += a
            pre = brave - a
    if A[-1] < pre:
        ans += A[-1]
    else:
        ans += pre
    print(ans)
    return


main()

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    readline = stdin.readline
    readline()
    *A, = map(int, readline().split())
    *B, = map(int, readline().split())
    B.append(0)
    ans = 0
    pre = 0
    for a, b in zip(A, B):
        brave = pre + b
        if brave < a:
            ans += brave
            pre = 0
        elif a < pre:
            ans += a
            pre = b
        else:
            ans += a
            pre = brave - a
    print(ans)
    return


main()

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    readline = stdin.readline
    readline()
    *A, = map(int, readline().split())
    *B, = map(int, readline().split())
    B.append(0)
    ans = 0
    pre = 0
    for a, b in zip(A, B):
        brave = pre + b
        if brave < a:
            ans += brave
            pre = 0
        elif a < pre:
            ans += a
            pre = b
        else:
            ans += a
            pre = brave - a
    if A[-1] < pre:
        ans += A[-1]
    else:
        ans += pre
    print(ans)
    return


main()

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

Submission status: Accepted

Solution code:
from sys import stdin

input = stdin.readline


def main():
    input()
    *A, = map(int, input().split())
    *B, = map(int, input().split())
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()