Problem:
Vector Compression
You are recording a result of a secret experiment, which consists of a large set of
N
-dimensional vectors.
Since the result may become very large, you are thinking of
compressing it. Fortunately you already have a good compression method
for vectors with small absolute values, all you have to do is to
preprocess the vectors and make them small.
You can record the set of vectors in any order you like.
Let's assume you process them in the order
v_1
,
v_2
,...,
v_M
.
Each vector
v_i
is recorded either
as is
, or as a difference vector.
When it is recorded as a difference,
you can arbitrarily pick up an already chosen vector
v_j (j<i)
and a real value
r
.
Then the actual vector value recorded is
(v_i - r v_j)
.
The values of
r
and
j
do not affect the compression ratio so much, so you don't have to care about them.
Given  a set of vectors,
your task is to write a program  that calculates the minimum sum of the squared length of the  recorded vectors.
Input
The input is like the following style.
N
M
v_{1,1}
v_{1,2}
...
v_{1,N}
...
v_{M,1}
v_{M,2}
...
v_{M,N}
The first line contains two integers
N
and
M
(
1 \leq N, M \leq 100
),
where
N
is the dimension of each vector,
and
M
is the number of the vectors.
Each of the following
M
lines contains
N
floating point values
v_{i,j}
(
-1.0 \leq v_{i,j} \leq 1.0
)
which represents the
j
-th element value of the
i
-th vector.
Output
Output the minimum sum of the squared length of the  recorded vectors. 
The output should not contain an absolute error greater than
10^{-6}
.
Sample Input 1
2 3
1.0 1.0
-1.0 0.0
0.5 0.5
Output for the Sample Input 1
1.0
Sample Input 2
1 1
1.0
Output for the Sample Input 2
1.0
Sample Input 3
4 3
1.0 1.0 0.0 0.0
-1.0 0.0 -1.0 0.0
0.5 0.5 0.5 0.5
Output for the Sample Input 3
3.0

Submitted solutions:

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

/**
 * @brief template
 */

struct SCC{
    int n,m,cur;
    vector<vector<int>> g;
    vector<int> low,ord,id;
    SCC(int _n=0):n(_n),m(0),cur(0),g(_n),low(_n),ord(_n,-1),id(_n){}
    void resize(int _n){
        n=_n;
        g.resize(n);
        low.resize(n);
        ord.resize(n,-1);
        id.resize(n);
    }
    void add_edge(int u,int v){g[u].emplace_back(v);}
    void dfs(int v,vector<int>& used){
        ord[v]=low[v]=cur++;
        used.emplace_back(v);
        for(auto& nxt:g[v]){
            if(ord[nxt]==-1){
                dfs(nxt,used); chmin(low[v],low[nxt]);
            }
            else{
                chmin(low[v],ord[nxt]);
            }
        }
        if(ord[v]==low[v]){
            while(1){
                int add=used.back(); used.pop_back();
                ord[add]=n; id[add]=m;
                if(v==add)break;
            }
            m++;
        }
    }
    void run(){
        vector<int> used;
        rep(v,0,n)if(ord[v]==-1)dfs(v,used);
        for(auto& x:id)x=m-1-x;
    }
};

/**
 * @brief Strongly Connected Components
 */

template <typename T>
struct DirectedMST {
    const vector<vector<T>>& G;
    T _INF;

    DirectedMST(const vector<vector<T>>& G) : _INF(numeric_limits<T>::max()), G(G) {}

    T DFS(vector<vector<T>>& G, int Root, T Cur) {
        int N = G.size();
        vector<int> rev(N, -1);
        vector<T> weight(N, _INF);
        rep(i,0,N) {
            rep(j,0,N) {
                if (chmin(weight[j], G[i][j])) {
                    rev[j] = i;
                }
            }
        }
        SCC scc(N);
        rep(i,0,N) {
            if (i == Root) continue;
            if (rev[i] == -1) return _INF;
            scc.add_edge(rev[i],i);
            Cur += weight[i];
        }
        scc.run();
        if (scc.m == N) return Cur;
        vector<vector<T>> NewG(scc.m,vector<T>(scc.m,_INF));
        rep(i,0,N) {
            rep(j,0,N) {
                if (scc.id[i] == scc.id[j]) continue;
                chmin(NewG[scc.id[i]][scc.id[j]], G[i][j]-weight[j]);
            }
        }
        return DFS(NewG, scc.id[Root], Cur);
    }
};

using Vec = vector<double>;

double dot(Vec A, Vec B) {
    int N = A.size();
    double Ret = 0.0;
    rep(i,0,N) Ret += A[i] * B[i];
    return Ret;
}

int main() {
    int N, M;
    cin >> M >> N;
    vector<Vec> V(N,vector<double>(M));
    rep(i,0,N) rep(j,0,M) cin >> V[i][j];
    vector<vector<double>> G(N+1,vector<double>(N+1,inf));
    rep(i,0,N) {
        rep(j,0,N) {
            if (i == j) continue;
            if (dot(V[i],V[i]) == 0.0) G[i][j] = dot(V[j],V[j]);
            G[i][j] = (dot(V[i],V[i])*dot(V[j],V[j])-dot(V[i],V[j])*dot(V[i],V[j])) / dot(V[i],V[i]);
        }
    }
    rep(i,0,N) {
        G[N][i] = dot(V[i],V[i]);
    }
    DirectedMST<double> mst(G);
    printf("%.12f\n",mst.DFS(G,N,0.0));
}
accuracy: 1
time_ms: 10
memory_kb: 4456
final_rank: 6

code_snippet: // #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;

template< typename T = int >
struct Edge {
  int from, to;
  T cost;
  int idx;

  Edge() = default;

  Edge(int from, int to, T cost = 1, int idx = -1) : from(from), to(to), cost(cost), idx(idx) {}

  operator int() const { return to; }
};

template< typename T = int >
struct Graph {
  vector< vector< Edge< T > > > g;
  int es;

  Graph() = default;

  explicit Graph(int n) : g(n), es(0) {}

  size_t size() const {
    return g.size();
  }

  void add_directed_edge(int from, int to, T cost = 1) {
    g[from].emplace_back(from, to, cost, es++);
  }

  void add_edge(int from, int to, T cost = 1) {
    g[from].emplace_back(from, to, cost, es);
    g[to].emplace_back(to, from, cost, es++);
  }

  void read(int M, int padding = -1, bool weighted = false, bool directed = false) {
    for(int i = 0; i < M; i++) {
      int a, b;
      cin >> a >> b;
      a += padding;
      b += padding;
      T c = T(1);
      if(weighted) cin >> c;
      if(directed) add_directed_edge(a, b, c);
      else add_edge(a, b, c);
    }
  }
};

template< typename T = int >
using Edges = vector< Edge< T > >;


/**
 * @brief Skew-Heap
 */
template< typename T, bool isMin = true >
struct SkewHeap {
  struct Node {
    T key, lazy;
    Node *l, *r;
    int idx;

    explicit Node(const T &key, int idx) : key(key), idx(idx), lazy(0), l(nullptr), r(nullptr) {}
  };

  SkewHeap() = default;

  Node *alloc(const T &key, int idx = -1) {
    return new Node(key, idx);
  }

  Node *propagate(Node *t) {
    if(t && t->lazy != 0) {
      if(t->l) t->l->lazy += t->lazy;
      if(t->r) t->r->lazy += t->lazy;
      t->key += t->lazy;
      t->lazy = 0;
    }
    return t;
  }

  Node *meld(Node *x, Node *y) {
    propagate(x), propagate(y);
    if(!x || !y) return x ? x : y;
    if((x->key < y->key) ^ isMin) swap(x, y);
    x->r = meld(y, x->r);
    swap(x->l, x->r);
    return x;
  }

  Node *push(Node *t, const T &key, int idx = -1) {
    return meld(t, alloc(key, idx));
  }

  Node *pop(Node *t) {
    assert(t != nullptr);
    return meld(t->l, t->r);
  }

  Node *add(Node *t, const T &lazy) {
    if(t) {
      t->lazy += lazy;
      propagate(t);
    }
    return t;
  }

  Node *make_root() {
    return nullptr;
  }
};


/**
 * @brief Directed-Minimum-Spanning-Tree(最小有向全域木)
 */
template< typename T >
struct MinimumSpanningTree {
  T cost;
  Edges< T > edges;
};

template< typename T >
MinimumSpanningTree< T > directed_minimum_spanning_tree(int V, int root, Edges< T > edges) {
  for(int i = 0; i < V; ++i) {
    if(i != root) edges.emplace_back(i, root, 0);
  }

  int x = 0;
  vector< int > par(2 * V, -1), vis(par), link(par);

  using Heap = SkewHeap< T, true >;
  using Node = typename Heap::Node;

  Heap heap;
  vector< Node * > ins(2 * V, heap.make_root());

  for(int i = 0; i < (int) edges.size(); i++) {
    auto &e = edges[i];
    ins[e.to] = heap.push(ins[e.to], e.cost, i);
  }
  vector< int > st;
  auto go = [&](int x) {
    x = edges[ins[x]->idx].from;
    while(link[x] != -1) {
      st.emplace_back(x);
      x = link[x];
    }
    for(auto &p : st) {
      link[p] = x;
    }
    st.clear();
    return x;
  };
  for(int i = V; ins[x]; i++) {
    for(; vis[x] == -1; x = go(x)) vis[x] = 0;
    for(; x != i; x = go(x)) {
      auto w = ins[x]->key;
      auto v = heap.pop(ins[x]);
      v = heap.add(v, -w);
      ins[i] = heap.meld(ins[i], v);
      par[x] = i;
      link[x] = i;
    }
    for(; ins[x] && go(x) == x; ins[x] = heap.pop(ins[x]));
  }
  T cost = 0;
  Edges< T > ans;
  for(int i = root; i != -1; i = par[i]) {
    vis[i] = 1;
  }
  for(int i = x; i >= 0; i--) {
    if(vis[i] == 1) continue;
    cost += edges[ins[i]->idx].cost;
    ans.emplace_back(edges[ins[i]->idx]);
    for(int j = edges[ins[i]->idx].to; j != -1 && vis[j] == 0; j = par[j]) {
      vis[j] = 1;
    }
  }
  return {cost, ans};
}

