You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i > a_{i - 1}$$$ and $$$a_i > a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.

Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.

NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.

t = readline.strip.to_i
t.times do
  n = readline.strip.to_i
  nums = readline.strip.split(/\s+/).map(&:to_i)

  c = nums.count

  if c <= 2
    puts 0
    puts nums.join(' ')
    next
  end

  i = 1
  changes = 0
  while i < c - 1
    if nums[i] > nums[i-1] && nums[i] > nums[i+1] # local max
      if i+2 < c && nums[i+1] < nums[i+2]
        nums[i+1] = [nums[i], nums[i+2]].max
        changes += 1
        i += 1
        # puts "i=#{i} next is local min"
      else
        changes += 1
        nums[i] = [nums[i-1], nums[i+1]].max
      end
    end
    i += 1
  end

#  1 3 2 3 1
#    i

  puts changes
  puts nums.join(' ')
end

t = readline.strip.to_i
t.times do
  n = readline.strip.to_i
  nums = readline.strip.split(/\s+/).map(&:to_i)

  c = nums.count

  if c <= 3
    puts 0
    puts nums.join(' ')
    next
  end

  i = 1
  changes = 0
  while i < c - 1
    if nums[i] > nums[i-1] && nums[i] > nums[i+1] # local max
      if i+2 < c && nums[i+1] < nums[i+2]
        nums[i+1] = [nums[i], nums[i+2]].max
        changes += 1
        i += 1
        # puts "i=#{i} next is local min"
      else
        changes += 1
        nums[i] = [nums[i-1], nums[i+1]].max
      end
    end
    i += 1
  end

#  1 3 2 3 1
#    i

  puts changes
  puts nums.join(' ')
end


