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 = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
#左側から倒せるだけ倒すのが正解
Monst = 0
killed = 0
for n,b in enumerate(B):
        cur = A[n]
        nex = A[n+1]
        cur -= killed
        if b<=cur:
            Monst += b
        elif b<=cur+nex:
            Monst += b
            killed = b - cur
        else:
            Monst += cur+nex
            killed = nex
print(Monst)

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

print(ans)

Submission status: Wrong Answer

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

cnt = 0

for i, b in enumerate(B):

    if b >= A[i]:
        cnt += A[i]
        b -= A[i]

        if b >= A[i+1]:
            cnt += A[i+1]
        else:
            cnt += b
    else:
        cnt += b
print(cnt)

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    a,b = LI()
    if (a-b)%2 == 0:
        print((a+b)//2)
    else:
        print("IMPOSSIBLE")
    return

#B
def B():
    n = I()
    p = LI()
    k = [p[i] for i in range(n)]
    k.sort()
    s = 0
    for i in range(n):
        s += (p[i] != k[i])
    if s < 3:
        print("YES")
    else:
        print("NO")
    return

#C
def C():
    n = I()
    a = LI()
    b = LI()
    b.append(0)
    ans = 0
    sb = sum(b)
    i = n
    while i >= 0:
        if a[i] > b[i]:
            a[i] -= b[i]
            b[i] = 0
        else:
            b[i] -= a[i]
            a[i] = 0
        if a[i] > b[i-1]:
            a[i] -= b[i-1]
            b[i] = 0
        else:
            b[i-1] -= a[i]
            a[i] = 0
        i -= 1
        
    print(sb-sum(b))
    return

#D
def D():
    n = I()

    return

#E
def E():
    n = I()

    return

#F
def F():
    n = I()

    return

#Solve
if __name__ == "__main__":
    C()

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(x) for x in input().split()]
b = [int(x) for x in input().split()]

ans = 0

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

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

Submission status: Wrong Answer

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

Z = A[::-1]

ans1 = 0
for i in range(N):
    rem = B[i]
    p = min(A[i], rem)
    ans1 += p
    rem -= p
    if rem:
        q = min(A[i+1], rem)
        ans1 += q

ans2 = 0
B = B[::-1]
for i in range(N):
    rem = B[i]
    p = min(Z[i], rem)
    ans2 += p
    rem -= p
    if rem:
        q = min(Z[i+1], rem)
        ans2 += q

print(max(ans1,ans2))

Submission status: Wrong Answer

Solution code:
a = int(input())
b = list(map(int, input().split()))
c = list(map(int, input().split()))
d = 0
for i in range(a-1, -1, -1):
    d += min(b[i+1], c[i])
    b[i+1] -= min(b[i+1], c[i])
    c[i] -= min(b[i+1], c[i])

    d += min(b[i], c[i])
    b[i] -= min(b[i], c[i])
    c[i] -= min(b[i], c[i])
print(d)

Submission status: Wrong Answer

Solution code:

