mickypinata

SMMR-T021: Shopping

Jun 1st, 2021 (edited)
611
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long lli;
  5.  
  6. const int N = 10;
  7.  
  8. int price[N + 10], value[N + 10];
  9.  
  10. int main(){
  11.  
  12.     int nItem, money;
  13.     scanf("%d%d", &nItem, &money);
  14.     for(int i = 0; i < nItem; ++i){
  15.         scanf("%d%d", &price[i], &value[i]);
  16.     }
  17.  
  18.     lli mx = 0;
  19.     for(int i = 1; i < (1 << nItem); ++i){
  20.         lli sumPrice = 0;
  21.         lli sumValue = 0;
  22.         for(int j = 0; j < nItem; ++j){
  23.             if((i & (1 << j)) != 0){
  24.                 sumPrice += price[j];
  25.                 sumValue += value[j];
  26.                 if(sumPrice > money){
  27.                     break;
  28.                 }
  29.             }
  30.         }
  31.         if(sumPrice <= money){
  32.             mx = max(mx, sumValue);
  33.         }
  34.     }
  35.     cout << mx;
  36.  
  37.     return 0;
  38. }
  39.  
Add Comment
Please, Sign In to add comment