You are given the string s of length n and the numbers p, q. Split the string s to pieces of length p and q.For example, the string "Hello" for p = 2, q = 3 can be split to the two strings "Hel" and "lo" or to the two strings "He" and "llo".Note it is allowed to split the string s to the strings only of length p or to the strings only of length q (see the second sample test).

The first line contains three positive integers n, p, q (1 ≤ p, q ≤ n ≤ 100). The second line contains the string s consists of lowercase and uppercase latin letters and digits.

If it's impossible to split the string s to the strings of length p and q print the only number "-1". Otherwise in the first line print integer k — the number of strings in partition of s. Each of the next k lines should contain the strings in partition. Each string should be of the length p or q. The string should be in order of their appearing in string s — from left to right. If there are several solutions print any of them.

#!/usr/bin/ruby
n,a,b=gets.split.map(&:to_i)
j=0.upto(n/a).find{|i|(n-i*a)%b==0}
if !j
	p -1
else
	k=(n-j*a)/b
	s=gets.chomp.chars.to_a
	p j+k
	j.times{puts s.shift(a)*''}
	k.times{puts s.shift(b)*''}
end

def count(n, p, q, s)
	i = 0

	while (n - i*q) >=0
		if (n-i*q)%p == 0
			res = Array.new
 			i.times do |e|
 				res << s[e*q...(e+1)*q]
			end

			head = i*q

			(((n-i*q)/p).to_i()).times do |e|
				res << s[head+e*p...head+(e+1)*p]
			end

			puts i+(n-i*q)/p.to_i
			puts res
			return 1
		end
		i += 1

	end
	return -1
end

n, p, q = gets.strip.split.map(&:to_i)
s = gets.strip
p, q = q, p if p > q
if count(n,p,q, s) == -1
	puts -1
end

a=gets.chomp.split(" ")
n=a[0].to_f
p=a[1].to_f
j=a[2].to_f
s=gets.chomp

sc1=0
sc1c=0
sc1n=n
loop do
    sc1n=sc1n-p
	break if sc1n <= 0
	if sc1n%j==0
		sc1+=1
	end
	sc1c+=1
	break if sc1n%j==0
end

sc2=0
sc2c=0
sc2n=n
loop do
    sc2n=sc2n-j
	break if sc2n <= 0
	if sc2n%p==0
		sc2+=1
	end
	sc2c+=1
	break if sc2n%p==0
end

if n==p+j
   puts 2
   puts s[0..(p-1)]
   puts s[p..(s.length-1)]
elsif n%p==0
    split=n/p
	puts split.to_i
	c1=0
	c2=0
	loop do
		break if c2==split
		puts s[c1..(c1+p-1)]
		c1=c1+p
        c2+=1
	end

elsif n%j==0
    split=n/j
	puts split.to_i
	c1=0
	c2=0
	loop do
		break if c2==split
		puts s[c1..(c1+j-1)]
		c1=c1+j
        c2+=1
	end
elsif sc1>0
	puts (sc1c+(n-sc1c*p)/j ).to_i
	c1=0
	c2=0
	loop do
		break if c2==sc1c
		puts s[c1..(c1+p-1)]
		c1=c1+p
        c2+=1
	end

	c2=0
	loop do
		break if c2==(n-sc1c*p)/j
		puts s[c1..(c1+j-1)]
		c1=c1+j
        c2+=1
	end
elsif sc2>0
	puts (sc2c+(n-sc2c*j)/p ).to_i

	c2=0
	loop do
		break if c2==sc2c
		puts s[c1..(c1+j-1)]
		c1=c1+j
        c2+=1
	end
	c1=0
	c2=0
	loop do
		break if c2==(n-sc2c*j)/p
		puts s[c1..(c1+p-1)]
		c1=c1+p
        c2+=1
	end
else
	puts -1
end

#!/usr/bin/env ruby

def split n, p, q
  max_left_coeff = n / p
  (0..max_left_coeff).each do |a|
    left = a * p
    right = n - left
    if right % q == 0
      return [a, (right / q)]
    end
  end
  [-1, 0]
end

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

a, b = split(n, p, q)

puts (a + b)
a.times do
  puts s[0, p]
  s = s[p..s.size-1]
end
b.times do
  puts s[0, q]
  s = s[q..s.size-1]
end

line1 = gets.to_s.split(' ').collect{ |x| x.to_i}
n = line1[0]
p = line1[1]
q = line1[2]

s = gets

x = n / p

y = n / q
total = -1
tmp = -1
(0..x).each do |index|
  ci = n - index * p
  if ci % q == 0
    total = index + ci / q
    tmp = index
    break
  end
end

puts total
if total != -1
  cur = 0
  (1..tmp).each do
    puts s[cur...cur+p]
    cur+=p
  end
  (1..total - tmp).each do
    puts s[cur...cur+q]
    cur += q
  end
end

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

a = b = -1
for i in 0..100
  for j in 0..100
    if p*i + q*j == n
      a = i
      b = j
    end
  end
end

if a == -1
  puts a
  exit
end

puts a+b

idx = 0
(1..a).each do
  puts s[idx, p]
  idx += p
end

(1..b).each do
  puts s[idx, q]
  idx += q
end

n,p,q=gets.split.map(&:to_i)
s=gets.chomp.split(//)
a=b=0

0.upto(100){|k|
	m=n-k*p
	break if m<0
	if m%q==0
		a=k
		b=m/q
		break
	end
}
if a==0&&b==0
	puts -1
else
	puts a+b
	a.times{puts s.shift(p).join}
	b.times{puts s.shift(q).join}
end

class Solver
	def main
		n, a, b = gets.split.map { |x| x.to_i }
		str = gets.chomp
		0.upto(n) do |l|
			r = n - l
			if l % a == 0 && r % b == 0
				puts l / a + r / b
				0.upto(l / a - 1) do |i|
					puts str[i * a, a]
				end
				0.upto(r / b - 1) do |i|
					puts str[l + i * b, b]
				end
				return
			end
		end
		puts -1
	end
end
Solver.new.main

a=gets.chomp.split(" ")
n=a[0].to_f
p=a[1].to_f
j=a[2].to_f
s=gets.chomp

sc1=0
sc1c=0
sc1n=n
loop do
    sc1n=sc1n-p
	break if sc1n <= 0
	if sc1n%j==0
		sc1+=1
	end
	sc1c+=1
	break if sc1n%j==0
end

sc2=0
sc2c=0
sc2n=n
loop do
    sc2n=sc2n-j
	break if sc2n <= 0
	if sc2n%p==0
		sc2+=1
	end
	sc2c+=1
	break if sc2n%p==0
end

if n==p+j
   puts 2
   puts s[0..(p-1)]
   puts s[p..(s.length-1)]
elsif n%p==0
    split=n/p
	puts split.to_i
	c1=0
	c2=0
	loop do
		break if c2==split
		puts s[c1..(c1+p-1)]
		c1=c1+p
        c2+=1
	end

elsif n%j==0
    split=n/j
	puts split.to_i
	c1=0
	c2=0
	loop do
		break if c2==split
		puts s[c1..(c1+j-1)]
		c1=c1+j
        c2+=1
	end
elsif sc1>0
	puts (sc1c+(n-sc1c*p)/j ).to_i
	c1=0
	c2=0
	loop do
		break if c2==sc1c
		puts s[c1..(c1+p-1)]
		c1=c1+p
        c2+=1
	end
	c1=0
	c2=0
	loop do
		break if c2==(n-sc1c*p)/j
		puts s[c1..(c1+j-1)]
		c1=c1+j
        c2+=1
	end
elsif sc2>0
	puts (sc2c+(n-sc2c*j)/p ).to_i
	c1=0
	c2=0
	loop do
		break if c2==sc2c
		puts s[c1..(c1+j-1)]
		c1=c1+j
        c2+=1
	end
	c1=0
	c2=0
	loop do
		break if c2==(n-sc2c*j)/p
		puts s[c1..(c1+p-1)]
		c1=c1+p
        c2+=1
	end
else
	puts -1
end

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

if s.length%p != 0 && s.length%q != 0 && s.length%(p+q) != 0
  puts -1
  exit
end

arr = []
if s.length%p == 0
  arr = [p]
  puts s.length/p
elsif s.length%(p+q) == 0
  arr = [p, q]
  puts (s.length/(p+q))*2
elsif s.length%q == 0
  arr = [q]
  puts s.length/q
end

count = 0
idx = 0
while count != s.length
  puts s[count, arr[idx%arr.length]]
  count += arr[idx%arr.length]
  idx += 1
end


You are given the set of vectors on the plane, each of them starting at the origin. Your task is to find a pair of vectors with the minimal non-oriented angle between them.Non-oriented angle is non-negative value, minimal between clockwise and counterclockwise direction angles. Non-oriented angle is always between 0 and π. For example, opposite directions vectors have angle equals to π.

First line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of vectors. The i-th of the following n lines contains two integers xi and yi (|x|, |y| ≤ 10 000, x2 + y2 > 0) — the coordinates of the i-th vector. Vectors are numbered from 1 to n in order of appearing in the input. It is guaranteed that no two vectors in the input share the same direction (but they still can have opposite directions).

Print two integer numbers a and b (a ≠ b) — a pair of indices of vectors with the minimal non-oriented angle. You can print the numbers in any order. If there are many possible answers, print any.

# c598-C

data = []

gets.to_i.times do |i|
	p, q = gets.split.map{|x| Integer(x) }
	data << [i, Complex(p, q).arg]
end

best_ij = []
best_theta = 7.0

s_data = data.sort_by{|d| d[1] }
(data.size - 1).times do |i|
	theta = s_data[i+1][1] - s_data[i][1]
	if (best_theta > theta)
		best_ij = [s_data[i][0], s_data[i+1][0]]
		best_theta = theta
	end
end

last_theta = (s_data.last)[1] = (s_data.first)[0]
if (last_theta < best_theta)
	puts "1 #{s_data.size}"
else
	puts "#{best_ij[0]+1} #{best_ij[1]+1}"
end

# c598-C

def compare(v1,v2,v3,v4)
	ij_cos = (v1[0]*v2[0]+v1[1]*v2[1])/Math.sqrt(v1[0]*v1[0]+v1[1]*v1[1])/Math.sqrt(v2[0]*v2[0]+v2[1]*v2[1])
	current_cos = (v3[0]*v4[0]+v3[1]*v4[1])/Math.sqrt(v3[0]*v3[0]+v3[1]*v3[1])/Math.sqrt(v4[0]*v4[0]+v4[1]*v4[1])
	#ij_cos = (-v1[1]*v2[0]+v1[0]*v2[1])/Math.sqrt(v1[0]*v1[0]+v1[1]*v1[1])/Math.sqrt(v2[0]*v2[0]+v2[1]*v2[1])
	#current_cos = (-v3[1]*v4[0]+v3[0]*v4[1])/Math.sqrt(v3[0]*v3[0]+v3[1]*v3[1])/Math.sqrt(v4[0]*v4[0]+v4[1]*v4[1])
	#puts ij_cos
	#puts current_cos
	#ij_cos.abs <=> current_cos.abs
	ij_cos <=> current_cos
end

data = []

gets.to_i.times do |i|
	p, q = gets.split.map{|x| Integer(x) }
	data << [i, Complex(p, q).arg, p, q]
end

best_ij = []
best_theta = 7.0

s_data = data.sort_by{|d| d[1] }
(data.size - 1).times do |i|
	theta = s_data[i+1][1] - s_data[i][1]
	#puts "#{theta} -- #{best_theta}"
	if (best_theta - theta > 1E-8)
		#puts "update #{s_data[i][0]},#{s_data[i+1][0]}"
		best_ij = [s_data[i][0], s_data[i+1][0], s_data[i][2,2], s_data[i+1][2,2]]
		best_theta = theta
	elsif (best_theta - theta).abs < 1E-7
		#ij_cos = (-best_ij[2][1]*best_ij[3][0]+best_ij[2][0]*best_ij[3][1])/Math.sqrt(best_ij[2][0]*best_ij[2][0]+best_ij[2][1]*best_ij[2][1])/Math.sqrt(best_ij[3][0]*best_ij[3][0]+best_ij[3][1]*best_ij[3][1])
		#current_cos = (-s_data[i][3]*s_data[i+1][2]+s_data[i][2]*s_data[i+1][3])/Math.sqrt(s_data[i][2]*s_data[i][2]+s_data[i][3]*s_data[i][3])/Math.sqrt(s_data[i+1][2]*s_data[i+1][2]+s_data[i+1][3]*s_data[i+1][3])
		if compare(best_ij[2], best_ij[3], s_data[i][2,2], s_data[i+1][2,2]) == -1
			#puts "update #{s_data[i][0]},#{s_data[i+1][0]}"
			best_ij = [s_data[i][0], s_data[i+1][0], s_data[i][2,2], s_data[i+1][2,2]]
			best_theta = theta
		end
	end
end

last_theta = (s_data.first)[1] - (s_data.last)[1] + 2 * Math::PI
if (last_theta < best_theta)
	puts "#{(s_data.last)[0]+1} #{(s_data.first)[0]+1}"
else
	puts "#{best_ij[0]+1} #{best_ij[1]+1}"
end

# c598-C

data = []

gets.to_i.times do |i|
	p, q = gets.split.map{|x| Integer(x) }
	data << [i, Complex(p, q).arg]
end

best_ij = []
best_theta = 7.0

s_data = data.sort_by{|d| d[1] }
(data.size - 1).times do |i|
	theta = s_data[i+1][1] - s_data[i][1]
	if (best_theta > theta)
		best_ij = [s_data[i][0], s_data[i+1][0]]
		best_theta = theta
	end
end

last_theta = (s_data.last)[1] = (s_data.first)[0]
if (last_theta < best_theta)
	puts "0 #{s_data.size + 1}"
else
	puts "#{best_ij[0]} #{best_ij[1]}"
end

# c598-C

data = []

gets.to_i.times do |i|
	p, q = gets.split.map{|x| Integer(x) }
	data << [i, Complex(p, q).arg]
end

best_ij = []
best_theta = 7.0

s_data = data.sort_by{|d| d[1] }
(data.size - 1).times do |i|
	theta = s_data[i+1][1] - s_data[i][1]
	if (best_theta > theta)
		best_ij = [s_data[i][0], s_data[i+1][0]]
		best_theta = theta
	end
end

last_theta = (s_data.last)[1] = (s_data.first)[0]
if (last_theta < best_theta)
	puts "1 #{s_data.size + 1}"
else
	puts "#{best_ij[0]+1} #{best_ij[1]+1}"
end

# c598-C

data = []

gets.to_i.times do |i|
	p, q = gets.split.map{|x| Integer(x) }
	data << [i, Complex(p, q).arg, p, q]
end

best_ij = []
best_theta = 7.0

s_data = data.sort_by{|d| d[1] }
(data.size - 1).times do |i|
	theta = s_data[i+1][1] - s_data[i][1]
	if (best_theta > theta)
		best_ij = [s_data[i][0], s_data[i+1][0], s_data[i][2,2], s_data[i+1][2,2]]
		best_theta = theta
	elsif best_theta == theta
		ij_cos = (-best_ij[2][1]*best_ij[3][0]+best_ij[2][0]*best_ij[3][1])/Math.sqrt(best_ij[2][0]*best_ij[2][0]+best_ij[2][1]*best_ij[2][1])/Math.sqrt(best_ij[3][0]*best_ij[3][0]+best_ij[3][1]*best_ij[3][1])
		current_cos = (-s_data[i][3]*s_data[i+1][2]+s_data[i][2]*s_data[i+1][3])/Math.sqrt(s_data[i][2]*s_data[i][2]+s_data[i][3]*s_data[i][3])/Math.sqrt(s_data[i+1][2]*s_data[i+1][2]+s_data[i+1][3]*s_data[i+1][3])
		if ij_cos > current_cos
			best_ij = [s_data[i][0], s_data[i+1][0], s_data[i][2,2], s_data[i+1][2,2]]
			best_theta = theta
		end
	end
end

last_theta = (s_data.first)[1] - (s_data.last)[1] + 2 * Math::PI
if (last_theta < best_theta)
	puts "#{(s_data.last)[0]+1} #{(s_data.first)[0]+1}"
else
	puts "#{best_ij[0]+1} #{best_ij[1]+1}"
end

# c598-C

data = []

gets.to_i.times do |i|
	p, q = gets.split.map{|x| Integer(x) }
	data << [i, Complex(p, q).arg]
end

best_ij = []
best_theta = 7.0

s_data = data.sort_by{|d| d[1] }
(data.size - 1).times do |i|
	theta = s_data[i+1][1] - s_data[i][1]
	if (best_theta >= theta)
		best_ij = [s_data[i][0], s_data[i+1][0]]
		best_theta = theta
	end
end

last_theta = (s_data.first)[1] - (s_data.last)[1] + 2 * Math::PI
if (last_theta < best_theta)
	puts "#{(s_data.last)[0]+1} #{(s_data.first)[0]+1}"
else
	puts "#{best_ij[0]+1} #{best_ij[1]+1}"
end

# c598-C

data = []

gets.to_i.times do |i|
	p, q = gets.split.map{|x| Integer(x) }
	data << [i, Complex(p, q).arg]
end

best_ij = []
best_theta = 7.0

s_data = data.sort_by{|d| d[1] }
(data.size - 1).times do |i|
	theta = s_data[i+1][1] - s_data[i][1]
	if (best_theta > theta)
		best_ij = [s_data[i][0], s_data[i+1][0]]
		best_theta = theta
	end
end

last_theta = (s_data.last)[1] = (s_data.first)[1]
if (last_theta < best_theta)
	puts "#{(s_data.last)[0]} #{(s_data.first)[0]}"
else
	puts "#{best_ij[0]+1} #{best_ij[1]+1}"
end

# c598-C

data = []

gets.to_i.times do |i|
	p, q = gets.split.map{|x| Integer(x) }
	data << [i, Complex(p, q).arg]
end

best_ij = []
best_theta = 7.0

s_data = data.sort_by{|d| d[1] }
(data.size - 1).times do |i|
	theta = s_data[i+1][1] - s_data[i][1]
	if (best_theta > theta)
		best_ij = [s_data[i][0], s_data[i+1][0]]
		best_theta = theta
	end
end

last_theta = (s_data.first)[1] - (s_data.last)[1] + 2 * Math::PI
if (last_theta < best_theta)
	puts "#{(s_data.last)[0]+1} #{(s_data.first)[0]+1}"
else
	puts "#{best_ij[0]+1} #{best_ij[1]+1}"
end

# c598-C

def compare(v1,v2,v3,v4)
	#ij_cos = (v1[0]*v2[0]+v1[1]*v2[1])/Math.sqrt(v1[0]*v1[0]+v1[1]*v1[1])/Math.sqrt(v2[0]*v2[0]+v2[1]*v2[1])
	#current_cos = (v3[0]*v4[0]+v3[1]*v4[1])/Math.sqrt(v3[0]*v3[0]+v3[1]*v3[1])/Math.sqrt(v4[0]*v4[0]+v4[1]*v4[1])
	ij_cos = (-v1[1]*v2[0]+v1[0]*v2[1])/Math.sqrt(v1[0]*v1[0]+v1[1]*v1[1])/Math.sqrt(v2[0]*v2[0]+v2[1]*v2[1])
	current_cos = (-v3[1]*v4[0]+v3[0]*v4[1])/Math.sqrt(v3[0]*v3[0]+v3[1]*v3[1])/Math.sqrt(v4[0]*v4[0]+v4[1]*v4[1])
	#puts ij_cos
	#puts current_cos
	current_cos.abs <=> ij_cos.abs
	#ij_cos <=> current_cos
end

data = []

gets.to_i.times do |i|
	p, q = gets.split.map{|x| Integer(x) }
	data << [i, Complex(p, q).arg, p, q]
end

best_ij = []
best_theta = 7.0

s_data = data.sort_by{|d| d[1] }
(data.size - 1).times do |i|
	theta = s_data[i+1][1] - s_data[i][1]
	#puts "#{theta} -- #{best_theta}"
	if (best_theta - theta > 9E-13)
		#puts "update #{s_data[i][0]},#{s_data[i+1][0]}"
		best_ij = [s_data[i][0], s_data[i+1][0], s_data[i][2,2], s_data[i+1][2,2]]
		best_theta = theta
	elsif (best_theta - theta).abs < 1E-12
		#ij_cos = (-best_ij[2][1]*best_ij[3][0]+best_ij[2][0]*best_ij[3][1])/Math.sqrt(best_ij[2][0]*best_ij[2][0]+best_ij[2][1]*best_ij[2][1])/Math.sqrt(best_ij[3][0]*best_ij[3][0]+best_ij[3][1]*best_ij[3][1])
		#current_cos = (-s_data[i][3]*s_data[i+1][2]+s_data[i][2]*s_data[i+1][3])/Math.sqrt(s_data[i][2]*s_data[i][2]+s_data[i][3]*s_data[i][3])/Math.sqrt(s_data[i+1][2]*s_data[i+1][2]+s_data[i+1][3]*s_data[i+1][3])
		if compare(best_ij[2], best_ij[3], s_data[i][2,2], s_data[i+1][2,2]) == -1
			#puts "update #{s_data[i][0]},#{s_data[i+1][0]}"
			best_ij = [s_data[i][0], s_data[i+1][0], s_data[i][2,2], s_data[i+1][2,2]]
			best_theta = theta
		end
	end
end

last_theta = (s_data.first)[1] - (s_data.last)[1] + 2 * Math::PI
if (last_theta < best_theta)
	puts "#{(s_data.last)[0]+1} #{(s_data.first)[0]+1}"
else
	puts "#{best_ij[0]+1} #{best_ij[1]+1}"
end


You are given a set of points $$$x_1$$$, $$$x_2$$$, ..., $$$x_n$$$ on the number line.Two points $$$i$$$ and $$$j$$$ can be matched with each other if the following conditions hold:  neither $$$i$$$ nor $$$j$$$ is matched with any other point;  $$$|x_i - x_j| \ge z$$$. What is the maximum number of pairs of points you can match with each other?

The first line contains two integers $$$n$$$ and $$$z$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$1 \le z \le 10^9$$$) — the number of points and the constraint on the distance between matched points, respectively. The second line contains $$$n$$$ integers $$$x_1$$$, $$$x_2$$$, ..., $$$x_n$$$ ($$$1 \le x_i \le 10^9$$$).

