A - 倉庫の在庫管理

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 266

問題文

高橋君は、ある会社の物流部門で働いています。会社には N 個の倉庫があり、倉庫 1 から倉庫 N まで番号が付けられています。各倉庫 i (1 \leq i \leq N) には現在 P_i 個の商品が保管されています。

本日、倉庫間で商品を移動させる M 件の配送指示が出されました。j 番目 (1 \leq j \leq M) の配送指示は「倉庫 U_j から倉庫 V_jW_j 個の商品を移動させる」という内容です。ここで、U_j \neq V_j です。同じ倉庫ペア間の配送指示が複数存在することもあります。

M 件の配送指示は逐次的にではなく一括で反映されます。すなわち、各倉庫の最終的な商品数は、初期の商品数に対して、その倉庫に入ってくる商品数の合計を加え、その倉庫から出て行く商品数の合計を引いたものです。

高橋君は、すべての配送指示を反映した後に商品数が最も多い倉庫の番号を求めたいです。商品数が最も多い倉庫が複数ある場合は、その中で番号が最も小さい倉庫の番号を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 0 \leq P_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq U_j \leq N (1 \leq j \leq M)
  • 1 \leq V_j \leq N (1 \leq j \leq M)
  • U_j \neq V_j (1 \leq j \leq M)
  • 1 \leq W_j \leq 10^9 (1 \leq j \leq M)
  • すべての入力値は整数である
  • すべての配送指示を反映した後、各倉庫の商品数は 0 以上になることが保証される

入力

N M
P_1 P_2 \ldots P_N
U_1 V_1 W_1
U_2 V_2 W_2
\vdots
U_M V_M W_M
  • 1 行目には、倉庫の数 N と配送指示の数 M が、スペース区切りで与えられる。
  • 2 行目には、各倉庫の初期の商品数 P_1, P_2, \ldots, P_N が、スペース区切りで与えられる。
  • 続く M 行では、各配送指示の内容が与えられる。M = 0 の場合、この部分は与えられない。
  • そのうち j 行目 (1 \leq j \leq M) では、j 番目の配送指示における移動元の倉庫番号 U_j、移動先の倉庫番号 V_j、移動する商品数 W_j が、スペース区切りで与えられる。

出力

すべての配送指示を反映した後に商品数が最も多い倉庫の番号を 1 行で出力してください。最も多い倉庫が複数ある場合は、番号が最も小さいものを出力してください。


入力例 1

3 2
10 20 30
1 2 5
3 2 10

出力例 1

2

入力例 2

3 2
10 20 10
2 1 5
2 3 5

出力例 2

1

入力例 3

6 5
100 200 150 300 250 50
4 2 50
1 3 30
5 6 100
2 1 80
3 5 40

出力例 3

4

入力例 4

10 8
500 300 800 200 600 100 450 350 700 250
3 1 200
5 7 150
9 2 300
4 6 100
1 10 400
8 3 50
7 9 100
6 5 50

出力例 4

3

入力例 5

1 0
1000000000

出力例 5

1

Score : 266 pts

Problem Statement

Takahashi works in the logistics department of a company. The company has N warehouses, numbered from warehouse 1 to warehouse N. Each warehouse i (1 \leq i \leq N) currently stores P_i items.

Today, M shipping instructions to move items between warehouses have been issued. The j-th (1 \leq j \leq M) shipping instruction is "move W_j items from warehouse U_j to warehouse V_j." Here, U_j \neq V_j. There may be multiple shipping instructions between the same pair of warehouses.

The M shipping instructions are applied all at once, not sequentially. That is, the final number of items in each warehouse is obtained by taking the initial number of items, adding the total number of items coming into that warehouse, and subtracting the total number of items going out of that warehouse.

Takahashi wants to find the warehouse number with the most items after all shipping instructions have been applied. If there are multiple warehouses with the most items, find the one with the smallest warehouse number.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 0 \leq P_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq U_j \leq N (1 \leq j \leq M)
  • 1 \leq V_j \leq N (1 \leq j \leq M)
  • U_j \neq V_j (1 \leq j \leq M)
  • 1 \leq W_j \leq 10^9 (1 \leq j \leq M)
  • All input values are integers.
  • It is guaranteed that after all shipping instructions have been applied, the number of items in each warehouse is 0 or more.

Input

