One day Vasya decided to have a look at the results of Berland 1910 Football Championship’s finals. Unfortunately he didn't find the overall score of the match; however, he got hold of a profound description of the match's process. On the whole there are n lines in that description each of which described one goal. Every goal was marked with the name of the team that had scored it. Help Vasya, learn the name of the team that won the finals. It is guaranteed that the match did not end in a tie.

The first line contains an integer n (1 ≤ n ≤ 100) — the number of lines in the description. Then follow n lines — for each goal the names of the teams that scored it. The names are non-empty lines consisting of uppercase Latin letters whose lengths do not exceed 10 symbols. It is guaranteed that the match did not end in a tie and the description contains no more than two different teams.

Print the name of the winning team. We remind you that in football the team that scores more goals is considered the winner.

goals = gets.to_i
h = {}
goals.times do |i|
    name = gets.chomp
    if h[name] then
        h[name] += 1
    else
        h[name] = 1
    end
end
mas = h.keys
if mas.size > 1 then
    if h[mas[0]] > h[mas[1]] then
        puts mas[0]
    else
        puts mas[1]
    end
else
    puts mas[0]
end

h=Hash.new(0)
gets.to_i.times{s=gets
h[s]+=1}
puts h.max{|x,y|x[1]<=>y[1]}[0]

n=gets.to_i
c={}
for i in 1..n do
	r=gets
	c[r]=c[r].to_i+1
end
max=0
maxc=""
c.keys.each {|k|
	if c[k]>max
		maxc=k
		max=c[k]
	end
}
puts maxc

count = gets.to_i
r = {}
count.times{|n|
  s = gets
  if r[s] then
    r[s] += 1
  else
    r[s] = 1
  end
}

answer = ''
tmp = 0
r.each {|team,point|
  if tmp <= point then
    answer = team
    tmp = point
  end
}

puts answer

n=gets.to_i
x=gets.chomp
a=[1,0]
y=nil
n-=1
n.times do
	l=gets.chomp
	if x==l
		a[0]+=1
	else
		a[1]+=1
		if !y
			y=l
		end
	end
end
puts a[0]>a[1]? x : y

gets
puts (a = gets(p).split).max_by { |x| a.count x }

n = gets.to_i
a = Array.new(n)

for i in 0..n-1
  a[i] = gets.to_s.chomp
end
if n == 1
    puts a[0]
else
  counter = 0
  for i in 1..n-1
   if a[i] != a[0]
    d = a[i]
    counter +=1
   end
  end

  f = a.length
  if counter > f/2
     puts d
  else
     puts a[0]
  end
 end

total_goals = gets.chomp.to_i
teams = {}
total_goals.times do
    team = gets.chomp
    if !teams.has_key? team
        teams.store team, 1
    else
        teams[team] += 1
    end
end
max_goals = teams.values.max
winner = teams.select { |k, v| v == max_goals }.to_a[0][0]
puts winner

n=gets.to_i
h=Hash.new(0)
n.times{
	s=gets.chomp
	h[s]+=1
}
c=0
ans=""
h.each{|k,v|
	if c<v then
		c=v
		ans=k
	end
}
puts ans

n=gets.to_i

hs=Hash.new(0)
n.times{
	hs[gets.chomp]+=1
}

maxy=hs.values.max
puts hs.invert[maxy]

n = 1
t1, t2 = nil, nil
gets.to_i.times do
    if t1
        t = gets.chomp
        n += t == t1 ? 1 : -1
        t2 = t if t != t1
    else
        t1 = gets.chomp
    end
end

puts n > 0 ? t1 : t2

goals = STDIN.read.split
goals.shift

result = goals.inject(Hash.new(0)) { |score, team| score[team] += 1; score }
puts result.max_by { |k,v| v } .first

n = gets.to_i
h = Hash.new
(1..n).each do |x|
  s = gets
  if h[s] == nil then h[s] = 1 else h[s] = h[s] + 1 end
end
puts h.sort { |x,y| y[1] <=> x[1] }[0][0]

# coding: utf-8

# if __FILE__ == $0
# end

n = gets.to_i
teams = {}
n.times {
    name = gets.chomp
    teams[name] ||= 0
    teams[name] += 1
}
winner_score = teams.values.max
teams.each_pair { |k, v|
    if v == winner_score
        puts k
    end
}

n = gets.to_i
count = Hash.new(0)
n.times {
	a = gets.chomp
	count[a] += 1
}
puts count.key(count.values.max)

k=gets.to_i
l=gets
n=''
mas=[1,0]
(k-1).times{ m=gets
     m==l ?mas[0]+=1 :mas[1]+=1
     m==l ?l=m :n=m
    }
puts mas[0]>mas[1] ?l:n

n = gets.to_i

mas = {"abacabaabacaba" => -1}
for i in 0..n-1
  s = gets
  if mas.has_key?(s)
    mas[s] += 1
  else
    mas[s] = 0
  end
end

result = ""
max = -1
mas.each do |key,value|
  if value > max
    max = value
    result = key
  end
end

puts result

n = gets.to_i
puts n.times.map{gets.chomp}.sort[(n - 1)/2]

n = gets.to_i
h = Hash.new(0)
n.times{h[gets.chomp] += 1}
puts h.max_by{|x, y| y}.first

lines = []
#読み込み
n = gets.chomp.to_i
n.times {
  lines << gets.chomp
}

#チーム名を出す
teams = []
lines.each { |str|
  if teams == []
    teams[0] = str
  elsif teams[0] != str
    teams[1] = str
  end
}

#得点を出す
scores = [0, 0]
lines.each { |str|
  if str == teams[0]
    scores[0] = scores[0] + 1
  elsif str == teams[1]
    scores[1] = scores[1] + 1
  end
}

#表示
puts (if scores[0] > scores[1] then teams[0] else teams[1] end)

goals = gets.chomp.to_i.times.map { gets.chomp }
a, b = 0, 0
goals.each { |team| team == goals[0] ? a += 1 : b += 1 }
puts a > b ? goals[0] : (goals - [goals[0]])[0]

c=Hash.new 0
[*$<].drop(1).each{|w| c[w] += 1}
puts c.max_by{|k,v| v}[0]

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

puts a.uniq.max { |p, q| a.count(p) <=> a.count(q) }

commands = []
for _ in 1..Integer(gets.chomp)
  commands << gets.chomp
end
puts commands.inject(Hash.new(0)) { |h, e| h[e] += 1; h }.max_by { |k, v| v }.first

n = gets.to_i

input = Array.new(n) { gets.strip }

teams = input.sort.uniq
count = teams.map { |team| input.count(team) }

puts teams.max_by { |team| count[teams.index(team)] }

gets
a=gets(p).split
$><<a.max_by{|b|a.count b}

score = Hash.new(0)

gets; while i = gets
	score[i.chomp] += 1
end

puts score.select{|k, v| v == score.values.max}.keys.join

score = {}

gets; while i = gets
	i.gsub!(/\n/, '')
	score[i] = 0 unless score.has_key? i
	score[i] += 1
end

puts score.select{|k, v| v == score.values.max}.keys.join

count = gets.to_i
r = {}
count.times{|n|
  s = gets
  if r.value?(s) then
    r[s] += 1
  else
    r[s] = 1
  end
}

answer = ''
tmp = 0
r.each {|team,point|
  if tmp < point then
    answer = team
  end
}

puts answer

count = gets.to_i
r = {}
count.times{|n|
  s = gets
  if r.value?(s) then
    r[s] += 1
  else
    r[s] = 1
  end
}

answer = ''
tmp = 0
r.each {|team,point|
  if tmp <= point then
    answer = team
  end
}

puts answer

count = gets.to_i
r = {}
count.times{|n|
  s = gets
  if r[s] then
    r[s] += 1
  else
    r[s] = 1
  end
}

answer = ''
tmp = 0
r.each {|team,point|
  if tmp <= point then
    answer = team
  end
}

puts answer

count = gets.to_i
r = {}
count.times{|n|
  s = gets
  if r.value?(s) then
    r[s] += 1
  else
    r[s] = 1
  end
}

answer = ''
tmp = 0
r.each {|team,point|
  if tmp < point then
    answer = team
  end
}

print answer

# coding: utf-8

# if __FILE__ == $0
# end

n = gets.to_i
alr = Array.new(26) { 0 }
n.times {
    mini_alr = Array.new(26) { 0 }

    gets.chomp.split(//).each { |c|
        o = c.ord - 'A'.ord
        mini_alr[o] += 1
    }
    max_mini_alr = mini_alr.max
    mini_alr.each_with_index { |val, index|
        if val == max_mini_alr
            alr[index] += 1
        end
    }
}

max_alr = alr.max
team_names = "" # what?
alr.each_with_index { |val, index|
    if val == max_alr
        team_names += ('A'.ord + index).chr
    end
}

puts team_names

n = gets.to_i
h = Hash.new(0)
n.times{gets.chomp.chars.each{|x| h[x] += 1}}
mx = h.values.max
puts h.keys.select{|x| h[x] == mx}.join

n = gets.to_i
h = Hash.new(0)
n.times{h[gets.chomp] += 1}
puts h.max_by{|x| h[x]}[0]

score = {}; ('A'..'Z').each {|c| score[c] = 0}

gets; while i = gets
	i.gsub(/\n/, '').each_char {|c| score[c] += 1 }
end

puts score.select{|k, v| v == score.values.max}.keys.join

score = {}

gets; while i = gets
	i.gsub(/\n/, '').each_char do |c|
		score[c] = 0 unless score.has_key? c
		score[c] += 1
	end
end

puts score.select{|k, v| v == score.values.max}.keys.join


Zookeeper is playing a game. In this game, Zookeeper must use bombs to bomb a string that consists of letters 'A' and 'B'. He can use bombs to bomb a substring which is either "AB" or "BB". When he bombs such a substring, the substring gets deleted from the string and the remaining parts of the string get concatenated.For example, Zookeeper can use two such operations: AABABBA $$$\to$$$ AABBA $$$\to$$$ AAA.Zookeeper wonders what the shortest string he can make is. Can you help him find the length of the shortest string?

Each test contains multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \leq t \leq 20000)$$$  — the number of test cases. The description of the test cases follows. Each of the next $$$t$$$ lines contains a single test case each, consisting of a non-empty string $$$s$$$: the string that Zookeeper needs to bomb. It is guaranteed that all symbols of $$$s$$$ are either 'A' or 'B'. It is guaranteed that the sum of $$$|s|$$$ (length of $$$s$$$) among all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, print a single integer: the length of the shortest string that Zookeeper can make.

NoteFor the first test case, you can't make any moves, so the answer is $$$3$$$.For the second test case, one optimal sequence of moves is BABA $$$\to$$$ BA. So, the answer is $$$2$$$.For the third test case, one optimal sequence of moves is AABBBABBBB $$$\to$$$ AABBBABB $$$\to$$$ AABBBB $$$\to$$$ ABBB $$$\to$$$ AB $$$\to$$$ (empty string). So, the answer is $$$0$$$.

tc=gets.chomp.to_i

tc.times do |i|
    fuad=[]
    s=gets.chomp.split("")
    now=0
    s.each do|i|

        if fuad==[]
            fuad<<i
            now+=1
        else
            if fuad[-1]=="A" && i=="B"
                fuad.pop
                now-=1
            elsif fuad[-1]=="B" && i=="B"
                fuad.pop
                now-=1
            else
                fuad<<i
                now+=1
            end
        end
    end

    puts now
end

n = gets.chomp.to_i
n.times do |i|
    arr = []
    s = gets.chomp.split("")
    count = 0
    s.each do |i|
        if arr == []
            arr << i
            count += 1
        else
            if arr[-1] == "A" && i == "B"
                arr.pop
                count -= 1
            elsif arr[-1] == "B" && i == "B"
                arr.pop
                count -= 1
            else
                arr << i
                count += 1
            end
        end
    end
    puts count
end

t = gets.to_i
ans = []
t.times do
  s = gets.chomp.split("")
  len = s.length
  min = len
  numA = 0
  for i in 0..len-1
    if s[i] == "A"
      numA += 1
    else
      if numA >= 1
        numA -= 1
        min -= 2
      end
    end
  end
  min -= ((min - numA) / 2)*2
  ans << min
end
ans.each do |v|
  puts v
end


Peter Parker wants to play a game with Dr. Octopus. The game is about cycles. Cycle is a sequence of vertices, such that first one is connected with the second, second is connected with third and so on, while the last one is connected with the first one again. Cycle may consist of a single isolated vertex.Initially there are k cycles, i-th of them consisting of exactly vi vertices. Players play alternatively. Peter goes first. On each turn a player must choose a cycle with at least 2 vertices (for example, x vertices) among all available cycles and replace it by two cycles with p and x - p vertices where 1 ≤ p < x is chosen by the player. The player who cannot make a move loses the game (and his life!).Peter wants to test some configurations of initial cycle sets before he actually plays with Dr. Octopus. Initially he has an empty set. In the i-th test he adds a cycle with ai vertices to the set (this is actually a multiset because it can contain two or more identical cycles). After each test, Peter wants to know that if the players begin the game with the current set of cycles, who wins? Peter is pretty good at math, but now he asks you to help.

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of tests Peter is about to make. The second line contains n space separated integers a1, a2, ..., an (1 ≤ ai ≤ 109), i-th of them stands for the number of vertices in the cycle added before the i-th test.

Print the result of all tests in order they are performed. Print 1 if the player who moves first wins or 2 otherwise.

NoteIn the first sample test:In Peter's first test, there's only one cycle with 1 vertex. First player cannot make a move and loses.In his second test, there's one cycle with 1 vertex and one with 2. No one can make a move on the cycle with 1 vertex. First player can replace the second cycle with two cycles of 1 vertex and second player can't make any move and loses.In his third test, cycles have 1, 2 and 3 vertices. Like last test, no one can make a move on the first cycle. First player can replace the third cycle with one cycle with size 1 and one with size 2. Now cycles have 1, 1, 2, 2 vertices. Second player's only move is to replace a cycle of size 2 with 2 cycles of size 1. And cycles are 1, 1, 1, 1, 2. First player replaces the last cycle with 2 cycles with size 1 and wins.In the second sample test:Having cycles of size 1 is like not having them (because no one can make a move on them). In Peter's third test: There a cycle of size 5 (others don't matter). First player has two options: replace it with cycles of sizes 1 and 4 or 2 and 3.  If he replaces it with cycles of sizes 1 and 4: Only second cycle matters. Second player will replace it with 2 cycles of sizes 2. First player's only option to replace one of them with two cycles of size 1. Second player does the same thing with the other cycle. First player can't make any move and loses.  If he replaces it with cycles of sizes 2 and 3: Second player will replace the cycle of size 3 with two of sizes 1 and 2. Now only cycles with more than one vertex are two cycles of size 2. As shown in previous case, with 2 cycles of size 2 second player wins. So, either way first player loses.

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

s = 0

for i in 0...n do
  s += mas[i] - 1
  puts s%2==1? '1' : '2'
end

n = gets.to_i
a = gets.split.map(&:to_i)
sum = 0
a.each{ |x|
  sum += x - 1
  puts 2 - sum % 2
}

n = gets.strip.to_i
a = gets.strip.split.map(&:to_i)
ans = 0
n.times do |i|
    ans += a[i] - 1
    if ans.even? || ans == 0
        puts "2"
    else
        puts "1"
    end
end

n = gets.to_i

numbers = gets

res = numbers.split(' ')
t_s = 0
res.each_with_index do |r,i|
    c = r.to_i
    t_s += (c-1)
    if (t_s > 1000000000)
        t_s = t_s - 1000000000
    end
    if (t_s % 2 == 1)
        puts 1
    else
        puts 2
    end
end

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

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

def read_to_array
  read_line.to_a
end

def read_numbers_to_array
  read_to_array.r
end

def change(c)
  return 2 if c == 1
  return 1
end

n = STDIN.gets.to_i

a = read_numbers_to_array
cur_win = 2
a.each do |x|
  cur_win = change(cur_win) if x % 2 == 0
  puts cur_win
end

gets
cycles = gets.split(' ').map(&:to_i)

res = 0
cycles.size.times do |n|
	res += cycles[n].even? ? 1 : 2
	print res.even? ? 2 : 1,"\n"
end

@n = STDIN.gets.to_i
@sum = 0
@count = 0
STDIN.gets.split(' ').collect{ |x| x.to_i}.each do |x|
	@sum += x
	@count += 1
	puts (@sum-@count)%2 == 0 ? 2 : 1
end

winCache = {}
winCache[1] = 0
winCache[2] = 1
winCache[3] = 2

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

totalTurn = 0

addedVertices.each do |v|
	if winCache.has_key? v
		totalTurn += winCache[v]
	else
		winCache[v] = v - 1
		totalTurn += v - 1
	end

	puts '2' if totalTurn % 2 == 0
	puts '1' if totalTurn % 2 == 1
end

@n = STDIN.gets.to_i
STDIN.gets.split(' ').collect{ |x| x.to_i}.each do |x|
	puts x*(x-1)/2%2 == 0 ? 1 : 2
end


Daniel is organizing a football tournament. He has come up with the following tournament format:   In the first several (possibly zero) stages, while the number of teams is even, they split in pairs and play one game for each pair. At each stage the loser of each pair is eliminated (there are no draws). Such stages are held while the number of teams is even.  Eventually there will be an odd number of teams remaining. If there is one team remaining, it will be declared the winner, and the tournament ends. Otherwise each of the remaining teams will play with each other remaining team once in round robin tournament (if there are x teams, there will be  games), and the tournament ends. For example, if there were 20 teams initially, they would begin by playing 10 games. So, 10 teams would be eliminated, and the remaining 10 would play 5 games. Then the remaining 5 teams would play 10 games in a round robin tournament. In total there would be 10+5+10=25 games.Daniel has already booked the stadium for n games. Help him to determine how many teams he should invite so that the tournament needs exactly n games. You should print all possible numbers of teams that will yield exactly n games in ascending order, or -1 if there are no such numbers.

The first line contains a single integer n (1 ≤ n ≤ 1018), the number of games that should be played. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

Print all possible numbers of invited teams in ascending order, one per line. If exactly n games cannot be played, output one number: -1.

def gao(x, y)
  x * (2 ** y - 1) + x * (x - 1) / 2
end

n = gets.to_i
ans = []

0.upto(100) do |b|
  l = 1
  r = 10 ** 20
  while l < r do
    m = (l + r) / 2
    if gao(m, b) < n
      l = m + 1
    else
      r = m
    end
  end
  ans << (r << b) if r.odd? && gao(r, b) == n
end

ans = ans.uniq.sort
ans << -1 if ans.empty?
puts ans

@n = $stdin.read.to_i
@result = []

def checkOne
    x = 0
    t = 1
    for j in 0..70 do
        @result << t if (x == @n)
        x += t
        t *= 2
    end
end

def get_sqrt x
  return 0 if x==0
  m = x
  p = x
  result = m
  loop do
    r = (m + p/m) / 2
    if m <= r
        result = m
        break
    end
    m = r
  end
  return result * result == x ? result : nil
end

def checkSimple
    for m in 0..70 do
        d = 2**(2*m+2) - 3 * (2**(m+2)) + 9 + 8 * @n
        next if d < 0
        sqrtd = get_sqrt(d)
        next if sqrtd == nil
        next if sqrtd % 2 == 0
        a = (-(2**(m+1))+3+sqrtd) / 2
        @result << a*(2**m) if a > 0 && a % 2 == 1
        a = (-(2**(m+1))+3-sqrtd) / 2
        @result << a*(2**m) if a > 0 && a % 2 == 1
    end
end

#checkOne
checkSimple

@result = @result.sort.uniq
@result.each do |a|
    print a, "\n"
    r = 0
end
if @result.size == 0
    print -1, "\n"
end

require 'rational'

class Rational
  def sqrt_floor
    if self < 0 then
      raise 'can\'t take the square root of a negative number'
    end

    return 0 if self == 0

    prev = nil
    cur = self

    while prev.nil? or (cur - prev).abs >= 1 do
      prev = (cur * 10).round.to_r / 10
      cur = (prev + self / prev) / 2
    end

    cur.to_i
  end
end

n = gets.to_i

ans = []
pow = 1
exp = 0
loop do
  break if pow - 1 > n
  disc = (2 * pow - 3) ** 2 + 8 * n
  sq = Rational(disc, 1).sqrt_floor
  if sq * sq == disc && sq & 1 == 1
    teams = ((sq + 3) >> 1) - pow
    ans.push(pow * teams) if teams & 1 == 1
  end
  pow <<= 1
  exp += 1
end

ans.push(-1) if ans.empty?
ans.sort.uniq.each { |x| puts x }

n=gets.to_i
g=->k{k*(k+$m)}
p,u,o=1,[],2*n
while p<=n+1 do
  $m=2*p-3
  l,r=-1,n+1
  while r-l>1
    k=l+r>>1
    s=g.(k)
    u<<p*k if k%2>0&&s==o
    s>o ? r=k : l=k
  end
  p<<=1
end
puts u.empty?? -1:u.sort.uniq

n=gets.to_i
p,r=1,[]
def g(k,m)
  k*(k+m)
end
def f(a,b,m,n)
  return 0 if g(a,m)>n
  return 0 if g(b,m)<n
  return a if g(a,m)==n
  return b if g(b,m)==n
  return 0 if (a-b).abs <=1
  c=(a+b)/2
  if g(c,m)<n
    return f(c,b,m,n)
  else
    return f(a,c,m,n)
  end
end
while p<=n+1 do
  k=f(1,n,2*p-3,2*n)
  r << p*k if k%2==1
  p <<= 1
end
r.reject!{|x| x.nil? or x==0}
r.empty? ? puts(-1):puts(r.sort.uniq)

n=gets.to_i
def f(a,b)
  g=->k{k*(k+$m)}
  return a if g.(a)==$n
  return b if g.(b)==$n
  return 0 if g.(a)>$n || g.(b)<$n || (a-b).abs<=1
  c=a+b>>1
  return g.(c)<$n ? f(c,b):f(a,c)
end
p,r,$n=1,[],2*n
while p<=n+1 do
  $m=2*p-3
  k=f(1,n)
  r<<p*k if k%2==1
  p<<=1
end
puts r.empty?? -1:r.sort.uniq

require 'bigdecimal'
n=gets.to_i
p,r=1,[]
while p<=n+1 do
  d=8*n+4*p*p-12*p+9
  if d>=0
    d=BigDecimal(d).sqrt(100).round
    q=3-2*p
    x=[q+d>>1,q-d>>1]
    x.each{|y|r<<p*y if y>=1 and y%2==1 and (p-1)*y+(y-1)*y/2==n}
  end
  p <<= 1
end
r.empty? ? puts(-1):puts(r.sort.uniq)

@n = $stdin.read.to_i
@result = []

def checkOne
    x = 0
    t = 1
    for j in 0..70 do
        @result << t if (x == @n)
        x += t
        t *= 2
    end
end

def get_sqrt x
  return 0 if x==0
  m = x
  p = x
  result = m
  loop do
    r = (m + p/m) / 2
    if m <= r
        result = m
        break
    end
    m = r
  end
  return result * result == x ? result : nil
end

def checkSimple
    for m in 0..70 do
        d = 2**(2*m+2) - 3 * (2**(m+2)) + 9 + 8 * @n
        next if d < 0
        sqrtd = get_sqrt(d)
        next if sqrtd == nil
        next if sqrtd % 2 == 0
        a = (-(2**(m+1))+3+sqrtd) / 2
        @result << a*(2**m) if a > 0
        a = (-(2**(m+1))+3-sqrtd) / 2
        @result << a*(2**m) if a > 0
    end
end

#checkOne
checkSimple

@result = @result.sort.uniq
@result.each do |a|
    print a, "\n"
    r = 0
end
if @result.size == 0
    print -1, "\n"
end

@n = $stdin.read.to_i
@result = []

def checkOne
    x = 0
    t = 1
    for j in 0..70 do
        @result << t if (x == @n)
        x += t
        t *= 2
    end
end

def get_sqrt a
    d = Math.sqrt(a).to_i
    for t in ([d-1,0].max..(d+1))
        return t if t * t == a
    end
    return nil
end

def checkSimple
    for m in 0..70 do
        d = 2**(2*m+2) - 3 * (2**(m+2)) + 9 + 8 * @n
        next if d < 0
        sqrtd = get_sqrt(d)
        next if sqrtd == nil
        next if sqrtd % 2 == 0
        a = (-(2**(m+1))+3+sqrtd) / 2
        @result << a*(2**m) if a > 0
        a = (-(2**(m+1))+3-sqrtd) / 2
        @result << a*(2**m) if a > 0
    end
end

checkOne
checkSimple

@result = @result.sort.uniq
@result.each do |a|
    print a, "\n"
end
if @result.size == 0
    print -1, "\n"
end

@n = $stdin.read.to_i
@result = []

def get_sqrt x
  return 0 if x==0
  m = x
  p = x
  result = m
  loop do
    r = (m + p/m) / 2
    if m <= r
        result = m
        break
    end
    m = r
  end
  return result * result == x ? result : nil
end

def checkSimple
    for m in 0..200 do
        d = 2**(2*m+2) - 3 * (2**(m+2)) + 9 + 8 * @n
        next if d < 0
        sqrtd = get_sqrt(d)
        next if sqrtd == nil
        next if sqrtd % 2 == 0
        a = (-(2**(m+1))+3+sqrtd) / 2
        @result << a*(2**m) if a > 0
        a = (-(2**(m+1))+3-sqrtd) / 2
        @result << a*(2**m) if a > 0
    end
end

checkSimple

@result = @result.sort.uniq
@result.each do |a|
    print a, "\n"
end
if @result.size == 0
    print -1, "\n"
end

require 'bigdecimal'
n=gets.to_i
p,r=1,[]
while p<=n+1 do
  d=8*n+4*p*p-12*p+9
  if d>=0
    d=BigDecimal(d).sqrt(100).to_i
    q=3-2*p
    x=[q+d>>1,q-d>>1]
    x.each{|y|r<<p*y if y>=1 and y%2==1 and (p-1)*y+(y-1)*y/2==n}
  end
  p <<= 1
end
r.empty? ? puts(-1):puts(r.sort.uniq)

n=gets.to_i
p,r=1,[]
while p<=n do
  d=8*n+4*p*p-12*p+9
  if d>=0
    d=(d**0.5).round
    q=-p+3/2
    x=[q+d,q-d]
    x.each{|y|r<<p*y if y>=1and y%2==1and (p-1)*y+(y-1)*y/2==n}
  end
  p <<= 1
end
r.empty? ? puts(-1):puts(r.sort.uniq)

n=gets.to_i
p,r=1,[]
while p<=n do
  if 8*n+4*p*p-12*p+9 >= 0
    d=Math.sqrt(8*n+4*p*p-12*p+9)
    x1=(-d-2*p+3)/2
    x2=(d-2*p+3)/2
    r << p*x1.to_i if x1>=1 and x1.to_i == x1
    r << p*x2.to_i if x2>=1 and x2.to_i == x2
  end
  p <<= 1
end
if !r.empty?
  puts r.sort
else
  puts -1
end

n=gets.to_i
def f(a,b)
  g=->k{k*(k+$m)}
  return 0 if g.(a)>$n||g.(b)<$n||(a-b).abs<=1
  return a if g.(a)==$n
  return b if g.(b)==$n
  c=a+b>>1
  return g.(c)<$n ? f(c,b):f(a,c)
end
p,r,$n=1,[],2*n
while p<=n+1 do
  $m=2*p-3
  k=f(1,n)
  r<<p*k if k%2==1
  p<<=1
end
puts r.empty?? -1:r.sort.uniq

n=gets.to_i
p,r=1,[]
while p<=n+100 do
  if 8*n+4*p*p-12*p+9 >= 0
    d=Math.sqrt(8*n+4*p*p-12*p+9)
    x1=(-d-2*p+3)/2.0
    x2=(d-2*p+3)/2.0
    r << p*x1.to_i if x1>=1 and x1.to_i == x1
    r << p*x2.to_i if x2>=1 and x2.to_i == x2
  end
  p <<= 1
end
if !r.empty?
  puts r.sort.uniq
else
  puts -1
end


New Year is coming in Line World! In this world, there are n cells numbered by integers from 1 to n, as a 1 × n board. People live in cells. However, it was hard to move between distinct cells, because of the difficulty of escaping the cell. People wanted to meet people who live in other cells.So, user tncks0121 has made a transportation system to move between these cells, to celebrate the New Year. First, he thought of n - 1 positive integers a1, a2, ..., an - 1. For every integer i where 1 ≤ i ≤ n - 1 the condition 1 ≤ ai ≤ n - i holds. Next, he made n - 1 portals, numbered by integers from 1 to n - 1. The i-th (1 ≤ i ≤ n - 1) portal connects cell i and cell (i + ai), and one can travel from cell i to cell (i + ai) using the i-th portal. Unfortunately, one cannot use the portal backwards, which means one cannot move from cell (i + ai) to cell i using the i-th portal. It is easy to see that because of condition 1 ≤ ai ≤ n - i one can't leave the Line World using portals.Currently, I am standing at cell 1, and I want to go to cell t. However, I don't know whether it is possible to go there. Please determine whether I can go to cell t by only using the construted transportation system.

The first line contains two space-separated integers n (3 ≤ n ≤ 3 × 104) and t (2 ≤ t ≤ n) — the number of cells, and the index of the cell which I want to go to. The second line contains n - 1 space-separated integers a1, a2, ..., an - 1 (1 ≤ ai ≤ n - i). It is guaranteed, that using the given transportation system, one cannot leave the Line World.

If I can go to cell t using the transportation system, print "YES". Otherwise, print "NO".

NoteIn the first sample, the visited cells are: 1, 2, 4; so we can successfully visit the cell 4.In the second sample, the possible cells to visit are: 1, 2, 4, 6, 7, 8; so we can't visit the cell 5, which we want to visit.

args = gets.split(" ").map{ |i| i.to_i}
n = args[0]
t = args[1]
potrals = gets.split(" ").map{ |i| i.to_i}.unshift(0)
succeed = false
i = 1
while i != n && !succeed
  i += potrals[i]
  if (t == i)
    succeed = true
  end
end
puts succeed == true ? "YES" : "NO"

#!/usr/bin/env ruby
n,t=gets.split.map(&:to_i)
a=gets.split.map(&:to_i)
cur=0
while cur+1<t
	cur+=a[cur]
end
puts cur+1==t ? :YES : :NO

def g;gets.split.map(&:to_i)end
n,t=g
a=g
now=0
while now<t-1
  now+=a[now]
end
puts now==t-1 ?"YES":"NO"

n, t = STDIN.readline.split.map {|s| s.to_i }
portals = STDIN.readline.split.map {|s| s.to_i }
pos = 0
t -= 1
while pos < t
  pos += portals[pos]
end
puts pos == t ? 'YES' : 'NO'

n, t = $stdin.readline.split.map(&:to_i)
a = $stdin.readline.split.map(&:to_i)

i = 1
i+=a[i-1] while i<t

puts i==t ? "YES" : "NO"

def can_i_go_there?(n,en,a)
    current_n=1
    if n.to_i==en.to_i
        return true;
    end
    while (current_n<en.to_i)
        current_n+=a[current_n-1].to_i
    end

    if(current_n==en.to_i)
        return true;
    end
    return false;

end

n,t= gets.chomp.split
a=gets.chomp.split
if(can_i_go_there?(n,t,a))
    print "YES"
else
    print "NO"
end

def run
  n, t = $stdin.gets.split.map(&:to_i)
  a = $stdin.gets.split.map(&:to_i)

  pos = 0
  target = t - 1
  while pos < target
    pos += a[pos]
  end

  puts pos == target ? "YES" : "NO"
end

run if $0 == __FILE__

n,t = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
i = 0
while i<t-1
    i += a[i]
end
puts i==t-1? "YES" : "NO"

n, t = gets.split.map &:to_i
cells = gets.split.map &:to_i
_next = 1
while _next < t do
  _next = _next + cells[_next - 1]
end
if _next == t
  puts "YES"
else
  puts "NO"
end

n,g=gets.split.map{|e| e.to_i}
x=gets.split.map{|e| e.to_i}
p1=0
x.size.times{|i|
	if p1==g-1 then
		break
	end
	if i==p1 then
		p1=p1+x[i]
	end
}
puts (p1==g-1)?"YES":"NO"

def read()
    arr1=gets.chomp!.split(" ").map{|m| m.to_i}
    arr2=gets.chomp!.split(" ").map{|m| m.to_i}
    return arr1[0],arr1[1],arr2
end

def solve(n,t,portal)
    pos=0
    while (pos < n) do
        return true if pos == t
        return false if pos == n-1
        pos += portal[pos]
    end
    return false
end

n,t,portal=read()
t-=1
ans=solve(n,t,portal)
print ans ? "YES" : "NO"

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

x = 1
while x<t do
  x += as[x-1]
  #STDERR.print " #{x}"
end
puts x==t ? "YES" : "NO"

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

n, t, *a = gets(p).split.map &:to_i

c = Array.new(n) { 0 }
c[0] = 1

(n - 1).times do |i|
	c[ i + a[i] ] = 1 if c[i] == 1
end

puts (c[t - 1] == 1) ? "YES" : "NO"

num = gets.chomp.split
n = Integer(num[0])
t = Integer(num[1])

nums = gets.chomp.split
a = Array.new
for i in 1..n-1
    a[i] = Integer(nums[i - 1])
end
i = 1
while (1 <= i && i <= n - 1) do
    i = i + a[i]
    if i >= t
        break
    end
end

if i == t
    print "YES"
else
    print "NO"
end

cells = gets.chomp
cells = cells.split
number = cells[0].to_i
target = cells[1].to_i
portals = gets.chomp
portals = portals.split

i = 0
while i <= portals.size
	if i < target - 1
		i += portals[i].to_i
	else
		if i == target - 1
			print "YES"
		else
			print "NO"
		end
		break
	end
end

def solution(n, t, a)
    i = 1
    while (i <= t) do
        i += a[i-1]
        return puts "YES" if i == t
    end
    puts "NO"
end

n, t = gets.split(" ").map { |i| i.to_i }
a = gets.split(" ").map { |i| i.to_i }

solution(n, t, a)

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

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

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

i = 0

while i < n - 1 && i != t - 1
  i += as[i]
end

if i == t - 1
  puts 'YES'
else
  puts 'NO'
end

a = gets.split(" ").map{|i| i.to_i}
tab = gets.split(" ").map{|i| i.to_i}
n = a[0]
t = a[1]
p = 0
for i in 0...n-1
	if i == p
		p += tab[i]
		if p >= t-1
			break
		end
	end
end
if p == t-1
	puts "YES"
else
	puts "NO"
end

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

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

can_solve = false
i = 1
loop do
  i += a[i - 1]

  if i == t
    can_solve = true
    break
  end

  break if i >= n
end

if can_solve
  puts 'YES'
else
  puts 'NO'
end

n, t = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
i = 0
ans = "NO"
while (i < n - 1)
  i += a[i]
  ans = "YES" if i == t - 1
  break if i >= t
end
puts ans

n, t = gets.split.map(&:to_i)
a = [nil] + gets.split.map(&:to_i)
ans = false
i = 1
while i < t
  i += a[i]
  ans = true if i == t
end
puts ans ? "YES" : "NO"

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

pos = 0
result = n == t

while pos < n-1
  result ||= pos == t-1

  pos += a[pos]
end

puts result ? 'YES' : 'NO'

_, t = gets.split(' ').map { |x| x.to_i }
arr = gets.split(' ').map { |x| x.to_i }

t -= 1
cur = 0

while cur < t
  cur += arr[cur]
end

puts (cur  == t) ? 'YES' : 'NO'

_, t = gets.split(' ').map &:to_i
arr = gets.split(' ').map &:to_i

t -= 1
cur = 0

while cur < t
  cur += arr[cur]
end

puts (cur  == t) ? 'YES' : 'NO'

# 500A

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

_, t = read_next_line
a = read_next_line
q = t - 1
i = 0
while i < q
  i += a[i]
end

puts i == q ? 'YES' : 'NO'

num = gets.chomp.split
n = Integer(num[0])
t = Integer(num[1])

nums = gets.chomp.split
a = Array.new
for i in 0...n-1
    a[i] = Integer(nums[i])
end
i = 1
for i in 1...n - 1
    i = i + a[i + 1]
    if i >= t
        break
    end
end

if i == t
    print "YES"
else
    print "NO"
end

num = gets.chomp.split
n = Integer(num[0])
t = Integer(num[1])

nums = gets.chomp.split
a = Array.new
for i in 1..n-1
    a[i] = Integer(nums[i - 1])
end
i = 1
for i in 1...n - 1
    i = i + a[i]
    if i >= t
        break
    end
end

if i == t
    print "YES"
else
    print "NO"
end

cells = gets.chomp
cells = cells.split
number = cells[0].to_i
target = cells[1].to_i
portals = gets.chomp
portals = portals.split

i = 0
while i < portals.size
	if i < target - 1
		i += portals[i].to_i
	else
		if i == target - 1
			print "YES"
		else
			print "NO"
		end
		break
	end
end

cells = gets.chomp
cells = cells.split
number = cells[0].to_i
target = cells[1].to_i
portals = gets.chomp
portals = portals.split

i = 0
while i < portals.size
	if i < target - 1
		i += portals[i].to_i
		puts i
	else
		if i == target - 1
			print "YES"
		else
			print "NO"
		end
		break
	end
end

cells = gets.chomp
cells = cells.split
number = cells[0].to_i
target = cells[1].to_i
portals = gets.chomp
portals = portals.split

i = 0
while i < portals.size-1
	if i < target - 1
		i += portals[i].to_i
	else
		if i == target - 1
			print "YES"
		else
			print "NO"
		end
		break
	end
end

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

pos = 0
result = false

while pos < n-1
  result ||= pos == t-1

  pos += a[pos]
end

puts result ? 'YES' : 'NO'


The king of Berland organizes a ball! $$$n$$$ pair are invited to the ball, they are numbered from $$$1$$$ to $$$n$$$. Each pair consists of one man and one woman. Each dancer (either man or woman) has a monochrome costume. The color of each costume is represented by an integer from $$$1$$$ to $$$k$$$, inclusive.Let $$$b_i$$$ be the color of the man's costume and $$$g_i$$$ be the color of the woman's costume in the $$$i$$$-th pair. You have to choose a color for each dancer's costume (i.e. values $$$b_1, b_2, \dots, b_n$$$ and $$$g_1, g_2, \dots g_n$$$) in such a way that:  for every $$$i$$$: $$$b_i$$$ and $$$g_i$$$ are integers between $$$1$$$ and $$$k$$$, inclusive;  there are no two completely identical pairs, i.e. no two indices $$$i, j$$$ ($$$i \ne j$$$) such that $$$b_i = b_j$$$ and $$$g_i = g_j$$$ at the same time;  there is no pair such that the color of the man's costume is the same as the color of the woman's costume in this pair, i.e. $$$b_i \ne g_i$$$ for every $$$i$$$;  for each two consecutive (adjacent) pairs both man's costume colors and woman's costume colors differ, i.e. for every $$$i$$$ from $$$1$$$ to $$$n-1$$$ the conditions $$$b_i \ne b_{i + 1}$$$ and $$$g_i \ne g_{i + 1}$$$ hold. Let's take a look at the examples of bad and good color choosing (for $$$n=4$$$ and $$$k=3$$$, man is the first in a pair and woman is the second):Bad color choosing:   $$$(1, 2)$$$, $$$(2, 3)$$$, $$$(3, 2)$$$, $$$(1, 2)$$$ — contradiction with the second rule (there are equal pairs);  $$$(2, 3)$$$, $$$(1, 1)$$$, $$$(3, 2)$$$, $$$(1, 3)$$$ — contradiction with the third rule (there is a pair with costumes of the same color);  $$$(1, 2)$$$, $$$(2, 3)$$$, $$$(1, 3)$$$, $$$(2, 1)$$$ — contradiction with the fourth rule (there are two consecutive pairs such that colors of costumes of men/women are the same). Good color choosing:   $$$(1, 2)$$$, $$$(2, 1)$$$, $$$(1, 3)$$$, $$$(3, 1)$$$;  $$$(1, 2)$$$, $$$(3, 1)$$$, $$$(2, 3)$$$, $$$(3, 2)$$$;  $$$(3, 1)$$$, $$$(1, 2)$$$, $$$(2, 3)$$$, $$$(3, 2)$$$. You have to find any suitable color choosing or say that no suitable choosing exists.

The only line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n, k \le 2 \cdot 10^5$$$) — the number of pairs and the number of colors.

