Problem p03677 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define each(a, b) for (auto(a) : (b))
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define svec(v)                                                                \
  cout << #v << ":";                                                           \
  rep(kbrni, v.size()) cout << " " << v[kbrni];                                \
  cout << endl
#define sset(s)                                                                \
  cout << #s << ":";                                                           \
  each(kbrni, s) cout << " " << kbrni;                                         \
  cout << endl

using namespace std;

typedef pair<int, int> P;

const int MAX_N = 100005;

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> a(n);
  vector<ll> imos(m + 10, 0);
  rep(i, n) { cin >> a[i]; }
  rep(i, n - 1) {
    if (a[i + 1] > a[i]) {
      if (a[i + 1] > a[i] + 1) {
        imos[a[i] + 4]++;
        imos[a[i + 1] + 3] -= a[i + 1] - a[i];
        imos[a[i + 1] + 4] += a[i + 1] - a[i] - 1;
      }
    } else {
      if (m - a[i] > 1) {
        imos[a[i] + 4]++;
        imos[m + 3] -= m - a[i];
        imos[m + 4] += m - a[i] - 1;
      }
      imos[1] += m - a[i] - 1;
      imos[2] -= m - a[i] - 1;
      imos[3]++;
      imos[a[i + 1] + 3] -= m - a[i] + a[i + 1];
      imos[a[i + 1] + 4] += m - a[i] + a[i + 1] - 1;
    }
  }
  rep(i, m + 5) { imos[i + 1] += imos[i]; }
  rep(i, m + 5) { imos[i + 1] += imos[i]; }
  ll sm = 0;
  rep(i, n - 1) {
    if (a[i + 1] > a[i]) {
      sm += a[i + 1] - a[i];
    } else {
      sm += m - a[i] + a[i + 1];
    }
  }
  ll mx = 0;
  for (int i = 3; i <= n + 2; i++) {
    mx = max(imos[i], mx);
  }
  cout << sm - mx << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define each(a, b) for (auto(a) : (b))
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define svec(v)                                                                \
  cout << #v << ":";                                                           \
  rep(kbrni, v.size()) cout << " " << v[kbrni];                                \
  cout << endl
#define sset(s)                                                                \
  cout << #s << ":";                                                           \
  each(kbrni, s) cout << " " << kbrni;                                         \
  cout << endl

using namespace std;

typedef pair<int, int> P;

const int MAX_N = 100005;

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> a(n);
  vector<ll> imos(m + 10, 0);
  rep(i, n) { cin >> a[i]; }
  rep(i, n - 1) {
    if (a[i + 1] > a[i]) {
      if (a[i + 1] > a[i] + 1) {
        imos[a[i] + 4]++;
        imos[a[i + 1] + 3] -= a[i + 1] - a[i];
        imos[a[i + 1] + 4] += a[i + 1] - a[i] - 1;
      }
    } else {
      if (m - a[i] > 1) {
        imos[a[i] + 4]++;
        imos[m + 3] -= m - a[i];
        imos[m + 4] += m - a[i] - 1;
      }
      imos[1] += m - a[i] - 1;
      imos[2] -= m - a[i] - 1;
      imos[3]++;
      imos[a[i + 1] + 3] -= m - a[i] + a[i + 1];
      imos[a[i + 1] + 4] += m - a[i] + a[i + 1] - 1;
    }
  }
  rep(i, m + 5) { imos[i + 1] += imos[i]; }
  rep(i, m + 5) { imos[i + 1] += imos[i]; }
  ll sm = 0;
  rep(i, n - 1) {
    if (a[i + 1] > a[i]) {
      sm += a[i + 1] - a[i];
    } else {
      sm += m - a[i] + a[i + 1];
    }
  }
  ll mx = 0;
  for (int i = 3; i <= m + 2; i++) {
    mx = max(imos[i], mx);
  }
  cout << sm - mx << "\n";
  return 0;
}