Any case where Chef and Pepperoni Pizza(PEPPERON) my solution fails

Problem - https://www.codechef.com/COOK109B/problems/PEPPERON/

While in contest, this was my first approach to solve this problem, but i don’t know where it fails.
https://www.codechef.com/viewsolution/26004700

Idea Overview:
1 - if(1st_half_sum>2nd_half_sum) then find the row with max value of (h2-h1) ,and reverse it and update answer.
2 - if(1st_half_sum<2nd_half_sum) then find the row with max value of (h1-h2) ,and reverse it and update answer.

Later during contest, i managed to write solution with other idea and it got accepted,but i want to know where this solution possibly fails. I know this code is too much implementation :\

Thanks in advance :slight_smile:

if(1st_half_sum>2nd_half_sum) then find the row with max value of (h1-h2) ,and reverse it and update answer.

By doing this you may end up increasing the difference to a more high value, think of it, what will happen when (h1-h2) is very large. So, for the former case you should simply find one such row where sum of the first half is smaller than the second half. Then consider reversing it(you don’t have to actually perform the reversal just do some additions and subtractions). Then check if doing that gets you a better answer.

My AC solution- https://www.codechef.com/viewsolution/26000760

1 Like

I actually did that, so to find min of our result.
I thought the row with max difference of(2ndhalf-1sthalf) will do it. No need to check for every row where 2ndhalfsum> 1sthalfsum, and yes i am not reversing it just doing some calculations.
So does this works?

Bro Can you look at my solution and tell me why i am getting WA.PleaseMy Solution. And also Explain your approach to solve this problem.