Problem:


Score : 300 points

Problem Statement

There are N+1 towns. The i-th town is being attacked by A_i monsters.

We have N heroes. The i-th hero can defeat monsters attacking the i-th or (i+1)-th town, for a total of at most B_i monsters.

What is the maximum total number of monsters the heroes can cooperate to defeat?

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_{N+1}
B_1 B_2 ... B_N

Output

Print the maximum total number of monsters the heroes can defeat.

Sample Input 1

2
3 5 2
4 5

Sample Output 1

9

If the heroes choose the monsters to defeat as follows, they can defeat nine monsters in total, which is the maximum result.

The first hero defeats two monsters attacking the first town and two monsters attacking the second town.

The second hero defeats three monsters attacking the second town and two monsters attacking the third town.

Sample Input 2

3
5 6 3 8
5 100 8

Sample Output 2

22

Sample Input 3

2
100 1 1
1 100

Sample Output 3

3



Sample input:
2
3 5 2
4 5


Sample output:
9

Submission status: Accepted

Solution code:
import sys, math
from collections import deque

readline = sys.stdin.readline

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

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))
count = 0
for i in range(n):
    c = min(a_list[i], b_list[i])
    a_list[i] -= c
    b_list[i] -= c
    count += c
    if b_list[i] > 0:
        c2 = min(a_list[i + 1], b_list[i])
        a_list[i + 1] -= c2
        count += c2
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]:
        ans+=B[i]
    else:
        a=A[i+1]
        A[i+1]=max(0,A[i+1]+A[i]-B[i])
        ans+=A[i]+(a-A[i+1])
print(ans)

Submission status: Accepted

Solution code:
def attack(i):
    attack = 0
    attack_i = A_list[i]
    if(B_list[i] > A_list[i]):
        B_list[i] = B_list[i] - A_list[i]
        attack = A_list[i]
        A_list[i] = 0 
        attack += min(B_list[i], A_list[i+1])
        A_list[i+1] = max(A_list[i+1] - B_list[i], 0)
    else:
        A_list[i] = A_list[i] -  B_list[i]
        attack += B_list[i]
    return attack

N = int(input())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))

ans = 0
for i in range(N):
    ans += attack(i)
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(100000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *

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]
        continue
    else:
        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]
print(ans)

Submission status: Accepted

Solution code:
import sys
#input = sys.stdin.readline
from collections import Counter
def inpl(): return list(map(int, input().split()))

N = int(input())
B = inpl()
A = inpl()

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

ans=0

k = min(A[0],B[0])
A[0] -= k
B[0] -= k
ans +=k
for i in range(1, n+1):
    kk = min(B[i-1],A[i])
    A[i] -= kk
    B[i-1] -= kk
    ans += kk
    if i!=n:
        mm=min(B[i],A[i])
        A[i] -= mm
        B[i] -= mm
        ans += mm
    #print(ans)
    #print(A,B)
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
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]>=0:
        cnt+=a[i]
        b[i]-=a[i]
    else:
        cnt+=b[i]
        b[i]=0
    if b[i]>0:
        if b[i]-a[i+1]>=0:
            cnt+=a[i+1]
            a[i+1]=0
        else:
            cnt+=b[i]
            a[i+1]-=b[i]
print(cnt)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
def InputToInt():
    A = input().split()
    for i in range(len(A)):
        A[i] = int(A[i])
    return A

if __name__ == '__main__':
    N = int(input())
    A = InputToInt()
    B = InputToInt()

    C = 0
    for i in range(N):
        if B[i] <= A[i]:
            C += B[i]
        elif B[i] <= A[i] + A[i+1]:
            C += B[i]
            A[i+1] -= B[i] - A[i]
        else:
            C += A[i] + A[i+1]
            A[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()))

S = sum(A)

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

print(S-sum(A))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ans=0
for i in range(0,n)[::-1]:
  if a[i+1]+a[i]<=b[i]:
    ans+=a[i+1]+a[i]
    a[i+1]=0;a[i]=0
  elif a[i+1]<=b[i]<a[i]+a[i+1]:
    ans+=b[i]
    a[i]-=(b[i]-a[i+1]);a[i+1]=0
  elif a[i+1]>b[i]:
    ans+=b[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()))
s=sum(a)
for i in range(n):
    if a[i]>b[i]:a[i]-=b[i]
    else: a[i+1]=max(a[i+1]-b[i]+a[i],0);a[i]=0
print(s-sum(a))

Submission status: Accepted

Solution code:
import bisect
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline #文字列入力のときは注意

n = int(input())
a = [int(i) for i in readline().split()]
b = [int(i) for i in readline().split()] + [0]


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

Submission status: Accepted

Solution code:
#

import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
from heapq import heappush, heappop

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = 0
for i in range(N):
    b = b_list[i]
    a1 = a_list[i]
    a2 = a_list[i+1]

    if b >= a1:
        tmp = a1
        ans += tmp
        b -= tmp
        if b > 0:
            if b >= a2:
                tmp2 = a2
                ans += tmp2
                a_list[i+1] -= tmp2
            else:
                ans += b
                a_list[i+1] -= b
    else:
        ans += b

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

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

