题干:

You are given a sorted array a1,a2,…,ana1,a2,…,an (for each index i>1i>1 condition ai≥ai−1ai≥ai−1holds) 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 ∑i=1k(max(i)−min(i))∑i=1k(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≤k≤n≤3⋅1051≤k≤n≤3⋅105).

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109, ai≥ai−1ai≥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].

题目大意:

给一个有序数组(n=3e5),分成K份,要求每份的最大值减最小值的差最小,求这个差值。

解题报告:

化简个公式然后求相邻点的差的最小值就可以了。取前k-1个,拿个优先队列维护一下。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 4e5 + 5;
int a[MAX],n,k;
priority_queue<int> pq;
ll ans;
int main()
{cin>>n>>k;for(int i = 1; i<=n; i++) cin>>a[i];for(int i = 2; i<=n; i++) pq.push(a[i] - a[i-1]);for(int i = 1; i<=k-1; i++) ans -= pq.top(),pq.pop();ans += a[n]-a[1];cout << ans << endl;return 0 ;
}

【CodeForces - 1197C】Array Splitting(水题)相关推荐

  1. codeforces 1060a(思维水题)

    Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", ...

  2. Codeforces Gym 100286I iSharp 水题

    Problem I. iSharp Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest ...

  3. Eugeny and Array(水题,注意题目描述即可)

    Eugeny has array a = a1, a2, ..., an, consisting of n integers. Each integer ai equals to -1, or to ...

  4. CodeForces - 1287B Hyperset(暴力水题)

    题目链接:点击查看 题目大意:给出"集合"的定义: 集合中必须有三个元素 每个元素的每个特征值必须全部相同或者全部不同,不能存在两者相同但与第三者不同 现在给出n个元素以及m个特征 ...

  5. CodeForces 931C Laboratory Work 水题,构造

    *这种题好像不用写题解... 题意: 一个人要改动别人的实验记录,实验记录记录是一个集合 实验记录本身满足:$max(X)-min(X)<=2$ 改动结果要求: 1.新的集合平均值和之前的一样 ...

  6. 【CodeForces - 1066A~E】水题,模拟(有技巧),思维,题意难懂的模拟,二进制问题(有技巧)

    A. 题目大意: x坐标上1~L有L个点都是整数,每v个长度就有一个灯亮着,但是有 [ l , r ] 这段区间上有列火车挡住了,问你能看到多少亮灯. 解题报告: 大水题啊,找几个样例就会发现需要特殊 ...

  7. 【HDU 5532 Almost Sorted Array】水题,模拟

    给出一个序列(长度>=2),问去掉一个元素后是否能成为单调不降序列或单调不增序列. 对任一序列,先假设其可改造为单调不降序列,若成立则输出YES,不成立再假设其可改造为单调不增序列,若成立则输出 ...

  8. CodeForces - 13A Numbers【水题】

    题目链接:https://codeforces.com/contest/13/problem/A 暴力 #include <iostream> using namespace std; i ...

  9. CodeForces - 1042C (emmmmmm水题)

    You are given an array aa consisting of nn integers. You can perform the following operations with i ...

  10. Educational Codeforces Round 30 A[水题/数组排序]

    A. Chores time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

最新文章

  1. java queue size_Java中的PriorityQueue size() 方法 - Break易站
  2. 小技巧 | 使用 mv 重命名文件无需两次键入文件名称
  3. 免费数学神器有了手机版,再复杂的公式,拍照就能转成LaTeX
  4. js读取服务器txt文件,ZK中使用JS读取客户端txt文件内容问题
  5. 每天练习50个shell
  6. 这11个JavaScript小技巧,你在大多数教程中是找不到的!
  7. cas4.x 单点登录开发入门
  8. 写出一个程序,接受一个正浮点数值,输出该数值的近似整数值。如果小数点后数值大于等于5,向上取整;小于5,则向下取整。
  9. 49个excel常用技巧(一)
  10. Quasi-globally Optimal and Near/True Real-time Vanishing Point Estimation in Manhattan World 论文阅读学习
  11. 完美解决PC电脑0x0和0x800BFA07等错误,成功加入Windows10/11预览体验计划
  12. java红宝石是哪本_AES Java编码,红宝石解码
  13. Java数据类型系列之包装类
  14. 关于echarts自定义合并中国地图分区展示的问题
  15. 手把手教你用Python轻松玩转SQL注入
  16. 精通AngularJS(三)深入scope,继承结构,事件系统和生命周期
  17. 周总结2020-2-16
  18. 老曹眼中的“团队管理”
  19. 360安全卫士,还有这么多,小兄弟?
  20. android开发获取应用本身耗电量_近期值得关注的 iOS、Android 和 PC App

热门文章

  1. 实时修改和读取webconfig
  2. 162. Find Peak Element
  3. 机器学习的一些注意事项
  4. [Leetcode][第174题][JAVA][地下城游戏][DFS][动态规划]
  5. 我的世界javamod怎么装_装暖气片,10个有8个人都想知道的这点事儿!
  6. css加了固定定位就不显示内容_前端开发必备,学好”定位“向菜鸟说拜拜
  7. 怎么查看usb读取信息_电脑弹出USB设备时提示该设备正在使用中的解决方法
  8. MongoClient类参考文档
  9. python123阶乘累加_使用多线程计算阶乘累加 1!+2!+3!+...+19!+20!。其中一个线程计算阶乘,另一线程实现累加并输出结果。...
  10. 鸿蒙是内核名字,华为徐直军:鸿蒙只是内核的名字,是媒体给误解成操作系统...