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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#include <cmath>
#include <iostream>
#include <limits>
#include <sstream>
#include <vector>

using namespace std;

#define TESTY_DOMOWE_SKIP

#ifdef TESTY_DOMOWE
	#include "perr.hpp"
	#define main nomain
#endif

int count_bits( long long n)
{
	long long upper = 2;
	int bits = 1;
	while( n >= upper ) {
		upper *= 2;
		bits += 1;
	}
	return bits;
}

int count_set_bits(long long n) 
{ 
    int count = 0; 
    while (n) { 
        n &= (n - 1); 
        count++; 
    } 
    return count; 
}

int get_max_count( long long const lower_bound, long long const upper_bound, int const extra = 0 ) {
	long long const upper_back = upper_bound - 1;

	if( upper_back == 0 ) {
		return extra;
	}

	if( count_bits(upper_back) == count_set_bits( upper_back ) ) {
		return extra + count_set_bits( upper_back );
	}

	if( count_bits(lower_bound) < count_bits(upper_back) ) {
		return extra + count_bits( upper_back ) - 1;
	}

	long long pow2 = lower_bound;
	while( pow2 & (pow2 - 1) ) {
		pow2 = pow2 & (pow2 - 1);
	};

	long long const mask = ~pow2;
	return get_max_count( lower_bound & mask, (upper_back & mask) + 1, extra + 1);
}


int get_min_count( long long const lower_bound, long long const upper_bound, int const extra = 0) {
	long long const upper_back = upper_bound - 1;

	if( lower_bound == 0 ) {
		return extra;
	}

	if( count_bits(lower_bound) < count_bits(upper_back) ) {
		return 1 + extra;
	}

	long long pow2 = lower_bound;
	while( pow2 & (pow2 - 1) ) {
		pow2 = pow2 & (pow2 - 1);
	};

	long long const mask = ~pow2;
	return get_min_count( lower_bound & mask, (upper_back & mask) + 1, extra + 1);
}

long long make_max_number( int num_ones, long long upper_bound, long long extra = 0L) {
	if( num_ones == 0 ) {
		return extra;
	}

	long long first = 1LL;
	while( (first * 2) + ( pow( 2LL, num_ones - 1) - 1 ) < upper_bound ) {
		first *= 2;
	}

	return extra + make_max_number( num_ones - 1, min(upper_bound - first, first), first );
}

enum class minimaxi_t { Maxi, Mini };
ostream& operator<<( ostream& os, minimaxi_t ) { return os; };

struct vbc { /* value and bit count */
	int bit_count;
	long long max_val_with_given_bits;
};

struct Beater {
	long long bit_lower;
	long long const bit_upper;
	int last_bit_count;
	minimaxi_t mode;

	Beater( long long bit_lower, long long bit_upper, bool ge_zero ) : 
		bit_lower(bit_lower),
		bit_upper(bit_upper),
		last_bit_count(ge_zero ? numeric_limits< int >::max() : numeric_limits< int >::min() ),
		mode(ge_zero ? minimaxi_t::Maxi : minimaxi_t::Mini)
	{}

	bool bitte(vbc& v) {
		if( this->bit_lower >= this->bit_upper) {
			return false;
		}

		if( this->mode == minimaxi_t::Maxi ) {
			return bitte_max( v );
		}
		else {
			return bitte_min( v );
		}
	}

	bool bitte_max( vbc& v) {
		int max_bit_count = get_max_count( this->bit_lower, this->bit_upper );
		if (max_bit_count < this->last_bit_count) {
			long long max_value = make_max_number( max_bit_count, bit_upper );

			this->last_bit_count = max_bit_count;
			this->bit_lower = max_value + 1;

			v.bit_count = max_bit_count;
			v.max_val_with_given_bits = max_value;
			return true;
		}
		else {
			return false;
		}
	}

	bool bitte_min( vbc& v) {
		int min_bit_count = get_min_count( this->bit_lower, this->bit_upper );
		if (min_bit_count > this->last_bit_count) {
			long long max_value = make_max_number( min_bit_count, bit_upper );

			this->last_bit_count = min_bit_count;
			this->bit_lower = max_value + 1;

			v.bit_count = min_bit_count;
			v.max_val_with_given_bits = max_value;
			return true;
		}
		else {
			return false;
		}
	}
};

