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=map(int,input().split())
B=map(int,input().split())
a=list(A)
b=list(B)
s=0
m=0

for i in range(0,n):
    l=a[i]-m
    if l<=0:
        s=s+a[i]
        m=b[i]
    elif l>b[i]:
        s=s+b[i]+m
        m=0
    else:
        s=s+a[i]
        m=b[i]-l

if a[n]<m:
    s=s+a[n]
else:
    s=s+m


print(s)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
ans = 0
for i in range(n):
    if b[i] >= a[i]:
        ans += a[i]
        b[i] -= a[i]
        a[i] = 0
    else:
        ans += b[i]
        a[i] -= b[i]
        b[i] = 0
    if b[i]:
        if b[i] >= a[i+1]:
            ans += a[i+1]
            a[i+1] = 0
            b[i] -= a[i+1]
        else:
            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()))
AA = A.copy()
count = sum(A)
ans = 0
for i in range(N-1,-1,-1):
    if A[i+1] + A[i] <= B[i]:
        ans += A[i+1] + A[i]
        A[i+1] = 0
        A[i] = 0
    else:
        now = A[i+1] - B[i]
        A[i+1] = max(now, 0)
        A[i] = A[i] - (max(-now, 0))
        ans += B[i]
    #print(A)
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin

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


c=a

max=0
"""
for i in range(n+1):
    max+=a[i]"""

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


print(max)

Submission status: Accepted

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

beaten_monster_num = 0

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

print(int(beaten_monster_num))

Submission status: Accepted

Solution code:
N = int(input())
Alist = [int(i) for i in list(input().split())]
Blist = [int(i) for i in list(input().split())]
ans = 0
for i in range(N):
    tmp = Alist[i]-Blist[i]
    if tmp < 0:
        ans += Alist[i]
        Blist[i] = -1 * (tmp)
    else:
        ans += Blist[i]
        continue
    tmp = Alist[i+1] - Blist[i]
    if tmp < 0:
        ans += Alist[i+1]
        Alist[i + 1] = 0
    else:
        ans += Blist[i]
        Alist[i+1] = tmp

print(ans)

Submission status: Accepted

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

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

    if A[i+1] >= B[i]:
        total += B[i]
        A[i+1] = A[i+1] - B[i]
    elif A[i+1] < B[i]:
        total += A[i+1]
        A[i+1] = 0

print(total)

Submission status: Accepted

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

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

Submission status: Accepted

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

killed = 0
reserved_power = 0

if N == 1:
    battle = B[0] - A[0]
    if battle >= 0:
        killed += A[0]
        reserved_power = battle
        killed += min(reserved_power, A[1])
    else:
        killed += B[0]
    print(killed)
    exit()

for i in range(N):

    if i == 0:
        battle = B[i] - A[i]
        if battle >= 0:
            reserved_power = battle
            killed += A[i]
        else:
            reserved_power = 0
            killed += B[i]

    elif i == N - 1:
        other_monster = A[i] - reserved_power
        if other_monster <= 0:
            reserved_power = B[i]
            killed += A[i]
            killed += min(reserved_power, A[i+1])
        else:
            killed += reserved_power
            battle = B[i] - other_monster - A[i+1]
            if battle >= 0:
                killed += other_monster + A[i+1]
            else:
                killed += B[i]

    else:
        other_monster = A[i] - reserved_power

        if other_monster <= 0:
            reserved_power = B[i]
            killed += A[i]
        else:
            killed += reserved_power
            battle = B[i] - other_monster

            if battle >= 0:
                reserved_power = battle
                killed += other_monster
            else:
                reserved_power = 0
                killed += B[i]

print(killed)

Submission status: Accepted

Solution code:
input()

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

lenB=len(b)
count=0
for i in range(lenB):
    if b[i]>a[i]:
        count+=a[i]
        b[i]-=a[i]
        a[i]=0
        if b[i]>a[i+1]:
            count+=a[i+1]
            b[i]-=a[i+1]
            a[i+1]=0
        else:
            count+=b[i]
            a[i+1]-=b[i]
            b[i]=0
    else:
        count+=b[i]
        a[i]-=b[i]
        b[i]=0


