実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 266 点
問題文
高橋君は大学図書館の司書です。この図書館には N 冊の本があり、それぞれの本 i(1 \leq i \leq N)には「難易度」 P_i が設定されています。異なる本が同じ難易度を持つこともありますが、それらはそれぞれ別の本として扱います。
図書館では、学生の学習をサポートするために M 種類の「閲覧許可証」を発行しています。各閲覧許可証 j(1 \leq j \leq M)には「許可レベル」 L_j が設定されており、この許可証を持つ学生は難易度が L_j 以下の本すべてを閲覧することができます。
ある日、青木君がこの図書館にやってきました。青木君は K 枚の閲覧許可証を持っており、その番号は T_1, T_2, \ldots, T_K です。
青木君は、持っているいずれかの閲覧許可証で閲覧できる本をすべて読みたいと考えています。すなわち、K 枚の閲覧許可証それぞれで閲覧できる本の集合の和集合に含まれる本が対象です。同一の本が複数の閲覧許可証によって閲覧可能であっても、その本は 1 冊として数えます。
青木君が閲覧できる本の難易度の総和を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq K \leq M
- 1 \leq P_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq L_j \leq 10^9 (1 \leq j \leq M)
- 1 \leq T_k \leq M (1 \leq k \leq K)
- T_1, T_2, \ldots, T_K はすべて異なる
- 入力はすべて整数
入力
N M K P_1 P_2 \ldots P_N L_1 L_2 \ldots L_M T_1 T_2 \ldots T_K
- 1 行目には、本の冊数 N、閲覧許可証の種類数 M、青木君が持っている閲覧許可証の枚数 K がスペース区切りで与えられる。
- 2 行目には、各本の難易度 P_1, P_2, \ldots, P_N がスペース区切りで与えられる。
- 3 行目には、各閲覧許可証の許可レベル L_1, L_2, \ldots, L_M がスペース区切りで与えられる。
- 4 行目には、青木君が持っている閲覧許可証の番号 T_1, T_2, \ldots, T_K がスペース区切りで与えられる。
出力
青木君が閲覧できる本の難易度の総和を整数として 1 行で出力せよ。
入力例 1
5 3 2 3 7 2 5 4 5 3 7 1 3
出力例 1
21
入力例 2
6 4 3 10 20 30 40 50 60 25 15 45 35 2 3 4
出力例 2
100
入力例 3
10 5 2 100 200 300 400 500 600 700 800 900 1000 150 350 550 750 950 3 5
出力例 3
4500
Score : 266 pts
Problem Statement
Takahashi is a librarian at a university library. The library has N books, and each book i (1 \leq i \leq N) has a "difficulty" P_i assigned to it. Different books may have the same difficulty, but they are treated as separate books.
To support students' learning, the library issues M types of "viewing permits." Each viewing permit j (1 \leq j \leq M) has a "permit level" L_j, and a student holding this permit can view all books with difficulty L_j or less.
One day, Aoki visits this library. Aoki holds K viewing permits, numbered T_1, T_2, \ldots, T_K.
Aoki wants to read all books that can be viewed with any of his viewing permits. That is, the target books are those in the union of the sets of books viewable by each of his K viewing permits. Even if the same book can be viewed by multiple viewing permits, it is counted as one book.
Find the sum of difficulties of all books that Aoki can view.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq K \leq M
- 1 \leq P_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq L_j \leq 10^9 (1 \leq j \leq M)
- 1 \leq T_k \leq M (1 \leq k \leq K)
- T_1, T_2, \ldots, T_K are all distinct
- All input values are integers
Input
N M K P_1 P_2 \ldots P_N L_1 L_2 \ldots L_M T_1 T_2 \ldots T_K
- The first line contains the number of books N, the number of types of viewing permits M, and the number of viewing permits Aoki holds K, separated by spaces.
- The second line contains the difficulties of each book P_1, P_2, \ldots, P_N, separated by spaces.
- The third line contains the permit levels of each viewing permit L_1, L_2, \ldots, L_M, separated by spaces.
- The fourth line contains the numbers of the viewing permits Aoki holds T_1, T_2, \ldots, T_K, separated by spaces.
Output
Output the sum of difficulties of the books Aoki can view as an integer on a single line.
Sample Input 1
5 3 2 3 7 2 5 4 5 3 7 1 3
Sample Output 1
21
Sample Input 2
6 4 3 10 20 30 40 50 60 25 15 45 35 2 3 4
Sample Output 2
100
Sample Input 3
10 5 2 100 200 300 400 500 600 700 800 900 1000 150 350 550 750 950 3 5
Sample Output 3
4500
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 333 点
問題文
高橋君は、大量のログデータを効率よく保存するためのシンプルな圧縮アルゴリズムを実装することにしました。
高橋君が考えた圧縮ルールは以下の通りです:
英小文字からなる文字列 S が与えられたとき、連続する同じ文字の並びを「その文字1つ + 連続した個数」に置き換えます。ただし、連続が 1 個の場合は個数を省略し、文字のみを出力します。
例えば、文字列 aaabbc は以下のように圧縮されます:
aaa→a3(aが 3 個連続)bb→b2(bが 2 個連続)c→c(cが 1 個なので個数は省略)- 結果:
a3b2c
この圧縮方式はランレングス圧縮と呼ばれ、同じ文字が連続して現れるデータに対して効果的です。
高橋君のために、与えられた文字列を上記のルールで圧縮した結果を出力してください。
制約
- 1 \leq |S| \leq 2 \times 10^5
- S は英小文字のみからなる
入力
S
- 1 行目には、英小文字からなる文字列 S が与えられる。
出力
文字列 S を圧縮した結果を 1 行で出力せよ。
入力例 1
aaabbc
出力例 1
a3b2c
入力例 2
abcde
出力例 2
abcde
入力例 3
aaabbbbccdddddeeefghhhh
出力例 3
a3b4c2d5e3fgh4
入力例 4
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbccccccccccddddddddddddeeeeeeeeeeffffffffgggggggggggggghhhhhhhhhiiiiiiiiiiijjjjjjjjkkkkkkkkkkklllllllllllmmmmmmmmmnnnnnnnnnnooooooooooppppppppppqqqqqqqqqqrrrrrrrrrrssssssssssttttttttttuuuuuuuuuuvvvvvvvvvvwwwwwwwwwwxxxxxxxxxxyyyyyyyyyyzzzzzzzzzz
出力例 4
z51a20b15c10d12e10f8g14h9i11j8k11l11m9n10o10p10q10r10s10t10u10v10w10x10y10z10
入力例 5
a
出力例 5
a
Score : 333 pts
Problem Statement
Takahashi decided to implement a simple compression algorithm to efficiently store a large amount of log data.
The compression rules Takahashi devised are as follows:
Given a string S consisting of lowercase English letters, replace each consecutive sequence of the same character with "that character once + the count of consecutive occurrences." However, if the count is 1, omit the count and output only the character.
For example, the string aaabbc is compressed as follows:
aaa→a3(3 consecutivea's)bb→b2(2 consecutiveb's)c→c(only 1c, so the count is omitted)- Result:
a3b2c
This compression method is called run-length encoding, and it is effective for data where the same character appears consecutively.
For Takahashi, please output the result of compressing the given string according to the rules above.
Constraints
- 1 \leq |S| \leq 2 \times 10^5
- S consists only of lowercase English letters
Input
S
- The first line contains a string S consisting of lowercase English letters.
Output
Output the result of compressing the string S in a single line.
Sample Input 1
aaabbc
Sample Output 1
a3b2c
Sample Input 2
abcde
Sample Output 2
abcde
Sample Input 3
aaabbbbccdddddeeefghhhh
Sample Output 3
a3b4c2d5e3fgh4
Sample Input 4
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbccccccccccddddddddddddeeeeeeeeeeffffffffgggggggggggggghhhhhhhhhiiiiiiiiiiijjjjjjjjkkkkkkkkkkklllllllllllmmmmmmmmmnnnnnnnnnnooooooooooppppppppppqqqqqqqqqqrrrrrrrrrrssssssssssttttttttttuuuuuuuuuuvvvvvvvvvvwwwwwwwwwwxxxxxxxxxxyyyyyyyyyyzzzzzzzzzz
Sample Output 4
z51a20b15c10d12e10f8g14h9i11j8k11l11m9n10o10p10q10r10s10t10u10v10w10x10y10z10
Sample Input 5
a
Sample Output 5
a
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 366 点
問題文
高橋君は会社の新規事業担当として、 N 個のプロジェクト案の中から実行するものを選定しようとしています。各プロジェクトは実行するかしないかのどちらかであり、同じプロジェクトを複数回実行することはできません。また、プロジェクトを1つも選ばないことも許されます。
各プロジェクト i (1 \leq i \leq N) を実行するためには C_i 万円の費用が必要であり、実行すると P_i 万円の収益が得られます。ここで、収益と費用はそれぞれ独立した値であり、最大化の対象は収益の合計です。費用は予算の制約にのみ関わり、収益から差し引かれることはありません。
しかし、いくつかのプロジェクト同士は、担当チームや使用設備が競合するため、両方を同時に選ぶことができません。このような競合関係は M 組あり、 j 番目 (1 \leq j \leq M) の競合関係はプロジェクト U_j とプロジェクト V_j の両方を選ぶことができないことを表します。
高橋君に与えられた予算の上限は K 万円です。選んだプロジェクトの費用の合計が K 万円以下であり、かつどの競合関係についても両方のプロジェクトを同時に選ばないようにしたとき、選んだプロジェクトの収益の合計の最大値を求めてください。
すなわち、選んだプロジェクトの集合を S としたとき、以下の条件をすべて満たす範囲で \displaystyle\sum_{i \in S} P_i の最大値を求めてください。
- \displaystyle\sum_{i \in S} C_i \leq K
- すべての j (1 \leq j \leq M) について、 U_j と V_j が同時に S に含まれない(すなわち \{U_j, V_j\} \not\subseteq S)
プロジェクトを1つも選ばない場合( S が空集合の場合)の収益の合計は 0 です。
制約
- 1 \leq N \leq 18
- 0 \leq M \leq \frac{N(N-1)}{2}
- 1 \leq K \leq 10^5
- 1 \leq C_i \leq 10^5 (1 \leq i \leq N)
- 1 \leq P_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq U_j < V_j \leq N (1 \leq j \leq M)
- 同じ競合関係が複数回与えられることはない
- 入力はすべて整数
入力
N M K C_1 P_1 C_2 P_2 \vdots C_N P_N U_1 V_1 U_2 V_2 \vdots U_M V_M
- 1 行目には、プロジェクトの数を表す整数 N 、競合関係の数を表す整数 M 、予算の上限を表す整数 K が、スペース区切りで与えられる。
- 2 行目から N + 1 行目では、各プロジェクトの情報が与えられる。
- 1 + i 行目 (1 \leq i \leq N) には、プロジェクト i を実行するために必要な費用 C_i と、得られる収益 P_i が、スペース区切りで与えられる。
- N + 2 行目から N + M + 1 行目では、競合関係の情報が与えられる。 M = 0 の場合、この部分は存在しない。
- N + 1 + j 行目 (1 \leq j \leq M) には、 j 番目の競合関係におけるプロジェクトの番号 U_j と V_j が、スペース区切りで与えられる。
出力
選んだプロジェクトの収益の合計の最大値を整数で 1 行に出力してください。
入力例 1
3 1 100 50 80 60 90 40 50 1 2
出力例 1
140
入力例 2
5 3 200 30 100 50 150 40 120 60 200 80 180 1 2 2 3 4 5
出力例 2
420
入力例 3
8 6 500 100 500 80 400 120 600 90 450 70 350 110 550 60 300 150 800 1 2 1 3 2 4 3 4 5 6 7 8
出力例 3
2350
Score : 366 pts
Problem Statement
Takahashi, as the person in charge of new business at his company, is trying to select which projects to execute from N project proposals. Each project is either executed or not, and the same project cannot be executed more than once. It is also allowed to select no projects at all.
Executing each project i (1 \leq i \leq N) requires a cost of C_i ten-thousand yen, and yields a profit of P_i ten-thousand yen. Here, profit and cost are independent values, and the objective is to maximize the total profit. Cost is only relevant to the budget constraint and is not subtracted from the profit.
However, some pairs of projects conflict with each other due to competing team assignments or shared equipment, and cannot both be selected at the same time. There are M such conflict relationships, and the j-th (1 \leq j \leq M) conflict relationship indicates that both project U_j and project V_j cannot be selected simultaneously.
Takahashi's budget limit is K ten-thousand yen. Find the maximum total profit of the selected projects such that the total cost of the selected projects is at most K ten-thousand yen and no conflict relationship has both projects selected simultaneously.
In other words, letting S be the set of selected projects, find the maximum value of \displaystyle\sum_{i \in S} P_i subject to all of the following conditions:
- \displaystyle\sum_{i \in S} C_i \leq K
- For all j (1 \leq j \leq M), U_j and V_j are not both contained in S (i.e., \{U_j, V_j\} \not\subseteq S)
If no projects are selected (i.e., S is the empty set), the total profit is 0.
Constraints
- 1 \leq N \leq 18
- 0 \leq M \leq \frac{N(N-1)}{2}
- 1 \leq K \leq 10^5
- 1 \leq C_i \leq 10^5 (1 \leq i \leq N)
- 1 \leq P_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq U_j < V_j \leq N (1 \leq j \leq M)
- The same conflict relationship is not given more than once
- All inputs are integers
Input
N M K C_1 P_1 C_2 P_2 \vdots C_N P_N U_1 V_1 U_2 V_2 \vdots U_M V_M
- The first line contains three space-separated integers: N representing the number of projects, M representing the number of conflict relationships, and K representing the budget limit.
- From the 2nd line to the (N + 1)-th line, information about each project is given.
- The (1 + i)-th line (1 \leq i \leq N) contains the cost C_i required to execute project i and the profit P_i obtained, separated by a space.
- From the (N + 2)-th line to the (N + M + 1)-th line, information about the conflict relationships is given. If M = 0, this part does not exist.
- The (N + 1 + j)-th line (1 \leq j \leq M) contains the project numbers U_j and V_j involved in the j-th conflict relationship, separated by a space.
Output
Output the maximum total profit of the selected projects as an integer on a single line.
Sample Input 1
3 1 100 50 80 60 90 40 50 1 2
Sample Output 1
140
Sample Input 2
5 3 200 30 100 50 150 40 120 60 200 80 180 1 2 2 3 4 5
Sample Output 2
420
Sample Input 3
8 6 500 100 500 80 400 120 600 90 450 70 350 110 550 60 300 150 800 1 2 1 3 2 4 3 4 5 6 7 8
Sample Output 3
2350
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
高橋君は N 個の地点と M 本の道路からなる街に住んでいます。各地点には 1 から N までの番号が付けられています。道路は双方向に移動可能で、i 番目の道路は地点 u_i と地点 v_i を結び、通過するのに w_i の時間がかかります。同じ 2 地点間に複数の道路が存在することもあります。
ある日、大規模な災害が発生し、高橋君は自宅のある地点 1 から避難所のある地点 N へ避難することになりました。街の K 個の地点が冠水しており、冠水している地点の番号は g_1, g_2, \ldots, g_K です(K = 0 のときは冠水地点はありません)。
高橋君は地点 1 から出発し、道路をたどって地点 N に到達します。高橋君の経路は、地点の列と使用する道路の列の組として表されます。具体的には、地点の列 p_0, p_1, p_2, \ldots, p_L(L \geq 1)と、各 i (1 \leq i \leq L) に対して地点 p_{i-1} と地点 p_i を直接結ぶ道路 e_i の選択からなります。ここで p_0 = 1, p_L = N です。同じ地点を複数回通ることも許されます。
高橋君がこの経路で移動したときの合計時間は、次の 2 つの値の和として定義されます。
- 移動時間:経路上で通過した各道路の所要時間の総和。すなわち \displaystyle\sum_{i=1}^{L} w_{e_i}(w_{e_i} は道路 e_i の所要時間)です。
- 冠水ペナルティ:地点列 p_0, p_1, \ldots, p_L の各要素について、その地点が冠水地点であれば T の追加時間が発生します。出発地点 p_0 や目的地点 p_L も対象です。同じ冠水地点を複数回訪れた場合は、地点列に現れた回数だけ追加時間 T が加算されます。これらの総和です。
高橋君が地点 1 から地点 N まで移動するのにかかる合計時間の最小値を求めてください。なお、地点 1 から地点 N へ到達可能であることは保証されます。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 0 \leq K \leq N
- 0 \leq T \leq 10^9
- 1 \leq u_i, v_i \leq N (1 \leq i \leq M)
- u_i \neq v_i (1 \leq i \leq M)
- 1 \leq w_i \leq 10^9 (1 \leq i \leq M)
- 同じ 2 地点間に複数の道路が存在することがある
- 1 \leq g_j \leq N (1 \leq j \leq K)
- g_j はすべて異なる
- 地点 1 から地点 N へ到達可能である
- 入力はすべて整数である
入力
N M K T u_1 v_1 w_1 u_2 v_2 w_2 : u_M v_M w_M g_1 g_2 \ldots g_K
- 1 行目には、地点の数を表す N 、道路の数を表す M 、冠水している地点の数を表す K 、冠水地点を訪れるたびに発生する追加時間を表す T が、スペース区切りで与えられる。
- 2 行目から M + 1 行目では、道路の情報が M 行で与えられる。
- 1 + i 行目では、 i 番目の道路が結ぶ 2 つの地点の番号 u_i , v_i と、その道路を通過するのにかかる時間 w_i が、スペース区切りで与えられる。
- K \geq 1 の場合、M + 2 行目には、冠水している地点の番号 g_1, g_2, \ldots, g_K が、スペース区切りで与えられる。K = 0 の場合、この行は与えられない(入力は M + 1 行目で終了する)。
出力
高橋君が地点 1 から地点 N まで移動するのにかかる合計時間の最小値を 1 行で出力せよ。
入力例 1
4 4 1 10 1 2 3 2 4 4 1 3 5 3 4 6 2
出力例 1
11
入力例 2
6 7 3 5 1 2 5 1 3 1 2 5 2 3 5 1 2 4 4 4 6 3 5 6 3 1 3 6
出力例 2
20
入力例 3
8 10 0 100 1 2 2 1 3 10 2 5 3 3 4 5 4 6 2 5 7 4 6 7 8 7 8 1 3 5 6 6 8 12
出力例 3
10
Score : 400 pts
Problem Statement
Takahashi lives in a city consisting of N locations and M roads. Each location is numbered from 1 to N. The roads are bidirectional, and the i-th road connects location u_i and location v_i, taking w_i time to traverse. There may be multiple roads between the same pair of locations.
One day, a large-scale disaster occurs, and Takahashi must evacuate from location 1 (his home) to location N (the evacuation shelter). K locations in the city are flooded, and the flooded locations are numbered g_1, g_2, \ldots, g_K (when K = 0, there are no flooded locations).
Takahashi starts at location 1 and travels along roads to reach location N. His route is represented as a pair of a sequence of locations and a sequence of roads used. Specifically, it consists of a sequence of locations p_0, p_1, p_2, \ldots, p_L (L \geq 1) and, for each i (1 \leq i \leq L), a choice of road e_i that directly connects location p_{i-1} and location p_i. Here, p_0 = 1 and p_L = N. Visiting the same location multiple times is allowed.
The total time when Takahashi travels along this route is defined as the sum of the following two values:
- Travel time: The sum of the traversal times of all roads used along the route. That is, \displaystyle\sum_{i=1}^{L} w_{e_i} (where w_{e_i} is the traversal time of road e_i).
- Flooding penalty: For each element in the location sequence p_0, p_1, \ldots, p_L, if that location is a flooded location, an additional time of T is incurred. This applies to the starting location p_0 and the destination p_L as well. If the same flooded location is visited multiple times, the additional time T is added for each occurrence in the location sequence. The flooding penalty is the sum of all such additions.
Find the minimum total time for Takahashi to travel from location 1 to location N. It is guaranteed that location N is reachable from location 1.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 0 \leq K \leq N
- 0 \leq T \leq 10^9
- 1 \leq u_i, v_i \leq N (1 \leq i \leq M)
- u_i \neq v_i (1 \leq i \leq M)
- 1 \leq w_i \leq 10^9 (1 \leq i \leq M)
- There may be multiple roads between the same pair of locations
- 1 \leq g_j \leq N (1 \leq j \leq K)
- All g_j are distinct
- Location N is reachable from location 1
- All input values are integers
Input
N M K T u_1 v_1 w_1 u_2 v_2 w_2 : u_M v_M w_M g_1 g_2 \ldots g_K
- The first line contains N (the number of locations), M (the number of roads), K (the number of flooded locations), and T (the additional time incurred each time a flooded location is visited), separated by spaces.
- From the 2nd line to the (M + 1)-th line, road information is given over M lines.
- The (1 + i)-th line contains the numbers u_i, v_i of the two locations connected by the i-th road, and the time w_i required to traverse that road, separated by spaces.
- If K \geq 1, the (M + 2)-th line contains the numbers of the flooded locations g_1, g_2, \ldots, g_K, separated by spaces. If K = 0, this line is not given (the input ends at the (M + 1)-th line).
Output
Output in one line the minimum total time for Takahashi to travel from location 1 to location N.
Sample Input 1
4 4 1 10 1 2 3 2 4 4 1 3 5 3 4 6 2
Sample Output 1
11
Sample Input 2
6 7 3 5 1 2 5 1 3 1 2 5 2 3 5 1 2 4 4 4 6 3 5 6 3 1 3 6
Sample Output 2
20
Sample Input 3
8 10 0 100 1 2 2 1 3 10 2 5 3 3 4 5 4 6 2 5 7 4 6 7 8 7 8 1 3 5 6 6 8 12
Sample Output 3
10
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 466 点
問題文
高橋君は図書館の司書です。図書館には N 個の棚が一列に並んでおり、左から順に 1, 2, \ldots, N と番号が付けられています。
ある日、M 冊の本を棚に戻す作業を行います。本を戻す順番は決まっており、1 番目、2 番目、\ldots、M 番目の順に一冊ずつ棚に戻します。i 番目 (1 \leq i \leq M) に戻す本の、戻すべき棚の番号は G_i です。なお、同じ棚に複数の本が戻されることもあります。
高橋君は i 番目 (1 \leq i \leq M) の本を棚に戻すたびに、以下のように定義される値 L_i を記録します。
- L_i:i+1 番目、i+2 番目、\ldots、M 番目に戻す予定の M - i 冊の本(すなわち、i 番目の本を戻した時点でまだ棚に戻していない本すべて)のうち、戻すべき棚の番号が G_i 未満であるものの冊数。i = M のとき、該当する本は 0 冊であるため L_M = 0 です。
高橋君は一日の作業終了後、記録した値の合計 L_1 + L_2 + \cdots + L_M を報告書に書かなければなりません。
棚の数 N、本の数 M、および各本を戻すべき棚の番号 G_i が与えられるとき、L_1 + L_2 + \cdots + L_M の値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq G_i \leq N (1 \leq i \leq M)
- 入力はすべて整数である
入力
N M G_1 G_2 \vdots G_M
- 1 行目には、棚の数を表す整数 N と本の数を表す整数 M が、スペース区切りで与えられる。
- 続く M 行のうち i 行目 (1 \leq i \leq M) には、i 番目に戻す本の、戻すべき棚の番号 G_i が与えられる。
出力
L_1 + L_2 + \cdots + L_M の値を 1 行で出力せよ。
入力例 1
5 4 3 1 4 2
出力例 1
3
入力例 2
3 5 2 3 1 2 1
出力例 2
6
入力例 3
10 8 5 3 8 1 7 2 6 4
出力例 3
15
入力例 4
100 12 10 9 8 7 6 5 4 3 2 1 1 1
出力例 4
63
入力例 5
1 1 1
出力例 5
0
Score : 466 pts
Problem Statement
Takahashi is a librarian at a library. The library has N shelves arranged in a row, numbered 1, 2, \ldots, N from left to right.
One day, he performs the task of returning M books to the shelves. The order of returning books is fixed: he returns them one by one in order — the 1st book, the 2nd book, \ldots, the Mth book. The shelf number to which the i-th (1 \leq i \leq M) book should be returned is G_i. Note that multiple books may be returned to the same shelf.
Each time Takahashi returns the i-th (1 \leq i \leq M) book to its shelf, he records a value L_i defined as follows:
- L_i: Among the M - i books scheduled to be returned as the (i+1)-th, (i+2)-th, \ldots, M-th books (that is, all books that have not yet been returned to the shelves at the moment the i-th book is returned), the number of books whose destination shelf number is less than G_i. When i = M, there are 0 such books, so L_M = 0.
After the day's work is finished, Takahashi must write the total of the recorded values L_1 + L_2 + \cdots + L_M in his report.
Given the number of shelves N, the number of books M, and the destination shelf number G_i for each book, find the value of L_1 + L_2 + \cdots + L_M.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq G_i \leq N (1 \leq i \leq M)
- All input values are integers
Input
N M G_1 G_2 \vdots G_M
- The first line contains an integer N representing the number of shelves and an integer M representing the number of books, separated by a space.
- The i-th (1 \leq i \leq M) of the following M lines contains G_i, the destination shelf number for the i-th book to be returned.
Output
Output the value of L_1 + L_2 + \cdots + L_M on a single line.
Sample Input 1
5 4 3 1 4 2
Sample Output 1
3
Sample Input 2
3 5 2 3 1 2 1
Sample Output 2
6
Sample Input 3
10 8 5 3 8 1 7 2 6 4
Sample Output 3
15
Sample Input 4
100 12 10 9 8 7 6 5 4 3 2 1 1 1
Sample Output 4
63
Sample Input 5
1 1 1
Sample Output 5
0