A boy named Gena really wants to get to the "Russian Code Cup" finals, or at least get a t-shirt. But the offered problems are too complex, so he made an arrangement with his n friends that they will solve the problems for him.The participants are offered m problems on the contest. For each friend, Gena knows what problems he can solve. But Gena's friends won't agree to help Gena for nothing: the i-th friend asks Gena xi rubles for his help in solving all the problems he can. Also, the friend agreed to write a code for Gena only if Gena's computer is connected to at least ki monitors, each monitor costs b rubles.Gena is careful with money, so he wants to spend as little money as possible to solve all the problems. Help Gena, tell him how to spend the smallest possible amount of money. Initially, there's no monitors connected to Gena's computer.

The first line contains three integers n, m and b (1 ≤ n ≤ 100; 1 ≤ m ≤ 20; 1 ≤ b ≤ 109) — the number of Gena's friends, the number of problems and the cost of a single monitor. The following 2n lines describe the friends. Lines number 2i and (2i + 1) contain the information about the i-th friend. The 2i-th line contains three integers xi, ki and mi (1 ≤ xi ≤ 109; 1 ≤ ki ≤ 109; 1 ≤ mi ≤ m) — the desired amount of money, monitors and the number of problems the friend can solve. The (2i + 1)-th line contains mi distinct positive integers — the numbers of problems that the i-th friend can solve. The problems are numbered from 1 to m.

Print the minimum amount of money Gena needs to spend to solve all the problems. Or print -1, if this cannot be achieved.

#$stdin.reopen('/cygdrive/d/input.txt', 'r')

class Friend
	def initialize(cost, monitor, nsolve, array)
		@cost,@monitor, @nsolve, @array = cost,
			monitor, nsolve, array
	end
	attr_accessor :cost, :monitor, :nsolve, :array
end

class Min
	def initialize(cost,monitor,ruble)
		@cost,@monitor,@ruble=cost,monitor,ruble

	end
	attr_accessor :cost, :monitor, :ruble
end

def init

	friend_list = []
	n,m,b = gets.split.map(&:to_i)

	(1..n).each do |ind|
		cost,monitor,nsolve=gets.split.map(&:to_i)
		array = gets.split.map(&:to_i)
		friend_list.push(Friend.new(cost,monitor,nsolve,array))
	end

	array = [0]*(m+1)

	(0..m).each do |ind|
		array[ind] = []
	end

	(0...friend_list.size).each do |ind|
		friend_list[ind].array.each do |e|
			array[e].push(friend_list[ind])
		end
	end

	#each element in array should be sorted.

	array.each do |e|
		e.sort! do |a,c|
			if (a.cost+a.monitor*b)/a.nsolve.to_f > (c.cost+c.monitor*b)/c.nsolve.to_f
				1
			else
				-1
			end
		end
	end

	return n,m,b,array
end

def index(problem)

	i,s=1,0
	size = problem.size
	while i<size
		s=s<<1
		s+=1 if problem[i]>=1
		i+=1
	end
	s
end

def solve(array,ind,problem,t,current,min)

	if ind==array.size
		#puts current.cost
		if min.cost == -1 or current.cost<min.cost
			min.cost = current.cost
			min.monitor = current.monitor
		end
		return
	end

	if problem[ind] >= 1
		solve(array, ind+1,  problem, t, current, min)
	else

		if array[ind].empty?
			puts -1
			exit 0
		end

		array[ind].each do |e|

			i=e.monitor>current.monitor ? e.monitor-current.monitor : 0
			cost = current.cost+i*min.ruble+e.cost
			next if (min.cost!=-1 and cost >= min.cost)
			current.cost, current.monitor = cost, current.monitor+i
			e.array.each do |ind|
				problem[ind]+=1
			end
			#print problem; puts
=begin
			m= index(problem)
			if t[m] == -1 or current.cost< t[m]
				t[m] = current.cost
				solve(array, ind+1, problem,t, current,min)
			end
=end

			m =problem.to_s+current.monitor.to_s
			if !t.has_key?(m) or current.cost < t[m]
				t[m] = current.cost
				solve(array,ind+1,problem, t, current, min)
			end

			current.cost-= (e.cost+i*min.ruble)
			current.monitor -= i
			e.array.each do |ind|
				problem[ind]-=1
			end

		end
	end
end

def main
	n,m,b,array = init
=begin
	array[1].each do |e|
		puts (e.cost+e.monitor*b)/e.nsolve.to_f
	end
=end
	current, min, problem  = Min.new(0,0,b), Min.new(-1,0,b), [0]*(m+1)

	#t = [-1]*(1<<22)
	t=Hash.new

	solve(array, 1, problem, t,current, min)
	puts min.cost

end

main

#$stdin.reopen('/cygdrive/d/input.txt', 'r')

class Friend
	def initialize(cost, monitor, nsolve, array)
		@cost,@monitor, @nsolve, @array = cost,
			monitor, nsolve, array
	end
	attr_accessor :cost, :monitor, :nsolve, :array
end

class Min
	def initialize(cost,monitor,ruble)
		@cost,@monitor,@ruble=cost,monitor,ruble

	end
	attr_accessor :cost, :monitor, :ruble
end

def init

	friend_list = []
	n,m,b = gets.split.map(&:to_i)

	(1..n).each do |ind|
		cost,monitor,nsolve=gets.split.map(&:to_i)
		array = gets.split.map(&:to_i)
		friend_list.push(Friend.new(cost,monitor,nsolve,array))
	end

	array = [0]*(m+1)

	(0..m).each do |ind|
		array[ind] = []
	end

	(0...friend_list.size).each do |ind|
		friend_list[ind].array.each do |e|
			array[e].push(friend_list[ind])
		end
	end

	#each element in array should be sorted.

	array.each do |e|
		e.sort! do |a,c|
			if (a.cost+a.monitor*b)/a.nsolve.to_f > (c.cost+c.monitor*b)/c.nsolve.to_f
				1
			else
				-1
			end
		end
	end

=begin
	array.each do |e|
		e.sort! do |a,c|
			if a.monitor< c.monitor
				1
			else
				-1
			end
		end
	end
=end
	return n,m,b,array
end

def index(problem)

	i,s=1,0
	size = problem.size
	while i<size
		s=s<<1
		s+=1 if problem[i]>=1
		i+=1
	end
	s
end

def solve(array,ind,problem,t,current,min)

	if ind==array.size
		#puts current.cost
		if min.cost == -1 or current.cost<min.cost
			min.cost = current.cost
			min.monitor = current.monitor
		end
		return
	end

	if problem[ind] >= 1
		solve(array, ind+1,  problem, t, current, min)
	else

		if array[ind].empty?
			puts -1
			exit 0
		end

		array[ind].each do |e|

			i=e.monitor>current.monitor ? e.monitor-current.monitor : 0
			cost = current.cost+i*min.ruble+e.cost
			next if (min.cost!=-1 and cost >= min.cost)
			current.cost, current.monitor = cost, current.monitor+i
			e.array.each do |ind|
				problem[ind]+=1
			end
			#print problem; puts
=begin
			m= index(problem)
			if t[m] == -1 or current.cost< t[m]
				t[m] = current.cost
				solve(array, ind+1, problem,t, current,min)
			end
=end

			m =problem.to_s+current.monitor.to_s
			if !t.has_key?(m) or current.cost < t[m]
				t[m] = current.cost
				solve(array,ind+1,problem, t, current, min)
			end

			current.cost-= (e.cost+i*min.ruble)
			current.monitor -= i
			e.array.each do |ind|
				problem[ind]-=1
			end

		end
	end
end

def main
	n,m,b,array = init
=begin
	array[1].each do |e|
		puts (e.cost+e.monitor*b)/e.nsolve.to_f
	end
=end
	current, min, problem  = Min.new(0,0,b), Min.new(-1,0,b), [0]*(m+1)

	#t = [-1]*(1<<22)
	t=Hash.new

	solve(array, 1, problem, t,current, min)
	puts min.cost

end

main

#$stdin.reopen('/cygdrive/d/input.txt', 'r')

class Friend
	def initialize(cost, monitor, nsolve, array)
		@cost,@monitor, @nsolve, @array = cost,
			monitor, nsolve, array
	end
	attr_accessor :cost, :monitor, :nsolve, :array
end

class Min
	def initialize(cost,monitor,ruble)
		@cost,@monitor,@ruble=cost,monitor,ruble

	end
	attr_accessor :cost, :monitor, :ruble
end

def init

	friend_list = []
	n,m,b = gets.split.map(&:to_i)

	(1..n).each do |ind|
		cost,monitor,nsolve=gets.split.map(&:to_i)
		array = gets.split.map(&:to_i)
		friend_list.push(Friend.new(cost,monitor,nsolve,array))
	end

	array = [0]*(m+1)

	(0..m).each do |ind|
		array[ind] = []
	end

	(0...friend_list.size).each do |ind|
		friend_list[ind].array.each do |e|
			array[e].push(friend_list[ind])
		end
	end

	#each element in array should be sorted.
=begin
	array.each do |e|
		e.sort! do |a,c|
			if (a.cost+a.monitor*b)/a.nsolve.to_f > (c.cost+c.monitor*b)/c.nsolve.to_f
				1
			else
				-1
			end
		end
	end
=end

	array.each do |e|
		e.sort! do |a,c|
			if a.monitor> c.monitor
				1
			else
				-1
			end
		end
	end
	return n,m,b,array
end

def index(problem)

	i,s=1,0
	size = problem.size
	while i<size
		s=s<<1
		s+=1 if problem[i]>=1
		i+=1
	end
	s
end

def solve(array,ind,problem,t,current,min)

	if ind==array.size
		#puts current.cost
		if min.cost == -1 or current.cost<min.cost
			min.cost = current.cost
			min.monitor = current.monitor
		end
		return
	end

	if problem[ind] >= 1
		solve(array, ind+1,  problem, t, current, min)
	else

		if array[ind].empty?
			puts -1
			exit 0
		end

		array[ind].each do |e|

			i=e.monitor>current.monitor ? e.monitor-current.monitor : 0
			cost = current.cost+i*min.ruble+e.cost
			next if (min.cost!=-1 and cost >= min.cost)
			current.cost, current.monitor = cost, current.monitor+i
			e.array.each do |ind|
				problem[ind]+=1
			end
			#print problem; puts
=begin
			m= index(problem)
			if t[m] == -1 or current.cost< t[m]
				t[m] = current.cost
				solve(array, ind+1, problem,t, current,min)
			end
=end

			m =problem.to_s+current.monitor.to_s
			if !t.has_key?(m) or current.cost < t[m]
				t[m] = current.cost
				solve(array,ind+1,problem, t, current, min)
			end

			current.cost-= (e.cost+i*min.ruble)
			current.monitor -= i
			e.array.each do |ind|
				problem[ind]-=1
			end

		end
	end
end

def main
	n,m,b,array = init
=begin
	array[1].each do |e|
		puts (e.cost+e.monitor*b)/e.nsolve.to_f
	end
=end
	current, min, problem  = Min.new(0,0,b), Min.new(-1,0,b), [0]*(m+1)

	#t = [-1]*(1<<22)
	t=Hash.new

	solve(array, 1, problem, t,current, min)
	puts min.cost

end

main

#$stdin.reopen('/cygdrive/d/input.txt', 'r')

class Friend
	def initialize(cost, monitor, nsolve, array)
		@cost,@monitor, @nsolve, @array = cost,
			monitor, nsolve, array
	end
	attr_accessor :cost, :monitor, :nsolve, :array
end

class Min
	def initialize(cost,monitor,ruble)
		@cost,@monitor,@ruble=cost,monitor,ruble

	end
	attr_accessor :cost, :monitor, :ruble
end

def init

	friend_list = []
	n,m,b = gets.split.map(&:to_i)

	(1..n).each do |ind|
		cost,monitor,nsolve=gets.split.map(&:to_i)
		array = gets.split.map(&:to_i)
		friend_list.push(Friend.new(cost,monitor,nsolve,array))
	end

	array = [0]*(m+1)

	(0..m).each do |ind|
		array[ind] = []
	end

	(0...friend_list.size).each do |ind|
		friend_list[ind].array.each do |e|
			array[e].push(friend_list[ind])
		end
	end

	#each element in array should be sorted.
=begin
	array.each do |e|
		e.sort! do |a,c|
			if (a.cost+a.monitor*b)/a.nsolve.to_f > (c.cost+c.monitor*b)/c.nsolve.to_f
				1
			else
				-1
			end
		end
	end
=end

	array.each do |e|
		e.sort! do |a,c|
			if a.monitor< c.monitor
				1
			else
				-1
			end
		end
	end
	return n,m,b,array
end

def index(problem)

	i,s=1,0
	size = problem.size
	while i<size
		s=s<<1
		s+=1 if problem[i]>=1
		i+=1
	end
	s
end

def solve(array,ind,problem,t,current,min)

	if ind==array.size
		#puts current.cost
		if min.cost == -1 or current.cost<min.cost
			min.cost = current.cost
			min.monitor = current.monitor
		end
		return
	end

	if problem[ind] >= 1
		solve(array, ind+1,  problem, t, current, min)
	else

		if array[ind].empty?
			puts -1
			exit 0
		end

		array[ind].each do |e|

			i=e.monitor>current.monitor ? e.monitor-current.monitor : 0
			cost = current.cost+i*min.ruble+e.cost
			next if (min.cost!=-1 and cost >= min.cost)
			current.cost, current.monitor = cost, current.monitor+i
			e.array.each do |ind|
				problem[ind]+=1
			end
			#print problem; puts
=begin
			m= index(problem)
			if t[m] == -1 or current.cost< t[m]
				t[m] = current.cost
				solve(array, ind+1, problem,t, current,min)
			end
=end

			m =problem.to_s+current.monitor.to_s
			if !t.has_key?(m) or current.cost < t[m]
				t[m] = current.cost
				solve(array,ind+1,problem, t, current, min)
			end

			current.cost-= (e.cost+i*min.ruble)
			current.monitor -= i
			e.array.each do |ind|
				problem[ind]-=1
			end

		end
	end
end

def main
	n,m,b,array = init
=begin
	array[1].each do |e|
		puts (e.cost+e.monitor*b)/e.nsolve.to_f
	end
=end
	current, min, problem  = Min.new(0,0,b), Min.new(-1,0,b), [0]*(m+1)

	#t = [-1]*(1<<22)
	t=Hash.new

	solve(array, 1, problem, t,current, min)
	puts min.cost

end

main

#$stdin.reopen('/cygdrive/d/input.txt', 'r')

class Friend
	def initialize(cost, monitor, nsolve, array)
		@cost,@monitor, @nsolve, @array = cost,
			monitor, nsolve, array
	end
	attr_accessor :cost, :monitor, :nsolve, :array
end

class Min
	def initialize(cost,monitor,ruble)
		@cost,@monitor,@ruble=cost,monitor,ruble

	end
	attr_accessor :cost, :monitor, :ruble
end

def init

	friend_list = []
	n,m,b = gets.split.map(&:to_i)

	(1..n).each do |ind|
		cost,monitor,nsolve=gets.split.map(&:to_i)
		array = gets.split.map(&:to_i)
		friend_list.push(Friend.new(cost,monitor,nsolve,array))
	end

	array = [0]*(m+1)

	(0..m).each do |ind|
		array[ind] = []
	end

	(0...friend_list.size).each do |ind|
		friend_list[ind].array.each do |e|
			array[e].push(friend_list[ind])
		end
	end

	#each element in array should be sorted.
=begin
	array.each do |e|
		e.sort! do |a,c|
			if (a.cost+a.monitor*b)/a.nsolve.to_f > (c.cost+c.monitor*b)/c.nsolve.to_f
				1
			else
				-1
			end
		end
	end
=end
	return n,m,b,array
end

def index(problem)

	i,s=1,0
	size = problem.size
	while i<size
		s=s<<1
		s+=1 if problem[i]>=1
		i+=1
	end
	s
end

def solve(array,ind,problem,t,current,min)

	if ind==array.size
		#puts current.cost
		if min.cost == -1 or current.cost<min.cost
			min.cost = current.cost
			min.monitor = current.monitor
		end
		return
	end

	if problem[ind] >= 1
		solve(array, ind+1,  problem, t, current, min)
	else

		if array[ind].empty?
			puts -1
			exit 0
		end

		array[ind].each do |e|

			i=e.monitor>current.monitor ? e.monitor-current.monitor : 0
			cost = current.cost+i*min.ruble+e.cost
			next if (min.cost!=-1 and cost >= min.cost)
			current.cost, current.monitor = cost, current.monitor+i
			e.array.each do |ind|
				problem[ind]+=1
			end
			#print problem; puts
=begin
			m= index(problem)
			if t[m] == -1 or current.cost< t[m]
				t[m] = current.cost
				solve(array, ind+1, problem,t, current,min)
			end
=end

			m =problem.to_s+current.monitor.to_s
			if !t.has_key?(m) or current.cost < t[m]
				t[m] = current.cost
				solve(array,ind+1,problem, t, current, min)
			end

			current.cost-= (e.cost+i*min.ruble)
			current.monitor -= i
			e.array.each do |ind|
				problem[ind]-=1
			end

		end
	end
end

def main
	n,m,b,array = init
=begin
	array[1].each do |e|
		puts (e.cost+e.monitor*b)/e.nsolve.to_f
	end
=end
	current, min, problem  = Min.new(0,0,b), Min.new(-1,0,b), [0]*(m+1)

	#t = [-1]*(1<<22)
	t=Hash.new

	solve(array, 1, problem, t,current, min)
	puts min.cost

end

main

#$stdin.reopen('/home/chen/Desktop/input.txt', 'r')

class Friend
	def initialize(cost, monitor, nsolve, array)
		@cost,@monitor, @nsolve, @array = cost,
			monitor, nsolve, array
	end
	attr_accessor :cost, :monitor, :nsolve, :array
end

class Min
	def initialize(cost,monitor,ruble)
		@cost,@monitor,@ruble=cost,monitor,ruble

	end
	attr_accessor :cost, :monitor, :ruble
end

def init

	friend_list = []
	n,m,b = gets.split.map(&:to_i)

	(1..n).each do |ind|
		cost,monitor,nsolve=gets.split.map(&:to_i)
		array = gets.split.map(&:to_i)
		friend_list.push(Friend.new(cost,monitor,nsolve,array))
	end

	array = [0]*(m+1)

	(0..m).each do |ind|
		array[ind] = []
	end

	(0...friend_list.size).each do |ind|
		friend_list[ind].array.each do |e|
			array[e].push(friend_list[ind])
		end
	end

	#each element in array should be sorted.
	array.each do |e|
		e.sort! do |a,c|
			if (a.cost+a.monitor*b)/a.nsolve.to_f > (c.cost+c.monitor*b)/c.nsolve.to_f
				1
			else
				-1
			end
		end
	end

	return n,m,b,array
end

def solve(array,ind,problem,current,min)

	if ind==array.size
		if current.cost<min.cost
			min.cost = current.cost
			min.monitor = current.monitor
		end
		return
	end

	if problem[ind] >= 1
		solve(array, ind+1,  problem, current, min)
	else

		if array[ind].empty?
			puts -1
			exit 0
		end

		array[ind].each do |e|

			i=e.monitor>current.monitor ? e.monitor-current.monitor : 0
			cost = current.cost+i*min.ruble+e.cost
			next if cost > min.cost
			current.cost, current.monitor = cost, current.monitor+i
			e.array.each do |m|
				problem[m]+=1
			end
			print problem
			solve(array, ind+1, problem, current,min)
			current.cost-= (e.cost+i*min.ruble)
			current.monitor -= i
			e.array.each do |m|
				problem[m]-=1
			end

		end
	end
end

def main
	n,m,b,array = init

	array[1].each do |e|
		puts (e.cost+e.monitor*b)/e.nsolve.to_f
	end
	current, min, problem  = Min.new(0,0,b), Min.new(1<<32,0,b), [0]*(m+1)
	solve(array, 1, problem, current, min)
	puts min.cost

end

main

#$stdin.reopen('/home/chen/Desktop/input.txt', 'r')

class Friend
	def initialize(cost, monitor, nsolve, array)
		@cost,@monitor, @nsolve, @array = cost,
			monitor, nsolve, array
	end
	attr_accessor :cost, :monitor, :nsolve, :array
end

class Min
	def initialize(cost,monitor,ruble)
		@cost,@monitor,@ruble=cost,monitor,ruble

	end
	attr_accessor :cost, :monitor, :ruble
end

def init

	friend_list = []
	n,m,b = gets.split.map(&:to_i)

	(1..n).each do |ind|
		cost,monitor,nsolve=gets.split.map(&:to_i)
		array = gets.split.map(&:to_i)
		friend_list.push(Friend.new(cost,monitor,nsolve,array))
	end

	array = [0]*(m+1)

	(0..m).each do |ind|
		array[ind] = []
	end

	(0...friend_list.size).each do |ind|
		friend_list[ind].array.each do |e|
			array[e].push(friend_list[ind])
		end
	end

	#each element in array should be sorted.
	array.each do |e|
		e.sort! do |a,c|
			if (a.cost+a.monitor*b)/a.nsolve.to_f > (c.cost+c.monitor*b)/c.nsolve.to_f
				1
			else
				-1
			end
		end
	end

	return n,m,b,array
end
def index(problem)

	i,s=1,0
	size = problem.size
	while i<size
		if problem[i]!=0
			s= (s<<1)+1
		end
		i+=1
	end
	s
end

def solve(array,ind,problem,t,current,min)

	if ind==array.size

		if min.cost == -1 or current.cost<min.cost
			min.cost = current.cost
			min.monitor = current.monitor
		end
		return
	end

	if problem[ind] >= 1
		solve(array, ind+1,  problem, t, current, min)
	else

		if array[ind].empty?
			puts -1
			exit 0
		end

		array[ind].each do |e|

			i=e.monitor>current.monitor ? e.monitor-current.monitor : 0
			cost = current.cost+i*min.ruble+e.cost
			next if min.cost!=-1 and cost >= min.cost
			current.cost, current.monitor = cost, current.monitor+i
			e.array.each do |m|
				problem[m]+=1
			end

			#print problem; puts
			x= index(problem)
			if t[x] == -1 or current.cost< t[x]
				t[x] = current.cost
				solve(array, ind+1, problem,t, current,min)
			end
			current.cost-= (e.cost+i*min.ruble)
			current.monitor -= i
			e.array.each do |m|
				problem[m]-=1
			end

		end
	end
end

def main
	n,m,b,array = init

=begin
	array[1].each do |e|
		puts (e.cost+e.monitor*b)/e.nsolve.to_f
	end
=end
	current, min, problem  = Min.new(0,0,b), Min.new(-1,0,b), [0]*(m+1)
	t = [-1]*(1<<22)
	solve(array, 1, problem, t,current, min)
	puts min.cost

end

main

#$stdin.reopen('/cygdrive/d/input.txt', 'r')

class Friend
	def initialize(cost, monitor, nsolve, array)
		@cost,@monitor, @nsolve, @array = cost,
			monitor, nsolve, array
	end
	attr_accessor :cost, :monitor, :nsolve, :array
end

class Min
	def initialize(cost,monitor,ruble)
		@cost,@monitor,@ruble=cost,monitor,ruble

	end
	attr_accessor :cost, :monitor, :ruble
end

def init

	friend_list = []
	n,m,b = gets.split.map(&:to_i)

	(1..n).each do |ind|
		cost,monitor,nsolve=gets.split.map(&:to_i)
		array = gets.split.map(&:to_i)
		friend_list.push(Friend.new(cost,monitor,nsolve,array))
	end

	array = [0]*(m+1)

	(0..m).each do |ind|
		array[ind] = []
	end

	(0...friend_list.size).each do |ind|
		friend_list[ind].array.each do |e|
			array[e].push(friend_list[ind])
		end
	end

	#each element in array should be sorted.
	array.each do |e|
		e.sort! do |a,c|
			if (a.cost+a.monitor*b)/a.nsolve.to_f > (c.cost+c.monitor*b)/c.nsolve.to_f
				1
			else
				-1
			end
		end
	end

	return n,m,b,array
end

def index(problem)

	i,s=1,0
	size = problem.size
	while i<size
		if problem[i]>=1
			s= (s<<1)+1
		end
		i+=1
	end
	s
end

def solve(array,ind,problem,t,current,min)

	if ind==array.size

		if min.cost == -1 or current.cost<min.cost
			min.cost = current.cost
			min.monitor = current.monitor
		end
		return
	end

	if problem[ind] >= 1
		solve(array, ind+1,  problem, t, current, min)
	else

		if array[ind].empty?
			puts -1
			exit 0
		end

		array[ind].each do |e|

			i=e.monitor>current.monitor ? e.monitor-current.monitor : 0
			cost = current.cost+i*min.ruble+e.cost
			next if (min.cost!=-1 and cost >= min.cost)
			current.cost, current.monitor = cost, current.monitor+i
			e.array.each do |m|
				problem[m]+=1
			end
			#print problem; puts
			x= index(problem)
			if t[x] == -1 or current.cost< t[x]
				t[x] = current.cost
				solve(array, ind+1, problem,t, current,min)
			end
			current.cost-= (e.cost+i*min.ruble)
			current.monitor -= i
			e.array.each do |m|
				problem[m]-=1
			end

		end
	end
end

def main
	n,m,b,array = init

=begin
	array[1].each do |e|
		puts (e.cost+e.monitor*b)/e.nsolve.to_f
	end
=end
	current, min, problem  = Min.new(0,0,b), Min.new(-1,0,b), [0]*(m+1)
	t = [-1]*(1<<22)
	solve(array, 1, problem, t,current, min)
	puts min.cost

end

main

#$stdin.reopen('/home/chen/Desktop/input.txt', 'r')

class Friend
	def initialize(cost, monitor, nsolve, array)
		@cost,@monitor, @nsolve, @array = cost,
			monitor, nsolve, array
	end
	attr_accessor :cost, :monitor, :nsolve, :array
end

class Min
	def initialize(cost,monitor,ruble)
		@cost,@monitor,@ruble=cost,monitor,ruble

	end
	attr_accessor :cost, :monitor, :ruble
end

def init

	friend_list = []
	n,m,b = gets.split.map(&:to_i)

	(1..n).each do |ind|
		cost,monitor,nsolve=gets.split.map(&:to_i)
		array = gets.split.map(&:to_i)
		friend_list.push(Friend.new(cost,monitor,nsolve,array))
	end

	array = [0]*(m+1)

	(0..m).each do |ind|
		array[ind] = []
	end

	(0...friend_list.size).each do |ind|
		friend_list[ind].array.each do |e|
			array[e].push(friend_list[ind])
		end
	end

	#each element in array should be sorted.
	array.each do |e|
		e.sort! do |a,c|
			if (a.cost+a.monitor*b)/a.nsolve.to_f > (c.cost+c.monitor*b)/c.nsolve.to_f
				1
			else
				-1
			end
		end
	end

	return n,m,b,array
end
def index(problem)

	i,s=1,0
	size = problem.size
	while i<size
		if problem[i]!=0
			s= (s<<1)+1
		end
		i+=1
	end
	s
end

def solve(array,ind,problem,t,current,min)

	if ind==array.size

		if min.cost == -1 or current.cost<min.cost
			min.cost = current.cost
			min.monitor = current.monitor
		end
		return
	end

	if problem[ind] >= 1
		solve(array, ind+1,  problem, t, current, min)
	else

		if array[ind].empty?
			puts -1
			exit 0
		end

		array[ind].each do |e|

			i=e.monitor>current.monitor ? e.monitor-current.monitor : 0
			cost = current.cost+i*min.ruble+e.cost
			next if min.cost!=-1 and cost >= min.cost
			current.cost, current.monitor = cost, current.monitor+i
			e.array.each do |m|
				problem[m]+=1
			end

			#print problem; puts
			i= index(problem)
			if t[i] == -1 or current.cost< t[i]
				t[i] = current.cost
				solve(array, ind+1, problem,t, current,min)
			end
			current.cost-= (e.cost+i*min.ruble)
			current.monitor -= i
			e.array.each do |m|
				problem[m]-=1
			end

		end
	end
end

def main
	n,m,b,array = init

=begin
	array[1].each do |e|
		puts (e.cost+e.monitor*b)/e.nsolve.to_f
	end
=end
	current, min, problem  = Min.new(0,0,b), Min.new(-1,0,b), [0]*(m+1)
	t = [-1]*(1<<22)
	solve(array, 1, problem, t,current, min)
	puts min.cost

end

main

#$stdin.reopen('/home/chen/Desktop/input.txt', 'r')

class Friend
	def initialize(cost, monitor, nsolve, array)
		@cost,@monitor, @nsolve, @array = cost,
			monitor, nsolve, array
	end
	attr_accessor :cost, :monitor, :nsolve, :array
end

class Min
	def initialize(cost,monitor,ruble)
		@cost,@monitor,@ruble=cost,monitor,ruble

	end
	attr_accessor :cost, :monitor, :ruble
end

def init

	friend_list = []
	n,m,b = gets.split.map(&:to_i)

	(1..n).each do |ind|
		cost,monitor,nsolve=gets.split.map(&:to_i)
		array = gets.split.map(&:to_i)
		friend_list.push(Friend.new(cost,monitor,nsolve,array))
	end

	array = [0]*(m+1)

	(0..m).each do |ind|
		array[ind] = []
	end

	(0...friend_list.size).each do |ind|
		friend_list[ind].array.each do |e|
			array[e].push(friend_list[ind])
		end
	end

	#each element in array should be sorted.
	array.each do |e|
		e.sort! do |a,c|
			if (a.cost+a.monitor*b)/a.nsolve.to_f > (c.cost+c.monitor*b)/c.nsolve.to_f
				1
			else
				-1
			end
		end
	end

	return n,m,b,array
end

def solve(array,ind,problem,current,min)

	if ind==array.size
		if current.cost<min.cost
			min.cost = current.cost
			min.monitor = current.monitor
		end
		return
	end

	if problem[ind] >= 1
		solve(array, ind+1,  problem, current, min)
	else

		if array[ind].empty?
			puts -1
			exit 0
		end

		array[ind].each do |e|

			i=e.monitor>current.monitor ? e.monitor-current.monitor : 0
			cost = current.cost+i*min.ruble+e.cost
			next if cost > min.cost
			current.cost, current.monitor = cost, current.monitor+i
			e.array.each do |m|
				problem[m]+=1
			end
			#print problem
			solve(array, ind+1, problem, current,min)
			current.cost-= (e.cost+i*min.ruble)
			current.monitor -= i
			e.array.each do |m|
				problem[m]-=1
			end

		end
	end
end

def main
	n,m,b,array = init

=begin
	array[1].each do |e|
		puts (e.cost+e.monitor*b)/e.nsolve.to_f
	end
=end
	current, min, problem  = Min.new(0,0,b), Min.new(1<<32,0,b), [0]*(m+1)
	solve(array, 1, problem, current, min)
	puts min.cost

end

main

#$stdin.reopen('/cygdrive/d/input.txt', 'r')

class Friend
	def initialize(cost, monitor, nsolve, array)
		@cost,@monitor, @nsolve, @array = cost,
			monitor, nsolve, array
	end
	attr_accessor :cost, :monitor, :nsolve, :array
end

class Min
	def initialize(cost,monitor,ruble)
		@cost,@monitor,@ruble=cost,monitor,ruble

	end
	attr_accessor :cost, :monitor, :ruble
end

def init

	friend_list = []
	n,m,b = gets.split.map(&:to_i)

	(1..n).each do |ind|
		cost,monitor,nsolve=gets.split.map(&:to_i)
		array = gets.split.map(&:to_i)
		friend_list.push(Friend.new(cost,monitor,nsolve,array))
	end

	array = [0]*(m+1)

	(0..m).each do |ind|
		array[ind] = []
	end

	(0...friend_list.size).each do |ind|
		friend_list[ind].array.each do |e|
			array[e].push(friend_list[ind])
		end
	end

	#each element in array should be sorted.

	array.each do |e|
		e.sort! do |a,c|
			if (a.cost+a.monitor*b)/a.nsolve.to_f > (c.cost+c.monitor*b)/c.nsolve.to_f
				1
			else
				-1
			end
		end
	end

	return n,m,b,array
end

def index(problem)

	i,s=1,0
	size = problem.size
	while i<size
		s=s<<1
		s+=1 if problem[i]>=1
		i+=1
	end
	s
end

def solve(array,ind,problem,t,current,min)

	if ind==array.size

		if min.cost == -1 or current.cost<min.cost
			min.cost = current.cost
			min.monitor = current.monitor
		end
		return
	end

	if problem[ind] >= 1
		solve(array, ind+1,  problem, t, current, min)
	else

		if array[ind].empty?
			puts -1
			exit 0
		end

		array[ind].each do |e|

			i=e.monitor>current.monitor ? e.monitor-current.monitor : 0
			cost = current.cost+i*min.ruble+e.cost
			next if (min.cost!=-1 and cost >= min.cost)
			current.cost, current.monitor = cost, current.monitor+i
			e.array.each do |ind|
				problem[ind]+=1
			end
			#print problem; puts

			m= index(problem)
			if t[m] == -1 or current.cost< t[m]
				t[m] = current.cost
				solve(array, ind+1, problem,t, current,min)
			end
			current.cost-= (e.cost+i*min.ruble)
			current.monitor -= i
			e.array.each do |ind|
				problem[ind]-=1
			end

		end
	end
end

def main
	n,m,b,array = init
=begin
	array[1].each do |e|
		puts (e.cost+e.monitor*b)/e.nsolve.to_f
	end
=end
	current, min, problem  = Min.new(0,0,b), Min.new(-1,0,b), [0]*(m+1)
	t = [-1]*(1<<22)
	solve(array, 1, problem, t,current, min)
	puts min.cost

end

main


Your math teacher gave you the following problem:There are $$$n$$$ segments on the $$$x$$$-axis, $$$[l_1; r_1], [l_2; r_2], \ldots, [l_n; r_n]$$$. The segment $$$[l; r]$$$ includes the bounds, i.e. it is a set of such $$$x$$$ that $$$l \le x \le r$$$. The length of the segment $$$[l; r]$$$ is equal to $$$r - l$$$.Two segments $$$[a; b]$$$ and $$$[c; d]$$$ have a common point (intersect) if there exists $$$x$$$ that $$$a \leq x \leq b$$$ and $$$c \leq x \leq d$$$. For example, $$$[2; 5]$$$ and $$$[3; 10]$$$ have a common point, but $$$[5; 6]$$$ and $$$[1; 4]$$$ don't have.You should add one segment, which has at least one common point with each of the given segments and as short as possible (i.e. has minimal length). The required segment can degenerate to be a point (i.e a segment with length zero). The added segment may or may not be among the given $$$n$$$ segments.In other words, you need to find a segment $$$[a; b]$$$, such that $$$[a; b]$$$ and every $$$[l_i; r_i]$$$ have a common point for each $$$i$$$, and $$$b-a$$$ is minimal.

The first line contains integer number $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 10^{5}$$$) — the number of segments. The following $$$n$$$ lines contain segment descriptions: the $$$i$$$-th of them contains two integers $$$l_i,r_i$$$ ($$$1 \le l_i \le r_i \le 10^{9}$$$). The sum of all values $$$n$$$ over all the test cases in the input doesn't exceed $$$10^5$$$.

For each test case, output one integer — the smallest possible length of the segment which has at least one common point with all given segments.

NoteIn the first test case of the example, we can choose the segment $$$[5;7]$$$ as the answer. It is the shortest segment that has at least one common point with all given segments.

gets.to_i.times do
  s = []
  lr, rl = nil, nil
  gets.to_i.times do
    l, r = gets.split.map(&:to_i)
    rl ||= l
    lr ||= r
    rl = [rl, l].max
    lr = [lr, r].min
  end
  p [rl - lr, 0].max
end

gets.to_i.times do
  s = []
  gets.to_i.times do
    s << gets.split.map(&:to_i)
  end
  s.sort!
  p [s[-1][0] - s[0][1], 0].max
end


The spring is coming and it means that a lot of fruits appear on the counters. One sunny day little boy Valera decided to go shopping. He made a list of m fruits he wanted to buy. If Valera want to buy more than one fruit of some kind, he includes it into the list several times. When he came to the fruit stall of Ashot, he saw that the seller hadn't distributed price tags to the goods, but put all price tags on the counter. Later Ashot will attach every price tag to some kind of fruits, and Valera will be able to count the total price of all fruits from his list. But Valera wants to know now what can be the smallest total price (in case of the most «lucky» for him distribution of price tags) and the largest total price (in case of the most «unlucky» for him distribution of price tags).

The first line of the input contains two integer number n and m (1 ≤ n, m ≤ 100) — the number of price tags (which is equal to the number of different kinds of fruits that Ashot sells) and the number of items in Valera's list. The second line contains n space-separated positive integer numbers. Each of them doesn't exceed 100 and stands for the price of one fruit of some kind. The following m lines contain names of the fruits from the list. Each name is a non-empty string of small Latin letters which length doesn't exceed 32. It is guaranteed that the number of distinct fruits from the list is less of equal to n. Also it is known that the seller has in stock all fruits that Valera wants to buy.

Print two numbers a and b (a ≤ b) — the minimum and the maximum possible sum which Valera may need to buy all fruits from his list.

6 5
3 5 1 6 8 1
peach
grapefruit
banana
orange
orange

n, m = gets.split.map(&:to_i)
price = gets.split.map(&:to_i).sort
fruits = {}
m.times do
	fruit = gets.chomp
	fruits.has_key?(fruit) ? fruits[fruit] += 1 : fruits[fruit] = 1
end
count = fruits.values.sort.reverse
mn, mx = 0, 0
count.each_index{|i| mn, mx = mn + price[i] * count[i], mx + price[n-1-i] * count[i]}
puts "#{mn} #{mx}"

n,m = gets.chomp.split(/ /).map!{|x| x.to_i}
prices = gets.chomp.split(/ /).map!{|x| x.to_i}
h = Hash.new
for i in 0..m-1
	s = gets.chomp
	if h.key?(s)
		wk = h[s]
		h[s] = wk+1
	else
		h[s] = 1
	end
end

sum1 = 0
sum2 = 0
ha = h.to_a.sort!{ |a,b| b[1] <=> a[1] }
prices.sort!
for i in 0..ha.length-1
	sum1 += ha[i][1]*prices[i]
	sum2 += ha[i][1]*prices[(prices.length-1)-i]
end
puts sum1.to_s + " " + sum2.to_s

#!/usr/bin/ruby
n=gets.chomp.split(" ").map!{|i|i=i.to_i}
a=gets.chomp.split(" ").map!{|i|i=i.to_i}.sort
b=a.reverse
h=Hash.new
h.default=0
n[1].times{|i|
  h[gets.chomp]+=1;
}
g=h.values.sort.reverse
m=M=0
g.length.times{|i|
  m+=g[i]*a[i]
  M+=g[i]*b[i]
}
puts "#{m} #{M}"

n,m=gets.split.map(&:to_i)
costs=[gets.split.map(&:to_i).sort]
costs.push(costs[0].reverse)
fruit=Hash.new
fruit.default=0
m.times{ fruit[gets.split[0]]+=1 }
ans=[0,0]
fruit.size.times{|i|
    found=false
    m.downto(1){|j|
        fruit.each{|a,b|
            if b==j then
                2.times{|k| ans[k]+=j*costs[k][i] }
                found=a
                break
            end
        }
        fruit[found]=0
        break if found!=false
    }
}
printf "%d %d\n",ans[0],ans[1]

buf = STDIN.gets.chomp.split
n = buf[0].to_i
m = buf[1].to_i
prices = STDIN.gets.chomp.split
for i in 0 .. prices.size-1
  prices[i] = prices[i].to_i
end
prices.sort!

hash = Hash.new
m.times { |i|
  name = STDIN.gets.chomp
  if (name == nil)
    puts "ERROR"
  end
  if (hash[name] == nil)
    hash[name] = 1
  else
    hash[name] += 1
  end
}

nums = []
hash.each_value { |i|
  nums.push(i)
}

nums.sort!

minT = 0
maxT = 0
for i in 0..nums.size-1
  minT += nums[nums.size-1-i]*prices[i]
  maxT += nums[nums.size-1-i]*prices[prices.size-1-i]
end
puts minT.to_s + " " + maxT.to_s

fruits = {}

n,m = gets.split.map(&:to_i)
prices = gets.split.map(&:to_i)
m.times do
	fruit = gets.chomp
	if !fruits[fruit]
		fruits[fruit] = 1
	else
		fruits[fruit] += 1
	end
end

numbers = fruits.values.sort!.reverse!

prices.sort!
min_cost = 0
for i in 0...numbers.size
	min_cost += prices[i] * numbers[i]
end
print "#{min_cost} "

prices.reverse!
max_cost = 0
for i in 0...numbers.size
	max_cost += prices[i] * numbers[i]
end
puts max_cost

str=gets
str.chomp!
tmp=str.split()
num=tmp[1].to_i
str2=gets
str2.chomp!
memo=[]
str2.split().each do |s|
  memo << s.to_i
end
memo.sort!
max=0
min=0
hash=Hash.new
while name=gets
  name.chomp!
  if(hash.key?(name)==false)
    hash.store(name,1)
  else
    hash.store(name,hash[name]+1)
  end
end
number=hash.values
number=number.sort_by{|i| -i}
hash.length.times do |i|
  min+=number[i]*memo[i]
  max+=number[i]*memo[memo.length-1-i]
end
printf("%d %d\n",min,max)

n,m=gets.split.map(&:to_i)
prices = gets.split.map(&:to_i)
prices.sort!
#p prices
fruits = Hash.new
m.times do
  f = gets.chomp
  if fruits[f] == nil then
    fruits[f] = 1
  else
    fruits[f] += 1
  end
end
#p fruits
fvalues = fruits.values
fvalues.sort!
fvalues.reverse!
len = n < fvalues.length() ? n : fvalues.length()

sum = 0
len.times do |i|
  sum += fvalues[i] *  prices[i]
end
print sum, " "

prices.reverse!
sum = 0
len.times do |i|
  sum += fvalues[i] *  prices[i]
end
print sum
puts

a=gets().chomp().split(' ').map(&:to_i)
b = gets().chomp().split(' ').map(&:to_i).sort!{|x,y| x<=>y}
h=Hash.new
a[1].times { temp=gets().chomp; (h.has_key?(temp)) ? h[temp]+=1 : h[temp]=1 }
h= (h.sort_by{|_key, value| value}).reverse
s=0;e=b.length()-1;max=0;min=0
h.each(){|key,value| min+=value.to_i*b[s];max+=value.to_i*b[e];s+=1;e-=1; }
print min,' ', max

fruits = Hash.new { |hash, key| hash[key] = 0 }
n, m = STDIN.readline.chomp.split.map { |c| Integer(c) }
prices = STDIN.readline.chomp.split.map { |c| Integer(c) }
m.times do
  fruits[STDIN.readline.chomp] += 1
end

prices.sort!
fruits_sorted_d = fruits.sort { |a, b| b[1] <=> a[1] }

min_value, max_value = 0, 0
fruits_sorted_d.each_with_index do |value, index|
  min_value += (prices[index] * value[1])
  max_value += (prices[prices.size - 1 - index] * value[1])
end

puts "#{min_value} #{max_value}"


Yaroslav has an array, consisting of (2·n - 1) integers. In a single operation Yaroslav can change the sign of exactly n elements in the array. In other words, in one operation Yaroslav can select exactly n array elements, and multiply each of them by -1.Yaroslav is now wondering: what maximum sum of array elements can be obtained if it is allowed to perform any number of described operations?Help Yaroslav.

The first line contains an integer n (2 ≤ n ≤ 100). The second line contains (2·n - 1) integers — the array elements. The array elements do not exceed 1000 in their absolute value.

In a single line print the answer to the problem — the maximum sum that Yaroslav can get.

NoteIn the first sample you do not need to change anything. The sum of elements equals 150.In the second sample you need to change the sign of the first two elements. Then we get the sum of the elements equal to 100.

n = gets.to_i
a = gets.split.map(&:to_i)
m = a.count{|i| i < 0}
a = a.map(&:abs)
s = a.inject(&:+)
s -= 2 * a.min if n.even? && m.odd?
p s

def run
  n = $stdin.gets.to_i
  a = $stdin.gets.split.map(&:to_i)
  m = a.count {|x| x < 0}
  a.map!(&:abs)
  s = a.reduce(&:+)
  s -= 2 * a.min if n.even? && m.odd?
  p s
end

run if $0 == __FILE__

n = gets.to_i
a = gets.split(" ").map{|s| s.to_i}
a = a.sort
ans = 0
if n%2==1
    a.each {|x| ans+=x.abs}
else
	dlt=0
	a.each_with_index {|x,i| ans+=x;dlt=[-ans,dlt].max if i%2==1}
	ans+=dlt+dlt
end
p ans

n = gets.to_i
a = gets.strip.split(' ').map {|x| x.to_i}
h = Hash.new 0
a.each do |e|
  if e > 0
    h[1] += 1
  elsif e < 0
    h[-1] += 1
  else
    h[0] += 1
  end
end
m = h[-1]%2
k = (n%2 == 1) ? 0 : m
aa = a.map {|e| e.abs}.sort
a1 = aa[0,k]
a2 = aa[k..-1]
puts( a2.inject(0, :+) - a1.inject(0, :+) )

n = gets.to_i
a = gets.split.map(&:to_i)
m = a.count{|i| i < 0}
a = a.map(&:abs)
s = a.inject(&:+)
s -= 2 * a.min if m.odd?
p s

def convert(a, n)
  a[0, n].map {|x| x *= -1} + a[n, n - 1]
end

def run
  n = $stdin.gets.to_i
  a = $stdin.gets.split.map(&:to_i).sort
  s = a.reduce(:+)

  2.times do
    a1 = convert(a, n)
    s1 = a1.reduce(:+)
    a = a1.sort if s1 > s
  end

  puts a.reduce(:+)
end

run if $0 == __FILE__


In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$).

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$).

For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.

def solve()
    n, k = ints
    k += 1
    a = ints
    a << 100
    t = 0
    n.times do |i|
        break if k == 0
        limit = 10**(a[i + 1] - a[i]) - 1
        x = [k, limit].min
        k -= x
        t += x * 10**a[i]
    end
    t
end

def main
  t = int
  t.times do
    puts solve
  end
end

DEBUG = true
MOD = 10**9+7
YESNO = %w(No Yes)
INF = 10**9

def int; gets.to_s.to_i end
def ints; gets.to_s.split.map { |s| s.to_i } end
def int1s; gets.to_s.split.map { |s| s.to_i - 1 } end
def float; gets.to_s.to_f end
def floats; gets.to_s.split.map { |s| s.to_f } end
def array_of(&convert); gets.to_s.split.map(&convert) end
def string; gets.to_s.chomp end
def rep(n, &b); Array.new(n, &b) end
def yes; puts YESNO[1] end
def no; puts YESNO[0] end
def yesno t; puts YESNO[t] end
def zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end
def max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end
def min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end
def minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end
def gcd(*xs); xs.inject(0, :gcd) end
def matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end
def debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end
def debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join("") } if DEBUG; grid end
def if_debug; yield if DEBUG end

module Boolean
  def coerce(other); [other, to_i] end
  def +@; to_i end
  def to_int; to_i end
  def *(other); to_i * other end
end

class TrueClass
  include Boolean
  def to_i; 1 end
end

class FalseClass
  include Boolean
  def to_i; 0 end
end

class Integer
  def div_ceil(y); (self + y - 1) / y end
  def mod_inv(mod = MOD); pow(mod-2, mod) end
  def mod_div(y, mod = MOD); self * mod_inv(y, mod) % mod end
  def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end
  def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster
  def bitbrute(&block); (1<<self).times(&block) end
  def nCr(r); x = 1; (1..r).each { |i| x *= self + 1 - i; x /= i }; x; end
  def each_divisor; return Enumerator.new { |y| each_divisor { |k| y << k } } unless block_given?; k = 1; while k * k < self; if self % k == 0; yield k; yield self / k end; k += 1; end; yield k if k * k == self end
  def divisors; each_divisor.to_a end
end

class Range
  def end_open; exclude_end? ? self.end : self.end + 1 end
  def end_close; exclude_end? ? self.end - 1 : self.end end
  def upper_bound; ac, wa = self.begin, self.end_open; while wa - ac > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end
  def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end
  def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end
  def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end
end

class Array
  def power(&block); (0 ... 1 << size).each(&block) end
  def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end
  def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end
  def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end
  def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end
  def cumdiff(range); self[range.end_open] - self[range.begin]; end
end

module Enumerable
  def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end
  def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end
end

main


Berland Government decided to improve relations with neighboring countries. First of all, it was decided to build new roads so that from each city of Berland and neighboring countries it became possible to reach all the others. There are n cities in Berland and neighboring countries in total and exactly n - 1 two-way roads. Because of the recent financial crisis, the Berland Government is strongly pressed for money, so to build a new road it has to close some of the existing ones. Every day it is possible to close one existing road and immediately build a new one. Your task is to determine how many days would be needed to rebuild roads so that from each city it became possible to reach all the others, and to draw a plan of closure of old roads and building of new ones.

The first line contains integer n (2 ≤ n ≤ 1000) — amount of cities in Berland and neighboring countries. Next n - 1 lines contain the description of roads. Each road is described by two space-separated integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — pair of cities, which the road connects. It can't be more than one road between a pair of cities. No road connects the city with itself.

Output the answer, number t — what is the least amount of days needed to rebuild roads so that from each city it became possible to reach all the others. Then output t lines — the plan of closure of old roads and building of new ones. Each line should describe one day in the format i j u v — it means that road between cities i and j became closed and a new road between cities u and v is built. Cities are numbered from 1. If the answer is not unique, output any.

class DisjointSet
	def initialize(n)
		@p = [*0..n]
	end

	def root(i)
		i == @p[i] ? i : @p[i] = root(@p[i])
	end

	def setp(i, j)
		i = root(i)
		j = root(j)
		return if i == j
		@p[i] = j
	end
end

n = gets.to_i
e = n.times.map{[]}
d = DisjointSet.new(n + 1)
x = []
(2..n).each{
	a, b = gets.split.map(&:to_i)
	x << [a, b] unless d.setp(a, b)
}
p x.size
(2..n).each{|i|
	next unless d.setp(i - 1, i)
	puts "#{x[0][0]} #{x[0][1]} #{i - 1} #{i}"
	x.shift
}

$n = gets.chomp.to_i

$p = Array.new($n+1) {|i| i}
$rank = Array.new($n+1, 0)

def find(x)
  while ($p[x]!=x)
    x=$p[x]
  end
  x
end

def union(x, y)
  px = find(x)
  py = find(y)
  return if px==py
  if $rank[px]<$rank[py]
    $p[px]=py
  else
    $p[py]=px
    $rank[px]=$rank[px]+1 if $rank[px]==$rank[py]
  end
end

def find_need_road()
  1.upto($n-1){|i|
    (i+1).upto($n){|j|
      return i, j if find(i)!=find(j)
    }
  }
end

bad_roads = []
($n-1).times{
  x, y = gets.chomp.split(' ').map{|i| i.to_i}
  x, y = y, x if x > y
  if find(x)!=find(y)
    union(x, y)
  else
    bad_roads << [x, y]
  end
}

puts bad_roads.size
bad_roads.size.times{|t|
  a, b = find_need_road()
  union(a, b)
  puts "#{bad_roads[t][0]} #{bad_roads[t][1]} #{a} #{b}"
}


Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresponding to triple. Such triples are called Pythagorean triples.For example, triples (3, 4, 5), (5, 12, 13) and (6, 8, 10) are Pythagorean triples.Here Katya wondered if she can specify the length of some side of right triangle and find any Pythagorean triple corresponding to such length? Note that the side which length is specified can be a cathetus as well as hypotenuse.Katya had no problems with completing this task. Will you do the same?

