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

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

Submission status: Accepted

Solution code:
n = int(input())
ml = list(map(int, input().split()))
bl = list(map(int, input().split()))

bl = [0] + bl
mod = 0
kill = 0
for i in reversed(range(n+1)):
	if ml[i] >= bl[i] + mod:
		kill += bl[i] + mod
		mod = 0
	else:
		kill += ml[i]
		if mod >= ml[i]:
			mod = bl[i]
		else:
			mod = bl[i] + mod  - ml[i] 

print(kill)

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):
    for j in range(2):
        if b_list[i] >= a_list[i+j]:
            b_list[i] -= a_list[i+j]
            count += a_list[i+j]
            a_list[i+j] = 0
            
        else:
            a_list[i+j] -= b_list[i]
            count += b_list[i]
            b_list[i] = 0
print(count)

Submission status: Accepted

Solution code:
n=int(input())
As=list(map(int,input().split()))
Bs=list(map(int,input().split()))
Bs.append(0)

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

Submission status: Accepted

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

dp = [0]*(N+1)
Rm = [0]*(N+2)
dp[1] = min(A[1]+A[2],B[1])
Rm[1] = A[1]

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solver(A,B):
    
    A_before=sum(A)
    
    
    for target in range(n+1):
    
        
        if target>0 and B[target-1]>0:#i-1にゆうしゃがのこっているとき
            if A[target]<=B[target-1]:
                B[target-1]=B[target-1]-A[target]
                A[target]=0
                continue #B[target]が0になるまで
            else:
                A[target]=A[target]-B[target-1]
                B[target-1]=0
                
                
        if target < n:        
            if A[target] >= B[target]:#iにおけるモンスターの数>=勇者の数
                A[target]=A[target]-B[target]
                B[target]=0
                continue
            else:
                B[target]=B[target]-A[target]
                A[target]=0
                
                
            
    A_after=sum(A)
    
    return A_before-A_after
    
n = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
print(solver(A,B))

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

Submission status: Accepted

Solution code:
import math
import sys
import numpy as np
from collections import defaultdict, deque, Counter 
from itertools import accumulate, product, permutations,combinations, combinations_with_replacement
from operator import itemgetter
from bisect import bisect_left,bisect
from heapq import heappop,heappush
from fractions import gcd

max_value = 1000000000
min_value = -1000000000
sys.setrecursionlimit(max_value)

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

v = 0
for i in range(N):
    if As[i] >= Bs[i]:
        v += Bs[i]
        As[i] -= Bs[i]
        Bs[i] = 0
        continue
    else:
        v += As[i]
        Bs[i] -= As[i]
        As[i] = 0
        if As[i+1] >= Bs[i]:
            v += Bs[i]
            As[i+1] -= Bs[i]
            Bs[i] = 0
        else:
            v += As[i+1]
            Bs[i] -= As[i+1]
            As[i+1] = 0
print(v)

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#ABC135 C

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
a = LI()
b = LI()
ans = 0
for i in range(n):
    if a[i] >= b[i]:
        ans += b[i]
    else:
        s = b[i] - a[i]
        ans += a[i]
        if a[i+1] - s >= 0:
            a[i+1] -= s
            ans += s
        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()))

before = sum(A)

for  n in range(N):
    t = min(A[n],B[n])
    A[n] -= t
    B[n] -= t
    A[n+1] -= min(A[n+1],B[n])
    
after = sum(A)
print(before-after)

Submission status: Accepted

Solution code:
N = int(input())
#モンスター
a = list(map(int, input().split()))
#勇者
b = list(map(int, input().split()))

#残った勇者
x=0
c=0

for n in range(0,N):
  #街n
  #倒したモンスター数
  c = c+min(b[n]+x,a[n])
  #残った勇者
  if a[n]<x:
    x=b[n]
  else:
    x=max(b[n]+x-a[n],0)
    
c = c+min(x,a[N])
#残った勇者
if a[N]<x:
  x=a[N]
else:
  x=max(x-a[N],0)

print(c)

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])
tmp = max(0, b[0]-a[0])

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

Submission status: Accepted

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

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

Submission status: Accepted

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

remained_enemies_sum = sum(A)
killed_enemies_sum = enemy_sum - remained_enemies_sum
print(killed_enemies_sum)

Submission status: Accepted

Solution code:
# ABC135A-137B.py

SELECT = "13" + "567"[0] + "ABC"[2]


if SELECT == "135A":  # Harmony

    a, b = map(int, input().split())  # Ex. (6,10)
    c = abs(a - b)  # 4
    if c % 2 == 0:  # 2
        print(c // 2 + min((a, b)))  # 8. OK.
    else:
        print("IMPOSSIBLE")


if SELECT == "135B":  # 0 or 1 Swap

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

    # ソート検査
    count = 0
    for i in range(len(nums)):
        if nums[i] != (i + 1):
            count += 1
    if count < 3:
        print("YES")
    else:
        print("NO")


if SELECT == "135C":  # CitySaver

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

            if a[i + 1] < d:
                count += a[i + 1]
                a[i + 1] = 0
            else:
                count += d
                a[i + 1] -= d

        else:
            count += b[i]

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

Submission status: Accepted

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

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

Submission status: Accepted

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

B=B+[0]
counter = 0
for i in reversed(range(1, N+1)):
    dif = A[i] - B[i]
    if dif > 0:
        B[i] += min(dif, B[i-1])
        B[i-1] -= min(dif, B[i-1]) 
    counter += min(A[i], B[i])
counter += min(A[i-1], B[i-1])    
print(counter)

Submission status: Accepted

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

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

NumMonsBegin = sum(mons)

for i in range(0,n):
    if mons[i] >= hero[i]:
        mons[i] = mons[i]-hero[i]
#        print("----")
#        print(mons)
    else:
        yoryoku = hero[i]-mons[i]
        mons[i] = 0
        mons[i+1] = mons[i+1] - yoryoku
        if mons[i+1] < 0:
            mons[i+1] = 0

NumMonsLeft = sum(mons)

print(NumMonsBegin - NumMonsLeft)

Submission status: Accepted

Solution code:
import sys
from copy import deepcopy
from collections import deque
sys.setrecursionlimit(20000000)


def main():
    N = int(input())
    monsters = list(map(int, input().split()))
    heroes = list(map(int, input().split()))

    defeated = 0

    for i in reversed(range(len(heroes))):
        if heroes[i] == 0:
            continue

        for _ in [1, 0]:
            if monsters[i+_] >= heroes[i]:
                defeated += heroes[i]
                monsters[i+_] -= heroes[i]
                heroes[i] = 0
            elif monsters[i+_] < heroes[i]:
                defeated += monsters[i+_]
                heroes[i] -= monsters[i+_]
                monsters[i+_] = 0

    print(defeated)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N=int(input())
l1=list(map(int,input().split()))
l2=list(map(int,input().split()))

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

Submission status: Accepted

Solution code:
#C問題になると、正攻法ではTLEになる気がするが、ダメもとで出してみるか…
#更新してから足しちゃうと、答え変わるから注意。(書きかけの時）
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
beat = []
remain = []
 
for i in range(n+1):
  if i==0:
    if a[i]<=b[i]:
      beat.append(a[i])
      remain.append(0)
      b[i]-=a[i]
    else:
      beat.append(b[i])
      remain.append(a[i]-b[i])
      b[i] = 0
  elif i==n:
    if a[i]<=b[i-1]:
      beat.append(a[i])
      remain.append(0)
      b[i-1]-=a[i]
    else:
      beat.append(b[i-1])
      remain.append(a[i]-b[i-1])
      b[i-1] = 0
  elif a[i]<=b[i-1]:
    b[i-1] -= a[i]
    beat.append(a[i])
    remain.append(0)
  elif a[i] > b[i-1] and a[i]<=b[i]+b[i-1]:
    b[i] -= a[i]-b[i-1]
    b[i-1] = 0
    beat.append(a[i])
    remain.append(0)
  else:
    beat.append(b[i-1]+b[i])
    remain.append(a[i]-b[i-1]-b[i]) 
    b[i]=0
    b[i-1]=0
print(sum(beat))

Submission status: Accepted

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


at = 0
over = 0
for i in range(n):
    diff = max(0, a[i] - over) - b[i]
    over = max(0, -diff)
    at += max(0, diff)
print(sum(a) - (at + max(0, a[-1] - over)))

Submission status: Accepted

Solution code:
n=int(input())
a_list=[int(s) for s in input().split()]
b_list=[int(s) for s in input().split()]

total=0
for i,b in enumerate(b_list):
    a1=a_list[i]
    
    enemy=min(a1,b)
    if a1>=0 and b>0:
        b=b-enemy
        total+=enemy
    
    if b<=0:
        continue
    
    a2=a_list[i+1]
    enemy=min(a2,b)
    total+=enemy
    a_list[i+1]-=enemy
print(total)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#C問題になると、正攻法ではTLEになる気がするが、ダメもとで出してみるか…
#更新してから足しちゃうと、答え変わるから注意。(書きかけの時）
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
beat = []
remain = []

for i in range(n+1):
  if i==0:
    if a[i]<=b[i]:
      beat.append(a[i])
      remain.append(0)
      b[i]-=a[i]
    else:
      beat.append(b[i])
      remain.append(a[i]-b[i])
      b[i] = 0
  elif i==n:
    if a[i]<=b[i-1]:
      beat.append(a[i])
      remain.append(0)
      b[i-1]-=a[i]
    else:
      beat.append(b[i-1])
      remain.append(a[i]-b[i-1])
      b[i-1] = 0
  elif a[i]<=b[i-1]:
    b[i-1] -= a[i]
    beat.append(a[i])
    remain.append(0)
  elif a[i] > b[i-1] and a[i]<=b[i]+b[i-1]:
    b[i] -= a[i]-b[i-1]
    b[i-1] = 0
    beat.append(a[i])
    remain.append(0)
  else:
    beat.append(b[i-1]+b[i])
    remain.append(a[i]-b[i-1]-b[i]) 
    b[i]=0
    b[i-1]=0

print(sum(beat))

Submission status: Accepted

Solution code:
import math
import copy
import sys
import numpy as np

# ===FUNCTION===

def getInputNum():
    inputNum = int(input())
    return inputNum

def getInputListInt():
    outputoData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData

def getSomeInputInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputaNum]
        outputDataList.append(inputDataList)

    return outputDataList

