Problem p02764 (C++)

Buggy submission (status: Runtime Error):
#ifndef LOCAL
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,mmx,tune=native")
#endif // LOCAL

#define _SCL_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS

#pragma comment(linker, "/STACK:256000000")

// #define push_back pb
#define first x
#define second y

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <ciso646>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <forward_list>
#include <fstream>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#ifdef LOCAL
// #include <vld.h>
#endif // LOCAL
using namespace std;

// typedef __int128_t ll;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef long double ld;

#define speedup                                                                \
  cout.tie(nullptr);                                                           \
  cin.tie(nullptr);                                                            \
  ios_base::sync_with_stdio(false)
#define coutdouble                                                             \
  cout << setprecision(20);                                                    \
  cout << fixed

#define all(v) (v).begin(), (v).end()
#define sz(v) (int)(v).size()

#ifdef LOCAL
mt19937 rd(57322);
#else
mt19937 rd((uint)chrono::steady_clock::now().time_since_epoch().count());
#endif
/*------CommentInInteractive--------*/
#ifndef LOCAL
#define endl '\n'
#endif // LOCAL
/*----------------------------------*/
const int INF = 1000 * 1000 * 1000 + 322;
const ll LLINF = 2LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL + 57;
constexpr uint MOD = 1000 * 1000 * 1000 + 7;
constexpr uint FFT_MOD = 998244353;
const int P1E6 = 1336337;
const int P1E3 = 1009;
const ll P1E14 = 100000000000031;
const ll P1E17 = 100000000000000003;
const ld PI = acosl(-1.); // 3.1415926535897932384626433832795
const ld EPS = 1e-6;
/*------------------------------------------------IO_OPERATORS---------------------------------------------*/
template <typename T, typename U>
inline ostream &operator<<(ostream &_out, const pair<T, U> &_p) {
  _out << _p.first << " " << _p.second;
  return _out;
}
template <typename T, typename U>
inline istream &operator>>(istream &_in, pair<T, U> &_p) {
  _in >> _p.first >> _p.second;
  return _in;
}
template <typename T>
inline ostream &operator<<(ostream &_out, const vector<T> &_v) {
  if (_v.empty())
    return _out;
  _out << _v.front();
  for (auto _it = ++_v.begin(); _it != _v.end(); ++_it)
    _out << ' ' << *_it;
  return _out;
}
template <typename T> inline istream &operator>>(istream &_in, vector<T> &_v) {
  for (auto &_i : _v)
    _in >> _i;
  return _in;
}
template <typename T>
inline ostream &operator<<(ostream &_out, const set<T> &_s) {
  if (_s.empty())
    return _out;
  _out << *_s.begin();
  for (auto _it = ++_s.begin(); _it != _s.end(); ++_it)
    _out << ' ' << *_it;
  return _out;
}
template <typename T>
inline ostream &operator<<(ostream &_out, const multiset<T> &_s) {
  if (_s.empty())
    return _out;
  _out << *_s.begin();
  for (auto _it = ++_s.begin(); _it != _s.end(); ++_it)
    _out << ' ' << *_it;
  return _out;
}
template <typename T>
inline ostream &operator<<(ostream &_out, const unordered_set<T> &_s) {
  if (_s.empty())
    return _out;
  _out << *_s.begin();
  for (auto _it = ++_s.begin(); _it != _s.end(); ++_it)
    _out << ' ' << *_it;
  return _out;
}
template <typename T>
inline ostream &operator<<(ostream &_out, const unordered_multiset<T> &_s) {
  if (_s.empty())
    return _out;
  _out << *_s.begin();
  for (auto _it = ++_s.begin(); _it != _s.end(); ++_it)
    _out << ' ' << *_it;
  return _out;
}
template <typename T, typename U>
inline ostream &operator<<(ostream &_out, const map<T, U> &_m) {
  if (_m.empty())
    return _out;
  _out << '(' << _m.begin()->first << ": " << _m.begin()->second << ')';
  for (auto _it = ++_m.begin(); _it != _m.end(); ++_it)
    _out << ", (" << _it->first << ": " << _it->second << ')';
  return _out;
}
template <typename T, typename U>
inline ostream &operator<<(ostream &_out, const unordered_map<T, U> &_m) {
  if (_m.empty())
    return _out;
  _out << '(' << _m.begin()->first << ": " << _m.begin()->second << ')';
  for (auto _it = ++_m.begin(); _it != _m.end(); ++_it)
    _out << ", (" << _it->first << ": " << _it->second << ')';
  return _out;
}
/*--------------------------------------------------IO_FILES-----------------------------------------------*/
const char *infile =
#ifdef LOCAL
    "input.txt"
