Hostel Visit TLE in test case 4

static class HeapGeneric {

	ArrayList<Long> data = new ArrayList<>();

	public void add(long item) {
		data.add(item);
		upheapify(data.size() - 1);
	}

	private void upheapify(int ci) {
		int pi = (ci - 1) / 2;
		if (data.get(ci)<data.get(pi)) {
			swap(pi, ci);
			upheapify(pi);
		}

	}

	private void swap(int i, int j) {
		long ith = data.get(i);
		long jth = data.get(j);
		data.set(i, jth);
		data.set(j, ith);
	}

	public void display() {
		System.out.println(data);
	}

	public int size() {
		return this.data.size();
	}

	public boolean isEmpty() {
		return this.size() == 0;
	}

	public long remove() {
		swap(0, data.size() - 1);
		long rv = this.data.remove(this.data.size() - 1);
		downHeapify(0);
		return rv;
	}

	private void downHeapify(int pi) {
		int lci = 2 * pi + 1;
		int rci = 2 * pi + 2;
		int mini = pi;
		if (lci < this.data.size() && (data.get(lci)<data.get(mini))) {
			mini = lci;
		}
		if (rci < this.data.size() && (data.get(rci)<data.get(mini))) {
			mini = rci;
		}
		if (mini != pi) {
			swap(pi, mini);
			downHeapify(mini);
		}
	}

	public long nearest(long k) {
		ArrayList<Long> temp = new ArrayList<>();
		long result = 0;
		for (int i = 0; i < k; i++) {
			result = remove();
			temp.add(result);

		}
		for (int i = 0; i < k; i++) {
			add(temp.get(i));
		}
		return result;
	}

	// if t is having higher priority then return +ve value

// public int isLarger(long t, long o) {
// return t.compareTo(o);
// }

}

public static void main(String[] args) {
	HeapGeneric heap = new HeapGeneric();
	Scanner sc = new Scanner(System.in);
	long q = sc.nextLong();
	long k = sc.nextLong();
	int s = 0;
	while (q-- != 0) {
		Long t = sc.nextLong();
		if (t == 1) {
			long x = sc.nextLong();
			long y = sc.nextLong();
			Long rd = x * x + y * y;

// System.out.println(rd);
heap.add(rd);
s++;
} else if (t == 2 && s >= k) {
System.out.println(heap.nearest(k));
}
}
}