A - Addressing Wedding Invitations

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 266

問題文

高橋君は国際会議の運営スタッフとして、参加者への招待状を作成することになりました。

招待状には N 人の参加者がリストアップされており、i 番目 (1 \leq i \leq N) の参加者には名前 S_i と肩書き R_i が登録されています。招待状では、各参加者の名前の後ろに、以下のルールで決まる敬称を半角スペース区切りで並べて記載します。

  • 肩書きが teacher または doctor の人には、敬称として sensei を付ける
  • 肩書きが student または other の人には、敬称として san を付ける

さらに、共同運営者の青木君から「リストの並び順を調整してほしい」との要望がありました。具体的には、参加者を以下の2つのグループに分け、グループ A の全員をグループ B の全員より前に配置します。

  • グループ A:肩書きが teacher または doctor の人
  • グループ B:肩書きが student または other の人

各グループ内では、入力で与えられた順番(i の昇順)をそのまま保ちます。すなわち、同じグループに属する参加者同士の前後関係は、入力での前後関係と一致するようにします。

並べ替えた後のリストについて、各参加者の名前と敬称を半角スペース区切りで順に出力してください。ここで出力する敬称は、肩書きそのものではなく、上記ルールにより決まる sensei または san です。

制約

  • 1 \leq N \leq 2 \times 10^5
  • S_i は英小文字のみからなる長さ 1 以上 20 以下の文字列である。
  • R_iteacher, doctor, student, other のいずれかである。
  • 同じ名前の参加者が複数いる場合もある。また、同じ名前かつ同じ肩書きの参加者が複数いる場合もある。

入力

N
S_1 R_1
S_2 R_2
\vdots
S_N R_N

1 行目には、参加者の人数を表す整数 N が与えられる。続く N 行のうち i 行目には、i 番目の参加者の名前を表す文字列 S_i と肩書きを表す文字列 R_i が半角スペース区切りで与えられる。

出力

並べ替え後のリストを N 行で出力せよ。j 行目 (1 \leq j \leq N) には、並べ替え後の j 番目の参加者について、名前と敬称を半角スペース区切りで出力せよ。ここで敬称とは、肩書きそのものではなく、上記ルールにより決まる sensei または san のことである。


入力例 1

4
alice student
bob teacher
carol other
dave doctor

出力例 1

bob sensei
dave sensei
alice san
carol san

入力例 2

3
emma teacher
frank doctor
grace student

出力例 2

emma sensei
frank sensei
grace san

入力例 3

10
akira other
ben doctor
chika student
diana teacher
eri doctor
felix other
gina teacher
hiro student
iris other
jack doctor

出力例 3

ben sensei
diana sensei
eri sensei
gina sensei
jack sensei
akira san
chika san
felix san
hiro san
iris san

入力例 4

20
alice student
bob teacher
carol doctor
dave other
emma student
frank doctor
grace teacher
henry other
iris doctor
jack student
kate teacher
leo other
mia student
noah doctor
olivia teacher
paul other
quinn doctor
ruby student
sam teacher
tina other

出力例 4

bob sensei
carol sensei
frank sensei
grace sensei
iris sensei
kate sensei
noah sensei
olivia sensei
quinn sensei
sam sensei
alice san
dave san
emma san
henry san
jack san
leo san
mia san
paul san
ruby san
tina san

入力例 5

1
a other

出力例 5

a san

Score : 266 pts

Problem Statement

Takahashi, as an organizing staff member of an international conference, has been tasked with preparing invitation letters for the participants.

The invitation list contains N participants. The i-th participant (1 \leq i \leq N) has a name S_i and a title R_i registered. On the invitation, each participant's name is followed by an honorific determined by the following rules, separated by a space:

  • For people whose title is teacher or doctor, the honorific sensei is appended.
  • For people whose title is student or other, the honorific san is appended.

Furthermore, co-organizer Aoki has requested that "the order of the list be adjusted." Specifically, the participants are divided into the following two groups, and all members of Group A are placed before all members of Group B:

  • Group A: People whose title is teacher or doctor
  • Group B: People whose title is student or other

Within each group, the order given in the input (ascending order of i) is preserved. That is, the relative order of participants belonging to the same group must match their relative order in the input.

For the rearranged list, output each participant's name and honorific separated by a space, in order. Note that the honorific to output is not the title itself, but sensei or san as determined by the rules above.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • S_i is a string of length between 1 and 20 inclusive, consisting only of lowercase English letters.
  • R_i is one of teacher, doctor, student, other.
  • There may be multiple participants with the same name. There may also be multiple participants with both the same name and the same title.

Input

N
S_1 R_1
S_2 R_2
\vdots
S_N R_N

The first line contains an integer N representing the number of participants. In the following N lines, the i-th line contains a string S_i representing the name of the i-th participant and a string R_i representing their title, separated by a space.

Output

Output the rearranged list in N lines. The j-th line (1 \leq j \leq N) should contain the name and honorific of the j-th participant after rearrangement, separated by a space. Note that the honorific is not the title itself, but sensei or san as determined by the rules above.


Sample Input 1

4
alice student
bob teacher
carol other
dave doctor

Sample Output 1

bob sensei
dave sensei
alice san
carol san

Sample Input 2

3
emma teacher
frank doctor
grace student

Sample Output 2

emma sensei
frank sensei
grace san

Sample Input 3

10
akira other
ben doctor
chika student
diana teacher
eri doctor
felix other
gina teacher
hiro student
iris other
jack doctor

Sample Output 3

ben sensei
diana sensei
eri sensei
gina sensei
jack sensei
akira san
chika san
felix san
hiro san
iris san

Sample Input 4

20
alice student
bob teacher
carol doctor
dave other
emma student
frank doctor
grace teacher
henry other
iris doctor
jack student
kate teacher
leo other
mia student
noah doctor
olivia teacher
paul other
quinn doctor
ruby student
sam teacher
tina other

Sample Output 4

bob sensei
carol sensei
frank sensei
grace sensei
iris sensei
kate sensei
noah sensei
olivia sensei
quinn sensei
sam sensei
alice san
dave san
emma san
henry san
jack san
leo san
mia san
paul san
ruby san
tina san

Sample Input 5

1
a other

Sample Output 5

a san
B - Available Time Slots for Meeting Rooms

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

高橋君は、会社の会議室の予約管理を任されています。

会議室の利用可能時間は時刻 0 から時刻 T までの区間 [0, T] です。

今日の会議室には N 件の予約が入っています。i 番目の予約は時刻 S_i に開始し時刻 E_i に終了します。すなわち、i 番目の予約によって半開区間 [S_i, E_i) の時間帯が使用されます。予約の時間帯は互いに重なりません。ただし、予約は開始時刻の昇順に与えられるとは限りません。

利用可能時間 [0, T] のうち、どの予約にも使用されていない部分を空き時間と呼びます。高橋君は、急遽開催することになった打ち合わせのために、できるだけ長い連続した空き時間を確保したいと考えています。

今日の会議室における最も長い連続した空き時間の長さを求めてください。空き時間が存在しない場合は 0 を出力してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq T \leq 10^9
  • 0 \leq S_i < E_i \leq T (1 \leq i \leq N)
  • 予約の時間帯は互いに重ならない。すなわち、i \neq j ならば半開区間 [S_i, E_i) と半開区間 [S_j, E_j) は共通部分を持たない
  • 入力はすべて整数

入力

N T
S_1 E_1
S_2 E_2
\vdots
S_N E_N
  • 1 行目には、予約の件数を表す整数 N と、会議室の利用可能終了時刻を表す整数 T が、スペース区切りで与えられる。
  • 2 行目から N + 1 行目では、各予約の開始時刻と終了時刻が与えられる。
  • 1 + i 行目では、i 番目の予約の開始時刻 S_i と終了時刻 E_i がスペース区切りで与えられる。

出力

最も長い連続した空き時間の長さを 1 行で出力してください。空き時間が存在しない場合は 0 を出力してください。


入力例 1

3 10
1 3
5 6
8 9

出力例 1

2

入力例 2

5 100
0 10
20 35
40 60
70 85
95 100

出力例 2

10

入力例 3

4 1000000000
100000000 200000000
300000000 400000000
600000000 700000000
900000000 950000000

出力例 3

200000000

Score : 300 pts

Problem Statement

Takahashi is in charge of managing meeting room reservations at his company.

The meeting room is available during the interval [0, T], from time 0 to time T.

There are N reservations for the meeting room today. The i-th reservation starts at time S_i and ends at time E_i. That is, the i-th reservation occupies the half-open interval [S_i, E_i). The time intervals of the reservations do not overlap with each other. However, the reservations are not necessarily given in ascending order of start time.

Among the available time [0, T], the portions not used by any reservation are called free time. Takahashi wants to secure the longest possible continuous free time for a meeting that was suddenly scheduled.

Find the length of the longest continuous free time in today's meeting room. If there is no free time, output 0.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq T \leq 10^9
  • 0 \leq S_i < E_i \leq T (1 \leq i \leq N)
  • The time intervals of the reservations do not overlap. That is, if i \neq j, then the half-open intervals [S_i, E_i) and [S_j, E_j) have no common part.
  • All inputs are integers.

Input

N T
S_1 E_1
S_2 E_2
\vdots
S_N E_N
  • The first line contains an integer N representing the number of reservations and an integer T representing the end of the meeting room's available time, separated by a space.
  • From the 2nd line to the (N + 1)-th line, the start time and end time of each reservation are given.
  • The (1 + i)-th line contains the start time S_i and end time E_i of the i-th reservation, separated by a space.

Output

Output the length of the longest continuous free time in a single line. If there is no free time, output 0.


Sample Input 1

3 10
1 3
5 6
8 9

Sample Output 1

2

Sample Input 2

5 100
0 10
20 35
40 60
70 85
95 100

Sample Output 2

10

Sample Input 3

4 1000000000
100000000 200000000
300000000 400000000
600000000 700000000
900000000 950000000

Sample Output 3

200000000
C - Darts with Bonus Area

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 366

問題文

高橋君はダーツ大会の得点集計を担当しています。

この大会では、青木君が合計 N 回ダーツを投げます。i 回目の投擲(とうてき)では、ダーツが刺さった位置のダーツボードの中心からの距離 D_i が記録されます。

このダーツ大会には特殊な得点ルールがあります。ダーツボード上には M 個の「ボーナスエリア」が設定されています。j 番目のボーナスエリアは、ダーツボードの中心からの距離が L_j 以上 R_j 以下であるリング状の領域です。なお、ボーナスエリア同士が重なっていることもあります。

各投擲の得点は次のように計算されます。

  • ダーツが刺さった位置の中心からの距離 D_i が、M 個のボーナスエリアのうち少なくとも 1 つに含まれる場合(すなわち、L_j \leq D_i \leq R_j を満たす j1 つ以上存在する場合)、その投擲の得点は 2 \times D_i です。複数のボーナスエリアに同時に含まれる場合でも、得点は 2 \times D_i のままです。
  • いずれのボーナスエリアにも含まれない場合、その投擲の得点は D_i です。

青木君が N 回投げた結果が与えられるので、高橋君に代わって得点の合計を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 0 \leq D_i \leq 10^9 (1 \leq i \leq N)
  • 0 \leq L_j \leq R_j \leq 10^9 (1 \leq j \leq M)
  • 入力はすべて整数である。

入力

N M
D_1 D_2 \ldots D_N
L_1 R_1
L_2 R_2
\vdots
L_M R_M
  • 1 行目には、投擲回数を表す整数 N とボーナスエリアの数を表す整数 M が、スペース区切りで与えられる。
  • 2 行目には、各投擲でダーツが刺さった位置のダーツボードの中心からの距離を表す整数 D_1, D_2, \ldots, D_N が、スペース区切りで与えられる。
  • 3 行目から 2+M 行目までの M 行では、各ボーナスエリアの範囲が与えられる。
  • 2 + j 行目 (1 \leq j \leq M) では、j 番目のボーナスエリアの下限 L_j と上限 R_j が整数としてスペース区切りで与えられる。

出力

得点の合計を整数として 1 行で出力せよ。


入力例 1

3 2
5 10 15
3 7
12 20

出力例 1

50

入力例 2

4 1
1 2 3 4
2 3

出力例 2

15

入力例 3

5 3
0 100 50 75 200
0 60
40 80
150 300

出力例 3

750

入力例 4

10 4
1000000000 500000000 0 999999999 250000000 750000000 100 200 300 400
0 100
999999998 1000000000
200 300
500000000 750000000

出力例 4

6750001598

入力例 5

1 1
0
0 0

出力例 5

0

Score : 366 pts

Problem Statement

Takahashi is in charge of scoring at a darts tournament.

In this tournament, Aoki throws darts a total of N times. For the i-th throw, the distance D_i from the center of the dartboard to where the dart lands is recorded.

This darts tournament has a special scoring rule. On the dartboard, M "bonus areas" are defined. The j-th bonus area is a ring-shaped region consisting of all points whose distance from the center of the dartboard is at least L_j and at most R_j. Note that bonus areas may overlap with each other.

The score for each throw is calculated as follows:

  • If the distance D_i from the center to where the dart lands is contained in at least one of the M bonus areas (that is, if there exists at least one j such that L_j \leq D_i \leq R_j), the score for that throw is 2 \times D_i. Even if the dart is contained in multiple bonus areas simultaneously, the score remains 2 \times D_i.
  • If it is not contained in any bonus area, the score for that throw is D_i.

Given the results of Aoki's N throws, calculate the total score on behalf of Takahashi.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 0 \leq D_i \leq 10^9 (1 \leq i \leq N)
  • 0 \leq L_j \leq R_j \leq 10^9 (1 \leq j \leq M)
  • All input values are integers.

Input

N M
D_1 D_2 \ldots D_N
L_1 R_1
L_2 R_2
\vdots
L_M R_M
  • The first line contains an integer N representing the number of throws and an integer M representing the number of bonus areas, separated by a space.
  • The second line contains integers D_1, D_2, \ldots, D_N representing the distances from the center of the dartboard to where the dart landed for each throw, separated by spaces.
  • The next M lines (from line 3 to line 2+M) give the range of each bonus area.
  • The (2 + j)-th line (1 \leq j \leq M) contains the lower bound L_j and upper bound R_j of the j-th bonus area as integers separated by a space.

Output

Output the total score as an integer on a single line.


Sample Input 1

3 2
5 10 15
3 7
12 20

Sample Output 1

50

Sample Input 2

4 1
1 2 3 4
2 3

Sample Output 2

15

Sample Input 3

5 3
0 100 50 75 200
0 60
40 80
150 300

Sample Output 3

750

Sample Input 4

10 4
1000000000 500000000 0 999999999 250000000 750000000 100 200 300 400
0 100
999999998 1000000000
200 300
500000000 750000000

Sample Output 4

6750001598

Sample Input 5

1 1
0
0 0

Sample Output 5

0
D - Maze and Trap Squares

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

高橋君は HW 列のマス目で表される迷路を探索しています。

開始地点 S からゴール G へ辿り着く必要があります。

各マスには次のいずれかの文字が書かれています。

  • S : 開始地点(通行可能)
  • G : ゴール(通行可能)
  • O : 通常マス(通行可能、アルファベットの大文字オー)
  • B : 壁(通行不可能)
  • P : 罠マス(通行可能)