#else
    ""
#endif // LOCAL
    ;
const char *outfile =
#ifdef LOCAL
    ""
#else
    ""
#endif // LOCAL
    ;

/*-------------------------------------------------ALLOCATOR----------------------------------------------*/
// #define ALLOC_LOCAL
#ifdef ALLOC_LOCAL
const int ML_ = 450;
char mem_[ML_ * 1024 * 1024];
size_t _ptr = 0, _magic = 21323400;
void *operator new(size_t cnt) {
  if (_ptr + cnt < sizeof mem_) {
    _ptr += cnt;
    return mem_ + _ptr - cnt;
  } else {
    assert(false);
    _ptr = cnt + _magic;
    return mem_ + _magic;
  }
}
void operator delete(void *) {}
#endif // ALLOC_LOCAL
/*-----------------------------------------------------MATH------------------------------------------------*/
inline ll gcd(ll a, ll b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}
inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
inline int pwm(ll xx, ll pow, int MD) {
  if (pow < 0) {
    pow = pow % (MD - 1) + MD - 1;
  }
  ll mlt = 1;
  while (pow) {
    if (pow & 1) {
      mlt *= xx;
      mlt %= MD;
    }
    xx *= xx;
    pow >>= 1;
    xx %= MD;
  }
  return (int)mlt;
}
inline ll gcdex(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll xx, yy;
  ll gc = gcdex(b, a % b, yy, xx);
  x = xx;
  y = yy - (a / b) * xx;
  return gc;
}
inline int inv(ll r, int _mod) { return pwm(r % _mod, _mod - 2, _mod); }
/*-----------------------------------------------------CODE------------------------------------------------*/

vector<ld> slvsq(const vector<ld> &cf) {
  assert(sz(cf) == 3);
  ld det = cf[1] * cf[1] - 4 * cf[0] * cf[2];
  if (det < 0) {
    return {};
  }
  return {(-cf[1] - sqrtl(det)) / (2 * cf[0]),
          (-cf[1] + sqrtl(det)) / (2 * cf[0])};
}

ld get_t(const pair<pair<ll, ll>, ll> &pp, ld x, ld y) {
  return pp.y * hypotl(pp.x.x - x, pp.x.y - y);
}

ld checkpt(const vector<pair<pair<ll, ll>, ll>> &v, ld x, ld y, int k) {
  vector<ld> dsts;
  for (const auto &pp : v) {
    dsts.push_back(get_t(pp, x, y));
  }
  nth_element(dsts.begin(), dsts.begin() + k - 1, dsts.end());
  return dsts[k - 1];
}

