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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define lli long long int
using namespace std;
#define mod 1000000007
#define MOD 1000000037
#define mod1 998244353
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define INF 1e18

int main() {
  fastio;
  int n;
  cin >> n;
  vector<pair<pair<int, int>, int>> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i].first.first >> v[i].first.second >> v[i].second;
  vector<pair<int, int>> sv(n);
  for (int i = 0; i < n; i++) {
    sv[i].first = v[i].first.first + v[i].first.second;
    sv[i].second = i;
  }
  sort(sv.begin(), sv.end());
  int l = 1e5 - 1;
  vector<vector<lli>> dp(n + 1, vector<lli>(1e5, 0));
  // lli dp[n+1][l+1];
  for (int i = 0; i <= l; i++) {
    for (int j = 0; j <= n; j++) {
      if (i == 0 or j == 0) {
        dp[j][i] = 0;
        continue;
      }
      int ind = sv[j - 1].second;
      int w = v[ind].first.first, st = v[ind].first.second;
      dp[j][i] = max(dp[j - 1][i], dp[j][i - 1]);
      lli a = 0;
      if (w <= i and i - w <= st)
        a = v[ind].second * 1LL + dp[j - 1][i - w];
      dp[j][i] = max(dp[j][i], a);
    }
  }
  // for(int i=0;i<=l;i++)
  // {
  //     for(int j=0;j<=n;j++)
  //         cout << dp[j][i] << " ";
  //     cout << endl;
  // }
  lli ans = 0;
  for (int i = 1; i <= n; i++)
    ans = max(ans, dp[i][l]);
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define lli long long int
using namespace std;
#define mod 1000000007
#define MOD 1000000037
#define mod1 998244353
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define INF 1e18

int main() {
  fastio;
  int n;
  cin >> n;
  vector<pair<pair<int, int>, int>> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i].first.first >> v[i].first.second >> v[i].second;
  vector<pair<int, int>> sv(n);
  for (int i = 0; i < n; i++) {
    sv[i].first = v[i].first.first + v[i].first.second;
    sv[i].second = i;
  }
  sort(sv.begin(), sv.end());
  int l = 2e4;
  vector<vector<lli>> dp(n + 1, vector<lli>(2e4 + 1, 0));
  // lli dp[n+1][l+1];
  for (int i = 0; i <= l; i++) {
    for (int j = 0; j <= n; j++) {
      if (i == 0 or j == 0) {
        dp[j][i] = 0;
        continue;
      }
      int ind = sv[j - 1].second;
      int w = v[ind].first.first, st = v[ind].first.second;
      dp[j][i] = max(dp[j - 1][i], dp[j][i - 1]);
      lli a = 0;
      if (w <= i and i - w <= st)
        a = v[ind].second * 1LL + dp[j - 1][i - w];
      dp[j][i] = max(dp[j][i], a);
    }
  }
  // for(int i=0;i<=l;i++)
  // {
  //     for(int j=0;j<=n;j++)
  //         cout << dp[j][i] << " ";
  //     cout << endl;
  // }
  lli ans = 0;
  for (int i = 1; i <= n; i++)
    ans = max(ans, dp[i][l]);
  cout << ans << endl;
}
Problem p03183 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>

using namespace std;
// State
// dp[i][j]: The maximum value we can get by using blocks from
//  0...i and sum of blocks in the tower = j

// Initialization
// dp[0][W[0]] = V[0]

// Transitions
// dp[i][j] = max(V[j]+dp[i-W[0]][j-1], dp[i][j-1])

// Order of evaluation
// Rowise or Columnwise

// Answer: argmax(dp[0...W_max][N])

struct B {
  long long w;
  long long s;
  long long v;
};

vector<vector<long long>> dp(10004, vector<long long>(1004, 0));

bool compare(B b1, B b2) { return (b1.w + b1.s <= b2.w + b2.s); }

int main() {
  long long N;
  cin >> N;
  vector<B> boxes(N);
  for (long long i = 0; i < N; i++) {
    long long w, s, v;
    cin >> w >> s >> v;
    boxes[i].w = w;
    boxes[i].s = s;
    boxes[i].v = v;
  }
  sort(boxes.begin(), boxes.end(), compare);
  long long W_max = 20004;
  dp[boxes[0].w][0] = boxes[0].v;
  for (int i = 0; i < W_max; i++) {
    if (i >= boxes[0].w) {
      dp[i][0] = boxes[0].v;
    }
  }
  for (long long j = 1; j < N; j++) {
    for (long long i = 0; i < W_max; i++) {
      if (i - boxes[j].w >= 0 && boxes[j].s + boxes[j].w - i >= 0) {
        dp[i][j] = max(dp[i - boxes[j].w][j - 1] + boxes[j].v, dp[i][j - 1]);
      } else {
        dp[i][j] = dp[i][j - 1];
      }
    }
  }
  long long result = 0;
  for (long long i = 0; i < W_max; i++) {
    if (result < dp[i][N - 1])
      result = dp[i][N - 1];
  }
  cout << result << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>

using namespace std;
// State
// dp[i][j]: The maximum value we can get by using blocks from
//  0...i and sum of blocks in the tower = j

// Initialization
// dp[0][W[0]] = V[0]

// Transitions
// dp[i][j] = max(V[j]+dp[i-W[0]][j-1], dp[i][j-1])

// Order of evaluation
// Rowise or Columnwise

// Answer: argmax(dp[0...W_max][N])

struct B {
  long long w;
  long long s;
  long long v;
};

vector<vector<long long>> dp(20004, vector<long long>(1004, 0));

bool compare(B b1, B b2) { return (b1.w + b1.s <= b2.w + b2.s); }

int main() {
  long long N;
  cin >> N;
  vector<B> boxes(N);
  for (long long i = 0; i < N; i++) {
    long long w, s, v;
    cin >> w >> s >> v;
    boxes[i].w = w;
    boxes[i].s = s;
    boxes[i].v = v;
  }
  sort(boxes.begin(), boxes.end(), compare);
  long long W_max = 20004;
  dp[boxes[0].w][0] = boxes[0].v;
  for (int i = 0; i < W_max; i++) {
    if (i >= boxes[0].w) {
      dp[i][0] = boxes[0].v;
    }
  }
  for (long long j = 1; j < N; j++) {
    for (long long i = 0; i < W_max; i++) {
      if (i - boxes[j].w >= 0 && boxes[j].s + boxes[j].w - i >= 0) {
        dp[i][j] = max(dp[i - boxes[j].w][j - 1] + boxes[j].v, dp[i][j - 1]);
      } else {
        dp[i][j] = dp[i][j - 1];
      }
    }
  }
  long long result = 0;
  for (long long i = 0; i < W_max; i++) {
    if (result < dp[i][N - 1])
      result = dp[i][N - 1];
  }
  cout << result << endl;
}
Problem p03183 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define i_7 (ll)(1E9 + 7)
#define i_5 (ll)(1E9 + 5)
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  else
    return c + i_7;
}
typedef pair<int, int> i_i;
typedef pair<ll, ll> l_l;
ll inf = (ll)1E12;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(ll *pos, ll val) {
  *pos = max(*pos, val);
} // Max(&dp[i][j],dp[i-1][j]);
void Min(ll *pos, ll val) { *pos = min(*pos, val); }
void Add(ll *pos, ll val) { *pos = mod(*pos + val); }
const long double EPS = 1E-8;
////////////////////////////////////////

