Problem:


Score : 300 points

Problem Statement

There are N+1 towns. The i-th town is being attacked by A_i monsters.

We have N heroes. The i-th hero can defeat monsters attacking the i-th or (i+1)-th town, for a total of at most B_i monsters.

What is the maximum total number of monsters the heroes can cooperate to defeat?

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_{N+1}
B_1 B_2 ... B_N

Output

Print the maximum total number of monsters the heroes can defeat.

Sample Input 1

2
3 5 2
4 5

Sample Output 1

9

If the heroes choose the monsters to defeat as follows, they can defeat nine monsters in total, which is the maximum result.

The first hero defeats two monsters attacking the first town and two monsters attacking the second town.

The second hero defeats three monsters attacking the second town and two monsters attacking the third town.

Sample Input 2

3
5 6 3 8
5 100 8

Sample Output 2

22

Sample Input 3

2
100 1 1
1 100

Sample Output 3

3



Sample input:
2
3 5 2
4 5


Sample output:
9

Submission status: Wrong Answer

Solution code:
n = int(input())
a_lst = list(map(int,input().split()))
b_lst = list(map(int,input().split()))
monster = 0

for i in range(n):
    a1 = a_lst[i]
    a2 = a_lst[i+1]
    b1 = b_lst[i]
    tmp = b1-a1

    monster += min(a1,b1)
    if tmp>0:
        if a2-tmp<=0:
            monster += a2
            a_lst[i+1] = 0
        else:
            monster += tmp
            a_lst[i+1] -= tmp

print(monster)
print(a_lst)

Submission status: Wrong Answer

Solution code:
n = int(input())
a_lst = list(map(int,input().split()))
b_lst = list(map(int,input().split()))
monster = 0

for i in range(n):
    a1 = a_lst[i]
    a2 = a_lst[i+1]
    b1 = b_lst[i]
    tmp = b1-a1

    monster += min(a1,b1)
    if tmp>0:
        if a2-tmp<=0:
            monster += a2
            a2 = 0
        else:
            monster += tmp
            a2 -= tmp

print(monster)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
# coding: utf-8

def main():
    
    sevar = int(input())
    monstar = list(map(int, input().split()))
    attack = list(map(int, input().split()))
    
    m_get = list()
    
    for i in range(sevar):
        if monstar[i] < attack[i]:
            a = attack[i] - monstar[i]
            m_get.append(monstar[i])
            
            if a > monstar[i+1]:
                m_get.append(monstar[i+1])
                monstar[i] = 0
            
            else:
                m_get.append(a)
                monstar[i+1] = monstar[i+1] - a
                
        else:
            m_get.append(attack[i])
            
            
    print(sum(m_get))
    
    return 0


if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
# coding: utf-8

def main():
    
    sevar = int(input())
    monstar = list(map(int, input().split()))
    attack = list(map(int, input().split()))
    
    m_get = list()
    
    for i in range(sevar):
        if monstar[i] < attack[i]:
            a = attack[i] - monstar[i]
            m_get.append(monstar[i])
            
            if a > monstar[i+1]:
                n = monstar[i+1]
                m_get.append(n)
            
            else:
                n = a
                m_get.append(n)
                monstar[i+1] = monstar[i+1] - a
                
        else:
            m_get.append(attack[i])
            
            
    print(sum(m_get))
    
    return 0


if __name__ == '__main__':
  main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

def right(a,b,n):
  a.reverse()
  b.reverse()
  return left(a,b,n)

print(max(left(a,b,n),right(a,b,n)))

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()
B = LIST()

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

if A[0] > temp:
	dest += temp
else:
	dest += A[0] 

print(dest)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

total = 0
dif = 0
for i in range(n):
    if a[i] - b[i] <= 0:
        total += a[i]
        dif = b[i] - a[i]
        if dif < a[i+1]:
            total += dif
    else:
        total += b[i]
    if a[i+1] - dif <= 0:
        total += a[i+1]
    a[i+1] = max(0, a[i+1] - dif)
print(total)

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, x in enumerate(b):
    for j in range(i, i+2):
        if a[j] >= x:
            count += x
            a[j] -=  x
            break
        else:
            count = count + a[j]
            x -= a[j]
