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

print(bSum)

Submission status: Accepted

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

print(bSum)

Submission status: Accepted

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

monster = []
for i in range(N):
    monster.append(min(A[i]+A[i+1],B[i]))
    A[i+1] = min((A[i]+A[i+1])-monster[i],A[i+1])
print(sum(monster))

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] + 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())
a = [int(i) for i in input().split(" ")]
b = [int(i) for i in input().split(" ")]
c = min(a[0], b[0])
d = max(0, b[0]-a[0])
for i in range(n-1):
    c += min(a[i+1], d)
    d = max(0, a[i+1]-d)
    c += min(b[i+1], d)
    d = max(0, b[i+1]-d)
c += min(a[n], 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 reversed(range(N)):
    
    for j in reversed(range(2)):
    
        temp = min(A[i + j], B[i])
        ans += temp
        A[i + j] -= temp
        B[i] -= temp
        
print(ans)

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

print(sum)

Submission status: Accepted

Solution code:
N=int(input())
monster_num = list(map(int,input().split()))
maximum_kill = list(map(int,input().split()))

kill_num = [[0,0] for i in range(N)]

kill_num[0][0] = min(monster_num[0],maximum_kill[0])
kill_num[0][1] = min(monster_num[1], maximum_kill[0] - kill_num[0][0])
for i in range(1,N):
    kill_num[i][0] = min(monster_num[i] - kill_num[i-1][1], maximum_kill[i])
    kill_num[i][1] = min(monster_num[i+1], maximum_kill[i] - kill_num[i][0])
#print(kill_num)
print(sum(sum(kill) for kill in kill_num))

Submission status: Accepted

Solution code:
# -*- coding:utf8 -*-

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
d = 0
for i,n in enumerate(b):
    c = a[i]
    if n + d >= c:
        ans += c
        if c - d <= 0:
            d = n
        else:
            d = n - (c - d)
    else:
        ans += n + d
        d = 0
l = a[-1]
if l <= d:
    ans += l
else:
    ans += d
print(ans)

Submission status: Accepted

Solution code:
def main(): 

    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 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]

    print(cnt)



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 = m = 0
for i in range(N):
    m = min(A[i], B[i])
    A[i] -= m
    B[i] -= m
    ans += m
    m = min(A[i + 1], B[i])
    A[i + 1] -= m
    B[i] -= m
    ans += m
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):
    x = min(A[i], B[i])
    A[i] -= x
    B[i] -= x
    ans += x

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

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

Submission status: Accepted

Solution code:
import math

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

ans = 0

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy

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

result = 0
rest_power = 0
for i in range(N):
    if B[i] > A[i]:
        rest_power = B[i] - A[i]
        result += A[i]
        # 次の街を見る
        mem = copy.copy(A[i + 1])
        A[i + 1] -= rest_power
        if A[i + 1] < 0:
            result += mem
            A[i + 1] = 0
        else:
            result += rest_power
    else:
        result += B[i]

print(result)

Submission status: Accepted

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


def solve(N: int, A: "List[int]", B: "List[int]"):
    s = 0
    for i, b in enumerate(B):
        if A[i] + A[i + 1] <= b:
            s += A[i] + A[i + 1]
            A[i] = 0
            A[i + 1] = 0
            B[i] = 0
    for i, b in enumerate(B):
        sub = min(A[i], B[i])
        A[i] -= sub
        B[i] -= sub
        s += sub
        sub = min(A[i + 1], B[i])
        A[i + 1] -= sub
        B[i] -= sub
        s += sub
    print(s)
    return


# Generated by 1.1.5 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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()

Submission status: Accepted

Solution code:
N = raw_input()
enemy = map(int, raw_input().split())
hero = map(int, raw_input().split())

counter = 0
enemyKill = 0

while counter < int(N):
  if hero[counter] > enemy[counter]:
    hero[counter] -= enemy[counter]
    enemyKill += enemy[counter]
    enemy[counter] = 0
    if hero[counter] > enemy[counter + 1]:
      hero[counter] -= enemy[counter + 1]
      enemyKill += enemy[counter + 1]
      enemy[counter + 1] = 0
    else:
      enemy[counter + 1] -= hero[counter]
      enemyKill += hero[counter]
      hero[counter] = 0
  else:
    enemy[counter] -= hero[counter]
    enemyKill += hero[counter]
    hero[counter] = 0
  counter += 1  
print enemyKill

Submission status: Accepted

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

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


B[N-1] = B[N-1] - A[N]
if B[N-1] < 0:
    B[N-1] = 0

print(totalB - sum(B))

Submission status: Accepted

Solution code:
def solve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    result = 0
    
    for i in range(N):
        attack = min(A[i],B[i])
        B[i] -= attack
        A[i] -= attack
        result += attack
        if B[i] > 0:
            attack = min(A[i+1], B[i])
            B[i] -= attack
            A[i+1] -= attack
            result += attack
    
    print( result )
    
solve()

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

count = 0
for i in range(n+1):
    rg = 0
    if i == 0:
        rg = range(0, 1)
    elif i == n:
        rg = range(-1, 0)
    else:
        rg = range(-1, 1)

    for j in rg:
        if a[i] > b[i+j]:
            count += b[i+j]
            a[i] -= b[i+j]
            b[i+j] = 0
        else:
            count += a[i]
            b[i+j] -= a[i]
            a[i] = 0

print(str(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):
    temp = B[i] - A[i]
    if temp >= 0:
        ans += A[i]
        if A[i+1] >= temp:
            A[i+1] -= temp
            ans += temp
        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 = [int(i) for i in input().split()]
B_list = [int(i) for i in input().split()]

killed = 0
for i in range(N) :
    brave = B_list[i]
    monster = A_list[i]
    # print(brave,monster)
    if (brave >= monster) :
        # 殺りきれる場合
        killed += monster
        brave -= monster
        monster = A_list[i+1]
        if (brave >= monster) :
            killed += monster
            brave -= monster
            monster = 0
        elif (brave < monster) :
            # 殺りきれない場合
            killed += brave
            monster -= brave # この処理はいる
        A_list[i+1] = monster
            
    elif (brave < monster) :
        # 殺りきれない場合
        killed += brave
        monster -= monster # この処理はいらない

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

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 += A[i]
        #倒した分だけ勇者をへらす
        B[i] -= A[i]
        #次の街に倒しにいく
        #勇者は次の街のモンスターを全部倒せる
        if B[i] > A[i+1]:
            #モンスターを全部倒す
            count += A[i+1]
            A[i+1] = 0
        #勇者の数が足らないのでできるだけ倒す
        else:
            count += B[i]
            A[i+1]-=B[i]
    #勇者の数がモンスターと一緒か足らない
    else:
        #勇者の数だけモンスターを倒す
        count += B[i]
        #倒した分だけモンスターを減らす
        A[i] -= B[i]

print(count)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
L = 0
for i in range(N):
  l = min(A[i], B[i])
  B[i] -= l
  r = min(A[i + 1], B[i])
  A[i + 1] -= r
  L += l + r
print(L)

Submission status: Accepted

Solution code:
#Coding:Utf-1

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

print(count)

Submission status: Accepted

Solution code:
N=input()
N=int(N)
A=list(map(int,input().split()))
B=list(map(int,input().split()))
P=0
p1=min(A[0],B[0])
P12=[]
P+=p1
for i in range(N):
    if i>=1:
        p1=min(A[i]-P12[i-1],B[i])
        P+=p1
    p12=min(A[i+1],B[i]-p1)
    P12.append(p12)
    P+=p12
print(P)

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

Submission status: Accepted

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

count = 0
for i in range(N):
  k = min(monster[i], hero[i])
  l = min(monster[i+1], hero[i]-k)
  count += k+l
  monster[i+1] -= l
print(count)

Submission status: Accepted

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

ans = 0
for i, b in enumerate(B):
    defeated = min(A[i], b)
    A[i] -= defeated; b -= defeated
    ans += defeated
    if b > 0: defeated = min(A[i+1], b); A[i+1] -= defeated; ans += defeated
print(ans)

Submission status: Accepted

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

result = 0

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

print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N = int(input())
    A, B = [list(map(int, input().split())) for l in range(2)]
    A_sum = sum(A)
    for i, brave in enumerate(B):
        A[i] -= brave
        if A[i] < 0:
            A[i+1] += A[i]
            if A[i+1] < 0:
                A[i+1] = 0
            A[i] = 0
            
    print(A_sum - sum(A))

Submission status: Accepted

Solution code:
from sys import exit, stderr, stdin
input = stdin.readline
# setrecursionlimit(10**7)

def debug(var, name="hoge"):
    print(name +":" + str(type(var)) + " = " + repr(var), file=stderr)
    return


def main():
    N = int(input())
    A = [int(n) for n in input().split()]
    B = [int(n) for n in input().split()]
    ans = 0
    for i in range(N, -1, -1):
        # print(A)
        # print(B)
        if A[i] > 0:
            if i != N:
                beat = min(B[i],A[i])
                ans += beat
                A[i]-=beat
                B[i]-=beat
                if A[i] == 0:
                    continue
            beat = min(B[max(0,i-1)],A[i])
            ans += beat
            A[i]-=beat
            B[max(0,i-1)]-=beat
    # print(A)
    # print(B)
    print(ans)







if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict, deque
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate
import sys


def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LIM(): return list(map(lambda x:int(x) - 1, sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def LIRM(n): return [LIM() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
mod = 1000000007


def main():
    n = I()
    monster = LI()
    ret = 0
    ans = sum(monster)
    savor_lim = LI()
    for i in range(n - 1, -1, -1):
        k = min(savor_lim[i], monster[i + 1])
        monster[i + 1] -= k
        savor_lim[i] -= k
        monster[i] -= min(savor_lim[i], monster[i])
    return ans - sum(monster)



print(main())

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
for i in range(len(B)-2, -1, -1):
    temp = min(A[i+1], B[i])
    ans += temp
    B[i] -= temp

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

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):
  am, ap = A[i], A[i+1]
  b = B[i]
  first = min(am, b)
  second = min(ap, b-first)
  A[i] -= first
  A[i+1] -= second
  c += first + second

print(c)

Submission status: Accepted

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

ini = sum(a)
ans = 0
remain = a[0]

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

    
fin = sum(a)

print(ini-fin)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
count = 0
left = 0
for i in range(n):
    if b[i] + left - a[i] >= 0:
        count += a[i]
        left = b[i] + left - a[i]
        if left > b[i]:
            left = b[i]
    else:
        count += b[i] + left
        left = 0

count += min(left, a[n])
print(count)

Submission status: Accepted

Solution code:
n = int(input())
A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]
ans = 0
for i in range(n):
    s1 = min(A[i], B[i])
    ans += s1
    B[i] -= s1
    A[i] -= s1
    s2 = min(A[i+1], B[i])
    ans += s2
    B[i] -= s2
    A[i+1] -= s2
    
print(ans)

Submission status: Accepted

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

MaxValue = 0
if N == 1:
  MaxValue = min(B[0], A[0]+A[1])
  print(MaxValue)
else:
  for i in range(N):
    if A[i] < B[i]:
      #MaxValue += A[i]
      if A[i+1] < B[i] - A[i]:
        MaxValue += A[i+1] + A[i]
        A[i+1] = 0
      else:
        temp = B[i] - A[i]
        MaxValue += B[i]
        A[i+1] -= temp
    else:
      MaxValue += B[i]
  print(MaxValue)

Submission status: Accepted

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

fi1 = min(A[0], B[0])
fi2 = min(A[1], B[0] - fi1)
ans = fi1 + fi2
for i in range(1, N):
    fi1 = min(A[i] - fi2, B[i])
    fi2 = min(A[i+1], B[i] - fi1)
    ans += fi1 + fi2
    
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 B[i] >= A[i] and (B[i]-A[i]) >= A[i+1]:
        ans += A[i] + A[i+1]
        A[i] = 0
        A[i+1] = 0
    elif B[i] >= A[i] and (B[i]-A[i]) < A[i+1]:
        ans += B[i]
        A[i+1] -=  (B[i]-A[i])
        A[i] = 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()))

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a_arr = list(map(int,input().split()))
b_arr = list(map(int,input().split()))
cnt = 0
for i in range(n):
    tmp = min(a_arr[i],b_arr[i])
    cnt += tmp
    a_arr[i] -= tmp
    cnt += min(b_arr[i]-tmp,a_arr[i+1])
    a_arr[i+1] -= min(b_arr[i]-tmp,a_arr[i+1])
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(x) for x in input().split(' ')]
b = [int(x) for x in input().split(' ')]
c = 0

