提交时间:2023-11-17 13:55:55
运行 ID: 110886
#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 l = 0,r = 0; cin >> n >> m; for(int i=1;i<=n;i++) cin>>a[i], r += a[i], l = max(l, a[i]); while(l<r) { int mid=(l+r)/2; if(chk(mid)) r=mid; else l=mid + 1; } cout<<l<<endl; }