Powerful Discount Tickets(优先队列)
时间限制: 1 Sec 内存限制: 128 MB
题目描述
Takahashi is going to buy N items one by one.
The price of the i-th item he buys is Ai yen (the currency of Japan).
He has M discount tickets, and he can use any number of them when buying an item.
If Y tickets are used when buying an item priced X yen, he can get the item for X/2Y (rounded down to the nearest integer) yen.
What is the minimum amount of money required to buy all the items?
Constraints
·All values in input are integers.
·1≤N,M≤105
·1≤Ai≤109
输入
Input is given from Standard Input in the following format:
N M
A1 A2 ... AN
输出
Print the minimum amount of money required to buy all the items.
样例输入
【样例1】
3 3
2 13 8
【样例2】
4 4
1 9 3 5
【样例3】
1 100000
1000000000
【样例4】
10 1
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
样例输出
【样例1】
9
【样例2】
6
【样例3】
0
【样例4】
9500000000
提示
样例1解释
We can buy all the items for 9 yen, as follows:
Buy the 1-st item for 2 yen without tickets.
Buy the 2-nd item for 3 yen with 2 tickets.
Buy the 3-rd item for 4 yen with 1 ticket.
样例3解释
We can buy the item priced 1000000000 yen for 0 yen with 100000 tickets.
思路:找到价值最大的商品,然后把优惠券给它用,直到用完为止,附上AC代码
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
int main()
{
ll n = 0; ll m = 0;
priority_queue<ll> q;
cin >> n >> m;
vector<ll> price(n, 0);
for(ll i = 0; i < n; ++i)
{
cin >> price[i];
q.push(price[i]);
}
while(m--)
{
ll temp = q.top();
temp /= 2;
q.push(temp);
q.pop();
}
ll res = 0;
while(!q.empty())
{
res += q.top();
q.pop();
}
cout << res << endl;
return 0;
}