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:
#!/usr/bin/env python

N = input()
N = int(N)

A = []
A = input().split()
A = [int(n) for n in A]
#A.append(0)

B = []
B = input().split()
B = [int(n) for n in B]
B.append(0)
#B.append(0)

#print('N=',n)
#print('A=',A)
#print('B=',B)

monster = 0

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

print(monster)

Submission status: Accepted

Solution code:
N=int(input())
A=[int(i) for i in input().split()]
C=[int(i) for i in input().split()]
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()))

bfr = 0
ans = 0

for i in range(2*n):
  idx = i//2
  if(i%2 == 0):
    nxt = min(a[idx]-bfr, b[idx])
  else:
    nxt = min(b[idx]-bfr, a[idx+1])
  ans += nxt
  bfr = nxt
  
print(ans)

Submission status: Accepted

Solution code:
# ABC135C - City Savers
# greedy algorithm
def main():
    n = int(input())
    A = tuple(map(int, input().split()))
    B = tuple(map(int, input().split()))
    ans, a1 = 0, A[0]
    for a2, b in zip(A[1:], B):
        cnt1 = a1 if a1 < b else b
        x = b - cnt1
        cnt2 = a2 if a2 < x else x
        ans += cnt1 + cnt2
        a1 = a2 - cnt2  # a2 becomes a1 in next turn
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC135C - City Savers
# greedy algorithm
def main():
    n = int(input())
    A = tuple(map(int, input().split()))
    B = tuple(map(int, input().split()))
    ans, a1 = 0, A[0]
    for a2, b in zip(A[1:], B):
        cnt1 = a1 if a1 < b else b
        cnt2 = a2 if a2 < b - cnt1 else b - cnt1
        ans += cnt1 + cnt2
        a1 = a2 - cnt2  # a2 becomes a1 in next turn
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC135C - City Savers
# greedy algorithm
def main():
    n = int(input())
    A = tuple(map(int, input().split()))
    B = tuple(map(int, input().split()))
    ans, a1 = 0, A[0]
    for a2, b in zip(A[1:], B):
        cnt1 = min(a1, b)
        cnt2 = min(a2, b - cnt1)
        ans += cnt1 + cnt2
        a1 = a2 - cnt2  # a2 becomes a1 in next turn
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC135C - City Savers
# greedy algorithm
import sys
input = sys.stdin.readline

def main():
    n = int(input())
    A = tuple(map(int, input().rstrip().split()))
    B = tuple(map(int, input().rstrip().split()))
    ans, a1 = 0, A[0]
    for a2, b in zip(A[1:], B):
        cnt1 = min(a1, b)
        cnt2 = min(a2, b - cnt1)
        ans += cnt1 + cnt2
        a1 = a2 - cnt2  # a2 becomes a1 in next turn
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC135C - City Savers
# greedy algorithm
def main():
    n = int(input())
    A = tuple(map(int, input().rstrip().split()))
    B = tuple(map(int, input().rstrip().split()))
    ans, a1 = 0, A[0]
    for a2, b in zip(A[1:], B):
        cnt1 = min(a1, b)
        cnt2 = min(a2, b - cnt1)
        ans += cnt1 + cnt2
        a1 = a2 - cnt2  # a2 becomes a1 in next turn
    print(ans)


if __name__ == "__main__":
    main()

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
inpl = lambda: list(map(int,input().split()))
N = int(input())
A = inpl()
B = inpl()
ans = 0
for i in range(N):
    p = min(A[i],B[i])
    B[i] -= p
    q = min(B[i],A[i+1])
    A[i+1] -= q
    ans += p + q
print(ans)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

c = A[:]
# print(c)
point = 0
for i in range(N):
    p = min(c[i], B[i])
    c[i] -= p
    point += p
    p = min(c[i+1], B[i]-p)
    c[i+1] -= p
    point += p

print(point)

Submission status: Accepted

Solution code:
#! /usr/bin/python
# -*- encoding: utf-8 -*-

import sys

if __name__ == '__main__':
    inputN = int(input())
    inputMonster = list(map(int, input().split()))
    inputPower = list(map(int, input().split()))

    sumVictory = 0
    zanMonster = inputMonster[-1]
    
    for i in range(inputN):
        # 末尾から，力量がモンスター数をうわまった場合，モンスター数を加算
        # そうでなければ，力量を加算し，一つ後の街の残りモンスター数を記録
        if inputPower[inputN - i - 1]  >= inputMonster[inputN - i - 1] + zanMonster:
            sumVictory += inputMonster[inputN - i - 1] + zanMonster
            zanMonster = 0
        
        else:
            sumVictory += inputPower[inputN - i - 1]
            if zanMonster > inputPower[inputN - i - 1]:
                zanMonster = inputMonster[inputN - i - 1]
            else:
                zanMonster = inputMonster[inputN - i - 1] + zanMonster - inputPower[inputN - i - 1]
        #print(sumVictory)
    sys.stdout.write(str(sumVictory))