end = sum(a)
print(start-end)

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):
	b = B[i]
	a1,a2 = A[i], A[i+1]
	if b <= a1:
		ans += b
	else:
		ans += a1
		rest = b - a1
		if rest <= a2:
			ans += rest
			A[i+1] -= rest
		else:
			ans += a2
			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+=B[i]
        A[i]-=B[i]
    else:
        cnt+=A[i]
        B[i]-=A[i]
        A[i]=0
        if A[i+1]>=B[i]:
            cnt+=B[i]
            A[i+1]-=B[i]
            B[i]=0
        else:
            cnt+=A[i+1]
            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()))
ans=0
for i,B in enumerate(b):
    tmp=a[i]
    if tmp<=B:
        ans+=tmp
        B-=tmp
        if a[i+1]<=B:
            ans+=a[i+1]
            a[i+1]=0
        else:
            ans+=B
            a[i+1]-=B
    else:
        ans+=B
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

count=0

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

print(count)

Submission status: Accepted

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

# N = 10**5
# a = [0 for i in range(N+1)]
# b = [0 for i in range(N)]

cnt = 0
for i in range(N):
    num = min(a[i], b[i])
    a[i] -= num
    b[i] -= num
    cnt += num
    num2 = min(a[i+1], b[i])
    a[i+1] -= num2
    b[i] -= num2
    cnt += num2
    # print(a,b)
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))
counter = 0
for i in range(n-1, -1, -1):
    if(i < n-1):
        if(b_list[i+1] >= a_list[i+1]):
            counter += a_list[i+1]
            a_list[i+1] = 0
        else:
            counter += b_list[i+1]
            a_list[i+1] -= b_list[i+1]
    if(b_list[i] >= a_list[i+1]):
        counter += a_list[i+1]
        b_list[i] -= a_list[i+1]
    else:
        counter += b_list[i]
        b_list[i] = 0
    # print(i, counter, a_list, b_list)
# b1がa1をやるパターンが抜けてる.
counter += min(a_list[0], b_list[0])
print(counter)

Submission status: Accepted

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

Submission status: Accepted

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

monster_list = [ int(v) for v in input().split() ]
aaaa_list = [ int(v) for v in input().split() ]

tottal_battle = 0
for i in range(n):
    battle = min(aaaa_list[i],monster_list[i])
    if aaaa_list[i] >= monster_list[i]:
        monster_list[i] = 0
        aaaa_list[i] -= battle
    else:
        aaaa_list[i] = 0
        monster_list[i] -= battle
    tottal_battle += battle
    battle = min(aaaa_list[i],monster_list[i+1])
    if aaaa_list[i] >= monster_list[i+1]:
        monster_list[i+1] = 0
        aaaa_list[i] -= battle
    else:
        aaaa_list[i] = 0
        monster_list[i+1] -= battle
    tottal_battle += battle

print(tottal_battle)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#### import ####
import sys
import math
from collections import defaultdict

#### 設定 ####
sys.setrecursionlimit(10**7)
def input():
  return sys.stdin.readline()[:-1]

#### 定数 ####
mod = 10**9 + 7

#### 読み込み ####
def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
  read_all = [tuple(map(int, input().split())) for _ in range(N)]
  return map(list,zip(*read_all))

#################

N = I()
A = III()
B = III()

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

Submission status: Accepted

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

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
from heapq import heappush, heappop
from collections import deque
import itertools
from itertools import permutations
import sys
import bisect
sys.setrecursionlimit(10**6)
def MI():
    return map(int,input().split())
def I():
    return int(input())
def LI():
    return [int(i) for i in input().split()]
YN=['Yes','No']
YNeos='YNeos'
mo=10**9+7
imp='IMPOSSIBLE'


n=I()
a=LI()[::-1]
b=LI()[::-1]
ans=0
for i in range(n):
    d=min(a[i],b[i])
    ans+=d
    d2=min(a[i+1],b[i]-d)
    a[i+1]-=d2
    ans+=d2


print(ans)

Submission status: Accepted

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

    res += kill + kill2

print(res)

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=[B[i] for i in range(N)]
X=[A[i] for i in range(N+1)]
for i in range(N):
    if X[i]<C[i]:
        C[i]-=X[i]
        X[i]=0
        X[i+1]=max(0,X[i+1]-C[i])
    else:
        X[i]-=C[i]
        C[i]=0
print(sum(A)-sum(X))

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N = int(input())
lstA = [int(s) for s in input().split()]
lstB = [int(s) for s in input().split()]
count=0
for i in range(N):
  ##まず前半を倒す
  pow_i=min([lstA[i],lstB[i]])
  count=count+pow_i
  power_rest=lstB[i]-pow_i
  ###後半を倒す
  pow_isuc=min([lstA[i+1],power_rest])
  count=count+pow_isuc
  lstA[i+1]=lstA[i+1]-pow_isuc
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]:
        a[i] -= b[i]
    else:
        if b[i] - a[i] <= a[i+1]:
            a[i+1] -= b[i] - a[i]
        else:
            a[i+1] = 0
        a[i] = 0



print(s - sum(a))

Submission status: Accepted

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

Submission status: Accepted

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

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

print(D)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

res = 0
for i in range(N):
    b = b_list[i]
    if b >= a_list[i]:
        b -= a_list[i]
        res += a_list[i]
        a_list[i] = 0
    else:
        res += b
        a_list[i] -= b
        b = 0

    if b >= a_list[i+1]:
        b -= a_list[i+1]
        res += a_list[i+1]
        a_list[i+1] = 0
    else:
        res += b
        a_list[i+1] -= b
        b = 0
print(res)

Submission status: Accepted

Solution code:
#import sys
#input = sys.stdin.readline
def main():
    N = int( input())
    A = list( map( int, input().split()))
    B = list( map( int, input().split()))
    ans = 0
    for i in range(N):
        if B[i] > A[i]:
            B[i] -= A[i]
            ans += A[i]
        else:
            ans += B[i]
            B[i] = 0
        if B[i] > A[i+1]:
            ans += A[i+1]
            A[i+1] = 0
        else:
            A[i+1] -= B[i]
            ans += B[i]
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(300000)


def solve(N: int, A: "List[int]", B: "List[int]"):
    ret = 0
    for i in range(N):
        if A[i] <= B[i]:
            B[i] -= A[i]
            ret += A[i]
            tmp = min(A[i + 1], B[i])
            #print(B[i], tmp)
            A[i + 1] -= tmp
            ret += tmp
        else:
            A[i] -= B[i]
            ret += B[i]
    print(ret)
    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()

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

Submission status: Accepted

Solution code:



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


tot=0

for i in range(n):
	kl=min(a[i],b[i])

	b[i]-=kl
	tot+=kl
	a[i]-=kl

	kl=min(a[i+1],b[i])
	b[i]-=kl
	tot+=kl
	a[i+1]-=kl

print(tot)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
n=int(input())
a_list=list(map(int,input().split()))
b_list=list(map(int,input().split()))
ans=0
for i in range(n):
    if b_list[i]<=a_list[i]:
        ans+=b_list[i]
    else:
        ans+=a_list[i]
        b=b_list[i]-a_list[i]
        if b<=a_list[i+1]:
            ans+=b
            a_list[i+1]-=b
        else:
            ans+=a_list[i+1]
            a_list[i+1]=0
print(ans)

Submission status: Accepted

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


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

    n = 0
    for i in range(len(B)):
        n1 = min(A[i],B[i])
        n += n1
        B[i] -= n1
        n2 = min(A[i+1],B[i])
        n += n2
        A[i+1] -= n2
    print(n)
    return


# Generated by 1.1.4 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 = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

res = 0
for n in range(N):
    b = min(A[n], B[n])
    B[n] -= b
    b2 = min(A[n + 1], B[n])
    A[n + 1] -= b2
    res += b + b2

print(res)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
from __future__ import division, print_function

import os
import sys
from io import BytesIO, IOBase

if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip


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

    td = 0

    for i, j in enumerate(b):
        mi = min(a[i], j)
        td += mi + min(j - mi, a[i + 1])
        a[i] -= mi
        a[i + 1] -= min(j - mi, a[i + 1])

    print(td)


# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

Submission status: Accepted

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

ans = 0
for i in range(N):
    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)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()
B = LIST()

ans = 0
for i in range(N):
	if A[i] > B[i]:
		ans += B[i]
	else:
		ans += A[i]
		B[i] -= A[i]  # のこり
		if B[i] > A[i+1]:
			ans += A[i+1]
			A[i+1] = 0
		else:
			ans += B[i]
			A[i+1] -= B[i]
print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

ans = 0
for i, bb in enumerate(b):
    use = min(a[i], b[i])
    ans += use
    a[i] -= use
    b[i] -= use

    if b[i] == 0:
        continue

    use = min(a[i + 1], b[i])
    ans += use
    a[i + 1] -= use

print(ans)

Submission status: Accepted

Solution code:
def makelist(n, m):
    return [[0 for _ in range(m)] for _ in range(n)]

# n
# a, b
# s = input()


N = int(input())
A = [0] + list(map(int, input().split()))
B = [0] + list(map(int, input().split()))

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

Submission status: Accepted

Solution code:
import sys


fin = sys.stdin.readline
N = int(fin())
A_list = [int(elem) for elem in fin().split()]
B_list = [int(elem) for elem in fin().split()]
max_num_beaten = 0
for i, B in enumerate(B_list):
    cur_power = B
    A0 = A_list[i]
    num_beaten = min(A0, cur_power)
    A_list[i] -= num_beaten
    max_num_beaten += num_beaten
    cur_power -= num_beaten

    A1 = A_list[i + 1]
    num_beaten = min(A1, cur_power)
    A_list[i + 1] -= num_beaten
    max_num_beaten += num_beaten

print(max_num_beaten)