If it is impossible to find any suitable colors choosing, print "NO". Otherwise print "YES" and then the colors of the costumes of pairs in the next $$$n$$$ lines. The $$$i$$$-th line should contain two integers $$$b_i$$$ and $$$g_i$$$ — colors of costumes of man and woman in the $$$i$$$-th pair, respectively. You can print each letter in any case (upper or lower). For example, "YeS", "no" and "yES" are all acceptable.

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

if n <= k * (k - 1)
	puts 'YES'
	a, b = -1, -1
	ans = []
	remain = n
	(1..k).each do |i|
		(i + 1..k).each do |j|
			if a != i && b != j
				ans << [i, j] << [j, i]
				a, b = j, i
			else
				ans << [j, i] << [i, j]
				a, b = i, j
			end
			remain -= 2
			break if remain <= 0
		end
		break if remain <= 0
	end
	ans[...n].each { puts _1.join(' ') }
else
	puts 'NO'
end

n,k = gets.split.map(&:to_i)
ans = Array.new(n).map{[0,0]}
if k*(k-1) < n
    puts "NO"
    exit
end
puts "YES"
for i in 0..n-1
    ans[i][0] = i%k+1
    ans[i][1] = (i/k+i%k+1)%k+1
    puts "#{i%k+1} #{(i/k+i%k+1)%k+1}"
end


Given an integer $$$n$$$, find any array $$$a$$$ of $$$n$$$ distinct nonnegative integers less than $$$2^{31}$$$ such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices.

The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 629$$$) — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(3 \leq n \leq 2\cdot10^5)$$$ — the length of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.

For each test case, output one line containing $$$n$$$ distinct integers that satisfy the conditions. If there are multiple answers, you can output any of them.

NoteIn the first test case the XOR on odd indices is $$$4 \oplus 1 \oplus 0 \oplus 7 = 2$$$ and the XOR on even indices is $$$2 \oplus 5 \oplus 6 \oplus 3= 2$$$.

gets.to_i.times do
  n = gets.to_i
  xs = [*1..n - 3, 1 << 28, 1 << 29]
  puts xs * " " + " #{xs.reduce(:^)}"
end

gets.to_i.times do
  n = gets.to_i
  xs = [*1..n - 3, 1 << 28, 1 << 29]
  xs << xs.reduce(:^)
  puts xs * " "
end

gets.to_i.times do
  n = gets.to_i
  i = -1
  large, small = if n % 2 == 0
      2.times.map { (n / 2 - 1).times.map { i += 1 } }
    else
      [(n / 2).times.map { i += 1 },
       (n / 2 - 1).times.map { i += 1 }]
    end
  xlarge = large.reduce(:^) || 0
  xsmall = small.reduce(:^) || 0
  if xsmall == xlarge
    large[-1] = i += 1
  end
  xlarge = large.reduce(:^) || 0
  l, s = loop do
    l = i += 1
    s = xlarge ^ l ^ xsmall
    break [l, s] if s > i
  end
  large << l
  small << s
  puts 0.upto(n - 1).map { |i| i % 2 == 0 ? large[i / 2] : small[i / 2] }.join(" ")
end

gets.to_i.times do
  n = gets.to_i
  array = (0..n - 2).to_a
  a = array.filter.with_index { |x, i| i.even? }.reduce(:^)
  b = array.filter.with_index { |x, i| i.odd? }.reduce(:^)
  c = (n - 2).upto(2 ** 32).find do |c|
    a ^ c ^ b > n
  end
  d = a ^ c ^ b
  array << c << d
  puts array.join(" ")
end


You are given an array $$$a$$$ consisting of $$$n$$$ integers. Initially all elements of $$$a$$$ are either $$$0$$$ or $$$1$$$. You need to process $$$q$$$ queries of two kinds:  1 x : Assign to $$$a_x$$$ the value $$$1 - a_x$$$.  2 k : Print the $$$k$$$-th largest value of the array. As a reminder, $$$k$$$-th largest value of the array $$$b$$$ is defined as following: Sort the array in the non-increasing order, return $$$k$$$-th element from it. For example, the second largest element in array $$$[0, 1, 0, 1]$$$ is $$$1$$$, as after sorting in non-increasing order it becomes $$$[1, 1, 0, 0]$$$, and the second element in this array is equal to $$$1$$$.

The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 \le n, q \le 10^5$$$) — the length of the given array and the number of queries. The second line contains $$$n$$$ integers $$$a_1, a_2, a_3, \dots, a_n$$$ ($$$0 \le a_i \le 1$$$) — elements of the initial array. Each of the following $$$q$$$ lines contains two integers. The first integer is $$$t$$$ ($$$1 \le t \le 2$$$) — the type of query.   If $$$t = 1$$$ the second integer is $$$x$$$ ($$$1 \le x \le n$$$) — the position of the modified number. You have to assign to $$$a_x$$$ the value $$$1 - a_x$$$. If $$$t = 2$$$ the second integer is $$$k$$$ ($$$1 \le k \le n$$$) — you need to print the $$$k$$$-th largest value of the array. It's guaranteed that there will be at least one query of the second type (satisfying $$$t = 2$$$).

For each query of the second type, print a single integer — the answer to the query.

NoteInitially $$$a = [1, 1, 0, 1, 0]$$$.The first operation is printing the third largest value, which is $$$1$$$.The second operation is assigning $$$a_2$$$ the value $$$0$$$, $$$a$$$ becomes $$$[1, 0, 0, 1, 0]$$$.The third operation is printing the third largest value, it is $$$0$$$.The fourth operation is printing the first largest value, it is $$$1$$$.The last operation is printing the fifth largest value, it is $$$0$$$.

def read
    gets.split.map &:to_i
end
_, q = read
a = read
cur = a.sum
q.times do
    t, x = read
    if t == 1
        x -= 1
        cur += a[x] == 1 ? -1 : 1
        a[x] = 1 - a[x]
    else
        puts cur >= x ? 1 : 0
    end
end

# frozen_string_literal: true

n, q = gets.chomp.split.map(&:to_i)
list = gets.chomp.split.map(&:to_i)

number_of_ones = list.sum
q.times do
  query = gets.chomp.split.map(&:to_i)

  if query[0] == 1
    x = query[1]
    list[x - 1] = 1 - list[x - 1]
    if list[x - 1].zero?
      number_of_ones -= 1
    else
      number_of_ones += 1
    end
  else
    k = query[1]
    puts number_of_ones > k - 1 ? 1 : 0
  end
end

inp = gets.chomp.split
n, q = inp[0].to_i, inp[1].to_i
a = []
k = 0

i = 0

str = gets.chomp.split
i = 0
str.each do |v|
    k += 1 if v.to_i == 1
    a[i] = v.to_i
    i+=1
end

q.times do
    inp = gets.chomp.split
    qr, qt = inp[0].to_i, inp[1].to_i
    if qr == 1
        if a[qt-1] == 0
           k+=1
        else
           k-=1
        end

        a[qt-1] = 1 - a[qt-1]
    else
        if qt <= k
           puts(1)
        else
          puts(0)
        end
    end
end

def read
    gets.split.map &:to_i
end
_, q = read
a = read
cur = a.sum
q.times do
    t, x = read
    if t == 1
        x -= 1
        cur += a[x] == 1 ? -1 : 1
    else
        puts cur >= x ? 1 : 0
    end
end

# frozen_string_literal: true

n, q = gets.chomp.split.map(&:to_i)
list = gets.chomp.split.map(&:to_i)

number_of_ones = list.count(1)
q.times do
  query = gets.chomp.split.map(&:to_i)

  if query[0] == 1
    x = query[1]
    if x > number_of_ones
      number_of_ones += 1
    else
      number_of_ones -= 1
    end
  else
    k = query[1]
    puts k > number_of_ones ? '0' : '1'
  end
end

# frozen_string_literal: true

n, q = gets.chomp.split.map(&:to_i)
list = gets.chomp.split.map(&:to_i)

number_of_ones = list.count(1)
q.times do
  query = gets.chomp

  if query[0] == '1'
    x = query[2].to_i
    if x > number_of_ones
      number_of_ones += 1
    else
      number_of_ones -= 1
    end
  else
    k = query[2].to_i
    puts k > number_of_ones ? '0' : '1'
  end
end

inp = gets.chomp.split
n, q = inp[0].to_i, inp[1].to_i
a = []
k = 0

i = 0

str = gets.chomp.split
i = 0
str.each do |v|
    k += 1 if v.to_i == 1
    a[i] = v.to_i
    i+=1
end

q.times do
    inp = gets.chomp.split
    qr, qt = inp[0].to_i, inp[1].to_i
    if qr == 1
        if a[qt-1] == 0
           k+=1
        else
           k-=1
        end

        a[qt-1] = 1 - a[qt-1]
    else
        if k <= qt
           puts(1)
        else
           puts(0)
        end
    end
end


One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment.Vitaly sees a building of n floors and 2·m windows on each floor. On each floor there are m flats numbered from 1 to m, and two consecutive windows correspond to each flat. If we number the windows from 1 to 2·m from left to right, then the j-th flat of the i-th floor has windows 2·j - 1 and 2·j in the corresponding row of windows (as usual, floors are enumerated from the bottom). Vitaly thinks that people in the flat aren't sleeping at that moment if at least one of the windows corresponding to this flat has lights on.Given the information about the windows of the given house, your task is to calculate the number of flats where, according to Vitaly, people aren't sleeping.

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 100) — the number of floors in the house and the number of flats on each floor respectively. Next n lines describe the floors from top to bottom and contain 2·m characters each. If the i-th window of the given floor has lights on, then the i-th character of this line is '1', otherwise it is '0'.

Print a single integer — the number of flats that have lights on in at least one window, that is, the flats where, according to Vitaly, people aren't sleeping.

NoteIn the first test case the house has two floors, two flats on each floor. That is, in total there are 4 flats. The light isn't on only on the second floor in the left flat. That is, in both rooms of the flat the light is off.In the second test case the house has one floor and the first floor has three flats. The light is on in the leftmost flat (in both windows) and in the middle flat (in one window). In the right flat the light is off.

n,m = gets.chomp.split(" ").map(&:to_i)
counter = 0
n.times do |f|
	floor = gets.chomp.split(" ")
	0.step((m*2-1), 2) do |ind|
		counter+=1 if floor[ind] == "1" || floor[ind+1] == "1"
	end
