#include<bits/stdc++.h>   using namespace std;   #define int long long       void hota(){     int n,m,x;     cin>>n>>m>>x;     vector<pair<int,int>> v;     for (int i = 0; i < n; ++i)     {      int x;      cin>>x;      v.push_back(make_pair(x,i+1));     }     sort(v.begin(), v.end());     std::vector<int> ans(m,0);     vector<pair<int,int>> r;     int j=0;     while(j<n)     {      int i =0;      while(i<m && j<n)      {       ans[i]+=v[j].first;       r.push_back(make_pair(v[j].second,i+1));       i++;       j++;      }     }     int mx = *max_element(ans.begin(), ans.end());     int mn = *min_element(ans.begin(), ans.end());     if(mx-mn>x || mn == 0)      {cout<< NO <<endl;}     else     {      sort(r.begin(), r.end());      cout<< YES <<endl;      for (int i = 0; i < n; ++i)      {       cout<<r[i].second<<   ;      }      cout<<endl;     }    }       signed main(){     std::ios::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin>>t;     for(int tc=1;tc<=t;tc++){           hota();     }    }


#include <bits/stdc++.h>   using namespace std;   vector<string> substrings;   bool cmp(pair<int, int> &a, pair<int, int> &b) {     if (a.first < b.first) return true;     if (a.first > b.first) return false;     if (substrings[a.second].length() > substrings[b.second].length())       return true;     return false;   }   int main() {     int n, m, pos, dlen, maxlen = 1;     cin >> n;     string dummy;     vector<pair<int, int> > v;     for (int i = 0; i < n; i++) {       cin >> dummy >> m;       substrings.push_back(dummy);       dlen = dummy.length();       for (int j = 0; j < m; j++) {         cin >> pos;         if (dlen + pos - 1 > maxlen) maxlen = dlen + pos - 1;         v.push_back({pos - 1, i});       }     }     sort(v.begin(), v.end(), cmp);     string s(maxlen,  a );     int curr_pos = v[0].first, offset, copy_size;     s.replace(curr_pos, substrings[v[0].second].length(),               substrings[v[0].second]);     curr_pos += substrings[v[0].second].length();     for (int i = 1; i < v.size(); i++) {       if (v[i].first + substrings[v[i].second].length() <= curr_pos) continue;       offset = curr_pos - v[i].first;       if (offset < 0) {         offset = 0;         curr_pos = v[i].first;       }       copy_size = substrings[v[i].second].length() - offset;       s.replace(curr_pos, copy_size, substrings[v[i].second], offset, copy_size);       curr_pos += copy_size;     }     cout << s <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<string, string> mp;   string get_type(string s) {     if (mp.count(s)) return mp[s];     int n = s.size();     if (s[0] ==  &  && s[n - 1] ==  * ) return get_type(s.substr(1, n - 2));     if (s[0] ==  & ) {       string ret = get_type(s.substr(1, n - 1));       int sz = ret.size();       if (ret ==  void  or ret ==  errtype )         return  errtype ;       else {         if (ret[sz - 1] ==  * ) return ret.substr(0, sz - 1);         return s[0] + ret;       }     }     if (s[n - 1] ==  * ) {       string ret = get_type(s.substr(0, n - 1));       if (ret[0] ==  & ) return ret.substr(1, n - 1);       if (ret ==  errtype ) return  errtype ;       return get_type(s.substr(0, n - 1)) + s[n - 1];     }     return  errtype ;   }   void add_type(string u, string v) { mp[v] = get_type(u); }   int main() {     mp[ void ] =  void ;     mp[ &void ] =  errtype ;     mp[ errtype ] =  errtype ;     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int q;     cin >> q;     while (q--) {       string fun, u, v;       cin >> fun;       if (fun ==  typedef ) {         cin >> u >> v;         add_type(u, v);       } else {         cin >> u;         cout << get_type(u) <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 8;   int main() {     char r1, r2;     int x1, x2;     while (4 == scanf(  %c%d %c%d , &r1, &x1, &r2, &x2)) {       int y1 = r1 -  a ;       int y2 = r2 -  a ;       --x1;       --x2;       int ans = 0;       for (int y = 0; y < (int)(N); y++)         for (int x = 0; x < (int)(N); x++) {           if (y1 == y || x1 == x) continue;           int dx1 = abs(x1 - x), dy1 = abs(y1 - y);           int dx2 = abs(x2 - x), dy2 = abs(y2 - y);           if (dx1 > dy1) swap(dx1, dy1);           if (dx2 > dy2) swap(dx2, dy2);           if (dx1 == 1 && dy1 == 2) continue;           if (dx2 == 1 && dy2 == 2) continue;           if (dx2 == 0 && dy2 == 0) continue;           ++ans;         }       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 998244353;   const int N = 1e5 + 2;   const int M = 1 << 19;   const int inf = 2e9;   const long long linf = 1e18;   long long n, m, k;   int ok(long long mid) {     long long ans = 0;     for (int i = 1; i <= n; i++) {       ans += min(m, mid / i);     }     return ans >= k;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> m >> k;     long long l = 1, r = n * m, ans = 0;     while (l <= r) {       long long mid = l + r >> 1;       if (ok(mid)) {         r = mid - 1;         ans = mid;       } else         l = mid + 1;     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   double pi = M_PI;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) {       long long n;       cin >> n;       n = n * 2;       double angle = ((n - 2) * M_PI) / (2 * n);       cout << fixed << setprecision(9) << tan(angle);       cout <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int tc;     cin >> tc;     while (tc--) {       int n;       cin >> n;       priority_queue<pair<int, int> > pq;       for (int i = 0; i < n; i++) {         int ai;         cin >> ai;         pq.push(pair<int, int>(ai, i));       }       int turn = 0;       while (not pq.empty()) {         auto pp = pq.top();         pq.pop();         turn = 1 - turn;         if (pq.empty()) break;         auto qq = pq.top();         pq.pop();         turn = 1 - turn;         pp.first -= 1;         qq.first -= 1;         if (pp.first != 0) pq.push(pp);         if (qq.first != 0) pq.push(qq);       }       if (turn == 0)         cout <<  HL n ;       else         cout <<  T n ;     }     return (0);   }


#include <bits/stdc++.h>   using namespace std;   const long long int N = 2e5 + 5;   void solve() {     long long int n;     cin >> n;     long long int k = sqrt(n);     long long int sum = 0;     if (n == k * k) {       cout << 4 * k <<   n ;       return;     }     sum = sum + k * 4;     n = n - (k * k);     if (n > (k)) {       sum = sum + (n / (k)) * 2;       if (n % k != 0) sum = sum + 2;     } else {       sum = sum + 2;     }     cout << sum <<   n ;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long int t = 1;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100005;   vector<int> g[N], ans;   bool vis[N];   int x[N];   void Set(int u) {     x[u] ^= 1;     ans.push_back(u);   }   void dfs(int u) {     Set(u);     vis[u] = 1;     for (int i = 0; i < g[u].size(); ++i) {       int v = g[u][i];       if (vis[v]) continue;       dfs(v);       Set(u);       if (x[v]) {         Set(v);         Set(u);       }     }   }   int main() {     int n, m, u, v, r = 0;     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; ++i) {       scanf( %d%d , &u, &v);       g[u].push_back(v);       g[v].push_back(u);     }     for (int i = 1; i <= n; ++i) {       scanf( %d , &x[i]);       if (x[i]) r = i;     }     dfs(r);     int f = 0;     if (x[r]) x[r] = 0, f = 1;     bool ok = true;     for (int i = 1; i <= n; ++i)       if (x[i]) {         ok = false;         break;       }     if (!ok)       puts( -1 );     else {       printf( %d n , ans.size() - f);       for (int i = 0; i < ans.size() - f; ++i) printf( %d  , ans[i]);     }     return 0;   }


