给一个数组,一共有n个数。
你能进行最多 k次操作。每次操作可以进行以下步骤:
- 选择数组中的一个偶数 ai,将其变成 ai/2 。
现在你进行不超过k次操作后,让数组中所有数之和尽可能小。请输出这个最小的和。
思路:贪心即可,先把偶数的存下来,然后每次挑出最大的偶数/2,如果除了后还是偶数,放回去,不然就累加贡献,不放回去。每次拿最大的直接priority_que即可。
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+100;
typedef long long LL;
priority_queue<LL>que;
int main(void)
{
cin.tie(0);std::ios::sync_with_stdio(false);
LL n,k;cin>>n>>k;
LL sum=0;
for(LL i=1;i<=n;i++){
LL x;cin>>x;
if(x&1) sum+=x;
else que.push(x);
}
///debug(sum);
LL cnt=0;
while(!que.empty())
{
cnt++;
if(cnt>k) break;
LL t=que.top();que.pop();
t/=2;
if(t&1) sum+=t;
else que.push(t);
}
while(!que.empty()){
LL t=que.top();
/// debug(t);
sum+=t;
que.pop();
}
cout<<sum<<endl;
return 0;
}