Magical Candy Bags

	public int maxCandies(int[] arr, int K) {
		PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(Comparator.reverseOrder());
		for (int i : arr)
			maxHeap.add(i);
		int maxCandies = 0;
		for (int i = 1; i <= K; i++) {
			int rem = maxHeap.remove();
			maxCandies += rem;
			maxHeap.add(rem / 2);
		}

		return maxCandies;
	}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s