Getting TLE in PIGBANK spoj

this is my dp implementation for PIGBANK on spoj https://www.spoj.com/problems/PIGBANK/ .
i got TLE 10 times please help thanks in advance

my solution : https://ideone.com/eztC6b

//