Allen dreams of one day owning a enormous fleet of electric cars, the car of the future! He knows that this will give him a big status boost. As Allen is planning out all of the different types of cars he will own and how he will arrange them, he realizes that he has a problem. Allen's future parking lot can be represented as a rectangle with $$$4$$$ rows and $$$n$$$ ($$$n \le 50$$$) columns of rectangular spaces, each of which can contain at most one car at any time. He imagines having $$$k$$$ ($$$k \le 2n$$$) cars in the grid, and all the cars are initially in the second and third rows. Each of the cars also has a different designated parking space in the first or fourth row. Allen has to put the cars into corresponding parking places.  Illustration to the first example. However, since Allen would never entrust his cars to anyone else, only one car can be moved at a time. He can drive a car from a space in any of the four cardinal directions to a neighboring empty space. Furthermore, Allen can only move one of his cars into a space on the first or fourth rows if it is the car's designated parking space. Allen knows he will be a very busy man, and will only have time to move cars at most $$$20000$$$ times before he realizes that moving cars is not worth his time. Help Allen determine if he should bother parking his cars or leave it to someone less important.

The first line of the input contains two space-separated integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 50$$$, $$$1 \le k \le 2n$$$), representing the number of columns and the number of cars, respectively. The next four lines will contain $$$n$$$ integers each between $$$0$$$ and $$$k$$$ inclusive, representing the initial state of the parking lot. The rows are numbered $$$1$$$ to $$$4$$$ from top to bottom and the columns are numbered $$$1$$$ to $$$n$$$ from left to right. In the first and last line, an integer $$$1 \le x \le k$$$ represents a parking spot assigned to car $$$x$$$ (you can only move this car to this place), while the integer $$$0$$$ represents a empty space (you can't move any car to this place). In the second and third line, an integer $$$1 \le x \le k$$$ represents initial position of car $$$x$$$, while the integer $$$0$$$ represents an empty space (you can move any car to this place). Each $$$x$$$ between $$$1$$$ and $$$k$$$ appears exactly once in the second and third line, and exactly once in the first and fourth line.

If there is a sequence of moves that brings all of the cars to their parking spaces, with at most $$$20000$$$ car moves, then print $$$m$$$, the number of moves, on the first line. On the following $$$m$$$ lines, print the moves (one move per line) in the format $$$i$$$ $$$r$$$ $$$c$$$, which corresponds to Allen moving car $$$i$$$ to the neighboring space at row $$$r$$$ and column $$$c$$$. If it is not possible for Allen to move all the cars to the correct spaces with at most $$$20000$$$ car moves, print a single line with the integer $$$-1$$$.

NoteIn the first sample test case, all cars are in front of their spots except car $$$5$$$, which is in front of the parking spot adjacent. The example shows the shortest possible sequence of moves, but any sequence of length at most $$$20000$$$ will be accepted.In the second sample test case, there is only one column, and the cars are in the wrong order, so no cars can move and the task is impossible.

N, K = gets.split.map(&:to_i)
goal = gets.split.map(&:to_i)
road = gets.split.map(&:to_i)
road.concat(gets.split.map(&:to_i).reverse)
goal.concat(gets.split.map(&:to_i).reverse)

def index2road(i)
    i %= 2*N
    i < N ? [2, i+1] : [3, 2*N-i]
end
def index2goal(i)
    i %= 2*N
    i < N ? [1, i+1] : [4, 2*N-i]
end

goaled = []
ans = []
(2*N).times do |i|
    if goal[i] > 0 && goal[i] == road[i]
        num = goal[i]
        ans.push([num] + index2goal(i))
        goaled[num] = true
        road[i] = 0
    end
end

if road.count(0) == 0
    puts -1
    exit
end

while goaled.count{|v| v} < K
    index = road.index(0)
    (index+1).upto(index+2*N-1) do |i|
        i %= 2*N
        next if road[i] == 0
        ans.push([road[i]] + index2road(i-1))
        road[(i-1) % (2*N)] = road[i]
        road[i] = 0
    end
    (2*N).times do |i|
        if goal[i] > 0 && goal[i] == road[i]
            num = goal[i]
            ans.push([num] + index2goal(i))
            goaled[num] = true
            road[i] = 0
        end
    end
end

puts ans.length
ans.each do |arr|
    puts arr.join(' ')
end

n, k = gets.strip.split.map(&:to_i)
r1 = gets.strip.split.map(&:to_i)
r2 = gets.strip.split.map(&:to_i)
r3 = gets.strip.split.map(&:to_i)
r4 = gets.strip.split.map(&:to_i)

if 2*n == k
  match_found = false
  0.upto(n-1).each do |i|
    if r1[i] == r2[i]
      match_found = true
      break
    end
  end
  if !match_found
    0.upto(n-1).each do |i|
      if r3[i] == r4[i]
        match_found = true
        break
      end
    end
  end
  if !match_found
    puts -1
    exit
  end
end

seq = []
0.upto(n-1).each do |i|
  if r2[i]!=0 && r1[i] == r2[i]
    seq << [r2[i], 1, i+1]
    r2[i] = 0
  end
end
0.upto(n-1).each do |i|
  if r3[i]!=0 && r3[i] == r4[i]
    seq << [r3[i], 4, i+1]
    r3[i] = 0
  end
end

loop do
  if r2[n-1]!=0 && r2[n-1]==r1[n-1]
    seq << [r2[n-1], 1, n]
    r2[n-1] = 0
  end
  if r2[n-1]!=0 && r3[n-1]==0
    seq << [r2[n-1], 3, n]
    r3[n-1] = r2[n-1]
    r2[n-1] = 0
  end
  (n-2).downto(0).each do |i|
    if r2[i]!=0 && r2[i]==r1[i]
      seq << [r2[i], 1, i+1]
      r2[i] = 0
    end
    if r2[i]!=0 && r2[i+1]==0
      seq << [r2[i], 2, i+2]
      r2[i+1] = r2[i]
      r2[i] = 0
    end
  end
  if r3[0]!=0 && r3[0]==r4[0]
    seq << [r3[0], 4, 1]
    r3[0] = 0
  end
  if r3[0]!=0 && r2[0]==0
    seq << [r3[0], 2, 1]
    r2[0] = r3[0]
    r3[0] = 0
  end
  (1).upto(n-1).each do |i|
    if r3[i]!=0 && r3[i]==r4[i]
      seq << [r3[i], 4, i+1]
      r3[i] = 0
    end
    if r3[i]!=0 && r3[i-1]==0
      seq << [r3[i], 3, i]
      r3[i-1] = r3[i]
      r3[i] = 0
    end
  end
  break if r2.uniq == [0] && r3.uniq == [0]
end

puts seq.size
seq.each do |s|
  puts s.join(" ")
end


You are given an array $$$a_1, a_2, \dots , a_n$$$ consisting of integers from $$$0$$$ to $$$9$$$. A subarray $$$a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$$$ is good if the sum of elements of this subarray is equal to the length of this subarray ($$$\sum\limits_{i=l}^{r} a_i = r - l + 1$$$).For example, if $$$a = [1, 2, 0]$$$, then there are $$$3$$$ good subarrays: $$$a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$$$ and $$$a_{1 \dots 3} = [1, 2, 0]$$$.Calculate the number of good subarrays of the array $$$a$$$.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of the array $$$a$$$. The second line of each test case contains a string consisting of $$$n$$$ decimal digits, where the $$$i$$$-th digit is equal to the value of $$$a_i$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case print one integer — the number of good subarrays of the array $$$a$$$.

NoteThe first test case is considered in the statement.In the second test case, there are $$$6$$$ good subarrays: $$$a_{1 \dots 1}$$$, $$$a_{2 \dots 2}$$$, $$$a_{1 \dots 2}$$$, $$$a_{4 \dots 4}$$$, $$$a_{5 \dots 5}$$$ and $$$a_{4 \dots 5}$$$. In the third test case there is only one good subarray: $$$a_{2 \dots 6}$$$.

t = gets.to_i
ans = []
t.times do
  n = gets.to_i
  a = gets.chomp.split("").map(&:to_i)
  arr = Array.new(n+1,0)
  arr[0] = 0
  cnt = Hash.new(0)
  n.times do |i|
    arr[i+1] = arr[i] + a[i]
    cnt[arr[i]-i] += 1
  end
  cnt[arr[n]-n] += 1
  sum = 0
  cnt.each_value do |x|
    sum += x * (x-1) / 2
  end
  ans << sum
end
ans.each do |x|
  puts x
end


Now it's time of Olympiads. Vanya and Egor decided to make his own team to take part in a programming Olympiad. They've been best friends ever since primary school and hopefully, that can somehow help them in teamwork.For each team Olympiad, Vanya takes his play cards with numbers. He takes only the cards containing numbers 1 and 0. The boys are very superstitious. They think that they can do well at the Olympiad if they begin with laying all the cards in a row so that:  there wouldn't be a pair of any side-adjacent cards with zeroes in a row;  there wouldn't be a group of three consecutive cards containing numbers one. Today Vanya brought n cards with zeroes and m cards with numbers one. The number of cards was so much that the friends do not know how to put all those cards in the described way. Help them find the required arrangement of the cards or else tell the guys that it is impossible to arrange cards in such a way.

The first line contains two integers: n (1 ≤ n ≤ 106) — the number of cards containing number 0; m (1 ≤ m ≤ 106) — the number of cards containing number 1.

In a single line print the required sequence of zeroes and ones without any spaces. If such sequence is impossible to obtain, print -1.

n, m = gets.split.map(&:to_i)
len = n + m
if n > m + 1
  p -1
  exit
end
if n == 0
  if m <= 2
    p "1" * m
  else
    p -1
  end
  exit
end
s = "01" * (n - 1) + "0"
m -= (n - 1)

k = [m, 2].min
m -= k
s = "1" * k + s
k = [m, 2].min
m -= k
s = s + "1" * k
if m > n - 1
  p -1
  exit
end
s = s[0, 2] + "011" * m + s[2 + 2 * m, s.length - (2 + 2 * m)] if m != 0
puts s

m,n = gets.chomp!.split.map { |e|  e.to_i}

if n+1 < m
    p -1
    exit
end

if (m+1)*2 < n
    p -1
    exit
end

if n == m
    n.times{printf "10" }
    puts ""

elsif m == n+1
    n.times{print "01"}
    puts 0
elsif (m+1)*2 == n
    m.times{printf "110"}
    puts "11"
elsif m*2 == n
    m.times{print "110"}
    puts ""
else
    k = n - 1 - m
    (k).times {print "110"}
    (m-k).times{print "10"}
    puts "1"
end

z,o = gets.split(" ").map(&:to_i)
if z >= o
	if o < z-1
		ans = "-1"
	else
		ans = "01" * (z-1)
		ans << "0"
		ans << "1" if z  == o
	end
else
	spaces = z + 1
	if o > (spaces * 2)
		ans = "-1"
	else
		pairs = o - spaces
		rem_ones = o - pairs * 2
		arr = ["11"] * pairs + ["1"] * rem_ones
		ans = arr.join("0")
	end
end

puts ans

n, m = gets.split.collect{|i| i.to_i}
if m < n-1 or m > (n+1)*2
    puts -1
elsif m == n-1
    puts (['0']*n).join('1')
elsif m == n
    puts (['0']*n).join('1')+'1'
else
    cnt = m-n-1
    puts (['11']*cnt+['1']*(n+1-cnt)).join('0')
end

#f = File.open('test1.txt', 'r')
f = STDIN

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

n, m = gets(f)
res =
    if m < n - 1
      -1
    elsif m <= (n + 1) * 2 #1010101010101010101
      remainder = m - (n - 1)
      s = '0'
      (n - 1).times do
        s << '1'
        if remainder > 0
          s << '1'
          remainder -= 1
        end
        s << '0'
      end
      if remainder > 0
        s.insert 0, ('1' * [remainder, 2].min)
        remainder -= [remainder, 2].min
      end
      if remainder > 0
        s.concat ('1' * remainder)
      end
      s
    else
      -1
    end
puts res

n, m = gets.split.collect{|i| i.to_i}
if m < n+1 or m > (n+1)*2
    puts -1
    exit
end
cnt = m-n-1
puts (['11']*cnt+['1']*(n+1-cnt)).join('0')


ZS the Coder and Chris the Baboon are travelling to Udayland! To get there, they have to get on the special IOI bus. The IOI bus has n rows of seats. There are 4 seats in each row, and the seats are separated into pairs by a walkway. When ZS and Chris came, some places in the bus was already occupied.ZS and Chris are good friends. They insist to get a pair of neighbouring empty seats. Two seats are considered neighbouring if they are in the same row and in the same pair. Given the configuration of the bus, can you help ZS and Chris determine where they should sit?

The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the number of rows of seats in the bus. Then, n lines follow. Each line contains exactly 5 characters, the first two of them denote the first pair of seats in the row, the third character denotes the walkway (it always equals '|') and the last two of them denote the second pair of seats in the row.  Each character, except the walkway, equals to 'O' or to 'X'. 'O' denotes an empty seat, 'X' denotes an occupied seat. See the sample cases for more details.

If it is possible for Chris and ZS to sit at neighbouring empty seats, print "YES" (without quotes) in the first line. In the next n lines print the bus configuration, where the characters in the pair of seats for Chris and ZS is changed with characters '+'. Thus the configuration should differ from the input one by exactly two charaters (they should be equal to 'O' in the input and to '+' in the output). If there is no pair of seats for Chris and ZS, print "NO" (without quotes) in a single line. If there are multiple solutions, you may print any of them.

NoteNote that the following is an incorrect configuration for the first sample case because the seats must be in the same pair.O+|+XXO|XXOX|OOXX|OXOO|OOOO|XX

n = gets.to_i

f =  false
out = []
n.times do
    s = gets.strip

    if !f && s["OO"]
        f = true
        s["OO"]="++"
    end

    out << s
end
if f
    puts "YES",out
else
    puts "NO"
end

n = gets.to_i

f =  false
out = []
n.times do
    l, r = gets.strip.split("|")

    if f == false && l == "OO"
        l = "++"
        f = true
    end

    if f == false && r == "OO"
        r = "++"
        f = true
    end

    out << l + "|" + r
end
if f
    puts "YES",out
else
    puts "NO"
end

n = gets.to_i

f =  false
out = []
n.times do
    s = gets.strip

    if !f && s.include?("OO")
        f = true
        s = s.sub("OO",'++')
    end

    out << s
end
if f
    puts "YES",out
else
    puts "NO"
end

n = gets.to_i

f =  false
out = []
n.times do
    s = gets.strip

    if !f && s.include?("OO")
        f = true
        s["OO"]="++"
    end

    out << s
end
if f
    puts "YES",out
else
    puts "NO"
end

#!/usr/bin/ruby
gets;a=$<.map(&:chomp)
if a.any?{|e|
	if e[0,2]=='OO'
		e[0,2]='++'
	elsif e[3,2]=='OO'
		e[3,2]='++'
	else
		false
	end
}
	puts :YES
	puts a
else
	puts :NO
end

n = gets.chomp.to_i

s = "".tap do |out|
  n.times { out << gets }
end

replaced = s.sub(/OO/, '++')
if s == replaced
  puts "NO"
else
  puts "YES"
  puts replaced
end

n = gets.to_i
ans = 'NO'
res = []
n.times do
  s1, s2 = gets.chomp.split("|")
  if s1 == "OO"
    s1 = "++" unless ans == 'YES'
    ans = 'YES'
  elsif s2 == "OO"
    s2 = "++" unless ans == 'YES'
    ans = 'YES'
  end
  res << [s1, s2].join("|")
end

if ans == 'YES'
  puts ans
  puts res
else
  puts ans
end

n = gets.strip.to_i
first = Array.new(n)
second = Array.new(n)
n.times do |i|
    first[i], second[i] = gets.strip.split("|")
end
found = false
n.times do |i|
    if first[i] == "OO"
        first[i] = "++"
        found = true
        break
    end
    if second[i] == "OO"
        second[i] = "++"
        found = true
        break
    end
end
if found
    puts "YES"
    n.times do |i|
        print first[i]
        print "|"
        print second[i]
        print "\n"
    end
else
    puts "NO"
end

n=gets.to_i
s=STDIN.read
if s.scan("OO").size>0 then
	s.sub!("OO","++")
	puts "YES",s
else
	puts "NO"
end

t = gets.chomp.to_i
change = true
arr = []
t.times.each do
  arr << gets.chomp
end
arr.each do |ts|
  if ts.include?('OO') and change
    ts.sub!('OO','++')
    change = false
  end
end

if change
  puts "NO"
else
  puts "YES"
  arr.each do |rr|
    puts rr
  end
end

n = gets.to_i
s = []
n.times do |i|
	s[i] = gets.chomp
end
x = false
for i in 0...n
	if x == false
		if s[i][0...2] == 'OO'
			s[i][0...2] = '++'
			x = true
		elsif s[i][3...5] == 'OO'
			s[i][3...5] = '++'
			x = true
		end

	end
end
if x == false
	puts 'NO'
else
	puts 'YES'
	0.upto(n-1) do |i|
		puts s[i]
	end
end

s = ''
(1..gets.chomp.to_i).each do
  s << gets.chomp << '#'
end
begin
  s[/OO/] = '++'
  puts 'YES'
  s.split('#').each { |q| puts q }
rescue
  puts 'NO'
end

def answer(a, b, lines)
  puts "YES"
  for i in 0..(lines.length-1) do
    if (i == a)
      if (b == 0)
        print "++|" + lines[i][1] + "\n"
      else
        print lines[i][0] + "|++\n"
      end
    else
      print lines[i][0] + "|" + lines[i][1] + "\n"
    end
  end
end

n = gets.strip.to_i
lines = []
n.times do
  input = gets.strip
  lines << [input[0..1], input[3..4]]
end
found = false
for i in 0..(lines.length-1) do
  if (lines[i][0] == "OO")
    answer(i, 0, lines)
    found = true
    break
  end
  if (lines[i][1] == "OO")
    answer(i, 1, lines)
    found = true
    break
  end
end
if not found
  puts "NO"
end

gets
s=$<.read
res=s.sub "OO", "++"
if res!=s
    puts "YES"
    puts res
else
    puts "NO"
end

# coding: utf-8

# if __FILE__ == $0
# end

n = gets.to_i
rows = []
n.times {
    rows << gets.chomp.split("|")
}
found = false
for i in 0...n
    if rows[i][0] == "OO"
        found = true
        rows[i][0] = "++"
        break
    end

    if rows[i][1] == "OO"
        found = true
        rows[i][1] = "++"
        break
    end
end

if found
    puts "YES"
    for row in rows
        puts row.join("|")
    end
else
    puts "NO"
end

n = Integer(gets.chomp)
bus = Array.new(n)
for i in 0...n
    bus[i] = gets.chomp.split("")
end

can_seat_together = false

for i in 0...n
    if bus[i][0] == "O" && bus[i][1] == "O"
        can_seat_together = true
        bus[i][0] = "+"
        bus[i][1] = "+"
        break
    elsif bus[i][3] == "O" && bus[i][4] == "O"
        can_seat_together = true
        bus[i][3] = "+"
        bus[i][4] = "+"
        break
    end
end

if can_seat_together
    puts " YES"
    for i in 0...n
        print bus[i].join
        puts
    end
else
    puts "NO"
end

rows = gets.chomp.to_i
seats = []
for i in (0..rows-1)
	seats[i] = gets.chomp
end

has_seat = -1
for i in (0..rows-1)
	split = seats[i].split("")
	for x in (0..split.size-1)
		if split[x] == "O" && split[x+1] == "O"
			split[x] = "+"
			split[x+1] = "+"
			puts "YES"
			new_seats = []
			for a in (0..4)
				new_seats << split[a]
			end
			has_seat = i
			break
		end
	end
	if has_seat != -1
		break
	end
end

if has_seat != -1
	for i in (0..has_seat)
		if i < has_seat
			puts seats[i]
		else
			for x in (0..4)
				if x == 4
					puts new_seats[x]
				else
					print new_seats[x]
				end
			end
		end
	end
	for i in (has_seat+1..rows-1)
		puts seats[i]
	end
else
	puts "NO"
end

require 'pp'
rows = gets.chomp.to_i
row = []
fact = "NO"
rows.times do |i|

  row << gets.chomp.to_s
  values = row[-1].split('|')
  if values.include?("OO") && fact == "NO"
  	fact = "YES"
  	row[-1].sub!("OO", "++")
  end
 end
 puts fact
 puts row if fact == "YES"

d = gets.to_i.times.map{|x| gets.strip.split("|")}
ans = false
list = []
d.each do |x|
    if ans == false && x.include?("OO")
        if x[0] == "OO"
            list.push(['++', x[1]])
        else
            list.push([x[0],'++'])
        end
        ans = true
    else
        list.push(x)
    end
end
puts (ans)? 'YES' : 'NO'
if ans
    list.each{|x| puts x.join('|')}
end

n = gets.chomp
array = Array.new(n.to_i) { |i| gets.chomp}

found = false
array.each do |i|
  if i[0] == 'O' && i[1] == 'O'
    i[0] = '+'
    i[1] = '+'
    found = true
    break
  elsif i[3] == 'O' && i[4] == 'O'
    i[3] = '+'
    i[4] = '+'
    found = true
    break
  end
end

if found == true
  puts 'YES'
  puts array
else
  puts 'NO'
end

n = gets.to_i
a = n.times.map{gets.chomp.chars}
ans = false
a.each do |x|
  if x[0] == 'O' && x[1] == 'O'
    x[0], x[1] = '+', '+'
    ans = true
    break
  elsif x[3] == 'O' && x[4] == 'O'
    x[3], x[4] = '+', '+'
    ans = true
    break
  end
end
puts ans ? 'YES' : 'NO'
puts a.map{|x| x.join('')} if ans

n = gets.to_i
f = false
a = Array.new(n, "")
n.times{|i|
    x, y = gets.chomp.split('|')
    if !f && x == "OO"
        f = true
        a[i] += "++"
    else
        a[i] += x
    end
    a[i] += "|"
    if !f && y == "OO"
        f = true
        a[i] += "++"
    else
        a[i] += y
    end
    }
if f
    puts "YES"
    puts a
else
    puts "NO"
end

n = gets.to_i
a = 1.upto(n).map { gets.chomp }

index = a.find_index { |s| s.include? 'OO' }

if index
  puts 'YES'
  a[index].sub!('OO', '++')

  puts a.join("\n")
else
  puts 'NO'
end

n=readline.to_i
result = []
found = false
(1..n).each do
	seats = readline.split('|').map{|x| x.strip}
	seats.each_index{ |x|
		if !found && seats[x]=='OO'
			seats[x]='++'
			found=true
		end
	}
	result << seats.join('|')
end
puts found ? 'YES' : 'NO'
puts result if found

n = gets.to_i

f =  false
out = []
n.times do
    s = gets.strip

    if s.include?("OO")
        f = true
        s = s.sub("OO",'++')
    end

    out << s
end
if f
    puts "YES",out
else
    puts "NO"
end

n = gets.to_i
s = []
n.times do |i|
	s[i] = gets.chomp
end
x = false
for i in 0...5
	if x == false
		if s[i][0...2] == 'OO'
			s[i][0...2] = '++'
			x = true
		elsif s[i][3...5] == 'OO'
			s[i][3...5] = '++'
			x = true
		end

	end
end
if x == false
	puts 'NO'
else
	puts 'YES'
	0.upto(4) do |i|
		puts s[i]
	end
end

s = ''
(1..gets.chomp.to_i).each do
  s << gets.chomp << '#'
end
begin
  s[/OO/] = '++'
  s.split('#').each { |q| puts q }
rescue
  puts 'NO'
end

d = gets.to_i.times.map{|x| gets.strip.split("|")}
ans = false
list = []
d.each do |x|
    if ans == false && x.include?("OO")
        if x[0] == "00"
            list.push(['++', x[1]])
        else
            list.push([x[0],'++'])
        end
        ans = true
    else
        list.push(x)
    end
end
puts (ans)? 'YES' : 'NO'
list.each{|x| puts x.join('|')}

d = gets.to_i.times.map{|x| gets.strip.split("|")}
ans = false
list = []
d.each do |x|
    if ans == false && x.include?("OO")
        if x[0] == "OO"
            list.push(['++', x[1]])
        else
            list.push([x[0],'++'])
        end
        ans = true
    else
        list.push(x)
    end
end
puts (ans)? 'YES' : 'NO'
list.each{|x| puts x.join('|')}

n = gets.to_i
a = n.times.map{gets.chomp.chars}
ans = false
a.each do |x|
  if x[0] == 'O' && x[1] == 'O'
    x[0], x[1] = '-', '-'
    ans = true
    break
  elsif x[3] == 'O' && x[4] == 'O'
    x[3], x[4] = '-', '-'
    ans = true
    break
  end
end
puts ans ? 'YES' : 'NO'
puts a.map{|x| x.join('')} if ans

n = gets.to_i
a = n.times.map{gets.chomp.chars}
ans = false
a.each do |x|
  if x[0] == 'O' && x[1] == 'O'
    x[0], x[1] = '-', '-'
    ans = true
    break
  elsif x[3] == 'O' && x[4] == 'O'
    x[3], x[4] = '+', '+'
    ans = true
    break
  end
end
puts ans ? 'YES' : 'NO'
puts a.map{|x| x.join('')} if ans


You are given n points on a line with their coordinates xi. Find the point x so the sum of distances to the given points is minimal.

The first line contains integer n (1 ≤ n ≤ 3·105) — the number of points on the line. The second line contains n integers xi ( - 109 ≤ xi ≤ 109) — the coordinates of the given n points.

Print the only integer x — the position of the optimal point on the line. If there are several optimal points print the position of the leftmost one. It is guaranteed that the answer is always the integer.

n=gets.to_i
a=gets.split.map(&:to_i).sort
p a[~-n/2]

n = gets.to_i
arr = gets.split.map(&:to_i)
arr.sort!

puts arr.size.even? ? arr[n/2 - 1] : arr[n/2]

n = gets.to_i
arr = gets.split.map(&:to_i)

puts arr.size.even? ? arr[n/2 - 1] : arr[n/2]


A sequence of $$$n$$$ integers is called a permutation if it contains all integers from $$$1$$$ to $$$n$$$ exactly once.Given two integers $$$n$$$ and $$$k$$$, construct a permutation $$$a$$$ of numbers from $$$1$$$ to $$$n$$$ which has exactly $$$k$$$ peaks. An index $$$i$$$ of an array $$$a$$$ of size $$$n$$$ is said to be a peak if $$$1 < i < n$$$ and $$$a_i \gt a_{i-1}$$$ and $$$a_i \gt a_{i+1}$$$. If such permutation is not possible, then print $$$-1$$$.

The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each containing two space-separated integers $$$n$$$ ($$$1 \leq n \leq 100$$$) and $$$k$$$ ($$$0 \leq k \leq n$$$) — the length of an array and the required number of peaks.

Output $$$t$$$ lines. For each test case, if there is no permutation with given length and number of peaks, then print $$$-1$$$. Otherwise print a line containing $$$n$$$ space-separated integers which forms a permutation of numbers from $$$1$$$ to $$$n$$$ and contains exactly $$$k$$$ peaks.  If there are multiple answers, print any.

NoteIn the second test case of the example, we have array $$$a = [2,4,1,5,3]$$$. Here, indices $$$i=2$$$ and $$$i=4$$$ are the peaks of the array. This is because $$$(a_{2} \gt a_{1} $$$, $$$a_{2} \gt a_{3})$$$ and $$$(a_{4} \gt a_{3}$$$, $$$a_{4} \gt a_{5})$$$.

def array_peaks(n, k)
  # n is size of output array
  # k is number of peaks required

  # if even max possible peaks is (n - 2) / 2
  # if odd max possible peaks is (n - 1) / 2
  if (n % 2) == 0
    if k > (n-2)/2
      puts -1
      return
    end
  else
    if k > (n-1)/2
      puts -1
      return
    end
  end

  a = (1..n).to_a
  o = []
  peaks = 0
  asc = true

  while(a.size > 0) do
    if peaks < k
      if asc
        o << a.shift
        asc = false
      else
        o << a.pop
        asc = true
        peaks += 1
      end
    else
      o << a.pop
    end
  end

  puts o.join(" ")
end

num_input = gets.chomp
num_input.to_i.times do
  args = gets.chomp.split(' ').map(&:to_i)
  array_peaks(*args)
end

test_cases = gets.chomp.to_i
for i in 1..test_cases do
    line = gets.chomp.split(" ")
    n = line[0].to_i
    k = line[1].to_i
    if k <= (n - 1) / 2
        a = Array.new(n, -1)
        for j in 1..k do
            a[2 * j - 1] = n + 1 - j
        end
        x = k + 1
        for j in 0..n - 1 do
            if a[j] == -1
                a[j] = n + 1 - x
                x += 1
            end
        end
        for j in 0..n - 1 do
            print "#{a[j]} "
        end
        puts ""
    else
        puts -1
    end
end

t = gets.to_s.to_i

1.upto(t) do |casenum|
  n, k = gets.to_s.split.map {|x| x.to_i }
  a = (1 .. n).to_a
  swap_pos = a.size - 1
  while k > 0 && swap_pos > 1
    a[swap_pos], a[swap_pos - 1] = a[swap_pos - 1], a[swap_pos]
    k -= 1
    swap_pos -= 2
  end

  if k > 0
    printf("-1\n")
  else
    printf("%s\n", a.join(" "))
  end
end


Vasya adores sport programming. He can't write programs but he loves to watch the contests' progress. Vasya even has a favorite coder and Vasya pays special attention to him.One day Vasya decided to collect the results of all contests where his favorite coder participated and track the progress of his coolness. For each contest where this coder participated, he wrote out a single non-negative number — the number of points his favorite coder earned in the contest. Vasya wrote out the points for the contest in the order, in which the contests run (naturally, no two contests ran simultaneously).Vasya considers a coder's performance in a contest amazing in two situations: he can break either his best or his worst performance record. First, it is amazing if during the contest the coder earns strictly more points that he earned on each past contest. Second, it is amazing if during the contest the coder earns strictly less points that he earned on each past contest. A coder's first contest isn't considered amazing. Now he wants to count the number of amazing performances the coder had throughout his whole history of participating in contests. But the list of earned points turned out long and Vasya can't code... That's why he asks you to help him.

The first line contains the single integer n (1 ≤ n ≤ 1000) — the number of contests where the coder participated. The next line contains n space-separated non-negative integer numbers — they are the points which the coder has earned. The points are given in the chronological order. All points do not exceed 10000.

Print the single number — the number of amazing performances the coder has had during his whole history of participating in the contests.

NoteIn the first sample the performances number 2 and 3 are amazing.In the second sample the performances number 2, 4, 9 and 10 are amazing.

n = gets.to_i
ar = gets.split(' ',n).map(&:to_i)
min, max, record = ar[0], ar[0], 0
ar[1..-1].each do |x|
  record += 1 if x<min
  record += 1 if x>max
  min, max = [min, max, x].minmax
end
puts record

n = gets.to_i
a = gets.split.map &:to_i
ans = -2
mx = -1e50
mi = -mx
a.each do |x|
  if x > mx
    mx = x
    ans += 1
  end
  if x < mi
    mi = x
    ans += 1
  end
end
p ans

n = gets.to_i

res = gets.split.map{|i| i.to_i}

max = res[0]
min = res[0]
result = 0
i = 1
while i < res.size do
    if res[i] > max then
        max = res[i]
        result += 1
    else
        if res[i] < min then
            min = res[i]
            result += 1
        end
    end
    i += 1
end

puts result

gets
a = []
gets.split.map(&:to_i).each do |b|
    a << b if a.empty? || b < a.min || b > a.max
end
p a.size - 1

n=gets.to_i
a=gets.split.map(&:to_i)
arr=[a[0]]
ans=0
1.upto(a.length-1){|i|
        ans+=1 if arr.min>a[i]||arr.max<a[i]
        arr.push(a[i])
}
p ans

n = gets.to_i
a = gets.split(" ").map(&:to_i)
min = a[0]
max = a[0]
cnt = 0
for i in 1...a.size
  if min > a[i]
    cnt += 1
    min = a[i]
  end
  if max < a[i]
    cnt += 1
    max = a[i]
  end
end
puts cnt

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

past = []
past << points[0]
c = 0
(1..n-1).each do |i|
  if points[i] > past.max
    c += 1
  elsif points[i] < past.min
    c += 1
  end
  past << points[i]
end

puts c

def count(a)
  tot_amazing = 0
  # The first content is the max and the min number of points
  min = a.shift
  max = min
  a.each do |p|
    if p < min
      min = p
      tot_amazing += 1
    elsif p > max
      max = p
      tot_amazing += 1
    end
  end
  tot_amazing
end

n = gets.to_i
a = gets.split(/\s/).map(&:to_i)

puts count(a)

n = STDIN.readline.to_i
scores = STDIN.readline.split.map {|s| s.to_i }
min, max = scores[0], scores[0]
count = 0
scores[1..-1].each do |score|
  if score < min
    count += 1
    min = score
  end
  if score > max
    count += 1
    max = score
  end
end
puts count

def i_arr(arr)
	for i in 0...arr.length
		arr[i] = arr[i].to_i
	end
	return arr
end

n = gets.to_i
arr = i_arr(gets.chomp.split)
min = arr[0]
max = arr[0]
t = 0

for i in 1...n
	a = arr[i]
	if a > max
		max = a
		t += 1
	elsif a < min
		min = a
		t += 1
	end
end

puts t

n = STDIN.gets.chomp.to_i
scores = STDIN.gets.chomp.split(/ /).take(n).map(&:to_i)

amazings = 0
min = 100000
max = -1
scores.each_with_index do |score, i|
  if score < min
    min = score
    amazings += 1 unless i == 0
  end
  if score > max
    max = score
    amazings += 1 unless i == 0
  end
end
p amazings

n=gets.chomp.to_i
a=gets.chomp.split(' ').map(&:to_i)
b=[a[0]]
i=1
count=0
while i<a.length
	if a[i]<b.min || a[i]>b.max
		#puts "YAY!"
		count+=1
	end
	b.push(a[i])
	i+=1
end
puts count

n = gets.to_i
a = gets.strip.split.map(&:to_i)
max = a[0]
min = a[0]
k = 0
(1...n).each do |i|
  if a[i] > max
    k+=1
    max = a[i]
  end
  if a[i] < min
    k+=1
    min = a[i]
  end
end
puts k

l = STDIN.read.split("\n")
n  = l[0].to_i
marks = l[1].split(" ").map(&:to_i)
n_i = 0
min = max = marks[0]

for i in 1..(n-1)
  if (min > marks[i])
  	n_i += 1
  	min = marks[i]
  elsif (max < marks[i])
  	n_i+= 1
  	max = marks[i]
  end
end

puts n_i

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

max = a[0]
min = a[0]
k = 0

a.each do |x|
  if x > max
    k+=1
    max = x
  end

  if x < min
    k+=1
    min = x
  end
end

puts k

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

  counter = 0
  min,max = a[0],a[0]
  for i in 1..n-1
    if a[i]>max
      max = a[i]
      counter+=1
    elsif a[i] < min
      min = a[i]
      counter+=1
    end
  end
  puts counter

def Floodico(n,a)

  counter = 0
  min,max = a[0],a[0]
  for i in 1..n-1
    if a[i]>max
      max = a[i]
      counter+=1
    elsif a[i] < min
      min = a[i]
      counter+=1
    end
  end
  puts counter

end
Floodico(gets.to_i,gets.split.map(&:to_i))

n = $stdin.readline.to_i
f = $stdin.readline.split.map(&:to_i)

max_count = 0
min_count = 0
max = f.first
min = f.first

for i in 1...f.count
    elem = f[i]
    if elem > max
        max_count+=1
        max = elem
    end
    if elem < min
        min_count+=1
        min = elem
    end
end

puts min_count + max_count

#!/usr/bin/ruby
def read_int; gets.chomp.to_i; end
def read_ints; gets.chomp.split.map(&:to_i); end

n = read_int
arr = read_ints

first = arr.shift
min = first
max = first
count = 0
arr.each do |i|
  if i < min
    min = i
    count += 1
  elsif i > max
    max = i
    count += 1
  end
end

puts count

gets
arr = gets.split.map &:to_i
max, min = arr.first, arr.first
k = 0
arr.shift
arr.each do |p|
  if p > max
    max = p
    k += 1
  elsif p < min
    min = p
    k += 1
  end
end
puts k

n = gets.chomp.to_i
arr = gets.split.map(&:to_i)
big = arr[0]
small = arr[0]
arr.shift
res = 0
arr.each do |num|
  if num > big
    big = num
    res+=1
  elsif num < small
    small = num
    res+=1
  end
end
puts res

a=gets.chomp.to_i
b=gets.chomp.split(" ")
if a==1
puts "0"
else
max=b[0].to_i
min=b[0].to_i
amz=0
1.upto(a-1) do |i|
if b[i].to_i>max
max=b[i].to_i
amz+=1
elsif b[i].to_i<min
min=b[i].to_i
amz+=1
end
end
puts "#{amz}"
end

n = gets.to_i
as = gets.split.map(&:to_i)

lowest = as[0]
highest = as[0]

ans = 0
(1..n-1).each do |i|
  if as[i] < lowest
    ans += 1
    lowest = as[i]
  elsif as[i] > highest
    ans += 1
    highest = as[i]
  end
end

puts ans

n = gets.strip.to_i
scores = gets.strip.split.map(&:to_i)
min_score = max_score = scores[0]
ans = 0
(1...n).each do |i|
    if scores[i] < min_score
        min_score = scores[i]
        ans += 1
    end
    if scores[i] > max_score
        max_score = scores[i]
        ans += 1
    end
end
puts ans

gets
x=gets.split.map{|e| e.to_i}
ans=0
min1=x[0]
max1=x[0]
x.shift
while !x.empty?
	t=x[0]
	if min1>t then
		min1=t
		ans=ans+1
	end
	if max1<t then
		max1=t
		ans=ans+1
	end
	x.shift
end
puts ans

a=gets.chomp.to_i
b=gets.chomp.split(' ')
b.length.times {|x| b[x]=b[x].to_i}
c=0;min=0;max=0
b.length.times { |x|
  c+=1 if x!=0&&(b[x]>max||b[x]<min)
  min=b[x] if b[x]<min
  max=b[x] if b[x]>max
  if x==0
    min=b[x];max=b[x]
  end
}
puts c

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

m = a[0]
pr = a[0]
c = 0

a.each do |e|
  if e > pr
    c += 1
    pr = e
  elsif e < m
    c += 1
    m = e
  end
end

p c

n = gets.to_i
a = gets.split.map(&:to_i)
m,l,c = a[0],a[0],0
for i in a
	if i > m
		c += 1
		m = i
	elsif i < l
		c += 1
		l = i
	end
end
puts c

ans=0
n=gets.to_i
a=gets.split.collect {|i|i.to_i}
min=max=a[0]
1.upto(n-1){|i|
	if (a[i] > max) then
		max=a[i]
		ans+=1
	elsif (a[i] < min) then
		min=a[i]
		ans+=1
	end}
p ans

n = gets.to_i
q = gets.chomp.split.map { |x| x.to_i }

min = q[0]
max = q[0]
wow = 0

for i in 1...n
  if q[i] > max
    max = q[i]
    wow +=1
  elsif q[i] < min
    min = q[i]
    wow +=1
  end
end
p wow

n = gets.to_i

a = gets.split.map(&:to_i)
b=[a[0]]
ans = a[1...a.size].count do |x|
  ret = (x > b.max or x < b.min)
  b << x
  ret
end

puts ans

# http://codeforces.com/problemset/problem/155/A

count = gets.chomp.to_i
scores = gets.chomp.split.map { |e| e.to_i }

tmp_scores = []
first = true

amazing = 0

scores.each do |score|
  if first
    first = false
    tmp_scores << score
  else
    if score > tmp_scores.max or score < tmp_scores.min
      amazing += 1
      tmp_scores << score
    end
  end
end

puts "#{amazing}"

n = gets.to_i
a = gets.split(' ').map(&:to_i)
max, min = a[0], a[0]
k = 0
for i in 1...n
  if a[i] > max
    k += 1
    max = a[i]
  end
  if a[i] < min
    k += 1
    min = a[i]
  end
end
puts k

number = gets.chomp.to_i
n = gets.chomp
contests = n.split

maxm = contests[0].to_i
minm = contests[0].to_i
count = 0
for i in (1..number-1) # i = subject
	x = i
	while x != 0 # x = compare to
		if contests[i].to_i > maxm
			maxm = contests[i].to_i
			count += 1
		elsif contests[i].to_i < minm
			minm = contests[i].to_i
			count += 1
		end
		x -= 1
	end
end

print count

contests = gets.to_i
points = gets.chomp.split(' ').map { |e| e.to_i }
amazing, max, min = 0, points[0], points[0]
(0...contests).each do |i|
  if points[i] > max
    amazing += 1
    max = points[i]
  end
  if points[i] < min
    amazing += 1
    min = points[i]
  end
end
puts amazing

gets
l = gets.split.map(&:to_i)
c = 0
l.each_with_index { |x, i| c += 1 if i != 0 and (l[0...i].all? { |y| y > x } or l[0...i].all? { |y| y < x }) }
puts c

n = gets.to_i
m = gets.split.collect &:to_i

min_rec = m[0]
max_rec = m[0]
asr = 0
i = 0

while i < n do
  if (min_rec > m[i])
    begin
      min_rec = m[i]
      asr += 1
    end
  end
  if (max_rec < m[i])
    begin
      max_rec = m[i]
      asr += 1
    end
  end
  i += 1
end
puts asr

n = gets.to_i
scs = gets.split.map {|s| s.to_i}
min = scs[0]
max = scs[0]
count = 0
for s in scs
  if(s < min)
  	min = s
  	count += 1
  elsif (s > max)
  	max = s
  	count += 1
  end
end
puts count

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

x = a.shift
result = a.inject({min: x, max: x, result: 0}) do |hash, i|
  r = hash[:result] + ((i<hash[:min] || i>hash[:max]) ? 1 : 0)
  { min: [hash[:min], i].min, max: [hash[:max], i].max, result: r }
end

puts result[:result]

gets

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

min = max = scores[0]

amazing = 0

scores.each do |score|
  if score < min
    min = score
    amazing += 1
  elsif score > max
    max = score
    amazing += 1
  end
end

puts amazing

gets
while 1 do
    gets and $a = $_.chomp.split(" ")
    $_ and $max = $min = $a.shift.to_i
    $_ and $sum = 0
    $a.empty? and break
    $p = $a.shift.to_i
    $p > $max and $max = $p and $sum+=1
    $p < $min and $min = $p and $sum+=1
end
puts $sum

n=gets.to_i
a=0
b=gets.split.map(&:to_i)
min=max=b.first
b.each{|c|
	if c>max
		a+=1
		max=c
	end
	if c<min
		a+=1
		min=c
	end
}
p a

gets; input = gets.strip.split.map &:to_i
min, max, c = input[0], input[0], 0

input.each do |i|
  (min = i; c += 1) if min > i
  (max = i; c += 1) if max < i
end

print c

#!ruby

n = gets.to_i

a = gets.split.map{|e|e.to_i}

max = min = a.shift

count = 0

a.each do |e|
  if e < min
    count += 1
    min = e
  elsif e > max
    count += 1
    max = e
  end
end

puts count

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

past = []
past << points[0]
c = 0
(1..n-1).each do |i|
  if points[i] > past.max
    c += 1
  elsif points[i] < past.min
    c += 1
  end
end

puts c

def Floodico(n,a)

  counter = 0
  min,max = a[0],a[0]
  for i in 1..n-1
    if a[i]>max
      max = a[i]
      counter+=1
    elsif a[i] < min
      min = a[i]
      counter+=1
    end
  end
  return counter

end
Floodico(gets.to_i,gets.split.map(&:to_i))

gets
x=gets.split.map{|e| e.to_i}
ans=0
s=x[0]
x.shift
while !x.empty?
	ans=ans+1 if x[0]>s
	s=x[0]
	x.shift
end
puts ans

a=gets.chomp.to_i
b=gets.chomp.split(' ')
b.length.times {|x| b[x]=b[x].to_i}
c=0;min=0;max=0
b.length.times { |x|
  c+=1 if x!=0&&(b[x]>max||b[x]<min)
  min=b[x];max=b[x] if x==0
}
puts c

n = gets.to_i
m = gets.split.collect &:to_i

min_rec = m[0]
max_rec = m[0]
asr = 0
i = 0
if(n == 1)
  puts 1
else
  while i < n do
    if (min_rec > m[i])
      begin
        min_rec = m[i]
        asr += 1
      end
    end
    if (max_rec < m[i])
      begin
        max_rec = m[i]
        asr += 1
      end
    end
    i += 1
  end
  puts asr
end


Now you can take online courses in the Berland State University! Polycarp needs to pass k main online courses of his specialty to get a diploma. In total n courses are availiable for the passage.The situation is complicated by the dependence of online courses, for each course there is a list of those that must be passed before starting this online course (the list can be empty, it means that there is no limitation).Help Polycarp to pass the least number of courses in total to get the specialty (it means to pass all main and necessary courses). Write a program which prints the order of courses. Polycarp passes courses consistently, he starts the next course when he finishes the previous one. Each course can't be passed more than once.

The first line contains n and k (1 ≤ k ≤ n ≤ 105) — the number of online-courses and the number of main courses of Polycarp's specialty.  The second line contains k distinct integers from 1 to n — numbers of main online-courses of Polycarp's specialty.  Then n lines follow, each of them describes the next course: the i-th of them corresponds to the course i. Each line starts from the integer ti (0 ≤ ti ≤ n - 1) — the number of courses on which the i-th depends. Then there follows the sequence of ti distinct integers from 1 to n — numbers of courses in random order, on which the i-th depends. It is guaranteed that no course can depend on itself.  It is guaranteed that the sum of all values ti doesn't exceed 105.

Print -1, if there is no the way to get a specialty.  Otherwise, in the first line print the integer m — the minimum number of online-courses which it is necessary to pass to get a specialty. In the second line print m distinct integers — numbers of courses which it is necessary to pass in the chronological order of their passage. If there are several answers it is allowed to print any of them.

NoteIn the first test firstly you can take courses number 1 and 2, after that you can take the course number 4, then you can take the course number 5, which is the main. After that you have to take only the course number 3, which is the last not passed main course.

require 'set'
dec = lambda{|x|x-1}
inc = lambda{|x|x+1}

n, k			 = gets.split.map(&:to_i)
main_courses 	 = gets.split.map(&:to_i).map(&dec) # decrement course id's, so 1=>0
deps 			 = []  #dependencies

n.times do
	cs = gets.split.map(&:to_i)
	deps << cs[1..-1].map(&dec)
end

class Set
	def pop
		c = first
		delete c
		return c
	end
end

passed_set = Set.new	# for lookup
passed_arr = []			# for order
passing = Set.new

queue = main_courses.dup
success = nil

#passed? <elem> - is in PASSED
define_method (:passed?) {|course| passed_set.include?(course)}
#passed! <elem> - add to PASSED, remove from PASSING
define_method (:passed!) do |course|
	passed_set << course
	passed_arr << course
	passing.delete course
end
#passing? <elem> - is in PASSING
define_method (:passing?) {|course| passing.include?(course)}
#passing! <elem> - add to PASSING
define_method (:passing!) {|course| passing << course}
loop do
	if queue.length==0
		success = true
		break
	end
	if queue[-1] == :open
		queue.pop  #:open
		passed! queue.pop
	else
		c_id = queue[-1]
		if passing? c_id
			success = false
			break
		end
		if passed? c_id
			queue.pop
			next
		end
		passing! c_id
		queue << :open
		deps[c_id].each do |dep_id|
			queue << dep_id
		end
	end
end

if success
	puts passed_arr.length
	puts passed_arr.map(&inc).join(' ')
else
	puts -1
end

=begin
while have_to_pass.length>0
	course = have_to_pass.pop
	# First, we need to pass all dependency courses.
	if passing_now.include? course # This means there is an unsolvable dependency loop, like in example#3
		puts -1
		exit
	end
	passing_now.add course
	deps[course].each do |c|
		have_to_pass
	end
end
=end

=begin
courses = Set.new # demanded courses, for searching
courses_rev = []  # demanded courses, preserving order

courses_to_demand = main_courses.dup

while courses_to_demand.length > 0
	c_id = courses_to_demand.pop
	next if courses.include?(c_id)
	courses << c_id
	courses_rev << c_id
	deps[c_id].each do |dep_id|
		courses_to_demand.push dep_id if !courses.include?(dep_id)
	end
end
=end


Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.Initially, the text cursor is at position p. Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?

The first line contains two space-separated integers n (1 ≤ n ≤ 105) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor. The next line contains n lowercase characters of Nam's string.

Print the minimum number of presses needed to change string into a palindrome.

NoteA string is a palindrome if it reads the same forward or reversed.In the sample test, initial Nam's string is:  (cursor position is shown bold).In optimal solution, Nam may do 6 following steps:The result, , is now a palindrome.

# Author : Fuad Ashraful Mehmet
#Date: 18th April

n,pos = gets.split(" ").map(&:to_i)
arr = gets.chomp
mid = (n+1) / 2
unless pos <= mid
	pos = n - pos + 1
	arr.reverse!
end
c = 0
last_ind = pos - 1
((pos - 1)...mid).each do |i|
	if arr[i] != arr[n - i - 1]
		a = arr[i].ord
		b = arr[n - i - 1].ord
		val = []
		if a < b
			val << b - a
			val << a - b + 26
		else
			val << a - b
			val << b - a + 26
		end
		c += val.min
		last_ind = i
	end
end

first_ind = nil
(0...(pos-1)).each do |i|
	if arr[i] != arr[n - i - 1]
		a = arr[i].ord
		b = arr[n - i - 1].ord
		val = []
		if a < b
			val << b - a
			val << a - b + 26
		else
			val << a - b
			val << b - a + 26
		end
		c += val.min
		first_ind = i unless first_ind
	end
end

unless first_ind
	c += last_ind - (pos - 1)
else
	first_dist = pos - 1 - first_ind
	last_dist = last_ind - (pos - 1)
	c += first_dist + last_dist
	c += [first_dist,last_dist].min
end

puts c

cost = {}
('a'..'z').each {|x| cost[x] = {} }
('a'..'z').each do |x|
	diff = -1
	(x..'z').each do |y|
		diff += 1
		cost[x][y] = cost[y][x] = [diff, 26-diff].min
	end
end

n, p = STDIN.readline.split.map {|s| s.to_i }
s = ' '+STDIN.readline.strip
center = (n+1)/2
if p > center
	p = n-p+1
end
q = n-p+1

left_correction, left_span = 0, 0
(p-1).downto(1) do |i|
	a, b = s[i], s[n-i+1]
	if a != b
		left_correction += cost[a][b]
		left_span = p-i
	end
end
right_correction, right_span = 0, 0
(p+1).upto(center) do |i|
	a, b = s[i], s[n-i+1]
	if a != b
		right_correction += cost[a][b]
		right_span = i-p
	end
end

total = cost[s[p]][s[q]]
#puts 'total so far = %d' % total
#puts 'left_correction = %d, left_span = %d' % [left_correction, left_span]
#puts 'right_correction = %d, right_span = %d' % [right_correction, right_span]
total += left_correction + right_correction
total += left_span + right_span
total += [left_span, right_span].min
puts total

# Here your code !

N, P_ = gets.split.map(&:to_i)
P__ = P_-1
str = gets.chomp
if P__>= N/2
    str.reverse!
    P = N - (P__+1)
else
    P = P__
end

def orddiff(c1, c2)
    if(c1.ord > c2.ord)
        c1, c2 = c2, c1
    end
    d = c2.ord - c1.ord
    if d>13
        d = 26 - d
    end
    return d
end

count = 0
rightmax = 0
(P...N/2).each{|p|
    if str[p] != str[N-p-1] then
        rightmax = p - P
        count += orddiff(str[p], str[N-p-1])
    end
}
leftmax = 0
(P-1).downto(0).each{|p|
    if str[p] != str[N-p-1] then
        leftmax = P-p
        count += orddiff(str[p], str[N-p-1])
    end
}
# puts "P=#{P} leftmax=#{leftmax}, rightmax=#{rightmax}, count=#{count}"

if leftmax == 0
    count += rightmax
elsif rightmax == 0
    count += leftmax
elsif leftmax < rightmax
    count += leftmax*2 + rightmax
else
    count += rightmax*2 + leftmax
end
puts count

n,pos = gets.split(" ").map(&:to_i)
arr = gets.chomp
mid = (n+1) / 2
unless pos <= mid
	pos = n - pos + 1
	arr.reverse!
end
c = 0
last_ind = pos - 1
((pos - 1)...mid).each do |i|
	if arr[i] != arr[n - i - 1]
		a = arr[i].ord
		b = arr[n - i - 1].ord
		val = []
		if a < b
			val << b - a
			val << a - b + 26
		else
			val << a - b
			val << b - a + 26
		end
		c += val.min
		last_ind = i
	end
end

first_ind = nil
(0...(pos-1)).each do |i|
	if arr[i] != arr[n - i - 1]
		a = arr[i].ord
		b = arr[n - i - 1].ord
		val = []
		if a < b
			val << b - a
			val << a - b + 26
		else
			val << a - b
			val << b - a + 26
		end
		c += val.min
		first_ind = i unless first_ind
	end
end
# puts "#{first_ind} #{pos-1} #{last_ind} #{c}"
unless first_ind
	c += last_ind - (pos - 1)
else
	first_dist = pos - 1 - first_ind
	last_dist = last_ind - (pos - 1)
	c += first_dist + last_dist
	c += [first_dist,last_dist].min
end

puts c

# Here your code !

N, P_ = gets.split.map(&:to_i)
P = P_-1
str = gets.chomp
if P>= N/2
    str.reverse!
    P = N - (P+1)
end

count = 0
rightmax = 0
(P...N/2).each{|p|
    if str[p] != str[N-p-1] then
        rightmax = p - P
        count +=1
    end
}
leftmax = 0
(P-1).downto(0).each{|p|
    if str[p] != str[N-p-1] then
        leftmax = P-p
        count +=1
    end
}
# puts "P=#{P} leftmax=#{leftmax}, rightmax=#{rightmax}, count=#{count}"

if leftmax < rightmax
    count += leftmax*2 + rightmax
else
    count += rightmax*2 + leftmax
end
puts count

# Here your code !

N, P_ = gets.split.map(&:to_i)
P__ = P_-1
str = gets.chomp
if P__>= N/2
    str.reverse!
    P = N - (P__+1)
else
    P = P__
end

count = 0
rightmax = 0
(P...N/2).each{|p|
    if str[p] != str[N-p-1] then
        rightmax = p - P
        count +=1
    end
}
leftmax = 0
(P-1).downto(0).each{|p|
    if str[p] != str[N-p-1] then
        leftmax = P-p
        count +=1
    end
}
# puts "P=#{P} leftmax=#{leftmax}, rightmax=#{rightmax}, count=#{count}"

if leftmax == 0
    count += rightmax
elsif rightmax == 0
    count += leftmax
elsif leftmax < rightmax
    count += leftmax*2 + rightmax
else
    count += rightmax*2 + leftmax
end
puts count

# Here your code !

N, P_ = gets.split.map(&:to_i)
P__ = P_-1
str = gets.chomp
if P__>= N/2
    str.reverse!
    P = N - (P__+1)
else
    P = P__
end

count = 0
rightmax = 0
(P...N/2).each{|p|
    if str[p] != str[N-p-1] then
        rightmax = p - P
        count +=1
    end
}
leftmax = 0
(P-1).downto(0).each{|p|
    if str[p] != str[N-p-1] then
        leftmax = P-p
        count +=1
    end
}
# puts "P=#{P} leftmax=#{leftmax}, rightmax=#{rightmax}, count=#{count}"

if leftmax < rightmax
    count += leftmax*2 + rightmax
else
    count += rightmax*2 + leftmax
end
puts count

n,pos = gets.split(" ").map(&:to_i)
arr = gets.chomp
mid = (n+1) / 2
unless pos <= mid
	pos = n - pos + 1
	arr.reverse!
end
c = 0
last_ind = pos - 1
((pos - 1)...mid).each do |i|
	if arr[i] != arr[n - i - 1]
		a = arr[i].ord
		b = arr[n - i - 1].ord
		val = []
		if a < b
			val << b - a
			val << a - b + 26
		else
			val << a - b
			val << b - a + 26
		end
		c += val.min
		last_ind = i
	end
end

first_ind = nil
(0...(pos-1)).each do |i|
	if arr[i] != arr[n - i - 1]
		a = arr[i].ord
		b = arr[n - i - 1].ord
		val = []
		if a < b
			val << b - a
			val << a - b + 26
		else
			val << a - b
			val << b - a + 26
		end
		c += val.min
		first_ind = i unless first_ind
	end
end
puts "#{first_ind} #{pos-1} #{last_ind} #{c}"
unless first_ind
	c += last_ind - (pos - 1)
else
	first_dist = pos - 1 - first_ind
	last_dist = last_ind - (pos - 1)
	c += first_dist + last_dist
	c += [first_dist,last_dist].min
end

puts c

n,pos = gets.split(" ").map(&:to_i)
arr = gets.chomp
mid = (n+1) / 2
unless pos <= mid
	pos = n - pos + 1
	arr.reverse!
end
c = 0
last_ind = pos
((pos - 1)...mid).each do |i|
	if arr[i] != arr[n - i - 1]
		a = arr[i].ord
		b = arr[n - i - 1].ord
		val = []
		if a < b
			val << b - a
			val << a - b + 26
		else
			val << a - b
			val << b - a + 26
		end
		c += val.min
		last_ind = i
	end
end

first_ind = nil
(0...(pos-1)).each do |i|
	if arr[i] != arr[n - i - 1]
		a = arr[i].ord
		b = arr[n - i - 1].ord
		val = []
		if a < b
			val << b - a
			val << a - b + 26
		else
			val << a - b
			val << b - a + 26
		end
		c += val.min
		first_ind = i unless first_ind
	end
end
# puts "#{first_ind} #{pos} #{last_ind} #{c}"
unless first_ind
	c += last_ind - pos - 1
else
	first_dist = pos - 1 - first_ind
	last_dist = last_ind - (pos - 1)
	c += first_dist + last_dist
	c += [first_dist,last_dist].min
end

puts c


It is a holiday season, and Koala is decorating his house with cool lights! He owns $$$n$$$ lights, all of which flash periodically.After taking a quick glance at them, Koala realizes that each of his lights can be described with two parameters $$$a_i$$$ and $$$b_i$$$. Light with parameters $$$a_i$$$ and $$$b_i$$$ will toggle (on to off, or off to on) every $$$a_i$$$ seconds starting from the $$$b_i$$$-th second. In other words, it will toggle at the moments $$$b_i$$$, $$$b_i + a_i$$$, $$$b_i + 2 \cdot a_i$$$ and so on.You know for each light whether it's initially on or off and its corresponding parameters $$$a_i$$$ and $$$b_i$$$. Koala is wondering what is the maximum number of lights that will ever be on at the same time. So you need to find that out.    Here is a graphic for the first example.

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$), the number of lights. The next line contains a string $$$s$$$ of $$$n$$$ characters. The $$$i$$$-th character is "1", if the $$$i$$$-th lamp is initially on. Otherwise, $$$i$$$-th character is "0". The $$$i$$$-th of the following $$$n$$$ lines contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i, b_i \le 5$$$)  — the parameters of the $$$i$$$-th light.

Print a single integer — the maximum number of lights that will ever be on at the same time.

NoteFor first example, the lamps' states are shown in the picture above. The largest number of simultaneously on lamps is $$$2$$$ (e.g. at the moment $$$2$$$).In the second example, all lights are initially on. So the answer is $$$4$$$.

n = gets.to_i
light = gets.chomp
param = []
rest = []
n.times do |i|
  param << gets.split.map(&:to_i)
  rest[i] = param[i][1]
end

ans = light.count("1")
200.times do |i|
  n.times do |i|
    rest[i] -= 1
    if rest[i] == 0
      rest[i] = param[i][0]
      light[i] = light[i] == "1" ? "0" : "1"
    end
  end
  cnt = light.count("1")
  ans = cnt if cnt > ans
end
p ans

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

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

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

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

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

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

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

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

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

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

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

require 'prime'

n = inp[0]
m = gets.chomp.split("")
t = []
n.times do
  t << inp
end
m.size.times do |i|
  if(m[i] == "1")
    m[i] = true
  else
    m[i] = false
  end
end
ans = m.count(true)
(0..1000).each do |turn|
  t.size().times do |i|
    t[i][1] -= 1
    if(t[i][1] == 0)
      t[i][1] += t[i][0]
      m[i] = !m[i]
    end
  end
  ans = [ans,m.count(true)].max
end
p ans

n = gets.to_i
lights = gets.chomp.each_char.map(&:to_i)
a = []
b = []
for i in 0...n do
  a[i], b[i] = gets.split.map(&:to_i)
end

ons = 0
lights.each do |x|
  ons += x
end

max_ons = ons
for t in 1..125 do
  for i in 0...n do
    if t >= b[i] && (t - b[i]) % a[i] == 0
      if lights[i] == 0
        lights[i] = 1
        ons += 1
      else
        lights[i] = 0
        ons -= 1
      end
    end
  end
  max_ons = ons if max_ons < ons
end
puts max_ons

n = gets.to_i
lights = gets.chomp.each_char.map(&:to_i)
a = []
b = []
for i in 0...n do
  a[i], b[i] = gets.split.map(&:to_i)
end

ons = 0
lights.each do |x|
  ons += x
end

max_ons = ons
for t in 1..125 do
  for i in 0...n do
    if t >= b[i] && (t - b[i]) % a[i] == 0
      if lights[i] == 0
        lights[i] = 1
        ons += 1
      else
        lights[i] = 0
        ons -= 1
      end
    end
  end
  p ons
  max_ons = ons if max_ons < ons
end
puts max_ons

n = gets.to_i
lights = gets.chomp.each_char.map(&:to_i)
a = []
b = []
for i in 0...n do
  a[i], b[i] = gets.split.map(&:to_i)
end

ons = 0
lights.each do |x|
  ons += x
end

max_ons = ons
for t in 1..120 do
  for i in 0...n do
    if (t - b[i]) % a[i] == 0
      if lights[i] == 0
        lights[i] = 1
        ons += 1
      else
        lights[i] = 0
        ons -= 1
      end
    end
  end
  max_ons = ons if max_ons < ons
end
puts max_ons

n = gets.to_i
lights = gets.chomp.each_char.map(&:to_i)
a = []
b = []
for i in 0...n do
  a[i], b[i] = gets.split.map(&:to_i)
end

ons = 0
lights.each do |x|
  ons += x
end

max_ons = ons
for t in 1..(5 + 3 * 4 * 5) do
  for i in 0...n do
    if (t - b[i]) % a[i] == 0
      if lights[i] == 0
        lights[i] = 1
        ons += 1
      else
        lights[i] = 0
        ons -= 1
      end
    end
  end
  max_ons = ons if max_ons < ons
end
puts max_ons

n = gets.to_i
lights = gets.chomp.each_char.map(&:to_i)
a = []
b = []
for i in 0...n do
  a[i], b[i] = gets.split.map(&:to_i)
end

ons = 0
lights.each do |x|
  ons += x
end

max_ons = ons
for t in 1..120 do
  for i in 0...n do
    if t >= b[i] && (t - b[i]) % a[i] == 0
      if lights[i] == 0
        lights[i] = 1
        ons += 1
      else
        lights[i] = 0
        ons -= 1
      end
    end
  end
  max_ons = ons if max_ons < ons
end
puts max_ons


This is an interactive problem. In the output section below you will see the information about flushing the output.Bear Limak thinks of some hidden number — an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite.Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite.You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no".For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14.When you are done asking queries, print "prime" or "composite" and terminate your program.You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct.You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below).

After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise.

Up to 20 times you can ask a query — print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line):    fflush(stdout) in C++;  System.out.flush() in Java;  stdout.flush() in Python;  flush(output) in Pascal;  See the documentation for other languages.  Hacking. To hack someone, as the input you should print the hidden number — one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input.

NoteThe hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process.The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30.59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries).

#!/usr/bin/ruby
require 'prime'
STDOUT.sync=true
M=100
r=0
Prime.each(M/2){|e|
	p e
	if gets.chomp=='yes'
		r+=1
		if e*e<=M
			p e*e
			r+=1 if gets.chomp=='yes'
		end
	end
}
puts r>1 ? :composite : :prime

require 'prime'

ns = (2..100).to_a
ms = [4, 9, 25, 49]
(2..50).each do |m|
  ms << m if m.prime?
end

ms.each do |m|
  puts m
  STDOUT.flush
  s = gets.chomp
  if s == 'yes'
    ns.delete_if { |n| n % m != 0 }
  else
    ns.delete_if { |n| n % m == 0 }
  end
end
if ns[0].prime?
  puts 'prime'
else
  puts 'composite'
end
STDOUT.flush

def convert ans
	if ans == 'yes'
		true
	elsif ans == 'no'
		false
	end
end

STDOUT.flush
list = []

primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 4, 9, 25, 49]
primes.each do |p|
	puts p
	STDOUT.flush

	isYes = convert gets.chomp
	if isYes
		list << p
	end
end

if list.length > 1
	puts 'composite'
	STDOUT.flush
else
	puts 'prime'
	STDOUT.flush
end

primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
squares = [4, 9, 25, 49]
cnt = 0
primes.each do |prime|
	puts prime
	STDOUT.flush
	cnt += (gets.chomp == "yes") ? 1 : 0
end
squares.each do |square|
	puts square
	STDOUT.flush
	cnt += (gets.chomp == "yes") ? 2 : 0
end

puts cnt >= 2 ? "composite" : "prime"

def convert ans
	if ans == 'yes'
		true
	elsif ans == 'no'
		false
	end
end

primes = [97, 89, 83, 79, 73, 71, 67, 61, 59, 53]

primes.each do |p|
	puts p
	STDOUT.flush
	isYes = convert gets.chomp
	if isYes
		puts 'prime'
		STDOUT.flush
		exit
	end
end

puts 2

STDOUT.flush
isYes = convert gets.chomp

if isYes
	divisor = [4, 3, 5, 7, 11, 13, 17, 19, 23]
	list = []

	divisor.each do |d|
		puts d

		STDOUT.flush
		isYes = convert gets.chomp

		if isYes
			list << d
		end

		if list.length > 0
			puts 'composite'
			STDOUT.flush
			exit
		end
	end
else
	divisor = [3, 5, 7, 9, 11, 13]
	list = []

	divisor.each do |d|
		puts d

		STDOUT.flush
		isYes = convert gets.chomp

		if isYes
			list << d
		end

		if list.length > 1
			puts 'composite'
			STDOUT.flush
			exit
		end
	end
end

puts 'prime'
STDOUT.flush

def convert ans
	if ans == 'yes'
		true
	elsif ans == 'no'
		false
	end
end

STDOUT.flush
primes = [97, 89, 83, 79, 73, 71, 67, 61, 59, 53]

primes.each do |p|
	puts p
	STDOUT.flush
	isYes = convert gets.chomp
	if isYes
		puts 'prime'
		STDOUT.flush
		exit
	end
end

puts 2

STDOUT.flush
isYes = convert gets.chomp

if isYes
	divisor = [4, 3, 5, 7, 11, 13, 17, 19, 23]
	list = []

	divisor.each do |d|
		puts d

		STDOUT.flush
		isYes = convert gets.chomp

		if isYes
			list << d
		end

		if list.length > 0
			puts 'composite'
			STDOUT.flush
			exit
		end
	end
else
	divisor = [3, 5, 7, 9, 11, 13, 17, 25, 49]
	list = []

	divisor.each do |d|
		puts d

		STDOUT.flush
		isYes = convert gets.chomp

		if isYes
			list << d
		end

		if list.length > 1
			puts 'composite'
			STDOUT.flush
			exit
		end
	end
end

puts 'prime'
STDOUT.flush

def convert ans
	if ans == 'yes'
		true
	elsif ans == 'no'
		false
	end
end

STDOUT.flush
primes = [97, 89, 83, 79, 73, 71, 67, 61, 59, 53]

primes.each do |p|
	puts p
	STDOUT.flush
	isYes = convert gets.chomp
	if isYes
		puts 'prime'
		STDOUT.flush
		exit
	end
end

puts 2

STDOUT.flush
isYes = convert gets.chomp

if isYes
	divisor = [4, 3, 5, 7, 11, 13, 17, 19, 23]
	list = []

	divisor.each do |d|
		puts d

		STDOUT.flush
		isYes = convert gets.chomp

		if isYes
			list << d
		end

		if list.length > 0
			puts 'composite'
			STDOUT.flush
			exit
		end
	end
else
	divisor = [3, 5, 7, 9, 11, 13, 25, 49]
	list = []

	divisor.each do |d|
		puts d

		STDOUT.flush
		isYes = convert gets.chomp

		if isYes
			list << d
		end

		if list.length > 1
			puts 'composite'
			STDOUT.flush
			exit
		end
	end
end

puts 'prime'
STDOUT.flush


Toastman came up with a very easy task. He gives it to Appleman, but Appleman doesn't know how to solve it. Can you help him?Given a n × n checkerboard. Each cell of the board has either character 'x', or character 'o'. Is it true that each cell of the board has even number of adjacent cells with 'o'? Two cells of the board are adjacent if they share a side.

The first line contains an integer n (1 ≤ n ≤ 100). Then n lines follow containing the description of the checkerboard. Each of them contains n characters (either 'x' or 'o') without spaces.

Print "YES" or "NO" (without the quotes) depending on the answer to the problem.

STEP_X = [0, 1, 0, -1]
STEP_Y = [1, 0, -1, 0]

n = gets.to_i
b = Array.new n
n.times do |i|
  b[i] = gets
end
valid = true
n.times do |i|
  n.times do |j|
    cnt = 0
    4.times do |k|
      tx = i + STEP_X[k]
      ty = j + STEP_Y[k]
      if 0 <= tx && tx < n then
        if 0 <= ty && ty < n then
          cnt += 1 if b[tx][ty] == 'o'
        end
      end
    end
    valid = false if cnt % 2 == 1
  end
end
if valid then
  puts 'YES'
else
  puts 'NO'
end

n=gets.to_i
board=[]
def solve(board)
	len=board.size
	for i in 0...len
		for j in 0...len
			s=0
			if i-1>=0
				s+=1 if board[i-1][j]=='o'
			end
			if i+1<=len-1
				s+=1 if board[i+1][j]=='o'
			end
			if j-1>=0
				s+=1 if board[i][j-1]=='o'
			end
			if j+1<=len-1
				s+=1 if board[i][j+1]=='o'
			end
			return 'NO' if s%2!=0
		end
	end
	return "YES"
end
n.times do
  board.push(gets.chomp.split('').map(&:to_s))
end
puts solve(board)

require 'pp'

n = STDIN.gets.chomp.to_i
m = []

n.times do |row|
  m.push STDIN.gets.chomp.split(//)
end

n.times do |i|
  n.times do |j|
    count = 0
    count += 1 if i != 0 &&   m[i-1][j] == 'o'
    count += 1 if i != n-1 && m[i+1][j] == 'o'
    count += 1 if j != 0 &&   m[i][j-1] == 'o'
    count += 1 if j != n-1 && m[i][j+1] == 'o'

    if count %2 != 0
      puts "NO"
      exit(0)
    end
  end
end
puts "YES"

n = gets.to_i
a = n.times.map{gets.chomp}
ans = true
n.times do |i|
  n.times do |j|
    c = 0
    c += 1 if i != 0 && a[i - 1][j] == 'o'
    c += 1 if j != 0 && a[i][j - 1] == 'o'
    c += 1 if i != n - 1 && a[i + 1][j] == 'o'
    c += 1 if j != n - 1 && a[i][j + 1] == 'o'
    ans = false if c % 2 != 0
  end
end
puts ans ? "YES" : "NO"

a=gets.chomp.to_i
array=[]
a.times do
array<<gets.chomp.split("")
end
api=0
i=0
loop do
k=0
loop do
temp=0
if (i-1!=-1) &&   array[i-1][k]=="o"
temp+=1
end
if (i+1!=a) && array[i+1][k]=="o"
temp+=1
end
if (k-1!=-1) && array[i][k-1]=="o"
temp+=1
end
if (k+1!=a) && array[i][k+1]=="o"
temp+=1
end
if temp%2!=0
api=1
break
end
k+=1
break if k==a
end
i+=1
break if i==a
break if api==1
end
if api==1
puts "NO"
else
puts "YES"
end

n = gets.chomp.to_i
board = []
n.times do
    line = gets.chomp.split("")
    board << line
end
adjacent = [[0, -1], [0, 1], [-1, 0], [1, 0]]
valid = true
(0...n).each do |row|
    break if !valid
    (0...n).each do |col|
        count = 0
        adjacent.each do |adj|
            i = row + adj[0]
            j = col + adj[1]
            next if i < 0 || j < 0 || i == n || j == n
            count += 1 if board[i][j] == 'o'
        end
        if count.odd?
            valid = false
            break
        end
    end
end
if valid
    puts "YES"
else
    puts "NO"
end

def f(x,y,m,n)
	if x<0 || x>=n || y<0 || y>=n
		return 0
	end
	return 1 if m[y][x]=="o"
	return 0
end

n=gets.to_i
m=[]
n.times{
	s=gets.chomp
	m<<s
}
ans="YES"
n.times{|y|
	n.times{|x|
		c1=f(x+1,y,m,n)+f(x,y+1,m,n)+f(x-1,y,m,n)+f(x,y-1,m,n)
		if c1%2==1 then
			ans="NO"
		end
	}
}
puts ans

n = gets.to_i
arr = []
ans = true

for i in 0...n
    arr[i] = gets.chomp
end

for i in 0...n
    count = 0
    for j in 0...n
        count += 1 if(j + 1 < n && arr[i][j + 1] == 'o')
        count += 1 if(j - 1 >= 0 && arr[i][j - 1] == 'o')
        count += 1 if(i + 1 < n && arr[i + 1][j] == 'o')
        count += 1 if(i - 1 >= 0 && arr[i - 1][j] == 'o')

        ans = false if count.odd?
    end
end

puts (ans ? "YES" : "NO")

n=gets.to_i
arr=[]
for i in 1..n
  arr[i]=gets
end
for i in 1..n
  for j in 0..n-1
    cnt=0
    if i>1&&arr[i-1][j]=='o'
      cnt+=1
    end
    if i<n&&arr[i+1][j]=='o'
      cnt+=1
    end
    if arr[i][j-1]=='o'
      cnt+=1
    end
    if arr[i][j+1]=='o'
      cnt+=1
    end
    if cnt&1==1
      puts "NO"
      exit(0)
    end
  end
end
puts "YES"

#$stdin = File.open "input.txt"

n = gets.to_i
a = gets(p).split "\n"

n.times do |i|
	n.times do |j|
		am = 0

		am += 1 if (i > 0) && (a[i - 1][j] == 'o')
		am += 1 if (i < n - 1) && (a[i + 1][j] == 'o')

		am += 1 if (j > 0) && (a[i][j - 1] == 'o')
		am += 1 if (j < n - 1) && (a[i][j + 1] == 'o')

		if am.odd?
			puts "NO"
			exit 0
		end
	end
end

puts "YES"

class Array
  def get_element(i, j, n)
    if i >= 0 && i < n && j >= 0 && j < n
      self[i][j]
    else
      nil
    end
  end
end

def is_good(i, j, s, n)
  t = 0
  t += 1 if s.get_element(i + 1, j, n) == 'o'
  t += 1 if s.get_element(i - 1, j, n) == 'o'
  t += 1 if s.get_element(i, j + 1, n) == 'o'
  t += 1 if s.get_element(i, j - 1, n) == 'o'
  # p [i, j, t]
  t % 2 == 0
end
def main
  n = gets.to_i
  s = n.times.map{gets}
  n.times do |i|
    n.times do |j|
      # p [i, j]
      return :NO unless is_good(i, j, s, n)
    end
  end
  :YES
end

puts main

t=gets.chomp.to_i
board=[]
def solve(board)
  len=board.size
  for i in 0...len
  	for j in 0...len
  		s=0
  	   if i-1>=0
         s+=1 if board[i-1][j]=='o'
  	   end
  	   if i+1<=len-1
  	   	 s+=1 if board[i+1][j]=='o'
  	   end
  	   if j-1>=0
  	   	 s+=1 if board[i][j-1]=='o'
  	   end
  	   if j+1<=len-1
  	   	s+=1 if board[i][j+1]=='o'
  	   end
  	   return 'NO' if s%2!=0
  	end
  end
  return "YES"
end
for i in 0...t
  board<<gets.chomp.split("").map {|x| x.to_s}

end
puts solve(board)

n = gets.to_i
matrix = []
n.times{
  matrix << gets.chomp.split("")
}
res = true
matrix.each_with_index do |row,i|
  row.each_with_index do |ele,j|
    adj_arr = []
    adj_arr << matrix[i+1][j] if matrix[i +1]
    adj_arr << matrix[i-1][j] if i-1 >= 0 && matrix[i-1]
    adj_arr << matrix[i][j+1]
    adj_arr << matrix[i][j-1]  if j-1 >= 0
    # puts "#{adj_arr}"
    res = false and break unless adj_arr.compact.count("o") % 2 == 0
  end
end

puts res ? "YES" : "NO"

require 'pp'
n = gets.to_i
array = Array.new(n)
n.times do |i|
  array[i] = gets.strip.split //
end

result = true
i = j = 0

SHIFT = [
  [-1, 0],
  [ 0,-1],
  [ 1, 0],
  [ 0, 1]
]

while i < n && result
  j = 0
  while j < n && result
    c = 0
    SHIFT.each do |(k,z)|
      k += i
      z += j
      c += 1 if k >= 0 && z >=0 && k < n && z < n && array[k][z] == 'o'
    end
    result = c%2 == 0
    j += 1
  end
  i += 1
end

puts result ? "YES" : "NO"

n = gets.to_i
a = n.times.map{gets.chomp.chars}
ans = true
n.times do |i|
  n.times do |j|
    c = 0
    c += 1 if i != 0 && a[i - 1][j] == 'o'
    c += 1 if j != 0 && a[i][j - 1] == 'o'
    c += 1 if i != n - 1 && a[i + 1][j] == 'o'
    c += 1 if j != n - 1 && a[i][j + 1] == 'o'
    if c % 2 != 0
      ans = false
    end
  end
end
puts ans ? "YES" : "NO"

n = gets.to_i
a = n.times.map{gets.chomp.chars}
ans = true
n.times do |i|
  break if !ans
  n.times do |j|
    c = 0
    c += 1 if i != 0 && a[i - 1][j] == 'o'
    c += 1 if j != 0 && a[i][j - 1] == 'o'
    c += 1 if i != n - 1 && a[i + 1][j] == 'o'
    c += 1 if j != n - 1 && a[i][j + 1] == 'o'
    if c % 2 != 0
      ans = false
      break
    end
  end
end
puts ans ? "YES" : "NO"

n=gets.to_i
arr=Array.new()
for i in 0...n do
    arr[i]=gets.to_s.chomp
end

flag=1
for i in 0...n do
    for j in 0...n do
        count=0
        if (i>0 && arr[i-1][j]=='o')
            count+=1;
        end
        if (i<n-1 && arr[i+1][j]=='o')
            count+=1;
        end
        if(j>0 && arr[i][j-1]=='o')
            count+=1;
        end
        if(j<n-1 && arr[i][j+1]=='o')
            count+=1;
        end

        if(count%2!=0)
            flag*=0
        end
    end
end

if (flag==1)
    puts "YES"
else
    puts "NO"
end

require 'pp'

n = STDIN.gets.chomp.to_i
m = []

n.times do |row|
  m.push STDIN.gets.chomp.split(//)
end

n.times do |i|
  n.times do |j|
    count = 0
    count += 1 if i != 0 &&   m[i-1][j] == 'o'
    count += 1 if i != n-1 && m[i+1][j] == 'o'
    count += 1 if j != 0 &&   m[i][j-1] == 'o'
    count += 1 if j != n-1 && m[i][j+1] == 'o'

    if count %2 != 0
      p "NO"
      exit(0)
    end
  end
end
p "YES"

n = gets.to_i
arr = []
n.times do
  arr << gets.chomp
end
arr.each do |x|
  if x.scan(/o/).empty?
    puts "NO"
    exit
  end
end
puts "YES"

n=gets.to_i
ok="YES"
n.times{|i|
	s=gets.chomp
	n.times{|j|
		ok="NO" if (i+j)%2==1 && s[j]=="o"
	}
}
puts ok

n=gets.to_i
ok="YES"
n.times{|i|
	s=gets.chomp
	if i==0 || i==n-1 then
		ok="NO" if /.o./.match(s)
	else
		ok="NO" if /^o|o$/.match(s)
	end
}
puts ok

n=gets.to_i
ans="NO"
s=STDIN.read.split

hit=true
n.times{|i|
	hit=false if s[i][i]=="x"
}
ans="YES" if hit

hit=true
n.times{|i|
	hit=false if s[i][n-i-1]=="x"
}
ans="YES" if hit
puts ans

n = gets.to_i
array = Array.new(n)
n.times do |i|
  array[i] = gets.strip.split //
end

result = true
i = j = 0

SHIFT = [
  [-1, 0],
  [ 0,-1],
  [ 1, 0],
  [ 0, 1]
]

while i < n && result
  while j < n && result do
    c = 0
    SHIFT.each do |(k,z)|
      k += i; z += j
      c += 1 if k >= 0 && z >=0 && k < n && z < n && array[k][z] == 'o'
    end
    result = c%2 == 0
    j += 1
  end
  i += 1
end

puts result ? "YES" : "NO"

require 'pp'
n = gets.to_i
array = Array.new(n)
n.times do |i|
  array[i] = gets.strip.split //
end

pp array
result = true
i = j = 0

SHIFT = [
  [-1, 0],
  [ 0,-1],
  [ 1, 0],
  [ 0, 1]
]

while i < n && result
  while j < n && result
    c = 0
    SHIFT.each do |(k,z)|
      k += i; z += j
      c += 1 if k >= 0 && z >=0 && k < n && z < n && array[k][z] == 'o'
    end
    result = c%2 == 0
    j += 1
  end
  i += 1
end

puts result ? "YES" : "NO"

require 'pp'
n = gets.to_i
array = Array.new(n)
n.times do |i|
  array[i] = gets.strip.split //
end

result = true
i = j = 0

SHIFT = [
  [-1, 0],
  [ 0,-1],
  [ 1, 0],
  [ 0, 1]
]

while i < n && result
  while j < n && result
    c = 0
    SHIFT.each do |(k,z)|
      k += i; z += j
      c += 1 if k >= 0 && z >=0 && k < n && z < n && array[k][z] == 'o'
    end
    result = c%2 == 0
    j += 1
  end
  i += 1
end

puts result ? "YES" : "NO"

n = gets.to_i
a = n.times.map{gets.chomp.chars}
ans = true
n.times do |i|
  n.times do |j|
    if a[i][j] == 'x'
      c = 0
      c += 1 if i != 0 && a[i - 1][j] == 'o'
      c += 1 if j != 0 && a[i][j - 1] == 'o'
      c += 1 if i != n - 1 && a[i + 1][j] == 'o'
      c += 1 if j != n - 1 && a[i][j + 1] == 'o'
      if c % 2 != 0
        ans = false
        break
      end
    end
  end
end
puts ans ? "YES" : "NO"

n = gets.to_i
a = n.times.map{gets.chomp.chars}
ans = true
n.times do |i|
  n.times do |j|
    if a[i][j] == 'o'
      c = 0
      c += 1 if i != 0 && a[i - 1][j] == 'x'
      c += 1 if j != 0 && a[i][j - 1] == 'x'
      c += 1 if i != n - 1 && a[i + 1][j] == 'x'
      c += 1 if j != n - 1 && a[i][j + 1] == 'x'
      if c % 2 != 0
        ans = false
        break
      end
    end
  end
end
puts ans ? "YES" : "NO"

(mx = (0...gets.to_i).map { |_| gets.chomp.split('') }).each_with_index { |a, i|
  a.each_with_index { |_, j|
    if [[i - 1, j], [i + 1, j], [i, j - 1], [i, j + 1]].map { |k| mx[k[0]] && mx[k[0]][k[1]] == 'o' ? 1 : 0 }.inject(:+).odd?
      puts 'NO'
      exit
    end
  }
}

puts 'YES'


You are given an array of positive integers $$$a_1, a_2, \ldots, a_n$$$.Make the product of all the numbers in the array (that is, $$$a_1 \cdot a_2 \cdot \ldots \cdot a_n$$$) divisible by $$$2^n$$$.You can perform the following operation as many times as you like:  select an arbitrary index $$$i$$$ ($$$1 \leq i \leq n$$$) and replace the value $$$a_i$$$ with $$$a_i=a_i \cdot i$$$. You cannot apply the operation repeatedly to a single index. In other words, all selected values of $$$i$$$ must be different.Find the smallest number of operations you need to perform to make the product of all the elements in the array divisible by $$$2^n$$$. Note that such a set of operations does not always exist.

The first line of the input contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4$$$) — the number test cases. Then the descriptions of the input data sets follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains exactly $$$n$$$ integers: $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$). It is guaranteed that the sum of $$$n$$$ values over all test cases in a test does not exceed $$$2 \cdot 10^5$$$.

For each test case, print the least number of operations to make the product of all numbers in the array divisible by $$$2^n$$$. If the answer does not exist, print -1.

NoteIn the first test case, the product of all elements is initially $$$2$$$, so no operations needed.In the second test case, the product of elements initially equals $$$6$$$. We can apply the operation for $$$i = 2$$$, and then $$$a_2$$$ becomes $$$2\cdot2=4$$$, and the product of numbers becomes $$$3\cdot4=12$$$, and this product of numbers is divided by $$$2^n=2^2=4$$$. In the fourth test case, even if we apply all possible operations, we still cannot make the product of numbers divisible by $$$2^n$$$  — it will be $$$(13\cdot1)\cdot(17\cdot2)\cdot(1\cdot3)\cdot(1\cdot4)=5304$$$, which does not divide by $$$2^n=2^4=16$$$.In the fifth test case, we can apply operations for $$$i = 2$$$ and $$$i = 4$$$.

def factors_of_two(num)
  res = 0
  until num.odd?
    res += 1
    num /= 2
  end
  res
end

def solve(n, num_array)
  number_of_twos = 0

  num_array.each do |num|
    number_of_twos += factors_of_two(num)
    return 0 if number_of_twos >= n
  end

  index_factors = (1..num_array.length).map { |num| factors_of_two(num) }
  index_factors.sort!

  indexes_modified = 0
  index_factors.reverse_each do |factors|
    number_of_twos += factors
    indexes_modified += 1
    return indexes_modified if number_of_twos >= n
  end

  -1
end

# code for tests:

number_of_tests = gets.chomp.to_i

number_of_tests.times do
  n = gets.chomp.to_i
  num_array = gets.chomp.split.map(&:to_i)

  puts solve(n, num_array)
end

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

def log2(n)
  cnt = 0
  while n % 2 == 0
    n /= 2
    cnt += 1
  end
  cnt
end

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

  rem = max(0, n - a.sum{ |ai| log2(ai) })

  ans = (1..n).map{ |i| log2(i) }.sort.reverse.sum do |i|
    if rem != 0 && rem - i >= 0
      rem -= i
      1
    else
      0
    end
  end

  puts rem == 0 ? ans : -1
end

def factors_of_two(num)
  res = 0
  until num.odd?
    res += 1
    num /= 2
  end
  res
end

def solve(n, num_array)
  number_of_twos = 0

  num_array.each do |num|
    number_of_twos += factors_of_two(num)
    return 0 if number_of_twos >= n
  end

  index_factors = (1..num_array.length).map { |num| factors_of_two(num) }
  index_factors.sort

  indexes_modified = 0
  index_factors.reverse_each do |factors|
    number_of_twos += factors
    indexes_modified += 1
    return indexes_modified if number_of_twos >= n
  end

  -1
end

# code for tests:

number_of_tests = gets.chomp.to_i

number_of_tests.times do
  n = gets.chomp.to_i
  num_array = gets.chomp.split.map(&:to_i)

  puts solve(n, num_array)
end

def factors_of_two(num)
  res = 0
  until num.odd?
    res += 1
    num /= 2
  end
  res
end

def solve(n, num_array)
  number_of_twos = 0

  num_array.each do |num|
    number_of_twos += factors_of_two(num)
    return 0 if number_of_twos >= n
  end

  index_factors = (1..num_array.length).map { |num| factors_of_two(num) }
  index_factors.sort

  indexes_modified = 0
  index_factors.reverse_each do |factors|
    number_of_twos += factors
    indexes_modified += 1
    return indexes_modified if number_of_twos >= n
  end

  -1
end

# # code for tests:

# number_of_tests = gets.chomp.to_i

# number_of_tests.times do
#   n = gets.chomp.to_i
#   num_array = gets.chomp.split.map(&:to_i)

#   puts solve(n, num_array)
# end

p solve(2, [3, 2])

def factors_of_two(num)
  res = 0
  until num.odd?
    res += 1
    num /= 2
  end
  res
end

def solve(n, num_array)
  number_of_twos = 0

  num_array.each do |num|
    number_of_twos += factors_of_two(num)
    return 0 if number_of_twos >= n
  end

  index_factors = (1..num_array.length - 1).map { |num| factors_of_two(num) }
  index_factors.sort

  indexes_modified = 0
  index_factors.reverse_each do |factors|
    number_of_twos += factors
    indexes_modified += 1
    return indexes_modified if number_of_twos >= n
  end

  -1
end

# code for tests:

number_of_tests = gets.chomp.to_i

number_of_tests.times do
  n = gets.chomp.to_i
  num_array = gets.chomp.split.map(&:to_i)

  puts solve(n, num_array)
end


You have a knapsack with the capacity of $$$W$$$. There are also $$$n$$$ items, the $$$i$$$-th one has weight $$$w_i$$$. You want to put some of these items into the knapsack in such a way that their total weight $$$C$$$ is at least half of its size, but (obviously) does not exceed it. Formally, $$$C$$$ should satisfy: $$$\lceil \frac{W}{2}\rceil \le C \le W$$$. Output the list of items you will put into the knapsack or determine that fulfilling the conditions is impossible. If there are several possible lists of items satisfying the conditions, you can output any. Note that you don't have to maximize the sum of weights of items in the knapsack.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains integers $$$n$$$ and $$$W$$$ ($$$1 \le n \le 200\,000$$$, $$$1\le W \le 10^{18}$$$).  The second line of each test case contains $$$n$$$ integers $$$w_1, w_2, \dots, w_n$$$ ($$$1 \le w_i \le 10^9$$$) — weights of the items. The sum of $$$n$$$ over all test cases does not exceed $$$200\,000$$$.

For each test case, if there is no solution, print a single integer $$$-1$$$.  If there exists a solution consisting of $$$m$$$ items, print $$$m$$$ in the first line of the output and $$$m$$$ integers $$$j_1$$$, $$$j_2$$$, ..., $$$j_m$$$ ($$$1 \le j_i \le n$$$, all $$$j_i$$$ are distinct) in the second line of the output  — indices of the items you would like to pack into the knapsack. If there are several possible lists of items satisfying the conditions, you can output any. Note that you don't have to maximize the sum of weights items in the knapsack.

NoteIn the first test case, you can take the item of weight $$$3$$$ and fill the knapsack just right.In the second test case, all the items are larger than the knapsack's capacity. Therefore, the answer is $$$-1$$$.In the third test case, you fill the knapsack exactly in half.

t = gets.chomp.to_i
t.times do
  n, w = gets.chomp.split(' ').map(&:to_i)
  arr = gets.chomp.split(' ').map(&:to_i).zip(1..).sort
  orig_arr = arr
  res = []
  sum = 0
  while sum < (w / 2.0).ceil && arr.any? && sum + arr[0][0] <= w
    res << arr.shift
    sum += res[-1][0]
  end
  if sum >= (w / 2.0).ceil
    puts res.size
    puts res.map{|el| el[1]}.join(' ')
  else
    res = []
    sum = orig_arr.find{|el| el[0] >= (w / 2.0).ceil && el[0] <= w}
    if sum
      puts 1
      puts sum[1]
    else
      puts -1
    end
  end
end

t = gets.chomp.to_i
t.times do
  n, w = gets.chomp.split(' ').map(&:to_i)
  arr = gets.chomp.split(' ').map(&:to_i).zip(1..).sort
  res = []
  sum = 0
  while sum < (w / 2.0).ceil && arr.any? && sum + arr[0][0] <= w
    res << arr.shift
    sum += res[-1][0]
  end
  if sum >= (w / 2.0).ceil
    puts res.map{|el| el[1]}.join(' ')
  else
    puts -1
  end
end

t = gets.chomp.to_i
t.times do
  n, w = gets.chomp.split(' ').map(&:to_i)
  arr = gets.chomp.split(' ').map(&:to_i).zip(1..).sort
  res = []
  sum = 0
  while sum < (w / 2.0).ceil && arr.any? && sum + arr[0][0] <= w
    res << arr.shift
    sum += res[-1][0]
  end
  if sum >= (w / 2.0).ceil
    puts res.size
    puts res.map{|el| el[1]}.join(' ')
  else
    puts -1
  end
end


A permutation p is an ordered group of numbers p1,   p2,   ...,   pn, consisting of n distinct positive integers, each is no more than n. We'll define number n as the length of permutation p1,   p2,   ...,   pn.Simon has a positive integer n and a non-negative integer k, such that 2k ≤ n. Help him find permutation a of length 2n, such that it meets this equation: .

The first line contains two integers n and k (1 ≤ n ≤ 50000, 0 ≤ 2k ≤ n).

Print 2n integers a1, a2, ..., a2n — the required permutation a. It is guaranteed that the solution exists. If there are multiple solutions, you can print any of them.

NoteRecord |x| represents the absolute value of number x. In the first sample |1 - 2| - |1 - 2| = 0.In the second sample |3 - 2| + |1 - 4| - |3 - 2 + 1 - 4| = 1 + 3 - 2 = 2.In the third sample |2 - 7| + |4 - 6| + |1 - 3| + |5 - 8| - |2 - 7 + 4 - 6 + 1 - 3 + 5 - 8| = 12 - 12 = 0.

n, k = gets.chomp.split.map(&:to_i)
ary = (1..2 * n).to_a
i = 0
k.times do
  ary[i], ary[i + 1] = ary[i + 1], ary[i]
  i += 2
end
puts ary.join(' ')

n, k = gets.split.map(&:to_i)
a = Array.new(2*n) { |i| i+1 }

k.times do |i|
  a[2*i] += 1
  a[2*i+1] -= 1
end

puts a.join(' ')

n,k=gets.chomp.split.map(&:to_i)
for i in 1..k
	print "#{2*i} #{2*i-1} "
end
for i in (k+1)..n
	print "#{2*i-1} #{2*i} "
end
puts

n,k = gets.split(" ").map(&:to_i)
arr = (1..(2 * n)).to_a
(1..k).each do |i|
	l,m = 2 * i - 1,(2 * i) - 2
	arr[l],arr[m] =  arr[m],arr[l]
end
puts arr.join(" ")

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

if k == 0 then
    a = (1..n*2).to_a.reverse
else
    a = [1, k+1] | (1..n*2).to_a.reverse
end

a.each do |x| print "#{x} " end and puts
#
# s = ms = 0
#
# a.each_index { |i|
#     if i%2 == 0 then
#         s += a[i] - a[i+1]
#         ms += (a[i]-a[i+1]).abs
#     end
# }
#
# print ms-s.abs, " ", k.*(2)

n,k=gets.split.map(&:to_i)
a=(1..2*n).to_a
puts a.shift,a.pop
puts a.delete(2+k),a.shift if !a.empty?&&k>0
puts a

n,k=gets.split.map(&:to_i)
a=(1..2*n).to_a
puts a.shift,a.pop
if !a.empty? && k>0
  puts 2+k,a.shift
  a.delete 2+k
end
puts a.shift,a.shift until a.empty?

n, k = gets.split.map { |x| 2 * x.to_i }
puts n.downto(1).each_slice(2).map { |x| x[0] <= k ? x.reverse : x } * ' '

line = gets.chomp.split(" ").map {|e| e.to_i}
second = 1
first = nil
nums = [second]
if line[1] != 0
  first = line[1] + 1
  nums = [first, second]
end

for i in 2..(line[0] * 2)
  if i != first
    nums.push i
  end
end

puts nums.join(" ")

n, k = gets.chomp.split.map(&:to_i)
ary = (1..2 * n).to_a
i = 0
k.times do
  ary[i], ary[i + 1] = ary[i + 1], ary[i]
  i += 2
end
p ary.join(" ")

n,k=gets.split.map(&:to_i)
a=(1..2*n).to_a
puts a.shift,a.pop
if !a.empty? && k>0
  puts a.shift,2+k
  a.delete 2+k
end
puts a.pop,a.pop until a.empty?

n,k=gets.split.map(&:to_i)
a=(1..2*n).to_a
puts a.shift,a.pop
if !a.empty? && k>0
  puts 2+k,a.shift
  a.delete 2+k
end
puts a.pop,a.pop until a.empty?


One way to create a task is to learn from life. You can choose some experience in real life, formalize it and then you will get a new task.Let's think about a scene in real life: there are lots of people waiting in front of the elevator, each person wants to go to a certain floor. We can formalize it in the following way. We have n people standing on the first floor, the i-th person wants to go to the fi-th floor. Unfortunately, there is only one elevator and its capacity equal to k (that is at most k people can use it simultaneously). Initially the elevator is located on the first floor. The elevator needs |a - b| seconds to move from the a-th floor to the b-th floor (we don't count the time the people need to get on and off the elevator).What is the minimal number of seconds that is needed to transport all the people to the corresponding floors and then return the elevator to the first floor?

The first line contains two integers n and k (1 ≤ n, k ≤ 2000) — the number of people and the maximal capacity of the elevator. The next line contains n integers: f1, f2, ..., fn (2 ≤ fi ≤ 2000), where fi denotes the target floor of the i-th person.

Output a single integer — the minimal time needed to achieve the goal.

NoteIn first sample, an optimal solution is:   The elevator takes up person #1 and person #2.  It goes to the 2nd floor.  Both people go out of the elevator.  The elevator goes back to the 1st floor.  Then the elevator takes up person #3.  And it goes to the 2nd floor.  It picks up person #2.  Then it goes to the 3rd floor.  Person #2 goes out.  Then it goes to the 4th floor, where person #3 goes out.  The elevator goes back to the 1st floor.

def read_line; gets.chomp.split.map(&:to_i);end
a,b = read_line
s = 0
read_line.sort.reverse.each_slice(b){|p| s+=(p[0]-1)*2}
puts s

a,b=0,gets.split.map(&:to_i)
gets.chomp.split.map(&:to_i).sort.reverse.each_with_index{|x,i| a+=(x-1)*2 if i%b[1]==0}
puts a

n, k = STDIN.readline.split.map {|s| s.to_i }
targets = STDIN.readline.split.map {|s| s.to_i }.sort.reverse
cost = 0
pos = 0
while pos < n
	cost += 2*targets[pos] - 2
	pos += k
end
puts cost

n,k=gets.split.map{|e| e.to_i}
xs=gets.split.map{|e| e.to_i}.sort.reverse
ans=0
while xs.empty? == false
	ans+=(xs.take(k).shift-1)*2
	xs=xs.drop(k)
end
puts ans

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

def solve(f, k)
    sum = 0
    cur = []
    while f.size > 0
        if f.size > k
            cur = f[0...k]
            f = f[k...f.size]
        else
            cur = f
            f = []
        end

        cur << 1
        #p cur

        floor = 1
        cur.each do |newfloor|
            sum += (newfloor - floor).abs
            floor = newfloor
        end
    end
    return sum
end

p [solve(f.dup, k), solve(f.reverse, k)].min

nums = gets.chomp.split
n = Integer(nums[0])
k = Integer(nums[1])
nums2 = gets.chomp.split
f = []
for i in 0...n
    f[i] = Integer(nums2[i])
end

f.sort!
m = n - 1
total = 0
while m >= 0 do
    total += 2*(f[m] - 1)
    m -= k
end
print total

nk=gets.chomp.split(" ").map {|x| x.to_i}
line=gets.chomp.split(" ").map {|x| x.to_i}
line.sort!
last=nk[0]
times=0
while last>0
  times+=(line[last-1]-1)*2
  last-=nk[1]
end
puts times

n, limit = gets.split.map(&:to_i)
goal = gets.split.map(&:to_i).sort
step = 0
position = 1
n.times do
	limit.times do
		step += (goal[-1] - position).abs
		position = goal[-1]
		goal.pop
		break if goal == []
	end
	step += position - 1
	break if goal == []
	position = 1
end
puts step

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

_, k = read_next_line
sum = 0
read_next_line.sort.reverse.each_slice(k) { |p|
  sum += (p.first - 1) * 2
}
puts sum

n, k = gets.split.map(&:to_i)
f_list = gets.split.map(&:to_i)
f_list.sort!.reverse!
sum = 0
((f_list.size - 1) / k + 1).times do |i|
  sum += (f_list[i * k, k].max - 1) * 2
end
puts sum

nk=gets.chomp.split(" ").map {|x| x.to_i}
line=gets.chomp.split(" ").map {|x| x.to_i}
line.sort!
last=nk[0]
times=0
while last>=0
  times+=(line[last-1]-1)*2
  last-=nk[1]
end
puts times


Nazar, a student of the scientific lyceum of the Kingdom of Kremland, is known for his outstanding mathematical abilities. Today a math teacher gave him a very difficult task.Consider two infinite sets of numbers. The first set consists of odd positive numbers ($$$1, 3, 5, 7, \ldots$$$), and the second set consists of even positive numbers ($$$2, 4, 6, 8, \ldots$$$). At the first stage, the teacher writes the first number on the endless blackboard from the first set, in the second stage — the first two numbers from the second set, on the third stage — the next four numbers from the first set, on the fourth — the next eight numbers from the second set and so on. In other words, at each stage, starting from the second, he writes out two times more numbers than at the previous one, and also changes the set from which these numbers are written out to another. The ten first written numbers: $$$1, 2, 4, 3, 5, 7, 9, 6, 8, 10$$$. Let's number the numbers written, starting with one.The task is to find the sum of numbers with numbers from $$$l$$$ to $$$r$$$ for given integers $$$l$$$ and $$$r$$$. The answer may be big, so you need to find the remainder of the division by $$$1000000007$$$ ($$$10^9+7$$$).Nazar thought about this problem for a long time, but didn't come up with a solution. Help him solve this problem.

The first line contains two integers $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq 10^{18}$$$) — the range in which you need to find the sum.

Print a single integer — the answer modulo $$$1000000007$$$ ($$$10^9+7$$$).

NoteIn the first example, the answer is the sum of the first three numbers written out ($$$1 + 2 + 4 = 7$$$).In the second example, the numbers with numbers from $$$5$$$ to $$$14$$$: $$$5, 7, 9, 6, 8, 10, 12, 14, 16, 18$$$. Their sum is $$$105$$$.

require 'scanf'
class T
  attr_accessor :lb
  attr_accessor :le
  attr_accessor :vb
  attr_accessor :ve
  attr_accessor :val
  def initialize(a1,a2,a3,a4,a5)
    @lb,@le,@vb,@ve,@val=a1,a2,a3,a4,a5
  end
end
dt=[]
$shit=1000000007
dt[1]=T.new(1,1,1,1,1)
dt[2]=T.new(2,3,2,4,6)
def size(a1,a2,a3)
  return (((a1+a2)*a3)/2)%$shit
end
for i in 3..100
  dt[i]=T.new(dt[i-1].le+1,2**i-1,dt[i-2].ve+2,dt[i-2].ve+2**i,1)
  dt[i].val=size(dt[i].vb,dt[i].ve,dt[i].le-dt[i].lb+1)
end
a,b=scanf("%d%d")
a-=1
l,r=0,0
for i in 1..100
  if dt[i].le<a
    l+=dt[i].val
  else
    l+=size(dt[i].vb,dt[i].vb+2*(a-dt[i].lb),a-dt[i].lb+1)
    break
  end
end
for i in 1..100
  if dt[i].le<b
    r+=dt[i].val
  else
    r+=size(dt[i].vb,dt[i].vb+2*(b-dt[i].lb),b-dt[i].lb+1)
    break
  end
end
x=r-l
while x<$shit
  x+=$shit
end
#puts l
#puts r
puts x%$shit

def p(i)
  ((1+i)*i)/2
end

def S(i)
  return 0 if i < 1
  return 1 if i == 1
  return 3 if i == 2
  q = Math.log2(i).ceil
  z = 0
  q.times do
    if z % 2 == 0
      z = z*2+1
    else
      z = z*2
    end
  end

  if i & (i-1) == 0
    z+=2
  end

  p(i) + p(i-z)
end

l, r = gets.split(/\s+/).map(&:to_i)
puts (S(r)-S(l-1))%1000000007

if File.exists?("input")
  $stdin = File.open("input")
  $stdout = File.open("output","w")
end

def foo(x)
	return 0 if(x==0)
	xlog = (Math.log(x)/Math.log(2)).to_i
	ans=0
	if(xlog%2==1)
		#our last number is odd
		asd = (xlog-1)/2
		numberofodds = (((4**(asd+1))-1)/3)
		numberofevens = x-numberofodds
		ans += numberofevens**2 + numberofevens
		ans += numberofodds**2
		return ans
	else
		#our last number is even
		asd = (xlog-2)/2
		numberofevens = 2*(((4**(asd+1))-1)/3)
		numberofodds = x-numberofevens
		ans += numberofevens**2 + numberofevens
		ans += numberofodds**2
		return ans
	end
end

lr = gets.chomp.split(" ").map(&:to_i)

l = lr[0]
r = lr[1]
puts (foo(r)-foo(l-1))%1000000007

def p(i)
  ((1+i)*i)/2
end

def S(i)
  return 0 if i < 1
  return 1 if i == 1
  return 3 if i == 2
  return 10 if i == 4
  q = Math.log2(i).ceil
  z = 0
  q.times do
    if z % 2 == 0
      z = z*2+1
    else
      z = z*2
    end
  end
  puts z
  p(i) + p(i-z)
end

l, r = gets.split(/\s+/).map(&:to_i)
puts (S(r)-S(l-1))%1000000007

def p(i)
  ((1+i)*i)/2
end

def S(i)
  return 0 if i < 1
  q = Math.log2(i).ceil
  z = 0
  q.times do
    puts z
    if z % 2 == 0
      z = z*2+1
    else
      z = z*2
    end
  end

  p(i) + p(i-z)
end

l, r= gets.split(/\s+/).map(&:to_i)
puts (S(l)-S(r-1))%1000000007

def p(i)
  ((1+i)*i)/2
end

def S(i)
  return 0 if i < 1
  return 1 if i == 1
  return 3 if i == 2
  q = Math.log2(i).ceil
  z = 0
  q.times do
    if z % 2 == 0
      z = z*2+1
    else
      z = z*2
    end
  end
  puts z
  p(i) + p(i-z)
end

l, r = gets.split(/\s+/).map(&:to_i)
puts (S(r)-S(l-1))%1000000007

def p(i)
  ((1+i)*i)/2
end

def S(i)
  return 0 if i < 1
  return 1 if i == 1
  return 3 if i == 2
  return 10 if i == 4
  q = Math.log2(i).ceil
  z = 0
  q.times do
    if z % 2 == 0
      z = z*2+1
    else
      z = z*2
    end
  end
  p(i) + p(i-z)
end

l, r = gets.split(/\s+/).map(&:to_i)
puts (S(r)-S(l-1))%1000000007

def p(i)
  ((1+i)*i)/2
end

def S(i)
  return 0 if i < 1
  return 1 if i == 1
  return 3 if i == 2
  q = Math.log2(i).ceil
  z = 0
  q.times do
    if z % 2 == 0
      z = z*2+1
    else
      z = z*2
    end
  end
  p(i) + p(i-z)
end

l, r = gets.split(/\s+/).map(&:to_i)
puts (S(r)-S(l-1))%1000000007