Problem Description

There are n piles of stones of sizes a1, a2, ..., an lying on the table in front of you.

During one move you can take one pile and add it to the other. As you add pile i to pile j, the size of pile j increases by the current size of pile i, and pile i stops existing. The cost of the adding operation equals the size of the added pile.

Your task is to determine the minimum cost at which you can gather all stones in one pile.

To add some challenge, the stone piles built up conspiracy and decided that each pile will let you add to it not more than k times (after that it can only be added to another pile).

Moreover, the piles decided to puzzle you completely and told you q variants (not necessarily distinct) of what k might equal.

Your task is to find the minimum cost for each of q variants.

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of stone piles. The second line contains n space-separated integers: a1, a2, ..., an (1 ≤ ai ≤ 109) — the initial sizes of the stone piles.

The third line contains integer q (1 ≤ q ≤ 105) — the number of queries. The last line contains q space-separated integers k1, k2, ..., kq (1 ≤ ki ≤ 105) — the values of number k for distinct queries. Note that numbers ki can repeat.

Output

Print q whitespace-separated integers — the answers to the queries in the order, in which the queries are given in the input.

Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.

Examples

Input

5
2 3 4 1 1
2
2 3

Output

9 8

Note

In the first sample one way to get the optimal answer goes like this: we add in turns the 4-th and the 5-th piles to the 2-nd one; then we add the 1-st pile to the 3-rd one; we add the 2-nd pile to the 3-rd one. The first two operations cost 1 each; the third one costs 2, the fourth one costs 5 (the size of the 2-nd pile after the first two operations is not 3, it already is 5).

In the second sample you can add the 2-nd pile to the 3-rd one (the operations costs 3); then the 1-st one to the 3-th one (the cost is 2); then the 5-th one to the 4-th one (the costs is 1); and at last, the 4-th one to the 3-rd one (the cost is 2).

题意:给出 n 堆石头,每次可以将一堆石头放到另一堆里,代价是移动该堆的大小,现在有 q 次询问,每次询问给出一个值 k,代表每堆石头最多只能移动 k 次,问每次询问的最小代价

思路:

对于每组样例,首先对石堆进行排序,由于每堆对多被放 k 次,那么可将每堆石头视为一个结点,其度为 k,从而从头到尾构建一个 k 叉树,其代价为每一个结点的值*路径长度,并将代价越大的结点尽量放在最高层,这样就是一个 k 叉的霍夫曼树

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 500000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;LL a[N];
LL res[N],sum[N];
LL bucket[N];
int main() {LL n;scanf("%lld",&n);for(int i=1;i<=n;i++)scanf("%lld",&a[i]);sort(a+1,a+n+1);for(int i=1;i<=n;i++)sum[i]=sum[i-1]+a[i];int q;scanf("%d",&q);bool flag=true;int cnt=0;for(int i=1;i<=q;i++){if(n==1)flag=false;else{int k;scanf("%d",&k);if(k>n)k=n;cnt++;if(bucket[k]!=0)res[cnt]=bucket[k];else{LL left=0,right=n-1;LL index=0,tot=1;LL temp=1;while(true){temp*=k;index+=temp;left=max(n-index,(LL)1);res[cnt]+=(sum[right]-sum[left-1])*tot;right=left-1;if(left==1)break;tot++;}bucket[k]=res[cnt];}}}if(flag){for(int i=1; i<=q; i++){printf("%lld ",res[i]);}}else{for(int i=1; i<=q; i++){printf("0 ");}}printf("\n");return 0;
}