There are $$$n$$$ emotes in very popular digital collectible card game (the game is pretty famous so we won't say its name). The $$$i$$$-th emote increases the opponent's happiness by $$$a_i$$$ units (we all know that emotes in this game are used to make opponents happy).You have time to use some emotes only $$$m$$$ times. You are allowed to use any emotion once, more than once, or not use it at all. The only restriction is that you cannot use the same emote more than $$$k$$$ times in a row (otherwise the opponent will think that you're trolling him).Note that two emotes $$$i$$$ and $$$j$$$ ($$$i \ne j$$$) such that $$$a_i = a_j$$$ are considered different.You have to make your opponent as happy as possible. Find the maximum possible opponent's happiness.

The first line of the input contains three integers $$$n, m$$$ and $$$k$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$1 \le k \le m \le 2 \cdot 10^9$$$) — the number of emotes, the number of times you can use emotes and the maximum number of times you may use the same emote in a row. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is value of the happiness of the $$$i$$$-th emote.

Print one integer — the maximum opponent's happiness if you use emotes in a way satisfying the problem statement.

NoteIn the first example you may use emotes in the following sequence: $$$4, 4, 5, 4, 4, 5, 4, 4, 5$$$.

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

nmk = gets.chomp.split(' ').map(&:to_i)
n = nmk[0]
m = nmk[1]
k = nmk[2]

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

max=0
maxtwo=0

(0..n-1).each do |i|
	if(arr[i]>max)
    	maxtwo = max
    	max=arr[i]
    elsif(arr[i]>maxtwo)
    	maxtwo = arr[i]
    end
end

ans=(m/(k+1))*(max*(k)+maxtwo)
leftover = m-(m/(k+1))*(k+1);
ans+=max*leftover

puts ans


You are given an array $$$a$$$ of length $$$n$$$.Let's define the eversion operation. Let $$$x = a_n$$$. Then array $$$a$$$ is partitioned into two parts: left and right. The left part contains the elements of $$$a$$$ that are not greater than $$$x$$$ ($$$\le x$$$). The right part contains the elements of $$$a$$$ that are strictly greater than $$$x$$$ ($$$> x$$$). The order of elements in each part is kept the same as before the operation, i. e. the partition is stable. Then the array is replaced with the concatenation of the left and the right parts.For example, if the array $$$a$$$ is $$$[2, 4, 1, 5, 3]$$$, the eversion goes like this: $$$[2, 4, 1, 5, 3] \to [2, 1, 3], [4, 5] \to [2, 1, 3, 4, 5]$$$.We start with the array $$$a$$$ and perform eversions on this array. We can prove that after several eversions the array $$$a$$$ stops changing. Output the minimum number $$$k$$$ such that the array stops changing after $$$k$$$ eversions.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case print a single integer $$$k$$$ — the number of eversions after which the array stops changing.

testcases = Integer(gets.chomp)
resultados = []

for i in 0..testcases-1
    n = Integer(gets.chomp)
    n -= 1
    arreglo = gets.chomp
    a = arreglo.split(" ")
    aux = a.sort
    mayor = aux[n].to_i
    k = a[n].to_i
    n -= 1
    divisiones = 0
    while n != -1
        if a[n] == mayor
            n = -1
        elsif !(a[n].to_i <= k)
            k = a[n].to_i
            divisiones += 1
        end
        n-=1
    end
    resultados.push(divisiones)
end
puts resultados


Vasya has a multiset $$$s$$$ consisting of $$$n$$$ integer numbers. Vasya calls some number $$$x$$$ nice if it appears in the multiset exactly once. For example, multiset $$$\{1, 1, 2, 3, 3, 3, 4\}$$$ contains nice numbers $$$2$$$ and $$$4$$$.Vasya wants to split multiset $$$s$$$ into two multisets $$$a$$$ and $$$b$$$ (one of which may be empty) in such a way that the quantity of nice numbers in multiset $$$a$$$ would be the same as the quantity of nice numbers in multiset $$$b$$$ (the quantity of numbers to appear exactly once in multiset $$$a$$$ and the quantity of numbers to appear exactly once in multiset $$$b$$$).

The first line contains a single integer $$$n~(2 \le n \le 100)$$$. The second line contains $$$n$$$ integers $$$s_1, s_2, \dots s_n~(1 \le s_i \le 100)$$$ — the multiset $$$s$$$.

If there exists no split of $$$s$$$ to satisfy the given requirements, then print "NO" in the first line. Otherwise print "YES" in the first line. The second line should contain a string, consisting of $$$n$$$ characters. $$$i$$$-th character should be equal to 'A' if the $$$i$$$-th element of multiset $$$s$$$ goes to multiset $$$a$$$ and 'B' if if the $$$i$$$-th element of multiset $$$s$$$ goes to multiset $$$b$$$. Elements are numbered from $$$1$$$ to $$$n$$$ in the order they are given in the input. If there exist multiple solutions, then print any of them.

n = gets.to_i
s = gets.split.map(&:to_i)
a = Hash.new(0)
n.times do |i|
  a[s[i]] += 1
end
if a.values.count(1).odd? && a.values.count{|j|j > 2} == 0
  puts "NO"
  exit
end
puts "YES"
ans = "A"*n
p = 0
n.times do |i|
  if a[s[i]] == 1
    if p == 1
      ans[i] = "B"
    end
    p = 1-p
  end
end
if p == 1
  n.times do |i|
    if a[s[i]] > 2
      ans[i] = "B"
      break
    end
  end
end
puts ans


During the hypnosis session, Nicholas suddenly remembered a positive integer $$$n$$$, which doesn't contain zeros in decimal notation. Soon, when he returned home, he got curious: what is the maximum number of digits that can be removed from the number so that the number becomes not prime, that is, either composite or equal to one?For some numbers doing so is impossible: for example, for number $$$53$$$ it's impossible to delete some of its digits to obtain a not prime integer. However, for all $$$n$$$ in the test cases of this problem, it's guaranteed that it's possible to delete some of their digits to obtain a not prime number.Note that you cannot remove all the digits from the number.A prime number is a number that has no divisors except one and itself. A composite is a number that has more than two divisors. $$$1$$$ is neither a prime nor a composite number.

Each test contains multiple test cases. The first line contains one positive integer $$$t$$$ ($$$1 \le t \le 10^3$$$), denoting the number of test cases. Description of the test cases follows. The first line of each test case contains one positive integer $$$k$$$ ($$$1 \le k \le 50$$$) — the number of digits in the number. The second line of each test case contains a positive integer $$$n$$$, which doesn't contain zeros in decimal notation ($$$10^{k-1} \le n < 10^{k}$$$). It is guaranteed that it is always possible to remove less than $$$k$$$ digits to make the number not prime. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^4$$$.

For every test case, print two numbers in two lines. In the first line print the number of digits, that you have left in the number. In the second line print the digits left after all delitions.  If there are multiple solutions, print any.

NoteIn the first test case, you can't delete $$$2$$$ digits from the number $$$237$$$, as all the numbers $$$2$$$, $$$3$$$, and $$$7$$$ are prime. However, you can delete $$$1$$$ digit, obtaining a number $$$27 = 3^3$$$.In the second test case, you can delete all digits except one, as $$$4 = 2^2$$$ is a composite number.

require 'prime'

t = gets.to_i
t.times do

k = gets.to_i
n = gets.chomp.chars.map!(&:to_i)

q = n.map.with_index { |x, i| x << 8 | i }
while (x = q.shift)
  d = x & 0xff
  x >>= 8

  unless Prime.prime?(x)
    puts x.digits.size
    puts x
    break
  end

  n[d.succ..-1].each.with_index(d.succ) do |y, i|
    q << ((x * 10 + y) << 8 | i)
  end
end

end


Let's call the following process a transformation of a sequence of length $$$n$$$.If the sequence is empty, the process ends. Otherwise, append the greatest common divisor (GCD) of all the elements of the sequence to the result and remove one arbitrary element from the sequence. Thus, when the process ends, we have a sequence of $$$n$$$ integers: the greatest common divisors of all the elements in the sequence before each deletion.You are given an integer sequence $$$1, 2, \dots, n$$$. Find the lexicographically maximum result of its transformation.A sequence $$$a_1, a_2, \ldots, a_n$$$ is lexicographically larger than a sequence $$$b_1, b_2, \ldots, b_n$$$, if there is an index $$$i$$$ such that $$$a_j = b_j$$$ for all $$$j < i$$$, and $$$a_i > b_i$$$.

The first and only line of input contains one integer $$$n$$$ ($$$1\le n\le 10^6$$$).

Output $$$n$$$ integers  — the lexicographically maximum result of the transformation.

NoteIn the first sample the answer may be achieved this way:  Append GCD$$$(1, 2, 3) = 1$$$, remove $$$2$$$.  Append GCD$$$(1, 3) = 1$$$, remove $$$1$$$.  Append GCD$$$(3) = 3$$$, remove $$$3$$$. We get the sequence $$$[1, 1, 3]$$$ as the result.

# require 'prime'

def gcd(a, b)
  (b==0 ? a : gcd(b, a%b))
end

def arygcd(ary)
  ans = ary[0]
  ary.each do |x|
    ans = gcd(ans, x)
  end
  ans
end

n = gets.to_i

def solve(ary)
  n = ary.size
  if n==1
    return [1]
  elsif n==2
    return [1, 2]
  elsif n==3
    return [1, 1, 3]
  end
  ans = []
  nary = []
  ary.each do |x|
    if x%2 != 0
      ans << 1
    else
      nary << x/2
    end
  end
  solve(nary).each do |x|
    ans << 2*x
  end
  ans
end

ary = (1..n).to_a
puts solve(ary).join(" ")

n = gets.to_f
x = Math.log(n,2)

count = 0
expon = 0
curr = 2**expon

minicount = 0

while (count < n-2)
  print "#{curr} "
  count += 1
  minicount += 1
  if minicount == (n/(curr*2)).round(0)
    minicount = 0
    expon += 1
    curr = 2**expon
  end
end

thisthing = 2**((x.floor)-1)
if n==1
  puts 1
  exit
end
print "#{thisthing} "
puts "#{(n.to_i/thisthing)*thisthing}"

n = gets.to_f
x = Math.log(n,2)

count = 0
expon = 0
curr = 2**expon

minicount = 0

while (count < n-2)
  print "#{curr} "
  count += 1
  minicount += 1
  if minicount == (n/(curr*2)).round(0)
    minicount = 0
    expon += 1
    curr = 2**expon
  end
end

thisthing = 2**((x.floor)-1)
if n==1
  exit
end
print "#{thisthing} "
puts "#{(n.to_i/thisthing)*thisthing}"


An array $$$b$$$ of length $$$k$$$ is called good if its arithmetic mean is equal to $$$1$$$. More formally, if $$$$$$\frac{b_1 + \cdots + b_k}{k}=1.$$$$$$Note that the value $$$\frac{b_1+\cdots+b_k}{k}$$$ is not rounded up or down. For example, the array $$$[1,1,1,2]$$$ has an arithmetic mean of $$$1.25$$$, which is not equal to $$$1$$$.You are given an integer array $$$a$$$ of length $$$n$$$. In an operation, you can append a non-negative integer to the end of the array. What's the minimum number of operations required to make the array good?We have a proof that it is always possible with finitely many operations.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 50$$$) — the length of the initial array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1,\ldots,a_n$$$ ($$$-10^4\leq a_i \leq 10^4$$$), the elements of the array.

For each test case, output a single integer — the minimum number of non-negative integers you have to append to the array so that the arithmetic mean of the array will be exactly $$$1$$$.

NoteIn the first test case, we don't need to add any element because the arithmetic mean of the array is already $$$1$$$, so the answer is $$$0$$$.In the second test case, the arithmetic mean is not $$$1$$$ initially so we need to add at least one more number. If we add $$$0$$$ then the arithmetic mean of the whole array becomes $$$1$$$, so the answer is $$$1$$$.In the third test case, the minimum number of elements that need to be added is $$$16$$$ since only non-negative integers can be added.In the fourth test case, we can add a single integer $$$4$$$. The arithmetic mean becomes $$$\frac{-2+4}{2}$$$ which is equal to $$$1$$$.

$\ = "\n"
gets.to_i.times do
    n = gets.to_i
    a = gets.split.map &:to_i
    s = a.sum
    if s < n
        print 1
    else
        print s - n
    end
end

gets.to_i.times do
    n = gets.to_i
    a = gets.split.map &:to_i
    s = a.sum
    if s < n
        puts 1
    else
        puts s - n
    end
end

gets.to_i.times do
    n = gets.to_i
    a = gets.split.map &:to_i
    s = a.sum
    puts s < n ? 1 : s - n
end

t = Integer(gets.chomp)
t.times{
    n = Integer(gets.chomp)
    a = gets.chomp.split().map!{|c| Integer(c)}

    som = a.sum()
    len = a.size()

    if(som < len)
        puts 1
    elsif(som == len)
        puts 0
    else
        puts som - len
    end
}


Roma (a popular Russian name that means 'Roman') loves the Little Lvov Elephant's lucky numbers.Let us remind you that lucky numbers are positive integers whose decimal representation only contains lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.Roma's got n positive integers. He wonders, how many of those integers have not more than k lucky digits? Help him, write the program that solves the problem.

The first line contains two integers n, k (1 ≤ n, k ≤ 100). The second line contains n integers ai (1 ≤ ai ≤ 109) — the numbers that Roma has.  The numbers in the lines are separated by single spaces.

In a single line print a single integer — the answer to the problem.

NoteIn the first sample all numbers contain at most four lucky digits, so the answer is 3.In the second sample number 447 doesn't fit in, as it contains more than two lucky digits. All other numbers are fine, so the answer is 2.

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

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

a.select! do |i|
  i.to_s.scan(/[47]/).length <=k
end

puts a.length

n,k=gets.split.map(&:to_i)
a=gets.split.map(&:to_i)
a.select! {|i| i.to_s.scan(/[47]/).length <=k}
puts a.length

n, k = gets.split.map(&:to_i)
puts gets.split.count{|x| x.count('47') <= k}

n, k = gets.split.map(&:to_i)
a = gets.split.map(&:chars)
puts a.count{|x| x.count('4') + x.count('7') <= k}

a=gets.chomp.split(" ")
b=gets.chomp.split(" ")
ans=0
max=a[1].to_i
b.each do |i|
if i.count("4")+i.count("7")<=max
ans+=1
end
end
puts "#{ans}"

k=gets.split[1].to_i
p gets.split.count{|x| x.count('47')<=k}

n, k = gets.split.map &:to_i
a = gets.split.map &:to_i
puts a.inject(0) { |s, x| x.to_s.split('').select { |d| d == '4' || d == '7'}.size <= k ? s + 1 : s }

n,k=gets.split.map(&:to_i)
p gets.split.map{|x| x.scan(/4|7/).size<=k ? 1 : 0}.reduce(:+)

n, k = gets.split.map(&:to_i)
p gets.split.map { |x|
  x.count('4') + x.count('7') <= k ? 1 : 0
}.reduce(:+)

def is_lucky?(n, k)
  while n > 0 do
    k-=1 if n%10 == 4 || n%10 == 7
    n/=10
  end
  k < 0 ? false : true
end

n, k = gets.split.map &:to_i
a = gets.split.map &:to_i
counter = 0
a.each {|el| counter += 1 if is_lucky?(el, k) }

puts counter

n, k = gets.chomp.split.map { |x| x.to_i }
a = gets.chomp.split
count = 0
for i in 0...n
  count += 1 if a[i].scan(/4|7/).count <= k
end
puts count

BEGIN{
	def get_line_int()
		return gets.split.map(&:to_i)
	end
	def count(x)
		cc = 0
		x.to_s.each_char do |c|
			if c == "4" or c == "7" then
				cc += 1
			end
		end
		return cc
	end
	def main()
		n, k = get_line_int
		a = get_line_int
		cc = 0
		a.each do |i|
			if count(i) <= k then
				cc += 1
			end
		end
		puts cc
	end
	Test_case = 1
	Test_case.times do
		main()
	end
}

a = gets.split(" ").collect{|x| x.to_i}
n = a[0]
k = a[1]

a = gets.split(" ").collect{|x| x.to_i}
result = 0

a.each{|x|
	c = 0
	while x > 0
		t = x % 10
		x /= 10
		if t == 4 || t == 7
			c += 1
		end
	end
	if c <= k
		result += 1
	end
}

puts result

n,k=gets.split.map{|s|s.to_i}
a=gets.split.map{|s|s.to_i}
r=0
n.times do |i|
  j=0
  while a[i] > 0 do
    j+=1 if [4,7].include?(a[i]%10)
    a[i] /= 10
  end
  r+=1 if j <= k
end
puts r

n, k = gets.split(' ').map(&:to_i)
puts gets.chomp.split(' ',n).map{|x| x.scan(/[47]/).count}.select{|x| x<=k}.count

st = gets.split
n = st[0].to_i
k = st[1].to_i

st = gets.split
result = 0
n.times do |i|
    x = st[i].count("4")
    y = st[i].count("7")
    if x + y <= k then
        result += 1
    end
end

puts result

 n,k=gets.split.map(&:to_i)
 a=gets.split.map(&:to_s)
 ans=0
 a.each do |ch|
	g=0
	for i in 0...ch.size do
		g+=1 if  ch[i]=='4' or ch[i]=='7'
	end
	ans+=1 if g<=k
 end
 puts ans

k = gets.split[1].to_i
puts gets.split.count{|a| a.count('47') <= k}

# -*- encoding: utf-8 -*-

n, k = gets.chomp.split(" ").map{|n| n.to_i}
digits = gets.chomp.split(" ")

c = 0

digits.each do |a|
  c+=1 if a.count("4") + a.count("7") <= k
end

puts c

n, k = STDIN.readline.split.map {|s| s.to_i }
ss = STDIN.readline.split
count = 0
ss.each do |s|
	count += 1 if s.count('4') + s.count('7') <= k
end
puts count

n,k=gets.chomp.split(' ').map(&:to_i)
sum=n
#puts sum
a=gets.chomp.split(' ').map(&:to_s)
for i in a do
	count=0
	j=i.split('')
	j.each do |x|
		if x=='4' || x=='7'
			count+=1
		end
	end
	if count>k
		sum-=1
	end
end
puts sum

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

a.each do |x|
   counter = 0
   x.split(//).each do |t|
      counter+=1 if t == "4" || t == "7"
   end
   n-=1 if counter>k
end
puts n

n,k = gets.chomp.split(" ").collect! {|x| x.to_i}
line = gets.chomp.split(" ")
s = 0
line.each {|w|
    n = 0
    w.split("").each {|t|
       if t =="4" or t=="7"
        n+=1
       end
    }
    if n<=k
        s +=1
    end
}

puts s

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

a.each do |x|
   counter = 0
   x.split("").each do |t|
      counter+=1 if t == "4" || t == "7"
   end
   n-=1 if counter>k
end
puts n

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

a.each {|x|
   counter = 0
   x.split("").each {|t|
      counter+=1 if t == "4" || t == "7"
   }
   n-=1 if counter>k
}
puts n

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

a.each do |x|
   counter = 0
   x.split("").each do |t|
      counter+=1 if t == "4" || t == "7"
   end
   n-=1 if counter>k
end
puts n

def run
  lines = ARGF.readlines

  while lines.length > 0
    ret = 0

    n, k = lines.shift.chomp.split(" ").map {|x| x.to_i }
    vals = lines.shift.chomp.split(" ")

    vals.each do |val|
      cnt = 0
      val.split(//).each do |c|
        cnt += 1 if c == '4' || c == '7'
      end

      ret += 1 if cnt <= k
    end

    puts ret
  end
end

run if $0 == __FILE__

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

ans = 0
as.each do |a|
  if a.count('4') + a.count('7') <= k
    ans += 1
  end
end

puts ans

n, k = gets.chomp.split(/ /).map(&:to_i)
list = gets.chomp.split(/ /)
ans = 0
list.each do |num|
    lucky_digits = 0
    num.each_char do |d|
        lucky_digits += 1 if d == '4' || d == '7'
    end
    ans += 1 if lucky_digits <= k
end
puts ans

n,k=gets.split.map{|e| e.to_i}
x=gets.split
ans=0
x.each{|e|
	c1=e.chars.count{|e2|
			e2=="4" || e2=="7"
	}
	ans+=1 if c1<=k
}
puts ans

require 'set'
require 'rational'
require 'mathn'
include Math
$stdin = File.open("./input.txt", "r") if ENV["USER"] == "new"
# ====================================

def nice( number, k )
  count = 0
  number.to_s.split(//).each{|c|
    count+=1 if c == "4" || c == "7"
  }
  count <= k
end

n, k = gets.split.map &:to_i
nums = gets.split.map &:to_i

ans = 0
nums.each{|v|
  ans+=1 if nice v, k
}

print ans

n,k = gets.split(" ").map(&:to_i)
arr = gets.split(" ")
count = 0
arr.each { |e|
	count += 1 if e.scan(/[47]/).length <= k
}
puts count

a=gets.split.map &:to_i;b=gets.split.map &:to_i;k=0;a[0].times{|x|b[x].to_s.count('4')+b[x].to_s.count('7')<=a[1] ?(k+=1):(0)};puts k

n,k=gets.split.map(&:to_i)
a=gets.split.select {|i| i.scan(/[47]/).length <=k}
puts a.length

n,k=gets.split.map(&:to_i)
a=gets.split.select {|i| i.to_s.scan(/[47]/).length <=k}
puts a.length

 n,k=gets.split.map(&:to_i)
 a=gets.split.map(&:to_s)
 ans=0
 a.each do |ch|
	g=0
	for i in 0...ch.size do
		g+=1 if  ch[i]==4 or ch[i]==7
	end
	ans+=1 if g<=k
 end
 puts ans

n, k = gets.chomp.split(/ /).map(&:to_i)
list = gets.chomp.split(/ /)
ans = 0
list.each do |num|
    ans += 1 if num.length <= k
end
puts ans


Unfortunately, Vasya can only sum pairs of integers (a, b), such that for any decimal place at least one number has digit 0 in this place. For example, Vasya can sum numbers 505 and 50, but he cannot sum 1 and 4.Vasya has a set of k distinct non-negative integers d1, d2, ..., dk.Vasya wants to choose some integers from this set so that he could sum any two chosen numbers. What maximal number of integers can he choose in the required manner?

The first input line contains integer k (1 ≤ k ≤ 100) — the number of integers. The second line contains k distinct space-separated integers d1, d2, ..., dk (0 ≤ di ≤ 100).

In the first line print a single integer n the maximum number of the chosen integers. In the second line print n distinct non-negative integers — the required integers. If there are multiple solutions, print any of them. You can print the numbers in any order.

#!/usr/bin/ruby1.9

n = STDIN.readline.to_i
nums = STDIN.readline.split.collect {|s| s.to_i }

zeros, one, ten, double, hundred = [0, 0, 0, 0, 0]
nums.each do |num|
	if num == 0
		zeros += 1
	elsif num == 100
		hundred = 1
	elsif num%10 == 0
		ten = num
	elsif num < 10
		one = num
	else
		double = num
	end
end

result = zeros.times.collect { 0 }
if one+ten+double > 0
	if one > 0
		result.push(one)
		if ten > 0
			result.push(ten)
		end
	elsif ten > 0
		result.push(ten)
	else
		result.push(double)
	end
end
if hundred > 0
	result.push(100)
end

puts result.length
puts result.join(' ')

n=gets.to_i
a=gets.chomp.split.map(&:to_i)
b,c=[],[nil,nil,nil,nil,nil]

a.each do |i|
	if i==0
		c[0]=1
	elsif i==100
		c[1]=1
	elsif i%10==0
		c[2]=i
	elsif i<10
		c[3]=i
	else
		c[4]=i
	end
end
b<<0 if c[0]
b<<100 if c[1]
b<<c[2] if c[2]
b<<c[3] if c[3]
b<<c[4] if !c[2] && !c[3] && c[4]
puts b.length
b.each {|i| print "#{i} "}
puts

k = gets.strip.to_i
d = gets.strip.split.map(&:to_i)
ans = []
cnt = []
d.each do |i|
    if i == 0
        cnt[0] = 1
        next
    end
    if i == 100
        cnt[1] = 1
        next
    end
    if i % 10 == 0
        cnt[2] = i
        next
    end
    if i < 10
        cnt[3] = i
        next
    end
    cnt[4] = i
end
ans << 0 if !cnt[0].nil?
ans << 100 if !cnt[1].nil?
ans << cnt[2] if !cnt[2].nil?
ans << cnt[3] if !cnt[3].nil?
ans << cnt[4] if !cnt[4].nil? && cnt[3].nil? && cnt[2].nil?
puts ans.length
puts ans.sort.join(" ")

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
zero = nil
one = nil
two = nil
three = nil

arr.each do |a|
	if !zero &&  a == 0
		zero = a
	elsif !one && a > 0 && a <= 9
		one = a
	elsif !two && a < 100 && (a % 10) == 0
		two = a
	elsif !three && a == 100
		three = a
	end
end
res = [zero,one,two,three].compact
if res.empty?
	res = [arr.first]
elsif !one && !two
	 val = arr.detect{|a| a.to_s.length == 2}
	 res << val if val
end

puts res.count
puts res.join(' ')

def get_bucket n
  s = 0
  if n%10 != 0
    s += 1
  end
  if (n/10)%10 != 0
    s = s + 2
  end
  if n==100
    s = 4
  end
  return s
end

k = gets.to_i
b = [0]*5
nn = [-1]*5
arr = gets.chomp.split(' ').collect {|x| x.to_i}
arr.each { |v|
   b[get_bucket(v)] = 1
   nn[get_bucket(v)] = v
}

m1 = b[0] + b[1] + b[2] + b[4]
m2 = b[3] + b[4] + b[0]

puts [m1,m2].max

if m1 > m2
  r = nn[0..2] + [nn[4]]
else
  r = nn[3..4] + [nn[0]]
end

puts r.keep_if {|x| x!=-1}.join " "

# 305A
# What a retarded task statement! Nothing can be understood...

gets
a = gets.chomp.split

r = []
q = a.find { |t| t == '0' }
r << q if q
w = a.find { |t| t == '100' }
r << w if w
z = a.find { |t| t.size == 1 && t != '0' }
r << z if z
x = a.find { |t| t.size == 2 && t[1] == '0' }
r << x if x
unless z || x
  y = a.find { |t| t.size == 2 }
  r << y if y
end

puts r.size
puts r * ' ' unless r.empty?

ans=[]
rest=[]
multiple10=false
one_digit=false
k=gets.to_i
d=gets.split.map(&:to_i)
d.each{|num|
  if num==0
  	ans<<num
  elsif num==100
  	ans<<num
  elsif !multiple10 && num%10==0
  	ans<<num
  	multiple10=true
  elsif !one_digit && num<10
  	ans<<num
  	one_digit=true
  else
  	rest<<num
  end
}
unless multiple10
  ans<< rest.first if !rest.empty? && !one_digit
end
puts ans.size
puts ans*" "

#!/usr/bin/ruby1.9

zeros = 10.times.collect { false }
n = STDIN.readline.to_i
nums = STDIN.readline.split.collect {|s| s.to_i }

result = [nums.shift]
nums.each do |num|
	a = num
	okay = true
	result.each do |b|
		while a != 0 and b != 0
			if a%10 != 0 and b%10 != 0
				break
			end
			a /= 10
			b /= 10
		end
		if a != 0 and b != 0
			okay = false
			break
		end
	end
	if okay
		result.push(num)
	end
end

puts result.length
puts result.join(' ')

n=gets.to_i
a=gets.chomp.split.map(&:to_i)
b,c=[],[nil,nil,nil,nil,nil]

a.each do |i|
	if i==0
		c[0]=1
	elsif i==100
		c[1]=1
	elsif i%10==0
		c[2]=i
	elsif i<10
		c[3]=i
	else
		c[4]=i
	end
end
b<<0 if c[0]
b<<100 if c[1]
b<<c[2] if c[2]
b<<c[3] if c[3]
b<<c[4] if !c[2] && !c[3] && !c[4]
puts b.length
b.each {|i| print "#{i} "}
puts

n = gets.to_i
arr = gets.split(" ")
res = arr.select{|a| a =~ /[0]/}
rem = arr - res
r = res.length-1
res << rem[0..r]
puts res.count
puts res.join(' ')

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
zero = nil
one = nil
two = nil
three = nil

arr.each do |a|
	if !zero &&  a == 0
		zero = a
	elsif !one && a > 0 && a <= 9
		one = a
	elsif !two && a < 100 && (a % 10) == 0
		two = a
	elsif !three && a == 100
		three = a
	end
end
res = [zero,one,two,three].compact

puts res.count
puts res.join(' ')

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
zero = nil
one = nil
two = nil
three = nil

arr.each do |a|
	if !zero &&  a == 0
		zero = a
	elsif !one && a > 0 && a <= 9
		one = a
	elsif !two && a < 100 && (a % 10) == 0
		two = a
	elsif !three && a == 100
		three = a
	end
end
res = [zero,one,two,three].compact
if res.empty?
	res = [arr.first]
end

puts res.count
puts res.join(' ')

# 305A

gets
a = gets.chomp.split
if a.size == 1
  puts 1
  puts a * ' '
  exit
end
r = []
z = a.find { |t| t.size == 1 && t != '0' }
r << z if z
n = a.find { |t| t == '0' }
r << n if n
u = a.find { |t| t =~ /^\d0$/ }
r << u if u
n = a.find { |t| t == '100' }
r << n if n
if n && z && !u
  u = a.find { |t| t.size == 2 }
  r << u if u
end

if r.empty?
  r << a[0]
end
puts r.size
puts r * ' ' unless r.empty?

# 305A

gets
a = gets.chomp.split
r = []
n = a.find { |t| t.size == 1 && t != '0' }
r << n if n
n = a.find { |t| t == '0' }
r << n if n
n = a.find { |t| t =~ /^\d0$/ }
r << n if n
n = a.find { |t| t == '100' }
r << n if n

puts r.size
puts r * ' ' unless r.empty?

# 305A

gets
a = gets.chomp.split
if a.size == 1
  puts 1
  puts a * ' '
  exit
end
r = []
n = a.find { |t| t.size == 1 && t != '0' }
r << n if n
n = a.find { |t| t == '0' }
r << n if n
u = a.find { |t| t =~ /^\d0$/ }
r << u if u
n = a.find { |t| t == '100' }
r << n if n
if n && !u
  u = a.find { |t| t.size == 2 }
  r << u if u
end

if r.empty?
  r << a[0]
end
puts r.size
puts r * ' ' unless r.empty?

# 305A

gets
a = gets.chomp.split
if a.size == 1
  puts 1
  puts a * ' '
end
r = []
n = a.find { |t| t.size == 1 && t != '0' }
r << n if n
n = a.find { |t| t == '0' }
r << n if n
n = a.find { |t| t =~ /^\d0$/ }
r << n if n
n = a.find { |t| t == '100' }
r << n if n

puts r.size
puts r * ' ' unless r.empty?

# 305A

gets
a = gets.chomp.split
if a.size == 1
  puts 1
  puts a * ' '
  exit
end
r = []
n = a.find { |t| t.size == 1 && t != '0' }
r << n if n
n = a.find { |t| t == '0' }
r << n if n
n = a.find { |t| t =~ /^\d0$/ }
r << n if n
n = a.find { |t| t == '100' }
r << n if n

if r.empty?
  r << a[0]
end
puts r.size
puts r * ' ' unless r.empty?

# 305A

gets
a = gets.chomp.split
if a.size == 1
  puts 1
  puts a * ' '
  exit
end
r = []
z = a.find { |t| t.size == 1 && t != '0' }
r << z if z
w = a.find { |t| t == '0' }
r << w if w
u = a.find { |t| t =~ /^\d0$/ }
r << u if u
n = a.find { |t| t == '100' }
r << n if n
if (n && w && !u) || r == ['0']
  u = a.find { |t| t.size == 2 }
  r << u if u
end

if r.empty?
  r << a[0]
end
puts r.size
puts r * ' ' unless r.empty?

# 305A

gets
a = gets.chomp.split
if a.size == 1
  puts 1
  puts a * ' '
  exit
end
r = []
z = a.find { |t| t.size == 1 && t != '0' }
r << z if z
w = a.find { |t| t == '0' }
r << w if w
u = a.find { |t| t =~ /^\d0$/ }
r << u if u
n = a.find { |t| t == '100' }
r << n if n
if n && w && !u
  u = a.find { |t| t.size == 2 }
  r << u if u
end

if r.empty?
  r << a[0]
end
puts r.size
puts r * ' ' unless r.empty?

# 305A

gets
a = gets.chomp.split
if a.size == 1
  puts 1
  puts a * ' '
  exit
end
r = []
n = a.find { |t| t.size == 1 && t != '0' }
r << n if n
n = a.find { |t| t == '0' }
r << n if n
n = a.find { |t| t =~ /^\d0$/ }
r << n if n
n = a.find { |t| t == '100' }
r << n if n

puts r.size
puts r * ' ' unless r.empty?

# 305A

gets
a, b = gets.chomp.split.partition { |token| token.include? '0' }
if a.empty?
  puts 0
else
  a << b[0] unless b.empty?
  puts a.size
  puts a * ' '
end

ans=[]
diff=[]
k=gets.to_i
d=gets.split.map(&:to_i)
d.each{|num|
  if num.to_s.count(?0)>0
    ans<<num
  else
    diff<<num
  end
}
unless ans.empty?
  ans<<diff.first unless diff.empty?
end
puts ans.size
puts ans*" "

ans=[]
diff=[]
k=gets.to_i
d=gets.split.map(&:to_i)
d.each{|num|
  if num.to_s.count(?0)>0
    ans<<num
  else
    diff<<num
  end
}
ans<<diff.first unless diff.empty?
puts ans.size
puts ans*" "


While Mike was walking in the subway, all the stuff in his back-bag dropped on the ground. There were several fax messages among them. He concatenated these strings in some order and now he has string s.  He is not sure if this is his own back-bag or someone else's. He remembered that there were exactly k messages in his own bag, each was a palindrome string and all those strings had the same length.He asked you to help him and tell him if he has worn his own back-bag. Check if the given string s is a concatenation of k palindromes of the same length.

The first line of input contains string s containing lowercase English letters (1 ≤ |s| ≤ 1000). The second line contains integer k (1 ≤ k ≤ 1000).

Print "YES"(without quotes) if he has worn his own back-bag or "NO"(without quotes) otherwise.

NotePalindrome is a string reading the same forward and backward.In the second sample, the faxes in his back-bag can be "saddas" and "tavvat".

#!/usr/bin/env ruby
s=gets.chomp
k=gets.to_i
puts s.size%k==0 && k.times.all?{|i|t=s[s.size/k*i,s.size/k];t==t.reverse} ? :YES : :NO

# http://codeforces.com/contest/548/problem/A
def palindrome?(a)
  a == a.reverse
end

def valid?(chars, k)
  return false if chars.length % k != 0

  messages = chars.each_slice(chars.length / k).to_a
  messages.all? { |m| palindrome?(m) }
end

chars = gets.chomp.chars
k = gets.chomp.to_i

puts valid?(chars, k) ? "YES" : "NO"

str = gets.chomp;
n = gets.to_i;
ans = false;
ans = str.chars.each_slice(str.length / n).all?{|x| x == x.reverse} if (str.length % n == 0);
puts ans ? "YES\n":"NO\n";

string = gets.chomp
k = gets.chomp.to_i
flag = true

def palindrome?(str)
	str == str.reverse
end

parts = []
nowStart = 0

until parts.length == k - 1
	parts << string.slice(nowStart, string.length / k)
	nowStart += string.length / k

	#puts nowStart
end

parts << string.slice(nowStart, string.length - nowStart)

=begin
parts.each do |partString|
	puts "partString : #{partString}"
end
=end

parts.each do |partString|
	flag = false if !palindrome?(partString)
	flag = false if partString.length != string.length / k
end

puts "YES" if flag
puts "NO" if !flag

a=gets.chomp
b=gets.chomp.to_i
l=a.length
if l%b!=0
puts "NO"
else
tr=0
i=0
b=l/b
if b>1
loop do
if a[i..i+b-1]!=a[i..i+b-1].reverse
tr=1
break
else
i+=b
end
break if i==l
end
end

if tr==1
puts "NO"
else
puts "YES"
end
end

s=gets.chomp
n=gets.to_i

if s.size%n!=0 then
	puts "NO"
else
	m=s.size/n
	ans="YES"
	s.chars.to_a.each_slice(m).each{|e|
		if e!=e.reverse then
			ans="NO"
		end
	}
	puts ans
end

def valid_fax(fax, num_pals)

  word_len = fax.size / num_pals

  ans = false
  if fax.size % num_pals == 0
    ans = fax.each_slice(word_len).all? { |wrd| wrd == wrd.reverse }
  end
  ans
end

words = gets().chomp.chars
len = gets().strip
if valid_fax(words,len.to_i) == true
  puts 'YES'
else
  puts 'NO'
end

#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

# a.rb

def is_palindrome(s)
    s.size.times do |i|
        return false if s[i] != s[s.size - i - 1]
    end
    return true
end

def ok(s, k)
    return false if s.size % k != 0
    n = s.size / k
    k.times do |i|
        return false if !is_palindrome(s[n * i, n])
    end
    return true
end

def main()
    s = gets.chomp
    k = gets.to_i

    puts ok(s, k) ? "YES" : "NO"
end

if __FILE__ == $0 then
    main()
end

s = gets.chomp
k = gets.to_i
if s.size%k != 0
    puts "NO"
    exit
end
len = s.size/k
(0...s.size).step(len).each do |i|
    if s[i,len] != s[i,len].reverse
        puts "NO"
        exit
    end
end
puts "YES"

s = gets.chomp.chars
k = gets.to_i
if s.size % k == 0
  ans = s.each_slice(s.size / k).all?{|x| x == x.reverse}
else
  ans = false
end
puts ans ? 'YES' : 'NO'

s = gets.chomp.chars
k = gets.to_i
a = []
valid = false
s.size.times do |i|
  a << s[i]
  if a == a.reverse
    start = a.size
    c = 1
    valid = true
    (k - 1).times do
      t = s[start...start + a.size]
      if start + a.size > s.size || t != t.reverse
        valid = false
        break
      end
      start += a.size
    end
    valid = false if a.size * k != s.size
    break if valid
  end
end
puts valid ? 'YES' : 'NO'

#!ruby

s = gets.chomp
k = gets.chomp.to_i

ul = s.length/k
if ul*k != s.length
  puts "NO"
  exit
end

0.upto(k-1) do |i|
  ss = s[ul*i..ul*(i+1)-1]
  if ss != ss.reverse
    puts "NO"
    exit
  end
end
puts "YES"
exit

# http://codeforces.com/contest/548/problem/A
def palindrome?(a)
  a == a.reverse
end

chars = gets.chomp.chars
k = gets.chomp.to_i

messages = chars.each_slice(chars.length / k).to_a
puts messages.all? { |m| palindrome?(m) } ? "YES" : "NO"

str = gets.chomp;
n = gets.to_i;
ans = 0;
ans = str.chars.each_slice(str.length / n).all?{|x| x == x.reverse} if (str.length % n == 0);
puts ans ? "YES\n":"NO\n";

str = gets.chomp;
n = gets.to_i;
ans = 0;
ans = str.chars.each_slice(str.length / n).all?{|x| x == x.reverse} if (str.length % n == 0);
puts ans == 1 ? "YES\n":"NO\n";

str = gets.chomp;
n = gets.to_i;
if (str.length / n > 0)
  arr = str.chars.each_slice(str.length / n).map(&:join);
  puts arr.all?{|x| x.length == arr[0].length && x == x.reverse} ? "YES":"NO";
else
  puts "NO\n";
end

str = gets.chomp;
n = gets.to_i;
if (str.length / n > 0)
  arr = str.chars.each_slice(str.length / n).map(&:join);
  puts arr.all?{|x| x.length == arr[0].length && x = x.reverse} ? "YES":"NO";
else
  puts "NO\n";
end

string = gets.chomp
k = gets.chomp.to_i
flag = true

def palindrome?(str)
	str == str.reverse
end

parts = []
nowStart = 0

until parts.length == k - 1
	parts << string.slice(nowStart, string.length / k)
	nowStart += string.length / k

	#puts nowStart
end

parts << string.slice(nowStart, string.length - nowStart)

=begin
parts.each do |partString|
	puts "partString : #{partString}"
end
=end

parts.each do |partString|
	flag = false if !palindrome?(partString)
	flag = false if partString.length != k
end

puts "YES" if flag
puts "NO" if !flag

string = gets.chomp
k = gets.chomp.to_i
flag = true

def palindrome?(str)
	str == str.reverse
end

parts = []
nowStart = 0

until parts.length == k
	parts << string.slice(nowStart, string.length / k)
	nowStart += string.length / k

	#puts nowStart
end

parts.each do |partString|
	flag = false if !palindrome?(partString)
end

puts "YES" if flag
puts "NO" if !flag

string = gets.chomp
k = gets.chomp.to_i
flag = true

def palindrome?(str)
	str == str.reverse
end

parts = []
nowStart = 0

until parts.length == k
	parts << string.slice(nowStart, string.length / k)
	nowStart += string.length / k

	puts nowStart
end

parts.each do |partString|
	flag = false if !palindrome?(partString)
end

puts "YES" if flag
puts "NO" if !flag

string = gets.chomp
k = gets.chomp.to_i
flag = true

def palindrome?(str)
	str == str.reverse
end

parts = []
nowStart = 0

until parts.length == k
	parts << string.slice(nowStart, string.length / k)
end

parts.each do |partString|
	flag = false if !palindrome? partString
end

puts "YES" if flag
puts "NO" if !flag

string = gets.chomp
k = gets.chomp.to_i
flag = true

def palindrome?(str)
	str == str.reverse
end

parts = []
nowStart = 0

until parts.length == k
	parts << string.slice(nowStart, string.length / k)
	nowStart += string.length / k

	#puts nowStart
end

parts.each do |partString|
	flag = false if !palindrome? partString
end

puts "YES" if flag
puts "NO" if !flag

a=gets.chomp
b=gets.chomp.to_i
l=a.length
if l%b!=0
puts "NO"
else
tr=0
i=0
if b>1
b=l/b
loop do
if a[i..i+b-1]!=a[i..i+b-1].reverse
tr=1
break
else
i+=b
end
break if i==l
end
end

if tr==1
puts "NO"
else
puts "YES"
end
end

def is_pal(word, word2)
  word == word2.reverse
end

def valid_fax(fax, word_len)

  #puts "fax size = #{fax.size-1}, word_len = #{word_len-1} mod #{(fax.size-1 % word_len-1)}"
  if ((fax.size-1) % (word_len-1)) != 0
    #puts "here"
    return false
  end

  i = 0
  puts fax.size
  while i < fax.size-1
    #puts "Testing #{fax[i,word_len]} and #{fax[i+word_len, word_len]}"
    unless is_pal(fax[i, word_len], fax[i+word_len, word_len])
      return false
    end
    i += word_len * 2
  end
  return true
end

words = gets()
len = gets()
if valid_fax(words,len.to_i + 1)
  puts 'YES'
else
  puts 'NO'
end

def is_pal(word)
  word == word.reverse
end

def valid_fax(fax, num_pals)

  #puts "Fax.size #{fax.size}, number of pals #{num_pals}"
  word_len = (fax.size) / num_pals
  #puts "pals are of length #{word_len}, size = #{fax.size}"

  #puts "#{word_len}"
  if word_len == 0
    return false
  end

  if (fax.size)%word_len != 0
    return false
  end

  fax.each_slice(word_len) do |wrd|
    return false unless is_pal(wrd)
  end
  return true
end

words = gets().strip.chars
len = gets().strip
if valid_fax(words,len.to_i)
  puts 'YES'
else
  puts 'NO'
end

def valid_fax(fax, num_pals)

  word_len = (fax.size) / num_pals

  #puts "#{word_len}"
  if word_len == 0
    return false
  end
  ans = false
  if fax.size % word_len == 0
    ans = fax.each_slice(word_len).all? do |wrd|
      wrd == wrd.reverse
    end
  end
  ans
end

words = gets().chomp.chars
len = gets().strip
if valid_fax(words,len.to_i) == true
  puts 'YES'
else
  puts 'NO'
end

def valid_fax(fax, num_pals)

  word_len = (fax.size) / num_pals

  #puts "#{word_len}"
  if word_len == 0
    return false
  end
  if fax.size % word_len != 0
    return false
  end

  return fax.each_slice(word_len).all? do |wrd|
    wrd == wrd.reverse
  end
end

words = gets().strip.chars
len = gets().strip
if valid_fax(words,len.to_i)
  puts 'YES'
else
  puts 'NO'
end

def valid_fax(fax, num_pals)

  word_len = (fax.size) / num_pals

  #puts "#{word_len}"
  if word_len == 0
    return false
  end
  if fax.size % word_len != 0
    return false
  end

  ! fax.each_slice(word_len).any? do |wrd|
    puts wrd
    wrd != wrd.reverse
  end
end

words = gets().strip.chars
len = gets().strip
if valid_fax(words,len.to_i)
  puts 'YES'
else
  puts 'NO'
end

def is_pal(word, word2)
  word == word2.reverse
end

def valid_fax(fax, word_len)

  #puts "fax size = #{fax.size-1}, word_len = #{word_len-1} mod #{(fax.size-1 % word_len-1)}"
  if ((fax.size-1) % (word_len-1)) != 0
    #puts "here"
    return false
  end

  i = 0
  #puts fax.size
  while i < fax.size-1
    #puts "Testing #{fax[i,word_len]} and #{fax[i+word_len, word_len]}"
    unless is_pal(fax[i, word_len], fax[i+word_len, word_len])
      return false
    end
    i += word_len * 2
  end
  return true
end

words = gets()
len = gets()
if valid_fax(words,len.to_i + 1)
  puts 'YES'
else
  puts 'NO'
end

def valid_fax(fax, num_pals)

  word_len = (fax.size) / num_pals

  #puts "#{word_len}"
  if word_len == 0
    return false
  end
  if fax.size % word_len != 0
    return false
  end

  return fax.each_slice(word_len).all? do |wrd|
    wrd == wrd.reverse
  end
end

words = gets().chomp.chars
len = gets().strip
if valid_fax(words,len.to_i)
  puts 'YES'
else
  puts 'NO'
end

def is_pal(word)
  word == word.reverse
end

def valid_fax(fax, num_pals)

  word_len = fax.size / num_pals
  #puts "pals are of length #{word_len}, size = #{fax.size-1}"
  if (fax.size-1)%word_len != 0
    return false
  end

  if word_len == 1
    return true
  end

  i = 0
  #puts fax.size
  while i < fax.size-1
    #puts "Testing #{fax[i,word_len]}"
    unless is_pal(fax[i, word_len])
      return false
    end
    i += word_len * 2
  end
  return true
end

words = gets()
len = gets()
if valid_fax(words,len.to_i)
  puts 'YES'
else
  puts 'NO'
end

def is_pal(word)
  word == word.reverse
end

def valid_fax(fax, num_pals)

  #puts "Fax.size #{fax.size}, number of pals #{num_pals}"
  word_len = (fax.size) / num_pals
  #puts "pals are of length #{word_len}, size = #{fax.size}"

  #puts "#{word_len}"
  if word_len == 0
    return false
  end

  if word_len == 1
    return true
  end
  if (fax.size)%word_len != 0
    return false
  end

  fax.each_slice(word_len) do |wrd|
    return false if !is_pal(wrd)
  end
  return true
end

words = gets().strip.chars
len = gets().strip
if valid_fax(words,len.to_i)
  puts 'YES'
else
  puts 'NO'
end

def is_pal(word, word2)
  word == word2.reverse
end

def valid_fax(fax, word_len)

  if fax%word_len != 0
    return false
  end

  i = 0
  puts fax.size
  while i < fax.size-1
    puts "Testing #{fax[i,word_len]} and #{fax[i+word_len, word_len]}"
    unless is_pal(fax[i, word_len], fax[i+word_len, word_len])
      return false
    end
    i += word_len * 2
  end
  return true
end

words = gets()
len = gets()
if valid_fax(words,len.to_i + 1)
  puts 'YES'
else
  puts 'NO'
end

def is_pal(word)
  word == word.reverse
end

def valid_fax(fax, num_pals)

  word_len = fax.size / num_pals

  if word_len == 1
    return true
  end

  #puts "pals are of length #{word_len}, size = #{fax.size-1}"
  if (fax.size-1)%word_len != 0
    return false
  end

  i = 0
  #puts fax.size
  while i < fax.size-1
    #puts "Testing #{fax[i,word_len]}"
    unless is_pal(fax[i, word_len])
      return false
    end
    i += word_len * 2
  end
  return true
end

words = gets()
len = gets()
if valid_fax(words,len.to_i)
  puts 'YES'
else
  puts 'NO'
end

def is_pal(word)
  word == word.reverse
end

def valid_fax(fax, num_pals)

  word_len = fax.size / num_pals
  #puts "pals are of length #{word_len}, size = #{fax.size-1}"
  if (fax.size-1)%word_len != 0
    return false
  end

  i = 0
  #puts fax.size
  while i < fax.size-1
    #puts "Testing #{fax[i,word_len]}"
    unless is_pal(fax[i, word_len])
      return false
    end
    i += word_len * 2
  end
  return true
end

words = gets()
len = gets()
if valid_fax(words,len.to_i)
  puts 'YES'
else
  puts 'NO'
end

def is_pal(word, word2)
  word == word2.reverse
end

def valid_fax(fax, word_len)

  puts "fax size = #{fax.size-1}, word_len = #{word_len-1} mod #{(fax.size-1 % word_len-1)}"
  if ((fax.size-1) % (word_len-1)) != 0
    puts "here"
    return false
  end

  i = 0
  puts fax.size
  while i < fax.size-1
    #puts "Testing #{fax[i,word_len]} and #{fax[i+word_len, word_len]}"
    unless is_pal(fax[i, word_len], fax[i+word_len, word_len])
      return false
    end
    i += word_len * 2
  end
  return true
end

words = gets()
len = gets()
if valid_fax(words,len.to_i + 1)
  puts 'YES'
else
  puts 'NO'
end

def is_pal(word)
  word == word.reverse
end

def valid_fax(fax, num_pals)

  #puts "Fax.size #{fax.size-1}, number of pals #{num_pals}"
  word_len = (fax.size) / num_pals
  #puts "pals are of length #{word_len}, size = #{fax.size-1}"

  #puts "#{word_len}"
  if word_len == 0
    return false
  end
  if word_len == 1
    return true
  end
  if (fax.size)%word_len != 0
    return false
  end

  fax.each_slice(word_len) do |wrd|
    return false unless is_pal(wrd)
  end
  return true
end

words = gets()
len = gets()
if valid_fax(words,len.to_i)
  puts 'YES'
else
  puts 'NO'
end

def valid_fax(fax, num_pals)

  word_len = (fax.size) / num_pals

  #puts "#{word_len}"
  if word_len == 0
    return false
  end
  if fax.size % word_len != 0
    return false
  end

  return fax.each_slice(word_len).all? do |wrd|
    wrd == wrd.reverse
  end
end

words = gets().chomp.chars
len = gets().strip
if valid_fax(words,len.to_i) == true
  puts 'YES'
else
  puts 'NO'
end

def is_pal(word)
  word == word.reverse
end

def valid_fax(fax, num_pals)

  word_len = fax.size / num_pals
  #puts "pals are of length #{word_len}"

  i = 0
  #puts fax.size
  while i < fax.size-1
    #puts "Testing #{fax[i,word_len]}"
    unless is_pal(fax[i, word_len])
      return false
    end
    i += word_len * 2
  end
  return true
end

words = gets()
len = gets()
if valid_fax(words,len.to_i)
  puts 'YES'
else
  puts 'NO'
end

def valid_fax(fax, num_pals)

  #puts "Fax.size #{fax.size}, number of pals #{num_pals}"
  word_len = (fax.size) / num_pals
  #puts "pals are of length #{word_len}, size = #{fax.size}"

  #puts "#{word_len}"
  if word_len == 0
    return false
  end

  if word_len == 1
    return true
  end
  if fax.size % word_len != 0
    return false
  end

  ! fax.each_slice(word_len).any? do |wrd|
    wrd != wrd.reverse
  end
end

words = gets().strip.chars
len = gets().strip
if valid_fax(words,len.to_i)
  puts 'YES'
else
  puts 'NO'
end

def is_pal(word)
  word == word.reverse
end

def valid_fax(fax, num_pals)

  #puts "Fax.size #{fax.size-1}, number of pals #{num_pals}"
  word_len = (fax.size-1) / num_pals
  #puts "pals are of length #{word_len}, size = #{fax.size-1}"

  #puts "#{word_len}"
  if word_len == 0
    return false
  end
  if word_len == 1
    return true
  end
  if (fax.size-1)%word_len != 0
    return false
  end

  i = 0
  #puts fax.size
  while i < fax.size-1
    #puts "Testing #{fax[i,word_len]}"
    unless is_pal(fax[i, word_len])
      return false
    end
    i += word_len * 2
  end
  return true
end

words = gets()
len = gets()
if valid_fax(words,len.to_i)
  puts 'YES'
else
  puts 'NO'
end

#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

# a.rb

def is_palindrome(s)
    return false if s.size == 1
    s.size.times do |i|
        return false if s[i] != s[s.size - i - 1]
    end
    return true
end

def ok(s, k)
    return false if s.size % k != 0
    n = s.size / k
    k.times do |i|
        return false if !is_palindrome(s[n * i, n])
    end
    return true
end

def main()
    s = gets.chomp
    k = gets.to_i

    puts ok(s, k) ? "YES" : "NO"
end

if __FILE__ == $0 then
    main()
end

#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

# a.rb

def is_palindrome(s)
    return false if s.size == 1
    s.size.times do |i|
        return false if s[i] != s[s.size - i - 1]
    end
    return true
end

def ok(s, k)
    if s == "" && k == 0 then
        return true
    end
    if (s != "" && k == 0) || (s == "" && k > 0) then
        return false
    end

    ret = false
    2.upto(s.size) do |i|
        ss = s[0, i]
        if is_palindrome(ss) then
            ret = ret | ok(s[i, s.size], k - 1)
        end
    end
    return ret
end

def main()
    s = gets.chomp
    k = gets.to_i

    puts ok(s, k) ? "YES" : "NO"
end

if __FILE__ == $0 then
    main()
end

s = gets.chomp.chars
n = gets.to_i
c = 0
a = []
i = 0
while i < s.size
  if a != [] && s[i...i + a.size] == a.reverse
    c += 1
    i += a.size
    a = []
  else
    a << s[i]
    i += 1
  end
end
puts c == n ? 'YES' : 'NO'

s = gets.chomp.chars
k = gets.to_i
a = []
valid = false
s.size.times do |i|
  a << s[i]
  if a == a.reverse
    start = a.size
    c = 1
    valid = true
    (k - 1).times do
      t = s[start...start + a.size]
      if start + a.size >= s.size || t != t.reverse
        valid = false
        break
      end
      start += a.size
    end
    valid = false if a.size * k != s.size
    break if valid
  end
end
puts valid ? 'YES' : 'NO'

s = gets.chomp.chars
k = gets.to_i
a = []
valid = false
s.size.times do |i|
  a << s[i]
  if s[a.size...2 * a.size] == a.reverse
    start = 2 * a.size
    (k - 1).times do
      valid = true
      if s[start...start + a.size] != s[start + a.size...start + 2 * a.size].reverse
        valid = false
        break
      end
      start += 2 * a.size
    end
    break if valid
  end
end
puts valid ? 'YES' : 'NO'

s = gets.chomp.chars
k = gets.to_i
a = []
valid = false
s.size.times do |i|
  a << s[i]
  if a == s[a.size...2 * a.size].reverse
    start = 2 * a.size
    valid = true
    (k - 1).times do
      if start + 2 * a.size > s.size || s[start...start + a.size] != s[start + a.size...start + 2 * a.size].reverse
        valid = false
        break
      end
      start += 2 * a.size
    end
    break if valid
  end
end
puts valid ? 'YES' : 'NO'

s = gets.chomp.chars
k = gets.to_i
puts s.each_slice(s.size / k).to_a.all?{|x| x == x.reverse} ? 'YES' : 'NO'

#!ruby

s = gets.chomp
k = gets.chomp.to_i

ul = s.length/k
0.upto(k-1) do |i|
  ss = s[ul*i..ul*(i+1)-1]
  if ss != ss.reverse
    puts "NO"
    exit
  end
end
puts "YES"
exit


A positive integer is called composite if it can be represented as a product of two positive integers, both greater than $$$1$$$. For example, the following numbers are composite: $$$6$$$, $$$4$$$, $$$120$$$, $$$27$$$. The following numbers aren't: $$$1$$$, $$$2$$$, $$$3$$$, $$$17$$$, $$$97$$$.Alice is given a sequence of $$$n$$$ composite numbers $$$a_1,a_2,\ldots,a_n$$$.She wants to choose an integer $$$m \le 11$$$ and color each element one of $$$m$$$ colors from $$$1$$$ to $$$m$$$ so that:  for each color from $$$1$$$ to $$$m$$$ there is at least one element of this color;  each element is colored and colored exactly one color;  the greatest common divisor of any two elements that are colored the same color is greater than $$$1$$$, i.e. $$$\gcd(a_i, a_j)>1$$$ for each pair $$$i, j$$$ if these elements are colored the same color. Note that equal elements can be colored different colors — you just have to choose one of $$$m$$$ colors for each of the indices from $$$1$$$ to $$$n$$$.Alice showed already that if all $$$a_i \le 1000$$$ then she can always solve the task by choosing some $$$m \le 11$$$.Help Alice to find the required coloring. Note that you don't have to minimize or maximize the number of colors, you just have to find the solution with some $$$m$$$ from $$$1$$$ to $$$11$$$.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then the descriptions of the test cases follow. The first line of the test case contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the amount of numbers in a sequence $$$a$$$. The second line of the test case contains $$$n$$$ composite integers $$$a_1,a_2,\ldots,a_n$$$ ($$$4 \le a_i \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^4$$$.

For each test case print $$$2$$$ lines. The first line should contain a single integer $$$m$$$ ($$$1 \le m \le 11$$$) — the number of used colors. Consider colors to be numbered from $$$1$$$ to $$$m$$$. The second line should contain any coloring that satisfies the above conditions. Print $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$1 \le c_i \le m$$$), where $$$c_i$$$ is the color of the $$$i$$$-th element. If there are multiple solutions then you can print any of them. Note that you don't have to minimize or maximize the number of colors, you just have to find the solution with some $$$m$$$ from $$$1$$$ to $$$11$$$. Remember that each color from $$$1$$$ to $$$m$$$ should be used at least once. Any two elements of the same color should not be coprime (i.e. their GCD should be greater than $$$1$$$).

NoteIn the first test case, $$$\gcd(6,10)=2$$$, $$$\gcd(6,15)=3$$$ and $$$\gcd(10,15)=5$$$. Therefore, it's valid to color all elements the same color. Note that there are other colorings which satisfy Alice's requirement in this test case.In the second test case there is only one element of each color, so the coloring definitely satisfies Alice's requirement.

# https://codeforces.com/contest/1332/problem/B

require 'set'

@primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]

def lowest_divisor(n)
 @primes.each do |p|
  if n % p == 0
   return p
  end
 end
end

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

 answer = ""
 colors = {}
 color_count = 1

 numbers.each do |n|
  color = lowest_divisor(n)
  if colors[color].nil?
   colors[color] = color_count
   color_count += 1
  end

  answer += "#{colors[color]} "
 end

 answer[-1] = ""

 puts colors.size
 puts answer
end

# https://codeforces.com/contest/1332/problem/B

primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]

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

 answer = []
 colors = {}
 color_count = 1

 numbers.each do |n|
  color = primes.find do |p|
   n % p == 0
  end

  if colors[color].nil?
   colors[color] = color_count
   color_count += 1
  end

  answer << colors[color]
 end

 puts colors.size
 puts answer.join(' ')
end

primes = [2,3,5,7,11,13,17,19,23,29,31]
t = gets.chomp.to_i

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

  colors = {}
  coloring = a.map do |aa|
              color = primes.find do |pr|
                aa % pr == 0
              end

              if colors.key?(color)
                colors[color]
              else
                colors[color] = colors.size + 1
              end
            end
  puts colors.size
  puts coloring.join(' ')
end

PRIME = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
t = gets.to_i
t.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  mapping = {}
  used = 0
  c = Array.new(n, nil)
  a.each_with_index do |v, i|
    PRIME.each_with_index do |p, j|
      if v % p == 0
        if !mapping.include?(p)
          used += 1
          mapping[p] = used
        end
        c[i] = mapping[p]
        break
      end
    end
  end
  puts used
  puts c.join(" ")
end

# https://codeforces.com/contest/1332/problem/B

require 'set'

@primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]

def lowest_divisor(n)
 @primes.each do |p|
  if n % p == 0
   return p
  end
 end
end

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

 answer = ""
 colors = Set.new()

 numbers.each do |n|
  color = lowest_divisor(n)
  colors << color
  answer += "#{@primes.index(color) + 1} "
 end

 answer[-1] = ""

 puts answer
end

# https://codeforces.com/contest/1332/problem/B

require 'set'

@primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]

def lowest_divisor(n)
 @primes.each do |p|
  if n % p == 0
   return p
  end
 end
end

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

 answer = ""
 colors = Set.new()

 numbers.each do |n|
  color = lowest_divisor(n)
  colors << color
  answer += "#{@primes.index(color) + 1} "
 end
end

# https://codeforces.com/contest/1332/problem/B

require 'set'

@primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]

def lowest_divisor(n)
 @primes.each do |p|
  if n % p == 0
   return p
  end
 end
end

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

 answer = ""
 colors = Set.new()

 numbers.each do |n|
  color = lowest_divisor(n)
  colors << color
  answer += "#{@primes.index(color) + 1} "
 end

 answer.rstrip!
end

# https://codeforces.com/contest/1332/problem/B

require 'set'

@primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]

def lowest_divisor(n)
 @primes.each do |p|
  if n % p == 0
   return p
  end
 end
