Companies always have a lot of equipment, furniture and other things. All of them should be tracked. To do this, there is an inventory number assigned with each item. It is much easier to create a database by using those numbers and keep the track of everything.During an audit, you were surprised to find out that the items are not numbered sequentially, and some items even share the same inventory number! There is an urgent need to fix it. You have chosen to make the numbers of the items sequential, starting with 1. Changing a number is quite a time-consuming process, and you would like to make maximum use of the current numbering.You have been given information on current inventory numbers for n items in the company. Renumber items so that their inventory numbers form a permutation of numbers from 1 to n by changing the number of as few items as possible. Let us remind you that a set of n numbers forms a permutation if all the numbers are in the range from 1 to n, and no two numbers are equal.

The first line contains a single integer n — the number of items (1 ≤ n ≤ 105). The second line contains n numbers a1, a2, ..., an (1 ≤ ai ≤ 105) — the initial inventory numbers of the items.

Print n numbers — the final inventory numbers of the items in the order they occur in the input. If there are multiple possible answers, you may print any of them.

NoteIn the first test the numeration is already a permutation, so there is no need to change anything.In the second test there are two pairs of equal numbers, in each pair you need to replace one number.In the third test you need to replace 2 by 1, as the numbering should start from one.

n = gets.chomp.to_i
arr = gets.chomp.split.map{|x| x.to_i}

ref = Array.new(n+1){0}

arr.each do |v|
	if v<=n
		ref[v]+=1
	end
end
stack = []
(1..n).each do |i|
	if ref[i] == 0
	stack.push(i)
	end
end

arr.each do |v|
	if v>n
		print "#{stack.pop} "
	elsif ref[v] > 1
		print "#{stack.pop} "
		ref[v]-=1
	else
			print "#{v} "
	end
end

require 'set'
n = STDIN.gets.chomp
input = STDIN.gets.chomp

def remap(n, input)
  order = Set.new
  n.to_i.times {|n| order << n+1}

  input = input.split.map(&:to_i)
  output = []

  reworked_input = []
  positions = []

  input.length.times do |position|
    key = input[position]
    unless order.include? key
      positions << position
    else
      order.delete key
    end
  end

  counter = 0

  order.each do |element|
    input[positions[counter]] = element
    counter += 1
  end

  return input.join(" ")
end

puts remap(n, input)

#!/usr/bin/ruby
N=gets.to_i
A=[]
h={}
gets.split.each_with_index{|e,i|
	n=e.to_i-1
	if !A[n]&&n<N
		A[n]=i
		h[i]=n
	end
}
x=0
N.times{|i|
	if !A[i]
		x+=1 while h[x]
		A[i]=x
		h[x]=i
	end
}
B=[]
A.each_with_index{|e,i|B[e]=i+1}
puts B*' '

# http://codeforces.com/contest/569/problem/B
n = gets.chomp.to_i
a = gets.chomp.split.map(&:to_i)

array = Array.new(n + 1) { 0 }
a.each { |e| array[e] += 1 if e <= n }

stack = [].tap do |out|
  (1..n).each { |idx| out << idx if array[idx] == 0 }
end

ans = a.map do |e|
  case
  when e > n
    stack.pop
  when array[e] > 1
    array[e] -= 1
    stack.pop
  else
    e
  end
end

puts ans.join(' ')

def read_ints; gets.chomp.split.map(&:to_i) end
n = read_ints[0]
a_elems = read_ints
bad_a_elems = Array.new(n)
positions = Array.new(n)
a_elems.each_with_index do |a,i|
	a-=1
	if positions[a].nil? && a<n
		positions[a]=true
	else
		bad_a_elems[i]=true
	end
end
current_free_postition = positions.find_index(nil)
bad_a_elems.each_with_index do |bad_a, i|
	if bad_a
		a_elems[i] = current_free_postition+1
		begin current_free_postition+=1
		end while positions[current_free_postition]
	end
end
puts a_elems.join(' ')

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

used = {}
nums.each do |n|
	used[n] = true
end

f = 0
free = Array.new(nums.size)
(1..nums.size).each do |n|
	next if used[n]

	free[f] = n
	f += 1
end

f = 0
res = Array.new(nums.size)
used = {}
nums.each_with_index do |n, i|
	if used[n] || n > nums.size
		res[i] = free[f]
		f += 1
	else
		res[i] = n
		used[n] = true
	end
end

puts res.join(' ')

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

bs = Array.new(n+1, false)
cs = Array.new(n+1, false)

for i in 0..n-1
  a = as[i]
  if 1 <= a && a <= n && !bs[a]
    bs[a] = true
    cs[i] = true
  end
end

j = 1
for i in 0..n-1
  a = as[i]
  while bs[j]
    j += 1
  end
  if cs[i]
    print as[i]
  else
    print j
    j += 1
  end
  print ' '
end
print "\n"

#!/usr/bin/ruby

n = ARGF.gets.to_i
a = ARGF.gets.split(' ').map { |s| s.to_i }

b = Array.new(n + 1, 0)
b[0] = 1

a.each do |e|
    if e <= n then b[e] += 1 end
end

c = Array.new
b.each_with_index do |e, i|
    if b[i] == 0 then c << i end
end

a.each_with_index do |e, i|
    if a[i] > n
       a[i] = c.shift
    elsif b[a[i]] > 1
       b[a[i]] -= 1
       a[i] = c.shift
    end
end

print a.shift
a.each do |e| print(" #{e}") end
print "\n"

#n = STDIN.gets.chomp
#input = STDIN.gets.chomp

def remap(n, input)
  order = []
  n.to_i.times {|n| order << n+1}

  input = input.split.map(&:to_i)
  output = []

  reworked_input = []

  input.each do |key|
    unless order.include? key
      reworked_input << [key, false]
    else
      index = order.index key
      order[index] = nil
      reworked_input << [key, true]
    end
  end

  order = order.compact
  counter = 0

  reworked_input.each do |element|
    key, value = element
    if value == false
      output << order[counter]
      counter += 1
    else
      output << key
    end
  end

  return output.join(" ")
end

#puts remap(n, input)

#!/usr/bin/ruby
N=gets.to_i
A=[]
h={}
gets.split.each_with_index{|e,i|
	n=e.to_i-1
	if !A[n]&&n<N
		A[n]=i
		h[i]=n
	end
}
x=0
N.times{|i|
	if !A[i]
		x+=1 while h[x]
		A[i]=x
		h[x]=i
	end
}
puts A[0,N].map{|e|e+1}*' '

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

used = {}
nums.each do |n|
	used[n] = true
end

f = 0
free = Array.new(nums.size)
(1..nums.size).each do |n|
	next if used[n]

	free[f] = n
	f += 1
end

f = 0
res = Array.new(nums.size)
used = {}
nums.each_with_index do |n, i|
	if used[n] || n >= nums.size
		res[i] = free[f]
		f += 1
	else
		res[i] = n
		used[n] = true
	end
end

puts res.join(' ')

#!/usr/bin/ruby

n = ARGF.gets.to_i
a = ARGF.gets.split(' ').map { |s| s.to_i }

b = Array.new(n + 1, 0)
b[0] = 1

a.each do |e|
    if e < n then b[e] += 1 end
end

c = Array.new
b.each_with_index do |e, i|
    if b[i] == 0 then c << i end
end

a.each_with_index do |e, i|
    if a[i] > n
       a[i] = c.shift
    elsif b[a[i]] > 1
       b[a[i]] -= 1
       a[i] = c.shift
    end
end

print a.shift
a.each do |e| print(" #{e}") end
print "\n"


Anton likes to play chess. Also, he likes to do programming. That is why he decided to write the program that plays chess. However, he finds the game on 8 to 8 board to too simple, he uses an infinite one instead.The first task he faced is to check whether the king is in check. Anton doesn't know how to implement this so he asks you to help.Consider that an infinite chess board contains one white king and the number of black pieces. There are only rooks, bishops and queens, as the other pieces are not supported yet. The white king is said to be in check if at least one black piece can reach the cell with the king in one move. Help Anton and write the program that for the given position determines whether the white king is in check.Remainder, on how do chess pieces move:   Bishop moves any number of cells diagonally, but it can't "leap" over the occupied cells.  Rook moves any number of cells horizontally or vertically, but it also can't "leap" over the occupied cells.  Queen is able to move any number of cells horizontally, vertically or diagonally, but it also can't "leap".

The first line of the input contains a single integer n (1 ≤ n ≤ 500 000) — the number of black pieces. The second line contains two integers x0 and y0 ( - 109 ≤ x0, y0 ≤ 109) — coordinates of the white king. Then follow n lines, each of them contains a character and two integers xi and yi ( - 109 ≤ xi, yi ≤ 109) — type of the i-th piece and its position. Character 'B' stands for the bishop, 'R' for the rook and 'Q' for the queen. It's guaranteed that no two pieces occupy the same position.

The only line of the output should contains "YES" (without quotes) if the white king is in check and "NO" (without quotes) otherwise.

NotePicture for the first sample:    White king is in check, because the black bishop can reach the cell with the white king in one move. The answer is "YES".Picture for the second sample:    Here bishop can't reach the cell with the white king, because his path is blocked by the rook, and the bishop cant "leap" over it. Rook can't reach the white king, because it can't move diagonally. Hence, the king is not in check and the answer is "NO".

def r
  gets.split.map(&:to_i)
end
def rook(p)
  "RQ".match(p[1])
end
def bish(p)
  "BQ".match(p[1])
end
gets
kx, ky = r

rl = cl = d1l = d2l = [-1e10, "dummy"]
rh = ch = d1h = d2h = [+1e10, "dummy"]

$<.each_line do |l|
  op, x, y = l.split
  x = x.to_i
  y = y.to_i

  rl = [rl, [y, op]].max if x == kx && y < ky
  rh = [rh, [y, op]].min if x == kx && y > ky
  cl = [cl, [x, op]].max if y == ky && x < kx
  ch = [ch, [x, op]].min if y == ky && x > kx

  d1l = [d1l, [x, op]].max if x+y == kx+ky && x < kx
  d1h = [d1h, [x, op]].min if x+y == kx+ky && x > kx
  d2l = [d2l, [x, op]].max if x-y == kx-ky && x < kx
  d2h = [d2h, [x, op]].min if x-y == kx-ky && x > kx
end
puts [rl, rh, cl, ch].any?{|x| rook(x)} || [d1l, d1h, d2l, d2h].any?{|x| bish(x)} ? "YES" : "NO"

def r
  gets.split.map(&:to_i)
end
def rook(p)
  "RQ".match(p[1])
end
def bish(p)
  "BQ".match(p[1])
end
gets
kx, ky = r

rl = cl = d1l = d2l = [-1e10, "dummy"]
rh = ch = d1h = d2h = [+1e10, "dummy"]

$<.each_line do |l|
  op, x, y = l.split
  x = x.to_i
  y = y.to_i

  if 1
    rl = [rl, [y, op]].max if x == kx && y < ky
  end
  if 1
  rh = [rh, [y, op]].min if x == kx && y > ky
  end
  if 1
  cl = [cl, [x, op]].max if y == ky && x < kx
  end
  if 1
  ch = [ch, [x, op]].min if y == ky && x > kx
  end

  d1l = [d1l, [x, op]].max if x+y == kx+ky && x < kx
  d1h = [d1h, [x, op]].min if x+y == kx+ky && x > kx
  d2l = [d2l, [x, op]].max if x-y == kx-ky && x < kx
  d2h = [d2h, [x, op]].min if x-y == kx-ky && x > kx
end
puts [rl, rh, cl, ch].any?{|x| rook(x)} || [d1l, d1h, d2l, d2h].any?{|x| bish(x)} ? "YES" : "NO"

vert = []
hori = []
psdiag = []
nsdiag = []

n = gets.to_i
kx, ky = gets.strip.split.map(&:to_i)

pieces = []
n.times do |i|
  p, x, y = gets.strip.split
  x = x.to_i
  y = y.to_i
  pieces << [p, x, y]
  vert << i if x == kx
  hori << i if y == ky
  psdiag << i if (y-x) == (ky-kx)
  nsdiag << i if (y+x) == (ky+kx)
end

under_attack = false

near_top = [10**10, nil]
near_bot = [-1 * (10**10), nil]
vert.each do |pi|
  p = pieces[pi]
  if p[2] > ky && p[2] < near_top[0]
    near_top[0] = p[2]
    near_top[1] = pi
  end
  if p[2] < ky && p[2] > near_bot[0]
    near_bot[0] = p[2]
    near_bot[1] = pi
  end
end
under_attack ||= pieces[near_top[1]][0] == "R" || pieces[near_top[1]][0] == "Q" if near_top[1]
under_attack ||= pieces[near_bot[1]][0] == "R" || pieces[near_bot[1]][0] == "Q" if near_bot[1]

if under_attack == false
  near_rgt = [10**10, nil]
  near_lft = [-1 * (10**10), nil]
  hori.each do |pi|
    p = pieces[pi]
    if p[1] > kx && p[1] < near_rgt[0]
      near_rgt[0] = p[1]
      near_rgt[1] = pi
    end
    if p[1] < kx && p[1] > near_lft[0]
      near_lft[0] = p[1]
      near_lft[1] = pi
    end
  end
  under_attack ||= pieces[near_rgt[1]][0] == "R" || pieces[near_rgt[1]][0] == "Q" if near_rgt[1]
  under_attack ||= pieces[near_lft[1]][0] == "R" || pieces[near_lft[1]][0] == "Q" if near_lft[1]
end

if under_attack == false
  near_tr = [10**10, nil]
  near_bl = [-1 * (10**10), nil]
  psdiag.each do |pi|
    p = pieces[pi]
    if p[2] > ky && p[2] < near_tr[0]
      near_tr[0] = p[2]
      near_tr[1] = pi
    end
    if p[2] < ky && p[2] > near_bl[0]
      near_bl[0] = p[2]
      near_bl[1] = pi
    end
  end
  under_attack ||= pieces[near_tr[1]][0] == "B" || pieces[near_tr[1]][0] == "Q" if near_tr[1]
  under_attack ||= pieces[near_bl[1]][0] == "B" || pieces[near_bl[1]][0] == "Q" if near_bl[1]
end

if under_attack == false
  near_br = [10**10, nil]
  near_bl = [-1 * (10**10), nil]
  nsdiag.each do |pi|
    p = pieces[pi]
    if p[1] > kx && p[1] < near_br[0]
      near_br[0] = p[1]
      near_br[1] = pi
    end
    if p[1] < kx && p[1] > near_bl[0]
      near_bl[0] = p[1]
      near_bl[1] = pi
    end
  end
  under_attack ||= pieces[near_br[1]][0] == "B" || pieces[near_br[1]][0] == "Q" if near_br[1]
  under_attack ||= pieces[near_bl[1]][0] == "B" || pieces[near_bl[1]][0] == "Q" if near_bl[1]
end

puts (under_attack ? "YES" : "NO")

# Try Codeforces
# author: Leonardone @ NEETSDKASU
############################################################
def gs() gets.strip end
def gi() gets.to_i end
def gss() gs.split end
def gis() gss.map(&:to_i) end
def nmapf(n,f) n.times.map{ __send__ f } end
def ngs(n) nmapf n,:gs end
def ngi(n) nmapf n,:gi end
def ngss(n) nmapf n,:gss end
def ngis(n) nmapf n,:gis end
def arr2d(h,w,v=0) h.times.map{[v] * w} end
def for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end
def nsum(n) n * (n + 1) / 2 end
def vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end
############################################################

n = gi
x, y = gis

ps = [nil] * 8

n.times do
    c, xi_s, yi_s = gss
    xi, yi = xi_s.to_i, yi_s.to_i
    dx = xi - x
    dy = yi - y
    d = dx.abs + dy.abs
    if x == xi
        if y < yi && (ps[6].nil? || d < ps[6][1])
            ps[6] = [c, d]
        elsif y > yi && (ps[1].nil? || d < ps[1][1])
            ps[1] = [c, d]
        end
    elsif y == yi
        if x < xi && (ps[4].nil? || d < ps[4][1])
            ps[4] = [c, d]
        elsif x > xi && (ps[3].nil? || d < ps[3][1])
            ps[3] = [c, d]
        end
    elsif dx.abs == dy.abs
        if dy < 0 && dx < 0 && (ps[0].nil? || d < ps[0][1])
            ps[0] = [c, d]
        elsif dy < 0 && dx > 0 && (ps[2].nil? || d < ps[2][1])
            ps[2] = [c, d]
        elsif dy > 0 && dx < 0 && (ps[5].nil? || d < ps[5][1])
            ps[5] = [c, d]
        elsif dy > 0 && dx > 0 && (ps[7].nil? || d < ps[7][1])
            ps[7] = [c, d]
        end
    end
end

case
when !ps[1].nil? && ps[1][0] != 'B'
    puts :YES
when !ps[3].nil? && ps[3][0] != 'B'
    puts :YES
when !ps[4].nil? && ps[4][0] != 'B'
    puts :YES
when !ps[6].nil? && ps[6][0] != 'B'
    puts :YES
when !ps[0].nil? && ps[0][0] != 'R'
    puts :YES
when !ps[2].nil? && ps[2][0] != 'R'
    puts :YES
when !ps[5].nil? && ps[5][0] != 'R'
    puts :YES
when !ps[7].nil? && ps[7][0] != 'R'
    puts :YES
else
    puts :NO
end

def r
  gets.split.map(&:to_i)
end
def rook(p)
  "RQ".match(p[1])
end
def bish(p)
  "BQ".match(p[1])
end
gets
kx, ky = r

rl = cl = d1l = d2l = [-1e10, "dummy"]
rh = ch = d1h = d2h = [+1e10, "dummy"]

$<.each_line do |l|
  op, x, y = l.split
  x = x.to_i
  y = y.to_i

  if x == kx
    rl = [rl, [y, op]].max if y < ky
    rh = [rh, [y, op]].min if y > ky
  end
  if y == ky
    cl = [cl, [x, op]].max if x < kx
    ch = [ch, [x, op]].min if x > kx
  end
  if x+y == kx+ky
    d1l = [d1l, [x, op]].max if x < kx
    d1h = [d1h, [x, op]].min if x > kx
  end
  if x-y == kx-ky
    d2l = [d2l, [x, op]].max if x < kx
    d2h = [d2h, [x, op]].min if x > kx
  end
end
puts [rl, rh, cl, ch].any?{|x| rook(x)} || [d1l, d1h, d2l, d2h].any?{|x| bish(x)} ? "YES" : "NO"

def r
  gets.split.map(&:to_i)
end
def rook(o)
  "RQ".match(o)
end
def bish(o)
  "BQ".match(o)
end
gets
kx, ky = r

rl = cl = d1l = d2l = [-1e10, "dummy"]
rh = ch = d1h = d2h = [+1e10, "dummy"]

$<.each_line do |l|
  op, x, y = l.split
  x = x.to_i
  y = y.to_i

  if x == kx
    rl = [rl, [y, op]].max if y < ky
    rh = [rh, [y, op]].min if y > ky
  end
  if y == ky
    cl = [cl, [x, op]].max if x < kx
    ch = [ch, [x, op]].min if x > kx
  end
  if x+y == kx+ky
    d1l = [d1l, [x, op]].max if x < kx
    d1h = [d1h, [x, op]].min if x > kx
  end
  if x-y == kx-ky
    d2l = [d2l, [x, op]].max if x < kx
    d2h = [d2h, [x, op]].min if x > kx
  end
end
puts [rl, rh, cl, ch].any?{|x,o| rook(o)} || [d1l, d1h, d2l, d2h].any?{|x,o| bish(o)} ? "YES" : "NO"


Today Sonya learned about long integers and invited all her friends to share the fun. Sonya has an initially empty multiset with integers. Friends give her t queries, each of one of the following type:   +  ai — add non-negative integer ai to the multiset. Note, that she has a multiset, thus there may be many occurrences of the same integer.   -  ai — delete a single occurrence of non-negative integer ai from the multiset. It's guaranteed, that there is at least one ai in the multiset.  ? s — count the number of integers in the multiset (with repetitions) that match some pattern s consisting of 0 and 1. In the pattern, 0 stands for the even digits, while 1 stands for the odd. Integer x matches the pattern s, if the parity of the i-th from the right digit in decimal notation matches the i-th from the right digit of the pattern. If the pattern is shorter than this integer, it's supplemented with 0-s from the left. Similarly, if the integer is shorter than the pattern its decimal notation is supplemented with the 0-s from the left. For example, if the pattern is s = 010, than integers 92, 2212, 50 and 414 match the pattern, while integers 3, 110, 25 and 1030 do not.

The first line of the input contains an integer t (1 ≤ t ≤ 100 000) — the number of operation Sonya has to perform. Next t lines provide the descriptions of the queries in order they appear in the input file. The i-th row starts with a character ci — the type of the corresponding operation. If ci is equal to '+' or '-' then it's followed by a space and an integer ai (0 ≤ ai < 1018) given without leading zeroes (unless it's 0). If ci equals '?' then it's followed by a space and a sequence of zeroes and onse, giving the pattern of length no more than 18. It's guaranteed that there will be at least one query of type '?'. It's guaranteed that any time some integer is removed from the multiset, there will be at least one occurrence of this integer in it.

For each query of the third type print the number of integers matching the given pattern. Each integer is counted as many times, as it appears in the multiset at this moment of time.

NoteConsider the integers matching the patterns from the queries of the third type. Queries are numbered in the order they appear in the input.   1 and 241.  361.  101 and 361.  361.  4000.

n = gets.to_i
h = Hash.new(0)
n.times do |i|
  x, num = gets.split(" ")
  a =  num.tr('0-9', '0101010101').to_i
  if x.ord == 43
    h[a] += 1
  elsif x.ord == 45
    h[a] -= 1
  elsif x.ord == 63
    puts h[a]
  end
end

n = gets.to_i
cnt = Array.new(2 ** 18, 0)
n.times do
  c, s = gets.split
  a = s.tr('0-9', '0101010101').to_i(2)
  case c
  when '+'
    cnt[a] += 1
  when '-'
    cnt[a] -= 1
  when '?'
    puts cnt[a]
  end
end

n = gets.to_i
h = Hash.new(0)
n.times.map do |i|
  x, s = gets.split
  s = s.tr('13579', '1').tr('24680', '0')
  case x
  when '+'
    h[s.rjust(18, '0')] += 1
  when '-'
    h[s.rjust(18, '0')] -= 1
  when '?'
    puts h[s.rjust(18, '0')]
  end
end

n = gets.to_i
h = Hash.new(0)
n.times.map do |i|
  x, s = gets.split
  a = s.tr('0-9', '0101010101').to_i
  case x
  when '+'
    h[a] += 1
  when '-'
    h[a] -= 1
  when '?'
    puts h[a]
  end
end

n = readline.to_i
h = {}
h.default = 0
(1..n).each do
  q_type, q_key = readline.split.map(&:strip)

  t=0
  (0...q_key.size).each do |i|
    t *= 2
    t += q_key[i].ord % 2
  end

  if q_type == '+'
    h[t] += 1
  elsif q_type == '-'
    h[t] -= 1
  else
    puts h[t]
  end
end

n = gets.to_i
h = Hash.new(0)
n.times.map do |i|
  x, s = gets.split
  s = s.tr('13579', '1').tr('24680', '1')
  case x
  when '+'
    h[s.rjust(18, '0')] += 1
  when '-'
    h[s.rjust(18, '0')] -= 1
  when '?'
    puts h[s.rjust(18, '0')]
  end
end

n = gets.to_i
h = Hash.new(0)
n.times do |x|
  c, n = gets.split
  case c
  when '+'
    h[n] += 1
  when '-'
    h[n] -= 1
  when '?'
    a = n.chars.map do |x|
      if x == '1'
        "[1|3|5|7]"
      elsif x == '0'
        "[2|4|6|8|0]"
      end
    end.join
    a << '$'
    puts h.keys.select{|x| x.rjust(5, '0').match(Regexp.new(a))}.inject(0){|s, x| s + h[x]}
  end
end

n = gets.to_i
h = Hash.new(0)
n.times do |x|
  c, n = gets.split
  case c
  when '+'
    h[n] += 1
  when '-'
    h[n] -= 1
  when '?'
    a = n.chars.map{|x| x == '1' ? '[1|3|5|7|9]' : '[2|4|6|8|0]'}.join
    a << '$'
    puts h.keys.select{|x| x.rjust(18, '0').match(Regexp.new(a))}.inject(0){|s, x| s + h[x]}
  end
end


New Year is coming! Vasya has prepared a New Year's verse and wants to recite it in front of Santa Claus.Vasya's verse contains $$$n$$$ parts. It takes $$$a_i$$$ seconds to recite the $$$i$$$-th part. Vasya can't change the order of parts in the verse: firstly he recites the part which takes $$$a_1$$$ seconds, secondly — the part which takes $$$a_2$$$ seconds, and so on. After reciting the verse, Vasya will get the number of presents equal to the number of parts he fully recited.Vasya can skip at most one part of the verse while reciting it (if he skips more than one part, then Santa will definitely notice it).Santa will listen to Vasya's verse for no more than $$$s$$$ seconds. For example, if $$$s = 10$$$, $$$a = [100, 9, 1, 1]$$$, and Vasya skips the first part of verse, then he gets two presents.Note that it is possible to recite the whole verse (if there is enough time). Determine which part Vasya needs to skip to obtain the maximum possible number of gifts. If Vasya shouldn't skip anything, print 0. If there are multiple answers, print any of them.You have to process $$$t$$$ test cases.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^5, 1 \le s \le 10^9$$$) — the number of parts in the verse and the maximum number of seconds Santa will listen to Vasya, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the time it takes to recite each part of the verse. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case print one integer — the number of the part that Vasya needs to skip to obtain the maximum number of gifts. If Vasya shouldn't skip any parts, print 0.

NoteIn the first test case if Vasya skips the second part then he gets three gifts.In the second test case no matter what part of the verse Vasya skips.In the third test case Vasya can recite the whole verse.

def solve(n, s, vals)
  sum = 0
  max_so_far = vals[0]
  vals.each_with_index do |e, i|
    max_so_far = [max_so_far, e].max
    sum += e
    if sum > s
      return vals.index { |x| x == max_so_far } + 1
    end
  end
  0
end

# gets.strip.split(' ').map(&:to_i)
gets.strip.to_i.times do
  n, s = gets.strip.split(' ').map(&:to_i)
  vals = gets.strip.split(' ').map(&:to_i)
  puts solve(n, s, vals)
end

gets.strip.to_i.times {
    n, s = gets.strip.split(' ').map(&:to_i)
    a = gets.strip.split(' ').map(&:to_i)
    mx = -1
    sum = 0
    done = false
    $part
    a.each_with_index { |e, i|
        if mx <= e
            mx = e
            $part = i + 1
        end
        sum += e
        if sum > s
            puts $part
            done = true
            break
        end
    }
    puts 0 if !done
}


It is the easy version of the problem. The only difference is that in this version $$$k = 3$$$.You are given a positive integer $$$n$$$. Find $$$k$$$ positive integers $$$a_1, a_2, \ldots, a_k$$$, such that:  $$$a_1 + a_2 + \ldots + a_k = n$$$  $$$LCM(a_1, a_2, \ldots, a_k) \le \frac{n}{2}$$$ Here $$$LCM$$$ is the least common multiple of numbers $$$a_1, a_2, \ldots, a_k$$$.We can show that for given constraints the answer always exists.

The first line contains a single integer $$$t$$$ $$$(1 \le t \le 10^4)$$$  — the number of test cases. The only line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$3 \le n \le 10^9$$$, $$$k = 3$$$).

For each test case print $$$k$$$ positive integers $$$a_1, a_2, \ldots, a_k$$$, for which all conditions are satisfied.

def solve(n, k)
    a = n / 4
    case n % 4
    when 0
        [a*2, a, a]
    when 1
        [a*2, a*2, 1]
    when 2
        [a*2, a*2, 2]
    when 3
        [a*2+1, a*2+1, 1]
    end
end

def main
    t = int
    t.times do
        n, k = ints
        a, b, c = solve(n, k)
        raise "not satisfied: #{a} + #{b} + #{c} == #{n}" unless a + b + c == n
        raise "not satisfied: LCM(#{a}, #{b}, #{c}) <= #{n}/2" unless 2 * a.lcm(b).lcm(c) <= n
        puts "#{a} #{b} #{c}"
    end
end

DEBUG = true
MOD = 10**9+7
YESNO = %w(No Yes)
INF = 10**9

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

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

class TrueClass
include Boolean
def to_i; 1 end
end

class FalseClass
include Boolean
def to_i; 0 end
end

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

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

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

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

main

gets.to_i.times {
  n, k = gets.split(' ').map(&:to_i)
  if n % 4 == 0
    a = n / 2
    b = a / 2
    puts [a, b, b].join(' ')
  else
    a = (n - 1) / 2
    rem = n - 2 * a
    while a > 0
      if a % rem == 0
        puts [a, a, rem].join(' ')
        break
      end
      a -= 1
      rem += 2
    end
  end
}

def fn(n,k)
  m = n / 2
  if n % 2  == 1
    x = (n - 1)/2
    ret = [x,x, 1]
  else
     if m % 2 == 0
       ret = [m,m/2,m/2]
     else
      ret = [2, m-1, m-1]
     end
  end

  puts ret.join(' ')
end

tt = gets.to_i
tt.times {
  (n, k)= gets.chomp!.split(' ').map &:to_i
  fn(n,k)
}


You are playing a very popular game called Cubecraft. Initially, you have one stick and want to craft $$$k$$$ torches. One torch can be crafted using one stick and one coal.Hopefully, you've met a very handsome wandering trader who has two trade offers:  exchange $$$1$$$ stick for $$$x$$$ sticks (you lose $$$1$$$ stick and gain $$$x$$$ sticks).  exchange $$$y$$$ sticks for $$$1$$$ coal (you lose $$$y$$$ sticks and gain $$$1$$$ coal). During one trade, you can use only one of these two trade offers. You can use each trade offer any number of times you want to, in any order.Your task is to find the minimum number of trades you need to craft at least $$$k$$$ torches. The answer always exists under the given constraints.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains three integers $$$x$$$, $$$y$$$ and $$$k$$$ ($$$2 \le x \le 10^9$$$; $$$1 \le y, k \le 10^9$$$) — the number of sticks you can buy with one stick, the number of sticks required to buy one coal and the number of torches you need, respectively.

For each test case, print the answer: the minimum number of trades you need to craft at least $$$k$$$ torches. The answer always exists under the given constraints.

gets.to_i.times do
  a, b, k = gets.strip.split(" ").map(&:to_i)
  cn = k
  csn = cn*b
  tsn = csn + k
  dsn = tsn - 1
  iop = dsn / (a-1)
  iop += 1 if (dsn % (a-1)) > 0
  puts iop + cn
end

T = gets.to_i

T.times do
  x, y, k = gets.split.map(&:to_i)

  ok = Rational(k * (1 + y) - 1, x - 1).ceil

  puts ok + k
end

t = gets.chomp.to_i
need = 0
kaisuu = 0
t.times do
    x, y, k = gets.chomp.split(" ").map(&:to_i)
    need = k + y * k #必要(松)
    sa = x - 1
    kaisuu = (need - 1) / sa
    amari = (need - 1) % sa
    if amari != 0
        kaisuu += 1
    end
    sekitan = (need - k)/ y
     puts kaisuu +  sekitan
end

gets.to_i.times do
  x, y, k = gets.split.map &:to_i
  puts ((y + 1) * k + x - 3) / (x - 1) + k
end


A progress bar is an element of graphical interface that displays the progress of a process for this very moment before it is completed. Let's take a look at the following form of such a bar.A bar is represented as n squares, located in line. To add clarity, let's number them with positive integers from 1 to n from the left to the right. Each square has saturation (ai for the i-th square), which is measured by an integer from 0 to k. When the bar for some i (1 ≤ i ≤ n) is displayed, squares 1, 2, ... , i - 1 has the saturation k, squares i + 1, i + 2, ... , n has the saturation 0, and the saturation of the square i can have any value from 0 to k.So some first squares of the progress bar always have the saturation k. Some last squares always have the saturation 0. And there is no more than one square that has the saturation different from 0 and k.The degree of the process's completion is measured in percents. Let the process be t% completed. Then the following inequation is fulfilled: An example of such a bar can be seen on the picture.  For the given n, k, t determine the measures of saturation for all the squares ai of the progress bar.

We are given 3 space-separated integers n, k, t (1 ≤ n, k ≤ 100, 0 ≤ t ≤ 100).

Print n numbers. The i-th of them should be equal to ai.

#!/usr/bin/env ruby

n, k, t = gets.chomp.split(/\s/).map{|s| s.to_i}
ret = []

max = n * k
value = (max * (t / 100.0)).to_i
(value / k).times do
  ret << k
end

ret << value % k

(n - (value.to_i / k) - 1).times do
  ret << 0
end

puts ret.slice(0, n).join(' ')

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

val = Rational(100, n*k)

s = 0
s += 1 while (s+1)*val <= t
o = [k] * (s/k)
o << s%k if s%k > 0
o = (o + [0]*n)[0,n]
puts o*" "

#!/usr/bin/ruby
n,k,t=gets.chop.split(" ").map{|i|i.to_i}
x=n*t/100
y=t*n*k/100-x*k
n.times{|i|if i<x then print k.to_s+" " elsif i==x then print y.to_s+" " else print 0.to_s; if i<n-1 then print " " end; end}

n, k, t = gets.split(' ').map &:to_i
p, bar = n * k * t / 100, []

n.times do |i|
	bar.push [p, k].min
	p -= bar.last
end

puts bar.join " "

n, k, t = gets.split.map(&:to_i)
s = n * k * t / 100
a = []
n.times do
	a << [s, k].min
	s -= a[-1]
end
puts a * ' '

input = gets.split.map(&:to_i)
l = (input[2] * input[0]*input[1]/100.0).floor
full = l/input[1]
full.times {print input[1], ' '}
if full != input[0]
    print l % input[1]
    (input[0] - full - 1).times {print ' ', 0}
end

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

n=input[0]
m=input[1]
t=input[2]

res=0
0.upto(n*m){|i|
    if i*100<=n*m*t then
        res=i
    end
}
for i in 1..n
    if m<=res then
        printf "%d ",m
        res-=m
    else
        printf "%d ",res
        res-=res
    end
end
puts

n,k,t = gets.split.map(&:to_i)
all = n*k
val = (t*n*k/100.0).to_i
arr = [0] * n
n.times do |i|
	arr[i] = [k, val].min
	val = [val-k, 0].max
end
puts arr.join(' ')

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

points = (input[2]*input[0]*input[1]) / 100

counter = 0
for counter in 1..(points/input[1])
  if counter != 1
    print ' '
  end
  print input[1].to_s
end

balance = points - (points/input[1]) * input[1]

if balance > 0
  print ' ' + balance.to_s
  counter += 1
end

for fcounter in 1 .. input[0]-counter
  print ' ' + 0.to_s
end

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

#n, k, t = 11, 13, 37

progress = (n.to_f * k / (100 / t.to_f)).truncate
nulls    = (n.to_f * k - progress).truncate

arr = []
while progress > 0 do
  if progress >= k then
    arr << k
    progress -= k
  else
    arr << progress
    progress = 0
  end
end

arr << 0 while arr.length < n

puts arr

input = STDIN.gets
n,k,t = input.strip.split(' ').map{|w| w.to_i}
a = Array.new(n).fill(0)
saturated = (t/(100.0/n)).to_i
sum_saturated = saturated * k
last_saturation = (t * n * k / 100) - sum_saturated
saturated.times do |i|
  a[i] = k.to_i
end
if saturated >= n
  print a.join(' ')
else
  a[saturated] = last_saturation.to_i
  print a.join(' ')
end

input = STDIN.gets
n,k,t = input.strip.split(' ').map{|w| w.to_i}
a = Array.new(n).fill(0)
if(t <= k)
  a[0] = t
  print a.join(' ')
else
  saturated = t / (100/n)
  sum_saturated = saturated * k
  last_saturation = (t * n * k / 100) - sum_saturated
  saturated.times do |i|
    a[i] = k.to_i
  end
  if saturated >= n
    print a.join(' ')
  else
    a[saturated] = last_saturation.to_i
    print a.join(' ')
  end
end

input = STDIN.gets
n,k,t = input.strip.split(' ').map{|w| w.to_i}
a = Array.new(n).fill(0)
#if(t <= k)
#  a[0] = t
#  print a.join(' ')
#else
  p = t*n/100
  q = (t - (p*(100.0/n))).to_i
  p.times do |i|
    a[i] = k
  end
  if(q >= (100.0/n.to_f/k.to_f))
    a[p] = q * n * k / 100
  end
  print a.join(' ')
  #saturated = t / (100/n)
  #sum_saturated = saturated * k
  #last_saturation = (t * n * k / 100) - sum_saturated
  #saturated.times do |i|
  #  a[i] = k.to_i
  #end
  #if saturated >= n
  #  print a.join(' ')
  #else
  #  a[saturated] = last_saturation.to_i
  #  print a.join(' ')
  #end
#end

#!/usr/bin/env ruby

n, k, t = gets.chomp.split(/\s/).map{|s| s.to_i}
ret = []

max = n * k
value = (max * (t / 100.0)).to_i
(value / k).times do
  ret << k
end

ret << value % k

(n - (value.to_i / k) - 1).times do
  p :hoge
  ret << 0
end

puts ret.slice(0, n).join(' ')

#!/usr/bin/env ruby

n, k, t = gets.chomp.split(/\s/).map{|s| s.to_i}
ret = []

max = n * k
value = (max * (t / 100.0)).to_i
(value / k).times do
  ret << k
end

ret << value % k

(n - (value.to_i / k) - 1).times do
  ret << 0
end

puts ret.join(' ')

n,k,t=gets.chop.split(" ").map{|i|i.to_i}
x,y=t.divmod(k)
n.times{|i|if i<x then print k.to_s+" " elsif i==x then print y.to_s+" " else print 0.to_s+" " end}
puts

n,k,t=gets.chop.split(" ").map{|i|i.to_i}
x,y=t.divmod(k)
n.times{|i|if i<x then print k.to_s+" " elsif i==x then print y.to_s+" " else print 0.to_s; if i<n-1 then print " " end; end}

#!/usr/bin/ruby
n,k,t=gets.chop.split(" ").map{|i|i.to_i}
x=n*t/100
y=t.modulo(k)
n.times{|i|if i<x then print k.to_s+" " elsif i==x then print y.to_s+" " else print 0.to_s; if i<n-1 then print " " end; end}

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

n=input[0]
m=input[1]
t=input[2]

res=0
0.upto(n*m){|i|
	if i*100<=n*m*t then
		res=i
	end
}
for i in 0..n
	if m<=res then
		printf "%d ",m
		res-=m
	else
		printf "%d ",res
		res-=res
	end
end
puts

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

points = (input[2]*input[0]*input[1]) / 100

for counter in 1..(points/input[1])
  print input[1].to_s + ' '
end

print points - (points/input[1]) * input[1]

for fcounter in (points/input[1])+2 .. input[0]
  print ' ' + 0.to_s
end

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

points = (input[2]*input[0]*input[1]) / 100

for counter in 1..(points/input[1])
  print input[1].to_s + ' '
end

balance = points - (points/input[1]) * input[1]

print balance if balance > 0

for fcounter in (points/input[1])+1 .. input[0]
  print ' ' + 0.to_s
end

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

points = (input[2]*input[0]*input[1]) / 100

for counter in 1..(points/input[1])
  print input[1].to_s + ' '
end

balance = points - (points/input[1]) * input[1]

print balance if balance > 0

for fcounter in (points/input[1])+2 .. input[0]
  print ' ' + 0.to_s
end

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

#n, k, t = 10, 10, 54

progress = (n.to_f * k / (100 / t.to_f)).truncate

arr = []
while progress > 0 do
  if progress >= k then
    arr << k
    progress -= k
  else
    arr << progress
    progress = 0
  end
end

puts arr

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

#n, k, t = 11, 13, 37

progress = (n.to_f * k / (100 / t.to_f)).truncate
nulls    = n.to_f * k - progress

arr = []
while progress > 0 do
  if progress >= k then
    arr << k
    progress -= k
  else
    arr << progress
    progress = 0
  end
end

begin arr << 0; nulls -= k end while nulls > k

puts arr

input = STDIN.gets
n,k,t = input.strip.split(' ').map{|w| w.to_i}
a = Array.new(n).fill(0)
if(t <= k)
  a[0] = t
  print a.join(' ')
else
  p = t * n / 100
  q = (t - (p*(100.0/n))).to_i
  p.times do |i|
    a[i] = k
  end
  if(q > 0)
    a[p] = q * n * k / 100
  end
  print a.join(' ')
  #saturated = t / (100/n)
  #sum_saturated = saturated * k
  #last_saturation = (t * n * k / 100) - sum_saturated
  #saturated.times do |i|
  #  a[i] = k.to_i
  #end
  #if saturated >= n
  #  print a.join(' ')
  #else
  #  a[saturated] = last_saturation.to_i
  #  print a.join(' ')
  #end
end

input = STDIN.gets
n,k,t = input.strip.split(' ').map{|w| w.to_i}
a = Array.new(n).fill(0)
saturated = t / (100/n)
sum_saturated = saturated * k
last_saturation = (t * n * k / 100) - sum_saturated
saturated.times do |i|
  a[i] = k
end
if saturated >= n
  print a.join(' ')
else
  a[saturated] = last_saturation
  if(saturated + 1 < n)
    ((saturated + 1)..n).each do |i|
      a[i] = 0
    end
  end
  print a.join(' ')
end

input = STDIN.gets
n,k,t = input.strip.split(' ').map{|w| w.to_i}
a = Array.new(n).fill(0)
if(t <= k)
  a[0] = t
  print a.join(' ')
else
  saturated = t / (100/n)
  sum_saturated = saturated * k
  last_saturation = t * n * k / 100 - sum_saturated
  saturated.times do |i|
    a[i] = k.to_i
  end
  if saturated >= n
    print a.join(' ')
  else
    a[saturated] = last_saturation.to_i
    if(saturated + 1 < n)
      ((saturated + 1)..n-1).each do |i|
        a[i] = 0
      end
    end
    print a.join(' ')
  end
end

input = STDIN.gets
n,k,t = input.strip.split(' ').map{|w| w.to_i}
a = Array.new(n).fill(0)
if(t <= k)
  a[0] = t
  print a.join(' ')
else
  saturated = t / (100/n)
  sum_saturated = saturated * k
  last_saturation = t * n * k / 100 - sum_saturated
  saturated.times do |i|
    a[i] = k.to_i
  end
  if saturated >= n
    print a.join(' ')
  else
    a[saturated] = last_saturation.to_i
    print a.join(' ')
  end
end

input = STDIN.gets
n,k,t = input.strip.split(' ').map{|w| w.to_i}
a = Array.new(n).fill(0)
if(t <= k)
  a[0] = t
  print a.join(' ')
else
  p = t*n/100
  q = (t - (p*(100.0/n))).to_i
  p.times do |i|
    a[i] = k
  end
  if(q >= (100.0/n.to_f/k.to_f))
    a[p] = q * n * k / 100
  end
  print a.join(' ')
  #saturated = t / (100/n)
  #sum_saturated = saturated * k
  #last_saturation = (t * n * k / 100) - sum_saturated
  #saturated.times do |i|
  #  a[i] = k.to_i
  #end
  #if saturated >= n
  #  print a.join(' ')
  #else
  #  a[saturated] = last_saturation.to_i
  #  print a.join(' ')
  #end
end

input = STDIN.gets
n,k,t = input.strip.split(' ').map{|w| w.to_i}
a = Array.new(n).fill(0)
if(t <= k)
  a[0] = k
  print a.join(' ')
else
  saturated = t / (100/n)
  sum_saturated = saturated * k
  last_saturation = t * n * k / 100 - sum_saturated
  saturated.times do |i|
    a[i] = k.to_i
  end
  if saturated >= n
    print a.join(' ')
  else
    a[saturated] = last_saturation.to_i
    if(saturated + 1 < n)
      ((saturated + 1)..n-1).each do |i|
        a[i] = 0
      end
    end
    print a.join(' ')
  end
end

input = STDIN.gets
n,k,t = input.strip.split(' ').map{|w| w.to_i}
a = Array.new(n).fill(0)
#if(t <= k)
#  a[0] = t
#  print a.join(' ')
#else
  #p = t*n/100
  #q = (t - (p*(100.0/n))).to_i
  #p.times do |i|
  #  a[i] = k
  #end
  #if(q >= (100.0/n.to_f/k.to_f))
  #  a[p] = q * n * k / 100
  #end
  #print a.join(' ')
  saturated = t / (100/n)
  sum_saturated = saturated * k
  last_saturation = (t * n * k / 100) - sum_saturated
  saturated.times do |i|
    a[i] = k.to_i
  end
  if saturated >= n
    print a.join(' ')
  else
    a[saturated] = last_saturation.to_i
    print a.join(' ')
  end
#end

input = STDIN.gets
n,k,t = input.strip.split(' ').map{|w| w.to_i}
a = Array.new(n).fill(0)
saturated = t / (100/n)
sum_saturated = saturated * k
last_saturation = (t * n * k / 100) - sum_saturated
saturated.times do |i|
  a[i] = k
end
if saturated >= n
  print a.join(' ')
else
  a[saturated] = last_saturation
  if(saturated + 1 < n)
    ((saturated + 1)..n-1).each do |i|
      a[i] = 0
    end
  end
  print a.join(' ')
end

input = STDIN.gets
n,k,t = input.strip.split(' ').map{|w| w.to_i}
a = Array.new(n).fill(0)
saturated = t / (100/n)
sum_saturated = saturated * k
last_saturation = (t * n * k / 100) - sum_saturated
saturated.times do |i|
  a[i] = k
end
if saturated >= n
  puts a
else
  a[saturated] = last_saturation
  if(saturated + 1 < n)
    ((saturated + 1)..n).each do |i|
      a[i] = 0
    end
  end
  puts a
end

input = STDIN.gets
n,k,t = input.strip.split(' ').map{|w| w.to_i}
a = Array.new(n).fill(0)
if(t <= k)
  a[0] = t
  print a.join(' ')
else
  p = t * n / 100
  q = (t - (p*(100.0/n))).to_i
  puts p,q
  p.times do |i|
    a[i] = k
  end
  if(q > 0)
    a[p] = q * n * k / 100
  end
  print a.join(' ')
  #saturated = t / (100/n)
  #sum_saturated = saturated * k
  #last_saturation = (t * n * k / 100) - sum_saturated
  #saturated.times do |i|
  #  a[i] = k.to_i
  #end
  #if saturated >= n
  #  print a.join(' ')
  #else
  #  a[saturated] = last_saturation.to_i
  #  print a.join(' ')
  #end
end


A camera you have accidentally left in a desert has taken an interesting photo. The photo has a resolution of n pixels width, and each column of this photo is all white or all black. Thus, we can represent the photo as a sequence of n zeros and ones, where 0 means that the corresponding column is all white, and 1 means that the corresponding column is black.You think that this photo can contain a zebra. In this case the whole photo should consist of several (possibly, only one) alternating black and white stripes of equal width. For example, the photo [0, 0, 0, 1, 1, 1, 0, 0, 0] can be a photo of zebra, while the photo [0, 0, 0, 1, 1, 1, 1] can not, because the width of the black stripe is 3, while the width of the white stripe is 4. Can the given photo be a photo of zebra or not?

The first line contains a single integer n (1 ≤ n ≤ 100 000) — the width of the photo. The second line contains a sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 1) — the description of the photo. If ai is zero, the i-th column is all black. If ai is one, then the i-th column is all white.

If the photo can be a photo of zebra, print "YES" (without quotes). Otherwise, print "NO". You can print each letter in any case (upper or lower).

NoteThe first two examples are described in the statements.In the third example all pixels are white, so the photo can be a photo of zebra.In the fourth example the width of the first stripe is equal to three (white color), the width of the second stripe is equal to three (black), and the width of the third stripe is equal to two (white). Thus, not all stripes have equal length, so this photo is not a photo of zebra.

n = gets.chomp.to_i
a = gets.chomp.split(" ").map{|x| x.to_i}
kek = 0
kek2 = 0
tt = a[0]
ans = "YES"
flag = true
for i in a do
  if i == tt
    kek += 1
  else
    flag = false
    if kek2 == kek
      tt = i
      kek = 1
    elsif kek2 == 0 then
      tt = i
      kek2 = kek
      kek = 1
    else
      ans = "NO"
      break
    end
  end
end
if kek != kek2
  ans = "NO"
end
if flag
  ans = "YES"
end
puts ans

n = gets.to_i
cnt0 = 0
cnt1 = 0
old = -1
ans = 1

arr = gets.split(" ").map(&:to_i)
for i in 0..n-1 do
    if (old == (-1))
        old = arr[i]
        cnt1 = cnt1 + 1
    elsif (arr[i] == old)
        cnt1 = cnt1 + 1
    elsif (arr[i] != old and cnt0 == 0)
        old = 1 - old
        cnt0 = cnt1
        cnt1 = 1
    elsif (arr[i] != old && cnt1 == cnt0)
        old = 1 - old
        cnt1 = 1
    elsif (arr[i] != old and cnt1 != cnt0)
            ans = 0

    end

end
if (cnt1 != cnt0 and cnt0 != 0)
    ans = 0
end
if (ans == 0)
  puts "NO"
else
  puts "YES"
end

n = gets.to_i

A = gets.split

len = 1
for i in 1..(n - 1)
	if (A[i - 1] != A[i])
		break
		end
	len += 1
end

x = 1

for i in 1..(n - 1)
	if (A[i - 1] != A[i] && i % len != 0)
		x = 0
		end
	if (A[i - 1] == A[i] && i % len == 0)
		x = 0
		end
end

if (x == 0 || n % len > 0)
	puts "NO"
else
	puts "YES"
end

n = gets.to_i
a = gets.chomp.split()
b = Array.new
lst = a.at(0)
c = 1
for i in 1..(n-1)
        if a.at(i) == lst
                c = c+1
        else
                b.push(c)
                c = 1
                lst = a.at(i)
        end
end
b.push(c)
ans = "NO"
if b.uniq.size == 1
        ans = "YES"
end
puts ans

r=->{gets.split.map &:to_i}
n=r[]
a=r[] + [2]

color = a[0]
l = 1
whitelen = -1
blacklen = -1

good = true

for i in 1 ... a.size
  if a[i] != color
    if color == 1
      if whitelen == -1 or whitelen == l
        whitelen = l
      else
        good = false
      end
    else
      if blacklen == -1 or blacklen == l
        blacklen = l
      else
        good = false
      end
    end
    color = a[i]
    l = 0
  end
  l += 1
end

if blacklen != -1 and whitelen != -1 and whitelen != blacklen
  good = false
end

if good
  puts "YES"
else
  puts "NO"
end

n = gets().split(' ').map{|x| Integer(x)}[0]
a = gets().split(' ').map{|x| Integer(x)}

cur = 1
st = -1
flag = true

(1...n).each do |i|
    if i == 0 or a[i] != a[i - 1] then
    	if st == -1 then
    		st = cur
    	elsif cur != st
    		flag = false
    	end
    	cur = 1
    else
    	cur += 1
    end
end

if st == -1 then
	st = cur
elsif cur != st
	flag = false
end

if flag then
	puts("YES")
else
	puts("NO")
end

# your code goes here

# fo = open("input.txt", 'r')
n = gets.to_i

res = gets.split()
cnt = -1
cur = 1
ans = 'Yes'
(n - 1).times do |i|
    if (res[i] == res[i + 1])
        cur += 1
    else
        if (cnt == -1)
            cnt = cur
        else
            if (cnt != cur)
                ans = 'No'
            end
        end
        cur = 1
    end
end

if (cnt != cur and cnt != -1)
    ans = 'No'
end

puts(ans)

n = gets
ar = gets.chomp.split.map(&:to_i)
combed = Hash.new
old = ar[0]
i = 1
(1..ar.length).each do |x|
    cur = ar[x]
    if old == cur
        i+=1
    else
        combed[i] = 1
        i = 1
        old = cur
    end
end

puts combed.size <= 1 ? "YES" : "NO"

n = gets.to_i
photo = gets.split

current_color = photo.first
first_strip_width = 0
current_strip_width = 0
photo.each do |pixel|
  if pixel == current_color
    current_strip_width += 1
  else
    first_strip_width = current_strip_width if first_strip_width == 0
    if current_strip_width != first_strip_width
      break
    end
    current_strip_width = 1
    current_color = pixel
  end
end

first_strip_width = current_strip_width if first_strip_width == 0
if current_strip_width == first_strip_width
  puts "YES"
else
  puts "NO"
end

n=gets.to_i()
s=gets.split()
p=0
q=0
h=0
f=1
for i in s
	if(i!=p)
		if(q!=h && h!=0)
			f=0
			break
		end
		h=q
		p=i
		q=1
	else
		q+=1
	end
end
if(q!=h && h!=0)
	f=0
end
if(f==1)
	puts('YES')
else
	puts('NO')
end

n = gets.chomp().to_i
prv = -1
len = -1
target = -1
ok = 1
gets.split.map(&:to_i).each do |a|
	if a != prv
		prv = a
		if target != -1 and len != target
			ok = 0
		end
		target = len
		len = 0
	else
		len = len + 1
	end
end
if target != -1 and len != target
	ok = 0
end
if ok == 1
	puts "Yes"
else
	puts "No"
end

gets
puts gets.split.map(&:to_i).chunk{|n| n==0}.map{|b,a| a.size}.uniq.length==1 ? "Yes" : "No"

n = gets.chomp.to_i

a = []
gets.chomp.split.each do |i|
  a << i.to_i
end
l = -1
pv = a[0]
cl = 0
fl = 0
a.each do |x|
    if (x != pv)
        if (l == -1)
            l = cl
        end
        if (l != cl)
            fl = 1
        end
        cl = 0
    end
    pv = x
    cl += 1
end
if (l == -1)
    l = cl
end
if (l != cl)
    fl = 1
end
if fl == 1
    puts "NO"
else
    puts "YES"
end

n = gets.to_i
cnt0 = 0
cnt1 = 0
old = -1
ans = 1

arr = gets.split(" ").map(&:to_i)
for i in 0..n-1 do
    if (old == (-1))
        old = arr[i]
        cnt1 = cnt1 + 1
    elsif (arr[i] == old)
        cnt1 = cnt1 + 1
     elsif (arr[i] == 1 - old and cnt0 == 0)
            old = 1 - old
            cnt0 = cnt1
            cnt1 = 1

    elsif (arr[i] == 1 - old and cnt1 != cnt0)
            ans = 0

    end

end
if (cnt1 != cnt0 and cnt0 != 0)
    ans = 0
end
if (ans == 0)
  puts "NO"
else
  puts "YES"
end

n = gets.to_i

A = gets.split

len = 1
for i in 1..(n - 1)
	if (A[i - 1] != A[i])
		break
		end
	len += 1
end

x = 1

for i in 1..(n - 1)
	if (A[i - 1] != A[i] && i % len != 0)
		x = 0
		end
end

if (x == 0 || n % len > 0)
	puts "NO"
else
	puts "YES"
end

n = gets.to_i
a = gets.chomp.split()
puts a
b = Array.new
lst = a.at(0)
c = 1
for i in 1..(n-1)

        if a.at(i) == lst
                c = c+1
        else
                b.push(c)
                c = 1
                lst = a.at(i)
        end
end
b.push(c)
ans = "NO"
if b.uniq.size == 1
        ans = "YES"
end
puts ans


You've got another problem dealing with arrays. Let's consider an arbitrary sequence containing n (not necessarily different) integers a1, a2, ..., an. We are interested in all possible pairs of numbers (ai, aj), (1 ≤ i, j ≤ n). In other words, let's consider all n2 pairs of numbers, picked from the given array.For example, in sequence a = {3, 1, 5} are 9 pairs of numbers: (3, 3), (3, 1), (3, 5), (1, 3), (1, 1), (1, 5), (5, 3), (5, 1), (5, 5).Let's sort all resulting pairs lexicographically by non-decreasing. Let us remind you that pair (p1, q1) is lexicographically less than pair (p2, q2) only if either p1 < p2, or p1 = p2 and q1 < q2.Then the sequence, mentioned above, will be sorted like that: (1, 1), (1, 3), (1, 5), (3, 1), (3, 3), (3, 5), (5, 1), (5, 3), (5, 5)Let's number all the pair in the sorted list from 1 to n2. Your task is formulated like this: you should find the k-th pair in the ordered list of all possible pairs of the array you've been given.

The first line contains two integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ n2). The second line contains the array containing n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). The numbers in the array can coincide. All numbers are separated with spaces. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout, streams or the %I64d specificator instead.

In the single line print two numbers — the sought k-th pair.

NoteIn the first sample the sorted sequence for the given array looks as: (1, 1), (1, 2), (2, 1), (2, 2). The 4-th of them is pair (2, 2).The sorted sequence for the array from the second sample is given in the statement. The 2-nd pair there is (1, 3).

I = ->{gets.split.map &:to_i}
n, m = I[]
m -= 1
a = I[].sort
h = Hash.new(0)
a.each{|_| h[_] += 1}
h.sort.each{|k, v|
    if m < v * n
        m /= v
        puts "#{k} #{a[m]}"
        break
    else
        m -= v * n
    end
}

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

a.sort!
k-=1
input=[]
n.times{|i|
        if i>0&&a[i]==a[i-1]
                input[input.length-1][1]+=1
        else
                input.push([a[i],1])
        end
}
input.each{|t|
        if k<t[1]*n
                p t[0]
                p a[k/t[1]]
                exit
        else
                k-=n*t[1]
        end
}

n, k = STDIN.readline.split.map {|s| s.to_i }
k -= 1
data = STDIN.readline.split.map {|s| s.to_i }.sort
ai = k/n
a = data[ai]
low = ai
while low-1 >= 0 and data[low-1] == a
  low -= 1
end
high = ai
while high+1 < n and data[high+1] == a
  high += 1
end
b = data[(k - low*n)/(high-low+1)]
puts '%d %d' % [a, b]

n,k = gets.split.map(&:to_i)
k -= 1
a = gets.split.map(&:to_i).sort

d,m = k.divmod n

s = a.select{|e|e == a[d] }.size

if s > 1
  f = a.index(a[d])*n
  m = (k-f)/s
end

print a[d], " ",a[m]

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

n, k = *$stdin.gets.chomp.split.map(&:to_i)
as = $stdin.gets.chomp.split.map(&:to_i)

h = Hash.new { 0 }
as.each do |a|
  h[a] += 1
end

h.keys.sort.each do |a1|
  c1 = h[a1]
  if k-c1*n <= 0
    h.keys.sort.each do |a2|
      c2 = h[a2]
      if k-c1*c2 <= 0
        puts [a1, a2].join(' ')
        exit 0
      end
      k -= c1*c2
    end
  end
  k -= c1*n
end

n,k=gets.chomp.split.collect{|a| a.to_i}
arr=gets.chomp.split.collect{|a| a.to_i}.sort
k=k-1
if arr[k/n] == arr[k/n+1] || arr[k/n]==arr[k/n-1]
m=arr.count arr[k/n]
z=arr.index arr[k/n]

print arr[k/n]," ",arr[(k-z*n)/m]
else
print arr[k/n]," " , arr[k%(n)].to_s
end

n, k = gets.split.collect{|i| i.to_i}
numbers = gets.split.collect{|i| i.to_i}.sort

k -= 1
f_idx = k / n
s_idx = k % n

fl = numbers.index(numbers[f_idx])
fr = numbers.rindex(numbers[f_idx])

if not fl == fr
  rem = k - fl * n
  s_idx = rem / (fr - fl + 1)
end

puts [numbers[f_idx], numbers[s_idx]].join(" ")

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

a.sort!
k-=1
n.times{|i|
        if k<n
                p a[i]
                p a[k]
                exit
        else
                k-=n
        end
}

n, k = STDIN.readline.split.map {|s| s.to_i }
k -= 1
data = STDIN.readline.split.map {|s| s.to_i }.sort
ai = k/n
a = data[ai]
low = ai-1
while low >= 0 and data[low] == a
  low -= 1
end
high = ai+1
while high < n and data[high] == a
  high += 1
end
b = data[(k - low*n)/(high-low+1)]
puts '%d %d' % [a, b]

n, k = STDIN.readline.split.map {|s| s.to_i }
data = STDIN.readline.split.map {|s| s.to_i }.sort
k -= 1
a = data[k/n]
b = data[k%n]
puts '%d %d' % [a, b]

n,k = gets.split.map(&:to_i)
k -= 1
a = gets.split.map(&:to_i).uniq.sort

d,m = k.divmod n

print a[d], " ",a[m]

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

n, k = *$stdin.gets.chomp.split.map(&:to_i)
as = $stdin.gets.chomp.split.map(&:to_i).sort

k -= 1
puts [as[k/n], as[k%n]].join(' ')

n,k=gets.chomp.split.collect{|a| a.to_i}
arr=gets.chomp.split.collect{|a| a.to_i}.sort
k=k-1
print arr[k/n]," " , arr[k%(n)].to_s

n,k=gets.chomp.split.collect{|a| a.to_i}
arr=gets.chomp.split.collect{|a| a.to_i}.sort
k=k-1
if arr[k/n] == arr[k/n+1] || arr[k/n]==arr[k/n-1]
print arr[k/n]," ",arr[k/n]
else
print arr[k/n]," " , arr[k%(n)].to_s
end

n,k=gets.chomp.split.collect{|a| a.to_i}
arr=gets.chomp.split.collect{|a| a.to_i}.sort
k=k-1
if arr[k/n] == arr[k/n+1] || arr[k/n]==arr[k/n-1]
m=arr.count arr[k/n]
print arr[k/n]," ",arr[(k/m)]
else
print arr[k/n]," " , arr[k%(n)].to_s
end

n, k = gets.split.collect{|i| i.to_i}
numbers = gets.split.collect{|i| i.to_i}.sort

k -= 1
f_idx = k / n
s_idx = k % n

puts [numbers[f_idx], numbers[s_idx]].join(" ")


There is a bookshelf which can fit $$$n$$$ books. The $$$i$$$-th position of bookshelf is $$$a_i = 1$$$ if there is a book on this position and $$$a_i = 0$$$ otherwise. It is guaranteed that there is at least one book on the bookshelf.In one move, you can choose some contiguous segment $$$[l; r]$$$ consisting of books (i.e. for each $$$i$$$ from $$$l$$$ to $$$r$$$ the condition $$$a_i = 1$$$ holds) and:  Shift it to the right by $$$1$$$: move the book at index $$$i$$$ to $$$i + 1$$$ for all $$$l \le i \le r$$$. This move can be done only if $$$r+1 \le n$$$ and there is no book at the position $$$r+1$$$.  Shift it to the left by $$$1$$$: move the book at index $$$i$$$ to $$$i-1$$$ for all $$$l \le i \le r$$$. This move can be done only if $$$l-1 \ge 1$$$ and there is no book at the position $$$l-1$$$. Your task is to find the minimum number of moves required to collect all the books on the shelf as a contiguous (consecutive) segment (i.e. the segment without any gaps).For example, for $$$a = [0, 0, 1, 0, 1]$$$ there is a gap between books ($$$a_4 = 0$$$ when $$$a_3 = 1$$$ and $$$a_5 = 1$$$), for $$$a = [1, 1, 0]$$$ there are no gaps between books and for $$$a = [0, 0,0]$$$ there are also no gaps between books.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 200$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 50$$$) — the number of places on a bookshelf. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 1$$$), where $$$a_i$$$ is $$$1$$$ if there is a book at this position and $$$0$$$ otherwise. It is guaranteed that there is at least one book on the bookshelf.

For each test case, print one integer: the minimum number of moves required to collect all the books on the shelf as a contiguous (consecutive) segment (i.e. the segment without gaps).

NoteIn the first test case of the example, you can shift the segment $$$[3; 3]$$$ to the right and the segment $$$[4; 5]$$$ to the right. After all moves, the books form the contiguous segment $$$[5; 7]$$$. So the answer is $$$2$$$.In the second test case of the example, you have nothing to do, all the books on the bookshelf form the contiguous segment already.In the third test case of the example, you can shift the segment $$$[5; 5]$$$ to the left and then the segment $$$[4; 4]$$$ to the left again. After all moves, the books form the contiguous segment $$$[1; 3]$$$. So the answer is $$$2$$$.In the fourth test case of the example, you can shift the segment $$$[1; 1]$$$ to the right, the segment $$$[2; 2]$$$ to the right, the segment $$$[6; 6]$$$ to the left and then the segment $$$[5; 5]$$$ to the left. After all moves, the books form the contiguous segment $$$[3; 4]$$$. So the answer is $$$4$$$.In the fifth test case of the example, you can shift the segment $$$[1; 2]$$$ to the right. After all moves, the books form the contiguous segment $$$[2; 5]$$$. So the answer is $$$1$$$.

T = gets.to_i

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

  books = []

  a.each_with_index do |x, i|
    next if x == 0

    books << i
  end

  ans = 0

  books.each_cons(2) do |a, b|
    ans += b - a - 1
  end

  puts ans
end

t = gets.chomp.to_i
t.times do
  n = gets.chomp.to_i
  arr = gets.chomp.split(' ').map(&:to_i)
  st = -1
  res = 0
  arr.each_with_index do |el, ind|
    if el == 1 && st != -1
      res += ind - st - 1
      st = ind
    end
    if el == 1 && st == -1
      st = ind
    end
  end
  puts res
end

t = gets.chomp.to_i
t.times do
    n = gets.chomp.to_i
    a = gets.chomp.split(" ").map(&:to_i)
    ans = 0
    count = 0
    an = 0
    a.each do |i|
        if i == 0 && count >= 1
            an += 1
        elsif i == 1
            count += 1
            ans += an
            an = 0
        end
    end
    puts ans
end

gets.to_i.times do
  n = gets.to_i
  v = gets.split.map(&:to_i)
  seen = false
  cur = 0
  r = 0
  v.each do |x|
    cur += 1 if seen && x == 0
    if x == 1
      seen = true
      r += cur
      cur = 0
    end
  end
  puts r
end

t = gets.to_i
ans = []
t.times do
  n = gets.to_i
  a = gets.chomp.split(" ").map(&:to_i)
  cnt = 0
  left = -1
  right = -1
  n.times do |i|
    if left == -1 && a[i] == 1
      left = i
    end
    if right == -1 && a[n-1-i] == 1
      right = n-1-i
    end
  end
  for i in left..right
    if a[i] == 0
      cnt += 1
    end
  end
  ans << cnt
end
puts ans

gets.to_i.times do
  _ = gets
  a = gets.split.map(&:to_i)
  puts a.rindex(1) - a.index(1) - a.count(1) + 1
end

t=gets.to_i
t.times do
  n=gets.to_i
  an=gets.chomp.split(' ').map(&:to_i)
  start = an.find_index {|ai|ai!=0}
  last =n-1-an.reverse.find_index{|ai|ai!=0}
  gapc=an[start..last].count do |ai|
    ai==0
  end
  p gapc
end


You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to say the number of such positive integers $$$x$$$ such that $$$x$$$ divides each number from the array. In other words, you have to find the number of common divisors of all elements in the array.For example, if the array $$$a$$$ will be $$$[2, 4, 6, 2, 10]$$$, then $$$1$$$ and $$$2$$$ divide each number from the array (so the answer for this test is $$$2$$$).

The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 4 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^{12}$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$.

Print one integer — the number of such positive integers $$$x$$$ such that $$$x$$$ divides each number from the given array (in other words, the answer is the number of common divisors of all elements in the array).

require 'prime'
class Integer
	def divisors
		self.prime_division.reduce([1]) do |a, (p, e)|
			(0..e).map { p**_1 }.product(a).map { _1 * _2 }
		end.sort
	end
	def divisor_count
		self.prime_division.reduce(1) { |acc, (p, e)| acc *= e + 1 }
	end
end

gets
puts gets.split.map(&:to_i).reduce(&:gcd).divisor_count

def divs(n)
  i = 1
  sq = Math.sqrt(n).ceil.to_i
  div = []

  while i <= sq
    if (n % i).zero?
      if (n / i) == i
        div << i
      else
        div << i
        div << n / i
      end
    end
    i += 1
  end
  div.uniq
end

n = gets.strip.to_i
a = gets.strip.split(' ').map(&:to_i).uniq
g = 0
a.each do |e|
  g = e.gcd(g)
end
puts divs(g).size

n = gets.to_i
g = 0
a = gets.strip.split.map(&:to_i)
a.each do |x|
  g  = g.gcd(x)
end
res = 0
i = 1
while i ** 2 <= g
  if g % i == 0
    res += 1
    res += 1 if i != g / i
  end
  i += 1
end
puts res

class NemiLib
  # 素因数分解ハッシュ形式
  # (例)100なら{2=>2,5=>2}
  def prime_factorization(num, result = Hash.new(0))
    return {} if num < 1
    return {1=>1} if num == 1

    divides = false
    to = Math.sqrt(num).floor
    # ２だけ別処理
    # 再起で呼び出すので1メソッド中に1回だけしか割らないようにする
    if num % 2 == 0
      result[2] += 1
      prime_factorization(num / 2, result)
      divides = true
    else
      3.step(to, 2) do |item|
        if num % item == 0
          result[item] += 1
          prime_factorization(num / item, result)
          divides = true
          break
        end
      end
    end
    # 何も割れないなら、その数を入れる
    result[num] += 1 unless divides

    result
  end

  # 約数
  def divisors(num)
    return [1] if num == 1

    pows = []
    # 0乗、1乗・・・した値を入れる
    prime_factorization(num).each_pair do |key, value|
      pow = []
      0.upto(value).each do |nn|
        pow << key ** nn
      end

      pows << pow
    end

    first_array = pows.delete_at(0)
    return first_array if pows.size == 0

    result = first_array.product(*pows)
    result.map do |item|
      item.inject { |sum, i| sum * i }
    end.sort
  end
end

n = gets.to_i
aa = gets.split.map(&:to_i)
gcd = aa.inject(:gcd)
pf = NemiLib.new.prime_factorization(gcd)
puts (pf[1]==1 ? 1 : pf.each_value.inject(1){|sum,i| sum*(i+1)})

def factors(n)
  facts = [1]

  while (n % 2).zero?
    facts << 2
    n = n / 2
  end
  i = 3
  sq = Math.sqrt(n).ceil.to_i
  while i <= sq
    while (n % i).zero?
      facts << i
      n = n / i
    end
    i += 2
  end
  facts << n

  facts.uniq
end

n = gets.strip.to_i
a = gets.strip.split(' ').map(&:to_i)
mn = a.min
mx = a.max
facts = (factors(mn) + factors(mx) + [mn, mx]).uniq
# puts facts.inspect

res = 0
facts.each do |f|
  res += 1 if a.all? { |e| e % f == 0 }
end
puts res

def factors(n)
  facts = [1]

  while (n % 2).zero?
    facts << 2
    n = n / 2
  end
  i = 3
  sq = Math.sqrt(n).ceil.to_i
  while i <= sq
    while (n % i).zero?
      facts << i
      n = n / i
    end
    i += 1
  end
  facts << n if n > 2

  facts.uniq
end

n = gets.strip.to_i
a = gets.strip.split(' ').map(&:to_i)
mn = a.min
facts = (factors(mn) + [mn]).uniq

res = 0
facts.each do |f|
  res += 1 if a.all? { |e| e % f == 0 }
end
puts res

class NemiLib
  # 素因数分解ハッシュ形式
  # (例)100なら{2=>2,5=>2}
  def prime_factorization(num, result = Hash.new(0))
    return {} if num < 1
    return {1=>1} if num == 1

    divides = false
    to = Math.sqrt(num).floor
    # ２だけ別処理
    # 再起で呼び出すので1メソッド中に1回だけしか割らないようにする
    if num % 2 == 0
      result[2] += 1
      prime_factorization(num / 2, result)
      divides = true
    else
      3.step(to, 2) do |item|
        if num % item == 0
          result[item] += 1
          prime_factorization(num / item, result)
          divides = true
          break
        end
      end
    end
    # 何も割れないなら、その数を入れる
    result[num] += 1 unless divides

    result
  end

  # 約数
  def divisors(num)
    return [1] if num == 1

    pows = []
    # 0乗、1乗・・・した値を入れる
    prime_factorization(num).each_pair do |key, value|
      pow = []
      0.upto(value).each do |nn|
        pow << key ** nn
      end

      pows << pow
    end

    first_array = pows.delete_at(0)
    return first_array if pows.size == 0

    result = first_array.product(*pows)
    result.map do |item|
      item.inject { |sum, i| sum * i }
    end.sort
  end
end

n = gets.to_i
aa = gets.split.map(&:to_i)
gcd = aa.inject(:gcd)
pf = NemiLib.new.prime_factorization(gcd)
puts (pf.size==1 ? 1 : pf.each_value.inject(1){|sum,i| sum*(i+1)})

class NemiLib
  # 素因数分解ハッシュ形式
  # (例)100なら{2=>2,5=>2}
  def prime_factorization(num, result = Hash.new(0))
    return {} if num < 1
    return {1=>1} if num == 1

    divides = false
    to = Math.sqrt(num).floor
    # TODO うまくかきたい。２だけ別処理
    if num % 2 == 0
      result[2] += 1
      prime_factorization(num / 2, result)
      divides = true
    end
    3.step(to, 2) do |item|
      if num % item == 0
        result[item] += 1
        prime_factorization(num / item, result)
        divides = true
        break
      end
    end
    # 何も割れないなら、その数を入れる
    result[num] += 1 unless divides

    result
  end

  # 約数
  def divisors(num)
    return [1] if num == 1

    pows = []
    # 0乗、1乗・・・した値を入れる
    prime_factorization(num).each_pair do |key, value|
      pow = []
      0.upto(value).each do |nn|
        pow << key ** nn
      end

      pows << pow
    end

    first_array = pows.delete_at(0)
    return first_array if pows.size == 0

    result = first_array.product(*pows)
    result.map do |item|
      item.inject { |sum, i| sum * i }
    end.sort
  end
end

n = gets.to_i
aa = gets.split.map(&:to_i)
gcd = aa.inject(:gcd)
pf = NemiLib.new.prime_factorization(gcd)
puts pf.each_value.inject(1){|sum,i| sum*(i+1)}

class NemiLib
  # 素因数分解ハッシュ形式
  # (例)100なら{2=>2,5=>2}
  def prime_factorization(num, result = Hash.new(0))
    return {} if num < 1
    return {1=>1} if num == 1

    divides = false
    to = Math.sqrt(num).floor
    # TODO うまくかきたい。２だけ別処理
    if num % 2 == 0
      result[2] += 1
      prime_factorization(num / 2, result)
      divides = true
    end
    3.step(to, 2) do |item|
      if num % item == 0
        result[item] += 1
        prime_factorization(num / item, result)
        divides = true
        break
      end
    end
    # 何も割れないなら、その数を入れる
    result[num] += 1 unless divides

    result
  end

  # 約数
  def divisors(num)
    return [1] if num == 1

    pows = []
    # 0乗、1乗・・・した値を入れる
    prime_factorization(num).each_pair do |key, value|
      pow = []
      0.upto(value).each do |nn|
        pow << key ** nn
      end

      pows << pow
    end

    first_array = pows.delete_at(0)
    return first_array if pows.size == 0

    result = first_array.product(*pows)
    result.map do |item|
      item.inject { |sum, i| sum * i }
    end.sort
  end
end

n = gets.to_i
aa = gets.split.map(&:to_i)
gcd = aa.inject(:gcd)
pf = NemiLib.new.prime_factorization(gcd)
puts (pf.size==1 ? 1 : pf.each_value.inject(1){|sum,i| sum*(i+1)})


One day n friends met at a party, they hadn't seen each other for a long time and so they decided to make a group photo together. Simply speaking, the process of taking photos can be described as follows. On the photo, each photographed friend occupies a rectangle of pixels: the i-th of them occupies the rectangle of width wi pixels and height hi pixels. On the group photo everybody stands in a line, thus the minimum pixel size of the photo including all the photographed friends, is W × H, where W is the total sum of all widths and H is the maximum height of all the photographed friends.As is usually the case, the friends made n photos — the j-th (1 ≤ j ≤ n) photo had everybody except for the j-th friend as he was the photographer.Print the minimum size of each made photo in pixels.

The first line contains integer n (2 ≤ n ≤ 200 000) — the number of friends.  Then n lines follow: the i-th line contains information about the i-th friend. The line contains a pair of integers wi, hi (1 ≤ wi ≤ 10, 1 ≤ hi ≤ 1000) — the width and height in pixels of the corresponding rectangle.

Print n space-separated numbers b1, b2, ..., bn, where bi — the total number of pixels on the minimum photo containing all friends expect for the i-th one.

count = gets.to_i
mas_width = Array.new(count)
mas_height = Array.new(count)
for i in 0..(count - 1)
    string = gets
    mas_width[i] = string.split(' ')[0].to_i
    mas_height[i] = string.split(' ')[1].to_i
end

all_width = 0
for i in 0..(count - 1)
    all_width += mas_width[i]
end

new_mas_height = Array.new(mas_height).sort

max = new_mas_height[count - 1]
max2 = new_mas_height[count - 2]

index_max = mas_height.index(max)

for i in 0..(count - 1)
    unless i == index_max
        print (all_width - mas_width[i]) * max
    else
        print (all_width - mas_width[i]) * max2
    end
    print ' ' unless i == count - 1
end

@data = $stdin.read.split
n = @data.shift.to_i
w = []
h = []
sum_w = 0
prf_h = []
suf_h = []
n.times do
	w << @data.shift.to_i
	h << @data.shift.to_i
	prf_h << h.last
	if h.size > 1
		prf_h[-1] = [prf_h.last, prf_h[-2]].max
	end
	suf_h << h.last
	sum_w += w.last
end

n.times do |i|
	next if i == 0
	suf_h[n - i - 1] = [suf_h[n-i-1], suf_h[n-i]].max
end

result = []
n.times do |i|
	h_left = i == 0 ? 0 : prf_h[i - 1]
	h_right = i == n - 1 ? 0 : suf_h[i + 1]
	result << (sum_w - w[i]) * [h_left, h_right].max
end

puts result.join(' ')

friends = gets.strip.to_i
fr_sizes = Array.new(friends)

w = 0
h1,h2 = 0,0

#read
friends.times do |n|
    pair = gets.strip.split(' ').map(&:to_i)
    w += pair[0]
    if h1 < pair[1] then
        h2 = h1
        h1 = pair[1]
    elsif h2 < pair[1] then
        h2 = pair[1]
    end
    fr_sizes[n] = pair
end

fr_sizes.each do |wf, hf|
    width = w - wf
    height = (hf == h1 ? h2 : h1)
    print "#{width * height} "
end

amount = gets.chomp.to_i

friends = []
sum = 0
max1 = 0
max2 = 0

amount.times {
	x = gets.chomp.split(" ")
	x[0] = x[0].to_i
	x[1] = x[1].to_i
	friends.push(x)
	sum = sum + x[0]

	if x[1] >= max1
		max1 = x[1]
	end

	if x[1] >= max2
		max1 = max2
		max2 = x[1]
	end
}

friends.each { |x|
	if x[1] == max2
		print max1 * (sum - x[0]), " "
	else
		print max2 * (sum - x[0]), " "
	end
}


