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
#include <iostream>
#include <vector>

using namespace std;

int Bits(int number)
{
	int n = 0;

	while (number != 0)
	{
		if (number & 1)
		{
			n++;
		}

		number >>= 1;
	}

	return n;
}

vector<long long> song;
int songLength;
long long maxPower;

long long Solve(int currentSecond, long long minPower, long long value, int stage)
{
	if (currentSecond >= songLength)
	{
		return value;
	}

	long long max = -1, t;
	for (long long i = minPower; i <= maxPower; i++)
	{
		long long v = value + (Bits(i) * song[currentSecond]);

		t = Solve(currentSecond + 1, i + 1, v, stage + 1);

		if (t > max)
		{
			max = t;
		}
	}

	return max;
}

int main()
{
	long long temp;

	cin >> songLength >> maxPower;

	for (int i = 0; i < songLength; i++)
	{
		cin >> temp;
		song.push_back(temp);
	}

	cout << Solve(0, 0, 0, 0) << endl;

	return 0;
}