日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Array Splitting CodeForces - 1197C

發(fā)布時(shí)間:2024/4/18 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Array Splitting CodeForces - 1197C 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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 42

Output

12

Input

4 4 1 3 3 7

Output

0

Input

8 1 1 1 2 3 5 8 13 21

Output

20

Note

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位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的Array Splitting CodeForces - 1197C的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。