You are given an array $$$a$$$ consisting of $$$n$$$ nonnegative integers. Let's define the prefix OR array $$$b$$$ as the array $$$b_i = a_1~\mathsf{OR}~a_2~\mathsf{OR}~\dots~\mathsf{OR}~a_i$$$, where $$$\mathsf{OR}$$$ represents the bitwise OR operation. In other words, the array $$$b$$$ is formed by computing the $$$\mathsf{OR}$$$ of every prefix of $$$a$$$.You are asked to rearrange the elements of the array $$$a$$$ in such a way that its prefix OR array is lexicographically maximum.An array $$$x$$$ is lexicographically greater than an array $$$y$$$ if in the first position where $$$x$$$ and $$$y$$$ differ, $$$x_i > y_i$$$.

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ nonnegative integers $$$a_1, \ldots, a_n$$$ ($$$0 \leq a_i \leq 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 $$$n$$$ integers — any rearrangement of the array $$$a$$$ that obtains the lexicographically maximum prefix OR array.

require 'set'
gets.to_i.times do
  n=gets.to_i
  a=gets.split.map(&:to_i)
  x=1<<32
  ans=[]
  b=0
  while x>0 do
    if b&x>0 then
      x>>=1
      next
    end
    c=[]
    a.each do |i|
      c << i if i&x>0
    end
    if c.empty? then
      x>>=1
      next
    end
    ans << c.max_by {|i| i|b}
    b|=ans[-1]
    a.delete_at(a.index(ans[-1]))
    x>>=1
  end
  puts (ans+a.to_a)*' '
end


You are given three positive (i.e. strictly greater than zero) integers $$$x$$$, $$$y$$$ and $$$z$$$.Your task is to find positive integers $$$a$$$, $$$b$$$ and $$$c$$$ such that $$$x = \max(a, b)$$$, $$$y = \max(a, c)$$$ and $$$z = \max(b, c)$$$, or determine that it is impossible to find such $$$a$$$, $$$b$$$ and $$$c$$$.You have to answer $$$t$$$ independent test cases. Print required $$$a$$$, $$$b$$$ and $$$c$$$ in any (arbitrary) order.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains three integers $$$x$$$, $$$y$$$, and $$$z$$$ ($$$1 \le x, y, z \le 10^9$$$).

For each test case, print the answer:   "NO" in the only line of the output if a solution doesn't exist;  or "YES" in the first line and any valid triple of positive integers $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \le a, b, c \le 10^9$$$) in the second line. You can print $$$a$$$, $$$b$$$ and $$$c$$$ in any order.

gets.to_i.times do
	a = gets.split.map(&:to_i)
	x, y, z = a
	min, max = a.minmax
	case a.count(max)
	when 3
		puts 'YES', a.join(' ')
	when 2
		puts 'YES',
		     (
				if x != max
					[min, min, max]
				elsif y != max
					[min, max, min]
				else
					[max, min, min]
				end
		     ).join(' ')
	else
		puts 'NO'
	end
end

# frozen_string_literal: true

lines = gets.to_i
lines.times do
  xyz = gets.split(' ').map(&:to_i).sort.reverse
  if xyz[0] == xyz[1]
    print 'YES', "\n"
    print "#{xyz[0]} #{xyz[2]} 1", "\n"
  else
    print 'NO', "\n"
  end
end

t = gets.chomp.to_i
t.times do
    atai = gets.chomp.split(" ").map(&:to_i)
    atai = atai.sort
    if  atai[0] != atai[1] && atai[1] != atai[2] && atai[0] != atai[2]
        puts "NO"
    elsif atai[0] == atai[1] && atai[1] == atai[2]
        puts "YES"
        print atai[0]
        print " "
        print atai[1]
        print " "
        print atai[2]
        print "\n"
    elsif atai[0] == atai[1] && atai[1] != atai[2]
        puts "NO"
    elsif atai[2] == atai[1] && atai[1] != atai[0]
        puts "YES"
        print atai[0]
        print " "
        print atai[0]
        print " "
        print atai[2]
        print "\n"
    end

end

tc = gets.to_i

tc.times do
    a = gets.split().map{|i| i.to_i }
    maxi = a.max
    b = a.uniq()
    if(a.count(maxi) > 1)
        puts "YES"
        if(b.size == 2)
            print "1 #{b[0]} #{b[1]}"
        else
            print "#{b[0]} "*3
        end
        puts ""
    else
        puts "NO"
    end
end

def solve
  x, y, z = gets.strip.split(' ').map(&:to_i)
  mx = [x , y, z].max
  c = 0; nt = -1
  if mx == x
    c += 1
  else
    nt = x
  end
  if mx == y
    c += 1
  else
    nt = y
  end
  if mx == z
    c += 1
  else
    nt = z
  end

  if c >= 2
    puts "YES"
    if nt == -1
      puts "#{x} #{y} #{z}"
    else
      puts "#{nt} #{nt} #{mx}"
    end
  else
    puts "NO"
  end
end

gets.strip.to_i.times { solve }

$, = ' '
gets.to_i.times do
    a = gets.split.map(&:to_i).sort
    if a[1] != a[2]
        puts "NO"
    else
        print "YES", a[0], a[0], a[2], "\n"
    end
end

n = gets.to_i
n.times do
  x,y,z = gets.chomp.split(" ").map(&:to_i)
  max = [x,y,z].max
  if x == max && y == max && z == max
    puts "YES"
    puts "#{max} #{max} #{max}"
  elsif x == max && y == max
    puts "YES"
    puts "#{z} #{max} #{z}"
  elsif x == max && z == max
    puts "YES"
    puts "#{max} #{y} #{y}"
  elsif y == max && z == max
    puts "YES"
    puts "#{x} #{x} #{max}"
  else
    puts "NO"
  end
end

gets.to_i.times do
  a = gets.split.map(&:to_i).sort
  puts 'NO' if a[1] != a[2]
  puts "YES\n#{a[2]}" + (" #{a[0]}" * 2) if a[1] == a[2]
end

gets.to_i.times do
  a = gets.split(' ').map(&:to_i).sort.reverse

  if a[0] != a[1]
    puts "NO"
    next
  end

  puts "YES"
  puts "#{a.first} #{a.last} 1"
end

# frozen_string_literal: true

lines = gets.to_i
lines.times do
  xyz = gets.split(' ').map(&:to_i).sort.reverse
  if xyz[0] == xyz[1]
    print 'YES', "\n"
    print "#{xyz[0]} 1 1", "\n"
  else
    print 'NO', "\n"
  end
end

# frozen_string_literal: true

lines = gets.to_i
lines.times do
  xyz = gets.split(' ').map(&:to_i).sort.reverse
  if xyz[0] == xyz[1]
    print 'YES', "\n"
    print "#{xyz[0]} #{xyz[1]} 1", "\n"
  else
    print 'NO', "\n"
  end
end

t = gets.chomp.to_i
t.times do
    x, y ,z = gets.chomp.split(" ").map(&:to_i)
    if x != y && y != z
        puts "NO"
    elsif x == y && y == z
        puts "YES"
        puts "#{x}" +" " + "#{y}" + " " + "#{z}"
    elsif x == y && y != z && x > z
        puts "YES"
        puts "#{z}" +" " + "#{y}" + " " + "#{z}"
    elsif x == y && y != z && x < z
        puts "NO"
    elsif x == z && y != z && x > y
        puts "YES"
        puts "#{x}" +" " + "#{y}" + " " + "#{y}"
    elsif x == z && y != z && x < y
        puts "NO"
    elsif y == z && x != z && y > x
        puts "YES"
        puts "#{x}" +" " + "#{x}" + " " + "#{z}"
    elsif y == z && x != z && y < x
        puts "NO"
    end
end

t = gets.chomp.to_i
t.times do
    atai = gets.chomp.split(" ").map(&:to_i)
    atai = atai.sort
    if  atai[0] != atai[1] && atai[1] != atai[2] && atai[0] != atai[2]
        puts "NO"
    elsif atai[0] != atai[1] && atai[1] != atai[2]
        puts "YES"
        print atai[0]
        print " "
        print atai[1]
        print " "
        print atai[2]
        print "\n"
    elsif atai[0] == atai[1] && atai[1] != atai[2]
        puts "NO"
    elsif atai[2] == atai[1] && atai[1] != atai[0]
        puts "YES"
        print atai[0]
        print " "
        print atai[0]
        print " "
        print atai[2]
        print "\n"
    end

end


— Oh my sweet Beaverette, would you fancy a walk along a wonderful woodland belt with me?   — Of course, my Smart Beaver! Let us enjoy the splendid view together. How about Friday night? At this point the Smart Beaver got rushing. Everything should be perfect by Friday, so he needed to prepare the belt to the upcoming walk. He needed to cut down several trees.Let's consider the woodland belt as a sequence of trees. Each tree i is described by the esthetic appeal ai — some trees are very esthetically pleasing, others are 'so-so', and some trees are positively ugly!The Smart Beaver calculated that he needed the following effects to win the Beaverette's heart:   The first objective is to please the Beaverette: the sum of esthetic appeal of the remaining trees must be maximum possible;  the second objective is to surprise the Beaverette: the esthetic appeal of the first and the last trees in the resulting belt must be the same;  and of course, the walk should be successful: there must be at least two trees in the woodland belt left. Now help the Smart Beaver! Which trees does he need to cut down to win the Beaverette's heart?

The first line contains a single integer n — the initial number of trees in the woodland belt, 2 ≤ n. The second line contains space-separated integers ai — the esthetic appeals of each tree. All esthetic appeals do not exceed 109 in their absolute value.   to get 30 points, you need to solve the problem with constraints: n ≤ 100 (subproblem A1);  to get 100 points, you need to solve the problem with constraints: n ≤ 3·105 (subproblems A1+A2).

In the first line print two integers — the total esthetic appeal of the woodland belt after the Smart Beaver's intervention and the number of the cut down trees k. In the next line print k integers — the numbers of the trees the Beaver needs to cut down. Assume that the trees are numbered from 1 to n from left to right. If there are multiple solutions, print any of them. It is guaranteed that at least two trees have equal esthetic appeal.

n=gets.to_i
a=gets.split.map(&:to_i)
o={}
s=[0]
m=-10**10
m1,m2=0
for i in 1..n do
  b=a[i-1]
  s[i]=s[i-1]+[0,b].max
  if o[b].nil?
    o[b]=i
  else
    c=s[i]-s[o[b]-1]+[2*b,0].min
    m,m1,m2=c,o[b],i if c>=m
  end
end
d=a.each_with_index.map {|x,i| i+1 if i<m1-1||x<0&&i>=m1&&i<m2-1||i>=m2 }.compact
#puts m1,m2
puts "#{m} #{d.size}"
puts d.join(' ')

n = gets.to_i
a = gets.split(' ').map(&:to_i)
a.insert(0, 0)
dp = []
dp[0] = 0
for i in 1..n
  dp[i] = dp[i-1]
  dp[i] += a[i] if a[i] > 0
end
hash = Hash.new(false)
ret = nil
seg = nil
for i in 1..n
  next if hash[a[i]]
  hash[a[i]] = true
  ind = a.rindex(a[i])
  if ind != i
    result = dp[ind] - dp[i-1]
    result += a[i] * 2 if a[i] < 0
    if ret == nil || ret < result
      ret = result
      seg = [i, ind]
    end
  end
end
tree = []
for i in 1...seg[0]
  tree << i
end
for i in seg[0] + 1 ... seg[1]
  tree << i if a[i] < 0
end
for i in seg[1] + 1 ..n
  tree << i
end
puts "#{ret} #{tree.size}"
puts tree.join(' ')

n = gets.to_i
a = gets.split(' ').map(&:to_i)
a.insert(0, 0)
seg = {}
for i in 1..n
  sum = a[i]
  for j in i + 1..n
    sum += a[j] unless a[j] < 0
    if a[i] == a[j]
      sum += a[j] if a[j] < 0
      seg[[i, j]] = sum
      sum -= a[j] if a[j] < 0
    end
  end
end
ret = nil
key = nil
seg.each do |k, v|
  if ret.nil? || v > ret
    key = k
    ret = v
  end
end
tree = []
for i in 1...key[0]
  tree << i
end
for i in key[0] + 1...key[1]
  tree << i if a[i] <= 0
end
for i in key[1] + 1..n
  tree << i
end
puts "#{ret} #{tree.size}"

puts tree.join(' ')

n = gets.to_i
a = gets.split(' ').map(&:to_i)
a.insert(0, 0)
dp = []
dp[0] = 0
pos = {}
for i in 1..n
  pos[a[i]] = [] if pos[a[i]].nil?
  pos[a[i]] << i
  dp[i] = dp[i-1]
  dp[i] += a[i] if a[i] > 0
end
hash = Hash.new(false)
ret = nil
seg = nil
for i in 1..n
  next if hash[a[i]]
  hash[a[i]] = true
  ind = pos[a[i]][-1]
  if ind != i
    result = dp[ind] - dp[i-1]
    result += a[i] * 2 if a[i] < 0
    if ret == nil || ret < result
      ret = result
      seg = [i, ind]
    end
  end
end
tree = []
for i in 1...seg[0]
  tree << i
end
for i in seg[0] + 1 ... seg[1]
  tree << i if a[i] < 0
end
for i in seg[1] + 1 ..n
  tree << i
end
puts "#{ret} #{tree.size}"
puts tree.join(' ')

n=gets.to_i
a=gets.split.map(&:to_i)
o={}
s=[0]
m=-10**10
m1,m2=0
for i in 1..n do
  b=a[i-1]
  oo=o[b]
  s[i]=s[i-1]+(b>0?b:0)
  if oo.nil?
    o[b]=i
  else
    c=s[i]-s[oo-1]+(b>0?0:2*b)
    m,m1,m2=c,oo-1,i-1 if c>m
  end
end
d=(1..m1).to_a+(m2+2..n).to_a+a[m1+1..m2-1].each_with_index.map {|x,i| i+2+m1 if x<0 }.compact
#puts m1,m2
puts "#{m} #{d.size}"
puts d.join(' ')

n = gets.to_i
a = gets.split(' ').map(&:to_i)
a.insert(0, 0)
seg = {}
for i in 1..n
  sum = a[i]
  for j in i + 1...a.size
    sum += a[j] unless a[j] < 0
    if a[i] == a[j]
      sum += a[j] if a[j] < 0
      seg[[i, j]] = sum
    end
  end
end
ret = nil
key = nil
seg.each do |k, v|
  if ret.nil? || v > ret
    key = k
    ret = v
  end
end
tree = []
for i in 1...key[0]
  tree << i
end
for i in key[0] + 1...key[1]
  tree << i if a[i] <= 0
end
for i in key[1] + 1..n
  tree << i
end
puts "#{ret} #{tree.size}"

puts tree.join(' ')

n = gets.to_i
a = gets.split(' ').map(&:to_i)
seg = {}
for i in 0...a.size
  sum = a[i]
  for j in i + 1...a.size
    sum += a[j] unless a[j] < 0
    if a[i] == a[j]
      sum += a[j] if a[j] < 0
      seg[[i+1, j+1]] = sum
    end
  end
end
ret = nil
key = nil
seg.each do |k, v|
  if ret.nil? || v > ret
    key = k
    ret = v
  end
end
tree = []
for i in 1...key[0]
  tree << i
end

for i in key[0] + 1...key[1]
  tree << i if a[i - 1] <= 0
end
for i in key[1] + 1..n
  tree << i
end
puts "#{ret} #{tree.size}"

puts tree.join(' ')

n = gets.to_i
a = gets.split(' ').map(&:to_i)
a.insert(0, 0)
dp = []
dp[0] = 0
for i in 1..n
  dp[i] = dp[i-1]
  dp[i] += a[i] if a[i] > 0
end
hash = Hash.new(false)
ret = nil
seg = nil
for i in 1..n
  next if hash[a[i]]
  hash[a[i]] = true
  ind = a.rindex(a[i])
  if ind != i
    result = dp[ind] - dp[i-1]
    if ret == nil || ret < result
      ret = result
      seg = [i, ind]
    end
  end
end
tree = []
for i in 1...seg[0]
  tree << i
end
for i in seg[0] + 1 ... seg[1]
  tree << i if a[i] < 0
end
for i in seg[1] + 1 ..n
  tree << i
end
puts "#{ret} #{tree.size}"
puts tree.join(' ')

n = gets.to_i
a = gets.split(' ').map(&:to_i)
seg = {}
for i in 0...a.size
  sum = a[i]
  for j in i + 1...a.size
    sum += a[j] unless a[j] < 0
    if a[i] == a[j]
      sum += a[j] if a[j] < 0
      seg[[i+1, j+1]] = sum
    end
  end
end
ret = nil
key = nil
seg.each do |k, v|
  if ret.nil? || v > ret
    key = k
    ret = v
  end
end
tree = []
for i in 1...key[0]
  tree << i
end
for i in key[0]...key[1]
  tree << i if a[i - 1] <= 0
end
for i in key[1]..n
  tree << i
end
puts "#{ret} #{tree.size}"

puts tree.join(' ')

n=gets.to_i
a=gets.split.map(&:to_i)
o={}
s=[0]
m=-10**10
m1,m2=0
for i in 1..n do
  b=a[i-1]
  oo=o[b]
  s[i]=s[i-1]+b>0?b:0
  if oo.nil?
    o[b]=i
  else
    c=s[i]-s[oo-1]+b>0?0:2*b
    m,m1,m2=c,oo-1,i-1 if c>m
  end
end
d=(0..m1).to_a+(m2..n-1).to_a+a[m1+1..m2-1].each_with_index.map {|x,i| i+1 if x<0 }.compact
puts "#{m} #{d.size}"
puts d.join(' ')


Every person likes prime numbers. Alice is a person, thus she also shares the love for them. Bob wanted to give her an affectionate gift but couldn't think of anything inventive. Hence, he will be giving her a graph. How original, Bob! Alice will surely be thrilled!When building the graph, he needs four conditions to be satisfied:   It must be a simple undirected graph, i.e. without multiple (parallel) edges and self-loops.  The number of vertices must be exactly $$$n$$$ — a number he selected. This number is not necessarily prime.  The total number of edges must be prime.  The degree (i.e. the number of edges connected to the vertex) of each vertex must be prime. Below is an example for $$$n = 4$$$. The first graph (left one) is invalid as the degree of vertex $$$2$$$ (and $$$4$$$) equals to $$$1$$$, which is not prime. The second graph (middle one) is invalid as the total number of edges is $$$4$$$, which is not a prime number. The third graph (right one) is a valid answer for $$$n = 4$$$.   Note that the graph can be disconnected.Please help Bob to find any such graph!

The input consists of a single integer $$$n$$$ ($$$3 \leq n \leq 1\,000$$$) — the number of vertices.

If there is no graph satisfying the conditions, print a single line containing the integer $$$-1$$$. Otherwise, first print a line containing a prime number $$$m$$$ ($$$2 \leq m \leq \frac{n(n-1)}{2}$$$) — the number of edges in the graph. Then, print $$$m$$$ lines, the $$$i$$$-th of which containing two integers $$$u_i$$$, $$$v_i$$$ ($$$1 \leq u_i, v_i \leq n$$$) — meaning that there is an edge between vertices $$$u_i$$$ and $$$v_i$$$. The degree of each vertex must be prime. There must be no multiple (parallel) edges or self-loops. If there are multiple solutions, you may print any of them. Note that the graph can be disconnected.

NoteThe first example was described in the statement.In the second example, the degrees of vertices are $$$[7, 5, 2, 2, 3, 2, 2, 3]$$$. Each of these numbers is prime. Additionally, the number of edges, $$$13$$$, is also a prime number, hence both conditions are satisfied.

def make(idx,n)
  ans = []
  if n == 4
    ans << [idx,idx+1]
    ans << [idx,idx+2]
    ans << [idx+1,idx+2]
    ans << [idx+1,idx+3]
    ans << [idx+2,idx+3]
  else
    ans << [idx,idx+n-1]
    (n-1).times do |i|
      ans << [idx+i,idx+i+1]
    end
  end
  return ans
end

require 'prime'
n = gets.to_i
ans = []
if n.prime?
  ans = make(1,n)
elsif n == 4
  ans = make(1,4)
else
  if n % 3 == 1
    ans = make(n-3,4)
    n -= 4
  elsif n % 3 == 2
    ans = make(n-4,5)
    n -= 5
  end
  (n/3).times do |i|
    ans << [i*3+1,i*3+2]
    ans << [i*3+2,i*3+3]
    ans << [i*3+1,i*3+3]
  end
  cnt = 0
  while !ans.length.prime?
    l,r = 6*(cnt/3) + cnt%3+1, 6*(cnt/3) +cnt%3+4
    ans << [l,r]
    cnt += 1
  end
end

p ans.length
ans.each do |a|
  puts a.join(" ")
end

def make(idx,n)
  ans = []
  if n == 4
    ans << [idx,idx+1]
    ans << [idx,idx+2]
    ans << [idx+1,idx+2]
    ans << [idx+1,idx+3]
    ans << [idx+2,idx+3]
  else
    ans << [idx,idx+n-1]
    (n-1).times do |i|
      ans << [idx+i,idx+i+1]
    end
  end
  return ans
end

require 'prime'
n = gets.to_i
ans = []
if n.prime?
  ans = make(1,n)
elsif n == 4
  ans = make(1,4)
else
  if n % 3 == 1
    ans = make(n-3,4)
    n -= 4
  elsif n % 3 == 2
    ans = make(n-4,5)
    n -= 5
  end
  (n/3).times do |i|
    ans << [i*3+1,i*3+2]
    ans << [i*3+2,i*3+3]
    ans << [i*3+1,i*3+3]
  end
  cnt = 0
  while !ans.length.prime?
    l,r = 6*(cnt/6) + cnt%3+1, 6*(cnt/6) +cnt%3+4
    ans << [l,r]
    cnt += 1
  end
end

p ans.length
ans.each do |a|
  puts a.join(" ")
end


After a terrifying forest fire in Berland a forest rebirth program was carried out. Due to it N rows with M trees each were planted and the rows were so neat that one could map it on a system of coordinates so that the j-th tree in the i-th row would have the coordinates of (i, j). However a terrible thing happened and the young forest caught fire. Now we must find the coordinates of the tree that will catch fire last to plan evacuation.The burning began in K points simultaneously, which means that initially K trees started to burn. Every minute the fire gets from the burning trees to the ones that aren’t burning and that the distance from them to the nearest burning tree equals to 1.Find the tree that will be the last to start burning. If there are several such trees, output any.

The first input line contains two integers N, M (1 ≤ N, M ≤ 2000) — the size of the forest. The trees were planted in all points of the (x, y) (1 ≤ x ≤ N, 1 ≤ y ≤ M) type, x and y are integers. The second line contains an integer K (1 ≤ K ≤ 10) — amount of trees, burning in the beginning.  The third line contains K pairs of integers: x1, y1, x2, y2, ..., xk, yk (1 ≤ xi ≤ N, 1 ≤ yi ≤ M) — coordinates of the points from which the fire started. It is guaranteed that no two points coincide.

Output a line with two space-separated integers x and y — coordinates of the tree that will be the last one to start burning. If there are several such trees, output any.

class Rect
	def initialize(x, y, z)
		@a1 = x + y - z
		@a2 = x + y + z
		@b1 = x - y - z
		@b2 = x - y + z
		@valid = z >= 0
	end

	[:a1, :a2, :b1, :b2].each do |i|
		class_eval <<-"end_eval"
			def #{i}
				@#{i}
			end

			attr_writer :#{i}
		end_eval
	end

	def valid?
		@valid &&= (@a1 <= @a2 && @b1 <= @b2)
	end

	def invalid!
		@valid = false
	end

	def cut!(x1, y1, x2, y2)
		@a1 = [@a1, x1 + y1].max
		@a2 = [@a2, x2 + y2].min
		@b1 = [@b1, x1 - y2].max
		@b2 = [@b2, x2 - y1].min
	end

	def **(o)
		!(@a2 <= o.a1 || o.a2 <= @a1) && !(@b2 <= o.b1 || o.b2 <= @b1)
	end

	def /(o)
		r = []
		if @a1 <= o.a1
			r << self.clone
			r[-1].a2 = o.a1
			@a1 = o.a1
		end
		if @a2 >= o.a2
			r << self.clone
			r[-1].a1 = o.a2
			@a2 = o.a2
		end
		if @b1 <= o.b1
			r << self.clone
			r[-1].b2 = o.b1
			@b1 = o.b1
		end
		if @b2 >= o.b2
			r << self.clone
			r[-1].b1 = o.b2
			@b2 = o.b2
		end
		r
	end
end

def gao(n, m, s, a)
	b = [Rect.new(0, 0, 10000)]
	a.each do |x, y|
		c = Rect.new(x, y, s)
		b.size.times do |j|
			next unless b[j] ** c
			b += b[j] / c
			b[j].invalid!
		end
		b.reject!{|_| !_.valid?}
	end
	b.map!{|_| _ / _}.flatten!
	b.each do |_|
		if _.a1 == _.a2
			s = _.a1
			_.cut!([1, s - m].max, [1, s - n].max, [n, s - 1].min, [m, s - 1].min)
			_.b1 += 1 unless s.odd? == _.b1.odd?	## !!
			_.b2 -= 1 unless s.odd? == _.b2.odd?
		else
			s = _.b1
			_.cut!([1, 1 + s].max, [1, 1 - s].max, [n, m + s].min, [m, n - s].min)
			_.a1 += 1 unless s.odd? == _.a1.odd?	## !!
			_.a2 -= 1 unless s.odd? == _.a2.odd?
		end
	end
	b.reject!{|_| !_.valid?}
end

I = open('input.txt')
O = open('output.txt', 'w')

n, m = I.gets.split.map(&:to_i)
k = I.gets.to_i
t = I.gets.split.map(&:to_i)
a = k.times.map{|i| [t[i + i], t[i + i + 1]]}

l = 0
r = n + m
while l < r do
	s = (l + r) / 2
	b = gao(n, m, s, a)
	unless b.empty? then
		l = s + 1
	else
		r = s
	end
end
r -= 1

if r == 0
	# RE8
	O.puts "1 1"
else
	q = []
	gao(n, m, r, a).each do |_|
		_.a1.upto(_.a2) do |i|
			_.b1.upto(_.b2) do |j|
			#	q << [(i + j) / 2, (i - j) / 2] if (i + j).even? && (i - j).even?
				if (i + j).even? && (i - j).even?
					O.puts "#{(i + j) / 2} #{(i - j) / 2}"
					exit
				end
			end
		end
	end
end
# q.sort!.uniq!
# O.puts "#{q[0][0]} #{q[0][1]}"

class Rect
	def initialize(x, y, z)
		@a1 = x + y - z
		@a2 = x + y + z
		@b1 = x - y - z
		@b2 = x - y + z
		@valid = z >= 0
	end

	[:a1, :a2, :b1, :b2].each do |i|
		class_eval <<-"end_eval"
			def #{i}
				@#{i}
			end

			attr_writer :#{i}
		end_eval
	end

	def valid?
		@valid &&= (@a1 <= @a2 && @b1 <= @b2)
	end

	def invalid!
		@valid = false
	end

	def cut!(x1, y1, x2, y2)
		@a1 = [@a1, x1 + y1].max
		@a2 = [@a2, x2 + y2].min
		@b1 = [@b1, x1 - y2].max
		@b2 = [@b2, x2 - y1].min
	end

	def **(o)
		!(@a2 <= o.a1 || o.a2 <= @a1) && !(@b2 <= o.b1 || o.b2 <= @b1)
	end

	def /(o)
		r = []
		if @a1 <= o.a1
			r << self.clone
			r[-1].a2 = o.a1
			@a1 = o.a1
		end
		if @a2 >= o.a2
			r << self.clone
			r[-1].a1 = o.a2
			@a2 = o.a2
		end
		if @b1 <= o.b1
			r << self.clone
			r[-1].b2 = o.b1
			@b1 = o.b1
		end
		if @b2 >= o.b2
			r << self.clone
			r[-1].b1 = o.b2
			@b2 = o.b2
		end
		r
	end
end

def gao(n, m, s, a)
	b = [Rect.new(0, 0, 10000)]
	a.each do |x, y|
		c = Rect.new(x, y, s)
		b.size.times do |j|
			next unless b[j] ** c
			b += b[j] / c
			b[j].invalid!
		end
		b.reject!{|_| !_.valid?}
	end
	b.map!{|_| _ / _}.flatten!
	b.each do |_|
		if _.a1 == _.a2
			s = _.a1
			_.cut!([1, s - m].max, [1, s - n].max, [n, s - 1].min, [m, s - 1].min)
		else
			s = _.b1
			_.cut!([1, 1 + s].max, [1, 1 - s].max, [n, m + s].min, [m, n - s].min)
		end
	end
	b.reject!{|_| !_.valid?}
end

I = open('input.txt')
O = open('output.txt', 'w')

n, m = I.gets.split.map(&:to_i)
k = I.gets.to_i
t = I.gets.split.map(&:to_i)
a = k.times.map{|i| [t[i + i], t[i + i + 1]]}

l = 0
r = n + m
while l < r do
	s = (l + r) / 2
	b = gao(n, m, s, a)
	unless b.empty? then
		l = s + 1
	else
		r = s
	end
end
r -= 1

if r <= 1
	# RE8
	O.puts "1 1"
else
	q = []
	gao(n, m, r, a).each do |_|
		_.a1.upto(_.a2) do |i|
			_.b1.upto(_.b2) do |j|
			#	q << [(i + j) / 2, (i - j) / 2] if (i + j).even? && (i - j).even?
				if (i + j).even? && (i - j).even?
					O.puts "#{(i + j) / 2} #{(i - j) / 2}"
					exit
				end
			end
		end
	end
end
# q.sort!.uniq!
# O.puts "#{q[0][0]} #{q[0][1]}"