#include <bits/stdc++.h>   const int32_t MAX_N = 1000;   bool isInA[MAX_N + 5], isInB[MAX_N + 5], isVisited[MAX_N + 5];   std::vector<int32_t> v[MAX_N + 5];   int32_t Dfs(int32_t nd) {     isVisited[nd] = true;     if (isInA[nd]) {       return nd;     }     int32_t ans = INT32_MAX;     for (auto &x : v[nd]) {       if (!isVisited[x]) {         ans = std::min(ans, Dfs(x));       }     }     return ans;   }   int main() {     std::ios_base::sync_with_stdio(false);     std::cin.tie(nullptr);     int32_t cntTests;     std::cin >> cntTests;     for (int32_t cs = 1; cs <= cntTests; cs++) {       int32_t n;       std::cin >> n;       for (int32_t i = 1; i <= n; i++) {         v[i].clear();       }       for (int32_t i = 0; i < n - 1; i++) {         int32_t a, b;         std::cin >> a >> b;         v[a].push_back(b);         v[b].push_back(a);       }       memset(isInA, 0, sizeof(isInA));       memset(isInB, 0, sizeof(isInB));       int32_t k1;       std::cin >> k1;       std::vector<int32_t> v1(k1);       for (int32_t i = 0; i < k1; i++) {         std::cin >> v1[i];         isInA[v1[i]] = true;       }       int32_t k2;       std::cin >> k2;       std::vector<int32_t> v2(k2);       for (int32_t i = 0; i < k2; i++) {         std::cin >> v2[i];         isInB[v2[i]] = true;       }       std::cout <<  B   << v2[0] <<   n  << std::flush;       int32_t stNode;       std::cin >> stNode;       memset(isVisited, 0, sizeof(isVisited));       int32_t target = Dfs(stNode);       std::cout <<  A   << target <<   n  << std::flush;       int32_t res;       std::cin >> res;       if (isInB[res]) {         std::cout <<  C   << target <<   n  << std::flush;       } else {         std::cout <<  C   << -1 <<   n  << std::flush;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int a[100000], b[100000], n, i, x, y;   int main() {     cin >> n;     for (i = 0; i < n; i++) {       cin >> a[i];       b[i] = a[i];     }     sort(b, b + n);     for (i = 0; i < n; i++) {       if (a[i] != b[i]) {         x = i;         break;       }     }     for (i = n - 1; i >= 0; i--) {       if (a[i] != b[i]) {         y = i;         break;       }     }     reverse(a + x, a + y + 1);     for (i = 0; i < n; i++) {       if (a[i] != b[i]) {         cout <<  no ;         return 0;       }     }     cout <<  yes  << endl << x + 1 <<     << y + 1;   }


#include<bits/stdc++.h>   #define ll long long int   #define mk make_pair   #define pb push_back   #define INF (ll)1e18   #define pii pair<ll,ll>   #define mod 1000000007 //998244353   #define f(i,a,b) for(ll i=a;i<b;i++)   #define fb(i,a,b) for(ll i=a;i>b;i--)   #define ff first   #define ss second   #define srt(v)  if(!v.empty())sort(v.begin(),v.end())   #define rev(v)  if(!v.empty())reverse(v.begin(),v.end())   #define PI 3.141592653589793238   #define pqr priority_queue<ll,vector<ll>,greater<ll>()>   using namespace std;   ll pow_mod(ll a,ll b)   {    ll res=1;    while(b!=0)    {     if(b&1)     {      res=(res*a)%mod;     }     a=(a*a)%mod;     b/=2;    }    return res;   }      ll calc(vector<ll> &a){       ll sz=a.size();       ll ans=0;              for(ll i=1;i<sz;i++){           ll j=i;                      while(j<sz&&a[j]==a[i])++j;                      i=--j;           ++ans;       }              return ans;   }   void solve()   {     ll n;     cin>>n;          ll a[n];          for(ll i=0;i<n;i++){         cin>>a[i];     }          vector<ll>v1,v2;          vector<ll>v[n+1];          for(ll i=1;i<=n;i++){         v[i].pb(INF);     }           for(ll i=n-1;i>=0;i--){         v[a[i]].pb(i);     }                   v1.pb(-1);     v2.pb(-1);               for(ll i=0;i<n;i++){         v[a[i]].pop_back();                  if(v1.back()==a[i]&&v2.back()==a[i]){              v1.pb(a[i]);                 }         else if(v1.back()==a[i]){             v1.pb(a[i]);         }         else if(v2.back()==a[i]){             v2.pb(a[i]);         }         else if(v1.back()==-1)         {             v1.pb(a[i]);         }         else if(v2.back()==-1){             v2.pb(a[i]);         }         else         {                             ll x=v[v1.back()].back();             ll y=v[v2.back()].back();                          if(x<y){                 v2.pb(a[i]);             }             else             v1.pb(a[i]);         }     }              ll ans=calc(v1)+calc(v2);          cout<<ans<<endl;   }   int main()   {    ios_base::sync_with_stdio(false);        cin.tie(NULL);        cout.tie(NULL);           //Start from Here.        ll t;     t=1;   // cin>>t;     while(t--)     solve();        //Good Bye!       return 0;   }


#include <bits/stdc++.h>   using namespace std;   int k, n, a[101];   int main() {     ios::sync_with_stdio(0);     cin >> n >> k;     for (int i = (0), _b = (n); i < _b; i++) cin >> a[i];     sort(a, a + n);     cout << a[n - k];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> P;   int m, n;   void bs() {     int lo = 1, hi = m, ans = 0;     int cnt = 0;     while (lo <= hi) {       int mid = (lo + hi) / 2;       cout << mid << endl;       int t;       cin >> t;       if (P[cnt % n] == 0) {         if (t == 0) {           exit(0);         }         if (t == -1) {           lo = mid + 1;         }         if (t == 1) {           hi = mid - 1;         }       }       if (P[cnt % n] == 1) {         if (t == 0) {           exit(0);         }         if (t == 1) {           lo = mid + 1;         }         if (t == -1) {           hi = mid - 1;         }       }       cnt++;     }   }   int main() {     cin >> m >> n;     P = vector<int>(n);     for (int i = 0; i < n; i++) {       cout << 1 << endl;       int t;       cin >> t;       if (t == 0 || t == -2) exit(0);       if (t == 1)         P[i] = 1;       else         P[i] = 0;     }     bs();     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize(2)   #pragma GCC optimize(3)   #pragma GCC optimize( Ofast )   using namespace std;   namespace ywy {   char v[300][300];   long long dk1[300], dk2[300];   int dp[600];   typedef struct _p {     long long h1, h2;     _p() {       h1 = 2333;       h2 = 6663;     }     friend bool operator==(const _p &a, const _p &b) {       return (a.h1 == b.h1 && a.h2 == b.h2);     }     friend bool operator!=(const _p &a, const _p &b) {       return ((a.h1 != b.h1) || (a.h2 != b.h2));     }     inline void insert(char x) {       h1 += dk1[x -  a  + 1];       h1 %= 998244353;       h2 += dk2[x -  a  + 1];       h2 %= 1000000007;     }   } pair;   pair str[600], real[600];   int s[300];   inline int solve(int n) {     int ptr = 0, mid = 1, ans = 0;     for (register int i = 2; i <= n; i++) {       if (mid + ptr < i)         mid = i, ptr = 0, dp[i] = 0;       else         dp[i] = min(mid + ptr - i, dp[mid * 2 - i]);       while (dp[i] < min(n - i, i - 1) &&              str[i + dp[i] + 1] == str[i - dp[i] - 1])         dp[i]++;       if (i + dp[i] >= mid + ptr) mid = i, ptr = dp[i];       if ((i & 1) == 0 && (s[i >> 1] != (s[i >> 1] & -s[i >> 1]))) continue;       ans += (dp[i] + 1) / 2;     }     return (ans);   }   void ywymain() {     int n, m;     cin >> n >> m;     for (register int i = 1; i <= n; i++) {       for (register int j = 1; j <= m; j++) cin >> v[i][j];     }     dk1[0] = dk2[0] = 1;     for (register int i = 1; i <= 26; i++) {       dk1[i] = (dk1[i - 1] * 233) % 998244353;       dk2[i] = (dk2[i - 1] * 233) % 1000000007;     }     int ans = 0;     for (register int l = 1; l <= m; l++) {       memset(s, 0, sizeof(s));       for (register int i = 1; i <= n * 2 + 1; i++) str[i] = real[i] = _p();       for (register int r = l; r <= m; r++) {         for (register int i = 1; i <= n; i++) {           s[i] ^= (1 << (v[i][r] - 1));           real[i].insert(v[i][r]);           if (s[i] != (s[i] & -s[i])) {             str[i * 2].h1 = i + 19260817;             str[i * 2].h2 = 19260817 - i;           } else             str[i * 2] = real[i];         }         ans += solve(n * 2 + 1);       }     }     cout << ans << endl;   }   }  // namespace ywy   int main() {     ywy::ywymain();     return (0);   }


#include <bits/stdc++.h>   using namespace std;   const double PI = acos(0) * 2;   const double EPS = 1e-8;   const long long MOD = 1e9 + 7;   const int MAXN = 1e5 + 5;   const int oo = 1e9;   const double foo = 1e30;   template <class T>   int getbit(T s, int i) {     return (s >> i) & 1;   }   template <class T>   T onbit(T s, int i) {     return s | (T(1) << i);   }   template <class T>   T offbit(T s, int i) {     return s & (~(T(1) << i));   }   template <class T>   int cntbit(T s) {     return __builtin_popcounll(s);   }   template <class T>   T sqr(T x) {     return x * x;   }   inline void addmod(int& a, int val, int p = MOD) {     if ((a = (a + val)) >= p) a -= p;   }   inline void submod(int& a, int val, int p = MOD) {     if ((a = (a - val)) < 0) a += p;   }   inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; }   int d, n, m, vs[MAXN], cnt[4], has[MAXN][4];   vector<pair<int, int> > dX, dY;   int main() {     cin >> d;     cin >> n >> m;     for (int i = 0; i < d; i++) {       int x, y;       cin >> x >> y;       dX.push_back(make_pair(x, i));       dY.push_back(make_pair(y, i));       cin >> x >> y;       dX.push_back(make_pair(x, i));       dY.push_back(make_pair(y, i));     }     for (int i = 0; i < 4; i++) cin >> cnt[i];     sort(dX.begin(), dX.end());     sort(dY.begin(), dY.end());     int pass = 0;     memset(vs, 0, sizeof(vs));     int tot = 0;     for (int i = 0; i < ((int)(dX).size()); i++) {       if (i && dX[i].first != dX[i - 1].first) {         tot += pass;         pass = 0;       }       if (!vs[dX[i].second]) {         pass++;         vs[dX[i].second] = 1;         has[dX[i].second][0] = max(has[dX[i].second][0], tot);       } else {         has[dX[i].second][0] = max(has[dX[i].second][0], tot - 1);       }     }     pass = 0;     memset(vs, 0, sizeof(vs));     tot = 0;     for (int i = ((int)(dX).size()) - 1; i >= 0; i--) {       if (i != ((int)(dX).size()) - 1 && dX[i].first != dX[i + 1].first) {         tot += pass;         pass = 0;       }       if (!vs[dX[i].second]) {         pass++;         vs[dX[i].second] = 1;         has[dX[i].second][1] = max(has[dX[i].second][1], tot);       } else {         has[dX[i].second][1] = max(has[dX[i].second][1], tot - 1);       }     }     pass = 0;     memset(vs, 0, sizeof(vs));     tot = 0;     for (int i = 0; i < ((int)(dY).size()); i++) {       if (i && dY[i].first != dY[i - 1].first) {         tot += pass;         pass = 0;       }       if (!vs[dY[i].second]) {         pass++;         vs[dY[i].second] = 1;         has[dY[i].second][2] = max(has[dY[i].second][2], tot);       } else {         has[dY[i].second][2] = max(has[dY[i].second][2], tot - 1);       }     }     pass = 0;     memset(vs, 0, sizeof(vs));     tot = 0;     for (int i = ((int)(dY).size()) - 1; i >= 0; i--) {       if (i != ((int)(dY).size()) - 1 && dY[i].first != dY[i + 1].first) {         tot += pass;         pass = 0;       }       if (!vs[dY[i].second]) {         pass++;         vs[dY[i].second] = 1;         has[dY[i].second][3] = max(has[dY[i].second][3], tot);       } else {         has[dY[i].second][3] = max(has[dY[i].second][3], tot - 1);       }     }     for (int i = 0; i < d; i++) {       int check = 1;       for (int j = 0; j < 4; j++)         if (has[i][j] != cnt[j]) {           check = 0;           break;         }       if (check) {         cout << i + 1;         return 0;       }     }     cout << -1;     return 0;   }


#include <bits/stdc++.h>   inline long long MAX2(long long a, long long int b) {     return (a) > (b) ? (a) : (b);   }   inline long long MAX3(long long a, long long b, long long c) {     return (a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c));   }   inline long long MIN2(long long a, long long b) {     return (a) < (b) ? (a) : (b);   }   inline long long MIN3(long long a, long long b, long long c) {     return (a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c));   }   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int q;     cin >> q;     while (q--) {       int b, w;       cin >> b >> w;       vector<pair<int, int> > co;       if (b > w) {         if (b > 3 * w + 1) {           cout <<  NO n ;           continue;         }         int x = 1e5 + 1, y = 1e5 + 1;         int i = 0;         while (i < w - 1) {           co.push_back(pair<int, int>(x, y));           y++;           co.push_back(pair<int, int>(x, y));           y++;           i++;         }         co.push_back(pair<int, int>(x, y));         y++;         int si = co.size();         int req = b - w + 1;         i = 0;         while (i < si && req > 0) {           co.push_back(pair<int, int>(co[i].first - 1, co[i].second));           req--;           if (req == 0) break;           co.push_back(pair<int, int>(co[i].first + 1, co[i].second));           req--;           i += 2;         }         if (req > 0) {           co.push_back(pair<int, int>(co[0].first, co[0].second - 1));           req--;         }         if (req > 0) {           co.push_back(               pair<int, int>(co[2 * (w - 1)].first, co[2 * (w - 1)].second + 1));           req--;         }       } else {         if (w > 3 * b + 1) {           cout <<  NO n ;           continue;         }         int x = 1e5, y = 1e5 + 1;         int i = 0;         while (i < b - 1) {           co.push_back(pair<int, int>(x, y));           y++;           co.push_back(pair<int, int>(x, y));           y++;           i++;         }         co.push_back(pair<int, int>(x, y));         y++;         int si = co.size();         int req = w - b + 1;         i = 0;         while (i < si && req > 0) {           co.push_back(pair<int, int>(co[i].first - 1, co[i].second));           req--;           if (req == 0) break;           co.push_back(pair<int, int>(co[i].first + 1, co[i].second));           req--;           i += 2;         }         if (req > 0) {           co.push_back(pair<int, int>(co[0].first, co[0].second - 1));           req--;         }         if (req > 0) {           co.push_back(               pair<int, int>(co[2 * (b - 1)].first, co[2 * (b - 1)].second + 1));           req--;         }       }       cout <<  YES n ;       int i, si = co.size();       for (i = 0; i < si; i++) cout << co[i].first <<     << co[i].second <<   n ;     }     cerr <<   n           <<  Time elapsed :   << clock() * 1000.0 / CLOCKS_PER_SEC <<   ms           <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1000000007;   typedef struct Cell {     int i;     int j;     int t;   } cell;   int n, k;   bool cmp(pair<int, int> a, pair<int, int> b) {     if (a.first < b.first)       return true;     else if (a.first == b.first && a.second < b.second)       return true;     return false;   }   bool check(int i, int j, pair<int, pair<int, int>> times[], int t) {     bool cond = false;     for (int tr = 0; tr < k; tr++) {       if (i == times[tr].first && j >= times[tr].second.first - 2 * t &&           j <= times[tr].second.second - 2 * t) {         cond = true;         break;       }     }     return cond;   }   int vis[3][100][101];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     int cases;     cin >> cases;     for (int T = 0; T < cases; T++) {       cin >> n >> k;       string arr[3];       for (int i = 0; i < 3; i++) {         cin >> arr[i];       }       int hi = -1, hj = -1;       for (int i = 0; i < 3; i++) {         for (int j = 0; j < n; j++) {           if (arr[i][j] ==  s ) {             hi = i;             hj = j;             break;           }         }         if (hi != -1) {           break;         }       }       pair<int, pair<int, int>> times[k];       int ind = 0;       for (int i = 0; i < 3; i++) {         for (int j = 0; j < n; j++) {           for (int t = 0; t < 101; t++) {             vis[i][j][t] = 0;           }         }       }       for (int i = 0; i < 3; i++)         for (int j = 0; j < n; j++) {           if (arr[i][j] >=  A  && arr[i][j] <=  Z ) {             int s = j;             int k = j;             while (k < n && arr[i][k] == arr[i][j]) k++;             int e = k - 1;             j = e;             times[ind].second.first = s;             times[ind].second.second = e;             times[ind].first = i;             ind++;           }         }       queue<cell> q;       cell node;       node.i = hi;       node.j = hj;       node.t = 0;       q.push(node);       bool cond = false;       while (q.size() > 0) {         auto p = q.front();         q.pop();         int i = p.i;         int j = p.j;         int t = p.t;         if (j == n - 1) {           cond = true;           break;         } else {           if (check(i, j, times, t)) continue;         }         if (j + 1 < n && !check(i, j + 1, times, t)) {           cell node;           node.i = i;           node.j = j + 1;           node.t = t + 1;           if (vis[i][j + 1][t + 1] == 0) {             q.push(node);             vis[i][j + 1][t + 1] = 1;           }           if ((i + 1 < 3) && (!check(i + 1, j + 1, times, t))) {             cell node;             node.i = i + 1;             node.j = j + 1;             node.t = t + 1;             if ((vis[i + 1][j + 1][t + 1] == 0)) {               vis[i + 1][j + 1][t + 1] = 1;               q.push(node);             }           }           if (i - 1 >= 0 && !check(i - 1, j + 1, times, t)) {             cell node;             node.i = i - 1;             node.j = j + 1;             node.t = t + 1;             if (vis[i - 1][j + 1][t + 1] == 0) {               vis[i - 1][j + 1][t + 1] = 1;               q.push(node);             }           }         }       }       if (cond)         cout <<  YES  << endl;       else         cout <<  NO  << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 200005;   inline int read() {     register int x = 0, w = 1, ch = getchar();     for (; ch <  0  || ch >  9 ; ch = getchar())       if (ch ==  - ) w = -1;     for (; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch - 48;     return w * x;   }   int n, m, k, tot1, tot2, tot3, cnt;   long long s[maxn];   struct node {     int opt, id, x;     long long v, w;   } m1[maxn], m2[maxn], m3[maxn], ans[maxn];   inline bool cmp1(node a, node b) {     return (a.x != b.x) ? (a.x < b.x) : (a.v < b.v);   }   inline bool cmp2(node a, node b) {     return (a.x != b.x) ? (a.x < b.x) : (a.v > b.v);   }   inline bool cmp3(node a, node b) { return a.opt < b.opt; }   inline bool cmp4(node a, node b) { return a.v * b.w > a.w * b.v; }   int main() {     n = read(), m = read(), k = read();     for (int i = 1; i <= n; i++) s[i] = read();     for (int i = 1; i <= m; i++) {       int a = read(), b = read(), c = read();       if (a == 1 && c > s[b])         m1[++tot1] = (node){1, i, b, c, 1};       else if (a == 2)         m2[++tot2] = (node){2, i, b, c, 1};       else if (a == 3)         m3[++tot3] = (node){3, i, b, c, 1};     }     sort(m1 + 1, m1 + tot1 + 1, cmp1);     int tmp = 0;     for (int i = 1; i <= tot1; i++)       if (m1[i].x != m1[i + 1].x) m1[++tmp] = m1[i];     tot1 = tmp;     for (int i = 1; i <= tot1; i++)       m1[i].v = m1[i].v - s[m1[i].x], m2[++tot2] = m1[i];     sort(m2 + 1, m2 + tot2 + 1, cmp2);     long long sum = 0;     for (int i = 1; i <= tot2; i++) {       if (m2[i].x != m2[i - 1].x) sum = s[m2[i].x];       m2[i].w = sum, sum += m2[i].v;     }     for (int i = 1; i <= tot3; i++) m3[i].v--;     for (int i = 1; i <= tot2; i++) m3[++tot3] = m2[i];     sort(m3 + 1, m3 + tot3 + 1, cmp4);     tmp = min(tot3, k);     for (int i = 1; i <= tmp; i++) ans[++cnt] = m3[i];     printf( %d n , cnt);     sort(ans + 1, ans + cnt + 1, cmp3);     for (int i = 1; i <= cnt; i++) printf( %d  , ans[i].id);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k;   struct kni {     int p, c, id;     long long ans;   } a[100005];   long long s[100005];   inline char nc() {     static char buf[10001], *p1 = buf, *p2 = buf;     return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 10000, stdin), p1 == p2)                ? EOF                : *p1++;   }   inline int read() {     register int s = 0, w = 1;     register char c = nc();     while (c <  0  || c >  9 ) {       if (c ==  - ) w = -1;       c = nc();     }     while (c >=  0  && c <=  9 ) s = (s << 1) + (s << 3) + c - 48, c = nc();     return s * w;   }   inline void print(long long x) {     if (x < 0) putchar( - ), x = -x;     if (x > 9) print(x / 10);     putchar(x % 10 + 48);   }   inline bool cmp(const kni &a, const kni &b) { return a.p < b.p; }   inline bool cmp2(const kni &a, const kni &b) { return a.id < b.id; }   int main() {     n = read();     k = read();     for (int i = 1; i <= n; ++i) a[i].p = read(), a[i].id = i;     for (int i = 1; i <= n; ++i) a[i].c = read();     sort(a + 1, a + n + 1, cmp);     priority_queue<long long, vector<long long>, greater<long long> > q;     for (int i = 1; i <= n; ++i) {       if (i <= k)         q.push(a[i].c), s[i] = s[i - 1] + a[i].c;       else if (k) {         if (q.top() < a[i].c) {           s[i] = s[i - 1] + a[i].c - q.top();           q.pop();           q.push(a[i].c);         } else           s[i] = s[i - 1];       }       a[i].ans = a[i].c + s[i - 1];     }     sort(a + 1, a + n + 1, cmp2);     for (int i = 1; i <= n; ++i) print(a[i].ans), putchar(   );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   namespace TYC {   const int N = 17, MX = (1 << 13) + 100, Q = 110;   int n, m, q, edge[N][N], a[Q], b[Q], lca[Q], bin[N], from[N], to[N];   long long dp[N][MX];   bool intree(int tree, int node) { return tree & bin[node]; }   long long dfs(int root, int tree) {     long long &ans = dp[root][tree];     if (~ans) return ans;     ans = 0;     int sub = tree ^ bin[root];     int x = 1;     for (; x <= n; x++)       if (intree(sub, x)) break;     for (int Subtree = sub; Subtree; Subtree = (Subtree - 1) & sub) {       if (!intree(Subtree, x)) continue;       bool flag = false;       for (int i = 1; i <= m; i++)         if (from[i] != root && to[i] != root &&             intree(Subtree, from[i]) != intree(Subtree, to[i])) {           flag = true;           break;         }       if (flag) continue;       for (int i = 1; i <= q; i++) {         if (lca[i] == root && intree(Subtree, a[i]) && intree(Subtree, b[i])) {           flag = true;           break;         }         if (intree(Subtree, lca[i]) &&             (!intree(Subtree, a[i]) || !intree(Subtree, b[i]))) {           flag = true;           break;         }       }       if (flag) continue;       int num = 0, son;       for (int i = 1; i <= n; i++)         if (intree(Subtree, i) && edge[root][i]) num++, son = i;       if (num > 1) continue;       if (!num) {         for (int i = 1; i <= n; i++)           if (intree(Subtree, i))             ans += dfs(i, Subtree) * dfs(root, tree ^ Subtree);       } else         ans += dfs(son, Subtree) * dfs(root, tree ^ Subtree);     }     return ans;   }   void work() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n >> m >> q;     int u, v;     for (int i = 1; i <= m; i++) {       cin >> u >> v;       edge[u][v] = edge[v][u] = 1;       from[i] = u, to[i] = v;     }     for (int i = 1; i <= q; i++) cin >> a[i] >> b[i] >> lca[i];     bin[1] = 1;     for (int i = 2; i <= n; i++) bin[i] = bin[i - 1] << 1;     memset(dp, -1, sizeof(dp));     for (int i = 1; i <= n; i++) dp[i][bin[i]] = 1;     cout << dfs(1, (1 << n) - 1);   }   }  // namespace TYC   int main() {     TYC::work();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using vi = vector<int>;   using vvi = vector<vi>;   using vll = vector<ll>;   using vvll = vector<vll>;   using vb = vector<bool>;   using vd = vector<double>;   using vs = vector<string>;   using pii = pair<int, int>;   using pll = pair<ll, ll>;   using pdd = pair<double, double>;   using vpii = vector<pii>;   using vvpii = vector<vpii>;   using vpll = vector<pll>;   using vvpll = vector<vpll>;   using vpdd = vector<pdd>;   using vvpdd = vector<vpdd>;   template <typename T>   void ckmin(T& a, const T& b) {     a = min(a, b);   }   template <typename T>   void ckmax(T& a, const T& b) {     a = max(a, b);   }   mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());   namespace __input {   template <class T1, class T2>   void re(pair<T1, T2>& p);   template <class T>   void re(vector<T>& a);   template <class T, size_t SZ>   void re(array<T, SZ>& a);   template <class T>   void re(T& x) {     cin >> x;   }   void re(double& x) {     string t;     re(t);     x = stod(t);   }   template <class Arg, class... Args>   void re(Arg& first, Args&... rest) {     re(first);     re(rest...);   }   template <class T1, class T2>   void re(pair<T1, T2>& p) {     re(p.first, p.second);   }   template <class T>   void re(vector<T>& a) {     for (int i = 0; i < (int((a).size())); i++) re(a[i]);   }   template <class T, size_t SZ>   void re(array<T, SZ>& a) {     for (int i = 0; i < (SZ); i++) re(a[i]);   }   }  // namespace __input   using namespace __input;   namespace __output {   template <class T1, class T2>   void pr(const pair<T1, T2>& x);   template <class T, size_t SZ>   void pr(const array<T, SZ>& x);   template <class T>   void pr(const vector<T>& x);   template <class T>   void pr(const deque<T>& x);   template <class T>   void pr(const set<T>& x);   template <class T1, class T2>   void pr(const map<T1, T2>& x);   template <class T>   void pr(const T& x) {     cout << x;   }   template <class Arg, class... Args>   void pr(const Arg& first, const Args&... rest) {     pr(first);     pr(rest...);   }   template <class T1, class T2>   void pr(const pair<T1, T2>& x) {     pr( { , x.first,  ,  , x.second,  } );   }   template <class T, bool pretty = true>   void prContain(const T& x) {     if (pretty) pr( { );     bool fst = 1;     for (const auto& a : x) pr(!fst ? pretty ?  ,   :     :   , a), fst = 0;     if (pretty) pr( } );   }   template <class T>   void pc(const T& x) {     prContain<T, false>(x);     pr(  n );   }   template <class T, size_t SZ>   void pr(const array<T, SZ>& x) {     prContain(x);   }   template <class T>   void pr(const vector<T>& x) {     prContain(x);   }   template <class T>   void pr(const deque<T>& x) {     prContain(x);   }   template <class T>   void pr(const set<T>& x) {     prContain(x);   }   template <class T1, class T2>   void pr(const map<T1, T2>& x) {     prContain(x);   }   void ps() { pr(  n ); }   template <class Arg>   void ps(const Arg& first) {     pr(first);     ps();   }   template <class Arg, class... Args>   void ps(const Arg& first, const Args&... rest) {     pr(first,    );     ps(rest...);   }   }  // namespace __output   using namespace __output;   namespace __algorithm {   template <typename T>   void dedup(vector<T>& v) {     sort((v).begin(), (v).end());     v.erase(unique((v).begin(), (v).end()), v.end());   }   template <typename T>   typename vector<T>::iterator find(vector<T>& v, const T& x) {     auto it = lower_bound((v).begin(), (v).end(), x);     return it != v.end() && *it == x ? it : v.end();   }   template <typename T>   size_t index(vector<T>& v, const T& x) {     auto it = find(v, x);     assert(it != v.end() && *it == x);     return it - v.begin();   }   template <typename C, typename T, typename OP>   vector<T> prefixes(const C& v, T id, OP op) {     vector<T> r(int((v).size()) + 1, id);     for (int i = 0; i < (int((v).size())); i++) r[i + 1] = op(r[i], v[i]);     return r;   }   template <typename C, typename T, typename OP>   vector<T> suffixes(const C& v, T id, OP op) {     vector<T> r(int((v).size()) + 1, id);     for (int i = (int((v).size())) - 1; i >= 0; i--) r[i] = op(v[i], r[i + 1]);     return r;   }   }  // namespace __algorithm   using namespace __algorithm;   struct monostate {     friend istream& operator>>(istream& is,                                const __attribute__((unused)) monostate& ms) {       return is;     }     friend ostream& operator<<(ostream& os,                                const __attribute__((unused)) monostate& ms) {       return os;     }   } ms;   template <typename W = monostate>   struct wedge {     int u, v, i;     W w;     wedge<W>(int _u = -1, int _v = -1, int _i = -1) : u(_u), v(_v), i(_i) {}     int operator[](int loc) const { return u ^ v ^ loc; }     friend void re(wedge& e) {       re(e.u, e.v, e.w);       --e.u, --e.v;     }     friend void pr(const wedge& e) { pr(e.u,  <- , e.w,  -> , e.v); }   };   namespace __io {   void setIn(string second) { freopen(second.c_str(),  r , stdin); }   void setOut(string second) { freopen(second.c_str(),  w , stdout); }   void setIO(string second =   ) {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.precision(15);     if (int((second).size())) {       setIn(second +  .in ), setOut(second +  .out );     }   }   }  // namespace __io   using namespace __io;   struct uf_monostate {     uf_monostate(__attribute__((unused)) int id) {}     void merge(__attribute__((unused)) uf_monostate& o,                __attribute__((unused)) const monostate& e) {}   };   template <typename T = uf_monostate, typename E = monostate>   struct union_find {     struct node {       int par, rnk, size;       T state;       node(int id = 0) : par(id), rnk(0), size(1), state(id) {}       void merge(node& o, E& e) {         if (rnk == o.rnk) rnk++;         if (size < o.size) swap(state, o.state);         size += o.size;         state.merge(o.state, e);       }     };     int cc;     vector<node> uf;     union_find(int N = 0) : uf(N), cc(N) {       for (int i = 0; i < N; i++) uf[i] = node(i);     }     int rep(int i) {       if (i != uf[i].par) uf[i].par = rep(uf[i].par);       return uf[i].par;     }     bool unio(int a, int b, E& e = ms) {       a = rep(a), b = rep(b);       if (a == b) return false;       if (uf[a].rnk < uf[b].rnk) swap(a, b);       uf[a].merge(uf[b], e);       uf[b].par = a;       cc--;       return true;     }     T& state(int i) { return uf[rep(i)].state; }   };   int main() {     setIO();     int N, S;     re(N, S);     vi a(N);     re(a);     vi ti(N);     vi st = a, did(N);     sort((st).begin(), (st).end());     vvi occ(N);     for (int i = 0; i < (N); i++)       if (a[i] != st[i]) {         int w = index(st, a[i]);         while (a[w + did[w]] == st[w + did[w]]) did[w]++;         ti[i] = w + did[w]++;         occ[w].push_back(i);       } else         ti[i] = i;     vb vis(N);     union_find<> uf(N);     for (int i = 0; i < (N); i++)       if (!vis[i]) {         vis[i] = true;         for (int t = ti[i]; t != i; t = ti[t]) {           uf.unio(i, t);           vis[t] = true;         }       }     for (int i = 0; i < (N); i++)       for (int j = 0; j < (int((occ[i]).size()) - 1); j++) {         if (uf.unio(occ[i][j], occ[i][j + 1])) {           swap(ti[occ[i][j]], ti[occ[i][j + 1]]);         }       }     int wr = 0;     for (int i = 0; i < (N); i++)       if (a[i] != st[i]) wr++;     if (wr > S) {       ps(-1);       return 0;     }     if (a == st) {       ps(0);       return 0;     }     vvi cyc;     for (int i = 0; i < (N); i++)       if (i == uf.rep(i) && i != ti[i]) {         cyc.push_back({i + 1});         for (int t = ti[i]; t != i; t = ti[t]) cyc.back().push_back(t + 1);       }     int merge = min(S - wr, int((cyc).size()));     if (merge > 2) {       vi loop, fix;       for (int c = (int((cyc).size()) - merge); c < (int((cyc).size())); c++) {         loop.insert(loop.end(), (cyc[c]).begin(), (cyc[c]).end());         fix.push_back(cyc[c].front());       }       reverse((fix).begin(), (fix).end());       cyc.erase(cyc.end() - merge, cyc.end());       cyc.push_back(loop);       cyc.push_back(fix);     }     ps(int((cyc).size()));     for (auto& c : cyc) ps(int((c).size())), pc(c);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = (int)1e5 + 100;   long long a[N];   int ansL[N], ansR[N];   int st[N];   long long diff[N];   long long solve(int x, int y) {     long long ans = 0;     int r = 0;     for (int i = x; i <= y - 1; i++) {       while (r > 0 && diff[st[r - 1]] < diff[i]) r--;       ansL[i] = (r == 0 ? x : st[r - 1] + 1);       st[r++] = i;     }     r = 0;     for (int i = y - 1; i >= x; i--) {       while (r > 0 && diff[st[r - 1]] <= diff[i]) r--;       ansR[i] = (r == 0 ? y : st[r - 1]);       st[r++] = i;     }     for (int i = x; i < y; i++) {       ans += 1LL * (i - ansL[i] + 1) * (ansR[i] - i) * diff[i];     }     return ans;   }   int main() {     int n, q;     scanf( %d%d , &n, &q);     for (int i = 0; i < n; i++) scanf( %lld , &a[i]);     for (int i = 0; i < n - 1; i++) diff[i] = llabs(a[i + 1] - a[i]);     for (int i = 0; i < q; i++) {       int l, r;       scanf( %d%d , &l, &r);       printf( %lld n , solve(l - 1, r - 1));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int64_t M = 1e2 + 5;   int64_t n, t[M], w[M], dp[M][20005], totalW;   int64_t solve(int64_t idx, int64_t wt) {     if (idx > n) {       if (wt >= 0) return 0;       return 1e9;     }     if (dp[idx][1001 + wt] != -1) return dp[idx][1001 + wt];     int64_t ans = t[idx] + solve(idx + 1, wt + t[idx]);     ans = min(ans, solve(idx + 1, wt - w[idx]));     return dp[idx][1001 + wt] = ans;   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     for (int64_t i = 1; i <= n; i++) cin >> t[i] >> w[i];     memset(dp, -1, sizeof(dp));     cout << solve(1, 0);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     pair<int, pair<int, int> > k[n];     for (int i = 0; i < n; i++) {       cin >> k[i].second.first >> k[i].second.second;       k[i].first = abs(k[i].second.first) + abs(k[i].second.second);     }     sort(k, k + n);     int j = 0;     j += 2 * n;     for (int i = 0; i < n; i++) {       if (k[i].second.first != 0) {         j += 2;       }       if (k[i].second.second != 0) {         j += 2;       }     }     cout << j << endl;     for (int i = 0; i < n; i++) {       if (k[i].second.first > 0) {         cout <<  1   << k[i].second.first <<   R  << endl;       } else if (k[i].second.first < 0) {         cout <<  1   << -k[i].second.first <<   L  << endl;       }       if (k[i].second.second > 0) {         cout <<  1   << k[i].second.second <<   U  << endl;       } else if (k[i].second.second < 0) {         cout <<  1   << -k[i].second.second <<   D  << endl;       }       cout << 2 << endl;       if (k[i].second.first > 0) {         cout <<  1   << k[i].second.first <<   L  << endl;       } else if (k[i].second.first < 0) {         cout <<  1   << -k[i].second.first <<   R  << endl;       }       if (k[i].second.second > 0) {         cout <<  1   << k[i].second.second <<   D  << endl;       } else if (k[i].second.second < 0) {         cout <<  1   << -k[i].second.second <<   U  << endl;       }       cout << 3 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 200000;   int n;   string s1, s2;   int main() {     cin >> n >> s1 >> s2;     int K = 7;     long long H = 0, HH = 0, D = 1;     n--;     for (int(i) = (1); i < (n + 1); ++(i)) {       int p = 0, pp = 0;       if (s1[n - i] ==  N ) p = 1;       if (s1[n - i] ==  S ) p = 4;       if (s1[n - i] ==  E ) p = 2;       if (s1[n - i] ==  W ) p = 3;       H = H * K + p;       if (s2[n - i] ==  N ) pp = 4;       if (s2[n - i] ==  S ) pp = 1;       if (s2[n - i] ==  E ) pp = 3;       if (s2[n - i] ==  W ) pp = 2;       HH += D * (pp);       D *= 7;       if (H == HH) {         cout <<  NO ;         return 0;       }     }     cout <<  YES ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n;     cin >> n;     long long int sum1 = (n * (n + 1)) / 2;     if (sum1 % 2 == 0) {       cout <<  0  << endl;     } else {       cout <<  1  << endl;     }   }


#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 a[505], ar[505], num[505], b[505];   bool used[505];   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     long long t;     cin >> t;     while (t--) {       long long n;       cin >> n;       vector<long long> ans;       memset(num, 0, sizeof(num));       memset(used, 0, sizeof(used));       bool swi = false;       long long swa1, swa2;       for (long long i = 0; i < n; i++) cin >> ar[i];       for (long long i = 0; i < n; i++) {         if (num[ar[i]]) {           swi = true;           swa1 = num[ar[i]] - 1, swa2 = i;           break;         }         num[ar[i]] = i + 1;       }       for (long long i = 0; i < n; i++) {         long long p, mi = 501;         for (long long j = 0; j < n; j++)           if (!used[j] && ar[j] < mi) mi = ar[j], p = j;         a[p] = i + 1;         b[p] = i + 1;         used[p] = true;       }       long long N = n;       while (n > 3) {         long long p, ma = 0;         for (long long i = 0; i < n; i++)           if (a[i] > ma) ma = a[i], p = i;         while (p < n - 3) {           ans.push_back(p + 1);           long long tem = a[p + 2];           a[p + 2] = a[p + 1];           a[p + 1] = a[p];           a[p] = tem;           p++;         }         while (p < n - 1) {           ans.push_back(n - 2);           long long tem = a[n - 1];           a[n - 1] = a[n - 2];           a[n - 2] = a[n - 3];           a[n - 3] = tem;           p++;         }         n--;       }       bool tag = false;       for (long long i = 0; i < 3; i++) {         if (a[0] <= a[1] && a[1] <= a[2]) {           tag = true;           break;         }         ans.push_back(1);         long long tem = a[2];         a[2] = a[1];         a[1] = a[0];         a[0] = tem;       }       if (tag) {         cout << ((long long)ans.size()) <<   n ;         for (long long i : ans) cout << i <<    ;         cout <<   n ;         continue;       }       if (!swi) {         cout <<  -1  <<   n ;         continue;       }       ans.clear();       n = N;       swap(b[swa1], b[swa2]);       for (long long i = 0; i < n; i++) a[i] = b[i];       while (n > 3) {         long long p, ma = 0;         for (long long i = 0; i < n; i++)           if (a[i] > ma) ma = a[i], p = i;         while (p < n - 3) {           ans.push_back(p + 1);           long long tem = a[p + 2];           a[p + 2] = a[p + 1];           a[p + 1] = a[p];           a[p] = tem;           p++;         }         while (p < n - 1) {           ans.push_back(n - 2);           long long tem = a[n - 1];           a[n - 1] = a[n - 2];           a[n - 2] = a[n - 3];           a[n - 3] = tem;           p++;         }         n--;       }       for (long long i = 0; i < 3; i++) {         if (a[0] <= a[1] && a[1] <= a[2]) {           tag = true;           break;         }         ans.push_back(1);         long long tem = a[2];         a[2] = a[1];         a[1] = a[0];         a[0] = tem;       }       if (tag) {         cout << ((long long)ans.size()) <<   n ;         for (long long i : ans) cout << i <<    ;         cout <<   n ;       } else         cout <<  -1  <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   #pragma GCC optimize( Ofast )   using namespace std;   long long n, c[2005], dp[2005], ans[2005], pos = 0;   vector<long long> v[2005];   void dfs1(long long nod) {     for (auto it : v[nod]) {       dfs1(it);       dp[nod] += dp[it] + 1;     }   }   void dfs2(long long node) {     for (auto it : v[node]) {       dfs2(it);     }     if (c[node] == dp[node]) {       ans[node] = ++pos;     } else {       ans[node] = pos - (dp[node] - c[node]) + 1;       pos++;       for (long long i = 1; i <= n; i++) {         if (ans[i] >= ans[node] && i != node) {           ans[i]++;         }       }     }   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     cin >> n;     long long root = -1;     for (long long i = 1; i <= n; i++) {       long long x;       cin >> x >> c[i];       if (x) {         v[x].push_back(i);       } else         root = i;     }     dfs1(root);     for (long long i = 1; i <= n; i++) {       if (dp[i] < c[i]) {         cout <<  NO  <<   n ;         return 0;       }     }     dfs2(root);     cout <<  YES  <<   n ;     for (long long i = 1; i <= n; i++) {       cout << ans[i] <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100005;   vector<int> level[MAXN];   vector<pair<int, int>> adj[MAXN];   vector<pair<int, pair<int, int>>> edges;   int dp[MAXN], d[MAXN], prv[MAXN];   bool OnPath[MAXN];   int main() {     int n, m, a, b, c, cnt = 0;     scanf( %d%d , &n, &m);     for (int i = 0; i < m; i++) {       scanf( %d%d%d , &a, &b, &c);       adj[a].push_back(make_pair(b, c));       adj[b].push_back(make_pair(a, c));       edges.push_back({a, {b, c}});       cnt += c;     }     memset(d, -1, sizeof(d));     memset(dp, -1, sizeof(dp));     memset(prv, -1, sizeof(prv));     queue<int> q;     d[1] = dp[1] = 0;     q.push(1);     while (!q.empty()) {       int cur = q.front();       q.pop();       for (auto p : adj[cur]) {         if (d[p.first] == -1) {           d[p.first] = d[cur] + 1;           q.push(p.first);         }       }     }     for (int i = 1; i <= n; i++) level[d[i]].push_back(i);     for (int i = 1; i <= d[n]; i++) {       for (int node : level[i]) {         for (auto p : adj[node]) {           int child = p.first;           if (d[child] == i - 1 && dp[child] + p.second > dp[node]) {             dp[node] = p.second + dp[child];             prv[node] = child;           }         }       }     }     int cur = n;     while (cur != -1) {       OnPath[cur] = 1;       cur = prv[cur];     }     int k = d[n] - dp[n] + cnt - dp[n];     printf( %d n , k);     for (int i = 0; i < m; i++) {       a = edges[i].first, b = edges[i].second.first, c = edges[i].second.second;       if (OnPath[a] && OnPath[b] && !c) printf( %d %d 1 n , a, b);       if ((!OnPath[a] || !OnPath[b]) && c) printf( %d %d 0 n , a, b);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, k, t, t1 = 0;     cin >> n >> k;     cin >> t1;     for (int i = 2; i <= n; i++) {       cin >> t;       t1 += t + 1;     }     if (t1 == k)       cout <<  YES ;     else       cout <<  NO ;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 2147483647;   template <typename T>   inline T read() {     T sum = 0, fl = 1;     char ch = getchar();     for (; !isdigit(ch); ch = getchar())       if (ch ==  - ) fl = -1;     for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch -  0 ;     return sum * fl;   }   const int maxn = 65;   int ans = 0;   long long v[200005];   vector<long long> vec[maxn];   map<long long, int> M;   void Solve() {     int n = read<int>();     for (int i = 0; i <= 62; ++i) M[1ll << i] = i;     for (int i = 1; i <= n; ++i) {       v[i] = read<long long>();       vec[M[v[i] & -v[i]]].push_back(v[i]);     }     for (int i = 0; i <= 62; ++i)       if (vec[ans].size() < vec[i].size()) ans = i;     printf( %d n , n - vec[ans].size());     for (int i = 1; i <= n; ++i)       if (M[v[i] & -v[i]] != ans) printf( %I64d n , v[i]);   }   int main() {     Solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main(void) {     stack<int> tmpRes;     string S;     cin >> S;     for (int i = 0; i < S.length(); i++) {       if (S[i] ==  r )         printf( %d n , i + 1);       else         tmpRes.emplace(i + 1);     }     while (!tmpRes.empty()) {       printf( %d n , tmpRes.top());       tmpRes.pop();     }     return (0);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int hh, mm, i;     while (~scanf( %d:%d , &hh, &mm)) {       mm = mm - 1;       for (i = 0;; i++) {         mm++;         if (mm >= 60) {           mm = mm - 60;           hh++;           if (hh >= 24) hh = 0;         }         if (hh / 10 == mm % 10 && hh % 10 == mm / 10) {           cout << i << endl;           return 0;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   void solve();   int main(int argc, char const *argv[]) {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     solve();   }   int const MAX = 1e6 + 7;   int cnt[MAX];   void solve() {     int n, k;     cin >> n >> k;     int val[n];     for (int i = 0; i < n; i++) cin >> val[i];     int x, y;     int ans = 0;     int l = 0;     int diff = 0;     for (int r = 0; r < n; r++) {       if (++cnt[val[r]] == 1) {         diff++;       }       while (diff > k) {         if (--cnt[val[l]] == 0) diff--;         l++;       }       if (r - l + 1 > ans) {         ans = r - l + 1;         x = l;         y = r;       }     }     cout << x + 1 <<     << y + 1 << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1003, INF = 0x3f3f3f3f;   long long qpow(long long a, long long b) {     long long ans = 1;     while (b) {       if (b & 1) ans = ans * a;       a = a * a;       b >>= 1;     }     return ans;   }   int n, m, k;   int a[N][N];   int ans = 0;   void dfs(int first, int second, int xx, int yy) {     if (first == n + 1) {       ans = yy;       return;     }     if (a[first][second] == 1) {       a[first][second] = 2;       dfs(first, second + 1, first, second);     } else if (a[first][second] == 2) {       dfs(first + 1, second, first, second);     } else {       a[first][second] = 2;       dfs(first, second - 1, first, second);     }   }   int main() {     ios::sync_with_stdio;     cin.tie(0);     cout.tie(0);     cin >> n >> m >> k;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         cin >> a[i][j];       }     }     for (int i = 1; i <= k; i++) {       int first;       cin >> first;       ans = 0;       dfs(1, first, 0, 0);       cout << ans <<    ;     }     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   template <typename T>   T sqr(T x) {     return x * x;   }   template <typename T>   bool exmax(T &a, T b) {     return (b > a) ? (a = b) || 1 : 0;   }   template <typename T>   bool exmin(T &a, T b) {     return (b < a) ? (a = b) || 1 : 0;   }   using namespace std;   int a[510], b[510], l;   void print() {     for (int i = l - 1; i >= 0; i--) cout << a[i];     cout << endl;   }   int getsum(int s, int t) {     int ret = 0;     for (int i = s; i < t; i++) ret += b[i];     return ret;   }   int main() {     memset(a, 0x00, sizeof(a));     int n;     cin >> n;     n--;     int m;     cin >> m;     l = 0;     int t = m;     while (m) {       if (m > 9)         a[l++] = 9, m -= 9;       else         a[l++] = m, m = 0;     }     print();     while (n--) {       cin >> m;       if (t < m) {         for (int i = 0, j = m - t; j; i++) {           if (j > 9 - a[i])             j -= 9 - a[i], a[i] = 9;           else             a[i] += j, j = 0;           exmax(l, i + 1);         }         t = m;       } else {         t = m;         for (int i = 0; true; i++) {           memcpy(b, a, sizeof(a));           b[i]++;           while (b[i] > 9) b[i] = 0, b[++i] += 1;           for (int j = 0; j < i; j++) b[j] = 0;           exmax(l, i + 1);           int s = getsum(i, l);           if (s > m) continue;           if (i * 9 < m - s) continue;           int k = 0;           m -= s;           while (m) {             if (m > 9)               b[k++] = 9, m -= 9;             else               b[k++] = m, m = 0;           }           break;         }         memcpy(a, b, sizeof(a));       }       print();     }     return 0;   }


#include <bits/stdc++.h>   template <typename Tp>   void in(Tp &x) {     char ch = getchar(), f = 1;     x = 0;     while (ch !=  -  && (ch <  0  || ch >  9 )) ch = getchar();     if (ch ==  - ) f = -1, ch = getchar();     while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar();     x *= f;   }   template <typename Tp>   bool chkmax(Tp &x, Tp y) {     return x >= y ? 0 : (x = y, 1);   }   template <typename Tp>   bool chkmin(Tp &x, Tp y) {     return x <= y ? 0 : (x = y, 1);   }   template <typename Tp>   Tp Max(const Tp &x, const Tp &y) {     return x > y ? x : y;   }   template <typename Tp>   Tp Min(const Tp &x, const Tp &y) {     return x < y ? x : y;   }   const int MAXN = 400010;   int n;   int s[MAXN], g[MAXN];   int head[MAXN], nxt[MAXN << 1], data[MAXN << 1], flow[MAXN << 1], cnt;   void add(int x, int y, int z) {     nxt[cnt] = head[x];     data[cnt] = y;     flow[cnt] = z;     head[x] = cnt++;   }   using std::queue;   queue<int> q;   bool instack[MAXN];   long long dis[MAXN];   void spfa() {     memset(dis, 0x3f, sizeof dis);     dis[0] = 0;     instack[0] = true;     q.push(0);     while (!q.empty()) {       int now = q.front();       q.pop();       instack[now] = false;       for (int i = head[now]; i != -1; i = nxt[i]) {         if (dis[data[i]] > dis[now] + flow[i]) {           dis[data[i]] = dis[now] + flow[i];           if (!instack[data[i]]) {             q.push(data[i]);             instack[data[i]] = true;           }         }       }     }     long long ans1 = 0;     for (int i = (1), i_END_ = (n); i <= i_END_; ++i) {       ans1 += dis[i];       if (dis[i] < 0) {         puts( -1 );         return;       }     }     printf( %I64d n , ans1);     for (int i = (1), i_END_ = (n); i <= i_END_; ++i)       printf( %I64d  , dis[i] + s[i]);     putchar(10);   }   int main() {     in(n);     for (int i = (1), i_END_ = (n); i <= i_END_; ++i) in(s[i]), in(g[i]);     memset(head, -1, sizeof head);     for (int i = (1), i_END_ = (n); i <= i_END_; ++i) add(0, i, g[i]);     for (int i = (1), i_END_ = (n - 1); i <= i_END_; ++i) {       int t = s[i] - s[i + 1];       add(i, i + 1, 1 + t);       add(i + 1, i, 1 - t);     }     spfa();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t = 0;     scanf( %d , &t);     for (int qwe = 0; qwe < t; qwe++) {       long long r = 0, l = 0, y = 0;       scanf( %lld%lld , &l, &r);       printf( %lld %lld n , l, l * 2);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long i, j, k;     int n;     cin >> n;     vector<long long> arr(n);     long long sum = 0;     for (i = 0; i < n; i++) {       cin >> arr[i];       sum += arr[i];     }     if (sum % 3 != 0) {       cout << 0 << endl;       return 0;     }     if (n < 3) {       cout << 0 << endl;       return 0;     }     long long s = sum / 3;     vector<int> ans(n, 0);     long long pref = 0;     for (i = n - 1; i >= 0; i--) {       pref += arr[i];       if (pref == s) {         ans[i] = 1;       }     }     for (i = n - 2; i >= 0; i--) {       ans[i] += ans[i + 1];     }     pref = 0;     long long soln = 0;     for (i = 0; i < n - 2; i++) {       pref += arr[i];       if (pref == s) {         soln += ans[i + 2];       }     }     cout << soln << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<vector<int> > numbersWithGrundy(1);   int a[200001], grundyValues[200001];   bool isPrime[200001], isMoveValid[200001];   vector<string> players{ Alice ,  Bob };   void PreProcess(int n, int m) {     vector<int> primes;     for (int i = 2; i <= n; ++i) {       isPrime[i] = 1;     }     for (int i = 2; i <= n; ++i) {       if (!isPrime[i]) {         continue;       }       for (int j = i + i; j <= n; j += i) {         isPrime[j] = 0;       }       primes.push_back(i);       for (int prime : primes) {         if (i * prime > n) {           break;         }         isMoveValid[i * prime] = 1;       }       isMoveValid[i] = 1;     }     isMoveValid[m] = 0;     numbersWithGrundy[0].push_back(0);     for (int i = 1; i <= n; ++i) {       int &k = grundyValues[i];       for (; k < numbersWithGrundy.size(); ++k) {         bool valid = 1;         for (int l = 0, r = numbersWithGrundy[k].size() - 1; l <= r && valid;              ++l, --r) {           valid = !(isMoveValid[i - numbersWithGrundy[k][l]] ||                     isMoveValid[i - numbersWithGrundy[k][r]]);         }         if (valid) {           break;         }       }       if (k >= numbersWithGrundy.size()) {         numbersWithGrundy.resize(k + 1);       }       numbersWithGrundy[k].push_back(i);     }   }   int main() {     int n, k;     scanf( %d%d , &n, &k);     for (int i = 1, j = 0; i <= n; ++i) {       int b, w, r;       scanf( %d%d%d , &b, &w, &r);       a[++j] = w - b - 1;       a[++j] = r - w - 1;     }     n = 2 * n;     int m = 0;     for (int i = 1; i <= n; ++i) {       m = max(m, a[i]);     }     PreProcess(m, k);     int ans = 0;     for (int i = 1; i <= n; ++i) {       ans ^= grundyValues[a[i]];     }     ans = (ans == 0) ? 1 : 0;     for (int i = 0; i < players.size(); ++i) {       cout << players[i ^ ans] <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   const int INF = 0x3f3f3f3f;   using namespace std;   int a[3030], b[3030];   int dp[3030];   int main() {     int t;     cin >> t;     while (t--) {       int n;       memset(dp, 0, sizeof(dp));       cin >> n;       for (int i = 1; i <= n; i++) {         cin >> a[i];         b[i] = a[i];       }       sort(b + 1, b + 1 + n);       for (int i = 1; i <= n; i++) {         a[i] = lower_bound(b + 1, b + n + 1, a[i]) - b;       }       int ans = 0;       for (int i = 1; i <= n; i++) {         dp[a[i]] = dp[a[i] - 1] + 1;         ans = max(ans, dp[a[i]]);       }       cout << n - ans << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   struct SNM {     vector<SNM*> trgs;     vector<SNM*> rv_trgs;     int stype;     bool asgn;     bool used;     void asgnN() {       if (asgn) return;       asgn = true;       for (vector<SNM*>::iterator i = trgs.begin(); i != trgs.end(); i++) {         (*i)->asgnN();       }     }     void usedN() {       if (used) return;       used = true;       if (stype == 1) return;       for (vector<SNM*>::iterator i = rv_trgs.begin(); i != rv_trgs.end(); i++) {         (*i)->usedN();       }     }     SNM() {       used = false;       asgn = false;     }   };   SNM nodes[100000];   int main() {     int n, m;     scanf( %d%d , &n, &m);     for (int i = 0; i < n; i++) {       scanf( %d , &nodes[i].stype);     }     for (int i = 0; i < m; i++) {       int a, b;       scanf( %d%d , &a, &b);       a--;       b--;       nodes[a].trgs.push_back(&nodes[b]);       nodes[b].rv_trgs.push_back(&nodes[a]);     }     for (int i = 0; i < n; i++) {       if (nodes[i].stype == 1) {         nodes[i].asgnN();       }     }     for (int i = 0; i < n; i++) {       if (nodes[i].stype == 2) {         nodes[i].usedN();       }     }     for (int i = 0; i < n; i++) {       if (nodes[i].asgn && nodes[i].used) {         printf( 1 n );       } else {         printf( 0 n );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s;   int main() {     long long n, q, x, y;     cin >> n >> q;     for (long long i = 1; i <= q; i++) {       cin >> x >> y;       if ((x + y) % 2 == 0) {         if (n % 2 == 0) {           long long linie = n / 2;           long long first = linie * (x - 1);           long long kth = (y + 1) / 2;           cout << first + kth <<   n ;         } else {           long long linie = n / 2;           long long lines = linie * (x - 1) + (x - 1) / 2 + (x - 1) % 2;           long long kth = (y + 1) / 2;           cout << lines + kth <<   n ;         }       } else {         long long depl = (n * n) / 2 + (n * n) % 2;         if (n % 2 == 0) {           long long linie = n / 2;           long long first = linie * (x - 1);           long long kth = (y + 1) / 2;           cout << depl + first + kth <<   n ;         } else {           long long linie = n / 2;           long long lines = linie * (x - 1) + (x - 1) / 2;           long long kth = (y + 1) / 2;           cout << depl + lines + kth <<   n ;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       int num2 = {}, num3 = {};       while (n % 2 == 0) {         n /= 2;         num2++;       }       while (n % 3 == 0) {         n /= 3;         num3++;       }       if (n != 1 || num2 > num3)         cout <<  -1 n ;       else         cout << 2 * num3 - num2 <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool valid(char str[]) {     int now = 0;     for (int i = 0; str[i]; i++) {       if (str[i] > now +  a ) return false;       if (str[i] == now +  a ) now++;     }     return true;   }   int main() {     char in[550];     scanf( %s , in);     int mx = 0;     for (int i = 0; in[i]; i++) mx = max(mx, in[i] -  a );     int cnt = 0;     if (valid(in))       printf( YES );     else       printf( NO );   }


#include <bits/stdc++.h>   int main() {     int n, m, k, p = 1;     scanf( %d , &n);     m = n;     while (m != 0) {       if (m % 10 == 1) {         m = m / 10;         continue;       }       if (m % 100 == 14) {         m = m / 100;         continue;       }       if (m % 1000 == 144) {         m = m / 1000;         continue;       }       p = 0;       break;     }     if (p == 1)       printf( YES );     else       printf( NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 105, A = 26;   bool win[N][N][A], known[N][N][A];   char g[N][N];   int n;   bool follow(int u, int v, char c) {     if (known[u][v][c -  a ]) return win[u][v][c -  a ];     for (int i = 1; i <= n; ++i)       if (g[u][i] >= c) win[u][v][c -  a ] |= !follow(v, i, g[u][i]);     known[u][v][c -  a ] = 1;     return win[u][v][c -  a ];   }   int main() {     int m;     scanf( %d%d , &n, &m);     while (m--) {       int a, b;       char c;       scanf( %d%d %c , &a, &b, &c);       g[a][b] = c;     }     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= n; ++j) {         printf( %c , follow(i, j,  a ) ?  A  :  B );       }       printf(  n );     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 5;   vector<long long> g[N];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t = 1;     while (t--) {       long long n;       cin >> n;       string s;       cin >> s;       long long i, j;       long long ans = 1e16;       for (int i = 0; i < n - 3; i++) {         long long sm = 0, c = 0;         for (int j = i; j < i + 4; j++) {           if (c % 4 == 0) {             sm += min(abs(s[j] -  A ), abs(s[j] -  Z ) + 1);           }           if (c % 4 == 1) {             sm += min(abs(s[j] -  C ), abs(s[j] -  Z ) + 3);           }           if (c % 4 == 2) {             sm += min(abs(s[j] -  T ), abs(s[j] -  A ) + 7);           }           if (c % 4 == 3) {             sm += min(abs(s[j] -  G ), abs(s[j] -  Z ) + 7);           }           c++;         }         ans = min(ans, sm);       }       cout << ans <<   n ;     }     return 0;   }


/*       Author - Param Kothari       Ah shit, here we go again   */      #include <bits/stdc++.h>       using namespace std;      #define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);   #define pb push_back   #define mk make_pair      typedef long long ll;   typedef long double ld;      void solve(){       int n;       cin >> n;       vector<int> a(n);       for(int i = 0; i < n; i++){           cin >> a[i];       }       vector<ll> c(n + 1);       for(int i = 0; i < n; i++){           c[a[i]]++;       }       vector<ll> pref(n + 2);       for(int i = 1; i <= n; i++){           pref[i] = pref[i - 1] + c[i];       }       ll res = 0;       auto nC2 = [&](ll x){           return (x * (x - 1)) / 2;       };       auto nC3 = [&](ll x){           return (x * (x - 1) * (x - 2)) / 6;       };       for(int i = 1; i <= n - 2; i++){           ll l = i, r = i + 2;           ll cnt = pref[r - 1] - pref[l];           res += 1LL * c[l] * c[r] * cnt;           res += 1LL * nC2(c[l]) * c[r];           res += 1LL * c[l] * nC2(c[r]);       }       for(int i = 1; i <= n - 1; i++){           ll l = i, r = i + 1;           res += 1LL * nC2(c[l]) * c[r];           res += 1LL * c[l] * nC2(c[r]);       }       for(int i = 1; i <= n; i++){           res += 1LL * nC3(c[i]);       }       cout << res <<   n ;       return;   }      int main(){       IOS;       int t = 1;       cin >> t;       for(int i = 1; i <= t; i++){           solve();       }       return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   long double dp[9000001] = {};   bool vis[9000001] = {};   long double solve(int inv) {     if (inv == 0) return 0;     if (inv == 1) return 1;     if (vis[inv]) return dp[inv];     vis[inv] = 1;     return dp[inv] = 4 + solve(inv - 2);   }   int main() {     std::ios::sync_with_stdio(false);     int n;     cin >> n;     std::vector<int> v(n);     for (int i = 0; i < n; i++) cin >> v[i];     int inv = 0;     for (int i = 0; i < n; i++) {       for (int j = i + 1; j < n; j++)         if (v[j] < v[i]) inv++;     }     cout << fixed << setprecision(9) << solve(inv);   }


#include <bits/stdc++.h>   using namespace std;   const long double eps = 1e-9;   const double EPS = 1e-9;   int ar[] = {1, -1, 0, 0, 1, 1, -1, -1};   int ac[] = {0, 0, 1, -1, 1, -1, 1, -1};   int p[200005];   int q[200005];   void solve1(int n) {     int k = 1;     if (n & 1) {       cout <<  NO n ;       return;     }     cout <<  YES n ;     while (k <= n) k *= 2;     k /= 2;     int last = n + 1;     while (k != 0) {       for (int i = k; i < last; i++) {         p[i] = k - (i - k + 1);         p[k - (i - k + 1)] = i;       }       last = min(last, k - (last - k));       k /= 2;     }     for (int i = 1; i <= n; i++) cout << p[i] <<    ;     cout <<   n ;   }   void solve2(int n) {     if (n < 6) {       cout <<  NO n ;       return;     }     int k = 1;     while (2 * k <= n) k *= 2;     if (n == k) {       cout <<  NO n ;       return;     }     cout <<  YES n ;     if (n == 6) {       cout <<  3 6 2 5 1 4 n ;       return;     }     for (int i = k; i < n; i++) q[i] = i + 1;     q[n] = k;     while (k != 4) {       for (int i = k / 2; i < k - 1; i++) {         q[i] = i + 1;       }       q[k - 1] = k / 2;       k /= 2;     }     q[1] = 5;     q[2] = 3;     q[3] = 2;     q[4] = 6;     q[5] = 7;     q[6] = 4;     q[7] = 1;     for (int i = 1; i <= n; i++) cout << q[i] <<    ;     cout <<   n ;     return;   }   int main() {     ios_base::sync_with_stdio(false);     int n;     cin >> n;     solve1(n);     solve2(n);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1000000007;   const int MAX = 200002;   int arr[MAX];   int cnt[MAX];   int main() {     int n, wrong = 0;     cin >> n;     for (int i = (1); i <= (n); i++) {       cin >> arr[i];     }     string s;     cin >> s;     cnt[1] = s[0] -  0 ;     for (int i = (2); i <= (n); i++) {       cnt[i] = s[i - 1] -  0 ;       cnt[i] += cnt[i - 1];     }     bool yes = 1;     for (int i = (1); i <= (n); i++) {       if (arr[i] != i) {         int has = 0;         if (arr[i] > i)           has = cnt[arr[i] - 1] - cnt[i - 1];         else           has = cnt[i - 1] - cnt[arr[i] - 1];         if (has < abs(arr[i] - i)) {           yes = 0;           break;         }       }     }     if (yes)       cout <<  YES n ;     else       cout <<  NO n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 10;   vector<int> v[maxn];   vector<int> edge[maxn];   int flag[maxn], val[2][maxn];   bool bad[maxn];   int par[maxn][20];   inline int dfs(int x, int p) {     par[x][0] = p;     for (int i = 1; i < 20; i++) par[x][i] = par[par[x][i - 1]][i - 1];     flag[x] = flag[p] + 1;     int ret = flag[x];     int cnt = 0;     for (int i = 0; i < v[x].size(); i++) {       int u = v[x][i];       if (flag[u] == 0)         ret = min(ret, dfs(u, x));       else {         if (flag[u] == flag[x] - 1)           cnt++;         else           ret = min(ret, flag[u]);       }     }     if (cnt > 1) ret = min(ret, flag[x] - 1);     bad[x] = ret == flag[x];     return ret;   }   inline int Par(int x, int k) {     for (int i = 20; i >= 0; i--)       if (k & (1 << i)) {         x = par[x][i];         k -= (1 << i);       }     return x;   }   inline int cpar(int x, int y) {     if (flag[x] < flag[y]) swap(x, y);     x = Par(x, flag[x] - flag[y]);     if (x == y) return x;     for (int i = 19; i >= 0; i--)       if (par[x][i] != par[y][i]) {         x = par[x][i];         y = par[y][i];       }     return par[x][0];   }   inline pair<int, int> dfs2(int x) {     pair<int, int> ret(0, 0);     ret.first += val[0][x];     ret.second += val[1][x];     flag[x] = true;     for (int i = 0; i < v[x].size(); i++)       if (flag[v[x][i]] == 0) {         pair<int, int> g = dfs2(v[x][i]);         ret.first += g.first;         ret.second += g.second;       }     if (bad[x] && ret.first && ret.second) {       cout <<  No ;       exit(0);     }     return ret;   }   int main() {     int n, m, q;     cin >> n >> m >> q;     for (int i = 1; i <= m; i++) {       int x, y;       cin >> x >> y;       v[x].push_back(y);       v[y].push_back(x);     }     for (int i = 1; i <= n; i++)       if (flag[i] == 0) dfs(i, 0);     while (q--) {       int x, y;       cin >> x >> y;       int u = cpar(x, y);       val[0][x]++;       val[0][u]--;       val[1][u]--;       val[1][y]++;     }     memset(flag, 0, sizeof flag);     for (int i = 1; i <= n; i++)       if (flag[i] == 0) {         pair<int, int> G = dfs2(i);         if (G.first || G.second) {           cout <<  No ;           return 0;         }       }     cout <<  Yes ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int s, n, b[7][7], a[49], used[49], n2;   bool check() {     for (int i = 0; i < (int)(n); i++) {       int sr = 0;       for (int j = 0; j < (int)(n); j++) sr += b[i][j];       if (sr != s) return false;       sr = 0;       for (int j = 0; j < (int)(n); j++) sr += b[j][i];       if (sr != s) return false;     }     int sr = 0;     for (int i = 0; i < (int)(n); i++) sr += b[i][i];     if (sr != s) return false;     sr = 0;     for (int i = 0; i < (int)(n); i++) sr += b[i][n - 1 - i];     if (sr != s) return false;     return true;   }   void rec(int i, int j, int sr) {     if (i == n) {       if (!check()) return;       for (int q = 0; q < (int)(n); q++) {         for (int w = 0; w < (int)(n); w++) {           if (w) printf(   );           printf( %d , b[q][w]);         }         printf(  n );       }       exit(0);     }     if (j == n - 1) {       int r1 = s - sr;       int r2 = r1;       int r3 = r1;       if (i == n - 1) {         r2 = 0, r3 = 0;         for (int z = 0; z < (int)(n - 1); z++) r2 += b[z][z], r3 += b[z][n - 1];         r2 = s - r2, r3 = s - r3;       }       if (r1 != r2 || r1 != r3) return;       for (int q = 0; q < (int)(n2); q++)         if (!used[q] && a[q] == r1) {           used[q] = true;           b[i][j] = a[q];           rec(i + 1, 0, 0);           used[q] = false;           break;         }       return;     }     if (i == n - 1) {       int sw = 0;       for (int z = 0; z < (int)(n - 1); z++) sw += b[z][j];       int t1 = s - sw;       int t2 = t1;       if (j == 0) {         int ss = 0;         for (int z = 0; z < (int)(n - 1); z++) ss += b[z][n - 1 - z];         t2 = s - ss;       }       if (t1 != t2) return;       for (int q = 0; q < (int)(n2); q++)         if (!used[q] && a[q] == t1) {           used[q] = true;           b[i][j] = a[q];           rec(i, j + 1, sr + a[q]);           used[q] = false;         }       return;     }     int lu = a[0] - 1;     for (int q = 0; q < (int)(n2); q++)       if (!used[q] && a[q] != lu) {         used[q] = true;         b[i][j] = lu = a[q];         rec(i, j + 1, sr + lu);         used[q] = false;       }   }   int aa(int x) { return x < 0 ? -x : x; }   int main() {     scanf( %d , &n);     n2 = n * n;     for (int i = 0; i < (int)(n2); i++) scanf( %d , &a[i]);     sort(a, a + n2);     s = 0;     for (int i = 0; i < (int)(n2); i++) s += a[i];     s /= n;     printf( %d n , s);     rec(0, 0, 0);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline void read(int &x) {     x = 0;     char c = getchar();     int f = 1;     while (!isdigit(c)) {       if (c ==  - ) f = -1;       c = getchar();     }     while (isdigit(c)) {       x = x * 10 + c -  0 ;       c = getchar();     }     x *= f;   }   inline unsigned int R() {     static unsigned int seed = 416;     return seed ^= seed >> 5, seed ^= seed << 17, seed ^= seed >> 13;   }   const int N = 2333;   int n, l[N], r[N], a[N], s[N];   void GG() {     puts( NO );     exit(0);   }   int main() {     read(n);     for (register int i = 1; i <= n; i++) read(l[i]);     for (register int i = 1; i <= n; i++) read(r[i]);     for (register int i = 1; i <= n; i++) a[i] = n - (l[i] + r[i]);     for (register int i = 1; i <= n; i++) {       int p = 0;       for (register int j = 1; j <= i - 1; j++) p += a[j] > a[i];       if (p != l[i]) GG();       p = 0;       for (register int j = i + 1; j <= n; j++) p += a[j] > a[i];       if (p != r[i]) GG();     }     puts( YES );     for (register int i = 1; i <= n; i++) printf( %d  , a[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool vt[202][202];   int cnt, n;   int dfs(int x, int y) {     int mx1 = 0, mx2 = 0, mx = 0;     for (int i = 1; i <= n; i++)       if (vt[x][i] && i != y) {         mx = max(mx, dfs(i, x));         if (cnt > mx1)           mx2 = mx1, mx1 = cnt;         else if (cnt > mx2)           mx2 = cnt;       }     mx = max(mx, mx1 + mx2);     cnt = mx1 + 1;     return mx;   }   int main() {     ios::sync_with_stdio(0);     int x, y, m, sm = 0;     cin >> n;     for (int i = 0; i < n - 1; i++) cin >> x >> y, vt[x][y] = 1, vt[y][x] = 1;     int mx = 0;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) {         int l = 0, k = 0;         if (vt[i][j]) {           l = dfs(i, j);           k = dfs(j, i);           mx = max(mx, l * k);         }       }     cout << mx;   }


