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

class MinMaxSegmentTree
	def initialize(arr, l = 0, r = arr.size - 1)
		@l = l
		@r = r
		if l == r
			@min = arr[l]
		else
			c = (l + r) / 2
			@left_child = self.class.new(arr, l, c)
			@min = @left_child.min
			@right_child = self.class.new(arr, c+1, r)
			r_min = @right_child.min
			@min = r_min if r_min < @min
		end
	end

	def query_min(l = @l, r = @r)
		return @min if l == @l && r == @r
		if r <= @left_child.r
			return @left_child.query_min(l, r)
		elsif @right_child.l <= l
			return @right_child.query_min(l, r)
		else
			lm = @left_child.query_min(l, @left_child.r)
			rm = @right_child.query_min(@right_child.l, r)
			return lm < rm ? lm : rm
		end
	end

	attr_reader :min, :l, :r
end

tickets = normalized.keys.sort_by{|a, b| angle(a, b)}
tree = MinMaxSegmentTree.new(tickets.map {|a, b| normalized[[a, b]]})

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

	cost = normalized[tickets[i]] + tree.query_min(i+1, j) + tree.query_min(k, tickets.size-1)
	min_cost = cost if cost < min_cost
end

p min_cost