Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define M 1000000007
#define mod 998244353
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define pi pair<ll, ll>
using namespace std;
const ll N = 100010;

ll h[N], a[N];

ll sgt[4 * N];

void build(ll st, ll en, ll pos) {
  if (st == en) {
    sgt[pos] = 0;
    return;
  }
  ll mid = (st + en) / 2;
  build(st, mid, 2 * pos + 1);
  build(mid + 1, en, 2 * pos + 2);
  sgt[pos] = max(sgt[2 * pos + 1], sgt[2 * pos + 2]);
}

void update(ll st, ll en, ll pos, ll idx, ll val) {
  if (st == en) {
    sgt[pos] = val;
    return;
  }
  ll mid = (st + en) / 2;
  if (idx >= st && idx <= mid) {
    update(st, mid, 2 * pos + 1, idx, val);
  } else {
    update(mid + 1, en, 2 * pos + 2, idx, val);
  }
  sgt[pos] = max(sgt[2 * pos + 1], sgt[2 * pos + 2]);
}

ll query(ll st, ll en, ll pos, ll l, ll r) {
  if (st > r || en < l) {
    return 0;
  }
  if (st >= l && en <= r) {
    return sgt[pos];
  }
  ll mid = (st + en) / 2;
  return max(query(st, mid, 2 * pos + 1, l, r),
             query(mid + 1, en, 2 * pos + 2, l, r));
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll n;
  cin >> n;
  for (ll i = 0; i < n; ++i) {
    cin >> h[i];
    --h[i];
  }
  for (ll i = 0; i < n; ++i) {
    cin >> a[i];
  }
  build(0, n - 1, 0);

  ll dp[n];
  dp[0] = a[0];
  update(0, n - 1, 0, h[0], a[0]);

  for (ll i = 1; i < n; ++i) {
    ll lol = 0;
    if (h[i] != 0) {
      lol = query(0, n - 1, 0, 0, h[i] - 1);
    }
    dp[i] = lol + a[i];
    update(0, n - 1, 0, h[i], dp[i]);
  }

  ll ans = *max_element(dp, dp + n);

  cout << ans << endl;

  return (0);
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define M 1000000007
#define mod 998244353
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define pi pair<ll, ll>
using namespace std;
const ll N = 200010;

ll h[N], a[N];

ll sgt[4 * N];

void build(ll st, ll en, ll pos) {
  if (st == en) {
    sgt[pos] = 0;
    return;
  }
  ll mid = (st + en) / 2;
  build(st, mid, 2 * pos + 1);
  build(mid + 1, en, 2 * pos + 2);
  sgt[pos] = max(sgt[2 * pos + 1], sgt[2 * pos + 2]);
}

void update(ll st, ll en, ll pos, ll idx, ll val) {
  if (st == en) {
    sgt[pos] = val;
    return;
  }
  ll mid = (st + en) / 2;
  if (idx >= st && idx <= mid) {
    update(st, mid, 2 * pos + 1, idx, val);
  } else {
    update(mid + 1, en, 2 * pos + 2, idx, val);
  }
  sgt[pos] = max(sgt[2 * pos + 1], sgt[2 * pos + 2]);
}

ll query(ll st, ll en, ll pos, ll l, ll r) {
  if (st > r || en < l) {
    return 0;
  }
  if (st >= l && en <= r) {
    return sgt[pos];
  }
  ll mid = (st + en) / 2;
  return max(query(st, mid, 2 * pos + 1, l, r),
             query(mid + 1, en, 2 * pos + 2, l, r));
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll n;
  cin >> n;
  for (ll i = 0; i < n; ++i) {
    cin >> h[i];
    --h[i];
  }
  for (ll i = 0; i < n; ++i) {
    cin >> a[i];
  }
  build(0, n - 1, 0);

  ll dp[n];
  dp[0] = a[0];
  update(0, n - 1, 0, h[0], a[0]);

  for (ll i = 1; i < n; ++i) {
    ll lol = 0;
    if (h[i] != 0) {
      lol = query(0, n - 1, 0, 0, h[i] - 1);
    }
    dp[i] = lol + a[i];
    update(0, n - 1, 0, h[i], dp[i]);
  }

  ll ans = *max_element(dp, dp + n);

  cout << ans << endl;

  return (0);
}