#include <bits/stdc++.h>   using namespace std;   //#pragma GCC optimize( Ofast )   //#pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )       #define ms(s, n) memset(s, n, sizeof(s))   #define FOR(i, a, b) for (int i = (a); i < (b); ++i)   #define FORd(i, a, b) for (int i = (a) - 1; i >= (b); --i)   #define FORall(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)   #define sz(a) int((a).size())   #define present(t, x) (t.find(x) != t.end())   #define all(a) (a).begin(), (a).end()   #define uni(a) (a).erase(unique(all(a)), (a).end())   #define pb push_back   #define pf push_front   #define mp make_pair   #define fi first   #define se second   #define prec(n) fixed<<setprecision(n)   #define bit(n, i) (((n) >> (i)) & 1)   #define bitcount(n) __builtin_popcountll(n)   typedef long long ll;   typedef unsigned long long ull;   typedef long double ld;   typedef pair<int, int> pi;   typedef vector<int> vi;   typedef vector<pi> vii;   const int MOD = (int) 1e9 + 7;   const int FFTMOD = 119 << 23 | 1;   const int INF = (int) 1e9 + 23111992;   const ll LINF = (ll) 1e18 + 23111992;   const ld PI = acos((ld) -1);   const ld EPS = 1e-9;   inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;}   inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}   inline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}   template<class T> inline int chkmin(T& a, const T& val) {return val < a ? a = val, 1 : 0;}   template<class T> inline int chkmax(T& a, const T& val) {return a < val ? a = val, 1 : 0;}   inline ull isqrt(ull k) {ull r = sqrt(k) + 1; while (r * r > k) r--; return r;}   inline ll icbrt(ll k) {ll r = cbrt(k) + 1; while (r * r * r > k) r--; return r;}   inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;}   inline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;}   inline int mult(int a, int b, int p = MOD) {return (ll) a * b % p;}   inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}   inline int sign(ld x) {return x < -EPS ? -1 : x > +EPS;}   inline int sign(ld x, ld y) {return sign(x - y);}   mt19937 mt(chrono::high_resolution_clock::now().time_since_epoch().count());   inline int mrand() {return abs((int) mt());}   inline int mrand(int k) {return abs((int) mt()) % k;}   #define db(x) cerr <<  [  << #x <<  :   << (x) <<  ]  ;   #define endln cerr <<   n ;      void chemthan() {       int n; cin >> n;       vi a(n);       vi b(n);       FOR(i, 0, n) cin >> a[i];       FOR(i, 0, n) cin >> b[i];       long long sum = 0;       FOR(i, 0, n) sum += abs(a[i] - b[i]);       auto func = [&] (int sa, int sb) {           long long res = sum;           vi dc;           FOR(i, 0, n) dc.pb(sb * a[i]), dc.pb(sb * b[i]);           sort(all(dc)), uni(dc);           vector<long long> fen(sz(dc) + 1, LINF);           auto upd = [&] (int p, long long v) {               p++;               for (; p < sz(fen); p += p & -p) {                   chkmin(fen[p], v);               }           };           auto query = [&] (int p) {               p++;               long long res = LINF;               for (; 0 < p; p -= p & -p) {                   chkmin(res, fen[p]);               }               return res;           };           vector<tuple<int, int, int>> v;           FOR(i, 0, n) {               v.pb({sa * a[i], sb * b[i], 1});               v.pb({sa * b[i], sb * a[i], 0});           }           sort(all(v));           for (auto [x, y, t] : v) {               int k = lower_bound(all(dc), y) - dc.begin();               if (!t) {                   upd(k, (long long) - x - y - abs(sa * x - sb * y));               }               else {                   chkmin(res, sum + x + y + query(k) - abs(sa * x - sb * y));               }           }           return res;       };       long long res = sum;       for (int sa : {-1, 1}) {           for (int sb : {-1, 1}) {               chkmin(res, func(sa, sb));           }       }       cout << res <<   n ;   }      int32_t main(int32_t argc, char* argv[]) {       ios_base::sync_with_stdio(0), cin.tie(0);       if (argc > 1) {           assert(freopen(argv[1],  r , stdin));       }       if (argc > 2) {           assert(freopen(argv[2],  wb , stdout));       }       chemthan();       cerr <<   nTime elapsed:   << 1000 * clock() / CLOCKS_PER_SEC <<  ms n ;       return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long nmax = 1e6 + 5;   const unsigned long long oo = 1e18;   const long long mod = 1e9 + 7;   const long long ox[4] = {-1, 1, 0, 0};   const long long oy[4] = {0, 0, -1, 1};   const long long dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};   const long long dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};   long long a[nmax];   int main() {     {       ios_base::sync_with_stdio(false);       cin.tie(0);       cout.tie(0);     };     long long t, n, res;     cin >> t;     while (t--) {       cin >> n;       bool kt = false;       for (int i = 1; i <= n; i++) cin >> a[i];       for (int i = 1; i <= n - 1; i++)         if (a[i] >= a[i + 1]) {           kt = 1;           break;         }       if (kt || !(n % 2)) {         cout <<  YES               <<   n ;       } else         cout <<  NO               <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 202020, P = 1e9 + 7;   int n;   int a[N], par[N], cnt[2][N], c[2][N];   long long ans;   vector<int> g[N];   void upd(long long &a, long long b) {     if (b < 0) b += P;     a += b;     if (a >= P) a -= P;   }   void dfs(int u, int fa) {     cnt[0][u] = 1;     cnt[1][u] = 0;     par[u] = fa;     for (int i = (0); i < ((int)g[u].size()); i++) {       int v = g[u][i];       if (v == fa) continue;       dfs(v, u);       cnt[0][u] += cnt[1][v];       cnt[1][u] += cnt[0][v];     }   }   void dfs2(int u) {     if (par[u]) {       c[0][u] = c[1][par[u]];       c[1][u] = c[0][par[u]];     } else {       c[0][u] = cnt[0][u];       c[1][u] = cnt[1][u];     }     for (int i = (0); i < ((int)g[u].size()); i++) {       int v = g[u][i];       if (v == par[u]) continue;       dfs2(v);     }   }   int main() {     scanf( %d , &n);     for (int i = (1); i < (n + 1); i++) {       scanf( %d , a + i);       if (a[i] < 0) a[i] += P;     }     for (int i = (1); i < (n); i++) {       int u, v;       scanf( %d%d , &u, &v);       g[u].push_back(v);       g[v].push_back(u);     }     dfs(1, 0);     dfs2(1);     for (int i = (1); i < (n + 1); i++) {       long long pre = ans;       int c0 = c[0][i], c1 = c[1][i];       upd(ans, 1ll * c0 * c0 % P * a[i] % P);       upd(ans, -1ll * c1 * c1 % P * a[i] % P);       if (par[i]) {         c0 = c[0][par[i]] - cnt[1][i];         c1 = c[1][par[i]] - cnt[0][i];         upd(ans, 1ll * c0 * c0 % P * a[i] % P);         upd(ans, -1ll * c1 * c1 % P * a[i] % P);       }       for (int j = (0); j < ((int)g[i].size()); j++) {         int v = g[i][j];         if (v != par[i]) {           c0 = cnt[0][v];           c1 = cnt[1][v];           upd(ans, 1ll * c0 * c0 % P * a[i] % P);           upd(ans, -1ll * c1 * c1 % P * a[i] % P);         }       }     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   #define Input(v, n) fr(i, n) cin >> v[i];   #define checkk(v, n) fr(i, n) cout << v[i] <<     << endl;   #define sort(v) sort(v.begin(), v.end());   #define int long long int   #define double long double   #define fr(i, n) for (int i = 0; i < n; i++)   #define frr(i, s, e) for (int i = s; i <= e; i++)   #define rfr(i, s, e) for (int i = s; i >= e; i--)   #define fix(f,n) fixed<<setprecision(n)<<f   #define endl   n    #define pb push_back   #define mp make_pair   #define mod 1000000007      int test;   double v,ans;      void add(double &c,double &m,double &p,int i,double t){      if(i==0){          if(m==0) p+=t;          else {              t=t/2.0;              m+=t,p+=t;          }      }      else{          if(c==0) p+=t;          else {              t=t/2.0;              c+=t,p+=t;          }      }   }      void calc(double c,double m,double p,double val,double count){       if(c<0.000001) c=0;       if(m<0.000001) m=0;          double t1=c,t2=m,t3=p;       ans+=(count*(val*p));              if(c!=0){           double nan=val*c;           if(c<=v) add(c,m,p,0,c),c=0,calc(c,m,p,nan,count+1.0);           else  add(c,m,p,0,v),c=c-v,calc(c,m,p,nan,count+1.0);       }       c=t1,m=t2,p=t3;       if(m!=0){           double nan=val*m;           if(m<=v) add(c,m,p,1,m),m=0,calc(c,m,p,nan,count+1.0);           else  add(c,m,p,1,v),m=m-v,calc(c,m,p,nan,count+1.0);       }          }      void solve()   {          ans=0.0;       double c,m,p;       cin>>c>>m>>p>>v;       calc(c,m,p,1.0,1.0);       cout<<fix(ans,12)<<endl;   }      int32_t main()   {       ios_base::sync_with_stdio(false);       cin.tie(0);               cin>>test;       //test=1;       while (test--)       {           solve();       }          return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<string> numbers;   unsigned n = 2;   unsigned k = 5;   int taken[100];   void print(unsigned i) {     unsigned l;     string str =   ;     for (l = 0; l <= i - 1; l++) str += (taken[l] + 1) +  0 ;     numbers.push_back(str);   }   void variate(unsigned i) {     unsigned j;     if (i >= k) {       print(i);       return;     }     for (j = 0; j < n; j++) {       taken[i] = j;       variate(i + 1);     }   }   int main(void) {     for (unsigned i = 1; i < 11; i++) {       k = i;       variate(0);     }     string input;     cin >> input;     for (int i = 0; i < input.size(); i++) {       if (input[i] ==  4 )         input[i] =  1 ;       else         input[i] =  2 ;     }     for (int i = 0; i < numbers.size(); i++) {       if (numbers[i] == input) cout << i + 1 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   const long long INF = (long long)2e18;   const int N = (int)1e6 + 77;   int n;   long long a[N];   long long solve(long long d) {     long long ans = 0;     long long cur = 0;     for (int i = 0; i < n; i++) {       long long h = a[i];       long long z = min(h, d - cur);       h -= z;       cur += z;       if (cur <= d / 2) ans += cur;       if (cur > d / 2) ans += d - cur;       if (cur == d) cur = 0;       if (h > 0) {         h %= d;         cur = h;         if (cur <= d / 2) ans += cur;         if (cur > d / 2) ans += d - cur;       }     }     if (cur != 0) throw;     return ans;   }   int main() {     scanf( %d , &n);     long long sum = 0;     for (int i = 0; i < n; i++) {       scanf( %lld , &a[i]);       sum += a[i];     }     if (sum == 1) {       printf( -1 n );       return 0;     }     long long ans = INF;     for (long long x = 2; x * x <= sum; x++) {       if (sum % x) continue;       ans = min(ans, solve(x));       while (sum % x == 0) sum /= x;     }     if (sum > 1) ans = min(ans, solve(sum));     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   int n, finans = 0;   int a[N], ans[N], sieve[N], countprime[N];   vector<int> primes[N];   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];       ans[i] = 1;     }     for (int i = 2; i <= 1e5; i++) sieve[i] = 1;     for (int i = 2; i <= 1e5; i++) {       if (sieve[i]) {         for (int j = 1; i * j <= 1e5; j++) {           primes[i * j].push_back(i);           sieve[i * j] = 0;         }       }     }     for (int i = n; i >= 1; i--) {       for (auto it : primes[a[i]]) ans[i] = max(ans[i], countprime[it] + 1);       for (auto it : primes[a[i]]) countprime[it] = max(countprime[it], ans[i]);       finans = max(ans[i], finans);     }     cout << finans;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100010;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n = 0;     string s;     long long int res = 0ll;     cin >> n;     cin >> s;     res = (1ll * n * (n + 1)) / 2ll - 1ll * n;     for (int i = 0; i + 1 < n; ++i) {       if (s[i] != s[i + 1]) {         int j;         for (j = i + 1; j < n; ++j) {           if (s[i] == s[j]) break;         }         res -= (j - i - 1);       }     }     reverse(s.begin(), s.end());     for (int i = 0; i + 1 < n; ++i) {       if (s[i] != s[i + 1]) {         int j;         for (j = i + 1; j < n; ++j) {           if (s[i] == s[j]) break;         }         res -= (j - i - 2);       }     }     cout << res;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   void out(T x) {     cout << x << endl;     exit(0);   }   const int maxn = 1e6 + 5;   vector<int> get(map<int, vector<int>> mp) {     vector<int> res;     for (auto p : mp) {       for (auto x : p.second) {         res.push_back(x);       }     }     return res;   }   int n;   map<int, vector<int>> mp;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n;     for (int i = 0; i < n; i++) {       int x;       cin >> x;       mp[x].push_back(i + 1);     }     vector<vector<int>> ans;     bool ok = false;     int val = -1;     vector<int> v;     for (auto p : mp) {       v.push_back(p.first);       if (int(p.second.size()) >= 3) {         val = p.first;         ok = true;         break;       }     }     map<int, vector<int>> _mp = mp;     if (ok) {       int x = val;       swap(mp[x][0], mp[x][1]);       ans.push_back(get(mp));       mp = _mp;       swap(mp[x][0], mp[x][2]);       ans.push_back(get(mp));       mp = _mp;       swap(mp[x][1], mp[x][2]);       ans.push_back(get(mp));       mp = _mp;     } else {       int N = v.size();       int val1, val2;       for (int i = 0; i < N; i++) {         for (int j = i + 1; j < N; j++) {           if (int(mp[v[i]].size()) >= 2 && int(mp[v[j]].size()) >= 2) {             val1 = v[i];             val2 = v[j];             ok = true;           }         }       }       if (!ok) out( NO );       ans.push_back(get(mp));       mp = _mp;       swap(mp[val1][0], mp[val1][1]);       ans.push_back(get(mp));       mp = _mp;       swap(mp[val2][0], mp[val2][1]);       ans.push_back(get(mp));       mp = _mp;     }     cout <<  YES  << endl;     for (auto p : ans) {       for (auto x : p) cout << x <<    ;       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int read() {     int X = 0, w = 1;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) w = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) X = X * 10 + c -  0 , c = getchar();     return X * w;   }   const int N = 200000 + 10;   const int inf = 0x3f3f3f3f;   int n, Q;   char s[N];   int sum[N][26];   struct SuffixArray {     int sa[N], rnk[N], height[N], x[N], y[N], z[N];     void SuffixSort() {       int m = 256;       for (int i = 1; i <= n; ++i) ++z[x[i] = s[i]];       for (int i = 2; i <= m; ++i) z[i] += z[i - 1];       for (int i = n; i; --i) sa[z[x[i]]--] = i;       for (int k = 1; k < n; k <<= 1) {         int p = 0;         for (int i = n - k + 1; i <= n; ++i) y[++p] = i;         for (int i = 1; i <= n; ++i)           if (sa[i] > k) y[++p] = sa[i] - k;         for (int i = 1; i <= m; ++i) z[i] = 0;         for (int i = 1; i <= n; ++i) ++z[x[i]];         for (int i = 2; i <= m; ++i) z[i] += z[i - 1];         for (int i = n; i; --i) sa[z[x[y[i]]]--] = y[i], y[i] = 0;         swap(x, y), x[sa[1]] = p = 1;         for (int i = 2; i <= n; ++i)           x[sa[i]] =               (y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + k] == y[sa[i] + k])                   ? p                   : ++p;         if (p == m) break;         m = p;       }     }     void GetHeight() {       for (int i = 1; i <= n; ++i) rnk[sa[i]] = i;       for (int i = 1, j = 0; i <= n; ++i) {         if (j) --j;         int p = sa[rnk[i] - 1];         while (s[i + j] == s[p + j]) ++j;         height[rnk[i]] = j;       }     }     int lg[N], st[18][N];     void buildST() {       for (int i = 2; i <= n; ++i) lg[i] = lg[i >> 1] + 1;       for (int i = 1; i <= n; ++i) st[0][i] = height[i];       for (int i = 1; i < 18; ++i)         for (int j = 1; j <= n; ++j)           st[i][j] = min(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);     }     int qmin(int l, int r) {       int t = lg[r - l + 1];       return min(st[t][l], st[t][r - (1 << t) + 1]);     }     int LCP(int x, int y) {       if (rnk[x] > rnk[y]) swap(x, y);       return qmin(rnk[x] + 1, rnk[y]);     }   } A, B;   struct SegmentTree {     int minv[N << 2], tag[N << 2];     SegmentTree() {       memset(minv, 0x3f, sizeof(minv)), memset(tag, 0x3f, sizeof(tag));     }     void update(int o, int l, int w) {       tag[o] = min(tag[o], w), minv[o] = min(minv[o], l + w);     }     void pushup(int o) { minv[o] = min(minv[(o << 1)], minv[(o << 1 | 1)]); }     void pushdown(int o, int l, int r) {       int mid = (l + r) >> 1;       if (tag[o] != inf)         update((o << 1), l, tag[o]), update((o << 1 | 1), mid + 1, tag[o]),             tag[o] = inf;     }     void modify(int o, int l, int r, int ql, int qr, int w) {       if (ql <= l && r <= qr) {         update(o, l, w);         return;       }       int mid = (l + r) >> 1;       pushdown(o, l, r);       if (ql <= mid) modify((o << 1), l, mid, ql, qr, w);       if (qr > mid) modify((o << 1 | 1), mid + 1, r, ql, qr, w);       pushup(o);     }     int query(int o, int l, int r, int ql, int qr) {       if (ql <= l && r <= qr) return minv[o];       int mid = (l + r) >> 1, res = inf;       pushdown(o, l, r);       if (ql <= mid) res = min(res, query((o << 1), l, mid, ql, qr));       if (qr > mid) res = min(res, query((o << 1 | 1), mid + 1, r, ql, qr));       return res;     }   } L, R;   void InitAA(SuffixArray &lcp_sa, SuffixArray &lcs_sa, SegmentTree &T) {     for (int l = 1; l <= n / 2; ++l)       for (int i = l, j = l << 1; j <= n; i += l, j += l) {         int lcp = lcp_sa.LCP(i, j), lcs = lcs_sa.LCP(n - i + 1, n - j + 1);         if (lcp + lcs - 1 >= l)           T.modify(1, 1, n, i - lcs + 1, i + lcp - l, 2 * l - 1);       }   }   bool CheckBorder(int l, int r) {     int len = r - l + 1, S = sqrt(n) + 1;     for (int i = 1; i < len && i <= S; ++i)       if (A.LCP(l, r - i + 1) >= i) return 1;     for (int i = A.rnk[l] - 1, h = A.height[i + 1], c = 0; i && c <= S && h > S;          ++c, h = min(h, A.height[i--]))       if (A.sa[i] >= l && A.sa[i] <= r && A.sa[i] + h > r) return 1;     for (int i = A.rnk[l] + 1, h = A.height[i], c = 0; i <= n && c <= S && h > S;          ++c, h = min(h, A.height[++i]))       if (A.sa[i] >= l && A.sa[i] <= r && A.sa[i] + h > r) return 1;     return 0;   }   bool CheckNS(int l, int r) {     int flag = 1;     for (int i = 0; i < 26; ++i)       if (sum[r][i] - sum[l - 1][i] >= 2) {         flag = 0;         break;       }     return flag;   }   bool Check1(int l, int r) {     int len = r - l + 1;     for (int i = 1; i * i <= len; ++i) {       if (len % i) continue;       if (A.LCP(l, l + i) >= len - i) return 1;       if (i != 1 && A.LCP(l, l + len / i) >= len - len / i) return 1;     }     return 0;   }   bool Check2(int l, int r) {     if (CheckBorder(l, r)) return 1;     if (L.query(1, 1, n, l, l) <= r) return 1;     if (R.query(1, 1, n, n - r + 1, n - r + 1) <= n - l + 1) return 1;     return 0;   }   bool Check3(int l, int r) {     if (sum[r][s[l] -  a ] - sum[l][s[l] -  a ]) return 1;     if (sum[r - 1][s[r] -  a ] - sum[l - 1][s[r] -  a ]) return 1;     if (L.query(1, 1, n, l, r) <= r) return 1;     return 0;   }   int main() {     n = read();     scanf( %s , s + 1);     for (int i = 1; i <= n; ++i)       for (int j = 0; j < 26; ++j) sum[i][j] = sum[i - 1][j] + (j == s[i] -  a );     A.SuffixSort(), A.GetHeight(), A.buildST();     reverse(s + 1, s + n + 1);     B.SuffixSort(), B.GetHeight(), B.buildST();     reverse(s + 1, s + n + 1);     InitAA(A, B, L), InitAA(B, A, R);     Q = read();     while (Q--) {       int l = read(), r = read();       if (CheckNS(l, r)) {         puts( -1 );         continue;       }       if (Check1(l, r)) {         puts( 1 );         continue;       }       if (Check2(l, r)) {         puts( 2 );         continue;       }       if (Check3(l, r)) {         puts( 3 );         continue;       }       puts( 4 );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 7;   const int inf = 0x3f3f3f3f;   const long long INF = 0x3f3f3f3f3f3f3f3f;   const int mod = 1000000007;   const double eps = 1e-6;   const double PI = acos(-1);   int tar, n, tot, a[N], hs[N], cnt[N], mx[N], now[N];   vector<int> vc;   bool check(int add) {     vc.clear();     int c = tar;     int p = mx[c];     while (c) {       if (add > c) add = 0;       while (!now[p] && p) p--;       p = min(p, mx[c]);       if (!p && !add) break;       if (p && hs[p] >= add) {         vc.push_back(p);         int v = hs[p], num = min(now[p], c / v);         now[p] -= num;         c -= num * v;       } else {         c -= add;         add = 0;       }     }     for (auto& x : vc) now[x] = cnt[x];     return c;   }   int main() {     scanf( %d%d , &tar, &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       hs[++tot] = a[i];     }     sort(hs + 1, hs + tot + 1);     tot = unique(hs + 1, hs + tot + 1) - hs - 1;     for (int i = 1; i <= n; i++)       cnt[lower_bound(hs + 1, hs + tot + 1, a[i]) - hs]++;     for (int i = 1; i <= tot; i++) now[i] = cnt[i];     for (int i = 1; i <= tar; i++)       mx[i] = upper_bound(hs + 1, hs + tot + 1, i) - hs - 1;     for (int i = 1; i <= tar; i++) {       if (check(i)) {         printf( %d n , i);         return 0;       }     }     puts( Greed is good );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   long long x[2005], y[2005];   long long c[2005];   long long k[2005];   bool vis[2005];   long long dis[2005];   long long tower[2005];   long long diss[2005];   long long way[2005][2005];   long long townum = 0;   long long add[2005][2];   long long addnum = 0;   long long ans = 0;   void pri() {     printf( %lld n , ans);     printf( %lld n , townum);     for (int i = 1; i <= townum; i++) {       printf( %lld  , tower[i]);     }     printf(  n%lld n , addnum);     for (int i = 1; i <= addnum; i++) {       printf( %lld %lld n , add[i][0], add[i][1]);     }   }   void init() {     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         way[i][j] = (abs(x[i] - x[j]) + abs(y[i] - y[j])) * (k[i] + k[j]);       }     }     for (int i = 1; i <= 2003; i++) dis[i] = 1e18;     for (int i = 1; i <= n; i++) way[i][0] = way[0][i] = c[i];   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %lld%lld , &x[i], &y[i]);     }     int minn = 1;     for (int i = 1; i <= n; i++) {       scanf( %lld , &c[i]);     }     for (int i = 1; i <= n; i++) {       scanf( %lld , &k[i]);     }     init();     dis[0] = 0;     for (int i = 0; i <= n; i++) {       long long ma = 1e18 + 10;       int id = -1;       for (int j = 0; j <= n; j++) {         if (!vis[j] && dis[j] < ma) {           id = j;           ma = dis[j];         }       }       vis[id] = true;       ans += dis[id];       if (id != 0) {         if (diss[id] != 0) {           add[++addnum][0] = diss[id];           add[addnum][1] = id;         } else {           tower[++townum] = id;         }       }       for (int j = 0; j <= n; j++) {         if (!vis[j]) {           if (dis[j] > way[id][j]) {             dis[j] = way[id][j];             diss[j] = id;           }         }       }     }     pri();   }


