You are given $$$n$$$ chips on a number line. The $$$i$$$-th chip is placed at the integer coordinate $$$x_i$$$. Some chips can have equal coordinates.You can perform each of the two following types of moves any (possibly, zero) number of times on any chip:  Move the chip $$$i$$$ by $$$2$$$ to the left or $$$2$$$ to the right for free (i.e. replace the current coordinate $$$x_i$$$ with $$$x_i - 2$$$ or with $$$x_i + 2$$$);  move the chip $$$i$$$ by $$$1$$$ to the left or $$$1$$$ to the right and pay one coin for this move (i.e. replace the current coordinate $$$x_i$$$ with $$$x_i - 1$$$ or with $$$x_i + 1$$$). Note that it's allowed to move chips to any integer coordinate, including negative and zero.Your task is to find the minimum total number of coins required to move all $$$n$$$ chips to the same coordinate (i.e. all $$$x_i$$$ should be equal after some sequence of moves).

The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the number of chips. The second line of the input contains $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$), where $$$x_i$$$ is the coordinate of the $$$i$$$-th chip.

Print one integer — the minimum total number of coins required to move all $$$n$$$ chips to the same coordinate.

NoteIn the first example you need to move the first chip by $$$2$$$ to the right and the second chip by $$$1$$$ to the right or move the third chip by $$$2$$$ to the left and the second chip by $$$1$$$ to the left so the answer is $$$1$$$.In the second example you need to move two chips with coordinate $$$3$$$ by $$$1$$$ to the left so the answer is $$$2$$$.

n = gets.to_i
a = gets.split.map(&:to_i)
puts [a.count(&:odd?), a.count(&:even?)].min

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

arr.map{ |x| x % 2 == 0 ? even += 1 : odd += 1 }

even < odd ? puts(even) : puts(odd)

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

odd_c = xs.count { |x| x.odd? }
even_c = n - odd_c

puts [odd_c, even_c].min

n = gets.to_i
nums = gets.split.map(&:to_i)
even = nums.select(&:even?).length
v = [even, n - even]
puts v.min

n = gets.to_i
a = gets.split.map{|i|i.to_i%2}.count(0)
puts [a,n-a].min

n = gets.strip.to_i
chips = gets.strip.split(' ').map(&:to_i)
cnt = chips.group_by { |x| x % 2 }
puts [(cnt[0] || []).size, (cnt[1] || []).size].min

n = gets.to_i
a = gets.split.map(&:to_i)
cnt_odd = 0
a.each do |x|
  cnt_odd += 1 if x % 2 == 1
end
puts [n - cnt_odd, cnt_odd].min

N = gets.to_i
arr = gets.split.map(&:to_i)
arr.map! { |x| x % 2 }
zero = arr.count 0
one = arr.count 1
if zero < one
	puts zero
else
	puts one
end

nn = gets.to_i
xx = gets.split.map(&:to_i)

min0=0
min1=0

0.upto(nn-1) do |n|
  t = xx[n]
  min0+=1 if t%2==0
  min1+=1 if (t+1)%2==0
end

puts min0<min1 ? min0 : min1

n = gets.to_i
x = gets.split.map &:to_i
puts [x.select{|x| x%2 == 0}.size, x.select{|x| x%2 == 1}.size].min

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

puts [a.count(&:even?), a.count(&:odd?)].min

n=gets.to_i
a=gets.split(' ').map(&:to_i)
e=[0,0]
a.each {|x| e[x&1]+=1}
puts e.min

n = gets.chomp.to_i
xx = gets.split.map(&:to_i)
puts n/2

n = gets.to_i
#xx = gets.split.map(&:to_i)
puts n/2

n = gets.to_i
x = gets.split.map(&:to_i).sort
puts x[1..-1].inject(0) { |s, e| s + (e - x[0])%2 }


Lunar New Year is approaching, and Bob is planning to go for a famous restaurant — "Alice's".The restaurant "Alice's" serves $$$n$$$ kinds of food. The cost for the $$$i$$$-th kind is always $$$c_i$$$. Initially, the restaurant has enough ingredients for serving exactly $$$a_i$$$ dishes of the $$$i$$$-th kind. In the New Year's Eve, $$$m$$$ customers will visit Alice's one after another and the $$$j$$$-th customer will order $$$d_j$$$ dishes of the $$$t_j$$$-th kind of food. The $$$(i + 1)$$$-st customer will only come after the $$$i$$$-th customer is completely served.Suppose there are $$$r_i$$$ dishes of the $$$i$$$-th kind remaining (initially $$$r_i = a_i$$$). When a customer orders $$$1$$$ dish of the $$$i$$$-th kind, the following principles will be processed. If $$$r_i > 0$$$, the customer will be served exactly $$$1$$$ dish of the $$$i$$$-th kind. The cost for the dish is $$$c_i$$$. Meanwhile, $$$r_i$$$ will be reduced by $$$1$$$. Otherwise, the customer will be served $$$1$$$ dish of the cheapest available kind of food if there are any. If there are multiple cheapest kinds of food, the one with the smallest index among the cheapest will be served. The cost will be the cost for the dish served and the remain for the corresponding dish will be reduced by $$$1$$$. If there are no more dishes at all, the customer will leave angrily. Therefore, no matter how many dishes are served previously, the cost for the customer is $$$0$$$.If the customer doesn't leave after the $$$d_j$$$ dishes are served, the cost for the customer will be the sum of the cost for these $$$d_j$$$ dishes.Please determine the total cost for each of the $$$m$$$ customers.

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 10^5$$$), representing the number of different kinds of food and the number of customers, respectively. The second line contains $$$n$$$ positive integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^7$$$), where $$$a_i$$$ denotes the initial remain of the $$$i$$$-th kind of dishes. The third line contains $$$n$$$ positive integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 10^6$$$), where $$$c_i$$$ denotes the cost of one dish of the $$$i$$$-th kind. The following $$$m$$$ lines describe the orders of the $$$m$$$ customers respectively. The $$$j$$$-th line contains two positive integers $$$t_j$$$ and $$$d_j$$$ ($$$1 \leq t_j \leq n$$$, $$$1 \leq d_j \leq 10^7$$$), representing the kind of food and the number of dishes the $$$j$$$-th customer orders, respectively.

Print $$$m$$$ lines. In the $$$j$$$-th line print the cost for the $$$j$$$-th customer.

NoteIn the first sample, $$$5$$$ customers will be served as follows. Customer $$$1$$$ will be served $$$6$$$ dishes of the $$$2$$$-nd kind, $$$1$$$ dish of the $$$4$$$-th kind, and $$$1$$$ dish of the $$$6$$$-th kind. The cost is $$$6 \cdot 3 + 1 \cdot 2 + 1 \cdot 2 = 22$$$. The remain of the $$$8$$$ kinds of food will be $$$\{8, 0, 2, 0, 4, 4, 7, 5\}$$$. Customer $$$2$$$ will be served $$$4$$$ dishes of the $$$1$$$-st kind. The cost is $$$4 \cdot 6 = 24$$$. The remain will be $$$\{4, 0, 2, 0, 4, 4, 7, 5\}$$$. Customer $$$3$$$ will be served $$$4$$$ dishes of the $$$6$$$-th kind, $$$3$$$ dishes of the $$$8$$$-th kind. The cost is $$$4 \cdot 2 + 3 \cdot 2 = 14$$$. The remain will be $$$\{4, 0, 2, 0, 4, 0, 7, 2\}$$$. Customer $$$4$$$ will be served $$$2$$$ dishes of the $$$3$$$-rd kind, $$$2$$$ dishes of the $$$8$$$-th kind. The cost is $$$2 \cdot 3 + 2 \cdot 2 = 10$$$. The remain will be $$$\{4, 0, 0, 0, 4, 0, 7, 0\}$$$. Customer $$$5$$$ will be served $$$7$$$ dishes of the $$$7$$$-th kind, $$$3$$$ dishes of the $$$1$$$-st kind. The cost is $$$7 \cdot 3 + 3 \cdot 6 = 39$$$. The remain will be $$$\{1, 0, 0, 0, 4, 0, 0, 0\}$$$.In the second sample, each customer is served what they order except the last one, who leaves angrily without paying. For example, the second customer is served $$$6$$$ dishes of the second kind, so the cost is $$$66 \cdot 6 = 396$$$.In the third sample, some customers may not be served what they order. For example, the second customer is served $$$6$$$ dishes of the second kind, $$$6$$$ of the third and $$$1$$$ of the fourth, so the cost is $$$66 \cdot 6 + 666 \cdot 6 + 6666 \cdot 1 = 11058$$$.

DBG = !true
n,m = gets.split.map{|z| z.to_i}
a = gets.split.map{|z| z.to_i}
c = gets.split.map{|z| z.to_i}
td = []
for i in 0...m
  t,d = gets.split.map{|z| z.to_i}
  td << [t-1,d]
  #a,b = gets.split
  #a = a.to_i
  #b = b.to_i
end

if DBG
  puts "n #{n} m #{m}"
  print a
  puts ""
  print c
  puts ""
  print td
  puts ""
end

sortedc = []
for i in 0...n
  sortedc << [i,c[i]]
end
sortedc.sort_by!{|z| 200000*z[1]+z[0]}
print sortedc if DBG
cheapest = 0

#totalcost = 0
for j in 0...m
  if cheapest == n
    puts "0"
    next
  end
  cost = 0
  t = td[j][0]
  d = td[j][1]
  if a[t] > 0
    amt = [d, a[t]].min
    d -= amt
    a[t] -= amt
    cost += c[t] * amt
  end
  while d > 0
    while  cheapest < n && a[sortedc[cheapest][0]] == 0
      cheapest += 1
    end
    if cheapest == n
      cost = 0
      break
    end
    t = sortedc[cheapest][0]
    amt = [d, a[t]].min
    d -= amt
    a[t] -= amt
    cost += c[t] * amt
  end
  if DBG
    print a
    puts ""
  end
  puts "#{cost}"
  #totalcost += cost
end

n,m=gets.split.map(&:to_i)
$stdout.sync = true
a=gets.split.map(&:to_i)
c=gets.split.map(&:to_i)
M=[]
n.times{|i|M<<i}
M.sort_by!{|e|c[e]}
s=0
m.times{
	t,d=gets.split.map(&:to_i)
	q=[a[t-1], d].min
	r=c[t-1]*q
	a[t-1]-=q
	d-=q
	i=s
	while d>0&&i<n do
		k=M[i]
		q=[a[k],d].min
		d-=q
		a[k]-=q
		r+=c[k]*q
		s+=1 if a[k]==0
		i+=1
	end
	print "#{r}\n" if d==0
	print "0\n" if d>0
}

s = (gets.chomp).split(' ').map(&:to_i)
n = s[0]
m = s[1]
t = Array.new(n)
ost = (gets.chomp).split(' ').map(&:to_i)
cost = (gets.chomp).split(' ').map(&:to_i)
(0..n-1).each do |i|
    t[i] = [cost[i], i]
end
t.sort_by!{ |x| x[0] }
pos = 0
m.times do |p|
    s = (gets.chomp).split(' ').map(&:to_i)
    a = s[0]
    b = s[1]
    ans = 0
    ans += cost[a-1] * [b, ost[a-1]].min()
    ost[a-1] -= b
    if ost[a-1] <= 0
        temp = ost[a-1].abs()
        ost[a-1] = 0

        while temp > 0 do
            if pos == n && temp > 0
                ans = 0
                break
            end
            tt = temp
            temp -= [ost[t[pos][1]], tt].min()
            ans += t[pos][0] * [ost[t[pos][1]], tt].min()
            ost[t[pos][1]] -= [ost[t[pos][1]], tt].min()
            if ost[t[pos][1]] == 0
                pos += 1
            end
        end
    end
    puts ans
end

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

nm = gets.chomp.split(" ").map(&:to_i)
n = nm[0]
m = nm[1]
amount = gets.chomp.split(" ").map(&:to_i)
cost = gets.chomp.split(" ").map(&:to_i)
orders = []
m.times do
	orders.push gets.chomp.split(" ").map(&:to_i)
end

costin = []

(0..n-1).each do |i|
	costin.push [cost[i],i]
end

costin.sort_by!{|x| [x[0],x[1]]}

cheapestindex = 0

orders.each do |ord|
	type = ord[0]-1
	num = ord[1]

	totalcost = 0

	if cheapestindex==n
		puts totalcost
		next
	end

	left = amount[type]

	if(num<=left)
		amount[type]-=num
		totalcost+=num*cost[type]
		num=0
	else
		totalcost+=left*cost[type]
		amount[type]=0
		num-=left
		while(num>0)
			nextdishind = costin[cheapestindex][1]
			left = amount[nextdishind]

			if(left<1)
				cheapestindex+=1
				if cheapestindex==n
					break
				end
				nextdishind = costin[cheapestindex][1]
				left = amount[nextdishind]
			end

			if(num<=left)
				amount[nextdishind]-=num
				totalcost+=num*cost[nextdishind]
				num=0
			else
				totalcost+=left*cost[nextdishind]
				amount[nextdishind]=0
				num-=left
			end
		end
	end

#	p amount

	if num>0
		puts 0
	else
		puts totalcost
	end
end

n,m=gets.split.map(&:to_i)
$stdout.sync = true
a=gets.split.map(&:to_i)
c=gets.split.map(&:to_i)
M=[]
n.times{|i|M<<i}
M.sort_by!{|e|c[e]}
s=0
m.times{
	t,d=gets.split.map(&:to_i)
	q=[a[t-1], d].min
	r=c[t-1]*q
	a[t-1]-=q
	d-=q
	i=s
	while d>0&&i<n do
		k=M[i]
		q=[a[k],d].min
		d-=q
		a[k]-=q
		r+=c[k]*q
		s+=1 if a[k]==0
		i+=1
	end
	print "#{d}\n" if d==0
	print "0\n" if d>0
}

n,m=gets.split.map(&:to_i)
$stdout.sync = true
a=gets.split.map(&:to_i)
c=gets.split.map(&:to_i)
M=[]
n.times{|i|M<<i}
M.sort_by!{|e|c[e]}
s=0
m.times{
	t,d=gets.split.map(&:to_i)
	q=[a[t-1], d].min
	r=c[t-1]*q
	a[t-1]-=q
	d-=q
	i=s
	while d>0&&i<n do
		k=M[i]
		q=[a[k],d].min
		d-=q
		a[k]-=q
		r+=c[k]*q
		s+=1 if a[k]==0
		i+=1
	end
	print "#{d}\n" if d>0
	print "0\n" if d==0
}


Alice guesses the strings that Bob made for her.At first, Bob came up with the secret string $$$a$$$ consisting of lowercase English letters. The string $$$a$$$ has a length of $$$2$$$ or more characters. Then, from string $$$a$$$ he builds a new string $$$b$$$ and offers Alice the string $$$b$$$ so that she can guess the string $$$a$$$.Bob builds $$$b$$$ from $$$a$$$ as follows: he writes all the substrings of length $$$2$$$ of the string $$$a$$$ in the order from left to right, and then joins them in the same order into the string $$$b$$$.For example, if Bob came up with the string $$$a$$$="abac", then all the substrings of length $$$2$$$ of the string $$$a$$$ are: "ab", "ba", "ac". Therefore, the string $$$b$$$="abbaac".You are given the string $$$b$$$. Help Alice to guess the string $$$a$$$ that Bob came up with. It is guaranteed that $$$b$$$ was built according to the algorithm given above. It can be proved that the answer to the problem is unique.

The first line contains a single positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the test. Then $$$t$$$ test cases follow. Each test case consists of one line in which the string $$$b$$$ is written, consisting of lowercase English letters ($$$2 \le |b| \le 100$$$) — the string Bob came up with, where $$$|b|$$$ is the length of the string $$$b$$$. It is guaranteed that $$$b$$$ was built according to the algorithm given above.

Output $$$t$$$ answers to test cases. Each answer is the secret string $$$a$$$, consisting of lowercase English letters, that Bob came up with.

NoteThe first test case is explained in the statement.In the second test case, Bob came up with the string $$$a$$$="ac", the string $$$a$$$ has a length $$$2$$$, so the string $$$b$$$ is equal to the string $$$a$$$.In the third test case, Bob came up with the string $$$a$$$="bcdaf", substrings of length $$$2$$$ of string $$$a$$$ are: "bc", "cd", "da", "af", so the string $$$b$$$="bccddaaf".

gets.to_i.times do
	s = gets.chomp
	n = s.size
	puts (0...n / 2).map { s[_1 * 2] }.join + s[-1]
end

n = gets.to_i
n.times do
	b = gets.chomp
	m = b.size
	a = b[0]
	(b.size/2-1).times do |i|
		a += b[1+i*2]
	end
	a += b[-1]
	puts a
end

gets.chomp.to_i.times do

    str = gets.chomp
    i=1
    a=str[0]
    while i < str.length-1
        a+= str[i] if(i%2!=1)
        i+=1
    end

    puts a+str[-1]
end

t = gets.to_i
t.times do
	b = gets.chomp
	a = b[0]
	for i in 1...b.length
		a += b[i] if i%2 == 1
	end
	puts a
end

n = gets.chomp.to_i
(0...n).each do |i|
    arr = gets.chomp.split("")
    arsize = arr.size
    print arr[0]
    (1...arsize).each do |j|
        if j % 2 == 0 && arr[j] == arr[j-1]
            next
        else
            print arr[j]
        end
    end
    print "\n"
end

data_lines = gets.chomp
result = []
(1..data_lines.to_i).each do
	b = gets.chomp
	if b.length < 4
		result.push b
		next
	end
	a = b[0]
	(1..(b.length-2)).step(2).each do |index|
		a += b[index]
	end
	a += b[b.length-1]
	result.push a
end
result.each do |res|
	puts res
end

def solve(str)
	return str if str.size < 3

	compact_mid = str[1..-2].chars.each_slice(2).map(&:first).join

	"%s%s%s" % [str[0], compact_mid,str[-1]]
end

inputs = ARGF.each_line.drop(1).map(&:strip).to_a

inputs.each do |i|
	puts solve(i)
end

n = gets.chomp.to_i
n.times do
	s = gets.chomp
	if s.length == 2
		puts s
		next
	end
	print s[0]
	i = 1
	while i < s.length - 1
		print s[i]
		i += 2
	end
	puts s[-1]
end

n=gets.chomp.to_i
n.times do
  inpstr = gets.chomp.split('')
  result = inpstr[0]
  (1..inpstr.size).step(2).each do |index|
    result << inpstr[index]
  end
  puts result
end

gets.to_i.times do
  s = gets.chomp
  puts (0.step(s.length, 2).to_a + [s.length - 1]).map { |i| s[i] }.join
end

n=gets.chomp.to_i
n.times do
  inpstr = gets.chomp.split('')
  result = inpstr[0]
  (1..inpstr.size).step(2).each do |index|
    result << inpstr[index]
  end
  p result
end


You are given a chessboard of size $$$n \times n$$$. It is filled with numbers from $$$1$$$ to $$$n^2$$$ in the following way: the first $$$\lceil \frac{n^2}{2} \rceil$$$ numbers from $$$1$$$ to $$$\lceil \frac{n^2}{2} \rceil$$$ are written in the cells with even sum of coordinates from left to right from top to bottom. The rest $$$n^2 - \lceil \frac{n^2}{2} \rceil$$$ numbers from $$$\lceil \frac{n^2}{2} \rceil + 1$$$ to $$$n^2$$$ are written in the cells with odd sum of coordinates from left to right from top to bottom. The operation $$$\lceil\frac{x}{y}\rceil$$$ means division $$$x$$$ by $$$y$$$ rounded up.For example, the left board on the following picture is the chessboard which is given for $$$n=4$$$ and the right board is the chessboard which is given for $$$n=5$$$.  You are given $$$q$$$ queries. The $$$i$$$-th query is described as a pair $$$x_i, y_i$$$. The answer to the $$$i$$$-th query is the number written in the cell $$$x_i, y_i$$$ ($$$x_i$$$ is the row, $$$y_i$$$ is the column). Rows and columns are numbered from $$$1$$$ to $$$n$$$.

The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 \le n \le 10^9$$$, $$$1 \le q \le 10^5$$$) — the size of the board and the number of queries. The next $$$q$$$ lines contain two integers each. The $$$i$$$-th line contains two integers $$$x_i, y_i$$$ ($$$1 \le x_i, y_i \le n$$$) — description of the $$$i$$$-th query.

For each query from $$$1$$$ to $$$q$$$ print the answer to this query. The answer to the $$$i$$$-th query is the number written in the cell $$$x_i, y_i$$$ ($$$x_i$$$ is the row, $$$y_i$$$ is the column). Rows and columns are numbered from $$$1$$$ to $$$n$$$. Queries are numbered from $$$1$$$ to $$$q$$$ in order of the input.

NoteAnswers to the queries from examples are on the board in the picture from the problem statement.

N, Q = gets.split.map(&:to_i)
Q.times do
    x, y = gets.split.map(&:to_i)
    ans = 0
    if (x+y)%2 == 0
        ans += (x-1)/2 * N
        ans += (N+1)/2 if x%2==0
        ans += (y+1)/2
    else
        ans += (N*N+1)/2
        ans += (x-1)/2 * N
        ans += N/2 if x%2==0
        ans += (y+1)/2
    end
    puts ans
end


Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School.In his favorite math class, the teacher taught him the following interesting definitions.A parenthesis sequence is a string, containing only characters "(" and ")".A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition.We define that $$$|s|$$$ as the length of string $$$s$$$. A strict prefix $$$s[1\dots l]$$$ $$$(1\leq l< |s|)$$$ of a string $$$s = s_1s_2\dots s_{|s|}$$$ is string $$$s_1s_2\dots s_l$$$. Note that the empty string and the whole string are not strict prefixes of any string by the definition.Having learned these definitions, he comes up with a new problem. He writes down a string $$$s$$$ containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in $$$s$$$ independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence.After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable.

The first line contains a single integer $$$|s|$$$ ($$$1\leq |s|\leq 3 \cdot 10^5$$$), the length of the string. The second line contains a string $$$s$$$, containing only "(", ")" and "?".

A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes).

NoteIt can be proved that there is no solution for the second sample, so print ":(".

n = gets.chomp.to_i
s = gets.chomp
a = 0
b = 0
n.times do |i|
  if s[i] == "("
    a += 1
  elsif s[i] == ")"
    b += 1
  end
end
q = n - (a + b)
c = a - b
if s[-1] == "(" || (q - c).odd?
  puts ":("
else
  y = (q + c) / 2
  x = y - c
  ans = []
  sum = 0
  n.times do |i|
    if s[i] == "?"
      if x != 0
        ans << "("
        x -= 1
        sum += 1
      else
        ans << ")"
        sum -= 1
      end
    elsif s[i] == "("
      sum += 1
      ans << s[i]
    else
      sum -= 1
      ans << s[i]
    end
    if sum == 0 && i < n - 1
      puts ":("
      exit
    end
  end
  if sum == 0
    puts ans.join("")
  else
    puts ":("
  end
end

n = gets.chomp.to_i
s = gets.chomp
a = 0
b = 0
n.times do |i|
  if s[i] == "("
    a += 1
  elsif s[i] == ")"
    b += 1
  end
end
q = n - (a + b)
c = a - b
if s[-1] == "(" || (q - c).odd?
  puts ":("
else
  y = (q + c) / 2
  x = y - c
  ans = []
  sum = 0
  n.times do |i|
    if s[i] == "?"
      if x != 0
        ans << "("
        x -= 1
        sum += 1
      else
        ans << ")"
        sum -= 1
      end
    elsif s[i] == "("
      sum += 1
      ans << s[i]
    else
      sum -= 1
      ans << s[i]
    end
    if sum == 0 && i < n - 1
      puts ":("
      exit
    end
  end
  puts ans.join("")
end


You have a sequence $$$a$$$ with $$$n$$$ elements $$$1, 2, 3, \dots, k - 1, k, k - 1, k - 2, \dots, k - (n - k)$$$ ($$$k \le n < 2k$$$).Let's call as inversion in $$$a$$$ a pair of indices $$$i < j$$$ such that $$$a[i] > a[j]$$$.Suppose, you have some permutation $$$p$$$ of size $$$k$$$ and you build a sequence $$$b$$$ of size $$$n$$$ in the following manner: $$$b[i] = p[a[i]]$$$.Your goal is to find such permutation $$$p$$$ that the total number of inversions in $$$b$$$ doesn't exceed the total number of inversions in $$$a$$$, and $$$b$$$ is lexicographically maximum.Small reminder: the sequence of $$$k$$$ integers is called a permutation if it contains all integers from $$$1$$$ to $$$k$$$ exactly once.Another small reminder: a sequence $$$s$$$ is lexicographically smaller than another sequence $$$t$$$, if either $$$s$$$ is a prefix of $$$t$$$, or for the first $$$i$$$ such that $$$s_i \ne t_i$$$, $$$s_i < t_i$$$ holds (in the first position that these sequences are different, $$$s$$$ has smaller number than $$$t$$$).

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$k \le n < 2k$$$; $$$1 \le k \le 10^5$$$) — the length of the sequence $$$a$$$ and its maximum. It's guaranteed that the total sum of $$$k$$$ over test cases doesn't exceed $$$10^5$$$.

For each test case, print $$$k$$$ integers — the permutation $$$p$$$ which maximizes $$$b$$$ lexicographically without increasing the total number of inversions. It can be proven that $$$p$$$ exists and is unique.

NoteIn the first test case, the sequence $$$a = [1]$$$, there is only one permutation $$$p = [1]$$$.In the second test case, the sequence $$$a = [1, 2]$$$. There is no inversion in $$$a$$$, so there is only one permutation $$$p = [1, 2]$$$ which doesn't increase the number of inversions.In the third test case, $$$a = [1, 2, 1]$$$ and has $$$1$$$ inversion. If we use $$$p = [2, 1]$$$, then $$$b = [p[a[1]], p[a[2]], p[a[3]]] = [2, 1, 2]$$$ and also has $$$1$$$ inversion.In the fourth test case, $$$a = [1, 2, 3, 2]$$$, and since $$$p = [1, 3, 2]$$$ then $$$b = [1, 3, 2, 3]$$$. Both $$$a$$$ and $$$b$$$ have $$$1$$$ inversion and $$$b$$$ is the lexicographically maximum.

gets.to_i.times do
  n, k = gets.split.map &:to_i
  (1...2 * k - n).each do |i|
    print i, " "
  end
  (2 * k - n..k).reverse_each do |i|
    print i, " "
  end
  puts
end

def inv(arr)
  i1 = i2 = 0
  res = 0
  while i1 < arr.size - 1
    while i2 < arr.size - 1
      i2 += 1
      res += 1 if arr[i2] < arr[i1]
    end
    i1 += 1
    i2 = i1
  end
  res
end

t = gets.chomp.to_i
t.times do
  n, k = gets.chomp.split(' ').map(&:to_i)
  max_n = k * 2 - 1
  # puts "max_n = #{max_n.to_s}"
  diff = max_n - n
  # puts "DIFF = #{diff.to_s}"
  arr = (1..(diff)).to_a
  # puts "ARR = #{arr.to_s}"
  arr << k
  # puts "ARR = #{arr.to_s}"
  while arr.size < k
    arr << arr[-1] - 1
  end
  # puts "ARR = #{arr.to_s}"
  puts arr.join(' ')
end

gets.to_i.times do
  n,k=gets.split.map(&:to_i)
  l=[*1...2*k-n]+[*2*k-n..k].reverse
  puts l.join(" ")
end


There are three sticks with integer lengths $$$l_1, l_2$$$ and $$$l_3$$$.You are asked to break exactly one of them into two pieces in such a way that:   both pieces have positive (strictly greater than $$$0$$$) integer length;  the total length of the pieces is equal to the original length of the stick;  it's possible to construct a rectangle from the resulting four sticks such that each stick is used as exactly one of its sides. A square is also considered a rectangle.Determine if it's possible to do that.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The only line of each testcase contains three integers $$$l_1, l_2, l_3$$$ ($$$1 \le l_i \le 10^8$$$) — the lengths of the sticks.

For each testcase, print "YES" if it's possible to break one of the sticks into two pieces with positive integer length in such a way that it's possible to construct a rectangle from the resulting four sticks. Otherwise, print "NO". You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as a positive answer).

NoteIn the first testcase, the first stick can be broken into parts of length $$$1$$$ and $$$5$$$. We can construct a rectangle with opposite sides of length $$$1$$$ and $$$5$$$.In the second testcase, breaking the stick of length $$$2$$$ can only result in sticks of lengths $$$1, 1, 2, 5$$$, which can't be made into a rectangle. Breaking the stick of length $$$5$$$ can produce results $$$2, 3$$$ or $$$1, 4$$$ but neither of them can't be put into a rectangle.In the third testcase, the second stick can be broken into parts of length $$$2$$$ and $$$2$$$. The resulting rectangle has opposite sides $$$2$$$ and $$$2$$$ (which is a square).In the fourth testcase, the third stick can be broken into parts of length $$$2$$$ and $$$2$$$. The resulting rectangle has opposite sides $$$2$$$ and $$$5$$$.

T = gets.to_i
T.times do
    l = gets.split.map(&:to_i)
    l.sort!
    if (l[0] + l[1] == l[2]) or (l[0] == l[1] and l[2].even?) or (l[1] == l[2] and l[0].even?)
        puts "YES"
    else
        puts "NO"
    end
end


There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can reach any city from any other by the roads.Theon and Yara Greyjoy are on a horse in the first city, they are starting traveling through the roads. But the weather is foggy, so they can’t see where the horse brings them. When the horse reaches a city (including the first one), it goes to one of the cities connected to the current city. But it is a strange horse, it only goes to cities in which they weren't before. In each such city, the horse goes with equal probabilities and it stops when there are no such cities. Let the length of each road be 1. The journey starts in the city 1. What is the expected length (expected value of length) of their journey? You can read about expected (average) value by the link https://en.wikipedia.org/wiki/Expected_value.

The first line contains a single integer n (1 ≤ n ≤ 100000) — number of cities. Then n - 1 lines follow. The i-th line of these lines contains two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — the cities connected by the i-th road. It is guaranteed that one can reach any city from any other by the roads.

Print a number — the expected length of their journey. The journey starts in the city 1. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .

NoteIn the first sample, their journey may end in cities 3 or 4 with equal probability. The distance to city 3 is 1 and to city 4 is 2, so the expected length is 1.5.In the second sample, their journey may end in city 4 or 5. The distance to the both cities is 2, so the expected length is 2.

N = gets.to_i
G = Array.new(N) { [] }
(N - 1).times do
  u, v = gets.split.map &:to_i
  u, v = u - 1, v - 1
  G[u] << v
  G[v] << u
end

def dfs u, fa, d, prob, leng, ans
=begin
  chc = G[u].size
  chc -= 1 if fa != -1
  G[u].each do |v|
    next if v == fa
    prob[v] = prob[u] / chc
    leng[v] = leng[u] + 1
    dfs v, u, d + 1, prob, leng, ans
  end
  ans[0] += prob[u] * leng[u] if chc == 0
=end
  fun = [[u, -1]]
  until fun.empty?
    u, fa = fun.shift
    chc = G[u].size
    chc -= 1 if fa != -1
    G[u].each do |v|
      next if v == fa
      prob[v] = prob[u] /chc
      leng[v] = leng[u] + 1
      fun.unshift [v, u]
    end
    ans[0] += prob[u] * leng[u] if chc == 0
  end
end

prob = [1.0] * N
leng = [0] * N
ans = [0.0]

dfs 0, -1, 0, prob, leng, ans

p ans[0]


You are given a string $$$s$$$. You need to find two non-empty strings $$$a$$$ and $$$b$$$ such that the following conditions are satisfied:  Strings $$$a$$$ and $$$b$$$ are both subsequences of $$$s$$$.  For each index $$$i$$$, character $$$s_i$$$ of string $$$s$$$ must belong to exactly one of strings $$$a$$$ or $$$b$$$.  String $$$a$$$ is lexicographically minimum possible; string $$$b$$$ may be any possible string. Given string $$$s$$$, print any valid $$$a$$$ and $$$b$$$.Reminder:A string $$$a$$$ ($$$b$$$) is a subsequence of a string $$$s$$$ if $$$a$$$ ($$$b$$$) can be obtained from $$$s$$$ by deletion of several (possibly, zero) elements. For example, "dores", "cf", and "for" are subsequences of "codeforces", while "decor" and "fork" are not.A string $$$x$$$ is lexicographically smaller than a string $$$y$$$ if and only if one of the following holds:  $$$x$$$ is a prefix of $$$y$$$, but $$$x \ne y$$$;  in the first position where $$$x$$$ and $$$y$$$ differ, the string $$$x$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$y$$$.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains one string $$$s$$$ ($$$2 \le |s| \le 100$$$ where $$$|s|$$$ means the length of $$$s$$$). String $$$s$$$ consists of lowercase Latin letters.

For each test case, print the strings $$$a$$$ and $$$b$$$ that satisfy the given conditions. If there are multiple answers, print any.

NoteIn the first test case, there are only two choices: either $$$a =$$$ f and $$$b = $$$ c or $$$a = $$$ c and $$$b = $$$ f. And $$$a = $$$c is lexicographically smaller than $$$a = $$$ f.In the second test case, a is the only character in the string.In the third test case, it can be proven that b is the lexicographically smallest subsequence of $$$s$$$. The second string can be of two variants; one of them is given here.

STDIN.read
  .split("\n")
  .drop(1)
  .each do |line|
    c = line.split("").sort.first
    puts c + " " + line.sub(c, "")
  end


Polycarp has a poor memory. Each day he can remember no more than $$$3$$$ of different letters. Polycarp wants to write a non-empty string of $$$s$$$ consisting of lowercase Latin letters, taking minimum number of days. In how many days will he be able to do it?Polycarp initially has an empty string and can only add characters to the end of that string.For example, if Polycarp wants to write the string lollipops, he will do it in $$$2$$$ days:   on the first day Polycarp will memorize the letters l, o, i and write lolli;  On the second day Polycarp will remember the letters p, o, s, add pops to the resulting line and get the line lollipops. If Polycarp wants to write the string stringology, he will do it in $$$4$$$ days:   in the first day will be written part str;  on day two will be written part ing;  on the third day, part of olog will be written;  on the fourth day, part of y will be written. For a given string $$$s$$$, print the minimum number of days it will take Polycarp to write it.

The first line of input data contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of a non-empty string $$$s$$$ consisting of lowercase Latin letters (the length of the string $$$s$$$ does not exceed $$$2 \cdot 10^5$$$) — the string Polycarp wants to construct. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, print a single number — minimum number of days it will take Polycarp to write the string $$$s$$$ from memory.

6

lollipops

stringology

abracadabra

codeforces

test

f

#input the number of test cases
t = gets.chomp.to_i

#list of results
results = []

#solve
for i in 1..t
    str = gets.chomp

    #solving code
	result = 0
    letterAlreadyToday = []
    for j in str.split ""
		if letterAlreadyToday.include? j
			next
		else
			if letterAlreadyToday.length == 3
				letterAlreadyToday = []
			end
		end
		if letterAlreadyToday.length < 1
			result += 1
		end
		letterAlreadyToday.push j
    end
	results.push result
end

puts results

require "set"

n = gets.to_i

n.times do
  s = gets.chomp

  set = Set.new
  days = 1
  i = 0

  while (i < s.size)
    char = s[i]
    if !set.include?(char) && set.size == 3
      set = Set.new
      days += 1
    else
      set.add(char)
      i += 1
    end
  end

  p days
end

t = gets.to_i
t.times {
  s = gets.chomp
  d = 1
  chrs = []
  while !s.empty?
    c = s[0]
    s = s[1..-1]
    if !(chrs.index c)
      if chrs.size == 3
        d += 1
        chrs = []
      end
      chrs += [c]
    end
  end
  puts d
}


Being tired of participating in too many Codeforces rounds, Gildong decided to take some rest in a park. He sat down on a bench, and soon he found two rabbits hopping around. One of the rabbits was taller than the other.He noticed that the two rabbits were hopping towards each other. The positions of the two rabbits can be represented as integer coordinates on a horizontal line. The taller rabbit is currently on position $$$x$$$, and the shorter rabbit is currently on position $$$y$$$ ($$$x \lt y$$$). Every second, each rabbit hops to another position. The taller rabbit hops to the positive direction by $$$a$$$, and the shorter rabbit hops to the negative direction by $$$b$$$.  For example, let's say $$$x=0$$$, $$$y=10$$$, $$$a=2$$$, and $$$b=3$$$. At the $$$1$$$-st second, each rabbit will be at position $$$2$$$ and $$$7$$$. At the $$$2$$$-nd second, both rabbits will be at position $$$4$$$.Gildong is now wondering: Will the two rabbits be at the same position at the same moment? If so, how long will it take? Let's find a moment in time (in seconds) after which the rabbits will be at the same point.

Each test contains one or more test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Each test case contains exactly one line. The line consists of four integers $$$x$$$, $$$y$$$, $$$a$$$, $$$b$$$ ($$$0 \le x \lt y \le 10^9$$$, $$$1 \le a,b \le 10^9$$$) — the current position of the taller rabbit, the current position of the shorter rabbit, the hopping distance of the taller rabbit, and the hopping distance of the shorter rabbit, respectively.

For each test case, print the single integer: number of seconds the two rabbits will take to be at the same position. If the two rabbits will never be at the same position simultaneously, print $$$-1$$$.

NoteThe first case is explained in the description.In the second case, each rabbit will be at position $$$3$$$ and $$$7$$$ respectively at the $$$1$$$-st second. But in the $$$2$$$-nd second they will be at $$$6$$$ and $$$4$$$ respectively, and we can see that they will never be at the same position since the distance between the two rabbits will only increase afterward.

num = gets.to_i
while num > 0
    myArray = gets.chomp.split().map { |e| e.to_i  }
    x = myArray[0]
    y = myArray[1]
    a = myArray[2]
    b = myArray[3]
    result = (y-x)/(a+b)
    if (y-x) % (a+b) == 0
        puts result
    else
        puts '-1'
    end
    num = num -1
end

def binary_search(x, y, a, b)
  first = 0
  last = (y - x)/[a,b].max

  while first <= last
    i = (first + last) / 2

    xx = x + i*a
    yy = y - i*b

    if xx == yy
      return i
    elsif xx > yy
      last = i - 1
    elsif yy > xx
      first = i + 1
    else
      return -1
    end
  end
  -1
end

n = gets.strip.to_i
results = []

n.times do
  arr = gets.strip.split.map(&:to_i)
  x, y, a, b = *arr
  results << binary_search(x, y, a, b)
end

n.times do |x|
  puts results[x]
end

class Rabbit

    def initialize(pos, jump_size)
        @pos = pos
        @jump_size = jump_size
    end

    def jump_size()
       @jump_size
    end

    def position()
       @pos
    end

end

def read_input()

   input = STDIN.gets()

   input = input.split(' ').map(&:to_i)

   raise "Invalid input" if input.size != 4

   return Rabbit.new(input[0], input[2]), Rabbit.new(input[1], input[3])

end

def calc_rabbits_jumps()

    rabbit1, rabbit2 = read_input()

    distance = rabbit2.position - rabbit1.position
    speed = rabbit1.jump_size + rabbit2.jump_size
    point = distance / speed.to_f

    if point % 1 == 0
        puts point.to_i
    else
        puts -1
    end

end

cnt = Integer(STDIN.gets())

cnt.times {
    calc_rabbits_jumps()
}

# frozen_string_literal: true

@data = $stdin.read.split

def next_token
  @data.shift
end

def next_int
  next_token.to_i
end

t = next_int

t.times do |_i|
  x = next_int
  y = next_int
  a = next_int
  b = next_int

  if (y - x) % (a + b) == 0
    t = (y - x) / (a + b)
    puts t
  else
    puts -1
  end
end

t = gets.to_i
z = []
(0..t - 1).each { |i|
  str = gets
  z[i] = str
  counter = 0
  bloc = z[i].split(' ')
  a = bloc[0].to_i; b = bloc[1].to_i; c = bloc[2].to_i; d = bloc[3].to_i
  if (b - a) % (c + d) != 0
    puts '-1'
  else
    puts (b - a) / (c + d)
  end
}

n = gets.to_i
n.times {
  x, y, a, b = gets.split.map(&:to_i)
  if (y-x) % (a + b) > 0
    puts "-1"
  else
    puts (y-x)/(a+b)
  end
}

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

  delta = y-x

  puts delta.modulo(a+b).zero? ? delta.div(a+b) : -1
end


NIT, the cleaver, is new in town! Thousands of people line up to orz him. To keep his orzers entertained, NIT decided to let them solve the following problem related to $$$\operatorname{or} z$$$. Can you solve this problem too?You are given a 1-indexed array of $$$n$$$ integers, $$$a$$$, and an integer $$$z$$$. You can do the following operation any number (possibly zero) of times:  Select a positive integer $$$i$$$ such that $$$1\le i\le n$$$. Then, simutaneously set $$$a_i$$$ to $$$(a_i\operatorname{or} z)$$$ and set $$$z$$$ to $$$(a_i\operatorname{and} z)$$$. In other words, let $$$x$$$ and $$$y$$$ respectively be the current values of $$$a_i$$$ and $$$z$$$. Then set $$$a_i$$$ to $$$(x\operatorname{or}y)$$$ and set $$$z$$$ to $$$(x\operatorname{and}y)$$$. Here $$$\operatorname{or}$$$ and $$$\operatorname{and}$$$ denote the bitwise operations OR and AND respectively.Find the maximum possible value of the maximum value in $$$a$$$ after any number (possibly zero) of operations.

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 two integers $$$n$$$ and $$$z$$$ ($$$1\le n\le 2000$$$, $$$0\le z<2^{30}$$$). The second line of each test case contains $$$n$$$ integers $$$a_1$$$,$$$a_2$$$,$$$\ldots$$$,$$$a_n$$$ ($$$0\le a_i<2^{30}$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.

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

NoteIn the first test case of the sample, one optimal sequence of operations is:  Do the operation with $$$i=1$$$. Now $$$a_1$$$ becomes $$$(3\operatorname{or}3)=3$$$ and $$$z$$$ becomes $$$(3\operatorname{and}3)=3$$$.  Do the operation with $$$i=2$$$. Now $$$a_2$$$ becomes $$$(4\operatorname{or}3)=7$$$ and $$$z$$$ becomes $$$(4\operatorname{and}3)=0$$$.  Do the operation with $$$i=1$$$. Now $$$a_1$$$ becomes $$$(3\operatorname{or}0)=3$$$ and $$$z$$$ becomes $$$(3\operatorname{and}0)=0$$$. After these operations, the sequence $$$a$$$ becomes $$$[3,7]$$$, and the maximum value in it is $$$7$$$. We can prove that the maximum value in $$$a$$$ can never exceed $$$7$$$, so the answer is $$$7$$$.In the fourth test case of the sample, one optimal sequence of operations is:  Do the operation with $$$i=1$$$. Now $$$a_1$$$ becomes $$$(7\operatorname{or}7)=7$$$ and $$$z$$$ becomes $$$(7\operatorname{and}7)=7$$$.  Do the operation with $$$i=3$$$. Now $$$a_3$$$ becomes $$$(30\operatorname{or}7)=31$$$ and $$$z$$$ becomes $$$(30\operatorname{and}7)=6$$$.  Do the operation with $$$i=5$$$. Now $$$a_5$$$ becomes $$$(27\operatorname{or}6)=31$$$ and $$$z$$$ becomes $$$(27\operatorname{and}6)=2$$$.

gets.to_i.times do
  n, z = gets.split.map(&:to_i)
  as = gets.split.map(&:to_i)
  p as.map { |a| a | z }.max
end

gets.to_i.times do
  n, z = gets.split.map(&:to_i)
  xs = gets.split.map(&:to_i)
  p xs.map { |x| x | z }.max
end

gets.to_i.times do
    n, z = gets.split.map(&:to_i)
    p gets.split.map(&:to_i).map{|x| x | z}.max
end

gets.to_i.times do
    n, z = gets.split.map(&:to_i)
    ans = 0
    gets.split.map(&:to_i).each do |x|
        ans = [ans, x | z].max
    end
    puts ans
end

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

gets.to_i.times do
  n, z = gets.split.map(&:to_i)
  a = gets.split.map(&:to_i).sort.reverse

  num = 0
  ai = -1
  zi = 0
  n.times do |i|
    if (a[i] & z) > ai
      ai = a[i] | z
      zi = a[i] & z
      num = i
    end
  end

  ans = ai
  n.times do |i|
    next if i == num
    ai = a[i] | z
    zi &= a[i]
    ans = [ans, ai].max
  end
  puts ans
end


A remote island chain contains n islands, labeled 1 through n. Bidirectional bridges connect the islands to form a simple cycle — a bridge connects islands 1 and 2, islands 2 and 3, and so on, and additionally a bridge connects islands n and 1. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal.The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: First, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal.Determine if it is possible for the islanders to arrange the statues in the desired order.

The first line contains a single integer n (2 ≤ n ≤ 200 000) — the total number of islands. The second line contains n space-separated integers ai (0 ≤ ai ≤ n - 1) — the statue currently placed on the i-th island. If ai = 0, then the island has no statue. It is guaranteed that the ai are distinct. The third line contains n space-separated integers bi (0 ≤ bi ≤ n - 1) — the desired statues of the ith island. Once again, bi = 0 indicates the island desires no statue. It is guaranteed that the bi are distinct.

Print "YES" (without quotes) if the rearrangement can be done in the existing network, and "NO" otherwise.

NoteIn the first sample, the islanders can first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3.In the second sample, the islanders can simply move statue 1 from island 1 to island 2.In the third sample, no sequence of movements results in the desired position.

islands = gets.chomp.to_i
array = gets.chomp.split(' ').map {|x| x.to_i }
result = gets.chomp.split(' ').map {|x| x.to_i }

a = array.dup
b = result.dup
a.delete(0)
b.delete(0)

a << a.dup
a.flatten!

for i in 0..a.length-1
  break if a.length - i< b.length
  yay = true
  for j in 0..b.length-1
    if a[i+j] != b[j]
      yay = false
      break
    end
  end
  if yay
    puts "YES"
    exit 0
  end
end

puts "NO"


Kolya got an integer array $$$a_1, a_2, \dots, a_n$$$. The array can contain both positive and negative integers, but Kolya doesn't like $$$0$$$, so the array doesn't contain any zeros.Kolya doesn't like that the sum of some subsegments of his array can be $$$0$$$. The subsegment is some consecutive segment of elements of the array. You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $$$0$$$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $$$0$$$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $$$0$$$.

The first line of the input contains one integer $$$n$$$ ($$$2 \le n \le 200\,000$$$) — the number of elements in Kolya's array. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^{9} \le a_i \le 10^{9}, a_i \neq 0$$$) — the description of Kolya's array.

Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $$$0$$$.

NoteConsider the first example. There is only one subsegment with the sum $$$0$$$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $$$1$$$ between second and third elements of the array.There are no subsegments having sum $$$0$$$ in the second example so you don't need to do anything.

n = gets.chomp.to_i
arr = gets.chomp.split(' ').map(&:to_i)
s = {0 => 1}
cur = 0
res = 0
arr.each do |el|
  cur += el
  if s[cur] == 1
    s = {0 => 1}
    res += 1
    cur = el
  end
  s[cur] = 1
end
puts res

n = gets.chomp.to_i
arr = gets.chomp.split(' ').map(&:to_i)
s = {}
s[0] = 1 if arr.sum == 0
cur = 0
arr.each do |el|
  cur += el
  s[cur] = 0 if s[cur].nil?
  s[cur] += 1
end
res = 0
s.each do |key, val|
  res += val - 1
end
puts res


You are given two strings $$$s$$$ and $$$t$$$, both consisting only of lowercase Latin letters.The substring $$$s[l..r]$$$ is the string which is obtained by taking characters $$$s_l, s_{l + 1}, \dots, s_r$$$ without changing the order.Each of the occurrences of string $$$a$$$ in a string $$$b$$$ is a position $$$i$$$ ($$$1 \le i \le |b| - |a| + 1$$$) such that $$$b[i..i + |a| - 1] = a$$$ ($$$|a|$$$ is the length of string $$$a$$$).You are asked $$$q$$$ queries: for the $$$i$$$-th query you are required to calculate the number of occurrences of string $$$t$$$ in a substring $$$s[l_i..r_i]$$$.

The first line contains three integer numbers $$$n$$$, $$$m$$$ and $$$q$$$ ($$$1 \le n, m \le 10^3$$$, $$$1 \le q \le 10^5$$$) — the length of string $$$s$$$, the length of string $$$t$$$ and the number of queries, respectively. The second line is a string $$$s$$$ ($$$|s| = n$$$), consisting only of lowercase Latin letters. The third line is a string $$$t$$$ ($$$|t| = m$$$), consisting only of lowercase Latin letters. Each of the next $$$q$$$ lines contains two integer numbers $$$l_i$$$ and $$$r_i$$$ ($$$1 \le l_i \le r_i \le n$$$) — the arguments for the $$$i$$$-th query.

Print $$$q$$$ lines — the $$$i$$$-th line should contain the answer to the $$$i$$$-th query, that is the number of occurrences of string $$$t$$$ in a substring $$$s[l_i..r_i]$$$.

NoteIn the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.

def single_input
	gets.chomp.to_i
end

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

def string_input
	gets.chomp
end

n, m, q = multiple_input
s = string_input
t = string_input

ac = Array.new(n+m, 0)

point = 0
(0+m-1).upto(n-1) do |i|
	flag = true
	m.times do |j|
		if s[i-m+1+j] != t[j]
			flag = false
		end
	end

	if flag
		ac[i] += 1
	end
	# p "#{s[i]} #{t[point]}, #{point}"

	if i != 0
		ac[i] += ac[i-1]
	end
end

# p ac
q.times do |_|
	x, y = multiple_input
	if x+m-3 > y-1
		puts 0
		# puts "CASE1"
	elsif x+m-3 >= 0 && x+m-3 < n
		puts ac[y-1] - ac[x+m-3]
		# puts "CASE2 #{y-1} #{x+m-3} #{ac[y-1]} #{ac[x+m-3]}"
	else
		puts ac[y-1]
		# puts "CASE3"
	end
end

N, M, Q = gets.split.map(&:to_i)
S = gets.chomp
T = gets.chomp
num = Array.new(N+1, 0)
(N-M+1).times do |i|
    num[i] = S[i, M] == T ? 1 : 0
end
(N-1).times do |i|
    num[i+1] += num[i]
end

Q.times do |i|
    l, r = gets.split.map(&:to_i)
    l -= 1
    r -= 1
    puts r-l+1 >= M ? num[r-M+1] - num[l-1] : 0
end

N, M, Q = gets.split.map(&:to_i)
S = gets.chomp
T = gets.chomp
num = Array.new(N+1, 0)
(N-M+1).times do |i|
    if S[i, M] == T
        num[i] = 1
    else
        num[i] = 0
    end
end
(N-1).times do |i|
    num[i+1] += num[i]
end

Q.times do |i|
    l, r = gets.split.map(&:to_i)
    l -= 1
    r -= 1
    ans = r >= M-1 ? num[r-M+1] - num[l-1] : 0
    puts ans > 0 ? ans : 0
end

nmq = gets.chomp.split(" ").map(&:to_i)
n = nmq[0]
m = nmq[1]
q = nmq[2]
str = gets.chomp
min = gets.chomp

qarr = []
q.times {qarr.push gets.chomp.split(" ").map(&:to_i)}

def count_em(string, substring)
 # count = 0
  ans = []
  biglen = string.length
  smallen = substring.length
  lookfor = substring[0]
  index= 0
  flag = 0
  (0..biglen-smallen+1).each do |i|
    next if i<index
    if string[i] == lookfor
      flag = 0
      isit = true
      (1..smallen-1).each do |j|
        char = substring[j]
        if flag==0 and char==lookfor
          index = i+j
          flag = 1
        end
        if string[i+j] != char
          isit = false
          break
        end
      end

      if isit
        ans.push i
      end
    end
  end
  return ans
end

arr = count_em(str,min)
len = min.length

#p arr
newarr = []
sum = 0
n.times do |i|
  sum += 1 if arr.include?(i)
  newarr.push sum
end
#p arr
#p newarr

sum = 0
qarr.each do |qu|
  s = qu[0]-1
  e = qu[1]-1
  #puts e-m+1
  #puts s
  flag = false
  if s == 0
    x = 0
    flag = true
  else
    x = s-1
  end

  if e-m+1<0 or e-m+1<(s-1)
    puts 0
    next
  end
  if flag
    puts newarr[e-m+1]
  else
    puts newarr[e-m+1]-newarr[x]
  end
  #puts
#  puts arr.count{|x| x>=s and x<=e-len+1}
end

def single_input
	gets.chomp.to_i
end

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

def string_input
	gets.chomp
end

n, m, q = multiple_input
s = string_input
t = string_input

ac = Array.new(n, 0)

point = 0
n.times do |i|

	# p "#{s[i]} #{t[point]}, #{point}"
	if s[i] == t[point]
		point += 1
	else
		point = 0
	end

	if point == m
		ac[i] += 1
		point = 0
	end

	if i != 0
		ac[i] += ac[i-1]
	end
end

# p ac
q.times do |_|
	x, y = multiple_input
	if x+m-3 >= 0 && x+m-3 < n
		puts ac[y-1] - ac[x+m-3]
	else
		puts ac[y-1]
	end
end

def single_input
	gets.chomp.to_i
end

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

def string_input
	gets.chomp
end

n, m, q = multiple_input
s = string_input
t = string_input

ac = Array.new(n, 0)

point = 0
n.times do |i|

	# p "#{s[i]} #{t[point]}, #{point}"
	if s[i] == t[point]
		point += 1
	else
		point = 0
	end

	if point == m
		ac[i] += 1
		point = 0
	end

	if i != 0
		ac[i] += ac[i-1]
	end
end

# p ac
q.times do |_|
	x, y = multiple_input
	if x+m-3 > y-1
		puts 0
		# puts "CASE1"
	elsif x+m-3 >= 0 && x+m-3 < n
		puts ac[y-1] - ac[x+m-3]
		# puts "CASE2 #{y-1} #{x+m-3} #{ac[y-1]} #{ac[x+m-3]}"
	else
		puts ac[y-1]
		# puts "CASE3"
	end
end

def single_input
	gets.chomp.to_i
end

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

def string_input
	gets.chomp
end

n, m, q = multiple_input
s = string_input
t = string_input

ac = Array.new(n, 0)

point = 0
n.times do |i|

	# p "#{s[i]} #{t[point]}, #{point}"
	if s[i] == t[point]
		point += 1
	else
		point = 0
	end

	if point == m
		ac[i] += 1
		point = 0
	end

	if i != 0
		ac[i] += ac[i-1]
	end
end

# p ac
q.times do |_|
	x, y = multiple_input
	if x-1 >= 0
		puts ac[y-1] - ac[x-1]
	else
		puts ac[y-1]
	end
end

N, M, Q = gets.split.map(&:to_i)
S = gets.chomp
T = gets.chomp
num = Array.new(N+1, 0)
(N-M+1).times do |i|
    if S[i, M] == T
        num[i] = 1
    else
        num[i] = 0
    end
end
(N-1).times do |i|
    num[i+1] += num[i]
end

Q.times do |i|
    l, r = gets.split.map(&:to_i)
    l -= 1
    r -= 1
    puts r >= M-1 ? num[r-M+1] - num[l-1] : 0
end

N, M, Q = gets.split.map(&:to_i)
S = gets.chomp
T = gets.chomp
num = Array.new(N+1, 0)
(N-M+1).times do |i|
    num[i] = S[i, M] == T ? 1 : 0
end
(N-1).times do |i|
    num[i+1] += num[i]
end

Q.times do |i|
    l, r = gets.split.map(&:to_i)
    l -= 1
    r -= 1
    ans = r-l+1 >= M ? num[r-M+1] - num[l-1] : 0
end

N, M, Q = gets.split.map(&:to_i)
S = gets.chomp
T = gets.chomp
num = Array.new(N+1, 0)
(N-M+1).times do |i|
    if S[i, M] == T
        num[i] = 1
    else
        num[i] = 0
    end
end
(N-1).times do |i|
    num[i+1] += num[i]
end

Q.times do |i|
    l, r = gets.split.map(&:to_i)
    l -= 1
    r -= 1
    puts num[r-M+1] - num[l-1]
end

nmq = gets.chomp.split(" ").map(&:to_i)
n = nmq[0]
m = nmq[1]
q = nmq[2]
str = gets.chomp
min = gets.chomp

qarr = []
q.times {qarr.push gets.chomp.split(" ").map(&:to_i)}

def count_em(string, substring)
 # count = 0
  ans = []
  biglen = string.length
  smallen = substring.length
  lookfor = substring[0]
  index= 0
  flag = 0
  (0..biglen-smallen+1).each do |i|
    next if i<index
    if string[i] == lookfor
      flag = 0
      isit = true
      (1..smallen-1).each do |j|
        char = substring[j]
        if flag==0 and char==lookfor
          index = i+j
          flag = 1
        end
        if string[i+j] != char
          isit = false
          break
        end
      end

      if isit
        ans.push i
      end
    end
  end
  return ans
end

arr = count_em(str,min)
len = min.length

#p arr
newarr = []
sum = 0
n.times do |i|
  sum += 1 if arr.include?(i-m+1)
  newarr.push sum
end
#p arr
#p newarr

sum = 0
qarr.each do |qu|
  s = qu[0]-1
  e = qu[1]-1
  puts newarr[e]-newarr[s]
#  puts arr.count{|x| x>=s and x<=e-len+1}
end

nmq = gets.chomp.split(" ").map(&:to_i)
n = nmq[0]
m = nmq[1]
q = nmq[2]
str = gets.chomp
min = gets.chomp

qarr = []
q.times {qarr.push gets.chomp.split(" ").map(&:to_i)}

def count_em(string, substring)
 # count = 0
  ans = []
  biglen = string.length
  smallen = substring.length
  lookfor = substring[0]
  index= 0
  flag = 0
  (0..biglen-smallen+1).each do |i|
    next if i<index
    if string[i] == lookfor
      flag = 0
      isit = true
      (1..smallen-1).each do |j|
        char = substring[j]
        if flag==0 and char==lookfor
          index = i+j
          flag = 1
        end
        if string[i+j] != char
          isit = false
          break
        end
      end

      if isit
        ans.push i
      end
    end
  end
  return ans
end

arr = count_em(str,min)
len = min.length

#p arr
newarr = []
sum = 0
n.times do |i|
  sum += 1 if arr.include?(i)
  newarr.push sum
end
#p arr
#p newarr

sum = 0
qarr.each do |qu|
  s = qu[0]-1
  e = qu[1]-1
  #puts e-m+1
  #puts s
  flag = false
  if s == 0
    x = 0
    flag = true
  else
    x = s-1
  end

  if e-m+1<0
    puts 0
    next
  end
  if flag
    puts newarr[e-m+1]
  else
    puts newarr[e-m+1]-newarr[x]
  end
  #puts
#  puts arr.count{|x| x>=s and x<=e-len+1}
end

nmq = gets.chomp.split(" ").map(&:to_i)
q = nmq[2]
str = gets.chomp
min = gets.chomp

qarr = []
q.times {qarr.push gets.chomp.split(" ").map(&:to_i)}

def count_em(string, substring)
 # count = 0
  ans = []
  biglen = string.length
  smallen = substring.length
  lookfor = substring[0]
  index= 0
  (0..biglen-smallen+1).each do |i|
    next if i<index

    if string[i] == lookfor
      isit = true
      (1..smallen-1).each do |j|
        char = substring[j]
        if char==lookfor
          index = i+j
        end
        if string[i+j] != char
          isit = false
          break
        end
      end

      if isit
        ans.push i
      end
    end
  end
  return ans
end

arr = count_em(str,min)
len = min.length
#p arr
qarr.each do |qu|
  s = qu[0]-1
  e = qu[1]-1
  puts arr.count{|x| x>=s and x<=e-len+1}
end

nmq = gets.chomp.split(" ").map(&:to_i)
n = nmq[0]
m = nmq[1]
q = nmq[2]
str = gets.chomp
min = gets.chomp

qarr = []
q.times {qarr.push gets.chomp.split(" ").map(&:to_i)}

def count_em(string, substring)
 # count = 0
  ans = []
  biglen = string.length
  smallen = substring.length
  lookfor = substring[0]
  index= 0
  flag = 0
  (0..biglen-smallen+1).each do |i|
    next if i<index
    if string[i] == lookfor
      flag = 0
      isit = true
      (1..smallen-1).each do |j|
        char = substring[j]
        if flag==0 and char==lookfor
          index = i+j
          flag = 1
        end
        if string[i+j] != char
          isit = false
          break
        end
      end

      if isit
        ans.push i
      end
    end
  end
  return ans
end

arr = count_em(str,min)
len = min.length

#p arr
newarr = []
sum = 0
n.times do |i|
  sum += 1 if arr.include?(i)
  newarr.push sum
end
#p arr
#p newarr

sum = 0
qarr.each do |qu|
  s = qu[0]-1
  e = qu[1]-1
  #puts e-m+1
  #puts s
  if s == 0
    x = 0
  else
    x = s-1
  end

  if e-m+1<0
    puts 0
    next
  end

  puts newarr[e-m+1]-newarr[x]
  #puts
#  puts arr.count{|x| x>=s and x<=e-len+1}
end


You are given three integers $$$n$$$, $$$l$$$, and $$$r$$$. You need to construct an array $$$a_1,a_2,\dots,a_n$$$ ($$$l\le a_i\le r$$$) such that $$$\gcd(i,a_i)$$$ are all distinct or report there's no solution.Here $$$\gcd(x, y)$$$ denotes the greatest common divisor (GCD) of integers $$$x$$$ and $$$y$$$.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1\le t\le 10^4$$$) — the number of test cases. The description of the test cases follows. The first line contains three integers $$$n$$$, $$$l$$$, $$$r$$$ ($$$1 \le n \le 10^5$$$, $$$1\le l\le r\le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case, if there is no solution, print "NO" (without quotes). You can print letters in any case (upper or lower). Otherwise, print "YES" (without quotes). In the next line, print $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ — the array you construct. If there are multiple solutions, you may output any.

class Solution
  def initialize(n, l, r)
    @n = n
    @l = l
    @r = r
    @arr = []
    solve
    print
  end

  def solve
    # unique gcd(i, a_i) means that a_i must be a multiple of i
    # -> try to find a_i s.t. a|a_i && l <= a_i <= r, i.e. smallest multiple of i > l
    # -> [(l - 1)/i + 1] * i
    (1..@n).each do |i|
      a_i = ((@l - 1)/i + 1) * i
      if a_i <= @r
        @arr << a_i
      else
        break
      end
    end
  end

  def print
    if @arr.length == @n
      puts 'YES'
      puts @arr.join(' ')
    else
      puts 'NO'
    end
  end
end

t = gets.chomp.to_i
t.times do
  n, l, r = gets.chomp.split(' ').map(&:to_i)
  Solution.new(n, l, r)
end

gets.to_i.times do
  n, l, r = gets.split.map(&:to_i)
  array = (1..n).map do |i|
    ((l - 1) / i + 1) * i
  end
  if array.max <= r
    puts "YES"
    puts array.join(" ")
  else
    puts "NO"
  end
end

gets.to_i.times do
  n, l, r = gets.split.map(&:to_i)
  array = (1..n).map do |i|
    ((l - 1) / i) * i + i
  end
  if array.max <= r
    puts "YES"
    puts array.join(" ")
  else
    puts "NO"
  end
end


Recently n students from city S moved to city P to attend a programming camp.They moved there by train. In the evening, all students in the train decided that they want to drink some tea. Of course, no two people can use the same teapot simultaneously, so the students had to form a queue to get their tea.i-th student comes to the end of the queue at the beginning of li-th second. If there are multiple students coming to the queue in the same moment, then the student with greater index comes after the student with lesser index. Students in the queue behave as follows: if there is nobody in the queue before the student, then he uses the teapot for exactly one second and leaves the queue with his tea; otherwise the student waits for the people before him to get their tea. If at the beginning of ri-th second student i still cannot get his tea (there is someone before him in the queue), then he leaves the queue without getting any tea. For each student determine the second he will use the teapot and get his tea (if he actually gets it).

The first line contains one integer t — the number of test cases to solve (1 ≤ t ≤ 1000). Then t test cases follow. The first line of each test case contains one integer n (1 ≤ n ≤ 1000) — the number of students. Then n lines follow. Each line contains two integer li, ri (1 ≤ li ≤ ri ≤ 5000) — the second i-th student comes to the end of the queue, and the second he leaves the queue if he still cannot get his tea. It is guaranteed that for every  condition li - 1 ≤ li holds. The sum of n over all test cases doesn't exceed 1000. Note that in hacks you have to set t = 1.

For each test case print n integers. i-th of them must be equal to the second when i-th student gets his tea, or 0 if he leaves without tea.

NoteThe example contains 2 tests:  During 1-st second, students 1 and 2 come to the queue, and student 1 gets his tea. Student 2 gets his tea during 2-nd second.  During 1-st second, students 1 and 2 come to the queue, student 1 gets his tea, and student 2 leaves without tea. During 2-nd second, student 3 comes and gets his tea.

inp = gets.chomp.to_i
otv = []
gg = []
inp.times{ |w|
ppl = gets.chomp.to_i

map = []

ppl.times{ |line|

user = gets.chomp.split(" ")
map.push([user[0].to_i,user[1].to_i])

}

#todo

ctime = 0
curentuser = 0

loop do
if curentuser>=map.size then break; end
ctime+=1

if ctime <= map[curentuser][1]

if ctime >= map[curentuser][0]

otv.push(ctime)
curentuser+=1
else
#wait
end

else
otv.push(0)
curentuser+=1
ctime-=1
end

end

gg.push(otv.join(" "))
otv = []

}

gg.size.times{|w|
puts gg[w]
}

def solve m
  ans = []
  cur = 1
  m.times do
    l, r = gets.chomp.split.map(&:to_i)
    if cur > r
      ans << 0
    elsif cur <= l
      ans << l
      cur = l + 1
    else
      ans << cur
      cur += 1
    end
  end
  puts ans.join(' ')
end

q = gets.to_i
q.times do
  m = gets.to_i
  solve m
end

def solve m
  ans = []
  cur = 1
  m.times do
    l, r = gets.chomp.split.map(&:to_i)
    if cur >= r
      ans << 0
    elsif cur <= l
      ans << l
      cur = l + 1
    else
      ans << cur
      cur += 1
    end
  end
  puts ans.join(' ')
end

q = gets.to_i
q.times do
  m = gets.to_i
  solve m
end


Today, Wet Shark is given n bishops on a 1000 by 1000 grid. Both rows and columns of the grid are numbered from 1 to 1000. Rows are numbered from top to bottom, while columns are numbered from left to right.Wet Shark thinks that two bishops attack each other if they share the same diagonal. Note, that this is the only criteria, so two bishops may attack each other (according to Wet Shark) even if there is another bishop located between them. Now Wet Shark wants to count the number of pairs of bishops that attack each other.

The first line of the input contains n (1 ≤ n ≤ 200 000) — the number of bishops. Each of next n lines contains two space separated integers xi and yi (1 ≤ xi, yi ≤ 1000) — the number of row and the number of column where i-th bishop is positioned. It's guaranteed that no two bishops share the same position.

Output one integer — the number of pairs of bishops which attack each other.

NoteIn the first sample following pairs of bishops attack each other: (1, 3), (1, 5), (2, 3), (2, 4), (3, 4) and (3, 5). Pairs (1, 2), (1, 4), (2, 5) and (4, 5) do not attack each other because they do not share the same diagonal.

n = gets.chomp.to_i
coords1 = Hash.new(0)
coords2 = Hash.new(0)
n.times do
  coord = gets.chomp.split(" ").map(&:to_i)
  coords1[coord[1] - coord[0]] += 1
  coords2[coord[1] + coord[0]] += 1
end

def comb(n, k)
  return ((n - k + 1)..n).inject(:*) / (1..k).inject(:*)
end

res = 0
coords1.values.each do |v|
  res += comb(v, 2)
end
coords2.values.each do |v|
  res += comb(v, 2)
end

puts res

r=0
h1=Hash.new(0)
h2=Hash.new(0)
gets.to_i.times{
	x,y=gets.split.map(&:to_i)
	r+=h1[x-y]
	h1[x-y]+=1
	r+=h2[x+y]
	h2[x+y]+=1
}
p r

n = gets.chomp.to_i
bishops1 = Hash.new(0)
bishops2 = Hash.new(0)

n.times do
  x, y = gets.chomp.split(" ").map(&:to_i)
  bishops1[x+y] += 1
  bishops2[x-y] += 1
end

def choose(n, k)
  # n!/(n-k)!
  pTop = (n-k+1 .. n).inject(1, &:*)
  # k!
  pBottom = (2 .. k).inject(1, &:*)
  pTop / pBottom
end

sum1 = bishops1.reduce(0) do |acc, (k, v)|
  acc+choose(v, 2)
end

sum2 = bishops2.reduce(0) do |acc, (k, v)|
  acc+choose(v, 2)
end

puts sum1+sum2

a=gets.chomp.to_i
b=Array.new(1000){Array.new(1000,0)}
a.times do
temp=gets.chomp.split(" ")
b[temp[0].to_i-1][temp[1].to_i-1]=1
end
col=[]
0.upto(998) do |i|
j=0
temp=0
p=0
p+=i
loop do
temp+=b[i][j]
i+=1
j+=1
break if i==1000
end
i=p
col << temp
temp=0
j=999
loop do
temp+=b[i][j]
i+=1
j-=1
break if i==1000
end
i=p
col << temp
end

1.upto(998) do |i|
j=0
p=0
p+=i
temp=0
loop do
temp+=b[j][i]
i+=1
j+=1
break if i==1000
end
i=p
col << temp
temp=0
j=0
loop do
temp+=b[j][i]
i-=1
j+=1
break if i<0
end
i=p
col << temp
end
col.delete(0)

ans=0
col.each {|i|  ans+=(i*(i-1))/2 }
puts "#{ans}"

n = gets.to_i
g = Array.new(1000) { Array.new(1000, -1) }
n.times do |v|
  a, b = gets.split.map(&:to_i)
  g[a-1][b-1] = v
end
ans = 0
m1 = 999
for s in 0..2*m1 do
  a = [0, s-m1].max
  b = [m1, s].min
  i = a
  j = s - a
  tmp = []
  while i <= b
    tmp.push(g[i][j]) if g[i][j] != -1
    i += 1
    j -= 1
  end
  ans += tmp.combination(2).size
end

for s in (-1*m1)..m1 do
  a = [0, (-1*s)].max
  b = [m1, (m1-s)].min
  i = a
  j = s + a
  tmp = []
  while i <= b
    tmp.push(g[i][j]) if g[i][j] != -1
    i += 1
    j += 1
  end
  ans += tmp.combination(2).size
end
puts ans

n = gets.chomp.to_i
xs = []
ys = []
hs0 = Hash.new(0)
hs1 = Hash.new(0)
n.times do
  x, y = gets.chomp.split.map(&:to_i)
  hs0[x + y] += 1
  hs1[x - y] += 1
end

cnt = 0
hs0.each do |_, v|
  cnt += v * (v - 1) / 2
end
hs1.each do |_, v|
  cnt += v * (v - 1) / 2
end
puts cnt

r=0
h1=Hash.new(0)
h2=Hash.new(0)
gets.to_i.times{
	x,y=gets.split.map(&:to_i)
	r+=h1[x-y]
	h1[x-y]+=1
	r+=h2[x+y]
	h2[x+y]+=1
}
puts r

# def solve_stupid(a)
#   cnt = 0
#   a.each_with_index do |(xx,yy), i|
#     dd = xx - yy
#     ss = xx + yy
#     cnt += (0...i).count do |j|
#       x, y = a[j]
#       x + y == ss || x - y == dd
#     end
#   end
#   cnt
# end

def solve(a)
  diag1 = Hash.new(0)
  diag2 = Hash.new(0)
  a.each do |x, y|
    diag1[x+y] += 1
    diag2[x-y] += 1
  end
  (diag1.values + diag2.values).map do |v|
    v*(v-1)
  end.reduce(0, :+) / 2
end

n = gets.to_i
a = Array.new(n) do
  gets.split.map(&:to_i)
end

puts solve(a)

# def rnd
#   require 'set'
#   s = Set.new
#   Array.new(rand(1..100)) do
#     c = [rand(100), rand(100)]
#     until s.add?(c)
#       c = [rand(100), rand(100)]
#     end
#     c
#   end
# end

# p((0..1000).all?{ t = rnd; solve(t) == solve_stupid(t)})

n = gets.to_i
b = []
d = Array.new(2001, 0)
nd = Array.new(2001, 0)
for _ in 0...n
  x, y = gets.split(' ').map &:to_i
  pos = y-x
  neg = y+x
  d[999+ pos]+=1
  nd[neg]+=1
end

def bin(n, k)
  u = l = 1
  for v in 1..k
    u*=n+1-v
    l*=v
  end
  u/l
end
p = 0
for i in 0...2001
  p += bin(d[i].to_i, 2) + bin(nd[i].to_i, 2)
end

puts p

n = gets.to_i
b = []
for _ in 0...n
  x, y = gets.split(' ').map &:to_i
  b << [x,y]
end

b = b.sort {|a,b| a[0] - b[0] }

p=0
for j in 0...(b.size-1)
  for i in (j+1)...b.size
    dx = b[j][0] - b[i][0]
    dy = b[j][1] - b[i][1]
    if dx!=0 && (dy/dx).abs == 1
      p += 1
    end
  end
end

puts p

n = gets.to_i
b = []
for _ in 0...n
  x, y = gets.split(' ').map &:to_i
  b << [x,y]
end

b = b.sort {|a,b| a[0] - b[0] }

p=0
for j in 0..b.size
  for i in (j+1)...b.size
    dx = b[j][0] - b[i][0]
    dy = b[j][1] - b[i][1]
    if dy>0 && (dx/dy).abs == 1
      p += 2
    end
  end
end

puts p

n = gets.to_i
b = []
for _ in 0...n
  x, y = gets.split(' ').map &:to_i
  b << [x,y]
end

b = b.sort {|a,b| a[0] - b[0] }

p=0
for j in 0...b.size
  for i in 0...b.size
    dx = b[j][0] - b[i][0]
    dy = b[j][1] - b[i][1]
    if dy>0 && (dx/dy).abs == 1
      p += 1
    end
  end
end

puts p

n = gets.to_i
b = []
d = Array.new(2002, 0)
nd = Array.new(2002, 0)
for _ in 0...n
  x, y = gets.split(' ').map &:to_i
  d[1001 + x - y]+=1
  nd[1001 + y - x]+=1
end

def bin(n, k)
  u = l = 1
  for v in 1..k
    u*=n+1-v
    l*=v
  end
  u/l
end
p = 0
for i in 0...2002
  p += bin(d[i].to_i, 2) + bin(nd[i].to_i, 2)
end

puts p

# b = b.sort {|a,b| a[0] - b[0] }

# p=0
# for j in 0...(b.size-1)
#   for i in (j+1)...b.size
#     dx = b[j][0] - b[i][0]
#     dy = b[j][1] - b[i][1]
#     if dx!=0 && (dy/dx).abs == 1
#       p += 1
#     end
#   end
# end

# puts p

n = gets.to_i
b = []
d = Array.new(2000, 0)
nd = Array.new(2000, 0)
for _ in 0...n
  x, y = gets.split(' ').map &:to_i

  d[1000 + x - y]+=1
  nd[1000 + y - x]+=1
end

def bin(n, k)
  u = l = 1
  for v in 1..k
    u*=n+1-v
    l*=v
  end
  u/l
end
p = 0
for i in 0...2000
  if d[i] > 1
    p += bin(d[i], 2)
  end

  if nd[i] > 1
    p += bin(nd[i], 2)
  end
end

puts p

# b = b.sort {|a,b| a[0] - b[0] }

# p=0
# for j in 0...(b.size-1)
#   for i in (j+1)...b.size
#     dx = b[j][0] - b[i][0]
#     dy = b[j][1] - b[i][1]
#     if dx!=0 && (dy/dx).abs == 1
#       p += 1
#     end
#   end
# end

# puts p

n = gets.to_i
b = []
for _ in 0...n
  x, y = gets.split(' ').map &:to_i
  b << [x,y]
end

b = b.sort {|a,b| a[0] - b[0] }

p=0
for j in 0..b.size
  for i in (j+1)...b.size
    dx = b[j][0] - b[i][0]
    dy = b[j][1] - b[i][1]
    if dy>0 && (dx/dy).abs == 1
      p += 1
    end
  end
end

puts p

n = gets.chomp.to_i
coords = Hash.new(0)
n.times do
  coord = gets.chomp.split(" ").map(&:to_i)
  k = coord[1] % coord[0]
  coords[k] += 1
end

def comb(n, k)
  return ((n - k + 1)..n).inject(:*) / (1..k).inject(:*)
end

res = 0
coords.values.each do |v|
  res += comb(v, 2)
end

puts res


Once again, Boris needs the help of Anton in creating a task. This time Anton needs to solve the following problem:There are two arrays of integers $$$a$$$ and $$$b$$$ of length $$$n$$$. It turned out that array $$$a$$$ contains only elements from the set $$$\{-1, 0, 1\}$$$.Anton can perform the following sequence of operations any number of times:   Choose any pair of indexes $$$(i, j)$$$ such that $$$1 \le i < j \le n$$$. It is possible to choose the same pair $$$(i, j)$$$ more than once.   Add $$$a_i$$$ to $$$a_j$$$. In other words, $$$j$$$-th element of the array becomes equal to $$$a_i + a_j$$$. For example, if you are given array $$$[1, -1, 0]$$$, you can transform it only to $$$[1, -1, -1]$$$, $$$[1, 0, 0]$$$ and $$$[1, -1, 1]$$$ by one operation.Anton wants to predict if it is possible to apply some number (zero or more) of these operations to the array $$$a$$$ so that it becomes equal to array $$$b$$$. Can you help him?

Each test contains multiple test cases.  The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10000$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$)  — the length of arrays. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-1 \le a_i \le 1$$$)  — elements of array $$$a$$$. There can be duplicates among elements. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$-10^9 \le b_i \le 10^9$$$)  — elements of array $$$b$$$. There can be duplicates among elements. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.

For each test case, output one line containing "YES" if it's possible to make arrays $$$a$$$ and $$$b$$$ equal by performing the described operations, or "NO" if it's impossible. You can print each letter in any case (upper or lower).

NoteIn the first test-case we can choose $$$(i, j)=(2, 3)$$$ twice and after that choose $$$(i, j)=(1, 2)$$$ twice too. These operations will transform $$$[1, -1, 0] \to [1, -1, -2] \to [1, 1, -2]$$$In the second test case we can't make equal numbers on the second position.In the third test case we can choose $$$(i, j)=(1, 2)$$$ $$$41$$$ times. The same about the fourth test case.In the last lest case, it is impossible to make array $$$a$$$ equal to the array $$$b$$$.

# https://codeforces.com/problemset/problem/1333/B

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

 first_plus_index = nil
 first_minus_index = nil

 possible = true
 n.times do |i|
  first_plus_index = i if first_plus_index.nil? && a[i] == 1
  first_minus_index = i if first_minus_index.nil? && a[i] == -1

  if a[i] != b[i] &&
   (i == 0 ||
   (b[i] < a[i] && (first_minus_index.nil? || first_minus_index == i)) ||
   (b[i] > a[i] && (first_plus_index.nil? || first_plus_index == i)))

   possible = false
   break
  end
 end

 puts possible ? "YES" : "NO"
