Problem p03677 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define SORT(x) sort((x).begin(), (x).end())
#define ALL(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < n; i++)
#define reps(i, m, n) for (ll i = m; i < n; i++)
#define repr(i, m, n) for (ll i = m; i >= n; i--)
#define de(x) cout << #x << "=" << x << endl;
template <class T> bool maxi(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool mini(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define dame                                                                   \
  {                                                                            \
    cout << "NO"                                                               \
         << "\n";                                                              \
    return;                                                                    \
  }
#define INF2 1000000000000000037
#define INF 1000000007
#define MOD 1000000007
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ld, ll>;
//--GLOBAL---------------------------------

//--MAIN-----------------------------------
void Main() {
  ll N, m;
  cin >> N >> m;
  vector<ll> A(N);
  rep(i, N) {
    cin >> A[i];
    --A[i];
  }
  ll sum = 0;
  rep(i, N - 1) {
    if (A[i] < A[i + 1])
      sum += A[i + 1] - A[i];
    else
      sum += A[i + 1] + m - A[i];
  }
  //(a->b]で 0,1,2,3... 0,0
  vector<ll> pot(m * 2 + 2, 0);
  vector<ll> hole(m * 2 + 2, 0);
  rep(i, N - 1) {
    if (A[i] < A[i + 1]) {
      ++pot[A[i] + 2];
      --pot[A[i + 1] + 1];
      hole[A[i + 1] + 1] -= A[i + 1] - A[i] - 1;
    } else {
      ++pot[A[i] + 2];
      --pot[A[i + 1] + m + 1];
      hole[A[i + 1] + m + 1] -= A[i + 1] + m - A[i] - 1;
    }
  }
  vector<ll> ans(m * 2, 0);
  ll poten = 0;
  ll now = 0;
  rep(i, m * 2) {
    poten += pot[i];
    now += poten;
    now += hole[i];
    ans[i] = now;
  }
  rep(i, m) A[i] += A[i + m];
  ll saidai = *(max_element(ALL(ans)));
  cout << sum - saidai << "\n";
}

//--START----------------------------------
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
}
//-----------------------------------------

Fixed submission:
#include <bits/stdc++.h>
#define SORT(x) sort((x).begin(), (x).end())
#define ALL(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < n; i++)
#define reps(i, m, n) for (ll i = m; i < n; i++)
#define repr(i, m, n) for (ll i = m; i >= n; i--)
#define de(x) cout << #x << "=" << x << endl;
template <class T> bool maxi(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool mini(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define dame                                                                   \
  {                                                                            \
    cout << "NO"                                                               \
         << "\n";                                                              \
    return;                                                                    \
  }
#define INF2 1000000000000000037
#define INF 1000000007
#define MOD 1000000007
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ld, ll>;
//--GLOBAL---------------------------------

//--MAIN-----------------------------------
void Main() {
  ll N, m;
  cin >> N >> m;
  vector<ll> A(N);
  rep(i, N) {
    cin >> A[i];
    --A[i];
  }
  ll sum = 0;
  rep(i, N - 1) {
    if (A[i] < A[i + 1])
      sum += A[i + 1] - A[i];
    else
      sum += A[i + 1] + m - A[i];
  }
  //(a->b]で 0,1,2,3... 0,0
  vector<ll> pot(m * 2 + 2, 0);
  vector<ll> hole(m * 2 + 2, 0);
  rep(i, N - 1) {
    if (A[i] < A[i + 1]) {
      ++pot[A[i] + 2];
      --pot[A[i + 1] + 1];
      hole[A[i + 1] + 1] -= A[i + 1] - A[i] - 1;
    } else {
      ++pot[A[i] + 2];
      --pot[A[i + 1] + m + 1];
      hole[A[i + 1] + m + 1] -= A[i + 1] + m - A[i] - 1;
    }
  }
  vector<ll> ans(m * 2, 0);
  ll poten = 0;
  ll now = 0;
  rep(i, m * 2) {
    poten += pot[i];
    now += poten;
    now += hole[i];
    ans[i] = now;
  }
  rep(i, m) ans[i] += ans[i + m];
  ll saidai = *(max_element(ALL(ans)));
  cout << sum - saidai << "\n";
}

//--START----------------------------------
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
}
//-----------------------------------------