What's wrong with this CODE (TWONMS)

Here is my code: https://ideone.com/Zami6q
The expected output is also produced and the limits of the variables is also satisfied. The output format is also correct. But I am exceeding the time limit, any help on how could I save time? Please help me with the code.
Thanks a lot!

Your codes complexity is O(n^2) and n can be upto 10^9. It won’t pass in 1 sec.

Don’t try to simulate what is asked in the question. Input constraints are huge for 1 sec limit. Try to see a pattern in different use cases or examples, and come up with something mathematical or logical.

1 Like

Could you help me with the code, because with LONG it throws Runtime Error.
Thanks!