DBG = !true
n = gets.to_i
a = gets.split.map{|z| z.to_i}
h = {}
for x in a
  if h.has_key?(x)
    h[x] += 1
  else
    h[x] = 1
  end
end
s4 = {}
s2 = {}
s1 = {}
h.each{ |k,v|
  if v%4 == 0
    s4[k] = v
  elsif v%2 == 0
    if n%2 == 0
      puts "NO"
      exit 0
    end
    s4[k] = v-2 if v>=6
    s2[k] = 2
  else
    if n%2 == 0 || s1.size > 0
      puts "NO"
      exit 0
    end
    if v>=4
      s4[k] = v-v%4
      v = v%4
    end
    if v>=2  # must be 3
      s2[k] = v-1
    end
    s1[k] = 1
  end
}
if n%2 == 1
  m = n/2
  if DBG
    print s4
    puts "- s4"
  end
  sum4 = s4.values.inject(:+)
  sum4 = 0 if sum4.nil?
  if sum4 < 4*m*m
    puts "NO"
    exit 0
  end
end

if DBG
  puts "n #{n}"
  print s4
  puts ""
  print s2
  puts ""
  print s1
  puts ""
end

t = Array.new(n)
t.map!{Array.new(n)}
i = 0
j = 0

m = n/2
if n%2 == 0
  s4.each { |k,v|
    while v>0
      t[i][j] = k
      v -= 4
      s4[k] -= 4
      if j==m-1
        i += 1
        j = 0
      else
        j += 1
      end
    end
  }
  for i in 0...m
    for j in 0...m
      t[2*m-1-i][j] = t[i][j]
      t[i][2*m-1-j] = t[i][j]
      t[2*m-1-i][2*m-1-j] = t[i][j]
    end
  end

else # n%2 == 1
  s4.each { |k,v|
    while v>0 && i<m
      t[i][j] = k
      v -= 4
      s4[k] -= 4
      if j==m-1
        i += 1
        j = 0
      else
        j += 1
      end
    end
  }
  s4.each{|k,v|
    if s2.has_key?(k)
      s2[k] += v
    else
      s2[k] = v
    end
  }
  if DBG
    puts "s2 now"
    print s2
    puts ""
  end

  i = m
  j = 0
  vert = true
  s2.each { |k,v|
    while v>0
      t[i][j] = k
      v -= 2
      if j==m-1
        i = 0
        j = m
        vert = false
      elsif vert
        j += 1
      else
        i += 1
      end
    end
  }

  s1.each { |k,v|
    t[m][m] = k
  }

  if DBG
    puts "after mm"
    for i in 0..m
      for j in 0..m
        print "#{t[i][j] }"
      end
      puts ""
    end
    puts ""
  end

  for i in 0...m
    for j in 0...m
      t[2*m-i][2*m-j] = t[i][j]
      t[i][2*m-j] = t[i][j]
      t[2*m-i][j] = t[i][j]
    end
    t[2*m-i][m] = t[i][m]
    t[m][2*m-i] = t[m][i]
  end
end

puts "YES"
for i in 0...n
  for j in 0...n
    print "#{t[i][j]} "
  end
  puts ""
end

n = gets.to_i
a = gets.split.map(&:to_i)
cnt = a.tally.map { |k, v| [v, k] }

ans = Array.new(n) { [-1] * n }

set = lambda do |i, j|
	count =
		if n.even?
			4
		else
			f1, f2 = i == n / 2, j == n / 2
			if f1 && f2
				1
			elsif !f1 && !f2
				4
			else
				2
			end
		end
	cnt = cnt.sort.reverse
	cnt[0][0] -= count
	puts('NO') + exit if cnt[0][0] < 0
	val = cnt[0][1]
	ans[i][j] = val
	ans[i][~j] = val
	ans[~i][j] = val
	ans[~i][~j] = val
end

if n.even?
	(0...n / 2).each { |i| (0...n / 2).each { |j| set[i, j] } }
else
	(0...n / 2).each { |i| (0...n / 2).each { |j| set[i, j] } }
	(0...n / 2).each do |i|
		set[n / 2, i]
		set[i, n / 2]
	end
	set[n / 2, n / 2]
end
puts 'YES'
puts ans.map { _1.join(' ') }.join("\n")

n = gets.to_i
cnt = gets.split.map(&:to_i).tally.map { [_2, _1] }
ans = Array.new(n) { [-1] * n }

set = lambda do |i, j|
	f1, f2 = n.odd? && i == n / 2, n.odd? && j == n / 2
	cnt.sort!.reverse!
	cnt[0][0] -=
		if f1 && f2
			1
		elsif !f1 && !f2
			4
		else
			2
		end
	puts('NO') + exit if cnt[0][0] < 0
	ans[i][j] = ans[i][~j] = ans[~i][j] = ans[~i][~j] = cnt[0][1]
end

(0...n / 2).each { |i| (0...n / 2).each { |j| set[i, j] } }
if n.odd?
	(0...n / 2).each do |i|
		set[n / 2, i]
		set[i, n / 2]
	end
	set[n / 2, n / 2]
end
puts 'YES', ans.map { _1.join(' ') }.join("\n")

def print_even(n, r4)
  puts "YES"
  h = n/2
  for i in 0..n-1
    for j in 0..n-1
      if i<n/2 && j<n/2
        x = i
        y = j
      end

      if i>=n/2 && j<n/2
        x = i%h
        x = h-x-1
        y = j
      end

      if i<n/2 && j>=n/2
        x = i
        y = j%h
        y = h-y-1
      end

      if i>=n/2 && j>=n/2
        x = i%h
        x = h-x-1
        y = j%h
        y = h-y-1
      end

      print(r4[x*h+y].to_s)
      print(" ") unless j == n-1
    end
    puts
  end
end

def print_odd(n, r4, r2, r1)
  puts "YES"
  c = n/2
  h = n/2

  r2_1 = r2[0..n/2-1]
  r2_2 = r2[n/2..-1]
  for i in 0..n-1
    for j in 0..n-1
      if i == c || j == c
        if i == c && j == c
          print r1
        elsif i == c
          if j < c
            print r2_1[j]
          else
            y = (j-1)%h
            y = h-y-1
            print r2_1[y]
          end

        elsif j == c
          if i < c
            print r2_2[i]
          else
            y = (i-1)%h
            y = h-y-1
            print r2_2[y]
          end
        end
      else

        if i<c && j<c
          x = i
          y = j
        end

        if i>c && j<c
          x = (i-1)%h
          x = h-x-1
          y = j
        end

        if i<c && j>c
          x = i
          y = (j-1)%h
          y = h-y-1
        end

        if i>c && j>c
          x = (i-1)%h
          x = h-x-1
          y = (j-1)%h
          y = h-y-1
        end

        print(r4[x*h+y].to_s)
      end
      print(" ") unless j == n-1
    end
    puts
  end
end

def main()
  n = STDIN.gets.to_i
  aa = STDIN.gets.strip.split.map(&:to_i)
  counts = {}
  aa.each do |a|
    counts[a] ||= 0;
    counts[a] += 1;
  end

  center = nil
  if n%2 == 0
    unless counts.all? { |k, v| v%4==0 }
      return puts "NO"
    end

    r4 = []
    counts.each do |k, v|
      (v/4).times do
        r4.push(k)
      end
    end
    print_even(n, r4)
  else
    # (4*n-1) + 2*(n-1) + 1
    r1 = counts.keys.select { |k| counts[k] % 2 == 1 }
    unless r1.size == 1
      puts "NO"
      return
    end
    r1 = r1[0]
    counts[r1] -= 1

    # すべて偶数
    unless counts.all? { |k, v| v%2==0 }
      return puts "NO"
    end

    r4_num = ((n-1)/2) ** 2
    r4 = []
    r2 = []

    keys = counts.keys.sort_by { |k| counts[k] }.reverse
    idx = 0
    while idx <= keys.length-1 do
      k = keys[idx]
      if counts[k] == 0
        idx+=1
        next
      end

      if r4.length < r4_num
        if counts[k]%4==0
          counts[k] -= 4
          r4.push(k)
          next
        end
      end

      counts[k] -= 2
      r2.push(k)
    end

    if r4.length < r4_num
      puts "NO"
      return
    end
    print_odd(n, r4, r2, r1)
  end
end

main

$n = Integer(gets)
$ans = []
def setans(key, val)
    $ans[key[0]][key[1]] = val
    $ans[$n-key[0]-1][key[1]] = val
    $ans[key[0]][$n-key[1]-1] = val
    $ans[$n-key[0]-1][$n-key[1]-1] = val
end
for i in 1..$n
    $ans.push(Array.new($n))
end
hsh = Hash.new()
gets.split().each do |y|
    x = Integer(y)
    hsh[x] = 0 unless hsh[x]
    hsh[x] += 1
end
fours = []
for i in 0...$n/2
    for j in 0...$n/2
        fours.push([i, j])
    end
end
twos = []
ones = []
if $n % 2 == 1
    for i in 0...$n/2
        twos.push([i, $n/2])
        twos.push([$n/2, i])
    end
    ones.push([$n/2, $n/2])
end
able = true
begin
    hsh.each do |num, cnt|
        if cnt%2 == 1
            setans(ones.pop(), num)
            cnt-=1
        end
        if cnt%4 == 2
            setans(twos.pop(), num)
            cnt-=2
        end
        until cnt == 0 or fours.empty?
            setans(fours.pop(), num)
            cnt-=4
        end
        until cnt == 0 or twos.empty?
            setans(twos.pop(), num)
            cnt-=2
        end
        until cnt == 0 or ones.empty?
            setans(ones.pop(), num)
            cnt-=1
        end
        raise "jizz" if cnt != 0
    end
    puts "YES"
    $ans.each do |x|
        puts x.join(" ")
    end
rescue
    puts "NO"
end

n = gets.to_i
a = gets.split.map(&:to_i)
b = Hash.new(0)
a.each do |i|
    b[i] += 1
end
c = d = 0
b.values.each do |i|
    c += 1 if i.odd?
    d += 1 if i%4 > 1
end
if (n.odd? && (c != 1 || d > n-1)) || (n.even? && (c != 0 || d != 0))
    puts "NO"
    exit
end
ans = Array.new(n).map{Array.new(n,0)}
if n.odd?
    b.keys.each do |k|
        if b[k].odd?
            ans[n/2][n/2] = k
            b[k] -= 1
            break
        end
    end
    for i in 0..n/2-1
        b.keys.each do |k|
            if b[k]%4 == 2
                ans[i][n/2] = ans[n-i-1][n/2] = k
                b[k] -= 2
                break
            end
        end
    end
    for i in 0..n/2-1
        b.keys.each do |k|
            if b[k]%4 == 2
                ans[n/2][i] = ans[n/2][n-i-1] = k
                b[k] -= 2
                break
            end
        end
    end
    for i in 0..n/2-1
        b.keys.each do |k|
            if ans[i][n/2] == 0 && b[k] != 0
                ans[i][n/2] = ans[n-i-1][n/2] = k
                b[k] -= 2
                break
            end
        end
    end
    for i in 0..n/2-1
        b.keys.each do |k|
            if ans[n/2][i] == 0 && b[k] != 0
                ans[n/2][i] = ans[n/2][n-i-1] = k
                b[k] -= 2
                break
            end
        end
    end
end
for i in 0..n/2-1
    for j in 0..n/2-1
        b.keys.each do |k|
            if b[k] != 0
                ans[i][j] = ans[i][n-1-j] = ans[n-1-i][j] = ans[n-1-i][n-1-j] = k
                b[k] -= 4
                break
            end
        end
    end
end
puts "YES"
ans.each do |arr|
    puts arr.join(" ")
end

def print_even(n, r4)
  puts "YES"
  h = n/2
  for i in 0..n-1
    for j in 0..n-1
      if i<n/2 && j<n/2
        x = i
        y = j
      end

      if i>=n/2 && j<n/2
        x = i%h
        x = h-x-1
        y = j
      end

      if i<n/2 && j>=n/2
        x = i
        y = j%h
        y = h-y-1
      end

      if i>=n/2 && j>=n/2
        x = i%h
        x = h-x-1
        y = j%h
        y = h-y-1
      end

      print(r4[x*h+y].to_s)
      print(" ") unless j == n-1
    end
    puts
  end
end

def print_odd(n, r4, r2, r1)
  puts "YES"
  c = n/2
  h = n/2

  r2_1 = r2[0..n/2-1]
  r2_2 = r2[n/2..-1]
  for i in 0..n-1
    for j in 0..n-1
      if i == c || j == c
        if i == c && j == c
          print r1
        elsif i == c
          if j < c
            print r2_1[j]
          else
            y = j%h
            print r2_1[y]
          end

        elsif j == c
          if i < c
            print r2_2[i]
          else
            x = i%h
            print r2_2[x]
          end
        end
      else

        if i<c && j<c
          x = i
          y = j
        end

        if i>c && j<c
          x = (i-1)%h
          x = h-x-1
          y = j
        end

        if i<c && j>c
          x = i
          y = (j-1)%h
          y = h-y-1
        end

        if i>c && j>c
          x = (i-1)%h
          x = h-x-1
          y = (j-1)%h
          y = h-y-1
        end

        print(r4[x*h+y].to_s)
      end
      print(" ") unless j == n-1
    end
    puts
  end
end

def main()
  n = STDIN.gets.to_i
  aa = STDIN.gets.strip.split.map(&:to_i)
  counts = {}
  aa.each do |a|
    counts[a] ||= 0;
    counts[a] += 1;
  end

  center = nil
  if n%2 == 0
    unless counts.all? { |k, v| v%4==0 }
      return puts "NO"
    end

    r4 = []
    counts.each do |k, v|
      (v/4).times do
        r4.push(k)
      end
    end
    print_even(n, r4)
  else
    # (4*n-1) + 2*(n-1) + 1
    r1 = counts.keys.select { |k| counts[k] % 2 == 1 }
    unless r1.size == 1
      puts "NO"
      return
    end
    r1 = r1[0]
    counts[r1] -= 1

    # すべて偶数
    unless counts.all? { |k, v| v%2==0 }
      return puts "NO"
    end

    r4_num = ((n-1)/2) ** 2
    r4 = []
    r2 = []

    keys = counts.keys.sort_by { |k| counts[k] }.reverse
    idx = 0
    while idx <= keys.length-1 do
      k = keys[idx]
      if counts[k] == 0
        idx+=1
        next
      end

      if r4.length < r4_num
        if counts[k]%4==0
          counts[k] -= 4
          r4.push(k)
          next
        end
      end

      counts[k] -= 2
      r2.push(k)
    end

    if r4.length < r4_num
      puts "NO"
      return
    end
    print_odd(n, r4, r2, r1)
  end
end

main

def print_even(n, r4)
  puts "YES"
  h = n/2
  for i in 0..n-1
    for j in 0..n-1
      if i<n/2 && j<n/2
        x = i
        y = j
      end

      if i>=n/2 && j<n/2
        x = i%h
        x = h-x-1
        y = j
      end

      if i<n/2 && j>=n/2
        x = i
        y = j%h
        y = h-y-1
      end

      if i>=n/2 && j>=n/2
        x = i%h
        x = h-x-1
        y = j%h
        y = h-y-1
      end

      print(r4[x*h+y].to_s)
      print(" ") unless j == n-1
    end
    puts
  end
end

def print_odd(n, r4, r2, r1)
  puts "YES"
  c = n/2
  h = n/2
  for i in 0..n-1
    for j in 0..n-1
      if i == c || j == c
        if i == c && j == c
          print r1
        elsif i == c
          if j < c
            print r2[j]
          else
            print r2[n-j-1]
          end

        elsif j == c
          if i < c
            print r2[i]
          else
            print r2[n-i-1]
          end
        end
      else

        if i<c && j<c
          x = i
          y = j
        end

        if i>c && j<c
          x = (i-1)%h
          x = h-x-1
          y = j
        end

        if i<c && j>c
          x = i
          y = (j-1)%h
          y = h-y-1
        end

        if i>c && j>c
          x = (i-1)%h
          x = h-x-1
          y = (j-1)%h
          y = h-y-1
        end

        print(r4[x*h+y].to_s)
      end
      print(" ") unless j == n-1
    end
    puts
  end
