A - 家計簿の修正

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

配点 : 266 点

問題文

高橋君は N 日間の家計簿をつけています。

高橋君の口座残高は最初 0 円です。 i 日目 (1 \leq i \leq N) には A_i 円の取引が記録されており、正の値なら入金、負の値なら出金を表します。各日の取引は順に口座残高に反映されます(口座残高が途中で負になることもあり得ます)。すべての取引を反映した N 日目終了時点の口座残高は A_1 + A_2 + \cdots + A_N 円です。

しかし、高橋君は家計簿を見直しているうちに、いくつかの取引が誤って記録されていたことに気づきました。そこで彼は Q 回の修正操作を順番に行います。 j 回目 (1 \leq j \leq Q) の操作では、 D_j 日目の取引額を 0 円に変更します。一度 0 円に変更された日の取引額は、それ以降の操作においても 0 のまま変わりません。つまり、操作を重ねるごとに取引額が 0 に変更された日が増えていきます。

各操作の後、すべての日の取引額の総和、すなわち N 日目終了時点での口座残高を求めてください。

なお、 D_1, D_2, \ldots, D_Q はすべて異なります。すなわち、同じ日の取引額が複数回変更されることはありません。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq N
  • -10^9 \leq A_i \leq 10^9
  • 1 \leq D_j \leq N
  • D_1, D_2, \ldots, D_Q はすべて異なる
  • 入力はすべて整数

入力

N Q
A_1 A_2 \cdots A_N
D_1
D_2
\vdots
D_Q
  • 1 行目には、家計簿の日数を表す整数 N と、修正操作の回数を表す整数 Q が、スペース区切りで与えられる。
  • 2 行目には、各日の取引額を表す N 個の整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。 A_i は i 日目の取引額である。
  • 続く Q 行のうち j 行目(全体では 2 + j 行目)には、 j 回目の操作で取引額を 0 に変更する日を表す整数 D_j が1つ与えられる。

出力

Q 行出力してください。 j 行目には、 1 回目から j 回目までの操作をすべて行った後の、すべての日の取引額の総和(N 日目終了時点での口座残高)を出力してください。


入力例 1

5 3
100 -50 200 -30 80
2
4
1

出力例 1

350
380
280

入力例 2

7 4
1000 -500 300 -200 400 -100 200
3
1
5
7

出力例 2

800
-200
-600
-800

入力例 3

10 5
1000000000 -999999999 500000000 -400000000 300000000 -200000000 100000000 -50000000 25000000 -12500000
1
3
5
7
9

出力例 3

-737499999
-1237499999
-1537499999
-1637499999
-1662499999

Score : 266 pts

Problem Statement

Takahashi is keeping a household account book for N days.

Takahashi's account balance is initially 0 yen. On day i (1 \leq i \leq N), a transaction of A_i yen is recorded, where a positive value represents a deposit and a negative value represents a withdrawal. Each day's transaction is applied to the account balance in order (the account balance may become negative during the process). The account balance at the end of day N, after all transactions have been applied, is A_1 + A_2 + \cdots + A_N yen.

However, while reviewing the account book, Takahashi noticed that some transactions were recorded incorrectly. He then performs Q correction operations in order. In the j-th operation (1 \leq j \leq Q), he changes the transaction amount on day D_j to 0 yen. Once a day's transaction amount has been changed to 0, it remains 0 in all subsequent operations. In other words, with each operation, the number of days whose transaction amounts have been set to 0 increases.

After each operation, find the sum of all days' transaction amounts, that is, the account balance at the end of day N.

Note that D_1, D_2, \ldots, D_Q are all distinct. In other words, the same day's transaction amount is never changed more than once.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq N
  • -10^9 \leq A_i \leq 10^9
  • 1 \leq D_j \leq N
  • D_1, D_2, \ldots, D_Q are all distinct
  • All input values are integers

Input

