Hostel Visit Challenge

My code is working for the given sample case but none of the test case is passing and I’m unable to figure it out, why it’s not working. Code Link (https://ide.codingblocks.com/s/162726)

@farhan786
Use long long int instead of int for your code. Change all instances of int to long long int.

@tarunluthra tried that but still getting wrong answer for both test cases.

@farhan786
Try this testcase.
Input :
10 2
1 5 5
1 -5 -5
1 -5 5
1 5 -5
1 1 1
2
1 3 4
2
1 -3 2
2

Your Output :
50
50
50

Expected Output :
50
25
13

Yes my code is failing on this test case, but I’m unable to figure out why. Is there any problem in my approach ?

@farhan786
You should maintain the size of priority queue to be atmost K. The size of priority queue can increase easily if you keep getting larger elements. Deploy conditions to make sure that this never happens.

I’m maintaining the size of heap to be k, see on line no. 72 in my code, I’ve a hostelCount static variable in my Hostel class that keeps track of the hostels, still test cases are failing…

@farhan786
That is not the case. Your priority queue’s size is exceeding k and there is no condition to stop that.
Try printing the size of your priority queue at the end of every loop iteration.
Just put this statement :
cout << "Size = " << maxHeap.size() << endl;

Use the testcase I mentioned above and see how your PQ size easily exceeds the value of K.

@tarunluthra thanks a lot for helping me out to fix this, I was missing the case to control the heap size. Thank you :slight_smile: