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 = [int(_) for _ in input().split()]
b = [int(_) for _ in input().split()]
res = 0
for i in range(n):
    j = n-1-i
    t = min(a[j+1],b[j])
    res += t
    b[j]-= t
    s = min(a[j],b[j])
    res += s
    a[j]-= s
print(res)

Submission status: Accepted

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

ans = 0
for i in xrange(n):
    can = b[i]
    x1 = min(a[i], can)
    a[i] -= x1
    can -= x1
    ans += x1

    x2 = min(a[i + 1], can)
    a[i + 1] -= x2
    can -= x2
    ans += x2

print ans

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

Submission status: Accepted

Solution code:
n = int(input())
cities = list(map(int,input().split()))
braves = list(map(int,input().split()))
braves.append(0)
count = 0
for i in range(n+1):
  c = min(braves[i],cities[i])
  count += c
  if i != n and c != braves[i]:
    d = min(braves[i]-c, cities[i+1])
    count += d
    cities[i+1] -= d
print(count)

Submission status: Accepted

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

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

cnt = 0

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

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())

a = [] #monster
a_li = input().split()

b = []
b_li = input().split()

for i in range(n):
    a.append(int(a_li[i]))
    b.append(int(b_li[i]))
a.append(int(a_li[n]))

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
mon = list(map(int, input().split()))
bra = list(map(int, input().split()))

sup = 0
total = 0
for i in range(n):
    spec = bra[i]+sup
    if mon[i]>spec:
        total += spec
        sup = 0
    else:
        total += mon[i]
        if sup>=mon[i]:
            sup = bra[i]
        else:
            sup = spec - mon[i]

if (sup>mon[n]):
    total += mon[n]
else:
    total += sup

print(total)

Submission status: Accepted

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

sum_A = sum(A)

for n in reversed(range(N)):
    beated = min(B[n], A[n+1])
    B[n] -= beated
    A[n+1] -= beated

    beated = min(B[n], A[n])
    B[n] -= beated
    A[n] -= beated

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()))
power = sum(B)
def draw(a, b):
    ba = a-b
    if ba < 0 :
        return 0, -ba
    else:
        return ba, 0

for i in range(N):
    B[i],A[i] = draw(B[i], A[i])
    A[i+1], B[i] = draw(A[i+1],B[i])
print(power - sum(B))

Submission status: Accepted

Solution code:
town_num = int(input())

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

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

counter = 0
left_num = 0
for i,(one,two) in enumerate(zip(monster_num,hero_power)):
    counter += min(two+left_num,one)
    left_num = min(max(0,two+left_num-one),two)
counter += min(monster_num[-1],left_num)

print(counter)

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

print(count)

Submission status: Accepted

Solution code:
n=int(input())  #数値入力 「N」だけの入力のとき
#n=2
a=list(map(int, input().split()))  #リスト入力 「a1 a2 a3 ...」みたいな配列のような入力のとき
#a = [100, 1, 1]
b=list(map(int, input().split()))  #リスト入力 「a1 a2 a3 ...」みたいな配列のような入力のとき
#b=[1,100]
ans = 0

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

Submission status: Accepted

Solution code:
N = int(input())
A_list = list(map(int,input().split()))
B_list = list(map(int,input().split()))

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

'''
B_odd = B_list[N-1] - A_list[N-1]
if A_list[N] > B_odd:
    result += B_odd
    print(B_odd)
else:
    result += A_list[N]
    print(A_list[N])
'''
print(result)

Submission status: Accepted

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

Submission status: Accepted

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

beats = list()
count = 0
for i in range(n):
    beat = min(Bs[i], As[i])
    count += beat
    Bs[i] -= beat
    As[i] -= beat

    beat = min(Bs[i], As[i + 1])
    count += beat
    Bs[i] -= beat
    As[i + 1] -= beat

    # print(As)
    # print(Bs)
    
print(count)
# print(beats)

# if As[n] > Bs[n - 1]:
#     beat = Bs[n - 1]
# else:
#     beat = As[n]

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split(" ")))
b = list(map(int,input().split(" ")))
fb = sum(b)
for i in range(n):
  tmp = min(a[i],b[i])
  a[i] -= tmp
  b[i] -= tmp
  tmp = min(a[i+1],b[i])
  a[i+1] -= tmp
  b[i] -= tmp
result = fb - sum(b)
print(result)

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

Submission status: Accepted

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

