prithivMLmods/Coder-Stat huggingface dataset:
Problem p02863 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

void solve(long long N, long long T, std::vector<long long> A,
           std::vector<long long> B) {
  vector<pair<long long, long long>> c;
  for (int i = 0; i < N; ++i) {
    c.emplace_back(A[i], B[i]);
  }
  sort(c.begin(), c.end());
  vector<vector<long long>> dp(N, vector<long long>(T, 0));
  dp[0][c[0].first] = c[0].second;
  for (int i = 1; i < N; ++i) {
    for (int j = 0; j + c[i].first < T; ++j) {
      dp[i][j + c[i].first] =
          max(dp[i - 1][j] + c[i].second, dp[i - 1][j + c[i].first]);
    }
  }
  long long last = 0;
  long long ret = 0;
  for (int i = N - 1; i >= 0; --i) {
    for (int j = 0; j < T; ++j) {
      ret = max(dp[i][j] + last, ret);
    }
    last = max(c[i].second, last);
  }
  ret = (last, ret);
  std::cout << ret << std::endl;
}

int main() {
  long long N;
  scanf("%lld", &N);
  long long T;
  scanf("%lld", &T);
  std::vector<long long> A(N);
  std::vector<long long> B(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
    scanf("%lld", &B[i]);
  }
  solve(N, T, std::move(A), std::move(B));
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

void solve(long long N, long long T, std::vector<long long> A,
           std::vector<long long> B) {
  vector<pair<long long, long long>> c;
  for (int i = 0; i < N; ++i) {
    c.emplace_back(A[i], B[i]);
  }
  sort(c.begin(), c.end());
  vector<vector<long long>> dp(N, vector<long long>(T, 0));
  if (c[0].first < T) {
    dp[0][c[0].first] = c[0].second;
  }
  for (int i = 1; i < N; ++i) {
    for (int j = 0; j + c[i].first < T; ++j) {
      dp[i][j + c[i].first] =
          max(dp[i - 1][j] + c[i].second, dp[i - 1][j + c[i].first]);
    }
  }
  long long last = 0;
  long long ret = 0;
  for (int i = N - 1; i >= 0; --i) {
    for (int j = 0; j < T; ++j) {
      ret = max(dp[i][j] + last, ret);
    }
    last = max(c[i].second, last);
  }
  ret = (last, ret);
  std::cout << ret << std::endl;
}

int main() {
  long long N;
  scanf("%lld", &N);
  long long T;
  scanf("%lld", &T);
  std::vector<long long> A(N);
  std::vector<long long> B(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
    scanf("%lld", &B[i]);
  }
  solve(N, T, std::move(A), std::move(B));
  return 0;
}
Problem p02863 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (int)(n); i++)
#define ve vector

int main() {
  int n, t;
  cin >> n >> t;
  ve<P> ab;
  rep(i, n) {
    ll a, b;
    cin >> a >> b;
    ab.push_back(P(a, b));
  }

  sort(ab.begin(), ab.end());

  ve<ve<ll>> dp1(n + 1, ve<ll>(t + 1));
  rep(i, n) {
    ll a = ab[i].first, b = ab[i].second;
    rep(j, a) dp1[i + 1][j] = dp1[i][j];
    rep(j, t - a + 1) { dp1[i + 1][j + a] = max(dp1[i][j + a], dp1[i][j] + b); }
  }

  ll ans = 0;
  rep(i, n) {
    ll memo = ab[i].second;
    ans = max(ans, memo + dp1[i][t - 1]);
  }

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (int)(n); i++)
#define ve vector

int main() {
  int n, t;
  cin >> n >> t;
  ve<P> ab;
  rep(i, n) {
    ll a, b;
    cin >> a >> b;
    ab.push_back(P(a, b));
  }

  sort(ab.begin(), ab.end());

  ve<ve<ll>> dp1(n + 1, ve<ll>(t + 1));
  rep(i, n) {
    ll a = ab[i].first, b = ab[i].second;
    rep(j, min(a, (ll)t)) dp1[i + 1][j] = dp1[i][j];
    rep(j, t - a + 1) { dp1[i + 1][j + a] = max(dp1[i][j + a], dp1[i][j] + b); }
  }

  ll ans = 0;
  rep(i, n) {
    ll memo = ab[i].second;
    ans = max(ans, memo + dp1[i][t - 1]);
  }

  cout << ans << endl;
  return 0;
}
Problem p02863 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

typedef pair<int, int> P;

int dp[4000][4000][2];

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

  vector<P> v;

  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    v.push_back(P(a, b));
  }

  for (int i = 1; i <= N; i++) {
    for (int j = 0; j < T; j++) {
      if (j >= v[i - 1].first) {
        dp[i][j][0] = max(dp[i - 1][j - v[i - 1].first][0] + v[i - 1].second,
                          dp[i - 1][j][0]);
        dp[i][j][1] =
            max(dp[i - 1][j][0] + v[i - 1].second,
                max(dp[i - 1][j - v[i - 1].first][1] + v[i - 1].second,
                    dp[i - 1][j][1]));
      } else {
        dp[i][j][0] = dp[i - 1][j][0];
        dp[i][j][1] = max(dp[i - 1][j - v[i - 1].first][0] + v[i - 1].second,
                          dp[i - 1][j][1]);
      }
    }
  }
  int ans = dp[N][T - 1][1];
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

typedef pair<int, int> P;

int dp[4000][4000][2];

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

  vector<P> v;

  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    v.push_back(P(a, b));
  }

  for (int i = 1; i <= N; i++) {
    for (int j = 0; j < T; j++) {
      if (j >= v[i - 1].first) {
        dp[i][j][0] = max(dp[i - 1][j - v[i - 1].first][0] + v[i - 1].second,
                          dp[i - 1][j][0]);
        dp[i][j][1] =
            max(dp[i - 1][j][0] + v[i - 1].second,
                max(dp[i - 1][j - v[i - 1].first][1] + v[i - 1].second,
                    dp[i - 1][j][1]));
      } else {
        dp[i][j][0] = dp[i - 1][j][0];
        dp[i][j][1] = max(dp[i - 1][j][0] + v[i - 1].second, dp[i - 1][j][1]);
      }
    }
  }
  int ans = dp[N][T - 1][1];
  cout << ans << endl;
  return 0;
}
Problem p02863 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL_DEBUG
#include "LOCAL_DEBUG.hpp"
#endif
#define int long long

signed main() {

  int n, t;
  cin >> n >> t;
  vector<int> a(n), b(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }

  vector<int> v;

  vector<int> order(n);
  iota(order.begin(), order.end(), 0);
  sort(order.begin(), order.end(), [&](int i, int j) {
    if (a[i] == a[j])
      return b[i] > b[j];
    return a[i] < a[j];
  });
  for (int i = 0; i < min(n, 10LL); i++) {
    v.push_back(order[i]);
  }

  iota(order.begin(), order.end(), 0);
  sort(order.begin(), order.end(), [&](int i, int j) {
    if (b[i] == b[j])
      return a[i] < a[j];
    return b[i] > b[j];
  });
  for (int i = 0; i < min(n, 10LL); i++) {
    v.push_back(order[i]);
  }

  int ans = 0;
  for (int u = 0; u < v.size(); u++) {
    vector<int> dp(t, 0);
    for (int i = 0; i < n; i++) {
      if (i == v[u])
        continue;
      for (int j = t; j >= 0; j--) {
        if (j + a[i] > t)
          continue;
        dp[j + a[i]] = max(dp[j + a[i]], dp[j] + b[i]);
      }
    }
    ans = max(ans, dp[t - 1] + b[v[u]]);
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL_DEBUG
#include "LOCAL_DEBUG.hpp"
#endif
#define int long long

signed main() {

  int n, t;
  cin >> n >> t;
  vector<int> a(n), b(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }

  vector<int> v;

  vector<int> order(n);
  iota(order.begin(), order.end(), 0);
  sort(order.begin(), order.end(), [&](int i, int j) {
    if (a[i] == a[j])
      return b[i] > b[j];
    return a[i] < a[j];
  });
  for (int i = 0; i < min(n, 10LL); i++) {
    v.push_back(order[i]);
  }

  iota(order.begin(), order.end(), 0);
  sort(order.begin(), order.end(), [&](int i, int j) {
    if (b[i] == b[j])
      return a[i] < a[j];
    return b[i] > b[j];
  });
  for (int i = 0; i < min(n, 10LL); i++) {
    v.push_back(order[i]);
  }

  int ans = 0;
  for (int u = 0; u < v.size(); u++) {
    vector<int> dp(t + 10, 0);
    for (int i = 0; i < n; i++) {
      if (i == v[u])
        continue;
      for (int j = t; j >= 0; j--) {
        if (j + a[i] > t)
          continue;
        dp[j + a[i]] = max(dp[j + a[i]], dp[j] + b[i]);
      }
    }
    ans = max(ans, dp[t - 1] + b[v[u]]);
  }
  cout << ans << endl;

  return 0;
}
Problem p02863 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ll n, t;
  cin >> n >> t;
  vector<vector<ll>> dp(t, vector<ll>(2));
  for (ll i = 0; i < n; i++) {
    ll a, b;
    cin >> a >> b;
    for (ll i = t; i-- > a;)
      chmax(dp[i][1], dp[i - a][1] + b);
    for (ll i = 0; i < t; i++)
      chmax(dp[i][1], dp[i][0] + b);
    for (ll i = t; i >= a; i--)
      chmax(dp[i][0], dp[i - a][0] + b);
  }
  cout << max(dp[t - 1][0], dp[t - 1][1]) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ll n, t;
  cin >> n >> t;
  vector<vector<ll>> dp(t, vector<ll>(2));
  for (ll i = 0; i < n; i++) {
    ll a, b;
    cin >> a >> b;
    for (ll i = t; i-- > a;)
      chmax(dp[i][1], dp[i - a][1] + b);
    for (ll i = 0; i < t; i++)
      chmax(dp[i][1], dp[i][0] + b);
    for (ll i = t; i-- > a;)
      chmax(dp[i][0], dp[i - a][0] + b);
  }
  cout << max(dp[t - 1][0], dp[t - 1][1]) << endl;
}
Problem p02863 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<ll, ll> pii;
typedef pair<ld, ld> pld;
typedef long double ld;
pll a[3009];
ll dp[3009][8009];
ll ans, n, t;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
  cin >> n >> t;
  for (int i = 1; i <= n; ++i)
    cin >> a[i].ff >> a[i].ss;
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; ++i)
    for (int j = 0; j < t; ++j) {
      dp[i][j] = max(dp[i][j], dp[i - 1][j]);
      dp[i][j + a[i].ff] = max(dp[i][j + a[i].ff], dp[i - 1][j] + a[i].ss);
      ans = max(ans, dp[i][j + a[i].ff]);
    }
  cout << ans << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<ll, ll> pii;
typedef pair<ld, ld> pld;
typedef long double ld;
pll a[3009];
ll dp[3009][8009];
ll ans, n, t;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  cin >> n >> t;
  for (int i = 1; i <= n; ++i)
    cin >> a[i].ff >> a[i].ss;
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; ++i)
    for (int j = 0; j < t; ++j) {
      dp[i][j] = max(dp[i][j], dp[i - 1][j]);
      dp[i][j + a[i].ff] = max(dp[i][j + a[i].ff], dp[i - 1][j] + a[i].ss);
      ans = max(ans, dp[i][j + a[i].ff]);
    }
  cout << ans << "\n";
}
Problem p02863 (Python)

Buggy submission (status: Runtime Error):
N, W = map(int, input().split())
items = sorted([list(map(int, input().split())) for i in range(N)])

# dp1[i][j] := 商品0 ~ i が対象
dp = [[0] * (W + 1) for i in range(N + 1)]

# dpテーブルを埋める
for i in range(N):
    wi, vi = items[i]
    for j in range(W + 1):
        if j + wi <= W:
            dp[i + 1][j + wi] = max(dp[i + 1][j + wi], dp[i][j] + vi)
        dp[i + 1][j] = max(dp[i][j], dp[i + 1][j])


ans = 0
for i, (wi, vi) in enumerate(items):
    for j in range(W):
        ans = max(ans, dp[i] + vi)

print(ans)


Fixed submission:
N, W = map(int, input().split())
items = sorted([list(map(int, input().split())) for i in range(N)])

# dp1[i][j] := 商品0 ~ i が対象
dp = [[0] * (W + 1) for i in range(N + 1)]

# dpテーブルを埋める
for i in range(N):
    wi, vi = items[i]
    for j in range(W + 1):
        if j + wi <= W:
            dp[i + 1][j + wi] = max(dp[i + 1][j + wi], dp[i][j] + vi)
        dp[i + 1][j] = max(dp[i][j], dp[i + 1][j])


ans = 0
for i, (wi, vi) in enumerate(items):
    for j in range(W):
        ans = max(ans, dp[i][j] + vi)

print(ans)
Problem p02863 (Python)

Buggy submission (status: Time Limit Exceeded):
def solve(n, t, ab_list):
    ab_list_s = sorted(ab_list, key=lambda x: x[0])
    res = 0
    dp = [0] * t
    for i in range(n):
        for s in range(t - 1, -1, -1):
            if dp[s] > 0 or s == 0:
                u = s + ab_list_s[i][0]
                r = dp[s] + ab_list_s[i][1]
                if u >= t:
                    res = max(res, r)
                else:
                    dp[u] = max(dp[u], r)
    return max(res, max(dp))


def main():
    n, t = map(int, input().split())
    ab_list = []
    for _ in range(n):
        a, b = map(int, input().split())
        ab_list.append([a, b])
    res = solve(n, t, ab_list)
    print(res)


def test():
    assert solve(2, 60, [[10, 10], [100, 100]]) == 110
    assert solve(3, 60, [[10, 10], [10, 20], [10, 30]]) == 60
    assert solve(3, 60, [[30, 10], [30, 20], [30, 30]]) == 50


if __name__ == "__main__":
    test()
    main()


Fixed submission:
def solve(n, t, ab_list):
    ab_list_s = sorted(ab_list, key=lambda x: x[0])
    res = 0
    dp = [0] * t
    for i in range(n):
        for s in range(t - 1, -1, -1):
            if dp[s] > 0 or s == 0:
                u = s + ab_list_s[i][0]
                r = dp[s] + ab_list_s[i][1]
                if u >= t:
                    res = max(res, r)
                else:
                    dp[u] = max(dp[u], r)
    return max(res, max(dp))


def main():
    n, t = map(int, input().split())
    ab_list = []
    for _ in range(n):
        a, b = map(int, input().split())
        ab_list.append([a, b])
    res = solve(n, t, ab_list)
    print(res)


def test():
    assert solve(2, 60, [[10, 10], [100, 100]]) == 110
    assert solve(3, 60, [[10, 10], [10, 20], [10, 30]]) == 60
    assert solve(3, 60, [[30, 10], [30, 20], [30, 30]]) == 50


if __name__ == "__main__":
    # test()
    main()
Problem p02863 (Python)

Buggy submission (status: Runtime Error):
n, t = map(int, input().split())
x = [list(map(int, input().split())) for _ in range(n)]
a_max = max([y[1] for y in x])

dp_pre = [0 for j in range(t + a_max)]
dp = [0 for j in range(t + a_max)]
for ai, bi in x:
    for j in range(ai):
        dp[j] = dp_pre[j]
    for j in range(ai, t + ai):
        dp[j] = max(dp_pre[j], dp_pre[j - ai] + bi)
    for j in range(t + ai, t + a_max):
        dp[j] = dp_pre[j]
    dp_pre, dp = dp, dp_pre
print(max(dp_pre))


Fixed submission:
n, t = map(int, input().split())
x = [list(map(int, input().split())) for _ in range(n)]
x.sort()
a_max = x[-1][0]

dp_pre = [0 for j in range(t + a_max)]
dp = [0 for j in range(t + a_max)]
for ai, bi in x:
    for j in range(ai):
        dp[j] = dp_pre[j]
    for j in range(ai, t + ai):
        dp[j] = max(dp_pre[j], dp_pre[j - ai] + bi)
    for j in range(t + ai, t + a_max):
        dp[j] = dp_pre[j]
    dp_pre, dp = dp, dp_pre
print(max(dp_pre))
Problem p02863 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10**9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, T, *AB = map(int, read().split())
    D = [(a, b) for a, b in zip(*[iter(AB)] * 2)]

    D.sort()

    dp = [0] * T
    ans = 0

    for i in range(N - 1):
        for t in range(T - 1, -1, -1):
            if t - D[i][0] >= 0 and dp[t] < dp[t - D[i][0]] + D[i][1]:
                dp[t] = dp[t - D[i][0]] + D[i][1]
        if ans < dp[T - 1] + D[i + 1][1]:
            ans = dp[T - 1] + D[i + 1][1]

    print(ans)
    return


if __name__ == "__main__":
    main()


Fixed submission:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10**9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, T, *AB = map(int, read().split())
    D = [(a, b) for a, b in zip(*[iter(AB)] * 2)]

    D.sort()

    dp = [0] * T
    ans = 0

    for i, (a, b) in enumerate(D[:-1]):
        for t in range(T - 1, a - 1, -1):
            if dp[t] < dp[t - a] + b:
                dp[t] = dp[t - a] + b
        if ans < dp[T - 1] + D[i + 1][1]:
            ans = dp[T - 1] + D[i + 1][1]

    print(ans)
    return


if __name__ == "__main__":
    main()
Problem p02863 (Python)

Buggy submission (status: Time Limit Exceeded):
N, T = map(int, input().split())
AB = []
for i in range(N):
    a, b = map(int, input().split())
    AB.append((a, b))
