Problem p03176 (C++)

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

vector<pair<long long, int>> item;

vector<long long> t;

void update(int v, int tl, int tr, int pos, long long val) {
  if (tl == tr) {
    t[v] = val;
    return;
  }

  int tm = (tl + tr) >> 1;
  int lc = (v << 1), rc = (v << 1) + 1;

  if (pos <= tm) {
    update(lc, tl, tm, pos, val);
  } else {
    update(rc, tm + 1, tr, pos, val);
  }

  t[v] = max(t[lc], t[rc]);
}

long long get(int v, int tl, int tr, int l, int r) {
  if (l > r) {
    return 0;
  }

  if (l == tl && r == tr) {
    return t[v];
  }

  int tm = (tl + tr) >> 1, lc = (v << 1), rc = (v << 1) + 1;
  return max(get(lc, tl, tm, l, min(tm, r)),
             get(rc, tm + 1, tr, max(tm + 1, l), r));
}

void solve() {
  int n;
  cin >> n;

  item.resize(n);

  for (int i = 0; i < n; i++) {
    cin >> item[i].second;
  }

  for (int i = 0; i < n; i++) {
    cin >> item[i].first;
  }

  int mx = 1e+5 + 1;

  t.assign(4 * mx, 0);

  long long ans = 0;
  for (int i = 0; i < n; i++) {
    long long getmax = get(1, 0, mx - 1, 0, item[i].second - 1);
    long long dp = getmax + item[i].first;
    update(1, 0, mx - 1, item[i].second, dp);
    ans = max(ans, dp);
  }

  cout << ans;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("file.in", "r", stdin);//freopen("file.out", "w", stdout);

  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

vector<pair<long long, int>> item;

vector<long long> t;

void update(int v, int tl, int tr, int pos, long long val) {
  if (tl == tr) {
    t[v] = val;
    return;
  }

  int tm = (tl + tr) >> 1;
  int lc = (v << 1), rc = (v << 1) + 1;

  if (pos <= tm) {
    update(lc, tl, tm, pos, val);
  } else {
    update(rc, tm + 1, tr, pos, val);
  }

  t[v] = max(t[lc], t[rc]);
}

long long get(int v, int tl, int tr, int l, int r) {
  if (l > r) {
    return 0;
  }

  if (l == tl && r == tr) {
    return t[v];
  }

  int tm = (tl + tr) >> 1, lc = (v << 1), rc = (v << 1) + 1;
  return max(get(lc, tl, tm, l, min(tm, r)),
             get(rc, tm + 1, tr, max(tm + 1, l), r));
}

void solve() {
  int n;
  cin >> n;

  item.resize(n);

  for (int i = 0; i < n; i++) {
    cin >> item[i].second;
  }

  for (int i = 0; i < n; i++) {
    cin >> item[i].first;
  }

  int mx = 2 * 1e+5 + 1;

  t.assign(4 * mx, 0);

  long long ans = 0;
  for (int i = 0; i < n; i++) {
    long long getmax = get(1, 0, mx - 1, 0, item[i].second - 1);
    long long dp = getmax + item[i].first;
    update(1, 0, mx - 1, item[i].second, dp);
    ans = max(ans, dp);
  }

  cout << ans;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("file.in", "r", stdin);//freopen("file.out", "w", stdout);

  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}