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 python3
# -*- coding: utf-8 -*-
# 問題：https://atcoder.jp/contests/abc135/tasks/abc135_c

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

f1 = min(a[0], b[0])
f2 = min(a[1], b[0] - f1)
res = f1 + f2
for i in range(1, n):
    f1 = min(a[i] - f2, b[i])
    f2 = min(a[i+1], b[i] - f1)
    res += f1 + f2

print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
monsters = [int(m) for m in input().split(" ")]
heroes = [int(h) for h in input().split(" ")]

cnt = 0

for i in range(N):
  if monsters[i] <= heroes[i]:
    cnt += monsters[i]
    heroes[i] -= monsters[i]
    monsters[i] = 0
  else:
    cnt += heroes[i]
    monsters[i] -= heroes[i]
    heroes[i] = 0
    
  if heroes[i] != 0:
    if monsters[i + 1] <= heroes[i]:
      cnt += monsters[i + 1]
      heroes[i] -= monsters[i + 1]
      monsters[i + 1] = 0
    else:
      cnt += heroes[i]
      monsters[i + 1] -= heroes[i]
      heroes[i] = 0
      
print(cnt)

Submission status: Accepted

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

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

print(monsters)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
for i in range(N):
    #i人目の勇者の戦闘
    rem = B[i] #残りの倒せるモンスター
    
    if A[i] >= rem:
        ans += rem
        continue
    else:
        rem -= A[i]
        ans += A[i]

    if A[i+1] >= rem:
        ans += rem
        A[i+1] -= rem
    else:
        ans += A[i+1]
        A[i+1] = 0
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
b_list =list(map(int, input().split()))
ans = 0
for i in range(n-1,-1,-1):
    if a_list[i+1] >= b_list[i]:
        a_list[i+1] -=b_list[i]
        ans += b_list[i]
    elif a_list[i+1] < b_list[i]:
        ans += a_list[i+1]
        ans += min(a_list[i], (b_list[i]-a_list[i+1]))
        a_list[i] = max(0,a_list[i] -(b_list[i]-a_list[i+1]))
        a_list[i+1]=0

    #print(a_list)
print(ans)

Submission status: Accepted

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

ans,ov = 0,0

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

Submission status: Accepted

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

Submission status: Accepted

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

        if braves[i] > monsters[i + 1]:
            destroys += monsters[i + 1]
            monsters[i + 1] = 0
        
        else:
            destroys += braves[i]
            monsters[i + 1] -= braves[i]
    else:
        destroys += braves[i]
print(destroys)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
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()))

cnt=0

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

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

Submission status: Accepted

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

c=0
np=0
for i in range(n):
    if M[i]<np :
        c+=M[i]
        np=Y[i]
    elif M[i]<Y[i]+np :
        np = Y[i]+np-M[i]
        c+=M[i] 
    else:
        c+=Y[i]+np
        np = 0 
c+=min(np,M[n])

print(c)

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

Submission status: Accepted

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

s = sum(a)

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


print(s-sum(a))

Submission status: Accepted

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

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

print(result)

Submission status: Accepted

Solution code:
import math
from math import gcd
INF = float("inf")

import sys
input=sys.stdin.readline
import itertools
from collections import Counter

def main():
    n = int(input())
    a = list(map(int, input().split()))
    ans = [0]*(n+1)
    b = list(map(int, input().split()))
    for i in range(n):
        if a[i]-ans[i] >= b[i]:
            ans[i] += b[i]
        else:
            k = ans[i]
            ans[i] += a[i]-k
            rest = b[i] - a[i] + k
            ans[i+1] += min(rest,a[i+1])
    print(sum(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()))
ans = 0

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

Submission status: Accepted

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

Submission status: Accepted

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

    for i in range(n):
        x = A[i] + A[i+1]
        ans += min(x,B[i])
        A[i+1] = min(A[i+1],max(x-B[i],0))
    return print(ans)

solv()

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

Submission status: Accepted

Solution code:
n = int(input().rstrip())
aa = list(map(int, input().split()))
bb = list(map(int, input().split()))

ans = 0
resid = 0
for i in range(n):
    prev_beat = min(resid, aa[i])
    cur_beat = min(aa[i] - prev_beat, bb[i])
    ans += prev_beat
    ans += cur_beat
    resid = bb[i] - cur_beat
