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

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

using namespace std;

const int N = 100001;

int d[N], n;

struct BIT {
  long long f[N];

  void update(int pos, long long x) {
    for (; pos <= n; pos += pos & -pos)
      f[pos] = max(f[pos], x);
  }

  long long get(int pos) {
    if (!pos)
      return 0;
    long long res = -1;
    for (; pos; pos -= pos & -pos)
      res = max(res, f[pos]);
    return res;
  }

} bit;

int h[N], a[N], s[N];

int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
  }
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  long long res = -1, cur;
  for (int i = 1; i <= n; i++) {
    cur = bit.get(h[i] - 1);
    cur += a[i];
    res = max(res, cur);
    bit.update(h[i], cur);
  }
  cout << res;
  return 0;
}


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

using namespace std;

const int N = 400001;

int d[N], n;

struct BIT {
  long long f[N];

  void update(int pos, long long x) {
    for (; pos <= n; pos += pos & -pos)
      f[pos] = max(f[pos], x);
  }

  long long get(int pos) {
    if (!pos)
      return 0;
    long long res = -1;
    for (; pos; pos -= pos & -pos)
      res = max(res, f[pos]);
    return res;
  }

} bit;

int h[N], a[N], s[N];

int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
  }
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  long long res = -1, cur;
  for (int i = 1; i <= n; i++) {
    cur = bit.get(h[i] - 1);
    cur += a[i];
    res = max(res, cur);
    bit.update(h[i], cur);
  }
  cout << res;
  return 0;
}
Problem p03176 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#define ll long long
#define MAX 25 * 1e13
#define N 200010

using namespace std;
int MOD = 1000000007;

vector<ll> bitree(N);
int n;
int lowbit(int t) { return t & (-t); }

ll query(int h) {
  ll ret = 0;
  for (int i = h; i; i -= lowbit(i)) {
    ret = max(ret, bitree[i]);
  }
  return ret;
}

void update(int h, ll v) {
  for (int i = h; i <= n; i++) {
    bitree[i] = max(bitree[i], v);
  }
}

int main() {
  cin >> n;
  ll ret = 0;
  vector<int> h(n + 1);
  vector<int> val(n + 1);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> val[i];
  }
  for (int i = 0; i < n; i++) {
    ll m = query(h[i]);
    ret = max(m + val[i], ret);
    update(h[i], m + val[i]);
  }
  cout << ret << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#define ll long long
#define MAX 25 * 1e13
#define N 200010

using namespace std;
int MOD = 1000000007;

vector<ll> bitree(N);
int n;
int lowbit(int t) { return t & (-t); }

ll query(int h) {
  ll ret = 0;
  for (int i = h; i; i -= lowbit(i)) {
    ret = max(ret, bitree[i]);
  }
  return ret;
}

void update(int h, ll v) {
  for (int i = h; i <= n; i += lowbit(i)) {
    bitree[i] = max(bitree[i], v);
  }
}

int main() {
  cin >> n;
  ll ret = 0;
  vector<int> h(n + 1);
  vector<int> val(n + 1);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> val[i];
  }
  for (int i = 0; i < n; i++) {
    ll m = query(h[i]);
    ret = max(m + val[i], ret);
    update(h[i], m + val[i]);
  }
  cout << ret << endl;
  return 0;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
// In the name of Allah
#include <bits/stdc++.h>
using namespace std;
#define MAX 200011
#define mod 1000000007
#define inf 1000000000000000
#define ll long long
#define endl '\n'
ll dp[2 * MAX];
int h[MAX], w[MAX];

void update(int node, int l, int r, int i, ll v) {
  if (l == r) {
    dp[node] = v;
  } else {
    int mid = (l + r) >> 1;
    if (i <= mid)
      update(2 * node + 1, l, mid, i, v);
    else
      update(2 * node + 2, mid + 1, r, i, v);
    dp[node] = max(dp[2 * node + 1], dp[2 * node + 2]);
  }
}
ll get(int node, int l, int r, int x, int y) {
  if (x <= l && r <= y) {
    return dp[node];
  }
  int mid = (l + r) >> 1;
  ll ans = 0;
  if (x <= mid)
    ans = max(ans, get(2 * node + 1, l, mid, x, y));
  if (y >= mid + 1)
    ans = max(ans, get(2 * node + 2, mid + 1, r, x, y));
  return ans;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> h[i];
  for (int i = 0; i < n; i++)
    cin >> w[i];
  update(0, 1, n, h[0], w[0]);
  for (int i = 1; i < n; i++)
    update(0, 1, n, h[i], get(0, 1, n, 1, h[i]) + w[i]);
  cout << dp[0];
  return 0;
}

Fixed submission:
// In the name of Allah
#include <bits/stdc++.h>
using namespace std;
#define MAX 200011
#define mod 1000000007
#define inf 1000000000000000
#define ll long long
#define endl '\n'
ll dp[4 * MAX];
int h[MAX], w[MAX];

void update(int node, int l, int r, int i, ll v) {
  if (l == r) {
    dp[node] = v;
  } else {
    int mid = (l + r) >> 1;
    if (i <= mid)
      update(2 * node + 1, l, mid, i, v);
    else
      update(2 * node + 2, mid + 1, r, i, v);
    dp[node] = max(dp[2 * node + 1], dp[2 * node + 2]);
  }
}
ll get(int node, int l, int r, int x, int y) {
  if (x <= l && r <= y) {
    return dp[node];
  }
  int mid = (l + r) >> 1;
  ll ans = 0;
  if (x <= mid)
    ans = max(ans, get(2 * node + 1, l, mid, x, y));
  if (y >= mid + 1)
    ans = max(ans, get(2 * node + 2, mid + 1, r, x, y));
  return ans;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> h[i];
  for (int i = 0; i < n; i++)
    cin >> w[i];
  update(0, 1, n, h[0], w[0]);
  for (int i = 1; i < n; i++)
    update(0, 1, n, h[i], get(0, 1, n, 1, h[i]) + w[i]);
  cout << dp[0];
  return 0;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <vector>
using namespace std;
const int Cv = 1 << 17, C = 100001;
typedef long long ll;

int n, a[C];
ll val[2 * Cv], x, v, am;

void insert(int a, ll w) {
  for (a += Cv; a > 0; a /= 2)
    val[a] = max(val[a], w);
}

ll query(int l) {
  ll ww = 0;
  for (l += Cv; l > 1; l /= 2)
    if (l % 2 == 1)
      ww = max(ww, val[l - 1]);
  return ww;
}

int main() {
  int i;
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &a[i]);
  for (i = 0; i < n; i++) {
    scanf("%lld", &v);
    x = query(a[i]);
    if (x + v > am)
      am = x + v;
    insert(a[i], x + v);
  }
  printf("%lld\n", am);

  return 0;
}

Fixed submission:
#include <stdio.h>
#include <vector>
using namespace std;
const int Cv = 1 << 18, C = 210001;
typedef long long ll;

int n, a[C];
ll val[2 * Cv], x, v, am;

void insert(int a, ll w) {
  for (a += Cv; a > 0; a /= 2)
    val[a] = max(val[a], w);
}

ll query(int l) {
  ll ww = 0;
  for (l += Cv; l > 1; l /= 2)
    if (l % 2 == 1)
      ww = max(ww, val[l - 1]);
  return ww;
}

int main() {
  int i;
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &a[i]);
  for (i = 0; i < n; i++) {
    scanf("%lld", &v);
    x = query(a[i]);
    if (x + v > am)
      am = x + v;
    insert(a[i], x + v);
  }
  printf("%lld\n", am);

  return 0;
}
Problem p03176 (C++)

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

int n;
int h[200055], a[200055];

typedef pair<int, long long> P;

#define ft first
#define sd second

int main() {
  cin >> n;

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

  set<P> dp;

  dp.insert(P(0, 0));

  for (int i = 0; i < n; i++) {
    auto ite = --dp.lower_bound(P(h[i], 0));
    P p = P(h[i], ite->sd + a[i]);

    ++ite;

    while (ite != dp.end()) {
      if (ite->sd <= p.sd) {
        ite = dp.erase(ite);
      } else {
        ++ite;
      }
    }

    dp.insert(p);
  }

  cout << dp.rbegin()->sd;
}


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

int n;
int h[200055], a[200055];

typedef pair<int, long long> P;

#define ft first
#define sd second

int main() {
  cin >> n;

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

  set<P> dp;

  dp.insert(P(0, 0));

  for (int i = 0; i < n; i++) {
    auto ite = --dp.lower_bound(P(h[i], 0));
    P p = P(h[i], ite->sd + a[i]);

    ++ite;

    while (ite != dp.end()) {
      if (ite->sd <= p.sd) {
        ite = dp.erase(ite);
      } else {
        break;
      }
    }

    dp.insert(p);
  }

  cout << dp.rbegin()->sd;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast", "unroll-loops")
#include <bits/stdc++.h>
#define pb push_back
#define eb emplace_back
#define MP make_pair
#define F first
#define S second
#define MEM(a, b) memset(a, b, sizeof a)
#define Tie ios::sync_with_stdio(0), cin.tie(0);
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll n, w[100001], v[100001], ans, tmp;
map<ll, ll> m;

int main() {
  Tie m[0] = 0;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> w[i];
  for (int i = 0; i < n; i++)
    cin >> v[i];
  for (int i = 0; i < n; i++) {
    auto p = m.upper_bound(w[i]);
    p--;
    ans = max(ans, m[w[i]] = p->S + v[i]);
    tmp = p->S + v[i], p++, p++;
    while (p != m.end() && p->S <= tmp) {
      auto tmp = p++;
      m.erase(tmp);
    }
  }
  cout << ans << '\n';
}

Fixed submission:
#pragma GCC optimize("Ofast", "unroll-loops")
#include <bits/stdc++.h>
#define pb push_back
#define eb emplace_back
#define MP make_pair
#define F first
#define S second
#define MEM(a, b) memset(a, b, sizeof a)
#define Tie ios::sync_with_stdio(0), cin.tie(0);
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll n, w[200001], v[200001], ans, tmp;
map<ll, ll> m;

int main() {
  Tie m[0] = 0;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> w[i];
  for (int i = 0; i < n; i++)
    cin >> v[i];
  for (int i = 0; i < n; i++) {
    auto p = m.upper_bound(w[i]);
    p--;
    ans = max(ans, m[w[i]] = p->S + v[i]);
    tmp = p->S + v[i], p++, p++;
    while (p != m.end() && p->S <= tmp) {
      auto tmp = p++;
      m.erase(tmp);
    }
  }
  cout << ans << '\n';
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long MOD = (long long)(1e9 + 7);
vector<int> graph[(int)(1e5 + 7)];

int main() {
  int n;
  cin >> n;
  vector<long long> h(n), a(n);
  int base = 1;
  while (base <= n)
    base *= 2;

  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  // sorted heights
  vector<long long> tree(base + 1);

  vector<long long> dp((long long)(2e5 + 8), 0);
  // n^2
  // optimise to nlogn
  int pos;
  long long ans = 0;
  dp[0] = a[0];
  for (int i = 0; i < n; i++) {
    long long best = 0;
    int ind = base + h[i];
    // max of dp[i] for i in range (0,h[i]);
    while (ind > 1) {
      if (ind % 2 == 1) {
        best = max(best, tree[ind - 1]);
      }
      ind /= 2;
    }
    dp[h[i]] = best + a[i];
    // update dp[i]
    for (int j = base + h[i]; j >= 1; j /= 2) {
      tree[j] = max(tree[j], dp[h[i]]);
    }
  }

  for (int i = 0; i < n; i++) {
    ans = max(ans, dp[h[i]]);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long MOD = (long long)(1e9 + 7);
vector<int> graph[(int)(1e5 + 7)];

int main() {
  int n;
  cin >> n;
  vector<long long> h(n), a(n);
  int base = 1;
  while (base <= n)
    base *= 2;

  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  // sorted heights
  vector<long long> tree(2 * base);

  vector<long long> dp((long long)(2e5 + 8), 0);
  // n^2
  // optimise to nlogn
  int pos;
  long long ans = 0;
  dp[0] = a[0];
  for (int i = 0; i < n; i++) {
    long long best = 0;
    int ind = base + h[i];
    // max of dp[i] for i in range (0,h[i]);
    while (ind > 1) {
      if (ind % 2 == 1) {
        best = max(best, tree[ind - 1]);
      }
      ind /= 2;
    }
    dp[h[i]] = best + a[i];
    // update dp[i]
    for (int j = base + h[i]; j >= 1; j /= 2) {
      tree[j] = max(tree[j], dp[h[i]]);
    }
  }

  for (int i = 0; i < n; i++) {
    ans = max(ans, dp[h[i]]);
  }
  cout << ans << endl;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll n, h[200005], b[200005], dp[200005];

int main() {
  cin >> n;
  int base = 1;
  while (base <= n)
    base *= 2;
  vector<ll> tree(n * 2, 0);
  for (int i = 0; i < n; i++)
    cin >> h[i];
  for (int i = 0; i < n; i++)
    cin >> b[i];
  for (int i = 0; i < n; i++) {
    ll query = base + h[i];
    ll yet_best = 0;
    while (query > 1) {
      if (query % 2)
        yet_best = max(yet_best, tree[query - 1]);
      query /= 2;
    }
    dp[h[i]] = yet_best + b[i];
    for (int j = base + h[i]; j > 0; j /= 2)
      tree[j] = max(tree[j], dp[h[i]]);
  }
  ll ans = 0;
  for (int i = 0; i <= n; i++)
    ans = max(ans, dp[i]);
  cout << ans << endl;
  return (0);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll n, h[200005], b[200005], dp[200005];

int main() {
  cin >> n;
  int base = 1;
  while (base <= n)
    base *= 2;
  vector<ll> tree(base * 2, 0);
  for (int i = 0; i < n; i++)
    cin >> h[i];
  for (int i = 0; i < n; i++)
    cin >> b[i];
  for (int i = 0; i < n; i++) {
    ll query = base + h[i];
    ll yet_best = 0;
    while (query > 1) {
      if (query % 2)
        yet_best = max(yet_best, tree[query - 1]);
      query /= 2;
    }
    dp[h[i]] = yet_best + b[i];
    for (int j = base + h[i]; j > 0; j /= 2)
      tree[j] = max(tree[j], dp[h[i]]);
  }
  ll ans = 0;
  for (int i = 0; i <= n; i++)
    ans = max(ans, dp[i]);
  cout << ans << endl;
  return (0);
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#define taskname ""

#include <algorithm>
#include <iostream>

using namespace std;
#define long long long

const int N = 1e5 + 10;

int n, h[N], a[N];
long f[N];

#define mid ((l + r) >> 1)
#define lc (id << 1)
#define rc (lc + 1)

struct segmentTree {
  long tree[N << 2];
  void update(int id, int l, int r, int x, long w) {
    if (x < l || r < x)
      return;
    if (l == r) {
      tree[id] = w;
      return;
    }
    update(lc, l, mid, x, w), update(rc, mid + 1, r, x, w);
    tree[id] = max(tree[lc], tree[rc]);
  }

  long get(int id, int l, int r, int x, int y) {
    if (y < l || r < x)
      return 0;
    if (x <= l && r <= y)
      return tree[id];
    return max(get(lc, l, mid, x, y), get(rc, mid + 1, r, x, y));
  }
} seg;

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

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

  for (int i = 1; i <= n; ++i) {
    long maxVal = seg.get(1, 1, n, 0, h[i] - 1);
    f[i] = maxVal + a[i];
    seg.update(1, 1, n, h[i], f[i]);
  }
  cout << seg.tree[1];

  return 0;
}


Fixed submission:
#define taskname ""

#include <algorithm>
#include <iostream>

using namespace std;
#define long long long

const int N = 2e5 + 10;

int n, h[N], a[N];
long f[N];

#define mid ((l + r) >> 1)
#define lc (id << 1)
#define rc (lc + 1)

struct segmentTree {
  long tree[N << 2];
  void update(int id, int l, int r, int x, long w) {
    if (x < l || r < x)
      return;
    if (l == r) {
      tree[id] = w;
      return;
    }
    update(lc, l, mid, x, w), update(rc, mid + 1, r, x, w);
    tree[id] = max(tree[lc], tree[rc]);
  }

  long get(int id, int l, int r, int x, int y) {
    if (y < l || r < x)
      return 0;
    if (x <= l && r <= y)
      return tree[id];
    return max(get(lc, l, mid, x, y), get(rc, mid + 1, r, x, y));
  }
} seg;

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

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

  for (int i = 1; i <= n; ++i) {
    long maxVal = seg.get(1, 1, n, 0, h[i] - 1);
    f[i] = maxVal + a[i];
    seg.update(1, 1, n, h[i], f[i]);
  }
  cout << seg.tree[1];

  return 0;
}
Problem p03176 (C++)

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

const int N = 100050;
typedef long long ll;

ll H[N];
ll B[N];
ll tree[4 * N];
int n;

void update(int nd, int s, int e, int i, ll v) {
  if (s > i || e < i)
    return;
  if (s >= i && e <= i) {
    tree[nd] = max(tree[nd], v);
    return;
  }
  int mid = (s + e) / 2, l = 2 * nd, r = l + 1;
  update(l, s, mid, i, v);
  update(r, mid + 1, e, i, v);
  tree[nd] = max(tree[l], tree[r]);
}

ll query(int nd, int s, int e, int i, int j) {
  if (s > j || e < i)
    return 0;
  if (s >= i && e <= j)
    return tree[nd];
  int mid = (s + e) / 2, l = 2 * nd, r = l + 1;
  return max(query(l, s, mid, i, j), query(r, mid + 1, e, i, j));
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &H[i]);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &B[i]);
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    ll ret = query(1, 1, n, 1, H[i]);
    ans = max(ans, ret + B[i]);
    update(1, 1, n, H[i], ret + B[i]);
  }
  printf("%lld\n", ans);
}


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

const int N = 200050;
typedef long long ll;

ll H[N];
ll B[N];
ll tree[4 * N];
int n;

void update(int nd, int s, int e, int i, ll v) {
  if (s > i || e < i)
    return;
  if (s >= i && e <= i) {
    tree[nd] = max(tree[nd], v);
    return;
  }
  int mid = (s + e) / 2, l = 2 * nd, r = l + 1;
  update(l, s, mid, i, v);
  update(r, mid + 1, e, i, v);
  tree[nd] = max(tree[l], tree[r]);
}

ll query(int nd, int s, int e, int i, int j) {
  if (s > j || e < i)
    return 0;
  if (s >= i && e <= j)
    return tree[nd];
  int mid = (s + e) / 2, l = 2 * nd, r = l + 1;
  return max(query(l, s, mid, i, j), query(r, mid + 1, e, i, j));
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &H[i]);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &B[i]);
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    ll ret = query(1, 1, n, 1, H[i]);
    ans = max(ans, ret + B[i]);
    update(1, 1, n, H[i], ret + B[i]);
  }
  printf("%lld\n", ans);
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define nt _int128
#define mod 1000000007
ll n, arr[200005], brr[200005];
ll dp[200005];
void update(ll i, ll val) {
  while (i <= n) {
    dp[i] = max(dp[i], val);
    i += (i & -i);
  }
}
ll query(ll i) {
  ll max1 = 0;
  while (i > 0) {
    max1 = max(max1, dp[i]);
    i = i - (i & -i);
  }
  return max1;
}
ll func(ll i) {
  ll max1 = 0LL;
  for (ll i = 1; i <= n; i++) {
    ll x = query(arr[i] - 1);
    update(arr[i], brr[i] + x);
    max1 = max(max1, x + brr[i]);
  }
  return max1;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t;
  // cin >> t;
  t = 1;
  while (t--) {
    cin >> n;
    for (ll i = 1; i <= n; i++)
      cin >> arr[i];
    for (ll i = 1; i <= n; i++)
      cin >> brr[i];
    memset(dp, 0, sizeof(dp));
    ll x = func(1);
    cout << x;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define nt _int128
#define mod 1000000007
ll n, arr[200005], brr[200005];
ll dp[200005];
void update(ll i, ll val) {
  while (i <= n) {
    dp[i] = max(dp[i], val);
    i += (i & -i);
  }
}
ll query(ll i) {
  ll max1 = 0;
  while (i > 0) {
    max1 = max(max1, dp[i]);
    i = i - (i & -i);
  }
  return max1;
}
ll func(ll i) {
  ll max1 = 0LL;
  for (ll i = 1; i <= n; i++) {
    ll x = query(arr[i] - 1);
    update(arr[i], brr[i] + x);
    max1 = max(max1, x + brr[i]);
  }
  return max1;
}
int main() {
  // #ifndef ONLINE_JUDGE
  //        freopen("input.txt","r",stdin);
  //     freopen("output.txt","w",stdout);
  // #endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t;
  // cin >> t;
  t = 1;
  while (t--) {
    cin >> n;
    for (ll i = 1; i <= n; i++)
      cin >> arr[i];
    for (ll i = 1; i <= n; i++)
      cin >> brr[i];
    memset(dp, 0, sizeof(dp));
    ll x = func(1);
    cout << x;
  }
  return 0;
}
Problem p03176 (C++)

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

using namespace std;
using ll = long long;

const int N = 1e5 + 5;

struct SegmentTree {
  ll t[N << 2];

  void init(int u, int b, int e) {
    if (b == e) {
      t[u] = 0;
      return;
    }
    int mid = (b + e) >> 1;
    init(u << 1, b, mid);
    init(u << 1 | 1, mid + 1, e);
    t[u] = max(t[u << 1], t[u << 1 | 1]);
  }

  void upd(int u, int b, int e, int i, ll x) {
    if (i < b or e < i)
      return;
    if (i <= b and e <= i) {
      t[u] = x;
      return;
    }
    int mid = (b + e) >> 1;
    upd(u << 1, b, mid, i, x);
    upd(u << 1 | 1, mid + 1, e, i, x);
    t[u] = max(t[u << 1], t[u << 1 | 1]);
  }

  ll query(int u, int b, int e, int i, int j) {
    if (j < b or e < i)
      return 0LL;
    if (i <= b and e <= j)
      return t[u];
    int mid = (b + e) >> 1;
    ll p1 = query(u << 1, b, mid, i, j);
    ll p2 = query(u << 1 | 1, mid + 1, e, i, j);
    return max(p1, p2);
  }
} t;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> h(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
  }
  vector<ll> dp(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    cin >> dp[i];
  }
  for (int i = 1; i <= n; i++) {
    if (h[i] == 1) {
      t.upd(1, 1, n, h[i], dp[i]);
      continue;
    }
    dp[i] += 1LL * t.query(1, 1, n, 1, h[i] - 1);
    t.upd(1, 1, n, h[i], dp[i]);
  }
  cout << *max_element(dp.begin(), dp.end()) << '\n';
}


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

using namespace std;
using ll = long long;

const int N = 2e5 + 5;

struct SegmentTree {
  ll t[N << 2];

  void init(int u, int b, int e) {
    if (b == e) {
      t[u] = 0;
      return;
    }
    int mid = (b + e) >> 1;
    init(u << 1, b, mid);
    init(u << 1 | 1, mid + 1, e);
    t[u] = max(t[u << 1], t[u << 1 | 1]);
  }

  void upd(int u, int b, int e, int i, ll x) {
    if (i < b or e < i)
      return;
    if (i <= b and e <= i) {
      t[u] = x;
      return;
    }
    int mid = (b + e) >> 1;
    upd(u << 1, b, mid, i, x);
    upd(u << 1 | 1, mid + 1, e, i, x);
    t[u] = max(t[u << 1], t[u << 1 | 1]);
  }

  ll query(int u, int b, int e, int i, int j) {
    if (j < b or e < i)
      return 0LL;
    if (i <= b and e <= j)
      return t[u];
    int mid = (b + e) >> 1;
    ll p1 = query(u << 1, b, mid, i, j);
    ll p2 = query(u << 1 | 1, mid + 1, e, i, j);
    return max(p1, p2);
  }
} t;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> h(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
  }
  vector<ll> dp(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    cin >> dp[i];
  }
  for (int i = 1; i <= n; i++) {
    if (h[i] == 1) {
      t.upd(1, 1, n, h[i], dp[i]);
      continue;
    }
    dp[i] += 1LL * t.query(1, 1, n, 1, h[i] - 1);
    t.upd(1, 1, n, h[i], dp[i]);
  }
  cout << *max_element(dp.begin(), dp.end()) << '\n';
}
Problem p03176 (C++)

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

using namespace std;
using ll = long long;
using ld = long double;

const int max_n = 100500;
int d = 1;

ll t[max_n << 2];

void update(int i, ll x, int tl = 0, int tr = d - 1, int v = 1) {
  if (tl == tr) {
    t[v] = x;
    return;
  }
  int tm = (tl + tr) >> 1;
  if (i <= tm)
    update(i, x, tl, tm, v << 1);
  else
    update(i, x, tm + 1, tr, v << 1 | 1);
  t[v] = max(t[v << 1], t[v << 1 | 1]);
}

ll get(int l, int r, int tl = 0, int tr = d - 1, int v = 1) {
  if (l == tl && r == tr) {
    return t[v];
  }
  int tm = (tl + tr) >> 1;
  ll ans = 0;
  if (l <= tm) {
    ans = max(ans, get(l, min(r, tm), tl, tm, v << 1));
  }
  if (r > tm) {
    ans = max(ans, get(max(l, tm + 1), r, tm + 1, tr, v << 1 | 1));
  }
  return ans;
}

int main() {
  ios_base::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);
  cout.precision(20);
  cout << fixed;
  int n;
  cin >> n;
  while (d <= n)
    d <<= 1;
  vector<ll> h(n), a(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    update(h[i], get(0, h[i] - 1) + a[i]);
  }
  cout << get(0, n);
}


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

using namespace std;
using ll = long long;
using ld = long double;

const int max_n = 200500;
int d = 1;

ll t[max_n << 2];

void update(int i, ll x, int tl = 0, int tr = d - 1, int v = 1) {
  if (tl == tr) {
    t[v] = x;
    return;
  }
  int tm = (tl + tr) >> 1;
  if (i <= tm)
    update(i, x, tl, tm, v << 1);
  else
    update(i, x, tm + 1, tr, v << 1 | 1);
  t[v] = max(t[v << 1], t[v << 1 | 1]);
}

ll get(int l, int r, int tl = 0, int tr = d - 1, int v = 1) {
  if (l == tl && r == tr) {
    return t[v];
  }
  int tm = (tl + tr) >> 1;
  ll ans = 0;
  if (l <= tm) {
    ans = max(ans, get(l, min(r, tm), tl, tm, v << 1));
  }
  if (r > tm) {
    ans = max(ans, get(max(l, tm + 1), r, tm + 1, tr, v << 1 | 1));
  }
  return ans;
}

int main() {
  ios_base::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);
  cout.precision(20);
  cout << fixed;
  int n;
  cin >> n;
  while (d <= n)
    d <<= 1;
  vector<ll> h(n), a(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    update(h[i], get(0, h[i] - 1) + a[i]);
  }
  cout << get(0, n);
}
Problem p03176 (C++)

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

#define fi first
#define se second

const int N = 100100;
const long long mod = 1e9 + 7;

using namespace std;

int n;
int a[N];
int b[N];
long long d[N];

void upd(int x, long long y) {
  while (x < N) {
    d[x] = max(d[x], y);
    x += x & -x;
  }
}

long long get(int x) {
  long long res = 0;
  while (x > 0) {
    res = max(res, d[x]);
    x -= x & -x;
  }
  return res;
}

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(0);

  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
  }
  long long res = 0;
  for (int i = 1; i <= n; i++) {
    long long now = get(a[i]) + b[i];
    res = max(res, now);
    upd(a[i], now);
  }
  cout << res << "\n";
}


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

#define fi first
#define se second

const int N = 200200;
const long long mod = 1e9 + 7;

using namespace std;

int n;
int a[N];
int b[N];
long long d[N];

void upd(int x, long long y) {
  while (x < N) {
    d[x] = max(d[x], y);
    x += x & -x;
  }
}

long long get(int x) {
  long long res = 0;
  while (x > 0) {
    res = max(res, d[x]);
    x -= x & -x;
  }
  return res;
}

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(0);

  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
  }
  long long res = 0;
  for (int i = 1; i <= n; i++) {
    long long now = get(a[i]) + b[i];
    res = max(res, now);
    upd(a[i], now);
  }
  cout << res << "\n";
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<ll, ll> pii;
typedef pair<ld, ld> pld;
ll n;
ll h[100009];
ll a[100009];
ll bit[100009];
void upd(ll idx, ll val) {
  ++idx;
  while (idx <= n + 1) {
    bit[idx] = max(bit[idx], val);
    idx += idx & (-idx);
  }
}
ll get(ll idx) {
  ++idx;
  ll ret = 0;
  while (idx > 0) {
    ret = max(bit[idx], ret);
    idx -= idx & (-idx);
  }
  return ret;
}
int main() {

  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);

  cin >> n;
  for (ll i = 0; i < n; ++i)
    cin >> h[i];
  for (ll i = 0; i < n; ++i)
    cin >> a[i];
  for (ll i = 0; i < n; ++i) {
    upd(h[i], a[i] + get(h[i] - 1));
  }
  cout << get(n) << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<ll, ll> pii;
typedef pair<ld, ld> pld;
ll n;
ll h[200009];
ll a[200009];
ll bit[200009];
void upd(ll idx, ll val) {
  ++idx;
  while (idx <= n + 1) {
    bit[idx] = max(bit[idx], val);
    idx += idx & (-idx);
  }
}
ll get(ll idx) {
  ++idx;
  ll ret = 0;
  while (idx > 0) {
    ret = max(bit[idx], ret);
    idx -= idx & (-idx);
  }
  return ret;
}
int main() {

  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);

  cin >> n;
  for (ll i = 0; i < n; ++i)
    cin >> h[i];
  for (ll i = 0; i < n; ++i)
    cin >> a[i];
  for (ll i = 0; i < n; ++i) {
    upd(h[i], a[i] + get(h[i] - 1));
  }
  cout << get(n) << "\n";
}
Problem p03176 (C++)

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

typedef long long ll;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  ll n;
  cin >> n;
  vector<ll> h(n);
  for (int i = 0; i < n; ++i)
    cin >> h[i];
  vector<ll> a(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  ll res = 0;
  set<pair<ll, ll>> st;
  st.insert({0ll, 0ll});
  for (int i = 0; i < n; ++i) {
    auto it = prev(st.lower_bound({h[i], 0}));
    ll cost = a[i] + (*it).second;
    it++;
    while (it != st.end()) {
      auto nxt = next(it);
      if ((*it).second <= cost) {
        st.erase(it);
      }
      it = nxt;
    }
    st.insert({h[i], cost});
    res = max(res, cost);
  }

  cout << res << endl;
  return 0;
}

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

typedef long long ll;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  ll n;
  cin >> n;
  vector<ll> h(n);
  for (int i = 0; i < n; ++i)
    cin >> h[i];
  vector<ll> a(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  ll res = 0;
  set<pair<ll, ll>> st;
  st.insert({0ll, 0ll});
  for (int i = 0; i < n; ++i) {
    auto it = prev(st.lower_bound({h[i], 0}));
    ll cost = a[i] + (*it).second;
    it++;
    while (it != st.end()) {
      auto nxt = next(it);
      if ((*it).second <= cost) {
        st.erase(it);
      } else {
        break;
      }
      it = nxt;
    }
    st.insert({h[i], cost});
    res = max(res, cost);
  }

  cout << res << endl;
  return 0;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <forward_list>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

const ll MOD = 1E+09 + 7;
const ll INF = 1E18;
const int MAX_N = 1E+05;

int N;

int H[MAX_N + 1];

ll dp[MAX_N + 1], A[MAX_N + 1];

ll getmax(int i);
void setmax(int i, ll x);

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

  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++) {
    setmax(H[i], getmax(H[i] - 1) + A[i]);
  }

  cout << getmax(N) << "\n";

  return 0;
}

ll getmax(int i) {
  ll s = 0;
  while (i > 0) {
    s = max(s, dp[i]);
    i -= i & -i;
  }
  return s;
}

void setmax(int i, ll x) {
  while (i <= N) {
    dp[i] = max(dp[i], x);
    i += i & -i;
  }
}

Fixed submission:
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <forward_list>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

const ll MOD = 1E+09 + 7;
const ll INF = 1E18;
const int MAX_N = 2E+05;

int N;

int H[MAX_N + 1];

ll dp[MAX_N + 1], A[MAX_N + 1];

ll getmax(int i);
void setmax(int i, ll x);

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

  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++) {
    setmax(H[i], getmax(H[i] - 1) + A[i]);
  }

  cout << getmax(N) << "\n";

  return 0;
}

ll getmax(int i) {
  ll s = 0;
  while (i > 0) {
    s = max(s, dp[i]);
    i -= i & -i;
  }
  return s;
}

void setmax(int i, ll x) {
  while (i <= N) {
    dp[i] = max(dp[i], x);
    i += i & -i;
  }
}
Problem p03176 (C++)

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

using namespace std;

int main() {
  int N;
  scanf("%d", &N);
  vector<int> H(N), A(N);
  vector<long long> dp(N, 0);
  int base = 1;
  while (base <= N) {
    base <<= 1;
  }
  vector<long long> tree(2 * base, 0);
  for (int i = 0; i < N; ++i) {
    scanf("%d", &H[i]);
  }
  for (int i = 0; i < N; ++i) {
    scanf("%d", &A[i]);
    dp[i] = A[i];
  }

  for (int i = 0; i < N; ++i) {
    int x = base + H[i];
    long long best = 0;
    while (x > 1) {
      if (x & 1) {
        best = max(best, tree[x - 1]);
      }
      x >>= 1;
    }
    dp[H[i]] = best + A[i];
    for (int k = base + H[i]; k >= 1; k >>= 1) {
      tree[k] = max(tree[k], dp[H[i]]);
    }
  }
  cout << tree[1];
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N;
  scanf("%d", &N);
  vector<int> H(N), A(N);
  vector<long long> dp(2 * N, 0);
  int base = 1;
  while (base <= N) {
    base <<= 1;
  }
  vector<long long> tree(2 * base, 0);
  for (int i = 0; i < N; ++i) {
    scanf("%d", &H[i]);
  }
  for (int i = 0; i < N; ++i) {
    scanf("%d", &A[i]);
    dp[i] = A[i];
  }

  for (int i = 0; i < N; ++i) {
    int x = base + H[i];
    long long best = 0;
    while (x > 1) {
      if (x & 1) {
        best = max(best, tree[x - 1]);
      }
      x >>= 1;
    }
    dp[H[i]] = best + A[i];
    for (int k = base + H[i]; k >= 1; k >>= 1) {
      tree[k] = max(tree[k], dp[H[i]]);
    }
  }
  cout << tree[1];
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
typedef pair<int, int> ii;
typedef long long LL;
#define pb push_back
const int INF = 2147483647;
const int N = 100005;
const int MOD = 1000000007;

int h[N], a[N], i, n, M;
LL w[3 * N];

void insert(int v, LL b) {
  int s = M + v;
  while (s) {
    w[s] = max(w[s], b);
    s /= 2;
  }
}

LL query(int a, int b) {
  int va = M + a, vb = M + b;
  LL wyn = w[va];
  if (va != vb)
    wyn = max(wyn, w[vb]);
  while (va / 2 != vb / 2) {
    if (va % 2 == 0)
      wyn = max(wyn, w[va + 1]);
    if (vb % 2 == 1)
      wyn = max(wyn, w[vb - 1]);
    va /= 2;
    vb /= 2;
  }
  return wyn;
}

void init(int a) {
  M = 1;
  while (M < a)
    M *= 2;
  for (int i = 1; i < 2 * M; i++)
    w[i] = 0;
  M--;
}

int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    scanf("%d", &h[i]);
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  init(n);
  for (i = 1; i <= n; i++) {
    if (h[i] == 1)
      insert(h[i], a[i]);
    else
      insert(h[i], query(1, h[i] - 1) + a[i]);
  }
  printf("%lld\n", query(1, n));
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
typedef pair<int, int> ii;
typedef long long LL;
#define pb push_back
const int INF = 2147483647;
const int N = 200005;
const int MOD = 1000000007;

int h[N], a[N], i, n, M;
LL w[3 * N];

void insert(int v, LL b) {
  int s = M + v;
  while (s) {
    w[s] = max(w[s], b);
    s /= 2;
  }
}

LL query(int a, int b) {
  int va = M + a, vb = M + b;
  LL wyn = w[va];
  if (va != vb)
    wyn = max(wyn, w[vb]);
  while (va / 2 != vb / 2) {
    if (va % 2 == 0)
      wyn = max(wyn, w[va + 1]);
    if (vb % 2 == 1)
      wyn = max(wyn, w[vb - 1]);
    va /= 2;
    vb /= 2;
  }
  return wyn;
}

void init(int a) {
  M = 1;
  while (M < a)
    M *= 2;
  for (int i = 1; i < 2 * M; i++)
    w[i] = 0;
  M--;
}

int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    scanf("%d", &h[i]);
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  init(n);
  for (i = 1; i <= n; i++) {
    if (h[i] == 1)
      insert(h[i], a[i]);
    else
      insert(h[i], query(1, h[i] - 1) + a[i]);
  }
  printf("%lld\n", query(1, n));
  return 0;
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb emplace_back
#define fi first
#define se second
#define int long long
using namespace std;
mt19937_64
    rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int l) {
  uniform_int_distribution<int> uid(0, l - 1);
  return uid(rang);
}
const int MAXN = 3e5;
int seg[MAXN];
int n;

void update(int i, int val) {
  for (seg[i += n] = val; i > 1; i >>= 1)
    seg[i >> 1] = max(seg[i], seg[i ^ 1]);
}

int query(int l, int r) {
  r++;
  int a = 0;
  for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
    if (r & 1)
      a = max(a, seg[--r]);
    if (l & 1)
      a = max(seg[l++], a);
  }
  return a;
}

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.precision(10);
  srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  cin >> n;
  vector<int> h(n);
  for (int o = 0; o < n; o++) {
    int y;
    cin >> y;
    h[y - 1] = o;
  }
  vector<int> a(n);
  for (int o = 0; o < n; o++) {
    cin >> a[o];
  }
  for (int i = 0; i < n; ++i)
    update(h[i], query(0, h[i]) + a[h[i]]);
  cout << query(0, n - 1);
}

Fixed submission:
#include <bits/stdc++.h>
#define pb emplace_back
#define fi first
#define se second
#define int long long
using namespace std;
mt19937_64
    rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int l) {
  uniform_int_distribution<int> uid(0, l - 1);
  return uid(rang);
}
const int MAXN = 5e5;
int seg[MAXN];
int n;

void update(int i, int val) {
  for (seg[i += n] = val; i > 1; i >>= 1)
    seg[i >> 1] = max(seg[i], seg[i ^ 1]);
}

int query(int l, int r) {
  r++;
  int a = 0;
  for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
    if (r & 1)
      a = max(a, seg[--r]);
    if (l & 1)
      a = max(seg[l++], a);
  }
  return a;
}

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.precision(10);
  srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  cin >> n;
  vector<int> h(n);
  for (int o = 0; o < n; o++) {
    int y;
    cin >> y;
    h[y - 1] = o;
  }
  vector<int> a(n);
  for (int o = 0; o < n; o++) {
    cin >> a[o];
  }
  for (int i = 0; i < n; ++i)
    update(h[i], query(0, h[i]) + a[h[i]]);
  cout << query(0, n - 1);
}
Problem p03176 (Python)

Buggy submission (status: Runtime Error):
def get(i):
    mx = 0
    while i > 0:
        if bit[i] > mx:
            mx = bit[i]
        i -= i & -i
    return mx


def update(i, x):
    while i < n + 1:
        if x > bit[i]:
            bit[i] = x
        i += i & -i


def f(hs, a_s):
    for h, a in zip(hs, a_s):
        update(h, get(h - 1) + a)
    print(max(bit))


n = int(input())
hs = list(map(int, input().split()))
a_s = list(map(int, input().split()))
bit = [0] * (n + 1)
f(n, hs, a_s)


Fixed submission:
def get(i):
    mx = 0
    while i > 0:
        if bit[i] > mx:
            mx = bit[i]
        i -= i & -i
    return mx


def update(i, x):
    while i < n + 1:
        if x > bit[i]:
            bit[i] = x
        i += i & -i


def f(hs, a_s):
    for h, a in zip(hs, a_s):
        update(h, get(h - 1) + a)
    print(max(bit))


n = int(input())
hs = list(map(int, input().split()))
a_s = list(map(int, input().split()))
bit = [0] * (n + 1)
f(hs, a_s)
Problem p03176 (C++)

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

#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif

const int N = 1e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;

int t[4 * N], h[N], b[N];

void upd(int nd, int s, int e, int id, int v) {
  if (s == e) {
    t[nd] = v;
    return;
  }
  int md = (s + e) >> 1;
  if (id <= md)
    upd(2 * nd, s, md, id, v);
  else
    upd(2 * nd + 1, md + 1, e, id, v);
  t[nd] = max(t[2 * nd], t[2 * nd + 1]);
}

int query(int nd, int s, int e, int l, int r) {
  if (s > e || s > r || e < l)
    return 0;
  if (s >= l && e <= r)
    return t[nd];
  int md = (s + e) >> 1;
  return max(query(2 * nd, s, md, l, r), query(2 * nd + 1, md + 1, e, l, r));
}

signed main() {
  IOS;
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n, ans = 0;
  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++) {
    int p = query(1, 1, n, 1, h[i]);
    ans = max(ans, p + b[i]);
    upd(1, 1, n, h[i], p + b[i]);
  }
  cout << ans;
  return 0;
}

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

#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif

const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;

int t[4 * N], h[N], b[N];

void upd(int nd, int s, int e, int id, int v) {
  if (s == e) {
    t[nd] = v;
    return;
  }
  int md = (s + e) >> 1;
  if (id <= md)
    upd(2 * nd, s, md, id, v);
  else
    upd(2 * nd + 1, md + 1, e, id, v);
  t[nd] = max(t[2 * nd], t[2 * nd + 1]);
}

int query(int nd, int s, int e, int l, int r) {
  if (s > e || s > r || e < l)
    return 0;
  if (s >= l && e <= r)
    return t[nd];
  int md = (s + e) >> 1;
  return max(query(2 * nd, s, md, l, r), query(2 * nd + 1, md + 1, e, l, r));
}

signed main() {
  IOS;
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n, ans = 0;
  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++) {
    int p = query(1, 1, n, 1, h[i]);
    ans = max(ans, p + b[i]);
    upd(1, 1, n, h[i], p + b[i]);
  }
  cout << ans;
  return 0;
}
Problem p03176 (C++)

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

using namespace std;

const int MAXN = 200005;

vector<long long> st(MAXN);

long long update(int l, int r, int v, int i, long long val) {
  if (l > i || r < i)
    return st[v];
  if (l == r)
    return st[v] = max(val, st[v]);
  int m = l + r >> 1;
  return st[v] = max(update(l, m, v << 1, i, val),
                     update(m + 1, r, v << 1 | 1, i, val));
}

long long query(int l, int r, int v, int li, int ri) {
  if (l > ri || r < li)
    return 0;
  if (l >= li && r <= ri)
    return st[v];
  int m = l + r >> 1;
  return max(query(l, m, v << 1, li, ri), query(m + 1, r, v << 1 | 1, li, ri));
}

int main() {
  int n;
  scanf("%i", &n);
  vector<int> h(n + 1), a(n + 1);
  for (int x = 1; x <= n; x++)
    scanf("%i", &h[x]);
  for (int x = 1; x <= n; x++)
    scanf("%i", &a[x]);
  for (int x = 1; x <= n; x++)
    update(1, n, 1, h[x], query(1, n, 1, 1, h[x] - 1) + a[x]);
  printf("%lli\n", st[1]);
}


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

using namespace std;

const int MAXN = 200005;

vector<long long> st(MAXN << 2);

long long update(int l, int r, int v, int i, long long val) {
  if (l > i || r < i)
    return st[v];
  if (l == r)
    return st[v] = max(val, st[v]);
  int m = l + r >> 1;
  return st[v] = max(update(l, m, v << 1, i, val),
                     update(m + 1, r, v << 1 | 1, i, val));
}

long long query(int l, int r, int v, int li, int ri) {
  if (l > ri || r < li)
    return 0;
  if (l >= li && r <= ri)
    return st[v];
  int m = l + r >> 1;
  return max(query(l, m, v << 1, li, ri), query(m + 1, r, v << 1 | 1, li, ri));
}

int main() {
  int n;
  scanf("%i", &n);
  vector<int> h(n + 1), a(n + 1);
  for (int x = 1; x <= n; x++)
    scanf("%i", &h[x]);
  for (int x = 1; x <= n; x++)
    scanf("%i", &a[x]);
  for (int x = 1; x <= n; x++)
    update(1, n, 1, h[x], query(1, n, 1, 1, h[x] - 1) + a[x]);
  printf("%lli\n", st[1]);
}
Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mod 1000000007;
#define inf (1LL << 60)
using namespace std;

struct flower {
  ll hi;
  ll bty;
};

ll solve(vector<flower> &v, int n) {
  vector<ll> dp(n + 1);
  map<ll, ll> m;
  dp[1] = v[1].bty;
  m[v[1].hi] = dp[1];
  ll ans = dp[1];
  for (int i = 2; i <= n; i++) {
    dp[i] = v[i].bty;
    auto it = m.lower_bound(v[i].hi + 1);
    if (it != m.begin()) {
      it--;
      dp[i] += it->second;
    }
    m[v[i].hi] = dp[i];
    it = m.upper_bound(v[i].hi);
    while (it->second <= dp[i]) {
      auto temp = it;
      temp++;
      m.erase(it);
      it = temp;
    }
    ans = max(ans, dp[i]);
  }
  return ans;
}
int main() {
  int n;
  cin >> n;
  vector<flower> v(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> v[i].hi;
  for (int i = 1; i <= n; i++)
    cin >> v[i].bty;
  cout << solve(v, n);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mod 1000000007;
#define inf (1LL << 60)
using namespace std;

struct flower {
  ll hi;
  ll bty;
};

ll solve(vector<flower> &v, int n) {
  vector<ll> dp(n + 1);
  map<ll, ll> m;
  dp[1] = v[1].bty;
  m[v[1].hi] = dp[1];
  ll ans = dp[1];
  for (int i = 2; i <= n; i++) {
    dp[i] = v[i].bty;
    auto it = m.lower_bound(v[i].hi + 1);
    if (it != m.begin()) {
      it--;
      dp[i] += it->second;
    }
    m[v[i].hi] = dp[i];
    it = m.upper_bound(v[i].hi);
    while (it != m.end() && it->second <= dp[i]) {
      auto temp = it;
      temp++;
      m.erase(it);
      it = temp;
    }
    ans = max(ans, dp[i]);
  }
  return ans;
}
int main() {
  int n;
  cin >> n;
  vector<flower> v(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> v[i].hi;
  for (int i = 1; i <= n; i++)
    cin >> v[i].bty;
  cout << solve(v, n);
  return 0;
}