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

#define pb push_back
#define pf push_front
#define turbo cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false);
#define fi first
#define sc second
#define ll long long
#define mp make_pair
#define un unsigned

#define debug 0

using namespace std;

const int ile=2007;

int input[ile];

inline int wzm(int x){
    int wynik=0;
    while(x!=0){
        wynik+=(x&1);
        x/=2;
    }
    return wynik;
}
int main()
{
    turbo
    int n;
    ll m;
    cin>>n>>m;
    for(int i=0;i<n;i++)
        cin>>input[i];
    int mask[m+1];
    for(ll i=0;i<=m;i++){
        if(i<m-n+1)
            mask[i]=0;
        else
            mask[i]=1;
    }
    ll maxi=LLONG_MIN;
    ll suma=0;
    int poz=0;
    for(ll i=0;i<=m;i++){
        if(mask[i]){
        suma+=input[poz]*wzm(i);
        poz++;
        }
    }
    maxi=max(maxi,suma);
    while(next_permutation(mask,mask+m+1)){
        poz=0;
        suma=0;
        for(ll i=0;i<=m;i++){
            if(mask[i]){
                suma+=input[poz]*wzm(i);
                poz++;
            }
        }
        maxi=max(maxi,suma);
    }
    cout<<maxi<<"\n";
    return 0;
}