Advertisement
AlejandroGY

Untitled

Apr 24th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <array>
  5.  
  6. int main( ) {
  7.     std::ios_base::sync_with_stdio(0);
  8.     std::cin.tie(0);
  9.  
  10.     int s, n;
  11.     std::cin >> s >> n;
  12.  
  13.     std::vector<std::array<int, 2>> v(n + 1);
  14.     for (int i = 0; i < n; ++i) {
  15.         std::cin >> v[i][0] >> v[i][1];
  16.     }
  17.  
  18.     int memo[2][s + 1];
  19.     int* act = &memo[0][0];
  20.     int* ant = &memo[1][0];
  21.     for (int i = n; i >= 0; --i, std::swap(act, ant)) {
  22.         for (int c = 0; c <= s; ++c) {
  23.             if (i == n) {
  24.                 act[c] = 0;
  25.             } else {
  26.                 int res = ant[c];
  27.                 if (v[i][0] <= c) {
  28.                     res = std::max(res, v[i][1] + ant[c - v[i][0]]);
  29.                 }
  30.                 act[c] = res;
  31.             }
  32.         }
  33.     }
  34.     std::cout << ant[s];
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement