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())
la = list(map(int,input().split()))
lb = list(map(int,input().split()))

total = 0
carry = 0
for i in range(n):
  tmp = la[i] - lb[i] - carry
  carry = max(min(-tmp, la[i+1]),0)
  
  if tmp >= 0:
    total += lb[i]
  else:
    total += la[i]

print(total+carry)

Submission status: Wrong Answer

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

count = 0
next_b = 0
for i, ai in enumerate(a):
  if i < n:
    bi = b[i]
    if ai > next_b:
      ai = ai - next_b
      count += next_b
    else:
      ai = 0
      next_b = next_b -ai  
      count += ai
    if ai < bi:
      count += ai
      next_b = bi -ai
    else:
      count += bi
      next_b = 0
  else:
    if ai > next_b:
      count += next_b
    else:
      count += ai
      
print(count)

Submission status: Wrong Answer

Solution code:
i1 = int(input())
i2 = input().split(' ')
i3 = input().split(' ')

w = -1
s = 0

for i in range(i1):
  ii2 = int(i2[i])
  ii3 = int(i3[i])
#  print(ii2)
#  print(ii3)

  if ii2 > ii3:
    #引ききれた場合
    s += ii3
  else:
    s += ii2
    w = ii3 - ii2

    iii2 = int(i2[i+1])

    if iii2 > w:
      s += w
      w = iii2 - w
    else:
      s += iii2
      w = 0

print(s)

Submission status: Wrong Answer

Solution code:
n = int(input())
list_a = []
list_b = []
list_a = input().split()
list_b = input().split()
list_a = list(map(int,list_a))
list_b = list(map(int,list_b))

x = 0
a = list_a[n]
for i in range(n):
    ii = n - i -1
    if a + list_a[ii] <= list_b[ii]:
        x += a + list_a[ii]
        a = 0
    else:
        a = a + list_a[ii] - list_b[ii]
        x += list_b[ii]

print(x)

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(0, N):
  rest = max(0, H[i]+rest-M[i])
  if M[i] >= H[i]+rest:
    sum_m += H[i]+rest
  else:
    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().strip().split()))
b = list(map(int, input().strip().split()))
b.append(0)
kill_count = 0
for i in range(n+1):
    kill_count += min(a[i],b[i])
    if a[i] < b[i] and i < n:
        b[i+1] += (b[i] - a[i])
print(kill_count)

Submission status: Wrong Answer

Solution code:
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; ansli = []; tmpli = []; candili = []
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]#WideIntPoint
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]#HeightString
def mi(n): return [wi() for _ in range(n)]#MatrixInt
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]

n = i()
a = wi()
b = wi()
c = 0

for i in range(n):
    ans += min(c + b[i], a[i])
    c = max(0, c + b[i] - a[i])

ans += min(c, a[n])

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
 
input = sys.stdin.readline

N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
adjacent = [A[i] + A[i+1] for i in range(N)]

kills = 0
for i in range(N):
    kills += min(adjacent[i], B[i]) 

print(kills)

Submission status: Wrong Answer

Solution code:

n = int(input())
a = list(map(int, input().strip().split()))
b = list(map(int, input().strip().split()))
b.append(0)
kill_count = 0
for i in range(n+1):
    kill_count += min(a[i],b[i])
    if a[i] < b[i] and i < n:
        b[i+1] += b[i] - a[i]
print(kill_count)

Submission status: Wrong Answer

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

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


if a[0] <= b[0]:
  a[0] = 0
else:
  a[0] = a[0] - b[0]
    


print(s - sum(a))

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+=b[i]
  elif(b[i]>=a[i]+a[i+1]):
    c+=a[i]+a[i+1]
    a[i+1]=0
  else:
    c+=b[i]
    a[i+1]=a[i+1]+a[i]-b[i]
