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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <string>

using namespace std;

vector<long long int> sekundy;
vector<long long int> mnozniki;
long long int suma=0;
bool pierwsze=true;
long long int sekund,maxmnoznik;


unsigned long long int countSetBits(unsigned long long int n)
{
    unsigned long long int count = 0;
    while (n) {
        count += n & 1;
        n >>= 1;
    }
    return count;
}


template<typename T>
void pretty_print(const T& c, long long int combo)
{
    vector<long long int> wiersz;
    long long int n = c.size();
    for (long long int i = 0; i < n; ++i) {
        if ((combo >> i) & 1)
            wiersz.push_back(countSetBits(c[i]));
    }
    long long int suman=0;
    for(long long int s=0;s<wiersz.size();s++)
    {
        suman += wiersz[s]*sekundy[s];
    }
    if(pierwsze)
    {
        suma=suman;
        pierwsze=false;
    }
    else if(suman>suma)
    {
        suma=suman;
    }
}

template<typename T>
void combo(const T& c, long long int k)
{
    long long int n = c.size();
    long long int combo = (1 << k) - 1;
    while (combo < 1<<n) {

        pretty_print(c, combo);

        long long int x = combo & -combo;
        long long int y = combo + x;
        long long int z = (combo & ~y);
        combo = z / x;
        combo >>= 1;
        combo |= y;
    }
}



int main()
{
    cin>>sekund>>maxmnoznik;
    for(long long int i=0;i<sekund;i++)
    {
        long long int sekunda;
        cin>>sekunda;
        sekundy.push_back(sekunda);
        mnozniki.push_back(i);
    }
    vector<long long int> c0;
    for(long long int c=0;c<=maxmnoznik;c++)
    {
        c0.push_back(c);
    }
    combo(c0, sekund);
    cout<<suma<<endl;
    return 0;
}