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

#define MINF -0x3fffffffffffffffLL
#define ALOT 8300000

struct PR{
    int64_t pos;
    int64_t score;
    bool operator<(const struct PR &par) {
        return (pos < par.pos);
    }
};

static PR tab0[ALOT];
static PR tab1[ALOT];

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);

    int i, n, hi = 1, b, count;
    int64_t m, a, pos, score, oldscore, mask;
    PR *cur = tab0;
    PR *next = tab1;
    PR *swap;
    int64_t cs, ns, it, sit, last;

    next[ns++] = PR{-1, 0};

    std::cin >> n >> m;
    for (i = 1; i <= n; ++i) {
        std::cin >> a;

        swap = cur;
        cur = next;
        next = swap;
        cs = ns;
        ns = 0;
        std::sort(cur, cur + cs);

        oldscore = MINF;
        for (it = 0; it < cs; ++it) {
             pos = cur[it].pos;
             score = cur[it].score;
            if (score <= oldscore) {
                continue;
            }
            oldscore = score;

            pos++;
            b = __builtin_popcountll(pos);
            if (a <= 0) {
                while (pos <= m - n + i) {
                    next[ns++] = PR{pos, score + b * a};
                    if (ns >= ALOT) {
                        std::sort(next, next + ns);
                        last = MINF;
                        for (sit = 0; sit < ns; ++sit) {
                            if (next[sit].score > last) {
                                last = next[sit].score;
                            } else {
                                ns--;
                                next[sit] = next[ns];
                            }
                        }
                        if (ns >= ALOT) {
                            std::cerr << ALOT << " is not enough\n";
                            return 1;
                        }
                    }
                    if (b <= 1) {
                        break;
                    }
                    count = 0;
                    mask = 1;
                    while (count < 2) {
                        if (mask & pos) {
                            count++;
                            pos = (pos & (~mask));
                        }
                        mask <<= 1;
                    }
                    while (mask & pos) {
                        count++;
                        pos = (pos & (~mask));
                        mask <<= 1;
                    }
                    b = b - count + 1;
                    pos |= mask;
                }
            } else {
                while (pos <= m - n + i) {
                    next[ns++] = PR{pos, score + b * a};
                    if (ns >= ALOT) {
                        std::sort(next, next + ns);
                        last = MINF;
                        for (sit = 0; sit < ns; ++sit) {
                            if (next[sit].score > last) {
                                last = next[sit].score;
                            } else {
                                ns--;
                                next[sit] = next[ns];
                            }
                        }
                        if (ns >= ALOT) {
                            std::cerr << ALOT << " is not enough\n";
                            return 1;
                        }
                    }
                    mask = 1;
                    while (mask & pos) {
                        mask <<= 1;
                    }
                    b++;
                    pos |= mask;
                }
            }
        }
    }
    oldscore = MINF;
    for (it = 0; it < ns; ++it) {
        if (next[it].score > oldscore) {
            oldscore = next[it].score;
        }
    }
    std::cout << oldscore << "\n";

    return 0;
}