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: Wrong Answer

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

#余りのモンスター
amari = 0
total = sum(A)

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

amari += A[N]

print(total - amari)

Submission status: Wrong Answer

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

for i in range(len(a)-1):
	count = count+min(b[i],(a[i]+a[i+1]))

print(count)

Submission status: Wrong Answer

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

answer = 0

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

        if A[i] < B[i-1]:
            answer += A[i]
            B[i-1] -= A[i]
            A[i] = 0

        else:
            answer += B[i-1]
            A[i] -= B[i-1]
            B[i-1] = 0

    else:
        if A[i] < B[i] + B[i-1]:
            answer += A[i]
            if A[i] < B[i-1]:
                B[i-1] -= A[i]
                A[i] = 0
            else:
                B[i-1] = 0
                B[i] -= (A[i]-B[i-1])
                A[i] = 0
        else:
            answer += (B[i]+B[i-1])
            A[i] -= (B[i]+B[i-1])
            B[i],B[i-1] = 0,0

print(answer)

Submission status: Wrong Answer

Solution code:
N= map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C =[]
T_ALL=0
T=0
R=0
for i in range(0,len(B),1):
    MON=A[i]   #i番目の場所のモンスター数
    POW=B[i]+R #i番目の場所のパワー
    T=min(MON,POW) #i番目の場所で倒した数

    R=POW-T #残り倒せる数
    T_ALL=T_ALL+T #i番目の場所までに倒した合計

MON=A[len(A)-1]
POW=R
T=min(MON,POW)
T_ALL=T_ALL+T
print(T_ALL)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
N =int(input())
A= np.array( list(map(int,input().split())))
B= np.array( list(map(int,input().split())))
result = 0
l=1
for i in range(1,N+1):
    if B[N-i]> A[N-i+1]:
        result+= A[N-i+1]
        B[N-i] +=-A[N-i+1]
        if B[N-i]>A[N-i]:
            result+=A[N-i]
            B[N-i] +=-A[N-i]
        else:
            result+= B[N-i]
            A[N-i]+=-B[N-i]
    else:
        result+= B[N-i]
        A[N-i+1]+=-B[N-i]
print(result)

Submission status: Wrong Answer

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

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

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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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

if A[-1] + A[-2] >= B[-1]:
    s = B[-1] 
    remain = A[-1] + A[-2] - B[-1]
else:
    s = A[-1] + A[-2]
    remain = 0

for i in range(N-2, -1, -1):
    if remain >= B[i]:
        s += B[i]
        remain = A[i]
    elif remain + A[i] >= B[i]:
        s += B[i]
        remain = remain + A[i] - B[i]
    else:
        s += remain + A[i]
        remain = 0

print(s)

Submission status: Wrong Answer

Solution code:
from sys import stdin


def main():
    N = int(stdin.readline().rstrip())
    As = [int(x) for x in stdin.readline().rstrip().split()][::-1]
    Bs = [int(x) for x in stdin.readline().rstrip().split()][::-1]
    result = 0
    for i, a in enumerate(As):
        if i == 0:
            b = Bs[i]
            if a <= b:
                result += a
                Bs[i] -= a
            else:
                result += b
                Bs[i] = 0
        elif i == N:
            b = Bs[i - 1]
            if a <= b:
                result += a
                Bs[i - 1] -= a
            else:
                result += b
                Bs[i - 1] = 0
        else:
            b = Bs[i - 1]
            if a <= b:
                result += a
                Bs[i - 1] -= a
            else:
                result += b
                Bs[i - 1] = 0
            b = Bs[i]
            if a <= b:
                result += a
                Bs[i] -= a
            else:
                result += b
                Bs[i] -= b
    print(result)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys;
import string;
sys.setrecursionlimit(20000) # 再起呼び出しの最大数をセット

import copy
import math
import itertools

from functools import lru_cache
#@lru_cache(maxsize = None)

PPP = 1000000007

# 整数の入力
n = int(input())

# スペース区切りの入力
#n, x = input().split()