print('{0}'.format(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)
sum1=0
rest=0
for i in range(N+1):
  x=min(a[i],b[i]+rest)
  sum1+=x
  a[i]-=min(a[i],b[i]+rest)
  b[i]=min(b[i],b[i]+rest-x)
  if a[i]==0:
    rest=b[i]

print(sum1)

Submission status: Wrong Answer

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

monster = 0
addPower = 0

for i in range(N):
    print(A[i],'+', A[i+1], ' vs ', B[i], end = ' => ')
    AA = A[i] + A[i+1]
    BA = B[i] - A[i]
    BAA= B[i] - AA
    if BAA >= 0:
        print(':', end='')
        monster += AA
        A[i+1] -= min(A[i+1], BA)
    elif BA > 0:
        print('%', end='')
        monster += B[i]
        A[i+1] -= min(A[i+1], BA)
    else:
        print('@', end='')
        monster += B[i]
    print(monster)

Submission status: Wrong Answer

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


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


if a[0] <= b[0]:
  a[0] = 0
else:
  a[0] = a[0] - b[0]
    


print(s - sum(a))

Submission status: Wrong Answer

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

c = [0]*n
count = 0
for i in range(n):
  index = n-1
  for j in range(n):
    if a[index] + a[index+1] < a[j] + a[j+1] and c[j] == 0:
       index = j
  c[index] = b[i]
for i in range(n, 0, -1):
  tmp = c[i-1]
  new_a = a[i] - min(a[i], c[i-1])
  c[i-1] = c[i-1] - a[i] if c[i-1] - a[i] > 0 else 0
  new_a_1 = a[i-1] - min(a[i-1], c[i-1])
  c[i-1] = c[i-1] - a[i-1] if c[i-1] - a[i-1] > 0 else 0
  a[i-1], a[i] = new_a_1, new_a

  count += tmp - c[i-1]
print(count)

Submission status: Wrong Answer

Solution code:
S = int(input())
alist = input().split()
blist = input().split()

for i,a in enumerate(alist):
  alist[i] = int(a)
for i,a in enumerate(blist):
  blist[i] = int(a)

alist.sort()
blist.sort()

tlist = []

for i,a in enumerate(blist):
  for b,d in enumerate(alist):
    if d <= a:
      tlist.append(alist.pop(b))

print(sum(tlist))

Submission status: Wrong Answer

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


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


if a[0] <= b[0]:
  a[0] = 0
else:
  a[0] = a[0] - b[0]
    


print(s - sum(a))

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

ans = sum(a)
tmp = 0
lst=[]

for i in range(len(a)):
    lst.append(a[i]-b[i])

ans_lst=[]
for i in range(len(lst)-1):
  ans_lst.append(lst[i] + lst[i+1])
  
tmp = abs(sum(ans_lst))
if tmp < ans:
  ans = ans - tmp

print(ans)

Submission status: Wrong Answer

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

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

print(count)

Submission status: Wrong Answer

Solution code:
import numpy as np
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a = np.array(a)
b = np.array(b)

ans = 0

ans_d = 1
while ans_d >0:
  for i in range(n-1):
      ans_d = 0
      a_sum = a[i]+a[i+1]+a[i+2]
      if  a_sum <= b[i]+b[i+1]:
          ans_d = a_sum
          ans += ans_d
          b[i+1]-=a[i+2]
          b[i]-=a[i]
          b[i+1]-= abs(b[i]- a[i+1])
          b[i] -= a[i + 1]
          a[i]=0
          a[i+1]=0
          a[i+2]=0


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

Submission status: Wrong Answer

Solution code:
# ' 'dで区切ってprintする方法
# print(*a, sep=' ')

import math

n = int(input())

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

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

beaten_monster = 0
carried = 0

for i in range(n):
    tmp_a = a[i]
    tmp_b = b[i]
    if tmp_a - tmp_b - carried > 0:
        beaten = tmp_b + carried
        carried = 0
    else:
        beaten = tmp_a
        carried = tmp_b + carried - tmp_a
    beaten_monster += beaten

beaten_monster += min(carried, a[n])

print(beaten_monster)

Submission status: Wrong Answer

Solution code:
import numpy as np

N = int(input()) + 1

A = input().split()

B = input().split()

enemy = np.zeros(N)
saver = np.zeros(N)

beated = 0
over = 0

for i in range(N - 1):
    enemy[i] = int(A[N - i - 1])
    saver[i] = int(B[N - i - 2])

enemy[N - 1] = int(A[0])

for i in range(N):
    if(enemy[i] - over > saver[i]):
        beated = beated + saver[i] + over
        over = 0
    else:
        beated = beated + enemy[i]
        if(over - enemy[i]):
            over = saver[i]
        else:
            over = saver[i] + over - enemy[i]

print(int(beated))

Submission status: Wrong Answer

Solution code:
n = int(input())
n
a = list(map(int, input().split(' ')))#モンスター
b = list(map(int, input().split(' ')))#勇者
b.append(0)
beat = 0
help = 0
for i in range(n):

    beat += min(b[i]+help,a[i])
    mada = min((b[i]+help)-a[i], b[i])
    if mada > 0:
        help = mada
beat += min(a[-1]+help,b[-1])

print(beat)

Submission status: Wrong Answer

Solution code:
def getInt(): return int(input())

def getIntList(): return [int(x) for x in input().split()]

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

Submission status: Wrong Answer

Solution code:
N = int(input())
N = N+1
monsters = list(map(int, input().split()))
monsters.append(0)
heros = list(map(int, input().split()))
stack = 0
count = 0
point = 0
life = 0
for i in range(0, len(heros)):
    point = monsters[i] - (stack + heros[i])
    if point < 0:
        if abs(point) > monsters[i+1]:
            stack = monsters[i+1]
        else:
            stack = heros[i] - monsters[i]
        count += monsters[i]
    else:
        count += heros[i]
count += stack
print(count)

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

print(defeat)

Submission status: Wrong Answer

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

N = input()
N = int(N)

A = []
A = input().split()
A = [int(n) for n in A]
A.append(0)

B = []
B = input().split()
B = [int(n) for n in B]
B.append(0)
B.append(0)

monster = 0

for i in range(N+1) :
	if A[i] < B[i] :
		monster = monster + A[i]
		delta = B[i] - A[i]
		B[i+1] = B[i+1] + delta
	if A[i] >= B[i] :
		monster = monster + B[i]

print(monster)

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

print(ans)

Submission status: Wrong Answer

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

if n == 1 or n == 2:
  print(min(sum(a),sum(b)))
else:
  a_mid = sum(a[1:n])
  b_mid = sum(b[1:n-1])

  mid_nokori = a_mid - b_mid

  if mid_nokori  > 0 :
    nokori0 = b[0] - a[0]
    nokorin = b[n-1] - a[n]
    if nokori0 <= 0 and nokorin <= 0:
      print(b_mid + b[0] + b[n-1])
    elif nokori0 <= 0 and nokorin > 0:
      print(b_mid + b[0] + a[n] + 
            min(mid_nokori,nokorin))
    elif nokori0 > 0 and nokorin <= 0:
      print(b_mid + a[0] + b[n-1] +
           min(mid_nokori,nokori0))
    elif nokori0 > 0 and nokorin > 0:
      print(b_mid + a[0] + a[n] +
           min(mid_nokori,nokori0 + nokorin))
  else:
    print(a_mid + min(a[0],b[0]) + min(a[n],b[n-1]))

Submission status: Wrong Answer

Solution code:
N = int(input())
N = N+1
monsters = list(map(int, input().split()))
monsters.append(0)
heros = list(map(int, input().split()))
stack = 0
count = 0
point = 0
life = 0
for i in range(0, len(heros)):
    point = monsters[i] - (stack + heros[i])
    if point < 0:
        if abs(point) > monsters[i+1]:
            stack = monsters[i+1]
        else:
            stack = heros[i]
        count += monsters[i]
    else:
        count += heros[i]
count += stack
print(count)

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

count = 0
next_b = 0
for i, ai in enumerate(a):
  if i < n:
    bi = b[i]
    ai = ai - next_b
    count += next_b
    next_b = 0
    if ai < bi:
      count += ai
      next_b = bi -ai
    else:
      count += bi
      next_b = 0
  else:
    if ai > next_b:
      count += next_b
    else:
      count += ai
      
print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int, input().split())) #i_1 i_2を取得し、iに値を入れる
B = list(map(int, input().split())) #i_1 i_2を取得し、iに値を入れる
B.append(0)
pure_arr = []
for j in range(n+1):
    pure_arr.append(j)
count=0
for i in pure_arr:
    if A[i]>=B[i]:
        count += B[i]
    elif A[i]<B[i]:
        if i<n:
            B[i+1] = B[i+1] + B[i] - A[i]
        count += A[i]

print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
teki = input().split()
yusya = input().split()
i = 0
taoshita = 0

while i < N :
  if int(teki[i]) >= int(yusya[i]):
    a = int(yusya[i]) 
    b = 0
  else:
    a = int(teki[i])
    yusya[i] = int(yusya[i]) - int(teki[i])
    if int(teki[i+1]) >= yusya[i]:
      b = yusya[i]
      teki[i+1] = int(teki[i+1]) - yusya[i]
    else:
      b = int(teki[i+1])
  taoshita += a + b
  i += 1

print(taoshita)

Submission status: Wrong Answer

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

ans = 0
remain_a = 0
remain_b = 0
for (a, b) in zip(A[::-1], B[::-1]):
    if remain_b != 0:
        if a >= remain_b:
            ans += remain_b
            a -= remain_b
            remain_b = 0
        else:
            ans += a
            remain_b -= a
            a = 0
    elif remain_a != 0:
        if remain_a >= b:
            ans += b
            remain_a -= b
            b = 0
        else:
            ans += remain_a
            b -= remain_a
            remain_a = 0
    
    a += remain_a
    b += remain_b
    if a >= b:
        ans += b
        remain_a = a - b
        remain_b = 0
    else:
        ans += a
        remain_a = 0
        remain_b = b - a

