A - Cut

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

N 枚からなるカードの山があり、上から i 枚目のカードには整数 A_i が書かれています。

山の下から K 枚のカードを取り出し、順序を保ったまま山の一番上に乗せました。

この操作後の山の上から順に、カードに書かれた整数を出力してください。

制約

  • 1 \leq K < N \leq 100
  • 1 \leq A_i \leq 100
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

N K
A_1 A_2 \ldots A_N

出力

操作後の山の上から i 枚目のカードに書かれた整数を B_i とする。B_1,B_2,\ldots,B_N をこの順に空白区切りで出力せよ。


入力例 1

5 3
1 2 3 4 5

出力例 1

3 4 5 1 2

最初、カードに書かれた整数は山の上から順に 1,2,3,4,5 です。

山の下から 3 枚のカードを取り出し、そのまま山の一番上に乗せたあと、カードに書かれた整数は山の上から順に 3,4,5,1,2 となります。


入力例 2

6 2
1 2 1 2 1 2

出力例 2

1 2 1 2 1 2

カードに書かれている整数は相異なるとは限りません。

Score : 100 points

Problem Statement

There is a stack of N cards, and the i-th card from the top has an integer A_i written on it.

You take K cards from the bottom of the stack and place them on top of the stack, maintaining their order.

Print the integers written on the cards from top to bottom after the operation.

Constraints

  • 1 \leq K < N \leq 100
  • 1 \leq A_i \leq 100
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N K
A_1 A_2 \ldots A_N

Output

Let B_i be the integer written on the i-th card from the top of the stack after the operation. Print B_1,B_2,\ldots,B_N in this order, separated by spaces.


Sample Input 1

5 3
1 2 3 4 5

Sample Output 1

3 4 5 1 2

Initially, the integers written on the cards are 1,2,3,4,5 from top to bottom.

After taking three cards from the bottom of the stack and placing them on top, the integers written on the cards become 3,4,5,1,2 from top to bottom.


Sample Input 2

6 2
1 2 1 2 1 2

Sample Output 2

1 2 1 2 1 2

The integers written on the cards are not necessarily distinct.

B - Fizz

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

正整数 N が与えられます。

N 行出力してください。i 行目 (1\le i\le N) には、i が 3 の倍数ならば Fizz を、3 の倍数でないならば i を出力してください。

制約

  • N は 1 以上 100 以下の整数

入力

入力は以下の形式で標準入力から与えられる。

N

出力

答えを出力せよ。


入力例 1

4

出力例 1

1
2
Fizz
4

1 行目では 1 は 3 の倍数ではないので 1 を出力します。

2 行目では 2 は 3 の倍数ではないので 2 を出力します。

3 行目では 3 は 3 の倍数なので Fizz を出力します。

4 行目では 4 は 3 の倍数ではないので 4 を出力します。


入力例 2

10

出力例 2

1
2
Fizz
4
5
Fizz
7
8
Fizz
10

Score : 100 points

Problem Statement

You are given a positive integer N.

Output N lines. The i-th line (1\le i\le N) should contain Fizz if i is a multiple of 3, and i if i is not a multiple of 3.

Constraints

  • N is an integer between 1 and 100, inclusive.

Input

The input is given from Standard Input in the following format:

N

Output

Output the answer.


Sample Input 1

4

Sample Output 1

1
2
Fizz
4

The first line should contain 1 since 1 is not a multiple of 3.

The second line should contain 2 since 2 is not a multiple of 3.

The third line should contain Fizz since 3 is a multiple of 3.

The fourth line should contain 4 since 4 is not a multiple of 3.


Sample Input 2

10

Sample Output 2

1
2
Fizz
4
5
Fizz
7
8
Fizz
10
C - TaK Code

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

高橋君は 2 次元コード TaK Code を考案しました。以下の条件を全て満たすものが TaK Code です。

  • 縦 9 マス 横 9 マスの領域である
  • 左上及び右下の縦 3 マス 横 3 マスの領域に含まれる計 18 マスは全て黒である
  • 左上及び右下の縦 3 マス 横 3 マスの領域に八方位で隣接する計 14 マスは全て白である

TaK Code を回転させることはできません。

縦 N マス、横 M マスのグリッドがあります。 グリッドの状態は N 個の長さ M の文字列 S_1,\ldots,S_N で与えられ、上から i 行目左から j 列目のマスは S_i の j 文字目が # のとき黒、. のとき白です。

グリッドに完全に含まれる縦 9 マス横 9 マスの領域で、TaK Code の条件を満たす箇所を全て求めてください。

制約

  • 9 \leq N,M \leq 100
  • N,M は整数である
  • S_i は . および # のみからなる長さ M の文字列である

入力

入力は以下の形式で標準入力から与えられる。

N M
S_1
\vdots
S_N

出力

上から i 行目左から j 列目のマスを左上とする縦 9 マス 横 9 マスの領域が TaK Code の条件を満たす (i,j) の組全てを辞書順の昇順で 1 行に 1 組ずつ、i,j をこの順に空白区切りで出力せよ。
(i,j) の組の辞書順の昇順とは、i の昇順、i が等しい組は j の昇順にすることである。


入力例 1

19 18
###......###......
###......###......
###..#...###..#...
..............#...
..................
..................
......###......###
......###......###
......###......###
.###..............
.###......##......
.###..............
............###...
...##.......###...
...##.......###...
.......###........
.......###........
.......###........
........#.........

出力例 1

1 1
1 10
7 7
10 2

TaK Code は以下のものです。# が黒マス、. が白マス、? が白黒どちらでもよいマスを表します。

###.?????
###.?????
###.?????
....?????
?????????
?????....
?????.###
?????.###
?????.###

入力で与えられたグリッドの上から 10 マス目左から 2 列目のマスを左上とする縦 9 マス 横 9 マスの領域は以下のようになっており、TaK Code の条件を満たします。

###......
###......
###......
.........
..##.....
..##.....
......###
......###
......###

入力例 2

9 21
###.#...........#.###
###.#...........#.###
###.#...........#.###
....#...........#....
#########...#########
....#...........#....
....#.###...###.#....
....#.###...###.#....
....#.###...###.#....

出力例 2

1 1

入力例 3

18 18
######............
######............
######............
######............
######............
######............
..................
..................
..................
..................
..................
..................
............######
............######
............######
............######
............######
............######

出力例 3


TaK Code の条件を満たす箇所が 1 つもないこともあります。

Score : 200 points

Problem Statement

Takahashi invented Tak Code, a two-dimensional code. A TaK Code satisfies all of the following conditions:

  • It is a region consisting of nine horizontal rows and nine vertical columns.
  • All the 18 cells in the top-left and bottom-right three-by-three regions are black.
  • All the 14 cells that are adjacent (horizontally, vertically, or diagonally) to the top-left or bottom-right three-by-three region are white.

It is not allowed to rotate a TaK Code.

You are given a grid with N horizontal rows and M vertical columns. The state of the grid is described by N strings, S_1,\ldots, and S_N, each of length M. The cell at the i-th row from the top and j-th column from the left is black if the j-th character of S_i is #, and white if it is ..

Find all the nine-by-nine regions, completely contained in the grid, that satisfy the conditions of a TaK Code.

Constraints

  • 9 \leq N,M \leq 100
  • N and M are integers.
  • S_i is a string of length M consisting of . and #.

Input

The input is given from Standard Input in the following format:

N M
S_1
\vdots
S_N

Output

For all pairs (i,j) such that the nine-by-nine region, whose top-left cell is at the i-th row from the top and j-th columns from the left, satisfies the conditions of a TaK Code, print a line containing i, a space, and j in this order.
The pairs must be sorted in lexicographical ascending order; that is, i must be in ascending order, and within the same i, j must be in ascending order.


Sample Input 1

19 18
###......###......
###......###......
###..#...###..#...
..............#...
..................
..................
......###......###
......###......###
......###......###
.###..............
.###......##......
.###..............
............###...
...##.......###...
...##.......###...
.......###........
.......###........
.......###........
........#.........

Sample Output 1

1 1
1 10
7 7
10 2

A TaK Code looks like the following, where # is a black cell, . is a white cell, and ? can be either black or white.

###.?????
###.?????
###.?????
....?????
?????????
?????....
?????.###
?????.###
?????.###

In the grid given by the input, the nine-by-nine region, whose top-left cell is at the 10-th row from the top and 2-nd column from the left, satisfies the conditions of a TaK Code, as shown below.

###......
###......
###......
.........
..##.....
..##.....
......###
......###
......###

Sample Input 2

9 21
###.#...........#.###
###.#...........#.###
###.#...........#.###
....#...........#....
#########...#########
....#...........#....
....#.###...###.#....
....#.###...###.#....
....#.###...###.#....

Sample Output 2

1 1

Sample Input 3

18 18
######............
######............
######............
######............
######............
######............
..................
..................
..................
..................
..................
..................
............######
............######
............######
............######
............######
............######

Sample Output 3


There may be no region that satisfies the conditions of TaK Code.

D - Booby Prize

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

1,\ldots,N の番号のついた N 人の選手がゲームを行いました。選手 i のスコアは A_i であり、スコアが小さい方が上位になります。

ブービー賞に該当する選手、すなわち、下位から 2 番目の選手の番号を求めてください。

制約

  • 2 \leq N \leq 2\times 10^5
  • 1 \leq A_i \leq 10^9
  • A_i は相異なる
  • 入力に含まれる値は全て整数である

入力

入力は以下の形式で標準入力から与えられる。

N
A_1 \ldots A_N 

出力

答えを出力せよ。


入力例 1

6
1 123 12345 12 1234 123456

出力例 1

3

6 人中 5 位になるのは、選手 3 です。


入力例 2

5
3 1 4 15 9

出力例 2

5

Score : 200 points

Problem Statement

N players, who are numbered 1, \ldots, N, have played a game. Player i has scored A_i, and a player with a smaller score ranks higher.

The player who ranks the second lowest will receive a booby prize. Who is this player? Answer with an integer representing the player.

Constraints

  • 2 \leq N \leq 2\times 10^5
  • 1 \leq A_i \leq 10^9
  • A_i are distinct.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 \ldots A_N 

Output

Print the answer.


Sample Input 1

6
1 123 12345 12 1234 123456

Sample Output 1

3

It is Player 3 who ranks fifth among the six players.


Sample Input 2

5
3 1 4 15 9

Sample Output 2

5
E - Collision 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

xy 座標平面上に N 人の人がいます。人 i は (X_i, Y_i) にいます。すべての人は異なる地点にいます。

L, R からなる長さ N の文字列 S があります。
人 i は S_i = R ならば右向きに、S_i = L ならば左向きに、一斉に同じ速度で歩き始めます。ここで、右は x 軸の正の向き、左は x 軸の負の向きです。

たとえば (X_1, Y_1) = (2, 3), (X_2, Y_2) = (1, 1), (X_3, Y_3) =(4, 1), S = RRL の場合は次の図のように動きます。

image

反対の向きに歩いている人同士が同じ地点に来ることを「衝突」と呼びます。すべての人が歩き続けたとき、衝突は発生しますか?

制約

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq X_i \leq 10^9
  • 0 \leq Y_i \leq 10^9
  • i \neq j ならば (X_i, Y_i) \neq (X_j, Y_j) である。
  • X_i, Y_i はすべて整数である。
  • S は L および R からなる長さ N の文字列である。

入力

入力は以下の形式で標準入力から与えられる。

N
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N
S

出力

衝突が発生するならば Yes を、発生しないならば No を出力せよ。


入力例 1

3
2 3
1 1
4 1
RRL

出力例 1

Yes

この入力は問題文にある例と同じケースです。
すべての人が歩き続けると人 2 と人 3 が衝突します。よって Yes を出力します。


入力例 2

2
1 1
2 1
RR

出力例 2

No

人 1 と人 2 は同じ向きに歩いているので衝突することはありません。


入力例 3

10
1 3
1 4
0 0
0 2
0 4
3 1
2 4
4 2
4 4
3 3
RLRRRLRLRR

出力例 3

Yes

Score : 300 points

Problem Statement

There are N people in an xy-plane. Person i is at (X_i, Y_i). The positions of all people are different.

We have a string S of length N consisting of L and R.
If S_i = R, Person i is facing right; if S_i = L, Person i is facing left. All people simultaneously start walking in the direction they are facing. Here, right and left correspond to the positive and negative x-direction, respectively.

For example, the figure below shows the movement of people when (X_1, Y_1) = (2, 3), (X_2, Y_2) = (1, 1), (X_3, Y_3) =(4, 1), S = RRL.

image

We say that there is a collision when two people walking in opposite directions come to the same position. Will there be a collision if all people continue walking indefinitely?

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq X_i \leq 10^9
  • 0 \leq Y_i \leq 10^9
  • (X_i, Y_i) \neq (X_j, Y_j) if i \neq j.
  • All X_i and Y_i are integers.
  • S is a string of length N consisting of L and R.

Input

Input is given from Standard Input in the following format:

N
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N
S

Output

If there will be a collision, print Yes; otherwise, print No.


Sample Input 1

3
2 3
1 1
4 1
RRL

Sample Output 1

Yes

This input corresponds to the example in the Problem Statement.
If all people continue walking, Person 2 and Person 3 will collide. Thus, Yes should be printed.


Sample Input 2

2
1 1
2 1
RR

Sample Output 2

No

Since Person 1 and Person 2 walk in the same direction, they never collide.


Sample Input 3

10
1 3
1 4
0 0
0 2
0 4
3 1
2 4
4 2
4 4
3 3
RLRRRLRLRR

Sample Output 3

Yes
F - (K+1)-th Largest Number

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

長さ N の数列 A = (A_1, A_2, \ldots, A_N) が与えられます。 K = 0, 1, 2, \ldots, N-1 のそれぞれについて、下記の問題を解いてください。

1 以上 N 以下の整数 i であって、次の条件を満たすものの個数を求めよ。

  • A に含まれる整数のうち A_i より大きいものはちょうど K 種類である。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

N
A_1 A_2 \ldots A_N

出力

N 行出力せよ。 i = 1, 2, \ldots, N について、i 行目には K = i-1 の場合の問題の答えを出力せよ。


入力例 1

6
2 7 1 8 2 8

出力例 1

2
1
2
1
0
0

例として、K = 2 の場合の問題の答えを以下で求めます。

  • A_1 = 2 に関して、A に含まれる整数のうち A_1 より大きいものは、7, 8 の 2 種類です。
  • A_2 = 7 に関して、A に含まれる整数のうち A_2 より大きいものは、8 の 1 種類です。
  • A_3 = 1 に関して、A に含まれる整数のうち A_3 より大きいものは、2, 7, 8 の 3 種類です。
  • A_4 = 8 に関して、A に含まれる整数のうち A_4 より大きいものは、0 種類です(存在しません)。
  • A_5 = 2 に関して、A に含まれる整数のうち A_5 より大きいものは、7, 8 の 2 種類です。
  • A_6 = 8 に関して、A に含まれる整数のうち A_6 より大きいものは、0 種類です(存在しません)。

よって、A に含まれる整数のうちA_i より大きいものがちょうど K = 2 種類であるような i は、i = 1 と i = 5 の 2 つです。よって、K = 2 の場合の答えは 2 です。


入力例 2

1
1

出力例 2

1

入力例 3

10
979861204 57882493 979861204 447672230 644706927 710511029 763027379 710511029 447672230 136397527

出力例 3

2
1
2
1
2
1
1
0
0
0

Score : 300 points

Problem Statement

You are given a sequence A = (A_1, A_2, \ldots, A_N) of length N. For each K = 0, 1, 2, \ldots, N-1, solve the following problem.

Find the number of integers i between 1 and N (inclusive) such that:

  • A contains exactly K distinct integers greater than A_i.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_N

Output

Print N lines. For i = 1, 2, \ldots, N, the i-th line should contain the answer for K = i-1.


Sample Input 1

6
2 7 1 8 2 8

Sample Output 1

2
1
2
1
0
0

For example, we will find the answer for K=2.

  • Regarding A_1 = 2, A contains 2 distinct integers greater than A_1: 7 and 8.
  • Regarding A_2 = 7, A contains 1 distinct integer greater than A_2: 8.
  • Regarding A_3 = 1, A contains 3 distinct integers greater than A_3: 2, 7, and 8.
  • Regarding A_4 = 8, A contains 0 distinct integers greater than A_4 (there is no such integer).
  • Regarding A_5 = 2, A contains 2 distinct integers greater than A_5: 7 and 8.
  • Regarding A_6 = 8, A contains 0 distinct integers greater than A_6 (there is no such integer).

Thus, there are two i's, i = 1 and i = 5, such that A contains exactly K = 2 distinct integers greater than A_i. Therefore, the answer for K = 2 is 2.


Sample Input 2

1
1

Sample Output 2

1

Sample Input 3

10
979861204 57882493 979861204 447672230 644706927 710511029 763027379 710511029 447672230 136397527

Sample Output 3

2
1
2
1
2
1
1
0
0
0
G - Square Pair

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

長さ N の非負整数列 A=(A_1,\ldots,A_N) が与えられます。以下の条件を共に満たす整数組 (i,j) の個数を求めてください。

  • 1\leq i < j\leq N
  • A_i A_j は平方数

ここで非負整数 a は、ある非負整数 d を用いて a=d^2 と表せる場合平方数と呼ばれます。

制約

  • 入力は全て整数
  • 2\leq N\leq 2\times 10^5
  • 0\leq A_i\leq 2\times 10^5

入力

入力は以下の形式で標準入力から与えられる。

N
A_1 \ldots A_N

出力

答えを出力せよ。


入力例 1

5
0 3 2 8 12

出力例 1

6

条件を満たす整数組は (i,j)=(1,2),(1,3),(1,4),(1,5),(2,5),(3,4) の 6 つです。

例えば、A_2A_5=36 であり 36 は平方数なので、(i,j)=(2,5) は条件を満たします。


入力例 2

8
2 2 4 6 3 100 100 25

出力例 2

7

Score: 400 points

Problem Statement

You are given a sequence of non-negative integers A=(A_1,\ldots,A_N) of length N. Find the number of pairs of integers (i,j) that satisfy both of the following conditions:

  • 1\leq i < j\leq N
  • A_i A_j is a square number.

Here, a non-negative integer a is called a square number when it can be expressed as a=d^2 using some non-negative integer d.

Constraints

  • All inputs are integers.
  • 2\leq N\leq 2\times 10^5
  • 0\leq A_i\leq 2\times 10^5

Input

The input is given from Standard Input in the following format:

N
A_1 \ldots A_N

Output

Print the answer.


Sample Input 1

5
0 3 2 8 12

Sample Output 1

6

Six pairs of integers, (i,j)=(1,2),(1,3),(1,4),(1,5),(2,5),(3,4), satisfy the conditions.

For example, A_2A_5=36, and 36 is a square number, so the pair (i,j)=(2,5) satisfies the conditions.


Sample Input 2

8
2 2 4 6 3 100 100 25

Sample Output 2

7
H - Manhattan Multifocal Ellipse

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 475 点

問題文

2 次元平面上の N 個の点 (x_1,y_1),(x_2,y_2),\dots,(x_N,y_N) と、非負整数 D が与えられます。

整数の組 (x,y) であって、 \displaystyle \sum_{i=1}^N (|x-x_i|+|y-y_i|) \leq D を満たすものの個数を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq D \leq 10^6
  • -10^6 \leq x_i, y_i \leq 10^6
  • i\neq j ならば (x_i,y_i) \neq (x_j,y_j)
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

N D
x_1 y_1
x_2 y_2
\vdots
x_N y_N

出力

答えを出力せよ。


入力例 1

2 3
0 0
1 0

出力例 1

8

下図は、サンプル 1 の入力と答えを可視化したものです。青い点が入力を表します。青い点と赤い点の合計 8 点が、問題文中の条件を満たす点です。


入力例 2

2 0
0 0
2 0

出力例 2

0

入力例 3

6 100
9 -6
10 -1
2 10
-1 7
-7 5
-1 -4

出力例 3

419

Score : 475 points

