#include <bits/stdc++.h>   using namespace std;   int s[3000005], ps[3000005][5];   int n, ans;   vector<int> temp;   int po(int a, int b) {     if (b == 0) return 1;     return a * po(a, b - 1);   }   void prep() {     int ptr1 = 1, ptr2 = 1, maxi = 0;     ps[1][0] = 1;     for (int i = (2); i <= (3000000); i++) {       while (po(ptr1, 4) < i) ptr1++;       while (po(ptr2 + 1, 2) <= i) ptr2++;       for (int j = (0); j <= (4); j++) ps[i][j] = ps[i - 1][j];       while (ps[ptr2][s[i]] - ps[ptr1 - 1][s[i]] > 0) s[i]++;       ps[i][s[i]]++;     }   }   int f(long long x) {     if (x <= 3000000) return s[x];     int ptr1 = ceil(pow(x, 0.25)), ptr2 = floor(pow(x, 0.5));     int ret = 0;     while (ps[ptr2][ret] - ps[ptr1 - 1][ret] > 0) ret++;     return ret;   }   int main() {     prep();     scanf( %d , &n);     for (int i = (1); i <= (n); i++) {       long long x;       scanf( %lld , &x);       ans ^= f(x);     }     if (ans)       printf( Furlo n );     else       printf( Rublo n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename A, size_t N, typename T>   void Fill(A (&array)[N], const T &val) {     std::fill((T *)array, (T *)(array + N), val);   }   template <class T>   inline bool chmax(T &a, T b) {     if (a < b) {       a = b;       return true;     }     return false;   }   template <class T>   inline bool chmin(T &a, T b) {     if (a > b) {       a = b;       return true;     }     return false;   }   int mi = 1000000007;   map<pair<int, int>, int> mp;   int minima[2000000];   int f(int a, int b) {     if (mp.find(make_pair(a, b)) != mp.end()) {       return mp[make_pair(a, b)];     }     cout <<  ?   << a <<     << b << endl;     int s;     cin >> s;     chmin(mi, s);     return mp[make_pair(a, b)] = s;   }   void SMAWK(vector<int> js, int ib, int ie, int id) {     if (ib > ie) return;     vector<int> js2;     for (int q = 0, i = ib; q < js.size(); ++q) {       while (!js2.empty() && f(i, js2.back()) >= f(i, js[q])) {         js2.pop_back();         i -= id;       }       if (js2.size() != (ie - ib) / id) {         js2.push_back(js[q]);         i += id;       }     }     SMAWK(js2, ib + id, ie, id * 2);     for (int i = ib, q = 0; i <= ie; i += id * 2) {       int jt = (i + id <= ie ? minima[i + id] : js.back());       int fm = 999999999 * 2, fq;       for (; q < js.size(); ++q) {         if (fm > (fq = f(i, js[q]))) {           fm = fq;           minima[i] = js[q];         }         if (js[q] == jt) break;       }     }   }   void rowMinimaTM(int ib, int ie, int jb, int je) {     vector<int> js;     for (int j = jb; j <= je; ++j) js.push_back(j);     SMAWK(js, ib, ie, 1);   }   int main() {     int n, m;     cin >> n >> m;     rowMinimaTM(1, n, 1, m);     cout <<  !   << mi << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int maxn = 2e5 + 5;   long long int sz[maxn], dp[maxn], ans[maxn];   vector<long long int> g[maxn];   long long int n;   long long int dp1(long long int node, long long int parent) {     sz[node] = 1;     for (auto& itr : g[node])       if (itr != parent) sz[node] += dp1(itr, node);     return sz[node];   }   long long int dp2(long long int node, long long int parent) {     dp[node] = sz[node];     for (auto& itr : g[node])       if (itr != parent) dp[node] += dp2(itr, node);     return dp[node];   }   void dp3(long long int node, long long int parent,            long long int fromParent = 0) {     if (parent == -1)       ans[node] = dp[node];     else       ans[node] = n - 2 * sz[node] + ans[parent];     for (auto& itr : g[node])       if (itr != parent) dp3(itr, node);   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> n;     for (long long int i = 1; i < n; i++) {       long long int u, v;       cin >> u >> v;       g[u].push_back(v);       g[v].push_back(u);     }     dp1(1, -1);     dp2(1, -1);     dp3(1, -1);     long long int answer = 0;     for (long long int i = 1; i <= n; i++) answer = max(answer, ans[i]);     cout << answer;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a[16] = {0};     int index = 0;     for (int i = 1; i <= n; i++) {       string s = to_string(i);       bool lucky = true;       for (int j = 0; j < (s.length()); j++) {         if (s[j] !=  4  && s[j] !=  7 ) {           lucky = false;           break;         }       }       if (lucky) {         a[index] = i;         index++;       }     }     for (int i = 0; i < 16; i++) {       if (a[i] != 0) {         if (n % a[i] == 0) {           cout <<  YES ;           return 0;         }       }     }     cout <<  NO ;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     set<long long> ss;     for (int i = 1; i <= 1e6; i++) {       long long need = i * 2;       long long cards = (i * (i + 1)) / 2;       long long lft = cards - i;       need += lft * 3;       if (need > 1e9) break;       ss.insert(-need);     }     while (t--) {       long long n;       cin >> n;       long long cnt = 0;       while (true) {         set<long long>::iterator it = ss.lower_bound(-n);         if (it == ss.end()) break;         cnt++;         n += *it;       }       cout << cnt <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct edge {     int v, nxt, c;   } e[1505 * 1505 * 2];   int p[1505], eid;   void init() {     memset(p, -1, sizeof p);     eid = 0;   }   void insert(int u, int v, int c) {     e[eid].v = v;     e[eid].c = c;     e[eid].nxt = p[u];     p[u] = eid++;   }   void add(int u, int v, int c) {     insert(u, v, c);     insert(v, u, 0);   }   int ch[10000005][2], cnt[10000005], tot, ys[1505], nxt[10000005], g[755][755],       L[1505], R[1505], ha[10000005];   string st;   queue<int> q;   void insert(int id) {     int p = 0, len = st.length();     L[id] = ha[0] + 1;     for (int i = 0; i < len; i++) {       ha[++ha[0]] = st[i] -  a ;       if (!ch[p][st[i] -  a ]) ch[p][st[i] -  a ] = ++tot;       p = ch[p][st[i] -  a ];     }     cnt[p] = id;     R[id] = ha[0];   }   void build() {     for (int i = 0; i < 2; i++)       if (ch[0][i]) q.push(ch[0][i]);     while (q.size()) {       int u = q.front();       q.pop();       for (int i = 0; i < 2; i++) {         if (!ch[u][i])           ch[u][i] = ch[nxt[u]][i];         else           nxt[ch[u][i]] = ch[nxt[u]][i], q.push(ch[u][i]);       }     }   }   int vis[10000005];   void run(int id) {     int p = 0;     vis[0] = 1;     for (int i = L[id]; i <= R[id]; i++) {       p = ch[p][ha[i]];       int pp = p, tt;       while (pp) {         if (cnt[pp] && cnt[pp] != id) {           g[id][cnt[pp]] = 1;           break;         }         pp = nxt[pp];       }       int haha = pp;       pp = p;       while (pp) {         if (cnt[pp] && cnt[pp] != id) {           break;         }         tt = pp;         pp = nxt[pp];         nxt[tt] = haha;       }     }   }   int dis[1505], S, T, n;   int bfs() {     memset(dis, -1, sizeof dis);     dis[S] = 1;     q.push(S);     while (q.size()) {       int u = q.front();       q.pop();       for (int i = p[u]; i + 1; i = e[i].nxt) {         int v = e[i].v;         if (dis[v] == -1 && e[i].c) {           dis[v] = dis[u] + 1;           q.push(v);         }       }     }     return dis[T] != -1;   }   int dfs(int u, int flow) {     if (u == T) return flow;     int ret = 0;     for (int i = p[u]; i + 1; i = e[i].nxt) {       int c = e[i].c, v = e[i].v;       if (c && dis[v] == dis[u] + 1) {         int tmp = dfs(v, min(flow, c));         ret += tmp, flow -= tmp;         e[i].c -= tmp, e[i ^ 1].c += tmp;         if (!flow) break;       }     }     if (!ret) dis[u] = -1;     return ret;   }   int Dinic() {     int ret = 0;     for (; bfs();) ret += dfs(S, 2147483646 / 2);     return ret;   }   int main() {     ios::sync_with_stdio(false);     init();     cin >> n;     T = 2 * n + 1;     for (int i = 1; i <= n; i++) cin >> st, insert(i);     build();     for (int i = 1; i <= n; i++) run(i);     for (int k = 1; k <= n; k++)       for (int i = 1; i <= n; i++)         for (int j = 1; j <= n; j++) g[i][j] |= (g[i][k] & g[k][j]);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++)         if (g[i][j] && i != j) add(i, j + n, 1);     for (int i = 1; i <= n; i++) add(S, i, 1), add(i + n, T, 1), vis[i + n] = 0;     cout << n - Dinic() << endl;     for (int i = 1; i <= n; i++)       if (dis[i] != -1 && dis[i + n] == -1) cout << i <<    ;     return 0;   }


#include <bits/stdc++.h>   const int MAXN = 1e5 + 5;   enum { GRASS =  G , WATER =  W , LAVA =  L  };   enum { G = 5, W = 3, L = 1 };   long long a[MAXN];   char s[MAXN];   int n;   bool water;   long long grass, stamina, t;   int main() {     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       scanf( %lld , &a[i]);     }     scanf(  %s , s);     water = false;     grass = stamina = t = 0LL;     for (int i = 0; i < n; ++i) {       switch (s[i]) {         case GRASS:           grass += 2 * a[i];           stamina += 2 * a[i];           grass = std::min(stamina / 2, grass);           t += G * a[i];           break;         case WATER:           water = true;           stamina += 2 * a[i];           t += W * a[i];           break;         case LAVA:           if (stamina < 2 * a[i]) {             t += ((water) ? W : G) * (a[i] - stamina / 2);             stamina = 0;           } else {             stamina -= 2 * a[i];           }           t += L * a[i];           grass = std::min(stamina / 2, grass);           break;       }     }     t -= (G - L) / 2 * grass;     t -= (W - L) / 2 * (stamina / 2 - grass);     printf( %lld n , t);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   vector<vector<int>> g;   vector<int> w;   vector<int> dp;   void dfs(int node, int p) {     dp[node] = w[node];     for (int u : g[node]) {       if (u != p) {         dfs(u, node);         dp[node] += dp[u];       }     }   }   int main() {     cin >> n;     int u, v, t;     g.assign(n + 1, vector<int>());     w.assign(n + 1, 0);     dp.assign(n + 1, 0);     vector<int> ans;     for (int i = 1; i < n; i++) {       cin >> u >> v >> t;       g[u].push_back(v);       g[v].push_back(u);       if (t == 2) {         w[u] = 1;         w[v] = 1;       }     }     dfs(1, 0);     for (int i = 1; i <= n; i++) {       if (dp[i] == 1 && w[i] == 1) ans.push_back(i);     }     cout << ans.size() << endl;     for (auto u : ans) cout << u <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int a[300010];   long long ans = 0;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n;     for (int i = 1; i <= n; ++i) {       cin >> a[i];     }     sort(a + 1, a + 1 + n);     for (int i = 1; i <= n / 2; ++i) {       long long sum = a[i] + a[n - i + 1];       ans += sum * sum;     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> a;   vector<int> b;   int n;   void get_input() { cin >> n; }   void solve() {     for (int i = 0; i < n; i++) {       int kom;       cin >> kom;       a.push_back(kom);       b.push_back(kom);     }     sort(b.begin(), b.begin() + n);     int cnt = 0;     for (int i = 0; i < n; i++) {       if (b[i] != a[i]) {         cnt++;       }     }     cout << (cnt > 2 ?  NO  :  YES );   }   void output() {}   int main() {     ios ::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int t = 1;     while (t--) {       get_input(), solve(), output();     }     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /stack:200000000 )   #pragma GCC optimize( Ofast )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )   #pragma GCC optimize( unroll-loops )   using namespace std;   const double pi =       3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342;   int main() {     int n, k;     scanf( %d%d , &n, &k);     int v = 2 * k + 1;     if (n <= k + 1)       printf( 1 n1 n );     else if (n % v == 0) {       printf( %d n , n / v);       int ile = 0, pos = k + 1;       while (ile < n) {         printf( %d  , pos);         pos += v;         ile += v;       }       printf(  n );     } else if (n % v > k) {       int r = n % v;       int pos = 1 + r - (k + 1);       int ile = r;       printf( %d n%d  , n / v + 1, pos);       pos = ile + k + 1;       while (ile < n) {         printf( %d  , pos);         pos += v;         ile += v;       }       printf(  n );     } else {       int res = n * n;       for (int i = (k + 1); i <= (v); ++i)         for (int j = (k + 1); j <= (v); ++j)           if ((n - i - j) >= 0 && (n - i - j) % v == 0)             res = min(res, 2 + (n - i - j) / v);       printf( %d n , res);       for (int i = (k + 1); i <= (v); ++i)         for (int j = (k + 1); j <= (v); ++j)           if ((n - i - j) >= 0 && (n - i - j) % v == 0 &&               2 + (n - i - j) / v == res) {             int pos = 1 + i - (k + 1);             int ile = i;             printf( %d %d  , pos, n - (j - (k + 1)));             n -= j;             pos = ile + k + 1;             while (ile < n) {               printf( %d  , pos);               pos += v;               ile += v;             }             printf(  n );             return 0;           }     }     return 0;   }


#include <bits/stdc++.h>   const int mod = 1000000007;   struct uinon_set {     int father[100003], n, value[100003];     int ans;     void init() {       ans = 0;       for (int i = 1; i <= n; i++) {         father[i] = i;         value[i] = 0;       }       return;     }     int getfather(int i) {       if (father[i] == i) return i;       int tans = getfather(father[i]);       value[i] = (value[i] + value[father[i]]) % mod;       return father[i] = tans;     }     void insert(int root, int i, int xi) {       int temp, tmp = getfather(root);       temp = (value[root] + xi) % mod;       father[tmp] = i;       value[tmp] = temp;       ans = (ans + temp) % mod;       return;     }   } set;   int main() {     int n, t, x, y, i, j;     scanf( %d , &n);     set.n = n;     set.init();     for (i = 1; i <= n; i++) {       scanf( %d , &t);       for (j = 0; j < t; j++) {         scanf( %d%d , &x, &y);         set.insert(x, i, y);       }     }     printf( %d n , (set.ans + mod) % mod);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MAXN = 5e5 + 5;   bool vis[MAXN];   long long sp[MAXN];   long long f[MAXN];   bool is[MAXN];   long long ar[MAXN];   long long POWER[25];   long long F[25];   void sieve() {     for (long long i = 0; i < MAXN; i++) vis[i] = false, sp[i] = 2;     for (long long i = 3; i < MAXN; i += 2) {       if (!vis[i]) {         sp[i] = i;         for (long long j = i * i; j < MAXN; j += 2 * i) {           vis[j] = true;           sp[j] = i;         }       }     }   }   int main() {     long long n, q;     cin >> n >> q;     for (long long i = 1; i <= n; i++) scanf( %lld , &ar[i]);     sieve();     long long added = 0;     POWER[0] = 1;     for (long long i = 1; i < 15; i++) POWER[i] = POWER[i - 1] << 1;     long long ans = 0;     while (q--) {       long long x;       long long temp = 0;       scanf( %lld , &x);       long long cc = 0;       long long val = ar[x];       while (val != 1) {         long long div = sp[val];         F[cc++] = div;         while (val % div == 0) val /= div;       }       for (long long i = 1; i < POWER[cc]; i++) {         long long p = 1;         long long counter = 0;         for (long long j = 0; j < cc; j++)           if (POWER[j] & i) counter++, p *= F[j];         if (counter & 1)           temp += f[p];         else           temp -= f[p];       }       if (is[x]) {         ans -= added - temp;         if (ar[x] == 1) ans++;         is[x] = false;         for (long long i = 1; i < POWER[cc]; i++) {           long long p = 1;           for (long long j = 0; j < cc; j++)             if (POWER[j] & i) p *= F[j];           f[p]--;         }         added--;       } else {         ans += added - temp;         is[x] = true;         for (long long i = 1; i < POWER[cc]; i++) {           long long p = 1;           for (long long j = 0; j < cc; j++)             if (POWER[j] & i) p *= F[j];           f[p]++;         }         added++;       }       printf( %lld n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int x, y;     string s, t;     cin >> x >> y >> s >> t;     int arr[y - x + 1];     int mx = -1, res;     for (int i = 0; i < y - x + 1; i++) {       int k = i;       arr[i] = 0;       for (int j = 0; j < x; j++, k++) {         if (t[k] == s[j]) arr[i] += 1;       }       if (mx < arr[i]) {         mx = arr[i];         res = i;       }     }     cout << x - mx << endl;     for (int i = res, j = 0; j < x; i++, j++) {       if (t[i] != s[j]) cout << j + 1 <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k, m;     cin >> n >> k;     m = n * (n - 1) / 2;     if ((double)k > ((double)n - 1) / 2) {       cout << -1;       return 0;     } else {       cout << k * n << endl;       for (int i = 1; i <= n; ++i) {         for (int j = 0; j < k; ++j) {           cout << i <<     << (i + j) % n + 1 << endl;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const long long inf = 0x3f3f3f3f3f3f3f3f;   const int maxn = 1005;   int main() {     int N;     cin >> N;     cout << 2 * N - 1 <<     << 2 << endl;     cout << 1 <<     << 2 << endl;     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;       cin >> n;       vector<pair<pair<int, int>, int> > a(2 * n - 1);       for (int i = 0; i < 2 * n - 1; i++) {         cin >> a[i].first.first;         cin >> a[i].first.second;         a[i].second = i + 1;       }       sort(a.begin(), a.end());       long long s = 0, s1 = 0, tt = 0, tt1 = 0;       for (int i = 0; i < 2 * n - 1; i++) {         if (i % 2 == 0) {           s += a[i].first.first;           s1 += a[i].first.second;         }         tt += a[i].first.first;         tt1 += a[i].first.second;       }       tt++;       tt1++;       tt /= 2;       tt1 /= 2;       cout <<  YES  << endl;       if (s >= tt && s1 >= tt1) {         for (int i = 0; i < 2 * n - 1; i++) {           if (i % 2 == 0) cout << a[i].second <<    ;         }         cout << endl;       } else {         for (int i = 0; i < 2 * n - 1; i++) {           if (i % 2 == 1) {             cout << a[i].second <<    ;           }         }         cout << a[2 * n - 2].second << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T1>   void print_list(const T1& a) {     cerr << a << endl;   }   template <typename T1, typename T2>   void print_list(const T1& a, const T2& b) {     cerr << a <<     << b << endl;   }   template <typename T1, typename T2, typename T3>   void print_list(const T1& a, const T2& b, const T3& c) {     cerr << a <<     << b <<     << c << endl;   }   template <typename T1, typename T2, typename T3, typename T4>   void print_list(const T1& a, const T2& b, const T3& c, const T4& d) {     cerr << a <<     << b <<     << c <<     << d << endl;   }   template <typename T1, typename T2, typename T3, typename T4, typename T5>   void print_list(const T1& a, const T2& b, const T3& c, const T4& d,                   const T5& e) {     cerr << a <<     << b <<     << c <<     << d <<     << e << endl;   }   template <typename T1, typename T2, typename T3, typename T4, typename T5,             typename T6>   void print_list(const T1& a, const T2& b, const T3& c, const T4& d, const T5& e,                   const T6& f) {     cerr << a <<     << b <<     << c <<     << d <<     << e <<     << f << endl;   }   template <typename T1, typename T2, typename T3, typename T4, typename T5,             typename T6, typename T7>   void print_list(const T1& a, const T2& b, const T3& c, const T4& d, const T5& e,                   const T6& f, const T7& g) {     cerr << a <<     << b <<     << c <<     << d <<     << e <<     << f <<              << g << endl;   }   template <typename A, typename B>   ostream& operator<<(ostream& out, const pair<A, B>& pp) {     out <<  (  << pp.first <<  ,  << pp.second <<  ) ;     return out;   }   template <typename A, typename B>   istream& operator<<(istream& in, pair<A, B>& pp) {     cerr <<  A pair wanted  << endl;     in >> pp.first >> pp.second;     return in;   }   template <typename T>   ostream& operator<<(ostream& out, const vector<T>& vect) {     out <<  length =   << vect.size() << endl;     for (int i = 0; i < vect.size(); i++) out << vect[i] <<    ;     out << endl;     return out;   }   template <typename T>   istream& operator>>(istream& in, vector<T>& vect) {     vect.clear();     int n;     cerr <<  A integer of length wanted  << endl;     in >> n;     vect.resize(n);     cerr << n <<   elements wanted  << endl;     for (int i = 0; i < n; i++) in >> vect[i];     return in;   }   template <typename T>   ostream& operator<<(ostream& out, const vector<vector<T> >& vect) {     out <<  row number=  << vect.size() << endl;     for (int i = 0; i < vect.size(); i++) {       out <<  row #  << i <<  : ;       for (int j = 0; j < vect[i].size(); j++) out <<     << vect[i][j];       out << endl;     }     return out;   }   template <typename T>   istream& operator>>(istream& in, vector<vector<T> >& vect) {     vect.clear();     int n, m;     cerr <<  Two integers wnated  << endl;     in >> n >> m;     vect.resize(n);     cerr <<  A matrix   << n <<   X   << m <<   wanted  << endl;     for (int i = 0; i < n; i++) {       vect[i].resize(m);       for (int j = 0; j < m; j++) in >> vect[i][j];     }     return in;   }   template <typename T>   inline void updateMax(T& a, const T& b) {     a = max(a, b);   }   template <typename T>   inline void updateMin(T& a, const T& b) {     a = min(a, b);   }   template <typename T>   inline vector<T> erase(vector<T> table, int ind) {     assert(ind < table.size());     table.erase(table.begin() + ind);     return table;   }   template <typename T>   vector<T> unique(vector<T> table) {     sort(table.begin(), table.end());     return vector<T>(table.begin(), unique(table.begin(), table.end()));   }   template <class T>   vector<T> parse(const string& ss, const char* cut =    ) {     vector<T> re;     for (int j = 0; j < ss.size(); j++) {       string s;       while (j < ss.size() && NULL == strchr(cut, ss[j])) s += ss[j++];       if (!s.empty()) {         T tmp;         istringstream is(s);         is >> tmp;         re.push_back(tmp);       }     }     return re;   }   int countBit(int n) {     int re = 0;     while (n) re++, n ^= ((((n) ^ ((n)-1)) & n));     return re;   }   int MSB(int n) {     if (n == 0) return 0;     while (((((n) ^ ((n)-1)) & n)) != n) n ^= ((((n) ^ ((n)-1)) & n));     return n;   }   void initHash(int n, int hash[]) {     for (int i = 0; i < n; i++) hash[((1) << (i))] = i;   }   void initBcnt(int n, int bcnt[]) {     bcnt[0] = 0;     for (int i = 1; i < ((1) << (n)); i++)       bcnt[i] = bcnt[i ^ ((((i) ^ ((i)-1)) & i))] + 1;   }   template <typename T>   T __gcd(T n, T m, T& a, T& b) {     T a1 = 0, b1 = 1;     a = 1, b = 0;     while (m) {       T c = n / m;       T r = n - m * c;       T t;       t = a;       a = a1;       a1 = t - c * a1;       t = b;       b = b1;       b1 = t - c * b1;       n = m;       m = r;     }     return n;   }   struct Node {     int r, c, d;     int v;     Node(int x, int y, int z, int v1) : r(x), c(y), d(z), v(v1){};     bool operator<(const Node& other) const { return v > other.v; };   };   priority_queue<Node> heap;   const int dir[][2] = {{-1, 0}, {0, 1},  {1, 0},  {0, -1},                         {1, 1},  {1, -1}, {-1, 1}, {-1, -1}};   const char dname[] =  NWSE ;   const string vv[] = { ......... ,  ....O.... ,  ..O...O.. ,  ..O.O.O.. ,                         O.O...O.O ,  O.O.O.O.O ,  O.OO.OO.O };   const string hh[] = { ......... ,  ....O.... ,  O.......O ,  O...O...O ,                         O.O...O.O ,  O.O.O.O.O ,  OOO...OOO };   char grid[2000][2000];   int dom[1000][1000];   long long int col1[1000], col2[1000];   long long int dp[1000];   const long long int mod = 1000000007;   long long int buf[1000];   int main() {     map<string, int> mem;     for (int i = 0; i < 7; i++) {       mem[vv[i]] |= 1;       mem[hh[i]] |= 2;     }     int n, m;     scanf( %d %d , &n, &m);     for (int i = 0; i < 4 * n + 1; i++) {       scanf( %s , grid[i]);     }     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) {         int x = 4 * i + 1;         int y = 4 * j + 1;         string s(9,  . );         for (int a = 0; a < 3; a++)           for (int b = 0; b < 3; b++) {             s[a * 3 + b] = grid[x + a][y + b];           }         assert(mem.count(s));         dom[i][j] = mem[s];       }     for (int i = 0; i < m; i++) {       col1[i] = 1 - (n & 1);       for (int j = 1; j < n && col1[i]; j += 2) {         if (0 == (dom[j][i] & 1) || 0 == (dom[j - 1][i] & 1)) {           col1[i]--;           break;         }       }     }     for (int i = 0; i + 1 < m; i++) {       dp[0] = 1;       for (int j = 1; j <= n; j++) {         dp[j] = 0;         if ((dom[j - 1][i] & 2) && (dom[j - 1][i + 1] & 2)) {           dp[j] += dp[j - 1];         }         if (j >= 2 && (dom[j - 1][i] & 1) && (dom[j - 2][i] & 1) &&             (dom[j - 1][i + 1] & 1) && (dom[j - 2][i + 1] & 1)) {           dp[j] += dp[j - 2];         }         dp[j] %= mod;       }       col2[i] = dp[n];     }     dp[0] = 1;     for (int i = 1; i <= m; i++) {       dp[i] = 0;       dp[i] += col1[i - 1] * dp[i - 1];       if (i >= 2) {         dp[i] += (col2[i - 2] + mod - col1[i - 1] * col1[i - 2]) * dp[i - 2];       }       dp[i] %= mod;     }     cout << dp[m] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, k;   char s[1005], ans[1005];   map<long long, bool> dp[1005];   map<long long, char> pre[1005];   inline long long read() {     long long q = 0, f = 1;     char ch = getchar();     while (!isdigit(ch)) {       if (ch ==  - ) f = 0;       ch = getchar();     }     while (isdigit(ch)) q = (q << 3) + (q << 1) + (ch ^ 48), ch = getchar();     return f ? q : -q;   }   inline void print(long long x) {     if (x < 0) putchar( - ), x = -x;     if (x > 9) print(x / 10);     putchar(x % 10 + 48);   }   signed main() {     n = read(), k = read(), scanf( %s , s + 1), dp[1][0] = true;     for (long long i = 2; i <= n + 1; ++i)       for (long long j = -k + 1; j < k; ++j)         if (dp[i - 1][j]) {           if (!(s[i - 1] ^  L ))             pre[i][j - 1] =  L , dp[i][j - 1] = true;           else if (!(s[i - 1] ^  W ))             pre[i][j + 1] =  W , dp[i][j + 1] = true;           else if (!(s[i - 1] ^  D ))             pre[i][j] =  D , dp[i][j] = true;           else             pre[i][j + 1] =  W , pre[i][j] =  D , pre[i][j - 1] =  L ,                        dp[i][j + 1] = dp[i][j] = dp[i][j - 1] = true;         }     if (dp[n + 1][k] | dp[n + 1][-k]) {       long long j = dp[n + 1][k] ? k : -k;       for (long long i = n + 1; i > 1; --i) {         char ch = pre[i][j];         ans[i - 1] = ch, ((!(ch ^  W )) ? --j : ((!(ch ^  L )) ? ++j : i));       }       for (long long i = 1; i <= n; ++i) putchar(ans[i]);     } else {       putchar( N ), putchar( O );       return 0;     };     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long modpow(long long a, long long b) {     long long ans = 1;     while (b) {       if (b & 1) {         ans = (ans * a) % 1000000007;       }       a = (a * a) % 1000000007;       b = b / 2;     }     return ans;   }   int main() {     cin.tie(0);     ios::sync_with_stdio(false);     int t;     cin >> t;     while (t--) {       long long x, y;       cin >> x >> y;       if (x - y == 1) {         cout <<  NO  << endl;       } else         cout <<  YES  << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   list<int> l[100005];   list<int> son[100005];   int n, dad[100005], x, y, val[100005], res;   void f(int a) {     for (list<int>::iterator it = l[a].begin(); it != l[a].end(); it++)       if ((*it) != dad[a]) {         dad[*it] = a;         f(*it);         son[a].push_back(*it);       }   }   pair<long long int, long long int> calc(int a) {     pair<long long int, long long int> ret =         pair<long long int, long long int>(0, 0);     pair<long long int, long long int> tmp;     for (list<int>::iterator it = son[a].begin(); it != son[a].end(); it++)       if (*it != dad[a]) {         tmp = calc(*it);         ret = pair<long long int, long long int>(max(tmp.first, ret.first),                                                  max(tmp.second, ret.second));       }     int change = ret.first - ret.second;     if (val[a] + change > 0)       ret.second += abs(val[a] + change);     else       ret.first += abs(val[a] + change);     return ret;   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n - 1; i++) {       scanf( %d %d , &x, &y);       l[x].push_back(y);       l[y].push_back(x);     }     for (int i = 1; i <= n; i++) scanf( %d , &val[i]);     f(1);     pair<long long int, long long int> res = calc(1);     cout << res.first + res.second << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     if (fopen( input.txt ,  r )) {       freopen( input.txt ,  r , stdin);       freopen( output.txt ,  w , stdout);     }     int n, a, b;     cin >> n;     bool visit[1000005] = {false};     int ans = -1;     while (n--) {       cin >> a >> b;       while (visit[a] || a < ans) {         a += b;       }       ans = max(ans, a);       visit[a] = true;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   T gcd(T a, T b) {     if (a == 0) return b;     return gcd(b % a, a);   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int tt;     cin >> tt;     while (tt--) {       int n;       int diff = 0;       cin >> n;       int a[2 * n];       for (int i = 0; i < 2 * n; i++) {         cin >> a[i];         if (a[i] & 1)           diff++;         else           diff--;       }       if (diff == 0) {         cout << 0 <<   n ;         continue;       }       map<int, int> mp;       mp[0] = 0;       int cnt = 0;       for (int i = n; i < 2 * n; i++) {         if (a[i] & 1)           cnt++;         else           cnt--;         if (mp.find(cnt) == mp.end()) mp[cnt] = i - (n - 1);       }       cnt = 0;       int ans;       if (mp.find(diff) == mp.end())         ans = 2 * n;       else         ans = mp[diff];       for (int i = n - 1; i >= 0; i--) {         if (a[i] & 1)           cnt++;         else           cnt--;         if (mp.find(diff - cnt) != mp.end()) {           ans = min(ans, mp[(diff - cnt)] + n - i);         }       }       cout << ans <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   template <typename T, typename U>   inline void amin(T &x, U y) {     if (y < x) x = y;   }   template <typename T, typename U>   inline void amax(T &x, U y) {     if (x < y) x = y;   }   int main() {     int n, p, k, ai;     cin >> n >> k >> p;     vector<int> ods;     vector<int> evns;     for (int i = 0; i < n; i++) {       cin >> ai;       if (ai % 2 == 0)         evns.push_back(ai);       else         ods.push_back(ai);     }     int rem = k - p;     vector<vector<int> > ans;     for (int i = 0; i < p; i++) {       if (evns.size()) {         vector<int> tmp;         tmp.push_back(evns.back());         ans.push_back(tmp);         evns.pop_back();       } else if (ods.size() >= 2) {         vector<int> tmp;         tmp.push_back(ods.back());         ods.pop_back();         tmp.push_back(ods.back());         ods.pop_back();         ans.push_back(tmp);       } else {         cout <<  NO ;         return 0;       }     }     for (int i = 0; i < rem; i++) {       if (ods.size()) {         vector<int> tmp;         tmp.push_back(ods.back());         ans.push_back(tmp);         ods.pop_back();       } else {         cout <<  NO ;         return 0;       }     }     if (ods.size() % 2 == 1) {       cout <<  NO ;       return 0;     }     if (k == 1) {       cout <<  YES  << endl;       int m = ans[0].size() + ods.size() + evns.size();       cout << m <<    ;       for (int j = 0; j < ans[0].size(); j++) cout << ans[0][j] <<    ;       for (int j = 0; j < ods.size(); j++) cout << ods[j] <<    ;       for (int j = 0; j < evns.size(); j++) cout << evns[j] <<    ;       return 0;     }     cout <<  YES  << endl;     for (int i = 0; i < ans.size(); i++) {       if (ods.size()) {         int m = ans[i].size() + ods.size();         cout << m <<    ;         for (int j = 0; j < ans[i].size(); j++) cout << ans[i][j] <<    ;         for (int j = 0; j < ods.size(); j++) cout << ods[j] <<    ;         ods.clear();         cout << endl;       } else if (evns.size()) {         int m = ans[i].size() + evns.size();         cout << m <<    ;         for (int j = 0; j < ans[i].size(); j++) cout << ans[i][j] <<    ;         for (int j = 0; j < evns.size(); j++) cout << evns[j] <<    ;         evns.clear();         cout << endl;       } else {         cout << ans[i].size() <<    ;         for (int j = 0; j < ans[i].size(); j++) cout << ans[i][j] <<    ;         cout << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int fa[101000][20], h[101000], lev[101000], ans[101000];   int dp[101000];   struct EDGE {     int to, nxt;   } box[2 * 101000];   int top, K;   void add(int u, int v) {     box[top].to = v;     box[top].nxt = h[u];     h[u] = top++;   }   void DFS(int u, int pre, int t) {     lev[u] = lev[pre] + 1;     fa[u][0] = pre;     if (!t)       for (int i = 1; i < 20; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1];     for (int p = h[u]; p != -1; p = box[p].nxt) {       int v = box[p].to;       if (pre == v) continue;       DFS(v, u, t);       if (t) {         dp[u] += dp[v];         ans[p / 2] = dp[v];       }     }   }   int lca(int u, int v) {     if (lev[u] < lev[v]) swap(u, v);     int c = lev[u] - lev[v];     for (int i = 19; i >= 0; i--) {       if (c & (1 << i)) {         u = fa[u][i];         c -= (1 << i);       }     }     if (u == v) return u;     for (int i = 19; i >= 0; i--) {       if (fa[u][i] != fa[v][i]) {         u = fa[u][i];         v = fa[v][i];       }     }     return fa[u][0];   }   int main() {     int n, k;     int a, b;     scanf( %d , &n);     memset(h, -1, sizeof(h));     for (int i = 1; i < n; i++) {       scanf( %d%d , &a, &b);       add(a, b);       add(b, a);     }     DFS(1, 1, 0);     scanf( %d , &k);     for (int i = 0; i < k; i++) {       scanf( %d%d , &a, &b);       int c = lca(a, b);       dp[a]++;       dp[b]++;       dp[c] -= 2;     }     DFS(1, 1, 1);     for (int i = 0; i < n - 1; i++) {       printf( %d%c , ans[i], i == n - 2 ?   n  :    );     }   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long extgcd(long long a, long long b, long long& x, long long& y) {     if (b == 0) {       x = 1;       y = 0;       return a;     }     long long x1, y1;     long long d = extgcd(b, a % b, x1, y1);     x = y1;     y = x1 - y1 * (a / b);     return d;   }   bool isPrime(long long n) {     if (n <= 1) return false;     if (n <= 3) return true;     if (n % 2 == 0 || n % 3 == 0) return false;     for (long long i = 5; i * i <= n; i = i + 6)       if (n % i == 0 || n % (i + 2) == 0) return false;     return true;   }   bool isPowerOfTwo(long long n) {     if (n == 0) return false;     return (ceil(log2(n)) == floor(log2(n)));   }   long long powm(long long a, long long b) {     long long res = 1;     while (b) {       if (b & 1) res = (res * a) % 1000000007;       a = (a * a) % 1000000007;       b >>= 1;     }     return res;   }   long long divide(long long a, long long b) {     return (a % 1000000007) * powm(b, 1000000007 - 2) % 1000000007;   }   long long mul(long long a, long long b) {     return ((a % 1000000007) * (b % 1000000007)) % 1000000007;   }   long long add(long long a, long long b) {     return (a % 1000000007 + b % 1000000007) % 1000000007;   }   long long Totfun(long long n) {     long long z = n;     if (n % 2 == 0) {       while (n % 2 == 0) {         n /= 2;       }       z /= 2;     }     for (long long i = 3; i <= sqrt(n); i += 2) {       if (isPrime(i) && n % i == 0) {         while (n % i == 0) {           n /= i;         }         z -= z / i;       }     }     if (n > 1) {       z -= z / n;     }     return z;   }   long long fact[100001];   long long nCr(long long n, long long r) {     if (r > n || r < 0) return 0;     long long z = fact[n];     z = mul(z, powm(fact[n - r], 1000000007 - 2));     z = mul(z, powm(fact[r], 1000000007 - 2));     return z;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long n, a = 0, b = 0, c = 0, z = 1;     cin >> n;     string s;     cin >> s;     for (long long i = 0; i < n; i++) {       if (s[i] ==  a )         a = add(a, z);       else if (s[i] ==  b )         b = add(b, a);       else if (s[i] ==  c )         c = add(c, b);       else {         c = add(3 * c, b);         b = add(3 * b, a);         a = add(3 * a, z);         z = mul(z, 3);       }     }     cout << c <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int n, ar[100001], max_s[100001], max_e[100001], pre_s[100001],       pre_e[100001], ans = INT_MIN;   int main() {     cin >> n;     for (int i = 0; i < n; i++) cin >> ar[i];     for (int i = 1; i < n; i++) pre_s[i] = pre_s[i - 1] + ar[i - 1];     for (int i = n - 2; i >= 0; i--) pre_e[i] = pre_e[i + 1] + ar[i + 1];     max_e[n - 1] = abs(ar[n - 1]);     max_s[0] = abs(ar[0]);     for (int i = n - 2; i >= 0; i--)       max_e[i] = max(ar[i] + max_e[i + 1], -(ar[i] + pre_e[i]));     for (int i = 1; i < n; i++)       max_s[i] = max(ar[i] + max_s[i - 1], -(ar[i] + pre_s[i]));     ans = max(ans, max_e[0]);     ans = max(ans, max_s[n - 1]);     for (int i = 0; i < n - 1; i++) ans = max(ans, max_s[i] + max_e[i + 1]);     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   #pragma warning(disable : 4996);   long long int n, h[1000007], d[1000007][2], mod = 1e9 + 7;   void solve() {     long long int i, j;     cin >> n;     for (i = 0; i < n; i++) {       cin >> h[i];       h[i] -= 1;     }     h[n] = 0;     d[0][0] = max((long long int)0, h[0] - h[1]);     d[0][1] = min(h[0], h[1]);     for (i = 1; i < n; i++) {       long long int k = min(h[i - 1], h[i]);       d[i][0] = d[i - 1][1] * max((long long int)0, k - h[i + 1]);       d[i][0] %= mod;       d[i][0] = (d[i][0] + max((long long int)0, h[i] - h[i + 1])) % mod;       d[i][1] = d[i - 1][1] * min(h[i + 1], k);       d[i][1] %= mod;       d[i][1] = (d[i][1] + min(h[i], h[i + 1])) % mod;     }     long long int ans = 0;     for (i = 0; i < n; i++) {       ans = (ans + d[i][0] + d[i][1]) % mod;     }     cout << ans;   }   int main() {   #pragma comment(linker,  /STACK:1073741824      solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int setBit(int n, int pos) { return n | (1 << pos); }   int resetBit(int n, int pos) { return n & ~(1 << pos); }   bool checkBit(int n, int pos) { return n & (1 << pos); }   inline long long bigmod(long long p, long long e, long long M) {     long long ret = 1;     while (e > 0) {       if (e % 2) ret = (ret * p) % M;       p = (p * p) % M;       e /= 2;     }     return ret;   }   inline long long power(long long x, long long y) {     long long ans = 1;     while (y > 0) {       if (y % 2) ans *= x;       x *= x;       y /= 2;     }     return ans;   }   template <class T>   T gcd(T a, T b) {     if (b == 0) return a;     return gcd(b, a % b);   }   template <class T>   T lcm(T a, T b) {     return a * (b / gcd(a, b));   }   int n, k, a, b, c, d;   int vis[1003];   int main() {     cin >> n >> k >> a >> b >> c >> d;     if (k < n + 1 || n == 4) {       cout << -1;       return 0;     }     vis[a] = vis[b] = vis[c] = vis[d] = 1;     printf( %d %d  , a, c);     for (int i = 1; i <= n; i++) {       if (vis[i] == 0) {         printf( %d  , i);       }     }     printf( %d %d n , d, b);     printf( %d %d  , c, a);     for (int i = 1; i <= n; i++) {       if (vis[i] == 0) {         printf( %d  , i);       }     }     printf( %d %d , b, d);   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,avx )   using namespace std;   int n, q, fa[66666], aa[66666], kh[66666], a[66666], p[66666], fp[66666],       siz[66666], ck, pa[66666];   vector<int> edge[66666];   int dd[66666];   void dfs(int u) {     siz[u] = 1;     p[++ck] = u;     fp[u] = ck;     pa[ck] = fp[fa[u]];     dd[ck] = dd[pa[ck]] + 1;     for (auto v : edge[u]) {       dfs(v);       siz[u] += siz[v];     }   }   int main() {     scanf( %d%d , &n, &q);     for (int i = 1; i <= n; ++i) {       scanf( %d , aa + i);     }     for (int i = 2; i <= n; ++i) {       scanf( %d , fa + i);       edge[fa[i]].push_back(i);     }     dfs(1);     for (int i = 1; i <= n; ++i) {       a[fp[i]] = aa[i];     }     for (int i = 1; i <= q; ++i) {       int x;       scanf( %d , &x);       int y = siz[x];       x = fp[x];       int pp = i + 1, gg = y >> 3, *jj = kh + x, *diao = dd + x, *aa = a + x;       int ans = 0, cnt = 0;       for (int j = 0; j ^ gg; ++j, jj += 8, diao += 8, aa += 8) {         if (jj[0] < pp) ans += diao[0], jj[0] = i + aa[0], ++cnt;         if (jj[1] < pp) ans += diao[1], jj[1] = i + aa[1], ++cnt;         if (jj[2] < pp) ans += diao[2], jj[2] = i + aa[2], ++cnt;         if (jj[3] < pp) ans += diao[3], jj[3] = i + aa[3], ++cnt;         if (jj[4] < pp) ans += diao[4], jj[4] = i + aa[4], ++cnt;         if (jj[5] < pp) ans += diao[5], jj[5] = i + aa[5], ++cnt;         if (jj[6] < pp) ans += diao[6], jj[6] = i + aa[6], ++cnt;         if (jj[7] < pp) ans += diao[7], jj[7] = i + aa[7], ++cnt;       }       gg <<= 3;       for (int j = gg; j < y; ++j) {         if (jj[j - gg] < pp)           ans += diao[j - gg], jj[j - gg] = i + aa[j - gg], ++cnt;         ;       }       ans = ans - cnt * dd[x];       printf( %d %d n , ans, cnt);     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b;     cin >> a >> b;     for (int i = 1; i <= 1000000000; i++) {       if (i % 2 == 1) {         if (a >= i) {           a -= i;         } else {           cout <<  Vladik ;           return 0;         }       } else {         if (b >= i) {           b -= i;         } else {           cout <<  Valera ;           return 0;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   int f[4005][4005];   int a[4005];   int main() {     int i, j, k, n, m = 0;     memset(f, 0, sizeof(f));     cin >> n;     for (i = 1; i <= n; i++) {       cin >> a[i];       k = 0;       for (j = 0; j < i; j++) {         f[i][j] = f[j][k] + 1;         if (a[j] == a[i]) {           k = j;         }         m = max(m, f[i][j]);       }     }     cout << m << endl;   }


#include <bits/stdc++.h>   using namespace std;   inline long long isqrt(long long k) {     long long r = sqrt(k) + 1;     while (r * r > k) r--;     return r;   }   inline long long icbrt(long long k) {     long long r = cbrt(k) + 1;     while (r * r * r > k) r--;     return r;   }   inline long long mnz(long long& a, long long b) { return a = (a > b ? b : a); }   inline long long mxz(long long& a, long long b) { return a = (a < b ? b : a); }   inline string toString(long long n) {     stringstream ss;     ss << n;     return ss.str();   }   double const eps = 1e-6;   long long const Base = 1e9 + 7, oo = 1e17, MAXN = 1e6;   long long A[MAXN + 5];   vector<long long> dske[MAXN + 5];   bitset<MAXN + 5> bi;   long long Solves() {     long long n, m, k, cnt = 0, ans = 0, x = -1, y = -1, q, c, sum = 0, v, t = -1;     cin >> n;     for (long long i = 1; i <= n; i++) dske[i].clear(), bi[i] = 0;     for (long long i = 1; i <= n; i++) {       cin >> k;       c = 0;       while (k--) {         cin >> x;         if (!bi[x] and !c) c = x;       }       if (c)         bi[c] = 1;       else         y = i;     }     if (y == -1) return cout <<  OPTIMAL  << endl, 0;     vector<long long> vec;     for (long long i = 1; i <= n; i++)       if (!bi[i]) vec.push_back(i);     map<long long, long long> mm;     for (long long i = 0; i < dske[y].size(); i++) mm[dske[y][i]] = 1;     for (long long i = 0; i < vec.size(); i++) {       if (!mm[vec[i]]) {         cout <<  IMPROVE  << endl;         cout << y <<     << vec[i] << endl;         return 0;       }     }     assert(0);     return 0;   }   int main() {     if (fopen( locin.txt ,  r )) freopen( locin.txt ,  r , stdin);     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long test = 1;     cin >> test;     for (long long T = 1; T <= test; T++) {       Solves();     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     vector<string> res;     for (int i = 0; i < t; ++i) {       long long int a, b;       cin >> a >> b;       if (a > b) swap(a, b);       if ((a + b) % 3 == 0 && 2 * a >= b)         res.push_back( YES );       else         res.push_back( NO );     }     for (int i = 0; i < t; ++i) {       cout << res[i] << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1000 + 10;   map<char, int> d, t;   string s;   string know[MAXN];   int pd[MAXN];   int n, m, ans, q, x;   int main() {     cin >> n;     cin >> s;     for (int i = 0; i < s.size(); i++) {       if (s[i] !=  * ) x++;     }     if (x == s.size()) {       cout <<  0  << endl;       return 0;     }     for (int i = 0; i < s.size(); i++) d[s[i]]++;     cin >> m;     for (int i = 1; i <= m; i++) cin >> know[i];     for (int i = 1; i <= m; i++) {       int cnt = 0, z = 0;       memset(pd, 0, sizeof(pd));       for (int j = 0; j < know[i].size(); j++) {         if (s[j] !=  *  && s[j] != know[i][j]) {           z = 1;           break;         }       }       if (z) {         q++;         continue;       }       for (int j = 0; j < know[i].size(); j++) {         if (d[know[i][j]] && s[j] ==  * ) {           z = 1;           break;         }       }       if (z) {         q++;         continue;       }       for (int j = 0; j < know[i].size(); j++) {         if (!pd[know[i][j] -  a ] && !d[know[i][j]]) {           pd[know[i][j] -  a ] = 1;           t[know[i][j]]++;           z = 1;         }       }       if (!z) q++;     }     for (int i = 0; i <= 25; i++) {       char k =  a  + i;       if (!d[k] && t[k] == m - q && t[k]) ans++;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) {     int c;     while (a != 0) {       c = a;       a = b % a;       b = c;     }     return b;   }   int main() {     ios_base::sync_with_stdio(0);     int n, m, b = 0;     cin >> n >> m;     vector<vector<int> > arr(n, vector<int>(4, 0));     for (int i = 0; i < n; i++) {       if (b < m) {         b++;         arr[i][0] = b;       }       if (b < m) {         b++;         arr[i][3] = b;       }     }     for (int i = 0; i < n; i++) {       if (b < m) {         b++;         arr[i][1] = b;       }       if (b < m) {         b++;         arr[i][2] = b;       }     }     for (int i = 0; i < n; i++) {       if (arr[i][1] != 0) cout << arr[i][1] <<    ;       if (arr[i][0] != 0) cout << arr[i][0] <<    ;       if (arr[i][2] != 0) cout << arr[i][2] <<    ;       if (arr[i][3] != 0) cout << arr[i][3] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ld = long double;   using ll = long long;   using ull = unsigned long long;   const int INF = 1 << 30;   const ll LINF = 1LL << 60;   const int SZ = 1 << 13;   char buff[SZ], *pos = buff + SZ - 1;   inline ll read() {     ll x = 0;     int f = 1,         c = (++pos == buff + SZ ? fread(pos = buff, 1, SZ, stdin), *pos : *pos);     for (; !isdigit(c);          c = (++pos == buff + SZ ? fread(pos = buff, 1, SZ, stdin), *pos : *pos))       if (c ==  - ) f = -f;     for (; isdigit(c);          c = (++pos == buff + SZ ? fread(pos = buff, 1, SZ, stdin), *pos : *pos))       x = (x << 3) + (x << 1) + (c ^ 48);     return x * f;   }   const int N = 5005;   int n;   int T;   int t[N];   ld p[N], pw[N];   ld dp[2][N];   int main() {     cout << fixed << setprecision(9);     n = read(), T = read();     for (int i = 1; i <= n; ++i)       p[i] = read() / 100.0, t[i] = read(), pw[i] = pow(1 - p[i], t[i] - 1);     ld ans = 0;     dp[0][0] = 1;     int pp = 0, qq = 1;     for (int i = 1; i <= n; ++i) {       ld sum = 0;       for (int j = 1; j <= T; ++j) {         sum = sum * (1 - p[i]) + dp[pp][j - 1];         if (j >= t[i]) sum -= dp[pp][j - t[i]] * pw[i];         dp[qq][j] = sum * p[i];         if (j >= t[i]) dp[qq][j] += dp[pp][j - t[i]] * pw[i];         ans += dp[qq][j];       }       swap(pp, qq);       memset(dp[qq], 0, sizeof dp[qq]);     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200002;   vector<vector<pair<int, int> > > ans;   vector<vector<int> > cs, twoCs, threeCs;   int n, p[N];   int main() {     ios::sync_with_stdio(0);     cin >> n;     for (int i = 1; i <= n; i++) cin >> p[i];     bool mark[N] = {};     for (int i = 1; i <= n; i++)       if (!mark[i] && p[i] != i) {         vector<int> newC;         int v = i;         while (!mark[v]) {           mark[v] = 1;           newC.push_back(v);           v = p[v];         }         cs.push_back(newC);       }     for (int i = 0; i < cs.size(); i++)       while (cs[i].size() >= 4) {         vector<pair<int, int> > move;         for (int j = max(0, (int)cs[i].size() - 5); j + 1 < cs[i].size(); j++)           move.push_back(make_pair(cs[i][j], cs[i][j + 1]));         move.push_back(             make_pair(cs[i].back(), cs[i][max((int)cs[i].size() - 5, 0)]));         ans.push_back(move);         for (int j = 0; j < 4; j++) cs[i].pop_back();       }     for (int i = 0; i < cs.size(); i++)       if (cs[i].size() == 2)         twoCs.push_back(cs[i]);       else if (cs[i].size() == 3)         threeCs.push_back(cs[i]);     while (threeCs.size()) {       vector<pair<int, int> > move;       vector<int> &tt = threeCs.back();       move.push_back(make_pair(tt[0], tt[1]));       move.push_back(make_pair(tt[1], tt[2]));       move.push_back(make_pair(tt[2], tt[0]));       threeCs.pop_back();       if (twoCs.size()) {         tt = twoCs.back();         move.push_back(make_pair(tt[0], tt[1]));         move.push_back(make_pair(tt[1], tt[0]));         twoCs.pop_back();       } else if (threeCs.size()) {         tt = threeCs.back();         move.push_back(make_pair(tt[0], tt[1]));         move.push_back(make_pair(tt[1], tt[0]));         vector<int> newC;         newC.push_back(tt[0]);         newC.push_back(tt[2]);         twoCs.push_back(newC);         threeCs.pop_back();       }       ans.push_back(move);     }     while (twoCs.size()) {       vector<pair<int, int> > move;       vector<int> &tt = twoCs.back();       move.push_back(make_pair(tt[0], tt[1]));       move.push_back(make_pair(tt[1], tt[0]));       twoCs.pop_back();       if (twoCs.size()) {         tt = twoCs.back();         move.push_back(make_pair(tt[0], tt[1]));         move.push_back(make_pair(tt[1], tt[0]));         twoCs.pop_back();       }       ans.push_back(move);     }     cout << ans.size() << endl;     for (int i = 0; i < ans.size(); i++) {       cout << ans[i].size() << endl;       for (int j = 0; j < ans[i].size(); j++) cout << ans[i][j].first <<    ;       cout << endl;       for (int j = 0; j < ans[i].size(); j++) cout << ans[i][j].second <<    ;       cout << endl;     }   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   const int N = 550;   using namespace std;   int n;   int m;   int k;   long long d[N][N][2];   long long cost[N][N];   vector<pair<int, long long> > R[N][N];   vector<pair<int, long long> > D[N][N];   vector<pair<int, long long> > nv;   void normal(vector<pair<int, long long> > &v) {     sort(v.begin(), v.end());     reverse(v.begin(), v.end());     for (int i = 1; i < v.size(); i++) {       v[i].second += v[i - 1].second;     }   }   void upd(long long &x, long long y) { x = min(x, y); }   bool good(vector<pair<int, int> > &g, int i, int t) {     int cur = g[i].first + g[i].second;     if (cur >= t && (cur - t) % 4 == i) {       return true;     }     return false;   }   int main() {     ios_base::sync_with_stdio(0);     scanf( %d%d%d , &n, &m, &k);     vector<pair<int, int> > g;     for (int i = 1, x, y, d, t, e; i <= k; i++) {       scanf( %d%d%d%d%d , &x, &y, &d, &t, &e);       g.clear();       g.push_back({x, y});       g.push_back({x + d, y - d});       g.push_back({x + d, y});       g.push_back({x, y + d});       for (int i = 0; i < 4; i++) {         if (good(g, i, t)) {           cost[g[i].first][g[i].second] += e;         }       }       if (good(g, 0, t) && good(g, 3, t)) {         R[g[3].first][g[3].second].push_back({g[0].second, e});       }       if (good(g, 0, t) && good(g, 2, t)) {         D[g[2].first][g[2].second].push_back({g[0].first, e});       }       if (good(g, 1, t) && good(g, 2, t)) {         R[g[2].first][g[2].second].push_back({g[1].second, e});       }     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         normal(R[i][j]);         normal(D[i][j]);       }     }     for (int i = 0; i <= n; i++) {       for (int j = 0; j <= m; j++) {         d[i][j][0] = 1e18;         d[i][j][1] = 1e18;       }     }     d[0][0][0] = 0;     d[0][0][1] = 0;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         long long cnt = d[i][j][1];         for (int h = j + 1; h < m; h++) {           while (!R[i][h].empty() && R[i][h].back().first < j) {             R[i][h].pop_back();           }           if (!R[i][h].empty()) {             cnt -= R[i][h].back().second;           }           cnt += cost[i][h];           upd(d[i][h][0], cnt);         }         cnt = d[i][j][0];         for (int h = i + 1; h < n; h++) {           while (!D[h][j].empty() && D[h][j].back().first < i) {             D[h][j].pop_back();           }           if (!D[h][j].empty()) {             cnt -= D[h][j].back().second;           }           cnt += cost[h][j];           upd(d[h][j][1], cnt);         }       }     }     printf( %lld n , min(d[n - 1][m - 1][0], d[n - 1][m - 1][1]) + cost[0][0]);   }


#include <bits/stdc++.h>   using namespace std;   long long n, _C;   long long lo, hi;   set<long long> S;   vector<long long> vec;   int actual;   long long offset;   void ask(long long x) {     S.insert(x);     vec.emplace_back(x);     if (actual) cout <<  ?   << x <<   n ;     if (actual)       cin >> x;     else       x = abs(vec[(int)vec.size() - 2] - vec.back()) >= _C;     if ((int)vec.size() > 1) {       long long d = abs(vec[(int)vec.size() - 2] - vec.back());       if (x)         hi = min(hi, d);       else         lo = max(lo, d + 1);     }   }   long long solve() {     vec.clear();     S.clear();     lo = 1;     hi = n;     long long cur = offset;     ask(cur);     while (lo < hi) {       long long mid = (lo + hi) / 2;       int p1 = (lo & 1);       int p2 = (hi & 1);       if ((lo & 1) == (hi & 1)) mid--;       if ((int)S.size() & 1)         ask(vec.back() - mid);       else         ask(vec.back() + mid);     }     if (actual) cout <<  =   << lo <<   n ;     return *S.begin();   }   int main() {     ios_base::sync_with_stdio(0);     int itest;     cin >> itest;     while (itest--) {       cin >> n;       offset = 0;       _C = n;       actual = 0;       offset = 1 - solve();       actual = 1;       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t, n;     cin >> t;     while (t--) {       long long x;       double sum = 0;       cin >> n >> x;       vector<long long> v(n);       for (int i = 0; i < n; i++) {         cin >> v[i];         sum += v[i];       }       sort(v.begin(), v.end());       int ans = sum / n >= x ? n : 0;       double subSum = 0;       for (int i = 0; i < n and !ans; i++) {         subSum += v[i];         if ((sum - subSum) / (n - (i + 1)) >= x) {           ans = n - (i + 1);           break;         }       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const int N = 5e5 + 5;   int n, m;   ll c[N];   vector<int> adj[N];   map<vector<int>, ll> mp;   ll custom_gcd(ll a, ll b) {     ll c;     while (b > 0) {       c = a % b;       a = b;       b = c;     }     return a;   }   void solve() {     mp.clear();     cin >> n >> m;     for (int i = 1; i <= n; ++i) {       cin >> c[i];       adj[i].clear();     }     for (int i = 1, u, v; i <= m; ++i) {       cin >> u >> v;       adj[v].emplace_back(u);     }     for (int i = 1; i <= n; ++i) {       sort(adj[i].begin(), adj[i].end());       if (!adj[i].empty()) mp[adj[i]] += c[i];     }     ll ans = 0;     for (auto &p : mp) {       ans = custom_gcd(ans, p.second);     }     cout << ans <<   n ;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000 + 1;   char a[N][N];   int f[N * 2], head[1000001], vis[2 * N], d[N * 2], d1[N * 2], bj[N * 2];   int ask(int x) { return x == f[x] ? x : f[x] = ask(f[x]); }   struct node {     int next, to;   } e[N * N];   int tot;   void add(int x, int y) {     e[++tot].to = y;     e[tot].next = head[x];     head[x] = tot;   }   int cnt;   int solve() {     queue<int> q;     for (int i = 1; i <= cnt; i++) {       if (d[i] == 0) q.push(i);     }     while (!q.empty()) {       int p = q.front();       q.pop();       for (int i = head[p]; i; i = e[i].next) {         int u = e[i].to;         d[u]--;         if (d[u] == 0) q.push(u);       }     }     for (int i = 1; i <= cnt; i++)       if (d[i]) return 1;     return 0;   }   void dfs(int u, int w) {     bj[u] = w;     for (int i = head[u]; i; i = e[i].next) {       int v = e[i].to;       if (bj[v] < w + 1) dfs(v, w + 1);     }   }   void solve1() {     queue<int> q;     for (int i = 1; i <= cnt; i++)       if (!d1[i]) q.push(i), bj[i] = 1;     while (!q.empty()) {       int p = q.front();       q.pop();       for (int i = head[p]; i; i = e[i].next) {         int u = e[i].to;         d1[u]--;         bj[u] = max(bj[u], bj[p] + 1);         if (!d1[u]) q.push(u);       }     }   }   int main() {     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);     int n, m;     cin >> n >> m;     for (int i = 1; i <= n + m; i++) f[i] = i;     for (int i = 1; i <= n; i++) {       cin >> a[i] + 1;     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         int f1 = ask(i), f2 = ask(n + j);         if (a[i][j] !=  = ) {           if (f1 == f2) {             cout <<  No n ;             return 0;           }         } else {           f[f1] = f2;         }       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         int f1 = ask(i), f2 = ask(n + j);         if (!vis[f1]) vis[f1] = ++cnt;         if (!vis[f2]) vis[f2] = ++cnt;         if (a[i][j] !=  = ) {           if (f1 == f2) {             cout <<  No n ;             return 0;           }           if (a[i][j] ==  < )             add(vis[f1], vis[f2]), d[vis[f2]]++, d1[vis[f2]]++;           else             add(vis[f2], vis[f1]), d[vis[f1]]++, d1[vis[f1]]++;         }       }     }     if (solve()) {       cout <<  No n ;       return 0;     }     solve1();     cout <<  Yes n ;     for (int i = 1; i <= n + m; i++) {       cout << bj[vis[ask(i)]] <<    ;       if (i == n) cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;       typedef long double ld;   const long long inf = 1100000000000000000;   #define maxn 1000005   #define mod 1000000007 //998244353   #define int long long   #define PB push_back   #define eb emplace_back   #define all(c) (c).begin(),(c).end()   #define pii pair <int,int>   #define vi vector<int>   #define F first   #define S second   #define rep(i,a,b)  for(int i=a;i<b;i++)   #define rep1(i,a,b) for(int i=a;i<=b;i++)   #define repd(i,a,b) for(int i=a;i>=b;--i)   #define sz(x) (int)((x).size())     #define mem(a,x) memset(a,x,sizeof(a))   const long double PI=3.141592653589793238462643383279502884197169399375105820974944;      void solve(){    int n, m; cin>>n>>m;    int z = 720720; //lcm{1..16}    int a[n][m];    rep(i, 0, n)      rep(j, 0, m)       cin>>a[i][j];    rep(i, 0, n){     rep(j, 0, m){      int k = z-pow(a[i][j], 4);      if((i+j)%2)       cout<<z<<   ;      else       cout<<k<<   ;     }cout<<  n ;    }   }      signed main(){       ios_base::sync_with_stdio(false), cin.tie(nullptr);        int T=1;// cin>>T;        while(T--){           solve();cout<<  n ;       }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   long n, ps[1000], tp, q, ar[1000], ans, x, y, a, b;   int main() {     ios_base::sync_with_stdio(0);     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> ar[i];       ps[ar[i]] = i;     }     cin >> q;     for (; q; --q) {       cin >> tp;       if (tp == 2) {         cin >> a >> b;         swap(ar[a], ar[b]);         ps[ar[a]] = a;         ps[ar[b]] = b;       } else {         cin >> x >> y;         ans = 1;         while (x < y) {           if (ps[x + 1] < ps[x]) ++ans;           ++x;         }         cout << ans << endl;       }     }     cin.get();     cin.get();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, a = 0;   int main() {     cin >> n;     n = abs(n);     while ((a * (a + 1)) / 2 < n || ((a * (a + 1)) / 2) % 2 != n % 2) a++;     cout << a;     return 0;   }


#include <bits/stdc++.h>   void solve() {     int n;     std::cin >> n;     int a = 0;     std::string s;     std::cin >> s;     bool flag = false;     int x, y;     x = y = 0;     if (s[0] ==  U ) {       ++y;     } else {       ++x;     }     if (x > y) flag = true;     for (size_t i = 1; i < s.size(); ++i) {       if (s[i] ==  U ) {         ++y;       } else {         ++x;       }       if (x > y && !flag) {         flag = true;         ++a;       } else if (x < y && flag) {         flag = false;         ++a;       }     }     std::cout << a;   }   int main() {     std::ios_base::sync_with_stdio(false);     std::cin.tie(nullptr);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long d = 0, n, i;     cin >> n;     int a[n];     for (i = 0; i < n; i++) cin >> a[i];     sort(a, a + n);     for (i = 0; i < n; i++) {       if (a[i] > d) d++;     }     cout << d << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10;   int pos[N];   int main() {     ios::sync_with_stdio(false);     int n, a;     while (cin >> n) {       for (int i = 1; i <= n; ++i) {         cin >> a;         pos[a] = i;       }       int last = 0;       for (int i = 1; i <= n; ++i) {         cin >> a;         cout << max(pos[a] - last, 0) <<    ;         last = max(last, pos[a]);       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     int n;     cin >> n;     vector<long long> a(n + 1, 0);     long long s1 = 0, s2 = 0, s3 = 0;     for (int i = 1; i <= n; i++) {       scanf( %lld , &a[i]);       a[i] += a[i - 1];     }     if (a[n] % 3) {       cout <<  0 n ;       return;     }     vector<int> a0, a1;     for (int i = 1; i < n; i++) {       if (a[i] == a[n] / 3) a0.push_back(i);       if (a[i] == 2 * a[n] / 3) a1.push_back(i);     }     long long ret = 0;     while (a0.size() && a1.size()) {       while (a0.size() && a0.back() >= a1.back()) {         a0.pop_back();       }       if (!a0.size()) break;       ret += a0.size();       a1.pop_back();     }     cout << ret << endl;   }   int main() {     solve();     return 0;   }


