Problem p03677 (C++)

Buggy submission (status: Runtime Error):
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define INF 0X3F3F3F3F
#define N 200055
#define M 200005
#define LL long long
#define FF(i, a, b) for (int i = a; i <= b; ++i)
#define RR(i, a, b) for (int i = a; i >= b; --i)
#define FJ(i, a, b) for (int i = a; i < b; ++i)
#define SC(x) scanf("%d", &x)
#define SCC(x, y) scanf("%d%d", &x, &y)
#define SCCC(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define SS(x) scanf("%s", x)
#define PR(x) printf("%d\n", x)
#define CL(a, x) memset(a, x, sizeof(a))
#define _P fd[rt]
#define _L fd[rt << 1]
#define _R fd[rt << 1 | 1]
#define MID int mid = ((l + r) >> 1)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mi d + 1, r
#define PII pair<int, int>
#define PLL pair<long long, long long>
#define MP make_pair
#define IN freopen("in.txt", "r", stdin)
#define OUT freopen("out.txt", "w", stdout)
using namespace std;
const int MOD = 1000000007;
const double PI = acos(-1.0);
const double EPS = 1e-8;
inline void II(int &n) {
  char ch = getchar();
  n = 0;
  bool t = 0;
  for (; ch < '0'; ch = getchar())
    if (ch == '-')
      t = 1;
  for (; ch >= '0'; n = n * 10 + ch - '0', ch = getchar())
    ;
  if (t)
    n = -n;
}
inline void OO(int a) {
  if (a < 0) {
    putchar('-');
    a = -a;
  }
  if (a >= 10)
    OO(a / 10);
  putchar(a % 10 + '0');
}
int n, m, a, b;
LL f[N], l[N], r[N], sr[N];
LL tot;
inline void up(LL &x, LL y) {
  if (y > x)
    x = y;
}
int main() {
  //  IN;
  SC(n);
  SC(m);
  SC(a);
  FF(i, 2, n) {
    SC(b);
    if (a > b)
      b += m;
    tot += b - a;
    ++l[a];
    ++r[b];
    sr[b] += b - a - 1;
    a = b > m ? b - m : b;
  }
  LL cur = 0;
  FF(i, 2, m * 2) {
    f[i] = f[i - 1] - sr[i - 1] + cur;
    cur += l[i - 1];
    cur -= r[i];
  }
  LL ans = 0;
  FF(i, 1, m * 2) up(ans, f[i] + f[i + m]);
  ans = tot - ans;
  cout << ans << endl;
  return 0;
}


Fixed submission:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define INF 0X3F3F3F3F
#define N 200055
#define M 200005
#define LL long long
#define FF(i, a, b) for (int i = a; i <= b; ++i)
#define RR(i, a, b) for (int i = a; i >= b; --i)
#define FJ(i, a, b) for (int i = a; i < b; ++i)
#define SC(x) scanf("%d", &x)
#define SCC(x, y) scanf("%d%d", &x, &y)
#define SCCC(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define SS(x) scanf("%s", x)
#define PR(x) printf("%d\n", x)
#define CL(a, x) memset(a, x, sizeof(a))
#define _P fd[rt]
#define _L fd[rt << 1]
#define _R fd[rt << 1 | 1]
#define MID int mid = ((l + r) >> 1)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mi d + 1, r
#define PII pair<int, int>
#define PLL pair<long long, long long>
#define MP make_pair
#define IN freopen("in.txt", "r", stdin)
#define OUT freopen("out.txt", "w", stdout)
using namespace std;
const int MOD = 1000000007;
const double PI = acos(-1.0);
const double EPS = 1e-8;
inline void II(int &n) {
  char ch = getchar();
  n = 0;
  bool t = 0;
  for (; ch < '0'; ch = getchar())
    if (ch == '-')
      t = 1;
  for (; ch >= '0'; n = n * 10 + ch - '0', ch = getchar())
    ;
  if (t)
    n = -n;
}
inline void OO(int a) {
  if (a < 0) {
    putchar('-');
    a = -a;
  }
  if (a >= 10)
    OO(a / 10);
  putchar(a % 10 + '0');
}
int n, m, a, b;
LL f[N], l[N], r[N], sr[N];
LL tot;
inline void up(LL &x, LL y) {
  if (y > x)
    x = y;
}
int main() {
  //  IN;
  SC(n);
  SC(m);
  SC(a);
  FF(i, 2, n) {
    SC(b);
    if (a > b)
      b += m;
    tot += b - a;
    ++l[a];
    ++r[b];
    sr[b] += b - a - 1;
    a = b > m ? b - m : b;
  }
  LL cur = 0;
  FF(i, 2, m * 2) {
    f[i] = f[i - 1] - sr[i - 1] + cur;
    cur += l[i - 1];
    cur -= r[i];
  }
  LL ans = 0;
  FF(i, 1, m) up(ans, f[i] + f[i + m]);
  ans = tot - ans;
  cout << ans << endl;
  return 0;
}