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
// Karol Kosinski 2019
#include <cstdio>
#include <algorithm>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
//#define DEBUG(x...) printf(x)
#define DEBUG(x...)
//#define FOR_DEBUG(i,a,b,x...) FOR(i,a,b) DEBUG(x)
#define FOR_DEBUG(i,a,b,x...)
using namespace std;

typedef long long LL;
const int NMX = 201;
const int K = 8;
const int MX = (1 << K);
const LL INF = 3'000'000'000'000'000'000LL;

int C[MX + 1];
LL A[NMX], P[NMX], T[NMX][NMX][MX + 1], V[131][NMX][NMX];

void prepare()
{
    DEBUG("MX = %d\n", MX);
    FOR(i,1,MX)
    {
        C[i] = C[i >> 1] + (i & 1);
        DEBUG("%d ", C[i]);
    }
    DEBUG("\n");
}

void run(int z, int n, LL m)
{
    DEBUG("\n%3lld ", T[z][z][0]);
    FOR(j,1,m)
    {
        T[z][z][j] = max(T[z][z][j - 1], A[z] * C[j]);
        DEBUG("%3lld ", T[z][z][j]);
    }
    DEBUG("\n");
    FOR(i,z+1,n)
    {
        FOR_DEBUG(j,0,i-z,"  - ");
        T[z][i][i - z - 1] = -INF;
        FOR(j,i-z,m)
        {
            T[z][i][j] = max(T[z][i][j - 1], T[z][i - 1][j - 1] + A[i] * C[j]);
            DEBUG("%3lld ", T[z][i][j]);
        }
        DEBUG("\n");
    }
}

LL compute(int ind, int a, int b)
{
    const auto &X = V[ind];
    const LL pb = P[b + 1];
    LL aux = max(X[a][b],  X[a][b] + pb - P[a]);
    FOR(i,a,b) aux = max(aux, X[a][i] + X[i + 1][b] + pb - P[i + 1]);
    DEBUG("V[%d][%d][%d] = %lld\n", ind + 1, a, b, aux);
    return aux;
}

LL compute2(int ind, int ind2, int a, int b, int prod)
{
    const auto &X = V[ind], &Y = V[ind2];
    const LL pb = P[b + 1];
    LL aux = max(X[a][b],  Y[a][b] + prod * (pb - P[a]));
    FOR(i,a,b) aux = max(aux, X[a][i] + Y[i + 1][b] + prod * (pb - P[i + 1]));
    DEBUG("* V[%d][%d][%d] = %lld\n", ind + 1, a, b, aux);
    return aux;
}

LL compute3(int ind, int n_i, int m_part, int a, int b, int prod)
{
    const auto &X = V[ind];
    const LL pb = P[b + 1];
    LL aux;
    if (b - a >= n_i)
        aux = X[a][b];
    else
        aux = max(X[a][b],  T[a][b][m_part - 1] + prod * (pb - P[a]));
    FOR(i,a,b)
    {
        if (b - i - 1 >= n_i) continue;
        DEBUG("(n_i, a, i, b) = (%d, %d, %d, %d)\n", n_i, a, i, b);
        DEBUG("                  %lld + %lld + %lld = %lld)\n",
              X[a][i], T[i + 1][b][m_part - 1], prod * (pb - P[i + 1]),
              X[a][i] + T[i + 1][b][m_part - 1] + prod * (pb - P[i + 1]));
        aux = max(aux, X[a][i] + T[i + 1][b][m_part - 1] + prod * (pb - P[i + 1]));
    }
    DEBUG("** V[%d][%d][%d] = %lld\n", ind + 1, a, b, aux);
    return aux;
}

int main()
{
    prepare();
    int n, index = K;
    LL m, m_part = MX;
    scanf("%d%lld", &n, &m);
    FOR(i,0,n) scanf("%lld", A + i);
    ++m;
    if (m <= MX)
    {
        run(0, n, m);
        printf("%lld\n", T[0][n - 1][m - 1]);
        return 0;
    }
    FOR(i,0,n)
    {
        run(i, n, MX);
        P[i + 1] = P[i] + A[i];
    }
    FOR(i,0,n) FOR(j,i,n) V[index][i][j] = T[i][j][MX - 1];
    m_part <<= 1;
    while (m_part <= m)
    {
        m_part <<= 1;
        ++index;
        FOR(i,0,n) FOR(j,i,n) V[index][i][j] = compute(index - 1, i, j);
    }
    if (m_part != m)
    {
        DEBUG("------------------------------\n");
        int ki = index - 1, prod = 0;
        while (ki >= K)
        {
            if (((1LL << ki) & m) != 0)
            {
                DEBUG("ki = %d\n", ki);
                ++index, ++prod;
                FOR(i,0,n) FOR(j,i,n) V[index][i][j] = compute2(index - 1, ki, i, j, prod);
            }
            --ki;
        }
        m_part = m % MX;
        if (m_part != 0)
        {
            DEBUG("-- reminder: %lld\n", m_part);
            int n2 = min(n, int(m_part));
            ++index, ++prod;
            FOR(i,0,n) FOR(j,i,n) V[index][i][j] = compute3(index - 1, n2, m_part, i, j, prod);
        }
    }
    printf("%lld\n", V[index][0][n - 1]);
    return 0;
}