Problem:
すぬけ君は，初期状態では動くことはできない．i 番目のチケットの値段は pi であり，これを買うと任意の (x, y) と任意の非負実数 t に対し(x, y) から(x + tai, y + tbi) に移動することができるようになる．すぬけ君が平面上の任意の二点間を(いくつかのチケットを組み合わせて) 移動できるようになるために買わなければならないチケットの合計金額の最小値を求めよ．

Constraints

1 ≤ n ≤ 200000

−109 ≤ ai, bi ≤ 109

1 ≤ pi ≤ 109

入力は全て整数である

Input

n
a1 b1 p1
. . .
an bn pn

Output

平面上の任意の二点間を移動できるようになるために買わなければならないチケットの合計金額の最小値を出力せよ．できない場合は-1 を出力せよ．

Sample Input 1

7
0 3 1
0 3 2
1 -1 2
0 0 1
-2 4 1
-4 0 1
2 1 2

たとえばチケット1, 3, 6 を買うとよい．

Sample Output 1

4

Sample Input 2

2
1 2 3
4 5 6

Sample Output 2

-1

Solution in Ruby (judge verdict: Wrong Answer)

class Array
	def bsearch_first_index(l = 0, r = size - 1, &pred)
		return nil if l > r
		c = (l + r) / 2 
		if pred[self[c]]
			bsearch_first_index(l, c-1, &pred) || c
		else
			bsearch_first_index(c+1, r, &pred)
		end
	end

	def bsearch_first_item(l = 0, r = size - 1, &pred)
		i = bsearch_first_index(l, r, &pred)
		i ? self[i] : nil
	end

	def bsearch_last_index(l = 0, r = size - 1, &pred)
		return nil if l > r
		c = (l + r) / 2 
		if pred[self[c]]
			bsearch_last_index(c+1, r, &pred) || c
		else
			bsearch_last_index(l, c-1, &pred)
		end
	end

	def bsearch_last_item(l = 0, r = size - 1, &pred)
		i = bsearch_last_index(l, r, &pred)
		i ? self[i] : nil
	end
end

n = gets.to_i

min_cost = Float::INFINITY

normalized = Hash.new(Float::INFINITY)
n.times do
	a, b, cost = gets.split.map(&:to_i)
	next if a == 0 && b == 0
	
	if a == 0
		b /= b.abs
	elsif b == 0
		a /= a.abs
	else
		b = Rational(b, a.abs)
		a /= a.abs
	end
	key = [Rational(a, 1), Rational(b, 1)]
	normalized[key] = cost if cost < normalized[key]
end

if normalized.size < 3
	puts -1
	exit
end

pair_cost = []
normalized.each do |(a, b), c1|
	if (c2 = normalized[[-a, -b]]) != Float::INFINITY
		pair_cost << c1 + c2
	end
end
if pair_cost.size >= 4
	min_cost = pair_cost.sort[0..3].inject(:+) / 2
end

include Math
def angle(x, y)
	t = atan2(y, x)
	t < 0 ? 2 * PI + t : t
end

tickets = normalized.keys.sort_by{|a, b| angle(a, b)}

table = [tickets.map {|a, b| normalized[[a, b]]}]
(1..log2(tickets.size).to_i).each do |k|
	table[k] = []
	(0..table[0].size-1).each do  |i|
		table[k][i] = table[k-1][i]
		j = i + (1 << k-1)
		if j < table[0].size && table[k-1][j] < table[k][i]
			table[k][i] = table[k-1][j]
		end
	end
end

EPS = 1e-10
(0..tickets.size-3).each do |i|
	t1 = angle(*tickets[i])
	j = tickets.bsearch_last_index {|a, b| angle(a, b) < t1 + PI - EPS}
	next if !j || j == i
	k = j + 1
	k += 1 until k >= tickets.size || angle(*tickets[k]) > t1 + PI + EPS
	next if k >= tickets.size

	c1 = normalized[tickets[i]]
	l = log2(j - i).to_i
	c2 = [table[l][i], table[l][j-2**l+1]].min
	l = log2(tickets.size - k).to_i
	c3 = [table[l][k], table[l][tickets.size-2**l]].min
	cost = c1 + c2 + c3
	min_cost = cost if cost < min_cost
end