Print one integer — the maximum number of pairs of points you can match with each other.

NoteIn the first example, you may match point $$$1$$$ with point $$$2$$$ ($$$|3 - 1| \ge 2$$$), and point $$$3$$$ with point $$$4$$$ ($$$|7 - 3| \ge 2$$$).In the second example, you may match point $$$1$$$ with point $$$3$$$ ($$$|5 - 10| \ge 5$$$).

n,z = gets.split.map(&:to_i)
x = gets.split.map(&:to_i).sort
match = []
ans = 0
(n/2).times do |i|
  idx = (0..n-1).bsearch{|j| x[j] >= x[i]+z}
  match[i] = idx.nil? ? -1 : n-idx+i
  #p [i,idx]
end

#p match
min = n/2
(n/2).times do |i|
  break if (i+1) > [match[i],min].min
  ans += 1
  min = match[i] if min > match[i]
end
p ans

n,z = gets.split.map(&:to_i)
x = gets.split.map(&:to_i).sort
match = []
ans = 0
(n/2).times do |i|
  idx = (0..n-1).bsearch{|j| x[j] >= x[i]+z}
  match[i] = idx.nil? ? -1 : n-idx
end

matchable = n/2
(n/2).times do |i|
  matchable = match[i]+i if matchable > match[i]+i
  break if (i+1) > matchable
  ans += 1
end
p ans

N, Z = gets.split.map(&:to_i)
arr = gets.split.map(&:to_i)
arr.sort!
p1, p2 = 0, N / 2
answer = 0
while p2 < N
	break if p1 == N / 2
	if arr[p2] - arr[p1] >= Z
		answer += 1
		p1 += 1
		p2 += 1
	else
		p2 += 1
	end
end
puts answer

n,z = gets.split.map(&:to_i)
x = gets.split.map(&:to_i).sort
used = [false]*n
ans = 0
n.times do |i|
  if !used[i]
    idx = (0..n-1).bsearch{|j| x[j] >= x[i]+z}
    if !idx.nil?
      used[i] = true
      used[idx] = true
      ans += 1
    end
  end
end
#p used
p ans

n,z = gets.split.map(&:to_i)
x = gets.split.map(&:to_i).sort
match = []
ans = 0
(n/2).times do |i|
  idx = (0..n-1).bsearch{|j| x[j] >= x[i]+z}
  match[i] = idx.nil? ? -1 : n-idx+i
end

#p match
(n/2).times do |i|
  break if (i+1) > match[i]
  ans += 1
end
p ans

n,z = gets.split.map(&:to_i)
x = gets.split.map(&:to_i).sort
match = []
ans = 0
(n/2).times do |i|
  idx = (0..n-1).bsearch{|j| x[j] >= x[i]+z}
  match[i] = idx.nil? ? -1 : n-idx
end

ans = 0
tmp = n/2
(n/2).times do |i|
  c = tmp < match[i]+i ? tmp : match[i]+i
  break if (i+1) > c || match[i] < 0
  ans += 1
end
p ans

n,z = gets.split.map(&:to_i)
x = gets.split.map(&:to_i).sort
match = []
ans = 0
(n/2).times do |i|
  idx = (0..n-1).bsearch{|j| x[j] >= x[i]+z}
  match[i] = idx.nil? ? -1 : n-idx
end

ans = n/2
(n/2).times do |i|
  t = ans < match[i]+i ? ans : match[i]+i
  break if (i+1) > t
  ans = t
end
p ans

N, Z = gets.split.map(&:to_i)
arr = gets.split.map(&:to_i).sort
p1, p2 = 0, 1
answer = 0
while p2 < N
	if arr[p1]
		if arr[p2]
			if arr[p2] - arr[p1] >= Z
				answer += 1
				arr[p2] = arr[p1] = nil
				p1 += 1
				p2 += 1
			else
				p2 += 1
			end
		else
			p2 += 1
		end
	else
		p1 += 1
	end
end
puts answer

N, Z = gets.split.map(&:to_i)
arr = gets.split.map(&:to_i)
arr.sort!
p1, p2 = 0, N / 2
answer = 0
while p2 < N
	if arr[p2] - arr[p1] >= Z
		answer += 1
		p1 += 1
		p2 += 1
	else
		p2 += 1
	end
end
puts answer

N, Z = gets.split.map(&:to_i)
arr = gets.split.map(&:to_i).sort
p1, p2 = 0, 1
answer = 0
while p2 < N
	if arr[p2]
		if arr[p1]
			if arr[p2] - arr[p1] >= Z
				answer += 1
				arr[p2] = arr[p1] = nil
				p1 += 1
				p2 += 1
			else
				p2 += 1
			end
		else
			p1 += 1
		end
	else
		p2 += 1
	end
end
puts answer


Recently Luba bought a very interesting book. She knows that it will take t seconds to read the book. Luba wants to finish reading as fast as she can.But she has some work to do in each of n next days. The number of seconds that Luba has to spend working during i-th day is ai. If some free time remains, she can spend it on reading.Help Luba to determine the minimum number of day when she finishes reading.It is guaranteed that the answer doesn't exceed n.Remember that there are 86400 seconds in a day.

The first line contains two integers n and t (1 ≤ n ≤ 100, 1 ≤ t ≤ 106) — the number of days and the time required to read the book. The second line contains n integers ai (0 ≤ ai ≤ 86400) — the time Luba has to spend on her work during i-th day.

Print the minimum day Luba can finish reading the book. It is guaranteed that answer doesn't exceed n.

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

SECONDS = 86400
num = 0
result = 0

while t > 0
	t -= (SECONDS - arr[result])
	result += 1
end

puts result

n, t = gets.strip.split.map(&:to_i)
a = gets.strip.split.map(&:to_i)
ans = 0
n.times do |i|
    free = 86400 - a[i]
    t -= free
    ans += 1
    break if t <= 0
end
puts ans

n,t=gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
cur = 0
n.times do |i|
    cur += 86400 - a[i]
    if cur >= t
        puts i + 1
        break
    end
end

in_str = gets
n, allt = in_str.split(' ')
n, allt = n.to_i, allt.to_i
in_str = gets
time_arr = in_str.split(' ').map { |element| element.to_i }
t = 0
time_arr.each_with_index do |elem, i|
  t += 86400 - elem
  if t >= allt
    puts i + 1
    break
  end
end

n, t= gets.split(" ").map(&:to_i)
day = 1
a = gets.split(" ").map(&:to_i)
a.each do |work_time|
  t = t-86400+work_time
  day+=1 if t>0
end
print(day)

n,t=gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
cur = 0
n.times do |i|
    cur += 86400 - a[i]
    if cur >= t
        puts i + 1
    end
end


A new agent called Killjoy invented a virus COVID-2069 that infects accounts on Codeforces. Each account has a rating, described by an integer (it can possibly be negative or very large).Killjoy's account is already infected and has a rating equal to $$$x$$$. Its rating is constant. There are $$$n$$$ accounts except hers, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th account's initial rating is $$$a_i$$$. Any infected account (initially the only infected account is Killjoy's) instantly infects any uninfected account if their ratings are equal. This can happen at the beginning (before any rating changes) and after each contest. If an account is infected, it can not be healed.Contests are regularly held on Codeforces. In each contest, any of these $$$n$$$ accounts (including infected ones) can participate. Killjoy can't participate. After each contest ratings are changed this way: each participant's rating is changed by an integer, but the sum of all changes must be equal to zero. New ratings can be any integer.Find out the minimal number of contests needed to infect all accounts. You can choose which accounts will participate in each contest and how the ratings will change.It can be proven that all accounts can be infected in some finite number of contests.

The first line contains a single integer $$$t$$$ $$$(1 \le t \le 100)$$$ — the number of test cases. The next $$$2t$$$ lines contain the descriptions of all test cases. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$2 \le n \le 10^3$$$, $$$-4000 \le x \le 4000$$$) — the number of accounts on Codeforces and the rating of Killjoy's account. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(-4000 \le a_i \le 4000)$$$ — the ratings of other accounts.

For each test case output the minimal number of contests needed to infect all accounts.

NoteIn the first test case it's possible to make all ratings equal to $$$69$$$. First account's rating will increase by $$$1$$$, and second account's rating will decrease by $$$1$$$, so the sum of all changes will be equal to zero.In the second test case all accounts will be instantly infected, because all ratings (including Killjoy's account's rating) are equal to $$$4$$$.

T = gets.to_i

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

  if a.all?(x)
    puts 0
  else
    cnt = a.count(x)

    if cnt >= 1
      puts 1
    else
      diff = 0

      a.each do |y|
        diff += x - y
      end

      if diff == 0
        puts 1
      else
        puts 2
      end
    end
  end
end

T = gets.to_i

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

  if a.all?(x)
    puts 0
  else
    cnt = a.count(x)

    if cnt >= 2
      puts 1
    elsif cnt >= 1
      ok = 10_000
      ng = -10_000

      f =-> (y) {
        diff = 0

        a.each do |v|
          diff += y - v
        end

        0 <= diff
      }

      while (ok - ng).abs > 1
        y = (ok + ng) / 2

        if f[y]
          ok = y
        else
          ng = y
        end
      end

      diff = 0

      a.each do |v|
        diff += ok - v
      end

      if diff == 0
        puts 1
      else
        puts 2
      end
    else
      diff = 0

      a.each do |y|
        diff += x - y
      end

      if diff == 0
        puts 1
      else
        puts 2
      end
    end
  end
end

T = gets.to_i

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

  if a.all?(x)
    puts 0
  else
    diff = 0

    a.each do |y|
      diff += y - x
    end

    if diff == 0
      puts 1
    else
      puts 2
    end
  end
end

T = gets.to_i

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

  if a.all?(x)
    puts 0
  else
    cnt = a.count(x)

    if cnt >= 2
      puts 1
    elsif cnt >= 1
      ok = 10 ** 10
      ng = -1 * (10 ** 10)

      f =-> (y) {
        diff = 0

        a.each do |v|
          diff += y - v
        end

        0 <= diff
      }

      while (ok - ng).abs > 1
        y = (ok + ng) / 2

        if f[y]
          ok = y
        else
          ng = y
        end
      end

      diff = 0

      a.each do |v|
        diff += ok - v
      end

      if diff == 0
        puts 1
      else
        puts 2
      end
    else
      diff = 0

      a.each do |y|
        diff += x - y
      end

      if diff == 0
        puts 1
      else
        puts 2
      end
    end
  end
end


The All-Berland Team Programming Contest will take place very soon. This year, teams of four are allowed to participate.There are $$$a$$$ programmers and $$$b$$$ mathematicians at Berland State University. How many maximum teams can be made if:  each team must consist of exactly $$$4$$$ students,  teams of $$$4$$$ mathematicians or $$$4$$$ programmers are unlikely to perform well, so the decision was made not to compose such teams. Thus, each team must have at least one programmer and at least one mathematician.Print the required maximum number of teams. Each person can be a member of no more than one team.

The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) —the number of test cases. This is followed by descriptions of $$$t$$$ sets, one per line. Each set is given by two integers $$$a$$$ and $$$b$$$ ($$$0 \le a,b \le 10^9$$$).

Print $$$t$$$ lines. Each line must contain the answer to the corresponding set of input data — the required maximum number of teams.

NoteIn the first test case of the example, two teams can be composed. One way to compose two teams is to compose two teams of $$$2$$$ programmers and $$$2$$$ mathematicians.In the second test case of the example, only one team can be composed: $$$3$$$ programmers and $$$1$$$ mathematician in the team.

t = gets.to_i
ans = []
t.times do
  a,b = gets.split.map(&:to_i).sort
  cnt = [a,(b-a)/2].min
  a -= cnt
  cnt += a / 2
  ans << cnt
end
puts ans


The elections in which three candidates participated have recently ended. The first candidate received $$$a$$$ votes, the second one received $$$b$$$ votes, the third one received $$$c$$$ votes. For each candidate, solve the following problem: how many votes should be added to this candidate so that he wins the election (i.e. the number of votes for this candidate was strictly greater than the number of votes for any other candidate)?Please note that for each candidate it is necessary to solve this problem independently, i.e. the added votes for any candidate do not affect the calculations when getting the answer for the other two candidates.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of one line containing three integers $$$a$$$, $$$b$$$, and $$$c$$$ ($$$0 \le a,b,c \le 10^9$$$).

For each test case, output in a separate line three integers $$$A$$$, $$$B$$$, and $$$C$$$ ($$$A, B, C \ge 0$$$) separated by spaces — the answers to the problem for the first, second, and third candidate, respectively.

t = gets.chomp.to_i
t.times {
	a, b, c = gets.split.map(&:to_i)
	if (a == b) && (c == a)
		puts "1 1 1"
		next
	end
	aa = [a, [b, c].max + 1].max - a
	bb = [b, [a, c].max + 1].max - b
	cc = [c, [a, b].max + 1].max - c
	puts "#{aa} #{bb} #{cc}"
}

gets.to_i.times do
    arr = gets.split.each_with_index.map{ |c, i| c.to_i  }
    mx = arr.max
    c = arr.count(mx)
    puts arr.map{|e| e != mx ? mx - e + 1 : c > 1 ? 1 : 0}.join ' '

end

class Pair
    attr_accessor :first, :second
    include Comparable

    def initialize(first, second)
        @first = first
        @second = second
    end

    def <=>(an_other)
        @first <=> an_other.first
    end
end
gets.to_i.times do
    arr = gets.split.each_with_index.map{ |c, i| Pair.new(c.to_i, i)  }
    arr = arr.sort()
    ans = Array.new;
    arr.length.times do |idx|
        if idx == arr.length - 1
            if arr.length >= 2 and arr[arr.length - 1].first == arr[arr.length - 2].first
                ans[ arr[arr.length - 1].second ] = 1;
            else
                ans[ arr[arr.length - 1].second ] = 0;
            end
        else
            ans[ arr[idx].second ] = arr[arr.length-1].first - arr[idx].first + 1;
        end
    end

    arr.length.times do |idx|
        if idx == arr.length - 1
            puts(ans[idx])
        else
            print(ans[idx])
            print(" ")
        end
    end

end

gets.to_i.times do
    votes = gets.split.map(&:to_i)
    m = votes.max
    c = votes.count(m)
    puts votes.map{|v| v != m ? m - v + 1 : c > 1 ? 1 : 0}.join ' '
end

inputs = STDIN.read.strip.split("\n")
inputs.shift

inputs.each do |inp|
  cans = inp.split(' ').map(&:to_i)

  r = []
  cans.each_with_index do |n, i|
    other = cans.reject.with_index{|_, j| j == i }
    max = other.max

    if n > max
      r << 0
    else
      r << max - n + 1
    end
  end

  puts r.join(' ')
end

t = gets().to_i()

def bar(arr)

	solve = []

	max = arr.max
	max_counter = 0

	for i in 0..arr.length - 1
		if (arr[i] == max)
			max_counter += 1
		end
	end

	for i in 0..arr.length - 1
		tmp = -1
		if (arr[i] == max)
			if (max_counter == 1)
				tmp = 0
			else
				tmp = 1
			end
		else
			tmp = max - arr[i] + 1
		end

		solve << tmp
	end
	puts solve.join(" ")
end

for i in 1..t
	arr = gets.chomp.split(" ").map{|item| item.to_i}

	bar(arr)

end

n = gets.chomp.to_i

n.times do
    votes = gets.chomp.split(' ').map(&:to_i)

    a = [[votes[1], votes[2]].max - votes[0] + 1, 0].max
    b = [[votes[0], votes[2]].max - votes[1] + 1, 0].max
    c = [[votes[0], votes[1]].max - votes[2] + 1, 0].max

    puts "#{a} #{b} #{c}"
end

def f(x, y, z)
  w = [y, z].max - x + 1
  w > 0 ? w : 0
end

gets.to_i.times do

a, b, c = gets.split.map!(&:to_i)
puts "#{f(a,b,c)} #{f(b,c,a)} #{f(c,a,b)}"

end

t = gets.chomp.to_i
t.times {
	a, b, c = gets.split.map(&:to_i)
	if (a == b) && (c == a)
		puts "#{a+1} #{b+1} #{c+1}"
		next
	end
	aa = [a, [b, c].max + 1].max - a
	bb = [b, [a, c].max + 1].max - b
	cc = [c, [a, b].max + 1].max - c
	puts "#{aa} #{bb} #{cc}"
}

t = gets.chomp.to_i
t.times {
	a, b, c = gets.split.map(&:to_i)
	if (a == b) && (c == b) && (c == a)
		puts "#{a+1} #{b+1} #{c+1}"
		next
	end
	ma = [a, b, c].max
	if ma - a == 0
		a = 0
	else
		a = ma - a + 1
	end
	if ma - b == 0
		b = 0
	else
		b = ma - b + 1
	end
	if ma - c == 0
		c = 0
	else
		c = ma - c + 1
	end
	puts "#{a} #{b} #{c}"
	# p [a, b, c].join' '
}

t = gets.chomp.to_i
t.times {
	a, b, c = gets.split.map(&:to_i)
	if (a == b) && (c == b) && (c == a)
		puts "#{a+1} #{b+1} #{c+1}"
		next
	end
	ma = [a, b, c].max
	if ma - a == 0
		a = 0
	else
		a = ma - a + 1
	end
	if ma - b == 0
		b = 0
	else
		b = ma - b + 1
	end
	if ma - c == 0
		c = 0
	else
		c = ma - c + 1
	end
	# puts "#{a} #{b} #{c}"
	p [a, b, c].join' '
}

t = gets.chomp.to_i
t.times {
	a, b, c = gets.split.map(&:to_i)
	if (a == b) && (c == b) && (c == a)
		puts a+1, a+1, a+1
		next
	end
	ma = [a, b, c].max
	if ma - a == 0
		a = 0
	else
		a = ma - a + 1
	end
	if ma - b == 0
		b = 0
	else
		b = ma - b + 1
	end
	if ma - c == 0
		c = 0
	else
		c = ma - c + 1
	end
	puts "#{a} #{b} #{c}"
}

inputs = STDIN.read.strip.split("\n")
inputs.shift

inputs.each do |inp|
  cans = inp.split(' ').map(&:to_i)

  r = []
  max = cans.max
  min = cans.min
  cans.each do |n|
    r << max - n + (max != n || max == min ? 1 : 0)
  end

  puts r.join(' ')
end

t = gets().to_i()

def bar(arr)

	max = arr.max

	solve = []

	for i in 0..arr.length - 1
		tmp = -1
		if (arr[i] == max)
			tmp = 0
		else
			tmp = max - arr[i] + 1
		end

		solve << tmp
	end
	puts solve.join(" ")
end

for i in 1..t
	arr = gets.chomp.split(" ").map{|item| item.to_i}

	bar(arr)

end


You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&10&11\\ 11&12&14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&1\\ 2&3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.

Print a string "Impossible" or "Possible".

NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&10\\ 11&12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&4\\ 3&5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.

n,m = gets.split.map(&:to_i)
first,second = [],[]
n.times do
  first << gets.split.map(&:to_i)
end
n.times do
  second << gets.split.map(&:to_i)
end
n.times do |i|
  m.times do |j|
    if first[i][j] > second[i][j]
      first[i][j],second[i][j] = second[i][j],first[i][j]
    end
  end
