提交时间:2023-11-17 13:54:08

运行 ID: 110882

#include<bits/stdc++.h> using namespace std; int a[100010], n, m; bool chk(int k){ int now=0,tot=0; for(int i=1;i<=n;i ++){ if(now+a[i] > k) { tot++; now=0; } now += a[i]; } if(now > 0)tot++; return tot <= m; } int main() { int r = 0; cin >> n >> m; for(int i=1;i<=n;i++) cin>>a[i], r += a[i]; int l=1; while(l<r) { int mid=(l+r)/2; if(chk(mid)) r=mid; else l=mid + 1; } cout<<l<<endl; }