1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>
#include <vector>
#include <limits>

using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;

int bits_in_use(ll x) {
	int res = 0;
	ll y = x;
	while (y > 0) { y >>= 1; res++; }
	return res;
}

inline int bit_count(ll x) {
	int res = 0;
	while (x > 0) { if (x & 1) res++; x >>= 1; }
	return res;
}

const ll nop = numeric_limits<ll>::min();

int main() {
	ios_base::sync_with_stdio(0);

	int n; ll m; cin >> n >> m;
	int k = bits_in_use(m) + 1;

	vll a(n); for (int i = 0; i < n; i++) cin >> a[i];
	vll s(n); s[0] = a[0];
	for (int i = 1; i < n; i++) s[i] = s[i-1] + a[i];

	vector<vector<vector<pll>>> r(k+1, vector<vector<pll>>(n+1, vector<pll>(n+1, { nop, nop })));

	for (int i = 0; i < n; i++) {
		r[0][i][i] = { 0, 0 };
		r[0][i][i+1] = { 0, 0 };
	}

	r[0][n][n] = { 0, 0 };

	#define total(S, T) (s[T-1] - (S == 0 ? 0 : s[S-1]))

	for (int d = 1; d <= k; d++) {
		ll mr = (m+1) & ((1L << d) - 1);
		ll rhalf = (1L << (d-1));

		for (int i = 0; i <= n; i++) {
			r[d][i][i] = { 0, 0 };
			for (int j = i+1; j <= n; j++) {
				if (j - i > (1L << d)) continue;
				ll cr = nop;
				for (int l = i; l <= j; l++) {
					if (r[d-1][i][l].first == nop || r[d-1][l][j].first == nop) continue;
					cr = max(cr, r[d-1][i][l].first + total(l, j) + r[d-1][l][j].first);
				}

				r[d][i][j].first = cr;
				cr = nop;

				if (mr == rhalf) {
					r[d][i][j].second = r[d-1][i][j].first;
					continue;
				}

				if (mr < rhalf) {
					r[d][i][j].second = r[d-1][i][j].second;
					continue;
				}

				for (int l = i; l <= j; l++) {
					if (r[d-1][i][l].first == nop || r[d-1][l][j].second == nop) continue;
					cr = max(cr, r[d-1][i][l].first + total(l, j) + r[d-1][l][j].second);
				}

				r[d][i][j].second = cr;
			}
		}
	}

	cout << r[k][0][n].second << endl;

	// for (int d = 0; d <= k; d++) {
	// 	ll mr = (m+1) & ((1L << d) - 1);
	// 	ll rhalf = d == 0 ? 0 : (1L << (d-1));

	// 	cerr << d << " " << mr << " " << rhalf << endl;

	// 	for (int i = 0; i <= n; i++) {
	// 		cerr << ". ";
	// 		for (int j = 0; j <= n; j++) {
	// 			if (r[d][i][j].first == nop) cerr << '-';
	// 			else cerr << r[d][i][j].first;
	// 			cerr << ' ';
	// 		}

	// 		cerr << ". ";
	// 		for (int j = 0; j <= n; j++) {
	// 			if (r[d][i][j].second == nop) cerr << '-';
	// 			else cerr << r[d][i][j].second;
	// 			cerr << ' ';
	// 		}
	// 		cerr << endl;
	// 	}

	// 	cerr << endl;
	// }

	return 0;
}