N Q
A_1 A_2 \cdots A_N
D_1
D_2
\vdots
D_Q
  • The first line contains an integer N representing the number of days in the account book and an integer Q representing the number of correction operations, separated by a space.
  • The second line contains N integers A_1, A_2, \ldots, A_N representing the transaction amounts for each day, separated by spaces. A_i is the transaction amount on day i.
  • Among the following Q lines, the j-th line (the (2 + j)-th line overall) contains a single integer D_j representing the day whose transaction amount is changed to 0 in the j-th operation.

Output

Print Q lines. On the j-th line, print the sum of all days' transaction amounts (the account balance at the end of day N) after performing all operations from the 1-st through the j-th.


Sample Input 1

5 3
100 -50 200 -30 80
2
4
1

Sample Output 1

350
380
280

Sample Input 2

7 4
1000 -500 300 -200 400 -100 200
3
1
5
7

Sample Output 2

800
-200
-600
-800

Sample Input 3

10 5
1000000000 -999999999 500000000 -400000000 300000000 -200000000 100000000 -50000000 25000000 -12500000
1
3
5
7
9

Sample Output 3

-737499999
-1237499999
-1537499999
-1637499999
-1662499999
B - パイプの接続

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

配点 : 333 点

問題文

高橋君は N 本のパイプを持っています。i 番目のパイプの長さは A_i です。

一部のパイプは錆びています。i 番目のパイプが錆びている場合は B_i = 1、錆びていない場合は B_i = 0 で表されます。錆びたパイプは錆の影響で実際に使える長さ(有効長)が短くなり、\max(A_i - C, 0) になります。錆びていないパイプの有効長は A_i です。

高橋君はこれらの N 本のパイプから 1 本以上 N 本以下 を自由に選び(同じパイプを複数回選ぶことはできません)、一列につなぎ合わせて 1 本のパイプを作ります。1 本だけ選ぶ場合はそのパイプの有効長がそのまま完成品の長さになります。2 本以上選ぶ場合は、パイプ同士の接続にジョイント部品を使用するため、接続箇所 1 箇所あたり完成品の長さが K だけ短くなります。

選んだ本数を M とすると、接続箇所は M - 1 箇所あるため、完成するパイプの長さは次の式で求められます。

(\text{選んだ } M \text{ 本のパイプの有効長の合計}) - (M - 1) \times K

高橋君は、完成するパイプの長さを D 以上にしたいです。選ぶパイプの組み合わせと本数を最適に決めたとき、完成するパイプの長さの最大値を求めてください。最大値が D 以上であればその値を、どのように選んでも D 以上にできない場合は -1 を出力してください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq D \leq 10^{14}
  • 1 \leq K \leq 10^9
  • 1 \leq C \leq 10^9
  • 1 \leq A_i \leq 10^9
  • B_i \in \{0, 1\}
  • 入力はすべて整数である

入力

N D K C
A_1 B_1
A_2 B_2
\vdots
A_N B_N
  • 1 行目には、パイプの本数 N、必要な長さ D、接続 1 箇所あたりの長さの減少 K、錆による長さの減少 C が、スペース区切りで与えられる。
  • 2 行目から N + 1 行目では、各パイプの情報が与えられる。
  • 1 + i 行目では、i 番目のパイプの長さ A_i と、錆びているかを表す B_i(0 または 1)が、スペース区切りで与えられる。

出力

完成するパイプの長さの最大値が D 以上の場合は、その最大値を 1 行で出力せよ。D 以上にできない場合は -1 を 1 行で出力せよ。


入力例 1

4 15 3 4
10 0
8 1
7 0
6 1

出力例 1

15

入力例 2

3 20 5 3
9 0
8 1
7 0

出力例 2

-1

入力例 3

8 40 4 5
12 0
11 1
20 0
9 1
15 0
7 0
14 1
10 0

出力例 3

55

入力例 4

15 90 7 10
30 0
25 1
18 0
40 1
12 1
9 0
27 0
14 1
33 0
21 1
16 0
45 1
8 1
19 0
24 1

出力例 4

180

入力例 5

1 1000000000 1000000000 1000000000
1000000000 0

出力例 5

1000000000

Score : 333 pts

Problem Statement

Takahashi has N pipes. The length of the i-th pipe is A_i.

Some pipes are rusted. If the i-th pipe is rusted, B_i = 1; if it is not rusted, B_i = 0. Due to rust, the effective usable length of a rusted pipe is reduced to \max(A_i - C, 0). The effective length of a non-rusted pipe is A_i.

Takahashi freely selects at least 1 and at most N of these N pipes (each pipe can be selected at most once) and connects them in a line to form a single pipe. If only one pipe is selected, its effective length directly becomes the length of the finished pipe. If two or more pipes are selected, joint parts are used to connect the pipes, so the finished pipe's length is reduced by K for each connection point.

If the number of selected pipes is M, there are M - 1 connection points, so the length of the finished pipe is given by the following formula:

(\text{sum of effective lengths of the } M \text{ selected pipes}) - (M - 1) \times K

Takahashi wants the length of the finished pipe to be at least D. When the combination and number of pipes are chosen optimally, find the maximum possible length of the finished pipe. If the maximum value is at least D, output that value; if it is impossible to achieve a length of D or more regardless of the selection, output -1.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq D \leq 10^{14}
  • 1 \leq K \leq 10^9
  • 1 \leq C \leq 10^9
  • 1 \leq A_i \leq 10^9
  • B_i \in \{0, 1\}
  • All input values are integers.

Input

N D K C
A_1 B_1
A_2 B_2
\vdots
A_N B_N
  • The first line contains the number of pipes N, the required length D, the length reduction per connection point K, and the length reduction due to rust C, separated by spaces.
  • From the 2nd line to the (N + 1)-th line, the information for each pipe is given.
  • The (1 + i)-th line contains the length A_i of the i-th pipe and B_i (0 or 1) indicating whether it is rusted, separated by spaces.

Output

If the maximum possible length of the finished pipe is at least D, output that maximum value on a single line. If it is impossible to achieve a length of at least D, output -1 on a single line.


Sample Input 1

4 15 3 4
10 0
8 1
7 0
6 1

Sample Output 1

15

Sample Input 2

3 20 5 3
9 0
8 1
7 0

Sample Output 2

-1

Sample Input 3

8 40 4 5
12 0
11 1
20 0
9 1
15 0
7 0
14 1
10 0

Sample Output 3

55

Sample Input 4

15 90 7 10
30 0
25 1
18 0
40 1
12 1
9 0
27 0
14 1
33 0
21 1
16 0
45 1
8 1
19 0
24 1

Sample Output 4

180

Sample Input 5

1 1000000000 1000000000 1000000000
1000000000 0

Sample Output 5

1000000000
C - お花見の散歩道

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

配点 : 366 点

問題文

高橋君の住む町には、一直線に並んだ N 地点の桜スポットがあり、順にスポット 1, スポット 2, \ldots, スポット N と番号が付けられています。隣り合うスポット同士はそれぞれ遊歩道で結ばれており、スポット i とスポット i + 1 を結ぶ遊歩道 i(1 \leq i \leq N - 1)が存在します。遊歩道は合計 N - 1 本です。

各スポット j(1 \leq j \leq N)には、その地点の桜の美しさを表すスコア P_j が定められています。また、各遊歩道 i(1 \leq i \leq N - 1)には整備コスト C_i が設定されています。

高橋君は、連続する区間の遊歩道をまとめて整備し、散歩コースを作ろうと考えています。具体的には、整数 l, r(1 \leq l \leq r \leq N - 1)を選び、遊歩道 l, 遊歩道 l+1, \ldots, 遊歩道 r をすべて整備します。このとき、整備された遊歩道沿いにあるスポット l, スポット l+1, \ldots, スポット r+1 の すべて を散歩コースとして訪れることができます。

散歩コースから得られる 満足度 は、コースに含まれる r - l + 2 個のスポットのスコアの合計

P_l + P_{l+1} + \cdots + P_{r+1}

と定義されます。

一方、整備にかかる 総コスト は

C_l + C_{l+1} + \cdots + C_r

です。

高橋君の予算は B であり、総コストが B 以下となるような (l, r) のみ選択できます。高橋君は必ずいずれかの区間を選んで整備を行います。

総コストが B 以下であるようなすべての (l, r) の選び方の中で、満足度の最大値 を求めてください。

なお、制約より各遊歩道の整備コストは B 以下であるため、l = r(遊歩道を 1 本だけ整備する場合)は必ず条件を満たし、選択可能な (l, r) は 1 つ以上存在します。

制約

  • 2 \leq N \leq 5 \times 10^5
  • 1 \leq B \leq 10^{14}
  • 1 \leq P_j \leq 10^9(1 \leq j \leq N)
  • 1 \leq C_i \leq B(1 \leq i \leq N - 1)
  • 入力はすべて整数である

入力

N B
P_1 P_2 \ldots P_N
C_1 C_2 \ldots C_{N-1}
  • 1 行目には、桜スポットの数を表す整数 N と、予算を表す整数 B が、スペース区切りで与えられる。
  • 2 行目には、各スポットのスコアを表す N 個の整数 P_1, P_2, \ldots, P_N が、スペース区切りで与えられる。
  • 3 行目には、各遊歩道の整備コストを表す N - 1 個の整数 C_1, C_2, \ldots, C_{N-1} が、スペース区切りで与えられる。

出力

総コストが B 以下となる整備区間における満足度の最大値を 1 行で出力せよ。


入力例 1

5 10
3 1 4 1 5
2 3 4 5

出力例 1

10

入力例 2

4 3
10 20 30 40
1 2 3

出力例 2

70

入力例 3

10 15
5 8 3 10 7 2 9 6 4 11
3 2 5 1 4 6 2 3 7

出力例 3

35

入力例 4

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

出力例 4

130

入力例 5

2 100000000000000
1000000000 1000000000
1

出力例 5

2000000000

Score : 366 pts

Problem Statement

In the town where Takahashi lives, there are N cherry blossom spots arranged in a straight line, numbered Spot 1, Spot 2, \ldots, Spot N in order. Adjacent spots are connected by promenades, and there exists promenade i (1 \leq i \leq N - 1) connecting Spot i and Spot i + 1. There are N - 1 promenades in total.

Each Spot j (1 \leq j \leq N) has a score P_j representing the beauty of the cherry blossoms at that location. Additionally, each promenade i (1 \leq i \leq N - 1) has a maintenance cost C_i.

Takahashi plans to maintain a contiguous interval of promenades to create a walking course. Specifically, he chooses integers l, r (1 \leq l \leq r \leq N - 1) and maintains all of promenade l, promenade l+1, \ldots, promenade r. By doing so, he can visit all of Spot l, Spot l+1, \ldots, Spot r+1 along the maintained promenades as his walking course.

The satisfaction obtained from the walking course is defined as the sum of the scores of the r - l + 2 spots included in the course:

P_l + P_{l+1} + \cdots + P_{r+1}

On the other hand, the total cost of maintenance is:

C_l + C_{l+1} + \cdots + C_r

Takahashi's budget is B, and he can only choose (l, r) such that the total cost is at most B. Takahashi must choose and maintain exactly one such interval.

Among all ways to choose (l, r) such that the total cost is at most B, find the maximum value of the satisfaction.

Note that, due to the constraints, the maintenance cost of each promenade is at most B, so l = r (maintaining just one promenade) always satisfies the condition, and there exists at least one valid choice of (l, r).

Constraints

  • 2 \leq N \leq 5 \times 10^5
  • 1 \leq B \leq 10^{14}
  • 1 \leq P_j \leq 10^9 (1 \leq j \leq N)
  • 1 \leq C_i \leq B (1 \leq i \leq N - 1)
  • All input values are integers

Input

N B
P_1 P_2 \ldots P_N
C_1 C_2 \ldots C_{N-1}
  • The first line contains an integer N representing the number of cherry blossom spots and an integer B representing the budget, separated by a space.
  • The second line contains N integers P_1, P_2, \ldots, P_N representing the scores of each spot, separated by spaces.
  • The third line contains N - 1 integers C_1, C_2, \ldots, C_{N-1} representing the maintenance costs of each promenade, separated by spaces.

Output

Print in one line the maximum satisfaction among all maintenance intervals whose total cost is at most B.


Sample Input 1

5 10
3 1 4 1 5
2 3 4 5

Sample Output 1

10

Sample Input 2

4 3
10 20 30 40
1 2 3

Sample Output 2

70

Sample Input 3

10 15
5 8 3 10 7 2 9 6 4 11
3 2 5 1 4 6 2 3 7

Sample Output 3

35

Sample Input 4

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

Sample Output 4

130

Sample Input 5

2 100000000000000
1000000000 1000000000
1

Sample Output 5

2000000000
D - 最安経路

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

配点 : 400 点

問題文

高橋君は N 個の都市からなる国に住んでいます。各都市には 1 から N までの番号が付けられており、都市 i の人口は M_i です。

この国には K 本の道路が整備されており、道路 k は都市 U_k と都市 V_k を双方向に結んでいます。高橋君は道路で直接結ばれた2つの都市の間を、どちらの方向にも移動することができます。また、同じ道路や同じ都市を複数回通ることもできます。

高橋君が道路で直接結ばれた都市 a から隣接する都市 b へ1回移動するごとに、通行料として M_a \times M_b を支払わなければなりません。

高橋君は現在、都市 1 にいます。この国には P 個の空港のある都市があり、それらは都市 E_1, E_2, \dots, E_P です。高橋君はいずれかの空港のある都市にたどり着けば、この国から出国することができます。都市 1 自体が空港のある都市である場合は、移動せずに出国でき、通行料の合計は 0 です。

高橋君が都市 1 から出発し、いずれかの空港のある都市に到達するまでに支払う通行料の合計の最小値を求めてください。なお、都市 1 から少なくとも 1 つの空港のある都市へ到達可能であることが保証されています。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 2 \times 10^5
  • 1 \leq P \leq N
  • 1 \leq M_i \leq 10^4 (1 \leq i \leq N)
  • 1 \leq U_k < V_k \leq N (1 \leq k \leq K)
  • (U_k, V_k) の組はすべて異なる
  • 1 \leq E_p \leq N (1 \leq p \leq P)
  • E_1, E_2, \dots, E_P はすべて異なる
  • 都市 1 から少なくとも1つの空港のある都市へ道路を通じて到達可能である
  • 入力はすべて整数である

入力

N K P
M_1 M_2 \dots M_N
U_1 V_1
U_2 V_2
\vdots
U_K V_K
E_1 E_2 \dots E_P
  • 1 行目には、都市の数 N 、道路の数 K 、空港のある都市の数 P が、スペース区切りで与えられる。
  • 2 行目には、各都市の人口 M_1, M_2, \dots, M_N が、スペース区切りで与えられる。
  • 続く K 行にわたり、道路の情報が与えられる。
  • 2 + k 行目 (1 \leq k \leq K) には、道路 k が結ぶ2つの都市の番号 U_k と V_k が、スペース区切りで与えられる。
  • K + 3 行目には、空港のある都市の番号 E_1, E_2, \dots, E_P が、スペース区切りで与えられる。

出力

高橋君が都市 1 からいずれかの空港のある都市に到達するまでに支払う通行料の合計の最小値を 1 行で出力せよ。


入力例 1

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

出力例 1

7

入力例 2

3 2 1
5 3 7
1 2
2 3
1

出力例 2

0

入力例 3

7 8 2
3 10 2 5 1 8 4
1 2
1 3
2 4
2 5
3 4
3 5
4 6
5 7
6 7

出力例 3

12

入力例 4

10 15 3
4 9 2 7 1 3 8 6 5 10
1 2
1 3
2 4
2 5
3 5
3 6
4 7
5 7
5 8
6 8
6 9
7 10
8 9
8 10
9 10
7 9 10

出力例 4

18

入力例 5

2 1 1
1 10000
1 2
2

出力例 5

10000

Score : 400 pts

Problem Statement

Takahashi lives in a country consisting of N cities. Each city is numbered from 1 to N, and the population of city i is M_i.

There are K roads in this country, and road k bidirectionally connects city U_k and city V_k. Takahashi can travel in either direction between two cities that are directly connected by a road. He may also pass through the same road or the same city multiple times.

Each time Takahashi travels from city a to an adjacent city b that is directly connected by a road, he must pay a toll of M_a \times M_b.

Takahashi is currently in city 1. There are P cities with airports in this country, which are cities E_1, E_2, \dots, E_P. Takahashi can leave the country once he reaches any city with an airport. If city 1 itself has an airport, he can leave without moving, and the total toll is 0.

Find the minimum total toll Takahashi must pay to travel from city 1 to any city with an airport. It is guaranteed that at least one city with an airport is reachable from city 1.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 2 \times 10^5
  • 1 \leq P \leq N
  • 1 \leq M_i \leq 10^4 (1 \leq i \leq N)
  • 1 \leq U_k < V_k \leq N (1 \leq k \leq K)
  • All pairs (U_k, V_k) are distinct
  • 1 \leq E_p \leq N (1 \leq p \leq P)
  • E_1, E_2, \dots, E_P are all distinct
  • At least one city with an airport is reachable from city 1 via roads
  • All input values are integers

Input

N K P
M_1 M_2 \dots M_N
U_1 V_1
U_2 V_2
\vdots
U_K V_K
E_1 E_2 \dots E_P
  • The first line contains the number of cities N, the number of roads K, and the number of cities with airports P, separated by spaces.
  • The second line contains the population of each city M_1, M_2, \dots, M_N, separated by spaces.
  • The following K lines provide information about the roads.
  • The (2 + k)-th line (1 \leq k \leq K) contains the numbers of the two cities connected by road k, U_k and V_k, separated by spaces.
  • The (K + 3)-th line contains the numbers of the cities with airports E_1, E_2, \dots, E_P, separated by spaces.

Output

Print in one line the minimum total toll Takahashi must pay to travel from city 1 to any city with an airport.


Sample Input 1

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

Sample Output 1

7

Sample Input 2

3 2 1
5 3 7
1 2
2 3
1

Sample Output 2

0

Sample Input 3

7 8 2
3 10 2 5 1 8 4
1 2
1 3
2 4
2 5
3 4
3 5
4 6
5 7
6 7

Sample Output 3

12

Sample Input 4

10 15 3
4 9 2 7 1 3 8 6 5 10
1 2
1 3
2 4
2 5
3 5
3 6
4 7
5 7
5 8
6 8
6 9
7 10
8 9
8 10
9 10
7 9 10

Sample Output 4

18

Sample Input 5

2 1 1
1 10000
1 2
2

Sample Output 5

10000
E - フェンスの塗装

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

配点 : 433 点

問題文

高橋君は N 枚の板からなるフェンスを塗装する仕事を請け負いました。板には 1 から N までの番号が付いており、最初すべての板は未塗装です。

青木君は高橋君に M 回の塗装指示を出します。i 回目 (1 \leq i \leq M) の指示は「板 L_i から板 R_i まで(両端を含む)のすべての板を塗る」というものです。ある板が複数の指示によって塗られる場合も、その板は 1 回塗装されたのと同じ状態になります。

高橋君は体力の都合上、M 回の指示のうち連続するちょうど K 回分の指示を無視しなければなりません。具体的には、1 \leq s \leq M - K + 1 を満たす整数 s を 1 つ選び、s 回目から s + K - 1 回目までの K 回の指示を無視して実行せず、それ以外の M - K 回の指示をすべて実行します。

高橋君は、無視する区間を最適に選ぶことで、最終的に塗装されている板の枚数を最大化したいです。最終的に塗装されている板の枚数の最大値を求めてください。

制約

  • 1 \leq N \leq 10^9
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq K \leq M
  • 1 \leq L_i \leq R_i \leq N (1 \leq i \leq M)
  • 入力はすべて整数である

