Polycarp has n dice d1, d2, ..., dn. The i-th dice shows numbers from 1 to di. Polycarp rolled all the dice and the sum of numbers they showed is A. Agrippina didn't see which dice showed what number, she knows only the sum A and the values d1, d2, ..., dn. However, she finds it enough to make a series of statements of the following type: dice i couldn't show number r. For example, if Polycarp had two six-faced dice and the total sum is A = 11, then Agrippina can state that each of the two dice couldn't show a value less than five (otherwise, the remaining dice must have a value of at least seven, which is impossible).For each dice find the number of values for which it can be guaranteed that the dice couldn't show these values if the sum of the shown values is A.

The first line contains two integers n, A (1 ≤ n ≤ 2·105, n ≤ A ≤ s) — the number of dice and the sum of shown values where s = d1 + d2 + ... + dn. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 106), where di is the maximum value that the i-th dice can show.

Print n integers b1, b2, ..., bn, where bi is the number of values for which it is guaranteed that the i-th dice couldn't show them.

NoteIn the first sample from the statement A equal to 8 could be obtained in the only case when both the first and the second dice show 4. Correspondingly, both dice couldn't show values 1, 2 or 3.In the second sample from the statement A equal to 3 could be obtained when the single dice shows 3. Correspondingly, it couldn't show 1, 2, 4 or 5.In the third sample from the statement A equal to 3 could be obtained when one dice shows 1 and the other dice shows 2. That's why the first dice doesn't have any values it couldn't show and the second dice couldn't show 3.

n, sum = gets.split(' ').map(&:to_i)
ds = gets.split(' ').map(&:to_i)
max = ds.reduce(:+)

n.times do |i|
  d = ds[i]
  other_max = max-d

  my_min = 1
  if sum > other_max
    my_min = sum-other_max
  end
  my_max = [sum - (n-1), d].min

  print ' ' if i != 0
  print (d - [(my_max-my_min+1), 0].max)
end
print "\n"

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

# c.rb

def main()
    n, a = gets.chomp.split(" ").map{|s| next s.to_i }
    d = gets.chomp.split(" ").map{|s| next s.to_i }
    s = d.inject{|ret, i| next ret + i }
    r = []
    d.each_with_index do |di, i|
        ss = s - di
        least = [1, a - ss].max
        most = [di, a - (n - 1)].min
        cand = most - least + 1
        r << di - cand
    end

    puts(r * " ")
end

if __FILE__ == $0 then
    main()
end

n,a = gets.split.map(&:to_i)
d = gets.split.map(&:to_i)
sum = d.inject(:+)

ans = d.map do |di|
  less = [0, a - (sum - di) - 1].max
  more = [0, di - (a - (n - 1))].max

  less + more
end

puts ans.join(' ')


Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, which is based on Splay trees. Specifically, he is now studying the expose procedure.Unfortunately, Rostislav is unable to understand the definition of this procedure, so he decided to ask programmer Serezha to help him. Serezha agreed to help if Rostislav solves a simple task (and if he doesn't, then why would he need Splay trees anyway?)Given integers l, r and k, you need to print all powers of number k within range from l to r inclusive. However, Rostislav doesn't want to spent time doing this, as he got interested in playing a network game called Agar with Gleb. Help him!

The first line of the input contains three space-separated integers l, r and k (1 ≤ l ≤ r ≤ 1018, 2 ≤ k ≤ 109).

Print all powers of number k, that lie within range from l to r in the increasing order. If there are no such numbers, print "-1" (without the quotes).

NoteNote to the first sample: numbers 20 = 1, 21 = 2, 22 = 4, 23 = 8 lie within the specified range. The number 24 = 16 is greater then 10, thus it shouldn't be printed.

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

a = 1

a *= k while a < l

f = true

while a <= r
  print " " if not f
  f = false
  print a
  a *= k
end

if f
  puts "-1"
elsif
  puts
end

l, r, k = gets.split.map(&:to_i)
x = 1
done = 0
while x<=r
    if x>=l
        print(x," ")
        done=1
    end
    x*=k
end

if done==0
    p -1
end

    require 'scanf'

	x=scanf("%d")[0]
    y=scanf("%d")[0]
    k=scanf("%d")[0]
	o=1
	ch=1
	while o<x

		o*=k
	end
	while o>=x&&o<=y

		print o
        print " "
		o*=k
		ch=0
	end
	if ch==1
		puts -1
	end

require 'scanf'

while s = $stdin.gets() do
  l, r, k = s.scanf("%d %d %d")
  n = 1
  f = false
  while n <= r do
    if l <= n && n <= r then
      if f then
        print(" ")
      end
      f = true
      print(n)
    end
    n *= k;
  end
  if f then
    puts("");
  else
    puts("-1");
  end
end

l,r,k = gets.chomp.split.map(&:to_i)
ans = []
c = 1
while c <= r
  ans << c if l <= c
  c *= k
end

puts ans.size == 0 ? -1 : ans.join(" ")

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

l, r, k = gis
v = 1
a = []
loop do
    if v <= r
        a << v if v >= l
    else
        break
    end
    v *= k
end

if a.empty?
    puts '-1'
else
    puts a * " "
end

l,r,k=gets.split.map(&:to_i)
a=[]
64.times{|i|a.push k**i if k**i>=l&&k**i<=r}
puts (a.last)? a.join(' '):-1

l, r, k = gets.chomp.split(' ').map(&:to_i)

ans = []

power = 1

while power <= r do
  if power >= l then
    ans << power
  end
  power *= k
end

if ans.size == 0 then
  puts -1
else
  puts ans.join(' ')
end

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

ans=0
i=0
loop do
if a[2]**i>=a[0] && a[2]**i<=a[1]
printf "#{a[2]**i} "
ans+=1
end
i+=1
break if a[2]**i>a[1]
end
if ans==0
puts "-1"
end

l,r,k=gets.split.map(&:to_i)
ans=[]
64.times do |i|
	break if k**i>r
	ans.push k**i if k**i>=l
end
ans=[-1] if !ans.last
puts ans.join(' ')

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

ll = (Math.log(l) / Math.log(k)).round
lr = (Math.log(r) / Math.log(k)).round
ns = (ll..lr).map { |x| k**x }.select { |x| l <= x && x <= r }

if ns.size == 0
  puts -1
else
  puts ns.*' '
end

def solve(l, r, k)
   ans = []
   p = 0
   while k**p <= r do
      ans << k**p
      p+= 1
   end
   ret = []
   for i in ans do
      if i >= l && i <= r
          ret << i
      end
   end
   return -1 if ret.length == 0
   return ret.join(' ')
end
l, r, k = gets.strip.split(" ").map(&:to_i)
puts solve(l, r, k)

s = gets.to_s

m = Array.new(s.split(" "))

l = m[0].to_i
r = m[1].to_i
k = m[2].to_i

s = ""

for i in 0 ... 74
	p = k ** i
	if l <= p && p <= r
		s += (p.to_s + " ")
	end
end

if s == "" then puts "-1"
else puts s end

a = gets.chomp.split(" ").map{|x| x.to_i}
kek = 0
purkka = false
a[1].times do |i|
  s = a[2] ** i
  if s <= a[1] and s >= a[0]
     puts s
     purkka = true
  elsif (s > a[1])
    break
  end
end
puts "-1" if not purkka

#!/usr/bin/ruby

l,r,k = gets.strip.split.map {|num| num.to_i}

num = k
while num < l do
	num *= k
end

if l == 1
	num = 1
end

if l <= num and num <= r
	while l <= num and num <= r do
		print num
		print ' '
		num *= k
	end
	puts ''
else
	puts -1
end

numbers = gets.split(" ")
first = numbers[0].to_i
last = numbers[1].to_i
potencial = numbers[2].to_i
res = ""
first_exponencial = Math.log10(first)/Math.log10(potencial)
last_exponencial = Math.log(last)/Math.log10(potencial)
array = []
if first_exponencial - first_exponencial.to_i > 0
	first_exponent  = first_exponencial.to_i + 1
else
	first_exponent = first_exponencial.to_i
end
for i in first_exponent.to_i..last_exponencial.to_i
	if potencial ** i <= last && potencial**i >= first
		array << potencial ** i
	end
end
array.each do |current|
	res << current.to_s + " "
end
if array.size == 0
	puts "-1"
else
	puts res
end

l, r, k = gets.chomp.split(" ").map(&:to_i)

from = Math.log(l, k)
to = Math.log(r, k)

a = ((from.floor)..(to.ceil)).to_a.map{|i| k**i}.keep_if{|i| i >= l and i <= r}
if a.length == 0 then
  puts -1
else
  puts a.join(" ")
end

input = gets.chomp
i = input.split(" ")

l = i[0].to_i
r = i[1].to_i
k = i[2].to_i

aux = 1
flag = false

while aux<=r
	if aux>=l
		flag = true
		print "#{aux} "
	end
	aux *= k
end

if !flag
	print "-1 "
end

print "\n"

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

x=1
t=0

60.times{
    if (x>=l && x<=r)
        print x , " "
        t=1
    end

    x=x*k
    if (x>r) then break; end
}

if (t==0)
    print -1
end

class Solver
	def main
		l, r, k = gets.split.map { |x| x.to_i }
		ans = []
		64.times do |i|
			x = k ** i
			ans << x if x.between? l, r
		end
		if ans.empty?
			puts -1
		else
			puts ans.join ' '
		end
	end
end

Solver.new.main

a=gets.chomp.split(" ")
num=a[2].to_i
min=(Math.log(a[0].to_i,num)).ceil
max=(Math.log(a[1].to_i,num)).floor
if min-max==1
puts "-1"
else
min.upto(max) do |i|
printf "#{num**i} "
end
end

def solve(l, r, k)
   if k < l || k > r
       return "-1"
   end
   printed = false
   p = 0
   ans = ""
   while k**p <= r do
        if k**p >= l
           ans << (k**p).to_s << " "
           printed = true
       end
       p += 1
   end
   return "-1" if printed == false
   return ans.strip
end
l, r, k = gets.strip.split(" ").map(&:to_i)
puts solve(l, r, k)

def solve(l, r, k)
   if k < l || k > r
       return "-1"
   end
   printed = false
   p = 0
   ans = ""
   while k**p <= r do
       ans << k**p << " "
       p += 1
       printed = true
   end
   return "-1" if printed == false
   return ans.strip
end
l, r, k = gets.strip.split(" ").map(&:to_i)
puts solve(l, r, k)

def solve(l, r, k)
   if k < l || k > r
       return "-1"
   end
   printed = false
   p = 0
   ans = ""
   while k**p <= r do
       ans << (k**p).to_s << " "
       p += 1
       printed = true
   end
   return "-1" if printed == false
   return ans.strip
end
l, r, k = gets.strip.split(" ").map(&:to_i)
puts solve(l, r, k)

numbers = gets.split(" ")
first = numbers[0].to_i
last = numbers[1].to_i
potencial = numbers[2].to_i
res = ""
first_exponencial = Math.log10(first)/Math.log10(potencial)
last_exponencial = Math.log(last)/Math.log10(potencial)
array = []
if first_exponencial - first_exponencial.to_i > 0
	first_exponent  = first_exponencial.to_i + 1.0
else
	first_exponent = first_exponencial.to_i
end
for i in first_exponent.to_i..last_exponencial.to_i
	if potencial ** i <= last + 1
		array << potencial ** i
	end
end
array.each do |current|
	res << current.to_s + " "
end
if array.size == 0
	puts "-1"
else
	puts res
end

numbers = gets.split(" ")
first = numbers[0].to_i
last = numbers[1].to_i
potencial = numbers[2].to_i
res = ""
first_exponencial = Math.log(first, potencial)
last_exponencial = Math.log(last, potencial)
array = []
if first_exponencial - first_exponencial.to_i > 0
	first_exponent  = first_exponencial.to_i + 1
else
	first_exponent = first_exponencial.to_i
end
for i in first_exponent.to_i..last_exponencial.to_i
	if potencial ** i <= last
		array << potencial ** i
	end
end
array.each do |current|
	res << current.to_s + " "
end
if array.size == 0
	puts "-1"
else
	puts res
end

numbers = gets.split(" ")
first = numbers[0].to_i
last = numbers[1].to_i
potencial = numbers[2].to_i
res = ""
first_exponencial = Math.log(first, potencial)
last_exponencial = Math.log(last, potencial)
array = []
if first_exponencial - first_exponencial.to_i > 0
	first_exponent  = first_exponencial.to_i + 1
else
	first_exponent = first_exponencial.to_i
end
for i in first_exponent.to_i..last_exponencial.to_i
	if potencial ** i < last
		array << potencial ** i
	end
end
array.each do |current|
	res << current.to_s + " "
end
if array.size == 0
	puts "-1"
else
	puts res
end

numbers = gets.split(" ")
first = numbers[0].to_i
last = numbers[1].to_i
potencial = numbers[2].to_i
res = ""
first_exponencial = Math.log(first, potencial)
last_exponencial = Math.log(last, potencial)
array = []
if first_exponencial - first_exponencial.to_i > 0
	first_exponent  = first_exponencial.to_i + 1
else
	first_exponent = first_exponencial.to_i
end
for i in first_exponent.to_i..last_exponencial.to_i
	array << potencial ** i
end
array.each do |current|
	res << current.to_s + " "
end
if array.size == 0
	puts "-1"
else
	puts res
end

numbers = gets.split(" ")
first = numbers[0].to_i
last = numbers[1].to_i
potencial = numbers[2].to_i
res = ""
first_exponencial = Math.log10(first)/Math.log10(potencial)
last_exponencial = Math.log(last)/Math.log10(potencial)
array = []
if first_exponencial - first_exponencial.to_i > 0
	first_exponent  = first_exponencial.to_i + 1
else
	first_exponent = first_exponencial.to_i
end
for i in first_exponent.to_i..last_exponencial.to_i
	if potencial ** i <= last
		array << potencial ** i
	end
end
array.each do |current|
	res << current.to_s + " "
end
if array.size == 0
	puts "-1"
else
	puts res
end

l, r, k = gets.chomp.split(" ").map(&:to_i)

from = Math.log(l, k)
to = Math.log(r, k)

if to.floor - from.ceil <= 0 then
  puts -1
else
  puts ((from.ceil)..(to.floor)).to_a.map{|i| k**i}.join(" ")
end


You are given n points on the straight line — the positions (x-coordinates) of the cities and m points on the same line — the positions (x-coordinates) of the cellular towers. All towers work in the same way — they provide cellular network for all cities, which are located at the distance which is no more than r from this tower.Your task is to find minimal r that each city has been provided by cellular network, i.e. for each city there is at least one cellular tower at the distance which is no more than r.If r = 0 then a tower provides cellular network only for the point where it is located. One tower can provide cellular network for any number of cities, but all these cities must be at the distance which is no more than r from this tower.

The first line contains two positive integers n and m (1 ≤ n, m ≤ 105) — the number of cities and the number of cellular towers. The second line contains a sequence of n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the coordinates of cities. It is allowed that there are any number of cities in the same point. All coordinates ai are given in non-decreasing order. The third line contains a sequence of m integers b1, b2, ..., bm ( - 109 ≤ bj ≤ 109) — the coordinates of cellular towers. It is allowed that there are any number of towers in the same point. All coordinates bj are given in non-decreasing order.

Print minimal r so that each city will be covered by cellular network.

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

as.uniq!
bs.uniq!
n = as.size
m = bs.size

ans = 0
l = 0
r = 0
for i in 0...n
  while r < m - 1 && bs[r] <= as[i]
    r += 1
  end
  l = [r - 1, 0].max
  ans = [ans, [(as[i] - bs[l]).abs, (as[i] - bs[r]).abs].min].max
end

puts ans


Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold:   there is no actor in the cell the spotlight is placed to;  there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.

The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.

Print one integer — the number of good positions for placing the spotlight.

NoteIn the first example the following positions are good:  the (1, 1) cell and right direction;  the (1, 1) cell and down direction;  the (1, 3) cell and left direction;  the (1, 3) cell and down direction;  the (1, 4) cell and left direction;  the (2, 2) cell and left direction;  the (2, 2) cell and up direction;  the (2, 2) and right direction;  the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.

def f(m)
 c=0
 m.each{|e|
  s=e.count 1
  l=e.size
  if s==1
   c+=l-1
  elsif s>1
   x=e.index 1
   y=e.reverse.index 1
   c+=x+2*(l-(x+y)-s)+y
  end
 }
 c
end
gets
m=$<.map{|e|e.chomp.split.map(&:to_i)}
p f(m)+f(m.transpose)

gets
m=$<.map{|e|e.chomp.split.map(&:to_i)}
p [m,m.transpose].reduce(0){|r,m|
 c=0
 m.each{|e|
  s=e.count 1
  l=e.size
  if s==1
   c+=l-1
  elsif s>1
   x=e.index 1
   y=e.reverse.index 1
   c+=x+2*(l-(x+y)-s)+y
  end
 }
 r+c
}

H,W=gets.split.map(&:to_i)
m=$<.map{|e|e.chomp.tr(' ','').chars}
col=m.map{|e|e.join.to_i(2)}.reduce(:&)
row=m.transpose.map{|e|e.join.to_i(2)}.reduce(:&)
r=0
H.times{|h|W.times{|w|
 r+=1 if m[h][w]=='0' and col[w]>0||row[h]>0
}}
p r


You are given a set of n vectors on a plane. For each vector you are allowed to multiply any of its coordinates by -1. Thus, each vector vi = (xi, yi) can be transformed into one of the following four vectors:  vi1 = (xi, yi),  vi2 = ( - xi, yi),  vi3 = (xi,  - yi),  vi4 = ( - xi,  - yi).  You should find two vectors from the set and determine which of their coordinates should be multiplied by -1 so that the absolute value of the sum of the resulting vectors was minimally possible. More formally, you should choose two vectors vi, vj (1 ≤ i, j ≤ n, i ≠ j) and two numbers k1, k2 (1 ≤ k1, k2 ≤ 4), so that the value of the expression |vik1 + vjk2| were minimum.

The first line contains a single integer n (2 ≤ n ≤ 105). Then n lines contain vectors as pairs of integers "xi yi" ( - 10000 ≤ xi, yi ≤ 10000), one pair per line.

Print on the first line four space-separated numbers "i k1 j k2" — the answer to the problem. If there are several variants the absolute value of whose sums is minimum, you can print any of them.

NoteA sum of two vectors v = (xv, yv) and u = (xu, yu) is vector s = v + u = (xv + xu, yv + yu).An absolute value of vector v = (x, y) is number . In the second sample there are several valid answers, such as:(3 1 4 2), (3 1 4 4), (3 4 4 1), (3 4 4 3), (4 1 3 2), (4 1 3 4), (4 2 3 1).

n, cd = nil

File.open("input.txt") do |f|
  n = f.gets.to_i
  cd = []
  n.times do |i|
    x, y = f.gets.split.map(&:to_i)
    cd << [x.abs, y.abs, i+1, x, y]
  end
end

#cd = cd.sort_by{|a,b,c,u,v| [u, v]}
cd = cd.sort

mn = 1e20
ret = []
s = 0
lp = 0
n.times do |i|
  #idx, x0, y0, u0, v0 = cd[i]
  u0, v0, idx, x0, y0 = cd[i]
  break if mn == 0
  s += 1 while s < n && (cd[s][0]-u0)**2 > mn
  break if s >= n

  (s..n-1).each do |j|
    lp += 1
    #jdx, x1, y1, u1, v1 = cd[j]
    u1, v1, jdx, x1, y1 = cd[j]
    next if idx == jdx

    break if (u1-u0)**2 > mn

    len = (u1-u0)**2 + (v1-v0)**2

    if len < mn
      d0 = (x0<0 ? 2 : 1) + (y0<0 ? 2 : 0)
      d1 = (x1>0 ? 2 : 1) + (y1>0 ? 2 : 0)
      #p [idx, [u0,v0], jdx, [u1,v1], len]

      mn = len
      ret = [idx, d0, jdx, d1]
    end

  end
end

#p lp
#p ret

File.open("output.txt", "w") do |f|
  f.puts ret*" "
end


Dreamoon is a big fan of the Codeforces contests.One day, he claimed that he will collect all the places from $$$1$$$ to $$$54$$$ after two more rated contests. It's amazing!Based on this, you come up with the following problem:There is a person who participated in $$$n$$$ Codeforces rounds. His place in the first round is $$$a_1$$$, his place in the second round is $$$a_2$$$, ..., his place in the $$$n$$$-th round is $$$a_n$$$.You are given a positive non-zero integer $$$x$$$.Please, find the largest $$$v$$$ such that this person can collect all the places from $$$1$$$ to $$$v$$$ after $$$x$$$ more rated contests.In other words, you need to find the largest $$$v$$$, such that it is possible, that after $$$x$$$ more rated contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.For example, if $$$n=6$$$, $$$x=2$$$ and $$$a=[3,1,1,5,7,10]$$$ then answer is $$$v=5$$$, because if on the next two contest he will take places $$$2$$$ and $$$4$$$, then he will collect all places from $$$1$$$ to $$$5$$$, so it is possible to get $$$v=5$$$.

The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5$$$) denoting the number of test cases in the input. Each test case contains two lines. The first line contains two integers $$$n, x$$$ ($$$1 \leq n, x \leq 100$$$). The second line contains $$$n$$$ positive non-zero integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$).

For each test case print one line containing the largest $$$v$$$, such that it is possible that after $$$x$$$ other contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.

NoteThe first test case is described in the statement.In the second test case, the person has one hundred future contests, so he can take place $$$1,2,\ldots,99$$$ and place $$$101$$$ on them in some order, to collect places $$$1,2,\ldots,101$$$.

t = gets.chomp.to_i
while t>0 do
    tmp = gets.chomp.split(" ")
    n = tmp[0].to_i
    k = tmp[1].to_i
    a = gets.chomp.split(" ")
    cnt = Array.new(205,0)
    for i in 0..n-1
        cnt[a[i].to_i] += 1
    end
    ans = 1
    while k>=0 do
        if cnt[ans]==0 then
            k -= 1
        end
        ans += 1
    end
    puts ans-2
    t -= 1
end

t = gets.to_i
t.times do
  n, x = gets.split.map(&:to_i)
  a = gets.split.map(&:to_i).sort.uniq
  v = 0
  a.each do |aa|
    if aa - (v + 1) <= x
      x -= aa - (v + 1)
      v = aa
    else
      break
    end
  end
  v += x
  puts v
end

t = gets.chomp.to_i
while t>0 do
    tmp = gets.chomp.split(" ")
    n = tmp[0].to_i
    k = tmp[1].to_i
    a = gets.chomp.split(" ")
    cnt = Array.new(205,0)
    for i in 0..n-1
        cnt[a[i].to_i] += 1
    end
    ans = 1
    while k>=0 do
        if cnt[ans]==0 then
            k -= 1
        end
        ans += 1
    end
    puts ans-1
    t -= 1
end


Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.Vasya has a train lighting schedule for all n hours of the trip — n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.

The first input line contains two integers n and k (1 ≤ n ≤ 1000, 1 ≤ k ≤ n) — the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers ai (0 ≤ ai ≤ 100), ai is the light level at the i-th hour.

