#include #include using namespace std; using namespace atcoder; using ll = long long; ll isqrt(ll n) { ll ok = 0ll; ll ng = 2000000000ll; while(abs(ok - ng) > 1){ ll mid = (ok + ng) / 2; if(mid * mid <= n){ ok = mid; } else{ ng = mid; } } return ok; } ll pow_ll(ll a, int b){ ll ret = 1; for(int i = 0; i < b; ++i){ ret *= a; } return ret; } int main(){ int n; cin >> n; vector > planets(n); for(auto&[x, y, z] : planets){ cin >> x >> y >> z; } vector > edges = {}; for(int i = 0; i < n - 1; ++i){ auto&[x1, y1, t1] = planets[i]; for(int j = i + 1; j < n; ++j){ auto&[x2, y2, t2] = planets[j]; ll d = pow_ll(x1 - x2, 2) + pow_ll(y1 - y2, 2); if(t1 != t2){ ll r1 = x1 * x1 + y1 * y1; ll r2 = x2 * x2 + y2 * y2; d = r1 + r2 - isqrt(4 * r1 * r2); } edges.push_back({d, i, j}); } } sort(edges.begin(), edges.end()); dsu uni(n); ll ans = 0; for(auto&[d, x, y] : edges){ ans = d; uni.merge(x, y); if(!uni.same(0, n - 1)){ continue; } break; } cout << ans << endl; return 0; }