print(count)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int, input().split()))
B=list(map(int, input().split()))
A.insert(0, 0) #モンスター
B.insert(0, 0) #勇者の能力
C=[0]*(N+1)     #次の街に回せる能力
D=[0]*(N+1) #必要な担当勇者(iが等しい)が倒す数
E=[0]*(N+2) #その街で実際に倒す数
F=[0]*(N+2) #合計能力
 
for i in range(1,N+1):
  if i==1:
    F[i]=B[i]
  else:
    F[i]=B[i]+C[i-1] #余った能力をもらったうえで使える能力を計算
    
  if F[i]>=A[i]: #所有能力が勝ってたら
    E[i]=A[i]
    C[i]=min(F[i]-A[i],B[i])
  else:
    E[i]=F[i]
    C[i]=0
 
  #print(i,A[i],B[i],C[i],D[i],E[i],F[i])
 
if C[N]>=A[N+1]: #剰余能力がモンスターより多かったら
  E[N+1]=A[N+1]
else:
  E[N+1]=C[N]
 
ans=sum(E)
#print(E)
print(ans)

Submission status: Accepted

Solution code:
import copy

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

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

print(ans)

Submission status: Accepted

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

for i in range(N):
    mons = a[i]
    hero = b[i]
    if hero <= mons:
        ans += hero
    else:
        ans += mons
        hero -= mons
        mons_next = a[i+1]
        if hero >= mons_next:
            ans += mons_next
            a[i+1] = 0
        else:
            ans += hero
            a[i+1] = mons_next - hero

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
a = int(input())
mon = list(map(int, input().split(" ")))
att = list(map(int, input().split(" ")))
kil = 0

b = len(mon)

for i in range(b):
#    print(mon[i])
    if i == a :
        if  mon[i] >= att[i-1]:
            kil = kil + att[i-1]
        elif mon[i] < att[i-1] :
            kil = kil + mon[i]
        break
    elif i > 0 and att[i-1] > 0 :
        if mon[i] >= att[i-1]:
            kil = kil + att[i-1]
            mon[i] = mon[i] - att[i-1] 
        elif mon[i] < att[i-1] :
            kil = kil + mon[i]
            continue
    if mon[i] >= att[i]:
        kil = kil + att[i]
        att[i] = 0
    elif mon[i] < att[i] :
        kil = kil + mon[i]
        att[i] = att[i] - mon[i]
#    print('result'+str(i)+':')
#    print('kil:'+str(kil))
#    print('zanAtt:'+att[])
        
print(kil)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy,copy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

def main():
    n=INT()
    a=LIST()
    b=LIST()
    p=sum(a)

    for i in range(n):
        tmp=max(b[i]-a[i], 0)
        a[i]=max(a[i]-b[i],0)

        a[i+1] = max(a[i+1]-tmp,0)
        #print(i,tmp)

    #print(a)
    print(p-sum(a))

if  __name__=='__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
# ----------------------------------------------------------------
# Use Solve Function

def solve(lines):
    N = int(lines.pop(0))
    A = list(map(int, lines.pop(0).split(' ')))
    B = list(map(int, lines.pop(0).split(' ')))

    res = 0
    ex  = 0

    for i,x in enumerate(A):
        if ( i <= len(B) - 1 ):
            bi = B[i]
        else:
            bi = 0
        if ( ex > 0 ):
            if ( ex - x >= 0 ):
                res += x
                ex = bi
                continue
            else:
                res += ex
                x -= ex
        if (bi - x >= 0):
            res += x
            bi -= x
            ex = bi
        else:
            res += bi
            ex = 0

    print(res)


lines = [x.strip() for x in sys.stdin.readlines()]

#
# solve !!
#
solve(lines)

Submission status: Accepted

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

t = b[0]
t2 = b[-1]
count = 0

half = n // 2

if n >= 2:
  for i in range(half):
    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

    if a[-1-i] > b[-1-i]:
      count += b[-1-i]
    else:
      count += a[-1-i]
      b[-1-i] -= a[-1-i]
      if a[-2-i] > b[-1-i]:
        count += b[-1-i]
        a[-2-i] -= b[-1-i]
      else:
        count += a[-2-i]
        a[-2-i] = 0

if n % 2 == 1:
  t = a[half] + a[half+1]
  if t > b[half]:
    count += b[half]
  else:
    count += t

