Codeforces551C:GukiZ hates Boxes

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

这题二分呢,主要就是check函数难写,这题有几个坑点,首先第一个要处理掉序列末尾的0,因为在最后的0是没有用的,不用理,所以要先处理,被坑了好久。然后就是check函数,这个函数的思想就是让每个人从头开始搬运,遇到时间不够的,就补充一个人,根据题意,每次补充一个人,我们都能减少temp-i-1这么多的时间,最后判断人数是否足够,东西是否搬完。

其实我觉得也可以从后面开始做,但是wa了,回去想想,先说下思路,就是因为最后面是肯定需要人去搬,所以从后向前,思路也是一样的,每一次派一个人,工作的时间就是temp-n(就是二分的时间减去走的路程),如果工作的时间多余那堆盒子数量,就搬下一堆的,然后堆的数量减一,如果少于呢,就继续派人去搬运。同样的道理到最后检查人数是否用完,东西是否搬完。

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

typedef long long LL;
const double eps=1e-8;
const int maxn=100005;
int n,m;
LL a[maxn],b[maxn],ans;
LL tot;

int check(LL temp)
{
    int cnt=m;
    LL sum=0;
    for(int i=0;i<n;i++)
    {
        sum+=a[i];
        while(sum+i+1>=temp)
        {
            sum-=temp-i-1;
            cnt--;
            if(cnt<0)
                return 0;
        }
    }
    if(cnt==0)
        return sum<=0;
    return 1;
}

int main()
{
	while(cin>>n>>m)
    {
        tot=0;
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
            tot+=a[i];
        }
        while(a[n-1]==0)
            n--;
        LL leftt=n+1,rightt=tot+n+1,mid;
        while(leftt<=rightt)
        {
            mid=(leftt+rightt)/2;
            //cout<<mid<<endl;
            if(check(mid))
                rightt=mid-1;
            else
                leftt=mid+1;
            //cout<<rightt<<" "<<leftt<<endl;
        }
        cout<<leftt<<endl;
    }
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Abandoninged/article/details/81188805