end

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

 answer = ""
 colors = Set.new()

 numbers.each do |n|
  color = lowest_divisor(n)
  colors << color
  answer += "#{@primes.index(color) + 1} "
 end

 answer[-1] = ""

 puts colors.size
 puts answer
end


You are given a rebus of form ? + ? - ? + ? = n, consisting of only question marks, separated by arithmetic operation '+' and '-', equality and positive integer n. The goal is to replace each question mark with some positive integer from 1 to n, such that equality holds.

The only line of the input contains a rebus. It's guaranteed that it contains no more than 100 question marks, integer n is positive and doesn't exceed 1 000 000, all letters and integers are separated by spaces, arithmetic operations are located only between question marks.

The first line of the output should contain "Possible" (without quotes) if rebus has a solution and "Impossible" (without quotes) otherwise. If the answer exists, the second line should contain any valid rebus with question marks replaced by integers from 1 to n. Follow the format given in the samples.

def solve(line)
  plus = 1
  minu = 0

  line.each_char do |ch|
    if ch == '+'
      plus += 1
    elsif ch == '-'
      minu += 1
    end
  end

  line =~ /([0-9]+)/
  n = Regexp.last_match[0].to_i

  if n > plus * n - minu || n < plus - minu * n
    puts "Impossible"
    return
  end

  curr = plus - minu

  signs = '+' + line.gsub(/[^+-]/, '')

  first = true

  puts "Possible"

  signs.each_char do |sign|
    print " #{sign} " if !first
    first = false
    if sign == '+'
      if curr >= n
        print 1
      else
        x = [n - curr + 1, n].min
        curr += x - 1
        print x
      end
    else
      if curr <= n
        print 1
      else
        x = [curr - n + 1, n].min
        curr -= x - 1
        print x
      end
    end
  end

  puts " = #{n}"
