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:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from itertools import permutations, accumulate, combinations, combinations_with_replacement
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce
#from fractions import gcd
#from math import gcd
import sys
def I(): return int(input())
def Is(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()
def LS(): return list(input())
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(10**6)
MOD = 10**9+7
INF = float('inf')


n = I()
A = LI()
B = LI()

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
as_=list(map(int,input().split()))  # n+1
bs=list(map(int,input().split()))  # n
bf=sum(as_)
for i in range(n):
    if as_[i]-bs[i]<0:
        bs[i]-=as_[i]
        as_[i]=0
    else:
        as_[i]-=bs[i]
        bs[i]=0
    if as_[i+1]-bs[i]<0:
        as_[i+1]=0
    else:
        as_[i+1]-=bs[i]
print(bf-sum(as_))

Submission status: Accepted

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


solve()

Submission status: Accepted

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

Submission status: Accepted

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

sa = sum(A)

for i, b in enumerate(B):
    tmp = min(A[i], b)  # 削り取れる分
    A[i] -= tmp  # 削り取る
    A[i + 1] -= min(A[i + 1], b - tmp)  # i+1から削れる分はここで削っておく。b-tmpがi時点でのA[i+1]への攻撃力

print(sa - sum(A))  # 全量から削れなかった分を引く

Submission status: Accepted

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


c = []
count = 0

for i in range(n):

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

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

        cnt += A[i] + (B[i]-A[i]) 

    elif B[i] < A[i] + A[i+1] and B[i] < A[i]:
        cnt +=B[i]

print(cnt)

Submission status: Accepted

Solution code:
# C - City Savers

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

total = 0
for i in range(N):
    attack = min(A[i], B[i])
    A[i] -= attack
    B[i] -= attack
    total += attack
    if 0 < B[i]:
        attack2 = min(B[i], A[i + 1])
        A[i + 1] -= attack2
        B[i] -= attack2
        total += attack2

print(total)

Submission status: Accepted

Solution code:
n = int(input())
alis = list(map(int, input().split()))
blis = list(map(int, input().split()))
ans = 0
cross = 0
for i in range(n):
    front = min(alis[i]-cross, blis[i])
    cross = min(alis[i+1], blis[i]-front)
    ans += front + cross
print(ans)

Submission status: Accepted

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

print(count)

Submission status: Accepted

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

Submission status: Accepted

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

C=[]
for i in range(N):
    C.append(min(A[i+1]+A[i],B[i]))
    if B[i]>A[i]:
        A[i+1]=A[i+1]-(B[i]-A[i])
        if A[i+1]<0:
            A[i+1]=0

print(sum(C))

Submission status: Accepted

Solution code:
import sys

N = int(input())
A = list(map(int, sys.stdin.readline().rsplit()))
B = list(map(int, sys.stdin.readline().rsplit()))

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

print(res)

Submission status: Accepted

Solution code:
import sys, heapq, bisect, math, fractions
from collections import deque

N = int(input())
A = list(map(int, sys.stdin.readline().rsplit()))
B = list(map(int, sys.stdin.readline().rsplit()))

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

print(res)

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

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(x) for x in input().split()]
    B = [int(x) for x in input().split()]
    ans = 0
    for i in range(N):
        k = B[i] - A[i]
        if k >= 0:
            if k <= A[i + 1]:
                ans += B[i]
                A[i + 1] -= k
            else:
                ans += A[i] + A[i + 1]
                A[i + 1] = 0
        else:
            ans += 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()))
m = sum(a)
for i in range(n):
    if a[i] <= b[i]:
        b[i] -= a[i]
        a[i] = 0
    else:
        a[i] -= b[i]
        b[i] = 0
    if a[i+1] <= b[i]:
        b[i] -= a[i]
        a[i+1] = 0
    else:
        a[i+1] -= b[i]
        b[i] -= a[i+1]
print(m - sum(a))

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest

def resolve():
    n = int(input())
    aa = list(map(int, input().split()))
    bb = list(map(int, input().split()))

    ans = 0
    for i, b in enumerate(bb):
        a = aa[i]
        next_a = aa[i+1]

        if a < b:
            ans += a
            remain = b - a

            if remain <= next_a:
                ans += remain
                aa[i+1] -= remain
            else:
                ans += next_a
                aa[i+1] = 0
        elif a >= b:
            ans += b

    print(ans)


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """2
3 5 2
4 5"""
        output = """9"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """3
5 6 3 8
5 100 8"""
        output = """22"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """2
100 1 1
1 100"""
        output = """3"""
        self.assertIO(input, output)

if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Accepted

Solution code:
def resolve():
    n = int(input())
    monster = [int(i) for i in input().split()]
    damage = [int(i) for i in input().split()]

    before = sum(monster)
    for i, (mst, dmg) in enumerate(zip(monster, damage)):
        if dmg >= monster[i]:
            _dmg = dmg - monster[i]
            monster[i] = 0
            monster[i + 1] = max(monster[i + 1] - _dmg, 0)
        else:
            monster[i] = monster[i] - dmg

    after = sum(monster)
    print(before - after)


resolve()

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):
    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
ans = sum_a - sum(a)
print(ans)

Submission status: Accepted

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

sumA = sum(As)
for i in range(N):
    num = min(As[i], Bs[i])
    As[i] -= num
    Bs[i] -= num
    num = min(As[i+1], Bs[i])
    As[i+1] -= num
    Bs[i] -= num

ans = sumA - sum(As)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(s) for s in input().split()]
sum_A = sum(A)
B = [int(s) for s in input().split()]
for i in range(N):
    attack1 = A[i] - B[i]
    if attack1 < 0:
        attack2 = A[i + 1] + attack1
        A[i] = 0
        A[i + 1] = attack2 if attack2 >= 0 else 0
    else:
        A[i] = attack1

print(sum_A - sum(A))

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split(' ')))
    B = list(map(int, input().split(' ')))
    result = 0
    dead_enemy = min([A[0], B[0]])  # at town 1
    result += dead_enemy
    rest_power = B[0] - dead_enemy
    for i in range(1, N + 1):
        # (i-1)-th brave result
        dead_enemy = min([A[i], rest_power])
        result += dead_enemy
        rest_enemy = A[i] - dead_enemy
        # i-th brave result
        if i <= N - 1:
            dead_enemy = min([rest_enemy, B[i]])
            result += dead_enemy
            rest_power = B[i] - dead_enemy
    print(result)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

print(sb if sb - sum(b) == 0 else sb - sum(b))

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

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

print(cnt)

Submission status: Accepted

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

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

Submission status: Accepted

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

mon = 0
for i in range(N):
    if lb[i] <= la[i]:
        mon += lb[i]
    elif lb[i] > la[i]:
        if lb[i] - la[i] >= la[i+1]:
            mon += la[i] + la[i+1]
            la[i+1] = 0
        else:
            mon += la[i] + lb[i] - la[i]
            la[i+1] -= lb[i] - la[i]

print(mon)

Submission status: Accepted

Solution code:
from sys import stdin
import math
import fractions

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

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

print(cnt)

Submission status: Accepted

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

fii=min(a[0],b[0])
fij=min(a[1],b[0]-fii)
icnt=fii+fij
for i in range(1,n):
    fii=min(a[i]-fij,b[i])
    fij=min(a[i+1],b[i]-fii)
    icnt+=fij+fii

print(icnt)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

c = [0] * n

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

print(sum(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 += b[i]
    else:
        x = min(b[i], a[i]+a[i+1])
        a[i+1] = a[i+1]-x+a[i]
        ans += x

print(ans)

Submission status: Accepted

Solution code:
ii = lambda:int(input())
mi = lambda:list(map(int,input().split()))
ix = lambda x:list(input() for _ in range(x))
mix = lambda x:list(li() for _ in range(x))
iix = lambda x:list(int(input()) for _ in range(x))
##########
def main(n,a,b):
    before = sum(a)
    for i in range(len(b)):
        if b[i] < a[i]:
            a[i] -= b[i]
        else:
            a[i+1] = max(0, a[i] + a[i+1] - b[i])
            a[i] = 0
    return before - sum(a)
            
n = ii()
a = mi()
b = mi()
print(main(n,a,b))

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 n in range(N):
  if B[n]>=A[n]:
    count+=A[n]
    if B[n]-A[n]>A[n+1]:
      count+=A[n+1]
      A[n+1]=0
    else:
      count+=B[n]-A[n]
      A[n+1]-=(B[n]-A[n])
  else:
    count+=B[n]
print(count)

Submission status: Accepted

Solution code:
n = int(input())
mons = list(map(int,input().split()))
yusya = list(map(int,input().split()))
mons[::-1]
yusya[::-1]

ans = 0
for i in range(n):
    if yusya[i] >= mons[i]:#今の村を倒しきれるとき
        ans += mons[i]
        if yusya[i]-mons[i] >= mons[i+1]:#次の村を倒しきれる
            ans += mons[i+1]
            mons[i+1] = 0
        else:#次の村を倒しきれない
            ans += yusya[i] - mons[i]
            mons[i+1] -= yusya[i] - mons[i]
    else:#今の村を倒しきれない
        ans += yusya[i]
            

print(ans)

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(a[i],b[i])
print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
arra = list(map(int,input().split()))
arrb = list(map(int,input().split()))

hero_res = 0
def_cnt = 0
for i in range(0,N+1):
    nmons = arra[i]
    mons_res = max(nmons - hero_res, 0)
    def_cnt += nmons - mons_res

    if i < N:
        hero = arrb[i]
        mons_res_2 = max(mons_res - hero, 0)
        def_cnt += mons_res - mons_res_2
        hero_res = hero - (mons_res - mons_res_2)
print(def_cnt)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
import math
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 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)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = 0
for i in range(n):
  if hero[i] < monster[i]:
    ans += hero[i]
  else:
    ans += monster[i]
    hero[i] -= monster[i]
    if hero[i] < monster[i+1]:
      monster[i+1] -= hero[i]
      ans += hero[i]
    else:
      ans += monster[i+1]   
      monster[i+1] = 0
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n, p, *a = map(int, open(0).read().split())
c = 0
for a, b in zip(a, a[n:]):
    c += min(p+a, b)
    p = a - max(min(b-p, a), 0)
print(c)

Submission status: Accepted

Solution code:
n, p, *a = map(int, open(0).read().split())
c = 0
for a, b in zip(a, a[n:]):
    c += min(p+a, b)
    p = a - max(min(b-p, a), 0)
print(c)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    *a, = map(int, input().split())
    *b, = map(int, input().split())
    b += [0]

    total_beaten = 0
    carry = 0
    for beat, number in zip(b, a):
        by_prev = min(number, carry)

        total_beaten += by_prev
        number -= by_prev

        by_curr = min(number, beat)
        total_beaten += by_curr

        carry = beat - by_curr

    print(total_beaten)


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 = sum(B)

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

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

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

print(ans-sum(B))

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

        a[i+1] = max(a[i+1] - (b[i] - a[i]),0)
        a[i] = 0

        #print(a[i] + s)

    else:
        a[i] -= b[i]
        ans += b[i]
        #print(b[i])
#print(a)
print(ans)

Submission status: Accepted

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


if __name__ == '__main__':
    solve()

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

Submission status: Accepted

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

monsall = 0
monsnext = a[0]

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

print(monsall)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))[::-1]
B = list(map(int, input().split()))[::-1]
ans = 0

for i, b in enumerate(B):
    ans += min(A[i], b)
    remain = max(0, b - A[i])
    ans += min(A[i + 1], remain)
    A[i + 1] = max(0, A[i + 1] - remain)

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,bi in enumerate(b):
    x = min(a[i],bi)
    bi-=x
    a[i]-=x
    ans+=x
    if bi>0:
        x=min(a[i+1],bi)
        a[i+1]-=x
        ans+=x
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 A[i]<B[i]:
        ans+=A[i]
        tmp = min(B[i]-A[i], A[i+1])

        A[i+1]-=tmp
        ans+=tmp
    else:
        ans+=B[i]

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int, input().split()))
B=list(map(int, input().split()))
SUM=sum(A)
for i in range(N):
    res=B[i]
    fir=min(res,A[i])
    A[i]=A[i]-fir
    res-=fir
    sec=min(res,A[i+1])
    A[i+1]-=sec

print(SUM-sum(A))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  # , insort_left, insort_right

# https://atcoder.jp/contests/abc135/tasks/abc135_c
# 前から倒せるだけ倒していくのが最適
N = read_a_int()
A = read_ints()
B = read_ints()
ans = 0
for i in ra(N):
    power = B[i]
    left = max(A[i] - power, 0)  # 残りのモンスター数
    used = A[i] - left
    ans += used  # 減った数
    power -= used

    # 二体目の処理
    left = max(A[i + 1] - power, 0)
    used = A[i + 1] - left
    A[i + 1] = left
    ans += used  # 減った数
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

kill_score = 0
for i in range(n):
    if a[i] >= b[i]:
        kill_score += b[i]

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


print(kill_score)
#print(a)