Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Author: D. Nisritha Reddy
- #include<bits/stdc++.h>
- #define int long long
- #define pb push_back
- #define ndl "\n"
- #define yes cout<<"YES\n"
- #define no cout<<"NO\n"
- #define mod (int)(1e9 + 7)
- #define F first
- #define S second
- #define vi vector<int>
- #define vc vector<char>
- #define vpi vector<pair<int,int>>
- #define pi pair<int,int>
- #define mi map<int, int>
- #define miv map<int,vector<int>>
- using namespace std;
- template<class T>
- void inp(vector<T>&a){ for(auto &x : a)cin>>x; }
- template<class T>
- void print(vector<T>&a){
- for(auto &x: a)cout<<x<<" ";
- cout<<ndl;
- }
- template<class T>
- void print(T x){cout<<x<<" ";}
- template<class T>
- void println(T x){ cout<<x<<"\n";}
- template<class T>
- T maxEle(vector<T>&a){
- T maxi = LLONG_MIN;
- for(auto x : a)
- if(x>maxi)maxi = x;
- return maxi;
- }
- template<class T>
- T minEle(vector<T>&a){
- T mini = LLONG_MAX;
- for(auto x: a)
- if(x<mini) mini = x;
- return mini;
- }
- template<class T>
- void sortAll(vector<T>&a){ sort(a.begin(), a.end());}
- template<class T>
- T sumAll(vector<T>&a){
- T sum = 0;
- for(auto x : a) sum +=x;
- return sum;
- }
- template<class T>
- void revAll(vector<T>&a){reverse(a.begin(),a.end());}
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- int n, k;
- vi a;
- bool isPos(int mid){
- int sum = 0, grps = 1;
- for(int i = 0; i<n; i++){
- if(sum + a[i] > mid){
- sum = 0; grps++;
- }
- sum += a[i];
- }
- return grps <= k;
- }
- void solve(){
- cin>>n>>k;
- a.resize(n); inp(a);
- int l = maxEle(a), h = sumAll(a);
- int ans = 0;
- while(l<=h){
- int mid = ( l + h ) >> 1;
- if(isPos(mid)){
- ans = mid;
- h = mid - 1;
- }
- else{
- l = mid + 1;
- }
- }
- cout<<ans<<"\n";
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- signed main(){
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- int t = 1; //cin>>t;
- while(t--){
- solve();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment