Problem p03240 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vs = vector<string>;
using pll = pair<ll, ll>;
using vp = vector<pll>;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, a, b) for (ll i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((ll)(x).size())
const ll MOD = 1000000007;
const ll INF = 100000000000000000LL;
inline ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
inline ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }
inline ll powint(unsigned long long x, ll y) {
  ll r = 1;
  while (y) {
    if (y & 1)
      r *= x;
    x *= x;
    y >>= 1;
  }
  return r;
}
inline ll powmod(ll x, ll y, ll m = MOD) {
  ll r = 1;
  while (y) {
    if (y & 1)
      r *= x;
    x *= x;
    r %= m;
    x %= m;
    y >>= 1;
  }
  return r;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#ifdef OJ_LOCAL
#include "dump.hpp"
#else
#define dump(...) ((void)0)
#endif

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  ll n;
  cin >> n;
  vll x(n), y(n), h(n);
  ll c = 0;
  rep(i, n) {
    cin >> x[i] >> y[i] >> h[i];
    if (h[i])
      c++;
  }
  vvvll ans(101, vvll(101, vll(c)));
  vvvll ans2(101, vvll(101, vll(n - c)));
  ll p = 0;
  ll p2 = 0;
  rep(i, n) {
    if (h[i]) {
      rep(j, 101) rep(k, 101) {
        ans[j][k][p] = abs(j - x[i]) + abs(k - y[i]) + h[i];
      }
      p++;
    } else {
      rep(j, 101) rep(k, 101) { ans[j][k][p2] = abs(j - x[i]) + abs(k - y[i]); }
      p2++;
    }
  }
  if (n - c > 0) {
    rep(i, 101) rep(j, 101) {
      sort(ALL(ans[i][j]));
      sort(ALL(ans2[i][j]));
      if (ans[i][j][0] == ans[i][j][c - 1] && ans2[i][j][0] >= ans[i][j][0]) {
        cout << i << " " << j << " " << ans[i][j][0] << endl;
        return 0;
      }
    }
  } else {
    rep(i, 101) rep(j, 101) {
      sort(ALL(ans[i][j]));
      if (ans[i][j][0] == ans[i][j][c - 1]) {
        cout << i << " " << j << " " << ans[i][j][0] << endl;
        return 0;
      }
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vs = vector<string>;
using pll = pair<ll, ll>;
using vp = vector<pll>;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, a, b) for (ll i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((ll)(x).size())
const ll MOD = 1000000007;
const ll INF = 100000000000000000LL;
inline ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
inline ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }
inline ll powint(unsigned long long x, ll y) {
  ll r = 1;
  while (y) {
    if (y & 1)
      r *= x;
    x *= x;
    y >>= 1;
  }
  return r;
}
inline ll powmod(ll x, ll y, ll m = MOD) {
  ll r = 1;
  while (y) {
    if (y & 1)
      r *= x;
    x *= x;
    r %= m;
    x %= m;
    y >>= 1;
  }
  return r;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#ifdef OJ_LOCAL
#include "dump.hpp"
#else
#define dump(...) ((void)0)
#endif

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  ll n;
  cin >> n;
  vll x(n), y(n), h(n);
  ll c = 0;
  rep(i, n) {
    cin >> x[i] >> y[i] >> h[i];
    if (h[i])
      c++;
  }
  vvvll ans(101, vvll(101, vll(c)));
  vvvll ans2(101, vvll(101, vll(n - c)));
  ll p = 0;
  ll p2 = 0;
  rep(i, n) {
    if (h[i]) {
      rep(j, 101) rep(k, 101) {
        ans[j][k][p] = abs(j - x[i]) + abs(k - y[i]) + h[i];
      }
      p++;
    } else {
      rep(j, 101) rep(k, 101) {
        ans2[j][k][p2] = abs(j - x[i]) + abs(k - y[i]);
      }
      p2++;
    }
  }
  if (n - c > 0) {
    rep(i, 101) rep(j, 101) {
      sort(ALL(ans[i][j]));
      sort(ALL(ans2[i][j]));
      if (ans[i][j][0] == ans[i][j][c - 1] && ans2[i][j][0] >= ans[i][j][0]) {
        cout << i << " " << j << " " << ans[i][j][0] << endl;
        return 0;
      }
    }
  } else {
    rep(i, 101) rep(j, 101) {
      sort(ALL(ans[i][j]));
      if (ans[i][j][0] == ans[i][j][c - 1]) {
        cout << i << " " << j << " " << ans[i][j][0] << endl;
        return 0;
      }
    }
  }
  return 0;
}