prithivMLmods/Coder-Stat huggingface dataset:
Problem p03240 (Python)

Buggy submission (status: Runtime Error):
def search_center(xyh_list):
    xyh_list.sort(key=lambda r: r[2], reverse=True)
    highest = xyh_list[0]
    for cx in range(101):
        for cy in range(101):
            h = highest[2] + abs(highest[0] - cx) + abs(highest[1] - cy)
            matched = True
            for xyh in xyh_list[1:]:
                if xyh[2] == 0:
                    if h - abs(highest[0] - cx) - abs(highest[1] - cy) > 0:
                        matched = False
                        break
                else:
                    hh = xyh[2] + abs(xyh[0] - cx) + abs(xyh[1] - cy)
                    if h != hh:
                        matched = False
                        break
            if matched:
                return cx, cy, h
    return None


def main():
    N = int(input())
    xyh_list = [list(map(int, input().split(" "))) for _ in range(N)]
    xyh_list.sort(key=lambda r: r[2], reverse=True)
    cx, cy, ph = search_center(xyh_list)
    print("{} {} {}".format(cx, cy, ph))


if __name__ == "__main__":
    main()


Fixed submission:
def search_center(xyh_list):
    xyh_list.sort(key=lambda r: r[2], reverse=True)
    highest = xyh_list[0]
    for cx in range(101):
        for cy in range(101):
            h = highest[2] + abs(highest[0] - cx) + abs(highest[1] - cy)
            matched = True
            for xyh in xyh_list[1:]:
                if xyh[2] == 0:
                    if h - abs(xyh[0] - cx) - abs(xyh[1] - cy) > 0:
                        matched = False
                        break
                else:
                    hh = xyh[2] + abs(xyh[0] - cx) + abs(xyh[1] - cy)
                    if h != hh:
                        matched = False
                        break
            if matched:
                return cx, cy, h
    return None


def main():
    N = int(input())
    xyh_list = [list(map(int, input().split(" "))) for _ in range(N)]
    xyh_list.sort(key=lambda r: r[2], reverse=True)
    cx, cy, ph = search_center(xyh_list)
    print("{} {} {}".format(cx, cy, ph))


if __name__ == "__main__":
    main()
Problem p03240 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

class point {
public:
  point();
  point(long x, long y);
  point(const point &other);
  long x, y, h;
  long height(long cx, long cy);
};

point::point(long x, long y) : x(x), y(y) {}

point::point(const point &other) : x(other.x), y(other.y) {}

point::point() : point(101, 101) {}

long point::height(long cx, long cy) {
  return this->h + abs(this->x - cx) + abs(this->y - cy);
}

int main() {
  vector<point> ps(100);
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> ps[i].x >> ps[i].y >> ps[i].h;
  }
  point *anchor;
  for (anchor = &ps[0]; anchor->h == 0; anchor++)
    ;

  point *ans = nullptr;
  for (long x = 0; x <= 100; x++) {
    for (long y = 0; y <= 100; y++) {
      bool sat = true;
      long height = anchor->height(x, y);
      for (int i = 0; i < N; i++) {
        long test = height - abs(ps[i].x - x) - abs(ps[i].y - y);
        if (ps[i].h != test) {
          sat = false;
          break;
        }
      }
      if (sat)
        ans = new point(x, y);
    }
  }

  cout << ans->x << " " << ans->y << " " << anchor->height(ans->x, ans->y)
       << endl;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

class point {
public:
  point();
  point(long x, long y);
  point(const point &other);
  long x, y, h;
  long height(long cx, long cy);
};

point::point(long x, long y) : x(x), y(y) {}

point::point(const point &other) : x(other.x), y(other.y) {}

point::point() : point(101, 101) {}

long point::height(long cx, long cy) {
  return this->h + abs(this->x - cx) + abs(this->y - cy);
}

int main() {
  vector<point> ps(100);
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> ps[i].x >> ps[i].y >> ps[i].h;
  }
  point *anchor;
  for (anchor = &ps[0]; anchor->h == 0; anchor++)
    ;

  point *ans = nullptr;
  for (long x = 0; x <= 100; x++) {
    for (long y = 0; y <= 100; y++) {
      bool sat = true;
      long height = anchor->height(x, y);
      for (int i = 0; i < N; i++) {
        long test = height - abs(ps[i].x - x) - abs(ps[i].y - y);
        if (ps[i].h != max(test, 0L)) {
          sat = false;
          break;
        }
      }
      if (sat)
        ans = new point(x, y);
    }
  }

  cout << ans->x << " " << ans->y << " " << anchor->height(ans->x, ans->y)
       << endl;
}
Problem p03240 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define all(s) s.begin(), s.end()
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<P> vp;
const int MX = 100005, INF = 1001001001;
const ll LINF = 1e18;
const double eps = 1e-10;
const int DIV = 1e9 + 7;

int main() {
  int n;
  vi x, y, h;
  cin >> n;
  x.resize(n);
  y.resize(n);
  h.resize(n);
  rep(i, n) cin >> x[i] >> y[i] >> h[i];

  for (int xi = 0; xi <= 100; xi++) {
    for (int yi = 0; yi <= 100; yi++) {
      bool fl = true;
      ll top = max(abs(xi - x[0]) + abs(yi - y[0]) + h[0], 0);
      for (int i = 1; i < n; i++) {
        ll height = max(top - (abs(xi - x[i]) + abs(yi - y[i])), 0LL);
        if (height != h[i])
          fl = false;
      }
      if (fl) {
        cout << xi << " " << yi << " " << top << endl;
        return 0;
      }
    }
  }
  return 1;
}


Fixed submission:
#include "bits/stdc++.h"
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define all(s) s.begin(), s.end()
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<P> vp;
const int MX = 100005, INF = 1001001001;
const ll LINF = 1e18;
const double eps = 1e-10;
const int DIV = 1e9 + 7;

int main() {
  int n;
  vi x, y, h;
  cin >> n;
  x.resize(n);
  y.resize(n);
  h.resize(n);
  rep(i, n) cin >> x[i] >> y[i] >> h[i];

  for (int xi = 0; xi <= 100; xi++) {
    for (int yi = 0; yi <= 100; yi++) {
      bool fl = true;
      int top;
      rep(i, n) {
        if (h[i] != 0) {
          top = abs(xi - x[i]) + abs(yi - y[i]) + h[i];
        }
      }
      for (int i = 0; i < n; i++) {
        int height = max(top - (abs(xi - x[i]) + abs(yi - y[i])), 0);
        if (height != h[i])
          fl = false;
      }
      if (fl) {
        cout << xi << " " << yi << " " << top << endl;
        return 0;
      }
    }
  }
  return 1;
}
Problem p03240 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAX = 1000000;
const ll INF = (1LL << 31) - 1;
const double pi = acos(-1);

ll n, x[101], y[101], h[101];
vector<tuple<ll, ll, ll>> vec;

int main() {
  cin >> n;
  tuple<ll, ll, ll> G;
  rep(i, n) {
    cin >> x[i] >> y[i] >> h[i];
    if (h[i])
      G = make_tuple(x[i], y[i], h[i]);
  }
  rep(i, 101) {
    rep(j, 101) {
      ll v = get<2>(G) + abs(get<0>(G) - i) + abs(get<1>(G) - j);
      bool flag = true;
      rep(k, n) {
        ll H = v - abs(x[k] - i) - abs(y[k] - j);
        if (H != h[k])
          flag = false;
      }
      if (flag)
        vec.push_back(make_tuple(i, j, v));
    }
  }
  cout << get<0>(vec[0]) << " " << get<1>(vec[0]) << " " << get<2>(vec[0])
       << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAX = 1000000;
const ll INF = (1LL << 31) - 1;
const double pi = acos(-1);

ll n, x[101], y[101], h[101];
vector<tuple<ll, ll, ll>> vec;

int main() {
  cin >> n;
  tuple<ll, ll, ll> G;
  rep(i, n) {
    cin >> x[i] >> y[i] >> h[i];
    if (h[i])
      G = make_tuple(x[i], y[i], h[i]);
  }
  rep(i, 101) {
    rep(j, 101) {
      ll v = get<2>(G) + abs(get<0>(G) - i) + abs(get<1>(G) - j);
      bool flag = true;
      rep(k, n) {
        ll H = v - abs(x[k] - i) - abs(y[k] - j);
        H = max(H, 0ll);
        if (H != h[k])
          flag = false;
      }
      if (flag)
        vec.push_back(make_tuple(i, j, v));
    }
  }
  cout << get<0>(vec[0]) << " " << get<1>(vec[0]) << " " << get<2>(vec[0])
       << endl;
}
Problem p03240 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  cin >> n;
  int x[111], y[111], h[111], z;
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i] >> h[i];
    if (h[i])
      z = i;
  }
  for (int i = 0; i < 101; i++) {
    for (int j = 0; j < 101; j++) {
      int a = 1, b = 2e9;
      while (a + 1 < b) {
        int c = (a + b) / 2;
        if (c - abs(x[z] - i) - abs(y[z] - j) > h[z])
          b = c;
        else
          a = c;
      }
      for (int k = 0; k < n; k++)
        if (max(a - abs(x[k] - i) - abs(y[k] - j), 0) != h[k])
          goto q;
      cout << i << ' ' << j << ' ' << a << endl;
      return 0;
    q:;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  cin >> n;
  int x[111], y[111], h[111], z;
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i] >> h[i];
    if (h[i])
      z = i;
  }
  for (int i = 0; i < 101; i++) {
    for (int j = 0; j < 101; j++) {
      int a = 1, b = 1e9 + 1000;
      while (a + 1 < b) {
        int c = (a + b) / 2;
        if (c - abs(x[z] - i) - abs(y[z] - j) > h[z])
          b = c;
        else
          a = c;
      }
      for (int k = 0; k < n; k++)
        if (max(a - abs(x[k] - i) - abs(y[k] - j), 0) != h[k])
          goto q;
      cout << i << ' ' << j << ' ' << a << endl;
      return 0;
    q:;
    }
  }
  return 0;
}
Problem p03240 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int i, j, k;
  ll N, H, hight, count = 0;
  bool judge;

  cin >> N;

  vector<ll> x(N);
  vector<ll> y(N);
  vector<ll> h(N);

  rep(i, N) cin >> x[i] >> y[i] >> h[i];

  rep(i, 101) {
    rep(j, 101) {
      judge = true;
      rep(k, N) {
        if (h[k] != 0) {
          H = llabs(x[k] - i) + llabs(y[k] - j) + h[k];
          break;
        }
        cout << "something error" << endl;
        return -1;
      }
      rep(k, N) {
        hight = H - llabs(i - x[k]) - llabs(j - y[k]);
        if (h[k] != max((ll)0, hight)) {
          judge = false;
          break;
        }
      }
      if (judge == true) {
        cout << i << " " << j << " " << H;
        return 0;
      }
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int i, j, k;
  ll N, H, hight, count = 0;
  bool judge;

  cin >> N;

  vector<ll> x(N);
  vector<ll> y(N);
  vector<ll> h(N);

  rep(i, N) cin >> x[i] >> y[i] >> h[i];

  rep(i, 101) {
    rep(j, 101) {
      judge = true;
      rep(k, N) {
        if (h[k] != 0) {
          H = llabs(x[k] - i) + llabs(y[k] - j) + h[k];
          break;
        }
      }
      rep(k, N) {
        hight = H - llabs(i - x[k]) - llabs(j - y[k]);
        if (h[k] != max((ll)0, hight)) {
          judge = false;
          break;
        }
      }
      if (judge == true) {
        cout << i << " " << j << " " << H;
        return 0;
      }
    }
  }
}
Problem p03240 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

template <typename T> inline T get() {
  T x;
  cin >> x;
  return x;
}

int main() {
  int n = get<int>();
  vector<int> x(n), y(n), h(n);
  int k = -1;
  for (int i = 0; i < n; i++) {
    x[i] = get<int>(), y[i] = get<int>(), h[i] = get<int>();
    if (h[i] > 0)
      k = i;
  }
  assert(k != -1);

  int xx, yy, hh;
  for (xx = 0; xx <= 100; xx++) {
    for (yy = 0; yy <= 100; yy++) {
      hh = h[k] + abs(xx - x[k]) + abs(yy - y[k]);
      for (int i = 0; i < n; i++) {
        if (hh != h[i] + abs(xx - x[i]) + abs(yy - y[i]))
          goto NextCandidate;
      }
      goto Found;
    NextCandidate:;
    }
  }
  cerr << "Error";
  exit(1);
Found:;
  cout << xx << " " << yy << " " << hh << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

template <typename T> inline T get() {
  T x;
  cin >> x;
  return x;
}

int main() {
  int n = get<int>();
  vector<int> x(n), y(n), h(n);
  int k = -1;
  for (int i = 0; i < n; i++) {
    x[i] = get<int>(), y[i] = get<int>(), h[i] = get<int>();
    if (h[i] > 0)
      k = i;
  }
  assert(k != -1);

  int xx, yy, hh;
  for (xx = 0; xx <= 100; xx++) {
    for (yy = 0; yy <= 100; yy++) {
      hh = h[k] + abs(xx - x[k]) + abs(yy - y[k]);
      for (int i = 0; i < n; i++) {
        if (h[i] != max(0, hh - abs(xx - x[i]) - abs(yy - y[i])))
          goto NextCandidate;
      }
      goto Found;
    NextCandidate:;
    }
  }
  cerr << "Error";
  exit(1);
Found:;
  cout << xx << " " << yy << " " << hh << endl;
  return 0;
}
Problem p03240 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <stack>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

vector<int> x, y, h;

int is_ok(int Cx, int Cy, int X_base, int Y_base, int h_base, int X, int Y,
          int h) {

  if (h > 0 && abs(X_base - Cx) + abs(Y_base - Cy) + h_base ==
                   abs(X - Cx) + abs(Y - Cy) + h) {
    return 1;
  }

  else if (h == 0 && abs(X_base - Cx) + abs(Y_base - Cy) + h_base <=
                         abs(X - Cx) + abs(Y - Cy) + h) {
    return 1;
  }

  else {

    return 0;
  }
}

int main() {
  int N;
  int s, t, u;
  int X, Y;
  int H;
  int ansx = 0, ansy = 0, ansh = 0;

  cin >> N;

  rep(i, N) {
    cin >> s >> t >> u;

    x.push_back(s);
    y.push_back(t);
    h.push_back(u);
  }

  rep(i, N) {
    if (h[i] > 0) {
      X = x[i];
      Y = y[i];
      H = h[i];
      break;
    }
  }

  bool ok = true;

  rep(i, 101) {
    rep(j, 101) {
      rep(k, N) {
        if (is_ok(i, j, X, Y, H, x[k], y[k], h[k]) != 1) {
          ok = false;
          break;
        }
      }

      if (ok == true) {
        ansx = i;
        ansy = j;
        ansh = abs(X - ansx) + abs(Y - ansy) + H;

        cout << ansx << " " << ansy << " " << ansh << endl;

        while (1) {
        }

        return 0;
      }

      ok = true;
    }
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <stack>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

vector<int> x, y, h;

int is_ok(int Cx, int Cy, int X_base, int Y_base, int h_base, int X, int Y,
          int h) {

  if (h > 0 && abs(X_base - Cx) + abs(Y_base - Cy) + h_base ==
                   abs(X - Cx) + abs(Y - Cy) + h) {
    return 1;
  }

  else if (h == 0 && abs(X_base - Cx) + abs(Y_base - Cy) + h_base <=
                         abs(X - Cx) + abs(Y - Cy) + h) {
    return 1;
  }

  else {

    return 0;
  }
}

int main() {
  int N;
  int s, t, u;
  int X, Y;
  int H;
  int ansx = 0, ansy = 0, ansh = 0;

  cin >> N;

  rep(i, N) {
    cin >> s >> t >> u;

    x.push_back(s);
    y.push_back(t);
    h.push_back(u);
  }

  rep(i, N) {
    if (h[i] > 0) {
      X = x[i];
      Y = y[i];
      H = h[i];
      break;
    }
  }

  bool ok = true;

  rep(i, 101) {
    rep(j, 101) {
      rep(k, N) {
        if (is_ok(i, j, X, Y, H, x[k], y[k], h[k]) != 1) {
          ok = false;
          break;
        }
      }

      if (ok == true) {
        ansx = i;
        ansy = j;
        ansh = abs(X - ansx) + abs(Y - ansy) + H;

        cout << ansx << " " << ansy << " " << ansh << endl;

        return 0;
      }

      ok = true;
    }
  }

  return 0;
}
Problem p03240 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

int main(void) {
  int n;
  cin >> n;
  int cx, cy, ch;
  vector<int> x(n), y(n), h(n);
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i] >> h[i];
  }
  for (ch = 1; ch <= 100001000; ch++) {
    for (cx = 0; cx <= 100; cx++) {
      for (cy = 0; cy <= 100; cy++) {
        for (int i = 0; i < n; i++) {
          if (h[i] != max(ch - abs(x[i] - cx) - abs(y[i] - cy), 0))
            break;
          if (i == n - 1) {
            cout << cx << " " << cy << " " << ch << endl;
            return 0;
          }
        }
      }
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

int main(void) {
  int n;
  cin >> n;
  int cx, cy, ch;
  vector<int> x(n), y(n), h(n);
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i] >> h[i];
  }
  for (cx = 0; cx <= 100; cx++) {
    for (cy = 0; cy <= 100; cy++) {
      ch = 0;
      for (int i = 0; i < n; i++) {
        if (h[i] > 0 && ch <= 0)
          ch = h[i] + abs(x[i] - cx) + abs(y[i] - cy);
        else
          continue;
      }
      for (int i = 0; i < n; i++) {
        if (h[i] != max(ch - abs(x[i] - cx) - abs(y[i] - cy), 0))
          break;
        if (i == n - 1) {
          cout << cx << " " << cy << " " << ch << endl;
          return 0;
        }
      }
    }
  }
  return 0;
}
Problem p03240 (C++)

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

int CalHeight(int Cx, int Cy, int H, int h, int x, int y) {
  if (h != 0)
    return abs(x - Cx) + abs(y - Cy) + h;
  else if (H <= abs(x - Cx) + abs(y - Cy))
    return H;
  else
    return -1;
}

int main() {
  int N, cnt = 0, hs, xs, ys;
  cin >> N;
  int *x = new int[N];
  int *y = new int[N];
  int *h = new int[N];
  for (int i = 0; i < N; i++) {
    cin >> x[i] >> y[i] >> h[i];
    if (h[i] != 0) {
      hs = h[i];
      xs = x[i];
      ys = y[i];
    }
  }
  for (int Cx = 0; Cx <= 100; Cx++) {
    for (int Cy = 0; Cy <= 100; Cy++) {
      int H = abs(xs - Cx) + abs(ys - Cy) + hs;
      for (int i = 0; i < N; i++) {
        if (H != CalHeight(Cx, Cy, H, h[i], x[i], y[i]))
          break;
        if (i == N - 1) {
          cout << Cx << " " << Cy << " " << H << " " << endl;
          return 1;
        }
      }
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int CalHeight(int Cx, int Cy, int H, int h, int x, int y) {
  if (h != 0)
    return abs(x - Cx) + abs(y - Cy) + h;
  else if (H <= abs(x - Cx) + abs(y - Cy))
    return H;
  else
    return -1;
}

int main() {
  int N, cnt = 0, hs, xs, ys;
  cin >> N;
  int *x = new int[N];
  int *y = new int[N];
  int *h = new int[N];
  for (int i = 0; i < N; i++) {
    cin >> x[i] >> y[i] >> h[i];
    if (h[i] != 0) {
      hs = h[i];
      xs = x[i];
      ys = y[i];
    }
  }
  for (int Cx = 0; Cx <= 100; Cx++) {
    for (int Cy = 0; Cy <= 100; Cy++) {
      int H = abs(xs - Cx) + abs(ys - Cy) + hs;
      for (int i = 0; i < N; i++) {
        if (H != CalHeight(Cx, Cy, H, h[i], x[i], y[i]))
          break;
        if (i == N - 1) {
          cout << Cx << " " << Cy << " " << H << " " << endl;
        }
      }
    }
  }
}
Problem p03240 (Python)

Buggy submission (status: Time Limit Exceeded):
N = int(input())
P = [list(map(int, input().split())) for i in range(N)]

i = 0
while 1:
    if P[0][2] > 0:
        break
    i += 1
x0, y0, h0 = P[i]
for cy in range(101):
    for cx in range(101):
        h = h0 + abs(cx - x0) + abs(cy - y0)
        for x, y, h1 in P:
            if h1 != max(h - abs(cx - x) - abs(cy - y), 0):
                break
        else:
            print(cx, cy, h)
            exit(0)
exit(1)


Fixed submission:
N = int(input())
P = [list(map(int, input().split())) for i in range(N)]

i = 0
while 1:
    if P[i][2] > 0:
        break
    i += 1
x0, y0, h0 = P[i]
for cy in range(101):
    for cx in range(101):
        h = h0 + abs(cx - x0) + abs(cy - y0)
        for x, y, h1 in P:
            if h1 != max(h - abs(cx - x) - abs(cy - y), 0):
                break
        else:
            print(cx, cy, h)
            exit(0)
exit(1)
Problem p03240 (Python)

Buggy submission (status: Runtime Error):
# -*- coding: utf-8 -*-

N = int(input())
X, Y, H = [], [], []
for _ in range(N):
    x, y, h = map(int, input().split())
    X.append(x)
    Y.append(y)
    H.append(h)

for x in range(101):
    for y in range(101):
        hc = {H[i] + abs(X[i] - x) + abs(Y[i] - y) for i in range(N) if H[i] > 0}
        h0 = {H[i] + abs(X[i] - x) + abs(Y[i] - y) for i in range(N) if H[i] == 0}
        if len(hc) == 1 and max(h0) <= max(hc):
            print(x, y, list(hc)[0])
            break


Fixed submission:
# -*- coding: utf-8 -*-

N = int(input())
X, Y, H = [], [], []
for _ in range(N):
    x, y, h = map(int, input().split())
    X.append(x)
    Y.append(y)
    H.append(h)

for x in range(101):
    for y in range(101):
        hc = {H[i] + abs(X[i] - x) + abs(Y[i] - y) for i in range(N) if H[i] > 0}
        h0 = {H[i] + abs(X[i] - x) + abs(Y[i] - y) for i in range(N) if H[i] == 0}
        if len(hc) == 1:
            if (len(h0) > 0 and min(h0) >= list(hc)[0]) or len(h0) == 0:
                print(x, y, list(hc)[0])
                break
Problem p03240 (Python)

Buggy submission (status: Runtime Error):
N = int(input())
data = [tuple(map(int, input().split())) for _ in range(N)]
data.sort(key=lambda x: x[2])
for cx in range(101):
    for cy in range(101):
        h0 = None
        for x, y, h in data:
            if h:
                h_tmp = h + abs(cx - x) + abs(cy - y)
                if h_tmp != h0 and h0 is not None:
                    break
                h0 = h_tmp
            else:
                if abs(cx - x) + abs(cy - y) < h0:
                    break
        else:
            print(cx, cy, h0)
            exit()


Fixed submission:
N = int(input())
data = [tuple(map(int, input().split())) for _ in range(N)]
data.sort(key=lambda x: -x[2])
for cx in range(101):
    for cy in range(101):
        h0 = None
        for x, y, h in data:
            if h:
                h_tmp = h + abs(cx - x) + abs(cy - y)
                if h_tmp != h0 and h0 is not None:
                    break
                h0 = h_tmp
            else:
                if abs(cx - x) + abs(cy - y) < h0:
                    break
        else:
            print(cx, cy, h0)
            exit()
Problem p03240 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
using namespace std;
int x[102], y[102], h[102];
int c_x, c_y, c_h;
int max_h;
int main() {
  int n;
  scanf("%d", &n);
  max_h = -1;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d%d", x + i, y + i, h + i);
    max_h = max(max_h, h[i]);
  }
  int sign = 1;
  for (c_h = max_h; sign; c_h++)
    for (c_x = 0; sign && c_x <= 100; c_x++)
      for (c_y = 0; sign && c_y <= 100; c_y++) {
        int i = 1;
        for (; i <= n; i++)
          if (h[i] != (c_h - abs(c_x - x[i]) - abs(c_y - y[i])))
            break;
        if (i > n)
          sign = 0;
      }
  printf("%d %d %d\n", c_x - 1, c_y - 1, c_h - 1);
  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
using namespace std;
int x[102], y[102], h[102];
int c_x, c_y, c_h;
int max_h;
int main() {
  int n;
  scanf("%d", &n);
  max_h = -1;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d%d", x + i, y + i, h + i);
    max_h = max(max_h, h[i]);
  }
  int sign = 1;
  for (c_h = max_h; sign; c_h++)
    for (c_x = 0; sign && c_x <= 100; c_x++)
      for (c_y = 0; sign && c_y <= 100; c_y++) {
        int i = 1;
        for (; i <= n; i++)
          if (h[i] != max(0, (c_h - abs(c_x - x[i]) - abs(c_y - y[i]))))
            break;
        if (i > n)
          sign = 0;
      }
  printf("%d %d %d\n", c_x - 1, c_y - 1, c_h - 1);
  return 0;
}
Problem p03240 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const int INF = 1001001001;
const int MOD = 1000000007;

template <typename T> void print(const T &v);

int main() {
  int n;
  cin >> n;
  vector<int> xs(n), ys(n), hs(n);
  for (int i = 0; i < n; i++) {
    cin >> xs[i] >> ys[i] >> hs[i];
  }
  if (n == 5) {
    map<int, int> mp;
    for (int i = 0; i < 5; i) {
      mp[hs[i]]++;
    }
    if (mp[0] == 4 && mp[1] == 1) {
      int xabs = 0, yabs = 0;
      for (int i = 0; i < 5; i++) {
        xabs += xs[i];
        yabs += ys[i];
      }
      cout << xabs / 5 << ' ' << yabs / 5 << ' ' << 1 << endl;
      return 0;
    }
  }
  for (int x = 0; x <= 100; x++) {
    for (int y = 0; y <= 100; y++) {
      bool flag = true;
      int before = -1;
      for (int i = 0; i < n; i++) {
        if (hs[i] == 0) {
          continue;
        }
        int h = abs(x - xs[i]) + abs(y - ys[i]) + hs[i];
        if (before != -1 && before != h) {
          flag = false;
          break;
        }
        before = h;
      }
      if (flag) {
        cout << x << ' ' << y << ' ' << before << endl;
        return 0;
      }
    }
  }
}

// Use For Debug
template <typename T> void print(T const &v) {
  for (int i = 0; i < v.size(); i++) {
    if (i)
      cout << " ";
    cout << v[i];
  }
  cout << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const int INF = 1001001001;
const int MOD = 1000000007;

template <typename T> void print(const T &v);

int main() {
  int n;
  cin >> n;
  vector<int> xs(n), ys(n), hs(n);
  for (int i = 0; i < n; i++) {
    cin >> xs[i] >> ys[i] >> hs[i];
  }
  if (n == 5) {
    map<int, int> mp;
    for (int i = 0; i < 5; i++) {
      mp[hs[i]]++;
    }
    if (mp[0] == 4 && mp[1] == 1) {
      int xabs = 0, yabs = 0;
      for (int i = 0; i < 5; i++) {
        xabs += xs[i];
        yabs += ys[i];
      }
      cout << xabs / 5 << ' ' << yabs / 5 << ' ' << 1 << endl;
      return 0;
    }
  }
  for (int x = 0; x <= 100; x++) {
    for (int y = 0; y <= 100; y++) {
      bool flag = true;
      int before = -1;
      for (int i = 0; i < n; i++) {
        if (hs[i] == 0) {
          continue;
        }
        int h = abs(x - xs[i]) + abs(y - ys[i]) + hs[i];
        if (before != -1 && before != h) {
          flag = false;
          break;
        }
        before = h;
      }
      if (flag) {
        cout << x << ' ' << y << ' ' << before << endl;
        return 0;
      }
    }
  }
}

// Use For Debug
template <typename T> void print(T const &v) {
  for (int i = 0; i < v.size(); i++) {
    if (i)
      cout << " ";
    cout << v[i];
  }
  cout << endl;
}
Problem p03240 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>

using namespace std;

int main() {

  int a;
  cin >> a;

  long long int x[110], y[110], h[110];

  for (int i = 0; i < a; i++) {
    cin >> x[i] >> y[i] >> h[i];
  }
  for (int posx = 0; posx <= 100; posx++) {

    for (int posy = 0; posy <= 100; posy++) {
      for (int nh = (h[0] - 1000); nh <= h[0] + 1000; nh++) {

        bool abcd = true;

        for (int i = 0; i < a; i++) {

          long long int k = 0;
          if (h[i] != max(nh - abs(posx - x[i]) - abs(posy - y[i]), k))
            abcd = false;
        }
        if (abcd == true)
          cout << posx << " " << posy << " " << nh << endl;
      }
    }
  }
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>

using namespace std;

int main() {

  int a;
  cin >> a;

  long long int x[110], y[110], h[110];

  for (int i = 0; i < a; i++) {
    cin >> x[i] >> y[i] >> h[i];
  }
  for (int posx = 0; posx <= 100; posx++) {

    for (int posy = 0; posy <= 100; posy++) {
      for (int nh = (h[0] - 250); nh <= h[0] + 250; nh++) {

        bool abcd = true;

        for (int i = 0; i < a; i++) {

          long long int k = 0;
          if (h[i] != max(nh - abs(posx - x[i]) - abs(posy - y[i]), k))
            abcd = false;
        }
        if (abcd == true)
          cout << posx << " " << posy << " " << nh << endl;
      }
    }
  }
}
Problem p03240 (C++)

Buggy submission (status: Runtime Error):
// 06, 10, 2018, 09:13:45 Rafaelcs cpp
#include <bits/stdc++.h>

using namespace std;
typedef pair<int, int> pii;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;

const int N = 100;
pii p[N + 10];
int h[N + 10];
int n;

int can(int i, int j) {
  int h0 = 1;

  for (int k = 0; k < n; k++) {
    if (h[k] != 0) {
      h0 = abs(p[0].first - i) + abs(p[0].second - j) + h[0];
      break;
    }
  }

  for (int k = 0; k < n; k++) {
    if (h[k] == 0)
      continue;

    int hi = abs(p[k].first - i) + abs(p[k].second - j) + h[k];

    if (hi != h0) {
      return -1;
    }
  }

  return h0;
}

bool ver(int H, int cx, int cy) {
  for (int i = 0; i < n; i++) {
    if (max(H - (abs(cx - p[i].first) + abs(cy - p[i].second)), 0) != h[i]) {
      return false;
    }
  }

  return true;
}

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

  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf("%d %d %d", &p[i].first, &p[i].second, &h[i]);
  }

  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= N; j++) {
      int d = can(i, j);

      if (d != -1 && d > 0 && ver(d, i, j)) {
        printf("%d %d %d\n", i, j, d);
        return 0;
      }
    }
  }

  assert(false);

  return 0;
}

Fixed submission:
// 06, 10, 2018, 09:13:45 Rafaelcs cpp
#include <bits/stdc++.h>

using namespace std;
typedef pair<int, int> pii;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;

const int N = 100;
pii p[N + 10];
int h[N + 10];
int n;

int can(int i, int j) {
  int h0 = 1;

  for (int k = 0; k < n; k++) {
    if (h[k] != 0) {
      h0 = abs(p[k].first - i) + abs(p[k].second - j) + h[k];
      break;
    }
  }

  for (int k = 0; k < n; k++) {
    if (h[k] == 0)
      continue;

    int hi = abs(p[k].first - i) + abs(p[k].second - j) + h[k];

    if (hi != h0) {
      return -1;
    }
  }

  return h0;
}

bool ver(int H, int cx, int cy) {
  for (int i = 0; i < n; i++) {
    if (max(H - (abs(cx - p[i].first) + abs(cy - p[i].second)), 0) != h[i]) {
      return false;
    }
  }

  return true;
}

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

  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf("%d %d %d", &p[i].first, &p[i].second, &h[i]);
  }

  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= N; j++) {
      int d = can(i, j);

      if (d != -1 && d > 0 && ver(d, i, j)) {
        printf("%d %d %d\n", i, j, d);
        return 0;
      }
    }
  }

  assert(false);

  return 0;
}
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 vll = vector<ll>;
using P = pair<int, int>;

const int INF = 1e9;
const ll INFLL = 1e18;
const int MOD = 1e9 + 7;
const int NIL = -1;
const ld PI = acosl(-1);

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(n) n.begin(), n.end()

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;
}

struct Pos {
  ll x = -1;
  ll y = -1;
  ll h = -1;
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  // cout << fixed << setprecision(6);
  ll n;
  cin >> n;
  vector<Pos> p(n);
  for (int i = 1; i <= n; i++)
    cin >> p[i].x >> p[i].y >> p[i].h;

  Pos G;
  rep(i, n) if (p[i].h >= 1) G = p[i];

  Pos ans;
  for (int cx = 0; cx <= 100; cx++) {
    for (int cy = 0; cy <= 100; cy++) {
      ll v1 = G.h + abs(cx - G.x) + abs(cy - G.y);
      v1 = max(v1, 0LL);
      bool flg = true;
      for (int i = 1; i <= n; i++) {
        ll v2 = v1 - abs(cx - p[i].x) - abs(cy - p[i].y);
        v2 = max(v2, 0LL);
        if (p[i].h != v2)
          flg = false;
      }
      if (flg) {
        ans.x = cx;
        ans.y = cy;
        ans.h = v1;
      }
    }
  }

  printf("%lld %lld %lld\n", ans.x, ans.y, ans.h);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;
using vi = vector<int>;
using vll = vector<ll>;
using P = pair<int, int>;

const int INF = 1e9;
const ll INFLL = 1e18;
const int MOD = 1e9 + 7;
const int NIL = -1;
const ld PI = acosl(-1);

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(n) n.begin(), n.end()

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;
}

struct Pos {
  ll x = -1;
  ll y = -1;
  ll h = -1;
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  // cout << fixed << setprecision(6);
  ll n;
  cin >> n;
  vector<Pos> p(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> p[i].x >> p[i].y >> p[i].h;

  Pos G;
  rep(i, n) if (p[i].h >= 1) G = p[i];

  Pos ans;
  for (int cx = 0; cx <= 100; cx++) {
    for (int cy = 0; cy <= 100; cy++) {
      ll v1 = G.h + abs(cx - G.x) + abs(cy - G.y);
      v1 = max(v1, 0LL);
      bool flg = true;
      for (int i = 1; i <= n; i++) {
        ll v2 = v1 - abs(cx - p[i].x) - abs(cy - p[i].y);
        v2 = max(v2, 0LL);
        if (p[i].h != v2)
          flg = false;
      }
      if (flg) {
        ans.x = cx;
        ans.y = cy;
        ans.h = v1;
      }
    }
  }

  printf("%lld %lld %lld\n", ans.x, ans.y, ans.h);
  return 0;
}
Problem p03240 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
// 　ceil(a/b)   (a + (b - 1))/ b
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
typedef pair<int, int> P;
const int inf = 1012345678;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

// int gcd(int a, int b){
//   if (a%b==0){
//     return(b);
//   }
//   else{
//     return(gcd(b,a%b));
//   }
// }

int x[110];
int y[110];
int h[110];

int main() {
  int N;
  cin >> N;
  rep(i, N) { cin >> x[i] >> y[i] >> h[i]; }

  rep(i, 101) {
    rep(j, 101) {
      int cx = i;
      int cy = j;
      int id = 0;
      int height = 0;
      while (1) {
        if (h[id] != 0) {
          height = h[id] + abs(cx - x[id]) + abs(cy - y[id]);
          break;
        }
      }
      bool f = true;
      rep(i, N) {
        if (h[i] != max(0, height - abs(cx - x[i]) - abs(cy - y[i]))) {
          f = false;
        }
      }
      if (f) {
        cout << cx << " " << cy << " " << height << endl;
      }
    }
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
// 　ceil(a/b)   (a + (b - 1))/ b
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
typedef pair<int, int> P;
const int inf = 1012345678;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

// int gcd(int a, int b){
//   if (a%b==0){
//     return(b);
//   }
//   else{
//     return(gcd(b,a%b));
//   }
// }

int x[110];
int y[110];
int h[110];

int main() {
  int N;
  cin >> N;
  rep(i, N) { cin >> x[i] >> y[i] >> h[i]; }

  rep(i, 101) {
    rep(j, 101) {
      int cx = i;
      int cy = j;
      int id = 0;
      int height = 0;
      while (1) {
        if (h[id] != 0) {
          height = h[id] + abs(cx - x[id]) + abs(cy - y[id]);
          break;
        }
        id++;
      }
      bool f = true;
      rep(i, N) {
        if (h[i] != max(0, height - abs(cx - x[i]) - abs(cy - y[i]))) {
          f = false;
        }
      }
      if (f) {
        cout << cx << " " << cy << " " << height << endl;
      }
    }
  }
}
Problem p03240 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define ll long long
using namespace std;

int main() {
  int N;
  cin >> N;
  struct P {
    int x;
    int y;
    ll h;
  };
  P p[N];
  P sample;
  for (int i = 0; i < N; i++) {
    cin >> p[i].x >> p[i].y >> p[i].h;
    if (p[i].h >= 1LL) {
      sample = p[i];
    }
  }

  vector<P> ansPoss;
  for (int x = 0; x <= 100; x++) {
    for (int y = 0; y <= 100; y++) {
      bool flag = true;
      ll V = sample.h + abs(sample.x - x) + abs(sample.y - y);
      V = max(V, 0LL);
      for (int i = 0; i < N; i++) {
        if (p[i].h != V - abs(p[i].x - x) - abs(p[i].y - y)) {
          flag = false;
        }
      }
      if (flag == true) {
        ansPoss.push_back(P{x, y, V});
      }
    }
  }
  P ans = ansPoss[0];
  cout << ans.x << " " << ans.y << " " << ans.h << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define ll long long
using namespace std;

int main() {
  int N;
  cin >> N;
  struct P {
    int x;
    int y;
    ll h;
  };
  P p[N];
  P sample;
  for (int i = 0; i < N; i++) {
    cin >> p[i].x >> p[i].y >> p[i].h;
    if (p[i].h >= 1LL) {
      sample = p[i];
    }
  }

  vector<P> ansPoss;
  for (int x = 0; x <= 100; x++) {
    for (int y = 0; y <= 100; y++) {
      bool flag = true;
      ll V = sample.h + abs(sample.x - x) + abs(sample.y - y);
      V = max(V, 0LL);
      for (int i = 0; i < N; i++) {
        if (p[i].h != max(V - abs(p[i].x - x) - abs(p[i].y - y), 0LL)) {
          flag = false;
        }
      }
      if (flag == true) {
        ansPoss.push_back(P{x, y, V});
      }
    }
  }
  P ans = ansPoss[0];
  cout << ans.x << " " << ans.y << " " << ans.h << endl;
  return 0;
}
Problem p03240 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pi;
typedef pair<double, double> pd;
typedef pair<double, ll> pdl;
#define F first
#define S second
const ll E = 1e18 + 7;
const ll MOD = 1000000007;

vector<pair<ll, pll>> V;

ll judge(ll x, ll y) {
  ll h = V[0].F + abs(x - V[0].S.F) + abs(y - V[0].S.S);
  if (h <= 0) {
    return -1;
  }
  for (int i = 1; i < V.size(); i++) {
    ll H = h - abs(x - V[i].S.F) - abs(y - V[i].S.S);
    if (H != V[i].F) {
      return -1;
    }
  }
  return h;
}

int main() {
  ll n;
  cin >> n;
  V.resize(n);
  for (int i = 0; i < n; i++) {
    ll x, y, h;
    cin >> x >> y >> h;
    V[i] = {h, {x, y}};
  }
  sort(V.begin(), V.end(), greater<pair<ll, pll>>());
  if (V[0].F == 0) {
    while (1)
      ;
  }
  for (int i = 0; i <= 100; i++) {
    for (int t = 0; t <= 100; t++) {
      ll ret = judge(i, t);
      if (ret != -1) {
        cout << i << " " << t << " " << ret << endl;
        return 0;
      }
    }
  }
  assert(false);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pi;
typedef pair<double, double> pd;
typedef pair<double, ll> pdl;
#define F first
#define S second
const ll E = 1e18 + 7;
const ll MOD = 1000000007;

vector<pair<ll, pll>> V;

ll judge(ll x, ll y) {
  ll h = V[0].F + abs(x - V[0].S.F) + abs(y - V[0].S.S);
  if (h <= 0) {
    return -1;
  }
  for (int i = 1; i < V.size(); i++) {
    ll H = h - abs(x - V[i].S.F) - abs(y - V[i].S.S);
    H = max(H, (ll)0);
    if (H != V[i].F) {
      return -1;
    }
  }
  return h;
}

int main() {
  ll n;
  cin >> n;
  V.resize(n);
  for (int i = 0; i < n; i++) {
    ll x, y, h;
    cin >> x >> y >> h;
    V[i] = {h, {x, y}};
  }
  sort(V.begin(), V.end(), greater<pair<ll, pll>>());
  if (V[0].F == 0) {
    while (1)
      ;
  }
  for (int i = 0; i <= 100; i++) {
    for (int t = 0; t <= 100; t++) {
      ll ret = judge(i, t);
      if (ret != -1) {
        cout << i << " " << t << " " << ret << endl;
        return 0;
      }
    }
  }
  assert(false);

  return 0;
}
Problem p03240 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N;
  int x[1000], y[1000], h[1000];
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> x[i] >> y[i] >> h[i];
  }
  vector<int> CX;
  vector<int> CY;
  vector<int> HH;

  for (int cx = 0; cx <= 100; cx++) {
    for (int cy = 0; cy <= 100; cy++) {
      vector<int> H;
      for (int i = 0; i < N; i++) {
        if (h[i] != 0) {
          H.push_back(h[i] + abs(x[i] - cx) + abs(y[i] - cy));
        }
      }
      bool flag = true;
      for (auto itr = H.begin(); itr != H.end() - 1; itr++) {
        if (*itr != *(itr + 1))
          flag = false;
      }
      if (flag == true) {
        CX.push_back(cx);
        CY.push_back(cy);
        HH.push_back(H[0]);
      }
    }
  }
  for (int i = 0; i < CX.size(); i++) {
    bool flag2 = true;
    for (int j = 0; j < N; j++) {
      if (h[i] == 0) {
        if (max(HH[i] - abs(x[i] - CX[i]) - abs(y[i] - CY[i]), 0) != 0)
          flag2 = false;
      }
    }
    if (flag2 == true) {
      cout << CX[i] << " " << CY[i] << " " << HH[i] << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N;
  int x[1000], y[1000], h[1000];
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> x[i] >> y[i] >> h[i];
  }
  vector<int> CX;
  vector<int> CY;
  vector<int> HH;

  for (int cx = 0; cx <= 100; cx++) {
    for (int cy = 0; cy <= 100; cy++) {
      vector<int> H;
      for (int i = 0; i < N; i++) {
        if (h[i] != 0) {
          H.push_back(h[i] + abs(x[i] - cx) + abs(y[i] - cy));
        }
      }
      bool flag = true;
      for (auto itr = H.begin(); itr != H.end() - 1; itr++) {
        if (*itr != *(itr + 1))
          flag = false;
      }
      if (flag == true) {
        CX.push_back(cx);
        CY.push_back(cy);
        HH.push_back(H[0]);
      }
    }
  }
  for (int i = 0; i < CX.size(); i++) {
    bool flag2 = true;
    for (int j = 0; j < N; j++) {
      if (h[j] == 0) {
        if (max(HH[i] - abs(x[j] - CX[i]) - abs(y[j] - CY[i]), 0) != 0)
          flag2 = false;
      }
    }
    if (flag2 == true) {
      cout << CX[i] << " " << CY[i] << " " << HH[i] << endl;
    }
  }
  return 0;
}
Problem p03240 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <complex>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

int N;
ll x[110], y[110], z[110];

ll myabs(ll xx) {
  if (xx >= 0) {
    return xx;
  } else {
    return -xx;
  }
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> x[i] >> y[i] >> z[i];
  }

  ll h_max = (ll)1e9 + 1;
  for (ll i = 0; i <= 100; i++) {
    for (ll j = 0; j <= 100; j++) {
      ll h = (ll)1e9 + 1;
      ll h_min = -1;
      bool ok = true;
      for (int k = 0; k < N; k++) {
        ll dis = max(h_max - myabs(i - x[k]) - myabs(j - y[k]), 0LL);
        if (z[k] == 0 && dis > h) {
          ok = false;
          break;
        } else if (z[k] == 0) {
          h_min = max(h_min, dis);
        } else if (z[k] > 0 && h == (ll)1e9 + 1) {
          h = dis - z[k];
        } else if (z[k] > 0 && h != dis - z[k]) {
          ok = false;
          break;
        }
      }
      if (ok) {
        for (int k = 0; k < N; k++) {
          ll dis = max(h_max - h - myabs(i - x[k]) - myabs(j - y[k]), 0LL);
          if (dis != z[k]) {
            ok = false;
            break;
          }
        }
      }
      if (ok) {
        cout << i << " " << j << " " << h_max - h << endl;
      }
    }
  }

  exit(1);
}


Fixed submission:
#include <algorithm>
#include <complex>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

int N;
ll x[110], y[110], z[110];

ll myabs(ll xx) {
  if (xx >= 0) {
    return xx;
  } else {
    return -xx;
  }
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> x[i] >> y[i] >> z[i];
  }

  ll h_max = (ll)1e9 + 1;
  for (ll i = 0; i <= 100; i++) {
    for (ll j = 0; j <= 100; j++) {
      ll h = (ll)1e9 + 1;
      ll h_min = -1;
      bool ok = true;
      for (int k = 0; k < N; k++) {
        ll dis = max(h_max - myabs(i - x[k]) - myabs(j - y[k]), 0LL);
        if (z[k] == 0 && dis > h) {
          ok = false;
          break;
        } else if (z[k] == 0) {
          h_min = max(h_min, dis);
        } else if (z[k] > 0 && h == (ll)1e9 + 1) {
          h = dis - z[k];
        } else if (z[k] > 0 && h != dis - z[k]) {
          ok = false;
          break;
        }
      }
      if (ok) {
        for (int k = 0; k < N; k++) {
          ll dis = max(h_max - h - myabs(i - x[k]) - myabs(j - y[k]), 0LL);
          if (dis != z[k]) {
            ok = false;
            break;
          }
        }
      }
      if (ok) {
        cout << i << " " << j << " " << h_max - h << endl;
        return 0;
      }
    }
  }

  exit(1);
}