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

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    unsigned long long int m;
    long long int a;
    int cnt = 0;

    cin >> n >> m;

    vector<long long int> scores(n);

    for (int i = 0; i < n; i++) {
        cin >> a;
        scores[i] = a;
    }
    
    int max_idx;
    vector<long long int> max_vals(n);

    for (unsigned long long b = 1; b <= m; ++b) {
        int binary_ones = 0;
        unsigned long long other_b = b;
        while (other_b != 0) {
            other_b = other_b & (other_b - 1);
            binary_ones++;
        }


        if (b < n) {
            max_idx = (int) b;
        } else {
            max_idx = n - 1;
        }


        for (; max_idx > 0; --max_idx) {
            long long int curr = binary_ones * scores[max_idx];


            if (b > max_idx) {
                max_vals[max_idx] = max((max_vals[max_idx - 1] + curr), max_vals[max_idx]);
            } else {
                max_vals[max_idx] = max_vals[max_idx - 1] + curr;
            }
        }

        max_vals[0] = max(max_vals[0], scores[0] * binary_ones);
        
    }

    cout << max_vals[n - 1] << '\n';

    return 0;
}