Max_sum_k_parition , Getting wrong answer on one test case , may i know where i am going wrong

This is my code , and i couldn’t understand why it is failing 1 test case …

@aryan_007
Consider this testcase
6 5
5 -4 -3 -2 -1 10
Output should be “10” (no element from 1st block and 10 from second block) but your code is giving “9” as output (-1 from 1st block and 10 from second block).

1 Like

@aryan_007
Consider this testcase too.
6 1
5 4 3 2 1 10
Ans should be “18” (5+3+10 =18) but your code is giving “10”

1 Like

@aryan_007
There are few mistakes in your code

  1. what if size of your pq is 1 then your p_max2 doesn’t exist. Make a case when pq.size()==0 then p_max2={0,0}
  2. In your case you were taking atleast one element from each block which is not the required condition. So you know need to create priority_queue for each iteration. Also don’t pop any element.
  3. Your initial max_sum = -1 *10^18 not INT_MAX.

I have made changes in your code and it’s working fine for all testcase. Check it here

1 Like

Thank you so so much sir !! My problem is solved now :slightly_smiling_face:

@aryan_007
Please mark it as resolved. thanks

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.