Advertisement
tepyotin2

Optimal Work Division

May 1st, 2025
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int n, k;
  6. int task[21];
  7. vector<int> mach;
  8. int ans = INT_MAX;
  9.  
  10. void dfs(int idx){
  11.     if(idx==n){
  12.         int value = *max_element(mach.begin(), mach.end());
  13.         ans = min(ans, value);
  14.         return;
  15.     }
  16.     for(int i=0; i<k; i++){
  17.         if(mach[i]+task[idx]<=ans){
  18.             mach[i]+=task[idx];
  19.             dfs(idx+1);
  20.             mach[i]-=task[idx];
  21.         }
  22.         if(mach[i]==0) break;
  23.     }
  24. }
  25.  
  26. int main(){
  27.     //freopen("workdivision.in", "r", stdin);
  28.    
  29.     cin >> n >> k;
  30.     mach.resize(k, 0);
  31.     for(int i=0; i<n; i++){
  32.         cin >> task[i];
  33.     }
  34.     sort(task, task+n, greater<int>());
  35.     //for(int i=0; i<n; i++){
  36.         //cout << task[i] << '\n';
  37.     //}
  38.     dfs(0);
  39.     cout << ans << '\n';
  40.    
  41.     return 0;
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement