National High School Programming Contest Bangladesh 2016. National level, Senior Category Problem E.
If you are stuck on the problem, feel free to ask for help. If you have solved the problem, a hint by hint solution would be useful for others (sample).