Problem p00486 (C++)

Buggy submission (status: Memory Limit Exceeded):
// JOI2010-2011本選問４歩くサンタクロース(Walking Santa)
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
int main() {

  long long sum = 0LL, osum = 0LL;
  long long f1, f2, f3, w, h, L, HL, temp, oxy[2][2], *LXY[2], ox, oy, max = 0,
                                                                       dx, dy;
  vector<long long> XY[2];
  freopen("in/18-02.in", "r", stdin);
  scanf("%lld %lld", &w, &h);
  scanf("%lld", &L);
  for (f1 = 0; f1 < 2; f1++)
    LXY[f1] = new long long[L];
  // 100k
  for (f1 = 0; f1 < L; f1++) {
    scanf("%lld %lld", &LXY[0][f1], &LXY[1][f1]);
    XY[0].push_back(LXY[0][f1]);
    XY[1].push_back(LXY[1][f1]);
  }
  // 1m
  stable_sort(XY[0].begin(), XY[0].end());
  stable_sort(XY[1].begin(), XY[1].end());
  HL = (L + 1) / 2;
  // x軸での最小値の点、y軸での最小値の点を探す
  // 200k
  if (L == 1) {
    printf("0\n");
    printf("%lld %lld\n", LXY[0][0], LXY[1][0]);
  } else {
    for (f1 = 0; f1 < 2; f1++) {
      oxy[0][f1] = XY[f1][HL - 1];
      oxy[1][f1] = XY[f1][HL];
    }
    dx = (oxy[0][0] == oxy[1][0]) ? 1 : 2;
    dy = (oxy[0][1] == oxy[1][1]) ? 1 : 2;
    // 400k
    for (f2 = 0; f2 < dx; f2++) {
      for (f3 = 0; f3 < dy; f3++) {
        sum = 0LL;
        max = 0LL;
        for (f1 = 0; f1 < L; f1++) {
          temp = (long long)abs(LXY[0][f1] - oxy[f2][0]) +
                 abs(LXY[1][f1] - oxy[f3][1]);
          sum += temp;
          if (max < temp)
            max = temp;
        }
        sum *= 2;
        sum -= max;
        if (osum == 0 || osum > sum) {
          osum = sum;
          ox = oxy[f2][0];
          oy = oxy[f3][1];
        }
      }
    }
    printf("%lld\n", osum);
    printf("%lld %lld\n", ox, oy);
  }
  return 0;
}

Fixed submission:
// JOI2010-2011本選問４歩くサンタクロース(Walking Santa)
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
int main() {

  long long sum = 0LL, osum = 0LL;
  long long f1, f2, f3, w, h, L, HL, temp, oxy[2][2], *LXY[2], ox, oy, max = 0,
                                                                       dx, dy;
  vector<long long> XY[2];
  scanf("%lld %lld", &w, &h);
  scanf("%lld", &L);
  for (f1 = 0; f1 < 2; f1++)
    LXY[f1] = new long long[L];
  // 100k
  for (f1 = 0; f1 < L; f1++) {
    scanf("%lld %lld", &LXY[0][f1], &LXY[1][f1]);
    XY[0].push_back(LXY[0][f1]);
    XY[1].push_back(LXY[1][f1]);
  }
  // 1m
  stable_sort(XY[0].begin(), XY[0].end());
  stable_sort(XY[1].begin(), XY[1].end());
  HL = (L + 1) / 2;
  // x軸での最小値の点、y軸での最小値の点を探す
  // 200k
  if (L == 1) {
    printf("0\n");
    printf("%lld %lld\n", LXY[0][0], LXY[1][0]);
  } else {
    for (f1 = 0; f1 < 2; f1++) {
      oxy[0][f1] = XY[f1][HL - 1];
      oxy[1][f1] = XY[f1][HL];
    }
    dx = (oxy[0][0] == oxy[1][0]) ? 1 : 2;
    dy = (oxy[0][1] == oxy[1][1]) ? 1 : 2;
    // 400k
    for (f2 = 0; f2 < dx; f2++) {
      for (f3 = 0; f3 < dy; f3++) {
        sum = 0LL;
        max = 0LL;
        for (f1 = 0; f1 < L; f1++) {
          temp = (long long)abs(LXY[0][f1] - oxy[f2][0]) +
                 abs(LXY[1][f1] - oxy[f3][1]);
          sum += temp;
          if (max < temp)
            max = temp;
        }
        sum *= 2;
        sum -= max;
        if (osum == 0 || osum > sum) {
          osum = sum;
          ox = oxy[f2][0];
          oy = oxy[f3][1];
        }
      }
    }
    printf("%lld\n", osum);
    printf("%lld %lld\n", ox, oy);
  }
  return 0;
}