Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 233 点
問題文
高橋君は料理コンテストに参加することになりました。
このコンテストでは、主催者が用意した N 種類の基本レシピがあり、それぞれのレシピには「基本点」と呼ばれる整数値が設定されています。i 番目 (1 \leq i \leq N) の基本レシピの基本点は A_i です。
高橋君は M 品の料理を作る予定です。j 番目 (1 \leq j \leq M) の料理では、基本レシピ B_j をベースにします。なお、異なる料理が同じ基本レシピをベースにしていることもあります。
各料理の得点は、ベースとなる基本レシピの基本点に、高橋君が加えるアレンジ点 S_j を足した値です。すなわち、j 番目の料理の得点は A_{B_j} + S_j です。アレンジ点は負の値をとることもあり、その場合は料理の得点が基本点より低くなります。料理の得点自体が負になることもあり得ます。
コンテストでは、高橋君が作った M 品の料理の得点の合計値によって順位が決まります。
高橋君の料理の得点の合計値を求めてください。なお、合計値は負になることもあります。
制約
- 1 \leq N \leq 10^5
- 1 \leq M \leq 10^5
- 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq B_j \leq N (1 \leq j \leq M)
- -10^9 \leq S_j \leq 10^9 (1 \leq j \leq M)
- 入力はすべて整数
入力
N M A_1 A_2 \ldots A_N B_1 S_1 B_2 S_2 \vdots B_M S_M
- 1 行目には、基本レシピの数 N と、高橋君が作る料理の数 M がスペース区切りで与えられる。
- 2 行目には、各基本レシピの基本点を表す N 個の整数 A_1, A_2, \ldots, A_N がスペース区切りで与えられる。
- 続く M 行にわたって、高橋君が作る各料理の情報が与えられる。
- 2 + j 行目 (1 \leq j \leq M) には、j 番目の料理でベースにする基本レシピの番号 B_j と、その料理に加えるアレンジ点 S_j がスペース区切りで与えられる。
出力
高橋君が作った M 品の料理の得点の合計値を 1 行で出力せよ。
入力例 1
3 2 10 20 30 1 5 3 -10
出力例 1
35
入力例 2
5 4 100 200 300 400 500 2 50 4 -100 1 200 5 0
出力例 2
1350
入力例 3
10 8 1000000000 500000000 300000000 700000000 100000000 900000000 200000000 800000000 600000000 400000000 1 -500000000 6 1000000000 3 -300000000 10 999999999 5 100000000 8 -800000000 2 500000000 7 0
出力例 3
5199999999
Score : 233 pts
Problem Statement
Takahashi is going to participate in a cooking contest.
In this contest, there are N types of basic recipes prepared by the organizers, each of which has an integer value called a "base score." The base score of the i-th (1 \leq i \leq N) basic recipe is A_i.
Takahashi plans to make M dishes. For the j-th (1 \leq j \leq M) dish, he will use basic recipe B_j as the base. Note that different dishes may use the same basic recipe as their base.
The score of each dish is the base score of the underlying basic recipe plus the arrangement score S_j that Takahashi adds. That is, the score of the j-th dish is A_{B_j} + S_j. The arrangement score can be negative, in which case the dish's score will be lower than the base score. It is also possible for the dish's score itself to be negative.
In the contest, Takahashi's ranking is determined by the total score of the M dishes he makes.
Find the total score of Takahashi's dishes. Note that the total score may be negative.
Constraints
- 1 \leq N \leq 10^5
- 1 \leq M \leq 10^5
- 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq B_j \leq N (1 \leq j \leq M)
- -10^9 \leq S_j \leq 10^9 (1 \leq j \leq M)
- All input values are integers
Input
N M A_1 A_2 \ldots A_N B_1 S_1 B_2 S_2 \vdots B_M S_M
- The first line contains the number of basic recipes N and the number of dishes Takahashi makes M, separated by a space.
- The second line contains N integers A_1, A_2, \ldots, A_N representing the base scores of each basic recipe, separated by spaces.
- The following M lines provide information about each dish Takahashi makes.
- The (2 + j)-th line (1 \leq j \leq M) contains the number B_j of the basic recipe used as the base for the j-th dish and the arrangement score S_j added to that dish, separated by a space.
Output
Output the total score of the M dishes Takahashi made, on a single line.
Sample Input 1
3 2 10 20 30 1 5 3 -10
Sample Output 1
35
Sample Input 2
5 4 100 200 300 400 500 2 50 4 -100 1 200 5 0
Sample Output 2
1350
Sample Input 3
10 8 1000000000 500000000 300000000 700000000 100000000 900000000 200000000 800000000 600000000 400000000 1 -500000000 6 1000000000 3 -300000000 10 999999999 5 100000000 8 -800000000 2 500000000 7 0
Sample Output 3
5199999999
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
高橋君の会社には N 台の機器があり、それぞれが電力を消費しています。
i 番目の機器 (1 \leq i \leq N) の現在の消費電力は A_i ワットです。高橋君はこれらの N 台の機器の中から、ちょうど K 台を選んで「省エネモード」に設定しなければなりません。
省エネモードに設定された機器の消費電力は、元の消費電力を半分にした値の小数部分を切り捨てた値になります。すなわち、消費電力が A_i ワットの機器を省エネモードに設定すると、その消費電力は \lfloor A_i / 2 \rfloor ワットになります。ここで \lfloor x \rfloor は x 以下の最大の整数を表します。省エネモードに設定されなかった機器の消費電力は A_i ワットのまま変わりません。
各機器は省エネモードに設定するかしないかのいずれかであり、同じ機器を複数回選ぶことはできません。
高橋君は、省エネモードに設定する K 台の機器をうまく選ぶことで、全機器の消費電力の合計を最小化したいと考えています。
消費電力の合計の最小値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq N
- 1 \leq A_i \leq 10^9
- 入力はすべて整数
入力
N K A_1 A_2 \cdots A_N
- 1 行目には、機器の台数を表す整数 N と、省エネモードに設定する機器の台数を表す整数 K が、空白区切りで与えられる。
- 2 行目には、各機器の消費電力を表す整数 A_1, A_2, \ldots, A_N が、空白区切りで与えられる。
出力
ちょうど K 台の機器を省エネモードに設定したときの、全機器の消費電力の合計の最小値を 1 行で出力せよ。
入力例 1
5 2 10 7 3 8 5
出力例 1
24
入力例 2
4 4 100 50 30 20
出力例 2
100
入力例 3
8 3 1000000000 999999999 500000000 123456789 987654321 111111111 222222222 333333333
出力例 3
2783950614
Score : 300 pts
Problem Statement
Takahashi's company has N devices, each of which consumes electric power.
The current power consumption of the i-th device (1 \leq i \leq N) is A_i watts. Takahashi must select exactly K devices from these N devices and set them to "energy-saving mode".
The power consumption of a device set to energy-saving mode becomes the original power consumption halved, with the fractional part discarded. That is, if a device with power consumption A_i watts is set to energy-saving mode, its power consumption becomes \lfloor A_i / 2 \rfloor watts. Here, \lfloor x \rfloor denotes the largest integer not exceeding x. The power consumption of devices not set to energy-saving mode remains A_i watts.
Each device is either set to energy-saving mode or not, and the same device cannot be selected more than once.
Takahashi wants to minimize the total power consumption of all devices by choosing the K devices to set to energy-saving mode wisely.
Find the minimum total power consumption.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq N
- 1 \leq A_i \leq 10^9
- All inputs are integers
Input
N K A_1 A_2 \cdots A_N
- The first line contains an integer N representing the number of devices and an integer K representing the number of devices to set to energy-saving mode, separated by a space.
- The second line contains integers A_1, A_2, \ldots, A_N representing the power consumption of each device, separated by spaces.
Output
Print in one line the minimum total power consumption of all devices when exactly K devices are set to energy-saving mode.
Sample Input 1
5 2 10 7 3 8 5
Sample Output 1
24
Sample Input 2
4 4 100 50 30 20
Sample Output 2
100
Sample Input 3
8 3 1000000000 999999999 500000000 123456789 987654321 111111111 222222222 333333333
Sample Output 3
2783950614
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 366 点
問題文
高橋君は図書館でアルバイトをしています。図書館には N 冊の本が一列に並んだ本棚があり、高橋君はこの本棚の整理を任されました。
本は左から順に 1 から N までの番号が付いており、本 i( 1 \leq i \leq N )を整理するには A_i 分かかります。高橋君は今日の勤務で使える時間が K 分しかないため、本棚全体を整理することはできないかもしれません。
そこで高橋君は、本棚の中から 連続する区間を1つ 選び、その区間に含まれるすべての本を整理することにしました。具体的には、整数 l, r( 1 \leq l \leq r \leq N )を選び、本 l から本 r までのすべてを整理します。このとき、整理にかかる時間の合計は A_l + A_{l+1} + \cdots + A_r 分です。
高橋君は、整理にかかる時間の合計が K 分以下となるように区間を選びたいと考えています。条件を満たす区間の中で、整理できる本の冊数 r - l + 1 を最大化してください。
すなわち、
A_l + A_{l+1} + \cdots + A_r \leq K
を満たす整数の組 (l, r)( 1 \leq l \leq r \leq N )における r - l + 1 の最大値を求めてください。
ただし、条件を満たす (l, r) が1つも存在しない場合、つまりすべての i( 1 \leq i \leq N )について A_i > K である場合は、0 を出力してください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq 10^{15}
- 1 \leq A_i \leq 10^9( 1 \leq i \leq N )
- 入力はすべて整数
入力
N K A_1 A_2 \cdots A_N
- 1 行目には、本の冊数を表す整数 N と、高橋君が使える時間(分)を表す整数 K が、スペース区切りで与えられる。
- 2 行目には、各本を整理するのにかかる時間(分)を表す N 個の整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
出力
高橋君が整理できる本の最大冊数を 1 行で出力せよ。条件を満たす区間が存在しない場合は 0 を出力せよ。
入力例 1
5 10 3 1 4 1 5
出力例 1
4
入力例 2
8 15 5 2 6 3 1 2 4 7
出力例 2
5
入力例 3
10 1000000000000 500000000 200000000 300000000 100000000 400000000 600000000 150000000 250000000 350000000 450000000
出力例 3
10
Score : 366 pts
Problem Statement
Takahashi works part-time at a library. The library has a bookshelf with N books arranged in a row, and Takahashi has been assigned to organize this bookshelf.
The books are numbered from 1 to N from left to right, and organizing book i (1 \leq i \leq N) takes A_i minutes. Since Takahashi only has K minutes available for today's shift, he may not be able to organize the entire bookshelf.
Therefore, Takahashi has decided to choose one contiguous interval from the bookshelf and organize all the books within that interval. Specifically, he chooses integers l, r (1 \leq l \leq r \leq N) and organizes all books from book l to book r. The total time required for organizing is A_l + A_{l+1} + \cdots + A_r minutes.
Takahashi wants to choose an interval such that the total organizing time is at most K minutes. Among all intervals satisfying this condition, maximize the number of books organized, r - l + 1.
In other words, find the maximum value of r - l + 1 over all integer pairs (l, r) (1 \leq l \leq r \leq N) satisfying
A_l + A_{l+1} + \cdots + A_r \leq K
However, if no such (l, r) exists, that is, if A_i > K for all i (1 \leq i \leq N), output 0.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq 10^{15}
- 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
- All input values are integers
Input
N K A_1 A_2 \cdots A_N
- The first line contains an integer N representing the number of books and an integer K representing the time (in minutes) available to Takahashi, separated by a space.
- The second line contains N integers A_1, A_2, \ldots, A_N representing the time (in minutes) required to organize each book, separated by spaces.
Output
Output the maximum number of books Takahashi can organize in a single line. If no interval satisfying the condition exists, output 0.
Sample Input 1
5 10 3 1 4 1 5
Sample Output 1
4
Sample Input 2
8 15 5 2 6 3 1 2 4 7
Sample Output 2
5
Sample Input 3
10 1000000000000 500000000 200000000 300000000 100000000 400000000 600000000 150000000 250000000 350000000 450000000
Sample Output 3
10
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋君は、庭に一列に並んだ N 本の花の手入れを担当しています。花には左から順に番号 1, 2, \ldots, N が付いており、花 i の高さは A_i です。
高橋君は、これらの花を K 人の友人に分担して水やりを頼むことにしました。分担は以下のルールに従います:
- N 本の花を、並んでいる順番を保ったまま、ちょうど K 個の空でない連続した区間に分割する。すなわち、0 = r_0 < r_1 < r_2 < \cdots < r_K = N を満たす整数列 r_0, r_1, \ldots, r_K を選び、j 番目 (1 \leq j \leq K) の友人は花 r_{j-1}+1 から花 r_j までを担当する。
- 各区間の 手間 は、その区間に含まれる花の高さの最大値と最小値の差として定義される。区間に花が 1 本だけ含まれる場合、手間は 0 である。
- K 個の区間の手間の合計を 総手間 と呼ぶ。
高橋君は、総手間をできるだけ小さくするように区間分けを行いたいと考えています。
総手間の最小値を求めてください。
制約
- 1 \leq K \leq N \leq 200
- 1 \leq A_i \leq 1000
- 入力はすべて整数である。
入力
N K A_1 A_2 \ldots A_N
- 1 行目には、花の本数を表す整数 N と、分割する区間の数を表す整数 K が、スペース区切りで与えられる。
- 2 行目には、花 1, 2, \ldots, N の高さを表す N 個の整数 A_1, A_2, \ldots, A_N がスペース区切りで与えられる。
出力
総手間の最小値を 1 行で出力せよ。
入力例 1
5 2 3 1 4 1 5
出力例 1
3
入力例 2
6 3 10 1 10 1 10 1
出力例 2
9
入力例 3
10 4 5 8 3 7 2 9 1 6 4 10
出力例 3
8
入力例 4
20 5 15 3 12 7 20 1 18 5 14 9 2 16 8 11 4 19 6 13 10 17
出力例 4
19
入力例 5
1 1 42
出力例 5
0
Score : 400 pts
Problem Statement
Takahashi is in charge of caring for N flowers arranged in a row in his garden. The flowers are numbered 1, 2, \ldots, N from left to right, and the height of flower i is A_i.
Takahashi has decided to ask K friends to share the task of watering the flowers. The assignment follows these rules:
- Divide the N flowers into exactly K non-empty contiguous intervals while preserving their original order. That is, choose an integer sequence r_0, r_1, \ldots, r_K satisfying 0 = r_0 < r_1 < r_2 < \cdots < r_K = N, where the j-th friend (1 \leq j \leq K) is responsible for flowers from r_{j-1}+1 to r_j.
- The effort of each interval is defined as the difference between the maximum and minimum heights of the flowers contained in that interval. If an interval contains only one flower, the effort is 0.
- The sum of the efforts of the K intervals is called the total effort.
Takahashi wants to partition the flowers so that the total effort is as small as possible.
Find the minimum value of the total effort.
Constraints
- 1 \leq K \leq N \leq 200
- 1 \leq A_i \leq 1000
- All inputs are integers.
Input
N K A_1 A_2 \ldots A_N
- The first line contains an integer N representing the number of flowers and an integer K representing the number of intervals, separated by a space.
- The second line contains N integers A_1, A_2, \ldots, A_N representing the heights of flowers 1, 2, \ldots, N, separated by spaces.
Output
Print the minimum value of the total effort on a single line.
Sample Input 1
5 2 3 1 4 1 5
Sample Output 1
3
Sample Input 2
6 3 10 1 10 1 10 1
Sample Output 2
9
Sample Input 3
10 4 5 8 3 7 2 9 1 6 4 10
Sample Output 3
8
Sample Input 4
20 5 15 3 12 7 20 1 18 5 14 9 2 16 8 11 4 19 6 13 10 17
Sample Output 4
19
Sample Input 5
1 1 42
Sample Output 5
0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 433 点
問題文
高橋君は宅配ドライバーとして働いており、毎日お客様のもとへ荷物を届けています。
高橋君が担当するエリアは N 個の交差点と M 本の道路からなっています。交差点には 1 から N までの番号が付けられています。 i 番目 (1 \leq i \leq M) の道路は交差点 U_i と交差点 V_i を双方向に結んでおり、どちらの方向に通過しても W_i 分かかります。同じ 2 つの交差点を結ぶ道路が複数存在することもあります。
今日は K 件の配達依頼があり、 j 番目 (1 \leq j \leq K) の依頼では交差点 D_j にいるお客様に荷物を届ける必要があります。 K 件の配達先の交差点はすべて異なり、いずれも営業所のある交差点 S とは異なります。高橋君は営業所のある交差点 S から出発し、 K 件すべての配達先を訪れた後、再び交差点 S に戻ってこなければなりません。配達先を訪れる順番は自由に決めることができます。また、移動の途中で同じ交差点や同じ道路を何度通ってもかまいません。
ある配達先への配達は、移動経路の途中であっても、その交差点を通った時点で自動的に完了するものとします。すなわち、ある配達先に向かう途中で別の配達先を経由した場合、その配達先への配達も完了します。荷物の受け渡しにかかる時間は考えないものとします。
すべての配達を終えて営業所に戻るまでの最小の合計移動時間(分)を求めてください。
なお、交差点 S からすべての配達先へ到達可能であることが保証されます(道路は双方向なので、各配達先から交差点 S に戻ることも可能です)。
制約
- 2 \leq N \leq 10\,000
- 1 \leq M \leq 20\,000
- 1 \leq U_i, V_i \leq N
- U_i \neq V_i
- 1 \leq W_i \leq 10^6
- 1 \leq S \leq N
- 1 \leq K \leq 15
- 1 \leq D_j \leq N
- D_j はすべて異なる
- D_j \neq S
- すべての配達先へ S から到達可能である
- 入力はすべて整数である
入力
N M U_1 V_1 W_1 U_2 V_2 W_2 \vdots U_M V_M W_M S K D_1 D_2 \ldots D_K
- 1 行目には、交差点の数 N と道路の数 M がスペース区切りで与えられる。
- 続く M 行の i 行目 (1 \leq i \leq M) には、 i 番目の道路が結ぶ 2 つの交差点の番号 U_i, V_i と、通過にかかる時間 W_i (分)がスペース区切りで与えられる。
- 次の行には、営業所のある交差点の番号 S と配達件数 K がスペース区切りで与えられる。
- 次の行には、 K 個の配達先の交差点番号 D_1, D_2, \ldots, D_K がスペース区切りで与えられる。
出力
すべての配達先を訪れて営業所に戻るまでの最小の合計移動時間(分)を整数で 1 行に出力せよ。
入力例 1
4 5 1 2 2 2 3 3 3 4 4 1 4 7 1 3 10 1 2 3 4
出力例 1
16
入力例 2
6 8 1 2 3 1 3 5 2 3 1 2 4 7 3 5 4 4 5 2 4 6 3 5 6 6 1 3 4 5 6
出力例 2
26
入力例 3
10 14 1 2 5 1 3 12 2 3 4 2 4 8 3 5 3 4 5 6 4 6 2 5 7 7 6 7 4 6 8 9 7 9 3 8 9 5 8 10 2 9 10 6 1 5 3 5 7 9 10
出力例 3
54
Score : 433 pts
Problem Statement
Takahashi works as a delivery driver, delivering packages to customers every day.
The area Takahashi is responsible for consists of N intersections and M roads. The intersections are numbered from 1 to N. The i-th road (1 \leq i \leq M) bidirectionally connects intersection U_i and intersection V_i, and it takes W_i minutes to traverse in either direction. There may be multiple roads connecting the same pair of intersections.
Today there are K delivery requests. The j-th request (1 \leq j \leq K) requires delivering a package to a customer at intersection D_j. All K delivery destination intersections are distinct, and none of them is the same as intersection S where the office is located. Takahashi must depart from intersection S where the office is located, visit all K delivery destinations, and then return to intersection S. He is free to choose the order in which he visits the delivery destinations. He may pass through the same intersection or the same road any number of times during his travel.
A delivery to a destination is automatically completed the moment Takahashi passes through that intersection, even if it is in the middle of traveling to another destination. In other words, if he passes through a delivery destination while heading to a different one, the delivery to that destination is also completed. The time for handing over packages is negligible.
Find the minimum total travel time (in minutes) to complete all deliveries and return to the office.
It is guaranteed that all delivery destinations are reachable from intersection S (since the roads are bidirectional, it is also possible to return from each delivery destination to intersection S).
Constraints
- 2 \leq N \leq 10\,000
- 1 \leq M \leq 20\,000
- 1 \leq U_i, V_i \leq N
- U_i \neq V_i
- 1 \leq W_i \leq 10^6
- 1 \leq S \leq N
- 1 \leq K \leq 15
- 1 \leq D_j \leq N
- All D_j are distinct
- D_j \neq S
- All delivery destinations are reachable from S
- All input values are integers
Input
N M U_1 V_1 W_1 U_2 V_2 W_2 \vdots U_M V_M W_M S K D_1 D_2 \ldots D_K
- The first line contains the number of intersections N and the number of roads M, separated by a space.
- The following M lines each contain, for the i-th road (1 \leq i \leq M), the numbers of the two intersections U_i, V_i connected by the road and the travel time W_i (in minutes), separated by spaces.
- The next line contains the intersection number S where the office is located and the number of deliveries K, separated by a space.
- The next line contains the K delivery destination intersection numbers D_1, D_2, \ldots, D_K, separated by spaces.
Output
Output in a single line the minimum total travel time (in minutes) as an integer to visit all delivery destinations and return to the office.
Sample Input 1
4 5 1 2 2 2 3 3 3 4 4 1 4 7 1 3 10 1 2 3 4
Sample Output 1
16
Sample Input 2
6 8 1 2 3 1 3 5 2 3 1 2 4 7 3 5 4 4 5 2 4 6 3 5 6 6 1 3 4 5 6
Sample Output 2
26
Sample Input 3
10 14 1 2 5 1 3 12 2 3 4 2 4 8 3 5 3 4 5 6 4 6 2 5 7 7 6 7 4 6 8 9 7 9 3 8 9 5 8 10 2 9 10 6 1 5 3 5 7 9 10
Sample Output 3
54