ann8497

TollGate/Samsung

Aug 21st, 2019
1,137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. /*
  2. TESTED AND VERIFIED
  3. */
  4.  
  5. #include<iostream>
  6. using namespace std;
  7.  
  8. int n;
  9. int cost[25];
  10. int mens[25];
  11.  
  12. int ans;
  13.  
  14. void solve(int index, int one, int two, int three, int total){
  15.    
  16.     // optimization
  17.     if(total > ans)return;
  18.    
  19.     int menAvailable = one + two + three;
  20.    
  21.     // base case
  22.     if(index == n-1){
  23.         if(menAvailable >= mens[n-1])
  24.         ans = min(ans, total);
  25.         else
  26.         ans = min(ans, total + cost[n-1]);
  27.         return;
  28.     }
  29.    
  30.     // recursive case
  31.     // hire the men
  32.     solve(index + 1, one + mens[index], two, three, total + 2*cost[index]);
  33.     //pay the cost
  34.     solve(index + 1, one, two, three, total + cost[index]);
  35.     // fight the men
  36.     if(menAvailable >= mens[index]){
  37.         if(two + three < mens[index])one = menAvailable - mens[index];
  38.         if(three < mens[index])two = (two + three <= mens[index]) ? 0 : two - mens[index] + three;
  39.         solve(index + 1, 0, one, two, total);
  40.     }
  41.    
  42. }
  43.  
  44. int main(){
  45.    
  46.     cin>>n;
  47.    
  48.     for(int i = 0; i<n; i++)
  49.     cin>>mens[i]>>cost[i];
  50.    
  51.     ans = 1000000;
  52.    
  53.     solve(0,0,0,0,0);
  54.     cout<<ans<<endl;
  55.     return 0;
  56. }
Add Comment
Please, Sign In to add comment