#include <bits/stdc++.h> using ll = long long; using ull = unsigned long long; using namespace std; ll cntBits(ll n) { static unordered_map<ll,ll> hash; if(hash.find(n) != hash.end()) return hash[n]; if (n == 0) return 0; else { hash[n] = 1 + cntBits(n & (n - 1)); return hash[n]; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n,m,x,bit; cin>>n>>m; vector<ll> v; for(ll i=0;i<n;i++) { cin>>x; v.emplace_back(x); } vector<ll> numbers(m+1); iota(numbers.begin(), numbers.end(), 0); ll SUM=0,sum=0; do { if(is_sorted(numbers.begin(), numbers.begin()+n)) { sum = 0; for(int i=0;i<n;i++) { sum+=v[i]*cntBits(numbers[i]); } SUM = max(SUM,sum); } } while(std::next_permutation(numbers.begin(), numbers.end())); cout<<SUM<<'\n'; return 0; }
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 | #include <bits/stdc++.h> using ll = long long; using ull = unsigned long long; using namespace std; ll cntBits(ll n) { static unordered_map<ll,ll> hash; if(hash.find(n) != hash.end()) return hash[n]; if (n == 0) return 0; else { hash[n] = 1 + cntBits(n & (n - 1)); return hash[n]; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n,m,x,bit; cin>>n>>m; vector<ll> v; for(ll i=0;i<n;i++) { cin>>x; v.emplace_back(x); } vector<ll> numbers(m+1); iota(numbers.begin(), numbers.end(), 0); ll SUM=0,sum=0; do { if(is_sorted(numbers.begin(), numbers.begin()+n)) { sum = 0; for(int i=0;i<n;i++) { sum+=v[i]*cntBits(numbers[i]); } SUM = max(SUM,sum); } } while(std::next_permutation(numbers.begin(), numbers.end())); cout<<SUM<<'\n'; return 0; } |