A - 窓口の受付処理

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

配点 : 233

問題文

高橋君は市役所の総合案内係として働いています。この市役所では、年度末になると手続きの申請が殺到するため、効率的な窓口管理が求められています。

市役所には N 個の窓口があり、窓口には 1 から N までの番号が付いています。各窓口 i (1 \leq i \leq N) には1日の受付上限 C_i が定められており、その日に最大 C_i 件までの申請を受け付けることができます。1日の始まりの時点では、すべての窓口の受付済み件数は 0 です。

ある日、M 件の申請が順番に届きました。j 番目 (1 \leq j \leq M) の申請では、市民が窓口 T_j での手続きを希望しています。

申請は j = 1, 2, \ldots, M の順に1件ずつ処理されます。各申請 j について、以下のように処理が行われます。

  • 希望する窓口 T_j の、その時点での受付済み件数が受付上限 C_{T_j} 未満であれば、その申請は受理され、窓口 T_j の受付済み件数が 1 増えます。
  • 希望する窓口 T_j の、その時点での受付済み件数が受付上限 C_{T_j} に等しい(すなわち受付上限に達している)場合、その申請は却下されます。却下された場合、受付済み件数は変化せず、他の窓口に振り替えられることもありません。

すべての申請を処理した後、受理された申請の総件数を求めてください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq C_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq T_j \leq N (1 \leq j \leq M)
  • 入力はすべて整数である

入力

N M
C_1 C_2 \ldots C_N
T_1
T_2
\vdots
T_M
  • 1 行目には、窓口の数を表す整数 N と、申請の件数を表す整数 M が、スペース区切りで与えられる。
  • 2 行目には、各窓口の1日の受付上限を表す整数 C_1, C_2, \ldots, C_N が、スペース区切りで与えられる。
  • 3 行目から 2 + M 行目までの各行には、申請で希望される窓口の番号が1つずつ与えられる。このうち (2 + j) 行目 (1 \leq j \leq M) には、j 番目の申請で希望される窓口の番号 T_j が与えられる。

出力

受理された申請の総件数を1行で出力してください。


入力例 1

3 5
2 1 3
1
2
1
1
2

出力例 1

3

入力例 2

4 10
3 2 1 4
1
2
3
4
1
2
3
1
4
4

出力例 2

9

入力例 3

5 15
1000000000 1 2 3 1
1
1
1
2
2
3
3
3
4
4
4
4
5
5
1

出力例 3

11

Score : 233 pts

Problem Statement

Takahashi works as a general information clerk at a city hall. At this city hall, applications for procedures flood in at the end of the fiscal year, so efficient counter management is required.

The city hall has N counters, numbered from 1 to N. Each counter i (1 \leq i \leq N) has a daily reception limit C_i, meaning it can accept up to C_i applications per day. At the beginning of the day, the number of accepted applications at every counter is 0.

On a certain day, M applications arrived in order. The j-th application (1 \leq j \leq M) is from a citizen who wishes to have their procedure handled at counter T_j.

The applications are processed one by one in the order j = 1, 2, \ldots, M. Each application j is processed as follows:

  • If the current number of accepted applications at the desired counter T_j is less than the reception limit C_{T_j}, the application is accepted, and the number of accepted applications at counter T_j increases by 1.
  • If the current number of accepted applications at the desired counter T_j is equal to the reception limit C_{T_j} (i.e., the reception limit has been reached), the application is rejected. In the case of rejection, the number of accepted applications does not change, and the application is not redirected to any other counter.

After processing all applications, find the total number of accepted applications.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq C_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq T_j \leq N (1 \leq j \leq M)
  • All input values are integers

Input

N M
C_1 C_2 \ldots C_N
T_1
T_2
\vdots
T_M
  • The first line contains two integers separated by a space: N, the number of counters, and M, the number of applications.
  • The second line contains N integers separated by spaces: C_1, C_2, \ldots, C_N, representing the daily reception limit of each counter.
  • From the 3rd line to the (2 + M)-th line, each line contains the counter number desired in an application. Specifically, the (2 + j)-th line (1 \leq j \leq M) contains T_j, the counter number desired in the j-th application.

Output

Print the total number of accepted applications in a single line.


Sample Input 1

3 5
2 1 3
1
2
1
1
2

Sample Output 1

3

Sample Input 2

4 10
3 2 1 4
1
2
3
4
1
2
3
1
4
4

Sample Output 2

9

Sample Input 3

5 15
1000000000 1 2 3 1
1
1
1
2
2
3
3
3
4
4
4
4
5
5
1

Sample Output 3

11
B - 駅から駅へ

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

配点 : 300

問題文

高橋君は、鉄道路線の調査をしています。この路線には N 個の駅があり、それぞれ 1 から N までの番号が付けられています。

この路線は特殊な構造をしています。駅 i1 \leq i \leq N-1)からは、駅 P_i へ向かう列車だけが運行されています。ここで P_ii とは異なる 1 以上 N 以下の整数です。駅 N は最終目的地であり、駅 N から出発する列車はありません。

高橋君は駅 1 から出発し、各駅で運行されている唯一の列車に乗ることを繰り返して駅 N を目指します。駅 N に到着した時点で移動は終了します。

入力は、駅 1 から上記の移動を繰り返すと、同じ駅を2度訪れることなく有限回の移動で必ず駅 N に到着するように与えられます。

1 から駅 N に到達するまでに訪れる駅の個数を求めてください。出発駅である駅 1 と到着駅である駅 N の両方を個数に含めます。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq P_i \leq N (1 \leq i \leq N-1)
  • P_i \neq i (1 \leq i \leq N-1)
  • 1 から駅 N に、同じ駅を2度訪れることなく到達可能であることが保証される
  • 入力はすべて整数

入力

N
P_1 P_2 \cdots P_{N-1}
  • 1 行目には、駅の数を表す整数 N が与えられる。
  • 2 行目には、N-1 個の整数 P_1, P_2, \ldots, P_{N-1} がスペース区切りで与えられる。P_i は駅 i から列車で向かう次の駅の番号を表す。駅 N から出発する列車はないため、P_N は与えられない。

出力

1 から駅 N に到達するまでに訪れる駅の個数(駅 1 と駅 N を含む)を 1 行で出力せよ。


入力例 1

5
2 4 5 5

出力例 1

4

入力例 2

8
3 5 7 2 8 4 6

出力例 2

8

入力例 3

10
2 3 4 5 6 7 8 9 10

出力例 3

10

Score : 300 pts

Problem Statement

Takahashi is investigating a railway line. This line has N stations, numbered from 1 to N.

This line has a special structure. From station i (1 \leq i \leq N-1), only a train heading to station P_i is operated. Here, P_i is an integer between 1 and N, inclusive, that is different from i. Station N is the final destination, and no trains depart from station N.

Takahashi starts at station 1 and repeatedly boards the only train available at each station, aiming to reach station N. The journey ends when he arrives at station N.

The input is given such that starting from station 1 and repeating the above process, he will always reach station N in a finite number of moves without visiting the same station twice.

Find the number of stations visited from station 1 until reaching station N. Include both the departure station (station 1) and the arrival station (station N) in the count.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq P_i \leq N (1 \leq i \leq N-1)
  • P_i \neq i (1 \leq i \leq N-1)
  • It is guaranteed that station N can be reached from station 1 without visiting the same station twice
  • All input values are integers

Input

N
P_1 P_2 \cdots P_{N-1}
  • The first line contains an integer N, representing the number of stations.
  • The second line contains N-1 integers P_1, P_2, \ldots, P_{N-1} separated by spaces. P_i represents the number of the next station reachable by train from station i. Since no trains depart from station N, P_N is not given.

Output

Print in one line the number of stations visited from station 1 until reaching station N (including both station 1 and station N).


Sample Input 1

5
2 4 5 5

Sample Output 1

4

Sample Input 2

8
3 5 7 2 8 4 6

Sample Output 2

8

Sample Input 3

10
2 3 4 5 6 7 8 9 10

Sample Output 3

10
C - 花壇の水やり

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

配点 : 366

問題文

高橋君は、庭に一列に並んだ N 個の花壇を管理しています。花壇には左から順に 1, 2, \ldots, N の番号が付いており、花壇 i1 \leq i \leq N)の土の水分量は現在 A_i です。

明日は来客があるため、高橋君はすべての花壇の水分量を目標値 T 以上にして、花を美しく咲かせたいと考えています。水分量が T を超えても構いません。

高橋君は以下の操作を好きな回数(0 回でもよい)行うことができます。

  • 操作: 1 \leq l \leq N - K + 1 を満たす整数 l1 つ選び、花壇 l から花壇 l + K - 1 までの連続する K 個の花壇にスプリンクラーで水をまく。これにより、それら K 個の花壇の水分量がそれぞれ 1 増加する。この操作 1 回あたり C 円の水道代がかかる。

各回の操作で選ぶ l の値は自由であり、異なる回の操作で同じ l を選ぶこともできます。

すべての花壇の水分量を T 以上にするために必要な水道代の合計(円)の最小値を求めてください。

制約

  • 1 \leq K \leq N \leq 2 \times 10^5
  • 1 \leq T \leq 10^9
  • 1 \leq C \leq 10^9
  • 1 \leq A_i \leq 10^91 \leq i \leq N
  • 入力はすべて整数
  • 答えは 2^{63} - 1 以下であることが保証される(すなわち、符号付き 64 ビット整数型に収まる)

入力

N K T C
A_1 A_2 \cdots A_N
  • 1 行目には、花壇の個数 N、スプリンクラーで同時に水をまく花壇の個数 K、目標水分量 T1 回の操作あたりの水道代 C(円)が、スペース区切りで与えられる。
  • 2 行目には、各花壇の初期水分量 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。

出力

すべての花壇の水分量を T 以上にするために必要な水道代の合計の最小値(円)を整数で 1 行に出力せよ。


入力例 1

5 3 10 100
8 7 9 12 11

出力例 1

300

入力例 2

8 2 15 50
12 10 14 13 11 15 9 16

出力例 2

750

入力例 3

10 4 20 1000
18 15 12 19 17 14 16 20 13 21

出力例 3

15000

Score : 366 pts

Problem Statement

Takahashi manages N flower beds arranged in a row in his garden. The flower beds are numbered 1, 2, \ldots, N from left to right, and the current moisture level of the soil in flower bed i (1 \leq i \leq N) is A_i.

Since he will have guests tomorrow, Takahashi wants to make all flower beds have a moisture level of at least the target value T so that the flowers bloom beautifully. It is fine if the moisture level exceeds T.

Takahashi can perform the following operation any number of times (possibly 0 times):

  • Operation: Choose an integer l satisfying 1 \leq l \leq N - K + 1, and water the K consecutive flower beds from flower bed l to flower bed l + K - 1 using a sprinkler. This increases the moisture level of each of those K flower beds by 1. Each operation costs C yen in water charges.

The value of l chosen in each operation is arbitrary, and the same value of l may be chosen in different operations.

Find the minimum total water cost (in yen) required to make the moisture level of all flower beds at least T.

Constraints

  • 1 \leq K \leq N \leq 2 \times 10^5
  • 1 \leq T \leq 10^9
  • 1 \leq C \leq 10^9
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • All inputs are integers
  • It is guaranteed that the answer is at most 2^{63} - 1 (i.e., it fits in a signed 64-bit integer type)

Input

N K T C
A_1 A_2 \cdots A_N
  • The first line contains the number of flower beds N, the number of flower beds watered simultaneously by the sprinkler K, the target moisture level T, and the water cost per operation C (yen), separated by spaces.
  • The second line contains the initial moisture levels of each flower bed A_1, A_2, \ldots, A_N, separated by spaces.

Output

Print the minimum total water cost (in yen) required to make the moisture level of all flower beds at least T, as an integer on a single line.


Sample Input 1

5 3 10 100
8 7 9 12 11

Sample Output 1

300

Sample Input 2

8 2 15 50
12 10 14 13 11 15 9 16

Sample Output 2

750

Sample Input 3

10 4 20 1000
18 15 12 19 17 14 16 20 13 21

Sample Output 3

15000
D - 会議室の予約

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

配点 : 400

問題文

高橋君は、会社の会議室の予約管理を担当しています。この会社には会議室が 1 つしかなく、同時に行える会議は最大 1 つだけです。

ある日、 N 件の会議の予約申請が届きました。 i 番目の会議は時刻 L_i に開始し、時刻 R_i に終了します( L_i < R_i )。会議室は 1 つしかないため、時間帯が重なる会議を同時に行うことはできません。

ここで、2 つの会議 ij が「重なる」とは、区間 (L_i, R_i)(L_j, R_j) が共通部分を持つこと、すなわち L_i < R_j かつ L_j < R_i が成り立つことを指します。ある会議の終了時刻と別の会議の開始時刻が一致する場合(例えば R_i = L_j )、それらは重ならないものとし、連続して行うことができます。

高橋君は、できるだけ多くの会議を実施できるようにスケジュールを組みたいと考えています。互いに重ならないように選べる会議の最大数を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq L_i < R_i \leq 10^9
  • 入力はすべて整数である。

入力

N
L_1 R_1
L_2 R_2
:
L_N R_N
  • 1 行目には、会議の予約申請の件数を表す N が与えられる。
  • 2 行目から N + 1 行目では、各会議の開始時刻 L_i と終了時刻 R_i がスペース区切りで与えられる。
  • 1 + i 行目では、 i 番目の会議の開始時刻 L_i と終了時刻 R_i が与えられる。

出力

互いに重ならないように選べる会議の最大数を 1 行で出力せよ。


入力例 1

5
1 5
3 8
1 3
5 8
2 6

出力例 1

2

入力例 2

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

出力例 2

5

入力例 3

12
0 100
50 150
100 200
200 500
150 300
300 600
500 700
600 800
700 800
750 900
1000 1000000000
999999999 1000000000

出力例 3

6

Score : 400 pts

Problem Statement

Takahashi is in charge of managing meeting room reservations at his company. The company has only 1 meeting room, and at most 1 meeting can be held at any given time.

One day, N meeting reservation requests arrived. The i-th meeting starts at time L_i and ends at time R_i (L_i < R_i). Since there is only one meeting room, meetings with overlapping time slots cannot be held simultaneously.

Here, two meetings i and j are said to "overlap" if the intervals (L_i, R_i) and (L_j, R_j) have a common part, that is, if L_i < R_j and L_j < R_i both hold. If the end time of one meeting coincides with the start time of another meeting (for example, R_i = L_j), they are considered non-overlapping and can be held consecutively.

Takahashi wants to schedule as many meetings as possible. Find the maximum number of meetings that can be selected such that no two of them overlap.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq L_i < R_i \leq 10^9
  • All input values are integers.

Input

N
L_1 R_1
L_2 R_2
:
L_N R_N
  • The first line contains N, the number of meeting reservation requests.
  • From the 2nd line to the (N + 1)-th line, the start time L_i and end time R_i of each meeting are given separated by a space.
  • The (1 + i)-th line contains the start time L_i and end time R_i of the i-th meeting.

Output

Print the maximum number of meetings that can be selected such that no two of them overlap, in a single line.


Sample Input 1

5
1 5
3 8
1 3
5 8
2 6

Sample Output 1

2

Sample Input 2

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

Sample Output 2

5

Sample Input 3

12
0 100
50 150
100 200
200 500
150 300
300 600
500 700
600 800
700 800
750 900
1000 1000000000
999999999 1000000000

Sample Output 3

6
E - 展示会の配置

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

配点 : 433

問題文

高橋君は美術館の展示会で、N 個の絵画を一列に並べて展示する担当になりました。

各絵画には 1 から N までの番号が付けられており、絵画 i の色調値は P_i です。絵画を一列に並べたとき、隣り合う絵画の色調値の差が大きいほど、鑑賞者にインパクトを与えることができます。

展示スペースには左から順に N か所の位置があり、それぞれの位置にちょうど 1 つの絵画を配置します。また、隣り合う位置の間には仕切り壁があり、合計 N - 1 か所の仕切り壁があります。左から i 番目の位置と i + 1 番目の位置の間にある仕切り壁には重み W_i が設定されています。重みの大きい壁の前では鑑賞者が立ち止まりやすいため、そこに隣り合って配置された絵画の組のインパクトがより大きく評価されます。

高橋君は N 個の絵画をすべてちょうど 1 回ずつ使い、N か所の位置に配置します。左から i 番目の位置に配置する絵画の番号を Q_i とすると、(Q_1, Q_2, \ldots, Q_N)(1, 2, \ldots, N) の順列になります。このとき、展示全体の評価値は

\sum_{i=1}^{N-1} |P_{Q_i} - P_{Q_{i+1}}| \times W_i

で計算されます。ここで、P_{Q_i} は左から i 番目の位置に配置された絵画 Q_i の色調値を表します。

高橋君は、評価値が最大となるように絵画の配置を決めたいと考えています。N 個の絵画の色調値 P_1, P_2, \ldots, P_N と、N - 1 か所の仕切り壁の重み W_1, W_2, \ldots, W_{N-1} が与えられたとき、すべての配置の中での評価値の最大値を求めてください。

制約

  • 2 \leq N \leq 16
  • 1 \leq P_i \leq 10^7 (1 \leq i \leq N)
  • 1 \leq W_i \leq 10^7 (1 \leq i \leq N-1)
  • 入力はすべて整数である。

入力

N
P_1 P_2 \ldots P_N
W_1 W_2 \ldots W_{N-1}
  • 1 行目には、絵画の個数を表す整数 N が与えられる。
  • 2 行目には、各絵画の色調値を表す N 個の整数 P_1, P_2, \ldots, P_N がスペース区切りで与えられる。P_i は絵画 i の色調値である。
  • 3 行目には、各仕切り壁の重みを表す N - 1 個の整数 W_1, W_2, \ldots, W_{N-1} がスペース区切りで与えられる。W_i は左から i 番目の位置と i + 1 番目の位置の間にある仕切り壁の重みである。

出力

評価値の最大値を整数として 1 行で出力せよ。


入力例 1

3
1 5 3
2 4

出力例 1

20

入力例 2

4
10 10 20 30
1 3 2

出力例 2

110

入力例 3

8
8 1 14 7 20 3 11 17
5 2 9 1 4 8 3

出力例 3

422

入力例 4

16
42 7 81 19 63 5 97 28 54 12 76 33 88 21 69 40
3 11 2 17 5 13 7 19 4 23 6 29 8 31 10

出力例 4

11913

入力例 5

2
1 10000000
10000000

出力例 5

99999990000000

Score : 433 pts

Problem Statement

Takahashi is in charge of displaying N paintings in a row at an art museum exhibition.

Each painting is numbered from 1 to N, and painting i has a tone value of P_i. When the paintings are arranged in a row, the greater the difference in tone values between adjacent paintings, the more impact it gives to the viewers.

The exhibition space has N positions arranged from left to right, and exactly one painting is placed at each position. Between adjacent positions, there are partition walls, totaling N - 1 partition walls. The partition wall between the i-th position from the left and the (i + 1)-th position from the left has a weight W_i. Viewers are more likely to stop in front of walls with larger weights, so the impact of the pair of paintings placed adjacent to such a wall is evaluated more highly.

Takahashi uses all N paintings exactly once and places them at the N positions. Let Q_i denote the number of the painting placed at the i-th position from the left. Then (Q_1, Q_2, \ldots, Q_N) is a permutation of (1, 2, \ldots, N). The overall evaluation score of the exhibition is calculated as

\sum_{i=1}^{N-1} |P_{Q_i} - P_{Q_{i+1}}| \times W_i

where P_{Q_i} represents the tone value of painting Q_i placed at the i-th position from the left.

Takahashi wants to determine the arrangement of paintings that maximizes the evaluation score. Given the tone values P_1, P_2, \ldots, P_N of the N paintings and the weights W_1, W_2, \ldots, W_{N-1} of the N - 1 partition walls, find the maximum evaluation score among all possible arrangements.

Constraints

  • 2 \leq N \leq 16
  • 1 \leq P_i \leq 10^7 (1 \leq i \leq N)
  • 1 \leq W_i \leq 10^7 (1 \leq i \leq N-1)
  • All input values are integers.

Input

N
P_1 P_2 \ldots P_N
W_1 W_2 \ldots W_{N-1}
  • The first line contains an integer N, the number of paintings.
  • The second line contains N integers P_1, P_2, \ldots, P_N separated by spaces, representing the tone values of the paintings. P_i is the tone value of painting i.
  • The third line contains N - 1 integers W_1, W_2, \ldots, W_{N-1} separated by spaces, representing the weights of the partition walls. W_i is the weight of the partition wall between the i-th position from the left and the (i + 1)-th position from the left.

Output

Output the maximum evaluation score as an integer on a single line.


Sample Input 1

3
1 5 3
2 4

Sample Output 1

20

Sample Input 2

4
10 10 20 30
1 3 2

Sample Output 2

110

Sample Input 3

8
8 1 14 7 20 3 11 17
5 2 9 1 4 8 3

Sample Output 3

422

Sample Input 4

16
42 7 81 19 63 5 97 28 54 12 76 33 88 21 69 40
3 11 2 17 5 13 7 19 4 23 6 29 8 31 10

Sample Output 4

11913

Sample Input 5

2
1 10000000
10000000

Sample Output 5

99999990000000