Hostel Visit-Priority queue

Can u please help me that why i am getting time limit exceeded and wrong answer in this question.
I have tried many testcases but answer coming right. So what’s the problem in this solution,

Please share your code at https://ide.codingblocks.com/.

1 Like

If it helps, here is my program that got accepted. I can’t understand c++ very well, but my guess is you took a min heap instead max.
import java.util.*;

public class Main {

    public static void main(String[] args) {

        PriorityQueue<Long> pq = new PriorityQueue<>(Collections.reverseOrder());

        Scanner scn = new Scanner(System.in);

        long q = scn.nextLong();

        long k = scn.nextLong();

        while(q!=0) {

            long choice = scn.nextLong();

            if(choice==1) {

                long x = scn.nextLong();

                long y = scn.nextLong();

                long rDistance = x*x+y*y;

                if(pq.size()<k) {

                    pq.add(rDistance);

                }

                else {

                    if(rDistance<pq.peek()) {

                        pq.remove();

                        pq.add(rDistance);

                    }

                }

            }

            else {

                System.out.println(pq.peek());

            }

            q--;

        }

    }

}