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 sys import stdin

input = stdin.readline


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


def main():
    input()
    *A, = read()
    *B, = read()
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    stdin.readline()
    *A, = map(int, stdin.readline().split())
    *B, = map(int, stdin.readline().split())
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    readline = stdin.readline
    readline()
    *A, = map(int, readline().split())
    *B, = map(int, readline().split())
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()

Submission status: Accepted

Solution code:
from sys import stdin

readline = stdin.readline


def read():
    return map(int, readline().split())


def main():
    input()
    *A, = read()
    *B, = read()
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()

Submission status: Accepted

Solution code:
from sys import stdin

input = stdin.readline


def read():
    return tuple(map(int, input().split()))


def main():
    input()
    A = read()
    B = read()
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()

Submission status: Accepted

Solution code:
def read():
    return tuple(map(int, input().split()))


def main():
    input()
    A = read()
    B = read()
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()

Submission status: Accepted

Solution code:
from sys import stdin

readline = stdin.readline


def read():
    return tuple(map(int, readline().split()))


def main():
    readline()
    A = read()
    B = read()
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    readline = stdin.readline
    readline()
    A = tuple(map(int, readline().split()))
    B = tuple(map(int, readline().split()))
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    input()
    readline = stdin.readline
    A = tuple(map(int, readline().split()))
    B = tuple(map(int, readline().split()))
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()

Submission status: Accepted

Solution code:
from sys import stdin

readline = stdin.readline


def main():
    input()
    A = tuple(map(int, readline().split()))
    B = tuple(map(int, readline().split()))
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()

Submission status: Accepted

Solution code:
from sys import stdin

readline = stdin.readline


def main():
    readline()
    A = tuple(map(int, readline().split()))
    B = tuple(map(int, readline().split()))
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    input()
    A = tuple(map(int, stdin.readline().split()))
    B = tuple(map(int, stdin.readline().split()))
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    lines = stdin.readlines()
    A = tuple(map(int, lines[1].split()))
    B = tuple(map(int, lines[2].split()))
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = min(pre, b)
        b -= tmp1
        tmp2 = min(a, b)
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()

Submission status: Accepted

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

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

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

print(sum_a)

Submission status: Accepted

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

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

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

    if i == n:
        break
    elif a[i] <= b[i]:
        c = b[i] - a[i]
        ans += a[i]
        # print("c:" + str(c) + ":" +str(ans))
    else:
        c = 0
        ans += b[i]
        # print("d:" + str(c) + ":" +str(ans))
    

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

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

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
monster = list(map(int, input().split()))
yuusha = list(map(int, input().split()))
a = sum(monster)
for i in range(n):
    if monster[i] < yuusha[i]:
        yuusha[i] -= monster[i]
        monster[i] = 0
        if monster[i+1] < yuusha[i]:
            yuusha[i] -= monster[i+1]
            monster[i+1] = 0
        else:
            monster[i+1] -= yuusha[i]
            yuusha[i] = 0
    else:
        monster[i] -= yuusha[i]
        yuusha[i] = 0
a -= sum(monster)
print(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] + a[i+1] >= b[i]:
		ans += b[i]
	else:
		ans += a[i] + a[i+1]
	if a[i] < b[i]:
		a[i+1] = a[i+1] - (b[i] - a[i])
		if a[i+1] < 0:
			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()))

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

Submission status: Accepted

Solution code:
N = int(input())
mon = [int(x) for x in input().split()]
her = [int(x) for x in input().split()]
n = 0
for i in range(len(her)):
    z = min(mon[i], her[i])
    mon[i] -= z
    her[i] -= z
    n += z
    z = min(mon[i+1], her[i])
    mon[i+1] -= z
    her[i] -= z
    n += z
print (n)

Submission status: Accepted

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

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

  
print(count)

Submission status: Accepted

Solution code:
n = int(input())

a = [num for num in map(int, input().split(' '))]
b = [num for num in map(int, input().split(' '))]

