Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define fio                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define per(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define For(i, n) for (int i = 1; i <= (int)(n); i++)
#define Rep(i, n) for (int i = 0; i <= (int)(n); i++)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define Max(a, b, c) max(a, max(b, c))
#define Min(a, b, c) min(a, min(b, c))
#define cmax(x, y) x = max(x, y)
#define cmin(x, y) x = min(x, y)
#define pii pair<int, int>
#define vi vector<int>
#define lb lower_bound
#define ub upper_bound
#define ll long long
#define pb push_back
#define re return
#define se second
#define fi first
const int INF = 0x7fffffff;
const int MAXN = 2e5 + 3;

int n, a[MAXN], b[MAXN];
ll tr[MAXN * 2], ans;

#define lson l, mid, k << 1
#define rson mid + 1, r, k << 1 | 1

void change(const int &p, const ll &v, const int &l, const int &r,
            const int &k) {
  if (l == p && r == p) {
    tr[k] = v;
    re;
  }
  int mid = (l + r) >> 1;
  if (p <= mid)
    change(p, v, lson);
  if (p > mid)
    change(p, v, rson);
  tr[k] = max(tr[k << 1], tr[k << 1 | 1]);
}

ll query(const int &L, const int &R, const int &l, const int &r, const int &k) {
  if (L > R)
    re 0;
  if (l >= L && r <= R)
    re tr[k];
  int mid = (l + r) >> 1;
  ll ret = 0;
  if (L <= mid)
    cmax(ret, query(L, R, lson));
  if (R > mid)
    cmax(ret, query(L, R, rson));
  re ret;
}

int main() {
  fio;
  cin >> n;
  For(i, n) { cin >> a[i]; }
  For(i, n) { cin >> b[i]; }
  For(i, n) {
    ll x = query(1, a[i] - 1, 1, n, 1);
    change(a[i], x + b[i], 1, n, 1);
    cmax(ans, x + b[i]);
  }
  cout << ans << endl;
  re 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define fio                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define per(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define For(i, n) for (int i = 1; i <= (int)(n); i++)
#define Rep(i, n) for (int i = 0; i <= (int)(n); i++)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define Max(a, b, c) max(a, max(b, c))
#define Min(a, b, c) min(a, min(b, c))
#define cmax(x, y) x = max(x, y)
#define cmin(x, y) x = min(x, y)
#define pii pair<int, int>
#define vi vector<int>
#define lb lower_bound
#define ub upper_bound
#define ll long long
#define pb push_back
#define re return
#define se second
#define fi first
const int INF = 0x7fffffff;
const int MAXN = 2e5 + 3;

int n, a[MAXN], b[MAXN];
ll tr[MAXN * 4], ans;

#define lson l, mid, k << 1
#define rson mid + 1, r, k << 1 | 1

void change(const int &p, const ll &v, const int &l, const int &r,
            const int &k) {
  if (l == p && r == p) {
    tr[k] = v;
    re;
  }
  int mid = (l + r) >> 1;
  if (p <= mid)
    change(p, v, lson);
  if (p > mid)
    change(p, v, rson);
  tr[k] = max(tr[k << 1], tr[k << 1 | 1]);
}

ll query(const int &L, const int &R, const int &l, const int &r, const int &k) {
  if (L > R)
    re 0;
  if (l >= L && r <= R)
    re tr[k];
  int mid = (l + r) >> 1;
  ll ret = 0;
  if (L <= mid)
    cmax(ret, query(L, R, lson));
  if (R > mid)
    cmax(ret, query(L, R, rson));
  re ret;
}

int main() {
  fio;
  cin >> n;
  For(i, n) { cin >> a[i]; }
  For(i, n) { cin >> b[i]; }
  For(i, n) {
    ll x = query(1, a[i] - 1, 1, n, 1);
    change(a[i], x + b[i], 1, n, 1);
    cmax(ans, x + b[i]);
  }
  cout << ans << endl;
  re 0;
}