#include <bits/stdc++.h>   int main() {     std::ios_base::sync_with_stdio(0);     int n, m, k, a, ans = 0;     std::cin >> n >> m >> k;     for (int i = 0; i < n; i++) {       std::cin >> a;       if (a == 1) {         m--;         if (m == -1) {           m++;           ans++;         }       }       if (a == 2) {         if (k > 0)           k--;         else {           m--;           if (m == -1) {             m++;             ans++;           }         }       }     }     std::cout << ans++;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int row[5001] = {0};   int rowt[5001] = {0};   int col[5001] = {0};   int colt[5001] = {0};   int main() {     int n, m, k;     cin >> n >> m >> k;     int q, t, a;     for (int i = 1; i < k + 1; i++) {       cin >> q >> t >> a;       t--;       if (q == 1) {         row[t] = a;         rowt[t] = i;       }       if (q == 2) {         col[t] = a;         colt[t] = i;       }     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (colt[j] > rowt[i])           cout << col[j] <<    ;         else           cout << row[i] <<    ;       }       cout << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int Array[n];     for (int i = (int)0; i < (int)n; ++i) cin >> Array[i];     stable_sort(Array, Array + n);     int Sum = accumulate(Array, Array + n, 0, plus<int>());     int Middle = Sum / n;     vector<int> a(n, Middle);     int NewSum = Middle * n;     int Diff = n - (Sum - NewSum);     int Ans = 0;     for (int i = 0; i < n; ++i) {       if (i >= Diff) {         Ans += abs(Array[i] - (a[i] + 1));       } else {         Ans += abs(Array[i] - a[i]);       }     }     cout << Ans / 2;     return 0;   }


#include <bits/stdc++.h>   const double pi = 3.1415926535897;   using namespace std;   int main() {     string s;     cin >> s;     int counter = 0;     for (int i = 0; i < s.length(); i++) {       if (s[i] ==  a ) counter++;     }     if (s.length() / 2 < counter)       cout << s.length();     else       cout << (counter - 1) * 2 + 1;   }


/*#pragma GCC optimize( O3 )   #pragma GCC target( avx,avx2,fma )   #pragma GCC optimize( unroll-loops )*/   #include<bits/stdc++.h>   using namespace std;      #define  CIN            ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)   #define  ll             long long int   #define  pb             push_back   #define  ull            unsigned long long int   #define  pii            pair < ll, ll>   #define  pll            pair < ll, pii>   #define  ff             first   #define  ss             second   #define  mx4            1000005   #define  MOD            1000000007   #define  mod2           1000000009   #define  min3(a,b,c)    min(a,min(b,c))   #define  min4(a,b,c,d)  min(d,min(a,min(b,c)))   #define  max3(a,b,c)    max(a,max(b,c))   #define  max4(a,b,c,d)  max(d,max(a,max(b,c)))   #define  ms(a,b)        memset(a,b,sizeof(a))   #define  mpp            make_pair   #define  gcd(a, b)      __gcd(a,b)   #define  lcm(a, b)      ((a)*(b)/gcd(a,b))   #define  input          freopen( input.txt , rt , stdin)   #define  output         freopen( output.txt , wt , stdout)   #define  PI             2*acos(0.0)   #define  zero(a)        memset(a,0,sizeof a)   #define  all(a)         a.begin(),a.end()   #define  rall(a)        a.rbegin(),a.rend()   #define  rev(a)         reverse(all(a))   #define  upper(s)       transform(s.begin(),s.end(),s.begin(), ::toupper)   #define  lower(s)       transform(s.begin(),s.end(),s.begin(), ::tolower)   #define  Unique(X)      (X).erase(unique(all(X)),(X).end())   #define  MX             200005   #define  nl               n    #define  yes            cout<< YES <<endl   #define  no             cout<< NO <<endl   #define  fix(x)         cout<<fixed<<setprecision(x)   #define  coutv(v)       for(auto it:v)cout<<it<<space;cout<<nl;   #define  cinv(v)        for(auto &it:v)cin>>it;   #define  Case(x)        cout<< Case  <<x<< :     #define  strtoint(a)    atoi(a.c_str())   #define  dbg(x)         cerr<<#x<<  is  <<x<<  n ;   #define  fix(x)         cout<<fixed<<setprecision(x)   #define  leastonepos(x) __builtin_ffs(x)   #define  leadingoff(x)  __builtin_clz(x)   #define  trailingoff(x) __builtin_ctz(x)   #define  numofone(x)    __builtin_popcount(x)   #define  error(args...) { string _s = #args; replace(_s.begin(), _s.end(),  , ,    ); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }      ///Compute_phi_co_prime   /*void computeTotient(int n){ll phi[n+5];for (int i=1; i<=n; i++)phi[i] = i;for (int p=2; p<=n; p++)   {if (phi[p] == p){phi[p] = p-1;for (int i = 2*p; i<=n; i += p){phi[i] = (phi[i]/p) * (p-1);}}}}*/   ///seieve   /*vector<bool>mark(100000004);vector<int>prm;void sieve(int x){mark[0]=mark[1]=1;   for(int i =4; i<=x; i=i+2)mark[i]=1;for(int i=3; i*i<=x; i+=2)if(!mark[i])   for(int j=i*i; j<=x; j+=2*i)mark[j]=1;prm.push_back(2);for(int i=3; i<=x; i+=2)   if(mark[i]==0)prm.push_back(i);}*/   ///PrimeFactorization   /*vector<int>pf;void primeFact(ll n){for(int i=0; i<prm.size()&&prm[i]*prm[i]<=n; i++)   {if(n%prm[i]==0){while(n%prm[i]==0){n=n/prm[i] ;pf.push_back(prm[i]);}}}if(n>1)pf.push_back(n);}*/   ///power   /*ll pow( ll base , ll val ){ll res = 1 ;for(int i = 1 ; i <= val ; i++ ){res *= base ;}return res ;}*/   ///Fibonacci   /*ll fib[ 100005 ] ;fib[ 1 ] = 1 ;fib[ 2 ] = 2 ;   for( int i = 3 ; i <= 100000 ; i ++ ){ fib[ i ] = ( fib[ i - 1 ] + fib[ i - 2 ] ) % MOD ;}*/   //min_dis(character)   /*ll min_dis(char ch1,char ch2){ll x = abs(ch1-ch2);return min(x,26-x);}*/   ///leap_year   /*inline bool isLeapYear(ll year) { return (year%400==0) || (year%4==0 && year%100!=0); }*/   ///BinToDec   /*ll BinToDec(bitset<64>bs)   {ll sum = 0 ,lim=bs.size();for( int i = 0 ; i <= lim ; i ++ )sum += bs2[ i ] * pow( 2, i ) ;return sum ;}*/   ///Multiplication_With_Mod   /*long long fun(long long a, long long b){long long res = 0;a %= MOD;while (b)   {if (b & 1)res = (res + a) % MOD;a = (2 * a) % MOD;b >>= 1;}return res;}*/   ///power of two   /*ll Pow[65];void power_of_two(){Pow[0]=1;   for(int i = 1 ; i <= 60 ; i++) Pow[i]=Pow[i-1]*2ll;}*/   /* ll com[200005][102];   void combination()   {       com[1][1]=com[0][0]=1;       com[1][0]=1;       for(ll i=2; i<=200004; i++)       {           com[i][0]=1;           for(ll j=1; j<=min(i,100LL); j++)           {               com[i][j]=com[i-1][j]+com[i-1][j-1];               com[i][j]%=MOD;           }       }   }   */   ///---------------Order set-------------------   ///*os.find_by_order(index) os.order_of_key(value)   /*#include <ext/pb_ds/assoc_container.hpp>   #include <ext/pb_ds/tree_policy.hpp>   using namespace __gnu_pbds;   template<typename T> using orderset = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;   orderset<ll>os ;*/   /*ll Os_Bound(ll x , ll UpOrLow)   {if(UpOrLow==2) x++;ll pos = os.order_of_key(x);if (pos == os.size()) return -1;else return *(os.find_by_order(pos));}*/      ///priority_queue<int,vector<int>,greater<int> >pq;   ///string s5 =  abcdefghijklmnopqrstuvwxyz ;   ///string s6 =  ABCDEFGHIJKLMNOPQRSTUVWXYZ ;   ///int days [14] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};   ///string month[14]= { January ,  February ,  March ,  April ,  May ,  June ,  July ,  August ,  September ,  October ,  November ,  December };   ///--------------Graph Moves--------------------------------------   ///const int fx[] = {+1,-1,+0,+0};   ///const int fy[] = {+0,+0,+1,-1};   ///const int fx[] = {+0,+0,+1,-1,-1,+1,-1,+1}; ///King s move   ///const int fy[] = {-1,+1,+0,+0,+1,+1,-1,-1}; ///king s Move   ///const int fx[] = {-2,-2,-1,-1,+1,+1,+2,+2}; ///knight s move   ///const int fy[] = {-1,+1,-2,+2,-2,+2,-1,+1}; ///knight s move   ///---------------------------------------------------------------   void err(istream_iterator<string> it)   {       cerr<<endl;   }   template<typename T, typename... Args>   void err(istream_iterator<string> it, T a, Args... args)   {       cerr << *it <<   =   << a <<   |  ;       err(++it, args...);   }   /*int Set(int N,int pos){return N=N | (1<<pos);}   bool check(int N,int pos){return (bool)(N & (1<<pos));}   int reset(int N,int pos){return N= N & ~(1<<pos);}*/   /*   /*ll bigmod(ll a , ll b , ll m)   {       if(b==0) return 1;       if(b&1) return (a%m * bigmod(a,b-1,m))%m;       ll x = bigmod(a,b/2,m);       return (x*x)%m;   }   ll inverse(ll a , ll b , ll m)   {       return ((a%m)*bigmod(b,m-2,m)%m);   }*/   /*   set<ll>st;   set<ll>::reverse_itertor it ;   for( it = rbegin() ; it!=st.rend() ; it++)   st.erase(next(it).base());   set<ll>::iterator it ;   for( it = begin() ; it!=st.end() ; it++)   st.erase(it);   */   /*   struct data{       ll wt,iq,indx;       bool operator <(const data &other) const{           if (wt==other.wt) return iq>other.iq;           return wt<other.wt;       }   }ara[1005];   */      vector<ll>v1,v2,v3,v4;   ///vector<pii>v5;   string s1, s2, s3;   ///map<ll,ll>mp;   ///map<ll,ll>::iterator it;   ///ll ara[100005];   ///multiset<ll>ms;   ///multiset<ll>::iterator it;   ///set<ll>st;   ///set<ll>::iterator it;   ///R () ;      int main()   {       CIN;       ll  a,b,c,d,e,f,i,j,k,m,n,cnt1=0,cnt2=0,sum1=0,sum2=0,chk1=0,chk2=0,T=1;       cin>> T ;       for( i = 1 ; i<= 32 ; i++)       {           v1.pb(pow(2,i));           //cout<<v1.back()<<endl;       }       while(T--)       {           cin >> a ;           ll ck=0;           if(a%2)           {               no;               continue;           }              ll x = a/2;           ll y = sqrt(x);           if(y*y==x)           {               yes;               continue;           }              ll low = 1 , high = 100000, ans = 0;              while(low<=high)           {               ll mid = (low+high)/2ll;               if(mid*mid*4<=a)               {                   ans=mid;                   low=mid+1;               }               else{                   high=mid-1;               }           }           if(ans*ans*4==a)           {               yes;           }           else{               no;           }             }       return 0 ;   }   ///printf & cout ekshatey use korbo nah


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   array<int, N> a;   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     int n, k;     cin >> n >> k;     for (int i = 0; i < n; i++) cin >> a[i];     long long ans = 0;     long long power = 1;     while (abs(power) < 1e15) {       map<long long, int> m;       m[0] = 1;       long long sum = 0ll;       for (int i = 0; i < n; i++) {         sum += a[i];         ans += m[sum - power];         m[sum]++;       }       power *= k;       if (power == 1) break;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve(int n, int k) {     int i;     for (i = 1; i <= 2 * k; ++i)       if (i & 1)         printf( %d %d  , 2 * i - 1, 2 * i);       else         printf( %d %d  , 2 * i, 2 * i - 1);     for (i = 4 * k + 1; i <= 2 * n; ++i) printf( %d  , i);     putchar(  n );   }   int main() {     int n, k;     scanf( %d %d , &n, &k);     solve(n, k);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 8;   const int dxs[] = {2, 1, -1, -2, -2, -1, 1, 2};   const int dys[] = {-1, -2, -2, -1, 1, 2, 2, 1};   char s0[8], s1[8];   bool flds[N][N];   inline void readpos(char s[], int &x, int &y) {     x = s[0] -  a ;     y = s[1] -  1 ;   }   inline void setknight(int x, int y) {     for (int di = 0; di < 8; di++) {       int vx = x + dxs[di], vy = y + dys[di];       if (vx >= 0 && vx < N && vy >= 0 && vy < N) flds[vy][vx] = true;     }   }   int main() {     scanf( %s%s , s0, s1);     int rx, ry, kx, ky;     readpos(s0, rx, ry);     readpos(s1, kx, ky);     flds[ry][rx] = flds[ky][kx] = true;     for (int x = 0; x < N; x++) flds[ry][x] = true;     for (int y = 0; y < N; y++) flds[y][rx] = true;     setknight(kx, ky);     setknight(rx, ry);     int cnt = 0;     for (int y = 0; y < N; y++)       for (int x = 0; x < N; x++)         if (!flds[y][x]) cnt++;     printf( %d n , cnt);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, x, y;     cin >> n >> x >> y;     if ((x == n / 2 || x == n / 2 + 1) && (y == n / 2 || y == n / 2 + 1))       cout <<  NO  << endl;     else       cout <<  YES  << endl;   }


#include <bits/stdc++.h>   const int MAXINT = 2147483640;   const long long MAXLL = 9223372036854775800LL;   const long long MAXN = 1000000;   const double pi = 3.1415926535897932384626433832795;   using namespace std;   double s, v1, v2;   long long n, kol;   long long check(double xx) {     long long sum = 0;     double anstime = 0, vk, s1, s2, tin, s3;     while (1) {       s3 = s - v1 * anstime;       vk = (s3 - (xx - anstime) * v1) / (v2 - v1);       anstime += vk;       sum += kol;       if (sum >= n) break;       s2 = vk * v2 + (anstime - vk) * v1;       s1 = anstime * v1;       tin = (s2 - s1) / (v1 + v2);       anstime += tin;       if (anstime > xx) return 0;     }     if (xx - anstime >= 0)       return 1;     else       return 0;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     srand(time(0));     double time, kolend, s1, t1, s2, l, r, c;     cin >> n >> s >> v1 >> v2 >> kol;     l = 0;     r = s / v1 + 1;     while (r - l > 1e-6) {       c = (l + r) / 2;       if (check(c))         r = c;       else         l = c;     }     cout << fixed << setprecision(7) << l << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, ans;     scanf( %lld , &n);     if (n % 2 == 0) {       ans = n / 2;     }     if (n % 2 == 1) {       ans = (n - 1) / 2 - n;     }     printf( %lld n , ans);     return 0;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, d, a[1000];   int main() {     cin >> n >> d;     for (int i = 0; i < n; i++) {       int s;       cin >> s;       a[s]++;     }     int maxn = 0;     for (int i = 1; i <= 100; i++) {       int l = 0;       for (int j = i; j <= i + d; j++) {         l += a[j];       }       if (l > maxn) {         maxn = l;       }     }     cout << n - maxn << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int s1[105], s2[105];   int a[105], b[105];   int tt[305];   int main() {     int n, T;     scanf( %d%d , &n, &T);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       b[i] = a[i];     }     sort(b + 1, b + 1 + n);     for (int i = 1; i <= n; i++) tt[b[i]] = i;     memset(s1, 0, sizeof s1);     memset(s2, 0, sizeof s2);     int boardl = min(T / 2, 100);     int boardr = min(T - boardl, 100);     for (int i = 1; i <= boardl; i++)       for (int j = 1; j <= n; j++) {         int nowm = 0;         for (int k = 1; k <= tt[a[j]]; k++) nowm = max(nowm, s1[k]);         s1[tt[a[j]]] = nowm + 1;       }     for (int i = 1; i <= boardr; i++)       for (int j = n; j; j--) {         int nowm = 0;         for (int k = n; k >= tt[a[j]]; k--) nowm = max(nowm, s2[k]);         s2[tt[a[j]]] = nowm + 1;       }     int ans = 0;     for (int i = 1; i <= n; i++) {       int ans1 = 0, ans2 = 0;       if (tt[b[i]] != tt[b[i - 1]]) {         for (int j = 1; j <= tt[b[i]]; j++) ans1 = max(ans1, s1[j]);         for (int j = tt[b[i]]; j <= n; j++) ans2 = max(ans2, s2[j]);         ans = max(             ans, ans1 + ans2 + (T - boardl - boardr) * (tt[b[i]] - tt[b[i - 1]]));       }     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 8;   int a[N], b[N];   int n, m;   long long x, k, y;   bool chk(int l, int r, long long &res) {     if (l > r) return true;     bool canDel = false;     int mx = *max_element(a + l, a + r + 1);     int len = r - l + 1;     if (l - 1 >= 0 && a[l - 1] > mx) canDel = true;     if (r + 1 < n && a[r + 1] > mx) canDel = true;     if (len < k && !canDel) return false;     int need = len % k;     res += need * y;     len -= need;     if (y * k >= x) {       res += len / k * x;     } else if (canDel) {       res += len * y;     } else {       res += (len - k) * y + x;     }     return true;   }   int main() {     cin >> n >> m >> x >> k >> y;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < m; i++) cin >> b[i];     long long res = 0;     int lst = -1, posa = 0, posb = 0;     while (posb < m) {       while (posa < n && a[posa] != b[posb]) posa++;       if (posa == n) {         cout <<  -1 ;         return 0;       }       if (!chk(lst + 1, posa - 1, res)) {         cout <<  -1 ;         return 0;       }       lst = posa;       posb++;     }     if (!chk(lst + 1, n - 1, res)) {       cout <<  -1 ;       return 0;     }     cout << res;     return 0;   }


#include <bits/stdc++.h>   int x, k;   int NumsBetween(int a, int b) { return (b / x - (a - 1) / x); }   int main() {     int n;     std::cin >> n >> x >> k;     std::vector<int> array(n, 0);     for (int i = 0; i < n; ++i) {       std::cin >> array[i];     }     std::sort(array.begin(), array.end());     long long answ = 0;     int pointer = 0;     int start = 0;     int finish = 0;     while (pointer < n) {       for (; start < n; start += 1) {         if (NumsBetween(array[pointer], array[start]) >= k) break;       }       for (; finish < n; finish += 1) {         if (NumsBetween(array[pointer], array[finish]) > k) break;       }       answ += finish - start;       pointer += 1;       if (start != n && pointer != n && array[pointer] > array[start])         start = pointer;       if (finish != n && start != n && array[start] > array[finish])         finish = start;     }     std::cout << answ << std::endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline T sqr(const T &x) {     return x * x;   }   inline long long sqr(int x) { return sqr<long long>(x); }   template <class T>   T binpow(const T &a, long long n) {     return n == 0 ? 1 : sqr(binpow(a, n / 2)) * (n % 2 ? a : 1);   }   long long binpow(long long a, long long n, long long modulo) {     return n == 0 ? 1                   : sqr(binpow(a, n / 2, modulo)) % modulo * (n % 2 ? a : 1) %                         modulo;   }   long long gcd(long long a, long long b, long long &x, long long &y) {     if (a == 0) {       x = 0;       y = 1;       return b;     }     long long x1, y1;     long long d = gcd(b % a, a, x1, y1);     x = y1 - (b / a) * x1;     y = x1;     return d;   }   inline long long phi(long long n) {     long long result = n;     for (long long i = 2; i * i <= n; ++i)       if (n % i == 0) {         while (n % i == 0) n /= i;         result -= result / i;       }     if (n > 1) result -= result / n;     return result;   }   inline vector<long long> inverseAll(long long m) {     vector<long long> r(m);     r[1] = 1;     for (int i = 2; i < m; ++i) r[i] = (m - (m / i) * r[m % i] % m) % m;     return r;   }   inline long long gcd(long long a, long long b) { return gcd(a, b, a, b); }   inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }   inline long long llrand() {     const long long lsbToFill = (sizeof(long long) << 3) - 1;     const long long bitsFilledInIteration = __builtin_popcountll(RAND_MAX);     long long number = 0;     for (long long lsbFilled = 0; lsbFilled <= lsbToFill;          lsbFilled += bitsFilledInIteration) {       number ^= (((long long)rand()) << lsbFilled);     }     return number & numeric_limits<long long>::max();   }   inline long long llrand(long long begin, long long end) {     return begin + llrand() % (end - begin);   }   struct Dinic {     struct Edge {       int u, v;       long long cap, flow;       Edge() {}       Edge(int u, int v, long long cap) : u(u), v(v), cap(cap), flow(0) {}     };     int N;     vector<Edge> E;     vector<vector<int>> g;     vector<int> d, pt;     Dinic(int N) : N(N), E(0), g(N), d(N), pt(N) {}     void AddEdge(int u, int v, long long cap) {       if (u != v) {         E.emplace_back(Edge(u, v, cap));         g[u].emplace_back(E.size() - 1);         E.emplace_back(Edge(v, u, 0));         g[v].emplace_back(E.size() - 1);       }     }     bool BFS(int S, int T) {       queue<int> q({S});       fill(d.begin(), d.end(), N + 1);       d[S] = 0;       while (!q.empty()) {         int u = q.front();         q.pop();         if (u == T) break;         for (int k : g[u]) {           Edge &e = E[k];           if (e.flow < e.cap && d[e.v] > d[e.u] + 1) {             d[e.v] = d[e.u] + 1;             q.emplace(e.v);           }         }       }       return d[T] != N + 1;     }     long long DFS(int u, int T, long long flow = -1) {       if (u == T || flow == 0) return flow;       for (int &i = pt[u]; i < g[u].size(); ++i) {         Edge &e = E[g[u][i]];         Edge &oe = E[g[u][i] ^ 1];         if (d[e.v] == d[e.u] + 1) {           long long amt = e.cap - e.flow;           if (flow != -1 && amt > flow) amt = flow;           if (long long pushed = DFS(e.v, T, amt)) {             e.flow += pushed;             oe.flow -= pushed;             return pushed;           }         }       }       return 0;     }     long long MaxFlow(int S, int T) {       long long total = 0;       while (BFS(S, T)) {         fill(pt.begin(), pt.end(), 0);         while (long long flow = DFS(S, T)) total += flow;       }       return total;     }   };   vector<long long> Dijkstra(const vector<list<pair<int, long long>>> &g, int s) {     vector<long long> d(int((g).size()), numeric_limits<long long>::max() / 2LL);     priority_queue<pair<long long, int>> q;     d[s] = 0;     q.emplace(-0, s);     while (!q.empty()) {       while (q.top().first > d[q.top().second]) {         q.pop();       }       int v = q.top().second;       q.pop();       for (const auto &cw : g[v]) {         if (d[v] + cw.second < d[cw.first]) {           d[cw.first] = d[v] + cw.second;           q.emplace(-d[cw.first], cw.first);         }       }     }     return d;   }   struct BinarySearchIterator       : public std::iterator<std::forward_iterator_tag, bool> {     long long value;     typename iterator_traits<BinarySearchIterator>::difference_type operator-(         const BinarySearchIterator &it) const {       return value - it.value;     }     BinarySearchIterator &operator++() {       ++value;       return *this;     }     bool operator!=(const BinarySearchIterator &it) const {       return value != it.value;     }     bool operator*() const { return true; }   };   template <int ALPHA>   class AhoCorasick {    public:     static const int ILLEGAL_INDEX;     static const int ROOT;     struct Node {       bool leaf;       int parent;       int parentCharacter;       int link;       int next[ALPHA];       int go[ALPHA];       int outputFunction;       Node(int parent = ILLEGAL_INDEX, int parentCharacter = ALPHA)           : leaf(false),             parent(parent),             parentCharacter(parentCharacter),             link(ILLEGAL_INDEX),             outputFunction(ILLEGAL_INDEX) {         fill_n(next, ALPHA, ILLEGAL_INDEX);         fill_n(go, ALPHA, ILLEGAL_INDEX);       }     };     vector<Node> tree = vector<Node>(1);     AhoCorasick() {}     AhoCorasick(int maxStatesNumber) { tree.reserve(maxStatesNumber); }     template <class Iterator>     void add(int length, const Iterator begin) {       int vertex = ROOT;       for (int i = 0; i < length; ++i) {         if (ILLEGAL_INDEX == tree[vertex].next[begin[i]]) {           tree[vertex].next[begin[i]] = int((tree).size());           tree.push_back(Node(vertex, begin[i]));         }         vertex = tree[vertex].next[begin[i]];       }       tree[vertex].leaf = true;     }     int getLink(int vertex) {       assert(0 <= vertex && vertex < tree.size());       if (ILLEGAL_INDEX == tree[vertex].link) {         if (ROOT == vertex || ROOT == tree[vertex].parent) {           tree[vertex].link = ROOT;         } else {           tree[vertex].link =               go(getLink(tree[vertex].parent), tree[vertex].parentCharacter);         }       }       return tree[vertex].link;     }     int go(int vertex, int character) {       assert(0 <= character && character < ALPHA);       assert(0 <= vertex && vertex < tree.size());       if (ILLEGAL_INDEX == tree[vertex].go[character]) {         if (ILLEGAL_INDEX == tree[vertex].next[character]) {           tree[vertex].go[character] =               ROOT == vertex ? ROOT : go(getLink(vertex), character);         } else {           tree[vertex].go[character] = tree[vertex].next[character];         }       }       return tree[vertex].go[character];     }     int getOutputFunction(int vertex) {       assert(0 <= vertex && vertex < tree.size());       if (ILLEGAL_INDEX == tree[vertex].outputFunction) {         if (tree[vertex].leaf || ROOT == vertex) {           tree[vertex].outputFunction = vertex;         } else {           tree[vertex].outputFunction = getOutputFunction(getLink(vertex));         }       }       return tree[vertex].outputFunction;     }   };   template <int ALPHA>   const int AhoCorasick<ALPHA>::ILLEGAL_INDEX = -1;   template <int ALPHA>   const int AhoCorasick<ALPHA>::ROOT = 0;   int main(int argc, const char *argv[]) {     ios::sync_with_stdio(false);     cin.tie(0);     srand((unsigned int)time(NULL));     int n;     while (cin >> n) {       vector<long long> a(n);       for (int i = (0); i < (n); ++i) {         cin >> a[i];       }       map<long long, long long> valueToPrevious;       set<long long> values;       map<long long, int> valueToPosition;       for (int i = (0); i < (n); ++i) {         auto it = values.lower_bound(a[i]);         if (it == values.begin()) {           valueToPrevious[a[i]] = -1;         } else {           --it;           valueToPrevious[a[i]] = *it;           values.erase(it);         }         values.insert(a[i]);         valueToPosition[a[i]] = i;       }       vector<list<long long>> lists;       set<long long> used;       for (int i = n - 1; i >= 0; --i) {         list<long long> currentList;         if (!used.count(a[i])) {           for (long long current = a[i]; - 1 != current;                current = valueToPrevious[current]) {             currentList.push_front(current);             used.insert(current);           }           lists.push_back(currentList);         }       }       sort((lists).begin(), (lists).end(),            [&](const list<long long> &a, const list<long long> &b) {              return valueToPosition[a.front()] < valueToPosition[b.front()];            });       for (const auto &currentList : lists) {         for (const long long element : currentList) {           cout << element <<    ;         }         cout << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MX = 100000;   vector<int> divisors[MX + 10];   int n, last[MX + 10], pos, X;   int main() {     for (int j = 1; j <= MX; j++)       for (int i = j; i <= MX; i += j) divisors[i].push_back(j);     cin >> n;     for (int j = 1; j <= n; j++) {       cin >> X >> pos;       pos = j - pos;       int sz = divisors[X].size(), ret = 0, div;       for (int i = 0; i < sz; i++) {         div = divisors[X][i];         if (last[div] < pos) ret++;         last[div] = j;       }       cout << ret << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const double EPS = 1e-8;   const long long SZ = 1010, SSZ = 100010, APB = 26, one = 93,                   INF = 0x3f3f3f3f3f3f3f3f, mod = 1000000007;   long long n, m, bg, fn, cnt;   long long to[SSZ], wt[SSZ], head[SZ];   long long nex[SSZ], mk[SSZ];   long long dfn[SZ], low[SZ], tot;   bool vst[SZ];   void add(long long u, long long v, long long w) {     ++cnt;     nex[cnt] = head[u];     head[u] = cnt;     to[cnt] = v, wt[cnt] = w;   }   bool dfs(long long x) {     vst[x] = 1;     if (x == fn) return 1;     for (long long i = head[x]; i != -1; i = nex[i]) {       long long t = to[i];       if (!vst[t]) {         mk[i] = 1;         if (dfs(t)) return 1;         mk[i] = 0;       }     }     return 0;   }   stack<long long> stk;   void tar(long long x, long long p, long long &res2, long long &eg2) {     dfn[x] = low[x] = ++tot;     vst[x] = 1;     for (long long i = head[x]; i != -1; i = nex[i]) {       if (i != (p ^ 1) && mk[i] != 2 && mk[i ^ 1] != 2) {         long long t = to[i], w = wt[i];         if (!dfn[t]) {           tar(t, i, res2, eg2);           low[x] = min(low[x], low[t]);         } else           low[x] = min(low[x], low[t]);         if (dfn[x] < low[t] && dfn[fn] >= dfn[t]) {           if (w < res2) {             res2 = w, eg2 = i;           }         }       }     }   }   void init() {     cin >> n >> m >> bg >> fn;     cnt = -1;     memset(head, -1, sizeof(head));     for (long long i = 1; i <= m; ++i) {       long long a, b, c;       cin >> a >> b >> c;       add(a, b, c);       add(b, a, c);     }     dfs(bg);     if (vst[fn] == 0) {       cout << 0 << endl << 0;       return;     }     long long res = INF, vt1 = -1, vt2;     for (long long i = 0; i <= cnt; ++i) {       if (mk[i]) {         mk[i] = 2;         memset(vst, 0, sizeof(vst));         tot = 0;         memset(dfn, 0, sizeof(dfn));         memset(low, 0, sizeof(low));         long long res1 = wt[i], res2 = INF, eg1 = i, eg2;         tar(bg, -1, res2, eg2);         if (!vst[fn]) res2 = 0, eg2 = -5;         if (res1 + res2 < res) {           res = res1 + res2;           vt1 = eg1 / 2 + 1, vt2 = eg2 / 2 + 1;         }         mk[i] = 1;       }     }     if (vt1 == -1) {       cout << -1 << endl;       return;     }     cout << res << endl;     cout << (vt2 == -1 ? 1 : 2) << endl;     cout << vt1 <<    ;     if (vt2 != -1) cout << vt2 << endl;   }   void work() {}   int main() {     std::ios::sync_with_stdio(0);     long long casenum;     {       init();       work();     }     return 0;   }


#include <bits/stdc++.h>   struct node {     int h, typ;     bool operator<(node b) const {       if (typ == b.typ) return h > b.h;       return typ < b.typ;     }   } a[10010];   int n, x, y, sum, L, R, ans, dp[10010];   int main() {     scanf( %d%d%d , &n, &x, &y);     for (int i = 1; i <= n; i++) scanf( %d , &a[i].h), sum += a[i].h;     for (int i = 1; i <= n; i++) scanf( %d , &a[i].typ);     L = sum - y, R = sum - x;     std::sort(a + 1, a + n + 1);     memset(dp, 128, sizeof(dp));     dp[0] = 0;     for (int i = 1; i <= n; i++)       for (int j = sum; j >= a[i].h; j--) {         int res = dp[j - a[i].h];         if (L <= j && R >= j) res += a[i].typ;         dp[j] = std::max(dp[j], res);       }     for (int i = 0; i <= sum; i++) ans = std::max(ans, dp[i]);     printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     long long data[n + 1];     long long mx = LLONG_MIN;     for (int i = 1; i <= n; i++) {       cin >> data[i];       mx = max(mx, data[i]);     }     vector<int> graph[n + 1];     for (int i = 0; i < n - 1; i++) {       int s, t;       cin >> s >> t;       graph[s].push_back(t);       graph[t].push_back(s);     }     int mxi = 0;     long long mxcnt = 0;     for (int i = 1; i <= n; i++) {       if (data[i] == mx) {         mxi = i;         mxcnt++;       }     }     long long res = mx;     if (mxcnt == 1) {       int level[n + 1];       fill_n(level, n + 1, -1);       deque<int> queue;       queue.push_back(mxi);       level[mxi] = 0;       while (!queue.empty()) {         int cur = queue.front();         res = max(res, data[cur] + level[cur]);         queue.pop_front();         for (int next : graph[cur]) {           if (level[next] < 0) {             level[next] = min(2, level[cur] + 1);             queue.push_back(next);           }         }       }     } else {       bool level1 = false;       for (int i = 1; !level1 && i <= n; i++) {         long long cnt = data[i] == mx ? 1 : 0;         for (int next : graph[i]) {           if (data[next] == mx) cnt++;         }         level1 |= cnt == mxcnt;       }       res = level1 ? mx + 1 : mx + 2;     }     cout << res <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long tree[3 * 100005];   bool lazy[3 * 100005];   void update(long long node, long long b, long long e, long long i, long long j,               long long v) {     if (i > e || j < b) {       return;     }     if (b >= i && e <= j) {       lazy[node] = true;       { tree[node] = tree[node] | v; }       return;     }     if (lazy[node]) {       tree[node * 2] |= tree[node];       tree[node * 2 + 1] |= tree[node];       lazy[node * 2] = 1;       lazy[node * 2 + 1] = 1;       lazy[node] = 0;     }     update(node * 2, b, (b + e) / 2, i, j, v);     update(node * 2 + 1, (b + e) / 2 + 1, e, i, j, v);     tree[node] = tree[node * 2] & tree[node * 2 + 1];   }   long long query(long long node, long long b, long long e, long long i,                   long long j) {     if (i > e || j < b) {       return (1LL << 33) - 1;     }     if (b >= i && e <= j) {       return tree[node];     }     if (lazy[node]) {       tree[node * 2] |= tree[node];       tree[node * 2 + 1] |= tree[node];       lazy[node * 2] = 1;       lazy[node * 2 + 1] = 1;       lazy[node] = 0;     }     return query(node * 2, b, (b + e) / 2, i, j) &            query(node * 2 + 1, (b + e) / 2 + 1, e, i, j);   }   int main() {     long long n, m;     cin >> n >> m;     pair<long long, long long> p[m + 1];     long long a[m + 1];     memset(tree, 0, sizeof tree);     memset(lazy, 0, sizeof lazy);     for (long long i = 1; i <= m; i++) {       long long x, y, z;       cin >> x >> y >> z;       update(1, 1, n, x, y, z);       p[i].first = x;       p[i].second = y;       a[i] = z;     }     int f = 1;     for (long long i = 1; i <= m; i++) {       long long x = query(1, 1, n, p[i].first, p[i].second);       if (x != a[i]) {         f = 0;       }     }     if (f == 1) {       cout <<  YES n ;       for (long long i = 1; i <= n; i++) {         cout << query(1, 1, n, i, i) <<    ;       }       cout << endl;     } else {       cout <<  NO n ;     }   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast,no-stack-protector,unroll-loops )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )   using namespace std;   const long long mod = 1e9 + 7;   long long sod(long long n) {     long long sum = 0;     while (n > 0) {       sum += n % 10;       n /= 10;     }     return sum;   }   long long dig(long long n) {     long long cnt = 0;     while (n > 0) {       n /= 10;       cnt++;     }     return cnt;   }   long long divs(long long n) {     long long cnt = 0;     for (long long x = 1; x * x <= n; x++) {       if (n % x == 0) {         cnt++;         if (n / x != x) cnt++;       }     }     return cnt;   }   long long isprime(long long n) {     if (n < 2) return false;     for (long long x = 2; x * x <= n; x++)       if (n % x == 0) return false;     return true;   }   long long power(long long x, unsigned long long y, long long p = mod) {     long long res = 1;     x = x % p;     while (y > 0) {       if (y & 1) res = (res * x) % p;       y = y >> 1;       x = (x * x) % p;     }     return res;   }   const long long inf = 1e18;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long t;     cin >> t;     while (t--) {       long long n;       cin >> n;       string s;       cin >> s;       long long ans = 0;       long long found = 0;       for (long long x = 0; x < n; x++) {         long long pos = x + 1;         long long bck = n - x;         long long cnt = 0;         if (s[x] ==  1 ) {           cnt = 2 * pos;           ans = max(ans, cnt);           long long cnt2 = 2 * bck;           ans = max(ans, cnt2);         }       }       if (ans == 0) ans = n;       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int nodes_size;   long long int edge_size;   class edge {    public:     long long int node;     long long int cost;     edge(long long int i, long long int j) {       node = i;       cost = j;     }   };   class graph {    public:     long long int node;     vector<edge> edges;     long long int parent;     long long int cost;     vector<long long int> children;   } nodes[1005];   long long int visited[1005];   class point {    public:     long long int x;     long long int y;     point() {}     point(long long int i, long long int j) {       x = i;       y = j;     }   };   void fill_array(long long int ar[], long long int value, long long int size) {     for (long long int i = 0; i < size; i++) ar[i] = value;   }   void print_ll_vector(vector<long long int> v) {     for (long long int i = 0; i < v.size(); i++) cout << v[i] <<    ;     cout << endl;   }   void input_graph(bool is_undirected, bool is_cost) {     cin >> nodes_size;     edge_size = nodes_size - 1;     for (long long int i = 0; i < edge_size; i++) {       long long int a, b, c;       cin >> a >> b;       if (is_cost) {         cin >> c;         nodes[a].edges.push_back(edge(b, c));         if (is_undirected) nodes[b].edges.push_back(edge(a, c));       } else {         nodes[a].edges.push_back(edge(b, 1));         if (is_undirected) nodes[b].edges.push_back(edge(a, 1));       }     }   }   vector<pair<long long int, long long int> > destroyed_edges;   long long int ctr = 0, c;   vector<long long int> ans;   void dfs(long long int node, long long int parent) {     visited[node] = 1;     for (long long int i = 0; i < nodes[node].edges.size(); i++) {       if (nodes[node].edges[i].node != parent) {         if (visited[nodes[node].edges[i].node]) {           if (node > nodes[node].edges[i].node) {             if (c > 0 || ctr == 1) {               destroyed_edges.push_back(                   make_pair(node, nodes[node].edges[i].node));               c++;             } else {               c++;               ans.push_back(node);               ans.push_back(nodes[node].edges[i].node);             }           }         } else           dfs(nodes[node].edges[i].node, node);       }     }   }   vector<long long int> new_edges;   int main() {     input_graph(true, false);     fill_array(visited, 0, nodes_size + 1);     for (long long int i = 1; i <= nodes_size; i++) {       if (!visited[i]) {         c = 0;         ctr++;         dfs(i, -1);         if (c == 0 && i != 1) new_edges.push_back(i);       }     }     cout << (ans.size() / 2) + new_edges.size() << endl;     for (long long int i = 0; i < ans.size();) {       cout << ans[i] <<     << ans[i + 1] <<     << ans[i] <<     << 1 << endl;       i = i + 2;     }     for (long long int i = 0; i < new_edges.size(); i++)       cout << destroyed_edges[i].first <<     << destroyed_edges[i].second <<                << destroyed_edges[i].first <<     << new_edges[i] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int e, f;     cin >> e >> f;     cout << e + f << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   pair<long long, long long> h2md_logl[100000][19];   pair<long long, long long> h2md_logr[100000][19];   long long dist_log[100000][19];   long long h[100000];   int max_2h_minus_d(int from, int to, int dir, long long& dist,                      long long& alldist) {     long long curd = 0;     long long dmark = 0;     long long curtree = from;     int curind = from;     int l = 0;     if (dir == 1) {       l = to - from;       if (l < 0) l += n;     } else if (dir == 0) {       l = from - to;       if (l < 0) l += n;     }     l++;     int l2 = 1;     int deg = 0;     while (l > 0) {       if (l % 2 == 1) {         pair<long long, long long> pret;         if (dir == 0)           pret = h2md_logl[curtree][deg];         else           pret = h2md_logr[curtree][deg];         long long dist_bw_marks = 0;         int newtree = curtree;         if (dir == 1) {           newtree += l2;           if (newtree >= n) newtree -= n;         } else {           newtree -= l2;           if (newtree < 0) newtree += n;         }         if (dir == 1)           dist_bw_marks = dist_log[curtree][deg];         else {           dist_bw_marks = dist_log[newtree][deg];         }         int pret_ind = pret.first;         if (pret.second - 2 * h[curind] - dmark > 0) {           dmark = dist_bw_marks + pret.second - 2 * h[pret_ind];           curind = pret_ind;         } else {           dmark += dist_bw_marks;         }         curtree = newtree;         curd += dist_bw_marks;       }       l /= 2;       l2 *= 2;       deg += 1;     }     dist = curd - dmark;     if (dir == 0)       alldist = curd - dist_log[curtree][0];     else {       curtree--;       if (curtree < 0) curtree += n;       alldist = curd - dist_log[curtree][0];     }     return curind;   }   int main() {     int m;     cin >> n >> m;     for (int i = 0; i < n; i++) {       cin >> dist_log[i][0];     }     for (int i = 0; i < n; i++) {       cin >> h[i];       h2md_logl[i][0] = make_pair(i, 2 * h[i]);       h2md_logr[i][0] = make_pair(i, 2 * h[i]);     }     int n2 = 2;     int deg = 1;     while (n2 < 2 * n) {       int half = n2 / 2;       for (int i = 0; i < n; i++) {         int mid = i + half;         int midl = i - half;         if (midl < 0) midl += n;         if (mid >= n) mid -= n;         long long disttohalf = dist_log[i][deg - 1];         long long disttohalfl = dist_log[midl][deg - 1];         dist_log[i][deg] = disttohalf + dist_log[mid][deg - 1];         if (h2md_logr[i][deg - 1].second >             h2md_logr[mid][deg - 1].second - disttohalf) {           h2md_logr[i][deg] = h2md_logr[i][deg - 1];         } else {           h2md_logr[i][deg] =               make_pair(h2md_logr[mid][deg - 1].first,                         h2md_logr[mid][deg - 1].second - disttohalf);         }         if (h2md_logl[i][deg - 1].second >             h2md_logl[midl][deg - 1].second - disttohalfl) {           h2md_logl[i][deg] = h2md_logl[i][deg - 1];         } else {           h2md_logl[i][deg] =               make_pair(h2md_logl[midl][deg - 1].first,                         h2md_logl[midl][deg - 1].second - disttohalfl);         }       }       deg++;       n2 *= 2;     }     long long distl, distr, alldist;     for (int i = 0; i < m; i++) {       int a, b;       cin >> a >> b;       int f, t;       f = b;       if (f >= n) f -= n;       t = a - 2;       if (t < 0) t += n;       int indf = max_2h_minus_d(f, t, 1, distl, alldist);       int indt = max_2h_minus_d(t, f, 0, distr, alldist);       if (indf != indt) {         cout << alldist + 2 * (h[indf] + h[indt]) - distl - distr;       } else if (indf == f) {         int newindt = indt + 1;         if (newindt == n) newindt = 0;         long long ndr, nar;         int indtt = max_2h_minus_d(t, newindt, 0, ndr, nar);         cout << alldist + 2 * (h[indf] + h[indtt]) - distl - ndr;       } else if (indt == t) {         int newindf = indf - 1;         if (newindf < 0) newindf = n - 1;         long long ndl, nal;         int indff = max_2h_minus_d(f, newindf, 1, ndl, nal);         cout << alldist + 2 * (h[indff] + h[indt]) - ndl - distr;       } else {         int newindf = indf - 1;         int newindt = indt + 1;         if (newindf < 0) newindf = n - 1;         if (newindt == n) newindt = 0;         long long ndl, ndr;         long long nal, nar;         int indff = max_2h_minus_d(f, newindf, 1, ndl, nal);         int indtt = max_2h_minus_d(t, newindt, 0, ndr, nar);         cout << max(alldist + 2 * (h[indff] + h[indt]) - ndl - distr,                     alldist + 2 * (h[indf] + h[indtt]) - distl - ndr);       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long inf = 1e18;   int main(void) {     ios_base::sync_with_stdio(false);     int N;     cin >> N;     long long minx = inf, maxx = -inf, miny = inf, maxy = -inf;     vector<pair<long long, long long>> A(N);     for (int i = 0; i < N; ++i) {       cin >> A[i].first >> A[i].second;       minx = min(minx, A[i].first);       miny = min(miny, A[i].second);       maxx = max(maxx, A[i].first);       maxy = max(maxy, A[i].second);     }     long long ans4 = 2 * (maxx - minx) + 2 * (maxy - miny);     long long ans3 = 0;     for (int s = 0; s < (1 << 6); ++s) {       vector<int> cntx(3, 0), cnty(3, 0);       for (int i = 0; i < 3; ++i) {         if (s & (1 << i)) {           cntx[i]++;           cntx[(i + 1) % 3]--;         } else {           cntx[i]--;           cntx[(i + 1) % 3]++;         }         if (s & (1 << (3 + i))) {           cnty[i]++;           cnty[(i + 1) % 3]--;         } else {           cnty[i]--;           cnty[(i + 1) % 3]++;         }       }       long long perim = 0;       for (int i = 0; i < 3; ++i) {         long long best = -1e18;         for (auto& p : A) {           best = max(best, p.first * cntx[i] + p.second * cnty[i]);         }         perim += best;       }       ans3 = max(ans3, perim);     }     cout << ans3;     for (int i = 4; i <= N; ++i) {       cout <<     << ans4;     }     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int INF = LONG_LONG_MAX;   const long long int NINF = LONG_LONG_MIN;   const long long int mmod = 998244353;   int32_t main() {     ios::sync_with_stdio(false);     cin.tie(0);     long long int t;     cin >> t;     while (t-- > 0) {       long long int c, m, x;       cin >> c >> m >> x;       long long int a = min(c, m);       cout << min(a, (c + m + x) / 3) << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2e5 + 5;   long long mod = 1000000007;   vector<long long> gr[200001];   vector<long long> grev[200001];   long long a[200005];   void solve() {     long long n, d, a, b;     cin >> n >> d >> a >> b;     vector<pair<long long, long long> > ans;     for (long long i = int(0); i < int(n); i++) {       long long x, y;       cin >> x >> y;       long long val = x * a + y * b;       ans.push_back({val, i + 1});     }     sort(ans.begin(), ans.end());     long long val = 0;     vector<long long> ind;     long long i = 0;     for (i = 0; i < n; i++) {       val += ans[i].first;       if (val <= d)         ind.push_back(ans[i].second);       else         break;     }     cout << i <<   n ;     for (long long i = int(0); i < int(ind.size()); i++) {       cout << ind[i] <<    ;     }     cout <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long t;     t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = int(2e5) + 9;   int main() {     int t;     cin >> t;     while (t--) {       string s;       cin >> s;       int n = s.size();       int m1 = 0, m2 = 0;       bool on = false;       int n1 = 0, n0 = 0, N1 = 0, N0 = 0;       for (int i = 0; i < n; i++) {         n0 += (s[i] ==  0 );         n1 += (s[i] ==  1 );       }       int ans = n;       for (int i = 0; i < n; i++) {         N0 += (s[i] ==  0 );         N1 += (s[i] ==  1 );         n0 -= (s[i] ==  0 );         n1 -= (s[i] ==  1 );         ans = min(ans, N0 + n1);         ans = min(ans, N1 + n0);       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e4 + 50;   const int INF = 1e8 + 100;   int val[MAXN], vec[MAXN];   int dp[MAXN][100];   int n, t;   bool check(int x) { return x == ((1 << 3) - 1); }   int solve(int id, int suco) {     if (check(suco)) return 0;     if (id >= n) return (INF);     if (dp[id][suco] != -1) return dp[id][suco];     int ans = INF;     ans = min(solve(id + 1, (suco | vec[id])) + val[id], solve(id + 1, suco));     return dp[id][suco] = ans;   }   int32_t main() {     cin >> n;     string s;     for (int i = 0; i < n; i++) {       int aux = 0;       cin >> t >> s;       for (auto c : s) aux |= (1 << (int(c -  A )));       vec[i] = aux;       val[i] = t;     }     memset(dp, -1, sizeof dp);     int ans = solve(0, 0);     cout << (ans == INF ? -1 : ans) <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s1 =        What are you doing at the end of the world? Are you busy? Will you save          us? ;   string s2 =  What are you doing while sending   ;   string s3 =   ? Are you busy? Will you send   ;   string s4 =   ? ;   long long M[100005];   char f(long long n, long long k) {     if (n == 0) return s1[k];     if (k < s2.size()) return s2[k];     k = k - s2.size();     if (k < M[n - 1]) return f(n - 1, k);     k = k - M[n - 1];     if (k < s3.size()) return s3[k];     k = k - s3.size();     if (k < M[n - 1]) return f(n - 1, k);     k = k - M[n - 1];     if (k < s4.size()) return s4[k];     k = k - s4.size();     return  . ;   }   int main() {     ios_base::sync_with_stdio(0);     long long q;     cin >> q;     M[0] = s1.size();     for (long long i = 1; i <= 53; i++) {       if (M[i - 1])         M[i] = s2.size() + M[i - 1] + s3.size() + M[i - 1] + s4.size();     }     for (long long i = 54; i <= 100001; i++) M[i] = M[53];     for (long long i = 0; i < q; i++) {       long long n, k;       cin >> n >> k;       long long n2 = n;       if (n2 > 53) n2 = 53;       if (k > M[n2])         cout <<  . ;       else         cout << f(n, k - 1);     }   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }   long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); }   int n, arr[109];   vector<int> odd, even;   int main() {     scanf( %d , &n);     for (int i = 0; i < (int)(n); i++) {       scanf( %d , &arr[i]);       if (arr[i] % 2 == 0)         even.push_back(i);       else         odd.push_back(i);     }     if (even.size() == n - 1)       printf( %d n , odd[0] + 1);     else       printf( %d n , even[0] + 1);   }


#include <bits/stdc++.h>   using namespace std;   struct node {     int data;     long int sum;     struct node *left, *right;     struct node* next;   };   struct node* newNode(int data) {     struct node* Node = new (struct node);     Node->data = data;     Node->left = Node->right = Node->next = NULL;     return (Node);   }   long long ans[110][110][2];   void solveTestCase() {     int n1, n2, k1, k2;     scanf( %d , &n1);     ;     scanf( %d , &n2);     ;     scanf( %d , &k1);     ;     scanf( %d , &k2);     ;     k1 = min(k1, n1);     k2 = min(k2, n2);     ans[0][0][0] = 1;     ans[0][0][1] = 1;     for (int i = 0; i < n1 + 1; i++) {       for (int j = 0; j < n2 + 1; j++) {         if (i == 0 && j == 0) {           continue;         }         for (int k = 1; k < k1 + 1; k++) {           if (i - k < 0) {             break;           }           ans[i][j][0] = (ans[i][j][0] + ans[i - k][j][1]) % 100000000;         }         for (int k = 1; k < k2 + 1; k++) {           if (j - k < 0) {             break;           }           ans[i][j][1] = (ans[i][j][1] + ans[i][j - k][0]) % 100000000;         }       }     }     long long ans_final = (ans[n1][n2][0] + ans[n1][n2][1]) % 100000000;     printf( %I64d n , ans_final);     ;   }   int main() {     solveTestCase();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   long long a[100001];   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %I64d , &a[i]);     scanf( %d , &m);     int Left = 1;     for (; m--;) {       int x, y;       scanf( %d%d , &x, &y);       while (Left < x) {         a[Left + 1] = max(a[Left + 1], a[Left]);         ++Left;       }       printf( %I64d n , a[Left]);       a[Left] += y;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200005;   int n, m, lim, p = 1, a[N], nex[N * 2], spot[N * 2], head[N], fa[N], f[N], g[N];   int maxcolor[N][2], c[N];   bool color[N], v[N];   void add(int x, int y) {     nex[++p] = head[x], head[x] = p, spot[p] = y;     nex[++p] = head[y], head[y] = p, spot[p] = x;   }   void dfs1(int x) {     int tp, y, cnt = 0, m1 = 0, m2 = 0;     v[x] = 1;     for (tp = head[x]; y = spot[tp], tp; tp = nex[tp])       if (a[y] >= lim && y != fa[x]) {         fa[y] = x;         dfs1(y);         if (!c[y])           cnt += f[y];         else {           c[x]++;           if (f[y] > m2) m2 = f[y];           if (m1 < m2) swap(m1, m2);         }       }     maxcolor[x][0] = m1;     maxcolor[x][1] = m2;     f[x] = cnt + m1 + 1;     c[x] += color[x];   }   void dfs2(int x, int &ret) {     if (fa[x]) {       if (c[fa[x]] - (c[x] != 0) > 0) {         if (c[x]) {           if (f[x] < maxcolor[fa[x]][0]) {             g[x] = f[x] - maxcolor[x][0] + max(maxcolor[x][0], g[fa[x]]);             if (g[fa[x]] > maxcolor[x][1]) maxcolor[x][1] = g[fa[x]];             if (maxcolor[x][0] < maxcolor[x][1])               swap(maxcolor[x][0], maxcolor[x][1]);           } else {             g[x] = f[x] - maxcolor[x][0] +                    max(maxcolor[x][0],                        g[fa[x]] - maxcolor[fa[x]][0] + maxcolor[fa[x]][1]);             if (g[fa[x]] - maxcolor[fa[x]][0] + maxcolor[fa[x]][1] >                 maxcolor[x][1])               maxcolor[x][1] = g[fa[x]] - maxcolor[fa[x]][0] + maxcolor[fa[x]][1];             if (maxcolor[x][0] < maxcolor[x][1])               swap(maxcolor[x][0], maxcolor[x][1]);           }         } else           g[x] = f[x] - maxcolor[x][0] + max(maxcolor[x][0], g[fa[x]] - f[x]);       } else {         if (c[x]) {           if (f[x] < maxcolor[fa[x]][0])             g[x] = f[x] + g[fa[x]];           else             g[x] = f[x] + g[fa[x]] - maxcolor[fa[x]][0] + maxcolor[fa[x]][1];         } else           g[x] = g[fa[x]];       }       c[x] += ((c[fa[x]] - (c[x] != 0)) != 0);     } else       g[x] = f[x];     ret = max(ret, g[x]);     for (int tp = head[x]; tp; tp = nex[tp])       if (a[spot[tp]] >= lim && spot[tp] != fa[x]) dfs2(spot[tp], ret);   }   int solve() {     memset(v, 0, sizeof(v));     memset(fa, 0, sizeof(fa));     memset(c, 0, sizeof(c));     int maxn = 0;     for (int i = 1; i <= n; i++)       if (a[i] >= lim && v[i] == 0) {         dfs1(i), dfs2(i, maxn);       }     return maxn >= m;   }   int main() {     int i, x, y, l, r, tp;     cin >> n >> m;     for (i = 1; i <= n; i++) scanf( %d , &a[i]);     for (i = 1; i < n; i++) {       scanf( %d%d , &x, &y);       add(x, y);     }     for (l = 1, r = 1000000; l < r;) {       lim = l + r + 1 >> 1;       memset(color, 0, sizeof(color));       for (i = 1; i <= n; i++)         if (a[i] < lim)           for (tp = head[i]; tp; tp = nex[tp]) color[spot[tp]] = 1;       if (solve())         l = lim;       else         r = lim - 1;     }     cout << l;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, b[101][101];   bool isHorrizontal() {     int i, j;     for (i = 0; i < n / 3; i++) {       for (j = 0; j < m; j++) {         if (b[i][j] != 1) return false;       }     }     for (i = n / 3; i < (n - n / 3) / 2 + (n / 3); i++) {       for (j = 0; j < m; j++) {         if (b[i][j] != 2) return false;       }     }     for (i = (n - n / 3) / 2 + (n / 3); i < n; i++) {       for (j = 0; j < m; j++) {         if (b[i][j] != 3) return false;       }     }     return true;   }   bool isVertical() {     int i, j;     for (i = 0; i < n; i++) {       for (j = 0; j < m / 3; j++) {         if (b[i][j] != 1) return false;       }     }     for (i = 0; i < n; i++) {       for (j = m / 3; j < (m - m / 3) / 2 + (m / 3); j++) {         if (b[i][j] != 2) return false;       }     }     for (i = 0; i < n; i++) {       for (j = (m - m / 3) / 2 + (m / 3); j < m; j++) {         if (b[i][j] != 3) return false;       }     }     return true;   }   int main() {     int i, j, c = 0, tmp = 0;     scanf( %d %d , &n, &m);     char a;     map<char, int> mp;     for (i = 0; i < n; i++) {       for (j = 0; j < m; j++) {         scanf(  %c , &a);         if (!mp.count(a)) {           tmp++;           mp[a] = tmp;         }         b[i][j] = mp[a];         if (b[i][j] == 3) c = 1;       }     }     if (c == 1) {       if (n % 3 == 0 || m % 3 == 0) {         if (n % 3 == 0 && isHorrizontal())           puts( YES );         else if (m % 3 == 0 && isVertical())           puts( YES );         else           puts( NO );       } else         puts( NO );     } else       puts( NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using pii = pair<int, int>;   int main() {     ios_base::sync_with_stdio(false);     int t;     cin >> t;     while (t--) {       ll a, b, c;       cin >> a >> b >> c;       ll x, y;       if (a == c and b == 1) {         cout <<  -1 -1  <<   n ;         continue;       }       if (c <= a)         x = -1;       else         x = 1;       if (a * b <= c)         y = -1;       else         y = b;       cout << x <<     << y <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int grid[101][101];   int canDoRowOperation(int r, int c) {     int m = 1000;     for (int j = 0; j < c; j++) m = min(m, grid[r][j]);     return m;   }   int canDoClmOperation(int r, int c) {     int m = 1000;     for (int i = 0; i < r; i++) m = min(m, grid[i][c]);     return m;   }   int main() {     int r, c;     scanf( %d %d , &r, &c);     for (int i = 0; i < r; i++) {       for (int j = 0; j < c; j++) {         scanf( %d , &grid[i][j]);       }     }     vector<string> r_c;     vector<int> vec_num;     if (r <= c) {       for (int i = 0; i < r; i++) {         int m = canDoRowOperation(i, c);         if (m) {           for (int j = 0; j < c; j++) grid[i][j] -= m;         }         for (int k = 0; k < m; k++) {           r_c.push_back( row );           vec_num.push_back(i + 1);         }       }       for (int j = 0; j < c; j++) {         int m = canDoClmOperation(r, j);         if (m) {           for (int i = 0; i < r; i++) grid[i][j] -= m;         }         for (int k = 0; k < m; k++) {           r_c.push_back( col );           vec_num.push_back(j + 1);         }       }     } else {       for (int j = 0; j < c; j++) {         int m = canDoClmOperation(r, j);         if (m) {           for (int i = 0; i < r; i++) grid[i][j] -= m;         }         for (int k = 0; k < m; k++) {           r_c.push_back( col );           vec_num.push_back(j + 1);         }       }       for (int i = 0; i < r; i++) {         int m = canDoRowOperation(i, c);         if (m) {           for (int j = 0; j < c; j++) grid[i][j] -= m;         }         for (int k = 0; k < m; k++) {           r_c.push_back( row );           vec_num.push_back(i + 1);         }       }     }     bool isPossible = true;     for (int i = 0; i < r; i++)       for (int j = 0; j < c; j++)         if (grid[i][j]) isPossible = false;     if (isPossible == false) {       cout << -1 << endl;     } else {       int sz = r_c.size();       cout << sz << endl;       for (int i = 0; i < sz; i++) cout << r_c[i] <<     << vec_num[i] << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 100010;   inline long long read() {     long long p = 0;     long long f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       p = p * 10 + ch -  0 ;       ch = getchar();     }     return p * f;   }   long long a[N];   long long R[N << 1], L[N << 1];   int main() {     long long n = read();     long long m = read();     if (n == 1) {       printf( 0 n );       return 0;     }     for (long long i = 1; i <= m; i++) a[i] = read();     long long ans = 0;     long long l = 1;     long long r = n;     for (long long i = 1; i <= n; i++) R[i] = i;     for (long long i = 1; i <= m; i++) {       long long x = a[i];       l++;       r++;       R[r] = R[r - 1];       R[l + x - 1] = R[l + x - 2];     }     l++;     r++;     R[r] = R[r - 1];     for (long long i = l; i <= r; i++) ans += R[i] + 1;     l = m + 2;     r = n + m + 1;     for (long long i = 1; i <= n; i++) L[i + m + 1] = i;     for (long long i = 1; i <= m; i++) {       long long x = a[i];       l--;       r--;       L[l] = L[l + 1];       L[l + x - 1] = L[l + x];     }     l--;     r--;     L[l] = L[l + 1];     for (long long i = l; i <= r; i++) ans -= L[i];     return printf( %lld n , ans), 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, s;     scanf( %d%d , &n, &s);     int a, max, sum = 0;     scanf( %d , &a);     max = a;     sum += a;     for (int i = 1; i < n; i++) {       scanf( %d , &a);       if (a > max) max = a;       sum += a;     }     sum -= max;     if (sum <= s)       printf( YES );     else       printf( NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int pos[100005];   pair<int, long long> ai[4 * 100005];   void update_ai(int node, int l, int r, int pos, pair<int, long long> val) {     if (l == r) {       ai[node] = val;       return;     }     int mid = (l + r) >> 1;     if (pos <= mid)       update_ai(2 * node, l, mid, pos, val);     else       update_ai(2 * node + 1, mid + 1, r, pos, val);     ai[node].first = ai[2 * node].first + ai[2 * node + 1].first;     ai[node].second = ai[2 * node].second + ai[2 * node + 1].second;   }   void query_ai(int node, int l, int r, int lq, int rq,                 pair<int, long long> &ans) {     if (lq <= l && r <= rq) {       ans.first += ai[node].first;       ans.second += ai[node].second;       return;     }     int mid = (l + r) >> 1;     if (lq <= mid) query_ai(2 * node, l, mid, lq, rq, ans);     if (mid + 1 <= rq) query_ai(2 * node + 1, mid + 1, r, lq, rq, ans);   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) {       int x;       scanf( %d , &x);       pos[x] = i;     }     double sol = 0;     for (int i = 1; i <= n; ++i) {       pair<int, long long> x = make_pair(0, 0);       query_ai(1, 1, n, 1, pos[i], x);       sol += ((double)(n - pos[i] + 1)) / n * (((double)x.second) / (n + 1));       x = make_pair(0, 0);       query_ai(1, 1, n, pos[i], n, x);       sol += x.first -              ((double)pos[i]) / n * (x.first - ((double)x.second) / (n + 1));       update_ai(1, 1, n, pos[i], make_pair(1, pos[i]));     }     printf( %.20f n , sol);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long nx2(long long n) {     long long p = 1;     if (n && !(n & (n - 1))) return n;     while (p < n) p <<= 1;     return p;   }   namespace Debug {   void dout() { cerr <<   n ; }   template <typename Head, typename... Tail>   void dout(Head H, Tail... T) {     cerr <<     << H;     dout(T...);   }   template <typename T>   void douta(const T *a, int n) {     cerr <<   n[ ;     for (int i = 0; i < n; i++) {       cerr <<     << a[i];     }     cerr <<   ] n ;   }   template <typename T>   void doutaa(T **b, int r, int c) {     for (int i = 0; i < r; i++) {       cerr <<  [ ;       for (int j = 0; j < c; j++) {         cerr <<     << b[i][j];       }       cerr <<   ] n ;     }   }   template <typename T>   void dout(const vector<T> v) {     cerr <<   n[ ;     for (T i : v) {       cerr <<     << i;     }     cerr <<   ] n ;   }   template <typename T>   void dout(const vector<vector<T>> v) {     cerr <<   n ;     for (vector<T> u : v) {       cerr <<  [ ;       for (T i : u) {         cerr <<     << i;       }       cerr <<   ] n ;     }   }   template <typename F, typename S>   void dout(const vector<pair<F, S>> u) {     cerr <<   n ;     for (pair<F, S> v : u) {       cerr <<  [   << v.first <<     << v.second <<   ] n ;     }   }   }  // namespace Debug   using namespace Debug;   signed main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long n, m, k;     cin >> n >> m >> k;     vector<vector<vector<long long>>> adj(n);     vector<long long> si(k);     for (long long i = 0; i < m + k; i++) {       if (i < m) {         long long u, v, x;         cin >> u >> v >> x, --u, --v;         adj[u].push_back({v, x, i});         adj[v].push_back({u, x, i});       } else {         long long s, y;         cin >> s >> y, --s;         si[i - m] = s;         adj[0].push_back({s, y, i});         adj[s].push_back({0, y, i});       }     }     vector<long long> vis(n), vis1(n), dist(n, INT_MAX);     priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,                    greater<pair<long long, long long>>>         pq;     dist[0] = 0;     pq.push({dist[0], 0});     vis[0] = 0;     while (!pq.empty()) {       long long u = pq.top().second;       pq.pop();       if (vis1[u]) {         continue;       }       vis1[u] = 1;       for (auto vwi : adj[u]) {         long long v = vwi[0], w = vwi[1], i = vwi[2];         if (v == 0) {           continue;         }         if (dist[v] > dist[u] + w) {           vis[v] = i;           dist[v] = dist[u] + w;           pq.push({dist[v], v});         } else if (dist[v] == dist[u] + w && vis[v] >= m && i < m) {           vis[v] = i;           pq.push({dist[v], v});         }       }     }     long long res = 0;     for (long long i = 0; i < k; i++) {       res += (vis[si[i]] != i + m);     }     cout << res <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   long long x, y, u = 0, v = 0;   vector<pair<long long, long long> > visited;   void move(char c) {     if (c ==  U )       v++;     else if (c ==  D )       v--;     else if (c ==  L )       u--;     else       u++;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> x >> y;     if (x == 0 && y == 0) {       cout <<  Yes ;       return 0;     }     string s;     cin >> s;     for (long long i = 0; i < (long long)s.size(); ++i) {       move(s[i]);       if (u == x && v == y) {         cout <<  Yes ;         return 0;       }       visited.push_back(make_pair(u, v));     }     if (u == 0 && v == 0) {       cout <<  No ;       return 0;     }     long long h_shift = u, v_shift = v;     for (long long i = 0; i < (long long)s.size(); ++i) {       move(s[i]);       if (u == x && v == y) {         cout <<  Yes ;         return 0;       }       long long u_ref = x - visited[i].first;       long long v_ref = y - visited[i].second;       long long p = (h_shift ? u_ref % h_shift : u_ref);       long long q = (v_shift ? v_ref % v_shift : v_ref);       if (((u_ref < 0 && h_shift > 0) || (u_ref > 0 && h_shift < 0)) ||           ((v_ref < 0 && v_shift > 0) || (v_ref > 0 && v_shift < 0)))         continue;       if (p == 0 && q == 0 && (u_ref * v_shift == v_ref * h_shift)) {         cout <<  Yes ;         return 0;       }     }     cout <<  No ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct pt {     double x, y;     pt(const double &_x = 0.0, const double &_y = 0.0) : x(_x), y(_y) {}   };   pt operator+(const pt &a, const pt &b) { return pt(a.x + b.x, a.y + b.y); }   int n, m, id[102][5];   pt verts[500], radius, pent[2][5], centre;   const double PI = 3.141592653589793L;   const double D2R = PI / 180.0L;   inline int vertid(const int &i, const int &j) { return id[i][j] + 1; }   inline int addvert(const pt &p) {     verts[m] = p;     return m++;   }   int main() {     scanf( %d , &n);     double radius = 10.0L * sinl(54.0L * D2R) / sinl(72.0L * D2R);     for (int i = 0; i < (5); ++i) {       double theta = i * 2.0L * 3.141592653589793L / 5.0L;       pent[0][i].x = radius * cosl(theta);       pent[0][i].y = radius * sinl(theta);       pent[1][i].x = -pent[0][i].x;       pent[1][i].y = pent[0][i].y;     }     memset(id, -1, sizeof id);     for (int i = 0; i < (n); ++i) {       bool odd = i & 1;       if (i) centre = centre + pent[!odd][0] + pent[!odd][1];       for (int j = 0; j < (5); ++j)         if (id[i][j] == -1) {           id[i][j] = addvert(centre + pent[odd][j]);           if (j == 0) id[i + 1][4] = id[i][j];           if (j == 1) id[i + 1][0] = id[i + 2][4] = id[i][j];         }     }     printf( %d n , m);     for (int i = 0; i < (m); ++i)       printf( %.15lf %.15lf n , verts[i].x, verts[i].y);     for (int i = 0; i < (n); ++i)       for (int j = 0; j < (5); ++j)         printf( %d%c , vertid(i, j), (j < 4) ?     :   n );     printf( %d , vertid(0, 4));     for (int i = 0; i < (n); ++i)       printf(  %d %d %d , vertid(i, 1), vertid(i, 3), vertid(i, 0));     for (int i = (n)-1; i >= 0; --i) printf(  %d %d , vertid(i, 2), vertid(i, 4));     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     string s;     cin >> s;     vector<int> v[30];     for (int i = 0; i < n; i++) {       v[s[i] - 96].push_back(i);     }     int m;     cin >> m;     while (m--) {       string a;       cin >> a;       int h[30] = {0};       for (int i = 0; i < a.size(); i++) {         h[a[i] - 96]++;       }       int ma = -1;       for (int i = 1; i <= 26; i++) {         if (h[i] != 0) {           ma = max(ma, v[i][h[i] - 1]);         }       }       cout << ma + 1 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main(int argc, char const *argv[]) {     string s, t;     cin >> s >> t;     int count = 0;     int i = s.length() - 1;     int j = t.length() - 1;     while (s[i] == t[j] && i >= 0 && j >= 0) {       i -= 1;       j -= 1;       count += 1;     }     cout << (s.length() + t.length() - 2 * count);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[1010];   int main() {     int n, i;     cin >> n;     for (i = 0; i < n; i++) cin >> a[i];     sort(a, a + n);     int ans = 0;     int x;     for (int i = 0; i < n; i++) {       x = 1;       if (a[i] >= 0) {         ans++;         for (int j = i + 1; j < n; j++) {           if (a[j] >= x) {             x++;             a[j] = -1;           }         }       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 998244353;   long long tmp[1000005];   long long n;   void get_tmp() {     tmp[1] = -1;     for (int i = 1; i <= n; i++) tmp[i + 1] = (tmp[i] + 1) * i % mod;   }   long long a[1000005];   int main() {     cin >> n;     get_tmp();     a[1] = 1;     for (int i = 1; i <= n; i++) a[i + 1] = (a[i] * (i + 1) % mod + tmp[i]) % mod;     cout << a[n] * n % mod << endl;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast ,  unroll-loops )   using namespace std;   const int max_n = 100011, inf = 1000111222;   const int mod = 1000000007;   int n, m, fenv[333][333];   map<int, vector<pair<int, int>>> mp;   pair<int, int> a[max_n];   int sum(int tt[], int r) {     int result = 0;     for (; r >= 0; r = (r & (r + 1)) - 1) result += tt[r];     return result;   }   void inc(int t[], int i, int delta) {     for (; i < m; i = (i | (i + 1))) t[i] += delta;   }   bool cmp(pair<int, int> a, pair<int, int> b) {     if (a.second == b.second) {       return a.first > b.first;     }     return a.second < b.second;   }   int main() {     int t;     cin >> t;     while (t--) {       cin >> n >> m;       mp.clear();       for (int i = 0; i < n; ++i) {         for (int q = 0; q < m; ++q) {           fenv[i][q] = 0;         }       }       for (int i = 0; i < n * m; ++i) {         cin >> a[i].first;         a[i].second = i;       }       sort(a, a + n * m);       int pos = 0;       for (int i = 0; i < n * m; ++i) {         mp[a[i].first].push_back({pos % m, pos / m});         ++pos;         swap(a[i].first, a[i].second);       }       for (auto& a : mp) {         sort(a.second.begin(), a.second.end(), cmp);         reverse(a.second.begin(), a.second.end());       }       sort(a, a + n * m);       int ans = 0;       for (int i = 0; i < n * m; ++i) {         int x = mp[a[i].second].back().second;         int y = mp[a[i].second].back().first;         mp[a[i].second].pop_back();         ans += sum(fenv[x], y);         inc(fenv[x], y, 1);       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int64_t inf = (int64_t)1e18;   vector<int64_t> a[2], g[2];   void dfs(int obj, int idx, int64_t cur) {     if (idx == a[obj].size()) {       g[obj].push_back(cur);       return;     }     dfs(obj, idx + 1, cur);     while (a[obj][idx] <= inf / cur) cur *= a[obj][idx], dfs(obj, idx + 1, cur);   }   int order(int64_t x) {     int res = 0;     int i = g[0].size() - 1, j = 0;     for (; i >= 0; i--) {       for (; j < g[1].size() && g[1][j] <= x / g[0][i]; j++)         ;       res += j;     }     return res;   }   int main(void) {     int n, k;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       int p;       scanf( %d , &p);       a[i & 1].push_back(p);     }     scanf( %d , &k);     dfs(0, 0, 1LL);     sort(g[0].begin(), g[0].end());     dfs(1, 0, 1LL);     sort(g[1].begin(), g[1].end());     int64_t low = 0LL;     int64_t high = inf;     while (low <= high) {       int64_t mid = (low + high) / 2;       int ord = order(mid);       if (ord < k)         low = mid + 1;       else         high = mid - 1;     }     printf( %I64d n , low);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<int, int> > ans;   pair<int, int> ar1[2100];   int ar2[2100];   int n, x;   long long anss = 0;   void findPlace(int i);   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> x;       ar1[i].first = x;     }     for (int i = 1; i <= n; i++) {       cin >> ar2[i];       for (int j = 1; j <= n; j++)         if (ar1[j].first == ar2[i]) {           ar1[j].second = i;           break;         }     }     for (int i = 1; i <= n; i++)       while (ar1[i].first != ar2[i]) findPlace(i);     cout << anss << endl;     cout << ans.size() << endl;     for (vector<pair<int, int> >::iterator i = ans.begin(); i != ans.end(); i++)       printf( %d %d n , (*i).first, (*i).second);     cin >> n;     return 0;   }   void findPlace(int pos) {     int dest = ar1[pos].second, curPos = pos;     for (int i = pos + 1; i <= dest; i++) {       if (ar1[i].second <= curPos) {         swap(ar1[i], ar1[curPos]);         ans.push_back(make_pair(i, curPos));         anss += (i - curPos);         curPos = i;       } else if (ar1[i].second > i) {         findPlace(i);         i--;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     scanf( %d , &n);     char a[n][n];     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++) a[i][j] =  * ;     int begin, end;     begin = end = n / 2;     for (int i = 0; i <= n / 2; i++) {       for (int j = begin; j <= end; j++) a[i][j] =  D ;       begin--;       end++;     }     begin += 2;     end -= 2;     for (int i = n / 2 + 1; i < n; i++) {       for (int j = begin; j <= end; j++) a[i][j] =  D ;       begin++;       end--;     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) printf( %c , a[i][j]);       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, c = 0, x;     int max, min;     cin >> n >> m >> min >> max;     for (int i = 0; i < m; i++) {       cin >> x;       if (x > min && x < max) c++;       if (x < min || x > max) return cout <<  Incorrect , 0;     }     if (c + 2 <= n)       cout <<  Correct ;     else       cout <<  Incorrect ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int ans, ansIndex, n, s, x, val, res = 2000000006;   int main() {     scanf( %d%d%d , &n, &s, &x);     int nxt = s, m = min(800, n);     for (int i = 0; i < m; i++) {       printf( ? %d n , (rand() * 107 + rand()) % n + 1);       fflush(stdout);       scanf( %d %d , &val, &nxt);       if (val < x && val > ans) {         ans = val;         ansIndex = nxt;       }     }     nxt = ansIndex == 0 ? s : ansIndex, val = -1;     while (nxt != -1 && val < x) {       printf( ? %d n , nxt);       fflush(stdout);       scanf( %d %d , &val, &nxt);       if (val >= x) res = min(res, val);     }     printf( ! %d n , 2000000006 == res ? -1 : res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool life[100005];   int num[100005];   int pre[100005];   int main() {     int now = 0, temp = 0;     int n;     long long ans = 0;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &num[i]);       pre[i] = now;       now = i;       while (num[i] + 1 > temp) {         temp++;         life[now] = 1;         now = pre[now];       }     }     temp = 0;     for (int i = 1; i <= n; i++) {       if (life[i]) temp++;       ans += temp - 1 - num[i];     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool func(char a, char b) {     if (a + 1 == b - 1) return true;     if (a + 1 == b + 1) return true;     if (a - 1 == b - 1) return true;     if (a - 1 == b + 1) return true;     return false;   }   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       string s;       cin >> s;       for (int i = 0, j = s.size() - 1; i < j; i++, j--) {         if (!func(s[i], s[j])) goto label;       }       cout <<  YES  << endl;       continue;     label:       cout <<  NO  << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1000000007;   template <class T>   typename T::value_type arr_sum(const T& v, int n) {     typename T::value_type sum = 0;     for (int i = (0); i < (n); ++i) sum += v[i];     return sum;   }   struct Sync_stdio {     Sync_stdio() {       cin.tie(NULL);       ios_base::sync_with_stdio(false);     }   } _sync_stdio;   int main() {     int n;     cin >> n;     if (n == 1) {       cout << 0;       return 0;     }     list<int> a;     a.push_back(0);     for (int i = (0); i < (n); ++i) {       int x;       cin >> x;       a.push_back(x);     }     a.push_back(0);     auto i = a.begin();     auto j = i;     ++j;     auto k = j;     ++k;     long long res = 0;     while (k != a.end()) {       if (*i >= *j && *j <= *k) {         res += min(*i, *k);         a.erase(j);         j = i;         --i;       } else {         ++i;         ++j;         ++k;       }     }     i = a.begin();     j = i;     ++j;     k = j;     ++k;     a.pop_back();     a.pop_front();     while (a.size() >= 3) {       if (a.front() < a.back()) {         res += a.front();         a.pop_front();       } else {         res += a.back();         a.pop_back();       }     }     cout << res;   }


#include <bits/stdc++.h>   using namespace std;   const int base = 13331;   const int MOD = 1e9 + 7;   const int MOD1 = 998244353;   const int mod = 65536;   const long long INF = 0x3f3f3f3f;   const long long LINF = 0x3f3f3f3f3f3f3f3f;   const int N = 1e6 + 10, M = 20000010;   const int NN = 3e5 + 10;   inline long long read() {     char ch = getchar();     long long f = 1, x = 0;     while (ch >  9  || ch <  0 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * f;   }   int n, m, k, idx = 0;   int c[N], ex[N], ey[N];   bool vis[N];   map<pair<int, int>, int> mp;   vector<int> ve[N];   struct Undo_Dsu {     stack<pair<int, int> > st;     int fa[N], siz[N];     void init() {       while (!st.empty()) st.pop();       for (int i = 1; i <= 2 * n + 1; i++) fa[i] = i, siz[i] = 1;     }     int getrt(int x) { return x == fa[x] ? x : getrt(fa[x]); }     bool merge(int u, int v) {       int fax = getrt(u), fay = getrt(v);       if (fax == fay) return false;       if (siz[fax] > siz[fay]) swap(fax, fay), swap(u, v);       fa[fax] = fay, siz[fay] += siz[fax];       st.push({fax, fay});       return true;     }     void undo() {       pair<int, int> no = st.top();       fa[no.first] = no.first;       siz[no.second] -= siz[no.first];       st.pop();     }   } dsu;   int main() {     n = read(), m = read(), k = read();     dsu.init();     for (int i = 1; i <= n; i++) c[i] = read();     int u, v;     long long cnt = k;     for (int i = 1; i <= m; i++) {       u = read(), v = read();       ex[i] = u, ey[i] = v;       if (c[u] == c[v]) {         int fx = dsu.getrt(u);         int fy = dsu.getrt(v);         if (fx == fy) {           if (!vis[c[u]]) {             vis[c[u]] = 1;             --cnt;           }         } else           dsu.merge(u, v + n), dsu.merge(v, u + n);       }     }     long long ans = cnt * (cnt - 1) / 2;     for (int i = 1; i <= m; i++) {       if (c[ex[i]] != c[ey[i]] && !vis[c[ex[i]]] && !vis[c[ey[i]]]) {         int lc = c[ex[i]], rc = c[ey[i]];         if (lc > rc) swap(lc, rc);         int id = mp[make_pair(lc, rc)];         if (!id) id = mp[make_pair(lc, rc)] = ++idx;         ve[id].push_back(i);       }     }     for (auto in : mp) {       int w = in.second;       int now = dsu.st.size();       for (auto v : ve[w]) {         int uid = ex[v], vid = ey[v];         int fu = dsu.getrt(uid), fv = dsu.getrt(vid);         if (fu == fv) {           --ans;           break;         }         dsu.merge(uid, vid + n);         dsu.merge(vid, uid + n);       }       while (dsu.st.size() > now) dsu.undo();     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int t, sx, sy, ex, ey;   string s;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> t >> sx >> sy >> ex >> ey >> s;     for (int i = 0; i < t; i++) {       if (s[i] ==  S  && sy > ey) {         sy--;       } else if (s[i] ==  N  && sy < ey) {         sy++;       } else if (s[i] ==  E  && sx < ex) {         sx++;       } else if (s[i] ==  W  && sx > ex) {         sx--;       }       if (sx == ex && sy == ey) {         cout << i + 1 <<   n ;         return 0;       }     }     cout << -1 <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   ifstream fin( input.txt );   ofstream fout( output.txt );   int gcd(int a, int b) {     while (a && b) a > b ? a %= b : b %= a;     return a + b;   }   int val(char c) {     if (c >=  0  && c <=  9 )       return (int)c -  0 ;     else       return (int)c -  A  + 10;   }   long long pows(int a, int b) {     long long res = 1;     for (int i = 0; i < b; ++i) {       res *= a;     }     return res;   }   long long logx(long long base, long long num) {     int cnt = 0;     while (num != 1) {       num /= base;       ++cnt;     }     return cnt;   }   long long divisibles(long long a, long long b, long long m) {     if (a % m == 0)       return (b / m) - (a / m) + 1;     else       return (b / m) - (a / m);   }   vector<int> root(100001, 0);   vector<int> size(100001, 0);   int find(int x) {     while (x != root[x]) x = root[x];     return x;   }   bool same(int a, int b) { return find(a) == find(b); }   void unite(int a, int b) {     a = find(a);     b = find(b);     if (size[a] < size[b]) swap(a, b);     size[a] += size[b];     root[b] = a;   }   vector<int> vis(100001, 0);   vector<int> adj[100001];   int main() {     std::ios::sync_with_stdio(false);     set<double> s;     double n, x, y;     cin >> n >> x >> y;     for (int i = 0; i < n; ++i) {       double a, b;       cin >> a >> b;       b = y - b;       a = x - a;       if (!a) {         s.insert(INT_MAX);         continue;       }       s.insert(b / a);     }     cout << s.size() <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200005, P = 1e9 + 7;   int dp[N][3], val[3], n;   int add(int a, int b) { return (a += b) >= P ? a - P : a; }   int qpow(int a, int b) {     int res = 1;     while (b) {       if (b & 1) res = 1ll * res * a % P;       a = 1ll * a * a % P;       b >>= 1;     }     return res;   }   int main() {     string s;     cin >> n >> s;     s =  %  + s;     int qs = count(s.begin(), s.end(),  ? );     const int f3 = qpow(3, qs), inv3 = qpow(3, P - 2);     for (int i = 1; i <= n; ++i) {       if (s[i] ==  a ) {         dp[i][1] = dp[i][2] = 0;         dp[i][0] = 1;         val[0] = add(val[0], 1);       } else if (s[i] ==  b ) {         dp[i][0] = dp[i][2] = 0;         dp[i][1] = val[0];         val[1] = add(val[1], dp[i][1]);       } else if (s[i] ==  c ) {         dp[i][0] = dp[i][1] = 0;         dp[i][2] = val[1];         val[2] = add(val[2], dp[i][2]);       } else {         dp[i][0] = inv3;         dp[i][1] = (long long)inv3 * val[0] % P;         dp[i][2] = (long long)inv3 * val[1] % P;         val[0] = add(val[0], inv3);         val[1] = add(val[1], dp[i][1]);         val[2] = add(val[2], dp[i][2]);       }     }     cout << (long long)val[2] * f3 % P <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5;   int N, A[maxn];   unordered_map<int, int> cnt;   unordered_map<int, int> cnt_popped;   int main() {     bool on[26] = {};     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> N;     string S;     getline(cin, S);     for (auto j = (0); j != (N); ++j) {       getline(cin, S);       for (char ch : S) {         on[ch -  a ] = !on[ch -  a ];       }       int mask = 0;       for (auto i = (0); i != (26); ++i) {         if (on[i]) {           mask |= 1 << i;           on[i] = false;         }       }       A[j] = mask;     }     long long ret = 0;     for (auto k = (0); k != (N); ++k) {       int m = A[k];       ret += cnt[m];       ret += cnt_popped[m];       for (int i = 0, j = 1; i < 26; i++, j <<= 1) {         if (m & j) {           ret += cnt[m ^ j];           cnt_popped[m ^ j]++;         }       }       cnt[m]++;     }     cout << ret << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long getnum() {     char c = getchar();     long long num, sign = 1;     for (; c <  0  || c >  9 ; c = getchar())       if (c ==  - ) sign = -1;     for (num = 0; c >=  0  && c <=  9 ;) {       c -=  0 ;       num = num * 10 + c;       c = getchar();     }     return num * sign;   }   vector<int> V[100003];   double D[100003];   int ch[100003], par[100003];   void dfs(int n) {     ch[n] = 0;     for (int i = 0; i < V[n].size(); i++) {       int x = V[n][i];       dfs(x);       ch[n] += ch[x];     }     double sum = 0.5;     for (int i = 0; i < V[n].size(); i++) {       int x = V[n][i];       D[x] = sum * (ch[n] - ch[x]);     }     ch[n]++;   }   void ddfs(int n) {     D[n] += 1.0 + D[par[n]];     for (int i = 0; i < V[n].size(); i++) ddfs(V[n][i]);   }   int main() {     int n = getnum();     for (int i = 2; i <= n; i++) {       par[i] = getnum();       V[par[i]].push_back(i);     }     dfs(1);     ddfs(1);     for (int i = 1; i <= n; i++) printf( %0.8f  , D[i]);   }


#include<bits/stdc++.h>   using namespace std;      bool possible(int *arr, int n)   {    int s=0;    s = accumulate(arr,arr+n,s);    if(s%2)     return false;    bitset<2000000> b;    b[0] = 1;    for(int i=0;i<n;i++)     b = b|(b<<arr[i]);    return b[s/2];   }      int main()   {    int n;     cin>>n;     int arr[n];     for(int i=0;i<n;i++)     {      cin>>arr[i];     }     if(possible(arr,n))     {      pair<int,int> mn;      mn = make_pair(__builtin_ctz(arr[0]),1);      for(int i=1;i<n;i++)      {       mn = min(make_pair(__builtin_ctz(arr[i]),i+1),mn);      }      cout<<1<<endl<<mn.second<<endl;     }     else     {      cout<<0<<endl;     }    return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s, v;   bool g(char k) {     return (k ==  a  || k ==  e  || k ==  i  || k ==  o  || k ==  u );   }   int main() {     ios_base::sync_with_stdio(0);     cin >> s;     long long tek = 0;     char t1 =  - , t2 =  - ;     for (long long i = 0; i < s.size(); i++) {       if (!g(s[i])) {         if (t1 ==  - ) {           t1 = s[i];           ++tek;           v += s[i];         } else if (t2 ==  - ) {           t2 = s[i];           ++tek;           v += s[i];         } else if (s[i] == t1 && s[i] == t2) {           ++tek;           v += s[i];         } else {           char k = s[i];           v +=    ;           cout << v;           t1 = k;           t2 =  - ;           tek = 1;           v =   ;           v += t1;         }       } else {         t1 =  - ;         t2 =  - ;         tek = 0;         v += s[i];       }     }     cout << v;   }


#include <bits/stdc++.h>   using namespace std;   const int M = 100500;   vector<int> a, b, diff, c;   int cnt[220000];   int cnt1[220000];   int cnt3[220000];   int cntt[220000];   int cntt2[220000];   int cntt3[220000];   int cnt2[220000];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     for (int i = 0; i < n; i++) {       int x;       cin >> x;       cnt[x + M]++;       cntt[x + M]++;       a.push_back(x);       diff.push_back(x + M);     }     sort(a.begin(), a.end());     sort(diff.begin(), diff.end());     diff.resize(unique(diff.begin(), diff.end()) - diff.begin());     if (diff.size() == 1 || (diff.size() == 2 && abs(diff[0] - diff[1]) == 1)) {       cout << n << endl;       for (int i = 0; i < a.size(); i++) cout << a[i] <<    ;       return 0;     } else if (diff.size() == 2 && abs(diff[0] - diff[1]) == 2) {       diff.push_back(a[0] + 1 + M);       sort(diff.begin(), diff.end());     }     while (cnt[diff[0]] > 0 && cnt[diff[2]] > 0) {       cnt[diff[0]]--;       cnt[diff[2]]--;       cnt2[diff[1]] += 2;     }     cnt2[diff[1]] += cntt[diff[1]];     for (int i = 0; i < cnt2[diff[1]]; i++) b.push_back(diff[1] - M);     for (int i = 0; i < cnt[diff[2]]; i++) b.push_back(diff[2] - M);     for (int i = 0; i < cnt[diff[0]]; i++) b.push_back(diff[0] - M);     for (int i = 0; i < b.size(); i++) cntt2[b[i] + M]++;     int ans1 = 0;     for (int i = 0; i < diff.size(); i++)       ans1 += min(cntt2[diff[i]], cntt[diff[i]]);     while (cnt[diff[1]] >= 2) {       cnt1[diff[0]]++;       cnt3[diff[2]]++;       cnt[diff[1]] -= 2;     }     cnt1[diff[0]] += cntt[diff[0]];     cnt3[diff[2]] += cntt[diff[2]];     for (int i = 0; i < cnt1[diff[0]]; i++) c.push_back(diff[0] - M);     for (int i = 0; i < cnt[diff[1]]; i++) c.push_back(diff[1] - M);     for (int i = 0; i < cnt3[diff[2]]; i++) c.push_back(diff[2] - M);     for (int i = 0; i < c.size(); i++) cntt3[c[i] + M]++;     int ans2 = 0;     for (int i = 0; i < diff.size(); i++)       ans2 += min(cntt3[diff[i]], cntt[diff[i]]);     if (ans1 < ans2) {       cout << ans1 << endl;       for (int i = 0; i < b.size(); i++) cout << b[i] <<    ;     } else {       cout << ans2 << endl;       for (int i = 0; i < c.size(); i++) cout << c[i] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct UnionFind {     int n;     vector<int> p, rank;     int findSet(int i) { return (p[i] == i ? i : (p[i] = findSet(p[i]))); }     bool isSameSet(int i, int j) { return (findSet(i) == findSet(j)); }     void unionSet(int i, int j) {       if (!isSameSet(i, j)) {         int x = findSet(i);         int y = findSet(j);         if (rank[x] > rank[y]) {           p[y] = x;         } else {           p[x] = y;         }         if (rank[x] == rank[y]) {           rank[y]++;         }       }     }     UnionFind(int _n) {       n = _n;       for (int i = 0; i < n; i++) {         p.push_back(i);         rank.push_back(0);       }     }   };   int n, m;   vector<int> P;   vector<int> type;   vector<pair<int, int> > query;   vector<pair<int, int> > t3;   vector<int> H;   void calculateHeight() {     vector<int> inDegree(n, 0);     for (int i = 0; i < n; i++) {       if (P[i] != -1) {         inDegree[P[i]] += 1;       }     }     H.resize(n, -1);     queue<int> q;     for (int i = 0; i < n; i++) {       if (inDegree[i] == 0) {         H[i] = 0;         q.push(i);       }     }     while (!q.empty()) {       int u = q.front();       q.pop();       if (P[u] == -1) {         continue;       }       H[P[u]] = max(H[P[u]], H[u] + 1);       inDegree[P[u]] -= 1;       if (inDegree[P[u]] == 0) {         q.push(P[u]);       }     }   }   set<pair<int, int> > isParent;   vector<vector<int> > G;   vector<vector<int> > Q;   vector<int> visited;   void dfs(int u) {     visited[u] = true;     for (int i = 0; i < Q[u].size(); i++) {       int p = Q[u][i];       if (visited[p]) {         isParent.insert(make_pair(p, u));       }     }     for (int i = 0; i < G[u].size(); i++) {       dfs(G[u][i]);     }     visited[u] = false;   }   int main() {     cin >> n >> m;     P.resize(n, -1);     G.resize(n);     Q.resize(n);     visited.resize(n, false);     type.resize(m);     query.resize(m);     for (int i = 0; i < m; i++) {       cin >> type[i];       if (type[i] == 1) {         cin >> query[i].first >> query[i].second;         query[i].first -= 1;         query[i].second -= 1;         P[query[i].first] = query[i].second;         G[query[i].second].push_back(query[i].first);       } else if (type[i] == 2) {         cin >> query[i].first;         query[i].first -= 1;       } else {         cin >> query[i].first >> query[i].second;         query[i].first -= 1;         query[i].second -= 1;         t3.push_back(make_pair(query[i].second, i));       }     }     int t3i = 0;     int doc = 0;     sort(t3.begin(), t3.end());     for (int i = 0; i < m; i++) {       if (type[i] == 2) {         for (; t3i < t3.size() && t3[t3i].first == doc; t3i++) {           int c = query[i].first;           int p = query[t3[t3i].second].first;           Q[c].push_back(p);         }         doc++;       }     }     for (int i = 0; i < n; i++) {       if (P[i] == -1) {         dfs(i);       }     }     UnionFind uf(n);     t3i = 0;     doc = 0;     for (int i = 0; i < m; i++) {       if (type[i] == 1) {         uf.unionSet(query[i].first, query[i].second);       } else if (type[i] == 2) {         for (; t3i < t3.size() && t3[t3i].first == doc; t3i++) {           int c = query[i].first;           int p = query[t3[t3i].second].first;           if (uf.isSameSet(c, p) && isParent.count(make_pair(p, c))) {             query[t3[t3i].second].second = true;           } else {             query[t3[t3i].second].second = false;           }         }         doc++;       }     }     for (int i = 0; i < m; i++) {       if (type[i] == 3) {         if (query[i].second) {           cout <<  YES n ;         } else {           cout <<  NO n ;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   long long f1[120000], f2[120000], f3[120000], f4[120000], ans;   vector<int> E[120000];   int n, a[120000];   void dfs1(int x, int fa) {     f1[x] = f2[x] = a[x];     for (int i : E[x])       if (i != fa) {         dfs1(i, x);         f1[x] = max(f1[i], f1[x]);         f1[x] = max(f1[x], f2[x] + f2[i]);         f2[x] = max(f2[x], f2[i] + a[x]);       }   }   void dfs2(int x, int fa) {     pair<long long, long long> ip[5];     pair<long long, long long> iq[5];     for (int i = (0); i <= (3); ++i) ip[i] = iq[i] = make_pair(-1e18, 0);     for (int i : E[x])       if (i != fa) {         ip[0] = make_pair(f2[i] + a[x], i);         sort(ip, ip + 4);         iq[0] = make_pair(f1[i], i);         sort(iq, iq + 3);       }     ip[0] = make_pair(f4[x], -1);     sort(ip, ip + 4);     for (int i : E[x])       if (i != fa) {         f3[i] = f3[x];         if (i == ip[3].second)           f3[i] = max(f3[i], ip[2].first + ip[1].first - a[x]);         else if (i == ip[2].second)           f3[i] = max(f3[i], ip[1].first + ip[3].first - a[x]);         else           f3[i] = max(f3[i], ip[2].first + ip[3].first - a[x]);         if (i == iq[2].second)           f3[i] = max(f3[i], iq[1].first);         else           f3[i] = max(f3[i], iq[2].first);         if (i == ip[3].second)           f4[i] = ip[2].first + a[i];         else           f4[i] = ip[3].first + a[i];         dfs2(i, x);       }   }   int main() {     scanf( %d , &n);     for (int i = (1); i <= (n); ++i) scanf( %d , &a[i]);     for (int i = (1); i <= (n - 1); ++i) {       int x, y;       scanf( %d%d , &x, &y);       E[x].push_back(y);       E[y].push_back(x);     }     dfs1(1, 0);     f4[1] = a[1];     dfs2(1, 0);     ans = 0;     for (int i = (1); i <= (n); ++i) ans = max(ans, f1[i] + f3[i]);     printf( %I64d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100005;   const int MOD = 1000000007;   int N;   int a[MAXN];   vector<int> ch[MAXN];   int dp[MAXN][2];   inline int add(int x, int y) {     x += y;     if (x >= MOD) x -= MOD;     return x;   }   inline int mul(int x, int y) { return (long long)x * y % MOD; }   void load() {     scanf( %d , &N);     for (int i = 1; i < N; i++) {       int x;       scanf( %d , &x);       ch[x].push_back(i);     }     for (int i = 0; i < N; i++) scanf( %d , a + i);   }   int dfs(int x) {     vector<int> tmp;     for (int i = 0; i < ch[x].size(); i++) tmp.push_back(dfs(ch[x][i]));     int prod = 1, sz = tmp.size();     for (int i = 0; i < sz; i++) prod = mul(prod, tmp[i]);     if (a[x])       dp[x][1] = prod;     else {       dp[x][0] = prod;       vector<int> suff(sz + 1);       suff[sz] = 1;       for (int i = sz - 1; i >= 0; i--) suff[i] = mul(suff[i + 1], tmp[i]);       prod = 1;       for (int i = 0; i < sz; i++) {         dp[x][1] = add(dp[x][1], mul(mul(prod, suff[i + 1]), dp[ch[x][i]][1]));         prod = mul(prod, tmp[i]);       }     }     return add(dp[x][0], dp[x][1]);   }   int solve() {     dfs(0);     return dp[0][1];   }   int main() {     load();     printf( %d n , solve());     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     int t;     cin >> t;     while (t--) {       int n, m;       cin >> n >> m;       string a;       cin >> a;       int arr[n + 5];       set<int> st;       int mark[n + 5];       memset(mark, 0, sizeof mark);       for (int i = 0; i < m; i++) {         int x;         cin >> x;         mark[0]++;         mark[x]--;       }       mark[0]++;       for (int i = 1; i <= n; i++) {         mark[i] += mark[i - 1];       }       int ans[28];       memset(ans, 0, sizeof ans);       for (int i = 0; i < n; i++) {         int x = a[i] -  a ;         ans[x] += mark[i];       }       for (int i = 0; i < 26; i++) {         cout << ans[i] <<    ;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n, m;     cin >> n >> m;     vector<int> v;     vector<bool> b;     vector<int> u;     vector<int> s;     vector<int> t(m, 0);     for (int i = 0; i < n + m; i++) {       long long x;       cin >> x;       v.push_back(x);     }     for (int i = 0; i < n + m; i++) {       long long x;       cin >> x;       b.push_back(x);     }     for (int i = 0; i < n + m; i++) {       if (b[i])         s.push_back(v[i]);       else         u.push_back(v[i]);     }     for (int i = 0; i < u.size(); i++) {       long long x, y;       y = (upper_bound(s.begin(), s.end(), u[i]) - s.begin());       if (y == 0)         t[0]++;       else if (y == m)         t[m - 1]++;       else {         x = y - 1;         if (abs(s[y] - u[i]) >= abs(s[x] - u[i]))           t[x]++;         else           t[y]++;       }     }     for (int i = 0; i < m; i++) cout << t[i] <<    ;     cout << endl;   }