Why in some problems I find myself in the choice of using C or C++ to solve the problem and accept it. for example this code in java https://mirror.codeforces.com/contest/433/submission/42722521 Time limit in the last test case test 46 which this test must take less time than which the code pass but when I use the same code in C++ https://mirror.codeforces.com/contest/433/submission/42722213 accepted in time ( 186 ms ) and test 46 took ( 15 ms )
try like this solution
also you can write faster Scanner
but why the code get time limit because of the Long not long
Check this Blog
Or try this.
Shuffle before sorting.