Problem Statement

You are given N points (x_1, y_1), (x_2, y_2), \dots, (x_N, y_N) on a two-dimensional plane, and a non-negative integer D.

Find the number of integer pairs (x, y) such that \displaystyle \sum_{i=1}^N (|x-x_i|+|y-y_i|) \leq D.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq D \leq 10^6
  • -10^6 \leq x_i, y_i \leq 10^6
  • (x_i, y_i) \neq (x_j, y_j) for i \neq j.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N D
x_1 y_1
x_2 y_2
\vdots
x_N y_N

Output

Print the answer.


Sample Input 1

2 3
0 0
1 0

Sample Output 1

8

The following figure visualizes the input and the answer for Sample 1. The blue points represent the input. The blue and red points, eight in total, satisfy the condition in the statement.


Sample Input 2

2 0
0 0
2 0

Sample Output 2

0

Sample Input 3

6 100
9 -6
10 -1
2 10
-1 7
-7 5
-1 -4

Sample Output 3

419
I - Interval Inversion Count

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500 点

問題文

正整数 N と、(1,2,\ldots,N) の順列 P=(P _ 1,P _ 2,\ldots,P _ N) が与えられます。

整数 K が与えられます。 次の 2 つの条件を満たす整数の組 (l,r) がいくつあるか求めてください。

  • 1\le l\le r\le N
  • 列 (P _ l,P _ {l+1},\ldots,P _ r) の転倒数が K と等しい。

制約

  • 1\le N\le5\times10 ^ 5
  • 0\le K\le\dfrac{N(N-1)}2
  • 1\le P _ i\le N\ (1\le i\le N)
  • P _ i\ne P _ j\ (1\le i\lt j\le N)
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

N K
P _ 1 P _ 2 \ldots P _ N

出力

条件を満たす整数の組 (l,r) の個数を出力せよ。


入力例 1

7 3
6 3 2 1 7 5 4

出力例 1

5

例えば、列 (P _ 1,P _ 2,P _ 3)=(6,3,2) の転倒数が 3 なので、(l,r)=(1,3) は条件を満たします。 他には (l,r)=(2,4),(2,5),(4,7),(5,7) が条件を満たすので、5 を出力してください。


入力例 2

4 1
1 2 3 4

出力例 2

0

P の連続する部分列の転倒数はすべて 0 になるので、条件を満たす (l,r) は存在しません。 よって、0 を出力してください。


入力例 3

25 18
14 19 24 8 12 11 6 5 3 13 22 15 17 2 9 4 7 18 10 25 23 16 1 20 21

出力例 3

3

Score : 500 points

Problem Statement

You are given a positive integer N and a permutation P=(P _ 1,P _ 2,\ldots,P _ N) of (1,2,\ldots,N).

You are given an integer K. Find the number of pairs of integers (l,r) satisfying the following two conditions.

  • 1\le l\le r\le N
  • The inversion number of the sequence (P _ l,P _ {l+1},\ldots,P _ r) equals K.

Constraints

  • 1\le N\le5\times10 ^ 5
  • 0\le K\le\dfrac{N(N-1)}2
  • 1\le P _ i\le N\ (1\le i\le N)
  • P _ i\ne P _ j\ (1\le i\lt j\le N)
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N K
P _ 1 P _ 2 \ldots P _ N

Output

Output the number of pairs of integers (l,r) satisfying the conditions.


Sample Input 1

7 3
6 3 2 1 7 5 4

Sample Output 1

5

For example, the inversion number of the sequence (P _ 1,P _ 2,P _ 3)=(6,3,2) is 3, so (l,r)=(1,3) satisfies the conditions. The other pairs satisfying the conditions are (l,r)=(2,4),(2,5),(4,7),(5,7), so output 5.


Sample Input 2

4 1
1 2 3 4

Sample Output 2

0

The inversion number of every contiguous subsequence of P is 0, so there are no pairs (l,r) satisfying the conditions. Thus, output 0.


Sample Input 3

25 18
14 19 24 8 12 11 6 5 3 13 22 15 17 2 9 4 7 18 10 25 23 16 1 20 21

Sample Output 3

3