# Codeforces Round

=begin
# A
a, b = gets.split.map(&:to_i)
c, d = [a, b].min, [a, b].max
puts "#{[a, b].min} #{(a - b).abs / 2}"

# B
n = gets.to_i
h = gets.split.map(&:to_i)
m, a = [0] * (n - 1) + [h[n - 1]], [0] * n
(n - 2).downto(0) do |i|
  if h[i] <= m[i + 1]
    m[i] = m[i + 1]
    a[i] = m[i] - h[i] + 1
  else
    m[i] = h[i]
  end
end
puts a.join(' ')
=end

# C
n, k = gets.split.map(&:to_i)
cmp = ->(x, y) { (y % 10) <=> (x % 10) }
a = gets.split.map(&:to_i).sort(&cmp).map do |x|
  if x % 10 == 0 || k == 0
    x
  else
    d = [10 - x % 10, k].min
    k -= d
    x += d
  end
end
ans = 0
a.each do |y|
  d = [100 - y, k].min
  k -= d
  ans += (y + d) / 10
end
puts ans

input = STDIN.gets
arr = input.split.map(&:to_i)
# всего навыков
n = arr[0]
# очки улучшений
k = arr[1]
# очков на одну способность, если поровну
# constant = (k / n).floor
# получаем все очки
input = STDIN.gets
abilities = input.split.map(&:to_i)
# создаем словарь
indexes = Hash.new

i = 0
# n шагов
abilities.each do |item|
  # заполняем словарь значениями
  indexes[i] = 10 - item % 10
  i += 1
end
# создаем ещё один словарь из первого
# отсортированный по значениям
# ? шагов - не знаю как сортировка устроена
sort =  Hash[indexes.sort_by{|k, v| v}]

# добавляем очки там, где их нужно меньше всего,
# чтобы "добить" до величины, кратной 10-ти
# ещё n шагов
sort.each do |key, value|
  if k - value >= 0
    abilities[key] += value
    k = k - value
  else
    break
  end
end

# если ещё остались очки - разбрасываем их по всем, добивая до 100 сколько
# сможем
if k > 0
  abilities.map! { |element|
    if k >= 100 - element
      k = k - (100 - element)
      100
    else
      prev_k = k
      k = 0
      element + prev_k
    end
  }
end

rating = 0
# финальный проход
# n шагов
abilities.each do |item|
  if item > 100
    item = 100
  end
  rating += (item * 0.1).floor
end

# Итого примерно
# 3n + ? шагов
# ? - сортировка
puts "#{rating}"

#!/usr/bin/ruby
n,k=gets.split.map(&:to_i)
a=gets.split.map(&:to_i).sort_by{|e|0-e%10}
a.size.times{|i|
	y=10-a[i]%10
	next if y==10
	x=[y,k].min
	a[i]+=x
	k-=x
}
a.size.times{|i|
	y=100-a[i]
	next if y==0
	x=[y,k].min
	a[i]+=x
	k-=x
}
p a.map{|e|e/10}.reduce(:+)

input = gets.chomp.split
n = Integer(input[0])
k = Integer(input[1])
input = gets.chomp.split
skills = []
for i in 0...n
    skills[i] = Integer(input[i])
end

sum = 0
sequence = []
skill = []
for i in 0...n
    if skills[i] == 100
        sum += 10
    else
        skill.push(skills[i])
    end
end
for i in 0...skill.length
    sequence[i] = []
    sequence[i][0] = skill[i] / 10
    sequence[i][1] = skill[i] % 10
end
sequence = sequence.sort{|x, y| y[1] <=> x[1]}
for i in 0...sequence.length
    if 10 - sequence[i][1] < k
        k -= 10 - sequence[i][1]
        sequence[i][1] = 10
    else
        sequence[i][1] += k
        k = 0
        break
    end
end
numbers = []
for i in 0...sequence.length
    numbers[i] = 10 * sequence[i][0] + sequence[i][1]
end
if k >= 10
    for i in 0...numbers.length
        if k >= 100 - numbers[i]
            k -= 100 - numbers[i]
            numbers[i] = 100
        else
            numbers[i] += k
            k = 0
            break
        end
    end
end

for i in 0...numbers.length
    sum += (numbers[i].to_f / 10).floor
end
puts sum

firstInput = gets.chomp.split(' ').map {|item| item.to_i}
skillNum, itemNum = firstInput[0], firstInput[1]
ans = 0
skillLevel = gets.chomp.split(' ').map {|item| item.to_i}
skillLevel.sort! {|a, b| b % 10 <=> a % 10}

10.times do |a|
index = 0
until itemNum == 0 || index == skillNum
	if skillLevel[index] == 100
		index += 1
		next
	end

	rest = 10 - (skillLevel[index] % 10)
	itemNum -= rest
	break if itemNum < 0
	skillLevel[index] += rest
	index += 1
end
end

skillLevel.each do |level|
	ans += (level / 10)
end

puts ans

input = STDIN.gets
arr = input.split.map(&:to_i)
# всего навыков
n = arr[0]
# очки улучшений
k = arr[1]
# очков на одну способность, если поровну
# constant = (k / n).floor
# получаем все очки
input = STDIN.gets
abilities = input.split.map(&:to_i)
# создаем словарь
indexes = Hash.new

i = 0
# n шагов
abilities.each do |item|
  # заполняем словарь значениями
  indexes[i] = 10 - item % 10
  i += 1
end
# создаем ещё один словарь из первого
# отсортированный по значениям
# ? шагов - не знаю как сортировка устроена
sort =  Hash[indexes.sort_by{|k, v| v}]

# добавляем очки там, где их нужно меньше всего,
# чтобы "добить" до величины, кратной 10-ти
# ещё n шагов
sort.each do |key, value|
  if k - value >= 0
    abilities[key] += value
    k = k - value
  else
    break
  end
end

# если ещё остались очки - разбрасываем их по всем, добивая до 100 сколько
# сможем
if k > 0
  abilities.map! { |element|
    if k >= 100 - element
      k = k - (100 - element)
      element = 100
    else
      element += k
      k = 0
    end
  }
end

rating = 0
# финальный проход
# n шагов
abilities.each do |item|
  if item > 100
    item = 100
  end
  rating += (item * 0.1).floor
end

# Итого примерно
# 3n + ? шагов
# ? - сортировка
puts "#{rating}"

input = STDIN.gets
arr = input.split.map(&:to_i)
# всего навыков
n = arr[0]
# очки улучшений
k = arr[1]
# очков на одну способность, если поровну
# constant = (k / n).floor
# получаем все очки
input = STDIN.gets
abilities = input.split.map(&:to_i)
# создаем словарь
indexes = Hash.new

i = 0
# n шагов
abilities.each do |item|
  # заполняем словарь значениями
  indexes[i] = 10 - item % 10
  i += 1
end
# создаем ещё один словарь из первого
# отсортированный по значениям
# ? шагов - не знаю как сортировка устроена
sort =  Hash[indexes.sort_by{|k, v| v}]

# добавляем очки там, где их нужно меньше всего,
# чтобы "добить" до величины, кратной 10-ти
# ещё n шагов
sort.each do |key, value|
  if k - value >= 0
    abilities[key] += value
    k = k - value
  else
    break
  end
end

# если ещё остались очки - разбрасываем их по всем, добивая до 100 сколько
# сможем
if k > 0
  abilities.map! { |element|
    if k >= 100 - element
      k = k - (100 - element)
      element = 100
    else
      element += k
      k = 0
    end
  }
end

puts "#{abilities}"
rating = 0
# финальный проход
# n шагов
abilities.each do |item|
  if item > 100
    item = 100
  end
  rating += (item * 0.1).floor
end

# Итого примерно
# 3n + ? шагов
# ? - сортировка
puts "#{rating}"

input = STDIN.gets
arr = input.split.map(&:to_i)
# всего навыков
n = arr[0]
# очки улучшений
k = arr[1]
# получаем все очки
input = STDIN.gets
abilities = input.split.map(&:to_i)
# создаем словарь
indexes = Hash.new

i = 0
# n шагов
abilities.each do |item|
  # заполняем словарь значениями
  indexes[i] = 10 - item % 10
  i += 1
end
# создаем ещё один словарь из первого
# отсортированный по значениям
# ? шагов - не знаю как сортировка устроена
sort =  Hash[indexes.sort_by{|k, v| v}]

# добавляем очки там, где их нужно меньше всего,
# чтобы "добить" до величины, кратной 10-ти
# ещё n шагов в пределе, хотя может и раньше закончиться
sort.each do |key, value|
  if k - value >= 0
    abilities[key] += value
    k = k - value
  else
    break
  end
end

rating = 0
# финальный проход
# n шагов
abilities.each do |item|
  rating += (item * 0.1).floor
end

# Итого примерно
# 3n + ? шагов
# ? - сортировка
puts "#{rating}"

input = STDIN.gets
arr = input.split.map(&:to_i)
# всего навыков
n = arr[0]
# очки улучшений
k = arr[1]
# получаем все очки
input = STDIN.gets
abilities = input.split.map(&:to_i)
# создаем словарь
indexes = Hash.new

rating = 0
i = 0
# n шагов
abilities.each do |item|
  # заполняем словарь значениями
  indexes[i] = 10 - item % 10
  i += 1
end
# создаем ещё один словарь из первого
# отсортированный по значениям
# ? шагов - не знаю как сортировка устроена
sort =  Hash[indexes.sort_by{|k, v| v}]

# добавляем очки там, где их нужно меньше всего,
# чтобы "добить" до величины, кратной 10-ти
# ещё n шагов в пределе, хотя может и раньше закончиться
sort.each do |key, value|
  if k - value > 0
    abilities[key] += value
    k = k - value
  else
    break
  end
end

# финальный проход
# n шагов
abilities.each do |item|
  rating += (item * 0.1).floor
end

# Итого примерно
# 3n + ? шагов
# ? - сортировка
puts "#{rating}"

input = gets.chomp.split
n = Integer(input[0])
k = Integer(input[1])
input = gets.chomp.split
skills = []
for i in 0...n
    skills[i] = Integer(input[i])
end

skills.sort!{|x, y| y <=> x}
for i in 0...n
    if skills[i] % 10 != 0
        unit_needed = 10 - skills[i] % 10
    else
        unit_needed = 0
    end
    if unit_needed > k
        break
    else
        skills[i] += unit_needed
        k -= unit_needed
    end
end
if k >= 10
    for i in 0...n
        x = k / 10
        if skills[i] < 100
            y = (100 - skills[i]) / 10
            if x <= y
                skills[i] += 10 * x
                break
            else
                skills[i] = 100
                k -= 10 * y
            end
        end
    end
end
sum = 0
if skills[0] == skills[n-1] && skills[0] == 0
    sum = (k.to_f / 10).floor
else
    for i in 0...n
        sum += (skills[i].to_f / 10).floor
    end
end
puts sum

input = gets.chomp.split
n = Integer(input[0])
k = Integer(input[1])
input = gets.chomp.split
skills = []
for i in 0...n
    skills[i] = Integer(input[i])
end

sum = 0
for i in 0...skills.length
    if skills[i] == 100
        sum += 10
        skills.delete_at(i)
    end
end
sequence = []
for i in 0...skills.length
    sequence[i] = []
    sequence[i][0] = skills[i] / 10
    sequence[i][1] = skills[i] % 10
end
sequence = sequence.sort{|x, y| y[1] <=> x[1]}
for i in 0...sequence.length
    if 10 - sequence[i][1] < k
        k -= 10 - sequence[i][1]
        sequence[i][1] = 10
    else
        sequence[i][1] += k
        k = 0
        break
    end
end
numbers = []
for i in 0...sequence.length
    numbers[i] = 10 * sequence[i][0] + sequence[i][1]
end
if k >= 10
    for i in 0...numbers.length
        if k >= 100 - numbers[i]
            numbers[i] = 100
            k -= 100 - numbers[i]
        else
            numbers[i] += k
            k = 0
            break
        end
    end
end
for i in 0...numbers.length
    sum += (numbers[i].to_f / 10).floor
end
puts sum

input = gets.chomp.split
n = Integer(input[0])
k = Integer(input[1])
input = gets.chomp.split
skills = []
for i in 0...n
    skills[i] = Integer(input[i])
end

skills.sort!{|x, y| y <=> x}
for i in 0...n
    if skills[i] % 10 != 0
        unit_needed = 10 - skills[i] % 10
    else
        unit_needed = 0
    end
    if unit_needed > k
        break
    else
        skills[i] += unit_needed
        k -= unit_needed
    end
end
sum = 0
if skills[0] == skills[n-1] && skills[0] == 0
    sum = (k.to_f / 10).floor
else
    for i in 0...n
        sum += (skills[i].to_f / 10).floor
    end
end
puts sum

input = gets.chomp.split
n = Integer(input[0])
k = Integer(input[1])
input = gets.chomp.split
skills = []
for i in 0...n
    skills[i] = Integer(input[i])
end

skills.sort!{|x, y| y <=> x}
for i in 0...n
    if skills[i] % 10 != 0
        unit_needed = 10 - skills[i] % 10
    else
        unit_needed = 0
    end
    if unit_needed > k
        break
    else
        skills[i] += unit_needed
        k -= unit_needed
    end
end
if k >= 10
    skills[0] += 10 * (k / 10)
end
sum = 0
if skills[0] == skills[n-1] && skills[0] == 0
    sum = (k.to_f / 10).floor
else
    for i in 0...n
        sum += (skills[i].to_f / 10).floor
    end
end
puts sum

input = gets.chomp.split
n = Integer(input[0])
k = Integer(input[1])
input = gets.chomp.split
skills = []
for i in 0...n
    skills[i] = Integer(input[i])
