Problem:


Score : 300 points

Problem Statement

There are N+1 towns. The i-th town is being attacked by A_i monsters.

We have N heroes. The i-th hero can defeat monsters attacking the i-th or (i+1)-th town, for a total of at most B_i monsters.

What is the maximum total number of monsters the heroes can cooperate to defeat?

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_{N+1}
B_1 B_2 ... B_N

Output

Print the maximum total number of monsters the heroes can defeat.

Sample Input 1

2
3 5 2
4 5

Sample Output 1

9

If the heroes choose the monsters to defeat as follows, they can defeat nine monsters in total, which is the maximum result.

The first hero defeats two monsters attacking the first town and two monsters attacking the second town.

The second hero defeats three monsters attacking the second town and two monsters attacking the third town.

Sample Input 2

3
5 6 3 8
5 100 8

Sample Output 2

22

Sample Input 3

2
100 1 1
1 100

Sample Output 3

3



Sample input:
2
3 5 2
4 5


Sample output:
9

Submission status: Accepted

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

ans = 0
"""
ai = 0
bi = 0
while 1:
    if max(b) == 0:
        break
    if a[ai] < b[bi]:
        ans += a[ai]
        b[bi] -= a[ai]
        a[ai] = 0
        ai += 1
    elif a[ai] == b[bi]:
        ans += a[ai]
        b[bi] = 0
        a[ai] = 0
        ai += 1
        bi += 1
    else:
        ans += b[bi]
        a[ai] -= b[bi]
        b[bi] = 0
        if ai > bi:
            bi += 1
        else:
            ai += 1
            bi += 1
 #   print(a)
 #   print(b)
 #   print(ai,bi)
    if ai >= n+1 or bi >= n:
        break
print(ans)
"""
for i in range(n):
    if b[i] >= a[i] + a[i+1]:
        ans += a[i] + a[i+1]
        a[i+1] = 0
    else:
        if b[i] >= a[i]:
            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=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
tt=0
for i in range(1,n+1):
  temp=min(b[i-1],a[i-1])
  b[i-1]-=temp
  a[i-1]-=temp
  tt+=temp
  temp=min(b[i-1],a[i])
  b[i-1]-=temp
  a[i]-=temp
  tt+=temp
print(tt)

Submission status: Accepted

Solution code:
N = int(input())
al = list(map(int, input().split()))
bl = list(map(int, input().split()))

k = 0
for i in range(N):
    j = N - i - 1
    bi = bl[j]
    ai1 = al[j+1]
    if bi <= ai1:
        k += bi
        continue
    elif bi > ai1:
        k += ai1
        bi -= ai1
        ai = al[j]
        if bi <= ai:
            k += bi
            al[j] -= bi
            continue
        elif bi > ai:
            k += ai
            al[j] = 0
            continue

print(k)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

def input():
    return sys.stdin.readline().rstrip()

if __name__ == "__main__":
    n = int(input())
    al = list(map(int, input().split()))
    bl = list(map(int, input().split()))

    total = 0
    for i in range(n):
        if al[i] < bl[i]:
            total += al[i]
            if al[i+1] <= bl[i] - al[i]:
                total += al[i+1]
                al[i+1] = 0
            else:
                total += bl[i] - al[i]
                al[i+1] -= bl[i] - al[i]
        else:
            total += bl[i]

    print(total)

Submission status: Accepted

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

guchoku=0
for i,b in enumerate(B):
    t = A[i] - b
    if t <= -1:
        zen = max(A[i+1] + t, 0)
        guchoku += A[i+1] - zen
        A[i+1] = zen
    guchoku += min(A[i], b)
print(guchoku)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# 
import math
import sys
import itertools
import numpy as np

# いっこ入力
n =int(input())

# 空白区切りの数列をリストに代入
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
max_mon = 0
if a[0] < b[0]:
    max_mon += a[0]
    b[0] = b[0] - a[0]
else:
    max_mon += b[0]
    b[0]=0
for i in range(1,n):
    if a[i] < b[i-1]:
        max_mon += a[i]
        b[i-1] = b[i-1] - a[i]
        a[i] = 0
    else:
        max_mon += b[i-1]
        a[i] = a[i] - b[i-1]
        b[i-1] = 0
    if a[i] < b[i]:
        max_mon += a[i]
        b[i] = b[i] - a[i]
        a[i] = 0
    else:
        max_mon += b[i]
        a[i] = a[i] - b[i]
        b[i] = 0
