We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t Т-prime, if t has exactly three distinct positive divisors.You are given an array of n positive integers. For each of them determine whether it is Т-prime or not.

The first line contains a single positive integer, n (1 ≤ n ≤ 105), showing how many numbers are in the array. The next line contains n space-separated integers xi (1 ≤ xi ≤ 1012). Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is advised to use the cin, cout streams or the %I64d specifier.

Print n lines: the i-th line should contain "YES" (without the quotes), if number xi is Т-prime, and "NO" (without the quotes), if it isn't.

NoteThe given test has three numbers. The first number 4 has exactly three divisors — 1, 2 and 4, thus the answer for this number is "YES". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is "NO".

n = gets.to_i
x = gets.split.map &:to_i

max = x.max

def sieve_numbers n
  sieve_array = Array.new(n + 1, true)
  sieve_array[0] = false
  sieve_array[1] = false

  for current_prime in 2 .. Math.sqrt(n).to_i
    next unless sieve_array[current_prime]
    i = 2
    while current_prime * i <= n
      sieve_array[current_prime * i] = false
      i += 1
    end
  end

  return sieve_array
end

def t_prime? sieve_array, n
  n_sqrt = Math.sqrt(n).to_i
  return n_sqrt * n_sqrt == n && sieve_array[n_sqrt]
end

primes = sieve_numbers(Math.sqrt(max).ceil)

x.each do |test_case|
  if t_prime? primes, test_case
    puts "YES"
  else
    puts "NO"
  end
end

MAXP = 1e6.to_i
is_prime = Array.new MAXP, true
t_primes = {}
(2...MAXP).each do |i|
  if is_prime[i] then
    t_primes[i * i] = 0
    ((i + i)...MAXP).step(i).each do |j|
      is_prime[j] = false
    end
  end
end
n = gets
x = gets.split.map &:to_i
x.each do |i|
  if t_primes.has_key? i then
    puts 'YES'
  else
    puts 'NO'
  end
end

#!/usr/bin/ruby

limit = 10**6 + 1
is_prime = {}
sieve = Array.new(limit)
pos = 2
while true
	while pos != limit and sieve[pos]
		pos += 1
	end
	break if pos == limit
	seek = pos
	while seek < limit
		sieve[seek] = true
		seek += pos
	end
	is_prime[pos] = true
end

n = STDIN.readline.to_i
arr = STDIN.readline.split.map {|s| s.to_i }
lines = []
arr.each do |x|
	y = (x**0.5).floor
	if y*y == x and is_prime[y]
		lines.push('YES')
	else
		lines.push('NO')
	end
end
puts lines.join("\n")

gets.to_i;
arr = gets.chomp.split(' ').map(&:to_i);
primes = Array.new(1000000, 1);
primes[1] = nil;
(2..1000000).each do |i|
  if primes[i]
    (i**2).step(1000000, i) {|m| primes[m] = nil}
  end
end

arr.each {|x|
  sq = Math.sqrt(x).to_i;
  puts (sq**2 == x && primes[sq] ? "YES" : "NO")
}

a=Array.new(1000000,0)
a[0]=1
for i in 2...1000000
	if a[i-1]==0
		j=2
		while i*j<=1000000
			a[i*j-1]=1
			j+=1
		end
	end
end
t=gets.to_i
ar=gets.chomp.split.map(&:to_i)
ar.each do |i|
	j=Math.sqrt(i).to_i
	puts (a[j-1]==0 && j*j==i) ? "YES" : "NO"
end

def eratosthenes(top)
  sieve = []
  for i in 2 .. top
    sieve[i] = i
  end
  for i in 2 .. Math.sqrt(top)
    next unless sieve[i]
    (i*i).step(top, i) do |j|
      sieve[j] = nil
    end
  end
  sieve
end
e = eratosthenes(1_000_000)
gets
gets.split.map(&:to_i).each do |x|
  f = Math.sqrt(x)
  if f == f.round and !e[f].nil?
    puts :YES
  else
    puts :NO
  end
end

require 'set'

n = gets.to_i

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

N = 10 ** 6
M = 10 ** 3

sieve = Array.new(N+1,true)

S = Set.new

(2..N).each do |i|
  next if not sieve[i]
  S.add(i ** 2)
  m = 2 * i
  while m <= N
    sieve[m] = false
    m += i
  end
end

x.each{|e| S.member?(e) ? (puts 'YES') : (puts 'NO')}

#!/usr/bin/env ruby
def prime(n)
  all_nums = (0..n).to_a
  all_nums[0] = all_nums[1] = nil
  all_nums.each do |p|
    next unless p
    break if p * p > n
    (p*p).step(n, p){ |m| all_nums[m] = nil }
  end
  all_nums.compact
end

amount = 1000000
primes = prime(amount)

t = {}
primes.map{|p| t[p*p]=true }

col = gets.to_i
arr = gets.split(" ").map{|p| p.to_i }

(0..col-1).each do |i|
	if t[arr[i]] then
		puts "YES"
	else
		puts "NO"
	end
end

require 'prime'

c=(0..10**6).map{0}
c[0]=1
c[1]=1
b=2
while b*b<=(10**6)
	if c[b]==0 then
		b2=b+b
		while b2<=10**6
			c[b2]=1
			b2=b2+b
		end
	end
	b=b+1
end
gets
x=gets.split.map{|e| e.to_i}

x.each{|e|
	m=(e**0.5).to_i
	if m*m==e && c[m]==0 then
		puts "YES"
	else
		puts "NO"
	end
}

a = Array.new(1000001,0)
a[1] = -1
i = 3
while i < 1000
	if a[i] == 0
		j = i+i
		while j < 1000000
			a[j] = -1
			j += i
		end
	end
	i += 2
end
n = gets.to_i
gets.split.map(&:to_i).each do |i|
	x = Math.sqrt(i)
	if x % 1 == 0
		if x % 2 == 0 and x != 2
			puts 'NO'
		else
			if a[x] == -1
				puts 'NO'
			else
				puts 'YES'
			end
		end

	else
		puts 'NO'
	end
end

#!/usr/bin/ruby

N = Integer gets.chomp
numbers = gets.chomp.split
for i in 0...N do
	numbers[i] = Integer numbers[i]
end

X = 1000000
flags = Array.new(X+1)
for i in 0..X do
	flags[i] = true
end
flags[0] = flags[1] = false
for i in 2..X do
	if flags[i]
		j = i * i
		while (j <= X) do
			flags[j] = false
			j += i;
		end
	end
end

for i in numbers do
	j = Integer Math.sqrt(i)
	if flags[j] and j * j == i
		print "YES\n"
	else
		print "NO\n"
	end
end

def generate_prime(n)
	primes = (0..n).to_a
	primes[0] = nil
	primes[1] = nil
	(2..Math.sqrt(n)).each do |i|
		if primes[i]
			(i**2..n).step(i).each do |j|
				primes[j] = nil
			end
		end
	end
	primes.compact
end

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
@primes = generate_prime(10 ** 6).map{|x| x *x}
true_arr = @primes & arr

true_hash = Hash[true_arr.map{|x| [x,true]}]
#d puts "#{true_hash}"
res = []
arr.each do |a|
	res << (true_hash[a] ? "YES" : "NO")
end

res.each do |i|
	puts i
end

def Eratosthenes2(n)
	l = Array 0..n

	l[0] = l[1] = nil

	for i in (2..n ** (1.0 / 2))
		if l[i]
			j = i ** 2
			while j <= n
				l[j] = nil
				j += i
			end
		end
	end

	return l.compact.map { |x| x ** 2 }
end

l = Eratosthenes2(1000000)

class Array
	def bs(x)

		s = 0
		e = size - 1

		while s <= e
			t = at((s + e) / 2)

			if x < t
				e = (s + e) / 2 - 1
			elsif x > t
				s = (s + e) / 2 + 1
			else
				return t
			end
		end

		return nil
	end
end

gets
gets.split.each { |x| puts (l.bs(x.to_i) != nil) ? "YES" : "NO" }

PRIME_LIMIT = Math.sqrt(10**12).to_i + 1
@prime_numbers = Array.new(PRIME_LIMIT, true)

def sieve
  @prime_numbers[1] = false
  i = 2
  while i*i < PRIME_LIMIT do
    if @prime_numbers[i] != false
      j = 2 * i
      while j < PRIME_LIMIT do
        @prime_numbers[j] = false
        j += i
      end
    end
    i+= 1
  end
end

sieve()

gets
gets
values = $_.split.map(&:to_i)
values.each do |value|
  svalue = Math.sqrt(value).to_i
  if (svalue * svalue == value && @prime_numbers[svalue] == true && value != 1)
    puts "YES"
  else
    puts "NO"
  end
end

require 'set'

tprimes = Set.new
sieve = Array.new(1000001,true)
sieve[0] = false
sieve[1] = false
(2..1000000).each do |p|
  next if sieve[p] == false
  i = p*p
  tprimes.add(i)
  while(i<=1000000)
    sieve[i] = false
    i += p
  end
end

gets.to_i
gets.split(/\s+/).each do |x|
  puts tprimes.member?(x.to_i) ? "YES" : "NO"
end

n = gets.to_i
a = gets.split.map(&:to_i)
c = Array.new(1000000, true)
c[1] = false
2.upto(1000) do |i|
  if c[i]
    (i * i).step(1000000, i){|j| c[j] = false}
  end
end
a.each do |x|
  m = Math.sqrt(x).round
  if m * m == x && c[m]
    puts 'YES'
  else
    puts 'NO'
  end
end

n = gets.to_i
a = gets.split.map(&:to_i)
c = Array.new(1000000, true)
c[1] = false
2.upto(1000000) do |i|
  if c[i]
    (i + i).step(1000000, i){|j| c[j] = false}
  end
end
a.each do |x|
  m = Math.sqrt(x)
  if m.ceil == m.to_i && c[m]
    puts 'YES'
  else
    puts 'NO'
  end
end

require 'set'

c = Array.new(1_000_001, false)
c[2] = true
p = [ 2 ]
3.step(1_000_000, 2) do |i|
  next if c[i]

  p << i

  i.step(1_000_000, i) { |x| c[x] = true }
end

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

pp = Set.new p.map { |x| x*x }

a.each do |n|
  puts pp.include?(n) ? 'YES' : 'NO'
end

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

L = []

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

(2..1000000).each do |i|
  kq = 2*i
  if L[i] == 1
    while kq <= 1000000 do
      L[kq] = 0
      kq += i
    end
  end
end

(0..n-1).each do |i|
  can = Math.sqrt(a[i])
  if can == can .ceil && L[can] == 1
    puts "YES"
  else
    puts "NO"
  end
end

n = gets.to_i
a = gets.split(' ').map{|i| i.to_i}
L = Array.new

(1..1000000).each do |i|
  L[i] = 1
end

L[1] = 0

(2..1000000).each do |i|
  kq = 2*i
  if L[i] == 1
    while kq <= 1000000 do
      L[kq] = 0
      kq += i
    end
  end
end

a.each do |i|
    if Math.sqrt(i.to_f) == Math.sqrt(i.to_f).floor
        f = Math.sqrt(i.to_f).to_i
        if L[f] == 1
            puts 'YES'
        else
            puts 'NO'
        end
    else
        puts 'NO'
    end
end

# require 'prime'

n = gets.to_i
f = gets.split.map { |x| x.to_i }

# p = Prime.first(700000)
is_prime = Array.new(1000001, true)
is_prime[0] = is_prime[1] = false
2.upto(1000001) do |i|
	if is_prime[i]
		j = i * 2
		while j < 1000001
			is_prime[j] = false
			j += i
		end
	end
end

f.each do |x|
	t = (x ** 0.5).round
	if t ** 2 == x and is_prime[t]
		puts 'YES'
	else
		puts 'NO'
	end
end

n = gets.to_i
x = gets.split.map &:to_i

max = x.max

def sieve_numbers n
  sieve_array = Array.new(n + 1, true)
  sieve_array[0] = false
  sieve_array[1] = false

  for current_prime in 2 .. Math.sqrt(n).to_i
    next unless sieve_array[current_prime]
    i = 2
    while current_prime * i <= n
      sieve_array[current_prime * i] = false
      i += 1
    end
  end

  prime_numbers = []
  sieve_array.each_index do |index|
    prime_numbers << index if sieve_array[index]
  end
  return prime_numbers
end

primes = sieve_numbers(Math.sqrt(max).ceil)
squared_primes = primes.map{|p| p * p}

x.each do |test_case|
  if squared_primes.bsearch{|element| test_case - element}
    puts "YES"
  else
    puts "NO"
  end
end

gets.to_i;
arr = gets.chomp.split(' ').map(&:to_i);
primes = Array.new(1000000, 1);
(2..1000000).each do |i|
  if primes[i]
    (i**2).step(1000000, i) {|m| primes[m] = nil}
  end
end

arr.each {|x|
  sq = Math.sqrt(x).to_i;
  puts (sq**2 == x && primes[sq] ? "YES" : "NO")
}

