#include <bits/stdc++.h>
using namespace std;
struct state {
  list<int> D;
  priority_queue<int, vector<int>, greater<int> > PQ;
};
int N, K;
map<list<int>, bool> M;
int dfs(state s, int k, int sum) {
  if (k == 0 || M[s.D]) return sum;
  int res = sum;
  M[s.D] = true;
  if (s.D.size() > 0) {
    priority_queue<int, vector<int>, greater<int> > pq;
    pq = s.PQ;
    int l = *s.D.begin();
    s.D.pop_front();
    if (l < 0) s.PQ.push(l);
    res = max(dfs(s, k - 1, sum + l), res);
    s.D.push_front(l);
    s.PQ = pq;
    int r = *(--s.D.end());
    s.D.pop_back();
    if (r < 0) s.PQ.push(r);
    res = max(dfs(s, k - 1, sum + r), res);
    s.D.push_back(r);
    s.PQ = pq;
  }
  if (s.PQ.size() > 0 && (s.D.size() == 0 || k <= s.D.size())) {
    int val = s.PQ.top();
    s.PQ.pop();
    s.D.push_front(val);
    res = max(dfs(s, k - 1, sum - val), res);
    s.D.pop_front();
    s.D.push_back(val);
    res = max(dfs(s, k - 1, sum - val), res);
  }
  return res;
}
int main(void) {
  state s;
  scanf("%d %d", &N, &K);
  for (int i = 0; i < N; i++) {
    int v;
    scanf("%d", &v);
    s.D.push_back(v);
  }
  int ans = dfs(s, K, 0);
  printf("%d\n", ans);
}
