#include #include using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf 1000000300 int N,K; vector A; int get(int l,int r,int isL,int now){ if(l<0||r>=N)return -Inf; if(now>K)return -Inf; static vector dp(301,vector(301,vector(2,vector(301,-Inf)))); if(dp[l][r][isL][now]!=-Inf)return dp[l][r][isL][now]; int ret = -Inf+1; if(l==r){ if(now==K)ret = now * A[l]; else ret = -Inf; } else{ if(isL){ ret = max(ret,get(l+1,r,isL,now+1) + A[l] * now); } else{ ret = max(ret,get(l,r-1,isL,now+1) + A[r]*now); } ret = max(ret,get(l,r,isL,now+2)); ret = max(ret,get(l,r,isL^1,now+(r-l))); } dp[l][r][isL][now] = ret; return ret; } int main(){ cin>>N>>K; A.resize(N); rep(i,N)cin>>A[i]; vector ans(N,-Inf); for(int i=0;i=i)ans[j-k] = max(ans[j-k],t); t = get(i,j,1,k); if(i+k<=j)ans[i+k] = max(ans[i+k],t); } } } //cout<