alexjercan/bugnet huggingface dataset:
Problem p00496 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
int dp[3001][3001];
int main(void) {
  int N, T, S, i, j, max = 0, A[3001], B[3001];
  scanf("%d%d%d", &N, &T, &S);
  for (i = 1; i <= N; i++) {
    scanf("%d%d", &A[i], &B[i]);
  }
  for (i = 0; i <= N; i++) {
    for (j = 0; j <= T; j++) {
      dp[i][j] = -1;
    }
  }
  dp[0][0] = 0;
  for (i = 0; i < N; i++) {
    for (j = 0; j <= T; j++) {
      if (dp[i + 1][j] < dp[i][j]) {
        dp[i + 1][j] = dp[i][j];
      }
      if (dp[i][j] != -1) {
        if (j < S && S < j + B[i + 1]) {
          if (S + B[i + 1] <= T &&
              dp[i + 1][S + B[i + 1]] < dp[i][j] + A[i + 1]) {
            dp[i + 1][S + B[i + 1]] = dp[i][j] + A[i + 1];
          }
        } else if (j >= S || S >= j + B[i + 1]) {
          if (j + B[i + 1] <= T &&
              dp[i + 1][j + B[i + 1]] < dp[i][j] + A[i + 1]) {
            dp[i + 1][j + B[i + 1]] = dp[i][j] + A[i + 1];
          }
        }
      }
    }
  }
  /*for(i=0;i<=N;i++){
          for(j=0;j<=T;j++){
                  printf("%d ",dp[i][j]);
          }
          printf("\\n");
  }*/
  for (i = 0; i <= T; i++) {
    if (max < dp[N][i]) {
      max = dp[N][i];
    }
  }
  printf("%d\\n", max);
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
int dp[3001][3001];
int main(void) {
  int N, T, S, i, j, max = 0, A[3001], B[3001];
  scanf("%d%d%d", &N, &T, &S);
  for (i = 1; i <= N; i++) {
    scanf("%d%d", &A[i], &B[i]);
  }
  for (i = 0; i <= N; i++) {
    for (j = 0; j <= T; j++) {
      dp[i][j] = -1;
    }
  }
  dp[0][0] = 0;
  for (i = 0; i < N; i++) {
    for (j = 0; j <= T; j++) {
      if (dp[i][j] != -1 && dp[i + 1][j] < dp[i][j]) {
        dp[i + 1][j] = dp[i][j];
      }
      if (dp[i][j] != -1) {
        if (j < S && S < j + B[i + 1]) {
          if (dp[i + 1][S + B[i + 1]] < dp[i][j] + A[i + 1] &&
              S + B[i + 1] <= T) {
            dp[i + 1][S + B[i + 1]] = dp[i][j] + A[i + 1];
          }
        } else if (j >= S || S > j + B[i + 1]) {
          if (dp[i + 1][j + B[i + 1]] < dp[i][j] + A[i + 1] &&
              j + B[i + 1] <= T) {
            dp[i + 1][j + B[i + 1]] = dp[i][j] + A[i + 1];
          }
        }
      }
    }
  }
  /*for(i=0;i<=N;i++){
          for(j=0;j<=T;j++){
                  printf("%d ",dp[i][j]);
          }
          printf("\\n");
  }*/
  for (i = 0; i <= T; i++) {
    if (max < dp[N][i]) {
      max = dp[N][i];
    }
  }
  printf("%d\\n", max);
  return 0;
}
Problem p00496 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

#define rep(i, j) for (int i = 0; i < (j); i++)

int N, T, S;
int A[3001], B[3001];
int dp[3001][3001];

int dfs(int n, int t) {
  if (n >= N)
    return 0;
  if (t > T)
    return 0;

  int &res = dp[n][t];
  if (res >= 0)
    return res;
  res = 0;
  if (t + B[n] <= T && (S <= t || t + B[n] <= S))
    res = max(res, dfs(n + 1, t + B[n]) + A[n]);
  else if (t + B[n] <= T && (t < S && S < t + B[n]) && S + B[n] <= T)
    res = max(res, dfs(n + 1, S + B[n]) + A[n]);
  res = max(res, dfs(n + 1, t));

  return res;
}

int main() {
  scanf("%d%d%d", &N, &T, &S);
  rep(i, N) scanf("%d%d", &A[i], &B[i]);
  memset(dp, -1, sizeof(dp));
  printf("%d\\n", dfs(0, 0));
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

#define rep(i, j) for (int i = 0; i < (j); i++)

int N, T, S;
int A[4096], B[4096];
int dp[4096][4096];

int dfs(int n, int t) {
  if (n >= N)
    return 0;
  if (t > T)
    return 0;

  int &res = dp[n][t];
  if (res >= 0)
    return res;
  res = 0;
  if (t + B[n] <= T && (S <= t || t + B[n] <= S))
    res = max(res, dfs(n + 1, t + B[n]) + A[n]);
  else if (t + B[n] <= T && (t < S && S < t + B[n]) && S + B[n] <= T)
    res = max(res, dfs(n + 1, S + B[n]) + A[n]);
  res = max(res, dfs(n + 1, t));

  return res;
}

int main() {
  scanf("%d%d%d", &N, &T, &S);
  rep(i, N) scanf("%d%d", &A[i], &B[i]);
  memset(dp, -1, sizeof(dp));
  printf("%d\\n", dfs(0, 0));
}
Problem p00496 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef double db;

#define fr first
#define sc second
#define pb push_back
#define rep(i, x) for (ll i = 0; i < x; i++)
#define rep1(i, x) for (ll i = 1; i <= x; i++)
#define rrep(i, x) for (ll i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (ll i = x; i > 0; i--)
ll n, t, s, a[3000], b[3000];
ll s1, s2;
ll dp1[3001];  //??????j??\\???????????§?\\???????
ll dp2[3001];  //??????j??\\???????????§?\\???????
ll ans1[3001]; // i???????????§??§s1??\\???????????§joy
ll ans2[3001]; // i????????\\?????§s2??\\???????????§joy
int main() {
  cin >> n >> t >> s;
  s1 = s;
  s2 = t - s;
  memset(dp1, 0, sizeof(dp1));
  memset(dp2, 0, sizeof(dp2));
  rep(i, n) cin >> a[i] >> b[i];
  for (ll i = b[0]; i <= t; i++)
    dp1[i] = a[0];
  for (ll i = b[n - 1]; i <= t; i++)
    dp2[i] = a[n - 1];
  ans1[0] = dp1[s1];
  ans2[n - 1] = dp2[s2];
  rep1(i, n - 1) {
    // rep(j,t+1) dp1[i][j]=dp1[i-1][j];
    rrep(j, t + 1 - b[i]) { dp1[j + b[i]] = max(dp1[j + b[i]], dp1[j] + a[i]); }
    ans1[i] = dp1[s1];
  }
  rrep(i, n - 1) {
    // rep(j,t+1) dp2[i][j]=dp2[i+1][j];
    rrep(j, t + 1 - b[i]) { dp2[j + b[i]] = max(dp2[j + b[i]], dp2[j] + a[i]); }
    ans2[i] = dp2[s2];
  }
  ll ans = max(ans1[n - 1], ans2[0]);
  rep(i, n - 1) { ans = max(ans, ans1[i] + ans2[i + 1]); }
  cout << ans << endl;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef int ll;
typedef pair<ll, ll> P;
typedef double db;

#define fr first
#define sc second
#define pb push_back
#define rep(i, x) for (ll i = 0; i < x; i++)
#define rep1(i, x) for (ll i = 1; i <= x; i++)
#define rrep(i, x) for (ll i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (ll i = x; i > 0; i--)
ll n, t, s, a[3000], b[3000];
ll s1, s2;
ll dp1[3000][3000]; // i???????????§??§ ??????j??\\???????????§?\\???????
ll dp2[3000][3000]; // i????????\\?????§ ??????j??\\???????????§?\\???????
int main() {
  cin >> n >> t >> s;
  s1 = s;
  s2 = t - s;
  memset(dp1, 0, sizeof(dp1));
  memset(dp2, 0, sizeof(dp2));
  rep(i, n) cin >> a[i] >> b[i];
  for (ll i = b[0]; i <= t; i++)
    dp1[0][i] = a[0];
  for (ll i = b[n - 1]; i <= t; i++)
    dp2[n - 1][i] = a[n - 1];
  rep1(i, n - 1) {
    rep(j, t + 1) dp1[i][j] = dp1[i - 1][j];
    rrep(j, t + 1 - b[i]) {
      dp1[i][j + b[i]] = max(dp1[i][j + b[i]], dp1[i - 1][j] + a[i]);
    }
  }
  rrep(i, n - 1) {
    rep(j, t + 1) dp2[i][j] = dp2[i + 1][j];
    rrep(j, t + 1 - b[i]) {
      dp2[i][j + b[i]] = max(dp2[i][j + b[i]], dp2[i + 1][j] + a[i]);
    }
  }
  ll ans = max(dp1[n - 1][s1], dp2[0][s2]);
  rep(i, n - 1) { ans = max(ans, dp1[i][s1] + dp2[i + 1][s2]); }
  cout << ans << endl;
}
Problem p00496 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <chrono> //1e+9??§?????? auto end= chrono::system_clock::now()-st;  cout<<end.count()%1e+9<<endl;
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef int ll;
typedef pair<ll, ll> P;
typedef long double db;

#define fr first
#define sc second
#define pb push_back
#define rep(i, x) for (ll i = 0; i < x; i++)
#define rep1(i, x) for (ll i = 1; i <= x; i++)
#define rrep(i, x) for (ll i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (ll i = x; i > 0; i--)

ll N, S, T;
ll V[3001], C[3001];
ll dp1[3002][3001]; // i?????§???j????????\\??????????\\???????
ll dp2[3002][3001];
int main() {
  cin >> N >> T >> S;
  T -= S;
  rep1(i, N) { cin >> V[i] >> C[i]; }
  rep1(i, N) {
    for (ll j = 0; j <= S; j++) {
      if (j - C[i] >= 0) {
        dp1[i][j] = max(dp1[i - 1][j], dp1[i - 1][j - C[i]] + V[i]);
      } else
        dp1[i][j] = dp1[i - 1][j];
    }
  }
  rrep1(i, N) {
    for (ll j = 0; j <= T; j++) {
      if (j - C[i] >= 0) {
        dp2[i][j] = max(dp2[i + 1][j], dp2[i + 1][j - C[i]] + V[i]);
      } else
        dp2[i][j] = dp2[i + 1][j];
    }
  }
  ll ans = 0;
  for (ll i = 0; i <= N; i++) {
    ans = max(ans, dp1[i][S] + dp2[i + 1][T]);
  }
  cout << ans << endl;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <chrono> //1e+9??§?????? auto end= chrono::system_clock::now()-st;  cout<<end.count()%1e+9<<endl;
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef long double db;

#define fr first
#define sc second
#define pb push_back
#define rep(i, x) for (ll i = 0; i < x; i++)
#define rep1(i, x) for (ll i = 1; i <= x; i++)
#define rrep(i, x) for (ll i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (ll i = x; i > 0; i--)

ll N, S, T;
ll V[3001], C[3001];
ll dp1[3002][3001]; // i?????§???j????????\\??????????\\???????
ll dp2[3002][3001];
int main() {
  cin >> N >> T >> S;
  T -= S;
  rep1(i, N) { cin >> V[i] >> C[i]; }
  rep1(i, N) {
    for (ll j = 0; j <= S; j++) {
      if (j - C[i] >= 0) {
        dp1[i][j] = max(dp1[i - 1][j], dp1[i - 1][j - C[i]] + V[i]);
      } else
        dp1[i][j] = dp1[i - 1][j];
    }
  }
  rrep1(i, N) {
    for (ll j = 0; j <= T; j++) {
      if (j - C[i] >= 0) {
        dp2[i][j] = max(dp2[i + 1][j], dp2[i + 1][j - C[i]] + V[i]);
      } else
        dp2[i][j] = dp2[i + 1][j];
    }
  }
  ll ans = 0;
  for (ll i = 0; i <= N; i++) {
    ans = max(ans, dp1[i][S] + dp2[i + 1][T]);
  }
  cout << ans << endl;
}
Problem p00496 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define MAX 3004
const int inf = 1e9;
#define rrep(i, n) for (int i = 1; i <= n; i++)

int a[MAX], b[MAX], res[MAX];
int dp1[MAX][MAX], dp2[MAX][MAX]; // 店,時間
int main() {
  int n, t, s;
  scanf("%d%d%d", &n, &t, &s);
  rrep(i, n) scanf("%d%d", &a[i], &b[i]);

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

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

  for (int i = 1; i <= n; i++)
    res[i] = max(res[i - 1], dp2[i][t - s]);
  /*for(int i = 0;i <= n;i++)printf("%d ", res[i]);
  printf("\\n");*/

  int ans = 0;
  for (int i = 0; i <= n; i++) {
    ans = max(ans, dp1[i][s] + res[n - i]);
  }

  /*for(int i = 1;i <= n;i++){
          for(int j = 0;j <= t-s;j++)printf("%d ", dp2[i][j]);
          printf("\\n");
  }*/
  printf("%d\\n", ans);
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = 1; i <= n; i++)
#define drep(i, n) for (int i = n; i >= 0; i--)
#define INF 100000005
#define MAX 100001
#define ll long long
#define dmp make_pair
#define dpb push_back
#define P pair<int, int>
#define fi first
#define se second
using namespace std;
//__gcd(a,b), __builtin_popcount(a);

ll a[3001], b[3001];
ll dp1[3001][3001], dp2[3001][3001];
int main() {
  int n, t, s;
  scanf("%d%d%d", &n, &t, &s);
  rrep(i, n) scanf("%d%d", &a[i], &b[i]);
  rrep(i, n) dp1[i][0] = INF;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= s; j++) {
      if (j == 1)
        dp1[i][j] = dp1[i - 1][j];
      else
        dp1[i][j] = max(dp1[i][j - 1], dp1[i - 1][j]);
      if (j - b[i] >= 0 && dp1[i][j - b[i]] == INF)
        dp1[i][j] = max(a[i], dp1[i - 1][j]);
      else if (dp1[i - 1][j - b[i]])
        dp1[i][j] = max(dp1[i - 1][j - b[i]] + a[i], dp1[i - 1][j]);
    }
  }
  int ii, m = t - s + 1;
  rrep(i, n) dp2[i][0] = INF;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      ii = n - i + 1;
      if (j == 1)
        dp2[i][j] = dp2[i - 1][j];
      else
        dp2[i][j] = max(dp2[i][j - 1], dp2[i - 1][j]);
      if (j - b[ii] >= 0 && dp2[i][j - b[ii]] == INF)
        dp2[i][j] = max(a[ii], dp2[i - 1][j]);
      else if (dp2[i - 1][j - b[ii]])
        dp2[i][j] = max(dp2[i - 1][j - b[ii]] + a[ii], dp2[i - 1][j]);
    }
  }
  ll ans = 0;
  for (int i = 1; i <= n - 1; i++) {
    ans = max(ans, dp1[i][s] + dp2[n - i][m]);
  }
  /*for(int i = 1;i <= n;i++){
          for(int j = 1;j <= m;j++)printf("%d ", dp2[i][j]);
          printf("\\n");
  }*/
  printf("%d\\n", ans);
  return 0;
}
Problem p00496 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

ifstream fi("MARKET.INP");
ofstream fo("MARKET.OUT");

#define fi cin
#define fo cout

int n, T, S;
int a[3010], b[3010], F[3001], G[3001];
int f[3001][3001], res;

void nhapdl() {
  fi >> n >> T >> S;
  for (int i = 1; i <= n; ++i)
    fi >> a[i] >> b[i];
}

void tinh(int x) {
  S = T - S;

  res += f[n][S];
}

void xuli() {
  for (int i = 1; i <= n; ++i)
    for (int j = 0; j <= S; ++j) {
      f[i][j] = f[i - 1][j];
      if (j - b[i] >= 0)
        f[i][j] = max(f[i][j], f[i - 1][j - b[i]] + a[i]);
      F[i] = f[i][j];
    }
  for (int i = n; i >= 1; --i)
    for (int j = 0; j <= T - S; ++j) {
      f[i][j] = f[i + 1][j];
      if (j - b[i] >= 0)
        f[i][j] = max(f[i][j], f[i + 1][j - b[i]] + a[i]);
      G[i] = f[i][j];
    }
  for (int i = 0; i <= n; ++i)
    res = max(res, F[i] + G[i + 1]);
}

int main() {
  nhapdl();
  xuli();
  fo << res << '\\n';
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

ifstream fi("MARKET.INP");
ofstream fo("MARKET.OUT");

#define fi cin
#define fo cout

int n, T, S;
int a[3010], b[3010];
int f[3001][3001], res, g[3001][3001];

void nhapdl() {
  fi >> n >> T >> S;
  for (int i = 1; i <= n; ++i)
    fi >> a[i] >> b[i];
}

void tinh(int x) {
  S = T - S;

  res += f[n][S];
}

void xuli() {
  for (int i = 1; i <= n; ++i)
    for (int j = 0; j <= S; ++j) {
      f[i][j] = f[i - 1][j];
      if (j - b[i] >= 0)
        f[i][j] = max(f[i][j], f[i - 1][j - b[i]] + a[i]);
    }
  for (int i = n; i >= 1; --i)
    for (int j = 0; j <= T - S; ++j) {
      g[i][j] = g[i + 1][j];
      if (j - b[i] >= 0)
        g[i][j] = max(g[i][j], g[i + 1][j - b[i]] + a[i]);
    }
  for (int i = 0; i <= n; ++i)
    res = max(res, f[i][S] + g[i + 1][T - S]);
}

int main() {
  nhapdl();
  xuli();
  fo << res << '\\n';
}
Problem p00496 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
ll dp1[3030], dp2[3030];
int main() {
  int n, t, s;
  cin >> n >> t >> s;
  vector<int> a(n), b(n);
  rep(i, n) cin >> a[i] >> b[i];
  ll ans = 0;
  rep(i, n) {
    rep(j, t) {
      dp2[j] = max(dp2[j], dp1[j]); // その屋台をスキップ
      if (j < t)
        dp1[j + 1] = max(dp1[j + 1], dp1[j]); // その時間は何もしない
      if (j + b[i] <= t) {                    // その屋台で遊ぶ
        if (j < s && s < j + b[i])
          continue;
        dp2[j + b[i]] = max(dp2[j + b[i]], dp1[j] + a[i]);
        ans = max(ans, dp2[j + b[i]]);
      }
    }
    rep(j, 3030) {
      dp1[j] = dp2[j];
      dp2[j] = 0;
    }
  }
  cout << ans << endl;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
ll dp[3030][3030];
int main() {
  int n, t, s;
  cin >> n >> t >> s;
  vector<int> a(n), b(n);
  rep(i, n) cin >> a[i] >> b[i];
  ll ans = 0;
  rep(i, n) {
    rep(j, t) {
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
      if (j < t)
        dp[i][j + 1] = max(dp[i][j + 1], dp[i][j]);
      if (j + b[i] <= t) {
        if (j < s && s < j + b[i])
          continue;
        dp[i + 1][j + b[i]] = max(dp[i + 1][j + b[i]], dp[i][j] + a[i]);
        ans = max(ans, dp[i + 1][j + b[i]]);
      }
    }
  }
  cout << ans << endl;
}
Problem p00496 (C++) — buggy submission, verdict: Runtime Error

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

int a[3010], b[3010], dp[2][3010], mm1[3010][2], mm2[3010][2];
int main() {
  int N, T, S, I, O, hog, hma, res;
  cin >> N >> T >> S;
  for (int i = 1; i <= N; i++) {
    cin >> a[i] >> b[i];
  }
  memset(dp[0], -1, sizeof(dp[0]));
  I = 0;
  O = 1;
  dp[0][0] = 0;
  for (int i = 1; i <= N; i++) {
    memset(dp[O], -1, sizeof(dp[O]));
    hma = -1;
    for (int j = 0; j <= S; j++) {
      hog = dp[I][j];
      if (j >= b[i] && dp[I][j - b[i]] != -1)
        hog = max(hog, dp[I][j - b[i]] + a[i]);
      dp[O][j] = hog;
      hma = max(hma, hog);
    }
    mm1[i][0] = hma;
    hog = dp[I][S + 1];
    if (S + 1 >= b[i] && dp[I][S + 1 - b[i]] != -1)
      hog = max(hog, dp[I][S + 1 - b[i]] + a[i]);
    dp[O][S + 1] = hog;
    mm1[i][1] = max(hma, hog);
    swap(I, O);
  }
  memset(dp[I], -1, sizeof(dp[I]));
  dp[I][0] = 0;
  for (int i = N; i >= 1; i--) {
    memset(dp[O], -1, sizeof(dp[O]));
    hma = -1;
    for (int j = 0; j <= T - S; j++) {
      hog = dp[I][j];
      if (j >= b[i] && dp[I][j - b[i]] != -1)
        hog = max(hog, dp[I][j - b[i]] + a[i]);
      dp[O][j] = hog;
      hma = max(hma, hog);
    }
    mm2[i][1] = hma;
    hog = dp[I][T - S + 1];
    if (T - S + 1 >= b[i] && dp[I][T - S + 1 - b[i]] != -1)
      hog = max(hog, dp[I][T - S + 1 - b[i]] + a[i]);
    dp[O][T - S + 1] = hog;
    mm2[i][0] = max(hma, hog);
    swap(I, O);
  }
  res = 0;
  mm1[0][0] = mm1[0][1] = mm2[N + 1][0] = mm2[N + 1][1] = 0;
  for (int i = 0; i <= N; i++) {
    // cout<<i<<" "<<j<<" "<<mm1[i][j]<<" "<<mm2[i+1][j]<<endl;
    res = max(mm1[i][0] + mm2[i + 1][1], res);
  }
  cout << res << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int a[3010], b[3010];
int dp[2][1510][3010] = {};
int main() {
  int N, T, S, res, hog;
  int ii, oo;
  bool fla = false;
  cin >> N >> T >> S;
  if (S > T - S) {
    S = T - S;
    fla = true;
  }
  for (int i = 1; i <= N; i++) {
    cin >> a[i] >> b[i];
    b[i]++;
  }
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j <= T + 1; j++) {
      memset(dp[i][j], -1, sizeof(dp[i][j]));
    }
  }
  dp[0][0][0] = 0;
  ii = 0;
  oo = 1;
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j <= S + 1; j++) {
      for (int k = 0; j + k <= T + 1; k++) {
        hog = dp[ii][j][k];
        if (j >= b[i] && dp[ii][j - b[i]][k] != -1)
          hog = max(hog, dp[ii][j - b[i]][k] + a[i]);
        if (k >= b[i] && dp[ii][j][k - b[i]] != -1)
          hog = max(hog, dp[ii][j][k - b[i]] + a[i]);
        dp[oo][j][k] = hog;
        while (true)
          ;
        // cout<<hog<<" ";
      }
      // cout<<endl;
    }
    swap(ii, oo);
    // cout<<endl;
  }
  res = 0;
  for (int i = 0; i <= S + 1; i++) {
    for (int j = 0; i + j <= T + 1; j++) {
      res = max(res, dp[ii][i][j]);
    }
  }
  cout << res << endl;
  return 0;
}
Problem p00496 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define inf (1 << 28)
#define MAX 3003
using namespace std;
typedef pair<int, int> ii;
int dp[MAX], N, T, S;

bool cmp(const ii &a, const ii &b) {
  if (a.second != b.second)
    return a.second < b.second;
  return a.first > b.first;
}

int main() {
  while (cin >> N >> T >> S) {
    vector<ii> vec(N);
    rep(i, N) cin >> vec[i].first >> vec[i].second;
    // sort(vec.begin(),vec.end(),cmp);
    int mexico = 0;
    rep(i, N + 1) dp[i] = 0;

    rep(i, N) {                      // どの店
      for (int j = T; j >= 0; j--) { // 時間
        if (!(S <= j || j + vec[i].second <= S))
          continue;
        if (j + vec[i].second > T)
          continue;
        dp[j + vec[i].second] =
            max(dp[j + vec[i].second], dp[j] + vec[i].first);

        mexico = max(mexico, dp[j + vec[i].second]);
      }
    }

    cout << mexico << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define inf (1 << 28)
#define MAX 3003
using namespace std;
typedef pair<int, int> ii;
int dp[MAX][MAX], N, T, S;

bool cmp(const ii &a, const ii &b) {
  if (a.second != b.second)
    return a.second < b.second;
  return a.first > b.first;
}

int main() {
  while (cin >> N >> T >> S) {
    vector<ii> vec(N);
    rep(i, N) cin >> vec[i].first >> vec[i].second;
    // sort(vec.begin(),vec.end(),cmp);
    int mexico = 0;
    rep(i, N + 1) rep(j, N + 1) dp[i][j] = 0;

    dp[0][0] = 0;

    rep(i, N) {                      // どの店
      for (int k = N; k >= 0; k--) { // 何回店を訪れた
        rep(j, T + 1) {              // 時間
          if (dp[k][j] == -inf)
            continue;
          // if(j < S && S < j+vec[i].second)continue;
          if (!(S <= j || j + vec[i].second <= S))
            continue;
          if (j + vec[i].second > T)
            continue;
          dp[k + 1][j + vec[i].second] =
              max(dp[k + 1][j + vec[i].second], dp[k][j] + vec[i].first);

          mexico = max(mexico, dp[k + 1][j + vec[i].second]);
        }
      }
    }

    cout << mexico << endl;
  }
  return 0;
}