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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <stdio.h>
#include <stdlib.h>

#define xint64 long long

#define P1 10519499
#define P2 3985207
#define P3 4622017
#define P4 7285427
#define P5  5240593 
#define P6  12407573 
#define P7  14732033 
#define P8  15615491 

#define MAXH 5175557
#define MAXI 10000000

#define BADV 2000000000000000000
#define INIV 2000000000000000001
#define MAXN 256
#define MAXL 62

xint64 a[MAXN];
xint64 pow2[MAXL];
xint64 sum[MAXN];

typedef struct {
  unsigned char l;
  unsigned char r;
  unsigned char lev;
  xint64 m;
  xint64 mx;
  int n;
} t_item;

int hash[MAXH];
int ii = 0;
t_item items[MAXI];

int bits(int n) {
  int s = 0;
  while (n>0) {
    if (n % 2==1)
      s++;
    n /= 2;
  }
  return s;
}

xint64 pow2x(int n) {
  xint64 v = 1;
  for (int i=0;i<n;i++)
    v *= 2;
  return v;
}

xint64 sumx(int l, int r) {
  return sum[r] - (l==0 ? 0 : sum[l-1]);
}

unsigned int mkkey(int l, int r, int lev, xint64 m) {
  unsigned int xl = l;
  unsigned int xr = r;
  unsigned int xlev = lev;
  unsigned int m1 = m % 65536;
  m /= 65536;
  unsigned int m2 = m % 65536;
  m /= 65536;
  unsigned int m3 = m % 65536;
  m /= 65536;
  unsigned int m4 = m % 65536;
  unsigned int v = (P1 + xl*P2 + xr*P3 + xlev*P4 + m1*P5 + m2*P6 + m3*P7 + m4*P8) % MAXH;
  return v;
}

int has(int l, int r, int lev, xint64 m) {
  int key = mkkey(l, r, lev, m);
  int k = hash[key];
  while (k!=-1) {
    if (items[k].l==l && items[k].r==r && items[k].lev==lev && items[k].m==m)
      return 1==1;
    k = items[k].n;
  }
  return 1==0;
}

xint64 get(int l, int r, int lev, xint64 m) {
  int key = mkkey(l, r, lev, m);
  int k = hash[key];
  while (k!=-1) {
    if (items[k].l==l && items[k].r==r && items[k].lev==lev && items[k].m==m)
      return items[k].mx;
    k = items[k].n;
  }
  printf("SHIT!\n");
}

xint64 put(int l, int r, int lev, xint64 m, xint64 mx) {
  int key = mkkey(l, r, lev, m);
  items[ii].l = l;
  items[ii].r = r;
  items[ii].lev = lev;
  items[ii].m = m;
  items[ii].mx = mx;
  items[ii].n = hash[key];
  hash[key] = ii;
  ii++;
  return mx;
}

xint64 go(int l, int r, int lev, xint64 m) {
  if (l>r)
    return 0;
  if (r-l+1>m+1)
    return BADV;
  if (lev==1) {
    if (l+1==r)
      return a[r];
    else if (l==r) {
      if (m==0)
        return 0;
      else if (a[l]>0)
        return a[l];
      else
        return 0;
    } else
      printf("WTF??? [l=%d, r=%d, lev=%d, m=%lld]\n", l, r, lev, m);
  }  
  xint64 v = pow2[lev-1];
  if (has(l, r, lev, m))
    return get(l, r, lev, m);
  //printf("go(%d, %d, %d, %lld) [%lld]\n", l, r, lev, m, 2*pow2[lev-1]-1);
  if (m<v) {
    //printf("N %lld -> %lld\n", m, v);
    return go(l, r, lev-1, m);
  }
  xint64 s = sumx(l, r);
  int f = 0;
  xint64 mx = 0;
  for (int i=l-1;i<=r;i++) {
    xint64 t1 = go(l, i, lev-1, v-1);
    if (t1!=BADV) {
      xint64 t2 = go(i+1, r, lev-1, m-v);
      //printf("s[%d, %d] = %lld\n", i+1, r, s);
      if (t2!=BADV && (f==0 || s+t1+t2>mx)) {
        f = 1;
        mx = s+t1+t2;
        //printf("MAX(%d, %d, %d, %lld) => [%d/%d/%lld]=%lld, [%d/%d/%lld]=%lld => %lld\n", l, r, lev, m, l, i, v-1, t1, i+1, r, m-v, t2, mx);
      }
    }
    s -= a[i+1];
  }
  if (f==1) {
    //printf("go(%d, %d, %d, %d) = %lld\n", l, r, lev, m, mx);
    return put(l, r, lev, m, mx);
  }
  return put(l, r, lev, m, BADV);
}

void stat() {
  printf("ii=%d / %d\n", ii, MAXI);
  int m = 0;
  for (int i=0;i<MAXH;i++) {
    int k = hash[i];
    int l = 0;
    while (k!=-1) {
      l++;
      k = items[k].n;
    }
    if (l>m)
      m = l;
  }
  printf("maxl=%d\n", m);
}

int main() {
  int n;
  xint64 m;
  scanf("%d%lld", &n, &m);
  //printf("%d\n", n);
  for (int i=0;i<n;i++)
    scanf("%lld", &a[i]);
  sum[0] = a[0];
  for (int i=1;i<n;i++)
    sum[i] = sum[i-1] + a[i];
  for (int k=0;k<MAXH;k++)
    hash[k] = -1;
  for (int k=0;k<MAXL;k++)
    pow2[k] = pow2x(k);
  printf("%lld\n", go(0, n-1, MAXL-1, m));
//  stat();
  return 0;
}