【CodeForces - 1197C】Array Splitting(水题)

题干:

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 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440038.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Django项目部署在Centos7

把Django项目部署在Centos7下 先有一个Django项目 一个员工管理系统&#xff08;ems&#xff09; 员工管理系统 使用VMware创建一个虚拟的Linux系统 Centos7下安装MySql5.7 详细安装 Centos7下安装Python3.5 详细安装 Centos7下安装Django2.0.6 - 安装数据库驱动&#xf…

(7)树

目录 树的知识点&#xff1a; 定义&#xff1a; 专业术语&#xff1a; 操作(重点): 树的操作&#xff1a; 创建二叉树&#xff1a; 先序遍历&#xff1a; 中序遍历&#xff1a; 后序遍历&#xff1a; 这篇笔记是根据郝斌老师的上课讲义整理而得。 树的知识点&#xf…

由浅到深理解ROS(5)- launch启动文件的理解与编写

ROS提供了一个同时启动节点管理器&#xff08;master&#xff09;和多个节点的途径&#xff0c;即使用启动文件&#xff08;launch file&#xff09;。事实上&#xff0c;在ROS功能包中&#xff0c;启动文件的使用是非常普遍的。任何包含两个或两个以上节点的系统都可以利用启动…

【CodeForces - 1199C】MP3(思维,离散化)

题干&#xff1a; One common way of digitalizing sound is to record sound intensity at particular time moments. For each time moment intensity is recorded as a non-negative integer. Thus we can represent a sound file as an array of nn non-negative integers…

Centos7下安装MySql

1、安装方式 1.1 方式一 yum安装&#xff08;推荐使用该方式&#xff09; 在CentOS中默认安装有MariaDB&#xff0c;这个是MySQL的分支 如果必须要安装MySQL&#xff0c;首先必须添加mysql社区repo通过输入命令&#xff1a; rpm -ivh http://dev.mysql.com/get/mysql-commu…

(8)排序

排序&#xff1a; 排序和查找的关系&#xff1a;排序是查找的前提&#xff0c;排序是重点。 冒泡 插入 选择 归并排序 快速排序&#xff1a;一次确定一个数的位置 #include <stdio.h>void QuickSort(int *a, int low, int high) {int pos;if(low < high){pos FindP…

由浅到深理解ROS(6)-坐标转换

转自 ROS 中对于多坐标系的处理是使用树型表示&#xff0c;在机器人自主导航中&#xff0c;ROS会构建这几个很重要的坐标系&#xff1a; base_link: 一般位于tf tree的最根部&#xff0c;物理语义原点一般为表示机器人中心&#xff0c;为相对机器人的本体的坐标系。(base_foot…

Centos7下安装Python3.5

1、安装依赖 - yum -y install python-devel openssl-devel bzip2-devel zlib-devel expat-devel ncurses-devel sqlite-devel gdbm-devel xz-devel tk-devel readline-devel gcc - yum -y groupinstall "Development tools"如上两步&#xff0c;汇总安装了p…

【BZOJ - 3450】Tyvj1952 Easy(数学期望,期望的线性性)

题干&#xff1a; 某一天WJMZBMR在打osu~~~但是他太弱逼了&#xff0c;有些地方完全靠运气:( 我们来简化一下这个游戏的规则 有n次点击要做&#xff0c;成功了就是o&#xff0c;失败了就是x&#xff0c;分数是按comb计算的&#xff0c;连续a个comb就有a*a分&#xff0c;comb就…

吐血推荐收藏的学位论文排版教程(完整版)

目录 01.保存的高级选项设置 02.纸张设置为A4纸大小 03.设置页边距和装订线距离 04.度量衡的设置 05.创建“论文正文”样式 06.修改论文正文样式 07.设置并修改标题样式 08.给中英文摘要、附录、等大标题套用样式 09.两个Word文档之间进行并排查看 12.一键生成多级列…

由浅到深理解ROS(7)-URDF

