Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
#define ld long double
#define dd double
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define sz(a) (ll)(a.size())
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<pll> vplll;
ll mod = 1e9 + 7;
const ld PI = 2 * acos(0.0);
const vector<ll> dx = {1, -1, 0, 0};
const vector<ll> dy = {0, 0, 1, -1};
#define round(x, y) ((x + y - 1) / y)
#define ce(x, y) ((x + y - 1) / y)
#define amax(x, y)                                                             \
  if (y > x)                                                                   \
    x = y;
#define amin(x, y)                                                             \
  if (y < x)                                                                   \
    x = y;
#define lcm(x, y) ((x) * (y) / __gcd(x, y))
#define len(x) (ll) x.length()
#define sq(x) ((x) * (x))
#define cb(x) ((x) * (x) * (x))
const int N = 1e5; // limit for array size
int n;             // array size
ll seg[2 * N];

void build(int ar[]) { // build the tree
  for (int i = n - 1; i > 0; --i)
    seg[i] = max(seg[i << 1], seg[i << 1 | 1]);
}

void modify(int p, ll value) { // set value at position p
  for (seg[p += n] = value; p > 1; p >>= 1)
    seg[p >> 1] = max(seg[p], seg[p ^ 1]);
}

ll query(int l, int r) { // sum on interval [l, r)
  ll res = 0;
  for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
    if (l & 1)
      res = max(res, seg[l++]);
    if (r & 1)
      res = max(res, seg[--r]);
  }
  return res;
}
vector<pair<ll, pll>> v;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll a, b, c, t, m, k, q;
  t = 1;
  // cin>>t;
  while (t--) {
    cin >> n;
    ll ar[n], br[n];
    for (int i = 0; i < n; i++)
      cin >> ar[i];
    for (int i = 0; i < n; i++) {
      cin >> br[i];
      v.pb(mp(ar[i], mp(br[i], i + 1)));
    }
    sort(all(v));
    reverse(all(v));
    for (auto j : v) {
      pll y = j.ss;
      // cout<<j.ff<<" "<<y.ff<<" "<<y.ss<<endl;
      ll ans = query(y.ss, n);
      // cout<<ans<<" ";
      ans += br[y.ss - 1];
      // cout<<ans<<"c"<<j.ff-1<<endl;;
      modify(y.ss - 1, ans);
    }
    cout << query(0, n) << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
#define ld long double
#define dd double
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define sz(a) (ll)(a.size())
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<pll> vplll;
ll mod = 1e9 + 7;
const ld PI = 2 * acos(0.0);
const vector<ll> dx = {1, -1, 0, 0};
const vector<ll> dy = {0, 0, 1, -1};
#define round(x, y) ((x + y - 1) / y)
#define ce(x, y) ((x + y - 1) / y)
#define amax(x, y)                                                             \
  if (y > x)                                                                   \
    x = y;
#define amin(x, y)                                                             \
  if (y < x)                                                                   \
    x = y;
#define lcm(x, y) ((x) * (y) / __gcd(x, y))
#define len(x) (ll) x.length()
#define sq(x) ((x) * (x))
#define cb(x) ((x) * (x) * (x))
const int N = 1e5; // limit for array size
int n;             // array size
ll seg[4 * N];

void build(int ar[]) { // build the tree
  for (int i = n - 1; i > 0; --i)
    seg[i] = max(seg[i << 1], seg[i << 1 | 1]);
}

void modify(int p, ll value) { // set value at position p
  for (seg[p += n] = value; p > 1; p >>= 1)
    seg[p >> 1] = max(seg[p], seg[p ^ 1]);
}

ll query(int l, int r) { // sum on interval [l, r)
  ll res = 0;
  for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
    if (l & 1)
      res = max(res, seg[l++]);
    if (r & 1)
      res = max(res, seg[--r]);
  }
  return res;
}
vector<pair<ll, pll>> v;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll a, b, c, t, m, k, q;
  t = 1;
  // cin>>t;
  while (t--) {
    cin >> n;
    ll ar[n], br[n];
    for (int i = 0; i < n; i++)
      cin >> ar[i];
    for (int i = 0; i < n; i++) {
      cin >> br[i];
      v.pb(mp(ar[i], mp(br[i], i + 1)));
    }
    sort(all(v));
    reverse(all(v));
    for (auto j : v) {
      pll y = j.ss;
      // cout<<j.ff<<" "<<y.ff<<" "<<y.ss<<endl;
      ll ans = query(y.ss, n);
      // cout<<ans<<" ";
      ans += br[y.ss - 1];
      // cout<<ans<<"c"<<j.ff-1<<endl;;
      modify(y.ss - 1, ans);
    }
    cout << query(0, n) << endl;
  }
  return 0;
}