alexjercan/bugnet huggingface dataset:
Problem p00496 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;

int a[3000], b[3000];
int dp1[3001][3001], dp2[3001][3001];

int main() {
  int n, t, s;
  scanf("%d%d%d", &n, &t, &s);
  rep(i, n) scanf("%d%d", &a[i], &b[i]);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= s; j++) {
      if (b[i] > j)
        dp1[i + 1][j] = dp1[i][j];
      else
        dp1[i + 1][j] = max(dp1[i][j], dp1[i][j - b[i]] + a[i]);
    }
  }
  reverse(a, a + n);
  reverse(b, b + n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= t - s; j++) {
      if (b[i] > j)
        dp2[i + 1][j] = dp2[i][j];
      else
        dp2[i + 1][j] = max(dp2[i][j], dp2[i][j - b[i]] + a[i]);
    }
  }
  int Max = 0;
  for (int i = 0; i < n; i++)
    Max = max(Max, dp1[i][s] + dp2[n - i - 1][t - s]);
  printf("%d\\n", Max);
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;

int a[3000], b[3000];
ll dp1[3001][3001], dp2[3001][3001];

int main() {
  int n, t, s;
  scanf("%d%d%d", &n, &t, &s);
  rep(i, n) scanf("%d%d", &a[i], &b[i]);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= s; j++) {
      if (b[i] > j)
        dp1[i + 1][j] = dp1[i][j];
      else
        dp1[i + 1][j] = max(dp1[i][j], dp1[i][j - b[i]] + a[i]);
    }
  }
  reverse(a, a + n);
  reverse(b, b + n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= t - s; j++) {
      if (b[i] > j)
        dp2[i + 1][j] = dp2[i][j];
      else
        dp2[i + 1][j] = max(dp2[i][j], dp2[i][j - b[i]] + a[i]);
    }
  }
  ll Max = 0;
  for (int i = 0; i < n; i++)
    Max = max(Max, dp1[i][s] + dp2[n - i - 1][t - s]);
  printf("%lld\\n", Max);
}
Problem p00496 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define MAX 3001

int N, S, T, a[MAX], b[MAX];
int dp[MAX][MAX], ans;

void solve() {
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= T; j++) {
      dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
      ;
      int k = j - b[i];
      if (k >= 0 && !(j - b[i] < S && S < j)) {
        dp[i][j] = max(dp[i][j], dp[i - 1][k] + a[i]);
      }
    }
  }
  ans = -1;
  for (int i = 1; i <= T; i++) {
    ans = max(ans, dp[N][i]);
  }
  cout << ans << endl;
}

int main() {
  cin >> N >> T >> S;
  for (int i = 1; i <= N; i++) {
    cin >> a[i] >> b[i];
  }
  solve();

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define MAX 3001

int N, S, T, a[MAX], b[MAX];
long long dp[MAX][MAX], ans;

void solve() {
  memset(dp, 0, sizeof(dp));
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j < S; j++) {
      dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
      int k = j - b[i];
      if (k >= 0 && dp[i - 1][k] <= T) {
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][k] + a[i]);
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }
  ans = -1;
  for (int i = 1; i <= S; i++) {
    ans = max(ans, dp[N][i]);
  }
  cout << ans << endl;
}

int main() {
  cin >> N >> S >> T;
  for (int i = 1; i <= N; i++) {
    cin >> a[i] >> b[i];
  }
  solve();

  return 0;
}
Problem p00496 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <vector>

using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define all(co) co.begin(), co.end()
const int INF = 1 << 29;
signed main() {
  int n, t, s;
  while (cin >> n >> t >> s && n) {
    vector<int> a(n), b(n);
    rep(i, n) cin >> a[i] >> b[i];

    vector<int> dp(t + 1);
    int ans = 0;
    rep(i, n) for (int j = t; j >= 0;
                   j--) if (j + b[i] <= t && (j + b[i] <= s || s <= j)) {
      dp[j + b[i]] = max(dp[j + b[i]], dp[j] + a[i]);
      ans = max(ans, dp[j + b[i]]);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

const int INF = 1 << 29;
typedef pair<int, int> P;
typedef long long LL;

int n, t, s;
vector<int> a, b;
int dp[2500][3001][2];

int dfs(int ni, int time, bool visited) {
  if (ni == n)
    return 0;

  if (ni > 500 && dp[3000 - ni][time][visited])
    return dp[3000 - ni][time][visited];

  int ret = 0;
  if (time + b[ni] <= t && (time + b[ni] <= s || visited))
    ret = dfs(ni + 1, time + b[ni], visited) + a[ni];
  ret = max(ret, dfs(ni + 1, time, visited));
  if (!visited && time <= s)
    ret = max(ret, dfs(ni, s, true));
  if (ni > 500)
    return dp[3000 - ni][time][visited] = ret;
  return ret;
}

int main(void) {
  while (cin >> n >> t >> s && n) {
    memset(dp, 0, sizeof(dp));
    a.resize(n);
    b.resize(n);
    for (int i = 0; i < n; i++) {
      cin >> a[i] >> b[i];
    }
    cout << dfs(0, 0, false) << endl;
  }
  return 0;
}
Problem p00496 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
int dp[3001][3001];
ll n, t, s;
struct A {
  int a, b;
};
A a[3000];
int solve(ll i, ll j) {
  if (i == n)
    return 0;
  int sum = 0;
  if (j + a[i].b <= t) {
    if (j < s && j + a[i].b > s && s + a[i].b <= t) {
      if (dp[i + 1][s + a[i].b] != -1) {
        sum = max(sum, a[i].a + dp[i + 1][s + a[i].b]);
      } else {
        sum = max(sum,
                  a[i].a + (dp[i + 1][s + a[i].b] = solve(i + 1, s + a[i].b)));
      }
    } else if (!(j < s && j + a[i].b > s)) {
      if (dp[i + 1][j + a[i].b] != -1) {
        sum = max(sum, a[i].a + dp[i + 1][j + a[i].b]);
      } else {
        sum = max(sum,
                  a[i].a + (dp[i + 1][j + a[i].b] = solve(i + 1, j + a[i].b)));
      }
    }
  } //??????
  if (dp[i + 1][j] != -1) {
    sum = max(sum, dp[i + 1][j]);
  } else {
    sum = max(sum, dp[i + 1][j] = solve(i + 1, j));
  } //????????????
  return sum;
}
int main() {
  while (cin >> n >> t >> s) {
    for (ll i = 0; i < n; i++) {
      cin >> a[i].a >> a[i].b;
      for (ll j = 0; j <= t; j++) {
        dp[i][j] = -1;
      }
    }
    cout << solve(0, 0) << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
ll dp[3001][3001];
ll n, t, s;
struct A {
  ll a, b;
};
A a[3000];
ll solve(ll i, ll j) {
  if (i == n)
    return 0;
  ll sum = 0;
  if (j + a[i].b <= t) {
    if (j < s && j + a[i].b > s && s + a[i].b <= t) {
      if (dp[i + 1][s + a[i].b] != -1) {
        sum = max(sum, a[i].a + dp[i + 1][s + a[i].b]);
      } else {
        sum = max(sum,
                  a[i].a + (dp[i + 1][s + a[i].b] = solve(i + 1, s + a[i].b)));
      }
    } else if (!(j < s && j + a[i].b > s)) {
      if (dp[i + 1][j + a[i].b] != -1) {
        sum = max(sum, a[i].a + dp[i + 1][j + a[i].b]);
      } else {
        sum = max(sum,
                  a[i].a + (dp[i + 1][j + a[i].b] = solve(i + 1, j + a[i].b)));
      }
    }
  } //??????
  if (dp[i + 1][j] != -1) {
    sum = max(sum, dp[i + 1][j]);
  } else {
    sum = max(sum, dp[i + 1][j] = solve(i + 1, j));
  } //????????????
  return sum;
}
int main() {
  while (cin >> n >> t >> s) {
    for (ll i = 0; i < n; i++) {
      cin >> a[i].a >> a[i].b;
      for (ll j = 0; j <= t; j++) {
        dp[i][j] = -1;
      }
    }
    cout << solve(0, 0) << endl;
  }
}
Problem p00496 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define LL long long
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define REP(i, n) for (int i = 0; i < n; ++i)
using namespace std;
static const LL INF = 1LL << 61LL;
typedef pair<int, int> PII;

int N, T, S;
PII V[3030];
int mem[3030][3030];

int cal(int n, int time) {
  int res = 0;
  if (n > N || time > T)
    return 0;
  if (mem[n][time] != -1)
    return mem[n][time];
  if (time + V[n].second > T)
    res = max(cal(n + 1, time), max(cal(n + 1, time), res));
  else if (time + V[n].second > S && time < S)
    res = max(cal(n, S), max(cal(n + 1, time), res));
  else
    res = max(cal(n + 1, time + V[n].second) + V[n].first,
              max(cal(n + 1, time), res));
  return mem[n][time] = res;
}

int main() {
  memset(mem, -1, sizeof(mem));
  cin >> N >> T >> S;
  FOR(i, 1, N) { cin >> V[i].first >> V[i].second; }
  cout << cal(1, 0) << endl;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define LL long long
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define REP(i, n) for (int i = 0; i < n; ++i)
using namespace std;
static const LL INF = 1LL << 61LL;
typedef pair<int, int> PII;

int N, T, S;
PII V[3030];
LL mem[3030][3030];

LL cal(int n, int time) {
  LL res = 0;
  if (mem[n][time] != -1)
    return mem[n][time];
  if (n > N || time > T)
    return 0;
  if (time + V[n].second > T)
    res = max(cal(n + 1, time), res);
  else if (time + V[n].second > S && time < S)
    res = max(cal(n, S), res);
  else
    res = max(cal(n + 1, time + V[n].second) + V[n].first, res);
  return mem[n][time] = res;
}

int main() {
  memset(mem, -1, sizeof(mem));
  cin >> N >> T >> S;
  FOR(i, 1, N) { cin >> V[i].first >> V[i].second; }
  cout << cal(1, 0) << endl;
}
Problem p00496 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const int INF = 1e9;
int dp[2][3030][2];
int ma[3030][2];
int main() {
  int n, t, s, a[3030], b[3030];
  cin >> n >> t >> s;
  int r = t - s;
  REP(i, n) cin >> a[i + 1] >> b[i + 1];
  memset(dp, -1, sizeof(dp));
  dp[0][0][0] = 0;
  REP(i, n) {
    REP(j, t + 1) {
      if (dp[i & 1][j][0] != -1) {
        dp[(i + 1) & 1][j][0] = max(dp[(i + 1) & 1][j][0], dp[i & 1][j][0]);
        if (j + b[i + 1] <= s)
          dp[(i + 1) & 1][j + b[i + 1]][0] =
              max(dp[(i + 1) & 1][j + b[i + 1]][0], dp[i & 1][j][0] + a[i + 1]);
      }
    }
    REP(j, s + 1) ma[i + 1][0] = max(ma[i + 1][0], dp[(i + 1) & 1][j][0]);
  }
  dp[(n + 1) & 1][0][1] = 0;
  int ans = 0;
  for (int i = n + 1; i > 1; i--) {
    REP(j, t + 1) {
      if (dp[i & 1][j][1] != -1) {
        dp[(i - 1) & 1][j][1] = max(dp[(i - 1) & 1][j][1], dp[i & 1][j][1]);
        if (j + b[i - 1] <= r)
          dp[(i - 1) & 1][j + b[i - 1]][1] =
              max(dp[(i - 1) & 1][j + b[i - 1]][1], dp[i & 1][j][1] + a[i - 1]);
      }
    }
    REP(j, r + 1) ma[i - 1][1] = max(ma[i - 1][1], dp[(i - 1) & 1][j][1]);
  }
  REP(i, n + 1) { ans = max(ans, ma[i][0] + ma[i + 1][1]); }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const int INF = 1e9;
int main() {
  int n, t, s, a[3030], b[3030];
  cin >> n >> t >> s;
  int r = t - s;
  REP(i, n) cin >> a[i + 1] >> b[i + 1];
  int dp1[n + 2][s + 1];
  int dp2[n + 2][r + 1];
  memset(dp1, -1, sizeof(dp1));
  memset(dp2, -1, sizeof(dp2));
  dp1[0][0] = 0;
  REP(i, n) {
    REP(j, s + 1) {
      if (dp1[i][j] != -1) {
        dp1[i + 1][j] = dp1[i][j];
        if (j + b[i + 1] <= s)
          dp1[i + 1][j + b[i + 1]] =
              max(dp1[i + 1][j + b[i + 1]], dp1[i][j] + a[i + 1]);
      }
    }
  }
  dp2[n + 1][0] = 0;
  for (int i = n + 1; i > 1; i--) {
    REP(j, r) {
      if (dp2[i][j] != -1) {
        dp2[i - 1][j] = dp2[i][j];
        if (j + b[i - 1] <= r)
          dp2[i - 1][j + b[i - 1]] =
              max(dp2[i - 1][j + b[i - 1]], dp2[i][j] + a[i - 1]);
      }
    }
  }
  int ans = 0;
  REP(i, n + 1) {
    int ma1 = 0, ma2 = 0;
    REP(j, t) {
      if (j <= s)
        ma1 = max(ma1, dp1[i][j]);
      if (j <= r)
        ma2 = max(ma2, dp2[i + 1][j]);
    }
    ans = max(ans, ma1 + ma2);
  }
  cout << ans << endl;
}
Problem p00496 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define INF (1 << 31)
// #define int long long

using namespace std;

int n, t, s, a[3005], b[3005], dp[3005];

signed main() {
  cin >> n >> t >> s;

  for (int i = 1; i <= n; ++i)
    cin >> a[i] >> b[i];

  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = t; 0 <= j - b[i]; --j) {
      int nx = j - b[i];
      if ((s <= nx) || (j <= s))
        dp[j] = max(dp[j], dp[nx] + a[i]);
      ans = max(ans, dp[j]);
    }
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define INF (1LL << 61)
// #define int long long

using namespace std;

int n, t, s, a[3005], b[3005], dp[3005][3005];

signed main() {
  fill(dp[0], dp[3005], -1);
  cin >> n >> t >> s;

  for (int i = 1; i <= n; ++i)
    cin >> a[i] >> b[i];

  int ans = 0;
  dp[1][0] = 0;
  for (int i = 1; i <= t; ++i) {
    for (int j = 0; j <= n; ++j) {
      if (dp[i][j] == -1)
        continue;

      for (int k = j + 1; k <= n; ++k) {
        int nxt = i;
        if ((i < s) && (s < i + b[k])) {
          nxt = s;
        }
        nxt += b[k];
        if (t < nxt)
          continue;

        dp[nxt][k] = max(dp[nxt][k], dp[i][j] + a[k]);
        if ((s <= nxt) && (ans < dp[nxt][k]))
          ans = dp[nxt][k];
      }
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p00496 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define INF (2 << 28)
#define fr first
#define sc second
using namespace std;

typedef long long ll;
typedef pair<int, int> iP;

int n, T, S, a[3333], b[3333], mini = INF;
int dp[3001][3001];
bool used[3333];

int rec(int idx, int now, int g) {

  if (idx == n)
    return 0;
  if (~dp[idx][now])
    return dp[idx][now];

  int ret = 0;
  if (now + b[idx] <= g)
    ret = rec(idx + 1, now + b[idx], g) + a[idx];

  ret = max(ret, rec(idx + 1, now, g));
  if (g == S)
    ret = max(ret, rec(idx, S, T));

  return dp[idx][now] = ret;
}

int main() {

  cin >> n >> T >> S;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
    mini = min(mini, b[i]);
  }

  memset(dp, -1, sizeof(dp));
  cout << rec(0, 0, S) << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define INF (2 << 28)
#define fr first
#define sc second
using namespace std;

typedef long long ll;
typedef pair<int, int> iP;

int n, T, S, a[3333], b[3333];
int dp[3333][3333], used[3333];

int rec(int idx, int now, int g) {

  if (idx == n)
    return 0;
  if (dp[idx][now])
    return dp[idx][now];

  int ret = 0;
  if (now + b[idx] <= g)
    ret = rec(idx + 1, now + b[idx], g) + a[idx];
  else if (g == S)
    ret = rec(idx, S, T);

  ret = max(ret, rec(idx + 1, now, g));

  return dp[idx][now] = ret;
}

int main() {

  cin >> n >> T >> S;
  for (int i = 0; i < n; i++)
    cin >> a[i] >> b[i];

  cout << rec(0, 0, S) << endl;
}
Problem p00496 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;

int dp[3001][3001] = {0};

int main() {
  int N, T, S;
  cin >> N >> T >> S;

  int A[3000], B[3000];
  for (int i = 0; i < N; i++)
    cin >> A[i] >> B[i];

  int ans = 0;

  for (int n = 1; n <= N; n++) {
    for (int t = 1; t <= T; t++) {
      dp[n][t] = max(dp[n - 1][t], dp[n][t - 1]);
      if (t - B[n - 1] < 0)
        continue;
      if (S <= t - B[n - 1] || t <= S)
        dp[n][t] = max(dp[n][t], dp[n - 1][t - B[n - 1]] + A[n - 1]);
      ans = max(ans, dp[n][t]);
    }
  }
  cout << ans << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int N, T, S;
  cin >> N >> T >> S;

  int A[3001], B[3001];
  for (int i = 0; i < N; i++)
    cin >> A[i] >> B[i];

  int dp[3001][3001];
  int ans = 0;
  for (int n = 1; n <= N; n++) {
    for (int t = B[n]; t <= T; t++) {
      dp[n][t] = max(dp[n - 1][t], dp[n][t - 1]);
      if (S <= t - B[n] || t <= S)
        dp[n][t] = max(dp[n][t], dp[n - 1][t - B[n]] + A[n]);
      ans = max(ans, dp[n][t]);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p00496 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;

int n, t, s, a[3001], b[3001], dp[3001][3001];

int main() {
  cin >> n >> t >> s;
  for (int i = 1; i <= n; i++)
    cin >> a[i] >> b[i];

  for (int i = 1; i <= t; i++) {
    for (int j = 1; j <= n; j++) {
      if (i - b[j] >= ((i <= s) ? 0 : s) &&
          max(dp[i][j - 1], dp[i - 1][j]) < dp[i - b[j]][j - 1] + a[j])
        dp[i][j] = dp[i - b[j]][j - 1] + a[j];
      else
        dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
    }
  }
  cout << dp[t][n] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int n, t, s, a[3001], b[3001], dp1[3001][3001], dp2[3001][3001];

int main() {
  cin >> n >> t >> s;
  for (int i = 1; i <= n; i++)
    cin >> a[i] >> b[i];

  for (int i = 1; i <= s; i++) {
    for (int j = 1; j <= n; j++) {
      if (i - b[j] >= 0 && dp1[i][j - 1] < dp1[i - b[j]][j - 1] + a[j])
        dp1[i][j] = dp1[i - b[j]][j - 1] + a[j];
      else
        dp1[i][j] = dp1[i][j - 1];
    }
  }
  int i1 = dp1[s][n];
  for (int j = n; j > 0; j--) {
    if (dp1[i1][j] != dp1[i1][j - 1]) {
      i1 -= b[j];
      a[j] = 0;
    }
  }
  for (int i = s + 1; i <= t; i++) {
    for (int j = 1; j <= n; j++) {
      if (i - b[j] >= s && dp2[i][j - 1] < dp2[i - b[j]][j - 1] + a[j])
        dp2[i][j] = dp2[i - b[j]][j - 1] + a[j];
      else
        dp2[i][j] = dp2[i][j - 1];
    }
  }
  cout << dp1[s][n] + dp2[t][n] << endl;
  return 0;
}
Problem p00496 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
// #define int long long
#define PB push_back
#define MP make_pair
#define F first
#define S second
using namespace std;

int n, t, s, a[3010], b[3010];
int dp[3010][3010];

signed main() {
  cin >> n >> t >> s;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i];
  }

  for (int i = 1; i <= t; i++) {
    for (int j = 1; j <= n; j++) {
      dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      if (0 <= i - b[j] && i <= s || s <= i - b[j]) {
        dp[i][j] = max(dp[i][j], dp[i - b[j]][j - 1] + a[j]);
      }
    }
  }

  cout << dp[t][n] << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define PB push_back
#define MP make_pair
#define F first
#define S second
using namespace std;

int n, t, s, a[3010], b[3010];
int dp[3010][3010];

signed main() {
  cin >> n >> t >> s;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i];
  }

  for (int i = 1; i <= t; i++) {
    for (int j = 1; j <= n; j++) {
      dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      if (0 <= i - b[j] && i <= s || s <= i - b[j]) {
        dp[i][j] = max(dp[i][j], dp[i - b[j]][j - 1] + a[j]);
      }
    }
  }

  cout << dp[t][n] << endl;
  return 0;
}
Problem p00496 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int dp[3001][3001];

int main() {
  ios::sync_with_stdio(0);

  int N, T, S;
  cin >> N >> T >> S;

  vector<pair<int, int>> xs(N);
  for (int i = 0; i < N; ++i) {
    int x, y;
    cin >> x >> y;
    xs[i] = make_pair(x, y);
  }

  for (int i = 0; i < 3001; ++i)
    for (int j = 0; j < 3001; ++j)
      dp[i][j] = 0;

  int ans = 0;
  dp[0][0] = 0;
  for (int t = 0; t <= T; ++t) {
    dp[t][0] = (t >= xs[0].second) ? xs[0].first : 0;
    ans = max(ans, dp[t][0]);
  }

  for (int t = 0; t <= T; ++t) {
    for (int i = 1; i <= N; ++i) {
      if (t > 0)
        dp[t][i] = dp[t - 1][i];
      if (i > 0) {
        dp[t][i] = max(dp[t][i], dp[t][i - 1]);
        if (t >= xs[i].second) {
          int prevt = t - xs[i].second;
          if (!(prevt < S && t > S)) {
            dp[t][i] = max(dp[t][i], dp[prevt][i - 1] + xs[i].first);
          }
        }
      }
      // cout << t << ' ' << i << ' ' << dp[t][i] << endl;
      ans = max(ans, dp[t][i]);
    }
  }

  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int dp[3001][3001];

int main() {
  ios::sync_with_stdio(0);

  int N, T, S;
  cin >> N >> T >> S;

  vector<pair<int, int>> xs(N);
  for (int i = 0; i < N; ++i) {
    int x, y;
    cin >> x >> y;
    xs[i] = make_pair(x, y);
  }

  for (int i = 0; i < 3001; ++i)
    for (int j = 0; j < 3001; ++j)
      dp[i][j] = -1;

  dp[0][0] = 0;

  int ans = 0;
  for (int t = 0; t <= T; ++t) {
    for (int i = 0; i <= N; ++i) {
      if (dp[t][i] == -1)
        continue;
      ans = max(ans, dp[t][i]);
      if (t + 1 <= T) {
        dp[t + 1][i] = max(dp[t + 1][i], dp[t][i]);
      }

      for (int j = i; j < N; ++j) {
        int nv = dp[t][i] + xs[j].first;
        int nt = t + xs[j].second;
        if (t < S && nt > S)
          continue;
        if (nt > T)
          continue;
        dp[nt][j + 1] = max(dp[nt][j + 1], nv);
      }
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p00496 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int dp[3001][3001] = {};
int main() {
  int n, t, s, a, b, ans = 0;
  cin >> n >> t >> s;
  for (int i = 1; i <= n; i++) {
    cin >> a >> b;
    for (int j = 0; j <= t; j++) {
      dp[i][j] = dp[i - 1][j];
      if (j >= b && !(j - b < s && s < j))
        dp[i][j] = max(dp[i][j], dp[i - 1][j - b] + a);
      if (i == n)
        ans = max(ans, dp[i][j]);
    }
  }
  cout << ans << endl;
  return (0);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, t, s, a[3000], b[3000];
int memo[3000][3000], fl;
int dp(int jun, int ti) {
  if (ti >= t)
    return 0;
  if (memo[jun][ti] != fl)
    return memo[jun][ti];
  int ret = 0;
  for (int i = jun; i < n; i++) {
    if (ti < s && s < ti + b[i]) {
      if (s + b[i] <= t)
        ret = max(ret, dp(i + 1, s + b[i]) + a[i]);
      else
        ret = max(ret, dp(i + 1, s + b[i]));
    } else {
      if (ti + b[i] <= t)
        ret = max(ret, dp(i + 1, ti + b[i]) + a[i]);
      else
        ret = max(ret, dp(i + 1, ti + b[i]));
    }
  }
  return memo[jun][ti] = ret;
}
int main() {
  scanf("%d %d %d", &n, &t, &s);
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &a[i], &b[i]);
  }
  printf("%d\\n", dp(0, 0));
  return (0);
}