Algorithm
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
int const N = 2e5 + 1;
int n, a[N], fr[N];
vector<int> all;
int main() {
scanf("%d", &n);
for(int i = 0; i < n; ++i)
scanf("%d", a + i), all.push_back(a[i]);
sort(all.begin(), all.end());
all.resize(unique(all.begin(), all.end()) - all.begin());
for(int i = 0; i < n; ++i)
a[i] = lower_bound(all.begin(), all.end(), a[i]) - all.begin(),
++fr[a[i]];
all.clear();
for(int i = 0; i < N; ++i)
if(fr[i] != 0)
all.push_back(fr[i]);
sort(all.begin(), all.end());
int res = 0, tmp, idx, cur;
for(int i = 1; i <= all.back(); ++i) {
tmp = 0, idx = -1, cur = i;
while(true) {
idx = lower_bound(all.begin() + idx + 1, all.end(), cur) - all.begin();
if(idx == all.size())
break;
tmp += cur;
cur += cur;
}
res = max(res, tmp);
}
printf("%d\n", res);
return 0;
}
Copy The Code &
Try With Live Editor
Input
18
2 1 2 10 2 10 10 2 2 1 10 10 10 10 1 1 10 10
2 1 2 10 2 10 10 2 2 1 10 10 10 10 1 1 10 10
Output
14
Demonstration
Codeforces Solution-E. Thematic Contests-Solution in C, C++, Java, Python ,Thematic Contests,Codeforces Solution