end
flg = true
n.times do |i|
  m.times do |j|
    if i < n-1 && (first[i][j] >= first[i+1][j] || second[i][j] >= second[i+1][j])
      flg = false
      break
    end
    if j < m-1 && (first[i][j] >= first[i][j+1] || second[i][j] >= second[i][j+1])
      flg = false
      break
    end
  end
end

puts flg ? "Possible" : "Impossible"


John Doe started thinking about graphs. After some thought he decided that he wants to paint an undirected graph, containing exactly k cycles of length 3. A cycle of length 3 is an unordered group of three distinct graph vertices a, b and c, such that each pair of them is connected by a graph edge. John has been painting for long, but he has not been a success. Help him find such graph. Note that the number of vertices there shouldn't exceed 100, or else John will have problems painting it.

A single line contains an integer k (1 ≤ k ≤ 105) — the number of cycles of length 3 in the required graph.

In the first line print integer n (3 ≤ n ≤ 100) — the number of vertices in the found graph. In each of next n lines print n characters "0" and "1": the i-th character of the j-th line should equal "0", if vertices i and j do not have an edge between them, otherwise it should equal "1". Note that as the required graph is undirected, the i-th character of the j-th line must equal the j-th character of the i-th line. The graph shouldn't contain self-loops, so the i-th character of the i-th line must equal "0" for all i.

def c(n, m)
  c = 1
  m.times do |i|
    c = c * (n - i) / (i + 1)
  end
  c
end

# c(n, 3) - c(n - 1, 3) = c(n - 1, 2) !!
# c(n, 2) - c(n - 1, 2) = c(n - 1, 1) = n - 1 !!

m = gets.to_i
k = 3
k += 1 while c(k + 1, 3) <= m
m -= c(k, 3)

a = []
while m > 0
  b = 2
  b += 1 while c(b + 1, 2) <= m
  m -= c(b, 2)
  a << b
end

n = k + a.size
e = Array.new(n){'0' * n}
k.times do |i|
  k.times do |j|
    e[i][j] = '1' if i != j
  end
end
a.each_with_index do |c, j|
  c.times do |i|
    e[i][k + j] = '1'
    e[k + j][i] = '1'
  end
end
p n
puts e * "\n"

n=gets.to_i
a=Array.new(101){|el| el=Array.new(101){|elem| elem=0}}
c,y=0,0
for i in 0..101 do
y=0
	while y<i do
		break if y>n
		a[i][y],a[y][i]=1,1
		n-=y
		y+=1
	end
c+=1
break if n==0
end
puts c
for i in 0..c-1 do
	for j in 0..c-1 do
		print "#{a[i][j]}"
	end
	printf("\n")
end

x = gets.to_i
d = Array.new(105) { Array.new(105) {0} }
cnt = Math.log((x * 2 * 3), 3).floor
while (cnt + 1) * (cnt + 2) * (cnt + 3) <= x * 6
  cnt += 1
end
while (cnt) * (cnt + 1) * (cnt + 2) > x * 6
  cnt -= 1
end
cnt += 2
0.upto(cnt - 2) do |i|
  (i + 1).upto(cnt - 1) do |j|
    d[i][j] = d[j][i] = 1
  end
end
p = cnt - 1
x -= cnt * (cnt - 1) * (cnt - 2) / 6
while x > 0
  while p * (p - 1) / 2 > x
    p -= 1
  end
  0.upto(p - 1) do |i|
    d[cnt][i] = d[i][cnt] = 1
  end
  cnt += 1
  x -= p * (p - 1) / 2
end
puts cnt
0.upto(cnt - 1) do |i|
  0.upto(cnt - 1) do |j|
    print "#{d[i][j]}"
  end
  puts ''
end

def c(n, m)
  c = 1
  m.times do |i|
    c = c * (n - i) / (i + 1)
  end
  c
end

# c(n, 3) - c(n - 1, 3) = c(n - 1, 2) !!
# c(n, 2) - c(n - 1, 2) = c(n - 1, 1) = n - 1 !!

m = gets.to_i
k = 3
k += 1 while c(k + 1, 3) <= m
m -= c(k, 3)

a = []
while m > 0
  b = 2
  b += 1 while c(b + 1, 2) <= m
  m -= c(k, 2)
  a << b
end

n = k + a.size
e = Array.new(n){'0' * n}
k.times do |i|
  k.times do |j|
    e[i][j] = '1' if i != j
  end
end
a.each_with_index do |c, j|
  c.times do |i|
    e[i][k + j] = '1'
    e[k + j][i] = '1'
  end
end
p n
puts e * "\n"

n=gets.to_i
a=Array.new(101){|el| el=Array.new(101){|elem| elem=0}}
c,y=0,0
for i in 0..101 do
y=0
	while y<i do
		break if y>n
		a[i][y],a[y][i]=1,1
		n-=y
		y+=1
	end
c+=1
break if n==0
end
puts c
for i in 0..c-1 do
	for j in 0..c-1 do
		print "#{a[i][j]} "
	end
	printf("\n")
end

x = gets.to_i
d = Array.new(105) { Array.new(105) {0} }
cnt = Math.log((x * 2 * 3), 3).floor
while (cnt + 1) * (cnt + 2) * (cnt + 3) <= x * 6
  cnt += 1
end
while (cnt) * (cnt + 1) * (cnt + 2) > x * 6
  cnt -= 1
end
cnt += 2
0.upto(cnt - 2) do |i|
  (i + 1).upto(cnt - 1) do |j|
    d[i][j] = d[j][i] = 1
  end
end
x -= cnt * (cnt - 1) * (cnt - 2) / 6
while x > 0
  p = (x >= cnt / 2) ? (cnt / 2) : x
  x -= p
  0.upto(p - 1) do |i|
    d[2 * i][cnt] = d[2 * i + 1][cnt] = d[cnt][2 * i] = d[cnt][2 * i + 1] = 1
  end
  cnt += 1
end
puts cnt
0.upto(cnt - 1) do |i|
  0.upto(cnt - 1) do |j|
    print "#{d[i][j]}"
  end
  puts ''
end


You still have partial information about the score during the historic football match. You are given a set of pairs $$$(a_i, b_i)$$$, indicating that at some point during the match the score was "$$$a_i$$$: $$$b_i$$$". It is known that if the current score is «$$$x$$$:$$$y$$$», then after the goal it will change to "$$$x+1$$$:$$$y$$$" or "$$$x$$$:$$$y+1$$$". What is the largest number of times a draw could appear on the scoreboard?The pairs "$$$a_i$$$:$$$b_i$$$" are given in chronological order (time increases), but you are given score only for some moments of time. The last pair corresponds to the end of the match.

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10000$$$) — the number of known moments in the match. Each of the next $$$n$$$ lines contains integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \le a_i, b_i \le 10^9$$$), denoting the score of the match at that moment (that is, the number of goals by the first team and the number of goals by the second team). All moments are given in chronological order, that is, sequences $$$x_i$$$ and $$$y_j$$$ are non-decreasing. The last score denotes the final result of the match.

Print the maximum number of moments of time, during which the score was a draw. The starting moment of the match (with a score 0:0) is also counted.

NoteIn the example one of the possible score sequences leading to the maximum number of draws is as follows: 0:0, 1:0, 2:0, 2:1, 3:1, 3:2, 3:3, 3:4.

n = gets.to_i
pa,pb = 0,0
ans = 1
n.times do
  a,b = gets.split.map(&:to_i)
  if a == b
    ans += a-[pa,pb].max+(pa==pb ? 0 : 1)
  elsif a > b && pa <= b
    ans += b-[pa,pb].max+(pa==pb ? 0 : 1)
  elsif a < b && pb <= a
    ans += a-[pa,pb].max+(pa==pb ? 0 : 1)
  end
  pa,pb = a,b
end
p ans

DBG = !true
n = gets.to_i
sum = 0
pends = -1
pende = -1
for i in 0...n
  a,b = gets.split.map{|z| z.to_i}
  mn = [a,b].min
  mx = [a,b].max
  if pende != -1 && mn >= pends
      sum += [mn, pende].min - pends + 1
  end
  if mx <= mn + 1
    pende = -1
    pends = -1
  else
    pends = mn + 1
    pende = mx - 1
  end
  puts "a #{a} b #{b} sum #{sum} pends #{pende}" if DBG
end
puts "#{[a,b].min - sum + 1}"

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

n = gets.to_i
arr = []
n.times do
	arr.push gets.chomp.split(" ").map(&:to_i)
end

prevx = 0
prevy = 0

def inrange(a,b,x)
	return true if x>=a and x<=b
end

ans = 0

maxtie = -1

arr.each do |z|
	x = z[0]
	y = z[1]

#	if(prevx==x && prevy==y)
#		next
#	end

	if(y<prevx || x<prevy)
		prevx = x
		prevy = y
		prevx+=1 if prevx==maxtie
		prevy+=1 if prevy==maxtie
		next
	elsif(x==y && prevx==prevy)
		ans+=x-prevx+1
		prevx=x+1
		prevy=y+1
		maxtie=x
		next
	else
		if(inrange(prevx,x,prevy))
			if(inrange(prevx,x,y))
				ans+=y-prevy+1
				maxtie = y
			else
				ans+=x-prevy+1
				maxtie = x
			end
		elsif(inrange(prevx,x,y))
			ans+=y-prevx+1
			maxtie = y
		else#(inrange(prevy,y,prevx)&&inrange(prevy,y,x))
			ans+=x-prevx+1
			maxtie = x
		end

	end
=begin
	p x
	p y
	p prevx
	p prevy
#	p toincx
#	p toincy
	p maxtie
	p ans
	puts
=end
	prevx=x
	prevy=y

	prevx+=1 if prevx==maxtie
	prevy+=1 if prevy==maxtie

end

#if arr[n-1][0]==0 and arr[n-1][1]==0
#	ans+=1
#end
puts ans

n = gets.to_i
pa,pb = 0,0
ans = 1
n.times do
  a,b = gets.split.map(&:to_i)
  if a == b
    ans += a-[pa,pb].max+(pa==pb ? 0 : 1)
  elsif a > b && pa <= b
    ans += b-[pa,pb].max+(pa==pb ? 0 : 1)
  elsif a < b && pb <= a
    ans += a-[pa,pb].max+(pa==pb ? 0 : 1)
  end
  pa,pb = a,b
  p ans
end
p ans

DBG = true
n = gets.to_i
sum = 0
prev = -1
for i in 0...n
  a,b = gets.split.map{|z| z.to_i}
  d = [[a,b].min, prev].max
  sum += [0, [a,b].max - d - 1].max
  prev = (a==b ? a : ([a,b].max - 1))
end
puts "#{[a,b].min + 1 - sum}"

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

n = gets.to_i
arr = []
n.times do
	arr.push gets.chomp.split(" ").map(&:to_i)
end

prevx = 0
prevy = 0
start=1

def inrange(a,b,x)
	return true if x>=a and x<=b
end

ans = 0
arr.each do |z|
	x = z[0]
	y = z[1]

	if(prevx==x && prevy==y)
		next
	end
	start=0

	if(y<prevx || x<prevy)
		prevx = x
		prevy = y
		next
	else
		if(inrange(prevx,x,prevy))
			if(inrange(prevx,x,y))
				ans+=y-prevy+1
			else
				ans+=x-prevy+1
			end
		end
		if(inrange(prevy,y,prevx)&&inrange(prevy,y,x))
			ans+=x-prevx+1
		end
	end
=begin
	p x
	p y
	p prevx
	p prevy
	p ans
	puts
=end
	prevx=x
	prevy=y
end
if arr[n-1][0]==0 and arr[n-1][1]==0
	ans+=1
end
puts ans

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

n = gets.to_i
arr = []
n.times do
	arr.push gets.chomp.split(" ").map(&:to_i)
end

prevx = 0
prevy = 0

def inrange(a,b,x)
	return true if x>=a and x<=b
end

ans = 1
arr.each do |z|
	x = z[0]
	y = z[1]

	if(prevx==x && prevy==y)
		next
	end

	if(y<prevx || x<prevy)
		prevx = x
		prevy = y
		next
	else
		if(inrange(prevx,x,prevy))
			if(inrange(prevx,x,y))
				ans+=y-prevy
			else
				ans+=x-prevy
			end
		end
		if(inrange(prevy,y,prevx)&&inrange(prevy,y,x) && !(prevx==prevy && x==y))
			ans+=x-prevx+1
		end
	end
=begin
	p x
	p y
	p prevx
	p prevy
	p ans
	puts
=end
	prevx=x
	prevy=y
end

#if arr[n-1][0]==0 and arr[n-1][1]==0
#	ans+=1
#end
puts ans

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

n = gets.to_i
arr = []
n.times do
	arr.push gets.chomp.split(" ").map(&:to_i)
end

prevx = 0
prevy = 0

def inrange(a,b,x)
	return true if x>=a and x<=b
end

ans = 0

maxtie = -1

arr.each do |z|
	x = z[0]
	y = z[1]

	if(prevx==x && prevy==y)
		next
	end

	if(y<prevx || x<prevy)
		prevx = x
		prevy = y
		next
	else
		if(inrange(prevx,x,prevy))
			if(inrange(prevx,x,y))
				ans+=y-prevy+1
				maxtie = y
			else
				ans+=x-prevy+1
				maxtie = x
			end
		elsif(inrange(prevx,x,y))
			ans+=y-prevx+1
			maxtie = y
		end

		if(inrange(prevy,y,prevx)&&inrange(prevy,y,x) && !(prevx==prevy && x==y))
			ans+=x-prevx+1
			maxtie = x
		end
	end
=begin
	p x
	p y
	p prevx
	p prevy
	p toincx
	p toincy
	p ans
	puts
=end
	prevx=x
	prevy=y

	prevx+=1 if prevx==maxtie
	prevy+=1 if prevy==maxtie

end

if arr[n-1][0]==0 and arr[n-1][1]==0
	ans+=1
end
puts ans

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

n = gets.to_i
arr = []
n.times do
	arr.push gets.chomp.split(" ").map(&:to_i)
end

prevx = 0
prevy = 0

def inrange(a,b,x)
	return true if x>=a and x<=b
end

ans = 0

maxtie = -1

arr.each do |z|
	x = z[0]
	y = z[1]

#	if(prevx==x && prevy==y)
#		next
#	end

	if(y<prevx || x<prevy)
		prevx = x
		prevy = y
		prevx+=1 if prevx==maxtie
		prevy+=1 if prevy==maxtie
		next
	else
		if(inrange(prevx,x,prevy))
			if(inrange(prevx,x,y))
				ans+=y-prevy+1
				maxtie = y
			else
				ans+=x-prevy+1
				maxtie = x
			end
		elsif(inrange(prevx,x,y))
			ans+=y-prevx+1
			maxtie = y
		end

		if(inrange(prevy,y,prevx)&&inrange(prevy,y,x) && !(prevx==prevy && x==y))
			ans+=x-prevx+1
			maxtie = x
		end
	end
=begin
	p x
	p y
	p prevx
	p prevy
#	p toincx
#	p toincy
	p maxtie
	p ans
	puts
=end
	prevx=x
	prevy=y

	prevx+=1 if prevx==maxtie
	prevy+=1 if prevy==maxtie

end

#if arr[n-1][0]==0 and arr[n-1][1]==0
#	ans+=1
#end
puts ans


You are given a system of pipes. It consists of two rows, each row consists of $$$n$$$ pipes. The top left pipe has the coordinates $$$(1, 1)$$$ and the bottom right — $$$(2, n)$$$.There are six types of pipes: two types of straight pipes and four types of curved pipes. Here are the examples of all six types:  Types of pipes You can turn each of the given pipes $$$90$$$ degrees clockwise or counterclockwise arbitrary (possibly, zero) number of times (so the types $$$1$$$ and $$$2$$$ can become each other and types $$$3, 4, 5, 6$$$ can become each other).You want to turn some pipes in a way that the water flow can start at $$$(1, 0)$$$ (to the left of the top left pipe), move to the pipe at $$$(1, 1)$$$, flow somehow by connected pipes to the pipe at $$$(2, n)$$$ and flow right to $$$(2, n + 1)$$$.Pipes are connected if they are adjacent in the system and their ends are connected. Here are examples of connected pipes:  Examples of connected pipes Let's describe the problem using some example:  The first example input And its solution is below:   The first example answer As you can see, the water flow is the poorly drawn blue line. To obtain the answer, we need to turn the pipe at $$$(1, 2)$$$ $$$90$$$ degrees clockwise, the pipe at $$$(2, 3)$$$ $$$90$$$ degrees, the pipe at $$$(1, 6)$$$ $$$90$$$ degrees, the pipe at $$$(1, 7)$$$ $$$180$$$ degrees and the pipe at $$$(2, 7)$$$ $$$180$$$ degrees. Then the flow of water can reach $$$(2, n + 1)$$$ from $$$(1, 0)$$$.You have to answer $$$q$$$ independent queries.

The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of queries. Then $$$q$$$ queries follow. Each query consists of exactly three lines. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of pipes in each row. The next two lines contain a description of the first and the second rows correspondingly. Each row description consists of $$$n$$$ digits from $$$1$$$ to $$$6$$$ without any whitespaces between them, each digit corresponds to the type of pipe in the corresponding cell. See the problem statement to understand which digits correspond to which types of pipes. It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 \cdot 10^5$$$.

For the $$$i$$$-th query print the answer for it — "YES" (without quotes) if it is possible to turn some pipes in a way that the water flow can reach $$$(2, n + 1)$$$ from $$$(1, 0)$$$, and "NO" otherwise.

NoteThe first query from the example is described in the problem statement.

q = gets.to_i
q.times{
  n = gets.to_i
  data = []
  data << gets.chomp.chars.map(&:to_i).map{|i| i<3}
  data << gets.chomp.chars.map(&:to_i).map{|i| i<3}

  state = true
  ans = true
  pos_r = 0 # 0..1
  pos_c = 0 # 0..n-1
  until pos_c==n
    if state && data[pos_r][pos_c]
      pos_c += 1
    elsif (state && (!data[pos_r][pos_c]))
      state = false
      pos_r = pos_r==0 ? 1 : 0
    elsif (!state) && data[pos_r][pos_c]
      ans = false
      break
    else
      state = true
      pos_c += 1
    end
  end
  puts (ans && state && pos_r==1) ? :YES : :NO
}

MOD=10**9+7
cnt=0; sum=0; prev=nil; can=true; h=Hash.new(0)
def gs() gets.chomp end
def gi() gets.to_i end
def gsmi() gets.chomp.split.map(&:to_i) end

def desc(ar) ar.sort!{|x,y|y<=>x} end
def min(a,b) a<=b ? a : b end
def max(a,b) a>=b ? a : b end
def sum(ar) ar.inject(:+) end

#def bs(ar,eq,n) en=(eq==1 ? :>= : :>); ar.bsearch_index{|x|x.send(en,n)} end
def C(a,b) b==0||a==b ? 1 : (b=a-b if a/2<b;(a-b+1..a).inject(:*)/(1..b).inject(:*)) end

def puts_yesno(can) puts(can ? 'Yes' : 'No') end
def putsend(s) puts s; exit end
def debug(k,v) puts "#{k}:#{v}" end
########### ( ˘ω˘ )ｽﾔｧ… ###########
def row(i)
  i==0 ? 1 : 0
end

q = gi
ans=[]
q.times do
  n = gi
  p1 = gs.chars.map(&:to_i)
  p2 = gs.chars.map(&:to_i)

  pi=[p1,p2]

  ri=0
  i=0
  fix=false
  while i<n do
    if pi[ri][i]<=2
    else
      ri=row(ri)
      if pi[ri][i]<=2
        ans<<'NO'
        fix=true
        break
      end
    end
    i+=1
  end

  next if fix

  if ri==0
    ans<<'NO'
  else
    ans<<'YES'
  end
end

puts ans


Lunar New Year is approaching, and you bought a matrix with lots of "crosses".This matrix $$$M$$$ of size $$$n \times n$$$ contains only 'X' and '.' (without quotes). The element in the $$$i$$$-th row and the $$$j$$$-th column $$$(i, j)$$$ is defined as $$$M(i, j)$$$, where $$$1 \leq i, j \leq n$$$. We define a cross appearing in the $$$i$$$-th row and the $$$j$$$-th column ($$$1 < i, j < n$$$) if and only if $$$M(i, j) = M(i - 1, j - 1) = M(i - 1, j + 1) = M(i + 1, j - 1) = M(i + 1, j + 1) = $$$ 'X'.The following figure illustrates a cross appearing at position $$$(2, 2)$$$ in a $$$3 \times 3$$$ matrix. X.X.X.X.X Your task is to find out the number of crosses in the given matrix $$$M$$$. Two crosses are different if and only if they appear in different rows or columns.

The first line contains only one positive integer $$$n$$$ ($$$1 \leq n \leq 500$$$), denoting the size of the matrix $$$M$$$. The following $$$n$$$ lines illustrate the matrix $$$M$$$. Each line contains exactly $$$n$$$ characters, each of them is 'X' or '.'. The $$$j$$$-th element in the $$$i$$$-th line represents $$$M(i, j)$$$, where $$$1 \leq i, j \leq n$$$.

Output a single line containing only one integer number $$$k$$$ — the number of crosses in the given matrix $$$M$$$.

NoteIn the first sample, a cross appears at $$$(3, 3)$$$, so the answer is $$$1$$$.In the second sample, no crosses appear since $$$n < 3$$$, so the answer is $$$0$$$.In the third sample, crosses appear at $$$(3, 2)$$$, $$$(3, 4)$$$, $$$(4, 3)$$$, $$$(4, 5)$$$, so the answer is $$$4$$$.

# Input
n = gets.to_i
m = Array.new(n)
n.times { |i| m[i] = gets.split("") }

# Processing
count = 0
1.upto(n - 2) do |i|
  1.upto(n - 2) do |j|
    condition = (m[i][j] == "X") &&
                (m[i - 1][j - 1] == "X") &&
                (m[i - 1][j + 1] == "X") &&
                (m[i + 1][j - 1] == "X") &&
                (m[i + 1][j + 1] == "X")
    count += 1 if condition
  end
end

# Output
puts count

n = gets.strip.to_i
board = Array.new n
n.times do |i|
  board[i] = gets.strip
end
counter = 0
if n > 0
  (1...(n - 1)).each do |i|
    (1...(n - 1)).each do |j|
      counter += 1 if board[i][j] == 'X' \
        && board[i][j] == board[i - 1][j - 1] \
        && board[i][j] == board[i - 1][j + 1] \
        && board[i][j] == board[i + 1][j - 1] \
        && board[i][j] == board[i + 1][j + 1]
    end
  end
end
puts counter

DBG = !true
n = gets.to_i
s = []
for i in 0...n
  s << gets.chomp
  #a,b = gets.split
  #a = a.to_i
  #b = b.to_i
end
print n if DBG
print s if DBG
sum = 0
for i in 1..(n-2)
 for j in 1..(n-2)
   if s[i][j] == 'X' &&
      s[i][j] == s[i-1][j-1] &&
      s[i][j] == s[i-1][j+1] &&
      s[i][j] == s[i+1][j-1] &&
      s[i][j] == s[i+1][j+1]
       sum += 1
    end
  end
end
puts "#{sum}"

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

n = gets.to_i
arr = []
n.times do
	arr.push gets.chomp.split("")
end

ans = 0
(1..n-2).each do |a|
	(1..n-2).each do |b|
		if arr[a][b]=="X" and arr[a-1][b-1]=="X" and arr[a+1][b-1]=="X" and arr[a-1][b+1]=="X" and arr[a+1][b+1]=="X"
			ans += 1
		end
	end
end

puts ans


Немногие знают, что сотрудники ВКонтакте могут менять цвет подсветки в куполе знаменитого Дома Зингера, где расположена штаб-квартира ВКонтакте. Для этого нужно всего лишь отправить сообщение с цветом в специальный чат «Зингер | color», а бот его распознает и сменит подсветку. При этом на время городских мероприятий смена цвета блокируется.Формально, бот обрабатывает три типа сообщений:   lock: заблокировать изменение цвета. Если оно и так заблокировано на данный момент, сообщение игнорируется.  unlock: разблокировать изменение цвета. Если оно и так разблокировано на данный момент, сообщение игнорируется.  red / orange / yellow / green / blue / indigo / violet: изменить цвет купола на заданный, если изменение цвета на данный момент не заблокировано. Вам дана история сообщений, полученных ботом, в хронологическом порядке. Считайте, что перед получением первого сообщения купол подсвечивается голубым (blue), а изменение цвета не заблокировано.Определите, какой цвет будет у купола Дома Зингера после обработки этих сообщений.

В первой строке задано одно целое число $$$n$$$ ($$$1 \le n \le 100$$$) — число сообщений, полученных ботом. В следующих $$$n$$$ строках заданы сообщения, полученные ботом, в хронологическом порядке, по одному сообщению в строке. Каждое сообщение — строка из следующего набора: lock, unlock, red, orange, yellow, green, blue, indigo, violet.

Выведите цвет купола после обработки сообщений ботом.

7
red
violet
unlock
red
orange
lock
indigo

n = Integer(gets)
is_locked = false
ans = "blue"
Lk = 'lock'
ULk = 'unlock'
while n > 0 do
    str1 = gets
    if str1[0] == 'l' and str1[1] == 'o' then
        is_locked = true
    else
        if str1[0] == 'u' and str1[1] == 'n' then
            is_locked = false
        else
            if is_locked == false then
                ans = str1
            end
        end
    end
    n = n - 1
end
print ans

current_color = 'blue'
count = gets.chomp.to_i
colors = []

count.times do
  colors << gets.chomp
end

locked = false
colors.each do |color_item|
  if locked
    locked = false if color_item == 'unlock'
  else
    if color_item == 'lock'
      locked = true
    elsif color_item != 'unlock'
      current_color = color_item
    end
  end
end

puts current_color


The Squareland national forest is divided into equal $$$1 \times 1$$$ square plots aligned with north-south and east-west directions. Each plot can be uniquely described by integer Cartesian coordinates $$$(x, y)$$$ of its south-west corner.Three friends, Alice, Bob, and Charlie are going to buy three distinct plots of land $$$A, B, C$$$ in the forest. Initially, all plots in the forest (including the plots $$$A, B, C$$$) are covered by trees. The friends want to visit each other, so they want to clean some of the plots from trees. After cleaning, one should be able to reach any of the plots $$$A, B, C$$$ from any other one of those by moving through adjacent cleared plots. Two plots are adjacent if they share a side.    For example, $$$A=(0,0)$$$, $$$B=(1,1)$$$, $$$C=(2,2)$$$. The minimal number of plots to be cleared is $$$5$$$. One of the ways to do it is shown with the gray color. Of course, the friends don't want to strain too much. Help them find out the smallest number of plots they need to clean from trees.

The first line contains two integers $$$x_A$$$ and $$$y_A$$$ — coordinates of the plot $$$A$$$ ($$$0 \leq x_A, y_A \leq 1000$$$). The following two lines describe coordinates $$$(x_B, y_B)$$$ and $$$(x_C, y_C)$$$ of plots $$$B$$$ and $$$C$$$ respectively in the same format ($$$0 \leq x_B, y_B, x_C, y_C \leq 1000$$$). It is guaranteed that all three plots are distinct.

On the first line print a single integer $$$k$$$ — the smallest number of plots needed to be cleaned from trees. The following $$$k$$$ lines should contain coordinates of all plots needed to be cleaned. All $$$k$$$ plots should be distinct. You can output the plots in any order. If there are multiple solutions, print any of them.

NoteThe first example is shown on the picture in the legend.The second example is illustrated with the following image:

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

xa = a[0]
ya = a[1]
xb = b[0]
yb = b[1]
xc = c[0]
yc = c[1]

ans = (xa-xb).abs + (ya-yb).abs + 1

rectop = [xa,xb].max
recbot = [xa,xb].min
recright = [ya,yb].max
recleft = [ya,yb].min

userecver = 0

if xc< recbot
  userecver = -1
  ans += recbot-xc
elsif xc > rectop
  userecver = 1
  ans += xc-rectop
end

userecside = 0
if yc< recleft
  userecside = -1
  ans += recleft-yc
elsif yc > recright
  userecside = 1
  ans += yc-recright
end

#ans += 1
puts ans

#puts userecver
#puts userecside

if userecver == -1
  #you definitely need the bottom
  (recleft..recright).each do |k|
    puts "#{recbot} #{k}"
  end

  if xa == recbot
    (recbot+1..xb).each do |k|
      puts "#{k} #{yb}"
    end
  else
    (recbot+1..xa).each do |k|
      puts "#{k} #{ya}"
    end
  end

  #DO THE C STUFF
  if userecside == -1
    (xc..recbot-1).each do |k|
      puts "#{k} #{yc}"
    end
    (yc+1..recleft-1).each do |k|
      puts "#{recbot} #{k}"
    end

  elsif userecside == 1
    (xc..recbot-1).each do |k|
      puts "#{k} #{yc}"
    end
    (recright+1..yc).each do |k|
      puts "#{recbot} #{k}"
    end

  else
    (xc..recbot-1).each do |k|
      puts "#{k} #{yc}"
    end
  end

elsif userecver == 1

  #you definitely need the top
  (recleft..recright).each do |k|
    puts "#{rectop} #{k}"
  end

  if xa == rectop
    (xb..rectop-1).each do |k|
      puts "#{k} #{yb}"
    end
  else
    (xa..rectop-1).each do |k|
      puts "#{k} #{ya}"
    end
  end

  #DO the C stuff
  if userecside == -1
    (rectop+1..xc).each do |k|
      puts "#{k} #{recleft}"
    end
    (yc..recleft-1).each do |k|
      puts "#{xc} #{k}"
    end

  elsif userecside == 1
    (rectop+1..xc).each do |k|
      puts "#{k} #{recright}"
    end
    (recright+1..yc).each do |k|
      puts "#{xc} #{k}"
    end

  else
    (rectop+1..xc).each do |k|
      puts "#{k} #{yc}"
    end
  end

else
  #You don't need the top or bottom, you may need the left or right

  if userecside == -1
    #You need the left
    (recbot..rectop).each do |k|
      puts "#{k} #{recleft}"
    end

    if ya == recleft
      (recleft+1..yb).each do |k|
        puts "#{xb} #{k}"
      end
    else
      (recleft+1..ya).each do |k|
        puts "#{xa} #{k}"
      end
    end

    (yc..recleft-1).each do |k|
      puts "#{xc} #{k}"
    end

  elsif userecside == 1
    #You need the right
    (recbot..rectop).each do |k|
      puts "#{k} #{recright}"
    end

    if ya == recright
      (yb..recright-1).each do |k|
        puts "#{xb} #{k}"
      end
    else
      (ya..recright-1).each do |k|
        puts "#{xa} #{k}"
      end
    end

    (recright+1..yc).each do |k|
      puts "#{xc} #{k}"
    end

  else
    #0,0 -> do a,c, c,b

    acxmin = [xa,xc].min
    acxmax = [xa,xc].max

    acymin = [ya,yc].min
    acymax = [ya,yc].max

    bcxmin = [xb,xc].min
    bcxmax = [xb,xc].max

    bcymin = [yb,yc].min
    bcymax = [yb,yc].max

    if xa<xc
      (xa..xc-1).each do |k|
        puts "#{k} #{ya}"
      end
    else
      (xc+1..xa).each do |k|
        puts "#{k} #{ya}"
      end
    end

    if ya<yc
      (ya..yc).each do |k|
        puts "#{xc} #{k}"
      end
    else
      (yc..ya).each do |k|
        puts "#{xc} #{k}"
      end
    end

    if xb<xc
      (xb..xc-1).each do |k|
        puts "#{k} #{yc}"
      end
    else
      (xc+1..xb).each do |k|
        puts "#{k} #{yc}"
      end
    end

    if yb<yc
      (yb..yc-1).each do |k|
        puts "#{xb} #{k}"
      end
    else
      (yc+1..yb).each do |k|
        puts "#{xb} #{k}"
      end
    end

  end
end

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

xa = a[0]
ya = a[1]
xb = b[0]
yb = b[1]
xc = c[0]
yc = c[1]

ans = (xa-xb).abs + (ya-yb).abs + 1

rectop = [xa,xb].max
recbot = [xa,xb].min
recright = [ya,yb].max
recleft = [ya,yb].min

userecver = 0

if xc< recbot
  userecver = -1
  ans += recbot-xc
elsif xc > rectop
  userecver = 1
  ans += xc-rectop
end

userecside = 0
if yc< recleft
  userecside = -1
  ans += recleft-yc
elsif yc > recright
  userecside = 1
  ans += yc-recright
end

#ans += 1
puts ans

#puts userecver
#puts userecside

if userecver == -1
  #you definitely need the bottom
  (recleft..recright).each do |k|
    puts "#{recbot} #{k}"
  end

  if xa == recbot
    (recbot+1..xb).each do |k|
      puts "#{k} #{yb}"
    end
  else
    (recbot+1..xa).each do |k|
      puts "#{k} #{ya}"
    end
  end

  #DO THE C STUFF
  if userecside == -1
    (xc..recbot).each do |k|
      puts "#{k} #{yc}"
    end
    (yc+1..recleft-1).each do |k|
      puts "#{recbot} #{k}"
    end

  elsif userecside == 1
    (xc..recbot).each do |k|
      puts "#{k} #{yc}"
    end
    (recright+1..yc).each do |k|
      puts "#{recbot} #{k}"
    end

  else
    (xc..recbot-1).each do |k|
      puts "#{k} #{yc}"
    end
  end

elsif userecver == 1

  #you definitely need the top
  (ya..yb).each do |k|
    puts "#{rectop} #{k}"
  end

  if xa == rectop
    (xb..rectop-1).each do |k|
      puts "#{k} #{yb}"
    end
  else
    (xa..rectop-1).each do |k|
      puts "#{k} #{ya}"
    end
  end

  #DO the C stuff
  if userecside == -1
    (rectop+1..xc).each do |k|
      puts "#{k} #{recleft}"
    end
    (yc..recleft-1).each do |k|
      puts "#{xc} #{k}"
    end

  elsif userecside == 1
    (rectop+1..xc).each do |k|
      puts "#{k} #{recright}"
    end
    (recright+1..yc).each do |k|
      puts "#{xc} #{k}"
    end

  else
    (rectop+1..xc).each do |k|
      puts "#{k} #{yc}"
    end
  end

else
  #You don't need the top or bottom, you may need the left or right

  if userecside == -1
    #You need the left
    (recbot..rectop).each do |k|
      puts "#{k} #{recleft}"
    end

    if ya == recleft
      (recleft+1..yb).each do |k|
        puts "#{xb} #{k}"
      end
    else
      (recleft+1..ya).each do |k|
        puts "#{xa} #{k}"
      end
    end

    (yc..recleft-1).each do |k|
      puts "#{xc} #{k}"
    end

  elsif userecside == 1
    #You need the right
    (recbot..rectop).each do |k|
      puts "#{k} #{recright}"
    end

    if ya == recright
      (yb..recright-1).each do |k|
        puts "#{xb} #{k}"
      end
    else
      (ya..recright-1).each do |k|
        puts "#{xa} #{k}"
      end
    end

    (recright+1..yc).each do |k|
      puts "#{xc} #{k}"
    end

  else
    #0,0 -> do a,c, c,b

    acxmin = [xa,xc].min
    acxmax = [xa,xc].max

    acymin = [ya,yc].min
    acymax = [ya,yc].max

    bcxmin = [xb,xc].min
    bcxmax = [xb,xc].max

    bcymin = [yb,yc].min
    bcymax = [yb,yc].max

    if xa<xc
      (xa..xc-1).each do |k|
        puts "#{k} #{ya}"
      end
    else
      (xc+1..xa).each do |k|
        puts "#{k} #{ya}"
      end
    end

    if ya<yc
      (ya..yc).each do |k|
        puts "#{xc} #{k}"
      end
    else
      (yc..ya).each do |k|
        puts "#{xc} #{k}"
      end
    end

    if xb<xc
      (xb..xc-1).each do |k|
        puts "#{k} #{yc}"
      end
    else
      (xc+1..xb).each do |k|
        puts "#{k} #{yc}"
      end
    end

    if yb<yc
      (yb..yc-1).each do |k|
        puts "#{xb} #{k}"
      end
    else
      (yc+1..yb).each do |k|
        puts "#{xb} #{k}"
      end
    end

  end
end

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

xa = a[0]
ya = a[1]
xb = b[0]
yb = b[1]
xc = c[0]
yc = c[1]

ans = (xa-xb).abs + (ya-yb).abs + 1

rectop = [xa,xb].max
recbot = [xa,xb].min
recright = [ya,yb].max
recleft = [ya,yb].min

userecver = 0

if xc< recbot
  userecver = -1
  ans += recbot-xc
elsif xc > rectop
  userecver = 1
  ans += xc-rectop
end

userecside = 0
if yc< recleft
  userecside = -1
  ans += recleft-yc
elsif yc > recright
  userecside = 1
  ans += yc-recright
end

#ans += 1
puts ans

#puts userecver
#puts userecside

if userecver == -1
  #you definitely need the bottom
  (recleft..recright).each do |k|
    puts "#{recbot} #{k}"
  end

  if xa == recbot
    (recbot+1..xb).each do |k|
      puts "#{k} #{yb}"
    end
  else
    (recbot+1..xa).each do |k|
      puts "#{k} #{ya}"
    end
  end

  #DO THE C STUFF
  if userecside == -1
    (xc..recbot).each do |k|
      puts "#{k} #{yc}"
    end
    (yc+1..recleft-1).each do |k|
      puts "#{recbot} #{k}"
    end

  elsif userecside == 1
    (xc..recbot).each do |k|
      puts "#{k} #{yc}"
    end
    (recright+1..yc).each do |k|
      puts "#{recbot} #{k}"
    end

  else
    (xc..recbot-1).each do |k|
      puts "#{k} #{yc}"
    end
  end

elsif userecver == 1

  #you definitely need the top
  (ya..yb).each do |k|
    puts "#{rectop} #{k}"
  end

  if xa == rectop
    (xb..rectop-1).each do |k|
      puts "#{k} #{yb}"
    end
  else
    (xa..rectop-1).each do |k|
      puts "#{k} #{ya}"
    end
  end

  #DO the C stuff
  if userecside == -1
    (rectop+1..xc).each do |k|
      puts "#{k} #{recleft}"
    end
    (yc..recleft-1).each do |k|
      puts "#{xc} #{k}"
    end

  elsif userecside == 1
    (rectop+1..xc).each do |k|
      puts "#{k} #{recright}"
    end
    (recright+1..yc).each do |k|
      puts "#{xc} #{k}"
    end

  else
    (rectop+1..xc).each do |k|
      puts "#{k} #{yc}"
    end
  end

else
  #You don't need the top or bottom, you may need the left or right

  if userecside == -1
    #You need the left
    (recbot..rectop).each do |k|
      puts "#{k} #{recleft}"
    end

    if ya == recleft
      (recleft+1..yb).each do |k|
        puts "#{xb} #{k}"
      end
    else
      (recleft+1..ya).each do |k|
        puts "#{xa} #{k}"
      end
    end

    (yc..recleft-1).each do |k|
      puts "#{xc} #{k}"
    end

  elsif userecside == 1
    #You need the right
    (recbot..rectop).each do |k|
      puts "#{k} #{recright}"
    end

    if ya == recright
      (yb..recright-1).each do |k|
        puts "#{xb} #{k}"
      end
    else
      (ya..recright-1).each do |k|
        puts "#{xa} #{k}"
      end
    end

    (recright+1..yc).each do |k|
      puts "#{xc} #{k}"
    end

  else
    #0,0 -> do a,c, c,b

    acxmin = [xa,xc].min
    acxmax = [xa,xc].max

    acymin = [ya,yc].min
    acymax = [ya,yc].max

    bcxmin = [xb,xc].min
    bcxmax = [xb,xc].max

    bcymin = [yb,yc].min
    bcymax = [yb,yc].max

    (acxmin..acxmax-1).each do |k|
      puts "#{k} #{ya}"
    end

    (acymin..acymax).each do |k|
      puts "#{xc} #{k}"
    end

    (bcxmin+1..bcxmax-1).each do |k|
      puts "#{k} #{yc}"
    end

    (bcymin+1..bcymax).each do |k|
      puts "#{xb} #{k}"
    end

  end
end

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

xa = a[0]
ya = a[1]
xb = b[0]
yb = b[1]
xc = c[0]
yc = c[1]

ans = (xa-xb).abs + (ya-yb).abs + 1

rectop = [xa,xb].max
recbot = [xa,xb].min
recright = [ya,yb].max
recleft = [ya,yb].min

userecver = 0

if xc< recbot
  userecver = -1
  ans += recbot-xc
elsif xc > rectop
  userecver = 1
  ans += xc-rectop
end

userecside = 0
if yc< recleft
  userecside = -1
  ans += recleft-yc
elsif yc > recright
  userecside = 1
  ans += yc-recright
end

#ans += 1
puts ans

#puts userecver
#puts userecside

if userecver == -1
  #you definitely need the bottom
  (recleft..recright).each do |k|
    puts "#{recbot} #{k}"
  end

  if xa == recbot
    (recbot+1..xb).each do |k|
      puts "#{k} #{yb}"
    end
  else
    (recbot+1..xa).each do |k|
      puts "#{k} #{ya}"
    end
  end

  #DO THE C STUFF
  if userecside == -1
    (xc..recbot).each do |k|
      puts "#{k} #{yc}"
    end
    (yc+1..recleft-1).each do |k|
      puts "#{recbot} #{k}"
    end

  elsif userecside == 1
    (xc..recbot).each do |k|
      puts "#{k} #{yc}"
    end
    (recright+1..yc).each do |k|
      puts "#{recbot} #{k}"
    end

  else
    (xc..recbot-1).each do |k|
      puts "#{k} #{yc}"
    end
  end