end

# coding: utf-8

t = gets.to_i
Unit = Struct.new(:val, :n, :z, :p)
t.times {
  n = gets.to_i
  neg, zero, pos = 0, 0, 0
  ar = []
  gets.split.each { |i|
    i = i.to_i
    ar << Unit.new(i, neg, zero, pos)
    neg += 1 if i < 0
    zero += 1 if i == 0
    pos += 1 if i > 0
  }

  failed = false
  br = gets.split.map(&:to_i)
  # p ar
  until br.empty?
    b = br.pop # future state
    a = ar.pop # past state
    # from past to future
    if b == a.val
      next
    else
      if a.val < b
        if a.p < 1
          failed = true
          break
        end
      else
        if a.n < 1
          failed = true
          break
        end
      end
    end
  end

  # initial_state  ->next_state -> next_state -> goal

  if failed == true
    puts "NO"
  else
    puts "YES"
  end
}

t = gets.to_i
t.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  b = gets.split.map(&:to_i)
  plus = false
  minus = false
  ok = true
  for i in 0...n do
    if b[i] > a[i]
      if !plus
        ok = false
        break
      end
    elsif b[i] < a[i]
      if !minus
        ok = false
        break
      end
    end
    plus = true if a[i] == 1
    minus = true if a[i] == -1
  end
  puts ok ? "YES" : "NO"