# ===CODE===

n = getInputNum()

a = getInputListInt()
b = getInputListInt()

b.append(0)

ans = 0
tmp = 0

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

print(ans)

Submission status: Accepted

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

#print(a,b)

result=0

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

print(result)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
ans=0
plus=0
for i in range(N):
  if A[i]>=B[i]+plus:
    ans+=B[i]+plus
    plus=0
  else:
    ans+=A[i]
    if A[i]>=plus:
      plus=B[i]+plus-A[i]
    else:
      plus=B[i]
if A[N]>=plus:
  ans+=plus
else:
  ans+=A[N]
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):
    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()))  # N+1個の街
B = list(map(int, input().split()))  # N人の勇者

total_monster = sum(A)

if A[0] >= B[0]:  # 最初の街について、モンスターの方が多かったら
    A[0] -= B[0]
    B[0] = 0
else:  # 最初の街について、勇者のキャパの方が大きかったら
    B[0] -= A[0]
    A[0] = 0

for i in range(1, N):
    # i-1番目の勇者の残りのキャパによって処理を変えていく
    if B[i-1] > 0:  # キャパが残っていた場合
        if A[i] >= B[i-1]:
            A[i] -= B[i-1]
            B[i-1] = 0
        else:  # i-1番目の勇者だけで処理できるなら
            B[i-1] -= A[i]
            A[i] = 0

    if B[i-1] == 0:  # i-1番目の勇者のキャパが0の場合
        if A[i] >= B[i]:
            A[i] -= B[i]
            B[i] = 0
        else:
            B[i] -= A[i]
            A[i] = 0

# 最後の街について処理をする
if A[-1] > 0:
    A[-1] -= min(B[-1], A[-1])

tmp = sum(A)

print(total_monster - tmp)

Submission status: Accepted

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

ans = 0
for i in range(N):
    m = M[i] + M[i + 1] - U[i]
    if m > 0:
        ans += U[i]
        M[i + 1] = min(m, M[i + 1])
    else:
        ans += M[i] + M[i + 1]
        M[i + 1] = 0

print(ans)

Submission status: Accepted

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

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

r = 0
k = 0

for i in range(n):
    if r >= 0:
        m = min(r + b[i], a[i])
        k += m
        re = b[i] - max(0, m - r)
        r = re

    else:
        m = min(b[i], a[i])
        k += m
        r = b[i] - m

if r > 0:
    k += min(r, a[n])


print(k)

Submission status: Accepted

Solution code:
n = int(input())
l_a = list(map(int, input().split()))
l_b = list(map(int, input().split()))

ans = 0
for a, _ in enumerate(l_b):
  if l_a[a] < l_b[a] :
    if l_a[a+1] <= l_b[a] - l_a[a]:
      ans += l_a[a] + l_a[a+1]
      l_a[a+1] = 0
    else:
      ans += l_a[a] + l_b[a] - l_a[a]
      l_a[a+1] -= l_b[a] - l_a[a]
  else:
    ans += l_b[a]
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 a[i] < b[i]:
    c += a[i]
    d = a[i+1]
    a[i+1] = a[i+1] + a[i] - b[i]
    if a[i+1] < 0:
      a[i+1] = 0
      c += d
    else:
      c += (b[i] - a[i])
    
print(c)

Submission status: Accepted

Solution code:
N = int(input())
monsters = list(map(int, input().split()))
heros = list(map(int, input().split()))
nums = 0

for i in range(len(heros)):
    if monsters[i] <= heros[i]:
        heros[i] -= monsters[i]
        nums += monsters[i]
        monsters[i] = 0

        if monsters[i+1] <= heros[i]:
            heros[i] -= monsters[i+1]
            nums += monsters[i+1]
            monsters[i+1] = 0
        else:
            nums += heros[i]
            monsters[i+1] -= heros[i]
            heros[i] = 0
    else:
        nums += heros[i]
        monsters[i] -= heros[i]
        heros[i] = 0

print(nums)

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+=a[i]+a[i+1]-max(a[i]+a[i+1]-b[i],0)
    a[i+1]=max(0,a[i+1]-max(0,b[i]-a[i]))
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

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

    elif A[i] - B[i] <= 0:
        output += B[i]
        A[i], A[i + 1] = 0, A[i + 1] - (B[i] - A[i])

    else:
        output += B[i]
        A[i] -= B[i]

print(output)

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):
    ans+=min(a[i], bb)
    sub=max(bb-a[i], 0)
    ans+=min(a[i+1], sub)
    a[i+1] = max(a[i+1]-sub, 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 n in range(N):
    if B[n] > A[n]:
        B[n] = B[n] - A[n]
        cnt = cnt + A[n]
    else:
        cnt = cnt + B[n]
        continue

    if B[n] > 0:
        if A[n+1] >= B[n]:
            A[n+1] = A[n+1] - B[n]
            cnt = cnt + B[n]
        else:
            cnt = cnt + A[n+1]
            A[n+1] = 0


print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
li_A = list(map(int,input().split()))
li_B = list(map(int,input().split()))
cnt = 0
for i in range(0,n):
    defeat = min(li_A[i],li_B[i])
    cnt += defeat
    li_B[i] -= defeat
    re_defeat = min(li_A[i+1],li_B[i])
    cnt+=re_defeat
    li_A[i+1] -=re_defeat
print(cnt)

Submission status: Accepted

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

count = 0
x = 0  # 持ち越しキャパシティ
for i in range(N):
    a = A[i]
    b = B[i]
    # print(a, b)
    if x < a:  # 持ち越し分では足りない
        count += x
        a = a - x  # a: 残りの敵

        if a <= b:  # 全部倒せる
            count += a
            # 持ち越し分の計算
            x = b - a
        else:
            count += b
            x = 0
    else:  # 持ち越し分でモンスター全部倒した
        count += a
        x = b
    # print(count)

# print('x:', x)
# count += min(x, A[N])
if A[N] >= x:
    count += x
else:
    count += A[N]
print(count)

Submission status: Accepted

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

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

print(sum)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#C問題
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]
        if B[i]-A[i] >= A[i+1]:
            count+=A[i+1]
            A[i+1] = 0
        else:
            A[i+1]-=(B[i]-A[i])
            count+=(B[i]-A[i])
        A[i] = 0
        
    else:
        A[i]-=(B[i])
        count+=B[i]
        
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input()) #勇者の数
A = list(map(int,input().split()))
B = list(map(int,input().split()))
# A:モンスターの数(A1~An+1) B:倒せる数(B1~Bn)

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = [0]*(n+1)
tmp = 0
tmp_2 = 0

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

    if tmp - a[n-i] > 0:
        c[n-i] = a[n-i]
        #tmp_2 = tmp - a[n-i]
    else:
        c[n-i] = tmp
        #tmp_2 = 0

print(sum(c))

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+=a[i]
        b[i]-=a[i]
    else:
        cnt+=b[i]
        b[i]=0
    if a[i+1]<=b[i]:
        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())
monster=list(map(int,input().split()))
yusha=list(map(int,input().split()))
moto=sum(monster)

for i in range(1, n+1):
    if yusha[-i]<=monster[-i]:
        monster[-i]-=yusha[-i]
    else:
        monster[-i-1]=monster[-i-1]-yusha[-i]+monster[-i]
        monster[-i]=0
        if monster[-i-1]<0:
            monster[-i-1]=0



ans=moto-sum(monster)

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
for i in range(n+1):
    ans += old[i] - a[i]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]
pow = 0
ans = 0
for a, b in zip(A, B):
    ans += a if pow >= a else pow
    a = 0 if pow >= a else a - pow
    ans += a if a <= b else b
    pow = b - a if b >= a else 0
ans += A[N] if pow >= A[N] else pow
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
L = list(map(int,input().split()))
M = list(map(int,input().split()))
begin = sum(L)
for i in range(N):
    if M[i] > L[i]:
        L[i+1] -= M[i] - L[i]
        L[i] = 0
        L[i+1] = max(L[i+1],0)
    else:
        L[i] -= M[i]
end = sum(L)
print(begin - end)