Monthly Expense

Farmer John is an astounding accounting wizard and has realized he might run out of money to run the farm. He has already calculated and recorded the exact amount of money (1 ≤ moneyi ≤ 10,000) that he will need to spend each day over the next N (1 ≤ N ≤ 100,000) days.

FJ wants to create a budget for a sequential set of exactly M (1 ≤ M ≤ N) fiscal periods called "fajomonths". Each of these fajomonths contains a set of 1 or more consecutive days. Every day is contained in exactly one fajomonth.

FJ's goal is to arrange the fajomonths so as to minimize the expenses of the fajomonth with the highest spending and thus determine his monthly spending limit.

Input

Line 1: Two space-separated integers: N and M 
Lines 2.. N+1: Line i+1 contains the number of dollars Farmer John spends on the ith day

Output

Line 1: The smallest possible monthly limit Farmer John can afford to live with.

Sample Input

7 5
100
400
300
100
500
101
400

Sample Output

500

Hint

If Farmer John schedules the months so that the first two days are a month, the third and fourth are a month, and the last three are their own months, he spends at most $500 in any month. Any other method of scheduling gives a larger minimum monthly limit.

#include <iostream>
#include   <cstring>
#include   <cstdlib>
#include    <cctype>
#include    <cstdio>
#include    <string>
using namespace std;
char h[110];
int have[5],need[100000+10],money[5],temp[5];
long long  k;
int n,m;
bool sloved(int mid){
    int sum=0,ans=1;
    for(int i=1;i<=n;i++){
        sum+=need[i];
        if(sum>mid){
            sum=need[i];
            ans++;
        }
    }
    if(ans>m)return 0;
        return 1;

}
int main()
{
    scanf("%d%d",&n,&m);
    int maxi=0;
    for(int i=1;i<=n;i++){
        scanf("%d",&need[i]);
        maxi=max(maxi,need[i]);
    }
    int l=maxi;
    int r=0x7ffffff;
    int mid;
    int ans=0;
    while(l<=r){
        mid=(l+r)/2;//cout <<mid<<endl;
        if(sloved(mid)){
            ans=mid;
            r=mid-1;
        }else{

        l=mid+1;
        }
    }
    cout <<ans<<endl;

    //cout << "Hello world!" << endl;
    return 0;
}

 

全部评论

相关推荐

牛客吹哨人:哨哥晚点统一更新到黑名单:能救一个是一个!26届毁意向毁约裁员黑名单https://www.nowcoder.com/discuss/1525833
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务