AB.sort()

ans = 0
dp = dp = [[0 for _ in range(3005)] for _ in range(3005)]
for i in range(N):
    a, b = AB[i]
    for j in range(T):
        # iを使わないパターン
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
        # iを使うパターン
        nj = j + a
        if nj < T:
            dp[i + 1][nj] = max(dp[i + 1][nj], dp[i][j] + b)
    now = dp[i][T - 1] + b
    ans = max(ans, now)

print(ans)


Fixed submission:
N, T = map(int, input().split())
AB = []
for i in range(N):
    a, b = map(int, input().split())
    AB.append((a, b))
AB.sort()

ans = 0
dp = [[0 for _ in range(3005)] for _ in range(3005)]
for i in range(N):
    a, b = AB[i]
    for j in range(T):
        # iを使わないパターン
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
        # iを使うパターン
        nj = j + a
        if nj < T:
            dp[i + 1][nj] = max(dp[i + 1][nj], dp[i][j] + b)
    now = dp[i][T - 1] + b
    ans = max(ans, now)

print(ans)
Problem p02863 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define loop(i, n) for (ll i = 0; i < n; i++)
#define rep(i, x, n) for (ll i = x; i <= n; i++)
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const ll N = 3e3 + 5, M = 5e5 + 5, MOD = 1e9 + 7, MAX = 5e6, OO = 1e9;

int memo[N][N], n, t;
pair<int, int> arr[N];

int dp(int i, int sum) {
  if (i >= n)
    return 0;
  if (sum >= t)
    return 0;
  int &res = memo[i][sum];
  if (res != -1)
    return res;
  return res =
             max(dp(i + 1, sum + arr[i].first) + arr[i].second, dp(i + 1, sum));
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "rt", stdin);
#endif
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  memset(memo, -1, sizeof(memo));
  cin >> n >> t;
  loop(i, n) { cin >> arr[i].first >> arr[i].second; }
  sort(arr, arr + n);
  cout << dp(0, 0) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define loop(i, n) for (ll i = 0; i < n; i++)
#define rep(i, x, n) for (ll i = x; i <= n; i++)
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const ll N = 3e3 + 5, M = 5e5 + 5, MOD = 1e9 + 7, MAX = 5e6, OO = 1e9;

int memo[N][N], n, t;
pair<int, int> arr[N];

int dp(int i, int sum) {
  if (i >= n)
    return 0;
  if (sum >= t)
    return 0;
  int &res = memo[i][sum];
  if (res != -1)
    return res;
  return res =
             max(dp(i + 1, sum + arr[i].first) + arr[i].second, dp(i + 1, sum));
}
int main() {
#ifndef ONLINE_JUDGE
  // freopen("in.txt", "rt", stdin);
#endif
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  memset(memo, -1, sizeof(memo));
  cin >> n >> t;
  loop(i, n) { cin >> arr[i].first >> arr[i].second; }
  sort(arr, arr + n);
  cout << dp(0, 0) << endl;
}
Problem p02863 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int maxn = 3000 + 5;
pii dish[maxn];
int dp[maxn][2 * maxn + 5], t;
bool cmp(const pii &p1, const pii &p2) {
  if ((p1.first < t && p2.first >= t) || (p1.first >= t && p2.first < t))
    return p1.first < p2.first;
  if ((double)p1.second / p1.first != (double)p2.second / p2.first)
    return (double)p1.second / p1.first > (double)p2.second / p2.first;
  else
    return p1.first <= p2.first;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n >> t;
  for (int i = 1; i <= n; ++i)
    cin >> dish[i].first >> dish[i].second;
  sort(dish + 1, dish + 1 + n, cmp);
  for (int i = 1; i <= n; ++i)
    for (int j = 0; j <= 2 * maxn; ++j)
      if (j < dish[i].first || j - dish[i].first >= t)
        dp[i][j] = dp[i - 1][j];
      else
        dp[i][j] =
            max(dp[i - 1][j], dp[i - 1][j - dish[i].first] + dish[i].second);
  cout << *max_element(dp[n], dp[n] + 2 * maxn + 3) << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int maxn = 3000 + 5;
pii dish[maxn];
int dp[maxn][2 * maxn + 5], t;
bool cmp(const pii &p1, const pii &p2) { return p1.first < p2.first; }
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n >> t;
  for (int i = 1; i <= n; ++i)
    cin >> dish[i].first >> dish[i].second;
  sort(dish + 1, dish + 1 + n, cmp);
  for (int i = 1; i <= n; ++i)
    for (int j = 0; j <= 2 * maxn; ++j)
      if (j < dish[i].first || j - dish[i].first >= t)
        dp[i][j] = dp[i - 1][j];
      else
        dp[i][j] =
            max(dp[i - 1][j], dp[i - 1][j - dish[i].first] + dish[i].second);
  cout << *max_element(dp[n], dp[n] + 2 * maxn + 3) << '\n';
  return 0;
}
Problem p02863 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;

#define REP(i, n) for (ll i = 0; i < n; i++)

int main() {
  ll n, t;
  cin >> n >> t;
  vector<pair<ll, ll>> v(n);
  REP(i, n) { cin >> v[i].first >> v[i].second; }
  sort(v.begin(), v.end());
  vector<ll> dp(t + 3000);
  REP(i, n) {
    vector<ll> tmp(t);
    REP(j, t) { tmp[j + v[i].first] = dp[j] + v[i].second; }
    REP(j, t + 3000) { dp[j] = max(dp[j], tmp[j]); }
  }
  cout << *max_element(dp.begin(), dp.end()) << endl;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;

#define REP(i, n) for (ll i = 0; i < n; i++)

int main() {
  ll n, t;
  cin >> n >> t;
  vector<pair<ll, ll>> v(n);
  REP(i, n) { cin >> v[i].first >> v[i].second; }
  sort(v.begin(), v.end());
  vector<ll> dp(t + 3000);
  REP(i, n) {
    vector<ll> tmp(t + 3000);
    REP(j, t) { tmp[j + v[i].first] = dp[j] + v[i].second; }
    REP(j, t + 3000) { dp[j] = max(dp[j], tmp[j]); }
  }
  cout << *max_element(dp.begin(), dp.end()) << endl;
}
Problem p02863 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <stdio.h>
#include <tuple>

#define FOR(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define REP(i, n) FOR(i, 0, n)

using lli = long long int;

constexpr int N = 3000;
std::pair<int, int> A[N];
std::array<lli, N + 1> R;

int main(void) {
  int n, t;
  scanf("%d%d", &n, &t);
  REP(i, n) scanf("%d%d", &A[i].first, &A[i].second);
  std::sort(A, A + n);
  REP(i, n) {
    int a = A[i].first, b = A[i].second;
    int j;
    for (j = t - 1; j + a >= t; --j)
      if (R[t] < R[j] + b)
        R[t] = R[j] + b;
    for (; j >= 0; --j)
      if (R[j + a] < R[j] + b)
        R[j + a] = R[j] + b;
  }
  lli res = 0;
  for (int i = 0; i <= t; ++i)
    if (res < R[i])
      res = R[i];
  printf("%lld\n", res);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <stdio.h>
#include <tuple>

#define FOR(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define REP(i, n) FOR(i, 0, n)

using lli = long long int;

constexpr int N = 3000;
std::pair<int, int> A[N];
std::array<lli, N + 1> R;

int main(void) {
  int n, t;
  scanf("%d%d", &n, &t);
  REP(i, n) scanf("%d%d", &A[i].first, &A[i].second);
  std::sort(A, A + n);
  REP(i, n) {
    int a = A[i].first, b = A[i].second;
    int j, e = (t > a) ? t - a : 0;
    for (j = t - 1; j >= e; --j)
      if (R[t] < R[j] + b)
        R[t] = R[j] + b;
    for (; j >= 0; --j)
      if (R[j + a] < R[j] + b)
        R[j + a] = R[j] + b;
  }
  lli res = 0;
  for (int i = 0; i <= t; ++i)
    if (res < R[i])
      res = R[i];
  printf("%lld\n", res);
  return 0;
}
Problem p02863 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string.h>
#include <vector>

using namespace std;

int i, j, k, T, A[3000], B[3000], N, memo1[3000][3001], memo2[3000][3001], ans;

int dp1(int n, int m) {
  int s, t, u;

  if (memo1[n][m] != -1) {
    return memo1[n][m];
  }

  if (n == 0) {
    if (A[n] <= m) {
      memo1[n][m] = B[n];
      return B[n];
    } else {
      memo1[n][m] = 0;
      return 0;
    }
  }

  t = dp1(n - 1, m);
  if (A[n] <= m) {
    t = max(t, dp1(n - 1, m - A[n]) + B[n]);
  }

  memo1[n][m] = t;
  return t;
}

int dp2(int n, int m) {
  int s, t, u;

  if (memo2[n][m] != -1) {
    return memo2[n][m];
  }

  if (n == N - 1) {
    if (A[n] <= m) {
      memo1[n][m] = B[n];
      return B[n];
    } else {
      memo1[n][m] = 0;
      return 0;
    }
  }

  t = dp2(n + 1, m);
  if (A[n] <= m) {
    t = max(t, dp2(n + 1, m - A[n]) + B[n]);
  }

  memo2[n][m] = t;
  return t;
}

int main() {

  cin >> N >> T;
  for (i = 0; i < N; i++) {
    cin >> A[i] >> B[i];
  }

  for (i = 0; i < N; i++) {
    for (j = 0; j < T + 1; j++) {
      memo1[i][j] = -1;
      memo2[i][j] = -1;
    }
    memo1[i][0] = 0;
    memo2[i][0] = 0;
  }

  ans = 0;
  for (i = 0; i < N; i++) {
    for (j = 0; j <= T - 1; j++) {
      ans = max(ans, dp1(i - 1, j) + dp2(i + 1, T - j - 1) + B[i]);
    }
  }

  cout << ans;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string.h>
#include <vector>

using namespace std;

int i, j, k, T, A[3000], B[3000], N, memo1[3000][3001], memo2[3000][3001], ans;

int dp1(int n, int m) {
  int s, t, u;

  if (memo1[n][m] != -1) {
    return memo1[n][m];
  }

  if (n == 0) {
    if (A[n] <= m) {
      memo1[n][m] = B[n];
      return B[n];
    } else {
      memo1[n][m] = 0;
      return 0;
    }
  }

  t = dp1(n - 1, m);
  if (A[n] <= m) {
    t = max(t, dp1(n - 1, m - A[n]) + B[n]);
  }

  memo1[n][m] = t;
  return t;
}

int dp2(int n, int m) {
  int s, t, u;

  if (memo2[n][m] != -1) {
    return memo2[n][m];
  }

  if (n == N - 1) {
    if (A[n] <= m) {
      memo1[n][m] = B[n];
      return B[n];
    } else {
      memo1[n][m] = 0;
      return 0;
    }
  }

  t = dp2(n + 1, m);
  if (A[n] <= m) {
    t = max(t, dp2(n + 1, m - A[n]) + B[n]);
  }

  memo2[n][m] = t;
  return t;
}

int main() {

  cin >> N >> T;
  for (i = 0; i < N; i++) {
    cin >> A[i] >> B[i];
  }

  for (i = 0; i < N; i++) {
    for (j = 0; j < T + 1; j++) {
      memo1[i][j] = -1;
      memo2[i][j] = -1;
    }
    memo1[i][0] = 0;
    memo2[i][0] = 0;
  }

  ans = 0;
  ans = max(ans, dp2(1, T - 1) + B[0]);
  ans = max(ans, dp1(N - 2, T - 1) + B[N - 1]);

  for (i = 1; i < N - 1; i++) {
    for (j = 0; j <= T - 1; j++) {
      ans = max(ans, dp1(i - 1, j) + dp2(i + 1, T - j - 1) + B[i]);
    }
  }

  cout << ans;

  return 0;
}
Problem p02863 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
const int man = 3e3 + 10;
#define IOS ios::sync_with_stdio(0)
template <typename T> T gcd(T a, T b) { return b == 0 ? a : gcd(b, a % b); }
template <typename T> T exgcd(T a, T b, T &g, T &x, T &y) {
  if (!b) {
    g = a, x = 1, y = 0;
  } else {
    exgcd(b, a % b, g, y, x);
    y -= x * (a / b);
  }
}
#ifndef ONLINE_JUDGE
#define debug(fmt, ...)                                                        \
  {                                                                            \
    printf("debug ");                                                          \
    printf(fmt, ##__VA_ARGS__);                                                \
    puts("");                                                                  \
  }
#else
#define debug(fmt, ...)
#endif
typedef long long ll;
const ll mod = 1e9 + 7;
struct node {
  int w, v;
  friend bool operator<(node a, node b) { return a.w < b.w; }
} a[man];
int dp[man];

int main() {
#ifndef ONLINE_JUDGE
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt","w",stdout);
#endif
  int n, t;
  cin >> n >> t;
  for (int i = 1; i <= n; i++)
    cin >> a[i].w >> a[i].v;
  sort(a + 1, a + 1 + n);
  for (int i = 1; i <= n; i++) {
    for (int j = t + a[i].w - 1; j >= a[i].w; j--) {
      dp[j] = max(dp[j], dp[j - a[i].w] + a[i].v);
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans = max(ans, dp[t + a[i].w - 1]);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
const int man = 1e4 + 10;
#define IOS ios::sync_with_stdio(0)
template <typename T> T gcd(T a, T b) { return b == 0 ? a : gcd(b, a % b); }
template <typename T> T exgcd(T a, T b, T &g, T &x, T &y) {
  if (!b) {
    g = a, x = 1, y = 0;
  } else {
    exgcd(b, a % b, g, y, x);
    y -= x * (a / b);
  }
}
#ifndef ONLINE_JUDGE
#define debug(fmt, ...)                                                        \
  {                                                                            \
    printf("debug ");                                                          \
    printf(fmt, ##__VA_ARGS__);                                                \
    puts("");                                                                  \
  }
#else
#define debug(fmt, ...)
#endif
typedef long long ll;
const ll mod = 1e9 + 7;
struct node {
  int w, v;
  friend bool operator<(node a, node b) { return a.w < b.w; }
} a[man];
int dp[man];

int main() {
#ifndef ONLINE_JUDGE
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt","w",stdout);
#endif
  int n, t;
  cin >> n >> t;
  for (int i = 1; i <= n; i++)
    cin >> a[i].w >> a[i].v;
  sort(a + 1, a + 1 + n);
  for (int i = 1; i <= n; i++) {
    for (int j = t + a[i].w - 1; j >= a[i].w; j--) {
      dp[j] = max(dp[j], dp[j - a[i].w] + a[i].v);
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans = max(ans, dp[t + a[i].w - 1]);
  cout << ans << endl;
  return 0;
}
Problem p02863 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define all(x) (x).begin(), (x).end()

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, t;
  cin >> n >> t;
  struct di {
    ll a, b;
  };
  vector<di> ds(n);
  rep(i, n) { cin >> ds[i].a >> ds[i].b; }
  sort(all(ds), [&](di a, di b) { return a.a < b.b; });
  vector<ll> dp(t);
  ll res = 0;
  rep(i, n) {
    auto tdp = dp;
    rep(j, t) {
      if (j + ds[i].a < t)
        chmax(tdp[j + ds[i].a], dp[j] + ds[i].b);
      else
        chmax(res, dp[j] + ds[i].b);
    }
    dp = tdp;
  }
  rep(i, t) { chmax(res, dp[i]); }
  cout << res << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define all(x) (x).begin(), (x).end()

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, t;
  cin >> n >> t;
  struct di {
    ll a, b;
  };
  vector<di> ds(n);
  rep(i, n) { cin >> ds[i].a >> ds[i].b; }
  sort(all(ds), [&](di a, di b) { return a.a < b.a; });
  vector<ll> dp(t);
  ll res = 0;
  rep(i, n) {
    auto tdp = dp;
    rep(j, t) {
      if (j + ds[i].a < t)
        chmax(tdp[j + ds[i].a], dp[j] + ds[i].b);
      else
        chmax(res, dp[j] + ds[i].b);
    }
    dp = tdp;
  }
  rep(i, t) { chmax(res, dp[i]); }
  cout << res << endl;

  return 0;
}
Problem p02863 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using Int = int_fast64_t;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, t;
  cin >> n >> t;
  using P = pair<int, int>;
  vector<P> v(n);
  for (int i = 0; i < n; ++i) {
    int a, b;
    cin >> a >> b;
    v[i] = P(a, b);
  }
  sort(v.begin(), v.end());
  vector<vector<int>> dp(n, vector<int>(t));
  dp[0][v[0].first] = v[0].second;
  for (int i = 1; i < t; ++i)
    dp[0][i] = max(dp[0][i], dp[0][i - 1]);
  for (int i = 1; i < n - 1; ++i)
    for (int j = 1; j < t; ++j) {
      dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
      if (j - v[i].first >= 0)
        dp[i][j] = max(dp[i][j], dp[i - 1][j - v[i].first] + v[i].second);
    }
  //	for(int i=0; i<n; ++i)
  //		cout << i << " " << v[i].first << " " << v[i].second << "\n";
  for (int i = n - 2; i >= 0; --i)
    v[i].second = max(v[i].second, v[i + 1].second);
  int ans = 0;
  for (int i = 0; i < n - 1; ++i) {
    ans = max(ans, dp[i][t - 1] + v[i + 1].second);
    //		cout << ans << " " << i << " " << dp[i][t-1] << "\n";
  }
  cout << ans << "\n";
  //	for(int i=2; i<4; ++i)
  //		for(int j=0; j<t; ++j)
  //			cout << i << " " << j << " " << dp[i][j] << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using Int = int_fast64_t;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, t;
  cin >> n >> t;
  using P = pair<int, int>;
  vector<P> v(n);
  for (int i = 0; i < n; ++i) {
    int a, b;
    cin >> a >> b;
    v[i] = P(a, b);
  }
  sort(v.begin(), v.end());
  vector<vector<int>> dp(n, vector<int>(t));
  if (v[0].first < t)
    dp[0][v[0].first] = v[0].second;
  for (int i = 1; i < t; ++i)
    dp[0][i] = max(dp[0][i], dp[0][i - 1]);
  for (int i = 1; i < n - 1; ++i)
    for (int j = 1; j < t; ++j) {
      dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
      if (j - v[i].first >= 0)
        dp[i][j] = max(dp[i][j], dp[i - 1][j - v[i].first] + v[i].second);
    }
  //	for(int i=0; i<n; ++i)
  //		cout << i << " " << v[i].first << " " << v[i].second << "\n";
  for (int i = n - 2; i >= 0; --i)
    v[i].second = max(v[i].second, v[i + 1].second);
  int ans = 0;
  for (int i = 0; i < n - 1; ++i) {
    ans = max(ans, dp[i][t - 1] + v[i + 1].second);
    //		cout << ans << " " << i << " " << dp[i][t-1] << "\n";
  }
  cout << ans << "\n";
  //	for(int i=2; i<4; ++i)
  //		for(int j=0; j<t; ++j)
  //			cout << i << " " << j << " " << dp[i][j] << "\n";
}
Problem p02863 (C++)

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef struct {
  int a;
  int b;
} Eat;
int comp(const void *a, const void *b) {
  Eat c = *(Eat *)a;
  Eat d = *(Eat *)b;
  return c.a - d.a;
}
int main(void) {
  int n, t;
  cin >> n >> t;
  Eat eat[3002];
  for (int i = 0; i < n; i++) {
    cin >> eat[i].a >> eat[i].b;
  }
  qsort(eat, n, sizeof(Eat), comp);
  // cout << eat[1].b << endl;
  int **dp;
  int h = 3001, w = 3001;
  dp = (int **)malloc(sizeof(int *) * w);
  for (int i = 0; i <= w; i++) {
    dp[i] = (int *)malloc(sizeof(int) * h);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= t - 1; j++) {
      if (j >= eat[i].a)
        dp[i + 1][j] = max(dp[i][j - eat[i].a] + eat[i].b, dp[i][j]);
      else
        dp[i + 1][j] = dp[i][j];
    }
  }
  /*for(int i=0;i<n;i++){
      for(int j=0;j<=t-1;j++){
          printf("%d ",dp[i][j]);
      }puts("");
  }*/
  int MAX = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++)
      MAX = max(dp[i][t - 1] + eat[j].b, MAX);
  }
  cout << MAX << endl;
  free(dp);
}


Fixed submission:
#include <cstring>
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef struct {
  int a;
  int b;
} Eat;
int comp(const void *a, const void *b) {
  Eat c = *(Eat *)a;
  Eat d = *(Eat *)b;
  return c.a - d.a;
}
int main(void) {
  int n, t;
  cin >> n >> t;
  Eat eat[3002];
  for (int i = 0; i < n; i++) {
    cin >> eat[i].a >> eat[i].b;
  }
  qsort(eat, n, sizeof(Eat), comp);
  // cout << eat[1].b << endl;
  int **dp;
  int h = 3001, w = 3001;
  dp = (int **)malloc(sizeof(int *) * w);
  for (int i = 0; i <= w; i++) {
    dp[i] = (int *)malloc(sizeof(int) * h);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= t - 1; j++) {
      if (j >= eat[i].a)
        dp[i + 1][j] = max(dp[i][j - eat[i].a] + eat[i].b, dp[i][j]);
      else
        dp[i + 1][j] = dp[i][j];
    }
  }
  /*for(int i=0;i<n;i++){
      for(int j=0;j<=t-1;j++){
          printf("%d ",dp[i][j]);
      }puts("");
  }*/
  int MAX = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++)
      MAX = max(dp[i][t - 1] + eat[j].b, MAX);
  }
  cout << MAX << endl;
}
Problem p02863 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
typedef pair<int, int> pint;
typedef pair<ll, int> pli;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
int dpa[3000][3000];
int dpb[3000][3000];

int main() {
  int N, T;
  cin >> N >> T;
  int a[N], b[N];
  rep(i, N) { cin >> a[i] >> b[i]; }
  rep(i, N) {
    rep(j, T) {
      if (j >= a[i])
        dpa[i + 1][j] = max(dpa[i][j], dpa[i][j - a[i]] + b[i]);
      else
        dpa[i + 1][j] = dpa[i][j];
    }
  }
  rep(i, N) {
    rep(j, T) {
      if (j >= a[N - 1 - i])
        dpb[i + 1][j] = max(dpb[i][j], dpb[i][j - a[N - 1 - i]] + b[N - 1 - i]);
      else
        dpb[i + 1][j] = dpb[i][j];
    }
  }
  int ans = 0;
  rep(i, N) {
    rep(j, T) { ans = max(dpa[i][j] + dpb[N - i - 1][T - 1 - j] + b[i], ans); }
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
typedef pair<int, int> pint;
typedef pair<ll, int> pli;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
int dpa[3010][3010];
int dpb[3010][3010];

int main() {
  int N, T;
  cin >> N >> T;
  int a[N], b[N];
  rep(i, N) { cin >> a[i] >> b[i]; }
  rep(i, N) {
    rep(j, T) {
      if (j >= a[i])
        dpa[i + 1][j] = max(dpa[i][j], dpa[i][j - a[i]] + b[i]);
      else
        dpa[i + 1][j] = dpa[i][j];
    }
  }
  rep(i, N) {
    rep(j, T) {
      if (j >= a[N - 1 - i])
        dpb[i + 1][j] = max(dpb[i][j], dpb[i][j - a[N - 1 - i]] + b[N - 1 - i]);
      else
        dpb[i + 1][j] = dpb[i][j];
    }
  }
  int ans = 0;
  rep(i, N) {
    rep(j, T) { ans = max(dpa[i][j] + dpb[N - i - 1][T - 1 - j] + b[i], ans); }
  }
  cout << ans << endl;
}
Problem p02863 (C++)

Buggy submission (status: Runtime Error):
// g++ -std=c++14 test.cpp -o test.out
#include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <stdlib.h>
#include <string.h>
#include <utility>
#include <vector>
using namespace std;

#define LL long long int
const LL INF = (LL)(1e18) + 1;
const LL mod = 1000000007ll;

LL N, T;
vector<pair<LL, LL>> dish; // pair<所要時間、満足度>

LL ans = 0;

bool compare_by_b(pair<int, int> a, pair<int, int> b) {
  if (a.second != b.second) {
    return a.first < b.first;
  } else {
    return a.second < b.second;
  }
}

void solve() {
  sort(dish.begin(), dish.end(), compare_by_b); // 昇順に並べ替え
  vector<vector<LL>> dp(3000, vector<LL>(3000, 0));
  // i-1個目までの料理から、
  // A（所要時間）の和がt以下となるよう選択した時の、
  // Bの和（満足度）の最大値をdp[i][t]とする
  // ただしdp[0][t] = 0
  for (int i = 0; i < N; i++) { // 食べる順番は考慮しなくても最大値を求められる
    // dp[i+1]をdp[i]から求める
    for (int t = 0; t < T; t++) {
      // i番目を食べない場合
      // この場合 所要時間は変化しない
      dp[i + 1][t] = max(dp[i + 1][t], dp[i][t]);
      // i番目を食べる場合
      // この場合所要時間にdish[i].firstを足す
      if (t + dish[i].first < T) {
        dp[i + 1][t + dish[i].first] =
            max(dp[i + 1][t], dp[i][t] + dish[i].second);
      }
    }
    ans = max(ans, dp[i][T - 1] + dish[i].second);
  }
}

int main() {
  cin >> N >> T;
  for (int i = 0; i < N; i++) {
    LL a, b;
    cin >> a >> b;
    dish.push_back(make_pair(a, b));
  }

  solve();

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

Fixed submission:
// g++ -std=c++14 test.cpp -o test.out
#include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <stdlib.h>
#include <string.h>
#include <utility>
#include <vector>
using namespace std;

#define LL long long int
const LL INF = (LL)(1e18) + 1;
const LL mod = 1000000007ll;

LL N, T;
vector<pair<LL, LL>> dish; // pair<所要時間、満足度>

LL ans = 0;

bool compare_by_b(pair<int, int> a, pair<int, int> b) {
  if (a.second != b.second) {
    return a.first < b.first;
  } else {
    return a.second < b.second;
  }
}

void solve() {
  sort(dish.begin(), dish.end(), compare_by_b); // 昇順に並べ替え
  vector<vector<LL>> dp(3001, vector<LL>(3001, 0));
  // i-1個目までの料理から、
  // A（所要時間）の和がt以下となるよう選択した時の、
  // Bの和（満足度）の最大値をdp[i][t]とする
  // ただしdp[0][t] = 0
  for (int i = 0; i < N; i++) { // 食べる順番は考慮しなくても最大値を求められる
    // dp[i+1]をdp[i]から求める
    for (int t = 0; t < T; t++) {
      // i番目を食べない場合
      // この場合 所要時間は変化しない
      dp[i + 1][t] = max(dp[i + 1][t], dp[i][t]);
      // i番目を食べる場合
      // この場合所要時間にdish[i].firstを足す
      if (t + dish[i].first < T) {
        dp[i + 1][t + dish[i].first] =
            max(dp[i + 1][t], dp[i][t] + dish[i].second);
      }
    }
    ans = max(ans, dp[i][T - 1] + dish[i].second);
  }
}

int main() {
  cin >> N >> T;
  for (int i = 0; i < N; i++) {
    LL a, b;
    cin >> a >> b;
    dish.push_back(make_pair(a, b));
  }

  solve();

  cout << ans << endl;
  return 0;
}
Problem p02863 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long ans[3001][6001];

int main() {
  int n, t;
  cin >> n >> t;
  int a[n], b[n];
  for (int i = 0; i < n; i++)
    cin >> a[i] >> b[i];
  vector<pair<int, int>> x;
  for (int i = 0; i < n; i++)
    x.push_back(make_pair(a[i], b[i]));
  sort(x.begin(), x.end());
  memset(ans, 0, sizeof(ans));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= t + 3000; j++) {
      if (0 <= j - x[i].first && j - x[i].first < t)
        ans[i + 1][j] = max(ans[i][j], ans[i][j - x[i].second] + x[i].second);
      else if (0 <= j - x[i].first)
        ans[i + 1][j] = max(ans[i + 1][j - 1], ans[i][j]);
      else
        ans[i + 1][j] = ans[i][j];
    }
  }
  cout << ans[n][t + 3000] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long ans[3001][6001];

int main() {
  int n, t;
  cin >> n >> t;
  int a[n], b[n];
  for (int i = 0; i < n; i++)
    cin >> a[i] >> b[i];
  vector<pair<int, int>> x;
  for (int i = 0; i < n; i++)
    x.push_back(make_pair(a[i], b[i]));
  sort(x.begin(), x.end());
  memset(ans, 0, sizeof(ans));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= t + 3000; j++) {
      if (0 <= j - x[i].first && j - x[i].first < t)
        ans[i + 1][j] = max(ans[i][j], ans[i][j - x[i].first] + x[i].second);
      else if (0 <= j - x[i].first)
        ans[i + 1][j] = max(ans[i + 1][j - 1], ans[i][j]);
      else
        ans[i + 1][j] = ans[i][j];
    }
  }
  cout << ans[n][t + 3000] << endl;
  return 0;
}