Hint needed for optimal solution

int main() {

priority_queue<long long int,vector<long long int>,greater<long long int>> s;
int t,k;
cin>>t>>k;
while(t--){
	long long int q,v1,v2;
	cin>>q;
	if(q==1){

		cin>>v1>>v2;
		long long int res = (v1*v1) + (v2*v2);
		s.push(res);

	} 
	if(q==2){
		
		stack <long long int> sta;
		int l = k-1;

		while(l--){
			sta.push(s.top());
			s.pop();
		}
		
		cout<<s.top()<<"\n";

		while(!sta.empty()){
			s.push(sta.top());
			sta.pop();
		}
		
	}

}
return 0;

}

This is my main code. I’m failing the fourth test case. Initially I did it using set and I passed only 3rd test case. Then I used priority queue, now I’m failing only 4rth test case. Hint / Help needed.

you are getting TLE in 4 case
because your code is not optimised
you are first popping the elements and then pushing back these elements
so time complexity will increase
instead you can use Max heap here like this

Reference Code

#include <bits/stdc++.h>
using namespace std;

#define int long long 

int32_t main () {
	int n, k; cin >> n >> k;
	priority_queue <int> pq;
	for (int i = 0; i < n; i++) {
		int qtype; cin >> qtype;
		if (qtype == 1) {
			int x, y; cin >> x >> y;
			pq.push(x*x + y*y);
		} else {
			while (pq.size() != k) {
				pq.pop();
			}
			cout << pq.top() << "\n";
		}
	}
	return 0;
}

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.