Problem:
Score : 1000 points

Problem Statement

#nck {
width: 30px;
height: auto;
}

There are N cities in a two-dimensional plane.
The coordinates of the i-th city is (x_i, y_i).
Initially, the amount of water stored in the i-th city is a_i liters.

Snuke can carry any amount of water from a city to another city.
However, water leaks out a bit while he carries it.
If he carries l liters of water from the s-th city to the t-th city, only max(l-d_{s,t}, 0) liters of water remains when he arrives at the destination.
Here d_{s,t} denotes the (Euclidean) distance between the s-th city and the t-th city.
He can perform arbitrary number of operations of this type.

Snuke wants to maximize the minimum amount of water among the N cities.
Find the maximum X such that he can distribute at least X liters of water to each city.

Constraints

1 ≤ N ≤ 15

0 ≤ x_i, y_i, a_i ≤ 10^9

All values in the input are integers.

No two cities are at the same position.

Input

The input is given from Standard Input in the following format:

N
x_1 y_1 a_1
:
x_N y_N a_N

Output

Print the maximum of the minimum amount of water among the N cities.
The absolute error or the relative error must be at most 10^{-9}.

Sample Input 1

3
0 0 10
2 0 5
0 5 8

Sample Output 1

6.500000000000

The optimal solution is to carry 3.5 liters of water from the 1st city to the 2nd city.
After the operation, both the 1st and the 2nd cities will have 6.5 liters of water, and the 3rd city will have 8 liters of water.

Sample Input 2

15
335279264 849598327 822889311
446755913 526239859 548830120
181424399 715477619 342858071
625711486 448565595 480845266
647639160 467825612 449656269
160714711 336869678 545923679
61020590 573085537 816372580
626006012 389312924 135599877
547865075 511429216 605997004
561330066 539239436 921749002
650693494 63219754 786119025
849028504 632532642 655702582
285323416 611583586 211428413
990607689 590857173 393671555
560686330 679513171 501983447

Sample Output 2

434666178.237122833729

Solution in Rust (1.15.1) (judge verdict: Accepted)

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::Read;
#[allow(dead_code)]
fn getline() -> String {
    let mut ret = String::new();
    std::io::stdin().read_line(&mut ret).ok().unwrap();
    ret
}
fn get_word() -> String {
    let mut stdin = std::io::stdin();
    let mut u8b: [u8; 1] = [0];
    loop {
        let mut buf: Vec<u8> = Vec::with_capacity(16);
        loop {
            let res = stdin.read(&mut u8b);
            if res.unwrap_or(0) == 0 || u8b[0] <= b' ' {
                break;
            } else {
                buf.push(u8b[0]);
            }
        }
        if buf.len() >= 1 {
            let ret = String::from_utf8(buf).unwrap();
            return ret;
        }
    }
}

#[allow(dead_code)]
fn get<T: std::str::FromStr>() -> T { get_word().parse().ok().unwrap() }

/**
 * Union-Find tree.
 * Verified by yukicoder No.94 (http://yukicoder.me/submissions/82111)
 */
struct UnionFind { disj: Vec<usize> }

impl UnionFind {
    fn new(n: usize) -> Self {
        let mut disj = vec![0; n];
        for i in 0 .. n {
            disj[i] = i;
        }
        UnionFind { disj: disj }
    }
    fn root(self: &mut Self, x: usize) -> usize {
        if x != self.disj[x] {
            let par = self.disj[x];
            let r = self.root(par);
            self.disj[x] = r;
        }
        return self.disj[x];
    }
    fn unite(self: &mut Self, x: usize, y: usize) {
        let xr = self.root(x);
        let yr = self.root(y);
        self.disj[xr] = yr;
    }
    fn is_same_set(self: &mut Self, x: usize, y: usize) -> bool {
        return self.root(x) == self.root(y);
    }
}

fn get_triple() -> (f64, f64, f64) {
    let x = get();
    let y = get();
    let a = get();
    (x, y, a)
}

fn cmp_f64(x: f64, y: f64) -> Ordering {
    if x < y { return Ordering::Less; }
    if x > y { return Ordering::Greater; }
    Ordering::Equal
}

fn max_f64(x: f64, y: f64) -> f64 {
    if x < y { y } else { x }
}
fn min_f64(x: f64, y: f64) -> f64 {
    if x > y { y } else { x }
}

fn conn_optimal(v: &[(f64, f64, f64)]) -> f64 {
    let n = v.len();
    let mut uf = UnionFind::new(n);
    let mut edges = Vec::new();
    for i in 0 .. n {
        for j in 0 .. i {
            let dist = ((v[i].0 - v[j].0).powi(2) + (v[i].1 - v[j].1).powi(2))
                .sqrt();
            edges.push((dist, (j, i)));
        }
    }
    edges.sort_by(|x, y| cmp_f64(x.0, y.0));
    let mut tot_a = 0.0;
    let mut tot_dist = 0.0; // Minimum Spanning Tree
    for i in 0 .. n {
        tot_a += v[i].2;
    }
    for (d, (u, v)) in edges {
        if !uf.is_same_set(u, v) {
            uf.unite(u, v);
            tot_dist += d;
        }
    }
    (tot_a - tot_dist) / n as f64
}

// This code is written after the author read the editorial.
fn solve() {
    let n = get();
    let xya: Vec<_> = (0 .. n).map(|_| get_triple()).collect();
    let mut dp = vec![1.0 / 0.0; 1 << n];
    let mut conn_opt = vec![-1.0 / 0.0; 1 << n];
    for bits in 1 .. 1 << n {
        let mut vert = Vec::new();
        for i in 0 .. n {
            if (bits & 1 << i) != 0 {
                vert.push(xya[i]);
            }
        }
        conn_opt[bits] = conn_optimal(&vert);
    }
    for bits in 1 .. 1 << n {
        let mut sub = bits;
        let mut ma = -1.0 / 0.0;
        loop {
            if sub > 0 {
                ma = max_f64(ma, min_f64(dp[bits - sub], conn_opt[sub]));
            }
            if sub == 0 { break; }
            sub = (sub - 1) & bits;
        }
        dp[bits] = ma;
    }
    println!("{}", dp[(1 << n) - 1]);
}

fn main() {
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}