struct tower {
  ll w, s, v;
};
bool comp(tower t1, tower t2) {
  if (t1.w + t1.s < t2.w + t2.s)
    return true;
  else
    return false;
}

int main() {
  ll n;
  cin >> n;
  tower t[n];
  rep(i, 0, n - 1) { cin >> t[i].w >> t[i].s >> t[i].v; }
  sort(t, t + n, comp);
  ll N = (ll)200001;
  ll dp[n + 1][N];
  memset(dp, 0, sizeof(dp));
  rep(i, 0, n - 1) {
    rep(j, 0, N - 1) {
      dp[i + 1][j] = dp[i][j];
      if (0 <= j - t[i].w && j - t[i].w <= t[i].s)
        Max(&dp[i + 1][j], dp[i][j - t[i].w] + t[i].v);
    }
  }
  /*rep(i,0,n){
      rep(j,0,20){
          cout<<dp[i][j]<<" ";
      }cout<<endl;
  }*/
  ll ans = 0;
  rep(i, 0, N - 1) Max(&ans, dp[n][i]);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define i_7 (ll)(1E9 + 7)
#define i_5 (ll)(1E9 + 5)
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  else
    return c + i_7;
}
typedef pair<int, int> i_i;
typedef pair<ll, ll> l_l;
ll inf = (ll)1E12;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(ll *pos, ll val) {
  *pos = max(*pos, val);
} // Max(&dp[i][j],dp[i-1][j]);
void Min(ll *pos, ll val) { *pos = min(*pos, val); }
void Add(ll *pos, ll val) { *pos = mod(*pos + val); }
const long double EPS = 1E-8;
////////////////////////////////////////

struct tower {
  ll w, s, v;
};
bool comp(tower t1, tower t2) {
  if (t1.w + t1.s < t2.w + t2.s)
    return true;
  else
    return false;
}

int main() {
  ll n;
  cin >> n;
  tower t[n];
  rep(i, 0, n - 1) { cin >> t[i].w >> t[i].s >> t[i].v; }
  sort(t, t + n, comp);
  ll N = (ll)2E4 + 1;
  ll dp[n + 1][N];
  memset(dp, 0, sizeof(dp));
  rep(i, 0, n - 1) {
    rep(j, 0, N - 1) {
      dp[i + 1][j] = dp[i][j];
      if (0 <= j - t[i].w && j - t[i].w <= t[i].s)
        Max(&dp[i + 1][j], dp[i][j - t[i].w] + t[i].v);
    }
  }
  /*rep(i,0,n){
      rep(j,0,20){
          cout<<dp[i][j]<<" ";
      }cout<<endl;
  }*/
  ll ans = 0;
  rep(i, 0, N - 1) Max(&ans, dp[n][i]);
  cout << ans << endl;
  return 0;
}
Problem p03183 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define all(a) (a).begin(), (a).end()
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define sz() size()
#define fr first
#define sc second
#define pb push_back
#define er erase
#define in insert
#define pi pair<int, int>
#define pii pair<pair<int, int>, int>
#define mp make_pair
#define int long long
#define rc(s) return cout << s, 0
#define rcc(s) cout << s, exit(0)
using namespace std;

const int mod = 1e9 + 7;
const int modx = 998244353;
const int per = 666013;

int n, dp[1005][200100];

struct block {
  int x, y, z;
  bool operator<(block &blok) { return x + y < blok.x + blok.y; }
} bloc[1005];

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cerr.tie(0);
  cout.tie(0);
  srand(chrono::steady_clock::now().time_since_epoch().count());
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> bloc[i].x >> bloc[i].y >> bloc[i].z;
  }
  sort(bloc + 1, bloc + n + 1);
  for (int i = 1; i <= n; i++) {
    for (int w = 0; w <= 20005; w++) {
      dp[i][w] = dp[i - 1][w];
      if (w - bloc[i].x >= 0 && w - bloc[i].x <= bloc[i].y)
        dp[i][w] = max(dp[i][w], dp[i - 1][w - bloc[i].x] + bloc[i].z);
    }
  }
  int res = 0LL;
  for (int i = 0LL; i <= 20005LL; i++)
    res = max(res, dp[n][i]);
  cout << res;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define all(a) (a).begin(), (a).end()
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define sz() size()
#define fr first
#define sc second
#define pb push_back
#define er erase
#define in insert
#define pi pair<int, int>
#define pii pair<pair<int, int>, int>
#define mp make_pair
#define int long long
#define rc(s) return cout << s, 0
#define rcc(s) cout << s, exit(0)
using namespace std;

const int mod = 1e9 + 7;
const int modx = 998244353;
const int per = 666013;

int n, dp[1005][20100];

struct block {
  int x, y, z;
  bool operator<(block &blok) { return x + y < blok.x + blok.y; }
} bloc[1005];

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cerr.tie(0);
  cout.tie(0);
  srand(chrono::steady_clock::now().time_since_epoch().count());
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> bloc[i].x >> bloc[i].y >> bloc[i].z;
  }
  sort(bloc + 1, bloc + n + 1);
  for (int i = 1; i <= n; i++) {
    for (int w = 0; w <= 20005; w++) {
      dp[i][w] = dp[i - 1][w];
      if (w - bloc[i].x >= 0 && w - bloc[i].x <= bloc[i].y)
        dp[i][w] = max(dp[i][w], dp[i - 1][w - bloc[i].x] + bloc[i].z);
    }
  }
  int res = 0LL;
  for (int i = 0LL; i <= 20005LL; i++)
    res = max(res, dp[n][i]);
  cout << res;
}
Problem p03183 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
// #include<bits/extc++.h>
using namespace std;
// using namespace __gnu_pbds;
// typedef
// tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
// set_t;
#define mp(a, b) make_pair((a), (b))
#define pii pair<int, int>
#define pll pair<LL, LL>
#define pdd pair<double, double>
#define pb push_back
#define x first
#define y second
#define sqr(x) ((x) * (x))
#define EPS 1e-6
#define MEM(x) memset(x, 0, sizeof(x))
#define MEMS(x) memset(x, -1, sizeof(x))
#define pi acos(-1)
#define index Index
#define Line pll
typedef long long LL;
int main() {
  int n;
  scanf("%d", &n);
  pair<int, pii> p[1005];
  for (int i = 0; i < n; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    p[i] = mp(a + b, mp(a, c));
  }
  sort(p, p + n);
  LL dp[10005];
  MEM(dp);
  LL ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = p[i].x - p[i].y.x; j >= 0; j--) {
      dp[j + p[i].y.x] = max(dp[j + p[i].y.x], dp[j] + p[i].y.y);
      ans = max(ans, dp[j + p[i].y.x]);
    }
  }
  printf("%lld\n", ans);
}


Fixed submission:
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
// #include<bits/extc++.h>
using namespace std;
// using namespace __gnu_pbds;
// typedef
// tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
// set_t;
#define mp(a, b) make_pair((a), (b))
#define pii pair<int, int>
#define pll pair<LL, LL>
#define pdd pair<double, double>
#define pb push_back
#define x first
#define y second
#define sqr(x) ((x) * (x))
#define EPS 1e-6
#define MEM(x) memset(x, 0, sizeof(x))
#define MEMS(x) memset(x, -1, sizeof(x))
#define pi acos(-1)
#define index Index
#define Line pll
typedef long long LL;
int main() {
  int n;
  scanf("%d", &n);
  pair<int, pii> p[1005];
  for (int i = 0; i < n; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    p[i] = mp(a + b, mp(a, c));
  }
  sort(p, p + n);
  LL dp[20005];
  MEM(dp);
  LL ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = p[i].x - p[i].y.x; j >= 0; j--) {
      dp[j + p[i].y.x] = max(dp[j + p[i].y.x], dp[j] + p[i].y.y);
      ans = max(ans, dp[j + p[i].y.x]);
    }
  }
  printf("%lld\n", ans);
}
Problem p03183 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define maxc 20005
#define maxn 105
using namespace std;
struct Pt {
  int w, s, v;
  void RD() { cin >> w >> s >> v; }
  bool operator<(const Pt &a) const { return w + s < a.w + a.s; }
};

int dp[maxc] = {};
Pt arr[maxn];
int n;

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    arr[i].RD();
  }
  sort(arr + 1, arr + n + 1);
  for (int i = 1; i <= n; i++) {
    int mx = arr[i].s + arr[i].w;
    for (int j = mx; j >= arr[i].w; j--) {
      dp[j] = max(dp[j], dp[j - arr[i].w] + arr[i].v);
    }
  }
  cout << *max_element(dp, dp + maxc) << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define maxc 20005
#define maxn 1005
using namespace std;
struct Pt {
  int w, s, v;
  void RD() { cin >> w >> s >> v; }
  bool operator<(const Pt &a) const { return w + s < a.w + a.s; }
};

int dp[maxc] = {};
Pt arr[maxn];
int n;

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    arr[i].RD();
  }
  sort(arr + 1, arr + n + 1);
  for (int i = 1; i <= n; i++) {
    int mx = arr[i].s + arr[i].w;
    for (int j = mx; j >= arr[i].w; j--) {
      dp[j] = max(dp[j], dp[j - arr[i].w] + arr[i].v);
    }
  }
  cout << *max_element(dp, dp + maxc) << '\n';
  return 0;
}
Problem p03183 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const int N = 1e3 + 5, MAX_S = 2e4 + 5;
int n;
long long dp[N][MAX_S], ans;

struct Block {
  int w, s, v;
  Block(int w = 0, int s = 0, int v = 0) : w(w), s(s), v(v) {}
  bool operator<(const Block &rhs) const {
    return w + s == rhs.w + rhs.s ? s < rhs.s : w + s < rhs.w + rhs.s;
  }
} block[N];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    int w, s, v;
    scanf("%d %d %d", &w, &s, &v);
    block[i] = Block(w, s, v);
  }
  sort(block + 1, block + n + 1);

  memset(dp, -1, sizeof dp);
  dp[0][0] = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < MAX_S; ++j) {
      dp[i][j] = max(dp[i][j], dp[i - 1][j]);
      if (dp[i - 1][j - block[i].w] != -1 && j - block[i].w >= 0 &&
          j - block[i].w <= block[i].s)
        dp[i][j] = max(dp[i][j], dp[i - 1][j - block[i].w] + block[i].v);

      ans = max(ans, dp[i][j]);
    }
  }

  printf("%lld", ans);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int N = 1e3 + 5, MAX_S = 2e4 + 5;
int n;
long long dp[N][MAX_S], ans;

struct Block {
  int w, s, v;
  Block(int w = 0, int s = 0, int v = 0) : w(w), s(s), v(v) {}
  bool operator<(const Block &rhs) const {
    return w + s == rhs.w + rhs.s ? s < rhs.s : w + s < rhs.w + rhs.s;
  }
} block[N];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    int w, s, v;
    scanf("%d %d %d", &w, &s, &v);
    block[i] = Block(w, s, v);
  }
  sort(block + 1, block + n + 1);

  memset(dp, -1, sizeof dp);
  dp[0][0] = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < MAX_S; ++j) {
      dp[i][j] = max(dp[i][j], dp[i - 1][j]);
      if (j - block[i].w >= 0 && j - block[i].w <= block[i].s &&
          dp[i - 1][j - block[i].w] != -1)
        dp[i][j] = max(dp[i][j], dp[i - 1][j - block[i].w] + block[i].v);

      ans = max(ans, dp[i][j]);
    }
  }

  printf("%lld", ans);

  return 0;
}
Problem p03183 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define i_7 (ll)(1E9 + 7)
#define i_5 (ll)(1E9 + 5)
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  else
    return c + i_7;
}
typedef pair<int, int> i_i;
typedef pair<ll, ll> l_l;
ll inf = (ll)1E12;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(ll *pos, ll val) {
  *pos = max(*pos, val);
} // Max(&dp[i][j],dp[i-1][j]);
void Min(ll *pos, ll val) { *pos = min(*pos, val); }
void Add(ll *pos, ll val) { *pos = mod(*pos + val); }
const long double EPS = 1E-8;
////////////////////////////////////////

