#include <bits/stdc++.h>   using namespace std;   int N, M;   pair<pair<long long, long long>, int> range[200000 - 1];   pair<long long, int> bridge[200000];   priority_queue<pair<long long, int>, vector<pair<long long, int> >,                  greater<pair<long long, int> > >       rends;   int ans[200000 - 1];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> N >> M;     long long prevL, prevR;     cin >> prevL >> prevR;     for (int i = 0; i < N - 1; i++) {       long long L, R;       cin >> L >> R;       range[i] = {{L - prevR, R - prevL}, i};       prevL = L;       prevR = R;     }     for (int i = 0; i < M; i++) {       cin >> bridge[i].first;       bridge[i].second = i;     }     sort(range, range + N - 1);     sort(bridge, bridge + M);     int ind = 0;     for (int i = 0; i < M; i++) {       while (ind < N - 1 && bridge[i].first >= range[ind].first.first) {         rends.push(make_pair(range[ind].first.second, range[ind].second));         ind++;       }       if (!rends.empty()) {         pair<long long, int> e = rends.top();         rends.pop();         if (e.first >= bridge[i].first) {           ans[e.second] = bridge[i].second + 1;         } else {           cout <<  No  << endl;           return 0;         }       }     }     if (ind < N - 1 || !rends.empty()) {       cout <<  No  << endl;       return 0;     }     cout <<  Yes  <<   n ;     cout << ans[0];     for (int i = 1; i < N - 1; i++) {       cout <<     << ans[i];     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int s;     cin >> s;     while (s) {       int n, a, b, c, d;       bool status = true;       cin >> n >> a >> b >> c >> d;       int rangem = (a - b) * n, rangeM = (a + b) * n;       if (((c + d) < rangem) || (c - d) > rangeM) status = false;       if (status)         cout <<  Yes  << endl;       else         cout <<  No  << endl;       s--;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long num[200000];   int sn = 0, asn = 0, save[200000], as[200000], m, n;   struct str {     int v, x;     long long l, c;   } str[200000];   void pre1() {     scanf( %d , &m);     int i, j;     for (i = 0; i < m; i++) {       scanf( %I64d , &str[i].v);       if (str[i].v == 1) {         scanf( %I64d , &str[i].x);       } else         scanf( %I64d%I64d , &str[i].l, &str[i].c);     }     scanf( %d , &n);     for (j = 0; j < n; j++) scanf( %I64d , &num[j]);   }   void table() {     int i, k, j;     for (i = 0; i < m; i++) {       if (str[i].v == 1) {         save[sn++] = str[i].x;         if (sn == 100000) return;       } else {         for (k = 0; k < str[i].c; k++) {           for (j = 0; j < str[i].l; j++) {             save[sn++] = save[j];             if (sn == 100000) return;           }         }       }     }   }   void work1() {     table();     int j = 0, i = 0;     long long now = 0;     while (i < n) {       while (j < m) {         if (str[j].v == 1) {           now++;           if (now == num[i]) {             as[asn++] = str[j].x;             i++;             if (i >= n) return;             j++;             break;           }           j++;         } else {           long long nn = now;           now += str[j].l * str[j].c;           if (now >= num[i]) {             while (i < n) {               if (num[i] <= now) {                 as[asn++] = save[(num[i++] - nn - 1) % str[j].l % 100000];                 if (i >= n) return;               } else {                 j++;                 break;               }             }           } else {             j++;           }         }       }     }   }   int main() {     pre1();     work1();     for (int i = 0; i < asn - 1; i++) printf( %d  , as[i]);     printf( %d n , as[asn - 1]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void setIO() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   const long long N = 3e2 + 0;   const long long M = 5e5 + 5;   const long long L = 2e1 + 1;   const long long inf = 9e17;   const long long mod = 1e9 + 7;   long long xyz = 1;   long long n, k;   long long dp[N][N];   long long fac[N];   long long inv[N];   long long chs[N][N];   long long pwr[2][N];   long long bin(long long b, long long e) {     return b == k ? pwr[0][e] : pwr[1][e];   }   long long cal(long long b, long long e) {     if (e == 0) return 1;     if (e == 1) return b;     if (e == 2) return b * b % mod;     return cal(cal(b, e / 2), 2) * cal(b, e % 2) % mod;   }   void prc(long long n, long long k) {     chs[n][k] = fac[n] * inv[k] % mod * inv[n - k] % mod;   }   void cmp(long long b, long long e) {     if (b == k - 0) {       pwr[0][e] = cal(b, e);     }     if (b == k - 1) {       pwr[1][e] = cal(b, e);     }   }   long long ncr(long long n, long long k) { return chs[n][k]; }   void run() {     cin >> n >> k;     fac[0] = 1;     inv[0] = 1;     for (long long i = 1; i <= n; i++) {       fac[i] = fac[i - 1] * i % mod;       inv[i] = inv[i - 1] * cal(i, mod - 2) % mod;     }     for (long long i = 0; i <= n; i++)       for (long long j = 0; j <= i; j++) prc(i, j);     for (long long i = 0; i <= n; i++) cmp(k, i), cmp(k - 1, i);     for (long long i = 1; i <= n; i++)       dp[1][i] = ncr(n, i) * bin(k - 1, n - i) % mod;     for (long long i = 2; i <= n; i++) {       for (long long j = 1; j <= n; j++) {         dp[i][j] = (bin(k, j) - bin(k - 1, j) + mod) * bin(k - 1, n - j) % mod *                    dp[i - 1][j] % mod;         for (long long x = 1; x < j; x++) {           dp[i][j] += bin(k - 1, n - j) * bin(k, x) % mod * ncr(n - x, j - x) %                       mod * dp[i - 1][x] % mod;           dp[i][j] %= mod;         }       }     }     cout << dp[n][n] << endl;   }   signed main() {     setIO();     while (xyz--) run();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int odd[2000], even[2000];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     for (int i = 1; i <= n * n; i += 2) {       odd[(i - 1) / 2] = i;     }     for (int i = 2; i <= n * n; i += 2) {       even[i / 2 - 1] = i;     }     int i = 0, k = 0;     if (n == 3) {       cout <<  2 1 4 n ;       cout <<  3 5 7 n ;       cout <<  6 9 8 n ;       return 0;     }     for (int x = 0; x < n; x++) {       for (int y = 0; y < n; y++) {         if ((n - 1) % 4 == 0) {           if (x == y) {             cout << odd[i++] <<    ;             continue;           }           if ((x < n / 2 && y > n / 2) || (x > n / 2 && y < n / 2)) {             cout << odd[i++] <<    ;             continue;           }         } else {           if (y < n / 2) {             cout << odd[i++] <<    ;             continue;           }           if ((x == 0 && y < n - 2) || (x == 2 && y >= n - 2)) {             cout << odd[i++] <<    ;             continue;           }         }         cout << even[k++] <<    ;       }       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 5e3 + 5, mod = 1e9 + 7, mod1 = 998244353, mod2 = 1e9 + 9,                   inf = 1e18 + 7;   const long long infll = 1e18 + 7;   long long n, k, X, Y, Z;   long long a[(long long)3e5 + 5];   long long dp[N][N];   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> k;     Y = n % k;     X = k - Y;     Z = n / k;     for (long long i = 1; i <= n; i++) {       cin >> a[i];     }     sort(a + 1, a + 1 + n);     for (long long i = 0; i <= X; i++) {       for (long long j = 0; j <= Y; j++) {         if (i == 0 && j == 0) {           dp[i][j] = 0;           continue;         }         dp[i][j] = inf;         long long t = i * Z + j * (Z + 1);         if (i) {           dp[i][j] = min(dp[i][j], dp[i - 1][j] + a[t] - a[t - Z + 1]);         }         if (j) {           dp[i][j] = min(dp[i][j], dp[i][j - 1] + a[t] - a[t - Z]);         }       }     }     cout << dp[X][Y];   }


