Score : 100 points

Problem Statement

Print the circumference of a circle of radius R.

Constraints

1 \leq R \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

R

Output

Print the circumference of the circle.
Your output is considered correct if and only if its absolute or relative error from our answer is at most 10^{-2}.

Sample Input 1

1

Sample Output 1

6.28318530717958623200

Since we accept an absolute or relative error of at most 10^{-2}, 6.28 is also an acceptable output, but 6 is not.

Sample Input 2

73

Sample Output 2

458.67252742410977361942


Global OOD problem IDs are disjoint from train, validation, and in-distribution test problem IDs.

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.


# set
r = gets.chomp.to_i
answer = 0

# main
answer = 2 * r * Math::PI
print ("#{answer}\n")


R = gets.chomp.to_i
puts R * 2 * Math::PI


r = gets.to_i

puts ((r * 2) * 3.14).to_f


n = gets.chomp.chars
if n[0] == 7 || n[1] == 7 || n[2] == 7
  puts "Yes"
else
  puts "No"
end

Score : 100 points

Problem Statement

Given is a three-digit integer N. Does N contain the digit 7?

If so, print Yes; otherwise, print No.

Constraints

100 \leq N \leq 999

Input

Input is given from Standard Input in the following format:

N

Output

If N contains the digit 7, print Yes; otherwise, print No.

Sample Input 1

117

Sample Output 1

Yes

117 contains 7 as its last digit.

Sample Input 2

123

Sample Output 2

No

123 does not contain the digit 7.

Sample Input 3

777

Sample Output 3

Yes


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

if(n[0] == 7 || n[1] == 7 || n[2] == 7)
  puts "Yes"
else
  puts "No"
end


num = gets.chomp

puts num.include?("7") ? "YES" : "NO"


# frozen_string_literal: true

n = gets.chomp.chars

puts n.index('7') ? :Yes : :No


a = gets.to_s.chars

if a.include? ("7")
  puts "Yes"
else
  puts "No"
end


line1 = gets.strip
s=line1.include?('7') ? 'Yes' : 'No'
puts s


N = gets
puts N.count('7') > 0 ? 'Yes' : 'No'


n = gets.chomp.to_i

puts n.to_s.include?('7') ? 'Yes' : 'No'


N = gets.chomp.chars
if N.include?('7')
  puts 'Yes'
else
  puts 'No'
end


n = gets.chomp.split("")
# p n
if n.include?("7")
  puts "Yes"
else
  puts "No"
end


puts gets.to_i.digits.any? {|i| i == 7} ? 'Yes' : 'No'


def get_i() #空白区切の入力を数値(整数)の配列で返す
  return gets.chomp.split(" ").map(&:to_i)
end
def get_f() #空白区切の入力を数値(実数)の配列で返す
  return gets.chomp.split(" ").map(&:to_f)
end
def get() #空白区切の入力を文字列の配列で返す
  return gets.chomp.split(" ")
end
def get_nsp() #入力されたものを一文字ずつに区切った文字列の配列で返す
  return gets.chomp.split("")
end
def yn_judge(bool,y="Yes",n="No") #boolに真偽を投げることで、trueならy、falseならnの値を出力する
  return bool ? y : n
end
def array(size1,init=nil,size2=-1) #size2に二次元配列時の最初の要素数、size1に次の配列の大きさ、initに初期値を投げることでその配列を返す
  if size2==-1
    return Array.new(size1){init}
  else
    return Array.new(size2){Array.new(size1){init}}
  end
end

N=get_nsp
puts yn_judge(N.include?("7"))


k = gets.chomp.to_i
sum = 0

(1..k).each do |x|
  (1..k).each do |y|
    (1..k).each do |z|
      sum += x.gcd(y).gcd(z)
    end
  end
end

puts sum

Score : 300 points

Problem Statement

Find \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K} \gcd(a,b,c)}.

Here \gcd(a,b,c) denotes the greatest common divisor of a, b, and c.

Constraints

1 \leq K \leq 200

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print the value of \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K} \gcd(a,b,c)}.

Sample Input 1

2

Sample Output 1

9

\gcd(1,1,1)+\gcd(1,1,2)+\gcd(1,2,1)+\gcd(1,2,2)
+\gcd(2,1,1)+\gcd(2,1,2)+\gcd(2,2,1)+\gcd(2,2,2)
=1+1+1+1+1+1+1+2=9

Thus, the answer is 9.

Sample Input 2

200

Sample Output 2

10813692


a=(1..gets.to_i).to_a.repeated_permutation(3).to_a

puts a.map{|n| n[0].gcd(n[1]).gcd(n[2])}.inject(:+)


a=(1..gets.to_i).to_a.repeated_permutation(3).to_a

puts a.map{|n| n[0].gcd(n[1]).gcd(n[2])}.sum


num = gets.to_i
arr = []
count = 0
(1..num).each do |i|
  (1..num).each do |j|
    (1..num).each do |k|
      arr.push [i,j,k]
    end
  end
end
p arr
(0..arr.length-1).each do |i|
  count += arr[i].inject{|a, b| a.gcd(b)}
end
p count


k = gets.to_i
result = 0
MEMO = {}
1.upto(k) do |i|
  MEMO[i] ||= {}
  MEMO[i][i] = i
  (i+1).upto(k) do |j|
    MEMO[i][j] = i.gcd(j)
  end
end

def gcd a,b,c
  ab = MEMO[a][b] || MEMO[b][a]
  return 1 if ab == 1
  return MEMO[ab][c] || MEMO[c][ab]
end
result += 1 + (k - 1) * (k-1) * 3 +  (k-1) * 3
list = (2..k).to_a
list.each do |a|
  list.each do |b|
    ab = a.gcd(b)
    if ab == 1
      result += k - 1
    else
      list.each do |c|
        result += ab.gcd(c)
      end
    end
  end
end
puts result


def gcd(a, b)
   if b == 0
      return a
   else
      return gcd(b, a%b);
    end
end

x = Array.new(201) {Array.new(201)}
k = gets.to_i
sum = 0

1.upto(200) do |a|
    a.upto(200) do |b|
        x[a][b] = gcd(a,b)
        x[b][a] = gcd(a,b)
    end
end

1.upto(k) do |a|
    1.upto(k) do |b|
        1.upto(k) do |c|
            sum += gcd(x[a][b], x[b][c])
        end
    end
end

puts sum


k = gets.to_i
ans = 0

1.upto(k) do |a|
  1.upto(k) do |b|
    1.upto(k) do |c|
      ans += a.gcd(b.gcd(c))
    end
  end
end

puts ans


k = gets.to_i
ans = 0
(1..k).to_a.each do |a|
  (1..k).to_a.each do |b|
    (1..k).to_a.each do |c|
      ans += a.gcd(b.gcd(c))
    end
  end
end
puts ans


n = gets.to_i
su = 0
(1..(n)).each do |i|
	(1..(n)).each do |j|
 		(1..(n)).each do |k|
          su += (i.gcd(j)).gcd(k)
	  	end
	end
end
puts su


n = gets.to_i
S = gets.chomp.split('')
n_r = S.count('R')
n_g = S.count('G')
n_b = S.count('B')
ans = 0
(0..n-1).each do |i|
  ((i + 1)..n-1).each do |j|
    next if S[i] == S[j]

    k = (j - i) + j
    next if k > n - 1
    next if S[i] == S[k] || S[j] == S[k]
    ans += 1
  end
end
puts n_r * n_g * n_b - ans

Score : 400 points

Problem Statement

We have a string S of length N consisting of R, G, and B.

Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions:

S_i \neq S_j, S_i \neq S_k, and S_j \neq S_k.

j - i \neq k - j.

Constraints

1 \leq N \leq 4000

S is a string of length N consisting of R, G, and B.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of triplets in question.

Sample Input 1

4
RRGB

Sample Output 1

1

Only the triplet (1,~3,~4) satisfies both conditions. The triplet (2,~3,~4) satisfies the first condition but not the second, so it does not count.

Sample Input 2

39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB

Sample Output 2

1800


n = gets.chomp.to_i
s = gets.chomp.split('')

r, g, b = 0, 0, 0
reject_count = 0
s.each_with_index do |val_i, ind_i|
  if val_i == 'R'
    r += 1
  elsif val_i == 'G'
    g += 1
  else
    b += 1
  end

  s[ind_i+1..n-1].each.with_index(ind_i+1) do |val_j, ind_j|
    next if val_i == val_j

    ind_k = 2 * ind_j - ind_i
    reject_count += 1 if ind_k < n && s[ind_k] != val_i && s[ind_k] != val_j
  end
end

all = r * g * b

puts all - reject_count


def main
    n = gets.to_i
    s = gets.chomp.split("")
    rs = []
    gs = []
    bs = []
    s.each_with_index do |c, i|
        rs.push i if c == "R"
        gs.push i if c == "G"
        bs.push i if c == "B"
    end
    ans = 0
    rs.each do |r|
        gs.each do |g|
            min = [r,g].min
            max = [r,g].max
            b1 = min - (max - min)
            if (max + min) % 2 == 0
                b2 = (max + min) / 2
            else
                b2 = n + 100
            end
            b3 = max + (max - min)

            ng = 0
            ng += 1 if bs.include? b1
            ng += 1 if bs.include? b2
            ng += 1 if bs.include? b3

            ans += bs.size - ng
        end
    end
    puts ans
end

main


# frozen_string_literal: true

n = gets.to_i
s = gets.strip

count = 0

0.upto(n-2) do |i|
  (i+1).upto(n-1) do |j|
    k = 2 * j - i
    next if k >= n

    if s[i] != s[j] && s[i] != s[k] && s[j] != s[k]
      count += 1
    end
  end
end

r = 0
g = 0
b = 0

cr = 'R'
cg = 'G'
s.chars.each do |c|
  if c == cr
    r += 1
  elsif c == cg
    g += 1
  else
    b += 1
  end
end

puts r * g * b - count


# r * g * bを計算する
# その中からj-i = k-j を満たすものを引く
# 2j = i + k
n = gets.chomp.to_i
# s = gets.chomp.chars
s = gets.chomp

h = Hash.new(0)

c_map = {
    'R' => 0,
    'G' => 1,
    'B' => 2
}

s.each_char do |c|
  h[c_map[c]] += 1
end

ans = h[0] * h[1] * h[2]

0.upto(n-2) do |i|
  i.upto(n-1) do |j|
    k = 2 * j - i
    next if k >= n

    ans -= 1 if s[i] != s[j] && s[j] != s[k] && s[k] != s[i]
  end
end

p ans


n = gets.to_i
s = gets.chomp

ss = s.split("")

rs = []
gs = []
bs = []
ss.each.with_index(1) do |rgb, i|
 if rgb == "R"
  rs << i
 elsif rgb == "G"
  gs << i
 else
  bs << i
 end
end

ans = 0
rs.each do |r|
  gs.each do |g|
    rg = (r - g).abs
    max = [r, g].max
    min = [r, g].min
    ans += bs.length
    if max + rg - 1 < ss.length && ss[max + rg - 1] == 'B'
      ans -= 1
    end
    if rg % 2 == 0 && ss[max - rg/2 - 1] == 'B'
      ans -= 1
    end
    if min - rg - 1 > 0 && ss[min - rg - 1] == 'B'
      ans -= 1
    end
  end
end
p ans


n = gets.to_s.to_i
s = gets.to_s.chomp.chars

cnt = 0
n.times do |i|
  t = 1
  while i + 2 * t < n
    j = i + t
    k = j + t
    cnt += 1 if s[i] != s[j] && s[j] != s[k] && s[i] != s[k]
    t += 1
  end
end

ans = s.count('R') * s.count('G') * s.count('B') - cnt

puts ans


n = gets.to_i
s = gets
ans = r = g = b = 0
0.upto(n-1) do |i|
  r += 1 if s[i] == "R"
  g += 1 if s[i] == "G"
  b += 1 if s[i] == "B"
  (i+1).upto(n-1) do |j|
    k = j + j - i
    if k < n && s[i] != s[j] && s[i] != s[k] && s[j] != s[k]
      ans += 1
    end
  end
end
puts (r * g * b - ans)


n = gets.to_i
s = gets.chomp
r_cnt = 0
g_cnt = 0
b_cnt = 0

n.times do |i|
	if s[i] == "R"
		r_cnt += 1
	elsif	s[i] == "G"
		g_cnt += 1
	else
		b_cnt += 1
	end
end

ans = r_cnt * g_cnt * b_cnt

n.times do |i|
	n.times do |j|
		next if i >= j
		k = 2*j - i
		break if k >= n
		if s[i] != s[j] && s[j] != s[k] && s[k] != s[i]
			ans -= 1
		end
	end
end

puts ans


n = gets.to_i
@s = gets.chomp
r = 0
g = 0
b = 0
a = []
for i in 0..(n-1)
  if @s[i] == "R"
    r += 1
  elsif @s[i] == "G"
    g += 1
  else
    b += 1
  end
end

def sp(a,b,c)
  if @s[a] != @s[b] && @s[b] != @s[c] && @s[c] != @s[a]
    true
  else
    false
  end
end

x = 0
if sp(0,1,2)
  x = 1
else
  x = 0
end

dp = Array.new(n)
dp[2] = x

for j in 3..(n-1)
  y = 0
  for k in 1..(j/2)
    if sp(j,j-k,j-(2*k))
      y += 1
    end
  end
  dp[j] = dp[j-1] + y
end

puts (r*g*b) - dp[n-1]


n = gets.to_i
s = gets

a = ((0..(n-1)).to_a.combination(3).to_a.select do |i, j, k|
  j - i != k - j && s[i] != s[j]
end).select do |i, j, k|
 s[i] != s[k] && s[j] != s[k]
end

print a.size


n = gets.to_i
s = gets.chop
all = s.count('R') * s.count('G') * s.count('B')
n.times do |i|
  ml = [i, n-1-i].min
  (1..ml).each do |j|
    all -= 1 if s[i-j] != s[i+j] && s[i] != s[i+j] && s[i-j] != s[i]
  end
end
puts all


n = gets.to_i
str = gets.chomp.split('').map(&:to_s)
r_lsit = []
g_list = []
b_list = []
str.each_with_index do |s, idx|
  if s == "R"
    r_lsit << idx
  elsif s == "G"
    g_list << idx
  else
    b_list << idx
  end
end
ans = 0
r_lsit.each do |r|
  g_list.each do |g|
    b_list.each do |b|
      ans += 1 if (r - g).abs != (b - g).abs
    end
  end
end
puts ans


n = gets.to_i
s = gets.chomp
ans = 0
(0..n-3).each do |i|
  (i+1..n-2).each do |j|
    (j+1..n-1).each do |k|
      if j-i != k-j && s[i] != s[j] && s[i] != s[k] && s[j] != s[k]
        ans += 1
      end
    end
  end
end
puts ans


x,y,z = gets.chomp.split(' ').map(&:to_i)

box = [z,x,y]

puts box.join(" ")

Score : 100 points

Problem Statement

We have three boxes A, B, and C, each of which contains an integer.

Currently, the boxes A, B, and C contain the integers X, Y, and Z, respectively.

We will now do the operations below in order. Find the content of each box afterward.

Swap the contents of the boxes A and B

Swap the contents of the boxes A and C

Constraints

1 \leq X,Y,Z \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y Z

Output

Print the integers contained in the boxes A, B, and C, in this order, with space in between.

Sample Input 1

1 2 3

Sample Output 1

3 1 2

After the contents of the boxes A and B are swapped, A, B, and C contain 2, 1, and 3, respectively.

Then, after the contents of A and C are swapped, A, B, and C contain 3, 1, and 2, respectively.

Sample Input 2

100 100 100

Sample Output 2

100 100 100

Sample Input 3

41 59 31

Sample Output 3

31 41 59


x,y,z = gets.chomp.split(' ').map(&:to_i)
puts [z,x,y].join(' ')


x,y,z = gets.split(' ').map(&:to_i)
arr = {a:x, b:y, c:z}
# A=>B
rep = arr[:a]
arr[:a] = arr[:b]
arr[:b] = rep
# A=>C
rep = arr[:a]
arr[:a] = arr[:c]
arr[:c] = rep
puts  arr.values.join(' ')


x,y,z = gets.split(' ').map(&:to_i)
print z,' ',x,' ',y


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

puts "#{a} #{a} #{b}"


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

def swap(input, index1, index2)
    output = input.dup
    output[index1] = input[index2]
    output[index2] = input[index1]
    output
end

swap1 = swap(input, 0, 1)
swap2 = swap(swap1, 0, 2)
print swap2.join(' ')


x, y, z = gets.split.map!(&:to_i)

puts [z, x, y].join(" ")

load __FILE__ unless $stdin.eof?


a,b,c = gets.split.map(&:to_i)
puts "#{c} #{a} #{b}"


x, y, z = gets.chomp.split(" ").map(&:to_i)
puts "#{z} #{x} #{y}"


a,b,c=gets.split.map(&:to_i)
puts "#{c} #{a} #{b}"


x,y,z = gets.chomp.split.map(&:to_i)
puts [z,x,y].join(" ")


input = gets.split(" ")
m = input[1].to_i
arr_tokuhyousuu = gets.split(" ").map(&:to_i)
soutouhyousuu = arr_tokuhyousuu.sum

cnt_select = 0

arr_tokuhyousuu.each do |tokuhyousuu|
  if tokuhyousuu >= (soutouhyousuu/(4*m)).to_f
    cnt_select += 1
  end
end

if cnt_select >= m
  puts "Yes"
else
  puts "No"
end

Score : 200 points

Problem Statement

We have held a popularity poll for N items on sale. Item i received A_i votes.

From these N items, we will select M as popular items. However, we cannot select an item with less than \dfrac{1}{4M} of the total number of votes.

If M popular items can be selected, print Yes; otherwise, print No.

Constraints

1 \leq M \leq N \leq 100

1 \leq A_i \leq 1000

A_i are distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 ... A_N

Output

If M popular items can be selected, print Yes; otherwise, print No.

Sample Input 1

4 1
5 4 2 1

Sample Output 1

Yes

There were 12 votes in total. The most popular item received 5 votes, and we can select it.

Sample Input 2

3 2
380 19 1

Sample Output 2

No

There were 400 votes in total. The second and third most popular items received less than \dfrac{1}{4\times 2} of the total number of votes, so we cannot select them. Thus, we cannot select two popular items.

Sample Input 3

12 3
4 56 78 901 2 345 67 890 123 45 6 789

Sample Output 3

Yes


### SNIPPET
  # n = gets.split.map(&:to_i)
  # array = n.times.map { gets.split.map(&:to_i) }
  # [].all?(&:even?)
  # a = [*1..m].repeated_combination(n).to_a
  # [1,2,3,4,5].select { |num| num.even? }  # => [2, 4]
  # ["a","a","b"].group_by(&:itself).map{|k,v| [k, v.count]}.to_h #=> {"a"=>2, "b"=>1}
  # 切り捨て: .floor(2).to_f ,切り上げ： .ceil(2).to_f,四捨五入: round(2)
  # 3.upto(6) do |i|, 6.downto(3) do |i|
  # 公約数125.gcd(100)、公倍数125.lcm(100)
  # PI = Math::PI
  # 高さ = a * Math.sin(w / 180.0 * Math::PI), 底辺 = a * Math.cos(w / 180.0 * Math::PI)
  # def chmax(a, b) a > b ? a : b end
  # INF = Float::INFINITY
  # def chmin(a, b) a < b ? a : b end

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

puts a.select!{|x| x >= (a.sum / (4 * m))}.length >= m ? "Yes" : "No"


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

border = a.reduce(:+) / (4 * m)
c = a.count{ |x| x >= border }

c >= m ? print('Yes') : print('No')


n, m = gets.split.map(&:to_i)
products_vote = (gets.split.map(&:to_i)).sort!.reverse!
# if <= products.sum / (4 * m)
products_vote_sum = products_vote.sum
count = 0
#puts n, m
border =  products_vote_sum * (1.0 / (4.0 * m))
#puts  border

(0..n-1).each { |product|
  if border < products_vote[product]
    count += 1
  end
}

if count >= m
  puts "Yes"
else
  puts "No"
end


_n,m = gets.split.map(&:to_i)
a = gets.split.map(&:to_i).sort.reverse

puts a[m - 1] > a.inject(:+) / 4 / m ? 'Yes':'No'


n,m,*as=`dd`.split.map &:to_i
puts as.sort.reverse.first(m).min >= as.reduce(0) { |sum, i| sum + i }/(4*m) ? "Yes" : "No"


n, a = gets.chomp.split(" ").map(&:to_i)
k = gets.chomp.split(" ").map(&:to_i)
j = k.sort.reverse
#p j
all = 0
(1..n).each do |i|
    all += k[i-1]
end
#puts all
count =0
#p all.to_i / (4 * a)
(0..a-1).each do |d|
    if j[d].to_i >= all.to_i / (4 * a)
        count += 1
    end
end
#p count
if a == count.to_i
    puts "Yes"
else
    puts "No"
end


input_1 = gets.split.map!(&:to_i)
input_2 = gets.split.map!(&:to_f)
if input_2.select{ |num| num >= (input_2.inject(:+) / (4 * input_1[1])).to_f }.length < input_1[1] then
  puts "No"
else
  puts "Yes"
end


n,m=gets.chomp.split(" ").map(&:to_i);
line = gets.split(' ').map(&:to_i)

sum = line.inject(:+)
check = sum/4/m

# print(sum, "\n")
# print(check, "\n")
# puts(line)
n=0
q=0
line.each do |i|
    if check < i
        q += 1
    end
end

#puts(q)
if m <= q
    puts("Yes")
elsif m > q
    puts("No")
end


n,m = gets.split(" ").map(&:to_i)
list = gets.split(" ").map(&:to_i)

sum = 0

for value in 0...list.length do
  sum = sum + list[value]
end

pre = sum / (4.0 * m)

ans = list.select { |num| num >= pre }

if ans.length >= m  then
  puts "Yes"
else
  puts "No"
end


n, m = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
if a.sort.reverse[m - 1] >= a.reduce(:+) / (4 * m)
    puts 'Yes'
else
    puts 'No'
end


N, M = gets.chomp.split(" ").map(&:to_i)
A = gets.chomp.split(" ").map(&:to_i).sort.reverse
all = A.inject(:+)

count = 0

A.each do |a|
    if a >= all.quo(4 * M)
        count += 1
    end
    if count == M
        break
    end
end

if count == M
    puts "Yes"
else
    puts "No"
end


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

items = gets.chomp.split.map(&:to_i).sort_by {|i| -i }
all = items.sum
limit = all.to_f / (4 * m)

vo = items[m - 1]

puts(vo >= limit ? "Yes" : "No")


n,m=gets.split.map(&:to_f)
arr=gets.split.map(&:to_f).sort.reverse
sum=arr.inject(:+)
puts arr[0..m-1].each.all?{|a|a>=sum/(4*m)} ? 'Yes' : 'No'


_n, m = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
b = a.inject(:+).to_f / (4 * m)

cnt = 0
a.each do |ai|
  cnt += 1 unless ai < b
end
if cnt >= m
  puts 'Yes'
else
  puts 'No'
end


n, m = gets.split.map(&:to_i)
nums = gets.split.map(&:to_i).sort.reverse
sum = nums.reduce(:+)

t = nums[m-1]

puts t >= (sum / (4.0*m)) ? 'Yes' : 'No'


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

a.sort.reverse
x = a.inject(:+)
y = x / (4 * m)

if a.select { |k| k >= y }.size >= m
  puts 'Yes'
else
  puts 'No'
end


x, y, a, b, c = gets.split.map(&:to_i)

ps = gets.split.map(&:to_i).sort.reverse
qs = gets.split.map(&:to_i).sort.reverse
rs = gets.split.map(&:to_i).sort.reverse

ps = ps[0..x-1]
qs = qs[0..y-1]

arr = ps + qs + rs

arr.sort!
arr.reverse!

ans = 0
(x+y).times do |i|
    ans += arr[i]
end

puts ans

Score : 500 points

Problem Statement

You are going to eat X red apples and Y green apples.

You have A red apples of deliciousness p_1,p_2, \dots, p_A, B green apples of deliciousness q_1,q_2, \dots, q_B, and C colorless apples of deliciousness r_1,r_2, \dots, r_C.

Before eating a colorless apple, you can paint it red or green, and it will count as a red or green apple, respectively.

From the apples above, you will choose the apples to eat while making the sum of the deliciousness of the eaten apples as large as possible.

Find the maximum possible sum of the deliciousness of the eaten apples that can be achieved when optimally coloring zero or more colorless apples.

Constraints

1 \leq X \leq A \leq 10^5

1 \leq Y \leq B \leq 10^5

1 \leq C \leq 10^5

1 \leq p_i \leq 10^9

1 \leq q_i \leq 10^9

1 \leq r_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y A B C
p_1 p_2 ... p_A
q_1 q_2 ... q_B
r_1 r_2 ... r_C

Output

Print the maximum possible sum of the deliciousness of the eaten apples.

Sample Input 1

1 2 2 2 1
2 4
5 1
3

Sample Output 1

12

The maximum possible sum of the deliciousness of the eaten apples can be achieved as follows:

Eat the 2-nd red apple.

Eat the 1-st green apple.

Paint the 1-st colorless apple green and eat it.

Sample Input 2

2 2 2 2 2
8 6
9 1
2 1

Sample Output 2

25

Sample Input 3

2 2 4 4 4
11 12 13 14
21 22 23 24
1 2 3 4

Sample Output 3

74


red_to_eat, green_to_eat, num_red, num_green, num_white = gets.split.map(&:to_i)
reds = gets.split.map(&:to_i).sort
greens = gets.split.map(&:to_i).sort
whites = gets.split.map(&:to_i).sort

res = 0

(red_to_eat + green_to_eat).times do
  ary = []
  ary.push([whites[-1], :white]) if whites.length > 0
  ary.push([reds[-1], :red]) if reds.length > 0 and red_to_eat > 0
  ary.push([greens[-1], :green]) if greens.length > 0 and green_to_eat > 0

  tmp = ary.max_by {|el| el[0] }
  res += tmp[0]

  case tmp[1]
  when :white
    whites.pop
  when :red
    reds.pop
    red_to_eat -= 1
  when :green
    greens.pop
    green_to_eat -= 1
  end
end

puts res


X, Y, A, B, C = gets.chomp.split(" ").map(&:to_i)
red = gets.chomp.split(" ").map(&:to_i).max(X) #最大のＸ個の要素
grn = gets.chomp.split(" ").map(&:to_i).max(Y)
P = gets.split(" ").map(&:to_i).sort.reverse
RG = red + grn
ans = 0

(X + Y).times.inject(0) do |j, i|
    if j < P.size && P[j] > RG[i]
        ans += P[j]
        j + 1
    else
        ans += RG[i]
        j
    end
end
puts ans


(x,y),l,q,r=$<.map{|s|s.split.map &:to_i}
p (l.max(x)+q.max(y)+r).max(x+y).inject:+


x, y = gets.split.map(&:to_i)[0..1]
pvalues = gets.split.map(&:to_i).sort.reverse[0...x]
qvalues = gets.split.map(&:to_i).sort.reverse[0...y]
rvalues = gets.split.map(&:to_i).sort.reverse

plast = x - 1
qlast = y - 1
rvalues.each do |r|
  p = (plast >= 0) ? pvalues[plast] : nil
  q = (qlast >= 0) ? qvalues[qlast] : nil
  if p.nil? && q.nil?
    break
  elsif p.nil?
    which = :q
  elsif q.nil?
    which = :p
  else
    which = (p < q) ? :p : :q
  end

  if which == :p
    if p < r
      pvalues[plast] = r
      plast -= 1
    else
      break
    end
  else # which == :q
    if q < r
      qvalues[qlast] = r
      qlast -= 1
    else
      break
    end
  end
end

total = pvalues.sum + qvalues.sum
puts total


a,b,c = gets.split.map(&:to_i)
if (a + b)**2 < c**2
  puts 'Yes'
else
  puts 'No'
end

Score : 300 points

Problem Statement

Does \sqrt{a} + \sqrt{b} < \sqrt{c} hold?

Constraints

1 \leq a, b, c \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

a \ b \ c

Output

If \sqrt{a} + \sqrt{b} < \sqrt{c}, print Yes; otherwise, print No.

Sample Input 1

2 3 9

Sample Output 1

No

\sqrt{2} + \sqrt{3} < \sqrt{9} does not hold.

Sample Input 2

2 3 10

Sample Output 2

Yes

\sqrt{2} + \sqrt{3} < \sqrt{10} holds.


a,b,c=$<.read.split.map(&:to_i)

if c < a+b
    puts 'No'
elsif 4*a*b < (c-a-b)**2
    puts 'Yes'
else
    puts 'No'
end


line = gets
a,b,c=line.split.map {|c| c.to_i}
if Math.sqrt(a)+Math.sqrt(b) < Math.sqrt(c)
  puts "Yes"
else
 puts "No"
end


a,b,c = gets.split(" ").map{|i| i.to_i}
a+b+2*Math.sqrt(a*b) < c ? puts("Yes") : puts("No")


a,b,c = gets.split(" ").map{|i| i.to_i}
(sqrt(a)+sqrt(b)) < sqrt(c) ? "Yes" : "No"


include Math
a,b,c = gets.split.map &:to_i

if sqrt(a) + sqrt(b) < sqrt(c)
  puts "Yes"
else
  puts "No"
end


require "bigdecimal/math"
include BigMath
def get_i() #空白区切の入力を数値(整数)の配列で返す
  return gets.chomp.split(" ").map(&:to_i)
end
def get_f() #空白区切の入力を数値(実数)の配列で返す
  return gets.chomp.split(" ").map(&:to_f)
end
def get() #空白区切の入力を文字列の配列で返す
  return gets.chomp.split(" ")
end
def get_nsp() #入力されたものを一文字ずつに区切った文字列の配列で返す
  return gets.chomp.split("")
end
def yn_judge(bool,y="Yes",n="No") #boolに真偽を投げることで、trueならy、falseならnの値を出力する
  return bool ? y : n
end
def array(size,n=1,init=nil) #nに配列の次元数、sizeに配列の大きさ、initに初期値を投げることでその配列を返す
  if n==1
    return Array.new(size){init}
  else
    return Array.new(n).map{Array.new(size){init}}
  end
end

a,b,c=get_f
puts yn_judge(4*a*b<(c-a-b)**2)


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

if c - a - b < 0
  puts 'No'
  exit
end

if 4 * a * b < (c - a - b) ** 2
  puts 'Yes'
else
  puts 'No'
end


a,b,c = gets.split.map(&:to_i)
d = a*b
if 4*d < (c-a-b)**2
  puts 'Yes'
else
  puts 'No'
end


a, b, c = readline.split.map(&:to_i)
# /a + /b < /c
# (/a + /b) ^2 < /c ^2
# a + b + 2/ab < c
# a + b - c < - 2/ab
# a + b - c < 0 -> (a + b - c)^2 < 4ab
# a + b - c >= 0 -> True
if a + b - c >= 0 or (a + b - c) * (a + b - c) > 4 * a * b
  puts "Yes"
else
  puts "No"
end


a, b, c = read_line.split.map(&.to_i128)

if a**2 + b**2 + c**2 - 2 * (a * b + b * c + c * a) > 0 && a + b < c
  puts "Yes"
else
  puts "No"
end


require 'bigdecimal'
require 'bigdecimal/math'

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

left = Bigdecimal(sqrt(a))  + Bigdecimal(sqrt(b))
right = Bigdecimal(sqrt(c))

if left < right then
  print("Yes")
else
  print("No")
end


input = gets.chomp.split
a = input[0].to_i
b = input[1].to_i
c = input[2].to_i

left = (c - a - b) ** 2
right = 4 * a * b
c_a_b = c - a - b > 0 ? true : false

if c_a_b && left > right
    puts "Yes"
else
    puts "No"
end

# if c_a_b
#     if left > right
#         puts "Yes"
#     else
#         puts "No"
#     end
# else
#     if left < right
#         puts "Yes"
#     else
#         puts "No"
#     end
# end


a,b,c = gets.split().map(&:to_i)
if c - a - b < 0
  puts "No"
else
  if 4 * a * b < (c - a - b) ** 2
  	puts "Yes"
  else
    puts "No"
  end
end


a,b,c = gets.split.map(&:to_i)
x=c-a-b
if d > 0 && d ** 2 > 4 * a * b
  puts 'Yes'
else
  puts 'No'
end


a,b,c = gets.chomp.split(" ").map(&:to_i)
if c - a - b > 0  then
	if 4*a*b < (c-a-b)**2
 		puts "Yes"
	else
 		puts "No"
	end
elsif c - a - b < 0 then
  puts 'No'
else
  puts 'No'
end


require 'bigdecimal'
require 'bigdecimal/util'  #<= to_d メソッドが使えるようになる
(A, B, C) = gets.split.map(&:to_i)

if Math.sqrt(A*B).to_d < (C - A - B) / 2.to_d
#if Math.sqrt(A) + Math.sqrt(B) < Math.sqrt(C)
#if (A+B-C)**2 < 4*A*B
  puts 'Yes'
else
  puts 'No'
end


a,b,c = gets.split(" ").map(&:to_i)
if sqrt(a)+sqrt(b)<sqrt(c)
  puts "Yes"
else
  puts "No"
end


i = gets.chomp.split(" ")

a = Math.sqrt(i[0].to_i.abs)
b = Math.sqrt(i[1].to_i.abs)
c = Math.sqrt(i[2].to_i.abs)

a *= -1 if i[0].to_i < 0
b *= -1 if i[1].to_i < 0
c *= -1 if i[2].to_i < 0

if a + b < c
  print("Yes")
else
  print("No")
end


a,b,c = gets.split.map(&:to_f)
x = Math.sqrt(a) + Math.sqrt(b) - Math.sqrt(c)

puts x<0 ? "Yes" : "No"


a, b, c = gets.chomp.split.map(&:to_i)

r_a = Math.sqrt(a)
r_b = Math.sqrt(b)
r_c = Math.sqrt(c)

if r_a + r_b < r_c
  puts "Yes"
else
  puts "No"
end


require 'bigdecimal'
require 'bigdecimal/util'
a,b,c = gets.chomp.split.map(&:to_d)
n = 100
if (a.sqrt(n) + b.sqrt(n)) < c.sqrt(n)
  puts "Yes"
else
  puts "No"
end


a,b,c = gets.chomp.split(" ").map(&:to_i);

if c-a-b < 0
  puts "No"
  exit 0
end

if 4* a * b < ((c-a-b))**2
  puts "Yes"
else
  puts "No"
end


a, b, c = gets.split.map(&:to_f)
(Math.sqrt(a) + Math.sqrt(b)) < Math.sqrt(c) ? puts("Yes") : puts("No")


require 'bigdecimal'

a,b,c = gets.split.map(&:to_i)
a =  BigDecimal(Math.sqrt(a).to_s)
b =  BigDecimal(Math.sqrt(b).to_s)
c =  BigDecimal(Math.sqrt(c).to_s)

if (a+b) < c
  puts "Yes"
else
  puts "No"
end


A, B, C = gets.split.map(&:to_i)
if Math.sqrt(A) + Math.sqrt(B) < Math.sqrt(C)
  puts 'Yes'
else
  puts 'No'
end


# frozen_string_literal: true
(a, b, c) = gets.split(" ").map(&:to_i)
ab = 4 * a * b
tmp = (c - a - b) * (c - a - b)
ans = ""
if (ab < tmp)
    ans = "Yes"
else
    ans = "No"
end
puts(ans)


# require 'complex'
require 'bigdecimal/math'

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

# if Math.sqrt(a) + Math.sqrt(b) < Math.sqrt(c)
#   puts "Yes"
# else
#   puts "No"
# end

# if a ** (1 / 2.0) + b ** (1 / 2.0) < c ** (1 / 2.0)
#   puts "Yes"
# else
#   puts "No"
# end

if BigMath::sqrt(BigDecimal(c), 1500) - BigMath::sqrt(BigDecimal(b), 1500) - BigMath::sqrt(BigDecimal(a), 1500) > 0
  puts "Yes"
else
  puts "No"
end


A, B, C = gets.split.map(&:to_i)
D = (C - A - B) ** 2

if 0 > 4 * A * B - D
  puts 'Yes'
else
  puts 'No'
end


a, b, c = gets.chomp.split(" ").map(&:to_i)

if Math::sqrt(a) + Math::sqrt(b) < Math::sqrt(c) then
  puts "Yes"
else
  puts "No"
end


a,b,c = gets.split(" ").map(&:to_i)

reft = Math.sqrt(a) + Math.sqrt(b)
right = Math.sqrt(c)

if(reft <= right)
  puts "Yes"
else
  puts "No"
end


class InputLine
  def self.to_s
    gets.chomp
  end

  def self.to_i
    InputLine.to_s.to_i
  end

  def self.to_f
    InputLine.to_s.to_f
  end

  def self.to_sa
    InputLine.to_s.split(' ')
  end

  def self.to_ia
    InputLine.to_sa.map(&:to_i)
  end

  def self.to_fa
    InputLine.to_sa.map(&:to_f)
  end
end

def main(argv)
  (a, b, c) = InputLine.to_ia

  if c < (a + b) then
    puts 'No'
    return
  end

  s = (c ** 2) + ((a + b) ** 2)
  t = (2 * (a + b) * c) + (4 * a * b)

  if s > t then
    puts 'Yes'
  else
    puts 'No'
  end
end

if self.to_s == 'main' then
  main(ARGV)
end


def get_i() #空白区切の入力を数値(整数)の配列で返す
  return gets.chomp.split(" ").map(&:to_i)
end
def get_f() #空白区切の入力を数値(実数)の配列で返す
  return gets.chomp.split(" ").map(&:to_f)
end
def get() #空白区切の入力を文字列の配列で返す
  return gets.chomp.split(" ")
end
def get_nsp() #入力されたものを一文字ずつに区切った文字列の配列で返す
  return gets.chomp.split("")
end
def yn_judge(bool,y="Yes",n="No") #boolに真偽を投げることで、trueならy、falseならnの値を出力する
  return bool ? y : n
end
def array(size,n=1,init=nil) #nに配列の次元数、sizeに配列の大きさ、initに初期値を投げることでその配列を返す
  if n==1
    return Array.new(size){init}
  else
    return Array.new(n).map{Array.new(size){init}}
  end
end

a,b,c=get_i
puts yn_judge((a**0.5).round(100)+(b**0.5).round(100)<(c**0.5).round(100))


a, b, c = gets.split.map(&:to_f)
puts Math.sqrt(a) + Math.sqrt(b) < Math.sqrt(c) ? "Yes" : "No"


a,b,c=gets.split.map &:to_i
puts (4*a*b<(c-a-b)*(c-a-b)) ? "Yes" : "No"


a,b,c = gets.split.map(&:to_i)
x = Math.sqrt(a)+Math.sqrt(b)
y = Math.sqrt(c)

puts x<y ? "Yes" : "No"


# c
require 'pp'
a, b, c = gets.split.map(&:to_i).map{|x| x ** (1/2.0)}
puts (a + b < c) ? 'Yes' : 'No'


def get_i() #空白区切の入力を数値(整数)の配列で返す
  return gets.chomp.split(" ").map(&:to_i)
end
def get_f() #空白区切の入力を数値(実数)の配列で返す
  return gets.chomp.split(" ").map(&:to_f)
end
def get() #空白区切の入力を文字列の配列で返す
  return gets.chomp.split(" ")
end
def get_nsp() #入力されたものを一文字ずつに区切った文字列の配列で返す
  return gets.chomp.split("")
end
def yn_judge(bool,y="Yes",n="No") #boolに真偽を投げることで、trueならy、falseならnの値を出力する
  return bool ? y : n
end
def array(size,n=1,init=nil) #nに配列の次元数、sizeに配列の大きさ、initに初期値を投げることでその配列を返す
  if n==1
    return Array.new(size){init}
  else
    return Array.new(n).map{Array.new(size){init}}
  end
end

a,b,c=get_i
puts yn_judge((a**0.5)+(b**0.5)<(c**0.5))


n,m=gets.split.map(&:to_i)
all=Array.new(n){0}

m.times.each do
    v,vv=gets.split.map(&:to_i)
    if all[v-1]==0
        all[v-1]=vv
    elsif all[v-1]==vv
    next
    elsif all[v-1]!=vv && all[v-1]!=0
    p -1
    exit
    end
end

if all[0]==0
p -1
else
puts all.join("")
end

Score : 300 points

Problem Statement

If there is an integer not less than 0 satisfying the following conditions, print the smallest such integer; otherwise, print -1.

The integer has exactly N digits in base ten. (We assume 0 to be a 1-digit integer. For other integers, leading zeros are not allowed.)

The s_i-th digit from the left is c_i. \left(i = 1, 2, \cdots, M\right)

Constraints

All values in input are integers.

1 \leq N \leq 3

0 \leq M \leq 5

1 \leq s_i \leq N

0 \leq c_i \leq 9

Input

Input is given from Standard Input in the following format:

N M
s_1 c_1
\vdots
s_M c_M

Output

Print the answer.

Sample Input 1

3 3
1 7
3 2
1 7

Sample Output 1

702

702 satisfies the conditions - its 1-st and 3-rd digits are 7 and 2, respectively - while no non-negative integer less than 702 satisfies them.

Sample Input 2

3 2
2 1
2 3

Sample Output 2

-1

Sample Input 3

3 1
1 0

Sample Output 3

-1


x,y=gets.split.map!(&:to_i)
s=Array.new(y)
c=Array.new(y)
dame=0
ans=Array.new(x,0)
ans[0]=1
for i in 0..y-1
  s[i],c[i]=gets.split.map!(&:to_i)
  if s[i]==1 && c[i]==0
    dame=1
  else
    ans[s[i]-1]=c[i]
  end
end
for i in 0..y-1
  for j in 0..y-1
    if s[i]==s[j]
      if c[i]!=c[j]
        dame=1
      end
    end
  end
end
if dame==1
  puts -1
else
  for k in 0..x-1
  print(ans[k])
  end
end


n,m = gets.chomp.split.map(&:to_i)
count = 0
ans = Array.new(n,-1)
num_list = []
flag = false
while(true)
    if count == m
        break
    end
    s,c = gets.chomp.split.map(&:to_i)
    if ans[s-1] == -1
        ans[s-1] = c
        num_list.push(s)
    elsif ans[s-1] != c
        flag = true
    end
    count += 1
end
num = 0
while(true)
    if num == 0
        if ans.size == 1 && ans[num] == -1
            ans[num] = 0
        elsif ans.size > 1 && ans[num] == -1
            ans[num] = 1
        end
    else
        if ans[num] == -1
            ans[num] = 0
        end
    end
    num += 1
    if num == n
        break
    end
end
if flag || (ans.size >= 2 && ans[0] == 0)
    print '-1'
else
    ans.each do |a|
        print a.to_i
    end
end


n,m = gets.chomp.split.map(&:to_i)
count = 0
ans = Array.new(n,-1)
num_list = []
flag = false
while(true)
    s,c = gets.chomp.split.map(&:to_i)
    if ans[s-1] == -1
        ans[s-1] = c
        num_list.push(s)
    elsif ans[s-1] != c
        flag = true
        break
    end
    count += 1
    if count == m
        break
    end
end
num = 0
while(true)
    if num == 0
        if ans[num] == -1
            ans[num] = 1
        end
    else
        if ans[num] == -1
            ans[num] = 0
        end
    end
    num += 1
    if num == n
        break
    end
end
if flag || ans.size >= 2 && ans[0] == 0
    print '-1'
else
    ans.each do |a|
        print a.to_i
    end
end


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

res = Array.new(n, 0)
visited = Array.new(n, false)

m.times do
  s, c = gets.chomp.split.map(&:to_i)
  if visited[s - 1] && res[s - 1] != c
    puts "-1"
    exit
  end
  visited[s - 1] = true
  res[s - 1] = c
end

if visited[0] && res[0] == 0
  puts "-1"
  exit
end

if !visited[0]
  res[0] = 1
end
puts res.join("")


n,m = gets.chomp.split(" ").map(&:to_i)
a = Array.new(n,nil)
flag = 1
m.times do
  s,c = gets.chomp.split(" ").map(&:to_i)
  if a[s-1] == nil
    a[s-1] = c
  else
    if a[s-1] == c
      next
    else
      flag = 0
      break
    end
  end
end

flag = 2 if n == 1 && m == 0

if flag == 1
  a.each_with_index do |val,i|
    if i == 0 && val == nil
      a[i]=1
      next
    end
    if val == nil
      a[i]=0
    end
  end
  if a[0] == 0 && a.size != 1
    puts -1
  else
    str=""
    a.each do |val|
      str+=val.to_s
    end
    puts str.to_i
  end
else
  if flag == 2
    puts 0
  else
    puts -1
  end
end


def solve
  n, m = gets.split.map!(&:to_i)
  arr = []
  ans = []
  (0..(m - 1)).each do |i|
    arr[i] = gets.split.map!(&:to_i)
  end

  (0..(m - 1)).each do |j|
    ans[arr[j][0] - 1] = arr[j][1]
  end

  if ans.length != n
    return -1
  elsif ans.length >= 2 && ans[0] == 0
    return -1
  end

  ans.each_with_index do |value, index|
    ans[index] = 0 if value.nil?
  end

  ans.join
end
print solve


require 'pp'

# 空白区切の入力値を数値の配列で返却する
def gets_i_list()
  gets.chomp.split(" ").map(&:to_i)
end

N, M = gets_i_list
sc = []
M.times do
  sc.push gets_i_list
end

sc = sc.uniq.sort
s = sc.transpose[0]

if s != s.uniq #|| s.max > N
  puts -1
else
  sch = sc.to_h
  str = []
  if N == 3
    sch[1].nil? ? str.push(" ") : str.push(sch[1].to_s)
    sch[2].nil? ? str.push(" ") : str.push(sch[2].to_s)
    sch[3].nil? ? str.push(" ") : str.push(sch[3].to_s)
  elsif N == 2
    sch[1].nil? ? str.push(" ") : str.push(sch[1].to_s)
    sch[2].nil? ? str.push(" ") : str.push(sch[2].to_s)
  elsif N == 1
    sch[1].nil? ? str.push(" ") : str.push(sch[1].to_s)
  end

  if str[0] == " "
    str[0] = "1"
  end
  ans = str.join.gsub(" ", "0").to_i
  if ans.to_s.length == N
    puts ans
  else
    puts -1
  end
end


require 'pp'

# 空白区切の入力値を数値の配列で返却する
def gets_i_list()
  gets.chomp.split(" ").map(&:to_i)
end

N, M = gets_i_list
sc = []
M.times do
  sc.push gets_i_list
end

sc = sc.uniq.sort
s = sc.transpose[0]

if s != s.uniq || s.max > N
  puts -1
else
  sch = sc.to_h
  str = []
  if N == 3
    sch[1].nil? ? str.push(" ") : str.push(sch[1].to_s)
    sch[2].nil? ? str.push(" ") : str.push(sch[2].to_s)
    sch[3].nil? ? str.push(" ") : str.push(sch[3].to_s)
  elsif N == 2
    sch[1].nil? ? str.push(" ") : str.push(sch[1].to_s)
    sch[2].nil? ? str.push(" ") : str.push(sch[2].to_s)
  elsif N == 1
    sch[1].nil? ? str.push(" ") : str.push(sch[1].to_s)
  end

  if str[0] == " "
    str[0] = "1"
  end
  ans = str.join.gsub(" ", "0").to_i
  if ans.to_s.size == N
    puts ans
  else
    puts -1
  end
end


n, m = gets.chomp.split(" ").map(&:to_i)
sc = m.times.map { gets.chomp.split(" ").map(&:to_i) }
ans = Array.new(n, nil)
sc.each do |s, c|
  if (s == 1 && c == 0) || (!ans[s - 1].nil? && ans[s - 1] != c)
    puts -1
    exit
  end
  ans[s - 1] = c
end
ans = ans.map(&:to_i)
ans[0] = 1 if ans[0] == 0
puts ans.join("")


n,m=gets.split(' ').map(&:to_i)
number=Array.new(n,-1)
#puts number
flag=1
m.times do
  s,c=gets.split(' ').map(&:to_i)
  if number[s-1]==-1||number[s-1]==c
    number[s-1]=c
  else
    flag=0
    break
  end
end

if n>0&&number[0]==0
  flag=0
end

if number.length==number.count(0)
  flag==0
end

if number[0]==-1
  number[0]=1
end

if n>0
  for i in 1..n
    if number[i]==-1
      number[i]=0
    end
  end
end

if flag==0
  puts -1
else
  puts number.join
end


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

m.times do
  sc << gets.chomp.split(' ').map(&:to_i)
end

for i in 0...1000
  i_s = i.to_s
  ok = true
  next if (i_s.length != n)
  sc.each do |pair|
    ok = false if (i_s[pair[0]-1] != pair[1].to_s)
  end
  if (ok)
    puts i_s
    exit
  end
end

puts '-1'


N, M = gets.split.map(&:to_i)

if M == 0
  print(-1)
  exit
end

from = 0
to = (10**N) - 1

check = M.times.to_a.map do
  gets.split.map(&:to_i)
end

from.upto(to) do |i|
  s = i.to_s
  c = check.all? do |v|
    s[v[0] - 1].to_i == v[1]
  end
  if c
    print i
    exit
  end
end

print(-1)


a=gets.chomp.split.map(&:to_i)
i=[]
a[0].times do
  i.push(0)
end
i[0]=1
u=[]
m=0
a[1].times do
  b=gets.chomp.split.map(&:to_i)
  if u.include?(b[0])&&i[b[0]-1]==b[1]
  elsif u.include?(b[0])==true
        m+=1
      puts -1
      break
  else i[b[0]-1]=b[1]
    u.push(b[0])
  end
end
if m!=0
1.times do

if i[0]==0&&a[0]==1
  puts 0
elsif i[0]==0
  puts -1
else puts i.join
end
end
end


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

digits = {}
scs.each do |s, c|
    d = digits[s]
    if d.nil?
        digits[s] = c
        next
    end

    if d == c
        next
    end

    puts '-1'
    exit
end

if digits[1] == 0
    puts (n == 1) ? '0' : '-1'
    exit
end

if digits.count == 0
    puts (n == 1) ? 0 : 10 ** (n - 1)
    exit
end

result = 0
(0...n).each do |i|
    s = i + 1
    d = digits[s] || (s == 1 ? 1 : 0)
    result = result + d * (10 ** (n - s))
end

puts result


n,m = gets.chomp.split(' ').map(&:to_i)
arys = []
m.times do
  arys << gets.chomp.split(' ').map(&:to_i)
end

arys = arys.uniq
hoge = arys.flatten.each_slice(2).map(&:first)

arys.each do |ary|
  if hoge.count(ary[0]) > 1
    puts -1
    exit
  end
end

strs = '~' * n
arys.each do |ary|
  hoge = strs[ary[0]-1]
  if hoge == '~'
    strs[ary[0]-1] = ary[1].to_s
    next
  end
  if hoge.to_i > ary[1]
    strs[ary[0]-1] = ary[1].to_s
  end
end

strs[0] = '1' if strs[0].to_s == '~' &&  strs.size > 1

val = strs.gsub(/~/, '0').to_i
if val.to_s.size == n
  puts val.to_i
else
  puts -1
end


io = STDIN
n,m=io.gets.split.map(&:to_i)
ar=m.times.map{io.gets.split.map(&:to_i)}
def check(ar,n)
  str=" "*n
  ar.each do |s,c|
    if n==1 && s==1 && str[0] == " " && c==0
      str[s-1]="0"
    elsif s==1 && c==0 && n != 1
      return "-1"
    elsif str[s-1] == " " ||
      str[s-1]==c.to_s
      str[s-1]=c.to_s
    else
      return "-1"
    end
  end
  if str[0]==" "
    if n==1
      str[0]="0"
    else
      str[0]="1"
    end
  end
  str.gsub(" ","0")
end
puts check(ar,n)


n,m = gets.chomp.split(" ").map(&:to_i)
cond1 = Array.new
m.times do
  cond1 << gets.chomp.split(" ").map(&:to_i)
end
condk1 = cond1.select{|elem| elem[0] ==1}.uniq.map{|elem| elem[1]}
condk2 = cond1.select{|elem| elem[0] ==2}.uniq.map{|elem| elem[1]}
condk3 = cond1.select{|elem| elem[0] ==3}.uniq.map{|elem| elem[1]}

p condk1
p condk2
p condk3

ng = false
ng = true if condk1.include?(0)

condk1 = [0] if condk1 ==[]
condk2 = [0] if condk2 ==[]
condk3 = [0] if condk3 ==[]

if ng
  puts -1
else
if n==1
  if condk1.length == 1
    puts condk1[0]
  else
    puts -1
  end
end

if n==2
  if (condk1.length ==1 and condk2.length==1 )
    if condk1[0] == 0
     puts 10 + condk2[0]
    else
     puts 10*condk1[0] + condk2[0]
    end
  else
    puts -1
  end
end

if n ==3
  if (condk1.length ==1 and condk2.length==1 and condk3.length ==1 )
    if condk1[0] ==0
      puts 100 + 10*condk2[0] + condk3[0]
    else
      puts 100*condk1[0] + 10*condk2[0] + condk3[0]
    end
  else
    puts -1
  end
end
end


a=gets.chomp.split.map(&:to_i)
i=[]
a[0].times do
  i.push(0)
end
u=[]
m=0
a[1].times do
  b=gets.chomp.split.map(&:to_i)
  if u.include?(b[0])==true&&i[b[0]-1]==b[1]
  elsif u.include?(b[0])==true
        m+=1
      puts -1
      break
  else i[b[0]-1]=b[1]
       u.push(b[0])
  end
end
1.times do
  if m!=0
    break
  end
 if i[0]==0
  puts -1
   break
 else puts i.join
 end
end


n,m=gets.split.map &:to_i
sc=m.times.map{gets.split.map &:to_i}
ch=("0"*n).chars
sc.each do |i|
  ch[i[0]-1]=i[1].to_s
end
puts ch[0] == "0" ? -1 : ch.join


n,m = gets.chomp.split(' ').map(&:to_i)
arys = []
m.times do
  arys << gets.chomp.split(' ').map(&:to_i)
end

strs = '~' * n
arys.each do |ary|
  strs[ary[0]-1] = ary[1].to_s
end

val = strs.gsub(/~/, '0').to_i
if val.to_s.size == n
  puts val.to_i
else
  puts -1
end


n, m = gets.split.map(&:to_i)
s, c = m.times.map { gets.split.map(&:to_i) }.transpose

nums = Array.new(n + 1, 0)
m.times do |i|
  nums[s[i]] = c[i]
end

if n >= 2 && nums[1] == 0
  puts -1
else
  puts nums[1..n].join
end


n, m = gets.split.map(&:to_i)
s = []
c = []
for i in 0..m-1
  a, b = gets.split.map(&:to_i)
  s << a
  c << b
end
ans = -1
for k in 0..999
  next if k.to_s.length != n
  flag = true
  s.each_with_index do |si, i|
    if k.to_s[si-1] != c[i].to_s
      flag = false
    end
  end
  if flag
    ans = k
    break
  end
end
puts ans


N,M = gets.split.map(&:to_i)
S, C = M.times.map{gets.split.map(&:to_i)}.transpose
arr = Array.new(N+1, nil)

S.each_with_index do |s,i|
    if s == 1 && C[i] == 0
        puts -1
        exit
    end

    if arr[s] && C[i] != arr[s]
        puts -1
        exit
    end

    arr[s] = C[i]
end

ans = 0

N.times do |i|
    if arr[1+i]
        ans += arr[1+i] * (10 ** (3 - i - 1))
    end
end

puts ans


class InputLine
  def self.to_s
    gets.chomp
  end

  def self.to_i
    InputLine.to_s.to_i
  end

  def self.to_f
    InputLine.to_s.to_f
  end

  def self.to_sa
    InputLine.to_s.split(' ')
  end

  def self.to_ia
    InputLine.to_sa.map(&:to_i)
  end

  def self.to_fa
    InputLine.to_sa.map(&:to_f)
  end
end

def main(argv)
  (n, m) = InputLine.to_ia

  flag = true
  v = [-1] * n

  m.times do |i|
    (s, c) = InputLine.to_ia

    if v[s - 1] < 0 then
      v[s - 1] = c
    else
      if v[s - 1] != c then
        flag = false
        break
      end
    end
  end

  unless flag then
    puts (-1).to_s
    return
  end

  if ((n > 1) and (v[0] == 0)) then
    flag = false
  end

  unless flag then
    puts (-1).to_s
    return
  end

  n.times{|i| v[i] = 0 if v[i] < 0}
  puts v.map(&:to_s).join('')
end

if self.to_s == 'main' then
  main(ARGV)
end


N, M = gets.split.map(&:to_i)
S, C = [], []
M.times do |i|
  S[i], C[i] = gets.split.map(&:to_i)
end

result = Array.new N

M.times do |i|
  s = S[i] - 1
  c = C[i]

  if result[s] && result[s] != c
    puts -1
    exit
  end
  result[s] = c
end

str = ""
result.each_with_index do |r, i|
  if r
    str += r.to_s
  else
    if i == 0
      str += "1"
    else
      str += "0"
    end
  end
end

if str[0] == "0"
  puts -1
else
  puts str
end


n,m = gets.chomp.split.map(&:to_i)
ts = m.times.map{gets.chomp.split.map(&:to_i)}
flag = Array.new(n,0)
ans = Array.new(n,0)
ts.each do |s,c|
  if flag[s-1] == 1 && ans[s-1] != c
    ans = [-1]
    break
  end
  flag[s-1] = 1
  ans[s-1] = c
end

if ans[0] == 0
  ans = [-1]
end
puts ans.join


n, k = gets.split.map(&:to_i)
a    = Array.new(n){ gets.to_i).sort

l = -10 ** 18
r =  10 ** 18

nega_sta = 0
zero_sta = nega_end = a.bsearch_index{|t| t >=  0} || n
posi_sta = zero_end = a.bsearch_index{|t| t > 0} || n
posi_end = n
# p a
# p [nega_end, nega_sta]
# p [posi_sta]
nn = nega_end - nega_sta
nz = zero_end - zero_sta
np = posi_end - posi_sta

nnp = nn * np # numuber of negative pairs
nzp = nz * (np + nn ) + nz * (nz - 1 ) / 2

if nnp < k && k <= nnp + nzp
  puts 0
  exit
end
# nega = a[nega_sta,nn]
# posi = a[posi_sta,np]
# p [nn,nz,np,nega,posi]
if k <= nnp

  ok =   0
  ng = -10 ** 18
  while ( ok - ng ).abs > 1
    x = ( ok + ng ) / 2
    cnt = 0
    r   = posi_sta
    nega_end.times do |l|
      while (r < n) && a[l] * a[r] > x
        r += 1
      end
      cnt += np - ( r - posi_sta )
    end
    if cnt >= k
      ok = x
    else
      ng = x
    end
  end
  puts ok

elsif k <= nnp + nzp
  puts 0

else

  k -= nnp + nzp
  ok = 10 ** 18
  ng = 0
  while ( ok - ng ).abs > 1
    x = ( ok + ng ) / 2
    cnt = 0

    r = n - 1
    (posi_sta...n).each do |l|
      while r < n && a[l] * a[r] > x && l < r
        r -= 1
      end
      cnt += (r - l) if r - l > 0
    end

    l = 0
    (nega_end-1).downto(0) do |r|
      while l < n && a[l] * a[r] > x && l < r
        l += 1
      end
      cnt += r - l if r - l > 0
    end
    # puts x
    if cnt >= k
      ok = x
    else
      ng = x
    end
  end

  puts ok
end

Score: 400 points

Problem Statement

We have N integers A_1, A_2, ..., A_N.

There are \frac{N(N-1)}{2} ways to choose two of them and form a pair. If we compute the product of each of those pairs and sort the results in ascending order, what will be the K-th number in that list?

Constraints

All values in input are integers.

2 \leq N \leq 2 \times 10^5

1 \leq K \leq \frac{N(N-1)}{2}

-10^9 \leq A_i \leq 10^9\ (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \dots A_N

Output

Print the answer.

Sample Input 1

4 3
3 3 -4 -2

Sample Output 1

-6

There are six ways to form a pair. The products of those pairs are 9, -12, -6, -12, -6, 8.

Sorting those numbers in ascending order, we have -12, -12, -6, -6, 8, 9. The third number in this list is -6.

Sample Input 2

10 40
5 4 3 2 -1 0 0 0 0 0

Sample Output 2

6

Sample Input 3

30 413
-170202098 -268409015 537203564 983211703 21608710 -443999067 -937727165 -97596546 -372334013 398994917 -972141167 798607104 -949068442 -959948616 37909651 0 886627544 -20098238 0 -948955241 0 -214720580 277222296 -18897162 834475626 0 -425610555 110117526 663621752 0

Sample Output 3

448283280358331064


N, K = gets.chomp.split(" ").map(&:to_i)
A = gets.chomp.split(" ").map(&:to_i)

pogi = []
nega = []
zero = 0

h = A.group_by {|a| a <=> 0} # 返り値はハッシュ
nega = h[-1] || [] # ハッシュから配列を取り出す
pogi = h[1] || []
zero = h[0] || []

pogi.sort!
nega.sort!.reverse!

n_size = nega.size
p_size = pogi.size
z_size = zero.size

pair_zero = z_size * (z_size - 1) / 2 + z_size * (p_size + n_size)
pair_nega = p_size * n_size
pair_pogi = p_size * (p_size - 1) + n_size * (n_size - 1)

if pair_nega < K && K <= (pair pair_nega + pair_zero)
    puts 0
    exit
end

    # 2分木探査の返り値 b を res に入れる

res = if K <= pair_nega
    (-10**19..0).bsearch do |b|
        sum = 0
        r = p_size - 1
        nega.each do |n|
            while r >= 0 && n * pogi[r] <= b
                r -= 1
            end
            sum += p_size - r - 1
        end
        sum >= K
    end
else
    (0..10**19).bsearch do |b|
        sum = 0
        [nega, pogi].each do |a| # 負の数同士、正の数同士考える
            l = 0
            r = a.size - 1
            while l < r
                while l < r && a[l] * a[r] > b　# 最小の値　×　最大の値
                    r -= 1
                end
                sum += r - l
                l += 1
            end
        end
        sum >= K - pair_nega - pair_zero
    end
end

puts res


num, result_index = gets.split(" ").map(&:to_i)
nums = gets.split(" ").map(&:to_i)

conbinations = nums.combination(2).to_a.map { |combi| combi.inject(:*) }.sort

puts conbinations[result_index - 1]


N, K = gets.split.map(&:to_i)
A = gets.split.map(&:to_i).sort

P = []
M = []
Z = []

A.each do |a|
  if a == 0
    Z << a
  elsif a < 0
    M << a
  else
    P << a
  end
end

ZS = Z.size
PS = P.size
MS = M.size
ZC = if ZS > 1
       Z.size * (PS + MS) + (1...ZS).inject(:+)
     else
       Z.size * (PS + MS)
     end

MC = PS * MS

# p MC
# p P
# p M

if K <= MC
  P.reverse!

  def f(x)
    pi = 1
    mi = 1

    while (pi < PS || mi < MS) && (P[pi - 1] * M[mi - 1] < x)
      if pi == PS
        mi += 1
      elsif mi == MS
        pi += 1
      elsif PS[pi - 1] < MS[mi - 1].abs
        mi += 1
      else
        pi += 1
      end
    end

    (pi * mi) >= K && P[pi - 1] * M[mi - 1] <= x
  end

  ok = 0
  ng = -1 * (10 ** 19)

  while (ok - ng).abs > 1
    x = (ok + ng) / 2

    if f(x)
      ok = x
    else
      ng = x
    end
  end

  puts x
elsif K <= MC + ZC
  puts 0
else
  L = [2, K - (MC + ZC)].max

  def f(x, nums)
    n = nums.size
    return 0 if n <= 1

    cnt = 0
    i = 0
    j = n - 1

    while i < n && j >= 0
      while j > 0 && nums[i] * nums[j] > x - 1
        j -= 1
      end

      if i <= j
        cnt += j
      else
        cnt += (j + 1)
      end

      i += 1
    end

    cnt / 2
  end

  ng = 10 ** 19
  ok = 0

  M.reverse!

  while (ok - ng).abs > 1
    x = (ok + ng) / 2
    cnt = f(x, P) + f(x, M)

    if cnt < L
      ok = x
    else
      ng = x
    end
  end

  puts ok
end


_, k = gets.split(" ").map(&:to_i)
a = gets.split(" ").map(&:to_i)
l = []
a.combination(2) {|i, j|
  l << i*j
}
print l.sort![k-1]


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

c = []

c_num = nums.combination(2).to_a

c_num.each do |nums|
  c << nums[0].to_i * nums[1].to_i
end

p c.sort![target_num - 1]


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

a = array.combination(2).map do |e|
  e[0] * e[1]
end

puts a.sort[k - 1]


# coding: utf-8
N, K = gets.split.map {|e| e.to_i}
nums = gets.split.map {|e| e.to_i}

# 組み合わせを生成
comb = []
nums.combination(2).each do |e|
  comb << e
end

# それぞれの積を計算
mult = comb.map {|e| e[0] * e[1]}

# ソート
mult.sort!

puts mult[K-1]


N,K=gets.split.map &:to_i
a=gets.split.map &:to_i
b=a.combination(2).map{|a,b| a*b}
puts b.sort[K-1]


a,b=gets.split.map(&:to_i)
if a > 9 || b > 9 then
  p -1
else
  p a * b
end

Score : 100 points

Problem Statement

Having learned the multiplication table, Takahashi can multiply two integers between 1 and 9 (inclusive) together. He cannot do any other calculation.

Given are two integers A and B.

If Takahashi can calculate A \times B, print the result; if he cannot, print -1 instead.

Constraints

1 \leq A \leq 20

1 \leq B \leq 20

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

If Takahashi can calculate A \times B, print the result; if he cannot, print -1.

Sample Input 1

2 5

Sample Output 1

10

2 \times 5 = 10.

Sample Input 2

5 10

Sample Output 2

-1

5\times 10 = 50, but Takahashi cannot do this calculation, so print -1 instead.

Sample Input 3

9 9

Sample Output 3

81


nums = gets.split.map(&:to_i)
p nums.all? {|num| num < 10} ? nums.inject(:*) : -1


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

if a > 9 || b > 9
  puts -1
else
  puts a * b
end


arr = gets.split.map(&:to_i)
a = arr[0]
b = arr[1]

puts a.between?(1, 9) && b.between?(1, 9) ? a * b : -1


A, B = gets.split.map(&:to_i)
p A>9 || B>9 ? -1 : A*B


a, b = gets.split.map{|i| i.to_i}
if b <= 9 && a <= 9
  puts a*b
else
  puts "-1"
end


input = gets.chomp.split
num1 = input.first
num2 = input.last

res = num1 * num2
if res > 81 || res < 1
  return -1
else
  return res
end


a,b=gets.split.map(&:to_i)
puts (a<10 && b<10) ? -1 : a*b


a,b=gets.split.map(&:to_i)
if a < 10 && b < 10
  puts a*b
else
  puts -1
end


a = gets.to_i
b = gets.to_i
if(a < 10 && b < 10)
    puts a*b
else
    puts -1
end


n=gets.chomp.split("").map(&:to_s)
v=n[0]
n.push(v)
#p n
t=gets.to_i
i=1
count=0
count
count_zero=0
while i<=n.length-1
    #p i
    if i==n.length-1 && n[i-1]==n[i]
        n[i]="#"
             count_zero=1
             i+=1
             next
    elsif n[i-1]==n[i]
        n[i]="#"
        count+=1
        i+=1
        next

    end
    i+=1
end

p count*t+count_zero*t

Score : 300 points

Problem Statement

Given is a string S. Let T be the concatenation of K copies of S.
We can repeatedly perform the following operation: choose a character in T and replace it with a different character.
Find the minimum number of operations required to satisfy the following condition: any two adjacent characters in T are different.

Constraints

1 \leq |S| \leq 100

S consists of lowercase English letters.

1 \leq K \leq 10^9

K is an integer.

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the minimum number of operations required.

Sample Input 1

issii
2

Sample Output 1

4

T is issiiissii. For example, we can rewrite it into ispiqisyhi, and now any two adjacent characters are different.

Sample Input 2

qq
81

Sample Output 2

81

Sample Input 3

cooooooooonteeeeeeeeeest
999993333

Sample Output 3

8999939997


n=gets.chomp.split("").map(&:to_s)
t=gets.to_i
i=0
count=0
while i<=n.length-1
    #p i
    if n[i-1]==n[i]
        n[i]="#"
        count+=1
        i+=1
        next

    end
    i+=1
end
p count*t


s = gets.chomp
k = gets.to_i
a = 0
t = s.dup
t.size.pred.times do |i|
  if t[i] == t[i + 1]
    a += 1
    t[i + 1] = '0'
  end
end
b = 0
t = s.dup * 2
t.size.pred.times do |i|
  if t[i] == t[i + 1]
    b += 1
    t[i + 1] = '0'
  end
end
c = 0
t = s.dup * 3
t.size.pred.times do |i|
  if t[i] == t[i + 1]
    c += 1
    t[i + 1] = '0'
  end
end
d = 0
t = s.dup * 4
t.size.pred.times do |i|
  if t[i] == t[i + 1]
    d += 1
    t[i + 1] = '0'
  end
end
if a == b - a
  puts a * k
elsif b - a == c - b
  puts a + (k - 1) * (b - a)
elsif a == (c - b) && (b - a) == (d - c)
  puts a * k / 2 + (b - a) * k / 2 if k.even?
  puts a * (k + 1) / 2 + (b - a) * (k - 1) / 2 if k.odd?
end


def sum(s,k)
  if s.split('').all?{|c| c==s[0] }
    puts s.length*k/2
  else
    top=0
    s.each_char.with_index do |c,i|
      next if i == 0
      if c==s[i-1]
        top+=1
      else
        next
      end
    end
    bot=0
    rs=s.reverse
    s.each_char.with_index do |c,i|
      next if i == 0
      if c==rs[i-1]
        bot+=1
      else
        next
      end
    end
    cnt=0
    s.each_char.with_index do |c,i|
      next if i ==0
      if c == s[i-1]
        s[i]='0'
        cnt+=1
      end
    end
    all=cnt*k
    if s[0] == s[s.length-1]
      all+=(top+bot)/2 * (k-1)+ top/2+(bot/2)
      puts all
    else
      #all+=((top/2+(bot/2))*k)
      puts all
    end
  end
end

sum(gets.chomp, gets.chomp.to_i)


S=gets.chomp
K=gets.chomp.to_i

if S.size == 1
  puts K/2
else
  have_to_change_count = S.scan(/(.)\1+?/).size
  if S[0] == S[-1] &&
    S.scan(/^#{S[0]}+/).first&.size.odd? &&
    S.reverse.scan(/^#{S[-1]}+/).first.size.odd?

    puts ((have_to_change_count + 1) * K - 1)
  else
    puts (have_to_change_count * K)
  end
end


s = gets.chomp
k = gets.to_i
n = s.length
if n == 1
  p k/2
else
  ans = 0
  if s[0] == s[n-1]
    ans += k
    s[n-1] = (n-1 % 10).to_s
  end
  1.upto(n-1) do |i|
    if s[i] == s[i-1]
      s[i] = (i % 10).to_s
      ans += k
    end
  end
  p ans
end


def count_cont(s)
    bef = ''
    cnt = 0
    s.each_char do |c|
        if c == bef
            cnt += 1
            bef = 'Z'
        else
            bef = c
        end
    end
    #puts cnt
    cnt
end

def aa
    s = gets.chomp
    k = gets.chomp.to_i

    if s.length == 1
        puts (k/2.0).floor
        exit
    end

    if s.split("").uniq.length == 1
        if s.length%2 == 0
            puts s.length/2 * k
        else
            if k == 1
                puts s.length/2
            elsif k%2 == 0
                puts k/2 * s.length
            else
                puts k/2 * s.length + s.length/2
            end
        end
        exit
    end

    cnt1 = count_cont(s)
    if k == 1
        puts cnt1
    elsif k%2 == 0
        cnt2 = count_cont(s+s)
        puts (k-1)*(cnt2-cnt1) + cnt1
    else
        cnt2 = count_cont(s+s)
        puts (k-1)*(cnt2-cnt1) + cnt1
    end
end

aa


class PriorityQueue
  def initialize
    @heap = [nil]
  end

  def up_heap(x)
    @heap << x
    idx = @heap.size - 1

    while idx != 1
      parent = idx / 2
      break if @heap[idx] < @heap[parent]

      @heap[idx], @heap[parent] = @heap[parent], @heap[idx]
      idx = parent
    end
  end

  def down_heap
    size = @heap.size
    return if size == 1
    return @heap.pop if size == 2

    max = @heap[1]
    @heap[1] = @heap.pop
    size -= 1
    idx = 1

    while idx * 2 < size
      l = idx * 2
      r = idx * 2 + 1

      if @heap[r]
        child = @heap[l] > @heap[r] ? l : r
      else
        child = l
      end

      break if @heap[idx] > @heap[child]

      @heap[idx], @heap[child] = @heap[child], @heap[idx]
      idx = child
    end

    max
  end

  def to_a
    @heap.shift
    @heap
  end

  alias_method :<<, :up_heap
  alias_method :push, :up_heap
  alias_method :shift, :down_heap
end

n, m = gets.split.map(&:to_i)
q = PriorityQueue.new
gets.split.map(&:to_i).each { |e| q << e }

m.times { q.push(q.shift / 2) }

puts q.to_a.inject(:+)

Score : 400 points

Problem Statement

Takahashi is going to buy N items one by one.

The price of the i-th item he buys is A_i yen (the currency of Japan).

He has M discount tickets, and he can use any number of them when buying an item.

If Y tickets are used when buying an item priced X yen, he can get the item for \frac{X}{2^Y} (rounded down to the nearest integer) yen.

What is the minimum amount of money required to buy all the items?

Constraints

All values in input are integers.

1 \leq N, M \leq 10^5

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_N

Output

Print the minimum amount of money required to buy all the items.

Sample Input 1

3 3
2 13 8

Sample Output 1

9

We can buy all the items for 9 yen, as follows:

Buy the 1-st item for 2 yen without tickets.

Buy the 2-nd item for 3 yen with 2 tickets.

Buy the 3-rd item for 4 yen with 1 ticket.

Sample Input 2

4 4
1 9 3 5

Sample Output 2

6

Sample Input 3

1 100000
1000000000

Sample Output 3

0

We can buy the item priced 1000000000 yen for 0 yen with 100000 tickets.

Sample Input 4

10 1
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 4

9500000000


class PriorityQueue
  attr_accessor :data, :size

  def initialize(array = [])
    @data = []
    array.each { |a| push(a) }
  end

  def push(element)
    current = @data.size
    while current > 0
      p_index = parent(current)
      break if @data[p_index] <= element
      @data[current] = @data[p_index]
      current = p_index
    end
    @data[current] = element
  end

  def pop
    ret = @data.first
    x = @data.last
    current = 0
    while have_child?(current)
      a_index = left_child_index(current)
      b_index = right_child_index(current)
      a_index = b_index if @data[b_index] && @data[b_index] < @data[a_index]
      break if @data[a_index] >= x
      @data[current] = @data[a_index]
      current = a_index
    end
    @data[current] = x
    @data.pop

    ret
  end

  def size
    @data.size
  end

  def front
    @data.first
  end

  def empty?
    @data.empty?
  end

  private

  def parent(num)
    (num - 1) / 2
  end

  def left_child_index(num)
    2 * num + 1
  end

  def right_child_index(num)
    2 * num + 2
  end

  def have_child?(index)
    left_child_index(index) < @data.size
  end
end

n, m = gets.split.map(&:to_i)
nums = gets.split.map(&:to_i)
pq = PriorityQueue.new(nums.map { |i| -i } )
m.times { pq.push((pq.pop / 2.0).ceil) }
puts -pq.data.inject(:+)


N,M=gets.split.map(&:to_i)
as=gets.split.map(&:to_i).sort
M.times do
    e=as.pop/2
    em=e-1
    idx=as.bsearch_index{|n|em<n}
    # as.insert(idx.nil? ? 0 : idx,e)
end
p as.inject(:+)


N,M = gets.split.map(&:to_i)
A = gets.split.map(&:to_i)

ans = A.sort.reverse!
M.times do
  ans[0] = ans[0] / 2
  if ans[0] < ans[1]
    n = ans.shift
    i = ans.bsearch_index {|x| x < n } || -1
    ans.insert(i, n)
  end
end
puts ans.inject(:+)


f =-> {gets.split.map(&:to_i)}
N,M = f[]
A = f[].sort!
c = 0
catch(:x) {
while A[-1] > 0
  1.upto(N) {|i|
    break  if A[-i] < A[-1]/2
    A[-i] /= 2
    throw :x  if M == c+=1
  }
  A.sort!
end}
p A.inject(:+)


n, m = gets.split.map(&:to_i)
a_arr = gets.split.map(&:to_i)
a_arr = a_arr.sort.reverse
a_arr.unshift(0)
a_arr.push(0)

before_i = 1
while m != 0
  if a_arr[before_i - 1] > a_arr[before_i]
    before_i = before_i - 1
  elsif a_arr[before_i+1] >= a_arr[before_i]
    if a_arr[before_i + 1] == 0
      break
    end
    before_i = before_i + 1
  else
    a_arr[before_i] /= 2
    m -= 1
  end
end

puts a_arr.inject(0, :+)


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

a.sort!

m.times do
  price = a[-1]
  discount_price = price / 2
  a.pop
  new_index = a.bsearch_index { |x| x >= discount_price }
  a.insert(new_index || 0, discount_price)
end

puts a.inject(:+)


n, m = gets.split.map(&:to_i)
a = gets.split.map(&:to_i).sort.reverse

ans = 0
if n == 1
  ans = a[0] / 2 ** m
else
  while m > 0
    x = a.shift
    x /= 2
    p = n - 2
    (0..(n - 2)).each do |i|
      if a[i] < x
        p = i
        break
      end
    end
    a.insert(p, x)
    m -= 1
  end
  ans = a.inject(:+)
end

puts ans


n,m = gets.split.map(&:to_i)
price = gets.split.map(&:to_i)
count = Array.new(n,0)
price.sort!
for i in 1..m
  min = 0
  max = n-1
  while max - min > 1
    if price[-1]/2 > price[(max+min)/2]
      min = (max+min)/2
    else
      max = (max+min)/2
    end
  end
  price[-1] /= 2
  price.insert(max,price[-1])
  price.delete_at(-1)
end
ans = 0
n.times{|j| ans += price[j]}
puts ans


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

a.sort!

m.times do|i|
  tmp=a[a.length-1]/2

  flag = -1
  a.length.times do|i|
    if a[i]>=tmp then
      flag = i
      break
    end
  end
  if flag!=-1 then
    a.insert(flag, tmp)
  else
    a.insert(a.length-1,tmp)
  end
    a.delete_at(a.length-1)
end

all=0
a.each do|elm|
  all += elm
end
puts all


class String
  def z_algorithm
    n = size
    c = 0
    z = [0] * n
    ( 1 ... n ).each do |i|
      l = i - c
      if l < z[c] - z[l]
        z[i] = z[l]
      else
        j = [0, z[c] - l].max
        j += 1 while i + j < n && self[j] == self[i+j]
        z[i] = j
        c = i
      end
    end
    #z[0] = n
    z
  end
end

class Array
  def z_algorithm
    n = size
    c = 0
    z = [0] * n
    ( 1 ... n ).each do |i|
      l = i - c
      if l < z[c] - z[l]
        z[i] = z[l]
      else
        j = [0, z[c] - l].max
        j += 1 while i + j < n && self[j] == self[i+j]
        z[i] = j
        c = i
      end
    end
    #z[0] = n
    z
  end
end

n = gets.to_s.to_i
s = gets.to_s.chomp

ans = 0
(n-1).times do |i|
  lcp = s[i, n-i].z_algorithm
  lcp.size.times do |j|
    l = [lcp[j], j].min
    ans = l if ans < l
  end
end

puts ans

Score : 500 points

Problem Statement

Given is a string S of length N.

Find the maximum length of a non-empty string that occurs twice or more in S as contiguous substrings without overlapping.

More formally, find the maximum positive integer len such that there exist integers l_1 and l_2 ( 1 \leq l_1, l_2 \leq N - len + 1 ) that satisfy the following:

l_1 + len \leq l_2

S[l_1+i] = S[l_2+i] (i = 0, 1, ..., len - 1)

If there is no such integer len, print 0.

Constraints

2 \leq N \leq 5 \times 10^3

|S| = N

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the maximum length of a non-empty string that occurs twice or more in S as contiguous substrings without overlapping. If there is no such non-empty string, print 0 instead.

Sample Input 1

5
ababa

Sample Output 1

2

The strings satisfying the conditions are: a, b, ab, and ba. The maximum length among them is 2, which is the answer.
Note that aba occurs twice in S as contiguous substrings, but there is no pair of integers l_1 and l_2 mentioned in the statement such that l_1 + len \leq l_2.

Sample Input 2

2
xy

Sample Output 2

0

No non-empty string satisfies the conditions.

Sample Input 3

13
strangeorange

Sample Output 3

5


def Z_Algorithm_ex(s, lim)
  ret = Array.new(s.size, 0)
  wk = 0
  i, j = [1, 0]
  i = lim if lim > 0
  while i < s.size do
    break if s.size - i < lim
    while i+j < s.size && s[j] == s[i+j] do j += 1 end
    ret[i] = j
    wk = calc(wk, i, j)
    (i += 1; next) if j == 0
    k = 1
    while i+k < s.size && k + ret[k] < j do
      ret[j+k] = ret[k]
      wk = calc(wk, j+k, ret[k])
      k += 1
    end
    i += k
    j -= k
  end
  wk
end

def calc(wk, i, j)
  tmp = i > j ? j : i
  wk < tmp ? tmp : wk
end

N = gets.to_i
S = gets.chomp.chars

ans = 0
(0..(N-2)).each{|i|
  break if i + ans > N
  wk = Z_Algorithm_ex(S[i...N], ans)
  ans = wk if ans < wk
}

puts ans


def who_says_a_pun(n, s)
  dp = Array.new(n + 1) { Array.new(n + 1, 0) }
  (n - 1).downto(0) do |i|
    (n - 1).downto(0) do |j|
      if s[i] != s[j]
        dp[i][j] = 0
      else
        dp[i][j] = dp[i + 1][j + 1] + 1
      end
    end
  end

  ans = 0
  0.upto(n - 1) do |i|
    (i + 1).upto(n -1) do |j|
      now = [dp[i][j], j - i].min
      ans = [now, ans].max
    end
  end
  ans
end

n = gets.to_i
s = gets.chomp
puts who_says_a_pun(n, s)


n = gets.to_i
s = gets.chomp
(1..(n / 2)).to_a.reverse_each do |j|
  map = Hash.new{[]}
  (0...n).each do |i|
    next if i + j > n
    key = s.slice(i, j)
    map[key] = map[key].push([i, j])
  end
  map.each do |_, v|
    next if v.count <= 1
    prev = v.first
    v.each do |k|
      prev_sum = prev[0] + prev[1]
      cur_sum = k[0] + k[1]
      if cur_sum - prev_sum >= j
        puts j
        exit 0
      end
      prev = k
    end
  end
end
puts 0


a = gets.to_i
s = gets.chomp

if a >= 3200
  print  "#{s}\n"
else
  print "red\n"
end

Score : 100 points

Problem Statement

You will be given an integer a and a string s consisting of lowercase English letters as input.

Write a program that prints s if a is not less than 3200 and prints red if a is less than 3200.

Constraints

2800 \leq a < 5000

s is a string of length between 1 and 10 (inclusive).

Each character of s is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

a
s

Output

If a is not less than 3200, print s; if a is less than 3200, print red.

Sample Input 1

3200
pink

Sample Output 1

pink

a = 3200 is not less than 3200, so we print s = pink.

Sample Input 2

3199
pink

Sample Output 2

red

a = 3199 is less than 3200, so we print red.

Sample Input 3

4049
red

Sample Output 3

red

a = 4049 is not less than 3200, so we print s = red.


a = gets.to_i
s = gets

if a >= 3200
    puts s
else
    puts 'red'
end


a = gets.to_i
s = gets
if a >= 3200
  puts s
else
  puts 'red'
end


a = gets.to_i
s = gets
puts s if a >= 3200
puts 'red' if a < 3200


a = gets.chomp.to_i
s = gets.chomp

puts( (a<3200) ? 'red' : s )


a=gets.to_i
s=gets

if a>=3200
    puts s
else
    puts "red"
end


    a = gets.to_s
    s = gets.chomp

    a >= 3200 ? puts s : puts 'red'


a = gets.chomp.to_i
s = gets.chomp

if a >= 3200
    puts s
else
    puts "red"
end


a = gets.to_i
s = gets.chomp

if a >= 3200 then
  print s
else
  print "red"
end


require 'pp'

a = gets.chomp!.to_i
s = gets.chomp!

if a >= 3200
    puts s
else
    puts "red"
end


if !ENV['RUBY_THREAD_VM_STACK_SIZE']
  exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000000'}, '/usr/bin/ruby', $0)
end

def solve(node)
  $used[node] = true
  $g[node].each do |k|
    next if $used[k]
    $points[k] += $points[node]
    solve(k) unless $used[k]
  end
end

n, q = gets.split.map(&:to_i)
$used = Array.new(n,false)

$g = Array.new(n){[]}
(n-1).times do
  a,b = gets.split.map{ |i| i.to_i - 1 }
  $g[a].push(b)
  $g[b].push(a)
end

$points = Array.new(n,0)
q.times do |i|
  node,x = gets.split.map(&:to_i)
  node -= 1
  $points[node] += x
end

solve(0)

puts $points.join(' ')

Score : 400 points

Problem Statement

Given is a rooted tree with N vertices numbered 1 to N.
The root is Vertex 1, and the i-th edge (1 \leq i \leq N - 1) connects Vertex a_i and b_i.

Each of the vertices has a counter installed. Initially, the counters on all the vertices have the value 0.

Now, the following Q operations will be performed:

Operation j (1 \leq j \leq Q): Increment by x_j the counter on every vertex contained in the subtree rooted at Vertex p_j.

Find the value of the counter on each vertex after all operations.

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq Q \leq 2 \times 10^5

1 \leq a_i < b_i \leq N

1 \leq p_j \leq N

1 \leq x_j \leq 10^4

The given graph is a tree.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
a_1 b_1
:
a_{N-1} b_{N-1}
p_1 x_1
:
p_Q x_Q

Output

Print the values of the counters on Vertex 1, 2, \ldots, N after all operations, in this order, with spaces in between.

Sample Input 1

4 3
1 2
2 3
2 4
2 10
1 100
3 1

Sample Output 1

100 110 111 110

The tree in this input is as follows:

Each operation changes the values of the counters on the vertices as follows:

Operation 1: Increment by 10 the counter on every vertex contained in the subtree rooted at Vertex 2, that is, Vertex 2, 3, 4. The values of the counters on Vertex 1, 2, 3, 4 are now 0, 10, 10, 10, respectively.

Operation 2: Increment by 100 the counter on every vertex contained in the subtree rooted at Vertex 1, that is, Vertex 1, 2, 3, 4. The values of the counters on Vertex 1, 2, 3, 4 are now 100, 110, 110, 110, respectively.

Operation 3: Increment by 1 the counter on every vertex contained in the subtree rooted at Vertex 3, that is, Vertex 3. The values of the counters on Vertex 1, 2, 3, 4 are now 100, 110, 111, 110, respectively.

Sample Input 2

6 2
1 2
1 3
2 4
3 6
2 5
1 10
1 10

Sample Output 2

20 20 20 20 20 20


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

count = Array.new(n + 1, 0)
road = Array.new(n + 1) {[]}

(n-1).times do
    a, b = gets.split.map(&:to_i)
    road[a] << b
    road[b] << a
end

q.times do
    q, x = gets.split.map(&:to_i)
    count[q] += x
end

que = [[1, 0]]
while !que.empty?
    now, pre = que.shift
    road[now].each do |i|
        next if i == pre
        count[i] += count[now]
        que << [i, now]
    end
end

puts count[1..-1].join(" ")


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

count = Array.new(n + 1, 0)
road = Array.new(n + 1) {[]}

(n-1).times do
    a, b = gets.split.map(&:to_i)
    road[a] << b
    road[b] << a
end

q.times do
    q, x = gets.split.map(&:to_i)
    count[q] += x
end

que = [[1, 0]]
while (now, pre = que.shift)
    road[now].each do |i|
        next if i == pre
        count[i] += count[now]
        que << [i, now]
    end
end

puts count[1..-1].join(" ")


N, Q = gets.split.map(&:to_i)
edges = Hash.new { |h, k| h[k] = [] }

(N - 1).times do
  a, b = gets.split.map(&:to_i)

  edges[a - 1] << b - 1
  edges[b - 1] << a - 1
end

counter = Array.new(N, 0)

Q.times do
  idx, cnt = gets.split.map(&:to_i)

  counter[idx - 1] += cnt
end

answers = Array.new(N, 0)

queue = [[0, counter[0]]]
visited = Hash.new(false)

until queue.empty?
  idx, cnt = queue.shift

  next if visited[idx]
  visited[idx] = true

  answers[idx] = cnt

  edges[idx].each do |child|
    queue << [child, cnt + counter[child]]
  end
end

puts answers.join(' ')


n, q = gets.chomp.split(' ').map(&:to_i)
ab = (1..(n - 1)).map { gets.chomp.split(' ').map(&:to_i) }
px = (1..q).map { gets.chomp.split(' ').map(&:to_i) }

@tree = Array.new(n) { [] }
@answer = Array.new(n, 0)
@is_added = Array.new(n, false)

ab.each do |a, b|
  @tree[a - 1] << b - 1
  @tree[b - 1] << a - 1
end
px.each { |p, x| @answer[p - 1] += x }

def add_each_child(p, children)
  @is_added[p] = true
  children.each do |i|
    next if @is_added[i]
    @answer[i] += @answer[p]
    @is_added[i] = true
    add_each_child(i, @tree[i])
  end
end

add_each_child(0, @tree[0])

puts @answer[0..(n - 1)].join(' ')


n, q = gets.chomp.split(/\s/).map(&:to_i)

t = []

(n - 1).times do
  a, b = gets.chomp.split(/\s/).map(&:to_i)
  t[a] ||= {counter: 0, leaf: []}
  t[b] ||= {counter: 0, leaf: []}
  t[a][:leaf].push b
end

def add(t, node, x)
  node[:counter] += x
  node[:leaf].each {|l| add(t, t[l],x)}
end

q.times do
  p1, x = gets.chomp.split(/\s/).map(&:to_i)

  add(t, t[p1], x)
end

puts t[1..-1].map {|l| l[:counter].to_s}.join(" ")


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

n,q = gets.split.map(&:to_i)
$ans = Array.new(n,0)

# 隣接リストで木を表現する。
$to = Array.new(n){ [] }

(n-1).times do |i|
  a,b = gets.split.map(&:to_i)
  a -= 1
  b -= 1
  $to[a].push(b)
  $to[b].push(a)
end

# 累積和を取るためのdfs
def dfs(v,p=-1)
  $to[v].each do |u|
    if u == p
      next
    end
    $ans[u] += $ans[v]
    dfs(u,v)
  end
end

q.times do |i|
  p,x = gets.split.map(&:to_i)
  p -= 1
  $ans[p] += x
end

# dfsで累積和を取る。
dfs(0)

(0..n-1).each do |i|
  puts $ans[i]
end


n, q = gets.split.map(&:to_i)
tree = Array.new(n) {[]}
(n - 1).times do
  a, b = gets.split.map(&:to_i)
  tree[a - 1] << b - 1
  tree[b - 1] << a - 1
end

total = Array.new(n, 0)
q.times do
  p, x = gets.split.map(&:to_i)
  total[p - 1] += x
end

ans = Array.new(n, 0)
dfs = ->(v, parent, value) {
  value += total[v]
  ans[v] = value
  tree[v].each do |c|
    next if c == parent
    dfs.(c, v, value)
  end
}
dfs.(0, -1, 0)

puts ans.join(" ")


Node = Struct.new(:children, :counter)

class Tree
  def initialize(n)
    @tree = Array.new(n + 1) {Node.new([], 0)}
  end

  def insert(a, b)
    down = ->(s) {
      if s == a
         @tree[s].children << b
      elsif s == b
         @tree[s].children << a
      else
        @tree[s].children.each {|child| down.(child)}
      end
    }
    down.(1)
  end

  def traverse(node, x)
    @tree[node].counter += x
    @tree[node].children.each {|child| traverse(child, x)}
  end

  def putout
    @tree[1..-1].map {|node| node.counter}
  end
end

n, q = gets.split.map(&:to_i)
t = Tree.new(n)

(n - 1).times do
  a, b = gets.split.map(&:to_i)
  t.insert(a, b)
end

q.times do
  p, x = gets.split.map(&:to_i)
  t.traverse(p, x)
end

puts t.putout.join(" ")


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

tree = Array.new(n) { [] }
(n - 1).times do
  a, b = gets.split.map(&:to_i)
  a -= 1
  b -= 1
  tree[a] << b
  tree[b] << a
end
cnt = Array.new(n, 0)
q.times do
  pi, xi = gets.split.map(&:to_i)
  pi -= 1
  cnt[pi] += xi
end

que = []
que << [0, 0] # node_id, cnt
ans = []
visited = []
while !que.empty?
  node, c = que.shift
  ans[node] = c + cnt[node]
  visited[node] = true
  if !tree[node].empty?
    tree[node].each do |child_node|
      que << [child_node, c + cnt[node]] if !visited[child_node]
    end
  end
end
puts ans * ' '

__END__
3 3
1 3
2 3
1 1
2 1
3 1


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

@trees = Array.new(n) { [] }
@val = Array.new(n) { 0 }

(n-1).times.each do
  p, c = gets.split(' ')

  @trees[p.to_i - 1] << c.to_i - 1
  @trees[c.to_i - 1] << p.to_i - 1
end

q.times.each do
  p, x = gets.split(' ')
  @val[p.to_i - 1] += x.to_i
end

def dfs(v, p = -1)
  @trees[v].each do |u|
    if u != p
      @val[u] += @val[v]
      dfs(u, v)
    end
  end
end

dfs(0)

@val.each do |n|
  puts n
end


@n,q = gets.chomp.split.map(&:to_i)
ts = (@n-1).times.map{gets.chomp.split.map(&:to_i)}
cs = q.times.map{gets.chomp.split.map(&:to_i)}
@tree = Array.new(@n){Array.new}
ts.each do |a,b|
  @tree[a-1].push(b-1)
end
ans = Array.new(@n,0)
@count = Array.new(@n,[])
def walk(num)
  pathed = [num]
  flag = true
  if !@tree[num].empty?
    @tree[num].each do |i|
      walk(i).each do|j|
        pathed.push(j)
      end
    flag = false
    end
  end
  @count[num] = pathed
  return pathed
end
walk(0)
cs.each do|c,add|
  @count[c-1].each do|k|
    ans[k] += add
  end
end
puts ans.join(" ")


#require 'pp'

n, q = gets.chomp!.split(" ").map(&:to_i)
$tree = {}

# O(n)
(n - 1).times do |i|
    a, b = gets.chomp!.split(" ").map(&:to_i)
    $tree[a] ||= {}
    $tree[a][b] = 0
end

# O(n)
scores = {}
n.times do |i|
    scores[i+1] = 0
end

# O(q)
operations = []
q.times do |_|
    p, x = gets.chomp!.split(" ").map(&:to_i)
    scores[p] += x
end

#pp scores
#pp $tree
ans = {}
tmp = [1]
while tmp.empty?.!
    d = tmp.pop
    ans[d] = scores[d]

    $tree[d].each do |k, _|
        scores[k] += scores[d]
        if $tree[k]
            tmp.push(k)
        else
            ans[k] = scores[k]
        end
    end
end

puts ans.sort.map! {|x, y| y }.join(" ")


n, q = gets.split(" ").map(&:to_i)

tree_c = {}
answers = []

(n - 1).times do |_|
  a, b = gets.split(" ").map(&:to_i)
  tree_c[a] = { p: nil, v: 0 } unless tree_c.key?(a)
  tree_c[b] = { p: a, v: 0 } unless tree_c.key?(b)
end

q.times do |_|
  a, b = gets.split(" ").map(&:to_i)
  tree_c[a][:v] += b
end

tree_v = {}
tree_c.keys.each do |k|
  p = tree_c[k][:p]
  v = tree_c[k][:v] + (tree_v[p] || 0)
  tree_v[k] = 0 unless tree_v.key?(k)
  tree_v[k] += v
  answers.push(v)
end

puts answers.join(" ")


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

treeRoot = Hash.new {|hash, key| hash[key] = []}
points = Array.new(n, 0)

def addTree(treeRoot, parentNum, childNum)
  treeRoot[parentNum] << childNum
end

def getChilds(treeRoot, rootNum)
  childs = [rootNum]
  treeRoot[rootNum].each {|child|
    if childs2 = getChilds(treeRoot, child)
      childs = [childs, childs2].flatten!
    end
  }
  childs
end

(n - 1).times do
  a, b = gets.split.map(&:to_i)
  addTree(treeRoot, a, b)
end

q.times do
  p, x = gets.split.map(&:to_i)
  getChilds(treeRoot, p).each {|toPointCHild|
    points[toPointCHild - 1] += x
  }
end

puts points.join(' ')


def calcs(x,y,ax,ay,bx,by) ((ax-x)*(by-y)-(bx-x)*(ay-y))/2.0 end

def inpf() a=gets.chomp.split(" ").map(&:to_f)end

def inps() a=gets.chomp.split(" ")end

def copy(a) Marshal.load(Marshal.dump(a)) end

def factorial(n)(n < 2)? 1 : (2..n).inject(:*) end

def scount(a) b=na(a.max+1);a.each{|n|b[n]+=1};return b end

def na(n,d=0) Array.new(n,d)end

def na2(n,m,d=0) Array.new(n){Array.new(m,d)}end

def na3(n,m,l,d=0) Array.new(n){Array.new(ym){Array.new(l,d)}}end

def yn(f) return (f)? "Yes": "No" end

def ynb(f) return (f)? "YES": "NO" end

def ap(a) a.each{|u| p u} end

def bit(n) n.to_s(2).split("").map(&:to_i) end

def inp() a=gets.chomp.split(" ").map(&:to_i)end

def answer(tree,counter)
  ans = na(counter.size)
  que = [[1,1,0]]
  used = na(counter.size,false)
  while que.size > 0
    pa, x, c = que.shift
    c += counter[x]
    ans[x] = c
    tree[x].each do |xx|
      next if(xx == pa)
      que.push([x,xx,c])
    end
  end
  return ans
end

n,q = inp
tree = na2(n+1,0)
counter = na(n+1,0)
(n-1).times do |i|
  d = inp
  tree[d[0]].push(d[1])
  tree[d[1]].push(d[0])
end
q.times do |j|
  d = inp
  counter[d[0]] += d[1]
end
ans = answer(tree,counter)
(1...n).each do |i|
  print "#{ans[i]} "
end
puts ans[n]


require 'set'

n, m, tax = gets.strip.split.map(&:to_i)
abc = readlines.map {|line| line.strip.split.map(&:to_i) }
abc = abc.map {|a, b, c| [a, b, -1*(c-tax)] }

# 特殊な隣接リストを構築
graph = Array.new(n+1) { [] }
r_graph = Array.new(n+1) { [] }
abc.each do |a, b, c|
  graph[a] << [b, c]
  r_graph[b] << [a, c]
end

# スタートとゴールから到達可能なノードを列挙
from_1 = Set.new
from_n = Set.new

cq = [1]
checked=Set.new
until cq.empty?
  c = cq.shift
  from_1 << c
  checked << c
  graph[c].each do |to, _|
    next if checked.include?(to)
    cq.push(to)
  end
end

cq = [n]
checked=Set.new
until cq.empty?
  c = cq.shift
  from_n << c
  checked << c
  r_graph[c].each do |to, _|
    next if checked.include?(to)
    cq.push(to)
  end
end

available_nodes = from_1 & from_n
available_roads = abc.select {|a, b, _| available_nodes.include?(a) || available_nodes.include?(b) }

# ベルマンフォードで経路探索、最短経路を算出
INF = 1 << 60
dist=Array.new(n+1, INF)
dist[1]=0
updated=false

available_nodes.each do
  updated=false
  available_roads.each do |from, to, cost|
    if dist[from] != INF && dist[to] > dist[from] + cost
      dist[to] = dist[from] + cost
      updated = true
    end
  end
  break unless updated
end

if updated
  p -1
  exit
end

#p abc
#p [from_1, from_n]
#p available_roads
#p dist
p [-1*dist[n], 0].max

Score : 500 points

Problem Statement

There is a directed graph with N vertices numbered 1 to N and M edges.
The i-th edge is directed from Vertex A_i to Vertex B_i, and there are C_i coins placed along that edge.
Additionally, there is a button on Vertex N.

We will play a game on this graph.
You start the game on Vertex 1 with zero coins, and head for Vertex N by traversing the edges while collecting coins.
It takes one minute to traverse an edge, and you can collect the coins placed along the edge each time you traverse it.
As usual in games, even if you traverse an edge once and collect the coins, the same number of coins will reappear next time you traverse that edge, which you can collect again.

When you reach Vertex N, you can end the game by pressing the button. (You can also choose to leave Vertex N without pressing the button and continue traveling.)
However, when you end the game, you will be asked to pay T \times P coins, where T is the number of minutes elapsed since the start of the game. If you have less than T \times P coins, you will have to pay all of your coins instead.

Your score will be the number of coins you have after this payment.
Determine if there exists a maximum value of the score that can be obtained. If the answer is yes, find that maximum value.

Constraints

2 \leq N \leq 2500

1 \leq M \leq 5000

1 \leq A_i, B_i \leq N

1 \leq C_i \leq 10^5

0 \leq P \leq 10^5

All values in input are integers.

Vertex N can be reached from Vertex 1.

Input

Input is given from Standard Input in the following format:

N M P
A_1 B_1 C_1
:
A_M B_M C_M

Output

If there exists a maximum value of the score that can be obtained, print that maximum value; otherwise, print -1.

Sample Input 1

3 3 10
1 2 20
2 3 30
1 3 45

Sample Output 1

35

There are two ways to travel from Vertex 1 to Vertex 3:

Vertex 1 \rightarrow 2 \rightarrow 3: You collect 20 + 30 = 50 coins on the way. After two minutes from the start of the game, you press the button, pay 2 \times 10 = 20 coins, and you have 50 - 20 = 30 coins left.

Vertex 1 \rightarrow 2: You collect 45 coins on the way. After one minute from the start of the game, you press the button, pay 1 \times 10 = 10 coins, and you have 45 - 10 = 35 coins left.

Thus, the maximum score that can be obtained is 35.

Sample Input 2

2 2 10
1 2 100
2 2 100

Sample Output 2

-1

The edge extending from Vertex 1 takes you to Vertex 2. If you then traverse the edge extending from Vertex 2 to itself t times and press the button, your score will be 90 + 90t. Thus, you can infinitely increase your score, which means there is no maximum value of the score that can be obtained.

Sample Input 3

4 5 10
1 2 1
1 4 1
3 4 1
2 2 100
3 3 100

Sample Output 3

0

There is no way to travel from Vertex 1 to Vertex 4 other than traversing the edge leading from Vertex 1 to Vertex 4 directly. You will pick up one coin along this edge, but after being asked to paying 10 coins, your score will be 0.

Note that you can collect an infinite number of coins if you traverse the edge leading from Vertex 1 to Vertex 2, but this is pointless since you can no longer reach Vertex 4 and end the game.


n, m, point = gets.strip.split.map(&:to_i)
abc = readlines.map {|line| line.strip.split.map(&:to_i) }

graph = Array.new(n+1) { [] }
reverse_graph = Array.new(n+1) { [] }
edges = []

abc.each do |a, b, c|
  edges << [a, b, (c-point)*-1]
  graph[a] << [b, (c-point)*-1]
  reverse_graph[b] << [a, (c-point)*-1]
end

def reachable(graph, s)
  visit = []
  visit.push(s)
  q=[s]
  until q.empty?
    u=q.shift
    graph[u].each do |n, _|
      next if visit.include?(n)
      visit.push(n)
      q.push(n)
    end
  end
  visit
end

reachable_from_s = reachable(graph, 1)
reachable_to_g = reachable(reverse_graph, n)
retrenched_nodes = (1..n).to_a.select {|t| reachable_from_s.include?(t) && reachable_to_g.include?(t) }

edges = edges.select {|from, to,  _| retrenched_nodes.include?(from) && retrenched_nodes.include?(to) }

INF = 1<<60
dist = Array.new(n+1, INF)
dist[1]=0
c=0

loop do
  updated = false
  edges.each do |from, to, cost|
    if dist[from] != INF && dist[to] > dist[from] + cost
      dist[to] = dist[from] + cost
      updated=true
    end
  end
  break unless updated
  c+=1
  if c==n
    p -1
    exit
  end
end

p [-dist[n], 0].max


require 'set'

n, m, point = gets.strip.split.map(&:to_i)
N=n
abc = readlines.map {|line| line.strip.split.map(&:to_i) }

graph = Array.new(n+1) { [] }
reverse_graph = Array.new(n+1) { [] }
edges = []

abc.each do |a, b, c|
  edges << [a, b, (c-point)*-1]
  graph[a] << [b, (c-point)*-1]
  reverse_graph[b] << [a, (c-point)*-1]
end

def reachable(graph, s)
  visit = Set.new
  visit.add(s)
  q=[s]
  until q.empty?
    u=q.shift
    graph[u].each do |n, _|
      next if visit.include?(n)
      visit.add(n)
      q.push(n)
    end
  end
  visit
end

reachable_from_s = reachable(graph, 1)
reachable_to_g = reachable(reverse_graph, n)
retrenched_nodes = reachable_from_s & reachable_to_g

edges = edges.select {|from, to,  _| retrenched_nodes.include?(from) && retrenched_nodes.include?(to) }

INF = Float::INFINITY
#dist = Array.new(n+1, INF)
#dist[1]=0
#c=0

def bellman_ford(edge_list, s, max_count)
  d = Array.new(N+1, INF)
  d[s] = 1
  c = 0
  loop do
    is_update = false
    edge_list.each do |(u, v, c)|
      if d[u] != INF && d[u] + c < d[v]
        d[v] = d[u] + c
        is_update = true
      end
    end
    return d if !is_update
    c += 1
    return nil if c == max_count
  end
end

ans = bellman_ford(edges, 1, retrenched_nodes.size)
if ans.nil?
  p -1
  exit
end

p [-ans[n], 0].max


N, M, P = gets.split.map(&:to_i)
Vertex, Edge = Struct.new(:distance), Struct.new(:from, :to, :weight)
vertices = Array.new(N.next) {|i| Vertex.new(i == 1 ? 0 : -Float::INFINITY)}
edges = $<.read.split(?\n).map do |x|
  a, b, c = x.split.map(&:to_i)
  from, to = vertices.values_at(a, b)
  weight = c - P
  Edge.new(from, to, weight)
end

N.times do
  edges.each do |edge|
    next if edge.to.distance < edge.from.distance + edge.weight
    edge.to.distance = edge.from.distance + edge.weight
  end
end

(puts 0; exit) if vertices[N].distance.negative?
(puts '-1'; exit) if edges.any? {|edge| edge.from.distance + edge.weight > edge.to.distance}
puts vertices[N].distance


n = gets.chomp.to_i
h = gets.chomp.split(" ").map(&:to_i)
i = 0
j = 1
if n == 1
    puts "Yes"
    exit
end

until j == n
    if h[i] < h[j]
        h[j] = h[j] - 1
    end
    if h[i] > h[j]
        puts "No"
        exit
    end
    j += 1
    i += 1
end
puts "Yes"

Score : 300 points

Problem Statement

There are N squares arranged in a row from left to right. The height of the i-th square from the left is H_i.

For each square, you will perform either of the following operations once:

Decrease the height of the square by 1.

Do nothing.

Determine if it is possible to perform the operations so that the heights of the squares are non-decreasing from left to right.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq H_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
H_1 H_2 ... H_N

Output

If it is possible to perform the operations so that the heights of the squares are non-decreasing from left to right, print Yes; otherwise, print No.

Sample Input 1

5
1 2 1 1 3

Sample Output 1

Yes

You can achieve the objective by decreasing the height of only the second square from the left by 1.

Sample Input 2

4
1 3 2 1

Sample Output 2

No

Sample Input 3

5
1 2 3 4 5

Sample Output 3

Yes

Sample Input 4

1
1000000000

Sample Output 4

Yes


#input of int(split by space)
def get_i()
  return gets.chomp.split(" ").map(&:to_i)
end
#input of float(split by space)
def get_f()
  return gets.chomp.split(" ").map(&:to_f)
end
#input of string(split by space)
def get()
  return gets.chomp.split(" ")
end
#input of string(split per one character)
def get_nsp()
  return gets.chomp.split("")
end
#yes or no decision
def yn_judge(bool,y="Yes",n="No")
  return bool ? y : n
end
#create of array
def array(size1,init=nil,size2=-1)
  if size2==-1
      return Array.new(size1){init}
  else
      return Array.new(size2){Array.new(size1){init}}
  end
end

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

def min(a,b)
    return a>b ? b : a
end

INF=Float::INFINITY

N=gets.to_i
h=get_i
flag=true
(N-1).times do|i|
  if h[i]>h[i+1]
      if h[i]-h[i+1]==1
          h[i]-=1
      end
  end
end
(N-1).times do|i|
    if h[i]>h[i+1]
        flag=false
        break
    end
end
puts yn_judge(flag)


N=gets.to_i
H=gets.split.map(&:to_i)
ans="Yes"

p0=0
p1=H.shift

(N-1).times {
    p2=H.shift
    if p2>=p1
        p0=p1
        p1=(p2-1>=p1 ? p2-1 : p2)
        next
    elsif p2>=p1-1 && p1-1>=p0
        p0=p1-1
        p1=(p2-1>=p1 ? p2-1 : p2)
        next
    else
        ans="No"
        break
    end
}

puts ans


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

if n == 1
  puts "Yes"
  exit
end

m = [h[0]]
1.step(n - 1) do |i|
  m << [h[i], m[-1]].max
end
puts (h.zip(m).any? {|i, j| i - j < -1 })? "No" : "Yes"


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

if ary.size == 1
    puts "Yes"
    exit
end

(n-1).times do |i|
    if  ary[i] > ary[i+1] + 1
        puts "No"
        exit
    elsif ary[i] == ary[i+1] + 1
        ary[i] -= 1
    end
end

puts ary == ary.sort ? "Yes" : "No"


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

for i in 0..(n-2)
    if h[i]-1 > h[i+1]
        puts "No"
        exit
    end
end
puts "Yes"


i = gets.to_i
t = gets.split(" ").map(&:to_i)
t << -1
(0..i-1).each do |n|
  if n == i-1
      puts "Yes"
      break
  end
  if t[n]>t[n+1]
      t[n] = t[n]-1
      if t[n]>t[n+1]
          puts "No"
          break
      end
  end
end


n=gets.to_i
h=gets.split.map(&:to_i)
p="yes"
(1..n-1).each do |i|
  if h[i]-h[i-1]>=-1 then
    p="No"
    break
  else
    p="yes"
  end
  end
  print p


n=gets.chop.to_i
array=gets.chomp.split(" ").map(&:to_i)
a='Yes'
num=1

if array[0]>=array[n-1]
a='No'
end
for i in 0...n-1 do

  if array[i+1]-array[i]<-1
  a='No'
  end

  if array[i+1]-array[i]==-1          #&&array[i+2]-array[i+1]==-1
num=num-1

end
end
if num<0
a='No'
end
if n==1
    a='Yes'
end
puts a


n=gets.chop.to_i
array=gets.chomp.split(" ").map(&:to_i)
a='Yes'

if array[0]>=array[n-1]
a='No'
end
for i in 0...n-1 do

  if array[i+1]-array[i]<-1
  a='No'
  end

  if array[i+1]-array[i]==-1&&array[i+2]-array[i+1]==-1
  a='No'
  end
end
if n==1
    a='Yes'
end
puts a


n=gets.chop.to_i
array=gets.chomp.split(" ").map(&:to_i)
a='Yes'

if array[0]>array[n-1]
a='No'
end
for i in 0...n-1 do
  if array[i+1]-array[i]==1||array[i+1]-array[i]==-1
  a='No'
  end

end
if n==1
    a='Yes'
end
puts a


n=gets.chop.to_i
array=gets.chomp.split(" ").map(&:to_i)
a='No'
num=0
num2=0
if n==1
    a='Yes'
end
if array[0]<array[n-1]
 for i in 0...n-1 do
  if array[i]<=array[i+1]
   num+=num
   end
  if array[i+1]-array[i]<=1
   num2+=num2
   end
  end
  if num==n&&num2==n
a='Yes'
  end
end

puts a


# Your code here!
n = gets.chop!
str = gets.split

str.map!{|s| s.to_i}
# array = array.map{|n| n.to_i}

def test(str)
for e in 1...str.length
    if str[e] - 1 < str[e-1]
        return "no"
    elsif str[e] - 1 == str[e-1]
        str[e] = str[e] - 1
    end
end

return "yes"
end

print test(str)


n = gets.to_i
if n == 1
  puts "Yes"
  exit
end
h = gets.split.map(&:to_i)
h[0] -= 1
if h[0] > h[1]
  puts "No"
  exit
else
  h[1] -= 1 if h[0] < h[1]
end
2.upto(n-2) do |i|
  if (h[i] > h[i+1]) && (h[i-1] >= h[i])
    puts "No"
    exit
  else
    h[i] -= 1
  end
end

if h[n-2] > h[n-1]
  puts "No"
else
  puts "Yes"
end


math_num = gets.to_i
heights = gets.split(" ").map(&:to_i)

if heights.length == 1
else
  for i in (1..heights.length-1) do
    if heights[i-1] <= heights[i] + 1
      next
    else
      puts 'No'
      exit
    end
  end
end
puts 'Yes'


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

def is_not_exist_plus_diff? m1,m2,p_ary
  x = p_ary.find{|p_idx| p_idx > m1}
  y = p_ary.reverse.find{|p_idx| p_idx < m2}
  x > y
rescue
  # findできない場合は、diffが-1,-1,2や2,-1,-1などngなパターンのみ。
  true
end

diff_ary = (ary.size - 1).times.map.with_index {|i,idx| [ary[i+1] - ary[i],idx]}
if diff_ary.any?{|diff| diff.first < -1}
  puts "No"
else
  # 最初のifで-2以下のdiffは無くなっている。
  m_a = minus_diff_ary_idx = diff_ary.select{|diff| diff.first == -1}.map(&:last)
  p_a = plus_diff_ary_idx = diff_ary.select{|diff| diff.first > 0}.map(&:last)
  if m_a.size == 0 || n == 1
    puts "Yes"
  elsif (m_a.size - 1).times.any? do |i|
      is_not_exist_plus_diff?(m_a[i],m_a[i+1],p_a)
    end
    puts "No"
  else
    puts "Yes"
  end
end


# 5
# 1 2 1 1 3
_ = gets.split.map(&:to_i).first
hs = gets.split.map(&:to_i)

height = hs.first
result = true
hs.each do |h|
  if height > h
    result = false
    break
  end
  next if height == h
  height = h - 1
end

puts result ? 'Yes' : 'No'


#!/bin/env ruby
#

s = STDIN.readlines

n = s[0].to_i
h = s[1].split(" ").map(&:to_i)

flag = true

0.upto(h.size - 2) do |i|
  if (h[i] - h[i+1]) > 1
    puts "No"
    exit
  elsif (h[i] - h[i+1]) == 1
    if !flag
      puts "No"
      exit
    end
    h[i] = h[i] - 1
    flag = false
  elsif (h[i] - h[i+1]) == 0
    flag = false
  else
    flag = true
  end
end

if h.size != 1 && (h[-1] != h.sort[-1])
  puts "No"
  exit
end

puts "Yes"


N = gets.to_i
heights = gets.split.map(&:to_i)
answer = true
previous = heights.shift

heights.each do |current|
  if previous < current
    previous = current - 1
  elsif previous == current
  elsif previous == current - 1
  else
    answer = false
    break
  end
end
puts answer ? 'Yes' : 'No'


n = gets.strip.to_i
h = gets.strip.split.map(&:to_i)

if (h.min+1 == h.max)
  puts 'Yes'
  exit
end

min=0
(n-1).times do |index|

  if h[index]-1 > h[index+1] || h[index+1] < min
    puts 'No'
    exit
  end

  min = h[index]  if h[index] > min
  if h[index]-1 == h[index+1]
    min = h[index+1]
  end

end

puts 'Yes'


n = gets.strip.to_i
h = gets.strip.split.map(&:to_i)

if (h.min+1 == h.max)
  puts 'Yes'
  exit
end

min=0
(n-1).times do |index|

  if h[index]-1 > h[index+1] || h[index+1] < min
    puts 'Yes'
    exit
  end

  min = h[index]  if h[index] > min
  if h[index]-1 == h[index+1]
    min -=1
  end

end

puts 'Yes'


n = gets.to_i
array = readline.split(' ').map(&:to_i)

flag = false
ans = 'No'

array.each_with_index do |e, i|
  if i == array.size - 1
    break
  end

  unless flag
    if e > array[i + 1]
      if e - array[i + 1] > 1
        ans = 'No'
        break
      end
      array[i + 1] = array[i + 1] + 1
      flag = true
      next
    end
  end

  if e > array[i + 1]
    ans = 'No'
    break
  end

  ans = "Yes"
end
if n == 1
  ans = "Yes"
end
puts ans


# coding: utf-8
n = gets.to_i
h = gets.split(" ").map{|e| e.to_i}

ans = "Yes"
min = 0
# 全ての要素に対して、前の要素以上となっていることを保証する
h.each_with_index do |e, i|
  break if i == n - 1

  if h[i] <= h[i + 1]
    # ok
    next
  elsif h[i] - 1 <= h[i + 1] and h[i] - 1 >= min
    h[i] -= 1
    min = h[i]
    # ok
    next
  else
    ans = "No"
    break
  end
end
puts ans


#1行目
hoge = gets.chomp.to_i

#2行目読み込み
x = gets.chomp.split(" ").map!{|i | i.to_i}
x[hoge] = 10000000000

sageta = Array.new

unchi = 0

for i in 0..(hoge-1) do
  if x[i] - x.min > 1
    unchi = 1
    break
  elsif
    x[i] - x[i+1] > 1
    unchi = 1
    break
  elsif x[i] - x[i+1] == 1 and sageta[i] == nil
    sageta[i + 1] = "下げたよ！"
  elsif x[i] - x[i+1] == 1 and sageta[i] != nil
    unchi = 1
    break
  end
  x[0] = 0
end

if unchi > 0
  print "No"
else print "Yes"
end


n = gets.to_i
hs = gets.split.map(&:to_i)

(0..n-2).each do |i|
  # if hs[i] > hs[i+1]
  if hs[i] > hs[i+1] || (i != 0 && hs[i] -1 >= hs[i-1])
    hs[i] -= 1
  end
end

ans = 'Yes'
(0..n-2).each do |i|
  if hs[i] > hs[i+1]
    ans = 'No'
  end
end

# puts hs.inspect

puts ans


n = gets.to_i

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

last = hs[0]
for i in 1...n do
  if last < hs[i] then
    last = hs[i]
  elsif hs[i] == last || hs[i] == last - 1 then
  else
    puts 'No'
    return
  end
end
puts 'Yes'


n=gets
h=gets.split.map(&:to_i)
h.reverse!
(h.size-1).times do |i|
  if h[i] >= h[i+1]
    next
  else
    h[i+1] -= 1
    if h[i] >= h[i+1]
      next
    else
      puts "No"
      exit
    end
  end
end

puts "Yes"


def min2(a,b)
    return a>b ? b : a
end
def max2(a,b)
    return a<b ? b : a
end

L,R = gets.split.map{|i| i.to_i}

if R - L >= 2018
    puts 0
    exit
end

lst =(L..R).map{|i| i % 2019}
lst.sort!()

puts (lst[0]*lst[1])% 2019

Score : 300 points

Problem Statement

You are given two non-negative integers L and R.
We will choose two integers i and j such that L \leq i < j \leq R.
Find the minimum possible value of (i \times j) \mbox{ mod } 2019.

Constraints

All values in input are integers.

0 \leq L < R \leq 2 \times 10^9

Input

Input is given from Standard Input in the following format:

L R

Output

Print the minimum possible value of (i \times j) \mbox{ mod } 2019 when i and j are chosen under the given condition.

Sample Input 1

2020 2040

Sample Output 1

2

When (i, j) = (2020, 2021), (i \times j) \mbox{ mod } 2019 = 2.

Sample Input 2

4 5

Sample Output 2

20

We have only one choice: (i, j) = (4, 5).


L, R = gets.chomp.split(" ").map(&:to_i)
MOD = 2019
MOD2 = 673
MOD3 = 3

ans = 0
if (R - L) < (MOD - 1) && (L % MOD) < (R % MOD) then
  if ((R - L) > (MOD2 - 1) || (L % MOD2) >= (R % MOD2)) && ((R - L) > (MOD3 - 1) || (L % MOD3) >= (R % MOD3)) then
    # 3 * 673 = 2019
    ans = 0
  else
    s = [L % MOD, (R % MOD) + MOD].min
    ans = (s * (s + 1)) % MOD
  end
end

puts ans


l, r = gets.split.map &:to_i
p 0 if r-l>=2019
ans = Float::INFINITY
(l...r).each do |i|
  (i+1..r).each do |j|
    m = i*j%1029
    ans = m if m < ans
  end
end
p ans


L, R = gets.chomp.split.map(&:to_i)

def solve
  if (R - L) > 2019
    p 0
    return
  end

  mods = []
  L.upto(R).each do |i|
    mod = i % 2019
    mods << mod
  end

  # p [L, R]
  # p mods

  mods.sort!
  # p mods[0]
  # p mods[1]

  p mods[0] * mods[1]
end

solve


L, R = gets.split.map(&:to_i)
array = (L..R).to_a

min = 2018
array.combination(2) do |a, b|
  (puts min ; exit) if min == 0
  mod = (a * b) % 2019
  min = mod if mod < min
end

puts min