Problem p00496 (C++) — buggy submission, verdict: Memory Limit Exceeded

class in {
  struct my_iterator {
    int it;
    const bool rev;
    explicit constexpr my_iterator(int it_, bool rev = false)
        : it(it_), rev(rev) {}
    int operator*() { return it; }
    bool operator!=(my_iterator &r) { return it != r.it; }
    void operator++() { rev ? --it : ++it; }
  };
  const my_iterator i, n;

public:
  explicit constexpr in(int n) : i(0), n(n) {}
  explicit constexpr in(int i, int n) : i(i, n < i), n(n) {}
  const my_iterator &begin() { return i; }
  const my_iterator &end() { return n; }
};

#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, t, s;
  cin >> n >> t >> s;
  vector<int> v(n), w(n);
  for (int i : in(n))
    cin >> v[i] >> w[i];
  vector<vector<int>> dp(n + 1, vector<int>(s + 1));

  for (int i : in(n))
    for (int j : in(s + 1)) {
      if (j < w[i])
        dp[i + 1][j] = dp[i][j];
      else
        dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
    }

  vector<int> memo(n + 1, 0);
  for (int i : in(n + 1))
    memo[i] = *max_element(dp[i].begin(), dp[i].end());

  dp.assign(n + 1, vector<int>(t - s + 1, 0));
  for (int i : in(n - 1, -1))
    for (int j : in(t - s + 1)) {
      if (j < w[i])
        dp[i][j] = dp[i + 1][j];
      else
        dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - w[i]] + v[i]);
    }

  int ans = 0;
  for (int i : in(n + 1)) {
    int ma = *max_element(dp[i].begin(), dp[i].end());
    ans = max(ans, ma + memo[i]);
  }
  cout << ans << endl;
  return 0;
}

Error: MLE

Fixed submission:

class in {
  struct my_iterator {
    int it;
    const bool rev;
    explicit constexpr my_iterator(int it_, bool rev = false)
        : it(it_), rev(rev) {}
    int operator*() { return it; }
    bool operator!=(my_iterator &r) { return it != r.it; }
    void operator++() { rev ? --it : ++it; }
  };
  const my_iterator i, n;

public:
  explicit constexpr in(int n) : i(0), n(n) {}
  explicit constexpr in(int i, int n) : i(i, n < i), n(n) {}
  const my_iterator &begin() { return i; }
  const my_iterator &end() { return n; }
};

#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, t, s;
  cin >> n >> t >> s;
  vector<int> v(n), w(n);
  for (int i : in(n))
    cin >> v[i] >> w[i];
  vector<vector<int>> dp(n + 1, vector<int>(t + 1));
  auto dp2 = dp;

  for (int i : in(n))
    for (int j : in(s + 1)) {
      if (j < w[i])
        dp[i + 1][j] = dp[i][j];
      else
        dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
    }

  for (int i : in(n - 1, -1))
    for (int j : in(t + 1)) {
      if (j < w[i])
        dp2[i][j] = dp2[i + 1][j];
      else
        dp2[i][j] = max(dp2[i + 1][j], dp2[i + 1][j - w[i]] + v[i]);
    }
  int ans = 0;

  for (int i : in(n + 1)) {
    int ma = 0, id = 0;
    for (int j : in(s + 1))
      if (ma < dp[i][j]) {
        ma = dp[i][j];
        id = j;
      }
    int ma2 = 0;
    if (i < n)
      for (int j : in(t - s + 1))
        ma2 = max(ma2, dp2[i][j]);
    ans = max(ans, ma + ma2);
  }
  cout << ans << endl;
  return 0;
}