Submission status: Accepted

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

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

print(sum(A) - sum(C))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().strip().split()))
b = list(map(int, input().strip().split()))
kill_count = 0
for i in range(n):
    kill_num = min(a[i],b[i])
    kill_count += kill_num
    b[i] -= kill_num
    a[i] -= kill_num

    kill_num_ex = min(a[i+1],b[i])
    kill_count += kill_num_ex
    a[i+1] -= kill_num_ex
    b[i] -= kill_num_ex

print(kill_count)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:

def main():

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


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





if __name__== "__main__":
    main()

Submission status: Accepted

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

cnt = 0
flag = False
for i in range(n) :
  if a[i] > b[i] :
    a[i] -= b[i]
    cnt += b[i]
    b[i] = 0
  elif a[i] == b[i]:
    cnt += b[i]
    b[i] = 0
    a[i] = 0
  else :
    b[i] -= a[i]
    cnt += a[i]
    a[i] = 0
    flag = True

  if flag :
    if a[i+1] > b[i] :
      a[i+1] -= b[i]
      cnt += b[i]
      b[i] = 0
    elif a[i+1] == b[i]:
      cnt += b[i]
      b[i] = 0
      a[i+1] = 0
    else :
      b[i] -= a[i+1]
      cnt += a[i+1]
      a[i+1] = 0
    flag = False

print(cnt)

Submission status: Accepted

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import bisect

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

def LI(): return list(map(int, sys.stdin.readline().split()))

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

ans = 0

right = 0
for i in range(N):
    left = min(A[i]-right,B[i])
    ans += left
    right = min((B[i]-left),A[i+1])
    ans += 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
t=0
for i in range(n):
	aa,bb=a[i],b[i]
	if t>0:
		ans+=min(t,aa)
		aa=aa-t
		if aa<0:
			aa=0
	ans+=min(aa,bb)
	t=bb-aa
if t>0:
	ans+=min(t,a[n])
print(ans)

Submission status: Accepted

Solution code:
i1 = int(input())
i2 = input().split(' ')
i3 = input().split(' ')

w = -1
s = 0

for i in range(i1):
  ii2 = int(i2[i])
  ii3 = int(i3[i])
#  print(ii2)
#  print(ii3)

  if w > -1:
    ii2 = w

  if ii2 > ii3 :
    #引ききれた場合
    s += ii3
    w = int(i2[i+1])
  else:
    s += ii2
    w = ii3 - ii2

    iii2 = int(i2[i+1])

    if iii2 > w:
      s += w
      w = iii2 - w
    else:
      s += iii2
      w = 0

print(s)

Submission status: Accepted

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

ans = 0
for i in range(N):
    cnt0 = min(A[i], B[i])
    A[i] -= cnt0
    B[i] -= cnt0
    ans += cnt0
    cnt1 = min(A[i+1], B[i])
    A[i+1] -= cnt1
    B[i] -= cnt1
    ans += cnt1
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]):
    c+=b[i]
  elif(b[i]>=a[i]+a[i+1]):
    c+=a[i]+a[i+1]
    a[i+1]=0
  else:
    c+=b[i]
    a[i+1]=a[i+1]+a[i]-b[i]
print('{0}'.format(c))

Submission status: Accepted

Solution code:
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; ansli = []; tmpli = []; candili = []
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]#WideIntPoint
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]#HeightString
def mi(n): return [wi() for _ in range(n)]#MatrixInt
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]

n = i()
a = wi()
b = wi()
c = 0

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

ans += min(c, a[n])

print(ans)

Submission status: Accepted

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

count = 0
next_b = 0
for i, ai in enumerate(a):
  #print(ai, next_b, count)
  if i < n:
    bi = b[i]
    if ai > next_b:
      ai = ai - next_b
      count += next_b
    else:
      count += ai
      ai = 0
      
    if ai < bi:
      count += ai
      next_b = bi -ai
    else:
      count += bi
      next_b = 0
  else:
    if ai > next_b:
      count += next_b
    else:
      count += ai
      
print(count)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
# ABC135C - City Savers
# greedy algorithm
def main():
    n = int(input())
    A = list(map(int, input().rstrip().split()))
    B = tuple(map(int, input().rstrip().split()))
    ans = 0
    for i in range(n):
        cur1 = min(A[i], B[i])
        cur2 = min(A[i + 1], B[i] - cur1)
        A[i] -= cur1
        A[i + 1] -= cur2
        ans += cur1 + cur2
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
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:
        enemies = a[i] + a[i+1]
        if enemies >= b[i]:
            ans += b[i]
            a[i+1] = a[i] + a[i+1] - b[i]
        else:
            ans += enemies
            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()))
b.append(0)
sum1=0
rest=0
for i in range(N+1):
  x=min(a[i],b[i]+rest)
  sum1+=x
  a[i]-=x
  b[i]=min(b[i],b[i]+rest-x)
  if a[i]==0:
    rest=b[i]
  if b[i]==0:
    rest=0

print(sum1)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split())) # N + 1
B = list(map(int, input().split())) # N
 
# 重みがないのですべて小さい方から倒せると仮定する
cnt = 0
for i in range(N):
    cnt += min(B[i], A[i])
    diff = B[i] - A[i]
    if diff > 0:
        if A[i+1] < diff:
            cnt += A[i+1]
            A[i+1] = 0
        else:
            cnt += diff
            A[i+1] -= diff
        #A[i] = 0
    #else:
    #    A[i] -= B[i]
#print("A",A)
#print("B",B)
print(cnt)

Submission status: Accepted

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

# 重みがないのですべて小さい方から倒せると仮定する
cnt = 0
for i in range(N):
    cnt += min(B[i], A[i])
    diff = B[i] - A[i]
    if diff > 0:
        if A[i+1] < diff:
            cnt += A[i+1]
            A[i+1] = 0
        else:
            cnt += diff
            A[i+1] -= diff
        #A[i] = 0
    #else:
    #    A[i] -= B[i]
#print("A",A)
#print("B",B)
print(cnt)

Submission status: Accepted

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

monster = 0
addPower = 0

for i in range(N):
    #print(A[i],'+', A[i+1], ' vs ', B[i], end = ' => ')
    AA = A[i] + A[i+1]
    BA = B[i] - A[i]
    BAA= B[i] - AA
    if BAA >= 0:
        #print(':', end='')
        monster += AA
        A[i+1] -= min(A[i+1], BA)
    elif BA > 0:
        #print('%', end='')
        monster += B[i]
        A[i+1] -= min(A[i+1], BA)
    else:
        #print('@', end='')
        monster += B[i]
print(monster)

Submission status: Accepted

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


nmon = 0
c = 0
for i in range(n):
	#b[i] = b[i] + c
	if c > a[i]:
		nmon += a[i]
		c = b[i]
		continue
	elif b[i] + c > a[i]:
		nmon += a[i]
		c = b[i] + c - a[i]
	else:
		nmon += b[i]+c
		c = 0
if a[n] >= c:
	nmon += c
else:
	nmon += a[n]
print(nmon)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
monsters = list(map(int, input().split()))
heros = list(map(int, input().split()))

killed = 0
attack = 0
attack_l = [0, heros[0]]
for i in range(0, n+1):
  if i != n and i != 0:
    attack_l[1] = heros[i]
  attack = attack_l[0] + attack_l[1]
  if (monsters[i] - attack) < 0:
    killed += monsters[i]
    if (attack_l[0] - monsters[i]) < 0:
      attack_l[1] += (attack_l[0] - monsters[i])
    attack_l[0] = attack_l[1]
    attack_l[1] = 0
  elif (monsters[i] - attack) > 0:
    killed += attack
    attack_l[0] = 0
    attack_l[1] = 0
  elif (monsters[i] - attack) == 0:
    killed += monsters[i]
    attack_l[0] = 0
    attack_l[1] = 0

print(killed)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input()) + 1

A = input().split()

B = input().split()

enemy = np.zeros(N)
saver = np.zeros(N)

beated = 0
over = 0

for i in range(N - 1):
    enemy[i] = int(A[N - i - 1])
    saver[i] = int(B[N - i - 2])

enemy[N - 1] = int(A[0])

for i in range(N):
    if(enemy[i] - over > saver[i]):
        beated = beated + saver[i] + over
        over = 0
    else:
        beated = beated + enemy[i]
        if(over > enemy[i]):
            over = saver[i]
        else:
            over = saver[i] + over - enemy[i]

print(int(beated))

Submission status: Accepted

Solution code:
N = int(input())
teki = input().split()
yusya = input().split()
i = 0
taoshita = 0

while i < N :
  if int(teki[i]) >= int(yusya[i]):
    a = int(yusya[i]) 
    b = 0
  else:
    a = int(teki[i])
    yusya[i] = int(yusya[i]) - int(teki[i])
    if int(teki[i+1]) >= yusya[i]:
      b = yusya[i]
      teki[i+1] = int(teki[i+1]) - yusya[i]
    else:
      b = int(teki[i+1])
      teki[i+1] = 0
  taoshita += a + b
  i += 1
  
print(taoshita)

Submission status: Accepted

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

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

    if a[-2-i] - b[-1-i] >= 0:
        a[-2-i] = a[-2-i] - b[-1-i]
    else:
        a[-2-i] = 0

print(sum_a - sum(a))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

mons = [int(i) for i in input().split()]
yuusha = [int(i) for i in input().split()]

taoshita = 0
#taoshinokoshi = mons[:]
for i in range(len(yuusha)):
    if mons[i] > yuusha[i]:
        taoshita = taoshita + yuusha[i]
        mons[i] = mons[i] - yuusha[i]
        yuusha[i] = 0
    else:

        if mons[i] + mons[i+1] < yuusha[i]:
            taoshita = taoshita + mons[i] + mons[i+1]
            mons[i+1] = 0

        else:
            if mons[i] > yuusha[i]:
                taoshita = taoshita + yuusha[i]
            else:
                taoshita = taoshita + yuusha[i]
                mons[i+1] = mons[i+1] - (yuusha[i] - mons[i])


print(taoshita)

Submission status: Accepted

Solution code:
inf = 10 ** 18
P = 10 ** 9 + 7

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

A_all = sum(A)

xi = min(A[0], B[0])
A[0] -= xi
B[0] -= xi
for i in range(N):
    yi = min(B[i], A[i + 1])
    A[1 + i] -= yi
    B[i] -= yi
    #
    if i < N - 1:
        xi = min(A[1 + i], B[1 + i])
        A[1 + i] -= xi
        B[1 + i] -= xi
    #
    # print("#", A, B, A_all - sum(A))
    
A_r = list(reversed(A))
B_r = list(reversed(B))


xi = min(A_r[0], B_r[0])
A_r[0] -= xi
B_r[0] -= xi
for i in range(N):
    yi = min(B_r[i], A_r[i + 1])
    A_r[1 + i] -= yi
    B_r[i] -= yi
    #
    if i < N - 1:
        xi = min(A_r[1 + i], B_r[1 + i])
        A_r[1 + i] -= xi
        B_r[1 + i] -= xi
    #
    # print("#", A, B, A_all - sum(A))

print(A_all - sum(A))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

sum = 0


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

print(sum)

Submission status: Accepted

Solution code:

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

dp_town = [0]*(n+1)
dp_town[0] = min(a[0],b[0])
b[0] = b[0] - min(a[0],b[0])
for i in range(1,n):
    if b[i-1] > 0:
        dp_town[i] = min(a[i],b[i-1])
        a[i] -= b[i-1]
        b[i-1] -= min(a[i],b[i-1])
    if a[i] > 0:
        dp_town[i] += min(a[i],b[i])
        b[i] -= min(a[i],b[i])
    if i == n-1 and b[i] > 0:
        dp_town[n] += min(a[n],b[i])

print(sum(dp_town))

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- 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):
    # iの村のモンスターを処理する
    if b[i] >= a[i]:
        b[i] -= a[i]
        count += a[i]
        a[i] = 0
    else:
        a[i] -= b[i]
        count += b[i]
        b[i] = 0

    # i+1の村の処理を行う
    if b[i] > 0:
        if b[i] >= a[i+1]:
            b[i] -= a[i+1]
            count += a[i+1]
            a[i+1] = 0
        else:
            a[i+1] -= b[i]
            count += b[i]
            b[i] = 0
# モンスターの数
print(count)

Submission status: Accepted

Solution code:
"""
# 標準入力取得
## 文字列
 = sys.stdin.readline().rstrip()
 = list(sys.stdin.readline().rstrip())

## 数値
 = int(sys.stdin.readline())
 = map(int, sys.stdin.readline().split())
 = list(map(int, sys.stdin.readline().split()))
 = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  # 二次元配列入力　二次元マップみたいな入力のとき

# 0埋め, 小数点出力桁指定時のときの出力
a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654

# 文字列をリストに格納
char_list = list("abcd") # ["a","b","c","d"]
"""

import math
import sys
import itertools
import queue
from fractions import gcd

def lcm(a, b):
    return a * b // gcd(a, b)

mod = 1000000007

if __name__ == "__main__":
    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 in range(n):
        ans += min(a[i]+a[i+1],b[i])
        if a[i] < b[i]:
            a[i+1] = a[i+1] - (b[i]-a[i])
            if a[i+1]<0:
                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()))

i = N - 1
k = 0

while i >= 0:

    if b[i] <= a[i+1]:
        k += b[i]
    elif a[i+1] < b[i] <= a[i] + a[i+1]:
        k += b[i]
        a[i] = a[i] + a[i+1] - b[i]
    else:
        k += a[i] + a[i+1]
        a[i] = 0

    i -= 1

print(k)

Submission status: Accepted

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

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):
  if a_list[i]+a_list[i+1] <= b_list[i]:
    count += a_list[i]+a_list[i+1]
    a_list[i+1] = 0
  else:
    count += b_list[i]
    if a_list[i] < b_list[i]:
      a_list[i+1] = a_list[i]+a_list[i+1] - b_list[i]
    

print(count)

Submission status: Accepted

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

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



print(str(ans))

Submission status: Accepted

Solution code:

# 2 5 1 7 3
# 4 2 2 5
#   2 
# 1 4 1 
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
b.append(0)
kuriage = 0
total = 0
for i in range(n+1):
        # print(str(i)+'ばんめ')
        # if a[i] <= b[i]:
        #         print('if'+str(1))
        #         kuriage = b[i] - a[i]
        #         # b[i] = b[i]+kuriage
        #         total += a[i]
        #         print(kuriage)
        #         print(total)
        if kuriage+b[i] >= a[i]:
                # print('if1')
                if a[i] >= kuriage:
                        res = a[i] - kuriage
                else:
                        res = 0
                kuriage = b[i] - res
                # b[i] = b[i]+kuriage
                total += a[i]
                # print(kuriage)
        elif kuriage+b[i] < a[i]:
                # print('if2')
                # total += a[i] - (kuriage+b[i])
                total += kuriage+b[i]
                kuriage = 0
                # print(kuriage)
print(total)

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
input = sys.stdin.readline

n = int(input())

monster = list(map(int,input().split()))
yusha = list(map(int,input().split()))
kill = 0
overkill = 0
for i in range(len(yusha)):
    #i番目の町、モンスターの方が多いなら勇者の数だけ倒す
    if monster[i] >= yusha[i]:
        kill += yusha[i]
    else:
        kill += monster[i]
        overkill = yusha[i] - monster[i]
        if monster[i+1] >= overkill:
            kill += overkill
            monster[i+1] -= overkill
        else:
            kill += monster[i+1]
            monster[i+1] = 0
    #print(kill)
            
print(kill)

Submission status: Accepted

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

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

def main():
    ans = 0
    for i in range(N):
        if B[i] <= A[i]:
            ans += B[i]
        else:
            ans += A[i]
            B[i] = B[i] - A[i]
            if B[i] <= A[i+1]:
                ans += B[i]
                A[i+1] = A[i+1] - B[i]
            else:
                ans += A[i+1]
                A[i+1] = 0
    return ans

if __name__ == '__main__':
    print(main())

Submission status: Accepted

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

index = 0
answer = 0

for i in B:
    next = i - A[index]
    if next >= 1:
        answer += A[index]
        if A[index+1] >= next:
            answer += next
            A[index+1] -= next
        else:
            answer += A[index+1]
            A[index+1] = 0
    else:
        answer += i
    index += 1

print(answer)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a_s = list(map(int, input().split(' ')))
    b_s = list(map(int, input().split(' ')))
    count = 0
    carry_over = 0
    for i, a in enumerate(a_s):
        count += min(carry_over, a)
        a = max(a-carry_over, 0)
        b = b_s[i] if i < n else 0
        count += min(a, b)
        carry_over = max(0, b-a)
    print(count)


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
flg=0
Ttmp=0
for i in range(N):
    if flg == 1:
        Atmp=Ttmp
    else:
        Atmp=A[i]
    flg=0
    Btmp=B[i]

    if Atmp <= Btmp:
        ans+=Atmp
        Btmp-=Atmp
    else:
        ans+=Btmp
        continue
    flg=1        
    if A[i+1] <= Btmp:
        ans+=A[i+1]
        Ttmp=0
    else:
        ans+=Btmp
        Ttmp=A[i+1]-Btmp

print(ans)