end

skills.sort!{|x, y| y <=> x}
for i in 0...n
    if skills[i] % 10 != 0
        unit_needed = 10 - skills[i] % 10
    else
        unit_needed = 0
    end
    if unit_needed > k
        break
    else
        skills[i] += unit_needed
        k -= unit_needed
    end
end
sum = 0
for i in 0...n
    sum += (skills[i].to_f / 10).floor
end
puts sum

firstInput = gets.chomp.split(' ').map {|item| item.to_i}
skillNum, itemNum = firstInput[0], firstInput[1]
skillHash = {}
ans = 0
skillLevel = gets.chomp.split(' ').map {|item| item.to_i}
skillLevel.each_with_index do |value, index|
	skillHash[index] = value % 10
end

skillLevel.sort! {|a, b| b % 10 <=> a % 10}

index = 0
until itemNum == 0 || index == skillNum
	rest = 10 - (skillLevel[index] % 10)
	itemNum -= rest
	break if itemNum < 0
	skillLevel[index] += rest
	index += 1
end

skillLevel.each do |level|
	ans += (level / 10)
end

puts ans

firstInput = gets.chomp.split(' ').map {|item| item.to_i}
skillNum, itemNum = firstInput[0], firstInput[1]
ans = 0
skillLevel = gets.chomp.split(' ').map {|item| item.to_i}
skillLevel.sort! {|a, b| b % 10 <=> a % 10}

index = 0
until itemNum == 0 || index == skillNum
	if skillLevel[index] == 100
		index += 1
		next
	end

	rest = 10 - (skillLevel[index] % 10)
	itemNum -= rest
	break if itemNum < 0
	skillLevel[index] += rest
	index += 1
end

skillLevel.sort! {|a, b| b % 100 <=> a % 100}

index = 0
until itemNum == 0 || index == skillNum
	if skillLevel[index] == 100
		index += 1
		next
	end

	rest = 100 - (skillLevel[index] % 100)
	itemNum -= rest
	break if itemNum < 0
	skillLevel[index] += rest
	index += 1
end

skillLevel.each do |level|
	ans += (level / 10)
end

puts ans


m,k=gets.split.map(&:to_i)
d=gets.split.map(&:to_i)
s=gets.split.map(&:to_i)
f=0
maxi=0
res=0
for i in 0..m-1 do
    maxi=i if s[maxi]<s[i]
    f+=s[i]
    res+=d[i]
    if f<d[i] then
        r=((d[i]-f+s[maxi]-1)/s[maxi]).to_i
        res+=r*k
        f+=r*s[maxi]
    end
    f-=d[i]
#    p [f, res]
end
puts res

# Bayan 2012-2013 Elimination Round
# Problem A -- Old Peykan
read = lambda { gets.split.map(&:to_i) }
m, k = read.call
d = read.call
s = read.call
answer = now = left = delta = 0
while now < m
    delta = [delta, s[now]].max
    left += s[now]
    while left < d[now]
        answer += k
        left += delta
    end
    answer += d[now]
    left -= d[now]
    now += 1
end
puts answer

def main()
	inpmk=gets.chomp.split(" ")
	inpmk=inpmk.map{|i| i.to_i}
	m=inpmk[0]
	k=inpmk[1]
	n=m+1
	inpd=gets.chomp.split(" ")
	d=inpd.map{|i| i.to_i}
	inps=gets.chomp.split(" ")
	s=inps.map{|i| i.to_i}
	mint=0;
	pet=s[0]
	for i in 0...m
		if pet>=d[i]
			pet-=d[i]
			mint+=d[i]
			if i < m-1
				pet+=s[i+1]
			end
		else
			maxp=s[0..i].max
			while(pet<d[i])
				mint+=k;
				pet+=maxp
			end
			pet-=d[i]
			mint+=d[i]
			if i < m-1
				pet+=s[i+1]
			end
		end
	end
	puts mint
end
main()

def main()
    inpmk=gets.chomp.split(" ")
    inpmk=inpmk.map{|i| i.to_i}
    m=inpmk[0]
    k=inpmk[1]
    n=m+1
    inpd=gets.chomp.split(" ")
    d=inpd.map{|i| i.to_i}
    inps=gets.chomp.split(" ")
    s=inps.map{|i| i.to_i}
    mint=0;
    pet=s[0]
    tiws=0;
    stop=false;
    for i in 0...m
        if pet>=d[i]
            pet-=d[i]
            tiws+=d[i]
            if i < m-1
                pet+=s[i+1]
            end
        else
            stop=true;
            break;
        end
    end
    if stop
        pet=s[0]
        for i in 0...m
            if pet>=d[i]
                pet-=d[i]
                mint+=d[i]
                if i < m-1
                    pet+=s[i+1]
                end
            else
                maxp=s[0..i].max
                while(pet<d[i])
                    mint+=k;
                    pet+=maxp
                end
                pet-=d[i]
                mint+=d[i]
                 if i < m-1
                    pet+=s[i+1]
                end
            end
        end
        puts mint;
    else
        puts tiws
    end
end
main()

m,k=gets.split.map(&:to_i)
d=gets.split.map(&:to_i)
s=gets.split.map(&:to_i)
f=s[0]
res=0
for i in 0..m-1 do
    a=(d[i]/f).to_i
    res+=(d[i]+a*k)
    f+=a*s[i]
    f+=s[i]
end
puts res

m,k=gets.split.map(&:to_i)
d=gets.split.map(&:to_i)
s=gets.split.map(&:to_i)
f=s[0]
maxi=0
res=0
for i in 0..m-1 do
    maxi=i if s[maxi]<s[i]
    if f<d[i] then
        r=((d[i]-f+s[maxi]-1)/s[maxi]).to_i
        res+=(r-1)*k
        f+=r*s[maxi]
    end
    f-=d[i]
    res+=d[i]
#    p [f, res]
end
puts res

def main()
	inpmk=gets.chomp.split(" ")
	inpmk=inpmk.map{|i| i.to_i}
	m=inpmk[0]
	k=inpmk[1]
	n=m+1
	inpd=gets.chomp.split(" ")
	d=inpd.map{|i| i.to_i}
	inps=gets.chomp.split(" ")
	s=inps.map{|i| i.to_i}
	mint=0;
	pet=s[0]
	tiws=0;
	stop=false;
	for i in 0...m
		if pet>=d[i]
			pet-=d[i]
			tiws+=d[i]
			if i < m-1
				pet+=s[i+1]
			end
		else
			stop=true;
			break;
		end
	end
	if stop
		pet=s[0]
		for i in 0...m
			if pet>=d[i]
				pet-=d[i]
				mint+=d[i]
				if i < m
					pet+=s[i+1]
				end
			else
				maxp=s[0..i].max
				while(pet<d[i])
					mint+=k;
					pet+=maxp
				end
				pet-=d[i]
				mint+=d[i]
			end
		end
		puts mint;
	else
		puts tiws
	end
end
main()


# Sort the elements
# find diff array
# check if d is the same for all elements
# if so insert at first and last
# if d is diff for just one element insert at that position
# if d is diff for more than one element no ap is possible
# special case when 2 elements - can be inserted in between by changing d as d/ 2

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
ans = nil
if arr.length == 1
	puts -1
else
	arr.sort!
	if arr.length == 2
		d = arr[1] - arr[0]
		ans = [arr[0] - d,arr[1]+d]
		mid = (arr[0] + arr[1]) / 2
		ans << mid if (mid - arr[0] == arr[1] - mid)
	else
		diff = []
		arr.each_cons(2) do |a,b|
			diff << b - a
		end
		d = diff.min
		count = diff.count{|x| x!=d}
		if count == 0
			ans = [arr[0] - d,arr[-1]+d]
		elsif count == 1
			i = diff.index{|x| x!=d}
			ans = [arr[i]+d] if arr[i]+d == arr[i+1]-d
		end

	end

	if ans
		ans.uniq!
		puts ans.length
		puts ans.sort.join(" ")
	else
		puts 0
	end
end

n=gets.to_i
a=gets.split.map(&:to_i).sort
h=Hash.new(0)
if n==1
  p -1
elsif n==2
  if a[0]==a[1]
    puts 1,a[0]
  elsif (a[0]+a[1])%2==0
    d=a[1]-a[0]
    puts 3,[a[0]-d,(a[0]+a[1])/2,a[1]+d].sort*' '
  else
    d=a[1]-a[0]
    puts 2,[a[0]-d,a[1]+d].sort*' '
  end
else
  j=nil
  (1...n).each do |i|
    h[a[i]-a[i-1]]+=1
    break if h.size>2
  end
  if h.size==1
    if h[0]>0
      puts 1,a[0]
    else
      d=a[1]-a[0]
      puts 2,[a[0]-d,a[-1]+d].sort*' '
    end
  elsif h.size==2
    k=h.keys
    d=e=nil
    if k[0]*2==k[1]
      d=k[0]
      e=k[1]
    elsif k[1]*2==k[0]
      d=k[1]
      e=k[0]
    end
    if d&&e&&h[e]==1
      (1...n).each do |i|
        if a[i]-a[i-1]==e
          puts 1,a[i-1]+d
          exit
        end
      end
    else
      p 0
    end
  else
    p 0
  end
end

inp = ARGF.read.split
n = inp.shift.to_i
a = inp.map(&:to_i).sort!

solve = lambda {
  if n == 0 then 0
  elsif n == 1 then -1
  else
    mp = {}
    (1...n).each { |i|
      x = a[i] - a[i - 1]
      mp[x] = 0 if not mp.key? x
      mp[x] += 1
    }
    x, y = mp.keys.min, mp.keys.max
    if mp.size == 1
      d = a[1] - a[0]
      il = [a[0] - d, a[n - 1] + d]
      il << a[0] + d / 2 if a.size == 2 and d % 2 == 0
      il.sort!.uniq!
      il.size.to_s + "\n" + il.map(&:to_s).join(' ')
    elsif mp.size == 2 and mp[y] == 1 and x * 2 == y
      (1...n).step(1) { |i|
        return "1\n#{a[i - 1] + x}" if a[i] - a[i - 1] == y
      }
    else 0 end
  end
}

puts solve.call

# Sort the elements
# find diff array
# check if d is the same for all elements
# if so insert at first and last
# if d is diff for just one element insert at that position
# if d is diff for more than one element no ap is possible
# special case when 2 elements - can be inserted in between by changing d as d/ 2

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
ans = nil
if arr.length == 1
	puts -1
else
	arr.sort!
	if arr.length == 2
		d = arr[1] - arr[0]
		ans = [arr[0] - d,arr[1]+d]
		mid = (arr[0] + arr[1]) / 2
		ans << mid if (mid - arr[0] == arr[1] - mid)
	else
		diff = []
		arr.each_cons(2) do |a,b|
			diff << b - a
		end
		d = diff.min
		count = diff.count{|x| x!=d}
		if count == 0
			ans = [arr[0] - d,arr[-1]+d]
		elsif count == 1
			i = diff.index{|x| x!=d}
			ans = [arr[i]+d] if arr[i]+d == arr[i+1]-d
		end

	end

	if ans
		puts ans.length
		puts ans.sort.join(" ")
	else
		puts 0
	end
end

inp = ARGF.read.split
n = inp.shift.to_i
a = inp.map(&:to_i).sort!

solve = lambda {
  if n == 0 then 0
  elsif n == 1 then -1
  else
    mp = {}
    (1...n).each { |i|
      x = a[n - i] - a[n - i - 1]
      mp[x] = 0 if not mp.key? x
      mp[x] += 1
    }
    if mp.size == 1
      d = a[1] - a[0]
      il = [a[0] - d, a[n - 1] + d]
      il << a[0] + d / 2 if a.size == 2 and d % 2 == 0
      il.sort!.uniq!
      il.size.to_s + "\n" + il.map(&:to_s).join(' ')
    elsif mp.size == 2
      x, y = mp.keys.min, mp.keys.max
      if x * 2 == y
        (1...n).step(1) { |i|
          return "1\n#{a[i - 1] + x}" if a[i] - a[i - 1] == y
        }
      else 0 end
    end
  end
}

puts solve.call


gets
x=$<.map{|x|x.split.map(&:to_i).sort[1]}
puts x

=begin

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

=end

t=gets.to_i
for i in 1..t
    a,b,c =gets.split.map &:to_i

    if a>b
        if (b>c)
            p b
        elsif a>c
            p c
        else
            p a
        end
    else
        if (a>c)
            p a
        elsif b>c
            p c
        else
            p b
        end
   end

end

main = -> {
  abc = gets.split.map(&:to_i)
  abc.sum - abc.min - abc.max
}

t = gets.to_i
puts t.times.map{ main.call }.join("\n")

gets.to_i.times{
    n=gets.split.map &:to_i
    n=n.sort
    puts n[1]
}


max_min = 0
n, m = gets.split.map(&:to_i)
for i in 1..n do
  row = gets.split.map(&:to_i)
  min = 1000000001
  row.each do |num|
    min = num if num < min
  end
  max_min = min if min > max_min
end
puts max_min

n, _ = gets.split.map &:to_i
prices = Array.new n
n.times do |i|
    prices[i] = gets.split.map(&:to_i).min
end
puts prices.max

ans = 0
rows, cols = gets.split.map(&:to_i)
rows.times do
	ans = [ans, gets.split.map(&:to_i).min].max
end

puts ans

n_e_m = gets.split(" ")
n = n_e_m[0].to_i
m = n_e_m[1].to_i
restaurantes = []
restaurante_escolhido = 0
n.times do
	ruas_numero = []
	ruas =  gets.split(" ")
	for i in 0..ruas.size - 1 do
		ruas_numero << ruas[i].to_i
	end
	restaurantes << ruas_numero.sort
end
for avenida in 0..restaurantes.size-1 do
	if restaurantes[avenida][0] > restaurante_escolhido
		restaurante_escolhido = restaurantes[avenida][0]
	end
end
puts restaurante_escolhido

n, m = gets.chomp.split(" ").map(&:to_i)
restraunts = []
street = gets.chomp.split(" ").map(&:to_i)
street_min = street.min
row = 0
restraunts << street
(n-1).times do |i|
  street = gets.chomp.split(" ").map(&:to_i)
  if street_min < street.min then
    street_min = street.min
    row = i + 1
  end
  restraunts << street
end
puts restraunts[row].min

n, m = gets.split(' ').map(&:to_i);
a = [];
n.times {
    a += [(gets.split(' ').map(&:to_i).min)];
}
puts a.max;

n =gets.split
ans=0
n[0].to_i.times do
    now=2000000000
    a=gets.split
    for x in a do
       now=x.to_i if x.to_i<now
    end
    ans=now if now>ans
end
p ans

n, m = gets.split.map(&:to_i)
max = 0
n.times do
  a = gets.split.map(&:to_i).min
  max = a if a > max
end
puts max

n_e_m = gets.split(" ")
n = n_e_m[0].to_i
m = n_e_m[1].to_i
restaurantes = []
ruas_numero = []
restaurante_escolhido = 0
n.times do
	ruas =  gets.split(" ")
	ruas.each do |rua|
		ruas_numero << rua.to_i
	end
	restaurantes << ruas_numero.sort
end
for avenida in 0..restaurantes.size-1 do
	if restaurantes[avenida][0] > restaurante_escolhido
		restaurante_escolhido = restaurantes[avenida][0]
	end
end
puts restaurante_escolhido

n_e_m = gets.split(" ")
n = n_e_m[0].to_i
restaurantes = []
restaurante_escolhido = 0
n.times do
	restaurantes << gets.split(" ").sort
end
for avenida in 0..restaurantes.size-1 do
	if restaurantes[avenida][0].to_i > restaurante_escolhido
		avenida_escolhida = restaurantes[avenida]
		restaurante_escolhido = restaurantes[avenida][0].to_i
	end
end
puts restaurante_escolhido

n_e_m = gets.split(" ")
n = n_e_m[0].to_i
m = n_e_m[1].to_i
restaurantes = []
avenida_letras = []
restaurante_da_emma = 0
emma = 0
jack = 100000000
n.times do
	restaurantes << gets.split(" ")
end
restaurantes.each_with_index do |avenida, index|
	for rua in 0..avenida.size - 1 do
		restaurante_atual = restaurantes[index][rua].to_i
		if restaurante_atual > restaurante_da_emma
			emma = index
			restaurante_da_emma = restaurante_atual
		end
	end
end
escolha_da_emma = restaurantes[emma]
for escolha in 0..escolha_da_emma.size-1 do
	if escolha_da_emma[escolha].to_i < jack
		jack = escolha_da_emma[escolha].to_i
	end
end
puts jack

n_e_m = gets.split(" ")
n = n_e_m[0].to_i
m = n_e_m[1].to_i
restaurantes = []
avenida_letras = []
emma = 0
nova_emma = 1000000000
jack = 100000000
n.times do
	restaurantes << gets.split(" ")
end
restaurantes.each_with_index do |avenida, index|
	for rua in 0..avenida.size - 1 do
		restaurante_atual = restaurantes[index][rua].to_i
		if restaurante_atual < nova_emma
			nova_emma = restaurante_atual
		end
	end
	if nova_emma > emma
		emma = index
	end
end
escolha_da_emma = restaurantes[emma]
for escolha in 0..escolha_da_emma.size-1 do
	if escolha_da_emma[escolha].to_i < jack
		jack = escolha_da_emma[escolha].to_i
	end
end
puts jack

n_e_m = gets.split(" ")
n = n_e_m[0].to_i
m = n_e_m[1].to_i
restaurantes = []
avenida_letras = []
emma = 0
nova_emma = 1000000000
jack = 100000000
n.times do
	restaurantes << gets.split(" ")
end
restaurantes.each_with_index do |avenida, index|
	for rua in 0..avenida.size - 1 do
		restaurante_atual = restaurantes[index][rua].to_i
		if restaurante_atual > nova_emma
			nova_emma = restaurante_atual
		end
	end
	if nova_emma > emma
		emma = index
	end
end
escolha_da_emma = restaurantes[emma]
for escolha in 0..escolha_da_emma.size-1 do
	if escolha_da_emma[escolha].to_i < jack
		jack = escolha_da_emma[escolha].to_i
	end
end
puts jack


gets.to_i.times do
  a, b, k = gets.split.map(&:to_i)
  puts (k/2) * (a-b) + (k%2)*a
end

def solve()
    line = gets

    a = line.split(' ')[0].to_i
    b = line.split(' ')[1].to_i
    k = line.split(' ')[2].to_i

    if ( k%2 == 0 )
        return ( a * (k/2) - b * (k/2))
    end

    return ( a * (k/2) - b * (k/2)) + a
end

ntest = gets

ntest.to_i.times { |nn|

    puts solve()

}

1.upto(gets.to_i) do
  a, b, k = gets.split(' ').map(&:to_i)

  puts (a-b)*k.div(2) + a*k.modulo(2)
end

n = gets.strip.to_i
results = []
for i in 0..n-1
  a, b, k = gets.strip.split.map(&:to_i)

  result = 0

  if a == b
    if k % 2 == 0
        result = 0
    else
        result = a
    end
  else
    if k % 2 == 0
        result = a*k/2-b*k/2
    else
      result = a*((k/2).floor+1)-b*(k/2).floor
    end
  end

  results << result
end
results.each do |result|
  p result
end

class Sol
  def initialize(a, b, k)
    @k = k
    @b = b
    @a = a
  end

  def res
    puts (@k + 1) / 2 * @a - (@k / 2) * @b
  end
end

t = gets.to_i
t.times do
  a, b, k = gets.split.map &:to_i
  s = Sol.new(a, b, k)
  s.res
end

t = gets.strip.to_i
t.times do
  a, b, k = gets.strip.split.map(&:to_i)
  puts (a - b) * (k / 2) + ((k % 2).zero? ? 0 : a)
end

gets.to_i.times do
    a,b,k = gets.split.map(&:to_i)
    puts a*((k+1)/2)-b*(k/2)
end

# coding: utf-8

# if __FILE__ == $0
# end

t = gets.to_i
t.times {
    a, b, k = gets.split.map(&:to_i)
    even_jumps, odd_jumps = (k/2.0).ceil, k/2
    puts even_jumps * a - odd_jumps * b
}

t = gets.to_i

t.times do
  s = gets.split.map &:to_i
  q = s[2] / 2 + s[2] % 2
  puts q * s[0] - (s[2] - q) * s[1]
end

t = gets.to_i
t.times do
  a, b, k = gets.split.map(&:to_i)
  p(a*((k + 1)/2) - b*(k/2))
end


n = gets.to_i
n.times do
  _ = gets.to_i
  puts gets.split(' ').uniq! ? 'NO' : 'YES'
end

gets.to_i.times do
  n=gets.to_i
  a=gets.split.map(&:to_i).uniq
  puts a.size==n ? 'YES' : 'NO'
end

t=gets.to_i
t.times do
    _=gets
    a=gets.split.map(&:to_i)
    l=a.length
    nl=a.uniq.length
    if l==nl
        puts "YES"
    else
        puts "NO"
    end
end


class Solution
  def initialize
    @n = gets.to_i
    @cities = []
    @n.times {@cities << gets.chomp}
    @d = gets.chomp
  end

  def solve
    length = (@cities.inject(0){|x, s| x + s.length})/(@n/2)

    left  = []
    right = []
    str_by_len = []
    for c in @cities do
      str_by_len[c.length] ||= []
      str_by_len[c.length] << c
    end
    (1..10).each do |i|
      unless str_by_len[i].nil?
        str_by_len[i].sort!
      end
    end

    (@n/2).times do
      min_s = '~' * 21
      min_i = -1
      min_j = -1
      (1..10).each do |i|
        next if str_by_len[i].nil? or str_by_len[i].empty?
        s1 = str_by_len[i].shift
        j = length - i
        next if str_by_len[j].nil? or str_by_len[j].empty?
        s2 = str_by_len[j].shift
        if s1 + @d + s2 < min_s
          min_s = s1 + @d + s2
          min_i = i
          min_j = j
        end
        str_by_len[j].unshift(s2)
        str_by_len[i].unshift(s1)
      end
      left  << str_by_len[min_i].shift
      right << str_by_len[min_j].shift
    end

    # find the last right column item
    (1..10).each do |i|
      if (not str_by_len[i].nil?) and (not str_by_len[i].empty?)
        right << str_by_len[i].shift
        break
      end
    end

    (@n/2).times do
      puts left.shift + @d + right.shift
    end
  end
end

solution = Solution.new
solution.solve

class Solution
  def initialize
    @n = gets.to_i
    @cities = []
    @n.times {@cities << gets.chomp}
    @d = gets.chomp
  end

  def solve
    length = (@cities.inject(0){|x, s| x + s.length})/(@n/2)

    left  = []
    right = []
    str_by_len = []
    for c in @cities do
      str_by_len[c.length] ||= []
      str_by_len[c.length] << c
    end
    (1..10).each do |i|
      unless str_by_len[i].nil?
        str_by_len[i].sort!
      end
    end

    (@n/2).times do
      min_s = '~' * 21
      min_i = -1
      min_j = -1
      (1..10).each do |i|
        next if str_by_len[i].nil? or str_by_len[i].empty?
        s1 = str_by_len[i].shift
        j = length - i
        next if str_by_len[j].nil? or str_by_len[j].empty?
        s2 = str_by_len[j].shift
        if s1 + @d + s2 < min_s
          min_s = s1 + @d + s2
          min_i = i
          min_j = j
        end
        str_by_len[j].unshift(s2)
        str_by_len[i].unshift(s1)
      end
      left  << str_by_len[min_i].shift
      right << str_by_len[min_j].shift
    end

    (@n/2).times do
      puts left.shift + @d + right.shift
    end
  end
end

solution = Solution.new
solution.solve


n,m = gets.chomp.split(/ /).map!{|x| x.to_i}
p0 = gets.chomp.split(/ /).map!{|x| x.to_i}
k = gets.chomp.to_i
step = 0
for i in 0..k-1
	p = gets.chomp.split(/ /).map!{|x| x.to_i}
	sx = 0
	sy = 0
	if p[0] != 0
		if p[0] < 0
			sx = (p0[0]-1) / -p[0]
		else
			sx = (n - p0[0]) / p[0]
		end
	end
	if p[1] != 0
		if p[1] < 0
			sy = (p0[1]-1) / -p[1]
		else
			sy = (m - p0[1]) / p[1]
		end
	end
	ws = [sx, sy].min
	ws = sy if p[0] == 0 && p[1] != 0
	ws = sx if p[0] != 0 && p[1] == 0
	p0 = [p0[0]+p[0]*ws, p0[1]+p[1]*ws]
	step += ws
end
puts step

w,h=gets.split.map(&:to_i)
x,y=gets.split.map(&:to_i)
x,y=x-1,y-1
k=gets.to_i
ans=0
k.times{|i|
	dx,dy=gets.split.map(&:to_i)
	minimum=[]
	if dx<0
		minimum+=[x/-dx]
	elsif dx>0
		minimum+=[(w-x-1)/dx]
	end

	if dy<0
		minimum+=[y/-dy]
	elsif dy>0
		minimum+=[(h-y-1)/dy]
	end
	ans+=minimum.min
	x+=dx*minimum.min
	y+=dy*minimum.min
}

p ans

#!/usr/bin/env ruby

def getmax(cur, lim, d)
  if d > 0
    (lim-cur) / d
  elsif d < 0
    (cur-1) / (-d)
  else
    1000000000
  end
end

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

ans = 0
k.times do
  dx, dy = gets.split.map(&:to_i)
  max_x = getmax(x, n, dx)
  max_y = getmax(y, m, dy)
  maxstep =  [max_x, max_y].min
  x += dx*maxstep
  y += dy*maxstep
  ans += maxstep
end

p ans

n, m = STDIN.readline.split.map {|s| s.to_i }
x, y = STDIN.readline.split.map {|s| s.to_i }

def max_steps(x, d, n)
  return 10**9 if d == 0
  span = (d > 0 ? n-x : x-1)
  d = d.abs
  return span/d
end

count = 0
k = STDIN.readline.to_i
k.times do
  dx, dy = STDIN.readline.split.map {|s| s.to_i }
  a, b = max_steps(x, dx, n), max_steps(y, dy, m)
  c = [a, b].min
  x += c*dx
  y += c*dy
  count += c
end

puts count

sizes=gets.chomp
init=gets.chomp
v=[]
n=gets.chomp.to_i
n.times do
  v<<gets.chomp
end
a=[]
v.length.times do |x|
  a<<v[x].split(' ')
  2.times {|y| a[x][y]=a[x][y].to_i }
end
c=[]
s=[]
c=init.split(' ')
s=sizes.split(' ')
2.times {|x| c[x]=c[x].to_i;s[x]=s[x].to_i}
steps=0
for j in (0..n-1)
i=0;min=0;max=2*10**9;delta=0
while i>-1 do
  delta=(max-min)/2
  i=min+delta
  if !(c[0]+a[j][0]*i<1||c[0]+a[j][0]*i>s[0]||c[1]+a[j][1]*i<1||c[1]+a[j][1]*i>s[1])
    min=i
    if delta==0
      c[0]+=a[j][0]*i;c[1]+=a[j][1]*i
      steps+=i
      break
    end
  else
    max=i
  end
end
end
puts steps

n,m = gets.split.map(&:to_i)
x,y = gets.split.map(&:to_i)
vs = gets.to_i
v = []
vs.times{ v << gets.split.map(&:to_i) }
st = 0

v.each do |dxy|
  dx,dy = dxy[0],dxy[1]
  cx = dx > 0 ? n : 1
  cy = dy > 0 ? m : 1
  xx = dx != 0 ? ((cx-x)/dx).abs : 1000000000000
  yy = dy != 0 ? ((cy-y)/dy).abs : 1000000000000
  s = [yy,xx].min
  y = y+s*dy
  x = x+s*dx
  st += s
end
p st

inp = gets.split(" ")
n = inp[0].to_i
m = inp[1].to_i
inp = gets.split(" ")
xc = inp[0].to_i
yc = inp[1].to_i
k = gets.to_i
steps = 0
for i in 0..(k-1)

  inp = gets.split(" ")
  dxi = inp[0].to_i
  dyi = inp[1].to_i

  xsteps = 1000000000
  ysteps = 1000000000
  if(dxi > 0)
    xsteps = (n - xc) / dxi
  elsif (dxi < 0)
    xsteps = (xc - 1) / (-dxi)
  end
  if(dyi > 0)
    ysteps = (m - yc) / dyi
  elsif (dyi < 0)
    ysteps = (yc - 1) / (-dyi)
  end
  #puts ysteps
  s = [xsteps, ysteps].min
  xc += s * dxi
  yc += s * dyi
  #puts s
  #puts xc.to_s + "," + yc.to_s
  steps += s
end

puts steps

INF = 1000000001
nm = gets.split(" ")
n = nm[0].to_i
m = nm[1].to_i

xy = gets.split(" ")
x = xy[0].to_i
y = xy[1].to_i

k = gets.to_i
ans = 0
while k > 0
  dxy = gets.split(" ")
  dx = dxy[0].to_i
  w = INF
  if dx > 0 then
    w = (n - x) / dx
  elsif dx < 0 then
    w = (x-1) / (-dx)
  end
  dy = dxy[1].to_i
  h = INF
  if dy > 0 then
    h = (m - y) / dy
  elsif dy < 0 then
    h = (y - 1) / (-dy)
  end
  mn = w < h ? w : h
  ans += mn
  x += dx * mn
  y += dy * mn
  k = k-1
end

puts ans

sizes=gets.chomp
init=gets.chomp
v=[]
n=gets.chomp.to_i
n.times do
  v<<gets.chomp
end
a=[]
v.length.times do |x|
  a<<v[x].split(' ')
  2.times {|y| a[x][y]=a[x][y].to_i }
end

c=[]
s=[]
c=init.split(' ')
s=sizes.split(' ')
2.times {|x| c[x]=c[x].to_i;s[x]=s[x].to_i}
steps=0
for j in (0..n-1)
i=10**9
while i>0 do
  if !(c[0]+a[j][0]*i<1||c[0]+a[j][0]*i>s[0]||c[1]+a[j][1]*i<1||c[1]+a[j][1]*i>s[1])
    c[0]+=a[j][0]*i;c[1]+=a[j][1]*i
    steps+=i
    break
  end
  i/=2
end
end
puts steps

sizes=gets.chomp
init=gets.chomp
v=[]
(n_v=gets.chomp.to_i).times do
  v<<gets.chomp
end
a=[]
v.length.times do |x|
  a<<v[x].split(' ')
  2.times {|y| a[x][y]=a[x][y].to_i }
end
i=0
c=[init[0].chr.to_i,init[2].chr.to_i]
s=[sizes[0].chr.to_i,sizes[2].chr.to_i]
steps=0
while i<v.length do
  until c[0]<1||c[0]>s[0]||c[1]<1||c[1]>s[1] do
    c[0]+=a[i][0];c[1]+=a[i][1]
    if c[0]<1||c[0]>s[0]||c[1]<1||c[1]>s[1]
      c[0]-=a[i][0];c[1]-=a[i][1]
      break
    end
    steps+=1
  end
  i+=1
end
puts steps

inp = gets.split(" ")
n = inp[0].to_i
m = inp[1].to_i
inp = gets.split(" ")
xc = inp[0].to_i
yc = inp[1].to_i
k = gets.to_i
steps = 0
for i in 0..(k-1)

  inp = gets.split(" ")
  dxi = inp[0].to_i
  dyi = inp[1].to_i

  xsteps = 1000000000
  ysteps = 1000000000
  if(dxi > 0)
    xsteps = (n - xc) / dxi
  elsif (dxi < 0)
    xsteps = (xc - 1) / (-dxi)
  end
  if(dyi > 0)
    ysteps = (m - yc) / dyi
  elsif (dyi < 0)
    ysteps = (yc - 1) / (-dyi)
  end
  #puts ysteps
  s = [xsteps, ysteps].min
  xc += s * dxi
  yc += s * dyi
  puts s
  puts xc.to_s + "," + yc.to_s
  steps += s
end

puts steps

inp = gets.split(" ")
n = inp[0].to_i
m = inp[1].to_i
inp = gets.split(" ")
xc = inp[0].to_i
yc = inp[1].to_i
k = gets.to_i
steps = 0
for i in 0..(k-1)

  inp = gets.split(" ")
  dxi = inp[0].to_i
  dyi = inp[1].to_i

  xsteps = 1000000000
  ysteps = 1000000000
  if(dxi > 0)
    xsteps = (n - xc) / dxi
  elsif (dxi < 0)
    xsteps = (xc - 1) / (-dxi)
  end
  if(dyi > 0)
    ysteps = (n - yc) / dyi
  elsif (dyi < 0)
    ysteps = (yc - 1) / (-dyi)
  end
  #puts ysteps
  s = [xsteps, ysteps].min
  xc += s * dxi
  yc += s * dyi
  #puts s
  #puts xc.to_s + "," + yc.to_s
  steps += s
end

puts steps


n, m = STDIN.gets.chomp.split(" ").map(&:to_i)

freq = Array.new(m){ {'A' => 0, 'B' => 0, 'C' => 0, 'D' => 0, 'E' => 0} }

n.times do |i|
  line = STDIN.gets.chomp.each_char.with_index do |c, j|
    freq[j][c] += 1
  end
end

scores = STDIN.gets.chomp.split(" ").map(&:to_i)
puts freq.map.with_index{|f, i| f.values.max*scores[i]}.reduce(&:+)

a, b = gets.split.map(&:to_i)
c = Array.new(a).map do
  gets.chomp.bytes
end
f = gets.split.map(&:to_i)
puts(
  c.transpose.zip(f).map do |d, i|
    i * d.uniq.map{ |e| d.count e }.max
  end.inject(:+)
)

n, m = gets.split.map &:to_i
s = []
n.times { s << gets.chomp }
b = m.times.map do |j|
  h = {}
  (0...n).each do |i|
    h[s[i][j]] ||= 0
    h[s[i][j]] += 1
  end
  h.values.max
end
puts gets.split.each_with_index.inject(0) { |s, x| s + x[0].to_i*b[x[1]] }


m,d = gets.split.map{|s| s.to_i}
men = Array::new(m+1,0)
d.times {
	cd = gets.split.map{|s| s.to_i}
	fixed = 1
	cd.each {|i|
		fixed = men[i] if men[i] > 0 # only one men participating in the dance may have color associated previously
	}
	idx = fixed
	cd.each {|i|
		if men[i] != fixed
			men[i] = idx%3 + 1
			idx += 1
		end
	}
}
print men[1..-1].join(" ")

ar=Array.new(100100,0)
n,m=gets.chomp.split.map(&:to_i)
m.times do
	a=gets.chomp.split.map(&:to_i)
	b=[0,0,0,0]
	b[ar[a[0]]],b[ar[a[1]]],b[ar[a[2]]]=1,1,1
	3.times do |i|
		for j in 1...4
			if b[j]==0 && ar[a[i]]==0
				ar[a[i]]=j
				b[j]=1
			end
		end
	end
end
for i in 1..n
	print "#{ar[i]} "
end
puts

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

assignment = {}

m.times do
	arr = gets.split(" ").map(&:to_i)
	colors = [3,2,1]
	intersec = arr.detect{|a| assignment[a]}
	if intersec
		arr = arr - [intersec]
		colors -= [assignment[intersec]]
		# puts "colors #{intersec}"
	end
	arr.each do |a|
		assignment[a] = colors.pop
	end
end
ans = []
assignment.keys.sort.each do |k|
	ans << assignment[k]
end
puts ans.join(" ")

def solution(n, dancers)
  input = {}
  value = 0

  dancers.each do |i|
    color = [1, 2, 3]
    value = 0

    i.each do |j|
      if input.has_key?(j)
        color.delete(input[j])
      end
    end

    i.each_with_index do |j, k|
      value += 1 if input.has_key?(j)
      input[j] = color[k-value] unless input.has_key?(j)
    end
  end

  output = []
  n.times { |i| output << input[i+1] }

  output.join(" ")
end

n, m = STDIN.readline.split(" ").map(&:to_i)
dancers = []
m.times do
  dancers << STDIN.readline.split(" ").map(&:to_i)
end

values = solution(n,dancers)
puts values

#! /usr/bin/env ruby

WHITE = 4
RED = 2
BLUE = 1

def convert(calc_num)
  case calc_num
  when WHITE
    1
  when RED
    2
  when BLUE
    3
  end
end

n, m = STDIN.gets.split(' ').map(&:to_i)
dances = []
m.times {
  dances << STDIN.gets.split(' ').map(&:to_i)
}

colors = Array.new(n){0}

def fill(colors, dancers, color)
  dancers.each do |dancer|
    if colors[dancer.to_i - 1].zero?
      colors[dancer.to_i - 1] = color
      break
    end
  end
  colors
end

dances.each do |dancers|
  result = dancers.inject(0) {|s, i|
    s += colors[i - 1]
  }
  next if result == 7

  if result < WHITE
    colors = fill(colors, dancers, WHITE)
  else
    result -= WHITE
  end

  if result < RED
    colors = fill(colors, dancers, RED)
  else
    result -= RED
  end

  if result < BLUE
    colors = fill(colors, dancers, BLUE)
  end
end

answer = colors.map do |color|
  convert(color)
end.join(' ')
puts answer

n, m = gets.split.map(&:to_i)
color = Array.new(n+1, 0)
for i in 0...m
  a = gets.split.map(&:to_i)
  mark = Array.new(4, false)
  mark[color[a[0]]] = true
  mark[color[a[1]]] = true
  mark[color[a[2]]] = true
  if !mark[1] then
    for j in 0..2
      if color[a[j]] == 0 then
	color[a[j]] = 1
	break
      end
    end
  end
  if !mark[2] then
    for j in 0..2
      if color[a[j]] == 0 then
	color[a[j]] = 2
	break
      end
    end
  end
  if !mark[3] then
    for j in 0..2
      if color[a[j]] == 0 then
	color[a[j]] = 3
	break
      end
    end
  end
end

for i in 1..n
  print color[i], " "
end

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

assignment = {}

m.times do
	arr = gets.split(" ").map(&:to_i)
	colors = [3,2,1]
	intersec = arr.detect{|a| assignment[a]}
	if !intersec
		arr = arr - [intersec]
		colors -= [assignment[intersec]]
		# puts "colors #{intersec}"
	end
	arr.each do |a|
		assignment[a] = colors.pop
	end
end
ans = []
assignment.keys.sort.each do |k|
	ans << assignment[k]
end
puts ans.join(" ")

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

assignment = {}

m.times do
	arr = gets.split(" ").map(&:to_i)
	colors = [3,2,1]
	intersec = arr & assignment.keys
	if !intersec.empty?
		arr = arr - assignment.keys
		colors -= [assignment[intersec[0]]]
		# puts "colors #{intersec}"
	end
	arr.each do |a|
		assignment[a] = colors.pop
	end
end
puts assignment.values.join(" ")

def solution(n, dancers)
  input = {}
  value = 0

  dancers.each do |i|
    color = [1, 2, 3]
    value = 0
    i.each_with_index do |j, k|
      if input.has_key?(j)
        color.delete(input[j])
        value += 1
      else
        input[j] = color[k-value]
      end
    end
  end

  input.values.join(" ")
end

n, m = STDIN.readline.split(" ").map(&:to_i)
dancers = []
m.times do
  dancers << STDIN.readline.split(" ").map(&:to_i)
end

values = solution(n,dancers)
puts values

def solution(n, dancers)
  input = {}
  value = 0

  dancers.each do |i|
    color = [1, 2, 3]
    value = 0
    i. each do |j|
      if input.has_key?(j)
        color.delete(input[j])
        value += 1
      end
    end

    i.each_with_index do |j, k|
        input[j] = color[k-value]
      end
  end

  output = []
  n.times { |i| output << input[i+1] }

  output.join(" ")
end

n, m = STDIN.readline.split(" ").map(&:to_i)
dancers = []
m.times do
  dancers << STDIN.readline.split(" ").map(&:to_i)
end

values = solution(n,dancers)
puts values

def solution(n, dancers)
  input = {}
  value = 0

  dancers.each do |i|
    color = [1, 2, 3]
    value = 0
    i. each do |j|
      if input.has_key?(j)
        color.delete(input[j])
        value += 1
      end
    end

    i.each_with_index do |j, k|
        input[j] = color[k]
      end
  end

  output = []
  n.times { |i| output << input[i+1] }

  output.join(" ")
end

n, m = STDIN.readline.split(" ").map(&:to_i)
dancers = []
m.times do
  dancers << STDIN.readline.split(" ").map(&:to_i)
end

values = solution(n,dancers)
puts values

def solution(n, dancers)
  input = {}
  value = 0

  dancers.each do |i|
    color = [1, 2, 3]
    value = 0
    i.each_with_index do |j, k|
      if input.has_key?(j)
        color.delete(input[j])
        value += 1
      else
        input[j] = color[k-value]
      end
    end
  end

  output = []
  n.times { |i| output << input[i+1] }

  output.join(" ")
end

n, m = STDIN.readline.split(" ").map(&:to_i)
dancers = []
m.times do
  dancers << STDIN.readline.split(" ").map(&:to_i)
end

values = solution(n,dancers)
puts values


n = gets.chomp.to_i
n.times do
	ngo = gets.chomp.to_i
	if ngo <= 3
		puts 4 - ngo
	elsif ngo.even?
		puts 0
	else
	  puts 1
	end
end

gets.to_i.times do
    n = gets.to_i
    puts n == 2 ? 2 : n % 2
end

#!usr/bin/env ruby

# https://codeforces.com/problemset/problem/1223/A

count = gets.to_i
answers = []

count.times do
  match_count = gets.to_i
  answers << if match_count == 2
               2
             else
                match_count % 2
             end
end

answers.each { |answer| puts answer }

q = gets.to_i
q.times do
  n = gets.to_i
  if n == 1
    puts 3
  elsif n == 2
    puts 2
  else
    a = (n.to_f/2).ceil
    b = n - a
    puts (a-b).abs
  end
end

q = gets.to_i
q.times do
  n = gets.to_i
  puts n < 4 ? 4 - n : n%2
end

1.upto(gets.to_i) do
  n = gets.to_i

  case n
  when 2..4
    puts 4 - n
  when proc(&:odd?)
    puts 1
  else
    puts 0
  end
end


t = Integer(gets)

for i in 0..t-1
    n = Integer(gets)
    arr = Array.new
    a = gets
    arr = a.split(" ")

    for j in 0..n-1
       arr[j] = Integer(arr[j]);
    end

    mn = arr.min
    ans = 0
    for j in 0..n-1
        ans += (arr[j] - mn)
    end
    puts ans
end

gets.to_i.times do
  gets
  as = gets.split.map(&:to_i)
  m = as.min
  puts as.map { _1 - m }.sum
end

def main
  gets.to_i.times do
    n = gi
    a = gli.sort
    puts a.sum - a[0] * n
  end
end

MOD = 10**9 + 7
def gs = gets.chomp
def gi = gets.to_i
def gli = gets.split.map(&:to_i)
def cyesno(x) = x ? :Yes : :No
def uyesno(x) = x ? :YES : :NO

main

main = -> {
  T = int
  T.times do
      n = int
      a = ints
      m = a.min
      puts a.sum { |x| x - m }
  end
}

DEBUG = true
MOD = 10**9+7
$yesno = %w(No Yes)
YesNo = %w(No Yes)
YESNO = %w(NO YES)
INF = 10**18

require "set"
require "prime"

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

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

class TrueClass
  include Boolean
  def to_i; 1 end
end

class FalseClass
  include Boolean
  def to_i; 0 end
end

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

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

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

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

main[]

gets.to_i.times do
  gets
  xs = gets.split.map(&:to_i)
  min = xs.min
  p xs.map { |x| x - min }.sum
end


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

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

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

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

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

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

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

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

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

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

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

n = inp[0]
n.times do
  d = inp[0]
  keta = 0
  ld = d
  li = 0
  100000.times do |i|
    ld = d
    keta += (i+1).to_s.size
    d -= keta
    li = i
    break if(d <= 0)
  end
  pta = keta
  keta = 0
  d = ld
  while 0 < d
    ld = d
    keta += 1
    d -= (9*(10**(keta-1)))*keta
  end
  ld -= 1
  no = ld % keta
  ka = ld/keta
  puts (ka+10**(keta-1)).to_s[no]
end

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

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

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

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

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

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

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

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

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

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

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

n = inp[0]
n.times do
  d = inp[0]
  keta = 0
  ld = d
  li = 0
  100000.times do |i|
    ld = d
    keta += (i+1).to_s.size
    d -= keta
    li = i
    break if(d <= 0)
  end
  pta = keta
  keta = 0
  d = ld
  while 0 <= d
    ld = d
    keta += 1
    d -= (9*(10**(keta-1)))*keta
  end
  ld -= 1
  no = ld % keta
  ka = ld/keta
  puts (ka+10**(keta-1)).to_s[no]
end


n = gets.to_i
s = gets.chomp

k = n % 3
r = ''
if k != 0
  r = s[0, 2] + '-'
  s = s[2 ... s.length]
end

for i in 0...n/3
  r = r + s[3 * i, 3] + '-'
end
puts r[0, r.length - 1]

n = gets.chomp.to_i
s = gets.chomp
first = true
if n % 2 == 0
	while s.length > 0
		print "-" if not first
		print s.slice!(0,2)
		first = false
	end
else
	len = 3
	while s.length > 0
		print "-" if not first
		print s.slice!(0,len)
		first = false
		len = 2
	end
end
puts

#!/usr/bin/ruby
gets;s=gets.chomp
if s.size%3==1
	t=s[-4..-1]
	x=s[0..-5].chars.each_slice(3).map{|e|e*''}*'-'
	puts (x.size>0 ? x+'-' : '')+t[0,2]+'-'+t[2,2]
else
	puts s.chars.each_slice(3).map{|e|e*''}*'-'
end

gets
s = gets.chop
print s.slice!(0..1)+'-' if s.size % 3 == 1
s.gsub!(/.../){$&+'-'}
s.chop! if s =~ /-$/
puts s

n=gets.to_i
m=gets.split[0]
i=0
s=0
if n%3==1 then
        print m[0..1]
        print "-"
        print m[2..3]
        s=1
        i=4
elsif n%3==2 then
        print m[0..1]
        s=1
        i=2
end
while i<n
        if s>0 then
                print "-"
        end
        s=1
        print m[i..(i+2)]
        i+=3
end
puts

n=gets.chomp.to_i
phone=gets.chomp
s=""
if n%2==0
  n.times do |i|
    s+=phone[i,1]
    if i%2 == 1
      s+="-"
    end
  end
  puts s[0,s.length-1]
else
  (n-2).times do |i|
    s+=phone[i,1]
    if i%2 == 1
      s+="-"
    end
  end
  s+=phone[-2,3]
  puts s
end

digits = gets.to_i
number = gets.chomp
if digits % 2 == 0
  telephone = number.scan(/.{2}/)
  (0...telephone.size).each do |i|
    if i == telephone.size - 1
      puts telephone[i]
    else
      print "#{telephone[i]}-"
    end
  end
else
  if digits % 3 == 0
    telephone = number.scan(/.{3}/)
    (0...telephone.size).each do |i|
      if i == telephone.size - 1
        puts telephone[i]
      else
        print "#{telephone[i]}-"
      end
    end
  else
    if (digits-3) % 2 == 0
      telephone = number[0...digits-3].scan(/.{2}/)
      (0...telephone.size).each do |i|
          print "#{telephone[i]}-"
      end
      puts number[digits-3...digits]
    end
  end
end

n, phone = gets, gets.chomp
left = phone.length
i=0
s=""
while left>0
  s+="-" if s!=""
  x = left==3?2:1;
  s << phone[i..i+x]
  i = i+x+1
  left = left-x-1
end
puts s

n = gets.to_i
gets.strip.each_char.with_index do |y,i|
  if i > 0 && (i%2 == 0 && i < n-1 || i == n-3 && n%2 != 0)
    print '-';
  end
  print y
end

n = gets.to_i
gets.strip.each_char.with_index do |y,i|
  if i > 0 && (i%2 == 0 && i < n-1 || i == n-3 && n%2 != 0)
    print '-';
  end;
  print y;
end;

n = gets.to_i
gets.strip.each_char.with_index do |y,i|
  if i > 0 && (i%2 == 0 && i < n-1 || i == n-3 && n%2 != 0)
    print '-';
  end;
  print y
end

n = gets.to_i
gets.strip.each_char.with_index do |y,i|
  if i > 0 && (i%2 == 0 && i < n-1 || i == n-3 && n%2 != 0)
    print '-'
  end
  print y
end

n = gets.to_i
gets.strip.each_char.with_index do |y,i|
  if i > 0 && (i%2 == 0 && i < n-1 || i == n-3 && n%2 != 0);
    print '-';
  end;
  print y;
end;

numOfDigits = gets.chomp.to_i
phoneNumber = gets.chomp

div5 = numOfDigits / 5
mod5 = numOfDigits % 5

numOf2 = div5
numOf3 = div5

if mod5 > 0
    if mod5 % 2 == 0
        #2 or 4
        numOf2 = numOf2 + (mod5 / 2)
    elsif mod5 % 3 == 0
        #3
        numOf3 = numOf3 + 1
    else
        #1
        numOf2 = numOf2 - 1
        numOf3 = numOf3 + 1
    end
end

result = ""
index = 0

1.upto(numOf2) do |i|
    result = result + "-" + phoneNumber[index, 2]
    index = index + 2
end

1.upto(numOf3) do |i|
    result = result + "-" + phoneNumber[index, 3]
    index = index + 3
end

if result[0, 1] == "-"
    result = result[1, result.length - 1]
end

puts result

gets
phone = gets.chomp.scan(/./)

res = ""
while phone.size >= 2
  res << phone.shift(2).join << '-'
end
res = res[0..-2]
res << phone.join

puts res

#!/usr/bin/ruby
gets;s=gets.chomp
if s.size%3==1
	t=s[-4..-1]
	puts s[0..-5].chars.each_slice(3).map{|e|e*''}*'-'+'-'+t[0,2]+'-'+t[2,2]
else
	puts s.chars.each_slice(3).map{|e|e*''}*'-'
end


n=gets.to_i
a=n.times.map{gets.split.map(&:to_i).max}
a[a.index(n-1)]=n
puts a*' '

n = gets.to_i
m = (1..n).map{gets.strip.split(" ").map(&:to_i)}

a = [nil] * n

i = 1
while i <= n
    m.each_with_index do |s, k|
        if s.all? {|x| x <= i} && s.any? {|x| x == i-1}
            a[k] = i
            i += 1
        end
    end
end

puts a.join(" ")

# http://codeforces.com/problemset/problem/618/B

n = gets.to_i
ans = []
n.times do |i|
    arr = gets.split.map(&:to_i)
    ans << arr.max
end
ans[ans.index(n - 1)] = n
puts ans * " "

n = gets.chomp.to_i
ps = []
f = false
n.times do
  as = gets.chomp.split.map(&:to_i)
  p = as.sort!.uniq.size - 1
  if p == n - 1
    ps << (f ? p : n)
    f = true
  else
    ps << p
  end
end

puts ps.*' '

n = gets.to_i
g = []
for _ in 0...n
  a = gets.split(' ').map(&:to_i)
  g << a
end

r = []
for j in 0...n
  for k in 0..j
    if g[j][k] == n-1
      r[j] = n
      r[k] = n-1
    end
  end
end

for i in 2...n
  x = n-i
  for j in 0...n
    for k in 0..j
      if g[j][k] == x && (!r[j] || !r[k])
        if r[k]
          r[j] = x
        else
          r[k] = x
        end
      end
    end
  end
end

puts r.join(' ')

n = gets.chomp.to_i
n.times do |i|
  r = gets.chomp.split(" ")
  if r.uniq.length == n then
    r[i] = n
    puts r.join(" ")
    exit
  end
end

# http://codeforces.com/problemset/problem/618/B

n = gets.to_i
ans = []
n.times do |i|
    arr = gets.split.map(&:to_i)
    ans << arr.max
end
ans[ans.index(n - 1)] = n
p ans * " "


n = gets.to_i
a = gets.split.map(&:to_i)
puts n - (1..3).map{|x| a.count(x)}.max

def function
a=gets.chomp
b=gets.chomp
b=b.split.map(&:to_i)
puts (b.length-b.count(b.uniq.max_by{ |i| b.count( i ) }))
end
function

n    = gets.chomp.to_i
ints = gets.chomp.split
h = Hash.new(0)
ints.each { |i| h[i] += 1 }
puts n - h.values.max

n = gets
arr = gets

nr1 = 0
nr2 = 0
nr3 = 0

arr.split(" ").map(&:to_i).each do |x|
  if x == 1
    nr1 += 1
  elsif x == 2
    nr2 += 1
  else
    nr3 += 1
  end
end

puts n.to_i - [nr1,nr2, nr3].max

gets
gets
p [*?1..?3].map{|_|$_.count _}.combination(2).map{|a,b|a+b}.min

gets; n = gets.strip
print [n.count("1"), n.count("2"), n.count("3")].sort[0,2].inject(0) { |sum, e| sum += e }

n = gets.chomp.to_i
a = gets.chomp.split.map(&:to_i)
puts n - [a.select {|x| x == 3 }.length, a.select{|x| x==2}.length, a.select{|x| x==1}.length].max

def function
a=gets.chomp
b=gets.chomp
b=b.split.map(&:to_i)
print (b.length-b.count(b.uniq.max_by{ |i| b.count( i ) }))
end

def function
a=gets.chomp
b=gets.chomp
b=b.split.map(&:to_i)
puts b.length-b.count(b.uniq.max_by{ |i| b.count( i ) })
end

n = gets
arr = gets

nr1 = 0
nr2 = 0
nr3 = 0

arr.split(" ").map(&:to_i).each do |x|
  if x == 1
    nr1 += 1
  elsif x == 2
    nr2 += 1
  else
    nr3 += 1
  end
end

n.to_i - [nr1,nr2, nr3].max

n = gets
arr = gets

nr1 = 0
nr2 = 0
nr3 = 0

if n.to_i == 0
  puts 0
elsif n.to_i == 1
  puts 0
end

arr.split(" ").map(&:to_i).each do |x|
  if x == 1
    nr1 += 1
  elsif x == 2
    nr2 += 1
  else
    nr3 += 1
  end
end

puts n.to_i - [nr1,nr2, nr3].max

n = gets
arr = gets

nr1 = 0
nr2 = 0
nr3 = 0

if n.to_i == 0
  puts 0
elsif n.to_i == 1
  puts 1
end

arr.split(" ").map(&:to_i).each do |x|
  if x == 1
    nr1 += 1
  elsif x == 2
    nr2 += 1
  else
    nr3 += 1
  end
end

puts n.to_i - [nr1,nr2, nr3].max

n = gets.chomp.to_i
a = gets.chomp.split.map(&:to_i)
puts n - [a.select {|x| x == 1 }.length, a.select{|x| x==2}.length, a.select{|x| x==1}.length].max


gets
flowers = gets.chomp.split.map(&:to_i).sort
sum = flowers.inject(0) { |s, v| s + v }

if sum.odd?
  puts sum
  exit
else
  first_odd = flowers.detect{ |f| f.odd? }
  if first_odd
    puts sum - first_odd
    exit
  end
end
puts 0

s = gets
odds = []
evens = []

gets.split(" ").each {|e|
  t = e.to_i
  if (t % 2 == 0)
    evens.push t
  else
    odds.push t
  end
}

odds.sort!
evens.sort!

if (odds.size == 0)
  puts 0
else
  t = odds.inject(0) {|r, i| r + i} + evens.inject(0) {|r, i| r + i}
  if (odds.size % 2 == 0)
    t -= odds[0]
  end
  puts t
end

N = gets.to_i
a = gets.split(' ').map {|i| i.to_i}
sum = a.inject(0) {|s, i| s + i}
cntNe = a.inject(0) {|s, i|
  r = s
  r += 1 if i % 2 != 0
  r
}

if cntNe == 0
  puts 0
  exit
end

if cntNe % 2 != 0
  puts sum
  exit
end

puts sum - a.select{|i| i % 2 != 0}.min

n = gets.chomp
input = gets.chomp.split(' ').collect { |i| i.to_i }
sum = 0

smallestOdd = input.collect do |i|
					if i.odd? then i else 101 end
			   end.min
smallestOdd = -1 if smallestOdd == 101
input.each { |i| sum = sum + i }

if sum.odd?
 puts sum
elsif smallestOdd == -1
puts 0
else
puts sum - smallestOdd
end

n=gets.to_i
a=gets.split.map(&:to_i).sort
b,c=[],[]
a.each{|k|
        if k%2==0  then
                b.push(k)
        else
                c.push(k)
        end
}
init=1
if c.length==0 then
        puts 0
        exit
end
ans=0
if c.length%2==0 then
        for i in 1..(c.length-1)
                ans+=c[i]
        end
else
        for i in 0..(c.length-1)
                ans+=c[i]
        end
end
b.each{|k| ans+=k}
p ans

n=gets.to_i
a=gets.split.map(&:to_i)
b,c=[],[]
a.each{|k|
        if k%2==0  then
                b.push(k)
        else
                c.push(k)
        end
}
init=1
if c.length==0 then
        puts 0
        exit
end
ans=0
if c.length%2==0 then
        for i in 1..(c.length-1)
                ans+=c[i]
        end
else
        for i in 0..(c.length-1)
                ans+=c[i]
        end
end
b.each{|k| ans+=k}
p ans

n = gets.chomp
input = gets.chomp.split(' ').collect { |i| i.to_i }
sum = 0

smallestOdd = input.collect do |i|
					if i.odd? then i else 101 end
			   end.min
smallestOdd = 0 if smallestOdd == 101
input.each { |i| sum = sum + i }

if sum.odd? then puts sum
else puts sum - smallestOdd
end

n = gets.chomp
input = gets.chomp.split(' ').collect { |i| i.to_i }
sum = 0

smallestOdd = input.collect do |i|
					if i.odd? then i else 101 end
			   end.min
input.each { |i| sum = sum + i }

puts sum - smallestOdd if sum.even?
puts sum if sum.odd?

gets
flowers = gets.chomp.split.map(&:to_i).sort
sum = flowers.inject(0) { |s, v| s + v }

if sum.odd?
  puts sum
  exit
else
  while flowers.size > 0
    sum = sum - flowers.shift
    if sum.odd?
      puts sum
      exit
    end
  end
end
puts 0


tc = gets().to_i
tc.times do
    n = gets().to_i
    arr = gets().split(' ').map(&:to_i).sort
    k = true
    for i in (1..n-1) do
        if arr[i]-arr[i-1] > 1
            puts "NO";
            k = false
            break;
        end
    end
    if (k)
        puts "YES"
    end
end

gets.to_i.times do
  answer = true

  gets
  gets.split(' ').map(&:to_i).sort.each_cons(2) { |a, b| answer &&= b - a <= 1 }

  puts answer ? 'YES' : 'NO'
end

gets.to_i.times do
  gets
  a = gets.split(' ').map(&:to_i).sort

  p = a.shift
  x = 0

  while a.size > 0
    x += 1 if (a.first - p).abs > 1
    p = a.shift
  end

  puts x.zero? ? "YES" : "NO"
end

test_case = gets.to_i
i = 0
while i < test_case
    result = true
    i += 1
    len_ = gets.to_i
    Acase = gets.chomp.split(' ').map(&:to_i)
    Acase.sort!
    #puts Acase.inspect()
    j = 0
    while j < len_-1

        if Acase[j+1].to_i - Acase[j].to_i >= 2
            result = false
            j += 1
            break
        end
        j += 1
    end
    puts (result) ? "Yes" : "No"

end

t=gets.chomp.to_i
#p "t= #{t}"
while t!=0

len=gets.chomp.to_i
#p "length= #{len}"
k=0
#while k<=len-1
ar = gets.chomp.split(' ').map!{ |o| o.to_i }
#k+=1
#end
ar.sort!
 #p ar
def func(ar)

l=ar.size
if l==1
 return "YES"
end
 i=0
while i < l-1

if ar[i+1]-ar[i]>1
  return "NO"
end
i+=1
end
return "YES"
end

puts func(ar)

t-=1
end

times = gets.to_i

def resolve(array)
  array.sort.each_cons(2) {|e,n| return "NO" if n-e >1}
  "YES"
end

(1..times).each do |i|
  array_size = gets.to_i

  a = Array.new(array_size)
  a = gets.split(/\s+/).map(&:to_i)
  puts resolve(a)
end

t = gets.chomp.to_i
t.times do
    n = gets.chomp.to_i
    a = gets.chomp.split(" ").map(&:to_i)
    aa = a.sort
    i = 0
    while true
        if n.to_s == "1"
            puts "YES"
            break
        end
        sa = (aa[i+1] - aa[i]).abs
        if sa <= 1
            aa.shift
            i = 0
        else
            puts "NO"
            break
        end
        if aa.size == 1
            puts "YES"
            break
        end
    end
end

gets.to_i.times do
    n = gets.to_i
    a = gets.split.map(&:to_i).sort!
    ok = true
    (1...n).each do |i|
        ok &= a[i] - a[i - 1] <= 1
    end
    puts ok ? "YES" : "NO"
end

t = gets.to_i
ans = []
t.times do
  n = gets.to_i
  a = gets.chomp.split(" ").map(&:to_i).sort
  judge = true
  (n-1).times do |i|
    if a[i+1] - a[i] >= 2
      judge = false
    end
  end
  if judge
    ans << "YES"
  else
    ans << "NO"
  end
end
ans.each do |str|
  puts str
end

t=gets.chomp.to_i
#p "t= #{t}"
while t!=0

len=gets.chomp.to_i
#p "length= #{len}"
k=0
#while k<=len-1
ar = gets.chomp.split(' ').map!{ |o| o.to_i }
#k+=1
#end
ar.sort!
 p ar
def func(ar)

l=ar.size
if l==1
 return "YES"
end
 i=0
while i < l-1

if ar[i+1]-ar[i]>1
  return "NO"
end
i+=1
end
return "YES"
end

p func(ar)

t-=1
end

t=gets.chomp.to_i
#p "t= #{t}"
while t!=0

len=gets.chomp.to_i
#p "length= #{len}"
k=0
#while k<=len-1
ar = gets.chomp.split(' ').map!{ |o| o.to_i }
#k+=1
#end
ar.sort!
 #p ar
def func(ar)

l=ar.size
if l==1
 return "YES"
end
 i=0
while i < l-1

if ar[i+1]-ar[i]>1
  return "NO"
end
i+=1
end
return "YES"
end

p func(ar)

t-=1
end

t = gets.chomp.to_i
t.times do
    n = gets.chomp.to_i
    a = gets.chomp.split(" ").map(&:to_i)
    aa = a.sort
    i = 0
    while true
        if n == 1
            puts "YES"
            break
        end
        sa = (a[i+1] - a[i]).abs
        if sa <= 1
            a.shift
            i = 0
        else
            puts "NO"
            break
        end
        if a.size == 1
            puts "YES"
            break
        end
    end
end

def array(arr)
    arr.sort!.each_cons(2) do |a, b|
        b - a > 1 ? 'NO' : 'YES'
    end
end

test_case = gets.to_i
i = 0
while i < test_case
    result = true
    i += 1
    len_ = gets.to_i
    Acase = gets.chomp.split(' ').map(&:to_i)
    Acase.sort!
    #puts Acase.inspect()
    j = 0
    while j < len_-1
        j += 1
        if Acase[j+1].to_i - Acase[j].to_i >= 2
            result = false
            break
        end
    end
    puts (result) ? "Yes" : "No"

end

tc = gets().to_i
tc.times do
    n = gets().to_i
    arr = gets().split(' ').map(&:to_i).sort
    if arr[n-1]-arr[0] > 1
        puts("NO")
    else
        puts("YES")
    end
end

tc = gets().to_i
tc.times do
    n = gets().to_i
    arr = gets().split(' ').map(&:to_i)
    k = true
    for i in (1..n-1) do
        if arr[i]-arr[i-1] > 1
            puts "NO";
            k = false
            break;
        end
    end
    if (k)
        puts "YES"
    end
end


n = gets.to_i
a = n.times.map{
	    x, y = gets.split
			    [y.to_i, x]
}.sort
b = []
a.each do |x, y|
	    break if x > b.size
			    b.each{|_| _[1] += 1 if _[1] >= x}
					    b << [y, x]
end
puts b.size < n ? -1 : b.map{|i, j| "#{i} #{n - j}"} * "\n"

n=gets.to_i
ans=[]
n.times.map{name,a=gets.split;[name,a.to_i]}.sort_by{|name,a|a}.each{|name,a|
	if ans.size<a
		p -1
		exit
	end
	ans.insert(a,[name,n-a])
}
ans.each{|_|puts"%s %d"%_}

def solve(data)
  data.sort_by!{|n,a| a}

  ret = []
  ho = []
  data.each_with_index do |(n, a), i|
    return nil if a > i
    ho[a, 0] = n
  end

  data.each do |n, a|
    ret << [n, data.size-ho.index(n)]
  end
  ret
end

def check(sol)
  hs = []
  sol.map{|n,h| v = [n, hs.count{|x| x > h}]; hs << h; v}
end

def test
  srand(122039)
  100.times do |i|
    p i
    d0 = 30.times.map{|x|["a#{x}", rand(10**9)]}
    input = check(d0)
    out  = solve(input)
    input2 = check(out)
    if input != input2
      p d0, input, input2
      raise
    end
  end
  exit
end

n = gets.to_i
data = n.times.map{ a, b = gets.split; [a, b.to_i]}
ret = solve(data)
if ret
  ret.each{|a| puts a*" "}
else
  puts -1
end

q=gets.to_i.times.map{name,a=gets.split;[name,a.to_i]}
r=[0.0,1e9]
ans=[]
h=q.group_by{|name,a|a}
0.upto(3000){|n|
	hn=h[n]
	next unless hn
	if r.size-2<n
		p -1
		exit
	end
	ins_idx=n+1
	vl,vr=r[ins_idx-1],r[ins_idx]
	d=(vr-vl)/(hn.size+1)
	d_range=(vl+d).step(vr-d,d).to_a
	d_range.each{|v|r.insert(ins_idx,v)}
	0.upto(hn.size-1){|i|ans<<[hn[i][0],d_range[i]]}
}
r.reverse!
ans.each{|name,v|puts"%s %d"%[name,r.index(v)]}


#!ruby
gets
puts$<.partition{|x|x.to_i<0}.map(&:size).min<2?:yes: :no

n = gets.chomp.to_i
arr = []
pos = 0
neg = 0
for i in (0..n-1)
	arr << gets.chomp.split.map(&:to_i)
	if arr[i][0] > 0
		pos += 1
	else
		neg += 1
	end
end

if pos == arr.size || neg == arr.size
	print "Yes"
else
	if pos == 1 || neg == 1
		print "Yes"
	else
		print "No"
	end
end

gets;puts$<.partition{|e|e.to_i<0}.any?{|e|e.size<2} ? :YES : :NO

n=gets.to_i
x=y=0;
n.times{ gets.to_i<0 ? y+=1 : x+=1 }
puts ([x, y].min<2 ? "Yes" : "No")

puts gets.to_i.times.count{gets.split.map(&:to_i).reduce(:+)<1}<1 ? :YES : :NO

puts gets.to_i.times.count{gets.split.map(&:to_i).reduce(:+)<1}<2 ? :YES : :NO

gets;puts$<.group_by{|e|e.to_i<0}.any?{|k,v|v.size<2} ? :YES : :NO


gets.to_i.times do
  n, k = gets.split.map &:to_i
  if n.even? and k.even?
    if k > n
      puts "NO"
    else
      puts "YES"
      puts "1 "*(k - 1) + (n - k + 1).to_s
    end
  elsif n.even? and k.odd?
    if k*2 > n
      puts "NO"
    else
      puts "YES"
      puts "2 "*(k - 1) + (n - (k - 1)*2).to_s
    end
  else
    if k.even? or k > n
      puts "NO"
    else
      puts "YES"
      puts "1 "*(k - 1) + (n - k + 1).to_s
    end
  end
end

n = gets.chomp.to_i

tests = []

n.times do
  tests.push(gets.split(' ').map {|e| e.to_i})
end

tests.each do |test|
  n, k = test

  if n < k then
    puts 'NO'
    next
  end

  if n.odd? && k.even? then
    puts 'NO'
    next
  end

  results = []
  if n.even? && k.odd? then
    if 2 * k > n then
      puts 'NO'
      next
    end
    results.fill(2, 0..k - 2)
    results_sum = results.reduce(0) {|sum, value| sum + value }
    results.push(n - results_sum)
    puts 'YES'
    puts results.join(' ')
  else
    results.fill(1, 0..k - 2)
    results_sum = results.reduce(0) {|sum, value| sum + value }
    results.push(n - results_sum)
    puts 'YES'
    puts results.join(' ')
  end

end

  t = gets.to_i
  t.times do
    n, k = gets.split(' ').map(&:to_i)
    if k == 1
      puts 'YES'
      puts n
      next
    end
    res = []
    (k-1).times do |i|
      res << 1
    end
    if((_res = n - res.reduce(&:+)) % 2 != 0 && _res > 0)
        res << _res
        puts 'YES'
        puts res.join(' ')
    else
      res = []
      (k-1).times do |i|
        res << 2
      end
      if((_res = n - res.reduce(&:+)) % 2 == 0 && _res > 0)
          res << _res
          puts 'YES'
          puts res.join(' ')
      else
        puts 'NO'
      end
    end
  end

t = gets.to_i

def all(a,b)
  puts "YES"
  (1..a).each do
    print "#{b} "
  end
  puts
end

def printArr(arr)
  puts "YES"
  arr.each do |x|
    print "#{x} "
  end
  puts
end

(1).upto(t) do
  x = gets.split(' ')
  n = x[0].to_i
  k = x[1].to_i
  val1 = n/k
  val2 = n.modulo(k)
  if val1 ==0
    puts "NO"
    next
  end
  if(val2 == 0)
    all(k,val1)
    next
  end
  arr = []
  (1..k).each do
    arr.push(val1)
  end
  if val2.modulo(2)==0
    arr[0]+=val2
    printArr(arr)
    next
  end
  if k.modulo(2)==0
    puts "NO"
    next
  end
  if arr[0]!=1
    (0.. arr.length-1).each do |i|
      arr[i]-=1
      val2+=1
    end
    arr[0]+=val2
    printArr(arr)
    next
  end
  puts "NO"
end

# Created by IntelliJ IDEA.
# User: gautam.roy
# Date: 5/9/20
# Time: 9:37 PM
# To change this template use File | Settings | File Templates.

t = gets.to_i
for i in 1..t do
  n, k = gets.split(' ').map { |s| s.to_i }
  ret = []
  if n < k
    puts "NO"
    next
  end
  if k == 1
    puts "Yes"
    puts n
    next
  end
  if k.even? && !n.even?
    puts "NO"
    next
  end
  if (k.even? && n.even?) || (!k.even? && !n.even?)
    puts "Yes"
    for j in 1..(k - 1)
      ret << 1
    end
    ret << n - (k - 1)
    puts ret.join(" ")
    next
  end
  # k odd and n even
  if (n - 2 * (k - 1)) > 0
    puts "YES"
    for j in 1..(k - 1)
      ret << 2
    end
    ret << n - 2 * (k - 1)
    puts ret.join(" ")
  else
    puts "NO"
  end
end

gets.to_i.times{
    n,k=gets.split.map &:to_i
    res=[1]*(k-1)
    res<<n-k+1
    if res[-1].even?
        res=[2]*(k-1)
        res<<n-(k-1)*2
    end
    if (res[0]%2!=res[-1]%2) or (res[-1]<=0)
        puts :NO
    else
        puts :YES
        puts res*' '
    end
}

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

  f = [1, 2].find do |i|
    delta = n - (k-1)*i
    delta > 0 && (delta.even? == i.even?)
  end

  if f
    puts "YES"
    puts "#{f} " * (k-1) + (n - (k-1)*f).to_s
  else
    puts "NO"
  end
end

t = gets.to_i
t.times do
  n, k = gets.split(' ').map(&:to_i)
  _res = n / k
  res = []
  if n % k == 0
    k.times { res << _res }
  else
    t_res = _res + 1
    n_res = n - (t_res * (k-1))
    if n_res < 1
    elsif((n_res.odd? && t_res.odd?) || (n_res.even? && t_res.even?))
      (k - 1).times { res << t_res }
      res << n_res
    end
    (2..(k-1)).to_a.each do |i|
      t_res += 1
      n_res = (n - (t_res * (k-i))) / i
      if n_res < 1
      elsif((n_res.odd? && t_res.odd?) || (n_res.even? && t_res.even?))
        (k - i).times { res << t_res }
        i.times { res << n_res }
      end
      if res.length != 0 && res.reduce(&:+) == n
        break
      else
        res = []
      end
    end if res.length == 0
  end
  puts(res.length == 0 ? 'NO' : 'YES')
  next if res.length == 0
  puts(res.join(' '))
end

t = gets.to_i

def all(a,b)
  puts "YES"
  (1..a).each do
    print "#{b} "
  end
  puts
end

def printArr(arr)
  puts "YES"
  arr.each do |x|
    print "#{x} "
  end
  puts
end

(1).upto(t) do
  x = gets.split(' ')
  n = x[0].to_i
  k = x[1].to_i
  val1 = n/k
  val2 = n.modulo(k)
  if(val2 == 0)
    all(k,val1)
    next
  end
  arr = []
  (1..k).each do
    arr.push(val1)
  end
  if val2.modulo(2)==0
    arr[0]+=val2
    printArr(arr)
    next
  end
  if k.modulo(2)==0
    puts "NO"
    next
  end
  if arr[0]!=1
    (0.. arr.length-1).each do |i|
      arr[i]-=1
      val2+=1
    end
    arr[0]+=val2
    printArr(arr)
    next
  end
  puts "NO"
end

def func(n, k)
    res=2**40-1
    a=2**39
    while a>0
        t=res^a
        cnt=t-t/n
        res=t if cnt>=k
        a>>=1
    end
    res
end

gets.to_i.times{
    n, k=gets.split.map &:to_i
    puts func(n, k)
}

gets.to_i.times do
  n, k = gets.split.map &:to_i
  if n.even? and k.even?
    if k > n
      puts "NO"
    else
      puts "YES"
      puts "1 "*(k - 1) + (n - k + 1).to_s
    end
  elsif n.even? and k.odd?
    if k*2 > n
      puts "NO"
    else
      puts "YES"
      puts "2 "*(k - 1) + (n - (k - 1)*2).to_s
    end
  else
    if k.even?
      puts "NO"
    else
      puts "YES"
      puts "1 "*(k - 1) + (n - k + 1).to_s
    end
  end
end

n = gets.chomp.to_i

tests = []

n.times do
  tests.push(gets.split(' ').map {|e| e.to_i})
end

tests.each do |test|
  if test[0] % 2 == 1 && test[1] % 2 == 0 then
    puts 'NO'
    next
  end

  if test[0] < test[1] then
    puts 'NO'
    next
  end

  results = []

  if test[0] == test[1] then
    puts 'YES'
    results.fill(1, 0..test[1])
    puts results.join(' ')
    next
  end

  if test[0] % test[1] == 0 then
    puts 'YES'
    results.fill(test[0] / test[1], 0..test[1]-1)
    puts results.join(' ')
  elsif test[0] % 2 == 0 && test[1] % 2 == 1 then
    division = test[0] / test[1]
    division -= 1
    if division > 0 then
        results.fill(division, 0..test[1]-2)
        sum = results.reduce(0){|sum, value| sum + value}
        results.push(test[0] - sum)
        puts 'YES'
        puts results.join(' ')
    else
        puts 'NO'
    end
  elsif test[0] % 2 == 1 && test[1] % 2 == 1 then
    division = test[0] / test[1]
    results.fill(division, 0..test[1]-2)
    sum = results.reduce(0){|sum, value| sum + value}
    results.push(test[0] - sum)
            puts 'YES'
        puts results.join(' ')
  end
end

n = gets.chomp.to_i

tests = []

n.times do
  tests.push(gets.split(' ').map {|e| e.to_i})
end

tests.each do |test|
  if test[0] % 2 == 1 && test[1] % 2 == 0 then
    puts 'NO'
    next
  end

  if test[0] < test[1] then
    puts 'NO'
    next
  end

  results = []

  if test[0] == test[1] then
    puts 'YES'
    results.fill(1, 0..test[1]-1)
    puts results.join(' ')
    next
  end

  if test[0] % test[1] == 0 then
    puts 'YES'
    results.fill(test[0] / test[1], 0..test[1]-1)
    puts results.join(' ')
  elsif test[0] % 2 == 0 && test[1] % 2 == 1 then
    division = test[0] / test[1]
    division -= 1
    if division > 0 then
        results.fill(division, 0..test[1]-2)
        sum = results.reduce(0){|sum, value| sum + value}
        results.push(test[0] - sum)
        puts 'YES'
        puts results.join(' ')
    else
        puts 'NO'
    end
  elsif test[0] % 2 == 1 && test[1] % 2 == 1 then
    division = test[0] / test[1]
    results.fill(division, 0..test[1]-2)
    sum = results.reduce(0){|sum, value| sum + value}
    results.push(test[0] - sum)
            puts 'YES'
        puts results.join(' ')
  end
end

n = gets.chomp.to_i

tests = []

n.times do
  tests.push(gets.split(' ').map {|e| e.to_i})
end

tests.each do |test|
  n, k = test

  if n < k then
    puts 'NO'
    next
  end

  if n.odd? && k.even? then
    puts 'NO'
    next
  end

  if n - k == 1 && n.even? then
    puts 'NO'
    next
  end

  results = []
  if n.even? && k.odd? then
    results.fill(2, 0..k - 2)
    results_sum = results.reduce(0) {|sum, value| sum + value }
    results.push(n - results_sum)
    puts 'YES'
    puts results.join(' ')
  else
    results.fill(1, 0..k - 2)
    results_sum = results.reduce(0) {|sum, value| sum + value }
    results.push(n - results_sum)
    puts 'YES'
    puts results.join(' ')
  end

end

n = gets.chomp.to_i

tests = []

n.times do
  tests.push(gets.split(' ').map {|e| e.to_i})
end

tests.each do |test|
  n, k = test

  if n < k then
    puts 'NO'
    next
  end

  if n.odd? && k.even? then
    puts 'NO'
    next
  end

  if n - k == 1 && n.even? then
    puts 'NO'
    next
  end

  results = []

  results.fill(1, 0..k - 2)
  results_sum = results.reduce(0) {|sum, value| sum + value }
  results.push(n - results_sum)
  puts 'YES'
  puts results.join(' ')
end


MOD = 10 ** 9 + 7
def gs; gets.chomp; end
def gi; gets.to_i; end
def gli; gets.split.map(&:to_i); end
def cyesno(x); puts x ? :Yes : :No; end
def uyesno(x); puts x ? :YES : :NO; end
def array_to_hash_by_count(c); Hash.new(0).tap { |x| c.each { |z| x[z] += 1 } }; end

a = []
c = 0
i = 1;
while c < 1010
  unless i% 3 == 0 || i % 10 == 3
    a << i
    c += 1
  end
  i += 1
end

gets.to_i.times do
  g = gets.to_i
  puts a[g-1]
end

# 1 2 3 4 5 6 7 8 9 1000
# 1 2 4 5 7 8 10 11 14 1666

t = gets.to_i
k = []
(0..t-1).each do
  x = gets.to_i
  k.push(x)
end

k.each do |i|
  j = 1
  c = 0
  while true
    if j % 3 != 0 && j % 10 != 3
      c += 1
    end
    if c == i
      puts j
      break
    end
    j += 1
  end
  # p arr
  # puts arr.last
end

t = gets.to_i
a = []
i = 1
while a.length() < 1001
  if i % 3 != 0 && i.to_s[-1] != "3"
    a << i
  end
  i += 1
end
while t > 0
  t  -= 1
  n = gets.to_i
  puts a[n - 1]
end

def solve(k)
  cnt = 0
  ptr = 0
  while cnt < k do
    ptr += 1
    next if ptr % 3 == 0 || ptr%10 == 3
    cnt += 1
  end
  ptr
end

t = gets.chomp.to_i

t.times do
  k = gets.chomp.to_i
  puts "#{solve(k)}\n"
end

MOD = 10 ** 9 + 7
def gs; gets.chomp; end
def gi; gets.to_i; end
def gli; gets.split.map(&:to_i); end
def cyesno(x); puts x ? :Yes : :No; end
def uyesno(x); puts x ? :YES : :NO; end
def array_to_hash_by_count(c); Hash.new(0).tap { |x| c.each { |z| x[z] += 1 } }; end

a = []
c = 0
i = 1;
while c < 1010
  unless i% 3 == 0 || i % 10 == 0
    a << i
    c += 1
  end
  i += 1
end

gets.to_i.times do
  g = gets.to_i
  puts a[g-1]
end


#!/usr/bin/ruby1.9

n = STDIN.readline.to_i
words = []
n.times { words.push(STDIN.readline.strip) }

title = 'a'

while true
	okay = true
	n.times do |i|
		if words[i].index(title) != nil
			okay = false
			break
		end
	end
	if okay
		puts title
		exit
	end

	title = title.next
end

a = ''
n = gets.to_i.times{ a += (gets.to_s.chomp + ' ')}
s = 'a'
while a.index(s) != nil do
	s.next!
end
puts s

n=gets.to_i
s=[]
n.times do
    s<<gets.chomp
end

found=false
res=""
for i in 'a'..'z' do
    ok=true
    s.each do |v|
        ok=false if v.include?(i)
    end
    if !found and ok then
        res=i
        found=true
    end
end

for i in 'a'..'z' do
    for j in 'a'..'z' do
        t=i+j
        ok=true
        s.each do |v|
            ok=false if v.include?(t)
        end
        if !found and ok then
            res=t
            found=true
        end
    end
end
puts res

#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

# b.rb

def main()
    n = gets.to_i
    s = []
    n.times do |k|
        s << gets.chomp
    end

    l = 1
    while true do
        m = []
        n.times do |k|
            next if s[k].size < l
            (s[k].size - l + 1).times do |a|
                m << s[k][a, l]
            end
        end
        m = m.sort.uniq
        if m.size == 26 ** l then
            l += 1
            next
        end
        c = [*"a".."z"]
        (26 ** l).times do |i|
            ii = i
            ss = ""
            l.times do |b|
                ss = c[ii % 26] + ss
                ii = (ii - ii % 26) / 26
            end
            if !m.index(ss) then
                puts ss
                return
            end
        end
    end
end

if __FILE__ == $0 then
    main()
end

n=gets.chomp.to_i;
words=[]
for i in 0...n
    words[i]=gets.chomp
end
current="a"
loop do
    result=true
    words.each{|element|
        if element.include?(current)
            result=false
            break
        end
    }
    if result
        print current+"\n"
        break
    else
        current.succ!
    end
end

n = gets.to_i

@s = []
n.times do |i|
	@s[i] = gets
end

@alphabet = "a b c d e f g h i j k l m n o p q r s t u v w x y z".split(' ')

# allstring = s.join('')

def name_include?(str)
	@s.each do |i|
		return true if i.include?(str)
	end
	return false
end

def findname(myname)
	found = false
	@alphabet.each do |let|
		if !name_include?(let)
			myname += let
			found = true
			break
		end
	end
	if !found
		@alphabet.each do |let1|
			@alphabet.each do |let2|
				let = let1 + let2
				if !name_include?(let)
					myname += let
					found = true
					break
				end
			end
			break if found
		end
	end

	return myname
end

myname = findname("")

print myname

def main()
	n=gets.chomp.to_i
	ar=[]
	names=[]
	for i in 0...n
		s=gets.chomp
		names << s;
		for j in 0...(s.length)
			ch=s[j].chr
			if !ar.include?(ch)
				ar << ch;
			end
		end

	end
	alp=[*'a'..'z']
	ar=ar.sort
	if ar.join()==alp.join()
		j=1;f=0;
		while(f==0)
			alpr=alp.map {|i| i*j}
			for i in 0...26
				charc=alp[i]
				for j in 0...26
					newc=charc+(alpr[j])
					if !checkss(names,newc)
						f=1;
						puts newc;
						break;
					end
				end
				if f==1
					break;
				end
			end
		j+=1;
		end
	else
		rr=alp-ar
		puts rr[0]
	end
end

def checkss(names,s)
	for i in names
		if i.include?(s)
			return true;
		end
	end
	return false;
end
main

ns = gets.to_i.times.map { gets.chomp }
start = "a"
while ns.any? { |n| n.index(start) }
  start.succ!
end
puts start

n = gets.chomp.to_i
titles = []
(1..n).each{
   t = gets.chomp
   titles.push t
}
letters = ['']
letters += ('a'..'z').to_a

arr = []
letters.each { |l1|
  #arr.push l1
  letters.each { |l2|
    #arr.push "#{l1}#{l2}"
    letters.each { |l3|
	   arr.push  "#{l1}#{l2}#{l3}"
	}
  }
}
#puts "#{arr}"
arr.each { |t|
	ok = false
	titles.each { |title|
	    #puts "- #{title}"
		if title.include? t
		  ok = true
		end
	}
	if ok==false
		puts t
		break
	end
}

n = gets.to_i;
a = (0..n-1).inject([]) {|acc, y|
  s = gets.chomp
  b = (0..s.length-1).inject([]){ |ai, x|
    (x..s.length-1).inject(ai){ |aj, y|
      aj << s[x..y]
    }
  }
  acc + b
}.sort{ |x, y|
  if x.size < y.size
    -1
  elsif x.size > y.size
    1
  else
    if x <= y then -1 else 1 end
  end
}.uniq
ans="a"
a.each{|x|
  break if x != ans
  ans.succ!
}
puts ans

def titles(prefix = '', len)
  len -= 1
  block = (len == 0) ? lambda { |z| prefix + z } : lambda { |z| titles(prefix + z, len) }
  ('a'..'z').to_a.map &block
end

def solve(used_titles)
  (1..2).each { |i|
    titles(i).flatten.each { |title|
      return title if used_titles.all? { |used_title| !used_title.include? title }
    }
  }
end

if __FILE__ == $0
  puts solve((1..gets.to_i).map { gets.chomp })
end

def titles(prefix = '', len)
  len -= 1
  block = (len == 0) ? lambda { |z| prefix + z } : lambda { |z| titles(prefix + z, len) }
  ('a'..'z').to_a.map &block
end

def solve(used_titles)
  (1..3).each { |i|
    titles(i).flatten.each { |title|
      return title if used_titles.all? { |used_title| !used_title.include? title }
    }
  }
end

if __FILE__ == $0
  puts solve((1..gets.to_i).map { gets.chomp })
end

    def nl(x)
        case x[-1]
        when "a"
            return (x[0...-1] + "b")
        when "b"
            return (x[0...-1] + "c")
        when "c"
            return (x[0...-1] + "d")
        when "d"
            return (x[0...-1] + "e")
        when "e"
            return (x[0...-1] + "f")
        when "f"
            return (x[0...-1] + "g")
        when "g"
            return (x[0...-1] + "h")
        when "h"
            return (x[0...-1] + "i")
        when "i"
            return (x[0...-1] + "j")
        when "j"
            return (x[0...-1] + "k")
        when "k"
            return (x[0...-1] + "l")
        when "l"
            return (x[0...-1] + "m")
        when "m"
            return (x[0...-1] + "n")
        when "n"
            return (x[0...-1] + "o")
        when "o"
            return (x[0...-1] + "p")
        when "p"
            return (x[0...-1] + "q")
        when "q"
            return (x[0...-1] + "r")
        when "r"
            return (x[0...-1] + "s")
        when "s"
            return (x[0...-1] + "t")
        when "t"
            return (x[0...-1] + "u")
        when "u"
            return (x[0...-1] + "v")
        when "v"
            return (x[0...-1] + "w")
        when "w"
            return (x[0...-1] + "x")
        when "x"
            return (x[0...-1] + "y")
        when "y"
            return (x[0...-1] + "z")
        when "z"
            return nil
        end
    end

    def func(mas, st,n)
        ans = []
        for s in "a".."z" do
            t = true
            i = 0
            while t && i < n do
                if mas[i].include? (st + s) then
                    t = false
                end
                i += 1
            end
            if t then
                ans << (st + s)
            end
        end
        return ans
    end

    n = gets.to_i

    names = []
    n.times do
        names << gets.chomp
    end

    ans = func(names,"",n)
    st = "a"
    k = 1
    while ans.empty? do
        if !st then
            k += 1
            st = k*"a"
        end
        ans = func(names,st,n)
        st = nl(st)
    end

    puts ans.min

def get_str(x)
  abc = " abcdefghijklmnopqrstuvwxyz".split("")
  sn = x.to_s 27
  r = ""
  sn.split("").each do |l|
    r += abc[l.to_i 27]
  end
  r
end

tn = gets.chomp.to_i
titles = []
tn.times do
  titles.push(gets.chomp)
end

n = 0
while true do
  n+=1
  done = true
  if n%27 != 0 then
    str = get_str n
    titles.each do |ttl|
      if ttl.include? str then
        done = false
        break
      end
    end
    puts str if done
    break if done
  end
end

#!/usr/bin/ruby1.9

n = STDIN.readline.to_i
words = []
n.times { words.push(STDIN.readline.strip) }

title = ''

while true
	pos = title.length-1
	while true
		if pos == -1
			title = (title.length+1).times.collect { 'a' }.join
			break
		end
		if title[pos] < 'z'
			suffix = (title.length-1-pos).times.collect { 'a' }.join
			title = title.slice(0, pos) + title[pos].next + suffix
			break
		end
		pos -= 1
	end

	okay = true
	n.times do |i|
		if words[i].index(title) != nil
			okay = false
			break
		end
	end
	if okay
		puts title
		exit
	end
end

#!/usr/bin/ruby1.9

n = STDIN.readline.to_i
hash = {}
n.times do
	word = STDIN.readline.strip
	(0..word.length-1).each do |start|
		(1..word.length-start).each do |length|
			hash[word.slice(start, length)] = true
		end
	end
end

title = 'a'
title = title.next while hash[title] != nil
puts title

def get_str(x)
  abc = " abcdefghijklmnopqrstuvwxyz".split("")
  sn = x.to_s 27
  r = ""
  sn.split("").each do |l|
    r += abc[l.to_i 27]
  end
  r
end

n = gets.chomp.to_i
titles = []
n.times do
  titles.push(gets.chomp)
end

n = 0
while true do
  n+=1
  done = true
  if n%27 != 0 then
    str = get_str n
    titles.each do |ttl|
      puts ttl
      if ttl.include? str then
        done = false
        break
      end
    end
    puts str if done
    break if done
  end
end

#!/usr/bin/ruby

n = STDIN.readline.to_i

titles = []
n.times { titles.push(STDIN.readline.strip) }

answer = ''

while true
	('a'..'z').each do |ch|
		candidate = answer+ch
		okay = true
		titles.each do |title|
			if title.index(candidate) != nil
				okay = false
				break
			end
		end
		if okay
			puts candidate
			exit
		end
	end
	answer += 'a'
end

n = gets.to_i

s = []
n.times do |i|
	s[i] = gets
end

alphabet = "a b c d e f g h i j k l m n o p q r s t u v w x y z".split(' ')

myname = "idk"
found = false

allstring = s.join('')

alphabet.each do |let|
	if !allstring.include?(let)
		myname = let
		found = true
		break
	end
end

myname = "a" if !found
while !found do
	alphabet.each do |let|
		# p myname + let
		if !allstring.include?(myname + let)
			myname = myname + let
			found = true
			break
		end

		# myname += "a" if let = "z" && !found
	end
	myname += "a" if !found
end

p  myname

ns = gets.to_i.times.map { gets.chomp }
start = "a"
while ns.any? { |n| n.index(start) }
  start.succ!
end
p start


n = gets.to_i
a = []

def find_ans(s,w)
  i = 0
  time = 0
  while (i < w.length - 1)
    if (w[i] == w[i+1])
      time += 0
    else
      time += (s.index(w[i]) - s.index(w[i+1])).abs
    end
    i += 1
  end
  time
end

(0..n-1).each do |x|
	s = gets.chomp
  w = gets.chomp
	a.append(find_ans(s,w))
end

a.each do |x|
 p x
end

for _ in 0...gets.chomp.to_i
  keyboard = gets.chomp
  word = gets.chomp
  time = 0
  if word.length() <= 1
    puts time
  else
    for i in 0...(word.length() - 1)
      time += (keyboard.index(word[i]) - keyboard.index(word[i + 1])).abs
    end
    puts time
  end
end

arr = []
t = gets.chomp.to_i
index = 0
while t>index
    s1 = gets.chomp.chars
    s2 = gets.chomp.chars
    kol_s2 = s2.length.to_i-1
    summa =0;
    i=1;
    while kol_s2>=1
        if s2.length>1
            summa = summa + ((s1.index(s2[i]))-(s1.index(s2[i-1]))).abs
            kol_s2-=1
            i+=1
        else
            summa = 0
        end
    end
    arr[index] = summa
    index+=1
end
arr.each{|a| puts a}

gets.to_i.times do
  a=gets.chomp.chars
  h={}
  26.times do |i|
    h[a[i]]=i
  end
  c=0
  s=gets.chomp.chars
  (s.size-1).times do |i|
    c+=(h[s[i+1]]-h[s[i]]).abs
  end
  puts c
end

STDIN.read
  .split(/\n/)
  .drop(1)
  .map!(&:strip)
  .each_slice(2) do |alphabet, word|
    puts word
      .split("")
      .map{ |c| alphabet.index(c) }
      .each_cons(2)
      .map{|a, b| (a - b).abs}
      .sum
  end

t = gets.to_i
t.times do
    dist = {}
    keys = gets.chomp
    s = gets.chomp
    keys.each_char.with_index do |c, i|
        dist[c] = i
    end
    ans = 0
    s.chars.each_cons(2) do |c1, c2|
        ans += (dist[c1] - dist[c2]).abs
    end
    puts ans
end

gets.to_i.times do
  xs = gets.chomp.chars
  puts gets.chomp.chars.each_cons(2).map { |a, b| (xs.index(a) - xs.index(b)).abs }.sum
end