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:
from sys import stdin


def main():
    N = int(stdin.readline().rstrip())
    As = [int(x) for x in stdin.readline().rstrip().split()]
    Bs = [int(x) for x in stdin.readline().rstrip().split()]
    result = 0

    if As[0] == 0 or Bs[0] == 0:
        pass
    elif Bs[0] < As[0]:
        result += Bs[0]
        Bs[0] = 0
    else:
        result += As[0]
        Bs[0] -= As[0]

    if As[N] == 0 or Bs[N - 1] == 0:
        pass
    elif Bs[N - 1] < As[N]:
        result += Bs[N - 1]
        Bs[N - 1] = 0
    else:
        result += As[N]
        Bs[N - 1] -= As[N]

    for i in range(1, N):
        a = As[i]
        b = Bs[i - 1]
        if a == 0 or b == 0:
            pass
        elif b < a:
            result += b
            Bs[i - 1] = 0
        else:
            result += a
            Bs[i - 1] -= a
        a = As[i]
        b = Bs[i]
        if a == 0 or b == 0:
            pass
        elif b < a:
            result += b
            Bs[i] = 0
        else:
            result += a
            Bs[i] -= a

    print(result)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())

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

sum_r = 0
pre_h_beat = 0
for i, b in enumerate(B):
    if i == 0:
        remain_mon = b

    left_beat = min(remain_mon, b)
    sum_r += left_beat

    cap_remain_beat = b - left_beat

    right_beat = min(cap_remain_beat, A[i + 1])

    sum_r += right_beat

    remain_mon = A[i + 1] - right_beat


print(sum_r)

Submission status: Wrong Answer

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

A_0 = A_list.pop(0)
#print(A_0)
#print(A_list)
A_list[0] = A_list[0] + A_0
#print(A_list)

taos = 0
nokori = 0
keep = 0

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

Submission status: Wrong Answer

Solution code:
N = int(input())


S = input()
S = S.split()
A = list(map(int, S))
A_copy = list(map(int, S))

S = input()
S = S.split()
B = list(map(int, S))


remain = 0

for i in range(len(B)):
    remain = int(A[i]-B[i])
    if remain <= 0:
        if A[i+1]+remain >= 0:
            A[i+1] = int(A[i+1] + remain)
        else:
            A[i+1] = 0
        A[i] = 0
    elif remain > 0:
        A[i] = remain



if A[N] == 0:
    print(sum(A_copy)-sum(A))

Submission status: Wrong Answer

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

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 

ta = A[-1]

for a, b in list(zip(A[:-1], B))[::-1]:
    print(ans, a,b,ta)
    tb = b
    if ta >= b:
        ans += b
        ta = a
        continue
    else:
        ans += ta
        tb -= ta
        ta = 0
        

    if a >= tb:
        ans += tb
        ta = a - tb
    else:
        ans += a
        ta = 0

print(ans)

Submission status: Wrong Answer

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

b = len(mon)

for i in range(b):
    if i == a :
        if  mon[i] >= att[i-1]:
            kil = kil + att[i-1]
            continue    
        elif mon[i] < att[i-1] :
            kil = kil + mon[i]
        
    elif mon[i] >= att[i]:
        kil = kil + att[i]
        continue    
    elif mon[i] < att[i] :
        kil = kil + mon[i]
        if i + 1 < a:
            att[i+1] = att[i+1] + att[i] - mon[i]
        elif i + 1 == a :
             att[i] = att[i] - mon[i]
print(kil)

Submission status: Wrong Answer

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

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()]
su = sum(a)
a_len = len(a)
count = 0
i = 0
a_idx = 0
b_idx = 0
a_num = a[a_idx]
b_num = b[b_idx]
while(b_idx < n and a_idx <= a_len and count < su):
    
    a_num = a_num - 1
    
    if i == b_num:
        b_num = b[b_idx]
        b_idx += 1
        i = 0
    elif a_num == 0:
        a_num = a[a_idx]
        a_idx += 1
    count += 1
    i += 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 = 0

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

    if ans == 0:
        print(np.sum(b))
        break

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

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
monster = list(map(int, input().split()))
yuusya = list(map(int, input().split()))
sub = 0
for i in range(len(monster)-1,0,-1):
  if yuusya[i-1] > monster[i]:
    sub += monster[i]
    yuusya[i-1] -= monster[i]
  else:
    sub += yuusya[i-1]
    yuusya[i-1] = 0
  if yuusya[i-1] > monster[i-1]:
    sub += monster[i-1]
    yuusya[i-1] -= monster[i-1]
  else:
    sub += yuusya[i-1]
    yuusya[i-1] = 0
if yuusya[0] > monster[0]:
  sub+=monster[0]
else:
  sub+=yuusya[0]
print(sub)

Submission status: Wrong Answer

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

attack = 0

for i in range(N):
  if i != (N-1):
    if a[i] > B[i]:
      a[i] = A[i]-B[i]
      b[i] = 0
      attack += B[i]
    else:
      a[i] = 0
      b[i] = B[i]-A[i]
      attack += A[i]
      if (B[i]-A[i]) > 0:
        a[i+1] = max(A[i+1]-b[i], 0)
        if b[i] > A[i+1]:
          attack += A[i+1]
        else:
          attack += b[i]
        b[i] = 0
  else:
    if a[N-1] > B[N-1]:
      a[N-1] = A[N-1]-B[N-1]
      b[N-1] = 0
      attack += B[i]
    else:
      a[N-1] = 0
      b[N-1] = B[N-1]-A[N-1]
      attack += A[N-1]
      if (B[N-1]-A[N-1]) > 0:
        a[N] = max(A[N]-b[N-1], 0)
        if b[N-1] > A[N]:
          attack += A[N]
        else:
          attack += b[N-1]
        b[N-1] = 0

print(attack)

Submission status: Wrong Answer

Solution code:
N = int(input())
An = list(map(int,input().split()))
Bn = list(map(int,input().split()))
secoundnums = [An[a]+An[a+1] for a in range(len(An)-1)]
changes = 0
for b in range(len(Bn)):
    change = secoundnums[b] if Bn[b]-secoundnums[b]>=0 else Bn[b]
    secoundnums[b]-=change
    changes+=change
print(changes)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split())) #モンスター数
B = list(map(int, input().split())) #倒せる数
result = 0
for i in range(0, N):
    if B[i] > A[i]:
        result += A[i]
        A[i] = 0
        B[i] -= A[i]
    else:
        result += B[i]
        A[i] -= B[i]
        B[i] = 0
for i in range(0, N):
    if B[i] != 0 or A[i+1] != 0:
        if A[i+1] <= B[i]:
            result += A[i+1]
        else:
            result += B[i]
print(result)

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]
            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:
N=int(input())
# L=[]
# for i in range(N):
#     L.append(int(read_input()))

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

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

Submission status: Wrong Answer

Solution code:

n=int(input())
a_L=list(map(int,input().split()))
b_L=list(map(int,input().split()))
ans=0

for i in range(n):
    ans+=min(a_L[i]+a_L[i+1],b_L[i])
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
al=[int(i) for i in input().split()]
bl=[int(i) for i in input().split()]
acc=0
carry=0
for i in range(n):
    ai=al[i]; bi=bl[i]
    if ai <= carry:
        acc+=ai
        carry=bi
    elif ai-carry <= bi:
        acc+=ai
        carry=bi-(ai-carry)
    else:
        acc+=bi
        carry=0
if not carry==0:
    if al[n] <= carry:
        acc+= al[n]
    else:
        acc+=carry
print(acc)

Submission status: Wrong Answer

Solution code:
N = int(input())
enemy = list(map(int, input().split()))
hero = list(map(int, input().split()))
count = 0
for i in range(N):
    if hero[i] <= enemy[i]:
        count += hero[i]
        continue
    else:
        count += enemy[i]
        hero[i] -= enemy[i]
        if enemy[i+1] >= hero[i]:
            count += hero[i]
            enemy[i+1] -= hero[i]
            continue
        else:
            count += enemy[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()))


ans=0
for _ in range(N-1):
  first = min(A[0],B[0])
  nokori =min([A[0]+A[1]-B[0], A[1],0])#+= min(A[1],max(B[0]-A[0],0))
  second = A[1]-nokori
  A[1] = nokori
  A = A[1:]
  B = B[1:]
  ans += first
  ans += second
ans += min(A[1],B[0])+min(A[0],max(B[0]-A[1],0))
print(ans)

Submission status: Wrong Answer

Solution code:
def LI(): return list(map(int, input().split()))

n = int(input())
a = LI() + [0]
b = LI() + [0, 0]

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

print(count)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=([int(n) for n in input().split()])
b=([int(n) for n in input().split()])
b.append(0)
r=0
t=0
x=0
for i in range(n+1):
  t=b[i]+x-a[i]
  if t>=0:
    r=r+a[i]
    x=max(b[i],t)
  else:
    r=r+b[i]+x
    x=0
print(r)

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()]
su = sum(a)
a_len = len(a)
count = 0
i = 0
a_idx = 0
b_idx = 0
a_num = a[a_idx]
b_num = b[b_idx]
while(count < su):
    
    a_num = a_num - 1
    
    if i == b_num:
        b_num = b[b_idx]
        b_idx += 1
        i = 0
        if b_idx == n:
            break
    if a_num == 0:
        a_num = a[a_idx]
        a_idx += 1
        if a_idx > a_len:
            break
    count += 1
    i += 1
    


print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
*a, = map(int,input().split())
*b, = map(int,input().split())
#print(a)
#print(b)
cnt = 0
for i in range(n):
  for j in range(i,i+1):
    x = b[j] - a[i]
    if x >= 0:
      b[j] = x
      cnt += a[i]
      y = b[j] - a[i+1]
      if y >= 0:
        cnt += a[i+1]
        a[i+1] = 0
      elif y < 0:
        cnt += b[j]
    elif x < 0:
      cnt += b[j]
      continue
print(cnt)

Submission status: Wrong Answer

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

A_0 = A_list.pop(0)
#print(A_0)
#print(A_list)
A_list[0] = A_list[0] + A_0
#print(A_list)

taos = 0
nokori = 0
keep = 0

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

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

    t = min(B[i], A[i])
    B[i] -= t
    ans += t

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

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

count = 0

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

print(count)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

attack = 0

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

print(attack)

Submission status: Wrong Answer

Solution code:
N = int(raw_input())
A = map(int, raw_input().split(' '))
B = map(int, raw_input().split(' '))

count = 0
zan_A_i = A[N]

for i in range(N):
    zan_B_i = B[N-1-i] - zan_A_i

    if zan_B_i < 0:
        count += zan_B_i
        zan_B_i = 0
    zan_A_i = A[N-1-i] - zan_B_i
    if zan_A_i < 0:
        count += zan_A_i
        zan_A_i = 0

print(sum(B)+count)

Submission status: Wrong Answer

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

for i in range(1, n + 1):
    hero = b[i]
    enemy = a[i - 1] + a[i]
    count.append(min(hero, enemy))
    a[i - 1] = enemy - count[i]

print(sum(count))

Submission status: Wrong Answer

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

ans = 0
a1 = 0
b1 = 0
a1 = a.pop(0)
b1 = b.pop(0)
while(len(b)>0):
    if a1 < b1:
        ans += a1
        b1 = b.pop(0)+b1-a1
        a1 = a.pop(0)
    else:
        ans += b1
        a1 = a.pop(0)
        b1 = b.pop(0)

if a1 < b1:
    ans += a1+min(b1-a1, a[0])
else:
    ans += b1
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]+a[i+1]:
                ans += a[i]+a[i+1]
        else:
                ans += b[i]
                if a[i] < b[i]:
                        a[i+1] -= b[i]-a[i]
if ans > sum(a):
        print(sum(a))
else:
        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()))

A_0 = A_list.pop(0)
#print(A_0)
#print(A_list)
A_list[0] = A_list[0] + A_0
#print(A_list)

taos = 0
nokori = 0
keep = 0

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

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

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    a_s = list(map(int, input().split(' ')))
    b_s = list(map(int, input().split(' ')))
    count = 0
    _a = a_s[n]
    for i, a in enumerate(reversed(a_s[:n])):
        s = _a + a
        b = b_s[n-(i+1)]
        count += min(s, b)
        # print(count)
        if b <= a:
            _a = a
        _a = max(0, s-_a)
    print(count)

if __name__ == '__main__':
    main()

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()]
    Bs = [int(x) for x in stdin.readline().rstrip().split()]
    result = 0

    if Bs[0] < As[0]:
        result += Bs[0]
        Bs[0] = 0
    else:
        result += As[0]
        Bs[0] -= As[0]

    if Bs[N - 1] < As[N]:
        result += Bs[N - 1]
        Bs[N - 1] = 0
    else:
        result += As[N]
        Bs[N - 1] -= As[N]

    for i in range(1, N):
        a = As[i]
        b = Bs[i - 1]
        if b < a:
            result += b
            Bs[i - 1] = 0
        else:
            result += a
            Bs[i - 1] -= a
        a = As[i]
        b = Bs[i]
        if b < a:
            result += b
            Bs[i] = 0
        else:
            result += a
            Bs[i] -= a

    print(result)


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

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

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    monsters = list(map(int,input().split()))
    heros = list(map(int,input().split()))
    neoMonsters = []
    for i in range(len(monsters)-1):
        neoMonsters.append(monsters[i] + monsters[i+1])

    cnt = 0
    for i in range(len(neoMonsters)):
        cnt += min(neoMonsters[i],heros[i])

    print(cnt)

main()

Submission status: Wrong Answer

Solution code:
from collections import deque
n=int(input())
a=deque([int(i) for i in input().split()])
k=sum(a)
b=deque([int(i) for i in input().split()])
b.appendleft(0)

for i in range(n):
    q=a[i]
    p=b[i]-q
    a[i]=max(0,q-(b[i]+b[i+1]))
    b[i]=0
    b[i+1]=abs(b[i+1]+p)
a[-1]=max(0,a[-1]-b[-1])

print(k-sum(a))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
count = 0
flag = 0
i = 0
while(i<len(a)-1):
	if b[i]<a[i]+a[i+1]:
		count = count+b[i]
		flag = 0
		i += 1

	else:
		if flag == 0:
			count = count+a[i]+a[i+1]
			flag = 1
			i = i + 1

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

b[n-1]-=a[n]
if b[n-1]>=0:
    sum+=a[n]
    a[n]=0
    b[n-1]-=a[n-1]
    if b[n-1]>=0:
        sum+=a[n-1]
        a[n-1]=0
    else:
        sum+=a[n-1]+b[n-1]
        a[n-1]+=a[n-1]+b[n-1]
else:
    sum+=a[n]+b[n-1]
    a[n]=a[n]+b[n-1]

print(sum)

Submission status: Wrong Answer

Solution code:
n = int(input())
*a, = map(int,input().split())
*b, = map(int,input().split())
#print(a)
#print(b)
cnt = 0
for i in range(n):
  for j in range(i,i+1):
    x = b[j] - a[i]
    if x > 0:
      b[i] = x
      cnt += a[i]
      y = b[i] - a[i+1]
      if y >= 0:
        cnt += a[i+1]
        a[i+1] = 0
      elif y < 0:
        cnt += b[i]
    elif x == 0:
      cnt += a[i]
      continue
    elif x < 0:
      cnt += b[i]
      continue
print(cnt)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
A=[0]*n
for i in range(n):
    A[i]=a[i]+a[i+1]
x=0
loop=0
while(1):
    loop+=1
    flag=0
    for i in range(n):
        if A[i]<=b[i] and A[i]>0:
            flag=1
            x+=A[i]
            b[i]-=A[i]
            A[i]=0
            if i<n-1:
                A[i+1]-=a[i+1]
            if i>0:
                A[i-1]-=a[i]
            a[i+1]=0
            a[i]=0
    if flag==0:
        break
for i in range(n):
    if b[i]<=A[i]:
        x+=b[i]

print(x)

Submission status: Wrong Answer

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

attack = 0

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

print(attack)

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())
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 = min(H[i], H[i]+rest-M[i])
    sum_m += M[i]
sum_m += min(rest, M[-1])
rest = 0
sum_n = 0
for i in range(N)[::-1]:
  if M[i+1] >= H[i]+rest:
    rest = 0
    sum_n += H[i]+rest
  else:
    rest = min(H[i], H[i]+rest-M[i+1])
    sum_n += M[i+1]
sum_n += min(rest, M[0])
print(max(sum_m, sum_n))

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    a_s = list(map(int, input().split(' ')))
    b_s = list(map(int, input().split(' ')))
    count = 0
    _a = a_s[n]
    for i, a in enumerate(reversed(a_s[:n])):
        s = _a + a
        b = b_s[n-(i+1)]
        count += min(s, b)
        # print(count)
        _a = max(0, s-_a)
    print(count)

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

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()]
su = sum(a)
a_len = len(a)
count = 0
i = 0
a_idx = 0
b_idx = 0
a_num = a[a_idx]
b_num = b[b_idx]
while(b_idx < n and a_idx <= a_len and count < su):
    
    a_num = a_num - 1
    
    if i == b_num:
        b_num = b[b_idx]
        b_idx += 1
        i = 0
    if a_num == 0:
        a_num = a[a_idx]
        a_idx += 1
    count += 1
    i += 1
    


print(count)

Submission status: Wrong Answer

Solution code:
num = int(input())

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

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

ans = 0

for i in reversed(range(num)):
  if town[i+1] >= fighter[i]:
    ans += fighter[i]
    continue
  else:
    ans += town[i+1]
    amari = fighter[i] - town[i+1]
    
  if town[i] >= amari:
    ans += amari
    town[i] -= amari
  else:
    ans += town[i]

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)

r = 0
s = 0

for a, b in zip(A, B):
    c = r + b

    r = max(0, c-a)
    s += min(a, c)
print(s)

Submission status: Wrong Answer

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

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):
  A=a[n-i]
  B=a[n-i-1]
  C=b[n-i-1]
  if A>=C:
    ans+=C
  elif A<C:
    ans+=A
    C=C-A
    if B>=C:
      ans+=C
      B=B-C
    else:
      ans+=B
      B=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()]
d=[0]*(n+2)
for i in range(n):
    d[i+1]=min(a[i]+a[i+1],b[i]+d[i])
print(sum(d))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A.reverse()
B.reverse()
ans = 0
t = A[0]
for a, b in zip(A[1:], B):
    t += a
    print(ans, t)
    m = min(t, b)
    ans += m
    t = t - m if t - a <= b else a

print(ans)

Submission status: Wrong Answer

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

b = B[0]
a = b - A[0]
if a > 0:
    ans += A[0]
    work = A[1] - a
    if work <= 0:
        ans += A[1]
        work = 0
    else:
        ans += a
else:
    ans += b
    work = float("INF")

for i in range(1, N):
    if work == 0:
        ans += min(B[i], A[i + 1])
        work = float("INF")
        continue
    b = B[i]
    a = b - min(A[i], work)
    if a > 0:
        ans += min(A[i], work)
        work = A[i + 1] - a
        if work <= 0:
            ans += A[i + 1]
            work = 0
        else:
            ans += a
    else:
        ans += b
        work = float("INF")

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()]
su = sum(a)
count = 0
i = 0
a_idx = 0
b_idx = 0
a_num = a[a_idx]
b_num = b[b_idx]
while(b_idx < n and a_idx <= len(a) and count < su):
    
    a_num = a_num - 1
    
    if i == b_num:
        b_num = b[b_idx]
        b_idx += 1
        i = 0
    if a_num == 0:
        a_num = a[a_idx]
        a_idx += 1
    count += 1
    i += 1
    


print(count)

Submission status: Wrong Answer

Solution code:
N= map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
T_ALL=0
T=0
R=0
for i in range(0,len(B),1):
    T=min(A[i],B[i]+R) #i番目の場所で倒した数
    R=B[i]+R-T #残り倒せる数
    T_ALL=T_ALL+T #i番目の場所までに倒した合計
T_ALL=T_ALL+min(A[len(A)-1],R)
print(T_ALL)

Submission status: Wrong Answer

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

count = 0

for num in range(N):
    if A[num] >= B[num]:
        num_dead_monster = B[num]
    else:
        num_dead_monster = A[num]
    #print("now index", num_dead_monster)
    count += num_dead_monster
    A[num] -= num_dead_monster
    B[num] -= num_dead_monster
    
    if B[num] >= 1:
        if A[num+1] >= B[num]:
            num_dead_monster = B[num]
        else:
            num_dead_monster = A[num+1]
        count += num_dead_monster
        A[num+1] -= num_dead_monster
        #print("next index", B[num])
    
    #print("A", A)
    #print("B", B)
    #print()

print("count", count)