#include <bits/stdc++.h>   using namespace std;   int a[100][100];   int main() {     int n, m, z, sum, sum2, ans = 1e9, temp;     scanf( %d %d %d , &n, &m, &z);     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) scanf( %d , &a[i][j]);     if (n > 10)       for (int i = 0; i < n; i++) {         temp = 0;         for (int j = 0; j < n; j++)           if (i != j) {             sum = sum2 = 0;             for (int k = 0; k < m; k++) {               sum += (a[i][k] == a[j][k]);               sum2 += (a[i][k] != a[j][k]);             }             temp += min(sum, sum2);           }         ans = min(ans, temp);       }     else {       for (int i = 0; i < 1 << n; i++) {         temp = 0;         for (int j = 0; j < m; j++) {           sum = sum2 = 0;           for (int k = 0; k < n; k++) {             sum += (a[k][j] == ((i >> k) & 1));             sum2 += (a[k][j] != ((i >> k) & 1));           }           temp += min(sum, sum2);         }         ans = min(ans, temp);       }     }     printf( %d n , ans > z ? -1 : ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long n;     cin >> n;     vector<long long> arr(n);     for (long long i = 0; i < n; ++i) {       cin >> arr[i];     }     sort(arr.begin(), arr.end());     long long grp = 0;     long long ptr = 0;     unordered_map<long long, long long> mp;     long long last = 0;     for (long long i = 0; i < n; ++i) {       if (arr[i] == 1) {         grp++;         continue;       } else if (ptr > 0) {         ptr--;         if (ptr == 0 && arr[i] == arr[i - 1]) {           grp++;         } else {           ptr = arr[i] - (i - last + 1);         }       } else {         ptr = arr[i] - 1;         last = i;       }     }     cout << grp <<   n ;   }   int32_t main() {     long long t;     cin >> t;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   long long a[100005];   void solve() {     long long n;     cin >> n;     for (long long i = 0; i < n; i++) cin >> a[i];     sort(a, a + n);     vector<long long> ans;     long long st = 0, end = n - 1;     while (end > st) {       ans.push_back(a[end]);       ans.push_back(a[st]);       st++;       end--;     }     if (st == end) {       ans.push_back(a[st]);     }     reverse((ans).begin(), (ans).end());     for (auto i : ans) {       cout << i <<    ;     }   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long t = 1;     cin >> t;     while (t--) {       solve();       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   int n, v[200000], m[200000];   long long s[200000];   pair<int, int> p[200000];   unordered_map<int, vector<pair<int, long long> > > mp;   bool cmp(pair<int, int> a, pair<int, int> b) { return a.second < b.second; }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %d%d , &p[i].second, &p[i].first);     sort(p, p + n, cmp);     for (int i = 0; i < n; i++) {       int lo = 0;       int hi = i - 1;       int j = -1;       while (lo <= hi) {         int mid = (lo + hi) / 2;         if (p[mid].second <= p[i].first) {           lo = mid + 1;           j = mid;         } else           hi = mid - 1;       }       if (j > -1) {         v[i] = m[j] + p[i].second - p[i].first;         int lo = 0;         int hi = mp[m[j]].size() - 1;         int k = -1;         while (lo <= hi) {           int mid = (lo + hi) / 2;           if (mp[m[j]][mid].first <= j) {             k = mid;             lo = mid + 1;           } else             hi = mid - 1;         }         if (k > -1)           s[i] = mp[m[j]][k].second;         else           s[i] = 1LL;         if (mp[v[i]].size() == 0)           mp[v[i]].push_back({i, s[i]});         else           mp[v[i]].push_back({i, (mp[v[i]].back().second + s[i]) % mod});       } else {         v[i] = p[i].second - p[i].first;         s[i] = 1LL;         if (mp[v[i]].size() == 0)           mp[v[i]].push_back({i, 1LL});         else           mp[v[i]].push_back({i, (mp[v[i]].back().second + 1LL) % mod});       }       m[i] = max(i > 0 ? m[i - 1] : 0, v[i]);     }     int h = p[n - 1].second - v[n - 1];     int lo = p[n - 1].first;     long long res = 0;     for (int i = n - 1; i >= 0; i--) {       if (p[i].second <= lo) break;       if (h > p[i].second - v[i]) {         h = p[i].second - v[i];         res = s[i];       } else if (h == p[i].second - v[i])         res = (res + s[i]) % mod;       lo = max(lo, p[i].first);     }     cout << (res % mod) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int in() {     int32_t x;     scanf( %d , &x);     return x;   }   inline long long lin() {     long long x;     scanf( %lld , &x);     return x;   }   inline int mul(int a, int b) {     long long x = a;     x *= (long long)b;     if (x >= 1000000007) x %= 1000000007;     return x;   }   inline int add(int a, int b) {     return (a + b) >= 1000000007 ? a + b - 1000000007 : a + b;   }   inline int sub(int a, int b) {     return (a - b) < 0 ? 1000000007 - b + a : a - b;   }   const int MAX = INT_MAX;   const int MIN = INT_MIN;   char c[100005];   int y[100005];   int main() {     int n;     cin >> n;     for (int i = 0; i < n; i++) cin >> c[i];     int d = 0, d1 = 0;     for (int i = 0; i < n; i++) {       y[i] = c[i] -  0 ;       if (y[i] == 1) d++;       if (y[i] == 0) d1++;     }     int f = 1;     int f2 = 0;     for (int i = 1; i < n; i++) {       if (y[i - 1] != y[i]) f++;     }     for (int i = 0; i < n - 1; i++) {       if (y[i] == y[i + 1]) {         f2 = 1;         break;       }     }     cout << min(f + 2, n);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, last;   struct rec {     int s, t;   } da[50100];   bool cmp(rec a, rec b) {     if (a.s < b.s) return true;     if (a.s > b.s) return false;     if (a.t < b.t) return true;     return false;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d%d , &da[i].s, &da[i].t);     sort(da + 1, da + n + 1, cmp);     for (int i = 1; i <= n; i++)       if (da[i].t >= last) {         last = da[i].t;       } else         last = da[i].s;     printf( %d , last);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int N;   long long A[10], rem[10];   int totnum, numv[10], pos[10], tat[10];   int result = 0x3f3f3f3f;   inline bool compare(const int& i1, const int& i2) {     return numv[i1] > numv[i2];   }   void Back(int x) {     if (x == N + 1) {       int num0 = 0;       for (int i = 1; i <= N; ++i)         if (tat[i] == 0) ++num0;       int resnow = (num0 >= 2) + N + totnum;       for (int i = 1; i <= N; ++i)         if (tat[i] != 0) resnow -= numv[pos[i]];       result = min(result, resnow);       return;     }     for (int i = 0; i <= x - 1; ++i)       if (i == 0 || rem[pos[i]] % A[pos[x]] == 0) {         rem[pos[i]] /= A[pos[x]];         tat[x] = i;         Back(x + 1);         rem[pos[i]] *= A[pos[x]];       }   }   int main() {     cin >> N;     for (int i = 1; i <= N; ++i) {       cin >> A[i];       long long aux = A[i];       for (int j = 2; 1LL * j * j <= aux; ++j)         while (aux % j == 0) {           ++numv[i];           aux /= j;         }       if (aux != 1) ++numv[i];       totnum += numv[i];       if (numv[i] == 1) --totnum;       pos[i] = i;       rem[i] = A[i];     }     sort(pos + 1, pos + N + 1, compare);     Back(1);     cout << result <<   n ;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:256000000 )   using namespace std;   const int SZ = 110;   const int INF = 1000 * 1000 * 1000;   double start, finish;   void prepare(string s) {     if ((int)s.size() != 0) {       freopen((s +  .in ).c_str(),  r , stdin);       freopen((s +  .out ).c_str(),  w , stdout);     }   }   void startTimer() {}   void stopTimer() {}   int n;   vector<int> g;   vector<vector<int> > p;   void getData() {     cin >> n;     g.resize(n);     for (int i = 0; i < n; ++i) {       cin >> g[i];     }     p.resize(n);     for (int i = 0; i < n; ++i) {       p[i].resize(g[i]);       for (int j = 0; j < g[i]; ++j) {         cin >> p[i][j];       }     }   }   void solve() {     int ans = INF;     g.assign(n, 0);     for (int i = 0; i < n; ++i) {       int cnt = 0;       for (int j = 0; j < (int)p[i].size(); ++j) {         cnt += p[i][j] * 5 + 15;       }       ans = min(ans, cnt);     }     cout << ans << endl;   }   int main() {     prepare(  );     ios::sync_with_stdio(false);     startTimer();     getData();     solve();     stopTimer();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void RI() {}   template <typename... T>   void RI(int& head, T&... tail) {     scanf( %d , &head);     RI(tail...);   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n;     RI(n);     cout <<  YES  << endl;     while (n--) {       int x1, y1, x2, y2;       RI(x1, y1, x2, y2);       cout << (((x1 & 1) << 1) + (y1 & 1) + 1) << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool point[2002][2002];   int N;   int x[3000], y[3000];   const int offset = 1001;   int main() {     while (scanf( %d , &N) != EOF) {       memset(point, 0, sizeof point);       for (int i = 0; i < N; ++i) {         scanf( %d%d , &x[i], &y[i]);         point[x[i] + offset][y[i] + offset] = true;       }       int tot = 0;       for (int i = 0; i < N; ++i) {         for (int j = i + 1; j < N; ++j) {           if (((x[i] + x[j]) & 1) == 1 || ((y[i] + y[j]) & 1) == 1) continue;           int mx = (x[i] + x[j]) >> 1;           int my = (y[i] + y[j]) >> 1;           if (point[mx + offset][my + offset]) ++tot;         }       }       printf( %d n , tot);     }   }


