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:
# -*- coding: utf-8 -*-
import sys;
import string;
sys.setrecursionlimit(20000) # 再起呼び出しの最大数をセット

import copy
import math
import itertools

from functools import lru_cache
#@lru_cache(maxsize = None)

PPP = 1000000007

# 整数の入力
n = int(input())

# スペース区切りの入力
#n, x = input().split()

# スペース区切りの整数の入力
#a, b = map(int, input().split())
#n, x = map(int, input().split())
#print("sp", n, k, file=sys.stderr)
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
#print("a", a, file=sys.stderr)
#p, q, r = map(int, input().split())
#s = [int(x) for x in input().split()]
#t = [int(x) for x in input().split()]

# 改行区切りの整数の入力
#a = []
#for x in range(n):
#  a.append(int(input()))
#  a.append([int(x) for x in input().split()])
#b = []
#for x in range(m):
#  b.append([int(x) for x in input().split()])
#p = [int(x) for x in input().split()]
#a.sort()
#print(l, file=sys.stderr)

# 文字列の入力
#s = input()

# 出力
#print("{} {}".format(a+b+c, s))
#a.sort()
#print(set(a), file=sys.stderr)
#print(len(set(a)))

#cache = dict()

import re
import fractions

def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)

def ton(n, k, x):
  s = ""
  while n > 0:
    s = str(n%k) + s
    n //= k
  return s


#print("a", a, file=sys.stderr)
#print("b", b, file=sys.stderr)

def getEnergy():
  for y in b:
    yield y

def doit():
  begin = 0
  end = n
  ng = 0
  total = 0
  for i in range(n):
    ab = min(a[i], b[i])
    total += ab
    a[i] -= ab
    b[i] -= ab
    ba = min(a[i+1], b[i])
    total += ba
    a[i+1] -= ba
    b[i] -= ba
  
  for i in range(n):
    i2 = n-1-i
    ab = min(a[i2+1], b[i2])
    total += ab
    a[i2+1] -= ab
    b[i2] -= ab
    ba = min(a[i2], b[i2])
    total += ba
    a[i2] -= ba
    b[i2] -= ba
  
  return total

def yn():
  if doit():
    return "YES"
  else:
    return "NO"

#print(doit([0] * 4))
#doit(a)
#print(doit())
try:
  #doit()
  print(doit())
  #print(yn())
except Exception as e:
  print("e", e, file=sys.stderr)

Submission status: Accepted

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

ans = 0
for i in range(N):
    # モンスターのほうが多い
    if B[i] <= A[i]:
        ans += B[i]
    elif B[i] > A[i]:
        ans += A[i]
        B[i] -= A[i]
        if B[i] <= A[i+1]:
            ans += B[i]
            A[i+1] -= B[i]
        elif B[i] > A[i+1]:
            ans += A[i+1]
            A[i+1] = 0
print(ans)

Submission status: Accepted

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

print(sum)

Submission status: Accepted

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

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

print(count)

Submission status: Accepted

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

pre = a[0]
count=0
for i in range(n):
  atack = min(a[i+1]+a[i],b[i])
  count += atack
  if atack > a[i]:
    a[i+1] -= atack-a[i]

print(count)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
A = list(map(int, str(input()).split()))
B = list(map(int, str(input()).split()))


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

print(monstar_count)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys, math, bisect, random, itertools
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    a, b = LI()
    if (a + b) % 2:
        print("IMPOSSIBLE")
        return
    print((a + b) // 2)
    return

#B
def B():
    n = II()
    p = LI()
    for i in range(n):
        for k in range(i+1, n):
            pi = p[::1]
            pi[i], pi[k] = pi[k], pi[i]
            for j in range(n-1):
                if pi[j] > pi[j + 1]:
                    break
            else:
                print("YES")
                return
    print("NO")
    return

#C
def C():
    n = II()
    a = LI()
    b = LI()
    ans = 0
    for i in range(n):
        if a[i] >= b[i]:
            ans += b[i]
            continue
        ans += a[i]
        b[i] -= a[i]
        if a[i + 1] >= b[i]:
            ans += b[i]
            a[i + 1] -= b[i]
            continue
        ans += a[i + 1]
        a[i + 1] = 0
    print(ans)
    return

#D
def D():
    s = S()
    s = s[::-1]
    dp = [[0] * 13 for _ in range(len(s))]
    remain = 1
    if s[0] == "?":
        for i in range(10):
            dp[0][i] = 1
    else:
        dp[0][int(s[0])] = 1
    for i in range(1, len(s)):
        remain = (remain * 10) % 13
        dpi = dp[i]
        dpi1 = dp[i-1]
        if s[i] == "?":
            for k in range(10):
                for j in range(13):
                    dpi[(j + remain * k) % 13] += dpi1[j]
                    dpi[(j + remain * k) % 13] %= mod
        else:
            for j in range(13):
                dpi[(j + remain * int(s[i])) % 13] += dpi1[j]
    print(dp[-1][5])
    return

#E
def E():
    return

#F
def F():
    return

#Solve
if __name__ == '__main__':
    C()

Submission status: Accepted

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

count = 0

for num in range(N):
    if A[num] >= B[num]:
        num_dead_monster = B[num]
    else:
        num_dead_monster = A[num]
    #print("now index", num_dead_monster)
    count += num_dead_monster
    A[num] -= num_dead_monster
    B[num] -= num_dead_monster
    
    if B[num] >= 1:
        if A[num+1] >= B[num]:
            num_dead_monster = B[num]
        else:
            num_dead_monster = A[num+1]
        count += num_dead_monster
        A[num+1] -= num_dead_monster
        #print("next index", B[num])
    
    #print("A", A)
    #print("B", B)
    #print()

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 a[i]<=b[i]:
        ans+=a[i]
        if a[i+1]>=b[i]-a[i]:
            ans+= b[i]-a[i] 
            a[i+1]-=b[i]-a[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()))
A.reverse()
B.reverse()
ans = 0
t = A[0]
for a, b in zip(A[1:], B):
    t += a
    m = min(t, b)
    ans += m
    t = t - m if t - a <= b else a

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

totalMonster = 0
kurikoshi = 0
for i in range(N + 1):
    if i == N:
        if kurikoshi >= A[i]:
            totalMonster = totalMonster + A[i]
        else:
            totalMonster = totalMonster + kurikoshi
        break

    if kurikoshi >= A[i]:
        totalMonster = totalMonster + A[i]
        kurikoshi = B[i]
    elif kurikoshi + B[i] >= A[i]:
        totalMonster = totalMonster + A[i]
        kurikoshi = kurikoshi + B[i] - A[i]
    else:
        totalMonster = totalMonster+ kurikoshi + B[i]
        kurikoshi = 0

print(totalMonster)

Submission status: Accepted

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

miss = 0
ex = 0
target = 0

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

print(sum(a) - miss)

Submission status: Accepted

Solution code:
n = int(input())
list_a=list(map(int, input().split()))  #リスト入力
list_b=list(map(int, input().split()))  #リスト入力
count = 0
list_b.append(0)
yuu_amari = 0
for i in range(n):
    if list_a[i] > yuu_amari:
        mon_now = list_a[i] - yuu_amari
    else:
        mon_now = 0
    yuu_now = list_b[i]
    count += min(mon_now, yuu_now)
    
    if yuu_now - mon_now > 0:
        yuu_amari = yuu_now - mon_now
        count += min(list_a[i+1], yuu_amari)
    else :
        yuu_amari = 0
print(count)

Submission status: Accepted

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

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

tmp = min(A[N - 1], B[N])
count += tmp
B[N] -= tmp
A[N - 1] -= tmp

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

tmp = 0
sum = 0
for i in range(N+1):
    if i == N:
        if A[i] - tmp < 0:
            sum += A[i]
        else:
            sum += tmp

    else:
        if A[i] - (B[i] + tmp) < 0:
            sum += A[i]
            A[i] = A[i] - tmp
            if A[i] < 0:
                A[i] = 0
            tmp = B[i] - A[i]
        else:
            sum += B[i] + tmp
            tmp = 0

print(sum)

Submission status: Accepted

Solution code:
n = int(input())
enemies = list(map(int, input().split()))
powers = list(map(int, input().split()))
powers.append(0)
nokori = 0
cnt = 0


for i in range(n+1):
  cnt += min(enemies[i], powers[i]+nokori)
  if powers[i]+nokori > enemies[i]:
    if nokori >= enemies[i]:
      nokori = powers[i]
    else:
      nokori = powers[i]+nokori-enemies[i]
  else:
    nokori = 0

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
city_backup = list(map(int, input().split()))
player = list(map(int, input().split()))
ans = 0
start_sum, end_sum = 0, 0
city = city_backup
for i in city:start_sum+=i
for i in range(n):
    #0 4 2
    if player[i]>city[i]:
        city[i+1] -= player[i] - city_backup[i]
        if city[i+1] < 0:
            city[i+1] = 0
        city[i] = 0
    else:city[i] -= player[i]
for i in city:end_sum+=i
print(start_sum-end_sum)

Submission status: Accepted

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

C = []
D = []
for n in range(N+1):
    if n == 0:
        C.append(min(A[0], B[0]))
        D.append(max(0, B[0] - A[0]))
    elif n == N:
        C.append(min(A[N], D[N-1]))
    else:
        C.append(min(A[n], B[n] + D[n-1]))
        D.append(max(0, B[n] - max(0, A[n] - D[n-1])))

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

Submission status: Accepted

Solution code:
import copy

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

d_A = copy.deepcopy(A)

ans = 0

for i in range(N):

    if A[i] <= B[i]:
        B[i] -= A[i]
        A[i] = 0
    else:
        A[i] -= B[i]
        B[i] = 0

    if B[i] > 0 and A[i+1] > 0:
        A[i+1] -= min(B[i], A[i+1])


ans = sum([_a - a for a,_a in zip(A, d_A)])


print(ans)

Submission status: Accepted

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

cnt = 0
i = 0
while i < N:
    if A[i] <= B[i]:
        cnt += A[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
        i += 1

    else:
        cnt += B[i]
        i += 1

print(cnt)

Submission status: Accepted

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

print(all_ene-sum(B))

Submission status: Accepted

Solution code:
N = int(input())
alist = [int(i) for i in input().split(" ")]
blist = [int(i) for i in input().split(" ")]
counts = 0
if alist[0] < blist[0]:
  counts += alist[0]
  blist[0] -= alist[0]
else:
  counts += blist[0]
  blist[0] = 0
for i in range(1,N):
  if alist[i] < blist[i-1]:
    counts += alist[i]
  elif alist[i] < blist[i-1] + blist[i]:
    counts += alist[i]
    blist[i] -= alist[i] - blist[i-1]
  else:
    counts += blist[i-1] + blist[i]
    blist[i] = 0
counts += min(alist[-1], blist[-1])
print(counts)

Submission status: Accepted

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


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

Submission status: Accepted

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

enemy = enemies[0]
count = 0
diff = 0

for i in range(n):
    enemy = enemies[i]

    kill = min(enemy, diff)
    count += kill
    enemy -= kill

    kill = min(enemy, heros[i])
    count += kill
    diff = heros[i] - kill

count += min(enemies[n], diff)
print(count)

Submission status: Accepted

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

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

print(dp[-1])

Submission status: Accepted

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

count = 0
for i in range(N+1):
    if i == 0:
        taosita = B[i]-max((B[i]-A[i]),0)
        count = taosita
        B[i]= B[i]-taosita
#        print("i=0")
    elif i!=N:
        #1つ前の勇者
        taosita1 = B[i-1]-max((B[i-1]-A[i]),0)
        count =count + taosita1
        A[i] = A[i]-taosita1
        #今回の勇者
        taosita2 = B[i]-max((B[i]-A[i]),0)
        count =count + taosita2
        B[i]= B[i]-taosita2
    if i == N:
        #1つ前の勇者
        taosita3 = B[i-1]-max((B[i-1]-A[i]),0)
        count =count + taosita3
#        A[i] = A[i]-taosita1
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 A[i] > B[i]:
        ans += B[i]
        A[i] -= B[i]
        B[i] -= B[i]
    else:
        ans += A[i]
        B[i] -= A[i]
        A[i] -= A[i]
    if A[i+1] > B[i]:
        ans += B[i]
        A[i+1] -= B[i]
        B[i] -= B[i]
    else:
        ans += A[i+1]
        B[i] -= A[i+1]
        A[i+1] -= A[i+1]
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()]

COUNT = 0

for i in range(N):
    if A[len(A)-i-1] >= B[len(B)-i-1]:
        COUNT = COUNT + B[len(B)-i-1]
        B[len(B)-i-1] = 0
        
    else:
        COUNT = COUNT + A[len(A)-i-1]
        X = B[len(B)-i-1] - A[len(A)-i-1]
        B[len(B)-i-1] = X
        
        if A[len(A)-i-2] >= B[len(B)-i-1]:
            COUNT = COUNT + B[len(B)-i-1]
            A[len(A)-i-2] = A[len(A)-i-2] - X
        else:
            COUNT = COUNT + A[len(A)-i-2]
            A[len(A)-i-2] = 0
        

print(COUNT)

Submission status: Accepted

Solution code:
a=[]
b=[]
ah=[]
bh=[]
c=int(input())
for x in input().split(" "):
    a.append(int(x))
for x in input().split(" "):
    b.append(int(x))

i=0
n=0

while i<c:
    if a[i]>=b[i]:
        n+=b[i]
        a[i]=a[i]-b[i]
    else:
        n+=a[i]
        b[i]-=a[i]
        a[i]=0
        if a[i+1]>=b[i]:
            n+=b[i]
            a[i+1]-=b[i]
        else:
            n+=a[i+1]
            a[i+1]=0
    i+=1

print(n)

Submission status: Accepted

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

# print(N)
# print(A)
# print(B)

a = A[::-1]
b = B[::-1]
# print(a)
# print(b)

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

Submission status: Accepted

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

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

    ans = 0
    motikosi = 0
    for i in range(N+1):
        if i <= N-1:
            if B[i]+motikosi > A[i]:
                # この街のモンスは全部倒す
                ans += A[i]
                restA = max(A[i]-motikosi, 0)
                motikosi = B[i] - restA
            else:
                # この街のモンスは全部倒しきれない
                ans += B[i]+motikosi
                motikosi = 0
        else:
            # 最後の街は総力戦
            if A[i] > motikosi:
                ans += motikosi
            else:
                ans += A[i]

    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Accepted

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

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

print(enemy)

Submission status: Accepted

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

taoshi_count = 0
for i in range(N):
    nokori = A[i] - B[i]
    if A[i] - B[i] >= 0 :
        taoshi_count += B[i]
    else :
        taoshi_count += A[i]
        nokori_power = abs(nokori)
        if A[i + 1] >= nokori_power :
            taoshi_count += nokori_power
            A[i + 1] -= nokori_power
        else :
            taoshi_count += A[i + 1]
            A[i + 1] = 0
print(taoshi_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):
	if a[i] >= b[i]:
		c += b[i]
	else:
		b[i] -= a[i]
		c += 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()))
an = 0

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

print(an)

Submission status: Accepted

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

count = 0
for i in range(n):
    if a[i] < b[i]:
        count += a[i]
        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()))
s=sum(a)
for i in range(n):
    t=a[i]-b[i]
    if t>=0:
        a[i]=t
    else:
        a[i]=0
        _t=a[i+1]-(-1*t)
        if _t>=0:
            a[i+1]=_t
        else:
            a[i+1]=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()))

mon=0
amari=0
for i in range(n):
    if a[i]<=amari:
        mon=mon+a[i]
        amari=b[i]
        continue
    if a[i]>=b[i]+amari:
        mon=mon+b[i]+amari
        amari=0
    if a[i]<b[i]+amari:
        mon=mon+a[i]
        amari=b[i]+amari-a[i]

if a[n]>amari:
    mon=mon+amari
if a[n]<=amari:
    mon=mon+a[n]

print(mon)

Submission status: Accepted

Solution code:
#coding : utf-8

import os
import sys

N = int(input())

A = list(map(lambda x: int(x), input().split(" ")))

B = list(map(lambda x: int(x), input().split(" ")))

#print(N)
#print(A)
#print(B)

total = 0

for i in range(N):
	if B[i] < A[i]:
		mons = B[i]
		total += mons
#		print("勇者%d bustered %d monsters => total=%d" % (i+1, mons, total))
	else:
		mons = A[i]
		total += mons
#		print("勇者%d bustered %d monsters => total=%d" % (i+1, mons, total))
		B[i] -= mons
		
		if B[i] > 0:
			mons = min(A[i+1], B[i])
			total += mons
#			print("勇者%d bustered %d monsters => total=%d" % (i+1, mons, total))
			B[i] -= mons
			A[i+1] -= mons

print(total)

Submission status: Accepted

Solution code:
n=int(input())
x=list(map(int,input().split()))
y=list(map(int,input().split()))

res=0

for i in range(n):
	if y[i]<x[i]:
		res+=y[i]
	else:
		if x[i+1]-(y[i]-x[i])>0:
			res+=y[i]
			x[i+1]=x[i+1]-(y[i]-x[i])
		else:
			res+=x[i]+x[i+1]
			x[i+1]=0

print(res)

Submission status: Accepted

Solution code:
"""
AtCoder
    :author: vierbein
"""
from __future__ import absolute_import, division, print_function

# import itertools as it
# import collections
import sys


def pp(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)


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

ans = 0
rest = A[0]
for a, b in zip(A[1:], B):
    inc = min(a + rest, b)
    ans += inc
    rest = max(0, a - max(0, b - rest))
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

a = list(int(_) for _ in input().split()) 
b = list(int(_) for _ in input().split()) 

cnt = min(a[0],b[0])
if b[0] > a[0]:
	tmp = b[0] - a[0]
else:
	tmp = 0

for i in range(1,n):

	if tmp >= a[i]:
		tmp =  b[i]
		cnt += a[i]
	elif b[i]+tmp >= a[i]:
		tmp = b[i]+tmp - a[i]
		cnt += a[i]
	else:
		cnt += b[i]
		cnt += tmp
		tmp = 0

if tmp > 0:
	cnt += min(a[n],tmp)

print(cnt)

Submission status: Accepted

Solution code:
from enum import Enum
from queue import Queue
import collections

import sys
import math

BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
a_li = list(map(int,input().split()))
b_li = list(map(int,input().split()))

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

Submission status: Accepted

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

beat=0
for i,b in enumerate(B):
    if A[i]>=b:
        beat+=b
    elif A[i+1]>=b-A[i]:
        beat+=b
        A[i+1]-=b-A[i]
    else:
        beat+=A[i] + A[i+1]
        A[i+1]=0

print(beat)

Submission status: Accepted

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

#余りのモンスター
amari = 0
total = sum(A)

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

amari += A[N]

print(total - amari)

Submission status: Accepted

Solution code:
n = int(input())

A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]

cnt = 0
for i in range(n):
    diff = B[i] - A[i]
    if diff > 0:
        cnt += A[i]
        if diff >= A[i+1]:
            cnt += A[i+1]
            A[i+1] = 0
        else:
            cnt += diff
            A[i+1] -= diff
    else:
        cnt += B[i]

print(cnt)

Submission status: Accepted

Solution code:
def li():
    return list(map(int, input().split()))


if __name__ == "__main__":
    n = int(input())
    a_list = li()
    b_list = li()

    monster = 0
    for index in range(n):
        monster_add = min(a_list[index], b_list[index])
        monster = monster + monster_add
        a_list[index] = a_list[index] - monster_add
        b_list[index] = b_list[index] - monster_add

        monster_add = min(a_list[index + 1], b_list[index])
        monster = monster + monster_add
        a_list[index + 1] = a_list[index + 1] - monster_add
        b_list[index] = b_list[index] - monster_add
    print(monster)

Submission status: Accepted

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

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

print(count)

Submission status: Accepted

Solution code:
N = int(input()) # 数字を変数に格納
List_A = list(map(int,input().split())) # 複数の数字をリストに格納
List_B = list(map(int,input().split())) # 複数の数字をリストに格納

import copy

List_a = copy.copy(List_A)
List_b = copy.copy(List_B)
count = 0

for i in range(N+1):
    if i >= 1:
        if List_a[i] > List_b[i-1]:
            count += List_b[i-1]
            List_a[i] -= List_b[i-1]
            List_b[i-1] = 0
        else:
            count += List_a[i]
            List_b[i-1] -= List_a[i]
            List_a[i] = 0
        
    if i < N:
        if List_a[i] > List_b[i]:
            count += List_b[i]
            List_a[i] -= List_b[i]
            List_b[i] = 0
        else:
            count += List_a[i]
            List_b[i] -= List_a[i]
            List_a[i] = 0

print(count)

Submission status: Accepted

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

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