if a[n] < b[n-1]:
    max_mon += a[n]
    b[n-1] = b[n-1] - a[n]
    a[n] = 0
else:
    max_mon += b[n-1]
    a[n] = a[n] - b[n-1]
    b[n-1] = 0


print(max_mon)

Submission status: Accepted

Solution code:
def sol():
    import sys
    input=sys.stdin.readline
    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+=a[i]
            b[i]-=a[i]
            ans+=min(b[i],a[i+1])
            a[i+1]=max(0,a[i+1]-b[i])
        else:
            ans+=b[i]
    print(ans)

if __name__=="__main__":
    sol()

Submission status: Accepted

Solution code:

def main():
  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] >= 0:
      cnt += A[i]
      B[i] -= A[i]
      A[i] = 0
    elif B[i] - A[i] < 0:
      cnt += B[i]
      tmp = B[i]
      B[i] = 0
      A[i] -= tmp
    if B[i] == 0:
      continue
    if B[i] - A[i+1] >= 0:
      cnt += A[i+1]
      B[i] -= A[i+1]
      A[i+1] = 0
    elif B[i] < A[i+1]:
      cnt += B[i]
      tmp = B[i]
      B[i] = 0
      A[i+1] -= tmp
  print(cnt)

if __name__ == "__main__":
  main()

Submission status: Accepted

Solution code:
N = int(input())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))

total = 0
for i in range(N):
    b = b_list[i]

    defeated = min(a_list[i], b)
    a_list[i] -= defeated
    b -= defeated
    total += defeated

    defeated = min(a_list[i + 1], b)
    a_list[i + 1] -= defeated
    total += defeated
print(total)

Submission status: Accepted

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]
B = [int(x) for x in stdin.readline().rstrip().split()]
ans = 0
hikitugi = 0

for ind,i in enumerate(B):
    if A[ind] <= i:
        ans += A[ind]
        hikitugi = i - A[ind]
        if A[ind+1] <= hikitugi:
            ans += A[ind+1]
            A[ind+1] = 0
        else:
            ans += hikitugi
            A[ind+1] = A[ind+1] - hikitugi
    else:
        A[ind] = A[ind] - i
        ans += i
print(ans)

Submission status: Accepted

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

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

print(cnt)

Submission status: Accepted

Solution code:
# coding: UTF-8
import sys
#sys.setrecursionlimit(n)
import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

readl= lambda: list(map(int, sys.stdin.readline().split()))
readt= lambda: tuple(map(int, sys.stdin.readline().split()))
read = lambda: sys.stdin.readline().rstrip()
readi = lambda: int(read())
readmi = lambda: map(int, sys.stdin.readline().split())
readms = lambda: map(str, sys.stdin.readline().split())

n = readi()
a = readl()
b = readl()
count = 0
for i in range(n):
    for j in range(2):
        t = min(a[i + j], b[i])
        count += t
        a[i + j] -= t
        b[i] -= t
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 range(n):
  if a[i] <= b[i]:
    count += a[i]
    b[i] -= a[i]
    count += min(a[i+1], b[i])
    a[i+1] = max(0, a[i+1] - b[i])
  else:
    count += b[i]

print(count)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10**9)
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def LIST(): return [int(x) for x in input().split()]
MOD = 10**9 + 7

N = INT()
A = LIST()
B = LIST()

kill = 0

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

print(kill)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n = ni()
a = list(li())
b = list(li())

cnt = 0

for i in range(n+1):
    for j in range(i-1, i+1):
        if j < 0 or j >= n:
            continue

        if b[j] == 0:
            continue

        elif a[i] <= b[j]:
            cnt += a[i]
            b[j] -= a[i]
            a[i] = 0
            break

        else:
            cnt += b[j]
            a[i] -= b[j]
            b[j] = 0