#include <iostream>   #include <vector>   #include <set>   #include <unordered_set>   #include <map>   #include <unordered_map>   #include <algorithm>   #include <numeric>   #include <queue>   #include <iomanip>   #include <numeric>   #include <cmath>   using namespace std;      int main() {    int t;    cin >> t;    for (int tt = 0; tt < t; tt++) {     int n, m;     cin >> n >> m;     vector<int> va(n, 0), vb(n, 0);     unordered_map<int, vector<int>> tofix;     unordered_map<int, int> fixed;     for (int i = 0; i < n; i++) {      cin >> va[i];     }     for (int i = 0; i < n; i++) {      cin >> vb[i];      if (va[i] == vb[i]) {       fixed[va[i]] = i;      } else {       tofix[vb[i]].push_back(i);      }     }     vector<int> res(m, 0);     vector<pair<int, int>> redundant;     for (int i = 0; i < m; i++) {      int x;      cin >> x;      if (tofix.count(x) > 0 and tofix[x].size() > 0) {       res[i] = tofix[x].back();       fixed[x] = tofix[x].back();       tofix[x].pop_back();       if (tofix[x].size() == 0) {        tofix.erase(x);       }          if (redundant.size() > 0) {        for (auto p : redundant) {         res[p.first] = fixed[x];        }        redundant.clear();       }      } else {       if (fixed.count(x) > 0) {        if (redundant.size() > 0) {         for (auto p : redundant) {          res[p.first] = fixed[x];         }         redundant.clear();         res[i] = fixed[x];        } else {         res[i] = fixed[x];        }       } else {        redundant.emplace_back(i, x);       }      }     }     if (redundant.empty() and tofix.size() == 0) {      cout <<  YES  << endl;      for (auto r : res) {       cout << r + 1 <<    ;      }      cout << endl;     } else {      cout <<  NO  << endl;     }    }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2.5 * 1e5 + 5;   int n, m, k;   int d[N];   struct Edge {     int num;     int next;   } edge[4 * N];   int tot, last[N];   vector<int> q;   vector<int> ans[N];   vector<int> path;   int anss;   bool flag;   void Add(int i, int j) {     tot++;     edge[tot].num = j;     edge[tot].next = last[i];     last[i] = tot;   }   void dfs(int i, int deep) {     if (flag) return;     d[i] = deep;     q.push_back(i);     if (deep > n / k) {       flag = 1;       for (int j = q.size() - 1; j >= 0; j--) path.push_back(q[j]);       return;     }     bool flag = 0;     for (int k = last[i]; k != 0; k = edge[k].next) {       int v = edge[k].num;       if (d[v] == 0) {         dfs(v, deep + 1);         flag = 1;       }     }     if (flag == 0) {       anss++;       int sign1 = 0, sign2 = 0;       bool sign = 0;       for (int k = last[i]; k != 0; k = edge[k].next) {         int v = edge[k].num;         if (d[v] + 1 == d[i]) continue;         if ((d[i] - d[v]) % 3 == 2) {           if (sign1 == 0)             sign1 = v;           else             sign2 = v;         } else {           for (int k = q.size() - 1; k >= 0; k--) {             ans[anss].push_back(q[k]);             if (q[k] == v) break;           }           sign = 1;           break;         }       }       if (sign == 0) {         bool en = 0;         ans[anss].push_back(i);         for (int k = q.size() - 1; k >= 0; k--) {           if ((q[k] == sign1 || q[k] == sign2) && en == 0) {             en = 1;             ans[anss].push_back(q[k]);             continue;           }           if (en) ans[anss].push_back(q[k]);           if ((q[k] == sign1 || q[k] == sign2) && en == 1) {             en = 0;             break;           }         }       }     }     q.pop_back();   }   int main() {     std::ios::sync_with_stdio(0);     cin >> n >> m >> k;     for (int i = 1; i <= m; i++) {       int u, v;       cin >> u >> v;       Add(u, v);       Add(v, u);     }     dfs(1, 1);     if (flag) {       cout <<  PATH n  << path.size() << endl;       for (int j = path.size() - 1; j >= 0; j--) cout << path[j] <<    ;     } else {       cout <<  CYCLES n ;       for (int i = 1; i <= k; i++) {         cout << ans[i].size() << endl;         ;         for (int j = 0; j <= ans[i].size() - 1; j++) cout << ans[i][j] <<    ;         cout << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int test_case;     cin >> test_case;     while (test_case--) {       int a, b, x, y;       vector<int> akash;       cin >> a >> b >> x >> y;       int temp1 = 0, temp2 = 0, temp3 = 0, temp4 = 0;       temp1 = ((b - y) - 1) * a;       temp2 = ((a - x) - 1) * b;       temp3 = (y - 0) * a;       temp4 = (x - 0) * b;       akash.push_back(temp1);       akash.push_back(temp2);       akash.push_back(temp3);       akash.push_back(temp4);       int max = akash[0];       for (int i = 1; i < 4; i++) {         if (max < akash[i]) {           max = akash[i];         }       }       cout << max << endl;       akash.clear();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[20000];   int main() {     ios_base::sync_with_stdio(NULL);     int n;     cin >> n;     for (int i = 0; i < n; i++) {       int x;       cin >> x;       if (x == 1) {         cout <<  HARD ;         return 0;       }     }     cout <<  EASY ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n, a[105];       cin >> n;       for (int i = 0; i < n; i++) cin >> a[i];       for (int i = n - 1; i >= 0; i--) cout << a[i] <<    ;       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   const int N = 1000000;   int n;   char str[N + 10];   int cdog = 0, fdog = 0, ldog = 0, cbone = 0, fbone = 0, lbone = 0;   int cntd[N + 10], cntb[N + 10];   int nxtd[N + 10], nxtb[N + 10];   int lstd[N + 10], lstb[N + 10];   int tp1[N + 10], tp2[N + 10][2];   bool dp[N + 10];   bool chk(int x) {     memset(dp, 0, sizeof(dp));     dp[0] = true;     for (int i = n; i >= 0; --i) {       if (str[i] !=  P ) continue;       int nd = lstd[std::min(i + x, n)];       if (nd > i)         tp1[i] = tp1[nd];       else         tp1[i] = i + x;     }     for (int i = n; i >= 0; --i) {       if (str[i] !=  P ) continue;       int nd = lstd[std::min(i + x, n)], bd = nxtd[i + 1];       if (!bd)         tp2[i][0] = n + 1, tp2[i][1] = 0;       else if (nd == bd)         tp2[i][0] = bd - x, tp2[i][1] = i + x;       else         tp2[i][0] = bd - x, tp2[i][1] = tp1[nd];     }     for (int i = 0; i < n; ++i) {       if (!dp[i]) continue;       int nd = nxtd[i + 1];       int nb = nxtb[i + 1];       if (nd == 0 || nb == 0) break;       if (nb < nd) {         if (nb >= nd - x) {           dp[nd] = true;         }         if (nb >= tp2[nd][0]) {           dp[std::min(tp2[nd][1], n)] = true;         }       } else {         dp[std::min(tp1[nd], n)] = true;       }     }     int lst = 0;     for (int i = n; i >= 0; --i)       if (dp[i]) {         lst = i;         break;       }     return lst >= lbone;   }   int main() {     scanf( %d , &n);     scanf( %s , str + 1);     for (int i = 1; i <= n; ++i)       if (str[i] ==  P ) {         ++cdog;         if (fdog == 0) fdog = i;         ldog = i;         cntd[i] = cntd[i - 1] + 1;         cntb[i] = cntb[i - 1];       } else if (str[i] ==  * ) {         ++cbone;         if (fbone == 0) fbone = i;         lbone = i;         cntd[i] = cntd[i - 1];         cntb[i] = cntb[i - 1] + 1;       } else {         cntd[i] = cntd[i - 1];         cntb[i] = cntb[i - 1];       }     if (cdog == 1) {       if (cntb[ldog] > cbone - cntb[ldog]) {         printf( %d %d n , cntb[ldog], ldog - fbone);       } else {         printf( %d %d n , cbone - cntb[ldog], lbone - ldog);       }       return 0;     }     printf( %d  , cbone);     for (int i = n; i >= 0; --i) {       if (str[i] ==  P )         nxtd[i] = i;       else         nxtd[i] = nxtd[i + 1];       if (str[i] ==  * )         nxtb[i] = i;       else         nxtb[i] = nxtb[i + 1];     }     for (int i = 1; i <= n; ++i) {       if (str[i] ==  P )         lstd[i] = i;       else         lstd[i] = lstd[i - 1];       if (str[i] ==  * )         lstb[i] = i;       else         lstb[i] = lstb[i - 1];     }     int l = 1, r = n, ans = 0;     while (l <= r) {       int mid = (l + r) >> 1;       if (chk(mid)) {         ans = mid, r = mid - 1;       } else {         l = mid + 1;       }     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100005, MAXM = 1000005, MAXT = 1 << 18;   struct Edge {     int to, val, next, id;   } edge[MAXM];   struct MST {     int u, v, w, id;   } mst[MAXM];   int ppar[MAXN], head[MAXN], ist[MAXM], dep[MAXN], n, m, tot;   void addedge(int u, int v, int w, int id) {     edge[++tot] = (Edge){v, w, head[u], id};     head[u] = tot;   }   int find(int x) { return x == ppar[x] ? x : ppar[x] = find(ppar[x]); }   void merge(int x, int y) {     x = find(x), y = find(y);     if (x != y) ppar[x] = y;   }   int mmn[MAXT], beg[MAXN], ed[MAXN], ans[MAXM], par[20][MAXN], mx[20][MAXN],       dcnt;   struct Que {     int l, r, id;   };   struct Mod {     int p, w;   };   vector<Que> qr1[MAXN], qr2[MAXN];   vector<Mod> md1[MAXN], md2[MAXN];   void dfs(int u, int fa) {     beg[u] = ++dcnt;     dep[u] = dep[fa] + 1;     for (int i = head[u]; i; i = edge[i].next) {       int v = edge[i].to;       if (v == fa) continue;       dfs(v, u);       qr1[beg[v]].push_back((Que){beg[v], ed[v], edge[i].id});       qr2[ed[v]].push_back((Que){beg[v], ed[v], edge[i].id});       par[0][v] = u;       mx[0][v] = edge[i].val;     }     ed[u] = dcnt;   }   void chk(int &x, int y) { x = x < y ? x : y; }   void cxk(int &x, int y) { x = x > y ? x : y; }   void upd(int x, int w, int l = 1, int r = n, int k = 1) {     if (l == r) {       chk(mmn[k], w);       return;     }     int mid = (l + r) >> 1;     if (x <= mid)       upd(x, w, l, mid, k << 1);     else       upd(x, w, mid + 1, r, k << 1 | 1);     chk(mmn[k], w);   }   int ask(int a, int b, int l = 1, int r = n, int k = 1) {     if (a > r || b < l) return 1E9;     if (a <= l && b >= r) return mmn[k];     int mid = (l + r) >> 1;     return min(ask(a, b, l, mid, k << 1), ask(a, b, mid + 1, r, k << 1 | 1));   }   int calc(int x, int y) {     if (dep[x] > dep[y]) swap(x, y);     int res = 0;     for (int i = 19; ~i; i--)       if (dep[par[i][y]] >= dep[x]) cxk(res, mx[i][y]), y = par[i][y];     if (x == y) return res;     for (int i = 19; ~i; i--)       if (par[i][x] != par[i][y])         cxk(res, mx[i][x]), cxk(res, mx[i][y]), x = par[i][x], y = par[i][y];     return max(res, max(mx[0][x], mx[0][y]));   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; i++) {       int u, v, w;       scanf( %d%d%d , &u, &v, &w);       mst[i] = (MST){u, v, w, i};       ans[i] = 1E9;     }     sort(mst + 1, mst + 1 + m,          [&](const MST &a, const MST &b) { return a.w < b.w; });     for (int i = 1; i <= n; i++) ppar[i] = i;     for (int i = 1; i <= m; i++) {       int u = mst[i].u, v = mst[i].v;       if (find(u) != find(v)) {         merge(u, v);         ist[mst[i].id] = 1;         addedge(u, v, mst[i].w, mst[i].id);         addedge(v, u, mst[i].w, mst[i].id);       }     }     for (int i = 0; i < MAXT; i++) mmn[i] = 1E9;     dfs(1, 0);     for (int i = 1; i < 20; i++)       for (int j = 1; j <= n; j++) {         par[i][j] = par[i - 1][par[i - 1][j]];         mx[i][j] = max(mx[i - 1][j], mx[i - 1][par[i - 1][j]]);       }     for (int i = 1; i <= m; i++)       if (!ist[mst[i].id]) {         int u = mst[i].u, v = mst[i].v;         if (beg[u] > beg[v]) swap(u, v);         md1[beg[u]].push_back((Mod){beg[v], mst[i].w});         md2[beg[v]].push_back((Mod){beg[u], mst[i].w});         ans[mst[i].id] = calc(u, v);       }     for (int i = 1; i <= n; i++) {       for (Que q : qr1[i]) chk(ans[q.id], ask(q.l, q.r));       for (Mod d : md1[i]) upd(d.p, d.w);     }     for (int i = 0; i < MAXT; i++) mmn[i] = 1E9;     for (int i = n; i > 0; i--) {       for (Que q : qr2[i]) chk(ans[q.id], ask(q.l, q.r));       for (Mod d : md2[i]) upd(d.p, d.w);     }     for (int i = 1; i <= m; i++) printf( %d n , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[17][17];   int main() {     int n, m, k, need;     scanf( %d%d%d%d , &n, &m, &k, &need);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         int x, y;         scanf( %d%d , &x, &y);         a[x][y] = 1;       }     }     int ans = 0;     for (int x1 = 1; x1 <= n; x1++) {       for (int y1 = 1; y1 <= m; y1++) {         for (int x2 = x1; x2 <= n; x2++) {           for (int y2 = 1; y2 <= m; y2++) {             int tot = 0;             for (int x = x1; x <= x2; x++) {               for (int y = y1; y <= y2; y++) {                 tot += a[x][y];               }             }             if (tot >= need) ans++;           }         }       }     }     printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   const int M = 2e5 + 5;   vector<int> G[M];   int n, A[M], num, ok, res, ans[2][M], C;   bool mark[M];   void ma(int a, int b) {     if (a == b || mark[a] && mark[b]) return;     printf( %d %d n , a, b);     ans[0][C] = a;     ans[1][C] = b;     C++;     mark[a] = mark[b] = 1;     ma(A[a], A[b]);   }   int main() {     scanf( %d , &n);     res = -1;     for (int i = 1; i <= n; i++) scanf( %d , A + i);     for (int i = 1; i <= n; i++) {       int nw = A[i], has = 0;       while (!mark[nw]) {         has = 1;         mark[nw] = 1;         G[num].push_back(nw);         nw = A[nw];       }       if (G[num].size() == 1) ok = A[i];       if (G[num].size() == 2) res = num;       if (has) num++;     }     memset(mark, 0, sizeof(mark));     if (ok) {       puts( YES );       for (int i = 1; i <= n; i++)         if (i != ok) printf( %d %d n , ok, i);     } else if (~res) {       for (int i = 0; i < num; i++)         if (G[i].size() & 1) {           puts( NO );           return 0;         }       puts( YES );       printf( %d %d n , G[res][0], A[G[res][0]]);       for (int i = 0; i < num; i++)         if (res != i) ma(G[res][0], G[i][0]);     } else       puts( NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const int INF = 1e9 + 10;   const int N = 1e2 + 5;   int mark[N];   int ans;   int main() {     int n, k;     cin >> n >> k;     for (int i = 0; i < n; i++) {       int x;       cin >> x;       mark[x] = i + 1;     }     for (int i = 0; i < N; i++) {       if (mark[i] != 0) ans++;     }     if (ans >= k) {       cout <<  YES  <<   n ;       int cnt = 0;       for (int i = 0; i < N; i++) {         if (cnt < k and mark[i] != 0) {           cout << mark[i] <<    ;           cnt++;         }       }     } else       cout <<  NO ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int i, j, len;     char s[105];     cin >> s;     len = strlen(s);     for (i = 0; i < len; i++) {       for (j = 0; j < len - i - 2; j += 2) {         if (s[j] > s[j + 2]) {           swap(s[j], s[j + 2]);         }       }     }     printf( %s , s);     return 0;   }