ans += min(resid, aa[n])

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a, b = [list(map(int, input().split())) for _ in range(2)]

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve():
    n = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    ans = 0
    for i in range(n):
        x = A[i] + A[i + 1]
        ans += min(B[i], x)
        A[i + 1] = min(A[i + 1], max(x - B[i], 0))
    print(ans)


solve()

Submission status: Accepted

Solution code:
def solve():
    n = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    ans = 0
    for i in range(n):
        ans += min(B[i], A[i] + A[i + 1])
        A[i + 1] = min(A[i + 1], max(A[i] + A[i + 1] - B[i], 0))
    print(ans)


solve()

Submission status: Accepted

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


solve()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
s = sum(A)
for i, b in enumerate(B):
    if A[i] <= b:
        A[i+1] = max(0, A[i+1] - (b-A[i]))
        A[i] = 0
    else:
        A[i] = A[i] - b
print(s-sum(A))

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

Submission status: Accepted

Solution code:
import bisect,collections,copy,heapq,itertools,math,string
from collections import defaultdict as D
from functools import reduce
import numpy as np
import sys
import os
from operator import mul

sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353    

n = I()
a = LI()
b = LI()

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

Submission status: Accepted

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

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

print(monsters-sum(a))

Submission status: Accepted

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

Submission status: Accepted

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

cnt=0
for i in range(len(B)):
  a=min(A[i], B[i])
  cnt+=a
  a=min(A[i+1],B[i]-a)
  cnt+=a
  A[i+1]-=a
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

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

    if B[i] > 0 and A[i] > 0:
        if B[i] > A[i]:
            res += A[i]
            A[i] = 0
        else:
            res += B[i]
            A[i] -= B[i]

print(res)

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

ans = 0

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

    else:
        ans += b[i]
        b[i] = 0
        continue

    if a[i+1] <= b[i]:
        ans += a[i+1]
        b[i] -= a[i+1]
        a[i+1] = 0

    else:
        ans += b[i]
        a[i+1] -= b[i]
        b[i] = 0

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

ans = sum(a)

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

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

Submission status: Accepted

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

def dm(i, j):
    d = min(a[i], b[j])
    a[i] -= d; b[j] -= d
    return d

for i in range(n):
    cnt += dm(i, i)
    cnt += dm(i + 1, i)
print(cnt)

Submission status: Accepted

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

sumA = sum(A)
a = A[:]
for i, b in enumerate(B):
    if a[i] < b:
        b -= a[i]
        a[i] = 0
        a[i+1] = max(0, a[i+1] - b)
    else:
        a[i] -= b
ans = sumA - sum(a)
a = A[:]
for i in range(N - 1, -1, -1):
    b = B[i]
    if a[i+1] < b:
        b -= a[i+1]
        a[i+1] = 0
        a[i] = max(0, a[i] - b)
    else:
        a[i+1] -= b
ans = max(ans, sumA - sum(a))
print(ans)

Submission status: Accepted

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

ene = 0
for i in range(n):
    if b[i] >= a[i]+a[i+1]:
        ene += a[i]+a[i+1]
        a[i]=0
        a[i+1]=0
        #print("a1")
    elif b[i] >= a[i]:
        ene += b[i]
        a[i+1] -= b[i]-a[i]
        a[i]=0
        #print("a2")
    else:
        ene += b[i]
        a[i] -= b[i]
        #print("a3")
    #print(a)
print(ene)

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_next = 0
ans = 0
for i in range(n):
    if b_next >= a[i]:
        ans += a[i]
        b_next = b[i]
    elif b_next < a[i]:
        if b[i] < a[i] - b_next:
            ans += b_next + b[i]
            b_next = 0
        elif b[i] >= a[i] - b_next:
            ans += a[i]
            b_next = b[i] - (a[i] - b_next)
ans += min(a[n], b_next)
print(ans)

Submission status: Accepted

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

eriminate_enemy = [0 for _ in range(N+1)]

for index, B in enumerate(Bs):
    if(B <= As[index]):
        eriminate_enemy[index] += B
        continue

    eriminate_enemy[index] += As[index]
    
    if(B <= As[index] + As[index + 1]):
        eriminate_enemy[index + 1] += B - As[index]
        As[index+1] -= eriminate_enemy[index + 1]
    
    else:
        eriminate_enemy[index + 1] += As[index+1] 
        As[index+1] = 0