#include <bits/stdc++.h>   const int T_DATA = 100000;   const int N = 100000;   int T;   int a, b;   char mp[N + 10];   int n, m;   int l[N + 10], r[N + 10];   int dis[N + 10];   int main() {     scanf( %d , &T);     while (T--) {       scanf( %d %d , &a, &b);       scanf( %s , mp + 1);       n = strlen(mp + 1);       m = 0;       for (int i = 1, j; i <= n; i = j + 1) {         while (i <= n && mp[i] !=  1 ) ++i;         if (i > n) break;         for (j = i; j < n && mp[j + 1] ==  1 ; ++j)           ;         ++m;         l[m] = i, r[m] = j;       }       if (m == 0) {         printf( 0 n );         continue;       }       if (m == 1) {         printf( %d n , a);         continue;       }       for (int i = 1; i <= m - 1; ++i) dis[i] = l[i + 1] - r[i] - 1;       std::sort(dis + 1, dis + m);       int ans = a * m;       for (int ind = 1; ind <= m - 1; ++ind) {         if (dis[ind] * b >= a) continue;         ans -= a - dis[ind] * b;       }       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os <<  (  << p.first <<  ,   << p.second <<  ) ; }   template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os <<  { ; string sep; for (const T &x : v) os << sep << x, sep =  ,  ; return os <<  } ; }   void dbg_out() { cerr << endl; }   template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr <<     << H; dbg_out(T...); }   #ifdef WA_DEBUG   #define dbg(...) cerr <<  (  << #__VA_ARGS__ <<  ): , dbg_out(__VA_ARGS__)   #else   #define dbg(...)   #endif      using ll  = long long;   using ull = unsigned long long;   #define pb push_back   #define fi first   #define se second   #define rep(i,a,b) for(int i=int(a);i<=(int)(b);i++)   #define per(i,a,b) for(int i=int(a);i>=(int)(b);i--)   const int mod  = 1e9+7;   const int inf  = 0x3f3f3f3f;   const int maxn = 1e5+10;   int main() {   #ifndef WA_DEBUG       ios::sync_with_stdio(false);cin.tie(nullptr);   #endif       int _;       cin>>_;       while(_--) {           int n;           cin>>n;            string a,b,c;           cin>>a>>b>>c;           int lena=0,lenb=0,lenc=0;           rep(i,1,3*n) {               vector<int> cnt(2,0);               if(lena<2*n) cnt[a[lena]- 0 ]++;               if(lenb<2*n) cnt[b[lenb]- 0 ]++;               if(lenc<2*n) cnt[c[lenc]- 0 ]++;               if(cnt[0]>cnt[1]) {                   cout<<0;                   if(lena<2*n&&a[lena]== 0 ) lena++;                   if(lenb<2*n&&b[lenb]== 0 ) lenb++;                   if(lenc<2*n&&c[lenc]== 0 ) lenc++;               }               else {                   cout<<1;                   if(lena<2*n&&a[lena]== 1 ) lena++;                   if(lenb<2*n&&b[lenb]== 1 ) lenb++;                   if(lenc<2*n&&c[lenc]== 1 ) lenc++;               }               if(lena==2*n||lenb==2*n||lenc==2*n) {                   break;               }           }           if(lena==2*n) {               if(lenb>lenc) {                   while(lenb<2*n) cout<<b[lenb++];               }               else {                   while(lenc<2*n) cout<<c[lenc++];               }           }           else if(lenb==2*n) {               if(lena>lenc) {                   while(lena<2*n) cout<<a[lena++];               }               else {                   while(lenc<2*n) cout<<c[lenc++];               }           }           else {               if(lena>lenb) {                   while(lena<2*n) cout<<a[lena++];               }               else {                   while(lenb<2*n) cout<<b[lenb++];               }           }           cout<<  n ;       }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   int NumberOfJus;   double n;   double sum;   int main() {     cin >> NumberOfJus;     for (int i = 0; i < NumberOfJus; i++) {       cin >> n;       sum += n / NumberOfJus;     }     cout << sum;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxbuff = 1 << 17;   char ibuff[maxbuff], obuff[maxbuff];   inline char Getchar() {     static char *p1 = ibuff, *p2 = ibuff;     if (p1 == p2) {       p1 = ibuff;       p2 = ibuff + fread(ibuff, 1, maxbuff, stdin);       if (p1 == p2) return -1;     }     return *p1++;   }   int optr = 0;   inline void Putchar(char c) {     if (optr == maxbuff) {       fwrite(obuff, 1, optr, stdout);       optr = 0;     }     obuff[optr++] = c;   }   inline void Flush() { fwrite(obuff, 1, optr, stdout); }   template <class T>   inline int read(T &x) {     x = 0;     char c = Getchar();     if (c == EOF) return -1;     for (; c <  0  || c >  9 ; c = Getchar())       ;     for (; c >=  0  && c <=  9 ; c = Getchar()) x = x * 10 + c -  0 ;     return 0;   }   template <class T>   inline void write(const T &x, int tag = 0) {     if (!x) {       if (!tag) Putchar(x +  0 );       return;     }     write(x / 10, 1);     Putchar(x % 10 +  0 );   }   template <class T>   inline void writeln(const T &x) {     write(x);     Putchar(  n );   }   template <class T>   inline void writesp(const T &x) {     write(x);     Putchar(   );   }   const int N = 4e6 + 19;   long long num[N], A[N];   int cnt, fac[N];   map<long long, int> mp1, mp2;   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   void solve(int n, long long x, long long y) {     long long t = y;     for (long long i = 2; i <= 1000000 && i * i <= y && i <= t; ++i) {       if (t % i == 0) {         while (t % i == 0) t /= i;         fac[cnt++] = i;       }     }     for (int i = 0; i < n; ++i) {       if (A[i] % x) continue;       long long tmp = gcd(A[i] / x, y / x);       int msk = 0;       for (int j = 0; j < cnt; ++j) msk |= (tmp % fac[j] == 0) << j;       ++num[msk];     }     for (int i = 0; i < cnt; ++i)       for (int j = 0; j < (1 << cnt); ++j)         if ((j & (1 << i)) == 0) num[j | (1 << i)] += num[j];     long long ans = 0;     for (int i = 0; i < n; ++i) {       if (y % A[i]) continue;       long long tmp = y / A[i];       int msk = 0;       for (int j = 0; j < cnt; ++j) msk |= (tmp % fac[j] == 0) << j;       ans += num[((1 << cnt) - 1) ^ msk];     }     writeln(ans);   }   signed main() {     long long n, x, y;     read(n);     read(x);     read(y);     if (y % x) {       puts( 0 );       return 0;     }     for (int i = 0; i < n; ++i) {       read(A[i]);       if (A[i] % x == 0) ++mp1[gcd(A[i] / x, y / x)];       if (y % A[i] == 0) ++mp2[y / A[i]];     }     if (mp1.size() < 5000 && mp2.size() < 5000) {       long long ans = 0;       for (auto u : mp1)         for (auto v : mp2)           if (gcd(u.first, v.first) == 1) ans += 1ll * u.second * v.second;       writeln(ans);     } else       solve(n, x, y);     Flush();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   pair<long long, long long> a[1000000];   long long ans = 0;   bool check(long long k, pair<long long, long long> l,              pair<long long, long long> r) {     long long cur = abs(k - l.second) + abs(k - r.second);     if (cur <= r.first - l.first) {       return true;     }     return false;   }   int main() {     long long n, m;     cin >> n >> m;     for (long long i = 1; i <= m; ++i) {       cin >> a[i].first >> a[i].second;       ans = max(ans, a[i].second * 1LL);     }     if (a[m].first != n) {       a[m + 1] = make_pair(n, a[m].second + n - a[m].first);       ++m;       ans = max(ans, a[m].second);     }     if (a[1].first != 1) {       for (int i = m + 1; i >= 2; --i) {         a[i] = a[i - 1];       }       a[1] = make_pair(1, a[2].second + a[2].first - 1);       ans = max(ans, a[1].second);       ++m;     }     for (long long i = 1; i <= m - 1; ++i) {       if (abs(a[i + 1].second - a[i].second) > a[i + 1].first - a[i].first) {         return 0 * printf( IMPOSSIBLE );       }       long long bot = min(a[i].second, a[i + 1].second);       long long top = 22814882281488;       while (top > bot + 1) {         long long mid = (bot + top) / 2;         if (check(mid, a[i], a[i + 1])) {           bot = mid;         } else {           top = mid;         }       }       ans = max(ans, bot);     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   long long int key[300005], dia[300005], m1[300005], m2[300005], dp1[300005],       dp2[300005];   vector<long long int> kv[300005], adj[300005];   bool mrk[300005];   inline void dfs(long long int x, long long int pr, long long int root) {     if (mrk[x]) return;     mrk[x] = 1;     key[x] = root;     if (x != root) kv[root].push_back(x);     for (auto j : adj[x]) {       if (j == pr) continue;       if (mrk[j]) continue;       dfs(j, x, root);       if (dp1[j] >= m1[x]) {         m2[x] = m1[x];         m1[x] = dp1[j];       } else if (dp1[j] >= m2[x])         m2[x] = dp1[j];     }     dp1[x] = m1[x] + 1;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     long long int n, m, q;     cin >> n >> m >> q;     for (long long int i = 1; i <= n; i++) {       m1[i] = m2[i] = -1;       key[i] = i;       kv[i].push_back(i);     }     for (long long int i = 0; i < m; i++) {       long long int x, y;       cin >> x >> y;       adj[x].push_back(y);       adj[y].push_back(x);     }     for (long long int i = 1; i <= n; i++) {       if (!mrk[i]) dfs(i, -1, i);     }     long long int mx = 0;     for (long long int i = 1; i <= n; i++) {       dp2[i] = m1[i] + m2[i] + 2;       mx = max(dp1[i], dp2[i]);       dia[key[i]] = max(dia[key[i]], mx);     }     while (q--) {       long long int ch;       cin >> ch;       if (ch == 1) {         long long int x;         cin >> x;         cout << dia[key[x]] <<   n ;         continue;       }       long long int x, y;       cin >> x >> y;       if (key[x] != key[y]) {         if (kv[key[x]].size() < kv[key[y]].size()) swap(x, y);         long long int xx = ceil(dia[key[x]] / 2.0) + ceil(dia[key[y]] / 2.0) + 1;         dia[key[x]] = max(max(dia[key[x]], dia[key[y]]), xx);         long long int z = key[y];         for (auto j : kv[z]) {           kv[key[x]].push_back(j);           key[j] = key[x];         }         kv[z].clear();       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1010, maxm = 1 << 16, INF = 1e9;   int n, dp[2][maxm], a[5], cMask[maxn];   vector<int> optionalMasks[5];   string s[4];   int addColumn(int column, int mask) {     return (mask >> 4) | (cMask[column] << 12);   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= 4; i++) scanf( %d , &a[i]);     for (int i = 0; i < 4; i++) cin >> s[i];     for (int sz = 1; sz <= 4; sz++) {       for (int i = 0; i + sz <= 4; i++) {         int mask = maxm - 1;         for (int c = 0; c < sz; c++) {           for (int r = i; r < i + sz; r++) {             mask ^= 1 << (r + c * 4);           }         }         optionalMasks[sz].push_back(mask);       }     }     for (int i = 0; i < n; i++) {       int mask = 0;       for (int j = 0; j < 4; j++) mask |= (s[j][i] ==  * ) << j;       cMask[i] = mask;     }     for (int i = 0; i < 2; i++)       for (int j = 0; j < maxm; j++) dp[i][j] = INF;     int mask = 0, cur = 0;     for (int i = 0; i < 4; i++) mask = addColumn(i, mask);     dp[cur][mask] = 0;     for (int i = 0; i < n; i++) {       int nxt = !cur;       for (int j = maxm - 1; j >= 0; j--) {         if (dp[cur][j] < INF) {           if (!(j & 15)) {             int nxtJ = addColumn(i + 4, j);             dp[nxt][nxtJ] = min(dp[nxt][nxtJ], dp[cur][j]);           }           for (int sz = 1; sz <= 4 && i + sz <= n; sz++) {             for (int k = 0; k < optionalMasks[sz].size(); k++) {               int nxtJ = j & optionalMasks[sz][k];               dp[cur][nxtJ] = min(dp[cur][nxtJ], dp[cur][j] + a[sz]);             }           }         }         dp[cur][j] = INF;       }       cur = !cur;     }     printf( %d n , dp[cur][0]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int const N = 100000;   vector<pair<int, int> > g[N + 1];   vector<int> imp;   long long cl[42][N + 1];   int dp[N + 1], n;   pair<int, long long> pa[N + 1][17];   bool vis[N + 1];   void pl(int v = 1, int p = 0, int w = 0) {     dp[v] = dp[p] + 1;     vis[v] = true;     pa[v][0] = make_pair(p, w);     int k = 1, z;     while (k < 17 && (z = pa[v][k - 1].first))       pa[v][k] = make_pair(pa[z][k - 1].first,                            pa[v][k - 1].second + pa[z][k - 1].second),       ++k;     for (int i = 0; i < (int)(g[v].size()); ++i) {       int u = g[v][i].first;       if (u == p) continue;       if (vis[u])         imp.push_back(v), imp.push_back(u);       else         pl(u, v, g[v][i].second);     }   }   long long dis(int a, int b) {     if (dp[a] < dp[b]) swap(a, b);     int d = dp[a] - dp[b], j = 0;     long long an = 0;     while (d) {       if (d & 1) an += pa[a][j].second, a = pa[a][j].first;       d >>= 1;       ++j;     }     if (a == b) return an;     for (int i = 16; i >= 0; --i)       if (pa[a][i].first != pa[b][i].first)         an += pa[a][i].second + pa[b][i].second, a = pa[a][i].first,                                                  b = pa[b][i].first;     return an + pa[a][0].second + pa[b][0].second;   }   void bfs(int v, long long dis[]) {     for (int i = 1; i < (int)(n + 1); ++i) dis[i] = 1e18;     priority_queue<pair<long long, int> > q;     q.push(make_pair(0, v));     dis[v] = 0;     while (!q.empty()) {       int v = q.top().second;       long long d = -q.top().first;       q.pop();       for (int i = 0; i < (int)(g[v].size()); ++i) {         int u = g[v][i].first;         long long nd = d + g[v][i].second;         if (dis[u] > nd) {           dis[u] = nd;           q.push(make_pair(-nd, u));         }       }     }   }   int main() {     int m;     scanf( %d%d , &n, &m);     for (int i = 0; i < (int)(m); ++i) {       int a, b, c;       scanf( %d%d%d , &a, &b, &c);       g[a].push_back(make_pair(b, c));       g[b].push_back(make_pair(a, c));     }     pl();     sort(imp.begin(), imp.end());     imp.resize(unique(imp.begin(), imp.end()) - imp.begin());     for (int i = 0; i < (int)(imp.size()); ++i) bfs(imp[i], cl[i]);     int q;     scanf( %d , &q);     while (q--) {       int a, b;       scanf( %d%d , &a, &b);       long long an = dis(a, b);       for (int i = 0; i < (int)(imp.size()); ++i)         for (int j = 0; j < (int)(imp.size()); ++j)           an = min(an, cl[i][a] + cl[i][imp[j]] + cl[j][b]);       printf( %lld n , an);     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100010;   int n, m;   int low[maxn], dfn[maxn], v[maxn << 1], A[maxn];   int nxt[maxn << 1], head[maxn], belong[maxn], a[maxn], vis[maxn << 1];   stack<int> s;   int indexs = 0, cnt = 0;   int ans = 0;   void add_edge(int x, int y) {     static int N = 1;     N++;     v[N] = y;     nxt[N] = head[x];     head[x] = N;   }   void tarjan(int x, int fa) {     dfn[x] = low[x] = ++indexs;     for (int i = head[x]; i != -1; i = nxt[i]) {       if (vis[i]) continue;       vis[i] = vis[i ^ 1] = 1;       s.push(i);       if (!dfn[v[i]]) {         tarjan(v[i], x);         low[x] = min(low[x], low[v[i]]);         if (low[v[i]] >= dfn[x]) {           cnt++;           int num = 0, bcnt = 0;           while (1) {             int tmp = s.top();             s.pop();             if (belong[v[tmp]] != cnt) belong[v[tmp]] = cnt, num++;             if (belong[v[tmp ^ 1]] != cnt) belong[v[tmp ^ 1]] = cnt, num++;             a[++bcnt] = tmp;             if (tmp == i) break;           }           if (num == bcnt)             for (int i = 1; i <= bcnt; i++) A[++ans] = a[i];         }       } else         low[x] = min(low[x], dfn[v[i]]);     }   }   int main() {     memset(head, -1, sizeof(head));     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; i++) {       int x, y;       scanf( %d%d , &x, &y);       add_edge(x, y);       add_edge(y, x);     }     for (int i = 1; i <= n; i++)       if (!dfn[i]) tarjan(i, 0);     sort(A + 1, A + 1 + ans);     printf( %d n , ans);     for (int i = 1; i <= ans; i++) cout << (A[i] >> 1) <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b;     cin >> a >> b;     if (a == b) {       cout <<  infinity ;       return 0;     }     if (a < b) {       cout << 0;       return 0;     }     a -= b;     int s = 0;     for (int i = 1; i * i <= a; i++)       if (a % i == 0) {         s += (i > b);         s += (a / i > b);         if (i == a / i and i > b) s--;       }     cout << s;   }


#include <bits/stdc++.h>   using namespace std;   const long long int inf = INT_MAX;   const long long int mod = 1000000007;   long long int gcd(long long int a, long long int b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long int lcm(long long int a, long long int b) {     return (a / gcd(a, b)) * b;   }   int factorial(long long int n) {     return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);   }   long long int sm(long long int n) {     long long int ans = n * (n + 1) / 2;     return ans;   }   bool isprime(long long int x) {     for (long long int i = 2; i * i <= x; i++) {       if (x % i == 0) return 0;     }     return 1;   }   bool perfectSquare(long double x) {     long double sr = sqrt(x);     return ((sr - floor(sr)) == 0);   }   void solve() {     int64_t w, h, i;     cin >> w >> h;     int64_t ans1 = 0, ans2 = 0;     for (i = 2; i <= w; i += 2) ans1 += (w - i + 1);     for (i = 2; i <= h; i += 2) ans2 += (h - i + 1);     cout << ans1 * ans2 <<   n ;     ;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     long long int test_case = 1;     while (test_case--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long b = 0, s = 0, c = 0;   long long nb, ns, nc;   long long pb, ps, pc;   long long r;   bool isPoss(long long m) {     long long rnb = b * m - nb < 0 ? 0 : b * m - nb;     long long rns = s * m - ns < 0 ? 0 : s * m - ns;     long long rnc = c * m - nc < 0 ? 0 : c * m - nc;     long long rs = rnb * pb + rns * ps + rnc * pc;     if (rs <= r && rs >= 0) {       return true;     }     return false;   }   int main() {     string st;     cin >> st;     long long n = st.size();     for (long long i = 0; i < n; i++) {       if (st[i] ==  B ) {         b++;       }       if (st[i] ==  S ) {         s++;       }       if (st[i] ==  C ) {         c++;       }     }     cin >> nb >> ns >> nc;     cin >> pb >> ps >> pc;     cin >> r;     long long l = 0;     long long h = 1e16;     long long m = (h + l) / 2;     int flag = 0;     while (l <= h) {       m = (h + l) / 2;       if (isPoss(m)) {         if (!isPoss(m + 1)) {           flag = 1;           cout << m << endl;           break;         } else {           l = m + 1;         }       } else {         h = m - 1;       }     }     if (flag == 0) cout << 0 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string S;   int ans;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     for (int i = 1; i <= 3; i++) {       getline(cin, S);       ans = 0;       for (int j = 0; j < S.length(); j++) {         if (S[j] ==  a  || S[j] ==  i  || S[j] ==  u  || S[j] ==  e  ||             S[j] ==  o )           ans++;       }       if (ans == 5 && i == 2) {         cout <<  NO n ;         return 0;       } else if (ans == 7 && i % 2 == 1) {         cout <<  NO n ;         return 0;       } else if (ans != 5 && ans != 7) {         cout <<  NO n ;         return 0;       }     }     cout <<  YES n ;   }


#include <bits/stdc++.h>   using namespace std;   long long int a[200000];   long long int left1[200000][2];   long long int right1[200000][2];   long long int ans[200000];   int main() {     long long int n;     cin >> n;     left1[1][0] = 0;     right1[n][0] = 0;     for (long long int i = 1; i <= n; i++) cin >> a[i];     left1[1][1] = a[1];     right1[n][1] = a[n];     for (long long int i = 2; i <= n; i++) {       left1[i][1] = max(a[i], left1[i - 1][1] + 1);       left1[i][0] = left1[i - 1][0] + (left1[i][1] - a[i]);     }     for (long long int i = n - 1; i >= 0; i--) {       right1[i][1] = max(a[i], right1[i + 1][1] + 1);       right1[i][0] = right1[i + 1][0] + right1[i][1] - a[i];     }     for (long long int i = 1; i <= n; i++)       ans[i] = left1[i - 1][0] + right1[i + 1][0] +                max(left1[i][1], right1[i][1]) - a[i];     long long int j = 1e18;     for (long long int i = 1; i <= n; i++) {       j = min(ans[i], j);     }     cout << j;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> poz[100];   int main() {     string s;     cin >> s;     for (int i(0); i <= 26; i++) poz[i].push_back(-1);     for (int i(0); i < s.size(); i++) poz[s[i] -  a ].push_back(i);     for (int i(0); i <= 26; i++) poz[i].push_back(s.size());     int kmin = s.size();     for (int i(0); i < 26; i++) {       int dmax = 0;       for (int j(0); j < poz[i].size() - 1; j++)         dmax = max(dmax, poz[i][j + 1] - poz[i][j]);       kmin = min(kmin, dmax);     }     cout << kmin;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = int64_t;   using vll = vector<ll>;   using vvll = vector<vll>;   using pll = pair<ll, ll>;   using vpll = vector<pll>;   using vvpll = vector<vpll>;   bool sorted(int arr[], int n) {     bool f1 = 1, f2 = 1;     for (int i = 0; i < n - 1; i++) {       if (arr[i + 1] >= arr[i]) {         f1 = 0;         break;       }     }     for (int i = 0; i < n - 1; i++) {       if (arr[i + 1] <= arr[i]) {         f2 = 0;         break;       }     }     return f1 || f2;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     ;     cout.setf(ios::fixed);     cout.precision(32);     ;     int q;     cin >> q;     while (q--) {       int n;       cin >> n;       int arr[n];       for (int i = 0; i < n; i++) cin >> arr[i];       int index = -1;       for (int i = 0; i < n - 1; i++) {         if (abs(arr[i] - arr[i + 1]) != 1) {           index = i;           break;         }       }       if (index != -1) {         reverse(arr, arr + index + 1);         reverse(arr + index + 1, arr + n);       }       if (sorted(arr, n))         cout <<  YES  << endl;       else         cout <<  NO  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     long long int n;     cin >> n;     getline(cin, s);     getline(cin, s);     long long int l = s.length();     long long int a = 0, ii = 0, f = 0;     for (long long int i = 0; i < l; i++) {       if (s[i] ==  A ) {         a++;       } else {         if (s[i] ==  I ) {           ii++;         } else {           f++;         }       }     }     if (ii == 0) {       cout << a << endl;     } else {       if (ii == 1) {         cout << 1 << endl;       } else {         cout << 0 << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 998244353;   long long mul(long long a, long long b) { return (a * b) % MOD; }   long long bin_pow(long long a, long long p) {     if (p == 0) return 1;     long long ans = bin_pow(mul(a, a), p / 2);     if (p % 2) ans = mul(ans, a);     return ans;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n, m, a;     cin >> n >> m >> a;     vector<long long> b(m + 1);     b[0] = 0;     for (long long i = 0; i < m; i++) cin >> b[i + 1];     long long ans = 1;     for (long long i = 0; i < (long long)(b).size() - 1; i++) {       long long seg = b[i + 1] - b[i];       long long p = bin_pow(a, seg);       long long p1 = (p * (p - 1) / 2) % MOD;       long long delta = (p1 + p) % MOD;       ans = mul(ans, delta);     }     long long ost = n - b.back() * 2;     ans = mul(ans, bin_pow(a, ost));     cout << ans;     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   int a[100 + 10];   int main() {     int n;     while (~scanf( %d , &n)) {       int p1, p2, p3, t1, t2;       scanf( %d%d%d%d%d , &p1, &p2, &p3, &t1, &t2);       int ans = 0;       int temp, len = 0;       for (int i = 1; i <= n; i++) {         int x, y;         scanf( %d%d , &x, &y);         ans += (y - x) * p1;         if (i > 1) {           a[len++] = x - temp;         }         temp = y;       }       for (int i = 0; i < len; i++) {         if (a[i] < t1) {           ans += a[i] * p1;         } else if (a[i] < t1 + t2) {           ans += t1 * p1 + (a[i] - t1) * p2;         } else {           ans += t1 * p1 + t2 * p2 + (a[i] - t1 - t2) * p3;         }       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   const int MAX_L = 2005;   const int INF_INT = 0x3f3f3f3f;   const long long INF_LL = 0x7fffffff;   const int MOD = 1e9 + 7;   const double eps = 1e-9;   const double pi = acos(-1);   using namespace std;   int n;   int a[26], b[26][26];   int main() {     while (cin >> n) {       string s;       memset(a, 0, sizeof(a));       memset(b, 0, sizeof(b));       for (int i = 1; i <= n; i++) {         cin >> s;         set<char> st;         for (int i = 0; i < s.size(); i++) st.insert(s[i]);         if (st.size() > 2) continue;         if (st.size() == 1)           a[s[0] -  a ] += s.size();         else if (st.size() == 2) {           char tmp[2];           int t = 0;           for (auto &i : st) {             tmp[t++] = i;           }           b[tmp[0] -  a ][tmp[1] -  a ] += s.size();           b[tmp[1] -  a ][tmp[0] -  a ] += s.size();         }       }       int ans = 0;       for (int i = 0; i < 26; i++) {         for (int j = 0; j < 26; j++) {           if (i == j) continue;           ans = max(ans, a[i] + a[j] + b[i][j]);         }       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void task();   int main() {     srand(time(0));     ios_base::sync_with_stdio(0);     task();     return 0;   }   const int INF = 0x3f3f3f3f;   const int N = 1e6 + 10;   const int M = 1e5 + 10;   struct SufAutomaton {     struct State {       int len;       int link;       int cnt;       map<char, int> next;       State() : len(0), link(-1), cnt(0){};     };     State st[2 * N];     int sz = 1;     int last = 0;     void push(char ch) {       int cur = sz++;       st[cur].len = st[last].len + 1;       st[cur].cnt = 1;       int p = last;       last = cur;       for (; p != -1 && !st[p].next.count(ch); p = st[p].link)         st[p].next[ch] = cur;       if (p == -1) {         st[cur].link = 0;         return;       }       int q = st[p].next[ch];       if (st[p].len + 1 == st[q].len) {         st[cur].link = q;         return;       }       int clone = sz++;       st[clone].len = st[p].len + 1;       st[clone].next = st[q].next;       st[clone].link = st[q].link;       st[q].link = st[cur].link = clone;       for (; p != -1 && st[p].next.count(ch) && st[p].next[ch] == q;            p = st[p].link)         st[p].next[ch] = clone;     }     void calc() {       vector<pair<int, int> > v;       for (int i = 0; i < sz; ++i) v.push_back(pair<int, int>{st[i].len, i});       sort((v.begin()), (v.end()));       reverse((v.begin()), (v.end()));       for (pair<int, int> p : v) st[st[p.second].link].cnt += st[p.second].cnt;     }     int prefix_function(const string&& s) {       int n = s.length();       vector<int> p(n, 0);       for (int i = 1; i < n; ++i) {         int j = p[i - 1];         while (j && s[i] != s[j]) j = p[j - 1];         if (s[i] == s[j]) p[i] = j + 1;         if (p[i] == n / 2) return i;       }     }     int solve(string& s) {       int n = s.length();       int yet = prefix_function(s + s) - n;       int len = 0;       int ans = 0;       int cur = 0;       for (int i = 0; i < n; ++i) {         while (cur != -1 && st[cur].next.count(s[i]) == 0) {           cur = st[cur].link;           len = cur == -1 ? 0 : st[cur].len;         }         if (cur == -1) {           cur = 0;           len = 0;         } else {           ++len;           cur = st[cur].next[s[i]];         }       }       if (len == n) ans += st[cur].cnt;       for (int i = 0; i < yet; ++i) {         if (len == n) {           --len;           if (len <= st[st[cur].link].len) cur = st[cur].link;         }         while (cur != -1 && st[cur].next.count(s[i]) == 0) {           cur = st[cur].link;           len = cur == -1 ? 0 : st[cur].len;         }         if (cur == -1) {           cur = 0;           len = 0;         } else {           ++len;           cur = st[cur].next[s[i]];         }         if (len == n) {           ans += st[cur].cnt;         }       }       return ans;     }   };   string s;   SufAutomaton sa;   void task() {     cin >> s;     for (int i = 0; i < s.length(); ++i) sa.push(s[i]);     sa.calc();     int n;     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> s;       cout << sa.solve(s) <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   struct node {     int a, v, c, next;     node() {}     node(int a, int v, int c, int next) : a(a), v(v), c(c), next(next) {}   } edge[200010];   struct pp {     int v, d1, d2;   };   queue<int> q;   int n, m, tot;   int dis[100010], d[100010], pre[100010], ans[100010], head[100010];   int vis[100010];   void add(int x, int y, int z) {     edge[++tot] = node(x, y, z, head[x]);     head[x] = tot;   }   void SPFA() {     memset(d, 0, sizeof d);     memset(ans, 0, sizeof ans);     memset(vis, 0, sizeof vis);     for (int i = 1; i <= 100010; i++) dis[i] = 111111;     dis[1] = 0;     vis[1] = 1;     q.push(1);     while (!q.empty()) {       int v = q.front();       q.pop();       vis[v] = 0;       for (int i = head[v]; i != -1; i = edge[i].next)         if (dis[v] + 1 < dis[edge[i].v] ||             (dis[v] + 1 == dis[edge[i].v] && d[v] + edge[i].c > d[edge[i].v])) {           dis[edge[i].v] = dis[v] + 1;           d[edge[i].v] = d[v] + edge[i].c;           pre[edge[i].v] = i;           if (!vis[edge[i].v]) {             q.push(edge[i].v);             vis[edge[i].v] = 1;           }         }     }     int xx = n;     while (xx != 1) {       ans[(pre[xx] + 1) / 2] = 1;       xx = edge[pre[xx]].a;     }   }   int main() {     while (scanf( %d %d , &n, &m) != EOF) {       tot = 0;       int sum = 0, x, y, z;       memset(head, -1, sizeof head);       for (int i = 1; i <= m; i++) {         scanf( %d %d %d , &x, &y, &z);         add(y, x, z);         add(x, y, z);         if (z) sum++;       }       SPFA();       printf( %d n , sum + dis[n] - 2 * d[n]);       for (int i = 2; i <= 2 * m; i += 2)         if (ans[i / 2] ^ edge[i].c)           printf( %d %d %d n , edge[i].a, edge[i].v, edge[i].c ^ 1);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long pow1(long long a, long long b) {     if (b == 0) return 1;     if (b == 1) return a;     long long ab = pow1(a, b / 2) % 1000000007;     if (b % 2 == 0)       return (ab * ab) % 1000000007;     else       return ((ab * ab) % 1000000007 * a) % 1000000007;   }   int main() {     string s;     long long k;     cin >> s >> k;     long long ans = 0;     for (int i = (int)(s.size()) - 1; i >= 0; i--) {       if (s[i] ==  0  || s[i] ==  5 ) {         ans = (ans + pow1(2, i)) % 1000000007;       }     }     long long ha = pow1(2, (int)(s.size()) * k) - 1;     ha = (ha * pow1(pow1(2, (int)(s.size())) - 1, 1000000007 - 2)) % 1000000007;     ans = (ans * ha) % 1000000007;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long n;     cin >> n;     vector<long long> arr(n, 0);     vector<long long> pref(n, 0);     vector<long long> suff(n, 0);     map<long long, long long> pre;     map<long long, long long> suf;     for (long long i = 0; i < n; i++) cin >> arr[i];     pref[0] = arr[0];     suff[n - 1] = arr[n - 1];     for (long long i = 1; i < n; i++) pref[i] = pref[i - 1] + arr[i];     for (long long i = n - 2; i >= 0; i--) suff[i] = suff[i + 1] + arr[i];     if (pref[n - 1] % 2 != 0) {       cout <<  NO n ;       return 0;     }     for (long long i = n - 1; i >= 0; i--) suf[arr[i]]++;     long long fl = 0;     for (long long i = 0; i < n - 1; i++) {       long long a = pref[i];       if (suf[arr[i]] == 1)         suf.erase(suf.find(arr[i]));       else         suf[arr[i]]--;       pre[arr[i]]++;       long long b = suff[i + 1];       if (1) {         long long temp = suff[i + 1] + arr[i];         long long diff = temp - pref[n - 1] / 2;         if (suf.find(diff) != suf.end()) {           fl = 1;           break;         }       }       if (a == b) {         fl = 1;         break;       }       if (1) {         long long temp = pref[i] + arr[i + 1];         long long diff = temp - pref[n - 1] / 2;         if (pre.find(diff) != pre.end()) {           fl = 1;           break;         }       }     }     if (!fl)       cout <<  NO n ;     else       cout <<  YES n ;     return 0;   }


#include <bits/stdc++.h>   namespace FASTIO {   char buf[1 << 21];   char *p1, *p2;   template <typename T>   inline void wt(T x, char c =   n ) {     if (x < 0) {       x = -x;       putchar( - );     }     static char sta[sizeof(T) * 8];     T top = 0;     do {       sta[top++] = x % 10;       x /= 10;     } while (x);     while (top) putchar(sta[--top] +  0 );     putchar(c);   }   template <typename T>   inline T rd() {     T sum = 0, fl = 1;     char ch =         (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2)              ? EOF              : *p1++);     for (; ch <  0  || ch >  9 ;          ch = (p1 == p2 &&                        (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2)                    ? EOF                    : *p1++))       if (ch ==  - )         fl = -1;       else if (ch == EOF)         exit(0);     for (;  0  <= ch && ch <=  9 ;          ch = (p1 == p2 &&                        (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2)                    ? EOF                    : *p1++))       sum = sum * 10 + ch -  0 ;     return sum * fl;   }   }  // namespace FASTIO   using namespace FASTIO;   const int N = 3e5 + 2, logN = 19;   int dep[N], anc[N][logN];   inline int lca(int x, int y) {     if (dep[x] < dep[y]) std::swap(x, y);     {       int dt = dep[x] - dep[y];       if (dt)         for (int i = (31 - __builtin_clz(dt)); i >= 0; i--)           if (dt & 1 << i) x = anc[x][i];     }     if (x == y) return x;     for (int i = (31 - __builtin_clz(dep[x])); i >= 0; i--)       if (anc[x][i] != anc[y][i]) {         x = anc[x][i];         y = anc[y][i];       }     return anc[x][0];   }   inline int dist(const int x, const int y) {     return dep[x] + dep[y] - dep[lca(x, y)] * 2;   }   std::vector<int> s1, s2;   int main() {     const int n = rd<int>() + 1;     s1.reserve(n + 1);     s2.reserve(n + 1);     s1.push_back(1);     dep[1] = 1;     for (int i = 2, maxd = 0, d1, d2; i <= n; i++) {       dep[i] = dep[anc[i][0] = rd<int>()] + 1;       for (int j = 1; 1 << j <= dep[i]; j++)         anc[i][j] = anc[anc[i][j - 1]][j - 1];       d1 = s1.empty() ? 0 : dist(i, s1.back());       d2 = s2.empty() ? 0 : dist(i, s2.back());       if (std::max(d1, d2) > maxd) {         maxd = std::max(d1, d2);         if (maxd != d1) {           s1.swap(s2);           std::swap(d1, d2);         }         for (const int &x : s2)           if (dist(x, i) == d1) s1.push_back(x);         s2.clear();       }       if (std::max(d1, d2) == maxd) (maxd == d1 ? s2 : s1).push_back(i);       wt(s1.size() + s2.size());     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100000 + 10;   int n, pre[maxn], son[maxn], dep[maxn], Ans[maxn], cnt, lis[maxn];   vector<int> g[maxn];   int main() {     scanf( %d , &n);     dep[1] = 1;     for (int i = 1; i < n; i++) {       scanf( %d , &pre[i + 1]);       pre[i + 1]++;       g[pre[i + 1]].push_back(i + 1);       son[pre[i + 1]]++;       dep[i + 1] = dep[pre[i + 1]] + 1;     }     int p = 1;     for (int i = 2; i <= n; i++)       if (dep[i] > dep[p]) p = i;     int from = p;     cnt = 0;     int lim = n - dep[p];     for (int i = 1; i <= lim; i++) {       while (pre[p] && son[pre[p]] <= 1) p = pre[p];       if (!pre[p]) break;       Ans[++cnt] = p;       int f = pre[p];       pre[p] = 0;       while (pre[g[f].back()] != f) g[f].pop_back();       pre[p] = g[f].back();       g[pre[p]].push_back(p);       son[pre[p]]++;       son[f]--;     }     int lcnt = 0;     p = from;     lis[++lcnt] = p;     while (pre[p]) {       lis[++lcnt] = pre[p];       p = pre[p];     }     for (int i = lcnt; i >= 1; i--) printf( %d  , lis[i] - 1);     puts(  );     printf( %d n , cnt);     for (int i = cnt; i >= 1; i--) {       int x = Ans[i] - 1;       printf( %d  , x);     }     puts(  );     return 0;   }


#include <bits/stdc++.h>   unsigned *G[222222], Ga[222222], Gi[222222];   void add(unsigned i, unsigned j) {     if (Ga[i] == Gi[i]) {       if (!Ga[i])         G[i] = (unsigned *)calloc(Ga[i] = 1, sizeof(unsigned));       else         G[i] = (unsigned *)realloc(G[i], (Ga[i] <<= 1) * sizeof(unsigned));     }     G[i][Gi[i]++] = j;     return;   }   unsigned P[222222], D[222222], IL[222222], mi, ma;   void F(unsigned n, unsigned p, unsigned d) {     if (!d) {       mi = -1;       ma = 0;     }     unsigned i, j;     P[n] = p;     D[n] = d;     if (IL[n]) {       if (mi > d) mi = d;       if (ma < d) ma = d;     }     for (i = Gi[n]; i--;)       if ((j = G[n][i]) != p) F(j, n, d + 1);     return;   }   unsigned L[222222], Li, Q[9], Qi;   int main() {     unsigned n, i, j, k, x;     scanf( %u , &n);     for (k = n; --k;) {       scanf( %u%u , &i, &j);       add(i, j);       add(j, i);     }     for (i = 0; ++i <= n;)       if (Gi[i] == 1) IL[L[Li++] = i] = 1;     F(L[0], 0, 0);     for (i = j = k = 0; ++i <= n;)       if (D[i] > k) k = D[j = i];     F(j, 0, 0);     for (i = j = k = 0; ++i <= n;)       if (D[i] > k) k = D[j = i];     for (i = k >> 1; i--;) j = P[j];     if (k & 1) {       F(i = P[j], j, 0);       if (mi != ma) goto NO;       F(j, i, 0);       if (mi != ma) goto NO;     } else {       for (i = Gi[j]; i--;) {         F(G[j][i], j, 0);         if (mi != ma) goto NO;         for (x = -1; ++x < Qi;)           if (Q[x] == mi) goto tebak;         Q[Qi++] = mi;         if (Qi > 2) goto NO;       tebak:;       }       for (k = 0, x = -1; ++x < Qi;) k += Q[x] + 1;     }     while (!(k & 1)) k >>= 1;     printf( %u n , k);     return 0;   NO:;     printf( -1 n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int const MAXN = 2e5 + 10;   int n;   int a[MAXN], L[MAXN * 30], R[MAXN * 30];   struct Trie {     int tot = 1;     int trie[MAXN * 30][2];     void insert(int pos) {       int x = 1;       for (int i = 30;; i--) {         int dir = (a[pos] >> i) & 1;         L[x] = min(L[x], pos), R[x] = max(R[x], pos);         if (i < 0) break;         if (!trie[x][dir]) trie[x][dir] = ++tot;         x = trie[x][dir];       }       return;     }     long long query(int x, int now, int dep) {       if (dep < 0) return 0ll;       int dir = (x >> dep) & 1;       if (trie[now][dir])         return query(x, trie[now][dir], dep - 1);       else         return query(x, trie[now][dir ^ 1], dep - 1) + (1ll << dep);     }     long long dfs(int now, int dep) {       if (dep < 0) return 0;       if (trie[now][0] && trie[now][1]) {         long long ans = 1ll << 30;         for (int i = L[trie[now][0]]; i <= R[trie[now][0]]; i++) {           ans = min(ans, (long long)query(a[i], trie[now][1], dep - 1));         }         ans += dfs(trie[now][0], dep - 1);         ans += dfs(trie[now][1], dep - 1);         ans += (1 << dep);         return ans;       } else if (trie[now][0]) {         return dfs(trie[now][0], dep - 1);       } else if (trie[now][1]) {         return dfs(trie[now][1], dep - 1);       }       return 0ll;     }   } trie;   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     sort(a + 1, a + n + 1);     memset(L, 0x3f, sizeof(L));     for (int i = 1; i <= n; i++) trie.insert(i);     printf( %lld n , trie.dfs(1, 30));     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 ,  unroll-all-loops )   #pragma GCC target( sse4.2 )   using namespace std;   ifstream in;   ofstream out;   const long long kk = 1000;   const long long ml = kk * kk;   const long long mod = 998244353;   const long long inf = ml * ml * ml + 7;   const int C = 5;   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   int n, nxt, root;   vector<int> p, comp, sz, edges;   set<pair<int, pair<int, int>>> e;   vector<vector<int>> kids;   bool viv = false;   bool big = false;   const long double PI = 3.1415926535897932384626433;   inline int rev(int val, int b) {     int res = 0;     for (int i = 0; i < b; i++)       if (val & (1 << i)) res |= (1 << (b - 1 - i));     return res;   }   vector<int> mul_stpd(const vector<int> &a, const vector<int> &b) {     vector<long long> res(a.size() + b.size() - 1);     for (int i = 0; i < b.size(); i++) {       for (int j = 0; j < a.size(); j++) {         res[i + j] += (long long)a[j] * b[i];         res[i + j] %= mod;       }     }     return vector<int>(res.begin(), res.end());   }   int pp(int v) {     if (p[v] == v) return v;     return p[v] = pp(p[v]);   }   bool full(int v) {     if (viv)       cout <<  at   << v <<   with sz =   << sz[v] <<   see   << edges[v] << endl;     return ((int)sz[v] - 1) * (sz[v]) == 2 * edges[v];   }   void unit(int v, int u) {     v = pp(v);     u = pp(u);     if (v == u) {       edges[v]++;       return;     }     if (full(v) && full(u)) {       if (viv) cout <<  Fully  << endl;       if (sz[v] < sz[u]) swap(v, u);       sz[v] += sz[u];       edges[v] += edges[u];       p[u] = v;       edges[v]++;       kids[nxt].push_back(comp[v]);       kids[nxt].push_back(comp[u]);       comp[v] = nxt++;     } else {       if (viv) cout <<  Not Fully  << endl;       if (sz[v] < sz[u]) swap(v, u);       if (full(v) && !full(u)) swap(v, u);       sz[v] += sz[u];       edges[v] += edges[u];       p[u] = v;       edges[v]++;       assert(comp[v] >= n);       if (full(u))         kids[comp[v]].push_back(comp[u]);       else {         for (auto s : kids[comp[u]]) kids[comp[v]].push_back(s);       }     }     root = comp[v];   }   void work(int v, int u) { unit(v, u); }   void show() {     for (int i = nxt - 1; i >= 0; i--) {       cout << i <<  :  ;       for (auto s : kids[i]) cout << s <<    ;       cout << endl;     }   }   vector<int> get(int v) {     if (viv) {       cout <<  get(  << v <<  )  << endl;     }     if (v < n) {       return vector<int>({0, 1});     }     vector<int> lres(1, 1);     for (auto s : kids[v]) {       lres = mul_stpd(lres, get(s));       if (viv) {         cout <<   tnow   << v <<  :  ;         for (auto i : lres) cout << i <<    ;         cout << endl;       }     }     while (lres.size() < 2) lres.push_back(0);     lres[1]++;     if (viv) {       cout << v <<  :  ;       for (auto i : lres) cout << i <<    ;       cout << endl;     }     return lres;   }   void solve() {     cin >> n;     nxt = n;     sz.resize(n, 1);     comp.resize(n);     p.resize(n);     edges.resize(n, 0);     for (int i = 0; i < n; i++) p[i] = i, comp[i] = i;     kids.resize(2 * n);     vector<pair<int, int>> use(n * (n - 1) / 2);     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         int w;         cin >> w;         if (i < j) {           use[w - 1] = {i, j};         }       }     }     for (auto p : use) work(p.first, p.second);     if (viv) show();     auto res = get(root);     while (res.size() < n + 1) res.push_back(0);     for (int i = 1; i <= n; i++) cout << res[i] <<    ;     cout << endl;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t = 1;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a1, a2, a3, a4;     scanf( %d %d %d %d , &a1, &a2, &a3, &a4);     int n = min(min(a1, a4), a3);     long long ans = 0;     ans += n * 256;     a1 -= n;     a3 -= n;     a4 -= n;     int m = min(a1, a2);     ans += m * 32;     printf( %lld n , ans);   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007;   const long long inf = 1e14;   long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long n, ans = 0;     cin >> n;     for (long long i = 2; i <= n; i++) {       for (long long j = i + i; j <= n; j += i) {         ans += (j / i);       }     }     cout << ans * 4;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int rem = 1000000007;   vector<long long> v;   vector<long long> d(21);   vector<long long> down(21);   long long n, s;   long long binpow(long long a, long long n) {     if (n == 0) return 1;     long long tmp = binpow(a, n / 2);     if (n & 1) return tmp * tmp % rem * a % rem;     return tmp * tmp % rem;   }   long long one = 1;   long long getc(long long m, long long n) {     if (n < m) return 0;     long long ans = 1;     for (int i = 1; i <= m; ++i)       ans = (n - i + 1) % rem * ans % rem * down[i] % rem;     return ans;   }   void fn(long long indx, long long numnumbers, long long sum) {     if (sum > s) return;     if (indx == n) {       sum = s - sum;       d[numnumbers] += getc(n - one, sum + n - one);       return;     }     fn(indx + one, numnumbers, sum);     fn(indx + one, numnumbers + one, sum + v[indx] + one);   }   int main() {     cin >> n >> s;     for (int i = 1; i <= 20; i++) {       down[i] = binpow(i, rem - 2);     }     v.resize(n);     for (int i = 0; i < n; i++) {       scanf( %I64d , &v[i]);     }     long long rest = getc(n - one, n + s - one);     fn(0, 0, 0);     for (int i = 1; i <= n; i++) {       if (i & 1)         rest -= d[i];       else         rest += d[i];       rest += rem;       rest %= rem;     }     cout << rest;     return 0;   }


#include<bits/stdc++.h>   using namespace std;   int a[305];   int main()   {       int t;       cin>>t;       while(t--)       {           int n,x;           cin>>n;           string s;           cin>>s;           bool flag=false;           if(s== 2020 )               flag=true;           else           {               for(int i=0; i<n; ++i)               {                   string tmp=s;                   if(n-4>=0)                       tmp.erase(i,n-4);                   if(tmp== 2020 )                       flag=true;               }           }           if(flag)               cout<< YES ;           else               cout<< NO ;           cout<<endl;       }       //getchar();       //getchar();       return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> adj[100005];   bitset<100005> vis;   int nodes = 0, edges = 0;   void dfs(int v) {     nodes++;     vis[v] = true;     for (auto nv : adj[v]) {       if (!vis[nv]) dfs(nv);       edges++;     }   }   int main() {     int n, k;     scanf( %d%d , &n, &k);     int x[k], y[k];     for (int i = 0; i < k; i++) {       scanf( %d%d , &x[i], &y[i]);       adj[--x[i]].push_back(--y[i]);       adj[y[i]].push_back(x[i]);     }     int sad = 0;     for (int i = 0; i < n; i++) {       if (!vis[i]) {         nodes = 0;         edges = 0;         dfs(i);         edges >>= 1;         sad += edges - nodes + 1;       }     }     printf( %d n , sad);   }


#include <bits/stdc++.h>   using namespace std;   long long power(long long a, long long b) {     long long ans = 1;     while (b > 0) {       if (b % 2 != 0) {         ans = (ans * a) % 1000000007;       }       a = (a * a) % 1000000007;       b >>= 1;     }     return ans;   }   int main() {     std::ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     int t = 1;     cin >> t;     while (t--) {       long long d, m;       cin >> d >> m;       vector<int> v, p, q;       long long z = 1, s = 1;       v.push_back(1);       for (int i = 2; i < 65; i++) {         long long k = pow(2, i - 1);         if (s + k < d) {           v.push_back(k);           s += k;         } else {           v.push_back(d - s);           break;         }       }       vector<long long> ha;       ha.push_back(1);       ha.push_back(0);       for (int i = 0; i < v.size(); i++) {         vector<long long> u;         u.push_back(1);         for (int j = 0; j < ha.size() - 1; j++) {           u.push_back((ha[j] * v[i] + ha[j + 1]) % m);         }         u.push_back(0);         ha = u;       }       long long sum = 0;       for (int i = 1; i < ha.size(); i++) {         sum += ha[i];         sum %= m;       }       cout << sum << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 7;   void read(long long &x) {     x = 0;     char ch = getchar();     long long pd = 1;     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) {         pd = -pd;       }       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     x *= pd;   }   long long gcd(long long x, long long y) { return !x ? y : gcd(y % x, x); }   long long l, n, m, aa[10010];   long long a[N], b[N];   long long ans, lcm, x, y, bg;   long long mul(long long a, long long b, long long P) {     long long s = 0;     for (; b; b >>= 1, a = (a + a) % P)       if (b & 1) s = (s + a) % P;     return s;   }   long long exgcd(long long a, long long b, long long &x, long long &y) {     if (!b) {       x = 1, y = 0;       return a;     }     long long gd = exgcd(b, a % b, y, x);     y -= a / b * x;     return gd;   }   long long excrt() {     ans = a[1], lcm = b[1], x, y;     for (long long i = 2; i <= l; i++) {       long long B = b[i], C = (a[i] - ans % B + B) % B;       long long d = exgcd(lcm, B, x, y);       if (C % d) return -1;       B = B / d;       C = C / d;       x = (x + B) % B;       x = mul(x, C, B);       ans += x * lcm;       lcm *= B;       ans %= lcm;     }     return ans == 0 ? lcm : ans;   }   signed main() {     scanf( %lld%lld%lld , &n, &m, &l);     if (l > m) {       puts( NO );       return 0;     }     for (register long long i = 1; i <= l; ++i) {       scanf( %lld , &aa[i]);       b[i] = aa[i];     }     for (register long long i = 1; i <= l; ++i) {       a[i] = (1 - i);       while (a[i] < aa[i]) {         a[i] += aa[i];       }       a[i] %= aa[i];     }     long long yy = excrt();     if (yy == -1) {       puts( NO );       return 0;     }     if (yy + l - 1 > m) {       puts( NO );       return 0;     }     if (lcm > n) {       puts( NO );       return 0;     }     for (register long long i = 1; i <= l; ++i) {       if (gcd(lcm, yy + i - 1) != aa[i]) {         puts( NO );         return 0;       }     }     puts( YES );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 998244353;   const long long nax = 1e5 + 100;   long long n, m;   signed main() {     cin >> n >> m;     long long dp[n][m], a[n][m];     for (long long i = 0; i < n; i++)       for (long long j = 0; j < m; j++) {         cin >> a[i][j];         dp[i][j] = 1;       }     for (long long j = 0; j < m; j++)       for (long long i = 1; i < n; i++)         if (a[i][j] >= a[i - 1][j]) dp[i][j] = dp[i - 1][j] + 1;     for (long long i = 0; i < n; i++) sort(dp[i], dp[i] + m);     long long q;     cin >> q;     while (q--) {       long long l, r;       cin >> l >> r;       --l;       --r;       if (dp[r][m - 1] >= (r - l + 1))         cout <<  Yes n ;       else         cout <<  No n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long rdtsc() {     long long tmp;     asm( rdtsc  :  =A (tmp));     return tmp;   }   inline int myrand() { return abs((rand() << 15) ^ rand()); }   inline int rnd(int x) { return myrand() % x; }   const int maxn = (int)1e5;   char s[maxn + 1];   long long hs[maxn + 1];   const long long Q = 239017;   long long qs[maxn + 1];   const int maxc = 26;   int colors[maxc][maxn + 1];   void precalc(int n) {     hs[0] = 0;     for (int i = 0; i < n; ++i) {       hs[i + 1] = hs[i];       hs[i + 1] *= Q;       hs[i + 1] += (int)s[i];     }     qs[0] = 1;     for (int i = 1; i <= n; ++i) qs[i] = qs[i - 1], qs[i] *= Q;     for (int color = 0; color < maxc; ++color) {       colors[color][0] = 0;       for (int i = 0; i < n; ++i)         colors[color][i + 1] = colors[color][i] + (s[i] == color +  a );     }   }   inline long long getHash(int pos, int len) {     long long tmp = hs[pos];     tmp *= qs[len];     long long res = hs[pos + len];     res -= tmp;     return res;   }   const int maxl = 17;   inline int commonLen(int left, int right, int n) {     assert(left < right);     int ans = 0;     for (int it = maxl - 1; it >= 0; --it) {       if (right + ans + (1 << it) <= n &&           getHash(left, ans + (1 << it)) == getHash(right, ans + (1 << it)))         ans += (1 << it);     }     return ans;   }   int gray[maxl][maxn];   int grayLen[maxl];   long long wholeTaken[maxn + 1];   long long getInitial(int n) {     for (int i = 0; i <= n; ++i) wholeTaken[i] = 0;     long long res = 0;     for (int len = 0; len < maxl; ++len) {       grayLen[len] = (1 << (len + 1)) - 1;       int n0 = n - grayLen[len] + 1;       for (int i = 0; i < n0; ++i) {         int &current = gray[len][i];         current = 0;         if (!len) {           current = 1;           continue;         }         int half = i + grayLen[len - 1];         int *countMidColor = colors[s[half] -  a ];         if (gray[len - 1][i] &&             countMidColor[i + grayLen[len]] - countMidColor[i] == 1 &&             getHash(i, grayLen[len - 1]) == getHash(half + 1, grayLen[len - 1]))           current = 1;       }       long long sqrLen = (long long)grayLen[len] * grayLen[len];       for (int i = 0; i < n0; ++i) {         if (!gray[len][i]) continue;         wholeTaken[i] += sqrLen;         wholeTaken[i + grayLen[len]] -= sqrLen;         res += sqrLen;       }     }     for (int i = 1; i <= n; ++i) wholeTaken[i] += wholeTaken[i - 1];     return res;   }   long long changes[maxc][maxn];   void findChanges(int n) {     for (int i = 0; i < maxc; ++i)       for (int j = 0; j < n; ++j) changes[i][j] = 0;     for (int len = 0; len < maxl; ++len) {       int n0 = n - grayLen[len] + 1;       for (int i = 0; i < n0; ++i) {         if (!len) {           for (int c = 0; c < maxc; ++c)             if (c +  a  != s[i]) changes[c][i] += 1;           continue;         }         int half = i + grayLen[len - 1];         if (!gray[len - 1][i] && !gray[len - 1][half + 1]) continue;         long long sqrLen = (long long)grayLen[len] * grayLen[len];         if (gray[len - 1][i] &&             getHash(i, grayLen[len - 1]) == getHash(half + 1, grayLen[len - 1])) {           for (int c = 0; c < maxc; ++c) {             int *countMidColor = colors[c];             if (countMidColor[i + grayLen[len]] - countMidColor[i] == 0) {               changes[c][half] += sqrLen;             }           }         }         int *countMidColor = colors[s[half] -  a ];         int firstLen = commonLen(i, half + 1, n);         if (firstLen >= grayLen[len - 1]) continue;         int left = i + firstLen, right = left + grayLen[len - 1] + 1;         int secondLen = commonLen(left + 1, right + 1, n);         if (firstLen + 1 + secondLen < grayLen[len - 1]) continue;         if (gray[len - 1][half + 1] &&             countMidColor[half + 1 + grayLen[len - 1]] -                     countMidColor[half + 1] ==                 0)           changes[s[right] -  a ][left] += sqrLen;         if (gray[len - 1][i] && countMidColor[half] - countMidColor[i] == 0)           changes[s[left] -  a ][right] += sqrLen;       }     }   }   bool solve() {     if (scanf( %s , s) < 1) return 0;     int n = strlen(s);     precalc(n);     long long ans = getInitial(n);     findChanges(n);     long long maxAdd = 0;     for (int i = 0; i < maxc; ++i)       for (int j = 0; j < n; ++j)         maxAdd = max(maxAdd, -wholeTaken[j] + changes[i][j]);     printf(          %lld            n ,         ans + maxAdd);     return 1;   }   int main() {     srand(rdtsc());     while (1) {       if (!solve()) break;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<string, int> fr;   int main() {     ios_base::sync_with_stdio(false);     string s;     cin >> s;     for (int k = 0; k <= 9; k++) {       string a;       cin >> a;       fr[a] = k;     }     int id = 0;     string act =   ;     for (int k = 0; k < 80; k++) {       act += s[k];       id++;       if (id == 10) {         cout << fr[act];         id = 0;         act =   ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mxn = 1e5 + 10;   int n, m;   bool jest[410][410];   bool vis_sam[mxn], vis_poc[mxn];   int sam[mxn], poc[mxn];   void wczytaj() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; i++) {       int a, b;       scanf( %d%d , &a, &b);       jest[a][b] = true;       jest[b][a] = true;     }   }   void bfs_sam() {     queue<int> q;     q.push(1);     vis_sam[1] = true;     while (!q.empty()) {       int v = q.front();       q.pop();       for (int i = 1; i <= n; i++) {         if (i == v) continue;         if (vis_sam[i]) continue;         if (!jest[i][v]) continue;         sam[i] = sam[v] + 1;         q.push(i);         vis_sam[i] = true;       }     }   }   void bfs_poc() {     queue<int> q;     q.push(1);     vis_poc[1] = true;     while (!q.empty()) {       int v = q.front();       q.pop();       for (int i = 1; i <= n; i++) {         if (i == v) continue;         if (vis_poc[i]) continue;         if (jest[i][v]) continue;         poc[i] = poc[v] + 1;         q.push(i);         vis_poc[i] = true;       }     }   }   int main() {     wczytaj();     bfs_poc();     bfs_sam();     if (vis_poc[n] == false || vis_sam[n] == false) {       printf( -1 n );       return 0;     }     printf( %d n , max(poc[n], sam[n]));   }


#include <bits/stdc++.h>   using namespace std;   const int N = 5050, b = 53, mod = 1073676287;   int n, q, pre[N], suf[N], ans[N], binv[N], dp[N][N];   int mul(int x, int y) { return (x * 1ll * y) % mod; }   int add(int x, int y) { return ((x + y) % mod + mod) % mod; }   int pw(int b, int p) {     if (!p) return 1;     int ret = pw(b, p / 2);     ret = mul(ret, ret);     if (p % 2) ret = mul(ret, b);     return ret;   }   int getl(int l, int r) {     int f = add(pre[r], -pre[l - 1]);     return mul(f, binv[l]);   }   int getr(int l, int r) {     int f = add(suf[l], -suf[r + 1]);     return mul(f, binv[n - r + 1]);   }   int check(int l, int r) { return getl(l, r) == getr(l, r); }   int solve(int i, int j) {     if (i > n || j <= 0 || j < i) return 0;     int &x = dp[i][j];     if (x != -1) return x;     return x = check(i, j) + solve(i, j - 1) + solve(i + 1, j) -                solve(i + 1, j - 1);   }   string s;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> s;     memset(dp, -1, sizeof dp);     n = s.size();     s =  @  + s;     int bb = 1;     for (int i = 1; i <= n; i++) {       binv[i] = pw(bb, mod - 2);       pre[i] = add(pre[i - 1], mul((s[i] -  a ), bb));       bb = mul(bb, b);     }     bb = 1;     for (int i = n; i >= 1; i--) {       suf[i] = add(suf[i + 1], mul((s[i] -  a ), bb));       bb = mul(bb, b);     }     cin >> q;     while (q--) {       int l, r;       cin >> l >> r;       cout << solve(l, r) <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   int a[20];   int main() {     cin >> n >> m;     for (int t, i = 1; i <= n; i++) {       cin >> t;       a[t]++;     }     int ans = 0;     for (int i = 1; i <= m; i++)       for (int j = i + 1; j <= m; j++) ans += a[i] * a[j];     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<int, int> > ke[105];   int d[105], x[105], y[105], a[105];   int n, m, s, t, module;   void dijkstra() {     set<pair<int, int> > q;     for (int u = 1; u <= n; ++u) d[u] = 1000000007;     d[1] = 0;     q.insert(make_pair(d[1], 1));     while (!q.empty()) {       pair<int, int> top = *(q.begin());       q.erase(top);       int u = top.second;       if (u == n) return;       for (int i = 0; i < ke[u].size(); ++i) {         int v = ke[u][i].first;         int w = ke[u][i].second;         if (d[v] > d[u] + w) {           q.erase(make_pair(d[v], v));           d[v] = d[u] + w;           q.insert(make_pair(d[v], v));         }       }     }   }   int main() {     cin >> n >> module;     for (int i = 2; i <= n - 1; ++i) scanf( %d , &a[i]);     for (int i = 1; i <= n; ++i) scanf( %d%d , &x[i], &y[i]);     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= n; ++j) {         if (i != j) {           ke[i].push_back(make_pair(               j, module * (abs(x[i] - x[j]) + abs(y[i] - y[j])) - a[j]));           ke[j].push_back(make_pair(               i, module * (abs(x[i] - x[j]) + abs(y[i] - y[j])) - a[i]));         } else {           ke[i].push_back(make_pair(j, 1000000007));           ke[j].push_back(make_pair(i, 1000000007));         }       }     }     s = 1;     t = n;     dijkstra();     if (d[t] == 1000000007)       printf( -1 );     else       printf( %d n , d[t]);     return 0;   }


#include <bits/stdc++.h>   const long long MOD = 1e9 + 7;   const long long MAXN = 1e6 + 1;   using namespace std;   long long readInt() {     bool minus1 = false;     long long result = 0;     char ch;     ch = getchar();     while (true) {       if (ch ==  - ) break;       if (ch >=  0  && ch <=  9 ) break;       ch = getchar();     }     if (ch ==  - )       minus1 = true;     else       result = ch -  0 ;     while (true) {       ch = getchar();       if (ch <  0  || ch >  9 ) break;       result = result * 10 + (ch -  0 );     }     if (minus1)       return -result;     else       return result;   }   long long a[MAXN];   long long b[MAXN];   long long pref_a[MAXN];   long long pref_b[MAXN];   long long mn[MAXN];   long long n, m;   long long x;   long long GGGG(long long kek) {     int l = 1, r = m;     while (r - l > 1) {       int mid = (l + r) / 2;       if (kek * mn[mid] <= x) {         l = mid;       } else {         r = mid;       }     }     return (kek * mn[r] <= x ? r : kek * mn[l] <= x ? l : 0);   }   int main() {     n = readInt(), m = readInt();     for (int i = 1; i <= n; i++) {       a[i] = readInt();     }     for (int j = 1; j <= m; j++) {       b[j] = readInt();     }     x = readInt();     for (int i = 1; i <= n; i++) {       pref_a[i] = pref_a[i - 1] + a[i];     }     for (int i = 1; i <= m; i++) {       pref_b[i] = pref_b[i - 1] + b[i];     }     multiset<pair<int, int> > second;     for (int i = 1; i <= m; i++) {       mn[i] = MOD;     }     for (int i = 1; i <= m; i++) {       for (int j = i; j <= m; j++) {         mn[j - i + 1] = min(mn[j - i + 1], pref_b[j] - pref_b[i - 1]);       }     }     long long mx = 0;     for (int i = 1; i <= n; i++) {       for (int j = i; j <= n; j++) {         mx = max(mx, (j - i + 1) * GGGG(pref_a[j] - pref_a[i - 1]));       }     }     cout << mx;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int mod = 1000000007;   long long int max(long long int a, long long int b) {     if (a > b)       return a;     else       return b;   }   long long int min(long long int a, long long int b) {     if (a < b)       return a;     else       return b;   }   long long int binpow(long long a, long long b, long long m) {     a %= m;     long long res = 1;     while (b > 0) {       if (b & 1) res = res * a % m;       a = a * a % m;       b >>= 1;     }     return res;   }   long long binpow(long long a, long long b) {     if (b == 0) return 1;     long long res = binpow(a, b / 2);     if (b % 2)       return res * res * a;     else       return res * res;   }   vector<vector<pair<int, int>>> adj;   vector<int> dp;   vector<int> ans;   int dfs(int x, int p) {     for (auto c : adj[x]) {       if (c.first == p) continue;       int val = dfs(c.first, x);       if (val == 0 && c.second == 2) {         dp[x] += val + 1;         ans.push_back(c.first);       } else {         dp[x] += val;       }     }     return dp[x];   }   void solve() {     int n;     cin >> n;     adj.resize(n + 1);     for (int i = 0; i < n - 1; i++) {       int x, y, t;       cin >> x >> y >> t;       adj[x].push_back({y, t});       adj[y].push_back({x, t});     }     dp.resize(n + 1);     dfs(1, 0);     cout << dp[1] << endl;     for (auto c : ans) {       cout << c <<    ;     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int t = 1;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   long long n;   int main() {     ios_base::sync_with_stdio(false);     cin >> n;     cout << (n - 3) * (n - 1) + 1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     long long l = s.size();     for (long long i = 1; i < l - 1; i++) {       if (s[i] == s[i - 1]) {         if (s[i - 1] !=  a  && s[i + 1] !=  a )           s[i] =  a ;         else if (s[i - 1] !=  b  && s[i + 1] !=  b )           s[i] =  b ;         else           s[i] =  c ;       }     }     if (s[l - 1] == s[l - 2]) {       if (s[l - 2] !=  a )         s[l - 1] =  a ;       else if (s[l - 2] !=  b )         s[l - 1] =  b ;       else         s[l - 1] =  c ;     }     cout << s;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     unsigned long long int ops = 0;     long long int x, y, m;     cin >> x >> y >> m;     if (x >= m || y >= m) {       cout << 0 << endl;       return 0;     }     if (x <= 0 && y <= 0) {       cout << -1 << endl;       return 0;     }     long long int mi = min(x, y), ma = max(x, y);     long long int dif = ma - mi;     ops = dif / ma;     if (dif % ma != 0) ops++;     mi += ops * ma;     swap(mi, ma);     while (mi < m && ma < m) {       mi += ma;       swap(mi, ma);       ops++;     }     cout << ops << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const int mod2 = 1e9 + 9;   const int maxN = 5e5 + 100;   const int INF = 1e9 + 7;   const int maxedge = 1e6 + 100;   const long long LL_INF = 1e18 + 11;   using namespace std;   long long n, ans;   map<long long, long long> ma;   void file() {     freopen( pluses.in ,  r , stdin);     freopen( pluses.out ,  w , stdout);   }   void boost() {     ios_base ::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);   }   int main() {     cin >> n;     for (int long long i = 1; i * i <= n; i++) {       if (n % i == 0) {         if (ma[i] == 0) {           ans++;           ma[i]++;         }         if (ma[n / i] == 0) {           ans++;           ma[n / i]++;         }       }     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1.5e3 + 5;   const int T = 1e5 + 5;   const int mod = 1e9 + 7;   int n, m, a, b, p, t;   int fac[T], inv[T], res;   int P[N], F[N], SP[N], SPL[N], S[N][N], L[N][N];   namespace _ {   inline int mul(int x, int y) { return 1ll * x * y % mod; }   inline int dec(int x, int y) {     x -= y;     if (x < 0) x += mod;     return x;   }   inline int add(int x, int y) {     x += y;     if (x >= mod) x -= mod;     return x;   }   inline int modpow(int x, int y, int res = 1) {     for (; y; y >>= 1, x = mul(x, x))       if (y & 1) res = mul(res, x);     return res;   }   inline int C(int n, int m) {     if (m > n) return 0;     return mul(fac[n], mul(inv[m], inv[n - m]));   }   template <typename _Tp>   inline void IN(_Tp& x) {     char ch;     bool flag = 0;     x = 0;     while (ch = getchar(), !isdigit(ch))       if (ch ==  - ) flag = 1;     while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar();     if (flag) x = -x;   }   }  // namespace _   using namespace _;   int main() {     fac[0] = inv[0] = 1;     IN(n), IN(m), IN(a), IN(b), IN(t), p = mul(a, modpow(b, mod - 2));     for (int i = 1; i <= t; ++i) fac[i] = mul(fac[i - 1], i);     for (int i = 1; i <= t; ++i) inv[i] = modpow(fac[i], mod - 2);     for (int i = 0; i <= m; ++i)       if (i <= t)         P[i] = mul(C(t, i), mul(modpow(p, i), modpow(dec(1, p), t - i)));     for (int i = 0; i <= m; ++i) SP[i] = i ? add(P[i], SP[i - 1]) : P[i];     S[0][m] = F[0] = 1;     for (int i = 1; i <= n; ++i) {       for (int r = 1; r <= m; ++r) {         res = dec(mul(SP[r - 1], dec(F[i - 1], L[i - 1][m - r + 1])), SPL[r]);         S[i][r] = mul(P[m - r], res), L[i][r + 1] = add(L[i][r], S[i][r]);         SPL[r] = add(SPL[r - 1], mul(P[r - 1], L[i][r]));       }       for (int r = 1; r <= m; ++r) F[i] = add(F[i], S[i][r]);     }     printf( %d n , F[n]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int matrix[105][105];   int vis[105][105];   int main() {     int n, k;     while (scanf( %d%d , &n, &k) != EOF) {       memset(matrix, 0, sizeof(matrix));       memset(vis, 0, sizeof(vis));       if (k > n * n) {         printf( -1 n );         continue;       }       for (int i = 1; i <= n; i++) {         for (int j = 1; j <= n; j++) {           if (!vis[i][j] && k) {             if (i == j) {               matrix[i][j]++;               vis[i][j] = 1;               k--;             } else if (k >= 2) {               matrix[i][j]++;               matrix[j][i]++;               vis[i][j] = vis[j][i] = 1;               k -= 2;             }           }         }       }       if (k) {         printf( -1 n );         continue;       }       for (int i = 1; i <= n; i++) {         for (int j = 1; j <= n; j++) {           if (j == n) {             printf( %d n , matrix[i][j]);           } else {             printf( %d  , matrix[i][j]);           }         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e4 + 5;   long long int dp[N][102];   int main() {     int n, k, m;     scanf( %d%d%d , &n, &k, &m);     for (int i = 1; i <= 9; i++) dp[1][i % k]++;     long long int wynik = 0, p = 1;     for (int x = 1; x <= n - 1; x++) {       p = (p * 10) % k;       wynik = (wynik * 10) % m;       wynik += dp[x][0] % m;       for (int i = 1; i < k; i++) {         dp[x][i] %= m;         for (int j = 0; j <= 9; j++)           if (j || (x != n - 1)) dp[x + 1][(i + j * p) % k] += dp[x][i];       }       for (int j = 1; j <= 9; j++) dp[x + 1][(j * p) % k]++;     }     wynik *= 9;     wynik += dp[n][0];     printf( %lld n , wynik % m);   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000006, B = 1000001969;   int n, k, m, L[N], R[N];   char a[N], b[N];   long long a1[N], M[N];   set<int> Set;   int actual(int x) {     auto it = Set.lower_bound(x);     return it == Set.end() ? n + 1 : *it;   }   void hash_build(char a[], int n, long long H[]) {     for (int i = 1; i <= n; i++) H[i] = (H[i - 1] * M[1] + a[i]) % B;   }   long long hash_range(long long H[], int L, int R) {     return (H[R] - H[L - 1] * M[R - L + 1] + 1LL * B * B) % B;   }   int main() {     scanf( %d%d , &n, &k);     scanf( %s , b + 1);     m = strlen(b + 1);     for (int i = 1; i <= k; i++) {       scanf( %d , &L[i]);       R[i] = L[i] + m - 1;     }     for (int i = 1; i <= n; i++) {       a[i] =  ? ;       Set.insert(i);     }     for (int i = 1; i <= k; i++) {       for (int j = actual(L[i]); j <= R[i]; j = actual(j + 1)) {         a[j] = b[j - L[i] + 1];         Set.erase(j);       }     }     M[0] = 1;     M[1] = 2309;     for (int i = 2; i < N; i++) M[i] = M[i - 1] * M[1] % B;     hash_build(a, n, a1);     if (k) {       long long H = hash_range(a1, L[1], R[1]);       for (int i = 2; i <= k; i++)         if (hash_range(a1, L[i], R[i]) != H) {           cout << 0 << endl;           return 0;         }     }     long long P = 1;     for (int i = 1; i <= Set.size(); i++) P = P * 26 % 1000000007;     cout << P << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int n;     cin >> n;     long long int arr[n], i, c = 0;     for (i = 0; i < n; i++) {       cin >> arr[i];       if (arr[i] == 1) c++;     }     if (c == n) {       for (i = 0; i < n - 1; i++) cout << 1 <<    ;       cout << 2 <<   n ;     } else {       sort(arr, arr + n);       cout << 1 <<    ;       for (i = 0; i < n - 1; i++) cout << arr[i] <<    ;       cout <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   unsigned N, M;   vector<unsigned> V;   double dens(const unsigned idxV1, const unsigned idxV2, const unsigned eW) {     if (eW == 0) {       return 0;     }     return (double)(V[idxV1] + V[idxV2]) / (double)eW;   }   int main() {     unsigned v1, v2, w, n, m;     double densidad;     double max = 0;     cout << setprecision(20);     cin >> N;     cin >> M;     V = vector<unsigned>(N);     for (n = 0; n < N; n++) {       cin >> w;       V[n] = w;     }     for (m = 0; m < M; m++) {       cin >> v1;       cin >> v2;       cin >> w;       v1--;       v2--;       densidad = dens(v1, v2, w);       if (densidad > max) {         max = densidad;       }     }     cout << max << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const int N = 2e6 + 10;   const long long mod = 1000000007;   const double eps = 1e-9;   const double PI = acos(-1);   template <typename T>   inline void read(T &a) {     char c = getchar();     T x = 0, f = 1;     while (!isdigit(c)) {       if (c ==  - ) f = -1;       c = getchar();     }     while (isdigit(c)) {       x = (x << 1) + (x << 3) + c -  0 ;       c = getchar();     }     a = f * x;   }   int gcd(int a, int b) { return (b > 0) ? gcd(b, a % b) : a; }   int n, m;   char str[N];   int main() {     int T;     read(T);     while (T--) {       read(n), read(m);       scanf( %s , str + 1);       int len = strlen(str + 1);       int dx = 0, dy = 0;       int minx = 0, miny = 0;       int maxx = 0, maxy = 0;       int ansx = 1, ansy = 1;       for (int i = 1; i <= len; i++) {         char it = str[i];         if (it ==  L )           dx--;         else if (it ==  R )           dx++;         else if (it ==  U )           dy--;         else if (it ==  D )           dy++;         minx = min(minx, dx);         miny = min(miny, dy);         maxx = max(maxx, dx);         maxy = max(maxy, dy);         if ((maxx - minx) >= m || (maxy - miny) >= n) break;         ansx = 1 - minx;         ansy = 1 - miny;       }       cout << ansy <<     << ansx << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     cin.tie(0);     cout.tie(0);     ios_base::sync_with_stdio(0);     ;     int T;     cin >> T;     while (T--) {       int n;       string s;       cin >> n;       cin >> s;       int arr[200002];       int cnt = 1;       int idx = 1;       char x = s[0];       for (int i = (1); i <= (n - 1); i++) {         if (s[i] == x) {           cnt++;         } else if (s[i] != x) {           x = s[i];           arr[idx] = cnt;           idx++;           cnt = 1;         }       }       arr[idx] = cnt;       arr[idx + 1] = 2;       int ans = 0;       int idx2 = 1;       int usable_idx = 1;       int len = idx;       while (arr[usable_idx] == 1) {         usable_idx++;       }       while (idx2 <= len) {         if (usable_idx > len) {           ans += (len - idx2) / 2 + 1;           break;         }         if (arr[idx2] == 1) {           arr[usable_idx]--;           if (arr[usable_idx] == 1) {             usable_idx++;             while (arr[usable_idx] == 1) {               usable_idx++;             }           }         } else if (arr[idx2] != 1) {           if (usable_idx == idx2) {             usable_idx++;             while (arr[usable_idx] == 1) {               usable_idx++;             }           }         }         idx2++;         ans++;       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int inf = 1000000000;   const long long int mod = 1000000000 + 7;   inline void IO() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   inline int dcmp(long double x) { return x < -1e-12 ? -1 : (x > 1e-12); }   template <class T>   inline int CHECK(T MASK, int i) {     return (MASK >> i) & 1;   }   template <class T>   inline T ON(T MASK, int i) {     return MASK | (T(1) << i);   }   template <class T>   inline T OFF(T MASK, int i) {     return MASK & (~(T(1) << i));   }   template <typename T>   inline int CNT(T MASK) {     if (numeric_limits<T>::digits <= numeric_limits<unsigned int>::digits)       return __builtin_popcount(MASK);     else       return __builtin_popcountll(MASK);   }   template <class T>   inline int RIGHT(T MASK) {     return log2(MASK & -MASK);   }   int dx4[] = {0, 0, -1, +1};   int dy4[] = {+1, -1, 0, 0};   int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1, 0};   int dy8[] = {0, 1, 1, 1, 0, -1, -1, -1, 0};   inline void I(int& a) { scanf( %d , &a); }   inline void I(long long int& a) { scanf( %I64d , &a); }   inline void I(unsigned long long int& a) { scanf( %I64u , &a); }   inline void I(char* a) { scanf( %s , a); }   char Iarr[2000010];   inline void I(string& a) {     scanf( %s , Iarr);     a = Iarr;   }   template <typename T, typename... Args>   void I(T& a, Args&... args) {     I(a);     I(args...);   }   inline void OUT(int a) { printf( %d , a); }   inline void OUT(long long int a) { printf( %I64d , a); }   inline void OUT(const char* a) { printf( %s , a); }   inline void OUT(char* a) { printf( %s , a); }   inline void OUT(bool a) { printf( %d , a); }   inline void OUT(string a) {     for (__typeof(a.end()) it = (a.begin()) - ((a.begin()) > (a.end()));          it != (a.end()) - ((a.begin()) > (a.end()));          it += 1 - 2 * ((a.begin()) > (a.end())))       printf( %c , *it);   }   inline void OUT(unsigned long long int a) { printf( %I64u , a); }   template <typename T, typename... Args>   void OUT(T a, Args... args) {     OUT(a);     OUT(   );     OUT(args...);   }   template <typename... Args>   void O(Args... args) {     OUT(args...);     OUT(  n );   }   template <typename T1, typename T2>   inline ostream& operator<<(ostream& os, pair<T1, T2> p) {     os <<  {  << p.first <<  ,   << p.second <<  } ;     return os;   }   template <typename T>   inline ostream& operator<<(ostream& os, vector<T>& a) {     os <<  [ ;     for (int i = 0; i < (int)a.size(); i++) {       if (i) os <<  ,  ;       os << a[i];     }     os <<  ] ;     return os;   }   void err(istream_iterator<string> it) {}   template <typename T, typename... Args>   void err(istream_iterator<string> it, T a, Args... args) {     cout << *it <<   =   << a << endl;     err(++it, args...);   }   const int M = 50010;   int pre[M][26];   int query(int in, char c) { return pre[in][c -  a ]; }   int main() {     string s;     I(s);     for (int i = 0; i < (int)s.size(); i++) {       for (int j = 0; j < 26; j++) {         if (i)           pre[i][j] = pre[i - 1][j];         else           pre[i][j] = -1;       }       pre[i][s[i] -  a ] = i;     }     int dp[(int)s.size()][101];     for (int i = 0; i < (int)s.size(); i++) {       for (int j = 1; j <= 100; j++) {         dp[i][j] = -1;         if (j == 1)           dp[i][j] = i;         else if (i) {           dp[i][j] = dp[i - 1][j];           int in;           if (j != 2)             in = dp[i - 1][j - 2];           else             in = i;           if (in > 0) {             int q = query(in - 1, s[i]);             if (q != -1) {               dp[i][j] = max(dp[i][j], q);             }           }         }       }     }     int len, st;     for (int i = 100; i >= 1; i--) {       if (dp[(int)s.size() - 1][i] != -1) {         len = i;         st = dp[(int)s.size() - 1][i];         break;       }     }     string second;     second.resize(len);     for (int i = (int)s.size() - 1; i >= 0 && len > 0; i--) {       if (s[st] == s[i]) {         int u = ((int)second.size() - len) / 2;         second[u] = s[st];         second[u + len - 1] = s[st];         len -= 2;         if (len > 0 && i) st = dp[i - 1][len];       }     }     O(second);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void dbg_out() { cerr <<   b b] n ; }   template <typename Head, typename... Tail>   void dbg_out(Head H, Tail... T) {     cerr << H <<  ,  ;     dbg_out(T...);   }   void solve() {     long double n;     cin >> n;     cout << fixed << setprecision(10);     if (n == 2) {       long double ans = 1;       cout << ans <<   n ;       return;     }     n *= 2;     const double pi = acosl((long double)-1);     double radian = pi / n;     double radius = 1.0 / tan(radian);     cout << radius <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int t = 0;     cin >> t;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long n;     cin >> n;     vector<long long int> a(n);     for (auto &it : a) cin >> it;     long long o = (n + 1) / 2, e = n / 2;     for (auto it : a) {       if (!it) continue;       if (it % 2)         o--;       else         e--;     }     long long l_val = -1, l_idx = -1;     vector<pair<long long int, long long int> > v11, v00, v10, v01;     for (long long i = 0; i < n; i++) {       long long it = a[i];       if (!it) continue;       if (it % 2) {         if (l_val == -1)           v11.push_back({i - l_idx - 1, 1});         else if (l_val % 2)           v11.push_back({i - l_idx - 1, 0});         else           v01.push_back({i - l_idx - 1, 0});       } else {         if (l_val == -1)           v00.push_back({i - l_idx - 1, 1});         else if (l_val % 2 == 0)           v00.push_back({i - l_idx - 1, 0});         else           v10.push_back({i - l_idx - 1, 0});       }       l_idx = i, l_val = it % 2;     }     if (l_val != -1) {       if (l_val % 2)         v11.push_back({n - l_idx - 1, 1});       else         v00.push_back({n - l_idx - 1, 1});     }     sort(v00.begin(), v00.end());     sort(v11.begin(), v11.end());     long long ans = ((long long int)(v01).size()) + ((long long int)(v10).size());     for (auto it : v11) {       if (it.second == 1) continue;       if (o >= it.first)         o -= it.first;       else         ans += 2;     }     for (auto it : v11) {       if (it.second == 0) continue;       if (o >= it.first)         o -= it.first;       else         ans++;     }     for (auto it : v00) {       if (it.second == 1) continue;       if (e >= it.first)         e -= it.first;       else         ans += 2;     }     for (auto it : v00) {       if (it.second == 0) continue;       if (e >= it.first)         e -= it.first;       else         ans++;     }     bool first = 0;     for (auto it : a) first |= it;     if (!first and n > 1) ans++;     cout << ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.precision(20);     long long t = 1;     while (t--) {       solve();       cout <<   n ;     }     cin.get();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void read() { return; }   void wrt() { cout << (string)   n ; }   void wrt(long long t) { cout << t; }   void wrt(int t) { cout << t; }   void wrt(char t) { cout << t; }   void wrt(string t) { cout << t; }   void wrt(double t) { cout << t; }   template <class T>   void wrt(set<T> v);   template <class T>   void wrt(vector<T> v);   template <class T>   void wrt(multiset<T> v);   template <class T, class V>   void wrt(map<T, V> v);   template <class T, class V>   void wrt(pair<T, V> p);   template <size_t T>   void wrt(const char (&a)[T]) {     string s = a;     wrt(s);     wrt();   }   template <class T>   void wrt(set<T> v) {     for (T i : v) {       wrt(i);       wrt(   );     }     wrt();   }   template <class T>   void wrt(vector<T> v) {     for (T i : v) {       wrt(i);       wrt(   );     }     wrt();   }   template <class T>   void wrt(multiset<T> v) {     for (T i : v) {       wrt(i);       wrt(   );     }     wrt();   }   template <class T, class V>   void wrt(map<T, V> v) {     for (auto i : v) {       wrt(i);       wrt(   );     }     wrt();   }   template <class T, class V>   void wrt(pair<T, V> p) {     wrt(p.first);     wrt(   );     wrt(p.second);     wrt();   }   template <class T, class... V>   void wrt(T x, V... args) {     (wrt(x), wrt(   ), wrt(args...));   }   template <class T, class... V>   void read(T &x, V &...args) {     ((cin >> x), read(args...));   }   template <class T>   void readArr(T &arr, int x, int y) {     for (int i = (int)x; i < (int)y; ++i) cin >> arr[i];   }   const int N = 1e6;   const int INF = INT_MAX;   const int MOD = 1e9 + 7;   int countBits(long long number) { return (int)log2(number) + 1; }   long long getSum(vector<int> a) { return accumulate(a.begin(), a.end(), 0ll); }   long long gcd(long long a, long long b) {     return (b == 0) ? (a) : (gcd(b, a %= b));   }   long long lcd(long long a, long long b) { return (a * b) / gcd(a, b); }   long long add(long long x, long long y) { return (x + y) % MOD; }   long long sub(long long x, long long y) { return (x - y + MOD) % MOD; }   long long mul(long long x, long long y) { return (x * 1ll * y) % MOD; }   long long inv(long long p, long long q) {     long long expo = MOD - 2;     while (expo) {       if (expo & 1) p = mul(p, q);       q = mul(q, q), expo >>= 1;     }     return p;   }   long long power(long long x, long long y) {     if (y == 0)       return 1;     else if (y % 2 == 0) {       long long tmp = power(x, y / 2);       return mul(tmp, tmp);     } else       return mul(x, power(x, y - 1));   }   void solveTestCase() {     int n, m;     read(n, m);     vector<int> f(n * m + 1, -1);     vector<vector<int> > r(n, vector<int>(m)), c(m, vector<int>(n));     for (int i = (int)0; i < (int)n; ++i) readArr(r[i], 0, m);     for (int i = (int)0; i < (int)m; ++i) readArr(c[i], 0, n);     for (int i = (int)0; i < (int)n; ++i) {       f[c[0][i]] = i;     }     vector<vector<int> > ans(n, vector<int>(m));     for (int i = (int)0; i < (int)n; ++i) {       int done = -1;       for (int j = (int)0; j < (int)m; ++j) {         if (f[r[i][j]] != -1) {           done = f[r[i][j]];           break;         }       }       for (int j = (int)0; j < (int)m; ++j) {         ans[done][j] = r[i][j];       }     }     wrt(ans);   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0), cout.tie(0);     ;     int t = 1;     cin >> t;     while (t--) solveTestCase();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int dp[100006][2];   int main(void) {     string s;     cin >> s;     int len = s.size();     memset(dp, -1, sizeof(dp));     dp[0][0] = 0;     dp[0][1] = 0;     for (int i = 1; i <= len; ++i) {       dp[i][1] = dp[i - 1][1] + (bool)islower(s[i - 1]);       dp[i][0] = min(dp[i - 1][0], dp[i - 1][1]) + (bool)isupper(s[i - 1]);     }     cout << min(dp[len][0], dp[len][1]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 100, mod = 998244353;   int n, c[N], s[N];   int qpow(int x, int y) {     int res = 1;     while (y) {       if (y & 1) res = 1ll * res * x % mod;       x = 1ll * x * x % mod, y >>= 1;     }     return res;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) scanf( %d , &c[i]);     sort(c + 1, c + n + 1), reverse(c + 1, c + n + 1);     for (int i = 1; i <= n; ++i) s[i] = (s[i - 1] + c[i]) % mod;     int iv = qpow(n, mod - 2);     for (int i = 1; i <= n; ++i) {       int ans = 0;       for (int j = i + 1; j <= n; j += i)         ans = (ans + 1ll * (s[min(n, j + i - 1)] - s[j - 1] + mod) *                          ((j - 1) / i) % mod) %               mod;       printf( %lld  , 1ll * ans * iv % mod);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[100007], b[100007];   vector<int> v, ans;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n, m, x;     cin >> n >> m;     for (int i = 0; i < n; i++) cin >> x, a[x]++;     for (int i = 0; i < n; i++) cin >> x, b[m - x - 1]++;     for (int x = 0; x < 2 * m; x++) {       int i = x % m;       while (a[i]) v.push_back(i), a[i]--;       while (b[i] && v.size())         ans.push_back((v.back() + m - i - 1) % m), v.pop_back(), b[i]--;     }     sort(ans.begin(), ans.end());     for (long i = ans.size() - 1; i >= 0; i--) cout << ans[i] <<    ;   }


#include <bits/stdc++.h>   using namespace std;   int popc(long long int x) {     int cnt = 0;     for (int i = 0; i < 50; i++) {       if ((1ll << i) & x) cnt++;     }     return cnt;   }   int main() {     ios_base::sync_with_stdio(0);     long long int s, x;     cin >> s >> x;     if (s < x) {       cout <<  0 n ;       return 0;     }     if (s == x) {       cout << ((1ll << (popc(s))) - 2) << endl;       return 0;     }     if ((s - x) % 2 == 1) {       cout <<  0  << endl;       return 0;     }     long long int rest = (s - x) / 2;     for (int i = 0; i < 50; i++) {       if ((((1ll << i) & x) != 0) && (((1ll << i) & rest) != 0)) {         cout <<  0 n ;         return 0;       }     }     cout << (1ll << popc(x)) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int p = 1;     int diagjump = n + 1;     for (int i = 1; i <= n; i++) {       p = i;       for (int j = 1; j <= n; j++) {         cout << p <<    ;         if (p == n * j)           p++;         else           p += diagjump;       }       cout << endl;     }     return 0;   }


#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);   }   bool checkprime(long long n) {     long long i = 2;     while (i * i <= n) {       if (n % i == 0) return 0;       i++;     }     return 1;   }   long long fact(long long n) {     if (n == 0) return 1;     return n * fact(n - 1);   }   bool compare(const pair<long long, long long>& i,                const pair<long long, long long>& j) {     return i.first < j.first;   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long n, m;     cin >> n >> m;     long long ans = 0;     long long l[m + 1], r[m + 1], t[m + 1], c[m + 1];     for (long long i = 1; i <= m; i++) cin >> l[i] >> r[i] >> t[i] >> c[i];     for (long long i = 1; i <= n; i++) {       long long mxc = 0, mx = 100000, mxi = -1;       for (long long j = 1; j <= m; j++) {         if (i >= l[j] && i <= r[j] && t[j] < mx) {           mxi = j;           mx = t[j];           mxc = c[j];         }       }       ans += mxc;     }     cout << ans <<   n ;     return 0;   }