end

while line = gets
  solve(line.chomp)
end

def isok(arr, n, target)
  pos, neg = arr.count{|x| x == "+"}, arr.count{|x| x == '-'}
  max, min = pos * n - neg, pos - neg * n
  #print "##", max, " ", min, " ", target
  #puts ""
  if target <= max && target >= min
    return 0
  elsif target > max
    return -1
  else
    return 1
  end
end

arr = readline.gsub(/[?=]/, "").split(' ')
n = arr.last.to_i
arr = ["+"] + arr[0..-2]

if isok(arr, n, n) == 0
  puts "Possible"
  target = n
  0.upto(arr.length - 1) do |ind|
    ans = 0
    flag = arr[ind] == "+" ? 1 : -1
    if isok(arr[ind + 1..-1], n, target - flag) == 0
      ans = 1
    elsif isok(arr[ind + 1..-1], n, target - n * flag) == 0
      ans = n
    else
      down, up = 1, n
      downf = isok(arr[ind + 1..-1], n, target - flag)
      upf = isok(arr[ind + 1..-1], n, target - n * flag)
      while down + 1 < up
        mid = (down + up) / 2
        res = isok(arr[ind + 1..-1], n, target - mid * flag)
        if res == 0
          ans = mid
          break
        elsif res == downf
          down = mid
        else
          up = mid
        end
      end
    end
    if ind != 0
      print " " + arr[ind] + " "
    end
    print ans
    target -= ans * flag
  end
  print " = "
  puts n
else
  puts "Impossible"
end

arr=gets.split('?')
arr[0]='+'
sz=arr.size
n=arr[sz-1]
n=n[3,n.size-3].to_i
a=Array.new(111)
s=-n
for i in 0..sz-2
	if arr[i].strip=='+'
		a[i]=n
		s+=n
	else
		a[i]=1
		s-=1
	end
