malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie();     string s, mx;     cin >> s;     int len = s.length();     for (int i = 1; i < 1 << len; i++) {       string foo;       for (int j = 0; j < len; j++)         if (i & (1 << j)) foo += s[j];       bool flag = 1;       for (int k = 0; k <= foo.length() / 2; k++)         if (foo[k] != foo[foo.length() - k - 1]) flag = 0;       if (flag && foo > mx) mx = foo;     }     cout << mx;   }
#include <bits/stdc++.h>   using namespace std;   const int size = 300000001;   bitset<size> gut;   int main() {     int l, r;     cin >> l >> r;     gut.set();     for (int i = 3; i * i <= r; i += 2) {       if (gut[i])         for (int j = i * i; j <= r; j += (i << 1)) {           gut[j] = false;         }     }     int rez = 0;     rez = (l <= 2 && r >= 2);     for (int i = 5; i <= r; i += 4) {       if (i >= l && gut[i]) rez++;     }     cout << rez << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 3010;   struct edge {     int v, next, flag;   } e[N << 2];   int head[N], cnt;   void addedge(int u, int v, int flag) {     e[cnt].v = v;     e[cnt].flag = flag;     e[cnt].next = head[u];     head[u] = cnt++;   }   int sum[N], dp[N];   void dfs(int u, int pre) {     sum[u] = 0;     for (int i = head[u]; i != -1; i = e[i].next) {       if (e[i].v == pre) continue;       dfs(e[i].v, u);       int v = e[i].v;       sum[u] += sum[v] + e[i].flag;     }     dp[u] = sum[u];     for (int i = head[u]; i != -1; i = e[i].next) {       if (e[i].v == pre) continue;       int v = e[i].v;       dp[u] = min(dp[u], sum[u] - sum[v] + 1 - e[i].flag + dp[v] - e[i].flag);     }   }   int main() {     memset(head, -1, sizeof(head));     int n;     scanf( %d , &n);     for (int i = 1; i < n; i++) {       int x, y, z;       scanf( %d%d , &x, &y);       addedge(x, y, 0);       addedge(y, x, 1);     }     int ans = 0x7fffffff;     for (int i = 1; i <= n; i++) {       dfs(i, -1);       for (int j = 1; j <= n; j++) {         ans = min(ans, sum[i] - sum[j] + dp[j]);       }     }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e3 + 100;   int N;   int p[MAXN], rp[MAXN];   char ans[MAXN][MAXN];   bool v[MAXN];   int lvl, C;   bool pd;   bool id(int* a, int as) {     for (int i = 1; i <= as; i++)       if (a[i] != i) return false;     return true;   }   char get_dir(bool d) { return d ?  /  :     ; }   char get_dir(int a, int b) {     assert(a != b);     return get_dir(a < b);   }   bool addE(int a, int b) {     assert(a != b);     if (a < b) {       ans[lvl][a] = ans[lvl][b] =  / ;       return true;     }     if (a > b) {       ans[lvl][a] = ans[lvl][b] =     ;       return false;     }     assert(false);     return false;   }   void init(int n) {     assert(p[n] != n);     v[n] = true;     C = n;     for (int i = p[n]; i != n; i = p[i]) {       v[i] = true;       pd = addE(p[i], i);       lvl--;     }   }   int addFE(int n, int lf) {     if ((pd) xor (n > C)) {       ans[lvl][n] = get_dir(p[n], C);       ans[lf][C] = get_dir(C, n);       pd = C < n;       ans[lf][n] = get_dir(C, n);       return -1;     }     if ((p[n] < n) xor (n < C)) {       ans[lvl - 1][C] = get_dir(p[n], C);       ans[lf][C] = get_dir(C, n);       ans[lf][n] = get_dir(C, n);       return -1;     }     assert(false);     return -1;   }   void fill(int n) {     v[n] = true;     int l = 1, s = -1;     for (int i = p[n]; i != n; i = p[i]) {       v[i] = true, l++;       if (((p[n] < n) xor (C < n)) or (pd xor (n > C))) s = i;     }     lvl += addFE(s, lvl - l);     for (int i = p[s]; i != s; i = p[i]) {       addE(p[i], i), lvl--;     }   }   int main() {     scanf( %d , &N);     lvl = N;     pd = false;     for (int i = 0; i <= N; i++) v[i] = false, p[i] = rp[i] = -1;     for (int i = 1; i <= N; i++) scanf( %d , rp + i);     for (int i = 1; i <= N; i++) p[rp[i]] = i;     for (int i = 1; i <= N; i++)       for (int j = 1; j <= N; j++) ans[i][j] =  . ;     if (id(p, N)) {       printf( %d n , N);       for (int i = 1; i <= N; i++, printf(  n ))         for (int j = 1; j <= N; j++) printf( %c , ans[i][j]);       return 0;     }     for (int i = 1; i <= N; i++)       if (p[i] == i) v[i] = true;     for (int i = 1, c = 1; c and i <= N; i++)       if (!v[i]) init(i), c = 0;     lvl++;     for (int i = 1; i <= N; i++)       if (!v[i]) fill(i);     printf( %d n , N - 1);     for (int i = 1; i <= N; i++, printf(  n ))       for (int j = 1; j <= N; j++) printf( %c , ans[i][j]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int t[35], mp[400][400], n;   void dfs(int step, int x, int y, int dir) {     if (step > n) return;     if (dir == 0) {       dfs(step + 1, x, y + t[step], 1 - dir);       for (int i = 0; i < 400; i++)         for (int j = 0; j < 400; j++)           if (mp[i][j]) {             mp[i][j] = mp[x * 2 - i][j] = 1;           }       for (int i = 1; i <= t[step]; i++) mp[x][y + i] = 1;     } else {       dfs(step + 1, x + t[step], y + t[step], 1 - dir);       int b = y - x;       for (int i = 0; i < 400; i++)         for (int j = 0; j < 400; j++)           if (mp[i][j]) {             mp[i][j] = mp[j - b][i + b] = 1;           }       for (int i = 1; i <= t[step]; i++) {         mp[x + i][y + i] = 1;       }     }   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , t + i);     memset(mp, 0, sizeof(mp));     dfs(1, 200, 199, 0);     int ans = 0;     for (int i = 0; i < 400; i++)       for (int j = 0; j < 400; j++)         if (mp[i][j]) ans++;     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2000000;   char a[MAXN + 10], b[MAXN + 10];   int main(void) {     int n;     scanf( %d%s%s , &n, a, b);     int cnt[2][2];     for (int i = 0; i < (2); ++i) {       for (int j = 0; j < (2); ++j) {         cnt[i][j] = 0;       }     }     n *= 2;     for (int i = 0; i < (n); ++i) {       ++cnt[a[i] -  0 ][b[i] -  0 ];     }     int sA = 0, sB = 0;     for (int i = 0; i < (n); ++i) {       if (cnt[1][1] > 0) {         --cnt[1][1];         ++sA;       } else if (cnt[1][0] > 0) {         --cnt[1][0];         ++sA;       } else if (cnt[0][1] > 0) {         --cnt[0][1];       } else {         --cnt[0][0];       }       if (cnt[1][1] > 0) {         --cnt[1][1];         ++sB;       } else if (cnt[0][1] > 0) {         --cnt[0][1];         ++sB;       } else if (cnt[1][0] > 0) {         --cnt[1][0];       } else {         --cnt[0][0];       }     }     if (sA > sB) {       puts( First );     } else if (sA < sB) {       puts( Second );     } else {       puts( Draw );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, X, Y, Z, p;     int x, y;     while (scanf( %d%d%d%d%d%d , &n, &m, &X, &Y, &Z, &p) != EOF) {       X %= 4;       Y %= 2;       Z %= 4;       int tn = n;       int tm = m;       for (int i = 1; i <= p; i++) {         n = tn;         m = tm;         scanf( %d%d , &x, &y);         if (X == 1) {           int xx = x;           x = y;           y = n + 1 - xx;           int nn = n;           n = m;           m = nn;         } else if (X == 2) {           x = n + 1 - x;           y = m + 1 - y;         } else if (X == 3) {           int xx = x;           x = m + 1 - y;           y = xx;           int nn = n;           n = m;           m = nn;         }         if (Y == 1) {           y = m + 1 - y;         }         if (Z == 1) {           int xx = x;           x = m + 1 - y;           y = xx;           int nn = n;           n = m;           m = nn;         } else if (Z == 2) {           x = n + 1 - x;           y = m + 1 - y;         } else if (Z == 3) {           int xx = x;           x = y;           y = n + 1 - xx;           int nn = n;           n = m;           m = nn;         }         printf( %d %d n , x, y);       }     }     return 0;   }
#include <bits/stdc++.h>   #pragma GCC optimize(2)   using namespace std;   const int INF = 1e9 + 7;   const int N = 4e5 + 7;   const int M = 5e6 + 7;   int vis[M];   int p[N];   int qt[N];   signed main() {     ios::sync_with_stdio(false);     int q, x, y;     cin >> q >> x;     set<pair<int, int>> vals;     for (int i = 0; i < x; i++) vals.insert({qt[i], i});     while (q--) {       int y;       cin >> y;       y %= x;       vals.erase({qt[y], y});       vals.insert({++qt[y], y});       auto k = *vals.begin();       int nVal = k.first * x + k.second;       cout << nVal << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, x, l[60], r[60], ans;   int main() {     scanf( %d %d , &n, &x);     for (int i = 1; i <= n; ++i) {       scanf( %d %d , &l[i], &r[i]);     }     for (int i = 1; i <= n; ++i) {       ans += (l[i] - r[i - 1] - 1) % x;       ans += r[i] - l[i] + 1;     }     printf( %d , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2000 + 7, Mod = 1000000000 + 7;   inline int Inc(int A, int B) {     A += B;     return A >= Mod ? A - Mod : A;   }   inline int Dec(int A, int B) {     A -= B;     return A < 0 ? A + Mod : A;   }   int n, m, A, B, Dpd[N][N], Dpr[N][N], Sumd[N][N], Sumr[N][N], d[N][N], r[N][N];   char Map[N][N];   int main() {     scanf( %d%d , &n, &m);     for (int i = (1), iLimit = (n); i <= iLimit; (++(i)))       for (int j = (1), jLimit = (m); j <= jLimit; (++(j))) cin >> Map[i][j];     if (n == 1 && m == 1) {       if (Map[1][1] ==  R )         puts( 0 );       else         puts( 1 );       return 0;     }     for (int i = (n), iLimit = (1); i >= iLimit; (--(i)))       for (int j = (m), jLimit = (1); j >= jLimit; (--(j))) {         d[i][j] = d[i + 1][j] + (Map[i + 1][j] ==  R );         r[i][j] = r[i][j + 1] + (Map[i][j + 1] ==  R );       }     Dpd[n][m] = Dpr[n][m] = Sumd[n][m] = Sumr[n][m] = (Map[n][m] !=  R );     for (int i = (n), iLimit = (1); i >= iLimit; (--(i)))       for (int j = (m), jLimit = (1); j >= jLimit; (--(j)))         if (i != n || j != m) {           Dpr[i][j] = Dec(Sumd[i][j + 1], Sumd[i][m - r[i][j] + 1]);           Dpd[i][j] = Dec(Sumr[i + 1][j], Sumr[n - d[i][j] + 1][j]);           Sumr[i][j] = Inc(Sumr[i + 1][j], Dpr[i][j]);           Sumd[i][j] = Inc(Sumd[i][j + 1], Dpd[i][j]);         }     printf( %d , Inc(Dpr[1][1], Dpd[1][1]));     return 0;   }
#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:256000000 )   const long long INF = 1e9;   const long long mod = 1e9 + 7;   const long long LINF = 1e14;   using namespace std;   long long n, l;   vector<long long> c;   vector<int> b;   int main() {     std::ios::sync_with_stdio(false);     cin >> n >> l;     c.resize(n);     cin >> c[0];     for (long long i = (1); i < (n); i++) {       cin >> c[i];       c[i] = min(c[i - 1] * 2, c[i]);     }     while (l > 0) b.push_back(l % 2), l /= 2;     while (c.size() < b.size()) c.push_back(c.back() * 2);     while (b.size() < c.size()) b.push_back(0);     c.push_back(c.back() * 2);     n = b.size();     vector<long long> d(n + 1, LINF);     d[0] = 0;     for (long long i = (1); i < (n + 1); i++) {       d[i] = min(d[i - 1], c[i - 1]);       if (b[i - 1] == 1) d[i] += c[i - 1];     }     cout << min(d[n], c.back()) << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int x;     cin >> x;     int k = x % 4;     if (k == 1) {       cout << 0 <<   A  << endl;     } else if (k == 0) {       cout << 1 <<   A  << endl;     } else if (k == 2) {       cout << 1 <<   B  << endl;     } else {       cout << 2 <<   A  << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   char arr[50][50];   int main() {     int n, m;     cin >> n >> m;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) {         if ((i % 2 == 0 ||              ((i % 4 == 1 && j == m - 1) || (i % 4 == 3 && j == 0))))           arr[i][j] =  # ;         else           arr[i][j] =  . ;       }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) cout << arr[i][j];       cout <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   template <typename A, typename B>   inline void chmin(A &a, B b) {     if (a > b) a = b;   }   template <typename A, typename B>   inline void chmax(A &a, B b) {     if (a < b) a = b;   }   struct UnionFindTree {     vector<long long> par, sz;     UnionFindTree(long long n) {       par.resize(n);       sz.resize(n);       for (long long i = 0; i < n; i++) {         par[i] = i;         sz[i] = 1;       }     }     long long find(long long x) {       return x == par[x] ? x : par[x] = find(par[x]);     }     void unite(long long x, long long y) {       x = find(x);       y = find(y);       if (x == y) return;       if (sz[x] < sz[y]) swap(x, y);       sz[x] += sz[y];       par[y] = x;     }     bool areSame(long long x, long long y) { return find(x) == find(y); }     long long size(long long x) { return sz[find(x)]; }   };   const long long SIZE = 1000000;   long long N, M;   long long A[SIZE], B[SIZE];   long long cnt[SIZE];   signed main() {     scanf( %lld%lld , &N, &M);     for (long long i = 0; i < (M); i++) scanf( %lld%lld , &A[i], &B[i]);     vector<long long> vs;     for (long long i = 0; i < (M); i++) {       vs.push_back(A[i]);       vs.push_back(B[i]);     }     sort((vs).begin(), (vs).end());     vs.erase(unique((vs).begin(), (vs).end()), vs.end());     for (long long i = 0; i < (M); i++) {       A[i] = lower_bound((vs).begin(), (vs).end(), A[i]) - vs.begin();       B[i] = lower_bound((vs).begin(), (vs).end(), B[i]) - vs.begin();     }     N = vs.size();     UnionFindTree uf(N);     for (long long i = 0; i < (M); i++) uf.unite(A[i], B[i]);     if (uf.size(0) != N) {       puts( 0 );       return 0;     }     long long luz = 0;     for (long long i = 0; i < (M); i++) {       if (A[i] == B[i])         luz++;       else {         cnt[A[i]]++;         cnt[B[i]]++;       }     }     long long ans = 0;     ans += luz * (M - luz);     ans += luz * (luz - 1) / 2;     for (long long i = 0; i < N; i++) ans += cnt[i] * (cnt[i] - 1) / 2;     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int N = 100000, M = 100000;   long long int MOD = 1000000007;   void solve() {     long long int n;     cin >> n;     long long int a[n + 1], b[n + 1];     for (long long int i = 0; i < n; i++) cin >> a[i];     for (long long int i = 0; i < n; i++) {       b[i] = a[i] - i;       if (b[i] < 0)         b[i] = 0;       else if (b[i] % n == 0)         b[i] = b[i] / n;       else         b[i] = (b[i] / n) + 1;     }     for (long long int i = 0; i < n; i++) b[i] = i + (n * b[i]);     long long int min1 = b[0];     long long int ans = 0;     for (long long int i = 0; i < n; i++) {       if (min1 > b[i]) {         min1 = b[i];         ans = i;       }     }     cout << ans + 1;   }   int main() {     long long int t = 1;     while (t--) {       solve();     }   }
#include <bits/stdc++.h>   using namespace std;   int k, m;   set<int> S;   int pw[10];   void rec(int ind, int number, int value) {     if (ind == 4) {       if (value > k && value - k < 10000) {         S.insert(number * 10000 + (value - k));       }       if (k >= value && k - value < 10000) {         S.insert(number * 10000 + (k - value));       }       return;     }     for (int i = 1; i + ind < 5; i++) {       for (int j = 0; j < pw[i]; j++) {         if (ind == 0) {           rec(ind + i, j, j);         } else {           rec(ind + i, number * pw[i] + j, value + j);           rec(ind + i, number * pw[i] + j, value - j);           rec(ind + i, number * pw[i] + j, value * j);         }       }     }   }   int main() {     scanf( %d , &k);     scanf( %d , &m);     pw[0] = 1;     for (int i = 1; i <= 6; i++) pw[i] = pw[i - 1] * 10;     rec(0, 0, 0);     for (set<int>::iterator it = S.begin(); (it != S.end()) && m; it++, m--) {       cout << setw(8) << setfill( 0 ) << (*it) << endl;     }     return 0;   }
#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:102400000,102400000 )   using namespace std;   int main() {     int cases;     scanf( %d , &cases);     while (cases--) {       char str[1009];       int ret[9];       scanf( %s , str);       int num = 0;       int i;       int cnt = 0;       int flag = -1;       for (i = 0; str[i]; i++) {         if (str[i] >=  A  && str[i] <=  Z ) {           str[i] = str[i] -  A  +  a ;         }         if (str[i] ==  : ) {           if (str[i + 1] ==  : ) {             flag = cnt;             i++;           }           ret[cnt] = num;           cnt++;           num = 0;         } else if (str[i] >=  0  && str[i] <=  9 ) {           num = num * 16 + str[i] -  0 ;         } else {           num = num * 16 + str[i] -  a  + 10;         }       }       ret[cnt++] = num;       int k = 0;       for (int j = 0; j < cnt; j++) {         printf( %04x , ret[j]);         k++;         if (k != 8) {           putchar( : );         }         if (flag == j) {           for (i = 0; i < 8 - cnt; i++) {             printf( 0000 );             k++;             if (k != 8) {               putchar( : );             }           }         }       }       putchar(10);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, i, j, r, mn, mx, x, cnt = 0, a = 1, b = 1;     string s;     cin >> n;     for (i = 0; i < n; i++) {       cin >> s;       if (s ==  UR  || s ==  DL )         a++;       else if (s ==  UL  || s ==  DR )         b++;       else {         a++, b++;       }     }     cout << a * b << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 2e9;   int n;   pair<int, int> p[25];   int dis[25][25];   int dp[1 << 24 | 1];   int pre[1 << 24 | 1];   int main() {     scanf( %d%d%d , &p[0].first, &p[0].second, &n);     for (int i = 1; i <= n; ++i) {       scanf( %d%d , &p[i].first, &p[i].second);     }     for (int i = 0; i <= n; ++i)       for (int j = i; j <= n; ++j) {         dis[i][j] = dis[j][i] =             pow(p[i].first - p[j].first, 2) + pow(p[i].second - p[j].second, 2);       }     for (int m = 1; m < (1 << n); ++m) {       dp[m] = INF;     }     dp[0] = 0;     for (int m = 0; m < (1 << n); ++m) {       if (dp[m] == INF) continue;       for (int i = 1; i <= n; ++i) {         if (m & 1 << (i - 1)) continue;         for (int j = 1; j <= n; ++j) {           if (m & 1 << (j - 1)) continue;           if (dp[m | (1 << i - 1) | (1 << j - 1)] >               dp[m] + dis[0][i] + dis[i][j] + dis[j][0]) {             dp[m | (1 << i - 1) | (1 << j - 1)] =                 dp[m] + dis[0][i] + dis[i][j] + dis[j][0];             pre[m | (1 << i - 1) | (1 << j - 1)] = m;           }         }         break;       }     }     printf( %d n , dp[(1 << n) - 1]);     int num = (1 << n) - 1;     while (num) {       printf( 0  );       int t = pre[num] ^ num;       for (int i = 0; i < n; ++i) {         if (t & (1 << i)) {           printf( %d  , i + 1);         }       }       num = pre[num];     }     printf( 0 n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long MAXN = 1000001;   long long n, l[MAXN], r[MAXN], id[MAXN], K;   bool cmp(long long i, long long j) {     return (l[i] / K == l[j] / K)                ? ((l[i] / K & 1) ? (r[i] < r[j]) : (r[i] > r[j]))                : (l[i] < l[j]);   }   int main() {     scanf( %lld , &n);     K = 1000;     for (long long i = 1; i <= n; i++) scanf( %lld%lld , &l[i], &r[i]), id[i] = i;     sort(id + 1, id + 1 + n, cmp);     for (int i = 1; i <= n; i++) printf( %lld  , id[i]);   }
#include <bits/stdc++.h>   using namespace std;   const long long neutral = 0;   class SegmentTree {     vector<long long> a;     int n;       public:     SegmentTree(long long* st, long long* en) {       int sz = int(en - st);       for (n = 1; n < sz; n <<= 1)         ;       a.assign(n << 1, neutral);       for (int i = 0; i < sz; i++) a[i + n] = st[i];       for (int i = n + sz - 1; i > 1; i--) a[i >> 1] = ((a[i >> 1]) | (a[i]));     }     void update(int i, long long x) {       a[i += n] = x;       for (i >>= 1; i; i >>= 1) a[i] = ((a[i << 1]) | (a[1 + (i << 1)]));     }     long long query(int l, int r) {       long long ans = neutral;       for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {         if (l & 1) ans = ((ans) | (a[l++]));         if (r & 1) ans = ((ans) | (a[--r]));       }       return ans;     }   };   int main() {     cin.sync_with_stdio(0);     cin.tie(0);     cin.exceptions(cin.failbit);     int n, q, m;     string s;     cin >> s;     n = (int)(s).size();     long long v[n];     for (int i = 0; i < (n); ++i) {       v[i] = 1LL << (s[i] -  a );     }     cin >> q;     SegmentTree st(v, v + n);     while (q--) {       int t;       cin >> t;       if (t == 2) {         int a, b;         cin >> a >> b;         long long res = st.query(a - 1, b - 1), c = 0;         while (res > 0) c++, res -= (res & -res);         cout << c << endl;       } else {         int a;         char c;         cin >> a >> c;         int b = c -  a ;         st.update(a - 1, 1LL << b);       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int num[400005];   int tot = 0;   set<int> st;   int mp[20];   int freq[20];   long long push[20][20];   long long addp[20][1048576];   vector<int> pos[20];   long long sum[20];   int cnt[20];   long long memo[1048576];   long long cost(int mask, int ofst, int id) {     return sum[id] + addp[id][mask] -            1LL * (ofst + 1 + ofst + cnt[id]) * cnt[id] / 2;     ;   }   long long dp(int i, int mask, int ofst) {     if (i == tot) return 0;     if (memo[mask] != -1) return memo[mask];     long long mn = 1000000000000000000;     int id;     for (int j = (0); j <= (tot - 1); j++) {       if ((mask >> j) & 1) continue;       long long v =           dp(i + 1, mask | (1 << j), ofst + cnt[j]) + cost(mask, ofst, j);       if (v < mn) {         mn = v;         id = j;       }     }     return memo[mask] = mn;   }   int main() {     int n;     scanf( %d , &n);     for (int i = (1); i <= (n); i++) {       scanf( %d , &num[i]);       --num[i];       st.insert(num[i]);     }     for (auto u : st) {       mp[u] = tot++;     }     for (int i = (0); i <= (tot - 1); i++) pos[i].push_back(0);     for (int i = (1); i <= (n); i++) {       num[i] = mp[num[i]];       int t = num[i];       pos[t].push_back(i);       cnt[t]++;       sum[t] += i;     }     memset(push, 0, sizeof(push));     for (int i = (1); i <= (n); i++) {       freq[num[i]]++;       for (int j = (0); j <= (tot - 1); j++) {         push[num[i]][j] += cnt[j] - freq[j];       }     }     memset(addp, 0, sizeof(addp));     for (int i = (0); i <= (tot - 1); i++) {       for (int j = (0); j <= ((1 << tot) - 1); j++) {         if ((j >> i) & 1) continue;         for (int k = (0); k <= (tot - 1); k++) {           if (~(j >> k) & 1) continue;           addp[i][j] += push[i][k];         }       }     }     memset(memo, -1, sizeof(memo));     printf( %lld n , dp(0, 0, 0));     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<int> adj[200010];   long long int ans = 0LL;   int odd = 0, sz[200010] = {};   void dfs(int cur, int par = -1, int ht = 0) {     sz[cur]++;     for (int u : adj[cur]) {       if (u == par) continue;       dfs(u, cur, ht ^ 1);       sz[cur] += sz[u];     }     if (ht) odd++;   }   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n - 1; i++) {       int x, y;       scanf( %d %d , &x, &y);       adj[x].push_back(y);       adj[y].push_back(x);     }     dfs(1);     for (int i = 1; i <= n; i++) ans += (1LL) * sz[i] * (n - sz[i]);     ans += (1LL) * odd * (n - odd);     printf( %lld n , ans / 2);     return 0;   }