Problem:


Score : 300 points

Problem Statement

There are N+1 towns. The i-th town is being attacked by A_i monsters.

We have N heroes. The i-th hero can defeat monsters attacking the i-th or (i+1)-th town, for a total of at most B_i monsters.

What is the maximum total number of monsters the heroes can cooperate to defeat?

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_{N+1}
B_1 B_2 ... B_N

Output

Print the maximum total number of monsters the heroes can defeat.

Sample Input 1

2
3 5 2
4 5

Sample Output 1

9

If the heroes choose the monsters to defeat as follows, they can defeat nine monsters in total, which is the maximum result.

The first hero defeats two monsters attacking the first town and two monsters attacking the second town.

The second hero defeats three monsters attacking the second town and two monsters attacking the third town.

Sample Input 2

3
5 6 3 8
5 100 8

Sample Output 2

22

Sample Input 3

2
100 1 1
1 100

Sample Output 3

3



Sample input:
2
3 5 2
4 5


Sample output:
9

Submission status: Accepted

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

ans=0
for i in range(n):
  if b[i]<=a[i]:
    a[i]-=b[i]
    ans+=b[i]
  elif b[i]<=(a[i]+a[i+1]):
    a[i+1]-=(b[i]-a[i])
    a[i]=0
    ans+=b[i]
  else:
    ans+=(a[i]+a[i+1])
    a[i]=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
for i,b in enumerate(B):
    t = min(b,A[i]+A[i+1])
    ans += t
    #A[i]を使いきったか否か
    A[i+1] -= max(0, t-A[i])
    
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):
    count+=min(A[i],B[i])
    if A[i]<B[i]:
        if A[i+1]<B[i]-A[i]:
            count+=A[i+1]
            A[i+1]=0
        else:
            count+=B[i]-A[i]
            A[i+1]-=B[i]-A[i]
print(count)

Submission status: Accepted

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

cnt=0
last=a[n]
for i in range(n):
    if b[i]>=a[i]:
        cnt+=min(b[i], a[i]+a[i+1])
        a[i+1]=max(0, a[i+1]-(b[i]-a[i]))
        a[i]=0
    else:
        cnt+=b[i]
        a[i]=a[i]-b[i]
print(cnt)

Submission status: Accepted

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

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

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

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

print(count)

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

Submission status: Accepted

Solution code:
# from math import ceil,sqrt,log,gcd,pi
# def ii():return int(input())
# def si():return input()
# def mi():return map(int,input().split())
# def li():return list(mi())

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

Submission status: Accepted

Solution code:
def exception_handling(n, ali, bli):

    bli.append(0)
    count = 0
    ans = 0

    for i in range(n):
        count = min(ali[i], bli[i])
        ans += count
        ali[i] -= count
        bli[i] -= count

        count = min(ali[i+1], bli[i])
        ans += count
        ali[i+1] -= count
        bli[i] -= count

    return ans

def main():
    n = int(input())
    ali = list(map(int, input().split()))
    bli = list(map(int, input().split()))
    print(exception_handling(n, ali, bli))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def resolve():
    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 - 1, -1, -1):
        tmp = min(A[i + 1], B[i])
        ans += tmp
        B[i] -= tmp
        if B[i] > 0:
            tmp = min(A[i], B[i])
            ans += tmp
            A[i] -= tmp
    print(ans)


resolve()

Submission status: Accepted

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

# check
# 考え方参考：https://note.com/tanon_cp/n/n6b51b9a57076
kill_monsters = 0
for i in range(N):
    if A[i] >= B[i]:
        kill_monsters += B[i]
        continue
    else:
        kill_monsters += A[i]
        B[i] -= A[i]
        if A[i + 1] > B[i]:
            kill_monsters += B[i]
            A[i + 1] -= B[i]
        else:
            kill_monsters += A[i + 1]
            A[i + 1] = 0

print(kill_monsters)

Submission status: Accepted

Solution code:
n = int(input())
lia = list(map(int,input().split()))
lib = list(map(int,input().split()))
mon = sum(lia)
for i in range(n):
    k = lia[i] - lib[i]
    if k <= 0:
        lia[i] = 0
        lia[i+1] += k
        if lia[i+1] < 0:
            lia[i+1] = 0
    else:
        lia[i] -= lib[i]
print(mon-sum(lia))

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):
    j = N-1-i
    b = B[j]
    if b < A[j+1]:
        s += b
        A[j+1] -= b
    elif b < A[j]+A[j+1]:
        s += b
        A[j] = A[j] -(b-A[j+1])
        A[j+1] = 0
    else:
        s += A[j]+A[j+1]
        A[j] = 0
print(s)

Submission status: Accepted

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

As[0], Bs[0], ans = max(As[0]-Bs[0], 0), max(Bs[0]-As[0], 0), min(As[0], Bs[0])

