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()))
A += [0]
count = 0
for index, b in enumerate(B):
    remain = b - A[index]
    if remain > 0:
        count += A[index]
        if remain - A[index + 1] > 0:
            count += A[index + 1]
        else:
            count += remain
    else:
        count += b
print(count)

Submission status: Wrong Answer

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

i = 0

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

Submission status: Wrong Answer

Solution code:
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

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

Submission status: Wrong Answer

Solution code:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
N = int(raw_input())
A = [int(i) for i in raw_input().split()]
B = [int(i) for i in raw_input().split()]

rest = [0] * N
score = [0] * (N + 1)

for i in range(0, N + 1):
    if i == 0:
        if A[i] < B[i]:
            score[i] = A[i]
            rest[i] = B[i] - A[i]
        else:
            score[i] = B[i]
            rest[i] = 0
    elif i == N:
        score[i] = min([A[i], rest[i - 1]])
    else:
        if (A[i] - rest[i - 1]) < B[i]:
            score[i] = A[i]
            rest[i] = min([B[i] - A[i] + rest[i - 1], B[i]])
        else:
            score[i] = B[i]
            rest[i] = 0
total = 0
for i in score:
    total = total + i

print total

Submission status: Wrong Answer

Solution code:
# C
import numpy as np

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

accum_a = a[1:]+a[:-1]

filled = np.zeros(n+1, dtype=np.bool)
filled[:n][accum_a < b] = True
filled[1:][accum_a < b] = True
used = np.zeros(n, dtype=np.bool)
used[accum_a < b] = True
result = np.sum(filled*a)
result += np.sum(b*(1-used))
print(result)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
num = 0
cnt = 0
inc = 0
"""
print(n)
print(a)
print(b)
"""

for i in a:
  """
  print("num: %d" % num)
  print("cnt: %d" % cnt)
  print("inc: %d" % inc)
  print("---")
  """
  if num >= n:
    if inc > 0 and inc <= i:
      cnt += inc
      inc = 0
    elif inc > 0 and inc > i:
      cnt += i
      inc = 0
    break
  elif num < n:
    b[num] += inc
  
  if i > b[num]:
    cnt += b[num]
    num += 1
    inc = 0
  else:
    inc = b[num] - i
    b[num] -= i
    cnt += i
    num += 1
print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
num = 0
cnt = 0
inc = 0
"""
print(n)
print(a)
print(b)
"""

for i in a:
  """
  print("num: %d" % num)
  print("cnt: %d" % cnt)
  print("inc: %d" % inc)
  print("---")
  """
  if num >= n:
    if inc > 0 and inc <= i:
      cnt += inc
      inc = 0
    break
  elif num < n:
    b[num] += inc
  
  if i > b[num]:
    cnt += b[num]
    num += 1
    inc = 0
  else:
    inc = b[num] - i
    b[num] -= i
    cnt += i
    num += 1
print(cnt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N=int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans=0
def f(a,b):
  if a>=b:
    return b
  else:
    return a
for i in range(N):
  ans+=f(A[i]+A[i+1],B[i])
  A[i+1]-f(A[i]+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()))

ans = B[:] + [0]

for i in range(N)[::-1]:
    remain = A[i + 1] - ans[i + 1]
    if remain > 0:
        if (ans[i] - remain) >= 0:
            ans[i] -= remain
            ans[i+1] += remain
        elif (ans[i] - remain) < 0:
            ans[i] = 0 
            ans[i + 1] += ans[i]
    else:
        ans[i + 1] = A[i + 1]
        
if A[0] < ans[0]:
    ans[0] = A[0]
    
print(sum(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()]
S=0
i=0
while i==0:
    i=0
    j=0
    for x in range(0,N):
        if A[x]+A[x+1]<=B[x] and B[x]!=0:
            S=S+A[x]+A[x+1]
            A[x]=0
            A[x+1]=0
            B[x]=0
            j=1
    if j==0:
        i=1
for x in range(0,N):
    S=S+B[x]
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):
    if a[i] < b[i]:
        if a[i]+a[i+1] <= b[i]:
            c += a[i]+a[i+1]
            a[i+1] = 0
        else:
            c += a[i]
            a[i+1] -= (b[i]-a[i])
    elif a[i] >= b[i]:
        c += b[i]
    
print(c)

Submission status: Wrong Answer

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

l=[0]*n
m=[0]*n

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

Submission status: Wrong Answer

Solution code:
n = int(input())
monsters = [int(value) for value in input().split()]
heros = [int(value) for value in input().split()]

#倒すことのできるモンスターの数
defeatable_monsters = 0

#リストを逆順にする
rev_monsters = monsters[::-1]
rev_heros = heros[::-1]

for offset, hero in enumerate(rev_heros):
    if hero >= rev_monsters[offset]:
        defeatable_monsters += rev_monsters[offset]
        hero -= rev_monsters[offset]
    else:
        defeatable_monsters += hero
        rev_monsters[offset] -= hero
        hero = 0
        continue

    if hero >= rev_monsters[offset+1]:
        defeatable_monsters += rev_monsters[offset+1]
        hero -= rev_monsters[offset+1]
    else:
        defeatable_monsters += hero
        rev_monsters[offset+1] -= hero
        hero = 0


print(defeatable_monsters)

Submission status: Wrong Answer

Solution code:
n = int(input())
monsters = [int(value) for value in input().split()]
heros = [int(value) for value in input().split()]

#倒すことのできるモンスターの数
defeatable_monsters = 0

#リストを逆順にする
rev_monsters = monsters[::-1]
rev_heros = heros[::-1]

for offset, hero in enumerate(rev_heros):
    if hero > rev_monsters[offset]:
        defeatable_monsters += rev_monsters[offset]
        hero -= rev_monsters[offset]
    else:
        defeatable_monsters += hero
        rev_monsters[offset] -= hero
        hero = 0
        continue

    if hero > rev_monsters[offset+1]:
        defeatable_monsters += rev_monsters[offset+1]
        hero -= rev_monsters[offset+1]
    else:
        defeatable_monsters += hero
        rev_monsters[offset+1] -= hero
        hero = 0


print(defeatable_monsters)

Submission status: Wrong Answer

Solution code:
n = int(input())
monsters = [int(value) for value in input().split()]
heros = [int(value) for value in input().split()]

#倒すことのできるモンスターの数
defeatable_monsters = 0

#リストを逆順にする
rev_monsters = monsters[::-1]
rev_heros = heros[::-1]

for offset, hero in enumerate(rev_heros):
    if hero > rev_monsters[offset]:
        defeatable_monsters += rev_monsters[offset]
        hero -= rev_monsters[offset]
    else:
        defeatable_monsters += hero
        rev_monsters[offset] -= hero
        hero = 0

    if hero > rev_monsters[offset+1]:
        defeatable_monsters += rev_monsters[offset+1]
        hero -= rev_monsters[offset+1]
    else:
        defeatable_monsters += hero
        rev_monsters[offset+1] -= hero
        hero = 0


print(defeatable_monsters)

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

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

count = 0
for i in range(N):
    n_defeat = min(A[i], B[i])
    A[i] -= n_defeat
    B[i] -= n_defeat
    count += n_defeat
    n_defeat = min(A[i + 1], B[i])
    A[i] -= n_defeat
    B[i] -= n_defeat
    count += n_defeat

print(count)

Submission status: Wrong Answer

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

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, b in enumerate(B):
    if b <= A[i]:
        ans += b
    else:
        ans += A[i]
        b -= A[i]
        if b <= A[i+1]:
            ans += b
            A[i+1] -= b
        else:
            ans += A[i+1]

print(ans)

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(n):
    # モンスターが倒しきれないとき
    if a[i] + a[i+1] > b[i]:
        count += b[i]
    
    # モンスターを倒し切るとき
    else:
         count += 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()))

count = 0

for i in range(n):
    # モンスターが倒しきれないとき
    if a[i] + a[i+1] >= b[i]:
        count += b[i]
    
    # モンスターを倒し切るとき
    else:
         count += a[i] + a[i+1]

print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
monsters = [int(x) for x in input().rstrip("\n").split(" ")]
damages = [int(x) for x in input().rstrip("\n").split(" ")]
total_monsters = sum(monsters)
result = 0
remain = monsters[n]
for i in range(n-1,-1,-1):
  damage = damages[i]
  if damage <= remain:
    result += remain - damage
    remain = monsters[i]
  else:
    damage -= remain
    if monsters[i] >= damage:
      remain = monsters[i] - damage
    else:
      remain = 0
print(total_monsters - result)

Submission status: Wrong Answer

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

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

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

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

killed_monster = 0
for i in reversed(range(N)):
  A_B = A[i] + A[i+1] - B[i]
  if A_B >= 0:  #B[i]だけ殺す
    killed_monster += B[i]
    A[i] = A_B
  else:         #皆殺し
    killed_monster += A[i] + A[i+1]
    A[i] = 0

print(killed_monster)

Submission status: Wrong Answer

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

print(cnt)

Submission status: Wrong Answer

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

'''
人iは街iと街1+1を倒せる
nokori = 次の人が倒すしかない数
'''

cnt = 0
nokori = 0


if a[0] > b[0]:
    cnt += a[1]
    nokori = a[0]
else:
    cnt += min(b[0],a[0]+a[1])
    nokori = a[0]+a[1]-b[0]

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

print(cnt)

Submission status: Wrong Answer

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

N = int(input())
A = [int(v) for v in input().split()]
B = [int(v) for v in input().split()]
s = [0] * (N + 1)

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

Submission status: Wrong Answer

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

ans=0
if len(b) < n+1:
  b.append(0)

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

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

Submission status: Wrong Answer

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

import sys
import copy

sys.setrecursionlimit(1000000)

#input = sys.stdin.readline
from collections import defaultdict

# ~~~~~~~~~~~~~~~~~~~~~_(＾～＾ ｣ ∠)_~~~~~~~~~~~~~~~~~~~~~

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

print(ans)

Submission status: Wrong Answer

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

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

print(attack)

Submission status: Wrong Answer

Solution code:
def resolve():

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

    ans = 0

    for i in range(n):

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

    print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
b.append(0) #n+1番目を作っとく
num=0
c=0
for i in range(n):
    if a[i]<=c:
        num+=a[i]
        c=b[i]
    else:
        a[i]-=c
        if b[i]>=a[i]:
            num+=a[i]
            c=b[i]-a[i]
        else:
            num+=b[i]
            c=0
        
if b[n]+c>=a[n]:
    num+=a[n]
else:
    num+=b[n]+c
print(num)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:

N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split())) + [0]
sums = sum(A)
subs = 0
tmp = 0
for i in range(N+1):
    A[i] -= tmp
    cmp = A[i] - B[i]
    if cmp > 0:
        subs += cmp
        tmp = 0
    else:
        tmp = -cmp

ans = sums - subs
    
A.reverse()
B.reverse()
subs = 0
tmp = 0
for i in range(N+1):
    A[i] -= tmp
    cmp = A[i] - B[i]
    if cmp > 0:
        subs += cmp
        tmp = 0
    else:
        tmp = -cmp
    
ans = max(ans,sums-subs)
print(ans)

Submission status: Wrong Answer

Solution code:

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

Submission status: Wrong Answer

Solution code:

N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
sums = sum(A)
subs = 0
tmp = 0
for i in range(N):
    if A[i] > B[i]+tmp:
        subs += A[i]-B[i]-tmp
    else:
        if tmp > A[i]:
            tmp = B[i]
        else:
            tmp = B[i] + tmp - A[i]
            
if A[N] > tmp:
    subs += A[N] - tmp

print(sums - subs)

Submission status: Wrong Answer

Solution code:

N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
sums = sum(A)
nokori = 0
subs = -A[0] + B[0]
if subs < 0:
    nokori += subs
    
for i in range(1,N):
    if subs > 0:
        A[i] = max(0, A[i]-subs)
    
    subs = B[i] - A[i]
    if subs < 0:
        nokori += -subs
 
subs = -A[N] + subs    
if subs < 0:
    nokori += subs

print(sums + nokori)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
b.append(0) #n+1番目を作っとく
num=0
for i in range(n):
    if b[i]>=a[i]:
        num+=a[i]
        b[i+1]+=b[i]-a[i]
    else:
        num+=b[i]
if b[n]>=a[n]:
    num+=a[n]
else:
    num+=b[n]
print(num)

Submission status: Wrong Answer

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

counter =[]
for i,Bi in enumerate(B):
  counter.append(Bi-A[i]-A[i+1])
  
ID=counter.index(max(counter))
#print(ID)

counter =0
for i in range(ID,len(B),1):
  if A[i] >= B[i]:
    counter +=B[i]
    A[i] -= B[i]
    B[i] = 0
    continue
  else:
    counter += A[i]
    B[i] -= A[i]
    A[i] =0
    if A[i+1] >= B[i]:
      counter +=B[i]
      A[i+1] -= B[i]
      B[i] =0
      continue
    else:
      counter += A[i+1]
      B[i] -= A[i+1]
      A[i+1] =0
for i in range(ID-1,-1,-1):
  if A[i+1] >= B[i]:
    counter +=B[i]
    A[i+1] -= B[i]
    B[i] = 0
    continue
  else:
    counter += A[i]
    B[i] -= A[i]
    A[i] =0
    if A[i] >= B[i]:
      counter +=B[i]
      A[i] -= B[i]
      B[i] =0
      continue
    else:
      counter += A[i]
      B[i] -= A[i]
      A[i] =0
print(counter)

Submission status: Wrong Answer

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


counter =0
for i,Bi in enumerate(B):
  if A[i] >= Bi:
    counter +=Bi
    continue
  else:
    counter += A[i]
    Bi -= A[i]
    if A[i+1] >= Bi:
      counter +=Bi
      A[i+1] -= Bi
      continue
    else:
      counter += A[i+1]
print(counter)

Submission status: Wrong Answer

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

sur = 0 #前の街で倒した勇者の余力
sum = 0

for i in range(n):
    if A_list[i] >= B_list[i] + sur:
        sum += B_list[i]
        sur = 0
    elif A_list[i] <= sur:
        sum += A_list[i]
        sur = B_list[i]
    elif A_list[i] < B_list[i] + sur:
        sum += A_list[i]
        sur = B_list[i] + sur - A_list[i]

if sur > A_list[n]:
    sur = A_list[n]

print(sum + sur)

Submission status: Wrong Answer

Solution code:
#coding: UTF-8

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

Ans = 0

for num in range(N):
    Ans += min(A[num], B[num])
    if A[num] > B[num]:
        pass
    else: # A[num] < B[num]
        B[num] -= A[num]
        Ans += min(A[num+1], B[num])
        if A[num+1] > B[num]:
            pass
        else:
            A[num+1] = 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()]
cnt = 0

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

Submission status: Wrong Answer

Solution code:
from sys import stdin


def main():
    N = int(input())
    A_lists = [int(x) for x in stdin.readline().rstrip().split()]
    B_lists = [int(x) for x in stdin.readline().rstrip().split()]

    kazyou = 0
    sum_ = 0

    for A, B in zip(A_lists, B_lists):
        if (A-kazyou) <= B:
            sum_ += A
            if A-kazyou < 0:
                kazyou = B
            else:
                kazyou = abs((A-kazyou)-B)
            # print(sum_, kazyou)
        else:
            sum_ += B
            kazyou = 0
            # print(sum_, kazyou)

    if A_lists[N] <= kazyou:
        sum_ += A_lists[N]
    else:
        sum_ += kazyou

    print(sum_)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from sys import stdin


def main():
    N = int(input())
    A_lists = [int(x) for x in stdin.readline().rstrip().split()]
    B_lists = [int(x) for x in stdin.readline().rstrip().split()]

    kazyou = 0
    sum_ = 0

    for A, B in zip(A_lists, B_lists):
        if A-kazyou <= B:
            sum_ += A
            kazyou = abs((A-kazyou)-B)
            # print(sum_, kazyou)
        else:
            sum_ += B
            kazyou = 0
            # print(sum_, kazyou)

    if A_lists[N] <= kazyou:
        sum_ += A_lists[N]
    else:
        sum_ += kazyou

    print(sum_)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
import math
Mod = 1000000007



def main(): #startline-------------------------------------------
    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])
        B[i] -= min(A[i],B[i])
        ans += min(A[i+1],B[i])
        B[i] -= min(A[i+1],B[i])
    print(ans)
if __name__ == "__main__":
    main() #endline===============================================




def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n < 2 is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))):
        if prime[i] == 1:
            for j in range(2*i,n):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n):
        if prime[i] == 1:
            res.append(i)
    return res

class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py

    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)

Submission status: Wrong Answer

Solution code:
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
import math
Mod = 1000000007



def main(): #startline-------------------------------------------
    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]))
    print(ans)
if __name__ == "__main__":
    main() #endline===============================================




def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n < 2 is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))):
        if prime[i] == 1:
            for j in range(2*i,n):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n):
        if prime[i] == 1:
            res.append(i)
    return res

class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py

    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)

Submission status: Wrong Answer

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

m_count = 0
for i in range(N):

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

print(m_count)

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

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

Submission status: Wrong Answer

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

if a[0] > bb:
  out+=bb
else:
  out+=a[0]
print(out)