malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int N = 3 + 2e5;   int root[N], ls[N << 5], rs[N << 5], id;   int sum[N << 5];   int n, q, p[N];   void update(int &u, int v, int l, int r, int p) {     sum[u = ++id] = sum[v] + 1;     if (l == r) return;     int mid = l + r >> 1;     if (p <= mid)       rs[u] = rs[v], update(ls[u], ls[v], l, mid, p);     else       ls[u] = ls[v], update(rs[u], rs[v], mid + 1, r, p);   }   int query(int u, int v, int l, int r, int L, int R) {     if (L <= l && r <= R) return sum[v] - sum[u];     int ans = 0, mid = l + r >> 1;     if (L <= mid) ans = query(ls[u], ls[v], l, mid, L, R);     if (R > mid) ans += query(rs[u], rs[v], mid + 1, r, L, R);     return ans;   }   int query(int d, int u, int l, int r) {     if (l > r || u < d) return 0;     return query(root[l - 1], root[r], 1, n, d, u);   }   int main() {     int l, d, r, u;     scanf( %d%d , &n, &q);     for (int i = 1; i <= n; ++i) {       scanf( %d , p + i);       update(root[i], root[i - 1], 1, n, p[i]);     }     while (q--) {       scanf( %d%d%d%d , &l, &d, &r, &u);       long long a1 = query(u + 1, n, 1, l - 1);       long long a2 = query(u + 1, n, l, r);       long long a3 = query(u + 1, n, r + 1, n);       long long a4 = query(d, u, 1, l - 1);       long long a5 = query(d, u, l, r);       long long a6 = query(d, u, r + 1, n);       long long a7 = query(1, d - 1, 1, l - 1);       long long a8 = query(1, d - 1, l, r);       long long a9 = query(1, d - 1, r + 1, n);       long long ans = 0;       ans = a1 * (a5 + a6 + a8 + a9);       ans += a2 * (a4 + a5 + a6 + a7 + a8 + a9);       ans += a3 * (a4 + a5 + a7 + a8);       ans += a4 * (a5 + a6 + a8 + a9);       ans += a5 * (a6 + a7 + a8 + a9);       ans += a6 * (a7 + a8);       ans += a5 * (a5 - 1) / 2;       printf( %lld n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, k, p;     cin >> n >> k;     int l = log2(n) + 1;     for (long int i = 1; i <= 30; i++) {       if (n - i * k > 0) {         p = n - i * k;         bitset<30> b(p);         if (i > b.count() && p != 1 && p >= i) {           cout << i;           break;         } else if (i == b.count()) {           cout << i;           break;         }       } else {         cout <<  -1 ;         break;       }     }   }
#include <bits/stdc++.h>   using namespace std;   inline int read() {     int s = 0, w = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) w = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       s = s * 10 + ch -  0 ;       ch = getchar();     }     return s * w;   }   pair<int, int> p[200005];   set<pair<int, int> > s;   void solve(int l, int r) {     if (l >= r) return;     int mid = (l + r) >> 1;     for (int i = l; i <= r; i++)       s.insert(pair<int, int>(p[mid].first, p[i].second));     solve(l, mid);     solve(mid + 1, r);     return;   }   int n;   void init() {     n = read();     for (int i = 1; i <= n; i++) {       p[i].first = read();       p[i].second = read();       s.insert(p[i]);     }     sort(p + 1, p + n + 1);     return;   }   void solv() {     solve(1, n);     printf( %d n , (int)s.size());     for (auto i = s.begin(); i != s.end(); i++) {       printf( %d %d n , i->first, i->second);     }     return;   }   int main() {     init();     solv();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long i, j, m, n, k, x, y, sum = 0;     cin >> n >> k;     vector<long long> sura;     vector<pair<long long, long long> > dada;     vector<pair<long long, long long> > dada2;     vector<long long> su;     long long arr[n];     priority_queue<long long, vector<long long>, greater<long long> > pq;     for (i = 0; i < n; i++) {       cin >> x;       sura.push_back(x);     }     for (i = 0; i < n; i++) {       cin >> x;       dada.push_back(make_pair(sura[i], x));       dada2.push_back(make_pair(sura[i], i));     }     sort(dada.begin(), dada.end());     sort(dada2.begin(), dada2.end());     for (i = 0; i < n; i++) {       if (k == 0) {         su.push_back(sum + dada[i].second);       } else if (k > 0) {         su.push_back(sum + dada[i].second);         if (i < k) {           sum += dada[i].second;           pq.push(dada[i].second);         } else {           if (dada[i].second > pq.top()) {             sum -= pq.top();             pq.pop();             sum += dada[i].second;             pq.push(dada[i].second);           }         }       }     }     for (i = 0; i < n; i++) {       arr[dada2[i].second] = su[i];     }     for (i = 0; i < n; i++) {       cout << arr[i] <<    ;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     bool neg = false;     cin >> s;     int n = s.length();     if (s[0] ==  - ) {       neg = true;       n--;     }     int cur = 0;     if (n < 3)       cur = 1;     else if (n == 3)       if ((!neg && s <=  127 ) || (neg && s <=  -128 ))         cur = 1;       else         cur = 2;     else if (n < 5)       cur = 2;     else if (n == 5)       if ((!neg && s <=  32767 ) || (neg && s <=  -32768 ))         cur = 2;       else         cur = 3;     else if (n < 10)       cur = 3;     else if (n == 10)       if ((!neg && s <=  2147483647 ) || (neg && s <=  -2147483648 ))         cur = 3;       else         cur = 4;     else if (n < 19)       cur = 4;     else if (n == 19)       if ((!neg && s <=  9223372036854775807 ) ||           (neg && s <=  -9223372036854775808 ))         cur = 4;       else         cur = 5;     else       cur = 5;     if (cur == 1)       cout <<  byte  << endl;     else if (cur == 2)       cout <<  short  << endl;     else if (cur == 3)       cout <<  int  << endl;     else if (cur == 4)       cout <<  long  << endl;     else       cout <<  BigInteger  << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long mp[2005];   int main() {     memset(mp, 0, sizeof mp);     long long key[2001], a[2001], n, k, p, i, j;     cin >> n >> k >> p;     for (i = 0; i < n; i++) {       scanf( %lld , a + i);     }     for (i = 0; i < k; i++) {       scanf( %lld , key + i);     }     sort(a, a + n);     sort(key, key + k);     long long mn = 1e18;     for (i = 0; i < k - n + 1; i++) {       long long x = -1;       for (int j = i, l = 0; l < n; l++, j++) {         long long y = abs(a[l] - key[j]) + abs(p - key[j]);         x = max(y, x);       }       mn = min(x, mn);     }     cout << mn;   }
#include <bits/stdc++.h>   using namespace std;   long long n, N, m, op, w, ax, ay, bx, by, cx, cy, ix, iy, bit[4][505][505];   long long v(long long x) {     if (x & 1)       return (x >> 1) + 1;     else       return x >> 1;   }   long long P(long long x, long long y) {     if ((x & 1) && (y & 1)) return 0;     if ((x & 1) && !(y & 1)) return 1;     if (!(x & 1) && (y & 1)) return 2;     if (!(x & 1) && !(y & 1)) return 3;   }   long long lowbit(long long x) { return x & (-x); }   void add(long long x, long long y, long long w) {     long long i, j, k = P(x, y);     ix = v(x);     iy = v(y);     for (i = ix; i <= N; i += lowbit(i))       for (j = iy; j <= N; j += lowbit(j)) bit[k][i][j] ^= w;   }   long long get(long long x, long long y) {     if (x == 0 || y == 0) return 0;     long long i, j, k = P(x, y), ret = 0;     ix = v(x);     iy = v(y);     for (i = ix; i; i -= lowbit(i))       for (j = iy; j; j -= lowbit(j)) ret ^= bit[k][i][j];     return ret;   }   signed main() {     long long i, j;     scanf( %lld%lld , &n, &m);     N = v(n);     while (m--) {       scanf( %lld%lld%lld%lld%lld , &op, &ax, &ay, &bx, &by);       if (op == 1)         printf( %lld n , get(bx, by) ^ get(ax - 1, by) ^ get(bx, ay - 1) ^                              get(ax - 1, ay - 1));       else {         scanf( %lld , &w);         add(ax, ay, w);         add(bx + 1, ay, w);         add(ax, by + 1, w);         add(bx + 1, by + 1, w);       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long poww(long long a, long long b, long long md) {     return (!b ? 1                : (b & 1 ? a * poww(a * a % md, b / 2, md) % md                         : poww(a * a % md, b / 2, md) % md));   }   const long long MAXN = 1e5 + 10;   const long long INF = 8e18;   const long long MOD = 1e9 + 7;   set<pair<long long, long long> > st;   set<long long> lst[MAXN], rst[MAXN], ust[MAXN], dst[MAXN];   long long n, m, k;   int main() {     cin >> n >> m >> k;     long long u = 0, d = n + 1, l = 0, r = m + 1;     for (int i = 0; i < k; i++) {       long long x, y;       cin >> x >> y;       st.insert({x, y});       dst[y].insert(x);       ust[y].insert(-x);       rst[x].insert(y);       lst[x].insert(-y);     }     long long dir = 0, cnt = 0;     while (u + 1 < d && l + 1 < r) {       if (dir == 0) {         u++;         auto it = rst[u].lower_bound(l + 1);         if (it != rst[u].end()) r = min(*it, r);         cnt += r - l - 1;       } else if (dir == 1) {         r--;         auto it = dst[r].lower_bound(u + 1);         if (it != dst[r].end()) d = min(*it, d);         cnt += d - u - 1;       } else if (dir == 2) {         d--;         auto it = lst[d].lower_bound(-1 * (r - 1));         if (it != lst[d].end()) l = max(-1 * (*it), l);         cnt += r - l - 1;       } else if (dir == 3) {         l++;         auto it = ust[l].lower_bound(-1 * (d - 1));         if (it != ust[l].end()) u = max(-1 * (*it), u);         cnt += d - u - 1;       }       dir++;       dir %= 4;     }     if (cnt == n * m - k) return cout <<  Yes  <<   n , 0;     cout <<  No  <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long i, a[3333];     int t, n;     cin >> t;     while (t--) {       cin >> n;       for (i = 0; i < n; i++) cin >> a[i];       sort(a, a + n);       long long x;       x = a[0] * a[n - 1];       int flag = 1, ans = 0;       for (i = 0; i < n; i++) {         if (x % a[i] != 0) flag = 0;       }       if (flag) {         for (i = 2; i <= sqrt(x); i++) {           if (x % i == 0) {             if (x == i * i)               ans++;             else               ans += 2;           }         }       }       if (flag && ans == n)         cout << x << endl;       else         cout <<  -1  << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   pair<int, int> cost[111111];   bool okay[111111];   int main(void) {     int n = 0;     int k = 0;     scanf( %d %d , &n, &k);     long long b = 0;     scanf( %I64d , &b);     for (int i = 0; i < n; i++) {       scanf( %d , &cost[i].first);       cost[i].second = i;     }     cost[n - 1].first = 0;     sort(cost, cost + n);     reverse(cost, cost + n);     long long sum = 0;     for (int i = 0; i < k; i++) {       sum += cost[i].first;       okay[cost[i].second] = true;     }     int last = cost[k - 1].first;     for (int i = 0; i < n; i++) swap(cost[i].first, cost[i].second);     sort(cost, cost + n);     int ans = -1;     if (sum <= b) {       ans = n - 1;     } else {       sum -= last;       for (int i = 0; i < n; i++) {         if (okay[i] || sum + cost[i].second > b) {           ans = i;           break;         }       }     }     printf( %d n , ans + 1);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXSZ = 100000;   int req[MAXSZ];   int prob[MAXSZ];   int main(int argc, const char* argv[]) {     int n, m, k;     scanf( %d%d , &n, &m);     int count = 1;     for (int i = 0; i < m; i++) {       scanf( %d , &k);       prob[k]++;       req[prob[k]]++;       if (req[count] == n) {         cout << 1;         count++;       } else {         cout << 0;       }     }   }
#include <bits/stdc++.h>   using namespace std;   map<long long int, long long int> m;   int main() {     long long int t;     cin >> t;     while (t--) {       long long int n;       cin >> n;       long long int a[n];       for (long long int i = 0; i < n; i++) cin >> a[i];       sort(a, a + n);       long long int x = 0, y = n - 1, iff = 1;       vector<long long int> v;       for (long long int i = 0; i < n; i++) {         if (iff) {           v.push_back(a[y]);           y--;           iff = 0;         } else {           v.push_back(a[x]);           x++;           iff = 1;         }       }       for (long long int i = n - 1; i >= 0; i--) cout << v[i] <<    ;       cout << endl;     }   }
#include<iostream>   #include<bits/stdc++.h>   using namespace std;   typedef long long int ll;   ll max(ll a,ll b){       if(a>b)return a;       return b;   }   int main(){       int T;       cin>>T;       while(T--){           int n,k;           cin>>n>>k;           string s1,s2;           cin>>s1;           cin>>s2;           vector<int>c1(26,0),c2(26,0);           for(int i=0;i<s1.length();i++){               c1[s1[i]- a ]++;           }           for(int i=0;i<s2.length();i++){               c2[s2[i]- a ]++;           }           for(int i=0;i<26;i++){               if(c1[i]==c2[i]){                  c1[i]=0;                  c2[i]=0;               }               else if(c1[i]<c2[i]){                   c2[i]-=c1[i];                   c1[i]=0;               }               else{                   c1[i]-=c2[i];                   c2[i]=0;               }           }           int f=0;           int j=25;int i=25;           while(j>=0 && i>=0){               if(c1[i]==0){                  i--;                  continue;               }               if(c2[j]==0){                  j--;                  continue;               }               if(i>j){                  f=1;                  break;               }                         if(c1[i]%k!=0){                  f=1;                  break;              }               if(c2[j]%k!=0){                  f=1;                  break;               }               if(c1[i]==c2[j]){                  c1[i]=0;                  c2[j]=0;                  i--;                  j--;                                 }               else if(c1[i]>c2[j]){                  c1[i]-=c2[j];                  j--;               }               else{                  c2[j]-=c1[i];                  i--;               }           }           if(f==1)           cout<< NO <<endl;           else           cout<< YES <<endl;       }   }
#include <bits/stdc++.h>   using namespace std;   int read() {     char c = getchar();     int d = 0, f = 1;     for (; c <  0  || c >  9 ; c = getchar())       if (c ==  - ) f = -1;     for (; c >=  0  && c <=  9 ; d = d * 10 + c - 48, c = getchar())       ;     return d * f;   }   const int mod = 1e9 + 7;   const int N = 3000010;   int n, q, ft[N + 1], Ft[N + 1], ans[N + 1];   int mi(int a, int b) {     int s = 1;     for (; b; b >>= 1, a = (long long)a * a % mod)       if (b & 1) s = (long long)s * a % mod;     return s;   }   int main() {     n = read(), q = read();     int maxn = n * 3 + 1;     ft[0] = 1;     for (int i = 1; i <= maxn; ++i) ft[i] = 1LL * ft[i - 1] * i % mod;     Ft[maxn] = mi(ft[maxn], mod - 2);     for (int i = maxn; i >= 1; --i) Ft[i - 1] = 1LL * Ft[i] * i % mod;     long long y0 = (3LL * (n + 1) * n / 2) % mod, y1 = (y0 + mod - n * 2) % mod,               y2 = (y0 + mod - n) % mod, ni3 = mi(3, mod - 2);     ans[1] = y0;     for (int i = 2; i <= n * 3; ++i) {       long long sum = 1LL * ft[maxn] * Ft[i + 1] % mod * Ft[maxn - 1 - i] % mod;       long long x1 = (sum - y1 - y1 - y2) * ni3 % mod;       if (x1 < 0) x1 += mod;       long long x2 = (x1 + y1);       if (x2 >= mod) x2 -= mod;       long long x0 = (x2 + y2);       if (x0 >= mod) x0 -= mod;       y0 = x0, y1 = x1, y2 = x2;       ans[i] = x0;     }     for (int i = 1; i <= q; ++i) {       int x = read();       printf( %d n , ans[x]);     }   }
#include <bits/stdc++.h>   using namespace std;   inline int mini(int a, int b) { return a < b ? a : b; }   inline int maxi(int a, int b) { return a > b ? a : b; }   int main() {     int n;     scanf( %d , &n);     int *a = new int[n];     int najm = 1000000010;     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);       najm = mini(najm, a[i]);     }     vector<int> vec;     for (int i = 0; i < n; i++) {       if (a[i] == najm) {         vec.push_back(i);       }     }     int najv = 0;     for (int i = 0; i < vec.size() - 1; i++) {       najv = maxi(najv, vec[i + 1] - vec[i] - 1);     }     najv = maxi(najv, n - 1 - vec[vec.size() - 1] + vec[0]);     long long res = ((long long)najm) * (long long)n + (long long)najv;     cout << res;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());   void setIO(string name) {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cin.exceptions(istream::failbit);   }   const int inf = 0x3f3f3f3f, mod = 1e9 + 7;   int main() {     setIO( 1 );     int n, k;     cin >> n >> k;     vector<int> a(n);     for (int i = 0; i < n; ++i) {       cin >> a[i];     }     int sol = 0, j = 0, used = 0;     pair<int, int> inter;     for (int i = 0; i < n; ++i, j = max(j, i)) {       if (used < 0) {         used = 0;       }       while (j < n) {         if (used + !a[j] > k) {           break;         }         if (a[j++] == 0) {           ++used;         }       }       if (j - i > sol) {         sol = j - i;         inter = {i, j};       }       if (a[i] == 0) {         --used;       }     }     cout << sol <<   n ;     for (int i = 0; i < inter.first; ++i) {       cout << a[i] <<    ;     }     for (int i = inter.first; i < inter.second; ++i) {       cout << 1 <<    ;     }     for (int i = inter.second; i < n; ++i) {       cout << a[i] <<    ;     }     cout <<   n ;     return 0;   }
#include <bits/stdc++.h>   int main() {     int i, j, l = 0, a, A[6], n[6], m = 0;     for (i = 0; i < 6; i++) {       scanf( %d , &a);       for (j = 0; j < l; j++) {         if (A[j] == a) break;       }       if (j == l) {         n[l] = 1;         A[l++] = a;       } else         n[j]++;     }     for (i = 0; i < l; i++) {       if (n[i] > m) m = n[i];     }     if (l == 1 || (l == 2 && m == 4))       printf( Elephant );     else if ((l == 2 && m == 5) || (l == 3 && m == 4))       printf( Bear );     else       printf( Alien );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, i;     cin >> n;     long a[n + 1], b[n + 1];     for (i = 0; i < n; i++) cin >> a[i];     a[n] = 1;     b[0] = 1;     for (i = 1; i <= n; i++) {       b[i] = (2 * b[i - 1] - b[a[i - 1] - 1] + 2) % 1000000007;       if (b[i] < 0) b[i] += 1000000007;     }     cout << (b[n] - 1);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int inf = 80009;   unsigned long long n;   vector<int> adj[inf];   unsigned long long low[inf];   unsigned long long sl[inf];   unsigned long long sz[inf];   unsigned long long als;   unsigned long long ans = 0;   void dfs(int x, int p) {     unsigned long long s = 0;     int k;     unsigned long long tr = 0, fr = 0;     for (int i = 0; i < adj[x].size(); ++i) {       k = adj[x][i];       if (k == p) continue;       dfs(k, x);       sl[x] += sl[k];       fr += sz[k] * tr;       tr += sz[k] * low[x];       low[x] += sz[k] * sz[x];       sz[x] += sz[k];     }     fr += (n - sz[x] - 1) * tr;     tr += (n - sz[x] - 1) * low[x];     ans += (fr + tr) * 6 + ((n - sz[x] - 1) * sz[x] + low[x]) * 2;     low[x] += sz[x];     low[x] += low[x] + 1;     sl[x] += low[x];     sz[x]++;   }   void solve(int x, int p) {     ans += (als - sl[x]) * low[x];     als -= low[x];     unsigned long long g = low[x] / 2 + (n - sz[x]) * sz[x];     int k;     for (int i = 0; i < adj[x].size(); ++i) {       k = adj[x][i];       if (k == p) continue;       ans += sl[k] * ((g - sz[k] * (n - sz[k])) * 2 + 1);       solve(k, x);     }   }   int main() {     int a, b;     scanf( %d , &n);     for (int i = 0; i < n - 1; ++i) {       scanf( %d%d , &a, &b);       adj[a].push_back(b);       adj[b].push_back(a);     }     dfs(1, 1);     als = sl[1];     solve(1, 1);     unsigned long long all = (n * (n - 1)) / 2;     all = all * all;     printf( %I64d n , all - ans);   }
#include <bits/stdc++.h>   using namespace std;   vector<int> graph[200000];   int color[200000], black, white, maxDist, farNode;   void solve(int node, int par, int dis) {     if (dis > maxDist) farNode = node, maxDist = dis;     for (auto &v : graph[node]) {       if (v == par) continue;       solve(v, node, dis + (color[node] != color[v]));     }   }   int main() {     ios ::sync_with_stdio(false);     cin.tie();     int n, a, b, firstNode;     cin >> n;     for (int i = 0; i < n; i++) cin >> color[i];     for (int i = 0; i < n - 1; i++) {       cin >> a >> b;       a--, b--;       graph[a].push_back(b);       graph[b].push_back(a);     }     solve(0, -1, 1);     maxDist = 1;     solve(farNode, -1, 1);     cout << (maxDist) / 2;     return 0;   }
#include <bits/stdc++.h>   const int INF = 0x3f3f3f3f;   const int maxn = 3e3 + 50;   const int Mod = 1000000007;   const double PI = acos(-1);   using namespace std;   struct Ans {     int id, x;     string ret;   };   Ans ans[maxn];   string v[maxn];   int cnt;   bool full;   int n;   inline bool cmpById(const Ans &a, const Ans &b) { return a.id < b.id; }   inline bool cmpByX(const Ans &a, const Ans &b) { return a.x < b.x; }   bool cmpByLen(const string &a, const string &b) { return a.size() > b.size(); }   void expend(int dem, int len) {     if (full) return;     while (1) {       int sz = cnt;       for (int i = sz - 1; i >= 0; i--) {         v[cnt++] = v[i] +  0 ;         v[i] = v[i] +  1 ;       }       if (cnt >= dem) full = true;       sort(v, v + cnt, cmpByLen);       if (full || v[cnt - 1].size() == len) return;     }   }   bool solve() {     v[cnt++] =  0 ;     v[cnt++] =  1 ;     for (int i = 0; i < n; i++) {       int tar = ans[i].x;       if (cnt == 0) return false;       if (v[cnt - 1].size() < tar) expend(n - i, tar);       if (full) {         v[cnt - 1] = v[cnt - 1] + string(tar - v[cnt - 1].size(),  1 );       }       ans[i].ret = v[cnt - 1];       cnt--;     }     return true;   }   int main() {     while (scanf( %d , &n) != EOF) {       for (int i = 0; i < n; i++) {         ans[i].id = i;         scanf( %d , &ans[i].x);       }       cnt = 0;       full = false;       sort(ans, ans + n, cmpByX);       if (solve()) {         puts( YES );         sort(ans, ans + n, cmpById);         for (int i = 0; i < n; i++) puts(ans[i].ret.c_str());       } else         puts( NO );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a[100010];     for (int i = 0; i < n; i++) {       cin >> a[i];     }     sort(a, a + n);     int ans = 0;     int re = 0;     for (int i = 0; i < n; i++) {       int t = 1;       while (a[i + 1] == a[i] && (i < n - 1)) {         t += 1;         i += 1;       }       if (i == n - 1) {         cout << ans;         return 0;       } else if (i == n - 2) {         cout << ans + 1;         return 0;       }       re = max(re, i + 1);       if (t < (n - re)) {         re += t;         ans += t;       } else if (t == n - re) {         ans += t;         cout << ans;         return 0;       } else {         ans += (n - re);         cout << ans;         return 0;       }     }     return 0;   }