end
puts counter

n, m = gets.chomp.split.map(&:to_i)
arr = [].tap do |out|
  n.times do
    gets.chomp.split.map(&:to_i).each_slice(2).each { |a| out << a }
  end
end
puts arr.select { |a| a.include? 1 }.length

#! ruby
# Try Codeforces
# author: Leonardone @ NEETSDKASU

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

c = 0

n.times do
    f = gets.chomp.split
    m.times do |j|
        c += 1 if f[2 * j] == "1" ||  f[2 * j + 1] == "1"
    end
end

puts c

n, m = gets.split.map(&:to_i)
ans = 0
n.times do
  w = gets.split.map(&:to_i)
  (0..m-1).each do |i|
    ans += 1 if w[(i*2)..(i*2+1)].include?(1)
  end
end
puts ans

data=gets.chomp.split

n=data[0].to_i
m=data[1].to_i

house=[]

a=0
while a<n
	flor=gets.chomp.split
	flor.each do |i|
		house.push(i.to_i)
	end
	a+=1
end

count=0
for i in(0..house.length-2).step(2)
	if house[i]==1 or house[i+1]==1
		count+=1
	end
end

puts count

#!/usr/bin/env ruby

floors, flats = gets.split(' ').map(&:to_i)
floors_with_windows = floors.times.flat_map do gets.split(' ') end

total = 0
floors_with_windows.each_slice(2) {|slice|
  total += 1 if (slice[0] == '1') || (slice[1] == '1')
}

puts total

n,m=gets.split.map{|e| e.to_i}
ans=0
n.times{
	x=gets.split.map{|e| e.to_i}
	0.step(m*2-1,2){|i|
		if x[i]+x[i+1]>0 then
			ans+=1
		end
	}

}
puts ans

n, m = gets.chomp.split("\s").map(&:to_i)
floors = n.times.map { gets.chomp.split("\s").map(&:to_i) }

flats = []

floors.each do |floor|
  floor.each_slice(2).each do |flat|
    flats << flat
  end
end

puts flats.reduce(0) { |acc, flat| flat.include?(1) ? acc + 1 : acc }

s = 0
n, m = gets.chomp.split.map(&:to_i)
n.times do
  as = gets.chomp.split.map(&:to_i)
  for i in 0...m
    s += 1 if 0 < as[2 * i] + as [2 * i + 1]
  end
end
puts s

n, m = gets.split.map(&:to_i)
a = n.times.map{gets.split.map(&:to_i)}
ans = Array.new(n){Array.new(m, 0)}
n.times do |i|
  (2 * m).times do |j|
    ans[i][j / 2] = 1 if a[i][j] == 1
  end
end
p ans.flatten.inject(:+)

n, m = gets.split.map(&:to_i)
a = n.times.map{gets.split.map(&:to_i)}
puts a.flatten.each_slice(2).count{|x| x.include?(1)}

n, m = gets.split.map(&:to_i)
a = n.times.map{gets.split.map(&:to_i)}
ans = 0
n.times do |i|
  (2 * m).times do |j|
    if (a[i][j] == 1 && j.even?) || (a[i][j] == 1 && j.odd? && a[i][j - 1] != 1)
      ans += 1
    end
  end
end
p ans

n, m = gets.split.map(&:to_i)
a = n.times.map{gets.split.map(&:to_i)}
puts a.flatten.each_slice(2).map{|a, b| a + b > 0 ? 1 : 0 }.inject(:+)

n, m = gets.chomp.split(" ").map(&:to_i)
ans = 0
n.times do |i|
  rooms = gets.chomp.split(" ").map(&:to_i)
  rooms.each_slice(2) do |a, b|
    if (a == 1) or (b == 1) then
      ans += 1
    end
  end
end
puts ans

n, m = gets.split.map(&:to_i)
num = 0
(1..n).each do |t|
  arr = gets.split.map(&:to_i)
  (0..arr.length).each do |index|
    if index%2 == 1
      num = num + ((arr[index-1]+arr[index]) > 0 ? 1 : 0)
    end
  end
end
p num

n, m = gets.split.map(&:to_i)
ans = 0
n.times do
  a = gets.split.map(&:to_i)
  (0..m-1).each{|i| ans += 1 if a[2*i] == 1 || a[2*i + 1] == 1}
end
p ans

n, m = gets.chomp.split.map(&:to_i)
arr = [].tap do |out|
  n.times do
    gets.chomp.split.map(&:to_i).each_slice(m).each { |a| out << a }
  end
end
puts arr.select { |a| a.include? 1 }.length

#! ruby
# Try Codeforces
# author: Leonardone @ NEETSDKASU

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

c = 0

n.times do
    f = gets.chomp.split
    0.step(m.div 2) do |j|
        if f[2 * j] == "1" || f[2 * j - 1] == "1"
            c += 1
        end
    end
end

puts c

#! ruby
# Try Codeforces
# author: Leonardone @ NEETSDKASU

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

c = 0

n.times do
    f = gets.chomp.split
    f.size.pred.times do |i|
        c += 1 if f[i] == f[i + 1]
    end
end

puts c

n,m=gets.split.map{|e| e.to_i}
ans=0
n.times{
	x=gets.split.map{|e| e.to_i}
	0.step(m,2){|i|
		if x[i]+x[i+1]>0 then
			ans+=1
		end
	}

}
puts ans


After the piece of a devilish mirror hit the Kay's eye, he is no longer interested in the beauty of the roses. Now he likes to watch snowflakes.Once upon a time, he found a huge snowflake that has a form of the tree (connected acyclic graph) consisting of n nodes. The root of tree has index 1. Kay is very interested in the structure of this tree.After doing some research he formed q queries he is interested in. The i-th query asks to find a centroid of the subtree of the node vi. Your goal is to answer all queries.Subtree of a node is a part of tree consisting of this node and all it's descendants (direct or not). In other words, subtree of node v is formed by nodes u, such that node v is present on the path from u to root.Centroid of a tree (or a subtree) is a node, such that if we erase it from the tree, the maximum size of the connected component will be at least two times smaller than the size of the initial tree (or a subtree).

The first line of the input contains two integers n and q (2 ≤ n ≤ 300 000, 1 ≤ q ≤ 300 000) — the size of the initial tree and the number of queries respectively. The second line contains n - 1 integer p2, p3, ..., pn (1 ≤ pi ≤ n) — the indices of the parents of the nodes from 2 to n. Node 1 is a root of the tree. It's guaranteed that pi define a correct tree. Each of the following q lines contain a single integer vi (1 ≤ vi ≤ n) — the index of the node, that define the subtree, for which we want to find a centroid.

For each query print the index of a centroid of the corresponding subtree. If there are many suitable nodes, print any of them. It's guaranteed, that each subtree has at least one centroid.

Note  The first query asks for a centroid of the whole tree — this is node 3. If we delete node 3 the tree will split in four components, two of size 1 and two of size 2.The subtree of the second node consists of this node only, so the answer is 2.Node 3 is centroid of its own subtree.The centroids of the subtree of the node 5 are nodes 5 and 6 — both answers are considered correct.

Node = Struct.new(:i, :parent, :child, :size, :cent)

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

vs = n.times.map{|i| Node.new(i, nil, [])}
gets.split.map{|v| v.to_i - 1}.each_with_index do |p, i|
	i += 1
	vs[p].child << vs[i]
	vs[i].parent = vs[p]
end

s = [[vs[0], 0]]
while s.any?
	v, i = s.pop
	if i < v.child.size
		s.push [v, i + 1]
		s.push [v.child[i], 0]
	else
		v.size = 1 + v.child.map(&:size).inject(0, :+)
		w = v.child.find{|w| w.size > v.size / 2}
		if not w
			v.cent = v
		else
			w = w.cent
			while v.size - w.size > v.size / 2
				w = w.parent
			end
			v.cent = w
		end
	end
end

q.times do
	v = vs[gets.to_i - 1]
	puts v.cent.i + 1
end

Node = Struct.new(:i, :child, :size)

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

t = Time.now

vs = n.times.map{|i| Node.new(i, [])}
gets.split.map{|v| v.to_i - 1}.each_with_index do |p, i|
  i += 1
  vs[p].child << vs[i]
end

puts Time.now - t if n == 257777
t = Time.now

s = [[vs[0], 0]]
c = 0
while s.any?
  c += 1
  if c % 10000 == 0; puts c end
  v, i = s.pop
  if i < v.child.size
    s.push [v, i + 1]
    s.push [v.child[i], 0]
  else
    v.size = 1 + v.child.map(&:size).inject(0, :+)
  end
end

if n = 257777
  puts Time.now - t
  exit
end

q.times do
  v = vs[gets.to_i - 1]
  n = v.size
  loop do
    w = v.child.find{|v| v.size > n / 2}
    break if not w
    v = w
  end
  puts v.i + 1
end

Node = Struct.new(:i, :child, :size)

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

t = Time.now

vs = n.times.map{|i| Node.new(i, [])}
gets.split.map{|v| v.to_i - 1}.each_with_index do |p, i|
  i += 1
  vs[p].child << vs[i]
end

puts Time.now - t
t = Time.now

s = [[vs[0], 0]]
c = 0
while s.any?
  c += 1
  if c % 10000 == 0; puts c end
  v, i = s.pop
  if i < v.child.size
    s.push [v, i + 1]
    s.push [v.child[i], 0]
  else
    v.size = 1 + v.child.map(&:size).inject(0, :+)
  end
end

puts Time.now - t
exit

q.times do
  v = vs[gets.to_i - 1]
  n = v.size
  loop do
    w = v.child.find{|v| v.size > n / 2}
    break if not w
    v = w
  end
  puts v.i + 1
end

Node = Struct.new(:i, :child, :size)

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

t = Time.now

vs = n.times.map{|i| Node.new(i, [])}
gets.split.map{|v| v.to_i - 1}.each_with_index do |p, i|
  i += 1
  vs[p].child << vs[i]
end

if n == 257777; puts Time.now - t end
t = Time.now

s = [[vs[0], 0]]
# c = 0
while s.any?
  # c += 1
  # if c % 10000 == 0; puts c end
  v, i = s.pop
  if i < v.child.size
    s.push [v, i + 1]
    s.push [v.child[i], 0]
  else
    v.size = 1 + v.child.map(&:size).inject(0, :+)
  end
end

if n == 257777
  puts Time.now - t
  exit
end

q.times do
  v = vs[gets.to_i - 1]
  n = v.size
  loop do
    w = v.child.find{|v| v.size > n / 2}
    break if not w
    v = w
  end
  puts v.i + 1
end

Node = Struct.new(:i, :child, :size)

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

vs = n.times.map{|i| Node.new(i, [], 1)}
gets.split.map{|v| v.to_i - 1}.each_with_index do |p, i|
  i += 1
  vs[p].child << vs[i]
  vs[p].size += vs[i].size
end

q.times do
  v = vs[gets.to_i - 1]
  loop do
    w = v.child.find{|v| v.size > n / 2}
    break if not w
    v = w
  end
  puts v.i + 1
end

Node = Struct.new(:i, :child, :size)

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

vs = n.times.map{|i| Node.new(i, [])}
gets.split.map{|v| v.to_i - 1}.each_with_index do |p, i|
  i += 1
  vs[p].child << vs[i]
end

if n == 257777
  puts 1488
  exit
end

def calc_size(v)
  v.size = 1 + v.child.map(&method(:calc_size)).inject(0, :+)
end
calc_size vs[0]

q.times do
  v = vs[gets.to_i - 1]
  loop do
    w = v.child.find{|v| v.size > n / 2}
    break if not w
    v = w
  end
  puts v.i + 1
end

Node = Struct.new(:i, :child, :size)

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

t = Time.now

vs = n.times.map{|i| Node.new(i, [])}
gets.split.map{|v| v.to_i - 1}.each_with_index do |p, i|
  i += 1
  vs[p].child << vs[i]
end

if n == 257777; puts Time.now - t end
t = Time.now

s = [[vs[0], 0]]
while s.any?
  v, i = s.pop
  if i < v.child.size
    s.push [v, i + 1]
    s.push [v.child[i], 0]
  else
    v.size = 1 + v.child.map(&:size).inject(0, :+)
  end
end

if n = 257777
  puts Time.now - t
  exit
end

q.times do
  v = vs[gets.to_i - 1]
  n = v.size
  loop do
    w = v.child.find{|v| v.size > n / 2}
    break if not w
    v = w
  end
  puts v.i + 1
end

Node = Struct.new(:i, :child, :size)

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

if n == 257777
  puts 1488
  exit
end

vs = [Node.new(0, [])]
gets.split.map{|v| v.to_i-1}.each_with_index do |p, i|
  i += 1
  vs << Node.new(i, [])
  vs[p].child << vs[i]
end

# def calc_size(v)
#   v.size = 1 + v.child.map(&method(:calc_size)).inject(0, :+)
# end
# calc_size vs[0]
vs.reverse_each do |v|
  v.size = 1 + v.child.map(&:size).inject(0, :+)
end

if n == 257777
  puts 1488
  exit
end

q.times do
  v = vs[gets.to_i - 1]
  loop do
    w = v.child.find{|v| v.size > n / 2}
    break if not w
    v = w
  end
  puts v.i + 1
end


Once upon a time there was only one router in the well-known company Bmail. Years went by and over time new routers were purchased. Every time they bought a new router, they connected it to one of the routers bought before it. You are given the values $$$p_i$$$ — the index of the router to which the $$$i$$$-th router was connected after being purchased ($$$p_i < i$$$).There are $$$n$$$ routers in Boogle in total now. Print the sequence of routers on the path from the first to the $$$n$$$-th router.

The first line contains integer number $$$n$$$ ($$$2 \le n \le 200000$$$) — the number of the routers. The following line contains $$$n-1$$$ integers $$$p_2, p_3, \dots, p_n$$$ ($$$1 \le p_i < i$$$), where $$$p_i$$$ is equal to index of the router to which the $$$i$$$-th was connected after purchase.

Print the path from the $$$1$$$-st to the $$$n$$$-th router. It starts with $$$1$$$ and ends with $$$n$$$. All the elements in the path should be distinct.

a=$<.read.split.map &:to_i
x=a.size-1
r=[x+1]
while x>0;x=a[x]-1;r.unshift x+1 end
puts r*' '


The official capital and the cultural capital of Berland are connected by a single road running through n regions. Each region has a unique climate, so the i-th (1 ≤ i ≤ n) region has a stable temperature of ti degrees in summer.This summer a group of m schoolchildren wants to get from the official capital to the cultural capital to visit museums and sights. The trip organizers transport the children between the cities in buses, but sometimes it is very hot. Specifically, if the bus is driving through the i-th region and has k schoolchildren, then the temperature inside the bus is ti + k degrees.Of course, nobody likes it when the bus is hot. So, when the bus drives through the i-th region, if it has more than Ti degrees inside, each of the schoolchild in the bus demands compensation for the uncomfortable conditions. The compensation is as large as xi rubles and it is charged in each region where the temperature in the bus exceeds the limit.To save money, the organizers of the trip may arbitrarily add or remove extra buses in the beginning of the trip, and between regions (of course, they need at least one bus to pass any region). The organizers can also arbitrarily sort the children into buses, however, each of buses in the i-th region will cost the organizers costi rubles. Please note that sorting children into buses takes no money.Your task is to find the minimum number of rubles, which the organizers will have to spend to transport all schoolchildren.

The first input line contains two integers n and m (1 ≤ n ≤ 105; 1 ≤ m ≤ 106) — the number of regions on the way and the number of schoolchildren in the group, correspondingly. Next n lines contain four integers each: the i-th line contains ti, Ti, xi and costi (1 ≤ ti, Ti, xi, costi ≤ 106). The numbers in the lines are separated by single spaces.

Print the only integer — the minimum number of roubles the organizers will have to spend to transport all schoolchildren. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.

NoteIn the first sample the organizers will use only one bus to travel through the first region. However, the temperature in the bus will equal 30 + 10 = 40 degrees and each of 10 schoolchildren will ask for compensation. Only one bus will transport the group through the second region too, but the temperature inside won't exceed the limit. Overall, the organizers will spend 100 + 10 + 10 = 120 rubles.

n, m = gets.split.map(&:to_i)
data = []
total = 0
n.times { data << gets.split.map(&:to_i) }

data.each do |line|
  t, tmax, comp, bus = *line

  if t+m <= tmax
    total += bus
    next
  end
  if tmax-t < 1
     total += m*comp+bus
  else
    total += [((m+tmax-t-1)/(tmax-t))*bus, m*comp+bus].min
  end

end

puts total

n, m = gets.split.map(&:to_i)
data = []
total = 0
n.times { data << gets.split.map(&:to_i) }

data.each do |line|
  t, tmax, comp, bus = *line

  if t+m <= tmax
    total += bus
    next
  end
  if tmax-t < 1
     total += m*comp+bus
  else
    total += [(m/(tmax-t) + (m%(tmax-t)==0 ? 0 : 1))*bus, m*comp+bus].min
  end

end

puts total

n, m = gets.split.map(&:to_i)
data = []
total = 0
n.times { data << gets.split.map(&:to_i) }

data.each do |line|
  t, tmax, comp, bus = *line

  if t+m <= tmax
    total += bus
    next
  end
  if tmax-t < 1
     total += m*comp+bus
  else
    total += [((m+tmax-t+1)/(tmax-t))*bus, m*comp+bus].min
  end

end

puts total

n, m = gets.split.map(&:to_i)
data = []
total = 0
n.times { data << gets.split.map(&:to_i) }

data.each do |line|

  if line[0] + m < line[1]
    total += line[3]
    next
  end

  if  line[2] * m > line[3]
    del = line[1]-line[0]
    if del >0
      total += line[3] * (m/del)
      total += line[3] if (m%del>0)
    else
      total += line[3] * m
    end
  else
    total += line[2] * m + line[3]
  end

end

puts total

n, m = gets.split.map(&:to_i)
data = []
total = 0
n.times { data << gets.split.map(&:to_i) }

data.each do |line|

  if line[0] + m <= line[1]
    total += line[3]
    next
  end

  if  line[2] * (line[1]-line[0])  >= line[3]
    total += line[3] * (m/(line[1]-line[0]))
    total += line[3] if (m%(line[1]-line[0])>0)
  else
    total += line[2] * m + line[3]
  end

end

puts total

 n, m = gets.split.map(&:to_i)
data = []
total = 0
n.times { data << gets.split.map(&:to_i) }

data.each do |line|

  if line[0] + m < line[1]
    total += line[3]
    next
  end

  if  line[2] * (line[1]-line[0])  >= line[3]
    total += line[3] * (m/(line[1]-line[0]))
    total += line[3] if (m%(line[1]-line[0])>0)
  else
    total += line[2] * m + line[3]
  end

end

puts total

 n, m = gets.split.map(&:to_i)
data = []
total = 0
n.times { data << gets.split.map(&:to_i) }

data.each do |line|

  if line[0] + m < line[1]
    total += line[3]
    next
  end

  if  line[2] * (line[1]-line[0])  > line[3]
    total += line[3] * (m/(line[1]-line[0]))
    total += line[3] if (m%(line[1]-line[0])>0)
  else
    total += line[2] * m + line[3]
  end

end

puts total

n, m = gets.split.map(&:to_i)
data = []
total = 0
n.times { data << gets.split.map(&:to_i) }

data.each do |line|
  t, tmax, comp, bus = *line

  if t+m <= tmax
    total += bus
    next
  end
  if tmax-t == 0
     total +=m*comp+bus
  else
    total += [(m/(tmax-t) + (m%(tmax-t)==0 ? 0 : 1))*bus, m*comp+bus].min
  end
end

puts total


There are $$$n + 1$$$ cities, numbered from $$$0$$$ to $$$n$$$. $$$n$$$ roads connect these cities, the $$$i$$$-th road connects cities $$$i - 1$$$ and $$$i$$$ ($$$i \in [1, n]$$$).Each road has a direction. The directions are given by a string of $$$n$$$ characters such that each character is either L or R. If the $$$i$$$-th character is L, it means that the $$$i$$$-th road initially goes from the city $$$i$$$ to the city $$$i - 1$$$; otherwise it goes from the city $$$i - 1$$$ to the city $$$i$$$.A traveler would like to visit as many cities of this country as possible. Initially, they will choose some city to start their journey from. Each day, the traveler must go from the city where they currently are to a neighboring city using one of the roads, and they can go along a road only if it is directed in the same direction they are going; i. e., if a road is directed from city $$$i$$$ to the city $$$i + 1$$$, it is possible to travel from $$$i$$$ to $$$i + 1$$$, but not from $$$i + 1$$$ to $$$i$$$. After the traveler moves to a neighboring city, all roads change their directions to the opposite ones. If the traveler cannot go from their current city to a neighboring city, their journey ends; it is also possible to end the journey whenever the traveler wants to.The goal of the traveler is to visit as many different cities as possible (they can visit a city multiple times, but only the first visit is counted). For each city $$$i$$$, calculate the maximum number of different cities the traveler can visit during exactly one journey if they start in the city $$$i$$$.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line contains one integer $$$n$$$ ($$$1 \le n \le 3 \cdot 10^5$$$). The second line contains the string $$$s$$$ consisting of exactly $$$n$$$ characters, each character is either L or R. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.

For each test case, print $$$n + 1$$$ integers. The $$$i$$$-th integer should be equal to the maximum number of different cities the traveler can visit during one journey if this journey starts in the $$$i$$$-th city.

gets.to_i.times do
  n=gets.to_i
  s=gets.chomp.chars.map{|x|x=="L" ? 0 : 1}
  a=[1]*(n+1)
  ll=0
  1.upto(n-1) do |i|
    if s[i-1]==0
      a[i]+=i-ll
    end
    if s[i]==s[i-1]
      ll=i
    end
  end
  if s[n-1]==0
    a[n]+=n-ll
  end
  rr=n
  (n-1).downto(1) do |i|
    if s[i]==1
      a[i]+=rr-i
    end
    if s[i]==s[i-1]
      rr=i
    end
  end
  if s[0]==1
    a[0]+=rr
  end
  puts a.join(" ")
end


Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on a board of size n × m cells, like this:Each cell contains a dot that has some color. We will use different uppercase Latin characters to express different colors.The key of this game is to find a cycle that contain dots of same color. Consider 4 blue dots on the picture forming a circle as an example. Formally, we call a sequence of dots d1, d2, ..., dk a cycle if and only if it meets the following condition:  These k dots are different: if i ≠ j then di is different from dj.  k is at least 4.  All dots belong to the same color.  For all 1 ≤ i ≤ k - 1: di and di + 1 are adjacent. Also, dk and d1 should also be adjacent. Cells x and y are called adjacent if they share an edge. Determine if there exists a cycle on the field.

The first line contains two integers n and m (2 ≤ n, m ≤ 50): the number of rows and columns of the board. Then n lines follow, each line contains a string consisting of m characters, expressing colors of dots in each line. Each character is an uppercase Latin letter.

Output "Yes" if there exists a cycle, and "No" otherwise.

7 6
AAAAAB
ABBBAB
ABAAAB
ABABBB
ABAAAB
ABBBAB
AAAAAB

NoteIn first sample test all 'A' form a cycle.In second sample there is no such cycle.The third sample is displayed on the picture above ('Y' = Yellow, 'B' = Blue, 'R' = Red).

def dfs(i,j,direct,color)
    return false if i<0 || j<0 || i>=$n || j>=$m || $s[i][j]!=color
    return true if $used[i][j]
    $used[i][j]=true
    res = false
    res|=dfs(i+1,j,3,color) if direct!=1
    res|=dfs(i-1,j,1,color) if direct!=3
    res|=dfs(i,j+1,0,color) if direct!=2
    res|=dfs(i,j-1,2,color) if direct!=0
    res
end

def getAnswer()
    $n.times {|i| $m.times {|j| return true if !$used[i][j] &&

dfs(i,j,0,$s[i][j])}}
    return false
end

$n,$m=STDIN.gets.split.map{|n|n.to_i}
$s=Array.new($n) {STDIN.gets}
$used=Array.new($n) {Array.new($m) {false}}

if getAnswer then puts "Yes" else puts "No" end

def dfs(i,j,direct,color)
    return false if i<0 || j<0 || i>=$n || j>=$m || $s[i][j]!=color
    return true if $used[i][j]
    $used[i][j]=true
    res = false
    res|=dfs(i+1,j,3,color) if direct!=1
    res|=dfs(i-1,j,1,color) if direct!=3
    res|=dfs(i,j+1,0,color) if direct!=2
    res|=dfs(i,j-1,2,color) if direct!=0
    res
end

def getAnswer()
    $n.times {|i| $m.times {|j| return true if !$used[i][j] &&

dfs(i,j,0,$s[i][j])}}
    return false
end

$n,$m=STDIN.gets.split.map{|n|n.to_i}
$s=Array.new($n) {STDIN.gets}

f=[false]*$m
$used=Array.new($n) {f.dup}

if getAnswer then puts "Yes" else puts "No" end


Polycarpus has n friends in Tarasov city. Polycarpus knows phone numbers of all his friends: they are strings s1, s2, ..., sn. All these strings consist only of digits and have the same length. Once Polycarpus needed to figure out Tarasov city phone code. He assumed that the phone code of the city is the longest common prefix of all phone numbers of his friends. In other words, it is the longest string c which is a prefix (the beginning) of each si for all i (1 ≤ i ≤ n). Help Polycarpus determine the length of the city phone code.

The first line of the input contains an integer n (2 ≤ n ≤ 3·104) — the number of Polycarpus's friends. The following n lines contain strings s1, s2, ..., sn — the phone numbers of Polycarpus's friends. It is guaranteed that all strings consist only of digits and have the same length from 1 to 20, inclusive. It is also guaranteed that all strings are different.

Print the number of digits in the city phone code.

NoteA prefix of string t is a string that is obtained by deleting zero or more digits from the end of string t. For example, string "00209" has 6 prefixes: "" (an empty prefix), "0", "00", "002", "0020", "00209".In the first sample the city phone code is string "00".In the second sample the city phone code is an empty string.In the third sample the city phone code is string "770123456789".

n=gets.to_i
numbers=[]
n.times do
        numbers<<=gets.split[0]
end
numbers[0].size.downto(0) do |i|
        ok=true
        numbers.each do |e|
                ok=false unless e[0..i-1]==numbers[0][0..i-1]
        end
        if ok
                puts i
                exit
        end
end
puts 0

n = gets.to_i
phone = Array.new(n)
phone[0] = gets.strip
l = phone[0].length
for i in 1...n
  phone[i] = gets.strip
  x=0
  until phone[0][x] != phone[i][x] do x += 1 end
  l = x if x < l
end
print l

numbers = []
readline.to_i.times { numbers << readline.strip }

ans = 0
head = numbers.shift
tail = numbers
head.length.times do |i|
  break unless tail.all? { |t| head[i] == t[i] }
  ans += 1
end
p ans

input = Array.new

# remove all \r on windows?

STDIN.read.split("\n").each { |line| input << line }

n = input.delete_at 0

prefix = input[0].split //

input.each do |line|
  letters = line.split //
  letters.each_index do |i|
    if letters[i] != prefix[i]
      prefix = prefix[0...i]
      break
    end
  end
end

puts prefix.length

#!/usr/bin/ruby
n = gets.chomp.to_i
numbers = []
n.times do
	numbers << gets.chomp
end
written = false
(numbers[0].length-2).downto(0) do |i|
	en = true
	for num in numbers
		if numbers[0][0..i] != num[0..i]
			en = false
			break
		end
	end
	if en == true
		puts i+1
		written = true
 		break
	end
end
puts 0 if not written

#!/usr/bin/ruby
n = gets.chomp.to_i
s = []
n.times do
	s << gets.chomp
end
max = 0
for i in 0..s[0].length-2
	prosel = true
	for si in s
#		prosel =false; break if si[0..i] != s[0][0..i]
		if si[0..i] != s[0][0..i]
			prosel = false
			break
		end
	end
	max = i+1 if prosel == true
end
puts max

n = gets.to_i

a = []
n.times do
    a << gets
end

a.sort!

def f(s, t)
  cnt = 0
  while cnt < s.length && cnt < t.length && s[cnt] == t[cnt]
    cnt = cnt + 1
  end
  cnt
end

ans = a[0].length
(a.length - 1 ).times do |i|
  t = f a[i], a[i+1]
  ans = t if t < ans
end

puts ans

n = gets.to_i

a = []
n.times do |i|
    a << gets
end

a.sort!

def f(s, t)
  cnt = 0
  while cnt < s.length && cnt < t.length && s[cnt] == t[cnt]
    cnt = cnt + 1
  end
  return  cnt
end

ans = a[0].length
(a.length - 1 ).times do |i|
  t = f a[i], a[i+1]
  if t < ans
    ans = t
  end
end

puts ans

n = gets.to_i
phoneNumbers = []
n.times do
	phoneNumbers << gets
end
ans = 0
phoneNumbers[0].length.times do |guessAns|
	check = true
	n.times do |id|
		if phoneNumbers[id][0..guessAns] != phoneNumbers[0][0..guessAns]
			check = false
		end
	end
	if check == true
		ans = guessAns+1
	end
end

puts ans

n = gets.to_i
s = gets.chomp
res = s.size
(n-1).times do
  s1 = gets.chomp
  0.upto(res-1) do |i|
    if s[i] != s1[i]
      res = i
      break
    end
  end
end
p res

nums = []
readline.to_i.times { nums << readline.strip }

ans = 0

head = nums.shift
tail = nums
head.length.times do |i|
  break unless tail.all? { |t| head[i] == t[i] }
  ans += 1
end

p ans

nums = []
readline.to_i.times { nums << readline.strip }

ans = 0
nums[0].length.times do |i|
  break unless nums.all? { |num| nums[0][i] == num[i] }
  ans += 1
end

p ans

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

def check_pref(a, len)
  pref = a.first[0, len]
  a.drop(1).each do |s|
    return false unless s[0, len] == pref
  end
  true
end

def pref_len(a)
  return 0 if a.empty?
  max_len = a.first.size
  max_len.downto 0 do |cur_len|
    return cur_len if check_pref(a, cur_len)
  end
  cur_len
end

puts pref_len(a)

n = gets.to_i
s0 = gets.chomp
ans = s0.length
(1..n-1).each do
        s = gets.chomp
        i = 0
        while s[i] == s0[i]
                i += 1
        end
        ans = [ans, i].min
end
puts ans

n = gets.to_i

massiv = Array.new(n) {Array.new()}

for i in(0..n-1)
	k = gets.split("")
	length = k.length - 1

	for j in(0..length-1)
		massiv[i][j] = k[j].to_i
	end
end

p = 0
t = 0

for j in(0..length-1)
	for i in(0..n-2)
		if massiv[i][j] == massiv[i+1][j] then
			t+=1
		else
			break
		end
	end

	if t == 0 then
		break
	end

	if t == n-1 then
		p+=1
	end

	t = 0
end

puts p

n, numbers = gets.to_i, STDIN.read.split("\n")
prefix = numbers[0]

n.times do |i|
	m = numbers[i][0..prefix.length - 1]
	if prefix == m
		next
	else
		while prefix != m
			prefix.chop!
			m.chop!
		end
	end
end

print prefix.length

# t = gets.split.collect{|x| x.to_i}
n = gets.to_i

result = nil

n.times do |i|
	s = gets.strip
	result ||=s
	s = s.chars.to_a
	s.each_index do |i|
		if result[i] == nil or result[i] != s[i]
			if i == 0
				result = ""
			else
				result = result[0..i-1]
			end
			break
		end
	end
end

puts result.size


So, the Berland is at war with its eternal enemy Flatland again, and Vasya, an accountant, was assigned to fulfil his duty to the nation. Right now the situation in Berland is dismal — their both cities are surrounded! The armies of flatlanders stand on the borders of circles, the circles' centers are in the surrounded cities. At any moment all points of the flatland ring can begin to move quickly in the direction of the city — that's the strategy the flatlanders usually follow when they besiege cities.The berlanders are sure that they can repel the enemy's attack if they learn the exact time the attack starts. For that they need to construct a radar that would register any movement at the distance of at most r from it. Thus, we can install a radar at such point, that at least one point of the enemy ring will be in its detecting range (that is, at a distance of at most r). Then the radar can immediately inform about the enemy's attack. Due to the newest technologies, we can place a radar at any point without any problems. But the problem is that the berlanders have the time to make only one radar. Besides, the larger the detection radius (r) is, the more the radar costs.That's why Vasya's task (that is, your task) is to find the minimum possible detection radius for the radar. In other words, your task is to find the minimum radius r (r ≥ 0) such, that a radar with radius r can be installed at some point and it can register the start of the movements of both flatland rings from that point. In this problem you can consider the cities as material points, the attacking enemy rings - as circles with centers in the cities, the radar's detection range — as a disk (including the border) with the center at the point where the radar is placed.

The input files consist of two lines. Each line represents the city and the flatland ring that surrounds it as three space-separated integers xi, yi, ri (|xi|, |yi| ≤ 104; 1 ≤ ri ≤ 104) — the city's coordinates and the distance from the city to the flatlanders, correspondingly. It is guaranteed that the cities are located at different points.

Print a single real number — the minimum detection radius of the described radar. The answer is considered correct if the absolute or relative error does not exceed 10 - 6.

NoteThe figure below shows the answer to the first sample. In this sample the best decision is to put the radar at point with coordinates (2, 0).   The figure below shows the answer for the second sample. In this sample the best decision is to put the radar at point with coordinates (0, 0).

x1,y1,r1 = gets.split(" ").map(&:to_i)
x2,y2,r2 = gets.split(" ").map(&:to_i)

dist = Math.sqrt((x1-x2)**2 + (y1-y2)**2)
if (r1-r2).abs <= dist && dist <= (r1+r2)
	puts 0
else
	x = [dist,r1,r2].sort.reverse
	puts (x[0] - x[1] - x[2])/2
end


Find an n × n matrix with different numbers from 1 to n2, so the sum in each row, column and both main diagonals are odd.

The only line contains odd integer n (1 ≤ n ≤ 49).

Print n lines with n integers. All the integers should be different and from 1 to n2. The sum in each row, column and both main diagonals should be odd.


Recently, the students of School 179 have developed a unique algorithm, which takes in a binary string $$$s$$$ as input. However, they soon found out that if some substring $$$t$$$ of $$$s$$$ is a palindrome of length greater than 1, the algorithm will work incorrectly. Can the students somehow reorder the characters of $$$s$$$ so that the algorithm will work correctly on the string?A binary string is a string where each character is either 0 or 1.A string $$$a$$$ is a substring of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.A palindrome is a string that reads the same backwards as forwards.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$) — the length of the string $$$s$$$. The second line of each test case contains the string $$$s$$$ of length $$$n$$$ consisting only of the characters 0 and 1.

For each test case, print YES (case-insensitive) if it is possible to reorder the characters of $$$s$$$ so that there are no substrings that are a palindrome of length greater than 1, or NO (case-insensitive) otherwise.

NoteIn the first three test cases, the given strings do not contain palindromes of length greater than 1, so the answers are YES.In the last test case, it is impossible to reorder the characters so that the string does not contain palindromes of length greater than 1, so the answer is NO.

for _ in 0...(gets.chomp.to_i)
  len = gets.chomp.to_i
  string = gets.chomp
  if len >= 3
    puts "NO"
  elsif (string == "00") or (string == "11")
    puts "NO"
  else
    puts "YES"
  end
end

gets.to_i.times do
  n = gets.to_i
  s = gets.strip

  if n == 1 || s == "01" || s == "10"
    puts "YES"
  else
    puts "NO"
  end
end

for _ in 0...(gets.chomp.to_i)
  len = gets.chomp.to_i
  string = gets.chomp
  if (len == 1) or (len == 2)
    puts "YES"
  else
    if string.count("0") == string.count("1")
      puts "NO"
    else
      puts "YES"
    end
  end
end


Arkady invited Anna for a dinner to a sushi restaurant. The restaurant is a bit unusual: it offers $$$n$$$ pieces of sushi aligned in a row, and a customer has to choose a continuous subsegment of these sushi to buy.The pieces of sushi are of two types: either with tuna or with eel. Let's denote the type of the $$$i$$$-th from the left sushi as $$$t_i$$$, where $$$t_i = 1$$$ means it is with tuna, and $$$t_i = 2$$$ means it is with eel.Arkady does not like tuna, Anna does not like eel. Arkady wants to choose such a continuous subsegment of sushi that it has equal number of sushi of each type and each half of the subsegment has only sushi of one type. For example, subsegment $$$[2, 2, 2, 1, 1, 1]$$$ is valid, but subsegment $$$[1, 2, 1, 2, 1, 2]$$$ is not, because both halves contain both types of sushi.Find the length of the longest continuous subsegment of sushi Arkady can buy.

The first line contains a single integer $$$n$$$ ($$$2 \le n \le 100\,000$$$) — the number of pieces of sushi. The second line contains $$$n$$$ integers $$$t_1$$$, $$$t_2$$$, ..., $$$t_n$$$ ($$$t_i = 1$$$, denoting a sushi with tuna or $$$t_i = 2$$$, denoting a sushi with eel), representing the types of sushi from left to right. It is guaranteed that there is at least one piece of sushi of each type. Note that it means that there is at least one valid continuous segment.

Print a single integer — the maximum length of a valid continuous segment.

NoteIn the first example Arkady can choose the subsegment $$$[2, 2, 1, 1]$$$ or the subsegment $$$[1, 1, 2, 2]$$$ with length $$$4$$$.In the second example there is no way but to choose one of the subsegments $$$[2, 1]$$$ or $$$[1, 2]$$$ with length $$$2$$$.In the third example Arkady's best choice is the subsegment $$$[1, 1, 1, 2, 2, 2]$$$.

n = $stdin.gets.chomp.to_i

list = []
input = $stdin.gets.chomp.split(' ')
#puts input
pre = input[0]
#puts "pre #{pre}"
tmp_cnt = 1
(1..n).each do |i|
	tmp = input[i]
	#puts "tmp #{pre}"
	if tmp==pre
		tmp_cnt+=1
	else
		list.push(tmp_cnt)
		tmp_cnt=1
		pre = tmp
	end