remain_a += A[0]
if remain_a >= remain_b:
    ans += remain_b
else:
    ans += remain_a
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()]
ans=0
mon1=a[0]
mon2=a[0]+a[1]
for i in range(n):
   if b[i] <= mon1:
      ans+=b[i]
      mon1=a[i+1]
      if i<=n-2:
         mon2=a[i+1]+a[i+2]
   elif mon1<b[i]<=mon2:
      ans+=b[i]
      mon1=a[i+1]-(b[i]-a[i])
      if i<=n-2:
         mon2=mon1+a[i+2]
   else:
      ans+=a[i]+a[i+1]
      if i<=n-2:
         mon2=a[i+2]
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] < a[i]:
        ans += b[i]
    else:
        ans += a[i]
        c = b[i] - a[i]
        if c > a[i+1]:
            ans += a[i+1]
            a[i+1] = 0
        else:
            a[i+1] - c
            ans += c

print(ans)

Submission status: Wrong Answer

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


N = input()
N = int(N)

A = []
A = input().split()
A = [int(n) for n in A]
A.append(0)

B = []
B = input().split()
B = [int(n) for n in B]
B.append(0)
B.append(0)

#print('N=',N)
#print('A=',A)
#print('B=',B)

monster = 0

for i in range(N+1) :
	if A[i] < B[i] :
		monster = monster + A[i]
		delta = B[i] - A[i]
		B[i+1] = B[i+1] + delta
	if A[i] >= B[i] :
		monster = monster + B[i]

print(monster)

Submission status: Wrong Answer

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

ans = 0
remain_a = 0
remain_b = 0
for (a, b) in zip(A[::-1], B[::-1]):
    if remain_b != 0:
        if a >= remain_b:
            ans += remain_b
            a -= remain_b
            remain_b = 0
        else:
            ans += a
            remain_b -= a
            a = 0
    elif remain_a != 0:
        if remain_a >= b:
            ans += b
            remain_a -= b
            b = 0
        else:
            ans += remain_a
            b -= remain_a
            remain_a = 0

    if a >= b:
        ans += b
        remain_a = a - b
        remain_b = 0
    else:
        ans += a
        remain_a = 0
        remain_b = b - a

remain_a += A[0]
if remain_a >= remain_b:
    ans += remain_b
else:
    ans += remain_a
print(ans)

Submission status: Wrong Answer

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

monster = 0
addPower = 0

for i in range(N):
    #print(A[i],'+', A[i+1], ' vs ', B[i], end = ' => ')
    AA = A[i] + A[i+1]
    BA = B[i] - A[i]
    BAA= B[i] - AA
    if BAA >= 0:
        #print(':', end='')
        monster += AA
        A[i+1] -= min(A[i+1], BA)
    elif BA > 0:
        #print('%', end='')
        monster += B[i]
        A[i+1] -= min(A[i+1], B[i])
    else:
        #print('@', end='')
        monster += B[i]

print(monster)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

N = int(input())
A = [int(i) for i in input().split()]
B = [int(j) for j in input().split()]
B.append(0)

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

if B[-1] >= A[-1]:
    print(monster + A[-1])
else:
    print(monster + B[-1])

Submission status: Wrong Answer

Solution code:
N=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
b.append(0)
sum1=0
rest=0
for i in range(N+1):
  sum1+=min(a[i],b[i]+rest)
  a[i]-=min(a[i],b[i]+rest)
  b[i]=min(b[i],b[i]+rest-min(a[i],b[i]+rest))
  if b[i]<a[i]:
    continue
  elif b[i]>a[i]:
    rest=b[i]-a[i]

print(sum1)

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

total = []
for i in range(len(B)):
    j = i + 1
    Ai = A[i]; Aj = A[j]
    Bi = B[i]

    m = Ai + Aj
    t = Bi

    if m > t:
        total.append(Bi)
    else:
        total.append(Ai + Aj)
if sum(A) >= sum(total):
    print(sum(total))
else:
    print(sum(A))

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

Submission status: Wrong Answer

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

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

print(monster)

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):
        brv = b[i] + tmp
        if brv <= a[i]:
            ans += brv
            tmp = 0
        else:
            ans += a[i]
            tmp = brv - a[i]

    if tmp <= a[n]:
        ans += tmp
    else:
        ans += a[n]
        
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

