malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int a[1000005];   long long dp[1000005];   multiset<int> se;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int i, c, j, k, l, m, n, t, r, cnt;     int flag = 0;     long long ans = 0, an = 0;     t = 1;     while (t--) {       scanf( %d , &n);       scanf( %d , &c);       for (i = 1; i <= n; i++) {         scanf( %d , &a[i]);       }       se.insert(0);       for (i = 1; i < c; i++) {         se.insert(a[i]);         dp[i] = dp[i - 1] + a[i];       }       an = dp[c - 1];       for (i = c; i <= n; i++) {         se.insert(a[i]);         se.erase(se.find(a[i - c]));         an += (a[i] - a[i - c]);         dp[i] = min(dp[i - 1] + a[i], dp[i - c] + an - *se.begin());       }       cout << dp[n] << endl;     }   }
#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:102400000,102400000 )   using namespace std;   template <class _T>   inline _T loww(_T n) {     return n & (-n);   }   template <class _T>   inline void swapp(_T &x, _T &y) {     _T t;     t = x, x = y, y = t;   }   template <class _T>   inline _T minn(_T i, _T j) {     return i > j ? j : i;   }   template <class _T>   inline _T maxx(_T i, _T j) {     return i > j ? i : j;   }   template <class _T>   inline _T gcd(_T b, _T c) {     return b && c ? (c ? gcd(c, b % c) : b) : b | c;   }   template <class _T>   inline _T abss(_T b) {     return b > 0 ? b : -b;   }   struct node {     int v, col;   };   node a[500005 << 2];   void DOWN(int l, int r, int dq) {     if (a[dq].col) {       if (l != r) {         a[dq << 1].col = a[dq << 1].v = a[dq << 1 | 1].col = a[dq << 1 | 1].v =             a[dq].col;       }       a[dq].col = 0;     }   }   void UP(int l, int r, int dq) {     DOWN(l, r, dq);     if (a[dq << 1].v == a[dq << 1 | 1].v)       a[dq].v = a[dq << 1].v;     else       a[dq].v = -1;   }   void U(int l, int r, int dq, int b, int c, int v) {     DOWN(l, r, dq);     if (l == r) {       a[dq].v = v;     } else {       if (b <= l && r <= c) {         a[dq].v = a[dq].col = v;       } else {         int mid = (l + r) >> 1;         if (b <= mid) U(l, mid, dq << 1, b, c, v);         if (c > mid) U(mid + 1, r, dq << 1 | 1, b, c, v);         UP(l, r, dq);       }     }   }   int Q(int l, int r, int dq, int b) {     DOWN(l, r, dq);     if (l == r || a[dq].v != -1)       return a[dq].v;     else {       int mid = (l + r) >> 1;       if (b <= mid)         return Q(l, mid, dq << 1, b);       else         return Q(mid + 1, r, dq << 1 | 1, b);     }   }   void build(int l, int r, int dq) {     if (l == r) {       a[dq].col = a[dq].v = 0;     } else {       a[dq].col = a[dq].v = 0;       int mid = (l + r) >> 1;       build(l, mid, dq << 1);       build(mid + 1, r, dq << 1 | 1);     }   }   bool outflag = true;   void out(int b) {     if (outflag)       outflag = false;     else       printf(   );     printf( %d , b);   }   pair<int, int> goal[500005];   int val[500005];   map<int, int> mymap;   int pre[500005], gl = 0;   int en = 0;   int main() {     int n, i, t, js, told, b;     scanf( %d , &n);     for (i = 1; i <= n; i++) {       scanf( %d , &val[i]);     }     build(1, n, 1);     for (i = 1; i <= n; i++) {       pre[i] = mymap[val[i]];       mymap[val[i]] = i;       t = pre[i];       told = i;       js = 0;       while (t > en) {         if (js++ > 1) break;         if (t + 1 <= told - 1) U(1, n, 1, t + 1, told - 1, val[i]);         b = Q(1, n, 1, t);         if (b != 0) {           goal[gl++] = make_pair(b, val[i]);           en = i;           break;         }         told = t;         t = pre[t];       }       if (js == 3) {         goal[gl++] = make_pair(val[i], val[i]);         en = i;       }     }     printf( %d n , gl * 4);     for (i = 0; i < gl; i++) {       out(goal[i].first);       out(goal[i].second);       out(goal[i].first);       out(goal[i].second);     }     printf(  n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[200010];   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     for (int i = 1; i <= (n + 1) / 2; i++) {       if (i % 2 == 1) {         int temp;         temp = a[i];         a[i] = a[n + 1 - i];         a[n + 1 - i] = temp;       }     }     for (int i = 1; i < n; i++) printf( %d  , a[i]);     printf( %d n , a[n]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[40][40];   int f[40][40];   int main() {     int i, j, k, l, o, n, nn, best, ans, tot, sum, better, tmp;     scanf( %d , &n);     for (i = 0; i < n; i++)       for (j = 0; j < n; j++) scanf( %d , &a[i][j]);     nn = (n + 1) / 2;     ans = -2000000000;     for (i = 0; i < (1 << nn); i++) {       for (j = 0; j < nn; j++)         if (((1 << j) & i) == 0)           f[j][nn - 1] = 0;         else           f[j][nn - 1] = 1;       for (j = nn; j < n; j++)         f[j][nn - 1] = f[j - nn][nn - 1] ^ f[nn - 1][nn - 1];       tot = 0;       for (j = 0; j < nn - 1; j++) {         best = -2000000000;         for (k = 0; k < 2; k++) {           f[nn - 1][j] = k;           f[nn - 1][j + nn] = f[nn - 1][j] ^ f[nn - 1][nn - 1];           sum = 0;           for (l = 0; l < nn - 1; l++) {             better = -2000000000;             for (o = 0; o < 2; o++) {               f[l][j] = o;               f[l + nn][j] = f[l][j] ^ f[nn - 1][j];               f[l][j + nn] = f[l][j] ^ f[l][nn - 1];               f[l + nn][j + nn] = f[l + nn][j] ^ f[l + nn][nn - 1];               tmp = 0;               tmp += (f[l][j] == 0 ? a[l][j] : -a[l][j]);               tmp += (f[l + nn][j] == 0 ? a[l + nn][j] : -a[l + nn][j]);               tmp += (f[l][j + nn] == 0 ? a[l][j + nn] : -a[l][j + nn]);               tmp += (f[l + nn][j + nn] == 0 ? a[l + nn][j + nn]                                              : -a[l + nn][j + nn]);               if (tmp > better) better = tmp;             }             sum += better;           }           sum += (f[nn - 1][j] == 0 ? a[nn - 1][j] : -a[nn - 1][j]);           sum +=               (f[nn - 1][j + nn] == 0 ? a[nn - 1][j + nn] : -a[nn - 1][j + nn]);           if (sum > best) best = sum;         }         tot += best;       }       for (j = 0; j < n; j++)         tot += (f[j][nn - 1] == 0 ? a[j][nn - 1] : -a[j][nn - 1]);       if (tot > ans) ans = tot;     }     printf( %d n , ans);   }
#include <bits/stdc++.h>   using namespace std;   const int N = 303;   int n;   bool eq[N][N];   int dp[N][N];   string s[N];   int main() {     cin >> n;     int allsum = n - 1;     for (int i = 0; i < n; ++i) {       cin >> s[i];       allsum += s[i].size();     }     for (int i = 0; i < n; ++i) {       eq[i][i] = true;       for (int j = 0; j < i; ++j) eq[i][j] = eq[j][i] = s[i] == s[j];     }     for (int i = n - 1; i >= 0; --i) {       for (int j = n - 1; j >= 0; --j) {         if (eq[i][j]) {           if (i + 1 < n && j + 1 < n)             dp[i][j] = dp[i + 1][j + 1] + 1;           else             dp[i][j] = 1;         }       }     }     int ans = allsum;     for (int i = 0; i < n; ++i) {       int sum = 0;       for (int j = 0; i + j < n; ++j) {         sum += s[i + j].size();         int cnt = 1;         for (int pos = i + j + 1; pos < n; ++pos) {           if (dp[i][pos] > j) {             ++cnt;             pos += j;           }         }         int cur = allsum - sum * cnt + (j + 1) * cnt - j * cnt;         if (cnt > 1 && ans > cur) {           ans = cur;         }       }     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int par[21], siz[21];   int root(int a) {     while (a != par[a]) {       a = par[a];     }     return a;   }   void unionn(int a, int b) {     int root_a = root(a);     int root_b = root(b);     if (root_a == root_b) {       return;     }     if (siz[root_a] < siz[root_b]) {       siz[root_b] += siz[root_a];       par[root_a] = root_b;     } else {       siz[root_a] += siz[root_b];       par[root_b] = root_a;     }   }   void init() {     int i;     for (i = 0; i <= 20; i++) {       siz[i] = 1;       par[i] = i;     }   }   int main() {     int t;     cin >> t;     while (t--) {       init();       int n;       cin >> n;       int i;       string s1, s2;       cin >> s1 >> s2;       s1 =  @  + s1;       s2 =  @  + s2;       int flag = 0;       for (i = 1; i <= n; i++) {         if (s1[i] > s2[i]) {           flag = 1;         }       }       if (flag == 1) {         cout <<  -1 n ;         continue;       }       set<int> v[20];       int ans = 0;       for (i = 1; i <= n; i++) {         if (s1[i] != s2[i]) v[s2[i] -  a ].insert(s1[i] -  a );       }       map<pair<int, int>, int> m;       set<int> sp;       for (i = 1; i <= 19; i++) {         if (v[i].size() == 0) {           continue;         }         set<int> sp;         for (auto it = v[i].begin(); it != v[i].end(); it++) {           sp.insert(root(*it));           unionn(*it, i);         }         ans += (int)(sp.size());       }       cout << ans << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   int w[100005];   long long p[100005];   int e[2005];   int edge(int n) {     int ans = n * (n - 1) / 2;     if (!(n & 1)) ans += n / 2 - 1;     return ans;   }   int main() {     int n, m, q;     cin >> n >> m;     for (int i = 0; i < m; i++) cin >> q >> w[i];     sort(w, w + m, greater<int>());     m = min(m, 2005);     while (m >= 2005 || edge(m) > n - 1) m--;     long long ans = 0LL;     for (int i = 0; i < m; i++) ans += w[i];     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mod = 1000000007;   inline int add(int a, int b) {     if ((a += b) >= mod) a -= mod;     return a;   }   inline int mult(int a, int b) {     long long t = 1ll * a * b;     if (t >= mod) t %= mod;     return t;   }   inline int dec(int a, int b) {     if ((a -= b) < 0) a += mod;     return a;   }   inline int power(int a, int b) {     int out = 1;     while (b) {       if (b & 1) out = mult(out, a);       a = mult(a, a);       b >>= 1;     }     return out;   }   int n, a[2510], f[100010], ans, mx, sum;   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]), sum += a[i];     mx = *max_element(a + 1, a + n + 1);     f[1] = mult(mult(sum - 1, sum - 1), power(sum, mod - 2));     for (int i = 1; i < mx; i++)       f[i + 1] = dec(add(f[i], f[i]),                      add(f[i - 1], mult(sum - 1, power(sum - i, mod - 2))));     for (int i = 1; i <= n; i++) ans = add(ans, f[a[i]]);     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int solve(void) {     int n, s;     cin >> n >> s;     set<int> avail;     char c;     for (int i = 0; i < n; i++) {       cin >> c;       avail.insert((int)c -  a  + 1);     }     int cnt = 1;     int ans = *avail.begin();     int prev = ans;     avail.erase(avail.begin());     while (cnt < s) {       if (avail.empty()) return -1;       while (*avail.begin() < prev + 2) {         avail.erase(avail.begin());         if (avail.empty()) return -1;       }       prev = *avail.begin();       ans += prev;       avail.erase(avail.begin());       cnt++;     }     return ans;   }   int main(void) { cout << solve() << endl; }
#include <bits/stdc++.h>   using namespace std;   inline long long mod(long long n, long long m) {     long long ret = n % m;     if (ret < 0LL) ret += m;     return ret;   }   long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); }   long long exp(long long a, long long b, long long m) {     if (b == 0LL) return 1LL;     if (b == 1LL) return mod(a, m);     long long k = mod(exp(a, b / 2, m), m);     if (b & 1LL) {       return mod(a * mod(k * k, m), m);     } else       return mod(k * k, m);   }   const long long MAXN = 300100;   long long res[MAXN];   pair<long long, long long> P[MAXN];   long long dist(pair<long long, long long> a, pair<long long, long long> b) {     return abs(a.first - b.first) + abs(a.second - b.second);   }   long long n;   long long dp[MAXN][3][4][4];   const long long inf = 2e9;   long long solve(long long id, long long j, long long xo, long long yo) {     if (j == 3) {       if (xo == 3 && yo == 3) return 0;       return -inf;     }     if (id == n + 1) {       return -inf;     }     long long &x = dp[id][j][xo][yo];     if (x != -1) return x;     x = 0;     long long op0 = solve(id + 1, j, xo, yo);     x = op0;     for (long long it1 = 0; it1 <= 2; it1++) {       for (long long it2 = 0; it2 <= 2; it2++) {         long long sum1 = it1 * P[id].first - (2 - it1) * P[id].first;         long long sum2 = it2 * P[id].second - (2 - it2) * P[id].second;         if (it1 + xo <= 3 && it2 + yo <= 3) {           x = max(x, sum1 + sum2 + solve(id + 1, j + 1, xo + it1, yo + it2));         }       }     }     return x;   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     cin >> n;     for (long long i = 1; i <= n; i++) {       cin >> P[i].first >> P[i].second;     }     memset(dp, -1, sizeof(dp));     long long tot = 0;     for (long long i = 1; i <= n; i++) {       tot += dist(P[i], P[(i == n ? 1 : i + 1)]);     }     cout << solve(1, 0, 0, 0) <<    ;     for (long long i = 4; i <= n; i++) cout << tot <<    ;     cout <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   int s[100100];   int main() {     scanf( %d , &n);     if (n % 4 == 2 || n % 4 == 3)       puts( -1 );     else {       for (int i = 0; i < n / 2; i += 2)         s[i] = i + 2, s[i + 1] = n - i, s[n - i - 1] = n - i - 1,         s[n - i - 2] = i + 1;       if (n % 4 == 1) s[n / 2] = n / 2 + 1;       for (int i = 0; i < n; i++) printf( %d  , s[i]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[200000];   int main() {     int n;     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> a[i];     }     sort(a, a + n);     int q;     cin >> q;     for (int i = 0; i < q; ++i) {       int m;       cin >> m;       int l = 0, r = n - 1;       while (l <= r) {         int mid = (l + r) / 2;         if (a[mid] > m) {           r = mid - 1;         } else {           l = mid + 1;         }       }       cout << l << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1010;   int n, m;   int g[N][N];   int ans = 0;   inline void Solve(int l, int r, int K, int dep) {     if (l >= r) return;     ans = ((ans) < (dep) ? (dep) : (ans));     int siz = (r - l + 1) / K;     if (!siz) ++siz;     int last = l, cnt = 0;     vector<pair<int, int> > S;     while (last <= r && cnt < K)       ++cnt, Solve(last, cnt == K ? r : min(last + siz - 1, r), K, dep + 1),           S.push_back(make_pair(last, cnt == K ? r : min(last + siz - 1, r))),           last += siz;     cerr <<  Solve(  << l <<  ,  << r <<  ,  << K <<  ,  << dep <<  ) n ;     cerr <<  S:  ;     for (auto x : S) cerr <<  [  << x.first <<  ,  << x.second <<  ]  ;     cerr << endl;     for (register int i = 0; i < (int)S.size(); ++i)       for (register int j = i + 1; j < (int)S.size(); ++j)         for (register int k = S[i].first; k <= S[i].second; ++k)           for (register int l = S[j].first; l <= S[j].second; ++l) g[k][l] = dep;   }   int main() {     scanf( %d%d , &n, &m);     for (register int len = 1; len < n; len *= m) {       ++ans;       for (register int i = 0; i < n; i += len)         for (register int j = 0; j < n; j += len)           if (i != j)             for (register int x = 0; x < len && i + x < n; ++x)               for (register int y = 0; y < len && j + y < n; ++y)                 g[i + x + 1][j + y + 1] = ans;     }     printf( %d n , ans);     for (register int i = 1; i <= n; ++i)       for (register int j = i + 1; j <= n; ++j) printf( %d  , g[i][j]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, l, sum = 0;     cin >> n;     string st;     for (l = 0; l < n; l++) {       cin >> st;       if (st[0] ==  T )         sum = sum + 4;       else if (st[0] ==  C )         sum = sum + 6;       else if (st[0] ==  O )         sum = sum + 8;       else if (st[0] ==  D )         sum = sum + 12;       else         sum = sum + 20;     }     cout << sum;     return 0;   }
#include <bits/stdc++.h>   const long long inf = 1000000000;   const long long inf64 = inf * inf;   const double pi = acos(-1.0);   long long Abs(long long x) { return (x >= 0 ? x : -x); }   using namespace std;   bool solve() {     int n;     cin >> n;     vector<vector<long long> > mat(n, vector<long long>(n));     vector<long long> ans(n, 0);     for (int i(0); i < n; i++)       for (int j(0); j < n; j++) cin >> mat[i][j];     for (int i(0); i < n; i++) {       for (int j(0); j < n; j++) {         if (i == j) continue;         ans[j] |= mat[i][j];       }     }     for (int i(0); i < n; i++) cout << ans[i] <<    ;     return true;   }   int main() {     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) {     if (b > a) {       return gcd(b, a);     }     if (b == 0) {       return a;     }     return gcd(b, a % b);   }   long long expo(long long a, long long b, long long mod) {     long long res = 1;     while (b > 0) {       if (b & 1) res = (res * a) % mod;       a = (a * a) % mod;       b = b >> 1;     }     return res;   }   vector<int> sieve(int n) {     int *arr = new int[n + 1]();     vector<int> vect;     for (int i = 2; i <= n; i++)       if (arr[i] == 0) {         vect.push_back(i);         for (int j = 2 * i; j <= n; j += i) arr[j] = 1;       }     return vect;   }   void extendgcd(long long a, long long b, long long *v) {     if (b == 0) {       v[0] = 1;       v[1] = 0;       v[2] = a;       return;     }     extendgcd(b, a % b, v);     long long x = v[1];     v[1] = v[0] - v[1] * (a / b);     v[0] = x;     return;   }   long long mminv(long long a, long long b) {     long long arr[3];     extendgcd(a, b, arr);     return arr[0];   }   long long mminvprime(long long a, long long b) { return expo(a, b - 2, b); }   bool revsort(long long a, long long b) { return a > b; }   void swap(int &x, int &y) {     int temp = x;     x = y;     y = temp;   }   long long combination(long long n, long long r, long long m, long long *fact) {     long long val1 = fact[n];     long long val2 = mminvprime(fact[r], m);     long long val3 = mminvprime(fact[n - r], m);     return ((val1 * val2) % m * val3) % m;   }   void dfs(int start, vector<int> *edges, bool *visited, int *blues, int *reds,            int *color, vector<pair<int, int>> &childs) {     if (color[start] == 1) blues[start]++;     if (color[start] == 2) reds[start]++;     visited[start] = true;     for (int i = 0; i < edges[start].size(); i++) {       if (visited[edges[start][i]]) continue;       dfs(edges[start][i], edges, visited, blues, reds, color, childs);       blues[start] += blues[edges[start][i]];       reds[start] += reds[edges[start][i]];       childs.push_back({start, edges[start][i]});     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int n;     cin >> n;     int color[n];     int sum1 = 0;     int sum2 = 0;     for (int i = 0; i < n; i++) {       cin >> color[i];       if (color[i] == 1) sum1++;       if (color[i] == 2) sum2++;     }     vector<int> *edges = new vector<int>[n];     vector<int> *ans1 = new vector<int>[n];     for (int i = 0; i < n - 1; i++) {       int u, v;       cin >> u >> v;       u--;       v--;       edges[u].push_back(v);       edges[v].push_back(u);       ans1[u].push_back(1);       ans1[v].push_back(1);     }     int *blues = new int[n]();     int *reds = new int[n]();     bool *visited = new bool[n]();     vector<pair<int, int>> childs;     dfs(0, edges, visited, blues, reds, color, childs);     int ans = 0;     set<pair<int, int>> s;     for (int i = 0; i < childs.size(); i++) {       int x = childs[i].second;       int blue = blues[x];       int red = reds[x];       if (blue == 0 && red == sum2) ans++;       if (red == 0 && blue == sum1) ans++;     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int N;     string team1 =   ;     string team2 =   ;     int team1points = 0;     int team2points = 0;     cin >> N;     while (N--) {       string goalTeam;       cin >> goalTeam;       if (team1 == goalTeam) {         team1points++;       } else if (team2 == goalTeam) {         team2points++;       } else if (team1 ==   ) {         team1 = goalTeam;         team1points++;       } else if (team2 ==   ) {         team2 = goalTeam;         team2points++;       }     }     cout << (team1points > team2points ? team1 : team2) << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool possible = 1;   void change(char str[], char mask[]) {     char best[6];     best[0] =   0 ;     for (int i = 0; i < (4); ++i) {       for (int d = 0; d < (10); ++d)         if (!(i == 0 && d == 0)) {           char temp = str[i];           str[i] = (d +  0 );           if (strcmp(str, mask) >= 0) {             if (best[0] ==   0 )               strcpy(best, str);             else if (strcmp(best, str) > 0)               strcpy(best, str);           }           str[i] = temp;         }     }     if (strcmp(best,  2011 ) > 0) possible = 0;     strcpy(str, best);   }   int main() {     int n;     scanf( %d , &n);     char in[1005][6];     for (int i = 0; i < (n); ++i) scanf( %s , in[i]);     change(in[0],  1000 );     possible = 1;     for (int i = (1); i <= (n - 1); ++i) {       change(in[i], in[i - 1]);     }     if (!possible)       puts( No solution );     else {       for (int i = 0; i < (n); ++i) printf( %s n , in[i]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     cin.tie(0);     cin.sync_with_stdio(0);     int n;     cin >> n;     vector<vector<int> > p(n);     for (int i = 1; i < n; i++) {       int tmp;       cin >> tmp;       p[tmp - 1].push_back(i);     }     int ans = 0;     queue<pair<int, int> > q;     q.push(pair<int, int>(0, 0));     vector<int> tmp(n, 0);     while (q.size()) {       pair<int, int> front = q.front();       int cur = front.first;       int level = front.second;       q.pop();       tmp[level] = !tmp[level];       for (auto it : p[cur]) q.push(pair<int, int>(it, level + 1));     }     for (int i = 0; i < n; i++) ans += tmp[i];     cout << ans << endl;     return 0;   }
#include<bits/stdc++.h>   using namespace std;   int a, b;   bool check(int cnt) {    int cnt6 = cnt/6;    int cnt2 = cnt/2 - cnt6;    int cnt3 = cnt/3 - cnt6;    if(cnt2 <= a) {     cnt6 -= a-cnt2;    }    if(cnt3 <= b) {     cnt6 -= b-cnt3;    }    return cnt6 >= 0;   }   int main() {   #ifdef DEBUG       a = 0, b = 1000000;   #elif not defined(DEBUG)    cin>>a>>b;   #endif    int l = max(a<<1, b*3);    int r = l<<1;    while(l+100<r) {     int mid = (l+r)>>1;     if(check(mid)) r = mid; else l = mid;    }    for(int i = l;i<=r;i++) if(check(i)) {     cout<<i<<endl;     break;    }    return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long N = 1e6 + 5;   const long long MOD = 1e9 + 7;   long long n, ct;   long long a[N], vis[N];   vector<long long> g[N];   void dfs(long long k) {     if (vis[k]) return;     vis[k] = 1;     ct++;     for (auto it : g[k]) dfs(it);   }   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     cin >> n;     for (long long i = 1; i <= n; i++) {       cin >> a[i];       g[i].push_back(a[i]);     }     vector<long long> ans;     for (long long i = 1; i <= n; i++) {       if (!vis[i]) {         ct = 0;         dfs(i);         ans.push_back(ct);       }     }     sort(ans.rbegin(), ans.rend());     long long answer = 0;     long long sz = ans.size();     for (long long i = 0; i < sz; i++) {       answer += ans[i] * ans[i];     }     if (sz >= 2) {       answer -= ans[0] * ans[0];       answer -= ans[1] * ans[1];       answer += (ans[0] + ans[1]) * (ans[0] + ans[1]);     }     cout << answer;   }