#include <bits/stdc++.h>   using namespace std;   struct SCIENT {     int index;     int k;     int inv;     long long a[200009];   } sc[3];   vector<pair<int, int> > v;   int K[3];   int dynam[2100][2100][3];   int recurse(int k1, int k2, int which) {     int last;     if (k1 == K[1] && k2 == K[2])       last = INT_MIN;     else if (which == 0)       last = sc[which + 1].a[k1 + 1];     else       last = sc[which + 1].a[k2 + 1];     if (dynam[k1][k2][which] != -1) return dynam[k1][k2][which];     if (k1 == 0 && k2 == 0) return dynam[k1][k2][which] = 0;     if (k1 == 0) {       return dynam[k1][k2][which] = recurse(k1, k2 - 1, 1) + (sc[2].a[k2] < last);     }     if (k2 == 0) {       return dynam[k1][k2][which] = recurse(k1 - 1, k2, 0) + (sc[1].a[k1] < last);     }     int p1 = recurse(k1 - 1, k2, 0) + (sc[1].a[k1] < last);     int p2 = recurse(k1, k2 - 1, 1) + (sc[2].a[k2] < last);     if (p1 < p2) {       return dynam[k1][k2][which] = p1;     } else {       return dynam[k1][k2][which] = p2;     }   }   int reconstruct(int k1, int k2, int which) {     if (k1 == 0 && k2 == 0) return 0;     int last;     if (k1 == K[1] && k2 == K[2])       last = INT_MIN;     else if (which == 0)       last = sc[which + 1].a[k1 + 1];     else       last = sc[which + 1].a[k2 + 1];     if (k1 == 0) {       v.push_back(make_pair(sc[2].a[k2], 2));       return reconstruct(k1, k2 - 1, 1) + (sc[2].a[k2] < last);     }     if (k2 == 0) {       v.push_back(make_pair(sc[1].a[k1], 1));       return reconstruct(k1 - 1, k2, 0) + (sc[1].a[k1] < last);     }     int p1 = dynam[k1 - 1][k2][0] + (sc[1].a[k1] < last);     int p2 = dynam[k1][k2 - 1][1] + (sc[2].a[k2] < last);     if (p1 < p2) {       v.push_back(make_pair(sc[1].a[k1], 1));       reconstruct(k1 - 1, k2, 0);       return p1;     } else {       v.push_back(make_pair(sc[2].a[k2], 2));       reconstruct(k1, k2 - 1, 1);       return p2;     }   }   int main() {     int N;     scanf( %d , &N);     memset(dynam, 0xff, sizeof(dynam));     for (int i = 1; i <= N; i++) {       long long k, a, x, y, m;       cin >> k >> a >> x >> y >> m;       K[i] = k;       sc[i].k = k;       sc[i].index = i;       sc[i].a[1] = a;       sc[i].inv = 0;       for (int j = 2; j <= k; j++) sc[i].a[j] = (sc[i].a[j - 1] * x + y) % m;       reverse(sc[i].a + 1, sc[i].a + k + 1);     }     int n = recurse(sc[1].k, sc[2].k, 2);     reconstruct(sc[1].k, sc[2].k, 2);     cout << n << endl;     for (int i = 0; i < v.size(); i++) {       cout << v[i].first <<     << v[i].second << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 998244353;   const long long N = 2005;   void init() {}   long long n, m, k;   long long dp[N][N];   void solve() {     scanf( %lld%lld%lld , &n, &m, &k);     dp[1][0] = m;     for (long long i = 2; i <= n; i++) {       for (long long j = 0; j < i; j++) {         dp[i][j] = (dp[i][j] + dp[i - 1][j]) % MOD;         if (j) dp[i][j] = (dp[i][j] + dp[i - 1][j - 1] * (m - 1) % MOD) % MOD;       }     }     printf( %lld n , dp[n][k]);   }   signed main() {     long long TTT = 1;     while (TTT--) {       init();       solve();     }     return 0;   }


#include <bits/stdc++.h>   int main() {     int T, t, a, b, n, k, temp;     scanf( %d , &T);     for (t = 0; t < T; t++) {       scanf( %d%d%d , &a, &b, &n);       k = 0;       while (a <= n) {         if (a > b) {           temp = a;           a = b;           b = temp;         }         a += b;         k++;       }       printf( %d n , k);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int chr[1000006];   long long int freq[1000006];   long long int Z[2000006];   void Zalgo(int end) {     int l = 1, r = 1;     for (int i = 2; i < end; ++i) {       if (r > i) {         int pre = Z[i - l + 1];         if (pre + i < r) {           Z[i] = pre;         } else {           l = i;           while (r < end && chr[r] == chr[r - i + 1] &&                  freq[r] == freq[r - i + 1]) {             ++r;           }           Z[i] = r - l;         }       } else {         l = r = i;         while (r < end && chr[r] == chr[r - i + 1] &&                freq[r] == freq[r - i + 1]) {           ++r;         }         Z[i] = r - l;       }     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n, m;     string s, t;     cin >> n >> m;     chr[0] = -1;     freq[0] = 0;     int index = 1;     for (int i = 1; i <= n; ++i) {       string s;       cin >> s;       if (s[s.size() - 1] -  a  == chr[index - 1]) {         freq[index - 1] += stoi(s, nullptr, 10);       } else {         chr[index] = s[s.size() - 1] -  a ;         freq[index] += stoi(s, nullptr, 10);         ++index;       }     }     int mid = index;     chr[index] = -1;     freq[index++] = 0;     for (int i = 1; i <= m; ++i) {       string s;       cin >> s;       if (s[s.size() - 1] -  a  == chr[index - 1]) {         freq[index - 1] += stoi(s, nullptr, 10);       } else {         chr[index] = s[s.size() - 1] -  a ;         freq[index] += stoi(s, nullptr, 10);         ++index;       }     }     reverse(chr, chr + index);     reverse(freq, freq + index);     mid = index - mid - 1;     index--;     Zalgo(index);     long long int ans = 0;     chr[index] = -1;     freq[index] = 0;     for (int i = mid + 1; i <= index; ++i) {       if (Z[i] == mid - 1 && chr[0] == chr[i - 1] && freq[0] <= freq[i - 1]) {         if (mid == 1) {           ans += freq[i - 1] - freq[0] + 1;         } else {           ++ans;         }       } else if (Z[i] == mid - 2 && chr[0] == chr[i - 1] &&                  freq[0] <= freq[i - 1] && chr[mid - 1] == chr[i + mid - 2] &&                  freq[mid - 1] <= freq[i + mid - 2]) {         ++ans;       }     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct InputReader {     char buf[1048576];     int p;     inline InputReader() { p = 1048576; }     inline void Flush() {       p = 0;       fread(buf, 1, 1048576, stdin);     }     inline char C() {       if (p >= 1048576) Flush();       return buf[p++];     }     inline char Readnum() {       char ch = C();       while (!isdigit(ch) && ch !=  - ) ch = C();       return ch;     }     inline void Readalpha(char &c) {       c = C();       while (!isalpha(c)) c = C();     }     int operator()() {       int ans = 0, fu = 1;       char ch = Readnum();       if (ch ==  - ) fu = -1, ch = C();       while (ch >=  0  && ch <=  9 ) {         ans = ans * 10 + ch -  0 ;         ch = C();       }       return ans * fu;     }     long long Readll() {       long long ans = 0LL, fu = 1LL;       char ch = Readnum();       if (ch ==  - ) fu = -1LL, ch = C();       while (ch >=  0  && ch <=  9 ) {         ans = ans * 10LL + ch -  0 ;         ch = C();       }       return ans * fu;     }     inline void Readstring(string &x) {       x.clear();       char ch = C();       while (!isdigit(ch) && !isalpha(ch) && ch !=  #  && ch !=  . ) ch = C();       while (isdigit(ch) || isalpha(ch) || ch ==  #  || ch ==  . ) {         x += ch;         ch = C();       }     }     inline void Readchstring(char s[]) {       int len = 0;       char ch = C();       while (!isdigit(ch) && !isalpha(ch)) ch = C();       while (isdigit(ch) || isalpha(ch)) {         s[len++] = ch;         ch = C();       }       s[len] =   0 ;     }     inline void Specialread(char &c) {       c = C();       while (!isdigit(c) && !isalpha(c) && c !=  #  && c !=  .  && c !=  =  &&              c !=  B )         c = C();     }   } In;   inline void Read(int &x) { x = In(); }   inline void Read(int &x, int &y) {     x = In();     y = In();   }   inline void Read(int &x1, int &x2, int &x3) {     x1 = In();     x2 = In();     x3 = In();   }   inline void Read(int &x1, int &x2, int &x3, int &x4) {     x1 = In();     x2 = In();     x3 = In();     x4 = In();   }   inline void Read(long long &x) { x = In.Readll(); }   inline void Read(long long &x, long long &y) {     x = In.Readll();     y = In.Readll();   }   inline void Read(long long &x1, long long &x2, long long &x3) {     x1 = In.Readll();     x2 = In.Readll();     x3 = In.Readll();   }   inline void Read(long long &x1, long long &x2, long long &x3, long long &x4) {     x1 = In.Readll();     x2 = In.Readll();     x3 = In.Readll();     x4 = In.Readll();   }   template <typename T>   void Read(T a[], int st, int ed) {     for (int(i) = (st); (i) <= (ed); ++(i)) Read(a[i]);   }   inline void FILEIO() {}   inline void FILEIO(string pname) {     freopen((pname +  .in ).c_str(),  r , stdin);     freopen((pname +  .out ).c_str(),  w , stdout);   }   inline void FILEIO_OICONTEST(string pname) {     freopen((pname +  .in ).c_str(),  r , stdin);     freopen((pname +  .out ).c_str(),  w , stdout);   }   void Printtime() {}   void END() {     Printtime();     exit(0);   }   template <typename T>   void END(T mes) {     cout << mes << endl;     END();   }   template <typename T>   void Print(T a[], int s, int t, char sp =    , char ed =   n ) {     if (s > t) return;     for (int i = s; i < t; i++) cout << a[i] << sp;     cout << a[t] << ed;     cout.flush();   }   template <typename T>   void Print(T a, int s = 0, int t = -1, char sp =    , char ed =   n ) {     if (t == -1) t = a.size() - 1;     for (int i = s; i <= t; i++) cout << a[i] << sp;     cout << ed;     cout.flush();   }   vector<int> Range(int l, int r) {     vector<int> ret{};     for (int(i) = (l); (i) <= (r); ++(i)) ret.push_back(i);     return ret;   }   const long long MOD = 998244353;   template <typename T>   T Mymo(const T &x) {     return x >= MOD ? x - MOD : x;   }   template <typename T>   void Mymod(T &x) {     x = x >= MOD ? x - MOD : x;   }   inline long long Pow(long long a, long long b, const long long p = MOD) {     long long ret = 1;     for (; b; a = a * a % p, b >>= 1LL)       if (b & 1LL) ret = ret * a % p;     return ret;   }   inline long long Inv(long long a, const long long p = MOD) {     return Pow(a, p - 2LL, p);   }   const int MAXN = 1024;   int n, k;   int a[MAXN], mxa;   long long ansl[131072];   int dpsum[MAXN][MAXN];   int Solve(int delta) {     if (delta > mxa / (k - 1)) return 0;     for (int(i) = 1; (i) <= (n); ++(i)) memset(dpsum[i], 0, k + 1 << 2);     int pre = 0;     int ans = 0;     for (int(i) = 1; (i) <= (n); ++(i)) {       while (pre != i - 1 and a[i] - a[pre + 1] >= delta) ++pre;       for (int(j) = (2); (j) <= (((i) < (k) ? (i) : (k))); ++(j)) {         int u = j == 2 ? pre : dpsum[pre][j - 1];         dpsum[i][j] = Mymo(dpsum[i - 1][j] + u);         if (j == k) {           ans = Mymo(ans + u);           Mymod(ans);         }       }     }     return ans;   }   int main() {     FILEIO();     Read(n, k);     Read(a, 1, n);     sort(a + 1, a + 1 + n);     mxa = a[n];     for (int(i) = (0); (i) <= (mxa); ++(i)) {       ansl[i] = Solve(i);     }     long long ans = 0;     for (int(i) = (0); (i) <= (mxa); ++(i)) {       long long cnt = ansl[i] - ansl[i + 1] + MOD;       ans += cnt * i % MOD;       Mymod(ans);     }     cout << ans << endl;     END();   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = (long long)1e18;   const long long MOD = (long long)1e9 + 7;   long long n, r[200000], a[200000], bad, brd, odd, ll, rr, cnt[200000],       cntl[200000], i, j, ans;   int main() {     scanf( %d , &n);     for (i = 0; i < n; ++i) scanf( %d , &r[i]), ++cnt[r[i]];     for (i = 0; i < 200000; ++i)       if (cnt[i] % 2LL) ++odd;     if (odd > 1) {       printf( %d n , 0);       return 0;     }     for (i = 0; i < 200000; ++i) cnt[i] = 0;     for (i = 0; i < n / 2LL && r[i] == r[n - i - 1]; ++i) ++brd;     for (i = brd; i < n - brd; ++i) a[i - brd] = r[i], ++cnt[r[i]];     if (n - 2LL * brd <= 1LL) {       printf( %lld n , (n * (n + 1LL)) / 2LL);       return 0;     }     n -= 2LL * brd;     if (n % 2LL) {       long long gmid = 0;       if (cnt[a[n / 2LL]] % 2LL) {         while (a[n / 2 - gmid] == a[n / 2 + gmid]) ++gmid;       }       for (i = n / 2 - gmid + 1; i <= n / 2 + gmid - 1; ++i) --cnt[a[i]];       long long minl, minr;       bad = 0;       for (i = 0; i < 200000; ++i)         if (cnt[i]) ++bad;       for (i = 0; i < 200000; ++i) cntl[i] = 0;       for (i = 0; i < n; ++i) {         ++cntl[a[i]];         if (i >= n / 2 - gmid + 1 && i <= n / 2 + gmid - 1) --cntl[a[i]];         if (cntl[a[i]] && cntl[a[i]] == (cnt[a[i]] + 1) / 2LL) --bad;         if (i == n / 2 + gmid - 1) {           for (j = n / 2 - gmid + 1; j <= n / 2 + gmid - 1; ++j) {             ++cntl[a[j]];             ++cnt[a[j]];           }           for (j = n / 2 - gmid + 1; j <= n / 2 + gmid - 1; ++j)             if (cntl[a[j]] == (cnt[a[j]] + 1) / 2LL) --bad;         }         if (!bad) {           minl = i;           break;         }       }       gmid = 0;       if (cnt[a[n / 2LL]] % 2LL) {         while (a[n / 2 - gmid] == a[n / 2 + gmid]) ++gmid;       }       for (i = n / 2 - gmid + 1; i <= n / 2 + gmid - 1; ++i) --cnt[a[i]];       bad = 0;       for (i = 0; i < 200000; ++i)         if (cnt[i]) ++bad;       for (i = 0; i < 200000; ++i) cntl[i] = 0;       for (i = n - 1; i >= 0; --i) {         ++cntl[a[i]];         if (i >= n / 2 - gmid + 1 && i <= n / 2 + gmid - 1) --cntl[a[i]];         if (cntl[a[i]] && cntl[a[i]] == (cnt[a[i]] + 1) / 2LL) --bad;         if (i == n / 2 - gmid + 1) {           for (j = n / 2 - gmid + 1; j <= n / 2 + gmid - 1; ++j) {             ++cntl[a[j]];             ++cnt[a[j]];           }           for (j = n / 2 - gmid + 1; j <= n / 2 + gmid - 1; ++j)             if (cntl[a[j]] == (cnt[a[j]] + 1) / 2LL) --bad;         }         if (!bad) {           minr = i;           break;         }       }       printf( %lld n , (brd + 1LL) * (brd + (n - minl)) +                            (brd + 1LL) * (brd + (minr + 1LL)) -                            (brd + 1LL) * (brd + 1LL));     } else {       long long gmid = 0;       while (a[n / 2 - gmid - 1] == a[n / 2 + gmid]) ++gmid;       for (i = n / 2 - gmid; i <= n / 2 + gmid - 1; ++i) --cnt[a[i]];       long long minl, minr;       bad = 0;       for (i = 0; i < 200000; ++i)         if (cnt[i]) ++bad;       for (i = 0; i < 200000; ++i) cntl[i] = 0;       for (i = 0; i < n; ++i) {         ++cntl[a[i]];         if (i >= n / 2 - gmid && i <= n / 2 + gmid - 1) --cntl[a[i]];         if (cntl[a[i]] == cnt[a[i]] / 2LL) --bad;         if (i == n / 2 + gmid - 1)           for (j = n / 2 - gmid; j <= n / 2 + gmid - 1; ++j) {             ++cntl[a[j]];             if (cntl[a[j]] == cnt[a[j]] / 2LL) --bad;           }         if (!bad) {           minl = i;           break;         }       }       bad = 0;       for (i = 0; i < 200000; ++i)         if (cnt[i]) ++bad;       for (i = 0; i < 200000; ++i) cntl[i] = 0;       for (i = n - 1; i >= 0; --i) {         ++cntl[a[i]];         if (i >= n / 2 - gmid && i <= n / 2 + gmid - 1) --cntl[a[i]];         if (cntl[a[i]] == cnt[a[i]] / 2LL) --bad;         if (i == n / 2 - gmid)           for (j = n / 2 - gmid; j <= n / 2 + gmid - 1; ++j) {             ++cntl[a[j]];             if (cntl[a[j]] == cnt[a[j]] / 2LL) --bad;           }         if (!bad) {           minr = i;           break;         }       }       printf( %lld n , (brd + 1LL) * (brd + (n - minl)) +                            (brd + 1LL) * (brd + (minr + 1LL)) -                            (brd + 1LL) * (brd + 1LL));     }   }


#include <bits/stdc++.h>   using namespace std;   int arr[100005];   int dp[100005];   int main() {     int n;     cin >> n;     arr[0] = -2000;     for (long long i = 1; i < n + 1; i++) {       cin >> arr[i];       dp[i] = 1e9;     }     dp[0] = 0;     int pay = 0;     for (long long i = 1; i < n + 1; i++) {       int x = upper_bound(arr, arr + i, arr[i] - 1) - arr;       x--;       dp[i] = min(dp[i], dp[x] + 20);       x = upper_bound(arr, arr + i, arr[i] - 90) - arr;       x--;       dp[i] = min(dp[i], dp[x] + 50);       x = upper_bound(arr, arr + i, arr[i] - 1440) - arr;       x--;       dp[i] = min(dp[i], dp[x] + 120);       cout << dp[i] - pay <<   n ;       pay = dp[i];     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 5;   int n;   string s[maxn];   map<int, pair<int, int>> mp;   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     cin >> n;     int correct = 0;     for (int i = 0; i < n; i++) {       cin >> s[i];       int balance = 0;       int min_balance = 0;       for (char c : s[i]) {         if (c ==  ( ) {           balance++;         } else {           balance--;         }         min_balance = min(min_balance, balance);       }       if (min_balance >= 0) {         if (balance == 0) {           correct++;         } else {           mp[balance].first++;         }       } else {         if (min_balance == balance) {           mp[-balance].second++;         }       }     }     int res = correct / 2;     for (auto& it : mp) {       res += min(it.second.first, it.second.second);     }     cout << res <<   n ;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   int n, a[200005];   long double eps = 1e-11, b[200005], s[200005];   long double f(long double first) {     long double mn = 0, ans = 0;     for (int i = 1; i <= n; i++) {       b[i] = -first + a[i];       s[i] = s[i - 1] + b[i];       ans = max(ans, abs(s[i] - mn));       mn = min(mn, s[i]);     }     mn = 0;     for (int i = 1; i <= n; i++) {       ans = max(ans, abs(s[i] - mn));       mn = max(mn, s[i]);     }     return ans;   }   int32_t main() {     ios_base ::sync_with_stdio(0);     cin.tie();     cout.tie();     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     long double l = -1e9, r = 1e9;     for (int i = 1; i <= 200; i++) {       long double mid1 = (l * 2 + r) / 3, mid2 = (l + r * 2) / 3;       if (f(mid1) > f(mid2))         l = mid1;       else if (f(mid1) < f(mid2))         r = mid2;       else         l = mid1, r = mid2;     }     cout << fixed << setprecision(7) << f(l);   }


#include <bits/stdc++.h>   using namespace std;   int cnt[1000010] = {};   void pre() {     for (int i = 1; i < 1000010; i++) {       for (int j = i; j < 1000010; j += i) {         cnt[j]++;       }     }   }   long long int tree[2 * 1000010];   int n;   void build() {     for (int i = n - 1; i > 0; i--) {       tree[i] = tree[i << 1] + tree[(i << 1) | 1];     }   }   void update(int idx, long long int val) {     tree[idx + n] = val;     for (int i = idx + n; i > 0; i >>= 1) {       tree[i >> 1] = tree[i] + tree[i ^ 1];     }   }   long long int query(int l, int r) {     long long int ans = 0;     l += n;     r += n;     for (; l < r; l >>= 1, r >>= 1) {       if (l & 1) {         ans += tree[l];         l++;       }       if (r & 1) {         r--;         ans += tree[r];       }     }     return ans;   }   int main() {     pre();     int m;     scanf( %d%d , &n, &m);     for (int i = 0; i < n; i++) {       scanf( %lld , &tree[n + i]);     }     build();     set<int> s;     for (int i = 0; i < n; i++) {       if (tree[n + i] > 2) {         s.insert(i);       }     }     while (m--) {       int t, l, r;       scanf( %d %d %d , &t, &l, &r);       l--;       r--;       if (t == 2) {         printf( %lld n , query(l, r + 1));       } else {         l--;         vector<int> v;         if (s.empty()) {           continue;         }         if (s.upper_bound(l) == s.end()) {           continue;         }         int idx = *s.upper_bound(l);         while (idx <= r) {           s.erase(idx);           long long int x = tree[n + idx];           update(idx, (long long int)cnt[x]);           if (cnt[x] > 2) {             v.push_back(idx);           }           if (s.empty()) {             break;           }           if (s.upper_bound(l) == s.end()) {             break;           }           idx = *s.upper_bound(l);         }         for (int u : v) {           s.insert(u);         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 5e3 + 10, MOD = 1e9 + 7;   int n, ans, l = -1, r = 1e4 + 10, mid, mark[MAXN];   pair<int, int> a[MAXN];   bool first;   vector<int> adj[MAXN];   inline int Dis(int i, int j) {     return abs(a[i].first - a[j].first) + abs(a[i].second - a[j].second);   }   inline void Make(int mid) {     memset(mark, 0ll, sizeof(mark));     for (int i = 0; i < n; i++) adj[i].clear();     for (int i = 0; i < n; i++)       for (int j = i + 1; j < n; j++)         if (Dis(i, j) > mid) adj[i].push_back(j), adj[j].push_back(i);   }   inline void DFS(int v) {     for (auto u : adj[v]) {       if (!mark[u])         mark[u] = 3 ^ mark[v], DFS(u);       else         first &= (mark[v] != mark[u]);     }   }   inline bool Check(int mid) {     Make(mid);     ans = first = 1;     for (int i = 0; i < n; i++)       if (!mark[i]) mark[i] = 1, ans <<= 1, ans %= MOD, DFS(i);     return first;   }   int main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i].first >> a[i].second;     while (r - l > 1) {       mid = (l + r) >> 1;       if (Check(mid))         r = mid;       else         l = mid;     }     Check(r);     cout << r <<   n  << ans;   }


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


#include <bits/stdc++.h>   using namespace std;   const int maxn = 300500;   int n, m, q;   vector<int> g[maxn];   bool used[maxn];   int p[maxn], sz[maxn], diameter[maxn];   int maxi, maxiV;   void dfs(int v, int depth, int p) {     if (depth > maxi) {       maxi = depth;       maxiV = v;     }     for (size_t i = 0; i < g[v].size(); i++) {       int to = g[v][i];       if (to != p) dfs(to, depth + 1, v);     }   }   int findLongestWay(int v) {     maxi = -1;     dfs(v, 0, -1);     maxi = -1;     dfs(maxiV, 0, -1);     return maxi;   }   void init() {     for (int i = 1; i <= n; i++) p[i] = i, sz[i] = 1, diameter[i] = 0;   }   int getSet(int v) {     if (p[v] == v) return v;     return p[v] = getSet(p[v]);   }   void unionSets1(int x, int y) {     x = getSet(x);     y = getSet(y);     if (sz[x] > sz[y]) swap(x, y);     sz[y] += sz[x];     p[x] = y;   }   int rad(int x) { return (diameter[x] + 1) / 2; }   void unionSets2(int x, int y) {     x = getSet(x);     y = getSet(y);     if (x == y) return;     if (sz[x] > sz[y]) swap(x, y);     sz[y] += sz[x];     p[x] = y;     diameter[y] = max(rad(x) + rad(y) + 1, max(diameter[x], diameter[y]));   }   int query(int x) { return diameter[getSet(x)]; }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     ;     cin >> n >> m >> q;     init();     for (int i = 1; i <= m; i++) {       int x, y;       cin >> x >> y;       g[x].push_back(y);       g[y].push_back(x);       unionSets1(x, y);     }     for (int i = 1; i <= n; i++) {       int pp = getSet(i);       if (!used[pp]) {         used[pp] = 1;         diameter[pp] = findLongestWay(i);       }     }     while (q--) {       int t, x, y;       cin >> t;       if (t == 2) {         cin >> x >> y;         unionSets2(x, y);       } else {         cin >> x;         printf( %d n , query(x));       }     }   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 1000010;   const double eps = 1e-6;   const double pi = 4 * atan(1);   long long n, a[N], k, res = 1e18;   int solve() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n;     if (n == 2)       cout <<  1  << endl;     else if (n == 3)       cout <<  2  << endl;     else {       long long x = 2, y = 3, k = 2;       res = y;       while (res < n) {         if (x + y > n) break;         res = x + y;         x = y;         y = res;         k++;       }       cout << k << endl;     }     return 0;   }   int main() { return solve(); }


#include <bits/stdc++.h>   using namespace std;   int n;   char s[1001001];   int p[1001001] = {1};   int v[10][1001001], deg[1001001];   int Rand() { return rand() * rand(); }   int main() {     scanf( %s , s);     n = strlen(s);     for (int i = 1; i <= n; i++) {       p[i] = p[i - 1] * 10 % 7;     }     int rlt = 0;     reverse(s, s + n);     for (int i = 0; s[i]; i++) {       int c = s[i] -  0 ;       rlt = (rlt + p[i] * c) % 7;       v[c][deg[c]++] = i;     }     while (rlt) {       int x = rand() % 9 + 1;       int y = rand() % 9 + 1;       if (x == y || !deg[x] || !deg[y]) {         continue;       }       int tx = Rand() % deg[x];       int ty = Rand() % deg[y];       swap(v[x][tx], v[y][ty]);       s[v[x][tx]] = x +  0 ;       s[v[y][ty]] = y +  0 ;       rlt = rlt + (p[v[x][tx]] - p[v[y][ty]]) * (x - y);       rlt %= 7;       if (rlt < 0) rlt += 7;     }     reverse(s, s + n);     puts(s);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int t, ans, tot = 1, n, temp;   bool flag, u[9];   struct none {     long long a;     int sum;   } f[9];   bool cmy(none f1, none f2) { return f1.a > f2.a; }   long long fuck(long long x) {     int back = 0;     for (long long i = 2; i * i <= x; i++)       while (x % i == 0) {         back++;         x /= i;       }     if (x != 1) back++;     if (back > 1) back++;     return back;   }   void init() {     long long x;     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %I64d , &f[i].a);     sort(f + 1, f + 1 + n, cmy);   }   void work() {     for (int i = 1; i <= n; i++) ans += f[i].sum;     for (int i = 1; i < n; i++) {       long long x = f[i].a;       while (1) {         temp = -1;         flag = false;         t = 0;         for (int j = i + 1; j <= n; j++)           if ((!u[j]) && (x % f[j].a == 0) && f[j].sum - 1 > temp) {             if (temp == -1) temp++;             t = j;             flag = true;             ans -= (f[j].sum - 1 - temp);             temp = f[j].sum - 1;           }         if (!flag) break;         x /= f[t].a;         tot++;         u[t] = true;         if (f[t].sum == 1) ans--;       }     }     if (tot < n) ans++;     printf( %d n , ans);   }   int main() {     init();     for (int i = 1; i <= n; i++) f[i].sum = fuck(f[i].a);     work();   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2500 * 4 + 10;   int n, k, d, sz;   bool isV[2000], M = 0;   string arr[N];   set<string> st[N];   string change(string z) {     int c = 0;     string s =   ;     for (int i = z.size() - 1; i >= 0; i--) {       s += z[i];       if (isV[z[i]]) M = 1, c++;       if (c == k) break;     }     if (c != k) M = 0;     reverse(s.begin(), s.end());     return s;   }   bool checkAABB() {     vector<string> v(10);     for (int i = 0; i < sz; i++) {       for (int j = 0; j < 4; j++) {         v[j] = arr[i * 4 + j];       }       if (v[0] != v[1] || v[2] != v[3]) return 0;     }     return 1;   }   bool checkABAB() {     vector<string> v(10);     for (int i = 0; i < sz; i++) {       for (int j = 0; j < 4; j++) {         v[j] = arr[i * 4 + j];       }       if (v[0] != v[2] || v[1] != v[3]) return 0;     }     return 1;   }   bool checkABBA() {     vector<string> v(10);     for (int i = 0; i < sz; i++) {       for (int j = 0; j < 4; j++) {         v[j] = arr[i * 4 + j];       }       if (v[0] != v[3] || v[2] != v[1]) return 0;     }     return 1;   }   bool checkAAAA() {     vector<string> v(10);     for (int i = 0; i < sz; i++) {       for (int j = 0; j < 4; j++) {         v[j] = arr[i * 4 + j];       }       if (v[0] != v[1] || v[2] != v[3] || v[1] != v[2]) return 0;     }     return 1;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     isV[ a ] = isV[ o ] = isV[ i ] = isV[ u ] = isV[ e ] = 1;     cin >> n >> k;     sz = n;     n *= 4;     bool Valid = 1;     for (int i = 0; i < n; i++) {       cin >> arr[i];       M = 0;       arr[i] = change(arr[i]);       if (!M) Valid = 0;     }     if (!Valid) return cout <<  NO  << endl, 0;     if (checkAAAA())       cout <<  aaaa  << endl;     else if (checkABAB())       cout <<  abab  << endl;     else if (checkABBA())       cout <<  abba  << endl;     else if (checkAABB())       cout <<  aabb  << endl;     else       cout <<  NO  << endl;   }


#include <bits/stdc++.h>   using namespace std;   inline long long mul(long long x, long long y) {     if (x >= (1ll << 60) / y)       return (1ll << 60);     else       return x * y;   }   int n, m, p;   int d[100005];   long long cat[100005], sum[100005];   long long dp[105][100005];   int dq[100005], s, e;   int main() {     scanf( %d%d%d , &n, &m, &p);     for (int i = 2; i <= n; ++i) scanf( %d , d + i), d[i] += d[i - 1];     for (int i = 1; i <= m; ++i) {       int h, t;       scanf( %d%d , &h, &t);       cat[i] = t - d[h] + 1000000000;     }     sort(cat + 1, cat + 1 + m);     for (int i = 1; i <= m; ++i) sum[i] = sum[i - 1] + cat[i];     memset(dp, 63, sizeof(dp));     for (int i = 0; i < 105; ++i) dp[i][0] = 0;     for (int k = 1; k <= p; ++k) {       dq[0] = 0, s = 0, e = 1;       for (int i = 1; i <= m; ++i) {         long long c1 = cat[i], c2 = i * c1 - sum[i];         while (s + 1 < e) {           long long x1 = dq[s], y1 = dp[k - 1][x1] + sum[x1];           long long x2 = dq[s + 1], y2 = dp[k - 1][x2] + sum[x2];           if ((y2 - y1) <= mul(c1, (x2 - x1)))             s++;           else             break;         }         int t = dq[s];         dp[k][i] = min(dp[k][i], dp[k - 1][t] + sum[t] - c1 * t + c2);         while (s + 1 < e) {           long long x1 = i, y1 = dp[k - 1][i] + sum[i];           long long x2 = dq[e - 1], y2 = dp[k - 1][x2] + sum[x2];           long long x3 = dq[e - 2], y3 = dp[k - 1][x3] + sum[x3];           if (mul((y1 - y2), (x2 - x3)) <= mul((y2 - y3), (x1 - x2)))             e--;           else             break;         }         dq[e++] = i;       }     }     printf( %I64d n , dp[p][m]);   }


#include <bits/stdc++.h>   using namespace std;   #pragma GCC optimize( O3 )   #pragma GCC optimize( unroll-loops )   const int maxn = 3e5 + 5, N = 1e6 + 5, SQ = 55, base = 879169, mod = 1e9 + 7,             INF = 1e9 + 5, lg = 20;   const long double eps = 1e-4;   struct node {     int mn = INF, lz = 0;     int c1 = 0, c2 = 0;   };   node seg[4 * maxn];   int n, p[maxn], pos[maxn], q[maxn];   inline void shift(int v) {     seg[2 * v].mn += seg[v].lz;     seg[2 * v + 1].mn += seg[v].lz;     seg[2 * v].lz += seg[v].lz;     seg[2 * v + 1].lz += seg[v].lz;     seg[v].lz = 0;   }   void add(int l, int r, int x, int v = 1, int s = 0, int e = n) {     if (l >= e || s >= r) {       return;     }     if (l <= s && e <= r) {       seg[v].mn += x;       seg[v].lz += x;       return;     }     shift(v);     add(l, r, x, 2 * v, s, (s + e) / 2);     add(l, r, x, 2 * v + 1, (s + e) / 2, e);     seg[v].mn = min(seg[2 * v].mn, seg[2 * v + 1].mn);   }   pair<int, int> get(int l, int r, int v = 1, int s = 0, int e = n) {     if (l >= e || s >= r) {       return {0, 0};     }     if (l <= s && e <= r) {       return {seg[v].c1, seg[v].c2};     }     shift(v);     pair<int, int> a = get(l, r, 2 * v, s, (s + e) / 2);     pair<int, int> b = get(l, r, 2 * v + 1, (s + e) / 2, e);     return {a.first + b.first, a.second + b.second};   }   void modify_num(int p, int x, int v = 1, int s = 0, int e = n) {     seg[v].c2++;     seg[v].mn = min(seg[v].mn, x);     if (e - s == 1) {       return;     }     shift(v);     if (p < (s + e) / 2) {       modify_num(p, x, 2 * v, s, (s + e) / 2);       return;     }     modify_num(p, x, 2 * v + 1, (s + e) / 2, e);   }   void modify_bomb(int p, int v = 1, int s = 0, int e = n) {     seg[v].c1++;     if (e - s == 1) {       return;     }     shift(v);     if (p < (s + e) / 2) {       modify_bomb(p, 2 * v, s, (s + e) / 2);       return;     }     modify_bomb(p, 2 * v + 1, (s + e) / 2, e);   }   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> p[i];     }     for (int i = 0; i < n; ++i) {       cin >> q[i];     }     for (int i = 0; i < n; ++i) {       p[i]--, q[i]--;       pos[p[i]] = i;     }     int ans = n - 1;     for (int i = 0; i < n; ++i) {       while (ans) {         add(0, pos[ans], -1);         pair<int, int> res = get(pos[ans], n);         if (res.first < res.second + 1 || seg[1].mn <= 0) {           add(0, pos[ans], 1);           break;         }         modify_num(pos[ans], res.first - res.second);         ans--;       }       cout << ans + 1 <<    ;       modify_bomb(q[i]);       add(0, q[i] + 1, 1);     }     cout <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3e3 + 1;   struct node {     int w, x, y, z;   };   queue<int> q;   vector<int> g[N], rg[N];   int d[N][N], rd[N][N], vis[N];   vector<pair<int, int>> fg[N], frg[N];   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n, m;     cin >> n >> m;     for (int i = 1; i <= m; i++) {       int u, v;       cin >> u >> v;       g[u].push_back(v);       rg[v].push_back(u);     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         vis[j] = 0;         d[i][j] = n + 1;       }       vis[i] = 1;       q.push(i);       d[i][i] = 0;       while (!q.empty()) {         int v = q.front();         q.pop();         for (auto u : g[v]) {           if (vis[u]) continue;           vis[u] = 1;           q.push(u);           d[i][u] = d[i][v] + 1;         }       }       for (int j = 1; j <= n; j++) {         if (j == i) continue;         if (d[i][j] != n + 1) fg[i].push_back({d[i][j], j});       }       sort(fg[i].begin(), fg[i].end());       for (int j = 1; j <= n; j++) {         vis[j] = 0;         rd[i][j] = n + 1;       }       vis[i] = 1;       q.push(i);       rd[i][i] = 0;       while (!q.empty()) {         int v = q.front();         q.pop();         for (auto u : rg[v]) {           if (vis[u]) continue;           vis[u] = 1;           q.push(u);           rd[i][u] = rd[i][v] + 1;         }       }       for (int j = 1; j <= n; j++) {         if (j == i) continue;         if (rd[i][j] != n + 1) frg[i].push_back({rd[i][j], j});       }       sort(frg[i].begin(), frg[i].end());     }     int ans = 0;     node A;     for (int b = 1; b <= n; b++) {       for (int c = 1; c <= n; c++) {         if (b == c || d[b][c] == n + 1) continue;         int ans1 = 0, z = 0;         node B;         int sz1 = frg[b].size(), sz2 = fg[c].size();         for (int j = sz1 - 1; j >= max(z, sz1 - 3); j--) {           int a = frg[b][j].second;           for (int k = sz2 - 1; k >= max(z, sz2 - 3); k--) {             int dd = fg[c][k].second;             if (a == c || dd == b || a == dd) continue;             int ans2 = d[b][c] + rd[b][a] + d[c][dd];             node C;             C.w = a;             C.x = b;             C.y = c;             C.z = dd;             if (ans2 > ans1) {               ans1 = ans2;               B = C;             }           }         }         if (ans1 > ans) {           ans = ans1;           A = B;         }       }     }     cout << A.w <<     << A.x <<     << A.y <<     << A.z <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = (int)1e6 + 5;   vector<int> adjList[N];   unordered_set<int> remaining;   bitset<N> inQ, nextToQ;   vector<int> ans;   void solve(int u) {     remaining.erase(u);     for (auto v : adjList[u]) {       remaining.erase(v);     }     if (!remaining.empty()) {       solve(*remaining.begin());     }     if (!nextToQ[u]) {       inQ[u] = 1;       nextToQ[u] = 1;       for (auto v : adjList[u]) {         nextToQ[v] = 1;       }       ans.push_back(u);     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, m;     cin >> n >> m;     for (int i = 0; i < m; i++) {       int u, v;       cin >> u >> v;       adjList[u].push_back(v);     }     for (int i = 1; i <= n; i++) {       remaining.insert(i);     }     solve(1);     cout << ans.size() << endl;     for (auto u : ans) {       cout << u <<    ;     }     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   const double EPS = 1e-6;   const string alpha =  abcdefghijklmnopqrstuvwxyz ;   const long long INFINF = 1e18;   const int INF = 1e9;   const int maxN = 1e6;   int T;   void tc() {     int N;     cin >> N;     string second =   ;     int d = 0;     for (int i = 0; i < N; i++) {       if (d < N) {         second +=  8 ;         d += 4;       } else {         second +=  9 ;       }     }     reverse(second.begin(), second.end());     cout << second << endl;   }   int main() {     cin >> T;     while (T--) {       tc();     }   }


#include <bits/stdc++.h>   #pragma GCC optimize( O2,Ofast,inline,unroll-all-loops,-ffast-math )   #pragma GCC target( avx,sse2,sse3,sse4,popcnt )   using namespace std;   int nxt[100010][26], f[255][255][255], pos[26], n, q, a = 0, b = 0, c = 0;   char s[100010], A[255], B[255], C[255];   template <class T>   void read(T &x) {     char ch = x = 0;     bool fl = false;     while (!isdigit(ch)) fl |= ch ==  - , ch = getchar();     while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar();     x = fl ? -x : x;   }   int main() {     read(n), read(q), scanf( %s , s + 1);     for (int i = 0; i < 26; i++) {       pos[i] = n + 1;     }     memcpy(nxt[n + 1], pos, sizeof(pos));     for (int i = n; ~i; i--) {       memcpy(nxt[i], pos, sizeof(pos)), pos[s[i] -  a ] = i;     }     char op[10];     for (int i = 1, x; i <= q; i++) {       scanf( %s , op);       if (op[0] ==  + ) {         read(x), scanf( %s , op);         if (x == 1) {           A[++a] = op[0];           for (int b = 0; b <= ::b; b++) {             for (int c = 0; c <= ::c; c++) {               f[a][b][c] = 1 << 30;               if (a)                 f[a][b][c] = min(f[a][b][c], nxt[f[a - 1][b][c]][A[a] -  a ]);               if (b)                 f[a][b][c] = min(f[a][b][c], nxt[f[a][b - 1][c]][B[b] -  a ]);               if (c)                 f[a][b][c] = min(f[a][b][c], nxt[f[a][b][c - 1]][C[c] -  a ]);             }           }         }         if (x == 2) {           B[++b] = op[0];           for (int a = 0; a <= ::a; a++) {             for (int c = 0; c <= ::c; c++) {               f[a][b][c] = 1 << 30;               if (a)                 f[a][b][c] = min(f[a][b][c], nxt[f[a - 1][b][c]][A[a] -  a ]);               if (b)                 f[a][b][c] = min(f[a][b][c], nxt[f[a][b - 1][c]][B[b] -  a ]);               if (c)                 f[a][b][c] = min(f[a][b][c], nxt[f[a][b][c - 1]][C[c] -  a ]);             }           }         }         if (x == 3) {           C[++c] = op[0];           for (int a = 0; a <= ::a; a++) {             for (int b = 0; b <= ::b; b++) {               f[a][b][c] = 1 << 30;               if (a)                 f[a][b][c] = min(f[a][b][c], nxt[f[a - 1][b][c]][A[a] -  a ]);               if (b)                 f[a][b][c] = min(f[a][b][c], nxt[f[a][b - 1][c]][B[b] -  a ]);               if (c)                 f[a][b][c] = min(f[a][b][c], nxt[f[a][b][c - 1]][C[c] -  a ]);             }           }         }       } else {         read(x);         if (x == 1) a--;         if (x == 2) b--;         if (x == 3) c--;       }       puts(f[a][b][c] <= n ?  YES  :  NO );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, k, A, B;   long long arr[100000];   long long solve(long long l, long long r, long long depth, long long add) {     if (l == r) return A;     if (depth == 0) return B * (r - l);     long long mid =         lower_bound(arr + l, arr + r, (1 << (depth - 1)) + add) - (arr);     return min(B * (r - l) * (1 << depth),                solve(l, mid, depth - 1, add) +                    solve(mid, r, depth - 1, add + (1 << (depth - 1))));   }   int32_t main() {     cin >> n >> k >> A >> B;     for (long long i = 0; i < k; ++i) cin >> arr[i];     sort(arr, arr + k);     cout << solve(0, k, n, 1) <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, q;     cin >> n >> m >> q;     vector<vector<int> > arr(n);     for (int i = 0; i < n; i++) {       arr[i].resize(m);     }     vector<pair<int, int> > did;     for (int i = 0; i < q; i++) {       int t;       cin >> t;       if (t == 1) {         int x, c;         cin >> x;         x--;         c = arr[x][0];         for (int j = 0; j < m - 1; j++) {           arr[x][j] = arr[x][j + 1];         }         arr[x][m - 1] = c;         did.push_back({1, x});       }       if (t == 2) {         int x, c;         cin >> x;         x--;         c = arr[0][x];         for (int j = 0; j < n - 1; j++) {           arr[j][x] = arr[j + 1][x];         }         arr[n - 1][x] = c;         did.push_back({2, x});       }       if (t == 3) {         int x, y, c;         cin >> x >> y >> c;         arr[x - 1][y - 1] = c;       }     }     for (int i = did.size() - 1; i >= 0; i--) {       int x = did[i].second;       if (did[i].first == 1) {         int c = arr[x][m - 1];         for (int j = m - 1; j > 0; j--) {           arr[x][j] = arr[x][j - 1];         }         arr[x][0] = c;       } else {         int c = arr[n - 1][x];         for (int j = n - 1; j > 0; j--) {           arr[j][x] = arr[j - 1][x];         }         arr[0][x] = c;       }     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         cout << arr[i][j] <<    ;       }       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   std::mt19937 rng(       (int)std::chrono::steady_clock::now().time_since_epoch().count());   struct PT {     long long x, y;     PT(long long _x = 0, long long _y = 0) : x(_x), y(_y) {}     PT operator+(const PT &p) const { return PT(x + p.x, y + p.y); }     PT operator-(const PT &p) const { return PT(x - p.x, y - p.y); }     PT operator*(long long c) const { return PT(x * c, y * c); }     long long operator*(const PT &p) const { return x * p.x + y * p.y; }     long long operator%(const PT &p) const { return x * p.y - y * p.x; }     bool operator<(const PT &p) const { return x != p.x ? x < p.x : y < p.y; }     bool operator==(const PT &p) const { return x == p.x && y == p.y; }     friend std::ostream &operator<<(std::ostream &os, const PT &p) {       return os << p.x <<     << p.y;     }     friend std::istream &operator>>(std::istream &is, PT &p) {       return is >> p.x >> p.y;     }   };   bool comp(PT a, PT b) {     int hp1 = (a.x < 0 || (a.x == 0 && a.y < 0));     int hp2 = (b.x < 0 || (b.x == 0 && b.y < 0));     if (hp1 != hp2) return hp1 < hp2;     long long R = a % b;     if (R) return R > 0;     return a * a < b * b;   }   const int ms = 6060;   int choice[3];   long long f[4];   long long go(int on, int lst) {     if (on == 3) {       int got = 0;       for (int i = 0; i < 3; i++)         for (int j = i + 1; j < 3; j++) {           if (choice[i] ^ choice[j]) {             got++;           }         }       if (got % 2 == 0) {         long long ans = 1;         for (int l = 0, r = 0; l < 3; l = r) {           while (r < 3 && choice[r] == choice[l]) r++;           long long v = f[choice[l]];           if (r - l == 1) {             ans *= v;           } else if (r - l == 2) {             ans *= v * (v - 1) / 2;           } else {             ans *= v * (v - 1) * (v - 2) / 6;           }         }         return ans;       }       return 0;     }     long long ans = 0;     choice[on] = lst;     ans = go(on + 1, lst);     if (lst < 3) {       ans += go(on, lst + 1);     }     return ans;   }   int main() {     std::ios_base::sync_with_stdio(false);     std::cin.tie(NULL);     int n;     std::cin >> n;     std::vector<PT> a(n);     for (int i = 0; i < n; i++) {       std::cin >> a[i];       int v1 = (int)a[i].x % 4 / 2, v2 = (int)a[i].y % 4 / 2;       f[v1 * 2 + v2]++;     }     std::cout << go(0, 0) <<   n ;   }