print(count)

Submission status: Wrong Answer

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

cnt = 0
amari_b = 0

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

print(cnt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    
    ans = 0
    for i in range(n):
        ans += min(a[i], b[i])
        # 勇者iがまだi+1番目のモンスターを倒せる場合
        if b[i] > a[i]:
            # 残りの倒せる体数かi+1番目のモンスター全部
            ans += min(b[i] - a[i], a[i+1])

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
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) :
    sA = A[i] + A[i+1]
    if sA >= B[i] :
        d = B[i] - A[i]
        if d >= 0:
            A[i] = 0
            A[i-1] -= d
        else :
            A[i] -= B[i]
        ans += B[i]
    else :
        A[i] = 0
        A[i+1] = 0
        ans += sA
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, A: "List[int]", B: "List[int]"):
    B.append(0)
    ans = 0
    tmp = 0

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

    print(ans)

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N + 1)]  # type: "List[int]"
    B = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A, B)

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
next_A = A[0]
for i in range(N):
    Ans += min(next_A+A[i+1], B[i])
    next_A = max(0, A[i+1] + min(A[i]-B[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()))
count = 0
for i in range(N):
  count += min(B[i], A[i] + A[i + 1])
print(count)

Submission status: Wrong Answer

Solution code:
# import itertools
# import math

import sys
input = sys.stdin.readline


def solve(N, A, B):
    defeat_monstar = 0
    for nn in range(N):
        if A[nn] >= B[nn]:
            defeat_monstar += B[nn]
        else:
            B[nn] -= A[nn]
            if A[nn + 1] < B[nn]:
                defeat_monstar += A[nn] + A[nn + 1]
            else:
                A[nn + 1] = A[nn + 1] - B[nn]
                defeat_monstar += A[nn] + B[nn]

    return defeat_monstar


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

# P = []
# S = []
# for i in range(N):
#     pp, ss = map(int, input().split())
#     P.append(pp)
#     S.append(ss)

print(solve(N, A, B))

Submission status: Wrong Answer

Solution code:
# import itertools
# import math

import sys
input = sys.stdin.readline


def solve(N, A, B):
    defeat_monstar = 0
    for nn in range(N):
        if A[nn] >= B[nn]:
            defeat_monstar += B[nn]
        elif A[nn + 1] < (B[nn] - A[nn]):
            defeat_monstar += (A[nn + 1] + A[nn])
            A[nn + 1] = 0
        else:
            defeat_monstar += B[nn]
            A[nn + 1] = B[nn] - A[nn]

    return defeat_monstar


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

# P = []
# S = []
# for i in range(N):
#     pp, ss = map(int, input().split())
#     P.append(pp)
#     S.append(ss)

print(solve(N, A, B))

Submission status: Wrong Answer

Solution code:
# import itertools
# import math

import sys
input = sys.stdin.readline


def solve(N, A, B):
    defeat_monstar = 0
    for nn in range(N):
        if A[nn] >= B[nn]:
            defeat_monstar += B[nn]
        elif (A[nn + 1] + A[nn]) < B[nn]:
            defeat_monstar += (A[nn + 1] + A[nn])
            A[nn + 1] = 0
        else:
            defeat_monstar += B[nn]
            A[nn + 1] = B[nn] - A[nn]

    return defeat_monstar


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

# P = []
# S = []
# for i in range(N):
#     pp, ss = map(int, input().split())
#     P.append(pp)
#     S.append(ss)

print(solve(N, A, B))

Submission status: Wrong Answer

Solution code:
# import itertools
# import math

import sys
input = sys.stdin.readline


def solve(N, A, B):
    defeat_monstar = 0
    for nn in range(N):
        monstar = A[nn] + A[nn + 1]
        if monstar <= B[nn]:
            defeat_monstar += monstar
            A[nn + 1] = 0
        else:
            defeat_monstar += B[nn]
            A[nn + 1] = min(A[nn + 1], monstar - defeat_monstar)
    return defeat_monstar


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

# P = []
# S = []
# for i in range(N):
#     pp, ss = map(int, input().split())
#     P.append(pp)
#     S.append(ss)

print(solve(N, A, B))

Submission status: Wrong Answer

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

def main():
    N = int(input())
    A = [int(c) for c in input().split()]
    B = [int(c) for c in input().split()] + [0]

    diffs = [(a-b, i) for i, (a, b) in enumerate(zip(A, B))]
    heapq.heapify(diffs)

    ans = sum(A)
    while diffs:
        diff, i = heapq.heappop(diffs)
        if diff != A[i] - B[i] or i >= N:
            continue

        A[i] = 0 if diff < 0 else diff
        B[i] = 0 if diff > 0 else -diff

        if diff < 0:
            A[i+1] = 0 if A[i+1] + diff < 0 else A[i+1] + diff
            heapq.heappush(diffs, (A[i+1] - B[i+1], i+1))

    print(ans - sum(A))
main()

Submission status: Wrong Answer

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

ans = 0
L = 0
for i in range(N):
    ans += min(B[i] + L, A[i])
    L = max(B[i] - (A[i] - L), 0)
if L > 0:
    ans += min(L, A[N])
print(ans)

Submission status: Wrong Answer

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

ans = 0
L = 0
for i in range(N):
    ans += min(B[i] + L, A[i])
    L = max(B[i] - A[i], 0)
if L > 0:
    ans += min(L, A[N])
print(ans)

Submission status: Wrong Answer

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

ans = 0
remain = 0
for i in range(N):
  # before
  A[i] -= remain
  ans += remain
  
  # now
  remain = max(B[i]-A[i], 0)
  ans += min(A[i], B[i])

if remain > 0:
  ans += min(A[N], remain)
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
count=int(0)
ans=int(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 A[i+1]<B[i]:
        ans+=A[i+1]
        B[i]-=A[i+1]
    else:
        ans+=B[i]
        B[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()))
b.append(0)
b.append(0)
z = 0
c = b[0]
for i in range(n+1):
    #print('i ',a[i])
    #print('i ',b[i],b[i+1])
    x = a[i] - c
    #print(z)
    if x <= 0:
        c = b[i+1] - x
        z += a[i]
    else:
        z += c
        c = b[i+1]
print(z)

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

  if A[index] - (B[index] + p) < 0:
    ans += A[index]
    p = abs(A[index] - B[index] - p)
    continue

  ans += B[index] + p
  p = 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
p = 0

for index, i in enumerate(A):
  if index > len(B) - 1:
    if p > A[index]:
      ans += A[index]
    else:
      ans += p
      
    break

  if A[index] - (B[index] + p) < 0:
    ans += A[index]
    p = abs(A[index] - B[index] - p)
    continue

  ans += B[index]
  p = 0

print(ans)

Submission status: Wrong Answer

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

ans = 0
p = 0

for index, i in enumerate(A):
  if index > len(B) - 1:
    if p > A[index]:
      ans += A[index]
    else:
      ans += p
      
    break

  if A[index] - (B[index] + p) < 0:
    ans += A[index]
    p = abs(A[index] - B[index] - p)
    continue

  ans += B[index]
  p = 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
p = 0

for index, i in enumerate(A):
  if index > len(B) - 1:
    if p > A[index]:
      ans += A[index]
    else:
      print(p)
      
    break

  if A[index] - (B[index] + p) < 0:
    ans += A[index]
    p = abs(A[index] - B[index] - p)
    continue

  ans += B[index]
  p = 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(len(B)):
  if A[i] - B[i] >= 0:
    A[i] = A[i] - B[i]
    ans += B[i]
  else:
    ans += A[i]
    A[i] = 0
    if A[i+1] - (B[i] - A[i]) >= 0:
      A[i+1] = A[i+1] - (B[i] - A[i])
      ans += (B[i] - A[i])
    else:
      ans += A[i+1]
      A[i+1] = 0
  #print(ans, A)
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):
    t = min(a[i], b[i])
    a[i] -= t
    b[i] -= t
    res += t
    d = min(a[i+1], b[i])
    a[i+1] -= t
    b[i] -= t
    res += t