long long wylicz( vector< long long > const& melodia, int ve, long long bit_upper, int level = 1 ) {
	string indent( size_t(2*level), ' ');

	if( ve == 0 ) {
		return 0;
	}

	// Znajduje najlepszy mnoznik.
	// Pierwszy argument musi odpowiadac
	// liczbie pozostalych nut / sekund.
	Beater bt( ve - 1, bit_upper, melodia[ve-1] >= 0 );

	vbc b;
	long long best = std::numeric_limits< long long >::min();
	while( bt.bitte(b) ) {
		long long tail = melodia[ve - 1] * b.bit_count;
		long long heads = wylicz(melodia, ve - 1, b.max_val_with_given_bits, level + 1);
		long long next = tail + heads;

		if( next > best ) {
			best = next;
		}
	}

	return best;
}

int main() {
	int dl; cin >> dl;
	long long bit_max; cin >> bit_max;

	vector< long long > melodia( dl );
	for ( long long i = 0; i < dl; ++i ) {
		cin >> melodia[i];
	}

	cout << wylicz( melodia, dl, bit_max + 1 ) << endl;
	return 0;
}



#ifdef TESTY_DOMOWE
#undef main

struct Test {
	bool ok = true;

	void check( bool v, string s = "no-name" ) {
		check( v, true, s );
	}

	template< typename T >
	void check ( T v1, T v2, string s = "no-name" )
	{
		if( v1 != v2 ) {
			cerr << "Error!! got " << v1 << " expected " << v2 << "   " << s << endl;
			this->ok = false;
		}
		else {
			cerr << "OK! " << s << endl;
		}
	}
};

bool testuj_get_max_count() {
	Test t;

	t.check( get_max_count(15, 23), "[15, 23) - 1111(15) == 4");
	t.check( get_max_count(15, 24), "[15, 24) - 1111(15) == 4");
	t.check( get_max_count(15, 25), "[15, 25) - 1111(15) == 4");
	t.check( get_max_count(15, 26), "[15, 26) - 1111(15) == 4");
	t.check( get_max_count(15, 27), "[15, 27) - 1111(15) == 4");
	t.check( get_max_count(15, 28), "[15, 28) - 1111(15) == 4");
	t.check( get_max_count(15, 29), "[15, 29) - 1111(15) == 4");
	t.check( get_max_count(15, 30), "[15, 30) - 1111(15) == 4");
	t.check( get_max_count(15, 31), "[15, 31) - 11111(31) == 5");
	t.check( get_max_count(15, 32), "[15, 32) - 11111(31) == 5");

	t.check( get_max_count(16, 22), "[16, 22) - 10011(19) == 3");
	t.check( get_max_count(16, 23), "[16, 23) - 10111(23) == 4");
	t.check( get_max_count(16, 24), "[16, 24) - 10111(23) == 4");
	t.check( get_max_count(16, 25), "[16, 25) - 10111(23) == 4");
	t.check( get_max_count(16, 26), "[16, 26) - 10111(23) == 4");
	t.check( get_max_count(16, 27), "[16, 27) - 10111(23) == 4");
	t.check( get_max_count(16, 28), "[16, 28) - 10111(23) == 4");
	t.check( get_max_count(16, 29), "[16, 29) - 10111(23) == 4");
	t.check( get_max_count(16, 30), "[16, 30) - 10111(23) == 4");
	t.check( get_max_count(16, 31), "[16, 31) - 11111(31) == 5");

	t.check( get_max_count(65540, 65542), "[65540, 65542) - 10000000000000101(65541) == 3");
	t.check( get_max_count(65540, 65543), "[65540, 65543) - 10000000000000111(65543) == 4");
	t.check( get_max_count(65540, 65544), "[65540, 65544) - 10000000000000111(65543) == 4");
	t.check( get_max_count(65540, 65545), "[65540, 65545) - 10000000000000111(65543) == 4");
	t.check( get_max_count(65540, 65546), "[65540, 65546) - 10000000000000111(65543) == 4");
	t.check( get_max_count(65540, 65547), "[65540, 65547) - 10000000000000111(65543) == 4");
	t.check( get_max_count(65540, 65548), "[65540, 65548) - 10000000000000111(65543) == 4");
	t.check( get_max_count(65540, 65549), "[65540, 65549) - 10000000000000111(65543) == 4");

	t.check( get_max_count(16, 22), "[16, 22) - 10011(19) == 3");
	t.check( get_max_count(0, 1, 0), 0, "zero" );
	t.check( get_max_count(0, 1, 100), 100, "nie zero (bo extra)" );
	t.check( get_max_count(0, 2, 0), 1, "jeden" );
	t.check( get_max_count(0, 2, 100), 101, "jeden i extra" );

	return t.ok;
}

bool testuj_count_bits() {
	Test t;
	t.check( count_bits(0), 1, "bits in 0" );
	t.check( count_bits(1), 1, "bits in 1" );
	t.check( count_bits(2), 2, "bits in 2" );
	t.check( count_bits(3), 2, "bits in 3" );
	t.check( count_bits(4), 3 );
	t.check( count_bits(5), 3 );
	return t.ok;
}

bool testuj_beatera() {
	Test t;

	Beater bt(0, 22, true);
	vbc v;

	t.check( bt.bitte(v), "znajduje kolejny" );
	t.check( v.bit_count, 4, "4 bity ustawione" );
	t.check( v.max_val_with_given_bits, 15LL, "value for 4 bits = 15 = b'1111" );
	t.check( bt.bit_lower, 16LL, "kres dolny 16" ); 
	t.check( bt.bit_upper, 22LL, "kres gorny 22" );
	t.check( bt.mode, minimaxi_t::Maxi, "maksymalista" );

	t.check( bt.bitte(v), "znajduje kolejny" );
	t.check( v.bit_count, 3, "3 bity ustawione" );
	t.check( v.max_val_with_given_bits, 21LL, "value for 3 bits = 19 = b'10101" );
	t.check( bt.bit_lower, 22LL, "kres dolny 22" );
	t.check( bt.bit_upper, 22LL, "kres gorny 22" );
	t.check( bt.mode, minimaxi_t::Maxi, "maksymalista" );

	t.check( bt.bitte(v), false, "koniec" );

	return t.ok;
}

bool testuj_make_max_number() {
	Test t;

	t.check( make_max_number(3, 15), 14LL, "[0, 15)  b'1110 (14)" );
	t.check( make_max_number(3, 16), 14LL, "[0, 16)  b'1110 (14)" );
	t.check( make_max_number(3, 17), 14LL, "[0, 17)  b'1110 (14)" );
	t.check( make_max_number(3, 18), 14LL, "[0, 18)  b'1110 (14)" );
	t.check( make_max_number(3, 19), 14LL, "[0, 19)  b'1110 (14)" );
	t.check( make_max_number(3, 20), 19LL, "[0, 20)  b'10011 (19)" );
	t.check( make_max_number(3, 21), 19LL, "[0, 21)  b'10011 (19)" );
	t.check( make_max_number(3, 22), 21LL, "[0, 22)  b'10101 (21)" );
	t.check( make_max_number(3, 23), 22LL, "[0, 23)  b'10110 (22)" );
	t.check( make_max_number(3, 24), 22LL, "[0, 24)  b'10110 (22)" );
	t.check( make_max_number(3, 25), 22LL, "[0, 25)  b'10110 (22)" );

	t.check( make_max_number(1, 15), 8LL, "[0, 15)  b'1000 (8)" );
	t.check( make_max_number(1, 16), 8LL, "[0, 16)  b'1000 (8)" );
	t.check( make_max_number(1, 17), 16LL, "[0, 17)  b'10000 (16)" );
	t.check( make_max_number(1, 18), 16LL, "[0, 18)  b'10000 (16)" );
	t.check( make_max_number(1, 19), 16LL, "[0, 19)  b'10000 (16)" );
	t.check( make_max_number(1, 20), 16LL, "[0, 20)  b'10000 (16)" );
	t.check( make_max_number(1, 21), 16LL, "[0, 21)  b'10000 (16)" );
	t.check( make_max_number(1, 22), 16LL, "[0, 22)  b'10000 (16)" );
	t.check( make_max_number(1, 23), 16LL, "[0, 23)  b'10000 (16)" );
	t.check( make_max_number(1, 24), 16LL, "[0, 24)  b'10000 (16)" );
	t.check( make_max_number(1, 25), 16LL, "[0, 25)  b'10000 (16)" );

	t.check(  make_max_number( 4, 21 ), 15LL, "4 bity < 21");
	return t.ok;
}

bool testuj_beatera2() {
	Test t;
	Beater bt( 2, 6, true );

	return t.ok;
}

bool testuj_tresc1() {
	Test t;
	t.check( wylicz( { 2, -1, 3 + 1 }, 3, 5 ), 9LL, "tresc 1 (3 5 [2, -1, 3])" );
	return t.ok;
}

bool testuj_tresc2() {
	Test t;
	t.check( wylicz( { 1, 1, -1 }, 3, 2 + 1 ), 0LL, "tresc 1 (3 2 [1, 1, -1])" );
	return t.ok;
}

bool testuj_moje() {
	Test t;
	t.check( wylicz( {1, 1, 1}, 3, 64 + 1), 16LL, "moj1 (3 64, [1, 1, 1]");
	t.check( wylicz( {6},       1, 6 + 1),  12LL, "test0004" );
	return t.ok;
}

bool bug1() {
	Test t;
	t.check( get_max_count( 0, 1, 0 ), 0, "0-1" );
	t.check( get_max_count( 0, 2, 0 ), 1, "0-2" );
	t.check( get_max_count( 0, 3, 0 ), 1, "0-3" );
	t.check( get_max_count( 0, 4, 0 ), 2, "0-4" );
	t.check( get_max_count( 0, 5, 0 ), 2, "0-5" );
	t.check( get_max_count( 0, 6, 0 ), 2, "0-6" );
	t.check( get_max_count( 0, 7, 0 ), 2, "0-7" );

	t.check( wylicz( {6}, 1, 6 + 1),  12LL, "test0004" );
	return t.ok;
}

bool bug2() {
	Test t;

	t.check( get_min_count(8, 16), 1, "min_count [8, 16)" );
	t.check( get_min_count(9, 16), 2, "min_count [9, 16)" );

	Beater bt( 2, 15, 0 );
	vbc v;

	t.check( bt.bitte(v), "znajduje kolejny" );
	t.check( v.bit_count, 1, "1 bit ustawiony" );
	t.check( v.max_val_with_given_bits, 8LL, "value for 1 bit = 8 = b'100" );
	t.check( bt.bit_lower, 9LL, "kres dolny 9" );
	t.check( bt.bit_upper, 15LL, "kres gorny 15" );
	t.check( bt.last_bit_count, 1, "Znalezione bity 1" );
	t.check( bt.mode, minimaxi_t::Mini, "minimalista" );

	t.check( bt.bitte(v), "znajduje kolejny" );
	t.check( v.bit_count, 2, "2 bity ustawione" );
	t.check( v.max_val_with_given_bits, 12LL, "value for 2 bits = 12 = b'1100" );
	t.check( bt.bit_lower, 13LL, "kres dolny 13" );
	t.check( bt.bit_upper, 15LL, "kres gorny 15" );
	t.check( bt.mode, minimaxi_t::Mini, "minimalista" );

	t.check( bt.bitte(v), "znajduje kolejny" );
	t.check( v.bit_count, 3, "3 bity ustawione" );
	t.check( v.max_val_with_given_bits, 14LL, "value for 3 bits = 14 = b'1110" );

	t.check( wylicz( {6, 6, -4}, 3, 15 + 1), 28LL, "test0197" );
	return t.ok;
}

bool bug3() {
	Test t;
	t.check( get_min_count( 15, 15 ), 4, "min_count[15,16>" );
	t.check( get_max_count( 14, 15 ), 3, "max_count[14,15>" );
	return t.ok;
}

bool bug4() {
	Test t;
	t.check( wylicz( {-1}, 1, 2 + 1), 0LL, "test0147" );
	return t.ok;
}

int main() {
	Test t;
	t.check( testuj_get_max_count(), "-- get_max_count" );
	t.check( testuj_make_max_number(), "-- make-max-number" );
	t.check( testuj_count_bits(), "-- count_bits()" );

	t.check( testuj_beatera(), "-- Beater" );
	
	t.check( testuj_tresc1(), "-- tresc 1" );
	t.check( testuj_tresc2(), "-- tresc 2" );
	t.check( testuj_moje(), "--- moje" );

	t.check( bug1() );
	t.check( bug2() );
	t.check( bug3() );
	t.check( bug4() );

	return 0;
}

#endif