N = int(input())
As =   list(map(int,input().split(" ")))
Bs =   list(map(int,input().split(" ")))
ans = 0
for i in range(N):
    ans += min(As[i],Bs[i]) #i町での討伐数
    Bs[i] = max(Bs[i] - As[i] , 0 ) #勇者iの余力
    ans +=min(As[i + 1],Bs[i]) #i + 1町での討伐数
    As[i] = max(As[i + 1] - Bs[i], 0)
    # if As[i] <= Bs[i]: #全員倒せた
    #     ans += As[i]
    #     Bs[i] -= As[i]
    #     if As[i + 1] <= Bs[i]:
    #         ans += Bs[i]
    #
    #
    #     As[i + 1] = max ( As[i + 1] - Bs[i] ,0)
    #
    # else: #全員は倒せなかった
    #     ans += Bs[i]

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    a,b = LI()
    if (a-b)%2 == 0:
        print((a+b)//2)
    else:
        print("IMPOSSIBLE")
    return

#B
def B():
    n = I()
    p = LI()
    k = [p[i] for i in range(n)]
    k.sort()
    s = 0
    for i in range(n):
        s += (p[i] != k[i])
    if s < 3:
        print("YES")
    else:
        print("NO")
    return

#C
def C():
    n = I()
    a = LI()
    b = LI()
    b.append(0)
    ans = 0
    i = n
    while i >= 0:
        if a[i] > b[i]:
            ans += b[i]
            a[i] -= b[i]
            b[i] = 0
        else:
            ans += a[i]
            b[i] -= a[i]
            a[i] = 0
        if a[i] > b[i-1]:
            ans += b[i-1]
            a[i] -= b[i-1]
            b[i] = 0
        else:
            ans += a[i]
            b[i-1] -= a[i]
            a[i] = 0
        i -= 1
    print(ans)
    return

#D
def D():
    n = I()

    return

#E
def E():
    n = I()

    return

#F
def F():
    n = I()

    return

#Solve
if __name__ == "__main__":
    C()

Submission status: Wrong Answer

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

        if heroes[i] >= monsters[i+1]:
            total += monsters[i+1]
            heroes[i] -= monsters[i+1]
        else:
            total += heroes[i]
            heroes[i] = 0

    print(total)

main()

Submission status: Wrong Answer

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
inf=float('inf')
mod = 10**9+7
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()
    B=LI()
    A=LI()
    ans = 0
    for i in range(N):
        ans += max(0,min(A[i],B[i]))
        A[i]-= B[i]
        ans += max(0,min(A[i],B[i+1]))
        B[i+1]-=A[i]
    #ans += max(0,min(B[-1],A[-1]))
    print(ans)
if __name__ == '__main__':
    main()

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


killed = 0
for i in range(n):
    mn = min(B[i], A[i])
    killed += mn
    B[i] -= mn
    mn = min(B[i], A[i+1])
    killed += mn

print(killed)

Submission status: Wrong Answer

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

for i in range(N):
    C[i] = max(C[i]-A[i],0)
    C[i] = max(C[i]-A[i+1],0)

for i in range(N):
    output += B[i]-C[i]

print(output)

Submission status: Wrong Answer

Solution code:
a = int(input())
b = list(map(int, input().split()))
c = list(map(int, input().split()))
d = 0
for i in range(a-1, -1, -1):
    d += min(b[i+1], c[i])
    c[i] = c[i] - min(b[i+1], c[i])

    d += min(b[i], c[i])
    c[i] = c[i] - min(b[i], c[i])
print(d)

Submission status: Wrong Answer

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


print(total)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(100000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *

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

Submission status: Wrong Answer

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

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    n = int(input())
    a = [int(s) for s in input().split()]
    b = [int(s) for s in input().split()]
    p = 0
    ans = 0
    for x, y in zip(a, b):
        y += p
        if x < y:
            p = y - x
            ans += x
        else:
            p = 0
            ans += y
    ans += min(a[-1], p)
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000)

def getN():
    return int(input())
def getList():
    return list(map(int, input().split()))
import math

n =getN()

anums = getList()
bnums = getList()

ans = 0
tmp = 0

for a, b in zip(anums, bnums):
    mx = b + tmp
    ans += min(mx, a)
    tmp = max(mx- a, 0)
    # print(ans, mx, tmp)

ans += min(tmp, anums[-1])

print(ans)

Submission status: Wrong Answer

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

    an_ = min(b, A[i+1])
    A[i] -= an_
    ans += an_
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
#2
a = [int(i) for i in input().split()]
#3 5 2
b = [int(i) for i in input().split()]
#4 5

sm = sum(b)

for i in range(n):
	c = a.copy()
	#print(a,b)
	#ans += min(c[i],b[i])
	a[i] -= min(c[i],b[i])
	b[i] -= min(c[i],b[i])
	#print(a,b)
	#ans += min(c[i+1],b[i])
	a[i+1] -= min(c[i+1],b[i])
	b[i] -= min(c[i+1],b[i])
	
print(sm-sum(b))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
AS = list(map(int, input().split()))
BS = list(map(int, input().split()))

monsters = 0

for i in range(N):
    while BS[i] > 0 and (AS[i] > 0 or AS[i + 1] > 0):
        if i == 0:
            if AS[i] > 0:
                AS[i] -= 1
                monsters += 1
            elif AS[i + 1] > 0:
                AS[i + 1] -= 1
                monsters += 1
            else:
                break
            BS[i] -= 1
        elif i == N - 1:
            if AS[i + 1] > 0:
                AS[i + 1] -= 1
                monsters += 1
            elif AS[i] > 0:
                AS[i] -= 1
                monsters += 1
            else:
                break
            BS[i] -= 1
        else:
            if AS[i] > 0:
                AS[i] -= 1
                monsters += 1
            elif AS[i + 1] > 0:
                AS[i + 1] -= 1
                monsters += 1
            else:
                break
            BS[i] -= 1

print(monsters)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
AS = list(map(int, input().split()))
BS = list(map(int, input().split()))

monsters = 0

for i in range(N):
    while BS[i] > 0:
        if i == 0:
            if AS[i] > 0:
                AS[i] -= 1
                monsters += 1
            elif AS[i + 1] > 0:
                AS[i + 1] -= 1
                monsters += 1
            else:
                break
            BS[i] -= 1
        elif i == N - 1:
            if AS[i + 1] > 0:
                AS[i + 1] -= 1
                monsters += 1
            elif AS[i] > 0:
                AS[i] -= 1
                monsters += 1
            else:
                break
            BS[i] -= 1
        else:
            if AS[i] > 0:
                AS[i] -= 1
                monsters += 1
            elif AS[i + 1] > 0:
                AS[i + 1] -= 1
                monsters += 1
            else:
                break
            BS[i] -= 1

print(monsters)

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
monster = list(map(int, input().split()))
yuusha = list(map(int, input().split()))
a = sum(monster)
for i in range(n):
    while yuusha[i] != 0:
        if monster[i] > 0:
            yuusha[i] -= 1
            monster[i] -= 1
        elif monster[i + 1] > 0:
            yuusha[i] -= 1
            monster[i + 1] -= 1
        else:
            break
a -= sum(monster)
print(a)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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] > 0:
        while a[i] > 0:
            if b[i] == 0:
                break
            a[i] -= 1
            b[i] -= 1
            cnt += 1
        while a[i+1] > 0:
            if b[i] == 0:
                break
            a[i+1] -= 1
            b[i] -= 1
            cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = [input() for i in range(2)]
A=list(map(int,S[0].split(" ")))
B=list(map(int,S[1].split(" ")))
mon_left=0
hero_left=0
counter=0
mon_killed=0

for i in range(N):
  if i==0:
    mon_sum=A[i]+A[i+1]
  else:
    mon_sum=(A[i]+A[i+1])-mon_killed
  hero = B[i]
    
  while hero>0 and mon_sum>0:
    counter+=1
    mon_sum -= 1
    hero -= 1
  if mon_sum > 0:
    mon_killed = 0
  elif mon_sum < 0:
    mon_killed = abs(mon_sum)

print(counter)

Submission status: Time Limit Exceeded

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

c = 0
for i in range(len(b)):
  # print('倒せる数', b)
  # print('倒せる街にいるモンスターの数',a)
  for j in range(b[i]):
    if a[i] > 0:
      b[i] -= 1
      a[i] -= 1
      c += 1
    elif a[i] == 0 and a[i+1] > 0:
      b[i] -= 1
      a[i+1] -= 1
      c += 1

print(c)

Submission status: Time Limit Exceeded

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

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

result = 0

for i in range(n):
    result += np.min([a[i]+a[i+1], b[i]])
    a[i+1] = np.min([np.max([0, a[i]+a[i+1] - b[i]]), a[i+1]])
print(result)

Submission status: Time Limit Exceeded

Solution code:
N = int( input() )
A, B = list( map(lambda x: int(x), input().split(" ")) ), list( map(lambda x: int(x), input().split(" ")) )
#A = monsters. B = number of to be able to kill monsters.殺せる数

 
def kill_monsters(Ai, Bi):
    while 0 < Ai and 0 < Bi:
        Ai -= 1
        Bi -= 1
    
    return (Ai, Bi)
 
 
def main(local_A, local_B, lim):
 
    for i in range(lim):
        local_A[i], local_B[i] = kill_monsters(local_A[i], local_B[i])
 
        if 0 < local_B[i]:
            local_A[i+1], local_B[i] = kill_monsters(local_A[i+1], local_B[i])
 
    return [local_A, local_B]