print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))

taos = 0
keep = 0

for i, (a, b) in enumerate(zip(A_list, B_list)):
  if a >= (b + keep) :
    taos += (b + keep)
    keep = 0
  else:
    taos += a
    if keep > a:
      keep = b
    else:
      keep = (b + keep) - a 
  #print(i, taos, keep)

if A_list[-1] >= keep:
  taos += keep
else:
  taos += A_list[-1]
print(taos)

Submission status: Accepted

Solution code:
heroes = int(input())
puts = input()
monsters = puts.split()
puts = input()
strength = puts.split()
Ans = 0

for i in range(heroes):
	if (int(strength[i]) > int(monsters[i])):
		if(int(strength[i]) > int (monsters[i]) + int (monsters[i+1])):
			Ans += int(monsters[i]) + int(monsters[i+1])
			monsters[i+1] = 0
		else:
			monsters[i+1] = int(monsters[i+1]) + int(monsters[i]) - int(strength[i])
			Ans += int(strength[i])
	else:
		Ans += int(strength[i])

print(Ans)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time
sys.setrecursionlimit(10**7)
inf = 10**20

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

ans = 0
for i in range(N):
    if (A[i] > B[i]):
        b = B[i]
        ans += b
        A[i] -= b 
        B[i] -= b
    else:
        a = A[i]
        ans += a
        A[i] -= a
        B[i] -= a
    if (A[i+1] > B[i]):
        b = B[i]
        ans += b
        A[i+1] -= b
        B[i] -= b
    else:
        a = A[i+1]
        ans += a
        A[i+1] -= a
        B[i] -= a
ans1 = ans
A =CA
B =CB

ans = 0
for i in range(N):
    if (A[i] > B[i]):
        b = B[i]
        ans += b
        A[i] -= b 
        B[i] -= b
    else:
        a = A[i]
        ans += a
        A[i] -= a
        B[i] -= a
    if (A[i+1] > B[i]):
        b = B[i]
        ans += b
        A[i+1] -= b
        B[i] -= b
    else:
        a = A[i+1]
        ans += a
        A[i+1] -= a
        B[i] -= a
ans2 = ans
ans = max(ans1,ans2)
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)
c = [0] * (n+1)

temp=0

for i in range(n+1):
	c[i] = min(a[i],temp )
	a[i] -= c[i]
	c[i] += min(a[i], b[i])
	temp = b[i] - min(a[i],b[i])

print(sum(c))

Submission status: Accepted

Solution code:
a=int(input())
b = list(map(int, input().split()))
c = list(map(int, input().split()))
d=0
for i in range(a):
    if b[i] <= c[i]: #討伐の方が多い
        d += b[i]
        if (c[i] - b[i]) <= b[i+1]:
            b[i+1] =b[i+1] - (c[i] - b[i])
            d += (c[i] - b[i])
        else:
            d += b[i+1]
            b[i+1] = 0
    else:
        d += c[i]

print(d)

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()] 
C = np.zeros(N+1)
count = 0
for i in range(N):
  a = A[i] - B[i] -C[i]
  if a >= 0:
    count += B[i] + C[i]
  else:
    count += A[i]
    if A[i] - C[i] < 0:
      C[i+1] = B[i]
    else:
      C[i+1] = -a
if C[N] > 0:
  if A[N] < C[N]:
    count += A[N]
  else:
    count += C[N]
print(int(count))

Submission status: Accepted

Solution code:
from copy import copy

N = int(input())
AAA = list(map(int, input().split()))
BBB = list(map(int, input().split()))

n_defeated = 0
for idx, (A, B) in enumerate(zip(AAA[:N + 1], BBB)):
    A = AAA[idx]
    Ap1 = AAA[idx + 1]
    mp_b = copy(B)
    # print('0:',AAA, mp_b)
    # position i
    if A > B:
        n_defeated += B
        AAA[idx] -= B
        mp_b = 0
    else:
        n_defeated += A
        AAA[idx] = 0
        mp_b = B - A
    # position i+1
    # print('1:',AAA, mp_b)
    if Ap1 > mp_b:
        n_defeated += mp_b
        AAA[idx + 1] -= mp_b
        mp_b = 0
    else:
        n_defeated += Ap1
        AAA[idx + 1] = 0
        mp_b -= Ap1
    # print('2:',AAA, mp_b)

print(n_defeated)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a = [i for i in map(int,input().split())]
b = [i for i in map(int,input().split())]
#print(alist)
#print(blist)
ene = 0
for i in range(N):
  if a[i]+a[i+1] >= b[i]:
    ene += b[i]
    if a[i] - b[i] <0:
	    a[i+1] = a[i]+a[i+1]-b[i]
  else:
    ene += a[i]+a[i+1]
    a[i+1] = 0
  #print(ene)
print(ene)

Submission status: Accepted

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


for i in range(N):
	#Bが大きい場合
	if B[i]-A[i] >= 0:
		B[i] = B[i]-A[i]
		A[i] = 0
		if B[i]-A[i+1]>=0:
			A[i+1] = 0
			B[i] -=A[i+1]
		else:
			A[i+1] -= B[i]
	#Aが大きい場合
	else:
		A[i] -= B[i]


print(sum_A - sum(A))

Submission status: Accepted

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

print(d)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(_) for _ in input().split()]
b=[int(_) for _ in input().split()]
c=0
for i in range(n):
    if a[i] <= b[i]:
        c += a[i]
        if a[i+1] <= b[i]-a[i]:
            c += a[i+1]
            a[i+1] = 0
        else:
            c += b[i]-a[i]
            a[i+1] -= b[i]-a[i]
    else:
        c += b[i]
print(c)

Submission status: Accepted

Solution code:
import logging

logging.basicConfig(level=logging.INFO, format="%(message)s")
#logging.disable(logging.CRITICAL)

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

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

    if B[N - 1] > A[N]:
        counter += A[N]
    elif B[N - 1] != 0:
        counter += B[N - 1]

    print(counter)
            
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import numpy as np
N=int(input())
A=np.array(list(int(i) for i in input().split())  )
B=np.array(list(int(i) for i in input().split())  )

num=np.sum(A)

for i in range(len(B)):
    if A[i]<=B[i]:
        B[i]=B[i]-A[i]
        A[i]=0
    else:
        A[i]=A[i]-B[i]
        B[i]=0
    if A[i+1]>=B[i]:
        A[i+1]=A[i+1]-B[i]
    else:
        A[i+1]=0
        B[i]=B[i]-A[i+1]
print(num-np.sum(A))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
cnt = 0
dp_1 = [0]*(n)
dp_1[0] = min(a[0], b[0])
dp_2 = [0]*(n)
dp_2[0] = min(a[1], b[0]-min(a[0], b[0]))
for i in range(n-1):
    dp_1[i+1] = min(b[i+1], a[i+1] - dp_2[i])
    dp_2[i+1] = min(a[i+2], b[i+1] - dp_1[i+1])
print(sum(dp_1+dp_2))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
B = [0] + B + [0]
total_ene = sum(A)


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

max_lose_ene = total_ene - survive_enemy

print(max_lose_ene)

Submission status: Accepted

Solution code:
N=int(input())
# L=[]
# for i in range(N):
#     L.append(int(read_input()))

A=[int(x) for x in input().split()]
B=[int(x) for x in input().split()]

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

i = 0
total = 0
C = [A[0]]
while i < N:
  C.append(A[i+1])
  if C[0] >= B[i]:
    total += (C[0] - B[i])
    del C[0]
  else:
    if sum(C) - B[i] >= 0:
      tmp = sum(C) - B[i]
    else:
      tmp = 0
    C.clear()
    C.append(tmp)
  i += 1 
    
print(sum(A) - (total+sum(C)))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
input()
a=map(int,input().split())
b=map(int,input().split())
i=next(a)
r=0
for j,k in zip(a,b):
	s=min(i,k)
	k-=s
	t=max(min(j,k),0)
	j-=t
	i=j
	r+=s+t
print(r)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(x) for x in input().split()]  # monster
b = [int(x) for x in input().split()]  # braver

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] < 0:
      a[i+1] = 0
    else:
      a[i+1] = a[i+1] - b[i]
  else:   # 一人の勇者が倒せる数よりもモンスターが多い場合
    a[i] = a[i] - b[i]
    

print(total - sum(a))

Submission status: Accepted

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

Submission status: Accepted

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

temp = 0

for i, j in enumerate(A):
    if i == N:
        break
    if j >= B[i]:
        temp += B[i]
        B[i] = 0
        continue
    else:
        temp += j
        B[i] = B[i] - j

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

print(temp)

Submission status: Accepted

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

count = 0
rem = 0	#余剰戦力
for i in range(N):
	if A[i]>=B[i]+rem:
		count += B[i]+rem
		rem = 0
	else:
		count += A[i]
		if (B[i]+rem-A[i])>A[i+1]:
			rem = A[i+1]
		else:
			rem = B[i]+rem-A[i]
count += rem #N+1番目の街

print(count)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
a = A[::-1]
b = B[::-1]
Sum = 0
for k in range(N-1,-1,-1):
    if b[k] >= a[k+1]:
        Sum += a[k+1]
        b[k] = b[k] - a[k+1]
    else:
        Sum += b[k]
        b[k] = 0
    if b[k] - a[k] >= 0:
        Sum += a[k]
        a[k] = 0
    else:
        Sum += b[k]
        a[k] = a[k] - b[k]
print(Sum)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
m = 0
ai = 0
m_l = []

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

print(m)

Submission status: Accepted

Solution code:
from collections import defaultdict


if __name__=="__main__":
    brave_n = int(input())
    monster_dic = defaultdict(int)
    brave_dic = defaultdict(int)
    monster_n = list(map(int, input().split()))
    brave_can = list(map(int, input().split()))
    kill_count = 0

    for i, m in enumerate(monster_n):
        monster_dic[i] = m

    for i, b in enumerate(brave_can):
        brave_dic[i] = b

    for i in range(len(brave_dic)):

        if brave_dic[i] >= monster_dic[i]:
            kill = monster_dic[i]
            monster_dic[i] -= kill
            brave_dic[i] -= kill
            kill_count += kill
            if brave_dic[i] >= monster_dic[i+1]:
                kill = monster_dic[i+1]
                monster_dic[i+1] -= kill
                brave_dic[i] -= kill
                kill_count += kill
            else:
                kill = brave_dic[i]
                monster_dic[i+1] -= kill
                brave_dic[i] -= kill
                kill_count += kill
        else :
            kill = brave_dic[i]
            monster_dic[i] -= kill
            brave_dic[i] -= kill
            kill_count += kill

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

Submission status: Accepted

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

import math

debug = False

def log(text):
    if debug:
        print(text)

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        lines.append(input())
        lines.append(input())
        lines.append(input())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]
    
    token = lines[0].split(" ")
    n = int(token[0])
    a = [int(i) for i in lines[1].split(" ")]
    b = [int(i) for i in lines[2].split(" ")]

    return (n, a, b)


def solve(n, a, b):
    
    result = 0
    for i in range(n):
        
        if a[i] >= b[i]:
            c = b[i]
            e = 0
            a[i] = a[i] - c
        else:
            c = a[i]
            d = b[i] - c
            a[i] = 0
            if a[i+1] >= d:
                e = d
                a[i+1] = a[i+1] - d
            else:
                e = a[i+1]
                a[i+1] = 0
        
        result = result + (c+e)

    return result


def main():
    # 出力

    print("%s" % solve(*parse_input()))


if __name__ == '__main__':

    main()

Submission status: Accepted

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

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

total += min(m, a[-1])
print(total)

Submission status: Accepted

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

for x in range(len(A)-1):
    count += min(A[x], B[x])
    nokori = B[x] - min(A[x], B[x])
    if nokori >= 0:
        count += min(A[x+1], nokori)
        A[x+1] -= min(A[x+1], nokori)
print(count)

Submission status: Accepted

Solution code:
# 先頭のB1から順番に処理をする
# Biはi番目に残っている敵の数をCiとして
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
sum = 0
c = a
for i in range(n):
    if c[i] > b[i]:
        sum += b[i] # 同じ街の敵のみ倒して終了
    elif c[i+1] > b[i] - c[i]: #次の街の敵も倒せるが、途中で力つきる
        sum += b[i]
        c[i+1] = c[i+1]-b[i]+c[i]
    else: #次の街まで全員倒した
        sum += c[i+1] + c[i]
        c[i+1] = 0
print(sum)