#include <bits/stdc++.h>   using namespace std;   int head[(1000 + 10)], to[(1000 + 10) * (1000 + 10)],       pre[(1000 + 10) * (1000 + 10)], tot;   bool vis[(1000 + 10)], color[(1000 + 10)];   int tag[(1000 + 10)], cc, L[(1000 + 10)], dis[(1000 + 10)];   int n, m;   void adde(int u, int v) { to[++tot] = v, pre[tot] = head[u], head[u] = tot; }   void dfs(int u) {     tag[u] = cc;     for (int i = head[u]; i; i = pre[i]) {       int v = to[i];       if (vis[v]) {         if (color[u] == color[v]) {           printf( -1 );           exit(0);         } else           continue;       }       vis[v] = 1, color[v] = 1 ^ color[u];       dfs(v);     }   }   void bfs(int u) {     memset(dis, 0xff, sizeof(dis));     dis[u] = 0;     int& res = L[tag[u]];     queue<int> Q;     Q.push(u);     while (!Q.empty()) {       int v = Q.front();       Q.pop();       for (int i = head[v]; i; i = pre[i]) {         int vv = to[i];         if (dis[vv] != -1) continue;         dis[vv] = dis[v] + 1;         Q.push(vv);         res = max(res, dis[vv]);       }     }   }   int main() {     scanf( %d %d , &n, &m);     while (m--) {       int u, v;       scanf( %d %d , &u, &v);       adde(u, v), adde(v, u);     }     for (int i = 1; i <= n; i++) {       if (!vis[i]) {         vis[i] = 1;         cc++;         dfs(i);       }     }     for (int i = 1; i <= n; i++) bfs(i);     int ans = 0;     for (int i = 1; i <= cc; i++) ans += L[i];     printf( %d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 5;   const long long INF = 1e18;   const long long LIMIT = 1500000LL * 1500000LL;   vector<int> V;   int ans[MAXN];   struct node {     long long x, y, pos;     long long dis() { return x * x + y * y; }   };   node p[MAXN];   bool cmp(node A, node B) { return A.dis() > B.dis(); }   node operator+(const node &A, const node &B) {     node ans = A;     ans.x += B.x;     ans.y += B.y;     return ans;   }   node operator-(const node &A, const node &B) {     node ans = A;     ans.x -= B.x;     ans.y -= B.y;     return ans;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     for (int i = 0; i < n; i++) cin >> p[i].x >> p[i].y, p[i].pos = i;     node cur{0, 0, 0};     for (int i = n - 1; i >= 0; i--) {       if (cmp(cur - p[i], cur + p[i]))         cur = cur + p[i], ans[i] = 1;       else         cur = cur - p[i], ans[i] = -1;     }     for (int i = 0; i < n; i++) cout << ans[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   const int N = 120005;   int n, p[N], m;   int stack1[N], top1, stack2[N], top2;   struct query {     int l, r, id;   } q[N];   bool operator<(query a, query b) { return a.r < b.r; }   int min[N << 2], num[N << 2], tag0[N << 2], tag1[N << 2];   long long sum[N << 2], ans[N];   void build(int o = 1, int l = 1, int r = n) {     min[o] = l, num[o] = 1;     if (l == r) return;     int mid = l + r >> 1;     build(o << 1, l, mid), build(o << 1 | 1, mid + 1, r);   }   void settag0(int o, int v) { tag0[o] += v, min[o] += v; }   void settag1(int o, int v) { sum[o] += (long long)v * num[o], tag1[o] += v; }   void pushdown(int o) {     if (tag0[o])       settag0(o << 1, tag0[o]), settag0(o << 1 | 1, tag0[o]), tag0[o] = 0;     if (tag1[o]) {       if (min[o] == min[o << 1]) settag1(o << 1, tag1[o]);       if (min[o] == min[o << 1 | 1]) settag1(o << 1 | 1, tag1[o]);       tag1[o] = 0;     }   }   void pushup(int o) {     min[o] = std::min(min[o << 1], min[o << 1 | 1]);     num[o] = 0;     if (min[o] == min[o << 1]) num[o] += num[o << 1];     if (min[o] == min[o << 1 | 1]) num[o] += num[o << 1 | 1];     sum[o] = sum[o << 1] + sum[o << 1 | 1];   }   void modify(int L, int R, int v, int o = 1, int l = 1, int r = n) {     if (L <= l && R >= r) return settag0(o, v);     int mid = l + r >> 1;     pushdown(o);     if (L <= mid) modify(L, R, v, o << 1, l, mid);     if (R > mid) modify(L, R, v, o << 1 | 1, mid + 1, r);     pushup(o);   }   long long query(int L, int o = 1, int l = 1, int r = n) {     if (L == l) return sum[o];     int mid = l + r >> 1;     pushdown(o);     if (L <= mid)       return query(L, o << 1, l, mid) + sum[o << 1 | 1];     else       return query(L, o << 1 | 1, mid + 1, r);   }   int main() {     std::ios::sync_with_stdio(0), std::cin.tie(0);     std::cin >> n;     for (int i = 1; i <= n; ++i) std::cin >> p[i];     std::cin >> m;     for (int i = 0; i < m; ++i) std::cin >> q[i].l >> q[i].r, q[i].id = i;     build(), std::sort(q, q + m);     int t = 0;     for (int i = 1; i <= n; ++i) {       while (top1 && p[stack1[top1]] < p[i])         modify(stack1[top1 - 1] + 1, stack1[top1], p[i] - p[stack1[top1]]),             --top1;       while (top2 && p[stack2[top2]] > p[i])         modify(stack2[top2 - 1] + 1, stack2[top2], p[stack2[top2]] - p[i]),             --top2;       stack1[++top1] = stack2[++top2] = i, settag1(1, 1);       while (t < m && q[t].r == i) ans[q[t].id] = query(q[t].l), ++t;     }     for (int i = 0; i < m; ++i) std::cout << ans[i] <<   n ;     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   const int INF = (int)1e9;   const int EPS = 1e-6;   const int mod = 1000000007;   const long double PI = 3.14159265359;   const int dx4[4] = {0, 1, 0, -1};   const int dy4[4] = {-1, 0, 1, 0};   const int dx8[8] = {-1, -1, -1, 0, 1, 1, 1, 0};   const int dy8[8] = {-1, 0, 1, 1, 1, 0, -1, -1};   inline bool grid_inside(int x, int y, int n, int m) {     return x >= 1 && x <= n && y >= 1 && y <= m;   }   template <typename T>   T lcm(T a, T b) {     return (a * b) / __gcd(a, b);   }   template <typename T>   T Dis(T x1, T y1, T x2, T y2) {     return sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2)));   }   template <typename T>   T Angle(T x1, T y1, T x2, T y2) {     return atan((double)(y1 - y2) / (double)(x1 - x2));   }   inline int Set(int N, int pos) { return N = N | (1 << pos); }   inline int Reset(int N, int pos) { return N = N & ~(1 << pos); }   inline bool Check(int N, int pos) { return (bool)(N & (1 << pos)); }   template <class T, class X>   inline T togglebit(T a, X i) {     T t = 1;     return (a ^ (t << i));   }   bool compare(const pair<int, int>& i, const pair<int, int>& j) {     return i.first < j.first;   }   int LSOne(long long int n) { return log2(n & -n) + 1; }   int rangeClear(int n, int i, int j) {     int ones = ~0;     int a = ones << (j + 1);     int b = (i << 2) - 1;     int mask = a | b;     int ans = n & mask;     return ans;   }   long long int mul_mod(long long int x, long long int y, long long int mod) {     long long int i = 0, j = x;     while (y > 0) {       if (y & 1) {         i = (i + j) % mod;       }       j = (j + j) % mod;       y >>= 1;     }     return i;   }   long long int fast_power(int a, int x) {     if (x == 0)       return 1;     else if (x == 1)       return a;     else {       long long int R = fast_power(a, x >> 1);       if (!(x & 1))         return R * R;       else         return R * a * R;     }   }   long long int fast_power_unlocked(long long int a, long long int n,                                     long long int mod) {     long long int res = 1;     while (n > 0) {       if (n & 1) {         res = mul_mod(res, a, mod);       }       a = mul_mod(a, a, mod);       n = n >> 1;     }     return res;   }   long long int modInverse(long long int A, long long int M) {     return fast_power_unlocked(A, M - 2, M);   }   const int maxn = 1e5 + 5;   vector<int> g[maxn];   bool ok;   int p[maxn], h[maxn], hp[maxn], vis_cnt[maxn];   void dfs(int src, int par) {     vis_cnt[src] = p[src];     int thp = 0;     for (auto x : g[src]) {       if (x == par) continue;       dfs(x, src);       thp += hp[x];       vis_cnt[src] += vis_cnt[x];     }     if ((vis_cnt[src] + h[src]) % 2 == 1) ok = false;     hp[src] = (vis_cnt[src] + h[src]) / 2;     if (vis_cnt[src] + h[src] < 0) ok = false;     if (hp[src] >= 0 && (vis_cnt[src] >= hp[src])) {     } else       ok = false;     if (thp > hp[src]) ok = false;   }   void solve() {     int n, m;     cin >> n >> m;     for (int i = 0; i < n; i++) cin >> p[i];     for (int i = 0; i < n; i++) cin >> h[i];     for (int i = 0; i < n - 1; i++) {       int x, y;       cin >> x >> y;       --x;       --y;       g[x].emplace_back(y);       g[y].emplace_back(x);     }     ok = true;     dfs(0, -1);     if (ok)       cout <<  YES ;     else       cout <<  NO ;     for (int i = 0; i < n; i++) g[i].clear();   }   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     ;     for (auto blockTime = make_pair(chrono::high_resolution_clock::now(), true);          blockTime.second;          fprintf(stderr,  %s: %ld ms n ,  Time Taken ,                  chrono::duration_cast<chrono::milliseconds>(                      chrono::high_resolution_clock::now() - blockTime.first)                      .count()),               fflush(stderr), blockTime.second = false) {       int t;       cin >> t;       for (int i = 0; i < t; ++i) {         solve();         cout <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2005;   int n;   long long dp[N], a[N], ans, k, l[N], r[N];   int main() {     scanf( %d%lld , &n, &k);     for (int i = 1; i <= n; ++i) scanf( %lld%lld%lld , l + i, r + i, a + i);     int flag = 0;     for (int i = n; i >= 1; --i) {       if (r[i] == l[i + 1])         dp[i] = max((long long)0, a[i] + dp[i + 1] - (r[i] - l[i]) * k);       else         dp[i] = max((long long)0, a[i] - (r[i] - l[i]) * k);       if (dp[i] > k) {         flag = 1;         break;       }     }     if (flag) {       printf( -1 n );       return 0;     }     long long tmp = k;     for (int i = 1; i <= n; ++i) {       if (tmp < dp[i]) ans += tmp, tmp = k;       ans += a[i];       if (a[i] >= tmp)         a[i] -= tmp, tmp = k - a[i] % k;       else         tmp -= a[i];     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 5e5 + 11;   int n, m;   int cha[N], fa[N];   int dfn[N], tot, sz[N];   int U, V;   int head[N], nex[N << 1], to[N << 1], size = 1;   bool vis[N << 1];   int fr[N];   int dl[N], zh[N], tp;   void add(int x, int y) {     to[++size] = y;     nex[size] = head[x];     head[x] = size;   }   void dfs(int u) {     dfn[u] = ++tot;     sz[u] = 1;     for (int i = head[u]; i; i = nex[i]) {       int v = to[i];       if (v == fa[u]) continue;       if (!dfn[v]) {         fa[v] = u;         dfs(v);         sz[u] += sz[v];         cha[u] += cha[v];       } else if (dfn[v] < dfn[u]) {         cha[u]++;         cha[v]--;       }     }     if (!U && cha[u] > 1) {       U = u;       return;     }   }   void bfs(int x, int id) {     int hd = 0, tl = 1;     dl[1] = x;     for (int i = 1; i <= n; i++) fr[i] = 0;     fr[x] = -1;     while (hd < tl) {       int u = dl[++hd];       for (int i = head[u]; i; i = nex[i]) {         int v = to[i];         if (fr[v] != 0 || vis[i]) continue;         fr[v] = i;         if (v == V) break;         dl[++tl] = v;       }     }     for (int v = V; v != U; v = to[fr[v] ^ 1]) {       zh[++tp] = v;       vis[fr[v]] = vis[fr[v] ^ 1] = 1;     }     zh[++tp] = U;   }   void get_V() {     for (int u = fa[U]; u; u = fa[u]) {       int cnt = 0;       for (int i = head[u]; i; i = nex[i]) {         int v = to[i];         if (dfn[v] >= dfn[U] + (u == fa[U]) && dfn[v] <= dfn[U] + sz[U] - 1) {           cnt++;         }       }       if (cnt >= 1) {         V = u;         return;       }     }   }   int main() {     cin >> n >> m;     int u, v;     for (int i = 1; i <= m; i++) {       scanf( %d%d , &u, &v);       add(u, v);       add(v, u);     }     for (int i = 1; i <= n; i++) {       if (!dfn[i]) dfs(i);     }     if (!U) {       puts( NO );     } else {       puts( YES );       get_V();       for (int i = 1; i <= 3; i++) {         bfs(U, i);         printf( %d  , tp);         while (tp) {           printf( %d  , zh[tp]);           tp--;         }         puts(  );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b, long long &x, long long &y) {     if (b == 0) {       x = 1;       y = 0;       return a;     }     long long x1, y1;     long long d = gcd(b, a % b, x1, y1);     x = y1;     y = x1 - y1 * (a / b);     return d;   }   bool find_any_solution(long long a, long long b, long long c, long long &x0,                          long long &y0, long long &g) {     g = gcd(abs(a), abs(b), x0, y0);     if (c % g) {       return false;     }     x0 *= c / g;     y0 *= c / g;     if (a < 0) x0 = -x0;     if (b < 0) y0 = -y0;     return true;   }   int32_t main() {     ios_base ::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long a, b, c, x, y, g;     cin >> a >> b >> c;     if (!find_any_solution(a, b, -c, x, y, g)) {       cout << -1 << endl;     } else {       cout << x <<     << y << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   long long int a, b, x1, y, x2, y2;   int main() {     cin >> a >> b >> x1 >> y >> x2 >> y2;     long long int da;     long long int k1 = x1 + y;     long long int k2 = x2 + y2;     long long int mi = min(k1, k2);     long long int ma = max(k1, k2);     long long int c = mi % (2 * a);     if (c < 0) c += 2 * a;     if (c != 0) mi += 2 * a - c;     c = ma % (2 * a);     if (c < 0) c += 2 * a;     ma -= c;     if (mi > ma)       da = 0;     else       da = (ma - mi) / (2 * a) + 1;     long long int db;     long long int t1 = x1 - y;     long long int t2 = x2 - y2;     mi = min(t1, t2);     ma = max(t1, t2);     c = mi % (2 * b);     if (c < 0) c += 2 * b;     if (c != 0) mi += 2 * b - c;     c = ma % (2 * b);     if (c < 0) c += 2 * b;     ma -= c;     if (mi > ma)       db = 0;     else       db = (ma - mi) / (2 * b) + 1;     cout << max(da, db);   }


#include <bits/stdc++.h>   using namespace std;   long long m;   int matrix[2][2] = {{3, 1}, {0, 1}};   class Matrix {    public:     long long c[2][2];     Matrix() {       for (int i = 0; i < 2; i++)         for (int j = 0; j < 2; j++) c[i][j] = 0;     }     void deal() {       for (int i = 0; i < 2; i++)         for (int j = 0; j < 2; j++) c[i][j] = matrix[i][j];     }   };   Matrix mul(const Matrix& t1, const Matrix& t2) {     int i, j, k;     Matrix t3;     for (i = 0; i < 2; i++) {       for (j = 0; j < 2; j++) {         for (k = 0; k < 2; k++) {           t3.c[i][j] = (t3.c[i][j] + t1.c[i][k] * t2.c[k][j]) % m;         }       }     }     return t3;   }   Matrix power(long long n) {     Matrix a, b;     b.deal();     if (n == 1)       return b;     else if (n % 2 == 1) {       a = (power(n / 2));       return mul(mul(a, a), b);     } else {       a = (power(n / 2));       return mul(a, a);     }   }   int main() {     long long n;     while (cin >> n >> m) {       if (n == 1) {         cout << 2 % m << endl;         continue;       } else {         Matrix s = power(n - 1);         cout << (s.c[0][0] * 2 + s.c[0][1] * 2) % m << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long double pi = acos(-1);   void boos() {     ios_base ::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);   }   void file() {     freopen( array.in ,  r , stdin);     freopen( array.out ,  w , stdout);   }   const long long INF = 1e9 + 7;   const int INF1 = 998244353;   const long long LLINF = (long long)1e18;   const int INTmx = (int)1e9;   const int N = 2e3 + 1;   int main() {     boos();     string second;     cin >> second;     int kol = 0;     for (int i = 0; i < second.size(); i++) {       if (second[i] ==  a ) kol++;     }     int n = second.size() - kol;     if (n % 2) {       cout <<  :( ;       return 0;     }     int m = n / 2;     int gg = m + kol;     string a, b;     for (int i = 0; i < gg; i++) {       a += second[i];       if (second[i] !=  a ) b += second[i];     }     if (a + b != second) {       cout <<  :( ;       return 0;     }     cout << a;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   int main() {     scanf( %d%d , &n, &m);     if (m == 3) {       if (n == 4) {         printf( 0 0 n3 0 n0 3 n1 1 n );         return 0;       }       if (n >= 5) {         printf( -1 n );         return 0;       }     }     for (int i = 0; i < m; i++) printf( %d %d n , i, 100000 + i * i);     for (int i = 0; i < n - m; i++) printf( %d %d n , i, -100000 - i * i);     return 0;   }


#include <iostream>   #include <algorithm>   using namespace std;      int INF = 2e9;      int pre[200001];   struct node{       int l, r, max, min;   } segtree[800001];      void build(int ind, int l, int r){       if (l == r){           segtree[ind] = (node){l, r, pre[l], pre[l]};       } else {           build(2*ind, l, (l+r)/2);           build(2*ind+1, (l+r)/2+1, r);           segtree[ind] = (node){l, r, max(segtree[2*ind].max, segtree[2*ind+1].max),                min(segtree[2*ind].min, segtree[2*ind+1].min)};       }   }      int find_min(int ind, int l, int r){       if (segtree[ind].l >= l && segtree[ind].r <= r) return segtree[ind].min;       if (segtree[ind].r < l || segtree[ind].l > r) return INF;       return min(find_min(ind*2, l, r), find_min(ind*2+1,l,r));   }         int find_max(int ind, int l, int r){       if (segtree[ind].l >= l && segtree[ind].r <= r) return segtree[ind].max;       if (segtree[ind].r < l || segtree[ind].l > r) return -INF;       return max(find_max(ind*2, l, r), find_max(ind*2+1,l,r));   }   int main(){       int t; cin >> t;       for (int i=0;i<t;i++){           int n,m; string s; cin >> n >> m; cin >> s;           pre[0] = 0;           for (int j=0;j<n;j++){               if (s[j] ==  + ) pre[j+1] = pre[j] + 1; else pre[j+1] = pre[j] - 1;           }           build(1, 0, n);           for (int j=0;j<m;j++){               int l,r; cin >> l >> r;               int M = find_max(1, 0, l-1), m = find_min(1, 0, l-1);               //cout << M <<     << m << endl;               int subsum = pre[r] - pre[l-1];               M = max(M, find_max(1, r+1, n) - subsum);               m = min(m, find_min(1, r+1, n) - subsum);               cout << M-m+1 << endl;           }                  }                }


#include <bits/stdc++.h>   using namespace std;   char b[9], s[9];   struct tm t;   int k, i;   string m(int i) {     if (i == 8) return  September ;     t.tm_mon = i;     strftime(b, 9,  %B , &t);     return b;   }   int main() {     cin >> s >> k;     for (;; i++)       if (m(i) == s) return cout << m((i + k) % 12), 0;   }


#include <bits/stdc++.h>   int main() {     char ai[105];     while (~scanf( %s , ai)) {       int t = 1, max = 1;       for (int i = 0; ai[i] !=   0 ; i++) {         if (ai[i] !=  A  && ai[i] !=  E  && ai[i] !=  I  && ai[i] !=  O  &&             ai[i] !=  U  && ai[i] !=  Y ) {           t++;           max = max > t ? max : t;         } else           t = 1;       }       printf( %d n , max);     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 200010;   int mx[maxn << 2], ls[maxn << 2], rs[maxn << 2], lp[maxn << 2], rp[maxn << 2];   bool col[maxn << 2];   void make(int rt, int l, int r) {     lp[rt] = l;     rp[rt] = r;     ls[rt] = rs[rt] = r - l + 1;     mx[rt] = (ls[rt] - 1) / 2;     col[rt] = true;   }   void build(int l, int r, int rt) {     make(rt, l, r);     if (l == r) return;     int m = l + r >> 1;     build(l, m, rt << 1);     build(m + 1, r, rt << 1 | 1);   }   void pushup(int rt, int l, int r) {     int L = rt << 1, R = L + 1, m = l + r >> 1;     mx[rt] = mx[L], lp[rt] = lp[L], rp[rt] = rp[L], col[rt] = col[L];     if (!col[rt] || (rs[L] + ls[R] - 1) / 2 > mx[rt]) {       mx[rt] = rs[L] + ls[R] - 1 >> 1;       lp[rt] = m - rs[L] + 1;       rp[rt] = m + ls[R];       col[rt] = (lp[rt] <= rp[rt]);     }     if (!col[rt] || mx[R] > mx[rt]) {       mx[rt] = mx[R];       lp[rt] = lp[R];       rp[rt] = rp[R];       col[rt] = col[R];     }     ls[rt] = ls[L] + (ls[L] == m - l + 1) * ls[R];     rs[rt] = rs[R] + (rs[R] == r - m) * rs[L];   }   int n, m;   void update(int p, int l = 0, int r = n - 1, int rt = 1) {     if (l == r) {       col[rt] ^= 1;       if (col[rt])         make(rt, l, r);       else         ls[rt] = rs[rt] = 0;       mx[rt] = -1;       return;     }     int m = l + r >> 1;     if (p <= m)       update(p, l, m, rt << 1);     else       update(p, m + 1, r, rt << 1 | 1);     pushup(rt, l, r);   }   int pos[1000010];   int main() {     scanf( %d%d , &n, &m);     build(0, n - 1, 1);     int op, id;     while (m--) {       scanf( %d%d , &op, &id);       if (op == 2) {         update(pos[id]);       } else {         int a = ls[1] - 1, b = rs[1] - 1, c = mx[1];         if (a >= b && a >= c) {           printf( 1 n );           pos[id] = 0;           update(0);         } else if (c >= b) {           int t = (lp[1] + rp[1]) / 2;           pos[id] = t;           update(t);           printf( %d n , t + 1);         } else {           printf( %d n , n);           pos[id] = n - 1;           update(n - 1);         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   ifstream in;   ofstream out;   const long long kk = 1000;   const long long ml = kk * kk;   const long long mod = ml * kk + 7;   const long long inf = ml * ml * ml + 7;   long long n, i, j;   vector<long long> m;   vector<pair<long long, long long> > x;   bool viv = false;   map<pair<long long, long long>, bool> was;   pair<long long, string> ans;   string ll_to_str(long long v) {     string s =   ;     if (v == 0) return  0 ;     bool obr = false;     if (v < 0) obr = true;     v = abs(v);     while (v > 0) {       short e = v % 10;       s += char( 0  + e);       v /= 10;     }     reverse(s.begin(), s.end());     if (obr) s =  -  + s;     return s;   }   void write(pair<long long, long long> a) {     if (was[a]) return;     was[a] = true;     ans.first++;     ans.second += ll_to_str(a.first);     ans.second +=    ;     ans.second += ll_to_str(a.second);     ans.second +=   n ;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     x.assign(3, {0, 0});     for (long long i = 0; i < 3; i++) cin >> x[i].first >> x[i].second;     sort(x.begin(), x.end());     pair<long long, long long> cur = x[0];     while (cur.first < x[1].first) {       write(cur);       cur.first++;     }     while (cur.second < x[1].second) {       write(cur);       cur.second++;     }     while (cur.second > x[1].second) {       write(cur);       cur.second--;     }     while (cur.second < x[2].second) {       write(cur);       cur.second++;     }     while (cur.second > x[2].second) {       write(cur);       cur.second--;     }     while (cur.first < x[2].first) {       write(cur);       cur.first++;     }     write(cur);     cout << ans.first << endl;     cout << ans.second << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long x;     while (cin >> x) {       long long c = x / 2, t = 0;       if (x % 2 == 1) c--, t = 1;       cout << c + t << endl;       for (int i = 0; i < c; i++) cout <<  2  ;       for (int i = 0; i < t; i++) cout <<  3  ;       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MX = 6e4, C = 10, L = 20;   long long nxt[MX][C], sz;   bitset<170> b[MX];   unordered_map<bitset<170>, long long> id;   inline long long low_bit(bitset<170> s) {     for (long long i = 0; i < 10; i++)       if (s.test(i)) return i;     return -1;   }   inline long long extend(bitset<170> s) {     if (id.count(s)) return id[s];     id[s] = sz;     b[sz] = s;     return sz++;   }   void find_states() {     bitset<170> s;     s.reset();     s.set(0);     extend(s);     for (long long i = 0; i < sz; i++) {       for (long long j = 0; j < C; j++) {         bitset<170> s = (b[i] << j | b[i] >> j);         for (long long k = 0; k <= j; k++)           if (b[i].test(k)) s.set(j - k);         nxt[i][j] = extend(s);       }     }   }   long long dp[L][MX][C];   void calc_dps() {     for (long long state = 0; state < sz; state++)       for (long long k = 0; k < C; k++)         dp[0][state][k] = (low_bit(b[state]) <= k);     for (long long len = 1; len < L; len++) {       for (long long state = 0; state < sz; state++) {         for (long long k = 0; k < C; k++) {           for (long long c = 0; c < C; c++) {             dp[len][state][k] += dp[len - 1][nxt[state][c]][k];           }         }       }     }   }   long long q;   long long digit[L];   inline long long get(long long lim, long long k) {     long long cp = lim;     for (long long i = 0; i < L; i++, cp /= 10) {       digit[i] = cp % 10;     }     long long res = 0;     long long state = 0;     for (long long i = L - 1; i >= 0; i--) {       for (long long j = 0; j < digit[i]; j++) {         res += dp[i][nxt[state][j]][k];       }       state = nxt[state][digit[i]];     }     return res;   }   int32_t main() {     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);     find_states();     calc_dps();     cin >> q;     while (q--) {       long long l, r, k;       cin >> l >> r >> k;       r++;       cout << get(r, k) - get(l, k) <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 202020, INF = 0x3f3f3f3f;   vector<int> G[MAXN], iG[MAXN];   int n, m, k, p[MAXN];   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; ++i) {       static int u, v;       scanf( %d%d , &u, &v);       G[u].push_back(v);       iG[v].push_back(u);     }     scanf( %d , &k);     for (int i = 1; i <= k; ++i) scanf( %d , &p[i]);     vector<int> d(MAXN, INF);     queue<int> q;     q.push(p[k]), d[p[k]] = 0;     while (!q.empty()) {       int f = q.front();       q.pop();       for (auto g : iG[f])         if (d[g] == INF) {           d[g] = d[f] + 1;           q.push(g);         }     }     int mn = 0, mx = 0;     for (int i = 1; i < k; ++i) {       if (d[p[i]] <= d[p[i + 1]])         mn++, mx++;       else {         int deg = 0;         for (auto g : G[p[i]])           if (d[g] == d[p[i]] - 1) deg++;         mx += deg > 1;       }     }     printf( %d %d n , mn, mx);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long double INF = 1e20;   vector<pair<long double, long double> > vec;   struct Line {     long double m, c;     Line(long double x, long double y) {       m = x;       c = y;     }     long double Get(long double x) { return m * x + c; }     bool operator<(const Line &other) const { return m < other.m; }   };   bool Bad(Line &P, Line &C, Line &N) {     return (P.c - C.c) * 1.0L * (N.m - P.m) > (P.c - N.c) * 1.0L * (C.m - P.m);   }   namespace ConvexHull {   vector<Line> hull;   void Add(long double m, long double c) {     if (hull.size() > 0 && hull.back().m == m) {       if (hull.back().c > c)         hull.pop_back();       else         return;     }     hull.push_back(Line(m, c));     int sz = hull.size();     while (sz > 2 && Bad(hull[sz - 3], hull[sz - 2], hull[sz - 1])) {       swap(hull[sz - 2], hull[sz - 1]);       hull.pop_back();       sz--;     }   }   };  // namespace ConvexHull   using namespace ConvexHull;   const int MAX = 3;   struct Matrix {     int row, col;     long double m[MAX][MAX];     Matrix() { memset(m, 0, sizeof(m)); }     void Set(int r, int c) {       row = r;       col = c;     }     Matrix(int r, int c) {       memset(m, 0, sizeof(m));       Set(r, c);     }   };   Matrix Multiply(Matrix A, Matrix B) {     Matrix ans(A.row, B.col);     for (int i = 0; i < A.row; i++) {       for (int j = 0; j < B.col; j++) {         ans.m[i][j] = 0;         for (int k = 0; k < A.col; k++) {           ans.m[i][j] += (A.m[i][k] * B.m[k][j]);         }       }     }     return ans;   }   const int LOG = 35;   Matrix pwr[LOG];   void preProcess(int idx, long double M) {     pwr[0].Set(3, 3);     pwr[0].m[0][0] = 1;     pwr[0].m[0][1] = -hull[idx].m;     pwr[0].m[0][2] = -hull[idx].c;     pwr[0].m[1][0] = 0;     pwr[0].m[1][1] = 1 + hull[idx].m;     pwr[0].m[1][2] = M + hull[idx].c;     pwr[0].m[2][0] = 0;     pwr[0].m[2][1] = 0;     pwr[0].m[2][2] = 1;     for (int i = 1; i < LOG; i++) pwr[i] = Multiply(pwr[i - 1], pwr[i - 1]);   }   pair<long long int, long double> jump(long long int cur, int id, long double x,                                         long double ans, long long int t) {     for (int j = LOG - 1; j >= 0; j--) {       if (cur + (1LL << j) > t) continue;       long double nx = pwr[j].m[1][1] * x + pwr[j].m[1][2];       if (id == hull.size() - 1 ||           hull[id].c - hull[id + 1].c <= (hull[id + 1].m - hull[id].m) * nx) {         cur += (1LL << j);         ans = pwr[j].m[0][0] * ans + pwr[j].m[0][1] * x + pwr[j].m[0][2];         x = nx;       }     }     return {cur, ans};   }   int main() {     int n;     long long int t;     scanf( %d %lld , &n, &t);     long double M = 0;     int a, b;     double p;     for (int i = 1; i <= n; i++) {       scanf( %d %d %lf , &a, &b, &p);       vec.push_back({p, a});       M = max(M, b * 1.0L * p);     }     sort(vec.begin(), vec.end());     for (auto v : vec) Add(-v.first, -v.first * v.second);     int id = 0;     long double ans = 0;     int sz = hull.size();     for (long long int i = 1; i <= t; i++) {       long double x = (i - 1) * M - ans;       while (id + 1 < sz &&              hull[id].c - hull[id + 1].c > (hull[id + 1].m - hull[id].m) * x)         id++;       ans -= hull[id].Get(x);       x = i * M - ans;       preProcess(id, M);       auto ret = jump(i, id, x, ans, t);       i = ret.first;       ans = ret.second;     }     double out = ans;     printf( %.12f , out);   }