ans = 0
pre = 0 # 

for i in range(n):
  # 勇者の攻撃力取得
  p = b[i]
  
  if p >= a[i]:
    # 全部倒せる + 余力あり or 余力なし
    ans += a[i]
    p -= a[i]
    # print('余力: ', p)
    # 隣町も倒しにかかる
    if p >= a[i+1]:
      # 隣町も全部倒す
      ans += a[i+1]
      a[i+1] = 0
    else:
      # 隣町の一部を倒す
      a[i+1] -= p
      ans += p
  else:
    # 一部を倒せる
    ans += p
  # print(a, ans)


print(ans)

Submission status: Accepted

Solution code:
n = int(input())
L = []
A = [0] + list(map(int, input().split()))
B = [0] + list(map(int, input().split())) + [0]

out = 0
for i in range(1, n+2):
  if A[i]- B[i-1] <= 0:
    out += A[i]
    A[i] = 0
  else:
    if A[i]- B[i-1] - B[i] <= 0:
      out += A[i]
      B[i] = B[i] - (A[i] - B[i-1])
      A[i] = 0
    else:
      out += B[i] + B[i-1]
      A[i] = (A[i] - B[i-1]) - B[i]
      B[i] = 0

print(out)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

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

print(sum)

Submission status: Accepted

Solution code:
N = int(input())
Alist = list(map(int, input().split()))
Blist = list(map(int, input().split()))
help = 0
kill = 0
for i in range(N):
    monster = Alist[i] - help
    if monster >= 0:
        kill += help
    else:
        kill += Alist[i]
        monster = 0

    can_kill = Blist[i]
    if can_kill >= monster:
        kill += monster
        help = can_kill - monster
    else:
        kill += can_kill
        help = 0
monster = Alist[-1]
if help >= monster:
    kill += monster
else:
    kill += help
print(kill)

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

Submission status: Accepted

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

ans = 0
for idx, item in enumerate(B):
    a = min(item, A[idx])
    ans += a
    if (item > a):
        if ((item-a) < A[idx+1]):
            ans += (item-a)
            A[idx+1] -= (item-a)
        else :
            ans += A[idx+1]
            A[idx+1] = 0     

print(ans)

Submission status: Accepted

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

ans = 0
for i in range(N):
    l = min(A[i], B[i])
    ans += l
    r = min(A[i+1], B[i]-l)
    ans += r
    A[i+1] -= r

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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):#N人の勇者目線で考える。
  count +=min(A[i],B[i])#一個目の町でモンスターを倒す。
  if A[i]>=B[i]:
    B[i]=0
  else:
    B[i] -= A[i]#二個目の町で倒せるモンスターの数
  count +=min(A[i+1],B[i])#二個目の町で倒せるモンスターの数
  if A[i+1]<=B[i]:
    A[i+1]=0
  else:
    A[i+1] -=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):
    cankill = B[i]
    ans += min([A[i],B[i]])
    if B[i] - A[i] > 0:
        additional = min([B[i] - A[i],A[i+1]])
        ans += additional
        A[i+1] += - additional
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):
    left = min(a[i], b[i])
    ans += left
    right = min(a[i + 1], b[i] - left)
    ans +=  right
    a[i + 1] -= right

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

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

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
input = sys.stdin.readline


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

    result = 0
    for i in reversed(range(n)):
        if b[i] > a[i + 1]:
            result += a[i + 1]
            b[i] = b[i] - a[i + 1]
            a[i + 1] = 0

            if b[i] > a[i]:
                result += a[i]
                b[i] = b[i] - a[i]
                a[i] = 0
            else:
                result += b[i]
                a[i] = a[i] - b[i]
                b[i] = 0
        else:
            result += b[i]
            a[i + 1] = a[i + 1] - b[i]
            b[i] = 0
            continue

    print(result)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# 2019-11-15 14:37:47(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 

