$$$n$$$ robots have escaped from your laboratory! You have to find them as soon as possible, because these robots are experimental, and their behavior is not tested yet, so they may be really dangerous!Fortunately, even though your robots have escaped, you still have some control over them. First of all, you know the location of each robot: the world you live in can be modeled as an infinite coordinate plane, and the $$$i$$$-th robot is currently located at the point having coordinates ($$$x_i$$$, $$$y_i$$$). Furthermore, you may send exactly one command to all of the robots. The command should contain two integer numbers $$$X$$$ and $$$Y$$$, and when each robot receives this command, it starts moving towards the point having coordinates ($$$X$$$, $$$Y$$$). The robot stops its movement in two cases:  either it reaches ($$$X$$$, $$$Y$$$);  or it cannot get any closer to ($$$X$$$, $$$Y$$$). Normally, all robots should be able to get from any point of the coordinate plane to any other point. Each robot usually can perform four actions to move. Let's denote the current coordinates of the robot as ($$$x_c$$$, $$$y_c$$$). Then the movement system allows it to move to any of the four adjacent points:  the first action allows it to move from ($$$x_c$$$, $$$y_c$$$) to ($$$x_c - 1$$$, $$$y_c$$$);  the second action allows it to move from ($$$x_c$$$, $$$y_c$$$) to ($$$x_c$$$, $$$y_c + 1$$$);  the third action allows it to move from ($$$x_c$$$, $$$y_c$$$) to ($$$x_c + 1$$$, $$$y_c$$$);  the fourth action allows it to move from ($$$x_c$$$, $$$y_c$$$) to ($$$x_c$$$, $$$y_c - 1$$$). Unfortunately, it seems that some movement systems of some robots are malfunctioning. For each robot you know which actions it can perform, and which it cannot perform.You want to send a command so all robots gather at the same point. To do so, you have to choose a pair of integer numbers $$$X$$$ and $$$Y$$$ so that each robot can reach the point ($$$X$$$, $$$Y$$$). Is it possible to find such a point?

The first line contains one integer $$$q$$$ ($$$1 \le q \le 10^5$$$) — the number of queries. Then $$$q$$$ queries follow. Each query begins with one line containing one integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of robots in the query. Then $$$n$$$ lines follow, the $$$i$$$-th of these lines describes the $$$i$$$-th robot in the current query: it contains six integer numbers $$$x_i$$$, $$$y_i$$$, $$$f_{i, 1}$$$, $$$f_{i, 2}$$$, $$$f_{i, 3}$$$ and $$$f_{i, 4}$$$ ($$$-10^5 \le x_i, y_i \le 10^5$$$, $$$0 \le f_{i, j} \le 1$$$). The first two numbers describe the initial location of the $$$i$$$-th robot, and the following four numbers describe which actions the $$$i$$$-th robot can use to move ($$$f_{i, j} = 1$$$ if the $$$i$$$-th robot can use the $$$j$$$-th action, and $$$f_{i, j} = 0$$$ if it cannot use the $$$j$$$-th action). It is guaranteed that the total number of robots over all queries does not exceed $$$10^5$$$.

You should answer each query independently, in the order these queries appear in the input. To answer a query, you should do one of the following:   if it is impossible to find a point that is reachable by all $$$n$$$ robots, print one number $$$0$$$ on a separate line;  if it is possible to find a point that is reachable by all $$$n$$$ robots, print three space-separated integers on the same line: $$$1$$$ $$$X$$$ $$$Y$$$, where $$$X$$$ and $$$Y$$$ are the coordinates of the point reachable by all $$$n$$$ robots. Both $$$X$$$ and $$$Y$$$ should not exceed $$$10^5$$$ by absolute value; it is guaranteed that if there exists at least one point reachable by all robots, then at least one of such points has both coordinates not exceeding $$$10^5$$$ by absolute value.

INF = 10**5
gets.to_i.times do
  l, r, d, u = -INF, INF, -INF, INF
  gets.to_i.times do
    x, y, f1, f2, f3, f4 = gets.split.map(&:to_i)
    l = [x, l].max if f1 == 0
    u = [y, u].min if f2 == 0
    r = [x, r].min if f3 == 0
    d = [y, d].max if f4 == 0
  end
  if r < l or u < d
    puts 0
  else
    puts [1, l, d].join ' '
  end
end

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 puts_yesno(can) puts(can ? 'Yes' : 'No') end
def putsend(s) puts s; exit end
def debug(k,v) puts "#{k}:#{v}" end
########### ( ˘ω˘ )ｽﾔｧ… ###########
q = gi
ans = []
MAX = 10**5
MIN = -10**5
q.times do |i|
  n = gi
  xf = n.times.map{gsmi}
  # x,y,xl,yu,xr,yd
  x_ran = [MIN,MAX]
  y_ran = [MIN,MAX]
  xf.each do |x,y,xl,yu,xr,yd|
    if xl==0&&xr==0
      unless x_ran[0]<=x && x<=x_ran[1]
        ans<<'0'
        break
      else
        x_ran = [x,x]
      end
    elsif xl==1&&xr==0
      if x<x_ran[0]
        ans<<'0'
        break
      else
        x_ran[1] = min(x_ran[1],x)
      end
    elsif xl==0&&xr==1
      if x_ran[1]<x
        ans<<'0'
        break
      else
        x_ran[0] = max(x_ran[0],x)
      end
    end

    # y
    if yd==0&&yu==0
      unless y_ran[0]<=y && y<=y_ran[1]
        ans<<'0'
        break
      else
        y_ran = [y,y]
      end
    elsif yd==1&&yu==0
      if y<y_ran[0]
        ans<<'0'
        break
      else
        y_ran[1] = min(y_ran[1],y)
      end
    elsif yd==0&&yu==1
      if y_ran[1]<y
        ans<<'0'
        break
      else
        y_ran[0] = max(y_ran[0],y)
      end
    end
  end

  ans << [1,x_ran[0],y_ran[0]].join(" ") if ans.size != i+1
end
puts ans


In this problem you have to implement an algorithm to defragment your hard disk. The hard disk consists of a sequence of clusters, numbered by integers from 1 to n. The disk has m recorded files, the i-th file occupies clusters with numbers ai, 1, ai, 2, ..., ai, ni. These clusters are not necessarily located consecutively on the disk, but the order in which they are given corresponds to their sequence in the file (cluster ai, 1 contains the first fragment of the i-th file, cluster ai, 2 has the second fragment, etc.). Also the disc must have one or several clusters which are free from files.You are permitted to perform operations of copying the contents of cluster number i to cluster number j (i and j must be different). Moreover, if the cluster number j used to keep some information, it is lost forever. Clusters are not cleaned, but after the defragmentation is complete, some of them are simply declared unusable (although they may possibly still contain some fragments of files).Your task is to use a sequence of copy operations to ensure that each file occupies a contiguous area of memory. Each file should occupy a consecutive cluster section, the files must follow one after another from the beginning of the hard disk. After defragmentation all free (unused) clusters should be at the end of the hard disk. After defragmenting files can be placed in an arbitrary order. Clusters of each file should go consecutively from first to last. See explanatory examples in the notes.Print the sequence of operations leading to the disk defragmentation. Note that you do not have to minimize the number of operations, but it should not exceed 2n.

The first line contains two integers n and m (1 ≤ n, m ≤ 200) — the number of clusters and the number of files, correspondingly. Next m lines contain descriptions of the files. The first number in the line is ni (ni ≥ 1), the number of clusters occupied by the i-th file. Then follow ni numbers ai, 1, ai, 2, ..., ai, ni (1 ≤ ai, j ≤ n). It is guaranteed that each cluster number occurs not more than once and , that is, there exists at least one unused cluster. Numbers on each line are separated by spaces.

In the first line print a single integer k (0 ≤ k ≤ 2n) — the number of operations needed to defragment the disk. Next k lines should contain the operations' descriptions as "i j" (copy the contents of the cluster number i to the cluster number j).

NoteLet's say that a disk consists of 8 clusters and contains two files. The first file occupies two clusters and the second file occupies three clusters. Let's look at examples of correct and incorrect positions of files after defragmentation. Example 2: each file must occupy a contiguous area of memory.Example 3: the order of files to each other is not important, at first the second file can be written, and then — the first one.Example 4: violating the order of file fragments to each other is not allowed.Example 5: unused clusters should be located at the end, and in this example the unused clusters are 3, 7, 8.

#!/usr/bin/ruby

# move sects[cur] to correct position using temporary
# return tmpidx
def move(sects, target, cur, tmp, acc)
  return tmp if sects[cur] == target[cur]
  return tmp if cur == tmp
  return tmp if sects[cur].nil?

  to = target.index(sects[cur])
  acc << [to, tmp] if to != tmp
  sects[tmp] = sects[to]

  acc << [cur, to]
  sects[to] = sects[cur]
  move(sects, target, tmp, cur, acc)
end

n, m = gets.split.map(&:to_i)
sects = Array.new(n, nil)
len = 0
target = []
m.times do |i|
  k, *as = gets.split.map(&:to_i)
  as.map{|v| v-1}.each_with_index do |a, j|
    sects[a] = [i,j]
    target << [i,j]
  end
  len = [len, as.max].max
end
target.freeze

tmpidx = sects.index(nil)

ans = []
cur = 0
while cur < len
  if cur != tmpidx and sects[cur] and sects[cur] != target[cur]
    tmpidx = move(sects, target, cur, tmpidx, ans)
  end
  cur += 1
end

puts ans.size
ans.each do |v|
  puts v.map{|a| a+1}.join(' ')
end

input = gets.chomp.split
n = Integer(input[0])
m = Integer(input[1])
cluster = []
for i in 0...m
    cluster[i] = []
    input = gets.chomp.split
    for j in 1...input.length
        cluster[i][j-1] = Integer(input[j]) - 1
    end
end

memory = Array.new(n) { |i| -1 }
pos = Array.new(n) { |i| -1 }
cursor = 0
for i in 0...cluster.length
    for j in 0...cluster[i].length
        memory[cluster[i][j]] = cursor
        pos[cursor] = cluster[i][j]
        cursor += 1
    end
end
len = cursor

empty = 0
for i in 0...n
    if memory[i] == -1
        empty = i
        break
    end
end

ops = []
cursor = 0
for i in 0...len
    if memory[i] != i
        if i != empty
            ops.push [i, empty]
            memory[empty] = memory[i]
            pos[memory[i]] = empty
        end

        ops.push [pos[i], i]
        memory[i] = i
        empty = pos[i]
        pos[i] = i
    end
end

puts ops.length
for i in 0...ops.length
    s, t = ops[i]
    print s + 1, " ", t + 1
    puts
end

input = gets.chomp.split
n = Integer(input[0])
m = Integer(input[1])
cluster = []
for i in 0...m
    cluster[i] = []
    input = gets.chomp.split
    for j in 1...input.length
        cluster[i][j-1] = Integer(input[j]) - 1
    end
end

memory = Array.new(n) { |i| -1 }
pos = Array.new(n) { |i| -1 }
cursor = 0
for i in 0...cluster.length
    for j in 0...cluster[i].length
        memory[cluster[i][j]] = cursor
        pos[cursor] = cluster[i][j]
        cursor += 1
    end
end
len = cursor

empty = 0
for i in 0...n
    if memory[i] == -1
        empty = i
        break
    end
end

ops = []
cursor = 0
for i in 0...len
    if memory[i] != i
        if i != empty
            ops.push [i, empty]
            memory[empty] = memory[i]
            pos[memory[i]] = empty
        end

        ops.push [pos[i], i]
        empty = pos[i]
    end
end

puts ops.length
for i in 0...ops.length
    s, t = ops[i]
    print s + 1, " ", t + 1
    puts
end

input = gets.chomp.split
n = Integer(input[0])
m = Integer(input[1])
cluster = []
for i in 0...m
    cluster[i] = []
    input = gets.chomp.split
    for j in 0...input.length
        cluster[i][j] = Integer(input[j])
    end
end

before_defrag = []
for i in 0...n
    before_defrag[i] = 0
end
after_defrag = []
k = 0
while k < m do
    for i in 0...m
        for j in 0...cluster[i][0]
            after_defrag.push((k+1) * 1000 + j + 1)
        end
        k += 1
    end
end
length = after_defrag.length
(n - length).times {after_defrag.push(0)}
for i in 0...m
    for j in 1..cluster[i][0]
        before_defrag[cluster[i][j]-1] = (i+1) * 1000 + j
    end
end
empty = 0
for i in 0...n
    if before_defrag[i] == 0
        empty = i
        break
    end
end
count = 0
operation = []
for i in 0...length
    if before_defrag[i] != after_defrag[i]
        if empty != i
            operation.push([i+1, empty+1])
            count += 1
            before_defrag[empty] = before_defrag[i]
        end
        for j in i+1...n
            if before_defrag[j] == after_defrag[i]
                empty = j
                operation.push([empty+1, i+1])
                count += 1
                before_defrag[i] = before_defrag[empty]
            end
        end
    end
end
puts count
if operation != []
    for i in 0...operation.length
        print operation[i][0].to_s + " " + operation[i][1].to_s
        puts
    end
end

#!/usr/bin/ruby

# move sects[cur] to correct position using temporary
# return tmpidx
def move(sects, target, cur, tmp, acc)
  return tmp if sects[cur] == target[cur]

  to = target.index(sects[cur])
  acc << [to, tmp] if to != tmp
  sects[tmp] = sects[to]

  acc << [cur, to]
  sects[to] = sects[cur]
  move(sects, target, tmp, cur, acc)
end

n, m = gets.split.map(&:to_i)
sects = Array.new(n, nil)
len = 0
target = []
m.times do |i|
  k, *as = gets.split.map(&:to_i).map{|v| v-1}
  as.each_with_index do |a, j|
    sects[a] = [i,j]
    target << [i,j]
  end
  len += k
end
target.freeze

tmpidx = sects.index(nil)

ans = []
cur = 0
while cur < len
  if sects[cur] != target[cur]
    tmpidx = move(sects, target, cur, tmpidx, ans)
  end
  cur += 1
end

puts ans.size
ans.each do |v|
  puts v.map{|a| a+1}.join(' ')
end

input = gets.chomp.split
n = Integer(input[0])
m = Integer(input[1])
cluster = []
for i in 0...m
    cluster[i] = []
    input = gets.chomp.split
    for j in 1...input.length
        cluster[i][j-1] = Integer(input[j]) - 1
    end
end

memory = Array.new(n) { |i| -1 }
pos = Array.new(n) { |i| -1 }
cursor = 0
for i in 0...cluster.length
    for j in 0...cluster[i].length
        memory[cluster[i][j]] = cursor
        pos[cursor] = cluster[i][j]
        cursor += 1
    end
end
len = cursor

empty = 0
for i in 0...n
    if memory[i] == -1
        empty = i
        break
    end
end

ops = []
cursor = 0
for i in 0...len
    if memory[i] != i
        if i != empty
            ops.push [i, empty]
            pos[memory[i]] = empty
        end

        ops.push [pos[i], i]
        empty = pos[i]
    end
end

puts ops.length
for i in 0...ops.length
    s, t = ops[i]
    print s + 1, " ", t + 1
    puts
end

input = gets.chomp.split
n = Integer(input[0])
m = Integer(input[1])
cluster = []
for i in 0...m
    cluster[i] = []
    input = gets.chomp.split
    for j in 1...input.length
        cluster[i][j-1] = Integer(input[j]) - 1
    end
end

memory = Array.new(n) { |i| -1 }
pos = Array.new(n) { |i| -1 }
cursor = 0
for i in 0...cluster.length
    for j in 0...cluster[i].length
        memory[cluster[i][j]] = cursor
        pos[cursor] = cluster[i][j]
        cursor += 1
    end
end
len = cursor

empty = 0
for i in 0...n
    if memory[i] == -1
        empty = i
        break
    end
end

ops = []
cursor = 0
for i in 0...len
    if memory[i] != i
        ops.push [i, empty]
        memory[empty] = memory[i]
        pos[memory[i]] = empty

        ops.push [pos[i], i]
        memory[i] = i
        empty = pos[i]
        pos[i] = i
    end
end

puts ops.length
for i in 0...ops.length
    s, t = ops[i]
    print s + 1, " ", t + 1
    puts
end

input = gets.chomp.split
n = Integer(input[0])
m = Integer(input[1])
cluster = []
for i in 0...m
    cluster[i] = []
    input = gets.chomp.split
    for j in 0...input.length
        cluster[i][j] = Integer(input[j])
    end
end

before_defrag = []
for i in 0...n
    before_defrag[i] = 0
end
after_defrag = []
k = 0
while k < m do
    for i in 0...m
        for j in 0...cluster[i][0]
            after_defrag.push((k+1) * 10 + j + 1)
        end
        k += 1
    end
end
length = after_defrag.length
(n - length).times {after_defrag.push(0)}
for i in 0...m
    for j in 1..cluster[i][0]
        before_defrag[cluster[i][j]-1] = (i+1) * 10 + j
    end
end
empty = 0
for i in 0...n
    if before_defrag[i] == 0
        empty = i
        break
    end
end
count = 0
operation = []
for i in 0...length
    if before_defrag[i] != after_defrag[i]
        if empty != i
            operation.push([i+1, empty+1])
            count += 1
            before_defrag[empty] = before_defrag[i]
        end
        for j in i+1...n
            if before_defrag[j] == after_defrag[i]
                empty = j
                operation.push([empty+1, i+1])
                count += 1
                before_defrag[i] = before_defrag[empty]
            end
        end
    end
end
puts count
if operation != []
    for i in 0...operation.length
        print operation[i][0].to_s + " " + operation[i][1].to_s
        puts
    end
end

input = gets.chomp.split
n = Integer(input[0])
m = Integer(input[1])
cluster = []
for i in 0...m
    cluster[i] = []
    input = gets.chomp.split
    for j in 1...input.length
        cluster[i][j-1] = Integer(input[j]) - 1
    end
end

memory = Array.new(n) { |i| -1 }
pos = Array.new(n) { |i| -1 }
cursor = 0
for i in 0...cluster.length
    for j in 0...cluster[i].length
        memory[cluster[i][j]] = cursor
        pos[cursor] = cluster[i][j]
        cursor += 1
    end
end
len = cursor

empty = 0
for i in 0...n
    if memory[i] == -1
        empty = i
        break
    end
end

ops = []
cursor = 0
for i in 0...len
    if i != empty && memory[i] != -1
        ops.push [i, empty]
        pos[memory[i]] = empty
    end
    if pos[i] != i
        ops.push [pos[i], i]
        empty = pos[i]
    end
end

puts ops.length
for i in 0...ops.length
    s, t = ops[i]
    print s + 1, " ", t + 1
    puts
end

input = gets.chomp.split
n = Integer(input[0])
m = Integer(input[1])
cluster = []
for i in 0...m
    cluster[i] = []
    input = gets.chomp.split
    for j in 1...input.length
        cluster[i][j-1] = Integer(input[j]) - 1
    end
end

memory = Array.new(n) { |i| -1 }
pos = Array.new(n) { |i| -1 }
cursor = 0
for i in 0...cluster.length
    for j in 0...cluster[i].length
        memory[cluster[i][j]] = cursor
        pos[cursor] = cluster[i][j]
        cursor += 1
    end
end
len = cursor

empty = 0
for i in 0...n
    if memory[i] == -1
        empty = i
        break
    end
end

ops = []
cursor = 0
for i in 0...len
    if i != empty
        ops.push [i, empty]
        pos[memory[i]] = empty
    end
    if pos[i] != i
        ops.push [pos[i], i]
        empty = pos[i]
    end
end

puts ops.length
for i in 0...ops.length
    s, t = ops[i]
    print s + 1, " ", t + 1
    puts
end


It's holiday. Mashmokh and his boss, Bimokh, are playing a game invented by Mashmokh. In this game Mashmokh writes sequence of n distinct integers on the board. Then Bimokh makes several (possibly zero) moves. On the first move he removes the first and the second integer from from the board, on the second move he removes the first and the second integer of the remaining sequence from the board, and so on. Bimokh stops when the board contains less than two numbers. When Bimokh removes numbers x and y from the board, he gets gcd(x, y) points. At the beginning of the game Bimokh has zero points.Mashmokh wants to win in the game. For this reason he wants his boss to get exactly k points in total. But the guy doesn't know how choose the initial sequence in the right way. Please, help him. Find n distinct integers a1, a2, ..., an such that his boss will score exactly k points. Also Mashmokh can't memorize too huge numbers. Therefore each of these integers must be at most 109.

The first line of input contains two space-separated integers n, k (1 ≤ n ≤ 105; 0 ≤ k ≤ 108).

If such sequence doesn't exist output -1 otherwise output n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).

Notegcd(x, y) is greatest common divisor of x and y.

n ,k = gets.split(' ').map { |x| x.to_i }
arr = Array.new

if ( k < (n/2).floor)
	puts '-1'
	exit
end
arr[0] = k - ((n-2)/2).floor
arr[1] = arr[0]*2
for i in 2..n-1
	arr[i] = arr[i-1]+1
end

if n == 1 && k != 0
	puts '-1'
	exit
elsif n == 1 && k == 0
	puts '1'
	exit
end

arr.each do|x|
	print x
	print ' '
	end

n, m = gets.split.map &:to_i
if n == 1
	puts m == 0 ? '1' : '-1'
	exit
end
one = n / 2 - 1
noone = m - one
if noone <= 0
	puts '-1'
	exit
end
ans = []
use = {}
if noone > 0
	ans << noone
	ans << noone * 2
	use[noone] = true
	use[noone * 2] = true
end
if one > 0
	1.step(1000000000,2) do |i|
		if !use[i] && !use[i + 1]
			ans << i
			ans << i + 1
			one -= 1
			break if one == 0
		end
	end
end
ans << 1000000000 if ans.size < n
flag = false
ans.each do |x|
	flag = true if x > 1000000000
end
puts flag ? '-1' : ans.join(' ')

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

pairs = n / 2

remaining = k - (pairs - 1)
if n == 1
	ans = k == 0 ? [1] : [-1]
elsif remaining > 0
	ans = []
	x = remaining * 2 + 2
	((pairs-1)*2).times do
		ans << x
		x += 1
	end
	ans << remaining << remaining * 2
	one_off = ans[-1] + 1
	ans << one_off if n % 2 != 0
else
	ans = [-1]
end

puts ans.join(" ")

n, k = gets.split.collect{|i| i.to_i}
if n/2 > k
    puts -1
elsif n == 1
    if k > 0
        puts -1
    else
        puts 1
    end
else
    ans = Array.new(n)
    ans[0] = k-n/2+1
    ans[1] = 2*ans[0]
    for i in 2...n
        ans[i] = ans[i-1]+1
    end
    puts ans.join(' ')
end

n, k = gets.split.collect{|i| i.to_i}
if n/2 > k
    puts -1
elsif n == 1
    puts -1 if k > 0
    puts 1 if k == 0
else
    ans = Array.new(n)
    ans[0] = k-n/2+1
    ans[1] = 2*ans[0]
    for i in 2...n
        ans[i] = ans[i-1]+1
    end
    puts ans.join(' ')
end

require 'set'

n, k = gets.split.map(&:to_i)
used = Set.new
ans = []

if k == 0 and n == 1
  p 1
  exit
elsif n == 1
  if k == 0
    p 1
  else
    p (-1)
  end
  exit
elsif n / 2 > k
  p (-1)
  exit
end

m = k - (n - 2) / 2
ans << m
ans << m * 2
used << m
used << m * 2
n -= 2

c = 1

while n >= 2
  a = c
  b = c + 1
  c += 2
  while used.member?(a) || used.member?(b)
    a = c
    b = c + 1
    c += 2
  end
  ans << a
  ans << b
  n -= 2
end

if n == 1
  a = c
  while used.member?(a) || used.member?(b)
    c += 1
    a = c
  end
  ans << a
end

puts ans.join(' ')

n ,k = gets.split(' ').map { |x| x.to_i }
arr = Array.new

if ( k < n/2)
	puts '-1'
	exit
end
arr[0] = k - (n-2)/2
arr[1] = arr[0]*2
for i in 2..n-1
	arr[i] = arr[i-1]+1
end

arr.each do|x|
	print x
	print ' '
	end

n ,k = gets.split(' ').map { |x| x.to_i }
arr = Array.new

if ( k < n/2)
	puts '-1'
	exit
end
arr[0] = k - (n-2)/2
arr[1] = arr[0]*2
for i in 2..n-1
	arr[i] = arr[i-1]*2
end

arr.each do|x|
	print x
	print ' '
	end

n ,k = gets.split(' ').map { |x| x.to_i }
arr = Array.new

if ( k < n/2)
	puts '-1'
	exit
end
arr[0] = k - (n-2)/2
arr[1] = arr[0]*2
for i in 2..n
	arr[i] = arr[i-1]*2
end

arr.each do|x|
	print x
	print ' '
	end

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

pairs = n / 2

remaining = k - (pairs - 1)
if n == 1
	ans = k == 0 ? [1] : [-1]
elsif remaining > 0
	ans = []
	x = remaining * 2 + 1
	((pairs-1)*2).times do
		ans << x
		x += 1
	end
	one_off = ans[-1] + 1 if ans[-1]
	ans << remaining << remaining * 2
	ans << one_off if n % 2 != 0
else
	ans = [-1]
end

puts ans.join(" ")

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

pairs = n / 2

remaining = k - (pairs - 1)

if n > 1 && remaining > 0
	ans = []
	x = remaining * 2 + 1
	((pairs-1)*2).times do
		ans << x
		x += 1
	end
	one_off = ans[-1] + 1
	ans << remaining << remaining * 2
	ans << one_off if n % 2 != 0
else
	ans = [-1]
end

puts ans.join(" ")

n, k = gets.split.collect{|i| i.to_i}
if n/2 > k
    puts -1
else
    ans = Array.new(n)
    ans[0] = k-n/2+1
    ans[1] = 2*ans[0]
    for i in 2...n
        ans[i] = ans[i-1]+1
    end
    puts ans.join(' ')
end

n, k = gets.split.map(&:to_i)
if k < n / 2
	puts -1
	exit 0
end
m = n / 2 - 1
s = "1 1 " * m + (k - m).to_s + " " + (k - m).to_s
s = s + " 1" if n % 2 == 1
puts s

require 'set'

n, k = gets.split.map(&:to_i)
used = Set.new
ans = []
c = 1

if n / 2 > k
  p (-1)
  exit
end

while n > 1
  m = k - (n - 2) / 2

  while true

    if m == 1
      a = c
      b = c + 1
      c += 2
      while used.member?(b) || used.member?(a)
        a = c
        b = c + 1
        c += 2
      end
    else
      a = m
      b = 2 * m
    end

    if used.member?(b) || used.member?(a)
      m -= 1
      next
    else
      break
    end
  end

  used << a
  used << b
  ans << a
  ans << b
  k -= a.gcd(b)
  # p [[a, b], a.gcd(b)]

  n -= 2
end

while n > 0
  while used.member?(c)
    c += 1
  end
  ans << c
  n = 0
end

puts ans.join(' ')

require 'set'

n, k = gets.split.map(&:to_i)
used = Set.new
ans = []

if k == 0 and n == 1
  p 1
  exit
elsif n / 2 > k
  p (-1)
  exit
end

m = k - (n - 2) / 2
ans << m
ans << m * 2
used << m
used << m * 2
n -= 2

c = 1

while n >= 2
  a = c
  b = c + 1
  c += 2
  while used.member?(a) || used.member?(b)
    a = c
    b = c + 1
    c += 2
  end
  ans << a
  ans << b
  n -= 2
end

if n == 1
  a = c
  while used.member?(a) || used.member?(b)
    c += 1
    a = c
  end
  ans << a
end

puts ans.join(' ')

n ,k = gets.split(' ').map { |x| x.to_i }
arr = Array.new

if ( k < n/2)
	puts '-1'
	exit
end
arr[0] = k - (n-2)/2
arr[1] = arr[0]*2
for i in 2..n-1
	arr[i] = arr[i-1]+1
end

if n == 1
	puts '-1'
	exit
	end

arr.each do|x|
	print x
	print ' '
	end

n ,k = gets.split(' ').map { |x| x.to_i }
arr = Array.new

if ( k < n/2)
	puts '-1'
	exit
end
arr[0] = k - (n-2)/2
arr[1] = arr[0]*2
arr[2] = 1
for i in 3..n-1
	arr[i] = arr[i-1]+1
end

if n == 1
	puts '-1'
	exit
	end

arr.each do|x|
	print x
	print ' '
	end

n ,k = gets.split(' ').map { |x| x.to_i }
arr = Array.new

if ( k < (n/2).floor)
	puts '-1'
	exit
end
arr[0] = k - ((n-2)/2).floor
arr[1] = arr[0]*2
for i in 2..n-1
	arr[i] = arr[i-1]+1
end

if n == 1
	puts '-1'
	exit
	end

arr.each do|x|
	print x
	print ' '
	end

require 'set'

n, k = gets.split.map(&:to_i)
used = Set.new
ans = []

if n / 2 > k

  if n == 1
    p 1
  else
    p (-1)
  end
  exit
end

m = k - (n - 2) / 2
ans << m
ans << m * 2
used << m
used << m * 2
n -= 2

c = 1

while n >= 2
  a = c
  b = c + 1
  c += 2
  while used.member?(a) || used.member?(b)
    a = c
    b = c + 1
    c += 2
  end
  ans << a
  ans << b
  n -= 2
end

if n == 1
  a = c
  while used.member?(a) || used.member?(b)
    c += 1
    a = c
  end
  ans << a
end

puts ans.join(' ')

require 'set'

n, k = gets.split.map(&:to_i)
used = Set.new
ans = []

if n / 2 > k
  p (-1)
  exit
end

m = k - (n - 2) / 2
ans << m
ans << m * 2
used << m
used << m * 2
n -= 2

c = 1

while n >= 2
  a = c
  b = c + 1
  c += 2
  if used.member?(a) || used.member?(b)
    a = c
    b = c + 1
    c += 2
  end
  ans << a
  ans << b
  n -= 2
end

if n == 1
  a = c
  if used.member?(a) || used.member?(b)
    c += 1
    a = c
  end
  ans << a
end

puts ans.join(' ')

require 'set'

n, k = gets.split.map(&:to_i)
used = Set.new
ans = []

if n / 2 > k
  p (-1)
  exit
end

m = k - (n - 2) / 2
ans << m
ans << m * 2
used << m
used << m * 2
n -= 2

c = 1

while n >= 2
  a = c
  b = c + 1
  c += 2
  while used.member?(a) || used.member?(b)
    a = c
    b = c + 1
    c += 2
  end
  ans << a
  ans << b
  n -= 2
end

if n == 1
  a = c
  while used.member?(a) || used.member?(b)
    c += 1
    a = c
  end
  ans << a
end

puts ans.join(' ')


Suppose there is a $$$h \times w$$$ grid consisting of empty or full cells. Let's make some definitions:  $$$r_{i}$$$ is the number of consecutive full cells connected to the left side in the $$$i$$$-th row ($$$1 \le i \le h$$$). In particular, $$$r_i=0$$$ if the leftmost cell of the $$$i$$$-th row is empty.  $$$c_{j}$$$ is the number of consecutive full cells connected to the top end in the $$$j$$$-th column ($$$1 \le j \le w$$$). In particular, $$$c_j=0$$$ if the topmost cell of the $$$j$$$-th column is empty. In other words, the $$$i$$$-th row starts exactly with $$$r_i$$$ full cells. Similarly, the $$$j$$$-th column starts exactly with $$$c_j$$$ full cells.    These are the $$$r$$$ and $$$c$$$ values of some $$$3 \times 4$$$ grid. Black cells are full and white cells are empty. You have values of $$$r$$$ and $$$c$$$. Initially, all cells are empty. Find the number of ways to fill grid cells to satisfy values of $$$r$$$ and $$$c$$$. Since the answer can be very large, find the answer modulo $$$1000000007\,(10^{9} + 7)$$$. In other words, find the remainder after division of the answer by $$$1000000007\,(10^{9} + 7)$$$.

The first line contains two integers $$$h$$$ and $$$w$$$ ($$$1 \le h, w \le 10^{3}$$$) — the height and width of the grid. The second line contains $$$h$$$ integers $$$r_{1}, r_{2}, \ldots, r_{h}$$$ ($$$0 \le r_{i} \le w$$$) — the values of $$$r$$$. The third line contains $$$w$$$ integers $$$c_{1}, c_{2}, \ldots, c_{w}$$$ ($$$0 \le c_{j} \le h$$$) — the values of $$$c$$$.

NoteIn the first example, this is the other possible case.  In the second example, it's impossible to make a grid to satisfy such $$$r$$$, $$$c$$$ values.In the third example, make sure to print answer modulo $$$(10^9 + 7)$$$.

h,w = gets.split.map(&:to_i)
r = gets.split.map(&:to_i)
c = gets.split.map(&:to_i)

cell = []
h.times { cell<<Array.new(w) }

h.times {|i|
  r[i].times{|j|
    cell[i][j] = true
  }
  if r[i] < w
    cell[i][r[i]] = false
  end
}
w.times {|j|
  c[j].times{|i|
    if cell[i][j] == false
      p 0
      exit
    end
    cell[i][j] = true
  }
  if c[j] < h
    if cell[c[j]][j] == true
      p 0
      exit
    end
    cell[c[j]][j] = false
  end
}

freedom = cell.map {|cell_i|
  cell_i.count(nil)
}.inject(:+)

p (2**freedom) % 1000000007

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
########### ( ˘ω˘ )ｽﾔｧ… ###########
h,w=gsmi
r=gsmi
c=gsmi

# check
h.times do |i|
  tar = r[i]
  next if tar==w
  if c[tar]>i
    putsend 0
  end
end
w.times do |i|
  tar = c[i]
  next if tar==h
  if r[tar]>i
    putsend 0
  end
end

# cnt
h.times do |i|
  w.times do |j|
    cnt+=1 if r[i]<j && c[j]<i
  end
end

puts 2**cnt % MOD

n, m = readline.split.map(&:to_i)
l1 = readline.split.map(&:to_i)
l2 = readline.split.map(&:to_i)
a = Array.new(n, Array.new(m, 0))
for i in 0..n-1 do a[i] = Array.new(m, 0) end
for i in 0..n-1
	for j in 0..l1[i]-1
		a[i][j] = 1
	end
end

for j in 0..m-1
	if l2[j]<n && a[l2[j]][j] == 1
		puts 0
		exit(0)
	end
	for i in 0..l2[j]-1
		a[i][j] = 1
	end
end
for i in 0..n-1
	if l1[i]<m && a[i][l1[i]] == 1
		puts 0
		exit(0)
	end
end
for i in 0..n-1
	if l1[i] < m then l1[i] += 1 end
	for j in 0..l1[i]-1
		a[i][j] = 1
	end
end
for j in 0..m-1
	if l2[j] < n then l2[j] += 1 end
	for i in 0..l2[j]-1
		a[i][j] = 1
	end
end
ans = 1
mod = 1000000007
for i in 0..n-1
	for j in 0..m-1
		if a[i][j] == 0
			ans = ans * 2 % mod
		end
	end
end
puts ans

h,w = gets.split.map(&:to_i)
r = gets.split.map(&:to_i)
c = gets.split.map(&:to_i)

cell = []
h.times { cell<<Array.new(w) }

h.times {|i|
  r[i].times{|j|
    cell[i][j] = true
  }
  cell[i][r[i]] = false if r[i] < w
}
w.times {|j|
  c[j].times{|i|
    if cell[i][j] == false
      p 0
      exit
    end
    cell[i][j] = true
  }
  cell[c[j]][j] = false if c[j] < h
}

freedom = cell.map {|cell_i|
  cell_i.count(nil)
}.inject(:+)

p (2**freedom) % 1000000007


A team quiz game called "What? Where? When?" is very popular in Berland. The game is centered on two teams competing. They are the team of six Experts versus the team of the Audience. A person from the audience asks a question and the experts are allowed a minute on brainstorming and finding the right answer to the question. All it takes to answer a typical question is general knowledge and common logic. The question sent be the audience are in envelops lain out in a circle on a round table. Each envelop is marked by the name of the asker's town. Each question is positioned in a separate sector. In the centre of the table is a spinning arrow. Thus, the table rather resembles a roulette table with no ball but with a spinning arrow instead. The host sets off the spinning arrow to choose a question for the experts: when the arrow stops spinning, the question it is pointing at is chosen. If the arrow points at the question that has already been asked, the host chooses the next unanswered question in the clockwise direction. Your task is to determine which will be the number of the next asked question if the arrow points at sector number k.

The first line contains two positive integers n and k (1 ≤ n ≤ 1000 and 1 ≤ k ≤ n) — the numbers of sectors on the table and the number of the sector where the arrow is pointing. The second line contains n numbers: ai = 0 if the question from sector i has already been asked and ai = 1 if the question from sector i hasn't been asked yet (1 ≤ i ≤ n). The sectors are given in the clockwise order, the first sector follows after the n-th one.

Print the single number — the number of the sector containing the question the experts will be asked. It is guaranteed that the answer exists, that is that not all the questions have already been asked.

f = File.open('input.txt', 'r') or die "Unable to open file..."
input = []  # start with an empty array
  f.each_line {|line|
    input.push line
  }

selected = input[0].scan(/\w+/)[1].to_i - 1
items = input[1].scan(/\w+/)

while items[selected].to_i != 1 do
 (selected < items.length)? selected += 1 : selected = 0
end

if (items[selected].to_i == 1)
   File.open('output.txt', 'w') { |o| o.puts "#{selected+1}"}
end

n, k, a = nil
File.open("input.txt") do |f|
  n, k = f.gets.split.map(&:to_i)
  a = f.gets.split.map(&:to_i)
end

a += a
r = (k..k+n).find{|x| a[x-1] == 1}
r -= n if r > n

File.open("output.txt", "w") do |f|
  f.puts r
end

f = File.open('input.txt', 'r') or die "Unable to open file..."
input = []  # start with an empty array
  f.each_line {|line|
    input.push line
  }

selected = input[0].scan(/\w+/)[1].to_i
items = input[1].scan(/\w+/)

until items[selected].to_i == 1 do
 (selected < items.length)? selected += 1 : selected = 0
 if (items[selected].to_i == 1)
   File.open('output.txt', 'w') { |o| o.puts "#{selected+1}"}
 end
end


On a history lesson the teacher asked Vasya to name the dates when n famous events took place. He doesn't remembers the exact dates but he remembers a segment of days [li, ri] (inclusive) on which the event could have taken place. However Vasya also remembers that there was at most one event in one day. Help him choose such n dates of famous events that will fulfill both conditions. It is guaranteed that it is possible.

The first line contains one integer n (1 ≤ n ≤ 100) — the number of known events. Then follow n lines containing two integers li and ri each (1 ≤ li ≤ ri ≤ 107) — the earliest acceptable date and the latest acceptable date of the i-th event.

Print n numbers — the dates on which the events took place. If there are several solutions, print any of them. It is guaranteed that a solution exists.

class Event
	def initialize(st, en, id)
		@st = st
		@en = en
		@id = id
		@used = 0
	end
	attr_accessor :st, :en, :id, :used
end

n = gets.chop.to_i

aused = Array.new(1000000)
ar = Array.new
for i in 0..n-1
	s,e = gets.chomp.split(/ /).map!{|x| x.to_i}
	ar.push(Event.new(s,e,i))
end

ar.sort!{|a,b|
	if a.en == b.en
		a.st <=> b.st
	else
		a.en <=> b.en
	end
}

for i in 0..n-1
	st = ar[i].st
	while aused[st-1] == true
		st += 1
	end
	aused[st-1] = true
	ar[i].used = st
end

ret = ""
ar.sort!{|a,b|
	a.id <=> b.id
}
for i in 0..n-1
	ret << " " if i > 0
	ret << ar[i].used.to_s
end
puts ret

class Segment
  attr_accessor :from, :to

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

  def <=> (rhs)
    return -1 if from < rhs.from
    return to <=> rhs.to if from == rhs.from
    return +1
  end
end

def max(a, b)
  a > b ? a : b
end

n = gets.to_i
segs = Array.new(n) { Segment.new(*gets.split.map { |v| v.to_i }) }
result = Array.new(n)
order = (0 ... n).to_a

while not order.empty?
  order = order.sort_by { |i| segs[i] }
  cur = order.shift
  result[cur] = segs[cur].from
  order.each do |index|
    segs[index].from = max(segs[index].from, segs[cur].from + 1)
  end
end

puts result.join(' ')

n=gets.to_i
a,b,c,d={},0,-1,[0]*n
1.upto(n){a[c+=1]=gets.split.map(&:to_i)}
n.times{|i|
	x,t=a.to_a.sort_by{|x,(y,z)|(10**7+1)*[y,b+1].max+z}.shift
	y,z=t
	b=b<y ? y : b+=1
	d[x]=b
	a.delete x
}
puts d*" "

class Event
	def initialize(st, en)
		@st = st
		@en = en
		@used = false
	end
	attr_accessor :st, :en, :used
end

n = gets.chop.to_i

ar = Array.new
for i in 0..n-1
	s,e = gets.chomp.split(/ /).map!{|x| x.to_i}
	ar.push(Event.new(s,e))
end

ar.sort!{|a,b|
	if a.st == b.st
		a.en <=> b.en
	else
		a.st <=> b.st
	end
}

