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 = list(map(int, input().split()))
b = list(map(int, input().split()))

ans = 0
b_i = 0
t = 1
for i in range(len(b)):
    t = 1
    while t == 1:
        num = a[i] - b[i]
        if num >= 0:
            ans = ans + b[i]
            num = 0
            break
        else:
            ans = ans + a[i]
            num = abs(num)
            b_i = num
            num = a[i+1] - b_i

            if num <= 0:
                ans = ans + a[i+1]
                a[i+1] = 0
                t = 0
            else:
                ans = ans + b_i
                a[i+1] = num
                t = 0

print(ans)

Submission status: Accepted

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

B.append(0)
C=[0]*(N+1)

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

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-



N = int(input())
An = list(map(int, input().split()))
Bn = list(map(int, input().split()))

ret = 0
for i in range(N):
  ret += min([An[i]+An[i+1],Bn[i]])
  An[i+1] = max([An[i+1]-max([Bn[i]-An[i],0]),0])
print(ret)

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

Submission status: Accepted

Solution code:
def getInt(): return int(input())

def getIntList(): return [int(x) for x in input().split()]

N = getInt()
A = getIntList()
B = getIntList()
sum = 0
sum += min(A[0],B[0])
f = min(A[1],B[0]-min(A[0],B[0]))
sum += f
for i in range(N-1):
    sum += min(A[i+1]-f,B[i+1])
    f = min(A[i+2],B[i+1]-min(A[i+1]-f,B[i+1]))
    sum += f
print(sum)

Submission status: Accepted

Solution code:
n = int(input())
monster = list(map(int, input().split()))
hero = list(map(int, input().split()))
hero.append(0)
rest = 0
score = 0
for m, h in zip(monster, hero):
  x = min(m, rest)
  score += x
  m -= x
  if h < m:
    score += h
    rest = 0
    continue
  score += m
  rest = h - m
  
print(score)

Submission status: Accepted

Solution code:
n = int(input())
list_a = []
list_b = []
list_a = list(map(int,input().split()))
list_b = list(map(int,input().split()))

x = 0
a = list_a[n]
for i in range(n):
    ii = n - i -1
    if list_a[ii] + a <= list_b[ii]:
        x += list_a[ii] + a
        a = 0
    elif a <= list_b[ii]:
        a = list_a[ii] + a - list_b[ii]
        x += list_b[ii]
    else:
        a = list_a[ii]
        x += list_b[ii]

print(x)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
s=0
y=0
for i in range(n):
    v=max(0,a[i]-y)
    if v<=b[i]:
        s+=a[i]
        y=b[i]-v
    elif v>b[i]:
        s+=b[i]+y
        y=0
print(s+min(a[-1],y))

Submission status: Accepted

Solution code:
_,a,b=open(0)
a=list(map(int,a.split()))
b=list(map(int,b.split()))
c=sum(a)
for i in range(len(b)):
	if a[i]>b[i]:
		a[i]-=b[i]
	else:
		a[i+1]-=(b[i]-a[i])
		a[i+1]=0if a[i+1]<0else a[i+1]
		a[i]=0
print(c-sum(a))

Submission status: Accepted

Solution code:
num_lines = int(input())
if num_lines<1 or num_lines>10**5:
	print ('Illegal')
	exit()
#print(num_lines)
a=list(map(int,input().split(" ")))
b=list(map(int,input().split(" ")))

if len(a) !=num_lines +1 or len(b)!=num_lines:
	print ('Illegal')
	exit()

#print (a)

s=0
r=0
for i in range(num_lines):
	x=min(a[i]-r,b[i])
	s+=x
	r=0
	if(x<b[i]):
		y=min(a[i+1],b[i]-x)
		s+=y
		r=y
print(s)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))  # N+1体のモンスター
B = list(map(int, input().split()))  # N人の勇者

before_A = sum(A)
# 一つ目の街の処理を先に行う
if A[0] >= B[0]:  # モンスターの数の多いなら
    A[0] -= B[0]
    B[0] = 0
else:  # 勇者のキャパシティの方が大きいなら
    B[0] -= A[0]
    A[0] = 0

# i番目の街について処理を行なっていく
for i in range(1, N):
    if B[i-1] > 0:  # まだ以前の勇者にキャパが残ってるなら
        tmp = min(A[i], B[i-1])
        A[i] -= tmp
        B[i-1] -= tmp

    if A[i] > 0:  # まだi番目の街にモンスターが残っているならば
        tmp = min(A[i], B[i])
        A[i] -= tmp
        B[i] -= tmp

if (A[-1] > 0) & (B[-1] > 0):
    tmp = min(A[-1], B[-1])
    A[-1] -= tmp
    B[-1] -= tmp

after_A = sum(A)
# 
# print(A)
# print(B)

print(before_A - after_A)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

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

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

for i in range(n):
    if b_stock <= a[i]:
        gekitai += b_stock
        a[i] -= b_stock
        b_stock = 0
    else:
        gekitai += a[i]
        b_stock = 0
        a[i] = 0

    if b[i] <= a[i]:
        gekitai += b[i]

    else:
        gekitai += a[i]
        b_stock += b[i] - a[i]


if b_stock <= a[n]:
    gekitai += b_stock
else:
    gekitai += a[n]


print(gekitai)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from functools import reduce

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

# 小さい番号の勇者から順に小さい番号の街のモンスターを優先して倒す
ans, _ = reduce(
    lambda acc, i: (
        acc[0] + min(acc[1] + A[i + 1], B[i]),
        max(0, A[i + 1] - max(0, B[i] - acc[1]))
    ),
    range(N),
    (0, A[0])
)

print(ans)

Submission status: Accepted

Solution code:
import copy

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

ans = 0

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

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

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

Submission status: Accepted

Solution code:
N = int( input() )
A, B = list( map(lambda x: int(x), input().split(" ")) ), list( map(lambda x: int(x), input().split(" ")) )
#A = monsters. B = number of to be able to kill monsters.殺せる数

 
def kill_monsters(Ai, Bi):

    Ai -= Bi
    Bi = 0

    if Ai < 0:
        Bi += -1*Ai
        Ai = 0

    return (Ai, Bi)
 
def main(local_A, local_B, lim):
 
    for i in range(lim):
        local_A[i], local_B[i] = kill_monsters(local_A[i], local_B[i])
 
        if 0 < local_B[i]:
            local_A[i+1], local_B[i] = kill_monsters(local_A[i+1], local_B[i])
 
    return [local_A, local_B]

print( sum(A) - sum(main(A, B, N)[0]) )

Submission status: Accepted

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

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

Submission status: Accepted

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

def main():
	n = int(input().strip())
	a = list(map(int, input().strip().split()))
	b = list(map(int, input().strip().split()))
	s = 0
	for i in range(n):
		l = min(a[i], b[i])
		a[i] -= l
		b[i] -= l
		r = min(a[i+1], b[i])
		a[i+1] -= r
		b[i] -= r
		s += l + r
	print(s)

if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
num_input = input()
listA_input = input().split(" ")
listB_input = input().split(" ")
N = int(num_input)
allkenemies = 0
A = []
B = []
for n_A in listA_input:
  A.append(int(n_A))
for n_B in listB_input:
  B.append(int(n_B))
for num in range(0,N):
  kenemies = min(A[num],B[num])
  allkenemies += kenemies
  A[num] -= kenemies
  B[num] -= kenemies
  kenemies = min(A[num+1],B[num])
  allkenemies += kenemies
  A[num+1] -= kenemies
print(allkenemies)

Submission status: Accepted

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

Submission status: Accepted

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


def main():
    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
        b[i] -= left

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
R = lambda: map(int, input().split())
input()
a = list(R())
def f():
    s = 0
    for x, y, z in zip(R(), a, a[1:]):
        t = min(x, y - s)
        s = min(z, x - t)
        yield s + t
print(sum(f()))

Submission status: Accepted

Solution code:
R = lambda: map(int, input().split())
input()
a = list(R())
def f():
    y = 0
    for u, v, w in zip(R(), a, a[1:]):
        x = min(v - y, u)
        y = min(w, u - x)
        yield x + y
print(sum(f()))

Submission status: Accepted

Solution code:
R = lambda: map(int, input().split())
input()
a = list(R())
r = y = 0
for u, v, w in zip(R(), a, a[1:]):
    x = min(v - y, u)
    y = min(w, u - x)
    r += x + y
print(r)

Submission status: Accepted

Solution code:
from itertools import tee

R = lambda: map(int, input().split())
input()
(a, b), c = tee(R()), R()
next(b)
r = y = 0
for u, v, w in zip(c, a, b):
    x = min(v - y, u)
    y = min(w, u - x)
    r += x + y
print(r)

Submission status: Accepted

Solution code:
R = lambda: map(int, input().split())
input()
a, b = list(R()), R()
r = y = 0
for u, v, w in zip(b, a, a[1:]):
    x = min(v - y, u)
    y = min(w, u - x)
    r += x + y
print(r)

Submission status: Accepted

Solution code:
R = lambda: map(int, input().split())
input()
a, b = list(R()), R()
r = 0
for i, u in enumerate(b):
    x = min(a[i], u)
    y = min(a[i + 1], u - x)
    a[i + 1] -= y
    r += x + y
print(r)

Submission status: Accepted

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

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]
        yo = b[i]-a[i]

        if a[i+1]<= yo:
            ans += a[i+1]
            a[i+1]=0
        else:
            ans += yo
            a[i+1] -= yo
          
print(ans)

Submission status: Accepted

Solution code:

n=int(input())
mns=list(map(int,input().split()))
hrs=list(map(int,input().split()))
mk=0
for i in range(n):
  if(hrs[i]>=mns[i]+mns[i+1]):
    mk+=mns[i]+mns[i+1]
    mns[i]=0
    mns[i+1]=0
  else:
    if(hrs[i]<=mns[i]):
      mk+=hrs[i]
    else:
      mk+=hrs[i]
      mns[i+1]-=(hrs[i]-mns[i])
print(mk)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# ABC 135, C

n = int(input())
A = [int(el) for el in input().split(' ')]
B = [int(el) for el in input().split(' ')]

total = 0
for i in range(n):
    if i == 0:
        f = min(A[0], B[0])
        g = min(A[1], B[0]-f)
        total += f + g
    else:
        f = min(A[i] - g, B[i])
        g = min(A[i+1], B[i] - f)
        total += f + g



print(total)

Submission status: Accepted

Solution code:
N = int(input())
m = list(map(int, input().split()))
b = list(map(int, input().split()))
s = 0
for i in range(N):
  l = min(m[i],b[i])
  s += l
  m[i] -= l
  b[i] -= l
  
  r = min(m[i+1],b[i])
  s += r
  m[i+1] -= r
print(s)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
class ABC135:
    def __init__(self):
        pass

    def solve(self, N):
        ans = 0
        A = list(map(int, input().split()))
        B = list(map(int, input().split()))
        for i in range(len(B)):
            diff = min(A[i], B[i]);
            A[i] -= diff
            B[i] -= diff
            ans += diff
            diff = min(A[i+1], B[i]);
            A[i+1] -= diff
            B[i] -= diff
            ans += diff
        print(ans)

if __name__ == '__main__':
    N = int(input())
    problem = ABC135()
    problem.solve(N)

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

    tmp = 0
    ans = 0
    for i in range(n):
        mon = max(0, a[i] - tmp)

        if b[i] <= mon:
            ans += b[i] + tmp
            tmp = 0
        else:
            ans += a[i]
            tmp = b[i] - mon
            
    if tmp <= a[i + 1]:
        ans += tmp
    else:
        ans += a[i + 1]
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
monsters = list(map(int, input().split()))
savers = list(map(int, input().split()))
ko = 0

for i in range(n):
    left = min(monsters[i], savers[i])
    ko += left
    monsters[i] -= left
    savers[i] -= left

    right = min(monsters[i+1], savers[i])
    ko += right
    monsters[i+1] -= right
    savers[i] -= right

print(ko)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
enemy = list(map(int, input().split()))
beat = list(map(int, input().split()))
ans = 0 
for i in range(n):
  #左を全力で倒す
  left = min([enemy[i], beat[i]])
  ans += left
  beat[i] -= left
  
  right = min([enemy[i+1], beat[i]])
  ans += right
  enemy[i + 1] -= right
  beat[i] -= right
  
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
enemy = list(map(int, input().split()))
beat = list(map(int, input().split()))
ans = 0 
for i in range(n):
  #左を全力で倒す
  left = min([enemy[i], beat[i]])
  ans += left
  enemy[i] -= left
  beat[i] -= left
  
  right = min([enemy[i+1], beat[i]])
  ans += right
  enemy[i + 1] -= right
  beat[i] -= right
  
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

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

ans = 0
for i in range(len(hero)):
	if mon[i]+mon[i+1] >= hero[i]:
		ans += hero[i]
		mon[i+1] = min(mon[i+1],mon[i]+mon[i+1]-hero[i])
	else:
		ans += mon[i]+mon[i+1]
		mon[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()))

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

print(z)

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

print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
M = list(map(int, input().split()))
H = list(map(int, input().split()))

sum_m = 0
rest = 0
for i in range(N):
  if M[i] >= H[i]+rest:
    sum_m += H[i]+rest
    rest = 0
  else:
    sum_m += M[i]
    rest = min(H[i], H[i]+rest-M[i])
sum_m += min(rest, M[-1])

print(sum_m)

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:
N = int(input())
M = list(map(int, input().split()))
H = list(map(int, input().split()))

sum_m = 0
killed = 0
rest = 0
for i in range(0, N):
  killed = min(H[i]+rest, M[i])
  rest = min(H[i]+rest-killed, H[i])
  sum_m += killed
sum_m += min(rest, M[-1])
print(sum_m)

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,-1,-1):
    # i+1の討伐
    if a[i+1] < b[i]:
        count += a[i+1]
        b[i] -= a[i+1]
        a[i+1] = 0
    else:
        count += b[i]
        a[i+1] -= b[i]
        b[i] = 0
    # iの討伐
    if a[i] < b[i]:
        count += a[i]
        b[i] -= a[i]
        a[i] = 0
    else:
        count += b[i]
        a[i] -= b[i]
        b[i] = 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-1,-1,-1):
    # i+1の討伐
    if a[i+1] < b[i]:
        count += a[i+1]
        b[i] -= a[i+1]
        a[i+1] = 0
    else:
        count += b[i]
        a[i+1] -= b[i]
        b[i] = 0
    # iの討伐
    if a[i] < b[i]:
        count += a[i]
        b[i] -= a[i]
        a[i] = 0
    else:
        count += b[i]
        a[i] -= b[i]
        b[i] = 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 += a[i]
        b[i] -= a[i]
        a[i] = 0
    else:
        count += b[i]
        a[i] -= b[i]
        b[i] = 0
    if a[i+1] < b[i]:
        count += a[i+1]
        b[i] -= a[i+1]
        a[i+1] = 0
    else:
        count += b[i]
        a[i+1] -= b[i]
        b[i] = 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

def f(n):
    nn = min(a[n], b[n])
    nnn = min(a[n+1], b[n] - nn)
    a[n+1] -= nnn
    return nn + nnn

for i in range(n):
    ans += f(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()]

k=0
for i,j in enumerate(b):
  if a[i]+a[i+1]<=j:
    k+=a[i]+a[i+1]
    a[i],a[i+1]=0,0
  elif a[i]<=j:
    k+=j
    a[i],a[i+1]=0,a[i]+a[i+1]-j
  else:
    k+=j
    a[i]-=j
print(k)

Submission status: Accepted

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

initial = sum(A)
for i in range(1,N+1):
  diff1 = B[-i] - A[-i]
  if (diff1 >= 0):
    A[-i] = 0
    B[-i] = diff1
  else:
    A[-i] = - diff1
    B[-i] = 0
    
  diff2 = B[-i] - A[-i-1]
  if (diff2 >= 0):
    A[-i-1] = 0
    B[-i] = diff2
  else:
    A[-i-1] = - diff2
    B[-i] = 0
    
print(initial - sum(A))

Submission status: Accepted

Solution code:
N = int(input())
M = list(map(int, input().split()))
H = list(map(int, input().split()))

sum_m = 0
killed = 0
rest = 0
for i in range(0, N):
  killed = min(H[i]+rest, M[i])
  rest = max(0, min(H[i]+rest-killed, H[i]))
  sum_m += killed
sum_m += min(rest, M[-1])
print(sum_m)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def main():
    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)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))

ans = 0
tmp = 0
for a,b in zip(As,Bs[:N]):
    # 前の余力で敵を頃す
    if tmp > 0:
        if a >= tmp:
            ans += tmp
            a -= tmp
        else:
            ans += a
            a = 0
    # 町から敵を頃す
    if a >= b:
        ans += b
        tmp = 0
    else:
        ans += a
        tmp = b-a
# 最後の町
a = As[N]
if a >= tmp:
    ans += tmp
else:
    ans += a

print(ans)

Submission status: Accepted

Solution code:
# n,m = map(int,input().split())
n = int(input())
# n = input()
a = list(map(int,input().split()))
b = list(map(int,input().split()))
cnt = 0
left = 0
for i in range(n):
    a1,b1 = a[i],b[i]
    if a1 <= b1 + left:
        cnt += a1
        left = min(b1 - a1 + left,b1)
    else:
        cnt += b1 + left
        left = 0
if a[n] <= left:
    cnt += a[n]
else:
    cnt += left
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
ans += min(a[0], b[0])
b[0] = max(0,b[0]-a[0])
ans += min(a[-1], b[-1])
b[-1] = max(0,b[-1]-a[-1])
i1 = 0
i2 = b[0]
for i in range(n-1):
  i1 = i2
  i2 = b[i+1]
  ai = a[i+1]
  if ai <= i1:
    ans += ai
  elif ai <= i1 + i2:
    ans += ai
    i2 -= ai - i1
  else:
    ans += i1 + i2
    i2 = 0
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))

sumA = sum(As)

for i, B in enumerate(Bs):
    d = min(As[i], B)
    As[i] -= d
    B -= d
    d = min(As[i+1], B)
    As[i+1] -= d
    B -= d

print(sumA - sum(As))

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

print(ans)

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
c=0
for i in range(2*n):j=i*-n%len(a);m=min(a[j],a[j-n]);c+=m;a[j-n]-=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):
    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:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
count = 0
flag = 0
i = 0
while(i<len(a)-1):
	if b[i]<a[i]+a[i+1]:
		count = count+b[i]
		if a[i]<b[i]:
			a[i+1] = a[i]+a[i+1]-b[i]
		flag = 0
		i += 1

	else:
		if flag == 0:
			count = count+a[i]+a[i+1]
			a[i+1] = 0
			flag = 1
			i = i + 1

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

print(count)

Submission status: Accepted

Solution code:
n,x,*a=map(int,open(0).read().split())
c=0
for a,b in zip(a,a[n:]):c+=min(x+a,b);x=a-max(min(b-x,a),0)
print(c)

Submission status: Accepted

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

A=list(reversed(A))
B=list(reversed(B))
cnt=0
tmp=0
for i in range(N):
    if A[i]>=B[i]:
        cnt+=B[i]
        tmp=0
    else:
        cnt+=A[i]
        tmp=min(B[i]-A[i],A[i+1])
        cnt+=tmp
        A[i+1]-=tmp
print(cnt)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys

raw_input = sys.stdin.readline

f = lambda : raw_input().rstrip()    
f1 = lambda : f().split()
f2 = lambda t: map(t,f1())

def main1():
    a,b = f2(int)
    if (a+b)%2 == 0:
       print (a+b)/2
    elif a == b:
       print 0
    else:
       print "IMPOSSIBLE"

def main2():
    n = int(f())
    a = f2(int)
    b = sorted(a)
    m = 0
    for i in xrange(n):
        if a[i] != b[i]:
            m = m + 1
    if m<=2:
        print "YES"
    else:
        print "NO"

def main3():
    n = int(f())
    a = f2(int)
    b = f2(int)
    ans = 0
    for i in xrange(n):
        if a[i] >= b[i]:
            ans = ans + b[i]
        elif a[i] < b[i] :
            ans = ans + a[i]
            b[i] = b[i] - a[i]
            ans = ans + min(a[i+1], b[i])
            a[i+1] = max(0,a[i+1] - b[i])  
    print ans 
 
def main4():
    pass
    
    
if __name__ == "__main__":
    main3()

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

Submission status: Accepted

Solution code:
def c_city_savers(N, A, B):
    ans = 0
    for i in range(N):
        t1 = min(A[i], B[i])  # 勇者iが街iで最大限倒せるモンスターの数
        B[i] -= t1  # 倒した分、街i+1で倒せるモンスターの数が減る
        t2 = min(A[i + 1], B[i])  # 勇者iが街i+1で最大限倒せるモンスターの数
        A[i + 1] -= t2
        ans += t1 + t2
    return ans

N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
print(c_city_savers(N, A, B))

Submission status: Accepted

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