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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const static ll MOD = 1e9 + 7;
const static ll INF = 1e14;

ll N;
vector<vector<ll>> V(17, vector<ll>(17));
vector<ll> DP;

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> V[i][j];
    }
  }
  DP.resize(1 << N);
  for (int i = 0; i < (1 << N); i++) {
    bitset<17> B = i;
    ll score = 0;
    for (int i = 0; i < N; i++) {
      for (int j = i; j < N; j++) {
        if (B[i] == 1 && B[j] == 1)
          score += V[i][j];
      }
    }
    DP[i] = score;
  }
  for (int i1 = 0; i1 < (1 << N); i1++) {
    bitset<17> B = i1;
    ll cnt = B.count();
    for (int t = i1; t >= 0; t = (t - 1) & i1) {
      // for(int t = (i1-1)&i1; t > 0; t = (t-1)&i1){
      DP[i1] = max(DP[i1], DP[t] + DP[i1 - t]);
    }
  }
  cout << DP[(1 << N) - 1] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const static ll MOD = 1e9 + 7;
const static ll INF = 1e14;

ll N;
vector<vector<ll>> V(17, vector<ll>(17));
vector<ll> DP;

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> V[i][j];
    }
  }
  DP.resize(1 << N);
  for (int i = 0; i < (1 << N); i++) {
    bitset<17> B = i;
    ll score = 0;
    for (int i = 0; i < N; i++) {
      for (int j = i; j < N; j++) {
        if (B[i] == 1 && B[j] == 1)
          score += V[i][j];
      }
    }
    DP[i] = score;
  }
  for (int i1 = 0; i1 < (1 << N); i1++) {
    bitset<17> B = i1;
    ll cnt = B.count();
    for (int t = i1; t > 0; t = (t - 1) & i1) {
      // for(int t = (i1-1)&i1; t > 0; t = (t-1)&i1){
      DP[i1] = max(DP[i1], DP[t] + DP[i1 - t]);
    }
  }
  cout << DP[(1 << N) - 1] << endl;
}
Problem p03180 (C++)

Buggy submission (status: Time Limit Exceeded):
#define taskname ""

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
#define long long long

const int N = 16 + 10;
const int MOD = 1e9 + 7;

int n, a[N][N], mark[1 << N];
long dp[1 << N];

inline int bit(int i, int mask) { return (mask >> i) & 1; }
void cre_dp() {
  for (int mask = 1; mask < (1 << n); ++mask) {
    for (int i = 0; i < n; ++i)
      if (bit(i, mask))
        for (int j = i + 1; j < n; ++j)
          if (bit(j, mask))
            dp[mask] += a[i][j];
  }
}

long get(int mask) {
  if (mark[mask])
    return dp[mask];

  vector<int> pos;
  for (int i = 0; i < n; ++i)
    if (bit(i, mask))
      pos.push_back(i);
  int num = pos.size();

  for (int i = 1; i < (1 << num) - 1; ++i) {
    int cnt = 0;
    for (int j = 0; j < num; ++j)
      if (bit(j, i))
        cnt += 1 << pos[j];
    dp[mask] = max(dp[mask], get(cnt) + get(mask ^ cnt));
  }

  mark[mask] = 1;
  return dp[mask];
}

int main() {
  //    freopen(taskname".INP", "r", stdin);
  //    freopen(taskname".OUT", "w", stdout);
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);

  cin >> n;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      cin >> a[i][j];
  cre_dp();
  cout << get((1 << n) - 1);

  return 0;
}


Fixed submission:
#define taskname ""

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
#define long long long

const int N = 16 + 10;
const int MOD = 1e9 + 7;

int n, a[N][N], mark[1 << N];
long dp[1 << N];

inline int bit(int i, int mask) { return (mask >> i) & 1; }
void cre_dp() {
  for (int mask = 1; mask < (1 << n); ++mask) {
    for (int i = 0; i < n; ++i)
      if (bit(i, mask))
        for (int j = i + 1; j < n; ++j)
          if (bit(j, mask))
            dp[mask] += a[i][j];
  }
}

long get(int mask) {
  if (mark[mask])
    return dp[mask];
  for (int t = (mask - 1) & mask; t; t = (t - 1) & mask)
    dp[mask] = max(dp[mask], get(t) + get(mask ^ t));

  mark[mask] = 1;
  return dp[mask];
}

int main() {
  //    freopen(taskname".INP", "r", stdin);
  //    freopen(taskname".OUT", "w", stdout);
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);

  cin >> n;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      cin >> a[i][j];
  cre_dp();
  cout << get((1 << n) - 1);

  return 0;
}
Problem p03180 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define int long long
#define rep(i, n) for (i = 0; i < n; i++)
using namespace std;

void chmax(int &a, int b) { a = max(a, b); }

int INF = 1e+16;
int n;
int a[16][16];
int score[1 << 16];
int dp[1 << 16]; // dp[used] = max-value

signed main() {
  int i, j, k;

  cin >> n;
  rep(i, n) rep(j, n) cin >> a[i][j];

  rep(i, (1 << n)) {
    rep(j, n) {
      if ((i >> j) % 2 == 0)
        continue;
      for (k = j + 1; k < n; k++) {
        if ((i >> k) % 2 == 0)
          continue;
        score[i] += a[j][k];
      }
    }
  }

  rep(i, (1 << n)) dp[i] = -INF;
  dp[0] = 0;

  rep(i, (1 << n) - 1) {
    int uku[16], cnt = 0;
    rep(j, n) {
      if ((i >> j) % 2 == 0) {
        uku[cnt++] = j;
      }
    }

    rep(j, (1 << cnt)) {
      if (j == 0)
        continue;

      int beet = 0;
      rep(k, cnt) {
        if ((j >> k) % 2 == 1) {
          beet += (1 << uku[k]);
        }
      }
      chmax(dp[i + beet], dp[i] + score[beet]);
    }
  }
  cout << dp[(1 << n) - 1] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define int long long
#define rep(i, n) for (i = 0; i < n; i++)
using namespace std;

void chmax(int &a, int b) { a = max(a, b); }

int INF = 1e+16;
int n;
int a[16][16];
int score[1 << 16];
int dp[1 << 16]; // dp[used] = max-value

signed main() {
  int i, j, k;

  cin >> n;
  rep(i, n) rep(j, n) cin >> a[i][j];

  rep(i, (1 << n)) {
    rep(j, n) {
      if ((i >> j) % 2 == 0)
        continue;
      for (k = j + 1; k < n; k++) {
        if ((i >> k) % 2 == 0)
          continue;
        score[i] += a[j][k];
      }
    }
  }

  rep(i, (1 << n)) dp[i] = -INF;
  dp[0] = 0;

  rep(i, (1 << n) - 1) {
    int uku[16], cnt = 0;
    rep(j, n) {
      if ((i >> j) % 2 == 0) {
        uku[cnt++] = j;
      }
    }

    rep(j, (1 << cnt)) {
      if (j == 0)
        continue;
      if (j % 2 == 0)
        continue;

      int beet = 0;
      rep(k, cnt) {
        if ((j >> k) % 2 == 1) {
          beet += (1 << uku[k]);
        }
      }
      chmax(dp[i + beet], dp[i] + score[beet]);
    }
  }
  cout << dp[(1 << n) - 1] << endl;
  return 0;
}
Problem p03180 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long solve() {
  int n;
  cin >> n;
  long long A[n][n];
  long long dp[(1 << n)];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> A[i][j];
  for (int i = 1; i < 1 << 16; i++) {
    dp[i] = 0;
    for (int j = 0; j < n; j++) {
      if (i & (1 << j)) {
        for (int k = j + 1; k < n; k++) {
          if (i & (1 << k))
            dp[i] += A[j][k];
        }
      }
    }
  }
  for (int mask = 1; mask < 1 << n; mask++) {
    for (int ss = mask; ss > 0; ss = (ss - 1) & mask) {
      dp[mask] = max(dp[mask], dp[ss] + dp[mask ^ ss]);
    }
  }
  return dp[(1 << n) - 1];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << solve() << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long solve() {
  int n;
  cin >> n;
  long long A[n][n];
  long long dp[(1 << n)];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> A[i][j];
  dp[0] = 0;
  for (int i = 1; i < 1 << n; i++) {
    dp[i] = 0;
    for (int j = 0; j < n; j++) {
      if (i & (1 << j)) {
        for (int k = j + 1; k < n; k++) {
          if (i & (1 << k))
            dp[i] += A[j][k];
        }
      }
    }
  }
  for (int mask = 1; mask < 1 << n; mask++) {
    for (int ss = mask; ss > 0; ss = (ss - 1) & mask) {
      dp[mask] = max(dp[mask], dp[ss] + dp[mask ^ ss]);
    }
  }
  return dp[(1 << n) - 1];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << solve() << "\n";
}
Problem p03181 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n, m;
int a, b;
vector<int> v[100005];
int dp[100005];
int out[100005];
void dfs(int node, int prev) {
  dp[node] = 1;
  for (auto it : v[node]) {
    if (it != prev) {
      dfs(it, node);
      dp[node] = 1LL * dp[node] * (dp[it] + 1) % m;
    }
  }
}
void dfs2(int node, int prev) {
  vector<int> pref(n), suf(n);
  pref.resize(n);
  suf.resize(n);
  out[node] = dp[node];
  int last = 1;
  for (int i = 0; v[node].size() > i; i++) {
    pref[i] = 1LL * last * (dp[v[node][i]] + 1) % m;
    last = pref[i];
  }
  last = 1;
  for (int i = v[node].size() - 1; i >= 0; i--) {
    suf[i] = 1LL * last * (dp[v[node][i]] + 1) % m;
    last = suf[i];
  }
  for (int i = 0; v[node].size() > i; i++) {
    if (v[node][i] == prev)
      continue;
    int ile = dp[v[node][i]];
    int it = v[node][i];
    int val = 1;
    if (i > 0)
      val = 1LL * val * pref[i - 1] % m;
    if (i < v[node].size() - 1)
      val = 1LL * val * suf[i + 1] % m;
    int co = dp[node];
    dp[node] = val;
    dp[it] = (1LL * dp[it] * (val + 1)) % m;
    dfs2(it, node);
    dp[it] = ile;
    dp[node] = co;
  }
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; n - 1 > i; i++) {
    scanf("%d%d", &a, &b);
    v[a].push_back(b);
    v[b].push_back(a);
  }
  dfs(1, -1);
  dfs2(1, -1);
  for (int i = 1; n >= i; i++)
    printf("%d\n", out[i]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n, m;
int a, b;
vector<int> v[100005];
int dp[100005];
int out[100005];
void dfs(int node, int prev) {
  dp[node] = 1;
  for (auto it : v[node]) {
    if (it != prev) {
      dfs(it, node);
      dp[node] = 1LL * dp[node] * (dp[it] + 1) % m;
    }
  }
}
void dfs2(int node, int prev) {
  int f = v[node].size();
  vector<int> pref(f), suf(f);
  pref.resize(f);
  suf.resize(f);
  out[node] = dp[node];
  int last = 1;
  for (int i = 0; v[node].size() > i; i++) {
    pref[i] = 1LL * last * (dp[v[node][i]] + 1) % m;
    last = pref[i];
  }
  last = 1;
  for (int i = v[node].size() - 1; i >= 0; i--) {
    suf[i] = 1LL * last * (dp[v[node][i]] + 1) % m;
    last = suf[i];
  }
  for (int i = 0; v[node].size() > i; i++) {
    if (v[node][i] == prev)
      continue;
    int ile = dp[v[node][i]];
    int it = v[node][i];
    int val = 1;
    if (i > 0)
      val = 1LL * val * pref[i - 1] % m;
    if (i < v[node].size() - 1)
      val = 1LL * val * suf[i + 1] % m;
    int co = dp[node];
    dp[node] = val;
    dp[it] = (1LL * dp[it] * (val + 1)) % m;
    dfs2(it, node);
    dp[it] = ile;
    dp[node] = co;
  }
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; n - 1 > i; i++) {
    scanf("%d%d", &a, &b);
    v[a].push_back(b);
    v[b].push_back(a);
  }
  dfs(1, -1);
  dfs2(1, -1);
  for (int i = 1; n >= i; i++)
    printf("%d\n", out[i]);
  return 0;
}
Problem p03181 (C++)

Buggy submission (status: Runtime Error):
/*********************
 *  Author: xuziyuan  *
 *********************/

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define LL long long
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair

using namespace std;

LL n, MOD, x, y, dp[100010], ans[100010], pro[200];
vector<LL> g[100010];

LL dfs(LL pos, LL no) {
  dp[pos] = 1;
  rep(i, g[pos].size()) if (g[pos][i] != no) dp[pos] =
      dp[pos] * (dfs(g[pos][i], pos) + 1) % MOD;
  // cout<<pos<<' '<<dp[pos]<<'p'<<endl;
  return dp[pos];
}

void dfs2(LL pos, LL no, LL val) {
  // cout<<pos<<' '<<val<<endl;
  ans[pos] = dp[pos] * (val + 1) % MOD;
  LL t = floor(sqrt((LL)g[pos].size()));
  // cout<<t<<endl;
  LL pro[130];
  rep(i, 125) pro[i] = 1;
  rep(i, g[pos].size()) if (g[pos][i] != no) pro[i / t] =
      pro[i / t] * (dp[g[pos][i]] + 1) % MOD;
  LL res;
  rep(i, g[pos].size()) {
    if (g[pos][i] == no)
      continue;
    res = val + 1;
    rep(j, (g[pos].size() - 1) / t + 1) if (j != i / t) res =
        res * pro[j] % MOD;
    for (int j = i / t * t; j < min((LL)g[pos].size(), (i / t + 1) * t); j++)
      if (g[pos][j] != no && j != i)
        res = res * (dp[g[pos][j]] + 1) % MOD;
    dfs2(g[pos][i], pos, res);
  }
}

int main() {
  cin >> n >> MOD;
  rep(i, n - 1) {
    scanf("%lld%lld", &x, &y);
    g[x].pb(y);
    g[y].pb(x);
  }
  dfs(1, 0);
  dfs2(1, 0, 0);
  repn(i, n) printf("%lld\n", ans[i]);
  return 0;
}

Fixed submission:
/*********************
 *  Author: xuziyuan  *
 *********************/

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define LL long long
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair

using namespace std;

LL n, MOD, x, y, dp[100010], ans[100010], pro[200];
vector<LL> g[100010];

LL dfs(LL pos, LL no) {
  dp[pos] = 1;
  rep(i, g[pos].size()) if (g[pos][i] != no) dp[pos] =
      dp[pos] * (dfs(g[pos][i], pos) + 1) % MOD;
  // cout<<pos<<' '<<dp[pos]<<'p'<<endl;
  return dp[pos];
}

void dfs2(LL pos, LL no, LL val) {
  // cout<<pos<<' '<<val<<endl;
  ans[pos] = dp[pos] * (val + 1) % MOD;
  LL t = floor(sqrt((LL)g[pos].size()));
  // cout<<t<<endl;
  LL pro[400];
  rep(i, 395) pro[i] = 1;
  rep(i, g[pos].size()) if (g[pos][i] != no) pro[i / t] =
      pro[i / t] * (dp[g[pos][i]] + 1) % MOD;
  LL res;
  rep(i, g[pos].size()) {
    if (g[pos][i] == no)
      continue;
    res = val + 1;
    rep(j, (g[pos].size() - 1) / t + 1) if (j != i / t) res =
        res * pro[j] % MOD;
    for (int j = i / t * t; j < min((LL)g[pos].size(), (i / t + 1) * t); j++)
      if (g[pos][j] != no && j != i)
        res = res * (dp[g[pos][j]] + 1) % MOD;
    dfs2(g[pos][i], pos, res);
  }
}

int main() {
  cin >> n >> MOD;
  rep(i, n - 1) {
    scanf("%lld%lld", &x, &y);
    g[x].pb(y);
    g[y].pb(x);
  }
  dfs(1, 0);
  dfs2(1, 0, 0);
  repn(i, n) printf("%lld\n", ans[i]);
  return 0;
}
Problem p03181 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n, m;
int cnt = 0, head[100005], to[100005], nxt[100005];
ll f[100005], ans[100005];
ll p[100005], a[100005], b[100005], dp[100005];
void add(int x, int y) {
  to[++cnt] = y;
  nxt[cnt] = head[x];
  head[x] = cnt;
}
void dfs(int x, int par = -1) {
  int cnter = 0;
  f[x]++;
  for (int i = head[x]; i; i = nxt[i]) {
    if (to[i] != par) {
      dfs(to[i], x);
      f[x] = f[x] * f[to[i]] % m;
    }
  }
  for (int i = head[x]; i; i = nxt[i]) {
    if (to[i] != par) {
      p[++cnter] = to[i];
    }
  }
  f[x]++;
  f[x] %= m;
  a[0] = b[cnter + 1] = 1;
  for (int i = 1; i <= cnter; ++i) {
    a[i] = a[i - 1] * f[p[i]] % m;
  }
  for (int i = cnter; i > 0; --i) {
    b[i] = b[i + 1] * f[p[i]] % m;
  }
  for (int i = 1; i <= cnter; ++i) {
    dp[p[i]] = a[i - 1] * b[i + 1] % m;
  }
}
void DFS(int x, int par = -1) {
  if (x == 1) {
    ans[x] = 1;
  } else {
    ans[x] = (dp[x] * ans[par] + 1) % m;
  }
  for (int i = head[x]; i; i = nxt[i]) {
    if (to[i] != par) {
      DFS(to[i], x);
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i < n; ++i) {
    int a, b;
    cin >> a >> b;
    add(a, b), add(b, a);
  }
  dfs(1);
  DFS(1);
  for (int i = 1; i <= n; ++i) {
    cout << ans[i] * (m + f[i] - 1) % m << '\n';
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n, m;
int cnt = 0, head[200005], to[200005], nxt[200005];
ll f[200005], ans[200005];
ll p[200005], a[200005], b[200005], dp[200005];
void add(int x, int y) {
  to[++cnt] = y;
  nxt[cnt] = head[x];
  head[x] = cnt;
}
void dfs(int x, int par = -1) {
  int cnter = 0;
  f[x]++;
  for (int i = head[x]; i; i = nxt[i]) {
    if (to[i] != par) {
      dfs(to[i], x);
      f[x] = f[x] * f[to[i]] % m;
    }
  }
  for (int i = head[x]; i; i = nxt[i]) {
    if (to[i] != par) {
      p[++cnter] = to[i];
    }
  }
  f[x]++;
  f[x] %= m;
  a[0] = b[cnter + 1] = 1;
  for (int i = 1; i <= cnter; ++i) {
    a[i] = a[i - 1] * f[p[i]] % m;
  }
  for (int i = cnter; i > 0; --i) {
    b[i] = b[i + 1] * f[p[i]] % m;
  }
  for (int i = 1; i <= cnter; ++i) {
    dp[p[i]] = a[i - 1] * b[i + 1] % m;
  }
}
void DFS(int x, int par = -1) {
  if (x == 1) {
    ans[x] = 1;
  } else {
    ans[x] = (dp[x] * ans[par] + 1) % m;
  }
  for (int i = head[x]; i; i = nxt[i]) {
    if (to[i] != par) {
      DFS(to[i], x);
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i < n; ++i) {
    int a, b;
    cin >> a >> b;
    add(a, b), add(b, a);
  }
  dfs(1);
  DFS(1);
  for (int i = 1; i <= n; ++i) {
    cout << ans[i] * (m + f[i] - 1) % m << '\n';
  }
  return 0;
}
Problem p03181 (C++)

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> G(n), x(n);
  vector<int> dp(n, 1), par(n);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  function<void(int, int)> dfs1 = [&](int u, int p) {
    for (int v : G[u]) {
      if (v == p) {
        par[u] = x[u].size();
        x[u].push_back(-1);
      } else {
        dfs1(v, u);
        x[u].push_back((dp[v] + 1) % m);
        dp[u] = 1LL * dp[u] * x[u].back() % m;
      }
    }
  };
  dfs1(0, -1);
  function<void(int, int, int)> dfs2 = [&](int u, int p, int y) {
    if (p != -1) {
      x[u][par[u]] = y;
      dp[u] = 1LL * dp[u] * y % m;
    }
    vector<int> s = x[u];
    for (int i = 0; i + 1 < s.size(); i++)
      s[i + 1] = 1LL * s[i + 1] * s[i] % m;
    for (int i = s.size() - 1; i; i--)
      x[u][i - 1] = 1LL * x[u][i - 1] * x[u][i] % m;
    for (int i = 0; i < s.size(); i++) {
      if (G[u][i] != p) {
        int a = (i ? s[i - 1] : 1);
        int b = (i + 1 < s.size() ? x[u][i + 1] : 1);
        dfs2(G[u][i], u, (1LL * a * b + 1) % m);
      }
    }
  };
  dfs2(0, -1, 1);
  for (int i = 0; i < n; i++)
    cout << dp[i] << endl;
}

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> G(n), x(n);
  vector<int> dp(n, 1), par(n);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  function<void(int, int)> dfs1 = [&](int u, int p) {
    for (int v : G[u]) {
      if (v == p) {
        par[u] = x[u].size();
        x[u].push_back(-1);
      } else {
        dfs1(v, u);
        x[u].push_back((dp[v] + 1) % m);
        dp[u] = 1LL * dp[u] * x[u].back() % m;
      }
    }
  };
  dfs1(0, -1);
  function<void(int, int, int)> dfs2 = [&](int u, int p, int y) {
    if (p != -1) {
      x[u][par[u]] = y;
      dp[u] = 1LL * dp[u] * y % m;
    }
    vector<int> s = x[u];
    for (int i = 0; i + 1 < s.size(); i++)
      s[i + 1] = 1LL * s[i + 1] * s[i] % m;
    for (int i = s.size() - 1; i > 0; i--)
      x[u][i - 1] = 1LL * x[u][i - 1] * x[u][i] % m;
    for (int i = 0; i < s.size(); i++) {
      if (G[u][i] != p) {
        int a = (i ? s[i - 1] : 1);
        int b = (i + 1 < s.size() ? x[u][i + 1] : 1);
        dfs2(G[u][i], u, (1LL * a * b + 1) % m);
      }
    }
  };
  dfs2(0, -1, 1);
  for (int i = 0; i < n; i++)
    cout << dp[i] << endl;
}
Problem p03182 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mx = 2e5;
vector<ll> lazy(2 * mx + 100);
vector<ll> segtree(2 * mx + 100);

void push(int k, int l, int r) {
  if (l != r) {
    lazy[2 * k] += lazy[k];
    lazy[2 * k + 1] += lazy[k];
  }
  segtree[k] += lazy[k];
  lazy[k] = 0;
}

void pull(int k) { segtree[k] = max(segtree[2 * k], segtree[2 * k + 1]); }

void update(int a, int b, int k, int x, int y, ll inc) {
  push(k, x, y);
  if (x > b || y < a) {
    return;
  }
  if (a <= x && y <= b) {
    lazy[k] = inc;
    push(k, x, y);
    return;
  }
  int m = (x + y) / 2;
  update(a, b, 2 * k, x, m, inc);
  update(a, b, 2 * k + 1, m + 1, y, inc);
  pull(k);
}

ll get(int a, int b, int k, int x, int y) {
  push(k, x, y);
  if (x > b || y < a) {
    return LLONG_MIN;
  }
  if (a <= x && y <= b) {
    return segtree[k];
  }
  int m = (x + y) / 2;
  return max(get(a, b, 2 * k, x, m), get(a, b, 2 * k + 1, m + 1, y));
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<ll> inc[n + 1];
  vector<pair<ll, ll>> rem[n + 1];
  for (int q = 0; q < m; q++) {
    ll l, r, a;
    cin >> l >> r >> a;
    inc[l].push_back(a);
    rem[r].push_back({l, a});
  }
  ll ans = 0;
  for (int q = 1; q <= n; q++) {
    for (ll w : inc[q]) {
      update(0, q - 1, 1, 0, n, w);
    }
    ll best = get(0, q - 1, 1, 0, n);
    update(q, q, 1, 0, n, best);
    for (pair<ll, ll> w : rem[q]) {
      update(0, w.first - 1, 1, 0, n, -1 * w.second);
    }
    ans = max(ans, best);
  }
  cout << ans << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mx = 2e5;
vector<ll> lazy(3 * mx + 5);
vector<ll> segtree(3 * mx + 5);

void push(int k, int l, int r) {
  if (l != r) {
    lazy[2 * k] += lazy[k];
    lazy[2 * k + 1] += lazy[k];
  }
  segtree[k] += lazy[k];
  lazy[k] = 0;
}

void pull(int k) { segtree[k] = max(segtree[2 * k], segtree[2 * k + 1]); }

void update(int a, int b, int k, int x, int y, ll inc) {
  push(k, x, y);
  if (x > b || y < a) {
    return;
  }
  if (a <= x && y <= b) {
    lazy[k] = inc;
    push(k, x, y);
    return;
  }
  int m = (x + y) / 2;
  update(a, b, 2 * k, x, m, inc);
  update(a, b, 2 * k + 1, m + 1, y, inc);
  pull(k);
}

ll get(int a, int b, int k, int x, int y) {
  push(k, x, y);
  if (x > b || y < a) {
    return LLONG_MIN;
  }
  if (a <= x && y <= b) {
    return segtree[k];
  }
  int m = (x + y) / 2;
  return max(get(a, b, 2 * k, x, m), get(a, b, 2 * k + 1, m + 1, y));
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<ll> inc[n + 1];
  vector<pair<ll, ll>> rem[n + 1];
  for (int q = 0; q < m; q++) {
    ll l, r, a;
    cin >> l >> r >> a;
    inc[l].push_back(a);
    rem[r].push_back({l, a});
  }
  ll ans = 0;
  for (int q = 1; q <= n; q++) {
    for (ll w : inc[q]) {
      update(0, q - 1, 1, 0, n, w);
    }
    ll best = get(0, q - 1, 1, 0, n);
    update(q, q, 1, 0, n, best);
    for (pair<ll, ll> w : rem[q]) {
      update(0, w.first - 1, 1, 0, n, -1 * w.second);
    }
    ans = max(ans, best);
  }
  cout << ans << "\n";
  return 0;
}
Problem p03182 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mx = 2e5;
vector<ll> lazy(2 * mx + 5);
vector<ll> segtree(2 * mx + 5);

void push(int k, int l, int r) {
  if (l != r) {
    lazy[2 * k] += lazy[k];
    lazy[2 * k + 1] += lazy[k];
  }
  segtree[k] += lazy[k];
  lazy[k] = 0;
}

void pull(int k) { segtree[k] = max(segtree[2 * k], segtree[2 * k + 1]); }

void update(int a, int b, int k, int x, int y, ll inc) {
  push(k, x, y);
  if (x > b || y < a) {
    return;
  }
  if (a <= x && y <= b) {
    lazy[k] = inc;
    push(k, x, y);
    return;
  }
  int m = (x + y) / 2;
  update(a, b, 2 * k, x, m, inc);
  update(a, b, 2 * k + 1, m + 1, y, inc);
  pull(k);
}

ll get(int a, int b, int k, int x, int y) {
  push(k, x, y);
  if (x > b || y < a) {
    return LLONG_MIN;
  }
  if (a <= x && y <= b) {
    return segtree[k];
  }
  int m = (x + y) / 2;
  return max(get(a, b, 2 * k, x, m), get(a, b, 2 * k + 1, m + 1, y));
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<ll> inc[n + 1];
  vector<pair<ll, ll>> rem[n + 1];
  for (int q = 0; q < m; q++) {
    ll l, r, a;
    cin >> l >> r >> a;
    inc[l].push_back(a);
    rem[r].push_back({l, a});
  }
  ll ans = 0;
  for (int q = 1; q <= n; q++) {
    for (ll w : inc[q]) {
      update(0, q - 1, 1, 0, n, w);
    }
    ll best = get(0, q - 1, 1, 0, n);
    update(q, q, 1, 0, n, best);
    for (pair<ll, ll> w : rem[q]) {
      update(0, w.first - 1, 1, 0, n, -1 * w.second);
    }
    ans = max(ans, best);
  }
  cout << ans << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mx = 2e5;
vector<ll> lazy(4 * mx + 5);
vector<ll> segtree(4 * mx + 5);

void push(int k, int l, int r) {
  if (l != r) {
    lazy[2 * k] += lazy[k];
    lazy[2 * k + 1] += lazy[k];
  }
  segtree[k] += lazy[k];
  lazy[k] = 0;
}

void pull(int k) { segtree[k] = max(segtree[2 * k], segtree[2 * k + 1]); }

void update(int a, int b, int k, int x, int y, ll inc) {
  push(k, x, y);
  if (x > b || y < a) {
    return;
  }
  if (a <= x && y <= b) {
    lazy[k] = inc;
    push(k, x, y);
    return;
  }
  int m = (x + y) / 2;
  update(a, b, 2 * k, x, m, inc);
  update(a, b, 2 * k + 1, m + 1, y, inc);
  pull(k);
}

ll get(int a, int b, int k, int x, int y) {
  push(k, x, y);
  if (x > b || y < a) {
    return LLONG_MIN;
  }
  if (a <= x && y <= b) {
    return segtree[k];
  }
  int m = (x + y) / 2;
  return max(get(a, b, 2 * k, x, m), get(a, b, 2 * k + 1, m + 1, y));
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<ll> inc[n + 1];
  vector<pair<ll, ll>> rem[n + 1];
  for (int q = 0; q < m; q++) {
    ll l, r, a;
    cin >> l >> r >> a;
    inc[l].push_back(a);
    rem[r].push_back({l, a});
  }
  ll ans = 0;
  for (int q = 1; q <= n; q++) {
    for (ll w : inc[q]) {
      update(0, q - 1, 1, 0, n, w);
    }
    ll best = get(0, q - 1, 1, 0, n);
    update(q, q, 1, 0, n, best);
    for (pair<ll, ll> w : rem[q]) {
      update(0, w.first - 1, 1, 0, n, -1 * w.second);
    }
    ans = max(ans, best);
  }
  cout << ans << "\n";
  return 0;
}
Problem p03182 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fir first
#define sec second
#define MAX_N 100000
#define mid ((s + t) >> 1)
using namespace std;
typedef long long lnt;
typedef pair<int, int> pii;
template <class T> inline void read(T &x) {
  x = 0;
  int c = getchar(), f = 1;
  for (; !isdigit(c); c = getchar())
    if (c == 45)
      f = -1;
  for (; isdigit(c); c = getchar())
    (x *= 10) += f * (c - '0');
}
int n, m;
vector<pii> p[MAX_N + 5];
lnt tr[(MAX_N << 2) + 5], tag[(MAX_N << 2) + 5];
void modify(int v, int s, int t, int l, int r, lnt x) {
  if (s >= l && t <= r) {
    tr[v] += x, tag[v] += x;
    return;
  }
  if (l <= mid)
    modify(v << 1, s, mid, l, r, x);
  if (r >= mid + 1)
    modify(v << 1 | 1, mid + 1, t, l, r, x);
  tr[v] = max(tr[v << 1], tr[v << 1 | 1]) + tag[v];
}
int main() {
  read(n), read(m);
  for (int i = 1, l, r, c; i <= m; i++)
    read(l), read(r), read(c), p[r].push_back(pii(l, c));
  for (int i = 1; i <= n; i++) {
    modify(1, 1, n, i, i, tr[1]);
    for (pii q : p[i])
      modify(1, 1, n, q.fir, i, q.sec);
  }
  return printf("%lld\n", max(tr[1], 0LL)), 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define fir first
#define sec second
#define MAX_N 200000
#define mid ((s + t) >> 1)
using namespace std;
typedef long long lnt;
typedef pair<int, int> pii;
template <class T> inline void read(T &x) {
  x = 0;
  int c = getchar(), f = 1;
  for (; !isdigit(c); c = getchar())
    if (c == 45)
      f = -1;
  for (; isdigit(c); c = getchar())
    (x *= 10) += f * (c - '0');
}
int n, m;
vector<pii> p[MAX_N + 5];
lnt tr[(MAX_N << 2) + 5], tag[(MAX_N << 2) + 5];
void modify(int v, int s, int t, int l, int r, lnt x) {
  if (s >= l && t <= r) {
    tr[v] += x, tag[v] += x;
    return;
  }
  if (l <= mid)
    modify(v << 1, s, mid, l, r, x);
  if (r >= mid + 1)
    modify(v << 1 | 1, mid + 1, t, l, r, x);
  tr[v] = max(tr[v << 1], tr[v << 1 | 1]) + tag[v];
}
int main() {
  read(n), read(m);
  for (int i = 1, l, r, c; i <= m; i++)
    read(l), read(r), read(c), p[r].push_back(pii(l, c));
  for (int i = 1; i <= n; i++) {
    modify(1, 1, n, i, i, tr[1]);
    for (pii q : p[i])
      modify(1, 1, n, q.fir, i, q.sec);
  }
  return printf("%lld\n", max(tr[1], 0LL)), 0;
}
Problem p03182 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ll long long
#define N 100010
using namespace std;

struct query {
  ll l, r, v;
} a[N];
ll n, m, cnt, t[N << 2], lt[N << 2];

bool cmp(query a, query b) { return a.r < b.r; }

void push(int x) {
  t[x << 1] += lt[x];
  lt[x << 1] += lt[x];
  t[x << 1 | 1] += lt[x];
  lt[x << 1 | 1] += lt[x];
  lt[x] = 0;
}

void update(int x, int l, int r, int a, int b, ll v) {
  if (b < l || a > r)
    return;
  if (l == a && r == b) {
    t[x] += v;
    lt[x] += v;
    return;
  }
  push(x);
  int m = (l + r) / 2;
  update(2 * x, l, m, a, min(b, m), v);
  update(2 * x + 1, m + 1, r, max(a, m + 1), b, v);
  t[x] = max(t[2 * x], t[2 * x + 1]);
}

ll query(int x, int l, int r, int a, int b) {
  if (b < l || a > r)
    return 0;
  if (l == a && r == b)
    return t[x];
  push(x);
  int m = (l + r) / 2;
  return max(query(2 * x, l, m, a, min(b, m)),
             query(2 * x + 1, m + 1, r, max(a, m + 1), b));
}

int main() {
  cin >> n >> m;
  FOR(i, 0, m) cin >> a[i].l >> a[i].r >> a[i].v;
  sort(a, a + m, cmp);
  FOR(i, 1, n + 1) {
    update(1, 1, n, i, i, query(1, 1, n, 1, i));
    while (a[cnt].r == i) {
      update(1, 1, n, a[cnt].l, a[cnt].r, a[cnt].v);
      cnt++;
    }
  }
  cout << max(t[1], 0ll) << '\n';

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ll long long
#define N 200010
using namespace std;

struct query {
  ll l, r, v;
} a[N];
ll n, m, cnt, t[N << 2], lt[N << 2];

bool cmp(query a, query b) { return a.r < b.r; }

void push(int x) {
  t[x << 1] += lt[x];
  lt[x << 1] += lt[x];
  t[x << 1 | 1] += lt[x];
  lt[x << 1 | 1] += lt[x];
  lt[x] = 0;
}

void update(int x, int l, int r, int a, int b, ll v) {
  if (b < l || a > r)
    return;
  if (l == a && r == b) {
    t[x] += v;
    lt[x] += v;
    return;
  }
  push(x);
  int m = (l + r) / 2;
  update(2 * x, l, m, a, min(b, m), v);
  update(2 * x + 1, m + 1, r, max(a, m + 1), b, v);
  t[x] = max(t[2 * x], t[2 * x + 1]);
}

ll query(int x, int l, int r, int a, int b) {
  if (b < l || a > r)
    return 0;
  if (l == a && r == b)
    return t[x];
  push(x);
  int m = (l + r) / 2;
  return max(query(2 * x, l, m, a, min(b, m)),
             query(2 * x + 1, m + 1, r, max(a, m + 1), b));
}

int main() {
  cin >> n >> m;
  FOR(i, 0, m) cin >> a[i].l >> a[i].r >> a[i].v;
  sort(a, a + m, cmp);
  FOR(i, 1, n + 1) {
    update(1, 1, n, i, i, query(1, 1, n, 1, i));
    while (a[cnt].r == i) {
      update(1, 1, n, a[cnt].l, a[cnt].r, a[cnt].v);
      cnt++;
    }
  }
  cout << max(t[1], 0ll) << '\n';

  return 0;
}
Problem p03182 (C++)

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

const int TREE_SZ = 1 << 19;

vector<int> mx(TREE_SZ, -2e18);
vector<int> buf(TREE_SZ);

int upd(int v, int l, int r) {
  mx[v] += buf[v];
  if (l < r - 1) {
    buf[2 * v + 1] += buf[v];
    buf[2 * v + 2] += buf[v];
  }
  buf[v] = 0;
}

void add_seg(int v, int l, int r, int L, int R, int x) {
  upd(v, l, r);
  if (r <= L || R <= l) {
    return;
  }
  if (L <= l && r <= R) {
    buf[v] += x;
    upd(v, l, r);
    return;
  }
  int m = (l + r) / 2;
  add_seg(2 * v + 1, l, m, L, R, x);
  add_seg(2 * v + 2, m, r, L, R, x);
  mx[v] = max(mx[2 * v + 1], mx[2 * v + 2]);
}

void set_elem(int v, int l, int r, int i, int x) {
  upd(v, l, r);
  if (l == r - 1) {
    mx[v] = x;
    return;
  }
  int m = (l + r) / 2;
  if (i < m) {
    set_elem(2 * v + 1, l, m, i, x);
    upd(2 * v + 2, m, r);
  } else {
    upd(2 * v + 1, l, m);
    set_elem(2 * v + 2, m, r, i, x);
  }
  mx[v] = max(mx[2 * v + 1], mx[2 * v + 2]);
}

int32_t main() {
  int n, m;
  cin >> n >> m;
  vector<int> l(n, 0);
  vector<vector<pair<int, int>>> r(n + 1);
  for (int i = 0; i < m; i++) {
    int L, R, a;
    cin >> L >> R >> a;
    L--;
    l[L] += a;
    r[R].push_back({L, a});
  }
  int cur_max = 0;
  set_elem(0, -1, n, -1, 0);
  for (int i = 0; i < n; i++) {
    add_seg(0, -1, n, -1, i, l[i]);
    for (auto x : r[i]) {
      add_seg(0, -1, n, -1, x.first, -x.second);
    }
    cur_max = max(cur_max, mx[0]);
    set_elem(0, -1, n, i, mx[0]);
  }
  cout << cur_max << endl;
}


Fixed submission:
#include "bits/stdc++.h"
#define int long long
using namespace std;

const int TREE_SZ = 1 << 19;

vector<int> mx(TREE_SZ, -2e18);
vector<int> buf(TREE_SZ);

void upd(int v, int l, int r) {
  mx[v] += buf[v];
  if (l < r - 1) {
    buf[2 * v + 1] += buf[v];
    buf[2 * v + 2] += buf[v];
  }
  buf[v] = 0;
}

void add_seg(int v, int l, int r, int L, int R, int x) {
  upd(v, l, r);
  if (r <= L || R <= l) {
    return;
  }
  if (L <= l && r <= R) {
    buf[v] += x;
    upd(v, l, r);
    return;
  }
  int m = (l + r) / 2;
  add_seg(2 * v + 1, l, m, L, R, x);
  add_seg(2 * v + 2, m, r, L, R, x);
  mx[v] = max(mx[2 * v + 1], mx[2 * v + 2]);
}

void set_elem(int v, int l, int r, int i, int x) {
  upd(v, l, r);
  if (l == r - 1) {
    mx[v] = x;
    return;
  }
  int m = (l + r) / 2;
  if (i < m) {
    set_elem(2 * v + 1, l, m, i, x);
    upd(2 * v + 2, m, r);
  } else {
    upd(2 * v + 1, l, m);
    set_elem(2 * v + 2, m, r, i, x);
  }
  mx[v] = max(mx[2 * v + 1], mx[2 * v + 2]);
}

int32_t main() {
  int n, m;
  cin >> n >> m;
  vector<int> l(n, 0);
  vector<vector<pair<int, int>>> r(n + 1);
  for (int i = 0; i < m; i++) {
    int L, R, a;
    cin >> L >> R >> a;
    L--;
    l[L] += a;
    r[R].push_back({L, a});
  }
  int cur_max = 0;
  set_elem(0, -1, n, -1, 0);
  for (int i = 0; i < n; i++) {
    add_seg(0, -1, n, -1, i, l[i]);
    for (auto x : r[i]) {
      add_seg(0, -1, n, -1, x.first, -x.second);
    }
    cur_max = max(cur_max, mx[0]);
    set_elem(0, -1, n, i, mx[0]);
  }
  cout << cur_max << endl;
}
Problem p03182 (C++)

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

#define pb push_back
#define fi first
#define se second
#define pq priority_queue
#define mp make_pair
#define pii pair<int, int>
#define mod 998244353

int lowbit(int x) { return x & (-x); }

int n, m;
vector<pii> p[200010];
long long tree[400040];
long long tag[400040];

void update(int cur, int l, int r, int ll, int rr, long long c) {
  if (ll <= l && rr >= r) {
    tree[cur] += c;
    tag[cur] += c;
    return;
  }
  int mid = (l + r) >> 1;
  if (ll <= mid)
    update(cur * 2, l, mid, ll, rr, c);
  if (rr > mid)
    update(cur * 2 + 1, mid + 1, r, ll, rr, c);
  tree[cur] = max(tree[cur * 2], tree[cur * 2 + 1]) + tag[cur];
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int l, r, a;
    scanf("%d%d%d", &l, &r, &a);
    p[r].pb(mp(l, a));
  }
  for (int i = 1; i <= n; i++) {
    update(1, 1, n, i, i, tree[1]);
    for (int j = 0; j < p[i].size(); j++)
      update(1, 1, n, p[i][j].fi, i, p[i][j].se);
  }
  cout << max(0ll, tree[1]);
  return 0;
}


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

#define pb push_back
#define fi first
#define se second
#define pq priority_queue
#define mp make_pair
#define pii pair<int, int>
#define mod 998244353

int lowbit(int x) { return x & (-x); }

int n, m;
vector<pii> p[200010];
long long tree[800040];
long long tag[800040];

void update(int cur, int l, int r, int ll, int rr, long long c) {
  if (ll <= l && rr >= r) {
    tree[cur] += c;
    tag[cur] += c;
    return;
  }
  int mid = (l + r) >> 1;
  if (ll <= mid)
    update(cur * 2, l, mid, ll, rr, c);
  if (rr > mid)
    update(cur * 2 + 1, mid + 1, r, ll, rr, c);
  tree[cur] = max(tree[cur * 2], tree[cur * 2 + 1]) + tag[cur];
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int l, r, a;
    scanf("%d%d%d", &l, &r, &a);
    p[r].pb(mp(l, a));
  }
  for (int i = 1; i <= n; i++) {
    update(1, 1, n, i, i, tree[1]);
    for (int j = 0; j < p[i].size(); j++)
      update(1, 1, n, p[i][j].fi, i, p[i][j].se);
  }
  cout << max(0ll, tree[1]);
  return 0;
}
Problem p03183 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<ll, ll> l_l;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<l_l> vpl;
typedef vector<string> vs;
typedef pair<l_l, ll> lll;
#define pb push_back
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = 1; i <= (n); i++)
const int INF = 1001001000;
const int mINF = -1001001000;
const ll LINF = 10100100100100100;
const ll dx[4] = {1, -1, 0, 0};
const ll dy[4] = {0, 0, 1, -1};
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
ll n;
ll w[1100], s[1100], v[1100];
ll dp[1100][220000];
int main() {
  cin >> n;
  rep(i, n) cin >> w[i] >> s[i] >> v[i];
  vl block;
  rep(i, n) block.pb(i);
  sort(block.begin(), block.end(),
       [&](int x, int y) { return s[x] + w[x] < s[y] + w[y]; });
  for (int i = 1; i <= n; i++) {
    ll ind = block[i - 1];
    for (int j = 0; j < 220000; j++) {
      dp[i][j] = dp[i - 1][j];
      if (j - w[ind] <= s[ind] && j - w[ind] >= 0) {
        dp[i][j] = max(dp[i][j], dp[i - 1][j - w[ind]] + v[ind]);
      }
    }
  }
  ll ans = 0;
  rep(i, 220000) chmax(ans, dp[n][i]);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<ll, ll> l_l;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<l_l> vpl;
typedef vector<string> vs;
typedef pair<l_l, ll> lll;
#define pb push_back
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = 1; i <= (n); i++)
const int INF = 1001001000;
const int mINF = -1001001000;
const ll LINF = 10100100100100100;
const ll dx[4] = {1, -1, 0, 0};
const ll dy[4] = {0, 0, 1, -1};
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
ll n;
ll w[1100], s[1100], v[1100];
ll dp[1100][22000];
int main() {
  cin >> n;
  rep(i, n) cin >> w[i] >> s[i] >> v[i];
  vl block;
  rep(i, n) block.pb(i);
  sort(block.begin(), block.end(),
       [&](int x, int y) { return s[x] + w[x] < s[y] + w[y]; });
  for (int i = 1; i <= n; i++) {
    ll ind = block[i - 1];
    for (int j = 0; j < 220000; j++) {
      dp[i][j] = dp[i - 1][j];
      if (j - w[ind] <= s[ind] && j - w[ind] >= 0) {
        dp[i][j] = max(dp[i][j], dp[i - 1][j - w[ind]] + v[ind]);
      }
    }
  }
  ll ans = 0;
  rep(i, 220000) chmax(ans, dp[n][i]);
  cout << ans << endl;
  return 0;
}
Problem p03183 (C++)

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

#define x first
#define y second
#define y1 Y1
#define y2 Y2
#define mp make_pair
#define pb push_back

using namespace std;

typedef long long LL;
typedef pair<int, int> pii;

template <typename T> inline int Chkmax(T &a, T b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T> inline int Chkmin(T &a, T b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> T read() {
  T sum = 0, fl = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fl = -1;
  for (; isdigit(ch); ch = getchar())
    sum = (sum << 3) + (sum << 1) + ch - '0';
  return sum * fl;
}

inline void proc_status() {
  ifstream t("/proc/self/status");
  cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>())
       << endl;
}

const int Maxn = 1e3 + 100;

int N;
LL Dp[10000 + 100];
struct info {
  int w, s, v;
} A[Maxn];

inline int cmp(info a, info b) { return a.s + a.w < b.s + b.w; }

int Max;

inline void Solve() {
  sort(A + 1, A + N + 1, cmp);
  LL ans = 0;
  for (int i = 1; i <= N; ++i)
    for (int j = A[i].s; j >= 0; --j) {
      Chkmax(Dp[j + A[i].w], Dp[j] + A[i].v);
      Chkmax(ans, Dp[j + A[i].w]);
    }
  cout << ans << endl;
}

inline void Input() {
  N = read<int>();
  for (int i = 1; i <= N; ++i)
    A[i].w = read<int>(), A[i].s = read<int>(), A[i].v = read<int>(),
    Chkmax(Max, A[i].s);
}

int main() {
#ifdef hk_cnyali
  freopen("X.in", "r", stdin);
  freopen("X.out", "w", stdout);
#endif
  Input();
  Solve();
  return 0;
}

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

#define x first
#define y second
#define y1 Y1
#define y2 Y2
#define mp make_pair
#define pb push_back

using namespace std;

typedef long long LL;
typedef pair<int, int> pii;

template <typename T> inline int Chkmax(T &a, T b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T> inline int Chkmin(T &a, T b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> T read() {
  T sum = 0, fl = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fl = -1;
  for (; isdigit(ch); ch = getchar())
    sum = (sum << 3) + (sum << 1) + ch - '0';
  return sum * fl;
}

inline void proc_status() {
  ifstream t("/proc/self/status");
  cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>())
       << endl;
}

const int Maxn = 1e3 + 100;

int N;
LL Dp[20000 + 100];
struct info {
  int w, s, v;
} A[Maxn];

inline int cmp(info a, info b) { return a.s + a.w < b.s + b.w; }

int Max;

inline void Solve() {
  sort(A + 1, A + N + 1, cmp);
  LL ans = 0;
  for (int i = 1; i <= N; ++i)
    for (int j = A[i].s; j >= 0; --j) {
      Chkmax(Dp[j + A[i].w], Dp[j] + A[i].v);
      Chkmax(ans, Dp[j + A[i].w]);
    }
  cout << ans << endl;
}

inline void Input() {
  N = read<int>();
  for (int i = 1; i <= N; ++i)
    A[i].w = read<int>(), A[i].s = read<int>(), A[i].v = read<int>(),
    Chkmax(Max, A[i].s);
}

int main() {
#ifdef hk_cnyali
  freopen("X.in", "r", stdin);
  freopen("X.out", "w", stdout);
#endif
  Input();
  Solve();
  return 0;
}
Problem p03183 (C++)

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

using namespace std;

struct trio {
  int wt, sz, v;
  bool operator<(const trio &t) const { return sz + wt < t.sz + t.wt; }
};

int N;
trio arr[1005];
long long dp[10005];

int main() {
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> arr[i].wt >> arr[i].sz >> arr[i].v;
  }
  sort(arr + 1, arr + N + 1);
  long long ans = 0;
  for (int i = 1; i <= N; i++) {
    for (int j = arr[i].sz; j >= 0; j--) {
      dp[j + arr[i].wt] = max(dp[j + arr[i].wt], dp[j] + arr[i].v);
      ans = max(dp[j + arr[i].wt], ans);
    }
  }
  cout << ans << endl;
}


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

using namespace std;

struct trio {
  int wt, sz, v;
  bool operator<(const trio &t) const { return sz + wt < t.sz + t.wt; }
};

int N;
trio arr[1005];
long long dp[30005];

int main() {
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> arr[i].wt >> arr[i].sz >> arr[i].v;
  }
  sort(arr + 1, arr + N + 1);
  long long ans = 0;
  for (int i = 1; i <= N; i++) {
    for (int j = arr[i].sz; j >= 0; j--) {
      dp[j + arr[i].wt] = max(dp[j + arr[i].wt], dp[j] + arr[i].v);
      ans = max(dp[j + arr[i].wt], ans);
    }
  }
  cout << ans << endl;
}
Problem p03183 (C++)

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

typedef long long ll;

#define ff first
#define ss second
#define pb push_back
#define mp make_pair

using namespace std;

const int N = 1e4 + 69;
const ll INF = 1e18;

int n;
ll answer;
ll best[N];
bool fix[N];

vector<pair<int, int>> P;

struct bla {
  int w, s, v;
};

bla A[N];

bool cmp(bla A, bla B) {
  int cnt_A = A.s - B.w;
  int cnt_B = B.s - A.w;
  if (cnt_A == cnt_B) {
    return A.s < B.s;
  }
  return cnt_A < cnt_B;
}

int main() {

  ios::sync_with_stdio(0);

  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> A[i].w >> A[i].s >> A[i].v;
  }

  sort(A + 1, A + 1 + n, cmp);

  fix[0] = 1;
  cout << endl;
  for (int i = 1; i <= n; i++) {
    ll answer_i = 0;
    for (int j = 0; j <= A[i].s; j++) {
      answer_i = max(answer_i, best[j]);
    }

    answer_i += A[i].v;
    answer = max(answer, answer_i);

    for (int j = A[i].s; j >= 0; j--) {
      if (fix[j]) {
        int to = j + A[i].w;
        best[to] = max(best[to], best[j] + A[i].v);
        fix[to] = 1;
      }
    }
  }

  cout << answer << endl;
}


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

typedef long long ll;

#define ff first
#define ss second
#define pb push_back
#define mp make_pair

using namespace std;

const int N = 1e4 + 69;
const ll INF = 1e18;

int n;
ll answer;
ll best[N];
bool fix[N];

vector<pair<int, int>> P;

struct bla {
  int w, s, v;
};

bla A[N];

bool cmp(bla A, bla B) {
  int cnt_A = A.s - B.w;
  int cnt_B = B.s - A.w;
  if (cnt_A == cnt_B) {
    return A.s < B.s;
  }
  return cnt_A < cnt_B;
}

int main() {

  ios::sync_with_stdio(0);

  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> A[i].w >> A[i].s >> A[i].v;
  }

  sort(A + 1, A + 1 + n, cmp);

  fix[0] = 1;
  cout << endl;
  for (int i = 1; i <= n; i++) {
    ll answer_i = 0;
    for (int j = 0; j <= A[i].s; j++) {
      answer_i = max(answer_i, best[j]);
    }

    answer_i += A[i].v;
    answer = max(answer, answer_i);

    for (int j = A[i].s; j >= 0; j--) {
      if (fix[j]) {
        int to = j + A[i].w;
        if (to > 1e4)
          continue;
        best[to] = max(best[to], best[j] + A[i].v);
        fix[to] = 1;
      }
    }
  }

  cout << answer << endl;
}
Problem p03183 (C++)

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

#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#pragma GCC optimise("ofast")
#pragma GCC optimise("unroll-loops")

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int N = 100 + 10, W = 20010;
const ll MOD = 1000000000 + 7;
const ll INF = 1000000000000000000;
const ll LOG = 25;

int n, a[N], w[N], s[N], v[N], ind[N];
ll dp[N][W], ps[N][W];

bool cmp(int x, int y) { return w[x] + s[x] < s[y] + w[y]; }

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> w[i] >> s[i] >> v[i];
    ind[i] = i;
  }
  sort(ind + 1, ind + n + 1, cmp);
  for (int i = 1; i <= n; i++) {
    // cout << ind[i] << '\n';
    for (int j = 0; j < w[ind[i]]; j++)
      dp[i][j] = dp[i - 1][j];
    for (int j = w[ind[i]]; j <= min(W - 1, s[ind[i]] + w[ind[i]]); j++) {
      // cout << i << ' ' << j << ' ' << v[ind[i]] << '\n';
      dp[i][j] = max(dp[i - 1][j], ps[i - 1][j - w[ind[i]]] + v[ind[i]]);
    }
    for (int j = min(W - 1, s[ind[i]] + w[ind[i]]) + 1; j < W; j++)
      dp[i][j] = dp[i - 1][j];
    ps[i][0] = dp[i][0];
    for (int j = 1; j < W; j++)
      ps[i][j] = max(dp[i][j], ps[i][j - 1]);
  }
  // cout << ps[1][2] << '\n';
  // cout << dp[2][4] << '\n';
  cout << ps[n][W - 1];

  return 0;
}


Fixed submission:
// khodaya khodet komak kon
#include <bits/stdc++.h>

#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#pragma GCC optimise("ofast")
#pragma GCC optimise("unroll-loops")

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int N = 1000 + 10, W = 20010;
const ll MOD = 1000000000 + 7;
const ll INF = 1000000000000000000;
const ll LOG = 25;

int n, a[N], w[N], s[N], v[N], ind[N];
ll dp[N][W], ps[N][W];

bool cmp(int x, int y) { return w[x] + s[x] < s[y] + w[y]; }

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> w[i] >> s[i] >> v[i];
    ind[i] = i;
  }
  sort(ind + 1, ind + n + 1, cmp);
  for (int i = 1; i <= n; i++) {
    // cout << ind[i] << '\n';
    for (int j = 0; j < w[ind[i]]; j++)
      dp[i][j] = dp[i - 1][j];
    for (int j = w[ind[i]]; j <= min(W - 1, s[ind[i]] + w[ind[i]]); j++) {
      // cout << i << ' ' << j << ' ' << v[ind[i]] << '\n';
      dp[i][j] = max(dp[i - 1][j], ps[i - 1][j - w[ind[i]]] + v[ind[i]]);
    }
    for (int j = min(W - 1, s[ind[i]] + w[ind[i]]) + 1; j < W; j++)
      dp[i][j] = dp[i - 1][j];
    ps[i][0] = dp[i][0];
    for (int j = 1; j < W; j++)
      ps[i][j] = max(dp[i][j], ps[i][j - 1]);
  }
  // cout << ps[1][2] << '\n';
  // cout << dp[2][4] << '\n';
  cout << ps[n][W - 1];

  return 0;
}