require'tsort' class Array include TSort alias tsort_each_node each_index def tsort_each_child(node, &block) block.call self[node] end end r=1..n=gets.to_i s=[] l=r.map{|i|a,b=gets.split;s[i-1]=b.to_i-1;a.to_f} gone=[false]*n sum=0 s.each_strongly_connected_component{|a| c=a.min_by{|i|l[i]} while c&&!gone[c] sum+=gone[s[c]]?l[c]/2:l[c] gone[c]=true c=s.index(c) end } p sum