Problem p03176 (C++)

Buggy submission (status: Runtime Error):
/*
 _____ _             _              _           _
|_   _| |__   ___   / \   _ __  ___| |__  _   _| |
  | | | '_ \ / _ \ / _ \ | '_ \/ __| '_ \| | | | |
  | | | | | |  __// ___ \| | | \__ \ | | | |_| | |
  |_| |_| |_|\___/_/   \_\_| |_|___/_| |_|\__,_|_|

*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define pb push_back
#define ppb pop_back
#define endl '\n'
#define mii map<ll, ll>
#define msi map<string, ll>
#define mis map<ll, string>
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, a, b) for (ll i = b - 1; i >= a; i--)
#define trav(a, x) for (auto &a : x)
#define pii pair<ll, ll>
#define vi vector<ll>
#define vii vector<pair<ll, ll>>
#define vs vector<string>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (ll) x.size()
#define hell 1000000007
#define lbnd lower_bound
#define ubnd upper_bound
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)

/*	For Debugging	*/
#define DEBUG cerr << "\n>>>I'm Here<<<\n" << endl;
#define display(x)                                                             \
  trav(a, x) cout << a << " ";                                                 \
  cout << endl;
#define what_is(x) cerr << #x << " is " << x << endl;

std::mt19937_64
    rng(std::chrono::steady_clock::now().time_since_epoch().count());
#define ordered_set                                                            \
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define TIME                                                                   \
  cerr << "\nTime elapsed: " << setprecision(5)                                \
       << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
#define DECIMAL(n)                                                             \
  cout << fixed;                                                               \
  cout << setprecision(n);
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace __gnu_pbds;
using namespace std;
#define PI 3.141592653589793
#define N 100005
vi seg(4 * N);
void build(ll cur, ll st, ll end) {
  if (st == end) {
    seg[cur] = 0;
    return;
  }
  ll mid = (st + end) >> 1;
  build(2 * cur, st, mid);
  build(2 * cur + 1, mid + 1, end);
  seg[cur] = max(seg[2 * cur], seg[2 * cur + 1]); /*  1-change here  */
}
ll query(ll cur, ll st, ll end, ll l, ll r) {
  if (l <= st && r >= end)
    return seg[cur];
  if (l > r || r < st || l > end)
    return 0; /*  2-change here  */
  ll mid = (st + end) >> 1;
  ll ans1 = query(2 * cur, st, mid, l, r);
  ll ans2 = query(2 * cur + 1, mid + 1, end, l, r);
  return max(ans1, ans2); /*  3-change here  */
}
void update(ll cur, ll st, ll end, ll pos, ll upd) {
  if (st == end) {
    // a[pos]+=upd;					/*  4-change here  */
    seg[cur] += upd; /*  5-change here  */
    return;
  }
  ll mid = (st + end) >> 1;
  if (st <= pos && pos <= mid)
    update(2 * cur, st, mid, pos, upd);
  else
    update(2 * cur + 1, mid + 1, end, pos, upd);
  seg[cur] = max(seg[2 * cur], seg[2 * cur + 1]); /*  6-change here  */
}
void solve() {
  ll n;
  cin >> n;
  vi v(n), a(n);
  rep(i, 0, n) cin >> v[i];
  rep(i, 0, n) cin >> a[i];
  build(1, 1, n);
  repr(i, 0, n) { update(1, 1, n, v[i], query(1, 1, n, v[i] + 1, n) + a[i]); }
  cout << query(1, 1, n, 1, n) << endl;

  return;
}
int main() {
  FAST int TESTS = 1;
  //	cin>>TESTS;
  while (TESTS--) {
    solve();
  }
  TIME return 0;
}

Fixed submission:
/*
 _____ _             _              _           _
|_   _| |__   ___   / \   _ __  ___| |__  _   _| |
  | | | '_ \ / _ \ / _ \ | '_ \/ __| '_ \| | | | |
  | | | | | |  __// ___ \| | | \__ \ | | | |_| | |
  |_| |_| |_|\___/_/   \_\_| |_|___/_| |_|\__,_|_|

*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define pb push_back
#define ppb pop_back
#define endl '\n'
#define mii map<ll, ll>
#define msi map<string, ll>
#define mis map<ll, string>
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, a, b) for (ll i = b - 1; i >= a; i--)
#define trav(a, x) for (auto &a : x)
#define pii pair<ll, ll>
#define vi vector<ll>
#define vii vector<pair<ll, ll>>
#define vs vector<string>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (ll) x.size()
#define hell 1000000007
#define lbnd lower_bound
#define ubnd upper_bound
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)

/*	For Debugging	*/
#define DEBUG cerr << "\n>>>I'm Here<<<\n" << endl;
#define display(x)                                                             \
  trav(a, x) cout << a << " ";                                                 \
  cout << endl;
#define what_is(x) cerr << #x << " is " << x << endl;

std::mt19937_64
    rng(std::chrono::steady_clock::now().time_since_epoch().count());
#define ordered_set                                                            \
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define TIME                                                                   \
  cerr << "\nTime elapsed: " << setprecision(5)                                \
       << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
#define DECIMAL(n)                                                             \
  cout << fixed;                                                               \
  cout << setprecision(n);
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace __gnu_pbds;
using namespace std;
#define PI 3.141592653589793
#define N 200005
vi seg(4 * N);
void build(ll cur, ll st, ll end) {
  if (st == end) {
    seg[cur] = 0;
    return;
  }
  ll mid = (st + end) >> 1;
  build(2 * cur, st, mid);
  build(2 * cur + 1, mid + 1, end);
  seg[cur] = max(seg[2 * cur], seg[2 * cur + 1]); /*  1-change here  */
}
ll query(ll cur, ll st, ll end, ll l, ll r) {
  if (l <= st && r >= end)
    return seg[cur];
  if (l > r || r < st || l > end)
    return 0; /*  2-change here  */
  ll mid = (st + end) >> 1;
  ll ans1 = query(2 * cur, st, mid, l, r);
  ll ans2 = query(2 * cur + 1, mid + 1, end, l, r);
  return max(ans1, ans2); /*  3-change here  */
}
void update(ll cur, ll st, ll end, ll pos, ll upd) {
  if (st == end) {
    // a[pos]+=upd;					/*  4-change here  */
    seg[cur] += upd; /*  5-change here  */
    return;
  }
  ll mid = (st + end) >> 1;
  if (st <= pos && pos <= mid)
    update(2 * cur, st, mid, pos, upd);
  else
    update(2 * cur + 1, mid + 1, end, pos, upd);
  seg[cur] = max(seg[2 * cur], seg[2 * cur + 1]); /*  6-change here  */
}
void solve() {
  ll n;
  cin >> n;
  vi v(n), a(n);
  rep(i, 0, n) cin >> v[i];
  rep(i, 0, n) cin >> a[i];
  build(1, 1, n);
  repr(i, 0, n) { update(1, 1, n, v[i], query(1, 1, n, v[i] + 1, n) + a[i]); }
  cout << query(1, 1, n, 1, n) << endl;

  return;
}
int main() {
  FAST int TESTS = 1;
  //	cin>>TESTS;
  while (TESTS--) {
    solve();
  }
  TIME return 0;
}