# スペース区切りの整数の入力
#a, b = map(int, input().split())
#n, x = map(int, input().split())
#print("sp", n, k, file=sys.stderr)
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
#print("a", a, file=sys.stderr)
#p, q, r = map(int, input().split())
#s = [int(x) for x in input().split()]
#t = [int(x) for x in input().split()]

# 改行区切りの整数の入力
#a = []
#for x in range(n):
#  a.append(int(input()))
#  a.append([int(x) for x in input().split()])
#b = []
#for x in range(m):
#  b.append([int(x) for x in input().split()])
#p = [int(x) for x in input().split()]
#a.sort()
#print(l, file=sys.stderr)

# 文字列の入力
#s = input()

# 出力
#print("{} {}".format(a+b+c, s))
#a.sort()
#print(set(a), file=sys.stderr)
#print(len(set(a)))

#cache = dict()

import re
import fractions

def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)

def ton(n, k, x):
  s = ""
  while n > 0:
    s = str(n%k) + s
    n //= k
  return s


#print("a", a, file=sys.stderr)
#print("b", b, file=sys.stderr)

def getEnergy():
  for y in b:
    yield y

def doit():
  begin = 0
  end = n
  ng = 0
  total = 0
  for i in range(n):
    ab = min(a[i], b[i])
    total += ab
    b[i] -= ab
    ba = min(a[i+1], b[i])
    total += ba
  
  return total

def yn():
  if doit():
    return "YES"
  else:
    return "NO"

#print(doit([0] * 4))
#doit(a)
#print(doit())
try:
  #doit()
  print(doit())
  #print(yn())
except Exception as e:
  print("e", e, file=sys.stderr)

Submission status: Wrong Answer

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

print(temp)

Submission status: Wrong Answer

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

sum_ = 0
rest = 0

for i in range(n):
  if a[i] >= b[i] + rest:
    sum_ += b[i] + rest
    rest = 0
  elif a[i] >= rest:
    sum_ += a[i]
    rest = b[i]
  else:
    sum_ += a[i]
    rest = b[i]

sum_ += min(a[n], rest)
print(sum_)

Submission status: Wrong Answer

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

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

print(enemy)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
b.append(0)
c=[0]*(n+1)
t=0
for i in range(n+1):
    
    c[i]=min(a[i],b[i]+t)
    t=b[i]-(a[i]-t)
    if t<0:
        t=0
print(sum(c))

Submission status: Wrong Answer

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

count = 0
can_kill = 0
for i in range(N+1):
  target = A[i] - can_kill
  if i != N:
    if target <= B[i]:
      count += A[i]
      can_kill = B[i] - target
    else:
      count += B[i]
      can_kill = 0
  else:
    if A[i] > can_kill:
      count += can_kill
    else:
      count += A[i]
  #print("can_kill : {}".format(can_kill))
  #print("count : {}".format(count))
print(count)

Submission status: Wrong Answer

Solution code:
n=int(input())  #数値入力 「N」だけの入力のとき
#n=2
a=list(map(int, input().split()))  #リスト入力 「a1 a2 a3 ...」みたいな配列のような入力のとき
#a = [100, 1, 1]
b=list(map(int, input().split()))  #リスト入力 「a1 a2 a3 ...」みたいな配列のような入力のとき
#b=[1,100]
ans = 0

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import copy
from itertools import accumulate
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))

OA = copy.deepcopy(A)
#M = [A[i] - B[i] if A[i] >= B[i] else 0 for i in range(N)]
#print(M)
#SM = list(accumulate(M))
#print(SM)
#SA = accumulate(A)
#SB = accumulate(B)
#print()

count = 0
for i in range(N):
    nokori = A[i] - B[i]
    if A[i] - B[i] >= 0 :
        count += nokori
    else :
        A[i + 1] -= abs(nokori)
        if A[i + 1] < 0 :
            A[i + 1] = 0
print(sum(OA) - count + A[N])

Submission status: Wrong Answer

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

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

if A[-1] + A[-2] >= B[-1]:
    s = B[-1] 
    remain = A[-1] + A[-2] - B[-1]
else:
    s = A[-1] + A[-2]
    remain = 0

for i in range(N-2, -1, -1):
    if remain > B[i]:
        s += B[i]
        remain = A[i]
    elif remain + A[i] > B[i]:
        s += B[i]
        remain = remain + A[i] - B[i]
    else:
        s += remain + A[i]
        remain = 0

print(s)

Submission status: Wrong Answer

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

C=0
for i in range(N):
  p=B[i]
  
  #town1
  if p<=A[i]:
    p=0
  else:
    p-=A[i]
  #town2
  if p!=0 or i!=len(A)-1:
    if A[i+1]<p:
      p-=A[i+1]
    else:
      A[i+1]-=p
      p=0
  
  C+=B[i]-p
  
print(C)

Submission status: Wrong Answer

Solution code:
N  = int(input())
mon_arr = list(map(int, input().split()))
two_mon_arr = [mon_arr[i] + mon_arr[i+1] for i in range(N)]
hero_arr = list(map(int, input().split()))

min_arr = [min(two_mon_arr[i],hero_arr[i])  for i in range(N)]

print(sum(min_arr))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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]:
        cnt += B[i]
        B[i] = 0
        A[i] = 0
    else:
        cnt += min(B[i],A[i]+A[i+1])
        A[i] = 0
        A[i+1] = max(0,A[i+1]-(B[i]-A[i]))
    # print(A)
    # print(B)
print(cnt)

Submission status: Wrong Answer

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

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

s2 = 0
for i in range(N):
  d = rA[i] - rB[i]
  if d <= 0:
    s2 += rA[i]
    if rA[i+1] + d <= 0:
      s2 += rA[i+1]
    else:
      s2 += -d
      rA[i+1] += d
  else:
    s2 += rB[i]
print(max(s1, s2))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

beats = list()
count = 0
for i in range(n):
    beat = min(Bs[i], As[i])
    count += beat
    Bs[i] -= beat
    As[i] -= beat

    beat = min(Bs[i], As[i + 1])
    count += beat
    Bs[i] -= beat
    As[i] -= beat

    # print(As)
    # print(Bs)
    
print(count)
# print(beats)

# if As[n] > Bs[n - 1]:
#     beat = Bs[n - 1]
# else:
#     beat = As[n]

Submission status: Wrong Answer

Solution code:
n=int(input())

monst=list(map(int,input().split()))
yusya=list(map(int,input().split()))

monst.reverse()
yusya.reverse()
ans=0
for i in range(n):
  if(i==0):
    kill=min(yusya[0],monst[0])
    ans+=kill
    yusya[0]-=kill
    monst[0]-=kill
  else:
    kill=min(yusya[i-1],monst[i])
    ans+=kill
    yusya[i-1]-=kill
    monst[i-1]-=kill
    kill=min(yusya[i],monst[i])
    ans+=kill
    yusya[i]-=kill
    monst[i]-=kill
    
ans+=min(monst[-1],yusya[-1])

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

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

ans=0
avail=0

for i in range(N+1):
	a=A[i]
	if i<N: avail+=B[i]
	if 0<a:
		if a<=avail:
			ans+=a
			avail-=a
		elif avail<a:
			ans+=avail
			avail-=avail

print ans

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

p = 0
ans = 0

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

if A[N] <= p:
    ans += A[N]
else:
    ans += p

print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
a=[]
b=[]
ah=[]
bh=[]
c=int(input())
for x in input().split(" "):
    a.append(int(x))
for x in input().split(" "):
    b.append(int(x))

"""for x in a:
    ah.append(x)
for x in b:
    bh.append(x)"""

ah=a
bh=b

i=0
n=0

while i<c:
    if ah[i]>bh[i]:
        n+=bh[i]
        ah[i]=ah[i]-bh[i]
    else:
        n+=ah[i]
        ah[i]=0
        bh[i]-=ah[i]
        if ah[i+1]>bh[i]:
            n+=bh[i]
            ah[i+1]-=bh[i]
        else:
            n+=ah[i+1]
            ah[i+1]=0
    i+=1

print(n)