end


Bob is a competitive programmer. He wants to become red, and for that he needs a strict training regime. He went to the annual meeting of grandmasters and asked $$$n$$$ of them how much effort they needed to reach red."Oh, I just spent $$$x_i$$$ hours solving problems", said the $$$i$$$-th of them. Bob wants to train his math skills, so for each answer he wrote down the number of minutes ($$$60 \cdot x_i$$$), thanked the grandmasters and went home. Bob could write numbers with leading zeroes — for example, if some grandmaster answered that he had spent $$$2$$$ hours, Bob could write $$$000120$$$ instead of $$$120$$$.Alice wanted to tease Bob and so she took the numbers Bob wrote down, and for each of them she did one of the following independently:   rearranged its digits, or  wrote a random number. This way, Alice generated $$$n$$$ numbers, denoted $$$y_1$$$, ..., $$$y_n$$$.For each of the numbers, help Bob determine whether $$$y_i$$$ can be a permutation of a number divisible by $$$60$$$ (possibly with leading zeroes).

The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 418$$$) — the number of grandmasters Bob asked. Then $$$n$$$ lines follow, the $$$i$$$-th of which contains a single integer $$$y_i$$$ — the number that Alice wrote down. Each of these numbers has between $$$2$$$ and $$$100$$$ digits '0' through '9'. They can contain leading zeroes.

Output $$$n$$$ lines. For each $$$i$$$, output the following. If it is possible to rearrange the digits of $$$y_i$$$ such that the resulting number is divisible by $$$60$$$, output "red" (quotes for clarity). Otherwise, output "cyan".

NoteIn the first example, there is one rearrangement that yields a number divisible by $$$60$$$, and that is $$$360$$$.In the second example, there are two solutions. One is $$$060$$$ and the second is $$$600$$$.In the third example, there are $$$6$$$ possible rearrangments: $$$025$$$, $$$052$$$, $$$205$$$, $$$250$$$, $$$502$$$, $$$520$$$. None of these numbers is divisible by $$$60$$$.In the fourth example, there are $$$3$$$ rearrangements: $$$228$$$, $$$282$$$, $$$822$$$.In the fifth example, none of the $$$24$$$ rearrangements result in a number divisible by $$$60$$$.In the sixth example, note that $$$000\dots0$$$ is a valid solution.

require 'set'; require 'prime'
INF=Float::INFINITY; MOD=10**9+7
t = gets.chomp.to_i
t.times do
  a = gets.chomp.split(//).map(&:to_i)
  index = a.index(0)
  if index.nil?
    puts 'cyan'
    next
  end
  a.delete_at(index)
  is_2 = a.any?{|e| e.even?}
  is_3 = a.inject(&:+) % 3 == 0
  puts (is_3 && is_2) ? 'red' : 'cyan'
end

gets.strip.to_i.times {
    x = gets.strip.split('')
    even = false
    zero1 = false
    zero2 = false
    odd = false
    sum = 0
    x.each { |i|
        w = i.to_i
        odd = true if w.odd?
        even = true if w.even? && w > 0
        zero2 = true if w == 0 && zero1
        zero1 = true if w == 0
        sum += w
    }
    if sum % 3 == 0 && ((!odd && !even && zero1) || (even && odd && zero1) || (even && zero1) || (odd && zero1 && zero2))
        puts "red"
    else
        puts "cyan"
    end
}

gets.strip.to_i.times {
    x = gets.strip.split('')
    even = false
    zero = false
    odd = false
    sum = 0
    x.each { |i|
        w = i.to_i
        odd = true if w.odd?
        even = true if w.even? && w > 0
        zero = true if w.even? && w == 0
        sum += w
    }
    if sum % 3 == 0 && ((!odd && !even && zero) || (even && odd && zero) || (even && zero))
        puts "red"
    else
        puts "cyan"
    end
}

gets.strip.to_i.times {
    x = gets.strip.split('')
    even = false
    zero = false
    odd = false
    sum = 0
    x.each { |i|
        w = i.to_i
        odd = true if w.odd?
        even = true if w.even? && w > 0
        zero = true if w.even? && w == 0
        sum += w
    }
    if sum % 3 == 0 && ((!odd && !even && zero) || (even && odd && zero) || (even && zero))
        puts "red"
    else
        puts "clan"
    end
}


You are given n × m table. Each cell of the table is colored white or black. Find the number of non-empty sets of cells such that:  All cells in a set have the same color.  Every two cells in a set share row or column.

The first line of input contains integers n and m (1 ≤ n, m ≤ 50) — the number of rows and the number of columns correspondingly. The next n lines of input contain descriptions of rows. There are m integers, separated by spaces, in each line. The number equals 0 if the corresponding cell is colored white and equals 1 if the corresponding cell is colored black.

Output single integer  — the number of non-empty sets from the problem description.

NoteIn the second example, there are six one-element sets. Additionally, there are two two-element sets, the first one consists of the first and the third cells of the first row, the second one consists of the first and the third cells of the second row. To sum up, there are 8 sets.

n, m=gets.split.map &:to_i
res=0
a=[]
n.times{
    a<<s=gets.split
    k=s.count ?0
    res+=(2**k-1)+(2**(m-k)-1)
}
a.transpose.each{|s|
    k=s.count ?0
    res+=(2**k-1)+(2**(n-k)-1)
}
puts res-n*m

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

ans = 0
n, m = gets.split(/\s+/).map(&:to_i)
a = Array.new(n)

n.times do |i|
  a[i] = gets.split(/\s+/).map(&:to_i)
end

0.upto(n-1) do |i|
  p = a[i].reduce(:+)
  q = m - p

  ans += max(2**p - 1 - p, 0)
  ans += max(2**q - 1 - q, 0)
end

0.upto(m-1) do |j|
  p = 0
  0.upto(n-1) {|i| p += a[i][j] }
  q = n - p
  ans += max(2**p - 1 - p, 0)
  ans += max(2**q - 1 - q, 0)
end

puts ans + n * m

n, m = gets.split.map(&:to_i)
l, res = Array.new(m, 0), 4
n.times do
  row = gets.split.map(&:to_i)
  x = row.count 1
  res += 2**x + 2**(m - x)
  row.each_with_index {|c, i| l[i] += c}
end
l.each {|x| res += 2**x + 2**(n - x)}
p res - (n + 2) * (m + 2)


Let's define $$$S(x)$$$ to be the sum of digits of number $$$x$$$ written in decimal system. For example, $$$S(5) = 5$$$, $$$S(10) = 1$$$, $$$S(322) = 7$$$.We will call an integer $$$x$$$ interesting if $$$S(x + 1) < S(x)$$$. In each test you will be given one integer $$$n$$$. Your task is to calculate the number of integers $$$x$$$ such that $$$1 \le x \le n$$$ and $$$x$$$ is interesting.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — number of test cases. Then $$$t$$$ lines follow, the $$$i$$$-th line contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) for the $$$i$$$-th test case.

Print $$$t$$$ integers, the $$$i$$$-th should be the answer for the $$$i$$$-th test case.

NoteThe first interesting number is equal to $$$9$$$.

gets.to_i.times {
  n = gets.to_i
  ans = n / 10
  ans += 1 if n % 10 == 9
  p ans
}


You are given a positive integer $$$x$$$. Check whether the number $$$x$$$ is representable as the sum of the cubes of two positive integers.Formally, you need to check if there are two integers $$$a$$$ and $$$b$$$ ($$$1 \le a, b$$$) such that $$$a^3+b^3=x$$$.For example, if $$$x = 35$$$, then the numbers $$$a=2$$$ and $$$b=3$$$ are suitable ($$$2^3+3^3=8+27=35$$$). If $$$x=4$$$, then no pair of numbers $$$a$$$ and $$$b$$$ is suitable.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case contains one integer $$$x$$$ ($$$1 \le x \le 10^{12}$$$). Please note, that the input for some test cases won't fit into $$$32$$$-bit integer type, so you should use at least $$$64$$$-bit integer type in your programming language.

For each test case, output on a separate line:    "YES" if $$$x$$$ is representable as the sum of the cubes of two positive integers.  "NO" otherwise.  You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).

NoteThe number $$$1$$$ is not representable as the sum of two cubes.The number $$$2$$$ is represented as $$$1^3+1^3$$$.The number $$$4$$$ is not representable as the sum of two cubes.The number $$$34$$$ is not representable as the sum of two cubes.The number $$$35$$$ is represented as $$$2^3+3^3$$$.The number $$$16$$$ is represented as $$$2^3+2^3$$$.The number $$$703657519796$$$ is represented as $$$5779^3+7993^3$$$.

def cbrt(x)
  return false if x <= 0
  y = Math.cbrt(x)
  y.ceil == y
end

t = gets.to_i
t.times do
  x = gets.to_i
  puts (1..10000).map { |a| cbrt(x - a.pow(3)) }.any? ? "YES" : "NO"
end
#
#

def cbrt(x)
  return false if x <= 0
  y = Math.cbrt(x)
  y.ceil - y < Float::EPSILON
end

t = gets.to_i
t.times do
  x = gets.to_i
  puts (1..10000).map { |a| cbrt(x - a.pow(3)) }.any? ? "YES" : "NO"
end
#
#

def cbrt(x)
  return false if x <= 0
  y = Math.cbrt(x)
  (y.round - y).abs < Float::EPSILON
end

t = gets.to_i
t.times do
  x = gets.to_i
  puts (1..10000).map { |a| cbrt(x - a.pow(3)) }.any? ? "YES" : "NO"
end
#
#

def cbrt(x)
  return false if x <= 0
  y = Math.cbrt(x)
  y.floor == y
end

t = gets.to_i
t.times do
  x = gets.to_i
  puts !(1..10000).select { |a| cbrt(x - a.pow(3)) }.empty? ? "YES" : "NO"
end
#

def cbrt(x)
  return false if x <= 0
  y = Math.cbrt(x)
  y % 1 == 0
end

t = gets.to_i
t.times do
  x = gets.to_i
  puts !(1..10000).select { |a| cbrt(x - a.pow(3)) }.empty? ? "YES" : "NO"
end

def cbrt(x)
  return false if x <= 0
  y = Math.cbrt(x)
  y - y.floor < Float::EPSILON
end

t = gets.to_i
t.times do
  x = gets.to_i
  puts !(1..10000).select { |a| cbrt(x - a.pow(3)) }.empty? ? "YES" : "NO"
end

def cbrt(x)
  return false if x <= 0
  y = Math.cbrt(x)
  (y.round - y).abs < Float::EPSILON
end

t = gets.to_i
t.times do
  x = gets.to_i
  puts !(1..10000).select { |a| cbrt(x - a.pow(3)) }.empty? ? "YES" : "NO"
end

def method(x, sum)
  y = sum ** 2 - x
  return false if y % 3 != 0
  b = -sum
  c = y / 3
  return false if b * b - 4 * c < 0
  d = Integer.sqrt(b * b - 4 * c)
  return false if d ** 2 != (b * b - 4 * c)
  x1, x2 = (-b - d) / 2, (-b + d) / 2
  return x1 > 0 && x2 > 0 && x1 + x2 == -b && x1 * x2 == c
end

t = gets.to_i
t.times do
  x = gets.to_i
  puts (1...20000).map { |sum| method(x / sum, sum) if x % sum == 0 }.any? ? "YES" : "NO"
end

t=gets.chomp.to_i
t.times do
x = gets.chomp.to_i
a=0.0
b=0.0
c=0.0
to = (x**(1.0/3.0)).to_i

#puts "to " + to.to_s

tr = false

for i in 1..to
    a=(i**3).to_f
    b=(x-a).to_f
    c=(b**(1.0/3.0)).round
    #puts "#{i} + #{a} + #{b} + #{c}"
    if(c**3==b && a+b==x && b!=0)
        puts "YES"
        tr=true
    end
    break if tr
end

if(!tr)
    puts "NO"
end

end


Alice gave Bob two integers $$$a$$$ and $$$b$$$ ($$$a > 0$$$ and $$$b \ge 0$$$). Being a curious boy, Bob wrote down an array of non-negative integers with $$$\operatorname{MEX}$$$ value of all elements equal to $$$a$$$ and $$$\operatorname{XOR}$$$ value of all elements equal to $$$b$$$.What is the shortest possible length of the array Bob wrote?Recall that the $$$\operatorname{MEX}$$$ (Minimum EXcluded) of an array is the minimum non-negative integer that does not belong to the array and the $$$\operatorname{XOR}$$$ of an array is the bitwise XOR of all the elements of the array.

The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The only line of each test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 \leq a \leq 3 \cdot 10^5$$$; $$$0 \leq b \leq 3 \cdot 10^5$$$) — the $$$\operatorname{MEX}$$$ and $$$\operatorname{XOR}$$$ of the array, respectively.

For each test case, output one (positive) integer — the length of the shortest array with $$$\operatorname{MEX}$$$ $$$a$$$ and $$$\operatorname{XOR}$$$ $$$b$$$. We can show that such an array always exists.

