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
#include <bits/stdc++.h>
#include <stdint.h>

using namespace std;


void fix(map<uint64_t, int64_t> &NDP) {
  map<uint64_t, int64_t>::iterator it = NDP.begin();
  if(it == NDP.end()) return;
  map<uint64_t, int64_t>::iterator jt = it; jt++;
  while(jt != NDP.end()) {
    if(jt->second <= it->second) {
      jt = NDP.erase(jt);
    } else {
      it = jt;
      jt++;
    }
  }
}


int64_t solve(vector<int64_t> const &A, uint64_t m) {
  map<uint64_t, int64_t> DP;
  
  DP[0] = 0;
  
  for(size_t pos = 0; pos < A.size(); pos++) {
    map<uint64_t, int64_t> NDP;
    
    if(A[pos] == 0) {
      for(map<uint64_t, int64_t>::const_iterator it = DP.begin(); it != DP.end(); it++) {
        uint64_t b = it->first;
        int64_t pre = it->second;
        
        NDP[b + 1] = pre;
      }
    } else if(A[pos] < 0) {
      for(map<uint64_t, int64_t>::const_iterator it = DP.begin(); it != DP.end(); it++) {
        uint64_t b = it->first;
        int64_t pre = it->second;
        unsigned int pc = __builtin_popcountll(b);
        unsigned int mpc = pc;
        
        NDP[b + 1] = pre + pc * A[pos];
        
        while(pc > 1) {
          b += (b & (~b + 1));
          if(b > m) break;
          pc = __builtin_popcountll(b);
          if(pc < mpc) {
            NDP[b + 1] = pre + pc * A[pos];
            mpc = pc;
          }
        }
      }
    } else /* if(A[pos] > 0) */ {
      for(map<uint64_t, int64_t>::const_iterator it = DP.begin(); it != DP.end(); it++) {
        uint64_t b = it->first;
        int64_t pre = it->second;
        unsigned int pc = __builtin_popcountll(b);
        
        NDP[b + 1] = pre + pc * A[pos];
        
        while(true) {
          b += (~b & (b + 1));
          if(b > m) break;
          pc = __builtin_popcountll(b);
          NDP[b + 1] = pre + pc * A[pos];
        }
      }
    }
    
    fix(NDP);
    DP.swap(NDP);
  }
  
  return DP.rbegin()->second;
}



int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  
  int n;
  uint64_t m;
  cin >> n >> m;
  
  vector<int64_t> A(n);
  for(int i = 0; i < n; i++) cin >> A[i];
  
  cout << solve(A, m) << '\n';
  
  return 0;
}