Because it is too slow ...
My Python solution passes in approx. 1.5 s and is about 5 times faster than your code according to some random data I processed.
So much work for you to do.
You should generate some sample data and run your code on large input files (look at the constraints!). Then find the bottlenecks and then think about it, how to make them wider. Or use a faster language.