Solution Demonstrating Reading and Storing Input
# 1.3 https://open.kattis.com/problems/airfaregrants
# import sys to change standard input to read from file
import sys
# change the standard input to read from a file
sys.stdin = open("test.txt", "r")
# now use sys.stdin.readline() to read in a line of text
# convert to integer
n = int(sys.stdin.readline())
# this reads in each flight and stores it in a list
# see below for more efficient solution
flights = []
for _ in range(n):
flights.append(int(sys.stdin.readline()))
mx = max(flights)
mn = min(flights)
c = mn - (mx // 2)
if c <= 0:
print(0)
else:
print(c)
More Efficient Version (No List Storage)
# This solution finds the min and max while reading flights
# without storing them.
import sys
n = int(sys.stdin.readline())
mn = int(sys.stdin.readline())
mx = mn
for _ in range(n - 1):
x = int(sys.stdin.readline())
if x < mn:
mn = x
if x > mx:
mx = x
c = mn - (mx // 2)
if c <= 0:
print(0)
else:
print(c)