Array Splitting CodeForces - 1197C
You are given a?sorted?array?a_1, a_2, \dots, a_na1?,a2?,…,an??(for each index?i > 1i>1?condition?a_i \ge a_{i-1}ai?≥ai?1??holds) and an integer?kk.
You are asked to divide this array into?kk?non-empty consecutive subarrays. Every element in the array should be included in exactly one subarray.
Let?max(i)max(i)?be equal to the maximum in the?ii-th subarray, and?min(i)min(i)?be equal to the minimum in the?ii-th subarray. The cost of division is equal to?\sum\limits_{i=1}^{k} (max(i) - min(i))i=1∑k?(max(i)?min(i)). For example, if?a = [2, 4, 5, 5, 8, 11, 19]a=[2,4,5,5,8,11,19]?and we divide it into?33?subarrays in the following way:?[2, 4], [5, 5], [8, 11, 19][2,4],[5,5],[8,11,19], then the cost of division is equal to?(4 - 2) + (5 - 5) + (19 - 8) = 13(4?2)+(5?5)+(19?8)=13.
Calculate the minimum cost you can obtain by dividing the array?aa?into?kk?non-empty consecutive subarrays.
Input
The first line contains two integers?nn?and?kk?(1 \le k \le n \le 3 \cdot 10^51≤k≤n≤3?105).
The second line contains?nn?integers?a_1, a_2, \dots, a_na1?,a2?,…,an??(1 \le a_i \le 10^91≤ai?≤109,?a_i \ge a_{i-1}ai?≥ai?1?).
Output
Print the minimum cost you can obtain by dividing the array?aa?into?kk?nonempty consecutive subarrays.
Examples
Input
6 3 4 8 15 16 23 42Output
12Input
4 4 1 3 3 7Output
0Input
8 1 1 1 2 3 5 8 13 21Output
20Note
In the first test we can divide array?aa?in the following way:?[4, 8, 15, 16], [23], [42][4,8,15,16],[23],[42].
#include<iostream> using namespace std; #include<string> #include<algorithm> #pragma warning (disable:4996) #include <climits> #include <vector> int a[1000000]; int d[1000000]; bool cmp(int a, int b); int main() {int n, k;cin >> n >> k;int sum = 0;for (int cnt = 0; cnt < n; cnt++) {scanf("%d" , &a[cnt]);}d[0] = 0;for (int cnt = 1; cnt < n; cnt++) {d[cnt] = a[cnt] - a[cnt - 1];sum += d[cnt];}sort(d, d + n, cmp);for (int cnt = 0; cnt < k - 1; cnt++) {sum -= d[cnt];}cout << sum;return 0; } bool cmp(int a, int b) {return a > b; } 與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的Array Splitting CodeForces - 1197C的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java学习笔记2022.1.12
- 下一篇: java学习笔记2022.1.13