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

Submission status: Wrong Answer

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

print(cnt)

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    b_list = list(map(int, input().split()))

    count = 0
    for i in range(n):
        # print("-"*50)
        # print("A: {} A+1: {} B: {}".format(a_list[i], a_list[i+1], b_list[i]))
        if a_list[i] >= b_list[i]:
            a_list[i] -= b_list[i]
            count += b_list[i]
            b_list[i] = 0
        else:
            b_list[i] -= a_list[i]
            count += a_list[i]
            a_list[i] = 0

        # print("A: {} A+1: {} B: {}".format(a_list[i], a_list[i + 1], b_list[i]))

        if a_list[i+1] >= b_list[i]:
            a_list[i+1] -= b_list[i]
            count += b_list[i]
            b_list[i] = 0
        else:
            b_list[i] -= a_list[i+1]
            count += a_list[i+1]
            a_list[i] = 0

        # print("A: {} A+1: {} B: {}".format(a_list[i], a_list[i + 1], b_list[i]))

    print(count)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
l1 = list(map(int, input().split()))
l2 = list(map(int, input().split()))
c = 0

for i in range(n):
    if i != n-1:
        if l2[i] >= l1[i]:
            c += l1[i]
        else:
            c += l2[i]

        l2[i+1] += max(0, l2[i]-l1[i])
    else:
        if l2[i] >= l1[i] + l1[i+1]:
            c += l1[i] + l1[i+1]
        else:
            c += l2[i]

print(c)

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])
	res = max(res + ls_b[i] - ls_a[i], 0)

print(ans + min(res, ls_a[n]))

Submission status: Wrong Answer

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

ans = min(ls_a[0], ls_b[0])
res = max(ls_b[0] - ls_a[0], 0)

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