N M
P_1 P_2 \ldots P_N
U_1 V_1 W_1
U_2 V_2 W_2
\vdots
U_M V_M W_M
  • The first line contains the number of warehouses N and the number of shipping instructions M, separated by a space.
  • The second line contains the initial number of items in each warehouse P_1, P_2, \ldots, P_N, separated by spaces.
  • The following M lines contain the details of each shipping instruction. If M = 0, this part is not given.
  • The j-th line (1 \leq j \leq M) contains the source warehouse number U_j, the destination warehouse number V_j, and the number of items to move W_j for the j-th shipping instruction, separated by spaces.

Output

Print on one line the warehouse number with the most items after all shipping instructions have been applied. If there are multiple warehouses with the most items, print the one with the smallest number.


Sample Input 1

3 2
10 20 30
1 2 5
3 2 10

Sample Output 1

2

Sample Input 2

3 2
10 20 10
2 1 5
2 3 5

Sample Output 2

1

Sample Input 3

6 5
100 200 150 300 250 50
4 2 50
1 3 30
5 6 100
2 1 80
3 5 40

Sample Output 3

4

Sample Input 4

10 8
500 300 800 200 600 100 450 350 700 250
3 1 200
5 7 150
9 2 300
4 6 100
1 10 400
8 3 50
7 9 100
6 5 50

Sample Output 4

3

Sample Input 5

1 0
1000000000

Sample Output 5

1
B - 荷物の配送センター

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 333

問題文

高橋君は配送会社で働いています。今日は N 個の荷物をそれぞれ指定された届け先に届ける必要があります。第 i 個の荷物の届け先は、数直線上の座標 D_i にあります。

この配送会社には M 箇所の配送センターがあり、第 j 番目の配送センターは数直線上の座標 S_j に位置しています。各荷物はいずれか 1 つの配送センターから出発して届け先へ運ばれます。配送センター j から荷物 i の届け先まで運ぶコストは、距離 |D_i - S_j| です。

各荷物はちょうど 1 つの配送センターに割り当てなければなりません。ただし、1 つの配送センターが複数の荷物を担当することも、どの荷物も担当しない配送センターがあることも許されます。

すべての荷物の配送コストの合計を最小化してください。

すなわち、各荷物 i1 \leq i \leq N )に対して配送センター f(i)1 \leq f(i) \leq M )を割り当てたとき、

\sum_{i=1}^{N} |D_i - S_{f(i)}|

の最小値を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • -10^9 \leq D_i \leq 10^91 \leq i \leq N
  • -10^9 \leq S_j \leq 10^91 \leq j \leq M
  • 入力はすべて整数である。

入力

N M
D_1 D_2 \ldots D_N
S_1 S_2 \ldots S_M
  • 1 行目には、荷物の個数を表す N と、配送センターの数を表す M が、スペース区切りで与えられる。
  • 2 行目には、各荷物の届け先の座標を表す D_1, D_2, \ldots, D_N が、スペース区切りで与えられる。
  • 3 行目には、各配送センターの座標を表す S_1, S_2, \ldots, S_M が、スペース区切りで与えられる。

出力

配送コストの合計の最小値を 1 行で出力せよ。


入力例 1

3 2
1 4 7
3 6

出力例 1

4

入力例 2

5 3
-10 -3 0 5 15
-7 2 12

出力例 2

15

入力例 3

10 4
-100 -50 -20 0 10 30 60 80 200 500
-40 15 70 450

出力例 3

325

Score : 333 pts

Problem Statement

Takahashi works at a delivery company. Today, he needs to deliver N packages to their designated destinations. The destination of the i-th package is at coordinate D_i on a number line.

This delivery company has M distribution centers, and the j-th distribution center is located at coordinate S_j on the number line. Each package departs from exactly one distribution center and is transported to its destination. The cost of transporting package i from distribution center j to its destination is the distance |D_i - S_j|.

Each package must be assigned to exactly one distribution center. However, a single distribution center may handle multiple packages, and there may be distribution centers that handle no packages at all.

Minimize the total delivery cost of all packages.

That is, when assigning a distribution center f(i) (1 \leq f(i) \leq M) to each package i (1 \leq i \leq N), find the minimum value of

\sum_{i=1}^{N} |D_i - S_{f(i)}|

Constraints

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

Input

N M
D_1 D_2 \ldots D_N
S_1 S_2 \ldots S_M
  • The first line contains N, the number of packages, and M, the number of distribution centers, separated by a space.
  • The second line contains D_1, D_2, \ldots, D_N, the coordinates of each package's destination, separated by spaces.
  • The third line contains S_1, S_2, \ldots, S_M, the coordinates of each distribution center, separated by spaces.

Output

Print the minimum total delivery cost on a single line.


Sample Input 1

3 2
1 4 7
3 6

Sample Output 1

4

Sample Input 2

5 3
-10 -3 0 5 15
-7 2 12

Sample Output 2

15

Sample Input 3

10 4
-100 -50 -20 0 10 30 60 80 200 500
-40 15 70 450

Sample Output 3

325
C - 連鎖停電

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 366

問題文

高橋君はある地域の電力網を管理しています。この電力網には N 個の変電所があり、変電所には 1 から N までの番号が付けられています。

変電所間には M 本の送電線が敷かれています。送電線 k1 \leq k \leq M)は変電所 U_k から変電所 V_k への方向に電力を供給しています。ここで、自己ループはありません(U_k \neq V_k)。また、同じ順序対 (U_k, V_k) が複数回現れることはありません。ただし、ある二つの変電所 u, v について、変電所 u から変電所 v への送電線と、変電所 v から変電所 u への送電線が同時に存在することはありえます。

各変電所 i1 \leq i \leq N)には耐久値 T_i が設定されています。また、各変電所 i には負荷と呼ばれる非負整数の値が定まります。初期状態では全ての変電所が稼働しており、変電所 i の負荷は、変電所 i に電力が流入する送電線の本数、すなわち V_k = i を満たす送電線 k の本数(有向グラフとしての変電所 i の入次数)に等しいです。

この電力網では、変電所の負荷が耐久値を超えると停電が発生し、それが連鎖的に広がる可能性があります。具体的な手順は以下の通りです。

なお、一度停電を起こした変電所の負荷はその後変化しません。

  1. 停電の発生: まだ停電を起こしていない変電所の中に、負荷が耐久値を真に超えているもの(すなわち、変電所 j について 負荷 > T_j であるもの)が存在すれば、その中からどれか一つを選ぶ。選んだ変電所を変電所 s とする。そのような変電所が存在しなければ、連鎖は終了する。
  2. 停電の処理: 変電所 s は停電を起こし、機能を停止する。変電所 s からの送電が途絶えるため、送電先の各変電所は不足した電力を他の経路から補おうとし、その結果負荷が増大する。具体的には、U_k = s であるような各送電線 k について、送電先の変電所 V_k がまだ停電を起こしていなければ、変電所 V_k の負荷を 1 増加させる(既に停電を起こしている変電所の負荷は変化しない)。
  3. 繰り返し: 手順 1 に戻る。

変電所の数は有限であるため、この連鎖は必ず有限回のステップで終了します。

注意: 手順 1 で複数の変電所が条件を満たす場合にどれを選んでも、最終的に停電を起こす変電所の集合は同一になることが証明できます。したがって、答えは一意に定まります。

連鎖が全て収まったとき(すなわち、まだ停電を起こしていない変電所の中に負荷が耐久値を真に超えるものが存在しなくなったとき)、最終的に停電を起こした変電所の番号を全て求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 0 \leq T_i \leq N1 \leq i \leq N
  • 1 \leq U_k \leq N1 \leq k \leq M
  • 1 \leq V_k \leq N1 \leq k \leq M
  • U_k \neq V_k1 \leq k \leq M
  • 同じ順序対 (U_k, V_k) は複数回現れない
  • 入力は全て整数である

入力

N M
T_1 T_2 \ldots T_N
U_1 V_1
U_2 V_2
\vdots
U_M V_M
  • 1 行目には、変電所の数 N と送電線の数 M が、スペース区切りで与えられる。
  • 2 行目には、各変電所の耐久値 T_1, T_2, \ldots, T_N が、スペース区切りで与えられる。
  • 続く M 行のうち k 行目(1 \leq k \leq M)には、送電線 k の情報として U_kV_k がスペース区切りで与えられる。これは変電所 U_k から変電所 V_k へ電力を供給する送電線があることを意味する。

出力

最終的に停電を起こした変電所の番号を昇順にスペース区切りで 1 行で出力せよ。停電を起こした変電所が 1 つもない場合は、代わりに -1 を出力せよ。


入力例 1

5 5
0 1 1 1 1
1 2
1 3
2 3
3 4
4 5

出力例 1

3 4 5

入力例 2

3 2
0 1 1
1 2
1 3

出力例 2

-1

入力例 3

7 9
0 1 1 1 1 2 2
1 2
1 3
2 4
3 4
4 5
4 6
5 7
6 7
2 6

出力例 3

4 5 6 7

入力例 4

10 12
0 1 1 1 1 1 1 1 2 1
1 2
1 3
2 3
3 4
3 5
4 6
5 6
6 7
6 8
7 9
8 9
9 10

出力例 4

3 4 5 6 7 8 9 10

入力例 5

1 0
0

出力例 5

-1

Score : 366 pts

Problem Statement

Takahashi manages the power grid of a certain region. This power grid has N substations, numbered from 1 to N.

There are M transmission lines laid between substations. Transmission line k (1 \leq k \leq M) supplies power in the direction from substation U_k to substation V_k. There are no self-loops (U_k \neq V_k). Also, the same ordered pair (U_k, V_k) does not appear more than once. However, for two substations u, v, it is possible for a transmission line from substation u to substation v and a transmission line from substation v to substation u to exist simultaneously.

Each substation i (1 \leq i \leq N) has a durability value T_i. Also, each substation i has a non-negative integer value called its load. Initially, all substations are operational, and the load of substation i equals the number of transmission lines through which power flows into substation i, that is, the number of transmission lines k satisfying V_k = i (the in-degree of substation i in the directed graph).

In this power grid, when a substation's load exceeds its durability value, a blackout occurs, and it may spread in a chain reaction. The specific procedure is as follows.

Note that the load of a substation that has already experienced a blackout does not change afterwards.

  1. Blackout occurrence: Among the substations that have not yet experienced a blackout, if there exists one whose load strictly exceeds its durability value (that is, for substation j, load > T_j), choose any one of them. Let the chosen substation be substation s. If no such substation exists, the chain ends.
  2. Blackout processing: Substation s experiences a blackout and ceases to function. Since power transmission from substation s is cut off, each destination substation tries to compensate for the lost power through other routes, resulting in increased load. Specifically, for each transmission line k such that U_k = s, if the destination substation V_k has not yet experienced a blackout, the load of substation V_k is increased by 1 (the load of substations that have already experienced a blackout does not change).
  3. Repeat: Return to step 1.

Since the number of substations is finite, this chain always terminates in a finite number of steps.

Note: It can be proven that regardless of which substation is chosen when multiple substations satisfy the condition in step 1, the final set of substations that experience a blackout is the same. Therefore, the answer is uniquely determined.

When the chain has completely settled (that is, when no substation that has not yet experienced a blackout has a load strictly exceeding its durability value), find all the substation numbers that ultimately experienced a blackout.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 0 \leq T_i \leq N (1 \leq i \leq N)
  • 1 \leq U_k \leq N (1 \leq k \leq M)
  • 1 \leq V_k \leq N (1 \leq k \leq M)
  • U_k \neq V_k (1 \leq k \leq M)
  • The same ordered pair (U_k, V_k) does not appear more than once
  • All input values are integers

Input

N M
T_1 T_2 \ldots T_N
U_1 V_1
U_2 V_2
\vdots
U_M V_M
  • The first line contains the number of substations N and the number of transmission lines M, separated by a space.
  • The second line contains the durability values T_1, T_2, \ldots, T_N of each substation, separated by spaces.
  • In the following M lines, the k-th line (1 \leq k \leq M) contains U_k and V_k separated by a space, representing the information of transmission line k. This means there is a transmission line supplying power from substation U_k to substation V_k.

Output

Output the numbers of all substations that ultimately experienced a blackout, in ascending order, separated by spaces, on a single line. If no substation experienced a blackout, output -1 instead.


Sample Input 1

5 5
0 1 1 1 1
1 2
1 3
2 3
3 4
4 5

Sample Output 1

3 4 5

Sample Input 2

3 2
0 1 1
1 2
1 3

Sample Output 2

-1

Sample Input 3

7 9
0 1 1 1 1 2 2
1 2
1 3
2 4
3 4
4 5
4 6
5 7
6 7
2 6

Sample Output 3

4 5 6 7

Sample Input 4

10 12
0 1 1 1 1 1 1 1 2 1
1 2
1 3
2 3
3 4
3 5
4 6
5 6
6 7
6 8
7 9
8 9
9 10

Sample Output 4

3 4 5 6 7 8 9 10

Sample Input 5

1 0
0

Sample Output 5

-1
D - アルバイトのシフト割り当て

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400

問題文

高橋君は学園祭の実行委員長を務めており、N 人のアルバイトスタッフを雇っています。各スタッフ i (1 \leq i \leq N) には、1時間あたりにこなせる作業量を表す作業効率 V_i が決まっています。

