A - 本棚の整理

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

配点 : 200

問題文

高橋君は、自分の本棚を整理しながら、読み直す本を選ぼうとしています。

本棚には N 冊の本があり、i 番目の本(1 \leq i \leq N)には「これまでに読んだ回数」を表す値 C_i と「満足度」を表す値 D_i が記録されています。

高橋君は、これまでに読んだ回数が少ない本を優先的に読み直そうと考えました。具体的には、読んだ回数が K 回以下の本を全て読み直す候補として選び出し、それらの満足度の合計を知りたいと思っています。

N 冊の本の中から、読んだ回数が K 回以下であるものを全て選んだときの、満足度の合計値を求めてください。該当する本が 1 冊もない場合、合計値は 0 とします。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq K \leq 10^9
  • 0 \leq C_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq D_i \leq 10^9 (1 \leq i \leq N)
  • 入力はすべて整数
  • 答えは 2 \times 10^{14} 以下であることが保証される

入力

N K
C_1 D_1
C_2 D_2
\vdots
C_N D_N
  • 1 行目には、本の冊数を表す整数 N と、読んだ回数の上限を表す整数 K が、スペース区切りで与えられる。
  • 続く N 行のうち i 行目(1 \leq i \leq N)には、i 番目の本のこれまでに読んだ回数 C_i と満足度 D_i が、スペース区切りで与えられる。

出力

読んだ回数が K 回以下である本の満足度の合計値を 1 行で出力せよ。該当する本がない場合は 0 を出力せよ。


入力例 1

5 2
1 10
3 20
0 15
2 30
5 25

出力例 1

55

入力例 2

4 1
5 100
3 200
10 50
2 300

出力例 2

0

入力例 3

10 5
3 120
7 250
5 80
0 300
12 60
4 150
6 90
1 200
9 110
5 170

出力例 3

1020

入力例 4

20 100
50 1000000000
101 500000000
99 800000000
200 300000000
0 999999999
100 750000000
150 600000000
30 450000000
75 200000000
110 350000000
1 900000000
88 100000000
100 550000000
250 400000000
99 650000000
1000000000 1000000000
45 700000000
102 850000000
100 500000000
60 300000000

出力例 4

7899999999

入力例 5

1 0
0 1

出力例 5

1

Score : 200 pts

Problem Statement

Takahashi is trying to choose books to reread while organizing his bookshelf.

There are N books on the bookshelf, and for the i-th book (1 \leq i \leq N), a value C_i representing "the number of times it has been read so far" and a value D_i representing "satisfaction" are recorded.

Takahashi decided to prioritize rereading books that he has read fewer times. Specifically, he wants to select all books that have been read K times or fewer as candidates for rereading, and he wants to know the total satisfaction of those books.

From the N books, find the total satisfaction of all books that have been read K times or fewer. If there are no such books, the total is 0.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq K \leq 10^9
  • 0 \leq C_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq D_i \leq 10^9 (1 \leq i \leq N)
  • All inputs are integers
  • It is guaranteed that the answer is at most 2 \times 10^{14}

Input

N K
C_1 D_1
C_2 D_2
\vdots
C_N D_N
  • The first line contains an integer N representing the number of books and an integer K representing the upper limit on the number of times read, separated by a space.
  • In the following N lines, the i-th line (1 \leq i \leq N) contains the number of times the i-th book has been read C_i and its satisfaction D_i, separated by a space.

Output

Output in one line the total satisfaction of books that have been read K times or fewer. If there are no such books, output 0.


Sample Input 1

5 2
1 10
3 20
0 15
2 30
5 25

Sample Output 1

55

Sample Input 2

4 1
5 100
3 200
10 50
2 300

Sample Output 2

0

Sample Input 3

10 5
3 120
7 250
5 80
0 300
12 60
4 150
6 90
1 200
9 110
5 170

Sample Output 3

1020

Sample Input 4

20 100
50 1000000000
101 500000000
99 800000000
200 300000000
0 999999999
100 750000000
150 600000000
30 450000000
75 200000000
110 350000000
1 900000000
88 100000000
100 550000000
250 400000000
99 650000000
1000000000 1000000000
45 700000000
102 850000000
100 500000000
60 300000000

Sample Output 4

7899999999

Sample Input 5

1 0
0 1

Sample Output 5

1
B - 街灯の明るさ

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

配点 : 266

問題文

高橋君は市役所の街灯管理係として働いています。彼が担当する通りには N 個の街灯が一列に並んでおり、左から順に 1, 2, \ldots, N と番号が付けられています。

各街灯 i には初期の明るさを表す整数値 A_i が与えられています。高橋君が街灯 i の整備を行うと、その街灯だけでなく隣接する街灯にも影響が及びます。具体的には、街灯 i-1, i, i+1 のうち番号が 1 以上 N 以下であるものすべての明るさが 1 ずつ増加します。

高橋君は合計で M 回の整備作業を行います。j 回目 (1 \leq j \leq M) の作業では街灯 B_j の整備を行います。なお、同じ街灯に対して複数回整備が行われることもあります。

すべての作業が終わった後の、各街灯の明るさを求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^9
  • 1 \leq B_j \leq N
  • 入力はすべて整数

入力

N M
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M
  • 1 行目には、街灯の個数を表す整数 N と、整備作業の回数を表す整数 M が、スペース区切りで与えられる。
  • 2 行目には、各街灯の初期の明るさを表す整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
  • 3 行目には、各作業で整備を行う街灯の番号を表す整数 B_1, B_2, \ldots, B_M が、スペース区切りで与えられる。

出力

すべての作業が終わった後の各街灯の明るさを、街灯 1 から街灯 N の順にスペース区切りで 1 行で出力せよ。


入力例 1

5 3
1 2 3 4 5
2 4 4

出力例 1

2 3 6 6 7

入力例 2

7 5
0 0 0 0 0 0 0
1 3 5 7 4

出力例 2

1 2 2 3 2 2 1

入力例 3

10 8
100 200 300 400 500 600 700 800 900 1000
1 1 5 5 5 10 3 7

出力例 3

102 203 301 404 503 604 701 801 901 1001

Score : 266 pts

Problem Statement

Takahashi works as a street light manager at the city hall. The street he is responsible for has N street lights arranged in a row, numbered 1, 2, \ldots, N from left to right.

Each street light i is given an integer value A_i representing its initial brightness. When Takahashi performs maintenance on street light i, the effect extends not only to that street light but also to its adjacent street lights. Specifically, the brightness of all street lights among i-1, i, i+1 whose numbers are between 1 and N (inclusive) increases by 1.

Takahashi performs a total of M maintenance operations. In the j-th operation (1 \leq j \leq M), he performs maintenance on street light B_j. Note that the same street light may be maintained multiple times.

Determine the brightness of each street light after all operations are completed.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^9
  • 1 \leq B_j \leq N
  • All input values are integers

Input

N M
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M
  • The first line contains an integer N representing the number of street lights and an integer M representing the number of maintenance operations, separated by a space.
  • The second line contains integers A_1, A_2, \ldots, A_N representing the initial brightness of each street light, separated by spaces.
  • The third line contains integers B_1, B_2, \ldots, B_M representing the street light numbers to be maintained in each operation, separated by spaces.

Output

Print the brightness of each street light after all operations are completed, in order from street light 1 to street light N, separated by spaces, on a single line.


Sample Input 1

5 3
1 2 3 4 5
2 4 4

Sample Output 1

2 3 6 6 7

Sample Input 2

7 5
0 0 0 0 0 0 0
1 3 5 7 4

Sample Output 2

1 2 2 3 2 2 1

Sample Input 3

10 8
100 200 300 400 500 600 700 800 900 1000
1 1 5 5 5 10 3 7

Sample Output 3

102 203 301 404 503 604 701 801 901 1001
C - 花壇の同色チェック

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

配点 : 333

問題文

高橋君は学校の花壇の管理を任されている。花壇には一列に N 個の区画があり、左から i 番目( 1 \leq i \leq N )の区画には色 C_i の花が植えられている。ここで、花の色は正の整数で表される。

青木君は花壇の見栄えを調査するために、高橋君に Q 個の質問をした。花壇において、隣り合う 2 つの区画に同じ色の花が植えられていると、見た目の変化が少なく単調に見えてしまう。そこで青木君は、指定した範囲内に、隣り合う区画の花の色が同じである箇所がいくつあるかを知りたいと考えている。

j 番目( 1 \leq j \leq Q )の質問では、区画 L_j から区画 R_j までの範囲が指定される。この範囲の中で、 L_j \leq i \leq R_j - 1 かつ C_i = C_{i+1} を満たす整数 i の個数を求めてほしい。すなわち、区画 L_j から区画 R_j までの連続した区間に含まれる隣り合う区画の組のうち、花の色が一致しているものの数を答えることになる。

Q 個の質問それぞれに対して答えを求めてください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq C_i \leq 10^91 \leq i \leq N
  • 1 \leq L_j < R_j \leq N1 \leq j \leq Q
  • 入力はすべて整数である。

入力

N Q
C_1 C_2 \ldots C_N
L_1 R_1
L_2 R_2
\vdots
L_Q R_Q
  • 1 行目には、区画の数を表す整数 N と質問の数を表す整数 Q が、スペース区切りで与えられる。
  • 2 行目には、各区画の花の色を表す整数 C_1, C_2, \ldots, C_N が、スペース区切りで与えられる。
  • 続く Q 行のうち j 行目には、 j 番目の質問で指定される範囲の左端 L_j と右端 R_j が、スペース区切りで与えられる。

出力

Q 行にわたって出力してください。 j 行目( 1 \leq j \leq Q )には、 j 番目の質問に対する答え、すなわち L_j \leq i \leq R_j - 1 かつ C_i = C_{i+1} を満たす整数 i の個数を出力してください。


入力例 1

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

出力例 1

3
1
2

入力例 2

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

出力例 2

4
1
2
2
1

入力例 3

15 8
10 10 5 5 5 3 3 7 7 7 7 1 1 2 2
1 15
1 5
5 10
10 15
3 7
2 14
6 8
1 2

出力例 3

9
3
3
3
3
7
1
1

Score : 333 pts

Problem Statement

Takahashi is in charge of managing the school's flower bed. The flower bed has N plots arranged in a row, and the i-th plot from the left (1 \leq i \leq N) has a flower of color C_i planted in it. Here, flower colors are represented by positive integers.

Aoki asked Takahashi Q questions to investigate the appearance of the flower bed. In the flower bed, if two adjacent plots have flowers of the same color, there is little visual variation and it looks monotonous. Therefore, Aoki wants to know how many places within a specified range have adjacent plots with the same flower color.

In the j-th question (1 \leq j \leq Q), a range from plot L_j to plot R_j is specified. Find the number of integers i satisfying L_j \leq i \leq R_j - 1 and C_i = C_{i+1}. In other words, among the pairs of adjacent plots within the contiguous interval from plot L_j to plot R_j, count the number of pairs where the flower colors match.

Find the answer for each of the Q questions.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq C_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq L_j < R_j \leq N (1 \leq j \leq Q)
  • All input values are integers.

Input

N Q
C_1 C_2 \ldots C_N
L_1 R_1
L_2 R_2
\vdots
L_Q R_Q
  • The first line contains the integer N representing the number of plots and the integer Q representing the number of questions, separated by a space.
  • The second line contains the integers C_1, C_2, \ldots, C_N representing the flower color of each plot, separated by spaces.
  • In the following Q lines, the j-th line contains the left endpoint L_j and the right endpoint R_j of the range specified in the j-th question, separated by a space.

Output

Output Q lines. On the j-th line (1 \leq j \leq Q), output the answer to the j-th question, that is, the number of integers i satisfying L_j \leq i \leq R_j - 1 and C_i = C_{i+1}.


Sample Input 1

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

Sample Output 1

3
1
2

Sample Input 2

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

Sample Output 2

4
1
2
2
1

Sample Input 3

15 8
10 10 5 5 5 3 3 7 7 7 7 1 1 2 2
1 15
1 5
5 10
10 15
3 7
2 14
6 8
1 2

Sample Output 3

9
3
3
3
3
7
1
1
D - チームビルディング

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

配点 : 366

問題文

高橋君は、会社のプロジェクトチームを編成する担当者です。

社内には N 人の社員がおり、各社員には 1 から N までの社員番号が付けられています。社員 i の能力値は A_i です。

高橋君は、N 人の中からちょうど K 人の社員を選んでチームを編成することにしました。同じ社員を複数回選ぶことはできず、各社員はチームに選ばれるか選ばれないかのいずれかです。

ただし、社員同士には相性の問題がある場合があります。M 組の「相性の悪いペア」が与えられ、j 番目のペアは社員 U_j と社員 V_j からなります。この 2 人がともにチームに選ばれている場合、連携がうまくいかず、チームの総合力が B_j だけ減少してしまいます。ペアのうち片方のみが選ばれている場合や、どちらも選ばれていない場合には減少は発生しません。

具体的には、選ばれた K 人の社員番号の集合を SS \subseteq \{1, 2, \ldots, N\}, |S| = K)とするとき、チームの総合力は次のように定義されます。

\sum_{i \in S} A_i \ - \sum_{\substack{1 \leq j \leq M \\ U_j \in S \text{ かつ } V_j \in S}} B_j

すなわち、選ばれた K 人の能力値の合計から、両者がともに S に含まれるすべての相性の悪いペアによる減少分の合計を引いた値です。

高橋君がチームの総合力を最大化するように K 人を選ぶとき、チームの総合力の最大値を求めてください。なお、総合力は負になることもありますが、その場合でも最大値をそのまま出力してください。

制約

  • 1 \leq N \leq 18
  • 0 \leq M \leq \frac{N(N-1)}{2}
  • 1 \leq K \leq N
  • 1 \leq A_i \leq 10^9
  • 1 \leq U_j < V_j \leq N
  • 1 \leq B_j \leq 10^9
  • (U_j, V_j) はすべて相異なる
  • 入力はすべて整数

入力

N M K
A_1 A_2 \ldots A_N
U_1 V_1 B_1
U_2 V_2 B_2
\vdots
U_M V_M B_M
  • 1 行目には、社員の数 N 、相性の悪いペアの数 M 、チームに選ぶ社員の数 K が、スペース区切りで与えられる。
  • 2 行目には、各社員の能力値 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
  • 続く M 行(3 行目から M + 2 行目)には、相性の悪いペアの情報が与えられる。M = 0 の場合、この部分は存在しない。
  • 2 + j 行目では、j 番目の相性の悪いペアを構成する社員の番号 U_jV_j 、およびチーム総合力の減少量 B_j が、スペース区切りで与えられる。

出力

チームの総合力の最大値を 1 行で出力してください。


入力例 1

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

出力例 1

16

入力例 2

5 4 3
100 90 80 70 60
1 2 50
1 3 40
2 3 30
4 5 10

出力例 2

220

入力例 3

8 6 4
1000000000 900000000 800000000 700000000 600000000 500000000 400000000 300000000
1 2 500000000
1 3 400000000
2 3 300000000
1 4 200000000
5 6 100000000
7 8 50000000

出力例 3

2700000000

Score : 366 pts

Problem Statement

Takahashi is in charge of forming a project team at his company.

There are N employees in the company, each assigned an employee number from 1 to N. The ability value of employee i is A_i.

