Painter problem

my answer is correct but its not passes all the test cases.