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
#include <iostream>
#include <algorithm>
using namespace std;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int n,m;
	cin >> n >> m;
	
	long long *A = new long long[n];
	for(int i=0;i<n;i++)
		cin >> A[i];
	
	long long *actDp = new long long[m+1];
	long long *prevDp = new long long[m+1];
	fill(prevDp, prevDp+m+1, 0ll);
	
	for(int i=0;i<n;i++){
		actDp[i]=(i>0 ? prevDp[i-1] : 0)+A[i]*__builtin_popcount(i);
		for(long long mx=i+1;mx<=m;mx++)
			actDp[mx] = max(actDp[mx-1], prevDp[mx-1]+A[i]*__builtin_popcountll(mx));
		swap(actDp, prevDp);
	}
	
	cout << prevDp[m] << "\n";
	
	delete []prevDp;
	delete []actDp;
	delete []A;
	return 0;
}