Codeforces Rd #366 (Div. 2) Problem C

I tried many combination of data structure but still got an time limit exceeded on test 7. At the end, I found that it’s about the way of printing.

What I did is instead of calling print every time in the for loop, I used a variable to store the answer and print it at once at the end.

# Time excceed
for i in range(n):
	print "some answer"

# Passed
ans = ""
for i in range(n):
	ans += "some answer" + '\n'
print(ans)

However, note that it is not always faster when I time it in my code (Please share your thought if you have any idea!) but it could be a trick to improve performance in Codeforces.