#include <bits/stdc++.h>   int c[100];   int main() {     int n, s, max = 0, i, j;     scanf( %d , &n);     for (i = 0; i < n; i++) {       scanf( %d , &s);       c[s] += 1;       if (s > max) max = s;     }     for (i = 0; i <= max; i++)       for (j = 0; j < c[i]; j++) printf( %d  , i);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, k, m, d, i = 0, b = 1, ans = 0;   int main(void) {     cin >> n >> k >> m >> d;     for (i = 1; i <= d && b <= n; i++, b += k) {       ans = max(ans, min(m, n / b) * i);     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     long long x;     bool s;     node(long long a, bool b) { x = a, s = b; }   };   bool operator<(node a, node b) { return a.x > b.x; }   priority_queue<node> pq;   int main() {     int n;     long long temp;     cin >> n;     long long ans = 0;     for (int i = 1; i <= n; i++) {       cin >> temp;       if (pq.empty() || pq.top().x > temp) {         pq.push(node(temp, 0));       } else {         long long now = pq.top().x;         bool se = pq.top().s;         pq.pop();         if (se) pq.push(node(now, 0));         ans += temp - now, pq.push(node(temp, 1));       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     long long n;     cin >> n;     long long ar[n + 1];     for (long long i = 1; i <= n; i++) {       cin >> ar[i];     }     for (long long i = 1; i <= n; i++) {       long long f = ar[i];       long long t = ar[f];       if (ar[t] == i) {         return cout <<  YES , 0;       }     }     cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   const int MOD = 1e9 + 7, MAX = 1e6 + 5;   const long long INF = 1e18 + 5;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) {       int n, q;       cin >> n >> q;       vector<int> A(n + 2);       for (int in = 1; in < 1 + n; in++) cin >> A[in];       A[0] = -n - 1;       A[n + 1] = -n - 1;       long long ans = 0;       set<int> Pos;       set<int> Neg;       for (int i = 1; i <= n; i++) {         if (A[i] > A[i - 1] && A[i] > A[i + 1]) {           ans += A[i];           Pos.insert(A[i]);         }         if (A[i] < A[i - 1] && A[i] < A[i + 1]) {           ans -= A[i];           Neg.insert(A[i]);         }       }       cout << ans <<   n ;       while (q--) {         int u, v;         cin >> u >> v;         swap(A[u], A[v]);         long long diff = 0;         for (int i = u - 1; i <= u + 1; i++) {           if (i <= 0) continue;           if (i > n) continue;           if (A[i] > A[i - 1] && A[i] > A[i + 1]) {             if (Neg.count(A[i])) {               Neg.erase(A[i]);               Pos.insert(A[i]);               diff += 2 * A[i];             } else if (!Pos.count(A[i])) {               Pos.insert(A[i]);               diff += A[i];             }           } else if (A[i] < A[i - 1] && A[i] < A[i + 1]) {             if (Pos.count(A[i])) {               Pos.erase(A[i]);               Neg.insert(A[i]);               diff -= 2 * A[i];             } else if (!Neg.count(A[i])) {               Neg.insert(A[i]);               diff -= A[i];             }           } else {             if (Pos.count(A[i])) {               Pos.erase(A[i]);               diff -= A[i];             } else if (Neg.count(A[i])) {               Neg.erase(A[i]);               diff += A[i];             }           }         }         for (int i = v - 1; i <= v + 1; i++) {           if (i <= u + 1) continue;           if (i <= 0) continue;           if (i > n) continue;           if (A[i] > A[i - 1] && A[i] > A[i + 1]) {             if (Neg.count(A[i])) {               Neg.erase(A[i]);               Pos.insert(A[i]);               diff += 2 * A[i];             } else if (!Pos.count(A[i])) {               Pos.insert(A[i]);               diff += A[i];             }           } else if (A[i] < A[i - 1] && A[i] < A[i + 1]) {             if (Pos.count(A[i])) {               Pos.erase(A[i]);               Neg.insert(A[i]);               diff -= 2 * A[i];             } else if (!Neg.count(A[i])) {               Neg.insert(A[i]);               diff -= A[i];             }           } else {             if (Pos.count(A[i])) {               Pos.erase(A[i]);               diff -= A[i];             } else if (Neg.count(A[i])) {               Neg.erase(A[i]);               diff += A[i];             }           }         }         ans += diff;         cout << ans <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main(void) {     int a[10] = {0};     int n;     string d;     cin >> n;     cin >> d;     for (int i = 0; i < d.size(); i++) {       if (d[i] ==  L ) {         for (int j = 0; j < 10; j++) {           if (a[j] == 0) {             a[j] = 1;             break;           }         }       } else if (d[i] ==  R ) {         for (int j = 9; j >= 0; j--) {           if (a[j] == 0) {             a[j] = 1;             break;           }         }       } else {         a[d[i] -  0 ] = 0;       }     }     for (int i = 0; i < 10; i++) printf( %d , a[i]);   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10, mod = 1e9 + 7;   int n, K, ans, cnt;   char s[5];   struct DSU {     int fa[N];     bool mark[N];     int getfa(int x) { return fa[x] == x ? x : fa[x] = getfa(fa[x]); }     bool merge(int x, int y) {       int fx = getfa(x), fy = getfa(y);       if (getfa(x ^ 1) == fy || getfa(y ^ 1) == fx) return 0;       if (fx != fy) fa[fx] = fy;       return 1;     }     bool Set(int l, int r, int c) {       if (!c)         return merge(l * 2, r * 2) && merge(l * 2 + 1, r * 2 + 1);       else         return merge(l * 2 + 1, r * 2) && merge(l * 2, r * 2 + 1);     }   } T;   int qpow(int a, int b) {     int ret = 1;     for (; b; b >>= 1) {       if (b & 1) ret = 1ll * ret * a % mod;       a = 1ll * a * a % mod;     }     return ret;   }   int main() {     cin >> n >> K;     for (int i = 0; i <= (n + 2) * 2; i++) T.fa[i] = i;     for (int i = 1; i <= K; i++) {       int x, y;       scanf( %d%d%s , &x, &y, s + 1);       --x, --y;       int l = x - y, r = x + y;       if (l < 0) l = -l;       if (r > n - 1) r = (n - 1) * 2 - r;       if (!T.Set(l, r + 2, s[1] ==  o )) {         return puts( 0 ), 0;       }     }     for (int i = 0; i <= (n + 1) * 2 + 1; i++) ans += (T.getfa(i) == i);     printf( %d n , qpow(2, ans / 2 - 2));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100005;   const double eps = 1e-9;   const double pi = acos(-1);   struct P {     int x, y;   } a[N];   int n, vi[N], t[N];   double l[N], r[N], q[N];   long long k;   void change(int k, int v) {     for (; k; k -= k & (-k)) t[k] += v;   }   int ask(int k) {     int s = 0;     for (; k < N; k += k & (-k)) s += t[k];     return s;   }   struct node {     double p1, p2;     int v;   } op[N * 2];   bool cmp(node a, node b) { return a.p1 < b.p1; }   long long count(double R) {     int top = 0, op_top = 0;     memset(t, 0, sizeof(t));     for (int i = (int)(1); i <= (int)(n); i++) {       l[i] = r[i] = -1;       double d = sqrt(a[i].x * a[i].x + a[i].y * a[i].y);       if (d < R + eps) continue;       double theta = atan2(a[i].y, a[i].x);       double delta = acos(R / d);       l[i] = theta - delta;       r[i] = theta + delta;       for (; l[i] < 0; l[i] += 2 * pi)         ;       for (; r[i] < 0; r[i] += 2 * pi)         ;       for (; l[i] >= 2 * pi; l[i] -= 2 * pi)         ;       for (; r[i] >= 2 * pi; r[i] -= 2 * pi)         ;       if (l[i] > r[i]) swap(l[i], r[i]);       q[++top] = l[i];       op[++op_top] = (node){l[i], l[i], 1};       op[++op_top] = (node){r[i], l[i], -1};     }     sort(q + 1, q + top + 1);     sort(op + 1, op + op_top + 1, cmp);     long long sum = 0;     for (int i = (int)(1); i <= (int)(op_top); i++) {       int p = lower_bound(q + 1, q + top + 1, op[i].p2) - q;       change(p, op[i].v);       if (op[i].v == -1) sum += ask(p);     }     return sum;   }   int main() {     scanf( %d%lld , &n, &k);     for (int i = (int)(1); i <= (int)(n); i++) scanf( %d%d , &a[i].x, &a[i].y);     double l = 0, r = 15000;     for (int T = (int)(1); T <= (int)(50); T++) {       double mid = (l + r) / 2;       if (1ll * n * (n - 1) / 2 - count(mid) < k)         l = mid;       else         r = mid;     }     printf( %.10lf n , (l + r) / 2);   }


