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
#include <iostream>
#include <cstdio>
#include <string>
#include <sstream> 
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <ctime>
#include <cassert>
#include <random>
using namespace std;

using ll = long long;

const ll INF = 1e18;

int get_num_ones(ll n) {
    int ones = 0;
    while (n > 0) {
        if (n%2 == 1) {
            ++ones;
        }
        n /= 2;
    }
    return ones;
}

int main() {
    ios_base::sync_with_stdio(0);
    int n;
    ll m;
    cin >> n >> m;
    vector<ll> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }

    vector<int> ones(m+1);
    for (int i = 0; i <= m; ++i) {
        ones[i] = get_num_ones(i);
    }
    vector<vector<ll>> f(2, vector<ll>(m+1, -INF));
    int cur = 1;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j <= m; ++j) {
            f[cur][j] = -INF;
        }
        for (int j = i; j <= m; ++j) {
            if (i == 0) {
                f[cur][j] = a[i]*ones[j];
            } else {
                f[cur][j] = max(f[cur][j], f[1-cur][j-1] + a[i]*ones[j]);
            }
        }
        for (int j = 1; j <= m; ++j) {
            f[cur][j] = max(f[cur][j], f[cur][j-1]);
        }
        cur = 1-cur;
    }
    cout << f[1-cur].back() << endl;
    return 0;
}