#include<iostream>
#include<stdio.h>
//#include <bits/stdc++.h>
#include<vector>
#include<float.h>
#include<iomanip>
#include<algorithm>
#include<string>
#include<cstring>
#include<math.h>
#include<cmath>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include <cassert>
#include <cmath>
#include<cstdint>

#define INF 1e9
#define rep(i,n)for(int i=0;(i)<(int)(n);i++)
#define REP(i,a,b)for(int i=(int)(a);(i)<=(int)(b);i++)
#define VEC(type, c, n) std::vector<type> c(n);for(auto& i:c)std::cin>>i;
#define vec(type,n) vector<type>(n)
#define vvec(m,n) vector<vector<int>> (int(m),vector<int>(n))
#define ALL(a)  (a).begin(),(a).end()


using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using P = pair<ll, ll>;

struct edge{
    int to;
    int dist;
};

vector<bool> seen(100000, false);
vector<int> pnt(100000, INF);
vector<vector<edge>> G(100000);
int n, m;
bool f = true;


void dfs(int v, int dist){
    seen[v] = true;
    if(pnt[v] == INF)pnt[v] = dist;
    else {
        if(pnt[v] != dist)f = false;
        return;
    }

    for(int i = 0; i < (int)(G[v].size()); i++){
        edge e = G[v][i];
        if(pnt[e.to] != INF && pnt[e.to] != pnt[v] + e.dist){
            f = false;
            return;
        }
        if(seen[e.to])continue;
        dfs(e.to, e.dist + pnt[v]);
    }
}

int main(){
    ll n, m;
    cin >> n >> m;
    vector<ll> rui(n + 1, 0);
    rep(i, n){
        ll a;
        cin >> a;
        rui[i + 1] = rui[i] + a;
    }
    ll res = 0;
    REP(i, 1, n){
        rui[i] %= m;
        if(rui[i] == 0)res++;
    }
    map<ll, ll> mp;
    REP(i, 1, n)mp[rui[i]]++;

    for(auto m : mp){
        res += m.second * (m.second - 1) / 2;
    }
    cout << res << endl;
}

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, n) for (int i = n - 1; i >= a; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, k;
  cin >> n >> k;

  vector<int> v(n);
  rep(i, 0, n) cin >> v[i];

  ll ans = 0;
  rep(l, 0, k + 1) {
    rep(r, 0, k - l + 1) {
      if (l + r > n)
        continue;
      int d = k - l - r;
      ll now = 0;
      vector<int> s;
      rep(i, 0, l) {
        now += v[i];
        s.push_back(v[i]);
      }
      rep(i, n - r, n) {
        now += v[i];
        s.push_back(v[i]);
      }

      sort(s.begin(), s.end());
      rep(i, 0, d) {
        if (i >= s.size() || s[i] > 0)
          break;
        now -= s[i];
      }

      ans = max(ans, now);
    }
  }

  cout << ans << endl;
}
