# [均摊复杂度] Codeforces 1637E Best Pair

## 题目大意

• 定义 $$cnt_x$$​ 为 $$x$$ 在数组 $$a$$ 中出现的次数
• 定义 $$f(x,y)=(cnt_x+cnt_y)\cdot(x+y)$$

• $$t$$ 组数据，$$1\leq t\leq 10^4$$
• $$2\leq n,\sum n\leq 3×10^5,0\leq m,\sum m\leq 3×10^5$$
• $$1\leq a_i\leq 10^9$$

## Code

#include <bits/stdc++.h>
using namespace std;

#define LL long long

template<typename elemType>
elemType X = 0, w = 0; char ch = 0;
while (!isdigit(ch)) { w |= ch == '-';ch = getchar(); }
while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
T = (w ? -X : X);
}

map<int, int> cnt;
vector<int> vec[300010];
set<pair<int, int>> s;
int a[300010];
int T, n, m;

LL solve() {
LL ans = 0;
for (auto v : cnt) {
int x = v.first;
for (int i = 1;i <= v.second;++i) {
for (auto y : vec[i]) {
if (x == y || s.count(make_pair(min(x, y), max(x, y)))) continue;
ans = max(ans, 1LL * (x + y) * (v.second + i));
break;
}
}
}
return ans;
}

int main() {
while (T--) {
cnt.clear(); s.clear();
for (int i = 1;i <= n;++i) { Read(a[i]); ++cnt[a[i]]; vec[i].clear(); }
for (auto v : cnt)
vec[v.second].push_back(v.first);
for (int i = 1;i <= n;++i)
if (!vec[i].empty()) sort(vec[i].begin(), vec[i].end(), greater<int>());
for (int i = 1;i <= m;++i) {