二分三分 K CodeForces-551C GukiZ hates Boxes 贪心&二分

GukiZ hates Boxes

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Professor GukiZ is concerned about making his way to school, because massive piles of boxes are blocking his way.

In total there are n piles of boxes, arranged in a line, from left to right, i-th pile (1 ≤ i ≤ n) containing ai boxes. Luckily, m students are willing to help GukiZ by removing all the boxes from his way. Students are working simultaneously. At time 0, all students are located left of the first pile. It takes one second for every student to move from this position to the first pile, and after that, every student must start performing sequence of two possible operations, each taking one second to complete. Possible operations are:

  1. If i ≠ n, move from pile i to pile i + 1;
  2. If pile located at the position of student is not empty, remove one box from it.

GukiZ's students aren't smart at all, so they need you to tell them how to remove boxes before professor comes (he is very impatient man, and doesn't want to wait). They ask you to calculate minumum time t in seconds for which they can remove all the boxes from GukiZ's way. Note that students can be positioned in any manner after t seconds, but all the boxes must be removed.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 105), the number of piles of boxes and the number of GukiZ's students.

The second line contains n integers a1, a2, ... an (0 ≤ ai ≤ 109) where ai represents the number of boxes on i-th pile. It's guaranteed that at least one pile of is non-empty.

Output

In a single line, print one number, minimum time needed to remove all the boxes in seconds.

Examples

input

2 1
1 1

output

4

input

3 2
1 0 2

output

5

input

4 100
3 4 5 4

output

5

Note

First sample: Student will first move to the first pile (1 second), then remove box from first pile (1 second), then move to the second pile (1second) and finally remove the box from second pile (1 second).

Second sample: One of optimal solutions is to send one student to remove a box from the first pile and a box from the third pile, and send another student to remove a box from the third pile. Overall, 5 seconds.

Third sample: With a lot of available students, send three of them to remove boxes from the first pile, four of them to remove boxes from the second pile, five of them to remove boxes from the third pile, and four of them to remove boxes from the fourth pile. Process will be over in 5 seconds, when removing the boxes from the last pile is finished.

终于补上这道题hiahiahia

最最最开始遇见这道题的时候,一直以为教授也动emmm,英文贼差

首先,有个大坑:最后有可能有几个0。惊了!

那么我们假设,最后一个非零的位置是k,那么,最短的时间至少为 k+1(许多学生可以一起搬走这个地方的许多箱子),而最长时间就是只有一个人的时候了,箱子总数+k。

然后我们就可以进行愉快的二分辣~

但是,问题又出现了,在某个时间值时,能否满足条件并完成任务呢?

我们可以通过,比较每种时间情况下,完成任务所需要的人数,与实际给的人数,来进行判断。

贪心真的很不好理解辣,可以这样想,每多一个人,这个人在当前行程下就可以搬 t - i 个箱子,所要花费的时间就少了这些(不同步->同步),继续前进又要增加花费时间(但走路本来就是同步动作,故不需要考虑所加人之后地步数),所以每到花费时间超出所给时间时,就要增加一个人,这样不断地贪心下去。

真难想嘤!

#include <cstdio>

const int maxn=100000;

int n,m;
long a[maxn+10];
int flag;

int cal(long long t)
{
	long long al=0;
	int s=0;	//注!从0开始!好好理解!

	for(int i=0;i<=flag;++i){
		al+=a[i];
		while(al+i+1>=t){	//注!是个循环!>=!
			al-=(t-i-1);		//当前路程下,每增加一个人,这个人就有(t-i-1)的时间用来搬箱子。后来的al中加上啦!
			if(++s>m)
				return 0;
		}
	}
	if(al>0&&s==m) return 0;	//注还有这种情况
	return 1;
}
//判断需要的人数是否小于等于实际的人数-没想到的点
//贪心真难想嘤嘤嘤

int main()
{
	long long left=0,right=0,mid;	//用long long!

	scanf("%d%d",&n,&m);
	for(int i=0;i<n;++i){
		scanf("%ld",&a[i]);
		if(a[i]) flag=i;	//注意!!!应是算到最后一个非零!!!坑!!!
		right+=a[i];
	}
	left=flag+2;		//可以很多人一起搬完这样子!!!
	right+=(flag+1);
	while(right>left){
		mid=(left+right)/2;
		if(cal(mid))
			right=mid;
		else
			left=mid+1;
	}
	printf("%lld\n",right);

	return 0;
}

猜你喜欢

转载自blog.csdn.net/DADDY_HONG/article/details/81254413