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:

def input_from_console():
    n = int(input())
    a_list = list(map(int, input().split()))
    b_list = list(map(int, input().split()))
    return n, a_list, b_list


def solve(n, a_list, b_list):
    sum_of_a = sum(a_list)
    for i in range(n):
        if a_list[i] - b_list[i] <= 0:
            b_list[i] -= a_list[i]
            a_list[i] = 0
            a_list[i + 1] -= min(b_list[i], a_list[i + 1])
        else:
            a_list[i] -= b_list[i]
    return sum_of_a - sum(a_list)


def main():
    n, a_list, b_list = input_from_console()
    print(solve(n, a_list, b_list))


if __name__ == "__main__":
    import sys

    if sys.gettrace():  # Check if the python interpreter is in debug mode or not.
        check_cases()
    else:
        main()

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

Submission status: Accepted

Solution code:
n = int(input())
*a, = map(int,input().split())
*b, = map(int,input().split())
#print(a)
#print(b)
cnt = 0
for i in range(n):
  for j in range(i,i+1):
    x = b[j] - a[i]
    if x >= 0:
      b[j] = x
      cnt += a[i]
      y = b[j] - a[i+1]
      if y > 0:
        cnt += a[i+1]
        a[i+1] = 0
      elif y <= 0:
        cnt += b[j]
        a[i+1] = a[i+1] - b[j]
    elif x < 0:
      cnt += b[j]
      continue
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
rev_a = list(reversed(a))
rev_b = list(reversed(b))
stock = 0
getted = 0
 
for i in range(n):
  #Be careful that rev_a size is N+1 !
  if stock > 0:
    if rev_a[i] >= stock:
      getted += stock
      rev_a[i] = rev_a[i] - stock
    else:
      getted += rev_a[i]
      rev_a[i] = 0
    stock = 0
  
  if rev_a[i] >= rev_b[i]:
    getted += rev_b[i]
  else:
    getted += rev_a[i]
    stock = rev_b[i] - rev_a[i]
  
  if i == n-1:
    if rev_a[i+1] >= stock:
      getted += stock
    else:
      getted += rev_a[i+1]
 
print(getted)

Submission status: Accepted

Solution code:
nh  = int(input())
m  = [int(i) for i in input().split()]
h  = [int(i) for i in input().split()]

h1 = sum(h)

for i in range(len(m)):
    if h[i-1] != 0 and i != 0:
        val = abs(m[i] - h[i-1])
        if m[i] > h[i-1]:
            m[i]  = val
            h[i-1]  = 0
        else:
            m[i]  = 0
            h[i-1]  = val        

    if m[i] != 0 and i < nh:
        val = abs(m[i] - h[i])
        if m[i] > h[i]:
            m[i]  = val
            h[i]  = 0
        else:
            m[i]  = 0
            h[i]  = val


print(h1 - sum(h))

Submission status: Accepted

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

for i in range(len(a)-2,-1,-1):
    b[i] = max(b[i]-a[i+1],0)
    if b[i] < a[i]:
        a[i] = a[i]-b[i]
        b[i] = 0
    else:
        b[i] = b[i]-a[i]
        a[i] = 0
    
print(c-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):
    d1 = b[i] - a[i]
    if d1 <= 0:
        cnt += b[i]
    else:
        cnt += a[i]
        d2 = a[i+1] - d1
        if d2 > 0:
            a[i+1] = d2
            cnt += d1
        else:
            cnt += a[i+1]
            a[i+1] = 0

print(cnt)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(4100000)

import logging

logging.basicConfig(level=logging.DEBUG)

logger = logging.getLogger(__name__)


def resolve():
    N = [int(x) for x in sys.stdin.readline().split()][0]
    a_list = [int(x) for x in sys.stdin.readline().split()]
    b_list = [int(x) for x in sys.stdin.readline().split()]

    count = 0
    for i in range(N)[::-1]:
        a = a_list[i+1]
        b = b_list[i]
        if a >= b:
            count += b
            continue
        else:
            count += a
            b -= a

            if a_list[i] >= b:
                a_list[i] -= b
                count += b
            else:
                count += a_list[i]
                a_list[i] = 0


    print(count)
            





if __name__ == "__main__":
    resolve()

# AtCoder Unit Test で自動生成できる, 最後のunittest.main は消す
# python -m unittest template/template.py で実行できる
# pypy3 -m unittest template/template.py で実行できる

import sys
from io import StringIO
import unittest

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)

    def test_入力例_4(self):
        input = """1
1 1
1"""
        output = """1"""
        self.assertIO(input, output)

Submission status: Accepted

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

for i in range(n):
  if b[i]+c[i]-a[i]>=0:
    if c[i]-a[i]>=0:
      c.append(b[i])
      
    else:
      c.append(b[i]+c[i]-a[i])
    sum+=a[i]
  else:
    c.append(0)
    sum+=b[i]+c[i]

if c[n]>a[n]:
  print(sum+a[n])
else:
  print(sum+c[n])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
saver_num = int(input())
mons_num = list(map(int, input().split()))
power_num = list(map(int, input().split()))
count = 0

for i in range(saver_num):
    if power_num[i] <= mons_num[i]:
        count += power_num[i]
    else:
        count += mons_num[i]
        power = power_num[i] - mons_num[i]
        if power <= mons_num[i+1]:
            count += power
            mons_num[i+1] = mons_num[i+1] - power
        else:
            count += mons_num[i+1]
            mons_num[i+1] = 0

print(count)

Submission status: Accepted

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

total = 0
for i in range(N):
    yusha_life = Bs[N-1-i]
    yusha_life -= As[N-i]
    if yusha_life <= 0:
        total += Bs[N-1-i]
        As[N-i] -= Bs[N-1-i]
    else:
        total += As[N-i]
        total += min(yusha_life, As[N-i-1])
        As[N-i-1] = 0 if As[N-i-1] <= yusha_life else As[N-i-1] - yusha_life

print(total)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


def main():
    n = int(input())
    a_list = [int(i) for i in input().split()]
    b_list = [int(i) for i in input().split()]

    cnt = 0
    for i, b in enumerate(b_list):
        a = a_list[i]
        if a <= b:
            combo = a
            b = b - a
            a = a_list[i + 1]
            if a <= b:
                combo += a
                a = 0
            else:
                combo += b
                a -= b
            a_list[i + 1] = a
        else:
            combo = b

        cnt += combo
    print(cnt)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

count = 0

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

print(count)

Submission status: Accepted

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

count=0
for i in range(N):
    c=B[i]-A[i]
    if c>=0:
        count+=A[i]
#        print("1",i,A[i],B[i],c,count)
    if c>0 and A[i+1]>c:
        A[i+1]=A[i+1]-c
        count+=c
#        print("2",i,A[i],B[i],c,count)

    elif c>0 and A[i+1]<=c:
        count+=A[i+1]
        A[i+1]=0
#        print("3",i,A[i],B[i],c,count)

    if c<0:
        count+=B[i]
#        print("4",i,A[i],B[i],c,count)
    # if i ==N and A[i+1]>c:
    #     count+=A[i+1]
    #     print(N,i,"aaa")
    # if i ==N and A[i+1]>=c:
    #     count+=c
    #     print("bbb",i,N,c)

# if  A[i+1]>c:
#     count+=A[i+1]
#     print(N,i,"aaa")
# if  A[i+1]>=c:
#     count+=c
#     print("bbb",i,N,c)

print(count)

Submission status: Accepted

Solution code:
n=int(input())
al=[int(i) for i in input().split()]
bl=[int(i) for i in input().split()]
acc=0
carry=0
for i in range(n):
    ai=al[i]; bi=bl[i]
    if ai <= carry:
        acc+=ai
        carry=bi
    elif ai-carry <= bi:
        acc+=ai
        carry=bi-(ai-carry)
    else:
        acc+=bi+carry
        carry=0
if not carry==0:
    if al[n] <= carry:
        acc+= al[n]
    else:
        acc+=carry
print(acc)

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


main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# input
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
b.insert(0, 0)
c = 0
z = 0
for i in range(n, -1, -1):
    if z >= a[i]:
        c += a[i]
        z = b[i]
    else:
        t = b[i]-(a[i]-z)
        if t > 0:
            c += a[i]
            z = t
        elif t <= 0:
            c += b[i]+z
            z = 0
print(c)

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]
  elif A[i+1]+A[i] <= B[i]:
      cnt += A[i+1]+A[i]
      A[i+1] = 0
  elif A[i]<B[i] and A[i+1]+A[i] >= B[i]:
      cnt +=B[i]
      A[i+1] = A[i+1]-(B[i]-A[i])


print(cnt)

Submission status: Accepted

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

print(count)

Submission status: Accepted

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

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

A_after_sum = sum(A)
print(A_sum- A_after_sum)

Submission status: Accepted

Solution code:
N = int(input())


S = input()
S = S.split()
A = list(map(int, S))
A_copy = list(map(int, S))

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


remain = 0

for i in range(len(B)):
    remain = int(A[i]-B[i])
    if remain <= 0:
        if A[i+1]+remain >= 0:
            A[i+1] = int(A[i+1] + remain)
        else:
            A[i+1] = 0
        A[i] = 0
    elif remain > 0:
        A[i] = remain



print(sum(A_copy)-sum(A))

Submission status: Accepted

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

result = 0

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

print(result)

Submission status: Accepted

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

ans = 0
for i in range(N):

    t = min(B[i], A[i])
    B[i] -= t
    A[i] -= t
    ans += t

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

print(ans)

Submission status: Accepted

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

ans = monstarsum - sum(A)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
mons = [int(i) for i in input().split()]
yusya = [int(i) for i in input().split()]
sum = 0
for i in range(n + 1):
    if i >= 1:
        if yusya[i - 1] < mons[i]:
            mons[i] -= yusya[i - 1]
            sum += yusya[i - 1]
            yusya[i - 1] = 0
        else:
            yusya[i - 1] -= mons[i]
            sum += mons[i]
            mons[i] = 0
    if i < n:
        if yusya[i] < mons[i]:
            mons[i] -= yusya[i]
            sum += yusya[i]
            yusya[i] = 0
        else:
            yusya[i] -= mons[i]
            sum += mons[i]
            mons[i] = 0

print(sum)

Submission status: Accepted

Solution code:
# coding: utf-8

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

ans = 0 

ta = A[-1]

for a, b in list(zip(A[:-1], B))[::-1]:

    tb = b
    if ta >= b:
        ans += b
        ta = a
        continue
    else:
        ans += ta
        tb -= ta
        ta = 0
        

    if a >= tb:
        ans += tb
        ta = a - tb
    else:
        ans += a
        ta = 0

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
g = 0
for i in range(N):
    c = b[i] - min(a[i],b[i])
    g += min(a[i],b[i])
    a[i] -= min(a[i],b[i])
    if c == 0:pass
    else:
        d = c - min(a[i+1],c)
        g += min(a[i+1],c)
        a[i+1] -= min(a[i+1],c)
print(g)

Submission status: Accepted

Solution code:
n = int(input())
a = input()
la = list(map(int,a.split())) #街にいるモンスター数
b = input()
lb = list(map(int,b.split())) #ダメージ

damagecost = 0
killmonster = 0
for i in range(n):
    if lb[i] <= la[i]:
        killmonster += lb[i]
    else:
        killmonster += la[i]
        damagecost = lb[i] - la[i]
        if damagecost >= la[i + 1]:
            killmonster += la[i + 1]
            la[i + 1] = 0
        else:
            killmonster += damagecost
            la[i + 1] = la[i + 1] - damagecost
print(killmonster)

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] != 0 and B[i] != 0:
        k = min(A[i],B[i])
        A[i] -= k
        B[i] -= k
        count += k
    if A[i+1] != 0 and B[i] != 0:
        l = min(A[i+1],B[i])
        A[i+1] -= l
        B[i] -= l
        count += l

print(count)

Submission status: Accepted

Solution code:
n = int(input())
list_a = list(map(int, input().split()))
list_b = list(map(int, input().split()))

count = 0
prev = list_a[0]

for i in range(n):
    if list_b[i] <= prev:
        count += list_b[i]
        prev = list_a[i + 1]
    else:
        count += prev
        if list_b[i] - prev >= list_a[i + 1]:
            count += list_a[i + 1]
            prev = 0
        else:
            count += list_b[i] - prev
            prev = list_a[i + 1] - list_b[i] + prev

print(count)

Submission status: Accepted

Solution code:
n = int(input())
a1 = list(map(int,input().split()))
b1 = list(map(int,input().split()))
a = []
b = []
for i in (reversed(a1)):
    a.append(i)
for i in (reversed(b1)):
    b.append(i)

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

Submission status: Accepted

Solution code:
n = int(raw_input())
a = [int(i) for i in raw_input().split(' ')]
b = [int(i) for i in raw_input().split(' ')]
ret = 0
for i in range(n):
    e = min(a[i], b[i])
    ret += e
    a[i] -= e
    b[i] -= e
    e = min(a[i + 1], b[i])
    ret += e
    a[i + 1] -= e
    b[i] -= e

print ret

Submission status: Accepted

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

a_rev = list(reversed(a))
b_rev = list(reversed(b))


kill_m_1 = 0

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

for i in range(n):
    if a_rev[i] <= b_rev[i]:
        kill_m_2 += a_rev[i]
        b_rev[i] = b_rev[i] - a_rev[i]
        a_rev[i] = 0
    
    else:
        a_rev[i] = a_rev[i] - b_rev[i]
        kill_m_2 += b_rev[i]
        b_rev[i] = 0
    
    if b_rev[i] != 0:
        
        if a_rev[i+1] <= b_rev[i]:
            kill_m_2 += a_rev[i+1]
            b_rev[i] = b_rev[i] - a_rev[i+1]
            a_rev[i+1] = 0
    
        else:
            a_rev[i+1] = a_rev[i+1] - b_rev[i]
            kill_m_2 += b_rev[i]
            b_rev[i] = 0
    
print(max(kill_m_1,kill_m_2))

Submission status: Accepted

Solution code:
N  = int(input())
mon_arr = list(map(int, input().split()))
hero_arr = list(map(int, input().split()))

nums = 0

for i in range(N):
  if (mon_arr[i] + mon_arr[i+1]) > hero_arr[i]:
    nums += hero_arr[i]
    if mon_arr[i+1] > (mon_arr[i] + mon_arr[i+1] - hero_arr[i]):
      mon_arr[i+1] = (mon_arr[i] + mon_arr[i+1] - hero_arr[i])
  else:
    nums += (mon_arr[i] + mon_arr[i+1])
    mon_arr[i+1] = 0
    
print(nums)

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

ans = 0

for i in range(N):
    # print("--------------------")
    # print(Mons)
    if Mons[i] >= Hero[i]:
        hunting_count = Hero[i]
        Mons[i] -= hunting_count
        ans += hunting_count
    else:
        hunting_count_part1 = Mons[i]
        Mons[i] -= hunting_count_part1
        Hero[i] -= hunting_count_part1
        ans += hunting_count_part1

        if Mons[i+1] >= Hero[i]:
            hunting_count_part2 = Hero[i]
            Mons[i+1] -= hunting_count_part2
            ans += hunting_count_part2
        else:
            hunting_count_part2 = Mons[i+1]
            Mons[i+1] -= hunting_count_part2
            Hero[i] -= hunting_count_part2 # 今回は必要ない
            ans += hunting_count_part2

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

t = 0

for i in range(n):
    if i != n-1:
    #自分の担当箇所に全力投球

        #敵の方が多い
        if b[i] < a[i]:
            t += b[i]#倒せるだけ

        #倒せる数の方が多い
        else:
            #自分のとこ倒して、隣を倒せるだけ倒す
            if a[i+1] > b[i] - a[i]:
                t += min(a[i+1]+a[i],b[i])
                a[i+1] = max(0,a[i+1] + a[i]- b[i])

            #隣を全部倒す
            else:
                t += a[i]+a[i+1]
                a[i+1] = 0
            #a[i+1] = max(0,a[i+1] - (b[i]-a[i])) #残ったらその分を隣に回す
            #t += b[i] + max(0,(a[i]-b[i]))
        #print(i,t)
    else:
        if b[i] - a[i] < 0:
            t += b[i]
        else:
            t += (a[i] + min(b[i] - a[i],a[i+1]))#残りと最後のやつの比較

    #print(i,t)

        #t += min(b[n-1],)
print(t)

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 reversed(range(1, len(A))):
  if A[i] > 0:
    d = min(A[i], B[i-1])
    B[i-1] -= d
    A[i] -= d
    kill += d

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

print(kill)

Submission status: Accepted

Solution code:
import sys
import numpy as np
from scipy.misc import comb
sys.setrecursionlimit(1000000)
 
n = int(input())

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

tmp = 0
ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
monster = list(map(int, input().split()))
knight = list(map(int, input().split()))
result = 0

for i in range(n):
    if knight[i] <= monster[i]:
        result += knight[i]
    elif knight[i] <= (monster[i] + monster[i+1]):
        monster[i+1] -= (knight[i] - monster[i])
        result += knight[i]
    else:
        result += (monster[i] + monster[i+1])
        monster[i+1] = 0

print(result)

Submission status: Accepted

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

Submission status: Accepted

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

ans=0
for m,i in enumerate(la):
  ans+=min(i,l[m])+l[m+1]-max(0,l[m+1]-i+min(i,l[m]))
  l[m+1]=max(0,l[m+1]-i+min(i,l[m]))
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()]

next_minus = 0
total = 0
for i in range(N):
    targets = A[i] - next_minus
    rest = B[i] - targets

    total += B[i]
    if rest > 0:
        if A[i+1] > rest:
            next_minus = rest
        else:
            next_minus = A[i+1]
            total -= (rest - A[i+1])
    else:
        next_minus = 0
    
print(total)

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

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

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

amari=0
ans=0

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

ans+=amari
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

num_kill = 0
bb = 0 
aa = A[0]
for i in range(N):
    num_kill += min(B[i], aa + A[i + 1])
    bb = B[i] - aa
    if bb < 0:
        bb = 0

    aa = A[i + 1] - bb
    if aa < 0:
        aa = 0

print(num_kill)

Submission status: Accepted

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

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

print(Total)

Submission status: Accepted

Solution code:
n=int(input())
a=([int(n) for n in input().split()])
b=([int(n) for n in input().split()])
b.append(0)
r=0
t=0
x=0
for i in range(n+1):
  t=b[i]+x-a[i]
  if t>=0:
    r=r+a[i]
    x=min(b[i],t)
  else:
    r=r+b[i]+x
    x=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
for i in range(n):
    for j in range(i,i+2):
        if a[j]>0:
            x = min(a[j],b[i])
            b[i] -= x
            a[j] -= x
            ans += x

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

Submission status: Accepted

Solution code:
N = input()
A = input()
B = input()

A = [int(i) for i in A.split(" ")]
B = [int(i) for i in B.split(" ")]

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

print(count)

Submission status: Accepted

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

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

if __name__ == '__main__':
	main()