print(sum(eriminate_enemy))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7) #再帰関数の上限,10**5以上の場合python
import math
from copy import copy, deepcopy
from copy import deepcopy as dcp
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right#2分探索
#bisect_left(l,x), bisect(l,x)#aはソート済みである必要あり。aの中からx未満の要素数を返す。rightだと以下
from collections import deque, defaultdict
#deque(l), pop(), append(x), popleft(), appendleft(x)
#q.rotate(n)で → にn回ローテート
from collections import Counter#文字列を個数カウント辞書に、
#S=Counter(l),S.most_common(x),S.keys(),S.values(),S.items()
from itertools import accumulate,combinations,permutations,product#累積和
#list(accumulate(l))
from heapq import heapify,heappop,heappush
#heapify(q),heappush(q,a),heappop(q) #q=heapify(q)としないこと、返り値はNone
from functools import reduce,lru_cache#pypyでもうごく
#@lru_cache(maxsize = None)#maxsizeは保存するデータ数の最大値、2**nが最も高効率
from decimal import Decimal

def input(): 
    x=sys.stdin.readline()
    return x[:-1] if x[-1]=="\n" else x
def printe(*x):print("## ",*x,file=sys.stderr)
def printl(li): _=print(*li, sep="\n") if li else None
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds
def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65
def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65)
def matmat(A,B):
    K,N,M=len(B),len(A),len(B[0])
    return [[sum([(A[i][k]*B[k][j]) for k in range(K)]) for j in range(M)] for i in range(N)]
def matvec(M,v):
    N,size=len(v),len(M)
    return [sum([M[i][j]*v[j] for j in range(N)]) for i in range(size)]
def T(M):
    n,m=len(M),len(M[0])
    return [[M[j][i] for j in range(n)] for i in range(m)]
def binr(x): return bin(x)[2:]
def bitcount(x): #xは64bit整数
    x= x - ((x >> 1) & 0x5555555555555555)
    x= (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x= (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f 
    x+= (x >> 8); x+= (x >> 16); x+= (x >> 32) 
    return x & 0x7f

def main():
    mod = 1000000007
    #w.sort(key=itemgetter(1),reverse=True)  #二個目の要素で降順並び替え

    N = int(input())
    #N, K = map(int, input().split())
    A = list(map(int, input().split())) #1行ベクトル
    B = tuple(map(int, input().split()))
    #L = tuple(int(input()) for i in range(N)) #改行ベクトル
    #S = tuple(tuple(map(int, input().split())) for i in range(N)) #改行行列
    cnt=0
    for i in range(N):
        a=A[i];b=B[i]
        if b>a:
            cnt+=min(b,a+A[i+1])
            A[i]=0
            A[i+1]=max(0,A[i+1]-(b-a))
            
        else:
            A[i]=a-b
            cnt+=b
        
    print(cnt)




if __name__ == "__main__":
    main()

Submission status: Accepted

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

# 最初のモンスター総数
mons_n = sum(A)

# 各勇者について
for i in range(N):
    # 余力
    power = B[i]
    # A[i]で出来る限り倒す時の撃破数
    temp = min(A[i], power)
    A[i] -= temp
    power -= temp

    # 余力でA[i+1]で限界まで戦う
    A[i + 1] -= min(A[i + 1], power)

print(mons_n - sum(A))

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

# 合計で倒せる数
cnt = 0
# 次の街を助けることのできる数
help_cnt = 0
for i in range(n):
    # 自分の街で倒す数 = min(街のモンスター - 前の街の勇者が助けた数, 自分が倒せる数)
    now_kill_cnt = min(a[i]-help_cnt, b[i])

    # 次の街を助けることのできる数を更新
    # min(次の街のモンスター数, 余力)
    help_cnt = min(a[i+1], b[i]-now_kill_cnt)
    cnt += now_kill_cnt
    cnt += help_cnt


print(cnt)

Submission status: Accepted

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

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

print(defeat_monster)

Submission status: Accepted

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

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

print(monster - sum(a))

Submission status: Accepted

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

count=0

D=[A[0]]

for i in range(N):
    s=B[i]-D[i]
    if s>0:
        d=A[i+1]-s
        if d>0:
            d=d
            count+=B[i]
        else :
            d=0
            count+=D[i]+A[i+1]
    else:
        d=A[i+1]
        count+=B[i]
    D.append(d)

print(count)

Submission status: Accepted

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

Submission status: Accepted

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

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):
    t = min(a[i], b[i])
    cnt += t
    a[i] -= t
    b[i] -= t

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

print(cnt)