#define _USE_MATH_DEFINES #include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; constexpr int INF = 0x3f3f3f3f; constexpr ll LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr double EPS = 1e-8; constexpr int MOD = 1000000007; // constexpr int MOD = 998244353; constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1}; template inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; } template inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(20); } } iosetup; int main() { int n, k; cin >> n >> k; vector a(n); REP(i, n) cin >> a[i]; if (k == 1) { cout << *max_element(ALL(a)) << '\n'; return 0; } vector dp(2, vector(n, vector(n, vector(k, -INF)))); REP(i, n - 1) { dp[false][i][i + 1][k - 1] = a[i] * (k - 1) + a[i + 1] * k; dp[true][i][i + 1][k - 1] = a[i] * k + a[i + 1] * (k - 1); } for (int h = k - 1; h > 1; --h) { REP(l, n) FOR(r, l + 1, n) { if (l > 0) chmax(dp[false][l - 1][r][h - 1], dp[false][l][r][h] + a[l - 1] * (h - 1)); if (r + 1 < n) chmax(dp[true][l][r + 1][h - 1], dp[true][l][r][h] + a[r + 1] * (h - 1)); if (h - (r - l) >= 1) { chmax(dp[true][l][r][h - (r - l)], dp[false][l][r][h]); chmax(dp[false][l][r][h - (r - l)], dp[true][l][r][h]); } if (h >= 3) { chmax(dp[false][l][r][h - 2], dp[false][l][r][h]); chmax(dp[true][l][r][h - 2], dp[true][l][r][h]); } } } vector score(n, -INF); REP(i, n) { REP(l, i + 1) FOR(r, i, n) { if (i - l + 1 < k) chmax(score[i], dp[false][l][r][i - l + 1]); if (r - i + 1 < k) chmax(score[i], dp[true][l][r][r - i + 1]); } } REP(i, n) { int ans = -INF; if (i > 0) chmax(ans, score[i - 1]); if (i + 1 < n) chmax(ans, score[i + 1]); cout << ans << '\n'; } return 0; }