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

count=0

for i in range(len(li2)):
  temp=min(li2[i],li[i])
  count+=temp
  li[i]-=temp
  li2[i]-=temp
  
  temp=min(li2[i],li[i+1])
  count+=temp
  li[i+1]-=temp
  li2[i]-=temp
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
an, bn = [list(map(int, input().split())) for i in [0, 1]]
m = 0
for i in range(n):
    c = min(an[i], bn[i])
    m += c
    an[i] -= c
    bn[i] -= c
    n = min(an[i+1], bn[i])
    m += n
    an[i+1] -= n
    bn[i] -= n
print(m)

Submission status: Accepted

Solution code:

if __name__ == "__main__":
    N = int(input())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    moncnt = 0
    for i in range(N):
        if a[i] < b[i]:
            moncnt += min(b[i],a[i])
            b[i] -= a[i]
            if b[i] < 0:
                b[i] = 0
            moncnt += min(a[i+1],b[i])
            a[i+1] -= min(a[i+1],b[i])
        else:
            moncnt += b[i]
    print(moncnt)

Submission status: Accepted

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

cnt = 0
okobore = 0
for i in range(N):
    if (a[i] - okobore) > 0:
        cnt = cnt + okobore
        nokori = a[i] - okobore
    if (a[i] - okobore) <= 0:
        cnt = cnt + a[i]
        nokori = 0

    if (b[i] - nokori) > 0:
         cnt = cnt + nokori
         okobore = b[i] - nokori
    if (b[i] - nokori) <= 0:
        cnt = cnt + b[i]
        okobore = 0

if a[N] - okobore > 0:
    cnt = cnt + okobore
else:
    cnt = cnt + a[N]

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
B = [int(y) for y in input().split()]
C = 0
B.append(0)
ans = 0
for i in range(N+1):
    if A[i]<=B[i]+C:
        ans = ans + A[i]
        if C >= A[i]:
            C = B[i]
        else:
            C = B[i] + C - A[i]
    else:
        ans = ans + B[i] + C
        C = 0
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
list_A = list(map(int, input().split()))
list_B = list(map(int, input().split()))
list_ans = []

for i in range(N):
    r = list_A[i] + list_A[i+1] - list_B[i]
    list_ans.append(list_B[i] if r >= 0 else list_A[i] + list_A[i+1])
    if r < 0:
        list_A[i+1] = 0
    elif list_A[i] < list_B[i]:
        list_A[i+1] = r

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(x) for x in input().split()]
b=[int(x) for x in input().split()]
t=0

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

print(t)

Submission status: Accepted

Solution code:
import sys
import numpy as np


def solve(A, B, N):
    res = 0
    n = 0
    for i in range(N):
        x = min(A[i], B[i] + n)
        res += x
        n = min(B[i] + n - x, B[i])
    res += min(A[N], n)
    return res


line = sys.stdin.readline
N = int(line())
A = np.fromstring(line(), dtype=np.int64, sep=' ')
B = np.fromstring(line(), dtype=np.int64, sep=' ')
print(solve(A, B, N))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ax=[a[i] for i in range(n+1)]

ans=0

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

print(ans)

Submission status: Accepted

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

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

cnt = 0

for i in range(len(b)):

    if b[i] <= a[i]:        # 街のモンスターの方が多い場合
        cnt += b[i]
        a[i] -= b[i]
        b[i] = 0 


    else:                   # 街のモンスターの方が少ない場合
        cnt += a[i]
        b[i] -= a[i]
        a[i] = 0

        # 隣街のモンスターを倒しに行く
        if b[i] <= a[i+1]:  # 隣町のモンスターが多い場合
            a[i+1] -= b[i]
            cnt += b[i]
            b[i] = 0
        else:               # 隣町のモンスターが少ない場合
            cnt += a[i+1]
            b[i] -= a[i+1]
            a[i+1] = 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()]

defect_cnt = 0
cooperation = 0
B.append(0)