Naughty Stone Piles(CF-226B)相关推荐

  1. codeforce 227D Naughty Stone Piles (贪心+递归+递推)

    Description There are n piles of stones of sizes a1, a2, -, an lying on the table in front of you. D ...

  2. 【解题报告】随便练练二(CF 2300)

    [解题报告]随便练练二(CF 2300) A:Antimatter | CF383D 题意 思路 :DP 代码 B:Physical Education Lessons | CF915E 题意 思路一 ...

  3. Commentator problem(CF 2)

    题目链接 题目大意: 给定三个圆,询问是否存在点满足该点与三个圆夹角均相等,若存在多组解返回夹角最大值. 圆外一点到两圆夹角均相等: 即 sina = sinb = r1 / d1 = r2 / d2 ...

  4. D. Make a Power of Two(cf#739DIV3)

    D. Make a Power of Two 链接: link. 题意: 找出将数字转换为 2 的任意幂的最小移动次数. 题解: 先将2的xxx次幂的结果以字符串形式保存,输入字符串nnn后,因为存在 ...

  5. Web of Lies(CF 1548A)

    这是今天在打个人赛时碰见的一道题,是一道半图论半思维的题. Web of Lies 题目大意不难理解,在这里只需要注意一些细节.在加边时,只有当cnt[min]的值为1时答案才应该减1,而不是当cnt ...

  6. Magic Powder - 2 (CF 670_D)

    http://codeforces.com/problemset/problem/670/D2 The term of this problem is the same as the previous ...

  7. 【解题报告】博弈专场 (CF 2000~2200)前五题

    [解题报告]博弈专场 (CF 2000+)前五题 A:Fox and Card Game | CF388C 题意 思路 代码 B:Berzerk | CF786A 题意 思路 代码 C:Ithea P ...

  8. 软件设计师提纲+复习资料整理(上午题)

    文章目录 软件设计师考试大纲 上午题(选择题) 一.计算机组成原理 考点:CPU结构组成 考点:原码.反码.补码定点整数范围 考点:浮点数表示 考点:RISC和CISC计算机的区别 考点:奇校验与偶校 ...

  9. c性能大容量cket_5千左右预算,既轻薄(高颜值)又高性能的笔记本推荐(畅玩LOL、CF、DNF、流放之路、梦幻西游)...

    在目前笔记本制造技术中,轻薄和性能二者不可兼得,轻薄的本性能不高,高性能的太厚重, 这里推荐一些既轻薄,外观好看,同时又高性能的轻薄本. 很多轻薄本多为低压U+集成显卡,性能太弱,都不适合玩大型3D网 ...

最新文章

  1. golang异步协程调度原理
  2. App.config/Web.config 中特殊字符的处理
  3. learning material at the outer world
  4. 问题 | fatal: open /dev/null or dup failed: No Such file or directory
  5. ssl1615-Frogger【图论,最小生成树,并查集】
  6. Unit01: Servlet基础 、 HTTP协议
  7. 机器学习基础算法30-贝叶斯网络理论
  8. aspupload上传中文文件名乱码解决方法
  9. ecmobile php开发文档,ecmobile PHP接口说明文档之购物车(cart/create|list|detele|update)
  10. Ansys 2022 安装教程(附赠免费的安装包)
  11. VR全景图拍摄制作之无人机航拍
  12. ubuntu安装搜狗拼音输入法
  13. TIP 2021 | 重访CV经典!首个无监督深度学习图像拼接框架
  14. BIOS入口地址:FFFF:0000是指什么意思?在物理上是指内存条的什么位置?
  15. 系统错误null是什么意思_为什么NULL是错误的?
  16. 解决:Conda报错InvalidArchiveError
  17. dell进入u盘启动模式_台式戴尔电脑U盘装系统按F几是u盘启动
  18. bind blz mysql_MySQL™ 参考手册(通用安装指南)
  19. COUNT计算机公式,countif函数的使用方法(统计考勤函数计算公式)
  20. B端产品经理如何做好客户访谈?

热门文章

  1. [古诗十九首] 西北有高楼 —— 无名氏
  2. hibernate oracle查询最大值_Java大数据:Mybatis和Hibernate对比分析
  3. 进腾讯实习要交钱?腾讯2021年开除70名员工,拉黑13家公司
  4. 程序员终结者还是“白嫖”开源代码?GitHub推出的AI编程辅助工具陷入争议
  5. IDEA这样配置注释模板,让你高出一个逼格!!
  6. ThreadLocal到底有没有内存泄漏?从源码角度来剖析一波
  7. SpringCloud微服务:Ribbon和Feign组件,实现服务调用的负载均衡
  8. Linux系统:centos7下搭建Nginx和FastDFS文件管理中间件
  9. Linux IPC实践(9) --System V共享内存
  10. USACO翻译:USACO 2012 FEB Silver三题