require 'prime'

gets.to_i;
arr = gets.chomp.split(' ').map(&:to_i);
primes = Prime.each(100000).map {|x| [x, 1]};
hash = Hash[*primes.flatten];

arr.each { |x|
  sq = Math.sqrt(x).to_i;
  if (sq*sq == x && hash[sq])
    puts "YES";
  else
    puts "NO";
  end
}

require 'mathn'
n = gets.to_i
n.times do
  f = Math.sqrt(gets.to_i)
  puts (f == f.round and f.prime?) ? :YES : :NO
end

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

ind = 0
d = Array.new(100001,false)
ok = Array.new(100001)

for i in 2..1000001 do
    if (d[i] == false)
        ind += 1
        ok[ind] = i*i
        j = i+i
        begin
          d[j] = true
          j += i
          if (j > 100001) then break end
        end while true
    end
end

for i in 0..n-1 do
    l = 0x0
    r = 0x0
    l = 1.to_i
    r = ind.to_i
    ans = false
    begin
        if (l>=r) then break end
        mid = (l+r) / 2
        if (ok[mid] > a[i]) then r = mid end
        if (ok[mid] < a[i]) then l = mid+1 end
        if (ok[mid] == a[i]) then ans = true end
        if (ans == true) then break end
    end while true
    if (ok[l] == a[i]) then ans = true end
    if (ans == true) then  puts "YES"
        else puts "NO"
    end
end

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

ind = 0
d = Array.new(1000001,false)
ok = Array.new(0)

for i in 2..1000001 do
    if (d[i] == false)
        ind += 1
        ok.push(i*i)
        j = i+i
        begin
          d[j] = true
          j += i
          if (j > 1000001) then break end
        end while true
    end
end

for i in 0..n-1 do
    l = 0x0
    r = 0x0
    l = 1.to_i
    r = ind.to_i
    ans = false
    begin
        if (l>=r) then break end
        mid = (l+r) / 2
        if (ok[mid] > a[i]) then r = mid end
        if (ok[mid] < a[i]) then l = mid+1 end
        if (ok[mid] == a[i]) then ans = true end
        if (ans == true) then break end
    end while true
    if (ok[l] == a[i]) then ans = true end
    if (ans == true) then  puts "YES"
        else puts "NO"
    end
end

a = Array.new(1000001,0)
a[1] = -1
i = 3
while i < 1000
	if a[i] == 0
		j = i+i
		while j < 1000000
			a[j] = -1
			j += j
		end
	end
	i += 2
end
n = gets.to_i
gets.split.map(&:to_i).each do |i|
	x = Math.sqrt(i)
	if x % 1 == 0
		if x % 2 == 0 and x != 2
			puts 'NO'
		else
			if a[x] == -1
				puts 'NO'
			else
				puts 'YES'
			end
		end

	else
		puts 'NO'
	end
end

#!/usr/bin/ruby

N = Integer gets.chomp
numbers = gets.chomp.split
for i in 0...N do
	numbers[i] = Integer numbers[i]
end

X = 100000
flags = Array.new(X+1)
for i in 0..X do
	flags[i] = true
end
flags[0] = flags[1] = false
for i in 2..X do
	if flags[i]
		j = i * i
		while (j <= X) do
			flags[j] = false
			j += i;
		end
	end
end

for i in numbers do
	j = Integer Math.sqrt(i)
	if flags[j] and j * j == i
		print "YES\n"
	else
		print "NO\n"
	end
end

#!/usr/bin/ruby

N = Integer gets.chomp
numbers = gets.chomp.split
for i in 0...N do
	numbers[i] = Integer numbers[i]
end

X = 100000
flags = Array.new(X+1)
for i in 0..X do
	flags[i] = true
end
flags[0] = flags[1] = false
primes = Array.new
for i in 2..X do
	if flags[i]
		primes.push i
		j = i * i
		while (j <= X) do
			flags[j] = false
			j += i;
		end
	end
end

for i in numbers do
	is_prime = i != 1
	for j in primes do
		if j * j > i
			break
		end
		if i % j == 0
			is_prime = false
			break
		end
	end
	if is_prime
		print "YES\n"
	else
		print "NO\n"
	end
end

def generate_prime(n)
	primes = Hash.new(true)
	primes[0] = false
	primes[1] = false
	(2..Math.sqrt(n)).each do |i|
		if primes[i]
			primes[i] = true
			(i**2..n).step(i).each do |j|
				primes[j] = false
			end
		end
	end
	primes.select {|k,v| v == true}.keys
end

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
@primes = generate_prime(10**6)
arr.each do |a|
	# puts @primes.include?(Math.sqrt(a)) ? "YES" : "NO"
end

def Eratosthenes2(n)
	l = Array 0..n

	l[0] = l[1] = nil

	for i in (2..n ** (1.0 / 2))
		if l[i]
			j = i ** 2
			while j <= n
				l[j] = nil
				j += i
			end
		end
	end

	return l.compact.map { |x| x ** 2 }
end

l = Eratosthenes2(1000000)

gets
gets.split.each { |x| puts (l[0] == x.to_i or l.bsearch { |y| y == x.to_i } != nil) ? "YES" : "NO" }

n = gets.to_i
a = gets.split.map(&:to_i)
c = Array.new(10, true)
2.upto(10) do |i|
  if c[i]
    i.step(10, i){|j| c[j] = false if j != i}
  end
end
a.each do |x|
  m = Math.sqrt(x)
  if m.ceil == m.to_i && c[m]
    puts 'YES'
  else
    puts 'NO'
  end
end

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

a.each do |x|
  m = x.prime_division
  if m.size == 1 && m[0][0] == m[0][1]
    puts 'YES'
  else
    puts 'NO'
  end
end

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

a.each do |x|
  m = x.prime_division
  if m.size == 1 && m[0][0] != x
    puts 'YES'
  else
    puts 'NO'
  end
end

n = gets.to_i
a = gets.split.map(&:to_i)
c = Array.new(10, true)
c[1] = false
2.upto(10) do |i|
  if c[i]
    i.step(10, i){|j| c[j] = false if j != i}
  end
end
a.each do |x|
  m = Math.sqrt(x)
  if m.ceil == m.to_i && c[m]
    puts 'YES'
  else
    puts 'NO'
  end
end

require 'set'

def t_prime(x)
  result = false
  $p.each do |i|
    break if i > x
    if x.modulo(i).zero? && x.div(i) > 1 && result == false
      result = true
    elsif x.modulo(i).zero?
      return false
    end
  end
  result
end

c = Set.new [ 2 ]
$p = [ 2 ]
i = 1
until i > 1_000_000
  i += 2

  next if c.include? i

  x = i
  until x > 1_000_000
    c << x
    x += i
  end
end

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

a.each do |n|
  puts t_prime(n) ? 'YES' : 'NO'
end

require 'set'

def t_prime(x)
  result = false
  $p.each do |i|
    break if i > x
    if x.modulo(i).zero? && x.div(i) > 1 && result == false
      result = true
    elsif x.modulo(i).zero?
      return false
    end
  end
  result
end

c = Set.new [ 2 ]
$p = [ 2 ]
c << 2
i = 1
until i > 1_000_000
  i += 2

  next if c.include? i

  $p << i

  x = i
  until x > 1_000_000
    c << x
    x += i
  end
end

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

a.each do |n|
  puts t_prime(n) ? 'YES' : 'NO'
end

n = gets.to_i
b = gets.split(' ')

a = []

(0..n-1).each do |i|
    a[i] = b[i].to_f
    if Math.sqrt(a[i]) == Math.sqrt(a[i]).ceil
        puts "YES"
    else
        puts "NO"
    end
end

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

(0..n-1).each do |i|
    dem = 0
    can = Math.sqrt(a[i])
  (2..can.floor).each do |j|
    dem +=1 if a[i]%j == 0
  end
  if can == can.floor
    if 2*dem + 1 == n
        puts "YES"
    else
        puts "NO"
    end
  else
    if 2*dem + 2 == n
        puts "YES"
    else
        puts "NO"
    end
  end
end

n = gets.to_i
b = gets.split(' ')

a = []
kt = 1

(0..n-1).each do |i|
    kt = 1
    a[i] = b[i].to_f
    can = Math.sqrt(a[i])
    if can == can.ceil
        (2..Math.sqrt(can).floor).each do |i|
            kt = 0 if can%i == 0
        end
    else kt = 0
    end
    if kt == 0
        puts "NO"
    else
        puts "YES"
    end
end

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

(0..n-1).each do |i|
  kt = 1
  if a[i] < 4
    puts "NO"
  else
    if a[i] == 4
      puts "YES"
    else
        can = Math.sqrt(a[i]).to_f
      if (can.ceil == can)
        (2..Math.sqrt(can).floor).each do |j|
          if can%j == 0
            puts "NO"
            kt = 0
          end
        end
        if kt == 1
          puts "YES"
        end
      else
        puts "NO"
      end
    end
  end
end

n = gets.to_i
b = gets.split(' ')

a = []

(0..n-1).each do |i|
    a[i] = b[i].to_f
    dem = 0
    can = Math.sqrt(a[i])
  (2..can.floor).each do |j|
    dem +=1 if a[i]%j == 0
  end
  if can == can.floor
    if 2*dem + 1 == n
        puts "YES"
    else
        puts "NO"
    end
  else
    if 2*dem + 2 == n
        puts "YES"
    else
        puts "NO"
    end
  end
end

n = gets.to_i
b = gets.split(' ')

a = []

(0..n-1).each do |i|
    kt = 0
    a[i] = b[i].to_f
    (2..Math.sqrt(a[i]).floor).each do |j|
        if (a[i]%j == 0)
            kq = a[i]/j
            while kq > 1 do
                if kq %j != 0
                    break
                else
                    kq = kq/j
                end
            end
            kt = 1 if kq == 1
            break
        end
    end
    if kt == 0
        puts "NO"
    else
        puts "YES"
    end
end

require 'set'

n = gets.to_i

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

N = 10 ** 6
M = 10 ** 3

sieve = Array.new(N+1,true)

S = Set.new

(2..N).each do |i|
  next if not sieve[i]
  S.add(i ** 2)
  m = 2 * i
  while m < N
    sieve[m] = false
    m += i
  end
end

x.each{|e| S.member?(e) ? (puts 'YES') : (puts 'NO')}

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

a.each do |i|
    if Math.sqrt(i.to_f) == Math.sqrt(i.to_f).floor && Math.sqrt(i.to_f).floor > 1
        f = Math.sqrt(i.to_f).to_i
        g = Math.sqrt(f).to_i
        puts 'YES'

    else
        puts 'NO'
    end
end

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

a.each do |i|
	if Math.sqrt(i.to_f) == Math.sqrt(i.to_f).floor && Math.sqrt(i.to_f).floor > 1
		f = Math.sqrt(i.to_f).to_i
		if f == 2 || f == 3
			puts 'YES'
		elsif f > 3
			g = Math.sqrt(f)
			m = 0
			(2..g).each do |t|
				if f%t == 0
					puts 'NO'
					break
				end
				m = t
			end
			if m == g
				puts 'YES'
			end
		end
	else
		puts 'NO'
	end
end

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

a.each do |i|
    if Math.sqrt(i.to_f) == Math.sqrt(i.to_f).floor && Math.sqrt(i.to_f).floor > 1
        f = Math.sqrt(i.to_f).to_i
        if f == 2 || f == 3
            puts 'YES'
        elsif f > 3
            g = Math.sqrt(f).floor
            m = 0
            (2..g).each do |t|
                if f%g == 0
                    puts 'NO'
                    break
                end
                m = t
            end
            if m == g
                puts 'YES'
            end
        end
    else
        puts 'NO'
    end
end

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

a.each do |i|
    if Math.sqrt(i.to_f) == Math.sqrt(i.to_f).floor && Math.sqrt(i.to_f).floor > 1
        f = Math.sqrt(i.to_f).to_i
        if f == 2 || f == 3
            puts 'YES'
        elsif f > 3
            g = (f/2).floor
            m = 0
            (2..g).each do |t|
                if f%g == 0
                    puts 'NO'
                    break
                end
                m = t
            end
            if m == g
                puts 'YES'
            end
        end
    else
        puts 'NO'
    end
end

# require 'prime'

n = gets.to_i
f = gets.split.map { |x| x.to_i }

# p = Prime.first(700000)
is_prime = Array.new(1000001, true)
is_prime[0] = false
2.upto(1000001) do |i|
	if is_prime[i]
		j = i * 2
		while j < 1000001
			is_prime[j] = false
			j += i
		end
	end
end

f.each do |x|
	t = (x ** 0.5).round
	if t ** 2 == x and is_prime[t]
		puts 'YES'
	else
		puts 'NO'
	end
end


Along the railroad there are stations indexed from $$$1$$$ to $$$10^9$$$. An express train always travels along a route consisting of $$$n$$$ stations with indices $$$u_1, u_2, \dots, u_n$$$, where ($$$1 \le u_i \le 10^9$$$). The train travels along the route from left to right. It starts at station $$$u_1$$$, then stops at station $$$u_2$$$, then at $$$u_3$$$, and so on. Station $$$u_n$$$ — the terminus.It is possible that the train will visit the same station more than once. That is, there may be duplicates among the values $$$u_1, u_2, \dots, u_n$$$.You are given $$$k$$$ queries, each containing two different integers $$$a_j$$$ and $$$b_j$$$ ($$$1 \le a_j, b_j \le 10^9$$$). For each query, determine whether it is possible to travel by train from the station with index $$$a_j$$$ to the station with index $$$b_j$$$.For example, let the train route consist of $$$6$$$ of stations with indices [$$$3, 7, 1, 5, 1, 4$$$] and give $$$3$$$ of the following queries:   $$$a_1 = 3$$$, $$$b_1 = 5$$$It is possible to travel from station $$$3$$$ to station $$$5$$$ by taking a section of the route consisting of stations [$$$3, 7, 1, 5$$$]. Answer: YES.  $$$a_2 = 1$$$, $$$b_2 = 7$$$You cannot travel from station $$$1$$$ to station $$$7$$$ because the train cannot travel in the opposite direction. Answer: NO.  $$$a_3 = 3$$$, $$$b_3 = 10$$$It is not possible to travel from station $$$3$$$ to station $$$10$$$ because station $$$10$$$ is not part of the train's route. Answer: NO.

The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) —the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case is empty. The second line of each test case contains two integers: $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5, 1 \le k \le 2 \cdot 10^5$$$) —the number of stations the train route consists of and the number of queries. The third line of each test case contains exactly $$$n$$$ integers $$$u_1, u_2, \dots, u_n$$$ ($$$1 \le u_i \le 10^9$$$). The values $$$u_1, u_2, \dots, u_n$$$ are not necessarily different. The following $$$k$$$ lines contain two different integers $$$a_j$$$ and $$$b_j$$$ ($$$1 \le a_j, b_j \le 10^9$$$) describing the query with index $$$j$$$. It is guaranteed that the sum of $$$n$$$ values over all test cases in the test does not exceed $$$2 \cdot 10^5$$$. Similarly, it is guaranteed that the sum of $$$k$$$ values over all test cases in the test also does not exceed $$$2 \cdot 10^5$$$

For each test case, output on a separate line:   YES, if you can travel by train from the station with index $$$a_j$$$ to the station with index $$$b_j$$$ NO otherwise.  You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).

NoteThe first test case is explained in the problem statement.

require "set"

t = gets.to_i

t.times do
  gets

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

  lefts = Hash.new
  rights = Hash.new

  arr.each_with_index do |a, i|
    lefts[a] = i unless lefts[a]
    rights[a] = i
  end

  k.times do
    l, r = gets.split.map(&:to_i)

    if !lefts[l] || !rights[r]
      print "NO"
    elsif lefts[l] <= rights[r]
      print "YES"
    else
      print "NO"
    end

    puts
  end
end


Iahub and Iahubina went to a date at a luxury restaurant. Everything went fine until paying for the food. Instead of money, the waiter wants Iahub to write a Hungry sequence consisting of n integers. A sequence a1, a2, ..., an, consisting of n integers, is Hungry if and only if:   Its elements are in increasing order. That is an inequality ai < aj holds for any two indices i, j (i < j).  For any two indices i and j (i < j), aj must not be divisible by ai. Iahub is in trouble, so he asks you for help. Find a Hungry sequence with n elements.

The input contains a single integer: n (1 ≤ n ≤ 105).

Output a line that contains n space-separated integers a1 a2, ..., an (1 ≤ ai ≤ 107), representing a possible Hungry sequence. Note, that each ai must not be greater than 10000000 (107) and less than 1. If there are multiple solutions you can output any one.

a=gets.chomp.to_i
ans=a*3
0.upto(a-1) do |i|
print "#{ans+i} "
end

n = Integer(gets)

b = Array.new
b[0] = 2
b[1] = 3
for i in 0...10000
    c = 0
    for j in 0...b.length
        if (i + 2)% b[j] != 0
            c = 1
        else
            c = 0
            break
        end
    end
    if c == 1
        b.push(i + 2)
    end
end
c = Array.new

for i in 0...b.length
    for j in i...b.length
         c.push(b[i]*b[j])

        if c.length >= n
            break
        end
    end
    if c.length >= n
            break
        end
end
c = c.sort
puts c

n = Integer(gets)

b = Array.new
b[0] = 2
b[1] = 3
for i in 0...10000
    c = 0
    for j in 0...b.length
        if (i + 2)% b[j] != 0
            c = 1
        else
            c = 0
            break
        end
    end
    if c == 1
        b.push(i + 2)
    end
end
c = Array.new

for i in 0...b.length
    for j in i...b.length
         c.push(b[i]*b[j])

        if c.length >= n
            break
        end
    end
end
c = c.sort
for i in 0...n
    puts c[i]
end

n = gets.strip.to_i
a = []
n.times do |i|
  a << (10000000 - n + i)
end
puts a.join(" ")

def eratosthenes(n)
  nums = [true, false, true] * ((n + 5) / 6)
  nums[0] = false
  nums[1] = true

  i = 5
  until(m = i * i
  m > n) do
  if nums[i >> 1]
    i_times_2 = i << 1
    i_times_4 = i << 2
    while m <= n
      nums[m >> 1] = false
      m += i_times_2
      nums[m >> 1] = false
      m += i_times_4
    end
  end
  i += 2
  if nums[i >> 1]
    m = i * i
    i_times_2 = i << 1
    i_times_4 = i << 2
    while m <= n
      nums[m >> 1] = false
      m += i_times_4
      nums[m >> 1] = false
      m += i_times_2
    end
  end
  i += 4
  end

  primes = [2]
  nums.each_index {|i| primes << (i * 2 + 1) if nums[i]}
  primes.pop while primes.last > n
  return primes
end

puts eratosthenes(1299722)[0..gets.to_i-1].join(' ')

n = gets.to_i
MAX = 1300000
is_prime = Array.new(MAX + 1){true}
is_prime[0] = false
is_prime[1] = false
i = 2
while i * i <= MAX
    if is_prime[i]
        # puts i
        j = i * i
        while j < MAX + 1
            is_prime[j] = false
            j += i
        end
    end
    i += 1
end
res = []
is_prime.each_with_index{|x, i| res << i if x}
puts res[0...n].join(" ")

prime = Array.new(1300000, true)

prime[0] = prime[1] = false
2.upto(1300000) do |i|
	if prime[i]
		t = i * 2
		while t < 1300000
			prime[t] = false
			t += i
		end
	end
end

ans = []
n = gets.to_i
2.upto(1300000) do |i|
	if prime[i]
		if n > 0
			n -= 1
			ans << i
		end
	end
end

puts ans.join ' '

n = Integer(gets)

b = Array.new
b[0] = 2
b[1] = 3
for i in 0...4*n
    c = 0
    for j in 0...b.length
        if (i + 2)% b[j] != 0
            c = 1
        else
            c = 0
            break
        end
    end
    if c == 1
        b.push(i + 2)
    end
end
for i in 0...n
    print b[i], " "
end

n = Integer(gets)

b = Array.new
b[0] = 2
b[1] = 3
for i in 0...2*n
    c = 0
    for j in 0...b.length
        if (i + 2)% b[j] != 0
            c = 1
        else
            c = 0
            break
        end
    end
    if c == 1
        b.push(i + 2)
    end
end
for i in 0...n
    print b[i], " "
end

n = Integer(gets)

b = Array.new
b[0] = 2
b[1] = 3
for i in 0...n + 1000
    c = 0
    for j in 0...b.length
        if (i + 2)% b[j] != 0
            c = 1
        else
            c = 0
            break
        end
    end
    if c == 1
        b.push(i + 2)
    end
end
for i in 0...n
    print b[i], " "
end

n = Integer(gets)

b = Array.new
b[0] = 2
b[1] = 3
for i in 0...10000
    c = 0
    for j in 0...b.length
        if (i + 2)% b[j] != 0
            c = 1
        else
            c = 0
            break
        end
    end
    if c == 1
        b.push(i + 2)
    end
end
c = Array.new

for i in 0...b.length
    for j in i...b.length
         c.push(b[i]*b[j])

        if c.length >= n
            break
        end
    end
end
for i in 0...n
    puts c[i]
end

n = Integer(gets)

b = Array.new
b[0] = 2
b[1] = 3
for i in 0...n + 100
    c = 0
    for j in 0...b.length
        if (i + 2)% b[j] != 0
            c = 1
        else
            c = 0
            break
        end
    end
    if c == 1
        b.push(i + 2)
    end
end
for i in 0...n
    print b[i], " "
end

n = Integer(gets)

b = Array.new
b[0] = 2
b[1] = 3
for i in 0...6*n
    c = 0
    for j in 0...b.length
        if (i + 2)% b[j] != 0
            c = 1
        else
            c = 0
            break
        end
    end
    if c == 1
        b.push(i + 2)
    end
end
for i in 0...n
    print b[i], " "
end

n = Integer(gets)

b = Array.new
b[0] = 2
b[1] = 3
for i in 0...10000
    c = 0
    for j in 0...b.length
        if (i + 2)% b[j] != 0
            c = 1
        else
            c = 0
            break
        end
    end
    if c == 1
        b.push(i + 2)
    end
end
c = Array.new

for i in 0...b.length
    for j in i...b.length
         c.push(b[i]*b[j])

        if c.length >= n
            break
        end
    end
    if c.length >= n
            break
        end
end

puts c

n = Integer(gets)

b = Array.new
b[0] = 2
b[1] = 3
for i in 0...10000
    c = 0
    for j in 0...b.length
        if (i + 2)% b[j] != 0
            c = 1
        else
            c = 0
            break
        end
    end
    if c == 1
        b.push(i + 2)
    end
end
c = Array.new

for i in 0...b.length
    for j in 0...b.length
         c.push(b[i]*b[j])

        if c.length >= n
            break
        end
    end
end
for i in 0...n
    puts c[i]
end


Ivan wants to play a game with you. He picked some string $$$s$$$ of length $$$n$$$ consisting only of lowercase Latin letters. You don't know this string. Ivan has informed you about all its improper prefixes and suffixes (i.e. prefixes and suffixes of lengths from $$$1$$$ to $$$n-1$$$), but he didn't tell you which strings are prefixes and which are suffixes.Ivan wants you to guess which of the given $$$2n-2$$$ strings are prefixes of the given string and which are suffixes. It may be impossible to guess the string Ivan picked (since multiple strings may give the same set of suffixes and prefixes), but Ivan will accept your answer if there is at least one string that is consistent with it. Let the game begin!

The first line of the input contains one integer number $$$n$$$ ($$$2 \le n \le 100$$$) — the length of the guessed string $$$s$$$. The next $$$2n-2$$$ lines are contain prefixes and suffixes, one per line. Each of them is the string of length from $$$1$$$ to $$$n-1$$$ consisting only of lowercase Latin letters. They can be given in arbitrary order. It is guaranteed that there are exactly $$$2$$$ strings of each length from $$$1$$$ to $$$n-1$$$. It is also guaranteed that these strings are prefixes and suffixes of some existing string of length $$$n$$$.

Print one string of length $$$2n-2$$$ — the string consisting only of characters 'P' and 'S'. The number of characters 'P' should be equal to the number of characters 'S'. The $$$i$$$-th character of this string should be 'P' if the $$$i$$$-th of the input strings is the prefix and 'S' otherwise. If there are several possible answers, you can print any.

NoteThe only string which Ivan can guess in the first example is "ababa".The only string which Ivan can guess in the second example is "aaa". Answers "SPSP", "SSPP" and "PSPS" are also acceptable.In the third example Ivan can guess the string "ac" or the string "ca". The answer "SP" is also acceptable.

n = gets.to_i
str = []
(2*n-2).times do |i|
  str << [gets.chomp,i]
end

str.sort_by!{|s| s[0].length}

a,b,c,d = str[0][0],str[1][0],str[-1][0],str[-2][0]
guess = [a+c,a+d,b+c,b+d]

#p guess
str.each do |a|
  s = a[0]
  len = s.length
  guess.each do |t|
    guess.delete(t) if t[0,len] != s && t[n-len,len] != s
  end
  #p guess
end

origin = guess[0]

(n-1).times do |i|
  a,b = str[2*i][0],str[2*i+1][0]
  len = a.length

  #p [a,b,flg]
  if a == origin[0,len]
    str[2*i] << "P"
    str[2*i+1] << "S"
  else
    str[2*i] << "S"
    str[2*i+1] << "P"
  end
end
#p guess

str.sort_by!{|s| s[1]}
str.each do |a|
  print "#{a[2]}"
end

# Utility
class StringBuilder
  def initialize(chunks)
    chunks_sorted = chunks.map.with_index { |chunk, i| {i: i, str: chunk} }
                          .sort { |a, b| a[:str].length <=> b[:str].length }
    @pairs = Array.new(chunks_sorted.length / 2) { |i| [chunks_sorted[2 * i], chunks_sorted[2 * i + 1]] }

    @string = build

    @solution = "S" * chunks.length
    @pairs.each do |p|
      if @string.start_with? p[0][:str]
        @solution[p[0][:i]] = "P"
      else
        @solution[p[1][:i]] = "P"
      end
    end
  end

  attr_reader :string
  attr_reader :solution

  private
  def build
    str_1 = @pairs[-1][0][:str] + @pairs[-1][1][:str][-1]
    str_2 = @pairs[-1][1][:str] + @pairs[-1][0][:str][-1]
    return str_1 if correct? str_1
    return str_2 if correct? str_2
  end

  def correct?(str)
    (str.length - 1).times do |i|
      a = (str.start_with? @pairs[i][0][:str]) && (str.end_with? @pairs[i][1][:str])
      b = (str.start_with? @pairs[i][1][:str]) && (str.end_with? @pairs[i][0][:str])
      break false unless a || b
    end
  end
end

# Input
n = gets.to_i # 2 <= n <= 100
chunks = Array.new(2 * n - 2) { |i| gets.chop }

# Result
result = StringBuilder.new(chunks)
#puts result.string
puts result.solution

n = gets.to_i
arr = []
(0..n * 2 - 3).each do |i|
  arr.push([gets.chop, i])
end
brr = arr.sort_by{ |t| t.first.length }
c1 = brr[-1].first + brr[-2].first[-1]
c2 = brr[-2].first + brr[-1].first[-1]

def isPrefix(s, pr)
  s[0 .. pr.length - 1] == pr
end

def isSuffix(s, sf)
  s[-sf.length .. -1] == sf
end

res1 = "_" * (n * 2 - 2)
res2 = res1.dup

brr.each_slice(2) do |f, s|
  s1, n1 = f
  s2, n2 = s
  res1[n1], res1[n2] = "P", "S" if isPrefix(c1, s1) && isSuffix(c1, s2)
  res1[n1], res1[n2] = "S", "P" if isSuffix(c1, s1) && isPrefix(c1, s2)
  res2[n1], res2[n2] = "P", "S" if isPrefix(c2, s1) && isSuffix(c2, s2)
  res2[n1], res2[n2] = "S", "P" if isSuffix(c2, s1) && isPrefix(c2, s2)
end

if !res1.include? "_" then puts res1 else puts res2 end

n = gets.to_i
str = []
(2*n-2).times do |i|
  str << [gets.chomp,i]
end

str.sort_by!{|s| s[0].length}
guess = " "*n

((n-1)/2).times do |i|
  a,b,c,d = str[2*i][0],str[2*i+1][0],str[-2*i-1][0],str[-2*i-2][0]
  (i+1).times do |j|
    if a[j] == b[j]
      guess[j] = a[j]
      guess[n-i-1+j] = a[j]
    end
  end
  (n-i-1).times do |j|
    if c[j] == d[j]
      guess[j] = c[j]
      guess[i+j+1] = c[j]
    end
  end
  #p guess
end

(n-1).times do |i|
  a,b = str[2*i][0],str[2*i+1][0]
  flg1,flg2 = true,true
  (i+1).times do |j|
    flg1 = false if guess[j] != " " && guess[j] != a[j]
    flg2 = false if guess[j] != " " && guess[j] != b[j]
  end
  if flg1 && !flg2
    guess[0,i+1] = a
    guess[n-i-1,i+1] = b
  elsif !flg1 && flg2
    guess[0,i+1] = b
    guess[n-i-1,i+1] = a
  end
end
#p guess

(n-1).times do |i|
  a,b = str[2*i][0],str[2*i+1][0]
  flg = true
  (i+1).times do |j|
    flg = false if guess[j] != " " && guess[j] != a[j]
  end
  #p [a,b,flg]
  if flg
    str[2*i] << "P"
    str[2*i+1] << "S"
  else
    str[2*i] << "S"
    str[2*i+1] << "P"
  end
end
#p guess

str.sort_by!{|s| s[1]}
str.each do |a|
  print "#{a[2]}"
end

# Utility
class StringBuilder
  def initialize(chunks)
    chunks_sorted = chunks.map.with_index { |chunk, i| {i: i, str: chunk} }
                          .sort { |a, b| a[:str].length <=> b[:str].length }
    @pairs = Array.new(chunks_sorted.length / 2) { |i| [chunks_sorted[2 * i], chunks_sorted[2 * i + 1]] }

    @string = build

    @solution = "S" * chunks.length
    @pairs.each do |p|
      if @string.start_with? p[0][:str]
        @solution[p[0][:i]] = "P"
      else
        @solution[p[1][:i]] = "P"
      end
    end
  end

  attr_reader :string
  attr_reader :solution

  private
  def build
    str_1 = @pairs[-1][0][:str] + @pairs[-1][1][:str][-1]
    str_2 = @pairs[-1][1][:str] + @pairs[-1][0][:str][-1]
    return str_1 if correct? str_1
    return str_2 if correct? str_2
  end

  def correct?(str)
    (str.length - 1).times do |i|
      break false unless @pairs[i].find { |e| str.start_with? e[:str] }
    end
  end
end

# Input
n = gets.to_i # 2 <= n <= 100
chunks = Array.new(2 * n - 2) { |i| gets.chop }

# Result
result = StringBuilder.new(chunks)
#puts result.string
puts result.solution

# Utility
class StringBuilder
  def initialize(chunks)
    chunks_sorted = chunks.map.with_index { |chunk, i| {i: i, str: chunk} }
                          .sort { |a, b| a[:str].length <=> b[:str].length }
    pairs = Array.new(chunks_sorted.length / 2) { |i| [chunks_sorted[2 * i], chunks_sorted[2 * i + 1]].sort! { |a, b| a[:str] <=> b[:str] } }
    # Beacuse of sorting inside pairs
    #p pairs

    str_1 = pairs[-1][0][:str]
    str_2 = pairs[-1][1][:str]
    if str_1 + str_2[-1] == str_1[0] + str_2
      # str_1 is prefix
      @string = str_1 + str_2[-1]
    else
      # str_2 is prefix
      @string = str_2 + str_1[-1]
    end

    @solution = "S" * chunks.length
    pairs.each do |p|
      if @string.start_with? p[0][:str]
        @solution[p[0][:i]] = "P"
      else
        @solution[p[1][:i]] = "P"
      end
    end
  end

  attr_reader :string
  attr_reader :solution
end

# Input
n = gets.to_i # 2 <= n <= 100
chunks = Array.new(2 * n - 2) { |i| gets.chop }

# Result
result = StringBuilder.new(chunks)
puts result.string
puts result.solution

# Utility
class StringBuilder
  def initialize(chunks)
    @n = 1 + chunks.length / 2
    chunks_sorted = chunks.sort { |a, b| a[:str].length <=> b[:str].length }
    pairs = Array.new(chunks_sorted.length / 2) { |i| [chunks_sorted[2 * i], chunks_sorted[2 * i + 1]] }

    builder(pairs)
  end

  attr_reader :string
  attr_reader :solution

  private
  def builder(pairs, str = nil, prefix_indexes = [], last_char = nil)
    # Building from prefixes
    unless str
      # First call
      str = ""
    end

    if pairs.length == 0
      # Last call
      @string = str + last_char
      @solution = "S" * (2 * @n - 2)
      prefix_indexes.each { |i| @solution[i] = "P" }
      return @string
    end

    pair = pairs.shift
    str_ext = nil # Next string
    2.times do |i|
      next unless pair[i][:str].start_with?(str)
      #puts "#{str} => #{pair[i][:str]}"
      str_ext = builder(pairs, pair[i][:str], prefix_indexes + [pair[i][:i]], last_char || pair[1 - i][:str])
      break if str_ext # Built successfully
      #puts "X"
    end

    return str_ext
  end
end

# Input
n = gets.to_i # 2 <= n <= 100
chunks = Array.new(2 * n - 2) { |i| {i: i, str: gets.chop} }

# Result
result = StringBuilder.new(chunks)
#puts result.string
puts result.solution

# Utility
class StringBuilder
  def initialize(chunks)
    chunks_sorted = chunks.map.with_index { |chunk, i| {i: i, str: chunk} }
                          .sort { |a, b| a[:str].length <=> b[:str].length }
    pairs = Array.new(chunks_sorted.length / 2) { |i| [chunks_sorted[2 * i], chunks_sorted[2 * i + 1]].sort! { |a, b| a[:str] <=> b[:str] } }
    # Beacuse of sorting inside pairs
    #p pairs

    str_1 = pairs[-1][0][:str]
    str_2 = pairs[-1][1][:str]
    if str_1 + str_2[-1] == str_1[0] + str_2
      # str_1 is prefix
      @string = str_1 + str_2[-1]
    else
      # str_2 is prefix
      @string = str_2 + str_1[-1]
    end

    @solution = "S" * chunks.length
    pairs.each do |p|
      if @string.start_with? p[0][:str]
        @solution[p[0][:i]] = "P"
      else
        @solution[p[1][:i]] = "P"
      end
    end
  end

  attr_reader :string
  attr_reader :solution
end

# Input
n = gets.to_i # 2 <= n <= 100
chunks = Array.new(2 * n - 2) { |i| gets.chop }

# Result
result = StringBuilder.new(chunks)
#puts result.string
puts result.solution


Amugae has a hotel consisting of $$$10$$$ rooms. The rooms are numbered from $$$0$$$ to $$$9$$$ from left to right.The hotel has two entrances — one from the left end, and another from the right end. When a customer arrives to the hotel through the left entrance, they are assigned to an empty room closest to the left entrance. Similarly, when a customer arrives at the hotel through the right entrance, they are assigned to an empty room closest to the right entrance.One day, Amugae lost the room assignment list. Thankfully Amugae's memory is perfect, and he remembers all of the customers: when a customer arrived, from which entrance, and when they left the hotel. Initially the hotel was empty. Write a program that recovers the room assignment list from Amugae's memory.

The first line consists of an integer $$$n$$$ ($$$1 \le n \le 10^5$$$), the number of events in Amugae's memory. The second line consists of a string of length $$$n$$$ describing the events in chronological order. Each character represents:    'L': A customer arrives from the left entrance.  'R': A customer arrives from the right entrance.  '0', '1', ..., '9': The customer in room $$$x$$$ ($$$0$$$, $$$1$$$, ..., $$$9$$$ respectively) leaves.  It is guaranteed that there is at least one empty room when a customer arrives, and there is a customer in the room $$$x$$$ when $$$x$$$ ($$$0$$$, $$$1$$$, ..., $$$9$$$) is given. Also, all the rooms are initially empty.

In the only line, output the hotel room's assignment status, from room $$$0$$$ to room $$$9$$$. Represent an empty room as '0', and an occupied room as '1', without spaces.

NoteIn the first example, hotel room's assignment status after each action is as follows.   First of all, all rooms are empty. Assignment status is 0000000000.  L: a customer arrives to the hotel through the left entrance. Assignment status is 1000000000.  L: one more customer from the left entrance. Assignment status is 1100000000.  R: one more customer from the right entrance. Assignment status is 1100000001.  L: one more customer from the left entrance. Assignment status is 1110000001.  1: the customer in room $$$1$$$ leaves. Assignment status is 1010000001.  R: one more customer from the right entrance. Assignment status is 1010000011.  L: one more customer from the left entrance. Assignment status is 1110000011.  1: the customer in room $$$1$$$ leaves. Assignment status is 1010000011. So after all, hotel room's final assignment status is 1010000011.In the second example, hotel room's assignment status after each action is as follows.   L: a customer arrives to the hotel through the left entrance. Assignment status is 1000000000.  0: the customer in room $$$0$$$ leaves. Assignment status is 0000000000.  L: a customer arrives to the hotel through the left entrance. Assignment status is 1000000000 again.  0: the customer in room $$$0$$$ leaves. Assignment status is 0000000000.  L: a customer arrives to the hotel through the left entrance. Assignment status is 1000000000.  L: one more customer from the left entrance. Assignment status is 1100000000.  R: one more customer from the right entrance. Assignment status is 1100000001.  R: one more customer from the right entrance. Assignment status is 1100000011.  9: the customer in room $$$9$$$ leaves. Assignment status is 1100000010. So after all, hotel room's final assignment status is 1100000010.

def find_zero_index_from_left(array)
  found_index = nil
  array.each_with_index do |element, index|
    if element.eql?(0)
      found_index = index
      break
    end
  end
  found_index
end

def find_zero_index_from_right(array)
  found_index = nil
  (array.size - 1).downto(0).each do |index|
    if array[index].eql?(0)
      found_index = index
      break
    end
  end
  found_index
end

_ = gets.strip.to_i
arrives_code = gets.strip.split('')

codes = Array.new(10, 0)

arrives_code.each do |code|
  if code.eql?('L')
    index = find_zero_index_from_left(codes)
    codes[index] = 1
  elsif code.eql?('R')
    index = find_zero_index_from_right(codes)
    codes[index] = 1
  else
    index = code.to_i
    codes[index] = 0
  end
end

puts codes.join('')

def change_room_status_for(event_char, rooms_status)

  if event_char == "L"
    empty_room = rooms_status.index("0")
    rooms_status[empty_room] = "1"
  elsif event_char == "R"
    empty_room = rooms_status.rindex("0")
    rooms_status[empty_room] = "1"
  else
    checked_out_room = event_char.to_i
    rooms_status[checked_out_room] = "0"
  end

end

num_of_events_in_amugae_memory = gets.chomp.to_i

events = gets.chomp
rooms_status = '0' * 10

events.each_char { |event| change_room_status_for(event, rooms_status) }

puts rooms_status

def change_room_status_for(event_char, rooms_status)
  if event_char == "L"
    room_idx = closest_empty_room_from_left(rooms_status)
    rooms_status[room_idx] = "1"
  elsif event_char == "R"
    room_idx = closest_empty_room_from_right(rooms_status)
    rooms_status[room_idx] = "1"
  else
    leaving_customer_idx = event_char.to_i
    rooms_status[leaving_customer_idx] = "0"
  end
end

def closest_empty_room_from_left(rooms_status)
  rooms_status.index("0")
end

def closest_empty_room_from_right(rooms_status)
  idx = rooms_status.length - 1

  while idx >= 0
    if rooms_status[idx] == "0"
      return idx
    end

    idx -= 1
  end
end

num_of_events_in_amugae_memory = gets.chomp.to_i

events = gets.chomp
rooms_status = '0' * 10

events.each_char { |event| change_room_status_for(event, rooms_status) }

puts rooms_status

a=Array.new(10,0)
n=gets().to_i
s=gets()
for i in 0...n do
    if s[i]=="L" then
        a[a.index(0)]=1
    elsif s[i]=="R" then
        a[a.rindex(0)]=1
    else
        a[(s[i]).to_i]=0
    end
end
for i in 0..9 do
    print a[i]
end

n = gets.to_i
str = gets.chomp
ans = '0'*10
str.each_char do |x|
	if x == 'L'
		ans[ans.index('0')] = '1'
	elsif x == 'R'
		ans[ans.rindex('0')] = '1'
	else
		ans[x.to_i] = '0'
	end
end
puts ans

def test
 ind_l = 0
 ind_r = 0

input = $stdin.read
input = input.split("\n")
 length = input[0].to_i
 str = input[1].chars

 out = [0,0,0,0,0,0,0,0,0,0]
 for i in str
    if i == 'L'

      ind_l = out.index(0)
      out[ind_l] = 1
      ind_l  = ind_l +1 ;
    elsif i == 'R'

        out_rev=out.reverse
        ind_r=out_rev.index(0)
        out[9-ind_r] = 1
        ind_r = ind_r +1 ;
    else

        out[i.to_i] = 0
    end

 end
 puts out.join()
end
test

#!/usr/bin/env ruby

events_no = gets
events_seq = gets

output = Array.new(10, 0)

events_seq.delete!("\n").split('').each {  |c|

	if c == 'L'
		output[output.find_index(0)]=1
        elsif c == 'R'
		output[output.rindex(0)]=1

	else
		output[c.to_i] = 0
	end

}

puts output.join('')

def find_zero_index_from_left(array)
  found_index = nil
  array.each_with_index do |element, index|
    if element.eql?(0)
      found_index = index
      break
    end
  end
  found_index
end

def find_zero_index_from_right(array)
  found_index = nil
  (array.size - 1).downto(0).each do |index|
    if array[index].eql?(0)
      found_index = index
      break
    end
  end
  found_index
end

_ = gets.strip.to_i
arrives_code = gets.strip.split('')

codes = Array.new(10, 0)

arrives_code.each do |code|
  if code.eql?('L')
    index = find_zero_index_from_left(codes)
    codes[index] = 1
  elsif code.eql?('R')
    index = find_zero_index_from_right(codes)
    codes[index] = 1
  else
    index = code.to_i
    codes[index] = 0
  end
end

p codes

def test
 ind_l = 0
 ind_r = 0
 $/ = "1"
input = STDIN.gets
input = input.split("\n")
 length = input[0].to_i
 str = input[1].to_s.chars

 out = [0,0,0,0,0,0,0,0,0,0]
 for i in str
    if i == 'L'

      ind_l = out.index(0)
      out[ind_l] = 1
      ind_l  = ind_l +1 ;
    elsif i == 'R'

        out_rev=out.reverse
        ind_r=out_rev.index(0)
        out[9-ind_r] = 1
        ind_r = ind_r +1 ;
    else

        out[i.to_i] = 0
    end

 end
 return out
end
test

def test
 ind_l = 0
 ind_r = 0
 $/ = "1"
input = STDIN.gets
input = input.split("\n")
 length = input[0].to_i
 str = input[1].to_s.chars

 out = [0,0,0,0,0,0,0,0,0,0]
 for i in str
    if i == 'L'

      ind_l = out.index(0)
      out[ind_l] = 1
      ind_l  = ind_l +1 ;
    elsif i == 'R'

        out_rev=out.reverse
        ind_r=out_rev.index(0)
        out[9-ind_r] = 1
        ind_r = ind_r +1 ;
    else

        out[i.to_i] = 0
    end

 end
 puts out.to_s
end
test

def test
 ind_l = 0
 ind_r = 0
 $/ = "1"
input = STDIN.gets
input = input.split("\n")
 length = input[0].to_i
 str = input[1].to_s.chars

 out = [0,0,0,0,0,0,0,0,0,0]
 for i in str
    if i == 'L'

      ind_l = out.index(0)
      out[ind_l] = 1
      ind_l  = ind_l +1 ;
    elsif i == 'R'

        out_rev=out.reverse
        ind_r=out_rev.index(0)
        out[9-ind_r] = 1
        ind_r = ind_r +1 ;
    else

        out[i.to_i] = 0
    end

 end
 puts "#{out}"
end
test

def test
 ind_l = 0
 ind_r = 0
 $/ = "1"
input = STDIN.gets
input = input.split("\n")
 length = input[0].to_i
 str = input[1].to_s.chars

 out = [0,0,0,0,0,0,0,0,0,0]
 for i in str
    if i == 'L'

      ind_l = out.index(0)
      out[ind_l] = 1
      ind_l  = ind_l +1 ;
    elsif i == 'R'

        out_rev=out.reverse
        ind_r=out_rev.index(0)
        out[9-ind_r] = 1
        ind_r = ind_r +1 ;
    else

        out[i.to_i] = 0
    end

 end
 puts out.join()
end
test

def test
 ind_l = 0
 ind_r = 0

input = $stdin.read
input = input.split("\n")
 length = input[0].to_i
 str = input[1].chars
 puts length
 puts str
 out = [0,0,0,0,0,0,0,0,0,0]
 for i in str
    if i == 'L'

      ind_l = out.index(0)
      out[ind_l] = 1
      ind_l  = ind_l +1 ;
    elsif i == 'R'

        out_rev=out.reverse
        ind_r=out_rev.index(0)
        out[9-ind_r] = 1
        ind_r = ind_r +1 ;
    else

        out[i.to_i] = 0
    end

 end
 puts out.join()
end
test

def test
 ind_l = 0
 ind_r = 0
input = $stdin.read
input = input.split("\n")
 length = input[0].to_i
 str = input[1].to_s.chars

 out = [0,0,0,0,0,0,0,0,0,0]
 for i in str
    if i == 'L'

      ind_l = out.index(0)
      out[ind_l] = 1
      ind_l  = ind_l +1 ;
    elsif i == 'R'

        out_rev=out.reverse
        ind_r=out_rev.index(0)
        out[9-ind_r] = 1
        ind_r = ind_r +1 ;
    else

        out[i.to_i] = 0
    end

 end
 return out
end
test

def test
 ind_l = 0
 ind_r = 0
input = $stdin.read
input = input.split("\n")
 length = input[0].to_i
 str = input[1].to_s.chars

 out = [0,0,0,0,0,0,0,0,0,0]
 for i in str
    if i == 'L'
      puts 'L'
      ind_l = out.index(0)
      out[ind_l] = 1
      ind_l  = ind_l +1 ;
    elsif i == 'R'
        puts 'R'
        out_rev=out.reverse
        ind_r=out_rev.index(0)
        out[9-ind_r] = 1
        ind_r = ind_r +1 ;
    else
        puts 'ji'
        out[i.to_i] = 0
    end
    puts "#{out}"
 end
 return out
end

def test
 ind_l = 0
 ind_r = 0
 $/ = "1"
input = STDIN.gets
input = input.split("\n")
 length = input[0].to_i
 str = input[1].to_s.chars

 out = [0,0,0,0,0,0,0,0,0,0]
 for i in str
    if i == 'L'

      ind_l = out.index(0)
      out[ind_l] = 1
      ind_l  = ind_l +1 ;
    elsif i == 'R'

        out_rev=out.reverse
        ind_r=out_rev.index(0)
        out[9-ind_r] = 1
        ind_r = ind_r +1 ;
    else

        out[i.to_i] = 0
    end

 end
 puts out
end
test


At the school where Vasya is studying, preparations are underway for the graduation ceremony. One of the planned performances is a ball, which will be attended by pairs of boys and girls.Each class must present two couples to the ball. In Vasya's class, $$$a$$$ boys and $$$b$$$ girls wish to participate. But not all boys and not all girls are ready to dance in pairs.Formally, you know $$$k$$$ possible one-boy-one-girl pairs. You need to choose two of these pairs so that no person is in more than one pair.For example, if $$$a=3$$$, $$$b=4$$$, $$$k=4$$$ and the couples $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 2)$$$, $$$(3, 4)$$$ are ready to dance together (in each pair, the boy's number comes first, then the girl's number), then the following combinations of two pairs are possible (not all possible options are listed below):   $$$(1, 3)$$$ and $$$(2, 2)$$$;  $$$(3, 4)$$$ and $$$(1, 3)$$$; But the following combinations are not possible:   $$$(1, 3)$$$ and $$$(1, 2)$$$ — the first boy enters two pairs;  $$$(1, 2)$$$ and $$$(2, 2)$$$ — the second girl enters two pairs; Find the number of ways to select two pairs that match the condition above. Two ways are considered different if they consist of different pairs.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains three integers $$$a$$$, $$$b$$$ and $$$k$$$ ($$$1 \le a, b, k \le 2 \cdot 10^5$$$) — the number of boys and girls in the class and the number of couples ready to dance together. The second line of each test case contains $$$k$$$ integers $$$a_1, a_2, \ldots a_k$$$. ($$$1 \le a_i \le a$$$), where $$$a_i$$$ is the number of the boy in the pair with the number $$$i$$$. The third line of each test case contains $$$k$$$ integers $$$b_1, b_2, \ldots b_k$$$. ($$$1 \le b_i \le b$$$), where $$$b_i$$$ is the number of the girl in the pair with the number $$$i$$$. It is guaranteed that the sums of $$$a$$$, $$$b$$$, and $$$k$$$ over all test cases do not exceed $$$2 \cdot 10^5$$$. It is guaranteed that each pair is specified at most once in one test case.

For each test case, on a separate line print one integer — the number of ways to choose two pairs that match the condition above.

NoteIn the first test case, the following combinations of pairs fit:   $$$(1, 2)$$$ and $$$(3, 4)$$$;  $$$(1, 3)$$$ and $$$(2, 2)$$$;  $$$(1, 3)$$$ and $$$(3, 4)$$$;  $$$(2, 2)$$$ and $$$(3, 4)$$$. There is only one pair in the second test case.In the third test case, the following combinations of pairs fit:   $$$(1, 1)$$$ and $$$(2, 2)$$$;  $$$(1, 2)$$$ and $$$(2, 1)$$$.

tcases = gets.chomp.to_i
def solve()
  a, b, k = *(gets.chomp.split.map(&:to_i))
  boys = gets.chomp.split.map(&:to_i)
  girls = gets.chomp.split.map(&:to_i)
  boy_out = Array.new(a + 1, 0)
  girls_in =  Array.new(b + 1, 0)
  boys.each do |i|
    boy_out[i] += 1
  end
  girls.each do |i|
    girls_in[i] += 1
  end
  # puts "boy_out = #{boy_out.join(" ")}"
  # puts "girls_out = #{girls_in.join(" ")}"
  ans = 0
  0.upto k-1 do |i|
    b, g = boys[i], girls[i]
    bout, gin = boy_out[b], girls_in[g]
    ans += k - (bout + gin - 1)
  end
  # puts "ans = "
  puts ans / 2
end
tcases.times do solve() end

gets.to_i.times do
  aa,bb,k=gets.split.map(&:to_i)
  a=gets.split.map(&:to_i).sort
  b=gets.split.map(&:to_i).sort
  c=0
  q=0
  cc=0
  a.each do |ai|
    if ai==q
      cc+=1
    else
      c+=cc*(cc-1)/2
      cc=1
      q=ai
    end
  end
  c+=cc*(cc-1)/2
  q=0
  cc=0
  b.each do |ai|
    if ai==q
      cc+=1
    else
      c+=cc*(cc-1)/2
      cc=1
      q=ai
    end
  end
  c+=cc*(cc-1)/2
  puts k*(k-1)/2-c
end


Maksim has $$$n$$$ objects and $$$m$$$ boxes, each box has size exactly $$$k$$$. Objects are numbered from $$$1$$$ to $$$n$$$ in order from left to right, the size of the $$$i$$$-th object is $$$a_i$$$.Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $$$i$$$-th object fits in the current box (the remaining size of the box is greater than or equal to $$$a_i$$$), he puts it in the box, and the remaining size of the box decreases by $$$a_i$$$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).

The first line of the input contains three integers $$$n$$$, $$$m$$$, $$$k$$$ ($$$1 \le n, m \le 2 \cdot 10^5$$$, $$$1 \le k \le 10^9$$$) — the number of objects, the number of boxes and the size of each box. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le k$$$), where $$$a_i$$$ is the size of the $$$i$$$-th object.

Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.

NoteIn the first example Maksim can pack only $$$4$$$ objects. Firstly, he tries to pack all the $$$5$$$ objects. Distribution of objects will be $$$[5], [2, 1]$$$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $$$[2, 1], [4, 2]$$$. So the answer is $$$4$$$.In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $$$[4]$$$), but he can pack the last object ($$$[1]$$$).In the third example Maksim can pack all the objects he has. The distribution will be $$$[1, 2], [3], [1, 1]$$$.

def solution

  _, m, k = read_ints
  objects = read_ints.reverse

  used_boxes = 1
  crt_size = k
  packed = 0

  for o in objects
    if o <= crt_size
      packed += 1
      crt_size -= o
    else
      if used_boxes < m
        used_boxes += 1
        packed += 1
        crt_size = k - o
      else
        break
      end
    end
  end

  puts packed

end

def read_int
  gets.to_i
end

def read_ints
  gets.split.map(&:to_i)
end

def read_string
  gets.chomp
end

solution unless ENV['TEST__MODE']

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

cap = k
m -= 1
(n-1).downto(0) do |i|
  if a[i] <= cap
    cap -= a[i]
    ans += 1
  elsif m != 0 && a[i] <= k
    m -= 1
    cap = k-a[i]
    ans += 1
  else
    break
  end
end

puts ans


In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are n inflorescences, numbered from 1 to n. Inflorescence number 1 is situated near base of tree and any other inflorescence with number i (i > 1) is situated at the top of branch, which bottom is pi-th inflorescence and pi < i.Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in a-th inflorescence gets to pa-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time.Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest.

First line of input contains single integer number n (2 ≤ n ≤ 100 000)  — number of inflorescences. Second line of input contains sequence of n - 1 integer numbers p2, p3, ..., pn (1 ≤ pi < i), where pi is number of inflorescence into which the apple from i-th inflorescence rolls down.

Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest.

NoteIn first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them.In the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it.

n = gets.to_i
p = [0,1] + gets.split.map(&:to_i)
dist = [0,0]
gp = Array.new(n+1,0)
gp[0] = 1
2.upto(n) do |i|
  dist[i] = dist[p[i]] + 1
  gp[dist[i]] += 1
end
ans = 0
gp.each do |e|
  ans += 1 if e % 2 == 1
end
p ans

n = gets.chomp.to_i
arr = gets.chomp.split(" ").map {|e| e.to_i}

class Node
	attr_accessor :value, :children, :degree

	def initialize value
		@value = value
		@children = []
	end
end
an = Array.new(arr.length+1) { |i| Node.new(i+1)}
i = 0

while i < arr.length
	k = i+1
	an[arr[i]-1].children.push(an[k])
	i+=1
end
an.first.degree = 0
degree = [1]
queue = [an.first]
while queue.length > 0
	el = queue.shift
	el.children.each do |c|
		c.degree = el.degree+1
		degree[c.degree] ||= 0
		degree[c.degree]+=1
		queue.push(c)
	end
end
sum = 0
degree.each do |e|
	if e % 2 != 0
		sum+=1
	end
end
puts sum


You are a usual chat user on the most famous streaming platform. Of course, there are some moments when you just want to chill and spam something.More precisely, you want to spam the emote triangle of size $$$k$$$. It consists of $$$2k-1$$$ messages. The first message consists of one emote, the second one — of two emotes, ..., the $$$k$$$-th one — of $$$k$$$ emotes, the $$$k+1$$$-th one — of $$$k-1$$$ emotes, ..., and the last one — of one emote.For example, the emote triangle for $$$k=3$$$ consists of $$$5$$$ messages:  Of course, most of the channels have auto moderation. Auto moderator of the current chat will ban you right after you spam at least $$$x$$$ emotes in succession (you can assume you are the only user in the chat). Now you are interested — how many messages will you write before getting banned? Or maybe you will not get banned at all (i.e. will write all $$$2k-1$$$ messages and complete your emote triangle successfully)? Note that if you get banned as a result of writing a message, this message is also counted.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The next $$$t$$$ lines describe test cases. The only line of the test case contains integers $$$k$$$ and $$$x$$$ ($$$1 \le k \le 10^9; 1 \le x \le 10^{18}$$$).

For each test case, print the number of messages you will write before getting banned for the corresponding values $$$k$$$ and $$$x$$$.

NoteLet's analyze the test cases of the example.  In the first test case, you write three messages containing $$$1$$$, $$$2$$$ and $$$3$$$ emotes respectively, and since $$$1 + 2 + 3 \ge 6$$$, you get banned after that.  In the second test case, you write four messages containing $$$1$$$, $$$2$$$, $$$3$$$ and $$$4$$$ emotes respectively, and since $$$1 + 2 + 3 + 4 \ge 7$$$, you get banned after that.  In the third test case, you write one message containing exactly $$$1$$$ emote. It doesn't get you banned, since $$$1 < 2$$$, but you have already finished posting your emote triangle. So you wrote one message successfully.  In the fourth test case, you write four messages containing $$$1$$$, $$$2$$$, $$$3$$$ and $$$2$$$ emotes respectively, and since $$$1 + 2 + 3 + 2 \ge 7$$$, you get banned after that.  In the fifth test case, you write three messages containing $$$1$$$, $$$2$$$ and $$$1$$$ emote respectively. It doesn't get you banned, since $$$1 + 2 + 1 < 5$$$, but you have already finished posting your emote triangle. So you wrote three messages successfully.  In the sixth test case, since $$$x = 1$$$, you get banned as soon as you send your first message.  The seventh test case is too large to analyze, so we'll skip it.

main = -> {
  t = int
  t.times do
    k, x = ints
    # first, print k lines
    ac = 0
    wa = k * 2 + 1
    while wa - ac > 1
      wj = (ac + wa) >> 1
      # wj lines
      emotes = 0
      last = 0
      if wj > k
        emotes += k * (k + 1) / 2
        m = wj - k
        emotes += (k - 1 + k - m) * m / 2
        last = k - m
      else
        emotes += wj * (wj + 1) / 2
        last = wj
      end
      if emotes - last >= x
        wa = wj
      else
        ac = wj
      end
    end

    puts [ac, 2 * k - 1].min
  end
}

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

require "set"
require "prime"

def int; gets.to_s.to_i end
def ints; gets.to_s.split.map { |s| s.to_i } end
def int1s; gets.to_s.split.map { |s| s.to_i - 1 } end
def float; gets.to_s.to_f end
def floats; gets.to_s.split.map { |s| s.to_f } end
def array_of(&convert); gets.to_s.split.map(&convert) end
def string; gets.to_s.chomp end
def 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[]


Slavic has an array of length $$$n$$$ consisting only of zeroes and ones. In one operation, he removes either the first or the last element of the array. What is the minimum number of operations Slavic has to perform such that the total sum of the array is equal to $$$s$$$ after performing all the operations? In case the sum $$$s$$$ can't be obtained after any amount of operations, you should output -1.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \leq n, s \leq 2 \cdot 10^5$$$) — the length of the array and the needed sum of elements. The second line of each test case contains $$$n$$$ integers $$$a_i$$$ ($$$0 \leq a_i \leq 1$$$) — the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.

For each test case, output a single integer — the minimum amount of operations required to have the total sum of the array equal to $$$s$$$, or -1 if obtaining an array with sum $$$s$$$ isn't possible.

NoteIn the first test case, the sum of the whole array is $$$1$$$ from the beginning, so we don't have to make any operations.In the second test case, the sum of the array is $$$2$$$ and we want it to be equal to $$$1$$$, so we should remove the first element. The array turns into $$$[1, 0]$$$, which has a sum equal to $$$1$$$.In the third test case, the sum of the array is $$$5$$$ and we need it to be $$$3$$$. We can obtain such a sum by removing the first two elements and the last element, doing a total of three operations. The array turns into $$$[0, 1, 1, 1, 0, 0]$$$, which has a sum equal to $$$3$$$.

bsearch_last = ->range, map, target {
  l, r = range.begin, range.end + 1
  while l < r
    m = (l + r) / 2
    if map[m] > target
      r = m
    else
      l = m + 1
    end
  end
  map[r - 1] == target ? r - 1 : nil
}

gets.to_i.times do
  n, s = gets.split.map(&:to_i)
  xs = gets.split.map(&:to_i)
  ps = [0]
  xs.each { |x| ps << x + ps.last }
  sum_inclusive = ->(from, to) { ps[to + 1] - ps[from] }
  answer = (0..n - 1).filter_map do |from|
    to = bsearch_last[from..n - 1, ->to { sum_inclusive[from, to] }, s]
    to ? n - (to - from + 1) : nil
  end.min || -1
  p answer
end

gets.to_i.times do
  n, s = gets.split.map(&:to_i)
  xs = gets.split.map(&:to_i)
  map = {}
  sum, max = 0, -1
  xs.each.with_index do |x, i|
    sum += x
    max = i + 1 if sum == s
    map[sum] ||= i
    if j = map[sum - s]
      max = [max, i - j].max
    end
  end
  p max == -1 ? -1 : n - max
end

def solve(arr, s)
  len = arr.size

  len.times do |i|
    arr[i] += arr[i - 1] if i > 0
  end

  max_result = -1

  if arr[-1] < s
    return -1
  end

  len.times do |left|
    result = binsearch(arr, left, s)
    # p [left, result, (result - left + 1)]

    if (result - left + 1) > max_result
      max_result = result - left + 1
    end
  end

  len - max_result
end

def binsearch(arr, left, a)
  l = left - 1
  r = arr.size

  while l < r - 1
    m = (l + r) / 2
    if (arr[m] - (left > 0 ? arr[left - 1] : 0)) <= a
      l = m
    else
      r = m
    end
  end

  l
end

n = gets.to_i

n.times do
  size, s = gets.split.map(&:to_i)
  array = Array.new(size)
  array = gets.split.map(&:to_i)

  p solve(array, s)
end

def solve
    q = gets.split.map(&:to_i)
    a = gets.split.map(&:to_i)
    n = q[0]
    s = q[1]
    sum = 0
    a.each do |x|
        sum = sum + x
    end
    if s > sum
        puts "-1"
        return
    end
    l = 1
    r = a.length
    while l < r
        mid = (l + r + 1) / 2
        ok = false
        pref = Array.new(n + 1)
        pref[0] = 0
        i = 1
        a.each do |x|
            pref[i] = pref[i - 1]
            pref[i] = pref[i] + x
            if (i - mid) >= 0
                curr = pref[i] - pref[i - mid]
                if curr <= s
                    ok = true
                end
            end
            i = i + 1
        end
        if ok == true
            l = mid
        else
            r = mid - 1
        end
    end
    puts n - l
end

gets.to_i.times do
    solve
end

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

  cnt = 0
  l = 0
  inf = 10101010101010
  ans = inf
  n.times do |r|
    cnt += a[r]
    while l < r && cnt > s
      cnt -= a[l]
      l += 1
    end

    ans = [ans, n-(r-l+1)].min if cnt == s
  end

  puts ans == inf ? -1 : ans
end

main = -> {
    t = int
    t.times do
        n, s = ints
        a = ints
        a_sum = [0]
        a.each do |x|
            a_sum << a_sum[-1] + x
        end

        sum = a_sum[-1]

        if s > sum
            puts -1
            next
        end

        has_to_remove = sum - s

        ans = INF
        (0 ... n).each do |i|
            removed = a_sum[i]
            break if removed > has_to_remove
            j = (0 .. n).bsearch { |m| sum - a_sum[n - m] >= has_to_remove - removed } || 0
            ans = i + j if i + j < ans
        end

        puts ans
    end
}

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

