题解
每次选取最大的数字进行除2收益最大 使用优先队列存储选取最大值后再加入队列
k范围在1e9内 最多1e5个数字 每个值最多1e9所以最多进行nlogv次就能将数值全变为0 当队首为0时推出循环
AC代码
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
int main()
{
#ifdef LOCAL
freopen("C:/input.txt", "r", stdin);
#endif
int n, k, x;
cin >> n >> k;
priority_queue<int> pq;
for (int i = 0; i < n; i++)
{
scanf("%d", &x);
pq.push(x);
}
while (pq.top() != 0 && k) //队首为0继续无意义
{
int f = pq.top();
pq.pop();
f /= 2;
pq.push(f);
k--;
}
ll ans = 0;
while (!pq.empty())
ans += pq.top(), pq.pop();
cout << ans << endl;
return 0;
}