Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
bool sortinrev(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return (a.first > b.first);
}

ll query(ll tree[], ll index, ll qs, ll qe, ll s, ll e) {
  if (qs <= s && e <= qe) {
    return tree[index];
  }

  if (e < qs || s > qe) {
    return 0;
  }

  ll mid = (s + e) / 2;
  return max(query(tree, 2 * index, qs, qe, s, mid),
             query(tree, 2 * index + 1, qs, qe, mid + 1, e));
}

void build(ll tree[], ll s, ll e, ll index) {
  if (s == e) {
    tree[index] = 0;
  } else {
    int mid = (s + e) / 2;
    build(tree, s, mid, 2 * index);
    build(tree, mid + 1, e, 2 * index + 1);
    tree[index] = max(tree[2 * index], tree[2 * index + 1]);
  }
}

void update(ll tree[], ll s, ll e, ll value, ll hat, ll index) {
  if (s == e) {
    // arr[value]=1;
    tree[index] += hat;
  } else {
    ll mid = (s + e) / 2;
    if (value <= mid) {
      update(tree, s, mid, value, hat, 2 * index);
    } else {
      update(tree, mid + 1, e, value, hat, 2 * index + 1);
    }
    tree[index] = max(tree[2 * index], tree[2 * index + 1]);
  }
}

int main() {
  ll n;
  cin >> n;
  ll height[n];
  ll value[100005];
  for (ll i = 0; i < n; i++) {
    cin >> height[i];
  }
  for (ll i = 0; i < n; i++) {
    cin >> value[height[i]];
  }

  ll tree[4 * n + 1];

  vector<pair<ll, ll>> v(n);
  for (ll i = 0; i < n; i++) {
    v[i].first = height[i];
    v[i].second = i;
  }

  build(tree, 0, n - 1, 1);
  //  sort(v.begin(),v.end());
  sort(v.begin(), v.end(), sortinrev);

  // for(int i=0;i<n;i++)
  // {
  //   cout<<v[i].first<<" "<<v[i].second<<endl;
  // }
  vector<ll> final;
  for (ll i = 0; i < n; i++) {
    ll k = v[i].second;
    ll ans = query(tree, 1, k, n - 1, 0, n - 1);
    update(tree, 0, n - 1, k, ans + value[v[i].first], 1);
    final.push_back(ans + value[v[i].first]);
  }
  ll bug = *max_element(final.begin(), final.end());
  cout << bug << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
bool sortinrev(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return (a.first > b.first);
}

ll query(ll tree[], ll index, ll qs, ll qe, ll s, ll e) {
  if (qs <= s && e <= qe) {
    return tree[index];
  }

  if (e < qs || s > qe) {
    return 0;
  }

  ll mid = (s + e) / 2;
  return max(query(tree, 2 * index, qs, qe, s, mid),
             query(tree, 2 * index + 1, qs, qe, mid + 1, e));
}

void build(ll tree[], ll s, ll e, ll index) {
  if (s == e) {
    tree[index] = 0;
  } else {
    int mid = (s + e) / 2;
    build(tree, s, mid, 2 * index);
    build(tree, mid + 1, e, 2 * index + 1);
    tree[index] = max(tree[2 * index], tree[2 * index + 1]);
  }
}

void update(ll tree[], ll s, ll e, ll value, ll hat, ll index) {
  if (s == e) {
    // arr[value]=1;
    tree[index] += hat;
  } else {
    ll mid = (s + e) / 2;
    if (value <= mid) {
      update(tree, s, mid, value, hat, 2 * index);
    } else {
      update(tree, mid + 1, e, value, hat, 2 * index + 1);
    }
    tree[index] = max(tree[2 * index], tree[2 * index + 1]);
  }
}

int main() {
  ll n;
  cin >> n;
  ll height[n];
  ll value[200005];
  for (ll i = 0; i < n; i++) {
    cin >> height[i];
  }
  for (ll i = 0; i < n; i++) {
    cin >> value[height[i]];
  }

  ll tree[4 * n + 1];

  vector<pair<ll, ll>> v(n);
  for (ll i = 0; i < n; i++) {
    v[i].first = height[i];
    v[i].second = i;
  }

  build(tree, 0, n - 1, 1);
  //  sort(v.begin(),v.end());
  sort(v.begin(), v.end(), sortinrev);

  // for(int i=0;i<n;i++)
  // {
  //   cout<<v[i].first<<" "<<v[i].second<<endl;
  // }
  vector<ll> final;
  for (ll i = 0; i < n; i++) {
    ll k = v[i].second;
    ll ans = query(tree, 1, k, n - 1, 0, n - 1);
    update(tree, 0, n - 1, k, ans + value[v[i].first], 1);
    final.push_back(ans + value[v[i].first]);
  }
  ll bug = *max_element(final.begin(), final.end());
  cout << bug << endl;
}