struct tower {
  ll w, s, v;
};
bool comp(tower t1, tower t2) {
  if (t1.w + t1.s < t2.w + t2.s)
    return true;
  else
    return false;
}

int main() {
  ll n;
  cin >> n;
  tower t[n];
  rep(i, 0, n - 1) { cin >> t[i].w >> t[i].s >> t[i].v; }
  sort(t, t + n, comp);
  ll N = (ll)1E5 + 5;
  ll dp[n + 1][N];
  memset(dp, 0, sizeof(dp));
  rep(i, 0, n - 1) {
    rep(j, 0, N - 1) {
      dp[i + 1][j] = dp[i][j];
      if (0 <= j - t[i].w && j - t[i].w <= t[i].s)
        Max(&dp[i + 1][j], dp[i][j - t[i].w] + t[i].v);
    }
  }
  /*rep(i,0,n){
      rep(j,0,20){
          cout<<dp[i][j]<<" ";
      }cout<<endl;
  }*/
  ll ans = 0;
  rep(i, 0, N - 1) Max(&ans, dp[n][i]);
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define i_7 (ll)(1E9 + 7)
#define i_5 (ll)(1E9 + 5)
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  else
    return c + i_7;
}
typedef pair<int, int> i_i;
typedef pair<ll, ll> l_l;
ll inf = (ll)1E12;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(ll *pos, ll val) {
  *pos = max(*pos, val);
} // Max(&dp[i][j],dp[i-1][j]);
void Min(ll *pos, ll val) { *pos = min(*pos, val); }
void Add(ll *pos, ll val) { *pos = mod(*pos + val); }
const long double EPS = 1E-8;
////////////////////////////////////////

struct tower {
  ll w, s, v;
};
bool comp(tower t1, tower t2) {
  if (t1.w + t1.s < t2.w + t2.s)
    return true;
  else
    return false;
}

int main() {
  ll n;
  cin >> n;
  tower t[n];
  rep(i, 0, n - 1) { cin >> t[i].w >> t[i].s >> t[i].v; }
  sort(t, t + n, comp);
  ll N = 30005;
  ll dp[n + 1][N];
  memset(dp, 0, sizeof(dp));
  rep(i, 0, n - 1) {
    rep(j, 0, N - 1) {
      dp[i + 1][j] = dp[i][j];
      if (0 <= j - t[i].w && j - t[i].w <= t[i].s)
        Max(&dp[i + 1][j], dp[i][j - t[i].w] + t[i].v);
    }
  }
  /*rep(i,0,n){
      rep(j,0,20){
          cout<<dp[i][j]<<" ";
      }cout<<endl;
  }*/
  ll ans = 0;
  rep(i, 0, N - 1) Max(&ans, dp[n][i]);
  cout << ans << endl;
  return 0;
}
Problem p03183 (C++)

Buggy submission (status: Runtime Error):


#include <algorithm>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define eb emplace_back
#define el '\n'
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<pll> vpll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<bool> vb;

// const int mod=1000000007;
// const int mod=998244353;

struct node {
  ll s, w, v;
  bool inline operator<(const node &a) const {
    return this->s + this->w < a.s + a.w;
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t, n, m, k, q, x, a, b, c, y, l, r;
  cin >> n;
  vector<node> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i].w >> v[i].s >> v[i].v;
  }
  sort(all(v));
  m = 20000;
  vll dp(m, 0);
  for (int i = 0; i < n; i++) {
    for (int j = (int)v[i].s; j >= 0; j--) {
      assert(j + v[i].w < m);
      dp[j + v[i].w] = max(dp[j + v[i].w], dp[j] + v[i].v);
    }
  }
  cout << *max_element(all(dp)) << el;
  return 0;
}


Fixed submission:


#include <algorithm>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define eb emplace_back
#define el '\n'
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<pll> vpll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<bool> vb;

// const int mod=1000000007;
// const int mod=998244353;

