RE in HackerEarth question (hackWithInfy Practice Test)

I have been trying to solve the problem

using this approach :
https://www.hackerearth.com/submission/39268910/

The sample cases are running but it shows RE for other test cases.

Please help,
Thanks.

SIGSEGV this error is because of wrong memory access
m <= 1oooo not 100
use binary, for over greedy approach