#include <bits/stdc++.h>   using namespace std;   int n;   long long k;   vector<vector<long long>> operator*(vector<vector<long long>> a,                                       vector<vector<long long>> b) {     vector<vector<long long>> ans(n, vector<long long>(n));     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         ans[i][j] = 0;         for (int k = 0; k < n; k++)           ans[i][j] = (ans[i][j] + a[i][k] * b[k][j]) % 1000000007;       }     }     return ans;   }   vector<vector<long long>> fastxp(vector<vector<long long>> a, long long e) {     if (e == 1) return a;     vector<vector<long long>> ans = fastxp(a, e / 2);     ans = (ans * ans);     if (e % 2 == 1) ans = ans * a;     return ans;   }   int sumbits(long long a) {     int sum = 0;     while (a > 0) {       sum += a % 2;       a /= 2;     }     return sum;   }   int main() {     ios::sync_with_stdio(false);     cin >> n >> k;     vector<vector<long long>> aux(n, vector<long long>(n));     vector<vector<long long>> v(n, vector<long long>(n));     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         aux[i][j] = 0;         v[i][j] = 0;       }     }     for (int i = 0; i < n; i++) v[i][0] = 1;     long long a[110];     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         if (sumbits((a[i] ^ a[j])) % 3 == 0) aux[i][j] = 1;       }     }     if (k == 1) {       cout << n << endl;       return 0;     } else {       vector<vector<long long>> ans = fastxp(aux, k - 1);       v = ans * v;       long long sum = 0;       for (int i = 0; i < n; i++) {         sum = (sum + v[i][0]) % 1000000007;       }       cout << sum << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int a, b, n, i, x, y, v;   double min1, ans, k;   int main() {     cin >> a >> b;     cin >> n;     min1 = 999999;     for (i = 1; i <= n; i++) {       cin >> x >> y >> v;       k = (x - a) * (x - a) + (y - b) * (y - b);       ans = sqrt(k) / v;       min1 = min(min1, ans);     }     printf( %.12f n , min1);   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007;   long long powmod(long long a, long long b) {     long long res = 1;     a %= mod;     for (; b; b >>= 1) {       if (b & 1) res = res * a % mod;       a = a * a % mod;     }     return res;   }   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   int main() {     ios_base::sync_with_stdio(false);     int _;     int m, d, w;     for (cin >> _; _ && _--;) {       cin >> m >> d >> w;       int d_dcg = gcd(d - 1, w);       int d1 = (d - 1) / d_dcg;       int w1 = w / d_dcg;       int m_w1 = (min(m, d) - 1) / w1;       cout << 1LL * m_w1 * min(m, d) - 1LL * w1 * (1 + m_w1) * m_w1 / 2 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int i, n, k, j;     cin >> n >> k;     for (i = 0; i < n; i++) {       for (j = 0; j < n; j++)         if (i == j)           cout << k <<    ;         else           cout << 0 <<    ;       cout <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);     int t;     cin >> t;     while (t--) {       int c, d;       cin >> c >> d;       int temp = abs(c - d);       if (temp % 2) {         cout <<  -1  << endl;         continue;       }       if (c == 0 and d == 0) {         cout <<  0  << endl;         continue;       }       if (temp == 0) {         cout <<  1  << endl;         continue;       }       cout <<  2  << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 305;   long long n, k;   long long a[N][N], rows[N], cols[N];   long long sqr(long long x) { return x * x; }   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long t;     cin >> t;     while (t--) {       cin >> n >> k;       for (long long i = 0; i < n; i++) {         rows[i] = cols[i] = 0;         for (long long j = 0; j < n; j++) a[i][j] = 0;       }       long long sti = 0, i = 0, j = 0;       while (k > 0) {         if (a[i][j]) {           sti++;           i = sti, j = 0;         } else {           a[i][j] = 1;           rows[i]++;           cols[j]++;           i++, j++;           i %= n;           j %= n;           k--;         }       }       long long ans = 0;       sort(rows, rows + n);       sort(cols, cols + n);       ans += sqr(rows[n - 1] - rows[0]) + sqr(cols[n - 1] - cols[0]);       cout << ans <<   n ;       for (long long i = 0; i < n; i++) {         for (long long j = 0; j < n; j++) cout << a[i][j];         cout <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int M = 100000 + 10;   struct Node {     int l, r, q;   };   struct Tree {     int l, r;     int ans;     int lazy;   };   Tree tree[M * 3];   Node d[M];   int mex[M];   int n, m;   void get_same(int s) {     if (tree[s].l == tree[s].r) return;     if (tree[s].lazy) {       tree[s << 1].ans = tree[s << 1].ans | tree[s].ans;       tree[s << 1 | 1].ans = tree[s << 1 | 1].ans | tree[s].ans;       tree[s << 1].lazy = tree[s << 1 | 1].lazy = 1;       tree[s].lazy = tree[s].ans = 0;     }   }   void bulit(int s, int l, int r) {     tree[s].l = l;     tree[s].r = r;     tree[s].ans = 0;     tree[s].lazy = 0;     if (l != r) {       int mid = (l + r) >> 1;       bulit(s << 1, l, mid);       bulit(s << 1 | 1, mid + 1, r);     }   }   void updata(int s, int l, int r, int z) {     if (tree[s].l == l && tree[s].r == r) {       tree[s].ans = tree[s].ans | z;       tree[s].lazy = 1;       return;     }     get_same(s);     int mid = (tree[s].l + tree[s].r) >> 1;     if (r <= mid)       updata(s << 1, l, r, z);     else if (l > mid)       updata(s << 1 | 1, l, r, z);     else {       updata(s << 1, l, mid, z);       updata(s << 1 | 1, mid + 1, r, z);     }   }   void inse(int s) {     get_same(s);     if (tree[s].l == tree[s].r) {       mex[tree[s].l] = tree[s].ans;       return;     }     inse(s << 1);     inse(s << 1 | 1);     tree[s].ans = tree[s << 1].ans & tree[s << 1 | 1].ans;   }   int query(int s, int l, int r) {     if (tree[s].l == l && tree[s].r == r) {       return tree[s].ans;     }     int mid = (tree[s].l + tree[s].r) >> 1;     if (r <= mid)       return query(s << 1, l, r);     else if (l > mid)       return query(s << 1 | 1, l, r);     else       return query(s << 1, l, mid) & query(s << 1 | 1, mid + 1, r);   }   bool solve() {     for (int i = 0; i < m; i++) {       if (query(1, d[i].l, d[i].r) != d[i].q) return false;     }     return true;   }   int main() {     while (scanf( %d%d , &n, &m) == 2) {       bulit(1, 1, n);       for (int i = 0; i < m; i++) {         scanf( %d%d%d , &d[i].l, &d[i].r, &d[i].q);         updata(1, d[i].l, d[i].r, d[i].q);       }       inse(1);       if (solve()) {         puts( YES );         for (int i = 1; i <= n; i++) {           if (i != 1) printf(   );           printf( %d , mex[i]);         }         printf(  n );       } else {         puts( NO );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     char arr[105];     cin >> t;     gets(arr);     for (int i = 0; i < t; i++) {       int flg1 = 0;       int flg2 = 0;       gets(arr);       int len = strlen(arr);       if (arr[0] ==  m  && arr[1] ==  i  && arr[2] ==  a  && arr[3] ==  o  &&           arr[4] ==  . ) {         flg1 = 1;       }       if (arr[len - 1] ==  .  && arr[len - 2] ==  a  && arr[len - 3] ==  l  &&           arr[len - 4] ==  a  && arr[len - 5] ==  l ) {         flg2 = 1;       }       if ((flg1 == 1 && flg2 == 1) || (flg1 == 0 && flg2 == 0)) {         printf( OMG>.< I don t know! n );       } else if (flg1 == 1) {         printf( Rainbow s n );       } else {         printf( Freda s n );       }     }     return 0;   }


#include<cstdio>   #include<cstring>   #include<iostream>   #include<algorithm>   #include<cmath>   #include<vector>   #include<queue>   #include<stack>   #include<ctime>   #include<map>   #define ll long long   #define MAXN 20105   #define uns unsigned   #define INF 0x3f3f3f3f   #define MOD 1000000007ll   #define lowbit(x) ((x)&(-(x)))   using namespace std;   inline ll read(){    ll x=0;bool f=1;char s=getchar();    while((s< 0 ||s> 9 )&&s>0){if(s== - )f^=1;s=getchar();}    while(s>= 0 &&s<= 9 )x=(x<<1)+(x<<3)+s- 0 ,s=getchar();    return f?x:-x;   }   int n,q,a[MAXN],st[MAXN][20],dp[MAXN][20][35],lg[MAXN];   int A[35],B[35],tmp[35];   inline int maxx(int x,int y){return x+a[x]>y+a[y]?x:y;}   inline int rmq(int l,int r){    if(l>r)return 0;r=min(r,n);int k=lg[r-l+1];    return maxx(st[l][k],st[r-(1<<k)+1][k]);   }   signed main()   {    n=read(),q=read();    for(int i=1;i<=n;i++)a[i]=read(),st[i][0]=i;    lg[1]=0;    for(int i=2;i<=n;i++)lg[i]=lg[i>>1]+1;    for(int i=n;i>0;i--)     for(int j=1;i+(1<<j)-1<=n;j++)      st[i][j]=maxx(st[i][j-1],st[i+(1<<(j-1))][j-1]);    for(int i=1;i<=n;i++)     for(int k=0;k<=30;k++)dp[i][0][k]=i+a[i]+k;    for(int j=0;j<lg[n];j++)     for(int i=1;i<=n;i++)      for(int k=0;k<=30;k++){       for(int l=k;l<=30;l++)        dp[i][j+1][l]=max(dp[i][j+1][l],dp[ rmq(i,dp[i][j][k]) ][j][l-k]);       for(int l=k;l<=30;l++)        dp[i][j+1][l]=max(dp[i][j+1][l],dp[i][j+1][k]+l-k);      }    while(q--){     int l=read(),r=read(),k=read();     if(l==r){printf( 0 n );continue;}     if(r<=l+a[l]+k){printf( 1 n );continue;}     int ans=2;     for(int i=0;i<=k;i++)A[i]=l+a[l]+i;     for(int e=lg[r-l+1];e>=0;e--){      for(int i=0;i<=k;i++)tmp[i]=A[i],B[i]=rmq(l,A[i]);      bool off=0;      for(int i=0;i<=k&&!off;i++)       for(int j=i;j<=k&&!off;j++)        if(dp[B[i]][e][j-i]>tmp[j]){         tmp[j]=dp[B[i]][e][j-i];         if(tmp[j]>=r)off=1;        }      if(off)continue;ans+=(1<<e);      for(int i=0;i<=k;i++)A[i]=tmp[i];     }     printf( %d n ,ans);    }    return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a, b, arr[25], ans = 0, c[3];     bool r1 = 1;     cin >> n >> a >> b;     c[0] = a, c[1] = b;     for (int i = 0; i < n; i++) {       cin >> arr[i];     }     int mid = n / 2;     if (2 * mid < n) mid++;     for (int i = 0; i < mid; i++) {       if (arr[i] == arr[n - i - 1] && arr[i] != 2) {         continue;       } else if (arr[i] == arr[n - i - 1] && arr[i] == 2) {         if (i == n - i - 1)           ans += min(a, b);         else           ans += 2 * min(a, b);       } else if (arr[i] == 2 || arr[n - i - 1] == 2) {         if (arr[i] == 2)           ans += c[arr[n - i - 1]];         else           ans += c[arr[i]];       } else         r1 = 0;     }     if (r1)       cout << ans << endl;     else       cout << -1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 3e5 + 10;   const long long M = 1e9 + 7;   int n, x, y, z;   int last[N], to[2 * N], w[2 * N], num, nxt[2 * N];   long long a[N];   long long ans;   void Add(int u, int v, int c) {     nxt[++num] = last[u];     to[num] = v;     w[num] = c;     last[u] = num;   }   long long dp[N];   long long dfs(int u, int f) {     long long max1, max2;     max1 = max2 = 0;     for (int i = last[u]; i; i = nxt[i]) {       int v = to[i];       if (v == f) continue;       dfs(v, u);       long long now = dp[v] - w[i];       if (now >= max1) {         max2 = max1;         max1 = now;       } else if (now > max2)         max2 = now;     }     long long tot = max1 + max2 + a[u];     ans = max(ans, tot);     return dp[u] = max(max1, max2) + a[u];   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) scanf( %lld , a + i);     for (int i = 1; i <= n - 1; ++i) {       scanf( %d%d%d , &x, &y, &z);       Add(x, y, z);       Add(y, x, z);     }     dfs(1, 0);     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int a, b, c, t, d, e, f;   long long int max(long long int x, long long int y) {     if (x > y)       return x;     else       return y;   }   int main() {     scanf( %I64d%I64d%I64d , &a, &b, &c);     t = max(a, max(b, c));     d = max(t - 1, b);     e = max(t - 1, c);     f = max(t - 1, a);     printf( %I64d n , d - b + e - c + f - a);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T1, typename T2>   istream& operator>>(istream& in, pair<T1, T2>& a) {     in >> a.first >> a.second;     return in;   }   template <typename T1, typename T2>   ostream& operator<<(ostream& out, pair<T1, T2> a) {     out << a.first <<     << a.second;     return out;   }   template <typename T, typename T1>   T amax(T& a, T1 b) {     if (b > a) a = b;     return a;   }   template <typename T, typename T1>   T amin(T& a, T1 b) {     if (b < a) a = b;     return a;   }   const long long INF = 1e18;   const int32_t M = 1e9 + 7;   const int32_t MM = 998244353;   const double inf = 2 * INF;   const long long N = 0;   vector<long long> tmp, v;   long long pw(long long a, long long p) {     long long result = 1;     while (p > 0) {       if (p & 1) result = a * result;       a = a * a;       p >>= 1;     }     return result;   }   long long sq(long long x) {     long long ans = sqrt(x);     while (ans * ans < x) {       ans++;     }     while (ans * ans > x) {       ans--;     }     return ans;   }   void precompute() {     for (long long i = 3; i < 61; i++) {       for (long long j = 2; j < INF; j++) {         if (pow(j, i) >= inf) {           break;         }         tmp.push_back(pw(j, i));       }     }     sort((tmp).begin(), (tmp).end());     (tmp).erase(unique((tmp).begin(), (tmp).end()), (tmp).end());     for (long long x : tmp) {       long long t = sq(x);       if (t * t == x) {         continue;       }       v.push_back(x);     }   }   void solve() {     long long n;     cin >> n;     long long ans = upper_bound((v).begin(), (v).end(), n) - v.begin();     long long x = sq(n) - 1;     ans += x;     ans = n - 1 - ans;     cout << ans <<   n ;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     precompute();     long long t = 1;     cin >> t;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long srw[1000], scol[1000];   long long rows[1000001], cols[1000001];   int main() {     int x, n, m, k, p, y, i, j;     scanf( %d%d%d%d , &n, &m, &k, &p);     for (i = 0; i < n; i++)       for (j = 0; j < m; j++) {         scanf( %d , &x);         srw[i] += x;         scol[j] += x;       }     multiset<long long, greater<long long> > rsm, csm;     multiset<long long, greater<long long> >::iterator it;     for (i = 0; i < n; i++) rsm.insert(srw[i]);     for (j = 0; j < m; j++) csm.insert(scol[j]);     for (i = 1; i <= k; i++) {       it = rsm.begin();       long long val = *it;       rsm.erase(it);       rsm.insert(val - p * m);       rows[i] = rows[i - 1] + val;       it = csm.begin();       val = *it;       csm.erase(it);       csm.insert(val - p * n);       cols[i] = cols[i - 1] + val;     }     long long ans = -99999999999999999;     for (i = 0; i <= k; i++)       ans = max(ans, cols[i] + rows[k - i] - 1LL * (k - i) * i * p);     printf( %I64d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, i, j, T, ti;   long long MX = 4294967295;   long long temp;   long long loop(int n) {     long long ret = 0;     string str;     int rn;     cin >> str;     m--;     while (str !=  end ) {       if (str ==  add ) {         ret++;       } else if (str ==  for ) {         cin >> rn;         temp = loop(rn);         if (temp == -1) break;         ret += temp;       } else {         cerr <<  WOW   << str << endl;       }       if (m == 0) break;       cin >> str;       m--;     }     if (temp == -1) return -1;     if (ret * n > MX) {       return -1;     }     return ret * n;   }   int main() {     ios::sync_with_stdio(false);     cin >> m;     temp = 0;     temp = loop(1);     if (temp == -1) {       cout <<  OVERFLOW!!!  << endl;     } else {       cout << temp << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   void read(T &t) {     t = 0;     char ch = getchar();     int f = 1;     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     do {       (t *= 10) += ch -  0 ;       ch = getchar();     } while ( 0  <= ch && ch <=  9 );     t *= f;   }   const long long mod = 998244353;   long long ksm(long long x, long long y) {     if (x < 0) x += mod;     if (x >= mod) x -= mod;     long long res = 1;     while (y) {       if (y & 1) res = res * x % mod;       x = x * x % mod;       y >>= 1;     }     return res;   }   long long n, m, N, L, R, ans, x, y;   int main() {     read(n);     read(m);     read(L);     read(R);     N = n * m;     x = R / 2 - (L - 1) / 2;     y = R - L + 1 - x;     if (N % 2 == 1)       ans = ksm(x + y, N);     else {       ans = ksm(x + y, N) + ksm(x - y, N);       ans %= mod;       if (N % 2 == 0) ans = ans * ksm(2, mod - 2) % mod;     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline void read(T &k) {     k = 0;     int flag = 1;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) flag = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       k = k * 10 + c -  0 ;       c = getchar();     }     k *= flag;   }   const int N = 1005;   int n, m, k, s, t;   long long pa, pb, f[N][N], A, B;   void exgcd(long long a, long long b, long long &x, long long &y) {     if (a == 0) {       x = 0;       y = 1;       return;     }     exgcd(b % a, a, y, x);     x -= b / a * y;   }   long long inv(long long a) {     long long x, y;     exgcd(a, 1000000007, x, y);     return x;   }   int main() {     cin >> k >> pa >> pb;     A = pa * inv(pa + pb) % 1000000007;     B = pb * inv(pa + pb) % 1000000007;     for (int i = k; i >= 1; --i)       for (int j = k - 1; j >= 0; --j) {         if (i + j >= k) {           f[i][j] = (i + j + pa * inv(pb)) % 1000000007;         } else {           f[i][j] = (A * f[i + 1][j] + B * f[i][i + j]) % 1000000007;         }       }     cout << (f[1][0] + 1000000007) % 1000000007 << endl;   }


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


#include <bits/stdc++.h>   using namespace std;   const long double PI = 3.1415926535897923846;   const long long int MOD = 1000000007;   const long long int N = 998244353;   long long int power(long long int x, long long int n) {     long long int res = 1;     while (n > 0) {       if (n & 1) res = res * x % MOD;       x = x * x % MOD;       n >>= 1;     }     return res;   }   long long int modinverse(long long int a) { return power(a, MOD - 2); }   void solve() {     long long int n;     cin >> n;     vector<long long int> a(n);     for (long long int i = 0; i < n; i++) {       cin >> a[i];     }     if (n % 2 == 0) {       long long int xo = 0;       for (long long int i = 0; i < n - 1; i++) xo = (xo ^ a[i]);       if (a.back() - xo) {         cout <<  NO n ;         return;       }     }     n -= (1 - n % 2);     cout <<  YES n ;     cout << n - 1 << endl;     for (long long int i = 0; i + 2 < n; i += 2) {       cout << i + 1 <<     << i + 2 <<     << i + 3 << endl;     }     for (long long int i = n - 3; i >= 0; i -= 2) {       cout << i + 1 <<     << i + 2 <<     << i + 3 << endl;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int test = 1;     while (test--) {       solve();     }     cerr <<  Time :   << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC          <<  ms n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int p[3500];     int n;     cin >> n;     bool b = 1;     int j = 1;     for (int i = 1; i <= n; i++) {       cin >> p[i];     }     sort(p + 1, p + n + 1);     for (int i = 1; i <= n; i++) {       if (p[i] != j) {         cout << j;         return 0;       } else {         j++;       }     }     cout << p[n] + 1;     return 0;   }


#ifndef ONPC     #define _FORTIFY_SOURCE 0     #pragma GCC optimize( Ofast )     #pragma GCC optimize( no-stack-protector )     #pragma GCC optimize( unroll-loops )     #pragma GCC target( sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native )     #pragma GCC optimize( fast-math )   #endif      #include <bits/stdc++.h>      #define mp make_pair   #define eb emplace_back   #define x first   #define y second      template <typename T>   bool chmax(T &a, const T &b) {       if (a < b) {         a = b;           return true;       }       return false;   }      template <typename T>   bool chmin(T &a, const T &b) {       if (a > b) {         a = b;         return true;       }       return false;   }      using namespace std;      typedef long long ll;   typedef long double ld;   typedef pair<int, int> pt;      #ifdef ONPC   mt19937 rnd(228);   #else   mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());   #endif      const ll MOD = 1000000007LL;   const ll MOD2 = 998244353LL;   const ld PI = acos(-1);      #ifdef ONPC   const int MAXN = 100;   #else   const int MAXN = 300100;   #endif   //#define int long long   const ll INF = 1e18;      void solve(){     int n;     cin >> n;     vector<int> p(n);     for (int i = 1; i < n; i++){       cin >> p[i];       p[i]--;     }     vector<ll> a(n);     for (int i = 1; i < n; i++){       cin >> a[i];     }     vector<int> d(n, 0);     for (int i = 1; i < n; i++){       if (d[i]) continue;       vector<int> q;       int j = i;       while (j != 0) {         q.eb(j);         j = p[j];       }       reverse(begin(q), end(q));       for (int k = 0; k < q.size(); k++){         d[q[k]] = k + 1;       }     }        vector<vector<int>> v(n, vector<int>());     for (int i = 0; i < n; i++){       v[d[i]].eb(i);     }        vector<ll> dp(n, 0);     for (int i = 1; i < n; i++){       if (v[i].empty()) break;          ll mnc = INF, mxc = -INF;       for (int j : v[i]){         chmin(mnc, a[j]);         chmax(mxc, a[j]);       }       ll mx1 = -INF, mx2 = -INF;       for (int j : v[i]){         chmax(mx1, dp[p[j]] + a[j]);         chmax(mx2, dp[p[j]] - a[j]);       }       for (int j : v[i]){         dp[j] = dp[p[j]] + max(a[j] - mnc, mxc - a[j]);         chmax(dp[j], mx1 - a[j]);         chmax(dp[j], mx2 + a[j]);       }     }        ll ans = 0;     for (int i = 0; i < n; i++) chmax(ans, dp[i]);        //for (int i = 0; i < n; i++) cerr << i <<   :   << dp[i] <<   n ;        cout << ans <<   n ;   }      /*   #ifndef ONPC   #define FILEIO   #endif   */   signed main()   {       #ifdef FILEIO         freopen( input.txt ,  r , stdin);         freopen( output.txt ,  w , stdout);       #endif          ios_base::sync_with_stdio(0);       cin.tie(0);          int Q;       cin >> Q;       while (Q--)         solve();          return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string str;     cin >> str;     if (str[0] > 96) {       str[0] -= 32;     }     cout << str << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int dx[] = {0, 1, 0, -1, -1, 1, -1, 1};   long long int dy[] = {-1, 0, 1, 0, -1, 1, 1, -1};   const long long int MAXN = 2e5 + 5;   const long long int N = 2e5 + 5;   long long int n, m;   void LCSubsequence(string s1, string s2) {     long long int n1 = s1.size(), n2 = s2.size();     vector<vector<long long int>> dp(n1 + 1, vector<long long int>(n2 + 1, 0));     long long int res = 0;     for (long long int i = 0; i <= n1; i++) {       for (long long int j = 0; j <= n2; j++) {         if (i == 0 || j == 0) {           dp[i][j] = 0;         } else if (s1[i - 1] == s2[j - 1]) {           dp[i][j] = dp[i - 1][j - 1] + (4 - 2);         } else {           dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) - 1;         }         if (dp[i][j] < 0) dp[i][j] = 0;         res = max(res, dp[i][j]);       }     }     cout << res <<   n ;   }   long long int goAns = 0;   long long int go(long long int i, long long int j, string s1, string s2) {     if (i >= n || j >= m) return 0;     long long int x = 0, y = 0, res = 0;     if (s1[i] == s2[j]) {       res = (4 - 2) + max(0LL, go(i + 1, j + 1, s1, s2));     } else {       x = -1 + go(i + 1, j, s1, s2);       y = -1 + go(i, j + 1, s1, s2);       res = max({0LL, x, y});     }     goAns = max(goAns, res);     return res;   }   void solve() {     long long int x, y, z;     cin >> n >> m;     string s1, s2;     cin >> s1 >> s2;     LCSubsequence(s1, s2);   }   int32_t main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long int T = 1;     while (T--) solve();     return 0;   }


#include <bits/stdc++.h>   #pragma optimization_level 3   #pragma GCC optimize( Ofast,no-stack-protector,unroll-loops,fast-math,O3 )   #pragma GCC target( avx,avx2,fma )   using namespace std;   mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());   int a, k, nc = 0, isz = 0;   vector<unordered_set<int>> n, l;   vector<int> cmp, vrt;   void dfs(int v) {     cmp[v] = nc;     vrt.push_back(v);     for (int i : l[v]) {       if (cmp[i] == -1)         dfs(i);       else if (cmp[i] != nc) {         assert(cmp[i] == 0);         isz = 1;       }     }   }   int main() {     cin.tie(0);     cout.tie(0);     cin.sync_with_stdio(0);     cout.sync_with_stdio(0);     ;     cin >> a >> k;     cmp = vector<int>(a, -1);     l = vector<unordered_set<int>>(a, unordered_set<int>());     n = vector<unordered_set<int>>(a, unordered_set<int>());     for (int q = 0; q < k; q++) {       int x, y;       cin >> x >> y;       x--, y--;       n[x].insert(y);       n[y].insert(x);     }     for (int q = 0; q < a; q++) {       int sos = (a - 1) - n[q].size();       if (sos > a / 2) cmp[q] = 0;     }     for (int q = 0; q < a; q++) {       if (cmp[q] != -1) continue;       for (int w = 0; w < a; w++) {         if (w == q || n[q].count(w)) continue;         l[q].insert(w);       }     }     for (int q = 0; q < a; q++) {       if (cmp[q] != -1) continue;       nc = q + 1;       vrt.clear();       isz = 0;       dfs(q);       if (isz) {         for (int i : vrt) cmp[i] = 0;       }     }     map<int, int> mp;     for (int i : cmp) mp[i]++;     vector<int> u;     for (pair<int, int> p : mp) u.push_back(p.second);     sort(u.begin(), u.end());     cout << u.size() <<   n ;     for (int i : u) cout << i <<    ;   }


#include <bits/stdc++.h>   using namespace std;   string s;   int main() {     int n, i, j, k, l, m;     cin >> n;     cin >> s;     int cnt, ans;     char c, c1, c2, c3;     ans = 0;     for (i = 0; i < n - 1; i++) {       cnt = 0;       if (s[i] == s[i + 1] && i < n - 1) {         c = s[i];         while (s[i] == c) {           i++;           cnt++;         }         ans += (cnt / 2);         if (i < n) c2 = s[i];         if (c2 ==  R  && c ==  B )           c1 =  G ;         else if (c2 ==  R  && c ==  G )           c1 =  B ;         else if (c2 ==  B  && c ==  G )           c1 =  R ;         else if (c2 ==  B  && c ==  R )           c1 =  G ;         else if (c2 ==  G  && c ==  B )           c1 =  R ;         else if (c2 ==  G  && c ==  R )           c1 =  B ;         if (i >= n) {           if (c ==  R )             c1 =  B ;           else if (c ==  B )             c1 =  G ;           else             c1 =  R ;         }         for (j = i - cnt + 1; j < i; j += 2) s[j] = c1;         i--;       }     }     printf( %d n , ans);     cout << s << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     int a[110];     for (int i = 0; i < n; i++) {       cin >> a[i];     }     int i, one, two, j, ans = 0;     for (i = 0; i < k; i++) {       for (one = 0, two = 0, j = i; j < n; j += k) {         if (a[j] == 1)           one++;         else           two++;       }       ans += min(one, two);     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, a, b;   int main() {     cin >> n >> a >> b;     int t = 1;     int r = 0;     if (a > b) {       a ^= b ^= a ^= b;     }     while (t < n) {       t <<= 1;       ++r;     }     t >>= 1;     if (a <= t && b > t) {       cout <<  Final!  << endl;       return 0;     }     while (a > t || b <= t) {       a -= a > t ? t : 0;       b -= b > t ? t : 0;       t >>= 1;       --r;     }     cout << r << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mo = 1e9 + 7;   const int maxi = 1e6 + 6;   long long a[maxi], x[maxi], b[maxi];   int n;   long long t;   int sum[maxi];   int los[maxi];   int ll[maxi];   int main() {     cin >> n >> t;     for (int i = 1; i <= n; i++) scanf( %lld , &a[i]);     for (int i = 1; i <= n; i++) {       scanf( %lld , &x[i]);       sum[x[i] - 1]++;       sum[i - 1]--;     }     for (int i = n; i > 0; i--) sum[i] += sum[i + 1];     for (int i = 1; i <= n; i++)       if (x[i] < i || x[i] < x[i - 1]) return !printf( No n );     for (int i = 1; i <= n; i++)       if (sum[i] > 0)         b[i] = a[i + 1] + t;       else         b[i] = a[i] + t;     for (int i = 2; i <= n; i++)       if (b[i] <= b[i - 1]) b[i] = b[i - 1] + 1;     for (int i = 2; i <= n; i++)       if (a[i] + t > b[i - 1]) los[i] = 1;     for (int i = 1; i <= n; i++) ll[i] = ll[i - 1] + los[i];     for (int i = 1; i <= n; i++)       if (x[i] < n && x[i] - i > 0 && ll[x[i] + 1] - ll[i] == 0)         return !printf( No n );     printf( Yes n );     for (int i = 1; i <= n; i++) printf( %lld  , b[i]);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   typedef vector<vector<int>> graph;   ifstream in( buffcraft.in , ios::in);   ofstream out( buffcraft.out , ios::out);   int pl, pr, vl, vr, k;   vector<int> p;   void dfs(long long x) {     if (x < 1000000000) {       p.push_back(x);       dfs(x * 10 + 4);       dfs(x * 10 + 7);     }   }   int main() {     dfs(0);     p.push_back(INT_MAX);     sort(p.begin(), p.end());     cin >> pl >> pr >> vl >> vr >> k;     double cnt = 0;     double all = ((vr - vl) + 1.0) * ((pr - pl) + 1.0);     for (int i = 1; i < p.size() - k; i++) {       int lm = p[i - 1] + 1, l = p[i], r = p[i + k - 1], rp = p[i + k] - 1;       if (pl <= l && pr >= lm && vl <= rp && vr >= r)         cnt +=             (min(pr, l) - max(pl, lm) + 1.0) * (min(vr, rp) - max(vl, r) + 1.0);       if (vl <= l && vr >= lm && pl <= rp && pr >= r) {         cnt +=             (min(vr, l) - max(vl, lm) + 1.0) * (min(pr, rp) - max(pl, r) + 1.0);         if (k == 1) cnt -= 1.0;       }     }     cout << setprecision(10) << cnt / all;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 256;   int n, a[MAXN], k, ans;   list<int> L;   set<int> s, S;   multiset<int> M;   int main() {     cin >> n >> k;     for (int i = 1; i <= n; ++i) {       cin >> a[i];       if (i >= k) s.insert(a[i]);     }     if (s.size() > 1) {       cout << -1;       return 0;     }     ans = k - 1;     while (a[ans] == a[k]) --ans;     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   template <class t, class u>   void chmax(t& first, u second) {     if (first < second) first = second;   }   template <class t, class u>   void chmin(t& first, u second) {     if (second < first) first = second;   }   template <class t>   using vc = vector<t>;   template <class t>   using vvc = vc<vc<t>>;   using pi = pair<ll, ll>;   using vi = vc<ll>;   template <class t, class u>   ostream& operator<<(ostream& os, const pair<t, u>& p) {     return os <<  {  << p.first <<  ,  << p.second <<  } ;   }   template <class t>   ostream& operator<<(ostream& os, const vc<t>& v) {     os <<  { ;     for (auto e : v) os << e <<  , ;     return os <<  } ;   }   using uint = unsigned;   using ull = unsigned long long;   template <class t, size_t n>   ostream& operator<<(ostream& os, const array<t, n>& first) {     return os << vc<t>(first.begin(), first.end());   }   template <ll i, class T>   void print_tuple(ostream&, const T&) {}   template <ll i, class T, class H, class... Args>   void print_tuple(ostream& os, const T& t) {     if (i) os <<  , ;     os << get<i>(t);     print_tuple<i + 1, T, Args...>(os, t);   }   template <class... Args>   ostream& operator<<(ostream& os, const tuple<Args...>& t) {     os <<  { ;     print_tuple<0, tuple<Args...>, Args...>(os, t);     return os <<  } ;   }   template <class t>   void print(t x, ll suc = 1) {     cout << x;     if (suc == 1) cout <<   n ;     if (suc == 2) cout <<    ;   }   ll read() {     ll i;     cin >> i;     return i;   }   vi readvi(ll n, ll off = 0) {     vi v(n);     for (ll i = ll(0); i < ll(n); i++) v[i] = read() + off;     return v;   }   template <class T>   void print(const vector<T>& v, ll suc = 1) {     for (ll i = ll(0); i < ll(v.size()); i++)       print(v[i], i == ll(v.size()) - 1 ? suc : 2);   }   string readString() {     string s;     cin >> s;     return s;   }   template <class T>   T sq(const T& t) {     return t * t;   }   void yes(bool ex = true) {     cout <<  Yes           <<   n ;     if (ex) exit(0);   }   void no(bool ex = true) {     cout <<  No           <<   n ;     if (ex) exit(0);   }   void possible(bool ex = true) {     cout <<  Possible           <<   n ;     if (ex) exit(0);   }   void impossible(bool ex = true) {     cout <<  Impossible           <<   n ;     if (ex) exit(0);   }   constexpr ll ten(ll n) { return n == 0 ? 1 : ten(n - 1) * 10; }   const ll infLL = LLONG_MAX / 3;   const ll inf = infLL;   ll topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }   ll topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }   ll botbit(signed first) { return first == 0 ? 32 : __builtin_ctz(first); }   ll botbit(ll first) { return first == 0 ? 64 : __builtin_ctzll(first); }   ll popcount(signed t) { return __builtin_popcount(t); }   ll popcount(ll t) { return __builtin_popcountll(t); }   bool ispow2(ll i) { return i && (i & -i) == i; }   ll mask(ll i) { return (ll(1) << i) - 1; }   bool inc(ll first, ll second, ll c) { return first <= second && second <= c; }   template <class t>   void mkuni(vc<t>& v) {     sort(v.begin(), v.end());     v.erase(unique(v.begin(), v.end()), v.end());   }   ll rand_int(ll l, ll r) {     static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());     return uniform_int_distribution<ll>(l, r)(gen);   }   template <class t>   void myshuffle(vc<t>& first) {     for (ll i = ll(0); i < ll(ll(first.size())); i++)       swap(first[i], first[rand_int(0, i)]);   }   template <class t>   ll lwb(const vc<t>& v, const t& first) {     return lower_bound(v.begin(), v.end(), first) - v.begin();   }   vi waf(vi x) {     sort(x.begin(), x.end());     vi y{0};     for (auto v : x) y.push_back(y.back() + v);     return y;   }   signed main() {     cin.tie(0);     ios::sync_with_stdio(0);     cout << fixed << setprecision(20);     ll n;     cin >> n;     vi pri(n + 1, 1), tot(n + 1);     vi al, od;     iota(tot.begin(), tot.end(), 0);     for (ll i = ll(2); i < ll(n + 1); i++)       if (pri[i]) {         for (ll j = i; j <= n; j += i) tot[j] = tot[j] / i * (i - 1);         for (ll j = i * 2; j <= n; j += i) pri[j] = 0;       }     for (ll i = ll(2); i < ll(n + 1); i++) {       al.push_back(tot[i]);       if (i % 2) od.push_back(tot[i]);     }     void(0);     void(0);     al = waf(al);     od = waf(od);     ll k;     cin >> k;     ll ans = inf;     if (k + 1 < ll(al.size())) chmin(ans, al[k + 1]);     if (k < ll(od.size())) chmin(ans, od[k]);     ans++;     print(ans);   }


#include <bits/stdc++.h>   using namespace std;   long long ret = 0;   long long La, Ra, Ta;   long long Lb, Rb, Tb;   long long GCD = 0;   pair<long long, long long> gcd(long long a, long long b) {     if (b == 0) {       GCD = a;       return {1, 0};     }     pair<long long, long long> nxt = gcd(b, a % b);     long long x = nxt.first;     long long y = nxt.second;     long long k = a / b;     return {y, x - k * y};   }   int main() {     cout << setprecision(10);     ios::sync_with_stdio(0);     cin.tie(0);     cin >> La >> Ra >> Ta;     cin >> Lb >> Rb >> Tb;     pair<long long, long long> eq = gcd(Ta, Tb);     long long L0 = La % GCD, R0 = L0 + (Ra - La);     long long L1 = Lb % GCD, R1 = L1 + (Rb - Lb);     La = L0;     Ra = R0;     Lb = L1;     Rb = R1;     for (long long offA = -2; offA <= 2; offA++)       for (long long offB = -2; offB <= 2; offB++) {         long long N_Ra = Ra + offA * Ta;         long long N_La = La + offA * Ta;         long long N_Rb = Rb + offB * Tb;         long long N_Lb = Lb + offB * Tb;         ret = max(ret, min(N_Ra, N_Rb) - max(N_La, N_Lb) + 1);       }     cout << ret <<   n ;     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 10;   int n, m, ans, l;   int mark[MAXN], comp[MAXN], scc[MAXN], cyc[MAXN];   vector<int> edge[MAXN], adj[MAXN], radj[MAXN];   deque<int> dq;   void dfs(int source) {     mark[source]++;     ans++, comp[source] = l;     for (auto &u : edge[source])       if (!mark[u]) dfs(u);   }   void dfs1(int source) {     mark[source]++;     for (auto &u : adj[source])       if (!mark[u]) dfs1(u);     dq.push_back(source);   }   void rev(int source) {     mark[source]++;     scc[l]++;     for (auto &u : radj[source])       if (mark[u] == 1) rev(u);   }   int main() {     cin >> n >> m;     while (m--) {       int x, y;       cin >> x >> y;       edge[x].push_back(y), edge[y].push_back(x);       adj[x].push_back(y), radj[y].push_back(x);     }     for (int i = 1; i <= n; i++)       if (!mark[i]) ++l, dfs(i), ans--;     fill(mark, mark + MAXN, 0);     l = 0;     for (int i = 1; i <= n; i++)       if (!mark[i]) dfs1(i);     while (dq.size()) {       int x = dq.back();       dq.pop_back();       if (mark[x] < 2 && !cyc[comp[x]]) {         ++l, rev(x);         if (scc[l] > 1) cyc[comp[x]] = true, ans++;       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int MAX3(long long int x, long long int y, long long int z) {     long long int t = max(x, y);     return max(t, z);   }   long long int power(long long int x, long long int n) {     long long int i, ans = 1;     for (i = 1; i <= (n); i += 1) {       ans = ans * x;     }     return ans;   }   void solve() {     long long int n, i, sum = 0;     vector<long long int> a;     cin >> n;     for (i = 0; i < (n); i += 1) {       long long int x;       cin >> x;       a.push_back(x);       if (i % 2 == 0) sum += x;     }     vector<long long int> a1, a2;     long long int m1 = 0, m2 = 0;     for (i = 0; i < (n - 1); i += 1) {       long long int t = -a[i] + a[i + 1];       i++;       a1.push_back(t);     }     for (i = 1; i < (n - 1); i += 1) {       long long int t = -a[i + 1] + a[i];       i++;       a2.push_back(t);     }     long long int s = a1.size();     long long int max_so_far = 0;     long long int max_ending_here = 0;     for (i = 0; i < (s); i += 1) {       max_ending_here += a1[i];       max_so_far = max(max_so_far, max_ending_here);       if (max_ending_here < 0) max_ending_here = 0;     }     m1 = max_so_far;     max_so_far = 0;     max_ending_here = 0;     s = a2.size();     for (i = 0; i < (s); i += 1) {       max_ending_here += a2[i];       max_so_far = max(max_so_far, max_ending_here);       if (max_ending_here < 0) max_ending_here = 0;     }     m2 = max_so_far;     long long int ans = max(m1, m2);     cout << ans + sum << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<vector<int> > a(n, vector<int>(n));     int sum = 0;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         scanf( %d , &a[i][j]);         if (i == j) sum += a[i][j];       }     }     sum &= 1;     int q;     scanf( %d , &q);     const char *cc =  01 ;     while (q--) {       int t;       scanf( %d , &t);       if (t == 3) {         printf( %c , cc[sum]);       } else {         scanf( %d , &t);         sum ^= 1;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   vector<vector<int>> g;   vector<int> deg;   ll ans;   int n, m;   void add_edge(int x, int y) {     if (x > y) {       int t = x;       x = y;       y = t;     }     g[x].push_back(y);     deg[x]++;     deg[y]++;   }   void init() {     scanf( %d %d , &n, &m);     deg.resize(n, 0);     g.resize(n, vector<int>());     ans = 0;     for (int i = 0; i < m; i++) {       int x, y;       scanf( %d%d , &x, &y);       x--;       y--;       add_edge(x, y);     }     ans = 0;     for (int i = 0; i < n; i++)       ans += (ll)g[i].size() * (ll)(deg[i] - g[i].size());   }   void work() {     int Q;     cout << ans << endl;     scanf( %d , &Q);     for (int qi = 0; qi < Q; qi++) {       int x;       scanf( %d , &x);       x--;       ans -= (ll)g[x].size() * (ll)(deg[x] - g[x].size());       ans += 0;       for (int y : g[x]) {         ll y_size = g[y].size();         ans -= (ll)y_size * (ll)(deg[y] - y_size);         g[y].push_back(x);         y_size++;         ans += (ll)y_size * (ll)(deg[y] - y_size);       }       g[x].clear();       printf( %lld n , ans);     }   }   int main() {     init();     work();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class c>   struct rge {     c b, e;   };   template <class c>   rge<c> range(c i, c j) {     return rge<c>{i, j};   }   template <class c>   auto dud(c *x) -> decltype(cerr << *x, 0);   template <class c>   char dud(...);   struct deb {     template <class c>     deb &operator<<(const c &) {       return *this;     }   };   template <class T>   T &chmin(T &a, const T &b) {     return a = min(a, b);   }   template <class T>   T &chmax(T &a, const T &b) {     return a = max(a, b);   }   const long long dx[8] = {0, 1, -1, 0, 1, 1, -1, -1};   const long long dy[8] = {1, 0, 0, -1, 1, -1, -1, 1};   const long long mod = 1e9 + 7;   inline long long add(long long a, long long b) {     return ((a % mod) + (b % mod)) % mod;   }   inline long long sub(long long a, long long b) {     return ((a % mod) - (b % mod) + mod) % mod;   }   inline long long mul(long long a, long long b) {     return ((a % mod) * (b % mod)) % mod;   }   template <typename T>   istream &operator>>(istream &is, vector<T> &vec) {     for (auto &v : vec) is >> v;     return is;   }   const double PI = acos(-1.0);   const long long inf = 1e18;   const long long maxn = 1e3 + 5;   void solve() {     long long n;     cin >> n;     vector<long long> arr(2 * n);     cin >> arr;     cin >> arr;     sort(arr.begin(), arr.end());     long long s1 = 0;     long long s2 = 0;     for (long long i = 0; i < n; i++) s1 += arr[i];     for (long long i = n; i < 2 * n; i++) s2 += arr[i];     if (s1 != s2) {       for (long long i = 0; i < 2 * n; i++) {         cout << arr[i] <<    ;       }       cout <<   n ;     } else {       cout << -1 <<   n ;     }   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t = 1;     for (long long tt = 1; tt <= t; tt++) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1);   const double eps = 1e-9;   const int inf = 2000000000;   const long long infLL = 9000000000000000000;   inline bool checkBit(long long n, int i) { return n & (1LL << i); }   inline long long setBit(long long n, int i) {     return n | (1LL << i);     ;   }   inline long long resetBit(long long n, int i) { return n & (~(1LL << i)); }   int dx[] = {0, 0, +1, -1};   int dy[] = {+1, -1, 0, 0};   inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }   inline bool isLeapYear(long long year) {     return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);   }   inline void normal(long long &a) {     a %= 1000000007;     (a < 0) && (a += 1000000007);   }   inline long long modMul(long long a, long long b) {     a %= 1000000007, b %= 1000000007;     normal(a), normal(b);     return (a * b) % 1000000007;   }   inline long long modAdd(long long a, long long b) {     a %= 1000000007, b %= 1000000007;     normal(a), normal(b);     return (a + b) % 1000000007;   }   inline long long modSub(long long a, long long b) {     a %= 1000000007, b %= 1000000007;     normal(a), normal(b);     a -= b;     normal(a);     return a;   }   inline long long modPow(long long b, long long p) {     long long r = 1;     while (p) {       if (p & 1) r = modMul(r, b);       b = modMul(b, b);       p >>= 1;     }     return r;   }   inline long long modInverse(long long a) { return modPow(a, 1000000007 - 2); }   inline long long modDiv(long long a, long long b) {     return modMul(a, modInverse(b));   }   inline long long power(long long bs, long long k) {     long long x = 1LL, y = bs;     if (k == 0) return 1LL;     while (k > 0) {       if (k % 2) x *= y;       y *= y;       k /= 2;     }     return x;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     cout.unsetf(ios::floatfield);     cout.precision(10);     cout.setf(ios::fixed, ios::floatfield);     ;     long long n;     cin >> n;     if (n % 2)       cout <<  black ;     else {       cout <<  white  <<   n ;       cout <<  1 2 ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MAX = 1e3 + 20;   long long inf = (long long)1e15;   long long mod = (long long)1e9 + 7;   long long n, cntr[MAX], cntc[MAX], l[MAX][MAX], used[MAX];   void swpr(long long ind1, long long ind2) {     swap(cntr[ind1], cntr[ind2]);     for (int i = 1; i <= n; i++) swap(l[ind1][i], l[ind2][i]);   }   void swpc(long long ind1, long long ind2) {     swap(cntc[ind1], cntc[ind2]);     for (int i = 1; i <= n; i++) swap(l[i][ind1], l[i][ind2]);   }   void solve() {     cin >> n;     for (int i = 0; i < n - 1; i++) {       long long x, y;       cin >> x >> y;       l[x][y] = 1;       cntr[x]++;       cntc[y]++;     }     vector<pair<long long, pair<long long, long long>>> ans;     long long need = 0;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         if (cntr[j] == need && !used[j]) {           used[i] = 1;           need = 0;           if (i != j) {             swpr(i, j);             ans.push_back({1, {i, j}});           }           break;         }       }       need++;     }     for (int i = 2; i <= n; i++) {       long long start = (i - 1) - cntr[i] + 1;       long long bord = start;       while (bord <= n) {         if (l[i][bord] == 1) {           if (bord != start) {             swpc(start, bord);             ans.push_back({2, {start, bord}});           }           start++;         }         bord++;       }     }     cout << ans.size() <<   n ;     for (int i = 0; i < ans.size(); i++) {       cout << ans[i].first <<     << ans[i].second.first <<                << ans[i].second.second <<   n ;     }   }   int main() {     mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());     ios::sync_with_stdio(0);     long long q;     q = 1;     while (q--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     string s;     cin >> s;     size_t pos1 = s.find( AB ), pos2 = s.find( BA );     string tmp = s;     s.erase(0, pos1 + 2);     tmp.erase(0, pos2 + 2);     bool check = false;     if (pos1 == string::npos || pos2 == string::npos) {       cout <<  NO ;     } else {       if (s.find( BA ) != string::npos || tmp.find( AB ) != string::npos)         check = true;       if (check)         cout <<  YES ;       else         cout <<  NO ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string mask(int a) {     string ans;     int x = a;     while (x) {       if (x % 10 == 4 || x % 10 == 7) ans.insert(ans.begin(), x % 10 +  0 );       x /= 10;     }     return ans;   }   int main() {     string b;     int a;     cin >> a >> b;     a++;     while (mask(a) != b) a++;     cout << a;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, c, d, e, f, mn1, mn2, res, ans1, ans2;     cin >> a >> b >> c >> d >> e >> f;     res = d;     mn2 = min(b, min(c, d));     d = abs(mn2 - d);     mn1 = min(a, d);     ans1 = (mn2 * f) + (mn1 * e);     mn1 = min(a, res);     res = abs(mn1 - res);     mn2 = min(b, min(c, res));     ans2 = (mn2 * f) + (mn1 * e);     cout << max(ans1, ans2) << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 10;   int node[4][11][11][maxn];   int h[110];   char s[maxn], e[11];   int lowbit(int x) { return x & (-x); }   void add(int ty, int pos, int x, int d) {     for (; x < maxn; x += lowbit(x)) {       for (int k = 1; k <= 10; k++) {         node[ty][k][pos % k][x] += d;       }     }   }   int sum(int ty, int len, int pos, int x) {     int ans = 0;     for (; x > 0; x -= lowbit(x)) {       ans += node[ty][len][pos][x];     }     return ans;   }   int query(int l, int r, int ty, int len, int pos) {     return sum(ty, len, pos, r) - sum(ty, len, pos, l - 1);   }   int main() {     int n;     h[ A ] = 0;     h[ T ] = 1;     h[ C ] = 2;     h[ G ] = 3;     scanf( %s%d , s, &n);     int len = strlen(s);     for (int i = 0; i < len; i++) {       add(h[s[i]], i, i + 1, 1);     }     while (n--) {       int a, b, c;       scanf( %d , &a);       if (a == 1) {         scanf( %d%s , &b, e);         add(h[s[b - 1]], b - 1, b, -1);         add(h[e[0]], b - 1, b, 1);         s[b - 1] = e[0];       } else {         scanf( %d%d%s , &b, &c, e);         len = strlen(e);         int ans = 0;         for (int i = 0; i < len; i++) {           ans += query(b, c, h[e[i]], len, (i + b - 1) % len);         }         printf( %d n , ans);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void die(string S) {     puts(S.c_str());     exit(0);   }   int number[333][333];   int cnt[100100];   vector<int> V[100100];   int grid[333][333];   int main() {     int t;     cin >> t;     while (t--) {       int n, m;       cin >> n >> m;       vector<int> v;       for (int i = 0; i < n * m; i++) {         int x;         cin >> x;         v.push_back(x);         number[i / m][i % m] = x;         cnt[i] = 0;         V[i].clear();       }       sort((v).begin(), (v).end());       for (int i = 0; i < n; i++)         for (int j = 0; j < m; j++) {           number[i][j] =               lower_bound((v).begin(), (v).end(), number[i][j]) - v.begin();           cnt[number[i][j]]++;           V[number[i][j]].push_back(i * m + j);         }       int pos = 0;       for (int i = 0; i < n * m; i++)         if (cnt[i]) {           int endp = (pos + cnt[i] - 1);           if (endp / m == pos / m) {             sort((V[i]).rbegin(), (V[i]).rend());             for (int j = 0; j < cnt[i]; j++)               grid[(pos + j) / m][(pos + j) % m] = V[i][j];           } else {             sort((V[i]).begin(), (V[i]).end());             int x = m - pos % m;             for (int j = x - 1; j >= 0; j--)               grid[(pos + x - j - 1) / m][(pos + x - j - 1) % m] = V[i][j];             x = endp % m + 1;             for (int j = cnt[i] - 1; j >= cnt[i] - x; j--)               grid[(endp / m * m + cnt[i] - j - 1) / m]                   [(endp / m * m + cnt[i] - j - 1) % m] = V[i][j];             for (int j = m - pos % m; j < cnt[i] - x; j++)               grid[(pos + j) / m][(pos + j) % m] = V[i][j];             for (int j = pos / m + 1; j < endp / m; j++)               reverse(grid[j], grid[j] + m);           }           pos += cnt[i];         }       int ans = 0;       for (int i = 0; i < n; i++)         for (int j = 0; j < m; j++)           for (int k = j + 1; k < m; k++)             if (grid[i][j] < grid[i][k]) ans++;       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxN = 10 + 5;   char a[maxN][maxN][maxN];   int mark[maxN][maxN][maxN];   int k, n, m;   int dx[] = {-1, 1, 0, 0, 0, 0};   int dy[] = {0, 0, -1, 1, 0, 0};   int dz[] = {0, 0, 0, 0, -1, 1};   int cnt;   void dfs(int x, int y, int z) {     mark[x][y][z] = 1;     cnt++;     for (int i = 0; i < 6; i++) {       int r = x + dx[i], c = y + dy[i], t = z + dz[i];       if (r < 0 || c < 0 || t < 0) continue;       if (r >= k || c >= n || t >= m) continue;       if (!mark[r][c][t] && a[r][c][t] ==  . ) dfs(r, c, t);     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> k >> n >> m;     for (int i = 0; i < k; i++)       for (int j = 0; j < n; j++)         for (int t = 0; t < m; t++) cin >> a[i][j][t];     int x, y;     cin >> x >> y;     x--;     y--;     dfs(0, x, y);     cout << cnt << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     string s;     cin >> s;     int n = s.size();     int cz = 0;     int co = 0;     int ct = 0;     for (int i = 0; i < n; i++) {       co += (s[i] ==  1 );       ct += (s[i] ==  2 );       cz += (s[i] ==  0 );     }     if (cz == 0 || ct == 0) {       sort(s.begin(), s.end());       cout << s <<   n ;       return 0;     }     if (co == 0) {       cout << s <<   n ;       return 0;     }     string aux =   ;     int idx = -1;     for (int i = 0; i < n; i++) {       if (s[i] !=  1 ) aux += s[i];     }     string res =   ;     int occ = -1;     for (int i = 0; i < aux.size(); i++) {       if (aux[i] ==  2  && occ == -1) {         while (co--) {           res +=  1 ;         }         res += aux[i];         occ = 1;       } else {         res += aux[i];       }     }     cout << res <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mx = (int)2e5 + 5;   int n, c[mx], ans[mx];   char a[mx], b[mx];   void solve() {     int cnt = 0, temp = 0;     for (int i = n - 1; i >= 0; i--) {       c[cnt++] = (a[i] + b[i] - 2 *  a  + temp) % 26;       temp = (a[i] + b[i] - 2 *  a  + temp) / 26;     }     c[cnt++] = temp;     temp = 0;     for (int i = cnt - 1; i >= 0; i--) {       temp *= 26;       ans[i] = (c[i] + temp) / 2;       temp = (c[i] + temp) % 2;     }     for (int i = n - 1; i >= 0; i--) {       printf( %c , ans[i] +  a );     }     cout << endl;   }   int main() {     cin >> n;     scanf( %s%s , a, b);     solve();   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b;     int c = 0;     cin >> a >> b;     while (a > 0 && b > 0) {       if (a > b) {         a -= 2;         b -= 1;       } else {         a -= 1;         b -= 2;       }       if (a > -1 && b > -1) c++;     }     cout << c;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 998244353, MAX = 100003, INF = 1 << 30;   long long rui(long long a, long long b) {     if (b == 1)       return a % mod;     else if (b == 0)       return 1;     else if (b % 2 == 0)       return (rui(a, b / 2) * rui(a, b / 2)) % mod;     else       return (rui(a, b - 1) * a) % mod;   }   int main() {     int Q;     cin >> Q;     for (int q = 0; q < Q; q++) {       int N, M;       cin >> N >> M;       vector<int> S[N], color(N, 0);       for (int i = 0; i < M; i++) {         int a, b;         cin >> a >> b;         a--;         b--;         S[a].push_back(b);         S[b].push_back(a);       }       long long ans = 1;       for (int i = 0; i < N; i++) {         if (color[i]) continue;         int odd = 1, even = 0;         color[i] = 1;         queue<int> Q;         Q.push(i);         while (!Q.empty()) {           int a = Q.front();           Q.pop();           for (int j = 0; j < S[a].size(); j++) {             int b = S[a][j];             if (color[b] == 0) {               color[b] = 3 - color[a];               if (color[b] % 2)                 odd++;               else                 even++;               Q.push(b);             } else if (color[a] == color[b]) {               ans = 0;             }           }         }         ans *= (rui(2, odd) + rui(2, even)) % mod;         ans %= mod;       }       cout << ans << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int lenA, lenB;   int A[1000004];   int B[1000004];   int idxB[1000004];   int lenP = 0, idxP = 0;   long long P[1000004 * 2];   int main() {     scanf( %d %d , &lenA, &lenB);     for (int i = 0, _n = (lenA); i < _n; ++i) scanf( %d , A + i);     memset(idxB, -1, sizeof(idxB));     for (int j = 0, _n = (lenB); j < _n; ++j) {       scanf( %d , B + j);       idxB[B[j]] = j;     }     long long offset = 0;     int res = 0;     for (int k = 0, _n = (lenA * 2); k < _n; ++k) {       long long p = idxB[A[k % lenA]];       if (p < 0) {         lenP = 0;         idxP = 0;         continue;       }       p += offset;       if (lenP > 0 && P[lenP - 1] >= p) {         p += lenB;         offset += lenB;       }       P[lenP++] = p;       idxP = lower_bound(P + idxP, P + lenP, p - lenB + 1) - P;       int len = lenP - idxP;       res = max(res, len);     }     printf( %d n , res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long t;     cin >> t;     while (t--) {       long long n, k;       cin >> n >> k;       long long d = (k / (n - 1)), r = (k % (n - 1));       if (r != 0) {         cout << (d * n) + r << endl;       } else {         cout << (d * n) - 1 << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   int n, m;   int main() {     long long ans;     int b, c, A, B, C, l;     scanf( %d%d , &n, &m);     n++;     m++;     ans = 0;     for (c = m; c <= n - m; c++) {       C = 2 * (n * n - n * c + c * c);       for (b = n - m; b >= m; b--) {         A = 2 * n - b - c;         B = b * (n + c);         l = (C - B) / A;         if (m > l) l = m - 1;         if (l >= n - m) break;         ans += n - m - l;       }     }     printf( %I64d n , ans * 3);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100005;   struct node {     int c, f, l, i;   } a[maxn], b[maxn];   bool cmp1(node a, node b) { return a.f < b.f; }   bool cmp2(node a, node b) { return a.c > b.c; }   int main() {     int n, d, s;     scanf( %d%d%d , &n, &d, &s);     int nn = n;     int m = 0;     int ss = s;     for (int i = 1; i <= n; i++) {       scanf( %d%d%d , &a[i].c, &a[i].f, &a[i].l);       a[i].i = i;       b[i] = a[i];       if (a[i].c != 0 && a[i].l >= d) {         if (m == 0 || a[m].f > a[i].f) m = i;       }     }     int ansf = 0;     int ansc = 0;     if (m != 0 && a[m].f <= s) {       s = s - a[m].f;       sort(a + 1, a + 1 + n, cmp2);       int sumc = a[1].c;       int sum = 1;       for (int i = 2; i <= n; i++) {         sum++;         sumc = sumc - 1 + a[i].c;         if (sumc == 0) break;       }       int j = 0;       for (int i = 1; i <= n; i++)         if (a[i].l >= d && a[i].i != m) {           a[++j] = a[i];         }       n = j;       sort(a + 1, a + 1 + n, cmp1);       for (int i = 1; i <= n; i++) {         if (sum == nn) break;         if (s >= a[i].f) {           sum++;           s -= a[i].f;         } else           break;       }       ansf = ss - s;       ansc = sum;     }     n = nn;     for (int i = 1; i <= n; i++) a[i] = b[i];     s = ss;     int sumc = 0;     int j = 0;     for (int i = 1; i <= n; i++)       if (a[i].c == 0 && a[i].l >= d) {         a[++j] = a[i];       }     n = j;     sort(a + 1, a + 1 + n, cmp1);     for (int i = 1; i <= n; i++) {       if (s >= a[i].f) {         sumc++;         s -= a[i].f;       } else         break;     }     if (ansc < sumc) {       ansf = ss - s;       ansc = sumc;     } else if (ansc == sumc && ansf > ss - s) {       ansf = ss - s;       ansc = sumc;     }     printf( %d %d n , ansc, ansf);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool flag;   int vis[(1 << 17)];   int n, m, k;   vector<pair<int, int> > vt[(1 << 17)];   void dfs(int u) {     for (int i = 0; i < vt[u].size(); i++) {       int v = vt[u][i].first;       int c = vt[u][i].second;       if (vis[v] == -1) {         if (c == 1)           vis[v] = vis[u];         else           vis[v] = !vis[u];         dfs(v);       }       if (c == 1 && (vis[v] != vis[u])) flag = true;       if (c == 0 && (vis[v] == vis[u])) flag = true;     }   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < m; i++) {       int u, v, c;       scanf( %d%d%d , &u, &v, &c);       vt[u].push_back(make_pair(v, c));       vt[v].push_back(make_pair(u, c));     }     memset(vis, -1, sizeof(vis));     for (int i = 1; i <= n; i++) {       if (vis[i] == -1) {         vis[i] = 0;         dfs(i);         k++;       }     }     long long ans = 1;     if (flag) {       puts( 0 );       return 0;     }     for (int i = 2; i <= k; i++) ans = ans * 2 % 1000000007;     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int main() {     pair<long long, long long> s, e;     long long t;     cin >> t;     cin >> s.first >> s.second >> e.first >> e.second;     map<char, int> mp;     string str;     cin >> str;     for (int i = 0; i < t; ++i) {       mp[str[i]]++;     }     char dir[] = { NSWE };     map<char, long long> nd;     for (int j = 0; j < 4; ++j) {       nd[dir[j]] = 0;     }     long long xdiff = abs(s.first - e.first);     if (s.first < e.first) {       nd[ E ] = xdiff;     } else if (s.first > e.first) {       nd[ W ] = xdiff;     }     long long ydiff = abs(s.second - e.second);     if (s.second < e.second) {       nd[ N ] = ydiff;     } else if (s.second > e.second) {       nd[ S ] = ydiff;     }     for (int i = 0; i < (int)str.size(); ++i) {       if (nd[str[i]] > 0) {         nd[str[i]]--;       }       int chck = 0;       for (int j = 0; j < 4; ++j) chck += nd[dir[j]] == 0;       if (chck == 4) {         cout << i + 1 << endl;         return 0;       }     }     cout << -1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   class graph {     int v;     list<int>* adj;       public:     graph(int x) {       this->v = x;       adj = new list<int>[v];     }     void add_edge(int a, int b) {       adj[a].push_back(b);       adj[b].push_back(a);     }     void count_colors(int a, int& n, int color[], int color_ar[]);   };   void graph::count_colors(int a, int& n, int color[], int color_ar[]) {     list<int>::iterator itr;     int x = 0;   }   int main() {     int n, m, c, x, y;     cin >> n >> m;     int color[100001] = {0}, Ar[100001] = {0}, g[100001] = {0};     for (int i = 0; i < n; i++) {       cin >> color[i];       g[color[i]] = 1;     }     map<pair<int, int>, int> mp;     for (int i = 0; i < m; i++) {       cin >> x >> y;       if (mp[make_pair(color[x - 1], color[y - 1])] == 0 &&           color[x - 1] != color[y - 1]) {         Ar[color[x - 1]]++;       }       if (mp[make_pair(color[y - 1], color[x - 1])] == 0 &&           color[x - 1] != color[y - 1]) {         Ar[color[y - 1]]++;       }       mp[make_pair(color[x - 1], color[y - 1])] = 1;       mp[make_pair(color[y - 1], color[x - 1])] = 1;     }     int max1 = INT_MIN, color1;     for (int i = 0; i < 100001; i++) {       if (g[i] == 1) {         if (Ar[i] > max1) {           max1 = Ar[i];           color1 = i;         }       }     }     cout << color1;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   const int M = 1e9 + 7;   int n;   double v, v2, a[N], b[N];   double x, y;   int id = 1;   int main() {     ios_base ::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> n >> v >> v2;     for (int i = 0; i < n; ++i) {       cin >> b[i];       a[i] = 1.0 * b[i] / v;     }     cin >> x >> y;     for (int i = 1; i < n; ++i) {       a[i] = a[i] + sqrt((b[i] - x) * (b[i] - x) + y * y) / v2;       if (a[i] <= a[id]) {         id = i;       }     }     cout << id + 1 <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int maxn = 1e5 + 6;   const long long int MOD = 1e9 + 7;   vector<int> prim(1000005, 1);   int fact[maxn];   long long int binomialCoeff(long long int n, long long int k) {     long long int res = 1;     if (k > n - k) k = n - k;     for (long long int i = 0; i < k; ++i) {       res *= (n - i);       res /= (i + 1);     }     return res;   }   bool isVowel(char ch) {     if (ch ==  a  || ch ==  i  || ch ==  e  || ch ==  u  || ch ==  o ) {       return true;     }     return false;   }   long long int power(long long int x, long long int i, long long int mod) {     long long int ans = 1;     while (i > 0) {       if (i & 1) ans = (ans * x) % mod;       i >>= 1;       x = (x * x) % mod;     }     return ans;   }   long long int modInverse(long long int x, long long int mod) {     return power(x, mod - 2, mod);   }   int nCr(int n, int r) {     if (n < r) {       return 0;     }     return (((fact[n] * modInverse(fact[n - r], MOD)) % MOD) *             modInverse(fact[r], MOD)) %            MOD;   }   long long int power(int x, unsigned int y) {     long long int temp;     if (y == 0) return 1;     temp = power(x, y / 2);     if (y % 2 == 0)       return temp * temp;     else       return x * temp * temp;   }   void erath(int n) {     prim[1] = 0;     prim[0] = 0;     prim[2] = 1;     for (int i = 2; i * i <= n; i++) {       if (prim[i]) {         for (int j = i * i; j <= n; j += i) {           prim[j] = 0;         }       }     }   }   long long int gcd(long long int a, long long int b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long int lcm(long long int a, long long int b) {     return (a / gcd(a, b)) * b;   }   bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {     return (a.second < b.second);   }   long long int n, m, k;   int getmid(int l, int r) { return (l + (r - l) / 2); }   struct segtree {     int siz;     vector<pair<long long int, long long int> > sums;     void init(int n) {       siz = 1;       while (siz < n) siz *= 2;       sums.assign(2 * siz, {LONG_MAX, 0});     }     void set(int i, int v, int cur, int l, int r) {       if (r - l == 1) {         sums[cur].first = v;         sums[cur].second = 1;         return;       }       int m = getmid(l, r);       if (i < m) {         set(i, v, 2 * cur + 1, l, m);       } else         set(i, v, 2 * cur + 2, m, r);       sums[cur].first = min(sums[2 * cur + 1].first, sums[2 * cur + 2].first);       if (sums[2 * cur + 1].first == sums[cur].first &&           sums[2 * cur + 2].first == sums[cur].first) {         sums[cur].second = sums[cur * 2 + 1].second + sums[cur * 2 + 2].second;       } else if (sums[2 * cur + 1].first == sums[cur].first) {         sums[cur].second = sums[cur * 2 + 1].second;       } else {         sums[cur].second = sums[cur * 2 + 2].second;       }     }     void set(int i, int v) { set(i, v, 0, 0, siz); }     pair<long long int, long long int> sum(int l, int r, int cur, int lx,                                            int rx) {       if (lx >= r || l >= rx) return {LONG_MAX, 0};       if (lx >= l && rx <= r) return {sums[cur].first, sums[cur].second};       int mid = getmid(lx, rx);       pair<long long int, long long int> s1 = sum(l, r, 2 * cur + 1, lx, mid),                                          s2 = sum(l, r, 2 * cur + 2, mid, rx);       if (s1.first < s2.first) {         return s1;       } else if (s1.first > s2.first)         return s2;       else         return {s1.first, s1.second + s2.second};     }     pair<long long int, long long int> sum(int l, int r) {       return sum(l, r, 0, 0, siz);     }   };   int nCrModp(int n, int r, int p) {     if (r > n - r) r = n - r;     int C[r + 1];     memset(C, 0, sizeof(C));     C[0] = 1;     for (int i = 1; i <= n; i++) {       for (int j = min(i, r); j > 0; j--) C[j] = (C[j] + C[j - 1]) % p;     }     return C[r];   }   void ans(int top) {     cin >> n >> m >> k;     long long int mx = max(n, m), mn = min(m, n);     if ((mn + (mx - mn)) > k) {       cout << -1 << endl;       return;     }     if (m == n) {       long long int add = k + 1 - n;       if (add % 2 == 0) add -= 2;       cout << (mn - 1 + add) << endl;       return;     }     long long int rem = mx - mn;     k -= mn;     if (rem & 1) {       cout << (mn + k - 1) << endl;     } else {       if (k & 1) k -= 2;       cout << (mn + k) << endl;     }   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int t = 1, co = 1;     cin >> t;     while (t--) {       ans(co);       co++;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct edge {     int v, w;   };   long long MOD = 1000000007;   long long _MOD = 1000000009;   double EPS = 1e-10;   long long extgcd(long long a, long long b, long long& x, long long& y) {     if (b == 0) {       x = (a >= 0) ? 1 : -1;       y = 0;       return abs(a);     } else {       long long res = extgcd(b, a % b, y, x);       y -= (a / b) * x;       return res;     }   }   long long mod_inverse(long long a, long long m) {     long long x, y;     extgcd(a, m, x, y);     return (m + x % m) % m;   }   long long f(int l, int r, vector<long long>& h, vector<long long>& zi) {     long long x = (h[r] - h[l]) * zi[l] % MOD;     return (x + MOD) % MOD;   }   int main() {     int N, M;     cin >> N >> M;     vector<pair<long long, char> > a, b;     while (N--) {       int l;       char c;       scanf( %d-%c , &l, &c);       if (a.size() && a.back().second == c)         a.back().first += l;       else         a.push_back(pair<long long, char>(l, c));     }     while (M--) {       int l;       char c;       scanf( %d-%c , &l, &c);       if (b.size() && b.back().second == c)         b.back().first += l;       else         b.push_back(pair<long long, char>(l, c));     }     N = a.size();     M = b.size();     if (M == 1) {       long long ans = 0;       for (int i = 0; i < N; i++)         if (a[i].second == b[0].second)           ans += max(0LL, a[i].first - b[0].first + 1);       cout << ans << endl;       return 0;     }     vector<pair<long long, char> > v;     for (pair<long long, char> x : a) v.push_back(x);     for (pair<long long, char> x : b) v.push_back(x);     sort(v.begin(), v.end());     v.erase(unique(v.begin(), v.end()), v.end());     vector<int> A(N), B(M);     for (int i = 0; i < N; i++)       A[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin() + 1;     for (int j = 0; j < M; j++)       B[j] = lower_bound(v.begin(), v.end(), b[j]) - v.begin() + 1;     vector<long long> z(200001), zi(200001);     z[0] = zi[0] = 1;     for (int i = 1; i <= 200000; i++) {       z[i] = z[i - 1] * 523541 % MOD;       zi[i] = mod_inverse(z[i], MOD);     }     vector<long long> ha(N + 1), hb(M + 1);     for (int i = 0; i < N; i++) ha[i + 1] = (ha[i] + A[i] * z[i]) % MOD;     for (int j = 0; j < M; j++) hb[j + 1] = (hb[j] + B[j] * z[j]) % MOD;     int cnt = 0;     for (int i = 0; i + M <= N; i++) {       if (!(a[i].second == b[0].second && a[i].first >= b[0].first)) continue;       if (!(a[i + M - 1].second == b[M - 1].second &&             a[i + M - 1].first >= b[M - 1].first))         continue;       if (f(i + 1, i + M - 1, ha, zi) != f(1, M - 1, hb, zi)) continue;       cnt++;     }     cout << cnt << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = ((long long)1E9 + 7);   template <class C>   void mini(C& a, C b) {     a = min(a, b);   }   template <class C>   void maxi(C& a, C b) {     a = max(a, b);   }   const int LG = 20;   struct node {     int par;     vector<int> inc;     bool monk;     int aim;     vector<pair<int, int> > mx;     int up[LG];     int depth;     int dlow, dup;     node() : par(-1), inc(), monk(false), aim(-1), depth(0), dlow(0), dup(0) {}   };   struct dart {     bool down;     int rev;     int dist;     int head;     int far;     list<int> pil;     dart(int _head = 0, int _dist = 0)         : down(false), rev(0), dist(_dist), head(_head), far(-INF), pil() {}   };   const int MN = 100100;   node T[MN];   dart D[2 * MN];   void assign(int i, int up, int d = 0) {     T[i].par = up;     T[i].depth = d;     for (__typeof((T[i].inc).begin()) it = ((T[i].inc).begin());          it != (T[i].inc).end(); ++it) {       if (*it == up) continue;       D[*it].down = true;       assign(D[*it].head, D[*it].rev, d + 1);     }   }   void distUp(int i) {     int mx = -INF;     int up = T[i].par;     for (__typeof((T[i].inc).begin()) it = ((T[i].inc).begin());          it != (T[i].inc).end(); ++it) {       if (*it == up) continue;       distUp(D[*it].head);       maxi(mx, D[*it].far);     }     if (T[i].monk) maxi(mx, 0);     if (up != -1) {       D[D[up].rev].far = mx + D[up].dist;     }   }   void distDown(int i) {     int up = T[i].par;     T[i].mx.push_back((up != -1) ? pair<int, int>(D[up].far, up)                                  : pair<int, int>(-INF, -1));     T[i].mx.push_back((T[i].monk) ? pair<int, int>(0, -1)                                   : pair<int, int>(-INF, -1));     T[i].mx.push_back(pair<int, int>(-INF, -1));     sort((T[i].mx).begin(), (T[i].mx).end(), greater<pair<int, int> >());     for (__typeof((T[i].inc).begin()) it = ((T[i].inc).begin());          it != (T[i].inc).end(); ++it) {       if (*it == up) continue;       if (D[*it].far > T[i].mx[2].first) {         T[i].mx[2] = make_pair(D[*it].far, *it);         sort((T[i].mx).begin(), (T[i].mx).end(), greater<pair<int, int> >());       }     }     for (__typeof((T[i].inc).begin()) it = ((T[i].inc).begin());          it != (T[i].inc).end(); ++it) {       if (*it == up) continue;       int j = (*it == T[i].mx[0].second);       D[D[*it].rev].far = T[i].mx[j].first + D[*it].dist;       distDown(D[*it].head);     }   }   void goUp(int i) {     int up = T[i].par;     if (T[i].monk) {       if (T[i].mx[0].first == T[i].mx[1].first)         T[i].aim = i;       else {         int rv = D[T[i].mx[0].second].rev;         D[rv].pil.insert(D[rv].pil.end(), i);       }     }     for (__typeof((T[i].inc).begin()) it = ((T[i].inc).begin());          it != (T[i].inc).end(); ++it) {       if (*it == up) continue;       goUp(D[*it].head);       int j = 0, k = 1;       if (T[i].mx[0].second == *it)         j = 1, k = 2;       else if (T[i].mx[1].second == *it)         k = 2;       int a = T[i].mx[j].second;       int b = T[i].mx[k].second;       if (a == -1 || (b != -1 && D[a].far == D[b].far)) {         for (__typeof((D[*it].pil).begin()) jt = ((D[*it].pil).begin());              jt != (D[*it].pil).end(); ++jt) {           T[*jt].aim = i;         }       } else {         D[D[a].rev].pil.splice(D[D[a].rev].pil.end(), D[*it].pil);       }     }   }   void goDown(int i) {     int up = T[i].par;     if (up != -1) {       int j = 0, k = 1;       if (T[i].mx[0].second == up)         j = 1, k = 2;       else if (T[i].mx[1].second == up)         k = 2;       int a = T[i].mx[j].second;       int b = T[i].mx[k].second;       if (a == -1 || (b != -1 && D[a].far == D[b].far)) {         for (__typeof((D[up].pil).begin()) jt = ((D[up].pil).begin());              jt != (D[up].pil).end(); ++jt) {           T[*jt].aim = i;         }       } else {         D[D[a].rev].pil.splice(D[D[a].rev].pil.end(), D[up].pil);       }     }     for (__typeof((T[i].inc).begin()) it = ((T[i].inc).begin());          it != (T[i].inc).end(); ++it) {       if (*it != up) goDown(D[*it].head);     }   }   void add(int i, int j) {     if (T[i].depth < T[j].depth) swap(i, j);     T[i].dlow++;     T[j].dlow++;     int dd = T[i].depth - T[j].depth;     for (int k = 0; k < (LG); ++k) {       if (dd & (1 << k)) {         i = T[i].up[k];       }     }     for (int k = (LG - 1); k >= (0); --k) {       if (T[i].up[k] != T[j].up[k]) {         i = T[i].up[k];         j = T[j].up[k];       }     }     if (i != j) {       i = T[i].up[0];       j = T[j].up[0];     }     T[i].dup++;   }   int bst = 0;   int cnt = 0;   int final(int i) {     int v = 0;     for (__typeof((T[i].inc).begin()) it = ((T[i].inc).begin());          it != (T[i].inc).end(); ++it) {       if (*it != T[i].par) v += final(D[*it].head);     }     v += T[i].dlow;     v -= T[i].dup;     if (!T[i].monk) {       if (v > bst) {         bst = v;         cnt = 0;       }       if (v == bst) ++cnt;     }     return v - T[i].dup;   }   int main() {     ios_base::sync_with_stdio(false);     int n, m;     cin >> n >> m;     for (int i = 0; i < (m); ++i) {       int x;       cin >> x;       --x;       T[x].monk = true;     }     for (int i = 0; i < (n - 1); ++i) {       int a, b, c;       cin >> a >> b >> c;       --a;       --b;       D[2 * i] = dart(b, c);       T[a].inc.push_back(2 * i);       D[2 * i + 1] = dart(a, c);       T[b].inc.push_back(2 * i + 1);     }     for (int i = 0; i < (2 * (n - 1)); ++i) D[i].rev = i ^ 1;     assign(0, -1);     distUp(0);     distDown(0);     goUp(0);     goDown(0);     for (int i = 0; i < (n); ++i) {       for (int j = 0; j < (LG); ++j) T[i].up[j] = -1;       int up = T[i].par;       if (up != -1) T[i].up[0] = D[up].head;     }     for (int j = 0; j < (LG - 1); ++j) {       for (int i = 0; i < (n); ++i) {         if (T[i].up[j] != -1) T[i].up[j + 1] = T[T[i].up[j]].up[j];       }     }     for (int i = 0; i < (n); ++i) {       if (T[i].monk) add(i, T[i].aim);     }     final(0);     cout << bst <<     << cnt << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int Set(int N, int pos) { return N = N | (1 << pos); }   int reset(int N, int pos) { return N = N & ~(1 << pos); }   bool check(long long N, int pos) { return (bool)(N & (1LL << pos)); }   int k, n;   int ase[30];   string arr[2505];   vector<int> pos;   vector<int> adj[5005];   vector<string> vec;   int main() {     cin >> k >> n;     for (int i = 1; i <= k; i++) {       cin >> arr[i];     }     if (n == 1) {       cout << -1 << endl;       return 0;     }     if (n == 2) {       swap(arr[1][0], arr[1][1]);       string s1 = arr[1];       swap(arr[1][0], arr[1][1]);       for (int i = 1; i <= k; i++) {         if (arr[i] != arr[1]) {           cout << -1 << endl;           return 0;         }       }       cout << s1 << endl;       return 0;     }     if (k == 1) {       swap(arr[1][0], arr[1][1]);       cout << arr[1] << endl;       return 0;     }     bool hobe = true, first = true;     int nul = 0;     for (int i = 2; i <= k; i++) {       int cnt = 0;       for (int j = 0; j < n; j++) {         if (arr[1][j] != arr[i][j]) {           if (first) pos.push_back(j);           adj[i].push_back(j);           cnt++;         }       }       if (adj[i].size() == 0) nul++;       if (cnt == 1 || cnt > 4) hobe = false;       if (cnt > 0 && first) first = false;     }     if (nul == k - 1) {       swap(arr[1][0], arr[1][1]);       cout << arr[1] << endl;       return 0;     }     if (!hobe)       cout << -1 << endl;     else {       for (int i = 0; i < pos.size(); i++) {         for (int j = 0; j < n; j++) {           if (pos[i] == j) continue;           swap(arr[1][pos[i]], arr[1][j]);           vec.push_back(arr[1]);           swap(arr[1][pos[i]], arr[1][j]);         }       }       int mx = 0;       for (int i = 0; i < n; i++) {         ase[arr[1][i] -  a ]++;         mx = max(mx, ase[arr[1][i] -  a ]);       }       if (mx > 1) vec.push_back(arr[1]);       string ans =  -1 ;       for (int p = 0; p < vec.size(); p++) {         int cnt = 0;         for (int i = 2; i <= k; i++) {           int mis = 0;           char m1[5001], m2[5001];           for (int j = 0; j < n; j++) {             if (vec[p][j] != arr[i][j]) {               mis++;               m1[mis] = vec[p][j];               m2[mis] = arr[i][j];             }           }           if (mis == 2 && m1[1] == m2[2] && m1[2] == m2[1])             cnt++;           else if (mis == 0 && mx >= 2)             cnt++;           else             break;         }         if (cnt == k - 1) {           ans = vec[p];           break;         }       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, dx[] = {0, 0, -1, 1}, dy[] = {1, -1, 0, 0}, mp[100005], b;   int cheak(int x, int y) {     if (abs(x - y) == m || abs(x - y) == 1)       return 1;     else       return 0;   }   void dfs(int x) {     if (x == n * m) {       b = 1;       cout <<  YES  << endl;       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) printf( %d  , mp[i * m + j] + 1);         printf(  n );       }       return;     }     for (int i = x; i < n * m; i++) {       swap(mp[i], mp[x]);       if ((x / m == 0 || cheak(mp[x], mp[(x / m - 1) * m + x % m]) == 0) &&           (x % m == 0 || cheak(mp[x], mp[x / m * m + x % m - 1]) == 0)) {         dfs(x + 1);         if (b == 1) return;         swap(mp[i], mp[x]);       }     }   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < n * m; i++) mp[i] = i;     dfs(0);     if (b == 0) printf( NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct coordinate {     float x;     float y;   };   float check1(coordinate c1, coordinate c2, coordinate c3, coordinate c4,                coordinate x1) {     float ans1 = (x1.y - c1.y) + (x1.x - c1.x);     float ans2 = (x1.y - c3.y) + (x1.x - c3.x);     if (ans1 == 0 || ans2 == 0)       return 0;     else {       float ans = ans1 / ans2;       return ans;     }   }   float check2(coordinate c1, coordinate c2, coordinate c3, coordinate c4,                coordinate x1) {     float m = (c3.y - c1.y) / (c3.x - c1.x);     float ans1 = (x1.y - c1.y) - m * (x1.x - c1.x);     float ans2 = (x1.y - c2.y) - m * (x1.x - c2.x);     if (ans1 == 0 || ans2 == 0)       return 0;     else {       float ans = ans1 / ans2;       return ans;     }   }   int main() {     float n, d;     cin >> n >> d;     coordinate c1, c2, c3, c4;     c1.x = d;     c1.y = 0;     c2.x = 0;     c2.y = d;     c3.x = n;     c3.y = n - d;     c4.x = n - d;     c4.y = n;     int t;     cin >> t;     while (t--) {       coordinate x1;       cin >> x1.x >> x1.y;       float ans1 = check1(c1, c2, c3, c4, x1);       float ans2 = check2(c1, c2, c3, c4, x1);       if (ans1 < 0 && ans2 < 0)         cout <<  YES  << endl;       else if (ans1 == 0 || ans2 == 0)         cout <<  YES  << endl;       else         cout <<  NO  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long mul_mod(long long a, long long b, long long MOD) { return a * b; }   struct myhash {     long long chash[401000][2];     long long mult[401000][2];     long long b1, M1, b2, M2;     myhash(long long b1 = 1000000021, long long M1 = 1200000000000000013LL,            long long b2 = 1000000009, long long M2 = 1200000000000000011LL)         : b1(b1), M1(M1), b2(b2), M2(M2) {       calc();     }     void calc() {       mult[0][0] = mult[0][1] = 1;       for (long long i = 1; i < 401000; i++) {         mult[i][0] = mul_mod(mult[i - 1][0], b1, M1);         mult[i][1] = mul_mod(mult[i - 1][1], b2, M2);       }     }     void compute(vector<long long> &st) {       chash[0][0] = chash[0][1] = st[0] + 1;       for (int i = 1; i < (int)st.size(); i++) {         chash[i][0] = (mul_mod(chash[i - 1][0], b1, M1) + st[i] + 1);         chash[i][1] = (mul_mod(chash[i - 1][1], b2, M2) + st[i] + 1);       }     }     pair<long long, long long> substr(long long l, long long r) {       if (!l) return make_pair(chash[r][0], chash[r][1]);       long long p1 =           (chash[r][0] - mul_mod(chash[l - 1][0], mult[r - l + 1][0], M1));       long long p2 =           (chash[r][1] - mul_mod(chash[l - 1][1], mult[r - l + 1][1], M2));       return make_pair(p1, p2);     }   } h1, h2;   long long n, m, a[401000];   vector<long long> diff;   int main() {     scanf( %lld %lld , &n, &m);     for (int i = 0; i < n; i++) {       scanf( %lld , &a[i]);       if (i) diff.push_back((a[i] - a[i - 1] + m) % m);     }     diff.push_back((a[0] - a[n - 1] + m) % m);     for (int i = 0, sz = diff.size(); i < sz; i++) diff.push_back(diff[i]);     h1.compute(diff);     reverse(diff.begin(), diff.end());     h2.compute(diff);     reverse(diff.begin(), diff.end());     long long acum = 0;     for (int i = 0; i < n; i++) acum += diff[i];     assert(acum % m == 0);     vector<long long> ans;     for (int i = n; i < diff.size(); i++) {       acum += diff[i];       if (h1.substr(0, i) == h2.substr(diff.size() - i - 1, diff.size() - 1)) {         ans.push_back((2LL * a[0] + acum) % m);       }     }     sort(ans.begin(), ans.end());     ans.resize(unique(ans.begin(), ans.end()) - ans.begin());     printf( %d n , ans.size());     for (int i = 0; i < ans.size(); i++) {       printf( %lld%c , ans[i], (i + 1 == ans.size()) ?   n  :    );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1000000007;   long long mpow(long long x, long long n) {     long long res = 1;     while (n) {       if (n & 1) res = res * x % mod;       x = x * x % mod;       n /= 2;     }     return res;   }   long long s2(long long n) {     n %= mod;     return n * (n - 1) % mod;   }   long long s3(long long n) {     n %= mod;     return n * (n - 1) % mod * (n - 2) % mod;   }   long long eval(vector<long long> p, long long t) {     long long res = 0;     for (int i = p.size() - 1; i >= 0; --i) {       res = (res * t + p[i]) % mod;     }     return res;   }   int main() {     std::ios::sync_with_stdio(false);     std::cin.tie(0);     long long n;     cin >> n;     vector<long long> p(4);     long long inv2 = mpow(2, mod - 2);     long long inv3 = mpow(3, mod - 2);     p[3] = (inv3) % mod;     p[2] = (-(n + 2) % mod * inv2) % mod;     p[1] = (-inv3 + (n + 2) % mod * inv2) % mod;     p[0] = (-s3(n + 2) * inv3 + (n + 2) % mod * inv2 % mod * s2(n + 1)) % mod;     long long s = eval(p, 1);     for (long long x = 1; x * x <= n; ++x) {       s = (s + 4 * eval(p, x * x % mod)) % mod;     }     vector<vector<long long> > pws(4, vector<long long>(1000003));     for (int i = 0; i < pws.size(); ++i) {       for (long long j = 1; j < pws[i].size(); ++j) {         long long j2 = j * j % mod;         long long add = 1;         for (int t = 0; t < i; ++t) add = add * j2 % mod;         pws[i][j] = (pws[i][j - 1] + add) % mod;       }     }     long long y = sqrt(n) + 2;     for (long long x = 1; x * x < n; ++x) {       while (x * x + y * y > n) --y;       long long x2 = x * x % mod;       long long s1 = 0;       s1 = (s1 + y * p[0]) % mod;       s1 = (s1 + (x2 * y % mod + pws[1][y]) * p[1]) % mod;       s1 =           (s1 + (x2 * x2 % mod * y % mod + 2 * x2 * pws[1][y] % mod + pws[2][y]) *                     p[2]) %           mod;       s1 = (s1 + (x2 * x2 % mod * x2 % mod * y % mod +                   3 * x2 * x2 % mod * pws[1][y] % mod + 3 * x2 * pws[2][y] % mod +                   pws[3][y]) *                      p[3]) %            mod;       s = (s + 4 * s1) % mod;     }     cout << (s % mod + mod) % mod << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool cmp(int a, int b) {     if (a > b)       return true;     else       return false;   }   struct pack {     long long t, T, x, cost;   };   int main() {     long long ans = 0, n, m;     cin >> n >> m;     vector<pack> data;     for (int i = 0; i < (n); ++i) {       pack temp;       cin >> temp.t >> temp.T >> temp.x >> temp.cost;       data.push_back(temp);     }     for (int i = 0; i < (n); ++i) {       data[i].T = data[i].T - data[i].t;       if (data[i].T > 0) {         long long ans1;         if (m > data[i].T)           ans1 = data[i].cost + m * data[i].x;         else           ans1 = data[i].cost;         if (m > data[i].T) {           long long k = data[i].cost * ((m + data[i].T - 1) / data[i].T);           ans1 = min(ans1, k);         }         if (m > 2 * data[i].T) {           long long ans2 = m % data[i].T + data[i].T;           ans1 = min(ans1, data[i].cost * (1 + (m - ans2) / data[i].T) +                                ans2 * data[i].x);         }         ans += ans1;       } else {         ans += data[i].cost + m * data[i].x;       }     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f;   const long long infl = 0x3f3f3f3f3f3f3f3fLL;   template <class T>   void rd(T &a) {     char ch = getchar();     bool ng = 0;     a = 0;     for (; ch <  0  || ch >  9 ; ch = getchar())       if (ch ==  - ) ng = 1;     for (; ch >=  0  && ch <=  9 ; ch = getchar())       a = (a << 1) + (a << 3) + (ch ^  0 );     if (ng) a = ~a + 1;   }   template <class T>   void wrt(T a) {     if (a < 0) {       putchar( - );       wrt(~a + 1);     } else {       if (a > 9) wrt(a / 10);       putchar(a % 10 ^  0 );     }   }   const int N = 5e5 + 5;   int n, q, nxt[N], a[N], ans[N];   map<int, int> pos;   vector<pair<int, int> > Q[N];   int fen[N];   void upd(int p, int x) {     for (; p <= n; p += p & (-p)) fen[p] = min(fen[p], x);   }   int get(int p) {     int res = inf;     for (; p; p -= p & (-p)) res = min(res, fen[p]);     return res;   }   int main() {     rd(n), rd(q);     for (int i = 1; i <= n; ++i) rd(a[i]);     for (int i = n; i; --i) {       if (pos.count(a[i]))         nxt[i] = pos[a[i]];       else         nxt[i] = -1;       pos[a[i]] = i;     }     for (int i = 1; i <= n; ++i) fen[i] = inf;     for (int i = 1; i <= q; ++i) {       int l, r;       rd(l), rd(r);       Q[l].push_back(pair<int, int>(r, i));     }     for (int i = n; i; --i) {       if (~nxt[i]) upd(nxt[i], nxt[i] - i);       for (int j = 0; j < (int)((Q[i]).size()); ++j) {         int r = Q[i][j].first, id = Q[i][j].second;         ans[id] = get(r);       }     }     for (int i = 1; i <= q; ++i) {       if (ans[i] >= inf)         puts( -1 );       else         wrt(ans[i]), puts(  );     }     return 0;   }


#include <bits/stdc++.h>   #define PI atan(1.0)*4   #define rp(i,s,t) for (register int i = (s); i <= (t); i++)   #define RP(i,t,s) for (register int i = (t); i >= (s); i--)   #define sc(x) scanf( %d ,&x)   #define scl(x) scanf( %lld ,&x)   #define ll long long   #define ull unsigned long long   #define mst(a,b) memset(a,b,sizeof(a))   #define lson l,m,rt<<1   #define rson m+1,r,rt<<1|1   #define pii pair<int,int>   #define pll pair<ll,ll>   #define pil pair<int,ll>   #define m_p make_pair   #define p_b push_back   #define ins insert   #define era erase   #define INF 0x3f3f3f3f   #define LINF 0x3f3f3f3f3f3f3f3f   #define dg if(debug)   #define outval(a) cout <<  Debuging...|  << #a <<  :   << a <<   n ;   using namespace std;   int debug = 0;   ll gcd(ll a,ll b){    return b?gcd(b,a%b):a;   }   ll lcm(ll a,ll b){    return a/gcd(a,b)*b;   }   inline int read(){    int s=0,f=1;    char ch=getchar();    while(ch< 0 ||ch> 9 ){     if(ch== - ) f=-1;     ch=getchar();    }    while(ch>= 0 &&ch<= 9 ){     s=s*10+ch- 0 ;     ch=getchar();    }    return s*f;   }   const int N = 1e6+7;   const double eps = 1e-7;   int a[N];   int f[N];   void solve(){    int n=read(),m=read();    rp(i,1,n) a[i]=read();    rp(i,1,n) f[i]=0;    RP(i,n,1){     if(a[i]!=i) break;     else f[i]=1;    }    int flag=1;    rp(i,1,n){     if(f[i]==0){      flag=0;     }    }    double ans=0.0;    double cur=1;    int ff=0;    rp(i,1,m){     int r;double p;     scanf( %d%lf ,&r,&p);     // printf( %d %lf n ,r,p);     if(r==n||f[r+1]){      ans+=cur*p;      cur*=(1.0-p);      if(fabs(p-1.0)<=eps) ff=1;     }    }    if(ff||flag){     printf( 1.000000 n );     return ;    }    printf( %.6f n ,ans);   }   int main(){    //ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);   #ifdef ONLINE_JUDGE   #else    freopen( in.txt ,  r , stdin);    //debug = 1;   #endif    time_t beg, end;    //if(debug) beg = clock();       int T=read();    while(T--) solve();       /*    if(debug) {     end = clock();     printf( time:%.2fs n , 1.0 * (end - beg) / CLOCKS_PER_SEC);    }    */    return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, t, k, x, d[200001] = {}, last_d = 1, last = 0, a;     long long sum = 0;     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> t;       if (1 == t) {         cin >> a >> x;         sum += a * x;         if (last_d == 0) {           last += x;         } else if (last_d == a) {           last += x;         } else {           d[a] -= x;         }       } else if (2 == t) {         cin >> k;         sum += k;         d[last_d++] = k - last;         last = k;       } else {         sum -= last;         last -= d[--last_d];       }       cout << setiosflags(ios::fixed) << setprecision(6)            << ((double)sum) / ((double)last_d) << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, d;     cin >> n >> d;     int t[n], sum = 0, rest = (n - 1) * 10;     for (int i = 0; i < n; i++) {       cin >> t[i];       sum = sum + t[i];     }     if ((rest + sum) > d) {       cout <<  -1 ;       return 0;     } else {       int bal = d - sum, max;       max = bal / 5;       cout << max;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int64_t INF = 0x7FFFFFFF;   const int MAX = 1;   unordered_map<int, int> maps;   unordered_map<int, int> cnt;   int main(int argc, char* args[]) {     int n;     scanf( %d , &n);     for (int(i) = (0), _n = (n - 1); (i) <= _n; (i)++) {       int x;       scanf( %d , &x);       maps[x]++;     }     for (auto it : maps) {       int x = it.second;       for (int(i) = (1), _n = (x); (i) <= _n; (i)++) {         cnt[i]++;       }     }     int ans = 0;     for (auto it : cnt) {       ans += it.second - 1;     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool lucky(char a) {     if (a ==  4  || a ==  7 )       return true;     else       return false;   }   int main() {     long long int a, b, c, d;     vector<bool> res;     cin >> a >> b >> c >> d;     if (abs(c - d) > 1 || min(a, b) < max(c, d)) {       puts( -1 );       return 0;     }     if (c > d) {       for (long long int i = 0; i < ((long long int)(a - d)); i++) cout << 4;       for (long long int i = 0; i < ((long long int)(d)); i++) cout << 74;       for (long long int i = 0; i < ((long long int)(b - d)); i++) cout << 7;     } else if (c < d) {       cout << 7;       for (long long int i = 0; i < ((long long int)(a - c)); i++) cout << 4;       for (long long int i = 0; i < ((long long int)(c - 1)); i++) cout << 74;       for (long long int i = 0; i < ((long long int)(b - c)); i++) cout << 7;       cout << 4;     } else {       if (a + b < c + d + 1) {         puts( -1 );         return 0;       }       for (long long int i = 0; i < ((long long int)(a - c)); i++) cout << 4;       if (a - c > 0) {         for (long long int i = 0; i < ((long long int)(c - 1)); i++) cout << 74;         for (long long int i = 0; i < ((long long int)(b - c + 1)); i++)           cout << 7;         cout << 4;       } else {         cout << 7;         for (long long int i = 0; i < ((long long int)(c)); i++) cout << 47;         for (long long int i = 0; i < ((long long int)(b - 1 - c)); i++)           cout << 7;       }     }     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char buf[1 << 21], *p1 = buf, *p2 = buf;   inline int qread() {     register char c =         (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2)              ? EOF              : *p1++);     register int x = 0, f = 1;     while (c <  0  || c >  9 ) {       if (c ==  - ) f = -1;       c = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2)                ? EOF                : *p1++);     }     while (c >=  0  && c <=  9 ) {       x = (x << 3) + (x << 1) + c - 48;       c = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2)                ? EOF                : *p1++);     }     return x * f;   }   inline int Abs(const int& x) { return (x > 0 ? x : -x); }   inline int Max(const int& x, const int& y) { return (x > y ? x : y); }   inline int Min(const int& x, const int& y) { return (x < y ? x : y); }   struct Segtree {     long long sumv[800005], sumidx[800005];     inline void Modify(int p, int pl, int pr, int id) {       if (pl == pr) {         sumv[p]++;         sumidx[p] += id;         return;       }       int mid = pl + pr >> 1;       if (mid >= id)         Modify(p << 1, pl, mid, id);       else         Modify(p << 1 | 1, mid + 1, pr, id);       sumv[p] = sumv[p << 1] + sumv[p << 1 | 1];       sumidx[p] = sumidx[p << 1] + sumidx[p << 1 | 1];     }     inline int Kth(int p, int pl, int pr, int k) {       if (pl == pr) return pl;       int mid = pl + pr >> 1;       if (k <= sumv[p << 1])         return Kth(p << 1, pl, mid, k);       else         return Kth(p << 1 | 1, mid + 1, pr, k - sumv[p << 1]);     }     inline long long Sumv(int p, int pl, int pr, int l, int r) {       if (l > r) return 0;       if (pl == l && pr == r) return sumv[p];       int mid = pl + pr >> 1;       if (mid >= r)         return Sumv(p << 1, pl, mid, l, r);       else if (mid + 1 <= l)         return Sumv(p << 1 | 1, mid + 1, pr, l, r);       else         return Sumv(p << 1, pl, mid, l, mid) +                Sumv(p << 1 | 1, mid + 1, pr, mid + 1, r);     }     inline long long Sumidx(int p, int pl, int pr, int l, int r) {       if (l > r) return 0;       if (pl == l && pr == r) return sumidx[p];       int mid = pl + pr >> 1;       if (mid >= r)         return Sumidx(p << 1, pl, mid, l, r);       else if (mid + 1 <= l)         return Sumidx(p << 1 | 1, mid + 1, pr, l, r);       else         return Sumidx(p << 1, pl, mid, l, mid) +                Sumidx(p << 1 | 1, mid + 1, pr, mid + 1, r);     }   };   Segtree sgt;   int n, a[200005], idx[200005];   inline void Read() {     n = qread();     for (int i = 1; i <= n; i++) idx[a[i] = qread()] = i;   }   inline long long Sum(int l, int k) { return 1ll * k * (l + l + k - 1) / 2; }   inline void Solve() {     long long inv = 0;     for (int i = 1; i <= n; i++) {       inv += sgt.Sumv(1, 1, n, idx[i] + 1, n);       sgt.Modify(1, 1, n, idx[i]);       int midv = sgt.Kth(1, 1, n, i + 1 >> 1);       long long lsum = sgt.Sumidx(1, 1, n, 1, midv - 1),                 rsum = sgt.Sumidx(1, 1, n, midv + 1, n);       printf( %lld  , inv + Sum(midv - (i - 1 >> 1), i - 1 >> 1) - lsum + rsum -                           Sum(midv + 1, i >> 1));     }   }   int main() {     Read();     Solve();     return 0;   }