Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define ld long double
#define fio()                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
#define MOD 1000000007
#define nl '\n'
#define pb push_back
#define mp make_pair

using namespace std;
const int N = 105;
const int K = 200005;
int t[N << 2];
void build(int a[], int v, int tl, int tr) {
  if (tl == tr) {
    t[v] = a[tl];
  } else {
    int tm = (tl + tr) / 2;
    build(a, v * 2, tl, tm);
    build(a, v * 2 + 1, tm + 1, tr);
    t[v] = t[v * 2] + t[v * 2 + 1];
  }
}
int query(int v, int tl, int tr, int l, int r) {
  if (l > r)
    return 0;
  if (l == tl && r == tr) {
    return t[v];
  }
  int tm = (tl + tr) / 2;
  return max(query(v * 2, tl, tm, l, min(r, tm)),
             query(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
}
void update(int v, int tl, int tr, int pos, int new_val) {
  if (tl == tr) {
    t[v] = new_val;
  } else {
    int tm = (tl + tr) / 2;
    if (pos <= tm)
      update(v * 2, tl, tm, pos, new_val);
    else
      update(v * 2 + 1, tm + 1, tr, pos, new_val);
    t[v] = max(t[v * 2], t[v * 2 + 1]);
  }
}
int32_t main() {
  fio();
  int n;
  cin >> n;
  vector<pair<pair<int, int>, int>> v;
  int h[n], a[n];
  for (int i = 0; i < n; i++)
    cin >> h[i];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    v.push_back({{h[i], a[i]}, i});
  sort(v.begin(), v.end());
  for (int i = 0; i < n; i++) {
    int u = 0;
    if (v[i].second != 0)
      u = query(1, 0, n - 1, 0, v[i].second - 1);
    // cout<<u<<endl;
    update(1, 0, n - 1, v[i].second, u + v[i].first.second);
    // cout<<query(1,0,n-1,1,1)<<endl;
  }
  cout << query(1, 0, n - 1, 0, n - 1);
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define ld long double
#define fio()                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
#define MOD 1000000007
#define nl '\n'
#define pb push_back
#define mp make_pair

using namespace std;
const int N = 105;
const int K = 200005;
int t[K << 2];
void build(int a[], int v, int tl, int tr) {
  if (tl == tr) {
    t[v] = a[tl];
  } else {
    int tm = (tl + tr) / 2;
    build(a, v * 2, tl, tm);
    build(a, v * 2 + 1, tm + 1, tr);
    t[v] = t[v * 2] + t[v * 2 + 1];
  }
}
int query(int v, int tl, int tr, int l, int r) {
  if (l > r)
    return 0;
  if (l == tl && r == tr) {
    return t[v];
  }
  int tm = (tl + tr) / 2;
  return max(query(v * 2, tl, tm, l, min(r, tm)),
             query(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
}
void update(int v, int tl, int tr, int pos, int new_val) {
  if (tl == tr) {
    t[v] = new_val;
  } else {
    int tm = (tl + tr) / 2;
    if (pos <= tm)
      update(v * 2, tl, tm, pos, new_val);
    else
      update(v * 2 + 1, tm + 1, tr, pos, new_val);
    t[v] = max(t[v * 2], t[v * 2 + 1]);
  }
}
int32_t main() {
  fio();
  int n;
  cin >> n;
  vector<pair<pair<int, int>, int>> v;
  int h[n], a[n];
  for (int i = 0; i < n; i++)
    cin >> h[i];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    v.push_back({{h[i], a[i]}, i});
  sort(v.begin(), v.end());
  for (int i = 0; i < n; i++) {
    int u = 0;
    if (v[i].second != 0)
      u = query(1, 0, n - 1, 0, v[i].second - 1);
    // cout<<u<<endl;
    update(1, 0, n - 1, v[i].second, u + v[i].first.second);
    // cout<<query(1,0,n-1,1,1)<<endl;
  }
  cout << query(1, 0, n - 1, 0, n - 1);
}