malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   long long int pi[1000], ai[1000];   long long int bsearch(long long int arr[], long long int x, long long int n) {     long long int a = 0;     long long int k;     long long int b = n - 1;     while (a <= b) {       k = (a + b) / 2;       if (arr[k] == x) {       }       if (arr[k] > x)         b = k - 1;       else         a = k + 1;     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long int i, j, k = 0, m = 0, n, f = 0, r1 = 0, r2 = 0, r3 = 0, r4 = 0,                         f1 = 0, f2 = 0, c1 = 0, c2 = 0, c3 = 0, c4 = 0, k1 = 0,                         k2 = 0, j1, j2;     cin >> n;     for (i = 1; i <= n; i++) {       if (n % i == 0) {         f1 = n / i;         j1 = (long long int)(log2(i * 1.0));         j2 = (long long int)(log2(f1 * 1.0));         for (; j1 >= 0; j1--) {           if (i & (1 << j1) && r2 == 0) {             c1++;             if (j1 == 0 && c2 == c1 - 1) {               k1 = max(k1, i);             }           } else if (!(i & (1 << j1))) {             c2++;             r2 = 1;             if (j1 == 0 && c2 == c1 - 1) {               k1 = max(k1, i);             }           } else             break;         }         for (; j2 >= 0; j2--) {           if (f1 & (1 << j2) && r4 == 0) {             c3++;             if (j2 == 0 && c3 - 1 == c4) {               k2 = max(k2, f1);             }           } else if (!(f1 & (1 << j2))) {             r4 = 1;             c4++;             if (j2 == 0 && c3 - 1 == c4) {               k2 = max(k2, f1);             }           } else             break;         }         r3 = 0;         r1 = 0;         r2 = 0;         r4 = 0;         c1 = 0;         c2 = 0;         c3 = 0;         c4 = 0;       }     }     cout << max(k1, k2);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1200 + 10;   char s[maxn];   int a[maxn];   int main() {     while (~scanf( %s , s)) {       memset(a, 0, sizeof(a));       int len = strlen(s);       int cnt = 0;       int m;       scanf( %d , &m);       if (len % m == 0) {         int cur = len / m;         int sign = 1;         for (int i = 0; i < len; i += cur) {           for (int j = i, k = i + cur - 1; j < k; j++, k--) {             if (s[j] != s[k]) {               sign = 0;               break;             }           }           if (sign == 0) break;         }         if (sign) cnt = 1;       }       if (cnt)         printf( YES n );       else         printf( NO n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int i, m;   string a, s;   int main() {     cin >> a >> s;     m = s.size();     sort(s.begin(), s.end());     for (m--; i < a.size(); i++) {       if (a[i] < s[m]) a[i] = s[m--];     }     cout << a;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     string one, o, two;     vector<int> a(2, 0);     cin >> n;     cin >> one;     a[0] += 1;     while (n-- > 1) {       cin >> o;       if (o == one)         a[0] += 1;       else {         a[1] += 1;         two = o;       }     }     if (a[0] > a[1])       cout << one;     else       cout << two;     return 0;   }
#include <bits/stdc++.h>   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx )   const long double eps = 1e-12;   const long double Pi = acos(-1);   const long long md = 1e9 + 7;   using namespace std;   long long n, m, p[10100], f[10100], x, y;   pair<long long, long long> d[10100];   bool used[10100];   bool cmp(pair<long long, long long> a, pair<long long, long long> b) {     if (f[b.second] && f[a.second]) {       return a.first > b.first;     }     if (!f[b.second] && !f[a.second]) {       return a.first > b.first;     }     return f[a.second];   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     cin >> n >> m;     for (long long i = 1; i <= 2 * n; i++) {       cin >> p[i];       d[i].first = p[i];       d[i].second = i;     }     for (long long i = 0; i < m; i++) {       cin >> x >> y;       f[x] = y;       f[y] = x;       d[x].first = p[x] - p[y];       d[y].first = p[y] - p[x];     }     sort(d + 1, d + 2 * n + 1, cmp);     long long t;     cin >> t;     long long cnt = 2 * n;     long long it = 1;     while (cnt) {       if (t == 2) {         cnt--;         cin >> x;         used[x] = true;         if (f[x]) {           cout << f[x] <<   n ;           used[f[x]] = true;           cnt--;           cout << flush;           ;         } else {           t = 3 - t;         }       } else {         for (; it <= 2 * n; it++) {           if (used[d[it].second]) continue;           cout << d[it].second <<   n ;           used[d[it].second] = true;           cout << flush;           ;           if (f[d[it].second]) {             cin >> x;             used[x] = true;             cnt--;             t = 3 - t;           }           break;         }         cnt--;         t = 3 - t;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 50, M1 = 998244353, M2 = 1e9 + 7;   struct ModInt {     int x1, x2;     ModInt(int a = 0, int b = 0) : x1(a), x2(b) {}   };   ModInt operator+(ModInt a, ModInt b) {     return ModInt((a.x1 + b.x1) % M1, (a.x2 + b.x2) % M2);   }   ModInt operator-(ModInt a, ModInt b) {     return ModInt((a.x1 - b.x1 + M1) % M1, (a.x2 - b.x2 + M2) % M2);   }   ModInt operator*(ModInt a, ModInt b) {     return ModInt(1ll * a.x1 * b.x1 % M1, 1ll * a.x2 * b.x2 % M2);   }   bool operator==(ModInt a, ModInt b) { return a.x1 == b.x1 && a.x2 == b.x2; }   int n, m, sz[N], B[2];   char s[N];   ModInt h[2][N], pw[N];   ModInt f(int l, int r) {     return h[l & 1][sz[r]] - h[l & 1][sz[l - 1]] * pw[sz[r] - sz[l - 1]];   }   int main(int argc, char *argv[]) {     scanf( %d%s , &n, s + 1);     pw[0] = ModInt(1, 1), B[0] = 213, B[1] = 233;     for (int i = 1; i <= (n); ++i) {       pw[i] = pw[i - 1] * ModInt(10007, 10007);       if (s[i] ==  0 ) {         ++m;         h[0][m] = h[0][m - 1] * pw[1] + ModInt(B[i & 1], B[i & 1]);         h[1][m] = h[1][m - 1] * pw[1] + ModInt(B[(i & 1) ^ 1], B[(i & 1) ^ 1]);       }       sz[i] = m;     }     int q;     scanf( %d , &q);     while (q--) {       int l1, l2, len;       scanf( %d%d%d , &l1, &l2, &len);       puts(f(l1, l1 + len - 1) == f(l2, l2 + len - 1) ?  Yes  :  No );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, k;     cin >> n >> k;     k--;     vector<int> a(n);     for (int i = 0; i < n; i++) cin >> a[i];     int res = 0;     if (a[k] != 0) {       res++;       a[k] = 0;     }     for (int i = 0; i < n; i++) {       if (i == k) continue;       if (a[i] == 0) a[i] = n;     }     sort(a.begin(), a.end());     int gap = 0;     int ind = 0;     int prev = 0;     int te = n;     for (int i = 0; i < n; i++) {       while (ind < n && a[ind] <= i) {         ind++;         if (ind == n || a[ind] > i) break;         gap += max(0, a[ind] - a[ind - 1] - 1);       }       ind--;       int wrong = n - ind - 1;       te = min(te, max(wrong, gap));     }     if (n > 1) res += te;     cout << res <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   inline T checkmin(T &a, T b) {     return (a < b) ? a : a = b;   }   template <class T>   inline T checkmax(T &a, T b) {     return (a > b) ? a : a = b;   }   template <class T>   T GCD(T a, T b) {     if (a < 0) return GCD(-a, b);     if (b < 0) return GCD(a, -b);     return (a == 0) ? b : GCD(b % a, a);   }   template <class T>   T LCM(T a, T b) {     if (a < 0) return LCM(-a, b);     if (b < 0) return LCM(a, -b);     return (a == 0 || b == 0) ? 0 : a / GCD(a, b) * b;   }   template <class T>   inline T sqr(T X) {     return X * X;   }   namespace Poor {   const int MaxiN = 2005;   const int MaxiM = 2005;   const int MaxiS = 2005;   int N, M;   int Id[MaxiM];   pair<int, int> Opt[MaxiS][MaxiS];   string Base[MaxiN];   string St;   vector<int> List[MaxiN][26];   void Run() {     cin >> N;     for (int i = 0; i < N; ++i) cin >> Base[i];     cin >> M;     for (int i = 0; i < M; ++i) {       cin >> Id[i];       --Id[i];     }     cin >> St;     for (int i = 0; i < N; ++i)       for (int j = 0; j < (int)Base[i].size(); ++j)         List[i][Base[i][j] -  a ].push_back(j);     int Len = St.size();     for (int i = 0; i < Len + 1; ++i)       for (int j = 0; j < Len + 1; ++j) Opt[i][j] = make_pair(M, 0);     Opt[0][0] = make_pair(0, 0);     for (int i = 1; i <= Len; ++i) {       int t = St[i - 1] -  a ;       Opt[i][0] = make_pair(0, 0);       for (int j = 1; j <= i; ++j) {         Opt[i][j] = Opt[i - 1][j];         pair<int, int> Tmp = Opt[i - 1][j - 1];         while (Tmp < Opt[i][j]) {           vector<int>::iterator it =               lower_bound(List[Id[Tmp.first]][t].begin(),                           List[Id[Tmp.first]][t].end(), Tmp.second);           if (it != List[Id[Tmp.first]][t].end()) {             Tmp.second = *it + 1;             break;           }           ++Tmp.first;           Tmp.second = 0;         }         checkmin(Opt[i][j], Tmp);       }     }     for (int j = Len; j >= 0; --j)       if (Opt[Len][j] < make_pair(M, 0)) {         cout << j << endl;         break;       }   }   }  // namespace Poor   int main() {     ios::sync_with_stdio(false);     Poor::Run();     return 0;   }
// Problem: A. Dungeon   // Contest: Codeforces - Educational Codeforces Round 100 (Rated for Div. 2)   // URL: https://codeforces.com/problemset/problem/1463/A   // Memory Limit: 256 MB   // Time Limit: 2000 ms   //    // Powered by CP Editor (https://cpeditor.org)      #include<bits/stdc++.h>   using namespace std;   #pragma GCC optimize( O2 )   #define M 1000000007   #define ll long long int   #define pb push_back      #define pf push_front   #define all(x) x.begin(), x.end()   #define pob pop_back   #define pof pop_front   #define debug1(x) cout<<#x<<   <<x<<endl;   #define debug2(x,y) cout<<#x<<   <<x<<     <<#y<<   <<y<<endl;   #define debug3(x,y,z) cout<<#x<<   <<x<<     <<#y<<   <<y<<     <<#z<<   <<z<<endl;   #define present(c,x) ((c).find(x) != (c).end())   #define null NULL   #define mp make_pair   #define sz(x) (ll)x.size()   #define fi first   #define se second   #define boost ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);   #define inf 1e18   //#define endl   n    //unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();   //shuffle (foo.begin(), foo.end(), std::default_random_engine(seed));   #define ordered_set tree<ll, null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>   typedef pair<int, int>  pii;   #define pi (3.14159265358979323846264338327950288)   template<typename T>   void printv(const T& t) {       std::copy(t.cbegin(), t.cend(), std::ostream_iterator<typename T::value_type>(std::cout,  ,  ));       cout<<endl;   }   ll modpower(ll a,ll b,ll c)   {    ll res=1;    while(b)    {     if(b&1LL)      res=(res*a)%c;     a=(a*a)%c;     b>>=1;    }    return res;   }   //-------------------------------Template--Above-----------------------------------------------   void solve(){    vector<ll>v(3);    for(ll i=0;i<3;i++)    cin>>v[i];    sort(all(v));    ll sum=v[0]+v[1]+v[2];    if(sum%9!=0){     cout<< NO <<endl;     return;    }    if(v[0]<sum/9){     cout<< NO <<endl;     return;    }    cout<< YES <<endl;       return;   }   int main()   {       boost       ll t;      // t=1;       cin>>t;       while(t--){           solve();       }       return 0;   }
#include <bits/stdc++.h>   using namespace std;   char ar[] =  1689 , s[1000010];   int val[10], ct[10];   int main() {     do {       val[atoi(ar) % 7] = atoi(ar);     } while (next_permutation(ar, ar + 4));     scanf( %s , s);     int len = strlen(s);     for (int i = 0; i < len; ++i) ct[s[i] -  0 ]++;     for (int i = 0; i < 4; ++i) ct[ar[i] -  0 ]--;     int rem = 0, div = 0;     for (int i = 1; i < 10; ++i) {       while (ct[i]--) {         printf( %d , i);         div = rem * 10 + i;         rem = div % 7;       }     }     rem *= 10000;     for (int i = 0; i < 7; ++i)       if (!((val[i] + rem) % 7)) {         printf( %d , val[i]);         while (ct[0]--) printf( 0 );         printf(  n );         return 0;       }     return 0;   }
#include <bits/stdc++.h>   const long long mod = 2147483647LL;   long long read() {     long long r = 0;     char c = getchar();     while (c <  0  || c >  9 ) c = getchar();     while (c >=  0  && c <=  9 ) {       r = 10LL * r + (long long)c -  0 ;       c = getchar();     }     return r;   }   long long fast(long long a, long long b) {     long long r = 1LL;     while (b) {       if (b & 1) r = (r * a) % mod;       b >>= 1;       a = (a * a) % mod;     }     return r;   }   std::string str;   int a, b, c, T;   signed main() {     T = read();     while (T--) {       std::cin >> str;       a = 0;       b = 0;       c = 0;       for (int i = 0; i < str.size(); ++i) {         if (str[i] ==  A ) ++a;         if (str[i] ==  B ) ++b;         if (str[i] ==  C ) ++c;       }       if (b == a + c)         puts( YES );       else         puts( NO );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 500010;   const int INF = 0x3f3f3f3f;   const int iinf = 1 << 30;   const long long linf = 2e18;   const int MOD = 1000000007;   const double eps = 1e-7;   void print(int x) {     cout << x << endl;     exit(0);   }   void PRINT(string x) {     cout << x << endl;     exit(0);   }   void douout(double x) { printf( %lf n , x + 0.0000000001); }   int num[N], bit[N];   int n;   struct node {     int x, y, z;   } a[N];   bool cmp(node a, node b) { return a.y > b.y; }   void lsh() {     sort(num + 1, num + n + 1);     int t = unique(num + 1, num + n + 1) - (num + 1);     for (int i = (1); i <= (n); i++)       a[i].x = t - (lower_bound(num + 1, num + 1 + t, a[i].x) - (num + 1));   }   void add(int x, int y) {     for (; x < N; x += (x & -x)) bit[x] = max(bit[x], y);   }   int ask(int x) {     int ans = 0;     for (; x; x -= (x & -x)) ans = max(ans, bit[x]);     return ans;   }   signed main() {     scanf( %d , &n);     for (int i = (1); i <= (n); i++) scanf( %d , &a[i].x), num[i] = a[i].x;     for (int i = (1); i <= (n); i++) scanf( %d , &a[i].y);     for (int i = (1); i <= (n); i++) scanf( %d , &a[i].z);     lsh();     sort(a + 1, a + n + 1, cmp);     int ans = 0;     for (int i = 1, j; i <= n; i = j) {       for (j = i; j <= n && a[j].y == a[i].y; j++) {         int z = ask(a[j].x - 1);         if (z > a[j].z) ans++;       }       for (; i < j; i++) add(a[i].x, a[i].z);     }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 500;   int n, ret, cnt;   char a[maxn][maxn];   int arr[maxn][maxn];   pair<int, int> par[maxn][maxn];   multiset<pair<int, int> > s;   pair<int, int> root(pair<int, int> c) {     if (par[c.first][c.second].first < 0) return c;     return par[c.first][c.second] = root(par[c.first][c.second]);   }   void merge(pair<int, int> A, pair<int, int> B) {     if ((A = root(A)) == (B = root(B))) return;     if (par[A.first][A.second].first < par[B.first][B.second].first) swap(A, B);     par[B.first][B.second].first += par[A.first][A.second].first;     par[A.first][A.second] = B;   }   void add(pair<int, int> A) {     A = root(A);     if (arr[A.first][A.second] == 0) ret += -par[A.first][A.second].first;     arr[A.first][A.second]++;   }   void Add(pair<int, int> A) {     int i = A.first, j = A.second;     if (a[i][j] ==  X ) ret++;     if (a[i][j] !=  X ) add({i, j});     if (i - 1 >= 0 && a[i - 1][j] !=  X ) add({i - 1, j});     if (i + 1 < n && a[i + 1][j] !=  X ) add({i + 1, j});     if (j - 1 >= 0 && a[i][j - 1] !=  X ) add({i, j - 1});     if (j + 1 < n && a[i][j + 1] !=  X ) add({i, j + 1});   }   void rem(pair<int, int> A) {     A = root(A);     arr[A.first][A.second]--;     if (arr[A.first][A.second] == 0) ret -= -par[A.first][A.second].first;   }   void Rem(pair<int, int> A) {     int i = A.first, j = A.second;     if (a[i][j] ==  X ) ret--;     if (a[i][j] !=  X ) rem({i, j});     if (i - 1 >= 0 && a[i - 1][j] !=  X ) rem({i - 1, j});     if (i + 1 < n && a[i + 1][j] !=  X ) rem({i + 1, j});     if (j - 1 >= 0 && a[i][j - 1] !=  X ) rem({i, j - 1});     if (j + 1 < n && a[i][j + 1] !=  X ) rem({i, j + 1});   }   int main() {     ios_base::sync_with_stdio(false);     int K;     cin >> n >> K;     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++) cin >> a[i][j];     for (int i = 0; i < maxn; i++)       for (int j = 0; j < maxn; j++) par[i][j] = {-1, -1};     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++)         if (a[i][j] ==  . ) {           if (i + 1 < n && a[i + 1][j] ==  . ) merge({i, j}, {i + 1, j});           if (j + 1 < n && a[i][j + 1] ==  . ) merge({i, j}, {i, j + 1});         }     int ans = 0;     for (int i = K - 1; i < n; i++) {       memset(arr, 0, sizeof arr);       ret = 0;       for (int j = i - K + 1; j <= i; j++)         for (int k = 0; k < K; k++) {           Add({j, k});         }       ans = max(ans, ret);       for (int j = K; j < n; j++) {         for (int k = i - K + 1; k <= i; k++) Add({k, j});         for (int k = i - K + 1; k <= i; k++) Rem({k, j - K});         ans = max(ans, ret);       }     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int T;     cin >> T;     while (T--) {       int n, k;       cin >> n >> k;       vector<int> a(n);       for (int i = 0; i < n; ++i) {         cin >> a[i];       }       if (n == 1) {         if (a[0] == k) {           cout <<  yes n ;         } else {           cout <<  no n ;         }         continue;       }       bool flag = false;       bool k_flag = false;       for (int i = 0; i < n; ++i) {         if (a[i] == k) {           k_flag = true;           break;         }       }       if (!k_flag) {         cout <<  no n ;         continue;       }       for (int i = 0; i < n; ++i) {         if (a[i] != k) {           continue;         }         if (i - 1 >= 0 && a[i - 1] >= k || i + 1 < n && a[i + 1] >= k) {           flag = true;           break;         }         if (i - 2 >= 0 && a[i - 2] >= k || i + 2 < n && a[i + 2] >= k) {           flag = true;           break;         }         if (flag) {           break;         }       }       for (int i = 0; i < n - 1; ++i) {         if (a[i] >= k && a[i + 1] >= k) {           flag = true;         }       }       for (int i = 0; i < n - 2; ++i) {         if (a[i] >= k && a[i + 2] >= k) {           flag = true;         }       }       if (flag) {         cout <<  yes n ;       } else {         cout <<  no n ;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int t, m;   int a[128], id = 1;   int main() {     cin >> t >> m;     for (int i = 0; i < t; ++i) {       string q;       cin >> q;       switch (q[0]) {         case  a : {           int k;           bool ok = false;           cin >> k;           for (int i = 0; i + k <= m; ++i) {             int j = 0;             for (j = 0; j < k && a[i + j] == 0; ++j)               ;             if (j == k) {               ok = true;               for (int j = 0; j < k; ++j) a[i + j] = id;               break;             }           }           if (ok)             cout << id++ << endl;           else             puts( NULL );           break;         }         case  e : {           bool ok = false;           int x;           cin >> x;           if (x > 0) {             for (int i = 0; i < m; ++i)               if (a[i] == x) ok = true, a[i] = 0;           }           if (!ok) puts( ILLEGAL_ERASE_ARGUMENT );           break;         }         case  d :           for (int i = remove(a, a + m, 0) - a; i < m; ++i) a[i] = 0;           break;       }     }     return 0;   }
#include <bits/stdc++.h>      using namespace std;   #define N 200020   int cnt, last[N], n, ans;   struct edge {       int to, next;   } e[N << 1];      inline void add(int u, int v) {       e[++cnt] = {v, last[u]}, last[u] = cnt;       e[++cnt] = {u, last[v]}, last[v] = cnt;   }      int dfs(int x, int fa) {       vector<pair<int, int> > q;       q.clear();       for (int i = last[x]; i; i = e[i].next) {           int y = e[i].to;           if (y == fa)continue;           q.push_back({dfs(y, x), y});       }       if (!q.size())return 0;       sort(q.begin(), q.end());       for (int i = 1; i < q.size(); ++i) {           ans = max(q[i - (x == 1)].first + 2, ans);       }       return q[(q.size() - 1) * (x == 1)].first + 1;   }      inline void solve() {       scanf( %d , &n);       for (int i = 1; i <= n; ++i)last[i] = 0;       cnt = 0;       for (int i = 1; i < n; ++i) {           int u, v;           scanf( %d%d , &u, &v);           add(u, v);       }       ans = 0;       ans = max(ans, dfs(1, 0));       printf( %d n , ans);   }      int main() {       int T = 1;       scanf( %d , &T);       for (int i = 1; i <= T; ++i)solve();   }
#include <bits/stdc++.h>   using namespace std;   int hag(int p, int t) {     int d;     d = max(3 * p / 10, p - ((p / 250) * t));     return d;   }   int main() {     int a, b, c, d;     cin >> a >> b >> c >> d;     if (hag(a, c) > hag(b, d)) cout <<  Misha ;     if (hag(a, c) == hag(b, d)) cout <<  Tie ;     if (hag(a, c) < hag(b, d)) cout <<  Vasya ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, k, f, t, sum = -999999999, m, x, i;     cin >> n >> k;     for (i = 0; i < n; i++) {       cin >> f >> t;       if (k < t) {         m = f - (t - k);       } else {         m = f;       }       x = max(sum, m);       sum = x;     }     cout << x << endl;   }
#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   unordered_map<string, int> ma;   int T, n, tot;   int a[1000010], b[1000010];   long long dp[1000010][2], ans;   struct node {     int per, to;   } e[1000010];   int head[1000010];   long long sum;   void addedge(int from, int to) {     e[++tot].to = to;     e[tot].per = head[from];     head[from] = tot;   }   void dfs(int u, int fa) {     dp[u][b[u]]++;     for (int i = head[u]; i > 0; i = e[i].per) {       int v = e[i].to;       if (v == fa) continue;       dfs(v, u);       sum += dp[v][0] * dp[u][1] + dp[v][1] * dp[u][0];       if (b[u] > 0) {         dp[u][0] += dp[v][1];         dp[u][1] += dp[v][0];       } else {         dp[u][0] += dp[v][0];         dp[u][1] += dp[v][1];       }     }     sum += b[u];   }   int main() {     T = 1;     while (T--) {       memset(head, -1, sizeof(head));       scanf( %d , &n);       for (int i = 1; i <= n; ++i) {         scanf( %d , &a[i]);       }       for (int i = 1; i < n; ++i) {         int u, v;         scanf( %d%d , &u, &v);         addedge(u, v);         addedge(v, u);       }       for (int i = 0; i <= 20; ++i) {         sum = 0;         for (int j = 1; j <= n; ++j) {           b[j] = (a[j] & (1 << i)) > 0;           dp[j][0] = dp[j][1] = 0;         }         dfs(1, -1);         ans += sum << i;       }       printf( %lld n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1000010;   bitset<N> b;   int p[N];   int cnt[N];   bool was[N];   int main() {     int n, k;     scanf( %d %d , &n, &k);     for (int i = 0; i < n; i++) {       scanf( %d , p + i);       p[i]--;       was[i] = false;     }     for (int i = 1; i <= n; i++) {       cnt[i] = 0;     }     int c1 = 0, c2 = 0;     for (int i = 0; i < n; i++) {       if (was[i]) {         continue;       }       int x = i;       int len = 0;       while (!was[x]) {         was[x] = true;         x = p[x];         len++;       }       c1 += len % 2;       c2 += len / 2;       cnt[len]++;     }     b.set(n);     for (int i = 1; i <= n; i++) {       if (cnt[i] == 0) {         continue;       }       for (int j = 0; (1 << j) <= cnt[i]; j++) {         int x = i * (1 << j);         b |= (b >> x);         cnt[i] -= (1 << j);       }       b |= (b >> (i * cnt[i]));     }     int ans_min = k;     if (!b.test(n - k)) {       ans_min++;     }     int ans_max = 0;     ans_max += 2 * min(k, c2);     k -= min(k, c2);     ans_max += min(k, c1);     printf( %d %d n , ans_min, ans_max);     return 0;   }