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

anscount=[0]*(n+1)
for i in range(n+1):
    if i%2==0:
        now=i//2
        teki=a[now]-anscount[now]
        if teki<=b[now]:
            b[now]-=teki
            anscount[now]+=teki
            if a[now+1]<=b[now]:
                anscount[now+1]=a[now+1]
                b[now]-=a[now+1]
            else:
                anscount[now+1]=b[now]
                b[now]-=0
        else:
            anscount[now]+=b[now]
            b[now]=0
    else:
        now=n-(i//2)
        teki=a[now]-anscount[now]
        if teki<=b[now-1]:
            b[now-1]-=teki
            anscount[now]+=teki
            if a[now-1]<=b[now-1]:
                anscount[now-1]=a[now-1]
                b[now-1]-=a[now-1]
            else:
                anscount[now-1]=b[now-1]
                b[now-1]-=0
        else:
            anscount[now]+=b[now-1]
            b[now-1]=0
# print(b)
print(sum(anscount))
# print(anscount)

Submission status: Wrong Answer

Solution code:
n = int(input())
cities = list(map(int,input().split()))
braves = list(map(int,input().split()))
braves.append(0)
count = 0
for i in range(n+1):
  c = min(braves[i],cities[i])
  count += c
  if i != n:
    braves[i+1] += braves[i] - c
print(count)

Submission status: Wrong Answer

Solution code:

N = int(input())
A_lst = list(map(int, input().split()))
B_lst = list(map(int, input().split()))

ans = 0

for i in range(N-1, -1, -1):
    if A_lst[i+1] >= B_lst[i]:
        ans += B_lst[i]
        A_lst[i+1] -= B_lst[i]
        B_lst[i] = 0
    else:
        ans += A_lst[i+1]
        B_lst[i] -= A_lst[i+1]
        A_lst[i+1] = 0

#    print("i=",i)
#    print(A_lst)
#    print(B_lst)
    
    if A_lst[i] >= B_lst[i]:
        ans += B_lst[i]
        A_lst[i] -= B_lst[i]
        B_lst[i] = 0
    else:
        ans += A_lst[i]
        B_lst[i] -= A_lst[i]
    

#    print("i=",i)
#    print(A_lst)
#    print(B_lst)
#    print()




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

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(_) for _ in input().split()]
b = [int(_) for _ in input().split()]
ans = 0
for i in range(n):
    if a[i] <= b[i]:
        ans += a[i]
        leftover = b[i] - a[i]
        if a[i + 1] <= leftover:
            ans += a[i + 1]
        else:
            ans += leftover
            a[i + 1] = a[i + 1] - leftover
    elif a[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()]

ans = 0
for i in range(N):
    # モンスターのほうが多い
    if B[i] <= A[i]:
        ans += B[i]
        B[i] = 0
    else:
        ans += A[i]
        B[i] -= A[i]
        if B[i] <= A[i+1]:
            ans += B[i]
            B[i] = 0
        else:
            ans += A[i+1]
            B[i] = 0
            A[i+1] -= 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()]

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

print(enemy)

Submission status: Wrong Answer

Solution code:
N = int(input())
An = list(map(int, input().split()))
Bn = list(map(int, input().split()))

cnt = 0

while len(An) > 2:
    m = An.pop(0)
    y = Bn.pop(0)
    if y < m:
        cnt += y
    elif m <= y < m+An[0]:
        cnt += y
        An[0] -= y-m
    else:
        cnt += m+An[0]

        
else:
    cnt += min(sum(An), sum(Bn))

print(cnt)

Submission status: Wrong Answer

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

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] <= 0:
        a[i] = 0
    if b[i] > a[i]:
        check = a[i+1]+a[i]
        a[i+1] -= b[i]-a[i]
    else:
        check = a[i]
        a[i] -= b[i]
    if a[i] == 0 or a[i+1] <= 0:
        cnt += check
    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()))
ans = 0
for i in range(N):
    if A[i] > B[i]:
        ans += B[i]
        A[i] -= B[i]
        B[i] -= B[i]
    else:
        ans += A[i]
        B[i] -= A[i]
        A[i] -= A[i]
        if A[i+1] > B[i]:
            ans += B[i]
            B[i] -= B[i]
            A[i+1] -= B[i]
        else:
            ans += A[i+1]
            B[i] -= A[i+1]
            A[i+1] -= A[i+1]
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] + A[i+1], B[i])
    A[i+1] -= max(B[i] - 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=0
b=0
if A[0]>B[0]:
    S+=B[0]
else:
    b=B[0]-A[0]
    S+=A[0]
for i in range(1,N):
    if b+B[i]<A[i]:
        S+=b+B[i]
    elif b>=A[i]:
        S+=A[i]
        b=B[i]
    else:
        S+=A[i]
        b=b+B[i]-A[i]
if b>A[N]:
    S+=A[N]
else:
    S+=b
print(S)

Submission status: Wrong Answer

Solution code:


def submit():
    n = int(input())
    alist = list(map(int, input().split()))
    blist = list(map(int, input().split()))


    # 順番にグリーディ
    a = alist.copy()
    b = blist.copy()
    score_a = 0
    for i in range(0, n):
        if a[i] < b[i]:
            score_a += a[i]
            b[i] -= a[i]
            a[i] = 0
        else:
            score_a += b[i]
            a[i] -= b[i]
            b[i] = 0

        if a[i + 1] < b[i]:
            score_a += a[i + 1]
            b[i] -= a[i + 1]
            a[i + 1] = 0
        else:
            score_a += b[i]
            a[i + 1] -= b[i]
            b[i] = 0

    # 逆順にグリーディ
    a = alist.copy()
    a.reverse()
    b = alist.copy()
    b.reverse()
    score_b = 0
    for i in range(0, n):
        if a[i] < b[i]:
            score_b += a[i]
            b[i] -= a[i]
            a[i] = 0
        else:
            score_b += b[i]
            a[i] -= b[i]
            b[i] = 0

        if a[i + 1] < b[i]:
            score_b += a[i + 1]
            b[i] -= a[i + 1]
            a[i + 1] = 0
        else:
            score_b += b[i]
            a[i + 1] -= b[i]
            b[i] = 0

    print(max(score_a, score_b))
    

if __name__ == '__main__':
    submit()

Submission status: Wrong Answer

Solution code:
n=int(input())

monst=list(map(int,input().split()))
yusya=list(map(int,input().split()))
ans=0
for i in range(n):
  if(i==0):
    kill=min(yusya[0],monst[0])
    ans+=kill
    yusya[0]-=kill
    monst[0]-=kill
  else:
    kill=min(yusya[i-1],monst[i])
    ans+=kill
    yusya[i-1]-=kill
    monst[i-1]-=kill
    kill=min(yusya[i],monst[i])
    ans+=kill
    yusya[i]-=kill
    monst[i]-=kill
    
ans+=min(monst[-1],yusya[-1])

print(ans)

Submission status: Wrong Answer

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

kills = 0

for i in range(N):
    if B[i] <= A[i]:
        # 次無理
        kills += B[i]
    else:
        # 次OK
        kills += A[i]
        stock = B[i] - A[i]
        if stock < A[i + 1]:
            # 殲滅無理
            kills += stock
        else:
            # 殲滅可能
            kills += A[i + 1]
print(kills)

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


kill = 0
tmp = 0
for i in range(len(A)):
    A[i] = A[i] - tmp
    kill += tmp
    if B[i] > A[i]:
        tmp = B[i] - A[i]
        kill += A[i]
    else:
        tmp = 0
        kill += B[i]

print(kill)

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

Submission status: Wrong Answer

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

def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    count = 0
    nokori = 0

    for i in range(len(A)):
        mon = A[i]
        if nokori > mon:
            count += mon
            mon = 0
        else:
            count  += nokori
            mon -= nokori
        if i < N:
            if B[i] >= mon:
                count += mon
                nokori = B[i] - mon
            else:
                count += B[i]
    print(count)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())

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

ans = 0

for i in range(len(b)-1, -1, -1):

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

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

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

Submission status: Wrong Answer

Solution code:

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 B[i] - A[i] >= 0:
      cnt += A[i]
      B[i] -= A[i]
      A[i] = 0
    elif B[i] - A[i] < 0:
      cnt += B[i]
      tmp = B[i]
      B[i] = 0
      A[i] -= tmp
    if B[i] == 0:
      continue
    if B[i] - A[i+1] >= 0:
      cnt += A[i+1]
      B[i] -= A[i+1]
      A[i+1] = 0
    elif B[i] < A[i+1]:
      cnt += B[i]
      tmp = B[i]
      B[i] = 0
      A[i] -= tmp
  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 = 0
"""
ai = 0
bi = 0
while 1:
    if max(b) == 0:
        break
    if a[ai] < b[bi]:
        ans += a[ai]
        b[bi] -= a[ai]
        a[ai] = 0
        ai += 1
    elif a[ai] == b[bi]:
        ans += a[ai]
        b[bi] = 0
        a[ai] = 0
        ai += 1
        bi += 1
    else:
        ans += b[bi]
        a[ai] -= b[bi]
        b[bi] = 0
        if ai > bi:
            bi += 1
        else:
            ai += 1
            bi += 1
 #   print(a)
 #   print(b)
 #   print(ai,bi)
    if ai >= n+1 or bi >= n:
        break
print(ans)
"""
for i in range(n):
    if b[i] >= a[i] + a[i+1]:
        ans += a[i] + a[i+1]
    else:
        if b[i] >= a[i]:
            ans += b[i]
            a[i+1] -= (b[i]-a[i])
        else:
            ans += b[i]
print(ans)

Submission status: Wrong Answer

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

cnt = 0

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

print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
enemies = list(map(int, input().split()))
powers = list(map(int, input().split()))
powers.append(0)
nokori = 0
cnt = 0


for i in range(n+1):
  cnt += min(enemies[i], powers[i]+nokori)
  if powers[i]+nokori > enemies[i]:
    nokori = powers[i]+nokori - enemies[i]
  else:
    nokori = 0

print(cnt)

Submission status: Wrong Answer

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

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

r_monster = 0
r_yusya = 0
monster_cnt = 0
for a, b in zip(reversed(A), reversed(B)):
    if a+r_monster > b+r_yusya:
        monster_cnt += b+r_yusya
        r_yusya = 0
        r_monster = min(a+r_monster - b - r_yusya, a)
    else:
        monster_cnt += a+r_monster
        r_yusya = min(b+r_yusya - a - r_monster, b)
        r_monster = 0

print(monster_cnt)

Submission status: Wrong Answer

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

def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    count = 0
    nokori = 0

    for i in range(len(A)):
        mon = A[i]
        if nokori > mon:
            count += mon
        else:
            count  += nokori
            mon -= nokori
        if i < N:
            if B[i] >= mon:
                count += mon
                nokori = B[i] - mon
            else:
                count += B[i]
    print(count)


if __name__ == '__main__':
    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()]

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

Submission status: Wrong Answer

Solution code:
# n,m = map(int,input().split())
n = int(input())
# n = input()
a = list(map(int,input().split()))
b = list(map(int,input().split()))
cnt = 0
left = 0
for i in range(n):
    a1,b1 = a[i],b[i]
    if a1 <= b1 + left:
        cnt += a1
        left = min(b1 - a1 + left,b1)
    else:
        cnt += b1
        left = 0
if a[n] <= left:
    cnt += a[n]
else:
    cnt += left
print(cnt)

Submission status: Wrong Answer

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

sum = 0
kuriage = 0
for i in range(n):
  if int(b[i]) + kuriage > int(a[i]):
    sum += int(a[i])
    kuriage = kuriage - int(a[i])
    if kuriage > 0:
      kuriage = 0
    kuriage += int(b[i])
    #print("amaru"+a[i])
  else:
    kuriage = 0
    sum += int(b[i]) + kuriage
    #print(int(b[i]) + kuriage)

if kuriage > 0:
  sum += min(kuriage,int(a[n]))
    
print(sum)

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

Submission status: Wrong Answer

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

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()] + [0]

c = 0
leave = 0

for i, j in zip(a, b):
    c += min(i, j+leave)
    leave = max(0, j+leave - i)
print(c)

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()]
last = 0
cnt = 0
for ind,i in enumerate(A):
    lmon = i-last
    if(ind<N):
        lpow = B[ind]
    else:
        lpow = 0
    cnt += min(last+lpow,i)
    last = max(lpow-lmon,0)
print(cnt)

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time
sys.setrecursionlimit(10**7)
inf = 10**20

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

Submission status: Wrong Answer

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

print(ans)

Submission status: Wrong Answer

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

    #print(now)
    #print(A)
    #print(B)
    if A[i] <= now:
        A[i] = 0
    else:
        A[i] -= now
    #print(A)
    #print(B)
print(count - sum(A))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
def INT_(n): return int(n)-1
def MI(): return map(int, input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_, input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n: int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split())) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')


def main():
    N = I()
    A = LI()
    B = LI()

    total = 0

    res = 0
    for i in range(N):
        monster = A[i] - res
        if monster >= B[i]:
            total += B[i]
            res = 0
        else:
            total += A[i]
            res = B[i] - monster
    total += min(res, A[N])
    print(total)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(raw_input())
ms = map(int, raw_input().split(' '))
bs = map(int, raw_input().split(' '))
msorig = ms[:]

for i in range(len(bs)):
    if bs[i] >= ms[i]:
        ms[i+1] = max(0, min(0, bs[i] - ms[i]))
        ms[i] = 0
    else:
        ms[i] = max(0, ms[i] - bs[i])

print sum(msorig) - sum(ms)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#ABC135 C

import sys
import math
import bisect
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
mod = 10**9 + 7

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

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

Submission status: Wrong Answer

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

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

#print(A)
#print(B)
print(defeated_num)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

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

ans = 0
yoryoku = 0
for i in range(N):
    power = B[i] + yoryoku
    ans += min(power, A[i])
    yoryoku = max(power - A[i], 0)

ans += min(A[N], yoryoku)
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)
mae=0
kazu=0
for i in range(N+1):
    if A[i]<=B[i]+mae:
        kazu+=A[i]
        mae=B[i]+mae-A[i]
    else:
        kazu+=B[i]+mae
        mae=0
print(kazu)

Submission status: Wrong Answer

Solution code:
# coding: UTF-8
import sys
#sys.setrecursionlimit(n)
import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

readl= lambda: list(map(int, sys.stdin.readline().split()))
readt= lambda: tuple(map(int, sys.stdin.readline().split()))
read = lambda: sys.stdin.readline().rstrip()
readi = lambda: int(read())
readmi = lambda: map(int, sys.stdin.readline().split())
readms = lambda: map(str, sys.stdin.readline().split())

n = readi()
a = readl()
b = readl()
count = 0
for i in range(n):
    for j in range(2):
        t = min(a[i + j], b[i])
        count += t
        a[i] -= t
        b[i] -= t
print(count)

Submission status: Wrong Answer

Solution code:

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

guchoku=0
for i,b in enumerate(B):
    t = A[i] - b
    if t <= -1:
        zen = min(A[i+1] - t, 0)
        guchoku += A[i+1] - zen
        A[i+1] = zen
    guchoku += min(A[i], b)
print(guchoku)

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:

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 B[i] >= A[i]:
      cnt += A[i]
      B[i] -= A[i]
    elif B[i] < A[i]:
      cnt += B[i]
      B[i] = 0
    if B[i] == 0:
      continue
    if B[i] >= A[i+1]:
      cnt += A[i+1]
      B[i] -= A[i+1]
    elif B[i] < A[i+1]:
      cnt += B[i]
      B[i] = 0
  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()))

cnt = 0

for i in range(N):
    if A[i] == 0:
        continue
    if A[i] >= B[i]:
        cnt += B[i]
        continue
    else:
        cnt += A[i]
        B[i] -= A[i]
        if A[i+1] >= B[i]:
            cnt += B[i]
            A[i+1] -= B[i]
            continue
        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()))
ans = 0
for i in range(N-1):
    ans += min(A[i], B[i])
    B[i+1] += max(B[i]-A[i], 0)
ans += min(A[N-1] + A[N], B[N-1])
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 A[n]>=B[n]:
        ans += B[n]
    elif A[n+1]>=B[n]-A[n]:
        ans += B[n]
        A[n+1] += A[n+1] - B[n] + A[n]
    else:
        ans += A[n] + A[n+1]
        A[n+1] = 0
        
print(ans)

Submission status: Wrong Answer

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

c = 0
leave = 0

for i, j in zip(a, b):
    c += min(i, j+leave)
    if j+leave - i > 0:
        leave = j+leave - i

print(c)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n = ni()
a = list(li())
b = list(li())

cnt = 0

for i in range(n+1):
    for j in range(i-1, i+1):
        if j < 0 or j >= n:
            continue

        if b[j] == 0:
            continue

        elif a[i] <= b[j]:
            cnt += a[i]
            b[j] -= a[i]
            a[i] = 0
            break

        else:
            cnt += b[j]
            b[j] = 0
            a[i] -= b[j]

print(cnt)

Submission status: Wrong Answer

Solution code:
inf = 10 ** 18
P = 10 ** 9 + 7

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

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

r_monster = 0
r_yusya = 0
monster_cnt = 0
for a, b in zip(reversed(A), reversed(B)):
    if a+r_monster > b+r_yusya:
        monster_cnt += b+r_yusya
        r_yusya = 0
        r_monster = min(a+r_monster - b - r_yusya, a)
    else:
        monster_cnt += a+r_monster
        r_yusya = min(b+r_yusya - a - r_monster, b)
        r_monster = 0

if A[0] > r_yusya:
    monster_cnt += r_yusya
else:
    monster_cnt += A[0]
print(monster_cnt)

Submission status: Wrong Answer

Solution code:
n=int(input())
T=0;
p=input().rstrip().split(' ')
q=input().rstrip().split(' ')
for i in range(0,len(q)):
    A=int(q[i])
    B=int(p[i])
    C=int(p[i+1])
    if A>=B:
        T+=B;
        A=A-B;
        if(A>=C):
            T+=C;
            A=A-C;
        else:
            p[i+1]=int(p[i+1])-A;
            T+=A;
    else:
        T+=A;
print(T)

Submission status: Wrong Answer

Solution code:
N = int(input())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))

sum_list = [None] * N
for i in range(N):
    sum_list[i] = a_list[i] + a_list[i + 1]

total = 0
for b, s in zip(b_list, sum_list):
    total += min(b, s)
print(total)