Problem p02603 (python)

Fixed submission:
N = int(input())
price = list(map(int,input().split()))
money = 1000
stack = 0

state = None
for i in range(1,N):
    if state == None:
        if price[i] < price[i-1]:
            money += price[i-1]*stack
            stack = 0
            state = False
        elif price[i] > price[i-1]:
            stack = money//price[i-1]
            money = money%price[i-1]
            state = True
    elif state == True:
        if price[i] < price[i-1]:
            money += price[i-1]*stack
            stack = 0
            state = False
    elif state == False:
        if price[i] > price[i-1]:
            stack = money//price[i-1]
            money = money%price[i-1]
            state = True

if stack != 0:
    money += price[-1]*stack
    stack = 0

print(money)

Buggy submission (verdict: Runtime Error):
N = int(input())

Xaxis = [[] for _ in range(200001)]
Yaxis = [[] for _ in range(200001)]
sum = [[] for _ in range(400001)]
dif = [[] for _ in range(40001)]
value = []
ans = 10**8

for i in range(N):
    X,Y,P = input().split()
    X,Y = int(X),int(Y)
    value.append([X,Y,P])
    Xaxis[X].append(i)
    Yaxis[Y].append(i)
    sum[X+Y].append(i)
    dif[X-Y].append(i)

for i in Xaxis:
    l = len(i)
    if l >= 2:
        for j in range(l-1):
            for k in range(j+1,l):
                if (value[i[j]][2] == 'D' and value[i[k]][2] == 'U'):
                    if value[i[j]][1] >= value[i[k]][1]:
                        ans = min((value[i[j]][1]-value[i[k]][1])*5,ans)
                elif (value[i[j]][2] == 'U' and value[i[k]][2] == 'D'):
                    if value[i[j]][1] <= value[i[k]][1]:
                        ans = min((value[i[k]][1]-value[i[j]][1])*5,ans)

for i in Yaxis:
    l = len(i)
    if l >= 2:
        for j in range(l-1):
            for k in range(j+1,l):
                if (value[i[j]][2] == 'L' and value[i[k]][2] == 'R'):
                    if value[i[j]][0] >= value[i[k]][0]:
                        ans = min((value[i[j]][0]-value[i[k]][0])*5,ans)
                elif (value[i[j]][2] == 'R' and value[i[k]][2] == 'L'):
                    if value[i[j]][0] <= value[i[k]][0]:
                        ans = min((value[i[k]][0]-value[i[j]][0])*5,ans)

for i in sum:
    l = len(i)
    if l >= 2:
        for j in range(l-1):
            for k in range(j+1,l):
                if  (value[i[j]][2] == 'L' and value[i[k]][2] == 'D') or (value[i[j]][2] == 'U' and value[i[k]][2] == 'R'):
                    if value[i[j]][0] >= value[i[k]][0]:
                        ans = min((value[i[j]][0]-value[i[k]][0])*10,ans)
                elif (value[i[j]][2] == 'D' and value[i[k]][2] == 'L') or (value[i[j]][2] == 'R' and value[i[k]][2] == 'U'):
                    if value[i[j]][0] <= value[i[k]][0]:
                        ans = min((value[i[k]][0]-value[i[j]][0])*10,ans)

for i in dif:
    l = len(i)
    if l >= 2:
        for j in range(l-1):
            for k in range(j+1,l):
                if (value[i[j]][2] == 'L' and value[i[k]][2] == 'U') or (value[i[j]][2] == 'D' and value[i[k]][2] == 'R'):
                    if value[i[j]][0] >= value[i[k]][0]:
                        ans = min((value[i[j]][0]-value[i[k]][0])*10,ans)
                elif (value[i[j]][2] == 'U' and value[i[k]][2] == 'L') or (value[i[j]][2] == 'R' and value[i[k]][2] == 'D'):
                    if value[i[j]][0] <= value[i[k]][0]:
                        ans = min((value[i[k]][0]-value[i[j]][0])*10,ans)

if ans == 10**8:
    print('SAFE')
else:
    print(ans)