#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace Std;
const int MAXN = 1000010;
__int64 SUM[MAXN];
__int64 NUM[MAXN];
__int64 ANS[MAXN];
__int64 VIS[MAXN];
__int64 query;
__int64 N, Q;
BOOL Cmp1 (__int64 A, __int64 b)
{
return a > B;
}
int main ()
{
while (~SCANF ("%i64d", &n))
{
for (int i = 1;i <= n;i++)
scanf ("%i64d", &num[i]);
Sort (num+1, num+1+n, CMP1);
Sum[0] = 0;
for (int i = 1;i <= n; i++)
Sum[i] = Sum[i-1] + num[i];
memset (ans, 0, sizeof (ans));
for (int k = 1;k < MAXN; k++)
{
__int64 pos_s = 0;
__int64 pos_e = 1;
__int64 t = 0;
__int64 len = (__int64) k;
while (pos_s < n)
{
if (Pos_e > N) pos_e = n;
Ans[k] + = (Sum[pos_e]-sum[pos_s]) *t;
t++;
pos_s = Pos_e;
Pos_e + = Len;
Len *= (__int64) k;
}
}
scanf ("%i64d", &q);
for (int i = 1;i <= q;i++)
{
scanf ("%i64d", &query);
if (query > N) query = (__int64) n;
printf ("%i64d%c", ans[query], i = = q? ' \ n ': ');
}
}
return 0;
}
Codeforces Round #140 (Div. 1) Naughty Stone piles greedy