Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define N 1009
#define INF 1000000009
#define MOD 1000000007
typedef long long int ll;
#define sd(x) scanf("%d", &x)
#define sd2(x, y) scanf("%lld%lld", &x, &y)
#define sd3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define pf(x) prllf("%d", x);
#define pf2(x, y) prllf("%d %d\n", x, y);
#define pf3(x, y, z) prllf("%d %d %d\n", x, y, z);
#define fi first
#define se second
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define _                                                                      \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define tr(x) cout << x << endl;
#define tr2(x, y) cout << x << " " << y << " " << endl;
#define tr3(x, y, z) cout << x << " " << y << " " << z << endl;
#define tr4(x, y, z, a)                                                        \
  cout << x << " " << y << " " << z << " " << a << " " << endl;
ll input[N];
ll tree[4 * N + 9];
ll a[N], b[N];

void update(ll low, ll high, ll pos, ll up, ll index) {
  if (low == high) {
    tree[pos] = up;
    return;
  }
  ll mid = (low + high) / 2;
  if (index <= mid) {
    update(low, mid, 2 * pos + 1, up, index);
  } else {
    update(mid + 1, high, 2 * pos + 2, up, index);
  }
  tree[pos] = max(tree[2 * pos + 1], tree[2 * pos + 2]);
}
ll query(ll low, ll high, ll pos, ll l, ll r) {
  if (low > r || high < l) {
    return 0;
  }
  if (low >= l && high <= r) {
    return tree[pos];
  }
  ll mid = (low + high) / 2;
  ll p1 = query(low, mid, 2 * pos + 1, l, r);
  ll p2 = query(mid + 1, high, 2 * pos + 2, l, r);
  return max(p1, p2);
}

int main() {
  ll i, j, n, m, t, k, x, y;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (i = 0; i < n; i++) {
    cin >> b[i];
  }
  ll ans = b[0], temp;
  update(0, n, 0, b[0], a[0]);
  for (i = 1; i < n; i++) {
    temp = query(0, n, 0, 0, a[i] - 1);
    temp += b[i];
    // tr3(i,a[i],temp);
    ans = max(ans, temp);
    update(0, n, 0, temp, a[i]);

    // temp=query(0,n,0,0,a[i]-1);
    // tr3(i,a[i],temp);
    // cout << endl;
  }
  tr(ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define N 200009
#define INF 1000000009
#define MOD 1000000007
typedef long long int ll;
#define sd(x) scanf("%d", &x)
#define sd2(x, y) scanf("%lld%lld", &x, &y)
#define sd3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define pf(x) prllf("%d", x);
#define pf2(x, y) prllf("%d %d\n", x, y);
#define pf3(x, y, z) prllf("%d %d %d\n", x, y, z);
#define fi first
#define se second
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define _                                                                      \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define tr(x) cout << x << endl;
#define tr2(x, y) cout << x << " " << y << " " << endl;
#define tr3(x, y, z) cout << x << " " << y << " " << z << endl;
#define tr4(x, y, z, a)                                                        \
  cout << x << " " << y << " " << z << " " << a << " " << endl;
ll input[N];
ll tree[4 * N + 9];
ll a[N], b[N];

void update(ll low, ll high, ll pos, ll up, ll index) {
  if (low == high) {
    tree[pos] = up;
    return;
  }
  ll mid = (low + high) / 2;
  if (index <= mid) {
    update(low, mid, 2 * pos + 1, up, index);
  } else {
    update(mid + 1, high, 2 * pos + 2, up, index);
  }
  tree[pos] = max(tree[2 * pos + 1], tree[2 * pos + 2]);
}
ll query(ll low, ll high, ll pos, ll l, ll r) {
  if (low > r || high < l) {
    return 0;
  }
  if (low >= l && high <= r) {
    return tree[pos];
  }
  ll mid = (low + high) / 2;
  ll p1 = query(low, mid, 2 * pos + 1, l, r);
  ll p2 = query(mid + 1, high, 2 * pos + 2, l, r);
  return max(p1, p2);
}

int main() {
  ll i, j, n, m, t, k, x, y;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (i = 0; i < n; i++) {
    cin >> b[i];
  }
  ll ans = b[0], temp;
  update(0, n, 0, b[0], a[0]);
  for (i = 1; i < n; i++) {
    temp = query(0, n, 0, 0, a[i] - 1);
    temp += b[i];
    // tr3(i,a[i],temp);
    ans = max(ans, temp);
    update(0, n, 0, temp, a[i]);

    // temp=query(0,n,0,0,a[i]-1);
    // tr3(i,a[i],temp);
    // cout << endl;
  }
  tr(ans);
}