Read once, print once
transcript
Your solution is correct. You submit it to a contest. Time limit exceeded.
The input holds a million numbers. One call per number took one point eight seconds. One read of everything took point one two seconds.
The loop itself was fast. The reading was slow. It was fifteen times slower than one read.
Read all the input in one call. Split it on whitespace, and take the count first. Then scan the values with a small state.
Printing has the same gap. A million prints, one per line, took point three seconds. One print of the joined lines took point one five.
Read everything at once. Never call a slow reader per number. Collect the answers in a list as you go. Join them and print once at the end.
Read the whole input in one call. Print the whole output in one call. Full breakdown in the lecture.