The only line of the input contains single integer n (1 ≤ n ≤ 109) — the length of some side of a right triangle.

Print two integers m and k (1 ≤ m, k ≤ 1018), such that n, m and k form a Pythagorean triple, in the only line. In case if there is no any Pythagorean triple containing integer n, print  - 1 in the only line. If there are many answers, print any of them.

# Try Codeforces
# author: Leonardone @ NEETSDKASU
############################################################
def gs() gets.chomp end
def gi() gets.to_i end
def gss() gs.split end
def gis() gss.map(&:to_i) end
def nmapf(n,f) n.times.map{ __send__ f } end
def ngs(n) nmapf n,:gs end
def ngi(n) nmapf n,:gi end
def ngss(n) nmapf n,:gss end
def ngis(n) nmapf n,:gis end
def arr2d(h,w,v=0) h.times.map{[v] * w} end
def for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end
def nsum(n) n * (n + 1) / 2 end
def vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end
############################################################

n = gi

# x ** 2 == z ** 2 - y ** 2 = (z + y) * (z - y)

m = (n ** 2) / 2
k = m + 1
if n.even?
    m /= 2
    k = m + 1
    m -= 1
end

if m > 0 && k > 0 && n ** 2 + m ** 2 == k ** 2
    puts "%d %d" % [m, k]
else
    puts -1
end

n = gets.to_i
if n == 1 || n == 2
  puts -1
else
  n2 = n
  n2 /= 2 while n2 % 2 == 0
  if n2 == 1
    time = n / 4
    puts "#{3 * time} #{5 * time}"
  else
    time = n / n2
    a = n2 * n2 / 2
    m, k = a * time, (a + 1) * time
    puts "#{m} #{k}"
  end
end

require 'prime'
n = gets.to_i
factors = n.prime_division
if n == 1 || n == 2
  puts -1
elsif factors.size == 1 && factors[0][0] == 2
  time = n / 4
  puts "#{3 * time} #{5 * time}"
else
  n2 = n
  n2 /= 2 while n2 % 2 == 0
  time = n / n2
  a = ((n2 ** 2) - 1) / 2
  m, k = a * time, (a + 1) * time
  puts "#{m} #{k}"
end


Mrs. Smith is trying to contact her husband, John Smith, but she forgot the secret phone number!The only thing Mrs. Smith remembered was that any permutation of $$$n$$$ can be a secret phone number. Only those permutations that minimize secret value might be the phone of her husband.The sequence of $$$n$$$ integers is called a permutation if it contains all integers from $$$1$$$ to $$$n$$$ exactly once.The secret value of a phone number is defined as the sum of the length of the longest increasing subsequence (LIS) and length of the longest decreasing subsequence (LDS). A subsequence $$$a_{i_1}, a_{i_2}, \ldots, a_{i_k}$$$ where $$$1\leq i_1 < i_2 < \ldots < i_k\leq n$$$ is called increasing if $$$a_{i_1} < a_{i_2} < a_{i_3} < \ldots < a_{i_k}$$$. If $$$a_{i_1} > a_{i_2} > a_{i_3} > \ldots > a_{i_k}$$$, a subsequence is called decreasing. An increasing/decreasing subsequence is called longest if it has maximum length among all increasing/decreasing subsequences.For example, if there is a permutation $$$[6, 4, 1, 7, 2, 3, 5]$$$, LIS of this permutation will be $$$[1, 2, 3, 5]$$$, so the length of LIS is equal to $$$4$$$. LDS can be $$$[6, 4, 1]$$$, $$$[6, 4, 2]$$$, or $$$[6, 4, 3]$$$, so the length of LDS is $$$3$$$.Note, the lengths of LIS and LDS can be different.So please help Mrs. Smith to find a permutation that gives a minimum sum of lengths of LIS and LDS.

The only line contains one integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of permutation that you need to build.

Print a permutation that gives a minimum sum of lengths of LIS and LDS.  If there are multiple answers, print any.

NoteIn the first sample, you can build a permutation $$$[3, 4, 1, 2]$$$. LIS is $$$[3, 4]$$$ (or $$$[1, 2]$$$), so the length of LIS is equal to $$$2$$$. LDS can be ony of $$$[3, 1]$$$, $$$[4, 2]$$$, $$$[3, 2]$$$, or $$$[4, 1]$$$. The length of LDS is also equal to $$$2$$$. The sum is equal to $$$4$$$. Note that $$$[3, 4, 1, 2]$$$ is not the only permutation that is valid.In the second sample, you can build a permutation $$$[2, 1]$$$. LIS is $$$[1]$$$ (or $$$[2]$$$), so the length of LIS is equal to $$$1$$$. LDS is $$$[2, 1]$$$, so the length of LDS is equal to $$$2$$$. The sum is equal to $$$3$$$. Note that permutation $$$[1, 2]$$$ is also valid.

N = gets.to_i
M = Math.sqrt(N).floor
ans = []
(N/M).downto(0) do |i|
    ((i*M+1)..(i*M+M)).each do |j|
        ans.push(j) if j <= N
    end
end
puts ans.join(' ')

n = gets.chomp.to_i

st = Math.sqrt(n).to_i

(1..n).step(st) do |i|
  [i+st-1,n].min.downto(i) { |j| print "#{j} " }
end

puts ""

(1..(n=gets.to_i)).to_a.each_slice((n**0.5).to_i).to_a.reverse.flatten.each {|x| print "#{x} "}


Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health.The building consists of $$$n$$$ floors, each with $$$m$$$ rooms each. Let $$$(i, j)$$$ represent the $$$j$$$-th room on the $$$i$$$-th floor. Additionally, there are $$$k$$$ ladders installed. The $$$i$$$-th ladder allows Ram to travel from $$$(a_i, b_i)$$$ to $$$(c_i, d_i)$$$, but not in the other direction. Ram also gains $$$h_i$$$ health points if he uses the ladder $$$i$$$. It is guaranteed $$$a_i < c_i$$$ for all ladders.If Ram is on the $$$i$$$-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from $$$(i, j)$$$ to $$$(i, k)$$$, he loses $$$|j-k| \cdot x_i$$$ health points.Ram enters the building at $$$(1, 1)$$$ while his helicopter is waiting at $$$(n, m)$$$. What is the minimum amount of health Ram loses if he takes the most optimal path? Note this answer may be negative (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot escape the clutches of Raghav.

The first line of input contains $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The first line of each test case consists of $$$3$$$ integers $$$n, m, k$$$ ($$$2 \leq n, m \leq 10^5$$$; $$$1 \leq k \leq 10^5$$$) — the number of floors, the number of rooms on each floor and the number of ladders respectively. The second line of a test case consists of $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \leq x_i \leq 10^6$$$). The next $$$k$$$ lines describe the ladders. Ladder $$$i$$$ is denoted by $$$a_i, b_i, c_i, d_i, h_i$$$ ($$$1 \leq a_i < c_i \leq n$$$; $$$1 \leq b_i, d_i \leq m$$$; $$$1 \leq h_i \leq 10^6$$$)  — the rooms it connects and the health points gained from using it. It is guaranteed $$$a_i < c_i$$$ for all ladders and there is at most one ladder between any 2 rooms in the building. The sum of $$$n$$$, the sum of $$$m$$$, and the sum of $$$k$$$ over all test cases do not exceed $$$10^5$$$.

Output the minimum health Ram loses on the optimal path from $$$(1, 1)$$$ to $$$(n, m)$$$. If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes).

NoteThe figure for the first test case is in the statement. There are only $$$2$$$ possible paths to $$$(n, m)$$$:   Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 2)$$$, takes the ladder to $$$(5, 1)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-2| - h_3 + x_5 \cdot |1-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 1 - 6 + 4 \cdot 2 \\ &= 16. \end{align*} $$$$$$  Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 1)$$$, takes the ladder to $$$(5, 2)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-1| - h_2 + a_5 \cdot |2-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 2 - 5 + 4 \cdot 1 \\ &= 21. \end{align*} $$$$$$  Therefore, the minimum health lost would be $$$16$$$.In the second test case, there is no path to $$$(n, m)$$$.In the third case case, Ram travels to $$$(1, 3)$$$ and takes the only ladder to $$$(5, 3)$$$. He loses $$$5 \cdot 2$$$ health points and gains $$$h_1 = 100$$$ health points. Therefore the total loss is $$$10-100=-90$$$ (negative implies he gains health after the path).

Loc = Struct.new(:i, :j)

class Ladder
  attr_accessor :from, :to, :hp_boost, :dist

  def initialize(from, to, hp)
    @from = from
    @to = to
    @hp_boost = hp
  end

  def self.from_ary(ary)
    a, b, c, d, h = ary
    Ladder.new(
      Loc.new(a - 1, b - 1),
      Loc.new(c - 1, d - 1),
      h
    )
  end

  def self.read
    from_ary(gets.split.map(&:to_i))
  end

  def j(i)
    i == from.i ? from.j : to.j
  end

  def inspect
    "#{from.i} #{from.j} #{to.i} #{to.j} hp:#{hp_boost} | dist:#{dist.inspect}"
  end
end

gets.to_i.times do
  n, m, k = gets.split.map(&:to_i)
  xs = gets.split.map(&:to_i)

  floors = Array.new(n) { [] }

  start_ladder = Ladder.from_ary([0, 0, 1, 1, 0])
  start_ladder.dist = 0

  floors[0] << start_ladder
  k.times do
    ladder = Ladder.read
    floors[ladder.from.i] << ladder
    floors[ladder.to.i] << ladder
  end
  floors[n - 1] << Ladder.from_ary([n, m, n + 1, -1, 0])

  floors.each_with_index do |floor, i|
    floor.sort_by! { _1.j(i) }

    # LTR.
    prev = nil
    floor.each do |lad|
      if prev && prev.dist
        tmp = prev.dist + (lad.j(i) - prev.j(i)) * xs[i]
        lad.dist = lad.dist ? [lad.dist, tmp].min : tmp
      end
      prev = lad
    end

    # RTL
    nxt = nil
    floor.reverse_each do |lad|
      if nxt && nxt.dist
        tmp = nxt.dist + (nxt.j(i) - lad.j(i)) * xs[i]
        lad.dist = lad.dist ? [lad.dist, tmp].min : tmp
      end
      nxt = lad
    end

    floor.each do |lad|
      if lad.dist && lad.from.i == i
        lad.dist -= lad.hp_boost
      end
    end
  end

  lad = floors[n - 1].last
  puts(lad.dist ? lad.dist : "NO ESCAPE")
end


Let's consider all integers in the range from $$$1$$$ to $$$n$$$ (inclusive).Among all pairs of distinct integers in this range, find the maximum possible greatest common divisor of integers in pair. Formally, find the maximum value of $$$\mathrm{gcd}(a, b)$$$, where $$$1 \leq a < b \leq n$$$.The greatest common divisor, $$$\mathrm{gcd}(a, b)$$$, of two positive integers $$$a$$$ and $$$b$$$ is the biggest integer that is a divisor of both $$$a$$$ and $$$b$$$.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$)  — the number of test cases. The description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^6$$$).

For each test case, output the maximum value of $$$\mathrm{gcd}(a, b)$$$ among all $$$1 \leq a < b \leq n$$$.

NoteIn the first test case, $$$\mathrm{gcd}(1, 2) = \mathrm{gcd}(2, 3) = \mathrm{gcd}(1, 3) = 1$$$.In the second test case, $$$2$$$ is the maximum possible value, corresponding to $$$\mathrm{gcd}(2, 4)$$$.

t = gets.chomp.to_i
t.times do
   n = gets.chomp.to_i
   if n % 2 == 0
        aa = n
        puts aa / 2
   elsif n % 2 == 1
    a = n - 1
    puts a / 2
   end
end

gets.to_i.times {
  puts gets.to_i / 2
}

gets.to_i.times do
  puts gets.to_i.div(2)
end


You are given $$$n$$$ positive integers $$$a_1, \ldots, a_n$$$, and an integer $$$k \geq 2$$$. Count the number of pairs $$$i, j$$$ such that $$$1 \leq i < j \leq n$$$, and there exists an integer $$$x$$$ such that $$$a_i \cdot a_j = x^k$$$.

The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 \leq n \leq 10^5$$$, $$$2 \leq k \leq 100$$$). The second line contains $$$n$$$ integers $$$a_1, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$).

Print a single integer — the number of suitable pairs.

#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']

def inpf() a=gets.chomp.split(" ").map(&:to_f)end

def inps() a=gets.chomp.split(" ")end

def copy(a) Marshal.load(Marshal.dump(a)) end

def kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end

def na(n,d=0) Array.new(n,d)end

def na2(n,m,d=0) Array.new(n){Array.new(m,d)}end

def na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end

def inp() a=gets.chomp.split(" ").map(&:to_i)end

def r_up(a, b) (a+b-1)/b end

def sum(a) a.inject(:+) end

def jo(n,d)
  return n if(d == 1)
  a = jo(n,d/2)
  return a if(a > 100000)
  return a * jo(n,d-d/2)
  return jo(n,d/2) * jo(n,d-d/2)
end

require 'prime'

trr = Hash.new(0)

n,k = inp
t = inp
per = 0
ans = 0
t.each.with_index do |d,i|
  f = true
  tt = d.prime_division
  ttt = 1
  tttttt = 1
  f = false
  tt.each.with_index do |a,i|
    r = a[1]%k
    next if r == 0
    ttt *= jo(a[0],r)
    tttttt *= jo(a[0],(k-r))
    if(tttttt > 100000)
      f = true
      break
    end
  end
  next if f
  ans += trr[tttttt]
  trr[ttt] += 1
end

p ans

#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']

def inpf() a=gets.chomp.split(" ").map(&:to_f)end

def inps() a=gets.chomp.split(" ")end

def copy(a) Marshal.load(Marshal.dump(a)) end

def kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end

def na(n,d=0) Array.new(n,d)end

def na2(n,m,d=0) Array.new(n){Array.new(m,d)}end

def na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end

def inp() a=gets.chomp.split(" ").map(&:to_i)end

def r_up(a, b) (a+b-1)/b end

def sum(a) a.inject(:+) end

def jo(n,d)
  return n if(d == 1)
  return jo(n,d/2) * jo(n,d-d/2)
end

require 'prime'

trr = Hash.new(0)

n,k = inp
t = inp
per = 0
ans = 0
hh = {}
t.each.with_index do |d,i|
  f = true
  tt = d.prime_division
  ttt = 1
  tttttt = 1
  f = false
  tt.each.with_index do |a,i|
    r = a[1]%k
    if(hh[a[0]] == true)
      f = true
      break
    elsif(hh[a[0]] == nil)
      hh[a[0]] = (jo(a[0],k) > 100000)
      f = hh[a[0]]
      break if f
    end
    next if r == 0
    ttt *= jo(a[0],r)
    tttttt *= jo(a[0],(k-r))
  end
  next if f == true
  ans += trr[tttttt]
  trr[ttt] += 1
end

p ans

#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']

def inpf() a=gets.chomp.split(" ").map(&:to_f)end

def inps() a=gets.chomp.split(" ")end

def copy(a) Marshal.load(Marshal.dump(a)) end

def kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end

def na(n,d=0) Array.new(n,d)end

def na2(n,m,d=0) Array.new(n){Array.new(m,d)}end

def na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end

def inp() a=gets.chomp.split(" ").map(&:to_i)end

def r_up(a, b) (a+b-1)/b end

def sum(a) a.inject(:+) end

require 'prime'

h = Hash.new(0)

n,k = inp
t = inp
tf = na(n,false)
per = 0
t.each.with_index do |d,i|
  f = true
  tt = d.prime_division
  ttt = []
  tt.each.with_index do |a,i|
    f = false if a[1]%k != 0
    ttt << [a[0],a[1]%k] if a[1]%k != 0
  end
  if f
    tf[i] = true
    per += 1
  end
  h[ttt] += 1
end
ans = 0

t.each.with_index do |d,i|
  if tf[i]
    per -= 1
    ans += per
  else
    tt = d.prime_division
    h[tt] -= 1
    ttt = []
    tt.each do |a|
      ttt << [a[0],k-a[1]%k]
    end
    ans += h[ttt]
  end
end

p ans

#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']

def inpf() a=gets.chomp.split(" ").map(&:to_f)end

def inps() a=gets.chomp.split(" ")end

def copy(a) Marshal.load(Marshal.dump(a)) end

def kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end

def na(n,d=0) Array.new(n,d)end

def na2(n,m,d=0) Array.new(n){Array.new(m,d)}end

def na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end

def inp() a=gets.chomp.split(" ").map(&:to_i)end

def r_up(a, b) (a+b-1)/b end

def sum(a) a.inject(:+) end

require 'prime'

h = Hash.new(0)

n,k = inp
t = inp
tf = na(n,false)
per = 0
t.each.with_index do |d,i|
  f = true
  tt = d.prime_division
  ttt = []
  tt.each.with_index do |a,i|
    f = false if a[1] != k
    ttt << a if a[1]%3 != 0
  end
  if f
    tf[i] = true
    per += 1
  end
  h[ttt] += 1
end
ans = 0
t.each.with_index do |d,i|
  if tf[i]
    per -= 1
    ans += per
  else
    tt = d.prime_division
    h[tt] -= 1
    ttt = []
    tt.each do |a|
      ttt << [a[0],k-a[1]%3]
    end
    ans += h[ttt]
  end
end

p ans


You are given an array of n integer numbers a0, a1, ..., an - 1. Find the distance between two closest (nearest) minimums in it. It is guaranteed that in the array a minimum occurs at least two times.

The first line contains positive integer n (2 ≤ n ≤ 105) — size of the given array. The second line contains n integers a0, a1, ..., an - 1 (1 ≤ ai ≤ 109) — elements of the array. It is guaranteed that in the array a minimum occurs at least two times.

Print the only number — distance between two nearest minimums in the array.

n,*a=$<.read.split.map &:to_i
b=[]
m=1<<30
a.each_with_index{|e,i|(b=[];m=e)if m>e;b<<i if m==e}
p b.each_cons(2).map{|x,y|y-x}.min

n=gets.to_i
a=gets.split.map &:to_i
x=a.min
res=n+1
l=-n, r=-n
a.each_with_index{|e, i|
    next if e!=x
    if r<0
        r=i
    else
        l=r
        r=i
        res=r-l if res>r-l
    end
}
puts res


The Greatest Secret Ever consists of n words, indexed by positive integers from 1 to n. The secret needs dividing between k Keepers (let's index them by positive integers from 1 to k), the i-th Keeper gets a non-empty set of words with numbers from the set Ui = (ui, 1, ui, 2, ..., ui, |Ui|). Here and below we'll presuppose that the set elements are written in the increasing order.We'll say that the secret is safe if the following conditions are hold:  for any two indexes i, j (1 ≤ i < j ≤ k) the intersection of sets Ui and Uj is an empty set;  the union of sets U1, U2, ..., Uk is set (1, 2, ..., n);  in each set Ui, its elements ui, 1, ui, 2, ..., ui, |Ui| do not form an arithmetic progression (in particular, |Ui| ≥ 3 should hold). Let us remind you that the elements of set (u1, u2, ..., us) form an arithmetic progression if there is such number d, that for all i (1 ≤ i < s) fulfills ui + d = ui + 1. For example, the elements of sets (5), (1, 10) and (1, 5, 9) form arithmetic progressions and the elements of sets (1, 2, 4) and (3, 6, 8) don't.Your task is to find any partition of the set of words into subsets U1, U2, ..., Uk so that the secret is safe. Otherwise indicate that there's no such partition.

The input consists of a single line which contains two integers n and k (2 ≤ k ≤ n ≤ 106) — the number of words in the secret and the number of the Keepers. The numbers are separated by a single space.

If there is no way to keep the secret safe, print a single integer "-1" (without the quotes). Otherwise, print n integers, the i-th of them representing the number of the Keeper who's got the i-th word of the secret. If there are multiple solutions, print any of them.

#!/usr/bin/ruby1.9

n, k = STDIN.readline.split.collect {|s| s.to_i }
if 3*k > n
	puts -1
	exit
end

keepers = (1..k).collect {|x| x }
assignments = []
(1..k).each do |i|
	assignments.push(i)
	assignments.push(i)
end
(2*k..n-1).each do |i|
	assignments.push(keepers[i%k])
end
puts assignments.join(' ')

def run
  n, k = ARGF.readline.split.map(&:to_i)
  if k * 3 > n
    puts "-1"
  else
    ret = []
    n.times do |i|
      if i < k * 2
        ret << (i / 2) + 1
      else
        ret << (i % k) + 1
      end
    end
    puts ret.join(" ")
  end
end

run if $0 == __FILE__

n, k = gets.split(' ').collect!{|i| i.to_i}

# def arifm?(arr)
# 	result = true
# 	arr.each_index do |i|
# 		if i < arr.length - 3
# 			result = false if !arr[i+1] - arr[i] == arr[i+2] - arr[i+1]
# 		end
# 	end
# 	return result
# end

# p arifm?([1, 5, 9])

if 3*k > n
	p -1
else
	mydr = Array.new(k)
	mydr.each_index do |i|
		mydr[i] = i % k + 1
	end
	# p mydr
	ans = Array.new(n)
	rot = false
	j = 0
	n.times do |i|
		if i < 2*k
			ans[i] = mydr[j]
			j += 1 if rot
			rot = !rot
			j = 0 if j > k-1
		else
			ans[i] = mydr[i % k]
		end
	end

	print ans.join(' ')
	# print "\n"
end

n, k = gets.split.map &:to_i

if n < 3*k
	puts -1
	exit
end

t = 1
for i in 1..k
	print t.to_s + " " + t.to_s + " "
	t+=1
end

t=1
for i in 1..k
	print t.to_s + " "
	t+=1
end

for i in 1..(n-3*k)
	print 1, " "
end

#!/usr/bin/ruby1.9

n, k = STDIN.readline.split.collect {|s| s.to_i }
if 3*k > n
	puts -1
	exit
end

keepers = (1..k).collect {|x| x }
assignments = []
keeper_ix = 0
(1..n).each do |pos|
	assignments.push(keepers[keeper_ix])
	keeper_ix += 1
	if keeper_ix == keepers.length
		keepers = keepers.rotate
		keeper_ix = 0
	end
end
puts assignments.join(' ')

def run
  n, k = ARGF.readline.split.map(&:to_i)
  if k * 3 > n
    puts "-1"
  else
    ret = []
    for i in 0..n
      if i < k * 2
        ret << (i / 2) + 1
      else
        ret << (i % k) + 1
      end
    end
    puts ret.join(" ")
  end
end

run if $0 == __FILE__

n, k = gets.split(' ').collect!{|i| i.to_i}

# def arifm?(arr)
# 	result = true
# 	arr.each_index do |i|
# 		if i < arr.length - 3
# 			result = false if !arr[i+1] - arr[i] == arr[i+2] - arr[i+1]
# 		end
# 	end
# 	return result
# end

# p arifm?([1, 5, 9])

if n / 3.0 < k
	p -1
else
	ans = Array.new(n)

	n.times do |i|
		if i < k
			ans[i] = k - i
		elsif i < 2*k
			ans[i] = i % k + 1
		elsif i < 3*k
			ans[i] = 3*k - i
		else
			ans[i] = i % k + 1
		end
	end

	print ans.join(' ')
	# print "\n"
end

n, k = gets.split(' ').collect!{|i| i.to_i}

# def arifm?(arr)
# 	result = true
# 	arr.each_index do |i|
# 		if i < arr.length - 3
# 			result = false if !arr[i+1] - arr[i] == arr[i+2] - arr[i+1]
# 		end
# 	end
# 	return result
# end

# p arifm?([1, 5, 9])

if n / 3.0 < k
	p -1
else
	mydr = Array.new(k)
	mydr.each_index do |i|
		mydr[i] = i % k + 1
	end
	mydr.rotate!
	# p mydr
	ans = Array.new(n)

	n.times do |i|
		if i < k
			ans[i] = k - i
		elsif i < 2*k
			ans[i] = i % k + 1
		# elsif i < 3*k
		# 	ans[i] = 3*k - i
		else
			ans[i] = mydr[i % k]
		end
	end

	print ans.join(' ')
	# print "\n"
end

n, k = gets.split(' ').collect!{|i| i.to_i}

# def arifm?(arr)
# 	result = true
# 	arr.each_index do |i|
# 		if i < arr.length - 3
# 			result = false if !arr[i+1] - arr[i] == arr[i+2] - arr[i+1]
# 		end
# 	end
# 	return result
# end

# p arifm?([1, 5, 9])

if n / 3.0 < k
	p -1
else
	ans = Array.new(n)

	n.times do |i|
		if i < k
			ans[i] = k - i
		else
			ans[i] = i % k + 1
		end

	end

	print ans.join(' ')
	# print "\n"
end

n, k = gets.split(' ').collect!{|i| i.to_i}

# def arifm?(arr)
# 	result = true
# 	arr.each_index do |i|
# 		if i < arr.length - 3
# 			result = false if !arr[i+1] - arr[i] == arr[i+2] - arr[i+1]
# 		end
# 	end
# 	return result
# end

# p arifm?([1, 5, 9])

if 3*k > n
	p -1
else
	mydr = Array.new(k)
	mydr.each_index do |i|
		mydr[i] = i % k + 1
	end
	p mydr
	ans = Array.new(n)
	rot = false
	j = 0
	n.times do |i|
		if i < 2*k
			ans[i] = mydr[j]
			j += 1 if rot
			rot = !rot
			j = 0 if j > k-1
		else
			ans[i] = mydr[i % k]
		end
	end

	print ans.join(' ')
	# print "\n"
end

n, k = gets.split(' ').collect!{|i| i.to_i}

# def arifm?(arr)
# 	result = true
# 	arr.each_index do |i|
# 		if i < arr.length - 3
# 			result = false if !arr[i+1] - arr[i] == arr[i+2] - arr[i+1]
# 		end
# 	end
# 	return result
# end

# p arifm?([1, 5, 9])

if n / 3.0 < k
	p -1
else
	mydr = Array.new(k)
	mydr.each_index do |i|
		mydr[i] = i % k + 1
	end
	mydr.rotate!
	# p mydr
	ans = Array.new(n)
	rot = false
	rot2 = false
	n.times do |i|
		if i < k
			ans[i] = k - i
		elsif i < 2*k
			ans[i] = i % k + 1
		elsif i < 3*k
			ans[i] = mydr[i % k]
		elsif i < 4*k
			ans[i] = i % k + 1
		elsif i < 5*k
			if !rot
				mydr.rotate!
				rot = true
			end
			ans[i] = mydr[i % k]
		else
			if !rot2
				mydr.rotate!.reverse!
				rot2 = true
			end
			ans[i] = mydr[i % k]
		end
	end

	print ans.join(' ')
	# print "\n"
end

n, k = gets.split(' ').collect!{|i| i.to_i}

# def arifm?(arr)
# 	result = true
# 	arr.each_index do |i|
# 		if i < arr.length - 3
# 			result = false if !arr[i+1] - arr[i] == arr[i+2] - arr[i+1]
# 		end
# 	end
# 	return result
# end

# p arifm?([1, 5, 9])

if n / 3.0 < k
	p -1
else
	mydr = Array.new(k)
	mydr.each_index do |i|
		mydr[i] = i % k + 1
	end
	mydr.rotate!
	# p mydr
	ans = Array.new(n)
	rot = false
	rot2 = false
	n.times do |i|
		if i < k
			ans[i] = k - i
		elsif i < 2*k
			ans[i] = i % k + 1
		elsif i < 100*k
			ans[i] = mydr[i % k]
		elsif i < 1000*k
			ans[i] = i % k + 1
		elsif i < 10000*k
			if !rot
				mydr.rotate!
				rot = true
			end
			ans[i] = mydr[i % k]
		else
			if !rot2
				mydr.rotate!
				rot2 = true
			end
			ans[i] = mydr[i % k]
		end
	end

	print ans.join(' ')
	# print "\n"
end

n, k = gets.split(' ').collect!{|i| i.to_i}

# def arifm?(arr)
# 	result = true
# 	arr.each_index do |i|
# 		if i < arr.length - 3
# 			result = false if !arr[i+1] - arr[i] == arr[i+2] - arr[i+1]
# 		end
# 	end
# 	return result
# end

# p arifm?([1, 5, 9])

if n / 3.0 < k
	p -1
else
	mydr = Array.new(k)
	mydr.each_index do |i|
		mydr[i] = i % k + 1
	end
	mydr.rotate!
	# p mydr
	ans = Array.new(n)
	rot = false
	rot2 = false
	n.times do |i|
		if i < k
			ans[i] = k - i
		elsif i < 2*k
			ans[i] = i % k + 1
		elsif i < 100*k
			ans[i] = mydr[i % k]
		elsif i < 1000*k
			ans[i] = i % k + 1
		elsif i < 10000*k
			if !rot
				mydr.rotate!
				rot = true
			end
			ans[i] = mydr[i % k]
		else
			if !rot2
				mydr.rotate!.reverse!
				rot2 = true
			end
			ans[i] = mydr[i % k]
		end
	end

	print ans.join(' ')
	# print "\n"
end

n, k = gets.split(' ').collect!{|i| i.to_i}

# def arifm?(arr)
# 	result = true
# 	arr.each_index do |i|
# 		if i < arr.length - 3
# 			result = false if !arr[i+1] - arr[i] == arr[i+2] - arr[i+1]
# 		end
# 	end
# 	return result
# end

# p arifm?([1, 5, 9])

if n / 3.0 < k
	p -1
else
	mydr = Array.new(k)
	mydr.each_index do |i|
		mydr[i] = i % k + 1
	end
	mydr.rotate!
	# p mydr
	ans = Array.new(n)
	rot = false
	n.times do |i|
		if i < k
			ans[i] = k - i
		elsif i < 2*k
			ans[i] = i % k + 1
		elsif i < 100*k
			ans[i] = mydr[i % k]
		elsif i < 1000*k
			ans[i] = i % k + 1
		else
			if !rot
				mydr.rotate!
				rot = true
			end
			ans[i] = mydr[i % k]
		end
	end

	print ans.join(' ')
	# print "\n"
end

n, k = gets.split(' ').collect!{|i| i.to_i}

# def arifm?(arr)
# 	result = true
# 	arr.each_index do |i|
# 		if i < arr.length - 3
# 			result = false if !arr[i+1] - arr[i] == arr[i+2] - arr[i+1]
# 		end
# 	end
# 	return result
# end

# p arifm?([1, 5, 9])

if n / 3.0 < k
	p -1
else
	mydr = Array.new(k)
	mydr.each_index do |i|
		mydr[i] = i % k + 1
	end
	mydr.rotate!
	# p mydr
	ans = Array.new(n)
	rot = false
	n.times do |i|
		if i < k
			ans[i] = mydr[i % k]
		elsif i < 2*k
			ans[i] = i % k + 1
		elsif i < 3*k
			if !rot
				mydr.reverse!
				rot = true
			end
			ans[i] = mydr[i % k]
		elsif i < 4*k
			ans[i] = i % k + 1
		elsif i < 5*k
			if rot
				mydr.rotate!
				rot = false
			end
			ans[i] = mydr[i % k]
		else
			if !rot
				mydr.rotate!.reverse!
				rot = true
			end
			ans[i] = mydr[i % k]
		end
	end

	print ans.join(' ')
	# print "\n"
end

n, k = gets.split(' ').collect!{|i| i.to_i}

# def arifm?(arr)
# 	result = true
# 	arr.each_index do |i|
# 		if i < arr.length - 3
# 			result = false if !arr[i+1] - arr[i] == arr[i+2] - arr[i+1]
# 		end
# 	end
# 	return result
# end

# p arifm?([1, 5, 9])

if n / 3.0 < k
	p -1
else
	mydr = Array.new(k)
	mydr.each_index do |i|
		mydr[i] = i % k + 1
	end
	mydr.rotate!
	# p mydr
	ans = Array.new(n)
	rot = false
	n.times do |i|
		if i < k
			ans[i] = k - i
		elsif i < 2*k
			ans[i] = i % k + 1
		elsif i < 3*k
			if !rot
				mydr.rotate!.reverse!
				rot = true
			end
			ans[i] = mydr[i % k]
		else
			if rot
				mydr.rotate!.reverse!
				rot = false
			end
			ans[i] = mydr[i % k]
		end
	end

	print ans.join(' ')
	# print "\n"
end

n, k = gets.split.map &:to_i

if n < 3*k
	puts -1
	exit
end

t = 1
for i in 1..k
	print t.to_s + " " + t.to_s + " "
	t+=1
end

t=1
for i in 1..k
	print t.to_s + " "
	t+=1
end

for i in 1..(n-k)
	print 1, " "
end


Nauuo is a girl who loves playing chess.One day she invented a game by herself which needs $$$n$$$ chess pieces to play on a $$$m\times m$$$ chessboard. The rows and columns are numbered from $$$1$$$ to $$$m$$$. We denote a cell on the intersection of the $$$r$$$-th row and $$$c$$$-th column as $$$(r,c)$$$.The game's goal is to place $$$n$$$ chess pieces numbered from $$$1$$$ to $$$n$$$ on the chessboard, the $$$i$$$-th piece lies on $$$(r_i,\,c_i)$$$, while the following rule is satisfied: for all pairs of pieces $$$i$$$ and $$$j$$$, $$$|r_i-r_j|+|c_i-c_j|\ge|i-j|$$$. Here $$$|x|$$$ means the absolute value of $$$x$$$.However, Nauuo discovered that sometimes she couldn't find a solution because the chessboard was too small.She wants to find the smallest chessboard on which she can put $$$n$$$ pieces according to the rules.She also wonders how to place the pieces on such a chessboard. Can you help her?

The only line contains a single integer $$$n$$$ ($$$1\le n\le 1000$$$) — the number of chess pieces for the game.

The first line contains a single integer — the minimum value of $$$m$$$, where $$$m$$$ is the length of sides of the suitable chessboard. The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$r_i$$$ and $$$c_i$$$ ($$$1\le r_i,c_i\le m$$$) — the coordinates of the $$$i$$$-th chess piece. If there are multiple answers, print any.

NoteIn the first example, you can't place the two pieces on a $$$1\times1$$$ chessboard without breaking the rule. But you can place two pieces on a $$$2\times2$$$ chessboard like this:In the second example, you can't place four pieces on a $$$2\times2$$$ chessboard without breaking the rule. For example, if you place the pieces like this:then $$$|r_1-r_3|+|c_1-c_3|=|1-2|+|1-1|=1$$$, $$$|1-3|=2$$$, $$$1<2$$$; and $$$|r_1-r_4|+|c_1-c_4|=|1-2|+|1-2|=2$$$, $$$|1-4|=3$$$, $$$2<3$$$. It doesn't satisfy the rule.However, on a $$$3\times3$$$ chessboard, you can place four pieces like this:

n = gets.to_i
m = n/2+1
puts m
(1..m).each do |i|
    puts "#{1} #{i}"
end
(2..m).each do |i|
    puts "#{m} #{i}" if n.odd? || i < m
end

def lscan;gets.split.map(&:to_i);end

n = gets.to_i

w = n / 2 + 1

p w
x = 1
y = 1
vx = 1
vy = 0
n.times do |i|
  puts "#{x} #{y}"
  x += vx
  y += vy
  if x > w
    x -= vx
    y -= vy
    vx,vy = vy,vx
    x += vx
    y += vy
  end
end


In the evening, after the contest Ilya was bored, and he really felt like maximizing. He remembered that he had a set of n sticks and an instrument. Each stick is characterized by its length li.Ilya decided to make a rectangle from the sticks. And due to his whim, he decided to make rectangles in such a way that maximizes their total area. Each stick is used in making at most one rectangle, it is possible that some of sticks remain unused. Bending sticks is not allowed.Sticks with lengths a1, a2, a3 and a4 can make a rectangle if the following properties are observed:  a1 ≤ a2 ≤ a3 ≤ a4  a1 = a2  a3 = a4 A rectangle can be made of sticks with lengths of, for example, 3 3 3 3 or 2 2 4 4. A rectangle cannot be made of, for example, sticks 5 5 5 7.Ilya also has an instrument which can reduce the length of the sticks. The sticks are made of a special material, so the length of each stick can be reduced by at most one. For example, a stick with length 5 can either stay at this length or be transformed into a stick of length 4.You have to answer the question — what maximum total area of the rectangles can Ilya get with a file if makes rectangles from the available sticks?

The first line of the input contains a positive integer n (1 ≤ n ≤ 105) — the number of the available sticks. The second line of the input contains n positive integers li (2 ≤ li ≤ 106) — the lengths of the sticks.

The first line of the output must contain a single non-negative integer — the maximum total area of the rectangles that Ilya can make from the available sticks.

n=gets.chomp.to_i
a=gets.chomp.split(" ").map{|x| x.to_i}
b=a.sort
i=n-1
x=1
c=0
s=0
while i>=1 do
	if (b[i]==b[i-1])
		x=x*b[i]
		i=i-1
		c=c+1
	else
		if (b[i-1]==b[i]-1)
			x=x*b[i-1]
			i=i-1
			c=c+1
		end
	end
	if c==2
		s=s+x
		x=1
		c=0
	end
i=i-1
end

puts s

gets
a = gets.split.map(&:to_i)

a.sort!

s = 0
t = 0

while a.size > 1
  ta, tb = a[-2..-1]

  if (ta - tb).abs <= 1
    if t == 0
      t = [ta, tb].min
    else
      s += t * [ta, tb].min
      t = 0
    end
    a = a[0..-3]
  else
    a = a[0..-2]
  end
end

puts s

n = gets.to_i
l = Hash.new(0)
ll = Hash.new(0)
gets.split.each{|i| l[i.to_i]+=1}
l = l.sort_by{|key, value| key}.reverse
(0...l.size).each do |i|
    if i > 0 and l[i-1][0] == l[i][0]+1 and l[i-1][1] > 0 and l[i][1] > 0
        ll[l[i][0]] += 1
        l[i-1][1] = 0
        l[i][1] -= 1
    end
    if l[i][1] >= 2
        ll[l[i][0]] += l[i][1]/2
        l[i][1] %= 2
    end
end
ll = ll.sort_by{|key, value| key}.reverse
ans = 0
(0...ll.size).each do |i|
    if ll[i][1] >= 2
        ans += ll[i][1]/2*ll[i][0]*ll[i][0]
        ll[i][1] %= 2
    end
    if ll[i][1] > 0 and i < ll.size-1
        ans += ll[i][0]*ll[i+1][0]
        ll[i][1] = 0
        ll[i+1][1] -= 1
    end
end
puts ans

n = gets.to_i
l = gets.split.map(&:to_i).sort.reverse
ans = 0
a = 0
b = 0
for i in 0...l.size - 1 do
  if l[i] == l[i + 1]
    if a == 0
      a = l[i]
    elsif b == 0
      b = l[i]
      ans += a * b
      a = 0
      b = 0
    end
    l[i] = 0
    l[i + 1] = 0
  elsif l[i] == l[i + 1] + 1
    if a == 0
      a = l[i + 1]
    elsif b == 0
      b = l[i + 1]
      ans += a * b
      a = 0
      b = 0
    end
    l[i] = 0
    l[i + 1] = 0
  end
end
puts ans

# f = File.open 'input.txt', 'r'
f = STDIN

def fuzzy_equal?(a, b)
  a == b || a - 1 == b
end

def find_pairs(sticks)
  marked_indices = []
  start_idx = 0
  sticks.each_with_index do |item, idx|
    next if idx < start_idx
    break if idx >= sticks.length - 1
    if fuzzy_equal?(item, sticks[idx + 1])
      start_idx = idx + 2
      marked_indices << sticks[idx + 1]
    end
  end
  marked_indices
end

def area(width, height)
  width * height
end

def total_areas(marked_indices)
  total_area = 0

  marked_indices.each_with_index do |item, idx|
    next if idx % 2 == 0
    total_area += area item, marked_indices[idx - 1]
  end
  total_area
end

def compute(n, sticks)
  marked_indices = find_pairs sticks
  total_areas marked_indices
end

while true
  raw_n = f.gets
  break unless raw_n
  n = raw_n.chomp.to_i
  sticks = f.gets.chomp.split.map &:to_i
  desc_sticks = sticks.sort{ |a, b| b <=> a }
  p compute(n, desc_sticks)
end

n=gets.chomp.to_i
a=gets.chomp.split(" ").map{|x| x.to_i}
b=a.sort
i=n-1
f=0
x=[]
y=[]
while i>=1 do
	if (b[i]==b[i-1])
		x<<(b[i])
		i=i-1
	end
	if x.length==2
		f=1
		break
	end
	if (b[i-1]==b[i]-1)
		y<<(b[i-1])
		i=i-1
	end
i=i-1
end
if f==1
puts x[0]*x[1]
else
	if x.length==0
		if y.length>=2
			puts y[0]*y[1]
		else
			puts 0
		end
	else
		if y.length>=1
			puts x[0]*y[0]
		else
			puts 0
		end
	end
end

n=gets.chomp.to_i
a=gets.chomp.split(" ").map{|x| x.to_i}
b=a.sort
i=n-1
x=[]
y=[]
c=0
while i>=1 do
	if (b[i]==b[i-1])
		x<<(b[i])
		i=i-1
		c=c+1
	end
	if (b[i-1]==b[i]-1)
		y<<(b[i-1])
		i=i-1
		c=c+1
	end
	if c==2
		break
	end
i=i-1
end

if c==2
	if x.length==2
	puts x[0]*x[1]
	else
		if y.length==2
		puts y[0]*y[1]
		else
		puts x[0]*y[0]
		end
	end
else
puts 0
end

n = gets.to_i
l = Hash.new(0)
ll = Hash.new(0)
gets.split.each{|i| l[i.to_i]+=1}
l = l.sort_by{|key, value| key}.reverse
(0...l.size).each do |i|
    if l[i][1] >= 2
        ll[l[i][0]] += l[i][1]/2
        l[i][1] %= 2
    end
end
(1...l.size).each do |i|
    if l[i-1][0] == l[i][0]+1 and l[i-1][1] > 0 and l[i][1] > 0
        ll[l[i][0]] += 1
        l[i-1][1] = 0
        l[i][1] = 0
    end
end
ll = ll.sort_by{|key, value| key}.reverse
ans = 0
(0...ll.size).each do |i|
    if ll[i][1] >= 2
        ans += ll[i][1]/2*ll[i][0]*ll[i][0]
        ll[i][1] %= 2
    end
    if ll[i][1] > 0 and i < ll.size-1
        ans += ll[i][0]*ll[i+1][0]
        ll[i][1] = 0
        ll[i+1][1] -= 1
    end
end
puts ans

# f = File.open 'input.txt', 'r'
f = STDIN

def fuzzy_equal?(a, b)
  a == b || a + 1 == b
end

def find_eql_sticks(n, sticks)
  [*0...(n - 1)].rindex{ |idx| fuzzy_equal? sticks[idx], sticks[idx + 1] }
end

def length(idx, sticks)
  idx ? sticks[idx] : 0
end

def area(width_idx, height_idx, sticks)
  length(width_idx, sticks) * length(height_idx, sticks)
end

def compute(n, sticks)
  first_pair_index = find_eql_sticks(n, sticks)
  second_pair_index = find_eql_sticks(first_pair_index, sticks)
  area(first_pair_index, second_pair_index, sticks)
end

while true
  raw_n = f.gets
  break unless raw_n
  n = raw_n.chomp.to_i
  sticks = f.gets.chomp.split.map &:to_i
  sorted_sticks = sticks.sort
  puts compute(n, sorted_sticks)
end


Vasya has found a piece of paper with a coordinate system written on it. There are n distinct squares drawn in this coordinate system. Let's number the squares with integers from 1 to n. It turned out that points with coordinates (0, 0) and (ai, ai) are the opposite corners of the i-th square.Vasya wants to find such integer point (with integer coordinates) of the plane, that belongs to exactly k drawn squares. We'll say that a point belongs to a square, if the point is located either inside the square, or on its boundary. Help Vasya find a point that would meet the described limits.

The first line contains two space-separated integers n, k (1 ≤ n, k ≤ 50). The second line contains space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109). It is guaranteed that all given squares are distinct.

In a single line print two space-separated integers x and y (0 ≤ x, y ≤ 109) — the coordinates of the point that belongs to exactly k squares. If there are multiple answers, you are allowed to print any of them.  If there is no answer, print "-1" (without the quotes).

n, k = gets.split(' ').map(&:to_i)
ar = gets.split(' ').map(&:to_i).sort
if k<=n
  puts "#{ar[-k]} #{ar[-k]}"
else
  puts -1
end

st = gets.split
n = st[0].to_i
k = st[1].to_i

st = gets.split
quad = []
n.times do |i|
    quad << st[i].to_i
end

quad = quad.sort
if quad[-k] then
    result = quad[-k].to_s + " " + quad[-k].to_s
else
    result = -1
end

puts result

n,k=gets.split.map(&:to_i)
puts k>n ? -1 : "#{gets.split.map(&:to_i).sort[-k]} 0"

 n,k=gets.split.map(&:to_i)
 a=gets.split.map(&:to_i)
 f=a.find_all{|ch| ch>0}.size
 if f<k and  n-f<k then
 puts -1
 else
   if n==k then
     print 0, " ", 0
     else
       if f <=n-f then
         a.sort!
         else
         a.sort!
         a.reverse!
       end
       print a[k-1]," ", a[k-1]
    end
 end

# -*- encoding: utf-8 -*-

def solve(n, k, points)
  return [- 1] if k > n

  points.sort!

  x = points[n - k]
  y = 0

  return [x, y]
end

n, k = gets.chomp.split(" ").map{|n| n.to_i}
points = gets.chomp.split(" ").map{|n| n.to_i}

puts solve(n, k, points).join(" ")

#!/usr/bin/ruby1.9

n, k = STDIN.readline.split.collect {|s| s.to_i }
as = STDIN.readline.split.collect {|s| s.to_i }
as.sort!
if k > n
	puts -1
	exit
end

ix = n-k
puts '%d %d' % [as[ix], as[ix]]

n, k = gets.split.map(&:to_i)
nums = gets.split.map(&:to_i).sort.reverse
puts n >= k ? "#{nums[k - 1]} #{nums[k - 1]}" : "-1"

def run
  n, k = ARGF.readline.split.map(&:to_i)
  ary = ARGF.readline.split.map(&:to_i).sort

  if ary.length - k >= 0
    puts ary[ary.length - k].to_s + " 0"
  else
    puts "-1"
  end
end

run if $0 == __FILE__

a=gets.chomp.split(" ").map {|i| i.to_i}
b=gets.chomp.split(" ").map {|i| i.to_i}
b.sort!
if a[0]-a[1]>=0
c=b[a[0]-a[1]]
puts "#{c} #{c-1}"
else
puts "-1"
end

n,k=gets.split.map(&:to_i)
p=gets.split.map(&:to_i).sort
print k>n ? -1 : "#{p[-k]} 0"

n,k=gets.split.map(&:to_i)
p=gets.split.map(&:to_i).sort
print ((k>n)? -1 : "#{p[-k]} #{0}")

require 'set'
require 'rational'
require 'mathn'
include Math
$stdin = File.open("./input.txt", "r") if ENV["USER"] == "new"
# ====================================

n, k = gets.split.map(&:to_i)
points = gets.split.map(&:to_i)

# -1
if k > n then
  puts -1
  exit
end

# other
points.sort!
puts "#{points[-k]} #{0}"

n, k = gets.split.map(&:to_i)
p = gets.split.map(&:to_i)
(puts -1) & exit if k>n
print ([p.sort[-k]]*2).join ' '

n,k = gets.split.map &:to_i
if k>n
    p -1
else
    v = []
    v = gets.split.map &:to_i
    v = v.sort
    puts "#{v[n-k]} #{v[n-k]}"
end

n,k=gets.split.map(&:to_i)
a=gets.split.map(&:to_i).sort
puts n<k ? -1 : ([a[n-k]]*2).join(' ')

input = gets.chomp
num = input.split
n = Integer(num[0])
k = Integer(num[1])
input2 = gets.chomp
nums = input2.split
a = Array.new
for i in 0...n
    a[i] = Integer(nums[i])
end

a = a.sort
if n >= k
    print a[n-k], " ", 0
else
    print -1
end

m,k = gets.split.map &:to_i
v = gets.split.map &:to_i
if k>m
    puts "-1"
else
    v = v.sort
    puts v[m-k].to_s+" 0"
end

n,k = gets.split.map &:to_i
v = gets.split.map &:to_i
if k>n
    puts "-1"
else
    v = v.sort
    puts v[n-k].to_s+" 0"
end

x, m = STDIN.readline.split.map(&:to_i)
arr = STDIN.readline.split.map(&:to_i).sort
if arr.size - m >= 0
	print "#{arr[arr.size-m]} #{arr[arr.size-m]}\n"
else
	p -1
end

n,k = gets.chomp.split(" ").collect! {|x| x.to_i}
line = gets.chomp.split(" ").collect! {|x| x.to_i}
line.sort!
if k > n
  puts "-1"
else
puts "#{line[n-k]} #{line[n-k]}"
end

def read_line
  gets.chomp.split.map &:to_i
end

n, k = read_line
a = read_line.sort

if k>n
	puts -1
else
	print a[n-k]
	print ' '
	puts a[n-k]
end

n,k = gets.split.map(&:to_i)
a=gets.split.map(&:to_i).sort.reverse

if k>n
  puts -1
else
  puts "%d %d" % [a[k-1],a[k-1]]
end

n, k = gets.split.map(&:to_i)
a = gets.split.map(&:to_i).sort
if k > n
  p -1
else
  print a[n-k],' ',a[n-k]
end

n, k = gets.chomp.split.map { |x| x.to_i }
a = gets.chomp.split.map { |x| x.to_i }.sort
puts(k <= n ? "#{a[n - k]} 0" : -1)

n, k = gets.split.map(&:to_i)
a = gets.split.map(&:to_i).sort
if k > n
    puts(-1)
else
    puts(([a[n - k]] * 2).join(' '))
end

#!/usr/bin/env ruby

n, k = gets.split.map(&:to_i)
a = gets.split.map(&:to_i).sort.reverse
i, t = 0, 0
while i < n
	c, j = 0, i
	c, j = c + 1, j + 1 while a[j] == a[i]
	t = t + c
	if t == k
		puts "#{a[i]} #{a[i]}"
		exit 0
	end
	i = j
end
puts -1

BEGIN{
	def gint()
		return gets.split.map(&:to_i)
	end
	def main()
		n, k = gint
		a = gint
		a = [0] + a
		a.sort!
		puts -1 if k > n
		return if k > n
		print a[-1] + 1, " ", a[-1] + 1, "\n" if k == 0
		return if k == 0
		p = n - k + 1
		print a[p], " ", a[p], "\n"
	end
	Test_case = 1
	Test_case.times do
		main()
	end
}

t = gets.split.collect{|x| x.to_i}
n = t[0]
k = t[1]

a = gets.split.collect{|x| x.to_i}
a.sort!{|x,y| y <=> x}

x = 10**9

a.each{|ai|
	if k > 0
		x = ai
		k -= 1
	elsif ai == x
		k -= 1
	end
}

if k != 0
	puts -1
else
	print x, ' ', x
end

n,k=gets.split.map(&:to_i)
a=gets.split.map(&:to_i).sort
puts k<n ? ([a[n-k]]*2).join(' ') : -1

n,k = gets.chomp.split(" ").collect! {|x| x.to_i}
line = gets.chomp.split(" ").collect! {|x| x.to_i}
line.sort!
puts "#{line[n-k]} #{line[n-k]}"