print(cnt)

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, A: "List[int]", B: "List[int]"):
    count = 0
    for i in range(N):
        if A[i] < B[i]:
            count += A[i]
            diff = B[i] - A[i]
            count += min(A[i+1], diff)
            A[i+1] = max(0, A[i+1] - diff)
        else:
            count += B[i]
    print(count)        
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [ int(next(tokens)) for _ in range(N+1) ]  # type: "List[int]"
    B = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, A, B)

if __name__ == '__main__':
    main()

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

Submission status: Accepted

Solution code:
n=int(input())
a=input().split()
for i in range(n+1):
    a[i]=int(a[i])
b=input().split()
for i in range(n):
    b[i]=int(b[i])
c=0
for i in range(n):
    if a[i]<=b[i]:
        c+=a[i]
        b[i]-=a[i]
        a[i]=0
        if a[i+1]<=b[i]:
            c+=a[i+1]
            a[i+1]=0
        else:
            c+=b[i]
            a[i+1]-=b[i]
    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()))
B.append(0)
mae=0
kazu=0
for i in range(N+1):
    if A[i]<=B[i]+mae:
        kazu+=A[i]
        if mae>A[i]:
            mae=B[i]
        else:
            mae=B[i]+mae-A[i]
    else:
        kazu+=B[i]+mae
        mae=0
print(kazu)

Submission status: Accepted

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

s = sum(A)
for i,v in enumerate(B):
    n = min(v, A[i])
    m = v - A[i] if v > A[i] else 0
    A[i] = A[i] - n
    A[i + 1] = A[i+1] - min(m, A[i+1])

e = sum(A)

print(s-e)

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

Submission status: Accepted

Solution code:
# coding: utf-8
n = int(input())
m = list(map(int, input().split()))
y = list(map(int, input().split()))

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

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
an = [int(i) for i in input().split()]
bn = [int(i) for i in input().split()]+[0]
temp = 0
ans = 0
for i in range(n+1):
  if an[i] >= temp:
    an[i] -= temp
    ans += temp
  else:
    ans += an[i]
    an[i] = 0
  if an[i] >= bn[i]:
    temp = 0
    ans += bn[i]
  else:
    temp = bn[i]-an[i]
    ans += an[i]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

n= int(sys.stdin.readline())
a= [ int(x) for x in sys.stdin.readline().split() ]
b= [ int(x) for x in sys.stdin.readline().split() ]

c= 0
r= a[0]
for i in range(n):
    m = r + a[i+1]
    bi= b[i]
    c+= min(m,bi)
    #
    if bi<r: r= a[i+1]
    elif bi<m: r= m - bi
    else: r=0
#
print( c )

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

Submission status: Accepted

Solution code:
import sys


def main():
    n_list = [int(s) for s in sys.stdin.readline().strip().split()]
    n = n_list[0]
    a = [int(s) for s in sys.stdin.readline().strip().split()]
    b = [int(s) for s in sys.stdin.readline().strip().split()]

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

count = 0
for i in range(n-1,-1,-1):
    k1 = min(a[i+1],b[i])
    b[i] = b[i]-k1
    k2 = min(a[i],b[i])
    a[i] = a[i]-k2
    count += (k1 + k2)
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):
    ans += min(A[i], B[i])
    num = max(0, B[i] - A[i])
    battle_num = min(A[i + 1], num)
    ans += battle_num
    A[i + 1] -= battle_num

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
hp=list(map(int,input().split()))
ak=list(map(int,input().split()))
ans=0
for i in range(n):
        if ak[i]<=hp[i]:
                ans+=ak[i]
                continue
        else:
                ak[i]-=hp[i]
                ans+=hp[i]
        ans+=min(ak[i],hp[i+1])
        hp[i+1]=max(0,hp[i+1]-ak[i])
print(ans)

Submission status: Accepted

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

def resolve():
    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):
        st = B[i]
        a = A[i+1]
        b = A[i]
        if st <= a:
            A[i+1] -= st
            ans += st
            st = 0
        else:
            ans += A[i+1]
            st -= A[i+1]
            A[i+1] = 0

        if st <= b:
            A[i] -= st
            ans += st
            st = 0
        else:
            ans += A[i]
            st -= A[i]
            A[i] = 0

    print(ans)


if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

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

print(max_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-1):
    if a[i]<b[i]:
        tmp=b[i]-a[i]
        ans+=a[i]
        if tmp<a[i+1]:
            ans+=tmp
            a[i+1]-=tmp
            
        else:
            ans+=a[i+1]
            a[i+1]=0
            
    else:
        ans+=b[i]
#print(ans)
#print(a)
hoge=a[-1]+a[-2]
#print(hoge)
y=b[-1]
#print(y)
if hoge>y:
    ans+=y
else:
    ans+=hoge
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 in range(N):
    a = A[i] - B[i]
    if a >= 0:
        count += B[i]
    else:
        count += A[i]
        if A[i + 1] + A[i] - B[i] > 0:
            count += B[i] - A[i]
        else:
            count += A[i + 1]
        A[i + 1] = max(0, A[i + 1] + A[i] - B[i])

print(count)

Submission status: Accepted

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

cnt = 0
next = A[0]

for i in range(N) :
    buf = B[i] - next
    if buf <= 0 :
        cnt += B[i]
        next = A[i+1]
        continue
    cnt += next
    buf2 = buf - A[i+1]
    if buf2 <= 0 :
        cnt += buf
        next = A[i+1] - buf
        continue
    else :
        cnt += A[i+1]
        next = 0

print(cnt)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n = int(input())
    a = [int(s) for s in input().split()]
    b = [int(s) for s in input().split()]
    ans = 0
    if b[0] < a[0]:
        ans += b[0]
        a[0] -= b[0]
        b[0] = 0
    else:
        ans += a[0]
        b[0] -= a[0]
        a[0] = 0
    for i in range(n - 1):
        if b[i] < a[i + 1]:
            ans += b[i]
            a[i + 1] -= b[i]
            b[i] = 0
        else:
            ans += a[i + 1]
            b[i] -= a[i + 1]
            a[i + 1] = 0
        if b[i + 1] < a[i + 1]:
            ans += b[i + 1]
            a[i + 1] -= b[i + 1]
            b[i + 1] = 0
        else:
            ans += a[i + 1]
            b[i + 1] -= a[i + 1]
            a[i + 1] = 0
    if b[n - 1] < a[n]:
        ans += b[n - 1]
        a[n] -= b[n - 1]
        b[n - 1] = 0
    else:
        ans += a[n]
        b[n - 1] -= a[n]
        a[n] = 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()))

killed = 0
remain = 0
b.insert(0, 0)
for monstar, hero in zip(a[::-1], b[::-1]):
    if monstar <= remain:
        killed += monstar
        monstar = 0
    else:
        killed += remain
        monstar -= remain

    if monstar <= hero:
        killed += monstar
        remain = hero - monstar
    else:
        killed += hero
        remain = 0

print(killed)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    [N] = string_to_int(inputs[0])
    A = string_to_int(inputs[1])
    B = string_to_int(inputs[2])

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

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

    return count


def string_to_int(string):
    return list(map(lambda x: int(x), string.split()))


if __name__ == "__main__":
    ret = solve(inputs(3))
    print(ret)

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

Submission status: Accepted

Solution code:
N = int(input())
A = []
B = []
num = 0

for a in input().split():
	A.append(int(a))
for b in input().split():
	B.append(int(b))

while len(B) > 0:
	a = A.pop()
	b = B.pop()
	if a >= b:
		num += b
	else:
		num += a
		b -= a
		if A[len(A)-1] >= b:
			A[len(A)-1] -= b
			num += b
		else:
			num += A[len(A)-1]
			A[len(A)-1] = 0
	#print(num,A,B)

print(num)

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

print(count)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
A = np.array(input().split(), dtype=np.int64)
B = np.array(input().split(), dtype=np.int64)
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(int(ans))

Submission status: Accepted

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

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

    n = 0
    b0 = 0

    for a, b in zip(A[:-1], B):
        n2 = min(a, b0)
        n += n2

        a -= n2
        n3 = min(a, b)
        n += n3

        b0 = b - n3

    n += min(b0, A[-1])
    print(n)




if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = [0]
at = list(map(int, input().split()))
a.extend(at)
b = [0]
bt = list(map(int, input().split()))
b.extend(bt)
b.append(0)
ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
import io
import sys


def IN_S():
  return input()


def IN_I():
  return int(input())


def IN_L_I():
  return list(map(int, input().split()))


def IN_L_S():
  return list(map(str, input().split()))


def STR_SPLIT(s, n):
  for l in range(0, len(s), n):
    yield s[0 + l:n + l]


def T_IN():
  global test_str
  sys.stdin = io.StringIO(test_str[1:-1])


test_str = '''
2
100 1 1
1 100
'''


def MAIN():
  # T_IN()
  A()


def A():
  n = IN_I()
  al = IN_L_I()
  bl = IN_L_I()

  killed = 0
  for i, b in enumerate(bl):
    a1 = al[i] - b
    if a1 >= 0:
      killed += b
      continue

    killed += al[i]

    b = b - al[i]
    a2 = al[i + 1] - b
    if a2 < 0:
      killed += al[i + 1]
      al[i + 1] = 0
    else:
      killed += b
      al[i + 1] -= b

  print(killed)
  return None


def B():
  return None


def C():
  return None


MAIN()

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

def get_num():
    return int(input())


def get_nums(n):
    res = [0 for _ in range(n)]
    for i in range(n):
        res[i] = int(input())
    return res


def get_numbers():
    return map(int, input().split())


n = get_num()
a = list(reversed(list(get_numbers())))
b = list(reversed(list(get_numbers())))

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

    cost = min(a[i + 1], b[i])
    a[i + 1] -= cost
    ans += cost

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

sum = 0

for i in range(n):
    ind = n - i - 1
    if b[ind] <= anext:
        sum += b[ind]
        anext = a[ind]
        continue
    elif (b[ind] > anext) and (b[ind] < anext + a[ind]):
        sum += b[ind]
        anext = anext + a[ind] - b[ind]
        continue
    else:
        sum += anext + a[ind]
        anext = 0

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 n in range(N):
    if A[n]>=B[n]:
        ans += B[n]
    elif A[n+1]>=B[n]-A[n]:
        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 = list(map(int, input().split()))
B_list = list(map(int, input().split()))

counter = 0

for i in range(N):
    if B_list[i] <= A_list[i]:
        # B_list[i] = B_list[i] - A_list[i]
        counter += B_list[i]
    
    else:
        if B_list[i] <= A_list[i] + A_list[i+1]:
            A_list[i+1] = A_list[i+1] - (B_list[i] - A_list[i])
            counter += B_list[i]

        else:
            counter += A_list[i] + A_list[i+1]
            A_list[i+1] = 0

print(counter)

Submission status: Accepted

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

def main():
    N = int(input())
    A = [0] + [int(a) for a in input().split()]
    B = [0] + [int(b) for b in input().split()] + [0] + [0]
    
    ans = 0
    
    for i in range(1,N+2):
        for j in range(-1,1):
            kill = min(A[i],B[i+j])
            ans += kill
            A[i] -= kill
            B[i+j] -= kill
    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()))

result = sum(A)

for i in range(N):
    beat = min(A[i], B[i])
    A[i] -= beat 
    B[i] -= beat
    A[i + 1] -= min(A[i + 1], B[i])
    
result -= sum(A)

print(result)

Submission status: Accepted

Solution code:
N = int(input())
As = [int(p) for p in input().split()]
Bs = [int(p) for p in input().split()]

count = 0
remain = 0

for a, b in zip(As, Bs + [0]):
    if a >= remain:
        count += remain
        a -= remain
    else:
        count += a
        remain = b
        continue
    if a >= b:
        count += b
        remain = 0
    else:
        count += a
        remain = b - a

print(count)

Submission status: Accepted

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

initial_monsters = sum(a_list)

for index, kill in enumerate(b_list):
    if a_list[index] > kill:
        a_list[index] = a_list[index] - kill
    else:
        remain_kill = kill - a_list[index]
        a_list[index] = 0
        a_list[index + 1] = a_list[index + 1] - remain_kill
        if a_list[index + 1] < 0:
            a_list[index + 1] = 0

print(initial_monsters - sum(a_list))

Submission status: Accepted

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

amari = 0
defeat = 0
for i in range(n):
    a_ = a[i]
    b_ = b[i]
    d1 = min(a_, amari)
    d2 = min(a_-d1, b_)
    defeat += (d1 + d2)
    amari = max(b_-d2, 0)

defeat += min(amari, a[-1])
print(defeat)