转自 相信许多爱好者止步于昂贵的机器人价格。虽然有了Arduino&#xff0c;但一个载重能力很弱的机器人小车成本也得一两百块钱。搭建自己的机器人更是费时费力。 所以如果你只是普通机器人爱好者&#xff0c;或者还没想好要拿机器人做什么&#xff0c;那我们还是从模拟器开始…

Apollo进阶课程 ① | 带你纵览无人车

目录 1&#xff09;无人驾驶级别的分类 2&#xff09;无人驾驶技术的关键点 3&#xff09;火热的无人驾驶 不缺独角兽 原文链接&#xff1a;Apollo进阶课程 ① | 带你纵览无人车 Apollo自动驾驶进阶课程是由百度Apollo联合北京大学共同开设的课程&#xff0c;邀请百度Apoll…

linux基本简介

Linux基础知识点 Linux是一套免费使用的类Unix操作系统&#xff0c;继承了Unix一网络为核心的设计思想&#xff0c;是一个性能稳定的多用户操作系统。 特点&#xff1a;免费、安全、可靠、稳定、多平台 Linux的目录结构 bin &#xff1a;存放二进制可执行文件sbin&#xff1…

【BZOJ - 4318】OSU!(概率dp,数学期望,期望的线性性)

题干&#xff1a; osu 是一款群众喜闻乐见的休闲软件。 我们可以把osu的规则简化与改编成以下的样子: 一共有n次操作&#xff0c;每次操作只有成功与失败之分&#xff0c;成功对应1&#xff0c;失败对应0&#xff0c;n次操作对应为1个长度为n的01串。在这个串中连续的 X个…

word2vec原理详解及实战

目录 1&#xff09;前言 1.1 语言模型 1.2N-gram模型 1.3词向量表示 2&#xff09;预备知识 2.1 sigmoid函数 2.2 逻辑回归 2.3贝叶斯公式 2.4 Huffman编码 3&#xff09;神经网络概率语言模型 4&#xff09;基于Hierarchial Sodtmax模型 4.1CBOW模型 4.2 Skip-gr…

由浅到深理解ROS(8)-线程管理

转自 单线程Spinning ros::spin()是最简单的单线程自旋, 它会一直调用直到结束 用法: ros::spin(); 另一个单线程spinning是ros::spinOnce(),它定期调用等待在那个点上的所有回调 用法: ros::spinOnce(); 简单的我们自己实现一个用法相同的ros::spin() 这样: ros::get…

计算机网络通讯协议

网络通讯&#xff1a; 就是要把特定意义的数据通过物理介质传送给对方。把电信号变成有意义的数据&#xff1a; 以字节为单位分组&#xff0c;标识好每一组电信号的信息特征&#xff0c;按照分组的顺序来依次发送。 以太网规定&#xff1a;一组电信号为一个数据包&#xff0c…

【CodeForces - 518D】Ilya and Escalator(概率dp,数学期望)

题干&#xff1a; Ilya got tired of sports programming, left university and got a job in the subway. He was given the task to determine the escalator load factor. Lets assume that n people stand in the queue for the escalator. At each second one of the tw…

Apollo进阶课程 ② | 开源模块讲解(上)

目录 1&#xff09;无人驾驶车介绍 2&#xff09;高精地图 3&#xff09;定位 4&#xff09;感知 5&#xff09;轨迹规划 6&#xff09;控制 7&#xff09;云端 原文链接&#xff1a;Apollo进阶课程 ② | 开源模块讲解&#xff08;上&#xff09; Apollo自动驾驶进阶课…

由浅到深理解ROS(9)- 几个基本概念的理解 坐标系 包

1.坐标系 最常用的就是map&#xff0c;odom&#xff0c;base_link&#xff0c;base_laser坐标系&#xff0c;这也是开始接触gmapping的一些坐标系。 map:地图坐标系&#xff0c;顾名思义&#xff0c;一般设该坐标系为固定坐标系&#xff08;fixed frame&#xff09;&#xff…