高橋君は最初 S のマスにおり、受けた累積ダメージは 0 です。

1 回の移動で、現在いるマスから上下左右に隣接するマスへ進むことができます。ただし、マス目の外や B のマスには進むことができません。同じマスを何度でも通ることができます。

移動先のマスが P であるとき、そのマスに入るたびに累積ダメージが 1 増えます。同じ P のマスであっても、入るたびにダメージは加算されます。S, G, O のマスに入ってもダメージは増えません。

高橋君が G のマスに到達した時点で、探索は即座に終了します。

S から G へ到達する経路が存在する場合、到達時の累積ダメージの最小値を出力してください。

どのように移動しても G に到達できない場合は -1 を出力してください。

制約

  • 1 \leq H \leq 2000
  • 1 \leq W \leq 2000
  • H \times W \leq 10^6
  • H, W は整数である
  • A_i \, (1 \leq i \leq H)S, G, O, B, P からなる長さ W の文字列である
  • 入力全体にちょうど 1 個の S と、ちょうど 1 個の G が含まれる

入力

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

H W
A_1
A_2
\vdots
A_H

H, W は迷路の行数と列数を表す整数である。

A_i \, (1 \leq i \leq H) は長さ W の文字列であり、A_ij 文字目 (1 \leq j \leq W) は上から i 行目、左から j 列目のマスの種類を表す。

出力

S から G へ到達するまでに受ける累積ダメージの最小値を 1 行で出力せよ。到達できない場合は -1 を出力せよ。


入力例 1

3 5
SPOOG
BBBBO
OOOOO

出力例 1

1

入力例 2

4 5
SOBBG
OOBBB
BBBBB
POOOO

出力例 2

-1

入力例 3

8 10
SOOPBPOOOO
BBOPBOBBBO
OOOPOOPOOO
OBBBBBBPOB
OOPPPBOOOB
BOBOPBOBPO
OPOOPOOOPO
BBBBOBBBPG

出力例 3

3

入力例 4

12 16
SPOOBPOOOOBOPPPO
OBBOOPOBOOOOBOPO
OPOPBOOOBBOPOOBO
OOBOPPBOOOOPBOOO
OPPOBOOOPBBBOOPO
OBOPOOBOOOPOPBOO
OOOOPBBBOBOOOPPO
OPBBOOPOOOOBOBOO
OOPOBBOOPPOOBBPO
OBOPOOOPBOBOOOOO
OPPBBOOOOPPOBPOO
OOOOPOOOOPPOOOOG

出力例 4

1

入力例 5

1 2
SG

出力例 5

0

Score : 400 pts

Problem Statement

Takahashi is exploring a maze represented by a grid with H rows and W columns.

He needs to reach the goal G from the starting point S.

Each square contains one of the following characters:

  • S : Starting point (passable)
  • G : Goal (passable)
  • O : Normal square (passable, uppercase letter O)
  • B : Wall (impassable)
  • P : Trap square (passable)

Takahashi starts on the S square with a cumulative damage of 0.

In one move, he can advance from his current square to an adjacent square in one of the four directions (up, down, left, right). However, he cannot move outside the grid or onto a B square. He may pass through the same square any number of times.

When the destination square is P, the cumulative damage increases by 1 each time he enters that square. Even for the same P square, damage is added every time he enters it. Entering S, G, or O squares does not increase the damage.

The exploration ends immediately when Takahashi reaches the G square.

If a path from S to G exists, output the minimum cumulative damage upon reaching the goal.

If it is impossible to reach G regardless of how he moves, output -1.

Constraints

  • 1 \leq H \leq 2000
  • 1 \leq W \leq 2000
  • H \times W \leq 10^6
  • H, W are integers
  • A_i \, (1 \leq i \leq H) is a string of length W consisting of S, G, O, B, P
  • The entire input contains exactly one S and exactly one G

Input

The input is given from standard input in the following format:

H W
A_1
A_2
\vdots
A_H

H, W are integers representing the number of rows and columns of the maze.

A_i \, (1 \leq i \leq H) is a string of length W, where the j-th character (1 \leq j \leq W) of A_i represents the type of the square at the i-th row from the top and the j-th column from the left.

Output

Output in one line the minimum cumulative damage received when traveling from S to G. If it is impossible to reach G, output -1.


Sample Input 1

3 5
SPOOG
BBBBO
OOOOO

Sample Output 1

1

Sample Input 2

4 5
SOBBG
OOBBB
BBBBB
POOOO

Sample Output 2

-1

Sample Input 3

8 10
SOOPBPOOOO
BBOPBOBBBO
OOOPOOPOOO
OBBBBBBPOB
OOPPPBOOOB
BOBOPBOBPO
OPOOPOOOPO
BBBBOBBBPG

Sample Output 3

3

Sample Input 4

12 16
SPOOBPOOOOBOPPPO
OBBOOPOBOOOOBOPO
OPOPBOOOBBOPOOBO
OOBOPPBOOOOPBOOO
OPPOBOOOPBBBOOPO
OBOPOOBOOOPOPBOO
OOOOPBBBOBOOOPPO
OPBBOOPOOOOBOBOO
OOPOBBOOPPOOBBPO
OBOPOOOPBOBOOOOO
OPPBBOOOOPPOBPOO
OOOOPOOOOPPOOOOG

Sample Output 4

1

Sample Input 5

1 2
SG

Sample Output 5

0
E - Pile of Cards

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 466

問題文

高橋君はカードゲームの片付けをしています。テーブルの上に N 枚のカードが左から右に一列に並んでおり、左から i 番目のカードを「カード i」と呼びます。カード i には整数 A_i が書かれています。

高橋君はこれらのカードをいくつかの山に分けて整理します。山の作り方には以下のルールがあります:

  • カード 1, カード 2, \ldots, カード Nこの順番で 1 枚ずつ処理する(順番を入れ替えることはできない)。
  • 各カードについて、次のいずれかの操作を選ぶ:
  • 新しい山を作り、そのカードを置く。
  • 既に存在するいずれかの山の一番上に、そのカードを重ねる。ただし、その山の現在の一番上のカードに書かれた数値を x、これから重ねるカードに書かれた数値を y としたとき、x \geq y でなければならない。

このルールにより、各山ではカードに書かれた数値が下から上に向かって単調非増加になります。

高橋君はできるだけ少ない数の山にまとめたいと考えています。

さらに、高橋君は各カードについて「そのカードの直下にあるカードはどれか」も記録したいと考えています。カード i直下のカードとは、カード i を既存の山に重ねた場合に、その時点でその山の一番上にあったカードのことを指します。カード i が新しい山を作って置かれた場合(すなわち山の一番下にある場合)、直下のカードは存在しません。

各カード ii = 1, 2, \ldots, N)について、値 B_i を次のように定めます:

  • カード i の直下のカードが存在しない場合(山の一番下にある場合)、B_i = 0 とする。
  • カード i の直下のカードがカード j である場合、B_i = j とする。

山の数を最小にするすべての積み方を考えたとき、その中で B_1 + B_2 + \cdots + B_N最大となるような積み方を一つ求めてください。

山の数の最小値を K として、KB_1, B_2, \ldots, B_N を出力してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^91 \leq i \leq N
  • 入力はすべて整数である。

入力

N
A_1 A_2 \cdots A_N
  • 1 行目には、カードの枚数を表す整数 N が与えられる。
  • 2 行目には、各カードに書かれた数値を表す N 個の整数 A_1, A_2, \ldots, A_N がスペース区切りで与えられる。

出力