def main():
    n = int(sys.stdin.readline().rstrip())
    a = [int(x) for x in sys.stdin.readline().split()]
    b = [int(x) for x in sys.stdin.readline().split()]
    at_beginning = sum(a)
    for i in range(n):
        if a[i] >= b[i]:
            a[i] -= b[i]
        else:
            a[i+1] = max(0, a[i+1] - (b[i] - a[i]))
            a[i] = 0

    at_ending = sum(a)

    ans = at_beginning - at_ending
    print(ans)
   

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
ii = lambda : int(input())
li = lambda : list(map(int, input().split()))

n = ii()
a = li()
b = li()

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

Submission status: Accepted

Solution code:
from collections import defaultdict,deque, Counter
import sys,heapq,bisect,math,itertools,string,queue,copy,time
import numpy as np
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

n = inp()
Alist = inpl()
Blist = inpl()
ans = 0
for i in range(n):
    ApA = Alist[i]+Alist[i+1]
    if Blist[i] >= ApA:
        ans += ApA
        Alist[i+1] = 0
    elif Blist[i] >= Alist[i] and Blist[i] <ApA:
        ans += Blist[i]
        Alist[i+1] = ApA - Blist[i]
    elif Blist[i] < Alist[i]:
        ans += Blist[i]

print(ans)

Submission status: Accepted

Solution code:
#%%
import sys
def input():
    return sys.stdin.readline().rstrip()

def main():
    N = int(input())
    As = list(map(int, input().split()))
    Bs = list(map(int, input().split()))

    r = 0
    d = 0
    for i in range(N):
        if (r+Bs[i]) >= As[i]:
            d += As[i]
            r = min((r+Bs[i] - As[i]), Bs[i])
        else:
            d += (r+Bs[i])
            r = 0
    else:
        d += min(As[N], r)
    print(d)

# %%
if __name__ == '__main__':
    main()

# %%
# from atcoder_test import doTest
# doTest("abc135","c",main)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
def print_ans(N, A, B):
    """Test Case
    >>> print_ans(2, "3 5 2", "4 5")
    9
    >>> print_ans(3, "5 6 3 8", "5 100 8")
    22
    >>> print_ans(2, "100 1 1", "1 100")
    3
    """
    ar = list(map(int, A.split()))
    br = list(map(int, B.split()))
    ans = 0
    for i in range(N):
        if br[i] < ar[i]:
            ans += br[i]
        else:
            ans += ar[i]
            br[i] -= ar[i]
            if ar[i + 1] <= br[i]:
                ans += ar[i + 1]
                ar[i + 1] = 0
            else:
                ans += br[i]
                ar[i + 1] -= br[i]  
    print(ans)

if __name__ == '__main__':
    N= int(input().rstrip())
    A = input().rstrip()
    B = input().rstrip()    
    print_ans(N, A, B)

Submission status: Accepted

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

if a[0] + a[1] - b[0] >= 0:
    count += b[0]
    if a[0] <= b[0]:
        rest = a[0] + a[1] - b[0]
    else:
        rest = a[1]    
else:
    count += a[0] + a[1]


for i in range(1,n):
    han = rest + a[i+1] - b[i]
    if han >= 0:
        count += b[i]
        if rest <= b[i]:
            rest = han
        else:
            rest = a[i+1]
    else:
        count += rest + a[i+1]
        rest = 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):
	
	if A[i] < B[i]:
		count = count + A[i]
		B[i] = B[i] - A[i]
		A[i] = 0
	else:
		count = count + B[i]
		A[i] = A[i] - B[i]
		B[i] = 0
	
	if A[i + 1] < B[i]:
		count = count + A[i + 1]
		B[i] = B[i] - A[i + 1]
		A[i + 1] = 0
	else:
		count = count + B[i]
		A[i + 1] = A[i + 1] - 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):
    ans += min(B[i], A[i]+A[i+1])
    A[i+1] = max(0, A[i+1] - max(0, (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()))
b.append(0)
res=0
ans=0
for i in range(n+1):
  if res >= a[i]:
    ans += a[i]
    res = b[i]
  elif res + b[i] >= a[i]:
    ans += a[i]
    res += b[i]
    res -= a[i]
  else:
    ans += res + b[i]
    res = 0
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
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 B[i] <= A[i]:
        count += B[i]
    else:
        count += A[i] + np.minimum((B[i] - A[i]), A[i+1])
        A[i+1] -= np.minimum((B[i] - A[i]), A[i+1])
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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()]
ans = 0
for i in range(N):
    rest = B[i]
    use_i = min(rest, A[i])
    rest -= use_i
    ans += use_i
    use_ii = min(rest, A[i+1])
    ans += use_ii
    A[i+1] -= use_ii
print(ans)

Submission status: Accepted

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

for i in range(n):
    for j in range(i,i+2):
        c = b[i] - a[j]
        if c <= 0:
            a[j] -= b[i]
            s += b[i]
            break
        else:
            b[i] -= a[j]
            s += a[j]
            a[j] = 0

print(s)

Submission status: Accepted

Solution code:
N = int(input())
Monsters = [int(i) for i in input().split()]
Heroes = [int(i) for i in input().split()]
res = 0
for i in range(N):
    r = Monsters[i] - Heroes[i]
    if r >= 0:
        Monsters[i] -= Heroes[i]
        res += Heroes[i]
    else:
        res += Monsters[i]
        Monsters[i] = 0
        k = Monsters[i+1] + r
        if k >= 0:
            res -= r
            Monsters[i+1] += r
        else:
            res += Monsters[i+1]
            Monsters[i+1] = 0

print(res)

Submission status: Accepted

Solution code:
# coding: utf-8

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
#s = [str(input()) for i in range(N)]
#print(A)

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

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


# 初項s, 交差dのn個の数列の和
def sum_of_arithmetic_progression(s, d, n):
    return n * (2 * s + (n - 1) * d) // 2


# aとbの最大公約数
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


# aとbの最小公倍数
def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


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

    total = sum(A)
    for i in range(N):
        r = min(A[i], B[i])
        A[i] -= r
        B[i] -= r

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

    print(total - sum(A))

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

hero_old = 0

for i in range(N + 1):
    enemy_now = A[i]
    if i == N:
        hero_now = 0
    else:
        hero_now = B[i]
    count += min([enemy_now, hero_old])
    enemy_now = max([0, enemy_now - hero_old])
    count += min([enemy_now, hero_now])
    hero_old = max([0, hero_now - enemy_now])

print(count)

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

print(l)

Submission status: Accepted

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

total = sum(a)

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

kill = total - sum(a)

print(kill)

Submission status: Accepted

Solution code:
N = int(input())
li_a = list(map(int, input().split())) #町にいるモンスターの数
li_b = list(map(int, input().split())) #勇者が倒せるモンスターの数
ans = 0
for i in range(N):
  if li_a[i] >= li_b[i]:
    ans += li_b[i]
    #li_a[i+1] += li_a[i] - li_b[i]
  else:
    ans += min(li_a[i] + li_a[i+1], li_b[i])
    li_a[i+1] = max(0, li_a[i] + li_a[i+1] - li_b[i])
print(ans)

Submission status: Accepted

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

for i, bi in enumerate(reversed(b)):
    id = n-i
    if a[id] >= bi:
        ans += bi
    else:
        ans += a[id]
        bi -= a[id]
        id -= 1
        if a[id] >= bi:
            ans += bi
            a[id] -= bi
        else:
            ans += a[id]
            a[id] = 0

print(ans)

Submission status: Accepted

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

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):

  diff = min(a[i], b[i])
  cnt += diff
  a[i] -= diff
  b[i] -= diff

  diff = min(a[i+1], b[i])
  cnt += diff
  a[i+1] -= diff
  b[i] -= diff

print(cnt)