Problem p03790 (C++)

Buggy submission (status: Runtime Error):
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
//                       _oo0oo_                         //
//                      o8888888o                        //
//                      88" . "88      ------ hzt1       //
//                      (| -_- |)                        //
//                      0\  =  /0                        //
//                    ___/`---'\___                      //
//                  .' \|     |// '.                     //
//                 / \|||  :  |||// \                    //
//                / _||||| -:- |||||- \                  //
//               |   | \  -  /// |     |                 //
//               | \_|  ''\---/''  |_/ |                 //
//               \  .-\__  '-'  ___/-. /                 //
//             ___'. .'  /--.--\  `. .'___               //
//          ."" '<  `.___\_<|>_/___.' >' "".             //
//         | | :  `- \`.;`\ _ /`;.`/ - ` : | |           //
//         \  \ `_.   \_ __\ /__ _/   .-` /  /           //
//     =====`-.____`.___ \_____/___.-`___.-'=====        //
//                       `=---='                         //
//                                                       //
//                                                       //
//     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~       //
//                                                       //
//                 God-He Bless All.                     //
//           This Code Will Never Explode.               //
//                                                       //
//                                                       //
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <vector>
#define pb push_back
#define mp make_pair
#define xx first
#define yy second
#define rep(i, a, b) for (int i = (a), i##_end_ = (b); i <= i##_end_; i++)
#define dwn(i, a, b) for (int i = (a), i##_end_ = (b); i >= i##_end_; i--)
using namespace std;
const int Size = 1 << 16;
char buffer[Size], *head, *tail;
inline char Getchar() {
  if (head == tail) {
    int l = fread(buffer, 1, Size, stdin);
    tail = (head = buffer) + l;
  }
  if (head == tail)
    return -1;
  return *head++;
}
inline int read() {
  int x = 0, f = 1;
  char c = Getchar();
  for (; !isdigit(c); c = Getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = Getchar())
    x = x * 10 + c - '0';
  return x * f;
}
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 100010;
int n, k, sum[maxn], A[maxn], B[maxn];
void GG() {
  puts("-1");
  exit(0);
}
pii p[maxn];
ll ans, f[maxn], g[maxn];
int add(int x, int y) { return ((x + y) % k + k) % k; }
const int maxnode = 3000010;
ll setv[maxn << 2];
void pushdown(int o) {
  int lc = o << 1, rc = lc | 1;
  setv[lc] = setv[rc] = setv[o];
  setv[o] = 0;
}
void update(int x, int l, int r, int ql, int qr, int v) {
  if (ql <= l && r <= qr)
    setv[x] = v;
  else {
    int mid = l + r >> 1, lc = x << 1, rc = lc | 1;
    if (setv[x])
      pushdown(x);
    if (ql <= mid)
      update(lc, l, mid, ql, qr, v);
    if (qr > mid)
      update(rc, mid + 1, r, ql, qr, v);
  }
}
ll query(int x, int l, int r, int p) {
  if (setv[x])
    return setv[x];
  if (l == r)
    return 0;
  int mid = l + r >> 1, lc = x << 1, rc = lc | 1;
  if (p <= mid)
    return query(lc, l, mid, p);
  return query(rc, mid + 1, r, p);
}
int cnt, ql[maxn], qr[maxn], L[maxn], R[maxn];
int Query(int p) { return query(1, 1, cnt, p); }
ll tmp[maxn * 4];
void prework() {
  rep(i, 1, n) tmp[++cnt] = p[i].xx, tmp[++cnt] = p[i].yy,
               tmp[++cnt] = add(p[i].xx, 1), tmp[++cnt] = add(p[i].yy, -1);
  sort(tmp + 1, tmp + cnt + 1);
  rep(i, 1, n) {
    ql[i] = lower_bound(tmp + 1, tmp + cnt + 1, add(p[i].xx, 1)) - tmp;
    qr[i] = lower_bound(tmp + 1, tmp + cnt + 1, add(p[i].yy, -1)) - tmp;
    L[i] = lower_bound(tmp + 1, tmp + cnt + 1, p[i].xx) - tmp;
    R[i] = lower_bound(tmp + 1, tmp + cnt + 1, p[i].yy) - tmp;
  }
}
int main() {
  n = read();
  k = read();
  rep(i, 1, n) {
    ans += A[i] = read();
    B[i] = read();
    sum[i] = (sum[i - 1] + A[i]) % k;
    if (B[i] == 1 && A[i] * 2 > k)
      GG();
    p[i] = mp((k - 2 * sum[i] % k) % k, (k - 2 * sum[i - 1] % k) % k);
  }
  prework();
  dwn(i, n, 1) {
    int j = Query(L[i]);
    if (j)
      f[i] = min(f[j] + add(p[j].xx, -p[i].xx), g[j] + add(p[j].yy, -p[i].xx));
    j = Query(R[i]);
    if (j)
      g[i] = min(f[j] + add(p[j].xx, -p[i].yy), g[j] + add(p[j].yy, -p[i].yy));
    if (B[i] == 1) {
      int l = ql[i], r = qr[i];
      if (l <= r)
        update(1, 1, cnt, l, r, i);
      else
        update(1, 1, cnt, l, cnt, i), update(1, 1, cnt, 1, r, i);
    }
  }
  ll res = 1ll << 60;
  memset(setv, 0, sizeof(setv));
  rep(i, 1, n) {
    if (!Query(L[i]))
      res = min(res, f[i]);
    if (!Query(R[i]))
      res = min(res, g[i]);
    if (B[i] == 1) {
      int l = ql[i], r = qr[i];
      if (l <= r)
        update(1, 1, cnt, l, r, i);
      else
        update(1, 1, cnt, l, cnt, i), update(1, 1, cnt, 1, r, i);
    }
  }
  printf("%lld\n", ans * 2 + res);
  return 0;
}


Fixed submission:
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
//                       _oo0oo_                         //
//                      o8888888o                        //
//                      88" . "88      ------ hzt1       //
//                      (| -_- |)                        //
//                      0\  =  /0                        //
//                    ___/`---'\___                      //
//                  .' \|     |// '.                     //
//                 / \|||  :  |||// \                    //
//                / _||||| -:- |||||- \                  //
//               |   | \  -  /// |     |                 //
//               | \_|  ''\---/''  |_/ |                 //
//               \  .-\__  '-'  ___/-. /                 //
//             ___'. .'  /--.--\  `. .'___               //
//          ."" '<  `.___\_<|>_/___.' >' "".             //
//         | | :  `- \`.;`\ _ /`;.`/ - ` : | |           //
//         \  \ `_.   \_ __\ /__ _/   .-` /  /           //
//     =====`-.____`.___ \_____/___.-`___.-'=====        //
//                       `=---='                         //
//                                                       //
//                                                       //
//     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~       //
//                                                       //
//                 God-He Bless All.                     //
//           This Code Will Never Explode.               //
//                                                       //
//                                                       //
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <vector>
#define pb push_back
#define mp make_pair
#define xx first
#define yy second
#define rep(i, a, b) for (int i = (a), i##_end_ = (b); i <= i##_end_; i++)
#define dwn(i, a, b) for (int i = (a), i##_end_ = (b); i >= i##_end_; i--)
using namespace std;
const int Size = 1 << 16;
char buffer[Size], *head, *tail;
inline char Getchar() {
  if (head == tail) {
    int l = fread(buffer, 1, Size, stdin);
    tail = (head = buffer) + l;
  }
  if (head == tail)
    return -1;
  return *head++;
}
inline int read() {
  int x = 0, f = 1;
  char c = Getchar();
  for (; !isdigit(c); c = Getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = Getchar())
    x = x * 10 + c - '0';
  return x * f;
}
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 100010;
int n, k, sum[maxn], A[maxn], B[maxn];
void GG() {
  puts("-1");
  exit(0);
}
pii p[maxn];
ll ans, f[maxn], g[maxn];
int add(int x, int y) { return ((x + y) % k + k) % k; }
ll setv[maxn << 4];
void pushdown(int o) {
  int lc = o << 1, rc = lc | 1;
  setv[lc] = setv[rc] = setv[o];
  setv[o] = 0;
}
void update(int x, int l, int r, int ql, int qr, int v) {
  if (ql <= l && r <= qr)
    setv[x] = v;
  else {
    int mid = l + r >> 1, lc = x << 1, rc = lc | 1;
    if (setv[x])
      pushdown(x);
    if (ql <= mid)
      update(lc, l, mid, ql, qr, v);
    if (qr > mid)
      update(rc, mid + 1, r, ql, qr, v);
  }
}
ll query(int x, int l, int r, int p) {
  if (setv[x])
    return setv[x];
  if (l == r)
    return 0;
  int mid = l + r >> 1, lc = x << 1, rc = lc | 1;
  if (p <= mid)
    return query(lc, l, mid, p);
  return query(rc, mid + 1, r, p);
}
int cnt, ql[maxn], qr[maxn], L[maxn], R[maxn];
int Query(int p) { return query(1, 1, cnt, p); }
ll tmp[maxn * 4];
void prework() {
  rep(i, 1, n) tmp[++cnt] = p[i].xx, tmp[++cnt] = p[i].yy,
               tmp[++cnt] = add(p[i].xx, 1), tmp[++cnt] = add(p[i].yy, -1);
  sort(tmp + 1, tmp + cnt + 1);
  rep(i, 1, n) {
    ql[i] = lower_bound(tmp + 1, tmp + cnt + 1, add(p[i].xx, 1)) - tmp;
    qr[i] = lower_bound(tmp + 1, tmp + cnt + 1, add(p[i].yy, -1)) - tmp;
    L[i] = lower_bound(tmp + 1, tmp + cnt + 1, p[i].xx) - tmp;
    R[i] = lower_bound(tmp + 1, tmp + cnt + 1, p[i].yy) - tmp;
  }
}
int main() {
  n = read();
  k = read();
  rep(i, 1, n) {
    ans += A[i] = read();
    B[i] = read();
    sum[i] = (sum[i - 1] + A[i]) % k;
    if (B[i] == 1 && A[i] * 2 > k)
      GG();
    p[i] = mp((k - 2 * sum[i] % k) % k, (k - 2 * sum[i - 1] % k) % k);
  }
  prework();
  dwn(i, n, 1) {
    int j = Query(L[i]);
    if (j)
      f[i] = min(f[j] + add(p[j].xx, -p[i].xx), g[j] + add(p[j].yy, -p[i].xx));
    j = Query(R[i]);
    if (j)
      g[i] = min(f[j] + add(p[j].xx, -p[i].yy), g[j] + add(p[j].yy, -p[i].yy));
    if (B[i] == 1) {
      int l = ql[i], r = qr[i];
      if (l <= r)
        update(1, 1, cnt, l, r, i);
      else
        update(1, 1, cnt, l, cnt, i), update(1, 1, cnt, 1, r, i);
    }
  }
  ll res = 1ll << 60;
  memset(setv, 0, sizeof(setv));
  rep(i, 1, n) {
    if (!Query(L[i]))
      res = min(res, f[i]);
    if (!Query(R[i]))
      res = min(res, g[i]);
    if (B[i] == 1) {
      int l = ql[i], r = qr[i];
      if (l <= r)
        update(1, 1, cnt, l, r, i);
      else
        update(1, 1, cnt, l, cnt, i), update(1, 1, cnt, 1, r, i);
    }
  }
  printf("%lld\n", ans * 2 + res);
  return 0;
}