Advertisement
paulomiranda98

Icaro

Mar 2nd, 2021
1,279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define int long long
  3. using namespace std;
  4. typedef pair<int, int> ii;
  5. typedef long long ll;
  6.  
  7. int n, s;
  8. vector<ii> item;
  9.  
  10. vector<int> mul(vector<int> a, vector<int> b) {
  11.   vector<int> ans(2001, -1e18);
  12.   for(int i = 0; i < a.size(); ++i) {
  13.     for(int j = 0; j < a.size(); ++j) {
  14.       int id = (i + j) - 1000;
  15.       if(0 <= id && id < a.size()){
  16.         ans[id] = max(ans[id], a[i] + b[j]);
  17.       }
  18.     }
  19.   }
  20.   return ans;
  21. }
  22.  
  23. int32_t main(){
  24.   ios::sync_with_stdio(0); cin.tie(0);
  25.   vector<int> dp;
  26.   cin >> n >> s;
  27.   for(int i = 0; i < n; ++i) {
  28.     int c, w;
  29.     cin >> w >> c;
  30.     item.push_back({c, w});
  31.   }
  32.   int m = 1000;
  33.   dp.resize(2002, 0);
  34.   for(int i = 0; i <= m; ++i) {
  35.     for(int j = 0; j < n; ++j) {
  36.       if(item[j].second <= i) {
  37.         dp[i] = max(dp[i], dp[i - item[j].second] + item[j].first);
  38.       }
  39.     }
  40.   }
  41.   vector<int> ans(2001, -1e18);
  42.   vector<int> aux(2001, -1e18);
  43.   ans[m] = 0;
  44.   for(int i = m; i < ans.size(); ++i) {
  45.     aux[i - 1] = dp[i - m];
  46.   }
  47.   while(s) {
  48.     if(s & 1) ans = mul(ans, aux);
  49.     aux = mul(aux, aux);
  50.     s >>= 1;
  51.   }
  52.   cout << ans[m] << '\n';
  53.   return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement