n = gets.to_i
a = gets.split.map(&:to_i).reverse
d = a.length
limit_by_bottom = [a.first, a.first]
q = []
a.each.with_index(1) do |ai, idx|
  di = d - idx
  # 下の深さからの制約（この階層のleaf数にプラスして下の階層のノード数が必要）
  bl = idx == 1 ? limit_by_bottom : [(limit_by_bottom[0]+ai).divmod(2).sum, limit_by_bottom[1]+ai]
  # この深さに対する制約
  cl = [idx == 1 ? ai : ai + 1, 2**di] # 存在可能なノード数の制約
  # 新制約
  nl = [[bl[0], cl[0]].max, [bl[1], cl[1]].min]

  # 制約が矛盾したら終了
  if nl[0] > nl[1]
    puts '-1'
    exit 0
  end

  # 矛盾しなければ上の深さに対する制約を設定して次へ
  q << [nl, ai]
  limit_by_bottom = nl
end
count = 0
lt = nil
q.reverse.each do |qi|
  if lt
    n = [lt[1], qi[0][1]].min
    count += n
    lt = [n - qi[1], (n - qi[1]) * 2]
  else
    count += qi[0].max
    lt = [qi[0].max - qi[1], (qi[0].max - qi[1]) * 2]
  end
end
puts count

Score : 600 points

Problem Statement

Given is an integer sequence of length N+1: A_0, A_1, A_2, \ldots, A_N. Is there a binary tree of depth N such that, for each d = 0, 1, \ldots, N, there are exactly A_d leaves at depth d? If such a tree exists, print the maximum possible number of vertices in such a tree; otherwise, print -1.

Notes

A binary tree is a rooted tree such that each vertex has at most two direct children.

A leaf in a binary tree is a vertex with zero children.

The depth of a vertex v in a binary tree is the distance from the tree's root to v. (The root has the depth of 0.)

The depth of a binary tree is the maximum depth of a vertex in the tree.

Constraints

0 \leq N \leq 10^5

0 \leq A_i \leq 10^{8} (0 \leq i \leq N)

A_N \geq 1

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_0 A_1 A_2 \cdots A_N

Output

Print the answer as an integer.

Sample Input 1

3
0 1 1 2

Sample Output 1

7

Below is the tree with the maximum possible number of vertices. It has seven vertices, so we should print 7.

Sample Input 2

4
0 0 1 0 2

Sample Output 2

10

Sample Input 3

2
0 3 1

Sample Output 3

-1

Sample Input 4

1
1 1

Sample Output 4

-1

Sample Input 5

10
0 0 1 1 2 3 5 8 13 21 34

Sample Output 5

264


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

if [2,4,5,7,9].include?(n[-1])
  puts 'hon'
elsif [0,1,6,8].include?(n[-1])
  puts 'pon'
elsif [3].include?(n[-1])
  puts 'bon'
end

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

Score: 100 points

Problem Statement

The cat Snuke wants to play a popular Japanese game called ÅtCoder, so Iroha has decided to teach him Japanese.

When counting pencils in Japanese, the counter word "本" follows the number. The pronunciation of this word varies depending on the number. Specifically, the pronunciation of "本" in the phrase "N 本" for a positive integer N not exceeding 999 is as follows:

hon when the digit in the one's place of N is 2, 4, 5, 7, or 9;

pon when the digit in the one's place of N is 0, 1, 6 or 8;

bon when the digit in the one's place of N is 3.

Given N, print the pronunciation of "本" in the phrase "N 本".

Constraints

N is a positive integer not exceeding 999.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

16

Sample Output 1

pon

The digit in the one's place of 16 is 6, so the "本" in "16 本" is pronounced pon.

Sample Input 2

2

Sample Output 2

hon

Sample Input 3

183

Sample Output 3

bon

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.


#標準入力
#整数一文字の場合
n = gets.to_i

s = gets.to_s

if s.length <= n then
	puts s
else
	s = s.slice(0..n-1) + "..."
	puts s
end

Score: 200 points

Problem Statement

We have a string S consisting of lowercase English letters.

If the length of S is at most K, print S without change.

If the length of S exceeds K, extract the first K characters in S, append ... to the end of them, and print the result.

Constraints

K is an integer between 1 and 100 (inclusive).

S is a string consisting of lowercase English letters.

The length of S is between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

K
S

Output

Print a string as stated in Problem Statement.

Sample Input 1

7
nikoandsolstice

Sample Output 1

nikoand...

nikoandsolstice has a length of 15, which exceeds K=7.

We should extract the first 7 characters in this string, append ... to the end of them, and print the result nikoand....

Sample Input 2

40
ferelibenterhominesidquodvoluntcredunt

Sample Output 2

ferelibenterhominesidquodvoluntcredunt

The famous quote from Gaius Julius Caesar.


K = gets.split.map(&:to_i)
S = gets.split.map(&:to_s)

if S.size <= K
  puts("S")
else
  ans = S.shift(K)
  puts("ans")
end


a, b, h, m = gets.chomp.split(" ").map(&:to_f)

i = Math::PI * 2 * (h + m / 60 ) / 12
j = Math::PI * 2 * m / 60

puts Math.sqrt( a**2 + b**2 - 2 * a * b * Math.cos( ( i - j ).abs ) )

Score: 300 points

Problem Statement

Consider an analog clock whose hour and minute hands are A and B centimeters long, respectively.

An endpoint of the hour hand and an endpoint of the minute hand are fixed at the same point, around which each hand rotates clockwise at constant angular velocity. It takes the hour and minute hands 12 hours and 1 hour to make one full rotation, respectively.

At 0 o'clock, the two hands overlap each other. H hours and M minutes later, what is the distance in centimeters between the unfixed endpoints of the hands?

Constraints

All values in input are integers.

1 \leq A, B \leq 1000

0 \leq H \leq 11

0 \leq M \leq 59

Input

Input is given from Standard Input in the following format:

A B H M

Output

Print the answer without units. Your output will be accepted when its absolute or relative error from the correct value is at most 10^{-9}.

Sample Input 1

3 4 9 0

Sample Output 1

5.00000000000000000000

The two hands will be in the positions shown in the figure below, so the answer is 5 centimeters.

Sample Input 2

3 4 10 40

Sample Output 2

4.56425719433005567605

The two hands will be in the positions shown in the figure below. Note that each hand always rotates at constant angular velocity.


require 'bigdecimal'
require 'bigdecimal/util'
require "bigdecimal/math"
include BigMath

A, B, H, M = gets.chomp.split.map(&:to_i)

digA = (30.0 * H).modulo(360) + 0.5 * M
digB = 6.0 * M
theta = (digA - digB).abs.modulo(180) * BigMath::PI(12) / 180.0

if digA == digB then
  puts (A - B).abs
elsif (digA - digB).abs.modulo(180) == 0 then
  puts (A + B).abs
else
  puts BigMath::sqrt(( A**2 + B**2 - (2 * A * B * BigMath::cos(theta,12)) ).abs, 12)
end


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

map = []
m.times do
    a, b = gets.chomp.split.map(&:to_i)
    map[a] = map[a] || []
    map[a] << b
    map[b] = map[b] || []
    map[b] << a
end

dp = []
dp[1] = 1

queue = Queue.new
queue << 1

loop do
    break if queue.empty?
    a = queue.pop
    bs = map[a]
    bs.each do |b|
        if dp[b].nil?
            dp[b] = a
            queue << b
        end
    end
end

result = dp[(2..n)]

if result.include?(nil)
    puts 'No'
else
    puts 'Yes'
    result.each do |r|
        puts r
    end
end

Score: 400 points

Problem Statement

There is a cave.

The cave has N rooms and M passages. The rooms are numbered 1 to N, and the passages are numbered 1 to M. Passage i connects Room A_i and Room B_i bidirectionally. One can travel between any two rooms by traversing passages. Room 1 is a special room with an entrance from the outside.

It is dark in the cave, so we have decided to place a signpost in each room except Room 1. The signpost in each room will point to one of the rooms directly connected to that room with a passage.

Since it is dangerous in the cave, our objective is to satisfy the condition below for each room except Room 1.

If you start in that room and repeatedly move to the room indicated by the signpost in the room you are in, you will reach Room 1 after traversing the minimum number of passages possible.

Determine whether there is a way to place signposts satisfying our objective, and print one such way if it exists.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

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

1 \leq A_i, B_i \leq N\ (1 \leq i \leq M)

A_i \neq B_i\ (1 \leq i \leq M)

One can travel between any two rooms by traversing passages.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
:
A_M B_M

Output

If there is no way to place signposts satisfying the objective, print No.

Otherwise, print N lines. The first line should contain Yes, and the i-th line (2 \leq i \leq N) should contain the integer representing the room indicated by the signpost in Room i.

Sample Input 1

4 4
1 2
2 3
3 4
4 2

Sample Output 1

Yes
1
2
2

If we place the signposts as described in the sample output, the following happens:

Starting in Room 2, you will reach Room 1 after traversing one passage: (2) \to 1. This is the minimum number of passages possible.

Starting in Room 3, you will reach Room 1 after traversing two passages: (3) \to 2 \to 1. This is the minimum number of passages possible.

Starting in Room 4, you will reach Room 1 after traversing two passages: (4) \to 2 \to 1. This is the minimum number of passages possible.

Thus, the objective is satisfied.

Sample Input 2

6 9
3 4
6 1
2 4
5 3
4 6
1 5
6 2
4 5
5 6

Sample Output 2

Yes
6
5
5
1
1

If there are multiple solutions, any of them will be accepted.


n = gets.to_i
a, b = Array.new(n) { gets.split.map(&:to_i) }.transpose
mod = 10**9 + 7
h = Hash.new(0)
arr = []
ans = 0

a.zip(b).each do |i, j|
  if i == 0 && j == 0
    ans += 1
  elsif i == 0
    h[0, 1] += 1
  elsif j == 0
    h[-1, 0] += 1
  else
    gcd = i.gcd(j)
    i *= -1 if j < 0
    h[[i / gcd, j.abs / gcd]] += 1
  end
end

h.each do |(i, j), v|
  if i > 0
    arr << 2**v + 2**h[[-j, i]] - 1
    h.delete([-j, i])
  elsif i < 0
    arr << 2**v + 2**h[[j, -i]] - 1
    h.delete([j, -i])
  else
    arr << 2**v
  end
end

ans = (ans + arr.inject { |i, j| i * j % mod }) % mod - 1

puts ans < 0 ? ans + mod : ans

Score: 500 points

Problem Statement

We have caught N sardines. The deliciousness and fragrantness of the i-th sardine is A_i and B_i, respectively.

We will choose one or more of these sardines and put them into a cooler. However, two sardines on bad terms cannot be chosen at the same time.

The i-th and j-th sardines (i \neq j) are on bad terms if and only if A_i \cdot A_j + B_i \cdot B_j = 0.

In how many ways can we choose the set of sardines to put into the cooler? Since the count can be enormous, print it modulo 1000000007.

Constraints

All values in input are integers.

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

-10^{18} \leq A_i, B_i \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
:
A_N B_N

Output

Print the count modulo 1000000007.

Sample Input 1

3
1 2
-1 1
2 -1

Sample Output 1

5

There are five ways to choose the set of sardines, as follows:

The 1-st

The 1-st and 2-nd

The 2-nd

The 2-nd and 3-rd

The 3-rd

Sample Input 2

10
3 2
3 2
-1 1
2 -1
-3 -9
-8 12
7 7
8 1
8 2
8 4

Sample Output 2

479


P = 1000000007
def pow(a,n)
	r = 1
	while n > 0
		if n % 2 == 1
			r *= a
			r %= P
		end
		a *= 2
		a %= P
		n /= 2
	end
	r
end
N = gets.to_i
A = Hash.new{0}
N.times{|n|
	a, b = gets.split.map(&:to_i)
	A[Rational(a, b)] += 1
}
keys = A.keys
r = 1
keys.each{|k|
	next unless A[k]
	if A[-1 / k]
		a = pow(2, A[k]) - 1
		b = pow(2, A[-1 / k]) - 1
		r *= a + b + 1
		r %= P
		A[-1 / k] = nil
	else
		r *= pow(2, A[k])
		r %= P
	end
}
puts (r - 1) % P


S=gets.chop
T=gets.chop
if(S==T.chop)
  print("Yes\n")
else
  print("No\n")
end

Score : 100 points

Problem Statement

Takahashi wants to be a member of some web service.

He tried to register himself with the ID S, which turned out to be already used by another user.

Thus, he decides to register using a string obtained by appending one character at the end of S as his ID.

He is now trying to register with the ID T. Determine whether this string satisfies the property above.

Constraints

S and T are strings consisting of lowercase English letters.

1 \leq |S| \leq 10

|T| = |S| + 1

Input

Input is given from Standard Input in the following format:

S
T

Output

If T satisfies the property in Problem Statement, print Yes; otherwise, print No.

Sample Input 1

chokudai
chokudaiz

Sample Output 1

Yes

chokudaiz can be obtained by appending z at the end of chokudai.

Sample Input 2

snuke
snekee

Sample Output 2

No

snekee cannot be obtained by appending one character at the end of snuke.

Sample Input 3

a
aa

Sample Output 3

Yes


a,b,c,k=`dd`.split.map &:to_i
if k<=a
  p k
elsif a<k && k<=a+b
  p a
else
  p a-(k-(a+b))
end

Score : 200 points

Problem Statement

We have A cards, each of which has an integer 1 written on it. Similarly, we also have B cards with 0s and C cards with -1s.

We will pick up K among these cards. What is the maximum possible sum of the numbers written on the cards chosen?

Constraints

All values in input are integers.

0 \leq A, B, C

1 \leq K \leq A + B + C \leq 2 \times 10^9

Input

Input is given from Standard Input in the following format:

A B C K

Output

Print the maximum possible sum of the numbers written on the cards chosen.

Sample Input 1

2 1 1 3

Sample Output 1

2

Consider picking up two cards with 1s and one card with a 0.
In this case, the sum of the numbers written on the cards is 2, which is the maximum possible value.

Sample Input 2

1 2 3 4

Sample Output 2

0

Sample Input 3

2000000000 0 0 2000000000

Sample Output 3

2000000000


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

cnt = 0
cnt = a
cnt += (k - a - b) * -1 if (k - a - b) > 0
puts cnt


A, B, C, K = gets.chomp.split.map(&:to_i)

ans = 0
if K <= A + B then
    ans = A
else
    ans = A - (K - A - B)
end

puts ans


N,M,X = gets.chomp.split.map(&:to_i)
C = N.times.map{gets.chomp.split.map(&:to_i)}

D = C.transpose
count = 0
(1..M).each do |i|
  if D[i].sum < X
    count += 1
  end
end
if count != 0
  puts "-1"
  exit
end

bits = [0, 1].repeated_permutation(N).to_a
ans = (10 ** 5) * 12

bits.each do |bit|
  rikaido = [0] * (M + 1)
  (0..N-1).each do |j|
    if bit[j] == 1
      (0..M).each do |k|
        rikaido[k] += C[j][k]
        if rikaido[1..M].all? { |n| n >= X }
          ans = rikaido[0] if ans >= rikaido[0]
        end
      end
    end
  end
end

puts ans

Score : 300 points

Problem

Takahashi, who is a novice in competitive programming, wants to learn M algorithms.
Initially, his understanding level of each of the M algorithms is 0.

Takahashi is visiting a bookstore, where he finds N books on algorithms.
The i-th book (1\leq i\leq N) is sold for C_i yen (the currency of Japan). If he buys and reads it, his understanding level of the j-th algorithm will increase by A_{i,j} for each j (1\leq j\leq M).
There is no other way to increase the understanding levels of the algorithms.

Takahashi's objective is to make his understanding levels of all the M algorithms X or higher. Determine whether this objective is achievable. If it is achievable, find the minimum amount of money needed to achieve it.

Constraints

All values in input are integers.

1\leq N, M\leq 12

1\leq X\leq 10^5

1\leq C_i \leq 10^5

0\leq A_{i, j} \leq 10^5

Input

Input is given from Standard Input in the following format:

N M X
C_1 A_{1,1} A_{1,2} \cdots A_{1,M}
C_2 A_{2,1} A_{2,2} \cdots A_{2,M}
\vdots
C_N A_{N,1} A_{N,2} \cdots A_{N,M}

Output

If the objective is not achievable, print -1; otherwise, print the minimum amount of money needed to achieve it.

Sample Input 1

3 3 10
60 2 2 4
70 8 7 9
50 2 3 9

Sample Output 1

120

Buying the second and third books makes his understanding levels of all the algorithms 10 or higher, at the minimum cost possible.

Sample Input 2

3 3 10
100 3 1 4
100 1 5 9
100 2 6 5

Sample Output 2

-1

Buying all the books is still not enough to make his understanding levels of all the algorithms 10 or higher.

Sample Input 3

8 5 22
100 3 7 5 3 1
164 4 5 2 7 8
334 7 2 7 2 9
234 4 7 2 8 2
541 5 4 3 3 6
235 4 8 6 9 7
394 3 6 1 6 2
872 8 4 3 7 2

Sample Output 3

1067


N, *Ss = readlines.map(&:chomp)

as, os, cs = [], 0, 0
Ss.each do |s|
  o, c = 0, 0
  s.chars.each do |x|
    if x == ')'
      o == 0 ? c += 1 : o -= 1
    else
      o += 1
    end
  end
  if o == 0
    cs += c
  elsif c == 0
    os += o
  else
    as << [c, o]
  end
end

as.sort_by{|c, o| c - o}.each do |c, o|
  os -= c
  break if os < 0
  os += o
end
puts os == cs ? 'Yes' : 'No'

Score : 600 points

Problem Statement

A bracket sequence is a string that is one of the following:

An empty string;

The concatenation of (, A, and ) in this order, for some bracket sequence A ;

The concatenation of A and B in this order, for some non-empty bracket sequences A and B /

Given are N strings S_i. Can a bracket sequence be formed by concatenating all the N strings in some order?

Constraints

1 \leq N \leq 10^6

The total length of the strings S_i is at most 10^6.

S_i is a non-empty string consisting of ( and ).

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

If a bracket sequence can be formed by concatenating all the N strings in some order, print Yes; otherwise, print No.

Sample Input 1

2
)
(()

Sample Output 1

Yes

Concatenating (() and ) in this order forms a bracket sequence.

Sample Input 2

2
)(
()

Sample Output 2

No

Sample Input 3

4
((()))
((((((
))))))
()()()

Sample Output 3

Yes

Sample Input 4

3
(((
)
)

Sample Output 4

No


# (()(()))  level
# (      ) --  0
#  ()(  )  -- -1
#     ()   -- -2
# 変化を計算して、
# - 最後0で終わる
# - 途中で0より大きくなることがない
# 変化量の合計は並べる順番に依存しない。
# 0より大きくならないようにするには、できるだけ深く潜るようにすればいい。

N = gets.to_i
LevelInfo = N.times.map do
  s = gets.chomp
  level = 0
  highest = 0
  s.each_char do |c|
    if c == '('
      level -= 1
    else
      level += 1
      highest = level if level > highest
    end
  end
  [level, highest]
end

# 変化の合計量のチェック
level_sum = LevelInfo.map{|level, highest| level}.sum()
if level_sum != 0
  puts 'No'
  exit(0)
end

# できるだけ0より大きくならないように並べ替える
downs = LevelInfo.select{|level, highest| level < 0}.sort_by{|level, highest| highest}
equals = LevelInfo.select{|level, highest| level == 0}
ups = LevelInfo.select{|level, highest| level > 0}.sort_by{|level, highest| - highest + level}

level_sum = 0
[downs, equals, ups].each do |sub|
  sub.each do |level, highest|
    if level_sum + highest > 0
      puts 'No'
      exit(0)
    end
    level_sum += level
  end
end

puts 'Yes'


s = gets.chomp

case s
when "ABC"
  puts "ARC"
when "ARC"
  puts "ABC"
end

Score : 100 points

Problem Statement

AtCoder Inc. holds a contest every Saturday.

There are two types of contests called ABC and ARC, and just one of them is held at a time.

The company holds these two types of contests alternately: an ARC follows an ABC and vice versa.

Given a string S representing the type of the contest held last week, print the string representing the type of the contest held this week.

Constraints

S is ABC or ARC.

Input

Input is given from Standard Input in the following format:

S

Output

Print the string representing the type of the contest held this week.

Sample Input 1

ABC

Sample Output 1

ARC

They held an ABC last week, so they will hold an ARC this week.


# abc166 B
n, k = gets.split.map(&:to_i)
a = []

sn = Array.new(n, 0)

k.times  {
  d = gets.to_i
  a << gets.split.map(&:to_i)
}

a.each do |aa|
  aa.each do |x|
    sn[x-1] += 1
  end
end

p sn.count(&:zero?)

Score : 200 points

Problem Statement

N Snukes called Snuke 1, Snuke 2, ..., Snuke N live in a town.

There are K kinds of snacks sold in this town, called Snack 1, Snack 2, ..., Snack K. The following d_i Snukes have Snack i: Snuke A_{i, 1}, A_{i, 2}, \cdots, A_{i, {d_i}}.

Takahashi will walk around this town and make mischief on the Snukes who have no snacks. How many Snukes will fall victim to Takahashi's mischief?

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq K \leq 100

1 \leq d_i \leq N

1 \leq A_{i, 1} < \cdots < A_{i, d_i} \leq N

Input

Input is given from Standard Input in the following format:

N K
d_1
A_{1, 1} \cdots A_{1, d_1}
\vdots
d_K
A_{K, 1} \cdots A_{K, d_K}

Output

Print the answer.

Sample Input 1

3 2
2
1 3
1
3

Sample Output 1

1

Snuke 1 has Snack 1.

Snuke 2 has no snacks.

Snuke 3 has Snack 1 and 2.

Thus, there will be one victim: Snuke 2.

Sample Input 2

3 3
1
3
1
3
1
3

Sample Output 2

2


n, k = gets.split.map(&:to_i)
d = []
k.times {
  du = gets.to_i
  d << gets.split.map(&:to_i)
}
d = d.uniq.flatten!
ans = 0
1.upto(n) {|i|
  unless d.include?(i) then ans += 1 end
}
p ans


N, K = gets.split(' ').map(&:to_i)
d = []
a = []

K.times do
  d << gets.to_i
  a += gets.split(' ').map(&:to_i)
end

puts N - a.uniq.size


n, m = gets.split(" ").map(&:to_i)
h_arr = [0] + gets.split(" ").map(&:to_i)

path_arr = []
m.times do
  a, b = gets.split(" ").map(&:to_i)

  path_arr[a] ||= []
  path_arr[a] << b
  path_arr[b] ||= []
  path_arr[b] << a
end
path_arr[n] ||= []

count = 0
path_arr.each_with_index do |arr, i|
  next if i == 0

  good = true
  height = h_arr[i]
  arr&.each do |ind|
    if height <= h_arr[ind]
      good = false
      break
    end
  end

  count += 1 if good
end
puts count

Score : 300 points

Problem Statement

There are N observatories in AtCoder Hill, called Obs. 1, Obs. 2, ..., Obs. N. The elevation of Obs. i is H_i.
There are also M roads, each connecting two different observatories. Road j connects Obs. A_j and Obs. B_j.

Obs. i is said to be good when its elevation is higher than those of all observatories that can be reached from Obs. i using just one road.
Note that Obs. i is also good when no observatory can be reached from Obs. i using just one road.

How many good observatories are there?

Constraints

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq H_i \leq 10^9

1 \leq A_i,B_i \leq N

A_i \neq B_i

Multiple roads may connect the same pair of observatories.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
H_1 H_2 ... H_N
A_1 B_1
A_2 B_2
:
A_M B_M

Output

Print the number of good observatories.

Sample Input 1

4 3
1 2 3 4
1 3
2 3
2 4

Sample Output 1

2

From Obs. 1, you can reach Obs. 3 using just one road. The elevation of Obs. 1 is not higher than that of Obs. 3, so Obs. 1 is not good.

From Obs. 2, you can reach Obs. 3 and 4 using just one road. The elevation of Obs. 2 is not higher than that of Obs. 3, so Obs. 2 is not good.

From Obs. 3, you can reach Obs. 1 and 2 using just one road. The elevation of Obs. 3 is higher than those of Obs. 1 and 2, so Obs. 3 is good.

From Obs. 4, you can reach Obs. 2 using just one road. The elevation of Obs. 4 is higher than that of Obs. 2, so Obs. 4 is good.

Thus, the good observatories are Obs. 3 and 4, so there are two good observatories.

Sample Input 2

6 5
8 6 9 1 2 1
1 3
4 2
4 3
4 6
4 6

Sample Output 2

3


# https://atcoder.jp/contests/abc166/tasks/abc166_c
# at_coder_friends test-all C.rb
# require 'pry'

N, M = gets.split.map(&:to_i)
Hs = gets.split.map(&:to_i)
As = Array.new(M)
Bs = Array.new(M)
m_h = Hash.new{|h,k| h[k] = []}
M.times do |i|
  As[i], Bs[i] = gets.split.map(&:to_i)
  m_h[As[i]] << Bs[i]
  m_h[Bs[i]] << As[i]
end

#p "m_h=#{m_h}"

fond_indexes = []
N.times do |i|
  hs = m_h[i+1]
  myh = Hs[i]
  if hs.size > 0
    myh_is_highest = hs.all? do |h|
      myh >= Hs[h-1]
    end
    if myh_is_highest
      fond_indexes << i
    end
  end
  #p "i=#{i}, myh=#{myh}, hs=#{hs},fond_indexes=#{fond_indexes}"
end
#p fond_indexes

ans = fond_indexes.size

puts ans


N, M = gets.split.map(&:to_i)
h = []
h = gets.split.map(&:to_i)
a = []
b = []
ma = []
ma = ma.fill(0,0..N-1)
M.times do |i|
  a[i], b[i] = gets.split.map(&:to_i)
  ma[a[i]-1] = [ h[b[i]-1], ma[a[i]-1] ].max
  ma[b[i]-1] = [ h[a[i]-1], ma[b[i]-1] ].max
end
ans = 0
N.times do |i|
  if ma[i] < h[i]
    ans += 1
  end
end
puts ans


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

edges = Array.new(n) { Array.new}

m.times do |_|
  a, b = gets.split(" ").map(&:to_i)
  a -= 1
  b -= 1
  edges[a] << b
  edges[b] << a
end

ans=0

n.times do |i|
  ans += 1 if edges[i].all? { |j| hh[i] > hh[j] }
end

puts ans


arr = Array.new
while line = $stdin.gets
arr << line.chomp.split(" ").map(&:to_i)
end
peak = arr[1]
maps=Array.new(arr[0][0]).fill(0)
(arr[2..-1]).each{|i|
    if peak[i[0]-1] == peak[i[1]-1] then
        maps[i[0]-1] = -1
        maps[i[1]-1] = -1
    elsif peak[i[0]-1] > peak[i[1]-1]
        maps[i[1]-1] = -1
    else
        maps[i[0]-1] = -1
    end
}
puts maps.count(0)


require 'set'; require 'prime'; require 'pp'
INF=Float::INFINITY; MOD=10**9+7
x = gets.chomp.to_i

def divisors(n)
  n.prime_division.inject([1]) do |ary, (p, e)|
    (0..e).map{ |e1| p ** e1 }.product(ary).map{ |a, b| a * b }
  end.sort
end

def uhen(a, b)
  a2 = a*a
  b2 = b*b
  (a2 * a2) + (a2 * a * b) + (a2 * b2) + (a * b * b2) + (b2 * b2)
end

upper = 10**9
divs = divisors(x)
de = false
divs.each do |div|
  p '-----------------------------------------------' if de
  # p div
  a = div
  b = 0
  while true
    a5b5 = a**5 - b**5 # (a-b) * uhen(a,b)
    p [a,b,a5b5] if de
    if a5b5 == x
      puts [a,b].join(' ')
      exit
    elsif upper < a5b5
      break
    else
      a += 1
      b += 1
    end
  end

  a = div
  b = 0
  while true
    a5b5 = a**5 - b**5 # (a-b) * uhen(a,b)
    p [a,b,a5b5] if de
    if a5b5 == x
      puts [a,b].join(' ')
      exit
    elsif upper < a5b5
      break
    else
      a -= 1
      b -= 1
    end
  end
end

Score : 400 points

Problem Statement

Give a pair of integers (A, B) such that A^5-B^5 = X.
It is guaranteed that there exists such a pair for the given integer X.

Constraints

1 \leq X \leq 10^9

X is an integer.

There exists a pair of integers (A, B) satisfying the condition in Problem Statement.

Input

Input is given from Standard Input in the following format:

X

Output

Print A and B, with space in between.
If there are multiple pairs of integers (A, B) satisfying the condition, you may print any of them.

A B

Sample Input 1

33

Sample Output 1

2 -1

For A=2 and B=-1, A^5-B^5 = 33.

Sample Input 2

1

Sample Output 2

0 -1


m=100
x=gets.to_i
c=0
while x>m do
m+=(m/100)
c+=1
end
p c

Score : 200 points

Problem Statement

Takahashi has a deposit of 100 yen (the currency of Japan) in AtCoder Bank.

The bank pays an annual interest rate of 1 % compounded annually. (A fraction of less than one yen is discarded.)

Assuming that nothing other than the interest affects Takahashi's balance, in how many years does the balance reach X yen or above for the first time?

Constraints

101 \le X \le 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X

Output

Print the number of years it takes for Takahashi's balance to reach X yen or above for the first time.

Sample Input 1

103

Sample Output 1

3

The balance after one year is 101 yen.

The balance after two years is 102 yen.

The balance after three years is 103 yen.

Thus, it takes three years for the balance to reach 103 yen or above.

Sample Input 2

1000000000000000000

Sample Output 2

3760

Sample Input 3

1333333333

Sample Output 3

1706


x = gets.to_i
deposit = 100
year = 0
(x-100).times do
  deposit += (deposit * 0.01).truncate
  year += 1
end
print year


$x = gets.chop.to_i

def rec(y, c)
  c += 1
  y = (y * 1.01).floor

  if y >= $x
    puts c
  else
    rec(y, c)
  end
end

rec(100, 0)


a,b,n = gets.split(" ").map(&:to_i)
i = 1
m = 0

until i > n || i > a
  o = (a*i/b) - a*(i/b)
  p o
  if o > m
    m = o
  end
  i += 1
end

print(m)

Score : 400 points

Problem Statement

Given are integers A, B, and N.

Find the maximum possible value of floor(Ax/B) - A × floor(x/B) for a non-negative integer x not greater than N.

Here floor(t) denotes the greatest integer not greater than the real number t.

Constraints

1 ≤ A ≤ 10^{6}

1 ≤ B ≤ 10^{12}

1 ≤ N ≤ 10^{12}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B N

Output

Print the maximum possible value of floor(Ax/B) - A × floor(x/B) for a non-negative integer x not greater than N, as an integer.

Sample Input 1

5 7 4

Sample Output 1

2

When x=3, floor(Ax/B)-A×floor(x/B) = floor(15/7) - 5×floor(3/7) = 2. This is the maximum value possible.

Sample Input 2

11 10 9

Sample Output 2

9


n,m=gets.split.map(&:to_i)
a=gets.split.map(&:to_i)
puts n-a.sum>0 ? n-a.sum : -1

Score : 200 points

Problem Statement

Takahashi has N days of summer vacation.

His teacher gave him M summer assignments. It will take A_i days for him to do the i-th assignment.

He cannot do multiple assignments on the same day, or hang out on a day he does an assignment.

What is the maximum number of days Takahashi can hang out during the vacation if he finishes all the assignments during this vacation?

If Takahashi cannot finish all the assignments during the vacation, print -1 instead.

Constraints

1 \leq N \leq 10^6

1 \leq M \leq 10^4

1 \leq A_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N M
A_1 ... A_M

Output

Print the maximum number of days Takahashi can hang out during the vacation, or -1.

Sample Input 1

41 2
5 6

Sample Output 1

30

For example, he can do the first assignment on the first 5 days, hang out on the next 30 days, and do the second assignment on the last 6 days of the vacation. In this way, he can safely spend 30 days hanging out.

Sample Input 2

10 2
5 6

Sample Output 2

-1

He cannot finish his assignments.

Sample Input 3

11 2
5 6

Sample Output 3

0

He can finish his assignments, but he will have no time to hang out.

Sample Input 4

314 15
9 26 5 35 8 9 79 3 23 8 46 2 6 43 3

Sample Output 4

9


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

arr = Array.new(N, 0)

A.each do |n|
    arr[n-1] += 1
end

arr.each do |a|
    puts a
end

Score : 300 points

Problem Statement

A company has N members, who are assigned ID numbers 1, ..., N.

Every member, except the member numbered 1, has exactly one immediate boss with a smaller ID number.

When a person X is the immediate boss of a person Y, the person Y is said to be an immediate subordinate of the person X.

You are given the information that the immediate boss of the member numbered i is the member numbered A_i. For each member, find how many immediate subordinates it has.

Constraints

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

1 \leq A_i < i

Input

Input is given from Standard Input in the following format:

N
A_2 ... A_N

Output

For each of the members numbered 1, 2, ..., N, print the number of immediate subordinates it has, in its own line.

Sample Input 1

5
1 1 2 2

Sample Output 1

2
2
0
0
0

The member numbered 1 has two immediate subordinates: the members numbered 2 and 3.

The member numbered 2 has two immediate subordinates: the members numbered 4 and 5.

The members numbered 3, 4, and 5 do not have immediate subordinates.

Sample Input 2

10
1 1 1 1 1 1 1 1 1

Sample Output 2

9
0
0
0
0
0
0
0
0
0

Sample Input 3

7
1 2 3 4 5 6

Sample Output 3

1
1
1
1
1
1
0


mod = 10 ** 9 + 7

n, k = gets.to_s.split.map{|e|e.to_i}

# ans = (k..n+1).map{|i| i * (n + n - i + 1) / 2 - i * ( i - 1 ) / 2 + 1 }.sum % mod
ans = n - k + 2
(k..n+1).each do |i|

  ans += i * (n - i + 1)
  ans %= mod if ans >= mod
end

puts ans


N = gets.to_i
A = gets.chomp.split.map(&:to_i)
left = 0
right = N-1
result = 0

B = A.map.with_index { |elem, index| [elem, index]}
C = B.each do |b|
  b << [b[0] * (b[1]-left), b[0] * (right-b[1])].max
end
C = B.sort_by { |b| b[2] }.reverse
C.each do |c|
  if c[1] - left >= right - c[1]
    result += c[0] * (c[1]-left)
    left += 1
  else
    result += c[0] * (right - c[1])
    right -= 1
  end
end

p result

Score : 500 points

Problem Statement

There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.

You can rearrange these children just one time in any order you like.

When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points.

Find the maximum total happiness points the children can earn.

Constraints

2 \leq N \leq 2000

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum total happiness points the children can earn.

Sample Input 1

4
1 3 4 2

Sample Output 1

20

If we move the 1-st child from the left to the 3-rd position from the left, the 2-nd child to the 4-th position, the 3-rd child to the 1-st position, and the 4-th child to the 2-nd position, the children earns 1 \times |1-3|+3 \times |2-4|+4 \times |3-1|+2 \times |4-2|=20 happiness points in total.

Sample Input 2

6
5 5 6 1 1 1

Sample Output 2

58

Sample Input 3

6
8 6 9 1 2 1

Sample Output 3

85


n=gets.to_i
array=gets.split.map(&:to_i)
ans=0
ansArray=Array.new(n,0)
leftsumArray=Array.new(n,0)
rightsumArray=Array.new(n,0)
arrayreverse=array.reverse
hash={}

array.map.with_index{|value,index|
    if hash[value]
        hash[value] = hash[value].push(index)
    else
        hash[value] = [index]
    end
}

leftsum=0
array.map.with_index{|value,index|
    leftsum+=value
    leftsumArray[index]=leftsum
}

rightsum=0
arrayreverse.map.with_index{|value,index|
    rightsum+=value
    rightsumArray[index]=rightsum
}

rightsumArray=rightsumArray.reverse
hash=hash.sort.reverse

leftnow=0
rightnow=n-1

hash.map{|value|
    uresisa=value[0]
    array2=value[1]

    array2.map{|place|
      leftdis=(place-leftnow).abs
      rightdis=(place-rightnow).abs

    #   leftsum=leftsumArray[place-1] ? leftsumArray[place-1] : 0
    #   rightsum=rightsumArray[place+1] ? rightsumArray[place+1] : 0

      if place==0
          leftsum=0
      else
          leftsum=array[0...place].select{|n| n<place}.inject(&:+)
      end

      if place==n-1
            rightsum=0
      else
            rightsum=array[place+1..n-1].select{|n| n<=place}.inject(&:+)
       end

      if leftsum==nil
          leftsum=0
      end

      if rightsum==nil
        rightsum=0
        end

      if leftdis*uresisa + rightsum > leftsum + rightdis*uresisa
          ansArray[leftnow]=uresisa
          ans+=leftdis*uresisa
          leftnow+=1
      else
          ansArray[rightnow]=uresisa
          ans+=rightdis*uresisa
          rightnow-=1
     end

    }

}

puts ans


n = gets.chomp.to_i

count = 0
1.step(n, 1) do |i|
  if i % 3 != 0 && i % 5 != 0
    count = count + i
  end
end

puts count

Score : 200 points

Problem Statement

Let us define the FizzBuzz sequence a_1,a_2,... as follows:

If both 3 and 5 divides i, a_i=\mbox{FizzBuzz}.

If the above does not hold but 3 divides i, a_i=\mbox{Fizz}.

If none of the above holds but 5 divides i, a_i=\mbox{Buzz}.

If none of the above holds, a_i=i.

Find the sum of all numbers among the first N terms of the FizzBuzz sequence.

Constraints

1 \leq N \leq 10^6

Input

Input is given from Standard Input in the following format:

N

Output

Print the sum of all numbers among the first N terms of the FizzBuzz sequence.

Sample Input 1

15

Sample Output 1

60

The first 15 terms of the FizzBuzz sequence are:

1,2,\mbox{Fizz},4,\mbox{Buzz},\mbox{Fizz},7,8,\mbox{Fizz},\mbox{Buzz},11,\mbox{Fizz},13,14,\mbox{FizzBuzz}

Among them, numbers are 1,2,4,7,8,11,13,14, and the sum of them is 60.

Sample Input 2

1000000

Sample Output 2

266666333332

Watch out for overflow.


INF = 10**15
n = gets.to_i
a = gets.split.map(&:to_i)

if n % 2 == 0
  evn = Array.new(n+1,0)
  odd = Array.new(n+1,0)
  for i in 0..(n-1)
    evp = odp = 0
    (i % 2 == 0) ? (evp = a[i]):(odp = a[i])
    evn[i+1] = evn[i] + evp
    odd[i+1] = odd[i] + odp
  end
  ans = -INF
  for i in 0..(n-1)
    if i % 2 == 0
      ans = [ans, evn[i]+odd[n]-odd[i]].max
    end
  end
  ans = [ans, evn[i]].max
  puts ans
else
  dp = Array.new(n).map{Array.new(3).map{Array.new(2,-INF)}}
  ans = -INF
  for i in 0..2
    dp[i][i][0] = a[i]
    dp[i][i][1] = 1
  end
  for i in 0..(n-1)
    for j in 0..2
      if i > j && (i % 2 == j % 2) && dp[i-2][j][1] < n/2
        for k in 0..j
          dp[i][j][0] = [dp[i][j][0], dp[i-2-k][j-k][0]+a[i]].max
        end
        dp[i][j][1] = dp[i-2][j][1] + 1
      end
      ans = [ans, dp[i][j][0]].max if dp[i][j][1] == n/2
    end
  end
  puts ans
end

Score : 600 points

Problem Statement

Given is an integer sequence A_1, ..., A_N of length N.

We will choose exactly \left\lfloor \frac{N}{2} \right\rfloor elements from this sequence so that no two adjacent elements are chosen.

Find the maximum possible sum of the chosen elements.

Here \lfloor x \rfloor denotes the greatest integer not greater than x.

Constraints

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

|A_i|\leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N

Output

Print the maximum possible sum of the chosen elements.

Sample Input 1

6
1 2 3 4 5 6

Sample Output 1

12

Choosing 2, 4, and 6 makes the sum 12, which is the maximum possible value.

Sample Input 2

5
-1000 -100 -10 0 10

Sample Output 2

0

Choosing -10 and 10 makes the sum 0, which is the maximum possible value.

Sample Input 3

10
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 3

5000000000

Watch out for overflow.

Sample Input 4

27
18 -28 18 28 -45 90 -45 23 -53 60 28 -74 -71 35 -26 -62 49 -77 57 24 -70 -93 69 -99 59 57 -49

Sample Output 4

295


input = gets.split("")
if input[2] == input[3] && input[4] == input[5]
    puts "Yes"
else
    puts "No"
end

Score : 100 points

Problem Statement

A string of length 6 consisting of lowercase English letters is said to be coffee-like if and only if its 3-rd and 4-th characters are equal and its 5-th and 6-th characters are also equal.

Given a string S, determine whether it is coffee-like.

Constraints

S is a string of length 6 consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S is coffee-like, print Yes; otherwise, print No.

Sample Input 1

sippuu

Sample Output 1

Yes

In sippuu, the 3-rd and 4-th characters are equal, and the 5-th and 6-th characters are also equal.

Sample Input 2

iphone

Sample Output 2

No

Sample Input 3

coffee

Sample Output 3

Yes


x=gets.to_i
a=0
until x<500
a+=1000
x-=500
end
until x<5
a+=5
x-=5
end
p a

Score : 200 points

Problem Statement

Takahashi loves gold coins. He gains 1000 happiness points for each 500-yen coin he has and gains 5 happiness points for each 5-yen coin he has. (Yen is the currency of Japan.)

Takahashi has X yen. If he exchanges his money so that he will gain the most happiness points, how many happiness points will he earn?

(We assume that there are six kinds of coins available: 500-yen, 100-yen, 50-yen, 10-yen, 5-yen, and 1-yen coins.)

Constraints

0 \leq X \leq 10^9

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

Print the maximum number of happiness points that can be earned.

Sample Input 1

1024

Sample Output 1

2020

By exchanging his money so that he gets two 500-yen coins and four 5-yen coins, he gains 2020 happiness points, which is the maximum number of happiness points that can be earned.

Sample Input 2

0

Sample Output 2

0

He is penniless - or yenless.

Sample Input 3

1000000000

Sample Output 3

2000000000

He is a billionaire - in yen.


# frozen_string_literal: true

k, n = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
a.push k + a[0]
b = []
(1..n).each do |i|
  b.push a[i] - a[i - 1]
end

m = b.index(b.max)

b.delete_at(m)
puts b.reduce(:+)

Score : 300 points

Problem Statement

There is a circular pond with a perimeter of K meters, and N houses around them.

The i-th house is built at a distance of A_i meters from the northmost point of the pond, measured clockwise around the pond.

When traveling between these houses, you can only go around the pond.

Find the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Constraints

2 \leq K \leq 10^6

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

0 \leq A_1 < ... < A_N < K

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K N
A_1 A_2 ... A_N

Output

Print the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Sample Input 1

20 3
5 10 15

Sample Output 1

10

If you start at the 1-st house and go to the 2-nd and 3-rd houses in this order, the total distance traveled will be 10.

Sample Input 2

20 3
0 5 15

Sample Output 2

10

If you start at the 2-nd house and go to the 1-st and 3-rd houses in this order, the total distance traveled will be 10.


i = 0
if N > 1
  nums = N - 1
  nums.times do
    i += nums
    nums -= 1
  end
end
if M > 1
  i += M
end
p i

Score : 100 points

Problem Statement

We have N+M balls, each of which has an integer written on it.

It is known that:

The numbers written on N of the balls are even.

The numbers written on M of the balls are odd.

Find the number of ways to choose two of the N+M balls (disregarding order) so that the sum of the numbers written on them is even.

It can be shown that this count does not depend on the actual values written on the balls.

Constraints

0 \leq N,M \leq 100

2 \leq N+M

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the answer.

Sample Input 1

2 1

Sample Output 1

1

For example, let us assume that the numbers written on the three balls are 1,2,4.

If we choose the two balls with 1 and 2, the sum is odd;

If we choose the two balls with 1 and 4, the sum is odd;

If we choose the two balls with 2 and 4, the sum is even.

Thus, the answer is 1.

Sample Input 2

4 3

Sample Output 2

9

Sample Input 3

1 1

Sample Output 3

0

Sample Input 4

13 3

Sample Output 4

81

Sample Input 5

0 3

Sample Output 5

3


a=gets.chomp.split.map(&:to_i)
b=[*2..a[0]]

c=b.combination(2).to_a
d=[*1..a[1]].combination(2).to_a.length
puts c.length+d


# frozen_string_literal: true

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

puts((N * (N - 1) + M * (M - 1)) / 2)


s = gets.chomp
n = s.length
num1 = ((n-1)/2)
num2 = ((n+3)/2)-1

str1 = s[0,num1]
str2 = s[num2,n]

p s == s.reverse && str1 == str1.reverse && str2 == str2.reverse ? 'Yes' : 'No'

Score : 200 points

Problem Statement

A string S of an odd length is said to be a strong palindrome if and only if all of the following conditions are satisfied:

S is a palindrome.

Let N be the length of S. The string formed by the 1-st through ((N-1)/2)-th characters of S is a palindrome.

The string consisting of the (N+3)/2-st through N-th characters of S is a palindrome.

Determine whether S is a strong palindrome.

Constraints

S consists of lowercase English letters.

The length of S is an odd number between 3 and 99 (inclusive).

Input

Input is given from Standard Input in the following format:

S

Output

If S is a strong palindrome, print Yes;
otherwise, print No.

Sample Input 1

akasaka

Sample Output 1

Yes

S is akasaka.

The string formed by the 1-st through the 3-rd characters is aka.

The string formed by the 5-th through the 7-th characters is aka.
All of these are palindromes, so S is a strong palindrome.

Sample Input 2

level

Sample Output 2

No

Sample Input 3

atcoder

Sample Output 3

No


l = (gets.to_i/3).floor(6)
puts (l**3).floor(6)

Score : 300 points

Problem Statement

Given is a positive integer L.
Find the maximum possible volume of a rectangular cuboid whose sum of the dimensions (not necessarily integers) is L.

Constraints

1 ≤ L ≤ 1000

L is an integer.

Input

Input is given from Standard Input in the following format:

L

Output

Print the maximum possible volume of a rectangular cuboid whose sum of the dimensions (not necessarily integers) is L.
Your output is considered correct if its absolute or relative error from our answer is at most 10^{-6}.

Sample Input 1

3

Sample Output 1

1.000000000000

For example, a rectangular cuboid whose dimensions are 0.8, 1, and 1.2 has a volume of 0.96.

On the other hand, if the dimensions are 1, 1, and 1, the volume of the rectangular cuboid is 1, which is greater.

Sample Input 2

999

Sample Output 2

36926037.000000000000


N = gets.chomp.to_i
array = gets.chomp.split.map(&:to_i)

def count(num)
  return 0 if num <= 1
  return (num - 1) + count(num - 1)
end

(1..N).each do |k|
  duped_array = array.dup
  duped_array.slice!(k - 1)
  banned_array = duped_array
  counter = 0
  banned_array.uniq.each do |num|
    tmp_array = banned_array.dup
    tmp_array.delete(num)
    num_count = banned_array.size - tmp_array.size
    # p num
    if num_count == 0
      next
    end

    counter += count(num_count)
  end
  puts counter
end

Score : 400 points

Problem Statement

We have N balls. The i-th ball has an integer A_i written on it.

For each k=1, 2, ..., N, solve the following problem and print the answer.

Find the number of ways to choose two distinct balls (disregarding order) from the N-1 balls other than the k-th ball so that the integers written on them are equal.

Constraints

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

1 \leq A_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

For each k=1,2,...,N, print a line containing the answer.

Sample Input 1

5
1 1 2 1 2

Sample Output 1

2
2
3
2
3

Consider the case k=1 for example. The numbers written on the remaining balls are 1,2,1,2.

From these balls, there are two ways to choose two distinct balls so that the integers written on them are equal.

Thus, the answer for k=1 is 2.

Sample Input 2

4
1 2 3 4

Sample Output 2

0
0
0
0

No two balls have equal numbers written on them.

Sample Input 3

5
3 3 3 3 3

Sample Output 3

6
6
6
6
6

Any two balls have equal numbers written on them.

Sample Input 4

8
1 2 1 4 2 1 4 1

Sample Output 4

5
7
5
7
7
5
7
5


# ABC 159 D
# https://atcoder.jp/contests/abc159/tasks/abc159_d

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

a_uniq_size = a.uniq.size

class Pattern
  def initialize(i)
    @i = i
    @count = 0
  end

  def increment
    @count = @count + 1
  end

  attr_reader :count

  def calc
    @calc ||= count * (count - 1) / 2
  end
end

h = {}

a.each do |ai|
  h[ai] ||= Pattern.new(ai)
  h[ai].increment
end

base = h.values.map(&:calc).sum

h2 = {}

a.each_with_index do |ai, i|
  pattern = h[ai]
  if h2[ai].nil?
    result = base - (pattern.count - 1)
    h2[ai] = result
  else
    result = h2[ai]
  end
  puts result
end


H,W=gets.split.map &:to_i
FIELD=H.times.map{gets.chomp.chars}

# 選ぶ進行方向は何通り
S=['r','l'].repeated_permutation(H+W-2).to_a
#puts S.inspect

arr=[]
S.each do |s|
  i=0
  j=0
  res=0
  flg=false
  s.each do |m|
    if m=='r'
      i+=1
    elsif m=='l'
	  j+=1
    end
    if i>H-1 || j>W-1
      flg=true
      break
    end
    res+=1 if FIELD[i][j]=='#'
    if !arr.empty? && arr.min<=res
      flg=true
      break
    end
  end
  next if flg==true
  arr << res
end
res = arr.min
#puts arr.inspect
res+=1 if FIELD[0][0]=='#'
#res+=1 if FIELD[H-1][W-1]=='#'
puts res

Score : 400 points

Problem Statement

Consider a grid with H rows and W columns of squares. Let (r, c) denote the square at the r-th row from the top and the c-th column from the left.
Each square is painted black or white.

The grid is said to be good if and only if the following condition is satisfied:

From (1, 1), we can reach (H, W) by moving one square right or down repeatedly, while always being on a white square.

Note that (1, 1) and (H, W) must be white if the grid is good.

Your task is to make the grid good by repeating the operation below. Find the minimum number of operations needed to complete the task. It can be proved that you can always complete the task in a finite number of operations.

Choose four integers r_0, c_0, r_1, c_1(1 \leq r_0 \leq r_1 \leq H, 1 \leq c_0 \leq c_1 \leq W). For each pair r, c (r_0 \leq r \leq r_1, c_0 \leq c \leq c_1), invert the color of (r, c) - that is, from white to black and vice versa.

Constraints

2 \leq H, W \leq 100

Input

Input is given from Standard Input in the following format:

H W
s_{11} s_{12} \cdots s_{1W}
s_{21} s_{22} \cdots s_{2W}
\vdots
s_{H1} s_{H2} \cdots s_{HW}

Here s_{rc} represents the color of (r, c) - # stands for black, and . stands for white.

Output

Print the minimum number of operations needed.

Sample Input 1

3 3
.##
.#.
##.

Sample Output 1

1

Do the operation with (r_0, c_0, r_1, c_1) = (2, 2, 2, 2) to change just the color of (2, 2), and we are done.

Sample Input 2

2 2
#.
.#

Sample Output 2

2

Sample Input 3

4 4
..##
#...
###.
###.

Sample Output 3

0

No operation may be needed.

Sample Input 4

5 5
.#.#.
#.#.#
.#.#.
#.#.#
.#.#.

Sample Output 4

4


#!/usr/bin/ruby

HH, WW = ARGF.gets.chomp.split.collect {|el| el.to_i}

$dp = Array.new(HH + 1) {Array.new(WW + 1)}

ss = ["-" * (WW + 1)]
HH.times do
  ss << ("-" + ARGF.gets.chomp)
end

$dp[0][1] = 0
$dp[1][0] = 0

for hh in 2..HH
  $dp[hh][0] = HH + WW + 1
end

for ww in 2..WW
  $dp[0][ww] = HH + WW + 1
end

for hh in 1..HH
  for ww in 1..WW
    if ss[hh][ww] == "."
      $dp[hh][ww] = [$dp[hh - 1][ww], $dp[hh][ww - 1]].min
    else
      if ss[hh - 1][ww] == "."
        uu = $dp[hh - 1][ww] + 1
      else
        uu = $dp[hh - 1][ww]
      end
      if ss[hh][ww - 1] == "."
        rr = $dp[hh][ww - 1] + 1
      else
        rr = $dp[hh][ww - 1]
      end
      #p uu, rr
      $dp[hh][ww] = [uu, rr].min
    end
    #p $dp
  end
end

puts $dp[HH][WW]


h, w = gets.chomp.split(" ").map(&:to_i)
if h.even?  then
	puts h * w / 2
else
	puts h * w / 2 + 1
end

Score : 200 points

Problem Statement

We have a board with H horizontal rows and W vertical columns of squares.
There is a bishop at the top-left square on this board.
How many squares can this bishop reach by zero or more movements?

Here the bishop can only move diagonally.
More formally, the bishop can move from the square at the r_1-th row (from the top) and the c_1-th column (from the left) to the square at the r_2-th row and the c_2-th column if and only if exactly one of the following holds:

r_1 + c_1 = r_2 + c_2

r_1 - c_1 = r_2 - c_2

For example, in the following figure, the bishop can move to any of the red squares in one move:

Constraints

1 \leq H, W \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H \ W

Output

Print the number of squares the bishop can reach.

Sample Input 1

4 5

Sample Output 1

10

The bishop can reach the cyan squares in the following figure:

Sample Input 2

7 3

Sample Output 2

11

The bishop can reach the cyan squares in the following figure:

Sample Input 3

1000000000 1000000000

Sample Output 3

500000000000000000


a,b = gets.split.map(&:to_i)
rem = a * b % 2
puts a * b / 2 + rem


h,w=gets.split.map &:to_i

if w % 2 == 0 then
  p (w/2).floor * h
else
  p ((w+1)/2).floor * ((h+1)/2).floor + (w/2).floor * (h/2).floor
end


Score : 400 points

Problem Statement

In this problem, we only consider strings consisting of lowercase English letters.

Strings s and t are said to be isomorphic when the following conditions are satisfied:

|s| = |t| holds.

For every pair i, j, one of the following holds:

s_i = s_j and t_i = t_j.

s_i \neq s_j and t_i \neq t_j.

For example, abcac and zyxzx are isomorphic, while abcac and ppppp are not.

A string s is said to be in normal form when the following condition is satisfied:

For every string t that is isomorphic to s, s \leq t holds. Here \leq denotes lexicographic comparison.

For example, abcac is in normal form, but zyxzx is not since it is isomorphic to abcac, which is lexicographically smaller than zyxzx.

You are given an integer N.
Print all strings of length N that are in normal form, in lexicographically ascending order.

Constraints

1 \leq N \leq 10

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Assume that there are K strings of length N that are in normal form: w_1, \ldots, w_K in lexicographical order.
Output should be in the following format:

w_1
:
w_K

Sample Input 1

1

Sample Output 1

a

Sample Input 2

2

Sample Output 2

aa
ab


# 文字列 hi が 1 個以上繋がってできる文字列を、 hitachi文字列 と定義します。
# 例えば、 hi や hihi などは hitachi文字列 であり、 ha や hii は hitachi文字列 ではありません。
# 文字列 S が与えられるので、 S が hitachi文字列 かどうかを判定してください。

s = gets.chomp
result = true

i = 0
while i < s.length
    if !(s[i] == "h" && s[i+1] == "i")
        result = false
        break
    end
    i += 2
end

if result
    puts "Yes"
else
    puts "No"
end

Score : 100 points

Problem Statement

A Hitachi string is a concatenation of one or more copies of the string hi.

For example, hi and hihi are Hitachi strings, while ha and hii are not.

Given a string S, determine whether S is a Hitachi string.

Constraints

The length of S is between 1 and 10 (inclusive).

S is a string consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S is a Hitachi string, print Yes; otherwise, print No.

Sample Input 1

hihi

Sample Output 1

Yes

hihi is the concatenation of two copies of hi, so it is a Hitachi string.

Sample Input 2

hi

Sample Output 2

Yes

Sample Input 3

ha

Sample Output 3

No


s = gets.chomp
s = s.gsub(/hi/, '')
if s.length == 0
  puts "Yes"
else
  puts "No"
end


S = gets.chomp.split(//)

puts S.uniq.length == 1 ? 'No' : 'Yes'

Score : 100 points

Problem Statement

In AtCoder City, there are three stations numbered 1, 2, and 3.

Each of these stations is operated by one of the two railway companies, A and B. A string S of length 3 represents which company operates each station. If S_i is A, Company A operates Station i; if S_i is B, Company B operates Station i.

To improve the transportation condition, for each pair of a station operated by Company A and one operated by Company B, there will be a bus service connecting them.

Determine if there is a pair of stations that will be connected by a bus service.

Constraints

Each character of S is A or B.

|S| = 3

Input

Input is given from Standard Input in the following format:

S

Output

If there is a pair of stations that will be connected by a bus service, print Yes; otherwise, print No.

Sample Input 1

ABA

Sample Output 1

Yes

Company A operates Station 1 and 3, while Company B operates Station 2.

There will be a bus service between Station 1 and 2, and between Station 2 and 3, so print Yes.

Sample Input 2

BBA

Sample Output 2

Yes

Company B operates Station 1 and 2, while Company A operates Station 3.

There will be a bus service between Station 1 and 3, and between Station 2 and 3, so print Yes.

Sample Input 3

BBB

Sample Output 3

No

Company B operates all the stations. Thus, there will be no bus service, so print No.


n,a,b = gets.split.map(&:to_i)
x = (n / (a+b) * a)
y = (n % (a+b))

if a < y
  x += a
else
  x += y
end

puts x

Score : 200 points

Problem Statement

Takahashi has many red balls and blue balls. Now, he will place them in a row.

Initially, there is no ball placed.

Takahashi, who is very patient, will do the following operation 10^{100} times:

Place A blue balls at the end of the row of balls already placed. Then, place B red balls at the end of the row.

How many blue balls will be there among the first N balls in the row of balls made this way?

Constraints

1 \leq N \leq 10^{18}

A, B \geq 0

0 < A + B \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the number of blue balls that will be there among the first N balls in the row of balls.

Sample Input 1

8 3 4

Sample Output 1

4

Let b denote a blue ball, and r denote a red ball. The first eight balls in the row will be bbbrrrrb, among which there are four blue balls.

Sample Input 2

8 0 4

Sample Output 2

0

He placed only red balls from the beginning.

Sample Input 3

6 2 4

Sample Output 3

2

Among bbrrrr, there are two blue balls.


n,a,b = gets.split.map(&:to_i)
ans = []
blue = []
red = []
a.times do
  blue << "b"
end
b.times do
  red << "r"
end
until ans.length == n
  ans << blue
  ans << red
end
puts ans.slice(0..n-1).count('b')


attrs = Array.new
while line = $stdin.gets
  attrs << line.chomp.split
end

s = attrs[0][0]
Q = attrs[1][0].to_i
is_reverse = false
Q.times do |i|
  t, f, c = attrs[2+i].map{|a| a}
  if t == '1' then
    is_reverse = !is_reverse
  else
    if f == '1' then
      if is_reverse then
        s = s + c
      else
        s = c + s
      end
    else
      if is_reverse then
        s = c + s
      else
        s = s + c
      end
    end
  end
end
puts is_reverse ? s.reverse : s

Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

Starting with this string, he will produce a new one in the procedure given as follows.

The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following:

If T_i = 1: reverse the string S.

If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.

If F_i = 1 : Add C_i to the beginning of the string S.

If F_i = 2 : Add C_i to the end of the string S.

Help Takahashi by finding the final string that results from the procedure.

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

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

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

In the 3-rd through the (Q+2)-th lines, Query_i is one of the following:

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

There will be Q = 4 operations. Initially, S is a.

Operation 1: Add p at the beginning of S. S becomes pa.

Operation 2: Reverse S. S becomes ap.

Operation 3: Add c at the end of S. S becomes apc.

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

a
6
2 2 a
2 1 b
1
2 2 c
1
1

Sample Output 2

aabc

There will be Q = 6 operations. Initially, S is a.

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy


s=gets.chomp
s0=""
s1=""
fl=false
q=gets.to_i
q.times do
  qu=gets.chomp
  if qu=="1"
    fl=!fl
  else
    if fl&&qu[2]=="1"||!fl&&qu[2]=="0"
      s1+=qu[4]
    else
      s0+=qu[4]
    end
  end
end
if !fl
  puts s0.reverse+s+s1
else
  puts s1.reverse+s.reverse+s0
end


n=gets.to_i
ans=((n+0.5)/2).to_i
puts ans

Score : 100 points

Problem Statement

Takahashi wants to print a document with N pages double-sided, where two pages of data can be printed on one sheet of paper.

At least how many sheets of paper does he need?

Constraints

N is an integer.

1 \leq N \leq 100

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

5

Sample Output 1

3

By printing the 1-st, 2-nd pages on the 1-st sheet, 3-rd and 4-th pages on the 2-nd sheet, and 5-th page on the 3-rd sheet, we can print all the data on 3 sheets of paper.

Sample Input 2

2

Sample Output 2

1

Sample Input 3

100

Sample Output 3

50


A1 = gets.chop.split.map(&:to_i)
A2 = gets.chop.split.map(&:to_i)
A3 = gets.chop.split.map(&:to_i)
N = gets.to_i
b = []
N.times { b << gets.to_i }
A1_bng= ['D','D','D']
A2_bng= ['D','D','D']
A3_bng= ['D','D','D']

for n in 0..N-1 do
	bs = b[n]
  	#puts bs
    for num in 0..2 do
      if bs == A1[num] then
        A1_bng[num] = bs
        #puts A1_bng
      elsif bs == A2[num] then
        A2_bng[num] = bs
        #puts A2_bng
      elsif bs == A3[num] then
        A3_bng[num] = bs
       	#puts A2_bng
      end
  	end
end
#puts A1_bng
#puts A2_bng
#puts A3_bng

# A1[0]
if A1_bng[0]!='D' && A1_bng[1]!='D' && A1_bng[2]!='D' then
    puts 'Yes'
elsif A1_bng[0]!='D' && A2_bng[0]!='D' && A3_bng[0]!='D' then
    puts 'Yes'
elsif A1_bng[0]!='D' && A2_bng[1]!='D' && A3_bng[2]!='D' then
    puts 'Yes'

# A1[2]
elsif A1_bng[2]!='D' && A2_bng[1]!='D' && A3_bng[0]!='D' then
    puts 'Yes'
elsif A1_bng[2]!='D' && A2_bng[2]!='D' && A3_bng[2]!='D' then
    puts 'Yes'

# A3[2]
elsif A3_bng[0]!='D' && A3_bng[1]!='D' && A3_bng[2]!='D' then
    puts 'Yes'

# 縦線中央
elsif A1_bng[1]!='D' && A2_bng[1]!='D' && A3_bng[1]!='D' then
    puts 'Yes'

# 横線中央
elsif A2_bng[0]!='D' && A2_bng[1]!='D' && A2_bng[2]!='D' then
    puts 'Yes'

else
    puts 'No'
end

Score : 200 points

Problem Statement

We have a bingo card with a 3\times3 grid. The square at the i-th row from the top and the j-th column from the left contains the number A_{i, j}.

The MC will choose N numbers, b_1, b_2, \cdots, b_N. If our bingo sheet contains some of those numbers, we will mark them on our sheet.

Determine whether we will have a bingo when the N numbers are chosen, that is, the sheet will contain three marked numbers in a row, column, or diagonal.

Constraints

All values in input are integers.

1 \leq A_{i, j} \leq 100

A_{i_1, j_1} \neq A_{i_2, j_2} ((i_1, j_1) \neq (i_2, j_2))

1 \leq N \leq 10

1 \leq b_i \leq 100

b_i \neq b_j (i \neq j)

Input

Input is given from Standard Input in the following format:

A_{1, 1} A_{1, 2} A_{1, 3}
A_{2, 1} A_{2, 2} A_{2, 3}
A_{3, 1} A_{3, 2} A_{3, 3}
N
b_1
\vdots
b_N

Output

If we will have a bingo, print Yes; otherwise, print No.

Sample Input 1

84 97 66
79 89 11
61 59 7
7
89
7
87
79
24
84
30

Sample Output 1

Yes

We will mark A_{1, 1}, A_{2, 1}, A_{2, 2}, A_{3, 3}, and complete the diagonal from the top-left to the bottom-right.

Sample Input 2

41 7 46
26 89 2
78 92 8
5
6
45
16
57
17

Sample Output 2

No

We will mark nothing.

Sample Input 3

60 88 34
92 41 43
65 73 48
10
60
43
88
11
48
73
65
41
92
34

Sample Output 3

Yes

We will mark all the squares.


sheet = []
3.times do |i|
  sheet << gets.split.map(&:to_i)
end
N = gets.to_i
#=begin
N.times do |n|
  num = gets.to_i
  3.times do |i|
    pos = sheet[i].index(num)
    unless pos.nil?
      sheet[i][pos] = "*"
    end
  end
end

# 縦
bingo = false
3.times do |pos|
  if (sheet[pos][0] == "*") && (sheet[pos][1] == "*") && (sheet[pos][2] == "*")
    puts "Yes"
    exit
  end
  if (sheet[0][pos] == "*") && (sheet[1][pos] == "*") && (sheet[2][pos] == "*")
    puts "Yes"
    exit
  end
end

if (sheet[0][0] == "*") && (sheet[1][1] == "*") && (sheet[2][2] == "*")
  puts "Yes"
  exit
end
if (sheet[0][2] == "*") && (sheet[1][1] == "*") && (sheet[2][0] == "*")
  puts "Yes"
  exit
end

puts "No"
#=end


a=[]
3.times do |i|
  a[i]=gets.split.map(&:to_i)
end
n=gets.to_i
n.times do
  b=gets.to_i
  3.times do |i|
    3.times do |j|
      if a[i][j]==b
        a[i][j]=0
      end
    end
  end
end
3.times do |i|
  if a[i][0]==0&&a[i][1]==0&&a[i][2]==0
    puts "Yes"
    exit
  end
end
3.times do |i|
  if a[0][i]==0&&a[1][i]==0&&a[2][i]==0
    puts "Yes"
    exit
  end
end
if a[0][0]==0&&a[1][1]==0&&a[2][2]==0
  puts "Yes"
  exit
end
if a[0][2]==0&&a[1][1]==0&&a[2][0]==0
  puts "Yes"
  exit
end
puts "No"


N = gets.to_i
S = gets.chomp
Q = gets.to_i
Query = []

Q.times {
    Query << gets.chomp.split(" ")
}

s = Hash.new{|h, k| h[k] = []}
(0...N).each {|i|
    s[S[i]] << (i + 1)
}

Query.each {|f, a, b|
    if f == "1"
        a_i = a.to_i
        if S[a_i - 1] != b the
            idx = s[S[a_i - 1]].bsearch_index{|x| x >= a_i}
            s[S[a_i - 1]].delete_at(idx)
            S[a_i - 1] = b
            idx = s[b].bsearch_index{|x| x >= a_i}
            idx.nil? ? s[b] << a_i : s[b].insert(idx, a_i)
        end
    else
        l = a.to_i
        r = b.to_i
        ans = 0
        (0..25).each {|i|
            sl = s[(97 + i).chr].bsearch{|x| x >= l}
            ans += 1 if(not sl.nil?) && sl <= r
        }
        puts ans
    end
}

Score : 500 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters.

Process Q queries of the following two types:

Type 1: change the i_q-th character of S to c_q. (Do nothing if the i_q-th character is already c_q.)

Type 2: answer the number of different characters occurring in the substring of S between the l_q-th and r_q-th characters (inclusive).

Constraints

N, Q, i_q, l_q, and r_q are integers.

S is a string consisting of lowercase English letters.

c_q is a lowercase English letter.

1 \leq N \leq 500000

1 \leq Q \leq 20000

|S| = N

1 \leq i_q \leq N

1 \leq l_q \leq r_q \leq N

There is at least one query of type 2 in each testcase.

Input

Input is given from Standard Input in the following format:

N
S
Q
Query_1
\vdots
Query_Q

Here, Query_i in the 4-th through (Q+3)-th lines is one of the following:

1 i_q c_q

2 l_q r_q

Output

For each query of type 2, print a line containing the answer.

Sample Input 1

7
abcdbbd
6
2 3 6
1 5 z
2 1 1
1 4 a
1 7 d
2 1 7

Sample Output 1

3
1
5

In the first query, cdbb contains three kinds of letters: b , c , and d, so we print 3.

In the second query, S is modified to abcdzbd.

In the third query, a contains one kind of letter: a, so we print 1.

In the fourth query, S is modified to abcazbd.

In the fifth query, S does not change and is still abcazbd.

In the sixth query, abcazbd contains five kinds of letters: a, b, c, d, and z, so we print 5.


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

class SegTree
  attr_accessor:node,:n
  def initialize(n)
    @n=1
    @n*=2 while @n<n
    @node=array(2*@n-1,1,0)
  end

  def update(i,x)
    i+=@n-1
    @node[i]=x
    while i>0
      i=(i-1)/2
      @node[i]=@node[i*2+1]+@node[i*2+2]
    end
  end

  def getSum(s,t,i,l,r)
    return 0 if r<=s or t<=l
    return @node[i] if s<=l and r<=t
    mid=(l+r)/2
    leaf_l=getSum(s,t,i*2+1,l,mid)
    leaf_r=getSum(s,t,i*2+2,mid,r)
    return leaf_l+leaf_r
  end
end
$exchange={}
("a".."z").to_a.each_with_index do|c,i|
  $exchange[c]=i
end
n=gets.to_i
s=get_nsp
q=gets.to_i
st=array(26,1)
26.times do|i|
  st[i]=SegTree.new(n)
end
n.times do|i|
  st[$exchange[s[i]]].update(i,1)
end
q.times do
  que=get
  if que[0].to_i==1
    next if s[que[1].to_i-1]==que[2]
    st[$exchange[s[que[1].to_i-1]]].update(que[1].to_i-1,0)
    st[$exchange[que[2]]].update(que[1].to_i-1,1)
  else
    ans=0
    26.times do|i|
      val=st[i].getSum(que[1].to_i-1,que[2].to_i,0,0,st[i].n)
      ans+=1 if val.nonzero?
    end
    puts ans
  end
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

class BIT
  attr_accessor:node,:n
  def initialize(n)
    @n=n
    @node=array(@n+1,1,0)
  end

  def update(i,x)
    while i<=@n
      @node[i]+=x
      i+=i&-i
    end
  end

  def sum(i)
    s=0
    while i>0
      s+=@node[i]
      i-=i&-i
    end
    return s
  end
  def getSum(s,t)
    return sum(t)-sum(s)
  end
end
$exchange={}
("a".."z").to_a.each_with_index do|c,i|
  $exchange[c]=i
end
n=gets.to_i
s=get_nsp
q=gets.to_i
bit=array(26,1)
26.times do|i|
  bit[i]=BIT.new(n)
end
n.times do|i|
  bit[$exchange[s[i]]].update(i+1,1)
end
q.times do
  que=get
  if que[0].to_i==1
    next if s[que[1].to_i-1]==que[2]
    bit[$exchange[s[que[1].to_i-1]]].update(que[1].to_i,-1)
    bit[$exchange[que[2]]].update(que[1].to_i,1)
  else
    ans=0
    26.times do|i|
      val=bit[i].getSum(que[1].to_i-1,que[2].to_i)
      ans+=1 if val.nonzero?
    end
    puts ans
  end
end


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

N = line[0]
R = line[1]

if N < 10 then
  print R + 100 * (10 - K)
else
  print R
end

Score : 100 points

Problem Statement

Takahashi is a member of a programming competition site, ButCoder.

Each member of ButCoder is assigned two values: Inner Rating and Displayed Rating.

The Displayed Rating of a member is equal to their Inner Rating if the member has participated in 10 or more contests. Otherwise, the Displayed Rating will be their Inner Rating minus 100 \times (10 - K) when the member has participated in K contests.

Takahashi has participated in N contests, and his Displayed Rating is R. Find his Inner Rating.

Constraints

All values in input are integers.

1 \leq N \leq 100

0 \leq R \leq 4111

Input

Input is given from Standard Input in the following format:

N R

Output

Print his Inner Rating.

Sample Input 1

2 2919

Sample Output 1

3719

Takahashi has participated in 2 contests, which is less than 10, so his Displayed Rating is his Inner Rating minus 100 \times (10 - 2) = 800.

Thus, Takahashi's Inner Rating is 2919 + 800 = 3719.

Sample Input 2

22 3051

Sample Output 2

3051


n, r = gets.chomp.split.map(&:to_i)
if n >= 10
  puts r
else
  puts r + (100 * (10 - n ))
end


N, K = gets.chomp.split(" ").map(&:to_i)
puts N.to_s(K).size

Score : 200 points

Problem Statement

Given is an integer N. Find the number of digits that N has in base K.

Notes

For information on base-K representation, see Positional notation - Wikipedia.

Constraints

All values in input are integers.

1 \leq N \leq 10^9

2 \leq K \leq 10

Input

Input is given from Standard Input in the following format:

N K

Output

Print the number of digits that N has in base K.

Sample Input 1

11 2

Sample Output 1

4

In binary, 11 is represented as 1011.

Sample Input 2

1010101 10

Sample Output 2

7

Sample Input 3

314159265 3

Sample Output 3

18


N = gets.to_i
Xs = gets.split.map(&:to_i)

a = Xs.inject(0) {|result, item|
    result + item**2
  }
b = Xs.inject(&:+)

c = (b.to_f / N)

p [c.floor, c.ceil].map{|n| N*(n**2) - 2*b*n + a}.min

Score : 300 points

Problem Statement

There are N people living on a number line.

The i-th person lives at coordinate X_i.

You are going to hold a meeting that all N people have to attend.

The meeting can be held at any integer coordinate. If you choose to hold the meeting at coordinate P, the i-th person will spend (X_i - P)^2 points of stamina to attend the meeting.

Find the minimum total points of stamina the N people have to spend.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq X_i \leq 100

Input

Input is given from Standard Input in the following format:

N
X_1 X_2 ... X_N

Output

Print the minimum total stamina the N people have to spend.

Sample Input 1

2
1 4

Sample Output 1

5

Assume the meeting is held at coordinate 2. In this case, the first person will spend (1 - 2)^2 points of stamina, and the second person will spend (4 - 2)^2 = 4 points of stamina, for a total of 5 points of stamina. This is the minimum total stamina that the 2 people have to spend.

Note that you can hold the meeting only at an integer coordinate.

Sample Input 2

7
14 14 2 13 56 2 37

Sample Output 2

2354


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

ans = 1_000_000_000
1.upto(100) do |i|
  sum = 0
  x.map do |x|
    sum += (x - i) ** 2
  end
  ans = sum if sum < ans
end

puts ans


MOD = 1000000007

def conv(a,b)
  if b > a/2
    b = a - b
  end
  ans = 1
  div = 1
  b.times do |i|
    ans *= a - i
    ans %= MOD
    div *= (i+1)
    div %= MOD
  end
  ans = ans * pow(div, MOD-2)
  ans % MOD
end

def pow(x,n)
  if n == 0
    1
  elsif n % 2 == 0
    pow(x*x%MOD, n/2) % MOD
  else
    x * pow(x, n-1) % MOD
  end
end

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

all = (pow(2,n) - 1) % MOD
if a > n/2
  a = n - a
end
if b > n/2
  b = n - b
end

all = all - conv(n,a) - conv(n,b)
all %= MOD

puts all

Score : 400 points

Problem Statement

Akari has n kinds of flowers, one of each kind.

She is going to choose one or more of these flowers to make a bouquet.

However, she hates two numbers a and b, so the number of flowers in the bouquet cannot be a or b.

How many different bouquets are there that Akari can make?

Find the count modulo (10^9 + 7).

Here, two bouquets are considered different when there is a flower that is used in one of the bouquets but not in the other bouquet.

Constraints

All values in input are integers.

2 \leq n \leq 10^9

1 \leq a < b \leq \textrm{min}(n, 2 \times 10^5)

Input

Input is given from Standard Input in the following format:

n a b

Output

Print the number of bouquets that Akari can make, modulo (10^9 + 7). (If there are no such bouquets, print 0.)

Sample Input 1

4 1 3

Sample Output 1

7

In this case, Akari can choose 2 or 4 flowers to make the bouquet.

There are 6 ways to choose 2 out of the 4 flowers, and 1 way to choose 4, so there are a total of 7 different bouquets that Akari can make.

Sample Input 2

1000000000 141421 173205

Sample Output 2

34076506

Print the count modulo (10^9 + 7).


list = gets.chomp.split(" ")

list = list.uniq

if list.length == 2
  puts "Yes"
else
  puts "No"
end

Score: 100 points

Problem Statement

A triple of numbers is said to be poor when two of those numbers are equal but the other number is different from those two numbers.

You will be given three integers A, B, and C. If this triple is poor, print Yes; otherwise, print No.

Constraints

A, B, and C are all integers between 1 and 9 (inclusive).

Input

Input is given from Standard Input in the following format:

A B C

Output

If the given triple is poor, print Yes; otherwise, print No.

Sample Input 1

5 7 5

Sample Output 1

Yes

A and C are equal, but B is different from those two numbers, so this triple is poor.

Sample Input 2

4 4 4

Sample Output 2

No

A, B, and C are all equal, so this triple is not poor.

Sample Input 3

4 9 6

Sample Output 3

No

Sample Input 4

3 3 4

Sample Output 4

Yes


a, b, c = gets.split.map(&:to_i)
if (a==b && b==c) || (a!=b&&b!=c&&a!=c)
  puts 'No'
else
  puts 'Yes'
end


S = {}
(gets.to_i).times do
  key = gets.chomp
    if S.key?(key)
        S[key] += 1
    else
        S.store(key, 1)
    end
end

max = S.values.max
puts S.keep_if {|_, v| v == max}
puts S.keys.sort


N = gets.chomp.split(' ').map(&:to_i)

if N.uniq.size == 2
  puts 'Yes'
else
  puts 'No'
end


N = gets.to_i
A = gets.split.map(&:to_i).select{|a|a.even?}
ans = A.inject(true) do |q, a|
  q &= (a % 3).zero? || (a % 5).zero?
end
if ans
  puts 'APPROVED'
else
  puts 'DENIED'
end

Score: 200 points

Problem Statement

You are an immigration officer in the Kingdom of AtCoder. The document carried by an immigrant has some number of integers written on it, and you need to check whether they meet certain criteria.

According to the regulation, the immigrant should be allowed entry to the kingdom if and only if the following condition is satisfied:

All even numbers written on the document are divisible by 3 or 5.

If the immigrant should be allowed entry according to the regulation, output APPROVED; otherwise, print DENIED.

Notes

The condition in the statement can be rephrased as "If x is an even number written on the document, x is divisible by 3 or 5".
Here "if" and "or" are logical terms.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq A_i \leq 1000

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

If the immigrant should be allowed entry according to the regulation, print APPROVED; otherwise, print DENIED.

Sample Input 1

5
6 7 9 10 31

Sample Output 1

APPROVED

The even numbers written on the document are 6 and 10.

All of them are divisible by 3 or 5, so the immigrant should be allowed entry.

Sample Input 2

3
28 27 24

Sample Output 2

DENIED

28 violates the condition, so the immigrant should not be allowed entry.


n = gets.to_i
ary = []
while a = gets
    ary.push(a)
end

flag = "DENIED"

ary.each do |a|
    a = a.to_i
    if a % 2 == 0
        if a % 3 == 0 or a % 5 == 0
            flag = "APPROVED"
        else
            flag = "DENIED"
        end
    end
end
puts flag


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

result = 'APPROVED'

a.each do |doc|
  result = 'DENIED' if (doc.even? && (doc%3 != 0 && doc%5 != 0))
end

puts result


N=gets.to_i
ss=$<.read.split
h={}
ss.each do |s|
  h[s] ||= 0
  h[s] += 1
end

arr = h.sort
max = arr[-1][1]
ret = arr.select {|k,v|max == v}.map(&:first).sort

puts ret.join("\n")

Score: 300 points

Problem Statement

We have N voting papers. The i-th vote (1 \leq i \leq N) has the string S_i written on it.

Print all strings that are written on the most number of votes, in lexicographical order.

Constraints

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

S_i (1 \leq i \leq N) are strings consisting of lowercase English letters.

The length of S_i (1 \leq i \leq N) is between 1 and 10 (inclusive).

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

Print all strings in question in lexicographical order.

Sample Input 1

7
beat
vet
beet
bed
vet
bet
beet

Sample Output 1

beet
vet

beet and vet are written on two sheets each, while beat, bed, and bet are written on one vote each. Thus, we should print the strings beet and vet.

Sample Input 2

8
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo

Sample Output 2

buffalo

Sample Input 3

7
bass
bass
kick
kick
bass
kick
kick

Sample Output 3

kick

Sample Input 4

4
ushi
tapu
nichia
kun

Sample Output 4

kun
nichia
tapu
ushi


count = gets.to_i
strs = Hash.new(0)
count.times do
    strs[gets.chomp] += 1
end
strs = strs.sort_by{ | k, v | v }.reverse
max = strs[0][1]
strs.each do |str|
  if  max == str[1]
    print(str[0] + "\r\n")
  end
end


require 'pp'

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

N, K = gets_i_list
p_list = gets_i_list

e_list = p_list.map do |p|
  (p + 1) * 0.5
end

sum_list = [0]
(0...N).each do |i|
  sum_list[i+1] = sum_list[i] + e_list[i]
end

max = 0
0.upto(N - K) do |i|
  total = sum_list[i+K] - sum_list[i]
  max  = [max, total].max
end
puts sprintf("%.12f", max)

Score : 400 points

Problem Statement

We have N dice arranged in a line from left to right. The i-th die from the left shows p_i numbers from 1 to p_i with equal probability when thrown.

We will choose K adjacent dice, throw each of them independently, and compute the sum of the numbers shown. Find the maximum possible value of the expected value of this sum.

Constraints

1 ≤ K ≤ N ≤ 200000

1 ≤ p_i ≤ 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
p_1 ... p_N

Output

Print the maximum possible value of the expected value of the sum of the numbers shown.

Your output will be considered correct when its absolute or relative error from our answer is at most 10^{-6}.

Sample Input 1

5 3
1 2 2 4 5

Sample Output 1

7.000000000000

When we throw the third, fourth, and fifth dice from the left, the expected value of the sum of the numbers shown is 7. This is the maximum value we can achieve.

Sample Input 2

4 1
6 6 6 6

Sample Output 2

3.500000000000

Regardless of which die we choose, the expected value of the number shown is 3.5.

Sample Input 3

10 4
17 13 13 12 15 20 10 13 17 11

Sample Output 3

32.000000000000


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

max_num = -1
max_arr = []
for i in 0..n-k do
  i_arr = arr[i...i+k]
  i_num = i_arr.reduce(:+)
  if i_num > max_num
    max_num = i_num
    max_arr = i_arr
  end
end
max_arr = max_arr.reduce(0){|res, i| res + (i.to_f + 1)/2}
printf("%.12f", max_arr)


H,A = gets.split.map(&:to_f)
puts (H / A).ceil

Score : 100 points

Problem Statement

Serval is fighting with a monster.

The health of the monster is H.

In one attack, Serval can decrease the monster's health by A.
There is no other way to decrease the monster's health.

Serval wins when the monster's health becomes 0 or below.

Find the number of attacks Serval needs to make before winning.

Constraints

1 \leq H \leq 10^4

1 \leq A \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H A

Output

Print the number of attacks Serval needs to make before winning.

Sample Input 1

10 4

Sample Output 1

3

After one attack, the monster's health will be 6.

After two attacks, the monster's health will be 2.

After three attacks, the monster's health will be -2.

Thus, Serval needs to make three attacks to win.

Sample Input 2

1 10000

Sample Output 2

1

Sample Input 3

10000 1

Sample Output 3

10000


h, n = gets.split(" ").map(&:to_i)
ls = []

ls = gets.split(" ").map(&:to_i)

if ls.inject(:+) >= h
  print("Yes\n")
else
  print("No\n")
end

Score : 200 points

Problem Statement

Raccoon is fighting with a monster.

The health of the monster is H.

Raccoon can use N kinds of special moves. Using the i-th move decreases the monster's health by A_i.
There is no other way to decrease the monster's health.

Raccoon wins when the monster's health becomes 0 or below.

If Raccoon can win without using the same move twice or more, print Yes; otherwise, print No.

Constraints

1 \leq H \leq 10^9

1 \leq N \leq 10^5

1 \leq A_i \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H N
A_1 A_2 ... A_N

Output

If Raccoon can win without using the same move twice or more, print Yes; otherwise, print No.

Sample Input 1

10 3
4 5 6

Sample Output 1

Yes

The monster's health will become 0 or below after, for example, using the second and third moves.

Sample Input 2

20 3
4 5 6

Sample Output 2

No

Sample Input 3

210 5
31 41 59 26 53

Sample Output 3

Yes

Sample Input 4

211 5
31 41 59 26 53

Sample Output 4

No


_,k,*h=`dd`.split.map(&:to_i)
k.times{h.pop}
p h.sum

Score : 300 points

Problem Statement

Fennec is fighting with N monsters.

The health of the i-th monster is H_i.

Fennec can do the following two actions:

Attack: Fennec chooses one monster. That monster's health will decrease by 1.

Special Move: Fennec chooses one monster. That monster's health will become 0.

There is no way other than Attack and Special Move to decrease the monsters' health.

Fennec wins when all the monsters' healths become 0 or below.

Find the minimum number of times Fennec needs to do Attack (not counting Special Move) before winning when she can use Special Move at most K times.

Constraints

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

0 \leq K \leq 2 \times 10^5

1 \leq H_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
H_1 ... H_N

Output

Print the minimum number of times Fennec needs to do Attack (not counting Special Move) before winning.

Sample Input 1

3 1
4 1 5

Sample Output 1

5

By using Special Move on the third monster, and doing Attack four times on the first monster and once on the second monster, Fennec can win with five Attacks.

Sample Input 2

8 9
7 9 3 2 3 8 4 6

Sample Output 2

0

She can use Special Move on all the monsters.

Sample Input 3

3 0
1000000000 1000000000 1000000000

Sample Output 3

3000000000

Watch out for overflow.


h,n=gets.split.map(&:to_i)
INF=10**9+1
dp=[INF]*(h+1)
dp[0]=0
n.times do
    a,b=gets.split.map(&:to_i)
    (h+1).times do |i|
        j= i+a>h ? h:i+a
        dp[j]=dp[i]+b if dp[i]!=INF and dp[j]>dp[i]+b
    end
end
p dp[h]

Score : 500 points

Problem Statement

Ibis is fighting with a monster.

The health of the monster is H.

Ibis can cast N kinds of spells. Casting the i-th spell decreases the monster's health by A_i, at the cost of B_i Magic Points.

The same spell can be cast multiple times. There is no way other than spells to decrease the monster's health.

Ibis wins when the health of the monster becomes 0 or below.

Find the minimum total Magic Points that have to be consumed before winning.

Constraints

1 \leq H \leq 10^4

1 \leq N \leq 10^3

1 \leq A_i \leq 10^4

1 \leq B_i \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H N
A_1 B_1
:
A_N B_N

Output

Print the minimum total Magic Points that have to be consumed before winning.

Sample Input 1

9 3
8 3
4 2
2 1

Sample Output 1

4

First, let us cast the first spell to decrease the monster's health by 8, at the cost of 3 Magic Points. The monster's health is now 1.

Then, cast the third spell to decrease the monster's health by 2, at the cost of 1 Magic Point. The monster's health is now -1.

In this way, we can win at the total cost of 4 Magic Points.

Sample Input 2

100 6
1 1
2 3
3 9
4 27
5 81
6 243

Sample Output 2

100

It is optimal to cast the first spell 100 times.

Sample Input 3

9999 10
540 7550
691 9680
700 9790
510 7150
415 5818
551 7712
587 8227
619 8671
588 8228
176 2461

Sample Output 3

139815


a, b = gets.split.map(&:to_i)
puts a==b ? "Yes" : "No"

Score : 100 points

Problem Statement

Takahashi is participating in a programming contest, AXC001. He has just submitted his code to Problem A.

The problem has N test cases, all of which must be passed to get an AC verdict.

Takahashi's submission has passed M cases out of the N test cases.

Determine whether Takahashi's submission gets an AC.

Constraints

1 \leq N \leq 100

0 \leq M \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

If Takahashi's submission gets an AC, print Yes; otherwise, print No.

Sample Input 1

3 3

Sample Output 1

Yes

All three test cases have been passed, so his submission gets an AC.

Sample Input 2

3 2

Sample Output 2

No

Only two out of the three test cases have been passed, so his submission does not get an AC.

Sample Input 3

1 1

Sample Output 3

Yes


MOD=10**9+7
cnt=0; sum=0; prev=nil; can=true; h=Hash.new(0)
def gs() gets.chomp end
def gi() gets.chomp.to_i end
def gsmi() gets.chomp.split.map(&:to_i) end

def desc(ar) ar.sort!{|x,y| y<=>x} end
def min(a,b) a<=b ? a : b end
def max(a,b) a>=b ? a : b end
def sum(ar) ar.inject(:+) end

def C(a,b) b==0||a==b ? 1 : (b=a-b if a/2<b;(a-b+1..a).inject(:*)/(1..b).inject(:*)) end
def rui(ar) s=[0]; ar.size.times{|i| s<<s[-1]+ar[i]}; s end
def rev_rui(ar) s=[0]; ar.size.times.reverse_each{|i| s<<s[-1]+ar[i]}; s end

def puts_yesno(b) puts(b ? 'Yes' : 'No') end
def putsend(s) puts s; exit end
def debug(k,v) puts "#{k}:#{v}" end
########### ( ˘ω˘ )ｽﾔｧ… ###########
a,b=gets.chomp.split
aa=a*b.to_i
bb=b*a.to_i
puts min(aa,bb)

Score : 200 points

Problem Statement

Given are 1-digit positive integers a and b. Consider these two strings: the concatenation of b copies of the digit a, and the concatenation of a copies of the digit b. Which of these is lexicographically smaller?

Constraints

1 \leq a \leq 9

1 \leq b \leq 9

a and b are integers.

Input

Input is given from Standard Input in the following format:

a b

Output

Print the lexicographically smaller of the two strings. (If the two strings are equal, print one of them.)

Sample Input 1

4 3

Sample Output 1

3333

We have two strings 444 and 3333. Between them, 3333 is the lexicographically smaller.

Sample Input 2

7 7

Sample Output 2

7777777


n = gets.to_i
P_arr = gets.chomp.split.map(&:to_i)
counter = 0
P_max = P_arr.max

n.times do |i|
  if P_max >= P_arr[i]
     counter += 1
     P_max = P_arr[i]
  end
end

p counter

Score : 300 points

Problem Statement

Given is a permutation P_1, \ldots, P_N of 1, \ldots, N.
Find the number of integers i (1 \leq i \leq N) that satisfy the following condition:

For any integer j (1 \leq j \leq i), P_i \leq P_j.

Constraints

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

P_1, \ldots, P_N is a permutation of 1, \ldots, N.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_1 ... P_N

Output

Print the number of integers i that satisfy the condition.

Sample Input 1

5
4 2 5 1 3

Sample Output 1

3

i=1, 2, and 4 satisfy the condition, but i=3 does not - for example, P_i > P_j holds for j = 1.

Similarly, i=5 does not satisfy the condition, either. Thus, there are three integers that satisfy the condition.

Sample Input 2

4
4 3 2 1

Sample Output 2

4

All integers i (1 \leq i \leq N) satisfy the condition.

Sample Input 3

6
1 2 3 4 5 6

Sample Output 3

1

Only i=1 satisfies the condition.

Sample Input 4

8
5 7 4 2 6 8 1 3

Sample Output 4

4

Sample Input 5

1
1

Sample Output 5

1


N = gets.to_i
nums = gets.split.map(&:to_i)
min = Float::INFINITY
ans = 0
nums.each do |ai|
  if ai < min
    min = ai
    ans += 1
  end
end
p ans


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

count = 0
minimal = array.first
(1...n).each do |y|
  if minimal < array[y]
    count += 1
  else
    minimal = array[y]
  end
end

puts n - count


io = STDIN
n=io.gets.to_i
P=io.gets.split.map(&:to_i)
cnt=0
n.times do |i|
  cnt +=1 if (0..i).all?{|j|P[i]<=P[j]}
end
puts cnt


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

min = 10**6 + 1
count = 0

ps.each do |p|
  if min > p
    min = p
    count += 1
  end
end

puts count


# frozen_string_literal: true

n, k, s = gets.split.map(&:to_i)
result = []

if n == k
  n.times do
    result << s
  end

  puts result.join(' ')
  exit

end

if s.even?
  if k > 0
    (k + 1).times do
      result << s / 2
    end

    (n - (k + 1)).times do
      result << s / 2 + 2
    end
  else
    n.times do
      result << s / 2 + 2
    end
  end
end

if s.odd?
  if k > 0
    flag = false
    (k + 1).times do
      tmp = flag ? s / 2 : s / 2 + 1
      result << tmp
      flag = !flag
    end

    (n - (k + 1)).times do
      result << s / 2 + 2
    end
  else
    n.times do
      result << s / 2 + 2
    end
  end
end

puts result.join(' ')

Score : 400 points

Problem Statement

Given are three integers N, K, and S.

Find a sequence A_1, A_2, ..., A_N of N integers between 1 and 10^9 (inclusive) that satisfies the condition below.
We can prove that, under the conditions in Constraints, such a sequence always exists.

There are exactly K pairs (l, r) of integers such that 1 \leq l \leq r \leq N and A_l + A_{l + 1} + \cdots + A_r = S.

Constraints

1 \leq N \leq 10^5

0 \leq K \leq N

1 \leq S \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K S

Output

Print a sequence satisfying the condition, in the following format:

A_1 A_2 ... A_N

Sample Input 1

4 2 3

Sample Output 1

1 2 3 4

Two pairs (l, r) = (1, 2) and (3, 3) satisfy the condition in the statement.

Sample Input 2

5 3 100

Sample Output 2

50 50 50 30 70


n, k, s = gets.split.map(&:to_i)
a = Array.new(n,1000000000)
(k).times do |i|
  a[i] = s
end
puts a.join(" ")


C = gets.chomp
alf = 'abcdefghijklmnopqrstuvwxyz'.to_s
alf.size.times do |i|
  puts alf[i+1] if alf[i] == C
end

Score : 100 points

Problem Statement

Given is a lowercase English letter C that is not z. Print the letter that follows C in alphabetical order.

Constraints

C is a lowercase English letter that is not z.

Input

Input is given from Standard Input in the following format:

C

Output

Print the letter that follows C in alphabetical order.

Sample Input 1

a

Sample Output 1

b

a is followed by b.

Sample Input 2

y

Sample Output 2

z

y is followed by z.


c=gets.chomp.to_s
a=("a".."z").to_a
puts a[a.index(c)+1]


n,m = gets.split(' ').map(&:to_i)
a = []
t1, t2 = 0, 0
m.times { a << gets.split(' ') }
a = a.group_by { |a| a[0] }
a.each do |b|
  tmp = b[1].transpose[1]
  if tmp.include?('AC')
    t1 += 1
    tmp.slice!(tmp.index('AC')..-1)
    t2 += tmp.count('WA')
  end
end
printf("%i %i\n", t1, t2)

Score : 300 points

Problem Statement

Takahashi participated in a contest on AtCoder.

The contest had N problems.

Takahashi made M submissions during the contest.

The i-th submission was made for the p_i-th problem and received the verdict S_i (AC or WA).

The number of Takahashi's correct answers is the number of problems on which he received an AC once or more.

The number of Takahashi's penalties is the sum of the following count for the problems on which he received an AC once or more: the number of WAs received before receiving an AC for the first time on that problem.

Find the numbers of Takahashi's correct answers and penalties.

Constraints

N, M, and p_i are integers.

1 \leq N \leq 10^5

0 \leq M \leq 10^5

1 \leq p_i \leq N

S_i is AC or WA.

Input

Input is given from Standard Input in the following format:

N M
p_1 S_1
:
p_M S_M

Output

Print the number of Takahashi's correct answers and the number of Takahashi's penalties.

Sample Input 1

2 5
1 WA
1 AC
2 WA
2 AC
2 WA

Sample Output 1

2 2

In his second submission, he received an AC on the first problem for the first time. Before this, he received one WA on this problem.

In his fourth submission, he received an AC on the second problem for the first time. Before this, he received one WA on this problem.

Thus, he has two correct answers and two penalties.

Sample Input 2

100000 3
7777 AC
7777 AC
7777 AC

Sample Output 2

1 0

Note that it is pointless to get an AC more than once on the same problem.

Sample Input 3

6 0

Sample Output 3

0 0


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

ca = 0
penalty = 0
num = 0

results.each do |result|
  next if result[0].to_i <= num
  if result[1] == 'AC'
    ca += 1
    num = result[0].to_i
  else
    next if num >= n
    penalty += 1
  end
end

puts "#{ca} #{penalty}"


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

flag = {}
ac_c = 0
wa_c = 0
m.times do
  p, s = gets.split(' ')
  next if flag[p] == 'AC'

  if s == 'AC'
    flag[p] = 'AC'
    ac_c += 1
  else
    wa_c += 1
  end
end

puts "#{ac_c} #{wa_c}"


lines = readlines.map{|line| line.split(' ')}
d=0
c=[]
for j in 1..lines[0][1].to_i do
  if lines[j][1]=="WA" && !c.include?(lines[j][0].to_i) then
    d=d+1
  elsif lines[j][1]=="AC" && !c.include?(lines[j][0].to_i) then
    c.push(lines[j][0].to_i)
  end
end
puts c.length.to_s+" "+d.to_s


$h, $w = gets.split.map(&:to_i)
$s = []
$h.times do
  $s << gets.chomp
end
$d = [[0, 1], [0, -1], [1, 0], [-1, 0]]

def bfs(sx, sy)
  bfs_map = Array.new($h) { Array.new($w, Float::INFINITY) }
  bfs_map[sy][sx] = 0
  q = []
  q << [sy, sx, 0]
  res = 0

  until q.empty?
    y, x, dist = q.shift
    $d.each do |dy, dx|
      ny = y + dy
      nx = x + dx
      next unless nx >= 0 && nx < $w && ny >= 0 && ny < $h && $s[ny][nx] != "#"
      if bfs_map[ny][nx] > dist + 1
        bfs_map[ny][nx] = dist + 1
        res = dist + 1 if dist + 1 != Float::INFINITY && dist + 1 > res
        q << [ny, nx, dist + 1]
      end
    end
  end
  # res = 0
  # bfs_map.each do |row|
  #   row.each do |c|
  #     if c != Float::INFINITY && c > res
  #       res = c
  #     end
  #   end
  # end
  return res
end

ans = -1
$h.times do |sy|
  $w.times do |sx|
    t = bfs(sx, sy)
    ans = t if t > ans
  end
end
p ans

Score : 400 points

Problem Statement

Takahashi has a maze, which is a grid of H \times W squares with H horizontal rows and W vertical columns.

The square at the i-th row from the top and the j-th column is a "wall" square if S_{ij} is #, and a "road" square if S_{ij} is ..

From a road square, you can move to a horizontally or vertically adjacent road square.

You cannot move out of the maze, move to a wall square, or move diagonally.

Takahashi will choose a starting square and a goal square, which can be any road squares, and give the maze to Aoki.

Aoki will then travel from the starting square to the goal square, in the minimum number of moves required.

In this situation, find the maximum possible number of moves Aoki has to make.

Constraints

1 \leq H,W \leq 20

S_{ij} is . or #.

S contains at least two occurrences of ..

Any road square can be reached from any road square in zero or more moves.

Input

Input is given from Standard Input in the following format:

H W
S_{11}...S_{1W}
:
S_{H1}...S_{HW}

Output

Print the maximum possible number of moves Aoki has to make.

Sample Input 1

3 3
...
...
...

Sample Output 1

4

If Takahashi chooses the top-left square as the starting square and the bottom-right square as the goal square, Aoki has to make four moves.

Sample Input 2

3 5
...#.
.#.#.
.#...

Sample Output 2

10

If Takahashi chooses the bottom-left square as the starting square and the top-right square as the goal square, Aoki has to make ten moves.


h,w = gets.chomp.split().map(&:to_i)
s= []
h.times do
  s.push gets.chomp.split(//)
end

def solve(grid,yy,xx, h,w)
  dy = [0, 1, 0, -1]; dx = [1, 0, -1, 0]

  visited = Array.new(h){Array.new(w, 0)}
  visited[yy][xx] = true
  q = [[yy,xx,0]]

  ans = 0
  until q.empty?
    y,x,cost = q.shift
    4.times do |i|
      ny = y + dy[i]; nx = x + dx[i]
      if (0 <= ny && ny < h) && (0 <= nx && nx < w) && grid[ny][nx] != '#' && visited[ny][nx] == 0
        visited[ny][nx] = 1
        q.push [ny, nx, cost+1]
        ans = cost+1 if ans < cost+1
      end
    end
  end
  ans
end

ans = 0
h.times do |i|
  w.times do |j|
    if s[i][j] != '#'
      solved = solve(s,i,j, h,w)
      ans = solved if ans < solved
    end
  end
end

puts ans