Submission status: Wrong Answer

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

cnt = 0
tmp = 0
for i in range(N):
    tmp_mon = A[i] - tmp
    cnt += min(B[i], tmp_mon) + tmp
    if B[i] > tmp_mon:
        tmp = B[i] - tmp_mon
    elif B[i] <= tmp_mon:
        tmp = 0

cnt += min(A[-1], tmp)

print(cnt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans = 0

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

Submission status: Wrong Answer

Solution code:
import sys

N = int(sys.stdin.readline().rstrip())
AList=list(map(int,sys.stdin.readline().split()))
BList=list(map(int,sys.stdin.readline().split()))

killed=0
for i in range (N, 0, -1):
  if AList[i] < BList[i-1]:
    killed+=AList[i]
    BList[i-1] -= AList[i]
    AList[i] = 0
  else:
    killed+=BList[i-1]
    BList[i-1] = 0
    AList[i] -= BList[i-1]
    
  if AList[i-1] < BList[i-1]:
    killed+=AList[i-1]
    BList[i-1] -= AList[i-1]
    AList[i-1] = 0
  else:
    killed+=BList[i-1]
    BList[i-1] = 0
    AList[i-1] -= BList[i-1]
print(killed)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

sum_beat = 0

for i in range(n):
    if a_list[i] + a_list[i + 1] <= b_list[i]:
        sum_beat += a_list[i] + a_list[i + 1]
        a_list[i + 1] = 0
    else:
        sum_beat += b_list[i]
        a_list[i + 1] = a_list[i + 1] - min(b_list[i] - a_list[i], 0)

print(sum_beat)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

cnt = 0

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

print(cnt)

Submission status: Wrong Answer

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

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

print(count + min(re, A[-1]))

Submission status: Wrong Answer

Solution code:
import copy
N = int(input())
liA = list(map(int,input().split()))
liB = list(map(int,input().split()))
liC = copy.deepcopy(liA)
for i in range(len(liB)):
    if liA[i] >= liB[i]:
        liA[i] = liC[i]-liB[i]
    else:
        liA[i+1] = liC[i+1] + liA[i] - liB[i]
        liA[i] = 0
print(sum(liC) - sum(liA))

Submission status: Wrong Answer

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

beats = list()
count = 0
for i in range(n):
    if As[i] != 0:
        beat = min(Bs[i], As[i])
    count += beat
    Bs[i] -= beat
    As[i] -= beat

    beat = min(Bs[i], As[i + 1])
    count += beat
    Bs[i] -= beat
    As[i] -= beat

    # print(As)
    # print(Bs)
    
print(count)
# print(beats)

# if As[n] > Bs[n - 1]:
#     beat = Bs[n - 1]
# else:
#     beat = As[n]

Submission status: Wrong Answer

Solution code:
N = int(input())
M = list(map(int, input().split()))
H = list(map(int, input().split()))

sum_m = 0
rest = 0
for i in range(N):
  if M[i] >= H[i]+rest:
    rest = 0
    sum_m += H[i]+rest
  else:
    rest += H[i]-M[i]
    sum_m += M[i]
sum_m += min(rest, M[-1])
print(sum_m)

Submission status: Wrong Answer

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

c = 0

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a_array = [int(x) for x in input().split()]
b_array = [int(x) for x in input().split()]

res = 0
amari = 0
for i in range(n):
    res += a_array[i] if b_array[i]+amari >= a_array[i] else b_array[i]+amari
    # amari = max(0, (b_array[i]+amari)-a_array[i])
    amari = 0 if b_array[i]+amari < a_array[i] else b_array[i]+amari-a_array[i]

res += a_array[-1] if amari >= a_array[-1] else amari
print(res)

Submission status: Wrong Answer

Solution code:
a=[]
b=[]
ah=[]
bh=[]
c=int(input())
for x in input().split(" "):
    a.append(int(x))
for x in input().split(" "):
    b.append(int(x))

for x in a:
    ah.append(x)
for x in b:
    bh.append(x)

i=0
n=0

while i<c:
    if ah[i]>bh[i]:
        n+=bh[i]
        ah[i]=ah[i]-bh[i]
    else:
        n+=ah[i]
        ah[i]=0
        bh[i]-=ah[i]
        if ah[i+1]>bh[i]:
            n+=bh[i]
            ah[i+1]-=bh[i]
        else:
            n+=ah[i+1]
            ah[i+1]=0
    i+=1

print(n)

Submission status: Wrong Answer

Solution code:
town_num = int(input())

monster_num = list(map(int,input().split()))

hero_power = list(map(int,input().split()))

counter = 0
left_num = 0
for i,(one,two) in enumerate(zip(monster_num,hero_power)):
    left_num = min(max(0,two+left_num-one),two)
    counter += min(two+left_num,one)
counter += min(monster_num[-1],left_num)

print(counter)

Submission status: Wrong Answer

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


def main():
    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] >= 0:
            ans += a[i]
            b[i] -= a[i]
            a[i] = 0

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

