fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. using ll = long long;
  5. const ll neg = -(1LL << 60);
  6.  
  7. int main() {
  8.  
  9. int n, m, k;
  10. cin >> n >> m >> k;
  11.  
  12. vector<pair<int, ll>> a(n + 1);
  13. for (int i = 1; i <= n; i++) {
  14. cin >> a[i].first >> a[i].second;
  15. }
  16.  
  17. vector<vector<ll>> dp(k + 1, vector<ll>(m + 1, neg));
  18. dp[0][0] = 0;
  19.  
  20. for (int i = 1; i <= n; i++) {
  21. int w = a[i].first;
  22. ll v = a[i].second;
  23. for (int j = k; j >= 1; j--) {
  24. for (int s = m; s >= w; s--) {
  25. if (dp[j - 1][s - w] != neg) {
  26. dp[j][s] = max(dp[j][s], dp[j - 1][s - w] + v);
  27. }
  28. }
  29. }
  30. }
  31.  
  32. ll ans = neg;
  33. for (int s = 0; s <= m; s++) ans = max(ans, dp[k][s]);
  34.  
  35. cout << (ans == neg ? -1 : ans) << '\n';
  36. return 0;
  37. }
  38.  
Runtime error #stdin #stdout #stderr 0.01s 5292KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc