Problem p01545 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

const int MAX_N = 200001; // 131072

struct RMQ {
  ll n;
  ll dat[MAX_N * 2 - 1];
  RMQ(ll _n) {
    for (n = 1; n < _n; n *= 2)
      ;
    fill(dat, dat + 2 * n - 1, 0);
  }
  RMQ(vector<ll> v) {
    for (n = 1; n < v.size(); n *= 2)
      ;
    fill(dat, dat + 2 * n - 1, 0);
    copy(ALL(v), dat + n - 1);
    for (ll i = n - 2; i >= 0; --i)
      dat[i] = max(dat[i * 2 + 1], dat[i * 2 + 2]);
  }
  void update(ll k, ll a) {
    k += n - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }
  ll query(ll a, ll b) { return q(a, b, 0, 0, n); }
  ll q(ll a, ll b, ll k, ll l, ll r) {
    if (r <= a || b <= l)
      return 0;
    if (a <= l && r <= b)
      return dat[k];
    return max(q(a, b, k * 2 + 1, l, (l + r) / 2),
               q(a, b, k * 2 + 2, (l + r) / 2, r));
  }
};

int main() {
  int n;
  cin >> n;
  RMQ rmq(n + 1);
  ll hoge = 0;
  ll ans = 0;
  REP(i, n) {
    ll x;
    cin >> x;
    ll a = rmq.query(0, x);
    // cout << x << " " << a << endl;
    rmq.update(x, a + x);
    ans = max(ans, a + x);
    hoge += x;
  }
  cout << hoge - ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

const int MAX_N = 100001; // 131072

struct RMQ {
  int n;
  int dat[MAX_N * 2 - 1];
  RMQ(int _n) {
    for (n = 1; n < _n; n *= 2)
      ;
    fill(dat, dat + 2 * n - 1, 0);
  }
  RMQ(vector<int> v) {
    for (n = 1; n < v.size(); n *= 2)
      ;
    fill(dat, dat + 2 * n - 1, 0);
    copy(ALL(v), dat + n - 1);
    for (int i = n - 2; i >= 0; --i)
      dat[i] = max(dat[i * 2 + 1], dat[i * 2 + 2]);
  }
  void update(int k, int a) {
    k += n - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }
  int query(int a, int b) { return q(a, b, 0, 0, n); }
  int q(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return 0;
    if (a <= l && r <= b)
      return dat[k];
    return max(q(a, b, k * 2 + 1, l, (l + r) / 2),
               q(a, b, k * 2 + 2, (l + r) / 2, r));
  }
};

int main() {
  int n;
  cin >> n;
  RMQ rmq(n + 1);
  int hoge = 0;
  int ans = 0;
  REP(i, n) {
    int x;
    cin >> x;
    int a = rmq.query(0, x);
    // cout << x << " " << a << endl;
    rmq.update(x, a + x);
    ans = max(ans, a + x);
    hoge += x;
  }
  cout << hoge - ans << endl;
}