#include <bits/stdc++.h>   const int maxn = 100020, INF = 1 << 29;   int n;   struct rec {     int n, x;   } a[maxn];   int al[maxn], ar[maxn];   struct SBT {     int root, tot;     int d[maxn], l[maxn], r[maxn], s[maxn];     SBT() { s[0] = root = tot = 0; }     void prt(int x, int *a, int &w) {       if (!x) return;       prt(l[x], a, w);       for (int i = 1; i <= w; ++i) a[++a[0]] = d[x];       prt(r[x], a, w);     }     void print(int *a, int w) { prt(root, a, w); }     void tl(int &x) {       int y = r[x];       r[x] = l[y];       l[y] = x;       s[y] = s[x];       s[x] = s[l[x]] + s[r[x]] + 1;       x = y;     }     void tr(int &x) {       int y = l[x];       l[x] = r[y];       r[y] = x;       s[y] = s[x];       s[x] = s[l[x]] + s[r[x]] + 1;       x = y;     }     void mt(int &x, bool w) {       if (!w) {         if (s[l[l[x]]] > s[r[x]])           tr(x);         else if (s[r[l[x]]] > s[r[x]]) {           tl(l[x]);           tr(x);         } else           return;       } else {         if (s[r[r[x]]] > s[l[x]])           tl(x);         else if (s[l[r[x]]] > s[l[x]]) {           tr(r[x]);           tl(x);         } else           return;       }       mt(l[x], false);       mt(r[x], true);       mt(x, false);       mt(x, true);     }     void ist(int &x, int w) {       if (!x) {         x = ++tot;         s[x] = 1;         d[x] = w;         l[x] = r[x] = 0;       } else {         ++s[x];         if (w < d[x])           ist(l[x], w);         else           ist(r[x], w);         mt(x, w >= d[x]);       }     }     int dl(int &x, int w) {       --s[x];       if ((d[x] == w) || ((w < d[x]) && (!l[x])) || ((w > d[x]) && (!r[x]))) {         int tmp = d[x];         if ((!l[x]) || (!r[x]))           x = l[x] + r[x];         else           d[x] = dl(l[x], d[x] + 1);         return tmp;       } else if (w < d[x])         return dl(l[x], w);       else         return dl(r[x], w);     }     int getpre(int w) {       int t = root, last = -INF;       while (t) {         if (d[t] < w && d[t] > last) last = d[t];         t = (w <= d[t]) ? l[t] : r[t];       }       return last;     }     int getsuc(int w) {       int t = root, last = INF;       while (t) {         if (d[t] > w && d[t] < last) last = d[t];         t = (w < d[t]) ? l[t] : r[t];       }       return last;     }     int select(int k) {       int t = root;       while (t) {         if (s[l[t]] + 1 == k) return d[t];         if (k > s[l[t]] + 1) {           k = k - s[l[t]] - 1;           t = r[t];         } else           t = l[t];       }     }     void delet(int w) { dl(root, w); }     void insert(int w) { ist(root, w); }     int size() { return s[root]; }   };   SBT left, right;   inline bool cmp(const rec &a, const rec &b) { return a.x > b.x; }   int main() {     int i, l, r, tl, tr, ts, ans, w;     scanf( %d , &n);     for (i = 1; i <= n; ++i) {       a[i].n = i;       scanf( %d , &a[i].x);     }     std::sort(a + 1, a + n + 1, cmp);     l = 1;     ans = al[0] = ar[0] = 0;     while (l <= n) {       for (r = l; r < n && a[r + 1].x == a[l].x;) ++r;       for (i = l; i <= r; ++i) {         w = a[i].n;         tr = right.getpre(w);         tl = left.getsuc(w);         if (tr + 1 == w) {           if (tl - 1 == w) {             right.delet(tr);             left.delet(tl);           } else {             right.delet(tr);             right.insert(w);           }         } else {           if (tl - 1 == w) {             left.delet(tl);             left.insert(w);           } else {             right.insert(w);             left.insert(w);           }         }       }       ts = (r == n) ? a[r].x : (a[r].x - a[r + 1].x);       left.print(al, ts);       right.print(ar, ts);       l = r + 1;     }     ans = al[0];     printf( %d n , ans);     for (i = 1; i <= ans; ++i) printf( %d %d n , al[i], ar[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void setIO(const string &name =   ) {     ios::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     if (name.length()) {       freopen((name +  .in ).c_str(),  r , stdin);       freopen((name +  .out ).c_str(),  w , stdout);     }   }   template <typename T>   void read(vector<T> &a) {     for (auto &x : a) cin >> x;   }   template <typename T>   void read(vector<T> &a, int n) {     a.resize(n);     for (auto &x : a) cin >> x;   }   template <class T, class U>   ostream &operator<<(ostream &out, const pair<T, U> &v) {     out <<  ( ;     out << v.first <<  ,  << v.second;     return out <<  ) ;   }   template <class T>   ostream &operator<<(ostream &out, const vector<T> &v) {     out <<  [ ;     for (auto(i) = 0; (i) < ((int)(v).size()); (i)++) {       if (i) out <<  ,  ;       out << v[i];     }     return out <<  ] ;   }   template <typename T>   void print(vector<T> &a) {     for (const auto &x : a) cout << x <<    ;     cout <<   n ;   }   void MOD(int &x, int m = 1000000007) {     x %= m;     if (x < 0) x += m;   }   template <typename T>   void dbg(const char *name, T &&arg1) {     cout << name <<   :   << arg1 <<   n ;   }   template <typename T, typename... U>   void dbg(const char *names, T &&arg1, U &&...args) {     const char *comma = strchr(names + 1,  , );     cout.write(names, comma - names) <<   :   << arg1 <<   |  ;     dbg(comma + 1, args...);   }   template <class T>   void read(T &x) {     cin >> x;   }   template <class T, class... U>   void read(T &t, U &...u) {     read(t);     read(u...);   }   template <class T>   void tell(T &x) {     cout << x;   }   template <class T, class... U>   void tell(T &t, U &...u) {     tell(t);     if (sizeof...(u)) tell(   );     tell(u...);   }   int gcd(int a, int b) { return !a ? b : gcd(b % a, a); }   int n, m;   vector<vector<int> > board;   int dp[101][int(5e4) + 10 + int(5e4) + 10];   int32_t main() {     setIO();     read(m, n);     board.resize(n);     for (auto &(c) : (board)) {       read(c, m);     }     int ans = int(2e9) + 15;     vector<int> ans_ar;     for (int diff = -int(5e4) + 10 + 1; diff < int(5e4) + 10; diff++) {       if (diff >= 0) {         dp[n][diff + int(5e4) + 10] = 0;       } else {         dp[n][diff + int(5e4) + 10] = int(2e9) + 15;       }     }     for (auto(i) = 0; (i) < (m - 1); (i)++) {       for (int idx = n - 1; idx >= 0; idx--) {         for (int diff = -int(5e4) + 10 + 1; diff < int(5e4) + 10; diff++) {           int &res = dp[idx][diff + int(5e4) + 10];           res = int(2e9) + 15;           res = min(res, dp[idx + 1][diff + int(5e4) + 10] + 1);           if (diff + (board[idx][i] - board[idx][m - 1]) >= int(5e4) + 10) {             res = 0;           } else if (diff + (board[idx][i] - board[idx][m - 1]) <=                      -int(5e4) + 10) {             res = int(2e9) + 15;           } else {             res =                 min(res, dp[idx + 1][diff + (board[idx][i] - board[idx][m - 1]) +                                      int(5e4) + 10]);           }         }       }       if (ans > dp[0][int(5e4) + 10]) {         ans = dp[0][int(5e4) + 10];         int cur_diff = 0;         ans_ar.clear();         for (int idx = 0; idx < n; idx++) {           const int &res = dp[idx][cur_diff + int(5e4) + 10];           if (res == dp[idx + 1][cur_diff + int(5e4) + 10] + 1) {             ans_ar.emplace_back(idx + 1);             continue;           } else {             if (cur_diff + (board[idx][i] - board[idx][m - 1]) >= int(5e4) + 10) {               break;             } else if (cur_diff + (board[idx][i] - board[idx][m - 1]) <=                        -int(5e4) + 10) {               break;             } else {               cur_diff += (board[idx][i] - board[idx][m - 1]);             }           }         }       }     }     cout << ans <<   n ;     print(ans_ar);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int printx(int x, int a, int b, int c) {     if (x == 0)       return a;     else if (x == 1)       return b;     else       return c;   }   int main() {     long long n;     int x;     cin >> n >> x;     n %= 6;     switch (n) {       case 0:         x = printx(x, 0, 1, 2);         break;       case 1:         x = printx(x, 1, 0, 2);         break;       case 2:         x = printx(x, 1, 2, 0);         break;       case 3:         x = printx(x, 2, 1, 0);         break;       case 4:         x = printx(x, 2, 0, 1);         break;       case 5:         x = printx(x, 0, 2, 1);         break;     }     cout << x << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string a[100];   int id[30];   vector<int> cn[30];   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 1; i < n; i++) {       char l1 =  - ;       char l2 =  - ;       int j = -1;       int s = a[i - 1].size();       if (a[i - 1].size() > a[i].size()) {         s = a[i].size();         if (a[i] == string(a[i - 1].substr(0, s))) id[0]++;       }       while (++j < s) {         if (a[i - 1][j] == a[i][j]) continue;         l1 = a[i - 1][j];         l2 = a[i][j];         break;       }       if (l1 !=  - ) {         cn[l1 -  a ].push_back(l2 -  a );         id[l2 -  a ]++;       }     }     queue<int> ts;     for (int i = 0; i < 26; i++)       if (!id[i]) ts.push(i);     string anw =   ;     while (!ts.empty()) {       int u = ts.front();       ts.pop();       anw += char(u +  a );       for (auto v : cn[u]) {         id[v]--;         if (!id[v]) ts.push(v);       }     }     cout << (anw.size() == 26 ? anw :  Impossible ) << endl;   }


#include <bits/stdc++.h>   using namespace std;   int dp[26][26];   int main(void) {     int n, i, j, a, b, l, ans = 0;     char s[20];     memset(dp, 0, sizeof dp);     scanf( %d , &n);     for (i = 0; i < n; i++) {       scanf( %s , s);       l = strlen(s);       a = s[0] -  a ;       b = s[l - 1] -  a ;       for (j = 0; j < 26; j++) {         if (dp[j][a]) {           dp[j][b] = max(dp[j][b], dp[j][a] + l);         }       }       dp[a][b] = max(dp[a][b], l);     }     for (i = 0; i < 26; i++) {       ans = max(ans, dp[i][i]);     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   inline void solve() {     int n;     cin >> n;     vector<int> a(n);     for (int& e : a) {       cin >> e;     }     long long b = a[0];     for (int i = 0; i < n - 1; ++i) {       int d = a[i + 1] - a[i];       if (d < 0) {         b += d;         if (b < 0) {           cout <<  NO n ;           return;         }       }     }     cout <<  YES n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     size_t t = 1;     cin >> t;     for (int i = 0; i < t; ++i) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 3e3 + 10;   int N;   vector<int> g[maxn];   int sub[maxn][maxn], fa[maxn][maxn];   long long dp[maxn][maxn];   void DFS1(int anc, int v, int p) {     sub[anc][v] = 1;     for (int u : g[v]) {       if (u != p) {         fa[anc][u] = v;         DFS1(anc, u, v);         sub[anc][v] += sub[anc][u];       }     }   }   long long DFS2(int v, int u) {     if (v == u) return 0;     if (dp[v][u] != -1) return dp[v][u];     dp[v][u] = (long long)sub[v][u] * sub[u][v] +                max(DFS2(v, fa[v][u]), DFS2(fa[u][v], u));     return dp[v][u];   }   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     cin >> N;     for (int i = 1; i <= N - 1; ++i) {       int u, v;       cin >> u >> v;       g[u].push_back(v);       g[v].push_back(u);     }     for (int i = 1; i <= N; ++i) {       fa[i][i] = i;       DFS1(i, i, i);     }     long long ret = 0;     for (int i = 1; i <= N; ++i) {       for (int j = 1; j <= N; ++j) {         dp[i][j] = -1;       }     }     for (int i = 1; i <= N; ++i) {       for (int j = 1; j <= N; ++j) {         ret = max(ret, DFS2(i, j));       }     }     cout << ret << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long n, k;     cin >> n >> k;     string fir;     cin >> fir;     vector<long long> v;     for (long long i = 0; i < fir.size(); ++i) {       if (fir[i] ==  0 ) {         v.push_back(i);       }     }     long long ans = pow(10, 7);     long long m = 0;     for (long long l = 0; l < v.size() - k; ++l) {       long long r = l + k;       ;       while (v[m] - v[l] < v[r] - v[m]) {         ++m;       }       ans = min(ans, max(v[m] - v[l], v[r] - v[m]));       ans = min(ans, max(v[m - 1] - v[l], v[r] - v[m - 1]));     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     int n, l, r, m, ans = 1e9;     cin >> n >> m;     for (int i = 0; i < m; i++) {       cin >> l >> r;       ans = min(ans, r - l + 1);     }     cout << ans <<   n ;     for (int i = 0; i < n; i++) cout << i % ans <<    ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     string buf;     while (getline(cin, buf)) s += buf;     vector<int> a;     string hoge;     vector<int> cnt;     for (int i = 0; i < s.length(); ++i) {       if (s[i] ==  < ) {         hoge =   ;       } else if (s[i] ==  > ) {         if (hoge ==  table ) {           cnt.push_back(0);         }         if (hoge ==  /table ) {           a.push_back(cnt.back());           cnt.pop_back();         }         if (hoge ==  td ) ++cnt.back();       } else {         hoge += s[i];       }     }     sort(a.begin(), a.end());     for (int i = 0; i < a.size(); ++i) {       if (i) cout <<    ;       cout << a[i];     }     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   int mod = 1e9 + 7;   long long powmod(long long a, long long b) {     long long res = 1;     if (a >= mod) a %= mod;     for (; b; b >>= 1) {       if (b & 1) res = res * a;       if (res >= mod) res %= mod;       a = a * a;       if (a >= mod) a %= mod;     }     return res;   }   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(0);     long long a, b, c, d, num0, num1;     cin >> a >> b >> c >> d;     int flag = 0;     long long i, j;     for (i = 1; i <= 1000000; i++) {       long long temp = (i * (i - 1)) / 2;       if (temp > a) {         flag = 1;         break;       }       if (temp == a) {         num0 = i;         break;       }     }     for (i = 1; i <= 1000000; i++) {       long long temp = (i * (i - 1)) / 2;       if (temp > d) {         flag = 1;         break;       }       if (temp == d) {         num1 = i;         break;       }     }     if (flag == 1) {       cout <<  Impossible ;       return 0;     } else if (a + b + c + d == 0) {       cout <<  0 ;     } else if (a == 0 && b == 0 && c == 0) {       for (i = 0; i < num1; i++) cout <<  1 ;     } else if (b == 0 && c == 0 && d == 0) {       for (i = 0; i < num0; i++) cout <<  0 ;     } else {       long long num0110 = num0 * num1;       if (b + c != num0110) {         cout <<  Impossible ;         return 0;       }       long long end = c / num0;       long long rem = c % num0;       long long last = num1 - end - 1;       if (rem == 0) last++;       for (i = 0; i < end; i++) cout <<  1 ;       for (i = 0; i < num0 - rem; i++) cout <<  0 ;       if (rem != 0) cout <<  1 ;       for (i = num0 - rem; i < num0; i++) cout <<  0 ;       for (i = 0; i < last; i++) cout <<  1 ;     }   }


#include <bits/stdc++.h>   using namespace std;   long long INF = 1e6;   long long M = 1e9 + 7;   void solve() {     long long n;     cin >> n;     vector<int> a(n), b(n - 1), c(n - 2);     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < n - 1; i++) cin >> b[i];     for (int i = 0; i < n - 2; i++) cin >> c[i];     sort((a).begin(), (a).end());     sort((b).begin(), (b).end());     sort((c).begin(), (c).end());     long long x, y;     bool f1 = true, f2 = true;     for (int i = 0; i < n - 1; i++) {       if (i < n - 2 && f2) {         if (b[i] != c[i]) {           y = b[i];           f2 = false;         }       }       if (a[i] != b[i] && f1) {         x = a[i];         f1 = false;       }     }     if (f1) x = a[n - 1];     if (f2) y = b[n - 2];     cout << x <<   n  << y <<   n ;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long t = 1;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 7;   int lz[N << 2][10];   long long sum[N << 2][10], tmp[10];   void push_up(int rt) {     for (int i = 0; i < 10; ++i) sum[rt][i] = 0;     for (int i = 0; i < 10; ++i) sum[rt][lz[(rt << 1)][i]] += sum[(rt << 1)][i];     for (int i = 0; i < 10; ++i)       sum[rt][lz[(rt << 1 | 1)][i]] += sum[(rt << 1 | 1)][i];   }   void build(int rt, int l, int r) {     for (int i = 0; i < 10; ++i) lz[rt][i] = i, sum[rt][i] = 0;     if (l == r) {       int t, tmp = 1;       scanf( %d , &t);       while (t) sum[rt][t % 10] += tmp, tmp *= 10, t /= 10;       return;     }     int m = (l + r) >> 1;     build((rt << 1), l, m);     build((rt << 1 | 1), m + 1, r);     push_up(rt);   }   void push_down(int rt) {     for (int i = 0; i < 10; ++i) lz[(rt << 1)][i] = lz[rt][lz[(rt << 1)][i]];     for (int i = 0; i < 10; ++i)       lz[(rt << 1 | 1)][i] = lz[rt][lz[(rt << 1 | 1)][i]];     memset(tmp, 0, sizeof(tmp));     for (int i = 0; i < 10; ++i) tmp[lz[rt][i]] += sum[rt][i];     for (int i = 0; i < 10; ++i) sum[rt][i] = tmp[i];     for (int i = 0; i < 10; ++i) lz[rt][i] = i;   }   void update(int rt, int l, int r, int ql, int qr, int x, int y) {     if (ql <= l && qr >= r) {       for (int i = 0; i < 10; ++i)         if (lz[rt][i] == x) lz[rt][i] = y;       return;     }     push_down(rt);     int m = (l + r) >> 1;     if (ql <= m) update((rt << 1), l, m, ql, qr, x, y);     if (qr > m) update((rt << 1 | 1), m + 1, r, ql, qr, x, y);     push_up(rt);   }   long long query(int rt, int l, int r, int ql, int qr) {     long long res = 0;     if (ql <= l && qr >= r) {       for (int i = 0; i < 10; ++i) res += sum[rt][i] * lz[rt][i];       return res;     }     push_down(rt);     int m = (l + r) >> 1;     if (ql <= m) res += query((rt << 1), l, m, ql, qr);     if (qr > m) res += query((rt << 1 | 1), m + 1, r, ql, qr);     return res;   }   int main() {     int n, q;     scanf( %d%d , &n, &q);     build(1, 1, n);     while (q--) {       int op, l, r, x, y;       scanf( %d%d%d , &op, &l, &r);       if (op == 1) {         scanf( %d%d , &x, &y);         update(1, 1, n, l, r, x, y);       } else         printf( %I64d n , query(1, 1, n, l, r));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e6 + 5;   int cnt[MAXN * 2];   struct state {     int len, link, first_pos;     bool isClone;     map<char, int> next;   };   state st[MAXN * 2];   int sz, last;   void sa_init() {     for (int i = 0; i < sz; i++) st[i].next.clear();     st[0].len = 0;     st[0].link = -1;     sz = last = 0;     ++sz;   }   void sa_extend(char c) {     int cur = sz++;     st[cur].len = st[last].len + 1;     st[cur].first_pos = st[cur].len - 1;     st[cur].isClone = false;     int p = last;     while (p != -1 && !st[p].next.count(c)) {       st[p].next[c] = cur;       p = st[p].link;     }     if (p == -1) {       st[cur].link = 0;     } else {       int q = st[p].next[c];       if (st[p].len + 1 == st[q].len) {         st[cur].link = q;       } else {         int clone = sz++;         st[clone].len = st[p].len + 1;         st[clone].next = st[q].next;         st[clone].link = st[q].link;         st[clone].first_pos = st[q].first_pos;         st[clone].isClone = true;         while (p != -1 && st[p].next[c] == q) {           st[p].next[c] = clone;           p = st[p].link;         }         st[q].link = st[cur].link = clone;       }     }     last = cur;   }   bool cmp(int i, int j) { return st[i].len > st[j].len; }   int b[MAXN];   int pow_cad(string &P) {     int i = 0, j = -1;     b[0] = -1;     while (i < (P).size()) {       while (j >= 0 && P[i] != P[j]) j = b[j];       i++, j++, b[i] = j;     }     int n = (P).size();     int k = n - b[n];     if (n % k == 0) return k;     return n;   }   void match(string &t, int k) {     int v = 0, l = 0, ans = 0;     for (int i = 0; i < (t).size(); i++) {       while (v && !st[v].next.count(t[i])) {         v = st[v].link;         l = st[v].len;       }       if (st[v].next.count(t[i])) {         v = st[v].next[t[i]];         l++;         if (l >= k) {           if (st[st[v].link].len >= k) {             v = st[v].link;             l = st[v].len;           }           ans += cnt[v];         }       }     }     cout << ans <<   n ;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     string s;     cin >> s;     sa_init();     for (auto c : s) {       sa_extend(c);     }     for (int i = 1; i < sz; i++) {       if (!st[i].isClone) cnt[i] = 1;     }     vector<int> node(sz);     iota((node).begin(), (node).end(), 0);     sort((node).begin(), (node).end(), cmp);     for (int u : node) {       if (~st[u].link) cnt[st[u].link] += cnt[u];     }     int n;     string pat;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> pat;       int k = (pat).size();       int rep = pow_cad(pat);       pat += pat.substr(0, rep - 1);       cerr <<  pat             <<  _=_  << pat <<   n ;       match(pat, k);     }     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:256000000 )   using namespace std;   const double infd = 2e+9;   const int infi = 1ll << 30;   const long long infl = 1ll << 60;   const int mod = 1e+9 + 7;   template <class T>   inline T sqr(T x) {     return x * x;   }   int main() {     string a, b;     int k;     cin >> a >> b >> k;     int n = a.length();     vector<int> p(k + 1);     p[0] = 1;     for (int i = 1; i <= k - 1; i++) p[i] = p[i - 1] * 1ll * (n - 1) % mod;     vector<int> d[2];     d[0].resize(k + 1);     d[1].resize(k + 1);     d[0][0] = 1;     d[1][0] = 0;     for (int i = 1; i <= k; i++) {       for (int m = 0; m < 2; m++) d[m][i] = (p[i - 1] - d[m][i - 1] + mod) % mod;     }     int result = 0;     for (int i = 0; i < n; i++) {       bool compactible = true;       for (int j = 0; j < n; j++) {         if (b[(i + j) % n] != a[j]) {           compactible = false;           break;         }       }       if (compactible) {         if (i == 0)           result += d[0][k];         else           result += d[1][k];         result %= mod;       }     }     cout << result;     return 0;   }


#include <bits/stdc++.h>   #define ll long long   #define ull unsigned long long   #define vll vector<ll>   using namespace std;   inline void sol() {     ll n, m;     cin >> n >> m;     vll a(n);     for(ll i=0;i<n;i++){       cin >> a[i];     }     vector<pair<int,double>> b(m);     for(int i=0;i<m;i++){       int r; double p;       cin >> r >> p;       b[i] = {r, p};     }     int rm;     for(rm = n-1; rm >= 0 and a[rm] == rm + 1; rm--);     if(rm == -1){       cout <<  1 n ;       return;     }     double ans = 1;     for(int i=0;i<m;i++){       if(b[i].first - 1 >= rm)         ans *= (1 - b[i].second);     }     // cout << 1 - ans << endl;     printf( %0.6lf n , 1 - ans);   }   int main() {     // cin.tie(0);     // ios_base::sync_with_stdio(false);        int t;     cin >> t;     while(t--)       sol();        return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, ans, u, v, start;   string opz;   int vis[2][200005];   vector<int> grafo[2][200005];   void dfs(int nodo, int mode) {     vis[mode][nodo] = 1;     for (auto i : grafo[mode][nodo]) {       if (vis[mode][i] == 1) {         cout <<  -1 n ;         exit(0);       }       if (vis[mode][i] == 0) {         dfs(i, mode);       }     }     vis[mode][nodo] = 2;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n >> m;     opz.resize(n);     for (int i = 0; i < m; i++) {       cin >> u >> v;       grafo[0][u].push_back(v);       grafo[1][v].push_back(u);     }     for (int i = 1; i <= n; i++) {       if (vis[0][i] == 0 && vis[1][i] == 0) {         opz[i - 1] =  A ;         ans++;       } else {         opz[i - 1] =  E ;       }       if (vis[0][i] == false) dfs(i, 0);       if (vis[1][i] == false) dfs(i, 1);     }     cout << ans <<   n  << opz <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void handle_io() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   void solve() {     int P;     cin >> P;     if (P == 5)       cout << 2 <<     << 4 << endl;     else       cout << 2 <<     << P / 2 << endl;   }   int main() {     handle_io();     int t;     cin >> t;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 1e6 + 7, Inf = 1e9 + 7;   int n, m, k = Inf, x, l, r, f, a[inf], b[inf], c[inf], d[inf];   map<int, int> p[inf];   vector<int> v;   string s;   int main() {     cin >> n >> s;     for (int i = 0; i < n; ++i) cin >> a[i];     for (int i = 1; i < n; ++i)       if (s[i] ==  L  && s[i - 1] ==  R ) k = min(k, (a[i] - a[i - 1]) / 2);     if (k == Inf)       cout << -1;     else       cout << k;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long> all;   void backtrack(long long x, int cnt4, int cnt7) {     if (x > 1e10) return;     if (cnt4 == cnt7) all.push_back(x);     long long nxt = x * 10 + 4;     backtrack(nxt, cnt4 + 1, cnt7);     nxt = x * 10 + 7;     backtrack(nxt, cnt4, cnt7 + 1);   }   int main() {     std::ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     int n;     cin >> n;     backtrack(0, 0, 0);     sort(all.begin(), all.end());     cout << *lower_bound(all.begin(), all.end(), n);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 2e5 + 7;   const long long mod = 1e9 + 7;   int n;   int a;   int b;   string s;   int main() {     cin >> n >> a >> b;     cin >> s;     if (s[b - 1] == s[a - 1]) {       cout << 0;       return 0;     }     int x = 1e7, y = 1e7;     for (int i = b - 1; i < n; i++) {       if (s[i] == s[b - 1]) {         x = i;         break;       }     }     for (int i = b - 1; i >= 0; i--) {       if (s[i] == s[b - 1]) {         y = i;         break;       }     }     if (b - x < 0) {       cout << b - x;     }     if (b - y < 0) {       cout << b - y;     }     cout << min(b - x, b - y);   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     int X = 0, w = 1;     char ch = 0;     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) w = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 )       X = (X << 3) + (X << 1) + ch -  0 , ch = getchar();     return X * w;   }   int n, tot, a[300007], val[300007 * 30], trie[300007 * 30][2],       cnt[300007 * 30][2];   void add(int x) {     int cur = 0;     for (int i = 29; i >= 0; i--) {       int y = (x >> i) & 1;       if (!trie[cur][y]) {         trie[cur][y] = ++tot;       }       cnt[cur][y]++;       cur = trie[cur][y];     }     val[cur] = x;   }   int ask(int x) {     int cur = 0;     for (int i = 29; i >= 0; i--) {       int y = (x >> i) & 1;       if (cnt[cur][y]) {         cnt[cur][y]--;         cur = trie[cur][y];       } else {         cnt[cur][y ^ 1]--;         cur = trie[cur][y ^ 1];       }     }     return val[cur];   }   int main() {     n = read();     for (int i = 1; i <= n; i++) {       a[i] = read();     }     for (int i = 1; i <= n; i++) {       int x = read();       add(x);     }     for (int i = 1; i <= n; i++) {       printf( %d  , a[i] ^ ask(a[i]));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = (int)2e3 + 113;   long long n, k, cnt = 0;   int main() {     cin >> k >> n;     while (n > k) {       if (n % k != 0) {         cout <<  NO ;         return 0;       }       n /= k;       cnt++;     }     if (n == k) {       cout <<  YES  << endl << cnt;       return 0;     }     cout <<  NO ;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100010;   const int inf = 0x3f3f3f3f;   const int modu = 1e9 + 7;   int t, n, m, k, p, q, cnta, cntb;   double sum, res, ans;   struct pen {     int money;     int num;     int pp;   } a[maxn];   vector<int> ve[110];   bool cmp(const pen a, const pen b) {     return a.pp == b.pp ? a.money > b.money : a.pp < b.pp;   }   int main() {     cnta = inf;     sum = 0;     res = 0;     sum = 0;     memset(a, 0, sizeof(a));     cin >> n >> k;     for (int i = 0; i < n; i++) {       cin >> p >> q;       cnta = min(cnta, p);       a[i].money = p;       a[i].pp = q;       a[i].num = i + 1;     }     sort(a, a + n, cmp);     int j;     for (j = 0; j < k - 1 && a[j].pp == 1; j++) {       sum += a[j].money * 0.5;     }     if (a[k - 1].pp == 1) sum -= cnta * 0.5;     for (j; j < n; j++) {       sum += a[j].money;     }     printf( %.1f n , sum);     for (int i = 0; i < k - 1; ++i) {       printf( 1 %d n , a[i].num);     }     cout << n - k + 1;     for (int i = k - 1; i < n; ++i) {       printf(  %d , a[i].num);     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     int T;     cin >> T;     for (int t = 1; t <= T; ++t) {       int64_t n;       cin >> n;       if (n == 1) {         cout << 0 <<   n ;         continue;       }       int count = 0;       int first = 1;       int64_t res = INT_MAX;       for (int i = 1; i <= 63500; ++i) {         first += 1;         count += 1;         int64_t sum = first;         res = min(res, (((n - sum) + (first - 1)) / first) + count);       }       cout << res <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ld = long double;   int main() {     ios::sync_with_stdio(0), cin.tie(0);     int tt;     cin >> tt;     while (tt--) {       int n, k;       cin >> n >> k;       int ans = 0;       int count = 0;       char x;       int lwb = 0;       bool f = false;       for (int i = 0; i < n; i++) {         cin >> x;         if (x ==  1 ) {           if (!f) {             lwb = count;           } else {             ans += count / (k + 1);             if (count / (k + 1) > 0 && count % (k + 1) != k) {               ans--;             }           }           count = 0;           f = true;         } else {           count++;         }       }       ans += lwb / (k + 1);       ans += count / (k + 1);       if (count == n && n % (k + 1) > 0) {         ans++;       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> vec[200010];   int a[200010], b[200010], color[200010];   bool flag = false;   void dfs(int now, int pre, int col) {     color[now] = col;     for (int i = 0; i < vec[now].size(); i++) {       int v = vec[now][i];       if (v == pre) continue;       if (color[v] == -1)         dfs(v, now, 1 - col);       else if (color[v] == color[now])         flag = true;     }   }   int main() {     int n, m;     cin >> n >> m;     memset(color, -1, sizeof(color));     for (int i = 0; i < m; i++) {       int u, v;       cin >> u >> v;       a[i] = u;       b[i] = v;       vec[u].push_back(v);       vec[v].push_back(u);     }     dfs(1, -1, 0);     if (flag) {       cout <<  NO  << endl;       return 0;     }     cout <<  YES  << endl;     for (int i = 0; i < m; i++) {       if (color[a[i]])         cout <<  0 ;       else         cout <<  1 ;     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n, m;     cin >> n >> m;     vector<vector<int>> a(n, vector<int>(m)), n_s(1001, vector<int>(1001)),         n_c(1001, vector<int>(1001));     vector<int> m_s(1001), m_c(1001);     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) cin >> a[i][j];     for (int i = 0; i < n; i++) {       vector<pair<int, int>> num;       for (int j = 0; j < m; j++) num.push_back(make_pair(a[i][j], j));       sort(num.begin(), num.end());       int h = 0;       for (int k = 0; k < num.size(); k++) {         if (k && num[k].first != num[k - 1].first) h++;         n_s[i][num[k].second] = h;       }       m_s[i] = h;     }     for (int i = 0; i < m; i++) {       vector<pair<int, int>> num;       for (int j = 0; j < n; j++) num.push_back(make_pair(a[j][i], j));       sort(num.begin(), num.end());       int h = 0;       for (int k = 0; k < num.size(); k++) {         if (k && num[k].first != num[k - 1].first) h++;         n_c[i][num[k].second] = h;       }       m_c[i] = h;     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         int l_s = n_s[i][j];         int l_c = n_c[j][i];         int g_s = m_s[i] - l_s;         int g_c = m_c[j] - l_c;         cout << max(l_s, l_c) + 1 + max(g_s, g_c) <<    ;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mxn = 2e5;   const long long INF = 2e18;   const int32_t M = 1000000007;   const long double pie = acos(-1);   template <typename Arg1>   void __f(const char *name, Arg1 &&arg1) {     cerr << name <<   :   << arg1 << std::endl;   }   template <typename Arg1, typename... Args>   void __f(const char *names, Arg1 &&arg1, Args &&...args) {     const char *comma = strchr(names + 1,  , );     cerr.write(names, comma - names) <<   :   << arg1 <<   |  ;     __f(comma + 1, args...);   }   void solve() {     long long n, m;     cin >> n >> m;     vector<long long> gr[n + 1];     vector<long long> ingr[n + 1];     for (long long i = 1; i <= m; i++) {       long long u, v;       cin >> u >> v;       gr[u].push_back(v);       ingr[v].push_back(u);     }     long long k;     cin >> k;     vector<long long> p(k + 1);     for (long long i = 1; i <= k; i++) {       cin >> p[i];     }     long long s = p[1], t = p[k];     vector<long long> d(n + 1, INF);     d[t] = 0;     queue<long long> q;     vector<bool> vis(n + 1, false);     q.push(t);     vis[t] = true;     while (!q.empty()) {       long long v = q.front();       q.pop();       for (long long u : ingr[v]) {         if (!vis[u]) {           q.push(u);           vis[u] = true;         }         d[u] = min(d[u], d[v] + 1);       }     }     long long mn = 0, mx = 0;     for (long long i = 1; i <= k - 1; i++) {       long long curmn = INF;       for (long long v : gr[p[i]]) {         curmn = min(curmn, d[v]);       }       long long cntmn = 0;       for (long long v : gr[p[i]]) {         if (d[v] == curmn) cntmn++;         if (cntmn == 2) break;       }       if (d[p[i + 1]] == curmn) {         if (cntmn == 2) mx++;       } else {         mx++;         mn++;       }     }     cout << mn <<     << mx;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long _t = 1;     for (long long i = 1; i <= _t; i++) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int max2(int a, int b) { return ((a > b) ? a : b); }   inline int min2(int a, int b) { return a < b ? a : b; }   inline int max3(int a, int b, int c) { return max2(a, max2(b, c)); }   long long int gcd(long long int a, long long int b) {     while (b) {       long long int temp = a;       a = b;       b = temp % b;     }     return a;   }   int main() {     int n, w, k;     scanf( %d , &k);     scanf( %d , &n);     scanf( %d , &w);     int total = w * (w + 1);     total = total / 2;     total = total * k;     int diff = total - n;     if (diff > 0)       printf( %d n , diff);     else       printf( %d n , 0);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   static const double EPS = 1e-10;   template <typename T, typename S>   vector<T>& operator<<(vector<T>& a, S b) {     a.push_back(b);     return a;   }   template <typename T>   void operator>>(vector<T>& a, int b) {     while (b--)       if (!a.empty()) a.pop_back();   }   bool isprime(int n) {     if (n < 2) return false;     for (int i = 2; i * i <= n; i++)       if (n % i == 0) return false;     return true;   }   long long b_pow(long long x, long long n) {     return n ? b_pow(x * x, n / 2) * (n % 2 ? x : 1) : 1ll;   }   string itos(int n) {     stringstream ss;     ss << n;     return ss.str();   }   int main() {     int n, m;     cin >> n >> m;     vector<int> atk, def, ciel;     for (int i = 0; i < n; i++) {       string x;       int y;       cin >> x >> y;       if (x ==  DEF )         def.push_back(y);       else         atk.push_back(y);     }     for (int i = 0; i < m; i++) {       int x;       cin >> x;       ciel.push_back(x);     }     sort(atk.begin(), atk.end());     sort(def.begin(), def.end());     sort(ciel.rbegin(), ciel.rend());     int answer1 = 0;     for (int i = 0; i < min(atk.size(), ciel.size()); i++) {       if (atk[i] < ciel[i]) {         answer1 += ciel[i] - atk[i];       }     }     int bad = 0;     int answer2 = 0;     sort(ciel.begin(), ciel.end());     int u[100] = {};     for (int j = 0; j < def.size(); j++) {       int ok = 0;       for (int i = 0; i < ciel.size(); i++) {         if (!u[i] && def[j] < ciel[i]) {           ok = 1;           u[i] = true;           break;         }       }       if (!ok) bad = true;     }     for (int j = 0; j < atk.size(); j++) {       int ok = 0;       for (int i = 0; i < ciel.size(); i++) {         if (!u[i] && atk[j] <= ciel[i]) {           answer2 += ciel[i] - atk[j];           ok = 1;           u[i] = true;           break;         }       }       if (!ok) bad = true;     }     for (int i = 0; i < m; i++) {       if (!u[i]) {         answer2 += ciel[i];       }     }     if (!bad) answer1 = max(answer1, answer2);     cout << answer1 << endl;   }


#include <bits/stdc++.h>   using namespace std;   int n, q, step[1000000];   long long a[2 * 1000000], pref[2 * 1000000 + 1];   int calcLen(int strt) {     int curr = strt, res = 0;     while (curr < strt + n) curr += step[curr % n], res++;     return res;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n >> q;     for (int i = 0; i < (n); i++) cin >> a[i];     for (int i = 0; i < (n); i++) a[n + i] = a[i];     for (int i = 0; i < (2 * n); i++) pref[i + 1] = pref[i] + a[i];     for (int query = 0; query < (q); query++) {       long long b;       cin >> b;       int lo = 0, hi = 0;       while (lo < n) {         while (lo < n && (hi == lo + n + 1 || pref[hi] > pref[lo] + b)) {           step[lo] = hi - lo - 1;           lo++;         }         hi++;       }       int res = INT_MAX;       pair<int, int> mn = make_pair(INT_MAX, -1);       for (int i = 0; i < (n); i++)         if (step[(i + n - 1) % n] <= step[i]) mn = min(mn, make_pair(step[i], i));       for (int i = 0; i < (mn.first); i++)         res = min(res, calcLen((mn.second + i) % n));       cout << res <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int a[60][60];   bool check[60];   int main() {     int n, x;     cin >> n;     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++) cin >> a[i][j];     for (int i = 0; i < n; i++) {       int t = 0;       for (int j = 0; j < n; j++) check[a[i][j]] = true;       for (int j = 1; j <= n; j++)         if (check[j] == true) t++;       if (t == n - 1) {         x = i;         break;       }     }     for (int i = 0; i < n; i++) {       if (a[x][i] == 0)         cout << n <<    ;       else         cout << a[x][i] <<    ;     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   const long long int mod = 1e9 + 7;   const long long int mod1 = 998244353;   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   long long int rand(long long int l, long long int r) {     uniform_int_distribution<long long int> uid(l, r);     return uid(rng);   }   const long long int N = 2e5 + 5;   vector<pair<long long int, long long int> > v;   long long int dsu[N], sz[N], a[N];   bool active[N];   long long int odd = 0;   long long int root(long long int u) {     return (dsu[u] == u) ? u : (dsu[u] = root(dsu[u]));   }   long long int n;   void uni(long long int u, long long int v) {     if (u < 1 || u > n) return;     u = root(u), v = root(v);     if (u == v) return;     odd -= (sz[u] & 1);     odd -= (sz[v] & 1);     sz[v] += sz[u];     odd += (sz[v] & 1);     dsu[u] = v;   }   void solve() {     cin >> n;     for (long long int i = 1; i <= n; i++) dsu[i] = i, sz[i] = 1;     for (long long int i = 1; i <= n; i++) cin >> a[i], v.push_back({a[i], i});     sort(v.begin(), v.end());     long long int pt = 0;     while (1) {       long long int val = v[pt].first;       while (pt < n && v[pt].first == val) {         active[v[pt].second] = 1;         odd++;         if (active[v[pt].second - 1]) {           uni(v[pt].second - 1, v[pt].second);         }         if (active[v[pt].second + 1]) {           uni(v[pt].second, v[pt].second + 1);         }         pt++;       }       if (odd && pt != n) {         cout <<  NO  <<   n ;         return;       }       if (pt == n) break;     }     cout <<  YES  <<   n ;   }   signed main() {     ios ::sync_with_stdio(0), cin.tie(0), cout.tie(0);     clock_t clk = clock();     long long int t = 1;     while (t--) {       solve();     }     cerr <<   n  << (double)(clock() - clk) / CLOCKS_PER_SEC;     return 0;   }


#include<iostream>   using namespace std;      int main(){    int w,h,n,t;    cin>>t;    while(t--){     int s=1;     cin>>w>>h>>n;     int size=w*h;     while(size>s){      if(w%2==0 && h%2!=0){       w=w/2;       s=s*2;      }      else if(h%2==0 && w%2!=0){       h=h/2;       s=s*2;      }      else if(w%2==0 && h%2==0){       w=w/2;       s=s*2;      }      else {       break;      }     }     if(s>=n){      cout<< Yes <<  n ;     }     else{      cout<< No <<  n ;     }    }   }


#include <bits/stdc++.h>   using namespace std;   int main(void) {     int xa, ya;     int xb, yb;     int xc, yc;     double k, b, y;     scanf( %d %d , &xa, &ya);     scanf( %d %d , &xb, &yb);     scanf( %d %d , &xc, &yc);     if (xa != xb) {       k = (double)(yb - ya) / (xb - xa);       b = ya - (double)(yb - ya) / (xb - xa) * xa;       y = k * xc + b;     }     if (xa < xb) puts(y < yc ?  LEFT  : (yc < y ?  RIGHT  :  TOWARDS ));     if (xa == xb)       puts(xc == xa ?  TOWARDS  : ((xc < xa) != (yb < ya) ?  LEFT  :  RIGHT ));     if (xa > xb) puts(y < yc ?  RIGHT  : (yc < y ?  LEFT  :  TOWARDS ));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 10, inf = 1e9;   int n;   int a[maxn];   int main() {     cin >> n;     for (int i = 1; i <= n; ++i) cin >> a[i];     sort(a + 1, a + 1 + n);     long long lsum = 0, rsum = 0;     for (int i = 1; i <= n / 2; ++i) lsum += a[i];     for (int i = n / 2 + 1; i <= n; ++i) rsum += a[i];     long long ans = lsum * lsum + rsum * rsum;     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool prime(long long n) {     for (long long i = 2; i * i <= n; i++) {       if (n % i == 0) return false;     }     return true;   }   int main() {     long long n, ans;     cin >> n;     if (prime(n))       ans = 1;     else if (n % 2 == 0)       ans = 2;     else if (prime(n - 2))       ans = 2;     else       ans = 3;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long i, j, n, c[123456], a[1111234], b[1125678], d[123456], l, ll, s, t, k,       m, xh, x, x1, y, z;   string p, r, w;   map<string, long long> q;   int main() {     cin >> n >> k >> m;     for (i = 0; i < n; i++) cin >> a[i];     sort(a, a + n);     cout << a[m] - a[m - 1];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, k, s, bigx[11], bigy[11], smlx[11], smly[11];   int main() {     scanf( %d%d%d%d , &n, &m, &k, &s);     for (int i = 1; i <= k; i++)       bigx[i] = bigy[i] = -2020, smlx[i] = smly[i] = 2 * 2020;     for (int i = 0; i < (n); i++)       for (int j = 0; j < (m); j++) {         int t;         scanf( %d , &t);         bigx[t] = max(bigx[t], i + j);         bigy[t] = max(bigy[t], i - j);         smlx[t] = min(smlx[t], i + j);         smly[t] = min(smly[t], i - j);       }     int a, b, ans = -1;     scanf( %d , &a);     for (int i = 1; i < s; i++) {       scanf( %d , &b);       ans = max(ans, max(abs(bigx[a] - smlx[b]), abs(bigx[b] - smlx[a])));       ans = max(ans, max(abs(bigy[a] - smly[b]), abs(bigy[b] - smly[a])));       a = b;     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:500000000 )   using namespace std;   int A[105];   int f[105];   int main() {     int n, i, p, x, y;     cin >> n;     for (i = 0; i < n; i++) cin >> A[i], f[A[i]] = i;     int q;     cin >> q;     while (q--) {       scanf( %d %d %d , &p, &x, &y);       if (p == 1) {         int res = 0;         while (x <= y) {           while (x < y && f[x + 1] > f[x]) x++;           x++;           res++;         }         printf( %d n , res);       } else {         x--;         y--;         int L = A[x], R = A[y];         swap(A[x], A[y]);         f[L] = y;         f[R] = x;       }     }   }


#include <bits/stdc++.h>   #include<limits>   using namespace std;   #define ll long long   #define vi vector<int>   #define vll vector<long long>   #define pi pair<int,int>   #define pll pair<ll,ll>   #define FASTIO   ios_base::sync_with_stdio(false);cin.tie(NULL);   #define unmap unordered_map   #define pb push_back   ll max2(ll x, ll y) {    if (x > y)     return x;    else     return y;   }   ll min2(ll x, ll y) {    if (x < y)     return x;    else     return y;   }   ll gcd(ll a, ll b) {    if (b == 0)     return a;    return gcd(b, a % b);   }   void solve(){          unsigned ll n;       cin>>n;       unsigned ll p=1,sum=0;          while(n>1){           sum+=p;           p*=2;           n>>=1;       }       cout<<sum<<endl;   }      int main() {    FASTIO    ll t = 1;    int q = 0;          cin >> t;       while (t--) {               solve();    }       return 0;      }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       vector<long long> pos, neg, a;       for (int i = 0; i < n; i++) {         long long x;         cin >> x;         a.push_back(x);         if (x >= 0)           pos.push_back(x);         else           neg.push_back(x);       }       sort((pos).begin(), (pos).end());       reverse((pos).begin(), (pos).end());       sort((neg).begin(), (neg).end());       long long ans = 1;       for (int i = 0; i < 5; i++) {         ans *= a[i];       }       for (int m = 0; m <= 4 and m <= neg.size(); m += 2) {         int p = 5 - m;         if (p > pos.size()) continue;         long long pro = 1;         for (int i = 0; i < p; i++) pro *= pos[i];         for (int i = 0; i < m; i++) pro *= neg[i];         ans = max(ans, pro);       }       if (neg.size() >= 5) {         reverse((neg).begin(), (neg).end());         long long temp = 1;         for (int i = 0; i < 5; i++) {           temp *= neg[i];         }         ans = max(ans, temp);       }       cout << ans << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000010;   int tree[4 * N];   int n, m;   void update(int root, int lo, int hi, int u, int v) {     if (v < lo || u > hi) return;     if (u <= lo && hi <= v) {       tree[root]++;       return;     }     int mid = (lo + hi) / 2, left = root * 2, right = left + 1;     update(left, lo, mid, u, v);     update(right, mid + 1, hi, u, v);     tree[root] = tree[left] + tree[right];   }   int query(int root, int lo, int hi, int u, int v) {     if (v < lo || u > hi) return 0;     if (u <= lo && hi <= v) return tree[root];     int mid = (lo + hi) / 2, left = root * 2, right = left + 1;     return query(left, lo, mid, u, v) + query(right, mid + 1, hi, u, v);   }   int main() {     scanf( %d%d , &n, &m);     int i, cur = 0, next, start, end, p1, p2;     long long ans = 1;     for (i = 1; i <= n; ++i) {       next = (cur + m) % n;       start = min(cur, next) + 1;       end = max(cur, next) + 1;       p1 = query(1, 1, n, start + 1, end - 1);       p2 = 2 * (i - 1) - query(1, 1, n, start, start) - query(1, 1, n, end, end) -            p1;       ans += min(p1, p2) + 1;       update(1, 1, n, start, start);       update(1, 1, n, end, end);       cur = next;       printf( %lld  , ans);     }     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   double ed(double x1, double y1, double x2, double y2) {     return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     double ax, ay, bx, by, tx, ty;     int n;     cin >> ax >> ay >> bx >> by >> tx >> ty >> n;     double td = 0;     vector<pair<double, int>> p1(n), p2(n);     for (int i = 0; i < n; i++) {       int x, y;       cin >> x >> y;       td += 2 * ed(x, y, tx, ty);       p1[i] = {ed(x, y, ax, ay) - ed(x, y, tx, ty), i};       p2[i] = {ed(x, y, bx, by) - ed(x, y, tx, ty), i};     }     sort(p1.begin(), p1.end());     sort(p2.begin(), p2.end());     double ans = min(td + p1[0].first, td + p2[0].first);     if (n == 1) {       cout << setprecision(12) << ans;       return 0;     }     if (p1[0].second != p2[0].second) {       ans = min(ans, td + p1[0].first + p2[0].first);     } else {       ans = min(ans, min(td + p1[0].first + p2[1].first,                          td + p1[1].first + p2[0].first));     }     cout << setprecision(12) << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   pair<int, int> tree[4 * 100005 + 10];   int a[100005];   int dp[100005];   int n, s, l;   int f[4 * 100005 + 10];   void init(int node, int b, int e) {     if (b == e) {       tree[node].first = tree[node].second = a[b];       return;     }     int mid = (b + e) / 2;     int lft = node * 2;     int rght = lft + 1;     init(lft, b, mid);     init(rght, mid + 1, e);     tree[node].first = min(tree[lft].first, tree[rght].first);     tree[node].second = max(tree[lft].second, tree[rght].second);     return;   }   pair<int, int> query(int node, int b, int e, int i, int j) {     if (i <= b && j >= e) return tree[node];     int mid = (b + e) / 2;     int lft = node * 2;     int rght = lft + 1;     if (j <= mid)       return query(lft, b, mid, i, j);     else if (mid < i)       return query(rght, mid + 1, e, i, j);     else {       pair<int, int> c, d, ans;       c = query(lft, b, mid, i, j);       d = query(rght, mid + 1, e, i, j);       ans.first = min(c.first, d.first);       ans.second = max(c.second, d.second);       return ans;     }   }   void update_(int node, int b, int e, int indx) {     if (b == e) {       f[node] = dp[indx];       return;     }     int mid = (b + e) / 2;     if (indx <= mid)       update_(node * 2, b, mid, indx);     else       update_(node * 2 + 1, mid + 1, e, indx);     f[node] = min(f[node * 2], f[node * 2 + 1]);   }   int get(int node, int b, int e, int i, int j) {     if (b >= i && j >= e) return f[node];     int mid = (b + e) / 2;     int c, d;     if (j <= mid)       return get(node * 2, b, mid, i, j);     else if (mid < i)       return get(node * 2 + 1, mid + 1, e, i, j);     else {       c = get(node * 2, b, mid, i, j);       d = get(node * 2 + 1, mid + 1, e, i, j);       return min(c, d);     }   }   int solve() {     pair<int, int> tmp;     if (l > n) return 20000000;     tmp = query(1, 1, n, 1, l);     if (tmp.second - tmp.first <= s)       dp[l] = 1;     else       return 20000000;     int j = 1;     update_(1, 1, n, l);     for (int i = l + 1; i <= n; i++) {       while (j <= i) {         tmp = query(1, 1, n, j, i);         if (tmp.second - tmp.first <= s) break;         j++;       }       if (i - j + 1 >= l) {         if (j == 1)           dp[i] = 1;         else           dp[i] = get(1, 1, n, j - 1, i - l) + 1;       } else         dp[i] = 20000000;       update_(1, 1, n, i);     }     return dp[n];   }   int main() {     cin >> n >> s >> l;     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     init(1, 1, n);     for (int i = 0; i <= 4 * n; i++) f[i] = 20000000;     int res = solve();     if (res >= 20000000)       cout << -1;     else       cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int n;   const long long int mod = 1000000007;   long long int f(long long int x, long long int k) {     long long int res = 1;     while (k) {       if (k & 1) res = res * x % mod;       x = x * x % mod;       k >>= 1;     }     return res;   }   int main() {     cin >> n;     n++;     if (n == 1)       cout <<  1 ;     else {       long long int a = f(4, n - 2);       long long int b = f(2, n - 2);       a = a * 2 % mod;       cout << (a + b) % mod;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int long long> fact(1000001);   int long long sm, a, b, n;   bool check() {     int long long m = sm;     while (m) {       if (m % 10 != b && m % 10 != a) return 0;       m /= 10;     }     return 1;   }   int long long inc(int long long t, int long long p) {     if (p == 0) return 1;     if (p % 2 == 0) {       int long long e = inc(t, p / 2);       return (e * e) % 1000003;     } else {       return (inc(t, p - 1) * t) % 1000003;     }   }   int long long comb(int long long i, int long long m) {     return (fact[m] *             (inc(((fact[m - i] * fact[i]) % 1000003), 1000003 - 2) % 1000003)) %            1000003;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie();     cout.tie();     int long long a, b;     cin >> a >> b;     fact[0] = fact[1] = 1;     for (long long i = 2; i <= 1000000; i++)       fact[i] = i * fact[i - 1], fact[i] %= 1000003;     cout << comb(b, a + b) - 1 << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     double net, res;     int d, h, vel, e;     cin >> d >> h >> vel >> e;     net = e - (vel / (3.14159265358979323846 * d * d / 4.0));     res = -h / net;     if (res <= 0 || res > 10000)       cout <<  NO n ;     else       cout <<  YES n  << fixed << setprecision(12) << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, comp_num;   vector<vector<int> > g;   vector<bool> used;   vector<vector<int> > comp;   vector<long long int> dv;   vector<long long int> dv_in_comp;   vector<int> components;   void dfs(int v) {     used[v] = true;     comp[comp.size() - 1].push_back(v);     dv_in_comp[dv_in_comp.size() - 1] += dv[v];     for (int i = 0; i < g[v].size(); i++) {       int to = g[v][i];       if (!used[to]) dfs(to);     }   }   void find_comps() {     for (int i = 0; i < n; i++) {       if (!used[i]) {         comp.push_back(vector<int>());         dv_in_comp.push_back(0);         dfs(i);       }     }   }   int main() {     cin >> n >> m;     g.resize(n);     dv.resize(n, 0);     used.resize(n, false);     components.resize(n, -1);     int ui, vi;     for (int i = 0; i < m; i++) {       scanf( %d%d , &ui, &vi);       ui--;       vi--;       g[ui].push_back(vi);       g[vi].push_back(ui);       dv[ui]++;       dv[vi]++;     }     find_comps();     int total = 0;     for (int i = 0; i < comp.size(); i++) {       if (dv_in_comp[i] > 2 * (comp[i].size() - 1)) {         total += 0;       } else         total++;     }     cout << total;     return 0;   }