Try guessing the statement from this picture:   You are given a non-negative integer $$$d$$$. You have to find two non-negative real numbers $$$a$$$ and $$$b$$$ such that $$$a + b = d$$$ and $$$a \cdot b = d$$$.

The first line contains $$$t$$$ ($$$1 \le t \le 10^3$$$) — the number of test cases. Each test case contains one integer $$$d$$$ $$$(0 \le d \le 10^3)$$$.

For each test print one line. If there is an answer for the $$$i$$$-th test, print "Y", and then the numbers $$$a$$$ and $$$b$$$. If there is no answer for the $$$i$$$-th test, print "N". Your answer will be considered correct if $$$|(a + b) - a \cdot b| \le 10^{-6}$$$ and $$$|(a + b) - d| \le 10^{-6}$$$.

=begin
n = gets.chomp.to_i

arr = gets.split(" ")
arr = arr.map { |element|  element.to_i }
sum = arr.inject(0){|sum,x| sum + x }

=end

n = gets.chomp.to_i
arr = []

n.times do |x|
	arr<<gets.chomp.to_i
end

arr.each do |element|
	d = element
	x = d*d - 4*d
	if x >= 0
		b  = (d + Math.sqrt(x))/2
		a = d/b
		a = 0.0 if b == 0
		puts "Y "+a.to_s+" "+b.to_s
	else
		puts "N"
	end
end

for i in gets.chomp.to_i.times do
        d = gets.chomp.to_i
        delta = d*d - 4*d
        if delta < 0
                puts 'N'
        else
                puts "Y " + ((d + Math.sqrt(delta))/2.0).to_s + " " + ((d - Math.sqrt(delta))/2.0).to_s
end
end

=begin
n = gets.chomp.to_i

arr = gets.split(" ")
arr = arr.map { |element|  element.to_i }
sum = arr.inject(0){|sum,x| sum + x }

=end

n = gets.chomp.to_i
arr = []

n.times do |x|
	arr<<gets.chomp.to_i
end

arr.each do |element|
	d = element
	x = d*d - 4*d
	if x >= 0
		b  = (d + Math.sqrt(x))/2
		a = d/b
		a = 0.0 if b == 0
		p "Y "+a.to_s+" "+b.to_s
	else
		p "N"
	end
end

=begin
n = gets.chomp.to_i

arr = gets.split(" ")
arr = arr.map { |element|  element.to_i }
sum = arr.inject(0){|sum,x| sum + x }

=end

n = gets.chomp.to_i
arr = []

n.times do |x|
	arr<<gets.chomp.to_i
end

arr.each do |element|
	d = element
	x = d*d - 4*d
	if x >= 0
		b  = (d + Math.sqrt(x))/2
		a = d/b
		a = 0.0 if b == 0
		puts "Y "+a.to_s+" "+b.to_s
	else
		p "N"
	end
end


One day Squidward, Spongebob and Patrick decided to go to the beach. Unfortunately, the weather was bad, so the friends were unable to ride waves. However, they decided to spent their time building sand castles.At the end of the day there were n castles built by friends. Castles are numbered from 1 to n, and the height of the i-th castle is equal to hi. When friends were about to leave, Squidward noticed, that castles are not ordered by their height, and this looks ugly. Now friends are going to reorder the castles in a way to obtain that condition hi ≤ hi + 1 holds for all i from 1 to n - 1.Squidward suggested the following process of sorting castles:   Castles are split into blocks — groups of consecutive castles. Therefore the block from i to j will include castles i, i + 1, ..., j. A block may consist of a single castle.  The partitioning is chosen in such a way that every castle is a part of exactly one block.  Each block is sorted independently from other blocks, that is the sequence hi, hi + 1, ..., hj becomes sorted.  The partitioning should satisfy the condition that after each block is sorted, the sequence hi becomes sorted too. This may always be achieved by saying that the whole sequence is a single block. Even Patrick understands that increasing the number of blocks in partitioning will ease the sorting process. Now friends ask you to count the maximum possible number of blocks in a partitioning that satisfies all the above requirements.

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of castles Spongebob, Patrick and Squidward made from sand during the day. The next line contains n integers hi (1 ≤ hi ≤ 109). The i-th of these integers corresponds to the height of the i-th castle.

Print the maximum possible number of blocks in a valid partitioning.

NoteIn the first sample the partitioning looks like that: [1][2][3].  In the second sample the partitioning is: [2, 1][3, 2]

n = gets.chomp.to_i
hs = gets.chomp.split.map(&:to_i)
lmaxs = Array.new(n)
rmins = Array.new(n)

lmaxs[0] = hs[0]
rmins[-1] = hs[-1]
for i in 1...n
  ri = n - i - 1
  lmaxs[i] = [lmaxs[i - 1], hs[i]].max
  rmins[ri] = [rmins[ri + 1], hs[ri]].min
end

cnt = 1
for i in 0...n - 1
  f = true
  f &&= lmaxs[i] <= rmins[i + 1]
  cnt += 1 if f
end
puts cnt

n = gets.chomp.to_i
h = gets.split(" ").map{|i| i.to_i}
h = [0]+h
hmax = [0]*(n+1)
hmin = [0]*(n+1)
hmax[1] = h[1]
hmin[n] = h[n]
for i in 2..n
	hmax[i] = [hmax[i-1], h[i]].max
end
for i in (1..(n-1)).to_a.reverse
	hmin[i] = [hmin[i+1], h[i]].min
end
s = 1
for i in 1...n
	if hmax[i] <= hmin[i+1]
		s +=1
	end
end
puts s

class Solver
	def main
		n = gets.to_i
		h = gets.split.map { |x| x.to_i }
		pre_max = Array.new(n + 2, 0)
		suf_min = Array.new(n + 2, 1e9.to_i)
		1.upto(n) do |i|
			pre_max[i] = [pre_max[i - 1], h[i - 1]].max
		end
		n.downto(1) do |i|
			suf_min[i] = [suf_min[i + 1], h[i - 1]].min
		end
		gap = (1...n).count { |i| pre_max[i] <= suf_min[i + 1] }
		puts gap + 1
	end
end

Solver.new.main

n = gets.chomp.to_i
hs = gets.chomp.split.map(&:to_i)
lmins = Array.new(n)
lmaxs = Array.new(n)
rmins = Array.new(n)
rmaxs = Array.new(n)

lmins[0] = hs[0]
lmaxs[0] = hs[0]
rmins[-1] = hs[-1]
rmaxs[-1] = hs[-1]
for i in 1...n
  ri = n - i - 1
  lmins[i] = [lmins[i - 1], hs[i]].min
  lmaxs[i] = [lmaxs[i - 1], hs[i]].max
  rmins[ri] = [rmins[ri + 1], hs[ri]].min
  rmaxs[ri] = [rmaxs[ri + 1], hs[ri]].max
end

cnt = 1
for i in 0...n - 1
  if lmins[i] <= rmins[i + 1] && rmaxs[i] <= rmaxs[i + 1]
     cnt += 1
  end
end
puts cnt

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

is = Hash.new(-1)
hs.each.with_index do |h, i|
  is[h] = [is[h], i].max
end

cnt = 0
idx = -1
hs.sort.uniq.each do |h|
  if idx < is[h]
    cnt += 1
    idx = is[h]
  end
end

puts cnt


You have two binary strings $$$a$$$ and $$$b$$$ of length $$$n$$$. You would like to make all the elements of both strings equal to $$$0$$$. Unfortunately, you can modify the contents of these strings using only the following operation:  You choose two indices $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le n$$$);  For every $$$i$$$ that respects $$$l \le i \le r$$$, change $$$a_i$$$ to the opposite. That is, $$$a_i := 1 - a_i$$$;  For every $$$i$$$ that respects either $$$1 \le i < l$$$ or $$$r < i \le n$$$, change $$$b_i$$$ to the opposite. That is, $$$b_i := 1 - b_i$$$. Your task is to determine if this is possible, and if it is, to find such an appropriate chain of operations. The number of operations should not exceed $$$n + 5$$$. It can be proven that if such chain of operations exists, one exists with at most $$$n + 5$$$ operations.

Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of the strings. The second line of each test case contains a binary string $$$a$$$, consisting only of characters 0 and 1, of length $$$n$$$. The third line of each test case contains a binary string $$$b$$$, consisting only of characters 0 and 1, of length $$$n$$$. It is guaranteed that sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.

For each testcase, print first "YES" if it's possible to make all the elements of both strings equal to $$$0$$$. Otherwise, print "NO". If the answer is "YES", on the next line print a single integer $$$k$$$ ($$$0 \le k \le n + 5$$$) — the number of operations. Then $$$k$$$ lines follows, each contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le n$$$) — the description of the operation. If there are several correct answers, print any of them.

YES
1
2 2
NO
NO
YES
2
1 2
2 2
YES
2
1 1
2 3

NoteIn the first test case, we can perform one operation with $$$l = 2$$$ and $$$r = 2$$$. So $$$a_2 := 1 - 1 = 0$$$ and string $$$a$$$ became equal to 000. $$$b_1 := 1 - 1 = 0$$$, $$$b_3 := 1 - 1 = 0$$$ and string $$$b$$$ became equal to 000.In the second and in the third test cases, it can be proven that it's impossible to make all elements of both strings equal to $$$0$$$.In the fourth test case, we can perform an operation with $$$l = 1$$$ and $$$r = 2$$$, then string $$$a$$$ became equal to 01, and string $$$b$$$ doesn't change. Then we perform an operation with $$$l = 2$$$ and $$$r = 2$$$, then $$$a_2 := 1 - 1 = 0$$$ and $$$b_1 = 1 - 1 = 0$$$. So both of string $$$a$$$ and $$$b$$$ became equal to 00.In the fifth test case, we can perform an operation with $$$l = 1$$$ and $$$r = 1$$$. Then string $$$a$$$ became equal to 011 and string $$$b$$$ became equal to 100. Then we can perform an operation with $$$l = 2$$$ and $$$r = 3$$$, so both of string $$$a$$$ and $$$b$$$ became equal to 000.

gets.to_i.times do
    n=gets.chomp.to_i
    arr1=gets.chomp.split('').map(&:to_i)
    arr2=gets.chomp.split('').map(&:to_i)
    valid=true
    ans=0
    op=Array.new
    onesStart=-1
    n.times do |i|
        if (arr1[0]+arr2[0])%2!=(arr1[i]+arr2[i])%2
            valid=false
            break
        end
        if arr1[i]==1
            if  onesStart==-1
            onesStart=i
            end
            if i==n-1
                ans+=1
                if  (arr1[0]+arr2[0])%2!=ans%2
                    ans+=1
                    if onesStart!=0
                        op.push([onesStart-1,onesStart-1])
                        onesStart-=1
                    else
                        op.push([0,0])
                        onesStart=1
                    end
                end
                op.push([onesStart,i])
            end
        end
        if arr1[i]== 0 && onesStart!=-1
            op.push([onesStart,i-1])
            onesStart=-1
            ans+=1
        end
    end
    if !valid
        puts "NO"
    else
        if  (arr1[0]+arr2[0])%2!=ans%2
            ans+=1
            lst=op[-1]
            if op.length==0
                ans+=2
                op.push([0,0])
                op.push([1,n-1])
                op.push([0,n-1])
            else
                op.pop
                op.push([lst[1]+1,lst[1]+1])
                op.push([lst[0],lst[1]+1])
            end
        end
        puts "YES"
        puts ans
        op.each do|a|
            puts "#{a[0]+1} #{a[1]+1}"
        end
    end

end

gets.to_i.times do
    n=gets.chomp.to_i
    arr1=gets.chomp.split('').map(&:to_i)
    arr2=gets.chomp.split('').map(&:to_i)
    valid=true
    ans=0
    op=Array.new
    onesStart=-1
    n.times do |i|
        if (arr1[0]+arr2[0])%2!=(arr1[i]+arr2[i])%2
            valid=false
            break
        end
        if arr1[i]==1
            if  onesStart==-1
            onesStart=i
            end
            if i==n-1
                ans+=1
                if  (arr1[0]+arr2[0])%2!=ans%2
                    ans+=1
                    if onesStart!=0
                        op.push([onesStart-1,onesStart-1])
                        onesStart-=1
                    else
                        op.push([0,0])
                        onesStart=1
                    end
                end
                op.push([onesStart,i])
            end
        end
        if arr1[i]== 0 && onesStart!=-1
            op.push([onesStart,i-1])
            onesStart=-1
            ans+=1
        end
    end
    if !valid
        puts "NO"
    else
        if  (arr1[0]+arr2[0])%2!=ans%2
            ans+=1
            lst=op[-1]
            if op.length==0
                op.push([0,n-1])
            else
                op.pop
                op.push([lst[1]+1,lst[1]+1])
                op.push([lst[0],lst[1]+1])
            end
        end
        puts "YES"
        puts ans
        op.each do|a|
            puts "#{a[0]+1} #{a[1]+1}"
        end
    end

end

gets.to_i.times do
    n=gets.chomp.to_i
    arr1=gets.chomp.split('').map(&:to_i)
    arr2=gets.chomp.split('').map(&:to_i)
    valid=true
    ans=0
    op=Array.new
    onesStart=-1
    n.times do |i|
        if (arr1[0]+arr2[0])%2!=(arr1[i]+arr2[i])%2
            valid=false
            break
        end
        if arr1[i]==1
            if  onesStart==-1
            onesStart=i
            end
            if i==n-1
                ans+=1
                if  (arr1[0]+arr2[0])%2!=ans%2
                    ans+=1
                    if onesStart!=0
                        op.push([onesStart-1,onesStart-1])
                    else
                        op.push([0,0])
                        onesStart=1
                    end
                end
                op.push([onesStart,i])
            end
        end
        if arr1[i]== 0 && onesStart!=-1
            op.push([onesStart,i-1])
            onesStart=-1
            ans+=1
        end
    end
    if !valid
        puts "NO"
    else
        if  (arr1[0]+arr2[0])%2!=ans%2
            ans+=1
            lst=op[-1]
            if op.length==0
                op.push([0,n-1])
            else
                op.pop
                op.push([lst[1]+1,lst[1]+1])
                op.push([lst[0],lst[1]+1])
            end
        end
        puts "YES"
        puts ans
        op.each do|a|
            puts "#{a[0]+1} #{a[1]+1}"
        end
    end

end


Find the minimum area of a square land on which you can place two identical rectangular $$$a \times b$$$ houses. The sides of the houses should be parallel to the sides of the desired square land.Formally,   You are given two identical rectangles with side lengths $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le 100$$$) — positive integers (you are given just the sizes, but not their positions).  Find the square of the minimum area that contains both given rectangles. Rectangles can be rotated (both or just one), moved, but the sides of the rectangles should be parallel to the sides of the desired square. Two rectangles can touch each other (side or corner), but cannot intersect. Rectangles can also touch the sides of the square but must be completely inside it. You can rotate the rectangles. Take a look at the examples for a better understanding.    The picture shows a square that contains red and green rectangles.

The first line contains an integer $$$t$$$ ($$$1 \le t \le 10\,000$$$) —the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case is a line containing two integers $$$a$$$, $$$b$$$ ($$$1 \le a, b \le 100$$$) — side lengths of the rectangles.

Print $$$t$$$ answers to the test cases. Each answer must be a single integer — minimal area of square land, that contains two rectangles with dimensions $$$a \times b$$$.

NoteBelow are the answers for the first two test cases:

gets.to_i.times do
	a, b = gets.split.map(&:to_i).sort
	puts [a * 2, b].max**2
end

t = gets.to_i
t.times do
	a,b = gets.split.map(&:to_i)
	puts [[a,b].max, [a,b].min*2].max ** 2
end

gets.strip.to_i.times do |t|
  a, b = gets.strip.split(' ').map(&:to_i)
  puts [[2 * a, b].max ** 2, [2 * b, a].max ** 2].min
end

t = gets.chomp.to_i
t.times do |testcase|
  (a,b) = gets.split.map(&:to_i)
  puts [[2*a , b].max , [2*b , a].max].min ** 2
end

n = gets.to_i

1.upto n do
  a, b = gets.split.map(&:to_i)
  if a == b
    puts ((a * 2) ** 2)
  else
    if [a,b].max > ([a,b].min * 2)
      puts ([a,b].max ** 2)
    else
      puts (([a,b].min * 2) ** 2)
    end
  end
end

gets.to_i.times {
    a, b=gets.split.map(&:to_i).minmax
    puts [a+a, b].max**2
}

gets.to_i.times {
  a, b = gets.split.map { |v|
    v.to_i
  }
  if a == b
    puts (a * 2) ** 2
  else
    if a > b
      a, b = b, a
    end
    a2 = a * 2
    if a2 < b
      puts b * b
    else
      puts a2 * a2
    end
  end
}

i = gets.chomp.to_i

def minimalsquare(a, b)
  if a < b
    minimal = Math.sqrt((a * 2) ** 2).ceil
    minimal2 = Math.sqrt((a * b) * 2).ceil
    if (minimal * minimal) >= (minimal2 * minimal2)
      return minimal * minimal
    else
      if minimal2 >= b
        return minimal2 * minimal2
      else
        return b ** 2
      end
    end
  else
    minimal = Math.sqrt((b * 2) ** 2).ceil
    minimal2 = Math.sqrt((a * b) * 2).ceil
    if (minimal * minimal) >= (minimal2 * minimal2)
      return minimal * minimal
    else
      if minimal2 >= a
        return minimal2 * minimal2
      else
        return a ** 2
      end
    end
  end
end

while (i > 0)
  a, b = gets.split.map(&:to_i)

  puts minimalsquare(a, b)

  i -= 1
end

t = gets.to_i
t.times do
  a, b = gets.split.map(&:to_i).sort
  if a * 2 >= b
    puts (a * 2) ** 2
  else
    puts b ** 2
  end
end

gets.to_i.times do
    r = gets.split(' ').map(&:to_i)
    puts [2 * r.min, r.max].max ** 2
end

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

  puts [2*[a, b].min, [a, b].max].max ** 2
end

t = gets.chomp.to_i
t.times do |testcase|
  (a,b) = gets.split.map(&:to_i)
  puts [2*a , 2*b].min ** 2
end

n = gets.to_i

1.upto n do
  a, b = gets.split.map(&:to_i)
  if a == 1 and b == 1
    puts 4
  elsif a == 1 or b == 1
    puts ([a,b].max) ** 2
  else
    puts ([a,b].min * 2) ** 2
  end
end

i = gets.chomp.to_i

def minimalsquare(a, b)
  if a < b
    minimal = Math.sqrt((a * 2) ** 2).ceil
    if (minimal * minimal) >= ((a * b) * 2)
      return minimal * minimal
    else
      minimal = Math.sqrt((a * b) * 2).ceil
      return minimal * minimal
    end
  else
    minimal = Math.sqrt((b * 2) ** 2).ceil
    if (minimal * minimal) >= ((a * b) * 2)
      return minimal * minimal
    else
      minimal = Math.sqrt((a * b) * 2).ceil
      return minimal * minimal
    end
  end
end

while (i > 0)
  a, b = gets.split.map(&:to_i)

  puts minimalsquare(a, b)

  i -= 1
end

gets.to_i.times do
    r = gets.to_s.split(' ').map(&:to_i)
    l = r.max
    b = r.min
    if l == b || b*2 >= l
        puts b*b*4
    else
        puts ((l/b.to_f).ceil)**2
    end
end


At many competitions that have a word «cup» in its official name the winner is presented with an actual cup. This time the organizers of one unusual programming competition have decided to please the winner even more and to add a nameplate to the cup with the handle of the winner.The nameplate is to be rectangular and the text on it will be printed as a table of several rows and columns. Having some measurements done, the organizers have found out that the number $$$a$$$ of rows cannot be greater than $$$5$$$ while the number $$$b$$$ of columns cannot exceed $$$20$$$. Every cell of the table will contain either an asterisk («*») or a letter of user's handle.Furthermore, the organizers want the rows of the table to be uniform, which means that the number of asterisks used in different rows should differ by at most one (i.e. you can't have two asterisks in the first row and none in the second). The main goal, however, is to obtain the winner's handle precisely when reading the table from top to bottom and from left to right in every row (skipping asterisks).The organizers want for the nameplate to have as few rows as possible and among all valid tables with the minimum number of rows they want to choose the one that has the minimum number of columns.The winner is not yet determined so your task is to write a program that, given a certain handle, generates the necessary table.

The only line contains one string $$$s$$$ ($$$1 \le |s| \le 100$$$), comprised of uppercase and lowercase Latin letters,  — the handle of the winner.

In the first line output the minimum number $$$a$$$ of rows in the table and the minimum number $$$b$$$ of columns in an optimal table with rows. The following $$$a$$$ lines should contain $$$b$$$ characters each  — any valid table.

3 15
MyNameIsLifeIAm
ForeverByYourSi
deMyNameIsL*ife

a = gets.strip
# a = "00000000000000012000000000000001200000000000000120000000000001"
if a.length > 20
    rows = 1
    while a.length.to_f/rows > 20 do
        rows +=1
    end
    if a.length % rows == 0
        print "#{rows} #{a.length/rows}\n"
        a.split("").each_slice(a.length/rows).to_a.each do |x|
            print "#{x.join}\n"
        end
    else
        count_of_stars = 0
        while (a.length + count_of_stars) % rows != 0 do
            count_of_stars += 1
        end
        symbols_in_row = (a.length + count_of_stars)/rows
        print "#{rows} #{symbols_in_row}\n"
        row_num = 1
        n = count_of_stars
        a.split("").each_with_index do |x,i|
            if count_of_stars > 0
                # p "kek: #{row_num*(symbols_in_row-1)}"
                if i == row_num*(symbols_in_row-1)
                    row_num+=1
                    count_of_stars-=1
                    print "*\n"
                end
            else
                if i == row_num*symbols_in_row-n
                    row_num+=1
                    print "\n"
                end
            end
            print x
        end
    end
else
    print "1 #{a.length}\n"
    print a
end

text = gets.chomp
len = text.length

reqa = 0
reqb = 0

done = false

(1..5).each do |a|
  (1..20).each do |b|
    tot = a*b
    if tot<len
      next
    else
      reqa = a
      reqb = b
      done = true
      break
    end
  end
  break if done
end

delta = reqa*reqb - len

astperrow = delta/reqa
rowast = []
reqa.times {rowast.push astperrow}

(0..reqa-1).each do |i|
  if rowast.inject(:+)<delta
    rowast[i] += 1
  end
end

currchar = 0
printed = 0

puts "#{reqa} #{reqb}"
(0..reqa-1).each do |row|
  while printed<reqb
    if(printed<reqb-rowast[row])
      print text[currchar]
      currchar+=1
      printed += 1
    else
      print '*'
      printed += 1
    end
  end
  puts
  printed = 0
end


Simon has a prime number x and an array of non-negative integers a1, a2, ..., an.Simon loves fractions very much. Today he wrote out number  on a piece of paper. After Simon led all fractions to a common denominator and summed them up, he got a fraction: , where number t equals xa1 + a2 + ... + an. Now Simon wants to reduce the resulting fraction. Help him, find the greatest common divisor of numbers s and t. As GCD can be rather large, print it as a remainder after dividing it by number 1000000007 (109 + 7).

The first line contains two positive integers n and x (1 ≤ n ≤ 105, 2 ≤ x ≤ 109) — the size of the array and the prime number. The second line contains n space-separated integers a1, a2, ..., an (0 ≤ a1 ≤ a2 ≤ ... ≤ an ≤ 109).

Print a single number — the answer to the problem modulo 1000000007 (109 + 7).

NoteIn the first sample . Thus, the answer to the problem is 8.In the second sample, . The answer to the problem is 27, as 351 = 13·27, 729 = 27·27.In the third sample the answer to the problem is 1073741824 mod 1000000007 = 73741817.In the fourth sample . Thus, the answer to the problem is 1.

require 'openssl'
n,x=gets.split.map(&:to_i)
a=gets.split.map(&:to_i)
m=10**9+7
s=a.reduce(&:+)
a.map!{|x|s-x}
p=a.min
a=Hash[a.map{|x|x-p}.inject(Hash.new(0)){|h,v|h[v]+=1;h}.sort]
i=0
loop do
  q,d=a[i].divmod x
  if d==0
    p+=1
    i+=1
    a[i]||=0
    a[i]+=q
  else
    break
  end
end
p x.to_bn.mod_exp([p,s].min,m)

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
kt = 1

while kt == 1 do
    while i > 0 && a[i] == a[i-1] do
        i -= 1
        k += 1
    end

    if i > 0
        i -= 1
        while k % x == 0 && a[n-1] > a[i] do
            k = k/x
            a[n-1] -= 1
        end
        if a[n-1] != a[i] || a[i] == 0
            kt = 0
        else
            k += 1
        end
    else
        while k % x == 0 do
            k = k/x
            a[n-1] -= 1
        end
        kt = 0
    end
end

result = s - a[n-1]

def modular_pow base, exponent, modulus
  result1 = 1
  while exponent > 0
    if (exponent % 2 == 1)
       result1 = (result1 * base) % modulus
    end
    exponent /= 2
    base = (base * base) % modulus
  end
  return result1
end

if result > s
    result = s
end

kq = modular_pow x, result, 1000000007

puts kq

require 'openssl'
x,n=gets.split.map(&:to_i)
a=gets.split.map(&:to_i)
m=10**9+7
s=a.reduce(&:+)
a.map!{|x|s-x}
p=a.min
a=a.group_by{|x|x}.sort.map{|a,b|[a,b.size]}
i=0
while a[i]
  q,d=a[i][1].divmod x
  if d==0
    p+=1
    i+=1
    a[i]||=[i,0]
    a[i][1]+=q
  else
    break
  end
end
p x.to_bn.mod_exp([p,s].min,m)

require 'openssl'
x,n=gets.split.map(&:to_i)
a=gets.split.map(&:to_i)
m=10**9+7
s=a.reduce(&:+)
a.map!{|x|s-x}
p=a.min
a=Hash[a.map{|x|x-p}.inject(Hash.new(0)){|h,v|h[v]+=1;h}.sort]
i=0
loop do
  q,d=a[i].divmod x
  if d==0
    p+=1
    i+=1
    a[i]||=0
    a[i]+=q
  else
    break
  end
end
p x.to_bn.mod_exp([p,s].min,m)

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
result = s - a[n-1]
kt = 1

while i> 0 && kt do
    kt = 1
    while i > 0 && a[i] == a[i-1] do
        i -= 1
        k += 1
    end

    if i > 0
        i -= 1
        while k % x == 0 && a[n-1] > a[i] do
            k = k/x
            a[n-1] -= 1
            result += 1
        end
        if a[n-1] != a[i]
            kt = 0
        else
            k += 1
        end
    else
        while k % x == 0 do
            k = k/x
            a[n-1] -= 1
            result += 1
        end
    end
end

kq = x**result % 1000000007

puts result

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]

s = 0

(0..n-1).each do |i|
    s +=a[i]
end

result = s - a[n-1]

i = n-1
while i > 0 && a[i-1] == a[i] do
    i -= 1
end

k = n - i
while k % x == 0
    k = k/x
    result += 1
end

kq = x**result % 1000000007

puts kq

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
kt = 1

while kt == 1 do
    while i > 0 && a[i] == a[i-1] do
        i -= 1
        k += 1
    end

    if i > 0
        i -= 1
        while k % x == 0 && a[n-1] > a[i] do
            k = k/x
            a[n-1] -= 1
        end
        if a[n-1] != a[i]
            kt = 0
        else
            k += 1
        end
    else
        while k % x == 0 do
            k = k/x
            a[n-1] -= 1
        end
        kt = 0
    end
end

result = s - a[n-1]

def modular_pow base, exponent, modulus
  result1 = 1
  while exponent > 0
    if (exponent % 2 == 1)
       result1 = (result1 * base) % modulus
    end
    exponent /= 2
    base = (base * base) % modulus
  end
  return result1
end

kq = modular_pow x, result, 1000000007

puts kq

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
result = s - a[n-1]
kt = 1

if x!= 1
    while kt == 1 do
        kt = 1
        while i > 0 && a[i] == a[i-1] do
            i -= 1
            k += 1
        end

        if i > 0
            i -= 1
            while k % x == 0 && a[n-1] > a[i] do
                k = k/x
                a[n-1] -= 1
                result += 1
            end
            if a[n-1] != a[i]
                kt = 0
            else
                k += 1
            end
        else
            while k % x == 0 do
                k = k/x
                a[n-1] -= 1
                result += 1
            end
            kt = 0
        end
    end
else
    result = 1
end

kq = x**result % 1000000007

puts kq

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
result = s - a[n-1]
kt = 1

while kt == 1 do
    kt = 1
    while i > 0 && a[i] == a[i-1] do
        i -= 1
        k += 1
    end

    if i > 0
        i -= 1
        while k % x == 0 && a[n-1] > a[i] do
            k = k/x
            a[n-1] -= 1
            result += 1
        end
        if a[n-1] != a[i]
            kt = 0
        else
            k += 1
        end
    else
        while k % x == 0 do
            k = k/x
            a[n-1] -= 1
            result += 1
        end
        kt = 0
    end
end

kq = ( x**result) % 1000000007

puts kq

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]
sodu = []

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
kt = 1

while kt == 1 do
    kt = 1
    while i > 0 && a[i] == a[i-1] do
        i -= 1
        k += 1
    end

    if i > 0
        i -= 1
        while k % x == 0 && a[n-1] > a[i] do
            k = k/x
            a[n-1] -= 1
        end
        if a[n-1] != a[i] || a[i] == 0
            kt = 0
        else
            k += 1
        end
    else
        while k % x == 0 do
            k = k/x
            a[n-1] -= 1
        end
        kt = 0
    end
end

result = s - a[n-1]

tuanhoan = 0

sodu[0] = 1
(1..result).each do |i|
    sodu[i] = (sodu[i-1]*x) % 1000000007
    if sodu[i] == 1
        tuanhoan = i
        break
    end
end

if tuanhoan != 0
    kq = sodu[result%tuanhoan]
else
    kq = sodu[result]
end

puts tuanhoan

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
result = s - a[n-1]
kt = 1

while i> 0 && kt do
    kt = 1
    while i > 0 && a[i] == a[i-1] do
        i -= 1
        k += 1
    end

    if i > 0
        i -= 1
        while k % x == 0 && a[n-1] > a[i] do
            k = k/x
            a[n-1] -= 1
            result += 1
        end
        if a[n-1] != a[i]
            kt = 0
        end
    else
        while k % x == 0 do
            k = k/x
            a[n-1] -= 1
            result += 1
        end
    end
end

kq = x**result % 1000000007

puts kq

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]
sodu1 = []

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
kt = 1

while kt == 1 do
    kt = 1
    while i > 0 && a[i] == a[i-1] do
        i -= 1
        k += 1
    end

    if i > 0
        i -= 1
        while k % x == 0 && a[n-1] > a[i] do
            k = k/x
            a[n-1] -= 1
        end
        if a[n-1] != a[i] || a[i] == 0
            kt = 0
        else
            k += 1
        end
    else
        while k % x == 0 do
            k = k/x
            a[n-1] -= 1
        end
        kt = 0
    end
end

result = s - a[n-1]

sodu1[0] = 1
(1..1000000).each do |i|
    sodu1[i] = (sodu1[i-1] * x ) % 1000000007
end

if result > 1000000
    sodu = sodu1[1000000]
    thuong = result/1000000
    du = result % 1000000
    (1..thuong).each do |i|
        sodu = (sodu * x) % 1000000007
    end
    if du != 0
        (1..du).each do |j|
            sodu = ( sodu * x) % 1000000007
        end
    end
end

if result <= 1000000
    kq = sodu1[result]
else
    kq = sodu
end

puts sodu

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
result = s - a[n-1]
kt = 1

while kt == 1 do
    kt = 1
    while i > 0 && a[i] == a[i-1] do
        i -= 1
        k += 1
    end

    if i > 0
        i -= 1
        while k % x == 0 && a[n-1] > a[i] do
            k = k/x
            a[n-1] -= 1
            result += 1
        end
        if a[n-1] != a[i]
            kt = 0
        else
            k += 1
        end
    else
        while k % x == 0 do
            k = k/x
            a[n-1] -= 1
            result += 1
        end
        kt = 0
    end
end

kq = x**result % 1000000007

puts kq

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
result = s - a[n-1]
kt = 1

puts result

while i> 0 && kt do
    kt = 1
    while i > 0 && a[i] == a[i-1] do
        i -= 1
        k += 1
    end

    if i > 0
        i -= 1
        while k % x == 0 && a[n-1] > a[i] do
            k = k/x
            a[n-1] -= 1
            result += 1
        end
        if a[n-1] != a[i]
            kt = 0
        end
    else
        while k % x == 0 do
            k = k/x
            a[n-1] -= 1
            result += 1
        end
    end
end

kq = x**result % 1000000007

puts result

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 0

while i> 0 && ( k % i == 0 || k == 1) do
    while i > 0 && a[i-1] == a[i] do
        i -= 1
        k += 1
    end

    while k % x == 0 && a[n-j] > a[n-i] do
        k = k/x
        a[n-1] -= 1
    end
end

while k % x == 0
    k = k/x
    a[n-1] -= 1
end

result = s - a[n-j]

kq = x**result % 1000000007

puts result

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
result = s - a[n-1]
kt = 1

while kt == 1 do
    kt = 1
    while i > 0 && a[i] == a[i-1] do
        i -= 1
        k += 1
    end

    if i > 0
        i -= 1
        while k % x == 0 && a[n-1] > a[i] do
            k = k/x
            a[n-1] -= 1
            result += 1
        end
        if a[n-1] != a[i] || a[i] == 0
            kt = 0
        else
            k += 1
        end
    else
        while k % x == 0 do
            k = k/x
            a[n-1] -= 1
            result += 1
        end
        kt = 0
    end
end

kq = ( x**result) % 1000000007

puts kq

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]
sodu1 = []

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
kt = 1

while kt == 1 do
    while i > 0 && a[i] == a[i-1] do
        i -= 1
        k += 1
    end

    if i > 0
        i -= 1
        while k % x == 0 && a[n-1] > a[i] do
            k = k/x
            a[n-1] -= 1
        end
        if a[n-1] != a[i] || a[i] == 0
            kt = 0
        else
            k += 1
        end
    else
        while k % x == 0 do
            k = k/x
            a[n-1] -= 1
        end
        kt = 0
    end
end

result = s - a[n-1]

def modular_pow base, exponent, modulus
  result1 = 1
  while exponent > 0
    if (exponent % 2 == 1)
       result1 = (result1 * base) % modulus
    end
    exponent /= 2
    base = (base * base) % modulus
  end
  return result1
end

kq = modular_pow x, result, (10**9 + 7)

puts kq

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
kt = 1

