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

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

using namespace std;

const int MAX = 1e5 + 10;
const long long mod = 1e9 + 7ll;
vector<vector<int>> adjlist;
long long dp[MAX][2];
int n;

long long solve(int parent, int node, int color) {
  if (adjlist.size() == 1 && parent != -1)
    return 1ll;
  long long &ret = dp[node][color];
  if (ret != -1)
    return ret;
  ret = 1ll;
  for (auto &child : adjlist[node]) {
    if (child == parent)
      continue;
    if (color == 1)
      ret = (ret * solve(node, child, 0)) % mod;
    else {
      long long ways = (solve(node, child, 0) + solve(node, child, 1)) % mod;
      ret = (ret * ways) % mod;
    }
  }
  return ret;
}

int main() {
  memset(dp, -1, sizeof(dp));
  ios::sync_with_stdio(0);
  cin.tie(0);
  scanf("%d", &n);
  adjlist = vector<vector<int>>(n + 2);
  for (int i = 0; i < n - 1; ++i) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    adjlist[x].push_back(y);
    adjlist[y].push_back(x);
  }
  return cout << ((solve(-1, 0, 0) + solve(-1, 0, 1)) % mod) % mod << "\n", 0;
}


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

using namespace std;

const int MAX = 1e5 + 10;
const long long mod = 1e9 + 7ll;
vector<vector<int>> adjlist;
long long dp[MAX][2];
int n;

long long solve(int parent, int node, int color) {
  if (adjlist.size() == 1 && parent != -1)
    return 1ll;
  long long &ret = dp[node][color];
  if (ret != -1)
    return ret;
  ret = 1ll;
  for (auto &child : adjlist[node]) {
    if (child == parent)
      continue;
    if (color == 1)
      ret = (ret * solve(node, child, 0)) % mod;
    else {
      long long ways = (solve(node, child, 0) + solve(node, child, 1)) % mod;
      ret = (ret * ways) % mod;
    }
  }
  return ret;
}

int main() {
  memset(dp, -1, sizeof(dp));
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  adjlist = vector<vector<int>>(n + 2);
  for (int i = 0; i < n - 1; ++i) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    adjlist[x].push_back(y);
    adjlist[y].push_back(x);
  }
  return cout << ((solve(-1, 0, 0) + solve(-1, 0, 1)) % mod) % mod << "\n", 0;
}
Problem p03175 (C++)

Buggy submission (status: Runtime Error):
#pragma gcc optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1e9 + 7;

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  vector<vector<int>> G(n + 1);
  for (int i = 0; i < n - 1; ++i) {
    int x, y;
    cin >> x >> y;
    G[x].emplace_back(y);
    G[y].emplace_back(x);
  }
  vector<vector<ll>> dp(n + 1, vector<ll>(n + 1));
  function<void(int, int)> dfs = [&](int u, int p) {
    dp[u][0] = dp[u][1] = 1;
    for (int &v : G[u]) {
      if (v == p)
        continue;
      dfs(v, u);
      (dp[u][0] *= (dp[v][0] + dp[v][1]) % MOD) %= MOD;
      (dp[u][1] *= dp[v][0]) %= MOD;
    }
  };
  dfs(1, 0);
  cout << (dp[1][0] + dp[1][1]) % MOD << '\n';
}

Fixed submission:
#pragma gcc optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1e9 + 7;

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  vector<vector<int>> G(n + 1);
  for (int i = 0; i < n - 1; ++i) {
    int x, y;
    cin >> x >> y;
    G[x].emplace_back(y);
    G[y].emplace_back(x);
  }
  vector<vector<ll>> dp(n + 1, vector<ll>(2));
  function<void(int, int)> dfs = [&](int u, int p) {
    dp[u][0] = dp[u][1] = 1;
    for (int &v : G[u]) {
      if (v == p)
        continue;
      dfs(v, u);
      (dp[u][0] *= (dp[v][0] + dp[v][1]) % MOD) %= MOD;
      (dp[u][1] *= dp[v][0]) %= MOD;
    }
  };
  dfs(1, 0);
  cout << (dp[1][0] + dp[1][1]) % MOD << '\n';
}
Problem p03175 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

typedef long long llt;

const int MaxN = 100000 + 5, MaxM = 100000 + 5;
const llt Mod = 1e9 + 7;

int N;
int cnte;
int Head[MaxN], To[MaxM], Next[MaxM];
int fa[MaxN];
llt dp[MaxN][2];

inline void add_edge(int from, int to) {
  cnte++;
  To[cnte] = to;
  Next[cnte] = Head[from];
  Head[from] = cnte;
}

void init() {
  scanf("%d", &N);
  for (int i = 1; i < N; ++i) {
    int u, v;
    scanf("%d %d", &u, &v);
    add_edge(u, v);
    add_edge(v, u);
  }
}

void dfs(int u) {
  dp[u][0] = dp[u][1] = 1;
  for (int i = Head[u]; i; i = Next[i]) {
    int v = To[i];
    if (v == fa[u])
      continue;
    fa[v] = u;
    dfs(v);

    dp[u][0] = dp[u][0] * (dp[v][0] + dp[v][1]) % Mod;
    dp[u][1] = dp[u][1] * dp[v][0] % Mod;
  }
}

void solve() {
  dfs(1);
  llt ans = (dp[1][0] + dp[1][1]) % Mod;
  cout << ans << endl;
}