rest = 0
c = 0
for i in range(len(Bs)):
    if As[i] >= rest:
        ene = As[i] - rest
        c += rest
        if ene >= Bs[i]:
            c += Bs[i]
            rest = 0
        else:
            c += ene
            rest = Bs[i] - ene
    else:
        c += As[i]
        rest = Bs[i]
if As[i+1] >= rest:
    c += rest
else:
    c += As[i+1]
print(c)

Submission status: Accepted

Solution code:
n = int(input())
la = list(map(int, input().split()))
lb = list(map(int, input().split()))

c = 0
for i in range(n):
    m = la[i] + la[i+1]
    if la[i] >= lb[i]:
        c += lb[i]
    elif m > lb[i]:
        la[i+1] = m - lb[i]
        c += lb[i]
    else:
        la[i+1] = 0
        c += m

print(c)

Submission status: Accepted

Solution code:
N=int(input())
An=list(map(int,input().split()[:N+1]))
Bn=list(map(int,input().split()[:N]))

ret=0

for i in range(N,0,-1):
    tmp=Bn[i-1]
    if An[i] >= tmp:
        An[i]-=tmp
        ret+=tmp
        continue
    else:
        tmp-=An[i]
        ret+=An[i]
        An[i]=0
        if An[i-1]>=tmp:
            An[i-1]-=tmp
            ret+=tmp
        else:
            ret+=An[i-1]
            An[i-1]=0
print(ret)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

s = sum(A)
for i in range(N):
    m = min(A[i], B[i])
    A[i]-=m
    A[i+1]-=min(A[i+1], (B[i]-m))
    
print(s-sum(A))

Submission status: Accepted

Solution code:
N=int(input())

A= list(map(int,input().split()))
B=list(map(int,input().split()))
C=[]
D=[]
count=0
j=A[0]
for i in range(N):

    if A[i]>=B[i]:
        A[i]=A[i]-B[i]
        count+=B[i]

    else:
        if A[i+1]+A[i]>=B[i]:
            A[i+1]=A[i+1]-(B[i]-A[i])
            A[i]=0
            count+=B[i]
        else:

            count+=A[i]+A[i+1]
            A[i + 1] = 0
            A[i] = 0

print(count)

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

n = INT()
A = LIST()
B = LIST()
ans = sum(A)
for i in range(n):
    # while A[i] > 0:
    if A[i] <= B[i]:
        B[i] = B[i] - A[i]
        A[i] = 0

        # print(A)
        # print(B[i] - A[i])
        # print(B[i])
        # print(B)
        if A[i+1] <= B[i]:
            B[i] = B[i] - A[i+1]
            A[i+1] = 0

        else:
            A[i+1] = A[i+1] - B[i]
            B[i] = 0
            # print(A)
    else:
        A[i] = A[i] - B[i]
        B[i] = 0
        if A[i+1] <= B[i]:
            B[i] = B[i] - A[i+1]
            A[i+1] = 0

        else:
            A[i+1] = A[i+1] - B[i]
            B[i] = 0
    # print(A)
# print(A)
# print(A)
ans = ans-sum(A)
print(ans)

    





    # if A[i] > 0:
    #     B[i] -= 1
    #     A[i] -= 1
        
    # while(B[i] >= 0):
    #     B[i] = B[i] - A[i]

# for i in range(n):
#     if A[i] <= A[i+1]:
#         t = A[i+1]
#         c = A[i+1] - B[i]
#         c = max(0, c)
#         A[i+1] = c
#     else:
#         t = A[i]
#         c = A[i] - B[i]
#         c = max(0, c)
#         A[i] =c
# print(A)

Submission status: Accepted

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

sum_beat = 0

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

print(sum_beat)

Submission status: Accepted

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

cnt = min(A[1],B[1])
x = max(B[1]-A[1],0)
for i in range(2,N+1):
    cnt += min(A[i],x+B[i])
    x = max(B[i]-max(A[i]-x,0),0)
cnt += min(A[N+1],x)
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(len(A)-1):
    if A[i] >= B[i]:
        cnt += B[i]
    else:
        cnt += A[i]
        B[i] -= A[i] 
        if A[i+1] >= B[i]:
            cnt += B[i]
            A[i+1] -= B[i]
        else:
            cnt += A[i+1]
            A[i+1] = 0

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())

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

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

Submission status: Accepted

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

_ans = 0
_buff = 0

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

    # print('ans2')
    # print(_ans)
    _buff = 0
    if i < N:
        if A[i]-B[i] > 0:
            _ans += B[i]
        else:
            _ans += A[i]
            _buff = B[i]-A[i]
    # print('ans')
    # print(_ans)
    # print('buff')
    # print(_buff)


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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

p = 0
ans = 0

for i in range(N):
    if A[i] <= p:
        ans += A[i]
        p = B[i]
    elif A[i] <= B[i]+p:
        ans += A[i]
        p = B[i]-(A[i]-p)
    else:
        ans += B[i]+p
        p = 0
    

if A[N] <= p:
    ans += A[N]
else:
    ans += p

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
monsters = list(map(lambda x : int(x), input().split()))
heros = list(map(lambda x : int(x), input().split()))

monster_point = 0
hero_point = 0

ans = 0

for i in range(n):
    monster1 = monsters[i]
    monster2 = monsters[i+1]
    hero1 = heros[i]
    
    if monster1 >= hero1:
        ans += hero1
    elif hero1 <= (monster1 + monster2):
        ans += hero1
        monsters[i+1] -= (hero1 - monster1)
    else:
        ans += (monster1 + monster2)
        monsters[i+1] = 0

print(ans)

Submission status: Accepted

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

score = 0

for i in range(0,N):
    # Bi のほうが多く殺せる
    if A[i] <= B[i]:
        score = score + A[i]
        B[i] = B[i] - A[i]
        # 不要だけど一応やっとく
        A[i] = 0
        
        #余力で隣を殺せる分だけ殺す
        if A[i+1] <= B[i]:
            score = score + A[i+1]
            A[i+1] = 0
            # 不要だけど一応やっとく
            B[i] = B[i] - A[i+1]
        else:
            score = score + B[i]
            A[i+1] = A[i+1] - B[i]
            B[i] = 0
        
    else:
        score = score + B[i]
        
        # 不要だけど一応やっとく
        A[i] = A[i] - B[i]
        B[i] = 0


print(score)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin

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

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

input = lambda: stdin.readline()
main()

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())

A = [int(s) for s in input().split()]
B = [int(t) for t in input().split()]
B.append(0)

A = np.array(A)
B = np.array(B)
C = B - A
dm_sum = 0

y = 0
for i in range(n+1):
    dm = C[i] + y
    if dm >= 0:
        dm_sum += A[i]
        if y >= A[i]:
            y = B[i]
        else:
            y = dm
    else:
        dm_sum += B[i] + y
        y = 0

print(dm_sum)

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:


def submit():
    n = int(input())
    alist = list(map(int, input().split()))
    blist = list(map(int, input().split()))


    # 順番にグリーディ
    a = alist.copy()
    b = blist.copy()
    score_a = 0
    for i in range(0, n):
        if a[i] < b[i]:
            score_a += a[i]
            b[i] -= a[i]
            a[i] = 0
        else:
            score_a += b[i]
            a[i] -= b[i]
            b[i] = 0

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


    print(score_a)


if __name__ == '__main__':
    submit()

Submission status: Accepted

Solution code:
n = int(input())
a_s = list(map(int, input().split()))
b_s = list(map(int, input().split()))

count = 0
next_b1_minus = 0
for a, b1, b2 in zip(a_s, [0]+b_s, b_s+[0]):
    b1 -= next_b1_minus
    if b1 >= a:
        count += a
        next_b1_minus = 0
    elif b1+b2 >= a:
        count += a
        next_b1_minus = a-b1
    else:
        count += b1+b2
        next_b1_minus = b2

print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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


tmp_ans=0

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



        if(A[i+1]<=B[i]):
            tmp_ans += A[i+1]
            B[i] = B[i]-A[i+1]
            A[i+1] = 0

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



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


print(tmp_ans)

Submission status: Accepted

Solution code:
x = int(input()) # input:"7" intを忘れない
a = [int(i) for i in input().split()] # input:"1 2 3 4 5"
b = [int(i) for i in input().split()] # input:"1 2 3 4 5"
ans=0
tmp=0
for i in range(x):
  point = 0
  tmp = b[x-i-1]-a[x-i]
  if tmp>=0:
    point = a[x-i]
    if tmp - a[x-i-1]>=0:
      point += a[x-i-1]
      a[x-i-1] = 0
    else:
      point += tmp
      a[x-i-1] = a[x-i-1] - tmp
  else:
    point = b[x-i-1]
  ans += point
print(ans)

Submission status: Accepted

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

