Problem p03176 (C++)

Buggy submission (status: Runtime Error):
//
//  ROIGold.cpp
//  Main calisma
//
//  Created by Rakhman on 05/02/2019.
//  Copyright © 2019 Rakhman. All rights reserved.
//

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

#define ios ios_base::sync_with_stdio(0), cout.tie(0), cin.tie(0);
#define S second
#define F first
#define pb push_back
#define nl '\n'
#define NL cout << '\n';
#define EX exit(0)
#define all(s) s.begin(), s.end()
#define no_answer return cout << "NO", 0;
#define FOR(i, start, finish, k) for (int i = start; i <= finish; i += k)

const int MXN = 1e5 + 200;
const long long MNN = 3e3 + 200;
const long long MOD = 1e9 + 7;
const long long INF = 1e13;
const int OO = 1e9 + 500;

typedef long long llong;
typedef unsigned long long ullong;

using namespace std;

llong n, k, h[MXN], a[MXN], dp[MXN], ans;
llong t[MXN * 4];

llong get(int v, int tl, int tr, int L, int R) {
  if (L <= tl && tr <= R) {
    return t[v];
  }
  if (L > tr || tl > R) {
    return 0;
  }
  int tm = (tl + tr) / 2;
  return max(get(v * 2, tl, tm, L, R), get(v * 2 + 1, tm + 1, tr, L, R));
}

void upd(llong v, llong tl, llong tr, llong ind, llong x) {
  if (tl == tr) {
    t[v] = x;
    return;
  }
  int tm = (tl + tr) / 2;
  if (ind <= tm) {
    upd(v * 2, tl, tm, ind, x);
  } else {
    upd(v * 2 + 1, tm + 1, tr, ind, x);
  }
  t[v] = max(t[v * 2], t[v * 2 + 1]);
}

int main() {
  ios;
  cin >> n;
  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[i] = get(1, 1, n, 1, h[i] - 1) + a[i];
    ans = max(dp[i], ans);
    upd(1, 1, n, h[i], dp[i]);
  }
  cout << ans;
}


Fixed submission:
//
//  ROIGold.cpp
//  Main calisma
//
//  Created by Rakhman on 05/02/2019.
//  Copyright © 2019 Rakhman. All rights reserved.
//

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

#define ios ios_base::sync_with_stdio(0), cout.tie(0), cin.tie(0);
#define S second
#define F first
#define pb push_back
#define nl '\n'
#define NL cout << '\n';
#define EX exit(0)
#define all(s) s.begin(), s.end()
#define no_answer return cout << "NO", 0;
#define FOR(i, start, finish, k) for (int i = start; i <= finish; i += k)

const int MXN = 2e5 + 200;
const long long MNN = 3e3 + 200;
const long long MOD = 1e9 + 7;
const long long INF = 1e13;
const int OO = 1e9 + 500;

typedef long long llong;
typedef unsigned long long ullong;

using namespace std;

llong n, k, h[MXN], a[MXN], dp[MXN], ans;
llong t[MXN * 4];

llong get(int v, int tl, int tr, int L, int R) {
  if (L <= tl && tr <= R) {
    return t[v];
  }
  if (L > tr || tl > R) {
    return 0;
  }
  int tm = (tl + tr) / 2;
  return max(get(v * 2, tl, tm, L, R), get(v * 2 + 1, tm + 1, tr, L, R));
}

void upd(llong v, llong tl, llong tr, llong ind, llong x) {
  if (tl == tr) {
    t[v] = x;
    return;
  }
  int tm = (tl + tr) / 2;
  if (ind <= tm) {
    upd(v * 2, tl, tm, ind, x);
  } else {
    upd(v * 2 + 1, tm + 1, tr, ind, x);
  }
  t[v] = max(t[v * 2], t[v * 2 + 1]);
}

int main() {
  ios;
  cin >> n;
  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[i] = get(1, 1, n, 1, h[i] - 1) + a[i];
    ans = max(dp[i], ans);
    upd(1, 1, n, h[i], dp[i]);
  }
  cout << ans;
}