Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); i++)
#define NFOR(i, a, b) for (ll i = (ll)(a); i >= (ll)(b); --i)
#define rep(i, a) for (ll i = 0; i < a; i++)
#define endl "\n"
#define fi first
#define se second
#define MOD 1000000007
#define inf 1e18
#define pb push_back
#define Case cout << "Case #" << ++cas << ": ";
#define fastio                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define all(v) v.begin(), v.end()

typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
typedef vector<vl> vvl;

#define pr(...) dbs(#__VA_ARGS__, __VA_ARGS__)
template <class T> void dbs(string str, T t) {
  cerr << str << " : " << t << "\n";
}
template <class T, class... S> void dbs(string str, T t, S... s) {
  int idx = str.find(',');
  cerr << str.substr(0, idx) << " : " << t << ",";
  dbs(str.substr(idx + 1), s...);
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class T> ostream &operator<<(ostream &os, const multiset<T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class S, class T>
ostream &operator<<(ostream &os, const map<S, T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class T> void prc(T a, T b) {
  cerr << "[";
  for (T i = a; i != b; ++i) {
    if (i != a)
      cerr << ", ";
    cerr << *i;
  }
  cerr << "]\n";
}

const int maxn = 20005;

inline ll L(ll a) { return (a << 1) + 1; }
inline ll R(ll a) { return (a << 1) + 2; }

class SegTree {
  ll T[maxn << 2];

public:
  void build() { memset(T, 0, sizeof(T)); }

  void update(ll node, ll st, ll en, ll pos, ll val) {
    if (st > en)
      return;
    if (st == en)
      T[node] = val;
    else {
      ll mid = (st + en) >> 1;
      if (pos > mid)
        update(R(node), mid + 1, en, pos, val);
      else
        update(L(node), st, mid, pos, val);

      T[node] = max(T[L(node)], T[R(node)]);
    }
  }

  ll query(ll node, ll st, ll en, ll pos) {
    if (st > en)
      return -1;
    if (st == en)
      return T[node];
    else {
      ll mid = (st + en) >> 1;
      if (pos > mid)
        return max(T[L(node)], query(R(node), mid + 1, en, pos));
      else
        return query(L(node), st, mid, pos);
    }
  }
};

void solve() {
  SegTree st;
  st.build();

  ll n;
  cin >> n;
  vl h(n);
  rep(i, n) cin >> h[i];

  vl a(n);
  rep(i, n) cin >> a[i];

  ll ans = 0;

  ll N = *max_element(all(h)) + 1;
  // pr(N);
  rep(i, n) {
    ll temp = st.query(0, 0, N, h[i]) + a[i];
    ans = max(temp, ans);
    st.update(0, 0, N, h[i], temp);
  }

  cout << ans << endl;
}

int main() {
  fastio;
  ll t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); i++)
#define NFOR(i, a, b) for (ll i = (ll)(a); i >= (ll)(b); --i)
#define rep(i, a) for (ll i = 0; i < a; i++)
#define endl "\n"
#define fi first
#define se second
#define MOD 1000000007
#define inf 1e18
#define pb push_back
#define Case cout << "Case #" << ++cas << ": ";
#define fastio                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define all(v) v.begin(), v.end()

typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
typedef vector<vl> vvl;

#define pr(...) dbs(#__VA_ARGS__, __VA_ARGS__)
template <class T> void dbs(string str, T t) {
  cerr << str << " : " << t << "\n";
}
template <class T, class... S> void dbs(string str, T t, S... s) {
  int idx = str.find(',');
  cerr << str.substr(0, idx) << " : " << t << ",";
  dbs(str.substr(idx + 1), s...);
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class T> ostream &operator<<(ostream &os, const multiset<T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class S, class T>
ostream &operator<<(ostream &os, const map<S, T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class T> void prc(T a, T b) {
  cerr << "[";
  for (T i = a; i != b; ++i) {
    if (i != a)
      cerr << ", ";
    cerr << *i;
  }
  cerr << "]\n";
}

const int maxn = 200005;

inline ll L(ll a) { return (a << 1) + 1; }
inline ll R(ll a) { return (a << 1) + 2; }

class SegTree {
  ll T[maxn << 2];

public:
  void build() { memset(T, 0, sizeof(T)); }

  void update(ll node, ll st, ll en, ll pos, ll val) {
    if (st > en)
      return;
    if (st == en)
      T[node] = val;
    else {
      ll mid = (st + en) >> 1;
      if (pos > mid)
        update(R(node), mid + 1, en, pos, val);
      else
        update(L(node), st, mid, pos, val);

      T[node] = max(T[L(node)], T[R(node)]);
    }
  }

  ll query(ll node, ll st, ll en, ll pos) {
    if (st > en)
      return -1;
    if (st == en)
      return T[node];
    else {
      ll mid = (st + en) >> 1;
      if (pos > mid)
        return max(T[L(node)], query(R(node), mid + 1, en, pos));
      else
        return query(L(node), st, mid, pos);
    }
  }
};

void solve() {
  SegTree st;
  st.build();

  ll n;
  cin >> n;
  vl h(n);
  rep(i, n) cin >> h[i];

  vl a(n);
  rep(i, n) cin >> a[i];

  ll ans = 0;

  ll N = *max_element(all(h)) + 1;
  // pr(N);
  rep(i, n) {
    ll temp = st.query(0, 0, N, h[i]) + a[i];
    ans = max(temp, ans);
    st.update(0, 0, N, h[i], temp);
  }

  cout << ans << endl;
}

int main() {
  fastio;
  ll t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}