#include #include #include #include using namespace std; #define endl '\n' #define PB push_back #define ALL(a) (a).begin(),(a).end() #define SZ(a) int((a).size()) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--) #define REP(i,n) FOR(i,0,n) #define RREP(i,n) for (int i=(n)-1;i>=0;i--) #define DEBUG(x) cout<<#x<<": "< P; typedef long long int LL; typedef unsigned long long ULL; typedef pair LP; int dp[3000+5][3000+5][2]; int main() { ios::sync_with_stdio(false); cin.tie(0); int n,m; cin >> n >> m; vector w(n); int ans = 0; if(m == n){ REP(i,n) ans = w[i]; cout << ans << endl; return 0; } REP(i,n) cin >> w[i]; int tmp = INT_MAX; int cu; REP(i,n){ if(tmp > w[i]+w[(i+1)%n]){ cu = i; tmp = w[i]+w[(i+1)%n]; } } //DEBUG(cu); FOR(i,1,n){ dp[0][i][0] = dp[0][i][1] = INT_MIN; } REP(i,n){ dp[i][0][1] = INT_MIN; } FOR(j,1,m+1){ int i = 1; dp[i][j][0] = max(dp[i-1][j][0],dp[i-1][j][1]); dp[i][j][1] = max(dp[i-1][j-1][0],(dp[i-1][j-1][1] == INT_MIN)?INT_MIN:(dp[i-1][j-1][1])); } FOR(i,2,n){ FOR(j,1,m+1){ dp[i][j][0] = max(dp[i-1][j][0],dp[i-1][j][1]); dp[i][j][1] = max(dp[i-1][j-1][0],(dp[i-1][j-1][1] == INT_MIN)?INT_MIN:(dp[i-1][j-1][1]+w[(cu+i)%n])); //DEBUG(w[(cu+i)%n]); } } /*cout << endl; REP(i,n){ REP(j,m+1){ cout << dp[i][j][0] << ' '; } cout << endl; } cout << endl; REP(i,n){ REP(j,m+1){ cout << dp[i][j][1] << ' '; } cout << endl; } cout << endl;*/ ans = max({dp[n-1][m][0],dp[n-1][m][1]}); cout << ans << endl; return 0; }