Idk whats wrong
Https://ide.codingblocks.com/s/218464
https://ide.codingblocks.com/s/218514 Here is the corrected code.
Error:
Use long instead of int.
Your logic was correct and implementation too(Except the long instead of int). I have used a faster Input/Output format in java called the Reader class instead of the scanner class. The scanner class is very slow inputs as compared to Reader class.
I would say the fastest way but is not recommended since it requires very cumbersome methods in its implementation.
It uses inputDataStream to read through the stream of data and uses read() method and nextInt() methods for taking inputs. This is by far the fastest ways of taking input but is difficult to remember and is cumbersome in its approach.