Từ Trường THPT chuyên Lương Văn Tụy, Ninh Bình
Thông tin
include <bits/stdc++.h>
using namespace std; int n,k; bool kt(const vector<int>& a,int n,int k,int M) { int de=1,trolg=0; for(int i=0;i<n;++i) { if(trolg+a[i]<=M) { trolg += a[i]; } else { de++; trolg=a[i]; if (de>k)return false; } } return true; } void di() { cin>>n>>k; vector<int> a(n); int l=0,r=0; for (int i=0;i<n;++i) { cin>>a[i]; r+=a[i]; l=max(l,a[i]); } int kq=r; while(l<=r) { int m=(l+r)/2; if(kt(a,n,k,m)) { kq=m; r=m-1; } else { l=m+1; } } cout<<kq<<"\n"; } int main() { iosbase::syncwith_stdio(false); cin.tie(NULL);cout.tie(NULL); di(); return 0; }