#include <bits/stdc++.h>   using namespace std;   template <class T, class U>   void ckmin(T &a, U b) {     if (a > b) a = b;   }   template <class T, class U>   void ckmax(T &a, U b) {     if (a < b) a = b;   }   const int MAXN = 52;   const int MAXM = 113;   const int MAXT = 20013;   const int INF = 1000000007;   const long double EPS = 1e-9;   const long double PI = 3.141592653589793;   vector<complex<long double> > roots;   void gen(int deg) {     roots.resize(deg);     roots[0] = complex<long double>(1, 0);     roots[1] = complex<long double>(cos(2.0 * PI / deg), sin(2.0 * PI / deg));     for (auto i = (2); i < (deg); i++) {       roots[i] = roots[i - 1] * roots[1];     }     return;   }   void fft(vector<complex<long double> > &f) {     int n = ((int)(f).size());     vector<complex<long double> > g(n);     for (int i = n >> 1; i; i >>= 1) {       for (int j = 0; j < n; j += i) {         int t = (j << 1) - (((j << 1) >= n) ? n : 0);         for (auto k = (0); k < (i); k++) {           g[j + k] = f[t + k] + roots[j] * f[t + k + i];         }       }       swap(f, g);     }   }   vector<long double> conv(vector<long double> p, vector<long double> q) {     int siz = ((int)(p).size()) + ((int)(q).size()) - 1,         siz1 = (1 << (32 - __builtin_clz(siz)));     vector<long double> res(siz);     if (((int)(p).size()) <= 100 || ((int)(q).size()) <= 100) {       for (auto i = (0); i < (((int)(p).size())); i++) {         for (auto j = (0); j < (((int)(q).size())); j++) {           res[i + j] += p[i] * q[j];         }       }       return res;     }     vector<complex<long double> > p1(((int)(p).size())), q1(((int)(q).size()));     for (auto i = (0); i < (((int)(p).size())); i++)       p1[i] = complex<long double>(p[i], 0);     for (auto i = (0); i < (((int)(q).size())); i++)       q1[i] = complex<long double>(q[i], 0);     gen(siz1);     p1.resize(siz1);     q1.resize(siz1);     fft(p1);     fft(q1);     for (auto i = (0); i < (siz1); i++) p1[i] *= q1[i];     fft(p1);     for (auto i = (0); i < (siz1); i++) p1[i] /= siz1;     reverse(p1.begin() + 1, p1.end());     for (auto i = (0); i < (siz); i++) {       res[i] = real(p1[i]);     }     return res;   }   int N, M, T, X;   vector<int> edge[MAXN];   int to[MAXM], cost[MAXM];   vector<long double> prob[MAXM], dp[MAXN];   int path[MAXN];   priority_queue<pair<int, int>, vector<pair<int, int> >,                  greater<pair<int, int> > >       pq1;   queue<int> q;   bitset<MAXN> active;   vector<long double> comb(vector<long double> a, vector<long double> b) {     for (auto i = (0); i < (T - 1); i++) b.push_back(b[T]);     reverse((a).begin(), (a).end());     vector<long double> c = conv(a, b);     vector<long double> res(T);     for (auto i = (0); i < (T); i++) {       res[i] = c[T - 1 + i];     }     return res;   }   int32_t main() {     cout << fixed << setprecision(12);     cerr << fixed << setprecision(4);     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> N >> M >> T >> X;     T++;     for (auto i = (0); i < (M); i++) {       int u, v, c;       cin >> u >> v >> c;       u--;       v--;       edge[v].push_back(i);       to[i] = u;       cost[i] = c;       prob[i].resize(T);       for (auto j = (1); j < (T); j++) {         int x;         cin >> x;         prob[i][j] = 0.00001 * x;       }     }     fill(path, path + N, INF);     path[N - 1] = 0;     pq1.push({0, N - 1});     while (!pq1.empty()) {       int d = pq1.top().first, u = pq1.top().second;       pq1.pop();       if (d != path[u]) {         continue;       }       for (int e : edge[u]) {         int v = to[e];         if (path[v] > d + cost[e]) {           path[v] = d + cost[e];           pq1.push({path[v], v});         }       }     }     for (auto i = (0); i < (N - 1); i++) {       dp[i].resize(T + 1);       fill((dp[i]).begin(), (dp[i]).end(), INF);       dp[i][T] = path[i] + X;     }     dp[N - 1].resize(T + 1);     fill((dp[N - 1]).begin(), (dp[N - 1]).end(), 0);     dp[N - 1][T] = X;     q.push(N - 1);     active[N - 1] = true;     while (!q.empty()) {       int u = q.front();       q.pop();       if (!active[u]) continue;       active[u] = false;       for (int e : edge[u]) {         int v = to[e];         vector<long double> res = comb(prob[e], dp[u]);         bool ins = false;         for (auto j = (0); j < (T); j++) {           res[j] += cost[e];           if (dp[v][j] > res[j] + EPS) {             dp[v][j] = res[j];             ins = true;           }         }         if (ins && !active[v]) {           active[v] = true;           q.push(v);         }       }     }     cout << dp[0][0] <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int dx[9] = {-1, 0, 1, 0, -1, -1, 1, 1, 0};   const int dy[9] = {0, 1, 0, -1, -1, 1, -1, 1, 0};   long long n;   void solve() {     int a = sqrt(n);     int b = cbrt(n);     int c = sqrt(cbrt(n));     cout << a + b - c <<   n ;   }   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int T;     cin >> T;     while (T--) {       cin >> n;       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = (int)2e5 + 5;   const int MOD = 998244353;   int bit[MAXN];   bool got[MAXN];   int p[MAXN];   void gcd(long long a, long long b, long long& x, long long& y) {     if (b == 0) {       x = 1;       y = 0;       return;     }     gcd(b, a % b, y, x);     y -= (a / b) * x;     return;   }   long long inv(long long a) {     long long x, y;     gcd(a, MOD, x, y);     return (x % MOD + MOD) % MOD;   }   void add(int idx, int n) {     while (idx <= n) {       bit[idx]++;       idx += idx & -idx;     }     return;   }   int query(int idx) {     int sum = 0;     while (idx > 0) {       sum += bit[idx];       idx -= idx & -idx;     }     return sum;   }   void init(int n) {     for (int i = 1; i <= n; i++) {       bit[i] = 0;     }     return;   }   long long calc(int n) {     long long ans = 0;     init(n);     for (int i = n; i >= 1; i--) {       if (p[i] != -1) {         ans += query(p[i]);         add(p[i], n);       }     }     return ans % MOD;   }   long long expected(int n) {     long long ans = (1LL * n * (n - 1)) % MOD;     ans *= inv(4);     return ans % MOD;   }   bool compatible(vector<int> v, int n) {     for (int i = 1; i <= n; i++) {       if (p[i] != -1 && v[i - 1] != p[i]) {         return false;       }     }     return true;   }   long long get_inversions(vector<int> v, int n) {     init(n);     long long ans = 0;     for (int i = n - 1; i >= 0; i--) {       ans += query(v[i]);       add(v[i], n);     }     return ans % MOD;   }   long long brute(int n) {     vector<int> v(n);     for (int i = 0; i < n; i++) {       v[i] = i + 1;     }     long long ans = 0;     long long perm = 0;     do {       if (compatible(v, n)) {         ans += get_inversions(v, n);         perm++;       }     } while (next_permutation(v.begin(), v.end()));     printf( here ans = %lld, perm = %lld, inv = %lld n , ans, perm, inv(perm));     return (1LL * ans * inv(perm)) % MOD;   }   int main(void) {     int n;     int vacant = 0;     int start_vacant = 0;     scanf(  %d , &n);     for (int i = 1; i <= n; i++) {       scanf(  %d , &p[i]);       got[p[i]] = true;       if (p[i] == -1) {         vacant++;       }     }     init(n);     start_vacant = vacant;     for (int i = 1; i <= n; i++) {       if (!got[i]) {         add(i, n);       }     }     long long ans = 0;     int left = 0;     for (int i = 1; i <= n; i++) {       if (p[i] != -1) {         long long aux = 1LL * query(p[i]) * vacant;         aux %= MOD;         ans += aux * inv(start_vacant);         ans %= MOD;         aux = 1LL * (query(n) - query(p[i])) * left;         aux %= MOD;         ans += aux * inv(start_vacant);         ans %= MOD;       } else {         left++;         vacant--;       }     }     ans += expected(start_vacant);     ans += calc(n);     ans %= MOD;     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char a[15][15], b[15][15], t[15][15];   int n;   void copy() {     for (int i = 0; i < n; ++i)       for (int j = 0; j < n; ++j) t[i][j] = a[i][j];   }   int check() {     for (int i = 0; i < n; ++i)       for (int j = 0; j < n; ++j)         if (a[i][j] != b[i][j]) return 0;     return 1;   }   void Upd() {     copy();     for (int i = 0; i < n; ++i)       for (int j = 0; j < n; ++j) a[i][j] = t[n - j - 1][i];   }   void put() {     for (int i = 0; i < n; ++i, puts(  )) printf( %s , a[i]);   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; ++i) scanf( %s , a[i]);     for (int i = 0; i < n; ++i) scanf( %s , b[i]);     for (int i = 0; i < 4; ++i) {       if (check()) return puts( Yes ), 0;       Upd();     }     copy();     for (int i = 0; i < n; ++i)       for (int j = 0; j < n; ++j) a[i][j] = t[n - i - 1][j];     for (int i = 0; i < 4; ++i) {       if (check()) return puts( Yes ), 0;       Upd();     }     copy();     for (int i = 0; i < n; ++i)       for (int j = 0; j < n; ++j) a[i][j] = t[i][n - j - 1];     for (int i = 0; i < 4; ++i) {       if (check()) return puts( Yes ), 0;       Upd();     }     return puts( No ), 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, cnt;   bool f;   vector<int> p, m, z;   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       int x;       scanf( %d , &x);       if (x < 0) m.push_back(x);       if (x > 0) p.push_back(x);       if (x == 0) z.push_back(x);     }     printf( 1 %d n , m[0]);     if (p.size() > 0)       printf( 1 %d  n , p[0]);     else {       printf( 2 %d %d n , m[1], m[2]);       f = true;     }     cnt = z.size();     for (int i = (f ? 0 : 1); i < p.size(); i++) cnt++;     for (int i = (f ? 3 : 1); i < m.size(); i++) cnt++;     printf( %d  , cnt);     for (int i = 0; i < z.size(); i++) printf( %d  , z[0]);     for (int i = (f ? 0 : 1); i < p.size(); i++) printf( %d  , p[i]);     for (int i = (f ? 3 : 1); i < m.size(); i++) printf( %d  , m[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf_int = 2e9;   const long long inf_ll = 2e18;   int Gcd(int p, int q) { return q == 0 ? p : Gcd(q, p % q); }   int Pow(int p, int q) {     int ans = 1;     while (q) {       if (q & 1) ans = ans * p;       p = p * p;       q >>= 1;     }     return ans;   }   inline int read() {     int ra, fh;     char rx;     rx = getchar(), ra = 0, fh = 1;     while ((rx <  0  || rx >  9 ) && rx !=  - ) rx = getchar();     if (rx ==  - ) fh = -1, rx = getchar();     while (rx >=  0  && rx <=  9 ) ra *= 10, ra += rx - 48, rx = getchar();     return ra * fh;   }   int n, q;   vector<int> v[300010];   vector<bool> vis;   int op, x;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     scanf( %d%d , &n, &q);     int ans = 0, k = 0;     for (int i = 1; i <= q; i++) {       scanf( %d%d , &op, &x);       if (op == 1) {         x--;         v[x].push_back(vis.size());         vis.push_back(true);         ans++;       } else if (op == 2) {         x--;         for (int j : v[x]) {           if (vis[j]) {             ans--;             vis[j] = false;           }         }         v[x].clear();       } else {         for (int j = k; j < x; j++) {           if (vis[j]) {             vis[j] = false;             ans--;           }         }         k = max(k, x);       }       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, b;     scanf( %d , &n);     int a = sqrt(n);     if (n % a == 0)       b = n / a;     else       b = n / a + 1;     printf( %d , (a + b) * 2);   }


