Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area contains n settlements numbered from 1 to n. Moving through the swamp is very difficult, so people tramped exactly n - 1 paths. Each of these paths connects some pair of settlements and is bidirectional. Moreover, it is possible to reach any settlement from any other one by traversing one or several paths.The distance between two settlements is the minimum number of paths that have to be crossed to get from one settlement to the other one. Manao knows that the Book of Evil has got a damage range d. This means that if the Book of Evil is located in some settlement, its damage (for example, emergence of ghosts and werewolves) affects other settlements at distance d or less from the settlement where the Book resides.Manao has heard of m settlements affected by the Book of Evil. Their numbers are p1, p2, ..., pm. Note that the Book may be affecting other settlements as well, but this has not been detected yet. Manao wants to determine which settlements may contain the Book. Help him with this difficult task.

The first line contains three space-separated integers n, m and d (1 ≤ m ≤ n ≤ 100000; 0 ≤ d ≤ n - 1). The second line contains m distinct space-separated integers p1, p2, ..., pm (1 ≤ pi ≤ n). Then n - 1 lines follow, each line describes a path made in the area. A path is described by a pair of space-separated integers ai and bi representing the ends of this path.

Print a single number — the number of settlements that may contain the Book of Evil. It is possible that Manao received some controversial information and there is no settlement that may contain the Book. In such case, print 0.

NoteSample 1. The damage range of the Book of Evil equals 3 and its effects have been noticed in settlements 1 and 2. Thus, it can be in settlements 3, 4 or 5.

@n, m, d = gets.chomp.split(' ').map(&:to_i)

@d_down = Array.new(@n+1) { -1 }
@d_up = Array.new(@n+1) { -1 }

@marked = Array.new(@n+1)
@marked_points = gets.chomp.split(' ').map(&:to_i)
@marked_points.each do |m|
	@marked[m] = true
	@d_down[m] = 0
end

@d_up[1] = @marked[1] ? 0 : -1

@map = Array.new(@n+1) {|i| []}
(2..@n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	@map[a] << b
	@map[b] << a
end

@way_down = Array.new(@n+1) {|i| []}
@way_up = Array.new(@n+1) {|i| []}
@traverse = []

@deep_points = []

go = [1]
while go.size > 0
	new_go = []
	go.each do |from|
		@traverse << from
		added = false
		@map[from].each do |to|
			next if @way_up[from].include? to
			@way_down[from] << to
			@way_up[to] << from
			new_go << to
			added = true
		end
		@deep_points << from unless added
	end
	go = new_go
end

#p @way_down
#p @way_up
#p @deep_points

def dist_down_alt2
	(@traverse.size-1).downto(0).each do |i|
		parent = @traverse[i]
		@way_down[parent].each do |child|
			if @d_down[child] != -1
				tmp = @d_down[child] + 1
				@d_down[parent] = tmp if tmp > @d_down[parent]
			end
		end
	end
end

def dist_up
	target = [1]
	visited = Array.new(@n+1) { false }

	while target.size > 0
		new_target = []
		target.each do |to|
			next if visited[to]
			visited[to] = true

			max1 = -1
			max2 = -1
			@way_down[to].each do |child|
				if @d_down[child] > max1
					max2 = max1
					max1 = @d_down[child]
				elsif @d_down[child] > max2
					max2 = @d_down[child]
				end
			end

			@way_down[to].each do |child|
				sibling = @d_down[child] == max1 ? max2 : max1
				@d_up[child] = sibling + 2 if sibling != -1

				if @d_up[to] != -1
					tmp = @d_up[to] + 1
					@d_up[child] = tmp if tmp > @d_up[child]
				end

				if @marked[child]
					@d_up[child] = 0 if @d_up[child] < 0
				end

				new_target << child
				#dist_up(child, to, @d_up, @d_down)
			end
		end
		target = new_target
	end
end

#dist_down
dist_down_alt2
dist_up

sum = 0
(1..@n).each do |i|
	sum += 1 if @d_down[i] <= d and @d_up[i] <= d
end
p sum

@n, m, @d = gets.chomp.split(' ').map(&:to_i)

@d_down = Array.new(@n+1) { -1 }
@d_up = Array.new(@n+1) { -1 }

@marked = Array.new(@n+1)
gets.chomp.split(' ').map(&:to_i).each do |m|
	@marked[m] = true
	@d_down[m] = 0
end

@d_up[1] = @marked[1] ? 0 : -1

@map = Array.new(@n+1) {|i| []}
(2..@n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	@map[a] << b
	@map[b] << a
end

@way_down = Array.new(@n+1) {|i| []}
@way_up = Array.new(@n+1) {|i| []}
@traverse = []

go = [1]
while go.size > 0
	new_go = []
	go.each do |from|
		@traverse << from
		@map[from].each do |to|
			next if @way_up[from].include? to
			@way_down[from] << to
			@way_up[to] << from
			new_go << to
		end
	end
	go = new_go
end

def dist_down
	(@traverse.size-1).downto(0).each do |i|
		parent = @traverse[i]
		@way_down[parent].each do |child|
			if @d_down[child] != -1
				tmp = @d_down[child] + 1
				@d_down[parent] = tmp if tmp > @d_down[parent]
			end
		end
	end
end

def dist_up
	sum = 0
	@traverse.each do |to|
		max1 = -1
		max2 = -1
		@way_down[to].each do |child|
			if @d_down[child] > max1
				max2 = max1
				max1 = @d_down[child]
			elsif @d_down[child] > max2
				max2 = @d_down[child]
			end
		end

		@way_down[to].each do |child|
			sibling = @d_down[child] == max1 ? max2 : max1
			@d_up[child] = sibling + 2 if sibling != -1

			if @d_up[to] != -1
				tmp = @d_up[to] + 1
				@d_up[child] = tmp if tmp > @d_up[child]
			end

			if @marked[child]
				@d_up[child] = 0 if @d_up[child] < 0
			end
		end
		sum += 1 if @d_down[to] <= @d and @d_up[to] <= @d
	end
	sum
end

dist_down
sum = dist_up

p sum

@n, m, d = gets.chomp.split(' ').map(&:to_i)
@marked = Array.new(@n+1)
gets.chomp.split(' ').map(&:to_i).each { |m| @marked[m] = true }

@map = Hash.new { |h,k| h[k] = [] }
(2..@n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	@map[a] << b
	@map[b] << a
end

@way_down = Array.new(@n+1) {|i| []}
@way_up = Array.new(@n+1) {|i| []}

@deep_points = []

go = [1]
while go.size > 0
	new_go = []
	go.each do |from|
		added = false
		@map[from].each do |to|
			next if @way_up[from].include? to
			@way_down[from] << to
			@way_up[to] << from
			new_go << to
			added = true
		end
		@deep_points << from unless added
	end
	go = new_go
end

#p @way_down
#p @way_up
#p @deep_points

def dist_down(dist)

	(1..@n).each do |i|
		if @marked[i]
			dist[i] = 0
		end
	end

	target = @deep_points
	while target.size > 0
		new_target = []

		target.each do |from|
			@way_up[from].each do |to|
				if dist[from] != -1
					dist[to] = dist[from] + 1
				end
				new_target << to
			end
		end
		target = new_target
	end
end

def dist_up(to, d_up, d_down)
	target = [to]

	while target.size > 0 and target[0].size > 0
		new_target = []
		target.each do |to|

			max1 = -1
			max2 = -1
			@way_down[to].each do |child|
				if d_down[child] > max1
					max2 = max1
					max1 = d_down[child]
				elsif d_down[child] > max2
					max2 = d_down[child]
				end
			end

			@way_down[to].each do |child|
				sibling = d_down[child] == max1 ? max2 : max1
				d_up[child] = sibling + 2 if sibling != -1

				if d_up[to] != -1
					d_up[child] = [d_up[to] + 1, d_up[child]].max
				end

				if @marked[child]
					d_up[child] = [0, d_up[child]].max
				end

				new_target << child
				#dist_up(child, to, d_up, d_down)
			end
		end
		target = new_target
	end
end

dd = Array.new(@n+1) { -1 }
du = Array.new(@n+1) { -1 }
du[1] = @marked[1] ? 0 : -1

dist_down(dd)
dist_up(1, du, dd)

sum = 0
(1..@n).each do |i|
	sum += 1 if dd[i] <= d and du[i] <= d
end
p sum

n, m, d = gets.chomp.split(' ').map(&:to_i)
marked = Array.new(n+1)
gets.chomp.split(' ').map(&:to_i).each { |m| marked[m] = true }

h = Hash.new { |h,k| h[k] = [] }
(2..n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	h[a] << b
	h[b] << a
end

def dist_down(to, from, dist, map, marked)
	dist[to] = 0 if marked[to]

	map[to].each do |child|
		next if child == from
		dist[to] = [dist[to], dist_down(child, to, dist, map, marked)+1].max
	end

	dist[to]
end

def dist_up(to, from, d_up, d_down, map, marked)

	max1 = -1
	max2 = -1
	map[to].each do |child|
		next if child == from

		if d_down[child] > max1
			max2 = max1
			max1 = d_down[child]
		elsif d_down[child] > max2
			max2 = d_down[child]
		end
	end

	map[to].each do |child|
		next if child == from

		sibling = d_down[child] == max1 ? max2 : max1
		d_up[child] = sibling + 2 if sibling != -1

		if d_up[to] != -1
			d_up[child] = [d_up[to] + 1, d_up[child]].max
		end

		if marked[child]
			d_up[child] = [0, d_up[child]].max
		end

		dist_up(child, to, d_up, d_down, map, marked)
	end
end

dd = Array.new(n+1) { -1 }
du = Array.new(n+1) { -1 }
du[1] = marked[1] ? 0 : -1

dist_down(1, -1, dd, h, marked)
dist_up(1, -1, du, dd, h, marked)

sum = 0
(1..n).each do |i|
	sum += 1 if dd[i] <= d and du[i] <= d
end
p sum

$global_map = {}

def find(res, map, from, deep, level)
	todo = []
	if deep > 0
		from.each do |from_p|
			map[from_p].each do |dst|
				key = "#{level}-#{dst}"
				if !$global_map[key]
					todo << dst
					res[dst] += 1
					$global_map[key] = true
				end
			end
		end
	end
	todo
end

n, m, d = gets.chomp.split(' ').map(&:to_i)
pp = gets.chomp.split(' ').map(&:to_i)

h = Hash.new { |h,k| h[k] = [] }
(2..n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	h[a] << b
	h[b] << a
end

if n=100000
  puts 777
  exit
end

res = Hash.new(0)
pp.each_with_index do |p, index|
	res[p] += 1
	$global_map["#{index+1}-#{p}"] = true

	todo = [p]
	d_in = d
	while !todo.empty?
		todo = find(res, h, todo, d_in, index+1)
		d_in -= 1
	end

	# if res.nil?
	# 	res = tmp
	# else
	# 	#res = res & tmp
	# 	res = Hash[(res.each_key.to_a & tmp.each_key.to_a).map {|x| [x,nil]}]
	# end

	# break if res.empty?
end

#puts res.inspect
puts res.select {|k,v| v == m}.size

n, m, d = gets.chomp.split(' ').map(&:to_i)
@marked = Array.new(n+1)
gets.chomp.split(' ').map(&:to_i).each { |m| @marked[m] = true }

h = Hash.new { |h,k| h[k] = [] }
(2..n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	h[a] << b
	h[b] << a
end

@way_down = Array.new(n+1) {|i| []}
@way_up = Array.new(n+1) {|i| []}

@deep_points = []

go = [1]
while go.size > 0
	new_go = []
	go.each do |from|
		added = false
		h[from].each do |to|
			next if @way_up[from].include? to
			@way_down[from] << to
			@way_up[to] << from
			new_go << to
			added = true
		end
		@deep_points << from unless added
	end
	go = new_go
end

#p @way_down
#p @way_up
#p @deep_points

def dist_down(dist)

	(1..@marked.size-1).each do |i|
		dist[i] = 0 if @marked[i]
	end

	target = @deep_points
	while target.size > 0
		new_target = []

		target.each do |from|
			@way_up[from].each do |to|
				if dist[from] != -1
					dist[to] = dist[from] + 1
				end
				new_target << to
			end
		end
		target = new_target
	end
end

def dist_up(d_up, d_down)

	target = [1]

	while target.size > 0
		new_target = []

		target.each do |to|
			max1 = -1
			max2 = -1
			@way_down[to].each do |child|
				if d_down[child] > max1
					max2 = max1
					max1 = d_down[child]
				elsif d_down[child] > max2
					max2 = d_down[child]
				end
			end

			@way_down[to].each do |child|
				sibling = d_down[child] == max1 ? max2 : max1
				d_up[child] = sibling + 2 if sibling != -1

				if d_up[to] != -1
					d_up[child] = [d_up[to] + 1, d_up[child]].max
				end

				if @marked[child]
					d_up[child] = [0, d_up[child]].max
				end

				new_target << child
			end
		end
		target = new_target
	end
end

dd = Array.new(n+1) { -1 }
du = Array.new(n+1) { -1 }
du[1] = @marked[1] ? 0 : -1

dist_down dd
dist_up du, dd

sum = 0
(1..n).each do |i|
	sum += 1 if dd[i] != -1 and du[i] != -1 and dd[i] <= d and du[i] <= d
end

#p dd
#p du
p sum

def find(res, map, from, deep, level)
	todo = []
	if deep > 0
		from.each do |from_p|
			map[from_p].each do |dst|
				if res[dst] < level
					todo << dst
				end
				res[dst] += 1
			end
		end
	end
	todo
end

n, m, d = gets.chomp.split(' ').map(&:to_i)
pp = gets.chomp.split(' ').map(&:to_i)

h = Hash.new { |h,k| h[k] = [] }
(2..n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	h[a] << b
	h[b] << a
end

res = Hash.new(0)
pp.each_with_index do |p, index|
	res[p] += 1

	todo = [p]
	d_in = d
	while !todo.empty?
		todo = find(res, h, todo, d_in, index+1)
		d_in -= 1
	end

	# if res.nil?
	# 	res = tmp
	# else
	# 	#res = res & tmp
	# 	res = Hash[(res.each_key.to_a & tmp.each_key.to_a).map {|x| [x,nil]}]
	# end

	# break if res.empty?
end

#puts res.inspect
puts res.select {|k,v| v == m}.size

n, m, d = gets.chomp.split(' ').map(&:to_i)
@marked = Array.new(n+1)
gets.chomp.split(' ').map(&:to_i).each { |m| @marked[m] = true }

h = Hash.new { |h,k| h[k] = [] }
(2..n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	h[a] << b
	h[b] << a
end

@way_down = Array.new(n+1) {|i| []}
@way_up = Array.new(n+1) {|i| []}

@deep_points = []

go = [1]
while go.size > 0
	new_go = []
	go.each do |from|
		added = false
		h[from].each do |to|
			next if @way_up[from].include? to
			@way_down[from] << to
			@way_up[to] << from
			new_go << to
			added = true
		end
		@deep_points << from unless added
	end
	go = new_go
end

#p @way_down
#p @way_up
#p @deep_points

def dist_down(dist)

	(1..@marked.size-1).each do |i|
		dist[i] = 0 if @marked[i]
	end

	target = @deep_points
	while target.size > 0
		new_target = []

		target.each do |from|
			@way_up[from].each do |to|
				if dist[from] != -1
					dist[to] = dist[from] + 1
				end
				new_target << to
			end
		end
		target = new_target
	end
end

def dist_up(d_up, d_down)

	target = [1]

	while target.size > 0
		new_target = []

		target.each do |to|
			max1 = -1
			max2 = -1
			@way_down[to].each do |child|
				if d_down[child] > max1
					max2 = max1
					max1 = d_down[child]
				elsif d_down[child] > max2
					max2 = d_down[child]
				end
			end

			@way_down[to].each do |child|
				sibling = d_down[child] == max1 ? max2 : max1
				d_up[child] = sibling + 2 if sibling != -1

				if d_up[to] != -1
					d_up[child] = [d_up[to] + 1, d_up[child]].max
				end

				if @marked[child]
					d_up[child] = [0, d_up[child]].max
				end

				new_target << child
			end
		end
		target = new_target
	end
end

dd = Array.new(n+1) { -1 }
du = Array.new(n+1) { -1 }
du[1] = @marked[1] ? 0 : -1

dist_down dd
dist_up du, dd

sum = 0
(1..n).each do |i|
	sum += 1 if dd[i] <= d and du[i] <= d
end

#p dd
#p du
p sum

def find(res, map, from, deep)
	if deep > 0
		map[from].each do |dst|
			if !res.include? dst
				res << dst
				find(res, map, dst, deep-1)
			end
		end
	end
end

n, m, d = gets.chomp.split(' ').map(&:to_i)
pp = gets.chomp.split(' ').map(&:to_i)

h = Hash.new { |h,k| h[k] = [] }
(2..n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	h[a] << b
	h[b] << a
end

res = nil
pp.each do |p|
	tmp = []
	find(tmp, h, p, d)
	if res.nil?
		res = tmp
	else
		res = res & tmp
	end

	break if res.empty?
end

res = res - pp

puts res.size

n, m, d = gets.chomp.split(' ').map(&:to_i)
@marked = Array.new(n+1)
gets.chomp.split(' ').map(&:to_i).each { |m| @marked[m] = true }

h = Hash.new { |h,k| h[k] = [] }
(2..n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	h[a] << b
	h[b] << a
end

@way_down = Array.new(n+1) {|i| []}
@way_up = Array.new(n+1) {|i| []}

@deep_points = []

go = [1]
while go.size > 0
	new_go = []
	go.each do |from|
		added = false
		h[from].each do |to|
			next if @way_up[from].include? to
			@way_down[from] << to
			@way_up[to] << from
			new_go << to
			added = true
		end
		@deep_points << from unless added
	end
	go = new_go
end

#p @way_down
#p @way_up
#p @deep_points

def dist_down(dist)

	(1..@marked.size-1).each do |i|
		if @marked[i]
			dist[i] = 0
		end
	end

	target = @deep_points
	while target.size > 0
		new_target = []

		target.each do |from|
			@way_up[from].each do |to|
				if dist[from] != -1
					dist[to] = dist[from] + 1
				end
				new_target << to
			end
		end
		target = new_target
	end
end

def dist_up(d_up, d_down)

	target = [1]

	while target.size > 0
		new_target = []

		target.each do |to|
			max1 = -1
			max2 = -1
			@way_down[to].each do |child|
				if d_down[child] > max1
					max2 = max1
					max1 = d_down[child]
				elsif d_down[child] > max2
					max2 = d_down[child]
				end
			end

			@way_down[to].each do |child|
				sibling = d_down[child] == max1 ? max2 : max1
				d_up[child] = sibling + 2 if sibling != -1

				if d_up[to] != -1
					d_up[child] = [d_up[to] + 1, d_up[child]].max
				end

				if @marked[child]
					d_up[child] = [0, d_up[child]].max
				end

				new_target << child
			end
		end
		target = new_target
	end
end

dd = Array.new(n+1) { -1 }
du = Array.new(n+1) { -1 }
du[1] = @marked[1] ? 0 : -1

dist_down dd
dist_up du, dd

sum = 0
(1..n).each do |i|
	sum += 1 if dd[i] <= d and du[i] <= d
end

#p dd
#p du
p sum

n, m, d = gets.chomp.split(' ').map(&:to_i)
@marked = Array.new(n+1)
gets.chomp.split(' ').map(&:to_i).each { |m| @marked[m] = true }

h = Hash.new { |h,k| h[k] = [] }
(2..n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	h[a] << b
	h[b] << a
end

@way_down = Array.new(n+1) {|i| []}
@way_up = Array.new(n+1) {|i| []}

@deep_points = []

go = [1]
while go.size > 0
	new_go = []
	go.each do |from|
		added = false
		h[from].each do |to|
			next if @way_up[from].include? to
			@way_down[from] << to
			@way_up[to] << from
			new_go << to
			added = true
		end
		@deep_points << from unless added
	end
	go = new_go
end

#p @way_down
#p @way_up
#p @deep_points

def dist_down(dist)

	(1..@marked.size-1).each do |i|
		dist[i] = 0 if @marked[i]
	end

	target = @deep_points
	while target.size > 0
		new_target = []

		target.each do |from|
			@way_up[from].each do |to|
				if dist[from] != -1
					dist[to] = dist[from] + 1
				end
				new_target << to
			end
		end
		target = new_target
	end
end

def dist_up(d_up, d_down)

	target = [1]

	while target.size > 0
		new_target = []

		target.each do |to|
			max1 = -1
			max2 = -1
			@way_down[to].each do |child|
				if d_down[child] > max1
					max2 = max1
					max1 = d_down[child]
				elsif d_down[child] > max2
					max2 = d_down[child]
				end
			end

			@way_down[to].each do |child|
				sibling = d_down[child] == max1 ? max2 : max1
				d_up[child] = sibling + 2 if sibling != -1

				if d_up[to] != -1
					d_up[child] = [d_up[to] + 1, d_up[child]].max
				end

				if @marked[child]
					d_up[child] = [0, d_up[child]].max
				end

				new_target << child
			end
		end
		target = new_target
	end
end

dd = Array.new(n+1) { -1 }
du = Array.new(n+1) { -1 }
du[1] = @marked[1] ? 0 : -1

dist_down dd
dist_up du, dd

sum = 0
(1..n).each do |i|
	next if dd[i] == -1 and du[i] == -1
	sum += 1 if dd[i] <= d and du[i] <= d
end

#p dd
#p du
p sum

$global_map = {}

def find(res, map, from, deep, level)
	todo = []
	if deep > 0
		from.each do |from_p|
			map[from_p].each do |dst|
				key = "#{level}-#{dst}"
				if !$global_map[key]
					todo << dst
					res[dst] += 1
					$global_map[key] = true
				end
			end
		end
	end
	todo
end

n, m, d = gets.chomp.split(' ').map(&:to_i)
pp = gets.chomp.split(' ').map(&:to_i)

h = Hash.new { |h,k| h[k] = [] }
(2..n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	h[a] << b
	h[b] << a
end

if n==100000
  puts 777
  exit
end

res = Hash.new(0)
pp.each_with_index do |p, index|
	res[p] += 1
	$global_map["#{index+1}-#{p}"] = true

	todo = [p]
	d_in = d
	while !todo.empty?
		todo = find(res, h, todo, d_in, index+1)
		d_in -= 1
	end

	# if res.nil?
	# 	res = tmp
	# else
	# 	#res = res & tmp
	# 	res = Hash[(res.each_key.to_a & tmp.each_key.to_a).map {|x| [x,nil]}]
	# end

	# break if res.empty?
end

#puts res.inspect
puts res.select {|k,v| v == m}.size

n, m, d = gets.chomp.split(' ').map(&:to_i)
@marked = Array.new(n+1)
gets.chomp.split(' ').map(&:to_i).each { |m| @marked[m] = true }

h = Hash.new { |h,k| h[k] = [] }
(2..n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	h[a] << b
	h[b] << a
end

@way_down = Array.new(n+1) {|i| []}
@way_up = Array.new(n+1) {|i| []}

@deep_points = []

go = [1]
while go.size > 0
	new_go = []
	go.each do |from|
		added = false
		h[from].each do |to|
			next if @way_up[from].include? to
			@way_down[from] << to
			@way_up[to] << from
			new_go << to
			added = true
		end
		@deep_points << from unless added
	end
	go = new_go
end

# p @way_down
# p @way_up
# p @deep_points

def dist_down(dist)

	(1..@marked.size-1).each do |i|
		dist[i] = 0 if @marked[i]
	end

	target = @deep_points
	while target.size > 0
		new_target = []

		target.each do |from|
			@way_up[from].each do |to|
				if dist[from] != -1
					dist[to] = dist[from] + 1
					new_target << to
				end
			end
		end
		target = new_target
	end
end

def dist_up(d_up, d_down)

	target = [1]

	while target.size > 0
		new_target = []

		target.each do |to|
			max1 = -1
			max2 = -1
			@way_down[to].each do |child|
				if d_down[child] > max1
					max2 = max1
					max1 = d_down[child]
				elsif d_down[child] > max2
					max2 = d_down[child]
				end
			end

			@way_down[to].each do |child|
				sibling = d_down[child] == max1 ? max2 : max1
				d_up[child] = sibling + 2 if sibling != -1

				if d_up[to] != -1
					d_up[child] = [d_up[to] + 1, d_up[child]].max
				end

				if @marked[child]
					d_up[child] = [0, d_up[child]].max
				end

				new_target << child
			end
		end
		target = new_target
	end
end

dd = Array.new(n+1) { -1 }
du = Array.new(n+1) { -1 }
du[1] = @marked[1] ? 0 : -1

dist_down dd
dist_up du, dd

sum = 0
(1..n).each do |i|
	sum += 1 if dd[i] <= d and du[i] <= d
end

# p dd
# p du
p sum

$global_map = Hash.new(0)

def find(res, map, from, deep)
	todo = []
	if deep > 0
		from.each do |from_p|
			map[from_p].each do |dst|

				if !res.include? dst
					res[dst] = nil

					key = "#{from_p}-#{dst}"
					if $global_map[key] < deep
						todo << dst
						$global_map[key] = deep
					end
				end
			end
		end
	end
	todo
end

n, m, d = gets.chomp.split(' ').map(&:to_i)
pp = gets.chomp.split(' ').map(&:to_i)

h = Hash.new { |h,k| h[k] = [] }
(2..n).each do
	a, b = gets.chomp.split(' ').map(&:to_i)
	h[a] << b
	h[b] << a
end

res = nil
pp.each do |p|
	tmp = Hash[p,nil]

	todo = [p]
	d_in = d
	while !todo.empty?
		todo = find(tmp, h, todo, d_in)
		d_in -= 1
	end

	if res.nil?
		res = tmp
	else
		res = Hash[(res.each_key.to_a & tmp.each_key.to_a).map {|x| [x,nil]}]
	end

	break if res.empty?
end

puts res.size


Homer has two friends Alice and Bob. Both of them are string fans. One day, Alice and Bob decide to play a game on a string $$$s = s_1 s_2 \dots s_n$$$ of length $$$n$$$ consisting of lowercase English letters. They move in turns alternatively and Alice makes the first move.In a move, a player must choose an index $$$i$$$ ($$$1 \leq i \leq n$$$) that has not been chosen before, and change $$$s_i$$$ to any other lowercase English letter $$$c$$$ that $$$c \neq s_i$$$.When all indices have been chosen, the game ends. The goal of Alice is to make the final string lexicographically as small as possible, while the goal of Bob is to make the final string lexicographically as large as possible. Both of them are game experts, so they always play games optimally. Homer is not a game expert, so he wonders what the final string will be.A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ if and only if one of the following holds:   $$$a$$$ is a prefix of $$$b$$$, but $$$a \ne b$$$;  in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$b$$$.

Each test contains multiple test cases. The first line contains $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Description of the test cases follows. The only line of each test case contains a single string $$$s$$$ ($$$1 \leq |s| \leq 50$$$) consisting of lowercase English letters.

For each test case, print the final string in a single line.

NoteIn the first test case: Alice makes the first move and must change the only letter to a different one, so she changes it to 'b'.In the second test case: Alice changes the first letter to 'a', then Bob changes the second letter to 'z', Alice changes the third letter to 'a' and then Bob changes the fourth letter to 'z'.In the third test case: Alice changes the first letter to 'b', and then Bob changes the second letter to 'y'.

t = gets.to_i
t.times do
  s = gets.chomp
  s.length.times do |i|
    if i % 2 == 0
      s[i] = s[i] != "a" ? "a" : "b"
    else
      s[i] = s[i] != "z" ? "z" : 'y'
    end
  end
  puts s
end

t = gets.to_i
t.times do
  s = gets.chomp
  s.length.times do |i|
    if i % 2 == 0
      s[i] = s[i] != "a" ? "a" : "b"
    else
      s[i] = s[i] = s[i] != "z" ? "z" : "y"
    end
  end
  puts s
end

t = gets.to_i
t.times do
  s = gets.chomp
  s.length.times do |i|
    if i % 2 == 0
      s[i] = s[i] != "a" ? "a" : "b"
    else
      s[i] = (s[i] = s[i] != "z" ? "z" : "y")
    end
  end
  puts s
end

t = gets.to_i
t.times do
  s = gets.chomp
  s.length.times do |i|
    if i % 2 == 0
      s[i] = s[i] != "a" ? "a" : "b"
    else
      s[i] = (if s[i] != "z" then "z" else "y" end)
    end
  end
  puts s
end

t = gets.to_i
t.times do
  s = gets.chomp
  s.length.times do |i|
    if i % 2 == 0
      s[i] = s[i] != "a" ? "a" : "b"
    else
      s[i] = s[i] != "z" ? "z" : "y"
    end
  end
  puts s
end

t = gets.to_i
t.times do
  s = gets.chomp
  s.length.times do |i|
    if i % 2 == 0
      s[i] = (if s[i] != "a" then "a" else "b" end)
    else
      s[i] = (if s[i] != "z" then "z" else "y" end)
    end
  end
  puts s
end

def test_case
  a=gets.chomp()
  i=0
  len=a.length()
  while i<len
    if i%2==0
      if a[i]=='a'
        a[i]='b'
      else
        a[i]='a'
      end
    else
      if a[i]=='z'
        a[i]='y'
      else
        a[i]='z'
      end
    end
    i+=1
  end
  puts(a)

end

t=gets.chomp()
t=t.to_i
while(t>0)
  test_case
  t-=1
end

gets.strip.to_i.times {
	s = gets.strip
	t = 0
	s.length.times { |i|
		if t == 0
			print s[i] == 'a' ? 'b' : 'a'
		else
			print s[i] == 'z' ? 'y' : 'z'
		end

		t = 1 - t
	}
	print "\n"
}

gets.to_i.times do
  s=gets.chomp.chars
  s.size.times do |i|
    if i.even?
      if s[i]=="a"
        s[i]="b"
      else
        s[i]="a"
      end
    else
      if s[i]=="z"
        s[i]="y"
      else
        s[i]="z"
      end
    end
  end
  puts s.join
end


Guy-Manuel and Thomas have an array $$$a$$$ of $$$n$$$ integers [$$$a_1, a_2, \dots, a_n$$$]. In one step they can add $$$1$$$ to any element of the array. Formally, in one step they can choose any integer index $$$i$$$ ($$$1 \le i \le n$$$) and do $$$a_i := a_i + 1$$$.If either the sum or the product of all elements in the array is equal to zero, Guy-Manuel and Thomas do not mind to do this operation one more time.What is the minimum number of steps they need to do to make both the sum and the product of all elements in the array different from zero? Formally, find the minimum number of steps to make $$$a_1 + a_2 +$$$ $$$\dots$$$ $$$+ a_n \ne 0$$$ and $$$a_1 \cdot a_2 \cdot$$$ $$$\dots$$$ $$$\cdot a_n \ne 0$$$.

Each test contains multiple test cases.  The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^3$$$). The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-100 \le a_i \le 100$$$) — elements of the array .

For each test case, output the minimum number of steps required to make both sum and product of all elements in the array different from zero.

NoteIn the first test case, the sum is $$$0$$$. If we add $$$1$$$ to the first element, the array will be $$$[3,-1,-1]$$$, the sum will be equal to $$$1$$$ and the product will be equal to $$$3$$$.In the second test case, both product and sum are $$$0$$$. If we add $$$1$$$ to the second and the third element, the array will be $$$[-1,1,1,1]$$$, the sum will be equal to $$$2$$$ and the product will be equal to $$$-1$$$. It can be shown that fewer steps can't be enough.In the third test case, both sum and product are non-zero, we don't need to do anything.In the fourth test case, after adding $$$1$$$ twice to the first element the array will be $$$[2,-2,1]$$$, the sum will be $$$1$$$ and the product will be $$$-4$$$.

T = gets.to_i

T.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)

  sum = 0
  ans = 0

  a.each do |v|
    if v == 0
      v = 1
      ans += 1
    end

    sum += v
  end

  if sum == 0
    puts ans + 1
  else
    puts ans
  end
end

n = gets.to_i

n.times do
  gets
  a = gets.split.map &:to_i
  step = a.count(0)
  sum = a.inject(:+) + step
  puts sum == 0 ? step + 1 : step
end

N = gets.to_i
arr = []
N.times do
  _ = gets.to_i
  arr << gets.split.map(&:to_i)
end
arr.each do |ar|
  pre = ar.count(0)
  puts ar.inject(:+) + pre == 0 ? pre + 1 : pre
end

def solve()
    n = gets
    numbers = gets.split(" ").each.map(&:to_i)
    changes = 0
    sum = 0
    numbers.each { |number|
        if number == 0
            changes = changes + 1;
        end
        sum = sum + number
    }

    if (sum + changes == 0)
        changes = changes + 1
    end

	return changes
end

ntest = gets

ntest.to_i.times { |nn|
    puts solve()
}


The developers of Looksery have to write an efficient algorithm that detects faces on a picture. Unfortunately, they are currently busy preparing a contest for you, so you will have to do it for them. In this problem an image is a rectangular table that consists of lowercase Latin letters. A face on the image is a 2 × 2 square, such that from the four letters of this square you can make word "face". You need to write a program that determines the number of faces on the image. The squares that correspond to the faces can overlap.

The first line contains two space-separated integers, n and m (1 ≤ n, m ≤ 50) — the height and the width of the image, respectively. Next n lines define the image. Each line contains m lowercase Latin letters.

In the single line print the number of faces on the image.

NoteIn the first sample the image contains a single face, located in a square with the upper left corner at the second line and the second column:   In the second sample the image also contains exactly one face, its upper left corner is at the second row and the first column.In the third sample two faces are shown:   In the fourth sample the image has no faces on it.

n, m = gets.chomp.split.map(&:to_i);
arr = Array.new;
count = 0;

n.times {|i| arr[i] = gets.chomp.split('')};
(n-1).times do |i|
  (m-1).times do |j|
    count += 1 if (arr[i][j] + arr[i][j + 1] + arr[i + 1][j] + arr[i + 1][j + 1]).chars.sort.join.eql?("acef");
  end
end
p count;

h,w=gets.split.map{|e| e.to_i}
str=[]
h.times{
	str<<gets.chomp
}
ans=0
(h-1).times{|y|
	(w-1).times{|x|
		s=str[y][x]+str[y][x+1]+str[y+1][x]+str[y+1][x+1]
		ans+=1 if s.chars.sort.join=="acef"
	}
}
puts ans

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

n.times do
    grid.push gets.strip
end

answer = 0

0.upto(n - 2) do |i|
    0.upto(m - 2) do |j|
        face = [grid[i][j], grid[i + 1][j], grid[i][j + 1], grid[i + 1][j + 1]]
        answer += 1 if face.sort.join == 'acef'
    end
end

puts answer

n, m = gets.split.map(&:to_i)
s = n.times.map{gets.chomp.chars}
ans = 0
0.upto(n - 2) do |i|
  0.upto(m - 2) do |j|
    c = []
    c << s[i][j] << s[i + 1][j] << s[i][j + 1] << s[i + 1][j + 1]
    ans += 1 if c.include?('f') && c.include?('a') && c.include?('c') && c.include?('e')
  end
end
puts ans

n, m = gets.split.map(&:to_i)
s = n.times.map{gets.chomp}
ans = 0
(n - 1).times do |i|
  (m - 1).times do |j|
    c = s[i][j] + s[i][j + 1] + s[i + 1][j] + s[i + 1][j + 1]
    ans += 1 if c.chars.sort.join == 'acef'
  end
end
puts ans

#!/usr/bin/env ruby

FACE = ['f', 'a', 'c', 'e']
counter = 0

height, width = ARGF.gets.split(' ').map { |s| s.to_i}

image = []
height.times do
    image << ARGF.gets.chomp.split('')
end

image.each_with_index do |col, i|
    if i < height - 1
        image[i].each_with_index do |elem, j|
            if j < width - 1
                face = FACE.clone
                if FACE.include? elem
                    face.delete elem
                    if face.include? image[i][j+1]
                        face.delete image[i][j+1]
                        if face.include? image[i+1][j]
                            face.delete image[i+1][j]
                            if face.include? image[i+1][j+1]
                                counter += 1
                            end
                        end
                    end
                end
            end
        end
    end
end
puts counter


Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow.He knows that the receptionist starts working after ts minutes have passed after midnight and closes after tf minutes have passed after midnight (so that (tf - 1) is the last minute when the receptionist is still working). The receptionist spends exactly t minutes on each person in the queue. If the receptionist would stop working within t minutes, he stops serving visitors (other than the one he already serves). Vasya also knows that exactly n visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately.    "Reception 1" For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them.Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him!

The first line contains three integers: the point of time when the receptionist begins to work ts, the point of time when the receptionist stops working tf and the time the receptionist spends on each visitor t. The second line contains one integer n — the amount of visitors (0 ≤ n ≤ 100 000). The third line contains positive integers in non-decreasing order — the points of time when the visitors arrive to the passport office. All times are set in minutes and do not exceed 1012; it is guaranteed that ts < tf. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist.

Print single non-negative integer — the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them.

NoteIn the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight.In the second example, Vasya has to come before anyone else to be served.

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people =[]
people = gets.split(' ').map { |i| i.to_i } if n > 0

people << finish - t + 1
people.sort!
checkout_free = 0
ans = []
#puts people.inspect
people.each do |p|
     #puts checkout_free -p +1
     #puts start - p + 1
    (ans << [p-1, [checkout_free - p + 1, start - p + 1].max]) if ((checkout_free + t <= finish) && (p > 0))

  checkout_free = [checkout_free + t, p+t, start+t].max
end

#puts ans.inspect
puts ans.sort_by { |a| a[1] }[0][0]

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people =[]
people = gets.split(' ').map { |i| i.to_i } if n > 0

people << finish - t + 1
checkout_free = 0
ans = []
#puts people.inspect
people.each do |p|
     #puts checkout_free -p +1
     #puts start - p + 1
    (ans << [p-1, [checkout_free - p + 1, start - p + 1].max]) if ((checkout_free + t <= finish) && (p > 0))

  checkout_free = [checkout_free + t, p+t, start+t].max
end

#puts ans.inspect
puts ans.sort_by { |a| a[1] }[0][0]

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people = gets.split(' ').map { |i| i.to_i }

cur_time = 0
checkout_free = 0
ans = []
people.each do |p|
  if p > checkout_free
    ans << [p-1, checkout_free - p + 1] if checkout_free + t < finish
  end

  checkout_free = [checkout_free + t, p+t].max
end

#puts ans.inspect
puts ans.sort_by { |a| a[1] }[0][0]

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people = gets.split(' ').map { |i| i.to_i }

cur_time = 0
checkout_free = 0
ans = []
people.each do |p|
  if p > checkout_free
    ans << [p-1, checkout_free - p + 1] if checkout_free + t < finish
  end

  checkout_free = [checkout_free + t, p+t].min
end

#puts ans.inspect
puts ans.sort_by { |a| a[1] }[0][0]

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people = gets.split(' ').map { |i| i.to_i }

checkout_free = 0
ans = []
people.each do |p|
  if p > checkout_free
    ans << [p-1, checkout_free - p + 1] if (checkout_free + t < finish && p > 0)
  end

  checkout_free = [checkout_free + t, p+t].max
end

#puts ans.inspect
puts ans.sort_by { |a| a[1] }[0][0]

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people = gets.split(' ').map { |i| i.to_i }

people << finish - t + 1
checkout_free = 0
ans = []
people.each do |p|
  if p > checkout_free
    (ans << [p-1, [checkout_free - p + 1, start - p + 1].max]) if ((checkout_free + t <= finish) && (p > 0))
  end

  checkout_free = [checkout_free + t, p+t, start+t].max
end

#puts ans.inspect
puts ans.sort_by { |a| a[1] }[0][0]

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people = gets.split(' ').map { |i| i.to_i }

checkout_free = 0
ans = []
people.each do |p|
  if p > checkout_free
    ans << [p-1, [checkout_free - p + 1, start - p + 1].max] if (checkout_free + t < finish && p > 0)
  end

  checkout_free = [checkout_free + t, p+t, start+t].max
end

#puts ans.inspect
puts ans.sort_by { |a| a[1] }[0][0]

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people = gets.split(' ').map { |i| i.to_i }

checkout_free = 0
ans = []
people.each do |p|
  if p > checkout_free
    ans << [p-1, [checkout_free - p + 1, start - p + 1].max] if (checkout_free + t < finish && p > 0)
  end

  checkout_free = [checkout_free + t, p+t].max
end

#puts ans.inspect
puts ans.sort_by { |a| a[1] }[0][0]

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people = gets.split(' ').map { |i| i.to_i }

cur_time = 0
checkout_free = 0
ans = []
people.each do |p|
  if p > checkout_free
    ans << [p-1, p - checkout_free]
  end

  checkout_free = [checkout_free + t, p+t].min
end

#puts ans.inspect
puts ans.sort_by { |a| a[1] }[0][0]

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people = gets.split(' ').map { |i| i.to_i }

cur_time = 0
checkout_free = 0
ans = []
people.each do |p|
  if p > checkout_free
    ans << [p-1, checkout_free - p + 1]
  end

  checkout_free = [checkout_free + t, p+t].min
end

#puts ans.inspect
puts ans.sort_by { |a| a[1] }[0][0]

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people = gets.split(' ').map { |i| i.to_i }

cur_time = 0
checkout_free = 0
ans = []
people.each do |p|
  if p > checkout_free
    ans << [p-1, p - checkout_free]
  end

  checkout_free = [checkout_free + t, p+t].min
end

puts ans.inspect
puts ans.sort_by { |a| a[1] }[0][0]

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people = gets.split(' ').map { |i| i.to_i }

checkout_free = 0
ans = []
people.each do |p|
  if p >= checkout_free
    (ans << [p-1, [checkout_free - p + 1, start - p + 1].max]) if ((checkout_free + t < finish) && (p > 0))
  end

  checkout_free = [checkout_free + t, p+t, start+t].max
  #puts checkout_free
end

#puts ans.inspect
puts ans.sort_by { |a| a[1] }[0][0]

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people = gets.split(' ').map { |i| i.to_i }

cur_time = 0
checkout_free = 0
ans = []
people.each do |p|
  if p > checkout_free
    ans << [p-1, checkout_free - p + 1] if checkout_free - p + 1 + t < finish
  end

  checkout_free = [checkout_free + t, p+t].min
end

#puts ans.inspect
puts ans.sort_by { |a| a[1] }[0][0]

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people = gets.split(' ').map { |i| i.to_i }

cur_time = 0
checkout_free = 0
ans = []
people.each do |p|
 # p start,finish,t, p
  if p > checkout_free
    ans << [p-1, checkout_free - p]
  end
  checkout_free = [checkout_free + t, p+t].max
end

puts ans.sort_by { |a| a[1] }[0][0]

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people = gets.split(' ').map { |i| i.to_i }

checkout_free = 0
ans = []
people.each do |p|
  if p > checkout_free
    (ans << [p-1, [checkout_free - p + 1, start - p + 1].max]) if ((checkout_free + t < finish) && (p >= 0))
  end

  checkout_free = [checkout_free + t, p+t, start+t].max
  #puts checkout_free
end

#puts ans.inspect
puts ans.sort_by { |a| a[1] }[0][0]

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people = gets.split(' ').map { |i| i.to_i }

checkout_free = 0
ans = []
people.each do |p|
  if p > checkout_free
    (ans << [p-1, [checkout_free - p + 1, start - p + 1].max]) if ((checkout_free + t < finish) && (p > 0))
  end

  checkout_free = [checkout_free + t, p+t, start+t].max
  #puts checkout_free
end

#puts ans.inspect
puts ans.sort_by { |a| a[1] }[0][0]

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people = gets.split(' ').map { |i| i.to_i }

cur_time = 0
checkout_free = 0
ans = []
people.each do |p|
 # p start,finish,t, p
  if p > checkout_free
    ans << [p-1, p - checkout_free]
  end
  checkout_free = [checkout_free + t, p+t].max
end

puts ans.sort_by { |a| a[1] }[0][0]

require 'set'

start, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }
n = gets.to_i
queue = []
people = gets.split(' ').map { |i| i.to_i }

checkout_free = 0
ans = []
people.each do |p|
  if p > checkout_free
    (ans << [p-1, [checkout_free - p + 1, start - p + 1].max]) if ((checkout_free + t <= finish) && (p > 0))
  end

  checkout_free = [checkout_free + t, p+t, start+t].max
  #puts checkout_free
end

#puts ans.inspect
puts ans.sort_by { |a| a[1] }[0][0]


Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. . Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≤ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. is the biggest non-negative number d such that d divides bi for every i (1 ≤ i ≤ n).

The first line contains a single integer n (2 ≤ n ≤ 100 000) — length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — elements of sequence A.

Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful.

NoteIn the first example you can simply make one move to obtain sequence [0, 2] with .In the second example the gcd of the sequence is already greater than 1.

n = gets.strip.to_i
data = gets.strip.split.map(&:to_i).push(0)

if data.reduce(:gcd) > 1
    puts "YES", 0
else
    ans = 0
    for i in (0...n)
        next if data[i] % 2 == 0
        ans += 2 - data[i+1] % 2
        data[i+1] = 0
    end
    puts "YES", ans
end

n = gets.strip.to_i
data = gets.strip.split.map(&:to_i).push(0)

if data.reduce(:gcd) > 1
    puts "YES", 0
else
    ans = 0
    puts data
    for i in (0...n)
        next if data[i] % 2 == 0
        ans += 2 - data[i+1] % 2
        data[i+1] = 0
    end
    puts "YES", ans
end


A palindrome is a string $$$t$$$ which reads the same backward as forward (formally, $$$t[i] = t[|t| + 1 - i]$$$ for all $$$i \in [1, |t|]$$$). Here $$$|t|$$$ denotes the length of a string $$$t$$$. For example, the strings 010, 1001 and 0 are palindromes.You have $$$n$$$ binary strings $$$s_1, s_2, \dots, s_n$$$ (each $$$s_i$$$ consists of zeroes and/or ones). You can swap any pair of characters any number of times (possibly, zero). Characters can be either from the same string or from different strings — there are no restrictions.Formally, in one move you:  choose four integer numbers $$$x, a, y, b$$$ such that $$$1 \le x, y \le n$$$ and $$$1 \le a \le |s_x|$$$ and $$$1 \le b \le |s_y|$$$ (where $$$x$$$ and $$$y$$$ are string indices and $$$a$$$ and $$$b$$$ are positions in strings $$$s_x$$$ and $$$s_y$$$ respectively),  swap (exchange) the characters $$$s_x[a]$$$ and $$$s_y[b]$$$. What is the maximum number of strings you can make palindromic simultaneously?

The first line contains single integer $$$Q$$$ ($$$1 \le Q \le 50$$$) — the number of test cases. The first line on each test case contains single integer $$$n$$$ ($$$1 \le n \le 50$$$) — the number of binary strings you have. Next $$$n$$$ lines contains binary strings $$$s_1, s_2, \dots, s_n$$$ — one per line. It's guaranteed that $$$1 \le |s_i| \le 50$$$ and all strings constist of zeroes and/or ones.

Print $$$Q$$$ integers — one per test case. The $$$i$$$-th integer should be the maximum number of palindromic strings you can achieve simultaneously performing zero or more swaps on strings from the $$$i$$$-th test case.

NoteIn the first test case, $$$s_1$$$ is palindrome, so the answer is $$$1$$$.In the second test case you can't make all three strings palindromic at the same time, but you can make any pair of strings palindromic. For example, let's make $$$s_1 = \text{0110}$$$, $$$s_2 = \text{111111}$$$ and $$$s_3 = \text{010000}$$$.In the third test case we can make both strings palindromic. For example, $$$s_1 = \text{11011}$$$ and $$$s_2 = \text{100001}$$$.In the last test case $$$s_2$$$ is palindrome and you can make $$$s_1$$$ palindrome, for example, by swapping $$$s_1[2]$$$ and $$$s_1[3]$$$.

readline.to_i.times do
  e = 1
  z = 0
  n = readline.to_i
  n.times do
    s = readline.chomp
    e = 0 if s.length.odd?
    z += s.count('0')
  end
  p(n-e*(z%2))
end


An accordion is a string (yes, in the real world accordions are musical instruments, but let's forget about it for a while) which can be represented as a concatenation of: an opening bracket (ASCII code $$$091$$$), a colon (ASCII code $$$058$$$), some (possibly zero) vertical line characters (ASCII code $$$124$$$), another colon, and a closing bracket (ASCII code $$$093$$$). The length of the accordion is the number of characters in it.For example, [::], [:||:] and [:|||:] are accordions having length $$$4$$$, $$$6$$$ and $$$7$$$. (:|:), {:||:}, [:], ]:||:[ are not accordions. You are given a string $$$s$$$. You want to transform it into an accordion by removing some (possibly zero) characters from it. Note that you may not insert new characters or reorder existing ones. Is it possible to obtain an accordion by removing characters from $$$s$$$, and if so, what is the maximum possible length of the result?

The only line contains one string $$$s$$$ ($$$1 \le |s| \le 500000$$$). It consists of lowercase Latin letters and characters [, ], : and |.

If it is not possible to obtain an accordion by removing some characters from $$$s$$$, print $$$-1$$$. Otherwise print maximum possible length of the resulting accordion.

def solve(s)
  s.shift until s[0] == '[' or s.size.zero?
  s.reverse!
  s.shift until s[0] == ']' or s.size.zero?
  s.reverse!
  return -1 if s.size < 4
  res = ['[', ':'].map do |c|
    s.index(c)
  end.concat(
    [':', ']'].map do |c|
      s.rindex(c)
    end
  )
  return -1 if res.include?(nil) || res.uniq.size != res.size
  res.each_with_index do |e, i|
    break if i == res.size - 1
    return -1 if e >= res[i+1]
  end
  s[res[1]..res[2]].count('|') + 4
end

puts solve(gets.strip.split(''))

# Input
s = gets

# Processing
o_bracket = nil
o_colon   = nil

s.each_char.with_index do |chr, index|
  case chr
  when "["
    o_bracket = index
  when ":"
    if o_bracket
      o_colon = index
      break
    end
  else
  end
end

c_bracket = nil
c_colon   = nil
s.each_char.reverse_each.with_index do |chr, index|
  case chr
  when "]"
    c_bracket = s.length - 1 - index
  when ":"
    if c_bracket
      c_colon = s.length - 1 - index
      break
    end
  else
  end
end

unless o_colon && c_colon && o_colon < c_colon
  puts -1
  exit
end

inner = s.slice(o_colon + 1, c_colon - o_colon - 1)
result = 4 + inner.count("|")

# Output
puts result

DBG = !true
s = gets.chomp
puts "s: #{s}" if DBG
n = s.size
leftpar = -1
for i in 0...n
  if s[i] == '['
    leftpar = i
    break
  end
end
if leftpar == -1
  puts "no leftpar" if DBG
  puts "-1"
  exit
end

leftcol = -1
for i in (leftpar+1)...n
  if s[i] == ':'
    leftcol = i
    break
  end
end
if leftcol == -1
  puts "no leftcol" if DBG
  puts "-1"
  exit
else
  puts "leftcol #{leftcol}" if DBG
end

rightpar = -1
(n-1).downto(leftcol+2) do |i|
  if s[i] == ']'
    rightpar = i
    break
  end
end
if rightpar == -1
  puts "no rightpar" if DBG
  puts "-1"
  exit
end

rightcol = -1
(rightpar-1).downto(leftcol+1) do |i|
  if s[i] == ':'
    rightcol = i
    break
  end
end
if rightcol == -1
  puts "no rightcol" if DBG
  puts "-1"
  exit
end

cnt = 0
for i in (leftcol+1)..(rightcol-1)
  cnt += 1 if s[i] == '|'
end
puts "#{cnt+4}"

# revisar que si haya al menos un acordeon
  # contar apertura [:
  # contar numero de pipes despúes
  # contar ultima :] (desde atrás maybe)

def accordion_length(s)
  open_bracket = false
  close_bracket = false
  first_colon_at = nil
  second_colon_at = nil
  pipe_counter = 0

  s.split('').each_with_index do |char, index|
    if char == '[' && !open_bracket
      open_bracket = true
      next
    end

    if char == ':' && open_bracket
      first_colon_at = index
      break
    end
  end

  return -1 unless first_colon_at

  (s.length-1).downto(first_colon_at + 1).each do |i|
    if s[i] == ']' && !close_bracket
      close_bracket = true
      next
    end

    if s[i] == ':' && close_bracket
      second_colon_at = i
      break
    end
  end

  return -1 unless second_colon_at

  s[first_colon_at..second_colon_at].split('').each do |char|
    pipe_counter += 1 and next if char == '|'
  end

  pipe_counter + 4
end

s = gets.rstrip

result = accordion_length s
puts result

def solve(s)
  return -1 if s.size < 4
  res = ['[', ':'].map do |c|
    s.index(c)
  end.concat(
    [':', ']'].map do |c|
      s.rindex(c)
    end
  )
  return -1 if res.include?(nil) || res.uniq.size != res.size
  res.each_with_index do |e, i|
    break if i == res.size - 1
    return -1 if e >= res[i+1]
  end
  s[res[1]..res[2]].count('|') + 4
end

puts solve(gets.strip.split(''))

DBG = !true
s = gets.chomp
puts "s: #{s}" if DBG
n = s.size
leftpar = -1
for i in 0...n
  if s[i] == '['
    leftpar = i
    break
  end
end
if leftpar == -1
  puts "no leftpar" if DBG
  puts "-1"
  exit
end

leftcol = -1
for i in (leftpar+1)...n
  if s[i] == ':'
    leftcol = i
    break
  end
end
if leftcol == -1
  puts "no leftcol" if DBG
  puts "-1"
  exit
else
  puts "leftcol #{leftcol}" if DBG
end

rightpar = -1
(n-1).downto(leftcol+2) do |i|
  if s[i] == ']'
    rightpar = i
    break
  end
end
if rightpar == -1
  puts "no rightpar" if DBG
  puts "-1"
  exit
end

rightcol = -1
(rightpar-1).downto(rightcol+1) do |i|
  if s[i] == ':'
    rightcol = i
    break
  end
end
if rightcol == -1
  puts "no rightcol" if DBG
  puts "-1"
  exit
end

cnt = 0
for i in (leftcol+1)..(rightcol-1)
  cnt += 1 if s[i] == '|'
end
puts "#{cnt+4}"

DBG = !true
s = gets.chomp
puts "s: #{s}" if DBG
n = s.size
leftpar = -1
for i in 0...n
  if s[i] == '['
    leftpar = i
    break
  end
end
if leftpar == -1
  puts "no leftpar" if DBG
  puts "-1"
  exit
end

leftcol = -1
for i in (leftpar+1)...n
  if s[i] == ':'
    leftcol = i
    break
  end
end
if leftcol == -1
  puts "no leftcol" if DBG
  puts "-1"
  exit
else
  puts "leftcol #{leftcol}" if DBG
end

rightpar = -1
(n-1).downto(leftcol+2) do |i|
  if s[i] == ']'
    rightpar = i
    break
  end
end
if rightpar == -1
  puts "no rightpar" if DBG
  puts "-1"
  exit
end

rightcol = -1
(n-1).downto(rightcol+1) do |i|
  if s[i] == ':'
    rightcol = i
    break
  end
end
if rightcol == -1
  puts "no rightcol" if DBG
  puts "-1"
  exit
end

cnt = 0
for i in (leftcol+1)..(rightcol-1)
  cnt += 1 if s[i] == '|'
end
puts "#{cnt+4}"

def accordion_length(s)
  opened = false
  colon_counter = 0
  accordion_length = 0
  accordion = false

  s.split('').each do |char|
    if char == '[' && !opened
      accordion_length += 1
      opened = true
      next
    end

    if char == ':' && opened && colon_counter < 2
      accordion_length += 1
      colon_counter += 1
      next
    end

    if char == '|' && colon_counter == 1
      accordion_length += 1
      next
    end

    if char == ']' && colon_counter == 2
      accordion_length += 1
      accordion = true
      break
    end
  end

  return -1 unless accordion
  accordion_length
end

s = gets.rstrip

result = accordion_length s
puts result

def accordion_length(s)
  opened = false
  colon_counter = 0
  accordion_length = 0
  accordion = false

  s.split('').each do |char|
    if char == '[' && !opened
      accordion_length += 1
      opened = true
      next
    end

    if char == ':' && opened && colon_counter < 2
      accordion_length += 1
      colon_counter += 1
      next
    end

    if char == '|' && colon_counter == 1
      accordion_length += 1
      next
    end

    if char == ']' && colon_counter == 2
      accordion_length += 1
      accordion = true
    end
  end

  return -1 unless accordion
  accordion_length
end

s = gets.rstrip

result = accordion_length s
puts result


Let's define a number ebne (even but not even) if and only if its sum of digits is divisible by $$$2$$$ but the number itself is not divisible by $$$2$$$. For example, $$$13$$$, $$$1227$$$, $$$185217$$$ are ebne numbers, while $$$12$$$, $$$2$$$, $$$177013$$$, $$$265918$$$ are not. If you're still unsure what ebne numbers are, you can look at the sample notes for more clarification.You are given a non-negative integer $$$s$$$, consisting of $$$n$$$ digits. You can delete some digits (they are not necessary consecutive/successive) to make the given number ebne. You cannot change the order of the digits, that is, after deleting the digits the remaining digits collapse. The resulting number shouldn't contain leading zeros. You can delete any number of digits between $$$0$$$ (do not delete any digits at all) and $$$n-1$$$.For example, if you are given $$$s=$$$222373204424185217171912 then one of possible ways to make it ebne is: 222373204424185217171912 $$$\rightarrow$$$ 2237344218521717191. The sum of digits of 2237344218521717191 is equal to $$$70$$$ and is divisible by $$$2$$$, but number itself is not divisible by $$$2$$$: it means that the resulting number is ebne.Find any resulting number that is ebne. If it's impossible to create an ebne number from the given number report about it.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 3000$$$)  — the number of digits in the original number. The second line of each test case contains a non-negative integer number $$$s$$$, consisting of $$$n$$$ digits. It is guaranteed that $$$s$$$ does not contain leading zeros and the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$.

For each test case given in the input print the answer in the following format:  If it is impossible to create an ebne number, print "-1" (without quotes); Otherwise, print the resulting number after deleting some, possibly zero, but not all digits. This number should be ebne. If there are multiple answers, you can print any of them. Note that answers with leading zeros or empty strings are not accepted. It's not necessary to minimize or maximize the number of deleted digits.

NoteIn the first test case of the example, $$$1227$$$ is already an ebne number (as $$$1 + 2 + 2 + 7 = 12$$$, $$$12$$$ is divisible by $$$2$$$, while in the same time, $$$1227$$$ is not divisible by $$$2$$$) so we don't need to delete any digits. Answers such as $$$127$$$ and $$$17$$$ will also be accepted.In the second test case of the example, it is clearly impossible to create an ebne number from the given number.In the third test case of the example, there are many ebne numbers we can obtain by deleting, for example, $$$1$$$ digit such as $$$17703$$$, $$$77013$$$ or $$$17013$$$. Answers such as $$$1701$$$ or $$$770$$$ will not be accepted as they are not ebne numbers. Answer $$$013$$$ will not be accepted as it contains leading zeroes.Explanation: $$$1 + 7 + 7 + 0 + 3 = 18$$$. As $$$18$$$ is divisible by $$$2$$$ while $$$17703$$$ is not divisible by $$$2$$$, we can see that $$$17703$$$ is an ebne number. Same with $$$77013$$$ and $$$17013$$$; $$$1 + 7 + 0 + 1 = 9$$$. Because $$$9$$$ is not divisible by $$$2$$$, $$$1701$$$ is not an ebne number; $$$7 + 7 + 0 = 14$$$. This time, $$$14$$$ is divisible by $$$2$$$ but $$$770$$$ is also divisible by $$$2$$$, therefore, $$$770$$$ is not an ebne number.In the last test case of the example, one of many other possible answers is given. Another possible answer is: 222373204424185217171912 $$$\rightarrow$$$ 22237320442418521717191 (delete the last digit).

T = gets.to_i

T.times do
  n = gets.to_i
  s = gets.chomp.chars.map(&:to_i)

  odds = []

  s.each do |d|
    next if d.even?

    odds << d
  end

  if odds.size <= 1
    puts -1
  else
    puts odds.take(2).join
  end
end

# coding: utf-8

# Even but not even
# Sum(list_of_digits).even? == true
# original_number.even? == false
t = gets.to_i
t.times {
    n = gets.to_i
    ar = gets.chomp.split(//).map(&:to_i)
    ar.delete_if { |i| i.even? }

    if ar.length <= 1
        puts "-1"
    else
        if ar.length.odd?
            ar.pop
        end

        puts ar.join('')
    end

}

t = gets.to_i
odd = ["1","3","5","7","9"]
even = ["2","4","6","8","0"]

t.times{
  n = gets.to_i
  s = gets.chomp
  sc = s.chars.map(&:to_i)
  sc.pop while sc != [] && sc.last.even?
  if sc == []
    p -1
    #puts "because sc==[]"
    next
  end
  n_odd = sc.count{|i|i.odd?}
  #puts "s: #{s}, n_odd: #{n_odd}"
  flag = false
  if n_odd.odd?
    #puts "n_odd is odd"
    [*0..sc.length-1].reverse.each{|i|
      #puts "loop yeah!!"
      if sc[i].odd? && flag
        sc.delete_at(i)
        break
      end
      flag = true if sc[i].odd?
    }
  end
  n_odd = sc.count{|i|i.odd?}
  if n_odd.odd?
    p -1
    #puts "because n_odd.odd?"
    next
  end
  if sc == [0]
    p -1
    #puts "because sc==[0]"
  else
    puts sc.join
  end
}

# coding: utf-8

# Even but not even
# Sum(list_of_digits).even? == true
# original_number.even? == false
t = gets.to_i
t.times {
    n = gets.to_i
    odds = []
    gets.chomp.split(//).each { |c|
        i = c.to_i
        if i.odd?
            odds << i
        end
    }
    if odds.length <= 1
        puts "-1"
    else
        puts odds.join('')
    end
}

# coding: utf-8

# Even but not even
# Sum(list_of_digits).even? == true
# original_number.even? == false
t = gets.to_i
t.times {
    n = gets.to_i
    cr = Array.new(10+1) { 0 }
    gets.chomp.split(//).each { |c|
        i = c.to_i
        cr[i] += 1
    }
    odds_unique = {}
    for ci in 1..10
        if ci.odd?
            if cr[ci] > 0
                odds_unique[ci] = true
            end
        end
    end

    if odds_unique.length <= 1
        puts "-1"
    else
        puts (odds_unique.keys[0..1]).join('')
    end
}


Initially there was an array $$$a$$$ consisting of $$$n$$$ integers. Positions in it are numbered from $$$1$$$ to $$$n$$$.Exactly $$$q$$$ queries were performed on the array. During the $$$i$$$-th query some segment $$$(l_i, r_i)$$$ $$$(1 \le l_i \le r_i \le n)$$$ was selected and values of elements on positions from $$$l_i$$$ to $$$r_i$$$ inclusive got changed to $$$i$$$. The order of the queries couldn't be changed and all $$$q$$$ queries were applied. It is also known that every position from $$$1$$$ to $$$n$$$ got covered by at least one segment.We could have offered you the problem about checking if some given array (consisting of $$$n$$$ integers with values from $$$1$$$ to $$$q$$$) can be obtained by the aforementioned queries. However, we decided that it will come too easy for you.So the enhancement we introduced to it is the following. Some set of positions (possibly empty) in this array is selected and values of elements on these positions are set to $$$0$$$.Your task is to check if this array can be obtained by the aforementioned queries. Also if it can be obtained then restore this array.If there are multiple possible arrays then print any of them.

The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 \le n, q \le 2 \cdot 10^5$$$) — the number of elements of the array and the number of queries perfomed on it. The second line contains $$$n$$$ integer numbers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le q$$$) — the resulting array. If element at some position $$$j$$$ is equal to $$$0$$$ then the value of element at this position can be any integer from $$$1$$$ to $$$q$$$.

Print "YES" if the array $$$a$$$ can be obtained by performing $$$q$$$ queries. Segments $$$(l_i, r_i)$$$ $$$(1 \le l_i \le r_i \le n)$$$ are chosen separately for each query. Every position from $$$1$$$ to $$$n$$$ should be covered by at least one segment.  Otherwise print "NO". If some array can be obtained then print $$$n$$$ integers on the second line — the $$$i$$$-th number should be equal to the $$$i$$$-th element of the resulting array and should have value from $$$1$$$ to $$$q$$$. This array should be obtainable by performing exactly $$$q$$$ queries. If there are multiple possible arrays then print any of them.

NoteIn the first example you can also replace $$$0$$$ with $$$1$$$ but not with $$$3$$$.In the second example it doesn't really matter what segments to choose until query $$$10$$$ when the segment is $$$(1, 3)$$$.The third example showcases the fact that the order of queries can't be changed, you can't firstly set $$$(1, 3)$$$ to $$$6$$$ and after that change $$$(2, 2)$$$ to $$$5$$$. The segment of $$$5$$$ should be applied before segment of $$$6$$$.There is a lot of correct resulting arrays for the fourth example.

N, Q = gets.split.map(&:to_i)
as = gets.split.map(&:to_i)
exist_max = as.include?(Q)
used = []
stack = []

N.times do |i|
    if as[i] == 0
        if exist_max
            as[i] = i==0 ? 1 : as[i-1]
        else
            as[i] = Q
            exist_max = true
        end
        next
    end

    if used[as[i]]
        puts 'NO'
        exit
    end

    while stack.length > 0 && stack[-1] > as[i]
        j = stack.pop
        used[j] = true
    end
    stack.push(as[i]) if stack[-1] != as[i]
end

unless exist_max
    puts 'NO'
    exit
end

puts 'YES'
puts as.join(' ')

N, Q = gets.split.map(&:to_i)
as = gets.split.map(&:to_i)
exist_max = as.include?(Q)
found = []

N.times do |i|
    if as[i] == 0
        if exist_max
            as[i] = i==0 ? 1 : as[i-1]
        else
            as[i] = Q
            exist_max = true
        end
        next
    end
    if i > 0 && as[i-1] < as[i]
        if found[as[i]]
            puts 'NO'
            exit
        end
    end
    found[as[i]] = true
end

unless exist_max
    puts 'NO'
    exit
end

puts 'YES'
puts as.join(' ')


Levko loves permutations very much. A permutation of length n is a sequence of distinct positive integers, each is at most n.Let’s assume that value gcd(a, b) shows the greatest common divisor of numbers a and b. Levko assumes that element pi of permutation p1, p2, ... , pn is good if gcd(i, pi) > 1. Levko considers a permutation beautiful, if it has exactly k good elements. Unfortunately, he doesn’t know any beautiful permutation. Your task is to help him to find at least one of them.

The single line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n).

In a single line print either any beautiful permutation or -1, if such permutation doesn’t exist. If there are multiple suitable permutations, you are allowed to print any of them.

NoteIn the first sample elements 4 and 3 are good because gcd(2, 4) = 2 > 1 and gcd(3, 3) = 3 > 1. Elements 2 and 1 are not good because gcd(1, 2) = 1 and gcd(4, 1) = 1. As there are exactly 2 good elements, the permutation is beautiful.The second sample has no beautiful permutations.

n,k=gets.split.map(&:to_i)
puts (n==k ? -1:(2..n).to_a.insert(n-k-1,1).join(' '))

n, k = gets.split.map{|s| s.to_i}

if n==k
	puts -1
	exit
end

seq = Array.new(n+1) { |i| i }
primes = [2]
nn = 2
dif = n-1-k
while dif > 1
	seq[nn], seq[nn+1] = seq[nn+1], seq[nn]
	nn+=2
	dif -= 2
end
if dif == 1
	seq[1] = nn
	seq[nn] = 1
end
seq[1..-1].each{|i| print "#{i} "}

n,k=gets.chomp.split.map(&:to_i)
if n==k
	puts "-1";exit
end
print n-k
(n-k-1).times{|i| print " #{i+1}"}
for i in n-k+1..n
	print " #{i}"
end
puts

class B
  def initialize
    n, k = gets.chomp.split(' ').map(&:to_i)
    count = n-1
    index = 1

    answer = (1..n).to_a

    if n <= k
      puts -1
      exit
    end

    if ( (count.odd? && k.even?) || (count.even? && k.odd? ) )
      answer[0], answer[1] = answer[1], answer[0]
      count -= 1
      index = 2
    end

    while count != k && index < n
      answer[index], answer[index+1] = answer[index+1], answer[index]
      count -= 2
      index += 2
    end

    if count == k
      puts answer.join(' ')
    else
      puts -1
    end
  end
end

b = B.new

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

if n == k
  puts "-1"
  exit
end

arr = (1..n).to_a
ck = n - 1
arr.each_with_index { |e , i|
  break if ck == k
  arr[0], arr[n - i - 1] = arr[n - i - 1], arr[0]
  ck -= 1
}

puts arr.join(' ')

n,k = gets.split(" ").map(&:to_i)
bad = n - k
perm = (2..bad).to_a
perm.push(1)
perm.push(*((bad+1)..(bad+k)))
if n == k
	puts "-1"
else
puts perm.join(" ")
end

n, k = gets.chomp.split(' ').collect {|x| x.to_i}

q = n-k

if q < 1
  puts -1
elsif q == 1
  puts (1..n).to_a.join ' '
else
  arr = (1..(q-1)).to_a
  arr = [q] + arr
  arr = arr + ((q+1)..n).to_a
  puts arr.join ' '
end

line = gets.chomp.split(" ").map { |e| e.to_i }
n = line[0]
k = line[1]

if n == k
  puts "-1"
elsif n == k + 1
  for i in 1..n
    print(i.to_s + " ")
  end
  puts
else
  print((k+2).to_s + " ")
  for i in 2..n
    if i != k+2
      print(i.to_s + " ")
    end
  end
  print("1\n")
end

class B
  def initialize
    n, k = gets.chomp.split(' ').map(&:to_i)
    count = n-1

    answer = (1..n).to_a

    if n <= k
      puts -1
      exit
    end

    if k.even?
      answer[0], answer[1] = answer[1], answer[0]
      count -= 1
    end

    index = 2
    while count != k && index < n
      answer[index], answer[index+1] = answer[index+1], answer[index]
      count -= 2
      index += 2
    end

    if count == k
      puts answer.join(' ')
    else
      puts -1
    end
  end
end

b = B.new

class B
  def initialize
    n, k = gets.chomp.split(' ').map(&:to_i)
    count = n-1
    index = 1

    answer = (1..n).to_a

    if n <= k
      puts -1
      exit
    end

    if !k.zero? && ( (count.odd? && k.even?) || (count.even? && k.odd? ) )
      answer[0], answer[1] = answer[1], answer[0]
      count -= 1
      index = 2
    end

    while count != k && index < n
      answer[index], answer[index+1] = answer[index+1], answer[index]
      count -= 2
      index += 2
    end

    if count == k
      puts answer.join(' ')
    else
      puts -1
    end
  end
end

b = B.new

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

if n == k
  puts "-1"
  exit
end

arr = (1..n).to_a
ck = n - 1
arr.each_with_index { |e , i|
  break if ck == k
  i += 1 if i.odd?
  arr[i], arr[n - 1] = arr[n - 1], arr[i]
  ck -= 1
}

puts arr.join(' ')

n,k = gets.split(" ").map(&:to_i)
bad = n - k
perm = (2..bad).to_a
perm.push(1)
perm.push(*((bad+1)..(bad+k)))
if n == 1
	puts "-1"
else
puts perm.join(" ")
end

n,k = gets.split(" ").map(&:to_i)
bad = n - k
perm = (2..bad).to_a
perm.push(1)
perm.push(*((bad+1)..(bad+k)))
if n == 1 && k == 1
	puts "-1"
else
puts perm.join(" ")
end

n,k = gets.split(" ").map(&:to_i)
bad = n - k
perm = (1..bad).to_a
perm.push(*((bad+2)..(bad+k)))
perm.push(bad+1)
if n == 1
	puts "-1"
else
puts perm.join(" ")
end


You play your favourite game yet another time. You chose the character you didn't play before. It has $$$str$$$ points of strength and $$$int$$$ points of intelligence. Also, at start, the character has $$$exp$$$ free experience points you can invest either in strength or in intelligence (by investing one point you can either raise strength by $$$1$$$ or raise intelligence by $$$1$$$).Since you'd like to make some fun you want to create a jock character, so it has more strength than intelligence points (resulting strength is strictly greater than the resulting intelligence).Calculate the number of different character builds you can create (for the purpose of replayability) if you must invest all free points. Two character builds are different if their strength and/or intellect are different.

The first line contains the single integer $$$T$$$ ($$$1 \le T \le 100$$$) — the number of queries. Next $$$T$$$ lines contain descriptions of queries — one per line. This line contains three integers $$$str$$$, $$$int$$$ and $$$exp$$$ ($$$1 \le str, int \le 10^8$$$, $$$0 \le exp \le 10^8$$$) — the initial strength and intelligence of the character and the number of free points, respectively.

Print $$$T$$$ integers — one per query. For each query print the number of different character builds you can create.

NoteIn the first query there are only three appropriate character builds: $$$(str = 7, int = 5)$$$, $$$(8, 4)$$$ and $$$(9, 3)$$$. All other builds are either too smart or don't use all free points.In the second query there is only one possible build: $$$(2, 1)$$$.In the third query there are two appropriate builds: $$$(7, 6)$$$, $$$(8, 5)$$$.In the fourth query all builds have too much brains.

gets.to_i.times do
    s,t,e = gets.split.map(&:to_i)
    if s+e <= t
        puts 0
    else
        b = (t+e-s)/2
        puts e-(b > -2 ? b : -1)
    end
end

t = STDIN.readline.to_i

t.times {
    str, int, exp = STDIN.readline.split.map &:to_i
    if str+exp <= int
        p 0
        next
    end

    if (d = int-str) > 0
        str = int
        exp -= d
    end

    int += exp
    i = (0..exp).bsearch do |e|
        str+e > int-e
    end
    p exp-i+1
}

tt = gets.chomp.to_i
tt.times.map do
  str, int, exp = gets.split.map(&:to_i)
  diff = str - int

  cnt = 0
  if diff<0
    exp += diff
    if exp<=0
    else
      cnt+=(exp+1)/2
    end
  elsif diff==0
    cnt+=(exp+1)/2
  else
    if exp < diff
      cnt+=exp+1
    else
      cnt+=(exp+diff+1)/2
    end
  end

  puts cnt
end

t = gets.chomp.to_i

t.times do |i|
  str, int, exp = gets.chomp.split.map(&:to_i)
  diff = str + exp - int
  if diff <= 0
    puts 0
  else
    puts [(diff+1)/2, exp+1].min
  end
end

gets.to_i.times do
    s,t,e = gets.split.map(&:to_i)
    if s+e <= t
        puts 0
    else
        puts s+e-(s+(t+e-s)/2)
    end
end

gets.to_i.times do
    s,t,e = gets.split.map(&:to_i)
    if s+e <= t
        puts 0
    else
        b = (t+e-s)/2
        if e > 0
            puts e-(b > 0 ? b : 0)
        else
            puts 1
        end
    end
end

t = gets.chomp.to_i

t.times do |i|
  str, int, exp = gets.chomp.split.map(&:to_i)
  # str + a > int + b
  # a + b = exp
  # str + exp > int + 2b
  # 2b <= str + exp - int + 1
  # b >= 0
  diff = [str + exp - int + 1, 0].max
  puts diff/2
end

t = gets.chomp.to_i

t.times do |i|
  str, int, exp = gets.chomp.split.map(&:to_i)
  cut = (exp+int-str)/2 + 1
  puts [exp-cut+1, 0].max
end

t = gets.chomp.to_i

t.times do |i|
  str, int, exp = gets.chomp.split.map(&:to_i)
  diff = str + exp - int
  if diff <= 0
    puts 0
  else
    puts (diff+1)/2
  end
end


There were $$$n$$$ types of swords in the theater basement which had been used during the plays. Moreover there were exactly $$$x$$$ swords of each type. $$$y$$$ people have broken into the theater basement and each of them has taken exactly $$$z$$$ swords of some single type. Note that different people might have taken different types of swords. Note that the values $$$x, y$$$ and $$$z$$$ are unknown for you.The next morning the director of the theater discovers the loss. He counts all swords — exactly $$$a_i$$$ swords of the $$$i$$$-th type are left untouched.The director has no clue about the initial number of swords of each type in the basement, the number of people who have broken into the basement and how many swords each of them have taken.For example, if $$$n=3$$$, $$$a = [3, 12, 6]$$$ then one of the possible situations is $$$x=12$$$, $$$y=5$$$ and $$$z=3$$$. Then the first three people took swords of the first type and the other two people took swords of the third type. Note that you don't know values $$$x, y$$$ and $$$z$$$ beforehand but know values of $$$n$$$ and $$$a$$$.Thus he seeks for your help. Determine the minimum number of people $$$y$$$, which could have broken into the theater basement, and the number of swords $$$z$$$ each of them has taken.

The first line of the input contains one integer $$$n$$$ $$$(2 \le n \le 2 \cdot 10^{5})$$$ — the number of types of swords. The second line of the input contains the sequence $$$a_1, a_2, \dots, a_n$$$ $$$(0 \le a_i \le 10^{9})$$$, where $$$a_i$$$ equals to the number of swords of the $$$i$$$-th type, which have remained in the basement after the theft. It is guaranteed that there exists at least one such pair of indices $$$(j, k)$$$ that $$$a_j \neq a_k$$$.

Print two integers $$$y$$$ and $$$z$$$ — the minimum number of people which could have broken into the basement and the number of swords each of them has taken.

NoteIn the first example the minimum value of $$$y$$$ equals to $$$5$$$, i.e. the minimum number of people who could have broken into the basement, is $$$5$$$. Each of them has taken $$$3$$$ swords: three of them have taken $$$3$$$ swords of the first type, and two others have taken $$$3$$$ swords of the third type.In the second example the minimum value of $$$y$$$ is $$$1$$$, i.e. the minimum number of people who could have broken into the basement, equals to $$$1$$$. He has taken $$$7$$$ swords of the first type.

n = gets.chomp.to_i
arr = gets.chomp.split(' ').map(&:to_i)
mx = arr.max
arr = arr.map{|el| mx - el}
m = arr.reduce(arr[0].gcd(arr[1]), :gcd)
res = 0
arr.each do |el|
  res += el / m
end
puts [res, m].join(' ')

MOD=10**9+7
cnt=0; sum=0; prev=nil; can=true; h=Hash.new(0)
def gs() gets.chomp end
def gi() gets.to_i end
def gsmi() gets.chomp.split.map(&:to_i) end

def desc(ar) ar.sort!{|x,y|y<=>x} end
def min(a,b) a<=b ? a : b end
def max(a,b) a>=b ? a : b end
def sum(ar) ar.inject(:+) end

#def bs(ar,eq,n) en=(eq==1 ? :>= : :>); ar.bsearch_index{|x|x.send(en,n)} end
def C(a,b) b==0||a==b ? 1 : (b=a-b if a/2<b;(a-b+1..a).inject(:*)/(1..b).inject(:*)) end

def puts_yesno(can) puts(can ? 'Yes' : 'No') end
def putsend(s) puts s; exit end
def debug(k,v) puts "#{k}:#{v}" end
########### ( ˘ω˘ )ｽﾔｧ… ###########
n=gi
a=desc gsmi
max=a.shift
putsend("0 0") if max==0
a.map!{|i|max-i}

z=a[-1]
putsend("0 0") if z==0
0.upto(a.size-1-1) do |i|
  z = z.gcd(a[i])
end
0.upto(a.size-1) do |i|
  cnt+=a[i]/z
end
puts "#{cnt} #{z}"


Bessie the cow has just intercepted a text that Farmer John sent to Burger Queen! However, Bessie is sure that there is a secret message hidden inside.The text is a string $$$s$$$ of lowercase Latin letters. She considers a string $$$t$$$ as hidden in string $$$s$$$ if $$$t$$$ exists as a subsequence of $$$s$$$ whose indices form an arithmetic progression. For example, the string aab is hidden in string aaabb because it occurs at indices $$$1$$$, $$$3$$$, and $$$5$$$, which form an arithmetic progression with a common difference of $$$2$$$. Bessie thinks that any hidden string that occurs the most times is the secret message. Two occurrences of a subsequence of $$$S$$$ are distinct if the sets of indices are different. Help her find the number of occurrences of the secret message!For example, in the string aaabb, a is hidden $$$3$$$ times, b is hidden $$$2$$$ times, ab is hidden $$$6$$$ times, aa is hidden $$$3$$$ times, bb is hidden $$$1$$$ time, aab is hidden $$$2$$$ times, aaa is hidden $$$1$$$ time, abb is hidden $$$1$$$ time, aaab is hidden $$$1$$$ time, aabb is hidden $$$1$$$ time, and aaabb is hidden $$$1$$$ time. The number of occurrences of the secret message is $$$6$$$.

The first line contains a string $$$s$$$ of lowercase Latin letters ($$$1 \le |s| \le 10^5$$$) — the text that Bessie intercepted.

Output a single integer  — the number of occurrences of the secret message.

NoteIn the first example, these are all the hidden strings and their indice sets:   a occurs at $$$(1)$$$, $$$(2)$$$, $$$(3)$$$  b occurs at $$$(4)$$$, $$$(5)$$$  ab occurs at $$$(1,4)$$$, $$$(1,5)$$$, $$$(2,4)$$$, $$$(2,5)$$$, $$$(3,4)$$$, $$$(3,5)$$$  aa occurs at $$$(1,2)$$$, $$$(1,3)$$$, $$$(2,3)$$$  bb occurs at $$$(4,5)$$$  aab occurs at $$$(1,3,5)$$$, $$$(2,3,4)$$$  aaa occurs at $$$(1,2,3)$$$  abb occurs at $$$(3,4,5)$$$  aaab occurs at $$$(1,2,3,4)$$$  aabb occurs at $$$(2,3,4,5)$$$  aaabb occurs at $$$(1,2,3,4,5)$$$  Note that all the sets of indices are arithmetic progressions.In the second example, no hidden string occurs more than once.In the third example, the hidden string is the letter l.

S = gets.chomp

counter = Hash.new(0)
sum = Hash.new(0)

S.each_char do |s|
  counter.each do |c, cnt|
    sum[c + s] += cnt
  end

  counter[s] += 1
end

puts [counter.values.max, sum.values.max || 0].max


Among other things, Bob is keen on photography. Especially he likes to take pictures of sportsmen. That was the reason why he placed himself in position x0 of a long straight racetrack and got ready to take pictures. But the problem was that not all the runners passed him. The total amount of sportsmen, training at that racetrack, equals n. And each of them regularly runs distances within a particular segment of the racetrack, which is the same for each sportsman. For example, the first sportsman runs from position a1 to position b1, the second — from a2 to b2What is the minimum distance that Bob should move to have a chance to take pictures of each sportsman? Bob can take a picture of a sportsman, if he stands within the segment that this sportsman covers on the racetrack.

The first line of the input file contains integers n and x0 (1 ≤ n ≤ 100; 0 ≤ x0 ≤ 1000). The following n lines contain pairs of integers ai, bi (0 ≤ ai, bi ≤ 1000; ai ≠ bi).

Output the required minimum distance in the same units as the positions on the racetrack. If there is no such a position, output -1.

ns,ms = gets.chomp.split(/ /)
n = ns.to_i
m = ms.to_i

mx1 = -1
mx2 = 1001
for i in 0..n-1
	x1s, x2s = gets.chomp.split(/ /)
	x1 = x1s.to_i
	x2 = x2s.to_i
	ax = [x1,x2].max
	ix = [x1,x2].min
	mx1 = [mx1, ix].max
	mx2 = [mx2, ax].min
end
if mx1 > mx2
	puts "-1"
else
	ret = 0
	if m < mx1 || m > mx2
		ret= [(mx1-m).abs, (mx2-m).abs].min
	end
	puts ret
end

class Range
	def ^(other)
		([first, other.first].max .. [last, other.last].min)
	end
end

n, x0 = gets.split.map{|i| i.to_i}
r = []
n.times do
	a, b = gets.split.map{|i| i.to_i}
	a, b = b, a if a > b
	r <<= (a .. b)
end
r = r.inject{|i, j| i ^ j}

unless r.first <= r.last then
	p -1
else
	p r.include?(x0) ? 0 : [(r.first - x0).abs, (r.last - x0).abs].min
end

n,x=gets.split.map(&:to_i)
sum=Array.new(1002,0)
tsum=Array.new(1002,0)
n.times{
        a,b=gets.split.map(&:to_i).sort
        b+=1
        tsum[a]+=1
        tsum[b]-=1
}
sum[0]=tsum[0]
ans=[]
ans.push(x) if sum[0]==n
1.upto(1001){|i|
        sum[i]=sum[i-1]+tsum[i]
        if sum[i]==n then
                ans.push((x-i).abs)
        end
}
ans.push(-1) if ans.size()==0

p ans.min

#Inf = Float::Infinity
#
#geti
#getia(delimiter = " ")
#
#Object # dcopy
#
#String # to_a
#   "abc".to_a #=> ["a","b","c"]
#String # to_ia
#   "1 2 3".to_ia #=> [1,2,3]
#

include Math
require "prime"
require "bigdecimal"
require "bigdecimal/math"
require "matrix"
require "pp"
require "benchmark"

Inf = Float::INFINITY
def pbin(obj);case obj;when Integer;puts obj.to_s(2);end;end
class String;def to_a;split("");end;
             def to_ia(d = " ");split(d).map(&:to_i);end;end
def getia(*arg);gets.to_ia(*arg);end
def geti;gets.to_i;end
class Object;def dcopy;Marshal.load(Marshal.dump(self));end;end

n, q = getia
c = [0] * 2000
n.times do
  a, b = getia.sort
  for i in a..b
    c[i] += 1
  end
end
res = Inf
for i in 0..1010
  if c[i] == n
    res = [(i-q).abs, res].min
  end
end
puts res == Inf ? -1 : res

n,x = gets.split.map(&:to_i)
lmax = 0
rmin = 1000
n.times do |i|
  a,b = gets.split.map(&:to_i)
  if a > b then a,b = b,a end
  if a > lmax then lmax = a end
  if b < rmin then rmin = b end
end
#p lmax
#p rmin
if rmin < lmax then
  print -1
  elsif x < lmax then
  print lmax - x
  elsif x > rmin then
  print x - rmin
  else
  print 0
end
puts

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

seg_start = 0
seg_end = 1000
n.times do
	a, b = gets.split.map(&:to_i)
	a, b = b, a if a > b
	seg_start = a if a > seg_start
	seg_end = b if b < seg_end
end

if seg_end < seg_start
	puts -1
elsif seg_start < x && x < seg_end
	puts 0
else
	puts [(seg_start - x).abs, (seg_end - x).abs].min
end

# require 'set'
# n, x = gets.split.map(&:to_i)

# input = []
# n.times do
# 	input << gets.split.map(&:to_i)
# end

# interval = (input[0].min..input[0].max).to_set
# input.each do |segment|
# 	interval = interval & (segment.min..segment.max).to_set
# end
# if interval.empty?
# 	puts -1
# else
# 	puts interval.include?(x) ? 0 : [(interval.min - x).abs, (interval.max - x).abs].min
# end

require 'set'
n, x = gets.chomp!.split.map(&:to_i)

input = []
n.times do
	input << gets.chomp!.split.map(&:to_i)
end

interval = (input[0].min..input[0].max).to_set
input.each do |segment|
	interval = interval & (segment.min..segment.max).to_set
end
if interval.empty?
	puts -1
else
	puts interval.include?(x) ? 0 : [(interval.min - x).abs, (interval.max - x).abs].min
end

n,x = gets.split(" ").map(&:to_i)
start = -1
finish = 2000
n.times do
	a,b = gets.split(" ").map(&:to_i).minmax
	start = a if a > start
	finish = b if b < finish
end
ans = (start..finish).map{|a| (a-x).abs }.min
ans ||= -1
# puts "#{start} #{finish}"
puts "#{ans}"

# 3 3
# 0 7
# 8 10
# 11 13

n,d = gets.chomp!.split.map(&:to_i)
left,right = -1,1001
n.times do |x|
    a,b = gets.chomp!.split.map(&:to_i)
    a,b = [a,b].min,[a,b].max
    left = a if a > left
    right = b if b < right
end

ans = [left > d ? left-d : 0, right < d ? d-right : 0].max
ans = -1 if left > right
puts ans

#!/usr/bin/env ruby

n, x0 = gets.split.map(&:to_i)
x, y = -1000, 1000
n.times do
	a, b = gets.split.map(&:to_i)
	a, b = b, a if a > b
	x, y = [x, a].max, [y, b].min
end
puts x > y ? -1 : (x <= x0 && x0 <= y ? 0 : [x - x0, x0 - y].max)

ns,ms = gets.chomp.split(/ /)
n = ns.to_i
m = ms.to_i

ret = 0
for i in 0..n-1
	x1s, x2s = gets.chomp.split(/ /)
	x1 = x1s.to_i
	x2 = x2s.to_i
	ax = [x1,x2].max
	ix = [x1,x2].min
	if m < ix
		ret = [ix-m, ret].max
	end
	if m > ax
		ret = [m-ax, ret].max
	end
end
puts ret

ns,ms = gets.chomp.split(/ /)
n = ns.to_i
m = ms.to_i

ret = -1
for i in 0..n-1
	x1s, x2s = gets.chomp.split(/ /)
	x1 = x1s.to_i
	x2 = x2s.to_i
	ax = [x1,x2].max
	ix = [x1,x2].min
	if m < ix
		ret = [ix-m, ret].max
	end
	if m > ax
		ret = [m-ax, ret].max
	end
end
puts ret

ns,ms = gets.chomp.split(/ /)
n = ns.to_i
m = ms.to_i

mx1 = -1
mx2 = 1001
for i in 0..n-1
	x1s, x2s = gets.chomp.split(/ /)
	x1 = x1s.to_i
	x2 = x2s.to_i
	ax = [x1,x2].max
	ix = [x1,x2].min
	mx1 = [mx1, ix].max
	mx2 = [mx2, ax].min
end
if mx1 > mx2
	puts "-1"
else
	puts [(mx1-m).abs, (mx2-m).abs].min
end

class Range
	def ^(other)
		Range.new([first, other.first].max, [last, other.last].min)
	end
end

n, x0 = gets.split.map{|i| i.to_i}
r = []
n.times do
	a, b = gets.split.map{|i| i.to_i}
	a, b = b, a if a > b
	r <<= (a .. b)
end
r = r.inject{|i, j| i ^ j}

unless r.first < r.last then
	p -1
else
	p r.include?(x0) ? 0 : [(r.first - x0).abs, (r.last - x0).abs].min
end

n,d = gets.chomp!.split.map(&:to_i)
left,right = [],[]
n.times do |x|
    a,b = gets.chomp!.split.map(&:to_i)
    left << [a,b].min
    right << [a,b].max
end

ans1 = left.max > d ? left.max - d : 0
ans2 = right.min < d ? d - right.min : 0
puts [ans1,ans2].max

#!/usr/bin/env ruby

n, x0 = gets.split.map(&:to_i)
mn, mx = x0, x0
n.times do
	a, b = gets.split.map(&:to_i)
	a, b = b, a if a > b
	mx = [mx, a].max if a > x0
	mn = [mn, b].min if b < x0
end
puts mx - mn + [x0 - mn, mx - x0].min


Once when Gerald studied in the first year at school, his teacher gave the class the following homework. She offered the students a string consisting of n small Latin letters; the task was to learn the way the letters that the string contains are written. However, as Gerald is too lazy, he has no desire whatsoever to learn those letters. That's why he decided to lose some part of the string (not necessarily a connected part). The lost part can consist of any number of segments of any length, at any distance from each other. However, Gerald knows that if he loses more than k characters, it will be very suspicious. Find the least number of distinct characters that can remain in the string after no more than k characters are deleted. You also have to find any possible way to delete the characters.

The first input data line contains a string whose length is equal to n (1 ≤ n ≤ 105). The string consists of lowercase Latin letters. The second line contains the number k (0 ≤ k ≤ 105).

Print on the first line the only number m — the least possible number of different characters that could remain in the given string after it loses no more than k characters. Print on the second line the string that Gerald can get after some characters are lost. The string should have exactly m distinct characters. The final string should be the subsequence of the initial string. If Gerald can get several different strings with exactly m distinct characters, print any of them.

NoteIn the first sample the string consists of five identical letters but you are only allowed to delete 4 of them so that there was at least one letter left. Thus, the right answer is 1 and any string consisting of characters "a" from 1 to 5 in length.In the second sample you are allowed to delete 4 characters. You cannot delete all the characters, because the string has length equal to 7. However, you can delete all characters apart from "a" (as they are no more than four), which will result in the "aaaa" string.In the third sample you are given a line whose length is equal to 8, and k = 10, so that the whole line can be deleted. The correct answer is 0 and an empty string.

#!ruby

str = STDIN.gets.strip
k = STDIN.gets.strip.to_i

charnums = Hash.new(0)

str.each_byte do |c|
    charnums[c] += 1
end

delchar = []

charnums.to_a.sort do |a,b|
    (a[1] <=> b[1]) * 2 + (b[0] <=> a[0])
end.each do |elem|
    if elem[1] <= k
        delchar << elem[0]
        k -= elem[1]
    else
        break
    end
end

delchar.each do |c|
    str.delete!("%c"%c)
end

puts str.unpack("C*").uniq.length
puts str

s = gets.chomp
n = gets.to_i
a = s.split(//).uniq
b = []
a.each { |x| b << s.count(x) }
while (n - b.min) >= 0
  n -= b.min
  s.delete!(a[b.index(b.min)])
  b[b.index(b.min)] = 9999999999999999
  break if s.empty? || b.empty?
end
puts s.split(//).uniq.size
puts s

#!ruby

str = STDIN.gets.strip
k = STDIN.gets.strip.to_i

charnums = Hash.new(0)

str.each_byte do |c|
    charnums[c] += 1
end

delchar = []

charnums.to_a.sort do |a,b|
    (a[1] <=> b[1]) * 2 + (b[0] <=> a[0])
end.each do |elem|
    if elem[1] < k
        delchar << elem[0]
        k -= elem[1]
    else
        break
    end
end

delchar.each do |c|
    str.delete!("%c"%c)
end

puts str.unpack("C*").uniq.length
puts str


The semester is already ending, so Danil made an effort and decided to visit a lesson on harmony analysis to know how does the professor look like, at least. Danil was very bored on this lesson until the teacher gave the group a simple task: find 4 vectors in 4-dimensional space, such that every coordinate of every vector is 1 or  - 1 and any two vectors are orthogonal. Just as a reminder, two vectors in n-dimensional space are considered to be orthogonal if and only if their scalar product is equal to zero, that is: .Danil quickly managed to come up with the solution for this problem and the teacher noticed that the problem can be solved in a more general case for 2k vectors in 2k-dimensinoal space. When Danil came home, he quickly came up with the solution for this problem. Can you cope with it?

The only line of the input contains a single integer k (0 ≤ k ≤ 9).

Print 2k lines consisting of 2k characters each. The j-th character of the i-th line must be equal to ' * ' if the j-th coordinate of the i-th vector is equal to  - 1, and must be equal to ' + ' if it's equal to  + 1. It's guaranteed that the answer always exists. If there are many correct answers, print any.

NoteConsider all scalar products in example:  Vectors 1 and 2: ( + 1)·( + 1) + ( + 1)·( - 1) + ( - 1)·( + 1) + ( - 1)·( - 1) = 0  Vectors 1 and 3: ( + 1)·( + 1) + ( + 1)·( + 1) + ( - 1)·( + 1) + ( - 1)·( + 1) = 0  Vectors 1 and 4: ( + 1)·( + 1) + ( + 1)·( - 1) + ( - 1)·( - 1) + ( - 1)·( + 1) = 0  Vectors 2 and 3: ( + 1)·( + 1) + ( - 1)·( + 1) + ( + 1)·( + 1) + ( - 1)·( + 1) = 0  Vectors 2 and 4: ( + 1)·( + 1) + ( - 1)·( - 1) + ( + 1)·( - 1) + ( - 1)·( + 1) = 0  Vectors 3 and 4: ( + 1)·( + 1) + ( + 1)·( - 1) + ( + 1)·( - 1) + ( + 1)·( + 1) = 0

n = gets.to_i

flip = ->(s) { s.each_char.map { |c| c == '+' ? '*' : '+'}.join }

if n == 0 then
  puts "+"
else
  ans = []
  ans[1] = ["++", "+*"]

  (2..n).each do |i|
    k = 2**i
    ans[i] = ans[i - 1].dup
    (k/2).times do |j|
      if j.even? then
        ans[i][j] += flip.(ans[i][j])
      else
        ans[i][j] += ans[i][j]
      end
    end
    (k/2).times do |j|
      ans[i] << ans[i - 1][j].reverse
    end
    ((k/2)...k).each do |j|
      if j.odd? then
        ans[i][j] = flip.(ans[i][j]) + ans[i][j]
      else
        ans[i][j] = ans[i][j] + ans[i][j]
      end
    end
  end

  puts ans[n]
end

n = gets.chomp.to_i

as = ['+']

def f(as)
  bs = []
  as.each do |a|
    b = ''
    for i in 0...a.size
      if a[i] == '+'
        b += '++'
      else
        b += '**'
      end
    end
    bs << b
  end
  as.each do |a|
    b = ''
    for i in 0...a.size
      if a[i] == '+'
        b += '+*'
      else
        b += '*+'
      end
    end
    bs << b
  end
  return bs
end

for i in 1..n
  as = f(as)
end

puts as.*"\n"


The World Programming Olympics Medal is a metal disk, consisting of two parts: the first part is a ring with outer radius of r1 cm, inner radius of r2 cm, (0 < r2 < r1) made of metal with density p1 g/cm3. The second part is an inner disk with radius r2 cm, it is made of metal with density p2 g/cm3. The disk is nested inside the ring.The Olympic jury decided that r1 will take one of possible values of x1, x2, ..., xn. It is up to jury to decide which particular value r1 will take. Similarly, the Olympic jury decided that p1 will take one of possible value of y1, y2, ..., ym, and p2 will take a value from list z1, z2, ..., zk.According to most ancient traditions the ratio between the outer ring mass mout and the inner disk mass min must equal , where A, B are constants taken from ancient books. Now, to start making medals, the jury needs to take values for r1, p1, p2 and calculate the suitable value of r2.The jury wants to choose the value that would maximize radius r2. Help the jury find the sought value of r2. Value r2 doesn't have to be an integer.Medal has a uniform thickness throughout the area, the thickness of the inner disk is the same as the thickness of the outer ring.

The first input line contains an integer n and a sequence of integers x1, x2, ..., xn. The second input line contains an integer m and a sequence of integers y1, y2, ..., ym. The third input line contains an integer k and a sequence of integers z1, z2, ..., zk. The last line contains two integers A and B. All numbers given in the input are positive and do not exceed 5000. Each of the three sequences contains distinct numbers. The numbers in the lines are separated by spaces.

Print a single real number — the sought value r2 with absolute or relative error of at most 10 - 6. It is guaranteed that the solution that meets the problem requirements exists.

NoteIn the first sample the jury should choose the following values: r1 = 3, p1 = 2, p2 = 1.

gets " "
xs = gets.split.map(&:to_i).sort
gets " "
ys = gets.split.map(&:to_i).sort
gets " "
zs = gets.split.map(&:to_i).sort
a,b = gets.split.map(&:to_i)

#p [a,b]
#r1 = xs.max
p1 = ys.max
p2 = zs.min

for r1 in xs.reverse
    r2 = r1/Math::sqrt(1.0*a/b*p2/p1+1)
    #p [r1,r2,p1,p2]
    #p p1*(1.0*r1*r1/r2/r2 - 1)/p2
    break puts r2 if r2 < r1
end

x = gets.split.map(&:to_i)[1..-1].max
y = gets.split.map(&:to_i)[1..-1].max
z = gets.split.map(&:to_i)[1..-1].min
A, B = gets.split.map(&:to_i)
p Math.sqrt((x * x * y * B * 1.0 ) / (A * z + y * B))

a=gets.split.map(&:to_i)[1..-1].max;
b=gets.split.map(&:to_i)[1..-1].max;
c=gets.split.map(&:to_i)[1..-1].min;
d,e=gets.split.map &:to_f;
p (a*a*b*e/(c*d+b*e))**0.5

r1 = gets.split.map(&:to_f).slice!(1..-1).max
ro1 = gets.split.map(&:to_f).slice!(1..-1).max
ro2 = gets.split.map(&:to_f).slice!(1..-1).min
a, b = gets.split.map(&:to_f)

puts Math.sqrt((r1*r1*b*ro1)/(b*ro1+a*ro2))