Problem p03176 (C++)

Buggy submission (status: Runtime Error):
/* * * * * * * * * * * **
 *                      *
 *	   saurabh8522	   *
 *	  I will handle	   *
 *		   IT.		   *
 *                      *
 * * * * * * * * * * * **/
#include <bits/stdc++.h>
#define mp(a, b) make_pair(a, b)
#define pb push_back
#define FastRead                                                               \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl '\n'
#define ld long double
#define zero(a) memset((a), 0, sizeof((a)))
#define one(a) memset((a), 1, sizeof((a)))
#define minus(a) memset((a), -1, sizeof((a)))
#define all(g) g.begin(), g.end()
#define ppb pop_back
using namespace std;
typedef long long int ll;
#define MOD 1000000007
ll extgcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  } else {
    int g = extgcd(b, a % b, y, x);
    y -= a / b * x;
    return g;
  }
}
ll modpow(ll a, ll b) {
  ll res = 1;
  a %= MOD;
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
  }
  return res;
}
ll numdigit(ll n) { return floor(log10(n)) + 1; }
bool isPowerTwo(ll x) { return x && (!(x & (x - 1))); }
ll seg[500005];
ll dp[500005];
void build(ll node, ll s, ll e) {
  // cout<<node<<" "<<s<<" "<<e<<endl;
  if (s == e) {
    seg[node] = dp[s];
    return;
  }
  ll mid = (s + e) >> 1;
  build(2 * node + 1, s, mid);
  build(2 * node + 2, mid + 1, e);
  seg[node] = max(seg[2 * node + 1], seg[2 * node + 2]);
}
ll query(ll node, ll s, ll e, ll qs, ll qe) {
  if (qs <= s && qe >= e) {
    return seg[node];
  }
  if (qe < s || qs > e) {
    return 0;
  }
  ll mid = (s + e) >> 1;
  return max(query(2 * node + 1, s, mid, qs, qe),
             query(2 * node + 2, mid + 1, e, qs, qe));
}
void update(ll node, ll s, ll e, ll index, ll val) {
  if (s == e) {
    seg[node] += val;
    return;
  }
  ll mid = (s + e) >> 1;
  if (index <= mid && index >= s) {
    update(2 * node + 1, s, mid, index, val);
  } else {
    update(2 * node + 2, mid + 1, e, index, val);
  }
  seg[node] = max(seg[2 * node + 1], seg[2 * node + 2]);
}
int main() {
  // FastRead;
  ll t = 1;
  // cin>>t;
  while (t--) {
    ll n;
    cin >> n;
    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];
    }
    // ll dp[n+1];
    zero(dp);
    build(0, 0, n);
    // cout<<0<<endl;
    for (int i = 0; i < n; i++) {
      ll ans = query(0, 0, n, 0, h[i] - 1);
      ll diff = dp[h[i]];
      dp[h[i]] = ans + a[i];
      update(0, 0, n, h[i], dp[h[i]] - diff);
    }
    ll ans = 0;
    for (int i = 0; i <= n; i++) {
      ans = max(ans, dp[h[i]]);
    }
    cout << ans << endl;
  }
}

Fixed submission:
/* * * * * * * * * * * **
 *                      *
 *	   saurabh8522	   *
 *	  I will handle	   *
 *		   IT.		   *
 *                      *
 * * * * * * * * * * * **/
#include <bits/stdc++.h>
#define mp(a, b) make_pair(a, b)
#define pb push_back
#define FastRead                                                               \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl '\n'
#define ld long double
#define zero(a) memset((a), 0, sizeof((a)))
#define one(a) memset((a), 1, sizeof((a)))
#define minus(a) memset((a), -1, sizeof((a)))
#define all(g) g.begin(), g.end()
#define ppb pop_back
using namespace std;
typedef long long int ll;
#define MOD 1000000007
ll extgcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  } else {
    int g = extgcd(b, a % b, y, x);
    y -= a / b * x;
    return g;
  }
}
ll modpow(ll a, ll b) {
  ll res = 1;
  a %= MOD;
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
  }
  return res;
}
ll numdigit(ll n) { return floor(log10(n)) + 1; }
bool isPowerTwo(ll x) { return x && (!(x & (x - 1))); }
ll seg[900005];
ll dp[500005];
void build(ll node, ll s, ll e) {
  // cout<<node<<" "<<s<<" "<<e<<endl;
  if (s == e) {
    seg[node] = dp[s];
    return;
  }
  ll mid = (s + e) >> 1;
  build(2 * node + 1, s, mid);
  build(2 * node + 2, mid + 1, e);
  seg[node] = max(seg[2 * node + 1], seg[2 * node + 2]);
}
ll query(ll node, ll s, ll e, ll qs, ll qe) {
  if (qs <= s && qe >= e) {
    return seg[node];
  }
  if (qe < s || qs > e) {
    return 0;
  }
  ll mid = (s + e) >> 1;
  return max(query(2 * node + 1, s, mid, qs, qe),
             query(2 * node + 2, mid + 1, e, qs, qe));
}
void update(ll node, ll s, ll e, ll index, ll val) {
  if (s == e) {
    seg[node] += val;
    return;
  }
  ll mid = (s + e) >> 1;
  if (index <= mid && index >= s) {
    update(2 * node + 1, s, mid, index, val);
  } else {
    update(2 * node + 2, mid + 1, e, index, val);
  }
  seg[node] = max(seg[2 * node + 1], seg[2 * node + 2]);
}
int main() {
  // FastRead;
  ll t = 1;
  // cin>>t;
  while (t--) {
    ll n;
    cin >> n;
    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];
    }
    // ll dp[n+1];
    zero(dp);
    build(0, 0, n);
    // cout<<0<<endl;
    for (int i = 0; i < n; i++) {
      ll ans = query(0, 0, n, 0, h[i] - 1);
      ll diff = dp[h[i]];
      dp[h[i]] = ans + a[i];
      update(0, 0, n, h[i], dp[h[i]] - diff);
    }
    ll ans = 0;
    for (int i = 0; i <= n; i++) {
      ans = max(ans, dp[h[i]]);
    }
    cout << ans << endl;
  }
}