print( sum(A) - sum(main(A, B, N)[0]) )

Submission status: Time Limit Exceeded

Solution code:
# 勇者の数
n = int(input())
# モンスターの数 (n+1)
a = list(map(int, input().split()))
# i番目の勇者が倒せるモンスターの数
b = list(map(int, input().split()))

# 倒した数
kill_num = 0

for i in range(n):
    # 倒す対象のモンスターがいる街
    a_i=i
    # その勇者が倒せれる限界数
    while b[i] != 0:
        # もうa_i番目の街に倒せるモンスターがいない
        if a[a_i] == 0:
            # print("\n{}番目の街にはモンスターはいません\n".format(a_i))
            a_i += 1
        # 最後の街にモンスターがいない
        if a[-1]==0:
            break
        # モンスターを倒す
        a[a_i]-=1
        # 倒せるモンスター数
        b[i]-=1
        # リザルト
        kill_num+=1

        # print("{}番目の勇者が{}番目の街のモンスターを倒した. {}勇者の残り撃破可能数: {}. {}番目の街残り {}体".format(i, a_i, i, b[i], a_i, a[a_i]))

print(kill_num)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
t = input()
u = input()

a = []
for k in range(0,n+1,1):
  a.append(int(t.split(' ')[k]))
b = []
for k in range(0,n,1):
  b.append(int(u.split(' ')[k]))
kill = 0
for k in range(0,n,1):
  if a[k] < b[k]:
    kill += a[k]
    res = b[k] - a[k]
    if res >= a[k+1]:
      kill += a[k+1]
      a[k+1] = 0
    else:
      kill += res
      a[k+1] -= res
  else:
    kill += b[k]
print(kill)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import copy

N = int(input())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))

count = 0
while max(As) != 0 and max(Bs) != 0 :
    max_a = max(As)
    max_a_i = As.index(max_a)


    b1 = Bs[max_a_i - 1] if max_a_i > 0 else -1
    b2 = Bs[max_a_i] if (max_a_i <= N -1) else -1

    if max(b1, b2) == 0:
        # もう倒せない,reset
        As[max_a_i] = 0
        continue

    count += 1
    if b1 >= b2:
        b1 -= 1
        Bs[max_a_i - 1] = b1
    else:
        b2 -= 1
        Bs[max_a_i] = b2
    As[max_a_i] = max_a - 1


print(count)

Submission status: Time Limit Exceeded

Solution code:
# https://atcoder.jp/contests/abc135/tasks/abc135_c
import numpy as np


def calc(N: int, A: np.ndarray, B: np.ndarray) -> int:
    kill_count = 0
    remainings = A.copy()
    for i in np.arange(len(A) - 2, -1, -1):
        kill_i_plus_1 = np.min((remainings[i + 1], B[i]))
        killable = B[i] - kill_i_plus_1
        kill_i = np.min((remainings[i], killable))
        remainings[i + 1] -= kill_i_plus_1
        remainings[i] -= kill_i
        kill_count += kill_i_plus_1 + kill_i

    return kill_count


def load_input_as_int_array() -> np.ndarray:
    return np.array([int(v) for v in input().split(" ")])


def main() -> None:
    N = load_input_as_int_array()
    A = load_input_as_int_array()
    B = load_input_as_int_array()
    result = calc(N, A, B)
    print(result)


main()

Submission status: Time Limit Exceeded

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


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

ans += min(A[1],B[0])+min(A[0],max(B[0]-A[1],0))
print(ans)

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

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


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

Submission status: Time Limit Exceeded

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

cnt=0
for i in range(N+1):
  while B[i]!=0 and A[i]!=0:
    A[i]-=1
    B[i]-=1
    cnt+=1
  if i!=N and B[i]>0:
    B[i+1]=B[i+1]+B[i]
    
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
B = [int(y) for y in input().split()]
A_sum = sum(A)

for i in range(N):
    while (A[i+1] != 0 and B[i] != 0):
        if A[i] == 0:
            A[i+1] -= 1
        else:
            A[i] -= 1
        B[i] -= 1

A_after_sum = sum(A)
print(A_sum- A_after_sum)

Submission status: Time Limit Exceeded

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

for i in range(N):
    while A[i] != 0 and B[i] != 0:
        A[i] -= 1
        B[i] -= 1
        count += 1
    while A[i+1] != 0 and B[i] != 0:
        A[i+1] -= 1
        B[i] -= 1
        count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:

def main():
	N = int(input()) 
	A = [int(f) for f in input().split()]
	B = [int(f) for f in input().split()]
	
	m = 0
	for i in range(N):
		while A[i] > 0 and B[i] > 0:
			A[i] -= 1
			B[i] -= 1
			m += 1
		if B[i] > 0:
			while A[i+1] > 0 and B[i] > 0:
				A[i+1] -= 1
				B[i] -= 1
				m += 1
	
	print(m)

if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

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


count = 0
tmp = 0
for i, a in enumerate(A_list[::-1]):
  
  if i < len(B_list):  
    if a <= B_list[::-1][i] + tmp:
      count += a
      # tmp = B_list[::-1][i] + tmp - a      
      if tmp > a:
        tmp = B_list[::-1][i]
      else:
        tmp = B_list[::-1][i] + tmp - a
      
    else:
      count += B_list[::-1][i] + tmp
      tmp = 0
      
    # print(count, tmp)
      
  elif i == len(B_list):
    if a <= tmp:
      count += a
    else:
      count += tmp
    
print(count)

Submission status: Time Limit Exceeded

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

cnt = 0

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

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import math
import fractions
import copy
import numpy as np
def j(q):
    if q==1: print("YES")
    elif q == 0:print("NO")
    exit(0)
rem = pow(10,9)+7
"""
def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")
"""

def ip():
    return int(input())
def iprow():
    return [int(i) for i in input().split()]
def ips():
    return (int(i) for i in input().split())
def printrow(a):
    for i in a:
        print(i)
"""
#decomment when needed
def combinations(n,r):
    if n<r:return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def permutations(n,r):
    if n<r:return 0
    return math.factorial(n) // math.factorial(n - r)
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
"""
n = ip()
a  = iprow()
ans = sum(a)
b = iprow()
pl = 0
pr = n-1
if n==1:
    print(min(sum(b),sum(a)))
    exit(0)

while sum(b):
    if b[pl] - a[pl]>0:
        b[pl] -= a[pl]
        a[pl] = 0
    else:
        a[pl]-=b[pl]
        b[pl] = 0
    if b[pl]:
        if b[pl] - a[pl+1]>0:
            b[pl] = 0
            a[pl+1] = 0
        else:
            a[pl+1]-=b[pl]
            b[pl] = 0
    if b[pr] - a[pr+1] > 0:
        b[pr] -= a[pr + 1]
        a[pr+1] = 0
    else:
        a[pr+1] -= b[pr]
        b[pr] = 0
    if b[pr]:
        if b[pr] - a[pr] > 0:
            a[pr] = 0
            b[pr] = 0
        else:
            a[pr] -= b[pr]
            b[pr] = 0
    pl+=1
    pr-=1
print(ans-sum(a))

Submission status: Time Limit Exceeded

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

ans = 0
while(len(b)>0):
    if a[0] < b[0]:
        if a[0]+a[1] <= b[0]:
            ans += a[0]+a[1]
            a[1] = 0
        else:
            ans += b[0]
            a[1] -= b[0]-a[0]
    else:
        ans += b[0]
    # print(a,b,ans)

    del a[0]
    del b[0]
print(ans)

Submission status: Time Limit Exceeded

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)

Submission status: Accepted

Solution code:
#import math
#import itertools
#import numpy as np
#from collections import deque
# sys.setrecursionlimit(10 ** 6)
#mod = 10 ** 9 + 7
#INF = 10 ** 9
#PI = 3.14159265358979323846

INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))

def do():
    n=INT()
    A=LIST()
    B=LIST()
    ans=0
    ta=A[0]
    for i in range(n):
        tb=B[i]
        ans+=min(ta,tb)
        ta,tb=max(0,ta-tb),max(0,tb-ta)
        ta+=A[i+1]
        ans+=min(ta,tb)
        ta=max(0,ta-tb)
        ta=min(ta,A[i+1])
        #print(ans,ta,tb)
    print(ans)

if __name__=='__main__':
    do()