Problem p02603 (python)

Fixed submission:
N = int(input())
A = list(map(int,input().split( )))
def f(tail,head):
    if tail < head:
        return 1
    elif tail > head:
        return -1
    else:
        return 0

actions = ['stay' for _ in range(N)]

if N > 2:
    i = 0
    while f(A[i],A[i+1]) == 0:
        i += 1
        if i+1 == N-1:
            break
    if f(A[i],A[i+1]) == 1:
        actions[i] = 'buy'
    else:
        actions[i] = 'stay'
    before = f(A[i],A[i+1])
    for j,(tail,head) in enumerate(zip(A[i+1:],A[i+2:])):
        now = f(tail,head)
        if now == before:
            before = now
        elif now == 0:
            before = before
        else:
            if before == -1:
                actions[j+i+1] = 'buy'
            else:
                actions[j+i+1] = 'sell'
            before = now

else:
    if f(A[0],A[1]) == 1:
        actions[0] = 'buy'
actions[-1] = 'sell'

def action(stock,money,a,price):
    if a == 'stay':
        return stock,money
    elif a == 'buy':
        stock += money//price
        money = money%price
        return stock,money
    else:
        money += stock*price
        stock = 0
        return stock,money
    
stock = 0
money = 1000
for a,price in zip(actions,A):
    stock,money = action(stock,money,a,price)
print(money)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(map(int,input().split( )))
start = 0
def f(tail,head):
    if tail < head:
        return 1
    elif tail > head:
        return -1
    else:
        return 0
i = 0
actions = ['stay' for _ in range(N)]
while f(A[i],A[i+1]) == 0:
    i += 1
if f(A[i],A[i+1]) == 1:
    actions[i] = 'buy'
else:
    actions[i] = 'stay'
before = f(A[i],A[i+1])
for j,(tail,head) in enumerate(zip(A[i+1:],A[i+2:])):
    now = f(tail,head)
    if now == before:
        before = now
    elif now == 0:
        before = before
    else:
        if before == -1:
            actions[j+i+1] = 'buy'
        else:
            actions[j+i+1] = 'sell'
        before = now
actions[-1] = 'sell'

def action(stock,money,a,price):
    if a == 'stay':
        return stock,money
    elif a == 'buy':
        stock += money//price
        money = money%price
        return stock,money
    else:
        money += stock*price
        stock = 0
        return stock,money
    
stock = 0
money = 1000
for a,price in zip(actions,A):
    stock,money = action(stock,money,a,price)
print(money)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(map(int,input().split( )))
start = 0
def f(tail,head):
    if tail < head:
        return 1
    elif tail > head:
        return -1
    else:
        return 0
i = 0
actions = ['stay' for _ in range(N)]
while f(A[i],A[i+1]) == 0:
    i += 1
if f(A[i],A[i+1]) == 1:
    actions[i] = 'buy'
else:
    actions[i] = 'stay'
before = f(A[i],A[i+1])
for j,(tail,head) in enumerate(zip(A[i+1:],A[i+2:])):
    now = f(tail,head)
    if now == before:
        before = now
    elif now == 0:
        before = before
    else:
        if before == -1:
            actions[j+i+1] = 'buy'
        else:
            actions[j+i+1] = 'sell'
        before = now
actions[-1] = 'sell'

def action(stock,money,a,price):
    if a == 'stay':
        return stock,money
    elif a == 'buy':
        stock += money//price
        money = money%price
        return stock,money
    else:
        money += stock*price
        stock = 0
        return stock,money
    
stock = 0
money = 1000
for a,price in zip(actions,A):
    stock,money = action(stock,money,a,price)
print(money)