while kt == 1 do
    while i > 0 && a[i] == a[i-1] do
        i -= 1
        k += 1
    end

    if i > 0
        i -= 1
        while k % x == 0 && a[n-1] > a[i] do
            k = k/x
            a[n-1] -= 1
        end
        if a[n-1] != a[i] || a[i] == 0
            kt = 0
        else
            k += 1
        end
    else
        while k % x == 0 do
            k = k/x
            a[n-1] -= 1
        end
        kt = 0
    end
end

result = s - a[n-1]

def modular_pow base, exponent, modulus
  result1 = 1
  while exponent > 0
    if (exponent % 2 == 1)
       result1 = (result1 * base) % modulus
    end
    exponent /= 2
    base = (base * base) % modulus
  end
  return result1
end

kq = modular_pow x, result, 1000000007

puts kq

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
result = s - a[n-1]
kt = 1

while kt == 1 do
    kt = 1
    while i > 0 && a[i] == a[i-1] do
        i -= 1
        k += 1
    end

    if i > 0
        i -= 1
        while k % x == 0 && a[n-1] > a[i] do
            k = k/x
            a[n-1] -= 1
            result += 1
        end
        if a[n-1] != a[i] || a[i] == 0
            kt = 0
        else
            k += 1
        end
    else
        while k % x == 0 do
            k = k/x
            a[n-1] -= 1
            result += 1
        end
        kt = 0
    end
end

kq = ( x**result) % 1000000007

puts result

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
kt = 1

while kt == 1 do
    kt = 1
    while i > 0 && a[i] == a[i-1] do
        i -= 1
        k += 1
    end

    if i > 0
        i -= 1
        while k % x == 0 && a[n-1] > a[i] do
            k = k/x
            a[n-1] -= 1
        end
        if a[n-1] != a[i] || a[i] == 0
            kt = 0
        else
            k += 1
        end
    else
        while k % x == 0 do
            k = k/x
            a[n-1] -= 1
        end
        kt = 0
    end
end

result = s - a[n-1]
kq = ( x**result) % 1000000007

puts kq

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
result = s - a[n-1]
kt = 1

while kt == 1 do
    kt = 1
    while i > 0 && a[i] == a[i-1] do
        i -= 1
        k += 1
    end

    if i > 0
        i -= 1
        while k % x == 0 && a[n-1] > a[i] do
            k = k/x
            a[n-1] -= 1
            result += 1
        end
        if a[n-1] != a[i] || a[i] == 0
            kt = 0
        else
            k += 1
        end
    else
        while k % x == 0 do
            k = k/x
            a[n-1] -= 1
            result += 1
        end
        kt = 0
    end
end

kq = ( x**result) % 1000000007
if kq == 0
    kq = 10**9 + 7
end

puts kq

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]

s = 0

(0..n-1).each do |i|
    s +=a[i]
end

result = s - a[n-1]

puts x**result

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]
sodu1 = []
sodu2 = []

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
kt = 1

while kt == 1 do
    kt = 1
    while i > 0 && a[i] == a[i-1] do
        i -= 1
        k += 1
    end

    if i > 0
        i -= 1
        while k % x == 0 && a[n-1] > a[i] do
            k = k/x
            a[n-1] -= 1
        end
        if a[n-1] != a[i] || a[i] == 0
            kt = 0
        else
            k += 1
        end
    else
        while k % x == 0 do
            k = k/x
            a[n-1] -= 1
        end
        kt = 0
    end
end

result = s - a[n-1]

tuanhoan = 0

sodu = 1
(1..1000000).each do |i|
    sodu = (sodu * x ) % 1000000007
end

if result > 1000000
    sodu1 = sodu
    thuong = result/1000000
    du = result % 1000000
    (1..thuong).each do |i|
        sodu1 = (sodu1 * sodu) % 1000000007
    end
    if du > 0
        (1..du).each do |j|
            sodu1 = ( sodu1 * x) % 1000000007
        end
    end
    sodu = sodu1
end

puts sodu

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]
sodu = []

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
kt = 1

while kt == 1 do
    kt = 1
    while i > 0 && a[i] == a[i-1] do
        i -= 1
        k += 1
    end

    if i > 0
        i -= 1
        while k % x == 0 && a[n-1] > a[i] do
            k = k/x
            a[n-1] -= 1
        end
        if a[n-1] != a[i] || a[i] == 0
            kt = 0
        else
            k += 1
        end
    else
        while k % x == 0 do
            k = k/x
            a[n-1] -= 1
        end
        kt = 0
    end
end

result = s - a[n-1]

tuanhoan = 0

sodu[1] = x
(2..result).each do |i|
    sodu[i] = (sodu[i-1]*x) % 1000000007
    if sodu[i] == sodu[1]
        tuanhoan = i-1
        sodu[0] = sodu[i-1]
        break
    end
end

if tuanhoan != 0
    kq = sodu[result%tuanhoan]
else
    kq = sodu[result]
end

puts kq


Vasya, or Mr. Vasily Petrov is a dean of a department in a local university. After the winter exams he got his hands on a group's gradebook.Overall the group has n students. They received marks for m subjects. Each student got a mark from 1 to 9 (inclusive) for each subject.Let's consider a student the best at some subject, if there is no student who got a higher mark for this subject. Let's consider a student successful, if there exists a subject he is the best at.Your task is to find the number of successful students in the group.

The first input line contains two integers n and m (1 ≤ n, m ≤ 100) — the number of students and the number of subjects, correspondingly. Next n lines each containing m characters describe the gradebook. Each character in the gradebook is a number from 1 to 9. Note that the marks in a rows are not sepatated by spaces.

Print the single number — the number of successful students in the given group.

NoteIn the first sample test the student number 1 is the best at subjects 1 and 3, student 2 is the best at subjects 1 and 2, but student 3 isn't the best at any subject.In the second sample test each student is the best at at least one subject.

