Drying
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7684   Accepted: 1967

Description

It is very hard to wash and especially to dry clothes in winter. But Jane is a very smart girl. She is not afraid of this boring process. Jane has decided to use a radiator to make drying faster. But the radiator is small, so it can hold only one thing at a time.

Jane wants to perform drying in the minimal possible time. She asked you to write a program that will calculate the minimal time for a given set of clothes.

There are n clothes Jane has just washed. Each of them took ai water during washing. Every minute the amount of water contained in each thing decreases by one (of course, only if the thing is not completely dry yet). When amount of water contained becomes zero the cloth becomes dry and is ready to be packed.

Every minute Jane can select one thing to dry on the radiator. The radiator is very hot, so the amount of water in this thing decreases by k this minute (but not less than zero — if the thing contains less than k water, the resulting amount of water will be zero).

The task is to minimize the total time of drying by means of using the radiator effectively. The drying process ends when all the clothes are dry.

Input

The first line contains a single integer n (1 ≤ n ≤ 100 000). The second line contains ai separated by spaces (1 ≤ ai ≤ 109). The third line contains k (1 ≤ k ≤ 109).

Output

Output a single integer — the minimal possible number of minutes required to dry all clothes.

Sample Input

sample input #1
3
2 3 9
5 sample input #2
3
2 3 6
5

Sample Output

sample output #1
3 sample output #2
2
 /*

 题意:烤衣服,每分钟水量少k。自然干,每分钟少1.
给10^5件衣服的水量。
求最小的时间,全部的衣服都干了。
不能理解成,烤衣服的时候,也自然干。k已经包括了。 二分时间。对于时间mid判断能否实现。 */ #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std; typedef __int64 LL;
LL a[]; bool fun(LL x,LL n,LL m)
{
LL i,num=,cur;
if( m== )
{
for(num=-,i=;i<=n;i++)
if(a[i]>num) num=a[i];
}
else
{
for(i=,num=;i<=n;i++)
{
cur=a[i]-x;
if( cur<=) continue;
if(cur%(m-)==)
num=num+cur/(m-);
else num=num+cur/(m-)+;
}
}
if( num<=x)return true;
else return false;
}
int main()
{
LL n,i,l,r,mid,m;
bool cur;
while(scanf("%I64d",&n)>)
{
for(i=;i<=n;i++)
scanf("%I64d",&a[i]);
scanf("%I64d",&m);
for(i=,l=,r=;i<=n;i++)
{
if(a[i]%m==)
r=r+a[i]/m;
else r=r+a[i]/m+;
}
while(l<r)
{
mid=(l+r)/;
cur=fun(mid,n,m);
if( cur==true)
r=mid;
else l=mid+;
}
printf("%I64d\n",r);
}
return ;
}

最新文章

  1. Android笔记——BaseAdapter的使用
  2. 学习笔记 ACCESS 延迟注入
  3. 7.echo(),print(),print_r()的区别
  4. iTextSharp带中文转换出来的PDF文档显示乱码
  5. mysql-linux命令登录,退出
  6. 【转】Java日期计算之Joda-Time
  7. Ruby--String
  8. [转] 解析Qt资源文件使用
  9. org.springframework.web.servlet.view.InternalResourceViewResolver
  10. 【转】深入理解Java内存模型(七)——总结
  11. Dive in python Chapter4 实例
  12. vscode调试html页面,及配置说明
  13. 浅谈localStorage、sessionStorage 与cookie
  14. CPanel/服务器文件及目录
  15. ASP.NET AJAX入门系列(3):使用ScriptManagerProxy控件
  16. Excel添加下拉菜单
  17. iphone手机safari浏览器访问网站滚动条不显示问题解决办法
  18. INtellJ IDEA 2017 创建Annotation注解类
  19. LeetCode-37.解数独
  20. 《剑指offer》— JavaScript(7)斐波那契数列

热门文章

  1. 学习xss模拟构造攻击(第一篇)
  2. Elasticsearch基础知识
  3. (四)SSO之CAS框架单点登录,自定义验证登录方式
  4. 关于iframe切换的问题
  5. FlowPortal-BPM——文件目录功能
  6. sparksql连接mysql
  7. golang (4) golang 操作mongdb
  8. Zynq-7000 FreeRTOS(二)中断:PL中断请求
  9. Sublime Text 3新建工程
  10. idea中web.xml报错 Servlet should have a mapping