end

def main()
  n = STDIN.gets.to_i
  aa = STDIN.gets.strip.split.map(&:to_i)
  counts = {}
  aa.each do |a|
    counts[a] ||= 0;
    counts[a] += 1;
  end

  center = nil
  if n%2 == 0
    unless counts.all? { |k, v| v%4==0 }
      return puts "NO"
    end

    r4 = []
    counts.each do |k, v|
      (v/4).times do
        r4.push(k)
      end
    end
    print_even(n, r4)
  else
    # (4*n-1) + 2*(n-1) + 1
    r1 = counts.keys.select { |k| counts[k] % 4 == 1 }
    unless r1.size == 1
      puts "NO"
      return
    end
    r1 = r1[0]
    counts[r1] -= 1

    # すべて偶数
    unless counts.all? { |k, v| v%2==0 }
      return puts "NO"
    end

    r4_num = ((n-1)/2) ** 2
    r4 = []
    r2 = []

    keys = counts.keys.sort_by { |k| counts[k] }.reverse
    idx = 0
    while idx <= keys.length-1 do
      k = keys[idx]
      if counts[k] == 0
        idx+=1
        next
      end

      if r4.length < r4_num
        if counts[k]%4==0
          counts[k] -= 4
          r4.push(k)
          next
        end
      end

      counts[k] -= 2
      r2.push(k)
    end

    if r4.length < r4_num
      puts "NO"
      return
    end
    print_odd(n, r4, r2, r1)
  end
end

main

def print_even(n, r4)
  puts "YES"
  h = n/2
  for i in 0..n-1
    for j in 0..n-1
      if i<n/2 && j<n/2
        x = i
        y = j
      end

      if i>=n/2 && j<n/2
        x = i%h
        x = h-x-1
        y = j
      end

      if i<n/2 && j>=n/2
        x = i
        y = j%h
        y = h-y-1
      end

      if i>=n/2 && j>=n/2
        x = i%h
        x = h-x-1
        y = j%h
        y = h-y-1
      end

      print(r4[x*h+y].to_s)
      print(" ") unless j == n-1
    end
    puts
  end
end

def print_odd(n, r4, r2, r1)
  puts "YES"
  c = n/2
  h = n/2
  for i in 0..n-1
    for j in 0..n-1
      if i == c || j == c
        if i == c && j == c
          print r1
        elsif i == c
          if j < c
            print r2[j]
          else
            print r2[n-j-1]
          end

        elsif j == c
          if i < c
            print r2[i]
          else
            print r2[n-i-1]
          end
        end
      else

        if i<c && j<c
          x = i
          y = j
        end

        if i>c && j<c
          x = (i-1)%h
          x = h-x-1
          y = j
        end

        if i<c && j>c
          x = i
          y = (j-1)%h
          y = h-y-1
        end

        if i>c && j>c
          x = (i-1)%h
          x = h-x-1
          y = (j-1)%h
          y = h-y-1
        end

        print(r4[x*h+y].to_s)
      end
      print(" ") unless j == n-1
    end
    puts
  end
end

def main()
  n = STDIN.gets.to_i
  aa = STDIN.gets.strip.split.map(&:to_i)
  counts = {}
  aa.each do |a|
    counts[a] ||= 0;
    counts[a] += 1;
  end

  center = nil
  if n%2 == 0
    unless counts.all? { |k, v| v%4==0 }
      return puts "NO"
    end

    r4 = []
    counts.each do |k, v|
      (v/4).times do
        r4.push(k)
      end
    end
    print_even(n, r4)
  else
    # (4*n-1) + 2*(n-1) + 1
    r1 = counts.keys.select { |k| counts[k] % 2 == 1 }
    unless r1.size == 1
      puts "NO"
      return
    end
    r1 = r1[0]
    counts[r1] -= 1

    # すべて偶数
    unless counts.all? { |k, v| v%2==0 }
      return puts "NO"
    end

    r4_num = ((n-1)/2) ** 2
    r4 = []
    r2 = []

    keys = counts.keys.sort_by { |k| counts[k] }.reverse
    idx = 0
    while idx <= keys.length-1 do
      k = keys[idx]
      if counts[k] == 0
        idx+=1
        next
      end

      if r4.length < r4_num
        if counts[k]%4==0
          counts[k] -= 4
          r4.push(k)
          next
        end
      end

      counts[k] -= 2
      r2.push(k)
    end

    if r4.length < r4_num
      puts "NO"
      return
    end
    print_odd(n, r4, r2, r1)
  end
end

main

DBG = !true
n = gets.to_i
a = gets.split.map{|z| z.to_i}
h = {}
for x in a
  if h.has_key?(x)
    h[x] += 1
  else
    h[x] = 1
  end
end
s4 = {}
s2 = {}
s1 = {}
h.each{ |k,v|
  if v%4 == 0
    s4[k] = v
  elsif v%2 == 0
    if n%2 == 0
      puts "NO"
      exit 0
    end
    s4[k] = v-2 if v>=6
    s2[k] = 2
  else
    if n%2 == 0 || s1.size > 0
      puts "NO"
      exit 0
    end
    if v>=4
      s4[k] = v-v%4
      v = v%4
    end
    if v>=2  # must be 3
      s2[k] = v-1
    end
    s1[k] = 1
  end
}
if n%2 == 1
  m = n/2
  print s4 if DBG
  sum4 = s4.values.inject(:+)
  sum4 = 0 if sum4.nil?
  if sum4 < m*m
    puts "NO"
    exit 0
  end
end

if DBG
  puts "n #{n}"
  print s4
  puts ""
  print s2
  puts ""
  print s1
  puts ""
end

t = Array.new(n)
t.map!{Array.new(n)}
i = 0
j = 0

m = n/2
if n%2 == 0
  s4.each { |k,v|
    while v>0
      t[i][j] = k
      v -= 4
      s4[k] -= 4
      if j==m-1
        i += 1
        j = 0
      else
        j += 1
      end
    end
  }
  for i in 0...m
    for j in 0...m
      t[2*m-1-i][j] = t[i][j]
      t[i][2*m-1-j] = t[i][j]
      t[2*m-1-i][2*m-1-j] = t[i][j]
    end
  end

else # n%2 == 1
  s4.each { |k,v|
    while v>0 && i<m
      t[i][j] = k
      v -= 4
      s4[k] -= 4
      if j==m-1
        i += 1
        j = 0
      else
        j += 1
      end
    end
  }
  s4.each{|k,v|
    if s2.has_key?(k)
      s2[k] += v
    else
      s2[k] = v
    end
  }
  if DBG
    puts "s2 now"
    print s2
    puts ""
  end

  i = m
  j = 0
  vert = true
  s2.each { |k,v|
    while v>0
      t[i][j] = k
      v -= 2
      if j==m-1
        i = 0
        j = m
        vert = false
      elsif vert
        j += 1
      else
        i += 1
      end
    end
  }

  s1.each { |k,v|
    t[m][m] = k
  }

  if DBG
    puts "after mm"
    for i in 0..m
      for j in 0..m
        print "#{t[i][j] }"
      end
      puts ""
    end
    puts ""
  end

  for i in 0...m
    for j in 0...m
      t[2*m-i][2*m-j] = t[i][j]
      t[i][2*m-j] = t[i][j]
      t[2*m-i][j] = t[i][j]
    end
    t[2*m-i][m] = t[i][m]
    t[m][2*m-i] = t[m][i]
  end
end

puts "YES"
for i in 0...n
  for j in 0...n
    print "#{t[i][j]} "
  end
  puts ""
end

DBG = !true
n = gets.to_i
a = gets.split.map{|z| z.to_i}
h = {}
for x in a
  if h.has_key?(x)
    h[x] += 1
  else
    h[x] = 1
  end
end
s4 = {}
s2 = {}
s1 = {}
h.each{ |k,v|
  if v%4 == 0
    s4[k] = v
  elsif v%2 == 0
    if n%2 == 0
      puts "NO"
      exit 0
    end
    s4[k] = v-2 if v>=6
    s2[k] = 2
  else
    if n%2 == 0 || s1.size > 0
      puts "NO"
      exit 0
    end
    if v>=4
      s4[k] = v-v%4
      v = v%4
    end
    if v>=2  # must be 3
      s2[k] = v-1
    end
    s1[k] = 1
  end
}

if DBG
  puts "n #{n}"
  print s4
  puts ""
  print s2
  puts ""
  print s1
  puts ""
end

t = Array.new(n)
t.map!{Array.new(n)}
i = 0
j = 0

m = n/2
if n%2 == 0
  s4.each { |k,v|
    while v>0
      t[i][j] = k
      v -= 4
      s4[k] -= 4
      if j==m-1
        i += 1
        j = 0
      else
        j += 1
      end
    end
  }
  for i in 0...m
    for j in 0...m
      t[2*m-1-i][j] = t[i][j]
      t[i][2*m-1-j] = t[i][j]
      t[2*m-1-i][2*m-1-j] = t[i][j]
    end
  end

else # n%2 == 1
  s4.each { |k,v|
    while v>0 && i<m
      t[i][j] = k
      v -= 4
      s4[k] -= 4
      if j==m-1
        i += 1
        j = 0
      else
        j += 1
      end
    end
  }
  s4.each{|k,v|
    if s2.has_key?(k)
      s2[k] += v
    else
      s2[k] = v
    end
  }
  if DBG
    puts "s2 now"
    print s2
    puts ""
  end

  i = m
  j = 0
  vert = true
  s2.each { |k,v|
    while v>0
      t[i][j] = k
      v -= 2
      if j==m-1
        i = 0
        j = m
        vert = false
      elsif vert
        j += 1
      else
        i += 1
      end
    end
  }

  s1.each { |k,v|
    t[m][m] = k
  }

  if DBG
    puts "after mm"
    for i in 0..m
      for j in 0..m
        print "#{t[i][j] }"
      end
      puts ""
    end
    puts ""
  end

  for i in 0...m
    for j in 0...m
      t[2*m-i][2*m-j] = t[i][j]
      t[i][2*m-j] = t[i][j]
      t[2*m-i][j] = t[i][j]
    end
    t[2*m-i][m] = t[i][m]
    t[m][2*m-i] = t[m][i]
  end
end

puts "YES"
for i in 0...n
  for j in 0...n
    print "#{t[i][j]} "
  end
  puts ""
end

n = gets.to_i
a = gets.split.map(&:to_i)
b = Hash.new(0)
a.each do |i|
    b[i] += 1
end
c = 0
b.values.each do |i|
    c += 1 if i%4 != 0
end
if (n.odd? && c != 1) || (n.even? && c != 0)
    puts "NO"
    exit
end
ans = Array.new(n).map{Array.new(n,0)}
if n.odd?
    b.keys.each do |k|
        if b[k].odd?
            ans[n/2][n/2] = k
            b[k] -= 1
            break
        end
    end
    for i in 0..n/2-1
        b.keys.each do |k|
            if b[k] != 0
                ans[i][n/2] = ans[n-i-1][n/2] = ans[n/2][i] = ans[n/2][n-i-1] = k
                b[k] -= 4
                break
            end
        end
    end
end
for i in 0..n/2-1
    for j in 0..n/2-1
        b.keys.each do |k|
            if b[k] != 0
                ans[i][j] = ans[i][n-1-j] = ans[n-1-i][j] = ans[n-1-i][n-1-j] = k
                b[k] -= 4
                break
            end
        end
    end
end
puts "YES"
ans.each do |arr|
    puts arr.join(" ")
end


puts gets.to_i.times.map{gets.split.map(&:to_i)}.any?{|*,a,b| a>=2400 && b>a} ? "YES" : "NO"

n = gets.to_i

n.times do
	s, b, a = gets.split
	bi = b.to_i
	if 2400 <= bi && bi < a.to_i then
		puts "YES"
		exit 0
	end
end

puts "NO"

#!/usr/bin/ruby
puts gets.to_i.times.any?{
	e=gets.split.map(&:to_i)
	2400<=e[1] && e[1]<e[2]
} ? :YES : :NO

def color(n)
  (n >= 2400) ? "red" : "other"
end

n = gets.chomp.to_i
arr = [].tap do |out|
  n.times do
    out << gets.chomp.split
  end
end

puts arr.any? { |n, a, b| color(a.to_i) == "red" && b.to_i > a.to_i } ? "YES" : "NO"

n = gets.to_i
ans = 'NO'
n.times do
  name, b, a = gets.split
  if b.to_i >= 2400 && b.to_i < a.to_i
    ans = 'YES'
  end
end
puts ans

n = gets.strip.to_i
ans = "NO"
n.times do
    line = gets.strip.split(/ /)
    before = line[1].to_i
    after = line[2].to_i
    ans = "YES" if before >= 2400 && after > before
end
puts ans

n = gets.to_i
q = []

(1..n).each do |i|
  a, b, c = gets.split
  b, c = b.to_i, c.to_i
  q << [a,b,c]
end

q.delete_if { |e| e[1] < 2400 || e[1] >= e[2] }

puts q.empty? ? 'NO' : 'YES'

n = gets.to_i
n.times do |i|
    a, b, c = gets.split
    if b.to_i >= 2400 && c.to_i - b.to_i > 0
        puts "YES"
        exit
    end
end
puts "NO"

n = gets.chomp.to_i
f = false
n.times do
  name, before, after = gets.chomp.split
  if 2400 <= before.to_i and before.to_i < after.to_i
    f = true
  end
end

puts f ? "YES" : "NO"

n = gets.chomp.to_i
a = []
count = 0
for i in (0..n-1)
	a << gets.chomp.split
	if a[i][1].to_i >= 2400 && a[i][2].to_i > a[i][1].to_i
		count += 1
	end
end
if count > 0
	print "YES"
else
	print "NO"
end

n = gets.to_i
ans = false
n.times do
  _, s1, s2 = gets.split.map(&:to_i)
  ans = true if s1 >= 2400 && s2 > s1
end
puts ans ? 'YES' : 'NO'

n = gets.to_i

out = 'NO'
for i in 1..n
  user = gets.split
  pre = user[1].to_i
  if pre >= 2400
    if user[2].to_i > pre
      out = 'YES'
    end
  end
end

puts out

n=gets.to_i
ans=false;
n.times{
	buf=gets.split(' ');
	name,before,after = buf[0], buf[1].to_i, buf[2].to_i
	ans=true if before>=2400&&after>before
}
puts ans==true ? "YES" : "NO"

n = gets.to_i
n.times do
  _, before, after = gets.split.map &:to_i
  if 2400 <= before and before < after
    puts 'YES'
    exit
  end
end
puts 'NO'

POINT_VALUE = 2400

time = gets.chomp.to_i

result = false
time.times do |n|
	line = gets.chomp
	_, before, after = line.split(' ').map &:to_i

	result = true if before >= POINT_VALUE and after > before
end

puts result ? 'YES' : 'NO'

good = false

n = gets.chomp.to_i
n.times do
  name, prev_score, after_score = gets.chomp.split
  if(prev_score.to_i >= 2400 && after_score.to_i > prev_score.to_i)
    good = true
    break
  end
end

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

peopleNum = gets.chomp.to_i
peoples = []

peopleNum.times do
    people = gets.chomp.split(' ')
    people[1] = people[1].to_i
    people[2] = people[2].to_i

    peoples << people
end

red = []

peoples.each do |people|
    red << people if people[1] >= 2400 && people[2] > people[1]
end

ans = if red.length > 0
    "YES"
else
    "NO"
end

puts ans

