# Arup Guha
# 6/10/2024
# Example showing how to read high scores from a file and update the file.

MAXSCORES = 10

def main():

    # Open the file.
    scores = open("highscores.txt", "r")

    # Get the number of scores in the file.
    n = int(scores.readline())

    # Will store the best scores here.
    best = []

    # Read each set of scores.
    for i in range(n):
        toks = scores.readline().split()

        # Put the score first, then the name.
        item = []
        item.append(int(toks[1]))
        item.append(toks[0])
        best.append(item)

    # Close it we don't need it.
    scores.close()

    newname = input("What is your name?\n")
    newscore = int(input("What was your score on the game?\n"))

    # Here is the new item to add to our score list.
    newitem = [newscore, newname]
    best.append(newitem)

    # This is the key, sort it!
    best.sort(reverse=True)

    # We'll write the scores back to the same place.
    newscores = open("highscores.txt", "w")

    # Number of scores we'll write to the file.
    cnt = min(MAXSCORES, len(best))

    # First line.
    newscores.write(str(cnt)+"\n")

    # Write each line.
    for i in range(cnt):
        newscores.write(best[i][1]+"\t"+str(best[i][0])+"\n")

    # We're done!
    newscores.close()

# Run it.
main()
                        

    

    

    
    