cnt = 0
for i, b in enumerate(BS):
    a = AS[i]
    if a <= b:
        cnt += a
        s = b - a
        if s >= AS[i + 1]:
            cnt += AS[i + 1]
            AS[i + 1] = 0
        else:
            cnt += s
            AS[i + 1] -= s
    else:
        cnt += b


print(cnt)

Submission status: Accepted

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

count = 0
i = N
j = N - 1
tmpA = A[i]
tmpB = B[j]
while (i >= 0 and j >= 0):
    if tmpA - tmpB > 0:
        tmpA -= tmpB
        count += tmpB
        j -= 1
        if j >= 0:
            tmpB = B[j]
        if i - j > 1:
            i -= 1
            if i >= 0:
                tmpA = A[i]
    elif tmpB - tmpA > 0:
        tmpB -= tmpA
        count += tmpA
        i -= 1
        if i >= 0:
            tmpA = A[i]
        if i - j < 0:
            j -= 1
            if j >= 0:
                tmpB = B[j]
    else:
        count += tmpA
        i -= 1
        j -= 1
        if i >= 0 and j >= 0:
            tmpA = A[i]
            tmpB = B[j]

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()]
# print(N,A,B)
Nhunt = 0
for i in range(N):
    if A[i]>=B[i]: # Hunter is not strong enough
        Nhunt+=B[i]
    elif A[i+1]>=B[i]-A[i]:# Hunter is not strong enough to hunt all enemies.
        Nhunt+=B[i]
        A[i+1]-=B[i]-A[i]
    else:#Hunter is too strong
        Nhunt+=A[i]+A[i+1]
        A[i+1]=0

print(Nhunt)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = np.array(input().split()).astype(type(N))
B = np.array(input().split()).astype(type(N))

Knock_Down = 0

for i in range(N):
    #print("{}番目の勇者について".format(i))
    if(A[i] >= B[i] ):
        #print("{}体倒しました この勇者は終了です".format(B[i]))
        Knock_Down +=   B[i]
        A[i] -=B[i] 
        B[i] = 0

    elif(A[i] < B[i]):
        Knock_Down += A[i]
        B[i] -= A[i]
        A[i] = 0
        #print("{}体倒しました まだ余力が{}残っています".format(A[i],B[i])) 
        if(A[i + 1] < B[i]):
            #print("追加で{}体倒しました まだ余力が{}残っています".format(A[i + 1],B[i] - A[i + 1])) 
            Knock_Down += A[i + 1]
            B[i] -= A[i+1]
            A[i + 1] = 0
        elif(A[i + 1] >= B[i]):
            Knock_Down += B[i]
            A[i + 1] -= B[i]
            B[i] = 0
#print(A,B)
print(Knock_Down)

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
B+=[0]
count=0
power0=0
for n in range(N+1):
    enemy=A[n]
    power1=B[n]
    if power0+power1>=enemy:
        count+=enemy
        power0=power1-max(enemy-power0,0)
    else:
        count+=power0+power1
        power0=0
print(count)

Submission status: Accepted

Solution code:
n = int(input())
an = []
an = [int(x) for x in input().split()]
bn = []
bn = [int(x) for x in input().split()]

sum = 0
rem = an[n]
for i in range(0, n):
    j = (n - 1) - i
    if rem + an[j] <= bn[j]:
        sum += rem + an[j]
        rem = 0
    else:
        sum += bn[j]
        rem = rem + an[j] - bn[j]
        if rem > an[j]:
            rem = an[j]

print(sum)

Submission status: Accepted

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

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

print(r)

Submission status: Accepted

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


ans=0
i=0

while i!=n:
    s=[a[i],b[i]]
    a[i]=a[i]-min(s)
    b[i]=b[i]-min(s)
    ans+=min(s)

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

Submission status: Accepted

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

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

r+=min(a[i+1],k)

print r

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, b in enumerate(B):
    k = min(A[i], b)
    ans += k
    A[i] -= k
    b -= k

    k = min(A[i+1], b)
    ans += k
    A[i+1] -= k
    b -= k

print(ans)

Submission status: Accepted

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

cnt = 0
for i, b in enumerate(B):
    if A[i] < b:
        cnt += A[i]
        delta = b - A[i]
        if A[i+1] < delta:
            cnt += A[i+1]
            A[i+1] = 0
        else:
            cnt += delta
            A[i+1] -= delta
    else:
        cnt += b

print(cnt)