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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
typedef long long ll;
ll abi[maxn];
int N;
int h[maxn], b[maxn];
void update(int pos, ll v) {
  for (; pos <= N; pos += pos & -pos)
    abi[pos] = max(abi[pos], v);
}
ll query(int pos) {
  ll sum = 0;
  for (; pos; pos -= pos & -pos)
    sum = max(abi[pos], sum);
  return sum;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // freopen(".in","r",stdin);
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> h[i];
  }
  for (int i = 1; i <= N; i++) {
    cin >> b[i];
  }
  for (int i = 1; i <= N; i++) {
    ll x = query(h[i] - 1) + b[i];
    update(h[i], x);
  }
  cout << query(N);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
typedef long long ll;
ll abi[maxn];
int N;
int h[maxn], b[maxn];
void update(int pos, ll v) {
  for (; pos <= N; pos += pos & -pos)
    abi[pos] = max(abi[pos], v);
}
ll query(int pos) {
  ll sum = 0;
  for (; pos; pos -= pos & -pos)
    sum = max(abi[pos], sum);
  return sum;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // freopen(".in","r",stdin);
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> h[i];
  }
  for (int i = 1; i <= N; i++) {
    cin >> b[i];
  }
  for (int i = 1; i <= N; i++) {
    ll x = query(h[i] - 1) + b[i];
    update(h[i], x);
  }
  cout << query(N);
  return 0;
}
Problem p03176 (C++)

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

#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()

using namespace std;

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

const int N = 100010;
const int MOD = 1000000007;
const int LOG = 20;

ll fen[N];
int n, h[N], a[N];

void add(int id, ll x) {
  id++;
  for (; id < N; id += id & (-id))
    fen[id] = max(fen[id], x);
}

ll get(int id) {
  id++;
  ll res = 0;
  for (; id; id -= id & (-id))
    res = max(res, fen[id]);
  return res;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  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++) {
    ll x = get(h[i] - 1) + a[i];
    add(h[i], x);
  }
  cout << get(n);

  return 0;
}

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

#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()

using namespace std;

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

const int N = 200010;
const int MOD = 1000000007;
const int LOG = 20;

ll fen[N];
int n, h[N], a[N];

void add(int id, ll x) {
  id++;
  for (; id < N; id += id & (-id))
    fen[id] = max(fen[id], x);
}

ll get(int id) {
  id++;
  ll res = 0;
  for (; id; id -= id & (-id))
    res = max(res, fen[id]);
  return res;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  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++) {
    ll x = get(h[i] - 1) + a[i];
    add(h[i], x);
  }
  cout << get(n);

  return 0;
}
Problem p03176 (C++)

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

struct RMQ {
  static const lint MAX_N = 1 << 17;
  lint n, dat[2 * MAX_N - 1];
  RMQ() {}
  RMQ(lint n_) { init(n_); }
  void init(lint n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    for (int i = 0; i < 2 * n - 1; i++)
      dat[i] = 0;
  }
  void update(lint k, lint a) {
    k += n - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }
  lint query(lint a, lint b, lint k, lint l, lint r) {
    if (r <= a || b <= l)
      return 0;
    if (a <= l && r <= b)
      return dat[k];
    else {
      lint vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
      lint vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
      return max(vl, vr);
    }
  }
  lint query(lint a, lint b) { return query(a, b, 0, 0, n); }
};

signed main() {
  int N;
  cin >> N;
  vector<lint> h(N), a(N);
  RMQ rmq(N + 10);
  for (int i = 0; i < N; i++)
    cin >> h[i], h[i]--;
  for (int i = 0; i < N; i++)
    cin >> a[i];
  lint ans = 0;
  for (int i = 0; i < N; i++) {
    lint ans1 = a[i] + rmq.query(0, h[i]);
    ans = max(ans, ans1);
    rmq.update(h[i], ans1);
  }
  cout << ans << endl;
}


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

struct RMQ {
  static const lint MAX_N = 3e5;
  lint n, dat[2 * MAX_N - 1];
  RMQ() {}
  RMQ(lint n_) { init(n_); }
  void init(lint n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    for (int i = 0; i < 2 * n - 1; i++)
      dat[i] = 0;
  }
  void update(lint k, lint a) {
    k += n - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }
  lint query(lint a, lint b, lint k, lint l, lint r) {
    if (r <= a || b <= l)
      return 0;
    if (a <= l && r <= b)
      return dat[k];
    else {
      lint vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
      lint vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
      return max(vl, vr);
    }
  }
  lint query(lint a, lint b) { return query(a, b, 0, 0, n); }
};

signed main() {
  int N;
  cin >> N;
  vector<lint> h(N), a(N);
  RMQ rmq(N + 10);
  for (int i = 0; i < N; i++)
    cin >> h[i], h[i]--;
  for (int i = 0; i < N; i++)
    cin >> a[i];
  lint ans = 0;
  for (int i = 0; i < N; i++) {
    lint ans1 = a[i] + rmq.query(0, h[i]);
    ans = max(ans, ans1);
    rmq.update(h[i], ans1);
  }
  cout << ans << endl;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define Int long long
#define N 200

Int dp[N];
Int tree[2 * N + 1];
Int a[N], f[N];

void update(Int x, Int v, Int node, Int l, Int r) {
  if (l == r) {
    tree[node] = v;
    return;
  }
  Int mid = (l + r) / 2;
  if (x <= mid) {
    update(x, v, (2 * node), l, mid);
  } else {
    update(x, v, (2 * node + 1), mid + 1, r);
  }
  tree[node] = max(tree[2 * node], tree[2 * node + 1]);
}

Int Q(Int s, Int e, Int node, Int l, Int r) {
  if (l >= s && r <= e)
    return tree[node];
  else if (l > e || r < s)
    return 0;
  int mid = (l + r) / 2;
  Int x = Q(s, e, 2 * node, l, mid);
  Int y = Q(s, e, 2 * node + 1, mid + 1, r);
  return max(x, y);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  Int n, m;
  cin >> n;
  for (Int i = 1; i <= n; i++)
    cin >> a[i];
  for (Int i = 1; i <= n; i++)
    cin >> f[i];
  for (Int i = 1; i <= n; i++) {
    dp[a[i]] = Q(1, (a[i] - 1), 1, 1, n) + f[i];

    update(a[i], dp[a[i]], 1, 1, n);
    // cout<<a[i]<< "  "<<Q(1,(a[i]),1,1,n)<<" "<<dp[a[i]]<<endl;
  }
  Int ans = 0;
  for (Int i = 1; i <= n; i++)
    ans = max(ans, dp[a[i]]);
  // for(Int i = 1;i<=(2*n-1);i++)cout<<i<<" "<<tree[i]<<endl;
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define Int long long
#define N 200200

Int dp[N];
Int tree[2 * N + 1];
Int a[N], f[N];

void update(Int x, Int v, Int node, Int l, Int r) {
  if (l == r) {
    tree[node] = v;
    return;
  }
  Int mid = (l + r) / 2;
  if (x <= mid) {
    update(x, v, (2 * node), l, mid);
  } else {
    update(x, v, (2 * node + 1), mid + 1, r);
  }
  tree[node] = max(tree[2 * node], tree[2 * node + 1]);
}

Int Q(Int s, Int e, Int node, Int l, Int r) {
  if (l >= s && r <= e)
    return tree[node];
  else if (l > e || r < s)
    return 0;
  int mid = (l + r) / 2;
  Int x = Q(s, e, 2 * node, l, mid);
  Int y = Q(s, e, 2 * node + 1, mid + 1, r);
  return max(x, y);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  Int n, m;
  cin >> n;
  for (Int i = 1; i <= n; i++)
    cin >> a[i];
  for (Int i = 1; i <= n; i++)
    cin >> f[i];
  for (Int i = 1; i <= n; i++) {
    dp[a[i]] = Q(1, (a[i] - 1), 1, 1, n) + f[i];

    update(a[i], dp[a[i]], 1, 1, n);
    // cout<<a[i]<< "  "<<Q(1,(a[i]),1,1,n)<<" "<<dp[a[i]]<<endl;
  }
  Int ans = 0;
  for (Int i = 1; i <= n; i++)
    ans = max(ans, dp[a[i]]);
  // for(Int i = 1;i<=(2*n-1);i++)cout<<i<<" "<<tree[i]<<endl;
  cout << ans << endl;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
long long t[512000];
int h[200001];
int n;
long long query(int index, int lb, int rb, int lq, int rq) {
  if (lb == rb)
    return t[index];
  if (rb == rq && lb == lq)
    return t[index];
  int mid = (lb + rb) / 2;
  if (rq <= mid)
    return query(index * 2, lb, mid, lq, rq);
  else if (lq > mid)
    return query(index * 2 + 1, mid + 1, rb, lq, rq);
  else
    return max(query(index * 2, lb, mid, lq, mid),
               query(index * 2 + 1, mid + 1, rb, mid + 1, rq));
}
long long update(int index, int lb, int rb, int leaf_id, long long value) {
  if (lb == rb && lb == leaf_id)
    return t[index] = value;
  int mid = (lb + rb) / 2;
  if (leaf_id <= mid)
    return t[index] = max(t[index], update(index * 2, lb, mid, leaf_id, value));
  else
    return t[index] = max(t[index],
                          update(index * 2 + 1, mid + 1, rb, leaf_id, value));
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &h[i]);
  for (int i = 1; i <= n; i++) {
    long long a;
    scanf("%lld", &a);
    update(1, 1, n, h[i], (long long)query(1, 1, n, 1, h[i] - 1) + a);
  }
  printf("%lld", t[1]);
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
long long t[600000];
int h[200001];
int n;
long long query(int index, int lb, int rb, int lq, int rq) {
  if (lb == rb)
    return t[index];
  if (rb == rq && lb == lq)
    return t[index];
  int mid = (lb + rb) / 2;
  if (rq <= mid)
    return query(index * 2, lb, mid, lq, rq);
  else if (lq > mid)
    return query(index * 2 + 1, mid + 1, rb, lq, rq);
  else
    return max(query(index * 2, lb, mid, lq, mid),
               query(index * 2 + 1, mid + 1, rb, mid + 1, rq));
}
long long update(int index, int lb, int rb, int leaf_id, long long value) {
  if (lb == rb && lb == leaf_id)
    return t[index] = value;
  int mid = (lb + rb) / 2;
  if (leaf_id <= mid)
    return t[index] = max(t[index], update(index * 2, lb, mid, leaf_id, value));
  else
    return t[index] = max(t[index],
                          update(index * 2 + 1, mid + 1, rb, leaf_id, value));
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &h[i]);
  for (int i = 1; i <= n; i++) {
    long long a;
    scanf("%lld", &a);
    update(1, 1, n, h[i], (long long)query(1, 1, n, 1, h[i] - 1) + a);
  }
  printf("%lld", t[1]);
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define pb push_back
// #all(x) (x).begin(),(x).end()

// int const N = 1;
int const MOD = 1e9 + 7;

ll bigmod(ll a, ll b) {
  if (b == 0)
    return 1 % MOD;
  ll x = bigmod(a, b / 2);
  x = (x * x) % MOD;
  if (b % 2)
    x = (x * a) % MOD;
  return x;
}

int const N = 1e5 + 10; //;
ll h[N], seg[4 * N];
ll dp[N];

ll Q(int pos, int l, int r, int ql, int qr) {
  if (l > qr or r < ql)
    return 0;
  if (ql <= l and r <= qr)
    return seg[pos];
  int mid = (l + r) / 2;
  return max(Q(pos * 2, l, mid, ql, qr), Q(pos * 2 + 1, mid + 1, r, ql, qr));
}

void update(int pos, int l, int r, int ind, ll val) {
  if (l == r and r == ind) {
    seg[pos] = val;
    return;
  }

  if (l > ind or r < ind)
    return;
  int mid = (l + r) / 2;

  update(pos * 2, l, mid, ind, val);
  update(pos * 2 + 1, mid + 1, r, ind, val);
  seg[pos] = max(seg[pos * 2], seg[pos * 2 + 1]);
}
int n;
ll val[N];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n;

  for (int i = 1; i <= n; i++)
    cin >> h[i];
  for (int i = 1; i <= n; i++)
    cin >> val[i];
  dp[0] = 0;

  for (int i = 1; i <= n; i++) {
    if (h[i] == 1) {
      dp[i] = val[i];
      update(1, 1, n, h[i], val[i]);
      continue;
    }

    ll here = Q(1, 1, n, 1, h[i] - 1);
    dp[i] = val[i] + here;
    update(1, 1, n, h[i], dp[i]);
  }
  cout << *max_element(dp + 1, dp + n + 1);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define pb push_back
// #all(x) (x).begin(),(x).end()

// int const N = 1;
int const MOD = 1e9 + 7;

ll bigmod(ll a, ll b) {
  if (b == 0)
    return 1 % MOD;
  ll x = bigmod(a, b / 2);
  x = (x * x) % MOD;
  if (b % 2)
    x = (x * a) % MOD;
  return x;
}

int const N = 2e5 + 10; //;
ll h[N], seg[4 * N];
ll dp[N];

ll Q(int pos, int l, int r, int ql, int qr) {
  if (l > qr or r < ql)
    return 0;
  if (ql <= l and r <= qr)
    return seg[pos];
  int mid = (l + r) / 2;
  return max(Q(pos * 2, l, mid, ql, qr), Q(pos * 2 + 1, mid + 1, r, ql, qr));
}

void update(int pos, int l, int r, int ind, ll val) {
  if (l == r and r == ind) {
    seg[pos] = val;
    return;
  }

  if (l > ind or r < ind)
    return;
  int mid = (l + r) / 2;

  update(pos * 2, l, mid, ind, val);
  update(pos * 2 + 1, mid + 1, r, ind, val);
  seg[pos] = max(seg[pos * 2], seg[pos * 2 + 1]);
}
int n;
ll val[N];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n;

  for (int i = 1; i <= n; i++)
    cin >> h[i];
  for (int i = 1; i <= n; i++)
    cin >> val[i];
  dp[0] = 0;

  for (int i = 1; i <= n; i++) {
    if (h[i] == 1) {
      dp[i] = val[i];
      update(1, 1, n, h[i], val[i]);
      continue;
    }

    ll here = Q(1, 1, n, 1, h[i] - 1);
    dp[i] = val[i] + here;
    update(1, 1, n, h[i], dp[i]);
  }
  cout << *max_element(dp + 1, dp + n + 1);
  return 0;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):

// Problem: Q - Flowers
// Contest: AtCoder - Educational DP Contest
// URL: https://atcoder.jp/contests/dp/tasks/dp_q
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

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

const int N = 800080;
long long lc[N], rc[N], t[N];
long long h[N], a[N];
int n;
#define ls (p << 1)
#define rs (p << 1 | 1)
void pushup(int p) { t[p] = max(max(t[p], t[ls]), t[rs]); }
void build(int p, int l, int r) {
  lc[p] = l;
  rc[p] = r;
  if (l == r)
    return;
  int mid = (l + r) >> 1;
  build(ls, l, mid);
  build(rs, mid + 1, r);
  //    pushup(p);
}
long long query(int p, int l, int r) {
  if (l <= lc[p] && rc[p] <= r)
    return t[p];
  if (l > rc[p] || rc[p] < l)
    return 0LL;
  return max(query(ls, l, r), query(rs, l, r));
}
void modify(int p, int x, long long v) {
  if (x == lc[p] && x == rc[p]) {
    t[p] = v;
    return;
  }
  if (x > rc[p] || x < lc[p])
    return;
  modify(ls, x, v);
  modify(rs, x, v);
  pushup(p);
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> h[i];
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  build(1, 0, n);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    long long cur;
    cur = query(1, 0, h[i] - 1) + a[i];
    ans = max(ans, cur);
    modify(1, h[i], cur);
  }
  cout << ans << endl;
}


Fixed submission:

// Problem: Q - Flowers
// Contest: AtCoder - Educational DP Contest
// URL: https://atcoder.jp/contests/dp/tasks/dp_q
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

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

const int N = 800080;
long long lc[N], rc[N], t[N];
long long h[N], a[N];
int n;
#define ls (p << 1)
#define rs (p << 1 | 1)
void pushup(int p) { t[p] = max(max(t[p], t[ls]), t[rs]); }
void build(int p, int l, int r) {
  lc[p] = l;
  rc[p] = r;
  if (l == r)
    return;
  int mid = (l + r) >> 1;
  build(ls, l, mid);
  build(rs, mid + 1, r);
  //    pushup(p);
}
long long query(int p, int l, int r) {
  if (l <= lc[p] && rc[p] <= r)
    return t[p];
  if (l > rc[p] || r < lc[p])
    return 0LL;
  return max(query(ls, l, r), query(rs, l, r));
}
void modify(int p, int x, long long v) {
  if (x == lc[p] && x == rc[p]) {
    t[p] = v;
    return;
  }
  if (x > rc[p] || x < lc[p])
    return;
  modify(ls, x, v);
  modify(rs, x, v);
  pushup(p);
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> h[i];
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  build(1, 0, n);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    long long cur;
    cur = query(1, 0, h[i] - 1) + a[i];
    ans = max(ans, cur);
    modify(1, h[i], cur);
  }
  cout << ans << endl;
}
Problem p03176 (C++)

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

long long segTree[1000000] = {};
int segSize;

void init(int n) {
  segSize = 1;
  while (segSize < n)
    segSize *= 2;
  return;
}

void update(int i, long long val) {
  i = segSize - 1 + i;
  segTree[i] = val;

  while (i > 0) {
    i = (i - 1) / 2;
    segTree[i] = max(segTree[i * 2 + 1], segTree[i * 2 + 2]);
  }

  return;
}

long long getmax(int a, int b, int k = 0, int l = 0, int r = segSize) {
  if (r <= a || b <= l)
    return 0;
  if (a <= l && r <= b)
    return segTree[k];

  long long vl = getmax(a, b, 2 * k + 1, l, (l + r) / 2);
  long long vr = getmax(a, b, 2 * k + 2, (l + r) / 2, r);
  return max(vl, vr);
}

int h[100010];
long long a[100010];

priority_queue<pair<int, int>> que;
int main() {
  int n;

  cin >> n;

  init(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
    que.push(make_pair(h[i], i));
  }

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

  long long ans = 0;

  while (!que.empty()) {
    int i = que.top().second;
    que.pop();

    long long next = a[i] + getmax(i + 1, n);
    update(i, next);
    ans = max(ans, next);
  }

  cout << ans << endl;

  return 0;
}

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

long long segTree[1000000] = {};
int segSize;

void init(int n) {
  segSize = 1;
  while (segSize < n)
    segSize *= 2;
  return;
}

void update(int i, long long val) {
  i = segSize - 1 + i;
  segTree[i] = val;

  while (i > 0) {
    i = (i - 1) / 2;
    segTree[i] = max(segTree[i * 2 + 1], segTree[i * 2 + 2]);
  }

  return;
}

