Problem p03176 (C++)

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

#define debug(x) cerr << #x << ": " << x << '\n'
#define debugArray(x, n)                                                       \
  for (long long hoge = 0; (hoge) < (n); ++(hoge))                             \
  cerr << #x << "[" << hoge << "]: " << x[hoge] << '\n'

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> Pll;
typedef vector<ll> vll;
const ll INF = LLONG_MAX / 10;
const ll MOD = 1e9 + 7;

template <typename T> struct SegmentTree {
  using F = function<T(T, T)>;
  int n;
  F f;
  T ti;
  vector<T> dat;
  SegmentTree(){};
  SegmentTree(F f, T ti) : f(f), ti(ti) {}
  void init(int n_) {
    n = 1;
    while (n < n_)
      n <<= 1;
    dat.assign(n << 1, ti);
  }
  void build(const vector<T> &v) {
    int n_ = v.size();
    init(n_);
    for (int i = 0; i < n_; i++)
      dat[n + i] = v[i];
    for (int i = n - 1; i; i--)
      dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]);
  }
  void set_val(int k, T x) {
    dat[k += n] = x;
    while (k >>= 1)
      dat[k] = f(dat[(k << 1) | 0], dat[(k << 1) | 1]);
  }
  //[a,b)
  T query(int a, int b) {
    T vl = ti, vr = ti;
    for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        vl = f(vl, dat[l++]);
      if (r & 1)
        vr = f(dat[--r], vr);
    }
    return f(vl, vr);
  }
};

ll dp[20010];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  cin >> N;
  vll h(N);
  for (ll i = 0; i < N; i++) {
    cin >> h[i];
    h[i]--;
  }
  vll A(N);
  for (ll i = 0; i < N; i++)
    cin >> A[i];
  SegmentTree<ll> seg([](ll a, ll b) { return max(a, b); }, 0ll);
  seg.init(N);
  for (ll i = 0; i < N; i++) {
    dp[i] = seg.query(0, h[i] + 1) + A[i];
    seg.set_val(h[i], dp[i]);
  }
  cout << seg.query(0, N) << endl;
  return 0;
}


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

#define debug(x) cerr << #x << ": " << x << '\n'
#define debugArray(x, n)                                                       \
  for (long long hoge = 0; (hoge) < (n); ++(hoge))                             \
  cerr << #x << "[" << hoge << "]: " << x[hoge] << '\n'

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> Pll;
typedef vector<ll> vll;
const ll INF = LLONG_MAX / 10;
const ll MOD = 1e9 + 7;

template <typename T> struct SegmentTree {
  using F = function<T(T, T)>;
  int n;
  F f;
  T ti;
  vector<T> dat;
  SegmentTree(){};
  SegmentTree(F f, T ti) : f(f), ti(ti) {}
  void init(int n_) {
    n = 1;
    while (n < n_)
      n <<= 1;
    dat.assign(n << 1, ti);
  }
  void build(const vector<T> &v) {
    int n_ = v.size();
    init(n_);
    for (int i = 0; i < n_; i++)
      dat[n + i] = v[i];
    for (int i = n - 1; i; i--)
      dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]);
  }
  void set_val(int k, T x) {
    dat[k += n] = x;
    while (k >>= 1)
      dat[k] = f(dat[(k << 1) | 0], dat[(k << 1) | 1]);
  }
  //[a,b)
  T query(int a, int b) {
    T vl = ti, vr = ti;
    for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        vl = f(vl, dat[l++]);
      if (r & 1)
        vr = f(dat[--r], vr);
    }
    return f(vl, vr);
  }
};

ll dp[200010];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  cin >> N;
  vll h(N);
  for (ll i = 0; i < N; i++) {
    cin >> h[i];
    h[i]--;
  }
  vll A(N);
  for (ll i = 0; i < N; i++)
    cin >> A[i];
  SegmentTree<ll> seg([](ll a, ll b) { return max(a, b); }, 0ll);
  seg.init(N);
  for (ll i = 0; i < N; i++) {
    dp[i] = seg.query(0, h[i] + 1) + A[i];
    seg.set_val(h[i], dp[i]);
  }
  cout << seg.query(0, N) << endl;
  return 0;
}