int main() {
  init();
  solve();
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

typedef long long llt;

const int MaxN = 100000 + 5, MaxM = 200000 + 5;
const llt Mod = 1e9 + 7;

int N;
int cnte;
int Head[MaxN], To[MaxM], Next[MaxM];
int fa[MaxN];
llt dp[MaxN][2];

inline void add_edge(int from, int to) {
  cnte++;
  To[cnte] = to;
  Next[cnte] = Head[from];
  Head[from] = cnte;
}

void init() {
  scanf("%d", &N);
  for (int i = 1; i < N; ++i) {
    int u, v;
    scanf("%d %d", &u, &v);
    add_edge(u, v);
    add_edge(v, u);
  }
}

void dfs(int u) {
  dp[u][0] = dp[u][1] = 1;
  for (int i = Head[u]; i; i = Next[i]) {
    int v = To[i];
    if (v == fa[u])
      continue;
    fa[v] = u;
    dfs(v);

    dp[u][0] = dp[u][0] * (dp[v][0] + dp[v][1]) % Mod;
    dp[u][1] = dp[u][1] * dp[v][0] % Mod;
  }
}

void solve() {
  dfs(1);
  llt ans = (dp[1][0] + dp[1][1]) % Mod;
  cout << ans << endl;
}

int main() {
  init();
  solve();
  return 0;
}
Problem p03175 (C++)

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

using namespace std;
typedef long long ll;

int n;
int dp[100100][2];
int head[100100], nxt[100100], to[100100];
int edgeCount;
int md = 1e9 + 7;
void addEdge(int x, int y) {
  to[edgeCount] = y;
  nxt[edgeCount] = head[x];
  head[x] = edgeCount;
  ++edgeCount;
}
int f(int u, int p, bool blockBlack) {
  int &ret = dp[u][blockBlack];
  if (~ret)
    return ret;
  ll ret1 = 1;
  ll ret2 = !blockBlack;
  for (int v = head[u]; ~v; v = nxt[v])
    if (to[v] != p) {
      ret1 *= f(to[v], u, 0);
      if (ret1 >= md)
        ret1 %= md;
      if (!blockBlack) {
        ret2 *= f(to[v], u, 1);
        if (ret2 >= md)
          ret2 %= md;
      }
    }
  ret1 += ret2;
  if (ret1 >= md)
    ret1 -= md;
  return ret = ret1;
}
int main() {
  memset(head, -1, sizeof head);
  memset(dp, -1, sizeof dp);

  scanf("%d", &n);
  for (int i = 1, x, y; i < n; ++i) {
    scanf("%d%d", &x, &y);
    --x;
    --y;
    addEdge(x, y);
    addEdge(y, x);
  }
  printf("%d\n", f(0, 0, 0));

  return 0;
}


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

using namespace std;
typedef long long ll;

int n;
int dp[100100][2];
int head[200100], nxt[200100], to[200100];
int edgeCount;
int md = 1e9 + 7;
void addEdge(int x, int y) {
  to[edgeCount] = y;
  nxt[edgeCount] = head[x];
  head[x] = edgeCount;
  ++edgeCount;
}
int f(int u, int p, bool blockBlack) {
  int &ret = dp[u][blockBlack];
  if (~ret)
    return ret;
  ll ret1 = 1;
  ll ret2 = !blockBlack;
  for (int v = head[u]; ~v; v = nxt[v])
    if (to[v] != p) {
      ret1 *= f(to[v], u, 0);
      if (ret1 >= md)
        ret1 %= md;
      if (!blockBlack) {
        ret2 *= f(to[v], u, 1);
        if (ret2 >= md)
          ret2 %= md;
      }
    }
  ret1 += ret2;
  if (ret1 >= md)
    ret1 -= md;
  return ret = ret1;
}
int main() {
  memset(head, -1, sizeof head);
  memset(dp, -1, sizeof dp);

  scanf("%d", &n);
  for (int i = 1, x, y; i < n; ++i) {
    scanf("%d%d", &x, &y);
    --x;
    --y;
    addEdge(x, y);
    addEdge(y, x);
  }
  printf("%d\n", f(0, 0, 0));

  return 0;
}
Problem p03175 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;

#define INF 1000000000000009LL
#define MOD 1000000007LL

ll dp[100005][2];
vector<vector<int>> v(10000);

ll dfs(int node, int prev_color, int p) {
  if (dp[node][prev_color] != -1)
    return dp[node][prev_color];
  ll white = 1LL, black = 1LL - ll(prev_color);
  for (int i = 0; i < v[node].size(); i++) {
    if (v[node][i] != p) {
      if (prev_color == 0) {
        white = (white * dfs(v[node][i], 0, node)) % MOD;
        black = (black * dfs(v[node][i], 1, node)) % MOD;
      } else {
        white = (white * dfs(v[node][i], 0, node)) % MOD;
        black = 0LL;
      }
    }
  }
  return dp[node][prev_color] = (white + black) % MOD;
}

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

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

  for (int i = 0; i < N; i++) {
    dp[i][0] = dp[i][1] = -1;
  }
  cout << dfs(0, 0, -1) << endl;
  return 0;
}


Fixed submission:
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;

#define INF 1000000000000009LL
#define MOD 1000000007LL

ll dp[100005][2];
vector<vector<int>> v(100000);

ll dfs(int node, int prev_color, int p) {
  if (dp[node][prev_color] != -1)
    return dp[node][prev_color];
  ll white = 1LL, black = 1LL - ll(prev_color);
  for (int i = 0; i < v[node].size(); i++) {
    if (v[node][i] != p) {
      if (prev_color == 0) {
        white = (white * dfs(v[node][i], 0, node)) % MOD;
        black = (black * dfs(v[node][i], 1, node)) % MOD;
      } else {
        white = (white * dfs(v[node][i], 0, node)) % MOD;
        black = 0LL;
      }
    }
  }
  return dp[node][prev_color] = (white + black) % MOD;
}

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

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

  for (int i = 0; i < N; i++) {
    dp[i][0] = dp[i][1] = -1;
  }
  cout << dfs(0, 0, -1) << endl;
  return 0;
}
Problem p03175 (C++)

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

vector<int> tree[100001];
int dp[100001][2];

int solve(int u, int constraint, int pr) {
  if (dp[u][constraint] != -1) {
    return dp[u][constraint];
  }
  int ans = 0;
  int w1 = 1;
  for (int child : tree[u]) {
    if (child != pr) {
      w1 = (w1 * solve(child, 0, u)) % 1000000007;
    }
  }
  ans = ans + w1;
  if (!constraint) {
    int w2 = 1;
    for (int child : tree[u]) {
      if (child != pr) {
        w2 = (w2 * solve(child, 1, u)) % 1000000007;
      }
    }
    ans = (ans + w2) % 1000000007;
  }
  return dp[u][constraint] = ans;
}
int32_t main() {
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v;
    tree[u].push_back(v);
    tree[v].push_back(u);
  }
  memset(dp, -1, sizeof dp);
  cout << solve(1, 0, -1);
}


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

vector<int> tree[100001];
int dp[100001][2];

int solve(int u, int constraint, int pr) {
  if (dp[u][constraint] != -1) {
    return dp[u][constraint];
  }
  int ans = 0;
  int w1 = 1;
  for (int child : tree[u]) {
    if (child != pr) {
      w1 = (w1 * solve(child, 0, u)) % 1000000007;
    }
  }
  ans = ans + w1;
  if (!constraint) {
    int w2 = 1;
    for (int child : tree[u]) {
      if (child != pr) {
        w2 = (w2 * solve(child, 1, u)) % 1000000007;
      }
    }
    ans = (ans + w2) % 1000000007;
  }
  return dp[u][constraint] = ans;
}
int32_t main() {
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    tree[u].push_back(v);
    tree[v].push_back(u);
  }
  memset(dp, -1, sizeof dp);
  cout << solve(1, 0, -1);
}
Problem p03176 (C++)

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

using namespace std;
#define taskname "TEST"
#define pb push_back
typedef long double ld;
typedef long long ll;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
int n;
ll dp[maxn], a[maxn], h[maxn];
void update(int x, ll val) {
  for (; x <= n; x += x & -x)
    dp[x] = max(dp[x], val);
}
ll query(int x) {
  ll res = 0;
  for (; x > 0; x &= x - 1)
    res = max(res, dp[x]);
  return res;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  if (fopen(taskname ".INP", "r"))
    freopen(taskname ".INP", "r", stdin), freopen(taskname ".OUT", "w", stdout);
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> h[i];
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  for (int i = 1; i <= n; ++i) {
    update(h[i], query(h[i] - 1) + a[i]);
  }
  cout << query(n);
}


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

using namespace std;
#define taskname "TEST"
#define pb push_back
typedef long double ld;
typedef long long ll;
const int maxn = 2e5 + 5;
const int mod = 1e9 + 7;
int n;
ll dp[maxn], a[maxn], h[maxn];
void update(int x, ll val) {
  for (; x <= n; x += x & -x)
    dp[x] = max(dp[x], val);
}
ll query(int x) {
  ll res = 0;
  for (; x > 0; x &= x - 1)
    res = max(res, dp[x]);
  return res;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  if (fopen(taskname ".INP", "r"))
    freopen(taskname ".INP", "r", stdin), freopen(taskname ".OUT", "w", stdout);
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> h[i];
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  for (int i = 1; i <= n; ++i) {
    update(h[i], query(h[i] - 1) + a[i]);
  }
  cout << query(n);
}
Problem p03176 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define sc(n) scanf("%d", &n)
#define scl(n) scanf("%lld", &n)
#define pr(n) printf("%d", n)
#define prl(n) printf("%lld", n)
#define nl printf("\n")
#define fr(i, n) for (i = 0; i < n; i++)
#define rep(i, st, en) for (i = st; i <= en; i++)
#define repv(i, en, st) for (i = en; i >= st; i--)
#define fout cout << fixed << setprecision(7)
#define bi(n) __builtin_popcount(n)
#define bil(n) __builtin_popcountll(n)

typedef long long ll;
typedef pair<int, int> pii;
const int N = 200010;
ll mod = 1e9 + 7;
ll fmod(ll b, ll exp) {
  ll res = 1;
  while (exp) {
    if (exp & 1ll)
      res = (res * b) % mod;
    b = (b * b) % mod;
    exp /= 2ll;
  }
  return res;
}
int n;
ll A[N], h[N], bit[N];
void upd(int idx, ll val) {
  for (int i = idx; i <= n; i += (i & -i))
    bit[i] = max(bit[i], val);
}
ll query(int idx) {
  ll res = 0;
  for (int i = idx; i; i--)
    res = max(res, bit[i]);
  return res;
}

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1, i, j, m;
  cin >> n;
  rep(i, 1, n) cin >> h[i];
  rep(i, 1, n) cin >> A[i];
  ll mx = 0;
  rep(i, 1, n) {
    ll cur = query(h[i]);
    ll ans = cur + A[i];
    mx = max(mx, ans);
    upd(h[i], ans);
  }
  cout << mx << "\n";

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define sc(n) scanf("%d", &n)
#define scl(n) scanf("%lld", &n)
#define pr(n) printf("%d", n)
#define prl(n) printf("%lld", n)
#define nl printf("\n")
#define fr(i, n) for (i = 0; i < n; i++)
#define rep(i, st, en) for (i = st; i <= en; i++)
#define repv(i, en, st) for (i = en; i >= st; i--)
#define fout cout << fixed << setprecision(7)
#define bi(n) __builtin_popcount(n)
#define bil(n) __builtin_popcountll(n)

typedef long long ll;
typedef pair<int, int> pii;
const int N = 200010;
ll mod = 1e9 + 7;
ll fmod(ll b, ll exp) {
  ll res = 1;
  while (exp) {
    if (exp & 1ll)
      res = (res * b) % mod;
    b = (b * b) % mod;
    exp /= 2ll;
  }
  return res;
}
int n;
ll A[N], h[N], bit[N];
void upd(int idx, ll val) {
  for (int i = idx; i <= n; i += (i & -i))
    bit[i] = max(bit[i], val);
}
ll query(int idx) {
  ll res = 0;
  for (int i = idx; i; i -= (i & -i))
    res = max(res, bit[i]);
  return res;
}

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1, i, j, m;
  cin >> n;
  rep(i, 1, n) cin >> h[i];
  rep(i, 1, n) cin >> A[i];
  ll mx = 0;
  rep(i, 1, n) {
    ll cur = query(h[i]);
    ll ans = cur + A[i];
    mx = max(mx, ans);
    upd(h[i], ans);
  }
  cout << mx << "\n";

  return 0;
}
Problem p03176 (C++)

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

#define ll long long

using namespace std;

const int N = 2e5 + 123, MAXN = 1e9;

int n;

ll h[N], a[N], dp[N], ans, L[N], R[N], sz = 1, t[N * 40];

void upd(int pos, ll val, int v = 1, int tl = 1, int tr = MAXN) {
  if (tl == tr) {
    t[v] = max(val, t[v]);
    return;
  }
  int tm = (tl + tr) >> 1;
  if (pos <= tm) {
    if (!L[v])
      L[v] = ++sz;
    upd(pos, val, L[v], tl, tm);
  } else {
    if (!R[v])
      R[v] = ++sz;
    upd(pos, val, R[v], tm + 1, tr);
  }
  t[v] = max(t[L[v]], t[R[v]]);
}

ll get(int l, int r, int v = 1, int tl = 1, int tr = MAXN) {
  if (tl > r || l > tr || !v)
    return 0;
  if (tl >= l && r >= tr)
    return t[v];
  int tm = (tl + tr) >> 1;
  return max(get(l, r, L[v], tl, tm), get(l, r, R[v], tm + 1, tr));
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    dp[i] = get(1, h[i] - 1) + a[i];
    upd(h[i], dp[i]);
    ans = max(ans, dp[i]);
  }
  cout << ans;
}


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

#define ll long long

using namespace std;

const int N = 2e5 + 123, MAXN = 1e9;

int n;

ll h[N], a[N], dp[N], ans, L[N * 40], R[N * 40], sz = 1, t[N * 40];

void upd(int pos, ll val, int v = 1, int tl = 1, int tr = MAXN) {
  if (tl == tr) {
    t[v] = max(val, t[v]);
    return;
  }
  int tm = (tl + tr) >> 1;
  if (pos <= tm) {
    if (!L[v])
      L[v] = ++sz;
    upd(pos, val, L[v], tl, tm);
  } else {
    if (!R[v])
      R[v] = ++sz;
    upd(pos, val, R[v], tm + 1, tr);
  }
  t[v] = max(t[L[v]], t[R[v]]);
}

ll get(int l, int r, int v = 1, int tl = 1, int tr = MAXN) {
  if (tl > r || l > tr || !v)
    return 0;
  if (tl >= l && r >= tr)
    return t[v];
  int tm = (tl + tr) >> 1;
  return max(get(l, r, L[v], tl, tm), get(l, r, R[v], tm + 1, tr));
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    dp[i] = get(1, h[i] - 1) + a[i];
    upd(h[i], dp[i]);
    ans = max(ans, dp[i]);
  }
  cout << ans;
}
Problem p03176 (C++)

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

#define show(x) cerr << #x << " = " << x << endl;
#define input_from_file freopen("input.txt", "r", stdin)
#define output_to_file freopen("output.txt", "w", stdout)
#define fastio ios_base::sync_with_stdio(0)
#define endl '\n'

const int N = 1e5 + 1;
long long tree[4 * N];
vector<vector<int>> v(N, vector<int>(3));
void update(int l, int r, int pos, int index, long long val) {
  if (l > r)
    return;
  else if (l == r) {
    tree[pos] = val;
    return;
  }
  int mid = l + (r - l) / 2;
  if (index <= mid)
    update(l, mid, 2 * pos + 1, index, val);
  else
    update(mid + 1, r, 2 * pos + 2, index, val);
  tree[pos] = max(tree[2 * pos + 1], tree[2 * pos + 2]);
  // cerr <<l << " "<< r << " "<< pos << " "<< tree[pos] << endl;
  return;
}
long long query(int ql, int qr, int l, int r, int pos) {
  if (l > r || qr < l || ql > r)
    return 0;
  else if (ql <= l && qr >= r)
    return tree[pos];
  int mid = l + (r - l) / 2;
  return max(query(ql, qr, l, mid, 2 * pos + 1),
             query(ql, qr, mid + 1, r, 2 * pos + 2));
}

int main() {
#ifdef LOCAL
  input_from_file;
  output_to_file;
#endif
  fastio;
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> v[i][0];
    v[i][1] = i;
  }
  for (int i = 0; i < n; ++i)
    cin >> v[i][2];
  sort(v.begin(), v.begin() + n);
  long long x;
  for (int i = 0; i < n; ++i) {
    x = query(0, v[i][1] - 1, 0, n - 1, 0);
    update(0, n - 1, 0, v[i][1], x + 1LL * v[i][2]);
  }
  cout << tree[0];

  return 0;
}

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

#define show(x) cerr << #x << " = " << x << endl;
#define input_from_file freopen("input.txt", "r", stdin)
#define output_to_file freopen("output.txt", "w", stdout)
#define fastio ios_base::sync_with_stdio(0)
#define endl '\n'

const int N = 2e5 + 1;
long long tree[4 * N];
vector<vector<int>> v(N, vector<int>(3));
void update(int l, int r, int pos, int index, long long val) {
  if (l > r)
    return;
  else if (l == r) {
    tree[pos] = val;
    return;
  }
  int mid = l + (r - l) / 2;
  if (index <= mid)
    update(l, mid, 2 * pos + 1, index, val);
  else
    update(mid + 1, r, 2 * pos + 2, index, val);
  tree[pos] = max(tree[2 * pos + 1], tree[2 * pos + 2]);
  // cerr <<l << " "<< r << " "<< pos << " "<< tree[pos] << endl;
  return;
}
long long query(int ql, int qr, int l, int r, int pos) {
  if (l > r || qr < l || ql > r)
    return 0;
  else if (ql <= l && qr >= r)
    return tree[pos];
  int mid = l + (r - l) / 2;
  return max(query(ql, qr, l, mid, 2 * pos + 1),
             query(ql, qr, mid + 1, r, 2 * pos + 2));
}

int main() {
#ifdef LOCAL
  input_from_file;
  output_to_file;
#endif
  fastio;
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> v[i][0];
    v[i][1] = i;
  }
  for (int i = 0; i < n; ++i)
    cin >> v[i][2];
  sort(v.begin(), v.begin() + n);
  long long x;
  for (int i = 0; i < n; ++i) {
    x = query(0, v[i][1] - 1, 0, n - 1, 0);
    update(0, n - 1, 0, v[i][1], x + 1LL * v[i][2]);
  }
  cout << tree[0];

  return 0;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(s) memset(s, 0, sizeof(s))
const int INF = 1e9;
inline int read() {
  int X = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch))
    X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  return w ? -X : X;
}

int n;
ll A[100005], h[100005], bit[100005];
void upd(int idx, ll val) {
  for (int i = idx; i <= n; i += (i & -i)) {
    bit[i] = max(bit[i], val);
  }
}
ll query(int idx) {
  ll res = 0;
  for (int i = idx; i; i -= (i & -i)) {
    res = max(res, bit[i]);
  }
  return res;
}
int main() {
  int t = 1;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
  }
  ll mx = 0;
  for (int i = 1; i <= n; i++) {
    ll cur = query(h[i]);
    ll ans = cur + A[i];
    mx = max(mx, ans);
    upd(h[i], ans);
  }
  cout << mx << "\n";
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(s) memset(s, 0, sizeof(s))
const int INF = 1e9;
inline int read() {
  int X = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch))
    X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  return w ? -X : X;
}

