Advertisement
skaram

Untitled

Sep 23rd, 2022
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. #pragma GCC optimize("O3")
  2. #pragma GCC optimize("Ofast")
  3. #include <bits/stdc++.h>
  4. #include <ext/pb_ds/assoc_container.hpp>
  5. #ifdef Local
  6. #include "debug/debug.h"
  7. #else
  8. #define debug(...)
  9. #endif
  10.  
  11. typedef long long ll;
  12. typedef long double ld;
  13.  
  14. #define int ll
  15.  
  16. #define vec vector
  17. #define str string
  18. #define all(x) (x).begin(), (x).end()
  19. #define rall(x) (x).rbegin(), (x).rend()
  20. #define rev(x) reverse(x)
  21. #define sz(x) (int)(x).size()
  22. #define pb push_back
  23.  
  24. using namespace std;
  25. using namespace __gnu_pbds;
  26.  
  27. void solve() {
  28.     int n, q;
  29.     cin >> n >> q;
  30.     vec<int> a(n);
  31.     gp_hash_table<int, int> cnt;
  32.     for (int& i : a) {
  33.         cin >> i;
  34.         cnt[i]++;
  35.     }
  36.     gp_hash_table<int, vec<int>> c;
  37.     for (auto& [k, i] : cnt) {
  38.         c[i].pb(k);
  39.     }
  40.     for (auto& [k, i] : c) {
  41.         sort(rall(i));
  42.     }
  43.     gp_hash_table<int, int> qs;
  44.     auto h = [&](int& a, int& b) { return min(a, b) << 30 | max(a, b); };
  45.     for (int i = 0, x, y; i < q; i++) {
  46.         cin >> x >> y;
  47.         qs[h(x, y)] = 1;
  48.     }
  49.     int ans = 0;
  50.     for (int i = 0; i < sz(a); i++) {
  51.         int x = a[i], g = cnt[x];
  52.         for (auto& [k, j] : c) {
  53.             if (k < g)
  54.                 continue;
  55.             for (int& y : j) {
  56.                 if (x != y && qs.find(h(x, y)) == qs.end()) {
  57.                     ans = max(ans, (cnt[x] + cnt[y]) * (x + y));
  58.                     break;
  59.                 }
  60.             }
  61.         }
  62.     }
  63.     cout << ans << '\n';
  64. }
  65.  
  66. signed main() {
  67.     ios::sync_with_stdio(false);
  68.     cin.tie(nullptr);
  69.  
  70.     int tt = 1;
  71.     cin >> tt;
  72.     while (tt--)
  73.         solve();
  74.  
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement