Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;
using ld = long double;

#define st first
#define nd second

const int MAXN = 1e5 + 5, inf = 1e9;
const ll INF = 1e18, mod = 1e9 + 7;
const ld PI = 3.1415926535897932384626433832795;

int cnt[MAXN];
ll dp[MAXN], h[MAXN], a[MAXN];
bool vis[MAXN], used[MAXN];

vector<int> G[MAXN];
vector<ll> V;
set<ll> S;
map<ll, int> M;
stack<ll> St;
queue<ll> Q;

ll Tree[2 * 1024 * 1024], Base = 1;

void chTree(int node, ll v) {
  node += Base;
  Tree[node] = v;
  while (node /= 2) {
    Tree[node] = max(Tree[2 * node], Tree[2 * node + 1]);
  }
}

ll read(int node, int lo, int hi, int l, int r) {
  if (r < lo or hi < l) {
    return 0;
  }
  if (l <= lo and hi <= r) {
    return Tree[node];
  }

  int mid = (lo + hi) / 2;
  return max(read(2 * node, lo, mid, l, r),
             read(2 * node + 1, mid + 1, hi, l, r));
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << setprecision(13) << fixed;

  //////////////////////////////////////////
  //////////////////////////////////////////

  int n;
  cin >> n;
  while (Base <= n) {
    Base *= 2;
  }

  for (int i = 1; i <= n; i++) {
    cin >> h[i];
  }

  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  for (int i = 1; i <= n; i++) {
    dp[h[i]] = read(1, 0, Base - 1, 1, h[i]) + a[i];
    chTree(h[i], dp[h[i]]);
  }

  ll res = 0;
  for (int i = 1; i <= n; i++) {
    // cout << dp[i] << " ";
    res = max(res, dp[i]);
  }
  // cout << "\n";
  cout << res << "\n";
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;
using ld = long double;

#define st first
#define nd second

const int MAXN = 2e5 + 5, inf = 1e9;
const ll INF = 1e18, mod = 1e9 + 7;
const ld PI = 3.1415926535897932384626433832795;

int cnt[MAXN];
ll dp[MAXN], h[MAXN], a[MAXN];
bool vis[MAXN], used[MAXN];

vector<int> G[MAXN];
vector<ll> V;
set<ll> S;
map<ll, int> M;
stack<ll> St;
queue<ll> Q;

ll Tree[2 * 1024 * 1024], Base = 1;

void chTree(int node, ll v) {
  node += Base;
  Tree[node] = v;
  while (node /= 2) {
    Tree[node] = max(Tree[2 * node], Tree[2 * node + 1]);
  }
}

ll read(int node, int lo, int hi, int l, int r) {
  if (r < lo or hi < l) {
    return 0;
  }
  if (l <= lo and hi <= r) {
    return Tree[node];
  }

  int mid = (lo + hi) / 2;
  return max(read(2 * node, lo, mid, l, r),
             read(2 * node + 1, mid + 1, hi, l, r));
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << setprecision(13) << fixed;

  //////////////////////////////////////////
  //////////////////////////////////////////

  int n;
  cin >> n;
  while (Base <= n) {
    Base *= 2;
  }

  for (int i = 1; i <= n; i++) {
    cin >> h[i];
  }

  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  for (int i = 1; i <= n; i++) {
    dp[h[i]] = read(1, 0, Base - 1, 1, h[i]) + a[i];
    chTree(h[i], dp[h[i]]);
  }

  ll res = 0;
  for (int i = 1; i <= n; i++) {
    // cout << dp[i] << " ";
    res = max(res, dp[i]);
  }
  // cout << "\n";
  cout << res << "\n";
}