rt,这个题由于 dp 需要带 16 倍大常数,导致闵和会 T 好多点,自己看了半天也想不到哪能再卡卡了,还是说闵和应该过不了/yun
#include<bits/stdc++.h>
#define ll long long
#define pi pair<ll,ll>
#define vi vector<ll>
#define cpy(x,y,s) memcpy(x,y,sizeof(x[0])*(s))
#define mset(x,v,s) memset(x,v,sizeof(x[0])*(s))
#define all(x) begin(x),end(x)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ary array
#define eb emplace_back
using namespace std;
#define N 2000005
#define inf 0x3f3f3f3f3f3f3f3f
const int SIZE = 1 << 21;
char buf[SIZE], *S, *T;
inline char tgetchar() {
if (S == T) {
T = (S = buf) + fread(buf, 1, SIZE, stdin);
if (S == T) return '\n';
}
return *S++;
}
// namespace Fread
ll read(){
ll x=0,f=1;char ch=tgetchar();
while(ch<'0' || ch>'9')f=(ch=='-'?-1:f),ch=tgetchar();
while(ch>='0' && ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=tgetchar();
return x*f;
}
void write(ll x){
if(x<0)x=-x,putchar('-');
if(x/10)write(x/10);
putchar(x%10+'0');
}
void chkmax(ll &x,ll y){x=(x>y?x:y);}
vi f[2][2][N];
ll tot;
void merge(vi &A,vi &B,vi &C,ll delta,ll op,ll op2){
ll posB=op,posC=op2;
while(posB<B.size() && posC<C.size()){
if(posB+posC+delta>=1)chkmax(A[posB+posC+delta],B[posB]+C[posC]);
if(posB+1<B.size() && (posC+1==C.size() || (C[posC+1]-C[posC]<B[posB+1]-B[posB])))posB++;
else posC++;
}
}
ll a[N];
ll solve(ll l,ll r){
ll p=++tot;
if(l==r){
for(ll i:{0,1})for(ll j:{0,1})f[i][j][p]={0};
f[1][1][p].pb(a[l]);return p;
}
ll mid=(l+r)>>1;
ll ls=solve(l,mid),rs=solve(mid+1,r);
for(ll i:{0,1})for(ll j:{0,1})f[i][j][p].resize((r-l+1)+1,-inf),f[i][j][p][0]=0;
for(ll i:{0,1})for(ll j:{0,1})for(ll i2:{0,1})for(ll j2:{0,1}){
merge(f[i][j2][p],f[i][j][ls],f[i2][j2][rs],((j==i2 && j==1)?-1:0),i|j,j2|i2);
}
for(ll i:{0,1})for(ll j:{0,1})vector<ll>().swap(f[i][j][ls]),vector<ll>().swap(f[i][j][rs]);
return p;
}
int main(){
#ifdef EAST_CLOUD
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
#endif
ll n=read(),k=read();
for(ll i=1;i<=n;i++)a[i]=read();
ll pos=solve(1,n),res=0;
for(ll i:{0,1})for(ll j:{0,1}){
for(int l=0;l<=k;l++)res=max(res,f[i][j][pos][l]);
}
write(res);
return 0;
}