Question: There are n (1 <= n <= 10 ^ 5) Heap stones, each Heap has ai (1 <= ai <= 10 ^ 9) stones, the minimum cost of merging a pile of stones into another pile of B is.
However, it is not that simple. Now m (1 <= m <= 10 ^ 5) queries ki (1 <= ki <= 10 ^ 5 ), ask each heap of stones to be merged at most ki times, and find the minimum cost of merging all stones.
Problem: The data volume is obviously greedy and messy.
First, let's think about the situation where ki = 1. It's not hard to think that a pile of stones are merged once, and a pile of stones are merged once... A pile of stones are merged once. This is obviously the least stone to merge other stones n-1 times.
Considering ki = 2, a pile of stones are merged twice, two piles of stones are merged twice, and four piles of stones are merged twice... That is, each time x 2.
It is easy to understand that the number of heaps that can be merged increases each time, and the number of merged heaps also increases. So after sorting, you can be greedy from big to small.
It's actually re on _ int64, but it's still not fully considered. 10 ^ 5 won't exceed int, but 10 ^ 5*10 ^ 5 will be decisive. Alas, the competition is a pity...
PS: cf is purple. It is estimated that rt will be dropped...
Sure original, reprinted please indicate the source.
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <memory. h>
# Include <algorithm>
Using namespace std;
Const int maxn = 100002;
_ Int64 stone [maxn], sum [maxn], ans [maxn];
Int m, n, q;
Bool cmp (_ int64 a ,__ int64 B)
{
Return a> B;
}
Void read ()
{
Memset (ans,-1, sizeof (ans ));
For (int I = 0; I <n; I ++)
{
Scanf ("% I64d", & stone [I]);
}
Sort (stone, stone + n, cmp );
Sum [0] = 0LL;
Sum [1] = stone [1];
For (int I = 2; I <n; I ++)
{
Sum [I] = sum [I-1] + stone [I];
}
Return;
}
Void solve ()
{
Scanf ("% d", & m );
Bool flag = false;
While (m --)
{
If (flag) printf ("");
Scanf ("% d", & q );
If (ans [q]! =-1)
{
Printf ("% I64d", ans [q]);
Continue;
}
_ Int64 res = 0;
_ Int64 cnt = q, val = 1;
For (_ int64 I = 1; I <1LL * n ;)
{
Int top;
If (cnt + I-1> 1LL * (n-1) top = n-1;
Else top = (int) (cnt + i-1 );
Res + = (sum [top]-sum [I-1]) * val;
I + = cnt;
Val ++;
Cnt * = 1LL * q;
}
Ans [q] = res;
Printf ("% I64d", res );
Flag = true;
}
Puts ("");
Return;
}
Int main ()
{
While (~ Scanf ("% d", & n ))
{
Read ();
Solve ();
}
Return 0;
}