for i in range(n):
  if a[i] > 0:
    d = min(a[i], b[i])
    c += d
    a[i] -= d
    b[i] -= d

  if b[i] > 0:
    d = min(a[i+1], b[i])
    c += d
    a[i+1] -= d
    b[i] -= d

print(c)

Submission status: Accepted

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

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

Submission status: Accepted

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

mons = 0
tmp = 0

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

if a[n] >= tmp:
    mons += tmp
else:
    mons += a[n]

print(mons)

Submission status: Accepted

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

for i in range(n):
    t = min(a[i], b[i])
    c += t
    b[i] -= t
    if b[i]:
        t = min(a[i + 1], b[i])
        c += t
        a[i + 1] -= t
print(c)

Submission status: Accepted

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

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

killed = 0

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

print(killed)

Submission status: Accepted

Solution code:
N = int(input())
A_l = list(map(int,input().split()))
A_all = sum(A_l)
B_l = list(map(int,input().split()))

for i in range(N):
  a1 = A_l[i]
  a2 = A_l[i+1]
  b1 = B_l[i]
  if b1>a1:
    A_l[i] = 0
    b1 -= a1
  else:
    A_l[i] = a1-b1
    continue
  if b1>a2:
    A_l[i+1] = 0
  else:
    A_l[i+1] = a2-b1

