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 <bits/stdc++.h>

#define F first
#define S second
#define pb push_back
#define ll long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define eb emplace_back
#define pll pair <ll, ll>
#define pii pair<int, int>
#define sz(x) int((x).size())
#define cd complex<long double>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define vpii vector<pii>
#define sqr(x) ((x) * (x))
#define fenwick(x) ((x) & -(x))
#define debug(x) cout << "[ " << #x << " ]: " << (x) << endl;
#define FOR(a, b, c) for(int a = b; a < c; ++a)
#define we ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;

ll pop(ll x)
{
    return __builtin_popcountll(x);
}

int main()
{
    we
    
    int N, M; cin >> N >> M;
    vector <ll> tab(N);
    vector < vector < ll > > dp(N + 10, vector <ll> (M + 123, -(1LL << 62LL)));
    vector < vector < ll > > maxa(N + 10, vector <ll> (M + 123, -(1LL << 62LL)));
    for(auto &p : tab) cin >> p;
    dp[0][0] = maxa[0][0] = tab[0]*pop(0);
    for(int i = 1; i <= M; ++i)
    {
        dp[0][i] = tab[0]*pop(i);
        maxa[0][i] = max(dp[0][i], maxa[0][i - 1]);
    }

    for(int i = 1; i < N; ++i)
    {
        for(int j = 1; j <= M; ++j)
        {
            ll mx = maxa[i - 1][j - 1];
            dp[i][j] = mx + tab[i] * pop(j);
            maxa[i][j] = max(dp[i][j], maxa[i][j - 1]);
        }
    }

    ll res = -(1LL << 62LL);

    for(int i = 0; i <= M; ++i)
    {
        res = max(res, dp[N - 1][i]);
    }

    cout << res << "\n";

    return 0;
}