int main() {
    int N, M; scanf("%d%d", &N, &M);
    vector< vector<double> > V(M, vector<double>(N));
    for(int i=0; i<M; i++) {
        for(int j=0; j<N; j++) {
            scanf("%lf", &V[i][j]);
        }
    }

    auto calc_cost = [&](int x, int y) {
                         double ub = INF, lb = -INF;
                         for(int i=0; i<200; i++) {
                             double m1 = (lb + lb + ub) / 3.0;
                             double m2 = (lb + ub + ub) / 3.0;
                             double f1 = 0.0, f2 = 0.0;
                             for(int i=0; i<N; i++) {
                                 f1 += (V[x][i] - m1 * V[y][i]) * (V[x][i] - m1 * V[y][i]);
                                 f2 += (V[x][i] - m2 * V[y][i]) * (V[x][i] - m2 * V[y][i]);                                   
                             }
                             if(f1 < f2) ub = m2;
                             else lb = m1;
                         }
                         double m = lb;
                         double f = 0.0;
                         for(int i=0; i<N; i++) {
                             f += (V[x][i] - m * V[y][i]) * (V[x][i] - m * V[y][i]);
                         }
                         return f;
                     };

    Edges<double> edges;
    for(int i=0; i<M; i++) {
        double cost = 0.0;
        for(int j=0; j<N; j++) {
            cost += V[i][j] * V[i][j];
        }
        edges.emplace_back(M, i, cost);
        for(int j=0; j<M; j++) {
            if(i == j) continue;
            edges.emplace_back(j, i, calc_cost(i, j));
        }
    }
    printf("%.12f\n", directed_minimum_spanning_tree(M+1, M, edges).cost);
    return 0;
}
accuracy: 1
time_ms: 220
memory_kb: 3948
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
const double INF = 100000;
int main(){
  cout << fixed << setprecision(20);
  int N, M;
  cin >> N >> M;
  vector<vector<double>> A(M, vector<double>(N));
  for (int i = 0; i < M; i++){
    for (int j = 0; j < N; j++){
      cin >> A[i][j];
    }
  }
  vector<vector<pair<double, int>>> E(M + 1);
  for (int i = 0; i < M; i++){
    for (int j = 0; j < M; j++){
      if (i != j){
        double a = 0, b = 0, c = 0;
        for (int k = 0; k < N; k++){
          a += A[i][k] * A[i][k];
          b -= 2 * A[i][k] * A[j][k];
          c += A[j][k] * A[j][k];
        }
        double d = c - (b * b) /  (4 * a);
        E[i].push_back(make_pair(d, j));
      }
    }
  }
  for (int i = 0; i < M; i++){
    double sum = 0;
    for (int j = 0; j < N; j++){
      sum += A[i][j] * A[i][j];
    }
    E[M].push_back(make_pair(sum, i));
  }
  int r = M;
  double ans = 0;
  while (true){
    int V = E.size();
    vector<double> mn(V, INF);
    vector<int> p(V);
    for (int i = 0; i < V; i++){
      for (auto P : E[i]){
        double c = P.first;
        int w = P.second;
        if (w != r && c < mn[w]){
          mn[w] = c;
          p[w] = i;
        }
      }
    }
    vector<int> d(V, 0);
    for (int i = 0; i < V; i++){
      if (i != r){
        d[p[i]]++;
      }
    }
    queue<int> Q;
    for (int i = 0; i < V; i++){
    	if (i != r && d[i] == 0){
        Q.push(i);
      }
    }
    while (!Q.empty()){
      int v = Q.front();
      Q.pop();
      if (v != r){
        d[p[v]]--;
        if (d[p[v]] == 0){
          Q.push(p[v]);
        }
      }
    }
    vector<int> cycle;
    vector<int> id(V, -1);
    for (int i = 0; i < V; i++){
      if (d[i] == 1){
        cycle.push_back(i);
        while (true){
          int v = cycle.back();
          ans += mn[v];
          id[v] = 0;
          if (p[v] == i){
            break;
          }
          cycle.push_back(p[v]);
        }
        break;
      }
    }
    if (cycle.empty()){
      for (int i = 0; i < V; i++){
        if (i != r){
          ans += mn[i];
        }
      }
      break;
    }
    int cnt = 1;
    for (int i = 0; i < V; i++){
      if (id[i] == -1){
        id[i] = cnt;
        cnt++;
      }
    }
    vector<vector<pair<double, int>>> E2(cnt);
    for (int i = 0; i < V; i++){
      for (auto P : E[i]){
        double c = P.first;
        int w = P.second;
        if (id[w] == 0){
          c -= mn[w];
        }
        if (id[i] != id[w]){
          E2[id[i]].push_back(make_pair(c, id[w]));
        }
      }
    }
    swap(E, E2);
    r = id[r];
  }
  cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3500
final_rank: 1

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
#define endl "\n"
using namespace std;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

const long long INF = 1e18;
//const ll mod = 1000000007;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef vector<int> vi;

struct RollbackUF {
	vi e; vector<pii> st;
	RollbackUF(int n) : e(n, -1) {}
	int size(int x) { return -e[find(x)]; }
	int find(int x) { return e[x] < 0 ? x : find(e[x]); }
	int time() { return sz(st); }
	void rollback(int t) {
		for (int i = time(); i --> t;)
			e[st[i].first] = st[i].second;
		st.resize(t);
	}
	bool join(int a, int b) {
		a = find(a), b = find(b);
		if (a == b) return false;
		if (e[a] > e[b]) swap(a, b);
		st.push_back({a, e[a]});
		st.push_back({b, e[b]});
		e[a] += e[b]; e[b] = a;
		return true;
	}
};
struct Edge { int a, b; double w; };
struct Node { /// lazy skew heap node
	Edge key;
	Node *l, *r;
	double delta;
	void prop() {
		key.w += delta;
		if (l) l->delta += delta;
		if (r) r->delta += delta;
		delta = 0;
	}
	Edge top() { prop(); return key; }
};
Node *merge(Node *a, Node *b) {
	if (!a || !b) return a ?: b;
	a->prop(), b->prop();
	if (a->key.w > b->key.w) swap(a, b);
	swap(a->l, (a->r = merge(b, a->r)));
	return a;
}
void pop(Node*& a) { a->prop(); a = merge(a->l, a->r); }

double dmst(int n, int r, vector<Edge>& g) {
	RollbackUF uf(n);
	vector<Node*> heap(n);
	for (Edge e : g) heap[e.b] = merge(heap[e.b], new Node{e});
	double res = 0;
	vi seen(n, -1), path(n), par(n);
	seen[r] = r;
	vector<Edge> Q(n), in(n, {-1,-1}), comp;
	deque<tuple<int, int, vector<Edge>>> cycs;
	rep(s,0,n) {
		int u = s, qi = 0, w;
		while (seen[u] < 0) {
			if (!heap[u]) return -1;
			Edge e = heap[u]->top();
			heap[u]->delta -= e.w, pop(heap[u]);
			Q[qi] = e, path[qi++] = u, seen[u] = s;
			res += e.w, u = uf.find(e.a);
			if (seen[u] == s) { /// found cycle, contract
				Node* cyc = 0;
				int end = qi, time = uf.time();
				do cyc = merge(cyc, heap[w = path[--qi]]);
				while (uf.join(u, w));
				u = uf.find(u), heap[u] = cyc, seen[u] = -1;
				cycs.push_front({u, time, {&Q[qi], &Q[end]}});
			}
		}
		rep(i,0,qi) in[uf.find(Q[i].b)] = Q[i];
	}

	for (auto& [u,t,comp] : cycs) { // restore sol (optional)
		uf.rollback(t);
		Edge inEdge = in[u];
		for (auto& e : comp) in[uf.find(e.b)] = e;
		in[uf.find(inEdge.b)] = inEdge;
	}
	rep(i,0,n) par[i] = in[i].a;
	return res;
}

double dist[105][105];

vector<double> Minus(vector<double> a, vector<double> b) {
	assert(a.size() == b.size());
	int n = a.size();
	vector<double> ret(n);
	for(int i = 0; i < n; i++) ret[i] = a[i] - b[i];
	return ret;
}
vector<double> prod(double t, vector<double> v) {
	for(auto &tmp : v) tmp *= t;
	return v;
}
double norm(vector<double> v) {
	double ret = 0;
	for(auto tmp : v) ret += tmp * tmp;
	return ret;
}

double f(vector<double> a, vector<double> b) {
	double r = 1e5;
	double l = -1e5;
	for(int t = 0; t < 60; t++) {
		double ll = (l * 2 + r) / 3;
		double rr = (l + r * 2) / 3;
    	if(norm(Minus(b, prod(ll, a))) < norm(Minus(b, prod(rr, a)))) {
		    r = rr;
    	} else {
		    l = ll;
	    }
	}
	return norm(Minus(b, prod(l, a)));
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
	int N, M;
	cin >> N >> M;
	vector<vector<double>> A(M + 1, vector<double>(N));
	for(int i = 1; i <= M; i++) {
		for(int j = 0; j < N; j++) cin >> A[i][j];
	}
	vector<Edge> edges;
	for(int i = 0; i <= M; i++) {
		for(int j = 0; j <= M; j++) {
			dist[i][j] = f(A[i], A[j]);
			//cerr << dist[i][j] << " ";
		}
		//cerr << endl;
		for(int j = 0; j <= M; j++) {
			if(i == j) continue;
			edges.push_back({i, j, dist[i][j]});
		}
	}
	double ans = dmst(M + 1, 0, edges);
	cout << fixed << setprecision(30) << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 370
memory_kb: 4156
final_rank: 9

code_snippet: #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<deque>
#include<iomanip>
#include<tuple>
#include<cassert>
#include<set>
using namespace std;
typedef long long int LL;
typedef pair<int,int> P;
typedef pair<long double,int> DP;
typedef pair<LL,int> LP;
const long double INF=1e9+7;

void array_show(int *array,int array_n,char middle=' '){
    for(int i=0;i<array_n;i++)printf("%d%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(LL *array,int array_n,char middle=' '){
    for(int i=0;i<array_n;i++)printf("%lld%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){
    if(vec_n==-1)vec_n=vec_s.size();
    for(int i=0;i<vec_n;i++)printf("%d%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
void array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){
    if(vec_n==-1)vec_n=vec_s.size();
    for(int i=0;i<vec_n;i++)printf("%lld%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}

long double vec[111][111];
vector<vector<long double>> dis;

int main(){
    int n,m;
    int i,j,k;
    int a,b,c;
    long double da,db,dc;
    cin>>m>>n;
    dis.assign(n,vector<long double>(n,INF));
    for(i=0;i<n;i++){
        for(j=0;j<m;j++){
            cin>>vec[i][j];
        }
    }
    for(i=0;i<n;i++){
        for(j=0;j<n;j++){
            if(i==j)continue;
            da=0,db=0;
            for(k=0;k<m;k++){
                da+=pow(vec[i][k],2),db+=2*vec[i][k]*vec[j][k];
            }
            if(da>0)db/=2*da;
            da=0;
            for(k=0;k<m;k++){
                da+=pow(vec[j][k]-vec[i][k]*db,2);
            }
            dis[i][j]=da;
        }
    }
    long double ds=INF;
    for(i=0;i<n;i++){
        db=0;
        for(j=0;j<m;j++){
            db+=pow(vec[i][j],2);
        }
        vector<vector<long double>> dis_temp=dis;
        vector<int> v1,v2,par(n,-1);
        for(j=0;j<n;j++){
            if(i!=j)v1.push_back(j);
        }
        while(!v1.empty()){
            a=v1.back(),v1.pop_back();
            da=INF,b=0;
            for(j=0;j<n;j++){
                if(da>dis_temp[j][a]){
                    da=dis_temp[j][a];
                    b=j;
                }
            }
            for(j=0;j<n;j++){
                if(j==b)dis_temp[j][a]=INF;
                else dis_temp[j][a]-=da;
            }
            db+=da,par[a]=b;
            v2.clear();
            c=a;
            while(c!=-1){
                v2.push_back(c);
                c=par[c];
                if(c==a)break;
            }
            if(c==-1)continue;
            a=v2.size();
            for(j=0;j<a;j++){
                for(k=0;k<a;k++){
                    b=v2[j],c=v2[k];
                    dis_temp[b][c]=INF;
                }
            }
            for(j=0;j<n;j++){
                b=v2[0];
                for(k=1;k<a;k++){
                    c=v2[k];
                    dis_temp[b][j]=min(dis_temp[b][j],dis_temp[c][j]);
                    dis_temp[j][b]=min(dis_temp[j][b],dis_temp[j][c]);
                    dis_temp[j][c]=INF,dis_temp[c][j]=INF;
                }
            }
            for(j=0;j<a;j++){
                b=v2[j];
                par[b]=-1;
                for(k=0;k<n;k++){
                    if(par[k]==b)par[k]=v2[0];
                }
            }
            v1.push_back(v2[0]);
        }
        ds=min(ds,db);
    }
    cout<<fixed<<setprecision(15)<<ds<<endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3604
final_rank: 4

code_snippet: #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<deque>
#include<iomanip>
#include<tuple>
#include<cassert>
#include<set>
using namespace std;
typedef long long int LL;
typedef pair<int,int> P;
typedef pair<long double,int> DP;
typedef pair<LL,int> LP;
const long double INF=1e9+7;

void array_show(int *array,int array_n,char middle=' '){
    for(int i=0;i<array_n;i++)printf("%d%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(LL *array,int array_n,char middle=' '){
    for(int i=0;i<array_n;i++)printf("%lld%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){
    if(vec_n==-1)vec_n=vec_s.size();
    for(int i=0;i<vec_n;i++)printf("%d%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
void array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){
    if(vec_n==-1)vec_n=vec_s.size();
    for(int i=0;i<vec_n;i++)printf("%lld%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}

long double vec[111][111];
vector<vector<long double>> dis;

int main(){
    int n,m;
    int i,j,k;
    int a,b,c;
    long double da,db,dc;
    cin>>m>>n;
    dis.assign(n,vector<long double>(n,INF));
    for(i=0;i<n;i++){
        for(j=0;j<m;j++){
            cin>>vec[i][j];
        }
    }
    for(i=0;i<n;i++){
        for(j=0;j<n;j++){
            if(i==j)continue;
            da=0,db=0;
            for(k=0;k<m;k++){
                da+=pow(vec[i][k],2),db+=2*vec[i][k]*vec[j][k];
            }
            if(da>0)db/=2*da;
            da=0;
            for(k=0;k<m;k++){
                da+=pow(vec[j][k]-vec[i][k]*db,2);
            }
            dis[i][j]=da;
        }
    }
    long double ds=INF;
    for(i=0;i<n;i++){
        db=0;
        for(j=0;j<m;j++){
            db+=pow(vec[i][j],2);
        }
        vector<vector<long double>> dis_temp=dis;
        vector<int> v1,v2,par(n,-1);
        
        for(int pos=0;pos<n;pos++){
            a=pos;
            while(a!=i){
                if(par[a]!=-1)break;
                da=INF,b=0;
                for(j=0;j<n;j++){
                    if(da>dis_temp[j][a]){
                        da=dis_temp[j][a];
                        b=j;
                    }
                }
                for(j=0;j<n;j++){
                    if(j==b)dis_temp[j][a]=INF;
                    else dis_temp[j][a]-=da;
                }
                db+=da,par[a]=b;
                v2.clear();
                c=a;
                while(c!=-1){
                    v2.push_back(c);
                    if(par[c]==a || par[c]<0)break;
                    c=par[c];
                }
                if(par[c]<0){
                    a=c;
                    continue;
                }
                int sz=v2.size();
                for(j=0;j<sz;j++){
                    for(k=0;k<sz;k++){
                        b=v2[j],c=v2[k];
                        dis_temp[b][c]=INF;
                    }
                }
                for(j=0;j<n;j++){
                    b=v2[0];
                    for(k=1;k<sz;k++){
                        c=v2[k];
                        dis_temp[b][j]=min(dis_temp[b][j],dis_temp[c][j]);
                        dis_temp[j][b]=min(dis_temp[j][b],dis_temp[j][c]);
                        dis_temp[j][c]=INF,dis_temp[c][j]=INF;
                    }
                }
                for(j=1;j<sz;j++){
                    b=v2[j];
                    par[b]=-2;
                    for(k=0;k<n;k++){
                        if(par[k]==b)par[k]=a;
                    }
                }
                par[a]=-1;
            }
        }
        ds=min(ds,db);
    }
    cout<<fixed<<setprecision(15)<<ds<<endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3592
final_rank: 2

code_snippet: #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<deque>
#include<iomanip>
#include<tuple>
#include<cassert>
#include<set>
using namespace std;
typedef long long int LL;
typedef pair<int,int> P;
typedef pair<long double,int> DP;
typedef pair<LL,int> LP;
const long double INF=1e9+7;

void array_show(int *array,int array_n,char middle=' '){
    for(int i=0;i<array_n;i++)printf("%d%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(LL *array,int array_n,char middle=' '){
    for(int i=0;i<array_n;i++)printf("%lld%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){
    if(vec_n==-1)vec_n=vec_s.size();
    for(int i=0;i<vec_n;i++)printf("%d%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
void array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){
    if(vec_n==-1)vec_n=vec_s.size();
    for(int i=0;i<vec_n;i++)printf("%lld%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}

long double vec[111][111];
vector<vector<long double>> dis;
priority_queue<DP,vector<DP>,greater<DP>> q1;

int main(){
    int n,m;
    int i,j,k;
    int a,b,c;
    long double da,db,dc;
    cin>>m>>n;
    dis.assign(n,vector<long double>(n,INF));
    for(i=0;i<n;i++){
        for(j=0;j<m;j++){
            cin>>vec[i][j];
        }
    }
    for(i=0;i<n;i++){
        for(j=0;j<n;j++){
            if(i==j)continue;
            da=0,db=0;
            for(k=0;k<m;k++){
                if(vec[i][k]==0)continue;
                da+=pow(vec[i][k],2),db+=2*vec[i][k]*vec[j][k];
            }
            if(da>0)db/=2*da;
            da=0;
            for(k=0;k<m;k++){
                da+=pow(vec[j][k]-vec[i][k]*db,2);
            }
            dis[i][j]=da;
        }
    }
    long double ds=INF;
    for(i=0;i<n;i++){
        vector<char> used(n);
        da=0;
        for(j=0;j<m;j++){
            da+=pow(vec[i][j],2);
        }
        q1.push(make_pair(da,i));
        db=0;
        while(!q1.empty()){
            da=q1.top().first,a=q1.top().second,q1.pop();
            if(used[a])continue;
            used[a]=1;
            db+=da;
            for(j=0;j<n;j++){
                if(a==j)continue;
                q1.push(make_pair(dis[a][j],j));
            }
        }
        ds=min(ds,db);
    }
    cout<<fixed<<setprecision(15)<<ds<<endl;
}
accuracy: 0.061224489795918366
time_ms: 50
memory_kb: 3488
final_rank: 16

code_snippet: #include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acosl(-1.0);

ll mod_pow(ll x, ll n, ll m = mod) {
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n % mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }

const int max_n = 1000;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}

int gcd(int a, int b) {
	if (a < b)swap(a, b);
	while (b) {
		int r = a % b; a = b; b = r;
	}
	return a;
}
struct uf {
private:
	vector<int> par, ran;
public:
	uf(int n) {
		par.resize(n, 0);
		ran.resize(n, 0);
		rep(i, n) {
			par[i] = i;
		}
	}
	int find(int x) {
		if (par[x] == x)return x;
		else return par[x] = find(par[x]);
	}
	void unite(int x, int y) {
		x = find(x), y = find(y);
		if (x == y)return;
		if (ran[x] < ran[y]) {
			par[x] = y;
		}
		else {
			par[y] = x;
			if (ran[x] == ran[y])ran[x]++;
		}
	}
	bool same(int x, int y) {
		return find(x) == find(y);
	}
};

using tval = ld;
struct edge {
	int to; tval cost;
};

vector<P> chu_liu_edmonds(vector<vector<edge>>&G, vector<bool> exi,int root=-1) {
	//cout << "wow\n";
	int n = G.size();
	if (root < 0) {
		root = n;
		G.push_back({});
		rep(i, n)G[root].push_back({ i,INF/10000 });
		n++;
		exi.push_back(true);
	}
	vector<vector<edge>> rG(n);
	rep(i, n)if(exi[i])for (edge e : G[i]) {
		rG[e.to].push_back({ i,e.cost });
	}
	vector<int> par(n);
	vector<tval> memo(n);
	rep(i, n)if (exi[i] && i != root) {
		tval mi = INF;
		for (edge e: rG[i]) {
			if (e.cost < mi) {
				mi = e.cost;
				par[i] = e.to;
			}
		}
		//cout << par[i] << " " << i << "\n";
		memo[i] = mi;
	}
	vector<bool> used(n);
	vector<bool> cused(n);
	vector<int> ts;
	used[root] = true;
	rep(i, n)if (exi[i] && i != root) {
		if (used[i])continue;
		vector<int> ids;
		int cur = i;
		cused[i] = true;
		ids.push_back(i);
		bool f = false;
		while (true) {
			cur = par[cur];
			if (used[cur]) {
				for (int id : ids) {
					cused[id] = false;
					used[id] = true;
				}
				break;
			}
			else if (cused[cur]) {
				f = true;
				rep(j, ids.size())if (ids[j] == cur) {
					ids.erase(ids.begin(), ids.begin() + j);
					break;
				}
				break;
			}
			else {
				cused[cur] = true;
				ids.push_back(cur);
			}
		}
		if (f) {
			ts = ids; break;
		}
	}
	if (ts.empty()) {
		vector<P> res; rep(i, n)if (exi[i] && i != root)res.push_back({ par[i],i });
		return res;
	}
	else {
		int cr = ts[0];
		vector<tval> mifr(n, INF), mito(n, INF);
		vector<int> idfr(n, -1), idto(n, -1);
		
		for (int id : ts) {
			for (edge e : G[id]) {
				if (e.cost < mito[e.to]) {
					mito[e.to] = e.cost;
					idto[e.to] = id;
				}
			}
			for (edge e : rG[id]) {
				if (e.cost-memo[id] < mifr[e.to]) {
					mifr[e.to] = e.cost-memo[id];
					idfr[e.to] = id;
				}
			}
		}

		vector<bool> nexi = exi;
		for (int id : ts) {
			nexi[id] = false;
			G[id].clear();
		}
		rep(i, n)if (nexi[i]) {
			vector<edge> nw;
			for (edge e : G[i])if (e.to != cr)nw.push_back(e);
			swap(nw, G[i]);
		}
		nexi[cr] = true;
		rep(i, n)if (i!=cr&&nexi[i]) {
			if (idfr[i] >= 0) {
				G[i].push_back({ cr,mifr[i] });
			}
			if (idto[i] >= 0) {
				G[cr].push_back({ i,mito[i] });
			}
		}
		vector<P> np = chu_liu_edmonds(G, nexi, root);
		//cout << "wow" <<cr<<"\n";
		//cout << memo[0] << "\n";
		//cout << mifr[3] << " " << idfr[3] << "\n";
		/*for (edge e : G[3]) {
			cout << e.to << " " << e.cost << "\n";
		}
		rep(i, np.size())cout << np[i].first << " " << np[i].second << "\n";
		*/
		int chk = -1;
		rep(i, np.size()) {
			if (np[i].second == cr) {
				chk = idfr[np[i].first];
				np[i].second = chk;
			}
			else if (np[i].first == cr) {
				np[i].first = idto[np[i].second];
			}
		}
		ts.push_back(ts[0]);
		rep(i, ts.size() - 1) {
			if (ts[i] != chk) {
				np.push_back({ ts[i + 1], ts[i] });
			}
		}
		return np;
	}
}

ld calc(vector<ld> dp, vector<ld> p) {
	ld le = -mod, ri = mod;
	auto cal = [&](ld t)->ld {
		ld sum = 0;
		rep(i, dp.size()) {
			ld x = p[i] + t * dp[i];
			sum += x * x;
		}
		return sum;
	};
	rep(i, 200) {
		ld ml = (le*2  + ri) / 3.0;
		ld mr = (le+ ri * 2) / 3.0;
		ld cl = cal(ml);
		ld cr = cal(mr);
		if (cl < cr) {
			ri = mr;
		}
		else le = ml;
	}
	ld sum = 0;
	rep(i, p.size())sum += p[i] * p[i];
	ld mem = cal(le);
	sum -= mem;
	return sum;
}

void solve() {
	int n, m; cin >> m >> n;
	vector<vector<ld>> vs(n, vector<ld>(m));
	vector<vector<edge>> G(n);
	rep(i, n)rep(j, m) {
		cin >> vs[i][j];
	}
	rep(i, n)Rep(j, i + 1, n) {
		//cout << i << " " << j << " "<<calc(vs[i], vs[j]) << " " << calc(vs[j], vs[i]) << "\n";
		G[i].push_back({ j,-calc(vs[i],vs[j]) });
		G[j].push_back({ i,-calc(vs[j],vs[i]) });
	}
	vector<bool> exi(n, true);
	ld ma = INF;
		vector<vector<edge>> nG = G;
		//cout << "hello\n";
		vector<P> res = chu_liu_edmonds(nG, exi);
		map<P, bool> mp;
		for (P p : res) {
			//cout << p.first << " " << p.second << "\n";
			mp[p] = true;
		}
		ld sum = 0;
		rep(i, n)for (edge e : G[i]) {
			if (mp[{i,e.to}])sum += e.cost;
		}ma = min(ma, sum);
	
	rep(i, n) {
		ld c = 0;
		rep(j, vs[i].size()) {
			c += vs[i][j] * vs[i][j];
		}
		ma +=c;
	}
	cout << ma << "\n";
}


signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(8);
	//init_f(); 
	//init();
	//expr();
	//int t; cin >> t; rep(i, t)
	solve();
	return 0;
}
accuracy: 1
time_ms: 450
memory_kb: 27940
final_rank: 10

code_snippet: #include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acosl(-1.0);

ll mod_pow(ll x, ll n, ll m = mod) {
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n % mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }

const int max_n = 1000;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}

int gcd(int a, int b) {
	if (a < b)swap(a, b);
	while (b) {
		int r = a % b; a = b; b = r;
	}
	return a;
}
struct uf {
private:
	vector<int> par, ran;
public:
	uf(int n) {
		par.resize(n, 0);
		ran.resize(n, 0);
		rep(i, n) {
			par[i] = i;
		}
	}
	int find(int x) {
		if (par[x] == x)return x;
		else return par[x] = find(par[x]);
	}
	void unite(int x, int y) {
		x = find(x), y = find(y);
		if (x == y)return;
		if (ran[x] < ran[y]) {
			par[x] = y;
		}
		else {
			par[y] = x;
			if (ran[x] == ran[y])ran[x]++;
		}
	}
	bool same(int x, int y) {
		return find(x) == find(y);
	}
};

using tval = ld;
struct edge {
	int to; tval cost;
};

vector<P> chu_liu_edmonds(vector<vector<edge>>&G, vector<bool> exi,int root=-1) {
	//cout << "wow\n";
	int n = G.size();
	if (root < 0) {
		root = n;
		G.push_back({});
		rep(i, n)G[root].push_back({ i,INF/10000 });
		n++;
		exi.push_back(true);
	}
	vector<vector<edge>> rG(n);
	rep(i, n)if(exi[i])for (edge e : G[i]) {
		rG[e.to].push_back({ i,e.cost });
	}
	vector<int> par(n);
	vector<tval> memo(n);
	rep(i, n)if (exi[i] && i != root) {
		tval mi = INF;
		for (edge e: rG[i]) {
			if (e.cost < mi) {
				mi = e.cost;
				par[i] = e.to;
			}
		}
		//cout << par[i] << " " << i << "\n";
		memo[i] = mi;
	}
	vector<bool> used(n);
	vector<bool> cused(n);
	vector<int> ts;
	used[root] = true;
	rep(i, n)if (exi[i] && i != root) {
		if (used[i])continue;
		vector<int> ids;
		int cur = i;
		cused[i] = true;
		ids.push_back(i);
		bool f = false;
		while (true) {
			cur = par[cur];
			if (used[cur]) {
				for (int id : ids) {
					cused[id] = false;
					used[id] = true;
				}
				break;
			}
			else if (cused[cur]) {
				f = true;
				rep(j, ids.size())if (ids[j] == cur) {
					ids.erase(ids.begin(), ids.begin() + j);
					break;
				}
				break;
			}
			else {
				cused[cur] = true;
				ids.push_back(cur);
			}
		}
		if (f) {
			ts = ids; break;
		}
	}
	if (ts.empty()) {
		vector<P> res; rep(i, n)if (exi[i] && i != root)res.push_back({ par[i],i });
		return res;
	}
	else {
		int cr = ts[0];
		vector<tval> mifr(n, INF), mito(n, INF);
		vector<int> idfr(n, -1), idto(n, -1);
		
		for (int id : ts) {
			for (edge e : G[id]) {
				if (e.cost < mito[e.to]) {
					mito[e.to] = e.cost;
					idto[e.to] = id;
				}
			}
			for (edge e : rG[id]) {
				if (e.cost-memo[id] < mifr[e.to]) {
					mifr[e.to] = e.cost-memo[id];
					idfr[e.to] = id;
				}
			}
		}

		vector<bool> nexi = exi;
		for (int id : ts) {
			nexi[id] = false;
			G[id].clear();
		}
		nexi[cr] = true;
		rep(i, n)if (i!=cr&&nexi[i]) {
			if (idfr[i] >= 0) {
				G[i].push_back({ cr,mifr[i] });
			}
			if (idto[i] >= 0) {
				G[cr].push_back({ i,mito[i] });
			}
		}
		vector<P> np = chu_liu_edmonds(G, nexi, root);
		int chk = -1;
		rep(i, np.size()) {
			if (np[i].second == cr) {
				chk = idfr[np[i].first];
				np[i].second = chk;
			}
			else if (np[i].first == cr) {
				np[i].first = idto[np[i].second];
			}
		}
		ts.push_back(ts[0]);
		rep(i, ts.size() - 1) {
			if (ts[i] != chk) {
				np.push_back({ ts[i + 1], ts[i] });
			}
		}
		return np;
	}
}

ld calc(vector<ld> dp, vector<ld> p) {
	ld le = -3, ri = 3;
	auto cal = [&](ld t) {
		ld sum = 0;
		rep(i, dp.size()) {
			ld x = p[i] + t * dp[i];
			sum += x * x;
		}
		return sqrtl(sum);
	};
	rep(i, 60) {
		ld ml = (le * 5 + ri * 4) / 9.0;
		ld mr = (le * 4 + ri * 5) / 9.0;
		ld cl = cal(ml);
		ld cr = cal(mr);
		if (cl < cr) {
			ri = mr;
		}
		else le = ml;
	}
	ld sum = 0;
	rep(i, p.size())sum += p[i] * p[i];
	ld mem = cal(le);
	sum -= mem * mem;
	return sum;
}

void solve() {
	int n, m; cin >> m >> n;
	vector<vector<ld>> vs(n, vector<ld>(m));
	vector<vector<edge>> G(n);
	rep(i, n)rep(j, m) {
		cin >> vs[i][j];
	}
	rep(i, n)Rep(j, i + 1, n) {
		//cout << i << " " << j << " "<<calc(vs[i], vs[j]) << " " << calc(vs[j], vs[i]) << "\n";
		G[i].push_back({ j,-calc(vs[i],vs[j]) });
		G[j].push_back({ i,-calc(vs[j],vs[i]) });
	}
	vector<bool> exi(n, true);
	ld ma = 0;
	rep(i, n) {
		vector<vector<edge>> nG = G;
		//cout << "hello\n";
		vector<P> res = chu_liu_edmonds(nG, exi,i);
		map<P, bool> mp;
		for (P p : res) {
			//cout << p.first << " " << p.second << "\n";
			mp[p] = true;
		}
		ld sum = 0;
		rep(i, n)for (edge e : G[i]) {
			if (mp[{i, e.to}])sum += e.cost;
		}ma = min(ma, sum);
	}
	
	rep(i, n) {
		ld c = 0;
		rep(j, vs[i].size()) {
			c += vs[i][j] * vs[i][j];
		}
		ma +=c;
	}
	cout << ma << "\n";
}


signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(8);
	//init_f(); 
	//init();
	//expr();
	//int t; cin >> t; rep(i, t)
	solve();
	return 0;
}
accuracy: 0.061224489795918366
time_ms: 520
memory_kb: 15224
final_rank: 20

code_snippet: #include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acosl(-1.0);

ll mod_pow(ll x, ll n, ll m = mod) {
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n % mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }

const int max_n = 1000;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}

int gcd(int a, int b) {
	if (a < b)swap(a, b);
	while (b) {
		int r = a % b; a = b; b = r;
	}
	return a;
}
struct uf {
private:
	vector<int> par, ran;
public:
	uf(int n) {
		par.resize(n, 0);
		ran.resize(n, 0);
		rep(i, n) {
			par[i] = i;
		}
	}
	int find(int x) {
		if (par[x] == x)return x;
		else return par[x] = find(par[x]);
	}
	void unite(int x, int y) {
		x = find(x), y = find(y);
		if (x == y)return;
		if (ran[x] < ran[y]) {
			par[x] = y;
		}
		else {
			par[y] = x;
			if (ran[x] == ran[y])ran[x]++;
		}
	}
	bool same(int x, int y) {
		return find(x) == find(y);
	}
};
ld calc(vector<ld> dp,vector<ld> p) {
	ld le = -3, ri = 3;
	auto cal = [&](ld t) {
		ld sum = 0;
		rep(i, dp.size()) {
			ld x = p[i] + t * dp[i];
			sum += x * x;
		}
		return sqrtl(sum);
	};
	rep(i, 60) {
		ld ml = (le * 5 + ri * 4) / 9.0;
		ld mr = (le * 4 + ri * 5) / 9.0;
		ld cl = cal(ml);
		ld cr = cal(mr);
		if (cl < cr) {
			ri = mr;
		}
		else le = ml;
	}
	ld sum = 0;
	rep(i, p.size())sum += p[i] * p[i];
	ld mem = cal(le);
	sum -= mem * mem;
	return sum;
}
void solve() {
	int n, m; cin >> m >> n;
	vector<vector<ld>> vs(n,vector<ld>(m));
	rep(i, n)rep(j, m) {
		cin >> vs[i][j];
	}
	vector < pair<ld,P>> v;
	rep(i, n)Rep(j, i + 1, n) {
		v.push_back({ calc(vs[i],vs[j]),{i,j} });
		v.push_back({ calc(vs[j],vs[i]),{j,i} });
	}
	ld ans = 0;
	rep(i, n) {
		ld sum = 0; rep(j, m)sum += vs[i][j] * vs[i][j];
		ans += sum;
	}
	sort(all(v), greater<pair<ld, P>>());
	uf u(n);
	vector<bool> used(n);
	int tmp = 0;
	rep(i, v.size()) {
		int l = v[i].second.first, r = v[i].second.second;
		if (!u.same(l,r)&&!used[r]) {
			u.unite(l, r); used[r] = true;
			ans -= v[i].first;

			//cout << v[i].first << " " << l << " " << r << "\n";
		}
	}
	cout << ans << "\n";
}


signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(8);
	//init_f(); 
	//init();
	//expr();
	//int t; cin >> t; rep(i, t)
	solve();
	return 0;
}
accuracy: 0.061224489795918366
time_ms: 30
memory_kb: 3480
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

// (重み, u->v)
using Edge = pair<double,pair<int,int>>;

const double INF = 19191919;

// 辺集合, 頂点数, 根
double Chu_Liu_Edmonds(const vector<Edge> es, int V, int root){
    // 頂点iに入ってくる辺(u->i)の中で、最小のコストの辺を管理 (コスト, u)
    vector<pair<double,int>> mn(V, {INF,-1});
    for(Edge e:es){
        int u = e.se.fi, v = e.se.se;
        mn[v] = min(mn[v], {e.fi, u});
    }
    mn[root] = {0,-1};

    // 連結成分に分ける(1つの閉路に対して1つの番号を与える)
    vector<int> cmp(V);
    // この連結成分が閉路であるか
    vector<bool> cycle(V,false);
    int cc = 0;

    vector<bool> vis(V);
    rep(i,V){
        if(vis[i]) continue;

        vector<int> path;
        int now = i;

        while(now!=-1 && !vis[now]){
            vis[now] = true;
            path.pb(now);

            // 今張っている辺を逆方向にたどる
            now = mn[now].se;
        }

        if(now != -1){
            bool in_cycle = false;
            for(int j:path){
                cmp[j] = cc;
                if(j == now){
                    // 閉路の始点(これ以降のpathに入ってる頂点が閉路をなす)
                    in_cycle = true;
                    cycle[cc] = true;
                }

                if(!in_cycle) ++cc;
            }
            if(in_cycle) ++cc;
        }
        else{
            // 閉路なし
            for(int j:path) cmp[j] = cc++;
        }
    }

    if(cc == V){
        // すべての頂点が違う連結成分に分かれたので 閉路なし
        double ans = 0;
        rep(i,V) ans += mn[i].fi;
        return ans;
    }

    // 閉路分のコスト
    double cycle_cost = 0;
    rep(i,V){
        if(i!=root && cycle[cmp[i]]) cycle_cost += mn[i].fi;
    }

    // コストを再設定
    vector<Edge> nes;
    for(Edge e:es){
        int u = e.se.fi, v = e.se.se;
        int cu = cmp[u], cv = cmp[v];

        if(cu == cv){
            // 閉路内の辺は無視
            continue;
        }
        else if(cycle[cv]){
            // コストを再設定
            nes.pb({e.fi - mn[v].fi, {cu,cv}});
        }
        else{
            nes.pb({e.fi, {cu,cv}});
        }
    }

    return cycle_cost + Chu_Liu_Edmonds(nes, cc, cmp[root]);
};

double size(const vector<double> &v){
    int n = v.size();
    double ret = 0;
    rep(i,n) ret += v[i]*v[i];
    return ret;
}

vector<double> getv(const vector<double> &v, const vector<double> &u, double r){
    int n = v.size();
    vector<double> ret(n);
    rep(i,n) ret[i] = v[i]+u[i]*r;
    return ret;
}

const double LIM = 100;
double calc(const vector<double> &v, const vector<double> &u){
    int n = v.size();

    double l=-LIM, r=LIM;
    rep(loop,60){
        double m1 = (2*l+r)/3, m2 = (l+2*r)/3;
        if(size(getv(v,u,m1)) < size(getv(v,u,m2))) r = m2;
        else l = m1;
    }

    return size(getv(v,u,r));
}

int main(){
    int n,m;
    cin >>n >>m;

    vector<vector<double>> v(m);
    rep(i,m){
        vector<double> tmp(n);
        rep(j,n) cin >>tmp[j];
        v[i] = tmp;
    }

    vector<Edge> Es;
    rep(i,m)rep(j,m)if(i!=j){
        Es.pb({calc(v[j],v[i]),{i,j}});
        // printf(" %d %d  -> %f\n",i,j,calc(v[j],v[i]));
    }

    double ans = INF;
    rep(i,m){
        double res = Chu_Liu_Edmonds(Es,m,i);
        ans = min(ans, size(v[i])+res);
        // printf("  %d::  %f\n",i,res);
    }
    printf("%.10f\n", ans);
    return 0;
}
accuracy: 1
time_ms: 960
memory_kb: 23768
final_rank: 11

code_snippet: #include <bits/stdc++.h>
#define re(i,n) for(int i=0;i<n;i++)
using namespace std;

struct UnionFind{
  int n;
  vector<int> r,p;
  UnionFind(){}
  UnionFind(int sz):n(sz),r(sz,1),p(sz,0){iota(p.begin(),p.end(),0);}
  int find(int x){
    return (x==p[x]?x:p[x]=find(p[x]));
  }
  bool same(int x,int y){
    return find(x)==find(y);
  }
  void unite(int x,int y){
    x=find(x);y=find(y);
    if(x==y) return;
    r[x]+=r[y];
    p[y]=x;
  }
};

template<typename T, typename E>
struct SkewHeap{
  using G = function<T(T,E)>;
  using H = function<E(E,E)>;
  using C = function<bool(T,T)>;
  G g;
  H h;
  C c;
  T INF;
  E ei;
  SkewHeap(G g,H h,C c,T INF,E ei):g(g),h(h),c(c),INF(INF),ei(ei){}
  
  struct Node{
    Node *l,*r;
    T val;
    E add;
    Node(T val,E add):val(val),add(add){l=r=nullptr;}
  };

  void eval(Node *a){
    if(a==nullptr) return;
    if(a->add==ei) return;
    if(a->l) a->l->add=h(a->l->add,a->add);
    if(a->r) a->r->add=h(a->r->add,a->add);
    a->val=g(a->val,a->add);
    a->add=ei;
  }
  
  T top(Node *a){
    return a!=nullptr?g(a->val,a->add):INF;
  }

  T snd(Node *a){
    eval(a);
    return a!=nullptr?min(top(a->l),top(a->r)):INF;
  }

  Node* add(Node *a,E d){
    if(a!=nullptr) a->add=h(a->add,d);
    return a;
  }
  
  Node* push(T v){
    return new Node(v,ei);
  }
  
  Node* meld(Node *a,Node *b){
    using V = tuple<Node*, Node*>;
    stack<V> st;
    Node* res;
  ENTRYPOINT:
    if(!a||!b) res=a?a:b;
    else{
      if(c(top(a),top(b))) swap(a,b);
      eval(a);
      st.emplace(a,b);
      a=a->r;
      goto ENTRYPOINT;
    RETURNPOINT:
      tie(a,b)=st.top();st.pop();
      a->r=res;
      swap(a->l,a->r);
      res=a;
    }
    if(!st.empty()) goto RETURNPOINT;
    return res;
  }
  
  Node* pop(Node* a){
    eval(a);
    auto res=meld(a->l,a->r);
    delete a;
    return res;
  }
  
};

//INSERT ABOVE HERE
template<typename T>
struct Arborescence{
  typedef pair<T, int> P;
  using Heap = SkewHeap<P, T>;
  
  struct edge{
    int from,to;
    T cost;
    edge(){}
    edge(int from,int to,T cost):from(from),to(to),cost(cost){}
  };
  
  int n;
  P INF;
  UnionFind uf;
  vector<edge> edges;
  vector<typename Heap::Node*> come;
  vector<int> used,from;
  vector<T> cost;
  
  Arborescence(int n,T INF):n(n),INF(INF,-1),uf(n),come(n,NULL),
                used(n,0),from(n,-1),cost(n,-1){};

  void add_edge(int from,int to,T cost){
    edges.emplace_back(from,to,cost);
  }

  void input(int m,int offset=0){
    for(int i=0;i<m;i++){
      int u,v;
      T c;
      cin>>u>>v>>c;
      add_edge(u+offset,v+offset,c);
    }
  }

  T build(int r){
    typename Heap::G g=[](P a,T b){return P(a.first+b,a.second);};
    typename Heap::H h=[](T a,T b){return a+b;};
    typename Heap::C c=[](P a,P b){return a>b;};
    Heap heap(g,h,c,INF,0);
  
    used[r]=2;
    for(int i=0;i<(int)edges.size();i++){
      edge &e=edges[i];
      come[e.to]=heap.meld(come[e.to],heap.push(P(e.cost,i)));
    }
    
    T res=0;
    for(int i=0;i<n;i++){
      if(used[i]) continue;
      int v=i;
      vector<int> l;
      while(used[v]!=2){
    used[v]=1;
    l.emplace_back(v);
    if(!come[v]) return T(-1);
    from[v]=uf.find(edges[come[v]->val.second].from);
    cost[v]=heap.top(come[v]).first;
    come[v]=heap.pop(come[v]);
    if(from[v]==v) continue;
    
    res+=cost[v];
    if(used[from[v]]==1){
      int p=v;
      do{
        if(come[p]!=nullptr) heap.add(come[p],-cost[p]);
        if(p!=v){
          uf.unite(v,p);
          come[v]=heap.meld(come[v],come[p]);
        }
        p=uf.find(from[p]);
      }while(p!=v);
    }else{
      v=from[v];
    }
      }
      for(int u:l) used[u]=2;
    }
    return res;
  }
};

int n,m;
double v[111][111];
double cost[111][111];
signed main(){
  cin>>n>>m;
  Arborescence<double> G(m+1,1e14);
  re(i,m)re(j,n)cin>>v[i][j];
  re(j,n)v[m][j]=0;
  re(i,m)re(j,m){
    if(i==j)continue;
    double L=-1000,R=1000;
    re(k,50){
      double mid=(L+R)/2;
      double s1=0,s2=0;
      re(l,n)s1+=abs(v[j][l]-mid*v[i][l])*abs(v[j][l]-mid*v[i][l]);
      re(l,n)s2+=abs(v[j][l]-(mid+1e-6)*v[i][l])*abs(v[j][l]-(mid+1e-6)*v[i][l]);
      if(s1>s2)L=mid;
      else R=mid;
    }
    double sum=0;
    re(l,n)sum+=abs(v[j][l]-L*v[i][l])*abs(v[j][l]-L*v[i][l]);
    G.add_edge(i,j,sum);
  }
  re(i,m){
    double sum=0;
    re(l,n)sum+=v[i][l]*v[i][l];
    G.add_edge(m,i,sum);
  }
  printf("%.9f\n", G.build(m));
}
accuracy: 1
time_ms: 80
memory_kb: 3844
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

#define double long double

double size(const vector<double> &v){
    int n = v.size();
    double ret = 0;
    rep(i,n) ret += v[i]*v[i];
    return ret;
}

vector<double> getv(const vector<double> &v, const vector<double> &u, double r){
    int n = v.size();
    vector<double> ret(n);
    rep(i,n) ret[i] = v[i]+u[i]*r;
    return ret;
}

const double LIM = 100;
double calc(const vector<double> &v, const vector<double> &u){
    int n = v.size();

    double l=-LIM, r=LIM;
    rep(loop,40){
        double m1 = (2*l+r)/3, m2 = (l+2*r)/3;
        if(size(getv(v,u,m1)) < size(getv(v,u,m2))) r = m2;
        else l = m1;
    }

    return size(getv(v,u,r));
}

int main(){
    int n,m;
    cin >>n >>m;

    vector<vector<double>> v(m);
    rep(i,m){
        vector<double> tmp(n);
        rep(j,n) cin >>tmp[j];
        v[i] = tmp;
    }

    double ans = 0;

    using P = pair<double,int>;
    priority_queue<P, vector<P>, greater<P>> pq;
    vector<bool> vis(m);
    rep(i,m) pq.push({size(v[i]),i});
    while(!pq.empty()){
        P now = pq.top();
        pq.pop();
        int x = now.se;
        if(vis[x]) continue;

        vis[x] = true;
        ans += now.fi;
        rep(i,m){
            pq.push({calc(v[i],v[x]),i});
        }
    }

    printf("%.10Lf\n", ans);
    return 0;
}
accuracy: 0.061224489795918366
time_ms: 110
memory_kb: 3384
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

#define double long double

double size(const vector<double> &v){
    int n = v.size();
    double ret = 0;
    rep(i,n) ret += v[i]*v[i];
    return ret;
}

vector<double> getv(const vector<double> &v, const vector<double> &u, double r){
    int n = v.size();
    vector<double> ret(n);
    rep(i,n) ret[i] = v[i]+u[i]*r;
    return ret;
}

const double LIM = 100;
double calc(const vector<double> &v, const vector<double> &u){
    int n = v.size();

    double l=-LIM, r=LIM;
    rep(loop,40){
        double m1 = (2*l+r)/3, m2 = (l+2*r)/3;
        if(size(getv(v,u,m1)) < size(getv(v,u,m2))) r = m2;
        else l = m1;
    }

    return size(getv(v,u,r));
}

bool comp(const vector<double> &v, const vector<double> &u){
    return size(v) + min(size(u),calc(u,v)) < size(u) + min(size(v),calc(v,u));
}

int main(){
    int n,m;
    cin >>n >>m;

    vector<vector<double>> v(m);
    rep(i,m){
        vector<double> tmp(n);
        rep(j,n) cin >>tmp[j];
        v[i] = tmp;
    }

    sort(all(v), comp);

    double ans = 0;
    rep(i,m){
        double add = size(v[i]);
        rep(j,i) add = min(add, calc(v[i],v[j]));
        ans += add;
    }
    printf("%.10Lf\n", ans);
    return 0;
}
accuracy: 0.061224489795918366
time_ms: 70
memory_kb: 3200
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

double size(const vector<double> &v){
    int n = v.size();
    double ret = 0;
    rep(i,n) ret += v[i]*v[i];
    return ret;
}

vector<double> getv(const vector<double> &v, const vector<double> &u, double r){
    int n = v.size();
    vector<double> ret(n);
    rep(i,n) ret[i] = v[i]+u[i]*r;
    return ret;
}

const double LIM = 3;
double calc(const vector<double> &v, const vector<double> &u){
    int n = v.size();

    double l=-LIM, r=LIM;
    rep(loop,40){
        double m1 = (2*l+r)/3, m2 = (l+2*r)/3;
        if(size(getv(v,u,m1)) < size(getv(v,u,m2))) r = m2;
        else l = m1;
    }

    return size(getv(v,u,r));
}

bool comp(const vector<double> &v, const vector<double> &u){
    return size(v) + calc(v,u) < size(u) + calc(u,v);
}

int main(){
    int n,m;
    cin >>n >>m;

    vector<vector<double>> v(m);
    rep(i,m){
        vector<double> tmp(n);
        rep(j,n) cin >>tmp[j];
        v[i] = tmp;
    }

    sort(all(v), comp);

    double ans = 0;
    rep(i,m){
        double add = size(v[i]);
        rep(j,i) add = min(add, calc(v[i],v[j]));
        ans += add;
    }
    printf("%.10f\n", ans);
    return 0;
}
accuracy: 0.061224489795918366
time_ms: 40
memory_kb: 3268
final_rank: 15

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

typedef double ldouble;
typedef vector<ldouble> Point;
ldouble INF=1e9;

struct UnionFind{
  vector<int> par,rank;
  void init(int n){
    par.clear();
    rank.clear();
    par.resize(n);
    rank.resize(n);
    for(int i=0;i<n;i++){
      par[i]=i;
      rank[i]=1;
    }
  }
  
  int find(int x){
    if(x==par[x])return x;
    return par[x]=find(par[x]);
  }

  bool same(int x,int y){
    return ( find(x)==find(y) );
  }
  
  void unite(int x,int y){
    x=find(x);
    y=find(y);
    if(x==y)return;
    if(rank[x]<rank[y])swap(x,y);
    par[y]=x;
    rank[x]+=rank[y];
  }
};

struct edge{
  int from;
  int to;
  ldouble cost;
  int id;
  edge(int from,int to,ldouble cost,int id) :from(from), to(to),cost(cost),id(id) {}
  bool operator < (const edge e)const{
    return cost > e.cost;
  }
};

typedef priority_queue< edge > prque;
typedef prque* Prque;

typedef vector<edge> Node;
typedef vector<Node> Graph;


Prque Merge(vector< Prque > &Q, vector<edge> &ev,int A,int C){
  if( Q[C]->size() < Q[A]->size() ){

    while( !Q[C]->empty() ){
      edge e=Q[C]->top();
      e.cost-=ev[C].cost;
      e.cost+=ev[A].cost;
      //      cout<<e.from<<' '<<e.to<<' '<<e.cost<<' '<<ev[A].cost<<' '<<ev[C].cost<<endl;
      Q[A]->push(e);
      Q[C]->pop();
    }
    ev[C].cost=ev[A].cost;
    return Q[A];
  }else{
    while( !Q[A]->empty() ){
      edge e=Q[A]->top();
      e.cost-=ev[A].cost;
      e.cost+=ev[C].cost;
      //      cout<<e.from<<' '<<e.to<<' '<<e.cost<<endl;
      Q[C]->push(e);
      Q[A]->pop();
    }
    return Q[C];
  }
}

ldouble solve(Graph &G,vector<edge> &edges,int root){

  int n=G.size();
  ldouble res=0;
  
  vector<int> used(n,0);
  vector< edge > ev(n, (edge){0,0,0,-1} );
  vector< prque > pool(n);
  vector< Prque > Q(n);
  for(int i=0;i<n;i++)Q[i]=&pool[i];
  
  UnionFind uf;
  uf.init(n);
  
  for(int i=0;i<(int)edges.size();i++){
    edge e=edges[i];
    Q[ e.to ]->push( e );
  }
  
  used[root]=2;
  for(int Pos=0;Pos<n;Pos++){
    if(used[Pos]==2)continue;
    int pos=Pos;
    vector<int> path;
    
    while( used[pos] != 2 ){
      pos=uf.find(pos);
      
      used[pos]=1;
      path.push_back(pos);
      if( Q[pos]->empty() ){
        return INF;
      }
      
      edge e=Q[pos]->top();

      
      Q[pos]->pop();
      e.cost-=ev[pos].cost;
      if( uf.same(e.from,pos) ) continue;
      ldouble tmpcost=ev[pos].cost;
      /*
      cout<<" pos="<<pos;
      cout<<" e.from="<<e.from;
      cout<<" e.to="<<e.to;
      cout<<" e.cost="<<e.cost;
      cout<<" tmpcost="<<tmpcost<<endl;
      cout<<endl;
      */
      res+=e.cost;
      e.cost+=tmpcost;
      ev[pos]=e;
      if( used[ uf.find(e.from) ] == 2 )break;
      if( used[ uf.find(e.from) ] == 0 ){
        pos=e.from;
        continue;
      }
      int pre=uf.find(e.from);
      for(int i=0;i<100;i++){
        if(!uf.same(pre,pos)){
          int A=uf.find(pre), B=uf.find(pos);
          uf.unite(A,B);
          int C=uf.find(A);
          /*
          cout<<" !!A="<<A;
          cout<<" !!B="<<B;
          cout<<" !!C="<<C<<endl;
          */
          Prque tmp=NULL;
          if(B==C)tmp=Merge(Q,ev,A,C);
          else if(A==C)tmp=Merge(Q,ev,B,C);
          else assert(0);
          
          Q[C]=tmp;
        }
        pre=uf.find(ev[pre].from);
      }
    }// while_pos

    for(int i=0;i<(int)path.size();i++)used[ path[i] ]=2;
  }// Pos
  return res;
}


Point add(Point a,Point b){
  Point res(a.size());
  for(int i=0;i<(int)a.size();i++)
    res[i]=a[i]+b[i];
  return res;
}

Point sub(Point a,Point b){
  Point res(a.size());
  for(int i=0;i<(int)a.size();i++)
    res[i]=a[i]-b[i];
  return res;
}

ldouble dot(Point a,Point b){
  ldouble res=0;
  for(int i=0;i<(int)a.size();i++)
    res+=a[i]*b[i];
  return res;
}

ldouble norm(Point p){
  return  dot(p,p);
}

ldouble abs(Point p){
  return  sqrt( norm(p) );
}

double Sqrt(double x){
  if(x<0)return 0;
  return sqrt(x);
}

double project(Point a,Point b){
  ldouble t=dot(a,b);
  
  //  cout<<a[0]<<' '<<a[1]<<endl;
  //  cout<<b[0]<<' '<<b[1]<<endl;
  //  cout<< dot(a,b) <<endl;
  return   abs( norm(b) - (t*t/norm(a)) );
}


int main(){
  int n,m;
  vector< Point > t;
  
  cin>>m>>n;
  for(int i=0;i<n;i++){
    Point a(m);
    for(int j=0;j<m;j++){
      cin>>a[j];
    }

    if(norm(a) < 0.00000001){
      continue;
    }
    t.push_back(a);
  }
  n=t.size();
  Graph G;
  G.resize(n+1);
  int cc=0;
  for(int i=0;i<n;i++){
    G[n].push_back( edge(n,i,norm(t[i]) , cc++) );
    for(int j=0;j<n;j++){
      if(i==j)continue;
      ldouble cost=project(t[i],t[j]);
      G[i].push_back( edge(i,j,cost,cc++) );
    }
  }

  vector<edge> edges;
  for(int i=0;i<=n;i++)
    for(int j=0;j<(int)G[i].size();j++)
      edges.push_back(G[i][j]);
  
  printf("%.10f\n",(double) solve(G,edges,n) );
  
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4420
final_rank: 5

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef vector<double> Point;

double INF=1e9;

struct UnionFind{
  vector<int> par,rank;
  void init(int n){
    par.clear();
    rank.clear();
    par.resize(n);
    rank.resize(n);
    for(int i=0;i<n;i++){
      par[i]=i;
      rank[i]=1;
    }
  }
  
  int find(int x){
    if(x==par[x])return x;
    return par[x]=find(par[x]);
  }

  bool same(int x,int y){
    return ( find(x)==find(y) );
  }
  
  void unite(int x,int y){
    x=find(x);
    y=find(y);
    if(x==y)return;
    if(rank[x]<rank[y])swap(x,y);
    par[y]=x;
    rank[x]+=rank[y];
  }
};

struct edge{
  int from;
  int to;
  double cost;
  int id;
  edge(int from,int to,double cost,int id) :from(from), to(to),cost(cost),id(id) {}
  bool operator < (const edge e)const{
    return cost > e.cost;
  }
};

typedef priority_queue< edge > prque;
typedef prque* Prque;

typedef vector<edge> Node;
typedef vector<Node> Graph;


Prque Merge(vector< Prque > &Q, vector<edge> &ev,int A,int C){
  if( Q[C]->size() < Q[A]->size() ){

    while( !Q[C]->empty() ){
      edge e=Q[C]->top();
      e.cost-=ev[C].cost;
      e.cost+=ev[A].cost;
      //      cout<<e.from<<' '<<e.to<<' '<<e.cost<<' '<<ev[A].cost<<' '<<ev[C].cost<<endl;
      Q[A]->push(e);
      Q[C]->pop();
    }
    ev[C].cost=ev[A].cost;
    return Q[A];
  }else{
    while( !Q[A]->empty() ){
      edge e=Q[A]->top();
      e.cost-=ev[A].cost;
      e.cost+=ev[C].cost;
      //      cout<<e.from<<' '<<e.to<<' '<<e.cost<<endl;
      Q[C]->push(e);
      Q[A]->pop();
    }
    return Q[C];
  }
}

double solve(Graph &G,vector<edge> &edges,int root){

  int n=G.size();
  double res=0;
  
  vector<int> used(n,0);
  vector< edge > ev(n, (edge){0,0,0,-1} );
  vector< prque > pool(n);
  vector< Prque > Q(n);
  for(int i=0;i<n;i++)Q[i]=&pool[i];
  
  UnionFind uf;
  uf.init(n);
  
  for(int i=0;i<(int)edges.size();i++){
    edge e=edges[i];
    Q[ e.to ]->push( e );
  }
  
  used[root]=2;
  for(int Pos=0;Pos<n;Pos++){
    if(used[Pos]==2)continue;
    int pos=Pos;
    vector<int> path;
    
    while( used[pos] != 2 ){
      pos=uf.find(pos);
      
      used[pos]=1;
      path.push_back(pos);
      if( Q[pos]->empty() ){
        return INF;
      }
      
      edge e=Q[pos]->top();

      
      Q[pos]->pop();
      e.cost-=ev[pos].cost;
      if( uf.same(e.from,pos) ) continue;
      double tmpcost=ev[pos].cost;
      /*
      cout<<" pos="<<pos;
      cout<<" e.from="<<e.from;
      cout<<" e.to="<<e.to;
      cout<<" e.cost="<<e.cost;
      cout<<" tmpcost="<<tmpcost<<endl;
      cout<<endl;
      */
      res+=e.cost;
      e.cost+=tmpcost;
      ev[pos]=e;
      if( used[ uf.find(e.from) ] == 2 )break;
      if( used[ uf.find(e.from) ] == 0 ){
        pos=e.from;
        continue;
      }
      int pre=uf.find(e.from);
      for(int i=0;i<100;i++){
        if(!uf.same(pre,pos)){
          int A=uf.find(pre), B=uf.find(pos);
          uf.unite(A,B);
          int C=uf.find(A);
          /*
          cout<<" !!A="<<A;
          cout<<" !!B="<<B;
          cout<<" !!C="<<C<<endl;
          */
          Prque tmp=NULL;
          if(B==C)tmp=Merge(Q,ev,A,C);
          else if(A==C)tmp=Merge(Q,ev,B,C);
          else assert(0);
          
          Q[C]=tmp;
        }
        pre=uf.find(ev[pre].from);
      }
    }// while_pos

    for(int i=0;i<(int)path.size();i++)used[ path[i] ]=2;
  }// Pos
  return res;
}


Point add(Point a,Point b){
  Point res(a.size());
  for(int i=0;i<(int)a.size();i++)
    res[i]=a[i]+b[i];
  return res;
}

Point sub(Point a,Point b){
  Point res(a.size());
  for(int i=0;i<(int)a.size();i++)
    res[i]=a[i]-b[i];
  return res;
}

double dot(Point a,Point b){
  double res=0;
  for(int i=0;i<(int)a.size();i++)
    res+=a[i]*b[i];
  return res;
}

double norm(Point p){
  return  dot(p,p);
}

Point project(Point a,Point b,Point p){
  Point base( a.size() );
  Point target( a.size() );
  for(int i=0;i<(int)a.size();i++){
    base[i]=b[i]-a[i];
    target[i]=p[i]-a[i];
  }
  double t=dot(base,target);
  Point res=a;
  for(int i=0;i<(int)a.size();i++)
    res[i]=res[i]+base[i]*t/norm(base);
  return res;
}


int main(){
  int n,m;
  vector< Point > t;
  
  cin>>m>>n;
  for(int i=0;i<n;i++){
    Point a(m);
    for(int j=0;j<m;j++){
      cin>>a[j];
    }
    t.push_back(a);
  }

  Point cen( m , 0 );
  
  Graph G;
  G.resize(n+1);
  int cc=0;
  for(int i=0;i<n;i++){
    G[n].push_back( edge(n,i,norm(t[i]) , cc++) );
    
    for(int j=0;j<n;j++){
      if(i==j)continue;
      Point pp = project( t[j] , add(t[j],t[i]) , cen);
      double cost= norm( pp);
      G[i].push_back( edge(i,j,cost,cc++) );
    }
  }

  vector<edge> edges;
  for(int i=0;i<=n;i++)
    for(int j=0;j<(int)G[i].size();j++)
      edges.push_back(G[i][j]);
  
  printf("%.10f\n", solve(G,edges,n) );
  
  return 0;
}
accuracy: 0.9183673469387755
time_ms: 130
memory_kb: 4564
final_rank: 12

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef vector<double> Point;

double INF=1e9;

struct UnionFind{
  vector<int> par,rank;
  void init(int n){
    par.clear();
    rank.clear();
    par.resize(n);
    rank.resize(n);
    for(int i=0;i<n;i++){
      par[i]=i;
      rank[i]=1;
    }
  }
  
  int find(int x){
    if(x==par[x])return x;
    return par[x]=find(par[x]);
  }

  bool same(int x,int y){
    return ( find(x)==find(y) );
  }
  
  void unite(int x,int y){
    x=find(x);
    y=find(y);
    if(x==y)return;
    if(rank[x]<rank[y])swap(x,y);
    par[y]=x;
    rank[x]+=rank[y];
  }
};

struct edge{
  int from;
  int to;
  double cost;
  int id;
  edge(int from,int to,double cost,int id) :from(from), to(to),cost(cost),id(id) {}
  bool operator < (const edge e)const{
    return cost > e.cost;
  }
};

typedef priority_queue< edge > prque;
typedef prque* Prque;

typedef vector<edge> Node;
typedef vector<Node> Graph;


Prque Merge(vector< Prque > &Q, vector<edge> &ev,int A,int C){
  if( Q[C]->size() < Q[A]->size() ){

    while( !Q[C]->empty() ){
      edge e=Q[C]->top();
      e.cost-=ev[C].cost;
      e.cost+=ev[A].cost;
      //      cout<<e.from<<' '<<e.to<<' '<<e.cost<<' '<<ev[A].cost<<' '<<ev[C].cost<<endl;
      Q[A]->push(e);
      Q[C]->pop();
    }
    ev[C].cost=ev[A].cost;
    return Q[A];
  }else{
    while( !Q[A]->empty() ){
      edge e=Q[A]->top();
      e.cost-=ev[A].cost;
      e.cost+=ev[C].cost;
      //      cout<<e.from<<' '<<e.to<<' '<<e.cost<<endl;
      Q[C]->push(e);
      Q[A]->pop();
    }
    return Q[C];
  }
}

double solve(Graph &G,vector<edge> &edges,int root){

  int n=G.size();
  double res=0;
  
  vector<int> used(n,0);
  vector< edge > ev(n, (edge){0,0,0,-1} );
  vector< prque > pool(n);
  vector< Prque > Q(n);
  for(int i=0;i<n;i++)Q[i]=&pool[i];
  
  UnionFind uf;
  uf.init(n);
  
  for(int i=0;i<(int)edges.size();i++){
    edge e=edges[i];
    Q[ e.to ]->push( e );
  }
  
  used[root]=2;
  for(int Pos=0;Pos<n;Pos++){
    if(used[Pos]==2)continue;
    int pos=Pos;
    vector<int> path;
    
    while( used[pos] != 2 ){
      pos=uf.find(pos);
      
      used[pos]=1;
      path.push_back(pos);
      if( Q[pos]->empty() ){
        return INF;
      }
      
      edge e=Q[pos]->top();

      
      Q[pos]->pop();
      e.cost-=ev[pos].cost;
      if( uf.same(e.from,pos) ) continue;
      int tmpcost=ev[pos].cost;
      /*
      cout<<" pos="<<pos;
      cout<<" e.from="<<e.from;
      cout<<" e.to="<<e.to;
      cout<<" e.cost="<<e.cost;
      cout<<" tmpcost="<<tmpcost<<endl;
      cout<<endl;
      */
      res+=e.cost;
      e.cost+=tmpcost;
      ev[pos]=e;
      if( used[ uf.find(e.from) ] == 2 )break;
      if( used[ uf.find(e.from) ] == 0 ){
        pos=e.from;
        continue;
      }
      int pre=uf.find(e.from);
      for(int i=0;i<100;i++){
        if(!uf.same(pre,pos)){
          int A=uf.find(pre), B=uf.find(pos);
          uf.unite(A,B);
          int C=uf.find(A);
          /*
          cout<<" !!A="<<A;
          cout<<" !!B="<<B;
          cout<<" !!C="<<C<<endl;
          */
          Prque tmp=NULL;
          if(B==C)tmp=Merge(Q,ev,A,C);
          else if(A==C)tmp=Merge(Q,ev,B,C);
          else assert(0);
          
          Q[C]=tmp;
        }
        pre=uf.find(ev[pre].from);
      }
    }// while_pos

    for(int i=0;i<(int)path.size();i++)used[ path[i] ]=2;
  }// Pos
  return res;
}


Point add(Point a,Point b){
  Point res(a.size());
  for(int i=0;i<(int)a.size();i++)
    res[i]=a[i]+b[i];
  return res;
}

Point sub(Point a,Point b){
  Point res(a.size());
  for(int i=0;i<(int)a.size();i++)
    res[i]=a[i]-b[i];
  return res;
}

double dot(Point a,Point b){
  double res=0;
  for(int i=0;i<(int)a.size();i++)
    res+=a[i]*b[i];
  return res;
}

double norm(Point p){
  return  dot(p,p);
}

Point project(Point a,Point b,Point p){
  Point base( a.size() );
  Point target( a.size() );
  for(int i=0;i<(int)a.size();i++){
    base[i]=b[i]-a[i];
    target[i]=p[i]-a[i];
  }
  double t=dot(base,target);
  Point res=a;
  for(int i=0;i<(int)a.size();i++)
    res[i]=res[i]+base[i]*t/norm(base);
  return res;
}


int main(){
  int n,m;
  vector< Point > t;
  
  cin>>m>>n;
  for(int i=0;i<n;i++){
    Point a(m);
    for(int j=0;j<m;j++){
      cin>>a[j];
    }
    t.push_back(a);
  }

  Point cen( m , 0 );
  
  Graph G;
  G.resize(n+1);
  int cc=0;
  for(int i=0;i<n;i++){
    G[n].push_back( edge(n,i, (norm(t[i])) , cc++) );
    
    
    for(int j=0;j<n;j++){
      if(i==j)continue;
      
      Point pp = project( t[j] , add(t[j],t[i]) , cen);
      double cost= norm( pp);
      G[i].push_back( edge(i,j,cost,cc++) );

      //      cout<<i<<' '<<j<<' '<< cost << ' ' <<pp[0]<<' '<<pp[1]<<endl;
    }
  }

  vector<edge> edges;
  for(int i=0;i<=n;i++)
    for(int j=0;j<(int)G[i].size();j++)
      edges.push_back(G[i][j]);
  
  printf("%.10f\n", solve(G,edges,n) );
  
  return 0;
}
accuracy: 0.061224489795918366
time_ms: 30
memory_kb: 4144
final_rank: 17

code_snippet: #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
using namespace std;


struct edge
{
 int begin;int end;	
};

const int maxx=120;
bool vis[maxx],indfs[maxx];
vector<double>zero;
int n,m,root;
vector<vector<double> >vec;
double cost[maxx][maxx],Cost[maxx][maxx];
bool operator <(const edge a,const edge b)
{
 double ac=cost[a.begin][a.end];double bc=cost[b.begin][b.end];
 return ac>bc;//´?????´?????	
};
priority_queue<edge>que[maxx];
double minv(vector<double> a,vector<double> b)
{
 double ab=0,bsqrt=0,asqrt=0;
 for(int i=0;i<a.size();i++)
 	{ab+=a[i]*b[i];bsqrt+=b[i]*b[i];asqrt+=a[i]*a[i];}
 	
 if((bsqrt-0.0)<1e-10)return asqrt;
 return asqrt-(ab*ab/bsqrt);
}
int belongcircle[maxx];
int findbelong(int x)
{
 if(x==belongcircle[x])return x;
 	else return belongcircle[x]=findbelong(belongcircle[x]);
}

double ans,ansnow;
double min(double a,double b)
{
 if(a<b)return a;
 	else return b;
}
void combinecircle(int now,int begin)
{
 now=findbelong(now);
 if(vis[now])return ;
 vis[now]=true;
 edge e1=que[now].top();
 double coste=cost[findbelong(e1.begin)][findbelong(e1.end)];
 ansnow+=coste;
 bool finish[maxx];
 memset(finish,0,sizeof(finish));
 for(int i=0;i<=m;i++)
 	{
 	 int po=findbelong(i);
 	 if(po==now||po==begin)continue;
 	 if(finish[po])continue;
 	 finish[po]=true;
 	 cost[begin][po]=min(cost[begin][po],cost[now][po]-coste);
 	 cost[po][begin]=min(cost[po][begin],cost[po][now]);
	}
 belongcircle[now]=begin;
 combinecircle(e1.end,begin);
 return ;
}
void build();
bool findcircle(int now)
{
 if(now==root)return false; 
 now=findbelong(now);
 if(indfs[now])
 	{
 	 memset(vis,0,sizeof(vis));
 	 combinecircle(now,now);
 	 build();
 	 return true;
	}
 indfs[now]=true;
 return findcircle(que[now].top().end);
}

void build()
{
 bool finish[maxx];
 memset(finish,0,sizeof(finish));
 for(int i=0;i<m;i++)
 	{
 	 int po=findbelong(i);
 	 if(finish[po])continue;
 	 finish[po]=true;
 	 while(!que[po].empty())que[po].pop();
 	 for(int j=0;j<=m;j++)
 	 	{
 	 	 int pb=findbelong(j);
 	 	 if(po==pb)continue;
 	 	 edge en;en.begin=po;en.end=pb;
 	 	 que[po].push(en);
		}
	}
 return ;
}
bool counted[maxx];
int main()
{
 ios::sync_with_stdio(false);
 cin>>n>>m;
 for(int i=0;i<n;i++)
    zero.push_back(0.0);
 for(int i=0;i<m;i++)
 	{
	 vector<double>nowv;
	 for(int j=0;j<n;j++)
	 	{ double nv;cin>>nv;nowv.push_back(nv);}
	 vec.push_back(nowv);
	}
 vec.push_back(zero);
 for(int i=0;i<=m;i++)
 	for(int j=0;j<=m;j++)
 		{ cost[i][j]=minv(vec[i],vec[j]);}
 root=m;
 ansnow=0;
 belongcircle[root]=root;
 	 for(int i=0;i<m;i++)
 	 	{
 	 	 belongcircle[i]=i;
 	 	}
	 bool exis=false;
	 	 do
	 	 	{
	 	 	 build();
	 	 	 exis=false;
	 	 	 bool finish[maxx];
	 	 	 memset(finish,0,sizeof(finish));
	 	 	 for(int i=0;i<m;i++)
	 	 	 {  
			    int po=findbelong(i);
			    if(finish[po])continue;
	 	 	    memset(indfs,0,sizeof(indfs));
	 	 	    exis=findcircle(po);
	 	 	    finish[po]=true;
			 }
			}while(exis);
	 memset(counted,0,sizeof(counted));
	 for(int i=0;i<m;i++)
	 	{
		 int po=findbelong(i);
		 if(counted[po])continue;
		 counted[po]=true;
		 ansnow+=cost[findbelong(que[po].top().begin)][findbelong(que[po].top().end)];	
		}
 printf("%.8f\n",ansnow);
 return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3564
final_rank: 3

code_snippet: #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
using namespace std;


struct edge
{
 int begin;int end;	
};

const int maxx=120;
bool vis[maxx],indfs[maxx];
vector<double>zero;
int n,m,root;
vector<vector<double> >vec;
double cost[maxx][maxx],Cost[maxx][maxx];
bool operator <(const edge a,const edge b)
{
 double ac=cost[a.begin][a.end];double bc=cost[b.begin][b.end];
 return ac>bc;//´?????´?????	
};
priority_queue<edge>que[maxx];
double minv(vector<double> a,vector<double> b)
{
 double ab=0,bsqrt=0,asqrt=0;
 for(int i=0;i<a.size();i++)
 	{ab+=a[i]*b[i];bsqrt+=b[i]*b[i];asqrt+=a[i]*a[i];}
 	
 if((bsqrt-0.0)<1e-10)return asqrt;
 return asqrt-(ab*ab/bsqrt);
}
int belongcircle[maxx];
int findbelong(int x)
{
 if(x==belongcircle[x])return x;
 	else return belongcircle[x]=findbelong(belongcircle[x]);
}

double ans,ansnow;
double min(double a,double b)
{
 if(a<b)return a;
 	else return b;
}
void combinecircle(int now,int begin)
{
 now=findbelong(now);
 if(vis[now])return ;
 vis[now]=true;
 edge e1=que[now].top();
 double coste=cost[findbelong(e1.begin)][findbelong(e1.end)];
 ansnow+=coste;
 bool finish[maxx];
 memset(finish,0,sizeof(finish));
 for(int i=0;i<=m;i++)
 	{
 	 int po=findbelong(i);
 	 if(po==now||po==begin)continue;
 	 if(finish[po])continue;
 	 finish[po]=true;
 	 cost[begin][po]=min(cost[begin][po],cost[now][po]-coste);
 	 cost[po][begin]=min(cost[po][begin],cost[po][now]);
	}
 belongcircle[now]=begin;
 combinecircle(e1.end,begin);
}

bool findcircle(int now)
{
 if(now==root)return false; 
 now=findbelong(now);
 if(indfs[now])
 	{
 	 memset(vis,0,sizeof(vis));
 	 combinecircle(now,now);
 	 return true;
	}
 indfs[now]=true;
 return findcircle(que[now].top().end);
}

void build()
{
 bool finish[maxx];
 memset(finish,0,sizeof(finish));
 for(int i=0;i<m;i++)
 	{
 	 int po=findbelong(i);
 	 if(finish[po])continue;
 	 finish[po]=true;
 	 while(!que[po].empty())que[po].pop();
 	 for(int j=0;j<=m;j++)
 	 	{
 	 	 int pb=findbelong(j);
 	 	 if(po==pb)continue;
 	 	 edge en;en.begin=po;en.end=pb;
 	 	 que[po].push(en);
		}
	}
 return ;
}
bool counted[maxx];
int main()
{
 cin>>n>>m;
 for(int i=0;i<n;i++)
    zero.push_back(0.0);
 for(int i=0;i<m;i++)
 	{
	 vector<double>nowv;
	 for(int j=0;j<n;j++)
	 	{ double nv;cin>>nv;nowv.push_back(nv);}
	 vec.push_back(nowv);
	}
 vec.push_back(zero);
 for(int i=0;i<=m;i++)
 	for(int j=0;j<=m;j++)
 		{ cost[i][j]=minv(vec[i],vec[j]);}
 root=m;
 ansnow=0;
 belongcircle[root]=root;
 	 for(int i=0;i<m;i++)
 	 	{
 	 	 belongcircle[i]=i;
 	 	}
	 bool exis=false;
	 	 do
	 	 	{
	 	 	 build();
	 	 	 exis=false;
	 	 	 bool finish[maxx];
	 	 	 memset(finish,0,sizeof(finish));
	 	 	 for(int i=0;i<m;i++)
	 	 	 {  
			    int po=findbelong(i);
			    if(finish[po])continue;
	 	 	    memset(indfs,0,sizeof(indfs));
	 	 	    exis=findcircle(po);
	 	 	    finish[po]=true;
	 	 	    if(exis){break;}
			 }
			}while(exis);
	 memset(counted,0,sizeof(counted));
	 for(int i=0;i<m;i++)
	 	{
		 int po=findbelong(i);
		 if(counted[po])continue;
		 counted[po]=true;
		 ansnow+=cost[findbelong(que[po].top().begin)][findbelong(que[po].top().end)];	
		}
 cout<<ansnow<<endl;
 return 0;
}
accuracy: 0.061224489795918366
time_ms: 10
memory_kb: 3372
final_rank: 13