for i,h in enumerate(hl):
    if(ml[i]<=h):
        ans += ml[i]
        h -= ml[i]
        if(ml[i+1]<=h):
            ans += ml[i+1]
            h -= ml[i+1]
        else:
            ans += h
            ml[i+1] -= h
            h = 0
    else:
        ans += h
        ml[i] -= h
        h = 0

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
L = 0
for i in range(len(B)):
    i_ = N-i-1
    if A[i_ +1]<= B[i_]:
        B_ = B[i_]-A[i_+1]
        A[i_+1]=0
        A[i_]=A[i_]-B_
        if A[i_]<0:
            L += A[i_]
        L += B[i_]
    else:
        L += B[i_]
        A[i_+1]-=B[i_]
print(L)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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
    elif A[i] <= rem:
        count += A[i]
        rem = B[i]
    else:
        count += A[i]
        rem = B[i] +rem - A[i]
        
if A[N] < rem:
    count += A[i]
else:
    count+= rem
print(count)

Submission status: Wrong Answer

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

    #print(now)
    #print(A)
    #print(B)
    if A[i] <= now:
        A[i] = 0
    else:
        A[i] -= now
    #print(A)
    #print(B)
ans1 = count - sum(A)

for i in range(N):
    now = B[i] - AA[i]
    if AA[i] <= B[i]:
        AA[i] = 0
    else:
        AA[i] -=  B[i]

    #print(now)
    #print(AA)
    #print(B)
    if AA[i+1] <= now:
        AA[i+1] = 0
    else:
        AA[i+1] -= now
    #print(AA)
    #print(B)
ans2 = count - sum(AA)
print(max(ans1,ans2))

Submission status: Wrong Answer

Solution code:
N = int(input())
ahoge = input().split()
bhoge = input().split()
a = []
b = []
for i in range(N):
  a.append(int(ahoge[i]))
  b.append(int(bhoge[i]))

a.append(int(ahoge[N]))
count = 0

for i in range(N-1,-1,-1):
  hoge = b[i] - a[i+1]
  if hoge > 0:
    count += a[i+1]
    huga = hoge - a[i]
    if huga > 0:
      count += a[i]
    else:
      count += hoge
      a[i] = a[i] - hoge
  else:
    count += b[i]

print(count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a_array = [int(x) for x in input().split()]
b_array = [int(x) for x in input().split()]

res = 0
amari = 0
for i in range(n):
    res += a_array[i] if b_array[i]+amari >= a_array[i] else b_array[i]+amari
    amari = max(0, (b_array[i]+amari)-a_array[i])

res += a_array[-1] if amari >= a_array[-1] else amari
print(res)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

    ki=0
    tmp=0
    for i in range(n):
        ki+=min(a[i],b[i]+tmp)
        tmp=max(b[i]+tmp-a[i], 0)
        #print(ki,tmp)
    ki+= min(a[n], tmp)
    print(ki)


if  __name__=='__main__':
    main()

Submission status: Wrong Answer

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

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

	else:
		count = count+a[i]+a[i+1]

print(count)

Submission status: Wrong Answer

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

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

print(count + min(re, A[-1]))