Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define ff first
#define ss second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define in insert
#define pb push_back
#define IOS                                                                    \
  std::ios::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define endl "\n"
inline ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
inline ll power(ll a, ll n, ll m) {
  if (n == 0)
    return 1;
  ll p = power(a, n / 2, m);
  p = (p * p) % m;
  if (n % 2)
    return (p * a) % m;
  else
    return p;
}
const ll mod = 1e9 + 7;
const int N = int(3e5) + 999;
#define int long long int
#define inf 1e18

int n;
int st[400005];
int dp[100005];

int query(int l, int r, int i, int qs, int qe) {
  if (l > r)
    return -inf;
  if (qs > r or qe < l)
    return -inf;
  if (qs <= l and qe >= r)
    return st[i];
  int m = (l + r) / 2;
  return max(query(l, m, 2 * i + 1, qs, qe),
             query(m + 1, r, 2 * i + 2, qs, qe));
}

void update(int l, int r, int i, int index, int val) {
  if (l > r)
    return;
  if (index > r or index < l)
    return;
  if (l == r) {
    st[i] = val;
    return;
  }
  int m = (l + r) / 2;
  update(l, m, 2 * i + 1, index, val);
  update(m + 1, r, 2 * i + 2, index, val);
  st[i] = max(st[2 * i + 1], st[2 * i + 2]);
}

int32_t main() {
  IOS;
  cin >> n;
  int h[n], b[n];
  for (int i = 0; i < n; i++)
    cin >> h[i];
  for (int i = 0; i < n; i++)
    cin >> b[i];

  int dp[n];
  for (int i = 0; i < n; i++) {
    int mx = 0;
    if (h[i] > 1)
      mx = query(0, n - 1, 0, 0, h[i] - 2);
    dp[i] = b[i] + mx;
    update(0, n - 1, 0, h[i] - 1, dp[i]);
  }
  int ans = 0;
  for (int i = 0; i < n; i++)
    ans = max(ans, dp[i]);
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define ff first
#define ss second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define in insert
#define pb push_back
#define IOS                                                                    \
  std::ios::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define endl "\n"
inline ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
inline ll power(ll a, ll n, ll m) {
  if (n == 0)
    return 1;
  ll p = power(a, n / 2, m);
  p = (p * p) % m;
  if (n % 2)
    return (p * a) % m;
  else
    return p;
}
const ll mod = 1e9 + 7;
const int N = int(3e5) + 999;
#define int long long int
#define inf 1e18

int n;
int st[4000005];
int dp[1000005];

int query(int l, int r, int i, int qs, int qe) {
  if (l > r)
    return -inf;
  if (qs > r or qe < l)
    return -inf;
  if (qs <= l and qe >= r)
    return st[i];
  int m = (l + r) / 2;
  return max(query(l, m, 2 * i + 1, qs, qe),
             query(m + 1, r, 2 * i + 2, qs, qe));
}

void update(int l, int r, int i, int index, int val) {
  if (l > r)
    return;
  if (index > r or index < l)
    return;
  if (l == r) {
    st[i] = val;
    return;
  }
  int m = (l + r) / 2;
  update(l, m, 2 * i + 1, index, val);
  update(m + 1, r, 2 * i + 2, index, val);
  st[i] = max(st[2 * i + 1], st[2 * i + 2]);
}

int32_t main() {
  IOS;
  cin >> n;
  int h[n], b[n];
  for (int i = 0; i < n; i++)
    cin >> h[i];
  for (int i = 0; i < n; i++)
    cin >> b[i];

  int dp[n];
  for (int i = 0; i < n; i++) {
    int mx = 0;
    if (h[i] > 1)
      mx = query(0, n - 1, 0, 0, h[i] - 2);
    dp[i] = b[i] + mx;
    update(0, n - 1, 0, h[i] - 1, dp[i]);
  }
  int ans = 0;
  for (int i = 0; i < n; i++)
    ans = max(ans, dp[i]);
  cout << ans;
  return 0;
}