elsif userecver == 1

  #you definitely need the top
  (ya..yb).each do |k|
    puts "#{rectop} #{k}"
  end

  if xa == rectop
    (xb..rectop-1).each do |k|
      puts "#{k} #{yb}"
    end
  else
    (xa..rectop-1).each do |k|
      puts "#{k} #{ya}"
    end
  end

  #DO the C stuff
  if userecside == -1
    (rectop+1..xc).each do |k|
      puts "#{k} #{recleft}"
    end
    (yc..recleft-1).each do |k|
      puts "#{xc} #{k}"
    end

  elsif userecside == 1
    (rectop+1..xc).each do |k|
      puts "#{k} #{recright}"
    end
    (recright+1..yc).each do |k|
      puts "#{xc} #{k}"
    end

  else
    (rectop+1..xc).each do |k|
      puts "#{k} #{yc}"
    end
  end

else
  #You don't need the top or bottom, you may need the left or right

  if userecside == -1
    #You need the left
    (recbot..rectop).each do |k|
      puts "#{k} #{recleft}"
    end

    if ya == recleft
      (recleft+1..yb).each do |k|
        puts "#{xb} #{k}"
      end
    else
      (recleft+1..ya).each do |k|
        puts "#{xa} #{k}"
      end
    end

    (yc..recleft-1).each do |k|
      puts "#{xc} #{k}"
    end

  elsif userecside == 1
    #You need the right
    (recbot..rectop).each do |k|
      puts "#{k} #{recright}"
    end

    if ya == recright
      (yb..recright-1).each do |k|
        puts "#{xb} #{k}"
      end
    else
      (ya..recright-1).each do |k|
        puts "#{xa} #{k}"
      end
    end

    (recright+1..yc).each do |k|
      puts "#{xc} #{k}"
    end

  else
    #0,0 -> do a,c, c,b

    acxmin = [xa,xc].min
    acxmax = [xa,xc].max

    acymin = [ya,yc].min
    acymax = [ya,yc].max

    bcxmin = [xb,xc].min
    bcxmax = [xb,xc].max

    bcymin = [yb,yc].min
    bcymax = [yb,yc].max

    (acxmin..acxmax-1).each do |k|
      puts "#{k} #{ya}"
    end

    (acymin..acymax).each do |k|
      puts "#{xc} #{k}"
    end

    (bcxmin+1..bcxmax-1).each do |k|
      puts "#{k} #{yc}"
    end

    (bcymin..bcymax).each do |k|
      puts "#{xb} #{k}"
    end

  end
end

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

xa = a[0]
ya = a[1]
xb = b[0]
yb = b[1]
xc = c[0]
yc = c[1]

ans = (xa-xb).abs + (ya-yb).abs + 1

rectop = [xa,xb].max
recbot = [xa,xb].min
recright = [ya,yb].max
recleft = [ya,yb].min

userecver = 0

if xc< recbot
  userecver = -1
  ans += recbot-xc
elsif xc > rectop
  userecver = 1
  ans += xc-rectop
end

userecside = 0
if yc< recleft
  userecside = -1
  ans += recleft-yc
elsif yc > recright
  userecside = 1
  ans += yc-recright
end

#ans += 1
puts ans

#puts userecver
#puts userecside

if userecver == -1
  #you definitely need the bottom
  (recleft..recright).each do |k|
    puts "#{recbot} #{k}"
  end

  if xa == recbot
    (recbot+1..xb).each do |k|
      puts "#{k} #{yb}"
    end
  else
    (recbot+1..xa).each do |k|
      puts "#{k} #{ya}"
    end
  end

  #DO THE C STUFF
  if userecside == -1
    (xc..recbot).each do |k|
      puts "#{k} #{yc}"
    end
    (yc+1..recleft-1).each do |k|
      puts "#{recbot} #{k}"
    end

  elsif userecside == 1
    (xc..recbot).each do |k|
      puts "#{k} #{yc}"
    end
    (recright+1..yc).each do |k|
      puts "#{recbot} #{k}"
    end

  else
    (xc..recbot-1).each do |k|
      puts "#{k} #{yc}"
    end
  end

elsif userecver == 1

  #you definitely need the top
  (recleft..recright).each do |k|
    puts "#{rectop} #{k}"
  end

  if xa == rectop
    (xb..rectop-1).each do |k|
      puts "#{k} #{yb}"
    end
  else
    (xa..rectop-1).each do |k|
      puts "#{k} #{ya}"
    end
  end

  #DO the C stuff
  if userecside == -1
    (rectop+1..xc).each do |k|
      puts "#{k} #{recleft}"
    end
    (yc..recleft-1).each do |k|
      puts "#{xc} #{k}"
    end

  elsif userecside == 1
    (rectop+1..xc).each do |k|
      puts "#{k} #{recright}"
    end
    (recright+1..yc).each do |k|
      puts "#{xc} #{k}"
    end

  else
    (rectop+1..xc).each do |k|
      puts "#{k} #{yc}"
    end
  end

else
  #You don't need the top or bottom, you may need the left or right

  if userecside == -1
    #You need the left
    (recbot..rectop).each do |k|
      puts "#{k} #{recleft}"
    end

    if ya == recleft
      (recleft+1..yb).each do |k|
        puts "#{xb} #{k}"
      end
    else
      (recleft+1..ya).each do |k|
        puts "#{xa} #{k}"
      end
    end

    (yc..recleft-1).each do |k|
      puts "#{xc} #{k}"
    end

  elsif userecside == 1
    #You need the right
    (recbot..rectop).each do |k|
      puts "#{k} #{recright}"
    end

    if ya == recright
      (yb..recright-1).each do |k|
        puts "#{xb} #{k}"
      end
    else
      (ya..recright-1).each do |k|
        puts "#{xa} #{k}"
      end
    end

    (recright+1..yc).each do |k|
      puts "#{xc} #{k}"
    end

  else
    #0,0 -> do a,c, c,b

    acxmin = [xa,xc].min
    acxmax = [xa,xc].max

    acymin = [ya,yc].min
    acymax = [ya,yc].max

    bcxmin = [xb,xc].min
    bcxmax = [xb,xc].max

    bcymin = [yb,yc].min
    bcymax = [yb,yc].max

    if xa<xc
      (xa..xc-1).each do |k|
        puts "#{k} #{ya}"
      end
    else
      (xc+1..xa).each do |k|
        puts "#{k} #{ya}"
      end
    end

    if ya<yc
      (ya..yc).each do |k|
        puts "#{xc} #{k}"
      end
    else
      (yc..ya).each do |k|
        puts "#{xc} #{k}"
      end
    end

    if xb<xc
      (xb..xc-1).each do |k|
        puts "#{k} #{yc}"
      end
    else
      (xc+1..xb).each do |k|
        puts "#{k} #{yc}"
      end
    end

    if yb<yc
      (yb..yc-1).each do |k|
        puts "#{xb} #{k}"
      end
    else
      (yc+1..yb).each do |k|
        puts "#{xb} #{k}"
      end
    end

  end
end


The USA Construction Operation (USACO) recently ordered Farmer John to arrange a row of $$$n$$$ haybale piles on the farm. The $$$i$$$-th pile contains $$$a_i$$$ haybales. However, Farmer John has just left for vacation, leaving Bessie all on her own. Every day, Bessie the naughty cow can choose to move one haybale in any pile to an adjacent pile. Formally, in one day she can choose any two indices $$$i$$$ and $$$j$$$ ($$$1 \le i, j \le n$$$) such that $$$|i-j|=1$$$ and $$$a_i>0$$$ and apply $$$a_i = a_i - 1$$$, $$$a_j = a_j + 1$$$. She may also decide to not do anything on some days because she is lazy.Bessie wants to maximize the number of haybales in pile $$$1$$$ (i.e. to maximize $$$a_1$$$), and she only has $$$d$$$ days to do so before Farmer John returns. Help her find the maximum number of haybales that may be in pile $$$1$$$ if she acts optimally!

The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 100$$$)  — the number of test cases. Next $$$2t$$$ lines contain a description of test cases  — two lines per test case. The first line of each test case contains integers $$$n$$$ and $$$d$$$ ($$$1 \le n,d \le 100$$$) — the number of haybale piles and the number of days, respectively.  The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 100$$$)  — the number of haybales in each pile.

For each test case, output one integer: the maximum number of haybales that may be in pile $$$1$$$ after $$$d$$$ days if Bessie acts optimally.

NoteIn the first test case of the sample, this is one possible way Bessie can end up with $$$3$$$ haybales in pile $$$1$$$:   On day one, move a haybale from pile $$$3$$$ to pile $$$2$$$  On day two, move a haybale from pile $$$3$$$ to pile $$$2$$$  On day three, move a haybale from pile $$$2$$$ to pile $$$1$$$  On day four, move a haybale from pile $$$2$$$ to pile $$$1$$$  On day five, do nothing  In the second test case of the sample, Bessie can do nothing on the first day and move a haybale from pile $$$2$$$ to pile $$$1$$$ on the second day.

# 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
a = Array.new(101)
t.times do
  n = next_int
  d = next_int

  n.times do |i|
    a[i] = next_int
  end

  (1...n).each do |i|
    break if d == 0

    k = d / i
    k = a[i] if k > a[i]
    a[0] += k
    d -= k * i
  end
  puts a[0]
end

T = gets.to_i

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

  ans = 0

  a.each_with_index do |cnt, dist|
    break if d <= 0
    next if cnt == 0

    if dist == 0
      ans += cnt
    else
      c = [cnt, d / dist].min
      cost = c * dist
      d -= cost
      ans += c
    end
  end

  puts ans
end


Game "Minesweeper 1D" is played on a line of squares, the line's height is 1 square, the line's width is n squares. Some of the squares contain bombs. If a square doesn't contain a bomb, then it contains a number from 0 to 2 — the total number of bombs in adjacent squares.For example, the correct field to play looks like that: 001*2***101*. The cells that are marked with "*" contain bombs. Note that on the correct field the numbers represent the number of bombs in adjacent cells. For example, field 2* is not correct, because cell with value 2 must have two adjacent cells with bombs.Valera wants to make a correct field to play "Minesweeper 1D". He has already painted a squared field with width of n cells, put several bombs on the field and wrote numbers into some cells. Now he wonders how many ways to fill the remaining cells with bombs and numbers are there if we should get a correct field in the end.

The first line contains sequence of characters without spaces s1s2... sn (1 ≤ n ≤ 106), containing only characters "*", "?" and digits "0", "1" or "2". If character si equals "*", then the i-th cell of the field contains a bomb. If character si equals "?", then Valera hasn't yet decided what to put in the i-th cell. Character si, that is equal to a digit, represents the digit written in the i-th square.

Print a single integer — the number of ways Valera can fill the empty cells and get a correct field. As the answer can be rather large, print it modulo 1000000007 (109 + 7).

NoteIn the first test sample you can get the following correct fields: 001**1, 001***, 001*2*, 001*10.

s = gets.chomp!
mods = 1000000007
dp = [[0,0,0],[0,0,0]]

len =s.size
cur =0;nxt=1;

case s[0]
when '0'
	dp[cur][0]=1
when '1'
	dp[cur][1]=1
when '2'
	p 0
	exit
when '*'
	dp[cur][2]=1
when '?'
	dp[cur][0]=dp[cur][1]=dp[cur][2]=1
end

for i in 1...len
	case s[i]
	when '0'
		dp[nxt][0]=dp[cur][0]
	when '1'
		dp[nxt][1]= dp[cur][0]
		dp[nxt][0]= dp[cur][2]
	when '2'
		dp[nxt][1]=dp[cur][2]
	when '*'
		dp[nxt][2]= (dp[cur][1]+dp[cur][2])%mods
	when '?'
		dp[nxt][1]= (dp[cur][2]+dp[cur][0])%mods
		dp[nxt][0]= (dp[cur][0]+dp[cur][2])%mods
		dp[nxt][2]= (dp[cur][1]+dp[cur][2])%mods
	end
	cur = nxt
	nxt = cur==0?1:0
	dp[nxt] = [0,0,0]
end

puts "#{(dp[cur][0]+dp[cur][2])%mods}"

s = gets.chomp!
mods = 1000000007
dp = [[0,0,0],[0,0,0]]

len =s.size
cur =0;nxt=1;

case s[0]
when '0'
	dp[cur][0]=1
when '1'
	dp[cur][1]=1
when '2'
	p 0
	exit
when '*'
	dp[cur][2]=1
when '?'
	dp[cur][0]=dp[cur][1]=dp[cur][2]=1
end

for i in 1...len
	case s[i]
	when '0'
		dp[nxt][0]=dp[cur][0]
	when '1'
		dp[nxt][1]= dp[cur][0]
		dp[nxt][0]= dp[cur][2]
	when '2'
		dp[nxt][1]=dp[cur][2]
	when '*'
		dp[nxt][2]= (dp[cur][1]+dp[cur][2])%mods
	when '?'
		dp[nxt][1]= (dp[cur][2]+dp[cur][0])%mods
		dp[nxt][0]= (dp[cur][0]+dp[cur][2])%mods
		dp[nxt][2]= (dp[cur][1]+dp[cur][2])%mods
	end
	a = cur
	cur = nxt
	nxt = a
	dp[nxt] = [0,0,0]
end

puts "#{(dp[cur][0]+dp[cur][2])%mods}"

s = gets.chomp!
mods = 1000000007
dp = [[0,0,0],[0,0,0]]

len =s.size
cur =0;nxt=1;

case s[0]
when '0'
	dp[cur][0]=1
when '1'
	dp[cur][1]=1
