Advertisement
paulomiranda98

TFG

Mar 2nd, 2021
1,298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <chrono>
  4. #include <random>
  5. #include <limits>
  6. #include <cassert>
  7.  
  8. std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());
  9.  
  10. const int ms = 1e6 + 10;
  11. const int mc = 1010;
  12.  
  13. long long dp[ms];
  14. int c[mc], lim[mc];
  15.  
  16. int main() {
  17.     std::ios_base::sync_with_stdio(false); std::cin.tie(NULL);
  18.     int n, s;
  19.     std::cin >> n >> s;
  20.     for(int i = 0; i < n; i++) {
  21.         int w, cost;
  22.         std::cin >> w >> cost;
  23.         c[w] = std::max(c[w], cost);
  24.     }
  25.     for(int i = 1; i < mc; i++) {
  26.         lim[i] = std::max(lim[i], lim[i-1]);
  27.         if(c[i] <= lim[i]) {
  28.             continue;
  29.         }
  30.         lim[i+1] = std::max(lim[i+1], c[i]);
  31.         for(int j = i; j < ms; j++) {
  32.             dp[j] = std::max(dp[j], dp[j-i] + c[i]);
  33.         }
  34.         for(int j = 2; i * j < mc; j++) {
  35.             lim[i*j] = std::max(lim[i*j], c[i] * j);
  36.         }
  37.     }
  38.     for(int i = 1; i < ms; i++) {
  39.         assert(dp[i] >= dp[i-1]);
  40.     }
  41.     long long ans = 0;
  42.     for(int i = 1; i < mc; i++) {
  43.         int freq = std::max(0, (s - ms) / i - 2);
  44.         while(s - freq * i >= ms) {
  45.             freq++;
  46.         }
  47.         ans = std::max(ans, (long long) freq * c[i] + dp[s - freq * i]);
  48.     }
  49.     std::cout << ans << '\n';
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement