Problem p00486 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;

#define EACH(i, a) for (auto &i : a)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; i--)
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define ALL(a) (a).begin(), (a).end()

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  EACH(x, vec) is >> x;
  return is;
}
/*
template<class... T>
ostream& operator<<(ostream& os, tuple<T...>& t) {
        for (size_t i = 0; i < tuple_size< tuple<T...> >::value; ++i) {
                if (i) os << " ";
                os << get<0>(t);
        }
        return os;
}
*/
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << " ";
    os << vec[i];
  }
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << endl;
    os << vec[i];
  }
  return os;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  int W, H;
  cin >> W >> H;
  int N;
  cin >> N;
  vector<int> x(N), y(N);
  REP(i, N) { cin >> x[i] >> y[i]; }
  int xl = 0, xr = W;

  vector<int> X = x, Y = y;
  sort(ALL(X));
  sort(ALL(Y));
  int idx = (N - 1) / 2;
  ll ans = linf;
  int ax = inf, ay = inf;
  vector<int> kx, ky;
  for (int d = -10; d <= 10; ++d) {
    if (idx + d < 0 || idx + d >= N)
      continue;
    kx.pb(X[idx + d]);
    ky.pb(Y[idx + d]);
  }
  REP(u, kx.size()) REP(w, ky.size()) {
    int sx = kx[u], sy = ky[w];
    vector<ll> a;
    REP(i, N) { a.pb(abs(x[i] - sx) + abs(y[i] - sy)); }
    sort(ALL(a));
    ll sum = a[N - 1];
    REP(i, N - 1) { sum += a[i] * 2; }
    if (sum < ans || (sum == ans && P(sx, sy) < P(ax, ay))) {
      ans = sum;
      tie(ax, ay) = P(sx, sy);
    }
  }
  cout << ans << endl;
  cout << ax << " " << ay << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;

#define EACH(i, a) for (auto &i : a)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; i--)
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define ALL(a) (a).begin(), (a).end()

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  EACH(x, vec) is >> x;
  return is;
}
/*
template<class... T>
ostream& operator<<(ostream& os, tuple<T...>& t) {
        for (size_t i = 0; i < tuple_size< tuple<T...> >::value; ++i) {
                if (i) os << " ";
                os << get<0>(t);
        }
        return os;
}
*/
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << " ";
    os << vec[i];
  }
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << endl;
    os << vec[i];
  }
  return os;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  int W, H;
  cin >> W >> H;
  int N;
  cin >> N;
  vector<int> x(N), y(N);
  REP(i, N) { cin >> x[i] >> y[i]; }
  int xl = 0, xr = W;

  vector<int> X = x, Y = y;
  sort(ALL(X));
  sort(ALL(Y));
  int idx = (N - 1) / 2;
  ll ans = linf;
  int ax = inf, ay = inf;
  vector<int> kx, ky;
  for (int d = -1000; d <= 1000; ++d) {
    if (idx + d < 0 || idx + d >= N)
      continue;
    kx.pb(X[idx + d]);
    ky.pb(Y[idx + d]);
  }
  REP(u, kx.size()) REP(w, ky.size()) {
    int sx = kx[u], sy = ky[w];
    vector<ll> a;
    REP(i, N) { a.pb(abs(x[i] - sx) + abs(y[i] - sy)); }
    sort(ALL(a));
    ll sum = a[N - 1];
    REP(i, N - 1) { sum += a[i] * 2; }
    if (sum < ans || (sum == ans && P(sx, sy) < P(ax, ay))) {
      ans = sum;
      tie(ax, ay) = P(sx, sy);
    }
  }
  cout << ans << endl;
  cout << ax << " " << ay << endl;
}