RE in HackerEarth question (hackWithInfy Practice Test)

I have been trying to solve the problem

using this approach :

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

Please help,

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