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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#pragma GCC optimize "Ofast"
#include<bits/stdc++.h>

using namespace std;

const long long linf = 2000000000000000009;
const int lim = 209;
int n;
long long m,tab[lim];
map<long long,long long> t[lim][lim];

long long f(long long l,long long p,int i,int j)
{
	if(i==j)
	{
		return 0;
	}
	if(j-i>p-l+1)
	{
		return -linf;
	}
	if(i==j-1)
	{
		long long pp = tab[j]-tab[i];
		if(pp<0)
		{
			return pp*__builtin_popcountll(l);
		}
		else
		{
			if(((p-l+1)&(l-p-1))==(p-l+1))
			{
				return pp*__builtin_popcountll(p);
			}
			else
			{
				int mx = __builtin_popcountll(p);
				while(p-1>=l)
				{
					mx = max(mx,__builtin_popcountll(p-1));
					p -= p&(-p);
				}
				return pp*mx;
			}
		}
	}
	long long pom = t[i][j][p-l];
	if(pom)
	{
		if(pom<0) ++pom;
		//printf("T %lld %lld %d %d %lld\n", l, p, i, j, pom);
		return pom + (tab[j]-tab[i])*__builtin_popcountll(l);
	}
	else
	{
		pom = -linf;
		long long mid = p-l+1;
		mid &= -mid;
		if(mid==p-l+1)
		{
			mid >>= 1;
			mid += l;
		}
		else
		{
			mid = p+1-mid;
		}
		for(int h = i;h<=j;++h)
		{
			pom = max(-linf,max(pom,f(l,mid-1,i,h)+f(mid,p,h,j)));
		}
		long long wyn = pom;
		pom -= (tab[j]-tab[i])*__builtin_popcountll(l);
		if(pom<=0) --pom;
		t[i][j][p-l] = pom;
		//printf("P %lld %lld %d %d %lld\n", l, p, i, j, wyn);
		return wyn;
	}
}

int main()
{
	scanf("%d%lld", &n, &m);
	for(int i = 1;i<=n;++i)
	{
		scanf("%lld", &tab[i]);
		tab[i] += tab[i-1];
	}
	for(int i = 0;i<n;++i)
	{
		if(tab[i+1]-tab[i]>0)
		{
			for(long long h = 1;h-1<=m;h <<= 1)
			{
				t[i][i+1][h-1] = (tab[i+1]-tab[i])*__builtin_popcountll(h-1);
				//printf("%d %d %lld %lld\n", i, i+1, h-1, t[i][i+1][h-1]);
			}
		}
	}
	for(int i = 2;i<=n;++i)
	{
		for(int j = n-i;j>=0;--j)
		{
			int pp = i+j;
			long long h = 1;
			while(h<i) h <<= 1;
			if(h<=m)
			{
				t[j][pp][h-1] = -linf;
				long long hh = (h>>1)-1;
				long long h1 = h-1;
				for(int g = j+1;g<pp;++g)
				{
					if(max(g-j,pp-g)<=(h>>1)) t[j][pp][h1] = max(t[j][pp][h1],t[j][g][hh]+t[g][pp][hh]+(tab[pp]-tab[g]));
				}
				//printf("%d %d %lld %lld\n", j, pp, h1, t[j][pp][h1]);
				h <<= 1;
				for(;h-1<=m;h <<= 1)
				{
					t[j][pp][h-1] = t[j][pp][(h>>1)-1]+max(0ll,tab[pp]-tab[j]);
					long long hh = (h>>1)-1;
					long long h1 = h-1;
					for(int g = j+1;g<pp;++g)
					{
						t[j][pp][h1] = max(t[j][pp][h1],t[j][g][hh]+t[g][pp][hh]+(tab[pp]-tab[g]));
					}
					//printf("%d %d %lld %lld\n", j, pp, h1, t[j][pp][h1]);
				}
			}
		}
	}
	for(int i = 0;i<n;++i)
	{
		for(int j = i+1;j<=n;++j)
		{
			for(long long h = 1;h-1<=m;h <<= 1)
			{
				if(t[i][j][h-1]<=0) --t[i][j][h-1]; 
			}
		}
	}
	printf("%lld\n", f(0,m,0,n));
}
/*
1 4
3 

6
//*/