ld calc(const vector<pair<pair<ll, ll>, ll>> &v, int i, int j, int k,
        int need) {
  if (v[i].y == v[j].y && v[i].y == v[k].y) {
    ld a0 = 2 * (v[j].x.y * v[j].y * v[j].y - v[i].x.y * v[i].y * v[i].y);
    ld b0 = 2 * (v[j].x.x * v[j].y * v[j].y - v[i].x.x * v[i].y * v[i].y);
    ld c0 = (v[i].y * v[i].y * v[i].x.x * v[i].x.x +
             v[i].y * v[i].y * v[i].x.y * v[i].x.y -
             v[j].y * v[j].y * v[j].x.x * v[j].x.x -
             v[j].y * v[j].y * v[j].x.y * v[j].x.y);

    ld a1 = 2 * (v[k].x.y * v[k].y * v[k].y - v[j].x.y * v[j].y * v[j].y);
    ld b1 = 2 * (v[k].x.x * v[k].y * v[k].y - v[j].x.x * v[j].y * v[j].y);
    ld c1 = (v[j].y * v[j].y * v[j].x.x * v[j].x.x +
             v[j].y * v[j].y * v[j].x.y * v[j].x.y -
             v[k].y * v[k].y * v[k].x.x * v[k].x.x -
             v[k].y * v[k].y * v[k].x.y * v[k].x.y);

    if (abs(a0) < EPS && abs(a1) < EPS) {
      return INF;
    }
    if (abs(a0) < EPS) {
      swap(a0, a1);
      swap(b0, b1);
      swap(c0, c1);
    }
    b0 /= a0;
    c0 /= a0;
    a0 = 1;
    b1 -= a1 * b0;
    c1 -= a1 * c0;
    a1 = 0;

    if (abs(b1) < EPS) {
      return INF;
    }

    ld x = -c1 / b1;
    ld y = -b0 * x - c0;

    ld t1 = get_t(v[i], x, y);
    ld t2 = get_t(v[j], x, y);
    ld t3 = get_t(v[k], x, y);
    if (max(abs(t1 - t2), abs(t2 - t3)) > EPS) {
      while (true) {
        t1 += 1;
      }
    }

    return checkpt(v, x, y, need);
  } else if (v[i].y != v[j].y && v[i].y != v[k].y) {
    swap(i, j);
  } else if (v[k].y != v[j].y && v[i].y != v[k].y) {
    swap(j, k);
  }
  ld dv0 = v[i].y * v[i].y - v[j].y * v[j].y;
  assert(abs(dv0) > EPS);
  ld al0 = 2 * (v[j].x.y * v[j].y * v[j].y - v[i].x.y * v[i].y * v[i].y) / dv0;
  ld bt0 = 2 * (v[j].x.x * v[j].y * v[j].y - v[i].x.x * v[i].y * v[i].y) / dv0;
  ld gm0 = (v[i].y * v[i].y * v[i].x.x * v[i].x.x +
            v[i].y * v[i].y * v[i].x.y * v[i].x.y -
            v[j].y * v[j].y * v[j].x.x * v[j].x.x -
            v[j].y * v[j].y * v[j].x.y * v[j].x.y) /
           dv0;

  ld dv1 = v[j].y * v[j].y - v[k].y * v[k].y;
  assert(abs(dv1) > EPS);
  ld al1 = 2 * (v[k].x.y * v[k].y * v[k].y - v[j].x.y * v[j].y * v[j].y) / dv1;
  ld bt1 = 2 * (v[k].x.x * v[k].y * v[k].y - v[j].x.x * v[j].y * v[j].y) / dv1;
  ld gm1 = (v[j].y * v[j].y * v[j].x.x * v[j].x.x +
            v[j].y * v[j].y * v[j].x.y * v[j].x.y -
            v[k].y * v[k].y * v[k].x.x * v[k].x.x -
            v[k].y * v[k].y * v[k].x.y * v[k].x.y) /
           dv1;

  ld cf1 = al0 - al1;
  ld cf2 = bt0 - bt1;
  ld cf3 = gm0 - gm1;

  vector<pair<ld, ld>> tocheck;
  if (abs(cf1) > EPS) {
    ld kk = -cf2 / cf1;
    ld bb = -cf3 / cf1;
    vector<ld> quad = {kk * kk + 1, 2 * kk * bb + al0 * kk + bt0,
                       gm0 + bb * bb + al0 * bb};
    auto xx = slvsq(quad);
    for (auto &x : xx) {
      // cerr << quad[0] * x * x + quad[1] * x + quad[2] << endl;
      // assert(abs(quad[0] * x * x + quad[1] * x + quad[2]) < EPS);
      tocheck.push_back({x, kk * x + bb});
    }
  } else if (abs(cf2) > EPS) {
    ld x = -cf3 / cf2;
    vector<ld> quad = {1, al0, bt0 * x + gm0 + x * x};
    auto yy = slvsq(quad);
    for (auto &y : yy) {
      tocheck.push_back({x, y});
    }
  } else {
    assert(abs(cf3) > EPS);
  }
  ld ans = INF;
  for (auto &p : tocheck) {
    ld x = p.x;
    ld y = p.y;

    ld t1 = get_t(v[i], x, y);
    ld t2 = get_t(v[j], x, y);
    ld t3 = get_t(v[k], x, y);
    if (max(abs(t1 - t2), abs(t2 - t3)) > EPS) {
      assert(false);
    }

    ans = min(ans, checkpt(v, x, y, need));
  }
  return ans;
}

inline void solve() {
  int n, k;
  cin >> n >> k;
  vector<pair<pair<ll, ll>, ll>> v(n);
  cin >> v;
  if (k == 1) {
    cout << 0 << endl;
    return;
  }
  if (k == 2) {
    ld tt = INF;
    for (int i = 0; i < n; ++i) {
      for (int j = i + 1; j < n; ++j) {
        ld dst = hypot(v[i].x.x - v[j].x.x, v[i].x.y - v[j].x.y);
        tt = min(tt, v[i].y * v[j].y * dst / (v[i].y + v[j].y));
      }
    }
    cout << tt << endl;
    return;
  }
  ld tt = INF;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      for (int kk = j + 1; kk < n; ++kk) {
        tt = min(tt, calc(v, i, j, kk, k));
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      ld cf = ld(v[j].y) / (v[i].y + v[j].y);
      ld x = v[i].x.x + (v[j].x.x - v[i].x.x) * cf;
      ld y = v[i].x.y + (v[j].x.y - v[i].x.y) * cf;
      tt = min(tt, checkpt(v, x, y, k));
    }
  }
  cout << tt << endl;
}

int main() {

  if (*infile != '\0')
    (void)freopen(infile, "r", stdin);
  if (*outfile != '\0')
    (void)freopen(outfile, "w", stdout);

  speedup;
  coutdouble;

  // int tst = 1;
  // srand(time(NULL));
  // cin >> tst;
  // scanf("%d", &tst);
  // while (tst-- > 0) {
  // while(true) {
  solve();
  // if ((tst & 0xF) == 0) {
  // cerr << "ok\n";
  // }
  // cerr << "/*-----------------*/\n";
  // }

#ifdef LOCAL
  cerr << "Time: " << (ld)clock() / CLOCKS_PER_SEC << endl;
  while (true)
    ;
#endif // LOCAL

  return 0;
}

Fixed submission:
#ifndef LOCAL
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,mmx,tune=native")
#endif // LOCAL

#define _SCL_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS

#pragma comment(linker, "/STACK:256000000")

// #define push_back pb
#define first x
#define second y

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <ciso646>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <forward_list>
#include <fstream>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#ifdef LOCAL
// #include <vld.h>
#endif // LOCAL
using namespace std;

// typedef __int128_t ll;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef long double ld;

#define speedup                                                                \
  cout.tie(nullptr);                                                           \
  cin.tie(nullptr);                                                            \
  ios_base::sync_with_stdio(false)
#define coutdouble                                                             \
  cout << setprecision(20);                                                    \
  cout << fixed

#define all(v) (v).begin(), (v).end()
#define sz(v) (int)(v).size()

#ifdef LOCAL
mt19937 rd(57322);
#else
mt19937 rd((uint)chrono::steady_clock::now().time_since_epoch().count());
#endif
/*------CommentInInteractive--------*/
#ifndef LOCAL
#define endl '\n'
#endif // LOCAL
/*----------------------------------*/
const int INF = 1000 * 1000 * 1000 + 322;
const ll LLINF = 2LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL + 57;
constexpr uint MOD = 1000 * 1000 * 1000 + 7;
constexpr uint FFT_MOD = 998244353;
const int P1E6 = 1336337;
const int P1E3 = 1009;
const ll P1E14 = 100000000000031;
const ll P1E17 = 100000000000000003;
const ld PI = acosl(-1.); // 3.1415926535897932384626433832795
const ld EPS = 1e-6;
/*------------------------------------------------IO_OPERATORS---------------------------------------------*/
template <typename T, typename U>
inline ostream &operator<<(ostream &_out, const pair<T, U> &_p) {
  _out << _p.first << " " << _p.second;
  return _out;
}
template <typename T, typename U>
inline istream &operator>>(istream &_in, pair<T, U> &_p) {
  _in >> _p.first >> _p.second;
  return _in;
}
template <typename T>
inline ostream &operator<<(ostream &_out, const vector<T> &_v) {
  if (_v.empty())
    return _out;
  _out << _v.front();
  for (auto _it = ++_v.begin(); _it != _v.end(); ++_it)
    _out << ' ' << *_it;
  return _out;
}
template <typename T> inline istream &operator>>(istream &_in, vector<T> &_v) {
  for (auto &_i : _v)
    _in >> _i;
  return _in;
}
template <typename T>
inline ostream &operator<<(ostream &_out, const set<T> &_s) {
  if (_s.empty())
    return _out;
  _out << *_s.begin();
  for (auto _it = ++_s.begin(); _it != _s.end(); ++_it)
    _out << ' ' << *_it;
  return _out;
}
template <typename T>
inline ostream &operator<<(ostream &_out, const multiset<T> &_s) {
  if (_s.empty())
    return _out;
  _out << *_s.begin();
  for (auto _it = ++_s.begin(); _it != _s.end(); ++_it)
    _out << ' ' << *_it;
  return _out;
}
template <typename T>
inline ostream &operator<<(ostream &_out, const unordered_set<T> &_s) {
  if (_s.empty())
    return _out;
  _out << *_s.begin();
  for (auto _it = ++_s.begin(); _it != _s.end(); ++_it)
    _out << ' ' << *_it;
  return _out;
}
template <typename T>
inline ostream &operator<<(ostream &_out, const unordered_multiset<T> &_s) {
  if (_s.empty())
    return _out;
  _out << *_s.begin();
  for (auto _it = ++_s.begin(); _it != _s.end(); ++_it)
    _out << ' ' << *_it;
  return _out;
}
template <typename T, typename U>
inline ostream &operator<<(ostream &_out, const map<T, U> &_m) {
  if (_m.empty())
    return _out;
  _out << '(' << _m.begin()->first << ": " << _m.begin()->second << ')';
  for (auto _it = ++_m.begin(); _it != _m.end(); ++_it)
    _out << ", (" << _it->first << ": " << _it->second << ')';
  return _out;
}
template <typename T, typename U>
inline ostream &operator<<(ostream &_out, const unordered_map<T, U> &_m) {
  if (_m.empty())
    return _out;
  _out << '(' << _m.begin()->first << ": " << _m.begin()->second << ')';
  for (auto _it = ++_m.begin(); _it != _m.end(); ++_it)
    _out << ", (" << _it->first << ": " << _it->second << ')';
  return _out;
}
/*--------------------------------------------------IO_FILES-----------------------------------------------*/
const char *infile =
#ifdef LOCAL
    "input.txt"
#else
    ""
#endif // LOCAL
    ;
const char *outfile =
#ifdef LOCAL
    ""
#else
    ""
#endif // LOCAL
    ;

/*-------------------------------------------------ALLOCATOR----------------------------------------------*/
// #define ALLOC_LOCAL
#ifdef ALLOC_LOCAL
const int ML_ = 450;
char mem_[ML_ * 1024 * 1024];
size_t _ptr = 0, _magic = 21323400;
void *operator new(size_t cnt) {
  if (_ptr + cnt < sizeof mem_) {
    _ptr += cnt;
    return mem_ + _ptr - cnt;
  } else {
    assert(false);
    _ptr = cnt + _magic;
    return mem_ + _magic;
  }
}
void operator delete(void *) {}
#endif // ALLOC_LOCAL
/*-----------------------------------------------------MATH------------------------------------------------*/
inline ll gcd(ll a, ll b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}
inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
inline int pwm(ll xx, ll pow, int MD) {
  if (pow < 0) {
    pow = pow % (MD - 1) + MD - 1;
  }
  ll mlt = 1;
  while (pow) {
    if (pow & 1) {
      mlt *= xx;
      mlt %= MD;
    }
    xx *= xx;
    pow >>= 1;
    xx %= MD;
  }
  return (int)mlt;
}
inline ll gcdex(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll xx, yy;
  ll gc = gcdex(b, a % b, yy, xx);
  x = xx;
  y = yy - (a / b) * xx;
  return gc;
}
inline int inv(ll r, int _mod) { return pwm(r % _mod, _mod - 2, _mod); }
/*-----------------------------------------------------CODE------------------------------------------------*/

vector<ld> slvsq(const vector<ld> &cf) {
  assert(sz(cf) == 3);
  ld det = cf[1] * cf[1] - 4 * cf[0] * cf[2];
  if (det < 0) {
    return {};
  }
  return {(-cf[1] - sqrtl(det)) / (2 * cf[0]),
          (-cf[1] + sqrtl(det)) / (2 * cf[0])};
}

ld get_t(const pair<pair<ll, ll>, ll> &pp, ld x, ld y) {
  return pp.y * hypotl(pp.x.x - x, pp.x.y - y);
}

ld checkpt(const vector<pair<pair<ll, ll>, ll>> &v, ld x, ld y, int k) {
  vector<ld> dsts;
  for (const auto &pp : v) {
    dsts.push_back(get_t(pp, x, y));
  }
  nth_element(dsts.begin(), dsts.begin() + k - 1, dsts.end());
  return dsts[k - 1];
}

ld calc(const vector<pair<pair<ll, ll>, ll>> &v, int i, int j, int k,
        int need) {
  if (v[i].y == v[j].y && v[i].y == v[k].y) {
    ld a0 = 2 * (v[j].x.y * v[j].y * v[j].y - v[i].x.y * v[i].y * v[i].y);
    ld b0 = 2 * (v[j].x.x * v[j].y * v[j].y - v[i].x.x * v[i].y * v[i].y);
    ld c0 = (v[i].y * v[i].y * v[i].x.x * v[i].x.x +
             v[i].y * v[i].y * v[i].x.y * v[i].x.y -
             v[j].y * v[j].y * v[j].x.x * v[j].x.x -
             v[j].y * v[j].y * v[j].x.y * v[j].x.y);

    ld a1 = 2 * (v[k].x.y * v[k].y * v[k].y - v[j].x.y * v[j].y * v[j].y);
    ld b1 = 2 * (v[k].x.x * v[k].y * v[k].y - v[j].x.x * v[j].y * v[j].y);
    ld c1 = (v[j].y * v[j].y * v[j].x.x * v[j].x.x +
             v[j].y * v[j].y * v[j].x.y * v[j].x.y -
             v[k].y * v[k].y * v[k].x.x * v[k].x.x -
             v[k].y * v[k].y * v[k].x.y * v[k].x.y);

    if (abs(a0) < EPS && abs(a1) < EPS) {
      return INF;
    }
    if (abs(a0) < EPS) {
      swap(a0, a1);
      swap(b0, b1);
      swap(c0, c1);
    }
    b0 /= a0;
    c0 /= a0;
    a0 = 1;
    b1 -= a1 * b0;
    c1 -= a1 * c0;
    a1 = 0;

    if (abs(b1) < EPS) {
      return INF;
    }

    ld x = -c1 / b1;
    ld y = -b0 * x - c0;

    ld t1 = get_t(v[i], x, y);
    ld t2 = get_t(v[j], x, y);
    ld t3 = get_t(v[k], x, y);
    if (max(abs(t1 - t2), abs(t2 - t3)) > EPS) {
      while (true) {
        t1 += 1;
      }
    }

    return checkpt(v, x, y, need);
  } else if (v[i].y != v[j].y && v[i].y != v[k].y) {
    swap(i, j);
  } else if (v[k].y != v[j].y && v[i].y != v[k].y) {
    swap(j, k);
  }
  ld dv0 = v[i].y * v[i].y - v[j].y * v[j].y;
  assert(abs(dv0) > EPS);
  ld al0 = 2 * (v[j].x.y * v[j].y * v[j].y - v[i].x.y * v[i].y * v[i].y) / dv0;
  ld bt0 = 2 * (v[j].x.x * v[j].y * v[j].y - v[i].x.x * v[i].y * v[i].y) / dv0;
  ld gm0 = (v[i].y * v[i].y * v[i].x.x * v[i].x.x +
            v[i].y * v[i].y * v[i].x.y * v[i].x.y -
            v[j].y * v[j].y * v[j].x.x * v[j].x.x -
            v[j].y * v[j].y * v[j].x.y * v[j].x.y) /
           dv0;

  ld dv1 = v[j].y * v[j].y - v[k].y * v[k].y;
  assert(abs(dv1) > EPS);
  ld al1 = 2 * (v[k].x.y * v[k].y * v[k].y - v[j].x.y * v[j].y * v[j].y) / dv1;
  ld bt1 = 2 * (v[k].x.x * v[k].y * v[k].y - v[j].x.x * v[j].y * v[j].y) / dv1;
  ld gm1 = (v[j].y * v[j].y * v[j].x.x * v[j].x.x +
            v[j].y * v[j].y * v[j].x.y * v[j].x.y -
            v[k].y * v[k].y * v[k].x.x * v[k].x.x -
            v[k].y * v[k].y * v[k].x.y * v[k].x.y) /
           dv1;

  ld cf1 = al0 - al1;
  ld cf2 = bt0 - bt1;
  ld cf3 = gm0 - gm1;

  vector<pair<ld, ld>> tocheck;
  if (abs(cf1) > EPS) {
    ld kk = -cf2 / cf1;
    ld bb = -cf3 / cf1;
    vector<ld> quad = {kk * kk + 1, 2 * kk * bb + al0 * kk + bt0,
                       gm0 + bb * bb + al0 * bb};
    auto xx = slvsq(quad);
    for (auto &x : xx) {
      // cerr << quad[0] * x * x + quad[1] * x + quad[2] << endl;
      // assert(abs(quad[0] * x * x + quad[1] * x + quad[2]) < EPS);
      tocheck.push_back({x, kk * x + bb});
    }
  } else if (abs(cf2) > EPS) {
    ld x = -cf3 / cf2;
    vector<ld> quad = {1, al0, bt0 * x + gm0 + x * x};
    auto yy = slvsq(quad);
    for (auto &y : yy) {
      tocheck.push_back({x, y});
    }
  } else {
    assert(abs(cf3) > EPS);
  }
  ld ans = INF;
  for (auto &p : tocheck) {
    ld x = p.x;
    ld y = p.y;

    ld t1 = get_t(v[i], x, y);
    ld t2 = get_t(v[j], x, y);
    ld t3 = get_t(v[k], x, y);
    // if (max(abs(t1 - t2), abs(t2 - t3)) > EPS) {
    //	assert(false);
    // }

    ans = min(ans, checkpt(v, x, y, need));
  }
  return ans;
}

inline void solve() {
  int n, k;
  cin >> n >> k;
  vector<pair<pair<ll, ll>, ll>> v(n);
  cin >> v;
  if (k == 1) {
    cout << 0 << endl;
    return;
  }
  if (k == 2) {
    ld tt = INF;
    for (int i = 0; i < n; ++i) {
      for (int j = i + 1; j < n; ++j) {
        ld dst = hypot(v[i].x.x - v[j].x.x, v[i].x.y - v[j].x.y);
        tt = min(tt, v[i].y * v[j].y * dst / (v[i].y + v[j].y));
      }
    }
    cout << tt << endl;
    return;
  }
  ld tt = INF;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      for (int kk = j + 1; kk < n; ++kk) {
        tt = min(tt, calc(v, i, j, kk, k));
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      ld cf = ld(v[j].y) / (v[i].y + v[j].y);
      ld x = v[i].x.x + (v[j].x.x - v[i].x.x) * cf;
      ld y = v[i].x.y + (v[j].x.y - v[i].x.y) * cf;
      tt = min(tt, checkpt(v, x, y, k));
    }
  }
  cout << tt << endl;
}

int main() {

  if (*infile != '\0')
    (void)freopen(infile, "r", stdin);
  if (*outfile != '\0')
    (void)freopen(outfile, "w", stdout);

  speedup;
  coutdouble;

  // int tst = 1;
  // srand(time(NULL));
  // cin >> tst;
  // scanf("%d", &tst);
  // while (tst-- > 0) {
  // while(true) {
  solve();
  // if ((tst & 0xF) == 0) {
  // cerr << "ok\n";
  // }
  // cerr << "/*-----------------*/\n";
  // }

#ifdef LOCAL
  cerr << "Time: " << (ld)clock() / CLOCKS_PER_SEC << endl;
  while (true)
    ;
#endif // LOCAL

  return 0;
}