n = gets.chomp.to_i
a = []
for i in (0..n-1)
	a << gets.chomp.split
	count = 0
	if a[i][1].to_i >= 2400 && a[i][2].to_i > a[i][1].to_i
		count += 1
	end
end
if count > 0
	print "YES"
else
	print "NO"
end

n = gets.chomp.to_i
a = []
for i in (0..n-1)
	a << gets.chomp.split
	count = 0
	if a[i][1].to_i >= 2400 && a[i][2].to_i != a[i][1].to_i && a[i][2].to_i >= 2400
		count += 1
	end
end
if count > 0
	print "YES"
else
	print "NO"
end

p gets.to_i.times.map{gets.split.map(&:to_i)}.any?{|*,a,b| a>=2400 && b>a} ? "YES" : "NO"

POINT_VALUE = 2400

time = gets.chomp.to_i

result = false
time.times do |n|
	line = gets.chomp
	_, before, after = line.split(' ').map &:to_i

	result = true if before > POINT_VALUE and after > before
end

puts result ? 'YES' : 'NO'


#!/usr/bin/ruby1.9

n = STDIN.readline.to_i
ka = []
n.times do
	k, a = STDIN.readline.split.collect {|s| s.to_i }
	ka.push([k, a])
end
#ka.sort!

sizes = []
ka.each do |k, a|
	p = (Math.log2(a)/2).ceil
	p = 1 if p == 0
	sizes.push(k+p)
end

puts sizes.sort[n-1]

n = gets.to_i
max = 0;
n.times{
    k,x = gets.split(" ").map(&:to_i)
    d = (Math.log2(Math.sqrt(x)) + k.to_f).ceil
    if ( d == k ) then
        d = d+1
    end
    if( d > max) then
        max = d
    end
}
if max>0 then
puts max
else
puts "0"
end

#!/usr/bin/ruby1.9

n = STDIN.readline.to_i
sizes = []
n.times do
	k, a = STDIN.readline.split.collect {|s| s.to_i }
	p = (Math.log2(a)/2).ceil
	p = 1 if p == 0
	sizes.push(k+p)
end

puts sizes.sort[n-1]

def run
  n = ARGF.readline.to_i

  ret = 0
  ARGF.readlines.each do |line|
    k, a = line.split.map(&:to_i)

    cur = k + [(Math.log(a, 4)).ceil, 1].max
    ret = [cur, ret].max
  end

  puts ret
end

run if $0 == __FILE__

#!/usr/bin/env ruby

n = gets.to_i
p = 0
n.times do
	k, a = gets.split.map(&:to_i)
	if a == 1
		p = [p, k + 1].max
	else
		p = [p, k + Math.log(a, 4).ceil].max
	end
end
puts p

#!/usr/bin/ruby1.9

n = STDIN.readline.to_i
ka = []
n.times do
	k, a = STDIN.readline.split.collect {|s| s.to_i }
	ka.push([k, a])
end
ka.sort!

sizes = []
ka.each do |k, a|
	p = (Math.log2(a)/2).ceil
	sizes.push(k+p)
end

puts sizes.sort[n-1]

n = gets.to_i
max = 0;
n.times{
    k,x = gets.split(" ").map(&:to_i)
    d = (Math.log2(x) + k.to_f).ceil
    if ( d == k ) then
        d = d+1
    end
    if( d > max) then
        max = d
    end
}
if max>0 then
puts max
else
puts "0"
end

n = gets.to_i
max = 0;
n.times{
	k,x = gets.split(" ").map(&:to_i)
	t = ((2**(k))**2) * x
	if( t > max) then
		max = t
	end
}
if max>0 then
puts Math.log2(Math.sqrt(max)).ceil
else
puts "0"
end

n = gets.to_i
max = 0;
n.times{
	k,x = gets.split(" ").map(&:to_i)
	t = ((2**(k))**2) * x
	if( t > max) then
		max = t
	end
}
puts Math.log2(Math.sqrt(max)).ceil

n = gets.to_i
max = 0;
n.times{
    k,x = gets.split(" ").map(&:to_i)
    d = k + Math.log2(x).ceil
    if ( d == k ) then
        d = d+1
    end
    if( d > max) then
        max = d
    end
}
if max>0 then
puts max
else
puts "0"
end

def run
  n = ARGF.readline.to_i

  ret = 0
  ARGF.readlines.each do |line|
    k, a = line.split.map(&:to_i)

    cur = k + [(Math.log(a, 4)).ceil, 1].min
    ret = [cur, ret].max
  end

  puts ret
end

run if $0 == __FILE__


n, d = gets.split.map{|i| i.to_i}

solds = gets.split.map{|i| i.to_i}

res = 0
n.times do |i|
    n.times do |j|
        if (solds[i] - solds[j]).abs <= d then
            res += 1
        end
    end
end

puts res-n

s = gets.chomp
ns,ds = s.split(/ /)
n = ns.to_i
d = ds.to_i
s = gets.chomp
ss = s.split(/ /)

a = Array.new
for i in 0..n-1
	a[i] = ss[i].to_i
end

sum = 0
for i in 0..n-2
	for j in i+1..n-1
		if (a[i]-a[j]).abs <= d
			sum += 2
		end
	end
end
puts sum

n,d=gets.split.map &:to_i
a=gets.split.map &:to_i
ans=0
n.times do |i|
    n.times do |j|
        if i!=j && (a[i]-a[j]).abs<=d
            ans+=1
        end
    end
end
p ans

#!/usr/bin/ruby
d,a=$<.map{|l|l.split.map(&:to_i)}
r=0
a.size.times{|i|(i+1).step(a.size-1){|j|r+=2 if (a[i]-a[j]).abs<=d[1]}}
p r

d=gets.split[1].to_i
ans=0
i=gets.split.map(&:to_i)
i.each{|a|
    i.each{|b|
        ans=ans+(((a-b).abs<=d)?1:0)
    }
}
puts ans-i.length

n, d = gets.split.map(&:to_i)
as = gets.split.map(&:to_i).sort

ans = 0
(0..n-2).each do |i|
  ((i+1)..n-1).each do |j|
    if (as[i] - as[j]).abs <= d
      ans += 1
    end
  end
end

puts ans * 2

ma = gets.split(" ").map{|i| i.to_i}
mas = gets.split(" ").map{|i| i.to_i}
mas = mas.sort
ans = 0
mas.each{|a|
    mas.each{|b|
    	if (a - b).abs <= ma[1]
    		ans = ans + 1
    	end
    }
}
puts ans - ma[0]

def main()
	n,d=gets.chomp.split(" ").map{|i| i.to_i}
	har=gets.chomp.split(" ").map{|i| i.to_i}
	ans=0
	for i in 0...n
		for j in 0...n
			if i==j
				next;
			end
			if (har[i]-har[j]).abs<=d
				ans+=1
			end
		end
	end
	puts ans
end
main()

s = gets.chomp!.split(" ")
n = s[0].to_i
d = s[1].to_i

s = gets.chomp!.split(" ")
mas = Array.new
for i in 1..n
  mas[i] = s[i-1].to_i
end

count = 0
for i in 1..n
  for j in 1..n
    if (i != j && (mas[i] - mas[j]).abs <= d)
      count += 1
    end
  end
end

puts(count)

n=gets(" ").chomp.to_i
d=gets.chomp.to_i
data=gets.chomp.split(/ /)
sol=[]
for i in 0..(n-1)
	sol.push(data[i].to_i)
end
#puts sol
sol=sol.sort{|a,b|
	a<=>b
}
#puts sol

ans=0
for i in 0..(n-1)
	j=1
	while (i+j)<n
		if sol[i+j]-sol[i]<=d then
			ans+=2
		else	break
		end
		j+=1
	end
end
puts ans

# To change this template, choose Tools | Templates
# and open the template in the editor.

cond = gets.split.map{ |s| s.to_i}
n=cond[0]
d=cond[1]
heights = gets.split.map { |s| s.to_i }.sort

if n != heights.length
  puts "ERROR!"
  exit
end
count = 0
heights.each_with_index do |h, i|
  heights[(i+1)..-1].each do |hh|
    if hh-h<=d
      count+=1
    else
      break
    end
  end

end
puts count*2


left,right = gets.split("^")
left.reverse!
sum_arr = []
i = 1
sum = 0
left.each_char do |l|
	sum += l.to_i * i if l != '='
	i += 1
end

sum_arr << sum

i = 1
sum = 0
right.each_char do |r|
	sum += r.to_i * i if r != '='
	i += 1
end
sum_arr << sum
# puts "#{sum_arr}"

if sum_arr[0] == sum_arr[1]
	ans = "balance"
elsif sum_arr[0] > sum_arr[1]
	ans = "left"
else
	ans = "right"
end
puts ans

bar = gets
pivot = bar.index("^")

left_weight = 0
right_weight = 0

(0...bar.length).each do |index|
  piece = bar[index].chr
  if piece =~ /[[:digit:]]/
    if index < pivot
      left_weight += (pivot - index) * piece.to_i
    else
      right_weight += (index - pivot) * piece.to_i
    end
  end
end

if left_weight > right_weight
  puts "left"
elsif right_weight > left_weight
  puts "right"
else
  puts "balance"
end

s=gets.chomp
x=s.index '^'
i=1
z=0
while x-i>=0
  c=s[x-i].to_i
  z+=c*i if c>0
  i+=1
end
i=1
while x+i<s.size
  c=s[x+i].to_i
  z-=c*i if c>0
  i+=1
end
if z>0
  puts 'left'
elsif z==0
  puts 'balance'
else
  puts 'right'
end

puts (->(x){ x.first > x.last ? 'left' : (x.first < x.last ? 'right' : 'balance')}.call(->(x) {[x.first.reverse, x.last] }.call((STDIN.readline.chomp).split('^').map{|p| p.scan(/./)}).map{|x| x.each_with_index.inject(0){|s,p| s = s + p.first.to_i  * (p.last + 1) }}))

a=gets.chomp.split('')
s=0
len=a.length
for i in 0..len-1
	if a[i]=='^'
		break
	end
end
for j in 0..len-1
	if a[j]!='='
		s+=a[j].to_i*(i-j)
	end
end
puts s>0? "left" : s==0? "balance" : "right"

s = gets.chomp
mid = s.index "^"
left = 0
right = 0
(0...mid).each do |i|
    left += (mid - i) * s[i].to_i if s[i] != '='
end
((mid + 1)...s.length).each do |i|
    right += (i - mid) * s[i].to_i if s[i] != '='
end
ans = right <=> left
case ans
when 0
    puts "balance"
when 1
    puts "right"
else
    puts "left"
end

class Weights
  attr_accessor :string, :balance

  def initialize(value)
    raise "Node value shouldn't be nil or empty" if value.nil?
    @string = value

  end

  def balance
    left, right = @string.split("^")
    left = left.reverse

    left_weight = weigh(left)
    right_weight = weigh(right)

    if left_weight > right_weight
      return "left"
    elsif left_weight < right_weight
      return "right"
    else
      return "balance"
    end

  end

  private

  def weigh(input)
    w = 0
    input.split("").each_with_index do |element, index|
      w += element.to_i * (index + 1) unless element == "="
    end
    w
  end

end

str = gets
puts Weights.new(str).balance

s = gets.chomp
fulcrum = s.index('^')
left = 0
right = 0
i = 1
while fulcrum-i >= 0
    if s[fulcrum-i].to_i.to_s == s[fulcrum-i]
        left += i*s[fulcrum-i].to_i
    end
    i += 1
end
i = 1
while fulcrum+i < s.size
    if s[fulcrum+i].to_i.to_s == s[fulcrum+i]
        right += i*s[fulcrum+i].to_i
    end
    i += 1
end
case left <=> right
when 1
    puts 'left'
when -1
    puts 'right'
when 0
    puts 'balance'
end

s = gets.chomp
fulcrum = s.index('^')
left = 0
right = 0
i = 1
while fulcrum-i >= 0
    left += i*s[fulcrum-i].to_i if s[fulcrum-i].to_i.to_s == s[fulcrum-i]
    i += 1
end
i = 1
while fulcrum+i < s.size
    right += i*s[fulcrum+i].to_i if s[fulcrum+i].to_i.to_s == s[fulcrum+i]
    i += 1
end
case left <=> right
when 1
    puts 'left'
when -1
    puts 'right'
when 0
    puts 'balance'
end

s = gets.chomp
mid = s.index "^"
left = []
right = []
(0...mid).each do |i|
    if s[i] != '='
        left << s[i].to_i
    else
        left << 0
    end
end
((mid + 1)...s.length).each do |i|
    if s[i] != '='
        right << s[i].to_i
    else
        right << 0
    end
end
left.delete 0
right.delete 0
ans = 0
if left.length == right.length
    left.length.times do |i|
        l = left[left.length - 1 - i]
        r = right[i]
        ans += r <=> l
    end
else
    ans = left.inject(:+) <=> right.inject(:+)
end
case ans
when 0
    puts "balance"
when 1
    puts "right"
else
    puts "left"
end

s = gets.chomp
mid = s.index "^"
left = 0
right = 0
(0...mid).each do |i|
    left += (mid - 1) * s[i].to_i if s[i] != '='
end
((mid + 1)...s.length).each do |i|
    right += (i - mid) * s[i].to_i if s[i] != '='
end
ans = right <=> left
case ans
when 0
    puts "balance"
when 1
    puts "right"
else
    puts "left"
end

->(x){ x.first > x.last ? 'left' : (x.first < x.last ? 'right' : 'balance')}.call(->(x) {[x.first.reverse, x.last] }.call((STDIN.readline.chomp).split('^').map{|p| p.scan(/./)}).map{|x| x.each_with_index.map{|p,i| [p, i+1]}}.map{|x| x.map{|t|[t.first.to_i, t.last]}}.map{|x| x.inject(0){|s, t| s = s + t.first * t.last }})

puts (->(x){ x.first > x.last ? 'left' : (x.first < x.last ? 'right' : 'balance')}.call(->(x) {[x.first.reverse, x.last] }.call((STDIN.readline.chomp).split('^').map{|p| p.scan(/./)}).map{|x| x.each_with_index.inject(0){|s,p| s = s + p.first.to_i * p.last}}))


N,K = gets.split.map(&:to_i)
bs = []
n = N
while n > 0
  bs << (n&1)
  n >>= 1
end

t = bs.count(1)
if K < t || N < K
  puts "NO"; exit
end

(1...bs.size).reverse_each{ |b|
  if K - t <= bs[b]
    bs[b] -= K-t
    bs[b-1] += 2*(K-t)
    break
  end
  bs[b-1] += 2*bs[b]
  t += bs[b]
  bs[b] = 0
}

puts "YES"
b = 1
puts bs.map.with_index{ |b,i| [1 << i] * b }.flatten.join(" ")

nk = gets.chomp.split(" ").map(&:to_i)
n = nk[0]
k = nk[1]

n = n.to_s(2)

mink = n.count("1")
maxk = 0
len = n.length

n.each_char.with_index do |ch,i|
  pos = len-i-1
  maxk += 2**pos if ch == "1"
end

if k < mink or k > maxk
  puts "NO"
  exit
end

def myprint(narr,k)
  sum = narr.inject(:+)
  while sum < k
    narr[0] -= 1
    narr[1] += 2
    sum += 1
    if narr[0] == 0
      narr.shift
    end
  end

  len = narr.length
  narr.each_with_index do |x,i|
    x.times do
      print 2**(len-i-1)
      print " "
    end
  end
end

puts "YES"
myprint(n.each_char.to_a.map(&:to_i),k)
puts

# Utility
def digits(num, base)
  num.to_s(base).split("").reverse!.map { |c| c.to_i }
end

# Input
n, k = *(gets.split.map { |s| s.to_i })

# Processing
b = digits(n, 2)
#p b
decomposition = b.map.with_index { |e, i| e * 2**i }
decomposition = decomposition.reject { |e| e == 0 }
decomposition.reverse!
# decomposition is sorted so largest element is first
#p decomposition

