実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
高橋君は
2025 年 5 月 17 日 A 時 B 分締切のレポートを、
2025 年 5 月 17 日 C 時 D 分に提出しました。
ここで、「A 時 B 分」と「C 時 D 分」は異なる時刻であることが保証されます。
高橋君が締切前にレポートを提出しているならば Yes
を、そうでないならば No
を出力してください。
制約
- 0 \leq A,C \leq 23
- 0 \leq B,D \leq 59
- (A,B)\neq(C,D)
- A,B,C,D は整数
入力
入力は以下の形式で標準入力から与えられる。
A B C D
出力
高橋君が締切前にレポートを提出しているならば Yes
を、そうでないならば No
を出力せよ。
入力例 1
22 40 22 30
出力例 1
Yes
レポートの締切は 22 時 40 分であり、高橋君は 22 時 30 分に提出しているため、締切前にレポートを提出しています。
よって、Yes
を出力します。
入力例 2
22 40 22 45
出力例 2
No
レポートの締切は 22 時 40 分であり、高橋君は 22 時 45 分に提出しているため、締切後にレポートを提出しています。
よって、No
を出力します。
入力例 3
12 0 11 30
出力例 3
Yes
Score : 100 points
Problem Statement
Takahashi had a report whose deadline was B minutes past A o'clock on May 17, 2025. He submitted it at D minutes past C o'clock on May 17, 2025.
It is guaranteed that "B minutes past A o'clock" and "D minutes past C o'clock" are different times.
Output Yes
if Takahashi submitted the report before the deadline, and No
otherwise.
Constraints
- 0 \leq A, C \leq 23
- 0 \leq B, D \leq 59
- (A, B) \neq (C, D)
- A, B, C, and D are integers.
Input
The input is given from Standard Input in the following format:
A B C D
Output
If Takahashi submitted the report before the deadline, output Yes
; otherwise, output No
.
Sample Input 1
22 40 22 30
Sample Output 1
Yes
The deadline is 22:40, and he submitted at 22:30, so he submitted before the deadline.
Hence, output Yes
.
Sample Input 2
22 40 22 45
Sample Output 2
No
The deadline is 22:40, and he submitted at 22:45, so he submitted after the deadline.
Hence, output No
.
Sample Input 3
12 0 11 30
Sample Output 3
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
高橋君は電卓を持っています。電卓には最初 1 が表示されています。
高橋君は電卓に対して N 回操作を行います。
i 回目 (1\leq i\leq N) の操作では、その時点で画面に表示されている数に正の整数 A_i をかけます。
しかし、電卓には K 桁までしか表示できないため、計算結果が (K+1) 桁以上になる場合、代わりに 1 が画面に表示されます。
そうでない場合は正しく計算結果が表示されます。
N 回の操作の後に電卓に表示されている数を求めてください。
制約
- 1 \leq N \leq 100
- 1 \leq K \leq 18
- 1 \leq A_i < 10^K
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \ldots A_N
出力
N 回の操作の後に電卓に表示されている数を出力せよ。
入力例 1
5 2 7 13 3 2 5
出力例 1
10
今回電卓は 2 桁まで表示することができ、最初 1 が表示されています。これに対して、次のように高橋君は操作を行います。
- 1 回目の操作で、7 をかけます。1\times 7=7 であり、電卓には 7 が表示されます。
- 2 回目の操作で、13 をかけます。7\times 13=91 であり、電卓には 91 が表示されます。
- 3 回目の操作で、3 をかけます。91\times 3=273 であり、3 桁になってしまうため、電卓には 1 が表示されます。
- 4 回目の操作で、2 をかけます。1\times 2=2 であり、電卓には 2 が表示されます。
- 5 回目の操作で、5 をかけます。2\times 5=10 であり、電卓には 10 が表示されます。
入力例 2
2 1 2 5
出力例 2
1
Score : 200 points
Problem Statement
Takahashi has a calculator that initially displays 1.
He will perform N operations on the calculator.
In the i-th operation (1 \leq i \leq N), he multiplies the currently displayed number by a positive integer A_i.
However, the calculator can display at most K digits.
If the result of the multiplication has (K+1) or more digits, the display shows 1 instead; otherwise, the result is shown correctly.
Find the number showing on the calculator after the N operations.
Constraints
- 1 \leq N \leq 100
- 1 \leq K \leq 18
- 1 \leq A_i < 10^K
- 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
Output the number shown on the calculator after the N operations.
Sample Input 1
5 2 7 13 3 2 5
Sample Output 1
10
This calculator can display at most two digits and initially shows 1. Takahashi operates as follows:
- The 1st operation multiplies by 7. Since 1 \times 7 = 7, the calculator shows 7.
- The 2nd operation multiplies by 13. Since 7 \times 13 = 91, the calculator shows 91.
- The 3rd operation multiplies by 3. Since 91\times 3=273, which has three digits, the calculator shows 1.
- The 4th operation multiplies by 2. Since 1\times 2=2, the calculator shows 2.
- The 5th operation multiplies by 5. Since 2\times 5=10, the calculator shows 10.
Sample Input 2
2 1 2 5
Sample Output 2
1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 350 点
問題文
整数列 A = (A_1, A_2, \ldots, A_{|A|}) に対し、A が チルダ型 とは以下の 4 つの条件をすべて満たすことであると定義します。
- A の長さ |A| は 4 以上である
- A_1 < A_2 である
- A_{i - 1} < A_i > A_{i + 1} を満たす 2 \leq i < |A| なる整数 i はちょうど 1 個である
- A_{i - 1} > A_i < A_{i + 1} を満たす 2 \leq i < |A| なる整数 i はちょうど 1 個である
数列 (1, 2, \ldots, N) を並べ替えて得られる数列 P = (P_1, P_2, \ldots, P_N) が与えられます。P の連続部分列であってチルダ型である数列の個数を求めてください。
制約
- 4 \leq N \leq 3 \times 10^5
- P = (P_1, P_2, \ldots, P_N) は (1, 2, \ldots, N) を並べ替えて得られる数列
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N P_1 P_2 \ldots P_N
出力
答えを出力せよ。
入力例 1
6 1 3 6 4 2 5
出力例 1
2
数列 (1, 3, 6, 4, 2, 5) の連続部分列のうちチルダ型であるものは (3, 6, 4, 2, 5), (1, 3, 6, 4, 2, 5) の 2 つのみです。
入力例 2
6 1 2 3 4 5 6
出力例 2
0
入力例 3
12 11 3 8 9 5 2 10 4 1 6 12 7
出力例 3
4
Score : 350 points
Problem Statement
For an integer sequence A = (A_1, A_2, \ldots, A_{|A|}), we say that A is tilde-shaped if it satisfies all of the following four conditions:
- The length |A| is at least 4.
- A_1 < A_2.
- There exists exactly one integer i with 2 \leq i < |A| such that A_{i-1} < A_i > A_{i+1}.
- There exists exactly one integer i with 2 \leq i < |A| such that A_{i-1} > A_i < A_{i+1}.
You are given a permutation P = (P_1, P_2, \ldots, P_N) of (1, 2, \ldots, N). Find the number of (contiguous) subarrays of P that are tilde-shaped.
Constraints
- 4 \leq N \leq 3 \times 10^5
- P = (P_1, P_2, \ldots, P_N) is a permutation of (1, 2, \ldots, N).
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N P_1 P_2 \ldots P_N
Output
Output the answer.
Sample Input 1
6 1 3 6 4 2 5
Sample Output 1
2
Among the subarrays of (1, 3, 6, 4, 2, 5), exactly two are tilde-shaped: (3, 6, 4, 2, 5) and (1, 3, 6, 4, 2, 5).
Sample Input 2
6 1 2 3 4 5 6
Sample Output 2
0
Sample Input 3
12 11 3 8 9 5 2 10 4 1 6 12 7
Sample Output 3
4
実行時間制限: 2.5 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
H 行 W 列のマス目があり、上から i 行目、左から j 列目のマスをマス (i, j) と表記します。
マス目の上には N 個のゴミが落ちており、i 番目のゴミはマス (X_i, Y_i) に落ちています。
Q 個のクエリが与えられるので、順に処理したときの各クエリの答えを求めてください。各クエリは、以下のいずれかです。
-
タイプ 1 :
1 x
の形式で入力として与えられる。マス目の x 行目に落ちているゴミの個数を答えとして求める。その後、x 行目に落ちているゴミはすべて捨てられ、マス目上から取り除かれる。 -
タイプ 2 :
2 y
の形式で入力として与えられる。マス目の y 列目に落ちているゴミの個数を答えとして求める。その後、y 列目に落ちているゴミはすべて捨てられ、マス目上から取り除かれる。
制約
- 1 \leq H, W, N \leq 2 \times 10^5
- 1 \leq X_i \leq H
- 1 \leq Y_i \leq W
- i \neq j のとき (X_i, Y_i) \neq (X_j, Y_j)
- 1 \leq Q \leq 2 \times 10^5
- タイプ 1 のクエリについて、1 \leq x \leq H
- タイプ 2 のクエリについて、1 \leq y \leq W
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
H W N X_1 Y_1 X_2 Y_2 \vdots X_N Y_N Q \text{query}_1 \text{query}_2 \vdots \text{query}_Q
ここで、\text{query}_i は i 番目のクエリであり、以下のいずれかの形式で与えられる。
1 x
2 y
出力
Q 行出力せよ。i 行目には i 番目のクエリの答えを出力せよ。
入力例 1
3 4 5 1 2 1 3 3 4 3 1 2 2 5 1 1 1 2 2 2 2 4 1 2
出力例 1
2 1 0 1 0
はじめ、ゴミはマス (1, 2), (1, 3), (3, 4), (3, 1), (2, 2) に落ちています。
1 番目のクエリでは、1 行目に落ちているゴミはマス (1, 2), (1, 3) に落ちているゴミの 2 つであるため答えは 2 となります。これらの 2 つのゴミは捨てられ、残ったゴミはマス (3, 4), (3, 1), (2, 2) に落ちているゴミです。
2 番目のクエリでは、2 行目に落ちているゴミはマス (2, 2) にあるゴミの 1 つであるため答えは 1 となります。このゴミは捨てられ、残ったゴミはマス (3, 4), (3, 1) に落ちているゴミです。
3 番目のクエリでは、2 列目に落ちているゴミは存在しないため答えは 0 となります。
4 番目のクエリでは、4 列目に落ちているゴミはマス (3, 4) にあるゴミの 1 つであるため答えは 1 となります。このゴミは捨てられ、残ったゴミはマス (3, 1) に落ちているゴミです。
5 番目のクエリでは、2 行目に落ちているゴミは存在しないため答えは 0 となります。
入力例 2
1 2 1 1 2 7 2 1 2 1 2 1 2 1 2 1 2 1 2 1
出力例 2
0 0 0 0 0 0 0
入力例 3
4 4 16 1 1 1 2 1 3 1 4 2 1 2 2 2 3 2 4 3 1 3 2 3 3 3 4 4 1 4 2 4 3 4 4 7 2 1 1 1 2 2 1 2 2 3 1 3 2 4
出力例 3
4 3 3 2 2 1 1
Score : 400 points
Problem Statement
There is a grid with H rows and W columns. Let (i, j) denote the cell at the i-th row from the top and the j-th column from the left.
There are N pieces of trash on the grid; the i-th piece is at cell (X_i, Y_i).
You are given Q queries, which you must process in order. Each query is of one of the following types:
-
Type 1: Given in the format
1 x
in the input. Report the number of trash pieces in the x-th row. Then, all trash pieces in the x-th row are removed from the grid. -
Type 2: Given in the format
2 y
in the input. Report the number of trash pieces in the y-th column. Then, all trash pieces in the y-th column are removed from the grid.
Constraints
- 1 \leq H, W, N \leq 2 \times 10^5
- 1 \leq X_i \leq H
- 1 \leq Y_i \leq W
- If i \neq j, then (X_i, Y_i) \neq (X_j, Y_j).
- 1 \leq Q \leq 2 \times 10^5
- For a type 1 query, 1 \leq x \leq H.
- For a type 2 query, 1 \leq y \leq W.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
H W N X_1 Y_1 X_2 Y_2 \vdots X_N Y_N Q \text{query}_1 \text{query}_2 \vdots \text{query}_Q
Here, \text{query}_i denotes the i-th query, which is given in one of the following formats:
1 x
2 y
Output
Output Q lines. The i-th line should contain the response to the i-th query.
Sample Input 1
3 4 5 1 2 1 3 3 4 3 1 2 2 5 1 1 1 2 2 2 2 4 1 2
Sample Output 1
2 1 0 1 0
Initially, trash pieces are at cells (1, 2), (1, 3), (3, 4), (3, 1), (2, 2).
In the 1st query, the 1st row contains two pieces of trash at (1, 2) and (1, 3), so report 2. These pieces are then removed; the remaining trash is at (3, 4), (3, 1), (2, 2).
In the 2nd query, the 2nd row contains one piece of trash at (2, 2), so report 1. This piece is then removed; the remaining trash is at (3, 4), (3, 1).
In the 3rd query, the 2nd column contains no trash, so report 0.
In the 4th query, the 4th column contains one piece of trash at (3, 4), so report 1. This piece is then removed; the remaining trash is at (3, 1).
In the 5th query, the 2nd row contains no trash, so report 0.
Sample Input 2
1 2 1 1 2 7 2 1 2 1 2 1 2 1 2 1 2 1 2 1
Sample Output 2
0 0 0 0 0 0 0
Sample Input 3
4 4 16 1 1 1 2 1 3 1 4 2 1 2 2 2 3 2 4 3 1 3 2 3 3 3 4 4 1 4 2 4 3 4 4 7 2 1 1 1 2 2 1 2 2 3 1 3 2 4
Sample Output 3
4 3 3 2 2 1 1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
正の整数 N,K が与えられます。
N 以下の正の整数 x であって、次の条件をみたすものの 総和 を 998244353 で割った余りを求めてください。
- x の popcount の値はちょうど K である。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
popcount とは
正整数 y に対して、y の popcount の値 \mathrm{popcount}(y) は、y を二進数表記したとき 1 となっている桁の個数を表します。 例えば、\mathrm{popcount}(5)=2, \mathrm{popcount}(16)=1, \mathrm{popcount}(25)=3 です。制約
- 1\leq T\leq 100
- 1\leq N < 2^{60}
- 1\leq K \leq 60
- T,N,K は整数
入力
入力は以下の形式で標準入力から与えられる。
T \mathrm{case}_1 \mathrm{case}_2 \vdots \mathrm{case}_T
\mathrm{case}_i は i 番目のテストケースを表す。 各テストケースは以下の形式で与えられる。
N K
出力
T 行出力せよ。i 行目 (1\leq i\leq T) には i 番目のテストケースに対する答えを出力せよ。
入力例 1
2 20 2 1234567890 17
出力例 1
100 382730918
1 番目のテストケースについて、20 以下の正の整数のうち、popcount の値が 2 であるものは 3,5,6,9,10,12,17,18,20 の 9 つであり、その総和は 100 となります。
100 を 998244353 で割った余りは 100 であるため、1 行目には 100 を出力します。
998244353 で割った余りを出力する必要があることに注意してください。
Score : 450 points
Problem Statement
You are given positive integers N and K.
Find the sum, modulo 998244353, of all positive integers x that do not exceed N and satisfy the following condition:
- the popcount of x is exactly K.
You are given T test cases; solve each of them.
What is popcount?
For a positive integer y, \mathrm{popcount}(y) denotes the number of 1 bits in the binary representation of y. For example, \mathrm{popcount}(5)=2, \mathrm{popcount}(16)=1, \mathrm{popcount}(25)=3.Constraints
- 1 \leq T \leq 100
- 1 \leq N < 2^{60}
- 1 \leq K \leq 60
- T, N, and K are integers.
Input
The input is given from Standard Input in the following format:
T \mathrm{case}_1 \mathrm{case}_2 \vdots \mathrm{case}_T
\mathrm{case}_i denotes the i-th test case. Each test case is given in the following format:
N K
Output
Output T lines. The i-th line (1 \leq i \leq T) should contain the answer for the i-th test case.
Sample Input 1
2 20 2 1234567890 17
Sample Output 1
100 382730918
For the first test case, there are nine positive integers not exceeding 20 whose popcount is 2: 3, 5, 6, 9, 10, 12, 17, 18, 20. Their sum is 100.
The remainder when 100 is divided by 998244353 is 100, so output 100
on the first line.
Remember to output the sum modulo 998244353.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
N 頂点の木 T があり、
頂点および辺はそれぞれ頂点 1, 頂点 2, \ldots, 頂点 N、辺 1, 辺 2, \ldots, 辺 (N-1) と番号づけられています。
特に、辺 i (1\leq i\leq N-1) は頂点 U_i と頂点 V_i を結んでいます。
また、各頂点には重みが定められており、最初、各頂点の重みはすべて 1 です。
Q 個のクエリが与えられるので、順に処理してください。 各クエリは次の 2 種類のいずれかです。
1 x w
: 頂点 x の重みを w 増加させる。2 y
: 辺 y を削除すると、T は 2 つの部分木(連結成分)に分かれる。それぞれの部分木に含まれる頂点の重みの総和をその部分木の重みとするとき、2 つの部分木の重みの差を出力する。
2 種類目のクエリについて、T から任意の辺を 1 つ選んで削除したとき、T は必ず 2 つの部分木に分かれることが証明できます。
また、2 種類目のクエリでは、実際に辺を削除しているわけではないことに注意してください。
制約
- 2\leq N\leq 3\times 10^5
- 1\leq U_i,V_i\leq N
- 1\leq Q\leq 3\times 10^5
- 1\leq x\leq N
- 1\leq w\leq 1000
- 1\leq y\leq N-1
- 入力はすべて整数
- 与えられるグラフは木である。
- 2 種類目のクエリが少なくとも 1 つ存在する。
入力
入力は以下の形式で標準入力から与えられる。
N U_1 V_1 U_2 V_2 \vdots U_{N-1} V_{N-1} Q \mathrm{query}_1 \mathrm{query}_2 \vdots \mathrm{query}_Q
各クエリ \mathrm{query}_i (1\leq i\leq Q) は次の形式のいずれかで与えられる。
1 x w
2 y
出力
2 種類目のクエリの個数を K 個として、K 行出力せよ。i 行目 (1\leq i\leq K) には i 番目の 2 種類目のクエリに対する答えを出力せよ。
入力例 1
6 1 2 1 3 2 4 4 5 4 6 5 2 1 1 1 3 2 1 1 4 10 2 5
出力例 1
2 1 17
木 T の構造、頂点番号の対応は下図左の通りです。最初、各頂点の重みはすべて 1 です。
1 番目のクエリでは、辺 1 を削除することを考えます。
このとき、頂点 1 を含む部分木と頂点 2 を含む部分木に分かれます。
頂点 1 を含む部分木の重みは 2、頂点 2 を含む部分木の重みは 4 であるため、その差の 2 を出力します。(下図右)
2 番目のクエリでは、頂点 1 の重みを 3 増加させます。
3 番目のクエリでは、辺 1 を削除することを考えます。
頂点 1 を含む部分木の重みは 5、頂点 2 を含む部分木の重みは 4 であるため、その差の 1 を出力します。(下図左)
4 番目のクエリでは、頂点 4 の重みを 10 増加させます。
5 番目のクエリでは、辺 5 を削除することを考えます。
このとき、頂点 4 を含む部分木と頂点 6 のみからなる部分木に分かれます。
頂点 4 を含む部分木の重みは 18、頂点 6 のみからなる部分木の重みは 1 であるため、その差の 17 を出力します。(下図右)
よって、2 種類目のクエリの答えである 2,1,17 をこの順に改行区切りで出力します。
Score : 500 points
Problem Statement
There is a tree T with N vertices.
The vertices are labeled as vertex 1, vertex 2, \ldots, vertex N, and the edges are labeled as edge 1, edge 2, \ldots, edge (N-1).
Edge i (1\leq i\leq N-1) connects vertices U_i and V_i.
Each vertex has a weight; initially, the weight of every vertex is 1.
You are given Q queries to process in order. Each query is of one of the following two types:
1 x w
: Increase the weight of vertex x by w.2 y
: If edge y were removed, the tree T would be split into two subtrees (connected components). For each subtree, let its weight be the sum of the weights of its vertices. Output the difference between the weights of the two subtrees.
For a query of the second type, it can be proved that removing any edge of T always splits it into exactly two subtrees.
Note that queries of the second type do not actually delete remove the edge.
Constraints
- 2 \leq N \leq 3 \times 10^5
- 1 \leq U_i, V_i \leq N
- 1 \leq Q \leq 3 \times 10^5
- 1 \leq x \leq N
- 1 \leq w \leq 1000
- 1 \leq y \leq N-1
- All input values are integers.
- The given graph is a tree.
- There is at least one query of the second type.
Input
The input is given from Standard Input in the following format:
N U_1 V_1 U_2 V_2 \vdots U_{N-1} V_{N-1} Q \mathrm{query}_1 \mathrm{query}_2 \vdots \mathrm{query}_Q
Each query \mathrm{query}_i (1\leq i\leq Q) is given in one of the following formats:
1 x w
2 y
Output
Let K be the number of queries of the second type. Output K lines; the i-th line (1 \leq i \leq K) should contain the answer to the i-th query of the second type.
Sample Input 1
6 1 2 1 3 2 4 4 5 4 6 5 2 1 1 1 3 2 1 1 4 10 2 5
Sample Output 1
2 1 17
The structure of the tree T and the vertex numbering are as shown on the left of the figure below. Initially, the weight of every vertex is 1.
In the 1st query, consider deleting edge 1.
The tree splits into the subtree containing vertex 1 and the subtree containing vertex 2.
The subtree with vertex 1 has weight 2; the subtree with vertex 2 has weight 4. Output the difference, 2 (figure below, right).
The 2nd query increases the weight of vertex 1 by 3.
In the 3rd query, consider deleting edge 1.
The subtree with vertex 1 has weight 5; the subtree with vertex 2 has weight 4. Output the difference, 1 (figure below, left).
The 4th query increases the weight of vertex 4 by 10.
In the 5th query, consider deleting edge 5.
The tree splits into the subtree containing vertex 4 and the subtree consisting only of vertex 6.
The subtree with vertex 4 has weight 18; the subtree with vertex 6 has weight 1. Output the difference, 17 (figure below, right).
Thus, output the answers to the queries of second type in order: 2, 1, and 17, separated by newlines.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 625 点
問題文
数直線上にあなたと N 人の商人がいます。商人には 1, 2, \ldots, N の番号が付けられています。
はじめ、あなたは座標 0 におり、商人 i は座標 X_i にいます。また、各商人は品物を 1 つずつ持っており、商人 i が持っている品物を品物 i と表記します。
あなたの目的は、品物 1, 2, \ldots, N をこの順に受け取ることです。
あなたは、以下の 3 つの操作を好きな順序で好きな回数繰り返すことができます。
- 自分が 1 移動する。この操作にはコストが C かかる。
- 商人を 1 人選び、選んだ商人に 1 移動してもらう。この操作にはコストが D かかる。
- 商人を 1 人選び、選んだ商人を商人 i とする。あなたと商人 i のいる座標が一致しており、あなたが商人 i から品物を受け取ったことがない場合、商人 i から品物 i を受け取る。そうでない場合、何もしない。この操作にはコストが 0 かかる。
目的を達成するためにかかるコストの合計の最小値を求めてください。
また、目的を達成するためにかかるコストの合計を最小化したときに各商人から品物を受け取る座標として考えられるもののうちのひとつを求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq C, D \leq 10^5
- -10^5 \leq X_i \leq 10^5
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N C D X_1 X_2 \ldots X_N
出力
2 行出力せよ。
1 行目には目的を達成するためにかかるコストの合計の最小値を出力せよ。
2 行目には、N 個の整数 A_1, A_2, \ldots, A_N をこの順に空白区切りで出力せよ。ただし、ある操作列が存在し、この操作列において以下の条件がともに満たされているようにせよ。
- 目的を達成しており、目的を達成するという条件のもとコストの合計は最小である。
- 1 \leq i \leq N なるすべての整数 i に対して商人 i から 座標 A_i で品物を受け取っている。
入力例 1
3 2 3 1 -1 2
出力例 1
10 0 0 2
例えば以下のように操作をすることで、コストの合計を 10 として目的を達成することができます。
- 商人 1 に座標 1 から座標 0 に移動してもらう。この操作にはコストが 3 かかる。
- 商人 2 に座標 -1 から座標 0 に移動してもらう。この操作にはコストが 3 かかる。
- 商人 1 から品物 1 を受け取る。この操作にはコストが 0 かかる。
- 商人 2 から品物 2 を受け取る。この操作にはコストが 0 かかる。
- あなたが座標 0 から座標 1 に移動する。この操作にはコストが 2 かかる。
- あなたが座標 1 から座標 2 に移動する。この操作にはコストが 2 かかる。
- 商人 3 から品物 3 を受け取る。この操作にはコストが 0 かかる。
コストの合計を 10 未満として目的を達成することはできません。
入力例 2
2 100000 60000 100000 -100000
出力例 2
12000000000 0 0
入力例 3
6 4 4 2 -1 5 -2 -2 2
出力例 3
56 0 -1 -1 -2 -2 2
Score : 625 points
Problem Statement
You and N merchants stand on a number line. The merchants are numbered 1, 2, \ldots, N.
Initially, you are at coordinate 0, and merchant i is at coordinate X_i. Each merchant holds one item; the item held by merchant i is called item i.
Your goal is to receive items 1, 2, \ldots, N in this order.
You may repeat any number of times, in any order, the following three operations:
- Move yourself by 1. The cost of this operation is C.
- Choose one merchant and move that merchant by 1. The cost of this operation is D.
- Choose one merchant, say merchant i. If you and merchant i are at the same coordinate, and you have not yet received item i, then receive item i from merchant i. Otherwise, do nothing. The cost of this operation is 0.
Find the minimum total cost required to achieve the goal.
Also, output one possible combination of coordinates at which you receive each item when the total cost is minimized.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq C, D \leq 10^5
- -10^5 \leq X_i \leq 10^5
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N C D X_1 X_2 \ldots X_N
Output
Output two lines.
The first line should contain the minimal total cost required to achieve the goal.
The second line should contain N integers A_1, A_2, \ldots, A_N separated by spaces. Here, there must exist a sequence of operations that satisfies both of the following conditions:
- The goal is achieved, with the minimum possible total cost.
- For every integer i such that 1 \leq i \leq N, you receive item i at coordinate A_i.
Sample Input 1
3 2 3 1 -1 2
Sample Output 1
10 0 0 2
For example, the following sequence of operations achieves the goal with total cost 10:
- Move merchant 1 from coordinate 1 to 0. The cost of this operation is 3.
- Move merchant 2 from coordinate -1 to 0. The cost of this operation is 3.
- Receive item 1 from merchant 1. The cost of this operation is 0.
- Receive item 2 from merchant 2. The cost of this operation is 0.
- Move yourself from coordinate 0 to 1. The cost of this operation is 2.
- Move yourself from coordinate 1 to 2. The cost of this operation is 2.
- Receive item 3 from merchant 3. The cost of this operation is 0.
It is impossible to achieve the goal with total cost less than 10.
Sample Input 2
2 100000 60000 100000 -100000
Sample Output 2
12000000000 0 0
Sample Input 3
6 4 4 2 -1 5 -2 -2 2
Sample Output 3
56 0 -1 -1 -2 -2 2