#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 200010
using namespace std;
int n,m;
int a[N];
int be[N];
int af[N];
int hash[N];
struct node
{
int no;
int val;
}tmp[N];
int tot;
priority_queue <node>q;
bool operator < (node s,node t)
{
return s.val<t.val;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
if(i==1)be[i]=n;
else be[i]=i-1;
if(i==n)af[i]=1;
else af[i]=i+1;
scanf("%d",&a[i]);
node tmp;
tmp.no=i,tmp.val=a[i];
q.push(tmp);
}
if(m>(n/2)){puts("Error!");return 0;}
int ans=0;
for(int i=1;i<=m;i++)
{
node u=q.top();
q.pop();
while(hash[u.no])
{
u=q.top();
q.pop();
}
ans+=u.val;
int bex=be[u.no],afx=af[u.no];
hash[bex]=hash[afx]=1;
tot=0;
u.val=a[u.no]=a[bex]+a[afx]-a[u.no];
af[u.no]=af[afx],be[u.no]=be[bex];
be[af[afx]]=u.no,af[be[bex]]=u.no;
q.push(u);
}
printf("%d\n",ans);
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/wzq_qwq/article/details/48366951