Problem p00294 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <limits.h>

const long MAX_N = 100000;
const long MAX_M = 10000;
const long FEE = 100;

long next(bool *stations, long N, long current) {
  long i = current + 1;

  if (i >= N) {
    i = 0;
  }

  if (i < 0) {
    i = N - 1;
  }

  while (!stations[i]) {
    i++;

    if (i >= N) {
      i = 0;
    }
  }

  return i;
}

long previous(bool *stations, long N, long current) {
  long i = current - 1;

  if (i >= N) {
    i = 0;
  }

  if (i < 0) {
    i = N - 1;
  }

  while (!stations[i]) {
    i--;

    if (i < 0) {
      i = N - 1;
    }
  }

  return i;
}

int main(void) {
  bool stations[MAX_M] = {false};

  long N = 0;
  long M = 0;
  long p = 0;

  std::cin >> N >> M >> p;

  stations[0] = true;

  for (long i = 0; i < M; i++) {
    long d = 0;

    std::cin >> d;

    long index = (d - p);

    if (index < 0) {
      index += N;
    }

    stations[index] = true;
  }

  long cost = LONG_MAX;

  long m = 0;
  long clockwiseCurrent = 0;
  long counterClockwiseCurrent = 0;

  while (m <= M) {
    long nextStation = next(stations, N, clockwiseCurrent);

    long c = LONG_MAX;

    if (nextStation == 0) {
      c = clockwiseCurrent;
    } else {
      c = 2 * clockwiseCurrent + (N - nextStation);
    }

    cost = std::min(c, cost);

    long previousStation = previous(stations, N, counterClockwiseCurrent);

    if (previousStation == 0) {
      c = N - counterClockwiseCurrent;
    } else {
      c = 2 * (N - counterClockwiseCurrent) + previousStation;
    }

    cost = std::min(c, cost);

    m++;

    clockwiseCurrent = nextStation;
    counterClockwiseCurrent = previousStation;
  }

  std::cout << (cost * FEE) << std::endl;

  return 0;
}

Fixed submission:
#include <iostream>
#include <limits.h>

const long MAX_N = 100000;
const long MAX_M = 10000;
const long FEE = 100;

long next(bool *stations, long N, long current) {
  long i = current + 1;

  if (i >= N) {
    i = 0;
  }

  if (i < 0) {
    i = N - 1;
  }

  while (!stations[i]) {
    i++;

    if (i >= N) {
      i = 0;
    }
  }

  return i;
}

long previous(bool *stations, long N, long current) {
  long i = current - 1;

  if (i >= N) {
    i = 0;
  }

  if (i < 0) {
    i = N - 1;
  }

  while (!stations[i]) {
    i--;

    if (i < 0) {
      i = N - 1;
    }
  }

  return i;
}

int main(void) {
  bool stations[MAX_N] = {false};

  long N = 0;
  long M = 0;
  long p = 0;

  std::cin >> N >> M >> p;

  stations[0] = true;

  for (long i = 0; i < M; i++) {
    long d = 0;

    std::cin >> d;

    long index = (d - p);

    if (index < 0) {
      index += N;
    }

    stations[index] = true;
  }

  long cost = LONG_MAX;

  long m = 0;
  long clockwiseCurrent = 0;
  long counterClockwiseCurrent = 0;

  while (m <= M) {
    long nextStation = next(stations, N, clockwiseCurrent);

    long c = LONG_MAX;

    if (nextStation == 0) {
      c = clockwiseCurrent;
    } else {
      c = 2 * clockwiseCurrent + (N - nextStation);
    }

    cost = std::min(c, cost);

    long previousStation = previous(stations, N, counterClockwiseCurrent);

    if (previousStation == 0) {
      c = N - counterClockwiseCurrent;
    } else {
      c = 2 * (N - counterClockwiseCurrent) + previousStation;
    }

    cost = std::min(c, cost);

    m++;

    clockwiseCurrent = nextStation;
    counterClockwiseCurrent = previousStation;
  }

  std::cout << (cost * FEE) << std::endl;

  return 0;
}