学園祭当日、M 件の仕事が発生しました。各仕事 j (1 \leq j \leq M) には、必要な作業量 D_j と、完了までの制限時間 T_j 時間が指定されています。

高橋君は、各仕事に対してスタッフを1人割り当てるかどうかを決めます。仕事に割り当てられなかったスタッフがいても構いませんし、スタッフが割り当てられず未完了のままとなる仕事があっても構いません。ただし、同じスタッフを複数の仕事に割り当てることはできません。つまり、各スタッフは最大で 1 件の仕事しか担当できません。

スタッフ i が仕事 j を担当する場合、仕事にかかる時間は D_j / V_i 時間です。この仕事を完了できるのは、かかる時間が制限時間以下である場合、すなわち D_j / V_i \leq T_jV_i \times T_j \geq D_j と同値)を満たす場合に限ります。この条件を満たさないスタッフをその仕事に割り当てることはできません。

高橋君がスタッフの割り当てを最適に行ったとき、完了できる仕事の最大件数を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq V_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq D_j \leq 10^9 (1 \leq j \leq M)
  • 1 \leq T_j \leq 10^9 (1 \leq j \leq M)
  • 入力はすべて整数

入力

N M
V_1 V_2 \ldots V_N
D_1 T_1
D_2 T_2
\vdots
D_M T_M
  • 1 行目には、スタッフの人数を表す N と、仕事の件数を表す M が、スペース区切りで与えられる。
  • 2 行目には、各スタッフの作業効率を表す V_1, V_2, \ldots, V_N が、スペース区切りで与えられる。
  • 3 行目から M 行にわたって、各仕事の情報が与えられる。
  • 2 + j 行目には、j 番目の仕事の必要作業量 D_j と制限時間 T_j が、スペース区切りで与えられる。

出力

完了できる仕事の最大件数を 1 行で出力してください。


入力例 1

3 3
5 3 7
10 2
15 3
6 1

出力例 1

2

入力例 2

4 5
2 4 6 8
12 2
20 5
8 1
24 4
30 3

出力例 2

3

入力例 3

6 7
10 20 15 5 25 30
100 5
50 2
200 10
75 3
150 6
300 15
80 4

出力例 3

3

Score : 400 pts

Problem Statement

Takahashi is serving as the executive committee chairman of a school festival and has hired N part-time staff members. Each staff member i (1 \leq i \leq N) has a work efficiency V_i, which represents the amount of work they can complete per hour.

On the day of the school festival, M jobs have arisen. Each job j (1 \leq j \leq M) has a required workload D_j and a time limit of T_j hours for completion.

Takahashi decides whether to assign a staff member to each job. It is acceptable for some staff members to not be assigned to any job, and it is also acceptable for some jobs to remain incomplete without any staff assigned. However, the same staff member cannot be assigned to multiple jobs. In other words, each staff member can handle at most 1 job.

When staff member i is assigned to job j, the time required for the job is D_j / V_i hours. The job can be completed only if the required time does not exceed the time limit, that is, only if D_j / V_i \leq T_j (equivalently, V_i \times T_j \geq D_j) is satisfied. A staff member who does not satisfy this condition cannot be assigned to that job.

Determine the maximum number of jobs that can be completed when Takahashi optimally assigns the staff.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq V_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq D_j \leq 10^9 (1 \leq j \leq M)
  • 1 \leq T_j \leq 10^9 (1 \leq j \leq M)
  • All inputs are integers

Input

N M
V_1 V_2 \ldots V_N
D_1 T_1
D_2 T_2
\vdots
D_M T_M
  • The first line contains N, the number of staff members, and M, the number of jobs, separated by a space.
  • The second line contains the work efficiencies V_1, V_2, \ldots, V_N of each staff member, separated by spaces.
  • The following M lines provide information about each job.
  • The (2 + j)-th line contains the required workload D_j and the time limit T_j of the j-th job, separated by a space.

Output

Output the maximum number of jobs that can be completed, on a single line.


Sample Input 1

3 3
5 3 7
10 2
15 3
6 1

Sample Output 1

2

Sample Input 2

4 5
2 4 6 8
12 2
20 5
8 1
24 4
30 3

Sample Output 2

3

Sample Input 3

6 7
10 20 15 5 25 30
100 5
50 2
200 10
75 3
150 6
300 15
80 4

Sample Output 3

3
E - 山並みの眺望

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 466

問題文

高橋君は山岳地帯をハイキングしています。この地帯には N 個の山が一列に並んでおり、左から i 番目の山の標高は H_i です。

高橋君は各山の山頂に立ち、左方向(インデックスが小さい方向)を眺めます。そして、ある条件を満たす山がいくつ見えるかを数えることを楽しみにしています。

i の山頂から左方向を見たとき、山 jj < i)が 見える とは、以下の条件をともに満たすことをいいます。

  1. H_j > H_i である。すなわち、山 j は山 i より厳密に高い。特に、H_j = H_i の場合は見えるとはみなしません。
  2. j < k < i を満たす全ての整数 k について H_k < H_j が成り立つ。すなわち、山 j と山 i の間にある全ての山の標高が H_j より厳密に低い。

条件2は、山 j と山 i の間に山 j 以上の標高をもつ山がある場合、山 j は遮られて見えなくなることを意味しています。j = i - 1 の場合(山 j と山 i が隣接している場合)は間に山が存在しないため、条件2は自動的に満たされます。

なお、この問題における「見える」の定義は上記の通りであり、現実の視線遮蔽の物理的なモデルとは異なります。

i の山頂から左方向に見える山の数を C_i とします。山 1 からは左方向に山が存在しないため C_1 = 0 です。

i = 1, 2, \ldots, N の全てについて C_i の合計値、すなわち \displaystyle\sum_{i=1}^{N} C_i を求めてください。

制約

  • 1 \leq N \leq 10^6
  • 1 \leq H_i \leq 10^9
  • 入力は全て整数である。
  • 同じ標高の山が複数存在することもある。

入力

N
H_1 H_2 \ldots H_N
  • 1 行目には、山の個数を表す整数 N が与えられる。
  • 2 行目には、各山の標高を表す N 個の整数 H_1, H_2, \ldots, H_N がスペース区切りで与えられる。

出力

\displaystyle\sum_{i=1}^{N} C_i の値を 1 行で出力せよ。


入力例 1

5
3 1 4 1 5

出力例 1

2

入力例 2

5
2 3 1 2 1

出力例 2

4

入力例 3

10
5 3 8 6 7 2 9 1 4 3

出力例 3

9

入力例 4

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

出力例 4

25

入力例 5

1
1000000000

出力例 5

0

Score : 466 pts

Problem Statement

Takahashi is hiking in a mountainous region. In this region, N mountains are lined up in a row, and the elevation of the i-th mountain from the left is H_i.

Takahashi stands at the summit of each mountain and looks to the left (in the direction of decreasing index). He enjoys counting how many mountains satisfying certain conditions are visible.

When looking to the left from the summit of mountain i, mountain j (j < i) is visible if and only if both of the following conditions are satisfied:

  1. H_j > H_i. That is, mountain j is strictly higher than mountain i. In particular, if H_j = H_i, mountain j is not considered visible.
  2. For all integers k satisfying j < k < i, H_k < H_j holds. That is, all mountains between mountain j and mountain i have elevations strictly lower than H_j.

Condition 2 means that if there is a mountain between mountain j and mountain i with elevation greater than or equal to H_j, then mountain j is blocked and not visible. When j = i - 1 (i.e., mountain j and mountain i are adjacent), there are no mountains between them, so condition 2 is automatically satisfied.

Note that the definition of "visible" in this problem is as described above and differs from a physical model of real line-of-sight occlusion.

Let C_i be the number of mountains visible to the left from the summit of mountain i. Since there are no mountains to the left of mountain 1, C_1 = 0.

Find the total of C_i for all i = 1, 2, \ldots, N, that is, \displaystyle\sum_{i=1}^{N} C_i.

Constraints

  • 1 \leq N \leq 10^6
  • 1 \leq H_i \leq 10^9
  • All inputs are integers.
  • There may be multiple mountains with the same elevation.

Input

N
H_1 H_2 \ldots H_N
  • The first line contains an integer N representing the number of mountains.
  • The second line contains N integers H_1, H_2, \ldots, H_N separated by spaces, representing the elevation of each mountain.

Output

Output the value of \displaystyle\sum_{i=1}^{N} C_i on a single line.


Sample Input 1

5
3 1 4 1 5

Sample Output 1

2

Sample Input 2

5
2 3 1 2 1

Sample Output 2

4

Sample Input 3

10
5 3 8 6 7 2 9 1 4 3

Sample Output 3

9

Sample Input 4

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

Sample Output 4

25

Sample Input 5

1
1000000000

Sample Output 5

0