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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

#define rep(i, N) for (int i = 0; i < N; i++)
#define pb push_back

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<int, ll> i_ll;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
typedef vector<int> vi;
struct edge {
  int v;
  ll w;
};

ll _MOD = 1000000009;
double EPS = 1e-10;
ll INF = LLONG_MAX / 2;

int main() {
  for (;;) {
    int n;
    cin >> n;
    if (!n)
      break;
    vector<int> a(n), b(n);
    rep(u, n) scanf("%d%d", &a[u], &b[u]);
    if (n == 2) {
      cout << abs(a[0] - a[1]) << endl;
      continue;
    }
    vector<i_i> ab;
    vector<int> c;
    rep(u, n) if (b[u] == 1) c.pb(a[u]);
    else ab.pb(i_i(a[u], b[u] - 2));
    int N = ab.size(), M = c.size();
    if (!N) {
      cout << -1 << endl;
      continue;
    }
    sort(ab.begin(), ab.end());
    ab.front().second++;
    ab.back().second++;
    sort(c.begin(), c.end());
    vector<ll> dp(M + 1, INF);
    dp[0] = 0;
    rep(i, N) {
      int a = ab[i].first, b = ab[i].second;
      vector<ll> _dp(M + 1, INF);
      deque<i_ll> d;
      ll sum = 0;
      rep(j, M + 1) {
        if (!d.empty() && j - d.front().first > b)
          d.pop_front();
        while (!d.empty() && d.back().second >= dp[j] - sum)
          d.pop_back();
        d.push_back(i_ll(j, dp[j] - sum));
        if (!d.empty())
          _dp[j] = d.front().second + sum;
        if (j < M)
          sum += abs(c[j] - a);
      }
      dp = _dp;
    }
    ll ans = dp[M] + ab.back().first - ab.front().first;
    if (ans >= INF)
      ans = -1;
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

#define rep(i, N) for (int i = 0; i < N; i++)
#define pb push_back

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<int, ll> i_ll;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
typedef vector<int> vi;
struct edge {
  int v;
  ll w;
};

ll _MOD = 1000000009;
double EPS = 1e-10;
ll INF = LLONG_MAX / 2;

int main() {
  for (;;) {
    int n;
    cin >> n;
    if (!n)
      break;
    vector<int> a(n), b(n);
    rep(u, n) scanf("%d%d", &a[u], &b[u]);
    if (n == 2) {
      cout << abs(a[0] - a[1]) << endl;
      continue;
    }
    vector<i_i> ab;
    vector<int> c;
    rep(u, n) if (b[u] == 1) c.pb(a[u]);
    else ab.pb(i_i(a[u], b[u] - 2));
    int N = ab.size(), M = c.size();
    sort(ab.begin(), ab.end());
    ab.front().second++;
    ab.back().second++;
    sort(c.begin(), c.end());
    vector<ll> dp(M + 1, INF);
    dp[0] = 0;
    rep(i, N) {
      int a = ab[i].first, b = ab[i].second;
      vector<ll> _dp(M + 1, INF);
      deque<i_ll> d;
      ll sum = 0;
      rep(j, M + 1) {
        if (!d.empty() && j - d.front().first > b)
          d.pop_front();
        while (!d.empty() && d.back().second >= dp[j] - sum)
          d.pop_back();
        d.push_back(i_ll(j, dp[j] - sum));
        if (!d.empty())
          _dp[j] = d.front().second + sum;
        if (j < M)
          sum += abs(c[j] - a);
      }
      dp = _dp;
    }
    ll ans = dp[M] + ab.back().first - ab.front().first;
    if (ans >= INF)
      ans = -1;
    cout << ans << endl;
  }
}