long long getmax(int a, int b, int k = 0, int l = 0, int r = segSize) {
  if (r <= a || b <= l)
    return 0;
  if (a <= l && r <= b)
    return segTree[k];

  long long vl = getmax(a, b, 2 * k + 1, l, (l + r) / 2);
  long long vr = getmax(a, b, 2 * k + 2, (l + r) / 2, r);
  return max(vl, vr);
}

int h[200010];
long long a[200010];

priority_queue<pair<int, int>> que;
int main() {
  int n;

  cin >> n;

  init(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
    que.push(make_pair(h[i], i));
  }

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

  long long ans = 0;

  while (!que.empty()) {
    int i = que.top().second;
    que.pop();

    long long next = a[i] + getmax(i + 1, n);
    update(i, next);
    ans = max(ans, next);
  }

  cout << ans << endl;

  return 0;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define de cout << "debuglandınız\n";
#define md 1000000007
#define MP make_pair
ll int n, m;
ll int ar[200005][2], vt[400005][3], i, j, mx, x, y;
void bul(ll int node) {
  if (vt[node][1] >= x and vt[node][2] <= y) {
    mx = max(mx, vt[node][0]);
    return;
  }
  if (vt[node][2] < x or vt[node][1] > y)
    return;
  bul(2 * node);
  bul((2 * node) + 1);
}
void ekle(ll int x, ll int y) {
  if (x > 1)
    ekle(x / 2, y);
  vt[x][0] = max(vt[x][0], y);
  return;
}
int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  cin >> n;
  m = 1;
  while (n > m)
    m *= 2;
  for (i = 0; i < m; i++) {
    vt[i][1] = 2 * m;
    vt[i][2] = m;
  }
  for (i = m; i < 2 * m; i++) {
    for (j = i; j > 0; j /= 2) {
      vt[j][1] = min(vt[j][1], i);
      vt[j][2] = max(vt[j][2], i);
    }
  }
  for (i = m; i < 2 * m; i++)
    vt[i][1] = vt[i][2];
  for (i = 0; i < n; i++)
    cin >> ar[i][0];
  for (i = 0; i < n; i++)
    cin >> ar[i][1];
  for (i = 0; i < n; i++) {
    mx = 0;
    x = m;
    y = m + ar[i][0] - 1;
    bul(1);
    ekle(m + ar[i][0] - 1, mx + ar[i][1]);
  }
  cout << vt[1][0];
  // for(i=0;i<2*m;i++)cout<<vt[i][0]<<" "<<vt[i][1]<<" "<<vt[i][2]<<" "<<endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define de cout << "debuglandınız\n";
#define md 1000000007
#define MP make_pair
ll int n, m;
ll int ar[200005][2], vt[500010][3], i, j, mx, x, y;
void bul(ll int node) {
  if (vt[node][1] >= x and vt[node][2] <= y) {
    mx = max(mx, vt[node][0]);
    return;
  }
  if (vt[node][2] < x or vt[node][1] > y)
    return;
  bul(2 * node);
  bul((2 * node) + 1);
}
void ekle(ll int x, ll int y) {
  if (x > 1)
    ekle(x / 2, y);
  vt[x][0] = max(vt[x][0], y);
  return;
}
int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  cin >> n;
  m = 1;
  while (n > m)
    m *= 2;
  for (i = 0; i < m; i++) {
    vt[i][1] = 2 * m;
    vt[i][2] = m;
  }
  for (i = m; i < 2 * m; i++) {
    for (j = i; j > 0; j /= 2) {
      vt[j][1] = min(vt[j][1], i);
      vt[j][2] = max(vt[j][2], i);
    }
  }
  for (i = m; i < 2 * m; i++)
    vt[i][1] = vt[i][2];
  for (i = 0; i < n; i++)
    cin >> ar[i][0];
  for (i = 0; i < n; i++)
    cin >> ar[i][1];
  for (i = 0; i < n; i++) {
    mx = 0;
    x = m;
    y = m + ar[i][0] - 1;
    bul(1);
    ekle(m + ar[i][0] - 1, mx + ar[i][1]);
  }
  cout << vt[1][0];
  // for(i=0;i<2*m;i++)cout<<vt[i][0]<<" "<<vt[i][1]<<" "<<vt[i][2]<<" "<<endl;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define mid ((l + r) >> 1)
#define lson (x << 1)
#define rson ((x << 1) | 1)
using namespace std;
typedef long long LL;

LL val[400005] = {0};

void pushup(LL x) { val[x] = max(val[lson], val[rson]); }

void modify(LL x, LL l, LL r, LL L, LL R, LL v) {
  if (L > R)
    return;
  if (L <= l && r <= R) {
    val[x] = max(val[x], v);
    return;
  }
  if (L <= mid)
    modify(lson, l, mid, L, R, v);
  if (R > mid)
    modify(rson, mid + 1, r, L, R, v);
  pushup(x);
}

LL query(LL x, LL l, LL r, LL L, LL R) {
  if (L > R)
    return 0;
  if (L <= l && r <= R)
    return val[x];
  LL ret = 0;
  if (L <= mid)
    ret = max(ret, query(lson, l, mid, L, R));
  if (R > mid)
    ret = max(ret, query(rson, mid + 1, r, L, R));
  pushup(x);
  return ret;
}

LL n;
LL h[200005];
LL a[200005];
LL dp[200005] = {0};

int main() {
  scanf("%lld", &n);
  for (LL i = 1; i <= n; i++)
    scanf("%lld", &h[i]);
  for (LL i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  LL ans = 0;
  for (LL i = 1; i <= n; i++) {
    dp[i] = query(1, 1, n, 1, h[i]) + a[i];
    modify(1, 1, n, h[i], h[i], dp[i]);
    ans = max(ans, dp[i]);
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define mid ((l + r) >> 1)
#define lson (x << 1)
#define rson ((x << 1) | 1)
using namespace std;
typedef long long LL;

LL val[800005] = {0};

void pushup(LL x) { val[x] = max(val[lson], val[rson]); }

void modify(LL x, LL l, LL r, LL L, LL R, LL v) {
  if (L > R)
    return;
  if (L <= l && r <= R) {
    val[x] = max(val[x], v);
    return;
  }
  if (L <= mid)
    modify(lson, l, mid, L, R, v);
  if (R > mid)
    modify(rson, mid + 1, r, L, R, v);
  pushup(x);
}

LL query(LL x, LL l, LL r, LL L, LL R) {
  if (L > R)
    return 0;
  if (L <= l && r <= R)
    return val[x];
  LL ret = 0;
  if (L <= mid)
    ret = max(ret, query(lson, l, mid, L, R));
  if (R > mid)
    ret = max(ret, query(rson, mid + 1, r, L, R));
  pushup(x);
  return ret;
}

LL n;
LL h[200005];
LL a[200005];
LL dp[200005] = {0};

int main() {
  scanf("%lld", &n);
  for (LL i = 1; i <= n; i++)
    scanf("%lld", &h[i]);
  for (LL i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  LL ans = 0;
  for (LL i = 1; i <= n; i++) {
    dp[i] = query(1, 1, n, 1, h[i]) + a[i];
    modify(1, 1, n, h[i], h[i], dp[i]);
    ans = max(ans, dp[i]);
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define all(a) (a).begin(), (a).end()
#define X first
#define Y second
#define inf 1e18 + 5
using namespace std;
const int N = 1e5 + 5;
int h[N], a[N];
int seg[4 * N];
void update(int t, int l, int r, int idx, int val) {
  if (l == r) {
    seg[t] = max(seg[t], val);
    return;
  }
  int mid = (l + r) / 2;
  if (idx <= mid)
    update(2 * t + 1, l, mid, idx, val);
  else
    update(2 * t + 2, mid + 1, r, idx, val);
  seg[t] = max(seg[2 * t + 1], seg[2 * t + 2]);
  return;
}
int query(int t, int l, int r, int c, int d) {
  if (r < c || l > d)
    return 0;
  if (l >= c && r <= d)
    return seg[t];
  int mid = (l + r) / 2;
  int q1 = query(2 * t + 1, l, mid, c, d);
  int q2 = query(2 * t + 2, mid + 1, r, c, d);
  return max(q1, q2);
}
void solve() {
  int n, ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> h[i];
    h[i]--;
  }
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    int q = 0;
    if (h[i])
      q = query(0, 0, n - 1, 0, h[i] - 1);
    ans = max(ans, a[i] + q);
    update(0, 0, n - 1, h[i], a[i] + q);
  }
  cout << ans;
  return;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--)
    solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define all(a) (a).begin(), (a).end()
#define X first
#define Y second
#define inf 1e18 + 5
using namespace std;
const int N = 2e5 + 5;
int h[N], a[N];
int seg[4 * N];
void update(int t, int l, int r, int idx, int val) {
  if (l == r) {
    seg[t] = max(seg[t], val);
    return;
  }
  int mid = (l + r) / 2;
  if (idx <= mid)
    update(2 * t + 1, l, mid, idx, val);
  else
    update(2 * t + 2, mid + 1, r, idx, val);
  seg[t] = max(seg[2 * t + 1], seg[2 * t + 2]);
  return;
}
int query(int t, int l, int r, int c, int d) {
  if (r < c || l > d)
    return 0;
  if (l >= c && r <= d)
    return seg[t];
  int mid = (l + r) / 2;
  int q1 = query(2 * t + 1, l, mid, c, d);
  int q2 = query(2 * t + 2, mid + 1, r, c, d);
  return max(q1, q2);
}
void solve() {
  int n, ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> h[i];
    h[i]--;
  }
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    int q = 0;
    if (h[i])
      q = query(0, 0, n - 1, 0, h[i] - 1);
    ans = max(ans, a[i] + q);
    update(0, 0, n - 1, h[i], a[i] + q);
  }
  cout << ans;
  return;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--)
    solve();
  return 0;
}
Problem p03176 (C++)

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

using namespace std;
typedef long long ll;
int n;
ll h[100003], v[100003], f[100003], tree[400003];
pair<ll, pair<ll, int>> a[100003];

void Update(int node, int start, int endd, int id, ll val) {
  if (id < start || id > endd) {
    return;
  }
  if (start == endd) {
    tree[node] = val;
    return;
  }
  int mid = (start + endd) / 2;
  Update(2 * node, start, mid, id, val);
  Update(2 * node + 1, mid + 1, endd, id, val);
  tree[node] = max(tree[2 * node], tree[2 * node + 1]);
}

ll Maxx(int node, int start, int endd, int l, int r) {
  if (r < start || l > endd) {
    return 0;
  }
  if (l <= start && endd <= r) {
    return tree[node];
  }
  int mid = (start + endd) / 2;
  return max(Maxx(2 * node, start, mid, l, r),
             Maxx(2 * node + 1, mid + 1, endd, l, r));
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> h[i];
  }
  for (int i = 1; i <= n; ++i) {
    cin >> v[i];
    a[i] = {h[i], {v[i], -i}};
  }
  sort(a + 1, a + 1 + n);
  for (int i = 1; i <= n; ++i) {
    if (-a[i].second.second == 1) {
      f[i] = a[i].second.first;
    } else {
      f[i] = Maxx(1, 1, n, 1, -a[i].second.second - 1) + a[i].second.first;
    }
    Update(1, 1, n, -a[i].second.second, f[i]);
  }
  ll res = 0;
  for (int i = 1; i <= n; ++i) {
    res = max(res, f[i]);
  }
  cout << res;
}


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

using namespace std;
typedef long long ll;
int n;
ll h[200003], v[200003], f[200003], tree[900003];
pair<ll, pair<ll, int>> a[200003];

void Update(int node, int start, int endd, int id, ll val) {
  if (id < start || id > endd) {
    return;
  }
  if (start == endd) {
    tree[node] = val;
    return;
  }
  int mid = (start + endd) / 2;
  Update(2 * node, start, mid, id, val);
  Update(2 * node + 1, mid + 1, endd, id, val);
  tree[node] = max(tree[2 * node], tree[2 * node + 1]);
}

ll Maxx(int node, int start, int endd, int l, int r) {
  if (r < start || l > endd) {
    return 0;
  }
  if (l <= start && endd <= r) {
    return tree[node];
  }
  int mid = (start + endd) / 2;
  return max(Maxx(2 * node, start, mid, l, r),
             Maxx(2 * node + 1, mid + 1, endd, l, r));
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> h[i];
  }
  for (int i = 1; i <= n; ++i) {
    cin >> v[i];
    a[i] = {h[i], {v[i], -i}};
  }
  sort(a + 1, a + 1 + n);
  for (int i = 1; i <= n; ++i) {
    if (-a[i].second.second == 1) {
      f[i] = a[i].second.first;
    } else {
      f[i] = Maxx(1, 1, n, 1, -a[i].second.second - 1) + a[i].second.first;
    }
    Update(1, 1, n, -a[i].second.second, f[i]);
  }
  ll res = 0;
  for (int i = 1; i <= n; ++i) {
    res = max(res, f[i]);
  }
  cout << res;
}
Problem p03176 (C++)

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

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <vector>

using namespace std;

using ll = long long;

int main() {
  int n;
  scanf("%d", &n);
  int base = 1;
  while (base <= n) {
    base *= 2;
  }
  vector<ll> dp(n + 1), bit(2 * base);

  vector<int> h(n), a(n);

  for (int i = 0; i < n; ++i) {
    scanf("%d", &h[i]);
  }

  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }

  for (int i = 0; i < n; ++i) {
    int tmp = a[i] + h[i];
    ll best = 0;
    while (tmp > 1) {
      if (tmp % 2 == 1) {
        if (bit[tmp - 1] > best) {
          best = bit[tmp - 1];
        }
      }
      tmp /= 2;
    }
    dp[h[i]] = best + a[i];
    for (int j = base + h[i]; j >= 1; j /= 2) {
      if (dp[h[i]] > bit[j]) {
        bit[j] = dp[h[i]];
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i <= n; i++) {
    if (dp[i] > ans) {
      ans = dp[i];
    }
  }
  printf("%lld\n", ans);
}

Fixed submission:
/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <vector>

using namespace std;

using ll = long long;

int main() {
  int n;
  scanf("%d", &n);
  int base = 1;
  while (base <= n) {
    base *= 2;
  }
  vector<ll> dp(n + 1), bit(2 * base);

  vector<int> h(n), a(n);

  for (int i = 0; i < n; ++i) {
    scanf("%d", &h[i]);
  }

  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }

  for (int i = 0; i < n; ++i) {
    int tmp = base + h[i];
    ll best = 0;
    while (tmp > 1) {
      if (tmp % 2 == 1) {
        if (bit[tmp - 1] > best) {
          best = bit[tmp - 1];
        }
      }
      tmp /= 2;
    }
    dp[h[i]] = best + a[i];
    for (int j = base + h[i]; j >= 1; j /= 2) {
      if (dp[h[i]] > bit[j]) {
        bit[j] = dp[h[i]];
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i <= n; i++) {
    if (dp[i] > ans) {
      ans = dp[i];
    }
  }
  printf("%lld\n", ans);
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second

int n, h[100007], x, a[100007];
long long it[400007];

void update(int k, int l, int r, int pos, long long val) {
  if (l > pos || r < pos)
    return;
  if (l == r) {
    it[k] = val;
    return;
  }
  int mid = (l + r) >> 1;
  update(k << 1, l, mid, pos, val);
  update(k << 1 | 1, mid + 1, r, pos, val);
  it[k] = max(it[k << 1], it[k << 1 | 1]);
}

long long get(int k, int l, int r, int L, int R) {
  if (l > R || r < L)
    return 0;
  if (l >= L && r <= R)
    return it[k];
  int mid = (l + r) >> 1;
  return max(get(k << 1, l, mid, L, R), get(k << 1 | 1, mid + 1, r, L, R));
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    h[x] = i;
  }
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  long long val = 0;
  for (int i = 1; i <= n; i++) {
    int id = h[i];
    val = get(1, 1, n, 1, id);
    val += a[id];
    // cout << val << endl;
    update(1, 1, n, id, val);
  }
  cout << it[1];
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second

int n, h[200007], x, a[200007];
long long it[800007];

void update(int k, int l, int r, int pos, long long val) {
  if (l > pos || r < pos)
    return;
  if (l == r) {
    it[k] = val;
    return;
  }
  int mid = (l + r) >> 1;
  update(k << 1, l, mid, pos, val);
  update(k << 1 | 1, mid + 1, r, pos, val);
  it[k] = max(it[k << 1], it[k << 1 | 1]);
}

long long get(int k, int l, int r, int L, int R) {
  if (l > R || r < L)
    return 0;
  if (l >= L && r <= R)
    return it[k];
  int mid = (l + r) >> 1;
  return max(get(k << 1, l, mid, L, R), get(k << 1 | 1, mid + 1, r, L, R));
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    h[x] = i;
  }
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  long long val = 0;
  for (int i = 1; i <= n; i++) {
    int id = h[i];
    val = get(1, 1, n, 1, id);
    val += a[id];
    // cout << val << endl;
    update(1, 1, n, id, val);
  }
  cout << it[1];
}
Problem p03176 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize(2)
#pragma G++ optimize(2)
#include <bits/stdc++.h>
#define ll long long
#define lowbit(x) (x & -x)
using namespace std;
const int N = 1e5 + 7, p = 1e9 + 7;
ll tre[N];
int n, a[N], h[N];
void change(int p, ll v) {
  for (int i = p; i <= n; i += lowbit(i)) {
    tre[i] = max(tre[i], v);
  }
  return;
}
ll query(int p) {
  ll res = 0;
  for (int i = p; i >= 1; i -= lowbit(i)) {
    res = max(res, tre[i]);
  }
  return res;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &h[i]);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 1; i <= n; i++) {
    change(h[i], query(h[i]) + a[i]);
  }
  printf("%lld\n", query(n));
  return 0;
}

