Advertisement
tepyotin2

Team Building

Jul 6th, 2025
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. struct Person {
  8.     int id;
  9.     long long audience_strength;
  10.     vector<long long> player_strengths;
  11. };
  12.  
  13. bool comparePeople(const Person& a, const Person& b) {
  14.     return a.audience_strength > b.audience_strength;
  15. }
  16.  
  17. int main() {
  18.     ios_base::sync_with_stdio(false);
  19.     cin.tie(NULL);
  20.  
  21.     int n, p, k;
  22.     cin >> n >> p >> k;
  23.  
  24.     vector<Person> people(n);
  25.     for (int i = 0; i < n; ++i) {
  26.         people[i].id = i;
  27.         cin >> people[i].audience_strength;
  28.     }
  29.  
  30.     for (int i = 0; i < n; ++i) {
  31.         people[i].player_strengths.resize(p);
  32.         for (int j = 0; j < p; ++j) {
  33.             cin >> people[i].player_strengths[j];
  34.         }
  35.     }
  36.  
  37.     sort(people.begin(), people.end(), comparePeople);
  38.  
  39.     vector<vector<long long>> dp(n + 1, vector<long long>(1 << p, -1));
  40.  
  41.     dp[0][0] = 0;
  42.  
  43.     for (int i = 1; i <= n; ++i) {
  44.         for (int mask = 0; mask < (1 << p); ++mask) {
  45.             // the number of players
  46.             int num_players_in_mask = 0;
  47.             for(int j=0; j<p; ++j){
  48.                 if((mask >> j) & 1){
  49.                     num_players_in_mask++;
  50.                 }
  51.             }
  52.  
  53.             // what if people i-1 is audience
  54.             long long non_player_strength = -1;
  55.             if (dp[i - 1][mask] != -1) {
  56.                 int audience_candidates = i - num_players_in_mask;
  57.                 non_player_strength = dp[i - 1][mask];
  58.  
  59.                 if (audience_candidates <= k)
  60.                     non_player_strength = dp[i - 1][mask] + people[i - 1].audience_strength;
  61.             }
  62.             dp[i][mask] = non_player_strength;
  63.  
  64.             // what if people i-1 is player
  65.             for (int j = 0; j < p; ++j) {
  66.                 if ((mask >> j) & 1) {
  67.                     int prev_mask = mask ^ (1 << j);
  68.                     if (dp[i - 1][prev_mask] != -1) {
  69.                         long long player_strength = dp[i - 1][prev_mask] + people[i - 1].player_strengths[j];
  70.                         dp[i][mask] = max(dp[i][mask], player_strength);
  71.                     }
  72.                 }
  73.             }
  74.         }
  75.     }
  76.  
  77.     cout << dp[n][(1 << p) - 1] << endl;
  78.  
  79.     return 0;
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement