Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <unordered_map>

using namespace std;

#define forn(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define sz(a) static_cast<int>((a).size())
#define pb emplace_back
#define mp make_pair
#define endl '\n'
#define f first
#define s second

#ifdef SEREZHKA
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(...)
#endif

template <typename T> void _dbg(const char *_s, T _h) {
  cerr << _s << " = " << _h << "\n";
}

template <typename T, typename... Ts>
void _dbg(const char *_s, T _h, Ts... _t) {
  int _b = 0;

  while (((_b += *_s == '(') -= *_s == ')') != 0 || *_s != ',')
    cerr << *_s++;

  cerr << " = " << _h << ",";
  _dbg(_s + 1, _t...);
}

using ll = long long;

const ll INF = static_cast<ll>(1e9) + 7ll;
const ll LINF = INF * INF;
const int MAXN = static_cast<int>(1e5) + 17;

struct segtree {
  int n;
  vector<ll> T;

  segtree() = default;
  segtree(int n) : n(n) { T.resize(4 * n); }

  void upd(int i, ll x, int v, int l, int r) {
    if (l == r) {
      T[v] = max(T[v], x);
      return;
    }

    auto m = (l + r) / 2;

    if (i <= m)
      upd(i, x, 2 * v, l, m);
    else
      upd(i, x, 2 * v + 1, m + 1, r);

    T[v] = max(T[2 * v], T[2 * v + 1]);
  }

  ll get(int lq, int rq, int v, int l, int r) {
    if (r < lq || l > rq)
      return 0;

    if (lq <= l && r <= rq)
      return T[v];

    auto m = (l + r) / 2;
    return max(get(lq, rq, 2 * v, l, m), get(lq, rq, 2 * v + 1, m + 1, r));
  }

  void upd(int i, ll x) { upd(i, x, 1, 0, n - 1); }

  ll get(int lq, int rq) { return get(lq, rq, 1, 0, n - 1); }
};

int n;
int h[MAXN], a[MAXN];

bool read() {
  if (!(cin >> n))
    return false;

  forn(i, n) cin >> h[i];

  forn(i, n) cin >> a[i];

  return true;
}

void solve() {
  segtree T(n + 1);
  ll ans = 0;

  for (int i = 0; i < n; ++i) {
    ll res = T.get(0, h[i] - 1) + a[i];
    T.upd(h[i], res);
    ans = max(ans, res);
  }

  cout << ans << endl;
}

int main() {
  cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);

#ifdef SEREZHKA
  freopen("file.in", "r", stdin);
#endif

  while (read())
    solve();

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <unordered_map>

using namespace std;

#define forn(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define sz(a) static_cast<int>((a).size())
#define pb emplace_back
#define mp make_pair
#define endl '\n'
#define f first
#define s second

#ifdef SEREZHKA
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(...)
#endif

template <typename T> void _dbg(const char *_s, T _h) {
  cerr << _s << " = " << _h << "\n";
}

template <typename T, typename... Ts>
void _dbg(const char *_s, T _h, Ts... _t) {
  int _b = 0;

  while (((_b += *_s == '(') -= *_s == ')') != 0 || *_s != ',')
    cerr << *_s++;

  cerr << " = " << _h << ",";
  _dbg(_s + 1, _t...);
}

using ll = long long;

const ll INF = static_cast<ll>(1e9) + 7ll;
const ll LINF = INF * INF;
const int MAXN = static_cast<int>(2e5) + 17;

struct segtree {
  int n;
  vector<ll> T;

  segtree() = default;
  segtree(int n) : n(n) { T.resize(4 * n); }

  void upd(int i, ll x, int v, int l, int r) {
    if (l == r) {
      T[v] = max(T[v], x);
      return;
    }

    auto m = (l + r) / 2;

    if (i <= m)
      upd(i, x, 2 * v, l, m);
    else
      upd(i, x, 2 * v + 1, m + 1, r);

    T[v] = max(T[2 * v], T[2 * v + 1]);
  }

  ll get(int lq, int rq, int v, int l, int r) {
    if (r < lq || l > rq)
      return 0;

    if (lq <= l && r <= rq)
      return T[v];

    auto m = (l + r) / 2;
    return max(get(lq, rq, 2 * v, l, m), get(lq, rq, 2 * v + 1, m + 1, r));
  }

  void upd(int i, ll x) { upd(i, x, 1, 0, n - 1); }

  ll get(int lq, int rq) { return get(lq, rq, 1, 0, n - 1); }
};

int n;
int h[MAXN], a[MAXN];

bool read() {
  if (!(cin >> n))
    return false;

  forn(i, n) cin >> h[i];

  forn(i, n) cin >> a[i];

  return true;
}

void solve() {
  segtree T(n + 1);
  ll ans = 0;

  for (int i = 0; i < n; ++i) {
    ll res = T.get(0, h[i] - 1) + a[i];
    T.upd(h[i], res);
    ans = max(ans, res);
  }

  cout << ans << endl;
}

int main() {
  cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);

#ifdef SEREZHKA
  freopen("file.in", "r", stdin);
#endif

  while (read())
    solve();

  return 0;
}