#include <bits/stdc++.h>   #pragma warning(disable : 4996)   using namespace std;   int n;   long long a[1005][2];   long long mi = ((1ll << 62) - 1);   void calc(long long x) {     int i;     long long sum = 0;     int cnt = 0;     vector<pair<int, int> > v;     for (i = 0; i < n; i++) {       if (a[i][0] <= x && a[i][1] <= x) {         if (a[i][0] <= a[i][1])           sum += a[i][0];         else           v.push_back(pair<int, int>(a[i][1] - a[i][0], i));       } else if (a[i][0] <= x)         sum += a[i][1], cnt++;       else if (a[i][1] <= x)         sum += a[i][0];       else         return;     }     if (cnt > n / 2) return;     sort(v.begin(), v.end());     for (auto e : v) {       if (cnt < n / 2)         sum += a[e.second][1], cnt++;       else         sum += a[e.second][0];     }     mi = min(mi, x * sum);   }   int main() {     int i, j, k;     cin >> n;     for (i = 0; i < n; i++)       for (j = 0; j < 2; j++) cin >> a[i][j];     for (i = 0; i < n; i++)       for (j = 0; j < 2; j++) calc(a[i][j]);     cout << mi;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void reverse_(vector<int> num) {     vector<int> a;     for (int i = num.size() - 1; i > -1; i--) a.push_back(num[i]);     num = a;   }   int main() {     int n, x;     cin >> n >> x;     vector<int> num(n), used(1e5 + 7, 0), step(1e5 + 7, 1e9);     set<int> all;     for (int i = 0; i < n; i++) {       cin >> num[i];       used[num[i]] = 1;       all.insert(num[i]);     }     if (all.size() < n) {       cout <<  0 n ;       return 0;     }     int ans = 1e9;     sort(num.begin(), num.end());     reverse_(num);     for (int i = 0; i < n; i++) {       int now = num[i];       int prev = -1;       int shag = 0;       while (now >= 0 && prev != now) {         if (now != num[i]) {           if (used[now]) {             ans = min(ans, shag);           }         }         if (step[now] != 1e9) {           ans = min(step[now] + shag, ans);         }         step[now] = min(step[now], shag);         prev = now;         now = now & x;         shag++;       }     }     if (ans == 1e9) {       cout <<  -1 n ;       return 0;     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n;     long long p[n], y;     set<long long> c[4];     set<long long>::iterator it;     c[1].insert(-1);     c[2].insert(-1);     c[3].insert(-1);     int temp;     for (int i = 0; i < n; i++) cin >> p[i];     for (int i = 0; i < n; i++) {       cin >> temp;       c[temp].insert(p[i]);     }     for (int i = 0; i < n; i++) {       cin >> temp;       c[temp].insert(p[i]);     }     cin >> m;     for (int i = 0; i < m; i++) {       cin >> temp;       it = c[temp].begin();       it++;       if (it == c[temp].end()) {         cout <<  -1  ;         continue;       } else {         y = *it;         cout << y <<    ;       }       c[1].erase(y);       c[2].erase(y);       c[3].erase(y);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int edge[510][510], vis[510], con, N, M, f, t;   void dfs(int x) {     vis[x] = 1;     for (int i = 1; i <= N; i++)       if (edge[x][i]) {         if (!vis[i])           dfs(i);         else if (vis[i] == 1)           con++;         if (con >= 2) break;       }     vis[x] = 2;   }   int main() {     scanf( %d%d , &N, &M);     for (int i = 1; i <= M; i++) {       scanf( %d%d , &f, &t);       edge[f][t] = 1;     }     for (int i = 1; i <= N; i++) {       con = 0;       memset(vis, 0, sizeof vis);       dfs(i);       for (int j = 1; j <= N; j++)         if (j != i) {           if (!vis[j]) dfs(j);           if (con >= 2) break;         }       if (con <= 1) return puts( YES ), 0;     }     return puts( NO ), 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, i, g, a[100001], p[100001], maxi;     vector<long long> v;     cin >> n;     for (i = 0; i < n; i++) {       cin >> a[i];       p[a[i]] = i + 1;     }     g = 1;     for (i = 1; i < n; i++) {       if (p[i] <= p[i + 1]) {         g++;       } else {         v.push_back(g);         g = 1;       }     }     v.push_back(g);     maxi = 0;     for (i = 0; i < v.size(); i++) {       maxi = max(maxi, v[i]);     }     cout << n - maxi;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100005;   int n, i, j, k, cnt, tot, f[maxn];   char s[maxn];   int main() {     scanf( %d%s , &n, s + 1);     if (n & 1) return puts( 0 ), 0;     tot = n >> 1;     f[0] = 1;     for (i = 1; i <= n; i++) {       if (s[i] ==  ? )         for (j = i >> 1, k = max(1, tot - (n - i)); j >= k; j--)           f[j] = f[j] + f[j - 1];       else         cnt++;     }     if (cnt > tot) return puts( 0 ), 0;     int ans = f[tot];     for (i = tot - cnt; i; i--) ans *= 25;     printf( %u , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       int a[n];       for (int i = 0; i < n; i++) {         cin >> a[i];       }       sort(a, a + n);       int count = 0;       int groups = 0;       for (int i = 0; i < n; i++) {         if (++count == a[i]) {           count = 0;           groups++;         }       }       cout << groups << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline T checkmin(T &a, T b) {     return (a < b) ? a : a = b;   }   template <class T>   inline T checkmax(T &a, T b) {     return (a > b) ? a : a = b;   }   template <class T>   T GCD(T a, T b) {     if (a < 0) return GCD(-a, b);     if (b < 0) return GCD(a, -b);     return (a == 0) ? b : GCD(b % a, a);   }   template <class T>   T LCM(T a, T b) {     if (a < 0) return LCM(-a, b);     if (b < 0) return LCM(a, -b);     return (a == 0 || b == 0) ? 0 : a / GCD(a, b) * b;   }   template <class T>   inline T sqr(T X) {     return X * X;   }   namespace Poor {   const int MaxiN = 105;   const long long Infinity = 2000000000000000000LL;   int N, M;   long long K;   int C[MaxiN][MaxiN];   int Weight[MaxiN + MaxiN];   pair<int, int> List[MaxiN + MaxiN];   char St[MaxiN + MaxiN];   inline void Inc(long long &a, long long b) {     a += b;     if (a > Infinity) a = Infinity;   }   long long DP() {     map<int, long long> Now, Suc;     Now.clear();     Now[0] = 1;     for (int i = 0; i < N + M - 1; ++i) {       for (typeof(Now.begin()) it = Now.begin(); it != Now.end(); ++it) {         if (St[i] !=  ) ) Inc(Suc[it->first + 1], it->second);         if (St[i] !=  (  && it->first > 0) Inc(Suc[it->first - 1], it->second);       }       Now = Suc;       Suc.clear();     }     return Now[0];   }   void Run() {     cin >> N >> M >> K;     for (int i = 0; i < N; ++i)       for (int j = 0; j < M; ++j) cin >> C[i][j];     fill(Weight, Weight + N + M - 1, INT_MAX);     for (int i = 0; i < N; ++i)       for (int j = 0; j < M; ++j) checkmin(Weight[i + j], C[i][j]);     for (int i = 0; i < N + M - 1; ++i) List[i] = make_pair(Weight[i], i);     sort(List, List + N + M - 1);     fill(St, St + N + M - 1,  ? );     for (int i = 0; i < N + M - 1; ++i) {       int p = List[i].second;       St[p] =  ( ;       long long dp = DP();       if (dp < K) {         K -= dp;         St[p] =  ) ;       }     }     for (int i = 0; i < N; ++i) {       for (int j = 0; j < M; ++j) putchar(St[i + j]);       puts(  );     }   }   }  // namespace Poor   int main() {     Poor::Run();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[7][7][101];   int d[101], t[7];   char cd[101];   bool dfs(int now, int saw, int n) {     if (saw == n) return true;     for (int i = 0; i < 7; i++)       if (a[now][i][0] > 0) {         d[saw] = a[now][i][a[now][i][0]];         cd[saw] =  + ;         a[now][i][0]--;         if (dfs(i, saw + 1, n)) return true;         a[now][i][0]++;       }     for (int i = 0; i < 7; i++)       if (a[i][now][0] > 0) {         d[saw] = a[i][now][a[i][now][0]];         cd[saw] =  - ;         a[i][now][0]--;         if (dfs(i, saw + 1, n)) return true;         a[i][now][0]++;       }     return false;   }   int main() {     int n;     cin >> n;     for (int i = 0; i < n; i++) {       int v1, v2;       cin >> v1 >> v2;       a[v1][v2][0]++;       a[v1][v2][a[v1][v2][0]] = i + 1;       t[v1]++;       t[v2]++;     }     int odd1 = -1, odd2 = -1;     for (int i = 0; i < 7; i++)       if (t[i] % 2 == 1) {         if (odd2 != -1) {           cout <<  No solution n ;           return 0;         } else if (odd1 == -1)           odd1 = i;         else           odd2 = i;       }     if (odd1 != -1 && odd2 == -1) {       cout <<  No solution n ;       return 0;     }     if (odd1 != -1) {       if (dfs(odd1, 0, n)) {         for (int i = 0; i < n; i++) cout << d[i] <<     << cd[i] << endl;         return 0;       } else {         cout <<  No solution n ;         return 0;       }     } else {       for (int i = 0; i < 7; i++)         if (t[i] > 0) {           if (dfs(i, 0, n)) {             for (int j = 0; j < n; j++) cout << d[j] <<     << cd[j] << endl;             return 0;           } else {             cout <<  No solution n ;             return 0;           }         }       cout <<  No solution n ;       return 0;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long mod = 1000000009;   int n, m, k;   class Mat {    public:     long long e[2][2];     Mat mul(Mat u) {       Mat s;       for (int i = 0; i < 2; i++) {         for (int j = 0; j < 2; j++) {           s.e[i][j] = 0;           for (int k = 0; k < 2; k++) {             s.e[i][j] += e[i][k] * u.e[k][j];             s.e[i][j] %= mod;           }         }       }       return s;     }     Mat powMat(Mat u, int k) {       Mat mat;       memset(mat.e, 0, sizeof(mat.e));       mat.e[0][0] = 1;       mat.e[1][1] = 1;       while (k) {         if (k & 1) {           mat = mat.mul(u);         }         u = u.mul(u);         k = (k >> 1);       }       return mat;     }   };   bool good(int mid) {     int n1 = n - mid * k;     int m1 = m - mid * k;     int ans = n1 / k * (k - 1) + n1 % k;     if (ans >= m1) return true;     return false;   }   int main() {     scanf( %d%d%d , &n, &m, &k);     int l = 0, r = m / k;     int mid;     while (l < r) {       mid = (l + r) / 2;       if (good(mid))         r = mid;       else         l = mid + 1;     }     Mat u;     u.e[0][0] = (long long)k;     u.e[0][1] = 1L;     u.e[1][0] = 0;     u.e[1][1] = 0;     Mat v;     v.e[0][0] = 2L;     v.e[0][1] = 0;     v.e[1][0] = (long long)k;     v.e[1][1] = 1L;     u = u.mul(v.powMat(v, l));     long long ans = (u.e[0][0] - k + (m - l * k) + mod) % mod;     cout << ans;     return 0;   }