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

cnt = 0
for i in range(n-1, -1,-1):
    if mons[i+1] < kill[i]:
        cnt += mons[i+1]
        kill[i] -= mons[i+1]
        mons[i+1] = 0
    else:
        cnt += kill[i]        
        mons[i+1] -= kill[i]
        kill[i] = 0
    if mons[i] < kill[i]:
        cnt += mons[i]
        kill[i] -= mons[i]
        mons[i] = 0
    else:
        cnt += kill[i]
        mons[i] -= kill[i]
        kill[i] = 0
        
print(cnt)

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

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):
    c = min(A[i]+A[i+1], B[i])
    cnt += c
    A[i+1] = A[i+1] - max(c-A[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):
    if B[i] <= A[i]:
        ans += B[i]
    else:
        ans += A[i]
        remat = B[i]-A[i]
        if A[i+1] - remat < 0:
            ans += A[i+1]
        else:
            ans += remat
        
        A[i+1] = max(0,A[i+1]-remat)

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 10 11:20:10 2020

@author: liang
"""

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

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

print(ans)

Submission status: Accepted

Solution code:

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

Submission status: Accepted

Solution code:
N = int(input())
As = input().split(' ')
As = [int(As[i]) for i in range(N+1)]
Bs = input().split(' ')
Bs = [int(Bs[i]) for i in range(N)]
WIN = 0
for i in range(N):
  if Bs[i] > As[i]:
    WIN += As[i]
    Bs[i] -= As[i]
    As[i] = 0
    if Bs[i] > As[i+1]:
      WIN += As[i+1]
      Bs[i] -= As[i+1]
      As[i+1] = 0
    elif Bs[i] == As[i+1]:
      WIN += As[i+1]
      Bs[i] = 0
      As[i+1] = 0
    else:
      WIN += Bs[i]
      As[i+1] -= Bs[i]
      Bs[i] = 0
  elif Bs[i] == As[i]:
    WIN += As[i]
    Bs[i] = 0
    As[i] = 0
  else:
    WIN += Bs[i]
    As[i] -= Bs[i]
    Bs[i] = 0
print(WIN)

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

Submission status: Accepted

Solution code:
n = int(input())
monster_list = list(map(int, input().split()))
kill_mon = list(map(int, input().split()))

# 配列数を合わせる
kill_mon.append(0)

# 持ち越しパワーの初期化
killed_monster = 0
carry_power = 0
for mons, kill in zip(monster_list, kill_mon):   
    # 全パワーを使い果たす
    if mons >= (kill + carry_power):
        killed_monster += (kill + carry_power)
        # 持ち越しパワーの初期化
        carry_power = 0
    # 余力を残して勝利する
    else:
        killed_monster += mons
        # 前回余力は今回のモンスターにしか使えない。次回のバトルに持ち越せない
        # だから、mons - carry_powerとなる
        # ただ、前回余力が今回モンスターよりも多い場合も考慮するためmax関数を使いマイナス値になることを避ける
        # 前回余力だけで今回バトルに勝利したら、今回パワーをそのまま持ち越せる
        carry_power = kill - max(0, mons - carry_power)
print(killed_monster)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

if a[-1] < 0:
    a[-1] = 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()))
count = 0
tmp = 0
for i in range(N):
    tmp = min(A[i],B[i])
    count +=tmp
    tmp = min(A[i+1],B[i]-tmp)
    A[i+1] -=tmp
    count +=tmp
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

if hero>0:
    monster += min(hero,A[N])
print(monster)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = [0]*(N+1) # 残り
cnt = 0
C[0] = A[0]
for i in range(1, N+1):
  #i-1番目の勇者が攻撃した後、i番目の街に残っているモンスターはC[i-1]体。N+1番目の街ではC[N]
  #i番目の勇者B[i-1]はi番目の街A[i-1]を攻撃したあと余力があれば(i+1)番目A[i]まで攻撃できる。
  
  if C[i-1] > B[i-1]: #余力がない
    C[i] = A[i]
    cnt += A[i-1] + B[i-1] -C[i-1] #i番目の街で倒したモンスター
    
  elif C[i-1] <= B[i-1]:
    if A[i] -B[i-1] + C[i-1] >=0:
      C[i] = A[i] -B[i-1] + C[i-1]
      cnt += A[i-1]
      
    else: #余力が有り余っている場合
      C[i] = 0
      cnt += A[i-1]
      


if B[N-1]-C[N-1] > 0:
  ans = cnt + min(A[N], (B[N-1] - C[N-1]))
else:
  ans = cnt
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: 	C
# CreatedDate:  2020-09-04 13:08:51 +0900
# LastModified: 2020-09-04 13:26:51 +0900
#


import os
import sys
# import numpy as np
# import pandas as pd


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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque
from bisect import bisect_left
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:
    cnt += A[i]
    d = B[i] - A[i]
    if d <= A[i+1]:
      cnt += d
      A[i+1] -= d
    else:#つまり d > A[i+1]
      cnt += A[i+1]
      A[i+1] = 0
#  print('i=', i, cnt)

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):
  c=b[i]-a[i]
  
  #city_iのみ攻撃
  if c<=0:
    cnt+=b[i]
  
  #city_i+1も攻撃
  else:
    d=b[i]-a[i+1]-a[i]
    
    #city_i+1生き残る
    if d<=0:
      cnt+=b[i]
      a[i+1]-=c
      
    #オーバキル
    else:
      cnt+=a[i]+a[i+1]
      a[i+1]=0
      
print(cnt)

Submission status: Accepted

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

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

    if B[N-1-i] > 0:
        if A[N-1-i] > B[N-1-i]:
            A[N-1-i] -= B[N-1-i]
            B[N-1-i] = 0
        else:
            B[N-1-i] -= A[N-1-i]
            A[N-1-i] = 0
    # print(A)
    # print(B)
    # print("=============")

print(ans-sum(B))

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    b_list = list(map(int, input().split()))
    ans = 0

    for i in range(n):
        c = min(a_list[i], b_list[i])
        ans += c
        d = min(b_list[i] - c, a_list[i + 1])
        ans += d
        a_list[i + 1] -= d

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    b_list = list(map(int, input().split()))
    ans = 0

    for i in range(n):
        if a_list[i] >= b_list[i]:
            ans += b_list[i]
        else:
            d = b_list[i] - a_list[i]
            ans += a_list[i]
            if a_list[i + 1] >= d:
                ans += d
                a_list[i + 1] = a_list[i + 1] - d
            else:
                ans += a_list[i + 1]
                a_list[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()))
ans = 0
for i in range(N):
  one = min(A[i],B[i])
  two = min(A[i+1],B[i]-one)
  ans+=one+two
  A[i+1]-=two
print(ans)

Submission status: Accepted

Solution code:
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N = I()
    A = LI()
    B = LI()

    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)

if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
from numpy import cumsum
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] -= max(0, min(b[i] - a[i], a[i+1]))

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

print(c)

Submission status: Accepted

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

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

Submission status: Accepted

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

goukei=sum(a)

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

print(sum)

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

Submission status: Accepted

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
# from collections import Counter,defaultdict,deque
# from itertools import permutations, combinations
# from heapq import heappop, heappush
# # input = sys.stdin.readline
# sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

n = inp()
al = inpl()
bl = inpl()
ans = 0
rem = 0
for i in range(0, n):
    if(al[i] <= bl[i] + rem):
        tmp = al[i] - rem
        if(tmp <= 0):
            tmp = 0
        rem = bl[i] - tmp
        ans += al[i]
    else:
        ans += bl[i] + rem
        rem = 0
if(al[n] >= rem):
    ans += rem
else:
    ans += al[n]
print(ans)

Submission status: Accepted

Solution code:
import sys

def main():
    input = sys.stdin.readline
    n = int(input())
    A_list = list(map(int, input().split()))
    B_list = list(map(int, input().split()))

    result = 0
    for i in range(n):
        current_b = B_list[i]
        rest_b = current_b - A_list[i]
        if rest_b > 0:
            result += A_list[i]
            rest_next_a = A_list[i+1] - rest_b
            if rest_next_a > 0:
                result += rest_b
                A_list[i+1] = rest_next_a
            else:
                result += A_list[i+1]
                A_list[i+1] = 0

        else:
            result += current_b

    print(result)


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

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

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

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
count=0
for i,b in enumerate(B):
    attack=min(b,A[i]+A[i+1])
    count+=attack
    A[i+1]-=max(0,attack-A[i])
print(count)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
score = 0
for _ in range(n):
    a = A.pop()
    aa = A.pop()
    b = B.pop()
    if (a+aa-b) >=0:
        A.append(min(aa,a+aa-b))
        score += b
    else:
        A.append(0)
        score += a+aa
print(score)

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

Submission status: Accepted

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

C = [0] * N

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

print(sum(C))

Submission status: Accepted

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

for i in range(n):
    hunted1 = min(a[i],b[i])
    a[i] -=hunted1
    b[i] -=hunted1
    hunted2 = min(a[i+1],b[i])
    a[i+1] -=hunted2
    b[i] -=hunted2
    
print(mosters-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, v in enumerate(b):
    for j in range(2):
        m =  min(a[i+j], v)
        a [i+j] -= m
        v -= m
        ans += m
print(ans)

Submission status: Accepted

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

for i, b in enumerate(B):
    c = min(A[i], b)
    A[i] -= c
    A[i + 1] -= min(A[i + 1], b - c)

print(sumA - sum(A))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

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

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

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

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):
  if List_A[i] >= List_B[i]:
    res += List_B[i]
  else:
    res += List_A[i]
    if List_A[i+1] >= List_B[i]-List_A[i]:
      res += List_B[i]-List_A[i]
      List_A[i+1] = List_A[i+1] - List_B[i] +List_A[i]
    else:
      res += List_A[i+1]
      List_A[i+1] = 0
print(res)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc135/tasks/abc135_c
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
ans = 0
tmp = 0
for i in range(n):
    #print(i,tmp)
    #print(a)
    #print(b)
    if tmp > 0:
        if a[i] <= tmp:
            ans += a[i]
            a[i] = 0
            tmp = b[i]
        else:
            ans += tmp
            a[i] -= tmp
            tmp = 0
    if a[i] > 0:
        if a[i] <= b[i]:
            ans += a[i]
            tmp = b[i] - a[i]
            a[i] = 0
            b[i] = tmp
        else:
            ans += b[i]
            a[i] -= b[i]
            b[i] = 0
            tmp = 0
tmp = min(tmp,a[-1])
#print(a)
#print(b)
#print(tmp)
print(ans+tmp)

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):
  a = A[i]
  b = B[i]
  d = max(min(a, b), 0)
  res += d
  
  r = b - d
  if r > 0:
    d = min(A[i+1], r)
    A[i+1] -= d
    res += d
print(res)

Submission status: Accepted

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

count = 0
last = 0#前のBの余力
for i in range(N):
    at = min(A[i] - last, B[i])
    if at < 0:
        at = 0
    if at == 0:
        count += A[i]
    else:
        count += at + last
    last = B[i] - at
count += min(A[-1],last)
print(count)

Submission status: Accepted

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

monster = 0

for i in range(N):

    if A[i] >= B[i]:
        monster += B[i]
        A[i] -= B[i]
        B[i] = 0

    else:
        monster += A[i]
        B[i] -= A[i]
        A[i] = 0

        if A[i + 1] >= B[i]:
            A[i + 1] -= B[i]
            monster += B[i]
            B[i] = 0

        else:
            monster += A[i + 1]
            B[i] -= A[i + 1]
            A[i + 1] = 0


# print(A)
# print(B)
print(monster)

Submission status: Accepted

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    A=LI()
    B=LI()
    
    ans=0
    
    for i in range(N):
        if A[i]>=B[i]:
            ans+=B[i]
        else:
            ans+=A[i]
            rem=B[i] - A[i]
            if A[i+1]>=rem:
                ans+=rem
                A[i+1]-=rem
            else:
                ans+=A[i+1]
                A[i+1]=0
                
                
    print(ans)
            
        

main()

Submission status: Accepted

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

allMonsters = sum(A)

defeatMonsters = 0
for i in range(N):

    if A[i] <= B[i]:
        defeatMonsters += A[i]
        B[i] -= A[i]

        if A[i+1] >= B[i]:
            defeatMonsters += B[i]
            A[i+1] -= B[i]
        else:
            defeatMonsters += A[i+1]
            A[i+1] = 0

    else:
        defeatMonsters += B[i]
        B[i] = 0

print(defeatMonsters)
# print(A)

Submission status: Accepted

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

total = sum(a)

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

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