end
for i in 0..sz-2
	if s>0 && arr[i].strip=='+'
		det = [n-1, s].min
		a[i]-=det
		s-=det
	end
	if s>0 && arr[i].strip=='-'
		det = [n-1, s].min
		a[i]+=det
		s-=det
	end
end
if s!=0
	puts "Impossible"
else
	puts "Possible"
	arr[0]=""
	for i in 0..sz-2
		print "#{arr[i]}#{a[i]}"
	end
	puts arr[sz-1]
end

str = ("+ " + gets).split
l = str.length
m = l/2 -1
n = str[l-1].to_i
a = Array.new(m){|e| e = 0}
s = 0
m.times{ |i|
	a[i] = str[i*2]=='+'?1: -1;
	s+=a[i]
}

i = 0

if (s<n)
	while(i<m && s!=n)
		if a[i]>0 && a[i]<n
			s+=1;a[i]+=1;
		else
			i+=1;
		end
	end
elsif s>n
	while(i<m && s!=n)
		if a[i]<0 && a[i]>-n
			s-=1;a[i]-=1;
		else
			i+=1;
		end
	end
end

if s!=n
	print "Impossible\n"
else
	print "Possible\n"
	str = ""
	for i in (0...m)
		str += "#{a[i]}" if i==0
		if i!=0
			if a[i]>0
				str += " + #{a[i]}"
			else
				str += " - #{-a[i]}"
			end
		end
	end
	str += " = #{n}"
	print str
end

def isok(arr, n, target)
  pos, neg = arr.count{|x| x == "+"}, arr.count{|x| x == '-'}
  max, min = pos * n - neg, pos - neg * n
  #print "##", max, " ", min, " ", target
  #puts ""
  if target <= max && target >= min
    return 0
  elsif target > max
    return -1
  else
    return 1
  end
end

arr = readline.gsub(/[?=]/, "").split(' ')
n = arr.last.to_i
arr = ["+"] + arr[0..-2]

if isok(arr, n, n) == 0
  puts "Possible"
  target = n
  0.upto(arr.length - 1) do |ind|
    ans = 0
    flag = arr[ind] == "+" ? 1 : -1
    if isok(arr[ind + 1..-1], n, target - flag) == 0
      ans = 1
    elsif isok(arr[ind + 1..-1], n, target - n * flag) == 0
      ans = n
    else
      down, up = 1, n
      downf = isok(arr[ind + 1..-1], n, target - flag)
      upf = isok(arr[ind + 1..-1], n, target - n * flag)
      while down + 1 < up
        mid = (down + up) / 2
        res = isok(arr[ind + 1..-1], n, target - mid)
        if res == 0
          ans = mid
          break
        elsif res == downf
          down = mid
        else
          up = mid
        end
      end
    end
    if ind != 0
      print " " + arr[ind] + " "
    end
    print ans
    target -= ans * flag
  end
  print " = "
  puts n
else
  puts "Impossible"
end

def isok(arr, n, target)
  pos, neg = arr.count{|x| x == "+"}, arr.count{|x| x == '-'}
  max, min = pos * n - neg, pos - neg * n
  #print "##", max, " ", min, " ", target
  #puts ""
  if target <= max && target >= min
    return 0
  elsif target > max
    return -1
  else
    return 1
  end
end

arr = readline.gsub(/[?=]/, "").split(' ')
n = arr.last.to_i
arr = ["+"] + arr[0..-2]

if isok(arr, n, n) == 0
  target = n
  0.upto(arr.length - 1) do |ind|
    ans = 0
    flag = arr[ind] == "+" ? 1 : -1
    if isok(arr[ind + 1..-1], n, target - flag) == 0
      ans = 1
    elsif isok(arr[ind + 1..-1], n, target - n * flag) == 0
      ans = n
    else
      down, up = 1, n
      downf = isok(arr[ind + 1..-1], n, target - flag)
      upf = isok(arr[ind + 1..-1], n, target - n * flag)
      while down + 1 < up
        mid = (down + up) / 2
        res = isok(arr[ind + 1..-1], n, target - mid)
        if res == 0
          ans = mid
          break
        elsif res == downf
          down = mid
        else
          up = mid
        end
      end
    end
    if ind != 0
      print " " + arr[ind] + " "
    end
    print ans
    target -= ans * flag
  end
  print " = "
  puts n
else
  puts "Impossible"
end

arr=gets.split('?')
arr[0]='+'
sz=arr.size
n=arr[sz-1]
n=n[3,n.size-3].to_i
a=Array.new(111)
s=-n
for i in 0..sz-2
	if arr[i].strip=='+'
		a[i]=n
		s+=n
	else
		a[i]=1
		s-=1
	end
end
for i in 0..sz-2
	if s>0 && a[i]==n
		det = [n-1, s].min
		a[i]-=det
		s-=det
	end
end
if s!=0
	puts "Impossible"
else
	puts "Possible"
	arr[0]=""
	for i in 0..sz-2
		print "#{arr[i]}#{a[i]}"
	end
	puts arr[sz-1]
end


Little Alyona is celebrating Happy Birthday! Her mother has an array of n flowers. Each flower has some mood, the mood of i-th flower is ai. The mood can be positive, zero or negative.Let's define a subarray as a segment of consecutive flowers. The mother suggested some set of subarrays. Alyona wants to choose several of the subarrays suggested by her mother. After that, each of the flowers will add to the girl's happiness its mood multiplied by the number of chosen subarrays the flower is in.For example, consider the case when the mother has 5 flowers, and their moods are equal to 1,  - 2, 1, 3,  - 4. Suppose the mother suggested subarrays (1,  - 2), (3,  - 4), (1, 3), (1,  - 2, 1, 3). Then if the girl chooses the third and the fourth subarrays then:   the first flower adds 1·1 = 1 to the girl's happiness, because he is in one of chosen subarrays,  the second flower adds ( - 2)·1 =  - 2, because he is in one of chosen subarrays,  the third flower adds 1·2 = 2, because he is in two of chosen subarrays,  the fourth flower adds 3·2 = 6, because he is in two of chosen subarrays,  the fifth flower adds ( - 4)·0 = 0, because he is in no chosen subarrays. Thus, in total 1 + ( - 2) + 2 + 6 + 0 = 7 is added to the girl's happiness. Alyona wants to choose such subarrays from those suggested by the mother that the value added to her happiness would be as large as possible. Help her do this!Alyona can choose any number of the subarrays, even 0 or all suggested by her mother.

The first line contains two integers n and m (1 ≤ n, m ≤ 100) — the number of flowers and the number of subarrays suggested by the mother. The second line contains the flowers moods — n integers a1, a2, ..., an ( - 100 ≤ ai ≤ 100). The next m lines contain the description of the subarrays suggested by the mother. The i-th of these lines contain two integers li and ri (1 ≤ li ≤ ri ≤ n) denoting the subarray a[li], a[li + 1], ..., a[ri]. Each subarray can encounter more than once.

Print single integer — the maximum possible value added to the Alyona's happiness.

NoteThe first example is the situation described in the statements.In the second example Alyona should choose all subarrays.The third example has answer 0 because Alyona can choose none of the subarrays.

n, m=gets.split.map &:to_i
a=gets.split.map &:to_i

s=[0]
for i in 1..a.size
    s<<s[i-1]+a[i-1]
end

res=0
m.times {
    l, r=gets.split.map &:to_i
    x=s[r]-s[l-1]
    res+=x if x>0
}
p res


A tuple of positive integers {x1, x2, ..., xk} is called simple if for all pairs of positive integers (i,  j) (1  ≤ i  <  j ≤ k), xi  +  xj is a prime.You are given an array a with n positive integers a1,  a2,  ...,  an (not necessary distinct). You want to find a simple subset of the array a with the maximum size.A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.Let's define a subset of the array a as a tuple that can be obtained from a by removing some (possibly all) elements of it.

The first line contains integer n (1 ≤ n ≤ 1000) — the number of integers in the array a. The second line contains n integers ai (1 ≤ ai ≤ 106) — the elements of the array a.

On the first line print integer m — the maximum possible size of simple subset of a. On the second line print m integers bl — the elements of the simple subset of the array a with the maximum size. If there is more than one solution you can print any of them. You can print the elements of the subset in any order.

def sieve
  low = 0
  high = 2000001
  arr = []
  for i in 0..high do
    arr[i] = true
  end
  for i in 2..Math.sqrt(high).ceil do
    if arr[i]
      j = i*i
      while j < high
        arr[j] = false
        j += i
      end
    end
  end
  return arr
end

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

# count 1's
cnt = 0
arr.each do |val|
  cnt += 1 if val == 1
  break if cnt > 1
end

subset = []
if cnt > 1
  arr.each do |val|
    subset.push(val) if val == 1
  end
  arr.each do |val|
    if(primes[val + 1] and val != 1)
      subset.push(val)
      break
    end
  end
else
  done = false
  for i in 0...n - 1
    for j in i + 1..n - 1
      if(primes[arr[i] + arr[j]])
        subset.push(arr[i])
        subset.push(arr[j])
        done = true
        break
      end
    end
    break if done
  end
end
subset[0] = arr[0] if subset.length == 0
puts subset.length
puts subset.join(" ")

def sieve
  low = 0
  high = 2000000
  arr = []
  for i in 0..high do
    arr[i] = true
  end
  for i in 2..Math.sqrt(high).ceil do
    if arr[i]
      j = i*i
      while j < high
        arr[j] = false
        j += i
      end
    end
  end
  return arr
end

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

# count 1's
cnt = 0
arr.each do |val|
  cnt += 1 if val == 1
  break if cnt > 1
end

subset = []
if cnt > 1
  arr.each do |val|
    subset.push(val) if val == 1
  end
  arr.each do |val|
    if(primes[val + 1] and val != 1)
      subset.push(val)
      break
    end
  end
else
  for i in 0..n - 1
    for j in i..n - 1
      if(primes[arr[i] + arr[j]])
        subset.push(arr[i])
        subset.push(arr[j])
        break
      end
    end
  end
end
puts subset.length
puts subset.join(" ")

def sieve
  low = 0
  high = 2000000
  arr = []
  for i in 0..high do
    arr[i] = true
  end
  for i in 2..Math.sqrt(high).ceil do
    if arr[i]
      j = i*i
      while j < high
        arr[j] = false
        j += i
      end
    end
  end
  return arr
end

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

# count 1's
cnt = 0
arr.each do |val|
  cnt += 1 if val == 1
  break if cnt > 1
end

subset = []
if cnt > 1
  arr.each do |val|
    subset.push(val) if val == 1
  end
  arr.each do |val|
    if(primes[val + 1] and val != 1)
      subset.push(val)
      break
    end
  end
else
  for i in 0..n - 1
    for j in i..n - 1
      if(primes[arr[i] + arr[j]])
        subset.push(arr[i])
        subset.push(arr[j])
        break
      end
    end
  end
end
subset[0] = arr[0] if subset.length == 0
puts subset.length
puts subset.join(" ")

def sieve
  low = 0
  high = 2000000
  arr = []
  for i in 0..high do
    arr[i] = true
  end
  for i in 2..Math.sqrt(high).ceil do
    if arr[i]
      j = i*i
      while j < high
        arr[j] = false
        j += i
      end
    end
  end
  return arr
end

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

# count 1's
cnt = 0
arr.each do |val|
  cnt += 1 if val == 1
  break if cnt > 1
end

subset = []
if cnt > 1
  arr.each do |val|
    subset.push(val) if val == 1
  end
  arr.each do |val|
    if(primes[val + 1] and val != 1)
      subset.push(val)
      break
    end
  end
else
  done = false
  for i in 0..n - 1
    for j in i..n - 1
      if(primes[arr[i] + arr[j]])
        subset.push(arr[i])
        subset.push(arr[j])
        done = true
        break
      end
    end
    break if done
  end
end
subset[0] = arr[0] if subset.length == 0
puts subset.length
puts subset.join(" ")

def sieve
  low = 0
  high = 2000000
  arr = []
  for i in 0..high do
    arr[i] = true
  end
  for i in 2..Math.sqrt(high).ceil do
    if arr[i]
      j = i*i
      while j < high
        arr[j] = false
        j += i
      end
    end
  end
  return arr
end

primes = sieve

n = gets.chomp.to_i
arr = gets.split.map(&:to_i)
max_subset = []
for c in 0...n do
  subset = [arr[c]]
  for i in c + 1...n do
    add = true
    subset.each do |val|
      if !primes[val + arr[i]]
        add = false
        break
      end
    end
    subset.push(arr[i]) if add
  end
  max_subset = subset if subset.length > max_subset.length
  break if max_subset.length >= n - (c + 1)
end
puts max_subset.length
puts max_subset.join(" ")

def sieve
  low = 0
  high = 2000001
  arr = []
  for i in 0..high do
    arr[i] = true
  end
  for i in 2..Math.sqrt(high).ceil do
    if arr[i]
      j = i*i
      while j < high
        arr[j] = false
        j += i
      end
    end
  end
  return arr
end

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

# count 1's
cnt = 0
arr.each do |val|
  cnt += 1 if val == 1
  break if cnt > 1
end

subset = []
if cnt > 1
  arr.each do |val|
    subset.push(val) if val == 1
  end
  arr.each do |val|
    if(primes[val + 1] and val != 1)
      subset.push(val)
      break
    end
  end
else
  done = false
  for i in 0..n - 1
    for j in i..n - 1
      if(primes[arr[i] + arr[j]])
        subset.push(arr[i])
        subset.push(arr[j])
        done = true
        break
      end
    end
    break if done
  end
end
subset[0] = arr[0] if subset.length == 0
puts subset.length
puts subset.join(" ")


