Untitled

mail@pastecode.io avatar
unknown
c_cpp
4 months ago
561 B
2
Indexable
int a[200005],n,k;
int chk(int m){
    int c=1, s=0;
    for(int i=0; i<n; ++i){
        if (a[i]>m) return 0;
        if (s+a[i]>m) {
            c++; s=a[i];
        }
        else s+=a[i];
    }
    return c;
}
 
void sol(){
    cin >> n >> k;
    int m=1e18;
    for(int i=0; i<n; ++i){
        cin >> a[i];
    }
    int l=0, r=1e18, mid;
    while(l<=r){
        mid=(l+r)>>1;
        int cnt=0, mcnt=0;
        if(chk(mid)<=k){
            r=mid-1;
            m=mid;
        }
        else l=mid+1;
    }
    cout << m;
}
Leave a Comment