K
B_1 B_2 \cdots B_N
  • 1 行目には、山の数の最小値 K を出力せよ。
  • 2 行目には、山の数が最小であるすべての積み方の中で B_1 + B_2 + \cdots + B_N が最大となる積み方における B_1, B_2, \ldots, B_N をスペース区切りで出力せよ。該当する積み方が複数ある場合は、そのうちどれを出力してもよい。

入力例 1

5
3 1 4 2 2

出力例 1

2
0 1 0 3 4

入力例 2

4
1 2 3 4

出力例 2

4
0 0 0 0

入力例 3

12
5 3 4 4 2 6 1 5 5 3 2 7

出力例 3

4
0 0 1 3 4 0 5 6 8 9 10 0

入力例 4

30
10 20 15 15 30 25 5 18 18 17 40 35 34 10 9 50 45 45 44 1 60 59 20 19 70 2 2 80 79 78

出力例 4

8
0 0 2 3 0 5 4 6 8 9 0 11 12 13 14 0 16 17 18 19 0 21 22 23 0 25 26 0 28 29

入力例 5

1
1000000000

出力例 5

1
0

Score : 466 pts

Problem Statement

Takahashi is cleaning up a card game. There are N cards lined up in a row from left to right on a table. The i-th card from the left is called "card i". Card i has an integer A_i written on it.

Takahashi will organize these cards by dividing them into several piles. The piles must be created according to the following rules:

  • Process card 1, card 2, \ldots, card N in this order, one by one (the order cannot be changed).
  • For each card, choose one of the following operations:
  • Create a new pile and place the card on it.
  • Place the card on top of an already existing pile. However, if the number written on the current top card of that pile is x and the number on the card to be placed is y, then x \geq y must hold.

Under these rules, the numbers written on the cards in each pile will be monotonically non-increasing from bottom to top.

Takahashi wants to minimize the number of piles.

Furthermore, for each card, Takahashi wants to record "which card is directly below it". The card directly below card i refers to the card that was at the top of the pile at the moment card i was placed on top of that existing pile. If card i was placed to start a new pile (i.e., it is at the bottom of the pile), there is no card directly below it.

For each card i (i = 1, 2, \ldots, N), we define the value B_i as follows:

  • If there is no card directly below card i (i.e., it is at the bottom of a pile), B_i = 0.
  • If the card directly below card i is card j, B_i = j.

Among all ways of piling the cards that minimize the number of piles, find one way that maximizes B_1 + B_2 + \cdots + B_N.

Let K be the minimum number of piles. Output K and B_1, B_2, \ldots, B_N.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • All input values are integers.

Input

N
A_1 A_2 \cdots A_N
  • The first line contains an integer N, representing the number of cards.
  • The second line contains N space-separated integers A_1, A_2, \ldots, A_N, representing the numbers written on the cards.

Output

K
B_1 B_2 \cdots B_N
  • In the first line, output the minimum number of piles K.
  • In the second line, output B_1, B_2, \ldots, B_N separated by spaces, representing a way of piling that maximizes B_1 + B_2 + \cdots + B_N among all ways that minimize the number of piles. If there are multiple such ways, you may output any of them.

Sample Input 1

5
3 1 4 2 2

Sample Output 1

2
0 1 0 3 4

Sample Input 2

4
1 2 3 4

Sample Output 2

4
0 0 0 0

Sample Input 3

12
5 3 4 4 2 6 1 5 5 3 2 7

Sample Output 3

4
0 0 1 3 4 0 5 6 8 9 10 0

Sample Input 4

30
10 20 15 15 30 25 5 18 18 17 40 35 34 10 9 50 45 45 44 1 60 59 20 19 70 2 2 80 79 78

Sample Output 4

8
0 0 2 3 0 5 4 6 8 9 0 11 12 13 14 0 16 17 18 19 0 21 22 23 0 25 26 0 28 29

Sample Input 5

1
1000000000

Sample Output 5

1
0