require "set"
require "prime"

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

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

class TrueClass
  include Boolean
  def to_i; 1 end
end

class FalseClass
  include Boolean
  def to_i; 0 end
end

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

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

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

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

main[]

bsearch_last = ->range, block {
  i = (0..range.end - range.begin).bsearch { |i| block[range.end - i] }
  i ? range.end - i : i
}

gets.to_i.times do
  n, s = gets.split.map(&:to_i)
  xs = gets.split.map(&:to_i)
  ps = [0]
  xs.each { |x| ps << x + ps.last }
  sum_inclusive = ->(from, to) { ps[to + 1] - ps[from] }
  answer = (0..n - 1).filter_map do |from|
    to = bsearch_last[from..n - 1, ->to { sum_inclusive[from, to] == s }]
    to ? n - (to - from + 1) : nil
  end.min || -1
  p answer
end


Polycarpus just has been out of luck lately! As soon as he found a job in the "Binary Cat" cafe, the club got burgled. All ice-cream was stolen.On the burglary night Polycarpus kept a careful record of all club visitors. Each time a visitor entered the club, Polycarpus put down character "+" in his notes. Similarly, each time a visitor left the club, Polycarpus put character "-" in his notes. We know that all cases of going in and out happened consecutively, that is, no two events happened at the same time. Polycarpus doesn't remember whether there was somebody in the club at the moment when his shift begun and at the moment when it ended.Right now the police wonders what minimum number of distinct people Polycarpus could have seen. Assume that he sees anybody coming in or out of the club. Each person could have come in or out an arbitrary number of times.

The only line of the input contains a sequence of characters "+" and "-", the characters are written one after another without any separators. The characters are written in the order, in which the corresponding events occurred. The given sequence has length from 1 to 300 characters, inclusive.

Print the sought minimum number of people

s = gets.chomp
manin = 0
manout = 0
s.size.times do |i|
    if s[i] == "+" then
        manin += 1
        if manout != 0 then
            manout -= 1
        end
    else
        manout += 1
        if manin != 0 then
            manin -= 1
        end
    end
end
result = manin + manout
puts "#{result}"

tmp,mn,mx=0,0,0
gets.to_s.chomp.each_char do |c|
	c=="-"?(tmp-=1):(tmp+=1)
	mn=tmp if tmp<mn
	mx=tmp if mx<tmp
end
puts (mn.abs+mx.abs)

dict = {
    'UCA' => 'S',    # Serine
    'UCC' => 'S',    # Serine
    'UCG' => 'S',    # Serine
    'UCU' => 'S',    # Serine
    'UUC' => 'F',    # Phenylalanine
    'UUU' => 'F',    # Phenylalanine
    'UUA' => 'L',    # Leucine
    'UUG' => 'L',    # Leucine
    'UAC' => 'Y',    # Uyrosine
    'UAU' => 'Y',    # Uyrosine
    'UAA' => '_',    # Stop
    'UAG' => '_',    # Stop
    'UGC' => 'C',    # Cysteine
    'UGU' => 'C',    # Cysteine
    'UGA' => '_',    # Stop
    'UGG' => 'W',    # Uryptophan
    'CUA' => 'L',    # Leucine
    'CUC' => 'L',    # Leucine
    'CUG' => 'L',    # Leucine
    'CUU' => 'L',    # Leucine
    'CCA' => 'P',    # Proline
    'CCC' => 'P',    # Proline
    'CCG' => 'P',    # Proline
    'CCU' => 'P',    # Proline
    'CAC' => 'H',    # Histidine
    'CAU' => 'H',    # Histidine
    'CAA' => 'Q',    # Glutamine
    'CAG' => 'Q',    # Glutamine
    'CGA' => 'R',    # Arginine
    'CGC' => 'R',    # Arginine
    'CGG' => 'R',    # Arginine
    'CGU' => 'R',    # Arginine
    'AUA' => 'I',    # Isoleucine
    'AUC' => 'I',    # Isoleucine
    'AUU' => 'I',    # Isoleucine
    'AUG' => 'M',    # Methionine
    'ACA' => 'T',    # Uhreonine
    'ACC' => 'T',    # Uhreonine
    'ACG' => 'T',    # Uhreonine
    'ACU' => 'T',    # Uhreonine
    'AAC' => 'N',    # Asparagine
    'AAU' => 'N',    # Asparagine
    'AAA' => 'K',    # Lysine
    'AAG' => 'K',    # Lysine
    'AGC' => 'S',    # Serine
    'AGU' => 'S',    # Serine
    'AGA' => 'R',    # Arginine
    'AGG' => 'R',    # Arginine
    'GUA' => 'V',    # Valine
    'GUC' => 'V',    # Valine
    'GUG' => 'V',    # Valine
    'GUU' => 'V',    # Valine
    'GCA' => 'A',    # Alanine
    'GCC' => 'A',    # Alanine
    'GCG' => 'A',    # Alanine
    'GCU' => 'A',    # Alanine
    'GAC' => 'D',    # Aspartic Acid
    'GAU' => 'D',    # Aspartic Acid
    'GAA' => 'E',    # Glutamic Acid
    'GAG' => 'E',    # Glutamic Acid
    'GGA' => 'G',    # Glycine
    'GGC' => 'G',    # Glycine
    'GGG' => 'G',    # Glycine
    'GGU' => 'G'    # Glycine
}

s = gets.chomp!

f = g = c =0; res = 0; s.each_char{|si|
	c = 1 if si == '+'; c = -1 if si == '-'
	f += c; g += c; f = 0 if f < 0; g = 0 if g > 0
	res = f if f > res; res = -g if -g > res
}

p res

dict = {
    'UCA' => 'S',    # Serine
    'UCC' => 'S',    # Serine
    'UCG' => 'S',    # Serine
    'UCU' => 'S',    # Serine
    'UUC' => 'F',    # Phenylalanine
    'UUU' => 'F',    # Phenylalanine
    'UUA' => 'L',    # Leucine
    'UUG' => 'L',    # Leucine
    'UAC' => 'Y',    # Uyrosine
    'UAU' => 'Y',    # Uyrosine
    'UAA' => '_',    # Stop
    'UAG' => '_',    # Stop
    'UGC' => 'C',    # Cysteine
    'UGU' => 'C',    # Cysteine
    'UGA' => '_',    # Stop
    'UGG' => 'W',    # Uryptophan
    'CUA' => 'L',    # Leucine
    'CUC' => 'L',    # Leucine
    'CUG' => 'L',    # Leucine
    'CUU' => 'L',    # Leucine
    'CCA' => 'P',    # Proline
    'CCC' => 'P',    # Proline
    'CCG' => 'P',    # Proline
    'CCU' => 'P',    # Proline
    'CAC' => 'H',    # Histidine
    'CAU' => 'H',    # Histidine
    'CAA' => 'Q',    # Glutamine
    'CAG' => 'Q',    # Glutamine
    'CGA' => 'R',    # Arginine
    'CGC' => 'R',    # Arginine
    'CGG' => 'R',    # Arginine
    'CGU' => 'R',    # Arginine
    'AUA' => 'I',    # Isoleucine
    'AUC' => 'I',    # Isoleucine
    'AUU' => 'I',    # Isoleucine
    'AUG' => 'M',    # Methionine
    'ACA' => 'T',    # Uhreonine
    'ACC' => 'T',    # Uhreonine
    'ACG' => 'T',    # Uhreonine
    'ACU' => 'T',    # Uhreonine
    'AAC' => 'N',    # Asparagine
    'AAU' => 'N',    # Asparagine
    'AAA' => 'K',    # Lysine

    'GGC' => 'G',    # Glycine
    'GGG' => 'G',    # Glycine
    'GGU' => 'G'    # Glycine
}

s = gets.chomp!

f = g = c =0; res = 0; s.each_char{|si|
    c = 1 if si == '+'; c = -1 if si == '-'
    f += c; g += c; f = 0 if f < 0; g = 0 if g > 0
    res = f if f > res; res = -g if -g > res
}

p res

s=gets.chomp
prev=s[0]
res=[]
k=1
res<<k
s[1..-1].each_char do |c|
    x=c==s[0] ? 1 : -1
    k+=x if prev==c
    res<<k
    prev=c
end
puts res.max-res.min+1

def main()
	s=gets.chomp.to_s
	nops=0
	inclub=0
	outclub=0
	for i in 0...(s.length)
		if s[i].chr=="+"
			inclub+=1
			if outclub ==0
				nops+=1
			else
				outclub-=1
			end
		else
			outclub+=1
			if inclub==0
				nops+=1
			else
				inclub-=1
			end
		end
	end
	puts nops
end
main()

pos=0
neg=0
count=0

gets.chomp.each_char {|c|
  if c==?+
    count+=1
  else
    count-=1
  end

  pos=count if count>pos
  neg=count if count<neg
}

puts pos.abs+neg.abs

def abs(x)
    if x > 0 then return x
    else return -x
    end
end

s = gets.chomp
plus = 0
minus = 0
k = false
t = false
s.size.times do |i|
    if s[i] == "+" then
        plus += 1
        k = true
    else
        minus += 1
        t = true
    end
end
man = abs(plus-minus)
if k && t then
    man += 1
end
puts "#{man}"

def dif(s,i)
    k = i
    while i < s.size && s[i] != s[i-1] do
        i += 1
    end
    return i-k
end

def find(x, s)
    x.each_index do |i|
        if x[i] == s then
            return i
        end
    end
    return false
end

s = gets.chomp
i = 1
man = [s[0]]
while i < s.size do
    if s[i] != s[i-1] then
        t = find(man, s[i])
        k = dif(s,i)
        i += k
        if t then
            man[t] = s[i-1]
        end
    else
        while s[i] == s[i-1] do
            if s[i] == "+" then
                t = find(man, "-")
            else
                t = find(man, "+")
            end
            if t then
                man[t] = s[i]
            else
                man += [s[i]]
            end
            i += 1
        end
    end
end
puts "#{man.size}"

def abs(x)
    if x > 0 then return x
    else return -x
    end
end

s = gets.chomp
plus = 0
minus = 0
s.size.times do |i|
    if s[i] == "+" then
        plus += 1
    else
        minus += 1
    end
end
if abs(plus-minus) != 0 then
    man = abs(plus-minus)
else
    man = 1
end
puts "#{man}"

def same(s, i)
    k = i
    while (i < s.size && s[i] == s[i-1]) do
        i += 1
    end
    return i-k
end

def dif(s, i)
    k = i
    while (i < s.size && s[i] != s[i-1]) do
        i += 1
    end
    return i-k
end

def alsame(s, i)
    if same(s,i) == s.size-1 then
        s = ""
    end
    return s
end

s = gets.chomp
plus = 0
minus = 0
man = 0
i = 1
while s.size != 0 do
    k = s.size
    s = alsame(s,i)
    if s == "" then
        man += k
    else
        t = dif(s,i)
        s = s[t..-1]
        if t != 0 then
            man += 1
            s = s[1..-1]
        end
        k = s.size
        s = alsame(s,i)
        if s == "" then
            man += k
        else
            k = same(s,i)
            s = s[k..-1]
            man += k
        end
    end
end
puts "#{man}"

s = gets
plus = 0
minus = 0
s.size.times do |i|
    if s[i] == "+" then plus += 1
    else
        minus += 1
    end
end
man = 1
man += (plus-minus).abs
puts "#{man}"

