Wrong answer error

I am facing wrong answers for some testcases.
I used the Binary Search Algo for this question.
Kindly have a look.

@mananaroramail try and dry run ur code for
8
5
8000 8565 8050 1234 1111
you are getting 1111
answer should be 8565

So I sorted the array first and the rest of my code is same but then too I am getting the same WRONG ANSWER ERROR as I submit.

@mananaroramail As you can see the constraints of the problem are that a painter can only paint contiguous boards. for eg. a painter can paint board no(1,2,3) but he can’t paint board no. (2,4,5) because its not contiguous. so the order of board given in input matters a lot. it means you can not reorder the boards and solve it.
Hint: Think about optimal substructure.