malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int n, p;   long double intial[100009], usage[100009];   bool solve(long double x) {     long double pp = p * x;     for (int i = 0; i < n; i++) {       pp -= max(x * usage[i] - intial[i], (long double)0);       if (pp < -1e-10) return false;     }     return true;   }   int main() {     cin >> n >> p;     for (int i = 0; i < n; i++) cin >> usage[i] >> intial[i];     long double low = 0, high = 1e19, m;     for (int i = 0; i < 200; i++) {       m = (low + high) / 2;       if (solve(m))         low = m;       else         high = m;     }     if (low > 1e18 + 20) return cout << -1, 0;     cout << setprecision(6) << fixed << low;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10;   int a[N];   int b[N];   int n;   int mp[2 * N];   int ans;   unordered_map<int, int> num;   int sol_01(int a[], int n) {     memset(mp, -1, sizeof mp);     int cur = 0;     int ans = 0;     int offset = 2e5;     for (int i = 1; i <= n; i++) {       cur += a[i];       if (cur == 0) ans = i;       if (mp[cur + offset] != -1)         ans = max(ans, i - mp[cur + offset]);       else         mp[cur + offset] = i;     }     return ans;   }   int main() {     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       num[a[i]]++;     }     int maxx = 0;     int cc = 0;     for (int i = 1; i <= 100; i++)       if (num[i] > cc) {         maxx = i;         cc = num[i];       }     for (int i = 1; i <= 100; i++)       if (num[i] && i != maxx) {         for (int j = 1; j <= n; j++)           if (a[j] == maxx)             b[j] = 1;           else if (a[j] == i)             b[j] = -1;           else             b[j] = 0;         ans = max(ans, sol_01(b, n));       }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string st;     while (getline(cin, st)) {       int f = 0;       int s = st.size() - 2;       if (st[s] ==    ) {         while (st[s] ==    ) {           if (st[s - 1] ==  a  || st[s - 1] ==  e  || st[s - 1] ==  i  ||               st[s - 1] ==  o  || st[s - 1] ==  u  || st[s - 1] ==  y  ||               st[s - 1] ==  A  || st[s - 1] ==  E  || st[s - 1] ==  I  ||               st[s - 1] ==  O  || st[s - 1] ==  U  || st[s - 1] ==  Y ) {             f++;             break;           }           s--;         }       }       if (st[s] ==  a  || st[s] ==  e  || st[s] ==  i  || st[s] ==  o  ||           st[s] ==  u  || st[s] ==  y  || st[s] ==  A  || st[s] ==  E  ||           st[s] ==  I  || st[s] ==  O  || st[s] ==  U  || st[s] ==  Y ) {         f++;       }       if (f == 0)         cout <<  NO  << endl;       else         cout <<  YES  << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int wrapper(int n) {     while (n) {       int a = n % 3;       n = n / 3;       if (a == 2) return 0;     }     return 1;   }   void solve() {     int n;     cin >> n;     while (wrapper(n) == 0) n++;     cout << n <<   n ;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t = 1;     cin >> t;     while (t--) solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 5;   const int MOD = 1e9 + 7;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     string s;     cin >> s;     sort(s.begin(), s.end());     long long int sz = (int)s.size();     int i = 0, ct = 0;     while (s[i] ==  a ) {       ct++;       i++;     }     while (sz && ct * 2 <= sz) {       char ch = s.back();       sz--;       if (ch ==  a ) ct--;     }     cout << sz;     return 0;   }
#include <bits/stdc++.h>   #include <stdio.h>   using namespace std;      typedef long long ll;   typedef unsigned long long ul;   #define len (int)1e5+5   #define mod (ll)(1e9+7)   #define pq(a) priority_queue< a >   #define pqd(b) priority_queue< b , vector< b > , greater< b >>   #define MP make_pair   #define v(i) vector<i>   #define p(aa,bb) pair< aa,bb>   #define pb push_back   #define pp pop_back   #define clr(a) memset((a),0,sizeof((a)))   #define rep(i,a,b) for(int i=(a);i<=(b);i++)   #define repd(i,a,b) for(ll i=(a);i>=(b);i--)   #define all(a) (a).begin(),(a).end()   #define F first   #define S second   #define ln length()   #define INF (1LL<<61)   #define co cout<<   #define ci cin>>   #define sc(a,b) scanf( %(a) ,&(b))   #define nl   n       #define fio(a,b) freopen(  a  , r ,stdin); freopen(  b , w ,stdout);   #define ios ios::sync_with_stdio(0); cin.tie(0);      ul modpow(ul x,ul n,ul m){       if(n==0) return 1UL%m;       ul u=modpow(x,n/2,m);       u = (u*u)%m;       if(n%2==1) u = (u*x)%m;       return u;   }   bool prm[len];   v(int) prm_no;   void make_prm(){       prm[0]=prm[1]=true;       for(int first=2;first<=len;first++){           if(!prm[first]){               prm_no.pb(first);               for(int second=2*first;second<=len;second+=first)prm[second]=true;           }       }   }   v(int) factor;   void make_factor(int n)   {       for(int i=1;i*i<=n;i++){           if(n%i==0){               factor.pb(i);factor.pb(n/i);               if(i*i==n)factor.pp();           }       }   }   ul pascal_tri[2][(int)105];   void make_pascalTri(int n){       bool f=0;clr(pascal_tri);       for(int i=0;i<=n;i++){           pascal_tri[f][0]=1;           for(int j=1;j<=i;j++){               pascal_tri[f][j]=pascal_tri[!f][j]+pascal_tri[!f][j-1];           }           f=!f;       }   }   int prm_div[len];   void make_prm_div(){       for(auto u:prm_no){           for(int i=u;i<=len;i+=u)prm_div[i]++;       }   }      ll gcd(ll a, ll b)   {       if (b == 0)           return a;       return gcd(b, a % b);   }   ll give_floor(ll n,ll a)   {       if(n%a==0)return n/a;       else{           return n/a+1;       }   }   bool is_perfect(ll n)   {       ll i;       for(i=1;i*i<n;i++);       return i*i==n;   }      int n,k;   void solve(){       ci n>>k;       string s;ci s;       int cnt=0;       for(int i=1;i<=(n-i);i++){           if(s[i-1]==s[n-i])cnt++;           else break;       }       if(cnt>=k and n>=2*k+1)co  YES n ;       else co  NO n ;   }      signed main(){       //ios       int t;//=1;       ci t;       while(t--){           solve();       }   }
#include <bits/stdc++.h>   using namespace std;   long long a, b, c, d, e, f[200009], i, j, zx, xc, s, lf[200009], rg[200009],       tim, up[200009];   vector<long long> v[200009], shv[200009];   pair<long long, long long> dp[200009];   bool bo[200009], ka[200009];   bool anc(long long q, long long w) {     if (lf[q] <= lf[w] && rg[q] >= rg[w])       return 1;     else       return 0;   }   void dfs(long long q, long long w) {     bo[q] = 1;     tim++;     lf[q] = rg[q] = tim;     for (vector<long long>::iterator it = v[q].begin(); it != v[q].end(); it++) {       if (bo[(*it)] == 1 && (*it) != w) {         ka[q] = 1;       }       if (bo[(*it)] == 1) continue;       shv[q].push_back((*it));       dfs((*it), q);       if (rg[q] < rg[(*it)]) rg[q] = rg[(*it)];       if (ka[(*it)] == 1) ka[q] = 1;     }   }   void dfs2(long long q, long long w) {     up[q] = q;     for (vector<long long>::iterator it = v[q].begin(); it != v[q].end(); it++) {       if ((*it) == w) continue;       if (anc((*it), up[q]) == 1) up[q] = (*it);     }     for (vector<long long>::iterator it = shv[q].begin(); it != shv[q].end();          it++) {       dfs2((*it), w);       if (anc(up[(*it)], up[q]) == 1) up[q] = up[(*it)];     }   }   void dfs3(long long q, long long w) {     dp[q].first = 0;     dp[q].second = 0;     long long pas = 0, mx = 0, mxx = 0, ma = 0;     for (vector<long long>::iterator it = shv[q].begin(); it != shv[q].end();          it++) {       dfs3((*it), q);       pas += dp[(*it)].first;       if (mxx < dp[(*it)].second) mxx = dp[(*it)].second;       if (ma < dp[(*it)].second - dp[(*it)].first)         ma = dp[(*it)].second - dp[(*it)].first;     }     if (ka[q] == 1) {       dp[q].first = pas + f[q];     }     dp[q].second = pas + f[q] + ma;   }   int main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     cin >> a >> b;     for (i = 1; i <= a; i++) cin >> f[i];     for (i = 1; i <= b; i++) {       cin >> c >> d;       v[c].push_back(d);       v[d].push_back(c);     }     cin >> s;     dfs(s, 0);     dfs2(s, 0);     dfs3(s, 0);     cout << dp[s].second;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long oo = 0x7FFFFFFF;   int n;   char bad[105][105];   int len[105];   char st[105];   int leng;   char let[5], letter;   bool mai[105];   bool alterar[105];   void read() {     scanf( %d , &n);     char *c;     for (int i = 0; i < n; i++) {       scanf( %s , bad[i]);       c = bad[i];       while (*c) {         if ( A  <= *c && *c <=  Z ) *c = *c -  A  +  a ;         c++;       }       len[i] = c - bad[i];     }     scanf( %s%s , st, let);     leng = 0;     while (st[leng]) {       if ( a  <= st[leng] && st[leng] <=  z )         mai[leng] = false;       else         mai[leng] = true;       alterar[leng] = false;       if (mai[leng]) st[leng] = st[leng] -  A  +  a ;       leng++;     }     letter = let[0];   }   void comp(int k) {     int lasts = leng - len[k] + 1;     for (int i = 0; i < lasts; i++) {       bool igual = true;       for (int j = 0; j < len[k]; j++)         if (st[i + j] != bad[k][j]) {           igual = false;           break;         }       if (!igual) continue;       for (int j = 0; j < len[k]; j++) alterar[i + j] = true;     }   }   void process() {     for (int i = 0; i < n; i++) comp(i);     char prox =  a ;     if (letter ==  a ) prox =  b ;     for (int i = 0; i < leng; i++) {       if (alterar[i]) {         if (st[i] == letter)           st[i] = prox;         else           st[i] = letter;       }       if (mai[i]) st[i] = st[i] +  A  -  a ;     }     printf( %s n , st);   }   int main() {     read();     process();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, k;   int odd, even;   bool win(int k, int odd, int even) {     if (odd * 2 <= k || (even * 2 <= k && (k - even) % 2 == odd % 2))       return false;     return true;   }   int main() {     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; i++) {       int x;       scanf( %d , &x);       if (x & 1)         odd++;       else         even++;     }     k = n - k;     if (k == 0) {       if (odd & 1)         printf( Stannis n );       else         printf( Daenerys n );     } else if (k % 2 == 0) {       if (even * 2 <= k && (k - even) % 2 != odd % 2)         printf( Stannis n );       else         printf( Daenerys n );     } else if (k % 2 == 1) {       if ((odd > 0 && win(k - 1, odd - 1, even)) ||           (even > 0 && win(k - 1, odd, even - 1)))         printf( Stannis n );       else         printf( Daenerys n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<pair<long long, long long> > v;   long long x;   long long k;   int broj;   int main(void) {     cin >> x;     k = 0;     long long i = 1;     for (long long i = 1; i <= sqrt(x); ++i) {       if (k + i * i > (1000000000000000000)) goto van;       k = k + i * i;       if (x - k < 0) goto van;       if ((2 * (x - k)) % (i * (i + 1)) == 0) {         v.push_back(             pair<long long, long long>(i, i + (2 * (x - k)) / (i * (i + 1))));       }     }   van:;     for (int i = 0; i < v.size(); ++i) {       broj++;     }     for (int i = v.size() - 1; i >= 0; --i) {       if (v[i].second == v[i].first) continue;       broj++;     }     cout << broj << endl;     for (int i = 0; i < v.size(); ++i) {       broj++;       cout << v[i].first <<     << v[i].second << endl;     }     for (int i = v.size() - 1; i >= 0; --i) {       if (v[i].second == v[i].first) continue;       broj++;       cout << v[i].second <<     << v[i].first << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     long long k;     scanf( %d , &n);     scanf( %I64d , &k);     int a;     scanf( %d , &a);     int ans = a;     int count = 0;     if (k > n - 2) {       for (int i = 1; i < n; ++i) {         scanf( %d , &a);         if (a > ans) ans = a;       }     } else {       for (int i = 1; i < n; ++i) {         scanf( %d , &a);         if (count == k) break;         if (ans > a)           count++;         else {           count = 1;           ans = a;         }       }     }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t, n;     scanf( %d , &t);     while (t--) {       scanf( %d , &n);       for (int i = n * 4, cnt = 1; cnt < n; cnt++, i -= 2) printf( %d  , i);       printf( %d n , n * 2 + 2);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, q;   char s[111111][12];   const int OFF = 128, BITS = 7, MASK = 127;   int dy[111111][12], dx[111111][12];   char qu[5];   int mx;   void go(int &y, int &x, int upTo) {     for (int z = 0; z < (int)(mx); ++z) {       if (s[y][x] ==  ^ ) {         --y;         if (y == upTo || y == 0) break;       } else if (s[y][x] ==  < ) {         if (--x == 0) break;       } else {         if (++x > m) break;       }     }   }   int main() {     scanf( %d%d%d , &n, &m, &q);     for (int i = 1; i <= n; ++i)       scanf( %s , s[i] + 1), s[i][0] = s[i][m + 1] =  f ;     for (int i = 0; i < (int)(m + 2); ++i) s[0][i] = s[n + 1][i] =  f ;     mx = (OFF + 2) * m;     for (int i = OFF; i <= n; i += OFF)       for (int j = 1; j <= m; ++j) {         int y = i, x = j;         go(y, x, i - OFF);         if (y != i - OFF && y != 0 && x != 0 && x != m + 1)           dy[i][j] = -1;         else {           dy[i][j] = y;           dx[i][j] = x;         }       }     for (int query = 0; query < (int)(q); ++query) {       scanf( %s , qu);       if (qu[0] ==  C ) {         int Y, X;         scanf( %d%d%s , &Y, &X, qu);         s[Y][X] = qu[0];         if (Y & MASK) {           Y &= ~MASK;           Y += OFF;         }         for (int j = 1; j <= m; ++j) {           int y = Y, x = j;           go(y, x, Y - OFF);           if (y != Y - OFF && y != 0 && x != 0 && x != m + 1)             dy[Y][j] = -1;           else {             dy[Y][j] = y;             dx[Y][j] = x;           }         }       } else {         int y, x;         scanf( %d%d , &y, &x);         if (y & MASK) {           int upTo = y & ~MASK;           go(y, x, upTo);           if (y != upTo && y != 0 && x != 0 && x != m + 1) {             printf( -1 -1 n );             continue;           }         }         bool flag = false;         while (y != 0 && x != 0 && x != m + 1) {           int newY = dy[y][x];           x = dx[y][x];           y = newY;           if (y == -1) {             printf( -1 -1 n );             flag = true;             break;           }         }         if (flag) continue;         printf( %d %d n , y, x);       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using LL = long long;   constexpr int N = 2e5 + 5;   int p[N];   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     int n;     cin >> n;     for (int i = 2; i <= n; i++) {       int x;       cin >> x;       p[i] = x;     }     vector<int> path;     int x = n;     while (1) {       path.push_back(x);       x = p[x];       if (x == 0) break;     }     reverse(begin(path), end(path));     for (int x : path) cout << x <<    ;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int i, j, k, res, n, sum, t, w, temp, sz1, sz2;     while (scanf( %d , &n) == 1) {       vector<int> v[3];       sum = 0;       for (i = 0; i < n; i++) {         scanf( %d%d , &t, &w);         v[t].push_back(w);         sum += w;       }       sort(v[1].begin(), v[1].end());       sort(v[2].begin(), v[2].end());       sz1 = v[1].size();       sz2 = v[2].size();       res = 10000000;       for (i = 0; i <= sz1; i++) {         for (j = 0; j <= sz2; j++) {           temp = sum;           for (k = sz1 - 1; k > sz1 - 1 - i; k--) {             temp -= v[1][k];           }           for (k = sz2 - 1; k > sz2 - 1 - j; k--) {             temp -= v[2][k];           }           if (temp <= i + j * 2) res = res < i + j * 2 ? res : i + j * 2;         }       }       printf( %d n , res);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool valid(long long num, long long den) {     if (den == 0) {       return num == 0;     } else {       return (num % den) == 0;     }   }   long long gcd(long long a, long long b) {     if (b == 0) {       return a;     } else {       return gcd(b, a % b);     }   }   bool valid(long long a, long long b, long long c) {     if (a == 0 && b == 0) {       return c == 0;     }     if (a < 0) {       a = -a;     }     if (b < 0) {       b = -b;     }     return c % gcd(a, b) == 0;   }   int main() {     long long ax, ay, bx, by, cx, cy;     cin >> ax >> ay >> bx >> by >> cx >> cy;     long long a_x = -ay, a_y = ax;     long long c_x = -cy, c_y = cx;     for (long long a = -1; a <= 1; ++a) {       for (long long a_ = -1; a_ <= 1; ++a_) {         if (abs(a) + abs(a_) == 1) {           long long X = bx - a * ax - a_ * a_x;           long long Y = by - a * ay - a_ * a_y;           long long num1 = X * c_y - Y * c_x;           long long den1 = cx * c_y - cy * c_x;           long long num2 = X * cy - Y * cx;           long long den2 = c_x * cy - c_y * cx;           bool ok;           if (den1 != 0 && den2 != 0) {             ok = (num1 % den1) == 0 && (num2 % den2) == 0;           } else {             if (cx == 0 && c_x == 0) {               ok = X == 0 && valid(cy, c_y, Y);             } else if (cy == 0 && c_y == 0) {               ok = Y == 0 && valid(cx, c_x, X);             } else {               if (cx == 0) {                 ok = (X % c_x) == 0 && (Y % c_y) == 0 && X / c_x == Y / c_y;               } else if (c_x == 0) {                 ok = (X % cx) == 0 && (Y % cy) == 0 && X / cx == Y / cy;               } else {                 ok = valid(cx, c_x, X) && X * cy == Y * cx;               }             }           }           if (ok) {             cout <<  YES  << endl;             return 0;           }         }       }     }     cout <<  NO  << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   double pi = 4 * atan(1.0);   double eps = 1e-9;   string stringmin =  ! ;   string stringmax =  ~ ;   int main() {     cout.precision(14);     int n;     cin >> n;     vector<int> v(n), vlen(n);     for (auto &i1 : v) cin >> i1;     for (int i = 0; i < n; i++) {       int f1 = 0;       int q = i;       for (int j = 0; j < 101; j++) {         q = v[q] - 1;         if (q == i) {           f1 = 1;           vlen[i] = j + 1;           break;         }       }       if (!f1) {         cout << -1;         return 0;       }     }     for (auto &i1 : vlen)       if ((i1 & 1) == 0) i1 >>= 1;     long long ans = 1;     for (int i = 2; i < 110; i++) {       int maxcnt = 0;       for (auto &i1 : vlen) {         int cnt = 0;         while (i1 % i == 0) {           cnt++;           i1 /= i;         }         maxcnt = max(cnt, maxcnt);       }       for (int j = 0; j < maxcnt; j++) ans *= i;     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct S {     int dist, u, v;     S(int dd, int uu, int vv) {       dist = dd;       u = uu;       v = vv;     }     bool operator<(const S& a) const {       if (dist != a.dist) return dist < a.dist;       return u < a.u;     }     bool operator>(const S& a) const {       if (dist != a.dist) return dist > a.dist;       return u > a.u;     }   };   vector<pair<int, int> > pA;   vector<pair<int, int> > pB;   vector<int> A;   vector<int> B;   pair<int, int> res[100007];   int main() {     int N, M;     scanf( %d %d  , &N, &M);     ;     for (int i = (0); i < (M); i++) {       int a, b;       scanf( %d %d  , &a, &b);       ;       if (b)         A.push_back(a);       else         B.push_back(a);       if (b)         pA.push_back(make_pair(a, i));       else         pB.push_back(make_pair(a, i));     }     sort(A.begin(), A.end());     sort(B.begin(), B.end());     sort(pA.begin(), pA.end());     sort(pB.begin(), pB.end());     priority_queue<S, vector<S>, greater<S> > pq;     for (int i = (0); i < ((int)A.size() - 1); i++)       pq.push(S(A[i + 1], i, i + 1));     for (int m = (0); m < (B.size()); m++) {       S s = pq.top();       pq.pop();       if (B[m] < s.dist) {         cout << -1 << endl;         return 0;       }       res[pB[m].second] = make_pair(s.u, s.v);       s.v++;       if (s.v < A.size()) {         s.dist -= A[s.v - 1];         s.dist += A[s.v];         pq.push(s);       }     }     for (int i = (0); i < (A.size()); i++)       res[pA[i].second] = make_pair(i, N - 1);     for (int i = (0); i < (M); i++)       cout << res[i].first + 1 <<     << res[i].second + 1 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     scanf( %d , &t);     while (t--) {       int n, s1 = 0, s2 = 0;       scanf( %d , &n);       int a[100] = {0};       for (int i = 0; i < n; i++) {         scanf( %d , a + i);         if (a[i] % 2)           s1++;         else           s2++;       }       if (s1 && s2)         printf( NO n );       else         printf( YES n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long l1, r1, l2, r2, k, l, r;     cin >> l1 >> r1 >> l2 >> r2 >> k;     l = max(l1, l2);     r = min(r1, r2);     if (r < l)       cout << 0;     else if (l <= k && k <= r)       cout << r - l;     else       cout << r - l + 1 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int putere(long long int a, long long int b) {     if (b == 0) {       return 1;     } else {       if (b == 1)         return a % 1000000007;       else {         long long int r;         r = putere(a, b / 2);         r = (r * r) % 1000000007;         if (b % 2 == 0)           return r;         else           return (r * a) % 1000000007;       }     }   }   int main() {     long long int x, k, nr1, nr2;     scanf( %lld%lld , &x, &k);     if (x == 0)       printf( 0 );     else {       x = x % 1000000007;       nr1 = putere(2, k);       nr2 = nr1 * x * 2 % 1000000007;       printf( %lld , (nr2 - nr1 + 1 + 1000000007) % 1000000007);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long int k, n, first = -1, sum = 0;     cin >> n >> k;     long long int a[n];     long long int ans = 0;     for (long long int i = 0; i < n; ++i) cin >> a[i];     string s;     cin >> s;     int flag = 0;     for (long long int i = 0; i < n; ++i) {       if (s[i] == s[i + 1]) {         if (first == -1) first = i;         flag = 1;       } else if (flag) {         sort(a + first, a + i + 1);         long long int mini = min(k, i - first + 1);         for (long long int j = 0; j < mini; ++j) {           sum += a[i - j];         }         flag = 0;         first = -1;       } else {         sum += a[i];       }     }     cout << sum << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct DSU {     long long int parent[500001], connected[500001];     void intialize(int x) {       for (long long int i = 1; i <= x; i++) {         parent[i] = i;         connected[i] = 1;       }     }     long long int getparent(long long int k) {       while (k != parent[k]) {         parent[k] = parent[parent[k]];         k = parent[k];       }       return k;     }     long long int getsize(long long int k) { return connected[getparent(k)]; }     void unionn(long long int u, long long int v) {       long long int parent1 = getparent(u), parent2 = getparent(v);       if (parent1 == parent2) return;       if (connected[parent1] > connected[parent2]) swap(parent1, parent2);       connected[parent2] += connected[parent1];       connected[parent1] = 0;       parent[parent1] = parent[parent2];     }   };   DSU dsu;   int main() {     long long int t;     cin >> t;     while (t--) {       long long int n;       cin >> n;       dsu.intialize(n + 1);       for (long long int i = 1; i <= n; i++) {         long long int x;         cin >> x;         dsu.unionn(i, x);       }       for (long long int i = 1; i <= n; i++) cout << dsu.getsize(i) <<    ;       cout <<   n ;     }   }