N = int(input())
A = [int(i) for i in input().split()]
B = [int(j) for j in input().split()]
B.append(0)

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

if B[-1] > A[-1]:
    print(monster + A[-1])
else:
    print(monster + B[-1])

Submission status: Wrong Answer

Solution code:
n = int(input())
monsters = list(map(int, input().split()))
heros = list(map(int, input().split()))

killed = 0
attack = 0
attack_l = [heros[0], 0]
for i in range(0, n+1):
  if i != n and i != 0:
    attack_l[1] = heros[i]
  attack = attack_l[0] + attack_l[1]
  if (monsters[i] - attack) < 0:
    killed += monsters[i]
    if (attack_l[0] - monsters[i]) < 0:
      attack_l[1] = attack_l[1] - (monsters[i] - attack_l[0])
    attack_l[0] = attack_l[1]
    attack_l[1] = 0
  elif (monsters[i] - attack) > 0:
    killed += attack
    attack_l[0] = 0
    attack_l[1] = 0
  elif (monsters[i] - attack) == 0:
    killed += monsters[i]
    attack_l[0] = 0
    attack_l[1] = 0

print(killed)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

if n == 1:
  print(min(sum(a),sum(b)))
else:
  a_mid = sum(a[1:n])
  if n > 2:
    b_mid = sum(b[1:n-1])
  else:
    b_mid = 0

  mid_nokori = a_mid - b_mid

  if mid_nokori  > 0 :
    nokori0 = b[0] - a[0]
    nokorin = b[n-1] - a[n]
    if nokori0 <= 0 and nokorin <= 0:
      print(b_mid + b[0] + b[n-1])
    elif nokori0 <= 0 and nokorin > 0:
      print(b_mid + b[0] + a[n] + 
            min(mid_nokori,nokorin))
    elif nokori0 > 0 and nokorin <= 0:
      print(b_mid + a[0] + b[n-1] +
           min(mid_nokori,nokori0))
    elif nokori0 > 0 and nokorin > 0:
      print(b_mid + a[0] + a[n] +
           min(mid_nokori,nokori0 + nokorin))
  else:
    print(a_mid + min(a[0],b[0]) + min(a[n],b[n-1]))

Submission status: Wrong Answer

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

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

print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
N = N+1
monsters = list(map(int, input().split()))
monsters.append(0)
heros = list(map(int, input().split()))
stack = 0
count = 0
point = 0
life = 0
for i in range(0, len(heros)):
    life -= 1
    if life == 0:
        stack = 0
    point = monsters[i] - (stack + heros[i])
    if point < 0:
        if abs(point) > monsters[i+1]:
            stack = monsters[i+1]
        else:
            stack = abs(point)
        count += monsters[i]
        life = 2
    else:
        count += heros[i]
count += stack
print(count)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

N = int(input())

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


B = np.append(B,0)
B = np.append(B,0)

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

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

total = []
for i in range(len(B)):
    j = i + 1
    Ai = A[i]; Aj = A[j]
    Bi = B[i]

    m = Ai + Aj
    t = Bi

    if m > t:
        total.append(Bi)
    else:
        total.append(Ai + Aj)
if sum(A) > sum(total):
    print(sum(total))
else:
    print(sum(A))

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

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, v in enumerate(b):
    if i != len(b) - 1:
        if v - a[i] >= 0:
            b[i + 1] += v - a[i] - b[i - 1]

            ans += a[i] 
        else:
            ans += v
    else:
        if v - (a[i] + a[i + 1]) >= 0:
            ans += (a[i] + a[i + 1])
        else:
            ans += v
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a = np.array(a)
b = np.array(b)

ans = 0
def cal(ans): 
  ans_d = 1
  while ans_d > 0:
      ans_d = 0
      for i in range(n):
          if a[i]+a[i+1] <= b[i]:
              ans_d += a[i]
              ans_d += a[i+1]
              ans += a[i]
              ans += a[i + 1]
              a[i] = 0
              a[i + 1] = 0
              b[i] = -1



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

cal(ans)

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

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
N = N+1
monsters = list(map(int, input().split()))
monsters.append(0)
heros = list(map(int, input().split()))
stack = 0
count = 0
point = 0
for i in range(0, len(heros)):
    point = monsters[i] - (stack + heros[i])
    if point < 0:
        if abs(point) > monsters[i+1]:
            stack = monsters[i+1]
        else:
            stack = abs(point)
        count += monsters[i]
    else:
        count += heros[i]
count += stack
print(count)