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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <stdio.h>
#include <algorithm>
#include <map>
#include <unordered_map>

#define LL long long
#define BIGMOD 1000012177LL
#define DNUM 31LL
#define INF 1000000000000000001LL
LL a_coef[202];
LL b_coef[202];
LL positive_sum[202];
LL negative_sum[202];

#define K3DBG(X)

LL count_1s(LL x) {
    return __builtin_popcountll(x);
}

LL bitcount(LL x) {
    LL res = 0;
    while (x > 0)
    {
        x >>=1 ;
        ++res;
    }
    return res;
}

LL najmniejsza_liczba_z_X_jedynkami_wieksza_od_Y(LL X, LL Y) {
    if (X == 0 && Y > 0) return -1;
    if (X == 0) return 0;
    
    K3DBG(printf("X=%lld Y=%lld\n", X, Y));
    LL p2x = (1LL << X);
    LL only1s = p2x - 1; // najmniejsza w ogole z X jedynkami
    if (only1s > Y) return only1s;
    LL cY = bitcount(Y);
    K3DBG(printf("cY=%lld\n", cY));
    LL p2tmp = 1LL << (cY - 1);
    LL msbitscountY = 0;
    while (p2tmp & Y) {
        msbitscountY++;
        p2tmp >>=1;
    }
    K3DBG(printf("msbitscountY=%lld ", msbitscountY));
    LL result = 0;
    if (X <= msbitscountY)
    {
        result = (1LL << cY) + (1LL << (X-1))-1;
    } 
    else
    {
        LL yc1s = count_1s(Y);
        LL to1sFill = X;
        p2tmp = 1LL << (cY - 1);
        LL cand = 0LL;
        while (to1sFill > 0 && yc1s > 0)
        {
            if (Y & p2tmp)
            {
                to1sFill--;
                yc1s--;
                cand |= p2tmp;
            }
            if (to1sFill == 0)
            {
                cand += p2tmp;
            }
            p2tmp >>= 1;
        }
        to1sFill = X - count_1s(cand);
        if (to1sFill > 0)
        {
            p2tmp = 1;
            while (to1sFill > 0)
            {
                if ((cand & p2tmp) == 0)
                {
                    cand |= p2tmp;
                    to1sFill--;
                }
                p2tmp <<= 1;
            }
        }
        result = cand;
        K3DBG(printf("result=%lld\n", result));
    }
    if (count_1s(result) != X)
    {
        printf("error: result has wrong number of 1s %lld %lld\n", X, Y);
    }
    if (result <= Y)
    {
        printf("error: result should be greater than Y");
    }
    return result;
}

struct pair_hash
{
	template <class T1, class T2>
	std::size_t operator() (const std::pair<T1, T2> &pair) const
	{
		return std::hash<T1>()(pair.first) ^ std::hash<T2>()(pair.second);
	}
};
//std::unordered_map<std::pair<LL, LL>, LL, pair_hash> cache;

std::map<LL,LL> cache[202];
LL best_level_results[202];
LL best_result_ever_recorded;

LL go(LL idx, LL min_m, const LL n, const LL max_m, const LL bitcount_m, const LL best_result_so_far)
{
    //printf("entering: %lld min=%lld max=%lld\n", idx, min_m, max_m);
    if (idx == n)
    {
       return 0;
    }

    if (min_m > max_m)
    {
        return -INF;
    }
    // reject if "result_so_far < best-result_so_far and min_m > best_min_m"

    auto it = cache[idx].find(min_m);
    if (it != cache[idx].end()) {
        return it->second;
    }
    it = cache[idx].lower_bound(min_m);
    if (it != cache[idx].end())
    {
        if (it != cache[idx].begin())
        {
            --it;
            if (it->first >= min_m)
            {
                printf("error: expected it->first to be lower");
            }
            if (best_result_so_far + it->second <= best_result_ever_recorded) {
                // printf("early skip\n");
                return -INF;
            }
        }
    }

    LL best_result = -INF;
    LL start_idx = 0, end_idx = bitcount_m+1, step = 1LL;
    if (a_coef[idx] > 0) {
        start_idx = bitcount_m+1;
        end_idx = -1LL;
        step = -1LL;
    }
    for (LL onescount=start_idx; onescount != end_idx; onescount += step)
    {
        LL bi = najmniejsza_liczba_z_X_jedynkami_wieksza_od_Y(onescount, min_m);
        if (bi <= min_m || bi > max_m) continue;
        //printf("onescount %lld, min_m=%lld, bi=%lld\n", onescount, min_m, bi);
        b_coef[idx] = bi;
        LL res = go(idx+1, bi, n, max_m, bitcount_m, best_result_so_far + a_coef[idx] * onescount);
        if (res == -INF) 
        {
            continue;
        }
        res += a_coef[idx] * onescount;
        best_result = std::max(best_result, res);
    }
    cache[idx][min_m] = best_result;
    best_level_results[idx] = std::max(best_level_results[idx], best_result);
    /*printf("idx=%lld best_result=%lld b_coeff:", idx, best_result);
    for (int i=0; i <= idx; i++) {
        printf("%lld ", b_coef[i]);
    }
    printf("\n");*/
    return best_result;
}

void preprocessing(int n) {
    positive_sum[0] = 0;
    negative_sum[0] = 0;
    for (int i=0; i < n; i++) {
        LL positive = a_coef[i] > 0 ? a_coef[i] : 0;
        LL negative = a_coef[i] < 0 ? a_coef[i] : 0;
        positive_sum[i+1] = positive_sum[i] + positive;
        negative_sum[i+1] = negative_sum[i] + negative;
        best_level_results[i] = -INF;
    }
}

#define ASSERT_TRUE(X) { if (!(X)) { printf(#X "is not true\n"); } }
void selftest() {

    ASSERT_TRUE(najmniejsza_liczba_z_X_jedynkami_wieksza_od_Y(2, 13) == 17);
    ASSERT_TRUE(najmniejsza_liczba_z_X_jedynkami_wieksza_od_Y(1, 8) == 16);
    ASSERT_TRUE(najmniejsza_liczba_z_X_jedynkami_wieksza_od_Y(1, 128) == 256);
    ASSERT_TRUE(najmniejsza_liczba_z_X_jedynkami_wieksza_od_Y(0, 0) == 0);
    ASSERT_TRUE(najmniejsza_liczba_z_X_jedynkami_wieksza_od_Y(0, 1) == -1);
    ASSERT_TRUE(najmniejsza_liczba_z_X_jedynkami_wieksza_od_Y(3, 15) == 19);
    ASSERT_TRUE(najmniejsza_liczba_z_X_jedynkami_wieksza_od_Y(2, 8) == 9);
    ASSERT_TRUE(najmniejsza_liczba_z_X_jedynkami_wieksza_od_Y(2, 9) == 10);
    ASSERT_TRUE(najmniejsza_liczba_z_X_jedynkami_wieksza_od_Y(2, 9) == 10);

    ASSERT_TRUE(najmniejsza_liczba_z_X_jedynkami_wieksza_od_Y(2, 8) == 9);
    ASSERT_TRUE(najmniejsza_liczba_z_X_jedynkami_wieksza_od_Y(3, 10) == 11);
    ASSERT_TRUE(najmniejsza_liczba_z_X_jedynkami_wieksza_od_Y(3, 19) == 21);
    ASSERT_TRUE(najmniejsza_liczba_z_X_jedynkami_wieksza_od_Y(0, 0) == 0);
}

int main() {
    selftest();
    LL n, m;
    scanf("%lld%lld", &n, &m);
    for (int i=0; i < n; i++) {
        scanf("%lld", &a_coef[i]);
    }
    preprocessing(n);
    best_result_ever_recorded = -INF;
    LL best_ever = go(0, -1, n, m, bitcount(m), 0);
    K3DBG(printf("best_ever=%lld\n", best_ever));
    printf("%lld\n", best_ever);
    //printf("%lld\n", najmniejsza_liczba_z_X_jedynkami_wieksza_od_Y(2, 14));
    return 0;
}