for i in range(len(A)):
    if A[i] < cooperation:
        defect_cnt += A[i]
        A[i] = 0
    else:
        defect_cnt += cooperation
        A[i] -= cooperation

    cooperation = 0

    if A[i] < B[i]:
        defect_cnt += A[i]
        cooperation = B[i] - A[i]
    else:
        defect_cnt += B[i]

print(str(defect_cnt))

Submission status: Accepted

Solution code:
n1 = int(input())
n2 = [int(i) for i in input().split()]
n3 = [int(j) for j in input().split()]

c1 = 0
s1 = 0
for i1 in range(n1 + 1):
    if i1 == n1:
        if c1 >= n2[i1]:
            s1 += n2[i1]
        else:
            s1 += c1
    elif c1 >= n2[i1]:
        s1 += n2[i1]
        c1 = n3[i1]
    else:
        if (c1 + n3[i1]) >= n2[i1]:
            s1 += n2[i1]
            c1 = (c1 + n3[i1]) - n2[i1]
        else:
            s1 += (c1 + n3[i1])
            c1 = 0

print(s1)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
count = 0
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

for i in range(n):
    if a[i] > b[i]:#倒しきれない
        count = count + b[i]
    else:
        if a[i+1] - (b[i] - a[i]) < 0:
            count = count + a[i] + a[i+1]
            a[i+1] = 0
        else:
            count = count + a[i] + (b[i] - a[i])
            a[i+1] = a[i+1] - (b[i] - a[i])
             
    
print(count)

Submission status: Accepted

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

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

Submission status: Accepted

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


def solve(N: int, A: "List[int]", B: "List[int]"):
    ans = 0

    # Bを配る
    for i in range(N):
        # A[i] に対してB[i]を配る
        # モンスターのほうが多い
        if(B[i] <= A[i]):
            ans += B[i]
            A[i] -= B[i]
            B[i] = 0
        # 勇者のほうが多い
        else:
            ans += A[i]
            B[i] -= A[i]
            A[i] = 0

        # A[i+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

    print(ans)

    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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(x) for x in input().split()]#町のモンスターの数
b=[int(x) for x in input().split()]#それぞれが倒せる敵の数
count=0
for i in range(n):
    if a[i]<=b[i]:
        count+=a[i]
        b[i]-=a[i]
        a[i]=0
        if a[i+1]<=b[i]:
            count+=a[i+1]
            b[i]-=a[i+1]
            a[i+1]=0
        else:
            count+=b[i]
            a[i+1]-=b[i]
            b[i]=0
    else:
        count+=b[i]
        a[i]-=b[i]
        b[i]=0
print(count)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
monster = list(map(int, input().split(' ')))
hero = tuple(map(int, input().split(' ')))

total = 0
for i in range(N):
    n = min(monster[i], hero[i])
    n_1 = min(monster[i+1], hero[i] - n)
    monster[i+1] -= n_1
    total += n + n_1
print(total)

Submission status: Accepted

Solution code:
n=int(input())

#n+1つの街にそれぞれいるモンスターの数のリスト
mnsnum=[int(i) for i in input().split()]

#n人の勇者
brave=[int(i) for i in input().split()]

sumnum=0
for i in range(n):
    bravepower=brave[i]
    if bravepower<=mnsnum[i]:
        mnsnum[i]=mnsnum[i]-bravepower
        sumnum+=bravepower
    else:
        sumnum+=mnsnum[i]
        bravepower-=mnsnum[i]
        mnsnum[i]=0
        if mnsnum[i+1]<=bravepower:
            sumnum+=mnsnum[i+1]
            mnsnum[i+1]=0
        else:
            mnsnum[i+1]=mnsnum[i+1]-bravepower
            sumnum+=bravepower
        
        



print(sumnum)

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
n = int(input())
aList = list(map(int, input().split()))
bList = list(map(int , input().split()))

deathSum = 0

for index in range(len(aList)-1):
    if(aList[index] < bList[index]):
        # a < bなら討伐数にaを加算する
        # さらに次のindexのaListを調査する
        deathSum += aList[index]
        bList[index] = bList[index] - aList[index]
        if(aList[index+1] > bList[index]):
            deathSum += bList[index]
            aList[index+1] -= bList[index]
        else:
            # a+1 < b
            deathSum += aList[index+1]
            aList[index+1] = 0
    else:
        # a >= bなら討伐数にbを加算する
        deathSum += bList[index]

print(deathSum)

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):
    a0 = min(A[i], B[i])
    B[i] -= a0
    A[i] -= a0

    a1 = min(A[i+1], B[i])
    B[i] -= a1
    A[i+1] -= a1

    ans += (a0 + a1)

print(ans)

Submission status: Accepted

Solution code:
# n = int(input()) 
# t = [int(input()) for i in range(n)] 

N = int(input())
B = list(map(int, input().split()))
A = list(map(int, input().split()))
b = len(B)
count = 0
for i in range(N):
    if (A[i]==B[i]):
        count +=A[i]
    elif (A[i]>B[i]):
        count +=B[i]
        A[i]=A[i]-B[i]
        if (i<b and A[i]==B[i+1]):
            count +=A[i] 
            B[i+1]=0
        elif (i<b and A[i]>B[i+1]):
            count +=B[i+1]
            B[i+1]=0
        elif(i<b):
            p = B[i+1]-A[i]
            count +=A[i]
            B[i+1]=p
    else:
        p = B[i]-A[i]
        count +=A[i]

print(count)



# count = 0
#a, b= (int(i) for i in input().split())

Submission status: Accepted

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

count = 0

for i,b in enumerate(Bs):
    if As[i] - b >= 0:
        count += b
    else:
        if As[i+1] + As[i] - b >= 0:
            count += b
            As[i+1] -= b-As[i]
        else:
            count += As[i+1] + As[i]
            As[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()))
if a[0] <= b[0]:
    ans = a[0]
    count = b[0] - a[0]
else:
    ans = b[0]
    count = 0
for i in range(1,n):
    if count + b[i] >= a[i]:
        ans = ans + a[i]
        if count >= a[i]:
            count = b[i]
        else:
            count = b[i] + count - a[i]
    else:
        ans = ans + count + b[i]
        count = 0
if a[n] <= count:
    ans = ans + a[n]
else:
    ans = ans + count
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):
    if a[i] < b[i]:
        count += a[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:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))

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

Submission status: Accepted

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

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

Submission status: Accepted

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

c = []
d = []
e = []
b.append(0)
for i in range(n+1):
    if i == 0:
        c.append(0)
        d.append(b[0])
    else:
        c.append(b[i-1] - max(e[i-1] - c[i-1], 0))
        d.append(b[i] + c[i])
    e.append(min(a[i], d[i]))

print(sum(e))

Submission status: Accepted

Solution code:
num = int(input())
c = input().split()
h = input().split()

for i in range(len(c)):
  c[i] = int(c[i])
for i in range(len(h)):
  h[i] = int(h[i])
  
sumc = sum(c)
for i in range(len(h)):
  c[i] -= h[i]
  h[i] = abs(c[i])
  if c[i] < 0:
    c[i]=0
    c[i+1] -= h[i]
    if c[i+1] < 0:
      c[i+1]=0
      
sumedc = sum(c)
print(sumc-sumedc)

Submission status: Accepted

Solution code:
import copy
N = int(input())
monster = list(map(int,input().split()))
warior = list(map(int,input().split()))
_monster = copy.copy(monster)

for index in range(N):
  x = warior[index]
  monster[index] = monster[index] - x
  if monster[index] < 0:
    monster[index+1] = monster[index+1] + monster[index]
    monster[index] = 0
    if monster[index+1] < 0:
      monster[index+1] = 0

print(sum(_monster)-sum(monster))

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]

knock_down = [0] * (n + 1)

for i in range(n):
    power = b[i]

    # まずはi番目の街のモンスターをできる限り倒す
    if(power >= a[i] - knock_down[i]):
        power -= (a[i] - knock_down[i])
        knock_down[i] = a[i]
    else:
        knock_down[i] += power
        continue

    if(power == 0):
        continue

    # 次に、残った力でi+1番目のモンスターをできる限り倒す
    if(power >= a[i+1]):
        knock_down[i+1] = a[i+1]
    else:
        knock_down[i+1] = power

print(sum(knock_down))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding:utf-8

n = int(input())
town_list = [int(x) for x in input().split(' ')]
saver_list = [int(x) for x in input().split(' ')] + [0]

count = 0

for i, town in enumerate(town_list):
    if town_list[i] >= saver_list[i]:
        count += saver_list[i]
    else:
        count += town_list[i]
        if not i+1 == len(saver_list):
            count += min(town_list[i+1], saver_list[i] - town_list[i])
            town_list[i+1] = max(town_list[i+1] - (saver_list[i] - town_list[i]), 0)

print(count)

Submission status: Accepted

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

remain = 0
kill = 0
for i in range(N):
    kill += min(A[i], B[i] + remain)
    remain = max(0,min(B[i], B[i] + remain - A[i]))
if remain > 0:
    kill += min(remain, A[N])
print(kill)

Submission status: Accepted

Solution code:
monster_size = int(input())
monster = list(map(int, input().split()))
hero = list(map(int, input().split()))

monster_all = sum(monster)

for i in range(monster_size):
    monster[i] = monster[i] - hero[i]
    if monster[i] < 0:
        monster[i+1] += monster[i]
        if monster[i+1] < 0:
            monster[i+1] = 0
        monster[i] = 0

print(monster_all - sum(monster))

Submission status: Accepted

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
s = [0 for x in a] + [0]

r = 0
for i in range(n):
    s[i] = min(a[i], b[i] + r)
    r = min(b[i], b[i] + r - s[i])
else:
    s[n] = min(a[n], r)

print(sum(s))

Submission status: Accepted

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

a = [int(i) for i in a]
b = [int(i) for i in b]

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

print(e)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
 
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
M = 0
 
for i in range(N):
	if A[i] > B[i]:
	
		M = B[i] + M
	
	elif B[i] > A[i] + A[i+1]:
 
		M = A[i] + A[i+1] + M
		A[i+1] = 0
 
	else:
 
		M = A[i] + min(B[i]-A[i],A[i+1]) + M
		A[i+1] = A[i+1] - (B[i] - A[i])
 
print(M)

Submission status: Accepted

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

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

score += min(remain, a[-1])
print(score)

Submission status: Accepted

Solution code:
n = int(input())

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

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

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

Submission status: Accepted

Solution code:
def fight(no_mons, fighter, remain = 0):
    no_mons -= remain
    if no_mons < 0:
        return [0, fighter]
    no_mons -= fighter
    if no_mons < 0:
        return [0, abs(no_mons)]
    else:
        return [no_mons, 0]

n = int(input())
raw_town = input().split()
raw_fighter = input().split()
town = [int(x) for x in raw_town]
fighter = [int(x) for x in raw_fighter]
sum = 0
fighter_remain = 0
for i in range(n):
    mons_remain, fighter_remain = fight(town[i], fighter[i], fighter_remain)
    sum += town[i] - mons_remain
last_mons_remain = fight(town[n], fighter_remain)[0]
sum += town[n]- last_mons_remain
print(sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve(n, a, b):
    ans = 0
    for i in range(n):
        d = min(b[i], a[i])
        ans += d
        d = min(b[i]-d, a[i+1])
        ans += d
        a[i+1] -= d
    return ans

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

Submission status: Accepted

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

ap=al[0]
i=0
sum=0
for a in al[1:]:
    y = bl[i]

    m1 = min([ap, y])
    sum += m1

    y1 = y - m1
    if y1 < 0:
        y1 = 0

    m2 = min([a, y1])
    sum += m2

    ap = a - y1
    if ap < 0:
        ap = 0

    i += 1

print(sum)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n = int(input())
    al = [int(a) for a in input().split()]
    bl = [int(b) for b in input().split()]

    score = 0
    remaining = 0
    for i in range(n):
        a = al[i]
        b = bl[i]
        if a >= b:
            score += b
        else:
            score += a
            b -= a
            a2 = al[i + 1]
            if a2 > b:
                score += b
                a2 -= b
                al[i + 1] = a2
            else:
                score += a2
                al[i + 1] = 0
    print(score)

Submission status: Accepted

Solution code:
n = int(input(''))
xs = list(map(int, input('').split(' ')))
ys = list(map(int, input('').split(' ')))

c = 0
i = 0
while i < len(ys):
    c += min(xs[i], ys[i])
    ys[i] -= xs[i]
    if ys[i] > 0:
        c += min(xs[i + 1], ys[i])
        xs[i + 1] -= ys[i]
        if xs[i + 1] < 0:
            xs[i + 1] = 0
    i += 1

print(c)

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

Submission status: Accepted

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

count = 0
for val in range(n):
    
    if a[val] >=  b[val]:
        count += b[val]
        a[val] = a[val] - b[val]
        b[val] = 0
        
    elif a[val] < b[val]:
        count += a[val]
        b[val] = b[val] - a[val]
        
        if a[val+1] >=  b[val]:
            count += b[val]
            a[val+1] = a[val+1] - b[val]
            b[val] = 0
            
        
        elif a[val+1] < b[val]:
            count += a[val + 1]
            a[val+1] = 0
        #print(count)
print(count)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N=int(input())
alist=list(map(int,input().split()))
blist=list(map(int,input().split()))

answer=0
for i in range(N):
  dmg=min(alist[i],blist[i])    
  alist[i]-=dmg
  blist[i]-=dmg
  answer+=dmg

  dmg=min(alist[i+1],blist[i])
  alist[i+1]-=dmg
  blist[i]-=dmg
  answer+=dmg
    
print(answer)

Submission status: Accepted

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

ans = 0

for i in reversed(range(n+1)):
  if i == n:
    temp = min(a[i], b[i-1])
    ans += temp
    b[i-1] -= temp
  elif i == 0:
    ans += min(a[i], b[i])
  else:
    temp1 = min(a[i], b[i])
    ans += temp1
    a[i] -= temp1
    temp2 = min(a[i], b[i-1])
    ans += temp2
    b[i-1] -= temp2
print(ans)

Submission status: Accepted

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

ans = 0

for i in range(n):
    if b_list[i] > a_list[i]:
        ans += a_list[i]
        b_list[i] -= a_list[i]
        if b_list[i] > a_list[i + 1]:
            ans += a_list[i + 1]
            a_list[i + 1] = 0
        else:
            ans += b_list[i]
            a_list[i + 1] -= b_list[i]
    else:
        ans += b_list[i]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a, b = [list(map(int, input().split())) for _ in range(2)]
count = 0
for i in range(len(b)):
    if b[i] > a[i]:
        d = b[i] - a[i]
        count += a[i]
        if d > a[i+1]:
            count += a[i+1]
            a[i + 1] = 0
        else:
            a[i+1] -= d
            count += d

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

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

Submission status: Accepted

Solution code:
n = int(input())
A= list(map(int, input().split()))
B= list(map(int, input().split()))
r = 0
for i, b in enumerate(B):
    if b <= A[i]:
        r += b
    else:
        b -= A[i]
        r += A[i]
        if b <= A[i+1]:
            A[i+1] -= b
            r += b
        else:
            r += 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()))
count=0
post=0
for i in range(N):
    if B[i]-A[i]>0:
        count +=A[i]
        if A[i+1]-(B[i]-A[i])<=0:
            count+=A[i+1]
            A[i+1]=0
        else:
            A[i + 1] -= B[i] - A[i]
            count+=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(' ')))

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

Submission status: Accepted

Solution code:
n  = int(input())
xs = [int(x) for x in input().split(" ")]
ys = [int(x) for x in input().split(" ")]
r  = 0
c  = 0

for i in range(0, n):
	a  = min(xs[i], r)
	b  = min(xs[i] - a, ys[i])
	r  = ys[i] - b
	c += a + b

c += min(xs[n], r)

print(c)

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