Painters problem

my code can not pass half of the testcases.