print(A_all - sum(A_l))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    a = list(map(int, input().rstrip('\n').split()))
    b = list(map(int, input().rstrip('\n').split()))
    cnt = 0
    for i in range(n):
        s = min(a[i], b[i])
        a[i] -= s
        b[i] -= s
        cnt += s

        s = min(a[i+1], b[i])
        a[i+1] -= s
        b[i] -= s
        cnt += s
    print(cnt)


if __name__ == '__main__':
    slove()

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):
    tmp = min(a[i], b[i])
    ans += tmp
    a[i] -= tmp
    tmp2 = min(a[i+1], b[i] - tmp)
    ans += tmp2
    a[i+1] -= tmp2
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 B[i] >= A[i] + A[i+1]:
        cnt += A[i] + A[i+1]
        A[i+1] = 0
    elif A[i] < B[i]:
        cnt += A[i] + (B[i] - A[i])
        A[i+1] =  A[i+1] - (B[i] - A[i])
    elif A[i] > B[i]:
        cnt += B[i]
    elif A[i] == B[i]:
        cnt += A[i]
print(cnt)

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

Submission status: Accepted

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

print(count)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
first = 0
second = 0
former_second = 0
count = 0
for i in range(0, N):
    first = min(A[i] - former_second, B[i])
    second = min(A[i + 1], B[i] - first)
    former_second = second
    count += first + second

print(count)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split())) # 0からN(N+1個)
B = list(map(int, input().split())) # 0からN-1(N個)

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split())) # 0からN(N+1個)
B = list(map(int, input().split())) # 0からN-1(N個)

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
ml=list(map(int,input().split()))
sl=list(map(int,input().split()))
ans,a1=0,ml[0]
for a2,b in zip(ml[1:],sl):
  x=min([a1,b])#first left saver's kill
  y=b-min([a1,b])#left saver's left power
  z=min([y,a2])#second left saver's kill
  a1=a2-z#middle monster's left power
  ans+=(x+z)#all kill
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):
    for j in range(2):
        if b[i] - a[i+j] <= 0:
            a[i+j] -= b[i]
            ans += b[i]
            break
        else:
            b[i] -= a[i+j]
            ans += a[i+j]
            a[i+j] = 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] * (N+1)

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

Submission status: Accepted

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

ans = 0
for i in range(a):
  ans += min(b[i],c[i])
  c[i] -= min(b[i],c[i])
  ans += min(b[i + 1], c[i])
  b[i + 1] -= min(b[i + 1], c[i])

print(ans)

Submission status: Accepted

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