#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1.0);   const int INF = 1000000000;   const int MOD = 1000000007;   const int base = 1000 * 1000 * 1000;   int main() {     int n, m;     cin >> n >> m;     int l = 1, r = n;     string str, str1, str2, str3;     int t;     for (int i = 0; i < m; i++) {       cin >> str >> str1 >> str2 >> str3 >> t;       if (str2 ==  left ) {         if (t <= r) {           r = t - 1;         }       } else {         if (t >= l) {           l = t + 1;         }       }       if (l > r) {         cout << -1;         return 0;       }     }     cout << r - l + 1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, k;   int X1[127], X2[128], Y1[111], Y2[105];   bool u[134][134];   vector<int> res;   int check(int xx1, int yy1, int x2, int y2) {     if (x2 < 0 || y2 < 0 || x2 >= n || y2 >= m || u[x2][y2]) return 0;     if (xx1 > x2) swap(xx1, x2);     if (yy1 > y2) swap(yy1, y2);     if (xx1 != x2) {       xx1++;       y2++;     } else {       x2++;       yy1++;     }     for (int i = 0; i < k; i++) {       int x3 = max(xx1, X1[i]);       int y3 = max(yy1, Y1[i]);       int x4 = min(x2, X2[i]);       int y4 = min(y2, Y2[i]);       if (x3 < x4 && y3 <= y4 || x3 <= x4 && y3 < y4) return 0;     }     return 1;   }   int go(int i, int j) {     int t = 1;     u[i][j] = 1;     if (check(i, j, i + 1, j)) t += go(i + 1, j);     if (check(i, j, i - 1, j)) t += go(i - 1, j);     if (check(i, j, i, j + 1)) t += go(i, j + 1);     if (check(i, j, i, j - 1)) t += go(i, j - 1);     return t;   }   int main() {     cin >> n >> m >> k;     for (int i = 0; i < k; i++) cin >> X1[i] >> Y1[i] >> X2[i] >> Y2[i];     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++)         if (!u[i][j]) res.push_back(go(i, j));     sort(res.begin(), res.end());     for (int i = 0; i < res.size(); i++) cout << res[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 10005;   int a[26][26][maxn];   char s[maxn];   int main(int argc, char const *argv[]) {     ios::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> s;     n = strlen(s);     for (int i = n; i < 2 * n - 1; ++i) {       s[i] = s[i - n];     }     for (int i = 0; i < n; ++i) {       for (int j = 1; j < n; ++j) {         a[s[i] -  a ][s[i + j] -  a ][j]++;       }     }     int res = 0;     for (int i = 0; i < 26; ++i) {       int c = 0;       for (int j = 1; j < n; ++j) {         int t = 0;         for (int k = 0; k < n; ++k) {           if (s[k] ==  a  + i) {             if (a[s[k] -  a ][s[j + k] -  a ][j] == 1) {               ++t;             }           }         }         c = max(c, t);       }       res += c;     }     cout << (double)res / n << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[1000005];   int main() {     int n;     while (cin >> n) {       for (int i = n; i >= 0; i--)         if (!a[i]) {           int mm = (1 << ((int)(log2(i) + 1))) - 1;           a[i] = mm ^ i;           a[mm ^ i] = i;         }       long long int ans = 0;       for (int i = 0; i <= n; i++) {         ans += a[i] ^ i;       }       cout << ans << endl;       for (int i = 0; i <= n; i++) {         cout << a[i];         if (i != n)           cout <<    ;         else           cout <<   n ;       }     }     return 0;   }