n=gets l=$<.map{|s|s.split.map &:to_i}.sort_by{|e|-e[2]} m=t=0;l.map{|e|t+=e[-1]} d=[*(0...n.to_i)] d.permutation.each{|a|h=a.zip(d).to_h s=t;l.take_while{|e|x,y,v=*e;h[y]m};m