#include <bits/stdc++.h>   using namespace std;   long long tu[2000001], tu1[2000001];   int main() {     long long n, m;     cin >> n;     long long q;     map<long long, long long> wp;     for (long long a = 1; a <= n; a++) {       scanf( %I64d , &q);       wp[q]++;     }     cin >> m;     for (long long a = 1; a <= m; a++) scanf( %I64d , &tu[a]);     for (long long a = 1; a <= m; a++) scanf( %I64d , &tu1[a]);     long long gx = -1, my = -1;     long long zuobiao = -1;     for (long long a = 1; a <= m; a++) {       if (wp[tu[a]] > gx || wp[tu[a]] == gx && wp[tu1[a]] >= my) {         gx = wp[tu[a]];         zuobiao = a;         my = wp[tu1[a]];       }     }     cout << zuobiao << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long t, n, i;     cin >> t;     while (t--) {       cin >> n;       if (n == 1) {         cout <<  -1 n ;         continue;       }       cout <<  2 ;       for (i = 2; i <= n; i++) {         cout <<  9 ;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   const long long INF = 1e9 + 7;   const long long maxn = 2e5 + 7;   using namespace std;   void solve() {     long long a, b, c, d;     cin >> a >> b >> c >> d;     long long ans = 0;     for (long long z = c; z < d + 1; z++) {       long long r1 = z - c + 1;       long long r2 = z - b + 1;       if (r1 > b) continue;       long long sStart = 1;       if (r2 < b) {         sStart += (b - r2);       }       r2 = max(r2, b);       long long numTerms = c - r2 + 1;       long long whichTermGreater = b - a + 2 - sStart;       long long numGreater = max(min(numTerms - whichTermGreater, numTerms), 0LL);       ans += numGreater * (b - a + 1);       numTerms -= numGreater;       long long finalTerm = sStart + numTerms - 1;       long long summn = (numTerms) * (sStart + finalTerm);       summn /= 2;       ans += summn;     }     cout << ans <<   n ;   }   int main(int argc, char** argv) {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int T = 1;     while (T--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<int, int> m;   int main() {     int long long n;     cin >> n;     int long long arr[n];     for (int i = 0; i < n; i++) {       cin >> arr[i];     }     sort(arr, arr + n);     int long long a = 0, b = 0;     if (arr[0] == arr[n - 1]) {       cout << 0 <<     << n * (n - 1) / 2 << endl;     } else {       for (int i = 0; i < n; i++) {         if (arr[i] == arr[0]) a++;         if (arr[i] == arr[n - 1]) b++;       }       cout << arr[n - 1] - arr[0] <<     << a * b << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   vector<long long int> xi;   vector<long long int> hi;   int main() {     long long int n, x, h, ans = 1, temp, temp1, curr, i;     scanf( %lld , &n);     for (i = 0; i < n; i++) {       scanf( %lld %lld , &x, &h);       xi.push_back(x);       hi.push_back(h);     }     curr = xi[0];     for (i = 1; i < n; i++) {       temp = xi[i] - hi[i];       temp1 = xi[i] + hi[i];       if (temp > curr) {         ans++;         curr = xi[i];       } else {         if (i + 1 < n && temp1 < xi[i + 1]) {           ans++;           curr = temp1;         } else {           if (i + 1 == n) ans++;           curr = xi[i];         }       }     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct single_node {     long long total;     long long prefix;     long long suffix;   };   vector<pair<long long, pair<int, int> > > v;   set<int> bounds;   long long a[300000 + 50];   long long d[300000 + 50];   long long c[300000 + 50];   long long lazy[1200000 + 50];   long long maxseg[1200000 + 50];   long long totseg[1200000 + 50];   long long maxpref[1200000 + 50];   long long maxsuff[1200000 + 50];   void build(int node, int l, int r) {     if (l == r) {       maxseg[node] = a[l];       maxpref[node] = a[l];       maxsuff[node] = a[l];       totseg[node] = a[l];       return;     }     int mid = (l + r) / 2;     build(2 * node + 1, l, mid);     build(2 * node + 2, mid + 1, r);     totseg[node] = totseg[2 * node + 1] + totseg[2 * node + 2];     maxseg[node] = max(maxsuff[2 * node + 1] + maxpref[2 * node + 2],                        max(maxseg[2 * node + 1], maxseg[2 * node + 2]));     maxpref[node] =         max(maxpref[2 * node + 1], totseg[2 * node + 1] + maxpref[2 * node + 2]);     maxsuff[node] =         max(maxsuff[2 * node + 2], totseg[2 * node + 2] + maxsuff[2 * node + 1]);     return;   }   single_node query(int node, int l, int r, int ql, int qr) {     if (l == r) {       if (ql <= l && qr >= r) {         single_node bc;         bc.total = totseg[node];         bc.prefix = maxpref[node];         bc.suffix = maxsuff[node];         return bc;       } else {         single_node bc;         bc.total = 0;         bc.prefix = 0;         bc.suffix = 0;         return bc;       }     }     if (ql <= l && qr >= r) {       single_node bc;       bc.total = totseg[node];       bc.prefix = maxpref[node];       bc.suffix = maxsuff[node];       return bc;     } else if (ql > r || qr < l) {       single_node bc;       bc.total = 0;       bc.prefix = 0;       bc.suffix = 0;       return bc;     }     int mid = (l + r) / 2;     single_node le = query(2 * node + 1, l, mid, ql, qr);     single_node ri = query(2 * node + 2, mid + 1, r, ql, qr);     single_node res;     res.total = le.total + ri.total;     res.prefix = max(le.prefix, le.total + ri.prefix);     res.suffix = max(ri.suffix, ri.total + le.suffix);     return res;   }   int main() {     ios::sync_with_stdio(false);     int n;     long long aa;     cin >> n >> aa;     long long ans = 0;     for (int i = 0; i < n; i++) {       cin >> d[i] >> c[i];       a[i] = aa - c[i];       ans = max(ans, a[i]);       if (i > 0) {         v.push_back(make_pair(d[i] - d[i - 1], make_pair(i - 1, i)));       }     }     build(0, 0, n - 1);     sort(v.begin(), v.end());     bounds.insert(0);     bounds.insert(n - 1);     for (int i = v.size() - 1; i >= 0; i--) {       int leend, ribeg;       leend = v[i].second.first;       ribeg = v[i].second.second;       set<int>::iterator iter1, iter2;       iter1 = bounds.lower_bound(ribeg);       iter2 = bounds.upper_bound(leend);       --iter2;       int lebeg, riend;       lebeg = (*iter2);       riend = (*iter1);       ans = max(ans, query(0, 0, n - 1, lebeg, leend).suffix +                          query(0, 0, n - 1, ribeg, riend).prefix -                          (v[i].first * v[i].first));       bounds.insert(leend);       bounds.insert(ribeg);     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void preKmp(char *src, int m, int *rule) {     int i = 0, j = -1;     rule[0] = j;     while (i < m) {       while (j != -1 && src[j] != src[i]) j = rule[j];       i++, j++;       if (j >= m)         rule[i] = rule[j - 1];       else         rule[i] = j;     }   }   int recount = 0;   void kmp(char *src, int m, char *dest, int n) {     int rule[10005];     preKmp(src, m, rule);     int i = 0, j = 0;     while (i < n) {       while (j != -1 && src[j] != dest[i]) {         j = rule[j];       }       i++, j++;       if (j == m) {         recount++;         j = rule[j];       }     }   }   char Reg[10005], ori[1000005];   char os[100][300], a[500], temp[500];   int l[100], r[100], n, len, le[100];   int main() {     set<string> now;     set<string>::iterator iter;     bool flag;     int ans = 0, i, j, k;     while (scanf( %s , a) != EOF) {       now.clear();       scanf( %d , &n);       for (i = 1; i <= n; i++) {         scanf( %s%d%d , os[i], &l[i], &r[i]);         le[i] = strlen(os[i]);       }       len = strlen(a);       ans = 0;       for (i = 0; i < len; i++)         for (j = i; j < len; j++) {           flag = true;           for (k = 1; k <= n; k++) {             recount = 0;             kmp(a + i, j - i + 1, os[k], le[k]);             if (recount < l[k] || recount > r[k]) {               flag = false;               break;             }           }           if (flag) {             for (k = i; k <= j; k++) temp[k - i] = a[k];             temp[k - i] =   0 ;             string wc = temp;             iter = now.find(wc);             if (iter == now.end()) {               now.insert(wc);               ans++;             }           }         }       printf( %d n , ans);     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e6 + 10;   int n, a[N], vis[N];   int main() {     scanf( %d , &n);     int64_t ans = 0;     int f = 0, g = 1, pos = 0;     for (int i = 1; i <= n; ++i) {       scanf( %d , &a[i]);       ans += abs(a[i] - i);       if (a[i] == 1) continue;       if (a[i] > i)         ++f, ++vis[a[i] - i];       else         ++g, ++vis[a[i] - i + n];     }     int64_t ret = ans;     for (int k = 1; k < n; ++k) {       ret -= abs(n + 1 - a[n - k + 1]);       ret += abs(1 - a[n - k + 1]);       ret += -f + g;       if (a[n - k + 1] != 1) ++f, --g;       f -= vis[k];       g += vis[k];       if (ret < ans) ans = ret, pos = k;     }     printf( %lld %d n , ans, pos);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k, a[1010], maxx = -1, res[1010], ans = 0;     cin >> n >> k;     for (int i = 0; i < n; i++) {       cin >> a[i];       if (res[a[i]] > 0)         res[a[i]]++;       else         res[a[i]] = 1;       maxx = max(maxx, res[a[i]]);     }     maxx = maxx + ((k - (maxx % k)) % k);     for (int i = 1; i < 101; i++) {       if (res[i] > 100 || res[i] < 1) continue;       ans += (maxx - res[i]);     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a, b;     cin >> n >> a >> b;     if (n - a > b + 1) {       cout << b + 1 << endl;     } else {       cout << n - a << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long t;     cin >> t;     while (t--) {       long long a, b, sum = 0;       cin >> a >> b;       while (a > 0 && b > 0) {         if (a < b) swap(a, b);         sum += a / b;         a = a - b * (a / b);       }       cout << sum << endl;     }     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   template <typename T>   std::ostream& operator<<(std::ostream& str, const std::vector<T>& v) {     str <<  [ ;     for (auto n : v) str << n <<  ,  ;     str <<  ] ;     return str;   }   template <typename K, typename V>   std::ostream& operator<<(std::ostream& str, const std::unordered_map<K, V>& m) {     str <<  [ ;     for (auto n : m) str << n.first <<   =>   << n.second <<  ,  ;     str <<  ] ;     return str;   }   void gen(int i, string c, vector<string>& r) {     if (i == 0) {       r.push_back(c);       return;     }     gen(i - 1, c +  4 , r);     gen(i - 1, c +  7 , r);   }   int main() {     cin.sync_with_stdio(false);     cout.sync_with_stdio(false);     int input;     cin >> input;     std::vector<string> all_n;     for (int i = 1; i <= 10; i++) {       gen(i - 1,  4 , all_n);       gen(i - 1,  7 , all_n);     }     stringstream ss;     ss << input;     string inp_string = ss.str();     for (int i = 0; i < all_n.size(); i++) {       if (all_n[i] == inp_string) {         cout << i + 1 << endl;         return 0;       }     }     cout <<  all_n           <<  :   << all_n << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t, n, x, y;     scanf( %d , &t);     while (t--) {       scanf( %d%d%d , &n, &x, &y);       int d, dif = y - x;       for (int i = 1; i <= dif; i++) {         if (dif % i == 0 && (dif / i <= n - 1)) {           d = i;           break;         }       }       printf( %d , y);       n--;       int val = y - d;       while (val > 0 && n > 0) {         n--;         printf(  %d , val);         val -= d;       }       val = y + d;       while (n > 0) {         n--;         printf(  %d , val);         val += d;       }       printf(  n );     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   const long long INF = 1e18 + 5;   const int MAXN = 1e6;   const int base = 317;   const long long bs = 1e5 + 1;   const long long mod = 998244353;   struct node {     node *left, *right;     int val;     node(node* l = NULL, node* r = NULL, int v = 0) {       left = l;       right = r;       val = v;     }   };   node* version[N];   int n, m, d[N], up[21][N];   int tin[N], tout[N], timer;   vector<int> g[N];   void build(node* v, int L, int R) {     if (L == R) return;     int md = (L + R) >> 1;     v->left = new node();     v->right = new node();     build(v->left, L, md);     build(v->right, md + 1, R);   }   node* upd(node* v, int L, int R, int pos, int val) {     if (L == R) {       return new node(NULL, NULL, val);     }     node* nw = new node();     int md = (L + R) >> 1;     if (md >= pos) {       nw->right = v->right;       nw->left = upd(v->left, L, md, pos, val);     } else {       nw->left = v->left;       nw->right = upd(v->right, md + 1, R, pos, val);     }     nw->val = nw->left->val + nw->right->val;     return nw;   }   void dfs(int v, int p = 0) {     d[v] = d[p] + 1;     up[0][v] = p;     tin[v] = ++timer;     for (int i = 1; i <= 20; i++) {       up[i][v] = up[i - 1][up[i - 1][v]];     }     for (int to : g[v]) {       if (to != p) {         dfs(to, v);       }     }     tout[v] = ++timer;   }   int lca(int a, int b) {     if (d[a] > d[b]) {       swap(a, b);     }     for (int i = 20; i >= 0; i--) {       if (d[a] <= d[up[i][b]]) {         b = up[i][b];       }     }     if (a == b) return a;     for (int i = 20; i >= 0; i--) {       if (up[i][a] != up[i][b]) {         a = up[i][a];         b = up[i][b];       }     }     return up[0][a];   }   int get(node* v, int L, int R, int l, int r) {     if (L > r || l > R) return 0;     if (l <= L && R <= r) return v->val;     int md = (L + R) >> 1;     return get(v->left, L, md, l, r) + get(v->right, md + 1, R, l, r);   }   int calc(int u, int v, int y1, int y2) {     int r = tin[u], l = tin[v];     return d[u] - d[v] + 1 -            (get(version[y2], 1, 2 * n, l, r) - get(version[y1], 1, 2 * n, l, r));   }   int getk(int a, int k, int y1, int y2) {     for (int i = 20; i >= 0; i--) {       int count = calc(a, up[i][a], y1, y2) - calc(a, a, y1, y2);       if (count < k) {         k -= count;         a = up[i][a];       }     }     return up[0][a];   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n;     for (int i = 1; i <= n; i++) {       int p;       cin >> p;       g[i].push_back(p);       g[p].push_back(i);     }     dfs(g[0][0], g[0][0]);     version[0] = new node();     build(version[0], 0, n * 2);     cin >> m;     for (int i = 1; i <= m; i++) {       int t;       cin >> t;       if (t == 1) {         int id;         cin >> id;         version[i] = upd(version[i - 1], 1, 2 * n, tin[id], 1);         version[i] = upd(version[i], 1, 2 * n, tout[id], -1);       } else {         int a, b, k, y;         cin >> a >> b >> k >> y;         version[i] = version[i - 1];         int lc = lca(a, b);         int len1 = calc(a, lc, y, i) - calc(a, a, y, i),             len2 = calc(b, lc, y, i) - calc(b, b, y, i);         int count = len1 + len2 - calc(lc, lc, y, i);         if (count < k) {           cout << -1 <<   n ;           continue;         }         if (len1 >= k) {           cout << getk(a, k, y, i) <<   n ;         } else {           cout << getk(b, count - k + 1, y, i) <<   n ;         }       }     }     return 0;   }


#include <bits/stdc++.h>   const int inf = 0x7f7f7f7f;   inline void gettime() { ; }   inline int read() {     static char c;     static int x;     int flag = 1;     while (c = getchar(), !isdigit(c))       if (c ==  - ) flag = -1;     x = c -  0 ;     while (c = getchar(), isdigit(c)) x = (x << 1) + (x << 3) + c -  0 ;     return x * flag;   }   inline char achar() {     static char c;     while (c = getchar(), !isdigit(c))       ;     return c;   }   int n, ones;   char s[200200];   std::queue<int> ns[200200];   std::queue<int> ps[2];   int main() {     scanf( %s , s + 1);     n = strlen(s + 1);     for (int i = 1; i <= n; ++i) ones += s[i] ==  1 ;     int k = (n - ones) - ones;     if (k <= 0) return puts( -1 ), 0;     for (int i = 1; i <= k; ++i) ps[0].push(i);     for (int i = 1; i <= n; ++i) {       int cur = s[i] ==  1 ;       if (ps[cur].empty()) return puts( -1 ), 0;       ns[ps[cur].front()].push(i);       ps[cur ^ 1].push(ps[cur].front());       ps[cur].pop();     }     if (!ps[0].empty()) return puts( -1 ), 0;     printf( %d n , k);     for (int i = 1; i <= k; ++i) {       printf( %d  , ns[i].size());       for (; !ns[i].empty(); ns[i].pop()) printf( %d  , ns[i].front());       printf(  n );     }   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 3e6;   long long n, m;   long long a[N];   long long read(void) {     long long s = 0, w = 0;     char c = getchar();     while (c <  0  || c >  9 ) w |= c ==  - , c = getchar();     while (c >=  0  && c <=  9 ) s = s * 10 + c - 48, c = getchar();     return w ? -s : s;   }   long long point(long long x) {     if (x % 2 == 1) return x * (x - 1) / 2 + 1;     if (x % 2 == 0) return x * (x - 1) / 2 + x / 2;   }   signed main(void) {     n = read(), m = read();     for (long long i = 1; i <= m; ++i) {       long long q = read();       a[i] = read();     }     sort(a + 1, a + m + 1);     reverse(a + 1, a + m + 1);     long long sum = 0;     for (long long i = 1; i <= m; ++i) {       if (point(i) > n) return cout << sum, 0;       sum += a[i];     }     cout << sum << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long M = 1e9 + 7, P = 1e7 + 19;   const int N = 200005;   int nt = 1, k, n, Q, x, y, tot, op;   long long pm(long long x, long long p) {     long long res = 1;     while (p) {       if (p & 1) res = res * x % M;       p >>= 1;       x = x * x % M;     }     return res;   }   struct node {     int l, r, v, w;   } q[N << 2];   int qc[N];   vector<int> a;   long long f[50][2000];   void cdq(int l, int r, int d, vector<int> a) {     if (d > 1) {       for (int i = 1; i <= k; i++) f[d][i] = f[d - 1][i];     }     for (int i = 0; i < (int)a.size(); ++i)       if (q[a[i]].l <= l && r <= q[a[i]].r) {         for (int j = k; j >= q[a[i]].w; j--) {           f[d][j] = max(f[d][j], f[d][j - q[a[i]].w] + q[a[i]].v);         }       }     if (l == r) {       long long res = 0;       for (int i = k; i >= 1; i--) {         res = (res * P + f[d][i]) % M;       }       for (int i = 1; i <= qc[l]; i++) {         printf( %I64d n , res);       }       return;     }     int mid = l + r >> 1;     vector<int> la, ra;     for (int i = 0; i < (int)a.size(); ++i) {       if (q[a[i]].l <= l && r <= q[a[i]].r) continue;       if (q[a[i]].r <= mid) {         la.push_back(a[i]);       } else if (q[a[i]].l > mid) {         ra.push_back(a[i]);       } else {         la.push_back(a[i]);         ra.push_back(a[i]);       }     }     cdq(l, mid, d + 1, la);     cdq(mid + 1, r, d + 1, ra);   }   int main() {     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; i++) {       scanf( %d%d , &x, &y);       q[++tot] = (node){1, 0, x, y};     }     scanf( %d , &Q);     while (Q--) {       scanf( %d , &op);       if (op == 1) {         ++tot;         ++nt;         scanf( %d%d , &x, &y);         q[tot] = (node){nt, 0, x, y};       } else if (op == 2) {         scanf( %d , &x);         q[x].r = nt;         ++nt;       } else {         qc[nt]++;       }     }     for (int i = 1; i <= tot; i++) {       a.push_back(i);       if (q[i].r == 0) q[i].r = nt;     }     cdq(1, nt, 1, a);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 201;   inline void NEXT(const char *T, int *next, int t_len) {     next[0] = -1;     for (int i = 1; i < t_len; i++) {       int j = next[i - 1];       while (j >= 0 && T[i - 1] != T[j]) j = next[j];       if (j >= 0 && T[i - 1] == T[j])         next[i] = j + 1;       else         next[i] = 0;     }   }   int COUNT_KMP(const char *S, const char *T, int s_len, int t_len) {     int next[t_len];     NEXT(T, next, t_len);     int index, count = 0;     for (index = 0; index < s_len; ++index) {       int pos = 0;       int iter = index;       while (pos < t_len && iter < s_len) {         if (S[iter] == T[pos]) {           ++iter;           ++pos;         } else {           if (pos == 0)             ++iter;           else             pos = next[pos - 1] + 1;         }       }       if (pos == t_len && (iter - index) == t_len) ++count;     }     return count;   }   int my_substr(char *s, char *t, int pos, int len) {     int i;     for (i = 0; i < len; ++i) {       if (t[i + pos] ==   0 ) {         break;       }       s[i] = t[i + pos];     }     s[i] =   0 ;     return i;   }   int64_t cal(const char *s, int n, int64_t k) {     int64_t sum[MAX_N];     memset(sum, 0, sizeof(sum));     char f[MAX_N][MAX_N * 10 + 1];     int len[MAX_N];     memset(len, 0, sizeof(len));     int s_len = strlen(s);     sprintf(f[0],  0 );     sprintf(f[1],  1 );     len[0] = len[1] = 1;     if (s_len == 1) {       if (s[0] ==  0 ) {         sum[0]++;       } else if (s[0] ==  1 ) {         sum[1]++;       }     }     int max_len = s_len - 1;     char tmp_str[MAX_N * 10 + 1];     for (int i = 2; i <= n; ++i) {       len[i] = len[i - 2] + len[i - 1];       sprintf(f[i],  %s%s , f[i - 2], f[i - 1]);       int start = 0;       if (len[i - 2] > max_len) {         start = len[i - 2] - max_len;       }       int my_len = my_substr(tmp_str, f[i - 2], start, max_len);       my_len += my_substr(tmp_str + my_len, f[i - 1], 0, max_len);       int64_t result = COUNT_KMP(tmp_str, s, my_len, s_len);       sum[i] = sum[i - 2] + sum[i - 1] + result;       if (sum[i] > k) {         return sum[i];       }       if (len[i] > max_len * 2) {         int del_num = len[i] - (max_len * 2);         for (int pos = max_len + del_num; pos < len[i]; ++pos) {           f[i][pos - del_num] = f[i][pos];         }         len[i] = max_len * 2;         f[i][len[i]] =   0 ;       }     }     return sum[n];   }   int process() {     int n, m;     int64_t k;     char suffix[] =          110101101011011010110101101101011011010110101101101011011010110101101101           011010110110101101101011010110110101101011011010110110101101011011010110           11010110101101101011010110110101101101011010110110101101 ;     int suffix_len = strlen(suffix);     cin >> n >> k >> m;     char s[MAX_N];     memset(s,   0 , sizeof(s));     for (int i = 0; i < m; ++i) {       int pos = suffix_len - i;       int s_pos = 0;       while (pos < suffix_len) {         if (s[s_pos] != suffix[pos]) {           break;         }         s_pos++;         pos++;       }       if (pos == suffix_len && i > 0) {         if (k == 1) {           break;         }         k--;       }       if (i > 0 && s[i - 1] ==  0 ) {         s[i] =  1 ;         s[i + 1] =   0 ;         continue;       }       if (i > 1 && s[i - 1] ==  1  && s[i - 2] ==  1 ) {         s[i] =  0 ;         s[i + 1] =   0 ;         continue;       }       s[i] =  0 ;       s[i + 1] =   0 ;       int64_t count = cal(s, n, k);       if (count < k) {         k = k - count;         s[i] =  1 ;       }     }     printf( %s n , s);     return 0;   }   int main(int argc, char *argv[]) {     process();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline void read(T &x) {     x = 0;     int f = 1;     char ch = getchar();     for (; !isdigit(ch); ch = getchar())       if (ch ==  - ) f = -1;     for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch -  0 ;     x *= f;   }   int t, n;   const int maxn = 1e5 + 10;   int a[maxn];   map<int, bool> taked;   int main() {     read(t);     while (t--) {       bool flag = true;       taked.clear();       read(n);       int cnt = 0;       for (register int i = 0; i < n; i++) {         read(a[i]);         int c = ((a[i] + cnt) % n + n) % n;         if (taked[c]) flag = false;         taked[c] = true;         cnt++;       }       if (flag)         printf( YES n );       else         printf( NO n );     }     return 0;   }


