This year in Equestria was a year of plenty, so Applejack has decided to build some new apple storages. According to the advice of the farm designers, she chose to build two storages with non-zero area: one in the shape of a square and another one in the shape of a rectangle (which possibly can be a square as well).Applejack will build the storages using planks, she is going to spend exactly one plank on each side of the storage. She can get planks from her friend's company. Initially, the company storehouse has $$$n$$$ planks, Applejack knows their lengths. The company keeps working so it receives orders and orders the planks itself. Applejack's friend can provide her with information about each operation. For convenience, he will give her information according to the following format:  $$$+$$$ $$$x$$$: the storehouse received a plank with length $$$x$$$  $$$-$$$ $$$x$$$: one plank with length $$$x$$$ was removed from the storehouse (it is guaranteed that the storehouse had some planks with length $$$x$$$). Applejack is still unsure about when she is going to order the planks so she wants to know if she can order the planks to build rectangular and square storages out of them after every event at the storehouse. Applejack is busy collecting apples and she has completely no time to do the calculations so she asked you for help!We remind you that all four sides of a square are equal, and a rectangle has two pairs of equal sides.

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$): the initial amount of planks at the company's storehouse, the second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^5$$$): the lengths of the planks. The third line contains a single integer $$$q$$$ ($$$1 \le q \le 10^5$$$): the number of events in the company. Each of the next $$$q$$$ lines contains a description of the events in a given format: the type of the event (a symbol $$$+$$$ or $$$-$$$) is given first, then goes the integer $$$x$$$ ($$$1 \le x \le 10^5$$$).

After every event in the company, print "YES" if two storages of the required shape can be built from the planks of that company's set, and print "NO" otherwise. You can print each letter in any case (upper or lower).

NoteAfter the second event Applejack can build a rectangular storage using planks with lengths $$$1$$$, $$$2$$$, $$$1$$$, $$$2$$$ and a square storage using planks with lengths $$$1$$$, $$$1$$$, $$$1$$$, $$$1$$$.After the sixth event Applejack can build a rectangular storage using planks with lengths $$$2$$$, $$$2$$$, $$$2$$$, $$$2$$$ and a square storage using planks with lengths $$$1$$$, $$$1$$$, $$$1$$$, $$$1$$$.

n = gets.to_i
a = gets.chomp.split(" ").map(&:to_i)
q = gets.to_i
s = Array.new{Array.new}
q.times do |i|
  s[i] = gets.chomp.split(" ")
end
hash = Hash.new(0)
more4 = Hash.new(0)
more2 = Hash.new(0)
a.each do |x|
  hash[x] += 1
  if hash[x] >= 2
    more2[x] += 1
  end
  if hash[x] >= 4
    more4[x] += 1
  end
end
ans = []
q.times do |i|
  if s[i][0] == "+"
    hash[s[i][1].to_i] += 1
    if hash[s[i][1].to_i] >=2
      more2[s[i][1].to_i] += 1
    end
    if hash[s[i][1].to_i] >=4
      more4[s[i][1].to_i] += 1
    end
  elsif s[i][0] == "-"
    hash[s[i][1].to_i] -= 1
    if more2[s[i][1].to_i] >= 1
      more2[s[i][1].to_i] -= 1
      if more2[s[i][1].to_i] == 0
        more2.delete(s[i][1].to_i)
      end
    end
    if more4[s[i][1].to_i] >= 1
      more4[s[i][1].to_i] -= 1
      if more4[s[i][1].to_i] == 0
        more4.delete(s[i][1].to_i)
      end
    end
  end
  if more4.length >= 2
    ans << "YES"
  elsif more4.length == 1
    key = 0
    value = 0
    more4.each do |k,v|
      key = k
      value = v
    end
    if value >= 5
      ans << "YES"
    elsif more2[key] >= 5
      if more2.length >= 2
        ans << "YES"
      else
        ans << "NO"
      end
    elsif more2.length >= 3
      ans << "YES"
    else
      ans << "NO"
    end
  else
    ans << "NO"
  end
end
ans.each do |str|
  puts str
end

n = gets.to_i
a = gets.chomp.split(" ").map(&:to_i)
q = gets.to_i
s = Array.new{Array.new}
q.times do |i|
  s[i] = gets.chomp.split(" ")
end
hash = Hash.new(0)
more4 = Hash.new(0)
more2 = Hash.new(0)
a.each do |x|
  hash[x] += 1
  if hash[x] >= 2
    more2[x] += 1
  end
  if hash[x] >= 4
    more4[x] += 1
  end
end
ans = []
q.times do |i|
  if s[i][0] == "+"
    hash[s[i][1].to_i] += 1
    if hash[s[i][1].to_i] >=2
      more2[s[i][1].to_i] += 1
    end
    if hash[s[i][1].to_i] >=4
      more4[s[i][1].to_i] += 1
    end
  elsif s[i][0] == "-"
    hash[s[i][1]] -= 1
    if more2[s[i][1].to_i] >= 1
      more2[s[i][1].to_i] -= 1
      if more2[s[i][1].to_i] == 0
        more2.delete(s[i][1].to_i)
      end
    end
    if more4[s[i][1].to_i] >= 1
      more4[s[i][1].to_i] -= 1
      if more4[s[i][1].to_i] == 0
        more4.delete(s[i][1].to_i)
      end
    end
  end
  if more4.length >= 2
    ans << "YES"
  elsif more4.length == 1
    key = 0
    value = 0
    more4.each do |k,v|
      key = k
      value = v
    end
    if value >= 5
      ans << "YES"
    elsif more2[key] >= 5
      if more2.length >= 2
        ans << "YES"
      else
        ans << "NO"
      end
    elsif more2.length >= 3
      ans << "YES"
    else
      ans << "NO"
    end
  else
    ans << "NO"
  end
end
ans.each do |str|
  puts str
end


The string $$$s$$$ is given, the string length is odd number. The string consists of lowercase letters of the Latin alphabet.As long as the string length is greater than $$$1$$$, the following operation can be performed on it: select any two adjacent letters in the string $$$s$$$ and delete them from the string. For example, from the string "lemma" in one operation, you can get any of the four strings: "mma", "lma", "lea" or "lem" In particular, in one operation, the length of the string reduces by $$$2$$$.Formally, let the string $$$s$$$ have the form $$$s=s_1s_2 \dots s_n$$$ ($$$n>1$$$). During one operation, you choose an arbitrary index $$$i$$$ ($$$1 \le i < n$$$) and replace $$$s=s_1s_2 \dots s_{i-1}s_{i+2} \dots s_n$$$.For the given string $$$s$$$ and the letter $$$c$$$, determine whether it is possible to make such a sequence of operations that in the end the equality $$$s=c$$$ will be true? In other words, is there such a sequence of operations that the process will end with a string of length $$$1$$$, which consists of the letter $$$c$$$?

The first line of input data contains an integer $$$t$$$ ($$$1 \le t \le 10^3$$$) — the number of input test cases. The descriptions of the $$$t$$$ cases follow. Each test case is represented by two lines:   string $$$s$$$, which has an odd length from $$$1$$$ to $$$49$$$ inclusive and consists of lowercase letters of the Latin alphabet;  is a string containing one letter $$$c$$$, where $$$c$$$ is a lowercase letter of the Latin alphabet.

For each test case in a separate line output:  YES, if the string $$$s$$$ can be converted so that $$$s=c$$$ is true; NO otherwise.  You can output YES and NO in any case (for example, the strings yEs, yes, Yes and YES will be recognized as a positive response).

5

abcde

c

abcde

b

x

y

aaaaaaaaaaaaaaa

a

contest

t

NoteIn the first test case, $$$s$$$="abcde". You need to get $$$s$$$="c". For the first operation, delete the first two letters, we get $$$s$$$="cde". In the second operation, we delete the last two letters, so we get the expected value of $$$s$$$="c".In the third test case, $$$s$$$="x", it is required to get $$$s$$$="y". Obviously, this cannot be done.

cases = STDIN.gets.chomp.to_i
cases.times.each do
    input_string = STDIN.gets.chomp
    input_query = STDIN.gets.chomp
    puts input_string.length.times.find_all { |i| input_string[i,input_query.length] == input_query && i%2 ==0 }.length>0 ? "YES" : "NO"
    end

cases = STDIN.gets.chomp.to_i
cases.times.each do
    input_string = STDIN.gets.chomp
    input_query = STDIN.gets.chomp
    puts input_string.length.times.find_all { |i| input_string[i,input_query.length] == input_query }.find_all { |i| i%2==0 }.length>0 ? "YES" : "NO"
    end

n=gets().chomp.to_i
for i in 1..n do
    s=gets().chomp
    n=gets().chomp
    c=0
    for i in 0...s.length do
        if (i+1)%2!=0 && s[i]==n
            puts "YES"
            c+=1
            break
        end
    end
    if c==0
        puts "NO"
    end
end

t = gets.to_i
t.times {
  s = gets.strip

  c = gets[0]
  a = (0...s.length).find_all { |i| s[i] == c }
  found = false
  a.each { |i|
    found ||= (i % 2 == 0 && (s.length - i - 1) % 2 == 0)
    #puts("#{i} #{i % 2 == 0} #{(s.length - i - 1) % 2 == 0} #{(s.length - i - 1)}")
  }
  puts(found ? 'yes' : 'no')
}

n=gets().chomp.to_i
for i in 1..n do
    s=gets().chomp
    n=gets().chomp
    i=0
    for i in 0...s.length do
        if (i+1)%2!=0 && s[i]==n
            puts "YES"
            break
        end
    end
    if i==s.length
        puts "NO"
    end
end


To satisfy his love of matching socks, Phoenix has brought his $$$n$$$ socks ($$$n$$$ is even) to the sock store. Each of his socks has a color $$$c_i$$$ and is either a left sock or right sock. Phoenix can pay one dollar to the sock store to either:   recolor a sock to any color $$$c'$$$ $$$(1 \le c' \le n)$$$  turn a left sock into a right sock  turn a right sock into a left sock  The sock store may perform each of these changes any number of times. Note that the color of a left sock doesn't change when it turns into a right sock, and vice versa. A matching pair of socks is a left and right sock with the same color. What is the minimum cost for Phoenix to make $$$n/2$$$ matching pairs? Each sock must be included in exactly one matching pair.

The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains three integers $$$n$$$, $$$l$$$, and $$$r$$$ ($$$2 \le n \le 2 \cdot 10^5$$$; $$$n$$$ is even; $$$0 \le l, r \le n$$$; $$$l+r=n$$$) — the total number of socks, and the number of left and right socks, respectively. The next line contains $$$n$$$ integers $$$c_i$$$ ($$$1 \le c_i \le n$$$) — the colors of the socks. The first $$$l$$$ socks are left socks, while the next $$$r$$$ socks are right socks. It is guaranteed that the sum of $$$n$$$ across all the test cases will not exceed $$$2 \cdot 10^5$$$.

For each test case, print one integer — the minimum cost for Phoenix to make $$$n/2$$$ matching pairs. Each sock must be included in exactly one matching pair.

NoteIn the first test case, Phoenix can pay $$$2$$$ dollars to:   recolor sock $$$1$$$ to color $$$2$$$  recolor sock $$$3$$$ to color $$$2$$$  There are now $$$3$$$ matching pairs. For example, pairs $$$(1, 4)$$$, $$$(2, 5)$$$, and $$$(3, 6)$$$ are matching.In the second test case, Phoenix can pay $$$3$$$ dollars to:   turn sock $$$6$$$ from a right sock to a left sock  recolor sock $$$3$$$ to color $$$1$$$  recolor sock $$$4$$$ to color $$$1$$$  There are now $$$3$$$ matching pairs. For example, pairs $$$(1, 3)$$$, $$$(2, 4)$$$, and $$$(5, 6)$$$ are matching.

t = gets.to_s.to_i
1.upto(t) do |casenum|
  n, l, r = gets.to_s.split.map {|x| x.to_i }
  a = gets.to_s.split.map {|x| x.to_i - 1 }

  c = {}
  a.each_with_index do |val, i|
    c[val] = [0, 0] unless c.has_key?(val)
    if i < l
      c[val][0] += 1
    else
      c[val][1] += 1
    end
  end

  leftn = 0
  lefto = 0
  left = {}
  rightn = 0
  righto = 0
  right = {}

  c.each do |k, v|
    if v[0] > v[1]
      left[k] = v[0] - v[1]
      leftn += v[0] - v[1]
      lefto += (v[0] - v[1]) % 2
    elsif v[1] > v[0]
      right[k] = v[1] - v[0]
      rightn += v[1] - v[0]
      righto += (v[1] - v[0]) % 2
    end
  end

  result = 0

  result += [lefto, righto].min
  lefto -= result
  righto -= result
  leftn -= result
  rightn -= result

  while lefto > 0 && rightn > 0
    step = [lefto, rightn].min
    lefto -= step
    leftn -= step
    rightn -= step
    result += step
  end

  while righto > 0 && leftn > 0
    step = [righto, leftn].min
    righto -= step
    leftn -= step
    rightn -= step
    result += step
  end

  printf("%d\n", result + lefto + righto + (leftn - lefto + rightn - righto) / 2)
end


One day Alice was cleaning up her basement when she noticed something very curious: an infinite set of wooden pieces! Each piece was made of five square tiles, with four tiles adjacent to the fifth center tile:    By the pieces lay a large square wooden board. The board is divided into $$$n^2$$$ cells arranged into $$$n$$$ rows and $$$n$$$ columns. Some of the cells are already occupied by single tiles stuck to it. The remaining cells are free.Alice started wondering whether she could fill the board completely using the pieces she had found. Of course, each piece has to cover exactly five distinct cells of the board, no two pieces can overlap and every piece should fit in the board entirely, without some parts laying outside the board borders. The board however was too large for Alice to do the tiling by hand. Can you help determine if it's possible to fully tile the board?

The first line of the input contains a single integer $$$n$$$ ($$$3 \leq n \leq 50$$$) — the size of the board. The following $$$n$$$ lines describe the board. The $$$i$$$-th line ($$$1 \leq i \leq n$$$) contains a single string of length $$$n$$$. Its $$$j$$$-th character ($$$1 \leq j \leq n$$$) is equal to "." if the cell in the $$$i$$$-th row and the $$$j$$$-th column is free; it is equal to "#" if it's occupied. You can assume that the board contains at least one free cell.

Output YES if the board can be tiled by Alice's pieces, or NO otherwise. You can print each letter in any case (upper or lower).

NoteThe following sketches show the example boards and their tilings if such tilings exist:

def tiles(n,x,y)
  if x == n-1 || x == 0 || y >= n-2
    return false
  else
    return [[x,y],[x-1,y+1],[x,y+1],[x+1,y+1],[x,y+2]]
  end
end

def solve (n,board)
  n.times do |i|
    n.times do |j|
      if board[i][j] == "."
        #p [i,j]
        t = tiles(n,j,i)
        #p t
        return false if !t
        t.each do |x,y|
          if board[y][x] == "#"
            return false
          else
            board[y][x] = "#"
          end
        end
      end
    end
  end
  return true
end

n = gets.to_i
board = []
n.times do
  board << gets.chomp
end
puts solve(n,board) ? "YES" : "NO"

N = gets.to_i
board = N.times.map { gets.chomp.chars }

N.times do |y|
  N.times do |x|
    next if board[y][x] == '#'

    ay, ax = y, x
    by, bx = y + 1, x - 1
    cy, cx = y + 1, x
    dy, dx = y + 1, x + 1
    ey, ex = y + 2, x

    max_y = [ay, by, cy, dy, ey].max
    min_x, max_x = [ax, bx, cx, dx, ex].minmax

    if max_y >= N || min_x < 0 || N <= max_x
      puts 'NO'
      exit
    end

    [ay, by, cy, dy, ey].zip([ax, bx, cx, dx, ex]).each do |ny, nx|
      if board[ny][nx] == '#'
        puts 'NO'
        exit
      end

      board[ny][nx] = '#'
    end
  end
end

puts 'YES'

n = gets.to_i
s = n.times.map{gets.chomp}
d = [0,1,0,-1]
for i in 1..n-2
    for j in 1..n-2
        if s[i][j] == "."
            f = true
            4.times do |k|
                f = false if s[i+d[k]][j+d[k^1]] == "#"
            end
            if f
                4.times do |k|
                    s[i+d[k]][j+d[k^1]] = "#"
                end
                s[i][j] = "#"
            end
        end
    end
end
if s.join("").count(".") > 0
    puts "NO"
else
    puts "YES"
end


Little Mishka is a great traveller and she visited many countries. After thinking about where to travel this time, she chose XXX — beautiful, but little-known northern country.Here are some interesting facts about XXX:  XXX consists of n cities, k of whose (just imagine!) are capital cities.  All of cities in the country are beautiful, but each is beautiful in its own way. Beauty value of i-th city equals to ci.  All the cities are consecutively connected by the roads, including 1-st and n-th city, forming a cyclic route 1 — 2 — ... — n — 1. Formally, for every 1 ≤ i < n there is a road between i-th and i + 1-th city, and another one between 1-st and n-th city.  Each capital city is connected with each other city directly by the roads. Formally, if city x is a capital city, then for every 1 ≤ i ≤ n,  i ≠ x, there is a road between cities x and i.  There is at most one road between any two cities.  Price of passing a road directly depends on beauty values of cities it connects. Thus if there is a road between cities i and j, price of passing it equals ci·cj.Mishka started to gather her things for a trip, but didn't still decide which route to follow and thus she asked you to help her determine summary price of passing each of the roads in XXX. Formally, for every pair of cities a and b (a < b), such that there is a road between a and b you are to find sum of products ca·cb. Will you help her?

The first line of the input contains two integers n and k (3 ≤ n ≤ 100 000, 1 ≤ k ≤ n) — the number of cities in XXX and the number of capital cities among them. The second line of the input contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 10 000) — beauty values of the cities. The third line of the input contains k distinct integers id1, id2, ..., idk (1 ≤ idi ≤ n) — indices of capital cities. Indices are given in ascending order.

Print the only integer — summary price of passing each of the roads in XXX.

NoteThis image describes first sample case:It is easy to see that summary price is equal to 17.This image describes second sample case:It is easy to see that summary price is equal to 71.

n, k = gets.split.map { |x| x.to_i }
c = gets.split.map { |x| x.to_i }
id = gets.split.map { |x| x.to_i }

cap = Array.new n
id.each do |x|
	cap[x - 1] = true
end

sc = 0
sc2 = 0
n.times do |i|
	if cap[i]
		sc += c[i]
		sc2 += c[i] ** 2
	end
end

sum = 0
ans = (sc ** 2 - sc2) / 2
n.times do |i|
	sum += c[i] if !cap[i]
end
n.times do |i|
	ans += c[i] * sum if cap[i]
end

n.times do |i|
	if !cap[i - 1] and !cap[i]
		ans += c[i - 1] * c[i]
	end
end

puts ans

cityNum, capitalNum = gets.chomp.split(' ').map(&:to_i)
prices = gets.chomp.split(' ').map(&:to_i)
capitals = gets.chomp.split(' ').map(&:to_i)
isCapital = Array.new(cityNum) { |i| i = false }
#isMarked = Array.new(cityNum) { |i| i = false }

rawSum = 0
rawCapSum = 0

prices.each do |p|
	rawSum += p
end

capitals.each do |c|
	rawCapSum += prices[c - 1]
	isCapital[c - 1] = true
end

totalSum = 0

for i in 0...(prices.length)
	totalSum += (prices[i] * prices[(i + 1) % cityNum])
end

#puts "Total : #{totalSum}"

capitals.each do |c|
	partialSum = rawSum - prices[c - 1]
	#puts "Partial : #{partialSum}"
	totalSum += partialSum * prices[c - 1]
	#puts "c : #{c}, ts = #{totalSum}"
end

#puts "Total : #{totalSum}"

	count = 1
	capitals.each do |c|
		totalSum -= prices[c - 1] * (prices[c % cityNum] + prices[if c - 2 >= 0 then c - 2 else cityNum - 1 end])
		break if count == capitalNum

		partialSum = rawCapSum - prices[c - 1]

		if isCapital[c % cityNum]
			partialSum -= prices[c % cityNum]
		end

		if c == 1 && isCapital[if c - 2 >= 0 then c - 2 else cityNum - 1 end]
			partialSum -= prices[if c - 2 >= 0 then c - 2 else cityNum - 1 end]
		end

		#puts "c : #{c}"
		#puts "raw : #{rawCapSum}"
		#puts "partial : #{partialSum}"

		totalSum -= prices[c - 1] * partialSum
		rawCapSum -= prices[c - 1]
		count += 1
	end

puts totalSum


A new cottage village called «Flatville» is being built in Flatland. By now they have already built in «Flatville» n square houses with the centres on the Оx-axis. The houses' sides are parallel to the coordinate axes. It's known that no two houses overlap, but they can touch each other.The architect bureau, where Peter works, was commissioned to build a new house in «Flatville». The customer wants his future house to be on the Оx-axis, to be square in shape, have a side t, and touch at least one of the already built houses. For sure, its sides should be parallel to the coordinate axes, its centre should be on the Ox-axis and it shouldn't overlap any of the houses in the village.Peter was given a list of all the houses in «Flatville». Would you help him find the amount of possible positions of the new house?

The first line of the input data contains numbers n and t (1 ≤ n, t ≤ 1000). Then there follow n lines, each of them contains two space-separated integer numbers: xi ai, where xi — x-coordinate of the centre of the i-th house, and ai — length of its side ( - 1000 ≤ xi ≤ 1000, 1 ≤ ai ≤ 1000).

Output the amount of possible positions of the new house.

NoteIt is possible for the x-coordinate of the new house to have non-integer value.

# Problem A
# by vi002

n, t = gets.split.map { |v| v.to_i }
data = Array.new(n) { gets.split.map { |v| v.to_i } }.sort

result = 2
(0 ... n - 1).each do |pos|
  distance = 2 * (data[pos + 1].first - data[pos].first)
  distance -= data[pos][1] + data[pos + 1][1]
  result += 2 if distance > 2 * t
  result += 1 if distance == 2 * t
end

puts result

require 'set'
def check(x,t)
        ok=false
        $m.each{|h|
                if x+t<=h[0] || h[0]+h[1]<=x then
                else
                        return false
                end
        }
        return true
end
n,t=gets.split.map(&:to_i)
t*=2
$m=[]
n.times{|i|
        $m.push(gets.split.map(&:to_i))
        $m[i][0]*=2
        $m[i][0]-=$m[i][1]
        $m[i][1]*=2
}

q=Set.new
$m.each{|h|
        if(check(h[0]-t,t)) then
                q.add(h[0]-t)
        end
        if(check(h[0]+h[1],t)) then
                q.add(h[0]+h[1])
        end
}
puts q.length

##
# 15A
# Author: Sammy Teahan
#

if __FILE__ == $0

  n, t = gets.split.map(&:to_i)
  count = 2
  houses = []

  class House
    attr_reader :left, :right

    def initialize(x, a)
      @left = x - a / 2
      @right = x + a / 2
    end

  end

  n.times do |k|
    x, a = gets.split.map(&:to_f)
    house = House.new(x, a)
    houses << house
  end

  houses.sort_by! do |house|
    house.left
  end

  (houses.size - 1).times do |house|
    if houses[house].right + t < houses[house + 1].left
      count += 2
    elsif
      houses[house].right + t == houses[house + 1].left
      count += 1
    end
  end

  puts count
end

##
# 15A
# Author: Sammy Teahan
#

if __FILE__ == $0

  n, t = gets.split.map(&:to_i)
  count = 2
  houses = []

  class House
    attr_reader :left, :right

    def initialize(x, a)
      @left = (x - a / 2)
      @right = (x + a / 2)
    end

  end

  n.times do |k|
    x, a = gets.split.map(&:to_f)
    house = House.new(x, a)
    houses << house
  end

  # Sort our houses
  houses.sort_by! do |house|
    house.left
  end

  (houses.size - 1).times do |house|
    if houses[house].right + t < houses[house + 1].left
      count += 2
    elsif
      houses[house].right + t == houses[house + 1].left
      count += 1
    end
  end

  puts count
end

n, t = gets.split.map(&:to_i)
t = t.to_f
koord = Array.new
n.times { koord << gets.split.map(&:to_f) }
koord.sort!
ret = 2
(1...(n)).each do |i|
  x_i_1 = koord[i-1][0] + koord[i-1][1] / 2
  x_i_2 = koord[i][0]   - koord[i][1].abs / 2
  diff = (x_i_1 - x_i_2).abs
  if diff == t
    ret += 1
  elsif diff > t
    ret += 2
  end
end
puts ret

n,t=gets.split.map(&:to_i)
built=[]
n.times{
	x,a=gets.split.map(&:to_i)
	built<<[2*x-a,2*x+a]
}
amount=2 #left and right most already possible
built.sort{|a,b|a[0]<=>b[0]}.each_cons(2){|l,r|amount+=r[0]-l[1]<=>2*t}
puts amount+n-1

n,t,x,a=(gets+gets).split.map &:to_i;y=2;2.upto(n){s=x+x+a;x,a=gets.split.map &:to_i;s=x+x-a-s;y+=(s<=>t+t)+1};puts y

n,t=gets.split.map(&:to_i)
built=[]
n.times{
	x,a=gets.split.map(&:to_i)
	built<<[x-a/2,x+a/2]
}
amount=2 #left and right most already possible
built.each_cons(2){|(l,r)|
	amount+=(r[0]-l[1]<=>t)+1
}
puts amount

n,t=gets.split.map(&:to_i)
built=[]
n.times{
	x,a=gets.split.map(&:to_i)
	built<<[x-a/2,x+a/2]
}
amount=2 #left and right most already possible
built.sort{|a,b|a[0]<=>b[0]}.each_cons(2){|(l,r)|amount+=r[0]-l[1]<=>t}
puts amount+n-1


A new entertainment has appeared in Buryatia — a mathematical circus! The magician shows two numbers to the audience — $$$n$$$ and $$$k$$$, where $$$n$$$ is even. Next, he takes all the integers from $$$1$$$ to $$$n$$$, and splits them all into pairs $$$(a, b)$$$ (each integer must be in exactly one pair) so that for each pair the integer $$$(a + k) \cdot b$$$ is divisible by $$$4$$$ (note that the order of the numbers in the pair matters), or reports that, unfortunately for viewers, such a split is impossible.Burenka really likes such performances, so she asked her friend Tonya to be a magician, and also gave him the numbers $$$n$$$ and $$$k$$$.Tonya is a wolf, and as you know, wolves do not perform in the circus, even in a mathematical one. Therefore, he asks you to help him. Let him know if a suitable splitting into pairs is possible, and if possible, then tell it.

The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The following is a description of the input data sets. The single line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$, $$$0 \leq k \leq 10^9$$$, $$$n$$$ is even) — the number of integers and the number being added $$$k$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, first output the string "YES" if there is a split into pairs, and "NO" if there is none. If there is a split, then in the following $$$\frac{n}{2}$$$ lines output pairs of the split, in each line print $$$2$$$ numbers — first the integer $$$a$$$, then the integer $$$b$$$.

NoteIn the first test case, splitting into pairs $$$(1, 2)$$$ and $$$(3, 4)$$$ is suitable, same as splitting into $$$(1, 4)$$$ and $$$(3, 2)$$$.In the second test case, $$$(1 + 0) \cdot 2 = 1 \cdot (2 + 0) = 2$$$ is not divisible by $$$4$$$, so there is no partition.

def main
  t = inp.to_i
  t.times do
    n, k = mapint(inarr)
    if k % 4 == 0
      puts "NO"
      next
    end
    puts "YES"
    ans = ""

    r = Array.new(n>>1)

    pos = 0
    i = 1
    while i <= n
        if ((i + k) * (i + 1) & 3) == 0
          r[pos] = "#{i} #{i+1}"
        else
          r[pos]  = "#{i + 1} #{i}"
        end
        pos+=1
        i+=2
      end

    puts r

  end

end

# lib
def inp
  gets.chomp
end

def inarr
  gets.chomp.split
end

def mapint(lst)
  lst.map(&:to_i)
end

def max(a, b)
  (a > b)? a:b
end

def min(a, b)
  (a < b)? a:b
end

main

gets.to_i.times do
  n, k = gets.split.map(&:to_i)
  abs = if k % 2 == 1
      (1..n).step(2).map do |i|
        [i, i + 1]
      end
    else
      odds = (1..n).step(2).to_a
      (4..n).step(4).map do |b|
        [odds.pop, b]
      end + (2..n).step(4).map do |a|
        [a, odds.pop]
      end
    end
  if abs.all? { |a, b| (a + k) * b % 4 == 0 }
    puts "YES"
    abs.each do |a, b|
      puts "#{a} #{b}"
    end
  else
    puts "NO"
  end
end

def main
  t = inp.to_i
  t.times do
    n, k = mapint(inarr)
    if k == 0
      puts "NO"
      next
    end
    puts "YES"
    ans = ""

      1.step(n, 2) do |i|
        if ((i + k) * (i + 1) & 3) == 0
          ans += "#{i} #{i+1}\n"
        else
          ans += "#{i + 1} #{i}\n"
        end

      end

    puts ans

  end
end

# lib
def inp
  gets.chomp
end

def inarr
  gets.chomp.split
end

def mapint(lst)
  lst.map(&:to_i)
end

def max(a, b)
  (a > b)? a:b
end

def min(a, b)
  (a < b)? a:b
end

main

def main
  t = inp.to_i
  t.times do
    n, k = mapint(inarr)
    if k == 0
      puts "NO"
      next
    end
    puts "YES"
    ans = ""
    if k % 2 == 0
      1.step(n, 2) do |i|
        ans += "#{i + 1} #{i}\n"
      end
    else
      1.step(n, 2) do |i|
        ans += "#{i} #{i+1}\n"
      end
    end
    puts ans

  end
end

# lib
def inp
  gets.chomp
end

def inarr
  gets.chomp.split
end

def mapint(lst)
  lst.map(&:to_i)
end

def max(a, b)
  (a > b)? a:b
end

def min(a, b)
  (a < b)? a:b
end

main

gets.to_i.times do
  n, k = gets.split.map(&:to_i)
  if k == 0
    puts "NO"
  elsif k % 2 == 1
    puts "YES"
    (1..n).step(2).each do |i|
      puts "#{i} #{i + 1}"
    end
  else
    puts "YES"
    (1..n).step(2).each do |i|
      if (i + k) % 4 == 1
        puts "#{i} #{i + 1}"
      else
        puts "#{i + 1} #{i}"
      end
    end
  end
end


You are given two strings $$$s$$$ and $$$t$$$. The string $$$s$$$ consists of lowercase Latin letters and at most one wildcard character '*', the string $$$t$$$ consists only of lowercase Latin letters. The length of the string $$$s$$$ equals $$$n$$$, the length of the string $$$t$$$ equals $$$m$$$.The wildcard character '*' in the string $$$s$$$ (if any) can be replaced with an arbitrary sequence (possibly empty) of lowercase Latin letters. No other character of $$$s$$$ can be replaced with anything. If it is possible to replace a wildcard character '*' in $$$s$$$ to obtain a string $$$t$$$, then the string $$$t$$$ matches the pattern $$$s$$$.For example, if $$$s=$$$"aba*aba" then the following strings match it "abaaba", "abacaba" and "abazzzaba", but the following strings do not match: "ababa", "abcaaba", "codeforces", "aba1aba", "aba?aba".If the given string $$$t$$$ matches the given string $$$s$$$, print "YES", otherwise print "NO".

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 2 \cdot 10^5$$$) — the length of the string $$$s$$$ and the length of the string $$$t$$$, respectively. The second line contains string $$$s$$$ of length $$$n$$$, which consists of lowercase Latin letters and at most one wildcard character '*'. The third line contains string $$$t$$$ of length $$$m$$$, which consists only of lowercase Latin letters.

Print "YES" (without quotes), if you can obtain the string $$$t$$$ from the string $$$s$$$. Otherwise print "NO" (without quotes).

NoteIn the first example a wildcard character '*' can be replaced with a string "force". So the string $$$s$$$ after this replacement is "codeforces" and the answer is "YES".In the second example a wildcard character '*' can be replaced with an empty string. So the string $$$s$$$ after this replacement is "vkcup" and the answer is "YES".There is no wildcard character '*' in the third example and the strings "v" and "k" are different so the answer is "NO".In the fourth example there is no such replacement of a wildcard character '*' that you can obtain the string $$$t$$$ so the answer is "NO".

_,a,b=$<.map &:chomp
if !a.index('*')
	puts a==b ? :Yes : :No
else
	x,y=a.split('*')
	x||='';y||=''
	puts b.start_with?(x) && b.end_with?(y) && b.size>=x.size+y.size ? :Yes : :No
end

N, M = gets.split.map(&:to_i)
s = '!' + gets.chomp + '!'
t = '!' + gets.chomp + '!'
if s.include?('*')
    s1, s2 = s.split('*')
    if s.length-1 <= t.length && s1 == t[0, s1.length] && s2 == t[t.length-s2.length, s2.length]
        puts 'YES'
    else
        puts 'NO'
    end
else
    if s == t
        puts 'YES'
    else
        puts 'NO'
    end
end

sl,tl=gets.strip.split.map(&:to_i)
ml = (sl<tl ? sl : tl)
s=gets.strip
t=gets.strip

if tl < sl-1
  puts "NO"
  exit 0
end

unless s.include?('*')
  puts (s==t ? "YES" : "NO")
else
  i=0
  while i<ml && s[i]==t[i] do
    i +=1
  end
  if i<ml && s[i]!='*'
    puts "NO"
    exit 0
  end
  i=sl-1
  j=tl-1
  while i>=0 && j>=0 && s[i]==t[j] do
    i -= 1
    j -= 1
  end
  if i>=0 && j>=0 && s[i]!='*'
    puts "NO"
    exit 0
  end
  puts "YES"
end

n,m = gets.chomp.split(" ").map(&:to_i)
s,t = gets.chomp,gets.chomp
ans = true
if s.include?("*")
  if s[-1] == "*" && s.delete("*") != t[0,s.size-1]
    ans = false
  elsif s[-1] != "*"
    a,b = s.split("*")
    if a != t[0,a.size] || b != t[m-b.size..-1] || m < n-1
      ans = false
    end
  end
elsif s != t
  ans = false
end
puts ans ? "YES" : "NO"

#! /usr/bin/env ruby

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

if s.include?("*")
  a, b = s.split("*")
  if a == nil and b == nil
    puts "YES"
  elsif a == nil
    puts (t.end_with?(b) ? "YES" : "NO")
  elsif b == nil
    puts (t.start_with?(a) ? "YES" : "NO")
  else
    if t.start_with?(a) and t.end_with?(b)
      if a.size + b.size <= m
        puts "YES"
      else
        puts "NO"
      end
    else
      puts "NO"
    end
  end
else
  puts (s == t ? "YES" : "NO")
end

gets;puts /#{gets.chomp.gsub('*','.*')}/=~gets.chomp ? :Yes : :No

n,m = gets.chomp.split(" ").map(&:to_i)
s,t = gets.chomp,gets.chomp
first,second = true,true if s.include?("*")
if s[0] != "*" && first
  a = s.split("*")[0]
  i = t.index(a)
  if i
    t.slice!(0,i+a.size)
  else
    first = false
  end
end
if s[-1] != "*" && second
  b = s.split("*")[1]
  i = t.rindex(b)
  if i
    t.slice!(0,i+b.size)
    if t.size != 0
      second = false
    end
  else
    second = false
  end
end
puts (first && second) || s == t ? "YES" : "NO"

n,m = gets.chomp.split(" ").map(&:to_i)
s,t = gets.chomp.split("*"),gets.chomp
ans = true
for i in 0..s.size-1
  j = t.index(s[i],0)
  if j
    t.slice!(0,j+s[i].size)
  else
    ans = false
  end
end
puts ans ? "YES" : "NO"

n,m = gets.chomp.split(" ").map(&:to_i)
s,t = gets.chomp.split("*"),gets.chomp
ans = true
p s,t
for i in 0..s.size-1
  j = t.index(s[i],0)
  if j
    t.slice!(0,j+s[i].size)
  else
    ans = false
  end
  p t
end
puts (ans&&t.size==1) ? "YES" : "NO"

n,m = gets.chomp.split(" ").map(&:to_i)
s,t = gets.chomp.split("*"),gets.chomp
ans = true
for i in 0..s.size-1
  j = t.index(s[i],0)
  if j
    t.slice!(0,j+s[i].size)
  else
    ans = false
  end
end
puts (ans&&t.size==0) ? "YES" : "NO"

n,m = gets.chomp.split(" ").map(&:to_i)
s,t = gets.chomp.split("*"),gets.chomp
ans = true
for i in 0..s.size-1
  j = t.index(s[i],0)
  if j
    t.slice!(0,j+s[i].size)
  else
    ans = false
  end
end
puts (ans && (t.size == 0 || s.size == 1)) ? "YES" : "NO"

n,m = gets.chomp.split(" ").map(&:to_i)
s,t = gets.chomp.split("*"),gets.chomp
ans = true
for i in 0..s.size-1
  j = t.index(s[i],0)
  if j
    t.slice!(0,j+s[i].size)
  else
    ans = false
  end
end
puts (ans && (t.size == 0 || s.size <= 1)) ? "YES" : "NO"

n,m = gets.chomp.split(" ").map(&:to_i)
s,t = gets.chomp.split("*"),gets.chomp
ans = true
p s,t
for i in 0..s.size-1
  j = t.index(s[i],0)
  if j
    t.slice!(0,j+s[i].size)
  else
    ans = false
  end
  p t
end
puts (ans&&t.size==0) ? "YES" : "NO"

#! /usr/bin/env ruby

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

if s.include?("*")
  a, b = s.split("*")
  if a == nil and b == nil
    puts "YES"
  elsif a == nil
    puts (t.include?(b) ? "YES" : "NO")
  elsif b == nil
    puts (t.include?(a) ? "YES" : "NO")
  else
    li, ri = t.index(a), t.rindex(b)
    if li == nil or ri == nil
      puts "NO"
    else
      if li + a.size <= ri
        puts "YES"
      else
        puts "NO"
      end
    end
  end
else
  puts (s == t ? "YES" : "NO")
end


Let's define a function $$$f(x)$$$ ($$$x$$$ is a positive integer) as follows: write all digits of the decimal representation of $$$x$$$ backwards, then get rid of the leading zeroes. For example, $$$f(321) = 123$$$, $$$f(120) = 21$$$, $$$f(1000000) = 1$$$, $$$f(111) = 111$$$.Let's define another function $$$g(x) = \dfrac{x}{f(f(x))}$$$ ($$$x$$$ is a positive integer as well).Your task is the following: for the given positive integer $$$n$$$, calculate the number of different values of $$$g(x)$$$ among all numbers $$$x$$$ such that $$$1 \le x \le n$$$.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n < 10^{100}$$$). This integer is given without leading zeroes.

For each test case, print one integer — the number of different values of the function $$$g(x)$$$, if $$$x$$$ can be any integer from $$$[1, n]$$$.

NoteExplanations for the two first test cases of the example:  if $$$n = 4$$$, then for every integer $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$;  if $$$n = 37$$$, then for some integers $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$ (for example, if $$$x = 23$$$, $$$f(f(x)) = 23$$$,$$$\dfrac{x}{f(f(x))} = 1$$$); and for other values of $$$x$$$, $$$\dfrac{x}{f(f(x))} = 10$$$ (for example, if $$$x = 30$$$, $$$f(f(x)) = 3$$$, $$$\dfrac{x}{f(f(x))} = 10$$$). So, there are two different values of $$$g(x)$$$.

t = gets.chomp.to_i
t.times do
    n = gets.chomp.split("")
    puts n.size

end

class CodeForces

  attr_reader :answer

  def initialize(test_cases)
    @test_cases = test_cases
  end

def answer

@test_cases.to_i.times do |i|

  args =  gets.chomp
  solve(args)

end
end

private

def solve(args)

  # multiple assign here!
  # use the right amount of variables

 # a = args.split(" ").map(&:to_i)

  # do work here
  answer = args.to_s.size

  puts answer
end
end

CodeForces.new(gets.chomp).answer


$$$n$$$ heroes fight against each other in the Arena. Initially, the $$$i$$$-th hero has level $$$a_i$$$.Each minute, a fight between two different heroes occurs. These heroes can be chosen arbitrarily (it's even possible that it is the same two heroes that were fighting during the last minute).When two heroes of equal levels fight, nobody wins the fight. When two heroes of different levels fight, the one with the higher level wins, and his level increases by $$$1$$$.The winner of the tournament is the first hero that wins in at least $$$100^{500}$$$ fights (note that it's possible that the tournament lasts forever if no hero wins this number of fights, then there is no winner). A possible winner is a hero such that there exists a sequence of fights that this hero becomes the winner of the tournament.Calculate the number of possible winners among $$$n$$$ heroes.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. Each test case consists of two lines. The first line contains one integer $$$n$$$ ($$$2 \le n \le 100$$$) — the number of heroes. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$), where $$$a_i$$$ is the initial level of the $$$i$$$-th hero.

For each test case, print one integer — the number of possible winners among the given $$$n$$$ heroes.

NoteIn the first test case of the example, the only possible winner is the first hero.In the second test case of the example, each fight between the heroes results in nobody winning it, so the tournament lasts forever and there is no winner.

alias g gets;g.to_i.times{g;a=g.split.map &:to_i;p a.size-a.count(a.min)}

alias g gets;g.to_i.times{g;a=g.split.map &:to_i;puts a.size-a.count(a.min)}

alias g gets
g.to_i.times {
  g
  a = g.split.map &:to_i
  puts a.size - a.count(a.min)
}

def g
    gets
end
g.to_i.times {
    g
    a = g.split.map &:to_i
    puts a.size - a.count(a.min)
}

gets.to_i.times do
    gets
    a = gets.split.map &:to_i
    puts a.size - a.count(a.min)
end

gets.to_i.times do
    gets
    a = gets.split.map &:to_i
    a.delete a.min
    puts a.size
end

gets.to_i.times do
  _ = gets
  arr = gets.split.map &:to_i
  min = arr.min
  sum = 0
  for i in arr
    sum += 1 if i > min
  end

  puts sum
end

gets.to_i.times do
  n=gets.to_i
  a=gets.split.map(&:to_i)
  a.delete(a.min)
  puts a.size
end


Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to ai.Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?

The first line of the input contains two integers n and h (1 ≤ n ≤ 1000, 1 ≤ h ≤ 1000) — the number of friends and the height of the fence, respectively. The second line contains n integers ai (1 ≤ ai ≤ 2h), the i-th of them is equal to the height of the i-th person.

Print a single integer — the minimum possible valid width of the road.

NoteIn the first sample, only person number 3 must bend down, so the required width is equal to 1 + 1 + 2 = 4.In the second sample, all friends are short enough and no one has to bend, so the width 1 + 1 + 1 + 1 + 1 + 1 = 6 is enough.In the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2 + 2 + 2 + 2 + 2 + 1 = 11.

# Vanya and Fence
# https://codeforces.com/problemset/problem/677/A

# _nums, f_height = gets.strip.split.map(&:to_i)
f_height = gets.strip.split.last.to_i
p_height = gets.strip.split.map(&:to_i)
width = 0

p_height.each do |h|
  width +=
    h <= f_height ? 1 : 2
end

puts width

n, h = gets.split.map(&:to_i)
arr = gets.split.map(&:to_i).select { |i| i > h }
puts n + arr.size

n,h = gets.chomp.split(" ")
friends = gets.chomp.split(" ").map(&:to_i)

def vanya_fence(n,h, friends)
  min_width = 0
  friends.each do |f|
    min_width += 1
    min_width += 1 if f > h.to_i
  end
  min_width
end

p vanya_fence(n,h, friends)

n, h = gets.chomp.split.map(&:to_i)
arr = gets.chomp.split.map(&:to_i).map { |d| d <= h ? 1 : 2 }
puts arr.inject(0, :+)

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

ans = 0

a = []
a = gets.split.map &:to_i

a.each do |i|
    if (i > m)
        ans += 2
    else
        ans += 1
    end
end

puts ans

_, zLen = STDIN.gets.chomp.split.map(&:to_i)
p STDIN.gets.chomp.split.inject(0){ |z, v| z + ((v.to_i - 1) / zLen) + 1 }

def string_to_int(string, delim = " ")
  result = []
  string = string.split(delim)
  for i in 0...string.length
    result[i] = string[i].to_i
  end
  return result
end

def width(string, delim = " ", height_limit)
  sum = 0
  string = string.split(delim)
  for i in 0...string.length
    if string[i].to_i > height_limit
		sum = sum + 2
    else
		sum = sum + 1
    end
  end
  return sum
end

input = string_to_int(gets.chomp)
limit = input[1]
puts width(gets.chomp, " ", limit)

x, y = gets.chomp().split(" ")
ans = 0
arr = gets().split(" ").each {|m| ans+= m.to_i > y.to_i ? 2:1}
puts ans

x, y = gets.chomp().split(" ").each {|x| x.to_i}
#puts x,y
ans = 0
arr = gets().split(" ").each {|m| ans+= m.to_i > y.to_i ? 2:1}
puts ans

n,k = gets.split(" ").map(&:to_i)
arr = gets.split(" ").map(&:to_i)
width =0
arr.each do |val|
  if val >  k
     width +=2
  else
      width +=1
  end
end
puts width

x= gets.chomp.split(' ').map(&:to_i)
y= gets.chomp.split(' ').map(&:to_i)
c = 0
y.each do |a|
     if (a>x[1])
     	c= c+2
     else
     	c = c +1
     end
 end
 puts c

n, h = readline.split(' ').collect(&:to_i)
arr = readline.split(' ').collect(&:to_i)
puts arr.map{|num| num > h ? 2 : 1}.inject(0, :+)

n, h = gets.split.map(&:to_i)
ans = 0
gets.split.map(&:to_i).each do |i|
  if i > h
    ans += 2
  else
    ans += 1
  end
end
puts ans

n, h = gets.chomp.split(/ /).map(&:to_i)
ar = gets.chomp.split(/ /).map(&:to_i)
ans = 0
ar.each_index do |i|
    a = ar[i]
    if a <= h
        ans += 1
    else
        ans += 2
    end
end
puts ans

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

sum=0
x.map{|e|
	sum=sum+((h<e)?2:1)
}
puts sum

n, h = gets.chomp.split.map(&:to_i)
arr = gets.chomp.split.map(&:to_i)

co = 0
arr.each do |el|
  if el > h
    co += 2
  else
  co += 1
  end
end
puts co

n,h = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
a.each do |i|
	if i > h
		n += 1
	end
end
puts n

n, h = gets.split.map(&:to_i)
puts gets.split.map { |e| e = e.to_i > h ? 2 : 1 }.reduce(:+)

a,b = gets.split(" ").map(&:to_i)
arr = gets.split(" ").map(&:to_i)
result = 0
arr.each_index do |i|
  num = arr[i]
  if num <= b
    result+=1
  else
    result+=2
  end
end
puts result

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

puts as.map { |a| a <= h ? 1 : 2 }.inject(&:+)

n_f = gets.chomp
h = gets.chomp
n_fence = n_f.split

heights = h.split
friends = n_fence[0]
fence = n_fence[1]

width = 0
heights.each do |height|
	if height.to_i <= fence.to_i
		width += 1
	else
		width += 2
	end
end

print width

#!/usr/bin/ruby

n, h = gets.chomp.split(" ")
h = h.to_i
a = gets.chomp

b = a.split(" ")

sum = 0
b.each do |i|
    sum += 1 if (i.to_i <= h.to_i)
    sum += 2 if (i.to_i > h.to_i)
end

puts sum

n , h = gets.split(" ")
n = n.to_i
h = h.to_i
s = gets.split(" ").map(&:to_i)
ans = 0
s.each do |i|
    if  i > h
        ans += 2
    else
        ans += 1
    end
end
puts ans

a, b = gets.split.map &:to_i
c = gets.split.map &:to_i
w = 0
c.map {|x| w += (x + b -1)/b}
puts w

#!/usr/bin/env ruby

n,h = gets.split.map(&:to_i)
ans = gets.split.map(&:to_i).inject(0) { |s,elem| s + (elem + h - 1) / h }
puts ans

n, h = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
puts a.count{|x| x > h} + n

n, h = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
ans = a.map{|x| x <= h ? 1 : 2}.inject(:+)
puts ans

n, h = gets.split.map(&:to_i)
p gets.split.map(&:to_i).map{|x|x <= h ? 1 : 2}.inject(:+)

first = gets.chomp.split(" ")
h = first[1].to_i
second = gets.chomp.split(" ")

arr = second.collect{ |el| el.to_i}

res = 0

for a in arr
  if a > h
    res += 2
  else
    res += 1
  end
end

puts res

totalFriends, fenceHeight = gets.split.map(&:to_i)
totalBendFriends = gets.split.count { |e| e.to_i > fenceHeight}
puts totalFriends + totalBendFriends

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

puts a.size + a.count { |x| x > h }

n,m = gets.chomp.split.map(&:to_i)
arr = gets.chomp.split.map(&:to_i)
puts arr.map{|i| i > m ? 2 : 1}.reduce(:+)

n, h = gets.chomp.split(' ').map { |i| i.to_i }
personHeight = gets.chomp.split(' ').map { |i| i.to_i }

ans = 0

personHeight.each do |pH|
	ans += 1 if pH <= h
	ans += 2 if pH > h
end

puts ans

n,h = gets.chomp.split(" ")
friends = gets.chomp.split(" ").map(&:to_i)

def vanya_fence(n,h, friends)
  min_width = 0
  friends.each do |f|
    min_width += 1 if f > h
  end
  min_width
end

n,k = gets.split(" ").map(&:to_i)
arr = gets.split(" ").map(&:to_i)
width =0
arr.each do |val|
  if val >= k
     width +=2
  else
      width +=1
  end
end
puts width


There are $$$n$$$ astronauts working on some space station. An astronaut with the number $$$i$$$ ($$$1 \le i \le n$$$) has power $$$a_i$$$.An evil humanoid has made his way to this space station. The power of this humanoid is equal to $$$h$$$. Also, the humanoid took with him two green serums and one blue serum.In one second , a humanoid can do any of three actions:  to absorb an astronaut with power strictly less humanoid power;  to use green serum, if there is still one left;  to use blue serum, if there is still one left. When an astronaut with power $$$a_i$$$ is absorbed, this astronaut disappears, and power of the humanoid increases by $$$\lfloor \frac{a_i}{2} \rfloor$$$, that is, an integer part of $$$\frac{a_i}{2}$$$. For example, if a humanoid absorbs an astronaut with power $$$4$$$, its power increases by $$$2$$$, and if a humanoid absorbs an astronaut with power $$$7$$$, its power increases by $$$3$$$.After using the green serum, this serum disappears, and the power of the humanoid doubles, so it increases by $$$2$$$ times.After using the blue serum, this serum disappears, and the power of the humanoid triples, so it increases by $$$3$$$ times.The humanoid is wondering what the maximum number of astronauts he will be able to absorb if he acts optimally.

The first line of each test contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains integers $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — number of astronauts and $$$h$$$ ($$$1 \le h \le 10^6$$$) — the initial power of the humanoid. The second line of each test case contains $$$n$$$ integers $$$a_i$$$ ($$$1 \le a_i \le 10^8$$$) — powers of astronauts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, in a separate line, print the maximum number of astronauts that a humanoid can absorb.

NoteIn the first case, you can proceed as follows:   use green serum. $$$h = 1 \cdot 2 = 2$$$  absorb the cosmonaut $$$2$$$. $$$h = 2 + \lfloor \frac{1}{2} \rfloor = 2$$$  use green serum. $$$h = 2 \cdot 2 = 4$$$  absorb the spaceman $$$1$$$. $$$h = 4 + \lfloor \frac{2}{2} \rfloor = 5$$$  use blue serum. $$$h = 5 \cdot 3 = 15$$$  absorb the spaceman $$$3$$$. $$$h = 15 + \lfloor \frac{8}{2} \rfloor = 19$$$  absorb the cosmonaut $$$4$$$. $$$h = 19 + \lfloor \frac{9}{2} \rfloor = 23$$$

gets.to_i.times do
    n,mh=gets.chomp.split(' ').map(&:to_i)
    arr=gets.chomp.split(' ').map(&:to_i).sort
    mx_ans,b=0,[0,1,2]
    b.each do |idx|
        ans =0
        s=0
        h=mh
        arr.each do |i|
            while i>=h && s<3
                if s==idx
                    h*=3
                else
                    h*=2
                end
                s+=1
            end
            if h>i
                h+=(i/2)
                ans+=1
            end
        end
        mx_ans=[mx_ans, ans].max
    end
    puts mx_ans
end


You are given two arrays of integers $$$a_1,\ldots,a_n$$$ and $$$b_1,\ldots,b_m$$$.Your task is to find a non-empty array $$$c_1,\ldots,c_k$$$ that is a subsequence of $$$a_1,\ldots,a_n$$$, and also a subsequence of $$$b_1,\ldots,b_m$$$. If there are multiple answers, find one of the smallest possible length. If there are still multiple of the smallest possible length, find any. If there are no such arrays, you should report about it.A sequence $$$a$$$ is a subsequence of a sequence $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero) elements. For example, $$$[3,1]$$$ is a subsequence of $$$[3,2,1]$$$ and $$$[4,3,1]$$$, but not a subsequence of $$$[1,3,3,7]$$$ and $$$[3,10,4]$$$.

The first line contains a single integer $$$t$$$ ($$$1\le t\le 1000$$$)  — the number of test cases. Next $$$3t$$$ lines contain descriptions of test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1\le n,m\le 1000$$$)  — the lengths of the two arrays. The second line of each test case contains $$$n$$$ integers $$$a_1,\ldots,a_n$$$ ($$$1\le a_i\le 1000$$$)  — the elements of the first array. The third line of each test case contains $$$m$$$ integers $$$b_1,\ldots,b_m$$$ ($$$1\le b_i\le 1000$$$)  — the elements of the second array. It is guaranteed that the sum of $$$n$$$ and the sum of $$$m$$$ across all test cases does not exceed $$$1000$$$ ($$$\sum\limits_{i=1}^t n_i, \sum\limits_{i=1}^t m_i\le 1000$$$).

For each test case, output "YES" if a solution exists, or "NO" otherwise. If the answer is "YES", on the next line output an integer $$$k$$$ ($$$1\le k\le 1000$$$)  — the length of the array, followed by $$$k$$$ integers $$$c_1,\ldots,c_k$$$ ($$$1\le c_i\le 1000$$$)  — the elements of the array. If there are multiple solutions with the smallest possible $$$k$$$, output any.

NoteIn the first test case, $$$[4]$$$ is a subsequence of $$$[10, 8, 6, 4]$$$ and $$$[1, 2, 3, 4, 5]$$$. This array has length $$$1$$$, it is the smallest possible length of a subsequence of both $$$a$$$ and $$$b$$$.In the third test case, no non-empty subsequences of both $$$[3]$$$ and $$$[2]$$$ exist, so the answer is "NO".

    t = gets.chomp.to_i
    while(t>0)
    	n_m = gets.chomp.split(" ")
    	n = n_m[0].to_i
    	m = n_m[1].to_i
    	a_inp = gets.chomp.split(" ")
    	a = a_inp.map{|i| i = i.to_i}
    	b_inp = gets.chomp.split(" ")
    	b = b_inp.map{|i| i = i.to_i}
    	c = []
    	c = a & b
    	unless(c.count>0)
    		puts "NO"
    	else
    		puts "YES"
    		puts "1 #{c[0]}"
    	end
    	t = t-1
    end

t = gets.to_i

1.upto t do
  n, m = gets.split.map(&:to_i)
  a = gets.split.map(&:to_i)
  b = gets.split.map(&:to_i)
  c = a.intersection(b)
  if c.empty?
    puts "NO"
  else
    puts "YES"
    puts "1 #{c[0]}"
  end
end

t = gets.chomp.to_i
t.times do
    arr = Array.new(1001,0)
    ar = Array.new(1001,0)
    n, m = gets.chomp.split(" ").map(&:to_i)
    if n >= 2
    a = gets.chomp.split(" ").map(&:to_i)
        a.each do |i|
            arr[i] += 1
        end
    else
        a = gets.chomp.to_i
        arr[a] += 1
    end

    if m >= 2
        b = gets.chomp.split(" ").map(&:to_i)
            b.each do |j|
                ar[j] += 1
            end
    else
            b = gets.chomp.to_i
            ar[b] += 1
    end
    ans = 0
    (1..1000).each do |k|
        if arr[k] >= 1 && ar[k] >= 1
            puts "YES"
            print "1"
            print " "
            print k
            print "\n"
            ans += 1
            break
        end
    end
    if ans == 0
    puts "NO"
    end
end

def read
    gets.split.map(&:to_i)
end
gets.to_i.times do
    n, m = read
    a = read
    b = read
    res = nil
    a.each do |i|
        b.each do |j|
            res = i if i == j
        end
    end
    if res.nil?
        puts "NO"
    else
        puts "YES", 1, res
    end
end

tc = gets.to_i

tc.times do
    n,m = gets.split().map(&:to_i)
    arr = gets.split().map(&:to_i)
    brr = gets.split().map(&:to_i)
    crr = arr & brr
    if(crr.size > 0)
        puts "YES"
        puts "1 #{crr[0]}"
    else
        puts "NO"
    end
end

t = gets.chomp.to_i
		while(t>1)
			n_m = gets.chomp.split(" ")
			n = n_m[0].to_i
			m = n_m[1].to_i
			a_inp = gets.chomp.split(" ")
			a = a_inp.map{|i| i = i.to_i}
			b_inp = gets.chomp.split(" ")
			b = b_inp.map{|i| i = i.to_i}
			c = []
			c = a & b
			unless(c.count>0)
				puts "NO"
			else
				puts "YES"
				puts "#{c.count} #{c.join(' ')}"
			end
			t = t-1
		end

t = gets.chomp.to_i
while(t>1)
	n_m = gets.chomp.split(" ")
	n = n_m[0].to_i
	m = n_m[1].to_i
	a_inp = gets.chomp.split(" ")
	a = a_inp.map{|i| i = i.to_i}
	b_inp = gets.chomp.split(" ")
	b = b_inp.map{|i| i = i.to_i}
	c = []
	c = a & b
	unless(c.count>0)
		puts "NO"
	else
		puts "YES"
		puts "1 #{c[0]}"
	end
	t = t-1
end

$, = "\n"
def read
    gets.split.map(&:to_i)
end
gets.to_i.times do
    n, m = read
    a = read
    b = read
    res = nil
    a.each do |i|
        b.each do |j|
            res = i if i == j
        end
    end
    if res.nil?
        puts "NO"
    else
        print "YES", 1, res
    end
end

tc = gets.to_i

tc.times do
    n,m = gets.split().map(&:to_i)
    arr = gets.split().map(&:to_i)
    brr = gets.split().map(&:to_i)
    crr = arr & brr
    if(crr.size > 0)
        puts "YES"
        puts crr[0]
    else
        puts "NO"
    end
end


You are given two sequences $$$a_1, a_2, \dots, a_n$$$ and $$$b_1, b_2, \dots, b_n$$$. Each element of both sequences is either $$$0$$$, $$$1$$$ or $$$2$$$. The number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$a$$$ is $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ respectively, and the number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$b$$$ is $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ respectively.You can rearrange the elements in both sequences $$$a$$$ and $$$b$$$ however you like. After that, let's define a sequence $$$c$$$ as follows:$$$c_i = \begin{cases} a_i b_i & \mbox{if }a_i > b_i \\ 0 & \mbox{if }a_i = b_i \\ -a_i b_i & \mbox{if }a_i < b_i \end{cases}$$$You'd like to make $$$\sum_{i=1}^n c_i$$$ (the sum of all elements of the sequence $$$c$$$) as large as possible. What is the maximum possible sum?

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ ($$$0 \le x_1, y_1, z_1 \le 10^8$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$a$$$. The second line of each test case also contains three integers $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ ($$$0 \le x_2, y_2, z_2 \le 10^8$$$; $$$x_1 + y_1 + z_1 = x_2 + y_2 + z_2 > 0$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$b$$$.

For each test case, print the maximum possible sum of the sequence $$$c$$$.

t =gets.to_i

t.times do
    a0,a1,a2 = gets.split.map(&:to_i)
    b0,b1,b2 = gets.split.map(&:to_i)
    mk = [a2,b1].min.clone
    ans = 2*mk
    a2 -= mk
    b1 -= mk
    mk = [a0,b2].min.clone
    a0-=mk
    b2-=mk
    if b2>0 then
        mk = [a2,b2].min.clone
        a2-=mk
        b2-=mk
    end
    ans -= 2*b2
    puts ans
end

def solve
  x1, y1, z1 = gets.strip.split(' ').map(&:to_i)
  x2, y2, z2 = gets.strip.split(' ').map(&:to_i)
  res = 1
  mn1 = [z1, y2].min #(2, 1)
  z1 -= mn1
  y2 -= mn1
  mn2 = [x1, z2].min #(0, 2)
  z2 -= mn2
  x1 -= mn2
  mn4 = [z1, z2].min #(2, 2)
  z1 -= mn4
  z2 -= mn4
  res *= (2 * mn1)
  res = res - (2 * z2) if !z2.zero?
  puts res
end

gets.strip.to_i.times { solve }

t = gets.chomp.to_i
t.times {
  x1, y1, z1 = gets.chomp.split(' ').map(&:to_i)
  x2, y2, z2 = gets.chomp.split(' ').map(&:to_i)
  ans = 0
  min = [y1, x2].min
  y1 -= min
  x2 -= min

  min = [x1, z2].min
  x1 -= min
  z2 -= min

  ans = ans + 2 * [z1, y2].min
  min = [z1, y2].min
  z1 -= min
  y2 -= min

  min = [z1, z2].min
  z1 -= min
  z2 -= min

  min = [y1, y2].min
  y1 -= min
  y2 -= min

  ans = ans +  -2 * [y1, z2].min
  puts ans
}

t = gets.to_i
ans = []
t.times do
  x = gets.chomp.split(" ").map(&:to_i)
  y = gets.chomp.split(" ").map(&:to_i)
  sum = 0
  num2 = [x[2],y[1]].min
  x[2] -= num2
  y[1] -= num2
  sum += num2 * 2
  minus = [y[2],x[0]].min
  y[2] -= minus
  x[0] -= minus
  minus = [y[2],x[2]].min
  y[2] -= minus
  x[2] -= minus
  num2 = [x[1],y[2]].min
  sum -= num2 * 2
  ans << sum
end
ans.each do |x|
  puts x
end

t = gets.chomp.to_i

while (t-=1) >= 0
  a, b, c = gets.chomp.split(" ").map(&:to_i)
  z, x, y = gets.chomp.split(" ").map(&:to_i)
  minCX = [c, x].min
  puts (minCX - [0, y - a - c + minCX].max)*2
end

require 'set'

def taskA
  a, b = gets.chomp.split(" ").map(&:to_i)
  puts b > a ? b - a : (a + b) % 2
end

def taskB
  a, b, c = gets.chomp.split(" ").map(&:to_i)
  z, x, y = gets.chomp.split(" ").map(&:to_i)
  minCX = [c, x].min
  puts (minCX - [0, y - a - c + minCX].max)*2
end

t = gets.chomp.to_i
while (t-=1) >= 0
  taskB
end

def solve
  x1, y1, z1 = gets.strip.split(' ').map(&:to_i)
  x2, y2, z2 = gets.strip.split(' ').map(&:to_i)
  res = 1
  mn1 = [z1, y2].min #(2, 1)
  z1 -= mn1
  y2 -= mn1
  mn2 = [x1, z2].min #(0, 2)
  z2 -= mn2
  x1 -= mn2
  mn3 = [y1, y2].min #(1, 1)
  y1 -= mn3
  y2 -= mn3
  mn4 = [z1, z2].min #(2, 2)
  z1 -= mn4
  z2 -= mn4
  res *= (2 * mn1)
  res = res - (z2 * y1) if !z2.zero?
  puts res
end

gets.strip.to_i.times { solve }

t = gets.chomp.to_i
t.times {
  x1, y1, z1 = gets.chomp.split(' ').map(&:to_i)
  x2, y2, z2  = gets.chomp.split(' ').map(&:to_i)
  ans = 0
  ans = ans + 2 * [z1, y2].min

  z1 -= [z1, y2].min
  y2 -= [z1, y2].min

  y1 -= [y1, x2].min
  x2 -= [y1, x2].min

  x1 = x1 - [x1, z2].min
  z2 = z2 - [x1, z2].min

  ans = ans +  -2 * [y1, z2].min
  puts ans
}

t = gets.chomp.to_i
t.times {
  x1, y1, z1 = gets.chomp.split(' ').map(&:to_i)
  x2, y2, z2  = gets.chomp.split(' ').map(&:to_i)
  puts (2 * [z1, y2].min)
}

# t = gets.chomp.to_i
# t.times {
#   n, k = gets.chomp.split(' ').map(&:to_i)
#   arr = gets.chomp.split(' ').map(&:to_i)
#   max = arr.max
#   arr.map! { |ele| max - ele }
#   max = arr.max
#   if k % 2 == 0
#     arr.map! { |ele| max - ele }
#   end
#   arr.each {|ele| print '%d ' % ele }
#   print '\n'
# }

t = gets.chomp.to_i
t.times {
  x1, y1, z1 = gets.chomp.split(' ').map(&:to_i)
  x2, y2, z2  = gets.chomp.split(' ').map(&:to_i)
  ans = 0
  ans = ans + 2 * [z1, y2].min

  z1 -= [z1, y2].min
  y2 -= [z1, y2].min

  y1 -= [y1, x2].min
  x2 -= [y1, x2].min

  x1 -= [x1, z2].min
  z2 -= [x1, z2].min

  y1 -= [y1, y2].min
  y2 -= [y1, y2].min

  ans = ans +  -2 * [y1, z2].min
  puts ans
}

t = gets.chomp.to_i
t.times {
  x1, y1, z1 = gets.chomp.split(' ').map(&:to_i)
  x2, y2, z2  = gets.chomp.split(' ').map(&:to_i)
  ans = 0
  y1 -= [y1, x2].min
  x2 -= [y1, x2].min

  x1 -= [x1, z2].min
  z2 -= [x1, z2].min

  ans = ans + 2 * [z1, y2].min

  z1 -= [z1, y2].min
  y2 -= [z1, y2].min

  z1 -= [z1, z2].min
  z2 -= [z1, z2].min

  y1 -= [y1, y2].min
  y2 -= [y1, y2].min

  ans = ans +  -2 * [y1, z2].min
  puts ans
}

t = gets.chomp.to_i
t.times {
  x1, y1, z1 = gets.chomp.split(' ').map(&:to_i)
  x2, y2, z2  = gets.chomp.split(' ').map(&:to_i)
  ans = 0
  ans = ans + 2 * [z1, y2].min

  z1 -= [z1, y2].min
  y2 -= [z1, y2].min

  y1 -= [y1, x2].min
  x2 -= [y1, x2].min

  x1 = x1 - [x1, z2].min
  z2 = z2 - [x1, z2].min

  x1 -= [x1, z2].min
  z2 -= [x1, z2].min

  ans = ans +  -2 * [y1, z2].min
  puts ans
}


You are given an array of $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$. You can make the following operation: choose an integer $$$x \geq 2$$$ and replace each number of the array by the remainder when dividing that number by $$$x$$$, that is, for all $$$1 \leq i \leq n$$$ set $$$a_i$$$ to $$$a_i \bmod x$$$.Determine if it is possible to make all the elements of the array equal by applying the operation zero or more times.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \leq a_i \leq 10^9$$$) where $$$a_i$$$ is the $$$i$$$-th element of the array. The sum of $$$n$$$ for all test cases is at most $$$2 \cdot 10^5$$$.

For each test case, print a line with YES if you can make all elements of the list equal by applying the operation. Otherwise, print NO. You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be recognized as a positive answer).

NoteIn the first test case, one can apply the operation with $$$x = 3$$$ to obtain the array $$$[2, 2, 0, 2]$$$, and then apply the operation with $$$x = 2$$$ to obtain $$$[0, 0, 0, 0]$$$.In the second test case, all numbers are already equal.In the fourth test case, applying the operation with $$$x = 4$$$ results in the array $$$[1, 1, 1, 1]$$$.

gets.to_i.times do
  gets
  as = gets.split.map(&:to_i).sort
  ok = as.include?(1) ? as.each_cons(2).none? { |a, b| b - a == 1 } : true
  puts ok ? "YES" : "NO"
end

gets.to_i.times do
  gets
  as = gets.split.map(&:to_i)
  ok = as.include?(1) ? as.all?(&:odd?) : true
  puts ok ? "YES" : "NO"
end


Mike and some bears are playing a game just for fun. Mike is the judge. All bears except Mike are standing in an n × m grid, there's exactly one bear in each cell. We denote the bear standing in column number j of row number i by (i, j). Mike's hands are on his ears (since he's the judge) and each bear standing in the grid has hands either on his mouth or his eyes.  They play for q rounds. In each round, Mike chooses a bear (i, j) and tells him to change his state i. e. if his hands are on his mouth, then he'll put his hands on his eyes or he'll put his hands on his mouth otherwise. After that, Mike wants to know the score of the bears.Score of the bears is the maximum over all rows of number of consecutive bears with hands on their eyes in that row.Since bears are lazy, Mike asked you for help. For each round, tell him the score of these bears after changing the state of a bear selected in that round.

The first line of input contains three integers n, m and q (1 ≤ n, m ≤ 500 and 1 ≤ q ≤ 5000). The next n lines contain the grid description. There are m integers separated by spaces in each line. Each of these numbers is either 0 (for mouth) or 1 (for eyes). The next q lines contain the information about the rounds. Each of them contains two integers i and j (1 ≤ i ≤ n and 1 ≤ j ≤ m), the row number and the column number of the bear changing his state.

After each round, print the current score of the bears.

a=gets.chomp.split(" ").map {|i|  i.to_i   }
ar=[]
on=[]
a[0].times do
temp=gets.chomp.split(" ")
max=0
t=0
temp.each do |i|
if i=="1"
t+=1
else
if t>max
max=t
end
t=0
end
end
if t>max
max=t
end
on << max
ar << temp
end

a[2].times do
temp=gets.chomp.split(" ").map {|i| i.to_i}
if ar[temp[0]-1][temp[1]-1]=="1"
ar[temp[0]-1][temp[1]-1]="0"
else
ar[temp[0]-1][temp[1]-1]="1"
end
max=0
t=0
ar[temp[0]-1].each do |i|
if i=="1"
t+=1
else
if t>max
max=t
end
t=0
end
end
if t>max
max=t
end
on[temp[0]-1]=max
puts "#{on.max}"
end

def row_sum(arr)
  sum = 0
  total = 0
  for i in 0...arr.size
    if arr[i] == 0
      sum = 0
    else
      sum += 1
    end
    total = [sum,total].max
  end
  total
end

input = gets.chomp.split(" ")
n , m , q = input[0].to_i, input[1].to_i, input[2].to_i
scores = []

grid = []
for i in 0...n
  #read each row
  grid[i] = gets.chomp.split(" ").collect{|bear| bear.to_i}
  sum = row_sum(grid[i])
  #puts "Sum is #{sum}"
  scores[i] = sum
end

solution = []
for i in 0...q
  move = gets.chomp.split(' ').collect{|num| num.to_i-1}
  if grid[move[0]][move[1]] == 1
    grid[move[0]][move[1]] = 0
  else
    grid[move[0]][move[1]] = 1
  end
  scores[move[0]] = row_sum(grid[move[0]])
  solution << "#{scores.max}"
end
puts solution

n, m, q = gets.split.map(&:to_i)
a = n.times.map{gets.split.map(&:to_i)}
c = Array.new(n, 0)
n.times do |i|
  j = 0
  while j < m
    if a[i][j] == 1
      t = 0
      j.upto(m) do |k|
        if a[i][k] == 1
          t += 1
        else
          j = k + 1
          c[i] = [c[i], t].max
          break
        end
      end
    else
      j += 1
    end
  end
end
q.times do
  i, j = gets.split.map(&:to_i).map{|x| x -= 1}
  a[i][j] ^= 1
  cc = 0
  y = 0
  while y < m
    if a[i][y] == 1
      t = 0
      y.upto(m) do |k|
        if a[i][k] == 1
          t += 1
        else
          y = k + 1
          cc = [cc, t].max
          break
        end
      end
    else
      y += 1
    end
  end
  c[i] = cc
  puts c.max
end

def getMax(row)
	startNum = 0
	currentMax = 0
	nowIndex = row.index(1)

	if nowIndex.nil?
		return 0
	end

	if nowIndex == row.length
		return 1
	end

	#puts "Rindex : #{endIndex}"
	until nowIndex == row.length
		startNum += 1 if row[nowIndex] == 1
		currentMax = startNum if currentMax < startNum

		startNum = 0 if row[nowIndex] == 0

		nowIndex += 1
	end

	return currentMax
end

input = gets.chomp.split(' ').map {|item| item.to_i}
bears = []
result = []
maxArray = []

for i in 0...input[0]
	column = gets.chomp.split(' ').map {|item| item.to_i}
	bears << column
end

bears.each do |row|
		maxArray << getMax(row)
end

for i in 0...input[2]
	queryInput = gets.chomp.split(' ').map {|item| item.to_i}

	bears[queryInput[0] - 1][queryInput[1] - 1] -= 1
	bears[queryInput[0] - 1][queryInput[1] - 1] = 1 if bears[queryInput[0] - 1][queryInput[1] - 1] < 0

	maxArray[queryInput[0] - 1] = getMax(bears[queryInput[0] - 1])

	result << maxArray.max
end

result.each do |item|
	puts item
end

def row_sum(arr)
  sum = 0
  chunks = []
  arr.chunk{|num| num==1}.each{|val|  chunks << val}
  chunks.each do |chk|
    if chk[0] == true
      sum = [sum, chk[1].size].max
    end
  end
  sum
end

input = gets.chomp.split(" ")
n , m , q = input[0].to_i, input[1].to_i, input[2].to_i

grid = []
max_score = 0
max_row = 0
for i in 0...n
  #read each row
  grid[i] = gets.chomp.split(" ").collect{|bear| bear.to_i}
  sum = row_sum(grid[i])
  #puts "Sum is #{sum}"
  if sum > max_score
    max_score = sum
    max_row = i
  end
end

solution = []
for i in 0...q
  move = gets.chomp.split(' ').collect{|num| num.to_i-1}
  if grid[move[0]][move[1]] == 1
    grid[move[0]][move[1]] = 0
  else
    grid[move[0]][move[1]] = 1
  end
  #if the move[0] row is bigger than current update it
  sum = row_sum(grid[move[0]])
  #puts "Move #{move[0]},#{move[1]} changed sum to #{sum}"
  #puts "\tOld Max score is #{max_score}, max row is #{max_row}"
  if sum > max_score || move[0] == max_row
    max_score = sum
    max_row = move[0]
  end
  #puts "\tNew Max score is #{max_score}, max row is #{max_row}"

  solution << "#{max_score}"
end
puts solution

n, m, q = gets.split.map(&:to_i)
a = n.times.map{gets.split.map(&:to_i)}
q.times do
  i, j = gets.split.map(&:to_i)
  a[i - 1][j - 1] ^= 1
  mx = 0
  a.each do |x|
    m.times do |k|
      if x[k] == 1
        c = (x[k...m].index(0) || m - k)
        mx = [mx, c].max
        break
      end
    end
  end
  puts mx
end

n, m, q = gets.split.map(&:to_i)
a = n.times.map{gets.split.map(&:to_i)}
q.times do
  i, j = gets.split.map(&:to_i)
  a[i - 1][j - 1] ^= 1
  puts a.map{|x| x.inject(:+)}.max
end


There are $$$n$$$ students standing in a circle in some order. The index of the $$$i$$$-th student is $$$p_i$$$. It is guaranteed that all indices of students are distinct integers from $$$1$$$ to $$$n$$$ (i. e. they form a permutation).Students want to start a round dance. A clockwise round dance can be started if the student $$$2$$$ comes right after the student $$$1$$$ in clockwise order (there are no students between them), the student $$$3$$$ comes right after the student $$$2$$$ in clockwise order, and so on, and the student $$$n$$$ comes right after the student $$$n - 1$$$ in clockwise order. A counterclockwise round dance is almost the same thing — the only difference is that the student $$$i$$$ should be right after the student $$$i - 1$$$ in counterclockwise order (this condition should be met for every $$$i$$$ from $$$2$$$ to $$$n$$$). For example, if the indices of students listed in clockwise order are $$$[2, 3, 4, 5, 1]$$$, then they can start a clockwise round dance. If the students have indices $$$[3, 2, 1, 4]$$$ in clockwise order, then they can start a counterclockwise round dance.Your task is to determine whether it is possible to start a round dance. Note that the students cannot change their positions before starting the dance; they cannot swap or leave the circle, and no other student can enter the circle. You have to answer $$$q$$$ independent queries.

The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of students. The second line of the query contains a permutation of indices $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$), where $$$p_i$$$ is the index of the $$$i$$$-th student (in clockwise order). It is guaranteed that all $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i. e. they form a permutation).

For each query, print the answer on it. If a round dance can be started with the given order of students, print "YES". Otherwise print "NO".

gets.strip.to_i.times do
  n = gets.strip.to_i
  a = gets.strip.split(' ').map(&:to_i)
  sorted = a.dup.sort
  res = false
  n.times do |i|
    rot = a.rotate(i)
    if sorted == rot || sorted.reverse == rot
      puts "YES"
      res = true
      break
    end
  end
  puts "NO" unless res
end

gets.to_i.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  s = a.dup.sort
  ok = false
  (0...n).each do |i|
    r = a.rotate i
    if s == r || s.reverse == r
      puts "YES"
      ok = true
      break
    end
  end
  puts "NO" unless ok
end

q = gets.to_i
ans = []
q.times do
  n = gets.to_i
  pp = gets.split.map(&:to_i)

  def_a = (1..n).to_a
  diff_a = pp.map.with_index{|num,i|num-def_a[i]}
  diff_a2 = pp.map.with_index{|num,i|num-def_a[n-1-i]}
  can = 2>=diff_a.uniq.size || 2>=diff_a2.uniq.size

  ans << (can ? 'YES' : 'NO')
end
puts ans

#!/usr/bin/env ruby

q = Integer(gets)

results = Array.new
q.times do

	n = Integer(gets)

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

	fail = false
	(n-1).times do |index|
		if (arr[index] - arr[index+1]).abs >1 && (arr[index] - arr[index+1]).abs != (n-1)
			fail = true
			results << "NO"
			break
		end
	end

	results << "YES" if !fail
end

results.each do |result|
	puts result
end

t = gets.to_i
t.times do |index|
    n = gets.to_i
    a = gets.split(" ").map(&:to_i)
    c = true
    cc = true
    (n - 1).times do |i|
        # p a[i].to_s+" "+a[i+1].to_s+" "+i.to_s+" "+index.to_s
        if !((a[i] == a[i+1] + 1) || (a[i] == 1 && a[i+1] == n))
            # p "false c"
            cc = false
        end
        if !((a[i] + 1 == a[i+1]) || (a[i] == n && a[i+1] == 1))
            # p "false cc"
            c = false
        end
    end
    puts (c || cc) ? "YES" : "NO"
end

q = gets.to_i
ans = []
q.times do
  n = gets.to_i
  pp = gets.split.map(&:to_i)

  def_a = (1..n).to_a
  diff_a = pp.map.with_index{|num,i|num-def_a[i]}
  diff_a2 = pp.map.with_index{|num,i|num-def_a[n-1-i]}
  can = n==diff_a.uniq.size || n==diff_a2.uniq.size

  ans << (can ? 'YES' : 'NO')
end
puts ans

q = gets.to_i
ans = []
q.times do
  n = gets.to_i
  pp = gets.split.map(&:to_i)

  min_i = pp.index(1)
  max_i = pp.index(n)
  dist = (min_i-max_i).abs
  can = false

  if dist==0
    can = true
  elsif dist == 1
    left_idx = min_i<max_i ? min_i : max_i
    la = pp[0..left_idx]
    ra = pp[(left_idx+1)..-1]
    can = (la==la.sort&&ra==ra.sort)||(la==la.sort{|a,b|b<=>a}&&ra==ra.sort{|a,b|b<=>a})
  else
    can = pp==pp.sort || pp==pp.sort{|a,b|b<=>a}
  end

  ans << (can ? 'YES' : 'NO')
end
puts ans

#!/usr/bin/env ruby

q = Integer(gets)

results = Array.new
q.times do

	n = Integer(gets)

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

	fail = false
	(n-1).times do |index|
		if (arr[index] - arr[index+1]).abs >1 && !(arr[index] ==1 && arr[index+1]== n)
			fail = true
			results << "NO"
			break
		end
	end

	results << "YES" if !fail
end

results.each do |result|
	puts result
end

t = gets.to_i
t.times do |index|
    n = gets.to_i
    a = gets.split(" ").map(&:to_i)
    c = true
    cc = true
    (n - 1).times do |i|
        # p a[i].to_s+" "+a[i+1].to_s+" "+i.to_s+" "+index.to_s
        if !((a[i] == a[i+1] + 1) || (a[i] == 1 && a[i+1] == n))
            # p "false c"
            cc = false
        end
        if !((a[i] + 1 == a[i+1]) || (a[i] == n && a[i+1] == 1))
            # p "false cc"
            c = false
        end
    end
    p (c || cc) ? "YES" : "NO"
end

t = gets.to_i
t.times do |index|
    n = gets.to_i
    a = gets.split(" ").map(&:to_i)
    c = true
    cc = true
    (n - 1).times do |i|
        # p a[i].to_s+" "+a[i+1].to_s+" "+i.to_s+" "+index.to_s
        if (a[i] != a[i+1] + 1) && !(a[i] == 1 && a[i+1] == n)
            # p "false c"
            c = false
        end
        if (a[i] + 1 != a[i+1]) && !(a[i] == n && a[i+1] == 1)
            # p "false cc"
            cc = false
        end
    end
    p (c || cc) ? "YES" : "NO"
end


Let $$$s$$$ be a string of lowercase Latin letters. Its price is the sum of the indices of letters (an integer between 1 and 26) that are included in it. For example, the price of the string abca is $$$1+2+3+1=7$$$.The string $$$w$$$ and the integer $$$p$$$ are given. Remove the minimal number of letters from $$$w$$$ so that its price becomes less than or equal to $$$p$$$ and print the resulting string. Note that the resulting string may be empty. You can delete arbitrary letters, they do not have to go in a row. If the price of a given string $$$w$$$ is less than or equal to $$$p$$$, then nothing needs to be deleted and $$$w$$$ must be output.Note that when you delete a letter from $$$w$$$, the order of the remaining letters is preserved. For example, if you delete the letter e from the string test, you get tst.

The first line of input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The following are descriptions of $$$t$$$ test cases. Each case consists of two lines. The first of them is the string $$$w$$$, it is non-empty and consists of lowercase Latin letters. Its length does not exceed $$$2\cdot10^5$$$. The second line contains an integer $$$p$$$ ($$$1 \le p \le 5\,200\,000$$$). It is guaranteed that the sum of string lengths $$$w$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

Output exactly $$$t$$$ rows, the $$$i$$$-th of them should contain the answer to the $$$i$$$-th set of input data. Print the longest string that is obtained from $$$w$$$ by deleting letters such that its price is less or equal to $$$p$$$. If there are several answers, then output any of them. Note that the empty string  — is one of the possible answers. In this case, just output an empty string.

5

abca

2

abca

6

codeforces

1

codeforces

10

codeforces

100

inputs = STDIN.read.strip.split("\n")
inputs.shift

inputs.each_slice(2) do |inp|
  chars = inp[0].split('')
  t     = inp[1].to_i
  cur   = 0
  res   = ''

  list = chars.each_with_index.map { |x, i| [x, x.bytes.first, i] }
  list = list.sort{|a, b| a[1] <=> b[1] }
  ok   = {}

  cur = 0
  list.each do |row|
    pos = row[1] - 96
    break if cur + pos > t

    cur += pos
    ok[ row[2] ] = true
  end

  chars.each_with_index do |char, index|
    next if !ok[index]

    print char
  end

  print "\n"
end

def solve(str, cost)
  # greedy solution, delete most expensive chars (lexicographically sized)
  hash = Hash.new { |h,k| h[k] = [] }
  str.split('').each_with_index do |ch, i|
    hash[ch] << i
  end

  chars = hash.keys.sort

  chars.each do |ch|
    cost_of_char = ch.ord - 'a'.ord + 1
    # max number of chars allowed in str is cost / cost_of_char
    max_num = cost / cost_of_char
    hash[ch] = hash[ch].take(max_num)
    cost -= hash[ch].length * cost_of_char
  end

  ans = []
  hash.each do |ch, indices|
    indices.each do |i|
      ans[i] = ch
    end
  end
  ans.join('')
end

cases = gets.to_i
cases.times do
  puts solve(gets.chomp, gets.to_i)
end

n = gets.to_i

n.times do
  s = gets.strip
  p = gets.to_i

  hashmap = Hash.new(0)

  alphabet = ('a'..'z').to_a
  costs = Hash[('a'..'z').each.with_index(1).to_a]

  total_cost = 0

  s.each_char do |ch|
    hashmap[ch] += 1
    total_cost += costs[ch]
  end

  index = alphabet.size - 1
  while (total_cost > p && index > -1)
    count = hashmap[alphabet[index]]
    if count > 0
      hashmap[alphabet[index]] -= 1
      total_cost -= index + 1
    else
      index -= 1
    end
  end

  s.each_char do |char|
    next if hashmap[char] == 0

    hashmap[char] -= 1
    print(char)
  end

  puts
end

inputs = STDIN.read.strip.split("\n")
inputs.shift

inputs.each_slice(2) do |inp|
  t = inp[1].to_i

  cur = 0
  res = ''
  inp[0].bytes.each do |char|
    pos = char - 96
    next if cur + pos > t

    cur += pos
    res += char.chr
  end

  puts res
end


Nauuo is a girl who loves playing cards.One day she was playing cards but found that the cards were mixed with some empty ones.There are $$$n$$$ cards numbered from $$$1$$$ to $$$n$$$, and they were mixed with another $$$n$$$ empty cards. She piled up the $$$2n$$$ cards and drew $$$n$$$ of them. The $$$n$$$ cards in Nauuo's hands are given. The remaining $$$n$$$ cards in the pile are also given in the order from top to bottom.In one operation she can choose a card in her hands and play it — put it at the bottom of the pile, then draw the top card from the pile.Nauuo wants to make the $$$n$$$ numbered cards piled up in increasing order (the $$$i$$$-th card in the pile from top to bottom is the card $$$i$$$) as quickly as possible. Can you tell her the minimum number of operations?

The first line contains a single integer $$$n$$$ ($$$1\le n\le 2\cdot 10^5$$$) — the number of numbered cards. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\le a_i\le n$$$) — the initial cards in Nauuo's hands. $$$0$$$ represents an empty card. The third line contains $$$n$$$ integers $$$b_1,b_2,\ldots,b_n$$$ ($$$0\le b_i\le n$$$) — the initial cards in the pile, given in order from top to bottom. $$$0$$$ represents an empty card. It is guaranteed that each number from $$$1$$$ to $$$n$$$ appears exactly once, either in $$$a_{1..n}$$$ or $$$b_{1..n}$$$.

The output contains a single integer — the minimum number of operations to make the $$$n$$$ numbered cards piled up in increasing order.

NoteExample 1We can play the card $$$2$$$ and draw the card $$$3$$$ in the first operation. After that, we have $$$[0,3,0]$$$ in hands and the cards in the pile are $$$[0,1,2]$$$ from top to bottom.Then, we play the card $$$3$$$ in the second operation. The cards in the pile are $$$[1,2,3]$$$, in which the cards are piled up in increasing order.Example 2Play an empty card and draw the card $$$1$$$, then play $$$1$$$, $$$2$$$, $$$3$$$ in order.

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

r = n-1
if i = b.index(1)
  while i < n-1 && b[i+1]-b[i]==1
    i += 1
  end
  if i==n-1
    k = 0
    l = b.index(1)
    while k < l
      if b[k] == 0 || b[k] > b[-1] + k + 1
        k += 1
      else
        break
      end
    end

    if k == l
      p l
      exit
    end
  end
end

ans = n
0.upto(n-1) do |i|
  #p [t,b[i],i+1-t,s]
  if b[i] <= i+1 && b[i] != 0
    tmp = i+1 + n-(b[i]-1)
    ans = tmp if ans < tmp
  end
end
p ans

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

r = n-1
if i = b.index(1)
  while i < n-1 && b[i+1]-b[i]==1
    i += 1
  end
  if i==n-1
    k = 0
    l = b.index(1)
    while k < l
      if b[k] > b[-1] + k + 1
        k += 1
      else
        break
      end
    end

    if k == l
      p l
      exit
    end
  end
end

ans = n
0.upto(n-1) do |i|
  #p [t,b[i],i+1-t,s]
  if b[i] <= i+1 && b[i] != 0
    tmp = i+1 + n-(b[i]-1)
    ans = tmp if ans < tmp
  end
end
p ans

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

r = n-1
if i = b.index(1)
  while i < n-1 && b[i+1]-b[i]==1
    i += 1
  end
  if i==n-1
    if j = a.index(b[-1]+1)
      k = 0
      x = b[-1]+1

      while x <= n
        while b[k] == 0
          k += 1
        end

        if j < n && a[j] == x
          j += 1
        elsif b[k] == x
          k += 1
        else
          break
        end

        x += 1
      end

      if x == n+1
        p b.index(1)
        exit
      end
    elsif b[-1] == n
      p 0
    end
  end
end

ans = n
0.upto(n-1) do |i|
  #p [t,b[i],i+1-t,s]
  if b[i] <= i+1 && b[i] != 0
    tmp = i+1 + n-(b[i]-1)
    ans = tmp if ans < tmp
  end
end
p ans

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

r = n-1
if i = b.index(1)
  while i < n-1 && b[i+1]-b[i]==1
    i += 1
  end
  if i==n-1
    j = a.index(b[-1]+1)
    k = 0
    x = b[-1]+1

    while x <= n
      while b[k] == 0
        k += 1
      end

      if j && j < n && a[j] == x
        j += 1
      elsif b[k] == x
        k += 1
      else
        break
      end

      x += 1
    end

    if x == n+1
      p b.index(1)
      exit
    end
  end
end

ans = n
0.upto(n-1) do |i|
  #p [t,b[i],i+1-t,s]
  if b[i] <= i+1 && b[i] != 0
    tmp = i+1 + n-(b[i]-1)
    ans = tmp if ans < tmp
  end
end
p ans

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

r = n-1
if i = b.index(1)
  while i < n-1 && b[i+1]-b[i]==1
    i += 1
  end
  if i==n-1
    r = n-b[-1]-1
  end
end

i = a.index(n-r) || 0
while i < n-1 && a[i+1]-a[i] <= 1
  i += 1
end
k = a[i]
#p [k,i,r]
if a[-1] == 0 && r != -1
  p n+r+2
  exit
end
if k == n || r == -1
  p r+1
  exit
end
t = b.index(k+1)
s = t-k
#p [t,s]

t.upto(r) do |i|
  #p [t,b[i],i+1-t,s]
  if b[i] <= i+1-t && b[i] != 0
    tmp = i - b[i] + 1
    s = tmp if s < tmp
  end
end
p n+s+1

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

r = n-1
if i = b.index(1)
  while i < n-1 && b[i+1]-b[i]==1
    i += 1
  end
  if i==n-1
    r = n-b[-1]-1
  end
end

i = a.index(n-r) || 0
while i < n-1 && a[i+1]-a[i] <= 1
  i += 1
end
k = a[i]
#p [k,i,r]
if a[-1] == 0 && r != -1
  p n*2
  exit
end
if k == n || r == -1
  p r+1
  exit
end
t = b.index(k+1)
s = t-k
#p [t,s]

t.upto(r) do |i|
#  p [t,b[t],i+1-t]
  if b[i] < i+1-t && b[i] != 0
    tmp = i - b[i] + 1
    s = tmp if s < tmp
  end
end
p n+s+1

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

r = n-1
if i = b.index(1)
  while i < n-1 && b[i+1]-b[i]==1
    i += 1
  end
  if i==n-1
    r = n-b[-1]-1
  end
end

i = a.index(n-r) || 0
while i < n-1 && a[i+1]-a[i] <= 1
  i += 1
end
k = a[i]
#p [k,i,r]
if a[-1] == 0 && r != n-1
  p n+r+2
  exit
end
if k == n || r == -1
  p r+1
  exit
end
t = b.index(k+1)
s = t-k
#p [t,s]

t.upto(r) do |i|
  #p [t,b[i],i+1-t,s]
  if b[i] <= i+1-t && b[i] != 0
    tmp = i - b[i] + 1
    s = tmp if s < tmp
  end
end
p n+s+1

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

r = n-1
if i = b.index(1)
  while i < n-1 && b[i+1]-b[i]==1
    i += 1
  end
  if i==n-1
    if j = a.index(b[-1]+1)
      k = 0
      x = b[-1]+1

      while x <= n
        while b[k] == 0
          k += 1
        end

        if j < n && a[j] == x
          j += 1
        elsif b[k] == x
          k += 1
        else
          break
        end

        x += 1
      end

      if x == n+1
        p b.index(1)
        exit
      end
    elsif b[-1] == n
      p 0
      exit
    end
  end
end

ans = n
0.upto(n-1) do |i|
  #p [t,b[i],i+1-t,s]
  if b[i] <= i+1 && b[i] != 0
    tmp = i+1 + n-(b[i]-1)
    ans = tmp if ans < tmp
  end
end
p ans

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

r = n-1
if i = b.index(1)
  while i < n-1 && b[i+1]-b[i]==1
    i += 1
  end
  if i==n-1
    r = n-b[-1]-1
  end
end

i = a.index(n-r) || 0
while i < n-1 && a[i+1]-a[i] <= 1
  i += 1
end
k = a[i]
#p [k,i,r]
if a[-1] == 0 && r != -1
  p n+r+2
  exit
end
if k == n || r == -1
  p r+1
  exit
end
t = b.index(k+1)
s = t-k
#p [t,s]

t.upto(r) do |i|
#  p [t,b[t],i+1-t]
  if b[i] < i+1-t && b[i] != 0
    tmp = i - b[i] + 1
    s = tmp if s < tmp
  end
end
p n+s+1