fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int INF = -1e9;
  5. int maximumTotalWeight(int k, int n, int m, vector<pair<int,int>>& clusters) {
  6. // Initialize the dp array with size [k+1][n+1][m+1]
  7. // dp[i][x][y] represents the maximum total weight when considering the first i clusters,
  8. // with x elements chosen for group 1 and y elements chosen for group 2
  9. vector<vector<vector<int>>> dp(k+1, vector<vector<int>>(n+1, vector<int>(m + 1, INF)));
  10.  
  11. dp[0][0][0] = 0; // Base case: no clusters considered, no elements chosen, total weight is 0
  12.  
  13. // Iterate through all clusters
  14. for(int i = 1; i <= k; i++) {
  15. int weightOfFirstGroup = clusters[i - 1].first;
  16. int weightOfSecondGroup = clusters[i - 1].second;
  17.  
  18. for(int x = 0; x <= n; x++) {
  19. for(int y = 0; y <= m; y++) {
  20. // Case 1: Do not choose this cluster
  21. dp[i][x][y] = dp[i-1][x][y];
  22.  
  23. // Case 2: Choose this cluster for group 1
  24. if(x > 0) {
  25. dp[i][x][y] = max(dp[i][x][y], dp[i-1][x-1][y] + weightOfFirstGroup);
  26. }
  27.  
  28. // Case 3: Choose this cluster for group 2
  29. if(y > 0) {
  30. dp[i][x][y] = max(dp[i][x][y], dp[i-1][x][y-1] + weightOfSecondGroup);
  31. }
  32. }
  33. }
  34. }
  35.  
  36. return dp[k][n][m];
  37. }
  38.  
  39. int main() {
  40. int k, n, m; cin >> k >> n >> m;
  41. vector<pair<int,int>> clusters(k);
  42. for(auto& x : clusters) cin >> x.first >> x.second;
  43.  
  44. cout << maximumTotalWeight(k, n, m, clusters);
  45.  
  46. return 0;
  47. }
Success #stdin #stdout 0s 5296KB
stdin
4 2 1
4 9
3 5
7 2
5 5
stdout
21