Problem p00486 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"
using namespace std;

#define int long long

#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, x, ...) x
#define REP1(i, s, cond, cal) for (signed i = signed(s); i cond; i cal)
#define REP2(i, s, n) REP1(i, s, < signed(n), ++)
#define REP3(i, n) REP2(i, 0, n)
#define rep(...) CHOOSE((__VA_ARGS__, REP1, REP2, REP3))(__VA_ARGS__)
#define rrep(i, s) rep(i, s, >= 0, --)

#define all(c) begin(c), end(c)
template <typename T> bool maxup(T &a, const T &&b) {
  if (a < b) {
    a = b;
    return true;
  };
}
template <typename T> bool maxup(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  };
}
template <typename T> bool minup(T &a, const T &&b) {
  if (a > b) {
    a = b;
    return true;
  };
}
template <typename T> bool minup(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  };
}

#define X first
#define Y second

using VV = vector<vector<int>>;
using V = vector<int>;
using P = pair<int, int>;
using IP = pair<int, P>;

template <typename T> inline void input(vector<T> &v) {
  for (auto &x : v)
    cin >> x;
}

signed main() {
  int W, H, N;
  cin >> W >> H >> N;
  V ws(N), hs(N);
  vector<P> v(N);
  rep(i, N) {
    int x, y;
    cin >> x >> y;
    ws[i] = x;
    hs[i] = y;
    v[i] = P(x, y);
  }
  sort(all(ws));
  sort(all(hs));

  int xi = N / 2;
  int yi = N / 2;

  VV dists(9, V(N));
  rep(i, N) rep(j, 3) rep(k, 3) {
    dists[j * 3 + k][i] =
        abs(v[i].first - ws[xi + j - 1]) + abs(v[i].second - hs[yi + k - 1]);
  }
  rep(i, 9) sort(all(dists[i]));

  int ans = 1ll << 62;
  int xpos = 0, ypos = 0;
  for (int i = 0; i < 9; i++) {
    int sum = accumulate(all(dists[i]), 0ll) * 2 - dists[i].back();
    if (sum < ans) {
      xpos = ws[xi + i / 3 - 1];
      ypos = hs[yi + i % 3 - 1];
      ans = sum;
    }
  }
  cout << ans << endl;
  cout << xpos << " " << ypos << endl;

  system("pause");
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"
using namespace std;

#define int long long

#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, x, ...) x
#define REP1(i, s, cond, cal) for (signed i = signed(s); i cond; i cal)
#define REP2(i, s, n) REP1(i, s, < signed(n), ++)
#define REP3(i, n) REP2(i, 0, n)
#define rep(...) CHOOSE((__VA_ARGS__, REP1, REP2, REP3))(__VA_ARGS__)
#define rrep(i, s) rep(i, s, >= 0, --)

#define all(c) begin(c), end(c)
template <typename T> bool maxup(T &a, const T &&b) {
  if (a < b) {
    a = b;
    return true;
  };
}
template <typename T> bool maxup(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  };
}
template <typename T> bool minup(T &a, const T &&b) {
  if (a > b) {
    a = b;
    return true;
  };
}
template <typename T> bool minup(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  };
}

#define X first
#define Y second

using VV = vector<vector<int>>;
using V = vector<int>;
using P = pair<int, int>;
using IP = pair<int, P>;

template <typename T> inline void input(vector<T> &v) {
  for (auto &x : v)
    cin >> x;
}

signed main() {
  int W, H, N;
  cin >> W >> H >> N;
  V ws(N), hs(N);
  vector<P> v(N);
  rep(i, N) {
    int x, y;
    cin >> x >> y;
    ws[i] = x;
    hs[i] = y;
    v[i] = P(x, y);
  }
  sort(all(ws));
  sort(all(hs));

  int xi = N / 2;
  int yi = N / 2;

  VV dists(9, V(N));
  rep(i, N) rep(j, 3) rep(k, 3) {
    dists[j * 3 + k][i] =
        abs(v[i].first - ws[xi + j - 1]) + abs(v[i].second - hs[yi + k - 1]);
  }
  rep(i, 9) sort(all(dists[i]));

  int ans = 1ll << 62;
  int xpos = 0, ypos = 0;
  for (int i = 0; i < 9; i++) {
    int sum = accumulate(all(dists[i]), 0ll) * 2 - dists[i].back();
    if (sum < ans) {
      xpos = ws[xi + i / 3 - 1];
      ypos = hs[yi + i % 3 - 1];
      ans = sum;
    }
  }
  cout << ans << endl;
  cout << xpos << " " << ypos << endl;

  // system("pause");
}