C++ solution with multiset


#1

int Solution::nchoc(int A, vector &B) {
multiset sorted;
for (int i : B)
sorted.insert(i);
int64_t res = 0;
while (A > 0 && !sorted.empty())
{
auto itr = sorted.end();
itr–;
int val = *itr;
res += val;
sorted.erase(itr);
val = val / 2;
if (val > 0)
sorted.insert(val);
–A;

	}

	res = res % int64_t(std::pow(10,9) + 7);
	return res;

}