print(ans + min(res, ls_a[n]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

print(monster)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/abc135/tasks/abc135_c

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = 0
l = A[0]
for i in range(N):
    r = A[i + 1]
    cur = B[i]
    # print(l, r, cur)
    # ans += min(l + r, cur)
    # l = max(r - max(cur - l, 0), 0)  # めちゃくちゃ整理するとこの形の式になる

    # わかりやすくかきなおす
    ans += min(l, cur)
    rest_power = max(cur - l, 0)
    ans += min(rest_power, l)
    l = max(r - rest_power, 0)  # next l

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 = A[0]
for i in range(N):
    r = A[i + 1]
    cur = B[i]
    # print(l, r, cur)
    ans += min(l + r, cur)
    l = max(r + max(cur - l, 0), 0)  # めちゃくちゃ整理するとこの形の式になる
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import itertools
import bisect
import math

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n = ni()
a = na()
b = na()

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

print(min(res, a[n]) + ans)

Submission status: Wrong Answer

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

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

print(total)

Submission status: Wrong Answer

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

def kill(saver, next, curr):
	killed = 0
	if saver >= next:
		saver -= next
		killed += next
		if saver >= curr:
			killed += curr
			return 0, killed
		else:
			killed += saver
			return curr-saver, killed 
	else:
		killed += next-saver
		return curr, killed

cnt = 0
res = A[-1]
for i in range(len(B)):
	ans = kill(B[len(B)-i-1], res, A[len(A)-i-2])
	res = ans[0]
	cnt += ans[1]
print(cnt)

Submission status: Wrong Answer

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

cnt = 0
c_ = 0

for i in range(n+1):
    if i == n:
        c = c_ - a[i]
        if c > 0:
            cnt += a[i]
        else:
            cnt += c_

    else:
        c = c_ + b[i] - a[i]
        if c > 0:
            c_ = c
            cnt += a[i]
        else:
            cnt += b[i] + c_
            c_ = 0

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
c_ = 0

for i in range(n+1):
    if i == n:
        c = c_ - a[i]
        if c > 0:
            cnt += a[i]
        else:
            cnt += c_

    else:
        c = c_ + b[i] - a[i]
        if c > 0:
            c_ = c
            cnt += a[i]
        else:
            c_ = 0
            cnt += b[i]


print(cnt)

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def main():
    N = I()
    a = LI()
    b = LI()
    a = [0] + a
    b = [0] + b + [0]
    cnt = 0
    for i in range(1, N+2):
        if a[i] - b[i-1] >= 0:
            cnt += b[i-1]
            a[i] = a[i] - b[i-1]
        else:
            cnt += a[i]
            b[i-1] = b[i-1] - a[i]

        if a[i] - b[i] >= 0:
            cnt += b[i]
            a[i] = a[i] - b[i]
        else:
            cnt += a[i]
            b[i] = b[i] - a[i]
    print(cnt)

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

print(count)

Submission status: Wrong Answer

Solution code:
n=int(input())
mons=list(map(int,input().split()))
fight=list(map(int,input().split()))
nums=[x for x in range(n)]
t=[]
for i in nums:
  mons[i]=mons[i]+mons[i+1]
for i in nums:
  if(mons[i]>=fight[i]):
    t.append(fight[i])
  else:
    t.append(mons[i])
print(sum(t))

Submission status: Wrong Answer

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

sumbuster = 0
for i in range(n):
    ibuster=0
    j = i
    ibuster += min(b[i],a[j]+a[j+1])
    if(b[i] >= (a[j]+a[j+1])):
        a[j+1] = 0
    sumbuster += ibuster

print(sumbuster)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

sumbuster = 0
for i in range(n):
    ibuster=0
    j = i
    ibuster += min(b[i],a[j]+a[j+1])
    print(ibuster)
    if(b[i] >= (a[j]+a[j+1])):
        a[j+1] = 0
    sumbuster += ibuster

print(sumbuster)

Submission status: Wrong Answer

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

res = 0
# iの時, B_(i+1)がまだ倒せる数
pre_B = 0
# iの時, A_(i)が残っている数
pre_A = 0
for i in range(N, -1, -1):
    num_hits = 0
    if (A[i+1]-pre_A)>B[i]:
        num_hits += B[i]
        pre_B = 0
    else:
        num_hits += A[i+1]
        pre_B = B[i]-A[i+1]
        if A[i]>pre_B:
            num_hits += pre_B
            pre_B = 0
            pre_A = A[i]-pre_B
        else:
            num_hits += A[i]
            pre_B = pre_B - A[i]
            pre_A = 0

    res += num_hits
print(res)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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 B[i]>=A[i]:
        sum+=A[i]
        if A[i+1]>=B[i]-A[i]:
            sum+=B[i]-A[i]
        else:
            sum+=A[i+1]
            A[i+1]=0
    else:
        sum+=B[i]
print(sum)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

total = 0
for i in reversed(range(len(B))):
    # print(i)
    if A[i+1] >= B[i]:
        total += B[i]
        B[i] = 0
        continue
    else:
        total += A[i+1]
        B[i] -= A[i+1]
        if A[i] >= B[i]:
            total += B[i]
            A[i] -= B[i]
        else:
            total += A[i]
            

print(total)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys

if sys.platform =='ios':
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

N = INT()
A = MAP()
B = MAP()

ans = 0
for i in range(N):
	if A[i] >= B[i]:
		ans += B[i]
		A[i] -= B[i]
	else:
		#print(i+1,'th yuusya({})'.format(B[i]),A[i],A[i+1])
		# 残っているA[i]がいれば片付ける
		ans += A[i]
		A[i] = 0
		B[i] -= A[i]
		
		# 次の怪獣をできるかぎり倒しておく
		if A[i+1] >= B[i]:
			A[i+1] -= B[i]
			ans += B[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()))
cnt=0
ans=0
for i in range(N):
  ans+=min(A[i], B[i])
  if A[i] < B[i]:
    cnt += B[i] - A[i]
print(ans+min(cnt,A[-1]))

Submission status: Wrong Answer

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

if N != 1:
    for i in range(1,N):
        if A[i] - B[i-1] <= 0:
            dp[i] = A[i]
        elif A[i] - B[i-1] >= B[i]:
            dp[i] = B[i-1] + B[i]
            B[i] = 0
        else:
            dp[i] = A[i]
            B[i-1] -= A[i]
            B[i] -= A[i] - B[i-1]
print(sum(dp))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

    if A[-1] <= B[-1]:
        dp[-1] = A[-1]
        B[-1] -= A[-1]
    else:
        dp[-1] = B[-1]
        B[-1] = 0

    print(sum(dp))

Submission status: Wrong Answer

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



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

Submission status: Wrong Answer

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

if A[-1] <= B[-1]:
    dp[-1] = A[-1]
    B[-1] -= A[-1]
else:
    dp[-1] = B[-1]
    B[-1] = 0

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

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    monsters = [int(x) for x in input().split()]
    heroes = [int(x) for x in input().split()]

    ans_list = [0] * (n+1)
    rest_hero = 0
    for i in range(n+1):
        if i == n:
            now_hero = rest_hero
        else:
            now_hero = rest_hero + heroes[i]
        if now_hero >= monsters[i]:
            ans_list[i] = monsters[i]
            rest_hero = now_hero - monsters[i]
        else:
            ans_list[i] = now_hero
            rest_hero = 0
    ans = sum(ans_list)
    print(ans)


    print()


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

print(ans+a[-1] if a[-1] < c else ans+c)

Submission status: Wrong Answer

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

total = 0
for i in reversed(range(len(B))):
    if i == 0:
        break
    if A[i+1] > 0:
        down1 = min(A[i+1], B[i])
        A[i+1] -= down1
        B[i] -= down1
        total += down1
    if B[i] > 0:
        down2 = min(A[i], B[i])
        A[i] -= down2
        B[i] -= down2
        total += down2
    # print("A:", A)
    # print("B:", B)

print(total)

Submission status: Wrong Answer

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

total = 0
for i in reversed(range(len(A))):
    if i == 0:
        break
    down1 = min(A[i], B[i-1])
    B[i-1] -= down1
    total += down1
    down2 = min(A[i-1], B[i-1])
    B[i-1] -= down2
    total += down2


print(total)

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

if A[-1] > 0:
  ans += max(0,B[-1]-A[-2])
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
ans = 0
for i in range(N):
  ans += min(A[i], B[i])
  A[i+1] -= max(0, B[i]-A[i])

if A[-1] > 0:
  ans += max(0,B[-1]-A[-2])
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
ans = 0
for i in range(N):
  ans += min(A[i], B[i])
  A[i+1] -= max(0, B[i]-A[i])

if A[-1] > 0:
  ans += max(0,B[i]-A[i])
print(ans)

Submission status: Wrong Answer

Solution code:
# C - City Savers
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

if B[0]-A[0] >= 0:
    ans = B[0]
    B[0] = B[0]-A[0]
    if B[0] - A[1] >= 0:
        A[1] = 0
    else:
        A[1] = A[1]-B[0]
else:
    ans = B[0]
    B[0] = 0

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

        else:
            A[i+1] = A[i+1] - tmp
    else:
        ans += B[i]
if B[N-1]-A[N] > 0:
    ans += A[N]
else:
    ans += B[N-1]


print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
ans = 0

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin


def main():
    readline = stdin.readline
    readline()
    *A, = map(int, readline().split())
    *B, = map(int, readline().split())
    B.append(0)
    ans = 0
    mon = 0
    pre = 0
    for a, b in zip(A, B):
        pre += b
        mon += a
        tmp = mon if mon < pre else pre
        ans += tmp
        mon = a - tmp
        pre -= tmp
    print(ans)
    return


main()

Submission status: Wrong Answer

Solution code:
from sys import stdin


def main():
    readline = stdin.readline
    readline()
    *A, = map(int, readline().split())
    *B, = map(int, readline().split())
    B.append(0)
    ans = 0
    pre = 0
    for a, b in zip(A, B):
        pre += b
        tmp = a if a < pre else pre
        pre -= tmp
        ans += tmp
    print(ans)
    return


main()

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

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

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

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

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

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
    right = min(a[i + 1], b[i])
    ans +=  (right + left) % (10**9 + 7)

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

print(ans)

Submission status: Wrong Answer

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

sum_a = [0] * n
for i in range(n):
    if i != n:
        sum_a[i] = a[i] + a[i + 1]

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

sum_a = [0] * n
for i in range(n):
    if i != n:
        sum_a[i] = a[i] + a[i + 1]

ans = 0
for x, y in zip(sum_a, b):
    if x >= y:
        ans += y
    elif x < y:
        ans += x

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
rest = 0

if a[0] + a[1] - b[0] >= 0:
    count += b[0]
    if a[0] <= b[0]:
        rest = a[0] + a[1] - b[0]
    else:
        rest = a[1]    
else:
    count += a[0] + a[1]


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

print(count)

Submission status: Wrong Answer

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

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

if a[0] + a[1] - b[0] >= 0:
    count += b[0]
    if a[0] <= b[0]:
        rest = a[0] + a[1] - b[0]
    else:
        rest = a[1]    
else:
    count += a[0] + a[1]


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

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

Submission status: Wrong Answer

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

for i in range(n):
    for j in range(i,i+2):
        c = b[i] - a[j]
        if c <= 0:
            a[j] -= b[i]
            s += b[i]
            break
        else:
            b[i] -= a[j]
            s += a[j]

print(s)

Submission status: Wrong Answer

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

for i in range(n):
    for j in range(i,i+2):
        c = b[i] - a[j]
        if c <= 0:
            s += b[i]
            break
        else:
            b[i] -= a[j]
            s += a[j]

print(s)

Submission status: Wrong Answer

Solution code:
N = int(input())
li_a = list(map(int, input().split())) #町にいるモンスターの数
li_b = list(map(int, input().split())) #勇者が倒せるモンスターの数
ans = 0
for i in range(N):
  if li_a[i] >= li_b[i]:
    ans += li_b[i]
    #li_a[i+1] += li_a[i] - li_b[i]
  else:
    ans += min(li_a[i] + li_a[i+1], li_b[i])
    li_a[i+1] -= min(li_b[i] - li_a[i], 0)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
li_a = list(map(int, input().split())) #町にいるモンスターの数
li_b = list(map(int, input().split())) #勇者が倒せるモンスターの数
ans = 0
for i in range(N):
  if li_a[i] >= li_b[i]:
    ans += li_b[i]
    #li_a[i+1] += li_a[i] - li_b[i]
  else:
    ans += min(li_a[i] + li_a[i+1], li_b[i])
    #li_a[i+1] = max(li_a[i] + li_a[i+1] - li_b[i], 0)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
li_a = list(map(int, input().split())) #町にいるモンスターの数
li_b = list(map(int, input().split())) #勇者が倒せるモンスターの数
ans = 0
for i in range(N):
  if li_a[i] >= li_b[i]:
    ans += li_b[i]
    li_a[i+1] += li_a[i] - li_b[i]
  else:
    ans += min(li_a[i] + li_a[i+1], li_b[i])
    li_a[i+1] = max(li_a[i] + li_a[i+1] - li_b[i], 0)
print(ans)

Submission status: Wrong Answer

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

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]+A[i+1]>B[i]:
        Sum += B[i]
    else:
        Sum += A[i]+A[i+1]
print(Sum)

Submission status: Wrong Answer

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


n=int(input())
al = list(map(int,input().split()))
bl = list(map(int,input().split()))

ans = 0
rp = 0
for idx, a in enumerate(al):
    if idx == n:
        ans += min(rp, a)
    else:
        b = bl[idx]
        enemy = min(a,b+rp)
        ans += enemy
        rp = max(b - enemy+ rp, 0)
print(ans)