if decomposition.length > k || k > n
  puts "NO"
else
  # Will be splitting numbers until we get enough
  while decomposition.length != k
    num = decomposition.shift
    if num == 1
      decomposition.push(num)
    else
      decomposition.push(num / 2, num / 2)
    end
  end

  puts "YES"
  puts decomposition.join(" ")
end

# Utility
def digits(num, base)
  num.to_s(base).split("").map { |c| c.to_i }
end

# Input
n, k = *(gets.split.map { |s| s.to_i })

# Processing
b = digits(n, 2)
decomposition = b.map.with_index { |e, i| e * 2**i }
decomposition = decomposition.reject { |e| e == 0 }
decomposition.reverse!
# decomposition is sorted so largest element is first

if decomposition.length > k || k > n
  puts "NO"
else
  # Will be splitting numbers until we get enough
  while decomposition.length != k
    num = decomposition.shift
    if num == 1
      decomposition.push(num)
    else
      decomposition.push(num / 2, num / 2)
    end
  end

  puts "YES"
  puts decomposition.join(" ")
end


1.upto(gets.chomp.to_i) do
  a, b = gets.chomp.split.map(&:to_i)
  if (a == 1 || b == 1 || (a <= 2 && b <= 2))
    puts 'YES'
  else
    puts 'NO'
  end
end

file = File.exist?('in.in') ? File.new('in.in','r') : STDIN
t = file.gets.chomp.to_i
t.times do
  n, m = file.gets.chomp.split.map(&:to_i)
  if (n == 1 || m == 1) || (n==2 && m == 2)
    puts "YES"
  else
    puts "NO"
  end
end

t = gets.to_i
0.upto t-1 do
  t1, t2 = gets.split.map(&:to_i)
  if t1 == 1 or t2 == 1
    puts "YES"
  elsif t1 == 2 and t2 == 2
    puts "YES"
  else
    puts "NO"
  end
end

t = gets.to_i
t.times do
 a ,b =gets.split.map(&:to_i)
 if a==1 or b==1 or a+b==4
    puts "YES"
 else
    puts"NO"
 end
end


gets
arr = gets.split.map(&:to_i).sort
for i in arr
  if not i == arr[0]
    puts i
    exit 0
  end
end
puts 'NO'

n = gets.to_i

a = gets.split.collect{|x| x.to_i}

min = a.min
a.delete(min)

answer = a.min

if answer == nil
	puts 'NO'
else
	puts answer
end

STDIN.readlines.drop(1).join.split.map(&:to_i).uniq.sort.tap { |x| puts(x.length < 2 ? "NO" : x[1])}

gets
values = gets.split.map { |v| v.to_i }.sort.uniq
result = values.size >= 2 ? values[1] : "NO"
puts result

n = gets.to_i

s = gets.split.map{|i| i.to_i}.sort

if s[0]  == s[-1] then
    result = "NO"
else
    i = 1
    while s[i] == s[0] && i < n do
        i += 1
    end
    result = s[i]
end

puts result

n = gets.chomp.to_i
a = gets.chomp.split(/ /)
an = Hash.new(n)
a.each{|aa| an[aa.to_i] = 0 }
ann = an.keys.sort
if ann.count <= 1
	puts "NO"
else
	puts ann[1]
end

gets
a=gets.split.map(&:to_i).uniq.sort[1]
puts a ?a:"NO"

gets
puts gets.split.map(&:to_i).sort.uniq[1] || 'NO'

gets
x = gets.split.map(&:to_i).uniq.sort
puts (x[1] || 'NO')

n=gets.to_i
i=gets.split.map(&:to_i)
i.sort!
m=i[0]
ans="NO"
i.each{|j|
        if j>m then
                ans=j.to_s
                break
        end
}

puts ans

n=gets.chomp.to_i
memo=gets.chomp.split.map{|i| i.to_i}.sort!
min=memo[0]
n.times do |i|
  if min!=memo[i]
    puts(memo[i])
    exit
  end
end
puts("NO")

n = gets.to_i
a = gets.split(' ')[0...n].map { |s| s.to_i }
puts a.sort.uniq[1] || 'NO'

num = gets.chomp
data = gets.chomp

array = data.split(/\s* \s*/).uniq.sort{|a, b| a.to_i <=> b.to_i}

if array.length > 1
  puts array[1]
else
  puts "NO"
end

amount = gets.to_i
elements = gets.split(' ').map { |e| e.to_i }
elements = elements.to_a.sort
answer = min = elements[0]
(0...amount).each do |i|
  if elements[i] > min
    answer = elements[i]
    break
  end
end
if answer == min
  print "NO"
else
  print answer
end

gets
k=gets.split.map(&:to_i).sort.uniq
puts k.size<=1 ?"NO":k[1]

n = gets.to_i
a = gets.split.collect{|i| i.to_i}
ans = nil
amin = a.min
a.each{|aa| ans = aa if aa > amin and (ans == nil or aa < ans)}
if ans == nil
    puts "NO"
else
    puts ans
end

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

min = nums.min
res = nums.sort.detect{ |x| x > min }
puts res.nil? ? "NO" : res

gets.chomp # Skip the first line
puts((seq = gets.chomp.split.map { |x| x.to_i }.uniq).size > 1 ? seq.sort[1] : 'NO')

n=gets.chomp.to_i
memo=gets.chomp.split.map{|i| i.to_i}.sort!
min=memo[0]
n.times do |i|
  if min!=memo[i]
    puts(memo[i])
    exit
  end
end
puts(-1)

amount = gets.to_i
elements = gets.split(' ').sort.map { |e| e.to_i }
min = elements[0]
(0...amount).each do |i|
  if elements[i] > min
    min = elements[i]
    break
  end
end
print min

amount = gets.to_i
elements = gets.split(' ').sort.map { |e| e.to_i }
answer = min = elements[0]
(0...amount).each do |i|
  if elements[i] > min
    answer = elements[i]
    break
  end
end
if answer == min
  print "NO"
else
  print answer
end

gets
k=gets.split.map(&:to_i).sort.uniq
puts k[1]

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

min = nums.min
res = nums.detect{ |x| x > min }
puts res.nil? ? "NO" : res

gets
arr = gets.split.map(&:to_i).sort
for i in arr
  if not i == arr[0]
    puts i
    exit 0
  end
end
puts arr[0]

gets
arr = gets.split.map(&:to_i).sort
for i in arr
  if not i == arr[0]
    puts i
    break
  end
end
puts arr[0]


n,m,d=gets.chomp.split.map(&:to_i)
a,flag=[],1
n.times { |j| gets.chomp.split.map(&:to_i).each {|i| a<<i} }
a.sort!
mid,ans=a[n*m/2],0
a.each do |i|
	x=(mid-i).abs
	(x%d!=0) ? flag=0 : ans+=x/d
end
puts (flag==0) ? "-1" : ans

class B
  def initialize
    n, m, d = $stdin.gets.chomp.split(' ').map(&:to_i)

    array = []
    n.times do
      array += $stdin.gets.chomp.split(' ').map(&:to_i)
    end

    array.sort!
    pipo = array[array.size/2]

    count = 0

    array.each do |num|
      diff = (pipo-num).abs
      if diff % d == 0
        count += diff/d
      else
        puts -1
        exit
      end
    end

    puts count
  end
end

b = B.new

require "Set"

n,m,d = gets.split(" ").map(&:to_i)
arr = []

n.times do
	arr << gets.split(" ").map(&:to_i)
end
arr.flatten!
arr.sort!
mid = arr[(arr.length)/2]
# puts "#{mid}"
rem = Set.new
sum = 0
arr.each do |a|
	rem.add(a%d)
	sum += (a - mid).abs
end
if rem.length > 1
	puts "-1"
else
	puts sum / d
end

# (0..(n-1)).each do |i|
# 	(0..(m-1)).each do |j|
# 		val = gcd(val,arr[i][j])
# 	end
# end

n, m, d = gets.split.map(&:to_i)
xs = n.times.map { gets.split.map(&:to_i) }.flatten.sort
mod = xs[0] % d
if xs.all? { |x| x % d == mod }
  median = xs[xs.size / 2]
  if median % d != mod
    median += median % d + mod
  end
  p xs.map { |x| (x - median).abs / d }.reduce(:+)
else
  p -1
end

n, m, d = gets.split(" ").collect{|x| x.to_i}

arr = []
n.times do
  gets.split(" ").collect{|x| arr << x.to_i}
end

rem = arr[0] % d
inv = 0
arr.each{|num|
  if num % d != rem
    inv = 1
    break
  end
}

if inv == 1
  puts -1
else
  arr.sort!
  target = arr[n * m / 2]

  mods = 0
  arr.each{|num|
    mods += (num - target).abs / d
  }

  puts mods
end

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

matrix = []
n.times do
  matrix += gets.strip.split.map(&:to_i)[0...m]
end

def possible?(matrix, d)
  mod_d = matrix[0] % d
  matrix.each do |x|
    return false unless (x % d) == mod_d
  end
  true
end

def trace(vars={})
  vars.each do |x,y|
    puts "#{x} => #{y}"
  end
end

def get_count(matrix, c, d)
  matrix.map{|x| (c-x).abs/d}.inject :+
end

if possible?(matrix, d)
  matrix.sort!
  c = matrix[matrix.length/2]
  puts get_count(matrix, c, d)
else
  puts -1
end

require "Set"

n,m,d = gets.split(" ").map(&:to_i)
arr = []

n.times do
	arr << gets.split(" ").map(&:to_i)
end
arr.flatten!
arr.sort!
mid = arr[(arr.length-1)/2 + 1]
# puts "#{mid}"
rem = Set.new
sum = 0
arr.each do |a|
	rem.add(a%d)
	sum += (a - mid).abs
end
if rem.length > 1
	puts "-1"
else
	puts sum / d
end

# (0..(n-1)).each do |i|
# 	(0..(m-1)).each do |j|
# 		val = gcd(val,arr[i][j])
# 	end
# end

n, m, d = gets.split.map(&:to_i)
matrix = n.times.map { gets.split.map(&:to_i) }
mod = matrix[0][0] % d
if matrix.all? { |row| row.all? { |el| el % d == mod } }
  sum = matrix.map { |row| row.reduce(:+) }.reduce(:+)
  target = sum / (n * m)
  target += target % d + mod if target % d != mod
  p matrix.map { |row| row.map { |el| (el - target).abs / d }.reduce(:+) }.reduce(:+)
else
  p -1
end

n, m, d = gets.split.map(&:to_i)
matrix = n.times.map { gets.split.map(&:to_i) }
if matrix.all? { |row| row.all? { |el| el % d == 0 } }
  sum = matrix.map { |row| row.reduce(:+) }.reduce(:+)
  target = sum / (n * m)
  target += target % d if target % d != 0
  p matrix.map { |row| row.map { |el| (el - target).abs / d }.reduce(:+) }.reduce(:+)
else
  p -1
end

n, m, d = gets.split.map(&:to_i)
matrix = n.times.map { gets.split.map(&:to_i) }
if matrix.all? { |row| row.all? { |el| el % d == 0 } }
  sum = matrix.map { |row| row.reduce(:+) }.reduce(:+)
  avg = sum / (n * m)
  p matrix.map { |row| row.map { |el| (el - avg).abs / d }.reduce(:+) }.reduce(:+)
else
  p -1
end

n, m, d = gets.split(" ").collect{|x| x.to_i}

arr = []
n.times do
  gets.split(" ").collect{|x| arr << x.to_i}
end

rem = arr[0] % d
sum = 0
inv = 0
arr.each{|num|
  if num % d != rem
    inv = 1
    break
  end

  sum += num
}

if inv == 1
  puts -1
else
  avg = sum.to_f / (n * m)
  ce = arr[0]
  diff = (arr[0] - avg).abs

  arr.each{|num|
    if (num - avg).abs < diff
      ce = num
      diff = (num - avg).abs
    end
  }

  mods = 0
  arr.each{|num|
    mods += (num - ce).abs / d
  }

  puts mods
end

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

matrix = []
n.times do
  matrix += gets.strip.split.map(&:to_i)[0...m]
end

def possible?(matrix, d)
  mod_d = matrix[0] % d
  matrix.each do |x|
    return false unless (x % d) == mod_d
  end
  true
end

def trace(vars={})
  vars.each do |x,y|
    puts "#{x} => #{y}"
  end
end

if possible?(matrix, d)
  mod_d = matrix[0] % d
  sum = matrix.inject(:+)
  avg = (( (sum.to_f / (n*m)).round - mod_d ) / d).round.to_i * d + mod_d

  #avg1 = ((avg - mod_d) / d).floor.to_i * d + mod_d
  #avg2 = ((avg - mod_d) / d).ceil.to_i * d + mod_d
  #trace avg1: avg1, avg2: avg2
  #
  #n1 = matrix.map{|x| (avg1 - x).abs / d}.inject(:+)
  #n2 = matrix.map{|x| (avg2 - x).abs / d}.inject(:+)
  #puts [n1,n2].min

  puts matrix.map{|x| (avg - x).abs / d}.inject(:+)

else
  puts -1
end

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

matrix = []
n.times do
  matrix += gets.strip.split.map(&:to_i)[0...m]
end

def possible?(matrix, d)
  mod_d = matrix[0] % d
  matrix.each do |x|
    return false unless x % d == mod_d
  end
  true
end

if possible?(matrix, d)
  mod_d = matrix[0] % d
  sum = matrix.inject(:+)
  avg = sum / (n*m)
  avg1 = ((avg - mod_d) / d) * d + mod_d
  avg2 = ((avg - mod_d) / d + 1) * d + mod_d
  n1 = matrix.map{|x| (x - avg1).abs / d}.inject(:+)
  n2 = matrix.map{|x| (x - avg2).abs / d}.inject(:+)
  puts [n1,n2].min
else
  puts -1
end

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

matrix = []
n.times do
  matrix += gets.strip.split.map(&:to_i)[0...m]
end

def possible?(matrix, d)
  mod_d = matrix[0] % d
  matrix.each do |x|
    return false unless (x % d) == mod_d
  end
  true
end

def trace(vars={})
  vars.each do |x,y|
    puts "#{x} => #{y}"
  end
end

def get_count(matrix, c)
  matrix.map{|x| (c-x).abs}.inject :+
end

if possible?(matrix, d)
  matrix.sort!
  min = matrix.first
  max = matrix.last
  c = min
  n = nil
  while c <= max
    count = get_count(matrix, c)
    n = count if n.nil? || count < n
    c += d
  end
  puts n
else
  puts -1
end

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

matrix = []
n.times do
  matrix += gets.strip.split.map(&:to_i)[0...m]
end

def possible?(matrix, d)
  mod_d = matrix[0] % d
  matrix.each do |x|
    return false unless x % d == mod_d
  end
  true
end

if possible?(matrix, d)
  sum = matrix.inject(:+)
  avg = sum.to_f / (n*m)
  avg1 = (avg / d).to_i * d
  avg2 = ((avg / d).to_i + 1) * d
  n1 = matrix.map{|x| (x - avg1).abs / d}.inject(:+)
  n2 = matrix.map{|x| (x - avg2).abs / d}.inject(:+)
  puts [n1,n2].min
else
  puts -1
end

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

matrix = []
n.times do
  matrix += gets.strip.split.map(&:to_i)[0...m]
end

$stderr.puts n,m,d, matrix.inspect

def possible?(matrix, d)
  mod_d = matrix[0] % d
  matrix.each do |x|
    return false unless (x % d) == mod_d
  end
  true
end

if possible?(matrix, d)
  mod_d = matrix[0] % d
  sum = matrix.inject(:+)
  avg = sum.to_f / (n*m)

  avg1 = ((avg - mod_d) / d).to_i * d + mod_d
  avg2 = (((avg - mod_d) / d).to_i + 1) * d + mod_d
  n1 = matrix.map{|x| (x - avg1).abs / d}.inject(:+)
  n2 = matrix.map{|x| (x - avg2).abs / d}.inject(:+)
  puts [n1,n2].min
else
  puts -1
end


def b(l,r,v=1)
	if l==r
		$t[v]=l
		return 0
	end
	return 0 if l>r
	m=(l+r)/2
	b(l,m,v*2)
	b(m+1,r,v*2+1)
	if $a[$t[v*2]]<$a[$t[v*2+1]]
		$t[v]=$t[v*2]
	else
		$t[v]=$t[v*2+1]
	end
end
def g(l,r,tl=0,tr=$n-1,v=1)
	return $n if tl>r || tr<l
	return $t[v] if tl>=l && tr<=r
	m=(tl+tr)/2
	a=g(l,r,tl,m,v*2)
	b=g(l,r,m+1,tr,v*2+1)
	return ($a[a]<$a[b]) ? a : b
end
def z(l,r,h)
	return 0 if l>r
	m=g(l,r)
	return [r-l+1,z(l,m-1,$a[m])+z(m+1,r,$a[m])+$a[m]-h].min
end
n=gets.to_i
$n=2
while $n<n
	$n*=2
end
$t=[0]*$n*4
$a=gets.split.map(&:to_i)+[1e18]*$n*4
b(0,$n-1)
p z(0,n-1,0)

class Array
	attr_accessor :seg_tree
	Info = Struct.new(:left, :right, :min, :index)

	def create_seg(left = 0, right = size - 1, n = 1)
		@seg_tree = [] if @seg_tree == nil

		if right == left
			@seg_tree[n] = Info.new(left, left, slice(left), left)
		else
			mid = (left + right) / 2
			min = [create_seg(left, mid, 2*n), create_seg(mid + 1, right, 2*n+1)].min{|x, y| x.min <=> y.min}
			@seg_tree[n] = Info.new(left, right, min.min, min.index)
		end

		@seg_tree[n]
	end

	def seg_min(l, r, n = 1)
		mid = (@seg_tree[n].left + @seg_tree[n].right) / 2

		if l <= @seg_tree[n].left and @seg_tree[n].right <= r
			@seg_tree[n]
		elsif mid >= r
			seg_min(l, r, n*2)
		elsif mid < l
			seg_min(l, r, n*2 + 1)
		else
			[seg_min(l, r, n*2), seg_min(l, r, n*2 + 1)].min{|x, y| x.min <=> y.min}
		end
	end
end

n = gets.chomp.to_i
a = gets.chomp.split.map{|i| i.to_i}
a.create_seg
# puts a.seg_tree
# puts a.seg_min(1, 2)

def sol(arr, l, r, base)
	# print arr
	return 0 if r == l && base == arr[r]
	return r - l + 1 if r - l + 1 <= 1
	m = arr.seg_min(l, r).min
	mid = arr.seg_min(l, r).index
	ans = [r - l + 1, sol(arr, l, mid - 1, m) + sol(arr, mid + 1, r, m) + (m - base)].min
	# puts "r - l + 1 = #{r - l + 1}, sol(arr, #{l}, #{mid - 1}, #{m}) = #{sol(arr, l, mid - 1, m)}, sol(arr, #{mid + 1}, #{r}, #{m}) = #{sol(arr, mid + 1, r, m)}"
	# puts "in (#{l}, #{r}), min = #{m}, base = #{base}, ans = #{ans}"
	ans
end

puts sol(a, 0, a.size - 1, 0)

n = gets.chomp.to_i
a = gets.chomp.split.map{|i| i.to_i}

def split0(arr)
	slice = []
	ans = []
	arr.each do |i|
		if i == 0 && (not slice.empty?)
			ans << slice
			slice = []
		elsif i != 0
			slice << i
		end
	end
	if (not slice.empty?)
		ans << slice
	end
	ans
end

def sol(arr)
	return arr.size if arr.size <= 1
	narr = arr.map{|i| i - arr.min}

	return [arr.size, split0(narr).map{|i| sol i}.inject(0, :+) + 1].min
end

puts sol(a)

class Array
	attr_accessor :seg_tree
	Info = Struct.new(:left, :right, :min, :index)

	def create_seg(left = 0, right = size - 1, n = 1)
		@seg_tree = [] if @seg_tree == nil

		if right == left
			@seg_tree[n] = Info.new(left, left, slice(left), left)
		else
			mid = (left + right) / 2
			min = [create_seg(left, mid, 2*n), create_seg(mid + 1, right, 2*n+1)].min{|x, y| x.min <=> y.min}
			@seg_tree[n] = Info.new(left, right, min.min, min.index)
		end

		@seg_tree[n]
	end

	def seg_min(l, r, n = 1)
		mid = (@seg_tree[n].left + @seg_tree[n].right) / 2

		if l <= @seg_tree[n].left && r >= @seg_tree[n].right
			@seg_tree[n]
		elsif mid >= r
			seg_min(l, r, n*2)
		elsif mid <= l
			seg_min(l, r, n*2 + 1)
		else
			[seg_min(l, r, n*2), seg_min(l, r, n*2 + 1)].min{|x, y| x.min <=> y.min}
		end
	end
end

n = gets.chomp.to_i
a = gets.chomp.split.map{|i| i.to_i}
a.create_seg

def sol(arr, l, r, base)
	# print arr
	return 0 if r == l && base == arr[r]
	return r - l + 1 if r - l + 1 <= 1
	m = arr.seg_min(l, r).min
	mid = arr.seg_min(l, r).index
	return [r - l + 1, sol(arr, l, mid - 1, m) + sol(arr, mid + 1, r, m) + (m - base)].min
end

puts sol(a, 0, a.size - 1, 0)


n=gets
text= gets
I=text.scan('I').length
F=text.scan('F').length
A=text.scan('A').length

if I>1
  puts "0"
elsif I==1
  puts "1"
else
  puts A.to_s
end

n = gets.to_i
s = gets.strip

ac = 0
ic = 0

for i in 0...n
  if s[i] == 'A' then ac += 1 end
  if s[i] == 'I' then ic += 1 end
end

if ic > 1 then puts 0
elsif ic == 1 then puts 1
else puts ac end

#!/usr/bin/ruby1.9

n = STDIN.readline.to_i
s = STDIN.readline.strip.downcase

counts = {'a' => 0, 'i' => 0, 'f' => 0}

n.times do |i|
	counts[s[i]] += 1
end

if counts['i'] > 1
	answer = 0
elsif counts['i'] == 1
	answer = 1
else
	answer = counts['a']
end

puts answer

n=gets.to_i
s=gets.chomp
i=s.count(?I)
p i>1 ? 0 : i>0 ? 1 : n-s.count(?F)

n = gets.to_i
s = gets.chomp
players = s.count('I')

if players == 1
  puts "1"
else
  if players == 0
    puts s.count('A')
  else
    puts "0"
  end
end

gets;a=gets;a.count('I')==1 ?(puts 1):(a.count('I')==0 ?(puts a.count('A')):(puts 0))

n = gets.to_i
s = gets.chomp

l = s.size
a = 0
i = 0
f = 0
(0...l).each { |p|
   if s[p].downcase == "a"
      a+=1
   end
   if s[p].downcase == "f"
      f+=1
   end
   if s[p].downcase == "i"
      i+=1
   end

}

if i == 0
 puts a
end

if i == 1
 puts 1
end

if i > 1
 puts 0
end

n = gets.to_i
s = gets

a = i = 0
n.times do |idx|
  if s[idx] == "A"
    a += 1
  elsif s[idx] == "I"
    i += 1
  end
end

if i > 1
  puts 0
elsif i == 0
  puts a
else
  puts 1
end

gets # Skip the first line
s = gets.chomp
players_in = s.count('I')
puts(players_in == 1 ? 1 : players_in == 0 ? s.count('A') : 0)

n = gets; a = gets
a.count('I')==1? (puts 1) : (a.count('I')==0? (puts a.count('A') ):(puts 0))

#!/usr/bin/ruby1.9

n = STDIN.readline.to_i
s = STDIN.readline.strip.downcase

counts = {'a' => 0, 'i' => 0, 'f' => 0}

n.times do |i|
	counts[s[i]] += 1
end

if counts['i'] == 1
	answer = 1
else
	answer = counts['a']
end

puts answer

n=gets.to_i
s=gets.chomp
p s.count(?I)>1 ? 0 : n-s.count(?F)


n = gets.chomp.to_i
a = gets.chomp.split(/ /).map!{|x| x.to_i}
b = []
idx = 1
a.each{|aa|
  b << [aa, idx]
  idx += 1
}
b = b.sort{|b2,b1|
  b1[0] <=> b2[0]
}
c1 = []
c2 = []
c1s = 0
c2s = 0
b.each{|bb|
  if c1.length > c2.length
    c2 << bb[1]
    c2s += bb[0]
  elsif c2.length > c1.length
    c1 << bb[1]
    c1s += bb[0]
  else
    if c1s > c2s
      c2 << bb[1]
      c2s += bb[0]
    else
      c1 << bb[1]
      c1s += bb[0]
    end
  end
}
puts c1.length
puts c1.join(" ")
puts c2.length
puts c2.join(" ")

n = gets.to_i
a = gets.split.map(&:to_i).zip(1.upto(n)).sort.reverse
l, r, x, y = 0, 0, [], []

if n.odd?
    b = a.pop
    l += b[0]
    x << b[1]
end

(n / 2).times do
    b = a.pop
    c = a.pop
    b, c = c, b if l <= r
    l += b[0]
    x << b[1]
    r += c[0]
    y << c[1]
end

p x.size
puts x * ' '
p y.size
puts y * ' '

n=gets.chomp.to_i
players=[]
arr=gets.chomp.split
arr.each_index{|i| players.push [i+1,arr[i].to_i]}
players.sort_by!{|a| a[1]}
dif=0
i=2
s_i=0
l_i=players.size-1
a=[]
b=[]
while i <= n
 if dif >0
   a.push players[s_i][0]
   dif =dif+players[s_i][1]
   s_i=s_i+1
 else
   a.push players[l_i][0]
   dif =dif+players[l_i][1]
   l_i=l_i-1
 end
  if dif <0
   b.push players[s_i][0]
   dif =dif-players[s_i][1]
   s_i=s_i+1
 else
   dif =dif-players[l_i][1]
   b.push players[l_i][0]
   l_i=l_i-1
 end
i=i+2
end
if n%2!=0
 if dif >0
   b.push players[l_i][0]
 else
   a.push players[l_i][0]
 end
end
#p dif
puts a.size,a.to_s.delete('[').delete(']').gsub(",",""),b.size,b.to_s.delete('[').delete(']').gsub(",","")

n=gets.chomp.to_i
players=[]
arr=gets.chomp.split
arr.each_index{|i| players.push [i+1,arr[i].to_i]}
players.sort_by!{|a| a[1]}
dif=0
i=2
s_i=0
l_i=players.size-1
a=[]
b=[]
while i <= n
 if dif >0
   a.push players[s_i][0]
   dif =dif+players[s_i][1]
   s_i=s_i+1
 else
   a.push players[l_i][0]
   dif =dif+players[l_i][1]
   l_i=l_i-1
 end
  if dif <0
   b.push players[s_i][0]
   dif =dif+players[s_i][1]
   s_i=s_i+1
 else
   dif =dif+players[l_i][1]
   b.push players[l_i][0]
   l_i=l_i-1
 end
i=i+2
end
if n%2!=0
 if dif >0
   b.push players[l_i][0]
 else
   a.push players[l_i][0]
 end
end
puts a.size,a.to_s.delete('[').delete(']').gsub(",",""),b.size,b.to_s.delete('[').delete(']').gsub(",","")


gets;a=gets.split.map &:to_i;gets;b=gets.split.map &:to_i;c=a.product(b).map{|x,y|y%x==0?(y/x):0};p c.count c.max

n = gets.to_i
nn = []
nmas = gets.split
m = gets.to_i
mm = []
mmas = gets.split
i = 0
while i<n do
    nn << nmas[i].to_i
    i += 1
end
i = 0
while i<m do
    mm << mmas[i].to_i
    i += 1
end
result = 0
max = 0
i = 0
while i<n do
    j = 0
    while j<m do
        k = mm[j]/nn[i]
        if (mm[j] % nn[i] == 0) then
            if  k == max then result += 1
            elsif k > max then
                result = 1
                max = k
            end
        end
        j += 1
    end
    i += 1
end
print "#{result}\n"

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

c = b.product(a).map{ |bi,aj| bi*1.0/aj }.select{ |i| i%1 == 0 }
puts c.count c.max

n = gets.to_i
gears1 = gets.split(' ').map{|x| x.to_i}
m = gets.to_i
gears2 = gets.split(' ').map{|x| x.to_i}

best = -1
count = 0

for i in gears1
        for j in gears2
                if j % i == 0 && j / i > best
                        best = j / i
                        count = 1
                elsif j % i == 0 && j / i == best
                        count += 1
                end
        end
end

print "#{count}\n"

n, a, m, b = readlines.map(&:chomp).map(&:split).map { |x| x.map!(&:to_i) }
s = a.product(b).map { |x| x[1]%x[0] == 0 ? x[1]/x[0] : nil }.compact
puts s.count { |x| x == s.max }

n = gets.to_i
a = gets.split.map! {|x| x.to_i }
m = gets.to_i
b = gets.split.map! {|x| x.to_i }
ans = []
for x in a do
    for y in b do
        ans << y / x if y % x == 0
    end
end
mx = -1
ans.each do |x|
    mx = x if x > mx
end
cnt = 0
ans.each do |x|
    cnt += 1 if x == mx
end
puts cnt

a=gets.chomp.to_i
b=gets.chomp.split(" ")
0.upto(a-1) {|i| b[i]=b[i].to_i}
c=gets.chomp.to_i
d=gets.chomp.split(" ")
0.upto(c-1) {|i| d[i]=d[i].to_i}

num=[]
d.each do |i|
b.each do |j|
if i%j==0
num << i/j
end
end
end
num.sort!
max=num[num.length-1]
ans=num.count(max)
puts "#{ans}"

gets
a=gets.split.map(&:to_i)
gets
b=gets.split.map(&:to_i)
c = []
a.each do |aa|
  b.each do |bb|
    c << bb / aa if bb % aa == 0
  end
end
p c.count c.max

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

n = gets.to_i
front = gets.split.map &:to_i
m = gets.to_i
back = gets.split.map &:to_i

max_value = 0
max_count = 0

for front_x in front do
  for back_x in back do
    next if back_x % front_x != 0
    next if back_x / front_x < max_value

    if back_x / front_x == max_value then
      max_count+=1
    else
      max_value = back_x / front_x
      max_count = 1
    end
  end
end

print max_count

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

n = gets.to_i
front = gets.split.map &:to_i
m = gets.to_i
back = gets.split.map &:to_i

hash = Hash.new( 0 )

for front_x in front do
  for back_x in back do
    next if back_x % front_x != 0

    hash[back_x / front_x]+=1
  end
end

print hash.to_a.max[1]

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

gets
front = gets.split.map &:to_i
gets
back = gets.split.map &:to_i

hash = Hash.new( 0 )

front.each{|v1|
  back.each{|v2|
    next if v2 % v1 != 0

    hash[v2 / v1]+=1
  }
}

print hash.to_a.max[1]

#!/usr/bin/ruby
n = gets.chomp.to_i
a = gets.chomp.split(' ').map{|x| x.to_i}
m = gets.chomp.to_i
b = gets.chomp.split(' ').map{|x| x.to_i}
maxrat = 0
ints = 0
for i in 0..n-1
	for j in 0..m-1
		if b[j]%a[i] == 0
			if b[j]/a[i] > maxrat
				ints = 1
				maxrat = b[j]/a[i]
			elsif b[j]/a[i] == maxrat
				ints +=1
			end
		end
	end
end
puts ints

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

values = []
b.each do |i|
  a.each do |j|
      values << i/j if i%j == 0
  end
end

values = values.sort_by{|el| -el}
puts values.count(values[0])

n = Integer(gets.chomp)
a = gets.chomp.split.map { |x| x.to_i }
m = Integer(gets.chomp)
b = gets.chomp.split.map { |x| x.to_i }

max = 0
for i in (m - 1).downto(0)
  for j in 0...n
    if b[i] % a[j] == 0
      r = b[i] / a[j]
      max = r if r > max
    end
  end
end

c = 0
for i in 0...m
  for j in 0...n
    c += 1 if b[i] % a[j] == 0 && b[i] / a[j] == max
  end
end
puts c

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

max = 0
count = 0

a.each do |ai|
  b.each do |bj|
    if bj % ai == 0
      r = bj / ai
      if r == max
        count += 1
      elsif r > max
        max = r
        count = 1
      end
    end
  end
end

p count

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

c = a.product(b).map!{|x,y| y%x == 0 ? y/x : 0}

p c.count(c.max)

n = Integer(gets.chomp)
a = gets.chomp.split.map { |x| x.to_i }
m = Integer(gets.chomp)
b = gets.chomp.split.map { |x| x.to_i }

max = 0
catch (:done) do
  for i in (m - 1).downto(0)
    for j in 0...n
      if b[i] % a[j] == 0
        max = b[i] / a[j]
        throw :done
      end
    end
  end
end

c = 0
for i in 0...m
  for j in 0...n
    c += 1 if b[i] % a[j] == 0 && b[i] / a[j] == max
  end
end
puts c


n,k = gets.split.map(&:to_i)
ans = Array.new(k){|i|i+1}
n -= k*(k+1)/2
if n < 0
    puts "NO"
    exit
end
k.times do |i|
    ans[i] += n/k
end
n %= k
(k-1).downto(1) do |i|
    m = [ans[i-1]*2-ans[i],n].min
    ans[i] += m
    n -= m
end
(k-1).times do |i|
    if ans[i]*2 < ans[i+1]
        puts "NO"
        exit
    end
end
if n > 0
    puts "NO"
    exit
end
puts "YES",ans.join(" ")

n,k = gets.split.map(&:to_i)
ans = Array.new(k){|i|i+1}
n -= k*(k+1)/2
if n < 0
    puts "NO"
    exit
end
k.times do |i|
    ans[i] += n/k
end
n %= k
(k-1).downto(1) do |i|
    m = [ans[i-1]*2-ans[i],n].min
    m = [ans[i+1]-1,m].min if i+1 < k
    ans[i] += m
    n -= m
end
(k-1).times do |i|
    if ans[i]*2 < ans[i+1]
        puts "NO"
        exit
    end
end
if n > 0
    puts "NO"
    exit
end
puts "YES",ans.join(" ")


s = gets.split('')
s.pop
k = gets.to_i
values_str = gets.split(' ')
values = Array.new(26) { Array.new(2) }
values_str.each_with_index do |val, i|
  values[i] = [i, val.to_i]
end

total = 0
s.each_with_index do |c, i|
  total += (i + 1) * values[c.ord - 'a'.ord][1]
end

values.sort! do |i, j|
  j[1] <=> i[1]
end

k.times do |i|
  total += (i + 1 + s.length) * values[0][1]
end

puts total

s = gets.chomp
k = gets.to_i
w = gets.split.map { |x| x.to_i }

ans = 0
s.each_char.with_index do |c, i|
	ans += w[c.ord - 97] * (i + 1)
end
ans += k * (s.length + 1 + s.length + k) / 2 * w.max

puts ans

#!/usr/bin/ruby
s=gets.chomp.bytes.map{|e|e-97}
n=gets.to_i
a=gets.split.map(&:to_i)
p (s.map{|e|a[e]}+[a.max]*n).each.with_index(1).reduce(0){|s,(e,i)|s+e*i}

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

ans = 0
s.each_char.with_index  do |c,i|
	weight = arr[c.ord - 97]
	ans += weight * (i +1)
end
sum = ((s.length+1)..(s.length+k)).inject(0,&:+)
ans +=  sum * arr.max
puts ans
#

str = gets.chomp
k = gets.to_i
azarr = gets.split(" ").collect { |e|  e.to_i}

az = []
str.each_char { |e|
    az << azarr[e.ord - 97]
}

max = [az.max, azarr.max].max

i = 1
len = str.length
#p len
sum = max*(k*(2*len+1+k))/2
#p sum
az.each { |e|
    sum += i*e
    i += 1
}
p sum

a = gets.chomp.split('').map { |c| c.ord - 97 }
k = gets.to_i
w = gets.chomp.split.map(&:to_i)
n = a.size + 1
puts a.each_with_index.map { |x, i| w[x] * (i + 1) }.inject(:+) + ((2 * n + k - 1) * k / 2) * w.max

a = gets.chomp.split('').map { |c| c.ord - 97 }
k = gets.to_i
w = gets.chomp.split.map(&:to_i)
puts a.each_with_index.map { |x, i| w[x] * (i + 1) }.inject(:+) + ((2 * a.size + k + 1) * k / 2) * w.max

s=gets.chomp
k=gets.to_i
w=gets.split.map &:to_i
l,t=s.size,0
l.times{|i| t+=(i+1)*w[s[i].ord-'a'.ord]}
t+=(k*l+k*(k+1)/2)*w.max
p t

s = gets.chomp
k = gets.to_i
w = gets.split.map {|i| i.to_i}
m = w.max
sum = 0
s.scan(/./).each_with_index {|i, x| sum += w[i.ord - 'a'.ord] * (x + 1)}
for i in 1..k
  sum += m * (i + s.length)
end
puts sum

s = gets
k = gets
vals = gets

k = k.to_i
vals = vals.split(" ")
vals = vals.map(&:to_i)

s = s.split(" ")
v1 = 0
i = 1

s[0].each_char do |c|
    v1+=(i*vals[c.ord-97])
    i+=1
end

vals = vals.sort.reverse

for j in 1..k
    v1 += (vals[0]*i)
    i+=1
end
i = 1

puts v1

s=gets.chomp.split('')
k=gets.to_i
a=gets.chomp.split.map(&:to_i)
m,ans,i=a.max,0,0
while i<s.length
	ans+=a[s[i].ord-97]*(i+1)
	i+=1
end
k.times do
	ans+=m*(i+1)
	i+=1
end
puts ans

s=gets
n=gets.to_i
a=gets.split.map{|i| i.to_i}+[0]*100
e=0
i=1
s.each_byte{|c| e+=a[c-'a'.ord]*i;i+=1}
p(e+a.max*(s.length*2+n-1)*n/2)

s=gets.chomp
k=gets.to_i
x=gets.chomp.split.map{|e| e.to_i}
y=x.max
i=0
h=Hash.new(0)
"a".upto("z"){|e|
	h[e]=x[i]
	i+=1
}
i=1
ans=0
s.chars{|e|
	ans+=h[e]*i
	i+=1
}
k.times{
	ans+=y*i
	i+=1
}
puts ans

s = gets.strip
k = gets.to_i
weights = gets.split(" ").map(&:to_i)

alph = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
alph_weights = {}
alph.each_with_index { |letter, index| alph_weights[letter] = weights[index] }

max, max_index = weights.each_with_index.max

s << alph[max_index] * k

str_weight = 0
s.chars.each_with_index { |char, index|
  str_weight += alph_weights[char]*(index+1)
}

#puts s
puts str_weight

#!/usr/bin/ruby
s=gets.chomp.bytes.map{|e|e-97}
n=gets.to_i
a=gets.split.map(&:to_i)
p (s.map{|e|a[e]}+[a.max]*3).sort.each.with_index(1).reduce(0){|s,(e,i)|s+e*i}

#!/usr/bin/ruby
s=gets.chomp.bytes.map{|e|e-97}
n=gets.to_i
a=gets.split.map(&:to_i)
p (s.map{|e|a[e]}+[a.max]*3).each.with_index(1).reduce(0){|s,(e,i)|s+e*i}

s = gets
k = gets
vals = gets

k = k.to_i
vals = vals.split(" ")
s = s.split(" ")
v1 = 0
i = 1

s[0].each_char do |c|
    v1+=(i*vals[c.ord-97].to_i)
    i+=1
end

vals = vals.sort.reverse

for j in 1..k
    v1 += (vals[0].to_i * i )
    i+=1
end

puts v1


inp = gets.to_i

a = 0
x = inp/7
found=false
res1=''
res2=''
while x >= 0 do
  a = inp - 7*x
  if a%4 == 0
    found = true
    break
  end
  x = x-1
end

res1 = res1.rjust(x,'7')
res2 = res2.rjust(a/4,'4')

if found
  puts res2+res1
else
  puts -1
end

#!/usr/bin/ruby

n = gets.to_i
seven = (n/7)
while true
	if (n-seven*7) % 4 == 0
		puts '4'*((n-seven*7)/4) << '7'*seven
		exit
	end
	seven -= 1
	if seven == -1
		puts -1
		exit
	end
end

n = gets.chop.to_i
a = 0
r = 0
vyslo = 0
(n-1).downto(0){ |b|
	#puts b
	a = (n-7*b)/4
	if 4*a + 7*b == n && a >= 0
		r = b
		vyslo = 1
		break
	end
}
if vyslo == 1
	for i in 0..a-1
		print "4"
	end
	for i in 0..r-1
		print "7"
	end
else
	print "-1"
end
print "\n"

def check
  count = 0
  val.each_char  { |s|
    if (s == '4' or s == '7')
      count += 1
    end
  }

  if (count == 4 or count == 7)
    return true
  else
    return false
  end
end

def succ(val, index)
  if index == -1
    return '4' + val
  end

  if (val[index] == '7')
    val[index] = '4'
    val = succ(val, index - 1)
  else
    val[index] = '7'
  end
  return val
end

def isucc(i)
  s = i.to_s
  return succ(s, s.length-1).to_i
end

def sum(i)
  h = Hash.new(0)
  h['4'] = 0
  h['7'] = 0
  i.each_char { |c|
    h[c] += 1
  }
  return h['4']*4 + h['7']*7
end

i = gets.chop.to_i

max4 = i / 4
max7 = i / 7
max4 += 1
max7 += 1

arr = Array.new(0)
res = "-1"
found = false
max7.downto(0) { |i7|
  #puts i7
  max4 = i - i7*7 + 7
  max4.downto(0) { |i4|
    if i == i7*7 + i4*4
      res = '4'*i4 + '7'*i7
      found = true
      break
    end
  }
  if found == true
    break
  end
}

puts res

#start = '4'
#100.times {
#  start = succ(start, start.length-1)
#  puts "#{start} sum = #{sum(start)}"
#}

i = STDIN.gets.to_i
  if (i % 7) == 0
    r = '7'*(i/7)
  else
    if i < 4
      r = '-1'
    else
      seven = i/7
      while seven > -1 && (i-seven*7) % 4 != 0
        seven -= 1
      end

      if seven == -1
        r = '-1'
      else
        r = "#{'4' * ((i - seven*7) / 4)}#{'7' * seven}\n"
      end
    end
  end
puts r

n = gets.to_i
s = n / 7
r = n - s * 7
while s > 0 and r % 4 != 0 do s -= 1; r = n - s * 7 end
if r % 4 != 0 or n < 4
  puts -1
else
  f = r / 4
  puts "4" * f + "7" * s
end

#!/usr/bin/env ruby

n = STDIN.gets.to_i

count = n/7
while  count > -1
  if (n-7*count) % 4 == 0
    puts [[4]*((n-7*count)/4),[7]*count].flatten.join
    exit
  end
  count -= 1
end
puts -1

def func(n)
    if (n == 0) then
        s = "-1"
    elsif (n % 7 == 0) then
        s = "7" * (n/7)
    elsif (n % 4 == 0) then
        s = "4"
        if n != 4 then
            n -= 4
            s += func(n)
        end
    else
        s = "-1"
    end
    return s
end

n = gets.to_i
if (s = func(n)) != "-1" then
    print "#{s}\n"
else
    r = 4
    s = "4"
    while r < n do
        if func(n-r) != "-1" then
            s += func(n-r)
            r = n
        elsif (r + 7) == n then
            r += 7
            s += "7"
        else
            r += 4
            s += "4"
        end
    end
    if r > n then s = "-1"
    end
    print "#{s}\n"
end

n = gets.to_i
# n = 7 * n7 + 4 * n4

n7 = n / 7
while n7 >= 0 do
  if (n - n7 * 7) % 4 == 0
    n4 = (n - n7 * 7) / 4
    break
  end
  n7 -= 1
end

if n7 >= 0
  puts (Array.new(n4) {4}).join + (Array.new(n7) {7}).join
else
  puts -1
end

sum = gets.chomp.to_i
div = 1
count = 0
while sum>=0
  if(sum%7==0 or sum == 0)
    puts ("4"*count + "7"*(sum/7)).split("").sort.join
    exit
  end
  sum-=4
  count+=1
end
puts -1

n = gets.to_i

s = n / 7
f = 0
while s * 7 + f * 4 != n and s >= 0 do
    f += 1
    if s * 7 + f * 4 > n then s -= 1 end
end

if s >= 0 then
    st = ''
    puts '4' * f + '7' * s
else
    puts -1
end

# def lucky(four,seven,n)
# 	if n == 0
# 		# puts "here"
# 		@fo = four
# 		@sev = seven
# 		return true
# 	end
# 	return false if n < 0
# 	if lucky(four,seven+1,n-7) || lucky(four+1,seven,n-4)
# 		return true
# 	else
# 		return false
# 	end
# end

# @fo = 0
# @sev = 0

# n = gets.to_i

# # puts lucky(0,0,n)
# if lucky(0,0,n)
# 	puts "4" * @fo + "7" * @sev
# else
# 	puts "-1"
# end

n = gets.to_i

b = 0
min_a = 10 ** 12
min_b = 10 ** 12
min_ab = 10 ** 12

a = 0
ans = false
# a * 4 + b * 7 = n
while a * 4 <= n
	b = (n - (a * 4))/7
	if (a * 4 + b * 7 == n) && ((a + b < min_ab) || (a + b == min_ab && b < min_b))
		min_a = a
		min_b = b
		min_ab = a + b
		ans = true
	end
	a += 1
end

if ans
	puts "4" * min_a + "7" * min_b
else
	puts "-1"
end

n = gets.to_i
m = -1
for i in 0..n/7
	m = i if ( n - 7 * i ) % 4 == 0
end
if m == -1
	puts -1
else
	n = ( n - 7 * m ) / 4
	n.times { print 4 }
	m.times { print 7 }
	puts
end

n = gets.to_i
cnt_7 = n/7
cnt_4 = (n-cnt_7*7)/4
while cnt_7 >= 0 and  cnt_7*7+cnt_4*4 != n
    cnt_7 -= 1
    cnt_4 = (n-cnt_7*7)/4
end
if cnt_7 >= 0 and  cnt_7*7+cnt_4*4 == n
    puts '4'*cnt_4+'7'*cnt_7
else
    puts -1
end

#!/usr/bin/ruby

n = gets.to_i
dp = Array.new(n+1, nil)
dp[0] = ''
1.upto(n) do |i|
    if i-4 >= 0 && dp[i-4]
        str = dp[i-4] + '4'
        if dp[i].nil?
            dp[i] = str
        else
            dp[i] = str if dp[i] > str
        end
    end

    if i-7 >= 0 && dp[i-7]
        str = dp[i-7] + '7'
        if dp[i].nil?
            dp[i] = str
        else
            dp[i] = str if dp[i] > str
        end
    end
end

puts dp[n] || -1

n = gets.chop.to_i
a = 0
b = 0
vyslo = 0
for b in 0..n-1
	a = (n-7*b)/4
	if 4*a + 7*b == n && a >= 0
		vyslo = 1
		break
	end
end
if vyslo == 1
	for i in 0..a-1
		print "4"
	end
	for i in 0..b-1
		print "7"
	end
else
	print "-1"
end
print "\n"

n = gets.chop.to_i
a = 0
b = 0
for b in 0..n-1
	a = (n-7*b)/4
	if 4*a + 7*b == n
		break

	end
end
puts "A" + a.to_s
puts "B" + b.to_s

n = gets.chop.to_i
a = 0
b = 0
for b in 0..n-1
	a = (n-7*b)/4
	if 4*a + 7*b == n
		break

	end
end
for i in 0..a-1
	print "4"
end
for i in 0..b-1
	print "7"
end
print "\n"

n = gets.chop.to_i
a = 0
b = 0
vyslo = 0;
for b in 0..n-1
	a = (n-7*b)/4
	if 4*a + 7*b == n
		vyslo = 1
		break
	end
end
if vyslo == 1
	for i in 0..a-1
		print "4"
	end
	for i in 0..b-1
		print "7"
	end
else
	print "-1"
end
print "\n"

def check
  count = 0
  val.each_char  { |s|
    if (s == '4' or s == '7')
      count += 1
    end
  }

  if (count == 4 or count == 7)
    return true
  else
    return false
  end
end

def succ(val, index)
  if index == -1
    return '4' + val
  end

  if (val[index] == '7')
    val[index] = '4'
    val = succ(val, index - 1)
  else
    val[index] = '7'
  end
  return val
end

def isucc(i)
  s = i.to_s
  return succ(s, s.length-1).to_i
end

def sum(i)
  h = Hash.new(0)
  h['4'] = 0
  h['7'] = 0
  i.each_char { |c|
    h[c] += 1
  }
  return h['4']*4 + h['7']*7
end

i = gets.chop.to_i
ii = i + 4 + 7 + 4
x = 4
fail = false

four_count = i / 4
start = '4' * four_count
#x = start.to_i

s = sum(start)
while s != i
  if (s > ii)
    fail = true
    break
  end

  start = succ(start, start.length-1)
  s = sum(start)
end

if (fail == false)
  puts start
else
  puts -1
end

i = STDIN.gets.to_i
  if (i % 4) == 0
    r = '4'*(i/4)
  else
    if i < 7
      r = '-1'
    else
      four = i/4
      while four > -1 && (i-four*4) % 7 != 0
        four -= 1
      end

      if four == -1
        r = '-1'
      else
        r = "#{'4' * four}#{'7' * ((i - four*4) / 7)}\n"
      end
    end
  end
puts r

#!/usr/bin/env ruby

n = STDIN.gets.to_i

count = 0
while  (7*count) < n
  if (n-7*count) % 4 == 0
    puts [[4]*((n-7*count)/4),[7]*count].flatten.join
    exit
  end
  count += 1
end
puts -1

def func(n)
    if (n == 0) then
        s = "-1"
    elsif (n % 7 == 0) then
        s = "7" * (n/7)
    elsif (n % 4 == 0) then
        s = "4" * (n/4)
    else
        s = "-1"
    end
    return s
end

n = gets.to_i
if (s = func(n)) != "-1" then
    print "#{s}\n"
else
    r = 4
    s = "4"
    while r < n do
        if func(n-r) != "-1" then
            s += func(n-r)
            r = n
        elsif (r + 7) == n then
            r += 7
            s += "7"
        else
            r += 4
            s += "4"
        end
    end
    if r > n then s = "-1"
    end
    print "#{s}\n"
end

def func(n)
    if (n == 0) then
        s = "-1"
    elsif (n % 7 == 0) then
        s = "7" * (n/7)
    elsif (n % 4 == 0) then
        s = "4"
        n -= 4
        s += func(n)
    else
        s = "-1"
    end
    return s
end

n = gets.to_i
if (s = func(n)) != "-1" then
    print "#{s}\n"
else
    r = 4
    s = "4"
    while r < n do
        if func(n-r) != "-1" then
            s += func(n-r)
            r = n
        elsif (r + 7) == n then
            r += 7
            s += "7"
        else
            r += 4
            s += "4"
        end
    end
    if r > n then s = "-1"
    end
    print "#{s}\n"
end

sum = gets.chomp.to_i
q4 = sum/4
div4 = sum%4
q7 = sum - q4*4
div7 = (sum - q4*4)%7

if(div7==0)
  num = "4"*q4 + "7"*q7
  num = num.split("").sort.join
  puts num
else
  puts -1
end

sum = gets.chomp.to_i
div = 1
count = 0
while sum>0
  sum-=4
  count+=1
  if(sum%7==0)
    puts ("4"*count + "7"*(sum/7)).split("").sort.join
    exit
  end
end
puts -1

sum = gets.chomp.to_i
q7 = sum/7
div7 = sum%7
q4 = (sum - q7*7)/4
div4 = (sum-q7*7)%4

if(div4==0)
  num = "4"*q4 + "7"*q7
  num = num.split("").sort.join
  puts num
else
  puts -1
end

require 'mathn'
sum = gets.chomp.to_i
q7 = (sum/7).floor
div7 = sum%7
q4 = ((sum - q7*7)/4).floor
div4 = (sum-q7*7)%4

if(div4==0)
  num = "4"*q4 + "7"*q7
  num = num.split("").sort.join
  puts num
else
  puts -1
end

# def lucky(four,seven,n)
# 	if n == 0
# 		# puts "here"
# 		@fo = four
# 		@sev = seven
# 		return true
# 	end
# 	return false if n < 0
# 	if lucky(four,seven+1,n-7) || lucky(four+1,seven,n-4)
# 		return true
# 	else
# 		return false
# 	end
# end

# @fo = 0
# @sev = 0

# n = gets.to_i

# # puts lucky(0,0,n)
# if lucky(0,0,n)
# 	puts "4" * @fo + "7" * @sev
# else
# 	puts "-1"
# end

n = gets.to_i

b = 0
min_a = 10 ** 5
min_b = 10 ** 5
min_ab = 10 ** 5

a = 0
ans = false
# a * 4 + b * 7 = n
while a * 4 <= n
	b = (n - (a * 4))/7
	if (a * 4 + b * 7 == n) && ((a + b < min_ab) || (a + b == min_ab && b < min_b))
		min_a = a
		min_b = b
		min_ab = a + b
		ans = true
	end
	a += 1
end

if ans
	puts "4" * min_a + "7" * min_b
else
	puts "-1"
end


def main
  t = gets.chomp.to_i
  t.times { solve }
end

def solve
  n, m = gets.chomp.split.map(&:to_i)
  a = gets.chomp.split.map(&:to_i)
  # S = gets.chomp.chars

  sum = 0
  0.upto(n-1).each do |i|
    sum += a[i]
  end
  a0 = [sum, m].min # min(sum, m) is a0
  p a0
end

main

#! /usr/bin/env ruby
# encoding: UTF-8

def solve
	n,m = gets.split.map{|x| x.to_i }
	xs = gets.split.map {|x| x.to_i }
	sum = xs.reduce(:+)
	puts([m, sum].min)
end

t = gets.to_i
t.times {solve}

T = gets.to_i

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

  sum = a.inject(:+)

  puts [m, sum].min
end

t = gets.to_i
t.times do
  n,m = gets.split.map(&:to_i)
  a = gets.split.map(&:to_i)
  p [a.inject(:+), m].min
end

def read_input()

   rules = STDIN.gets()
   students = STDIN.gets()

   rules = rules.split(' ').map(&:to_i)
   students = students.split(' ').map(&:to_i)

   raise "Invalid rules input" if rules.size != 2
   raise "Invalid input" if students.size != rules[0]

   return rules, students

end

def do_some_magic(cnt)
    cnt.times {

        rules, students = read_input()

        oth_cnt = students.size - 1

        # we need this num of points
        x = rules[1] - students[0]

        # available this num of points
        y = 0
        1.upto(oth_cnt) { |s|
            y += students[s]
        }

        # and this num of points we can add to first student
        z = y < x ? y : x

        puts students[0] + z

    }
end

cnt = Integer(STDIN.gets())

do_some_magic(cnt)


n,m,*a=$<.read.split.map &:to_i
s=q=0;r=[]
a.each{|e|s+=e;r<<s/m-q/m;q=s}
puts r*' '

def solve
  n, m = gets.strip.split.map(&:to_i)
  a = gets.strip.split.map(&:to_i)
  sum = 0
  pages = []
  a.each_with_index do |val, i|
    sum += val
    pages << (sum / m).to_i
    sum = sum % m if sum >= m
  end
  puts pages.join(' ')
end

solve

N, M = gets.split.map(&:to_i)
as = gets.split.map(&:to_i)
sum = 0
ans = []
N.times do |i|
    ans[i] = (sum+as[i])/M - sum/M
    sum += as[i]
end
puts ans.join(' ')

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

ans = Array.new(n, 0)
cu=m
0.upto(n-1).each do |i|
  ai = ais[i]
  if ai<cu
    cu-=ai
  elsif ai==cu
    cu=m
    ans[i]+=1
  else # ai>cu
    rai = ai-cu
    ans[i]+=1
    ans[i]+= (rai/m)
    cu = m - (rai%m)
    cu = m if cu==0
  end
end

puts ans.join(' ')

def single_input
	gets.chomp.to_i
end

def multiple_input
	gets.chomp.split(' ').map(&:to_i)
end

n, m = multiple_input
names = multiple_input

rem = m
ac = []
n.times do |i|
	if names[i] < rem
		rem -= names[i]
		ac << 0
		next
	end

	if rem == names[i]
		rem = m
		ac << 1
		next
	end

	x = (names[i]-rem) / m
	rem = (names[i]-rem) % m
	rem = m - rem
	ac << (x + 1)
end

print ac.map(&:to_s).join(" ")

def single_input
	gets.chomp.to_i
end

def multiple_input
	gets.chomp.split(' ').map(&:to_i)
end

n, m = multiple_input
names = multiple_input

rem = m
ac = []
n.times do |i|
	if names[i] < rem
		rem -= names[i]
		ac << 0
		next
	end

	if rem == names[i]
		rem = 0
		ac << 1
		next
	end

	x = (names[i]-rem) / m
	rem = (names[i]-rem) % m
	rem = m - rem
	ac << (x + 1)
end

print ac.map(&:to_s).join(" ")

def single_input
	gets.chomp.to_i
end

def multiple_input
	gets.chomp.split(' ').map(&:to_i)
end

n, m = multiple_input
names = multiple_input

rem = m
ac = []
n.times do |i|
	if names[i] < rem
		rem -= names[i]
		ac << 0
		next
	end

	if rem == names[i]
		rem = 0
		ac << 1
		next
	end

	x = (names[i]-rem) / m
	rem = (names[i]-rem) % m
	rem = m - rem
	ac << (x + 1)
end

p ac.map(&:to_s).join(" ")


n = gets.to_i
a = gets.split.map(&:to_i).sort
ans = []
c = 0
a.each do |v|
  if c % 2 == 0
    ans.push(v)
  else
    ans.unshift(v)
  end
  c += 1
end
puts ans.join(" ")

N = gets.to_i
X = gets.split.map(&:to_i).sort

o = X.each_with_index.select{|e,i| i.odd?}.map(&:shift)
o += X.each_with_index.select{|e,i| i.even?}.map(&:shift).reverse

puts o*" "

# #pragma GCC optimize ("O3")
# #include "bits/stdc++.h"
#
# using namespace std;
# using ll = long long int;
#
# #define debugos cout
# #define debug(v) {printf("L%d %s > ",__LINE__,#v);debugos<<(v)<<endl;}
# #define debugv(v) {printf("L%d %s > ",__LINE__,#v);for(auto e:(v)){debugos<<e<<" ";}debugos<<endl;}
# #define debuga(m,w) {printf("L%d %s > ",__LINE__,#m);for(int x=0;x<(w);x++){debugos<<(m)[x]<<" ";}debugos<<endl;}
# #define debugaa(m,h,w) {printf("L%d %s >\n",__LINE__,#m);for(int y=0;y<(h);y++){for(int x=0;x<(w);x++){debugos<<(m)[y][x]<<" ";}debugos<<endl;}}
# #define ALL(v) (v).begin(),(v).end()
# #define repeat(cnt,l) for(remove_reference<remove_const<decltype(l)>::type>::type cnt=0;(cnt)<(l);++(cnt))
# #define rrepeat(cnt,l) for(auto cnt=(l)-1;0<=(cnt);--(cnt))
# #define iterate(cnt,b,e) for(auto cnt=(b);(cnt)!=(e);++(cnt))
# #define diterate(cnt,b,e) for(auto cnt=(b);(cnt)!=(e);--(cnt))
# const ll MD = 1000000007ll; const long double PI = 3.1415926535897932384626433832795L;
# inline void assert_call(bool assertion, function<void()> f) { if (!assertion) { cerr << "assertion fault:" << endl; f(); abort(); } }
# template<typename T1, typename T2> inline ostream& operator <<(ostream &o, const pair<T1, T2> p) { o << '(' << p.first << ':' << p.second << ')'; return o; }
# template<typename Vec> inline ostream& _ostream_vecprint(ostream& os, const Vec& a) {
#     os << '['; for (const auto& e : a) os << ' ' << e << ' '; os << ']'; return os;
# }
# template<typename T> inline ostream& operator<<(ostream& o, const vector<T>& v) { return _ostream_vecprint(o, v); }
# template<typename T, size_t S> inline ostream& operator<<(ostream& o, const array<T, S>& v) { return _ostream_vecprint(o, v); }
# template<typename T> inline T& chmax(T& to, const T& val) { return to = max(to, val); }
# template<typename T> inline T& chmin(T& to, const T& val) { return to = min(to, val); }
# void bye(string s, int code = 0) { cout << s << endl; exit(code); }
# mt19937_64 randdev(8901016);
# template<typename T> inline T rand(T l, T h) { return uniform_int_distribution<T>(l, h)(randdev); }
# template<> inline double rand<double>(double l, double h) { return uniform_real_distribution<double>(l, h)(randdev); }
# template<> inline float rand<float>(float l, float h) { return uniform_real_distribution<float>(l, h)(randdev); }
#
# #if defined(_WIN32) || defined(_WIN64)
# #define getchar_unlocked _getchar_nolock
# #define putchar_unlocked _putchar_nolock
# #elif defined(__GNUC__)
# #else
# #define getchar_unlocked getchar
# #define putchar_unlocked putchar
# #endif
# namespace {
# #define isvisiblechar(c) (0x21<=(c)&&(c)<=0x7E)
#     class MaiScanner {
#     public:
#         template<typename T> void input_integer(T& var) {
#             var = 0; T sign = 1;
#             int cc = getchar_unlocked();
#             for (; cc < '0' || '9' < cc; cc = getchar_unlocked())
#                 if (cc == '-') sign = -1;
#             for (; '0' <= cc && cc <= '9'; cc = getchar_unlocked())
#                 var = (var << 3) + (var << 1) + cc - '0';
#             var = var * sign;
#         }
#         inline int c() { return getchar_unlocked(); }
#         inline MaiScanner& operator>>(int& var) { input_integer<int>(var); return *this; }
#         inline MaiScanner& operator>>(long long& var) { input_integer<long long>(var); return *this; }
#         inline MaiScanner& operator>>(string& var) {
#             int cc = getchar_unlocked();
#             for (; !isvisiblechar(cc); cc = getchar_unlocked());
#             for (; isvisiblechar(cc); cc = getchar_unlocked())
#                 var.push_back(cc);
#             return *this;
#         }
#         template<typename IT> void in(IT begin, IT end) { for (auto it = begin; it != end; ++it) *this >> *it; }
#     };
#     class MaiPrinter {
#     public:
#         template<typename T>
#         void output_integer(T var) {
#             if (var == 0) { putchar_unlocked('0'); return; }
#             if (var < 0)
#                 putchar_unlocked('-'),
#                 var = -var;
#             char stack[32]; int stack_p = 0;
#             while (var)
#                 stack[stack_p++] = '0' + (var % 10),
#                 var /= 10;
#             while (stack_p)
#                 putchar_unlocked(stack[--stack_p]);
#         }
#         inline MaiPrinter& operator<<(char c) { putchar_unlocked(c); return *this; }
#         inline MaiPrinter& operator<<(int var) { output_integer<int>(var); return *this; }
#         inline MaiPrinter& operator<<(long long var) { output_integer<long long>(var); return *this; }
#         inline MaiPrinter& operator<<(char* str_p) { while (*str_p) putchar_unlocked(*(str_p++)); return *this; }
#         inline MaiPrinter& operator<<(const string& str) {
#             const char* p = str.c_str();
#             const char* l = p + str.size();
#             while (p < l) putchar_unlocked(*p++);
#             return *this;
#         }
#         template<typename IT> void join(IT begin, IT end, char sep = ' ') { for (bool b = 0; begin != end; ++begin, b = 1) b ? *this << sep << *begin : *this << *begin; }
#     };
# }
# MaiScanner scanner;
# MaiPrinter printer;
#
#
# //
#
# /*
# あらかじめソート
#
# 低い人->高い人 を選択
# 高い人->低い人 を未選択する
#
# これを動的計画法する
# */
#
# int N;
# ll A[111];
#
# ll dp[111][111];
#
# int main() {
#     scanner >> N;
#     scanner.in(A, A + N);
#     sort(A, A + N);
#
#     fill(dp[0], dp[110], ll(1e18));
#     dp[0][0] = 0;
#
#     int M = N - 1;
#     iterate(i, 0, N) {
#         iterate(j, 0, i+1) {
#             ll d = 0;
#             int jp = j;
#             iterate(k, i+1, N) {
#                 chmin(dp[k][j], max(dp[i][j], max(d, A[k] - A[i])));
#
#                 chmax(d, A[k] - A[jp]);
#                 jp = k;
#             }
#         }
#     }
#     ll ans = 1e18;
#     repeat(i, N) {
#         chmin(ans, max(dp[N-1][i], A[N - 1] - A[i]));
#     }
#
#     debugaa(dp, N+1, N+1);
#
#     cout << ans << endl;
#
#     return 0;
# }

if File.exists?("input")
  $stdin = File.open("input")
  $stdout = File.open("output","w")
end

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

def getdisc(arr)
	max = 0
	arr[1..-1].each_with_index do |x,i|
		asd = (x-arr[i-1]).abs
		max = asd if asd>max
	end
	asd = (arr[-1]-arr[0]).abs
	max=asd if asd>max
	return max
end

def newgetdisc(arr)
	max = 0
	arr[1..-1].each_with_index do |x,i|
		asd = (x-arr[i-1]).abs
		max+= asd
	end
	asd = (arr[-1]-arr[0]).abs
	max+=asd# if asd>max
	return max
end

def swap(arr,a,b)
	temp  = arr[a]
	arr[a]=arr[b]
	arr[b]=temp
	return arr
end

#newarr = []
#disc =

#p arr
=begin
arr.each do |x|
	if newarr.length==0
		newarr.push x
	else
		scorearr = []
		(0..newarr.length).each do |insloc|
			newnewarr = newarr.dup
			newnewarr.insert(insloc,x)
			scorearr.push newgetdisc(newnewarr)
		end
		theloc = scorearr.index(scorearr.min)
		newarr.insert(theloc,x)
	end
	p newarr
end
=end
=begin
newarr = arr.dup

while true
	currscore = getdisc(newarr)
	tobreak = false
	newarr.each_with_index do |x,i|
		newarr[i+1..-1].each_with_index do |y,j|
			newnewarr = newarr.dup
			newnewarr = swap(newnewarr,i,j)
			if(getdisc(newnewarr)<currscore)
				newarr=newnewarr.dup
				tobreak = true
				break
			end
		end
		break if tobreak
	end

	break if not tobreak
end
p newarr
=end

arr.sort!
newarr = []
arr.each_with_index do |x,i|
	newarr.push x if i%2==0
end

arr.reverse!

if arr.length%2==0
	arr.each_with_index do |x,i|
		newarr.push x if i%2==0
	end
else
	arr.each_with_index do |x,i|
		newarr.push x if i%2==1
	end
end

newarr.each do |x|
	print x
	print " "
end
puts

DBG = true
n = gets.to_i
a = gets.split.map{|z| z.to_i}
a.sort!
m = n/2
b = Array.new(n)
for i in 0...m
  b[i] = a[2*i]
  b[n-1-i] = a[2*i+1]
end
if n%2 == 1
  b[m] = a[n-1]
end
for i in 0...n
  print "#{b[i]} "
end
puts ""


n, m = gets.split(' ').map(&:to_i)
words = {}

1.upto(m) do
  k, v = gets.chomp.split(' ')
  words[k] = v
end

result = gets.chomp.split(' ').map { |s| s.length > words[s].length ? words[s] : s }

puts result.join(' ')

n, m = gets.split.map &:to_i
dict = Hash.new
m.times do
	a, b = gets.split
	dict[a] = b.size < a.size ? b : a
end
puts gets.split.map { |_| dict[_] }.join(' ')

n, m = gets.split.map{|x| x.to_i}
h = {}
m.times{h.merge!(Hash[*gets.split])}
puts(gets.split.map{|x| x.size <= h[x].size ? x : h[x]}.join(' '))

q=[]
j=0
gets.split.each do |i|
q[j]=i.to_i
j=j+1
end
hash=Hash[]

q[1].times do
  s=gets.split
  hash[s[0]]=s[1]
end

gets.split.each do |i|
  if i.length <= hash[i].length
    print i + " "
  else
    print hash[i]+" "
  end
end

# http://codeforces.com/problemset/problem/499/B

n, m = gets.chomp.split.map(&:to_i)
lang_1 = []
lang_2 = []

m.times do
  words = gets.chomp.split
  lang_1 << words[0]
  lang_2 << words[1]
end

lecture = gets.chomp.split
note = []

for word in lecture
  index = lang_1.index(word)
  replace_word = [word, lang_2[index]].min_by(&:length)
  note << replace_word
end

puts note.join(" ")

# 499B

def read_line
  gets.chomp.split
end

w = {}
read_line[1].to_i.times do
  k, v = read_line
  w[k] = v
end
puts read_line.map { |k| v = w[k]; v.size < k.size ? v : k } * ' '

# f = File.open('input.txt', 'r')
f = STDIN
n, m = f.gets.chomp.split.map &:to_i
dict1, dict2 = {}, {}
m.times do
  s1, s2 = f.gets.chomp.split
  dict1[s1] = s2
  dict2[s2] = s1
end

strs = f.gets.chomp.split
t = strs.map do |s|
  if dict1[s]
    dict1[s].length < s.length ? dict1[s] : s
  else
    dict2[s].length < s.length ? dict2[s] : s
  end
end

puts t.join(' ')

class Array
	def __min
		self[0].length <= self[1].length ? self[0] : self[1]
	end
end

n, m = gets.split.map(&:to_i)
dict = {}
m.times do
	a, b = gets.split
	dict[a] = [a, b].__min
end
puts gets.split.map{|w| dict[w]}.join(' ')

n, m = gets.split.map(&:to_i)
d = Hash.new
m.times do
  a, b = gets.split
  d[a] = if a.length <= b.length; a; else b; end
end
puts gets.split.map {|w| d[w]}.join(' ')

n, m = gets.strip.split.map {|n| n.to_i}
lang1 = []
lang2 = []
m.times do
	w1, w2 = gets.strip.split.map {|n| n}
	lang1.push(w1)
	lang2.push(w2)
end
notes = ""
lec = gets.strip.split(" ")
lec.each do |word|
	i = lang1.index(word)
	notes += lang1[i].size <= lang2[i].size ? lang1[i] + " " : lang2[i]+ " "
end
puts notes

hash = Hash.new;
n, m = gets.chomp.split.map(&:to_i);

m.times do
  a, b = gets.chomp.split;
  hash[a] = b.length < a.length ? b : a;
end
gets.chomp.split.each {|x| print hash[x] + " "};

n, m = gets.chomp.split.map(&:to_i)
a={}
m.times do
  ai, bi = gets.chomp.split
  a[ai] = bi
end
ans=''

gets.chomp.split.each do |s|
  ans += (s.length <= a[s].length ? s : a[s]) + ' '
end
puts ans

n,m =gets.split().map(&:to_i)
dic = {}
m.times do
  a,b = gets.split(" ")
  dic[a] =b
end

val = gets.split(" ")
res = ''
val.each do |i|
  if i.size > dic[i].size
    res +=dic[i]
  else
    res +=i
  end
  res +=' '
end
puts res

n,m=gets.split.map{|e| e.to_i}
h={}
m.times{
	a,b=gets.chomp.split
	if ((a.size)<=>(b.size))<1 then
		h[a]=a
		h[b]=a
	else
		h[a]=b
		h[b]=b
	end
}
s=gets.chomp.split
i=0
s.each{|e|
	print " " if i>0
	print h[e]
	i+=1
}
print "\n"

#$stdin = File.open "input.txt"

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

replace = {}
m.times do
	a, b = gets.chomp.split
	replace[a] = b.size < a.size ? b : a
end

puts gets.chomp.split.map { |s| replace[s] }.join(" ")

#!/usr/bin/ruby
class InputReader
    def initialize
        @data = []
        @offset = 0
    end

    def next
        if @data.empty? || @offset >= @data.length
            line = STDIN.gets.chomp
            return nil if line.empty?
            @data = line.split(/\s+/)
            @offset = 0
        end
        @offset += 1
        return @data[@offset-1]
    end
end

# ===== Your solution goes here =====
reader = InputReader.new
n = reader.next.to_i
m = reader.next.to_i
dict = {}
m.times do
    k = reader.next
    v = reader.next
    dict[k] = v
end

n.times do
    word = reader.next
    if word.length <= dict[word].length
        print "#{word} "
    else
        print "#{dict[word]} "
    end
end

n, m = gets.split(' ').map(&:to_i)
dic = Hash[]
m.times do
  word = gets.split
  dic[word.first] = word.min_by &:length
end
puts gets.split.map {|w| dic[w]}.join(' ')

nums = gets.chomp.split
n = Integer(nums[0])
m = Integer(nums[1])
best = Array.new
bad = Array.new
input = Array.new
for i in 0...m
    input[i] = Array.new
    input[i] = gets.chomp.split
    if input[i][0].length <= input[i][1].length
        best.push(input[i][0])
        bad.push(input[i][1])
    else
        best.push(input[i][1])
        bad.push(input[i][0])
    end
end
lecture = Array.new
lecture = gets.chomp.split

note = {}
for i in 0...m
    note[best[i]] = best[i]
    note[bad[i]] = best[i]
end
best_note = []
for i in 0...n
    best_note.push(note[lecture[i]])
end

print best_note.join(" ")

nm = gets.chomp.split
n = nm[0].to_i
m = nm[1].to_i
ab = []
a = []
b = []
for i in (0..m-1)
	ab[i] = gets.chomp.split
	a[i] = ab[i][0]
	b[i] = ab[i][1]
	if a[i].size <= b[i].size
		b[i] = nil
	end
end
c = gets.chomp.split

for i in (0..n-1)
	if b[a.find_index(c[i])] == nil
		print a[a.find_index(c[i])] + " "
	else
		print b[a.find_index(c[i])] + " "
	end
end

n,m = gets.split(" ").map(&:to_i)
words = {}
m.times do
	a,b = gets.split(" ")
	words[a] =  b
end
lec = gets.split(" ")
op = []
lec.each do |a|
	if a.length > words[a].length
		op << words[a]
	else
		op << a
	end
end

puts op.join(" ")

n, m = gets.chomp.split(' ').map(&:to_i)
a = {}
m.times do
  ai, bi = gets.chomp.split ' '
  a[ai] = bi
end

res = ''

gets.chomp.split(' ').each do |s|
  res += (s.length <= a[s].length ? s : a[s]) + ' '
end
puts res

n,m = gets.split.map &:to_i
dic = Hash[]
m.times do
  w = gets.split
  dic[w.first] = w.min_by &:length
end
puts gets.split.map {|w| dic[w]}.join(' ')

n, m = gets.split.map(&:to_i)
a, b = m.times.map { gets.split }.transpose
puts gets.split.map { |x| [x, b[a.index(x)]].min_by(&:size) }.join(' ')

line = gets.chop!.split(" ")

n = line[0].to_i
m = line[1].to_i

A = {}
B = {}

for i in (1 .. m)
    line = gets.chop!.split(" ")

    first = line[0]
    second = line[1]

    B[first] = i
    B[second] = i

    if second.size < first.size
        A[i] = second
    elsif second.size > first.size
        A[i] = first
    else
        A[i] = first
    end
end

line = gets.chop!.split(" ")

for i in (1 .. n)
    print A[B[line[i-1]]]

    if i < n
        print " "
    end
end

hash = Hash.new;
n, m = gets.chomp.split.map(&:to_i);

m.times do
  a, b = gets.chomp.split;
  hash[a] = b.length < a.length ? b : a;
end

p gets.chomp.split.inject(){ |sum, x|
  # p "#{sum}, #{x}, #{hash[x]}";
  sum + " " + hash[x];
}

nm = gets.chomp.split
n = nm[0].to_i
m = nm[1].to_i
ab = []
a = []
b = []
for i in (0..m-1)
	ab[i] = gets.chomp.split
	a[i] = ab[i][0]
	b[i] = ab[i][1]
	if a[i].size < b[i].size
		b[i] = nil
	end
end
c = gets.chomp.split

for i in (0..n-1)
	if b[a.find_index(c[i])] == nil
		print a[a.find_index(c[i])] + " "
	else
		print b[a.find_index(c[i])] + " "
	end
end

n, m = gets.chomp.split(' ').map(&:to_i)
a = {}
m.times do
  ai, bi = gets.chomp.split ' '
  a[ai] = bi
end

res = ''

gets.chomp.split(' ').each do |s|
  res += (s.length >= a[s].length ? s : a[s]) + ' '
end
puts res

line = gets.chop!.split(" ")

n = line[0].to_i
m = line[1].to_i

A = {}
B = {}

for i in (1 .. m)
    line = gets.chop!.split(" ")

    first = line[0]
    second = line[1]

    B[first] = i
    B[second] = i

    if second.size < first.size
        A[i] = second
    elsif second.size > first.size
        A[i] = first
    else
        if second < first
            A[i] = second
        else
            A[i] = first
        end
    end
end

line = gets.chop!.split(" ")

for i in (1 .. n)
    print A[B[line[i-1]]]

    if i < n
        print " "
    end
end

line = gets.chop!.split(" ")

n = line[0].to_i
m = line[1].to_i

A = {}
B = {}

for i in (1 .. m)
    line = gets.chop!.split(" ")

    first = line[0]
    second = line[1]

    B[first] = i
    B[second] = i

    if second < first
        A[i] = second
    else
        A[i] = first
    end
end

line = gets.chop!.split(" ")

for i in (1 .. n)
    print A[B[line[i-1]]]

    if i < n
        print " "
    end
end

# http://codeforces.com/problemset/problem/499/B

n, m = gets.chomp.split.map(&:to_i)
lang_1 = []
lang_2 = []

m.times do
  words = gets.chomp.split
  lang_1 << words[0]
  lang_2 << words[1]
end

text_str = gets.chomp
text_arr = text_str.split

for i in 0..(m - 1)
  if !text_arr.include?(lang_1[i])
    next
  end

  word = lang_1[i].length <= lang_2[i].length ? lang_1[i] : lang_2[i]
  text_str.gsub!(lang_1[i], word)
end

puts text_str