Guest User

Untitled

a guest
Dec 17th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1. #include <iostream>
  2.  
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.     int n, max_weight;
  8.     cin >> n >> max_weight;
  9.     int w[n];
  10.     int c[n];
  11.  
  12.     for (int i = 0; i < n; i++) {
  13.         cin >> w[i];
  14.         cin >> c[i];
  15.     }
  16.  
  17.     /*cout << "n is " << n << endl;
  18.     cout << "max_weight is: " << max_weight << endl;
  19.  
  20.  
  21.     for (int i = 0; i < n; i++) {
  22.         cout << "weight[" << i << "] = " << w[i] << " and cost[" << i << "] = " << c[i] << endl;
  23.     }
  24.      */
  25.    
  26.     //если n = 3:
  27.     //000 - 0
  28.     //001 - 1
  29.     //010 - 2
  30.     //011 - 3
  31.     //100
  32.     //101
  33.     //110
  34.     //111
  35.     //
  36.     //
  37.     //110
  38.     //010
  39.    
  40.     int bestCost = 0;
  41.     int bestMask = 0;
  42.     for (int mask = 0; mask < (1<<n); mask++) {
  43.         int totalWeight = 0;
  44.         int totalCost = 0;
  45.         for (int j = 0; j < n; j++) {
  46.             if ((mask&(1<<j)) > 0) {
  47.                 totalWeight += w[j];
  48.                 totalCost += c[j];
  49.             }
  50.         }
  51.         if (totalWeight <= max_weight && totalCost > bestCost) {
  52.             bestCost = totalCost;
  53.             bestMask = mask;
  54.         }
  55.  
  56.     }
  57.     cout << bestCost << endl;
  58.     cout << bestMask << endl;
  59.     for (int i = 0; i < n; i++) {
  60.         if ((bestMask & (1<<i)) > 0)
  61.             cout << i << " ";
  62.     }
  63.     cout << endl;
  64.    
  65.  
  66.     return 0;
  67. }
Add Comment
Please, Sign In to add comment