print(res)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import _collections
import string
from functools import lru_cache

class cin():
    def int():
        return int(sys.stdin.readline().rstrip())
    def string():
        return sys.stdin.readline().rstrip()
    def mapInt():
        return [int(x) for x in cin.string().split()]
    def stringList(n):
        return [input() for i in range(n)]
    def intListList(n):
        return [cin.mapInt() for i in range(n)]
    def intColsList(n):
        return [int(input()) for i in range(n)]
        
class Math():
    def gcd(a,b):
        if b == 0:
            return a
        return Math.gcd(b,a % b)
    def lcm(a,b):
        return (a * b) // Math.gcd(a,b)
    def roundUp(a,b):
        return -(-a // b)
    def toUpperMultiple(a,x):
        return Math.roundUp(a,x) * x
    def toLowerMultiple(a,x):
        return (a // x) * x
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret

MOD = int(1e09) + 7

def main():
    N = cin.int()
    A = cin.mapInt()
    B = cin.mapInt()

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

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
Monster = list(map(int, input().split()))
Brave = list(map(int, input().split()))
cnt = 0
amari = 0

for i in range(N):
    cnt += min(Monster[i], Brave[i]+amari)
    
    if (Brave[i]+amari > Monster[i]):
        amari = Brave[i]+amari - Monster[i]
    else:
        amari = 0   
        
cnt += min(amari, Monster[N])
print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
Monster = list(map(int, input().split()))
Brave = list(map(int, input().split()))
cnt = 0
amari = 0

for i in range(N):
    cnt += min(Monster[i], Brave[i]+amari)
    
    if (Brave[i]+amari > Monster[i]):
        amari = Brave[i]+amari - Monster[i]    
        
cnt += min(amari, Monster[N])
print(cnt)

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

print(sum)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
b = sorted([int(i) for i in input().split()])
b.extend([0])
job = 0
count = 0
rest = 0
for i,j in zip(a,b):
    if rest == 0:
        if i >= j:
            count += j
        else:
            count += i
        rest = i-j
    elif rest <0:
        job = min(i, abs(rest))
        count += job
        rest = i - job - j
        count += min(i-job, j)
    else:
        job = min(rest, j)
        count += job
        rest = i+job-j
        count += min(i,j-job)
print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
monster = list(map(int, input().split()))
savers = list(map(int, input().split()))
first = sum(monster)
rest = []
for i in reversed(range(n)):
    monster_i = monster.pop()
    save_i = savers[i]
    # 倒したモンスター
    monster_ii = max(monster_i - save_i, 0)
    # 残り倒せるかず
    save_ii = save_i - monster_ii
    rest.append(monster_ii)
    monster[-1] = max(monster[-1] - save_ii, 0)
ans = first - sum(rest) - monster[0]
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()]

ans = 0
for i in range(n):
    if al[i] > bl[i]:
        ans += bl[i]
    elif al[i] + al[i+1] > bl[i]:
        ans += al[i] + al[i+1]
        al[i+1] = 0
    else:
        ans += bl[i]
        al[i+1] = al[i+1] + al[i] - bl[i]
        
print(ans)

Submission status: Wrong Answer

Solution code:
def LI():
  return [int(s) for s in input().split()]

N = int(input())

A_s = LI()
B_s = LI()

counter = 0

for i in range(N):
  if A_s[i] > B_s[i]:
    counter+=(A_s[i]-B_s[i])
  elif B_s[i] < A_s[i] + A_s[i+1]:
    counter+=B_s[i]
    A_s[i+1] = A_s[i+1] - (B_s[i]-A_s[i])
  else:
    counter+=A_s[i]+A_s[i+1]
    A_s[i+1] = 0
    
print(counter)

Submission status: Wrong Answer

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

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

ans += min(A[N],cnt)
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
cnt = 0

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

Submission status: Wrong Answer

Solution code:
n = int(input())
En = list(map(int, input().split()))
Pl = list(map(int, input().split()))
res = 0
for i in range(n):
    if En[i] < Pl[i]:
        res += En[i]
        Pl[i] -= En[i]
        if En[i+1] < Pl[i]:
            res += En[i+1]
        else:
            En[i+1] -= Pl[i]
            res += Pl[i]
    else:
        res += Pl[i]
print(res)

Submission status: Wrong Answer

Solution code:
n = int(input())
En = list(map(int, input().split()))
Pl = list(map(int, input().split()))
res = 0
for i in range(n):
    if En[i] < Pl[i]:
        res += En[i]
        Pl[i] -= En[i]
        if En[i+1] < Pl[i]:
            res += En[i+1]
        else:
            res += Pl[i]
    else:
        res += Pl[i]
print(res)

Submission status: Wrong Answer

Solution code:
N = int(input())
Alist = list(map(int, input().split()))
Blist = list(map(int, input().split()))
amari = 0
ans = 0
for i in range(N):
    can_kill = amari + Blist[i]
    enemy = Alist[i]
    if can_kill > enemy:
        ans += enemy
        amari = max(Blist[i] - enemy, 0)
    else:
        ans += can_kill
        amari = 0
if amari > Alist[-1]:
    ans += Alist[-1]
else:
    ans += amari
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
Alist = list(map(int, input().split()))
Blist = list(map(int, input().split()))
amari = 0
ans = 0
for i in range(N):
    can_kill = amari + Blist[i]
    enemy = Alist[i]
    if can_kill > enemy:
        ans += enemy
        amari = can_kill - enemy
    else:
        ans += max(can_kill, 0)
        amari = 0
if amari > Alist[-1]:
    ans += Alist[-1]
else:
    ans += amari
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
Alist = list(map(int, input().split()))
Blist = list(map(int, input().split()))
amari = 0
ans = 0
for i in range(N):
    can_kill = amari + Blist[i]
    enemy = Alist[i]
    if can_kill > enemy:
        ans += enemy
        amari = can_kill - enemy
    else:
        ans += can_kill
        amari = 0
if amari > Alist[-1]:
    ans += Alist[-1]
else:
    ans += amari
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
remain=A[0]
ans1=0
for i in range(n):
    if B[i]<=remain:
        ans1+=B[i]
        remain=A[i+1]
    if B[i]>remain:
        ans1+=remain
        yoryoku=B[i]-remain
        if yoryoku>A[i+1]:
            ans1+=A[i+1]
            remain=0
        else:
            ans1+=yoryoku
            remain=A[i+1]-yoryoku
rA=list(reversed(A))
rB=list(reversed(B))
remain=rA[0]
ans2=0
for i in range(n):
    if rB[i]<=remain:
        ans2+=rB[i]
        remain=rA[i+1]
    if rB[i]>remain:
        ans2+=remain
        yoryoku=rB[i]-remain
        if yoryoku>rA[i+1]:
            ans2+=rA[i+1]
            remain=0
        else:
            ans2+=yoryoku
            remain=rA[i+1]-yoryoku
print(max(ans1,ans2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
ai = list(map(int, input().split()))
bi = list(map(int, input().split()))

ans = 0
for i in range(n):
    ans += min(ai[i], bi[i])
    ans += min(ai[i+1], bi[i]-min(ai[i], bi[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):
    ans += min(A[i],B[i])+min(A[i+1],B[i]-min(A[i],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(0, N):
    a = min(A[i], B[i])
    ans += a
    ans += min(A[i + 1], B[i] - a)
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+=A[i]
        rem=B[i]-A[i]
        A[i]=0
        if rem>=A[i+1]:
            ans+=A[i+1]
            A[i+1]=0
        else:
            ans+=rem
            A[i+1]-=rem
    else:
        ans+=B[i]
        A[i]-=B[i]
    print(ans,A)
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(input().split())
b=list(input().split())
for i in range(len(a)):
    a[i]=int(a[i])
for i in range(len(b)):
    b[i]=int(b[i])
print(min(sum(a),sum(b)))

Submission status: Wrong Answer

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

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

print(fenum - sum(A))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n = int(input())
ls_a = list(map(int, input().split()))
ls_b = list(map(int, input().split()))

ans = 0
res = 0

for i in range(n):
	ans += min(ls_a[i], res + ls_b[i])
	if res >= ls_a[i]:
		res == ls_b[i]
	else:
		res = max(res + ls_b[i] - ls_a[i], 0)

if res >= ls_a[n]:
	print(ans + ls_a[n])
elif 0 <= res < ls_a[n]:
	print(ans + res)
else:
	print(ans)