ret = ""
cnt = 0
startidx = 0
seq = 0
while cnt < n
	wkidx = -1
	wken = 0
	for i in startidx..n-1
		next if ar[i].used
		if seq < ar[i].st
			wkidx = i
			wken = ar[i].st
			break
		else
			if wkidx < 0
				wkidx = i
				wken = ar[i].en
			elsif ar[i].en < wken
				wkidx = i
				wken = ar[i].en
			end
		end
		break if ar[i].st > seq
	end
	ret << " " if cnt > 0
	cnt += 1
	ar[wkidx].used = true
	seq = wken
	ret << seq.to_s
	for i in startidx+1..n-1
		break if ar[i].used == false
		startidx = i
	end
end

puts ret

class Event
	def initialize(st, en)
		@st = st
		@en = en
	end
	attr_accessor :st, :en
end

n = gets.chop.to_i

ar = Array.new
for i in 0..n-1
	s,e = gets.chomp.split(/ /).map!{|x| x.to_i}
	ar.push(Event.new(s,e))
end

ret = ""
seq = 0

for i in 0..n-1
	if seq < ar[i].st
		wkidx = i
		seq = ar[i].st
	else
		seq = seq+1
	end
	ret << " " if i > 0
	ret << seq.to_s
end

puts ret

class Event
	def initialize(st, en)
		@st = st
		@en = en
		@used = false
	end
	attr_accessor :st, :en, :used
end

n = gets.chop.to_i

ar = Array.new
for i in 0..n-1
	s,e = gets.chomp.split(/ /).map!{|x| x.to_i}
	ar.push(Event.new(s,e))
end

ar.sort!{|a,b|
	if a.st == b.st
		a.en <=> b.en
	else
		a.st <=> b.st
	end
}

ret = ""
cnt = 0
startidx = 0
seq = 0
nseq = 0
while cnt < n
	wkidx = -1
	wken = 0
	for i in startidx..n-1
		next if ar[i].used
		if seq < ar[i].st
			wkidx = i
			nseq = ar[i].st
			break
		else
			if wkidx < 0
				wkidx = i
				wken = ar[i].en
			elsif ar[i].en < wken
				wkidx = i
				wken = ar[i].en
			end
			nseq = seq+1
		end
		break if ar[i].st > seq
	end
	ret << " " if cnt > 0
	cnt += 1
	ar[wkidx].used = true
	seq = nseq
	ret << seq.to_s
	for i in startidx+1..n-1
		break if ar[i].used == false
		startidx = i
	end
end

puts ret

class Event
	def initialize(st, en, id)
		@st = st
		@en = en
		@id = id
		@used = 0
	end
	attr_accessor :st, :en, :id, :used
end

n = gets.chop.to_i

ar = Array.new
for i in 0..n-1
	s,e = gets.chomp.split(/ /).map!{|x| x.to_i}
	ar.push(Event.new(s,e,i))
end

ar.sort!{|a,b|
	if a.st == b.st
		a.en <=> b.en
	else
		a.st <=> b.st
	end
}

cnt = 0
startidx = 0
seq = 0
nseq = 0
while cnt < n
	wkidx = -1
	wken = 0
	for i in startidx..n-1
		next if ar[i].used > 0
		if seq < ar[i].st
			wkidx = i
			nseq = ar[i].st
			break
		else
			if wkidx < 0
				wkidx = i
				wken = ar[i].en
			elsif ar[i].en < wken
				wkidx = i
				wken = ar[i].en
			end
			nseq = seq+1
		end
		break if ar[i].st > seq
	end

	cnt += 1
	ar[wkidx].used = nseq
	seq = nseq

	for i in startidx+1..n-1
		break if ar[i].used == 0
		startidx = i
	end
end

ret = ""
ar.sort!{|a,b|
	a.id <=> b.id
}
for i in 0..n-1
	ret << " " if i > 0
	ret << ar[i].used.to_s
end
puts ret

class Event
	def initialize(st, en)
		@st = st
		@en = en
		@used = false
	end
	attr_accessor :st, :en, :used
end

n = gets.chop.to_i

ar = Array.new
for i in 0..n-1
	s,e = gets.chomp.split(/ /).map!{|x| x.to_i}
	ar.push(Event.new(s,e))
end

ar.sort!{|a,b|
	if a.st == b.st
		a.en <=> b.en
	else
		a.st <=> b.st
	end
}

ret = ""
cnt = 0
startidx = 0
seq = 0
while cnt < n
	wkidx = -1
	wken = 0
	nseq = 0
	for i in startidx..n-1
		next if ar[i].used
		if seq < ar[i].st
			wkidx = i
			nseq = ar[i].st
			break
		else
			if wkidx < 0
				wkidx = i
				wken = ar[i].en
			elsif ar[i].en < wken
				wkidx = i
				wken = ar[i].en
			end
			nseq = seq+1
		end
		break if ar[i].st > seq
	end
	ret << " " if cnt > 0
	cnt += 1
	ar[wkidx].used = true
	seq = nseq
	ret << seq.to_s
	for i in startidx+1..n-1
		break if ar[i].used == false
		startidx = i
	end
end

puts ret

class Segment
  attr_reader :from, :to

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

  def <=> (rhs)
    return -1 if @to < rhs.to
    return @from <=> rhs.from if @to == rhs.to
    return +1
  end
end

def max(a, b)
  a > b ? a : b
end

n = gets.to_i
segs = Array.new(n) { Segment.new(*gets.split.map { |v| v.to_i }) }
result, last = [], -1

(0 ... n).to_a.sort_by { |i| segs[i] }.each do |i|
  last = max(last, segs[i].from) + 1
  result[i] = last - 1
end

puts result.join(' ')

class Segment
  attr_reader :from, :to

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

  def <=> (rhs)
    return -1 if @from < rhs.from
    return @to <=> rhs.to if @from == rhs.from
    return +1
  end
end

def max(a, b)
  a > b ? a : b
end

n = gets.to_i
segs = Array.new(n) { Segment.new(*gets.split.map { |v| v.to_i }) }
result, last = [], -1

(0 ... n).to_a.sort_by { |i| segs[i] }.each do |i|
  last = max(last, segs[i].from) + 1
  result[i] = last - 1
end

puts result.join(' ')

class Segment
  attr_reader :from, :to

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

  def <=> (rhs)
    return -1 if @from < rhs.from
    return @to <=> rhs.to if @from == rhs.from
    return +1
  end
end

def max(a, b)
  a > b ? a : b
end

n = gets.to_i
segs = Array.new(n) { Segment.new(*gets.split.map { |v| v.to_i }) }.sort

result, from = [], -1

segs.each do |seg|
  result.push(max(seg.from, from))
  from = max(seg.from, from) + 1
end

puts result.join(' ')


Ford Prefect got a job as a web developer for a small company that makes towels. His current work task is to create a search engine for the website of the company. During the development process, he needs to write a subroutine for comparing strings S and T of equal length to be "similar". After a brief search on the Internet, he learned about the Hamming distance between two strings S and T of the same length, which is defined as the number of positions in which S and T have different characters. For example, the Hamming distance between words "permanent" and "pergament" is two, as these words differ in the fourth and sixth letters.Moreover, as he was searching for information, he also noticed that modern search engines have powerful mechanisms to correct errors in the request to improve the quality of search. Ford doesn't know much about human beings, so he assumed that the most common mistake in a request is swapping two arbitrary letters of the string (not necessarily adjacent). Now he wants to write a function that determines which two letters should be swapped in string S, so that the Hamming distance between a new string S and string T would be as small as possible, or otherwise, determine that such a replacement cannot reduce the distance between the strings.Help him do this!

The first line contains integer n (1 ≤ n ≤ 200 000) — the length of strings S and T. The second line contains string S. The third line contains string T. Each of the lines only contains lowercase Latin letters.

In the first line, print number x — the minimum possible Hamming distance between strings S and T if you swap at most one pair of letters in S. In the second line, either print the indexes i and j (1 ≤ i, j ≤ n, i ≠ j), if reaching the minimum possible distance is possible by swapping letters on positions i and j, or print "-1 -1", if it is not necessary to swap characters. If there are multiple possible answers, print any of them.

NoteIn the second test it is acceptable to print i = 2, j = 3.

*a = gets(p).split
c = Array.new(26){ [nil] * 26 }
x = [nil] * 26
y = [nil] * 26

d = 0
u = v = -1
for i in 0...a[0].to_i
    j, k = [a[1][i].ord - 97, a[2][i].ord - 97]

    if j != k
        c[j][k] = x[j] = y[k] = i + 1;
        d += 1
    end
end

a = d
for i in 0...26
    for j in 0...26
        if c[i][j] && c[j][i]
            p d - 2
            puts [c[i][j], c[j][i]] * ' '
            exit
        end
    end
    if x[i] && y[i]
        a = d - 1
        u, v = x[i], y[i]
    end
end

p a
puts puts [u, v] * ' '

*a = gets(p).split
c = Array.new(26){ [nil] * 26 }
x = [nil] * 26
y = [nil] * 26

d = 0
for i in 0...a[0].to_i
    j, k = [a[1][i].ord - 97, a[2][i].ord - 97]

    if j != k
        c[j][k] = x[j] = y[k] = i + 1;
        d += 1
    end
end

for i in 0...26
    for j in 0...26
        if c[i][j] && c[j][i]
            puts d - 2
            puts [c[i][j], c[j][i]].join(' ')
            exit
        end
    end
end

for i in 0...26
    if x[i] && y[i]
        puts d - 1
        puts [x[i], y[i]].join(' ')
        exit
    end
end

puts d
puts "-1 -1"

*a = gets(p).split
c = Array.new(26){ [nil] * 26 }
x = [nil] * 26
y = [nil] * 26

d = 0
u = v = -1
for i in 0...a[0].to_i
    j, k = [a[1][i].ord - 97, a[2][i].ord - 97]

    if j != k
        c[j][k] = x[j] = y[k] = i + 1;
        d += 1
    end
end

a = d
for i in 0...26
    for j in 0...26
        if c[i][j] && c[j][i]
            p d - 2
            puts [c[i][j], c[j][i]] * ' '
            exit
        end
    end
    if x[i] && y[i]
        t = d - 1
        u, v = x[i], y[i]
    end
end

p a
puts puts [u, v] * ' '

*a = gets(p).split
c = Array.new(26){ [nil] * 26 }
x = [nil] * 26
y = [nil] * 26

d = 0
u = v = -1
for i in 0...a[0].to_i
    j, k = [a[1][i].ord - 97, a[2][i].ord - 97]

    if j != k
        c[j][k] = x[j] = y[k] = i + 1;
        d += 1
    end
end

for i in 0...26
    for j in 0...26
        if c[i][j] && c[j][i]
            p d - 2
            puts [c[i][j], c[j][i]] * ' '
            exit
        end
    end
    if x[i] && y[i]
        p d - 1
        puts [x[i], y[i]] * ' '
        exit
    end
end

p d
puts puts [u, v] * ' '


You are given two integers $$$n$$$ and $$$m$$$. Find the $$$\operatorname{MEX}$$$ of the sequence $$$n \oplus 0, n \oplus 1, \ldots, n \oplus m$$$. Here, $$$\oplus$$$ is the bitwise XOR operator.$$$\operatorname{MEX}$$$ of the sequence of non-negative integers is the smallest non-negative integer that doesn't appear in this sequence. For example, $$$\operatorname{MEX}(0, 1, 2, 4) = 3$$$, and $$$\operatorname{MEX}(1, 2021) = 0$$$.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 30\,000$$$)  — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$0 \le n, m \le 10^9$$$).

For each test case, print a single integer  — the answer to the problem.

NoteIn the first test case, the sequence is $$$3 \oplus 0, 3 \oplus 1, 3 \oplus 2, 3 \oplus 3, 3 \oplus 4, 3 \oplus 5$$$, or $$$3, 2, 1, 0, 7, 6$$$. The smallest non-negative integer which isn't present in the sequence i. e. the $$$\operatorname{MEX}$$$ of the sequence is $$$4$$$.In the second test case, the sequence is $$$4 \oplus 0, 4 \oplus 1, 4 \oplus 2, 4 \oplus 3, 4 \oplus 4, 4 \oplus 5, 4 \oplus 6$$$, or $$$4, 5, 6, 7, 0, 1, 2$$$. The smallest non-negative integer which isn't present in the sequence i. e. the $$$\operatorname{MEX}$$$ of the sequence is $$$3$$$.In the third test case, the sequence is $$$3 \oplus 0, 3 \oplus 1, 3 \oplus 2$$$, or $$$3, 2, 1$$$. The smallest non-negative integer which isn't present in the sequence i. e. the $$$\operatorname{MEX}$$$ of the sequence is $$$0$$$.

gets
$stdin.each do |t|
  n, m = t.split.map(&:to_i)
  puts m.bit_length.downto(0).reduce(0) { |s, i|
    (s ^ n | ~(-1 << i)) <= m ? s | 1 << i : s
  }
end

gets
$stdin.each do |t|
  n, m = t.split.map(&:to_i)
  l = n.bit_length
  s = m & -1 << l
  (l - 1).downto(0) do |i|
    break if (s ^ n) > m
    if m[i] == 1 && n[i] == 0
      s |= 1 << i
    end
  end
  if (s ^ n) == m
    s = ((s ^ n) + 1) & ~n
  end
  puts s
end

gets
$stdin.each do |t|
  n, m = t.split.map(&:to_i)
  s = m & (-1 << n.bit_length)
  (n.bit_length - 1).downto(0) do |i|
    break if (s ^ n) > m
    if m[i] == 1 && n[i] == 0
      s |= 1 << i
    end
  end
  s += 1 if (s ^ n) == m
  puts s
end


You are given four integers $$$a$$$, $$$b$$$, $$$x$$$ and $$$y$$$. Initially, $$$a \ge x$$$ and $$$b \ge y$$$. You can do the following operation no more than $$$n$$$ times:  Choose either $$$a$$$ or $$$b$$$ and decrease it by one. However, as a result of this operation, value of $$$a$$$ cannot become less than $$$x$$$, and value of $$$b$$$ cannot become less than $$$y$$$. Your task is to find the minimum possible product of $$$a$$$ and $$$b$$$ ($$$a \cdot b$$$) you can achieve by applying the given operation no more than $$$n$$$ times.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains five integers $$$a$$$, $$$b$$$, $$$x$$$, $$$y$$$ and $$$n$$$ ($$$1 \le a, b, x, y, n \le 10^9$$$). Additional constraint on the input: $$$a \ge x$$$ and $$$b \ge y$$$ always holds.

For each test case, print one integer: the minimum possible product of $$$a$$$ and $$$b$$$ ($$$a \cdot b$$$) you can achieve by applying the given operation no more than $$$n$$$ times.

NoteIn the first test case of the example, you need to decrease $$$b$$$ three times and obtain $$$10 \cdot 7 = 70$$$.In the second test case of the example, you need to decrease $$$a$$$ one time, $$$b$$$ one time and obtain $$$11 \cdot 7 = 77$$$.In the sixth test case of the example, you need to decrease $$$a$$$ five times and obtain $$$5 \cdot 11 = 55$$$.In the seventh test case of the example, you need to decrease $$$b$$$ ten times and obtain $$$10 \cdot 1 = 10$$$.

def solve(a, b, x, y, n)
	b_dec = [[0, b - y].max, n].min
	b -= b_dec
	n -= b_dec
	a_dec = [[0, a - x].max, n].min
	a -= a_dec
	n -= a_dec
	a * b
end

t = gets.to_i
t.times do
	a, b, x, y, n = gets.split.map &:to_i
	p [solve(a, b, x, y, n), solve(b, a, y, x, n)].min
end

t = gets.to_i
t.times do
    a, b, x, y, n = gets.chomp.split(" ").map(&:to_i)
    if a < b
        tmp = a
        a = b
        b = tmp
        tmpx = x
        x = y
        y = tmpx
    end
    iti = b - n
    if iti >= y
        b = b - n
    else
        #puts "IIII"
        if y <= x
            over = n - (b - y)
            a = [x,a-over].max
            b = y
        elsif x < y
            point = a - n
            if point >= y
                over = n - (b - y)
                a = [x,a-over].max
                b = y
            elsif point < y
                if point >= x
                    a = point
                    # puts "OOOOOO"
                    # p a
                    # p b
                    # puts "UU"
                else
                    over = n - (a - x)
                    b = [b - over,y].max
                    a = x
                end
            end
        end
    end
    puts a * b
end

gets.to_i.times do
    a, b, x, y, n = gets.split.map &:to_i
    res = 1E18.to_i
    3.times do |i|
        d1 = [n, a - x].min
        d2 = [n - d1, b - y].min
        res = [res, (a - d1) * (b - d2)].min
        a, b, x, y = b, a, y, x
    end
    puts res
end

def solve
  a, b, x, y, n = gets.strip.split(' ').map(&:to_i)
  ck1 = a - x > n ? a - n : a - (a - x)
  ck2 = b - y > n ? b - n : b - (b - y)
  if ck1 < ck2
    n -= (a - ck1)
    if (b - y) > n
      b = b - n
    else
      b = b - (b - y)
    end
    puts ck1 * b
  else
    n = n - (b - ck2)
    if (a - x) > n
      a = a - n
    else
      a = a - (a - x)
    end
    puts ck2 * a
  end
end

gets.strip.to_i.times { solve }


A wise man told Kerem "Different is good" once, so Kerem wants all things in his life to be different. Kerem recently got a string s consisting of lowercase English letters. Since Kerem likes it when things are different, he wants all substrings of his string s to be distinct. Substring is a string formed by some number of consecutive characters of the string. For example, string "aba" has substrings "" (empty substring), "a", "b", "a", "ab", "ba", "aba".If string s has at least two equal substrings then Kerem will change characters at some positions to some other lowercase English letters. Changing characters is a very tiring job, so Kerem want to perform as few changes as possible.Your task is to find the minimum number of changes needed to make all the substrings of the given string distinct, or determine that it is impossible.

The first line of the input contains an integer n (1 ≤ n ≤ 100 000) — the length of the string s. The second line contains the string s of length n consisting of only lowercase English letters.

If it's impossible to change the string s such that all its substring are distinct print -1. Otherwise print the minimum required number of changes.

NoteIn the first sample one of the possible solutions is to change the first character to 'b'.In the second sample, one may change the first character to 'a' and second character to 'b', so the string becomes "abko".

#!/usr/bin/ruby
n=gets.to_i
h={}
gets.chomp.chars{|c|h[c]=1}
p n>26 ? -1 : n-h.size

# http://codeforces.com/contest/672/problem/B
n = gets.chomp.to_i
chars = gets.chomp.chars

if n > 26
  puts "-1"
else
  puts n - chars.uniq.length
end

n = readline.to_i
str = readline.strip.each_char.to_a
n = str.length
m = str.uniq.length
if n > 26
  puts -1
else
  puts n - m
end

n=gets.chomp.to_i
a=gets.chomp.split("")
if n>26
puts"-1"
else
n=n-a.uniq.length
puts"#{n}"
end

n = gets.chomp.to_i
s = gets.chomp.split ""
rep = 0
store = Array.new(255, 0)
s.each do |c|
    store[c.ord] += 1
    rep += store[c.ord] if store[c.ord] == 1
end
if n > 26
    puts "-1"
else
    puts n - rep
end

n=gets
s=gets.chomp

arr=[]
"a".upto("z"){|e|
	arr<<e
}
c1=0
s.chars{|e|
	if arr.index(e) then
		arr.delete(e)
	else
		c1+=1
	end
}
if c1<=arr.size then
	puts c1
else
	puts -1
end

a = Hash.new(0)
n = gets.chomp.to_i
s = gets.chomp
for i in 0..n-1
	a[s[i]]+=1
end
sum = 0
for i in'a'..'z'
	sum += a[i]-1 if a[i]>1
end
if n > 26
	puts "-1"
else
	puts sum
end

# coding: utf-8

n = gets.to_i
s = gets.chomp
counter = {}
subcnt = 0
for c in s.split(//)
  counter[c] ||= 0
  counter[c] += 1

  if counter[c] > 1
    subcnt += 1
  end
end

if 26 - (subcnt + counter.length) >= 0
  puts subcnt
else
  puts -1
end

n = gets.to_i
puts n <= 26 ? n - gets.chomp.chars.uniq.size : -1

input = gets.chomp.to_i
str = gets.chomp
flag = {}
dupCount = 0
blank = 0

if str.length > 26
	puts '-1'
	exit
end

str.each_char do |c|
	if flag.has_key? c
		flag[c] += 1
	else
		flag[c] = 1
	end
end

for c in ('a'..'z') do
	blank += 1 if !flag.has_key?(c)
end

flag.each do |k, v|
	dupCount += (v - 1) if v > 1
end

ans = 0

#puts "Dup : #{dupCount}"
#puts "Blank #{blank}"

until dupCount == 0 || blank == 0
	blank -= 1
	dupCount -= 1
	ans += 1
end

puts ans

gets

s = gets.chomp

let = Hash.new
let.default = 0

if s.length > 26
  print -1
  exit
end

res = 0

s.length.times { |num|
  ch = s[num]
  if let[ch] == 0
    let[ch] = 1
  else
    let[ch] += 1
    res += 1
  end
}

puts res

# qwertyuiopasdfghjklzxcvbnm
# mmmmmmmmmmmmmmmmmmmmmmmmmm

# coding: utf-8

n = gets.to_i
s = gets.chomp
counter = {}
subcnt = 0
for c in s.split(//)
  counter[c] ||= 0
  counter[c] += 1

  if counter[c] > 1
    subcnt += 1
  end
end

puts subcnt

n = gets.to_i
puts n - gets.chomp.chars.uniq.size

input = gets.chomp.to_i
str = gets.chomp
flag = {}
dupCount = 0
blank = 0

if str.length > 26
	puts '-1'
	exit
end

str.each_char do |c|
	if flag.has_key? c
		flag[c] += 1
	else
		flag[c] = 1
	end
end

for c in ('a'..'z') do
	blank += 1 if !flag.has_key?(c)
end

flag.each do |k, v|
	dupCount += 1 if v > 1
end

ans = 0

until dupCount == 0 || blank == 0
	blank -= 1
	dupCount -= 1
	ans += 1
end

puts ans


Sometime the classic solution are not powerful enough and we have to design our own. For the purpose of this problem you have to implement the part of the system of task scheduling.Each task should be executed at some particular moments of time. In our system you may set the exact value for the second, minute, hour, day of the week, day and month, when the task should be executed. Moreover, one can set a special value -1 that means any value of this parameter is valid.For example, if the parameter string is -1 59 23 -1 -1 -1, the problem will be executed every day at 23:59:00, 23:59:01, 23:59:02, ..., 23:59:59 (60 times in total).Seconds, minutes and hours are numbered starting from zero, while day, months and days of the week are numbered starting from one. The first day of the week is Monday.There is one special case that is treated separately. If both day of the week and day are given (i.e. differ from -1) to execute the task only one of these two (at least one, if both match this is fine too) parameters should match the current time (of course, all other parameters should match too). For example, the string of parameters 0 0 12 6 3 7 means that the task will be executed both on Saturday, July 2nd, 2016 and on Sunday, July 3rd, 2016 at noon.One should not forget about the existence of the leap years. The year is leap if it's number is divisible by 400, or is not divisible by 100, but is divisible by 4. Each leap year has 366 days instead of usual 365, by extending February to 29 days rather than the common 28.The current time is represented as the number of seconds passed after 00:00:00 January 1st, 1970 (Thursday).You are given the string of six parameters, describing the moments of time the task should be executed. You are also given a number of moments of time. For each of them you have to find the first moment of time strictly greater than the current when the task will be executed.

The first line of the input contains six integers s, m, h, day, date and month (0 ≤ s, m ≤ 59, 0 ≤ h ≤ 23, 1 ≤ day ≤ 7, 1 ≤ date ≤ 31, 1 ≤ month ≤ 12). Each of the number can also be equal to  - 1. It's guaranteed, that there are infinitely many moments of time when this task should be executed. Next line contains the only integer n (1 ≤ n ≤ 1000) — the number of moments of time you have to solve the problem for. Each of the next n lines contains a single integer ti (0 ≤ ti ≤ 1012).

Print n lines, the i-th of them should contain the first moment of time strictly greater than ti, when the task should be executed.

NoteThe moment of time 1467372658 after the midnight of January 1st, 1970 is 11:30:58 July 1st, 2016.

def leap(y)
  return y % 400 == 0 || (y % 100 != 0 && y % 4 == 0)
end

def check_time(t, s, m, h)
  return false unless s == -1 || t.sec == s
  return false unless m == -1 || t.min == m
  return false unless h == -1 || t.hour == h
  true
end

def fix_time(t, s, m, h)
  loop do
    ok = true

    if h != -1 && t.hour != h
      ok = false
      t += 1 while t.sec != 0
      t += 60 while t.min != 0
      t += 3600 while t.hour != h
    end

    if m != -1 && t.min != m
      ok = false
      t += 1 while t.sec != 0
      t += 60 while t.min != m
    end

    if s != -1 && t.sec != s
      ok = false
      t += 1 while t.sec != s
    end

    break if ok
  end

  t += 1 until check_time(t, s, m, h)
  t
end

def check_date(t, day, date, month)
  return false unless month == -1 || t.month == month
  if day != -1 && date != -1
    return day == t.wday || date == t.mday
  else
    return false unless day == -1 || t.wday == day
    return false unless date == -1 || t.mday == date
    return true
  end
end

def fix_date(t, day, date, month)
  until check_date(t, day, date, month)
    if month != -1 && t.month != month && t.mday == 1
      t += 28 * 86400
    else
      t += 86400
    end
  end
  t
end

s, m, h, day, date, month = gets.split.map(&:to_i)
day %= 7 if day != -1
q = gets.to_i
q.times do
  epoch = gets.to_i
  t = Time.at(epoch).utc + 1
  t = fix_time(t, s, m, h)
  t += 86400 - t.to_i % 86400 unless check_date(t, day, date, month)
  t = fix_time(t, s, m, h)
  t = fix_date(t, day, date, month)
  puts t.to_i
end

ENV['TZ'] = 'UTC'

def leap(y)
  return y % 400 == 0 || (y % 100 != 0 && y % 4 == 0)
end

def check_time(t, s, m, h)
  return false unless s == -1 || t.sec == s
  return false unless m == -1 || t.min == m
  return false unless h == -1 || t.hour == h
  true
end

def fix_time(t, s, m, h)
  loop do
    ok = true

    if h != -1 && t.hour != h
      ok = false
      t += 1 while t.sec != 0
      t += 60 while t.min != 0
      t += 3600 while t.hour != h
    end

    if m != -1 && t.min != m
      ok = false
      t += 1 while t.sec != 0
      t += 60 while t.min != m
    end

    if s != -1 && t.sec != s
      ok = false
      t += 1 while t.sec != s
    end

    break if ok
  end
  t += 1 until check_time(t, s, m, h)
  t
end

def check_date(t, day, date, month)
  return false unless month == -1 || t.month == month
  if day != -1 && date != -1
    return day == t.wday || date == t.mday
  else
    return false unless day == -1 || t.wday == day
    return false unless date == -1 || t.mday == date
    return true
  end
end

def fix_date(t, day, date, month)
  t += 86400 until check_date(t, day, date, month)
  t
end

s, m, h, day, date, month = gets.split.map(&:to_i)
q = gets.to_i
q.times do
  epoch = gets.to_i
  t = Time.at(epoch).utc + 1
  t = fix_time(t, s, m, h)
  t = fix_date(t, day, date, month)
  puts t.to_i
end

def leap(y)
  return y % 400 == 0 || (y % 100 != 0 && y % 4 == 0)
end

def check_time(t, s, m, h)
  return false unless s == -1 || t.sec == s
  return false unless m == -1 || t.min == m
  return false unless h == -1 || t.hour == h
  true
end

def fix_time(t, s, m, h)
  loop do
    ok = true

    if h != -1 && t.hour != h
      ok = false
      t += 1 while t.sec != 0
      t += 60 while t.min != 0
      t += 3600 while t.hour != h
    end

    if m != -1 && t.min != m
      ok = false
      t += 1 while t.sec != 0
      t += 60 while t.min != m
    end

    if s != -1 && t.sec != s
      ok = false
      t += 1 while t.sec != s
    end

    break if ok
  end

  t += 1 until check_time(t, s, m, h)
  t
end

def check_date(t, day, date, month)
  return false unless month == -1 || t.month == month
  if day != -1 && date != -1
    return day == t.wday || date == t.mday
  else
    return false unless day == -1 || t.wday == day
    return false unless date == -1 || t.mday == date
    return true
  end
end

def fix_date(t, day, date, month)
  t += 86400 until check_date(t, day, date, month)
  t
end

s, m, h, day, date, month = gets.split.map(&:to_i)
day %= 7
q = gets.to_i
q.times do
  epoch = gets.to_i
  t = Time.at(epoch).utc + 1
  t = fix_time(t, s, m, h)
  t += 86400 - t.to_i % 86400 unless check_date(t, day, date, month)
  t = fix_time(t, s, m, h)
  t = fix_date(t, day, date, month)
  puts t.to_i
end

ENV['TZ'] = 'UTC'

def leap(y)
  return y % 400 == 0 || (y % 100 != 0 && y % 4 == 0)
end

def check_time(t, s, m, h)
  return false unless s == -1 || t.sec == s
  return false unless m == -1 || t.min == m
  return false unless h == -1 || t.hour == h
  true
end

def fix_time(t, s, m, h)
  if h != -1 && t.hour != h
    t += 1 while t.sec != 0
    t += 60 while t.min != 0
    t += 3600 while t.hour != h
  end

  if m != -1 && t.min != m
    t += 1 while t.sec != 0
    t += 60 while t.min != m
  end
  t += 1 until check_time(t, s, m, h)
  t
end

def check_date(t, day, date, month)
  return false unless month == -1 || t.month == month
  if day != -1 && date != -1
    return day == t.wday || date == t.mday
  else
    return false unless day == -1 || t.wday == day
    return false unless date == -1 || t.mday == date
    return true
  end
end

def fix_date(t, day, date, month)
  t += 86400 until check_date(t, day, date, month)
  t
end

s, m, h, day, date, month = gets.split.map(&:to_i)
q = gets.to_i
q.times do
  epoch = gets.to_i
  t = Time.at(epoch) + 1
  t = fix_time(t, s, m, h)
  t = fix_date(t, day, date, month)
  puts t.to_i
end


Valera is a collector. Once he wanted to expand his collection with exactly one antique item.Valera knows n sellers of antiques, the i-th of them auctioned ki items. Currently the auction price of the j-th object of the i-th seller is sij. Valera gets on well with each of the n sellers. He is perfectly sure that if he outbids the current price of one of the items in the auction (in other words, offers the seller the money that is strictly greater than the current price of the item at the auction), the seller of the object will immediately sign a contract with him.Unfortunately, Valera has only v units of money. Help him to determine which of the n sellers he can make a deal with.

The first line contains two space-separated integers n, v (1 ≤ n ≤ 50; 104 ≤ v ≤ 106) — the number of sellers and the units of money the Valera has. Then n lines follow. The i-th line first contains integer ki (1 ≤ ki ≤ 50) the number of items of the i-th seller. Then go ki space-separated integers si1, si2, ..., siki (104 ≤ sij ≤ 106) — the current prices of the items of the i-th seller.

In the first line, print integer p — the number of sellers with who Valera can make a deal. In the second line print p space-separated integers q1, q2, ..., qp (1 ≤ qi ≤ n) — the numbers of the sellers with who Valera can make a deal. Print the numbers of the sellers in the increasing order.

NoteIn the first sample Valera can bargain with each of the sellers. He can outbid the following items: a 40000 item from the first seller, a 20000 item from the second seller, and a 10000 item from the third seller.In the second sample Valera can not make a deal with any of the sellers, as the prices of all items in the auction too big for him.

n, v = gets.chomp.split(/ /).map(&:to_i)
sellers = []
n.times do
    line = gets.chomp.split(/ /).map(&:to_i)
    prices = line[1...line.length]
    sellers << prices
end
deal = []
sellers.each_index do |seller|
    prices = sellers[seller]
    prices.each do |price|
        if price < v
            deal << seller + 1
            break
        end
    end
end
deal.sort!
puts deal.length
puts deal.join(" ")

n,v=gets.split.map{|e| e.to_i}
ans=[]
n.times{|i|
	x=gets.split.map{|e| e.to_i}
	x.shift
	if x.min<v then
		ans<<(i+1)
	end
}
puts ans.size
if ans.size>0 then
	puts ans*" "
end

# coding: utf-8

n, v = gets.split.map(&:to_i)
dealers = []
n.times { |dealer|
    _, *prices = gets.split.map(&:to_i)
    if v > prices.min
        dealers << dealer + 1
    end
}
puts dealers.length
puts dealers.join(' ')

nk = gets.chomp.split.map(&:to_i)
n = nk[0]
k = nk[1]
arr = []
count = 0
a = []
for i in (0..n-1)
	arr << gets.chomp.split.map(&:to_i)
	arr[i].delete_at(0)
	if arr[i].min < k
		count += 1
		a << i+1
	end
end

puts count
for num in a
	print num
	print " "
end

n,v=gets.split.map(&:to_i)
a=1.step(n).select{|i|
gets.split.map(&:to_i)[1..-1].any?{|e|e<v}
}
p a.size
puts a*' '

first_line=gets().split(" ")
ans=[]
n=first_line[0].to_i;money=first_line[1].to_i
for i in 0...n
  line=gets().split(" ")
  line.map!{|x| x.to_i}
  for j in 1...line.size
    if line[j]<money
      ans<< i+1
      break
    end
  end
  ans.sort!

end
if ans.size==0
  puts 0
else
  puts ans.size
  puts ans.join(" ")
end

n,v = gets.split(" ").map(&:to_i)
sellers = []
(1..n).each do |s|
  sellers << s if gets.split(" ").map(&:to_i)[1..-1].any?{|x| v > x}
end
puts sellers.count
puts sellers.join(" ")

n,v = gets.split.map(&:to_i)
d = (1..n).select { |_|
  items = gets.split.map(&:to_i).drop 1
  items.select { |i| v > i }.size >= 1
}
puts d.size
puts d.join " "

def read_next_line
  gets.chomp.split.map(&:to_i)
end

n, v = read_next_line
a = []
(1..n).each { |i|
  a << i if read_next_line[1..-1].any? { |x| x < v }
}
puts a.size
puts a * ' '

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

sellers_auctions = []
n.times do
  seller_auction = gets.split.map &:to_i
  seller_auction.shift
  sellers_auctions << seller_auction
end

bargainable_sellers = []

sellers_auctions.each_index do |i|
  buyable_items = sellers_auctions[i].select{ |item| item < v }
  bargainable_sellers << (i + 1) unless buyable_items.empty?
end

puts bargainable_sellers.length
puts bargainable_sellers.join(' ')

#!/usr/bin/ruby
n,v=gets.split.map(&:to_i)
a=1.step(n).select{gets.split.map(&:to_i)[1..-1].any?{|e|e<v}}
p a.size
puts a*' '

n,x=gets.split.map &:to_i
str=""
k=0
j=1
p=""
n.times do

  mas=gets.split.map &:to_i
  i=1
  while(i<mas.length)
    if x>mas[i]
      k+=1
      p+=j.to_s+" "
      break
    end
    i+=1
  end
j+=1
end
puts k
puts p

n,v=gets.chomp.split.map(&:to_i)
q=[]
n.times do |i|
	a=gets.chomp.split.map(&:to_i)
	k=a.shift
	for x in a
		if v>x
			q.push(i+1)
			break
		end
	end
end
puts q.count
if (!q.empty?)
	for i in q
		print i.to_s+' '
	end
end
puts

n,v = gets.chomp.split.map(&:to_i)
q=[]
n.times do |i|
	a=gets.chomp.split.map(&:to_i)
	k=a.shift
	for x in a
		if v>x
			q.push(i+1)
			break
		end
	end
end
puts q.count
if (!q.empty?)
	for i in q
		print i.to_s+' '
	end
end
puts

input = gets.split(" ").map{|el| el.to_i }
sellers = input[0]
money = input[1]
output = []

for i in 1..sellers
  input = []
  input = gets.split(" ").map{|el| el.to_i}
  for j in 1...input.size
    if input[j] < money
      output << i
      break
    end
  end
end

puts output.size
puts output.join(" ")

n, v = gets.split.map &:to_i
arr = []
n.times do
  arr << gets.split.map { |x| x.to_i }.to_a
  arr.last.shift
end
count = 0
indices = []
arr.each do |x|
  if x.count { |y| y < v } > 0
    count += 1
    indices << arr.index(x) + 1
  end
end
puts count
indices.each { |x| print x, " " }

n,v=gets.split.map{|i| i.to_i}
o=[]
n.times{|i| o<<(i+1) if gets.split.map{|i| i.to_i}[1..51].min<v}
p o.length
puts o.join' '

n, v = gets.chomp.split(/ /).map(&:to_i)
sellers = []
n.times do
    line = gets.chomp.split(/ /).map(&:to_i)
    prices = line[1...line.length]
    sellers << prices
end
deal = []
sellers.each_index do |seller|
    prices = sellers[seller]
    prices.each do |price|
        if price < v
            deal << seller + 1
        end
    end
end
deal.sort!
puts deal.length
puts deal.join(" ")

# coding: utf-8

n, v = gets.split.map(&:to_i)
dealers = []
n.times {
    dealer, *prices = gets.split.map(&:to_i)
    prices.each { |i|
        if i < v
            dealers << dealer
            break
        end
    }
}
puts dealers.length
puts dealers.join(' ')

first_line=gets().split(" ")
ans=[]
n=first_line[0].to_i;money=first_line[1].to_i
for i in 0...n
  line=gets().split(" ")
  line.map!{|x| x.to_i}
  for i in 1...line.size
    if line[i]<money
      ans<<line[0]
      break
    end
  end
  ans.sort!

end
if ans.size==0
  puts 0
else
  puts ans.size
  puts ans.join(" ")
end

first_line=gets().split(" ")
ans=[]
n=first_line[0].to_i;money=first_line[1].to_i
for i in 0...n
  line=gets().split(" ")
  line.map!{|x| x.to_i}
  for i in 1...line.size
    if line[i]<money
      ans<<line[0]
      break
    end
  end
  ans.sort!

end
if ans.size==0
  puts 0
else
  puts ans.join(" ")
end

n,v=gets.chomp.split.map(&:to_i)
q=[]
n.times do
	a=gets.chomp.split.map(&:to_i)
	k=a.shift
	for x in a
		if v>x
			q.push(k)
			break
		end
	end
end
puts q.count
if (!q.empty?)
	for i in q
		print i.to_s+' '
	end
end
puts


The sequence of $$$m$$$ integers is called the permutation if it contains all integers from $$$1$$$ to $$$m$$$ exactly once. The number $$$m$$$ is called the length of the permutation.Dreamoon has two permutations $$$p_1$$$ and $$$p_2$$$ of non-zero lengths $$$l_1$$$ and $$$l_2$$$.Now Dreamoon concatenates these two permutations into another sequence $$$a$$$ of length $$$l_1 + l_2$$$. First $$$l_1$$$ elements of $$$a$$$ is the permutation $$$p_1$$$ and next $$$l_2$$$ elements of $$$a$$$ is the permutation $$$p_2$$$. You are given the sequence $$$a$$$, and you need to find two permutations $$$p_1$$$ and $$$p_2$$$. If there are several possible ways to restore them, you should find all of them. (Note that it is also possible that there will be no ways.)

The first line contains an integer $$$t$$$ ($$$1 \le t \le 10\,000$$$) denoting the number of test cases in the input. Each test case contains two lines. The first line contains one integer $$$n$$$ ($$$2 \leq n \leq 200\,000$$$): the length of $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n-1$$$). The total sum of $$$n$$$ is less than $$$200\,000$$$.

For each test case, the first line of output should contain one integer $$$k$$$: the number of ways to divide $$$a$$$ into permutations $$$p_1$$$ and $$$p_2$$$. Each of the next $$$k$$$ lines should contain two integers $$$l_1$$$ and $$$l_2$$$ ($$$1 \leq l_1, l_2 \leq n, l_1 + l_2 = n$$$), denoting, that it is possible to divide $$$a$$$ into two permutations of length $$$l_1$$$ and $$$l_2$$$ ($$$p_1$$$ is the first $$$l_1$$$ elements of $$$a$$$, and $$$p_2$$$ is the last $$$l_2$$$ elements of $$$a$$$). You can print solutions in any order.

NoteIn the first example, two possible ways to divide $$$a$$$ into permutations are $$$\{1\} + \{4, 3, 2, 1\}$$$ and $$$\{1,4,3,2\} + \{1\}$$$.In the second example, the only way to divide $$$a$$$ into permutations is $$$\{2,4,1,3\} + \{2,1\}$$$.In the third example, there are no possible ways.

t = gets.to_i
t.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  l2 = a.max
  l1 = n - l2
  count = 0
  pair = []
  p1 = a.slice(0, l1)
  p2 = a.slice(l1, l2)
  if p1.min == 1 && p1.max == l1 && p2.min == 1 && p2.max == l2
    if p1.uniq.length == l1 && p2.uniq.length == l2
      count += 1
      pair.push([l1, l2])
    end
  end
  if l1 != l2
    p1 = a.slice(0, l2)
    p2 = a.slice(l2, l1)
    if p1.min == 1 && p1.max == l2 && p2.min == 1 && p2.max == l1
      if p1.uniq.length == l2 && p2.uniq.length == l1
        count += 1
        pair.push([l2, l1])
      end
    end
  end
  puts count
  pair.each do |pa|
    puts pa.join(" ")
  end
end

t = gets.to_i
t.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  l2 = a.max
  l1 = n - l2
  count = 0
  pair = []
  p1 = a.slice(0, l1)
  p2 = a.slice(l1, l2)
  if p1.uniq.length == l1 && p2.uniq.length == l2
    count += 1
    pair.push([l1, l2])
  end
  if l1 != l2
    p1 = a.slice(0, l2)
    p2 = a.slice(l2, l1)
    if p1.uniq.length == l2 && p2.uniq.length == l1
      count += 1
      pair.push([l2, l1])
    end
  end
  puts count
  pair.each do |pa|
    puts pa.join(" ")
  end
end

t = gets.to_i
t.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  l2 = a.max
  l1 = n - l2
  count = 0
  pair = []
  p1 = a.slice(0, l1)
  p2 = a.slice(l1, l2)
  if p1.uniq.length == l1 && p2.uniq.length == l2
    count += 1
    pair.push([l1, l2])
  end
  p1 = a.slice(0, l2)
  p2 = a.slice(l2, l1)
  if p1.uniq.length == l2 && p2.uniq.length == l1
    count += 1
    pair.push([l2, l1])
  end
  puts count
  pair.each do |pa|
    puts pa.join(" ")
  end
end


Dr. Bruce Banner hates his enemies (like others don't). As we all know, he can barely talk when he turns into the incredible Hulk. That's why he asked you to help him to express his feelings.Hulk likes the Inception so much, and like that his feelings are complicated. They have n layers. The first layer is hate, second one is love, third one is hate and so on...For example if n = 1, then his feeling is "I hate it" or if n = 2 it's "I hate that I love it", and if n = 3 it's "I hate that I love that I hate it" and so on.Please help Dr. Banner.

The only line of the input contains a single integer n (1 ≤ n ≤ 100) — the number of layers of love and hate.

I hate it
I hate that I love it
I hate that I love that I hate it

n = gets.to_i

ps = 'I hate '
p1 = 'that I love '
p2 = 'that I hate '
pe = 'it'
i = 1
while(i < n) do

    if (i % 2 ==1)
        ps += p1
    else
        ps += p2
    end

    i = i + 1
end
ps+=pe
puts ps

number = gets.chomp.to_i

hate = "I hate "
love = "I love "

if number == 1
	print hate
	print "it"
else
	for i in (1..number)
		if i % 2 != 0 # odd = hate
			print hate
			if i != number
				print "that "
			else
				print "it"
			end
		else	# even = love
			print love
			if i != number
				print "that "
			else
				print "it"
			end
		end
	end
end

#!/usr/bin/ruby

n = gets.chomp.to_i

s1 = "I hate "
s2 = "I love "
s3 = "that "
s4 = "it"

for i in 1..n
    if (i % 2 != 0)
        print s1
    else
        print s2
    end
    if i != n
        print s3
    else
        print s4
    end
end

n = gets.to_i
result = []
(1..n).each_with_index do |index|
	result << (index.even? ? 'I love' : 'I hate')
	result << (index == n ? 'it' : 'that')
end
print result.join(' ')

s = ""
n = gets.to_i

for i in (1..n-1)
  i%2 == 1 ? s += "I hate that " : s += "I love that "
end

puts s + (n%2 == 0 ? "I love it" : "I hate it")

$n = gets
$n = $n.to_i
print "I "
(1..$n).each do |i|
	if ((i%2 == 0)and(i == $n))
		print "love it"
		break
	end
	if ((i%2 == 1)and(i == $n))
		print "hate it"
		break
	end
	if i%2 == 0
		print "love that I "
	end
	if i%2 == 1
		print "hate that I "
	end
end

class Array
  def r
    self.map { |q| q.to_i }
  end
end

def read_line
  STDIN.gets.split(" ")
end

def read_to_array
  read_line.to_a
end

def read_numbers_to_array
  read_to_array.r
end

n = STDIN.gets.to_i
res = ""
hate = true
for i in 1..n - 1 do
  if hate
    res += "I hate that "
    hate = false
  else
    hate = true
    res += "I love that "
  end
end

if hate
  res += "I hate it"
else
  res += "I love it"
end

puts res

LEVELS = ['I hate', 'I love']

level = gets.to_i

puts LEVELS.cycle.take(level).join(' that ').concat(' it')

n = gets.chomp.to_i

n.times do |i|
	if (i + 1).even? then
		print "I love"
	else
		print "I hate"
	end
	print i == n - 1 ? " it " : " that "
end
puts

n = gets.to_i
puts n.times.map{|i| i.even? ? 'I hate' : 'I love'}.join(' that ') + ' it'

t = gets.to_i
for i in 0..t-1
	if i%2 == 0
		print "I hate "
		if i == t-1
			print "it"
		else
			print "that "
		end
	else
		print "I love "
		if i == t-1
			print "it"
		else
			print "that "
		end
	end
end
print "\n"

input = gets.to_i

ans = ["I hate " , "I love "].cycle(input).to_a[0..input-1].join("that ")
puts ans + "it"

input = gets.chomp.to_i
1.upto(input-1){ |i|
	 if i.even?
		print "I love "
	else
		print "I hate "
	end
	print "that "
}
if input.even?
		print "I love it"
else
		print "I hate it"
	end

input = gets.to_i
input.times {|i|
print i % 2 == 0 ?"I hate" : "I love"
print i == input - 1 ? " it" : " that "
}

n = gets.chomp.to_i
sentence = ""

for i in (1...n+1)
  if i % 2 != 0
    sentence += "I hate"
  else
    sentence += "I love"
  end
  if i != n
    sentence += " that "
  end
end

sentence += " it"
puts sentence

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

n = gets.to_i

feeling = "I hate it"

2.upto(n) do |i|
    if i % 2 == 0
        feeling.gsub!("it", "that I love it")
    else
        feeling.gsub!("it", "that I hate it")
    end
end

puts feeling

n = gets.chomp.to_i

w = ["I hate that", "I love that"]

result = "I hate it"
pos    = 1
if n%2 == 0 then
	result = "I love it"
	pos    = 0
end

1.upto(n-1) do
	result = w[pos] + " " + result
	pos = (pos+1)%2
end

puts result

n = STDIN.gets.to_i
s = ''
n.times do |i|
	s += 'I '+((i%2)==0 ? 'hate' : 'love')+' '+(i==n-1 ? 'it' : 'that ')
end
puts s

input = gets.to_i
feeling1 = "I hate"
feeling2 = "I love"
feeling = ""
if(input == 1)
  feeling = "I hate it"
else
  c = 1
  while(c <= input)
    if(c%2 == 1)
      feeling += feeling1
      if(c!=input)
        feeling += " that "
      end
    else
      feeling += feeling2
      if(c!=input)
        feeling += " that "
      end
    end
    c += 1
  end
  feeling += " it"
end
puts feeling

n=gets.to_i
s=""
n.times do |x|
	s+="I love " if x%2==1
	s+="I hate " if x%2==0
	s+="that " if x!=n-1
	s+="it" if x==n-1
end
puts s

n = gets.chomp.to_i
ans = ''

n.times do |i|
	i += 1

	ans += 'I hate ' if i % 2 == 1
	ans += 'I love ' if i % 2 == 0

	ans += 'that ' if i != n
end

ans += 'it'

puts ans

#!/usr/bin/ruby
N=gets.to_i
N.times{|i|
	print i%2==0?'I hate ':'I love '
	print i<N-1?'that ':'it'+$/
}

#!usr/bin/env ruby

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

n = gets.to_i

answer = "I hate "

(n-1).times do |n|
  feeling = n % 2 == 0 ? "love" : "hate"
  answer << "that I #{feeling} "
end

answer << "it"

puts answer

n = gets.to_i - 1

print 'I hate'

k = 1
n.times do
  if k==1
    print ' that I love'
    k = 0
  else
    print ' that I hate'
    k = 1
  end
end
print ' it'

n = gets.chomp.to_i
base = (n.to_f / 2.0).ceil
ans = ["I hate", "I love"].cycle(base).to_a[0...n].join(" that ")
puts "#{ans} it"

n = gets.to_i()
print "I hate "
for i in (2..n)
  if i % 2 > 0
    print "that I hate "
  else
    print "that I love "
  end
end
puts "it"

hate = "I hate"
love = "I love"

lvl = Integer(STDIN.gets())

prev_emotion = false
emotion = ""

lvl.times { |x|

    if prev_emotion
        emotion += " that "
    else
        prev_emotion = true
    end

    if x.even?
        emotion += hate
    else
        emotion += love
    end
}

emotion += " it"

puts emotion

feel_number = gets.strip.to_i
feels = []

index = 1
feel_number.times do
  if index.odd?
    feels.push('I hate')
  else
    feels.push('I love')
  end
  index += 1
end

output = feels.join(' that ')
output += ' it'
puts output

n = gets.to_i
n.times{ |x|
  print x % 2 == 0 ? "I hate " : "I love "
  print x + 1 == n ? "it " : "that "
}

word = ["I hate", "I love"]
input = gets.chomp!.to_i
puts word.cycle.take(input).join(" that ").concat(' it')

layers = gets.to_i
reduced = 0;
result = ""
until layers == 0
    if reduced == 0
        result = result + "I hate"
    end

    if reduced == 1
        result = result + " that I love"
    end

    if reduced > 1
        if reduced % 2 == 0
            result = result + " that I hate"
        end
        if reduced % 2 > 0
            result = result + " that I love"
        end
    end

    layers = layers - 1
    reduced = reduced + 1
end
result = result + " it"
puts result

n = gets.to_i
ans = 'I hate'
(2..n).each do |i|
  if i.odd?
    ans += ' that I hate'
  else
    ans += ' that I love'
  end
end
puts ans + ' it'

n = gets.strip.to_i
odd_s = "I hate"
even_s = "I love"
s = Array.new(n) { |i| (i + 1).odd? ? odd_s : even_s }
ans = s.join(" that ") + " it"
puts ans

n=gets.to_i
a=["I hate","I love"]
str=""
p1=0
n.times{|i|

	if i==0 then
		str=a[0]
	else
		str=str+" that "+a[p1]
	end
	p1=((p1==1)?0:1)
}
puts str+" it"

n = gets.chomp.to_i
s = ""

co = 0
n.times.each do
  if co == 0
    s += 'I hate it'
    co +=1
  elsif co %2 == 0
    s.gsub!('it', 'that I love it')
  else
    s.gsub!('it', 'that I hate it')
  end
  co +=1
end

puts s

n = gets.to_i
0.upto(n-1) do |i|
	if i != 0
		print 'that '
	end
	if i % 2 == 0
		print 'I hate '
	else
		print 'I love '
	end
end
puts 'it'

def love(n)
	phrase = ""
	while n > 0
		phrase << "I hate "
		n = n - 1
		phrase << "that " if n > 0
		phrase << "I love " if n > 0
		n = n - 1
		phrase << "that " if n > 0
	end
	phrase << "it"
	return phrase
end

n = gets.chomp.to_i
puts(love(n))

n=gets.to_i
res=""
n.times {|i|
    res+="that " if res.size>0
    if i.even?
        res+="I hate "
    else
        res+="I love "
    end
}
res+="it"
puts res

n=gets.chomp().to_i
s3="I love"
s2=" that "
s1="I hate"
s4="I hate"
s5=" it"
i=2
while i<=n
  if i%2==0
    s1+=s2+s3
  else
    s1+=s2+s4
  end
  i=i+1
end
s1=s1+s5
puts(s1)

n = gets.to_i

pc = 'I hate it'
p1 = 'I love that '
p2 = 'I hate that '
i = 1
while(i < n) do
    i = i + 1;
    if (i % 2 ==1)
        pc = p1 + pc
    else
        pc = p2 + pc
    end
end

puts pc

s = ""
n = gets.to_i

for i in (1..n-1)
  (n-i)%2 == 0 ? s += "I hate that " : s += "I love that "
end

puts s + "I hate it"

s = ""
n = gets.to_i

for i in (1..n-1)
  (n-i)%2 == 1 ? s += "I hate that " : s += "I love that "
end

puts s + (n%2 == 0 ? "I love it" : "I hate it")

LEVELS = ['I hate that I love that I hate it', 'I hate it', 'I hate that I love it']

level = gets.to_i

puts LEVELS[level % 3]

n = gets.chomp.to_i

n.times do |i|
	if (i + 1).even? then
		print "I love it "
	else
		print "I hate that"
	end
end
puts

n = gets.to_i
puts n.times.map{|i| i.even? ? 'I hate it' : 'I love it'}.join(' that ')

input = gets.chomp.to_i
1.upto(input-1){ |i|
	 if i.even?
		print "I Love "
	else
		print "I Hate "
	end
	print "that "
}
if input.even?
		print "I Love it"
else
		print "I Hate it"
	end

n = gets.chomp.to_i
sentence = ""

for i in (1...n+1)
  if i % 2 != 0
    sentence += "I hate"
  else
    sentence += "I love"
  end
  if i != n
    sentence += " that "
  end
end

sentence += " It"
puts sentence

word = ["I hate", "I love"]
input = gets.chomp!.to_i
p word.cycle.take(input).join(" that ").concat(' it')

word = ["I hate", "I love"]
input = gets.chomp!.to_i
p word.cycle.take(input).join(" that ").concat(' it ')

layers = gets.to_i
reduced = 0;
result = ""
until layers == 0
    if reduced == 0
        result = result + "I hate"
    end

    if reduced == 1
        result = result + " that I love"
    end

    if reduced > 1
        if reduced % 2 == 0
            result = result + " that I hate"
        end
        if reduced % 3 == 0
            result = result + " that I love"
        end
    end

    layers = layers - 1
    reduced = reduced + 1
end
result = result + " it"
puts result

layers = gets.to_i
reduced = 0;
result = ""
until layers == 0
    if reduced == 0
        result = result + "I hate it"
    end

    if reduced == 1
        result = result + " that I love it"
    end

    if reduced > 1
        if reduced % 2 == 0
            result = result + " that I hate it"
        end
        if reduced % 3 == 0
            result = result + " that I love it"
        end
    end

    layers = layers - 1
    reduced = reduced + 1
end
puts result

numbers = gets.chomp
start = 1
result = ""
while start <= numbers.to_i
    if start > 1
        result = result + " that "
    end
    if start % 2 == 0
        result = result + "I hate it"
    else
        result = result + "I love it"
    end
    start = start + 1
end
puts result

numbers = gets.chomp
start = 1
result = ""
while start <= numbers.to_i
    if start > 1
        result = result + " that "
        if start % 2 == 0
            result = result + "I hate it"
        else
            result = result + "I love it"
        end
    else
        result = result + "I hate it"
    end
    # if start % 2 == 0 && start > 1
    #     result = result + "I hate it"
    # elsif
    #     result = result + "I love it"
    # end
    start = start + 1
end
puts result

n=gets.to_i
a=["I hate","I love","I hate"]
str=""
n.times{|i|
	if i==0 then
		str=a[0]
	else
		str=str+" that "+a[i%3]
	end
}
puts str+" it"

n=gets.chomp().to_i
s3="i love it"
s2=" that "
s1="i hate it"
s4="i hate it"
i=2
while i<=n
  if i%2==0
    s1+=s2+s3
  else
    s1+=s2+s4
  end
  i=i+1
end
puts(s1)

n=gets.chomp().to_i
s3=" ilove it "
s2=" that "
s1="i hate it "
s4="i hate it "
i=2
while i<=n
  if i%2==0
    s1+=s2+s3
  else
    s1+=s2+s4
  end
  i=i+1
end
puts(s1)

n=gets.chomp().to_i
s3="I love it"
s2=" that "
s1="I hate it"
s4="I hate it"
i=2
while i<=n
  if i%2==0
    s1+=s2+s3
  else
    s1+=s2+s4
  end
  i=i+1
end
puts(s1)


Vasya collects coins: he has exactly one coin for every year from 1 to n. Naturally, Vasya keeps all the coins in his collection in the order in which they were released. Once Vasya's younger brother made a change — he took all the coins whose release year dated from l to r inclusively and put them in the reverse order. That is, he took a certain segment [l, r] and reversed it. At that the segment's endpoints did not coincide. For example, if n = 8, then initially Vasya's coins were kept in the order 1 2 3 4 5 6 7 8. If Vasya's younger brother chose the segment [2, 6], then after the reversal the coin order will change to 1 6 5 4 3 2 7 8. Vasya suspects that someone else could have spoilt the permutation after his brother. Help him to find that out. Check if the given permutation can be obtained from the permutation 1 2 ... n using exactly one segment reversal. If it is possible, find the segment itself.

The first line contains an integer n (1 ≤ n ≤ 1000) which is the number of coins in Vasya's collection. The second line contains space-separated n integers which are the spoilt sequence of coins. It is guaranteed that the given sequence is a permutation, i.e. it contains only integers from 1 to n, and every number is used exactly 1 time.

If it is impossible to obtain the given permutation from the original one in exactly one action, print 0 0. Otherwise, print two numbers l r (1 ≤ l < r ≤ n) which are the endpoints of the segment that needs to be reversed to obtain from permutation 1 2 ... n the given one.

def check a,n
  i = 0
  z = []
  while i < n do
    z[i]= i+1
    if a[i]!=z[i] then
      return false
    end
    i += 1
  end
  return true
end

def get_sub_array a,i
  j = i
  res = []
  while j < a.length-1 do
    if a[j]-a[j+1]==1 then
      if !res.include?(a[j]) then
       res << a[j]
      end
      if !res.include?(a[j+1]) then
        res << a[j+1]
      end
    else
      if !res.include?(a[j]) then
       res << a[j]
      end
      break
    end
    j+=1
  end
  return res.reverse
end

def conc a,b,i,n
  j,count = 0,0
        while j < a.length do
            if i == j - 1 then
                count = i;
        j2 = 0
                while j2 < b.length do
                    a[count] = b[j2];
          count += 1
          j2 += 1
        end
                break
      end
      j += 1
    end
        if check(a, n) then
            return true
    end
    return false
end

def solve!
  m = gets.to_i
  s = gets.chomp
  a = s.split(" ")
  q = a.map { |x| x.to_i }
  if check(q,m) then
    print "0 0"
  else
    i = 0
    for c in q
      if c != i+1 then
        z = get_sub_array(q , i)
        if conc(q, z, i, m)then
          print "#{z[0]} #{z[z.length - 1]}"
          break
        else
          print "0 0"
          break
        end
      end
      i += 1
    end
  end
end

solve!

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
l = r = 0
arr.each_with_index do |a,i|
	s = i + 1
	if a != s
		l = s
		r = a
		break
	end
end
# puts "#{l} #{r}"
if l != 0 && r != 0
	(l..r).each do |z|
		i = z-1
		arr[i] = z
	end
	val = (1..n).to_a
	# puts "#{arr} #{val}"
	if arr != val
		l = 0
		r = 0
	end
end
puts "#{l} #{r}"

n = gets.to_i
s = gets.chomp!.split(" ")
mas = Array.new()

for i in 0..n-1
  mas[i] = s[i].to_i
end

start,finish = 0, 0
wrong = false
for i in 1..n-1
  if mas[i] == mas[i-1] - 1
    start = i if start == 0
  else
    finish = i if start != 0 && finish == 0
  end
end

finish = n if finish == 0 && start != 0

for i in 0..start-3
  wrong = true if mas[i] + 1 != mas[i+1]
end
for i in finish..n-2
  wrong = true if mas[i] + 1 != mas[i+1]
end

puts wrong ? "0 0" : start.to_s + " "+finish.to_s

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
l = r = 0
arr.each_with_index do |a,i|
	s = i + 1
	if a != s
		l = s
		r = a
		break
	end
end
# puts "#{l} #{r}"
if l != 0 && r != 0
	x = r
	arr[(l-1)..(r-1)].each do |a|
		if a != x
			l = 0
			r = 0
			break
		end
		x -= 1
	end
end
puts "#{l} #{r}"

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
l = r = 0
arr.each_with_index do |a,i|
	s = i + 1
	if a != s
		l = s
		r = a
		break
	end
end
# puts "#{l} #{r}"
if l != 0 && r != 0
	(l..r).each do |z|
		i = z-1
		arr[i] = z
	end
	val = (1..n).to_a
	puts "#{arr} #{val}"
	if arr != val
		l = 0
		r = 0
	end
end
puts "#{l} #{r}"

n = gets.to_i
s = gets.chomp!.split(" ")
mas = Array.new()

for i in 0..n-1
  mas[i] = s[i].to_i
end

start,finish = 0, 0
wrong, first_wrong = false, true
for i in 1..n-1
  if mas[i] == mas[i-1] - 1
    start = i if start == 0

    wrong = true if finish != 0
  else
    finish = i if start != 0 && finish == 0

    first_wrong ? first_wrong = false : wrong = true if mas[i-1] + 1 != mas[i]
  end
end

finish = n if finish == 0 && start != 0

puts wrong ? "0 0" : start.to_s + " "+finish.to_s

n = gets.to_i
s = gets.chomp!.split(" ")
mas = Array.new()

for i in 0..n-1
  mas[i] = s[i].to_i
end

start,finish = 0, 0
for i in 1..n-1
  if mas[i] == mas[i-1] - 1
    if start == 0
      start = i
    end
    if finish != 0
      puts "0 0"
      exit
    end
  else
    if start != 0 && finish == 0
      finish = i
    end
  end
end

if finish == 0 && start != 0
  finish = n
end

puts start.to_s + " "+finish.to_s

n = gets.to_i
s = gets.chomp!.split(" ")
mas = Array.new()

for i in 0..n-1
  mas[i] = s[i].to_i
end

start,finish = 0, 0
wrong, first_wrong = false, true
for i in 1..n-1
  if mas[i] == mas[i-1] - 1
    start = i if start == 0

    wrong = true if finish != 0
  else
    finish = i if start != 0 && finish == 0

    first_wrong ? firstwrong = false : wrong = true if mas[i-1] + 1 != mas[i]
  end
end

finish = n if finish == 0 && start != 0

puts wrong ? "0 0" : start.to_s + " "+finish.to_s


Stepan likes to repeat vowel letters when he writes words. For example, instead of the word "pobeda" he can write "pobeeeedaaaaa".Sergey does not like such behavior, so he wants to write a program to format the words written by Stepan. This program must combine all consecutive equal vowels to a single vowel. The vowel letters are "a", "e", "i", "o", "u" and "y".There are exceptions: if letters "e" or "o" repeat in a row exactly 2 times, like in words "feet" and "foot", the program must skip them and do not transform in one vowel. For example, the word "iiiimpleeemeentatiioon" must be converted to the word "implemeentatioon".Sergey is very busy and asks you to help him and write the required program.

The first line contains the integer n (1 ≤ n ≤ 100 000) — the number of letters in the word written by Stepan. The second line contains the string s which has length that equals to n and contains only lowercase English letters — the word written by Stepan.

Print the single string — the word written by Stepan converted according to the rules described in the statement.

pobeda implemeentatioon aeiouyaeeioouy aoiuyeggghhh

gets
puts gets.chomp.squeeze("aiuy").gsub(/e{3,}/, 'e').gsub(/o{3,}/, 'o')

while l = gets
	a = gets
	b = a.squeeze("aiuy")
	c = b.gsub(/e{3,}/, 'e').gsub(/o{3,}/, 'o')
	print c
end

n = gets
s = gets
i = 0
last = '$'
vowels = 'aiuy'
ecount = 0
ocount = 0
s.split("").each do |c|
    if c == 'e'
        ecount += 1
    elsif ecount > 0
        if ecount == 2
            print 'ee'
        else
            print 'e'
        end
        ecount = 0
    end

    if c == 'o'
        ocount += 1
    elsif ocount > 0
        if ocount == 2
            print 'oo'
        else
            print 'o'
        end
        ocount = 0
    end

    if vowels.include? c
        unless c === last
            print c
        end
    elsif c != 'e' and c != 'o'
        print c
    end

    last = c
end
# puts "#{s}"

# require 'rubygems'
# gem 'capataz'
# require 'capataz'
#
# class A
#   def m1
#     1
#   # end
#
#   def m2
#     2
#   end
#
#   def self.m3
#     3
#   end
#
#   def self.m4
#     4
#   end
# end
#
# Capataz.config do
#
#   set_max_allowed_iterations 6
#
#   set_max_allowed_invocations :new, 6, :casecmp, 5
#
#   deny_declarations_of :module
#
#   deny_invoke_of :constantize
#
#   allowed_constants A, Set
#
#   #deny_for A, :m2
#
#   deny_on A, :m4
#
# end
#
# puts Capataz.eval <<-RUBY
#
# x = A.new
# x.m1
# x.m2
#
# A.m4
#
# RUBY

def cons(x)
  if ['a','e','i','o','u','y'].include?(x)
    return false
  else
    return true
  end
end

def a(x)
  if ['a','i','u','y'].include?(x)
    return true
  else
    return false
  end
end

def last(i,n,s)
  while true
    if i == n - 1 or s[i + 1] != s[i]
      return i
    end
    i += 1
  end
end

n = readline().to_i
s = readline()

i = 0
while i < n
  if cons(s[i])
    print s[i]
  elsif a(s[i])
    unless i < n - 1 and s[i] == s[i + 1]
      print s[i]
    else
    end
  else
    print s[i]
    pos = last(i,n,s)
    if pos > i + 1
      i = pos
    end
  end
  i += 1
end

def is_vowel( ch )
  vowel = "aeiouy"
  for i in 0..( vowel.length - 1 ) do
    if vowel[ i ] == ch then
      return true
    end
  end
  return false
end

N = gets.chomp.to_i
S = gets.chomp
ans = ""
prev = '$'
ptr = 0
while ptr < N do
  if not is_vowel( S[ ptr ] ) then
    ans.concat( S[ ptr ] )
    ptr += 1
    next # continue の身代わりらしい
  end
  nxt = ptr
  while nxt < N do
    if S[ nxt ] != S[ ptr ] then
      break
    end
    nxt += 1
  end
  if nxt - ptr == 2 and ( S[ ptr ] == 'e' or S[ ptr ] == 'o' ) then
    ans.concat( S[ ptr ] )
    ans.concat( S[ ptr ] )
  else
    ans.concat( S[ ptr ] )
  end
  ptr = nxt
end
print ans

n = gets.to_i
n = n-1
s = gets.chomp
last = '#'
cnt = 0
for i in (0..n)
    if s[i] != last then
        if "oe".count(last) > 0 and cnt == 2 then
            print last
        end
    end
    if "aeiouy".count(s[i]) > 0 then
        if s[i] == last then
            cnt = cnt + 1
        else
            print s[i]
            cnt = 1
        end
    else
        print s[i]
        cnt = 1
    end
    last = s[i]
end
if "oe".count(last) > 0 and cnt == 2 then
    print last
end
print "\n"

gets
puts gets.chomp.squeeze("aiuy").sub(/e{3,}/, 'e').sub(/o{3,}/, 'o')

while l = gets
	a = gets
	b = a.squeeze("aiuy")
	c = b.gsub(/e{3,}/, 'e').gsub(/o{3,}/, 'o')
	p c
end

while l = gets
	a = gets
	b = a.squeeze("aiuy")
	c = b.gsub(/e{3,}/, 'e').gsub(/o{3,}/, 'o')
	p "#{c}\n"
end