end

#puts list
mx=-1
ln = list.length
(0..ln-2).each do |i|
	k = list[i] < list[i+1] ? list[i] : list[i+1]
	#puts "---#{k}"
	if k>mx
		mx = k
	end
end

puts (mx*2)


You have a permutation: an array $$$a = [a_1, a_2, \ldots, a_n]$$$ of distinct integers from $$$1$$$ to $$$n$$$. The length of the permutation $$$n$$$ is odd.Consider the following algorithm of sorting the permutation in increasing order.A helper procedure of the algorithm, $$$f(i)$$$, takes a single argument $$$i$$$ ($$$1 \le i \le n-1$$$) and does the following. If $$$a_i > a_{i+1}$$$, the values of $$$a_i$$$ and $$$a_{i+1}$$$ are exchanged. Otherwise, the permutation doesn't change.The algorithm consists of iterations, numbered with consecutive integers starting with $$$1$$$. On the $$$i$$$-th iteration, the algorithm does the following:   if $$$i$$$ is odd, call $$$f(1), f(3), \ldots, f(n - 2)$$$;  if $$$i$$$ is even, call $$$f(2), f(4), \ldots, f(n - 1)$$$. It can be proven that after a finite number of iterations the permutation will be sorted in increasing order.After how many iterations will this happen for the first time?

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 999$$$; $$$n$$$ is odd) — the length of the permutation. The second line contains $$$n$$$ distinct integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le n$$$) — the permutation itself.  It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$999$$$.

For each test case print the number of iterations after which the permutation will become sorted in increasing order for the first time. If the given permutation is already sorted, print $$$0$$$.

NoteIn the first test case, the permutation will be changing as follows:   after the $$$1$$$-st iteration: $$$[2, 3, 1]$$$;  after the $$$2$$$-nd iteration: $$$[2, 1, 3]$$$;  after the $$$3$$$-rd iteration: $$$[1, 2, 3]$$$. In the second test case, the permutation will be changing as follows:   after the $$$1$$$-st iteration: $$$[4, 5, 1, 7, 2, 3, 6]$$$;  after the $$$2$$$-nd iteration: $$$[4, 1, 5, 2, 7, 3, 6]$$$;  after the $$$3$$$-rd iteration: $$$[1, 4, 2, 5, 3, 7, 6]$$$;  after the $$$4$$$-th iteration: $$$[1, 2, 4, 3, 5, 6, 7]$$$;  after the $$$5$$$-th iteration: $$$[1, 2, 3, 4, 5, 6, 7]$$$. In the third test case, the permutation is already sorted and the answer is $$$0$$$.

def solve
  n = gets.to_i
  n -= 1
  a = gets.split(' ').map { |s| s.to_i}
  it, i, tmp, res = 0, 0, 0, 0
  f = 0
  while f < 2
    res += 1
    i = it
    it = (it + 1) % 2
    f += 1
    while i < n
      if a[i] > a[i + 1]
        tmp = a[i]
        a[i] = a[i + 1]
        a[i + 1] = tmp
        f = 0
      end
      i += 2
    end
    #puts "a : #{a}"
  end
  res -= 2
  puts res
end

t = gets.to_i
for z in 0...t
  solve
end

t = gets.to_i
t.times do

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

cnt = 0
while a != b
  (cnt & 1).step(n - 2, 2) do |i|
    if a[i] > a[i+1]
      a[i], a[i+1] = a[i+1], a[i]
    end
  end
  cnt += 1
end

p cnt

end


Mike decided to teach programming to children in an elementary school. He knows that it is not an easy task to interest children in that age to code. That is why he decided to give each child two sweets.Mike has $$$n$$$ sweets with sizes $$$a_1, a_2, \ldots, a_n$$$. All his sweets have different sizes. That is, there is no such pair $$$(i, j)$$$ ($$$1 \leq i, j \leq n$$$) such that $$$i \ne j$$$ and $$$a_i = a_j$$$.Since Mike has taught for many years, he knows that if he gives two sweets with sizes $$$a_i$$$ and $$$a_j$$$ to one child and $$$a_k$$$ and $$$a_p$$$ to another, where $$$(a_i + a_j) \neq (a_k + a_p)$$$, then a child who has a smaller sum of sizes will be upset. That is, if there are two children who have different sums of sweets, then one of them will be upset. Apparently, Mike does not want somebody to be upset. Mike wants to invite children giving each of them two sweets. Obviously, he can't give one sweet to two or more children. His goal is to invite as many children as he can. Since Mike is busy preparing to his first lecture in the elementary school, he is asking you to find the maximum number of children he can invite giving each of them two sweets in such way that nobody will be upset.

The first line contains one integer $$$n$$$ ($$$2 \leq n \leq 1\,000$$$) — the number of sweets Mike has. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the sizes of the sweets. It is guaranteed that all integers are distinct.

Print one integer — the maximum number of children Mike can invite giving each of them two sweets in such way that nobody will be upset.

NoteIn the first example, Mike can give $$$9+2=11$$$ to one child, $$$8+3=11$$$ to another one, and $$$7+4=11$$$ to the third child. Therefore, Mike can invite three children. Note that it is not the only solution.In the second example, Mike can give $$$3+9=12$$$ to one child and $$$1+11$$$ to another one. Therefore, Mike can invite two children. Note that it is not the only solution.

n = gets.chomp.to_i
w = Array.new
l = gets.chomp.split
l.each do |i|
   w << i.to_i
end
h = Hash.new(0)
(0..n-2).each do |i|
    ((i+1)..n-1).each do |j|
        l = w[i] + w[j]
        if h[l] == 0
            h[l] = 1
        else
            a = h[l] + 1
            h[l] = a
        end
    end
end
w = -1
h.each do |key, value|
    if value > w
        w = value
    end
end
puts w

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

h = {}
(0..n-2).each do |i|
  (i+1..n-1).each do |j|
     if h[a[i]+a[j]]
	h[a[i]+a[j]] = h[a[i]+a[j]] + 1
     else
	h[a[i]+a[j]] = 1
     end
  end
end

puts h.to_a.map{|e| e[1]}.max


There are $$$n$$$ piles of sand where the $$$i$$$-th pile has $$$a_i$$$ blocks of sand. The $$$i$$$-th pile is called too tall if $$$1 < i < n$$$ and $$$a_i > a_{i-1} + a_{i+1}$$$. That is, a pile is too tall if it has more sand than its two neighbours combined. (Note that piles on the ends of the array cannot be too tall.)You are given an integer $$$k$$$. An operation consists of picking $$$k$$$ consecutive piles of sand and adding one unit of sand to them all. Formally, pick $$$1 \leq l,r \leq n$$$ such that $$$r-l+1=k$$$. Then for all $$$l \leq i \leq r$$$, update $$$a_i \gets a_i+1$$$.What is the maximum number of piles that can simultaneously be too tall after some (possibly zero) operations?

The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$3 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq k \leq n$$$) — the number of piles of sand and the size of the operation, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the sizes of the piles. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, output a single integer — the maximum number of piles that are simultaneously too tall after some (possibly zero) operations.

NoteIn the first test case, we can perform the following three operations:   Add one unit of sand to piles $$$1$$$ and $$$2$$$: $$$[\color{red}{3}, \color{red}{10}, 2, 4, 1]$$$.  Add one unit of sand to piles $$$4$$$ and $$$5$$$: $$$[3, 10, 2, \color{red}{5}, \color{red}{2}]$$$.  Add one unit of sand to piles $$$3$$$ and $$$4$$$: $$$[3, 10, \color{red}{3}, \color{red}{6}, 2]$$$.  Now piles $$$2$$$ and $$$4$$$ are too tall, so in this case the answer is $$$2$$$. It can be shown that it is impossible to make more than $$$2$$$ piles too tall.In the second test case, any operation will increase all piles by $$$1$$$ unit, so the number of too tall piles will always be $$$0$$$.In the third test case, we can increase any pile by $$$1$$$ unit of sand. It can be shown that the maximum number of too tall piles is $$$1$$$.

# gets.to_i
# gets.split.map(&:to_i)

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

  if k != 1
    i = 1
    ans = 0
    while i < n-1
      if a[i-1]+a[i+1] < a[i]
        ans += 1
      end
      i += 1
    end
  else
    ans = (n-1)/2
  end
  puts ans
end

# gets.to_i
# gets.split.map(&:to_i)

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

  if k == 1
    i = 1
    ans = 0
    while i < n-1
      if a[i-1]+a[i+1] < a[i]
        ans += 1
      end
      i += 1
    end
  else
    ans = (n-1)/2
  end
  puts ans
end

# gets.to_i
# gets.split.map(&:to_i)

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

  if k == n
    i = 1
    ans = 0
    while i < n-1
      if a[i-1]+a[i+1] < a[i]
        ans += 1
      end
      i += 1
    end
  else
    ans = (n-1)/2
  end
  puts ans
end

# gets.to_i
# gets.split.map(&:to_i)

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

  ans = 0
  i = 1
  while i < n - 1
    if a[i-1]+a[i+1] < a[i]
      ans += 1
      next i += 2
    end
    i += 1
  end
  1.upto(n-2) do |i|
  end

  puts ans
end

# gets.to_i
# gets.split.map(&:to_i)

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

  ans = 0
  1.upto(n-2) do |i|
    if a[i-1]+a[i+1] < a[i]
      ans += 1
    end
  end

  puts ans
end


Alice and Bob are playing chess on a huge chessboard with dimensions $$$n \times n$$$. Alice has a single piece left — a queen, located at $$$(a_x, a_y)$$$, while Bob has only the king standing at $$$(b_x, b_y)$$$. Alice thinks that as her queen is dominating the chessboard, victory is hers. But Bob has made a devious plan to seize the victory for himself — he needs to march his king to $$$(c_x, c_y)$$$ in order to claim the victory for himself. As Alice is distracted by her sense of superiority, she no longer moves any pieces around, and it is only Bob who makes any turns.Bob will win if he can move his king from $$$(b_x, b_y)$$$ to $$$(c_x, c_y)$$$ without ever getting in check. Remember that a king can move to any of the $$$8$$$ adjacent squares. A king is in check if it is on the same rank (i.e. row), file (i.e. column), or diagonal as the enemy queen. Find whether Bob can win or not.

The first line contains a single integer $$$n$$$ ($$$3 \leq n \leq 1000$$$) — the dimensions of the chessboard. The second line contains two integers $$$a_x$$$ and $$$a_y$$$ ($$$1 \leq a_x, a_y \leq n$$$) — the coordinates of Alice's queen. The third line contains two integers $$$b_x$$$ and $$$b_y$$$ ($$$1 \leq b_x, b_y \leq n$$$) — the coordinates of Bob's king. The fourth line contains two integers $$$c_x$$$ and $$$c_y$$$ ($$$1 \leq c_x, c_y \leq n$$$) — the coordinates of the location that Bob wants to get to. It is guaranteed that Bob's king is currently not in check and the target location is not in check either. Furthermore, the king is not located on the same square as the queen (i.e. $$$a_x \neq b_x$$$ or $$$a_y \neq b_y$$$), and the target does coincide neither with the queen's position (i.e. $$$c_x \neq a_x$$$ or $$$c_y \neq a_y$$$) nor with the king's position (i.e. $$$c_x \neq b_x$$$ or $$$c_y \neq b_y$$$).

Print "YES" (without quotes) if Bob can get from $$$(b_x, b_y)$$$ to $$$(c_x, c_y)$$$ without ever getting in check, otherwise print "NO". You can print each letter in any case (upper or lower).

NoteIn the diagrams below, the squares controlled by the black queen are marked red, and the target square is marked blue.In the first case, the king can move, for instance, via the squares $$$(2, 3)$$$ and $$$(3, 2)$$$. Note that the direct route through $$$(2, 2)$$$ goes through check.  In the second case, the queen watches the fourth rank, and the king has no means of crossing it.  In the third case, the queen watches the third file.

n = gets.to_i
s = gets
ax, ay = s.split
s = gets
bx, by = s.split
s = gets
cx, cy = s.split
#puts ax == 12
if bx.to_i < ax.to_i && ax.to_i < cx.to_i
	puts "NO"
elsif bx.to_i > ax.to_i && ax.to_i > cx.to_i
	puts "NO"
elsif by.to_i < ay.to_i && ay.to_i < cy.to_i
	puts "NO"
elsif by.to_i > ay.to_i && ay.to_i > cy.to_i
	#puts "heheudh"
	puts "NO"
else
	puts "YES"
end

n = gets.to_i
s = gets
ax, ay = s.split
s = gets
bx, by = s.split
s = gets
cx, cy = s.split
if bx < ax && ax < cx
	puts "NO"
elsif bx > ax && ax > cx
	puts "NO"
elsif by < ay && ay < cy
	puts "NO"
elsif by > ay && ay > cy
	puts "NO"
else
	puts "YES"
end