Sreekar_0125

Untitled

Mar 3rd, 2024
1,021
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. //Author: D. Nisritha Reddy
  2. #include<bits/stdc++.h>
  3. #define int long long
  4. #define pb push_back
  5. #define ndl "\n"
  6. #define yes cout<<"YES\n"
  7. #define no cout<<"NO\n"
  8. #define mod (int)(1e9 + 7)
  9. #define F first
  10. #define S second
  11. #define vi vector<int>
  12. #define vc vector<char>
  13. #define vpi vector<pair<int,int>>
  14. #define pi pair<int,int>
  15. #define mi map<int, int>
  16. #define miv map<int,vector<int>>
  17. using namespace std;
  18.  
  19. template<class T>
  20. void inp(vector<T>&a){ for(auto &x : a)cin>>x; }
  21.  
  22. template<class T>
  23. void print(vector<T>&a){
  24.     for(auto &x: a)cout<<x<<" ";
  25.     cout<<ndl;
  26. }
  27.  
  28. template<class T>
  29. void print(T x){cout<<x<<" ";}
  30.  
  31. template<class T>
  32. void println(T x){ cout<<x<<"\n";}
  33.  
  34. template<class T>
  35. T maxEle(vector<T>&a){
  36.     T maxi = LLONG_MIN;
  37.     for(auto x : a)
  38.         if(x>maxi)maxi = x;
  39.     return maxi;
  40. }
  41.  
  42. template<class T>
  43. T minEle(vector<T>&a){
  44.     T mini = LLONG_MAX;
  45.     for(auto x: a)
  46.         if(x<mini) mini = x;
  47.     return mini;
  48. }
  49.  
  50. template<class T>
  51. void sortAll(vector<T>&a){ sort(a.begin(), a.end());}
  52.  
  53. template<class T>
  54. T sumAll(vector<T>&a){
  55.     T sum = 0;
  56.     for(auto x : a) sum +=x;
  57.     return sum;
  58. }
  59.  
  60. template<class T>
  61. void revAll(vector<T>&a){reverse(a.begin(),a.end());}
  62.  
  63.  
  64.  
  65. ///////////////////////////////////////////////////////////////////////////////////////////////////
  66.  
  67.  
  68. int n, k;
  69. vi a;
  70.  
  71. bool isPos(int mid){
  72.    
  73.     int sum = 0, grps = 1;
  74.    
  75.     for(int i = 0; i<n; i++){
  76.        
  77.         if(sum + a[i] > mid){
  78.             sum = 0; grps++;
  79.         }
  80.         sum += a[i];
  81.     }
  82.    
  83.     return grps <= k;
  84.    
  85. }
  86.  
  87. void solve(){
  88.    
  89.     cin>>n>>k;
  90.    
  91.     a.resize(n); inp(a);
  92.    
  93.     int l = maxEle(a), h = sumAll(a);
  94.     int ans = 0;
  95.    
  96.     while(l<=h){
  97.        
  98.         int mid = ( l + h ) >> 1;
  99.        
  100.         if(isPos(mid)){
  101.             ans = mid;
  102.             h = mid - 1;
  103.         }
  104.         else{
  105.            
  106.             l = mid + 1;
  107.         }
  108.        
  109.        
  110.     }
  111.    
  112.     cout<<ans<<"\n";
  113.    
  114. }
  115.  
  116.  
  117.  
  118. ///////////////////////////////////////////////////////////////////////////////////////////////////
  119.  
  120.  
  121.  
  122. signed main(){
  123.    
  124.     ios_base::sync_with_stdio(false);
  125.     cin.tie(NULL);
  126.  
  127.     int t = 1; //cin>>t;
  128.     while(t--){
  129.         solve();
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment