Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize "-O3"
#include <bits/stdc++.h>
using namespace std;
#define lo long
const long long lmax = 1999999999;
const long long lmx = 1999999999999999999;
const long long MAXN = 100003;
long long n, t[4 * MAXN];
void build(long long a[], long long v, long long tl, long long tr) {
  if (tl == tr) {
    t[v] = a[tl];
  } else {
    long long tm = (tl + tr) / 2;
    build(a, v * 2, tl, tm);
    build(a, v * 2 + 1, tm + 1, tr);
    t[v] = max(t[v * 2], t[v * 2 + 1]);
  }
}
long long sum(long long v, long long tl, long long tr, long long l,
              long long r) {
  if (l > r)
    return 0;
  if (l == tl && r == tr) {
    return t[v];
  }
  long long tm = (tl + tr) / 2;
  long long summ1 = sum(v * 2, tl, tm, l, min(r, tm));
  long long summ2 = sum(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
  return max(summ1, summ2);
}

void update(long long v, long long tl, long long tr, long long pos,
            long long new_val) {
  if (tl == tr) {
    t[v] = new_val;
  } else {
    long long tm = (tl + tr) / 2;
    if (pos <= tm)
      update(v * 2, tl, tm, pos, new_val);
    else
      update(v * 2 + 1, tm + 1, tr, pos, new_val);
    t[v] = max(t[v * 2], t[v * 2 + 1]);
  }
}
long long h[200003], b[200003];
long long dp[200003], ans;
int main() {
  std::ios::sync_with_stdio(false);
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> h[i];
  }
  for (long long i = 1; i <= n; i++) {
    cin >> b[i];
    ans = max(ans, b[i]);
  }
  for (long long i = 1; i <= n; i++) {
    long long r = sum(1, 0, n + 1, 0, h[i]);
    dp[i] = r + b[i];
    ans = max(dp[i], ans);
    update(1, 0, n + 1, h[i], dp[i]);
  }
  cout << ans << endl;
}
/*

                   *         *
                  * *       * *
                 *   *     *   *
                *     *   *     *
               *       * *       *
               *        *        *
                *               *
                 *             *
                  *           *
                   *         *
                    *       *
                     *     *
                      *   *
                       * *
                        *



*/


Fixed submission:
#pragma GCC optimize "-O3"
#include <bits/stdc++.h>
using namespace std;
#define lo long
const long long lmax = 1999999999;
const long long lmx = 1999999999999999999;
const long long MAXN = 200003;
long long n, t[4 * MAXN];
void build(long long a[], long long v, long long tl, long long tr) {
  if (tl == tr) {
    t[v] = a[tl];
  } else {
    long long tm = (tl + tr) / 2;
    build(a, v * 2, tl, tm);
    build(a, v * 2 + 1, tm + 1, tr);
    t[v] = max(t[v * 2], t[v * 2 + 1]);
  }
}
long long sum(long long v, long long tl, long long tr, long long l,
              long long r) {
  if (l > r)
    return 0;
  if (l == tl && r == tr) {
    return t[v];
  }
  long long tm = (tl + tr) / 2;
  long long summ1 = sum(v * 2, tl, tm, l, min(r, tm));
  long long summ2 = sum(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
  return max(summ1, summ2);
}

void update(long long v, long long tl, long long tr, long long pos,
            long long new_val) {
  if (tl == tr) {
    t[v] = new_val;
  } else {
    long long tm = (tl + tr) / 2;
    if (pos <= tm)
      update(v * 2, tl, tm, pos, new_val);
    else
      update(v * 2 + 1, tm + 1, tr, pos, new_val);
    t[v] = max(t[v * 2], t[v * 2 + 1]);
  }
}
long long h[200003], b[200003];
long long dp[200003], ans;
int main() {
  std::ios::sync_with_stdio(false);
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> h[i];
  }
  for (long long i = 1; i <= n; i++) {
    cin >> b[i];
    ans = max(ans, b[i]);
  }
  for (long long i = 1; i <= n; i++) {
    long long r = sum(1, 0, n + 1, 0, h[i]);
    dp[i] = r + b[i];
    ans = max(dp[i], ans);
    update(1, 0, n + 1, h[i], dp[i]);
  }
  cout << ans << endl;
}
/*

                   *         *
                  * *       * *
                 *   *     *   *
                *     *   *     *
               *       * *       *
               *        *        *
                *               *
                 *             *
                  *           *
                   *         *
                    *       *
                     *     *
                      *   *
                       * *
                        *



*/