In the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b1, b2, ..., bk, — the indexes of hours Vasya will read at (1 ≤ bi ≤ n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers bi in an arbitrary order.

NoteIn the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20.

if ENV['USER'] != 'watashi'
  $stdin = open('input.txt')
  $> = open('output.txt', 'w')
end

I = ->{gets.split.map(&:to_i)}
n, m = I[]
a = I[].zip([*1..n]).sort[n - m, m]
p a[0][0]
puts a.map{|_| _[1]} * ' '

$stdin=File.open('input.txt')
$stdout=File.open('output.txt','w')
(n,k),a=gets.split.map(&:to_i),gets.split.map(&:to_i)
z=p a.sort[-k]
b=[]
n.times{|i|
	b<<i+1 if a[i]>=z
}
puts b.sort[0...k]*' '

$stdin=File.open('input.txt')
$stdout=File.open('output.txt', 'w')
a,b=Array.new,Array.new
n,a=gets.split.map(&:to_i)[1],gets.split.map(&:to_i)
puts a.sort[-n]
n.times do
b<<a.index(a.max)+1
a[a.index(a.max)]=-1
end
b.reverse!
puts b.sort.join(" ")

input = File.open("input.txt", "r")
output = File.open("output.txt", "w")

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

a = Array.new
for i in 0...n
  a << [rin[i].to_i, i+1]
end

a.sort! {|x, y| x[0] <=> y[0]}
a = a.drop(n-k)
output.puts a[0][0]

a.sort! {|x, y| x[1] <=> y[1]}
a.each {|x| output.print x[1], " "}

input.close
output.close

$stdin=open("input.txt")
$stdout=open("output.txt","w")
def search(a,b,n)
	n.times do |i|
		if a[i]==b
			a[i]=101
			return i+1
		end
	end
end
n,k=gets.chomp.split.map(&:to_i)
a=gets.chomp.split.map(&:to_i)
b=a.sort
puts b[n-k]
for i in (n-k)...n
	print("#{search(a,b[i],n)} ")
end
puts

$stdin=File.open('input.txt')
$stdout=File.open('output.txt','w')
n, m = gets.split.map{|i| i.to_i}
a = gets.split.map{|i| i.to_i}
p mins = a.sort[-m]
b = []
n.times do |i|
  b << i + 1 if a[i] >= mins
end
puts b.sort[0...m]*' '

n = 0
k = 0
arr = []
open("input.txt") {|f|
  n, k = f.gets.split.map(&:to_i)
  arr = f.gets.to_s.chomp.split.map(&:to_i)
}
File.open("output.txt", "w"){|f|
  arr2 = []
  i = 0
  arr.each do |a|
    i += 1
    arr2.push([i, a])
  end
  arr2.sort! {|a,b| a[1] <=> b[1] }
  f.write "#{arr2[n-k][1]}\n"
  k.times do |i|
    f.write "#{arr2[n-k+i][0]}"
    if i != k-1
      f.write " "
    else
      f.write "\n" if i == k-1
    end
  end
}

n,k=0
a=[]
File.open("input.txt") { |f|
    n,k=f.gets.split.map(&:to_i)
    a=f.gets.split.map(&:to_i)
}
b=[]
a.each_with_index do |v,i|
    b.push([-v,i+1])
end
b.sort!
File.open("output.txt", "w") { |f|
    f.puts -b[k-1][0]
    for i in 0..k-1
        f.print "#{b[i][1]} "
    end
}

line = File.read("input.txt")

inputs = line.split("\n")

n,k = inputs[0].split(" ").map(&:to_i)
arr = inputs[1].split(" ").map(&:to_i)
list = arr.each_with_index.map { |a,i| [a,i]  }

list.sort!
# puts "#{list.min}"
ans = list[((-k)..-1)]

File.open("output.txt", 'w') { |file|
	file.puts "#{ans.min[0]}"
	file.puts ans.map { |e| e[1]+1  }.join(" ")
}

File.open("input.txt", "r") do |input|
	File.open("output.txt", "w") do |output|
		n, k = input.readline.split.map(&:to_i)
		output << input.readline.split.map(&:to_i).zip((1..n).to_a).sort {|x, y| y.first <=> x.first}.take(k).reduce([101, []]) { |s, (x, y)|
			[[s.first, x].min, s.last + [y]]
		}.join(' ')
	end
end

input = File.open("input.txt", "r")
output = File.open("output.txt", "w")

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

a = Array.new
for i in 0...n
  a << [rin[i].to_i, i+1]
end

a.sort! {|x, y| x[0] <=> y[0]}
a = a.drop(n-k)
puts a[0][0]

a.sort! {|x, y| x[1] <=> y[1]}
a.each {|x| print x[1], " "}

input.close
output.close

$stdin=File.open('input.txt')
$stdout=File.open('output.txt','w')
n, m = gets.split.map{|i| i.to_i}
a = gets.split.map{|i| i.to_i}
p mins = a.sort[-m]
b = []
n.times do |i|
  b << i + 1 if a[i] >= mins
end
puts b.join " "

$stdin=File.open('input.txt')
$stdout=File.open('output.txt','w')
n, m = gets.split.map{|i| i.to_i}
a = gets.split.map{|i| i.to_i}
p a.sort
p mins = a.sort[-m]
b = []
n.times do |i|
  b << i + 1 if a[i] >= mins
end
puts b.join " "

n,k=0
a=[]
File.open("input.txt") { |f|
    n,k=f.gets.split.map(&:to_i)
    a=f.gets.split.map(&:to_i)
}
b=[]
a.each_with_index do |v,i|
    b.push([-v,i+1])
end
b.sort!
File.open("output.txt", "w") { |f|
    for i in 0..k-1
        f.print "#{b[i][1]} "
    end
}


Vasya and Petya are playing a simple game. Vasya thought of number x between 1 and n, and Petya tries to guess the number.Petya can ask questions like: "Is the unknown number divisible by number y?".The game is played by the following rules: first Petya asks all the questions that interest him (also, he can ask no questions), and then Vasya responds to each question with a 'yes' or a 'no'. After receiving all the answers Petya should determine the number that Vasya thought of.Unfortunately, Petya is not familiar with the number theory. Help him find the minimum number of questions he should ask to make a guaranteed guess of Vasya's number, and the numbers yi, he should ask the questions about.

A single line contains number n (1 ≤ n ≤ 103).

Print the length of the sequence of questions k (0 ≤ k ≤ n), followed by k numbers — the questions yi (1 ≤ yi ≤ n). If there are several correct sequences of questions of the minimum length, you are allowed to print any of them.

NoteThe sequence from the answer to the first sample test is actually correct.If the unknown number is not divisible by one of the sequence numbers, it is equal to 1.If the unknown number is divisible by 4, it is 4.If the unknown number is divisible by 3, then the unknown number is 3.Otherwise, it is equal to 2. Therefore, the sequence of questions allows you to guess the unknown number. It can be shown that there is no correct sequence of questions of length 2 or shorter.

n = gets.chomp.to_i

ps = [2]
for i in 3..1000
  f = true
  for j in 0..ps.size-1
    if i % ps[j] == 0
      f = false
      break
    end
  end
  ps << i if f
end

ys = []
ps.each do |p|
  q = 1
  while p ** q <= n
    ys << p**q
    q += 1
  end
end

puts ys.size
puts ys.*' '

def is_prime(x)
	m = (x**0.5).to_i
	for i in (2..m)
		if x % i == 0
			return false
		end
	end
	return true
end

n = gets.chomp.to_i
ans = []

for i in (2..n)
	if is_prime(i)
		j = i
		while j <= n
			ans << j
			j *= i
		end
	end
end

puts ans.length
puts ans.join(' ')

require "Prime"
a=gets.chomp.to_i
b=(Math.sqrt(a)).floor
sp=[]
p=[]
Prime.each(a) do|i|
if i<=b
sp<<i
else
p<<i
end
end
ad=[]
sp.each do |i|
temp=[]
pow=1
loop do
if i**pow<=a
temp<<(i**pow)
pow+=1
else
break
end
end
ad=ad+temp
end
ans=ad+p
puts "#{ans.length}"
ans.each {|i| printf "#{i} "}

require 'prime'

class Solver
	def main
		n = gets.to_i
		ans = []
		2.upto(n) do |i|
			if Prime.prime? i
				tmp = i
				while tmp <= n do
					ans << tmp
					tmp *= i
				end
			end
		end
		puts ans.size
		puts ans.join ' '
	end
end
Solver.new.main


When Valera has got some free time, he goes to the library to read some books. Today he's got t free minutes to read. That's why Valera took n books in the library and for each book he estimated the time he is going to need to read it. Let's number the books by integers from 1 to n. Valera needs ai minutes to read the i-th book.Valera decided to choose an arbitrary book with number i and read the books one by one, starting from this book. In other words, he will first read book number i, then book number i + 1, then book number i + 2 and so on. He continues the process until he either runs out of the free time or finishes reading the n-th book. Valera reads each book up to the end, that is, he doesn't start reading the book if he doesn't have enough free time to finish reading it. Print the maximum number of books Valera can read.

The first line contains two integers n and t (1 ≤ n ≤ 105; 1 ≤ t ≤ 109) — the number of books and the number of free minutes Valera's got. The second line contains a sequence of n integers a1, a2, ..., an (1 ≤ ai ≤ 104), where number ai shows the number of minutes that the boy needs to read the i-th book.

Print a single integer — the maximum number of books Valera can read.

n, t = gets.split.map(&:to_i)
l=0
r=-1
sum=0
an = 0
a = gets.split.map(&:to_i)
a.each do |key|
	while (sum+key>t)
		sum -= a[l]
		l += 1
	end
	sum += key
	r += 1
	an = [an, r-l+1].max
end
puts an

#!/usr/bin/ruby1.9

n, t = STDIN.readline.split.collect {|s| s.to_i }
a = STDIN.readline.split.collect {|s| s.to_i }

max = 0
left = right = 0
sum = 0

while right != n
	sum += a[right]
	right += 1
	while sum > t
		sum -= a[left]
		left += 1
	end
	if right-left > max
		max = right-left
	end
end

puts max

n,t=gets.chomp.split.map(&:to_i)
a=gets.chomp.split.map(&:to_i)
s,l,ans=0,0,0
a.count.times do |i|
	s+=a[i]
	while s>t
		s-=a[l]
		l+=1
	end
	d=(i-l+1)
	ans=d if ans<d
end
puts ans

def dp(str)
  puts str if false
end

def run
  n, t = gets.split.map(&:to_i)
  a = gets.split.map(&:to_i)

  sum = pos = max = val = 0
  n.times do |i|
    dp "sum = #{sum}, a[pos] = #{a[pos]}"
    while pos < n and a[pos] + sum <= t
      dp "pos = #{pos}"
      sum += a[pos]
      pos = pos + 1
    end

    val = pos - i
    sum -= a[i]
    dp "[in]  i = #{i}, max = #{max}, pos = #{pos}, sum = #{sum}, val = #{val}"
    max = [max, val].max
    dp "[out] i = #{i}, max = #{max}, pos = #{pos}, sum = #{sum}, val = #{val}"
  end

  puts max
end

run if $0 == __FILE__

def run
  n, t = gets.split.map(&:to_i)
  a = gets.split.map(&:to_i)

  sum = pos = max = val = 0
  n.times do |i|
    while pos < n and a[pos] + sum <= t
      sum += a[pos]
      pos += 1
    end

    max = [max, pos - i].max
    sum -= a[i]
  end

  puts max
end

run if $0 == __FILE__

n,m=gets.split.map{|e| e.to_i}
x=gets.split.map{|e| e.to_i}

ans=0
r=0
l=0
sum=x[0]
while r<n && l<n
	while sum>m && l<n
		sum-=x[l]
		l+=1
	end
	while sum<=m && r<n-1
		break if sum+x[r+1]>m
		sum+=x[r+1]
		r+=1
	end
	ans=[ans,r-l+1].max
	#p [l,r,sum,ans]
	break if r==n-1
	sum+=x[r+1]
	r+=1
end
puts ans

# coding: utf-8

# if __FILE__ == $0
# end

def bsearch(arr, b, goal)
    e = arr.length
    while b < e
        mid = (b + e)/2
        if arr[mid] == goal
            b = mid
            break
        elsif arr[mid] < goal
            b = mid
            if e - b == 1
                break
            end
        else
            e = mid
            if e - b == 1
                break
            end
        end
    end
    return b
end

n, t = gets.split.map(&:to_i)
acc = 0
ar = [0]
gets.split.each { |i|
    i = i.to_i
    acc += i
    ar << acc
}
i, j = 0, 0 # indexing: [i, j], inclusive range. be careful!
maximum_books = 0
# p ar
loop do
    #p "i: #{i}, ar[i] + t: #{ar[i] + t}"
    nj = bsearch(ar, i, ar[i] + t)
    books = nj - i
    if maximum_books < books
        maximum_books = books
    end
    j = nj
    i += 1
    break if j >= ar.length - 1
end

puts maximum_books

# def get_count(arr,l,r,t)

# 	while l >= 0 && l <= r
# 		m = l + (r-l) / 2
# 		puts "#{l} #{r}"
# 		return 1 if r ==m && arr[m] <= t
# 		if arr[r] - arr[m] > t
# 			l = m + 1
# 		elsif arr[r] - arr[m] < t
# 			if arr[r] - arr[m-1] <= t
# 				l = m - 1
# 			else
# 				puts "less case #{l} #{r} #{m}"
# 				return r - m
# 			end
# 		else
# 			puts "equal case #{l} #{r} #{m}"
# 			return r - m
# 		end
# 	end
# 	return 0
# end

#For each r what is the l such that sum[l..r] <= t and l..r is maximum
# def get_count(arr,l,r,t)
# 	# return r - l if arr[r] - arr[l] <= t
# 	return r - l + 1 if arr[r] <= t
# 	(l..(r-1)).each do |i|
# 		# puts "#{r} #{i}"
# 		return r - i if arr[r] - arr[i] <= t
# 	end
# 	return 0
# end

# def get_count(arr,l,r,t)
# 	# return r - l if arr[r] - arr[l] <= t
# 	return r - l + 1 if arr[r] <= t

# 	while l >=0 && l < r
# 		if arr[r] - arr[l] > t
# 			m = l + ((r-l)/2.0).ceil
# 			l = m
# 			# puts "#{l} #{m} #{r} #{l + (r-l)/2}"
# 		elsif arr[r] - arr[l] < t
# 			if l !=0 && arr[r] - arr[l-1] < t
# 				l = l - 1
# 			else
# 				return r - l
# 			end
# 		else
# 			return r - l
# 		end
# 	end

# 	return 0
# end

# def get_count(arr,l,r,t)
# 	# return r - l if arr[r] - arr[l] <= t
# 	# puts "#{l} #{r}"
# 	return r - l + 1 if arr[r] && arr[r] <= t

# 	while arr[r] && l < r
# 		m = l + (r-l) / 2
# 		if arr[l] + t > arr[r]
# 			r = m
# 		else
# 			if arr[]
# 			 	r =  r + 1
# 		 	else
# 		 		return r - l
# 		 	end

# 		end
# 	end

# 	return 0
# end

def upper_bound(arr,l,r,x)
	best = nil
	while l <= r
		m = (l+r)/2
		# puts "#{l} #{r} #{m}"
		if arr[m] <= x
			best = m
			# puts "m #{arr[m]} x #{x}"
			l = m + 1
		else
			r = m - 1
		end
	end
	return best

end
# puts upper_bound([1,2,3,4,5,6],0,5,10)
# puts upper_bound([1,2,3,6,7,8],0,5,4)
# puts upper_bound([1,2,3,4,5,6],0,5,0)
# puts upper_bound([1,2,3,4,5,6],0,5,9)
n,t = gets.split(" ").map(&:to_i)
arr = gets.split(" ").map(&:to_i)
sum_arr = [arr[0]]
(1..(n-1)).each do |i|
	sum_arr[i] = sum_arr[i-1] + arr[i]
end
# puts "#{sum_arr}"
max = 0
unless sum_arr[n-1] <= t
	sum_arr.each_with_index{|a,i|
		# val = get_count(sum_arr,i,n-1,t)
		up = upper_bound(sum_arr,i,n-1,sum_arr[i]+t)
	 	if up
			val = up - i
		# puts "#{sum_arr[i]+t}"
			# val = 1 if up == i && arr[i] <= t
		end
		max = val if val && val > max
	}

else
	max = n
end
# 1 2 3 4 5
# 1 3 6 10 15
puts max
#

# Good - 3 days struggle finally accepted - http://canhazcode.blogspot.in/2012/02/we-need-to-talk-about-binary-search.html

n,t = gets.chomp.split(" ").collect! {|x| x.to_i};
line = gets.chomp.split(" ").collect! {|x| x.to_i};

p=0
while p<n and line[p]>t
  p+=1
end

if p==n
	puts 0
	exit
end

first = p
 q = p
max = 1
crt = 1
sum = 0
#puts p
#puts q
#puts line
while q<n

	#puts "- #{q} #{sum}"

	sum += line[q]
    #puts "- #{p} #{q} #{sum}"
	if sum<=t

		if (q-p+1)>max
			max = q-p+1
		end
	else
		while p<=q and sum>t
			sum -= line[p]
			p += 1
		end
	end
	q += 1
end

puts max

# http://codeforces.com/problemset/problem/279/B
n, t = gets.chomp.split.map(&:to_i)
list = gets.chomp.split.map(&:to_i)

max_books = 0
time = 0
l = 0
for r in 0..(list.length - 1)

  while time > t
    time -= list[l]
    l += 1
  end

  time += list[r]
  if time <= t
    max_books = [r - l + 1, max_books].max
  end
end

p max_books

def read_next_line
  gets.split.map { |x| x.to_i }
end

n, t = read_next_line
a = read_next_line

p = cur_time = 0
max = -1_000_000_001
for i in 0...n
  cur_time += a[i]
  while cur_time > t
    cur_time -= a[p]
    p += 1
  end
  cur = i - p
  max = cur if cur > max
end
puts max + 1

n, t = gets.split.map(&:to_i)
l=0
r=-1
sum=0
ans = 0
a = gets.split.map(&:to_i)
a.each do |key|
	while (sum+key>t)
		sum -= a[l]
		l += 1
	end
	sum += key
	r += 1
	ans = [ans, r-l+1].max
end
puts ans

a, b = gets.split(" ")

time = b.to_i

eachBookTime = []

gets.split(" ").each do |item|

    eachBookTime.push item.to_i

end

#eachBookTime = eachBookTime.sort

sum = 0

count = 0

first = 0

max = 0

eachBookTime.each do |item|

    while(sum + item > time && sum > 0)

        sum-=eachBookTime[first]

        first+=1

        count-=1

    end

    if(item < time)

        sum+=item

        count+=1

        max = count if count > max

    else

        sum+=item

        count+=1

    end

end

puts max

def npos(pos, n)
  (pos + 1) % n
end

def ppos(pos, n)
  (pos - 1 + n) % n
end

def run
  n, t = gets.split.map(&:to_i)
  a = gets.split.map(&:to_i)

  if a.min > t
    puts 0
    return
  end

  list = []
  sum = pos = max = val = 0
  n.times do |i|
    while sum <= t
      if list.length == n
        puts n
        return
      end

      sum += a[pos]
      list << a[pos]
      pos = npos(pos, n)
    end
    pos = ppos(pos, n)
    sum -= a[pos]
    list.pop

    if pos != i
      val = (ppos(pos, n) - i + n) % n + 1
      sum -= a[i]
      list.shift
      max = [max, val].max
    end
  end

  puts max
end

run if $0 == __FILE__

def npos(pos, n)
  (pos + 1) % n
end

def ppos(pos, n)
  (pos - 1 + n) % n
end

def run
  n, t = gets.split.map(&:to_i)
  a = gets.split.map(&:to_i)

  sum = pos = max = 0

  n.times do |i|
    while sum <= t
      sum += a[pos]
      pos = npos(pos, n)
    end
    pos = ppos(pos, n)
    sum -= a[pos]

    val = (ppos(pos, n) - i + n) % n + 1

    max = [max, val].max
    sum -= a[i]
  end

  puts max
end

run if $0 == __FILE__

def npos(pos, n)
  (pos + 1) % n
end

def ppos(pos, n)
  (pos - 1 + n) % n
end

def run
  n, t = gets.split.map(&:to_i)
  a = gets.split.map(&:to_i)

  sum = pos = max = 0

  n.times do |i|
    while sum <= t
      sum += a[pos]
      pos = npos(pos, n)
    end
    pos = ppos(pos, n)
    sum -= a[pos]

    max = [max, ppos(pos, n) - i + 1].max
    sum -= a[i]
  end

  puts max
end

run if $0 == __FILE__

def npos(pos, n)
  (pos + 1) % n
end

def ppos(pos, n)
  (pos - 1 + n) % n
end

def run
  n, t = gets.split.map(&:to_i)
  a = gets.split.map(&:to_i)

  if a.min > t
    puts 0
    return
  end

  list = []
  sum = pos = max = val = 0
  n.times do |i|
    while sum <= t
      if list.length == n
        puts n
        return
      end

      sum += a[pos]
      list << a[pos]
      pos = npos(pos, n)
    end
    pos = ppos(pos, n)
    sum -= a[pos]
    list.pop

    if pos == i
      val = (pos - i + n) % n + 1
    else
      val = (ppos(pos, n) - i + n) % n + 1
      sum -= a[i]
      list.shift
    end

    max = [max, val].max
  end

  puts max
end

run if $0 == __FILE__

# coding: utf-8

# if __FILE__ == $0
# end

def bsearch(arr, b, goal)
    e = arr.length
    while b < e
        mid = (b + e)/2
        if arr[mid] == goal
            b = mid
            break
        elsif arr[mid] < goal
            b = mid
            if e - b == 1
                break
            end
        else
            e = mid
            if e - b == 1
                break
            end
        end
    end
    return b
end

n, t = gets.split.map(&:to_i)
acc = 0
ar = []
gets.split.each { |i|
    i = i.to_i
    acc += i
    ar << acc
}
i, j = 0, 0 # indexing: [i, j], inclusive range. be careful!
maximum_books = 0
loop do
    nj = bsearch(ar, i, ar[i] + t)
    books = nj - i
    if nj == i && ar[nj] <= t
        books = 1
    end
    if maximum_books < books
        maximum_books = books
    end
    j = nj
    i += 1
    break if  j == (ar.length - 1)
end

puts maximum_books

# coding: utf-8

# if __FILE__ == $0
# end

def bsearch(arr, b, goal)
    e = arr.length
    while b < e
        mid = (b + e)/2
        if arr[mid] == goal
            b = mid
            break
        elsif arr[mid] < goal
            b = mid
            if e - b == 1
                break
            end
        else
            e = mid
            if e - b == 1
                break
            end
        end
    end
    return b
end

n, t = gets.split.map(&:to_i)
acc = 0
ar = []
gets.split.each { |i|
    i = i.to_i
    acc += i
    ar << acc
}
i, j = 0, 0 # indexing: [i, j], inclusive range. be careful!
maximum_books = 0
while j < (ar.length - 1)
    nj = bsearch(ar, i, ar[i] + t)
    if maximum_books < (nj - i)
        maximum_books = nj - i
    end
    j = nj
    i += 1
end

puts maximum_books

# def get_count(arr,l,r,t)

# 	while l >= 0 && l <= r
# 		m = l + (r-l) / 2
# 		puts "#{l} #{r}"
# 		return 1 if r ==m && arr[m] <= t
# 		if arr[r] - arr[m] > t
# 			l = m + 1
# 		elsif arr[r] - arr[m] < t
# 			if arr[r] - arr[m-1] <= t
# 				l = m - 1
# 			else
# 				puts "less case #{l} #{r} #{m}"
# 				return r - m
# 			end
# 		else
# 			puts "equal case #{l} #{r} #{m}"
# 			return r - m
# 		end
# 	end
# 	return 0
# end

#For each r what is the l such that sum[l..r] <= t and l..r is maximum
# def get_count(arr,l,r,t)
# 	# return r - l if arr[r] - arr[l] <= t
# 	return r - l + 1 if arr[r] <= t
# 	(l..(r-1)).each do |i|
# 		# puts "#{r} #{i}"
# 		return r - i if arr[r] - arr[i] <= t
# 	end
# 	return 0
# end

# def get_count(arr,l,r,t)
# 	# return r - l if arr[r] - arr[l] <= t
# 	return r - l + 1 if arr[r] <= t

# 	while l >=0 && l < r
# 		if arr[r] - arr[l] > t
# 			m = l + ((r-l)/2.0).ceil
# 			l = m
# 			# puts "#{l} #{m} #{r} #{l + (r-l)/2}"
# 		elsif arr[r] - arr[l] < t
# 			if l !=0 && arr[r] - arr[l-1] < t
# 				l = l - 1
# 			else
# 				return r - l
# 			end
# 		else
# 			return r - l
# 		end
# 	end

# 	return 0
# end

# def get_count(arr,l,r,t)
# 	# return r - l if arr[r] - arr[l] <= t
# 	# puts "#{l} #{r}"
# 	return r - l + 1 if arr[r] && arr[r] <= t

# 	while arr[r] && l < r
# 		m = l + (r-l) / 2
# 		if arr[l] + t > arr[r]
# 			r = m
# 		else
# 			if arr[]
# 			 	r =  r + 1
# 		 	else
# 		 		return r - l
# 		 	end

# 		end
# 	end

# 	return 0
# end

def upper_bound(arr,l,r,x)
	best = nil
	while l <= r
		m = l + (r-l)/2
		if arr[m] <= x
			best = m
			l = m + 1
		else
			r = m - 1
		end
	end
	return best

end

n,t = gets.split(" ").map(&:to_i)
arr = gets.split(" ").map(&:to_i)
sum_arr = [arr[0]]
(1..(n-1)).each do |i|
	sum_arr[i] = sum_arr[i-1] + arr[i]
end
# puts "#{sum_arr}"
max = 0
sum_arr.each_with_index{|a,i|
	# val = get_count(sum_arr,i,n-1,t)
	up = upper_bound(sum_arr,i,n-1,sum_arr[i]+t)
	val = up - i if up
	max = val if val && val > max
}

# 1 2 3 4 5
# 1 3 6 10 15
puts max
#

# def get_count(arr,l,r,t)

# 	while l >= 0 && l <= r
# 		m = l + (r-l) / 2
# 		puts "#{l} #{r}"
# 		return 1 if r ==m && arr[m] <= t
# 		if arr[r] - arr[m] > t
# 			l = m + 1
# 		elsif arr[r] - arr[m] < t
# 			if arr[r] - arr[m-1] <= t
# 				l = m - 1
# 			else
# 				puts "less case #{l} #{r} #{m}"
# 				return r - m
# 			end
# 		else
# 			puts "equal case #{l} #{r} #{m}"
# 			return r - m
# 		end
# 	end
# 	return 0
# end

#For each r what is the l such that sum[l..r] <= t and l..r is maximum
# def get_count(arr,l,r,t)
# 	# return r - l if arr[r] - arr[l] <= t
# 	return r - l + 1 if arr[r] <= t
# 	(l..(r-1)).each do |i|
# 		# puts "#{r} #{i}"
# 		return r - i if arr[r] - arr[i] <= t
# 	end
# 	return 0
# end

# def get_count(arr,l,r,t)
# 	# return r - l if arr[r] - arr[l] <= t
# 	return r - l + 1 if arr[r] <= t

# 	while l >=0 && l < r
# 		if arr[r] - arr[l] > t
# 			m = l + ((r-l)/2.0).ceil
# 			l = m
# 			# puts "#{l} #{m} #{r} #{l + (r-l)/2}"
# 		elsif arr[r] - arr[l] < t
# 			if l !=0 && arr[r] - arr[l-1] < t
# 				l = l - 1
# 			else
# 				return r - l
# 			end
# 		else
# 			return r - l
# 		end
# 	end

# 	return 0
# end

# def get_count(arr,l,r,t)
# 	# return r - l if arr[r] - arr[l] <= t
# 	# puts "#{l} #{r}"
# 	return r - l + 1 if arr[r] && arr[r] <= t

# 	while arr[r] && l < r
# 		m = l + (r-l) / 2
# 		if arr[l] + t > arr[r]
# 			r = m
# 		else
# 			if arr[]
# 			 	r =  r + 1
# 		 	else
# 		 		return r - l
# 		 	end

# 		end
# 	end

# 	return 0
# end

def upper_bound(arr,l,r,x)
	best = nil
	while l <= r
		m = l + (r-l)/2
		if arr[m] <= x
			best = m
			l = m + 1
		else
			r = m - 1
		end
	end
	return best

end

n,t = gets.split(" ").map(&:to_i)
arr = gets.split(" ").map(&:to_i)
sum_arr = [arr[0]]
(1..(n-1)).each do |i|
	sum_arr[i] = sum_arr[i-1] + arr[i]
end
# puts "#{sum_arr}"
max = 0
sum_arr.each_with_index{|a,i|
	# val = get_count(sum_arr,i,n-1,t)
	up = upper_bound(sum_arr,i,n-1,sum_arr[i]+t)
	val = up - i if up
	val = 1 if up == i
	max = val if val && val > max
}

# 1 2 3 4 5
# 1 3 6 10 15
puts max
#

# def get_count(arr,l,r,t)

# 	while l >= 0 && l <= r
# 		m = l + (r-l) / 2
# 		puts "#{l} #{r}"
# 		return 1 if r ==m && arr[m] <= t
# 		if arr[r] - arr[m] > t
# 			l = m + 1
# 		elsif arr[r] - arr[m] < t
# 			if arr[r] - arr[m-1] <= t
# 				l = m - 1
# 			else
# 				puts "less case #{l} #{r} #{m}"
# 				return r - m
# 			end
# 		else
# 			puts "equal case #{l} #{r} #{m}"
# 			return r - m
# 		end
# 	end
# 	return 0
# end

#For each r what is the l such that sum[l..r] <= t and l..r is maximum
# def get_count(arr,l,r,t)
# 	# return r - l if arr[r] - arr[l] <= t
# 	return r - l + 1 if arr[r] <= t
# 	(l..(r-1)).each do |i|
# 		# puts "#{r} #{i}"
# 		return r - i if arr[r] - arr[i] <= t
# 	end
# 	return 0
# end

# def get_count(arr,l,r,t)
# 	# return r - l if arr[r] - arr[l] <= t
# 	return r - l + 1 if arr[r] <= t

# 	while l >=0 && l < r
# 		if arr[r] - arr[l] > t
# 			m = l + ((r-l)/2.0).ceil
# 			l = m
# 			# puts "#{l} #{m} #{r} #{l + (r-l)/2}"
# 		elsif arr[r] - arr[l] < t
# 			if l !=0 && arr[r] - arr[l-1] < t
# 				l = l - 1
# 			else
# 				return r - l
# 			end
# 		else
# 			return r - l
# 		end
# 	end

# 	return 0
# end

# def get_count(arr,l,r,t)
# 	# return r - l if arr[r] - arr[l] <= t
# 	# puts "#{l} #{r}"
# 	return r - l + 1 if arr[r] && arr[r] <= t

# 	while arr[r] && l < r
# 		m = l + (r-l) / 2
# 		if arr[l] + t > arr[r]
# 			r = m
# 		else
# 			if arr[]
# 			 	r =  r + 1
# 		 	else
# 		 		return r - l
# 		 	end

# 		end
# 	end

# 	return 0
# end

def upper_bound(arr,l,r,x)
	best = nil
	while l <= r
		m = l + (r-l)/2
		if arr[m] <= x
			best = m
			l = m + 1
		else
			r = m - 1
		end
	end
	return best

end

n,t = gets.split(" ").map(&:to_i)
arr = gets.split(" ").map(&:to_i)
sum_arr = [arr[0]]
(1..(n-1)).each do |i|
	sum_arr[i] = sum_arr[i-1] + arr[i]
end
# puts "#{sum_arr}"
max = 0
sum_arr.each_with_index{|a,i|
	# val = get_count(sum_arr,i,n-1,t)
	up = upper_bound(sum_arr,i,n-1,sum_arr[i]+t)
 	if up
		val = up - i
		val = 1 if up == i && arr[i] <= t
	end
	# puts "#{up}"
	max = val if val && val > max
}

# 1 2 3 4 5
# 1 3 6 10 15
puts max
#

# http://codeforces.com/problemset/problem/279/B
n, t = gets.chomp.split.map(&:to_i)
list = gets.chomp.split.map(&:to_i)

max = 0

time = 0
for i in 0..(n - 1)
  for j in i..(n - 1)
    time += list[j]

    if time > t
      break
    else
      potential = j - i + 1

      if potential > max
        max = potential
      end
    end
  end
end

p max

[3,1,2,1]

def read_next_line
  gets.split.map { |x| x.to_i }
end

n, t = read_next_line
a = read_next_line

max = p = cur_time = 0
for i in 0...n
  cur_time += a[i]
  while cur_time > t
    cur_time -= a[p]
    p += 1
  end
  cur = i - p
  max = cur if cur > max
end
puts max + 1

a, b = gets.split(" ")

time = b.to_i

eachBookTime = []

gets.split(" ").each do |item|

    eachBookTime.push item.to_i

end

sum = 0

count = 0

eachBookTime.each do |item|

    if (sum+item <= time)

        sum += item

        count+=1

    end

end

puts count

a, b = gets.split(" ")

time = b.to_i

eachBookTime = []

gets.split(" ").each do |item|

    eachBookTime.push item.to_i

end

eachBookTime = eachBookTime.sort

sum = 0

count = 0

eachBookTime.each do |item|

    if (sum+item < time)

        sum += item

        count+=1

    end

end

puts count

a, b = gets.split(" ")

time = b.to_i

eachBookTime = []

gets.split(" ").each do |item|

    eachBookTime.push item.to_i

end

eachBookTime = eachBookTime.sort

sum = 0

count = 0

eachBookTime.each do |item|

    if (sum+item <= time)

        sum += item

        count+=1

    end

end

puts count


Let's imagine: there is a chess piece billiard ball. Its movements resemble the ones of a bishop chess piece. The only difference is that when a billiard ball hits the board's border, it can reflect from it and continue moving.More formally, first one of four diagonal directions is chosen and the billiard ball moves in that direction. When it reaches the square located on the board's edge, the billiard ball reflects from it; it changes the direction of its movement by 90 degrees and continues moving. Specifically, having reached a corner square, the billiard ball is reflected twice and starts to move the opposite way. While it moves, the billiard ball can make an infinite number of reflections. At any square of its trajectory the billiard ball can stop and on that the move is considered completed.  It is considered that one billiard ball a beats another billiard ball b if a can reach a point where b is located.You are suggested to find the maximal number of billiard balls, that pairwise do not beat each other and that can be positioned on a chessboard n × m in size.

The first line contains two integers n and m (2 ≤ n, m ≤ 106).

Print a single number, the maximum possible number of billiard balls that do not pairwise beat each other. Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is preferred to use cin (also you may use the %I64d specificator).

n,m=gets.chomp.split.collect{|a| a.to_i}
puts ((n-1).gcd(m-1)+1)

require 'mathn'
m, n = gets.chomp.split.map{|s| s.to_i - 1}.sort
puts m.gcd(n) + 1

m, n = gets.chomp.split.map{|s| s.to_i}
puts m == n ? m : 2

m, n = gets.chomp.split.map{|s| s.to_i - 1}.sort
puts n % m == 0 ? m + 1 : 2


There was an epidemic in Monstropolis and all monsters became sick. To recover, all monsters lined up in queue for an appointment to the only doctor in the city.Soon, monsters became hungry and began to eat each other. One monster can eat other monster if its weight is strictly greater than the weight of the monster being eaten, and they stand in the queue next to each other. Monsters eat each other instantly. There are no monsters which are being eaten at the same moment. After the monster A eats the monster B, the weight of the monster A increases by the weight of the eaten monster B. In result of such eating the length of the queue decreases by one, all monsters after the eaten one step forward so that there is no empty places in the queue again. A monster can eat several monsters one after another. Initially there were n monsters in the queue, the i-th of which had weight ai.For example, if weights are [1, 2, 2, 2, 1, 2] (in order of queue, monsters are numbered from 1 to 6 from left to right) then some of the options are:  the first monster can't eat the second monster because a1 = 1 is not greater than a2 = 2;  the second monster can't eat the third monster because a2 = 2 is not greater than a3 = 2;  the second monster can't eat the fifth monster because they are not neighbors;  the second monster can eat the first monster, the queue will be transformed to [3, 2, 2, 1, 2]. After some time, someone said a good joke and all monsters recovered. At that moment there were k (k ≤ n) monsters in the queue, the j-th of which had weight bj. Both sequences (a and b) contain the weights of the monsters in the order from the first to the last.You are required to provide one of the possible orders of eating monsters which led to the current queue, or to determine that this could not happen. Assume that the doctor didn't make any appointments while monsters were eating each other.

The first line contains single integer n (1 ≤ n ≤ 500) — the number of monsters in the initial queue. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the initial weights of the monsters. The third line contains single integer k (1 ≤ k ≤ n) — the number of monsters in the queue after the joke.  The fourth line contains k integers b1, b2, ..., bk (1 ≤ bj ≤ 5·108) — the weights of the monsters after the joke.  Monsters are listed in the order from the beginning of the queue to the end.

In case if no actions could lead to the final queue, print "NO" (without quotes) in the only line.  Otherwise print "YES" (without quotes) in the first line. In the next n - k lines print actions in the chronological order. In each line print x — the index number of the monster in the current queue which eats and, separated by space, the symbol 'L' if the monster which stays the x-th in the queue eats the monster in front of him, or 'R' if the monster which stays the x-th in the queue eats the monster behind him. After each eating the queue is enumerated again.  When one monster eats another the queue decreases. If there are several answers, print any of them.

NoteIn the first example, initially there were n = 6 monsters, their weights are [1, 2, 2, 2, 1, 2] (in order of queue from the first monster to the last monster). The final queue should be [5, 5]. The following sequence of eatings leads to the final queue:  the second monster eats the monster to the left (i.e. the first monster), queue becomes [3, 2, 2, 1, 2];  the first monster (note, it was the second on the previous step) eats the monster to the right (i.e. the second monster), queue becomes [5, 2, 1, 2];  the fourth monster eats the mosnter to the left (i.e. the third monster), queue becomes [5, 2, 3];  the finally, the third monster eats the monster to the left (i.e. the second monster), queue becomes [5, 5]. Note that for each step the output contains numbers of the monsters in their current order in the queue.

def can_eat_left? arr, i
  return false unless  i > 0
  return arr[i].to_i > arr[i-1].to_i
end

def can_eat_right? arr, i
  return false unless arr.size > i + 1
  return arr[i].to_i > arr[i+1].to_i
end

def max_eat_other now_eating, now_index			# sure max element eat other
	while  now_eating.size > 1
		if can_eat_left? now_eating, now_index
			eat_left now_eating, now_index
			now_index = now_index -1
		else can_eat_right? now_eating, now_index
			eat_right  now_eating, now_index
		end
	end
end

def find_max eating_array		# return 1 max elemnt and suggest it to eat left or right,return -999 if can not eating
	index_all_max = Array.new				#m contain all max element of array
	multi_all_max = Array.new
	my_max = eating_array.max
	eating_array.each_with_index do |x, index|
		if x == my_max
			index_all_max.push(index)
		end
	end
	if index_all_max.size == 1
		if can_eat_left? eating_array, index_all_max[0]
			index_all_max.push(-1)			# suggest eat left
		elsif  can_eat_right? eating_array, index_all_max[0]
			index_all_max.push(1)			# suggest eat right
		else
			index_all_max.push(0)			# just enought not eat any more
		end
		index_all_max
	else
		index_all_max.each do |x|
			if can_eat_left? eating_array, x
				multi_all_max.push(x)
				multi_all_max.push(-1)
				return multi_all_max
			end
			if can_eat_right? eating_array, x
				#puts "checking with" + eating_array.to_s + "x:" + x.to_s
				multi_all_max.push(x)
				multi_all_max.push(1)
				return multi_all_max
			end
		end
		multi_all_max.push(-999)		# in case all monster can not eat other
		return multi_all_max
	end

end

def elements_number elements, total
	num = 0
	elements.each do |x|
		total = total - x
		num += 1
		if total == 0
			return num
		elsif  total < 0
			return -999
		end
	end
	return -999		# incase can no make total equal head number
end

def eat_from_head eating_array, num
	#puts eating_array.to_s
	#puts num
	#puts "xxxxxx-"
	number_of_attends = elements_number eating_array, num
	#puts "number_of_attends" + number_of_attends.to_s
	if number_of_attends < 0
		$can_eat_flag = false
		return -999
	end
	now_eating = eating_array.slice(0, number_of_attends)
	#puts "slice " + now_eating.to_s
	# check before eat
	sub_max = find_max now_eating
	#puts "submax" + sub_max.to_s
	if sub_max[0] < 0 	# -999
		return -999
	else
		if sub_max[1] < 0
			eat_left now_eating, sub_max[0]
			sub_max[0] = sub_max[0] -1
			max_eat_other now_eating, sub_max[0]
		elsif sub_max[1] > 0
			eat_right now_eating, sub_max[0]
			#puts "now eating" + now_eating.to_s
			max_eat_other now_eating, sub_max[0]
		else
			#do nothing
		end
	end
	# eating_array.drop(number_of_attends)
	$completed += 1
	return number_of_attends
end

def monster_eat original_monster, eaten_monsters
	#puts "x"
	if original_monster.size == 0 && eaten_monsters.size == 0
		return true
	elsif original_monster.size == 0 || eaten_monsters.size == 0
		return false
	end

	number_of_attends = eat_from_head(original_monster, eaten_monsters[0])
	if number_of_attends > 0
		(0..number_of_attends-1).each do |x|
			original_monster.delete_at(0)
		end
		eaten_monsters.delete_at(0)
		#puts original_monster.to_s
		#puts eaten_monsters.to_s
		return monster_eat original_monster, eaten_monsters
	else
		return false
	end
	return true
end

#===============#
def eat_left arr, i
  $eat_steps << (i + 1 + $completed).to_s + " L"
  arr[i-1] += arr[i]
  arr.delete_at(i)
end

def eat_right arr, i
  $eat_steps << (i + 1 + $completed).to_s + " R"
  arr[i] += arr[i+1]
  arr.delete_at(i+1)
end
#===============#

#========= MAIN ===========#
$completed = 0
$can_eat_flag = false
$eat_steps = Array.new

monster_number = gets.to_i
original_monster = Array.new
a = gets
monsters = a.split(" ")
monsters.each do |x|
  original_monster << x.to_i
end

eaten_number = gets.to_i
b = gets
e_monsters = b.split(" ")
eaten_monsters = Array.new
e_monsters.each do |x|
  eaten_monsters << x.to_i
end

$can_eat_flag = monster_eat(original_monster, eaten_monsters)
if $can_eat_flag
  puts "YES"
  $eat_steps.each do |x|
    puts x
  end
else
  puts "NO"
end

#========= MAIN ===========#

def can_eat_left? arr, i
  return false unless  i > 0
  return arr[i].to_i > arr[i-1].to_i
end
def can_eat_right? arr, i
  return false unless arr.size > i + 1
  return arr[i].to_i > arr[i+1].to_i
end

def eat_left arr, i
  $eat_steps << (i+1).to_s + " L"
  arr[i-1] += arr[i]
  arr.delete_at(i)
end

def eat_right arr, i
  $eat_steps << (i+1).to_s + " R"
  arr[i] += arr[i+1]
  arr.delete_at(i+1)
end

def become_eat original_monster, eaten_monsters
  # puts "ori:" + original_monster.to_s
  # puts "eaten:" + eaten_monsters.to_s
  eaten_monsters.each_with_index do |eaten, index|
    if eat_head(eaten, original_monster) == -1
      $flag = false                # this case only puts "NO"
      break
    end

    if eat_head(eaten, eaten_monsters) == 0
      original_monster.delete_at(0)
      eaten_monsters.delete_at(0)
      become_eat original_monster, eaten_monsters
    end
  end

  $flag = true if original_monster.size == 0 && eaten_monsters.size == 0
end

def eat_head value, original_monster
  # puts value
  # puts original_monster.to_s
  return -1 if value.to_i < original_monster[0].to_i    # this will puts "NO"
  return 0 if value.to_i == original_monster[0].to_i   # this will delete 2 element in 2 array, and continue to eat

  if can_eat_right?(original_monster, 0)
    eat_right(original_monster,0)
    return eat_head(value,original_monster)       # try to eat as possible
  end

  (0..original_monster.size - 1).each do |x|
    if can_eat_right?(original_monster, x)
      eat_right(original_monster, x)
      return eat_head(value,original_monster)
    end
    if can_eat_left?(original_monster, x)
      eat_left(original_monster, x)
      return eat_head(value,original_monster)
    end
  end

end

# input processor
monster_number = gets.to_i
original_monster = Array.new
a = gets
monsters = a.split(" ")
monsters.each do |x|
  original_monster << x.to_i
end

eaten_number = gets.to_i
b = gets
e_monsters = b.split(" ")
eaten_monsters = Array.new
e_monsters.each do |x|
  eaten_monsters << x.to_i
end

# Main
$eat_steps = Array.new
$flag = false
become_eat(original_monster, eaten_monsters)

if $flag
  puts "YES"
  $eat_steps.each do |x|
    puts x
  end
else
  puts "NO"
end

# puts "-----------------------------"
# puts monster_number
# puts original_monster.to_s
# puts eaten_number
# puts eaten_monsters.to_s

def can_eat_left? arr, i
  return false unless  i > 0
  return arr[i].to_i > arr[i-1].to_i
end
def can_eat_right? arr, i
  return false unless arr.size > i + 1
  return arr[i].to_i > arr[i+1].to_i
end

def eat_left arr, i
  $eat_steps << (i + 1 + $deleted_index).to_s + " L"
  arr[i-1] += arr[i]
  arr.delete_at(i)
end

def eat_right arr, i
  $eat_steps << (i + 1 + $deleted_index).to_s + " R"
  arr[i] += arr[i+1]
  arr.delete_at(i+1)
end

def become_eat original_monster, eaten_monsters
  # puts "ori:" + original_monster.to_s
  # puts "eaten:" + eaten_monsters.to_s
  eaten_monsters.each_with_index do |eaten, index|
    sw = eat_head(eaten, original_monster)
    if sw == -1
      $flag = false                # this case only puts "NO"
      break
    end

    if sw == 0
      original_monster.delete_at(0)
      eaten_monsters.delete_at(0)
      $deleted_index += 1
      become_eat original_monster, eaten_monsters
    end
  end

  $flag = true if original_monster.size == 0 && eaten_monsters.size == 0
end

def eat_head value, original_monster
  # puts value
  # puts original_monster.to_s
  return -1 if value.to_i < original_monster[0].to_i    # this will puts "NO"
  return 0 if value.to_i == original_monster[0].to_i   # this will delete 2 element in 2 array, and continue to eat

  # if can_eat_right?(original_monster, 0)
  #   eat_right(original_monster,0)
  #   return eat_head(value,original_monster)       # try to eat as possible
  # end

  (0..original_monster.size - 1).each do |x|
    if can_eat_right?(original_monster, x)
      eat_right(original_monster, x)
      return eat_head(value,original_monster)
    end
    if can_eat_left?(original_monster, x)
      eat_left(original_monster, x)
      return eat_head(value,original_monster)
    end
  end

end

# input processor
monster_number = gets.to_i
original_monster = Array.new
a = gets
monsters = a.split(" ")
monsters.each do |x|
  original_monster << x.to_i
end

eaten_number = gets.to_i
b = gets
e_monsters = b.split(" ")
eaten_monsters = Array.new
e_monsters.each do |x|
  eaten_monsters << x.to_i
end

# Main
$eat_steps = Array.new
$flag = false
$deleted_index = 0
become_eat(original_monster, eaten_monsters)

if $flag
  puts "YES"
  $eat_steps.each do |x|
    puts x
  end
else
  puts "NO"
end

# puts "-----------------------------"
# puts monster_number
# puts original_monster.to_s
# puts eaten_number
# puts eaten_monsters.to_s

def can_eat_left? arr, i
  return false unless  i > 0
  return arr[i].to_i > arr[i-1].to_i
end
def can_eat_right? arr, i
  return false unless arr.size > i + 1
  return arr[i].to_i > arr[i+1].to_i
end

def eat_left arr, i
  $eat_steps << (i+1).to_s + " L"
  arr[i-1] += arr[i]
  arr.delete_at(i)
end

def eat_right arr, i
  $eat_steps << (i+1).to_s + " R"
  arr[i] += arr[i+1]
  arr.delete_at(i+1)
end

def become_eat original_monster, eaten_monsters
  # puts "ori:" + original_monster.to_s
  # puts "eaten:" + eaten_monsters.to_s
  eaten_monsters.each_with_index do |eaten, index|
    if eat_head(eaten, original_monster) == -1
      $flag = false                # this case only puts "NO"
      break
    end

    if eat_head(eaten, eaten_monsters) == 0
      original_monster.delete_at(0)
      eaten_monsters.delete_at(0)
      become_eat original_monster, eaten_monsters
    end
  end

  $flag = true if original_monster.size == 0 && eaten_monsters.size == 0
end

def eat_head value, original_monster
  # puts value
  # puts original_monster.to_s
  return -1 if value.to_i < original_monster[0].to_i    # this will puts "NO"
  return 0 if value.to_i == original_monster[0].to_i   # this will delete 2 element in 2 array, and continue to eat

  # if can_eat_right?(original_monster, 0)
  #   eat_right(original_monster,0)
  #   return eat_head(value,original_monster)       # try to eat as possible
  # end

  (0..original_monster.size - 1).each do |x|
    if can_eat_right?(original_monster, x)
      eat_right(original_monster, x)
      return eat_head(value,original_monster)
    end
    if can_eat_left?(original_monster, x)
      eat_left(original_monster, x)
      return eat_head(value,original_monster)
    end
  end

end

# input processor
monster_number = gets.to_i
original_monster = Array.new
a = gets
monsters = a.split(" ")
monsters.each do |x|
  original_monster << x.to_i
end

eaten_number = gets.to_i
b = gets
e_monsters = b.split(" ")
eaten_monsters = Array.new
e_monsters.each do |x|
  eaten_monsters << x.to_i
end

# Main
$eat_steps = Array.new
$flag = false
become_eat(original_monster, eaten_monsters)

if $flag
  puts "YES"
  $eat_steps.each do |x|
    puts x
  end
else
  puts "NO"
end

# puts "-----------------------------"
# puts monster_number
# puts original_monster.to_s
# puts eaten_number
# puts eaten_monsters.to_s

def can_eat_left? arr, i
  return false unless  i > 0
  return arr[i].to_i > arr[i-1].to_i
end
def can_eat_right? arr, i
  return false unless arr.size > i + 1
  return arr[i].to_i > arr[i+1].to_i
end

def eat_left arr, i
  $eat_steps << (i + 1 + $deleted_index).to_s + " L"
  arr[i-1] += arr[i]
  arr.delete_at(i)
end

def eat_right arr, i
  $eat_steps << (i + 1 + $deleted_index).to_s + " R"
  arr[i] += arr[i+1]
  arr.delete_at(i+1)
end

def become_eat original_monster, eaten_monsters
  # puts "ori:" + original_monster.to_s
  # puts "eaten:" + eaten_monsters.to_s
  eaten_monsters.each_with_index do |eaten, index|
    sw = eat_head(eaten, original_monster)
    if sw == -1
      $flag = false                # this case only puts "NO"
      break
    end

    if sw == 0
      original_monster.delete_at(0)
      eaten_monsters.delete_at(0)
      $deleted_index += 1
      become_eat original_monster, eaten_monsters
    end
  end

  $flag = true if original_monster.size == 0 && eaten_monsters.size == 0
end

def eat_head value, original_monster
  # puts value
  # puts original_monster.to_s
  return -1 if value.to_i < original_monster[0].to_i    # this will puts "NO"
  return 0 if value.to_i == original_monster[0].to_i   # this will delete 2 element in 2 array, and continue to eat

  if can_eat_right?(original_monster, 0)
    eat_right(original_monster,0)
    return eat_head(value,original_monster)       # try to eat as possible
  end

  (1..original_monster.size - 1).each do |x|
    if can_eat_left?(original_monster, x)   # always try to eat left first
      eat_left(original_monster, x)
      return eat_head(value,original_monster)
    end

    if can_eat_right?(original_monster, x) # tr to eat right after
      eat_right(original_monster, x)
      return eat_head(value,original_monster)
    end
  end

end

# input processor
monster_number = gets.to_i
original_monster = Array.new
a = gets
monsters = a.split(" ")
monsters.each do |x|
  original_monster << x.to_i
end

eaten_number = gets.to_i
b = gets
e_monsters = b.split(" ")
eaten_monsters = Array.new
e_monsters.each do |x|
  eaten_monsters << x.to_i
end

# Main
$eat_steps = Array.new
$flag = false
$deleted_index = 0
become_eat(original_monster, eaten_monsters)

if $flag
  puts "YES"
  $eat_steps.each do |x|
    puts x
  end
else
  puts "NO"
end

# puts "-----------------------------"
# puts monster_number
# puts original_monster.to_s
# puts eaten_number
# puts eaten_monsters.to_s

def can_eat_left? arr, i
  return false unless  i > 0
  return arr[i].to_i > arr[i-1].to_i
end
def can_eat_right? arr, i
  return false unless arr.size > i + 1
  return arr[i].to_i > arr[i+1].to_i
end

def eat_left arr, i
  $eat_steps << (i+1).to_s + " L"
  arr[i-1] += arr[i]
  arr.delete_at(i)
end

def eat_right arr, i
  $eat_steps << (i+1).to_s + " R"
  arr[i] += arr[i+1]
  arr.delete_at(i+1)
end

def become_eat original_monster, eaten_monsters
  puts "ori:" + original_monster.to_s
  puts "eaten:" + eaten_monsters.to_s
  eaten_monsters.each_with_index do |eaten, index|
    if eat_head(eaten, original_monster) == -1
      $flag = false                # this case only puts "NO"
      break
    end

    if eat_head(eaten, eaten_monsters) == 0
      original_monster.delete_at(0)
      eaten_monsters.delete_at(0)
      become_eat original_monster, eaten_monsters
    end
  end

  $flag = true if original_monster.size == 0 && eaten_monsters.size == 0
end

def eat_head value, original_monster
  # puts value
  # puts original_monster.to_s
  return -1 if value.to_i < original_monster[0].to_i    # this will puts "NO"
  return 0 if value.to_i == original_monster[0].to_i   # this will delete 2 element in 2 array, and continue to eat

  if can_eat_right?(original_monster, 0)
    eat_right(original_monster,0)
    return eat_head(value,original_monster)       # try to eat as possible
  end

  (0..original_monster.size - 1).each do |x|
    if can_eat_right?(original_monster, x)
      eat_right(original_monster, x)
      return eat_head(value,original_monster)
    end
    if can_eat_left?(original_monster, x)
      eat_left(original_monster, x)
      return eat_head(value,original_monster)
    end
  end

end

# input processor
monster_number = gets.to_i
original_monster = Array.new
a = gets
monsters = a.split(" ")
monsters.each do |x|
  original_monster << x.to_i
end

eaten_number = gets.to_i
b = gets
e_monsters = b.split(" ")
eaten_monsters = Array.new
e_monsters.each do |x|
  eaten_monsters << x.to_i
end

# Main
$eat_steps = Array.new
$flag = false
become_eat(original_monster, eaten_monsters)

if $flag
  puts "YES"
  $eat_steps.each do |x|
    puts x
  end
else
  puts "NO"
end

# puts "-----------------------------"
# puts monster_number
# puts original_monster.to_s
# puts eaten_number
# puts eaten_monsters.to_s

def can_eat_left? arr, i
  return false unless  i > 0
  return arr[i].to_i > arr[i-1].to_i
end
def can_eat_right? arr, i
  return false unless arr.size > i + 1
  return arr[i].to_i > arr[i+1].to_i
end

def eat_left arr, i
  $eat_steps << (i + 1 + $deleted_index).to_s + " L"
  arr[i-1] += arr[i]
  arr.delete_at(i)
end

def eat_right arr, i
  $eat_steps << (i + 1 + $deleted_index).to_s + " R"
  arr[i] += arr[i+1]
  arr.delete_at(i+1)
end

def become_eat original_monster, eaten_monsters
  # puts "ori:" + original_monster.to_s
  # puts "eaten:" + eaten_monsters.to_s
  eaten_monsters.each_with_index do |eaten, index|
    sw = eat_head(eaten, original_monster)
    if sw == -1
      $flag = false                # this case only puts "NO"
      break
    end

    if sw == 0
      original_monster.delete_at(0)
      eaten_monsters.delete_at(0)
      $deleted_index += 1
      return become_eat(original_monster, eaten_monsters)
    end
  end

  $flag = true if original_monster.size == 0 && eaten_monsters.size == 0
end

def eat_head value, original_monster
  # puts value
  # puts original_monster.to_s
  return -1 if value.to_i < original_monster[0].to_i    # this will puts "NO"
  return 0 if value.to_i == original_monster[0].to_i   # this will delete 2 element in 2 array, and continue to eat

  if can_eat_right?(original_monster, 0)
    eat_right(original_monster,0)
    return eat_head(value,original_monster)       # try to eat as possible
  end

  (1..original_monster.size - 1).each do |x|
    if can_eat_left?(original_monster, x)   # always try to eat left first
      eat_left(original_monster, x)
      return eat_head(value,original_monster)
    end

    if can_eat_right?(original_monster, x) # tr to eat right after
      eat_right(original_monster, x)
      return eat_head(value,original_monster)
    end
  end

  return -1    # all monsters canot eat_left && canot eat_right

end

# input processor
monster_number = gets.to_i
original_monster = Array.new
a = gets
monsters = a.split(" ")
monsters.each do |x|
  original_monster << x.to_i
end

eaten_number = gets.to_i
b = gets
e_monsters = b.split(" ")
eaten_monsters = Array.new
e_monsters.each do |x|
  eaten_monsters << x.to_i
end

# Main
$eat_steps = Array.new
$flag = false
$deleted_index = 0
become_eat(original_monster, eaten_monsters)

if $flag
  puts "YES"
  $eat_steps.each do |x|
    puts x
  end
else
  puts "NO"
end

# puts "-----------------------------"
# puts monster_number
# puts original_monster.to_s
# puts eaten_number
# puts eaten_monsters.to_s

def can_eat_left? arr, i
  return false unless  i > 0
  return arr[i] > arr[i-1]
end
def can_eat_right? arr, i
  return false unless arr.size > i + 1
  return arr[i] > arr[i+1]
end

def eat_left arr, i
  puts i.to_s + " L"
  arr[i-1] += arr[i]
  arr.delete_at(i)
end

def eat_right arr, i
  puts i.to_s + " R"
  arr[i] += arr[i+1]
  arr.delete_at(i+1)
end

def become_eat original_monster, eaten_monsters
  eaten_monsters.each_with_index do |eaten, index|
    if eat_head(eaten, eaten_monsters) == -1
      puts "NO"
      return
    end

    if eat_head(eaten, eaten_monsters) == 0
      original_monster.delete_at(0)
      eaten_monsters.delete_at(0)
      become_eat original_monster, eaten_monsters
    end
  end
end

def eat_head value, eaten_monsters
  return -1 if value.to_i < eaten_monsters[0].to_i    # this will puts "NO"
  return 0 if value.to_i == eaten_monsters[0].to_i   # this will delete 2 element in 2 array, and continue to eat

  if can_eat_right(eaten_monsters, 0)
    eat_right(eaten_monsters,0)
    eat_head(value,eaten_monsters)       # try to eat as possible
  end

  (0..eaten_monsters.size - 1).each do |x|
    if can_eat_right(eaten_monsters, x)
      eat_right(eaten_monsters, x)
      eat_head(value,eaten_monsters)
    end
    if can_eat_left(eaten_monsters, x)
      eat_left(eaten_monsters, x)
      eat_head(value,eaten_monsters)
    end
  end

end

# input processor
monster_number = gets.to_i
original_monster = Array.new
a = gets
monsters = a.split(" ")
monsters.each do |x|
  original_monster << x.to_i
end

eaten_number = gets.to_i
b = gets
e_monsters = b.split(" ")
eaten_monsters = Array.new
e_monsters.each do |x|
  eaten_monsters << x.to_i
end
puts "-----------------------------"
puts monster_number
puts original_monster.to_s
puts eaten_number
puts eaten_monsters.to_s

def can_eat_left? arr, i
  return false unless  i > 0
  return arr[i].to_i > arr[i-1].to_i
end

def can_eat_right? arr, i
  return false unless arr.size > i + 1
  return arr[i].to_i > arr[i+1].to_i
end

def max_eat_other now_eating, now_index			# sure max element eat other
	while  now_eating.size > 1
		if can_eat_left? now_eating, now_index
			eat_left now_eating, now_index
			now_index = now_index -1
		else can_eat_right? now_eating, now_index
			eat_right  now_eating, now_index
		end
	end
end

def find_max eating_array		# return 1 max elemnt and suggest it to eat left or right,return -999 if can not eating
	index_all_max = Array.new				#m contain all max element of array
	multi_all_max = Array.new
	my_max = eating_array.max
	eating_array.each_with_index do |x, index|
		if x == my_max
			index_all_max.push(index)
		end
	end
	if index_all_max.size == 1
		if can_eat_left? eating_array, index_all_max[0]
			index_all_max.push(-1)			# suggest eat left
		elsif  can_eat_right? eating_array, index_all_max[0]
			index_all_max.push(1)			# suggest eat right
		else
			index_all_max.push(0)			# just enought not eat any more
		end
		index_all_max
	else
		index_all_max.each do |x|
			if can_eat_left? eating_array, x
				multi_all_max.push(x)
				multi_all_max.push(-1)
				return multi_all_max
			end
			if can_eat_right? eating_array, x
				puts "checking with" + eating_array.to_s + "x:" + x.to_s
				multi_all_max.push(x)
				multi_all_max.push(1)
				return multi_all_max
			end
		end
		multi_all_max.push(-999)		# in case all monster can not eat other
		return multi_all_max
	end

end

def elements_number elements, total
	num = 0
	elements.each do |x|
		total = total - x
		num += 1
		if total == 0
			return num
		elsif  total < 0
			return -999
		end
	end
	return -999		# incase can no make total equal head number
end

def eat_from_head eating_array, num
	#puts eating_array.to_s
	#puts num
	#puts "xxxxxx-"
	number_of_attends = elements_number eating_array, num
	#puts "number_of_attends" + number_of_attends.to_s
	if number_of_attends < 0
		$can_eat_flag = false
		return -999
	end
	now_eating = eating_array.slice(0, number_of_attends)
	#puts "slice " + now_eating.to_s
	# check before eat
	sub_max = find_max now_eating
	#puts "submax" + sub_max.to_s
	if sub_max[0] < 0 	# -999
		return -999
	else
		if sub_max[1] < 0
			eat_left now_eating, sub_max[0]
			sub_max[0] = sub_max[0] -1
			max_eat_other now_eating, sub_max[0]
		elsif sub_max[1] > 0
			eat_right now_eating, sub_max[0]
			#puts "now eating" + now_eating.to_s
			max_eat_other now_eating, sub_max[0]
		else
			#do nothing
		end
	end
	# eating_array.drop(number_of_attends)
	$completed += 1
	return number_of_attends
end

def monster_eat original_monster, eaten_monsters
	#puts "x"
	if original_monster.size == 0 && eaten_monsters.size == 0
		return true
	elsif original_monster.size == 0 || eaten_monsters.size == 0
		return false
	end

	number_of_attends = eat_from_head(original_monster, eaten_monsters[0])
	if number_of_attends > 0
		(0..number_of_attends-1).each do |x|
			original_monster.delete_at(0)
		end
		eaten_monsters.delete_at(0)
		#puts original_monster.to_s
		#puts eaten_monsters.to_s
		return monster_eat original_monster, eaten_monsters
	else
		return false
	end
	return true
end

#===============#
def eat_left arr, i
  $eat_steps << (i + 1 + $completed).to_s + " L"
  arr[i-1] += arr[i]
  arr.delete_at(i)
end

def eat_right arr, i
  $eat_steps << (i + 1 + $completed).to_s + " R"
  arr[i] += arr[i+1]
  arr.delete_at(i+1)
end
#===============#

#========= MAIN ===========#
$completed = 0
$can_eat_flag = false
$eat_steps = Array.new

monster_number = gets.to_i
original_monster = Array.new
a = gets
monsters = a.split(" ")
monsters.each do |x|
  original_monster << x.to_i
end

eaten_number = gets.to_i
b = gets
e_monsters = b.split(" ")
eaten_monsters = Array.new
e_monsters.each do |x|
  eaten_monsters << x.to_i
end

$can_eat_flag = monster_eat(original_monster, eaten_monsters)
if $can_eat_flag
  puts "YES"
  $eat_steps.each do |x|
    puts x
  end
else
  puts "NO"
end

#========= MAIN ===========#

def can_eat_left? arr, i
  return false unless  i > 0
  return arr[i].to_i > arr[i-1].to_i
end
def can_eat_right? arr, i
  return false unless arr.size > i + 1
  return arr[i].to_i > arr[i+1].to_i
end

def eat_left arr, i
  $eat_steps << (i+1).to_s + " L"
  arr[i-1] += arr[i]
  arr.delete_at(i)
end

def eat_right arr, i
  $eat_steps << (i+1).to_s + " R"
  arr[i] += arr[i+1]
  arr.delete_at(i+1)
end

def become_eat original_monster, eaten_monsters
  # puts "ori:" + original_monster.to_s
  # puts "eaten:" + eaten_monsters.to_s
  eaten_monsters.each_with_index do |eaten, index|
    if eat_head(eaten, original_monster) == -1
      $flag = false                # this case only puts "NO"
      break
    end

    if eat_head(eaten, eaten_monsters) == 0
      original_monster.delete_at(0)
      eaten_monsters.delete_at(0)
      become_eat original_monster, eaten_monsters
    end
  end

  $flag = true if original_monster.size == 0 && eaten_monsters.size == 0
end

def eat_head value, original_monster
  # puts value
  # puts original_monster.to_s
  return -1 if value.to_i < original_monster[0].to_i    # this will puts "NO"
  return 0 if value.to_i == original_monster[0].to_i   # this will delete 2 element in 2 array, and continue to eat

  if can_eat_right?(original_monster, 0)
    eat_right(original_monster,0)
    eat_head(value,original_monster)       # try to eat as possible
  end

  (0..original_monster.size - 1).each do |x|
    if can_eat_right?(original_monster, x)
      eat_right(original_monster, x)
      return eat_head(value,original_monster)
    end
    if can_eat_left?(original_monster, x)
      eat_left(original_monster, x)
      return eat_head(value,original_monster)
    end
  end

end

# input processor
monster_number = gets.to_i
original_monster = Array.new
a = gets
monsters = a.split(" ")
monsters.each do |x|
  original_monster << x.to_i
end

eaten_number = gets.to_i
b = gets
e_monsters = b.split(" ")
eaten_monsters = Array.new
e_monsters.each do |x|
  eaten_monsters << x.to_i
end

# Main
$eat_steps = Array.new
$flag = false
become_eat(original_monster, eaten_monsters)

if $flag
  puts "YES"
  $eat_steps.each do |x|
    puts x
  end
else
  puts "NO"
end

# puts "-----------------------------"
# puts monster_number
# puts original_monster.to_s
# puts eaten_number
# puts eaten_monsters.to_s

def can_eat_left? arr, i
  return false unless  i > 0
  return arr[i].to_i > arr[i-1].to_i
end
def can_eat_right? arr, i
  return false unless arr.size > i + 1
  return arr[i].to_i > arr[i+1].to_i
end

def eat_left arr, i
  $eat_steps << (i + 1 + $deleted_index).to_s + " L"
  arr[i-1] += arr[i]
  arr.delete_at(i)
end

def eat_right arr, i
  $eat_steps << (i + 1 + $deleted_index).to_s + " R"
  arr[i] += arr[i+1]
  arr.delete_at(i+1)
end

def become_eat original_monster, eaten_monsters
  # puts "ori:" + original_monster.to_s
  # puts "eaten:" + eaten_monsters.to_s
  eaten_monsters.each_with_index do |eaten, index|
    sw = eat_head(eaten, original_monster)
    if sw == -1
      $flag = false                # this case only puts "NO"
      break
    end

    if sw == 0
      original_monster.delete_at(0)
      eaten_monsters.delete_at(0)
      $deleted_index += 1
      return become_eat(original_monster, eaten_monsters)
    end
  end

  $flag = true if original_monster.size == 0 && eaten_monsters.size == 0
end

def eat_head value, original_monster
  # puts value
  # puts original_monster.to_s
  return -1 if value.to_i < original_monster[0].to_i    # this will puts "NO"
  return 0 if value.to_i == original_monster[0].to_i   # this will delete 2 element in 2 array, and continue to eat

  if can_eat_right?(original_monster, 0)
    eat_right(original_monster,0)
    return eat_head(value,original_monster)       # try to eat as possible
  end

  (1..original_monster.size - 1).each do |x|
    if can_eat_left?(original_monster, x)   # always try to eat left first
      eat_left(original_monster, x)
      return eat_head(value,original_monster)
    end

    if can_eat_right?(original_monster, x) # tr to eat right after
      eat_right(original_monster, x)
      return eat_head(value,original_monster)
    end
  end

end

# input processor
monster_number = gets.to_i
original_monster = Array.new
a = gets
monsters = a.split(" ")
monsters.each do |x|
  original_monster << x.to_i
end

eaten_number = gets.to_i
b = gets
e_monsters = b.split(" ")
eaten_monsters = Array.new
e_monsters.each do |x|
  eaten_monsters << x.to_i
end

# Main
$eat_steps = Array.new
$flag = false
$deleted_index = 0
become_eat(original_monster, eaten_monsters)

if $flag
  puts "YES"
  $eat_steps.each do |x|
    puts x
  end
else
  puts "NO"
end

# puts "-----------------------------"
# puts monster_number
# puts original_monster.to_s
# puts eaten_number
# puts eaten_monsters.to_s

def can_eat_left? arr, i
  return false unless  i > 0
  return arr[i].to_i > arr[i-1].to_i
end
def can_eat_right? arr, i
  return false unless arr.size > i + 1
  return arr[i].to_i > arr[i+1].to_i
end

def eat_left arr, i
  if $reveser_only1
    $eat_steps << (arr.size + 1 - (i + 1 + $deleted_index)).to_s + "R"
  else
    $eat_steps << (i + 1 + $deleted_index).to_s + " L"
  end

  arr[i-1] += arr[i]
  arr.delete_at(i)
end

def eat_right arr, i
  if $reveser_only1
    $eat_steps << (arr.size + 1 - (i + 1 + $deleted_index)).to_s + "L"
  else
    $eat_steps << (i + 1 + $deleted_index).to_s + " R"
  end
  arr[i] += arr[i+1]
  arr.delete_at(i+1)
end

def become_eat original_monster, eaten_monsters
  # puts "ori:" + original_monster.to_s
  # puts "eaten:" + eaten_monsters.to_s
  eaten_monsters.each_with_index do |eaten, index|
    sw = eat_head(eaten, original_monster)
    if sw == -1
      $flag = false                # this case only puts "NO"
      break
    end

    if sw == 0
      original_monster.delete_at(0)
      eaten_monsters.delete_at(0)
      $deleted_index += 1
      return become_eat(original_monster, eaten_monsters)
    end
  end

  # For case need to eat from tail
  if original_monster.size > 0 && eaten_monsters.size > 0 && $reveser_only1
    $reveser_only1 = false
    original_monster.reverse!
    eaten_monsters.reverse!
    return become_eat(original_monster, eaten_monsters)
  end

  $flag = true if original_monster.size == 0 && eaten_monsters.size == 0
end

def eat_head value, original_monster
  # puts value
  # puts original_monster.to_s
  return -1 if value.to_i < original_monster[0].to_i    # this will puts "NO"
  return 0 if value.to_i == original_monster[0].to_i   # this will delete 2 element in 2 array, and continue to eat

  if can_eat_right?(original_monster, 0)
    eat_right(original_monster,0)
    return eat_head(value,original_monster)       # try to eat as possible
  end

  (1..original_monster.size - 1).each do |x|
    if can_eat_left?(original_monster, x)   # always try to eat left first
      eat_left(original_monster, x)
      return eat_head(value,original_monster)
    end

    if can_eat_right?(original_monster, x) # tr to eat right after
      eat_right(original_monster, x)
      return eat_head(value,original_monster)
    end
  end

  return -1    # all monsters canot eat_left && canot eat_right

end

# input processor
monster_number = gets.to_i
original_monster = Array.new
a = gets
monsters = a.split(" ")
monsters.each do |x|
  original_monster << x.to_i
end

eaten_number = gets.to_i
b = gets
e_monsters = b.split(" ")
eaten_monsters = Array.new
e_monsters.each do |x|
  eaten_monsters << x.to_i
end

# Main
$eat_steps = Array.new
$flag = false
$reveser_only1 = true
$deleted_index = 0
become_eat(original_monster, eaten_monsters)

if $flag
  puts "YES"
  $eat_steps.each do |x|
    puts x
  end
else
  puts "NO"
end

# puts "-----------------------------"
# puts monster_number
# puts original_monster.to_s
# puts eaten_number
# puts eaten_monsters.to_s

def can_eat_left? arr, i
  return false unless  i > 0
  return arr[i].to_i > arr[i-1].to_i
end
def can_eat_right? arr, i
  return false unless arr.size > i + 1
  return arr[i].to_i > arr[i+1].to_i
end

def eat_left arr, i
  $eat_steps << (i + 1 + $deleted_index).to_s + " L"
  arr[i-1] += arr[i]
  arr.delete_at(i)
end

def eat_right arr, i
  $eat_steps << (i + 1 + $deleted_index).to_s + " R"
  arr[i] += arr[i+1]
  arr.delete_at(i+1)
end

def become_eat original_monster, eaten_monsters
  # puts "ori:" + original_monster.to_s
  # puts "eaten:" + eaten_monsters.to_s
  eaten_monsters.each_with_index do |eaten, index|
    sw = eat_head(eaten, original_monster)
    if sw == -1
      $flag = false                # this case only puts "NO"
      break
    end

    if sw == 0
      original_monster.delete_at(0)
      eaten_monsters.delete_at(0)
      $deleted_index += 1
      return become_eat(original_monster, eaten_monsters)
    end
  end

  $flag = true if original_monster.size == 0 && eaten_monsters.size == 0
end

def eat_head value, original_monster
  # puts value
  # puts original_monster.to_s
  return -1 if value.to_i < original_monster[0].to_i    # this will puts "NO"
  return 0 if value.to_i == original_monster[0].to_i   # this will delete 2 element in 2 array, and continue to eat

  if can_eat_right?(original_monster, 0)
    eat_right(original_monster,0)
    return eat_head(value,original_monster)       # try to eat as possible
  end

  (1..original_monster.size - 1).each do |x|
    if can_eat_left?(original_monster, x)   # always try to eat left first
      eat_left(original_monster, x)
      return eat_head(value,original_monster)
    end

    if can_eat_right?(original_monster, x) # tr to eat right after
      eat_right(original_monster, x)
      return eat_head(value,original_monster)
    end

    return -1    # canot eat_left && canot eat_right

  end

end

# input processor
monster_number = gets.to_i
original_monster = Array.new
a = gets
monsters = a.split(" ")
monsters.each do |x|
  original_monster << x.to_i
end

eaten_number = gets.to_i
b = gets
e_monsters = b.split(" ")
eaten_monsters = Array.new
e_monsters.each do |x|
  eaten_monsters << x.to_i
end

# Main
$eat_steps = Array.new
$flag = false
$deleted_index = 0
become_eat(original_monster, eaten_monsters)

if $flag
  puts "YES"
  $eat_steps.each do |x|
    puts x
  end
else
  puts "NO"
end

# puts "-----------------------------"
# puts monster_number
# puts original_monster.to_s
# puts eaten_number
# puts eaten_monsters.to_s


n people came to a party. Then those, who had no friends among people at the party, left. Then those, who had exactly 1 friend among those who stayed, left as well. Then those, who had exactly 2, 3, ..., n - 1 friends among those who stayed by the moment of their leaving, did the same.What is the maximum amount of people that could stay at the party in the end?

The first input line contains one number t — amount of tests (1 ≤ t ≤ 105). Each of the following t lines contains one integer number n (1 ≤ n ≤ 105).

For each test output in a separate line one number — the maximum amount of people that could stay in the end.

gets.to_i.times do
  n = gets.to_i
  if n < 3
    puts 0
  else
    puts n - 2
  end
end

t=gets.to_i
t.times{
        n=gets.to_i
        puts [n-2,0].max
}

gets.to_i.times { p [gets.to_i - 2, 0].max }

gets.to_i.times do
  puts gets.to_i / 3
end

gets.to_i.times do
  n = gets.to_i
  total = n / 4 * 2
  total += 1 if n % 4 == 3
  puts total
end

t=gets.to_i
t.times{
        n=gets.to_i
        puts n-2
}

gets.to_i.times do
	n = gets.to_i
	p n / 4 * 2 + (n % 4 == 3 ? 1 : 0)
end


Polycarp has $$$n$$$ coins, the value of the $$$i$$$-th coin is $$$a_i$$$. It is guaranteed that all the values are integer powers of $$$2$$$ (i.e. $$$a_i = 2^d$$$ for some non-negative integer number $$$d$$$).Polycarp wants to know answers on $$$q$$$ queries. The $$$j$$$-th query is described as integer number $$$b_j$$$. The answer to the query is the minimum number of coins that is necessary to obtain the value $$$b_j$$$ using some subset of coins (Polycarp can use only coins he has). If Polycarp can't obtain the value $$$b_j$$$, the answer to the $$$j$$$-th query is -1.The queries are independent (the answer on the query doesn't affect Polycarp's coins).

The first line of the input contains two integers $$$n$$$ and $$$q$$$ ($$$1 \le n, q \le 2 \cdot 10^5$$$) — the number of coins and the number of queries. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ — values of coins ($$$1 \le a_i \le 2 \cdot 10^9$$$). It is guaranteed that all $$$a_i$$$ are integer powers of $$$2$$$ (i.e. $$$a_i = 2^d$$$ for some non-negative integer number $$$d$$$). The next $$$q$$$ lines contain one integer each. The $$$j$$$-th line contains one integer $$$b_j$$$ — the value of the $$$j$$$-th query ($$$1 \le b_j \le 10^9$$$).

Print $$$q$$$ integers $$$ans_j$$$. The $$$j$$$-th integer must be equal to the answer on the $$$j$$$-th query. If Polycarp can't obtain the value $$$b_j$$$ the answer to the $$$j$$$-th query is -1.

N, Q = gets.split.map(&:to_i)
as = gets.split.map(&:to_i)
hash = Hash.new(0)
as.each do |a|
    hash[a] += 1
end
coins = hash.keys.sort.reverse

Q.times do |i|
    n = gets.to_i
    ans = 0
    coins.each do |c|
        result = n / c
        result = hash[c] if result > hash[c]
        n -= result*c
        ans += result
    end
    puts n == 0 ? ans : -1
end


Alice and Borys are playing tennis.A tennis match consists of games. In each game, one of the players is serving and the other one is receiving.Players serve in turns: after a game where Alice is serving follows a game where Borys is serving, and vice versa.Each game ends with a victory of one of the players. If a game is won by the serving player, it's said that this player holds serve. If a game is won by the receiving player, it's said that this player breaks serve.It is known that Alice won $$$a$$$ games and Borys won $$$b$$$ games during the match. It is unknown who served first and who won which games.Find all values of $$$k$$$ such that exactly $$$k$$$ breaks could happen during the match between Alice and Borys in total.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^3$$$). Description of the test cases follows. Each of the next $$$t$$$ lines describes one test case and contains two integers $$$a$$$ and $$$b$$$ ($$$0 \le a, b \le 10^5$$$; $$$a + b > 0$$$) — the number of games won by Alice and Borys, respectively. It is guaranteed that the sum of $$$a + b$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case print two lines. In the first line, print a single integer $$$m$$$ ($$$1 \le m \le a + b + 1$$$) — the number of values of $$$k$$$ such that exactly $$$k$$$ breaks could happen during the match. In the second line, print $$$m$$$ distinct integers $$$k_1, k_2, \ldots, k_m$$$ ($$$0 \le k_1 < k_2 < \ldots < k_m \le a + b$$$) — the sought values of $$$k$$$ in increasing order.

