• + 10 comments

    LOL the code's too complicated. It's a simple problem so the code should be simple too. Elegance is important in programming. There's also a lot of repetition in your code. Repeated blocks of code should be made into a function. This is the "do not repeat yourself" principle. Have a look at how small my python code is:

    from collections import deque
    
    def read_queue():
        return deque(map(int, input().strip().split()))
    
    nstacks = len(input().split())
    stacks = [read_queue() for i in range(nstacks)]
    heights = list(map(sum, stacks))
    
    while len(set(heights)) > 1:
        ihighest = heights.index(max(heights))
        heights[ihighest] -= stacks[ihighest].popleft()
    
    print(heights[0])
    

    Edit: made the code a bit more elegant.