Problem p03176 (C++)

Buggy submission (status: Runtime Error):
/*input
5
1 2 3 4 5
1000000000 1000000000 1000000000 1000000000 1000000000
*/
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pii pair<int, int>
#define pb push_back
#define f first
#define s second
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
int dp[100010];
int tree[400010];
int const N = 100001;
int V, ind;
void update(int TN, int S, int E) {
  if (S == E) {
    tree[TN] = V;
    return;
  }
  int mid = (S + E) / 2;
  if (mid >= ind) {
    update(2 * TN, S, mid);
  } else {
    update(2 * TN + 1, mid + 1, E);
  }
  tree[TN] = max(tree[2 * TN], tree[2 * TN + 1]);
}
int L, R;
int query(int TN, int S, int E) {
  if (S > R || E < L) {
    return 0;
  }
  if (S >= L && E <= R) {
    return tree[TN];
  }
  int mid = (S + E) / 2;
  return max(query(2 * TN, S, mid), query(2 * TN + 1, mid + 1, E));
}
signed main() {
  IOS;
  int n;
  cin >> n;
  int a[n + 1];
  int h[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  h[0] = 0;
  a[0] = 0;
  for (int i = 1; i <= n; i++) {
    dp[h[i]] = a[i];
    V = a[i];
    ind = h[i];
    update(1, 1, N);
    L = 0;
    R = h[i] - 1;
    dp[h[i]] = max(dp[h[i]], a[i] + query(1, 1, N));
    V = dp[h[i]];
    ind = h[i];
    update(1, 1, N);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dp[h[i]]);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
/*input
5
1 2 3 4 5
1000000000 1000000000 1000000000 1000000000 1000000000
*/
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pii pair<int, int>
#define pb push_back
#define f first
#define s second
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
int const N = 200001;
int dp[N];
int tree[4 * N];
int V, ind;
void update(int TN, int S, int E) {
  if (S == E) {
    tree[TN] = V;
    return;
  }
  int mid = (S + E) / 2;
  if (mid >= ind) {
    update(2 * TN, S, mid);
  } else {
    update(2 * TN + 1, mid + 1, E);
  }
  tree[TN] = max(tree[2 * TN], tree[2 * TN + 1]);
}
int L, R;
int query(int TN, int S, int E) {
  if (S > R || E < L) {
    return 0;
  }
  if (S >= L && E <= R) {
    return tree[TN];
  }
  int mid = (S + E) / 2;
  return max(query(2 * TN, S, mid), query(2 * TN + 1, mid + 1, E));
}
signed main() {
  IOS;
  int n;
  cin >> n;
  int a[n + 1];
  int h[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  h[0] = 0;
  a[0] = 0;
  for (int i = 1; i <= n; i++) {
    dp[h[i]] = a[i];
    V = a[i];
    ind = h[i];
    update(1, 1, N);
    L = 0;
    R = h[i] - 1;
    dp[h[i]] = max(dp[h[i]], a[i] + query(1, 1, N));
    V = dp[h[i]];
    ind = h[i];
    update(1, 1, N);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dp[h[i]]);
  }
  cout << ans << endl;
  return 0;
}