Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100001;
ll dp[N];
ll getMid(ll s, ll e) { return s + (e - s) / 2; }
ll MaxUtil(ll *st, ll ss, ll se, ll l, ll r, ll node) {
  if (l <= ss && r >= se)
    return st[node];
  if (se < l || ss > r)
    return -1;
  ll mid = getMid(ss, se);
  return max(MaxUtil(st, ss, mid, l, r, 2 * node + 1),
             MaxUtil(st, mid + 1, se, l, r, 2 * node + 2));
}
void updateValue(ll arr[], ll *st, ll ss, ll se, ll index, ll value, ll node) {
  if (index < ss || index > se) {
    cout << "Invalid Input" << endl;
    return;
  }
  if (ss == se) {
    arr[index] = value;
    st[node] = value;
  } else {
    ll mid = getMid(ss, se);
    if (index >= ss && index <= mid)
      updateValue(arr, st, ss, mid, index, value, 2 * node + 1);
    else
      updateValue(arr, st, mid + 1, se, index, value, 2 * node + 2);
    st[node] = max(st[2 * node + 1], st[2 * node + 2]);
  }
  return;
}
ll getMax(ll *st, ll n, ll l, ll r) {
  if (l < 0 || r > n - 1 || l > r) {
    printf("Invalid Input");
    return -1;
  }
  return MaxUtil(st, 0, n - 1, l, r, 0);
}
ll constructSTUtil(ll arr[], ll ss, ll se, ll *st, ll si) {
  if (ss == se) {
    st[si] = arr[ss];
    return arr[ss];
  }
  ll mid = getMid(ss, se);
  st[si] = max(constructSTUtil(arr, ss, mid, st, si * 2 + 1),
               constructSTUtil(arr, mid + 1, se, st, si * 2 + 2));
  return st[si];
}
ll *constructST(ll arr[], ll n) {
  ll x = (int)(ceil(log2(n)));
  ll max_size = 2 * (int)pow(2, x) - 1;
  ll *st = new ll[max_size];
  constructSTUtil(arr, 0, n - 1, st, 0);
  return st;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // n = 10;
  // ll a[10] = {1,2,3,4,5,6,7,8,9,10};
  // ll st = constructST(a,n);
  int n;
  cin >> n;
  ll h[n + 1], c[n + 1];
  for (int i = 0; i < n; ++i)
    cin >> h[i];
  for (int i = 0; i < n; ++i)
    cin >> c[i];
  ll *tree = constructST(dp, n);
  for (int i = 0; i < n; ++i) {
    if (h[i] == 1)
      updateValue(dp, tree, 0, n - 1, 0, c[i], 0);
    else {
      ll z = getMax(tree, n, 0, h[i] - 2);
      updateValue(dp, tree, 0, n - 1, h[i] - 1, z + c[i], 0);
    }
  }
  // for(int i = 0; i < n; ++i) cout<<dp[i]<<" ";
  // cout<<"\n";
  cout << *max_element(dp, dp + n) << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 200001;
ll dp[N];
ll getMid(ll s, ll e) { return s + (e - s) / 2; }
ll MaxUtil(ll *st, ll ss, ll se, ll l, ll r, ll node) {
  if (l <= ss && r >= se)
    return st[node];
  if (se < l || ss > r)
    return -1;
  ll mid = getMid(ss, se);
  return max(MaxUtil(st, ss, mid, l, r, 2 * node + 1),
             MaxUtil(st, mid + 1, se, l, r, 2 * node + 2));
}
void updateValue(ll arr[], ll *st, ll ss, ll se, ll index, ll value, ll node) {
  if (index < ss || index > se) {
    cout << "Invalid Input" << endl;
    return;
  }
  if (ss == se) {
    arr[index] = value;
    st[node] = value;
  } else {
    ll mid = getMid(ss, se);
    if (index >= ss && index <= mid)
      updateValue(arr, st, ss, mid, index, value, 2 * node + 1);
    else
      updateValue(arr, st, mid + 1, se, index, value, 2 * node + 2);
    st[node] = max(st[2 * node + 1], st[2 * node + 2]);
  }
  return;
}
ll getMax(ll *st, ll n, ll l, ll r) {
  if (l < 0 || r > n - 1 || l > r) {
    printf("Invalid Input");
    return -1;
  }
  return MaxUtil(st, 0, n - 1, l, r, 0);
}
ll constructSTUtil(ll arr[], ll ss, ll se, ll *st, ll si) {
  if (ss == se) {
    st[si] = arr[ss];
    return arr[ss];
  }
  ll mid = getMid(ss, se);
  st[si] = max(constructSTUtil(arr, ss, mid, st, si * 2 + 1),
               constructSTUtil(arr, mid + 1, se, st, si * 2 + 2));
  return st[si];
}
ll *constructST(ll arr[], ll n) {
  ll x = (int)(ceil(log2(n)));
  ll max_size = 2 * (int)pow(2, x) - 1;
  ll *st = new ll[max_size];
  constructSTUtil(arr, 0, n - 1, st, 0);
  return st;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // n = 10;
  // ll a[10] = {1,2,3,4,5,6,7,8,9,10};
  // ll st = constructST(a,n);
  int n;
  cin >> n;
  ll h[n + 1], c[n + 1];
  for (int i = 0; i < n; ++i)
    cin >> h[i];
  for (int i = 0; i < n; ++i)
    cin >> c[i];
  ll *tree = constructST(dp, n);
  for (int i = 0; i < n; ++i) {
    if (h[i] == 1)
      updateValue(dp, tree, 0, n - 1, 0, c[i], 0);
    else {
      ll z = getMax(tree, n, 0, h[i] - 2);
      updateValue(dp, tree, 0, n - 1, h[i] - 1, z + c[i], 0);
    }
  }
  // for(int i = 0; i < n; ++i) cout<<dp[i]<<" ";
  // cout<<"\n";
  cout << *max_element(dp, dp + n) << "\n";
}