Fixed submission:
#pragma GCC optimize(2)
#pragma G++ optimize(2)
#include <bits/stdc++.h>
#define ll long long
#define lowbit(x) (x & -x)
using namespace std;
const int N = 2e5 + 7, p = 1e9 + 7;
ll tre[N];
int n, a[N], h[N];
void change(int p, ll v) {
  for (int i = p; i <= n; i += lowbit(i)) {
    tre[i] = max(tre[i], v);
  }
  return;
}
ll query(int p) {
  ll res = 0;
  for (int i = p; i >= 1; i -= lowbit(i)) {
    res = max(res, tre[i]);
  }
  return res;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &h[i]);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 1; i <= n; i++) {
    change(h[i], query(h[i]) + a[i]);
  }
  printf("%lld\n", query(n));
  return 0;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define M 1000000007LL
#define sz(a) (ll) a.size()
#define rep(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define sep(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--)
#define mll map<ll, ll>
#define vl vector<ll>
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define F first
#define S second
#define pii pair<ll, ll>
#define mod 10000007
#define MAXN 200002
ll dp[200002], h[200002], a[200002];
ll query(ll node, ll st, ll en, ll l, ll r) {

  if (st >= l and en <= r)
    return dp[node];
  if (en < l or st > r)
    return 0;
  ll mid = (st + en) / 2;
  return max(query(2 * node, st, mid, l, r),
             query(2 * node + 1, mid + 1, en, l, r));
}
void upd(ll node, ll st, ll en, ll ind, ll val) {
  if (st == en) {
    dp[node] = val;
    return;
  }
  ll mid = (st + en) / 2;
  if (ind <= mid)
    upd(2 * node, st, mid, ind, val);
  else
    upd(2 * node + 1, mid + 1, en, ind, val);
  dp[node] = max(dp[2 * node], dp[2 * node + 1]);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll n;
  cin >> n;
  for (ll i = 1; i <= n; i++)
    cin >> h[i];
  for (ll i = 1; i <= n; i++)
    cin >> a[i];
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    ll dd = query(1, 1, n, 1, h[i]) + a[i];
    upd(1, 1, n, h[i], dd);
    ans = max(ans, dd);
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define M 1000000007LL
#define sz(a) (ll) a.size()
#define rep(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define sep(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--)
#define mll map<ll, ll>
#define vl vector<ll>
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define F first
#define S second
#define pii pair<ll, ll>
#define mod 10000007
#define MAXN 200002
ll dp[2000002], h[200002], a[200002];
ll query(ll node, ll st, ll en, ll l, ll r) {

  if (st >= l and en <= r)
    return dp[node];
  if (en < l or st > r)
    return 0;
  ll mid = (st + en) / 2;
  return max(query(2 * node, st, mid, l, r),
             query(2 * node + 1, mid + 1, en, l, r));
}
void upd(ll node, ll st, ll en, ll ind, ll val) {
  if (st == en) {
    dp[node] = val;
    return;
  }
  ll mid = (st + en) / 2;
  if (ind <= mid)
    upd(2 * node, st, mid, ind, val);
  else
    upd(2 * node + 1, mid + 1, en, ind, val);
  dp[node] = max(dp[2 * node], dp[2 * node + 1]);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll n;
  cin >> n;
  for (ll i = 1; i <= n; i++)
    cin >> h[i];
  for (ll i = 1; i <= n; i++)
    cin >> a[i];
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    ll dd = query(1, 1, n, 1, h[i]) + a[i];
    upd(1, 1, n, h[i], dd);
    ans = max(ans, dd);
  }
  cout << ans;
  return 0;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MAXN 100100
#define pii pair<int, int>
#define pb push_back
typedef long long ll;

using namespace std;

ll a[MAXN], h[MAXN], n, ans = 0;
ll lo[4 * MAXN + 1], hi[4 * MAXN + 1], tree[4 * MAXN + 1], delta[4 * MAXN + 1];

void init(int i, int l, int r) {
  lo[i] = l;
  hi[i] = r;
  if (l == r)
    return;
  int m = (l + r) / 2;
  init(2 * i, l, m);
  init(2 * i + 1, m + 1, r);
}

void prop(int i) {
  delta[2 * i] += delta[i];
  delta[2 * i + 1] += delta[i];
  delta[i] = 0;
}

void update(int i) {
  tree[i] = max(tree[2 * i] + delta[2 * i], tree[2 * i + 1] + delta[2 * i + 1]);
}

void inc(int i, int l, int r, ll val) {
  if (r < lo[i] || hi[i] < l)
    return;
  if (hi[i] <= r && lo[i] >= l) {
    delta[i] += val;
    return;
  }
  // partial cover case
  prop(i);
  inc(2 * i, l, r, val);
  inc(2 * i + 1, l, r, val);
  update(i);
}

ll query(int i, int l, int r) {
  if (r < lo[i] || hi[i] < l)
    return 0; // not in range
  if (hi[i] <= r && lo[i] >= l)
    return tree[i] + delta[i]; // completely in range
  prop(i);
  ll maxLeft = query(2 * i, l, r);
  ll maxRight = query(2 * i + 1, l, r);
  update(i);
  return max(maxLeft, maxRight);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  init(1, 0, n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    inc(1, h[i], h[i], a[i] + query(1, 0, h[i]));
    ans = max(ans, query(1, 0, h[i]));
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
#define MAXN 200100
#define pii pair<int, int>
#define pb push_back
typedef long long ll;

using namespace std;

ll a[MAXN], h[MAXN], n, ans = 0;
ll lo[4 * MAXN + 1], hi[4 * MAXN + 1], tree[4 * MAXN + 1], delta[4 * MAXN + 1];

void init(int i, int l, int r) {
  lo[i] = l;
  hi[i] = r;
  if (l == r)
    return;
  int m = (l + r) / 2;
  init(2 * i, l, m);
  init(2 * i + 1, m + 1, r);
}

void prop(int i) {
  delta[2 * i] += delta[i];
  delta[2 * i + 1] += delta[i];
  delta[i] = 0;
}

void update(int i) {
  tree[i] = max(tree[2 * i] + delta[2 * i], tree[2 * i + 1] + delta[2 * i + 1]);
}

void inc(int i, int l, int r, ll val) {
  if (r < lo[i] || hi[i] < l)
    return;
  if (hi[i] <= r && lo[i] >= l) {
    delta[i] += val;
    return;
  }
  // partial cover case
  prop(i);
  inc(2 * i, l, r, val);
  inc(2 * i + 1, l, r, val);
  update(i);
}

ll query(int i, int l, int r) {
  if (r < lo[i] || hi[i] < l)
    return 0; // not in range
  if (hi[i] <= r && lo[i] >= l)
    return tree[i] + delta[i]; // completely in range
  prop(i);
  ll maxLeft = query(2 * i, l, r);
  ll maxRight = query(2 * i + 1, l, r);
  update(i);
  return max(maxLeft, maxRight);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  init(1, 0, n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    inc(1, h[i], h[i], a[i] + query(1, 0, h[i]));
    ans = max(ans, query(1, 0, h[i]));
  }
  cout << ans;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define ALL(x) x.begin(), x.end()
#define DBG(x) cout << #x << ":" << x << endl
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;
const ll p = 1e9 + 7;
const ll INF = 1LL << 60;
#define MAX_N 1 << 17
#define int long long
int n, dat[2 * (MAX_N)-1];

// INT＿MAXで初期化
void init(int n_) {
  n = 1;
  while (n < n_)
    n *= 2;
  for (int i = 0; i < 2 * n - 1; i++)
    dat[i] = INT_MAX;
}

// k番目の要素をaに変更
void update(int k, int a) {
  k += n - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}

//[a,b)の最小値を求める
int query(int a, int b, int k = 0, int l = 0, int r = n) {
  if (r <= a || b <= l)
    return INT_MAX;
  if (a <= l && r <= b)
    return dat[k];
  else {
    int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
}

signed main() {
  ll cc, h[222222], c[222222];
  cin >> cc;
  init(cc + 1);
  for (int i = 0; i < cc; i++)
    cin >> h[i];
  for (int i = 0; i < cc; i++) {
    cin >> c[i];
    ll ma = query(0, h[i]);
    if (ma == INT_MAX)
      ma = 0;
    update(h[i], (ma - c[i]));
  }
  cout << -query(0, cc + 1) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define ALL(x) x.begin(), x.end()
#define DBG(x) cout << #x << ":" << x << endl
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;
const ll p = 1e9 + 7;
const ll INF = 1LL << 60;
#define MAX_N 1 << 18
#define int long long
int n, dat[2 * (MAX_N)-1];

// INT＿MAXで初期化
void init(int n_) {
  n = 1;
  while (n < n_)
    n *= 2;
  for (int i = 0; i < 2 * n - 1; i++)
    dat[i] = INT_MAX;
}

// k番目の要素をaに変更
void update(int k, int a) {
  k += n - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}

//[a,b)の最小値を求める
int query(int a, int b, int k = 0, int l = 0, int r = n) {
  if (r <= a || b <= l)
    return INT_MAX;
  if (a <= l && r <= b)
    return dat[k];
  else {
    int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
}

signed main() {
  ll cc, h[222222], c[222222];
  cin >> cc;
  init(cc + 1);
  for (int i = 0; i < cc; i++)
    cin >> h[i];
  for (int i = 0; i < cc; i++) {
    cin >> c[i];
    ll ma = query(0, h[i]);
    if (ma == INT_MAX)
      ma = 0;
    update(h[i], (ma - c[i]));
  }
  cout << -query(0, cc + 1) << endl;
  return 0;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
class kawaii {
#define boi(X) (X & -X)
public:
  kawaii(int N = 0) { poldo.resize(N + 1, 0); };
  long long int qry(int pos) {
    long long int x = -1;
    for (; pos > 0; pos -= boi(pos)) {
      x = max(x, poldo[pos]);
    }
    return x;
  }
  void upd(int pos, long long int val) {
    for (; pos < poldo.size(); pos += boi(pos)) {
      poldo[pos] = max(poldo[pos], val);
    }
  }

private:
  vector<long long int> poldo;
};
int main() {
#ifdef EVAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int N;
  cin >> N;
  kawaii kono(20003);
  long long int h[N], vet[N];
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }
  for (int i = 0; i < N; i++) {
    cin >> vet[i];
  }
  long long int sol = -1;
  for (int i = 0; i < N; i++) {
    kono.upd(h[i], kono.qry(h[i]) + vet[i]);
    sol = max(kono.qry(h[i]), sol);
  }
  cout << sol;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
class kawaii {
#define boi(X) (X & -X)
public:
  kawaii(int N = 0) { poldo.resize(N + 1, 0); };
  long long int qry(int pos) {
    long long int x = -1;
    for (; pos > 0; pos -= boi(pos)) {
      x = max(x, poldo[pos]);
    }
    return x;
  }
  void upd(int pos, long long int val) {
    for (; pos < poldo.size(); pos += boi(pos)) {
      poldo[pos] = max(poldo[pos], val);
    }
  }

private:
  vector<long long int> poldo;
};
int main() {
#ifdef EVAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int N;
  cin >> N;
  kawaii kono(200002);
  long long int h[N], vet[N];
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }
  for (int i = 0; i < N; i++) {
    cin >> vet[i];
  }
  long long int sol = -1;
  for (int i = 0; i < N; i++) {
    kono.upd(h[i], kono.qry(h[i]) + vet[i]);
    sol = max(kono.qry(h[i]), sol);
  }
  cout << sol;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define s second
#define ind(a) scanf("%d", &a)
#define inlld(a) scanf("%lld", &a)
#define ind2(a, b) scanf("%d%d", &a, &b)
#define inlld2(a, b) scanf("%lld%lld", &a, &b)
#define ind3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define inlld3(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)

using namespace std;

const int N = 1e5 + 5;
const int MOD = 1e9 + 7;

typedef long long ll;
typedef long double ld;

ll n, height[N], value[N], bit[N];

void upd(ll x, ll val) {
  for (; x <= n; x += x & -x)
    bit[x] = max(bit[x], val);
}

ll query(ll x) {
  ll ret = 0;
  for (; x >= 1; x -= x & -x)
    ret = max(ret, bit[x]);
  return ret;
}

int main() {
  inlld(n);
  for (ll a = 1; a <= n; a++)
    inlld(height[a]);
  for (ll a = 1; a <= n; a++)
    inlld(value[a]);
  ll ans = 0;
  for (ll a = 1; a <= n; a++) {
    ll maxi_here = query(height[a]);
    ans = max(ans, maxi_here + value[a]);
    upd(height[a], maxi_here + value[a]);
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define s second
#define ind(a) scanf("%d", &a)
#define inlld(a) scanf("%lld", &a)
#define ind2(a, b) scanf("%d%d", &a, &b)
#define inlld2(a, b) scanf("%lld%lld", &a, &b)
#define ind3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define inlld3(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)

using namespace std;

const int N = 2e5 + 5;
const int MOD = 1e9 + 7;

typedef long long ll;
typedef long double ld;

ll n, height[N], value[N], bit[N];

void upd(ll x, ll val) {
  for (; x <= n; x += x & -x)
    bit[x] = max(bit[x], val);
}

ll query(ll x) {
  ll ret = 0;
  for (; x >= 1; x -= x & -x)
    ret = max(ret, bit[x]);
  return ret;
}

int main() {
  inlld(n);
  for (ll a = 1; a <= n; a++)
    inlld(height[a]);
  for (ll a = 1; a <= n; a++)
    inlld(value[a]);
  ll ans = 0;
  for (ll a = 1; a <= n; a++) {
    ll maxi_here = query(height[a]);
    ans = max(ans, maxi_here + value[a]);
    upd(height[a], maxi_here + value[a]);
  }
  printf("%lld\n", ans);
  return 0;
}