when '2'
	p 0
	exit
when '*'
	dp[cur][2]=1
when '?'
	dp[cur][0]=dp[cur][1]=dp[cur][2]=1
end

for i in 1...len
	case s[i]
	when '0'
		dp[nxt][0]=dp[cur][0]
	when '1'
		dp[nxt][1]= dp[cur][0]
		dp[nxt][0]= dp[cur][2]
	when '2'
		dp[nxt][1]=dp[cur][2]
	when '*'
		dp[nxt][2]= (dp[cur][1]+dp[cur][2])%mods
	when '?'
		dp[nxt][1]= (dp[cur][2]+dp[cur][0])%mods
		dp[nxt][0]= (dp[cur][0]+dp[cur][2])%mods
		dp[nxt][2]= (dp[cur][1]+dp[cur][2])%mods
	end
	a = cur
	cur = nxt
	nxt = a
	dp[nxt] = [0,0,0]
end

p (dp[cur][0]+dp[cur][2])%mods


Pari has a friend who loves palindrome numbers. A palindrome number is a number that reads the same forward or backward. For example 12321, 100001 and 1 are palindrome numbers, while 112 and 1021 are not.Pari is trying to love them too, but only very special and gifted people can understand the beauty behind palindrome numbers. Pari loves integers with even length (i.e. the numbers with even number of digits), so she tries to see a lot of big palindrome numbers with even length (like a 2-digit 11 or 6-digit 122221), so maybe she could see something in them.Now Pari asks you to write a program that gets a huge integer n from the input and tells what is the n-th even-length positive palindrome number?

The only line of the input contains a single integer n (1 ≤ n ≤ 10100 000).

Print the n-th even-length palindrome number.

NoteThe first 10 even-length palindrome numbers are 11, 22, 33, ... , 88, 99 and 1001.

a = gets.chomp

a = a + a.reverse

puts a

#!/usr/bin/ruby
s=gets.chomp
puts s+s.reverse

str = gets.chomp

print str
print str.reverse

#! /usr/bin/env ruby
gets.chomp.tap{|s|print s; puts s.reverse}

a=gets.chomp
b=a.split("").reverse.join("")
print a
print b

s = gets.strip
d = s.reverse
ans = s+d
puts ans

# coding: utf-8

# if __FILE__ == $0
# end

order = gets.chomp

puts "#{order}#{order.reverse}"

pal = gets.strip
ans = ""
ans << pal << pal.reverse
puts ans

#!/usr/bin/ruby
n=gets.to_i
i=1
t=9
while n>t
	n-=t
	i+=1
	t=t*10+9
end
s='%0*d'%[i,n]
puts s.reverse+s

a=gets.chomp.to_i
(a*2).times do
print "1"
end

s=gets
print s
print s.reverse
print "\n"


A ticket is a string consisting of six digits. A ticket is considered lucky if the sum of the first three digits is equal to the sum of the last three digits. Given a ticket, output if it is lucky or not. Note that a ticket can have leading zeroes.

The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^3$$$) — the number of testcases. The description of each test consists of one line containing one string consisting of six digits.

Output $$$t$$$ lines, each of which contains the answer to the corresponding test case. Output "YES" if the given ticket is lucky, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).

NoteIn the first test case, the sum of the first three digits is $$$2 + 1 + 3 = 6$$$ and the sum of the last three digits is $$$1 + 3 + 2 = 6$$$, they are equal so the answer is "YES".In the second test case, the sum of the first three digits is $$$9 + 7 + 3 = 19$$$ and the sum of the last three digits is $$$8 + 9 + 4 = 21$$$, they are not equal so the answer is "NO".In the third test case, the sum of the first three digits is $$$0 + 4 + 5 = 9$$$ and the sum of the last three digits is $$$2 + 0 + 7 = 9$$$, they are equal so the answer is "YES".

 gets.to_i.times {
 	v = gets.chars.map {|x| x.to_i }
 	puts ({true=>"YES", false=>"NO"}[v[..2].sum==v[3..].sum])
 }

gets.to_i.times do
  s = gets.chomp.chars.map(&:to_i)
  puts s[..2].sum == s[3..].sum ? "YES" : "NO"
end

gets.to_i.times do
  s = gets.strip.chars
  fail unless s.length == 6
  a = s[..2].map(&:to_i).sum
  b = s[3..].map(&:to_i).sum
  puts(if a == b then 'YES' else 'NO' end)
end

def main
  gets.to_i.times do
    x = gets.chomp.chars.map(&:to_i)
    puts uyesno(x[..2].sum == x[3..].sum)
  end
end

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

main

main = -> {
  N = int
  N.times do
    digits = gets.chomp.chars.map(&:to_i)
    first = digits[0, 3].sum
    last = digits[3, 3].sum
    puts first == last ? "YES" : "NO"
  end
}

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

require "set"
require "prime"

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

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

class TrueClass
  include Boolean
  def to_i; 1 end
end

class FalseClass
  include Boolean
  def to_i; 0 end
end

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

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

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

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

main[]


Dima worked all day and wrote down on a long paper strip his favorite number $$$n$$$ consisting of $$$l$$$ digits. Unfortunately, the strip turned out to be so long that it didn't fit in the Dima's bookshelf.To solve the issue, Dima decided to split the strip into two non-empty parts so that each of them contains a positive integer without leading zeros. After that he will compute the sum of the two integers and write it down on a new strip.Dima wants the resulting integer to be as small as possible, because it increases the chances that the sum will fit it in the bookshelf. Help Dima decide what is the minimum sum he can obtain.

The first line contains a single integer $$$l$$$ ($$$2 \le l \le 100\,000$$$) — the length of the Dima's favorite number. The second line contains the positive integer $$$n$$$ initially written on the strip: the Dima's favorite number. The integer $$$n$$$ consists of exactly $$$l$$$ digits and it does not contain leading zeros. Dima guarantees, that there is at least one valid way to split the strip.

Print a single integer — the smallest number Dima can obtain.

NoteIn the first example Dima can split the number $$$1234567$$$ into integers $$$1234$$$ and $$$567$$$. Their sum is $$$1801$$$.In the second example Dima can split the number $$$101$$$ into integers $$$10$$$ and $$$1$$$. Their sum is $$$11$$$. Note that it is impossible to split the strip into "1" and "01" since the numbers can't start with zeros.

n = gets.to_i
s = gets.chomp
ans = []
if n%2 == 0
  3.times do |i|
    next if s[n/2-1+i] == "0"
    ans << s[0,n/2-1+i].to_i + s[n/2-1+i,n/2+1-i].to_i
  end
else
  4.times do |i|
    k = n/2-1+i
    next if k <= 0 || k >= n || s[k] == "0"
    ans << s[0,k].to_i + s[k,n-k].to_i
  end
end
if ans.empty?
  l,r = n/2,n/2
  l -=1 while l >= 0 && s[l] == "0"
  r += 1 while r < n && s[r] == "0"
  ans << s[0,l].to_i + s[l,n-l].to_i if l > 0
  ans << s[0,r].to_i + s[r,n-r].to_i if r < n
  #p [l,r]
end
#p ans
puts ans.min

L = gets.to_i
N = gets.chomp

ans = Float::INFINITY

pt = L/2
pt -= 1 while N[pt] == '0'
if pt > 0
    res = N[0, pt].to_i + N[pt..-1].to_i
    ans = [ans, res].min
end
pt = (L+1)/2
pt += 1 while pt < L && N[pt] == '0'
if pt < L
    res = N[0, pt].to_i + N[pt..-1].to_i
    ans = [ans, res].min
end

puts ans

l = gets.to_i
n = gets.chomp
c = l
idx = 0
(1...l).each do |i|
    if n[i] != "0"
        m = (l-i > i ? l-i : i)
        if c > m
            c = m
            idx = i
        end
    end
end
ans = n[0,idx].to_i+n[idx..-1].to_i
s = ans.to_s.size
(1...l).each do |i|
    if n[i] != "0"
        next if s < (l-i > i ? l-i : i)
        ret = n[0,i].to_i+n[i..-1].to_i
        if ret < ans
            ans = ret
            s = ans.to_s.size
        end
    end
end
puts ans

n = gets.to_i
l = gets.chomp

ans = 10 ** 20

(0..n-2).each do |i|
  if l[i+1] == '0' && i != n-2
    next
  end
  ans = [ans, l[0..i].to_i + l[(i+1)..n-1].to_i].min
end

puts ans

n = gets.to_i
l = gets.chomp

ans = 10 ** 20

(0..n-2).each do |i|
  if l[i+1] == '0'
    next
  end
  ans = [ans, l[0..i].to_i + l[(i+1)..n-1].to_i].min
end

puts ans

n = gets.to_i
l = gets.chomp

ans = 10 ** 20

(0..n-2).each do |i|
  a = l[0..i]
  b = l[(i+1)..n-1]
  if b.size >= 2 && b[0] == '0'
    next
  end
  ans = [ans, a.to_i + b.to_i].min
end

puts ans

n = gets.to_i
l = gets.chomp

ans = 10 ** 20

(0..n-2).each do |i|
  a = l[0..i]
  b = l[(i+1)..n-1]
  if b[0] == '0'
    next
  end
  ans = [ans, a.to_i + b.to_i].min
end

puts ans

l = gets.to_i
n = gets.chomp
c = l
idx = 0
(l-1).downto(1) do |i|
    if n[i] != "0"
        m = (l-i > i ? l-i : i)
        if c > m
            c = m
            idx = i
        end
    end
end
ans = n[0,idx].to_i+n[idx..-1].to_i
(1...l).each do |i|
    if n[i] != "0"
        break if ans.to_s.size < (l-i > i ? l-i : i)
        ret = n[0,i].to_i+n[i..-1].to_i
        if ret < ans
            ans = ret
        end
    end
end
puts ans


Devu being a small kid, likes to play a lot, but he only likes to play with arrays. While playing he came up with an interesting question which he could not solve, can you please solve it for him?Given an array consisting of distinct integers. Is it possible to partition the whole array into k disjoint non-empty parts such that p of the parts have even sum (each of them must have even sum) and remaining k - p have odd sum? (note that parts need not to be continuous).If it is possible to partition the array, also give any possible way of valid partitioning.

The first line will contain three space separated integers n, k, p (1 ≤ k ≤ n ≤ 105; 0 ≤ p ≤ k). The next line will contain n space-separated distinct integers representing the content of array a: a1, a2, ..., an (1 ≤ ai ≤ 109).

In the first line print "YES" (without the quotes) if it is possible to partition the array in the required way. Otherwise print "NO" (without the quotes). If the required partition exists, print k lines after the first line. The ith of them should contain the content of the ith part. Print the content of the part in the line in the following way: firstly print the number of elements of the part, then print all the elements of the part in arbitrary order. There must be exactly p parts with even sum, each of the remaining k - p parts must have odd sum. As there can be multiple partitions, you are allowed to print any valid partition.

def count_in_arrays(arrays, mod)
	arrays.map { |a|
		a.count { |x| x % 2 == mod }
	}.inject(0, &:+)
end

n,k,p = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
odds = a.select { |x| x % 2 == 1 }
evens = a.select { |x| x % 2 == 0 }

if k-p > odds.length or (odds.length-(k-p)) % 2 == 1 or (odds.length-(k-p)) / 2 + evens.length < p
	puts "NO"
	exit
end

oddSets = (0...(k-p)).map { |i| [odds[i]] }
evenSets = (0...p).map { |i|
	if i < evens.length
		[evens[i]]
	else
		oi = (k-p)+2*(i-evens.length)
		[odds[oi], odds[oi+1]]
	end
}

usedOdds = count_in_arrays(oddSets, 1) + count_in_arrays(evenSets, 1)
usedEvens = count_in_arrays(oddSets, 0) + count_in_arrays(evenSets, 0)

pushing_array = (p >= 1) ? evenSets[0] : oddSets[0]
odds[usedOdds...odds.length].each { |o| pushing_array.push o }
evens[usedEvens...evens.length].each { |e| pushing_array.push e }

puts "YES"
oddSets.each { |a|
	puts a.length.to_s + " " + a.join(" ")
}
evenSets.each { |a|
	puts a.length.to_s + " " + a.join(" ")
}


The Department of economic development of IT City created a model of city development till year 2100.To prepare report about growth perspectives it is required to get growth estimates from the model.To get the growth estimates it is required to solve a quadratic equation. Since the Department of economic development of IT City creates realistic models only, that quadratic equation has a solution, moreover there are exactly two different real roots.The greater of these roots corresponds to the optimistic scenario, the smaller one corresponds to the pessimistic one. Help to get these estimates, first the optimistic, then the pessimistic one.

The only line of the input contains three integers a, b, c ( - 1000 ≤ a, b, c ≤ 1000) — the coefficients of ax2 + bx + c = 0 equation.

In the first line output the greater of the equation roots, in the second line output the smaller one. Absolute or relative error should not be greater than 10 - 6.

a,b,c=gets.split.map(&:to_f)
(a=-a;b=-b;c=-c)if a<0
puts [1,-1].map{|s|(-b+s*Math.sqrt(b*b-4*a*c))/2/a}

a,b,c=gets.split.map(&:to_f)
if a<0
a=-a
b=-b
c=-c
end
p (-b+Math.sqrt(b*b-4*a*c))/2/a
p (-b-Math.sqrt(b*b-4*a*c))/2/a

a, b, c = gets.chomp.split

a = a.to_f
b = b.to_f
c = c.to_f

d=(b**2-4*a*c)**(0.5)
x=(-b+d)/(2*a)
y=(-b-d)/(2*a)

if y>x
x, y= y, x
end

puts x
print y

a,b,c=gets.split.map(&:to_f)
p (-b+Math.sqrt(b*b-4*a*c))/2
p (-b-Math.sqrt(b*b-4*a*c))/2

a,b,c=gets.split.map(&:to_f)
p (-b+Math.sqrt(b*b-4*a*c))/2/a
p (-b-Math.sqrt(b*b-4*a*c))/2/a