Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 266 点
問題文
高橋君はショッピングモールのポイントカードシステムを管理しています。このモールには N 人の会員がおり、会員 i (1 \leq i \leq N) の初期ポイント残高は S_i です。
これから M 件のポイント調整処理を 1 番目から M 番目まで順番に 1 つずつ行います。 j 番目 (1 \leq j \leq M) の処理では、会員 T_j のその時点でのポイント残高に V_j を加算し、その結果と 0 の大きい方を新しいポイント残高とします。すなわち、処理前の会員 T_j のポイント残高が P であるとき、処理後のポイント残高は \max(P + V_j,\ 0) となります。各処理は次の処理が行われる前に完了します。なお、同一の会員に対して複数回の処理が行われることもあります。
ポイント残高に上限はありません。一方、上記のルールにより、各処理の後のポイント残高は常に 0 以上です。
すべてのポイント調整処理を順番に実行した後の、各会員のポイント残高を求めてください。
制約
- 1 \leq N \leq 10^5
- 1 \leq M \leq 10^5
- 0 \leq S_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq T_j \leq N (1 \leq j \leq M)
- -10^9 \leq V_j \leq 10^9 (1 \leq j \leq M)
- 入力はすべて整数である
- 出力すべき値はすべて 2^{63}-1 以下であることが保証される(符号付き 64 bit 整数の範囲に収まる)
入力
N M S_1 S_2 \cdots S_N T_1 V_1 T_2 V_2 \vdots T_M V_M
- 1 行目には、会員の人数を表す整数 N と、ポイント調整処理の件数を表す整数 M が、スペース区切りで与えられる。
- 2 行目には、各会員の初期ポイント残高を表す整数 S_1, S_2, \ldots, S_N が、スペース区切りで与えられる。
- S_i は会員 i の初期ポイント残高を表す。
- 続く M 行のうち j 行目 (1 \leq j \leq M) には、 j 番目の処理で対象となる会員の番号を表す整数 T_j と、加算するポイント数を表す整数 V_j が、スペース区切りで与えられる。
出力
すべてのポイント調整処理を実行した後の、各会員のポイント残高を会員 1, 会員 2, \ldots, 会員 N の順にスペース区切りで 1 行に出力せよ。末尾に改行を出力すること。
入力例 1
3 4 100 200 300 1 50 2 -150 3 -400 1 -200
出力例 1
0 50 0
入力例 2
5 6 0 500 1000 750 300 3 -500 1 100 2 -600 4 250 5 -300 2 200
出力例 2
100 200 500 1000 0
入力例 3
10 12 1000000000 0 500000000 999999999 123456789 0 777777777 100000000 999999999 1 1 -1000000000 2 1000000000 3 -500000001 4 1 5 -123456790 6 0 7 -777777777 8 900000000 9 -1000000000 10 999999999 10 -500000000 3 1000000000
出力例 3
0 1000000000 1000000000 1000000000 0 0 0 1000000000 0 500000000
Score : 266 pts
Problem Statement
Takahashi manages the point card system of a shopping mall. The mall has N members, and member i (1 \leq i \leq N) has an initial point balance of S_i.
From now on, M point adjustment operations will be performed one by one in order from the 1-st to the M-th. In the j-th operation (1 \leq j \leq M), the value V_j is added to the current point balance of member T_j, and the new point balance is set to the maximum of the result and 0. That is, if member T_j's point balance before the operation is P, then the point balance after the operation becomes \max(P + V_j,\ 0). Each operation is completed before the next operation begins. Note that the same member may be subject to multiple operations.
There is no upper limit on the point balance. On the other hand, by the rule above, the point balance after each operation is always 0 or greater.
Determine the point balance of each member after all point adjustment operations have been performed in order.
Constraints
- 1 \leq N \leq 10^5
- 1 \leq M \leq 10^5
- 0 \leq S_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq T_j \leq N (1 \leq j \leq M)
- -10^9 \leq V_j \leq 10^9 (1 \leq j \leq M)
- All input values are integers.
- It is guaranteed that all values to be output are at most 2^{63}-1 (they fit within a signed 64-bit integer).
Input
N M S_1 S_2 \cdots S_N T_1 V_1 T_2 V_2 \vdots T_M V_M
- The first line contains an integer N representing the number of members and an integer M representing the number of point adjustment operations, separated by a space.
- The second line contains integers S_1, S_2, \ldots, S_N representing the initial point balances of each member, separated by spaces.
- S_i represents the initial point balance of member i.
- In the following M lines, the j-th line (1 \leq j \leq M) contains an integer T_j representing the member number targeted by the j-th operation and an integer V_j representing the number of points to add, separated by a space.
Output
Print the point balance of each member after all point adjustment operations have been performed, in the order of member 1, member 2, \ldots, member N, separated by spaces, on a single line. Output a newline at the end.
Sample Input 1
3 4 100 200 300 1 50 2 -150 3 -400 1 -200
Sample Output 1
0 50 0
Sample Input 2
5 6 0 500 1000 750 300 3 -500 1 100 2 -600 4 250 5 -300 2 200
Sample Output 2
100 200 500 1000 0
Sample Input 3
10 12 1000000000 0 500000000 999999999 123456789 0 777777777 100000000 999999999 1 1 -1000000000 2 1000000000 3 -500000001 4 1 5 -123456790 6 0 7 -777777777 8 900000000 9 -1000000000 10 999999999 10 -500000000 3 1000000000
Sample Output 3
0 1000000000 1000000000 1000000000 0 0 0 1000000000 0 500000000
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
高橋君は花壇の整備を行っています。花壇には N 本の花が一列に並んでおり、左から i 番目の花の品種は整数 S_i で表されます。同じ整数は同じ品種を、異なる整数は異なる品種を表します。
高橋君は、同じ品種の花が K 本以上連続して並んでいる部分を「見栄えの良い区間」と呼んでいます。
より正確には、整数の組 (l, r) が以下のすべてを満たすとき、左から l 番目の花から r 番目の花までの連続する並びを見栄えの良い区間と呼びます。
- 1 \leq l \leq r \leq N
- S_l = S_{l+1} = \cdots = S_r
- r - l + 1 \geq K
高橋君は、各花について、その花を含む見栄えの良い区間が少なくとも1つ存在するならばその花を残し、そのような区間が1つも存在しないならばその花を抜き取ることにしました。
すなわち、i 番目の花を残すのは、1 \leq l \leq i \leq r \leq N かつ上の条件を満たす組 (l, r) が存在するときです。そのような (l, r) が存在しない花は抜き取ります。
抜き取る花をすべて除去した後、残った花の品種を元の並び順を保ったまま並べた列を求めてください。すべての花が抜き取られ、1本も残らない場合もあり得ます。
制約
- 1 \leq N \leq 10^6
- 1 \leq K \leq N
- 1 \leq S_i \leq 10^9 (1 \leq i \leq N)
- 入力はすべて整数である。
入力
N K S_1 S_2 \ldots S_N
- 1 行目には、花の本数 N と、見栄えの良い区間に必要な最小の長さ K が、スペース区切りで与えられる。
- 2 行目には、各花の品種を表す整数 S_1, S_2, \ldots, S_N が、スペース区切りで与えられる。
出力
抜き取る花を除去した後に残った花の品種を、元の並び順を保ったままスペース区切りで 1 行で出力せよ。末尾には改行を出力すること。花が1本も残らない場合は、空の行(改行のみ)を出力せよ。
入力例 1
10 3 1 2 2 2 3 1 1 1 1 2
出力例 1
2 2 2 1 1 1 1
入力例 2
5 3 1 2 1 2 1
出力例 2
入力例 3
15 2 3 3 1 4 4 4 2 1 1 5 5 5 5 3 2
出力例 3
3 3 4 4 4 1 1 5 5 5 5
入力例 4
20 4 1 1 1 1 2 2 2 3 3 3 3 3 1 2 2 2 2 2 1 1
出力例 4
1 1 1 1 3 3 3 3 3 2 2 2 2 2
入力例 5
1 1 42
出力例 5
42
Score : 300 pts
Problem Statement
Takahashi is maintaining a flower bed. The flower bed contains N flowers arranged in a row, and the variety of the i-th flower from the left is represented by an integer S_i. The same integer represents the same variety, and different integers represent different varieties.
Takahashi calls a section where K or more consecutive flowers of the same variety are lined up a "beautiful interval."
More precisely, a continuous sequence from the l-th flower to the r-th flower from the left is called a beautiful interval when the pair of integers (l, r) satisfies all of the following conditions:
- 1 \leq l \leq r \leq N
- S_l = S_{l+1} = \cdots = S_r
- r - l + 1 \geq K
Takahashi decided that for each flower, if there exists at least one beautiful interval containing that flower, he will keep it; otherwise, he will remove it.
That is, the i-th flower is kept if there exists a pair (l, r) satisfying the above conditions with 1 \leq l \leq i \leq r \leq N. Flowers for which no such (l, r) exists are removed.
After removing all the flowers to be taken out, determine the sequence of varieties of the remaining flowers, listed in their original order. It is possible that all flowers are removed and none remain.
Constraints
- 1 \leq N \leq 10^6
- 1 \leq K \leq N
- 1 \leq S_i \leq 10^9 (1 \leq i \leq N)
- All input values are integers.
Input
N K S_1 S_2 \ldots S_N
- The first line contains the number of flowers N and the minimum length K required for a beautiful interval, separated by a space.
- The second line contains the integers S_1, S_2, \ldots, S_N representing the variety of each flower, separated by spaces.
Output
Output the varieties of the remaining flowers after removing the flowers to be taken out, in their original order, separated by spaces, on a single line. A trailing newline should be output. If no flowers remain, output an empty line (only a newline).
Sample Input 1
10 3 1 2 2 2 3 1 1 1 1 2
Sample Output 1
2 2 2 1 1 1 1
Sample Input 2
5 3 1 2 1 2 1
Sample Output 2
Sample Input 3
15 2 3 3 1 4 4 4 2 1 1 5 5 5 5 3 2
Sample Output 3
3 3 4 4 4 1 1 5 5 5 5
Sample Input 4
20 4 1 1 1 1 2 2 2 3 3 3 3 3 1 2 2 2 2 2 1 1
Sample Output 4
1 1 1 1 3 3 3 3 3 2 2 2 2 2
Sample Input 5
1 1 42
Sample Output 5
42
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 366 点
問題文
高橋君は冒険者として、古代遺跡の宝物庫に潜入しました。宝物庫には N 個の宝物が置かれており、高橋君はこれらの宝物をナップサックに詰めて持ち帰ろうとしています。
各宝物 i(1 \leq i \leq N)には、重さ w_i キログラムと価値 v_i が定められています。
高橋君のナップサックには、合計で最大 W キログラムまでの宝物を入れることができます。高橋君は N 個の宝物それぞれについて、ナップサックに入れるか入れないかを選びます。各宝物は最大 1 個しかなく、宝物を分割して一部だけ入れることもできません。
高橋君は、選んだ宝物の重さの合計が W キログラム以下となる範囲で、選んだ宝物の価値の合計を最大化したいと考えています。
価値の合計の最大値を求めてください。
制約
- 1 \leq N \leq 100
- 1 \leq W \leq 10^5
- 1 \leq w_i \leq W
- 1 \leq v_i \leq 10^9
- 入力はすべて整数
入力
N W w_1 v_1 w_2 v_2 \vdots w_N v_N
- 1 行目には、宝物の個数 N とナップサックの容量 W が、スペース区切りで与えられる。
- 続く N 行の i 番目(1 \leq i \leq N)には、宝物 i の重さ w_i と価値 v_i が、スペース区切りで与えられる。
出力
選んだ宝物の価値の合計の最大値を 1 行で出力してください。
入力例 1
3 8 3 30 4 50 5 60
出力例 1
90
入力例 2
5 15 2 10 3 20 5 30 7 40 9 50
出力例 2
90
入力例 3
10 50 12 150 8 90 15 200 6 80 20 250 3 40 10 120 18 220 5 60 25 300
出力例 3
640
Score : 366 pts
Problem Statement
Takahashi, as an adventurer, has infiltrated the treasure vault of ancient ruins. There are N treasures in the vault, and Takahashi is trying to pack them into his knapsack to bring them home.
Each treasure i (1 \leq i \leq N) has a weight of w_i kilograms and a value of v_i.
Takahashi's knapsack can hold treasures with a total weight of at most W kilograms. For each of the N treasures, Takahashi chooses whether or not to put it in the knapsack. There is at most one of each treasure, and it is not possible to split a treasure and put only a part of it in the knapsack.
Takahashi wants to maximize the total value of the chosen treasures, subject to the constraint that the total weight of the chosen treasures is at most W kilograms.
Find the maximum possible total value.
Constraints
- 1 \leq N \leq 100
- 1 \leq W \leq 10^5
- 1 \leq w_i \leq W
- 1 \leq v_i \leq 10^9
- All inputs are integers.
Input
N W w_1 v_1 w_2 v_2 \vdots w_N v_N
- The first line contains the number of treasures N and the knapsack capacity W, separated by a space.
- The i-th of the following N lines (1 \leq i \leq N) contains the weight w_i and value v_i of treasure i, separated by a space.
Output
Print the maximum possible total value of the chosen treasures on a single line.
Sample Input 1
3 8 3 30 4 50 5 60
Sample Output 1
90
Sample Input 2
5 15 2 10 3 20 5 30 7 40 9 50
Sample Output 2
90
Sample Input 3
10 50 12 150 8 90 15 200 6 80 20 250 3 40 10 120 18 220 5 60 25 300
Sample Output 3
640
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋君は配送センターで働いており、倉庫から N 軒の届け先に荷物を配送しなければなりません。
倉庫と全ての届け先は一本の数直線上にあります。倉庫は座標 0 の位置にあり、i 番目の届け先は座標 X_i の位置にあります(X_i は正または負の値をとりえます)。全ての届け先の座標は互いに異なり、倉庫の位置(座標 0)とも異なります。i 番目の届け先にはちょうど D_i 個の荷物を届ける必要があります。倉庫には最初から全ての届け先分の荷物、すなわち合計 \sum_{i=1}^{N} D_i 個の荷物が用意されています。
高橋君が使う台車には容量の制限があり、一度に最大 K 個の荷物しか積むことができません。高橋君は以下の手順を、全ての届け先に必要な個数の荷物を届け終わるまで繰り返します。
- 荷物を積む: 倉庫(座標 0)で台車に荷物を 1 個以上 K 個以下の好きな個数だけ積む。ただし、全ての届け先に対する未配送の荷物の残り個数の合計を超えて積むことはできない。
- 配送する: 倉庫を出発し、数直線上を自由に移動して、台車に積んだ荷物を好きな届け先に好きな個数ずつ届ける。1回の配送で複数の届け先を回ることができ、正の方向と負の方向の届け先を混在して訪れることもできる。例えば、まず正の方向に進んでいくつかの届け先に届け、引き返して負の方向の届け先に届ける、といった経路も可能である。各届け先に届ける個数は、その届け先の未配送の残り個数以下でなければならない。台車に積んだ荷物を全て届けきる必要はなく、届けなかった荷物は倉庫に持ち帰り、次回以降の配送で改めて使うことができる。なお、配送の途中で倉庫(座標 0)を通過しても、追加で荷物を積むことはできない。
- 倉庫に戻る: 倉庫(座標 0)に戻る。
各届け先への配送は複数回に分けて行うことができます。例えば、5 個の荷物を届ける必要がある届け先に、ある回で 3 個、別の回で 2 個届けても構いません。
1回の配送における移動距離は、倉庫を出発してから倉庫に戻るまでに高橋君が実際に移動した距離の合計です。座標 a から座標 b へ移動するときの距離は |a - b| です。例えば、倉庫(座標 0)から座標 5 まで進み、引き返して座標 -3 まで進み、倉庫(座標 0)に戻った場合、移動距離は 5 + 8 + 3 = 16 です。
高橋君は、全ての荷物を届け終え、最終的に倉庫(座標 0)に戻ってくるまでの、全ての配送を通じた移動距離の合計をできるだけ短くしたいと考えています。
移動距離の合計の最小値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq 10^9
- -10^9 \leq X_i \leq 10^9
- X_i \neq 0
- X_i はすべて異なる
- 1 \leq D_i \leq 10^9
- \sum_{i=1}^{N} D_i \leq 10^9
- 入力はすべて整数である
入力
N K X_1 D_1 X_2 D_2 \vdots X_N D_N
1 行目には、届け先の軒数 N と、台車に一度に積める荷物の最大個数 K がスペース区切りで与えられる。続く N 行のうち i 行目には、i 番目の届け先の座標 X_i と、その届け先に届ける必要がある荷物の個数 D_i がスペース区切りで与えられる。
出力
全ての荷物を届け終えて倉庫(座標 0)に戻るまでの、移動距離の合計の最小値を 1 行で出力せよ。
入力例 1
3 3 2 2 -4 1 5 2
出力例 1
22
入力例 2
2 2 -3 3 -1 1
出力例 2
12
入力例 3
8 5 -10 4 7 3 -2 6 15 2 4 5 -6 1 12 7 -14 3
出力例 3
128
入力例 4
20 7 -100 8 35 3 -12 15 80 6 5 9 -55 4 120 11 -3 2 60 7 -200 5 18 13 -75 10 150 1 -30 6 95 14 -140 3 42 8 -8 12 210 4 -180 9
出力例 4
3366
入力例 5
1 1000000000 -1000000000 1000000000
出力例 5
2000000000
Score : 400 pts
Problem Statement
Takahashi works at a delivery center and must deliver packages from a warehouse to N destinations.
The warehouse and all destinations are located on a single number line. The warehouse is at coordinate 0, and the i-th destination is at coordinate X_i (X_i can be positive or negative). All destination coordinates are distinct from each other and also distinct from the warehouse position (coordinate 0). Exactly D_i packages must be delivered to the i-th destination. The warehouse initially has all packages for all destinations, totaling \sum_{i=1}^{N} D_i packages.
The cart Takahashi uses has a capacity limit and can carry at most K packages at a time. Takahashi repeats the following procedure until the required number of packages has been delivered to all destinations:
- Load packages: At the warehouse (coordinate 0), load any number of packages from 1 to K onto the cart. However, he cannot load more than the total remaining number of undelivered packages across all destinations.
- Deliver: Depart from the warehouse and move freely along the number line, delivering packages from the cart to any destinations in any quantities. Multiple destinations can be visited in a single delivery trip, and destinations in both the positive and negative directions can be visited in the same trip. For example, a route where he first goes in the positive direction to deliver to some destinations, then turns back to deliver to destinations in the negative direction, is possible. The number of packages delivered to each destination must not exceed the remaining undelivered count for that destination. It is not necessary to deliver all packages loaded on the cart; undelivered packages are brought back to the warehouse and can be used in subsequent deliveries. Note that even if he passes through the warehouse (coordinate 0) during a delivery trip, he cannot load additional packages.
- Return to warehouse: Return to the warehouse (coordinate 0).
Deliveries to each destination can be split across multiple trips. For example, if 5 packages need to be delivered to a destination, it is acceptable to deliver 3 in one trip and 2 in another.
The travel distance of a single delivery trip is the total distance Takahashi actually travels from departing the warehouse until returning to the warehouse. The distance of moving from coordinate a to coordinate b is |a - b|. For example, if he goes from the warehouse (coordinate 0) to coordinate 5, turns back to coordinate -3, and returns to the warehouse (coordinate 0), the travel distance is 5 + 8 + 3 = 16.
Takahashi wants to minimize the total travel distance across all delivery trips until all packages are delivered and he has returned to the warehouse (coordinate 0).
Find the minimum total travel distance.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq 10^9
- -10^9 \leq X_i \leq 10^9
- X_i \neq 0
- All X_i are distinct
- 1 \leq D_i \leq 10^9
- \sum_{i=1}^{N} D_i \leq 10^9
- All inputs are integers
Input
N K X_1 D_1 X_2 D_2 \vdots X_N D_N
The first line contains the number of destinations N and the maximum number of packages K that can be loaded on the cart at once, separated by a space. The i-th of the following N lines contains the coordinate X_i of the i-th destination and the number of packages D_i that need to be delivered there, separated by a space.
Output
Print in one line the minimum total travel distance until all packages have been delivered and Takahashi has returned to the warehouse (coordinate 0).
Sample Input 1
3 3 2 2 -4 1 5 2
Sample Output 1
22
Sample Input 2
2 2 -3 3 -1 1
Sample Output 2
12
Sample Input 3
8 5 -10 4 7 3 -2 6 15 2 4 5 -6 1 12 7 -14 3
Sample Output 3
128
Sample Input 4
20 7 -100 8 35 3 -12 15 80 6 5 9 -55 4 120 11 -3 2 60 7 -200 5 18 13 -75 10 150 1 -30 6 95 14 -140 3 42 8 -8 12 210 4 -180 9
Sample Output 4
3366
Sample Input 5
1 1000000000 -1000000000 1000000000
Sample Output 5
2000000000
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 433 点
問題文
高橋君は、社内で使う連絡先リストのシステムを開発しています。社員はそれぞれ固有のユーザー名を持っていますが、ユーザー名が長いため、各社員に略称を割り当てる機能を実装することになりました。
N 人の社員がおり、i 番目の社員のユーザー名は英小文字からなる文字列 S_i です。各社員 i に対して、そのユーザー名 S_i の空でない接頭辞(prefix)をちょうど 1 つ選び、それを略称 T_i として割り当てます。すなわち、T_i は S_i の先頭から 1 文字以上 |S_i| 文字以下を取った文字列でなければなりません。ユーザー名全体を略称とすることも許されます。
このとき、略称の割り当て (T_1, T_2, \ldots, T_N) は以下の接頭辞フリー条件を満たす必要があります。
- 接頭辞フリー条件:どの異なる 2 人の社員 i, j(i \neq j)についても、T_i が T_j の接頭辞でなく、かつ T_j が T_i の接頭辞でもない。
ここで、文字列 A が文字列 B の接頭辞であるとは、|A| \leq |B| であり、かつ B の先頭 |A| 文字が A と一致することをいいます。特に、A = B のときも A は B の接頭辞です。
したがって、接頭辞フリー条件から、異なる社員 i, j(i \neq j)に対して T_i = T_j となることも許されません。
高橋君は、条件を満たす略称の割り当てが存在するならば、略称の長さの合計 \sum_{i=1}^{N} |T_i| をできるだけ小さくしたいと考えています。条件を満たす割り当てが存在する場合はその合計の最小値を、存在しない場合は -1 を出力してください。
制約
- 1 \leq N \leq 2 \times 10^5
- S_i は英小文字からなる文字列である。
- 1 \leq |S_i| \leq 5 \times 10^5
- i \neq j ならば S_i \neq S_j(すべてのユーザー名は互いに異なる)。
- \sum_{i=1}^{N} |S_i| \leq 5 \times 10^5
入力
N S_1 S_2 \vdots S_N
1 行目には、社員の人数を表す整数 N が与えられる。続く N 行のうち i 行目には、i 番目の社員のユーザー名を表す文字列 S_i が与えられる。
出力
条件を満たす略称の割り当てが存在する場合は、略称の長さの合計の最小値を 1 行で出力せよ。存在しない場合は -1 を出力せよ。
入力例 1
3 alice bob carol
出力例 1
3
入力例 2
2 a ab
出力例 2
-1
入力例 3
12 apple apricot banana bandit banner candle canary cattle dog dove duck zebra
出力例 3
38
入力例 4
40 aaaaaa aaaaab aaaaac aaaaad aaaaae aaaaba aaaabb aaaabc aaaabd aaaabe aaabaa aaabab aaabac aaabad aaabae aaabba aaabbb aaabbc aaabbd aaabbe aabaaa aabaab aabaac aabaad aabaae aababa aababb aababc aababd aababe abaaaa abaaab abaaac abaaad abaaaae ababaa ababab ababac ababad ababae
出力例 4
-1
入力例 5
1 a
出力例 5
1
Score : 433 pts
Problem Statement
Takahashi is developing a contact list system for internal use at his company. Each employee has a unique username, but since usernames are long, he decided to implement a feature that assigns an abbreviation to each employee.
There are N employees, and the username of the i-th employee is a string S_i consisting of lowercase English letters. For each employee i, exactly one non-empty prefix of their username S_i is chosen and assigned as their abbreviation T_i. That is, T_i must be a string consisting of the first 1 to |S_i| characters of S_i. Using the entire username as the abbreviation is also permitted.
The abbreviation assignment (T_1, T_2, \ldots, T_N) must satisfy the following prefix-free condition:
- Prefix-free condition: For any two distinct employees i, j (i \neq j), T_i is not a prefix of T_j, and T_j is not a prefix of T_i.
Here, a string A is a prefix of a string B if |A| \leq |B| and the first |A| characters of B match A. In particular, when A = B, A is also a prefix of B.
Therefore, by the prefix-free condition, having T_i = T_j for distinct employees i, j (i \neq j) is also not permitted.
Takahashi wants to minimize the total length of abbreviations \sum_{i=1}^{N} |T_i| if a valid abbreviation assignment exists. If a valid assignment exists, output the minimum total length; otherwise, output -1.
Constraints
- 1 \leq N \leq 2 \times 10^5
- S_i is a string consisting of lowercase English letters.
- 1 \leq |S_i| \leq 5 \times 10^5
- If i \neq j, then S_i \neq S_j (all usernames are distinct).
- \sum_{i=1}^{N} |S_i| \leq 5 \times 10^5
Input
N S_1 S_2 \vdots S_N
The first line contains an integer N representing the number of employees. The i-th of the following N lines contains a string S_i representing the username of the i-th employee.
Output
If a valid abbreviation assignment exists, output the minimum total length of abbreviations on a single line. If no valid assignment exists, output -1.
Sample Input 1
3 alice bob carol
Sample Output 1
3
Sample Input 2
2 a ab
Sample Output 2
-1
Sample Input 3
12 apple apricot banana bandit banner candle canary cattle dog dove duck zebra
Sample Output 3
38
Sample Input 4
40 aaaaaa aaaaab aaaaac aaaaad aaaaae aaaaba aaaabb aaaabc aaaabd aaaabe aaabaa aaabab aaabac aaabad aaabae aaabba aaabbb aaabbc aaabbd aaabbe aabaaa aabaab aabaac aabaad aabaae aababa aababb aababc aababd aababe abaaaa abaaab abaaac abaaad abaaaae ababaa ababab ababac ababad ababae
Sample Output 4
-1
Sample Input 5
1 a
Sample Output 5
1