#include <iostream> using namespace std; long long C(long long b){ int counter = 0; do{ if(b % 2 == 1) counter++; }while((b /= 2) > 0); return counter; } void moreStuff(long long * a, int n, long long m, long long * b, int index, long long & max){ if(index >= n){ long long tMax = 0; for(int i = 0; i < n; ++i){ tMax += a[i] * C(b[i]); } if(tMax > max) max = tMax; return; } for(long long i = b[index - 1] + 1; i <= m; ++i){ b[index] = i; moreStuff(a, n, m, b, index + 1, max); } } long long stuff(long long * a, int n, long long m){ long long b [n]; int index = 0; long long max = 0; for(long long i = 0; i <= m; ++i){ b[index] = i; moreStuff(a, n, m, b, index + 1, max); } return max; } int main(){ int n; long long m; cin >> n >> m; long long a[n]; for(int i = 0; i < n; ++i){ cin >> a[i]; } cout << stuff(a, n, m) << endl; 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 | #include <iostream> using namespace std; long long C(long long b){ int counter = 0; do{ if(b % 2 == 1) counter++; }while((b /= 2) > 0); return counter; } void moreStuff(long long * a, int n, long long m, long long * b, int index, long long & max){ if(index >= n){ long long tMax = 0; for(int i = 0; i < n; ++i){ tMax += a[i] * C(b[i]); } if(tMax > max) max = tMax; return; } for(long long i = b[index - 1] + 1; i <= m; ++i){ b[index] = i; moreStuff(a, n, m, b, index + 1, max); } } long long stuff(long long * a, int n, long long m){ long long b [n]; int index = 0; long long max = 0; for(long long i = 0; i <= m; ++i){ b[index] = i; moreStuff(a, n, m, b, index + 1, max); } return max; } int main(){ int n; long long m; cin >> n >> m; long long a[n]; for(int i = 0; i < n; ++i){ cin >> a[i]; } cout << stuff(a, n, m) << endl; return 0; } |