n, m = gets.split(' ').map(&:to_i)
a = []
n.times do
  a << gets.strip.split(//).map(&:to_i)
end
ary = Array.new(n, 0)
b = a.transpose
m.times do |c|
  mx = b[c].max
  n.times do |r|
    if mx == a[r][c]
      ary[r] = 1
    end
  end
end
p ary.count(1)

s = gets.split(' ')
n = s[0].to_i
m = s[1].to_i
a = Array.new(n).map!{ Array.new(m, 0) }
for r in 0..n-1
  ln = gets
  for c in 0..m-1
    a[r][c] = ln[c,1].to_i
  end
end
ary = Array.new(n, 0)
for c in 0..m-1
  mx = 0
  for r in 0..n-1
    if mx < a[r][c]
      mx = a[r][c]
    end
  end
  for r in 0..n-1
    if mx == a[r][c]
      ary[r] = 1
    end
  end
end
res = 0
for r in 0..n-1
  if ary[r] > 0
    res += 1
  end
end
p res

n, m = gets.split.map(&:to_i)
a = []
n.times {
  a << gets.strip.split(//).map(&:to_i)
}
ary = [0]
b = a.transpose
m.times { |c|
  mx = b[c].max
  n.times { |r|
    ary[r] = 1 if mx == a[r][c]
  }
}
p ary.count(1)

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

marks = []
subs = Array.new(m){|i| i = -1}
result = 0
reestr = Array.new(m){|i| i = []}
n.times do |i|
    marks << gets.chomp
    m.times do |j|
        if marks[i][j].to_i > subs[j]
            subs[j] = marks[i][j].to_i
            reestr[j] = []
            reestr[j] << i
        elsif marks[i][j].to_i == subs[j] then
            reestr[j] << i
        end
    end
end

students = Array.new(n){|i| i = 0}
m.times do |i|
    reestr[i].size.times do |j|
        students[reestr[i][j]] = 1
    end
end

students.delete(0)

puts students.size

n,m = gets.chomp.split(/ /).map!{|x| x.to_i}
a = []
s = []
for i in 0..n-1
	s << false
	a << gets.chomp.split(//).map!{|x| x.to_i}
end

for i in 0..m-1
	w = []
	for j in 0..n-1
		w << a[j][i]
	end
	wm = w.max
	for j in 0..n-1
		s[j] = true if w[j] == wm
	end
end
ret = 0
for j in 0..n-1
	ret += 1 if s[j] == true
end
puts ret

n, m = gets.split.map(&:to_i)
a = Array.new(n){gets.chars.to_a}
b = (['0'] * m).zip(*a).map(&:max)
p a.count{|_| _.zip(b).any?{|i, j| i == j}}

n,m=gets.split.map(&:to_i)
input=[]
n.times{ input.push(gets.split[0])}
ans=0
n.times{|i|
	ok=0
	m.times{|j|
		val=[]
		n.times{|k|
			val.push(input[k][j].to_i)
		}
		ok=1 if val.max==input[i][j].to_i
	}
	ans+=ok
}
p ans

#!/usr/bin/env ruby

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

highest = Array.new(m, 0)
list = []
n.times do
  arr = gets.chomp.each_char.to_a.map(&:to_i)
  arr.each_with_index do |score, idx|
    highest[idx] = [score, highest[idx]].max
  end
  list << arr
end

ans = 0
list.each do |arr|
  arr.each_with_index do |score, idx|
    if highest[idx] == score
      ans += 1
      break
    end
  end
end

p ans

tmp = gets.chomp.split " "
n = tmp[0].to_i
m = tmp[1].to_i

field = []
n.times do
  field.push gets.chomp.split("").map {|i| i.to_i }
end

d = {}
field = field.transpose

field.each_with_index do |line, no|
  max = line.max

  line.each_with_index do |e, i|
    if e == max
      d[i + 1] = true
    end
  end
end

puts d.keys.length

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

s = n.times.map do
  STDIN.gets.chars.map(&:to_i).take(m)
end

max = 0.upto(m-1).map do |i|
  s.max{|a,b| a[i] <=> b[i] }[i]
end
f = s.map do |student|
  max.each_with_index.any? do |m,i|
    student[i] >= m
  end
end
p f.select{|x| x }.size

n,m = gets.split.map(&:to_i)
a=[]
n.times do
  a<<gets.chomp.split('')
end
ans=0
for i in 0..n-1
	wb=false
	for j in 0..m-1
		ib=true
		for k in 0..n-1
			if(a[k][j] > a[i][j])
				ib = false
			end
		end
		if ib==true
			wb=true
		end
	end
	if wb==true
		ans+=1
	end
end
puts ans

def max_index a
  max = a.max
  (0...a.size).find_all{ |x| a[x] == max}
end
#n, m = gets.split.map(&:to_i)
gets
p readlines.map(&:chomp).map {|x| x.split('').map(&:to_i)}.transpose.map { |x| max_index(x) }.flatten.uniq.size

n, m = gets.chomp.split(/ /).map(&:to_i)
students = []
n.times do
    students << gets.chomp.split("").map(&:to_i)
end
toppers = []
m.times do |subject|
    topper = Array.new(n, 0)
    n.times do |student|
        marks = students[student][subject]
        topper[student] = marks if marks >= topper[student]
    end
    highest = topper.max
    topper.each_index do |student|
        marks = topper[student]
        toppers << student if marks == highest && !toppers.include?( student )
    end
end
puts toppers.length

k=gets.chomp
a=[]
b=[]
a=k.split(' ')
a[0].to_i.times do
  b<<gets.chomp
end
c=[]
a[0].to_i.times do |x|
  c<<b[x].split('')
end
max=[]
a[1].to_i.times do |y|
  maxt=0
  a[0].to_i.times do |x|
    maxt=[maxt,c[x][y].to_i].max
  end
  max[y]=maxt
end
is=[]
a[0].to_i.times do |x|
  a[1].to_i.times do |y|
    is[x]=true if c[x][y].to_i>=max[y]
  end
end
count=0
is.each do |x|
  count+=1 if x==true
end
puts count

n,m = gets.split.map(&:to_i)
a = []

n.times do
  a << gets.strip.split(//).map(&:to_i)
end

b = a.transpose

ans = Array.new(n){ false }
m.times do |i|
  mx = b[i].max
  b[i].each_with_index{|s,i| ans[i] = true if s == mx}
end

p ans.count(true)

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

n, m = gets.split.map &:to_i
table = []

n.times{
  table << gets.split(//).map(&:to_i)
}

students = Set.new
m.times{|index|
  max_value = table.collect{|column| column[index]}.max

  student_id = 0
  table.each{|array|
    students << student_id if array[index] == max_value
    student_id+=1
  }
}

print students.to_a.size

n,m=gets.chomp.split.collect{|a| a.to_i }
students=[]
max_scores=Array.new(m,0)
for i in 0..n-1
s=gets.chomp.split('').collect{|a| a.to_i}
students.push s
s.each_index{|j| if max_scores[j] < s[j] then max_scores[j]=s[j] end}
end
res=0
students.each do |student|
succ=false
student.each_index{|i| if student[i]==max_scores[i] then succ=true end}
if succ
 res+=1
end
end
puts res

input = gets.chomp
nums = input.split
n = Integer(nums[0])
m = Integer(nums[1])
a = Array.new
for i in 0...n
    a[i] = Array.new
    input = gets.chomp
    for j in 0...m
        a[i][j] = Integer(input[j])
    end
end

def best(a,n,j)
    b = Array.new
    c = 0
    for i in 0...n
        if a[i][j] > c
            c = a[i][j]
        end
    end
    for i in 0...n
        if a[i][j] == c
            b.push(i)
        end
    end
    return b
end
b = Array.new
for j in 0...m
    b += best(a,n,j)
end
b = b.sort
c = Array.new
c[0] = b[0]
for i in 0...b.length-1
    if b[i+1] != b[i]
        c.push(b[i+1])
    end
end
puts c.length

n, m = gets.split.map(&:to_i)
ar = Array.new(n){gets.chars.to_a}
b = (['0'] * m).zip(*ar).map(&:max)
cnt = 0
n.times do |i|
    a = false
    m.times do |j|
        if ar[i][j] == b[j] then
            a = true
        end
    end
    if a == true then
        cnt = cnt + 1
    end
end
p cnt

n,m = gets.split(" ").map(&:to_i)
max = Array.new(m,0)
arr = []
best = Hash.new(false)
n.times do |s|
	a = gets.split("").map(&:to_i)
	arr << a
	(0..m-1).each do |i|
		if a[i] >= max[i]
			max[i] = a[i]
		end
	end
end

arr.each_with_index do |a,i|
	(0..m-1).each do |j|
		best[i] = true if a[j] == max[j]
	end
end

puts best.keys.count

n, m = gets.split.map(&:to_i)
a = n.times.map{gets.chomp.chars.map(&:to_i)}
c = a.transpose.map(&:max)
puts a.count{|x| m.times.any?{|i| x[i] == c[i]}}

n, m = gets.split.map(&:to_i)
a = n.times.map{gets.chomp.split('').map(&:to_i)}
c = a.transpose.map{|x| x.max}
successful = Array.new(n, false)
n.times do |i|
  m.times do |j|
    if a[i][j] == c[j]
      successful[i] = true
      break
    end
  end
end
puts successful.count{|x| x == true}

n,m = gets.split(" ")
n = n.to_i
m = m.to_i
g = Array.new(m) {[]}
for i in 0..n-1
  gr = gets.chomp
  for j in 0..m-1
    g[j] << [gr[j].to_i, i]
	#puts gr[j]
  end
end
u = Array.new(n)
u.fill(0)
g.each do |q|
  q.sort_by! {|e| e[0]}
  q.reverse!
  curr = 0
  u[q[0][1]] = 1
  while curr < n - 1 && q[curr][0] == q[curr + 1][0]

    u[q[curr+1][1]] = 1
	curr += 1
  end
end

puts (u.inject(0) {|sum, curr| sum += curr})

students, subjects = gets.split.map(&:to_i)
a = []
students.times {
  a << gets.chomp.split('')
}
best = a.transpose.map(&:max)
res = 0
(0...students).each { |i|
  (0...subjects).each { |j|
    if a[i][j] == best[j]
      res += 1
      break
    end
  }
}
p res

require 'set'

n, m = gets.chomp.split.map { |x| x.to_i }
grades = []
for _ in 1..n
  grades << gets.chomp
end

suc_students = Set.new
for j in 0...m
  marks_for_subject = []
  for i in 0...n
    marks_for_subject << grades[i][j]
  end
  max = marks_for_subject.max
  for i in 0...n
    if marks_for_subject[i] == max
      suc_students << i
    end
  end
end

puts suc_students.size

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

gradebook = []
max = Array.new(m,0)

n.times do
  row = []
  row = gets.chomp.split(//).map(&:to_i)

  row.each_with_index do |o, i|
    if max[i] < o then max[i] = o end
  end

  gradebook << row
end

counter = 0

gradebook.each do |row|
  row.each_with_index do |o, i|
    if o == max[i]
      counter += 1
      break
    end
  end
end

puts counter

n,m=gets.split(' ').map(&:to_i)
a=Array.new(n)
best = Array.new(m){0}
best_student = Array.new(n){0}
n.times do |i|
    a[i] = gets.chomp.split('').map(&:to_i)
    a[i].each_index{|j|
        if(best[j] < a[i][j])
            best[j] = a[i][j]
        end
    }
end
count = 0
n.times do|i|
    a[i].each_index{|j|
        if(a[i][j] == best[j])
            count+=1
            break
        end
    }
end
p count

input = STDIN.read.split("\n")

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

students, subjects = ss[0], ss[1]

subj_max = []

subjects.times { subj_max.push 0 }

subjects.times do |i|
	students.times do |j|
		subj_max[i] = input[j][i].to_i if input[j][i].to_i > subj_max[i]
	end
end

succ = 0

students.times do |i|
	subjects.times do |j|
		if input[i][j].to_i == subj_max[j].to_i
			succ += 1
			break
		end
	end
end

STDOUT.puts succ

b=->(a){m=a.max;l=[];a.size.times{|i|a[i]==m&&l<<i};l}
n,m=gets.split.map &:to_i
g=[];r=[]
n.times{g << gets.bytes}
m.times{r|=b[g.map &:shift]}
puts r.size

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

s = n.times.map do
  STDIN.gets.chars.map(&:to_i).take(m)
end
f = Array.new(s.size,false)

max = 0.upto(m-1).map do |i|
  s.max{|a,b| a[i] <=> b[i] }[i]
end
p s.map do |student|
  max.each_with_index.any? do |m,i|
    student[i] >= m
  end
end

n,m = gets.split.map(&:to_i)
a=[]
n.times do
  a<<gets.chomp.split('')
end
best=a.transpose.map(&:max)
ans=0
i=0
j=0
while i<n do
	while j<m do
		if a[i][j] == best[j]
			ans += 1
			break
		end
		j+=1
	end
	i+=1
end
puts ans

n,m = gets.split.map(&:to_i)
a = []

n.times do
  a << gets.strip.split(//).map(&:to_i)
end

b = a.transpose

ans = Array.new(n){ false }
m.times do |i|
  mx = b[i].max
  b[i].each_with_index{|s,i| ans[i] = true if s == mx}
end
p ans
p ans.count(true)

n, m = gets.split.map(&:to_i)
ar = Array.new(n){gets.chars.to_a}
b = ['0' * m].zip(*ar).map(&:max)
cnt = 0
n.times do |i|
    a = false
    m.times do |j|
        if ar[i-1][j-1] == b[j-1] then
            a = true
        end
    end
    if a == true then
        cnt = cnt + 1
    end
end
p cnt

n, m = gets.split.map(&:to_i)
ar = Array.new(n){gets.chars.to_a}
b = ['0' * m].zip(*ar).map(&:max)
cnt = 0
n.times do |i|
	m.times do |j|
		if ar[i-1][j-1] == b[j-1] then
			cnt = cnt + 1
			break
		end
	end
end
p cnt

n, m = gets.split.map(&:to_i)
ar = Array.new(n){gets.chars.to_a}
b = (['0'] * m).zip(*ar).map(&:max)
cnt = 0
n.times do |i|
    a = false
    m.times do |j|
        if ar[i-1][j-1] == b[j-1] then
            a = true
        end
    end
    if a == true then
        cnt = cnt + 1
    end
end
p cnt

n,m = gets.split(" ")
n = n.to_i
m = n.to_i
g = Array.new(m) {[]}
for i in 0..n-1
  gr = gets
  for j in 0..m-1
    g[j] << [gr[j].to_i, i]
	#puts gr[j]
  end
end
u = Array.new(n)
u.fill(0)
g.each do |q|
  q.sort_by! {|e| e[0]}
  q.reverse!
  curr = 0
  u[q[0][1]] = 1
  while curr < n - 1 && q[curr][0] == q[curr + 1][0]
    u[q[curr+1][1]] = 1
	curr += 1
  end
end

puts (u.inject(0) {|sum, curr| sum += curr})

n,m = gets.split(" ")
n = n.to_i
m = n.to_i
g = Array.new(n){Array.new(m)}
for i in 0..n-1
  gr = gets
  for j in 0..m-1
    g[i][j] = gr[j]
	#puts gr[j]
  end
end

count = 0
for i in 0..n-1
  add = false
  for j in 0..m-1
    best = true
    for q in 0..n-1
      if(g[i][j] < g[q][j])
	    best = false
	  end
	end
	if best
	  add = true
	end
  end
  if add
    count += 1
  end
end

puts count


Vasya has his favourite number $$$n$$$. He wants to split it to some non-zero digits. It means, that he wants to choose some digits $$$d_1, d_2, \ldots, d_k$$$, such that $$$1 \leq d_i \leq 9$$$ for all $$$i$$$ and $$$d_1 + d_2 + \ldots + d_k = n$$$.Vasya likes beauty in everything, so he wants to find any solution with the minimal possible number of different digits among $$$d_1, d_2, \ldots, d_k$$$. Help him!

The first line contains a single integer $$$n$$$ — the number that Vasya wants to split ($$$1 \leq n \leq 1000$$$).

In the first line print one integer $$$k$$$ — the number of digits in the partition. Note that $$$k$$$ must satisfy the inequality $$$1 \leq k \leq n$$$. In the next line print $$$k$$$ digits $$$d_1, d_2, \ldots, d_k$$$ separated by spaces. All digits must satisfy the inequalities $$$1 \leq d_i \leq 9$$$. You should find a partition of $$$n$$$ in which the number of different digits among $$$d_1, d_2, \ldots, d_k$$$ will be minimal possible among all partitions of $$$n$$$ into non-zero digits. Among such partitions, it is allowed to find any. It is guaranteed that there exists at least one partition of the number $$$n$$$ into digits.

NoteIn the first test, the number $$$1$$$ can be divided into $$$1$$$ digit equal to $$$1$$$.In the second test, there are $$$3$$$ partitions of the number $$$4$$$ into digits in which the number of different digits is $$$1$$$. This partitions are $$$[1, 1, 1, 1]$$$, $$$[2, 2]$$$ and $$$[4]$$$. Any of these partitions can be found. And, for example, dividing the number $$$4$$$ to the digits $$$[1, 1, 2]$$$ isn't an answer, because it has $$$2$$$ different digits, that isn't the minimum possible number.

n = gets.chomp.to_i
puts n
n.times do |i|
	print "1 "
end

# Input
n = gets.to_i

# Output
puts n
puts "1 " * n

n = gets.to_i

puts n
puts ("1"*n).split("").join(" ")

a=gets.to_i
puts a
puts ([1]*a).join(" ")

num = gets.chomp
puts num
num.to_i.times do
  puts "1 "
end

n = gets.to_i
puts n
puts (["1"] * n).join(" ")

input = gets.to_i
flag_odd = 0
answer = 9

while input % answer != 0
  answer -= 1
end
  partition_num = input / answer
  answer = input / partition_num

puts partition_num
(partition_num - 1).times do
  print "#{answer} "
end

puts answer

n = gets.to_i
puts(n)
puts ([1] * n).join(' ')

n = gets.to_i
puts n
n.times do |i|
    print 1,(i == n-1 ? "\n" : " ")
end

DBG = true
n = gets.to_i
if n <= 9
  puts "1"
  puts "#{n}"

elsif n%2 == 0
  puts "#{n/2}"
  (n/2).times do
    print "2 "
  end
  puts ""

elsif n%3 == 0
  puts "#{n/3}"
  (n/3).times do
    print "3 "
  end
  puts ""

elsif n%5 == 0
  puts "#{n/5}"
  (n/5).times do
    print "5 "
  end
  puts ""

elsif n%7 == 0
  puts "#{n/7}"
  (n/7).times do
    print "7 "
  end
  puts ""

else
  puts "#{n}"
  (n).times do
    print "1 "
  end
  puts ""
end

def ascan; gets.split.map(&:to_i); end

n = gets.to_i

p n
puts [1]*n*" "

n = gets.to_i
puts n
n.times { print 1, ' ' }

n = gets.chomp.to_i
digits = []
while n > 0 do
	if n >= 9
		n = n - 9
		digits << 9
	else
		digits << n
		break
	end
end
puts digits.size
digits.each do |i|
	print "#{i} "
end

num = gets.chomp
puts num
num.to_i.times do
  puts 1.class
end

num = gets.chomp
num.to_i.times do
  puts "1" + " "
end

num = gets.chomp
num.to_i.times do
  puts 1.to_int
end

num = gets.chomp
num.to_i.times do
  puts 1
end

n = gets.to_i
n.times do |i|
    print 1,(i == n-1 ? "\n" : " ")
end

DBG = true
n = gets.to_i
if n <= 9
  puts "1"
  puts "#{n}"
elsif n%9 == 0
  puts "#{n/9}"
  (n/9).times do
    print "9 "
  end
  puts ""
else
  puts "#{(n/9)+1}"
  (n/9).times do
    print "9 "
  end
  puts "#{n%9}"
end

DBG = true
n = gets.to_i
if n <= 9
  puts "1"
  puts "#{n}"
elsif n%2 == 0
  puts "#{n/2}"
  (n/2).times do
    print "2 "
  end
  puts ""
elsif n%3 == 0
  puts "#{n/3}"
  (n/3).times do
    print "3 "
  end
  puts ""
elsif n%5 == 0
  puts "#{n/5}"
  (n/5).times do
    print "5 "
  end
  puts ""
elsif n%7 == 0
  puts "#{n/7}"
  (n/7).times do
    print "7 "
  end
  puts ""

else
  puts "#{(n/9)+1}"
  (n/9).times do
    print "9 "
  end
  puts "#{n%9}"
end

DBG = true
n = gets.to_i
if n <= 9
  puts "1"
  puts "#{n}"
elsif n%9 == 0
  puts "1"
  (n/9).times do
    print "9 "
  end
  puts ""
else
  puts "2"
  (n/9).times do
    print "9 "
  end
  puts "#{n%9}"
end

n = gets.to_i
puts (n + 8)/9
(n/9).times { print 9, ' ' }
print n%9 if n%9 != 0


Professor GukiZ is concerned about making his way to school, because massive piles of boxes are blocking his way. In total there are n piles of boxes, arranged in a line, from left to right, i-th pile (1 ≤ i ≤ n) containing ai boxes. Luckily, m students are willing to help GukiZ by removing all the boxes from his way. Students are working simultaneously. At time 0, all students are located left of the first pile. It takes one second for every student to move from this position to the first pile, and after that, every student must start performing sequence of two possible operations, each taking one second to complete. Possible operations are: If i ≠ n, move from pile i to pile i + 1; If pile located at the position of student is not empty, remove one box from it.GukiZ's students aren't smart at all, so they need you to tell them how to remove boxes before professor comes (he is very impatient man, and doesn't want to wait). They ask you to calculate minumum time t in seconds for which they can remove all the boxes from GukiZ's way. Note that students can be positioned in any manner after t seconds, but all the boxes must be removed.

The first line contains two integers n and m (1 ≤ n, m ≤ 105), the number of piles of boxes and the number of GukiZ's students.  The second line contains n integers a1, a2, ... an (0 ≤ ai ≤ 109) where ai represents the number of boxes on i-th pile. It's guaranteed that at least one pile of is non-empty.

In a single line, print one number, minimum time needed to remove all the boxes in seconds.

NoteFirst sample: Student will first move to the first pile (1 second), then remove box from first pile (1 second), then move to the second pile (1 second) and finally remove the box from second pile (1 second).Second sample: One of optimal solutions is to send one student to remove a box from the first pile and a box from the third pile, and send another student to remove a box from the third pile. Overall, 5 seconds.Third sample: With a lot of available students, send three of them to remove boxes from the first pile, four of them to remove boxes from the second pile, five of them to remove boxes from the third pile, and four of them to remove boxes from the fourth pile. Process will be over in 5 seconds, when removing the boxes from the last pile is finished.

n, m = gets.split.map &:to_i
a = gets.split.map &:to_i
l, r = 0, 2e14.to_i
ans = r
while l <= r
    mid = (l + r) >> 1
    b = a[0]
    i, j, t = 0, -1, 0
    while i < n and j < m
        if b == 0 then
            i += 1
            b = a[i]
            t -= 1
        else
            if t <= 0 then
                j += 1
                t = mid - i - 1
            else
                if t < b then
                    b -= t
                    t = 0
                else
                    t -= b
                    b = 0
                end
            end
        end
    end
    if i == n then
        r = mid - 1
        ans = [ans, mid].min
    else
        l = mid + 1
    end
end
puts ans


Polycarp must pay exactly $$$n$$$ burles at the checkout. He has coins of two nominal values: $$$1$$$ burle and $$$2$$$ burles. Polycarp likes both kinds of coins equally. So he doesn't want to pay with more coins of one type than with the other.Thus, Polycarp wants to minimize the difference between the count of coins of $$$1$$$ burle and $$$2$$$ burles being used. Help him by determining two non-negative integer values $$$c_1$$$ and $$$c_2$$$ which are the number of coins of $$$1$$$ burle and $$$2$$$ burles, respectively, so that the total value of that number of coins is exactly $$$n$$$ (i. e. $$$c_1 + 2 \cdot c_2 = n$$$), and the absolute value of the difference between $$$c_1$$$ and $$$c_2$$$ is as little as possible (i. e. you must minimize $$$|c_1-c_2|$$$).

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of one line. This line contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the number of burles to be paid by Polycarp.

For each test case, output a separate line containing two integers $$$c_1$$$ and $$$c_2$$$ ($$$c_1, c_2 \ge 0$$$) separated by a space where $$$c_1$$$ is the number of coins of $$$1$$$ burle and $$$c_2$$$ is the number of coins of $$$2$$$ burles. If there are multiple optimal solutions, print any one.

NoteThe answer for the first test case is "334 333". The sum of the nominal values of all coins is $$$334 \cdot 1 + 333 \cdot 2 = 1000$$$, whereas $$$|334 - 333| = 1$$$. One can't get the better value because if $$$|c_1 - c_2| = 0$$$, then $$$c_1 = c_2$$$ and $$$c_1 \cdot 1 + c_1 \cdot 2 = 1000$$$, but then the value of $$$c_1$$$ isn't an integer.The answer for the second test case is "10 10". The sum of the nominal values is $$$10 \cdot 1 + 10 \cdot 2 = 30$$$ and $$$|10 - 10| = 0$$$, whereas there's no number having an absolute value less than $$$0$$$.

t = gets().to_i
while(t!=0)
  n = gets().to_i
  if (n==1)
    puts "1 0\n"

  elsif (n==2)
    puts "0 1\n"

  elsif(n==3)
    puts "1 1\n"

  elsif (n==4)
    puts "2 1\n"

  elsif (n==5)
    puts "1 2\n"
  else
    h1 = n/3
    h2 = n/3.0
    if (h2+0.5>=h1+1)
      h = h1 + 1
    else
      h = h1
    end

    l = n - 2*h
    puts "#{l} #{h}\n"
  end
  t-=1
end

MOD = 10 ** 9 + 7
def gs; gets.chomp; end
def gi; gets.to_i; end
def gli; gets.split.map(&:to_i); end
def cyesno(x); puts x ? :Yes : :No; end
def uyesno(x); puts x ? :YES : :NO; end
def array_to_hash_by_count(c); Hash.new(0).tap { |x| c.each { |z| x[z] += 1 } }; end

gets.to_i.times do
  x = gets.to_i
  k = x / 3
  if x%3 == 0
    puts "#{k} #{k}"
  elsif x%3 == 1
    puts "#{k + 1} #{k}"
  else
    puts "#{k} #{k + 1}"
  end
end


Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, has a color ci, connecting vertex ai and bi.Mr. Kitayuta wants you to process the following q queries.In the i-th query, he gives you two integers — ui and vi.Find the number of the colors that satisfy the following condition: the edges of that color connect vertex ui and vertex vi directly or indirectly.

The first line of the input contains space-separated two integers — n and m (2 ≤ n ≤ 100, 1 ≤ m ≤ 100), denoting the number of the vertices and the number of the edges, respectively. The next m lines contain space-separated three integers — ai, bi (1 ≤ ai < bi ≤ n) and ci (1 ≤ ci ≤ m). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if i ≠ j, (ai, bi, ci) ≠ (aj, bj, cj). The next line contains a integer — q (1 ≤ q ≤ 100), denoting the number of the queries. Then follows q lines, containing space-separated two integers — ui and vi (1 ≤ ui, vi ≤ n). It is guaranteed that ui ≠ vi.

For each query, print the answer in a separate line.

NoteLet's consider the first sample.     The figure above shows the first sample.    Vertex 1 and vertex 2 are connected by color 1 and 2.  Vertex 3 and vertex 4 are connected by color 3.  Vertex 1 and vertex 4 are not connected by any single color.

class UF
def initialize;@uf=Hash.new{|_,k|k}end
def u a,b;@uf[f a]=@uf[f b]end
def f c;c==@uf[c]?c:@uf[c]=f(@uf[c])end
end

def g;gets.split.map(&:to_i)end
_,m=g
uf=(1..m).map{UF.new}
m.times{
  a,b,c=g
  uf[c-1].u(a,b)
}
gets.to_i.times{
  u,v=g
  p uf.count{|t|t.f(u)==t.f(v)}
}


Little Petya very much likes playing with little Masha. Recently he has received a game called "Zero-One" as a gift from his mother. Petya immediately offered Masha to play the game with him.Before the very beginning of the game several cards are lain out on a table in one line from the left to the right. Each card contains a digit: 0 or 1. Players move in turns and Masha moves first. During each move a player should remove a card from the table and shift all other cards so as to close the gap left by the removed card. For example, if before somebody's move the cards on the table formed a sequence 01010101, then after the fourth card is removed (the cards are numbered starting from 1), the sequence will look like that: 0100101. The game ends when exactly two cards are left on the table. The digits on these cards determine the number in binary notation: the most significant bit is located to the left. Masha's aim is to minimize the number and Petya's aim is to maximize it.An unpleasant accident occurred before the game started. The kids spilled juice on some of the cards and the digits on the cards got blurred. Each one of the spoiled cards could have either 0 or 1 written on it. Consider all possible variants of initial arrangement of the digits (before the juice spilling). For each variant, let's find which two cards are left by the end of the game, assuming that both Petya and Masha play optimally. An ordered pair of digits written on those two cards is called an outcome. Your task is to find the set of outcomes for all variants of initial digits arrangement.

The first line contains a sequence of characters each of which can either be a "0", a "1" or a "?". This sequence determines the initial arrangement of cards on the table from the left to the right. The characters "?" mean that the given card was spoiled before the game. The sequence's length ranges from 2 to 105, inclusive.

Print the set of outcomes for all possible initial digits arrangements. Print each possible outcome on a single line. Each outcome should be represented by two characters: the digits written on the cards that were left by the end of the game. The outcomes should be sorted lexicographically in ascending order (see the first sample).

NoteIn the first sample all 16 variants of numbers arrangement are possible. For the variant 0000 the outcome is 00. For the variant 1111 the outcome is 11. For the variant 0011 the outcome is 01. For the variant 1100 the outcome is 10. Regardless of outcomes for all other variants the set which we are looking for will contain all 4 possible outcomes.In the third sample only 2 variants of numbers arrangement are possible: 111 and 101. For the variant 111 the outcome is 11. For the variant 101 the outcome is 01, because on the first turn Masha can remove the first card from the left after which the game will end.

a=gets.split[0]
zero=0
one=0
hatena=0
a.each_char{|i|
        if i=="1" then
                one+=1
        elsif i=="0" then
                zero+=1
        elsif i=="?" then
                hatena+=1
        end
}
minus=zero+hatena+one-2
ans=[0,0,0,0]
res=["00","01","10","11"]
for i in 0..hatena
        z=zero+i
        o=one+hatena-i
        if minus%2==1 then z+=1 end
                if z>o then
                        ans[0]=1
                elsif z<o then
                        ans[3]=1
                else
                        if a[minus+1]=="?" then
                                if hatena-i>0 then ans[1]=1 end
                                if i>0 then ans[2]=1 end
                        elsif a[minus+1]=="1" then
                                ans[1]=1
                        else
                                ans[2]=1
                        end
                end
end
4.times{|i|
        if ans[i]>0 then
                puts res[i]
        end
}

def cal(str,ans)
  map={'0'=>0,'1'=>1,'?'=>2};
  if str[0].chr=='?' then
    str[0]='1'; cal(str,ans);
    str[0]='0'; cal(str,ans);
    return;
  end
  cnt=[0,0-str.length%2,0];
  str.each_byte{ |c| cnt[map[c.chr]]=cnt[map[c.chr]]+1; }
  ans.push("00") if str.length/2<cnt[0]+cnt[2];
  ans.push("11") if str.length/2<cnt[1]+cnt[2];
  return if str.length/2<cnt[0] || str.length/2<cnt[1];
  ans.push("10") if str[0].chr=='0';
  ans.push("01") if str[0].chr=='1';
end

ans=[]
a=gets.chomp.reverse;
cal(a,ans);
ans.sort!.uniq!;
ans.map{|s| puts(s); }

a=gets.split[0]
zero=0
one=0
hatena=0
a.each_char{|i|
        if i=="1" then
                one+=1
        elsif i=="0" then
                zero+=1
        elsif i=="?" then
                hatena+=1
        end
}
minus=zero+hatena+one-2
ans=[0,0,0,0]
res=["00","01","10","11"]
for i in 0..hatena
        z=zero+i
        o=one+hatena-i
        if minus%2==1 then z+=1 end
                if z>o then
                        ans[0]=1
                elsif z<o then
                        ans[3]=1
                else
                        if a[minus+1]=="?" then
                                ans[1]=1
                                ans[2]=1
                        elsif a[minus+1]=="1" then
                                ans[1]=1
                        else
                                ans[2]=1
                        end
                end
end
4.times{|i|
        if ans[i]>0 then
                puts res[i]
        end
}

a=gets.split[0]
zero=0
one=0
hatena=0
a.each_char{|i|
        if i=="1" then
                one+=1
        elsif i=="0" then
                zero+=1
        elsif i=="?" then
                hatena+=1
        end
}
minus=zero+hatena+one-2
ans=[0,0,0,0]
res=["00","01","10","11"]
for i in 0..hatena
        z=zero+i
        o=one+hatena-i
        if minus%2==1 then z+=1 end
                if z>o then
                        ans[0]=1
                elsif z<o then
                        ans[3]=1
                else
                        if a[minus+1]=="?" then
                                if hatena-i>0 then ans[1]=1 end
                                if hatena>0 then ans[2]=1 end
                        elsif a[minus+1]=="1" then
                                ans[1]=1
                        else
                                ans[2]=1
                        end
                end
end
4.times{|i|
        if ans[i]>0 then
                puts res[i]
        end
}

def cal(str,ans)
  map={'0'=>0,'1'=>1,'?'=>2};
  if str[0].chr=='?' then
    str[0]='1'; cal(str,ans);
    str[0]='0'; cal(str,ans);
    return;
  end
  cnt=[0-str.length%2,0,0];
  str.each_byte{ |c| cnt[map[c.chr]]=cnt[map[c.chr]]+1; }
  ans.push("00") if str.length/2<cnt[0]+cnt[2];
  ans.push("11") if str.length/2<cnt[1]+cnt[2];
  return if str.length/2<cnt[0] || str.length<cnt[1];
  ans.push("10") if str[0].chr=='0';
  ans.push("01") if str[0].chr=='1';
end

ans=[]
a=gets.chomp.reverse;
cal(a,ans);
ans.sort!.uniq!;
ans.map{|s| puts(s); }

def cal(str,ans)
  map={'0'=>0,'1'=>1,'?'=>2};
  if str[0].chr=='?' then
    str[0]='1'; cal(str,ans);
    str[0]='0'; cal(str,ans);
    return;
  end
  cnt=[0-str.length%2,0,0];
  str.each_byte{ |c| cnt[map[c.chr]]=cnt[map[c.chr]]+1; }
  ans.push("00") if str.length/2<cnt[0]+cnt[2];
  ans.push("11") if str.length/2<cnt[1]+cnt[2];
  return if str.length/2<cnt[0] || str.length<cnt[1];
  ans.push("10") if str[0].chr=='0';
  ans.push("01") if str[0].chr=='1';
end

ans=[]
a=gets.chomp.reverse;
cal(a,ans);
ans.sort!.uniq!;
ans.map{|s| p s; }

def cal(str,ans)
  map={'0'=>0,'1'=>1,'?'=>2};
  if str[0].chr=='?' then
    str[0]='1'; cal(str,ans);
    str[0]='0'; cal(str,ans);
    return;
  end
  cnt=[0-str.length%2,0,0];
  str.each_byte{ |c| cnt[map[c.chr]]=cnt[map[c.chr]]+1; }
  ans.push("00") if str.length/2<cnt[0]+cnt[2];
  ans.push("11") if str.length/2<cnt[1]+cnt[2];
  return if str.length/2<cnt[0] || str.length/2<cnt[1];
  ans.push("10") if str[0].chr=='0';
  ans.push("01") if str[0].chr=='1';
end

ans=[]
a=gets.chomp.reverse;
cal(a,ans);
ans.sort!.uniq!;
ans.map{|s| puts(s); }


You are given a sequence of numbers a1, a2, ..., an, and a number m.Check if it is possible to choose a non-empty subsequence aij such that the sum of numbers in this subsequence is divisible by m.

The first line contains two numbers, n and m (1 ≤ n ≤ 106, 2 ≤ m ≤ 103) — the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109).

In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist.

NoteIn the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5.In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist.In the third sample test you need to choose two numbers 3 on the ends.In the fourth sample test you can take the whole subsequence.

require 'set'
n, m = gets.chomp.split(' ').map(&:to_i)
as = gets.chomp.split(' ').map(&:to_i)

hs = Hash.new(0)
as.each do |a|
  hs[a % m] += 1 if hs[a % m] < m
end

bs = Set.new [0]

rtn = false
hs.each do |k, v|
  break if rtn
  cs = Set.new
  bs.each do |b|
    for j in 1..v
      cs << (k * j + b) % m
      rtn = true if (k * j + b) % m == 0
    end
  end
  bs += cs
end

puts rtn ? 'YES' : 'NO'

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

bs = Array.new(m, false)
bs[0] = true

rtn = false
as.each do |a|
  for i in 0..m - 1
    if bs[i]
      bs[(a + i) % m] = true
      rtn = true if (a + i) % m == 0
    end
  end
end

puts rtn ? 'YES' : 'NO'


Berland scientists know that the Old Berland language had exactly n words. Those words had lengths of l1, l2, ..., ln letters. Every word consisted of two letters, 0 and 1. Ancient Berland people spoke quickly and didn’t make pauses between the words, but at the same time they could always understand each other perfectly. It was possible because no word was a prefix of another one. The prefix of a string is considered to be one of its substrings that starts from the initial symbol.Help the scientists determine whether all the words of the Old Berland language can be reconstructed and if they can, output the words themselves.

The first line contains one integer N (1 ≤ N ≤ 1000) — the number of words in Old Berland language. The second line contains N space-separated integers — the lengths of these words. All the lengths are natural numbers not exceeding 1000.

If there’s no such set of words, in the single line output NO. Otherwise, in the first line output YES, and in the next N lines output the words themselves in the order their lengths were given in the input file. If the answer is not unique, output any.

class Letter
	def initialize(idx, len)
		@idx = idx
		@len = len
		@str = ""
	end
	attr_accessor :idx, :len, :str
end

n = gets.chomp.to_i
ar = Array.new(n)
a = gets.chomp.split(/ /).map!{|x|x.to_i}
for i in 0..n-1
	ar[i] = Letter.new(i, a[i])
end

ar.sort!{|a,b| a.len <=> b.len }
ret = "YES"
prev = 0
cnt = 0
cntmax = 0

for i in 0..n-1
	if cntmax > 0 && cnt >= cntmax-1
		ret = "NO"
		break
	end

	if prev == ar[i].len
		cnt += 1
	else
		diff = ar[i].len - prev
		if prev > 0
			cnt += 1
			cnt *= 2**diff
		else
			cnt = 0
		end
		cntmax = 2**ar[i].len
	end
	prev = ar[i].len
	ar[i].str = sprintf("%." + prev.to_s + "b", cnt)
end
ar.sort!{|a,b| a.idx <=> b.idx }
puts ret
if ret == "YES"
	ar.each{|x| puts x.str }
end

n = gets.chomp.to_i
a = gets.chomp.split(/ /).map!{|x|x.to_i}
a.sort!
ret = "YES"
prev = 0
cnt = 0
diff = 0
cntmax = 0
ra = Array.new
for i in 0..n-1
	if cntmax > 0 && cnt >= cntmax-1
		ret = "NO"
		break
	end

	if prev == a[i]
		cnt += 1
	else
		diff = a[i] - prev
		if prev > 0
			cnt += 1
			cnt *= 2**diff
		else
			cnt = 0
		end
		cntmax = 2**a[i]
	end
	prev = a[i]
	ra.push(sprintf("%." + prev.to_s + "b", cnt))
end
puts ret
if ret == "YES"
	ra.each{|x| puts x }
end

n = gets.chomp.to_i
a = gets.chomp.split(/ /).map!{|x|x.to_i}
a.sort!
ret = "YES"
prev = 0
cnt = 0
diff = 0
cntmax = 0
ra = Array.new
for i in 0..n-1
	if cntmax > 0 && cnt >= cntmax-1
		ret = "NO"
		break
	end

	if prev == a[i]
		cnt += 1
	else
		diff = a[i] - prev
		if prev > 0
			cnt += 1
			cnt *= 2**diff
		else
			cnt = 0
		end
		cntmax = 2**a[i]
	end
	prev = a[i]
	ra.push(cnt.to_s(2))
end
puts ret
if ret == "YES"
	ra.each{|x| puts x }
end


A and B are preparing themselves for programming contests.B loves to debug his code. But before he runs the solution and starts debugging, he has to first compile the code.Initially, the compiler displayed n compilation errors, each of them is represented as a positive integer. After some effort, B managed to fix some mistake and then another one mistake.However, despite the fact that B is sure that he corrected the two errors, he can not understand exactly what compilation errors disappeared — the compiler of the language which B uses shows errors in the new order every time! B is sure that unlike many other programming languages, compilation errors for his programming language do not depend on each other, that is, if you correct one error, the set of other error does not change.Can you help B find out exactly what two errors he corrected?

The first line of the input contains integer n (3 ≤ n ≤ 105) — the initial number of compilation errors. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the errors the compiler displayed for the first time.  The third line contains n - 1 space-separated integers b1, b2, ..., bn - 1 — the errors displayed at the second compilation. It is guaranteed that the sequence in the third line contains all numbers of the second string except for exactly one.  The fourth line contains n - 2 space-separated integers с1, с2, ..., сn - 2 — the errors displayed at the third compilation. It is guaranteed that the sequence in the fourth line contains all numbers of the third line except for exactly one.

Print two numbers on a single line: the numbers of the compilation errors that disappeared after B made the first and the second correction, respectively.

NoteIn the first test sample B first corrects the error number 8, then the error number 123.In the second test sample B first corrects the error number 1, then the error number 3. Note that if there are multiple errors with the same number, B can correct only one of them in one step.

#!/usr/bin/env ruby
def f(a,b)
	a[b.size.times.find{|i|
		a[i]<b[i]
	}||b.size]
end
x=$<.drop(1).map{|e|e.split.map(&:to_i).sort}
p f(x[0],x[1]),f(x[1],x[2])

# http://codeforces.com/contest/519/problem/B
class Errors
  def initialize(a, b, c)
    @errors = [a, b, c].map(&:sort)
  end

  def corrected_error(n)
    (0..@errors[n - 1].length).each do |idx|
      return @errors[n - 1][idx] if @errors[n][idx] != @errors[n - 1][idx]
    end
  end
end

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

erros = Errors.new(a, b, c)
puts erros.corrected_error(1)
puts erros.corrected_error(2)

gets.to_i
a=gets.split.map{|x| x.to_i}
b=gets.split.map{|x| x.to_i}
c=gets.split.map{|x| x.to_i}

puts a.inject(0){|sum, elem| elem+sum} - b.inject(0){|sum, elem| elem+sum}
puts b.inject(0){|sum, elem| elem+sum} - c.inject(0){|sum, elem| elem+sum}

gets
sum = gets.split.map { |x| x.to_i }.inject(:+)
sum_two = gets.split.map { |x| x.to_i }.inject(:+)
sum_three = gets.split.map { |x| x.to_i }.inject(:+)
puts sum - sum_two,sum_two - sum_three

def xor(a, b)
  y=0
  for x in a
    y^=x
  end
  for x in b
    y^=x
  end
  y
end
gets
a = gets.split.map(&:to_i)
b = gets.split.map(&:to_i)
c = gets.split.map(&:to_i)
puts xor(a,b)
puts xor(b,c)

class Array
  def subtract_once(values)
    counts = values.inject(Hash.new(0)) { |h, v| h[v] += 1; h }
    reject { |e| counts[e] -= 1 unless counts[e].zero? }
  end
end

num_rows = gets.strip.to_i

first_errors = gets.strip.split(" ").map{|i| i.to_i }
second_errors = gets.strip.split(" ").map{|i| i.to_i }
third_errors = gets.strip.split(" ").map{|i| i.to_i }

first_error = first_errors.subtract_once(second_errors)
second_error = second_errors.subtract_once(third_errors)

puts first_error
puts second_error

def f(a,b)
	p2=0
	l2=b.size
	while p2<l2
		if a[p2]!=b[p2] then
			return a[p2]
		end
		p2=p2+1
	end
	return a[b.size]
end

require 'set'
gets
a=gets.split.map{|e| e.to_i}.sort
b=gets.split.map{|e| e.to_i}.sort
c=gets.split.map{|e| e.to_i}.sort
puts f(a,b)
puts f(b,c)

require 'set'
gets.chomp
x0 = gets.chomp.split(' ').map(&:to_i).inject(:+);
x1 = gets.chomp.split(' ').map(&:to_i).inject(:+);
x2 = gets.chomp.split(' ').map(&:to_i).inject(:+);
puts x0-x1
puts x1-x2

n = gets.to_i
a = gets.split.map(&:to_i).each.inject(&:+)
b = gets.split.map(&:to_i).each.inject(&:+)
c = gets.split.map(&:to_i).each.inject(&:+)
puts a - b, b - c

class Array
  def subtract_once(values)
    counts = values.inject(Hash.new(0)) { |h, v| h[v] += 1; h }
    reject { |e| counts[e] -= 1 unless counts[e].zero? }
  end
end

n = gets.chomp.to_i

set1 = gets.chomp.split(' ').map { |e| e.to_i }.sort
set2 = gets.chomp.split(' ').map { |e| e.to_i }.sort
set3 = gets.chomp.split(' ').map { |e| e.to_i }.sort

p set1.subtract_once(set2)[0]
p set2.subtract_once(set3)[0]

n = Integer(gets)
get_first = gets.chomp.split
n_first = Array.new
for i in 0...get_first.length
    n_first[i] = Integer(get_first[i])
end
get_second = gets.chomp.split
n_second = Array.new
for i in 0...get_second.length
    n_second[i] = Integer(get_second[i])
end
get_third = gets.chomp.split
n_third = Array.new
for i in 0...get_third.length
    n_third[i] = Integer(get_third[i])
end

n_first = n_first.sort
n_second = n_second.sort
n_third = n_third.sort
first = 0
second = 0
for i in 0...n_second.length
    if n_first[i] != n_second[i]
        first = n_first[i]
        break
    else
        first = n_first[n_second.length]
    end
end
for i in 0...n_third.length
    if n_second[i] != n_third[i]
        second = n_second[i]
        break
    else
        second = n_second[n_third.length]
    end
end

puts first
puts second

#Made By Hasbi Hamza
gets.chomp
a = gets.chomp.split(' ').map(&:to_i).inject(:+);
b = gets.chomp.split(' ').map(&:to_i).inject(:+);
c = gets.chomp.split(' ').map(&:to_i).inject(:+);
puts a-b
puts b-c

n = gets.to_i
a = []
b =[]
c = []

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

def sum(n)
  return n.inject(0){|sum, i| sum+i}
end

puts sum(a) - sum(b)
puts sum(b) - sum(c)

def check(a, b)
  a.zip(b).find { |x| x.first != x.last }.first
end

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

puts check(a, b)
puts check(b, c)

def check(a, b)
  a.zip(b).each do |x|
    if x.first != x.last
      puts x.first
      break
    end
  end
end

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

check a, b
check b, c

# 519B

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

n = gets.to_i
s = read_line
t = read_line
p = ->(i) { s[i] != t[i] }
puts s[(0...n - 1).find(&p) || n - 1]
s = read_line
puts t[(0...n - 2).find(&p) || n - 2]

gets.to_i
a=gets.split.map{|x| x.to_i}.sort!
b=gets.split.map{|x| x.to_i}.sort!
c=gets.split.map{|x| x.to_i}.sort!

def otlich(a, b)
  for i in 0...a.max
    b<<i
    b.sort!
    if a == b
      b.delete(i)
      return i
    else
      b.delete(i)
    end
  end
end
puts otlich(a, b)
puts otlich(b, c)

gets.to_i
a=gets.split.map{|x| x.to_i}
b=gets.split.map{|x| x.to_i}
c=gets.split.map{|x| x.to_i}

puts a-b
puts b-c

gets
m = gets.split
ans = []
2.times do
  ans << m - gets.split
end
ans.uniq.each do |x| puts x end

n = gets.to_i
h = Hash.new { |hash, key| hash[key] = 0  }
for i in gets.split.map(&:to_i).each
	h[i] += 1
end
for i in gets.split.map(&:to_i).each
	h[i] += 1
end
for i in gets.split.map(&:to_i).each
	h[i] += 1
end
a = 0
b = 0
for i in h
	if i[1] % 3 == 1
		a = i[0]
	end
	if i[1] % 3 == 2
		b = i[0]
	end
end
puts a,b


Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk).Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost.

The first line contains an integer n (1 ≤ n ≤ 200000). The second line contains n integers a1, a2, ..., an, where ai is 0 if the cow number i is facing left, and 1 if it is facing right.

Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

NoteIn the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.

#$stdin.reopen('/home/chen/Desktop/input.txt', 'r')

n=gets.to_i

cows = gets.split.map(&:to_i)
n1 = 0
s=0

(0..(n-1)).each do |ind|

	if cows[ind] == 1
		n1+=1
	else
		s+=n1
	end

end

puts s

class ProblemA
  def initialize
    @dats = STDIN.read.split("\n")
    @n = @dats[0].to_i
    @cows = @dats[1].split(" ").map(&:to_i)
  end

  def solve
    left1 = 0
    ret = 0
    (0..(@n-1)).each do |i|
      left1 += 1 if @cows[i] == 1
      ret += left1 if @cows[i] == 0
    end
    return ret
  end
end

solver = ProblemA.new
print solver.solve

gets
m=gets.split
res=k=0
m.each do |c|
    k+=1 if (c == '1')
    res += k if (c == '0')
end
p res

n = gets.to_i
a = gets.split.collect{|i| i.to_i}
ans = Array.new(2,0)
cnt = 0
(1...n).each do |i|
    cnt += 1 if a[i-1] == 1
    ans[0] += cnt if a[i]== 0
end
cnt = 0
(n-2).downto(0).each do |i|
    cnt += 1 if a[i+1] == 0
    ans[1] += cnt if a[i]== 1
end
puts ans.min

n = gets.to_i
a = gets.split.collect{|i| i.to_i}
count = Array.new(n,0)
(1...n).each do |i|
    count[i] = count[i-1]
    count[i] += 1 if a[i-1] == 1
end
ans = 0
(0...n).each do |i|
    ans += count[i] if a[i]==0
end
count = Array.new(n,0)
(n-2).downto(0).each do |i|
    count[i] = count[i+1]
    count[i] += 1 if a[i+1] == 0
end
tmp = 0
(0...n).each do |i|
    tmp += count[i] if a[i]==1
end
puts [ans,tmp].min

n = gets.to_i
a = gets.split.collect{|i| i.to_i}
ans = Array.new(2,0)
count = Array.new(n,0)
(1...n).each do |i|
    count[i] = count[i-1]
    count[i] += 1 if a[i-1] == 1
    ans[0] += count[i] if a[i]== 0
end
count = Array.new(n,0)
(n-2).downto(0).each do |i|
    count[i] = count[i+1]
    count[i] += 1 if a[i+1] == 0
    ans[1] += count[i] if a[i]== 1
end
puts ans.min

n=gets.to_i
a=gets.split.map(&:to_i)
l=[0]*n
r=[0]*n
ll=rr=0
n.times do |i|
  l[n-i-1]=ll
  ll+=1 if a[n-i-1]==0
  r[i]=rr
  rr+=1 if a[i]==1
end
x=[0,0]
n.times do |i|
  x[0]+=l[i]if a[i]==1
  x[1]+=r[n-i-1]if a[n-i-1]==0
end
p x.min

n=gets.to_i
a=gets.split.map(&:to_i)
l=[]
r=[]
p=q=0
n.times do |i|
  l[n-i-1]=p
  p+=1 if a[n-i-1]==0
  r[i]=q
  q+=1 if a[i]==1
end
x=[0,0]
n.times do |i|
  x[0]+=l[i]if a[i]==1
  x[1]+=r[n-i-1]if a[n-i-1]==0
end
p x.min

n=gets.to_i
a=gets.split.map(&:to_i)
b=c=d=e=0
n.times do |i|
  c+=b if a[n-i-1]==1
  b+=1 if a[n-i-1]==0
  e+=d if a[i]==0
  d+=1 if a[i]==1
end
p [c,e].min