Problem p00462 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype

template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

template <class T> void max_swap(T &a, const T b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T b) { a = min(a, b); }

const double EPS = 1e-10;
typedef long long ll;
typedef pair<int, int> pint;

int main() {
  int round;
  while (scanf("%d", &round), round) {
    int n, m;
    scanf("%d%d", &n, &m);

    static int d[10100];
    d[0] = 0;
    for (int i = 1; i < n; ++i)
      scanf("%d", d + i);
    sort(d, d + n);

    int res = 0;
    while (m--) {
      int k;
      scanf("%d", &k);
      int i = lower_bound(d, d + n, k) - d;

      int dis = 1e9 + 10;
      if (i > 0)
        min_swap(dis, k - d[i - 1]);
      if (i < n)
        min_swap(dis, d[i] - k);
      min_swap(dis, round - k);

      res += dis;
    }
    printf("%d\n", res);
  }
}

Fixed submission:
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype

template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

template <class T> void max_swap(T &a, const T b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T b) { a = min(a, b); }

const double EPS = 1e-10;
typedef long long ll;
typedef pair<int, int> pint;

int main() {
  int round;
  while (scanf("%d", &round), round) {
    int n, m;
    scanf("%d%d", &n, &m);

    static int d[100100];
    d[0] = 0;
    for (int i = 1; i < n; ++i)
      scanf("%d", d + i);
    sort(d, d + n);

    int res = 0;
    while (m--) {
      int k;
      scanf("%d", &k);
      int i = lower_bound(d, d + n, k) - d;

      int dis = 1e9 + 10;
      if (i > 0)
        min_swap(dis, k - d[i - 1]);
      if (i < n)
        min_swap(dis, d[i] - k);
      min_swap(dis, round - k);

      res += dis;
    }
    printf("%d\n", res);
  }
}