#include <bits/stdc++.h> using namespace std; int bin(int a) { int wyn = 0; while(a > 0) { wyn += a % 2; a /= 2; } return wyn; } int main() { int n; long long m, wyn = 0, a; vector <pair <int, int> > b; scanf("%d" "%lld", &n, &m); for(int i = 0; i <= m; i++) b.push_back(make_pair(i, bin(i))); sort(b.begin(), b.end()); for(int i = 0; i < n; i++) { scanf("%lld", &a); wyn += a * b[b.size() - n + i].second; } printf("%lld", wyn); }
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 | #include <bits/stdc++.h> using namespace std; int bin(int a) { int wyn = 0; while(a > 0) { wyn += a % 2; a /= 2; } return wyn; } int main() { int n; long long m, wyn = 0, a; vector <pair <int, int> > b; scanf("%d" "%lld", &n, &m); for(int i = 0; i <= m; i++) b.push_back(make_pair(i, bin(i))); sort(b.begin(), b.end()); for(int i = 0; i < n; i++) { scanf("%lld", &a); wyn += a * b[b.size() - n + i].second; } printf("%lld", wyn); } |