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
#include <bits/stdc++.h>
using namespace std;
#define mk make_pair
#define fi first
#define sz(x) x.size()
#define se second
#define pb push_back
#define VAR(v) #v << " " << v << " "
#define debug if(0)
typedef long long ll;
typedef pair<int, int> ii;
const int maxn = (int)1e6 + 9;
const ll INF = (ll)1e18 + 7;
const int mod = (int)1e9 + 7;
ll dp[209][209][69];
ll dp2[209][209][69];
int n; ll m;
ll tab[209];
ll pref[209];
ll two[61];

ll sum(int l, int r){
    return pref[r] - pref[l-1];
}


void print(ll tab[][209][69])
{
    for(int k = 1; two[k - 1] <= m; k++)
    {
        cout<<VAR(k)<< endl;
        cout<<"v:\t";
        for(int v = 1; v <= n; v++)
            cout<<v<<"\t";
        cout<<endl;
        for(int i = 1; i <= n; i++)
        {
            cout<<i<<":\t";
            for(int j = 1; j <= n; j++)
            {
                if(tab[i][j][k] == -INF)
                    cout<<"#\t";
                else
                    cout<<tab[i][j][k]<<"\t";
            }
            cout<<endl;
        }
    }
}

int main(int argc, char* argv[])
{
    ios::sync_with_stdio(false);
    debug; else cin.tie(NULL), cout.tie(NULL);
    cin>>n>>m;
    for(int i = 1; i <= n; i++)
    {
        ll x; cin>>x;
        tab[i] = x;
        pref[i] = pref[i-1] + x;
    }
    for(int k = 0; k <= 61; k++)
        for(int i = 0; i <= n; i++)
            for(int j = i; j <= n; j++)
                dp[i][j][k] = dp2[i][j][k] = -INF;
    for(int i = 0; i <= n; i++)
        dp[i][i][0] = dp2[i][i][0] = 0;
    two[0] = 1;
    for(int i = 1; i <= 61; i++) two[i] = 2 * two[i-1];
    int log2m = 0; while(two[log2m] <= m) log2m++;

    for(int k = 1; two[k - 1] <= m; k++)
        for(int len = 1; len <= n; len++)
        {
            if(len > two[k])
                continue;
            for(int a = 1; a + len - 1 <= n; a++)
            {
                int b = a + len - 1;
                dp[a][b][k] = -INF;
                for(int x = a - 1; x <= b; x++)
                {
                    if(x - a + 1 > two[k-1] || b - x > two[k-1])
                        continue;
                    dp[a][b][k] = max(dp[a][x][k-1] + dp[x + 1][b][k-1] + sum(x + 1, b), dp[a][b][k]);
                }
            }
        }
    for(int k = 1; two[k - 1] <= m; k++)
    {
        for(int len = 1; len <= n; len++)
        {
            if(len > two[k])
                continue;
            for(int a = 1; a + len - 1 <= n; a++)
            {
                int b = a + len - 1;
                debug cout << VAR((two[k - 1] & m))<<endl;
                if(!(two[k - 1] & m))
                {
                    dp2[a][b][k] = dp2[a][b][k-1];
                    debug cout<<VAR(a) <<VAR(b) << VAR(k) << "skip"<<endl;
                    continue;
                }
                dp2[a][b][k] = dp[a][b][k-1];
                for(int x = a - 1; x <= b; x++)
                {
                    if(x - a + 1 > two[k-1] || b - x > two[k-1])
                        continue;
                    dp2[a][b][k] = max(dp[a][x][k-1] + dp2[x + 1][b][k-1] + sum(x + 1, b), dp2[a][b][k]);
                    if(k == 5 && a == 1) debug{
                        debug cout << VAR(a) << VAR(b) << VAR(x) << endl;
                        cout<<VAR(dp2[a][b][k])<<VAR(dp[a][x][k-1])<<VAR(dp2[x + 1][b][k-1])<<VAR(sum(x + 1, b))<<endl;
                    }
                }
            }
        }
    }
    debug{
        debug cout << VAR((two[4] & m))<<endl;
        cout<<"dp\n";
        print(dp);
        for(int i = 0; i <= m; i++)
        {
            int x = i;
            cout<<i<<": ";
            while(x)
            {
                cout<<(x&1);
                x>>=1;
            }
            cout<<endl;
        }
        cout<<"\ndp2\n";
        print(dp2);
    }
    cout<<dp2[1][n][log2m];
    return 0;
}