NoteIn the first test case, any number of breaks between $$$0$$$ and $$$3$$$ could happen during the match:   Alice holds serve, Borys holds serve, Alice holds serve: $$$0$$$ breaks;  Borys holds serve, Alice holds serve, Alice breaks serve: $$$1$$$ break;  Borys breaks serve, Alice breaks serve, Alice holds serve: $$$2$$$ breaks;  Alice breaks serve, Borys breaks serve, Alice breaks serve: $$$3$$$ breaks. In the second test case, the players could either both hold serves ($$$0$$$ breaks) or both break serves ($$$2$$$ breaks).In the third test case, either $$$2$$$ or $$$3$$$ breaks could happen:   Borys holds serve, Borys breaks serve, Borys holds serve, Borys breaks serve, Borys holds serve: $$$2$$$ breaks;  Borys breaks serve, Borys holds serve, Borys breaks serve, Borys holds serve, Borys breaks serve: $$$3$$$ breaks.

t = gets.to_i
t.times do

a, b = gets.split.map!(&:to_i).sort!
c = a + b

if c.even?
  h = c / 2
  x = (h - a).step(h + a, 2).to_a
else
  h1 = c.pred / 2
  h2 = c.succ / 2
  x = (h1 - a).step(h2 + a).to_a
end

puts x.size
puts x * " "

end