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())
monsterList = list(map(int, input().split()))
heroList = list(map(int, input().split()))
heroList.append(0)
count = 0
hero_cache = 0
for (monster, hero) in zip(monsterList, heroList):
    if(hero + hero_cache >= monster):
        hero_cache += hero - monster
        count += monster
    else:
        count += hero + hero_cache
print (count)

Submission status: Wrong Answer

Solution code:
# from math import ceil,sqrt,log,gcd,pi
# def ii():return int(input())
# def si():return input()
# def mi():return map(int,input().split())
# def li():return list(mi())

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

Submission status: Wrong Answer

Solution code:
def exception_handling(n, ali, bli):

    bli.append(0)
    count = 0
    ans = 0

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

    if ali[-1] < count:
        ans += ali[-1]
    else:
        ans += count

    return ans

def main():
    n = int(input())
    ali = list(map(int, input().split()))
    bli = list(map(int, input().split()))
    print(exception_handling(n, ali, bli))

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

cnt = 0

for i in range(N):
    mons = A[i+1] + A[i]
    if B[i] <= mons :         #倒しきれない
        zan = mons - B[i]
        A[i] = zan
        cnt +=B[i]
    elif B[i] > mons:         #倒しきれる
        A[i] = 0
        cnt += mons
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):
    mons = A[i+1] + A[i]
    if B[i] < mons :
        zan = mons - B[i]
        A[i] = zan
        cnt +=B[i]
    elif B[i] > mons:
        A[i] = 0
        cnt += mons
print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
lia = list(map(int,input().split()))
lib = list(map(int,input().split()))
mon = sum(lia)
for i in range(n):
    k = lia[i] - lib[i]
    if k <= 0:
        lia[i] = 0
        lia[i+1] += k
        if lia[i+1] < 0:
            lia[i+1] = 0
print(mon-sum(lia))

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()]
prev = a[0]
total = 0
for i, j in zip(a[1:], b):
    if prev >= j:
        total += j
        prev = i
    else:
        task = prev + i
        if task <= j:
            total += task
        else:
            total += j
            prev = task - j
print(total)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

taoshita = 0
for i in range(n):
  taoshita += min(a[i], b[i])
  yoryoku = max(a[i]-b[i], 0)
  taoshita += min(a[i+1], yoryoku)
  a[i+1] = max(a[i+1]-yoryoku, 0)

print(taoshita)

Submission status: Wrong Answer

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

taoshita = 0
for i in range(n):
  taoshita += min(a[i], b[i])
  yoryoku = max(b[i]-a[i], 0)
  taoshita += min(a[i+1], yoryoku)
  a[i+1] = max(yoryoku-a[i+1], 0)

print(taoshita)

Submission status: Wrong Answer

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

taoshita = 0
for i in range(n):
  taoshita += min(a[i], b[i])
  yoryoku = max(b[i]-a[i], 0)
  taoshita += min(a[i+1], yoryoku)
  nokori = max(yoryoku-a[i+1], 0)

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
for i in range(N):
    if A[i]+A[i+1] >= B[i]: ans+=B[i]
    else: ans+=A[i]+A[i+1]
    if B[i]-A[i] > 0:
        if A[i]+A[i+1] >= B[i]-A[i]: A[i+1]-=B[i]-A[i]
        else: 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()))
bp = [0] * (n + 1)
bp[0] = b[0]
for i in range(n + 1):
  if i == 0:
    if b[0] >= a[0]:
      mnum = a[0]
      bp[0] = b[0] - a[0]
    else:
      mnum = b[0]
      bp[0] = 0
  elif 0 < i < n:
    bp[i] = b[i] + bp[i - 1]
    if bp[i] < a[i]:
      mnum += a[i] - bp[i]
      bp[i] = 0
    else:
      if b[i] >= (bp[i] - a[i]):
        bp[i] = bp[i] - a[i]
      else:
        bp[i] = 0
      mnum += a[i]
  else:
    bp[i] = bp[i - 1]
    if bp[i] >= a[i]:
      mnum += a[i]
    else:
      mnum += a[i] - bp[i]
print(mnum)

Submission status: Wrong Answer

Solution code:
import sys
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def I(): return int(sys.stdin.readline().rstrip())
N = I()
A = LI()[::-1]
B = LI()[::-1]
ans = 0
for i in range(N):
    b = B[i]
    a = A[i]
    ans += a
    b -= a
    if b<=0:
        continue
    a2 = A[i+1]
    ans += min(a2,b)
    A[i+1] = a2-(min(a2,b))
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = []
A = [int(x) for x in input().split()]
moto = sum(A)
B = [int(x) for x in input().split()]
for i in reversed(range(N)):
  if A[i+1] >= B[i]:
    A[i+1] = A[i+1] -B[i]
  else:
    if A[i] >= B[i] -A[i+1]:
      A[i+1] = 0
      A[i] = A[i] + A[i+1] -B[i]
    else:
      A[i+1] = 0
      A[i] = 0
print(moto-sum(A))

Submission status: Wrong Answer

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


c = []
count = 0

for i in range(n):

    if a[i] - b[i] >= 0:
        count += b[i]
    elif a[i] - b[i] < 0:
        count += a[i]
        if a[i+1] + (a[i] - b[i]) >0:
            a[i+1] = a[i+1] + (a[i] - b[i])
            count += (a[i]-b[i])
        elif a[i+1] + (a[i]-b[i]) <=0:
            count += a[i+1]
            a[i+1] = 0
            
print(count)

Submission status: Wrong Answer

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


# In[]
c = []
count = 0

for i in range(n):

    if a[i] - b[i] >= 0:
        count += b[i]
    elif a[i] - b[i] < 0:
        count += a[i]
        if a[i+1] + (a[i] - b[i]) >0:
            a[i+1] = a[i+1] + (a[i] - b[i])
            count += (a[i]-b[i])
        elif a[i+1] + (a[i]-b[i]) <=0:
            count += a[i+1]
            a[i+1] = 0
            
print(count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
from io import StringIO
import unittest

def resolve():
    n = int(input())
    aa = list(map(int, input().split()))
    bb = list(map(int, input().split()))

    ans = 0
    for i, b in enumerate(bb):
        a = aa[i]
        next_a = aa[i+1]

        if a < b:
            ans += a
            remain = b - a

            if remain <= next_a:
                ans += remain
                next_a -= remain
            else:
                ans += next_a
                next_a = 0
        else:
            ans += b

    print(ans)


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """2
3 5 2
4 5"""
        output = """9"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """3
5 6 3 8
5 100 8"""
        output = """22"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """2
100 1 1
1 100"""
        output = """3"""
        self.assertIO(input, output)

if __name__ == "__main__":
    # unittest.main()
    resolve()

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(len(b)):
    kill = min(a[i], b[i])
    ans += kill
    kill = min(a[i+1],b[i]-min(a[i],b[i]))
    ans += kill
    a[i] -= min(a[i],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(len(b)):
    kill = min(a[i], b[i])
    ans += kill

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

    a[i] -= kill

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(len(b)):
    kill = min(a[i], b[i])
    ans += kill
    b[i] -= kill
    kill = min(a[i+1],b[i])
    ans += kill
    a[i] -= kill

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
la=list(map(int,input().split()))
lb=list(map(int,input().split()))
d=0
for i in range(len(lb)):
    k=la[i]-lb[i]
    if k==0:
        d+=la[i]
        la[i]=0
    elif k<0:
        d+=la[i]
        if la[i+1]>-k:
            d-=k
        else:
            d+=la[i+1]
        # la[i+1]+=k
    else:
        d+=lb[i]
        la[i]=k
print(d)

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

print(ans)

Submission status: Wrong Answer

Solution code:
#A B C 135
N = int(input())
A= [ int(a) for a in (input().split())]
B= [ int(a) for a in (input().split())]
mns = A[0]
cnt=0
for i in range(N):
    cnt+=min(B[i],A[i]+A[i+1])
    
print(min(cnt,sum(B)))

Submission status: Wrong Answer

Solution code:
#A B C 135
N = int(input())
A= [ int(a) for a in (input().split())]
B= [ int(a) for a in (input().split())]
mns = A[0]
cnt=0
for i in range(N):
    cnt+=min(B[i],A[i]+A[i+1])
print(cnt)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
import math
def main():
   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 += B[i]
       else:
           CNT += A[i]
           B[i] -= A[i]
           if A[i+1] >= B[i]:
               CNT += B[i]
           else:
               CNT += A[i+1]
   print(CNT)

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

  elif 1 <= i <= N-1:
    if B[i] <= A[i]:
      B[i] = 0
      A[i] -= B[i]
    else:
      B[i] -= A[i]
      A[i] = 0
    if B[i-1] <= A[i]:
      B[i-1] = 0
      A[i] -= B[i]
    else:
      B[i-1] -= A[i]
      A[i] = 0

  else:
    if B[i] <= A[i]:
      B[i] = 0
      A[i] -= B[i]
    else:
      B[i] -= A[i]
      A[i] = 0

print(ans-sum(B))

Submission status: Wrong Answer

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

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

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

        a[i+1] = max(a[i+1] - (b[i] - a[i]),0)
        a[i] = 0

        #print(a[i] + s)

    else:
        a[i] -= b[i]
        ans += b[i]
        print(b[i])
#print(a)
print(ans)

Submission status: Wrong Answer

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

monsall = 0
monsnext = a[0]

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

print(monsall)

Submission status: Wrong Answer

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

monsall = 0
monsnext = a[0]

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

print(monsall)

Submission status: Wrong Answer

Solution code:
def solve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    maximum = sum(A)
    for i in reversed(range(N)):
        reduce2 = max(0, A[i + 1] - B[i])
        reduce1 = max(0, A[i] - B[i] + reduce2)
        A[i + 1] = reduce2
        A[i] = reduce1
    now = sum(A)

    print(maximum - now)


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int, input().split()))[::-1]
B = list(map(int, input().split()))[::-1]
ans = 0

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

Submission status: Wrong Answer

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


print(ans)

Submission status: Wrong Answer

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

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

print(ans-sum(A))

Submission status: Wrong Answer

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

def solve(N,A,B):
    ans = 0
    for i in range(N):
        battle = min(A[i],B[i])
        B[i] -= battle
        ans += battle
        nex = min(A[i+1],B[i])
        A[i] -= nex
        ans += nex
    return ans
print(solve(N,A,B))

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

        a[i]=0

print(s-sum(a))

Submission status: Wrong Answer

Solution code:
N=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
ans=0
r=0
for i in range(N):
  if q[i]+r>p[i]:
    ans+=p[i]
    r=min(q[i]+r-p[i],q[i])
  else:
    ans+=q[i]+r
    leftover=0
ans+=min(p[N],r)
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
res=0

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
cnt = 0
cnt2 = 0
tmp = 0
tmp2 = 0

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

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

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()
cnt = 0
tmp = 0
tmp2 = 0

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

print(cnt)

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()
cnt = 0
tmp = 0

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

print(cnt)

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

Submission status: Wrong Answer

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

ans = sum(A)

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

print(ans - sum(A))

Submission status: Wrong Answer

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

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

if tmp > a[-1]:
    res += a[-1]
else:
    res += tmp

print(res)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

total_killed = 0
nokori = 0
for i in range(N):
    if B_list[i] >= A_list[i]:
        total_killed += A_list[i]
        nokori = B_list[i] - A_list[i]
        if nokori >= A_list[i+1]:
            A_list[i + 1] -= 0
            total_killed += A_list[i+1]
            nokori = 0
        else:
            A_list[i + 1] -= nokori
            total_killed += nokori
            nokori = 0
    else:
        nokori = 0
        total_killed += B_list[i]


print(total_killed)

Submission status: Wrong Answer

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

total_killed = 0
nokori = 0
for i in range(N):
    if B_list[i] >= A_list[i]:
        total_killed += A_list[i]
        nokori = B_list[i] - A_list[i]
        if nokori >= A_list[i+1]:
            A_list[i + 1] -= 0
            total_killed += A_list[i+1]
        else:
            A_list[i + 1] -= nokori
            total_killed += nokori
    else:
        nokori = 0
        total_killed += B_list[i]
        

print(total_killed)

Submission status: Wrong Answer

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

total_killed = 0
for i in range(N):
    if B_list[i] >= A_list[i]:
        total_killed += A_list[i]
        nokori = B_list[i] - A_list[i]
        if nokori >= A_list[i+1]:
            A_list[i + 1] -= 0
            total_killed += A_list[i+1]
        else:
            A_list[i + 1] -= nokori
            total_killed += nokori
    else:
        total_killed += B_list[i]

print(total_killed)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
 
ans = 0
for itr, b in enumerate(B):
  rem = b
  for i in range(2):
    dam = min(rem, A[itr + i])
    ans += dam
    rem -= dam
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    B = [int(i) for i in input().split()]
    ans = 0
    for i in range(N):
        if A[i] >= B[i]:
            ans += B[i]
            A[i] -= B[i]
            B[i] = 0
        else:
            ans += A[i]
            B[i] -= A[i]
            if A[i+1] >= B[i]:
                ans += B[i]
                A[i+1] -= B[i]
                B[i] = 0
            else:
                ans += A[i+1]
                B[i] -= A[i+1]
    print(ans)


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

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

Submission status: Wrong Answer

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

A.reverse()
B.reverse()

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

A.reverse()
B.reverse()

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

Submission status: Wrong Answer

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

killed_enemies = 0
killed_num = 0
for i in range(N):
    if killed_num + B_list[i] >= A_list[i]:
        killed_num = (killed_num + B_list[i]) - A_list[i]
        killed_enemies += A_list[i]

    else:
        killed_num = killed_num + B_list[i]
        killed_enemies += killed_num

if B_list[N-1] >= A_list[N]:
    killed_enemies += A_list[N]
else:
    killed_enemies += B_list[N-1]

print(killed_enemies)

Submission status: Wrong Answer

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

A_list.reverse()
B_list.reverse()

killed_enemies = 0
killed_num = 0
for i in range(N+1):
    if i < N:
        if killed_num + B_list[i] >= A_list[i]:
            killed_num = (killed_num + B_list[i]) - A_list[i]
            killed_enemies += A_list[i]

        else:
            killed_num = killed_num + B_list[i]
            killed_enemies += killed_num
    else:
        if B_list[i-1] >= A_list[i]:
            killed_enemies += A_list[i]
        else:
            killed_enemies += B_list[i-1]

print(killed_enemies)

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(0,N):
  y=B[i]-A[i]
  if y>=0:
    count+=A[i]
    z=y-A[i+1]
    if z>=0:
      count+=A[i+1]
      A[i+1]=0
    else:
      count+=y
      A[i+1]+=y
  else:
    p=A[i]-B[i]
    count+=B[i]
print(count)