fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 100005;
  5. long long a[N];
  6.  
  7. bool cmp(int i, int j)
  8. {
  9. return a[i] > a[j];
  10. }
  11.  
  12. int main() {
  13. int n, p, k;
  14. cin >> n >> p >> k;
  15. long long skills[n][p];
  16. long long dp[n + 1][1 << p];
  17. memset(dp, -1, sizeof dp);
  18. for(int i = 0; i < n; i++)
  19. {
  20. cin >> a[i];
  21. }
  22. for(int i = 0; i < n; i++)
  23. {
  24. for(int j = 0; j < p; j++)
  25. {
  26. cin >> skills[i][j];
  27. }
  28. }
  29. int ind[n];
  30. for(int i = 0; i < n; i++)
  31. {
  32. ind[i] = i;
  33. }
  34. sort(ind, ind + n, cmp);
  35. dp[0][0] = 0;
  36. for(int i = 0; i < n; i++)
  37. {
  38. int x = ind[i];
  39. for(long long mask = 0; mask < (1 << p); mask++)
  40. {
  41. if(dp[i][mask] == -1)
  42. {
  43. continue;
  44. }
  45. int inaud = i - __builtin_popcount(mask);
  46. if(inaud < k)
  47. {
  48. dp[i + 1][mask] = max(dp[i + 1][mask], dp[i][mask] + a[x]);
  49. }
  50. else
  51. {
  52. dp[i + 1][mask] = max(dp[i + 1][mask], dp[i][mask]);
  53. }
  54. for(int j = 0; j < p; j++)
  55. {
  56. if(((mask & (1 << j)) == 1) and (dp[i][mask ^ (1 << j)] != -1))
  57. {
  58. dp[i + 1][mask] = max(dp[i + 1][mask], dp[i][mask ^ (1 << j)] + skills[x][j]);
  59. }
  60. else if(((mask & (1 << j)) == 0))
  61. {
  62. dp[i + 1][mask | (1 << j)] = max(dp[i + 1][mask | (1 << j)], dp[i][mask] + skills[x][j]);
  63. }
  64. }
  65. cout << i + 1 << " " << mask << "\n";
  66. for(int j = 0; j < (1 << p); j++)
  67. {
  68. cout << dp[i + 1][j] << " ";
  69. }
  70. cout << "\n";
  71. }
  72. }
  73. cout << dp[n][(1 << p) - 1];
  74.  
  75. return 0;
  76. }
  77. /* 6 2 3
  78. 93 78 78 17 13 9
  79. 30 52
  80. 80 97
  81. 84 55
  82. 56 68
  83. 60 36
  84. 26 17 */
Success #stdin #stdout 0s 4352KB
stdin
6 2 3
93 78 78 17 13 9
30 52
80 97
84 55
56 68
60 36
26 17

stdout
1 0
93 30 52 -1 
2 0
171 173 190 -1 
2 1
171 173 190 127 
2 2
171 173 190 132 
3 0
249 255 226 -1 
3 1
249 255 226 228 
3 2
249 255 268 274 
3 3
249 255 268 274 
4 0
249 305 317 -1 
4 1
249 305 317 323 
4 2
249 305 317 324 
4 3
249 305 317 324 
5 0
249 309 285 -1 
5 1
249 309 285 341 
5 2
249 309 317 377 
5 3
249 309 317 377 
6 0
249 275 266 -1 
6 1
249 309 266 326 
6 2
249 309 317 343 
6 3
249 309 317 377 
377