CodeForces

来源:互联网 发布:淘宝商品详情页面html 编辑:程序博客网 时间:2024/06/01 19:23

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.

Example
Input
2 11 1
Output
4
Input
3 21 0 2
Output
5
Input
4 1003 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 (1 second) 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.



题意:找出最小的时间让箱子全部搬完,一个人经过1s要么向前1步要么在当前位置搬走一个箱子 首先二分寻找最小花费时间   因为每个人都有相同多时间 那么我们可以假设人是一个一个派出去的  因为走路是要花费时间的所以花在走路上的时间越短越好 其他时间都可以用来搬箱子 那么 我们可以先把第一个派去搬最末的箱子 从后面开始搬 那么后来的人花在走路的时间肯定会减少

#include<cstdio>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>#include<map>#include<vector>#include<queue>#define  LL  long longconst LL INF=1e18;using namespace std;LL n,m,mid;LL a[110000];LL aa(LL u){    LL p=n,q;    while(!a[p]) //找到第一个不为空的末尾箱        p--;    q=a[p];  for(LL i=1;i<=m;i++)//可以派m个人  {    LL o=u; //每个人时间都为u    o-=p; //减去走路时间    if(o<=0)  //如果连走路时间都不够        return 0;    while(o)     {        if(o>=q) //时间多余时 会用多余时间搬前面的箱子 然后后面箱子全搬完        {            o-=q;             p--;            while(!a[p])              p--;            if(p<1) //1号箱子也搬完了                 return 1;            q=a[p];        }        else //时间不够 留给下个人搬        {            q-=o;            break;        }    }  }  return 0;}int main(){ scanf("%I64d%I64d",&n,&m); for(LL i=1;i<=n;i++)     scanf("%I64d",&a[i]);    LL l=0,r=INF; while(l<r) //二分时间 {     mid=(l+r)/2;     if(aa(mid))      r=mid;      else        l=mid+1; } printf("%I64d\n",r);}



原创粉丝点击