int n;
ll A[1000005], h[1000005], bit[1000005];
void upd(int idx, ll val) {
  for (int i = idx; i <= n; i += (i & -i)) {
    bit[i] = max(bit[i], val);
  }
}
ll query(int idx) {
  ll res = 0;
  for (int i = idx; i; i -= (i & -i)) {
    res = max(res, bit[i]);
  }
  return res;
}
int main() {
  int t = 1;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
  }
  ll mx = 0;
  for (int i = 1; i <= n; i++) {
    ll cur = query(h[i]);
    ll ans = cur + A[i];
    mx = max(mx, ans);
    upd(h[i], ans);
  }
  cout << mx << "\n";
  return 0;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <utility>
#include <vector>
#define MAX_DOUBLE 40000000000.0
#include <math.h>
#include <unistd.h>

using namespace std;

const int N = 100010;

int n;

long long tree[2 * N];

long long max(long long a, long long b) {
  if (a > b)
    return a;
  return b;
}

void updateTreeNode(int p, long long value) {
  tree[p + n] = value;
  p = p + n;

  for (int i = p; i > 1; i >>= 1)
    tree[i >> 1] = max(tree[i], tree[i ^ 1]);
}

long long query(int l, int r) {
  long long res = 0;

  for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
    if (l & 1)
      res = max(res, tree[l++]);

    if (r & 1)
      res = max(res, tree[--r]);
  }

  return res;
}

int main() {

  cin >> n;

  vector<int> h;
  vector<long long> p;

  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    h.push_back(a);
  }

  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    p.push_back(a);
  }

  for (int i = 0; i < n; i++) {
    long long sf = query(1, h[i]);
    //		cout<<"sf:  "<<sf<<endl;
    updateTreeNode(h[i], sf + p[i]);
  }

  cout << query(1, n + 1);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <utility>
#include <vector>
#define MAX_DOUBLE 40000000000.0
#include <math.h>
#include <unistd.h>

using namespace std;

const int N = 262144;

int n;

long long tree[2 * N];

long long max(long long a, long long b) {
  if (a > b)
    return a;
  return b;
}

void updateTreeNode(int p, long long value) {
  tree[p + n] = value;
  p = p + n;

  for (int i = p; i > 1; i >>= 1)
    tree[i >> 1] = max(tree[i], tree[i ^ 1]);
}

long long query(int l, int r) {
  long long res = 0;

  for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
    if (l & 1)
      res = max(res, tree[l++]);

    if (r & 1)
      res = max(res, tree[--r]);
  }

  return res;
}

int main() {

  cin >> n;

  vector<int> h;
  vector<long long> p;

  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    h.push_back(a);
  }

  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    p.push_back(a);
  }

  for (int i = 0; i < n; i++) {
    long long sf = query(1, h[i]);
    //		cout<<"sf:  "<<sf<<endl;
    updateTreeNode(h[i], sf + p[i]);
  }

  cout << query(1, n + 1);

  return 0;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <vector>

using namespace std;
const long long MAXN = 100100;
const long long MOD = 1e9 + 7;
vector<long long> v1[MAXN];

long long fenwick[MAXN + 2];
long long arr[MAXN];
long long h[MAXN];
long long n;
void update(long long pos, long long val) {
  for (; pos <= n; pos += (pos & (-pos))) {
    fenwick[pos] = max(val, fenwick[pos]);
  }
}
long long query(long long pos) {
  long long ans = 0;
  for (; pos; pos -= (pos & (-pos))) {
    ans = max(ans, fenwick[pos]);
  }
  return ans;
}
long long ans;
int main() {

  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> arr[i];
  }
  for (long long i = 0; i < n; i++) {
    cin >> h[i];
  }
  for (long long i = 0; i < n; i++) {
    long long temp = query(arr[i]) + h[i];
    ans = max(ans, temp);
    update(arr[i], temp);
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <vector>

using namespace std;
const long long MAXN = 200100;
const long long MOD = 1e9 + 7;
vector<long long> v1[MAXN];

long long fenwick[MAXN + 2];
long long arr[MAXN];
long long h[MAXN];
long long n;
void update(long long pos, long long val) {
  for (; pos <= n; pos += (pos & (-pos))) {
    fenwick[pos] = max(val, fenwick[pos]);
  }
}
long long query(long long pos) {
  long long ans = 0;
  for (; pos; pos -= (pos & (-pos))) {
    ans = max(ans, fenwick[pos]);
  }
  return ans;
}
long long ans;
int main() {

  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> arr[i];
  }
  for (long long i = 0; i < n; i++) {
    cin >> h[i];
  }
  for (long long i = 0; i < n; i++) {
    long long temp = query(arr[i]) + h[i];
    ans = max(ans, temp);
    update(arr[i], temp);
  }
  cout << ans << endl;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long c[100001];
int h[100001], n;
int lowbit(int x) { return x & (-x); }
void updatemax(int index, long long value) {
  while (index <= n && value > c[index]) {
    c[index] = value;
    index += lowbit(index);
  }
}
long long maxrange(int index) {
  long long maxv = 0;
  while (index) {
    maxv = max(maxv, c[index]);
    index -= lowbit(index);
  }
  return maxv;
}
int main() {
  int a, i;
  long long t, maxv = 0;
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    scanf("%d", &h[i]);
  for (i = 1; i <= n; i++) {
    scanf("%d", &a);
    t = maxrange(h[i] - 1) + a;
    maxv = max(maxv, t);
    updatemax(h[i], t);
  }
  printf("%lld", maxv);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long c[200001];
int h[200001], n;
int lowbit(int x) { return x & (-x); }
void updatemax(int index, long long value) {
  while (index <= n && value > c[index]) {
    c[index] = value;
    index += lowbit(index);
  }
}
long long maxrange(int index) {
  long long maxv = 0;
  while (index) {
    maxv = max(maxv, c[index]);
    index -= lowbit(index);
  }
  return maxv;
}
int main() {
  int a, i;
  long long t, maxv = 0;
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    scanf("%d", &h[i]);
  for (i = 1; i <= n; i++) {
    scanf("%d", &a);
    t = maxrange(h[i] - 1) + a;
    maxv = max(maxv, t);
    updatemax(h[i], t);
  }
  printf("%lld", maxv);
  return 0;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MAX = 1e5 + 5;
LL a[MAX], b[MAX], dp[MAX];
LL ST[4 * MAX + 5];

LL query(LL start, LL end, LL node, LL s, LL e) {
  if (e < start || s > end || start > end)
    return 0;
  if (s <= start && e >= end) {
    return ST[node];
  }
  LL mid = (start + end) / 2;
  return max(query(start, mid, 2 * node, s, e),
             query(mid + 1, end, 2 * node + 1, s, e));
}

void update(LL start, LL end, LL node, LL pos, LL val) {
  if (pos < start || pos > end || start > end)
    return;
  if (start == end) {
    ST[node] = val;
    return;
  }
  LL mid = (start + end) / 2;
  if (pos >= start && pos <= mid)
    update(start, mid, 2 * node, pos, val);
  else
    update(mid + 1, end, 2 * node + 1, pos, val);
  ST[node] = max(ST[2 * node], ST[2 * node + 1]);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  memset(ST, 0, sizeof(ST));
  LL n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
  }
  for (int i = 1; i <= n; i++) {
    LL x = query(1, n, 1, 1, a[i]);
    dp[i] = x + b[i];
    update(1, n, 1, a[i], dp[i]);
  }
  LL ans = 0;
  for (int i = 1; i <= n; i++) {
    // cout << dp[i] << " ";
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MAX = 2e5 + 5;
LL a[MAX], b[MAX], dp[MAX];
LL ST[4 * MAX + 5];

LL query(LL start, LL end, LL node, LL s, LL e) {
  if (e < start || s > end || start > end)
    return 0;
  if (s <= start && e >= end) {
    return ST[node];
  }
  LL mid = (start + end) / 2;
  return max(query(start, mid, 2 * node, s, e),
             query(mid + 1, end, 2 * node + 1, s, e));
}

void update(LL start, LL end, LL node, LL pos, LL val) {
  if (pos < start || pos > end || start > end)
    return;
  if (start == end) {
    ST[node] = val;
    return;
  }
  LL mid = (start + end) / 2;
  if (pos >= start && pos <= mid)
    update(start, mid, 2 * node, pos, val);
  else
    update(mid + 1, end, 2 * node + 1, pos, val);
  ST[node] = max(ST[2 * node], ST[2 * node + 1]);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  memset(ST, 0, sizeof(ST));
  LL n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
  }
  for (int i = 1; i <= n; i++) {
    LL x = query(1, n, 1, 1, a[i]);
    dp[i] = x + b[i];
    update(1, n, 1, a[i], dp[i]);
  }
  LL ans = 0;
  for (int i = 1; i <= n; i++) {
    // cout << dp[i] << " ";
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p03176 (C++)

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

using namespace std;
typedef vector<int> VI;
typedef long long LL;
typedef pair<int, int> PI;
typedef pair<LL, LL> PLL;
typedef unsigned long long ULL;
typedef pair<double, double> PD;

#define FOR(x, b, e) for (int x = b; x <= (e); x++)
#define FORD(x, b, e) for (int x = b; x >= (e); x--)
#define REP(x, n) for (int x = 0; x < (n); ++x)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(x) ((int)(x).size())

#define PB push_back
#define IN insert
#define ST first
#define ND second
#define INF 2000000011
#define MOD 1000000007

#define MAXSIZE 200010
#define MAX_T 524300

int n;
int szer = 1;
LL tree[MAXSIZE];
int h[MAXSIZE];

LL query(int v, int l, int r, int a, int b) { //[l,r] szukany,[a,b] ogolny
  if (l == a && b == r)
    return tree[v];

  int mid = (a + b - 1) / 2;
  if (r <= mid)
    return query(v * 2, l, r, a, mid);
  else if (l > mid)
    return query(v * 2 + 1, l, r, mid + 1, b);
  else
    return max(query(v * 2, l, mid, a, mid),
               query(v * 2 + 1, mid + 1, r, mid + 1, b));
}
void ins(int p, LL val) {
  p += szer;
  tree[p] = val;
  p /= 2;

  while (p) {
    tree[p] = max(tree[p * 2], tree[p * 2 + 1]);
    p /= 2;
  }
}

int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;

  while (szer <= n)
    szer *= 2;

  REP(i, n)
  cin >> h[i];

  REP(i, n) {
    LL a1;
    cin >> a1;
    LL dp = query(1, 1, h[i], 0, szer - 1);
    ins(h[i], a1 + dp);
  }

  cout << query(1, 1, n, 0, szer - 1);

  return 0;
}


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

using namespace std;
typedef vector<int> VI;
typedef long long LL;
typedef pair<int, int> PI;
typedef pair<LL, LL> PLL;
typedef unsigned long long ULL;
typedef pair<double, double> PD;

#define FOR(x, b, e) for (int x = b; x <= (e); x++)
#define FORD(x, b, e) for (int x = b; x >= (e); x--)
#define REP(x, n) for (int x = 0; x < (n); ++x)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(x) ((int)(x).size())

#define PB push_back
#define IN insert
#define ST first
#define ND second
#define INF 2000000011
#define MOD 1000000007

#define MAXSIZE 200010
#define MAX_T 524300

int n;
int szer = 1;
LL tree[MAX_T];
int h[MAXSIZE];

LL query(int v, int l, int r, int a, int b) { //[l,r] szukany,[a,b] ogolny
  if (l == a && b == r)
    return tree[v];

  int mid = (a + b - 1) / 2;
  if (r <= mid)
    return query(v * 2, l, r, a, mid);
  else if (l > mid)
    return query(v * 2 + 1, l, r, mid + 1, b);
  else
    return max(query(v * 2, l, mid, a, mid),
               query(v * 2 + 1, mid + 1, r, mid + 1, b));
}
void ins(int p, LL val) {
  p += szer;
  tree[p] = val;
  p /= 2;

  while (p) {
    tree[p] = max(tree[p * 2], tree[p * 2 + 1]);
    p /= 2;
  }
}

int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;

  while (szer <= n)
    szer *= 2;

  REP(i, n)
  cin >> h[i];

  REP(i, n) {
    LL a1;
    cin >> a1;
    LL dp = query(1, 1, h[i], 0, szer - 1);
    ins(h[i], a1 + dp);
  }

  cout << query(1, 1, n, 0, szer - 1);

  return 0;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
#define int ll
const int N = 100000;
int n, a[N], h[N], dp[N];
#define all(x) x.begin(), x.end()
struct segment {
  int seg[200001 * 4] = {0};
  int pull(int x, int y) { return max(seg[x], seg[y]); }
  void update(int id, int l, int r, int i, int v) {
    if (l == r) {
      seg[id] = max(seg[id], v);
      return;
    }
    int m = (l + r) / 2;
    if (i <= m)
      update(id * 2 + 1, l, m, i, v);
    else
      update(id * 2 + 2, m + 1, r, i, v);
    seg[id] = pull(id * 2 + 1, id * 2 + 2);
  }
  int query(int id, int l, int r, int ql, int qr) {
    if (qr < l || r < ql)
      return 0;
    if (ql <= r && r <= qr)
      return seg[id];
    int m = (l + r) / 2;
    return max(query(id * 2 + 1, l, m, ql, qr),
               query(id * 2 + 2, m + 1, r, ql, qr));
  }

} seg;
void bi() {
  vector<int> tmp(h, h + n);
  sort(all(tmp));
  tmp.resize(unique(all(tmp)) - tmp.begin());
  for (int i = 0; i < n; i++) {
    h[i] = upper_bound(all(tmp), h[i]) - tmp.begin() - 1;
  }
}
signed main() {
  ios_base::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> h[i];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  bi();
  dp[0] = a[0];
  seg.update(0, 0, n - 1, h[0], a[0]);
  int ans = a[0];
  for (int i = 1; i < n; i++) {
    dp[i] = seg.query(0, 0, n - 1, 0, h[i] - 1) + a[i];
    seg.update(0, 0, n - 1, h[i], dp[i]);
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
#define int ll
const int N = 200001;
int n, a[N], h[N], dp[N];
#define all(x) x.begin(), x.end()
struct segment {
  int seg[200001 * 4] = {0};
  int pull(int x, int y) { return max(seg[x], seg[y]); }
  void update(int id, int l, int r, int i, int v) {
    if (l == r) {
      seg[id] = max(seg[id], v);
      return;
    }
    int m = (l + r) / 2;
    if (i <= m)
      update(id * 2 + 1, l, m, i, v);
    else
      update(id * 2 + 2, m + 1, r, i, v);
    seg[id] = pull(id * 2 + 1, id * 2 + 2);
  }
  int query(int id, int l, int r, int ql, int qr) {
    if (qr < l || r < ql)
      return 0;
    if (ql <= r && r <= qr)
      return seg[id];
    int m = (l + r) / 2;
    return max(query(id * 2 + 1, l, m, ql, qr),
               query(id * 2 + 2, m + 1, r, ql, qr));
  }

} seg;
void bi() {
  vector<int> tmp(h, h + n);
  sort(all(tmp));
  tmp.resize(unique(all(tmp)) - tmp.begin());
  for (int i = 0; i < n; i++) {
    h[i] = upper_bound(all(tmp), h[i]) - tmp.begin() - 1;
  }
}
signed main() {
  ios_base::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> h[i];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  bi();
  dp[0] = a[0];
  seg.update(0, 0, n - 1, h[0], a[0]);
  int ans = a[0];
  for (int i = 1; i < n; i++) {
    dp[i] = seg.query(0, 0, n - 1, 0, h[i] - 1) + a[i];
    seg.update(0, 0, n - 1, h[i], dp[i]);
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
}
Problem p03176 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <stdio.h>
#define Re register int
#define LL long long

int N, H[200005], A[200005];
LL f[200005], T[800005], ans;

template <typename T> inline void read(T &var) {
  T x = 0;
  int w = 0;
  char ch = 0;
  while (!isdigit(ch))
    w |= ch == '-', ch = getchar();
  while (isdigit(ch))
    x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  var = w ? -x : x;
}
void Add(int id, int l, int r, int pos, LL v) {
  if (l == r && l == pos) {
    T[id] = std::max(T[id], v);
    return;
  }
  int mid = (l + r) >> 1;
  if (pos <= mid)
    Add(id << 1, l, mid, pos, v);
  else
    Add(id << 1 | 1, mid + 1, r, pos, v);
  T[id] = std::max(T[id << 1], T[id << 1 | 1]);
}
LL Query(int id, int l, int r, int pos) {
  if (r <= pos)
    return T[id];
  int mid = (l + r) >> 1;
  LL ret = Query(id << 1, l, mid, pos);
  if (mid < pos)
    ret = std::max(ret, Query(id << 1 | 1, mid + 1, r, pos));
  return ret;
}
int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
  freopen("1.in", "r", stdin);
  // freopen("1.ans","w",stdout);
#endif
  read(N);
  for (Re i = 1; i <= N; ++i)
    read(H[i]);
  for (Re i = 1; i <= N; ++i)
    read(A[i]), f[i] = A[i];
  ans = A[1], Add(1, 1, N, H[1], f[1]);
  for (Re i = 2; i <= N; ++i) {
    f[i] = Query(1, 1, N, H[i]) + A[i];
    Add(1, 1, N, H[i], f[i]);
    ans = std::max(ans, f[i]);
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <iostream>
#include <stdio.h>
#define Re register int
#define LL long long

int N, H[200005], A[200005];
LL f[200005], T[800005], ans;

template <typename T> inline void read(T &var) {
  T x = 0;
  int w = 0;
  char ch = 0;
  while (!isdigit(ch))
    w |= ch == '-', ch = getchar();
  while (isdigit(ch))
    x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  var = w ? -x : x;
}
void Add(int id, int l, int r, int pos, LL v) {
  if (l == r && l == pos) {
    T[id] = std::max(T[id], v);
    return;
  }
  int mid = (l + r) >> 1;
  if (pos <= mid)
    Add(id << 1, l, mid, pos, v);
  else
    Add(id << 1 | 1, mid + 1, r, pos, v);
  T[id] = std::max(T[id << 1], T[id << 1 | 1]);
}
LL Query(int id, int l, int r, int pos) {
  if (r <= pos)
    return T[id];
  int mid = (l + r) >> 1;
  LL ret = Query(id << 1, l, mid, pos);
  if (mid < pos)
    ret = std::max(ret, Query(id << 1 | 1, mid + 1, r, pos));
  return ret;
}
int main(int argc, char const *argv[]) {
  read(N);
  for (Re i = 1; i <= N; ++i)
    read(H[i]);
  for (Re i = 1; i <= N; ++i)
    read(A[i]), f[i] = A[i];
  ans = A[1], Add(1, 1, N, H[1], f[1]);
  for (Re i = 2; i <= N; ++i) {
    f[i] = Query(1, 1, N, H[i]) + A[i];
    Add(1, 1, N, H[i], f[i]);
    ans = std::max(ans, f[i]);
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MOD 1000000007
#define ll long long
#define mp make_pair
#define pb push_back
#define N 100010
using namespace std;
ll tree[4 * N], A[N], h[N];
void update(int low, int high, int ind, ll val, int node) {
  if (low == high) {
    tree[node] = val;
    return;
  }
  int mid = (low + high) / 2;
  if (ind <= mid)
    update(low, mid, ind, val, 2 * node);
  else
    update(mid + 1, high, ind, val, 2 * node + 1);
  tree[node] = max(tree[2 * node], tree[2 * node + 1]);
}
ll query(int low, int high, int begin, int end, int node) {
  if (low > end || high < begin || begin > end)
    return -1e18;
  if (low >= begin && high <= end)
    return tree[node];
  int mid = (low + high) / 2;
  return max(query(low, mid, begin, end, 2 * node),
             query(mid + 1, high, begin, end, 2 * node + 1));
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> h[i];
  for (int i = 0; i < n; i++)
    cin >> A[i];
  ll maxi = 0;
  for (int i = 0; i < n; i++) {
    ll ans = A[i] + query(0, n - 1, 0, h[i] - 1, 1);
    // cerr << ans << '\n';
    update(0, n - 1, h[i], ans, 1);
    maxi = max(maxi, ans);
  }
  cout << maxi << '\n';
}

Fixed submission:
#include <bits/stdc++.h>
#define MOD 1000000007
#define ll long long
#define mp make_pair
#define pb push_back
#define N 200010
using namespace std;
ll tree[4 * N], A[N], h[N];
void update(int low, int high, int ind, ll val, int node) {
  if (low == high) {
    tree[node] = val;
    return;
  }
  int mid = (low + high) / 2;
  if (ind <= mid)
    update(low, mid, ind, val, 2 * node);
  else
    update(mid + 1, high, ind, val, 2 * node + 1);
  tree[node] = max(tree[2 * node], tree[2 * node + 1]);
}
ll query(int low, int high, int begin, int end, int node) {
  if (low > end || high < begin || begin > end)
    return -1e18;
  if (low >= begin && high <= end)
    return tree[node];
  int mid = (low + high) / 2;
  return max(query(low, mid, begin, end, 2 * node),
             query(mid + 1, high, begin, end, 2 * node + 1));
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> h[i];
  for (int i = 0; i < n; i++)
    cin >> A[i];
  ll maxi = 0;
  for (int i = 0; i < n; i++) {
    ll ans = A[i] + query(0, n - 1, 0, h[i] - 1, 1);
    // cerr << ans << '\n';
    update(0, n - 1, h[i], ans, 1);
    maxi = max(maxi, ans);
  }
  cout << maxi << '\n';
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):

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

typedef long long ll;
#define lowbit(x) (x & -x)

const int maxn = 100001;
int n;
ll h[maxn];
ll a[maxn];
ll dp[maxn];
ll bit[maxn];

ll query(ll x) {
  ll res = 0;
  for (; x; x -= lowbit(x)) {
    res = max(bit[x], res);
  }
  return res;
}

void update(ll x, ll y) {
  for (; x <= n; x += lowbit(x)) {
    bit[x] = max(bit[x], y);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> h[i];
  }
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; ++i) {
    dp[i] = query(h[i]) + a[i];
    update(h[i], dp[i]);
  }

  cout << query(n) << endl;

  return 0;
}


Fixed submission:

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

typedef long long ll;
#define lowbit(x) (x & -x)

const int maxn = 200001;
int n;
ll h[maxn];
ll a[maxn];
ll dp[maxn];
ll bit[maxn];

ll query(ll x) {
  ll res = 0;
  for (; x; x -= lowbit(x)) {
    res = max(bit[x], res);
  }
  return res;
}

void update(ll x, ll y) {
  for (; x <= n; x += lowbit(x)) {
    bit[x] = max(bit[x], y);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> h[i];
  }
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; ++i) {
    dp[i] = query(h[i]) + a[i];
    update(h[i], dp[i]);
  }

  cout << query(n) << endl;

  return 0;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define INF (1ll << 60)

int n;
vector<vector<int>> G;

class BIT {
public:
  vector<long long> bit;
  int M;

  BIT(int M) : bit(vector<long long>(M + 1, -(1ll << 60))), M(M) {}

  long long sum(int i) {
    if (i <= 0)
      return 0;
    return max(bit[i], sum(i - (i & -i)));
  }

  void add(int i, long long x) {
    if (i > M)
      return;
    bit[i] = max(bit[i], x);
    add(i + (i & -i), x);
  }
};

int main(int argc, char const *argv[]) {
  cin >> n;
  vector<long long> h(n + 1, -1);
  vector<long long> a(n + 1, 0), b(n + 1, 0);
  vector<long long> dp(n + 1, 0);
  for (int i = 0; i < n; ++i)
    cin >> h[i + 1];
  for (int i = 0; i < n; ++i)
    cin >> a[i + 1];
  for (int i = 0; i <= n; ++i)
    b[h[i]] = i;
  long long res = 0;
  int m = (1 << 20);
  BIT bit(m);
  bit.add(1, 0);
  for (int i = 1; i <= n; ++i) {
    int k = b[i];
    long long tmp = bit.sum(1 + k) + a[k];
    res = max(res, tmp);
    bit.add(1 + k, tmp);
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define INF (1ll << 60)

int n;
vector<vector<int>> G;

class BIT {
public:
  vector<long long> bit;
  int M;

  BIT(int M) : bit(vector<long long>(M + 1, -(1ll << 60))), M(M) {}

  long long sum(int i) {
    if (i <= 0)
      return 0;
    return max(bit[i], sum(i - (i & -i)));
  }

  void add(int i, long long x) {
    if (i > M)
      return;
    bit[i] = max(bit[i], x);
    add(i + (i & -i), x);
  }
};

int main(int argc, char const *argv[]) {
  cin >> n;
  vector<long long> h(n + 1, 0);
  vector<long long> a(n + 1, 0), b(n + 1, 0);
  vector<long long> dp(n + 1, 0);
  for (int i = 0; i < n; ++i)
    cin >> h[i + 1];
  for (int i = 0; i < n; ++i)
    cin >> a[i + 1];
  for (int i = 0; i <= n; ++i)
    b[h[i]] = i;
  long long res = 0;
  int m = (1 << 20);
  BIT bit(m);
  bit.add(1, 0);
  for (int i = 1; i <= n; ++i) {
    int k = b[i];
    long long tmp = bit.sum(1 + k) + a[k];
    res = max(res, tmp);
    bit.add(1 + k, tmp);
  }
  cout << res << endl;
  return 0;
}