for i in range(1,N):
  As[i], Bs[i-1], ans = max(As[i]-Bs[i-1], 0), max(Bs[i-1]-As[i], 0), ans + min(As[i], Bs[i-1])
  if As[i] > 0:
    As[i], Bs[i], ans = max(As[i]-Bs[i], 0), max(Bs[i]-As[i], 0), ans + min(As[i], Bs[i])

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

Submission status: Accepted

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

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

print(s - sum(a))

Submission status: Accepted

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()

from collections import defaultdict, deque, Counter
from sys import exit
import heapq
import math
import copy
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

N = getN()
A = getList()
B = getList()
cnt = 0
for i in range(N):
    minus1 = min(A[-i - 1], B[-i - 1])
    A[-i - 1] -= minus1
    B[-i - 1] -= minus1
    cnt += minus1
    minus2 = min(A[-i - 2], B[-i - 1])
    A[-i - 2] -= minus2
    B[-i - 1] -= minus2
    cnt += minus2
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
prev = a[0]
total = 0
for i, j in zip(a[1:], b):
    if prev >= j:
        total += j
        prev = i
    else:
        task = prev + i
        if task <= j:
            total += task
            prev = 0
        else:
            total += j
            prev = task - j
print(total)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
n = int(input())
an = list(map(int,input().split()))
bn = list(map(int,input().split()))
nokori = 0
mon = 0
for i in range(n+1):
    if i != n:
        a = an[i]
        if a<=nokori:
            mon += a
            nokori = bn[i]
        else:
            mon += nokori
            a -= nokori
            if a <= bn[i]:
                mon += a
                nokori = bn[i]-a
            else:
                mon += bn[i]
                nokori = 0
    else:
        a = an[i]
        mon += min(a,nokori)

print(mon)

Submission status: Accepted

Solution code:
N=int(input())
AList = list(map(int, input().split()))
BList = list(map(int, input().split()))

Asum=sum(AList)

i=0
while i<N:
    Amari=max(0,BList[i]-AList[i])
    AList[i]=max(AList[i]-BList[i],0)
    AList[i+1]=max(AList[i+1]-Amari,0)
    
    
    i+=1

print(Asum-sum(AList))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n=int(input())
a=[0]*(n+1)
b=[0]*n
a[:]=map(int,input().split())
b[:]=map(int,input().split())

r=sum(a)
minab=min(a[0],b[0])
a[0]-=minab
b[0]-=minab
for ii in range(n):
  minab=min(a[ii+1],b[ii])
  a[ii+1]-=minab
  b[ii]-=minab
  if ii+1<n:
    minab=min(a[ii+1],b[ii+1])
    a[ii+1]-=minab
    b[ii+1]-=minab
print(r-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):
    ans += min(a[i], b[i]) + 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 = list(map(int, input().split()))
B = list(map(int, input().split()))

ans = 0

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

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

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict
readline = sys.stdin.buffer.readline
geta = lambda fn: map(fn, readline().split())
gete = lambda fn: fn(readline())

# sys.setrecursionlimit(10**5)

def main():
    N = gete(int) 
    A = list(geta(int))
    B = list(geta(int))

    ans = 0
    for i, b in enumerate(B):
        if A[i] + A[i+1] < b:
            ans += A[i] + A[i+1]
            A[i+1] = 0
        else:
            ans += b 
            if b > A[i]:
                A[i+1] -= b - A[i]
    
    print(ans)
	
if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
def main():
  n, *lst = map(int, sys.stdin.read().split())
  a = lst[:n+1]
  b = lst[n+1:]
  res = 0
  for i in range(n):
    if i == 0:
      tmp = min(a[i], b[i])
      res += tmp
      b[i] -= tmp
    else:
      tmp = min(a[i], b[i] + b[i - 1])
      res += tmp
      b[i] -= min(b[i], max(tmp - b[i - 1], 0))
  res += min(a[n], b[n - 1])
  print(res)


if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
import sys
def main():
  n, *lst = map(int, sys.stdin.read().split())
  a = lst[n::-1]
  b = lst[-1:n:-1]
  res = 0
  for i in range(n):
    if i == 0:
      tmp = min(b[i], a[i])
      res += tmp
      a[i] -= tmp
      b[i] -= tmp
    else:
      tmp = min(b[i] + b[i - 1], a[i])
      res += tmp
      a[i] -= tmp
      b[i] -= min(b[i], max(tmp - b[i-1], 0))
  res += min(b[n - 1], a[n - 1] + a[n])
  print(res)


if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
import itertools
import math
import fractions
import functools
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
goke = sum(a)
for i in range(n):
    if b[i] > a[i]:
        a[i+1] -= (b[i]-a[i])
        if a[i+1] < 0:
            a[i+1] = 0
        a[i] = 0
    else:
        a[i] -= b[i]

print(goke - sum(a))

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
s = sum(a)
for i in range(n):
  a[i], b[i] = max(a[i]-b[i], 0), max(b[i]-a[i], 0)
  a[i+1] = max(a[i+1]-b[i], 0)
  
s2 = sum(a)
print(s-s2)

Submission status: Accepted

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

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

    else:
        c += b[i]
print(c)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
A = np.array(A)
B = np.array(B)
A_org = A.copy()

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

ans = A_org-A
print(ans.sum())

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n=int(input())
    la=list(map(int,input().split()))
    lb=list(map(int,input().split()))
    cnt=0
    for i in range(n):
        x=min(la[i],lb[i])
        cnt+=x
        lb[i]-=x
        la[i]-=x
        y=min(lb[i],la[i+1])
        cnt+=y
        la[i+1]-=y
    print(cnt)
resolve()

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n=int(input())
    la=list(map(int,input().split()))
    lb=list(map(int,input().split()))
    cnt=0
    for i in range(n):
        if la[i]>=lb[i]:
            cnt+=lb[i]
            la[i]-=lb[i]
        else:
            cnt+=la[i]
            lb[i]-=la[i]
            cnt+=min(lb[i],la[i+1])
            la[i+1]-=min(lb[i],la[i+1])
    print(cnt)
resolve()

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
s=sum(a)
b=list(map(int,input().split()))
for i in range(n):
  x=max(a[i]-b[i],0)
  b[i]=max(b[i]-a[i],0)
  a[i]=x
  a[i+1]=max(a[i+1]-b[i],0)
print(s-sum(a))

Submission status: Accepted

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

Submission status: Accepted

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

taoshita = 0
for i in range(n):
  taoshita += min(a[i], b[i])
  yoryoku = max(b[i]-a[i], 0)
  taoshita += min(a[i+1], yoryoku)
  a[i+1] = max(a[i+1]-yoryoku, 0)

print(taoshita)

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])+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 = list(map(int, input().split()))
b = list(map(int, input().split()))
count = 0 
for i in range(n):
  count += min(a[i] + a[i + 1], b[i])
  if a[i] < b[i]:
    a[i + 1] = max(a[i + 1] - (b[i] - a[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):
    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:
from math import factorial as fact
import math
import sys
import itertools
import numpy as np
from collections import Counter
import datetime
from collections import deque


#入力:N(int:整数)
def input1():
	return int(input())

#入力:N,M(int:整数)
def input2():
	return map(int,input().split())

#入力:[n1,n2,...nk](int:整数配列)
def input_array():
	return list(map(int,input().split()))

def keta(N):
	return len(str(N))


def input_daytime(input_time):
	time,distance=input_time.split()
	t=time.split(":")
	se=str(t[-1]).split(".")
	dt1 = datetime.timedelta(hours=int(t[0]),minutes=int(t[1]),seconds=int(se[0]),milliseconds=int(se[1]))
	return dt1

def combinations(n,r):
	return list(itertools.combinations(range(n),r))



def all_sets(num_list):
	subsets=[]
	for i in range(2,len(num_list) + 1):
		for c in combinations(num_list, i):
			subsets.append(c) 
	return subsets

#公約数の列挙
def ch(x1):
	cf=[]
	for i in range(2,math.sqrt(x1)+1):
		if x1 % i==0:
			cf.append(i)
	return cf

def CountOneRoots(x,y):
	#x:縦，y:横
	return fact(x+y)/fact(x)/fact(y)

# 素因数分解
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a
	
def count_str(m):
	tmp={}
	for i in range(10):
		if i in tmp:
			tmp[i]=1
		else:
			tmp[i]+=1
	return tmp

def is_prime(n):
	if n==1: return True

	for i in range(2,int(math.sqrt(n))+1):
		if n%i==0:
			return False
	return True

n=input1()
A=input_array()
B=input_array()
next_num=0 #繰越
count=0 #合計

for i in range(n+1):
	if A[i]<=next_num:
		count+=A[i]
		A[i]=0
	else:
		A[i]-=next_num
		count+=next_num
	if i==n:
		break
	if A[i]<B[i]:
		count+=A[i]
		next_num=B[i]-A[i]
	else:
		count+=B[i]
		next_num=0

print(count)

Submission status: Accepted

Solution code:
#print#!/usr/bin/env python3
#%% for atcoder uniittest use
import sys
input= lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**9)
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
def lispin(t=int):return list(pin(t))
#%%code
from itertools import accumulate
def resolve():
    N,=pin()
    A=lispin()
    B=lispin()
    cnt=0
    for i in range(N):
        if A[i]>B[i]:
            cnt+=B[i]
        else:
            t=B[i]-A[i]
            x=A[i+1]-t
            if x<0:
                cnt+=A[i]+A[i+1]
                A[i+1]=0
            else:
                A[i+1]=x
                cnt+=B[i]
    print(cnt)
        
    
#%%submit!
resolve()

Submission status: Accepted

Solution code:
import sys
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def I(): return int(sys.stdin.readline().rstrip())
N = I()
A = LI()[::-1]
B = LI()[::-1]
ans = 0
for i in range(N):
    b = B[i]
    a = A[i]
    ans += min(a,b)
    b -= a
    if b<=0:
        continue
    a2 = A[i+1]
    ans += min(a2,b)
    A[i+1] = a2-(min(a2,b))
print(ans)

Submission status: Accepted

Solution code:
# ABC 135

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(len(A)-1):

    cnt += min(A[i]+A[i+1],B[i])
    if A[i] < B[i]:
        A[i+1] = max(A[i+1] - (B[i] - A[i]),0)
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())

remained_monsters = [int(s) for s in input().split()]
remained_capacities = [int(s) for s in input().split()]
n_killed = 0

for i in range(n):
    if remained_monsters[i] <= remained_capacities[i]:
        n_killed += remained_monsters[i]
        remained_capacities[i] -= remained_monsters[i]
        remained_monsters[i] = 0
    else:
        n_killed += remained_capacities[i]
        remained_monsters[i] -= remained_capacities[i]
        remained_capacities[i] = 0
    if remained_monsters[i + 1] <= remained_capacities[i]:
        n_killed += remained_monsters[i + 1]
        remained_capacities[i] -= remained_monsters[i + 1]
        remained_monsters[i + 1] = 0
    else:
        n_killed += remained_capacities[i]
        remained_monsters[i + 1] -= remained_capacities[i]
        remained_capacities[i] = 0

print(n_killed)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Ans1=0
Ans1=sum(A)

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

Ans2=0
Ans2=sum(A)        
        
print(Ans1-Ans2)

Submission status: Accepted

Solution code:
def main():
    N=int(input())
    A=[int(_) for _ in input().split()]
    B=[int(_) for _ in input().split()]
    ans=0
    for i in range(N):
        if A[i]<B[i]:
            # 余力が残っている
            ans += (A[i]+min(A[i+1],B[i]-A[i]))
            A[i+1] = max(A[i+1]-(B[i]-A[i]),0)
        else:
            # 倒しきった or 倒しきれなかった時
            ans += min(A[i],B[i])
    print(ans)

main()

Submission status: Accepted

Solution code:
# 初期入力
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
B.append(0)
count =0
residue_attack_possible =[0]*(N+2)
residue_monster =0
for i in range(N+1):
    if A[i] <= B[i] +residue_attack_possible[i]:
        count +=A[i]
        residue_attack_possible[i+1] = B[i] -max(0,A[i] -residue_attack_possible[i])
    else:
        count +=B[i] +residue_attack_possible[i]
        #residue_attack_possible[i+1] = 0

print(count)

Submission status: Accepted

Solution code:
N = int(input())
arr_a = list(map(int, input().split()))
arr_b = list(map(int, input().split()))

ans = 0
for i in range(len(arr_b)):
    if arr_b[i] >= arr_a[i]+arr_a[i+1]:
        ans += arr_a[i]+arr_a[i+1]
        arr_a[i] = 0
        arr_a[i+1] = 0

    elif arr_b[i] < arr_a[i]+arr_a[i+1]:
        if arr_b[i] <= arr_a[i]:
            ans += arr_b[i]
            arr_a[i] -= arr_b[i]

        elif arr_b[i] > arr_a[i]:
            ans += arr_b[i]
            arr_a[i+1] -= arr_b[i]
            arr_a[i+1] += arr_a[i]
            arr_a[i] = 0
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a_ls = list(map(int, input().split()))
b_ls = list(map(int, input().split()))

ret = 0

for i in range(n):
    if a_ls[i] >= b_ls[i]:
        ret += b_ls[i]
    else:
        ret += a_ls[i]
        left = b_ls[i] - a_ls[i]
        ret += min(left, a_ls[i+1])
        a_ls[i+1] = max(0, a_ls[i+1] - left)

print(ret)

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

Submission status: Accepted

Solution code:
n = int(input())
a_input = list(map(int,input().split()))
b_input = list(map(int,input().split()))

ans=0
for i in range(n):
    first_attack=0
    second_attack=0

    first_attack = min(b_input[i],a_input[i])
    a_input[i]-=first_attack
    if first_attack<b_input[i]:
        second_attack = min(a_input[i+1],b_input[i]-first_attack)
        a_input[i+1]-= second_attack
    ans+=first_attack+second_attack

print(ans)

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

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

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

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