Maximize Letter Beauty | CodeChef

CodeChef: Practical coding for everyone , I tried to solve this problem by the approach like given query Q x y, maximum suffix sum from x-1 to 0 , maximum prefix sum from y+1 to n and sum from x to y with the help of iterative segment trees , It seems to me that sum of i.e. suffix, prefix and range giving fine but still getting WA. Please anyone tell for which test cases my solution is wrong .
Here is my code , please if anyone could help , it took me 2 days approx . thanks in advance