Problem p03240 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vd = vector<double>;
using vvd = vector<vd>;
using vl = vector<ll>;
using vvl = vector<vl>;
using pii = pair<int, int>;
using vs = vector<string>;
#define rep(i, n) range(i, 0, n)
#define range(i, a, n) for (int i = a; i < n; i++)
#define all(a) a.begin(), a.end()
#define LINF ((ll)1ll << 60)
#define INF ((int)1 << 30)
#define EPS (1e-9)
#define MOD (1000000007)
#define fcout(a) cout << setprecision(a) << fixed
#define fs first
#define sc second
#define PI 3.141592653589793

template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) {
  os << "[" << p.first << ", " << p.second << "]";
  return os;
};
template <class S> auto &operator<<(ostream &os, vector<S> t) {
  bool a = 1;
  for (auto s : t) {
    os << (a ? "" : " ") << s;
    a = 0;
  }
  return os;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vvi ans(101, vi(101, INF));
  vvi data;
  rep(i, n) {
    int x, y, h;
    cin >> x >> y >> h;
    data.push_back(vi{x, y, h});
  }
  sort(all(data), [](vi a, vi b) { return a[2] > b[2]; });
  for (auto &d : data) {
    rep(cx, 101) rep(cy, 101) {
      int H = abs(d[0] - cx) + abs(d[1] - cy) + d[2];
      if (ans[cy][cx] == INF)
        ans[cy][cx] = H;
      if (ans[cy][cx] != H)
        ans[cy][cx] = -1;
    }
  }
  rep(cx, 101) rep(cy, 101) {
    if (ans[cy][cx] != -1) {
      cout << cx << " " << cy << " " << ans[cy][cx] << endl;
      return 0;
    }
  }
  cout << ans[1000000][1] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vd = vector<double>;
using vvd = vector<vd>;
using vl = vector<ll>;
using vvl = vector<vl>;
using pii = pair<int, int>;
using vs = vector<string>;
#define rep(i, n) range(i, 0, n)
#define range(i, a, n) for (int i = a; i < n; i++)
#define all(a) a.begin(), a.end()
#define LINF ((ll)1ll << 60)
#define INF ((int)1 << 30)
#define EPS (1e-9)
#define MOD (1000000007)
#define fcout(a) cout << setprecision(a) << fixed
#define fs first
#define sc second
#define PI 3.141592653589793

template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) {
  os << "[" << p.first << ", " << p.second << "]";
  return os;
};
template <class S> auto &operator<<(ostream &os, vector<S> t) {
  bool a = 1;
  for (auto s : t) {
    os << (a ? "" : " ") << s;
    a = 0;
  }
  return os;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vvi ans(101, vi(101, INF));
  vvi data;
  rep(i, n) {
    int x, y, h;
    cin >> x >> y >> h;
    data.push_back(vi{x, y, h});
  }
  sort(all(data), [](vi a, vi b) { return a[2] > b[2]; });
  for (auto &d : data) {
    rep(cx, 101) rep(cy, 101) {
      int H = abs(d[0] - cx) + abs(d[1] - cy) + d[2];
      if (ans[cy][cx] == INF)
        ans[cy][cx] = H;
      if (d[2] == 0) {
        if (ans[cy][cx] > H)
          ans[cy][cx] = -1;
      } else {
        if (ans[cy][cx] != H)
          ans[cy][cx] = -1;
      }
    }
  }
  rep(cx, 101) rep(cy, 101) {
    if (ans[cy][cx] != -1) {
      cout << cx << " " << cy << " " << ans[cy][cx] << endl;
      return 0;
    }
  }
  cout << ans[1000000][1] << endl;
  return 0;
}