struct node {
  ll s, w, v;
  bool inline operator<(const node &a) const {
    return this->s + this->w < a.s + a.w;
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t, n, m, k, q, x, a, b, c, y, l, r;
  cin >> n;
  vector<node> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i].w >> v[i].s >> v[i].v;
  }
  sort(all(v));
  m = 30000;
  vll dp(m, 0);
  for (int i = 0; i < n; i++) {
    for (int j = (int)v[i].s; j >= 0; j--) {
      assert(j + v[i].w < m);
      dp[j + v[i].w] = max(dp[j + v[i].w], dp[j] + v[i].v);
    }
  }
  cout << *max_element(all(dp)) << el;
  return 0;
}
Problem p03183 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll inf = 1e18L + 5;
struct block {
  int w, s;
  ll v;
};
int main() {
  int n;
  cin >> n;
  vector<block> vec(n);
  int max_w = 0;
  for (int i = 0; i < n; i++) {
    cin >> vec[i].w >> vec[i].s >> vec[i].v;
    max_w += vec[i].w;
  }
  sort(vec.begin(), vec.end(),
       [](block a, block b) { return a.w + a.s < b.w + b.s; });
  // dp[i] max value with total weight i so far
  vector<ll> dp(max_w + 5);
  // dp[n-1]=vec[n-1].v;
  for (int i = 0; i < n; i++) {
    for (int j = max_w - vec[i].w; j >= 0; j--) {
      if (j <= vec[i].s) {
        dp[j + vec[i].w] = max(dp[j + vec[i].w], dp[j] + vec[i].v);
      }
    }
  }
  ll ans = -1;
  for (int j = max_w; j >= 0; j--) {
    ans = max(ans, dp[j]);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll inf = 1e18L + 5;
struct block {
  int w, s;
  ll v;
};
int main() {
  int n;
  cin >> n;
  vector<block> vec(n);
  int max_w = 0;
  for (int i = 0; i < n; i++) {
    cin >> vec[i].w >> vec[i].s >> vec[i].v;
    max_w += vec[i].w;
  }
  sort(vec.begin(), vec.end(),
       [](block a, block b) { return a.w + a.s < b.w + b.s; });
  // dp[i] max value with total weight i so far
  vector<ll> dp(max_w + 5);
  // dp[n-1]=vec[n-1].v;
  for (int i = 0; i < n; i++) {
    for (int j = min(vec[i].s, max_w - vec[i].w); j >= 0; j--) {
      // if(j<=vec[i].s)

      dp[j + vec[i].w] = max(dp[j + vec[i].w], dp[j] + vec[i].v);
    }
  }
  ll ans = -1;
  for (int j = max_w; j >= 0; j--) {
    ans = max(ans, dp[j]);
  }
  cout << ans << endl;
}
Problem p03183 (C++)

Buggy submission (status: Runtime Error):
// Author : Rifayat Samee (Sanzee)
// Problem :
// Algorithm:

/*

*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define maxval 10005
struct Node {
  int w;
  int s;
  ll v;
  bool operator<(const Node &b) const { return b.w + b.s > w + s; }
};

int main() {

  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);
  int n;
  scanf("%d", &n);
  vector<Node> B(n);
  for (int i = 0; i < n; i++) {
    scanf("%d %d %d", &B[i].w, &B[i].s, &B[i].v);
  }
  sort(B.begin(), B.end());
  vector<ll> P(maxval + 1, 0);
  for (int i = 0; i < n; i++) {
    vector<ll> dp(maxval + 1);
    dp = P;
    for (int w = 0; w <= B[i].s; w++) {
      dp[w + B[i].w] = max(P[w + B[i].w], P[w] + B[i].v);
    }
    P = dp;
  }
  ll res = 0;
  for (int w = 0; w <= maxval; w++)
    res = max(res, P[w]);
  printf("%lld\n", res);
  return 0;
}


Fixed submission:
// Author : Rifayat Samee (Sanzee)
// Problem :
// Algorithm:

/*

*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define maxval 2 * 10005
struct Node {
  int w;
  int s;
  ll v;
  bool operator<(const Node &b) const { return b.w + b.s > w + s; }
};

int main() {

  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);
  int n;
  scanf("%d", &n);
  vector<Node> B(n);
  for (int i = 0; i < n; i++) {
    scanf("%d %d %d", &B[i].w, &B[i].s, &B[i].v);
  }
  sort(B.begin(), B.end());
  vector<ll> P(maxval + 1, 0);
  for (int i = 0; i < n; i++) {
    vector<ll> dp(maxval + 1);
    dp = P;
    for (int w = 0; w <= B[i].s; w++) {
      dp[w + B[i].w] = max(P[w + B[i].w], P[w] + B[i].v);
    }
    P = dp;
  }
  ll res = 0;
  for (int w = 0; w <= maxval; w++)
    res = max(res, P[w]);
  printf("%lld\n", res);
  return 0;
}
Problem p03183 (C++)

Buggy submission (status: Runtime Error):
// thuanqvbn03
#include <bits/stdc++.h>

using namespace std;

struct Block {
  int w, s, v;
  bool operator<(const Block data) const { return (w + s <= data.w + data.s); }
};

int n;
Block a[1005];
long long dp[1005][20005];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i].w >> a[i].s >> a[i].v;
  }
  sort(a + 1, a + n + 1);
  int m = a[n].s * 2;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      dp[i][j] = dp[i - 1][j];
      if (j >= a[i].w && j - a[i].w <= a[i].s) {
        dp[i][j] = max(dp[i][j], dp[i - 1][j - a[i].w] + a[i].v);
      }
    }
  }
  long long ans = 0;
  for (int i = 1; i <= m; i++) {
    ans = max(ans, dp[n][i]);
  }
  cout << ans;
  return 0;
}

Fixed submission:
// thuanqvbn03
#include <bits/stdc++.h>

using namespace std;

struct Block {
  int w, s, v;
  bool operator<(const Block data) const {
    return (w + s != data.w + data.s ? w + s < data.w + data.s : s < data.s);
  }
};

int n;
Block a[1005];
long long dp[1005][20005];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i].w >> a[i].s >> a[i].v;
  }
  sort(a + 1, a + n + 1);
  int m = a[n].s * 2;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      dp[i][j] = dp[i - 1][j];
      if (j >= a[i].w && j - a[i].w <= a[i].s) {
        dp[i][j] = max(dp[i][j], dp[i - 1][j - a[i].w] + a[i].v);
      }
    }
  }
  long long ans = 0;
  for (int i = 1; i <= m; i++) {
    ans = max(ans, dp[n][i]);
  }
  cout << ans;
  return 0;
}
Problem p03184 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int mod = 1e9 + 7;
struct node {
  ll x, y;
};
node w[3030];
ll n, m, t, f[3030][2], ny[200020], jc[200020];
bool cmp(node a1, node b1) {
  if (a1.x == b1.x)
    return a1.y < b1.y;
  return a1.x < b1.x;
}
ll ksm(ll d, ll z) {
  ll base = d, res = 1;
  while (z) {
    if (z & 1)
      res = (res * base) % mod;
    base = (base * base) % mod;
    z >>= 1;
  }
  return res;
}
ll c(ll n1, ll m1) { return (jc[n1] * ny[n1 - m1] % mod * ny[m1] % mod) % mod; }
int main() {
  jc[0] = ny[0] = 1;
  for (int i = 1; i <= 200010; i++)
    jc[i] = (jc[i - 1] * i) % mod;
  for (int i = 1; i <= 200010; i++)
    ny[i] = ksm(jc[i], mod - 2);
  cin >> n >> m >> t;
  for (int i = 0; i < t; i++)
    cin >> w[i].x >> w[i].y;
  w[t].x = w[t].y = 1;
  w[t + 1].x = n, w[t + 1].y = m;
  sort(w, w + t + 2, cmp);
  f[0][0] = 1;
  for (int i = 0; i <= t + 1; i++) {
    for (int j = 0; j < i; j++) {
      ll dx = w[i].x - w[j].x, dy = w[i].y - w[j].y;
      f[i][0] = (f[i][0] + (f[j][1] * c(dx + dy, dy)) % mod) % mod;
      f[i][1] = (f[i][1] + (f[j][0] * c(dx + dy, dy)) % mod) % mod;
    }
  }
  cout << (f[t + 1][1] + mod - f[t + 1][0]) % mod << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int mod = 1e9 + 7;
struct node {
  ll x, y;
};
node w[3030];
ll n, m, t, f[3030][2], ny[200020], jc[200020];
bool cmp(node a1, node b1) {
  if (a1.x == b1.x)
    return a1.y < b1.y;
  return a1.x < b1.x;
}
ll ksm(ll d, ll z) {
  ll base = d, res = 1;
  while (z) {
    if (z & 1)
      res = (res * base) % mod;
    base = (base * base) % mod;
    z >>= 1;
  }
  return res;
}
ll c(ll n1, ll m1) { return (jc[n1] * ny[n1 - m1] % mod * ny[m1] % mod) % mod; }
int main() {
  jc[0] = ny[0] = 1;
  for (int i = 1; i <= 200010; i++)
    jc[i] = (jc[i - 1] * i) % mod;
  for (int i = 1; i <= 200010; i++)
    ny[i] = ksm(jc[i], mod - 2);
  cin >> n >> m >> t;
  for (int i = 0; i < t; i++)
    cin >> w[i].x >> w[i].y;
  w[t].x = w[t].y = 1;
  w[t + 1].x = n, w[t + 1].y = m;
  sort(w, w + t + 2, cmp);
  f[0][0] = 1;
  for (int i = 0; i <= t + 1; i++) {
    for (int j = 0; j < i; j++) {
      if (w[i].y >= w[j].y) {
        ll dx = w[i].x - w[j].x, dy = w[i].y - w[j].y;
        f[i][0] = (f[i][0] + (f[j][1] * c(dx + dy, dy)) % mod) % mod;
        f[i][1] = (f[i][1] + (f[j][0] * c(dx + dy, dy)) % mod) % mod;
      }
    }
  }
  cout << (f[t + 1][1] + mod - f[t + 1][0]) % mod << endl;
  return 0;
}
Problem p03184 (C++)

Buggy submission (status: Runtime Error):
// In The Name of Allah
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define X first
#define Y second

const int N = 3e1 + 10;
const int M = 2e5 + 10;
const ll mod = 1e9 + 7;

pair<ll, ll> a[N];
ll dp[N], fact[M], inv[M];

ll Pow(ll a, ll b) {
  if (b == 0)
    return 1;
  ll res = Pow(a, b / 2);
  res = res * res % mod;
  if (b % 2)
    res = res * a % mod;
  return res;
}

ll C(ll n, ll m) {
  if (n < m || m < 0)
    return 0;
  return fact[n] * inv[m] % mod * inv[n - m] % mod;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n, h, w;
  cin >> h >> w >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i].first >> a[i].second;
  a[n] = {h, w};
  sort(a, a + n + 1);
  fact[0] = 1;
  for (ll i = 1; i < M; i++)
    fact[i] = fact[i - 1] * i % mod;
  for (ll i = 0; i < M; i++)
    inv[i] = Pow(fact[i], mod - 2);
  for (int i = 0; i <= n; i++) {
    dp[i] = C(a[i].X + a[i].Y - 2, a[i].X - 1);
    for (int j = 0; j < i; j++)
      dp[i] =
          (dp[i] -
           dp[j] * C(a[i].X + a[i].Y - a[j].X - a[j].Y, a[i].X - a[j].X) % mod +
           mod) %
          mod;
  }
  cout << dp[n];
  return 0;
}


Fixed submission:
// In The Name of Allah
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define X first
#define Y second

const int N = 3e3 + 10;
const int M = 2e5 + 10;
const ll mod = 1e9 + 7;

pair<ll, ll> a[N];
ll dp[N], fact[M], inv[M];

ll Pow(ll a, ll b) {
  if (b == 0)
    return 1;
  ll res = Pow(a, b / 2);
  res = res * res % mod;
  if (b % 2)
    res = res * a % mod;
  return res;
}

ll C(ll n, ll m) {
  if (n < m || m < 0)
    return 0;
  return fact[n] * inv[m] % mod * inv[n - m] % mod;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n, h, w;
  cin >> h >> w >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i].first >> a[i].second;
  a[n] = {h, w};
  sort(a, a + n + 1);
  fact[0] = 1;
  for (ll i = 1; i < M; i++)
    fact[i] = fact[i - 1] * i % mod;
  for (ll i = 0; i < M; i++)
    inv[i] = Pow(fact[i], mod - 2);
  for (int i = 0; i <= n; i++) {
    dp[i] = C(a[i].X + a[i].Y - 2, a[i].X - 1);
    for (int j = 0; j < i; j++)
      dp[i] =
          (dp[i] -
           dp[j] * C(a[i].X + a[i].Y - a[j].X - a[j].Y, a[i].X - a[j].X) % mod +
           mod) %
          mod;
  }
  cout << dp[n];
  return 0;
}
Problem p03184 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"

#define int long long

using namespace std;

const int mod = 1e9 + 7;

int qpow(int a, int power) {
  if (power == 0) {
    return 1;
  }
  int b = qpow(a, power / 2);
  if (power % 2 == 0) {
    return (b * b) % mod;
  } else {
    return (((b * b) % mod) * a) % mod;
  }
}

int inv(int a) { return qpow(a, mod - 2); }

vector<int> f(2e5 + 10), inv_f(2e5 + 10);

int c(int n, int k) { return f[n] * inv_f[k] % mod * inv_f[n - k] % mod; }

int32_t main() {
  ios_base::sync_with_stdio(0);
  f[0] = 1;
  for (int i = 1; i < f.size(); i++) {
    f[i] = i * f[i - 1] % mod;
  }
  for (int i = 0; i < f.size(); i++) {
    inv_f[i] = inv(f[i]);
  }
  int h, w, n;
  cin >> h >> w >> n;
  vector<pair<int, int>> obst(n);
  for (int i = 0; i < n; i++) {
    cin >> obst[i].first >> obst[i].second;
    obst[i].first--;
    obst[i].second--;
  }
  obst.push_back({h - 1, w - 1});
  n++;
  sort(obst.begin(), obst.end());
  vector<int> dp(n);
  for (int i = 0; i < n; i++) {
    int x = obst[i].first, y = obst[i].second;
    dp[i] = c(x + y, x);
    for (int j = 0; j < i; j++) {
      int x1 = obst[j].first, y1 = obst[j].second;
      dp[i] = (dp[i] - dp[j] * c(x - x1 + y - y1, x - x1) % mod + mod) % mod;
    }
    // cerr << dp[i] << endl;
  }
  cout << dp.back() << endl;
}


Fixed submission:
#include "bits/stdc++.h"

#define int long long

using namespace std;

const int mod = 1e9 + 7;

int qpow(int a, int power) {
  if (power == 0) {
    return 1;
  }
  int b = qpow(a, power / 2);
  if (power % 2 == 0) {
    return (b * b) % mod;
  } else {
    return (((b * b) % mod) * a) % mod;
  }
}

int inv(int a) { return qpow(a, mod - 2); }

vector<int> f(2e5 + 10), inv_f(2e5 + 10);

int c(int n, int k) { return f[n] * inv_f[k] % mod * inv_f[n - k] % mod; }

int32_t main() {
  ios_base::sync_with_stdio(0);
  f[0] = 1;
  for (int i = 1; i < f.size(); i++) {
    f[i] = i * f[i - 1] % mod;
  }
  for (int i = 0; i < f.size(); i++) {
    inv_f[i] = inv(f[i]);
  }
  int h, w, n;
  cin >> h >> w >> n;
  vector<pair<int, int>> obst(n);
  for (int i = 0; i < n; i++) {
    cin >> obst[i].first >> obst[i].second;
    obst[i].first--;
    obst[i].second--;
  }
  obst.push_back({h - 1, w - 1});
  n++;
  sort(obst.begin(), obst.end());
  vector<int> dp(n);
  for (int i = 0; i < n; i++) {
    int x = obst[i].first, y = obst[i].second;
    dp[i] = c(x + y, x);
    for (int j = 0; j < i; j++) {
      int x1 = obst[j].first, y1 = obst[j].second;
      if (x1 <= x && y1 <= y) {
        dp[i] = (dp[i] - dp[j] * c(x - x1 + y - y1, x - x1) % mod + mod) % mod;
      }
    }
    // cerr << dp[i] << endl;
  }
  cout << dp.back() << endl;
}
Problem p03184 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MAXN 100005
typedef long long ll;
const ll mod = 1e9 + 7;
ll fac[MAXN], ifac[MAXN];
ll fastpow(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      (res *= a) %= mod;
    (a *= a) %= mod;
    b >>= 1;
  }
  return res;
}
ll C(int m, int n) { return fac[m] * ifac[n] % mod * ifac[m - n] % mod; }
struct T {
  int x, y;
} a[MAXN];
bool cmp(T a, T b) { return a.x < b.x || (a.x == b.x && a.y < b.y); }
ll ans[MAXN];
int main() {
  fac[0] = ifac[0] = 1;
  for (int i = 1; i < MAXN; i++)
    fac[i] = fac[i - 1] * i % mod,
    ifac[i] = ifac[i - 1] * fastpow(i, mod - 2) % mod;

  int h, w, n;
  cin >> h >> w >> n;
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &a[i].x, &a[i].y);
  sort(a + 1, a + n + 1, cmp);
  ++n;
  a[n].x = h;
  a[n].y = w;
  for (int i = 1; i <= n; i++)
    ans[i] = C(a[i].x + a[i].y - 2, a[i].x - 1);
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++)
      if (a[i].x <= a[j].x && a[i].y <= a[j].y) {
        ll x = a[j].x - a[i].x + 1, y = a[j].y - a[i].y + 1;
        ans[j] = (ans[j] - C(x + y - 2, x - 1) * ans[i] % mod + mod) % mod;
      }
  cout << ans[n] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MAXN 200005
typedef long long ll;
const ll mod = 1e9 + 7;
ll fac[MAXN], ifac[MAXN];
ll fastpow(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      (res *= a) %= mod;
    (a *= a) %= mod;
    b >>= 1;
  }
  return res;
}
ll C(int m, int n) { return fac[m] * ifac[n] % mod * ifac[m - n] % mod; }
struct T {
  int x, y;
} a[MAXN];
bool cmp(T a, T b) { return a.x < b.x || (a.x == b.x && a.y < b.y); }
ll ans[MAXN];
int main() {
  fac[0] = ifac[0] = 1;
  for (int i = 1; i < MAXN; i++)
    fac[i] = fac[i - 1] * i % mod,
    ifac[i] = ifac[i - 1] * fastpow(i, mod - 2) % mod;

  int h, w, n;
  cin >> h >> w >> n;
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &a[i].x, &a[i].y);
  sort(a + 1, a + n + 1, cmp);
  ++n;
  a[n].x = h;
  a[n].y = w;
  for (int i = 1; i <= n; i++)
    ans[i] = C(a[i].x + a[i].y - 2, a[i].x - 1);
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++)
      if (a[i].x <= a[j].x && a[i].y <= a[j].y) {
        ll x = a[j].x - a[i].x + 1, y = a[j].y - a[i].y + 1;
        ans[j] = (ans[j] - C(x + y - 2, x - 1) * ans[i] % mod + mod) % mod;
      }
  cout << ans[n] << endl;
  return 0;
}
Problem p03184 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define F first
#define S second

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;

const ll MOD = 1e9 + 7;
ll H, W, N;
vector<ll> fact, finv;
vector<pll> walls;
vector<vector<ll>> dp;

ll modpow(ll a, ll b) {
  ll result = 1;
  for (; b; b /= 2) {
    if (b % 2)
      result = result * a % MOD;
    a = a * a % MOD;
  }
  return result;
}

ll choose(ll n, ll k) { return fact[n] * finv[k] % MOD * finv[n - k] % MOD; }

ll ways(int i, int j) {
  ll dr = walls[j].F - walls[i].F;
  ll dc = walls[j].S - walls[i].S;
  return dr < 0 || dc < 0 ? 0 : choose(dr + dc, dc);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> H >> W >> N;

  fact = {1};
  finv = {1};
  for (ll n = 1; n <= H + W - 2; n++) {
    fact.push_back(fact.back() * n % MOD);
    finv.push_back(modpow(fact.back(), MOD - 2));
  }

  walls.resize(N);
  for (int i = 0; i < N; i++)
    cin >> walls[i].F >> walls[i].S;
  walls.push_back({1, 1});
  walls.push_back({H, W});
  sort(walls.begin(), walls.end());

  int n = walls.size();
  dp.assign(n, vector<ll>(n));
  for (int i = 0; i < n - 1; i++)
    dp[i][i + 1] = ways(i, i + 1);
  for (int w = 3; w <= n; w++) {
    for (int l = 0, r = w - 1; r < n; l++, r++) {
      dp[l][r] = ways(l, r);
      for (int m = l + 1; m < r; m++) {
        dp[l][r] -= dp[l][m] * ways(m, r) % MOD;
        if (dp[l][r] < 0)
          dp[l][r] += MOD;
      }
    }
  }
  cout << dp[0][n - 1] << endl;
}


Fixed submission:
#include <bits/stdc++.h>

#define F first
#define S second

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;

const ll MOD = 1e9 + 7;
ll H, W, N;
vector<ll> fact, finv;
vector<pll> walls;
vector<vector<ll>> dp;

ll modpow(ll a, ll b) {
  ll result = 1;
  for (; b; b /= 2) {
    if (b % 2)
      result = result * a % MOD;
    a = a * a % MOD;
  }
  return result;
}

ll choose(ll n, ll k) { return fact[n] * finv[k] % MOD * finv[n - k] % MOD; }

ll ways(int i, int j) {
  ll dr = walls[j].F - walls[i].F;
  ll dc = walls[j].S - walls[i].S;
  return dr < 0 || dc < 0 ? 0 : choose(dr + dc, dc);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> H >> W >> N;

  fact = {1};
  finv = {1};
  for (ll n = 1; n <= H + W - 2; n++) {
    fact.push_back(fact.back() * n % MOD);
    finv.push_back(modpow(fact.back(), MOD - 2));
  }

  walls.resize(N);
  for (int i = 0; i < N; i++)
    cin >> walls[i].F >> walls[i].S;
  walls.push_back({1, 1});
  walls.push_back({H, W});
  sort(walls.begin(), walls.end());

  int n = walls.size();
  dp.assign(n, vector<ll>(n));
  for (int j = 1; j < n; j++) {
    dp[0][j] = ways(0, j);
    for (int i = 1; i < j; i++) {
      dp[0][j] -= dp[0][i] * ways(i, j) % MOD;
      if (dp[0][j] < 0)
        dp[0][j] += MOD;
    }
  }
  cout << dp[0][n - 1] << endl;
}
Problem p03184 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
const ll mod = 1000000007;
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
// head

PII p[2010];
ll dp[2010], f[201000];
int h, w, n;
ll go(ll x, ll y) {
  assert(x >= 0 && y >= 0);
  return f[x + y] * powmod(f[x] * f[y], mod - 2) % mod;
}
int main() {
  scanf("%d%d%d", &h, &w, &n);
  f[0] = 1;
  rep(i, 1, 200001) f[i] = f[i - 1] * i % mod;
  rep(i, 0, n) scanf("%d%d", &p[i].fi, &p[i].se);
  p[n] = mp(h, w);
  p[n + 1] = mp(1, 1);
  n += 2;
  sort(p, p + n);
  dp[0] = 1;
  rep(i, 1, n) {
    dp[i] = go(p[i].fi - 1, p[i].se - 1);
    rep(j, 1, i) if (p[j].fi <= p[i].fi && p[j].se <= p[i].se) dp[i] =
        (dp[i] - go(p[i].fi - p[j].fi, p[i].se - p[j].se) * dp[j]) % mod;
  }
  (dp[n - 1] += mod) %= mod;
  printf("%I64d\n", dp[n - 1]);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
const ll mod = 1000000007;
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
// head

PII p[3010];
ll dp[3010], f[201000];
int h, w, n;
ll go(ll x, ll y) {
  assert(x >= 0 && y >= 0);
  return f[x + y] * powmod(f[x] * f[y], mod - 2) % mod;
}
int main() {
  scanf("%d%d%d", &h, &w, &n);
  f[0] = 1;
  rep(i, 1, 200001) f[i] = f[i - 1] * i % mod;
  rep(i, 0, n) scanf("%d%d", &p[i].fi, &p[i].se);
  p[n] = mp(h, w);
  p[n + 1] = mp(1, 1);
  n += 2;
  sort(p, p + n);
  dp[0] = 1;
  rep(i, 1, n) {
    dp[i] = go(p[i].fi - 1, p[i].se - 1);
    rep(j, 1, i) if (p[j].fi <= p[i].fi && p[j].se <= p[i].se) dp[i] =
        (dp[i] - go(p[i].fi - p[j].fi, p[i].se - p[j].se) * dp[j]) % mod;
  }
  (dp[n - 1] += mod) %= mod;
  printf("%I64d\n", dp[n - 1]);
}
Problem p03184 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MOD 1000000007
using namespace std;
typedef struct point {
  int x, y;
  bool operator<(const struct point &t) const {
    return x == t.x ? y < t.y : x < t.x;
  }
} POINT, *PPOINT;
int w, h;
POINT arr[30002];
long long fac[200010], rev[200010], f[30002];
void init() {
  int i;
  fac[0] = fac[1] = rev[0] = rev[1] = 1;
  for (i = 2; i <= h + w - 2; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    rev[i] = (MOD - MOD / i) * rev[MOD % i] % MOD;
  }
  for (i = 2; i <= h + w - 2; i++)
    rev[i] = rev[i - 1] * rev[i] % MOD;
}
int main() {
  int i, j, n;
  scanf("%d %d %d", &h, &w, &n);
  for (i = 0; i < n; i++)
    scanf("%d %d", &arr[i].x, &arr[i].y);
  arr[n].x = h;
  arr[n].y = w;
  sort(arr, arr + n + 1);
  init();
  for (i = 0; i <= n; i++) {
    f[i] = fac[arr[i].x + arr[i].y - 2] * rev[arr[i].x - 1] % MOD *
           rev[arr[i].y - 1] % MOD;
    // printf("%d %d\n",i,f[i]);
    for (j = 0; j < i; j++) {
      f[i] = (f[i] - f[j] * fac[arr[i].x - arr[j].x - arr[j].y + arr[i].y] %
                         MOD * rev[arr[i].x - arr[j].x] % MOD *
                         rev[arr[i].y - arr[j].y] % MOD);
    }
    f[i] = (f[i] + MOD) % MOD;
  }
  printf("%lld\n", (f[n] + MOD) % MOD);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define MOD 1000000007
using namespace std;
typedef struct point {
  int x, y;
  bool operator<(const struct point &t) const {
    return x == t.x ? y < t.y : x < t.x;
  }
} POINT, *PPOINT;
int w, h;
POINT arr[200002];
long long fac[200010], rev[200010], f[200002];
void init() {
  int i;
  fac[0] = fac[1] = rev[0] = rev[1] = 1;
  for (i = 2; i <= h + w - 2; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    rev[i] = (MOD - MOD / i) * rev[MOD % i] % MOD;
  }
  for (i = 2; i <= h + w - 2; i++)
    rev[i] = rev[i - 1] * rev[i] % MOD;
}
int main() {
  int i, j, n;
  scanf("%d %d %d", &h, &w, &n);
  for (i = 0; i < n; i++)
    scanf("%d %d", &arr[i].x, &arr[i].y);
  arr[n].x = h;
  arr[n].y = w;
  sort(arr, arr + n + 1);
  init();
  for (i = 0; i <= n; i++) {
    f[i] = fac[arr[i].x + arr[i].y - 2] * rev[arr[i].x - 1] % MOD *
           rev[arr[i].y - 1] % MOD;
    // printf("%d %d\n",i,f[i]);
    for (j = 0; j < i; j++) {
      f[i] = (f[i] - f[j] * fac[arr[i].x - arr[j].x - arr[j].y + arr[i].y] %
                         MOD * rev[arr[i].x - arr[j].x] % MOD *
                         rev[arr[i].y - arr[j].y] % MOD);
    }
    f[i] = (f[i] + MOD) % MOD;
  }
  printf("%lld\n", (f[n] + MOD) % MOD);
  return 0;
}