Buggy python submission (verdict: Time Limit Exceeded):
#! python3
# maximum_profit.py

n = int(input())
R = [int(input()) for i in range(n)]

max_val = R[1] - R[0]
for i in range(n-1):
    for j in range(i+1, n):
        val = R[j] - R[i]
        if max_val < val:
            max_val = val

print(max_val)


Fixed submission:
#! python3
# maximum_profit.py

n = int(input())
R = [int(input()) for i in range(n)]

min_i = 0
max_val = R[1]-R[0]
max_j = -1
for i in range(n-1):
    if R[min_i] < R[i]:
        continue
    elif R[min_i] > R[i]:
        min_i = i

    if max_j <= i:
        max_j = i+1
        for j in range(i+1, n):
            if R[max_j] < R[j]:
                max_j = j

    if max_val < R[max_j] - R[min_i]:
        max_val = R[max_j] - R[min_i]

print(max_val)

Buggy python submission (verdict: Time Limit Exceeded):
#! python3
# maximum_profit.py

n = int(input())
R = [int(input()) for i in range(n)]

maximum_profit = R[-1]-R[-2]
for i in range(n-2):
    max_profit_i = R[i+1]-R[i]
    for j in range(i+2, n):
        tmp = R[j]-R[i]
        if max_profit_i < tmp:
            max_profit_i = tmp
    if maximum_profit < max_profit_i:
        maximum_profit = max_profit_i
print(maximum_profit)



Fixed submission:
#! python3
# maximum_profit.py

n = int(input())
R = [int(input()) for i in range(n)]

min_i = 0
max_val = R[1]-R[0]
max_j = -1
for i in range(n-1):
    if R[min_i] < R[i]:
        continue
    elif R[min_i] > R[i]:
        min_i = i

    if max_j <= i:
        max_j = i+1
        for j in range(i+1, n):
            if R[max_j] < R[j]:
                max_j = j

    if max_val < R[max_j] - R[min_i]:
        max_val = R[max_j] - R[min_i]

print(max_val)

Buggy python submission (verdict: Time Limit Exceeded):
#! python3
# maximum_profit.py

n = int(input())
R = [int(input()) for i in range(n)]

maximum_profit = R[-1]-R[-2]
for i in range(n-2):
    max_profit_i = R[i+1]-R[i]
    for j in range(i+2, n):
        tmp = R[j]-R[i]
        if max_profit_i < tmp:
            max_profit_i = tmp
    if maximum_profit < max_profit_i:
        maximum_profit = max_profit_i
print(maximum_profit)



Fixed submission:
#! python3
# maximum_profit.py

n = int(input())
R = [int(input()) for i in range(n)]

min_i = 0
max_val = R[1]-R[0]
max_j = -1
for i in range(n-1):
    if R[min_i] < R[i]:
        continue
    elif R[min_i] > R[i]:
        min_i = i

    if max_j <= i:
        max_j = i+1
        for j in range(i+1, n):
            if R[max_j] < R[j]:
                max_j = j

    if max_val < R[max_j] - R[min_i]:
        max_val = R[max_j] - R[min_i]

print(max_val)

Buggy python submission (verdict: Time Limit Exceeded):
#! python3
# maximum_profit.py

n = int(input())
R = [int(input()) for i in range(n)]

maximum_profit = R[-1]-R[-2]
min_i = R[0]
for i in range(n-2):
    if R[i] > min_i:
        continue
    else:
        min_i = R[i]
    max_profit_i = R[i+1]-R[i]
    for j in range(i+2, n):
        tmp = R[j]-R[i]
        if max_profit_i < tmp:
            max_profit_i = tmp
    if maximum_profit < max_profit_i:
        maximum_profit = max_profit_i
print(maximum_profit)



Fixed submission:
#! python3
# maximum_profit.py

n = int(input())
R = [int(input()) for i in range(n)]

min_i = 0
max_val = R[1]-R[0]
max_j = -1
for i in range(n-1):
    if R[min_i] < R[i]:
        continue
    elif R[min_i] > R[i]:
        min_i = i

    if max_j <= i:
        max_j = i+1
        for j in range(i+1, n):
            if R[max_j] < R[j]:
                max_j = j

    if max_val < R[max_j] - R[min_i]:
        max_val = R[max_j] - R[min_i]

print(max_val)