Need Help in one of code Monk Question

Sir, I am used to program in c++ but i also know python and java so please give me solution in one of these languages and please so explain how you have done it.

The editorial is available - https://www.hackerearth.com/problem/algorithm/treat-in-restaurent-c02fc904/