Bread
Bread
题解:哈夫曼树模型
该题目是给出根节点,求出其WPL,但是我们要仔细阅读,题目说面包是可以被剩余的,所以我们只要将剩余的面包当成子节点放进队列即可
#include <bits/stdc++.h>
#define Zeoy std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0)
#define all(x) (x).begin(), (x).end()
#define endl '\n'
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-9;
const int N = 2e5 + 10;
ll qpow(ll x, ll y)
{
ll ans = 1;
while (y)
{
if (y & 1)
ans *= x;
x *= x;
y >>= 1;
}
return ans;
}
priority_queue<ll, vector<ll>, greater<ll>> q;
int main(void)
{
Zeoy;
int t = 1;
// cin >> t;
while (t--)
{
ll n, l;
cin >> n >> l;
for (int i = 1; i <= n; ++i)
{
ll x;
cin >> x;
q.push(x);
l -= x;
}
if (l > 0) //将多余的面包当成子节点放入队列
q.push(l);
ll sum = 0L;
while (q.size() != 1)
{
ll t = 0;
t += q.top();
q.pop();
t += q.top();
q.pop();
q.push(t);
sum += t;
}
cout << sum << endl;
}
return 0;
}