DPAIRS problem

in this problem i am getting tle for last 5 test case
this is my solution