Nikolay has only recently started in competitive programming, but already qualified to the finals of one prestigious olympiad. There going to be $$$n$$$ participants, one of whom is Nikolay. Like any good olympiad, it consists of two rounds. Tired of the traditional rules, in which the participant who solved the largest number of problems wins, the organizers came up with different rules.Suppose in the first round participant A took $$$x$$$-th place and in the second round — $$$y$$$-th place. Then the total score of the participant A is sum $$$x + y$$$. The overall place of the participant A is the number of participants (including A) having their total score less than or equal to the total score of A. Note, that some participants may end up having a common overall place. It is also important to note, that in both the first and the second round there were no two participants tying at a common place. In other words, for every $$$i$$$ from $$$1$$$ to $$$n$$$ exactly one participant took $$$i$$$-th place in first round and exactly one participant took $$$i$$$-th place in second round.Right after the end of the Olympiad, Nikolay was informed that he got $$$x$$$-th place in first round and $$$y$$$-th place in the second round. Nikolay doesn't know the results of other participants, yet he wonders what is the minimum and maximum place he can take, if we consider the most favorable and unfavorable outcome for him. Please help Nikolay to find the answer to this question.

The first line contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases to solve. Each of the following $$$t$$$ lines contains integers $$$n$$$, $$$x$$$, $$$y$$$ ($$$1 \leq n \leq 10^9$$$, $$$1 \le x, y \le n$$$) — the number of participants in the olympiad, the place that Nikolay took in the first round and the place that Nikolay took in the second round.

Print two integers — the minimum and maximum possible overall place Nikolay could take.

NoteExplanation for the first example:Suppose there were 5 participants A-E. Let's denote Nikolay as A. The the most favorable results for Nikolay could look as follows:  However, the results of the Olympiad could also look like this:  In the first case Nikolay would have taken first place, and in the second — third place.

T = gets.to_i

def estimate_top(n, x, y)
  rank = 1

  above_a = x - 1
  below_a = [0, n - (x + 1)].max
  above_b = y - 1
  below_b = [0, n - (y + 1)].max

  d1 = [0, [above_a, below_b].min].max
  d2 = [0, [above_b, below_a].min].max

  above_a -= d1
  above_b -= d2

  rank += [0, [above_a, above_b].max].max

  rank
end

def estimate_bottom(n, x, y)
  rank = 1

  above_a = x - 1
  below_a = n - x
  above_b = y - 1
  below_b = n - y

  d1 = [0, [above_a, below_b].min].max
  d2 = [0, [above_b, below_a].min].max

  above_a -= d1
  above_b -= d2

  rank += d1 + d2 + [0, [above_a, above_b].max].max

  rank
end

T.times do
  n, x, y = gets.split.map(&:to_i)
  x, y = [x, y].sort

  top = estimate_top(n, x, y)
  bottom = estimate_bottom(n, x, y)

  puts "#{top} #{bottom}"
end

def best(n, x, y)
  case x+y
  when (2..n) then 1
  when 2*n then n
  else x+y - n + 1
  end
end

def worst(n, x, y)
  x+y > n+1 ? n : x+y -1
end

1.upto(gets.chomp.to_i) do
  n,x,y = gets.chomp.split.map(&:to_i)

  puts [best(n,x,y), worst(n,x,y)].join(' ')
end

# frozen_string_literal: true

@data = $stdin.read.split

def next_token
  @data.shift
end

def next_int
  next_token.to_i
end

next_int.times do
  n = next_int
  x = next_int
  y = next_int

  maxp = [x + y - 1, n].min
  b = x + y + 1 - n
  # b -= 1 if y == n
  minp = [[b, n].min, 1].max
  # a = x + y - n + 1
  # b = x + y
  # a = [a, 1].max
  # b = [b, n].min
  # minp = b - a
  # minp += 1 if b < n
  # minp = n - minp
  puts "#{minp} #{maxp}"
end

#! /usr/bin/env ruby

=begin
1 2 3 4 5 和
1 2 3 4 5 两两配对
希望对子的和大于我的对子的和

注意到 2+5  3+4  .. 5+2 都是7

可以贪心。枚举第一场比赛成绩，可得 要这个人输给我，第二场得多差

n太大，得优化一下
avail_y = [1, ... my_y-1], [my_y+1, ... n]
阶段1：x + y_too_small  ...
阶段2：x + y
=end

def solve_best(n, my_x, my_y)
  goal = my_x + my_y + 1
  nlose = 0

  x = goal - n; y = n
  if x < 1 then x = 1 end
  loop do
	  break if x > n || y < 1
	  if my_y == y
	    y -= 1
	    if x+y < goal then x += 1 end
	  end
	  if my_x == x then x += 1 end
	  break if x > n || y < 1

	  len = n
	  stop_at = (y > my_y ? my_y : 0)
	  if len > y - stop_at then len = y - stop_at end
	  stop_at = (x < my_x ? my_x : n+1)
	  if len > stop_at - x then len = stop_at - x end
	  nlose += len
	  x += len
	  y -= len
  end

  print (n - nlose), ' '
end

def solve_worst(n, my_x, my_y)
  goal = my_x + my_y
  nwin = 1 # myself

  x = 1; y = goal - 1
  if y > n then y = n end
  loop do
    break if x > n || y < 1
    if my_x == x
      x += 1
      if x+y > goal then y -= 1 end
    end
    if my_y == y then y -= 1 end
    break if x > n || y < 1

    len = n
    stop_at = (y > my_y ? my_y : 0)
    if len > y - stop_at then len = y - stop_at end
    stop_at = (x < my_x ? my_x : n+1)
    if len > stop_at - x then len = stop_at - x end
    nwin += len
    x += len
    y -= len
  end

  puts nwin
end

def solve
  n, myx, myy = gets.split.map{|x| x.to_i}
  solve_best(n, myx, myy)
  solve_worst(n, myx, myy)
end

t = gets.to_i
t.times { solve }

# frozen_string_literal: true

@data = $stdin.read.split

def next_token
  @data.shift
end

def next_int
  next_token.to_i
end

next_int.times do
  n = next_int
  x = next_int
  y = next_int

  a = x + y - n
  b = x + y - 1
  a = [a, 1].max
  b = [b, n].min
  maxp = (b - a + 1)
  b = [[x + y + 1 - n, n].min, 1].max
  minp = b
  # a = x + y - n + 1
  # b = x + y
  # a = [a, 1].max
  # b = [b, n].min
  # minp = b - a
  # minp += 1 if b < n
  # minp = n - minp
  puts "#{minp} #{maxp}"
end

# frozen_string_literal: true

@data = $stdin.read.split

def next_token
  @data.shift
end

def next_int
  next_token.to_i
end

next_int.times do
  n = next_int
  x = next_int
  y = next_int

  a = x + y - n
  b = x + y - 1
  a = [a, 1].max
  b = [b, n].min
  maxp = (b - a + 1)
  b = x + y + 1 - n
  b -= 1 if y == n
  minp = [[b, n].min, 1].max
  # a = x + y - n + 1
  # b = x + y
  # a = [a, 1].max
  # b = [b, n].min
  # minp = b - a
  # minp += 1 if b < n
  # minp = n - minp
  puts "#{minp} #{maxp}"
end

# frozen_string_literal: true

@data = $stdin.read.split

def next_token
  @data.shift
end

def next_int
  next_token.to_i
end

next_int.times do
  n = next_int
  x = next_int
  y = next_int

  maxp = [x + y - 1, n].min
  b = x + y + 1 - n
  b -= 1 if y == n
  minp = [[b, n].min, 1].max
  # a = x + y - n + 1
  # b = x + y
  # a = [a, 1].max
  # b = [b, n].min
  # minp = b - a
  # minp += 1 if b < n
  # minp = n - minp
  puts "#{minp} #{maxp}"
end

# frozen_string_literal: true

@data = $stdin.read.split

def next_token
  @data.shift
end

def next_int
  next_token.to_i
end

next_int.times do
  n = next_int
  x = next_int
  y = next_int

  a = x + y - n
  b = x + y - 1
  a = [a, 1].max
  b = [b, n].min
  maxp = (b - a + 1)
  a = x + y - n + 1
  b = x + y
  a = [a, 1].max
  b = [b, n].min
  minp = b - a
  minp += 1 if b < n
  puts "#{n - minp} #{maxp}"
end

T = gets.to_i

def estimate_top(n, x, y)
  rank = 1

  above_a = x - 1
  d = n - (y + 1)

  if d < above_a
    rank += above_a - d
  end

  rank
end

def estimate_bottom(n, x, y)
  rank = 1

  above_a = x - 1
  above_b = y - 1

  rank += above_a + above_b

  rank
end

T.times do
  n, x, y = gets.split.map(&:to_i)
  x, y = [x, y].sort

  top = estimate_top(n, x, y)
  bottom = estimate_bottom(n, x, y)

  puts "#{top} #{bottom}"
end

T = gets.to_i

def estimate_top(n, x, y)
  rank = 1

  above_a = x - 1
  d = [0, n - (y + 1)].max

  if d < above_a
    rank += above_a - d
  end

  rank
end

def estimate_bottom(n, x, y)
  rank = 1

  above_a = x - 1
  below_a = n - x
  above_b = y - 1
  below_b = n - y

  d1 = [0, [above_a, below_b].min].max
  d2 = [0, [above_b, below_a].min].max

  above_a -= d1
  above_b -= d2

  rank += d1 + d2 + [above_a, above_b].min

  rank
end

T.times do
  n, x, y = gets.split.map(&:to_i)
  x, y = [x, y].sort

  top = estimate_top(n, x, y)
  bottom = estimate_bottom(n, x, y)

  puts "#{top} #{bottom}"
end

T = gets.to_i

def estimate_top(n, x, y)
  rank = 1

  above_a = x - 1
  d = n - (y + 1)

  if d < above_a
    rank += above_a - d
  end

  rank
end

def estimate_bottom(n, x, y)
  rank = 1

  above_a = x - 1
  below_a = n - x
  above_b = y - 1
  below_b = n - y

  d1 = [0, [above_a, below_b].min].max
  d2 = [0, [above_b, below_a].min].max

  rank += d1 + d2

  rank
end

T.times do
  n, x, y = gets.split.map(&:to_i)
  x, y = [x, y].sort

  top = estimate_top(n, x, y)
  bottom = estimate_bottom(n, x, y)

  puts "#{top} #{bottom}"
end

T = gets.to_i

def estimate_top(n, x, y)
  rank = 1

  above_a = x - 1
  below_a = [0, n - (x + 1)].max
  above_b = y - 1
  below_b = [0, n - (y + 1)].max

  d1 = [0, [above_a, below_b].min].max
  d2 = [0, [above_b, below_a].min].max

  above_a -= d1
  above_b -= d2

  rank += [above_a, above_b].min

  rank
end

def estimate_bottom(n, x, y)
  rank = 1

  above_a = x - 1
  below_a = n - x
  above_b = y - 1
  below_b = n - y

  d1 = [0, [above_a, below_b].min].max
  d2 = [0, [above_b, below_a].min].max

  above_a -= d1
  above_b -= d2

  rank += d1 + d2 + [above_a, above_b].min

  rank
end

T.times do
  n, x, y = gets.split.map(&:to_i)
  x, y = [x, y].sort

  top = estimate_top(n, x, y)
  bottom = estimate_bottom(n, x, y)

  puts "#{top} #{bottom}"
end


Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A = [a1, a2, ..., an] and B = [b1, b2, ..., bn] of length n each which he uses to ask people some quite peculiar questions.To test you on how good are you at spotting inequality in life, he wants you to find an "unfair" subset of the original sequence. To be more precise, he wants you to select k numbers P = [p1, p2, ..., pk] such that 1 ≤ pi ≤ n for 1 ≤ i ≤ k and elements in P are distinct. Sequence P will represent indices of elements that you'll select from both sequences. He calls such a subset P "unfair" if and only if the following conditions are satisfied: 2·(ap1 + ... + apk) is greater than the sum of all elements from sequence A, and 2·(bp1 + ... + bpk) is greater than the sum of all elements from the sequence B. Also, k should be smaller or equal to  because it will be to easy to find sequence P if he allowed you to select too many elements!Mike guarantees you that a solution will always exist given the conditions described above, so please help him satisfy his curiosity!

The first line contains integer n (1 ≤ n ≤ 105) — the number of elements in the sequences.  On the second line there are n space-separated integers a1, ..., an (1 ≤ ai ≤ 109) — elements of sequence A. On the third line there are also n space-separated integers b1, ..., bn (1 ≤ bi ≤ 109) — elements of sequence B.

On the first line output an integer k which represents the size of the found subset. k should be less or equal to . On the next line print k integers p1, p2, ..., pk (1 ≤ pi ≤ n) — the elements of sequence P. You can print the numbers in any order you want. Elements in sequence P should be distinct.

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

c = (0...n).sort { |i, j| a[j] <=> a[i] }

ans = [c[0]]
for i in (1...n).step(2)
    if i == n-1 or b[c[i]] > b[c[i+1]]
        ans.push(c[i])
    else
        ans.push(c[i+1])
    end
end
puts ans.length
puts ans.map { |i| i+1 }.map(&:to_s).join(' ')


Let's call a string "s-palindrome" if it is symmetric about the middle of the string. For example, the string "oHo" is "s-palindrome", but the string "aa" is not. The string "aa" is not "s-palindrome", because the second half of it is not a mirror reflection of the first half.  English alphabet You are given a string s. Check if the string is "s-palindrome".

The only line contains the string s (1 ≤ |s| ≤ 1000) which consists of only English letters.

Print "TAK" if the string s is "s-palindrome" and "NIE" otherwise.

s=gets.chomp;puts s==s.tr('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ','#d#b##########oqp####vwx##A######HI###M#O####TUVWXY#').reverse ? :TAK : :NIE

s = gets.strip
mirrored = ["A", "b", "d", "H", "I", "M", "O", "o", "p", "q", "T", "U", "V", "v", "W", "w", "X", "x", "Y"]
mirror_of = ["A", "d", "b", "H", "I", "M", "O", "o", "q", "p", "T", "U", "V", "v", "W", "w", "X", "x", "Y"]
i = 0
j = s.length - 1
palindrom = true
while i <= j
    l = mirrored.index s[i]
    r = mirror_of.index( s[j] )
    palindrom = false if l.nil? || r.nil? || l != r
    i += 1
    j -= 1
end
if palindrom
    puts "TAK"
else
    puts "NIE"
end

s = gets.chomp
puts s == s.tr('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ','!d!b!!!!!!!!!!oqp!!!!vwx!!A!!!!!!HI!!!M!O!!!!TUVWXY!').reverse ? "TAK" :"NIE"

s = gets.strip
puts s == s.tr('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ','#d#b##########oqp####vwx##A######HI###M#O####TUVWXY#').reverse ? "TAK" :"NIE"

word  = gets.to_s
char_array = word.chars.to_a
len = char_array.length-1
array_div = len/2
array_mod = len%2
char_valid = "AbdHIMOopqTUVvWwXxY".chars.to_a
#puts len
#puts array_div
#puts array_mod
ctr = 0
if (1 <= len) && (len <= 1000)
	if array_mod == 0
			for i in 0..array_div-1
				if char_valid.include?(char_array[i])
					if ((char_array[i] == 'q' && char_array[len-1-i] == 'p') || (char_array[i] == 'p' && char_array[len-1-i] == 'q'))
          ctr += 1
            #puts 'a'
            #puts  ctr
          elsif ((char_array[i] == 'd' && char_array[len-1-i] == 'b') || (char_array[i] == 'b' && char_array[len-1-i] == 'd'))
            ctr += 1
            #puts 'b'
            #puts ctr
          elsif (char_array[i] == char_array[len-1-i] && (char_array[i] != 'b' && char_array[i] != 'd' && char_array[i] != 'p' && char_array[i] != 'q'))
						ctr += 1
           # puts 'c'
            #puts ctr

					end
				end
      end
else
			for i in 0..array_div
				if char_valid.include?(char_array[i])
					if ((char_array[i] == 'q' && char_array[len-1-i] == 'p') || (char_array[i] == 'p' && char_array[len-1-i] == 'q'))
						ctr += 1
					#	puts 'a'
					#	puts ctr
          elsif ((char_array[i] == 'd' && char_array[len-1-i] == 'b') || (char_array[i] == 'b' && char_array[len-1-i] == 'd'))
						ctr += 1
					#	puts 'b'
					#	puts ctr
          elsif (char_array[i] == char_array[len-1-i] && i != len-1-i && (char_array[i] != 'b' && char_array[i] != 'd' && char_array[i] != 'p' && char_array[i] != 'q'))
						ctr += 1
					#	puts 'c'
					#	puts ctr
          elsif(i == len-1-i && (char_array[i] != 'b' && char_array[i] != 'd' && char_array[i] != 'p' && char_array[i] != 'q'))
						ctr += 1
					#	puts 'd'
					#	puts ctr
					end
				end
      end
  end
#puts ctr
  if array_mod == 0
    if ctr == array_div
			puts 'TAK'
    else
      puts 'NIE'
    end
  else
    if ctr == array_div+1
			puts 'TAK'
    else
      puts 'NIE'
    end
  end

end

s = gets.strip
mirrored = ["A", "b", "d", "H", "I", "i", "l", "M", "m", "n", "O", "o", "p", "T", "U", "u", "V", "v", "W", "w", "X", "x", "Y"]
mirror_of = ["A", "d", "b", "H", "I", "i", "l", "M", "m", "n", "O", "o", "q", "T", "U", "u", "V", "v", "W", "w", "X", "x", "Y"]
i = 0
j = s.length - 1
palindrom = true
while i <= j
    l = mirrored.index s[i]
    r = mirror_of.index( s[j] )
    palindrom = false if l.nil? || r.nil? || l != r
    i += 1
    j -= 1
end
if palindrom
    puts "TAK"
else
    puts "NIE"
end

s = gets.strip
mirrored = ["A", "b", "d", "H", "I", "M", "O", "o", "p", "T", "U", "V", "v", "W", "w", "X", "x", "Y"]
mirror_of = ["A", "d", "b", "H", "I", "M", "O", "o", "q", "T", "U", "V", "v", "W", "w", "X", "x", "Y"]
i = 0
j = s.length - 1
palindrom = true
while i < j
    l = mirrored.index s[i]
    r = mirror_of.index( s[j] )
    palindrom = false if l.nil? || r.nil? || l != r
    i += 1
    j -= 1
end
if palindrom
    puts "TAK"
else
    puts "NIE"
end

s = gets.strip
mirrored = ["A", "b", "d", "H", "I", "i", "M", "m", "n", "O", "o", "p", "T", "U", "u", "V", "v", "W", "w", "X", "x", "Y"]
mirror_of = ["A", "d", "b", "H", "I", "i", "M", "m", "n", "O", "o", "q", "T", "U", "u", "V", "v", "W", "w", "X", "x", "Y"]
i = 0
j = s.length - 1
palindrom = true
while i <= j
    l = mirrored.index s[i]
    r = mirror_of.index( s[j] )
    palindrom = false if l.nil? || r.nil? || l != r
    i += 1
    j -= 1
end
if palindrom
    puts "TAK"
else
    puts "NIE"
end

s = gets.strip
mirrored = ["A", "b", "d", "H", "I", "M", "O", "o", "p", "T", "U", "V", "v", "W", "w", "X", "x", "Y"]
mirror_of = ["A", "d", "b", "H", "I", "M", "O", "o", "q", "T", "U", "V", "v", "W", "w", "X", "x", "Y"]
i = 0
j = s.length - 1
palindrom = true
while i <= j
    l = mirrored.index s[i]
    r = mirror_of.index( s[j] )
    palindrom = false if l.nil? || r.nil? || l != r
    i += 1
    j -= 1
end
if palindrom
    puts "TAK"
else
    puts "NIE"
end

word  = gets.to_s
char_array = word.chars.to_a
len = char_array.length-1
array_div = len/2
array_mod = len%2
char_valid = "AbdHIMOopqTUVvWwXxYy".chars.to_a
#puts len
#puts array_div
#puts array_mod
ctr = 0
if (1 <= len) && (len <= 1000)
	if array_mod == 0
			for i in 0..array_div-1
				if char_valid.include?(char_array[i])
					if ((char_array[i] == 'q' && char_array[len-1-i] == 'p') || (char_array[i] == 'p' && char_array[len-1-i] == 'q'))
          ctr += 1
            #puts 'a'
            #puts  ctr
          elsif ((char_array[i] == 'd' && char_array[len-1-i] == 'b') || (char_array[i] == 'b' && char_array[len-1-i] == 'd'))
            ctr += 1
            #puts 'b'
            #puts ctr
          elsif (char_array[i] == char_array[len-1-i] && (char_array[i] != 'b' && char_array[i] != 'd' && char_array[i] != 'p' && char_array[i] != 'q'))
						ctr += 1
           # puts 'c'
            #puts ctr

					end
				end
      end
else
			for i in 0..array_div
				if char_valid.include?(char_array[i])
					if ((char_array[i] == 'q' && char_array[len-1-i] == 'p') || (char_array[i] == 'p' && char_array[len-1-i] == 'q'))
						ctr += 1
					#	puts 'a'
					#	puts ctr
          elsif ((char_array[i] == 'd' && char_array[len-1-i] == 'b') || (char_array[i] == 'b' && char_array[len-1-i] == 'd'))
						ctr += 1
					#	puts 'b'
					#	puts ctr
          elsif (char_array[i] == char_array[len-1-i] && i != len-1-i && (char_array[i] != 'b' && char_array[i] != 'd' && char_array[i] != 'p' && char_array[i] != 'q'))
						ctr += 1
					#	puts 'c'
					#	puts ctr
          elsif(i == len-1-i && (char_array[i] != 'b' && char_array[i] != 'd' && char_array[i] != 'p' && char_array[i] != 'q'))
						ctr += 1
					#	puts 'd'
					#	puts ctr
					end
				end
      end
  end
#puts ctr
  if array_mod == 0
    if ctr == array_div
			puts 'TAK'
    else
      puts 'NIE'
    end
  else
    if ctr == array_div+1
			puts 'TAK'
    else
      puts 'NIE'
    end
  end

end

word  = gets.to_s
char_array = word.chars.to_a
len = char_array.length-1
array_div = len/2
array_mod = len%2
char_valid = "AbdHIMOopqTUVvWwXxYy".chars.to_a
#puts len
#puts array_div
#puts array_mod
ctr = 0
if (1 <= len) && (len <= 1000)
	if array_mod == 0
			for i in 0..array_div-1
				if char_valid.include?(char_array[i])
					if ((char_array[i] == 'q' && char_array[len-1-i] == 'p') || (char_array[i] == 'p' && char_array[len-1-i] == 'q'))
          ctr += 1
            #puts 'a'
            #puts  ctr
          elsif ((char_array[i] == 'd' && char_array[len-1-i] == 'b') || (char_array[i] == 'b' && char_array[len-1-i] == 'd'))
            ctr += 1
            #puts 'b'
            #puts ctr
          elsif (char_array[i] == char_array[len-1-i] && (char_array[i] != 'b' && char_array[i] != 'd' && char_array[i] != 'p' && char_array[i] != 'q'))
						ctr += 1
           # puts 'c'
            #puts ctr

					end
				end
      end
else
			for i in 0..array_div
				if char_valid.include?(char_array[i])
					if ((char_array[i] == 'q' && char_array[len-1-i] == 'p') || (char_array[i] == 'p' && char_array[len-1-i] == 'q'))
						ctr += 1
					#	puts 'a'
					#	puts ctr
          elsif ((char_array[i] == 'd' && char_array[len-1-i] == 'b') || (char_array[i] == 'b' && char_array[len-1-i] == 'd'))
						ctr += 1
					#	puts 'b'
					#	puts ctr
          elsif (char_array[i] == char_array[len-1-i] && i != len-1-i && (char_array[i] != 'b' && char_array[i] != 'd' && char_array[i] != 'p' && char_array[i] != 'q'))
						ctr += 1
					#	puts 'c'
					#	puts ctr
          elsif(i == len-1-i && (char_array[i] != 'b' && char_array[i] != 'd' && char_array[i] != 'p' && char_array[i] != 'q'))
						ctr += 1
					#	puts 'd'
					#	puts ctr
					end
				end
      end
  end
#puts ctr
  if array_mod == 0
    if ctr == array_div
			puts 'TAK'
    else
      puts 'NEI'
    end
  else
    if ctr == array_div+1
			puts 'TAK'
    else
      puts 'NEI'
    end
  end

end


Alyona has recently bought a miniature fridge that can be represented as a matrix with $$$h$$$ rows and $$$2$$$ columns. Initially there is only one shelf at the bottom of the fridge, but Alyona can install arbitrary number of shelves inside the fridge between any two rows. A shelf is two cells wide, does not occupy any space but separates the inside of the fridge to the lower and upper part.  An example of a fridge with $$$h = 7$$$ and two shelves. The shelves are shown in black. The picture corresponds to the first example. Alyona has $$$n$$$ bottles of milk that she wants to put in the fridge. The $$$i$$$-th bottle is $$$a_i$$$ cells tall and $$$1$$$ cell wide. She can put a bottle on some shelf if the corresponding space above the shelf is at least as tall as the bottle. She can not put a bottle on top of another bottle (if there is no shelf between them). Two bottles can not share a cell.Alyona is interested in the largest integer $$$k$$$ such that she can put bottles $$$1$$$, $$$2$$$, ..., $$$k$$$ in the fridge at the same time. Find this largest $$$k$$$.

The first line contains two integers $$$n$$$ and $$$h$$$ ($$$1 \le n \le 10^3$$$, $$$1 \le h \le 10^9$$$) — the number of bottles and the height of the fridge. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \le a_i \le h$$$) — the heights of the bottles.

Print the single integer $$$k$$$ — the maximum integer such that Alyona can put the bottles $$$1$$$, $$$2$$$, ..., $$$k$$$ in the fridge at the same time. If Alyona can put all bottles in the fridge, print $$$n$$$. It is easy to see that Alyona can always put at least one bottle in the fridge.

NoteOne of optimal locations in the first example is shown on the picture in the statement.One of optimal locations in the second example is shown on the picture below.  One of optimal locations in the third example is shown on the picture below.

f="gets.split.map &:to_i"
n,k=eval f
a=eval f
n-=1 while k<(a[0...n].sort.values_at(*(-n|1...0).step(2)).reduce &:+)
p n

n,k = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
t = a.sort.reverse
n.times do |i|
  len = 0
  flg = true
  t.each do |j|
    len += j if flg
    flg = !flg
  end
  if len > k
    t.delete_at(t.find_index(a[-i-1]))
  else
    p n-i
    break
  end
end

N, H = gets.split.map(&:to_i)
A = gets.split.map(&:to_i)

def f(k)
  list = A.take(k).sort
  height = 0

  until list.empty?
    a, b = list.pop(2)
    h = [a, b || 0].max

    height += h
  end

  height <= H
end

puts [*1..N].reverse.bsearch { |k| f(k) }

n,h = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
min = 0
max = n+1
while max-min > 1
    mid = (min+max)/2
    b = a[0,mid].sort.reverse
    x = 0
    for i in 0..(mid-1)/2
        x += b[i*2]
    end
    if x > h
        max = mid
    else
        min = mid
    end
end
puts min