#include<bits/stdc++.h>
#define rr register int
typedef long long ll;
using namespace std;
inline ll read(){
    char i=getchar();
    ll f=1,res=0;
    while(i<'0'||i>'9'){if(i=='-')f=-1;i=getchar();}
    while(i>='0'&&i<='9'){res=res*10+i-'0';i=getchar();}
    return res*f;
}
const int N=5e3+50;
int head[N],n,m,k=0,in[N];
struct zk{
    int v,next;
}ed[N<<1];
int dp[N][N][2],rt,son[N],tmp[N][2];//dp[u][子树(k个)节点黑色][0黑/1白]
//拓扑排序跑同时保证判环以及叶子节点开始更新
inline void adde(int u,int v){
    ed[++k].v=v;
    ed[k].next=head[u];
    head[u]=k;
}
inline void dfs(int u,int fa){
	if(in[u]==1){
		dp[u][1][0]=dp[u][0][1]=0;//同黑和同白归零
		son[u]=1;
		return;
	}
//	cout<<u<<"DSAD";
	son[u]=0;
	dp[u][0][0]=dp[u][0][1]=0;//自己不算子树归零
	for(rr i=head[u];~i;i=ed[i].next){
		int v=ed[i].v;
		if(v==fa)continue;
		dfs(v,u);
		memset(tmp,0x3f,sizeof tmp);
		for(rr s1=son[u];s1>=0;--s1)//显然的dp转移
		for(rr s2=son[v];s2>=0;--s2){
			tmp[s1+s2][0]=min(tmp[s1+s2][0],dp[u][s1][0]+min(dp[v][s2][0],dp[v][s2][1]+1));
			tmp[s1+s2][1]=min(tmp[s1+s2][1],dp[u][s1][1]+min(dp[v][s2][0]+1,dp[v][s2][1]));
		}
		son[u]+=son[v];
		for(rr j=0;j<=son[u];++j){
			dp[u][j][0]=tmp[j][0];dp[u][j][1]=tmp[j][1];
		}
	}
}
int main(){
	n=read();
	int u,v;
	memset(head,-1,sizeof head);
	for(rr i=1;i<n;++i){
		u=read();v=read();
		adde(u,v);
		adde(v,u);
		++in[u],++in[v];
	}//找到一个入度不为1的点作为根节点
	rt=1;
	while(rt<n&&in[rt]==1)++rt;
	memset(dp,0x3f,sizeof dp);
	dfs(rt,0);
//	int k=(n-1)>>1;
    int k=son[rt]>>1;
	int ans=min(dp[rt][k][0],dp[rt][k][1]);
	printf("%d",ans);
}


/**
 * Codeforces
 * Problem#581F
 * Accepted
 * Time: 139ms
 * Memory: 198380k
 */
#include <bits/stdc++.h>
using namespace std;
typedef bool boolean;
#define smin(_a, _b) (_a = min(_a, _b))

const int N = 5005;

int n;
vector<int> *g;
// 0: black, 1: white
int f[N][N][2];    // node, number of the black nodes, the color of this node
int temp[N][2];
int root;
int clf[N];
int deg[N];

inline void init() {
    scanf("%d", &n);
    g = new vector<int>[(n + 1)];
    for(int i = 1, u, v; i < n; i++) {
        scanf("%d%d", &u, &v);
        g[u].push_back(v);
        g[v].push_back(u);
        deg[u]++, deg[v]++;
    }
    for(root = 1; root < n && deg[root] == 1; root++);
}

void treedp(int node, int fa) {
    if(deg[node] == 1) {
        f[node][1][0] = f[node][0][1] = 0;
        clf[node] = 1;
        return;
    }
//    memset(temp)
    clf[node] = 0;
    f[node][0][0] = f[node][0][1] = 0;
    for (int i = 0; i < deg[node]; i++) {
        int e = g[node][i];
        if (e == fa)    continue;
        treedp(e, node);
        memset(temp, 0x3f, sizeof(temp));
        for (int s1 = clf[node]; ~s1; s1--) {
            for (int s2 = clf[e]; ~s2; s2--) {
                smin(temp[s1 + s2][0], f[node][s1][0] + min(f[e][s2][0], f[e][s2][1] + 1));
                smin(temp[s1 + s2][1], f[node][s1][1] + min(f[e][s2][0] + 1, f[e][s2][1]));
            }
        }
        clf[node] += clf[e];
        for (int j = 0; j <= clf[node]; j++)
            f[node][j][0] = temp[j][0], f[node][j][1] = temp[j][1];
    }
}

inline void solve() {
    memset(f, 0x3f, sizeof(f));
    treedp(root, 0);
    int k = (n-1) >> 1;
    int ans = min(f[root][k][0], f[root][k][1]);
    printf("%d\n", ans);
}

int main() {
    init();
    solve();
    return 0;
}


/**
 * Codeforces
 * Problem#581F
 * Accepted
 * Time: 139ms
 * Memory: 198380k
 */
#include <bits/stdc++.h>
using namespace std;
typedef bool boolean;
#define smin(_a, _b) (_a = min(_a, _b))

const int N = 5005;

int n;
vector<int> *g;
// 0: black, 1: white
int f[N][N][2];    // node, number of the black nodes, the color of this node
int temp[N][2];
int root;
int clf[N];
int deg[N];

inline void init() {
    scanf("%d", &n);
    g = new vector<int>[(n + 1)];
    for(int i = 1, u, v; i < n; i++) {
        scanf("%d%d", &u, &v);
        g[u].push_back(v);
        g[v].push_back(u);
        deg[u]++, deg[v]++;
    }
    for(root = 1; root < n && deg[root] == 1; root++);
}

void treedp(int node, int fa) {
    if(deg[node] == 1) {
        f[node][1][0] = f[node][0][1] = 0;
        clf[node] = 1;
        return;
    }
//    memset(temp)
    clf[node] = 0;
    f[node][1][0] = f[node][0][1] = 0;
    for (int i = 0; i < deg[node]; i++) {
        int e = g[node][i];
        if (e == fa)    continue;
        treedp(e, node);
        memset(temp, 0x3f, sizeof(temp));
        for (int s1 = clf[node]; ~s1; s1--) {
            for (int s2 = clf[e]; ~s2; s2--) {
                smin(temp[s1 + s2][0], f[node][s1][0] + min(f[e][s2][0], f[e][s2][1] + 1));
                smin(temp[s1 + s2][1], f[node][s1][1] + min(f[e][s2][0] + 1, f[e][s2][1]));
            }
        }
        clf[node] += clf[e];
        for (int j = 0; j <= clf[node]; j++)
            f[node][j][0] = temp[j][0], f[node][j][1] = temp[j][1];
    }
}

inline void solve() {
    memset(f, 0x3f, sizeof(f));
    treedp(root, 0);
    int k = (n-1) >> 1;
    int ans = min(f[root][k][0], f[root][k][1]);
    printf("%d\n", ans);
}

int main() {
    init();
    solve();
    return 0;
}


#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>

using namespace std;
using namespace __gnu_pbds;

typedef long long LL;

typedef tree<
    int,
    null_type,
    less<int>,
    rb_tree_tag,
    tree_order_statistics_node_update>
ordered_set;
//find_by_order
//order_of_key

#define FO(i,a,b) for (int i = (a); i < (b); i++)

#define PB push_back
#define FRO freopen("in.txt","r",stdin);

#define CLR(arr) memset( (arr),0,sizeof(arr) );
#define NEG(arr) memset( (arr),-1,sizeof(arr) );
#define ALL(v) v.begin(),v.end()

#define X first
#define Y second
#define MP make_pair

typedef pair<int,int> pint;
typedef map<int,int> mint;

void show() {cout<<'\n';}
template<typename T,typename... Args>
void show(T a, Args... args) { cout<<a<<" "; show(args...); }
template<typename T>
void show_c(T& a) { for ( auto &x:a ){ cout<<x<<" ";}cout<<endl;  }

#define SIZE 5005

vector<int> out[SIZE];

int parent[SIZE];
int nxt[SIZE];
int subtree[SIZE];
bool leaf[SIZE];

void dfs( int x,int p ){
    parent[x] = p;
    leaf[x]=true;
    for ( auto&y:out[x] ){
        if ( y == p )continue;
        dfs( y,x );
        leaf[x] = false;
    }
    subtree[x] = 0;
    if ( leaf[x] )subtree[x]++;
    for ( auto&y:out[x] ){
        if ( y == p )continue;
        subtree[x] += subtree[y];
    }
}

int dp[SIZE][SIZE/2][2];

int func( int x,int have,int pc ){

    int &ret = dp[x][have][pc];
    if ( ret != -1 )return ret;

    ret = (1<<29);
    if(have > subtree[x] && nxt[x] == -1) return ret;

    if ( leaf[x] ){
        for (int c =0;c<2;++c){
            if ( have-c >=0 ){
                int tmp = (c!=pc);
                if ( nxt[x] != -1 ){
                    tmp += func( nxt[x],have-c,pc );
                }else{
                    if ( have != c ){
                        tmp = (1<<29);
                    }
                }
                ret = min(ret ,tmp );
            }
        }
        return ret;
    }

    for (int i=(nxt[x] == -1)?have:0;i<=have ;++i){
        for (int c=0 ; c<2;++c){
            int tmp = ( c!=pc )+func( out[x][0] ,i,c );
            if ( nxt[x] != -1 )tmp+= func( nxt[x],have-i,pc );
            ret = min( ret , tmp );
        }
    }
    return ret;
}

int main(){

    int n;
    scanf("%d",&n);
    int u,v;
    for (int i=1;i<n;++i){
        scanf("%d %d",&u,&v);
        out[u].PB( v );
        out[v].PB( u );
    }

    if ( n == 2 ){
        printf("1\n");
        return 0;
    }

    int st = 1;
    int cnt = 0;
    for (int i=1;i<=n;++i){
        if ( out[i].size() > 1 ){
            st = i;
        }else{
            cnt++;
        }
    }

    dfs( st,0 );

    NEG(nxt);
    for (int i=1;i<=n;++i){
        for ( int j=0;j<out[i].size();++j ){
            if ( out[i][j] == parent[i] ){
                swap( out[i][j] , out[i][ out[i].size()-1 ] );
                break;
            }
        }
        if ( out[i].back() == parent[i] ){
            out[i].pop_back();
        }
        for (int j=0;j+1<out[i].size();++j){
            nxt[ out[i][j] ] = out[i][j+1];
        }
    }

    NEG(dp);
    printf("%d\n",min( func( st,cnt/2,0 ), func( st,cnt/2,1 ) ) );

    return 0;
}


#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>

using namespace std;
using namespace __gnu_pbds;

typedef long long LL;

typedef tree<
    int,
    null_type,
    less<int>,
    rb_tree_tag,
    tree_order_statistics_node_update>
ordered_set;
//find_by_order
//order_of_key

#define FO(i,a,b) for (int i = (a); i < (b); i++)

#define PB push_back
#define FRO freopen("in.txt","r",stdin);

#define CLR(arr) memset( (arr),0,sizeof(arr) );
#define NEG(arr) memset( (arr),-1,sizeof(arr) );
#define ALL(v) v.begin(),v.end()

#define X first
#define Y second
#define MP make_pair

typedef pair<int,int> pint;
typedef map<int,int> mint;

void show() {cout<<'\n';}
template<typename T,typename... Args>
void show(T a, Args... args) { cout<<a<<" "; show(args...); }
template<typename T>
void show_c(T& a) { for ( auto &x:a ){ cout<<x<<" ";}cout<<endl;  }

#define SIZE 5005

vector<int> out[SIZE];

int parent[SIZE];
int nxt[SIZE];
int subtree[SIZE];
bool leaf[SIZE];

void dfs( int x,int p ){
    parent[x] = p;
    leaf[x]=true;
    for ( auto&y:out[x] ){
        if ( y == p )continue;
        dfs( y,x );
        leaf[x] = false;
    }
    subtree[x] = 0;
    if ( leaf[x] )subtree[x]++;
    for ( auto&y:out[x] ){
        if ( y == p )continue;
        subtree[x] += subtree[y];
    }
}

int dp[SIZE][SIZE/2][2];

int func( int x,int have,int pc ){

    int &ret = dp[x][have][pc];
    if ( ret != -1 )return ret;

    ret = (1<<29);
    if(have > subtree[x] && nxt[x] == -1) return ret;

    if ( leaf[x] ){
        for (int c =0;c<2;++c){
            if ( have-c >=0 ){
                int tmp = (c!=pc);
                if ( nxt[x] != -1 ){
                    tmp += func( nxt[x],have-c,pc );
                }else{
                    if ( have != c ){
                        tmp = (1<<29);
                    }
                }
                ret = min(ret ,tmp );
            }
        }
        return ret;
    }

    for (int i=(nxt[x] == -1)?have:0;i<=have && i<=subtree[x] ;++i){
        for (int c=0 ; c<2;++c){
            int tmp = ( c!=pc )+func( out[x][0] ,i,c );
            if ( nxt[x] != -1 )tmp+= func( nxt[x],have-i,pc );
            ret = min( ret , tmp );
        }
    }
    return ret;
}

int main(){

    int n;
    scanf("%d",&n);
    int u,v;
    for (int i=1;i<n;++i){
        scanf("%d %d",&u,&v);
        out[u].PB( v );
        out[v].PB( u );
    }

    if ( n == 2 ){
        printf("1\n");
        return 0;
    }

    int st = 1;
    int cnt = 0;
    for (int i=1;i<=n;++i){
        if ( out[i].size() > 1 ){
            st = i;
        }else{
            cnt++;
        }
    }

    dfs( st,0 );

    NEG(nxt);
    for (int i=1;i<=n;++i){
        for ( int j=0;j<out[i].size();++j ){
            if ( out[i][j] == parent[i] ){
                swap( out[i][j] , out[i][ out[i].size()-1 ] );
                break;
            }
        }
        if ( out[i].back() == parent[i] ){
            out[i].pop_back();
        }
        for (int j=0;j+1<out[i].size();++j){
            nxt[ out[i][j] ] = out[i][j+1];
        }
    }

    NEG(dp);
    printf("%d\n",min( func( st,cnt/2,0 ), func( st,cnt/2,1 ) ) );

    return 0;
}


#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>

using namespace std;
using namespace __gnu_pbds;

typedef long long LL;

typedef tree<
    int,
    null_type,
    less<int>,
    rb_tree_tag,
    tree_order_statistics_node_update>
ordered_set;
//find_by_order
//order_of_key

#define FO(i,a,b) for (int i = (a); i < (b); i++)

#define PB push_back
#define FRO freopen("in.txt","r",stdin);

#define CLR(arr) memset( (arr),0,sizeof(arr) );
#define NEG(arr) memset( (arr),-1,sizeof(arr) );
#define ALL(v) v.begin(),v.end()

#define X first
#define Y second
#define MP make_pair

typedef pair<int,int> pint;
typedef map<int,int> mint;

void show() {cout<<'\n';}
template<typename T,typename... Args>
void show(T a, Args... args) { cout<<a<<" "; show(args...); }
template<typename T>
void show_c(T& a) { for ( auto &x:a ){ cout<<x<<" ";}cout<<endl;  }

#define SIZE 5005

vector<int> out[SIZE];

int parent[SIZE];
int nxt[SIZE];
int subtree[SIZE];
bool leaf[SIZE];

void dfs( int x,int p ){
    parent[x] = p;
    leaf[x]=true;
    for ( auto&y:out[x] ){
        if ( y == p )continue;
        dfs( y,x );
        leaf[x] = false;
    }
    subtree[x] = 0;
    if ( leaf[x] )subtree[x]++;
    for ( auto&y:out[x] ){
        if ( y == p )continue;
        subtree[x] += subtree[y];
    }
}

int dp[SIZE][SIZE/2][2];

int func( int x,int have,int pc ){

    int &ret = dp[x][have][pc];
    if ( ret != -1 )return ret;

    ret = (1<<29);

    if ( leaf[x] ){
        for (int c =0;c<2;++c){
            if ( have-c >=0 ){
                int tmp = (c!=pc);
                if ( nxt[x] != -1 ){
                    tmp += func( nxt[x],have-c,pc );
                }else{
                    if ( have != c ){
                        tmp = (1<<29);
                    }
                }
                ret = min(ret ,tmp );
            }
        }
        return ret;
    }

    for (int i=(nxt[x] == -1)?have:0;i<=have ;++i){
        for (int c=0 ; c<2;++c){
            int tmp = ( c!=pc )+func( out[x][0] ,i,c );
            if ( nxt[x] != -1 )tmp+= func( nxt[x],have-i,pc );
            ret = min( ret , tmp );
        }
    }
    return ret;
}

int main(){

    int n;
    scanf("%d",&n);
    int u,v;
    for (int i=1;i<n;++i){
        scanf("%d %d",&u,&v);
        out[u].PB( v );
        out[v].PB( u );
    }

    if ( n == 2 ){
        printf("1\n");
        return 0;
    }

    int st = 1;
    int cnt = 0;
    for (int i=1;i<=n;++i){
        if ( out[i].size() > 1 ){
            st = i;
        }else{
            cnt++;
        }
    }

    dfs( st,0 );

    NEG(nxt);
    for (int i=1;i<=n;++i){
        for ( int j=0;j<out[i].size();++j ){
            if ( out[i][j] == parent[i] ){
                swap( out[i][j] , out[i][ out[i].size()-1 ] );
                break;
            }
        }
        if ( out[i].back() == parent[i] ){
            out[i].pop_back();
        }
        for (int j=0;j+1<out[i].size();++j){
            nxt[ out[i][j] ] = out[i][j+1];
        }
    }

    NEG(dp);
    printf("%d\n",min( func( st,cnt/2,0 ), func( st,cnt/2,1 ) ) );

    return 0;
}


#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>

using namespace std;
using namespace __gnu_pbds;

typedef long long LL;

typedef tree<
    int,
    null_type,
    less<int>,
    rb_tree_tag,
    tree_order_statistics_node_update>
ordered_set;
//find_by_order
//order_of_key

#define FO(i,a,b) for (int i = (a); i < (b); i++)

#define PB push_back
#define FRO freopen("in.txt","r",stdin);

#define CLR(arr) memset( (arr),0,sizeof(arr) );
#define NEG(arr) memset( (arr),-1,sizeof(arr) );
#define ALL(v) v.begin(),v.end()

#define X first
#define Y second
#define MP make_pair

typedef pair<int,int> pint;
typedef map<int,int> mint;

void show() {cout<<'\n';}
template<typename T,typename... Args>
void show(T a, Args... args) { cout<<a<<" "; show(args...); }
template<typename T>
void show_c(T& a) { for ( auto &x:a ){ cout<<x<<" ";}cout<<endl;  }

#define SIZE 5005

vector<int> dp[SIZE][SIZE/2][2];
vector<int>out[SIZE];

int subtree[SIZE];
int parent[SIZE];

int func( int x,int child,int have,int pc ){

    if ( child == out[x].size() ){
        if ( have != 0 )return (1<<29);
        else return 0;
    }

    int &ret = dp[x][have][pc][child];
    if ( ret != -1 )return ret;

    ret = (1<<29);

    int y = out[x][child];

    if ( y == parent[x] ){
        return ret = func( x,child+1,have,pc );
    }

    if ( out [y].size() == 1 ){
        ret = (pc==1)+func( x,child+1,have,pc );
        if ( have-1>=0 )
            ret = min( ret , (pc==0)+func( x,child+1,have-1,pc ) );
        return ret;
    }

    for (int i=0;i<=subtree[y] && i<=have ;++i){
        ret = min( ret , ( pc == 1 ) + func( y,0,i,0 ) + func( x,child+1,have-i , pc) );
        ret = min( ret , ( pc == 0 ) + func( y,0,i,1 ) + func( x,child+1,have-i , pc) );
    }
    return ret;
}

void dfs(int x,int p){

    subtree[x] = 0;
    parent[x] = p;
    if ( out[x].size() == 1 ){
        subtree[x] = 1;
        return ;
    }

    for ( auto&y:out[x] ){
        if ( y == p )continue;
        dfs( y,x );
        subtree[x] += subtree[y];
    }
}

int main(){

    int n;
    scanf("%d",&n);
    int u,v;
    for (int i=1;i<n;++i){
        scanf("%d %d",&u,&v);
        out[u].PB( v );
        out[v].PB( u );
    }

    if ( n == 2 ){
        printf("1\n");
        return 0;
    }

    int leaf = 0;
    int st = -1;
    for (int i=1;i<=n;++i){
        if ( out[i].size() == 1 ){
            leaf++;
        }else{
            st = i;
        }
    }

    for (int i=1;i<=n;++i){
        for (int j=0;j<=leaf;++j){
            dp[i][j][0].resize( out[i].size() );

            for (auto&x:dp[i][j][0]){
                x = -1;
            }

            dp[i][j][1].resize( out[i].size() );

            for (auto&x:dp[i][j][1]){
                x = -1;
            }

        }
    }

    dfs(st,0);

    printf("%d\n",min( func( st,0,leaf/2,0 ) , func( st,0,leaf/2,1 )  ) );

    return 0;
}


#include <iostream>
#include <vector>

using namespace std;

#define N 5555
#define pb push_back
#define mp make_pair

const int inf = 1e9;

int n;
int dp[N][N][2];
vector<int> g[N];

int leaves[N];

void dfs(int v, int p) {
	//cout << v << '\n';
	if(g[v].size () == 1) {
		dp[v][0][1] = 0;
		dp[v][1][0] = 0;
		dp[v][1][1] = inf;
		dp[v][0][0] = inf;
	}
	else {
		int d[2][N][2], mxcnt = 0;
		for(int i = 0; i <= n; ++i) d[0][i][0] = d[0][i][1] = inf;
		d[0][0][0] = d[0][0][1] = 0;
		int id = 0;
		for(int j = 0; j < g[v].size(); ++j) {
			int to = g[v][j];
			if(to == p) continue;
			dfs(to, v);
			//int id = j & 1;
			for(int j = 0; j <= mxcnt + leaves[to]; ++j) {
				d[id ^ 1][j][0] = d[id ^ 1][j][1] = inf;
			}

			for(int k = leaves[to]; k >= 0; --k) {
				for(int l = mxcnt; l >= 0; --l) {
					d[id ^ 1][l + k][0] = min(d[id ^ 1][l + k][0], d[id][l][0] + dp[to][k][0]);
					d[id ^ 1][l + k][0] = min(d[id ^ 1][l + k][0], d[id][l][0] + dp[to][k][1] + 1);

					d[id ^ 1][l + k][1] = min(d[id ^ 1][l + k][1], d[id][l][1] + dp[to][k][1]);
					d[id ^ 1][l + k][1] = min(d[id ^ 1][l + k][1], d[id][l][1] + dp[to][k][0] + 1);
				}
			}
			mxcnt += leaves[to];
			id ^= 1;
		}
		for(int i = 0; i <= mxcnt; ++i) {
			dp[v][i][0] = d[id][i][0];
			dp[v][i][1] = d[id][i][1];
		}
	}
}
void dfs0(int v, int p) {
	if(g[v].size() == 1) {
		leaves[v] = 1;
	}
	else {
		leaves[v] = 0;
		for(int i = 0; i < g[v].size(); ++i) {
			int to = g[v][i];
			if(to == p) continue;
			dfs0(to, v);
			leaves[v] += leaves[to];
		}
	}
}

int main () {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int v;
	cin >> n;
	if(n == 2) {
		cout<< 1;
		return 0;
	}
	int leafs = 0;
	for(int i = 1; i < n; ++i) {
		int x, y;
		cin >> x >> y;
		x--; y--;
		g[x].pb(y);
		g[y].pb(x);
	}
	for(int i = 0; i < n; ++i) {
		if(g[i].size() > 1) { v = i; break;}
	}
	for(int i = 0; i < n; ++i) {
		if(g[i].size() == 1) leafs++;
	}

	dfs0(v, -1);
	//return 0;
	//cout << v << ' ' << leaves[v] << endl;
	dfs(v, -1);
	leafs >>= 1;
	cout<<min(dp[v][leafs][0], dp[v][leafs][1]);

	return 0;
}


#include <iostream>
#include <vector>

using namespace std;

#define N 5555
#define pb push_back
#define mp make_pair

const int inf = 1e9;

int n;
int dp[N][N][2];
vector<int> g[N];

int leaves[N];

void dfs(int v, int p) {
	//cout << v << '\n';
	if(g[v].size () == 1) {
		dp[v][0][1] = 0;
		dp[v][1][0] = 0;
		dp[v][1][1] = inf;
		dp[v][0][0] = inf;
	}
	else {
		int d[2][N][2], mxcnt = 0;
		for(int i = 0; i <= n; ++i) d[0][i][0] = d[0][i][1] = inf;
		d[0][0][0] = d[0][0][1] = 0;
		for(int j = 0; j < g[v].size(); ++j) {
			int to = g[v][j];
			if(to == p) continue;
			dfs(to, v);
			int id = j & 1;
			for(int j = 0; j <= mxcnt + leaves[to]; ++j) {
				d[id ^ 1][j][0] = d[id ^ 1][j][1] = inf;
			}

			for(int k = leaves[to]; k >= 0; --k) {
				for(int l = mxcnt; l >= 0; --l) {
					d[id ^ 1][l + k][0] = min(d[id ^ 1][l + k][0], d[id][l][0] + dp[to][k][0]);
					d[id ^ 1][l + k][0] = min(d[id ^ 1][l + k][0], d[id][l][0] + dp[to][k][1] + 1);

					d[id ^ 1][l + k][1] = min(d[id ^ 1][l + k][1], d[id][l][1] + dp[to][k][1]);
					d[id ^ 1][l + k][1] = min(d[id ^ 1][l + k][1], d[id][l][1] + dp[to][k][0] + 1);
				}
			}
			mxcnt += leaves[to];
		}
		int id = g[v].size();
		id &= 1;
		for(int i = 0; i <= mxcnt; ++i) {
			dp[v][i][0] = d[id][i][0];
			dp[v][i][1] = d[id][i][1];
		}
	}
}
void dfs0(int v, int p) {
	if(g[v].size() == 1) {
		leaves[v] = 1;
	}
	else {
		leaves[v] = 0;
		for(int i = 0; i < g[v].size(); ++i) {
			int to = g[v][i];
			if(to == p) continue;
			dfs0(to, v);
			leaves[v] += leaves[to];
		}
	}
}

int main () {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int v;
	cin >> n;
	if(n == 2) {
		cout<< 1;
		return 0;
	}
	int leafs = 0;
	for(int i = 1; i < n; ++i) {
		int x, y;
		cin >> x >> y;
		x--; y--;
		g[x].pb(y);
		g[y].pb(x);
	}
	for(int i = 0; i < n; ++i) {
		if(g[i].size() > 1) { v = i; break;}
	}
	for(int i = 0; i < n; ++i) {
		if(g[i].size() == 1) leafs++;
	}

	dfs0(v, -1);
	//return 0;
	//cout << v << ' ' << leaves[v] << endl;
	dfs(v, -1);
	leafs >>= 1;
	cout<<min(dp[v][leafs][0], dp[v][leafs][1]);

	return 0;
}


#include <iostream>
#include <vector>

using namespace std;

#define N 5555
#define pb push_back
#define mp make_pair

const int inf = 1e9;

int n;
int dp[N][N][2];
vector<int> g[N];

int leaves[N];

void dfs(int v, int p) {
	//cout << v << '\n';
	if(g[v].size () == 1) {
		dp[v][0][1] = 0;
		dp[v][1][0] = 0;
		dp[v][1][1] = inf;
		dp[v][0][0] = inf;
	}
	else {
		int d[2][N][2], mxcnt = 0;
		for(int i = 0; i < n; ++i) d[0][i][0] = d[0][i][1] = inf;
		d[0][0][0] = d[0][0][1] = 0;
		for(int j = 0; j < g[v].size(); ++j) {
			int to = g[v][j];
			if(to == p) continue;
			dfs(to, v);
			int id = j & 1;
			for(int j = 0; j <= mxcnt + leaves[to]; ++j) {
				d[id ^ 1][j][0] = d[id ^ 1][j][1] = inf;
			}

			for(int k = leaves[to]; k >= 0; --k) {
				for(int l = mxcnt; l >= 0; --l) {
					d[id ^ 1][l + k][0] = min(d[id ^ 1][l + k][0], d[id][l][0] + dp[to][k][0]);
					d[id ^ 1][l + k][0] = min(d[id ^ 1][l + k][0], d[id][l][0] + dp[to][k][1] + 1);

					d[id ^ 1][l + k][1] = min(d[id ^ 1][l + k][1], d[id][l][1] + dp[to][k][1]);
					d[id ^ 1][l + k][1] = min(d[id ^ 1][l + k][1], d[id][l][1] + dp[to][k][0] + 1);
				}
			}
			mxcnt += leaves[to];
		}
		int id = g[v].size();
		id &= 1;
		for(int i = 0; i <= mxcnt; ++i) {
			dp[v][i][0] = d[id][i][0];
			dp[v][i][1] = d[id][i][1];
		}
	}
}
void dfs0(int v, int p) {
	if(g[v].size() == 1) {
		leaves[v] = 1;
	}
	else {
		leaves[v] = 0;
		for(int i = 0; i < g[v].size(); ++i) {
			int to = g[v][i];
			if(to == p) continue;
			dfs0(to, v);
			leaves[v] += leaves[to];
		}
	}
}

int main () {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int v;
	cin >> n;
	if(n == 2) {
		cout<< 1;
		return 0;
	}
	int leafs = 0;
	for(int i = 1; i < n; ++i) {
		int x, y;
		cin >> x >> y;
		x--; y--;
		g[x].pb(y);
		g[y].pb(x);
	}
	for(int i = 0; i < n; ++i) {
		if(g[i].size() > 1) { v = i; break;}
	}
	for(int i = 0; i < n; ++i) {
		if(g[i].size() == 1) leafs++;
	}

	dfs0(v, -1);
	//return 0;
	//cout << v << ' ' << leaves[v] << endl;
	dfs(v, -1);
	leafs >>= 1;
	cout<<min(dp[v][leafs][0], dp[v][leafs][1]);

	return 0;
}


#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>

using namespace std;

const int MAXV = 5010, INF = 2*MAXV;

int dp[MAXV][MAXV/2][2], dp2[MAXV][MAXV/2][2];
int nxt[MAXV], leaf[MAXV], vis[MAXV], leafCnt, go[MAXV];
vector<int> adj[MAXV];

int min(int a, int b, int c) {
    return min(a, min(b, c));
}

int g(int, int, int);

int f(int v, int k, int c) {
    int &ret = dp[v][k][c];
    if(ret == -1) {
        if(!leaf[v]) {
            ret = g(go[v], k, c);
        } else {
            ret = INF;
            if(c && k == 1) ret = 0;
            else if(!c && k == 0) ret = 0;
        }
    }
    return ret;
}

int g(int v, int k, int c) {
    int &ret = dp2[v][k][c];
    if(ret == -1) {
        if(nxt[v] == -1) {
            ret = min(f(v, k, c), f(v, k, !c) + 1);
        }
        else {
            ret = INF;
            for(int i = 0; i <= k; i++)
                ret = min(ret, f(v, i, c) + g(nxt[v], k - i, c), f(v, i, !c) + 1 + g(nxt[v], k - i, c));
        }
    }
    return ret;
}

void dfs(int v) {
    vis[v] = 1;
    int last = -1;
    for(int u : adj[v]) {
        if(!vis[u]) {
            nxt[u] = last;
            last = u;
            dfs(u);
        }
    }
    if(last == -1) {
        leaf[v] = 1;
        leafCnt++;
    } else {
        go[v] = last;
    }
}

int main() {
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++) {
        vis[i] = 0;
        leaf[i] = 0;
    }
    for(int i = 0; i < n - 1; i++) {
        int a, b;
        scanf("%d%d", &a, &b);
        a--; b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    int root = -1;
    for(int i = 0; i < n; i++) {
        if(adj[i].size() > 1) {
            root = i;
            break;
        }
    }
    if(root == -1) {
        printf("1\n");
        return 0;
    }
    dfs(root);
    for(int i = 0; i < n; i++) {
        for(int j = 0; j <= n/2; j++) {
            for(int k = 0; k < 2; k++) {
                dp[i][j][k] = dp2[i][j][k] = -1;
            }
        }
    }
    // printf("Raiz %d\n", root);
    // for(int i = 0; i < n; i++) {
    //  printf("nxt[%d] = %d\n", i + 1, nxt[i] + 1);
    // }
    // for(int i = 0; i < n; i++) {
    //  printf("go[%d] = %d\n", i + 1, go[i] + 1);
    // }
    printf("%d\n", min(f(root, leafCnt/2, 0), f(root, leafCnt/2, 1)));
    return 0;
}


#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>

using namespace std;

const int MAXV = 5010, INF = 2*MAXV;

int dp[MAXV][MAXV/2][2], dp2[MAXV][MAXV/2][2];
int nxt[MAXV], leaf[MAXV], vis[MAXV], nLeafs[MAXV], go[MAXV], leafCnt;
vector<int> adj[MAXV];

int min(int a, int b, int c) {
    return min(a, min(b, c));
}

int g(int, int, int);

int f(int v, int k, int c) {
    int &ret = dp[v][k][c];
    if(ret == -1) {
        if(!leaf[v]) {
            ret = g(go[v], k, c);
        } else {
            ret = INF;
            if(c && k == 1) ret = 0;
            else if(!c && k == 0) ret = 0;
        }
    }
    return ret;
}

int g(int v, int k, int c) {
    int &ret = dp2[v][k][c];
    if(ret == -1) {
        if(nxt[v] == -1) {
            ret = min(f(v, k, c), f(v, k, !c) + 1);
        }
        else {
            ret = INF;
            for(int i = 0; i <= min(nLeafs[v], k); i++)
                ret = min(ret, f(v, i, c) + g(nxt[v], k - i, c), f(v, i, !c) + 1 + g(nxt[v], k - i, c));
        }
    }
    return ret;
}

int dfs(int v) {
    vis[v] = 1;
    int last = -1;
    for(int u : adj[v]) {
        if(!vis[u]) {
            nxt[u] = last;
            last = u;
            nLeafs[v] += dfs(u);
        }
    }
    if(last == -1) {
        leaf[v] = 1;
        leafCnt++;
        return nLeafs[v] = 1;
    } else {
        go[v] = last;
        return nLeafs[v];
    }
}

int main() {
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++) {
        vis[i] = 0;
        nLeafs[i] = 0;
        leaf[i] = 0;
    }
    for(int i = 0; i < n - 1; i++) {
        int a, b;
        scanf("%d%d", &a, &b);
        a--; b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    int root = -1;
    for(int i = 0; i < n; i++) {
        if(adj[i].size() > 1) {
            root = i;
            break;
        }
    }
    if(root == -1) {
        printf("1\n");
        return 0;
    }
    dfs(root);
    for(int i = 0; i < n; i++) {
        for(int j = 0; j <= leafCnt/2; j++) {
            for(int k = 0; k < 2; k++) {
                dp[i][j][k] = dp2[i][j][k] = -1;
            }
        }
    }
    // printf("Raiz %d\n", root);
    // for(int i = 0; i < n; i++) {
    //  printf("nxt[%d] = %d\n", i + 1, nxt[i] + 1);
    // }
    // for(int i = 0; i < n; i++) {
    //  printf("go[%d] = %d\n", i + 1, go[i] + 1);
    // }
    printf("%d\n", min(f(root, leafCnt/2, 0), f(root, leafCnt/2, 1)));
    return 0;
}


#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>

using namespace std;

const int MAXV = 5010, INF = 2*MAXV;

int dp[MAXV][MAXV/2][2], dp2[MAXV][MAXV/2][2];
int nxt[MAXV], leaf[MAXV], vis[MAXV], leafCnt, go[MAXV];
vector<int> adj[MAXV];

int min(int a, int b, int c) {
    return min(a, min(b, c));
}

int g(int, int, int);

int f(int v, int k, int c) {
    int &ret = dp[v][k][c];
    if(ret == -1) {
        if(!leaf[v]) {
            ret = g(go[v], k, c);
        } else {
            ret = INF;
            if(c && k == 1) ret = 0;
            else if(!c && k == 0) ret = 0;
        }
    }
    return ret;
}

int g(int v, int k, int c) {
    int &ret = dp2[v][k][c];
    if(ret == -1) {
        if(nxt[v] == -1) {
            ret = min(f(v, k, c), f(v, k, !c) + 1);
        }
        else {
            ret = INF;
            for(int i = 0; i <= k; i++)
                ret = min(ret, f(v, i, c) + g(nxt[v], k - i, c), f(v, i, !c) + 1 + g(nxt[v], k - i, c));
        }
    }
    return ret;
}

void dfs(int v) {
    vis[v] = 1;
    int last = -1;
    for(int u : adj[v]) {
        if(!vis[u]) {
            nxt[u] = last;
            last = u;
            dfs(u);
        }
    }
    if(last == -1) {
        leaf[v] = 1;
        leafCnt++;
    } else {
        go[v] = last;
    }
}

int main() {
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++) {
        vis[i] = 0;
        leaf[i] = 0;
    }
    for(int i = 0; i < n - 1; i++) {
        int a, b;
        scanf("%d%d", &a, &b);
        a--; b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    int root = -1;
    for(int i = 0; i < n; i++) {
        if(adj[i].size() > 1) {
            root = i;
            break;
        }
    }
    if(root == -1) {
        printf("1\n");
        return 0;
    }
    dfs(root);
    for(int i = 0; i < n; i++) {
        for(int j = 0; j <= leafCnt/2; j++) {
            for(int k = 0; k < 2; k++) {
                dp[i][j][k] = dp2[i][j][k] = -1;
            }
        }
    }
    // printf("Raiz %d\n", root);
    // for(int i = 0; i < n; i++) {
    //  printf("nxt[%d] = %d\n", i + 1, nxt[i] + 1);
    // }
    // for(int i = 0; i < n; i++) {
    //  printf("go[%d] = %d\n", i + 1, go[i] + 1);
    // }
    printf("%d\n", min(f(root, leafCnt/2, 0), f(root, leafCnt/2, 1)));
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 5005;
struct EDGE
{
    int to, next;
    EDGE() {}
    EDGE(int to, int next) :to(to), next(next) {}
}edge[maxn << 1];
int head[maxn], edgecnt;
int num_of_leaves[maxn];
int dp[maxn][maxn][2];
int degree[maxn];
int max_col_leaves;
const int inf = 0x3F3F3F3F;
void add(int s, int t)
{
    edge[edgecnt] = EDGE(t, head[s]);
    head[s] = edgecnt++;
}
void dfs(int u, int fa)
{
    dp[u][0][0] = dp[u][0][1] = 0;
    if (degree[u] == 1)
    {
        dp[u][1][1] = 0;
        dp[u][0][1] = inf;
        num_of_leaves[u] = 1;
        return;
    }
    int have_count_leaves = 0;
    for (int i = head[u]; ~i; i = edge[i].next)
    {
        int v = edge[i].to;
        if (v == fa) continue;
        dfs(v, u);

        have_count_leaves += num_of_leaves[v];
        for (int j = min(max_col_leaves, have_count_leaves); j >= 0; j--)
        {
            int ans[2] = { inf,inf };
            for (int k = 0; j - k >= 0 && k <= num_of_leaves[v]; k++)
            {
                ans[0] = min(ans[0], min(dp[u][j - k][0] + dp[v][k][1] + 1, dp[u][j - k][0] + dp[v][k][0]));
                ans[1] = min(ans[1], min(dp[u][j - k][1] + dp[v][k][0] + 1, dp[u][j - k][1] + dp[v][k][1]));
            }
            dp[u][j][0] = ans[0];
            dp[u][j][1] = ans[1];
        }
    }
    num_of_leaves[u] = have_count_leaves;
}
void init()
{
    memset(dp, 0x3F, sizeof(dp));
    memset(head, -1, sizeof(head));
    memset(degree, 0, sizeof(degree));
    edgecnt = 0;
    max_col_leaves = 0;
}
int main()
{
    int n;
    while (~scanf("%d", &n))
    {
        init();
        for (int i = 1; i < n; i++)
        {
            int s, t;
            scanf("%d %d", &s, &t);
            add(s, t);
            add(t, s);
            degree[s]++;
            degree[t]++;
        }
        if (n == 2)
        {
            puts("1");
            continue;
        }
        int root = -1;
        for (int i = 1; i <= n; i++)
        {
            if (degree[i] > 1) root = i;
            else max_col_leaves++;
        }
        max_col_leaves >>= 1;
        root = 3;
        dfs(root, 0);
        printf("%d\n", min(dp[root][max_col_leaves][0], dp[root][max_col_leaves][1]));
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 5005;
struct EDGE
{
    int to, next;
    EDGE() {}
    EDGE(int to, int next) :to(to), next(next) {}
}edge[maxn << 1];
int head[maxn], edgecnt;
int num_of_leaves[maxn];
int dp[maxn][maxn][2];
int degree[maxn];
int max_col_leaves;
void add(int s, int t)
{
    edge[edgecnt] = EDGE(t, head[s]);
    head[s] = edgecnt++;
}
void dfs(int u, int fa)
{
    dp[u][0][0] = dp[u][0][1] = 0;
    if (degree[u] == 1)
    {
        dp[u][1][1] = 0;
        dp[u][0][1] = 0x3F3F3F3F;
        num_of_leaves[u] = 1;
        return;
    }
    int have_count_leaves = 0;
    for (int i = head[u]; ~i; i = edge[i].next)
    {
        int v = edge[i].to;
        if (v == fa) continue;
        dfs(v, u);
        have_count_leaves += num_of_leaves[v];
        for (int j = min(have_count_leaves, max_col_leaves); j >= 0; j--)
            for (int k = 0; j - k >= 0 && k <= num_of_leaves[v]; k++)
            {
                dp[u][j][0] = min(dp[u][j - k][0] + dp[v][k][1] + 1, dp[u][j - k][0] + dp[v][k][0]);
                dp[u][j][1] = min(dp[u][j - k][1] + dp[v][k][0] + 1, dp[u][j - k][1] + dp[v][k][1]);
            }
    }
    num_of_leaves[u] = have_count_leaves;
}
void init()
{
    memset(dp, 0x3F, sizeof(dp));
    memset(head, -1, sizeof(head));
    memset(degree, 0, sizeof(degree));
    edgecnt = 0;
    max_col_leaves = 0;
}
int main()
{
    int n;
    while (~scanf("%d", &n))
    {
        init();
        for (int i = 1; i < n; i++)
        {
            int s, t;
            scanf("%d %d", &s, &t);
            add(s, t);
            add(t, s);
            degree[s]++;
            degree[t]++;
        }
        if (n == 2)
        {
            puts("1");
            continue;
        }
        int root = -1;
        for (int i = 1; i <= n; i++)
        {
            if (degree[i] > 1) root = i;
            else max_col_leaves++;
        }
        max_col_leaves >>= 1;
        dfs(root, 0);
        printf("%d\n", min(dp[root][max_col_leaves][0], dp[root][max_col_leaves][1]));
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 5005;
struct EDGE
{
    int to, next;
    EDGE() {}
    EDGE(int to, int next) :to(to), next(next) {}
}edge[maxn << 1];
int head[maxn], edgecnt;
int num_of_leaves[maxn];
int dp[maxn][maxn][2];
int degree[maxn];
int max_col_leaves;
const int inf = 0x3F3F3F3F;
void add(int s, int t)
{
    edge[edgecnt] = EDGE(t, head[s]);
    head[s] = edgecnt++;
}
void dfs(int u, int fa)
{
    dp[u][0][0] = dp[u][0][1] = 0;
    if (degree[u] == 1)
    {
        dp[u][1][1] = 0;
        dp[u][0][1] = inf;
        num_of_leaves[u] = 1;
        return;
    }
    int have_count_leaves = 0;
    for (int i = head[u]; ~i; i = edge[i].next)
    {
        int v = edge[i].to;
        if (v == fa) continue;
        dfs(v, u);

        have_count_leaves += num_of_leaves[v];
        for (int j = min(max_col_leaves, have_count_leaves); j >= 0; j--)
        {
            int ans[2] = { inf,inf };
            for (int k = 0; j - k >= 0 && k <= num_of_leaves[v]; k++)
            {
                ans[0] = min(ans[0], min(dp[u][j - k][0] + dp[v][k][1] + 1, dp[u][j - k][0] + dp[v][k][0]));
                ans[1] = min(ans[1], min(dp[u][j - k][1] + dp[v][k][0] + 1, dp[u][j - k][1] + dp[v][k][1]));
            }
            dp[u][j][0] = ans[0];
            dp[u][j][1] = ans[1];
        }
    }
    num_of_leaves[u] = have_count_leaves;
}
void init()
{
    memset(dp, 0x3F, sizeof(dp));
    memset(head, -1, sizeof(head));
    memset(degree, 0, sizeof(degree));
    edgecnt = 0;
    max_col_leaves = 0;
}
int main()
{
    int n;
    while (~scanf("%d", &n))
    {
        init();
        for (int i = 1; i < n; i++)
        {
            int s, t;
            scanf("%d %d", &s, &t);
            add(s, t);
            add(t, s);
            degree[s]++;
            degree[t]++;
        }
        if (n == 2)
        {
            puts("1");
            continue;
        }
        int root = -1;
        for (int i = 1; i <= n; i++)
        {
            if (degree[i] > 1) root = i;
            else max_col_leaves++;
        }
        max_col_leaves >>= 1;
        //root = 3;
        dfs(root, 0);
        printf("%d\n", min(dp[root][max_col_leaves][0], dp[root][max_col_leaves][1]));
    }
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<set>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<deque>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#include<functional>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define me(s) memset(s,0,sizeof(s))
#define For(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define sz size
#define clr clear
#define F(a,b) for(int i=a;b;i++)

#define N 100000+5
ll P[N];
ll ans[N];
int n;

int main()
{
    //freopen("t.txt","r",stdin);
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%I64d",&P[i]);
        sort(P,P+n);
        for(int i=1;i<n;i++)
            P[i]+=P[i-1];
        for(int i=1;i<=n;i++)
        {
            int j=n-2,t=i;
            while(j>=0)
            {
                ans[i-1]+=P[j];
                j-=t;
                t*=i;
            }
        }
        int q;
        scanf("%d",&q);
        while(q--)
        {
            int k;
            scanf("%d",&k);
            printf("%I64d ",k<n?ans[k-1]:ans[n-1]);
        }
            puts("");
    return 0;
}


#include <iostream>
#include <algorithm>
#include<stdio.h>
#include<string.h>
#include<numeric>
#include<queue>
#include<cmath>
#include<sstream>
#include<set>
#include<map>
#include<vector>
#define N 100010
#define inf 1000000
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define X first
#define Y second
#define lst p<<1,l,mid
#define rst p<<1|1,mid+1,r
#define ll long long
#define vi vector<int>
#define mod 1000000007
using namespace std;
ll sum[N];
int main()
{
 //freopen("in.txt","r",stdin);
 int n,m,i,k;
 while(~scanf("%d",&n))
 {
  vi a(n);
  for(i=0;i<n;i++)scanf("%d",&a[i]);
  sort(a.rbegin(),a.rend());
  sum[0]=0;
  for(i=0;i<n;i++)sum[i+1]=sum[i]+a[i];
  ll a1=0;
  for(i=1;i<n;i++)a1+=(ll)i*a[i];
  scanf("%d",&m);
  while(m--)
  {
   scanf("%d",&k);
   if(k==1)printf("%I64d\n",a1);
   else
   {
    int np=1,s=k,cnt=1;ll ans=0;
    while(np<n)
    {
     ll tnp=np+s;
     if(tnp>n)tnp=n;
     ans+=cnt*(sum[tnp]-sum[np]);
     cnt++;
     np=tnp;s*=k;
    }
    printf("%I64d\n",ans);
   }
  }
 }
 return 0;
}


#include <iostream>
#include <algorithm>
#include<stdio.h>
#include<string.h>
#include<numeric>
#include<queue>
#include<cmath>
#include<sstream>
#include<set>
#include<map>
#include<vector>
#define N 100010
#define inf 1000000
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define X first
#define Y second
#define lst p<<1,l,mid
#define rst p<<1|1,mid+1,r
#define ll long long
#define vi vector<int>
#define mod 1000000007
using namespace std;
ll sum[N];int a[N];
int cmp(int a,int b){return a>b;}
int main()
{
 //freopen("in.txt","r",stdin);
 int n,m,i,k;
 while(~scanf("%d",&n))
 {
  for(i=0;i<n;i++)scanf("%d",&a[i]);
  sort(a,a+n,cmp);
  sum[0]=0;
  for(i=0;i<n;i++)sum[i+1]=sum[i]+a[i];
  ll a1=0;
  for(i=1;i<n;i++)a1+=(ll)(i*a[i]);
  scanf("%d",&m);
  while(m--)
  {
   scanf("%d",&k);
   if(k==1)printf("%I64d",a1);
   else
   {
    int np=1,cnt=1;ll s=k,ans=0;
    while(np<n)
    {
     ll tnp=np+s;
     if(tnp>n)tnp=n;
     ans+=cnt*(sum[tnp]-sum[np]);
     cnt++;
     np=tnp;s*=k;
    }
    printf("%I64d",ans);
   }
   printf("%c",m==0?'\n':' ');
  }
 }
 return 0;
}


#include <iostream>
#include <algorithm>
#include<stdio.h>
#include<string.h>
#include<numeric>
#include<queue>
#include<cmath>
#include<sstream>
#include<set>
#include<map>
#include<vector>
#define N 100010
#define inf 1000000
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define X first
#define Y second
#define lst p<<1,l,mid
#define rst p<<1|1,mid+1,r
#define ll long long
#define vi vector<int>
#define mod 1000000007
using namespace std;
ll sum[N];
int main()
{
 //freopen("in.txt","r",stdin);
 int n,m,i,k;
 while(~scanf("%d",&n))
 {
  vi a(n+1);
  for(i=0;i<n;i++)scanf("%d",&a[i]);
  sort(a.rbegin(),a.rend());
  sum[0]=0;
  for(i=0;i<n;i++)sum[i+1]=sum[i]+a[i];
  ll a1=0;
  for(i=1;i<n;i++)a1+=(ll)(i*a[i]);
  scanf("%d",&m);
  while(m--)
  {
   scanf("%d",&k);
   if(k==1)printf("%I64d",a1);
   else
   {
    int np=1,cnt=1;ll s=k,ans=0;
    while(np<n)
    {
     ll tnp=np+s;
     if(tnp>n)tnp=n;
     ans+=cnt*(sum[tnp]-sum[np]);
     cnt++;
     np=tnp;s*=k;
    }
    printf("%I64d",ans);
   }
   printf("%c",m==0?'\n':' ');
  }
 }
 return 0;
}


#include <iostream>
#include <algorithm>
#include<stdio.h>
#include<string.h>
#include<numeric>
#include<queue>
#include<cmath>
#include<sstream>
#include<set>
#include<map>
#include<vector>
#define N 100010
#define inf 1000000
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define X first
#define Y second
#define lst p<<1,l,mid
#define rst p<<1|1,mid+1,r
#define ll long long
#define vi vector<int>
#define mod 1000000007
using namespace std;
ll sum[N];
int main()
{
 //freopen("in.txt","r",stdin);
 int n,m,i,k;
 while(~scanf("%d",&n))
 {
  vi a(n+1);
  for(i=0;i<n;i++)scanf("%d",&a[i]);
  sort(a.rbegin(),a.rend());
  sum[0]=0;
  for(i=0;i<n;i++)sum[i+1]=sum[i]+a[i];
  ll a1=0;
  for(i=1;i<n;i++)a1+=(ll)(i*a[i]);
  scanf("%d",&m);
  while(m--)
  {
   scanf("%d",&k);
   if(k==1)printf("%I64d ",a1);
   else
   {
    int np=1,cnt=1;ll s=k,ans=0;
    while(np<n)
    {
     ll tnp=np+s;
     if(tnp>n)tnp=n;
     ans+=cnt*(sum[tnp]-sum[np]);
     cnt++;
     np=tnp;s*=k;
    }
    printf("%I64d ",ans);
   }
  }
 }
 return 0;
}


#include <iostream>
#include <algorithm>
#include<stdio.h>
#include<string.h>
#include<numeric>
#include<queue>
#include<cmath>
#include<sstream>
#include<set>
#include<map>
#include<vector>
#define N 100010
#define inf 1000000
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define X first
#define Y second
#define lst p<<1,l,mid
#define rst p<<1|1,mid+1,r
#define ll long long
#define vi vector<int>
#define mod 1000000007
using namespace std;
ll sum[N];
int main()
{
// freopen("in.txt","r",stdin);
 int n,m,i,k;
 while(~scanf("%d",&n))
 {
  vi a(n);
  for(i=0;i<n;i++)scanf("%d",&a[i]);
  sort(a.rbegin(),a.rend());
  sum[0]=0;
  for(i=0;i<n;i++)sum[i+1]=sum[i]+a[i];
  ll a1=0;
  for(i=1;i<n;i++)a1+=i*a[i];
  scanf("%d",&m);
  while(m--)
  {
   scanf("%d",&k);
   if(k==1)printf("%I64d\n",a1);
   else
   {
    int np=1,s=k,cnt=1;ll ans=0;
    while(np<n)
    {
     ll tnp=np+s;
     if(tnp>n)tnp=n;
     ans+=cnt*(sum[tnp]-sum[np]);
     cnt++;
     np=tnp;s*=k;
    }
    printf("%I64d\n",ans);
   }
  }
 }
 return 0;
}


#include <iostream>
#include <algorithm>
#include<stdio.h>
#include<string.h>
#include<numeric>
#include<queue>
#include<cmath>
#include<sstream>
#include<set>
#include<map>
#include<vector>
#define N 100010
#define inf 1000000
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define X first
#define Y second
#define lst p<<1,l,mid
#define rst p<<1|1,mid+1,r
#define ll long long
#define vi vector<int>
#define mod 1000000007
using namespace std;
ll sum[N];int a[N];
int cmp(int a,int b){return a>b;}
int main()
{
 //freopen("in.txt","r",stdin);
 int n,m,i,k;
 while(~scanf("%d",&n))
 {
  for(i=0;i<n;i++)scanf("%d",&a[i]);
  sort(a,a+n,cmp);
  sum[0]=0;
  for(i=0;i<n;i++)sum[i+1]=sum[i]+a[i];
  ll a1=0;
  for(i=1;i<n;i++)a1+=i*1ll*a[i];
  scanf("%d",&m);
  while(m--)
  {
   scanf("%d",&k);
   if(k==1)printf("%I64d",a1);
   else
   {
    int np=1,cnt=1;ll s=k,ans=0;
    while(np<n)
    {
     ll tnp=np+s;
     if(tnp>n)tnp=n;
     ans+=cnt*(sum[tnp]-sum[np]);
     cnt++;
     np=tnp;s*=k;
    }
    printf("%I64d",ans);
   }
   printf("%c",m==0?'\n':' ');
  }
 }
 return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define sc(a) scanf("%d", &a)
#define sc2(a,b) scanf("%d %d", &a, &b)
#define sc3(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pri(x) printf("%d\n", x)
#define prie(x) printf("%d ", x)
#define mp make_pair
#define pb push_back
#define BUFF ios::sync_with_stdio(false);
#define db(x) cerr << #x << " == " << x<<endl
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
const int INF= 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fl;
const ld pi = acos(-1);
const int MOD = 1e9+7;
const int N = 1e5+10;

ll ps[N];
ll v[N];
int n;

void pre() {
	cin>>n;
	for(int i=1; i<=n; i++) {
		cin>>v[i];
	}
	sort(v+1, v+n+1);
	for(int i=1; i<=n; i++) ps[i] = v[i] + ps[i-1];
}

ll vai(int n, int k) {
	if(n <= k+1) return ps[n-1];
	else return vai(n-k, k) + ps[n-1];
}

void comp() {
	for(int i=1; i<n; i++) v[i] = vai(n, i);
}

int main() {
	BUFF;

	pre();
	comp();

	int q;
	cin>>q;

	while(q--) {
		int x;
		cin>>x;
		x = min(x, n-1);
		cout<<v[x]<<' ';
	}
	cout<<endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define sc(a) scanf("%d", &a)
#define sc2(a,b) scanf("%d %d", &a, &b)
#define sc3(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pri(x) printf("%d\n", x)
#define prie(x) printf("%d ", x)
#define mp make_pair
#define pb push_back
#define BUFF ios::sync_with_stdio(false);
#define db(x) cerr << #x << " == " << x<<endl
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
const int INF= 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fl;
const ld pi = acos(-1);
const int MOD = 1e9+7;
const int N = 1e5+10;

ll ps[N];
ll v[N];
int n;

void pre() {
	cin>>n;
	for(int i=1; i<=n; i++) {
		cin>>v[i];
	}
	sort(v+1, v+n+1);
	for(int i=1; i<=n; i++) ps[i] = v[i] + ps[i-1];
}

ll vai(int n, int k, int b) {
	if(n <= k) return 0ll;
	else return vai(n-k, k*b, b) + ps[n-k];
}

void comp() {
	for(int i=1; i<n; i++) v[i] = vai(n, 1, i);
}

int main() {
	BUFF;

	pre();
	comp();

	int q;
	cin>>q;

	while(q--) {
		int x;
		cin>>x;
		x = min(x, n-1);
		cout<<v[x]<<' ';
	}
	cout<<endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define sc(a) scanf("%d", &a)
#define sc2(a,b) scanf("%d %d", &a, &b)
#define sc3(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pri(x) printf("%d\n", x)
#define prie(x) printf("%d ", x)
#define mp make_pair
#define pb push_back
#define BUFF ios::sync_with_stdio(false);
#define db(x) cerr << #x << " == " << x<<endl
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
const int INF= 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fl;
const ld pi = acos(-1);
const int MOD = 1e9+7;
const int N = 1e5+10;

ll ps[N];
ll v[N];
int n;

void pre() {
	cin>>n;
	for(int i=1; i<=n; i++) {
		cin>>v[i];
	}
	sort(v+1, v+n+1);
	for(int i=1; i<=n; i++) ps[i] = v[i] + ps[i-1];
}

ll get(int x) {
	if(x <= 0) return 0;
	else return ps[x];
}

ll vai(int n, ll k, ll b) {
	if(n <= k) return 0ll;
	else return vai(n-k, k*b, b) + get(n-k);
}

void comp() {
	for(int i=1; i<n; i++) v[i] = vai(n, 1, i);
}

int main() {
	BUFF;

	pre();
	comp();

	int q;
	cin>>q;

	while(q--) {
		int x;
		cin>>x;
		x = min(x, n-1);
		cout<<v[x]<<' ';
	}
	cout<<endl;

	return 0;
}


/* ***********************************************************************
  > File Name: team.cpp
  > Author: Key
  > Mail: keyld777@gmail.com
  > Created Time: Wed 05 Apr 2017 07:42:40 PM CST
 ********************************************************************** */
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <queue>

#define each(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define reach(i, n) for (int(i) = n - 1; (i) >= 0; (i)--)
#define range(i, st, en) for (int(i) = (st); (i) <= (en); (i)++)
#define rrange(i, st, en) for (int(i) = (en); (i) >= (st); (i)--)
#define fill(ary, num) memset((ary), (num), sizeof(ary))

using namespace std;

typedef long long ll;
const int maxn = 2e5 + 5;
int n;
ll ans[maxn], sum[maxn], ary[maxn];

void solve(int q)
{
    ll dep = 1, idx = 1, nxt;
    ll tmp, res = 0, d = q;
    while (idx <= n) {
        nxt = idx + d <= n ? idx + d : n;
        tmp = sum[nxt] - sum[idx];
        res += tmp * dep;
        idx += d, dep++, d *= q;
    }
    printf("%lld ", ans[q] = res);
}

int main()
{
    int qn, q;
    scanf("%d", &n);
    range(i, 1, n) scanf("%lld", ary + i);
    sort(ary + 1, ary + 1 + n, greater<int>());
    range(i, 1, n) sum[i] = ary[i] + sum[i - 1];
    scanf("%d", &qn);
    while (qn--) {
        scanf("%d", &q);
        if (ans[q])
            printf("%lld ", ans[q]);
        else
            solve(q);
    }
    return 0;
}


/* ***********************************************************************
  > File Name: team.cpp
  > Author: Key
  > Mail: keyld777@gmail.com
  > Created Time: Wed 05 Apr 2017 07:42:40 PM CST
 ********************************************************************** */
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <queue>

#define each(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define reach(i, n) for (int(i) = n - 1; (i) >= 0; (i)--)
#define range(i, st, en) for (int(i) = (st); (i) <= (en); (i)++)
#define rrange(i, st, en) for (int(i) = (en); (i) >= (st); (i)--)
#define fill(ary, num) memset((ary), (num), sizeof(ary))

using namespace std;

typedef long long ll;
const int maxn = 2e5 + 5;
int ary[maxn], n;

void solve(int q)
{
    int dep = 1, idx = 2;
    ll sum, res = 0;
    while (idx <= n) {
        sum = 0;
        each(i, q) sum += ary[idx + i];
        sum *= dep;
        res += sum;
        idx += q, dep++;
    }
    printf("%lld ", res);
}

int main()
{
    int qn, q;
    scanf("%d", &n);
    range(i, 1, n) scanf("%d", ary + i);
    sort(ary + 1, ary + 1 + n, greater<int>());
    scanf("%d", &qn);
    while (qn--) {
        scanf("%d", &q);
        solve(q);
    }
    return 0;
}


/* ***********************************************************************
  > File Name: team.cpp
  > Author: Key
  > Mail: keyld777@gmail.com
  > Created Time: Wed 05 Apr 2017 07:42:40 PM CST
 ********************************************************************** */
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <queue>

#define each(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define reach(i, n) for (int(i) = n - 1; (i) >= 0; (i)--)
#define range(i, st, en) for (int(i) = (st); (i) <= (en); (i)++)
#define rrange(i, st, en) for (int(i) = (en); (i) >= (st); (i)--)
#define fill(ary, num) memset((ary), (num), sizeof(ary))

using namespace std;

typedef long long ll;
const int maxn = 2e5 + 5;
int ary[maxn], n;
ll ans[maxn], sum[maxn];

void solve(int q)
{
    int dep = 1, idx = 1, nxt;
    ll tmp, res = 0;
    while (idx <= n) {
        nxt = idx + q <= n ? idx + q : n;
        tmp = sum[nxt] - sum[idx];
        res += tmp * dep;
        idx +=q, dep*=q;
    }
    printf("%lld ", ans[q] = res);
}

int main()
{
    int qn, q;
    scanf("%d", &n);
    range(i, 1, n) scanf("%d", ary + i);
    sort(ary + 1, ary + 1 + n, greater<int>());
    range(i, 1, n) sum[i] = ary[i] + sum[i - 1];
    scanf("%d", &qn);
    while (qn--) {
        scanf("%d", &q);
        if (ans[q])
            printf("%lld ", ans[q]);
        else
            solve(q);
    }
    return 0;
}


/* ***********************************************************************
  > File Name: team.cpp
  > Author: Key
  > Mail: keyld777@gmail.com
  > Created Time: Wed 05 Apr 2017 07:42:40 PM CST
 ********************************************************************** */
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <queue>

#define each(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define reach(i, n) for (int(i) = n - 1; (i) >= 0; (i)--)
#define range(i, st, en) for (int(i) = (st); (i) <= (en); (i)++)
#define rrange(i, st, en) for (int(i) = (en); (i) >= (st); (i)--)
#define fill(ary, num) memset((ary), (num), sizeof(ary))

using namespace std;

typedef long long ll;
const int maxn = 2e5 + 5;
int ary[maxn], n;
ll ans[maxn], sum[maxn];

void solve(int q)
{
    int dep = 1, idx = 1, nxt;
    ll tmp, res = 0;
    while (idx <= n) {
        nxt = idx + q <= n ? idx + q : n;
        tmp = sum[nxt] - sum[idx];
        res += tmp * dep;
        idx = nxt, dep++;
        if (idx == n)
            break;
    }
    printf("%lld ", ans[q] = res);
}

int main()
{
    int qn, q;
    scanf("%d", &n);
    range(i, 1, n) scanf("%d", ary + i);
    sort(ary + 1, ary + 1 + n, greater<int>());
    range(i, 1, n) sum[i] = ary[i] + sum[i - 1];
    scanf("%d", &qn);
    while (qn--) {
        scanf("%d", &q);
        if (ans[q])
            printf("%lld ", ans[q]);
        else
            solve(q);
    }
    return 0;
}


/* ***********************************************************************
  > File Name: team.cpp
  > Author: Key
  > Mail: keyld777@gmail.com
  > Created Time: Wed 05 Apr 2017 07:42:40 PM CST
 ********************************************************************** */
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <queue>

#define each(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define reach(i, n) for (int(i) = n - 1; (i) >= 0; (i)--)
#define range(i, st, en) for (int(i) = (st); (i) <= (en); (i)++)
#define rrange(i, st, en) for (int(i) = (en); (i) >= (st); (i)--)
#define fill(ary, num) memset((ary), (num), sizeof(ary))

using namespace std;

typedef long long ll;
const int maxn = 2e5 + 5;
int ary[maxn], n;
ll ans[maxn], sum[maxn];

void solve(int q)
{
    int dep = 1, idx = 1, nxt;
    ll tmp, res = 0, d = q;
    while (idx <= n) {
        nxt = idx + d <= n ? idx + d : n;
        tmp = sum[nxt] - sum[idx];
        res += tmp * dep;
        idx += d, dep++, d *= q;
    }
    printf("%lld ", ans[q] = res);
}

int main()
{
    int qn, q;
    scanf("%d", &n);
    range(i, 1, n) scanf("%d", ary + i);
    sort(ary + 1, ary + 1 + n, greater<int>());
    range(i, 1, n) sum[i] = ary[i] + sum[i - 1];
    scanf("%d", &qn);
    while (qn--) {
        scanf("%d", &q);
        if (ans[q])
            printf("%lld ", ans[q]);
        else
            solve(q);
    }
    return 0;
}


/* ***********************************************************************
  > File Name: team.cpp
  > Author: Key
  > Mail: keyld777@gmail.com
  > Created Time: Wed 05 Apr 2017 07:42:40 PM CST
 ********************************************************************** */
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <queue>

#define each(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define reach(i, n) for (int(i) = n - 1; (i) >= 0; (i)--)
#define range(i, st, en) for (int(i) = (st); (i) <= (en); (i)++)
#define rrange(i, st, en) for (int(i) = (en); (i) >= (st); (i)--)
#define fill(ary, num) memset((ary), (num), sizeof(ary))

using namespace std;

typedef long long ll;
const int maxn = 2e5 + 5;
int ary[maxn], n;
ll ans[maxn];

void solve(int q)
{
    if (ans[q] != 0) {
        printf("%lld ", ans[q]);
        return;
    }
    int dep = 1, idx = 2;
    ll sum, res = 0;
    while (idx <= n) {
        sum = 0;
        each(i, q) sum += ary[idx + i];
        sum *= dep;
        res += sum;
        idx += q, dep++;
    }
    printf("%lld ", ans[q] = res);
}

int main()
{
    int qn, q;
    scanf("%d", &n);
    range(i, 1, n) scanf("%d", ary + i);
    sort(ary + 1, ary + 1 + n, greater<int>());
    scanf("%d", &qn);
    while (qn--) {
        scanf("%d", &q);
        solve(q);
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <fstream>
#include <string.h>
using namespace std;

int main ()
{
ifstream f("stone.in");
int n, teste;
long long st[100005], v[100005];
cin>>n;
memset(v, -1, sizeof v);
for (int i=1; i<=n; i++)
    cin>>st[i];
sort (st+1, st+n+1);
for (int i=1; i<=n; i++)
    st[i]+=st[i-1];
cin>>teste;
for (int j=0; j<teste; j++)
{
    long long k, ck;
    cin>>k;
    ck=k;

    if (k>=n-1)
        {
            cout<<st[n-1]<<" ";
            continue;
        }
    if (v[k]!=-1)
    {
        cout<<v[k]<<" ";
        continue;
    }
    long long sum=0;
    for (int i=n-1; i>0; i-=ck, ck=min(ck*k, 1ll*n))
        sum+=st[i];
    v[k]=sum;
    cout<<v[k]<<" ";
}
  return 0;
}


#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
using namespace std;

int main()
{
    ifstream f("stone.in");

    int n, v[100005]={0};
    vector <int> st;
    cin>>n;
    for (int i=0; i<n; i++)
    {
        int k;
        cin>>k;
        st.push_back(k);
    }
    sort(st.begin(), st.end());
    int teste;
    cin>>teste;
    for (int i=0; i<teste; i++)
    {
        int t, sum=0, c=0, sp=0;
        cin>>t;
        if (v[t]!=0)
        {
            cout<<v[t]<<" ";
            continue;
        }
        for (int j=st.size()-2; j>=0; j--)
        {
            if ((st.size()-2-j)%t==0)
                {
                    sum+=c*sp;
                    sp=0;
                    c++;
                }
            sp+=st[j];
        }
        sum+=c*sp;
        v[t]=sum;
        cout<<sum<<" ";
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
using namespace std;

int main()
{
    ifstream f("stone.in");

    int n, v[100005]={0};
    vector <int> st;
    f>>n;
    for (int i=0; i<n; i++)
    {
        int k;
        f>>k;
        st.push_back(k);
    }
    sort(st.begin(), st.end());
    int teste;
    f>>teste;
    for (int i=0; i<teste; i++)
    {
        int t, sum=0, c=0, sp=0;
        f>>t;
        if (v[t]!=0)
        {
            cout<<v[t]<<" ";
            continue;
        }
        for (int j=st.size()-2; j>=0; j--)
        {
            if ((st.size()-2-j)%t==0)
                {
                    sum+=c*sp;
                    sp=0;
                    c++;
                }
            sp+=st[j];
        }
        sum+=c*sp;
        v[t]=sum;
        cout<<sum<<" ";
    }
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<bits/stdc++.h>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<bitset>
#include<unordered_map>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
// #include "boost/algorithm/string.hpp"
/* RAJJI KI MEHER */
#define fio ios_base::sync_with_stdio(false)
//#define mod 1000000007
//#define mod1 mod
#define mod2 100000009
#define li long long int
#define ll int
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define readi(x) scanf("%d",&x)
#define  reads(x)  scanf("%s", x)
#define readl(x) scanf("%lld",&x)
#define rep(i,n) for(i=0;i<n;i++)
#define revp(i,n) for(i=(n-1);i>=0;i--)
#define myrep1(i,a,b) for(i=a;i<=b;i++)
#define myrep2(i,a,b) for(i=b;i>=a;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define sec second
#define MAXN 100000000000000005
#define MINN -10000000000000000
#define INTMAX 1000001000
#define pii pair

#define pdd pair<double,double>
#define pic pair<int,char>
//#define N 5005
#define lgn 20
#define ddouble long double
#define minus minu
#define PI 3.1415926535
#define lgn 20
using namespace std;

#include <bits/stdc++.h>
using namespace std;

int main(){
   li n,q,i,j,k,s=0,cp,ans=0,c=0;
   readl(n);
   vector
a(n);
   vector
v(n);
   for(i=0;i<n;i++) {readl(a[i]);}
   sort(a.rbegin(),a.rend());
   for(i=0;i<n;i++){
   if(i) v[i]=v[i-1]+a[i];
  else v[i]=a[i];
  //cout<<v[i]<<" ";
   }
   readl(q);

   while(q--){
       readl(k);
       i=0;
       ans=0;
       s=0,cp=1,c=0;
       while(i<n){
if(i==0) {ans=ans+v[i+cp-1]*c;}
else {
    ans=ans+(v[min(i+cp-1,n-1)]-v[i-1])*c;
}
i=i+cp;
c++;
cp=cp*k;
//cout<<ans<<" ";
       }
       cout<<ans<<" ";
   }

}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<bits/stdc++.h>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<bitset>
#include<unordered_map>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
// #include "boost/algorithm/string.hpp"
/* RAJJI KI MEHER */
#define fio ios_base::sync_with_stdio(false)
//#define mod 1000000007
//#define mod1 mod
#define mod2 100000009
#define li long long int
#define ll int
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define readi(x) scanf("%d",&x)
#define  reads(x)  scanf("%s", x)
#define readl(x) scanf("%lld",&x)
#define rep(i,n) for(i=0;i<n;i++)
#define revp(i,n) for(i=(n-1);i>=0;i--)
#define myrep1(i,a,b) for(i=a;i<=b;i++)
#define myrep2(i,a,b) for(i=b;i>=a;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define sec second
#define MAXN 100000000000000005
#define MINN -10000000000000000
#define INTMAX 1000001000
#define pii pair

#define pdd pair<double,double>
#define pic pair<int,char>
//#define N 5005
#define lgn 20
#define ddouble long double
#define minus minu
#define PI 3.1415926535
#define lgn 20
using namespace std;

#include <bits/stdc++.h>
using namespace std;

int main(){
   li n,q,i,j,k,s=0,cp,ans=0,c=0;
   cin>>(n);
   vector
a(n);
   vector
v(n);
   for(i=0;i<n;i++) {cin>>(a[i]);}
   sort(a.rbegin(),a.rend());
   for(i=0;i<n;i++){
   if(i) v[i]=v[i-1]+a[i];
  else v[i]=a[i];
  //cout<<v[i]<<" ";
   }
   readl(q);
  map
m;
   while(q--){
       readl(k);
       i=0;
       ans=0;
       s=0,cp=1,c=0;
       if(m.count(k)!=0) {
           cout<<m[k]<<" ";
           continue;
       }
       while(i<n){
if(i==0) {ans=ans+v[i+cp-1]*c;}
else {
    ans=ans+(v[min(i+cp-1,n-1)]-v[i-1])*c;
}
i=i+cp;
c++;
cp=cp*k;
//cout<<ans<<" ";
       }
       m[k]=ans;
       cout<<ans<<" ";
   }

}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<bits/stdc++.h>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<bitset>
#include<unordered_map>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
// #include "boost/algorithm/string.hpp"
/* RAJJI KI MEHER */
#define fio ios_base::sync_with_stdio(false)
//#define mod 1000000007
//#define mod1 mod
#define mod2 100000009
#define li long long int
#define ll int
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define readi(x) scanf("%d",&x)
#define  reads(x)  scanf("%s", x)
#define readl(x) scanf("%lld",&x)
#define rep(i,n) for(i=0;i<n;i++)
#define revp(i,n) for(i=(n-1);i>=0;i--)
#define myrep1(i,a,b) for(i=a;i<=b;i++)
#define myrep2(i,a,b) for(i=b;i>=a;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define sec second
#define MAXN 100000000000000005
#define MINN -10000000000000000
#define INTMAX 1000001000
#define pii pair

#define pdd pair<double,double>
#define pic pair<int,char>
//#define N 5005
#define lgn 20
#define ddouble long double
#define minus minu
#define PI 3.1415926535
#define lgn 20
using namespace std;

#include <bits/stdc++.h>
using namespace std;

int main(){
   li n,q,i,j,k,s=0,cp,ans=0,c=0;
   readl(n);
   vector
a(n);
   vector
v(n);
   for(i=0;i<n;i++) {readl(a[i]);}
   sort(a.rbegin(),a.rend());
   for(i=0;i<n;i++){
   if(i) v[i]=v[i-1]+a[i];
  else v[i]=a[i];
  //cout<<v[i]<<" ";
   }
   readl(q);
  map
m;
   while(q--){
       readl(k);
       i=0;
       ans=0;
       s=0,cp=1,c=0;
       if(m.count(k)!=0) {
           cout<<m[k]<<" ";
           continue;
       }
       while(i<n){
if(i==0) {ans=ans+v[i+cp-1]*c;}
else {
    ans=ans+(v[min(i+cp-1,n-1)]-v[i-1])*c;
}
i=i+cp;
c++;
cp=cp*k;
//cout<<ans<<" ";
       }
       cout<<ans<<" ";
   }

}


/*
          ,     \    /      ,
         / \    )\__/(     / \
        /   \  (_\  /_)   /   \
   ____/_____\__\@  @/___/_____\____
  |             |\../|              |
  |              \VV/               |
  |        ------hoi-------         |
  |_________________________________|
   |    /\ /      \\       \ /\    |
   |  /   V        ))       V   \  |
   |/     `       //        '     \|
   `              V                '
*/

#include <bits/stdc++.h>

#define ld long double
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define f first
#define se second
#define Speedforce boost();
#define fop(s1, s2) freopen(s1, "r", stdin);freopen(s2, "w", stdout);

using namespace std;

void boost(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

const int N = 1e6;
const ll inf = 1e9 + 7;
const ld PI = acos(-1);

ll bin_pow(ll x, ll y){
    ll res = 1, base = x;
    while(y){
        if(y & 1){
            res *= base;
            res %= inf;
        }
        base *= base;
        base %= inf;
        y /= 2;
    }
    return res;
}

int n, q;
double p[N], a[N], sum;

int main(){
    Speedforce
    //fop("", "")
    cin >> n;
    for(int i = 1;i <= n;i ++){
        cin >> a[i];
    }
    sort(a + 1, a + 1 + n);
    for(int i = 1;i <= n;i ++){
        p[i] = p[i - 1] + a[i];
        sum += p[i - 1];
    }
    cin >> q;
    while(q--){
        ll x;
        cin >> x;
        if(x == 1){
            cout << sum << " ";
            continue;
        }
        int pos = n - 1;
        ll cnt = x;
        double cursum = 0;
        while(pos > 0){
            cursum += p[pos];
            pos -= cnt;
            cnt *= x;
        }
        cout << cursum << " ";
    }
}


/*
          ,     \    /      ,
         / \    )\__/(     / \
        /   \  (_\  /_)   /   \
   ____/_____\__\@  @/___/_____\____
  |             |\../|              |
  |              \VV/               |
  |        ------hoi-------         |
  |_________________________________|
   |    /\ /      \\       \ /\    |
   |  /   V        ))       V   \  |
   |/     `       //        '     \|
   `              V                '
*/

#include <bits/stdc++.h>

#define ld long double
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define f first
#define se second
#define Speedforce boost();
#define fop(s1, s2) freopen(s1, "r", stdin);freopen(s2, "w", stdout);

using namespace std;

void boost(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

const int N = 1e6;
const ll inf = 1e9 + 7;
const ld PI = acos(-1);

ll bin_pow(ll x, ll y){
    ll res = 1, base = x;
    while(y){
        if(y & 1){
            res *= base;
            res %= inf;
        }
        base *= base;
        base %= inf;
        y /= 2;
    }
    return res;
}

int n, q;
double p[N], a[N], sum;

int main(){
    Speedforce
    //fop("", "")
    cin >> n;
    for(int i = 1;i <= n;i ++){
        cin >> a[i];
    }
    sort(a + 1, a + 1 + n);
    for(int i = 1;i <= n;i ++){
        p[i] = p[i - 1] + a[i];
        sum += p[i - 1];
    }
    cin >> q;
    while(q--){
        ll x;
        cin >> x;
        if(x == 1){
            cout << sum << " ";
            continue;
        }
        double pos = n - 1;
        ll cnt = x;
        double cursum = 0;
        while(pos > 0){
            cursum += p[(int)pos];
            pos -= cnt;
            cnt *= x;
        }
        cout << cursum << " ";
    }
}


/*
          ,     \    /      ,
         / \    )\__/(     / \
        /   \  (_\  /_)   /   \
   ____/_____\__\@  @/___/_____\____
  |             |\../|              |
  |              \VV/               |
  |        ------hoi-------         |
  |_________________________________|
   |    /\ /      \\       \ /\    |
   |  /   V        ))       V   \  |
   |/     `       //        '     \|
   `              V                '
*/

#include <bits/stdc++.h>

#define ld long double
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define f first
#define se second
#define Speedforce boost();
#define fop(s1, s2) freopen(s1, "r", stdin);freopen(s2, "w", stdout);

using namespace std;

void boost(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

const int N = 1e6;
const ll inf = 1e9 + 7;
const ld PI = acos(-1);

ll bin_pow(ll x, ll y){
    ll res = 1, base = x;
    while(y){
        if(y & 1){
            res *= base;
            res %= inf;
        }
        base *= base;
        base %= inf;
        y /= 2;
    }
    return res;
}

int n, q;
ll p[N], a[N], sum;

int main(){
    Speedforce
    //fop("", "")
    cin >> n;
    for(int i = 1;i <= n;i ++){
        cin >> a[i];
    }
    sort(a + 1, a + 1 + n);
    for(int i = 1;i <= n;i ++){
        p[i] = p[i - 1] + a[i];
        sum += p[i - 1];
    }
    cin >> q;
    while(q--){
        ll x;
        cin >> x;
        if(x == 1){
            cout << sum << " ";
            continue;
        }
        double pos = n - 1;
        ll cnt = x;
        ll cursum = 0;
        while(pos > 0){
            cursum += p[(int)pos];
            pos -= cnt;
            cnt *= x;
        }
        cout << cursum << " ";
    }
}


/*
          ,     \    /      ,
         / \    )\__/(     / \
        /   \  (_\  /_)   /   \
   ____/_____\__\@  @/___/_____\____
  |             |\../|              |
  |              \VV/               |
  |        ------hoi-------         |
  |_________________________________|
   |    /\ /      \\       \ /\    |
   |  /   V        ))       V   \  |
   |/     `       //        '     \|
   `              V                '
*/

#include <bits/stdc++.h>

#define ld long double
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define f first
#define se second
#define Speedforce boost();
#define fop(s1, s2) freopen(s1, "r", stdin);freopen(s2, "w", stdout);

using namespace std;

void boost(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

const int N = 1e6;
const ll inf = 1e9 + 7;
const ld PI = acos(-1);

ll bin_pow(ll x, ll y){
    ll res = 1, base = x;
    while(y){
        if(y & 1){
            res *= base;
            res %= inf;
        }
        base *= base;
        base %= inf;
        y /= 2;
    }
    return res;
}

int n, q;
ll p[N], a[N], sum;

int main(){
    Speedforce
    //fop("", "")
    cin >> n;
    for(int i = 1;i <= n;i ++){
        cin >> a[i];
    }
    sort(a + 1, a + 1 + n);
    for(int i = 1;i <= n;i ++){
        p[i] = p[i - 1] + a[i];
        sum += p[i - 1];
    }
    cin >> q;
    while(q--){
        ll x;
        cin >> x;
        if(x == 1){
            cout << sum << " ";
            continue;
        }
        int pos = n - 1;
        ll cnt = x;
        ll cursum = 0;
        while(pos > 0){
            cursum += p[pos];
            pos -= cnt;
            cnt *= x;
        }
        cout << cursum << " ";
    }
}


#include <bits/stdc++.h>

using namespace std;

long long a[100100], ps[100100], ans[100100];
int n, m;

void solve() {
  int x;
  cin >> x;
  if (ans[x] == 0) {
    long long i = n, w = 1, h = 0;
    while (i > 0) {
      long long j = max(0ll, i - w);
      ans[x] += (ps[i] - ps[j]) * (h++);
      w *= x; i = j;
    }
  }
  cout << ans[x];
  cout << (m > 0 ? ' ' : '\n');
}

int main() {
  ios::sync_with_stdio(0); cin.tie(0);

  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  sort(a + 1, a + 1 + n);
  for (int i = 1; i <= n; i++) ps[i] = ps[i - 1] + a[i];

  cin >> m;
  while (m--) {
    solve();
  }
}


#include <bits/stdc++.h>
#define y0 han_solo
#define rng(a) a.begin(), a.end()
#define sz(x) (int)(x).size()
#define rep(i,a,b) for(int i = (a); i <= (b); i++)
#define rrep(i,a,b) for(int i = (b); i >= (a); i--)
#define rall(x,a) for(auto x : a)
#define se second
#define fi first
#define pb push_back
#define pcnt __builtin_popcountll
#define MOD 1000000007
#define endl "\n"
#define int long long
typedef long long ll;
using namespace std;

const int N = 111111;
int a[N], sum[N];
int n, q;

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> q;
    rep(i, 1, n) {
        cin >> a[i];
    }
    sort(a + 1, a + n + 1, std::greater<int>());
    rep(i, 1, n) {
        sum[i] = sum[i - 1] + a[i];
    }
    while(q--) {
        int k;
        cin >> k;
        int l = 1, r, p = 1, cnt = 0;
        int ans = 0;
        while(l <= n) {
            r = min(n, l + p - 1);
            ans += (sum[r] - sum[l - 1]) * cnt;
            l += p;
            p *= k;
            cnt++;
        }
        cout << ans << " ";
    }
    cout << endl;
    return 0;
}


#include <bits/stdc++.h>
#define y0 han_solo
#define rng(a) a.begin(), a.end()
#define sz(x) (int)(x).size()
#define rep(i,a,b) for(int i = (a); i <= (b); i++)
#define rrep(i,a,b) for(int i = (b); i >= (a); i--)
#define rall(x,a) for(auto x : a)
#define se second
#define fi first
#define pb push_back
#define pcnt __builtin_popcountll
#define MOD 1000000007
#define endl "\n"
#define int long long
typedef long long ll;
using namespace std;

const int N = 111111;
int a[N], sum[N];
int n, q;

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    rep(i, 1, n) {
        cin >> a[i];
    }
    sort(a + 1, a + n + 1, std::greater<int>());
    rep(i, 1, n) {
        sum[i] = sum[i - 1] + a[i];
    }
    cin >> q;
    while(q--) {
        int k;
        cin >> k;
        int l = 1, r, p = 1, cnt = 0;
        int ans = 0;
        while(l <= n) {
            r = min(n, l + p - 1);
            ans += (sum[r] - sum[l - 1]) * cnt;
            l += p;
            p *= k;
            cnt++;
        }
        cout << ans << " ";
    }
    cout << endl;
    return 0;
}


#include <bits/stdc++.h>
#define y0 han_solo
#define rng(a) a.begin(), a.end()
#define sz(x) (int)(x).size()
#define rep(i,a,b) for(int i = (a); i <= (b); i++)
#define rrep(i,a,b) for(int i = (b); i >= (a); i--)
#define rall(x,a) for(auto x : a)
#define se second
#define fi first
#define pb push_back
#define pcnt __builtin_popcountll
#define MOD 1000000007
#define endl "\n"
#define int long long
typedef long long ll;
using namespace std;

const int N = 111111;
int a[N], sum[N];
int n, q;

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    rep(i, 1, n) {
        cin >> a[i];
    }
    sort(a + 1, a + n + 1, std::greater<int>());
    rep(i, 1, n) {
        sum[i] = sum[i - 1] + a[i];
    }
    cin >> q;
    int a1 = 0;
    while(q--) {
        int k;
        cin >> k;
        if(k == 1 && a1) {
            cout << a1 << " ";
        } else {
            int l = 1, r, p = 1, cnt = 0;
            int ans = 0;
            while(l <= n) {
                r = min(n, l + p - 1);
                ans += (sum[r] - sum[l - 1]) * cnt;
                l += p;
                p *= k;
                cnt++;
            }
            if(k == 1) {
                a1 = ans;
            }
            cout << ans << " ";
        }
    }
    cout << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef pair<int, int> pi;

int n, q;
lint a[100005];
map<int, lint> mp;

int main(){
	cin >> n;
	for(int i=1; i<=n; i++){
		scanf("%lld",&a[i]);
	}
	sort(a+1, a+n+1);
	reverse(a+1, a+n+1);
	for(int i=2; i<=n; i++) a[i] += a[i-1];
	cin >> q;
	while(q--){
		int x;
		scanf("%d",&x);
		if(mp.find(x) != mp.end()) printf("%lld ", mp[x]);
		lint pw = 1, stp = 0, ret = 0;
		int s = 0, e = 0;
		while(e < n){
			int ns = e+1, ne = e + pw;
			ne = min(ne, n);
			ret += stp * (a[ne] - a[ns-1]);
			stp++;
			pw *= x;
			s = ns, e = ne;
		}
		mp[x] = ret;
		printf("%lld ", ret);
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef pair<int, int> pi;

int n, q;
lint a[100005];
map<int, lint> mp;

int main(){
	cin >> n;
	for(int i=1; i<=n; i++){
		scanf("%lld",&a[i]);
	}
	sort(a+1, a+n+1);
	reverse(a+1, a+n+1);
	for(int i=2; i<=n; i++) a[i] += a[i-1];
	cin >> q;
	while(q--){
		int x;
		scanf("%d",&x);
		if(mp.find(x) != mp.end()){
			cout << mp[x] << " ";
			continue;
		}
		lint pw = 1, stp = 0, ret = 0;
		lint pos = 1;
		while(pos < n){
			stp++;
			pw *= x;
			ret += stp * (a[min(1ll * n, pos + pw)] - a[pos]);
			pos += pw;
		}
		mp[x] = ret;
		cout << ret << " ";
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef pair<int, int> pi;

int n, q;
lint a[100005];
map<int, lint> mp;

int main(){
	cin >> n;
	for(int i=1; i<=n; i++){
		scanf("%lld",&a[i]);
	}
	sort(a+1, a+n+1);
	reverse(a+1, a+n+1);
	for(int i=2; i<=n; i++) a[i] += a[i-1];
	cin >> q;
	while(q--){
		int x;
		scanf("%d",&x);
		if(mp.find(x) != mp.end()){
			printf("%lld ", mp[x]);
			continue;
		}
		lint pw = 1, stp = 0, ret = 0;
		while(pw <= n){
			stp++;
			lint nxt = pw * x;
			ret += stp * (a[min(1ll * n, pw + nxt)] - a[pw]);
			pw += nxt;
		}
		mp[x] = ret;
		printf("%lld ", ret);
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef pair<int, int> pi;
typedef unsigned long long fuck;

int n, q;
lint a[100005];
map<int, fuck> mp;

int main(){
	cin >> n;
	for(int i=1; i<=n; i++){
		scanf("%lld",&a[i]);
	}
	sort(a+1, a+n+1);
	reverse(a+1, a+n+1);
	for(int i=2; i<=n; i++) a[i] += a[i-1];
	cin >> q;
	while(q--){
		int x;
		scanf("%d",&x);
		if(mp.find(x) != mp.end()){
			cout << mp[x] << " ";
			continue;
		}
		fuck pw = 1, stp = 0, ret = 0;
		while(pw < n){
			stp++;
			lint nxt = pw * x;
			ret += stp * (a[min((fuck)n, pw + nxt)] - a[pw]);
			pw += nxt;
		}
		mp[x] = ret;
		cout << ret << " ";
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef pair<int, int> pi;

int n, q;
lint a[100005];
map<int, lint> mp;

int main(){
	cin >> n;
	for(int i=1; i<=n; i++){
		scanf("%lld",&a[i]);
	}
	sort(a+1, a+n+1);
	reverse(a+1, a+n+1);
	for(int i=2; i<=n; i++) a[i] += a[i-1];
	cin >> q;
	while(q--){
		int x;
		scanf("%d",&x);
		if(mp.find(x) != mp.end()) printf("%lld ", mp[x]);
		lint pw = 1, stp = 0, ret = 0;
		while(pw <= n){
			stp++;
			lint nxt = pw * x;
			ret += stp * (a[min(1ll * n, pw + nxt)] - a[pw]);
			pw += nxt;
		}
		mp[x] = ret;
		printf("%lld ", ret);
	}
}


#include<bits/stdc++.h>
#define lli long long int

using namespace std;

lli n;
lli arr[100100];
lli A[100100];
lli C[100100];

void calSum()
{
    lli sum=0;
    for(lli i=1;i<=n;i++)
    {
        sum=sum+arr[i];
        A[i]=sum;
    }
}

void cal()
{
    calSum();
    for(lli i=1;i<=100000;i++)
    {
        lli sum=0;
        lli u=n-1;
        lli p=i;
        lli qq=1;
        while(u>=1)
        {
            int v=u-p;
            if(v<0) v=0;
            sum=sum+((A[u]-A[v])*qq);
            u=u-p;
            p=p*i;
            qq++;
        }
        C[i]=sum;
    }

}

int main()
{
    scanf("%lld",&n);
    for(lli i=1;i<=n;i++) scanf("%lld",&arr[i]);
    sort(arr+1,arr+n+1);
    cal();
    lli q;
    scanf("%lld",&q);
    for(lli i=1;i<=q;i++)
    {
        lli uu;
        scanf("%lld",&uu);
        printf("%lld\n",C[uu]);
    }

    return 0;
}


#include<bits/stdc++.h>
#define lli long long int

using namespace std;

lli n;
lli arr[100100];
lli A[100100];
lli C[100100];

void calSum()
{
    lli sum=0;
    for(lli i=1;i<=n;i++)
    {
        sum=sum+arr[i];
        A[i]=sum;
    }
}

void cal()
{
    calSum();
    for(lli i=1;i<=100000;i++)
    {
        lli sum=0;
        lli u=n-1;
        lli p=i;
        lli qq=1;
        while(u>=1)
        {
            int v=u-p;
            if(v<0) v=0;
            sum=sum+((A[u]-A[v])*qq);
            u=u-p;
            p=p*i;
            qq++;
        }
        C[i]=sum;
        //if(n==100000) cout<<i<<endl;
    }
    if(n==100000) cout<<"okokok"<<endl;
}

int main()
{
    scanf("%lld",&n);
    for(lli i=1;i<=n;i++) scanf("%lld",&arr[i]);
    sort(arr+1,arr+n+1);
    cal();
    lli q;
    scanf("%lld",&q);
    for(lli i=1;i<=q;i++)
    {
        lli uu;
        scanf("%lld",&uu);
        printf("%lld ",C[uu]);
    }

    printf("\n");

    return 0;
}


#include<bits/stdc++.h>
#define lli long long int

using namespace std;

lli n;
lli arr[100100];
lli A[100100];
lli C[100100];

void calSum()
{
    lli sum=0;
    for(lli i=1;i<=n;i++)
    {
        sum=sum+arr[i];
        A[i]=sum;
    }
}

void cal()
{
    calSum();
    for(lli i=1;i<=100000;i++)
    {
        lli sum=0;
        lli u=n-1;
        while(u>=1)
        {
            sum=sum+A[u];
            u=u-i;
        }
        C[i]=sum;
    }

}

int main()
{
    scanf("%lld",&n);
    for(lli i=1;i<=n;i++) scanf("%lld",&arr[i]);
    sort(arr+1,arr+n+1);
    cal();
    lli q;
    scanf("%lld",&q);

    for(lli i=1;i<=q;i++)
    {
        lli uu;
        scanf("%lld",&uu);
        printf("%lld ",C[uu]);
    }
    printf("\n");

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int n;
int arr[100100];
int A[100100];
int C[100100];

void calSum()
{
    int sum=0;
    for(int i=1;i<=n;i++)
    {
        sum=sum+arr[i];
        A[i]=sum;
    }
}

void cal()
{
    calSum();
    for(int i=1;i<=n;i++)
    {
        int sum=0;
        int u=n-1;
        while(u>=1)
        {
            sum=sum+A[u];
            u=u-i;
        }
        C[i]=sum;
    }

}

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&arr[i]);
    sort(arr+1,arr+n+1);
    cal();
    int q;
    scanf("%d",&q);

    for(int i=1;i<=q;i++)
    {
        int uu;
        scanf("%d",&uu);
        printf("%d ",C[uu]);
    }
    printf("\n");

    return 0;
}


#include<bits/stdc++.h>
#define lli long long int

using namespace std;

lli n;
lli arr[100100];
lli A[100100];
lli C[100100];

void calSum()
{
    lli sum=0;
    for(lli i=1;i<=n;i++)
    {
        sum=sum+arr[i];
        A[i]=sum;
    }
}

void cal()
{
    calSum();
    for(lli i=1;i<=100000;i++)
    {
        lli sum=0;
        lli u=n-1;
        lli p=i;
        lli qq=1;
        while(u>=1)
        {
            lli v=u-p;
            if(v<0) v=0;
            sum=sum+((A[u]-A[v])*qq);
            u=u-p;
            p=p*i;
            qq++;
        }
        C[i]=sum;
        //if(n==100000) cout<<i<<endl;
    }
    //if(n==100000) cout<<"okokok"<<endl;
}

int main()
{
    scanf("%lld",&n);
    for(lli i=1;i<=n;i++) scanf("%lld",&arr[i]);
    sort(arr+1,arr+n+1);
    cal();
    lli q;
    scanf("%lld",&q);
    for(lli i=1;i<=q;i++)
    {
        lli uu;
        scanf("%lld",&uu);
        printf("%lld ",C[uu]);
    }

    printf("\n");

    return 0;
}


#include<bits/stdc++.h>
#define lli long long int

using namespace std;

lli n;
lli arr[100100];
lli A[100100];
lli C[100100];

void calSum()
{
    lli sum=0;
    for(lli i=1;i<=n;i++)
    {
        sum=sum+arr[i];
        A[i]=sum;
    }
}

void cal()
{
    calSum();
    for(lli i=1;i<=100000;i++)
    {
        lli sum=0;
        lli u=n-1;
        while(u>=1)
        {
            sum=sum+A[u];
            u=u-i;
        }
        C[i]=sum;
    }

}

int main()
{
    scanf("%lld",&n);
    for(lli i=1;i<=n;i++) scanf("%lld",&arr[i]);
    sort(arr+1,arr+n+1);
    cal();
    lli q;
    scanf("%lld",&q);

    for(lli i=1;i<=q;i++)
    {
        lli uu;
        scanf("%lld",&uu);
        printf("%lld ",C[uu]);
        if(n==100000) cout<<uu<<endl;
    }
    printf("\n");

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int n;
int arr[100100];
int A[100100];
int C[100100];

void calSum()
{
    int sum=0;
    for(int i=1;i<=n;i++)
    {
        sum=sum+arr[i];
        A[i]=sum;
    }
}

void cal()
{
    calSum();
    for(int i=1;i<=100000;i++)
    {
        int sum=0;
        int u=n-1;
        while(u>=1)
        {
            sum=sum+A[u];
            u=u-i;
        }
        C[i]=sum;
    }

}

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&arr[i]);
    sort(arr+1,arr+n+1);
    cal();
    int q;
    scanf("%d",&q);

    for(int i=1;i<=q;i++)
    {
        int uu;
        scanf("%d",&uu);
        printf("%d ",C[uu]);
    }
    printf("\n");

    return 0;
}


#include<bits/stdc++.h>
#define lli long long int

using namespace std;

lli n;
lli arr[100100];
lli A[100100];
lli C[100100];

void calSum()
{
    lli sum=0;
    for(lli i=1;i<=n;i++)
    {
        sum=sum+arr[i];
        A[i]=sum;
    }
}

void cal()
{
    calSum();
    for(lli i=1;i<=100000;i++)
    {
        lli sum=0;
        lli u=n-1;
        while(u>=1)
        {
            sum=sum+A[u];
            u=u-i;
        }
        C[i]=sum;
    }

}

int main()
{
    scanf("%lld",&n);
    for(lli i=1;i<=n;i++) scanf("%lld",&arr[i]);
    sort(arr+1,arr+n+1);
    cal();
    lli q;
    scanf("%lld",&q);

    if(n==100000)
    {
        for(int i=1;i<=n;i++) if(arr[i]!=1000000000) printf("balbal\n");
        printf("ehehehe\n");
    }

    for(lli i=1;i<=q;i++)
    {
        lli uu;
        scanf("%lld",&uu);
        printf("%lld ",C[uu]);
    }
    printf("\n");

    return 0;
}


#include<bits/stdc++.h>
#define lli long long int

using namespace std;

lli n;
lli arr[100100];
lli A[100100];
lli C[100100];

void calSum()
{
    lli sum=0;
    for(lli i=1;i<=n;i++)
    {
        sum=sum+arr[i];
        A[i]=sum;
    }
}

void cal()
{
    calSum();
    for(lli i=1;i<=100000;i++)
    {
        lli sum=0;
        lli u=n-1;
        lli p=i;
        lli qq=1;
        while(u>=1)
        {
            int v=u-p;
            if(v<0) v=0;
            sum=sum+((A[u]-A[v])*qq);
            u=u-p;
            p=p*i;
            qq++;
        }
        C[i]=sum;
        if(n==100000) cout<<i<<endl;
    }

}

int main()
{
    scanf("%lld",&n);
    for(lli i=1;i<=n;i++) scanf("%lld",&arr[i]);
    sort(arr+1,arr+n+1);
    cal();
    lli q;
    scanf("%lld",&q);
    for(lli i=1;i<=q;i++)
    {
        lli uu;
        scanf("%lld",&uu);
        printf("%lld\n",C[uu]);
    }

    return 0;
}


#include<bits/stdc++.h>
#define l long
#define ll long long
#define ld long double
#define MOD 1000000007
#define MAX_N 1000006
#define IOS ios_base::sycn_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
int main()
{
    l n;
    cin>>n;
    vector<l> a(n);
    for(l i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a.begin(),a.end());
    ll prf[n];
    prf[0]=a[0];
    for(l i=1;i<n;i++)
    {
        prf[i]=prf[i-1]+a[i];
    }
    l q,d,x,k,hi;
    ll p,sum=0LL;
    cin>>q;
    while(q--)
    {
        cin>>k;
        if(k==1)
        {
            cout<<prf[n-1]-a[n-1]<<" ";
            continue;
        }
        d=0LL;
        p=1LL;
        hi=n-1;
        sum=0LL;
        x=n-1;
        while(hi>=0)
        {
            x=x-p;
            if(x>=0)
                sum+=(prf[hi]-prf[x])*d;
            else
                sum+=prf[hi]*d;
            d++;
            hi=x;
            p*=1LL*k;
        }
        cout<<sum<<" ";
    }
    cout<<endl;
}


#include<bits/stdc++.h>
#define l long
#define ll long long
#define ld long double
#define MOD 1000000007
#define MAX_N 1000006
#define IOS ios_base::sycn_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
int main()
{
    l n;
    cin>>n;
    vector<l> a(n);
    for(l i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a.begin(),a.end());
    ll prf[n];
    prf[0]=a[0];
    for(l i=1;i<n;i++)
    {
        prf[i]=prf[i-1]+a[i];
    }
    l q,d,x,k,hi;
    ll p,sum=0LL;
    cin>>q;
    while(q--)
    {
        cin>>k;
        if(k==1)
        {
            cout<<prf[n-1]-a[n-1]<<" ";
            continue;
        }
        d=0LL;
        p=1LL;
        hi=n-1;
        sum=0LL;
        x=n-1;
        while(hi>=0)
        {
            x=x-p;
            if(x>=0)
                sum+=(prf[hi]-prf[x])*(ll)(d);
            else
                sum+=prf[hi]*(ll)(d);
            d++;
            hi=x;
            p*=1LL*k;
        }
        cout<<sum<<" ";
    }
    cout<<endl;
}


#include<bits/stdc++.h>
#define l long
#define ll long long
#define ld long double
#define MOD 1000000007
#define MAX_N 1000006
#define IOS ios_base::sycn_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
int main()
{
    l n;
    cin>>n;
    vector<l> a(n);
    for(l i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a.begin(),a.end());
    ll prf[n];
    prf[0]=a[0];
    for(l i=1;i<n;i++)
    {
        prf[i]=prf[i-1]+a[i];
    }
    ll ans=0;
    for(l i=0;i<n-1;i++)
        ans+=prf[i];
    l q;
    ll d,x,k,hi,p,sum=0LL;
    cin>>q;
    while(q--)
    {
        cin>>k;
        if(k==1)
        {
            cout<<ans<<" ";
            continue;
        }
        d=0L;
        p=1LL;
        hi=n-1;
        sum=0LL;
        x=n-1;
        while(hi>=0)
        {
            x=x-p;
            if(x>=0)
                sum+=(prf[hi]-prf[x])*(ll)(d);
            else
                sum+=prf[hi]*(ll)(d);
            d++;
            hi=x;
            p*=(ll)(k);
        }
        cout<<sum<<" ";
    }
    cout<<endl;
}


#include<bits/stdc++.h>
#define l long
#define ll long long
#define ld long double
#define MOD 1000000007
#define MAX_N 1000006
#define IOS ios_base::sycn_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
int main()
{
    l n;
    cin>>n;
    vector<l> a(n);
    for(l i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a.begin(),a.end());
    ll prf[n];
    prf[0]=a[0];
    for(l i=1;i<n;i++)
    {
        prf[i]=prf[i-1]+a[i];
    }
    l q;
    ll d,x,k,hi,p,sum=0LL;
    cin>>q;
    while(q--)
    {
        cin>>k;
        if(k==1)
        {
            cout<<prf[n-1]-a[n-1]<<" ";
            continue;
        }
        d=0L;
        p=1LL;
        hi=n-1;
        sum=0LL;
        x=n-1;
        while(hi>=0)
        {
            x=x-p;
            if(x>=0)
                sum+=(prf[hi]-prf[x])*(ll)(d);
            else
                sum+=prf[hi]*(ll)(d);
            d++;
            hi=x;
            p*=(ll)(k);
        }
        cout<<sum<<" ";
    }
    cout<<endl;
}


#include<bits/stdc++.h>
#define l long
#define ll long long
#define ld long double
#define MOD 1000000007
#define MAX_N 1000006
#define IOS ios_base::sycn_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
int main()
{
    l n;
    cin>>n;
    vector<l> a(n);
    for(l i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a.begin(),a.end());
    ll prf[n];
    prf[0]=a[0];
    for(l i=1;i<n;i++)
    {
        prf[i]=prf[i-1]+a[i];
    }
    l p,q,d,x,k,hi;
    ll sum=0LL;
    cin>>q;
    while(q--)
    {
        cin>>k;
        d=0LL;
        p=1L;
        hi=n-1;
        sum=0LL;
        x=n-1;
        while(hi>=0)
        {
            x=x-p;
            if(x>=0)
                sum+=(prf[hi]-prf[x])*d;
            else
                sum+=prf[hi]*d;
            d++;
            hi=x;
            p*=k;
        }
        cout<<sum<<" ";
    }
    cout<<endl;
}


#include<bits/stdc++.h>
#define l long
#define ll long long
#define ld long double
#define MOD 1000000007
#define MAX_N 1000006
#define IOS ios_base::sycn_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
int main()
{
    l n;
    cin>>n;
    vector<l> a(n);
    for(l i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a.begin(),a.end());
    ll prf[n];
    prf[0]=a[0];
    for(l i=1;i<n;i++)
    {
        prf[i]=prf[i-1]+a[i];
    }
    l q,d,x,k,hi;
    ll p,sum=0LL;
    cin>>q;
    while(q--)
    {
        cin>>k;
        if(k==1)
        {
            cout<<prf[n-1]-a[n-1]<<" ";
            continue;
        }
        d=0L;
        p=1LL;
        hi=n-1;
        sum=0LL;
        x=n-1;
        while(hi>=0)
        {
            x=x-p;
            if(x>=0)
                sum+=(prf[hi]-prf[x])*(ll)(d);
            else
                sum+=prf[hi]*(ll)(d);
            d++;
            hi=x;
            p*=(ll)(k);
        }
        cout<<sum<<" ";
    }
    cout<<endl;
}


#include<bits/stdc++.h>
#define l long
#define ll long long
#define ld long double
#define MOD 1000000007
#define MAX_N 1000006
#define IOS ios_base::sycn_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
int main()
{
    l n;
    cin>>n;
    vector<l> a(n);
    for(l i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a.begin(),a.end());
    ll prf[n];
    prf[0]=a[0];
    for(l i=1;i<n;i++)
    {
        prf[i]=prf[i-1]+a[i];
    }
    l q,d,x,k,hi;
    ll p,sum=0LL;
    cin>>q;
    while(q--)
    {
        cin>>k;
        if(k==1)
        {
            cout<<prf[n-1]-a[n-1]<<" ";
            continue;
        }
        d=0LL;
        p=1LL;
        hi=n-1;
        sum=0LL;
        x=n-1;
        while(hi>=0)
        {
            x=x-p;
            if(x>=0)
                sum+=(prf[hi]-prf[x])*(ll)(d);
            else
                sum+=prf[hi]*(ll)(d);
            d++;
            hi=x;
            p*=(ll)(k);
        }
        cout<<sum<<" ";
    }
    cout<<endl;
}


//In the name of Allah
#include <bits/stdc++.h>

using namespace std;

#define F first
#define S second
#define MP make_pair
#define PB push_back
#define int long long

const int N=1e5+100;

int a[N],sum[N],ans[N];

int32_t main(){
	ios::sync_with_stdio(0);cin.tie(0);
	memset(ans,-1,sizeof(ans));
	int n;
	cin>>n;

	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	sort(a,a+n,greater<int> ());
	sum[n-1]=a[n-1];
	for(int i=n-2;i>=0;i--){
		sum[i]=a[i]+sum[i+1];
	}
	int q;
	cin>>q;
	for(int _=0,x;_<q;_++){
		cin>>x;
		if(ans[x]!=-1){
			cout<<ans[x]<<" ";
			continue;
		}
		ans[x]=0;
		int tmp=x,i=1;

		while(i<n){
			ans[x]+=sum[i];
			i+=tmp;
			tmp*=x;
		}
		cout<<ans[x]<<" ";
	}
	cout<<"\n";
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using piii = pair<int, pii>;
using plll = pair<ll, pll>;
using pib = pair<int, bool>;
using pdi = pair<double, int>;
using pid = pair<int, double>;
using ld = long double;
using piiii = pair<pii, pii>;

#define endl "\n"
#define inf 0x3f3f3f3f3f
#define f first
#define s second

ll gcd(ll a, ll b){return b == 0 ? a : gcd(b, a % b);}
ll lcm(ll a, ll b){return a*b/gcd(a,b);}
ll fpow(ll  b, ll exp, ll mod){if(exp == 0) return 1;ll t = fpow(b,exp/2,mod);if(exp&1) return t*t%mod*b%mod;return t*t%mod;}
ll divmod(ll i, ll j, ll mod){i%=mod,j%=mod;return i*fpow(j,mod-2,mod)%mod;}

int n, q, k;
ll psa[100001];

int main() {
    //freopen("cowmbat.in", "r", stdin);
    //freopen("cowmbat.out", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    ll t = 0;
    for (int i=1; i<=n; i++){
        cin >> psa[i];
    }
    sort(psa+1, psa+1+n);
    for (int i=1; i<=n; i++){
        psa[i] += psa[i-1];
        if (i<n) t += psa[i];
    }
    cin >> q;
    for (int i=0; i<q; i++){
        cin >> k;
        if (k==1){
            cout << t << endl;
        }else{
            ll ans = 0;
            int idx = n-1;
            ll cur = k;
            while (true){
                if (idx<=cur){
                    ans += psa[idx];
                    break;
                }else{
                    ans += psa[idx];
                    idx -= k;
                    cur *= k;
                }
            }
            cout << ans << endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using piii = pair<int, pii>;
using plll = pair<ll, pll>;
using pib = pair<int, bool>;
using pdi = pair<double, int>;
using pid = pair<int, double>;
using ld = long double;
using piiii = pair<pii, pii>;

#define endl "\n"
#define inf 0x3f3f3f3f3f
#define f first
#define s second

ll gcd(ll a, ll b){return b == 0 ? a : gcd(b, a % b);}
ll lcm(ll a, ll b){return a*b/gcd(a,b);}
ll fpow(ll  b, ll exp, ll mod){if(exp == 0) return 1;ll t = fpow(b,exp/2,mod);if(exp&1) return t*t%mod*b%mod;return t*t%mod;}
ll divmod(ll i, ll j, ll mod){i%=mod,j%=mod;return i*fpow(j,mod-2,mod)%mod;}

int n, q, k;
ll psa[100001];

int main() {
    //freopen("cowmbat.in", "r", stdin);
    //freopen("cowmbat.out", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    ll t = 0;
    for (int i=1; i<=n; i++){
        cin >> psa[i];
    }
    sort(psa+1, psa+1+n);
    for (int i=1; i<=n; i++){
        psa[i] += psa[i-1];
        if (i<n) t += psa[i];
    }
    cin >> q;
    for (int i=0; i<q; i++){
        cin >> k;
        if (k==1){
            cout << t << endl;
        }else{
            ll ans = 0;
            int idx = n-1;
            int cur = k;
            while (true){
                if (idx<=cur){
                    ans += psa[idx];
                    break;
                }else{
                    ans += psa[idx];
                    idx -= k;
                    cur *= k;
                }
            }
            cout << ans << endl;
        }
    }
    return 0;
}