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

// Bokan ga bokka--nn!!
// Daily Lunch Special Tanoshii !!
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
typedef long long ll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
#define s(x) scanf("%d", &x)
#define rep(i, x) for (int i = 0; i < x; i++)
int n, m, ret = 0;
int in[100005];
int v[100005] = {};
vector<int> val;
priority_queue<P, vector<P>, greater<P>> que;
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) {
    int f;
    scanf("%d", &f);
    val.pb(f);
  }
  for (int i = 0; i < n; i++) {
    bool x = 0, y = 0;
    if (!i)
      x = 1;
    else if (val[i] > val[i - 1])
      x = 1;

    if (i == n - 1)
      y = 1;
    else if (val[i] > val[i + 1])
      y = 1;

    v[i] = 2 - x - y;
    if (x && y) {
      que.push(mp(m - val[i], i));
      in[i] = 0;
    }
  }
  while (!que.empty()) {

    P p = que.top();
    que.pop();
    int x = p.first;
    int id = p.second;
    ret = x;
    if (id && val[id] > val[id - 1]) {
      v[id - 1]--;
      if (!v[id - 1])
        que.push(mp(ret + m - val[id - 1], id - 1));
    }
    if (id != n - 1 && val[id] > val[id + 1]) {
      v[id + 1]--;
      if (!v[id + 1])
        que.push(mp(ret + m - val[id + 1], id + 1));
    }
  }

  printf("%d\\n", ret);
}

Error: 0

Fixed submission:

// Bokan ga bokka--nn!!
// Daily Lunch Special Tanoshii !!
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
typedef long long ll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
#define s(x) scanf("%d", &x)
#define rep(i, x) for (int i = 0; i < x; i++)
int n, m, ret = 0;
int in[100005];
int v[100005] = {};
vector<int> val;
priority_queue<P, vector<P>, greater<P>> que;
int rec(priority_queue<P, vector<P>, greater<P>> cur) {
  if (cur.empty())
    return ret;
  P p = cur.top();
  cur.pop();
  int x = p.first;
  int id = p.second;
  ret = x;
  if (id && val[id] > val[id - 1]) {
    v[id - 1]--;
    if (!v[id - 1])
      cur.push(mp(ret + m - val[id - 1], id - 1));
  }
  if (id != n - 1 && val[id] > val[id + 1]) {
    v[id + 1]--;
    if (!v[id + 1])
      cur.push(mp(ret + m - val[id + 1], id + 1));
  }
  return rec(cur);
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) {
    int f;
    scanf("%d", &f);
    val.pb(f);
  }
  for (int i = 0; i < n; i++) {
    bool x = 0, y = 0;
    if (!i)
      x = 1;
    else if (val[i] > val[i - 1])
      x = 1;

    if (i == n - 1)
      y = 1;
    else if (val[i] > val[i + 1])
      y = 1;

    v[i] = 2 - x - y;
    if (x && y) {
      que.push(mp(m - val[i], i));
      in[i] = 0;
    }
  }
  printf("%d\\n", rec(que));
}