a = gets.to_s.chomp.split(//)
puts (a.find_all{|el|el=="+"}.size-a.find_all{|el|el=="-"}.size).abs

s= gets.to_s.chomp
a=s.split(//)
n= (a.find_all{|el|el=="+"}.size-a.find_all{|el|el=="-"}.size).abs
t1="+"*(s.size-1)
t2="-"*(s.size-1)
while s.index(t1)==nil&&s.index(t2)==nil&&t1!="+" do
t1.chop!
t2.chop!
end
puts [t1.size,n].max

s= gets.to_s.chomp
a=s.split(//)
n= (a.find_all{|el|el=="+"}.size-a.find_all{|el|el=="-"}.size).abs
t1="+"*(s.size-1)
t2="-"*(s.size-1)
while s.index(t1)==nil&&s.index(t2)==nil&&t1!="+" do
t1.chop!
p t1
t2.chop!
end
puts [t1.size,n].max

dict = {
    'UUG' => 'L',    # Leucine
    'UAC' => 'Y',    # Uyrosine
    'UAU' => 'Y',    # Uyrosine
    'UAA' => '_',    # Stop
    'UAG' => '_',    # Stop
    'UGC' => 'C',    # Cysteine
    'UGU' => 'C',    # Cysteine
    'CCU' => 'P',    # Proline
    'CAC' => 'H',    # Histidine
    'CAU' => 'H',    # Histidine
    'CAA' => 'Q',    # Glutamine
    'CAG' => 'Q',    # Glutamine
    'CGA' => 'R',    # Arginine
    'CGC' => 'R',    # Arginine
    'CGG' => 'R',    # Arginine
    'CGU' => 'R',    # Arginine
    'AUA' => 'I',    # Isoleucine
    'AUC' => 'I',    # Isoleucine
    'AUU' => 'I',    # Isoleucine
    'AUG' => 'M',    # Methionine
    'ACA' => 'T',    # Uhreonine

    'GUG' => 'V',    # Valine
    'GUU' => 'V',    # Valine
    'GCA' => 'A',    # Alanine
    'GCC' => 'A',    # Alanine
    'GCG' => 'A',    # Alanine
    'GCU' => 'A',    # Alanine
    'GAC' => 'D',    # Aspartic Acid
    'GAU' => 'D',    # Aspartic Acid
    'GAA' => 'E',    # Glutamic Acid
    'GAG' => 'E',    # Glutamic Acid
    'GGA' => 'G',    # Glycine
    'GGC' => 'G',    # Glycine
    'GGG' => 'G',    # Glycine
    'GGU' => 'G'    # Glycine
}

s = gets.chomp!

f = g = c =0; res = 0; s.each_char{|si|
    c = 1 if si == '+'; c = -1 if si == '-'
    f += c; g += c; f = 0 if f < 0; g = 0 if g > 0
    res = f if f > res; res = -g if -g > res
}

res=k=0
gets.chomp.each_char do |c|
    k+=1 if c=='+'
    k-=1 if c=='-'
    res=[res, k.abs].max
end
puts res

s=gets.gsub("-+", "- +").gsub("+-", "+ -").split
s.push("o") if s.size%2==1
n=s.size/2
res=0
for i in 0..n-1 do
    res=[res, [s[2*i].size, s[2*i+1].size].max].max
end
puts res

s=gets.chomp
res=p=m=0
s.each_char do |c|
    p+=1 if c=='+'
    m+=1 if c=='-'
    res=[res, (p-m).abs].max
end
puts res

s=gets.chomp
res=0
for i in 1..s.size()
    res=i if s.include?('+'*i) or s.include?('-'*i)
end
puts res

def main()
	s=gets.chomp.to_s
	totin=0
	totout=0
	nops=1
	for i in 0...(s.length)
		if s[i].chr=="+"
			totin+=1
			if totin-1 > totout
				nops+=1
			end
		else
			totout+=1
			if totout-1 > totin
				nops+=1
			end
		end
	end
	puts nops
end
main()

def main()
	s=gets.chomp.to_s
	totin=0
	totout=0
	nops=0
	for i in 0...(s.length)
		if s[i].chr=="+"
			totin+=1
			if totin-totout>1 || totout==0
				nops+=1
			end
		else
			totout+=1
			if totout-totin>1 || totin==0
				nops+=1
			end
		end
	end
	puts nops
end
main()

def main()
	s=gets.chomp.to_s
	nops=0
	for i in 0...(s.length)
		if s[i].chr=="+"
			lns=0
			indoln=-7
			j=i-1
			while(j>=0)
				if s[j].chr=="-"
					indoln=j
					break
				end
				j-=1
			end
			if indoln!=-7
				j=indoln
				while(j>=0)
					if s[j].chr=="-"
						lns+=1
						j-=1
					else
						break
					end
				end
				if lns<=i-indoln-1
					nops+=1
				end
			else
				nops+=1
			end
		else
			lps=0
			indolp=-7
			j=i-1
			while(j>=0)
				if s[j].chr=="+"
					indolp=j
					break
				end
				j-=1
			end
			if indolp!=-7
				j=indolp
				while(j>=0)
					if s[j].chr=="+"
						lps+=1
						j-=1
					else
						break
					end
				end
				if lps<=i-indolp-1
					nops+=1
				end
			else
				nops+=1
			end
		end
	end
	puts nops
end
main()

def main()
	s=gets.chomp.to_s
	maxp=0
	maxn=0
	ps="+"
	ns="-"
	j=300
	while(true)
		ind=s.index(ps*j)
		if ind!=nil
			maxp=j
			break
		end
		j-=1
	end
	j=300
	while(true)
		ind=s.index(ns*j)
		if ind!=nil
			maxn=j
			break
		end
		j-=1
	end
	puts [maxp,maxn].max
end
main()

max=0
count=0

gets.chomp.each_char {|c|
  if c==?+
    count=0 if count<0
    count+=1
  else
    count-=1
  end

  max=count.abs if count.abs>max
}

puts max

max=0
count=0

gets.chomp.each_char {|c|
  if c==?+
    count+=1
  else
    count-=1
  end

  max=count.abs if count.abs>max
}

puts max

pos=0
neg=0
count=0

gets.chomp.each_char {|c|
  if c==?+
    count+=1
  else
    count-=1
  end

  pos=count if count>pos
  neg=count if count<neg
}

puts [pos.abs,neg.abs].max

puts gets.chomp.chars.to_a.reduce([0, 0]) { |s, x| s[0] += (x == '+' ? 1 : -1); [s.first, [s.first.abs, s.last].max] }.last


Ehab loves number theory, but for some reason he hates the number $$$x$$$. Given an array $$$a$$$, find the length of its longest subarray such that the sum of its elements isn't divisible by $$$x$$$, or determine that such subarray doesn't exist.An array $$$a$$$ is a subarray of an array $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.

The first line contains an integer $$$t$$$ $$$(1 \le t \le 5)$$$ — the number of test cases you need to solve. The description of the test cases follows. The first line of each test case contains 2 integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 10^5$$$, $$$1 \le x \le 10^4$$$) — the number of elements in the array $$$a$$$ and the number that Ehab hates. The second line contains $$$n$$$ space-separated integers $$$a_1$$$, $$$a_2$$$, $$$\ldots$$$, $$$a_{n}$$$ ($$$0 \le a_i \le 10^4$$$) — the elements of the array $$$a$$$.

For each testcase, print the length of the longest subarray whose sum isn't divisible by $$$x$$$. If there's no such subarray, print $$$-1$$$.

NoteIn the first test case, the subarray $$$[2,3]$$$ has sum of elements $$$5$$$, which isn't divisible by $$$3$$$.In the second test case, the sum of elements of the whole array is $$$6$$$, which isn't divisible by $$$4$$$.In the third test case, all subarrays have an even sum, so the answer is $$$-1$$$.

def ans(r, x)
    r.count {|e| e % x !=0 } == 0 and return -1
    r.sum % x != 0 and return r.length
    # find first e % x != 0
    lidx = r.index {|e| e % x != 0}
    ridx = r.rindex {|e| e % x != 0}
    return [r.length - lidx -1, ridx].max
end

gets.to_i.times {
    (_x, x) = gets.chomp.split(' ').map &:to_i
    r = gets.chomp.split(' ').map &:to_i
    puts ans(r,x)
}

def solve
  _, x = gets.strip.split(' ').map(&:to_i)
  a = gets.strip.split(' ').map(&:to_i)

  h = Hash.new(0)
  a.each_with_index { |e, i|
    if i.zero?
      h[i] = e
      next
    end
    h[i] = h[i - 1] + e
  }

  sum = a.reduce(:+)
  an = a.size
  if sum % x != 0
    puts an
    return nil
  end

  i = 0
  while i < a.size
    if sum % x == 0 && (sum - h[i]) % x != 0
      an = an - i - 1;
      break;
    elsif sum % x == 0 && (sum - h[a.size - i - 2]) % x != 0
      an = an - i - 1
      break;
    end
    i += 1
  end
  if an == a.size
    puts "-1"
  else
    puts an
  end
end

gets.strip.to_i.times { solve }

def solve
  _, x = gets.strip.split(' ').map(&:to_i)
  a = gets.strip.split(' ').map(&:to_i)

  h = Hash.new(0)
  a.each_with_index { |e, i|
    if i.zero?
      h[i] = e
      next
    end
    h[i] = h[i - 1] + e
  }

  sum = a.reduce(:+)
  an = a.size
  if sum % x != 0
    puts an
    return nil
  end

  i = 0
  while i < a.size
    if sum % x == 0 && (sum - h[i]) % x != 0 || sum % x == 0 && (sum - h[a.size - i - 2]) % x != 0
      an = an - i - 1;
      break;
    end
    i += 1
  end
  if an == a.size
    puts "-1"
  else
    puts an
  end
end

gets.strip.to_i.times { solve }

def solve
  _, x = gets.strip.split(' ').map(&:to_i)
  a = gets.strip.split(' ').map(&:to_i)

  h = Hash.new(0)
  a.each_with_index { |e, i|
    if i.zero?
      h[i] = e
      next
    end
    h[i] = h[i - 1] + e
  }

  sum = a.reduce(:+)
  an = a.size
  if sum % x != 0
    puts an
    return nil
  end

  i = 0
  while i < a.size
    if sum % 3 == 0 && (sum - h[i]) % 3 != 0
      an = an - i - 1;
      break;
    elsif sum % 3 == 0 && (sum - h[a.size - i - 1]) % 3 != 0
      sum -= h[a.size - i - 1]
      an = an - i - 1
      break;
    end
    i += 1
  end
  if an == a.size
    puts "-1"
  else
    puts an
  end
end

gets.strip.to_i.times { solve }

def solve
  _, x = gets.strip.split(' ').map(&:to_i)
  a = gets.strip.split(' ').map(&:to_i)

  h = Hash.new(0)
  a.each_with_index { |e, i|
    if i.zero?
      h[i] = e
      next
    end
    h[i] = h[i - 1] + e
  }

  sum = a.reduce(:+)
  an = a.size
  if sum % x != 0
    puts an
    return nil
  end

  i = 0
  while i < a.size
    if sum % x == 0 && (sum - h[i]) % x != 0
      an = an - i - 1;
      break;
    elsif sum % x == 0 && (sum - h[a.size - i - 2]) % x != 0
      puts i
      an = an - i - 1
      break;
    end
    i += 1
  end
  if an == a.size
    puts "-1"
  else
    puts an
  end
end

gets.strip.to_i.times { solve }

def solve
  _, x = gets.strip.split(' ').map(&:to_i)
  a = gets.strip.split(' ').map(&:to_i)
  a.each_with_index { |e, i| a[i] = a[i] % x; }
  sum = a.reduce(:+)
  if (sum).zero?
    puts "-1"
    return nil
  end
  if sum % x != 0
    puts a.size
    return nil
  end
  l = 0; r = a.size - 1; an = a.size
  while a[l].zero?
    l += 1
    an -= 1
  end
  while a[r].zero?
    r -= 1
    an -= 1
  end
  puts an + 1
end

gets.strip.to_i.times { solve }

def solve
  _, x = gets.strip.split(' ').map(&:to_i)
  a = gets.strip.split(' ').map(&:to_i)

  h = Hash.new(0)
  a.each_with_index { |e, i|
    if i.zero?
      h[i] = e
      next
    end
    h[i] = h[i - 1] + e
  }

  sum = a.reduce(:+)
  an = a.size
  if sum % x != 0
    puts an
    return nil
  end

  i = 0
  while i < a.size
    if sum % 3 == 0 && (sum - h[i]) % 3 != 0
      an = an - i - 1;
      break;
    elsif sum % 3 == 0 && (sum - h[a.size - i - 1]) % 3 != 0
      sum -= h[a.size - i - 1]
      an = an - i - 1
      break;
    end
    i += 1
  end
  puts "-1" if an.zero? || an == a.size
  puts an if !an.zero?
end

gets.strip.to_i.times { solve }


Mike is the president of country What-The-Fatherland. There are n bears living in this country besides Mike. All of them are standing in a line and they are numbered from 1 to n from left to right. i-th bear is exactly ai feet high.   A group of bears is a non-empty contiguous segment of the line. The size of a group is the number of bears in that group. The strength of a group is the minimum height of the bear in that group.Mike is a curious to know for each x such that 1 ≤ x ≤ n the maximum strength among all groups of size x.

The first line of input contains integer n (1 ≤ n ≤ 2 × 105), the number of bears. The second line contains n integers separated by space, a1, a2, ..., an (1 ≤ ai ≤ 109), heights of bears.

Print n integers in one line. For each x from 1 to n, print the maximum strength among all groups of size x.

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

def find_left(a)
    s = []
    r = Array.new(a.length)
    (0...a.length).each do |i|
        s.pop while s.length > 0 and a[s[-1]] >= a[i]
        if s.length == 0 then r[i] = i + 1 else r[i] = i - s[-1] end
        s.push i
    end
    r
end

left = find_left a
right = (find_left a.reverse).reverse
ans = Array.new(n, a.min)
(0...n).each do |i|
    v = left[i] + right[i] - 2
    ans[v] = a[i] if a[i] > ans[v]
end
(n-2).downto(0) do |i|
    ans[i] = ans[i + 1] if ans[i + 1] > ans[i]
end
ans.each do |x|
    print x
    print ' '
end
puts