Takahashi has decided to select exactly K employees from the N employees to form a team. The same employee cannot be selected more than once, and each employee is either selected for the team or not.

However, there may be compatibility issues between some employees. M "incompatible pairs" are given, where the j-th pair consists of employee U_j and employee V_j. If both of these employees are selected for the team, their coordination suffers and the team's overall strength decreases by B_j. If only one of the pair is selected, or neither is selected, no decrease occurs.

Specifically, let S (S \subseteq \{1, 2, \ldots, N\}, |S| = K) be the set of employee numbers of the K selected employees. The team's overall strength is defined as follows:

\sum_{i \in S} A_i \ - \sum_{\substack{1 \leq j \leq M \\ U_j \in S \text{ and } V_j \in S}} B_j

That is, it is the total ability values of the K selected employees minus the total decrease from all incompatible pairs where both members are included in S.

When Takahashi selects K employees to maximize the team's overall strength, find the maximum value of the team's overall strength. Note that the overall strength may be negative, but even in that case, output the maximum value as is.

Constraints

  • 1 \leq N \leq 18
  • 0 \leq M \leq \frac{N(N-1)}{2}
  • 1 \leq K \leq N
  • 1 \leq A_i \leq 10^9
  • 1 \leq U_j < V_j \leq N
  • 1 \leq B_j \leq 10^9
  • All (U_j, V_j) are distinct
  • All input values are integers

Input

N M K
A_1 A_2 \ldots A_N
U_1 V_1 B_1
U_2 V_2 B_2
\vdots
U_M V_M B_M
  • The first line contains the number of employees N, the number of incompatible pairs M, and the number of employees to select for the team K, separated by spaces.
  • The second line contains the ability values of each employee A_1, A_2, \ldots, A_N, separated by spaces.
  • The following M lines (from the 3rd line to the (M + 2)-th line) contain the information about the incompatible pairs. If M = 0, this part does not exist.
  • The (2 + j)-th line contains the employee numbers U_j and V_j forming the j-th incompatible pair, and the decrease in overall strength B_j, separated by spaces.

Output

Output the maximum value of the team's overall strength in a single line.


Sample Input 1

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

Sample Output 1

16

Sample Input 2

5 4 3
100 90 80 70 60
1 2 50
1 3 40
2 3 30
4 5 10

Sample Output 2

220

Sample Input 3

8 6 4
1000000000 900000000 800000000 700000000 600000000 500000000 400000000 300000000
1 2 500000000
1 3 400000000
2 3 300000000
1 4 200000000
5 6 100000000
7 8 50000000

Sample Output 3

2700000000
E - 配送ルートの最適化

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

配点 : 433

問題文

高橋君は配送ドライバーとして働いています。今日の配送で関係する地点は全部で N 箇所あり、それぞれの地点には 1 から N までの番号が付けられています。地点 1 は配送センター(出発地点兼帰着地点)であり、地点 2 から地点 N までが荷物を届けるべき届け先です。

各地点 i は二次元平面上の座標 (X_i, Y_i) に位置しています。なお、異なる地点が同じ座標に位置することもあります。

高橋君は配送センター(地点 1)からスタートし、すべての届け先(地点 2 から地点 N)をちょうど 1 回ずつ訪問した後、配送センター(地点 1)に戻ってくる巡回ルートを計画しています。すなわち、(2, 3, \ldots, N) の順列 (p_1, p_2, \ldots, p_{N-1})1 つ選び、

1 \to p_1 \to p_2 \to \cdots \to p_{N-1} \to 1

の順に移動します。

高橋君の配送トラックは、座標 (a, b) から座標 (c, d) へ直接移動するときの燃料コストが (a - c)^2 + (b - d)^2 で計算されます。

巡回ルート全体の燃料コストは、ルート上で連続する 2 地点間の移動コストの総和です。具体的には、上の巡回ルートにおける燃料コストは

\mathrm{dist}(1, p_1) + \sum_{k=1}^{N-2} \mathrm{dist}(p_k, p_{k+1}) + \mathrm{dist}(p_{N-1}, 1)

です。ここで \mathrm{dist}(i, j) = (X_i - X_j)^2 + (Y_i - Y_j)^2 は地点 i から地点 j への移動コストを表します。

高橋君は、届け先を訪問する順序を自由に決めることで、巡回ルート全体の燃料コストを最小にしたいと考えています。移動は巡回ルートに対して直接行います。別の地点や適当な座標を経由することはできません。

燃料コストの最小値を求めてください。

制約

  • 2 \leq N \leq 16
  • -1000 \leq X_i \leq 1000 \quad (1 \leq i \leq N)
  • -1000 \leq Y_i \leq 1000 \quad (1 \leq i \leq N)
  • 入力はすべて整数である

入力

N
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N
  • 1 行目には、地点の数を表す整数 N が与えられる。
  • 2 行目から N + 1 行目では、各地点の座標が与えられる。
  • 1 + i 行目では、地点 ix 座標 X_iy 座標 Y_i がスペース区切りで与えられる。

出力

巡回ルート全体の燃料コストの最小値を整数で 1 行に出力せよ。


入力例 1

2
0 0
1 1

出力例 1

4

入力例 2

4
0 0
1 0
1 1
0 1

出力例 2

4

入力例 3

5
0 0
3 0
3 4
-1 3
0 3

出力例 3

46

Score : 433 pts

Problem Statement

Takahashi works as a delivery driver. There are a total of N locations relevant to today's deliveries, each numbered from 1 to N. Location 1 is the distribution center (serving as both the starting point and the return point), and locations 2 through N are the destinations where packages must be delivered.

Each location i is positioned at coordinates (X_i, Y_i) on a two-dimensional plane. Note that different locations may share the same coordinates.

Takahashi plans a tour route starting from the distribution center (location 1), visiting all destinations (locations 2 through N) exactly once each, and then returning to the distribution center (location 1). Specifically, he chooses a permutation (p_1, p_2, \ldots, p_{N-1}) of (2, 3, \ldots, N) and travels in the order:

1 \to p_1 \to p_2 \to \cdots \to p_{N-1} \to 1

The fuel cost for Takahashi's delivery truck to travel directly from coordinates (a, b) to coordinates (c, d) is calculated as (a - c)^2 + (b - d)^2.

The total fuel cost of the tour route is the sum of the travel costs between each pair of consecutive locations along the route. Specifically, the fuel cost of the above tour route is:

\mathrm{dist}(1, p_1) + \sum_{k=1}^{N-2} \mathrm{dist}(p_k, p_{k+1}) + \mathrm{dist}(p_{N-1}, 1)

where \mathrm{dist}(i, j) = (X_i - X_j)^2 + (Y_i - Y_j)^2 represents the travel cost from location i to location j.

Takahashi wants to minimize the total fuel cost of the tour route by freely choosing the order in which he visits the destinations. Travel is done directly along the tour route; he cannot pass through other locations or arbitrary coordinates along the way.

Find the minimum fuel cost.

Constraints

  • 2 \leq N \leq 16
  • -1000 \leq X_i \leq 1000 \quad (1 \leq i \leq N)
  • -1000 \leq Y_i \leq 1000 \quad (1 \leq i \leq N)
  • All input values are integers

Input

N
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N
  • The first line contains an integer N representing the number of locations.
  • Lines 2 through N + 1 give the coordinates of each location.
  • Line 1 + i contains the x-coordinate X_i and the y-coordinate Y_i of location i, separated by a space.

Output

Output the minimum total fuel cost of the tour route as an integer on a single line.


Sample Input 1

2
0 0
1 1

Sample Output 1

4

Sample Input 2

4
0 0
1 0
1 1
0 1

Sample Output 2

4

Sample Input 3

5
0 0
3 0
3 4
-1 3
0 3

Sample Output 3

46