#include <bits/stdc++.h>   #include <iostream>   #define ll long long   using namespace std;      const long long mod=998244353;   const long long mod2=1ll<<32;   const double pi = 2 * acos(0.0);      const ll N=200001;         bool compare(pair<int,int> quan1,pair<int,int> quan2)   {       return quan1.second <quan2.second ;   }   ll power(ll base,ll expo)   {       ll ans=1;       while(expo>0)       {           if(expo%2==1)           {               ans*=base;               ans=(ans)%mod;               expo--;           }           else           {               base*=base;               base=(base)%mod;               expo/=2;           }       }       return ans;   }      ll minimax(ll t, bool isMax,ll score,ll h,ll scores[])   {       if (t+1 == h)     return score;       if (isMax&&scores[t]!=1)       {         if(scores[t]%2==0)         {           return max(minimax(t+1, false, score+scores[t]-1, h,scores),      minimax(t+1,true, score+scores[t], h,scores));         }         else         {          return max(minimax(t+1, true, score+scores[t]-1, h,scores),      minimax(t+1, false, score+scores[t], h,scores));         }       }    else if (isMax&&scores[t]==1)       {         return minimax(t+1, false, score+1, h,scores);       }       else if (!isMax&&scores[t]!=1)       {         if(scores[t]%2==0)         {           return max(minimax(t+1, true, score, h,scores),      minimax(t+1,false, score, h,scores));         }         else         {          return max(minimax(t+1, false, score, h,scores),      minimax(t+1, true, score, h,scores));         }       }      else if (!isMax&&scores[t]==1)       {        return minimax(t+1, true, score, h,scores);       }   }      long long exponentiation(long long num_one,long long num_two,long long Mod)   {       long long answer= 1;       num_one = ((num_one)+Mod) % Mod;       while (num_two > 0)       {           if (num_two & 1)           {               answer =(((answer*num_one))+Mod) % Mod;           }           num_two = num_two>>1;           num_one = (((num_one*num_one))+Mod) % Mod;       }       return ((answer%Mod))%Mod;   }      ll combi(ll n,ll r,ll mod)   {    if(r==0)       {           return 1;       }       if(r>n)       {           return 0;       }       ll factorial[N+1];       ll inverse_fac[N+1];       factorial[0]=1;       inverse_fac[0]=1;       for(ll i=1;i<=n;i++)       {           factorial[i]=(factorial[i-1]*i)%mod;           inverse_fac[i]=power(factorial[i],mod-2);       }    return factorial[n]* inverse_fac[r]%mod* inverse_fac[n-r]%mod;   }      long long gcd(long long a,long long b)   {       if(b==0)       {           return a;       }       else       {           return gcd(b,a%b);       }   }   ll LCM(ll aa[],ll n,ll Mod)   {       ll lcm = aa[0];          for (ll i=1;i<n;i++)       {           lcm = ((((aa[i]*lcm)%Mod)+Mod)%Mod)/(gcd(aa[i],lcm));       }       return lcm;   }   vector<ll> finding_all_prime_factors(ll n)   {    vector<ll> v;    while(n%2==0)    {     v.push_back(2);     n = n/2;    }    for (ll y=3;y*y<=(n);y+=2)    {     while (n%y==0)     {      v.push_back(y);      n = n/y;     }    }    if(n>2)       {           v.push_back(n);       }       return v;   }   ll dfs(unordered_map<ll,ll> &size,ll node,bool visit[],vector<ll> adj[])   {       visit[node]=true;       ll s=1;       for(int i=0;i<adj[node].size();i++)       {           if(visit[adj[node][i]]==false)           {               s+=dfs(size,adj[node][i],visit,adj);           }       }       size[node]=s;       return size[node];   }   ll node;      void b_f_s(ll visited[],queue<ll> q,ll vertex,vector<ll> v[],vector<ll> adj[])   {       q.push(vertex);       visited[vertex]=1;       while(q.size()>0)       {           node=q.front();           visited[node]=1;           q.pop();           for(ll i=0;i<adj[node].size();i++)           {               if(visited[adj[node][i]]==0)               {                   //visited[adj[node][i]]=1;                   q.push(adj[node][i]);                   v[adj[node][i]]=v[node];                   v[adj[node][i]].push_back(node);               }           }       }   }      void sieve_to_count_primes(ll n)   {       ll is_it_prime[1000001];       for(ll i=1;i<=1000000;i++)       {           is_it_prime[i]=1;       }       for(ll i=2;i<=sqrt(1000000);i++)       {           if(is_it_prime[i]==1)           {               for(ll j=i*i;j<=1000000;)               {                   is_it_prime[j]=0;                   j+=i;               }           }       }   }      ll LIS_Size(vector<ll>& v)   {    if(v.size()==0)       {           return 0;       }    vector<ll> vv(v.size(),0);    ll size=1;    vv[0]=v[0];    for (ll i=1;i<v.size();i++)    {     auto b=vv.begin(),e =vv.begin()+size;     auto iter=lower_bound(b,e,v[i]);     if(iter==vv.begin()+size)     {         vv[size]=v[i];         size++;     }     else     {         *iter=v[i];     }    }    return size;   }         ll sum_in_rational_form_up(ll n,ll ups[],ll downs[],ll Mod)   {    ll ans_down=LCM(downs,n,Mod);    ll ans_up=0;    for (ll j=0;j<n;j++)    {     ans_up=(ans_up+((ups[j])*(ans_down/downs[j])))%Mod;    }    ll g_c_d=gcd(ans_up,ans_down);    ans_up/=g_c_d;    ans_down/=g_c_d;    //cout<<ans_up<<   <<ans_down;    return ((ans_up%Mod)+Mod)%Mod;       //cout<<ans.first<<   <<ans.second;   }      ll sum_in_rational_form_down(ll n,ll ups[],ll downs[],ll Mod)   {    ll ans_down=LCM(downs,n,Mod);    ll ans_up=0;    for (ll j=0;j<n;j++)    {     ans_up=(ans_up+((ups[j])*(ans_down/downs[j])))%Mod;    }    ll g_c_d=gcd(ans_up,ans_down);    ans_up/=g_c_d;    ans_down/=g_c_d;    //cout<<ans_up<<   <<ans_down;    return (ans_down%Mod+Mod)%Mod;       //cout<<ans.first<<   <<ans.second;   }      ll mod_inverse(ll a,ll m)   {       ll GCD=gcd(a,m);       if(GCD==1)       {           return power(a,m-2);       }   }      bool possible(ll ind,vector<ll>& from_left,ll val1,ll val2,vector<ll>& from_right)   {       ll n=from_left.size();       if(ind>=1&&(ind+2)<n)       {           if(val2>=from_left[ind-1]&&((val1-(val2-from_left[ind-1]))==from_right[ind+2]))           {               return true;           }           else           {               return false;           }       }       else if(ind==0)       {           if(((val1-val2)==from_right[ind+2]))           {               return true;           }           else           {               return false;           }       }       else       {           if(((val2-val1)==from_left[ind-1]))           {               return true;           }           else           {               return false;           }       }   }         int main()   {    ios::sync_with_stdio(false);    cin.tie(0);    ll t,n,k,m;       cin>>t;       while(t--)       {           cin>>n;           vector<ll> aa(n);           for(ll i=0;i<n;i++)           {               cin>>aa[i];           }           if(n==2)           {               if(aa[0]==aa[1])               {                   cout<< YES <<endl;               }               else               {                   cout<< NO <<endl;               }           }           else           {               vector<ll> bb(n);               bb=aa;               vector<ll> from_left(n),from_right(n);               from_left[0]=bb[0];               for(ll i=1;i<n;i++)               {                   if(bb[i]<from_left[i-1])                   {                       from_left[i]=LLONG_MIN;                       continue;                   }                   if(from_left[i-1]==LLONG_MIN)                   {                       from_left[i]=LLONG_MIN;                       continue;                   }                   from_left[i]=bb[i]-from_left[i-1];               }               bb=aa;               from_right[n-1]=bb[n-1];               for(ll i=n-2;i>=0;i--)               {                   if(bb[i]<from_right[i+1])                   {                       from_right[i]=LLONG_MIN;                       continue;                   }                   if(from_right[i+1]==LLONG_MIN)                   {                       from_right[i]=LLONG_MIN;                       continue;                   }                   from_right[i]=bb[i]-from_right[i+1];               }               ll f=0;               if(from_left[n-1]==0||from_right[0]==0)               {                   f=1;               }               for(ll i=0;i<n-1;i++)               {                   if(i>=1&&from_left[i-1]==LLONG_MIN)                   {                       continue;                   }                   if(i+2<=(n-1)&&from_right[i+2]==LLONG_MIN)                   {                       continue;                   }                   if(possible(i,from_left,aa[i],aa[i+1],from_right)==true)                   {                       f=1;                       break;                   }               }               if(f==1)               {                   cout<< YES <<endl;               }               else               {                   cout<< NO <<endl;               }              }          }    return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int arr[5] = {0};     for (int i = 0; i < n; ++i) {       int tmp;       cin >> tmp;       arr[tmp]++;     }     int ret = arr[4];     ret += min(arr[1], arr[3]);     ret += arr[2] / 2;     if (arr[3] > arr[1]) {       ret += arr[3] - arr[1];       ret += (arr[2] % 2) ? 1 : 0;     } else {       int tmp = arr[1] - arr[3];       ret += tmp / 4;       if (arr[2] % 2) {         if (tmp % 4 <= 2)           ret += 1;         else           ret += 2;       } else {         ret += (tmp % 4) ? 1 : 0;       }     }     cout << ret << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const long long inf = 0x3f3f3f3f3f3f3f3f;   const long long mod = 1e9 + 7;   const int N = 2e5 + 10;   int T;   int n, m;   int cnt[1000][2], mz[40][40];   int main() {     scanf( %d , &T);     while (T--) {       scanf( %d%d , &n, &m);       memset(cnt, 0, sizeof(cnt));       for (int i = 1; i <= n; i++) {         for (int j = 1; j <= m; j++) {           scanf( %d , &mz[i][j]);           int a = abs(i - 1) + abs(j - 1);           int b = abs(i - n) + abs(j - m);           cnt[min(a, b)][mz[i][j]]++;         }       }       int ans = 0;       for (int i = 0; i <= (n - 1 + m - 1 - 1) / 2; i++) {         ans += min(cnt[i][1], cnt[i][0]);       }       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1000005;   int N, M, dx, dy;   int xidx[MAXN];   int cnt[MAXN];   int ans[MAXN][2];   int main() {     cin >> N >> M >> dx >> dy;     for (int i = 0; i < N; i++) xidx[((long long)i * dx) % N] = i;     for (int i = 0; i < M; i++) {       int x, y;       cin >> x >> y;       int val = (y - (long long)xidx[x] * dy) % N;       if (val < 0) val += N;       cnt[val]++;       ans[val][0] = x, ans[val][1] = y;     }     int best = 0;     for (int i = 1; i < N; i++)       if (cnt[best] < cnt[i]) best = i;     cout << ans[best][0] <<     << ans[best][1] <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   void dbs(string str, T t) {     cerr << str <<   :   << t <<   n ;   }   template <class T, class... S>   void dbs(string str, T t, S... s) {     int idx = str.find( , );     cerr << str.substr(0, idx) <<   :   << t <<  ,  ;     dbs(str.substr(idx + 1), s...);   }   template <class S, class T>   ostream& operator<<(ostream& os, const pair<S, T>& p) {     return os <<  (  << p.first <<  ,   << p.second <<  ) ;   }   template <class T>   ostream& operator<<(ostream& os, const vector<T>& p) {     os <<  [  ;     for (auto& it : p) os << it <<    ;     return os <<  ] ;   }   template <class T>   ostream& operator<<(ostream& os, const set<T>& p) {     os <<  [  ;     for (auto& it : p) os << it <<    ;     return os <<  ] ;   }   template <class S, class T>   ostream& operator<<(ostream& os, const map<S, T>& p) {     os <<  [  ;     for (auto& it : p) os << it <<    ;     return os <<  ] ;   }   template <class T>   void prc(T a, T b) {     cerr <<  [ ;     for (T i = a; i != b; ++i) {       if (i != a) cerr <<  ,  ;       cerr << *i;     }     cerr <<  ] n ;   }   void code() {     long long n;     cin >> n;     vector<long long> cnt(4);     long long a, b, c;     a = b = c = 0;     for (long long i = 0; i < (long long)(n); ++i) {       string s;       cin >> s;       if (s ==  M ) a++;       if (s ==  S ) b++;       if (s ==  L )         c++;       else if (s ==  XS )         cnt[1]++;       else if (s ==  XXS )         cnt[2]++;       else if (s ==  XXXS )         cnt[3]++;     }     for (long long i = 0; i < (long long)(n); ++i) {       string s;       cin >> s;       if (s ==  M ) a--;       if (s ==  S ) b--;       if (s ==  L )         c--;       else if (s ==  XS )         cnt[1]--;       else if (s ==  XXS )         cnt[2]--;       else if (s ==  XXXS )         cnt[3]--;     }     long long ans = 0;     ans += abs(a) + abs(b) + abs(c);     ans /= 2;     ans += abs(cnt[1]) + abs(cnt[2]) + abs(cnt[3]);     cout << ans;   }   int main() {     int t = 1;     for (auto i = (1); i != (t + 1); i++) {       code();     }     return 0;   }