#include <bits/stdc++.h>
using namespace std;
bool fun(int mid, vector<int>arr,int m){
    int sum=0,count=0;
        for(int i=0;i<arr.size();i++){
            if(arr[i]>mid){
                return false;
            }
            else if(sum+arr[i]<=mid){
                sum+=arr[i];
                
            }
            else{
                sum=arr[i];//why is this needed..why not write sum=0;
                count++;
            }
        }
        return count+1<=m;
}
int main()
{
    vector<int>arr={5, 3, 20, 16, 18, 1, 10, 10, 9, 8};
    int n=arr.size();
    int m=3;
    int ans=-1;
    int l=1;
    int h=accumulate(arr.begin(),arr.end(),0);
    
    while(l<=h){
           int mid=l+(h-l)/2;
           //cout<<"mid"<<mid<<endl;
           if(fun(mid,arr,m)){
               ans=mid;
               h=mid-1;
           }
           else{
               l=mid+1;
           }
           //cout<<"ans"<<ans<<endl;
    }
    cout<<ans<<endl;
    return 0;
}