NoteIn the first test case, one of the shortest arrays with $$$\operatorname{MEX}$$$ $$$1$$$ and $$$\operatorname{XOR}$$$ $$$1$$$ is $$$[0, 2020, 2021]$$$.In the second test case, one of the shortest arrays with $$$\operatorname{MEX}$$$ $$$2$$$ and $$$\operatorname{XOR}$$$ $$$1$$$ is $$$[0, 1]$$$.It can be shown that these arrays are the shortest arrays possible.

x = 0
mexor = Array.new(300_001) { |i| x ^= i }

t = gets.to_i

t.times do

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

if mexor[a-1] == b
  puts a
else
  c = mexor[a-1] ^ b
  if a != c
    puts a + 1
  else
    puts a + 2
  end
end

end

cache = []
acc = 0
0.upto(3 * 100000 + 5).each {|x| cache.push(acc ^= x) }
t = gets.strip.to_i
t.times do
  a, b = gets.strip.split.map {|x| x.to_i }
  x = cache[a - 1]
  if x == b
    puts a
  elsif (x ^ b) != a
    puts a + 1
  else
    puts a + 2
  end
end

x = 0
mexor = Array.new(300_001) { |i| x ^= i }

t = gets.to_i

t.times do

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

if mexor[a-1] == b
  puts a
elsif a != b
  puts a + 1
else
  puts a + 2
end

end


There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.

Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.

NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.

gets.to_i.times{n=gets.to_i;i=n%2;puts 2,'3'*i,1;i+=3;(p -~i,i;i+=2)while i<n}

gets.to_i.times{
  n=gets.to_i;i=n%2+3;puts 2,'3'*(n%2),1
  (p -~i,i;i+=2)while i<n
}

t = gets.chomp.to_i
t.times do
  n = gets.chomp.to_i
  arr = Array(1..n)
  (0...n).step(2) do |i|
    next if i + 1 == n
    arr[i], arr[i+1] = arr[i+1], arr[i]
  end
  arr[n-1], arr[n-2] = arr[n-2], arr[n-1] if n % 2 == 1

  puts "#{arr.join(' ')}\n"
end

gets.to_i.times{n=gets.to_i;i=n%2;p 2,'3'*i,1;i+=3;(p -~i,i;i+=2)while i<n}


The Cybernetics Failures (CF) organisation made a prototype of a bomb technician robot. To find the possible problems it was decided to carry out a series of tests. At the beginning of each test the robot prototype will be placed in cell (x0, y0) of a rectangular squared field of size x × y, after that a mine will be installed into one of the squares of the field. It is supposed to conduct exactly x·y tests, each time a mine is installed into a square that has never been used before. The starting cell of the robot always remains the same.After placing the objects on the field the robot will have to run a sequence of commands given by string s, consisting only of characters 'L', 'R', 'U', 'D'. These commands tell the robot to move one square to the left, to the right, up or down, or stay idle if moving in the given direction is impossible. As soon as the robot fulfills all the sequence of commands, it will blow up due to a bug in the code. But if at some moment of time the robot is at the same square with the mine, it will also blow up, but not due to a bug in the code.Moving to the left decreases coordinate y, and moving to the right increases it. Similarly, moving up decreases the x coordinate, and moving down increases it.The tests can go on for very long, so your task is to predict their results. For each k from 0 to length(s) your task is to find in how many tests the robot will run exactly k commands before it blows up.

The first line of the input contains four integers x, y, x0, y0 (1 ≤ x, y ≤ 500, 1 ≤ x0 ≤ x, 1 ≤ y0 ≤ y) — the sizes of the field and the starting coordinates of the robot. The coordinate axis X is directed downwards and axis Y is directed to the right. The second line contains a sequence of commands s, which should be fulfilled by the robot. It has length from 1 to 100 000 characters and only consists of characters 'L', 'R', 'U', 'D'.

Print the sequence consisting of (length(s) + 1) numbers. On the k-th position, starting with zero, print the number of tests where the robot will run exactly k commands before it blows up.

NoteIn the first sample, if we exclude the probable impact of the mines, the robot's route will look like that: .

#what the f**k the problem statement was talking about.
#Good luk for the Contest 606, Problem: b

X, Y, x, y = gets.strip.split.map(&:to_i)
cmd = gets.strip.split('')

mp = Array.new(X){Array.new(Y,1)}

pos = [x-1, y-1]
res = Array.new
cmd.each do |c|

	res << mp[pos[0]][pos[1]]
	mp[pos[0]][pos[1]] = 0
	next if pos[0] == 0 and c == 'U'
	next if pos[0] == X-1 and c == 'D'
	next if pos[1] == 0 and c == 'L'
	next if pos[1] == Y-1 and c == 'R'
	case c
	when 'U'
		pos[0] -=1
	when 'D'
		pos[0] += 1
	when 'L'
		pos[1] -= 1
	when 'R'
		pos[1] += 1
	end

  end

res << mp.reduce(0){|r,v| r += v.reduce(:+)}

res.each do |e|
	print e,' '
end
print "\n"

x, y, x0, y0 = gets.chomp.split.map(&:to_i)
s = gets.chomp
d = "LRUD"
dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]
hs = Hash.new(false)

t = x * y
ans = []

for i in 0...s.length
  if hs[[x0, y0]]
    ans << 0
  else
    hs[[x0, y0]] = true
    ans << 1
    t -= 1
  end
  x0 = [[x0 + dx[d.index(s[i])], 1].max, x].min
  y0 = [[y0 + dy[d.index(s[i])], 1].max, y].min
end

ans << t
puts ans.*' '

X, Y, x, y = gets.split.map(&:to_i)
s = gets.chomp!

ans = []

m = []
X.times{ |x|
  m[x+1] = []
  Y.times { |y|
    m[x+1][y+1] = true
  }
}

s.each_char do |c|
  if m[x][y]
    ans << 1
    m[x][y] = false
  else
    ans << 0
  end
  case c
  when 'L'
    y -= 1
  when 'R'
    y += 1
  when 'U'
    x -= 1
  when 'D'
    x += 1
  end
  x = [[1, x].max, X].min
  y = [[1, y].max, Y].min
end

ans << X * Y - ans.inject(&:+)

puts ans.join(' ')


You have array a1, a2, ..., an. Segment [l, r] (1 ≤ l ≤ r ≤ n) is good if ai = ai - 1 + ai - 2, for all i (l + 2 ≤ i ≤ r).Let's define len([l, r]) = r - l + 1, len([l, r]) is the length of the segment [l, r]. Segment [l1, r1], is longer than segment [l2, r2], if len([l1, r1]) > len([l2, r2]).Your task is to find a good segment of the maximum length in array a. Note that a segment of length 1 or 2 is always good.

The first line contains a single integer n (1 ≤ n ≤ 105) — the number of elements in the array. The second line contains integers: a1, a2, ..., an (0 ≤ ai ≤ 109).

Print the length of the longest good segment in array a.

#!/usr/bin/ruby
n=gets.to_i
m=2;c=0
a=gets.split.map(&:to_i)
if a.size==1
	p 1
	exit
end
a.each_cons(3){|e|
	if e[0]+e[1]==e[2]
		c+=1
	else
		m=[m,c+2].max
		c=0
	end
}
p [m,c+2].max

#!/usr/bin/ruby
gets
m=2;c=0
a=gets.split.map(&:to_i)
if a.size==1
	p 1
	exit
end
a.each_cons(3){|e|
	if e[0]+e[1]==e[2]
		c+=1
	else
		m=[m,c+2].max
		c=0
	end
}
p [m,c+2].max

n = STDIN.readline.to_i
arr = STDIN.readline.split.collect {|s| s.to_i }
if n <= 2
	puts n
	exit
end
best = 2
pos = 0
while pos+2 < n
	count = 2
	seek = pos+2
	while seek < n
		break if arr[seek] != arr[seek-1] + arr[seek-2]
		count += 1
		seek += 1
	end
	best = [best, count].max
	pos = seek-1
end
puts best

n=gets.to_i
a=gets.chomp.split.map(&:to_i)
ans,d=0,0
n.times do |i|
	(i<2 || a[i]==a[i-1]+a[i-2]) ? d+=1 : d=2
	ans=d if d>ans
end
puts ans

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
seg = arr[0..1].length
max_seg = seg
if n > 1
	(2..(n-1)).each do |i|
		# puts "#{a == arr[i-2] + arr[i-1] }"
		if arr[i] == arr[i-2] + arr[i-1]
			# puts "here"
			if seg == 0
				seg = 3
			else
				seg += 1
			end
			max_seg = seg if seg > max_seg
		else
			seg = 0
		end
	end
end
puts max_seg
# 16
# 1 1 1 2 3 5 8 1 1 1 2 3 5 8 13 21

n = gets.to_i
a = gets.split.collect{|i| i.to_i}
if a.size < 3
    puts a.size
    exit
end
maxlength = 2
thislength = 2
for i in 2...n
    if a[i] == a[i-1]+a[i-2]
        thislength += 1
        maxlength = thislength if thislength > maxlength
    else
        thislength = 2
    end
end
puts maxlength

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

L = Array.new

(0..n-1).each do |i|
	L[i] = 2
end

L[0] = 1
max = 1

(2..n-1).each do |i|
	if a[i] == a[i-1] + a[i-2]
		L[i] = L[i-1] + 1
	end
	if L[i] > max
		max = L[i]
	end
end

if n == 2
	max = 2
end

puts max

#!/usr/bin/ruby
n=gets.to_i
m=2;c=0
gets.split.map(&:to_i).each_cons(3){|e|
	if e[0]+e[1]==e[2]
		c+=1
	else
		m=[m,c+2].max
		c=0
	end
}
p [m,c+2].max

n=gets.to_i;q=Array.new(n+1,0);p=gets.split.map &:to_i;p.each_with_index{|v,i| q[i+1]=(i>1?((v==p[i-1]+p[i-2])?q[i]+1:2):q[i]+1)};p q.max
p q

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

L = Array.new

(0..n-1).each do |i|
	L[i] = 2
end

L[0] = 1
max = 2

(2..n-1).each do |i|
	if a[i] == a[i-1] + a[i-2]
		L[i] = L[i-1] + 1
		if L[i] > max
			max = L[i]
		end
	end
end

puts max

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

L = Array.new

(0..n-1).each do |i|
    L[i] = 1
end

max = 1

(1..n-1).each do |i|
    if a[i] >= a[i-1]
        L[i] = 2
        if max < 2
            max = 2
        end
        if i > 1
            if a[i] == a[i-1] + a[i-2]
                L[i] = L[i-1] + 1
                if L[i] > max
                    max = L[i]
                end
            end
        end
    end
end

puts max

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

L = Array.new

(0..n-1).each do |i|
    L[i] = 2
end

L[0] = 1
max = 1

(2..n-1).each do |i|
    if a[i] == a[i-1] + a[i-2]
        L[i] = L[i-1] + 1
        if L[i] > max
            max = L[i]
        end
    end
end

puts max

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

L = Array.new

(0..n-1).each do |i|
    L[i] = 2
end

L[0] = 1
max = 1

(2..n-1).each do |i|
    if a[i] == a[i-1] + a[i-2]
        L[i] = L[i-1] + 1
    end
    if L[i] > max
        max = L[i]
    end
end

puts max


Sayaka Saeki is a member of the student council, which has $$$n$$$ other members (excluding Sayaka). The $$$i$$$-th member has a height of $$$a_i$$$ millimeters.It's the end of the school year and Sayaka wants to take a picture of all other members of the student council. Being the hard-working and perfectionist girl as she is, she wants to arrange all the members in a line such that the amount of photogenic consecutive pairs of members is as large as possible.A pair of two consecutive members $$$u$$$ and $$$v$$$ on a line is considered photogenic if their average height is an integer, i.e. $$$\frac{a_u + a_v}{2}$$$ is an integer.Help Sayaka arrange the other members to maximize the number of photogenic consecutive pairs.

The first line contains a single integer $$$t$$$ ($$$1\le t\le 500$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 2000$$$)  — the number of other council members. The second line of each test case contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \le a_i \le 2 \cdot 10^5$$$)  — the heights of each of the other members in millimeters. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$.

For each test case, output on one line $$$n$$$ integers representing the heights of the other members in the order, which gives the largest number of photogenic consecutive pairs. If there are multiple such orders, output any of them.

NoteIn the first test case, there is one photogenic pair: $$$(1, 1)$$$ is photogenic, as $$$\frac{1+1}{2}=1$$$ is integer, while $$$(1, 2)$$$ isn't, as $$$\frac{1+2}{2}=1.5$$$ isn't integer.In the second test case, both pairs are photogenic.

test_cases = gets.chomp.to_i
for i in 1..test_cases do
    n = gets.chomp.to_i
    line = gets.chomp.split(" ")
    odd = Array.new
    even = Array.new
    for j in 0..n - 1 do
        a = line[j].to_i
        if a % 2 == 1
            odd.push(a)
        else
            even.push(a)
        end
    end
    for j in 0..odd.size - 1 do
        print "#{odd[j]} "
    end
    for j in 0..even.size - 1 do
        print "#{even[j]} "
    end
    puts ""
end

t = gets.to_s.to_i

1.upto(t) do |casenum|
  n = gets.to_s.split.map {|x| x.to_i }
  ar = gets.to_s.split.map {|x| x.to_i }

  aro = ar.select {|x| x.odd? }
  are = ar.select {|x| x.even? }

  if aro.size > are.size
    printf("%s %s\n", aro.join(" "), are.join(" "))
  else
    printf("%s %s\n", are.join(" "), aro.join(" "))
  end
end