You are given an array 1,2,…, and an integer

.

You are asked to divide this array into

non-empty consecutive subarrays. Every element in the array should be included in exactly one subarray. Let () be the index of subarray the -th element belongs to. Subarrays are numbered from left to right and from 1 to

.

Let the cost of division be equal to ∑=1(⋅())

. For example, if =[1,−2,−3,4,−5,6,−7] and we divide it into 3 subbarays in the following way: [1,−2,−3],[4,−5],[6,−7], then the cost of division is equal to 1⋅1−2⋅1−3⋅1+4⋅2−5⋅2+6⋅3−7⋅3=−9

.

Calculate the maximum cost you can obtain by dividing the array

into

non-empty consecutive subarrays.

Input

The first line contains two integers

and (1≤≤≤3⋅105

).

The second line contains

integers 1,2,…, (||≤106

).

Output

Print the maximum cost you can obtain by dividing the array

into

nonempty consecutive subarrays.

Examples
Input

Copy

5 2
-1 -2 5 -4 8

Output

Copy

15

Input

Copy

7 6
-3 0 -1 -2 -2 -4 -1

Output

Copy

-45

Input

Copy

4 1
3 -1 6 0

Output

Copy

8

题解:将n个数的数组分成k个连续的子数组,并且第i个子数组的权值为i,则我们可以用后缀和。首先我们一定每个数都得至少取一次,则我么一定要取a[1],然后将后面的n-1个数排序,
因为题目要求获得答案最大,并且这后n-1个数我们任意取k-1个都能保证符合题意中的分法,则为了保证答案最大我们就要取比较大的前k-1个了~~
#include<bits/stdc++.h>
#include<iostream>
#include<stdio.h>
#include<iomanip>
#include<stack>
#include<queue>
#include<algorithm>
#include<cstring>
#include<map>
#include<vector>
#include<numeric>
#include<iterator>
#include<cmath>
#define mem(a,x) memset(a,x,sizeof(a));
using namespace std;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b); }
int lcm(int a, int b) { return a * b / gcd(a, b); }
const int INF = 0x3f3f3f3f;
typedef long long ll;
const int mod=1000000007;
typedef pair<int,int>Pi;
typedef pair<ll, ll>Pii;
map<int,int>mp;
map<int, char *>mp1;
map<char *, int>mp2;
map<char, int>mp3;
map<string,int>mp4;
map<char,int>mp5;
const int maxn = 300010;
ll a[maxn]; int read(){
int flag=1;
int sum=0;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-')flag=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
sum=sum*10+c-'0';
c=getchar();
}
return sum*flag;
}
ll Read(){
int flag=1;
ll sum=0;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-')flag=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
sum=sum*10+c-'0';
c=getchar();
}
return sum*flag;
}
ll quickmul(ll a,ll b){
ll ans=0;
while(b){
if(b&1){
ans=(ans+a)%mod;
}
a=(a+a)%mod;
b>>=1;
}
return ans;
}
ll quickpow(ll a,ll b){
ll ans=1;
while(b){
if(b&1)
ans=(ans*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return ans;
}
int main()
{
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=n;i>=1;i--)a[i]+=a[i+1];
sort(a+2,a+1+n,greater<ll>());
ll ans=0;
for(int i=1;i<=k;i++)ans+=a[i];
cout<<ans<<endl;
return 0;
}

最新文章

  1. 我为开源做贡献,网页正文提取——Html2Article
  2. 使用Topshelf快速搭建Windows服务
  3. css盒子模型 padding
  4. mysql 外键(FOREIGN KEY)
  5. JDE客户端get时报错“ERROR:fetch from table F0101 failed”
  6. C#根据日期DateTime和持续时间int找到日期
  7. iOS之正则表达式的使用
  8. IIS 64位上發佈32位asp.net設置
  9. 转载 Deep learning:六(regularized logistic回归练习)
  10. ASP.NET/MVC 配置log4net启用写错误日志功能
  11. lucene搜索之高级查询
  12. bat如何提取文本指定行的内容
  13. nim博弈
  14. Sql Server tempdb原理-启动过程解析实践
  15. java基础-引用数据类型之二维数组(Array)
  16. C#基础知识-使用XML完成一个小程序(十一)
  17. 算法与数据结构5.2 Bubble Sort
  18. 巨蟒python全栈开发数据库前端1:HTML基础
  19. redis启动后出现&quot;WARNING you have Transparent Huge Pages (THP) support enabled in your kernel&quot;问题
  20. HDOJ1073(gets 应用)

热门文章

  1. python 简单字符串字典加密
  2. Vue v-bind
  3. 图片分割之GrabCut算法、分水岭算法
  4. 目录服务不能与此服务器复制,因为距上一次与此服务器复制的时间已经超过了 tombstone 生存时间。
  5. [转]java 的HashMap底层数据结构
  6. JavaScript 2019.3.15
  7. tensorflow 读取训练集文件 from Hadoop
  8. DFS判断连通图
  9. YouTube推出慈善组合工具,能引国内视频网站跟风吗?
  10. awk grep sed 的一些问题