入力

N M K
L_1 R_1
L_2 R_2
\vdots
L_M R_M

1 行目には、板の枚数 N、指示の回数 M、無視する連続した指示の回数 K がスペース区切りで与えられる。続く M 行のうち i 行目 (1 \leq i \leq M) には、i 回目の指示で塗る範囲の左端 L_i と右端 R_i がスペース区切りで与えられる。

出力

無視する連続した K 回の指示を最適に選んだとき、最終的に塗装されている板の枚数の最大値を整数で出力せよ。


入力例 1

10 4 2
1 3
4 6
2 5
8 10

出力例 1

7

入力例 2

8 5 1
1 4
3 6
5 8
2 2
7 7

出力例 2

8

入力例 3

50 10 3
1 10
5 15
20 25
12 22
30 40
35 45
2 8
18 19
41 50
10 30

出力例 3

50

入力例 4

1000000000 25 7
1 100000
50000 200000
150000 300000
250000 400000
350000 500000
450000 600000
550000 700000
650000 800000
750000 900000
850000 1000000
10000000 20000000
15000000 25000000
24000000 30000000
100000000 200000000
180000000 260000000
250000000 350000000
340000000 420000000
500000000 600000000
590000000 700000000
690000000 800000000
799999999 900000000
850000000 950000000
940000000 1000000000
123456789 123456789
400000000 500000000

出力例 4

920350003

入力例 5

1 1 1
1 1

出力例 5

0

Score : 433 pts

Problem Statement

Takahashi has been hired to paint a fence consisting of N boards. The boards are numbered from 1 to N, and initially all boards are unpainted.

Aoki gives Takahashi M painting instructions. The i-th instruction (1 \leq i \leq M) is "paint all boards from board L_i to board R_i (inclusive)." If a board is painted by multiple instructions, it ends up in the same state as if it were painted once.

Due to his limited stamina, Takahashi must ignore exactly K consecutive instructions out of the M instructions. Specifically, he chooses an integer s satisfying 1 \leq s \leq M - K + 1, ignores the K instructions from the s-th to the (s + K - 1)-th (not executing them), and executes all the remaining M - K instructions.

Takahashi wants to maximize the number of boards that are ultimately painted by optimally choosing the interval of instructions to ignore. Find the maximum number of boards that are ultimately painted.

Constraints

  • 1 \leq N \leq 10^9
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq K \leq M
  • 1 \leq L_i \leq R_i \leq N (1 \leq i \leq M)
  • All inputs are integers

Input

N M K
L_1 R_1
L_2 R_2
\vdots
L_M R_M

The first line contains the number of boards N, the number of instructions M, and the number of consecutive instructions to ignore K, separated by spaces. The i-th (1 \leq i \leq M) of the following M lines contains the left endpoint L_i and right endpoint R_i of the range to paint in the i-th instruction, separated by spaces.

Output

Output the maximum number of boards that are ultimately painted as an integer, when the consecutive K instructions to ignore are chosen optimally.


Sample Input 1

10 4 2
1 3
4 6
2 5
8 10

Sample Output 1

7

Sample Input 2

8 5 1
1 4
3 6
5 8
2 2
7 7

Sample Output 2

8

Sample Input 3

50 10 3
1 10
5 15
20 25
12 22
30 40
35 45
2 8
18 19
41 50
10 30

Sample Output 3

50

Sample Input 4

1000000000 25 7
1 100000
50000 200000
150000 300000
250000 400000
350000 500000
450000 600000
550000 700000
650000 800000
750000 900000
850000 1000000
10000000 20000000
15000000 25000000
24000000 30000000
100000000 200000000
180000000 260000000
250000000 350000000
340000000 420000000
500000000 600000000
590000000 700000000
690000000 800000000
799999999 900000000
850000000 950000000
940000000 1000000000
123456789 123456789
400000000 500000000

Sample Output 4

920350003

Sample Input 5

1 1 1
1 1

Sample Output 5

0