#include <bits/stdc++.h>
 using namespace std;
 namespace INIT {
 char buf[1 << 15], *fs, *ft;
 inline char getc() {
   return (fs == ft &&
           (ft = (fs = buf) + fread(buf, 1, 1 << 15, stdin), fs == ft))
              ? 0
              : *fs++;
 }
 inline int read() {
   int x = 0, f = 1;
   char ch = getc();
   while (!isdigit(ch)) {
     if (ch == '-') f = -1;
     ch = getc();
   }
   while (isdigit(ch)) {
     x = x * 10 + ch - '0';
     ch = getc();
   }
   return x * f;
 }
 }  // namespace INIT
 using namespace INIT;
 int n, p[1000010], vis[1000010], ans[1000010];
 vector<int> a[1000010], b[1000010];
 int main() {
   n = read();
   for (int i = 1; i <= n; ++i) p[i] = read();
   for (int i = 1; i <= n; ++i) {
     int num = 0;
     for (int j = i; !vis[j]; j = p[j]) vis[j] = 1, ++num, a[i].push_back(j);
     b[num].push_back(i);
   }
   for (int i = 1; i <= n; ++i)
     if (i % 2 == 0 && (b[i].size() & 1)) {
       puts( -1 );
       return 0;
     }
   memset(vis, 0, sizeof(vis));
   for (int i = 0; i <= (int)b[1].size() - 1; ++i) ans[b[1][i]] = b[1][i];
   for (int i = 2; i <= n; ++i)
     for (int j = 0; j <= (int)b[i].size() - 1; ++j) {
       if (i & 1) {
         int x = a[b[i][j]][0], y = a[b[i][j]][i / 2 + 1];
         while (!vis[x]) {
           vis[x] = vis[y] = 1;
           ans[x] = y;
           x = p[x];
           ans[y] = x;
           y = p[y];
         }
       } else {
         int x = a[b[i][j]][0], y = a[b[i][j + 1]][0];
         while (!vis[x] && !vis[y]) {
           vis[x] = vis[y] = 1;
           ans[x] = y;
           x = p[x];
           ans[y] = x;
           y = p[y];
         }
         j++;
       }
     }
   for (int i = 1; i <= n; ++i) printf( %d  , ans[i]);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int MAXN = 100010;
 const int MAXM = 20010;
 const int MAXNODE = 16 * MAXN;
 const int LOGMAXN = 50;
 const int INF = 0x3f3f3f3f;
 const double eps = 1e-9;
 int N, M;
 int b[MAXM], z[MAXM], x[3 * (MAXN + MAXM)];
 struct Tree {
   int x, h;
   double l, r;
 } a[MAXN];
 struct SegmentTree {
   double mul[MAXNODE];
   void build(int o, int L, int R) {
     mul[o] = 1;
     if (L >= R) return;
     int mid = L + (R - L) / 2;
     build(o << 1, L, mid);
     build(o << 1 | 1, mid + 1, R);
   }
   void pushdown(int o) {
     mul[o << 1] *= mul[o];
     mul[o << 1 | 1] *= mul[o];
     mul[o] = 1;
   }
   void update(int o, int L, int R, int ql, int qr, double p) {
     if (ql <= L && qr >= R) {
       mul[o] *= p;
       return;
     }
     pushdown(o);
     int mid = L + (R - L) / 2;
     if (ql <= mid) update(o << 1, L, mid, ql, qr, p);
     if (qr > mid) update(o << 1 | 1, mid + 1, R, ql, qr, p);
   }
   double query(int o, int L, int R, int pos) {
     if (L >= R) return mul[o];
     pushdown(o);
     int mid = L + (R - L) / 2;
     double ans;
     if (pos <= mid)
       ans = query(o << 1, L, mid, pos);
     else
       ans = query(o << 1 | 1, mid + 1, R, pos);
     return ans;
   }
 } tree;
 int main() {
   while (scanf( %d%d , &N, &M) != EOF) {
     int k = 0;
     for (int i = 0; i < N; i++) {
       scanf( %d%d%lf%lf , &a[i].x, &a[i].h, &a[i].l, &a[i].r);
       a[i].l /= 100;
       a[i].r /= 100;
       x[k++] = a[i].x;
       x[k++] = a[i].x - a[i].h;
       x[k++] = a[i].x + a[i].h;
     }
     for (int i = 0; i < M; i++) {
       scanf( %d%d , &b[i], &z[i]);
       x[k++] = b[i];
     }
     sort(x, x + k);
     k = unique(x, x + k) - x;
     tree.build(1, 1, k);
     for (int i = 0; i < N; i++) {
       int qm = lower_bound(x, x + k, a[i].x) - x + 1;
       int ql = lower_bound(x, x + k, a[i].x - a[i].h) - x + 1,
           qr = lower_bound(x, x + k, a[i].x + a[i].h) - x + 1;
       tree.update(1, 1, k, ql, qm - 1, (1 - a[i].l));
       tree.update(1, 1, k, qm + 1, qr, (1 - a[i].r));
     }
     double ans = 0;
     for (int i = 0; i < M; i++) {
       int pos = lower_bound(x, x + k, b[i]) - x + 1;
       ans += z[i] * tree.query(1, 1, k, pos);
     }
     printf( %.10lf
 , ans);
   }
   return 0;
 }


#include <bits/stdc++.h>
 const int N = 502;
 void sw(float* x) {
   float tmp = *x;
   *x = *(x + 2);
   *(x + 2) = *(x + 1);
   *(x + 1) = tmp;
 }
 int main() {
   int _t;
   scanf( %d , &_t);
   while (_t--) {
     static float arr[N];
     static int anss[N * N];
     static int tm[N];
     memset(tm, 0, sizeof(tm));
     int n;
     scanf( %d , &n);
     int f2 = -1;
     for (int i = 1; i <= n; i++) {
       int a;
       scanf( %d , &a);
       tm[a]++;
       if (tm[a] == 2) f2 = a;
       arr[i] = a + 0.0001 * tm[a];
     }
     int nx = 0;
     for (int i = 1; i < n; i++)
       for (int j = i + 1; j <= n; j++)
         if (arr[i] > arr[j]) nx++;
     if (nx & 1) {
       if (f2 != -1) {
         int f21 = -1, f22 = -1;
         for (int i = 1; i <= n; i++)
           if (((int)arr[i]) == f2) {
             if (f21 == -1)
               f21 = i;
             else if (f22 == -1)
               f22 = i;
             else
               break;
           }
         std::swap(arr[f21], arr[f22]);
         nx++;
       } else {
         printf( -1
 );
         continue;
       }
     }
     int ans = 0;
     while (nx) {
       for (int i = 1; i <= n - 2; i++) {
         if (arr[i] < arr[i + 2] && arr[i + 2] < arr[i + 1] ||
             arr[i] > arr[i + 2] && arr[i + 2] > arr[i + 1]) {
           sw(arr + i);
           anss[++ans] = i;
         }
         if (arr[i] < arr[i + 1] && arr[i] > arr[i + 2] ||
             arr[i] > arr[i + 1] && arr[i + 1] > arr[i + 2]) {
           sw(arr + i);
           nx -= 2;
           anss[++ans] = i;
         }
       }
     }
     printf( %d
 , ans);
     for (int i = 1; i <= ans; i++) printf( %d  , anss[i]);
     printf(
 );
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int n, m;
   cin >> n >> m;
   string test;
   cin >> test;
   for (int i = 0; i < m; i++) {
     int l, r;
     char c1, c2;
     cin >> l >> r >> c1 >> c2;
     l -= 1;
     r -= 1;
     for (; l <= r; l++) {
       if (test[l] == c1) {
         test[l] = c2;
       }
     }
   }
   cout << test << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int maxn = 1e5 + 100;
 const long long mod = 1e9 + 7;
 char s[maxn];
 int num[55];
 long long dp[maxn];
 long long tmp[maxn];
 long long quesnum[55][55];
 long long ques[55][55];
 long long inv[maxn], fac[maxn], invf[maxn];
 void init(int hf) {
   inv[1] = 1;
   for (int i = 2; i < maxn; i++)
     inv[i] = ((mod - mod / i) * inv[mod % i]) % mod;
   fac[0] = 1;
   for (int i = 1; i < maxn; i++) fac[i] = (fac[i - 1] * (long long)i) % mod;
   invf[0] = 1;
   for (int i = 1; i < maxn; i++) invf[i] = (invf[i - 1] * inv[i]) % mod;
   memset(dp, 0, sizeof(dp));
   memset(ques, -1, sizeof(ques));
   dp[0] = 1;
   for (int i = 0; i < 55; i++) {
     if (!num[i]) continue;
     for (int j = hf; j >= num[i]; j--) {
       dp[j] = (dp[j] + dp[j - num[i]]) % mod;
     }
   }
   for (int i = 0; i < 52; i++) {
     for (int j = i; j < 52; j++) {
       for (int k = 0; k <= hf; k++) tmp[k] = dp[k];
       for (int k = num[i]; k <= hf; k++)
         tmp[k] = (tmp[k] - tmp[k - num[i]] + mod) % mod;
       if (i != j)
         for (int k = num[j]; k <= hf; k++)
           tmp[k] = (tmp[k] - tmp[k - num[j]] + mod) % mod;
       quesnum[i][j] = quesnum[j][i] = tmp[hf];
     }
   }
 }
 int main() {
   scanf( %s , s);
   memset(num, 0, sizeof(num));
   int len = strlen(s);
   int hf = len / 2;
   for (int i = 0; i < len; i++) {
     if (s[i] >= 'a' && s[i] <= 'z')
       num[s[i] - 'a']++;
     else
       num[s[i] - 'A' + 26]++;
   }
   init(hf);
   int q;
   scanf( %d , &q);
   int x, y;
   while (q--) {
     scanf( %d%d , &x, &y);
     x--;
     y--;
     int idx, idy;
     if (s[x] >= 'a' && s[x] <= 'z')
       idx = s[x] - 'a';
     else
       idx = s[x] - 'A' + 26;
     if (s[y] >= 'a' && s[y] <= 'z')
       idy = s[y] - 'a';
     else
       idy = s[y] - 'A' + 26;
     if (ques[idx][idy] != -1) {
       printf( %I64d
 , ques[idx][idy]);
       continue;
     }
     long long ans;
     if (idx == idy) {
       if (num[idx] > hf) {
         printf( 0
 );
         ques[idx][idx] = 0;
         continue;
       }
       long long cc =
           ((fac[hf] * invf[num[idx]]) % mod * invf[hf - num[idx]]) % mod;
       ans = ((cc * fac[hf - num[idx]]) % mod * fac[hf]) % mod;
     } else {
       if (num[idx] + num[idy] > hf) {
         ques[idx][idy] = ques[idy][idx] = 0;
         printf( 0
 );
         continue;
       }
       int numx = num[idx], numy = num[idy];
       int num_hfx = hf - numx;
       long long cc1 = ((fac[hf] * invf[numx]) % mod * invf[hf - numx]) % mod;
       long long cc2 =
           ((fac[num_hfx] * invf[numy]) % mod * invf[num_hfx - numy]) % mod;
       ans = (((cc1 * cc2) % mod * fac[hf]) % mod * fac[hf - numx - numy]) % mod;
     }
     for (int i = 0; i < 55; i++) {
       if (i == idx || i == idy) continue;
       ans = (ans * invf[num[i]]) % mod;
     }
     ans = (ans * quesnum[idx][idy]) % mod;
     ans = (ans * 2) % mod;
     ques[idx][idy] = ques[idy][idx] = ans;
     printf( %I64d
 , ans);
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 const int MAXN = 500000 + 10;
 int a[MAXN];
 int n, m, ans, tp, tot;
 int main() {
   scanf( %d , &n);
   for (int i = 1; i <= n; ++i) scanf( %d , a + i);
   ans = n;
   sort(a + 1, a + 1 + n);
   for (int i = n / 2; i >= 1; --i)
     if (a[ans] >= a[i] * 2) --ans;
   printf( %d
 , ans);
 }


#include <bits/stdc++.h>
 using namespace std;
 char a[500001 + 10], b[500001 + 10], s[(500001 << 1) + 10];
 int n, m;
 struct BIT {
   long long c[500001 << 1];
   long long query(int x) {
     if (x <= 0) return 0;
     long long ans = 0;
     for (int i = x; i; i -= (i & -i)) ans += c[i];
     return ans;
   }
   void add(int x, long long p) {
     if (x <= 0) x = 1;
     for (int i = x; i <= 1000000; i += (i & -i)) c[i] += p;
   }
 } b1, b2;
 int za[500001 * 3], zb[500001 * 3];
 char mod[500001 * 3];
 void zfunc(char *s, int n, int *z) {
   int l = 0, r = 0;
   z[1] = n;
   int pt;
   for (int i = 2; i <= n; ++i) {
     if (r < i) {
       for (pt = i; pt <= n && s[pt] == s[pt - i + 1]; ++pt)
         ;
       --pt;
       z[i] = pt - i + 1;
       if (z[i]) l = i, r = pt;
     } else {
       int b = r - i + 1, i_ = i - l + 1;
       if (z[i_] < b)
         z[i] = z[i_];
       else {
         for (pt = r + 1; pt <= n && s[pt] == s[pt - i + 1]; ++pt)
           ;
         --pt;
         z[i] = pt - i + 1;
         l = i, r = pt;
       }
     }
   }
 }
 int main() {
   scanf( %d%d , &n, &m);
   scanf( %s%s%s , a + 1, b + 1, s + 1);
   memcpy(mod + 1, s + 1, m - 1);
   mod[m] = '$';
   memcpy(mod + m + 1, a + 1, n);
   zfunc(mod, n + m, za);
   for (int i = 1; i <= n; ++i) za[i] = za[i + m];
   reverse(s + 1, s + 1 + m);
   reverse(b + 1, b + 1 + n);
   memcpy(mod + 1, s + 1, m - 1);
   mod[m] = '$';
   memcpy(mod + m + 1, b + 1, n);
   zfunc(mod, n + m, zb);
   for (int i = 1; i <= n; ++i) zb[i] = zb[i + m];
   reverse(zb + 1, zb + 1 + n);
   int cur = 1;
   long long ans = 0;
   for (int i = 1; i <= n; ++i) {
     while (cur <= min(i + m - 2, n))
       b1.add(m - 1 - zb[cur], 1), b2.add(m - 1 - zb[cur], zb[cur]), ++cur;
     ans += 1ll * b1.query(za[i]) * (za[i] - m + 1) + b2.query(za[i]);
     b1.add(m - 1 - zb[i], -1), b2.add(m - 1 - zb[i], -zb[i]);
   }
   printf( %lld
 , ans);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int INF = (int)2e9;
 const long long INFL = (long long)9e18;
 const int MAXINT = ((~0) ^ (1 << 31));
 const long long MAXLL = ((~0) ^ ((long long)1 << 63));
 template <class T>
 inline T pow2(T a) {
   return a * a;
 }
 template <class T>
 inline bool mineq(T& a, T b) {
   return (a > b) ? (a = b, true) : false;
 }
 template <class T>
 inline bool maxeq(T& a, T b) {
   return (a < b) ? (a = b, true) : false;
 }
 const int maxn = 2e5;
 long long n, k, x, cnt;
 pair<long long, long long> p[maxn];
 int main() {
   ios_base::sync_with_stdio(0);
   cin >> n >> k;
   for (int i = 1; i <= n; i++) p[i] = {1e10, 0};
   for (int i = 1; i <= k; i++) {
     cin >> x;
     p[x].first = min(p[x].first, (long long)i);
     p[x].second = max(p[x].second, (long long)i);
   }
   if (p[1].first > p[2].second && n != 1) {
     cnt++;
   }
   if (p[n].first > p[n - 1].second && n != 1) {
     cnt++;
   }
   if (p[1].second == 0) cnt++;
   if (p[n].second == 0 && n != 1) cnt++;
   for (int i = 2; i <= n - 1; i++) {
     if (p[i].second == 0) cnt++;
     if (p[i].first > p[i + 1].second) {
       cnt++;
     }
     if (p[i].first > p[i - 1].second) {
       cnt++;
     }
   }
   cout << cnt;
 }


#include <bits/stdc++.h>
 using namespace std;
 struct pen {
   int color;
   int diameter;
 };
 int n, m, u, v;
 pen marker[100000], cap[100000];
 struct match {
   vector<pen> marker;
   vector<pen> cap;
 };
 match singleMatch[1000 + 1];
 match doubleMatch[1000 + 1][1000 + 1];
 int main(int argc, char *argv[]) {
   u = 0, v = 0;
   cin >> n >> m;
   for (int i = 0; i < n; i++) {
     int color, diameter;
     cin >> color >> diameter;
     marker[i].color = color;
     marker[i].diameter = diameter;
     singleMatch[diameter].marker.push_back(marker[i]);
     doubleMatch[diameter][color].marker.push_back(marker[i]);
   }
   for (int i = 0; i < m; i++) {
     int color, diameter;
     cin >> color >> diameter;
     cap[i].color = color;
     cap[i].diameter = diameter;
     singleMatch[diameter].cap.push_back(marker[i]);
     doubleMatch[diameter][color].cap.push_back(cap[i]);
   }
   for (int i = 1; i < 1000 + 1; i++) {
     vector<pen> tempM = singleMatch[i].marker;
     vector<pen> tempC = singleMatch[i].cap;
     int temp1 = (int)tempM.size();
     int temp2 = (int)tempC.size();
     if (temp1 > 0 && temp2 > 0) u += min(temp1, temp2);
   }
   for (int i = 1; i < 1000 + 1; i++)
     for (int j = 1; j < 1000 + 1; j++) {
       vector<pen> tempM = doubleMatch[i][j].marker;
       vector<pen> tempC = doubleMatch[i][j].cap;
       int temp1 = (int)tempM.size();
       int temp2 = (int)tempC.size();
       if (temp1 > 0 && temp2 > 0) v += min(temp1, temp2);
     }
   cout << u <<     << v << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int inf = 109;
 const int mod = 1000000007;
 long long cnt[inf];
 long long dp[inf];
 long long temp[inf][inf];
 long long sol[inf][inf];
 long long bs[inf][inf];
 int main() {
   int n, x, a;
   scanf( %d%d , &n, &x);
   for (int i = 0; i < n; ++i) {
     scanf( %d , &a);
     cnt[a]++;
   }
   dp[0] = 1;
   int all = 0;
   for (int i = 1; i <= 100; ++i) {
     for (int j = 1; j <= i; ++j) dp[i] = (dp[i] + dp[i - j] * cnt[j]) % mod;
     all = (all + dp[i]) % mod;
   }
   dp[0] = all;
   for (int i = 1; i <= 50; ++i) swap(dp[i], dp[101 - i]);
   for (int i = 1; i <= 100; ++i) sol[i][i] = 1, bs[0][i] = bs[1][i] = cnt[i];
   sol[0][0] = bs[0][0] = 1;
   for (int i = 2; i <= 100; ++i) bs[i][i - 1] = 1;
   if (x <= 100) {
     int ad = 0;
     for (int i = 100; i > 0 && x > 0; --i, --x) ad = (ad + dp[i]) % mod;
     printf( %d
 , ad + 1);
     return 0;
   }
   x -= 100;
   while (x) {
     if (x & 1) {
       for (int i = 0; i <= 100; ++i)
         for (int j = 0; j <= 100; ++j) {
           temp[i][j] = 0;
           for (int u = 0; u <= 100; ++u)
             temp[i][j] = (temp[i][j] + sol[i][u] * bs[u][j]) % mod;
         }
       for (int i = 0; i <= 100; ++i)
         for (int j = 0; j <= 100; ++j) sol[i][j] = temp[i][j];
     }
     x = x >> 1;
     for (int i = 0; i <= 100; ++i)
       for (int j = 0; j <= 100; ++j) {
         temp[i][j] = 0;
         for (int u = 0; u <= 100; ++u)
           temp[i][j] = (temp[i][j] + bs[i][u] * bs[u][j]) % mod;
       }
     for (int i = 0; i <= 100; ++i)
       for (int j = 0; j <= 100; ++j) bs[i][j] = temp[i][j];
   }
   int ans = 0;
   for (int i = 0; i <= 100; ++i) ans = (ans + sol[0][i] * dp[i]) % mod;
   printf( %d
 , (ans + 1) % mod);
 }


#include <bits/stdc++.h>
 using namespace std;
 const long long INF = 1e9 + 7;
 const int N = 4e5 + 10;
 const int M = 22;
 class SegmentTree {
  public:
   int ql, qr, qx;
   int xorv[N];
   int sumv[N];
   void init() {
     memset((xorv), (0), sizeof(xorv));
     memset((sumv), (0), sizeof(sumv));
   }
   void set(int ql, int qr, int qx) {
     this->ql = ql;
     this->qr = qr;
     this->qx = qx;
   }
   void pushdown(int o, int len) {
     if (xorv[o]) {
       xorv[o << 1] ^= 1;
       xorv[o << 1 | 1] ^= 1;
       sumv[o << 1] = (len - len / 2) - sumv[o << 1];
       sumv[o << 1 | 1] = len / 2 - sumv[o << 1 | 1];
       xorv[o] = 0;
     }
   }
   void maintain(int o) { sumv[o] = sumv[o << 1] + sumv[o << 1 | 1]; }
   int query(int l, int r, int o = 1) {
     if (ql <= l && qr >= r) {
       return sumv[o];
     }
     int mid = (l + r) >> 1;
     pushdown(o, r - l + 1);
     int ans = 0;
     if (ql <= mid) ans += query(l, mid, o << 1);
     if (qr > mid) ans += query(mid + 1, r, o << 1 | 1);
     return ans;
   }
   void update(int l, int r, int o = 1) {
     if (ql <= l && qr >= r) {
       xorv[o] ^= 1;
       sumv[o] = r - l + 1 - sumv[o];
       return;
     }
     int mid = (l + r) >> 1;
     pushdown(o, r - l + 1);
     if (ql <= mid) update(l, mid, o << 1);
     if (qr > mid) update(mid + 1, r, o << 1 | 1);
     maintain(o);
   }
 } st[M];
 int main() {
   for (int i = 0; i < M; i++) st[i].init();
   int n;
   cin >> n;
   for (int i = 1; i <= n; i++) {
     int x;
     cin >> x;
     for (int j = 0; j < M; j++) {
       if (x >> j & 1) st[j].set(i, i, 1), st[j].update(1, n);
     }
   }
   int q;
   cin >> q;
   while (q--) {
     int op;
     cin >> op;
     if (op == 1) {
       int l, r;
       cin >> l >> r;
       long long ans = 0;
       for (int i = 0; i < M; i++) {
         st[i].set(l, r, 0);
         ans += (1LL << i) * st[i].query(1, n);
       }
       cout << ans << '
';
     } else {
       int l, r, x;
       cin >> l >> r >> x;
       for (int i = 0; i < M; i++) {
         if (x >> i & 1) st[i].set(l, r, 1), st[i].update(1, n);
       }
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 struct data {
   int s, m, r, t;
   int operator<(const data &b) const { return t < b.t; }
   void get() {
     scanf( %d , &s);
     scanf( %d , &m);
     scanf( %d , &r);
     t = r ? (m / r + (m % r > 0)) : 2e9;
   }
   int calc(int o) { return min(1ll * m, s + 1ll * r * o); }
 } a[20][100005];
 int T[100005 << 2];
 long long R[20][100005], sum[20][100005], ans;
 int n, Q, t, ql, qr;
 void Build(int x, int l, int r, int d) {
   T[x] = -2;
   if (l == r) {
     a[d][l].get();
     R[d][l] = R[d][l - 1] + a[d][l].r;
     sum[d][l] = sum[d][l - 1] + a[d][l].m;
     return;
   }
   int mid = (l + r) >> 1;
   Build(x << 1, l, mid, d + 1);
   Build(x << 1 | 1, mid + 1, r, d + 1);
   for (int i = l; i <= r; i++) a[d][i] = a[d + 1][i];
   sort(a[d] + l, a[d] + r + 1);
   for (int i = l; i <= r; i++) R[d][i] = R[d][i - 1] + a[d][i].r;
   for (int i = l; i <= r; i++) sum[d][i] = sum[d][i - 1] + a[d][i].m;
 }
 void Find(int x, int l, int r, int d) {
   if (ql <= l && r <= qr && T[x] != -1) {
     if (T[x] == -2) {
       for (int i = l; i <= r; i++) ans += a[d][i].calc(t);
       T[x] = t;
       return;
     }
     data tmp = (data){0, 0, 0, t - T[x]};
     int id = upper_bound(a[d] + l, a[d] + r + 1, tmp) - a[d];
     ans += 1ll * (R[d][r] - R[d][id - 1]) * (t - T[x]) + sum[d][id - 1] -
            sum[d][l - 1];
     T[x] = t;
     return;
   }
   if (T[x] >= 0) T[x << 1] = T[x << 1 | 1] = T[x];
   int mid = (l + r) >> 1;
   if (ql <= mid) Find(x << 1, l, mid, d + 1);
   if (qr > mid) Find(x << 1 | 1, mid + 1, r, d + 1);
   T[x] = (T[x << 1] == T[x << 1 | 1]) ? T[x << 1] : -1;
 }
 int main() {
   scanf( %d , &n);
   Build(1, 1, n, 0);
   scanf( %d , &Q);
   while (Q--) {
     scanf( %d , &t);
     scanf( %d , &ql);
     scanf( %d , &qr);
     ans = 0;
     Find(1, 1, n, 0);
     printf( %I64d
 , ans);
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 struct Edge {
   int v;
   Edge *link;
 } edge[400010 * 2], *adj[200010], *tree[200010 * 2];
 int totE, con, root, currT, top, top1;
 int totCut, totBi;
 int dep[200010], deps[200010], num[200010], dfn[200010], lowlink[200010],
     cut[200010], bi[200010], color[200010];
 int temp[200010], st1[200010], st2[200010], st3[200010], st4[200010],
     f[200010 * 2];
 int used[200010 * 2], odd[200010 * 2];
 int fa[200010][20];
 int LOG[200010];
 void addEdge(int u, int v) {
   Edge *p = &edge[totE++];
   p->v = v;
   p->link = adj[u];
   adj[u] = p;
 }
 void addTree(int u, int v) {
   Edge *p = &edge[totE++];
   p->v = v;
   p->link = tree[u];
   tree[u] = p;
 }
 void dfs(int u, int pre) {
   dep[u] = dep[pre] + 1;
   dfn[u] = lowlink[u] = ++currT;
   num[u] = con;
   Edge *p = adj[u];
   int cnt = 0;
   bool flag = false;
   while (p) {
     if (!dfn[p->v]) {
       ++cnt;
       dfs(p->v, u);
       lowlink[u] = min(lowlink[u], lowlink[p->v]);
       if (lowlink[p->v] >= dfn[u]) flag = true;
     } else if (p->v != pre)
       lowlink[u] = min(lowlink[u], dfn[p->v]);
     p = p->link;
   }
   if (flag && (u != root || cnt > 1)) cut[u] = ++totCut;
 }
 bool dfs2(int u) {
   Edge *p = adj[u];
   while (p) {
     if (bi[p->v] == totBi) {
       if (color[p->v] == -1) {
         color[p->v] = !color[u];
         if (!dfs2(p->v)) return false;
       } else if (color[p->v] == color[u])
         return false;
     }
     p = p->link;
   }
   return true;
 }
 void solve() {
   ++totBi;
   int cnt = 0;
   for (int i = 0; i < top1; ++i) {
     int u = st3[i], v = st4[i];
     bi[u] = totBi;
     bi[v] = totBi;
     color[u] = color[v] = -1;
     if (cut[u]) temp[cnt++] = cut[u];
     if (cut[v]) temp[cnt++] = cut[v];
   }
   sort(temp, temp + cnt);
   for (int i = 0; i < cnt; ++i)
     if (i == 0 || temp[i] != temp[i - 1]) {
       addTree(temp[i], totCut + totBi);
       addTree(totCut + totBi, temp[i]);
     }
   color[st3[0]] = 0;
   odd[totBi] = !dfs2(st3[0]);
 }
 void dfs1(int u, int pre) {
   dfn[u] = lowlink[u] = ++currT;
   Edge *p = adj[u];
   while (p) {
     if (!dfn[p->v]) {
       st1[top] = u;
       st2[top++] = p->v;
       dfs1(p->v, u);
       lowlink[u] = min(lowlink[u], lowlink[p->v]);
       if (lowlink[p->v] >= dfn[u]) {
         top1 = 0;
         while (true) {
           --top;
           st3[top1] = st1[top];
           st4[top1++] = st2[top];
           if (st1[top] == u && st2[top] == p->v) break;
         }
         solve();
       }
     } else {
       if (dfn[u] > dfn[p->v] && p->v != pre) {
         st1[top] = u;
         st2[top++] = p->v;
       }
       if (p->v != pre) lowlink[u] = min(lowlink[u], dfn[p->v]);
     }
     p = p->link;
   }
 }
 void dfs3(int u, int pre) {
   deps[u] = deps[pre] + 1;
   used[u] = true;
   fa[u][0] = pre;
   if (u <= totCut)
     f[u] = 0;
   else
     f[u] = odd[u - totCut];
   f[u] += f[pre];
   Edge *p = tree[u];
   while (p) {
     if (!used[p->v]) dfs3(p->v, u);
     p = p->link;
   }
 }
 int LCA(int u, int v) {
   if (deps[u] < deps[v]) swap(u, v);
   int diff = deps[u] - deps[v];
   for (int i = 0; diff; ++i, diff >>= 1)
     if (diff & 1) u = fa[u][i];
   if (u == v) return u;
   int t = 19;
   for (int i = t; i >= 0; --i) {
     if (fa[u][i] != fa[v][i]) {
       u = fa[u][i];
       v = fa[v][i];
     }
   }
   return fa[u][0];
 }
 int main() {
   LOG[1] = 0;
   for (int i = 2; i < 200010; ++i) LOG[i] = (LOG[i] >> 1) + 1;
   int n, m;
   scanf( %d%d , &n, &m);
   for (int i = 0; i < m; ++i) {
     int u, v;
     scanf( %d%d , &u, &v);
     addEdge(u, v);
     addEdge(v, u);
   }
   dep[0] = -1;
   for (int i = 1; i <= n; ++i)
     if (!dfn[i]) {
       root = i;
       dfs(i, 0);
       ++con;
     }
   currT = 0;
   memset(dfn, 0, sizeof(dfn));
   for (int i = 1; i <= n; ++i)
     if (!dfn[i]) dfs1(i, 0);
   for (int i = 1; i <= totCut + totBi; ++i)
     if (!used[i]) dfs3(i, 0);
   for (int l = 1; (1 << l) <= totCut + totBi; ++l)
     for (int i = 1; i <= totCut + totBi; ++i)
       if (fa[i][l - 1] != 0) fa[i][l] = fa[fa[i][l - 1]][l - 1];
   int q;
   scanf( %d , &q);
   for (int i = 0; i < q; ++i) {
     int u, v;
     scanf( %d%d , &u, &v);
     if (u == v || num[u] != num[v])
       puts( No );
     else {
       int x, y;
       if (cut[u])
         x = cut[u];
       else
         x = totCut + bi[u];
       if (cut[v])
         y = cut[v];
       else
         y = totCut + bi[v];
       int lca = LCA(x, y);
       int sum = f[x] + f[y] - 2 * f[fa[lca][0]];
       if (sum > 0)
         puts( Yes );
       else if ((dep[u] + dep[v]) % 2 == 0)
         puts( No );
       else
         puts( Yes );
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 template <typename T>
 inline void read(T &x) {
   x = 0;
   char c = getchar();
   while (!isdigit(c)) c = getchar();
   while (isdigit(c)) x = x * 10 + (c ^ 48), c = getchar();
 }
 using namespace std;
 int t;
 int pri[4001000], pcnt;
 bool depri[35001000];
 inline void init() {
   depri[1] = true;
   const int up = 3.5e7;
   for (int i = 2; i <= up; ++i) {
     if (!depri[i]) pri[++pcnt] = i;
     for (int j = 1; j <= pcnt && i * pri[j] <= up; ++j) {
       depri[i * pri[j]] = true;
       if (i % pri[j] == 0) break;
     }
   }
 }
 struct TC {
   int id;
   long long n, k;
   inline bool operator<(const TC &a) const { return k < a.k; }
 } tc[10100];
 bool ans[10100];
 long long p[100];
 int ptot;
 inline void Div(long long x) {
   ptot = 0;
   for (int i = 1; 1ll * pri[i] * pri[i] <= x; ++i) {
     if (x % pri[i] == 0) {
       p[++ptot] = pri[i];
       while (x % pri[i] == 0) x /= pri[i];
     }
   }
   if (x != 1) p[++ptot] = x;
 }
 struct node {
   int cur;
   long long val;
   inline bool operator<(const node &a) const { return val > a.val; }
 };
 priority_queue<node> q;
 long long dis[100100];
 bool vis[100100];
 inline void Update(long long k) {
   Div(k);
   if (ptot <= 2) return;
   memset(dis, 0x3f, sizeof(dis));
   memset(vis, 0, sizeof(vis));
   dis[0] = 0;
   q.push((node){0, 0});
   while (!q.empty()) {
     int cur = (q.top()).cur;
     q.pop();
     if (vis[cur]) continue;
     vis[cur] = true;
     for (int i = 2; i <= ptot; ++i) {
       int to = (cur + p[i]) % p[1];
       long long val = (cur + p[i]) / p[1];
       if (dis[to] <= dis[cur] + val) continue;
       dis[to] = dis[cur] + val;
       q.push((node){to, dis[to]});
     }
   }
 }
 inline long long quickmul(long long x, long long k, long long P) {
   x = (x % P + P) % P;
   k = (k % P + P) % P;
   long long res = 0;
   while (k) {
     if (k & 1) res = (res + x) % P;
     x = (x + x) % P;
     k >>= 1;
   }
   return res;
 }
 long long exgcd(long long a, long long b, long long &x, long long &y) {
   if (!b) {
     x = 1;
     y = 0;
     return a;
   }
   long long g = exgcd(b, a % b, x, y);
   long long tmp = x;
   x = y;
   y = tmp - a / b * y;
   return g;
 }
 inline bool get_ans(long long n) {
   if (!ptot) return false;
   if (ptot == 1) return n % p[1] == 0;
   if (ptot == 2) {
     long long x = 0, y = 1;
     exgcd(p[1], p[2], x, y);
     x = quickmul(x, n, p[2]);
     if (1.0 * x * p[1] > 2e18) return false;
     return n >= x * p[1];
   }
   if (1.0 * dis[n % p[1]] * p[1] > 2e18) return false;
   return dis[n % p[1]] * p[1] + n % p[1] <= n;
 }
 signed main() {
   init();
   read(t);
   for (int i = 1; i <= t; ++i) {
     read(tc[i].n), read(tc[i].k);
     tc[i].id = i;
   }
   sort(tc + 1, tc + 1 + t);
   for (int i = 1; i <= t; ++i) {
     if (tc[i].k != tc[i - 1].k) Update(tc[i].k);
     ans[tc[i].id] = get_ans(tc[i].n);
   }
   for (int i = 1; i <= t; ++i) puts(ans[i] ?  YES  :  NO );
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 long long MOD = 1e9 + 7;
 std::vector<long long> factorial;
 void precompute_factorial(int ma) {
   factorial.push_back(1);
   for (int i = 1; i <= ma; i++)
     factorial.push_back((factorial.back() * i) % MOD);
 }
 long long power(long long base, int e) {
   long long result = 1;
   while (e) {
     if (e & 1) result = (result * base) % MOD;
     base = (base * base) % MOD;
     e >>= 1;
   }
   return result;
 }
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   int n, k;
   cin >> n >> k;
   precompute_factorial(n + 1);
   vector<long long> inv_factorial(n + 1, 1);
   for (int i = 2; i < n + 1; i++)
     inv_factorial[i] = power(factorial[i], MOD - 2);
   vector<long long> inv(n + 2);
   for (int i = 1; i < n + 2; i++) inv[i] = power(i, MOD - 2);
   if (k == 0) {
     cout << ((factorial[n] - factorial[n - 1] + MOD) % MOD) << endl;
     return 0;
   }
   vector<long long> dp2(n + 1, 1);
   vector<long long> xxx(n + 1, 1);
   long long sum = 0;
   for (int i = 1; i < n + 1; i++) {
     sum = (sum + xxx[i - 1]) % MOD;
     if (i - 1 - k >= 0) {
       sum = (sum + MOD - xxx[i - 1 - k]) % MOD;
     }
     dp2[i] = (sum * factorial[i - 1]) % MOD;
     xxx[i] = (dp2[i] * inv_factorial[i]) % MOD;
   }
   vector<long long> dp(n + 1, 0);
   long long last = 0;
   long long total = 0;
   for (int i = k + 2; i <= n; i++) {
     long long y = i - k - 2;
     long long tt = dp2[y];
     tt = (tt + MOD) % MOD;
     tt = (tt * inv_factorial[y]) % MOD;
     tt = (tt * inv[i - 1]) % MOD;
     last += tt;
     last %= MOD;
     total += last;
     total %= MOD;
     dp[i] = (factorial[i - 1] * total) % MOD;
   }
   cout << dp.back() << endl;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 105;
 const int M = 505;
 const int MOD = int(1e9) + 7;
 const int INF = 0x3f3f3f3f;
 const double EPS = 1e-9;
 const double PI = acos(-1.0);
 const int dx[] = {-1, 1, 0, 0};
 const int dy[] = {0, 0, -1, 1};
 template <class T>
 inline T Min(T a, T b) {
   return a < b ? a : b;
 }
 template <class T>
 inline T Max(T a, T b) {
   return a > b ? a : b;
 }
 template <class T>
 inline T Min(T a, T b, T c) {
   return min(min(a, b), c);
 }
 template <class T>
 inline T Max(T a, T b, T c) {
   return max(max(a, b), c);
 }
 template <class T>
 inline T sqr(T a) {
   return a * a;
 }
 template <class T>
 inline T cub(T a) {
   return a * a * a;
 }
 template <class T>
 inline T gcd(T a, T b) {
   return b == 0 ? a : gcd(b, a % b);
 }
 template <class T>
 inline T lcm(T a, T b) {
   return a * b / gcd(a, b);
 }
 int main() {
   int a, b;
   while (scanf( %d%d , &a, &b) != EOF) {
     int i, tot = (a * (a - 1)) / 2;
     if (tot <= b) {
       printf( no solution
 );
     } else {
       for (i = 0; i < a; i++) {
         printf( %d %d
 , 0, i);
       }
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int n, q;
 int A[600006];
 int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }
 map<pair<int, int>, int> M;
 int L[600006], R[600006], lst[600006], cn;
 struct que {
   int l, r, idx;
 } Q[600006];
 vector<int> bc[600006];
 int T[600006];
 void ad(int x, int c) {
   while (x <= cn) T[x] += c, x += (x & -x);
 }
 int sum(int x) {
   int ret = 0;
   while (x > 0) ret += T[x], x -= (x & -x);
   return ret;
 }
 int ans[600006];
 void solve() {
   cin >> n;
   for (int i = (1), iend = (n); i <= iend; ++i) {
     int k;
     scanf( %d , &k);
     L[i] = cn + 1;
     int px, py, tx, ty, x, y, sx, sy;
     scanf( %d%d , &px, &py);
     sx = px, sy = py;
     for (int j = (1), jend = (k); j <= jend; ++j) {
       if (j != k)
         scanf( %d%d , &x, &y);
       else
         x = sx, y = sy;
       tx = x, ty = y;
       x -= px, y -= py;
       int g = gcd(abs(x), abs(y));
       x /= g, y /= g;
       ++cn;
       lst[cn] = M[make_pair(x, y)];
       bc[lst[cn]].push_back(cn);
       M[make_pair(x, y)] = cn;
       px = tx, py = ty;
     }
     R[i] = cn;
   }
   cin >> q;
   for (int i = (1), iend = (q); i <= iend; ++i) {
     int l, r;
     scanf( %d%d , &l, &r);
     l = L[l], r = R[r];
     Q[i] = (que){l, r, i};
   }
   sort(Q + 1, Q + 1 + q, [](que a, que b) { return a.l < b.l; });
   int cur = 1;
   for (int i = (0), iend = (cn); i <= iend; ++i) {
     while (cur <= q && Q[cur].l == i) {
       ans[Q[cur].idx] = sum(Q[cur].r) - sum(Q[cur].l - 1);
       ++cur;
     }
     for (int x : bc[i]) ad(x, 1);
   }
   for (int i = (1), iend = (q); i <= iend; ++i) printf( %d
 , ans[i]);
 }
 signed main() { solve(); }


#include <bits/stdc++.h>
 using namespace std;
 const long long remi = 1000000007;
 const long long remi2 = 998244353;
 const long long inf = 1e18 + 1e17 + 1e16 + 1e15 + 1e14;
 long long Pow(long long x, long long y, long long m) {
   if (y == 0) return 1;
   long long p = Pow(x, y / 2, m) % m;
   p = (p * p) % m;
   return (y % 2 == 0) ? p : (x * p) % m;
 }
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   cout.tie(NULL);
   long long i, j, k;
   long long n, m;
   cin >> n >> m;
   vector<long long> v(m);
   for (i = 0; i < m; i++) {
     cin >> v[i];
   }
   sort(v.begin(), v.end());
   bool ans = true;
   if (m == 0) {
     cout <<  YES ;
     exit(0);
   }
   if (v[0] == 1 or v[m - 1] == n) ans = false;
   for (i = 2; i < m; i++) {
     long long s0 = v[i - 2];
     long long s1 = v[i - 1];
     long long s2 = v[i];
     if ((s0 == s2 - 2) and (s1 == s2 - 1)) ans = false;
   }
   if (ans)
     cout <<  YES ;
   else
     cout <<  NO ;
 }


#include <bits/stdc++.h>
 using namespace std;

 int main()
 {
 	ios_base::sync_with_stdio(false), cin.tie(0);
 	int t;
 	cin >> t;
 	while (t--) {
 		map<int, int>L, R;
 		int bal = 0;
 		string s;
 		cin >> s;
 		for (char &c : s) {
 			if (c == '1') {
 				R[bal]++;
 				bal++;
 			} else {
 				L[bal]++;
 				bal--;
 			}
 		}

 		int cur = 0;
 		while (L[cur] > 0 || R[cur] > 0) {
 			if (L[cur] > 0 && R[cur - 1] > 0 || (!R[cur] && !L[cur + 1])) {
 				L[cur]--;
 				cur--;
 				cout << 0;
 			} else {
 				R[cur]--;
 				cur++;
 				cout << 1;
 			}
 		}
 		cout <<
 ;
 	}
 	return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int tree[100010 << 2];
 bool flag;
 struct node {
   int row, col, pos;
   friend bool operator<(node a, node b) {
     if (a.row == b.row) return a.col < b.col;
     return a.row < b.row;
   }
 };
 node p[100010];
 inline void pushUp(int rt) { tree[rt] = min(tree[rt << 1], tree[rt << 1 | 1]); }
 void update(int x, int y, int val, int L, int R, int rt) {
   if (flag) return;
   if (L == R) {
     if (tree[rt] == 0) {
       flag = true;
       tree[rt] = val;
     }
     return;
   } else if (x <= L && R <= y) {
     int mid = (L + R) >> 1;
     if (tree[rt << 1] == 0)
       update(x, y, val, L, mid, rt << 1);
     else if (tree[rt << 1 | 1] == 0)
       update(x, y, val, mid + 1, R, rt << 1 | 1);
   } else {
     int mid = (L + R) >> 1;
     if (x <= mid) update(x, y, val, L, mid, rt << 1);
     if (y > mid) update(x, y, val, mid + 1, R, rt << 1 | 1);
   }
   pushUp(rt);
 }
 void out(int L, int R, int rt) {
   if (L == R) {
     if (tree[rt]) printf( %d  , tree[rt]);
   } else {
     int mid = (L + R) >> 1;
     out(L, mid, rt << 1);
     out(mid + 1, R, rt << 1 | 1);
   }
 }
 int main() {
   int i;
   int n, m, cnt;
   while (~scanf( %d%d , &n, &m)) {
     memset(tree, 0, sizeof(tree));
     for (i = 0; i < m; i++) {
       scanf( %d%d , &p[i].row, &p[i].col);
       p[i].pos = i + 1;
     }
     sort(p, p + m);
     for (cnt = i = 0; i < m; i++) {
       flag = false;
       update(n - p[i].col + 1, p[i].row, p[i].pos, 1, n, 1);
       if (flag) cnt++;
     }
     printf( %d
 , cnt);
     out(1, n, 1);
     putchar('
');
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const long long MOD = 1e9 + 7;
 int n, t, sz[200010], cen;
 vector<int> g[200010], v;
 bool mark[200010];
 int dfs(int pos, int par) {
   sz[pos] = (int)mark[pos];
   bool ok = true;
   for (int i = 0; i < g[pos].size(); i++)
     if (g[pos][i] != par) {
       sz[pos] += dfs(g[pos][i], pos);
       if (sz[g[pos][i]] > t) ok = false;
     }
   if (ok && sz[pos] >= t) cen = pos;
   return sz[pos];
 }
 void dfs2(int pos, int par) {
   if (mark[pos]) v.push_back(pos);
   for (int i = 0; i < g[pos].size(); i++)
     if (g[pos][i] != par) dfs2(g[pos][i], pos);
 }
 int main() {
   cin >> n >> t;
   int x, y;
   for (int i = 0; i < n - 1; i++) {
     scanf( %d%d , &x, &y);
     g[x].push_back(y);
     g[y].push_back(x);
   }
   for (int i = 0; i < t + t; i++) {
     scanf( %d , &x);
     mark[x] = true;
   }
   dfs(1, 0);
   for (int i = 0; i < g[cen].size(); i++) dfs2(g[cen][i], cen);
   if (mark[cen]) v.push_back(cen);
   cout << 1 << endl << cen << endl;
   for (int i = 0; i < t; i++) printf( %d %d %d
 , v[i], v[i + t], cen);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 template <class T>
 inline int countbit(T n) {
   return (n == 0) ? 0 : (1 + countbit(n & (n - 1)));
 }
 template <class T>
 inline T gcd(T a, T b) {
   return b ? gcd(b, a % b) : a;
 }
 const double EPS = 1e-9;
 const double PI = acos(-1.0);
 int n, k, h;
 int ind[100005];
 int vel[100005];
 int mass[100005];
 int ret[100005];
 bool can(double mid) {
   int cnt = 0;
   double step = k;
   for (int j = n - 1; j >= 0 && cnt <= k; j--) {
     double tempo = step * h / ((double)vel[ind[j]]);
     if (tempo < mid - EPS || fabs(tempo - mid) < EPS) cnt++, step--;
   }
   return cnt >= k;
 }
 bool cmp(int i, int j) {
   if (mass[i] != mass[j]) return mass[i] < mass[j];
   return vel[i] < vel[j];
 }
 int main(void) {
   scanf( %d %d %d , &n, &k, &h);
   for (int i = (0); i < int(n); i++) scanf( %d , &mass[i]);
   for (int i = (0); i < int(n); i++) scanf( %d , &vel[i]);
   for (int i = (0); i < int(n); i++) ind[i] = i;
   sort(ind, ind + n, cmp);
   double l = 0, r = 10E9 + 100;
   for (int i = (0); i < int(100); i++) {
     double mid = (l + r) / 2;
     if (can(mid))
       r = mid;
     else
       l = mid;
   }
   double mid = r, step = k;
   int p = k - 1;
   for (int j = n - 1; j >= 0 && p >= 0; j--) {
     double tempo = step * h / ((double)vel[ind[j]]);
     if (tempo < mid - EPS || fabs(tempo - mid) < EPS) {
       step--;
       ret[p--] = ind[j] + 1;
     }
   }
   for (int i = (0); i < int(k); i++) {
     if (i) printf(   );
     printf( %d , ret[i]);
   }
   printf(
 );
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int n, m;
 map<int, int> mym;
 void kaisa() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   cout.tie(NULL);
   cin >> n >> m;
   queue<int> q;
   for (int i = 1; i <= n; i++) {
     int x;
     cin >> x;
     mym[x] = 0;
     q.push(x);
   }
   long long res = 0;
   vector<int> ans;
   while (!q.empty()) {
     int x = q.front();
     q.pop();
     if (mym[x] != 0) {
       res += mym[x];
       ans.push_back(x);
       if (ans.size() == m) break;
     }
     if (!mym.count(x + 1)) {
       mym[x + 1] = mym[x] + 1;
       q.push(x + 1);
     }
     if (!mym.count(x - 1)) {
       mym[x - 1] = mym[x] + 1;
       q.push(x - 1);
     }
   }
   cout << res <<
 ;
   for (int i : ans) {
     cout << i <<    ;
   }
   cout <<
 ;
 }
 int main() {
   kaisa();
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 long long q, x, y;
 int main() {
   ios::sync_with_stdio(0);
   cin.tie(0);
   cin >> q;
   while (q--) {
     cin >> x >> y;
     if (x - y > 1)
       cout <<  YES
            <<
 ;
     else
       cout <<  NO
            <<
 ;
   }
   return 0;
 }


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


#include <bits/stdc++.h>
 using namespace std;
 int gcd_(int a, int b) {
   if (b == 0)
     return a;
   else
     return gcd_(b, a % b);
 }
 int main() {
   int n, i, x;
   vector<int> v;
   cin >> n;
   for (i = 0; i < n; i++) {
     cin >> x;
     v.push_back(x);
   }
   int max1 = v[0];
   x = v[0];
   for (i = 1; i < n; i++) {
     x = gcd_(v[i], x);
     if (v[i] > max1) max1 = v[i];
   }
   int moves = ((max1 / x) - n);
   if (moves % 2 != 0)
     cout <<  Alice  << endl;
   else
     cout <<  Bob  << endl;
   return 0;
 }


#include <bits/stdc++.h>
 std::vector<int> ReadVector(size_t size) {
   std::vector<int> vector(size);
   for (auto& element : vector) {
     std::cin >> element;
   }
   return vector;
 }
 bool is_pal(const std::string& a) {
   for (size_t i = 0; i < a.size() / 2; ++i) {
     if (a[i] != a[a.size() - i - 1]) {
       return false;
     }
   }
   return true;
 }
 class Solver {
  public:
   std::vector<std::vector<int>> g;
   Solver() {}
   void read() {}
   void solve() {
     size_t n;
     std::cin >> n;
     g.resize(n);
     for (size_t i = 0; i < n - 1; ++i) {
       int a, b;
       std::cin >> a >> b;
       a--;
       b--;
       g[a].push_back(b);
       g[b].push_back(a);
     }
     int root = -1;
     int begin = -1;
     int end = -1;
     for (int v = 0; v < n; ++v) {
       if (g[v].size() > 2) {
         if (root != -1) {
           std::cout <<  No  << std::endl;
           return;
         } else {
           root = v;
         }
       }
       if (g[v].size() == 1) {
         if (begin == -1) {
           begin = v;
         } else {
           end = v;
         }
       }
     }
     std::cout <<  Yes
 ;
     if (root == -1) {
       std::cout << 1 << std::endl;
       std::cout << begin + 1 <<     << end + 1 << std::endl;
       return;
     }
     std::cout << g[root].size() << std::endl;
     for (auto ch : g[root]) {
       std::cout << root + 1 <<    ;
       int tmp = ch;
       int parent = root;
       while (g[tmp].size() == 2) {
         if (g[tmp][0] == parent) {
           parent = tmp;
           tmp = g[tmp][1];
         } else {
           parent = tmp;
           tmp = g[tmp][0];
         }
       }
       std::cout << tmp + 1 << std::endl;
     }
   }
 };
 int main() {
   std::ios_base::sync_with_stdio(false);
   std::cin.tie(nullptr);
   Solver solver;
   solver.solve();
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 long long INF = 1e9;
 const int maxn = 1e6 + 5;
 int a[maxn];
 int tpr = 0;
 vector<int> adj[maxn];
 void dfs(int root) {
   if (adj[root].size() > 0) {
     if (adj[root][0] == (root + 1)) {
       dfs(root + 1);
       tpr++;
     } else
       return;
   }
 }
 int main() {
   int n, m, t, i, j, k;
   int flag = 0, flag2;
   int ans = 0;
   cin >> n >> k;
   for (int i = 0; i < k; i++) {
     cin >> m;
     ans += m - 1;
     int y;
     int prev = -1;
     for (int j = 0; j < m; j++) {
       cin >> y;
       if (y == 1) {
         flag2 = m - 1;
       }
       if ((j == 0) && (y == 1)) {
         flag = 1;
       }
       if (prev != -1) adj[prev].push_back(y);
       prev = y;
     }
   }
   if (flag == 0) {
     ans += n - 1;
     cout << ans << endl;
     return 0;
   }
   dfs(1);
   ans -= tpr;
   ans += (n - tpr - 1);
   cout << ans << endl;
   return 0;
   return 0;
 }


#include <bits/stdc++.h>
 #pragma GCC optimize( O3 )
 #pragma GCC target( sse4 )
 using namespace std;
 using ii = pair<int, int>;
 using ll = long long;
 const int N = 3000 + 5;
 const int mod = 1e9 + 7;
 string source[N], target[N];
 int n;
 ii ranges[N];
 vector<int> FAIL(string pat) {
   int m = pat.size();
   vector<int> F(m + 1);
   int i = 0, j = -1;
   F[0] = -1;
   while (i < m) {
     while (j >= 0 && pat[i] != pat[j]) j = F[j];
     i++, j++;
     F[i] = j;
   }
   return F;
 }
 bool check(string a, string b, string s, string t) {
   vector<int> F = FAIL(s);
   int i = 0, j = 0;
   int n = a.size();
   int m = s.size();
   while (i < n) {
     while (j >= 0 && a[i] != s[j]) j = F[j];
     i++, j++;
     if (j == m) {
       int idx = i - j;
       for (int k = 0; k < m; k++) {
         a[k + idx] = t[k];
       }
       break;
     }
   }
   return a == b;
 }
 int main() {
   ios_base::sync_with_stdio(0);
   cin.tie(0);
   cin >> n;
   for (int i = 0; i < n; i++) cin >> source[i];
   for (int i = 0; i < n; i++) cin >> target[i];
   string s, t;
   string prefix, suffix;
   for (int i = 0; i < n; i++) {
     if (source[i] != target[i]) {
       int l = -1, r = -1;
       for (int j = 0; j < source[i].size(); j++) {
         if (source[i][j] != target[i][j]) r = j;
         if (source[i][j] != target[i][j] && l == -1) l = j;
       }
       ranges[i] = {l, r};
       if (s.empty()) {
         s = source[i].substr(l, r - l + 1);
         t = target[i].substr(l, r - l + 1);
         prefix = source[i].substr(0, l);
         suffix = source[i].substr(r + 1);
         reverse(prefix.begin(), prefix.end());
       } else {
         if (s != source[i].substr(l, r - l + 1) ||
             t != target[i].substr(l, r - l + 1)) {
           cout <<  NO
 ;
           return 0;
         }
         string cprefix = source[i].substr(0, l);
         string csuffix = source[i].substr(r + 1);
         reverse(cprefix.begin(), cprefix.end());
         string p, ss;
         for (int j = 0;
              j < min(cprefix.size(), prefix.size()) && cprefix[j] == prefix[j];
              j++) {
           p.push_back(prefix[j]);
         }
         for (int j = 0;
              j < min(csuffix.size(), suffix.size()) && csuffix[j] == suffix[j];
              j++) {
           ss.push_back(suffix[j]);
         }
         prefix = p;
         suffix = ss;
       }
     }
   }
   reverse(prefix.begin(), prefix.end());
   s = prefix + s + suffix;
   t = prefix + t + suffix;
   for (int i = 0; i < n; i++) {
     if (!check(source[i], target[i], s, t)) {
       cout <<  NO
 ;
       return 0;
     }
   }
   cout <<  YES
 ;
   cout << s << '
';
   cout << t << '
';
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 template <typename T>
 inline bool inrange(T a, T l, T r) {
   if (l > r) swap(l, r);
   return (a >= l and a <= r);
 }
 template <typename T, typename U>
 inline bool exist(T val, U &any) {
   return (any.find(val) != any.end());
 }
 template <typename T>
 inline void inp(T &any) {
   cin >> any;
 }
 template <typename T, typename... U>
 inline void inp(T &a, U &...b) {
   cin >> a;
   inp(b...);
 }
 template <typename T>
 inline istream &operator>>(istream &in, vector<T> &a) {
   for (auto &x : a) in >> x;
   return in;
 }
 template <typename T, typename U>
 inline istream &operator>>(istream &in, pair<T, U> &a) {
   in >> a.first >> a.second;
   return in;
 }
 void solve(long long int &T) {
   long long int n;
   std::cin >> n;
   vector<long long int> a(n), b(n);
   std::cin >> a >> b;
   vector<long long int> sol(n);
   for (long long int i = (0); i < (n); i++) sol[a[i] - 1] = b[i];
   for (auto &X : sol) std::cout << X <<    ;
   std::cout << '
';
 }
 int main() {
   ios::sync_with_stdio(0);
   cin.tie(0);
   cout.tie(0);
   ;
   int t = 1;
   for (long long int i = (1); i < (t + 1); i++) {
     cerr <<  Case #  << i <<
 ;
     solve(i);
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int n;
   cin >> n;
   vector<vector<int>> v;
   for (int i = 0; i < (n); ++i) {
     int l;
     cin >> l;
     vector<int> temp;
     for (int j = 0; j < (l); ++j) {
       int ele;
       cin >> ele;
       temp.push_back(ele);
     }
     v.push_back(temp);
   }
   vector<int> maxi;
   int cnt_no_ascent = 0;
   bool ascent[n];
   int mini_arr[n];
   int maxi_arr[n];
   for (int i = 0; i < (n); ++i) ascent[i] = false;
   for (int i = 0; i < (n); ++i) {
     int flag = 0;
     int maxi_el = INT_MIN;
     int mini_el = INT_MAX;
     for (int j = 0; j < (v[i].size()); ++j) {
       if (v[i][j] > maxi_el) {
         maxi_el = v[i][j];
       }
       if (v[i][j] < mini_el) {
         mini_el = v[i][j];
       }
       if (j != 0 && v[i][j - 1] < v[i][j]) {
         ++cnt_no_ascent;
         flag = 1;
         ascent[i] = true;
         break;
       }
     }
     if (flag == 0) {
       mini_arr[i] = mini_el;
       maxi.push_back(maxi_el);
       maxi_arr[i] = maxi_el;
     }
   }
   long long ans = ((long long)cnt_no_ascent * (long long)n);
   sort(maxi.begin(), maxi.end());
   for (int i = 0; i < (n); ++i) {
     if (ascent[i] == true) continue;
     auto it = upper_bound(maxi.begin(), maxi.end(), mini_arr[i]);
     ans = ans + (long long)(maxi.end() - it);
     ans = ans + cnt_no_ascent;
   }
   cout << ans << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int MAX = 1e6 + 6;
 int main() {
   int n;
   cin >> n;
   string str =   ;
   while (n) {
     str += ((n % 2) + '0');
     n /= 2;
   }
   reverse(str.begin(), str.end());
   while (str.size() < 6) str =  0  + str;
   string ss =   ;
   ss += str[0];
   ss += str[5];
   ss += str[3];
   ss += str[2];
   ss += str[4];
   ss += str[1];
   int ret = 0;
   for (auto x : ss) ret = ret * 2 + (x - '0');
   cout << ret << '
';
 }


#include <bits/stdc++.h>
 using namespace std;
 int getint() {
   unsigned int c;
   int x = 0;
   while (((c = getchar()) - '0') >= 10) {
     if (c == '-') return -getint();
     if (!~c) exit(0);
   }
   do {
     x = (x << 3) + (x << 1) + (c - '0');
   } while (((c = getchar()) - '0') < 10);
   return x;
 }
 int n, nn, sum;
 int as[20];
 int c[10][10];
 bool used[20];
 int num[20], have[20], ns;
 int perm[20][20][20];
 void dfs(int row, int col, int rows, int cols, int dir, int cnt) {
   int i, j, k;
   if (cnt == nn) {
     int tmp1 = 0, tmp2 = 0;
     for (i = 0; i < n; i++) {
       tmp1 += c[i][i];
       tmp2 += c[i][n - 1 - i];
     }
     if (tmp1 != sum) return;
     if (tmp2 != sum) return;
     tmp1 = tmp2 = 0;
     for (i = 0; i < n; i++) {
       tmp1 += c[n - 1][i];
       tmp2 += c[i][n - 1];
     }
     if (tmp1 != sum) return;
     if (tmp2 != sum) return;
     for (i = 0; i < n; i++) {
       for (j = 0; j < n; j++) {
         if (j) putchar(' ');
         printf( %d , c[i][j]);
       }
       puts(  );
     }
     exit(0);
   }
   if (row == n or col == n) {
     int tmp = 0;
     if (dir == 0) {
       for (i = 0; i < n; i++) tmp += c[row][i];
       if (tmp != sum) return;
     } else {
       for (i = 0; i < n; i++) tmp += c[i][col];
       if (tmp != sum) return;
     }
     if (dir == 0) {
       dfs(rows + 1, cols, rows + 1, cols, 1, cnt);
     } else {
       dfs(rows, cols + 1, rows, cols + 1, 0, cnt);
     }
   }
   for (i = 0; i < ns; i++) {
     int d = perm[row][col][i];
     if (have[d]) {
       have[d]--;
       c[row][col] = num[d];
       if (dir == 0) dfs(row, col + 1, rows, cols, dir, cnt + 1);
       if (dir == 1) dfs(row + 1, col, rows, cols, dir, cnt + 1);
       have[d]++;
     }
   }
 }
 int main() {
   int i, j, tcc, tc = 1 << 28;
   for (tcc = 0; tcc < tc; tcc++) {
     n = getint(), nn = n * n, sum = 0;
     for (i = 0; i < nn; i++) as[i] = getint(), sum += as[i];
     sum /= n;
     for (i = 0; i < nn; i++) num[i] = as[i];
     sort(num, num + nn);
     ns = unique(num, num + nn) - num;
     for (i = 0; i < ns; i++) {
       for (j = 0; j < nn; j++)
         if (num[i] == as[j]) have[i]++;
     }
     for (i = 0; i < n; i++)
       for (j = 0; j < n; j++) {
         for (int k = 0; k < ns; k++) perm[i][j][k] = k;
         random_shuffle(perm[i][j], perm[i][j] + ns);
       }
     printf( %d
 , sum);
     dfs(0, 0, 0, 0, 0, 0);
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int MXX = 3e5 + 5;
 int n, m;
 int a[MXX], ans[MXX];
 bool ok(int x) {
   int mx = m - 1;
   for (int i = n - 1; i >= 0; i--) {
     if (a[i] <= mx) {
       if (a[i] + x >= mx) {
         ans[i] = mx;
       } else {
         ans[i] = a[i] + x;
       }
     } else {
       if (a[i] + x >= mx + m)
         ans[i] = mx;
       else
         ans[i] = (a[i] + x) % m;
     }
     mx = ans[i];
   }
   for (int i = 0; i < n - 1; i++) {
     if (ans[i] > ans[i + 1]) {
       return false;
     }
   }
   return true;
 }
 bool ok2() {
   for (int i = 0; i < n - 1; i++) {
     if (a[i] > a[i + 1]) {
       return false;
     }
   }
   return true;
 }
 int main() {
   cin >> n >> m;
   for (int i = 0; i < n; i++) {
     cin >> a[i];
   }
   if (ok2()) {
     return cout << 0 << endl, 0;
   }
   int lo = 0, hi = m - 1, mid, ans = -1;
   while (lo <= hi) {
     mid = (lo + hi) / 2;
     if (ok(mid)) {
       ans = mid;
       hi = mid - 1;
     } else {
       lo = mid + 1;
     }
   }
   cout << ans << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 long long gcd(long long a, long long b);
 void print(long long arr[], long long n);
 void printv(vector<long long> arr);
 signed main() {
   long long t;
   cin >> t;
   while (t--) {
     long long n;
     cin >> n;
     long long k;
     cin >> k;
     vector<long long> arr(n);
     unordered_map<long long, long long> m;
     for (long long i = 0; i < n; i++) {
       cin >> arr[i];
       m[arr[i]]++;
     }
     vector<long long> tem = arr;
     sort(tem.begin(), tem.end());
     set<long long> a, b;
     if (arr[0] * 2 != k) a.insert(arr[0]);
     for (long long i = 1; i < n; i++) {
       if (arr[i] * 2 == k) continue;
       if (a.find(k - arr[i]) == a.end())
         a.insert(arr[i]);
       else
         b.insert(arr[i]);
     }
     long long ii = 0;
     for (long long i = 0; i < n; i++) {
       if (a.find(arr[i]) != a.end())
         cout << 1 <<    ;
       else if (b.find(arr[i]) != b.end())
         cout << 0 <<    ;
       else {
         cout << ii <<    ;
         ii = (ii + 1) % 2;
       }
     }
     cout <<
 ;
   }
 }
 long long gcd(long long a, long long b) {
   if (b == 0) return a;
   return gcd(b, a % b);
 }
 void print(long long arr[], long long n) {
   for (long long i = 0; i < n; i++) cout << arr[i] <<    ;
   cout <<
 ;
 }
 void printv(vector<long long> arr) {
   for (long long i = 0; i < arr.size(); i++) cout << arr[i] <<    ;
   cout <<
 ;
 }


#include <bits/stdc++.h>
 using namespace std;
 int par[300005];
 int vis[300005];
 unordered_map<int, int> edge[300005];
 int d[300005];
 vector<int> ans;
 int find(int u) { return par[u] == u ? u : par[u] = find(par[u]); }
 void join(int u, int v) {
   u = find(u);
   v = find(v);
   if (u > v) swap(u, v);
   par[v] = u;
 }
 void dfs(int u, int pr) {
   if (vis[u]) return;
   vis[u] = 1;
   for (auto i : edge[u]) {
     dfs(i.first, u);
   }
   if (d[u] == 1) {
     ans.push_back(edge[u][pr]);
     d[pr]++;
     d[pr] %= 2;
     return;
   }
 }
 int main() {
   ios_base::sync_with_stdio(false);
   memset(vis, 0, sizeof vis);
   int n, k, u, v;
   cin >> n >> k;
   for (int i = 1; i <= n; i++) {
     edge[i].clear();
     par[i] = i;
     cin >> d[i];
   }
   for (int i = 1; i <= k; i++) {
     cin >> u >> v;
     if (find(u) != find(v)) {
       edge[u][v] = i;
       edge[v][u] = i;
       join(u, v);
     }
   }
   int sum = 0;
   int avail = 0;
   for (int i = 1; i <= n; i++) {
     if (d[i] == -1) {
       avail++;
     } else
       sum += d[i];
   }
   if (sum % 2 && !avail) {
     cout << -1 << endl;
     return 0;
   }
   for (int i = 1; i <= n; i++) {
     if (d[i] == -1) {
       if (sum % 2 == 1) {
         d[i] = 1;
         sum++;
       } else
         d[i] = 0;
     }
   }
   dfs(1, 0);
   cout << ans.size() << endl;
   for (int i = 0; i < ans.size(); i++) {
     cout << ans[i] <<    ;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const long long MAX = 1e6 + 100;
 const long long mod = 1e9 + 7;
 const long long inf = 1e18 + 12;
 vector<long long> arr(MAX);
 map<long long, long long> mm;
 int main() {
   ios_base::sync_with_stdio(false);
   cout.tie(NULL);
   cout.tie(NULL);
   ;
   long long tt = 1;
   while (tt--) {
     long long n;
     cin >> n;
     long long a[n];
     for (long long i = 0; i < n; i++) cin >> a[i];
     long long w = 0, b = 0;
     long long i = 0;
     while (i < n) {
       if (i % 2 == 0) {
         w += (a[i] + 1) / 2;
         b += (a[i]) / 2;
       } else {
         w += a[i] / 2;
         b += (a[i] + 1) / 2;
       }
       i++;
     }
     cout << min(w, b);
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 const int MOD = 998244353;
 const int N = 200005;
 const double PI = 4 * atan(1);
 const long long MAX = 9223372036854775807;
 int dx[] = {1, -1, 0, 0, 1, -1, 1, -1};
 int dy[] = {0, 0, 1, -1, 1, 1, -1, -1};
 long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); }
 long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); }
 bool issquare(long long w) { return trunc(sqrt(w)) * trunc(sqrt(w)) == w; }
 bool isprime(long long u) {
   for (long long i = 2; i <= (int)sqrt(u); i++) {
     if (u % i == 0) return 0;
   }
   return 1;
 }
 long long mod(long long to_mod, long long MOD) {
   to_mod %= MOD;
   while (to_mod < 0) to_mod += MOD;
   return to_mod % MOD;
 }
 long long moduloMultiplication(long long a, long long b, long long mod) {
   long long res = 0;
   a %= mod;
   while (b) {
     if (b & 1) res = (res + a) % mod;
     a = (2 * a) % mod;
     b >>= 1;
   }
   return res;
 }
 long long power(long long x, long long y) {
   long long res = 1;
   x = x;
   while (y > 0) {
     if (y & 1) res = (res * x);
     y = y >> 1;
     x = (x * x);
   }
   return res;
 }
 long long n, m, x, y, t, k, z, w, l, r, ans;
 bool vis[N];
 long long a[N];
 long long lvl[N];
 map<long long, vector<long long> > adj;
 map<long long, long long> maa;
 void bfs(long long src) {
   queue<long long> q;
   q.push(src);
   lvl[src] = 1;
   while (!q.empty()) {
     long long ras = q.front();
     q.pop();
     if (lvl[ras] % 2 == 0) {
       vis[ras] = 1;
       continue;
     }
     for (auto t : adj[ras]) {
       if (vis[t]) continue;
       lvl[t] = lvl[ras] + 1;
       q.push(t);
     }
   }
 }
 int main() {
   ios::sync_with_stdio(0);
   cin.tie(NULL);
   cin >> n;
   for (int i = 0; i < n; i++) {
     cin >> a[i];
     maa[a[i]] = i;
   }
   for (int j = 0; j < n; j++) {
     k = a[j];
     for (int i = j; i < n; i += k) {
       if (a[i] > k) adj[i].push_back(j);
     }
     for (int i = j; i >= 0; i -= k) {
       if (a[i] > k) adj[i].push_back(j);
     }
   }
   for (int i = n; i >= 1; i--) {
     if (vis[maa[i]]) continue;
     bfs(maa[i]);
   }
   for (int i = 0; i < n; i++) {
     if (vis[i])
       cout <<  A ;
     else
       cout <<  B ;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 1e6 + 7;
 int n, lim, a[N], mn[N], mx[N], suf[N];
 long long ans;
 int main() {
   scanf( %d%d , &n, &lim);
   for (int i = 1; i <= lim; i++) mn[i] = n + 1;
   for (int i = 1; i <= n; i++) {
     scanf( %d , &a[i]);
     mn[a[i]] = min(mn[a[i]], i);
     mx[a[i]] = max(mx[a[i]], i);
   }
   int L = lim, R = 1, pre = mx[1];
   for (int i = 1; i < lim; i++) {
     pre = max(pre, mx[i]);
     if (pre > mn[i + 1]) {
       L = i;
       break;
     }
   }
   if (L == lim) {
     cout << 1ll * lim * (lim + 1) / 2;
     return 0;
   }
   pre = mn[lim];
   for (int i = lim; i > 1; i--) {
     pre = min(pre, mn[i]);
     if (mx[i - 1] > pre) {
       R = i;
       break;
     }
   }
   ans = lim - R + 2;
   suf[lim] = mn[lim];
   for (int i = lim - 1; i >= R; i--) suf[i] = min(suf[i + 1], mn[i]);
   pre = mx[1];
   for (int i = 1, j = R; i <= L; i++) {
     pre = max(pre, mx[i]);
     while (j <= lim && pre > suf[j]) j++;
     ans += lim - j + 2;
     if (j == i + 1) ans--;
   }
   cout << ans;
 }


#include <bits/stdc++.h>
 #pragma warning(disable : 4996)
 using namespace std;
 int n, m;
 int a[300000];
 int b[300000];
 vector<int> edge[300000];
 int ngood;
 int child[300000];
 int good[300000];
 int d[300000];
 int ans;
 void back(int now, int parent) {
   child[now] = 1;
   good[now] = b[now];
   for (auto e : edge[now])
     if (e != parent) {
       back(e, now);
       child[now] += child[e], good[now] += good[e];
     }
 }
 vector<pair<int, int> > v;
 void back2(int now, int parent) {
   for (auto e : edge[now])
     if (e != parent) {
       back2(e, now);
     }
   if (b[now] == 0) {
     d[now] = 0;
     return;
   }
   d[now] = 1;
   int xgood = 0;
   for (auto e : edge[now])
     if (e != parent) {
       if (child[e] == good[e]) xgood += child[e];
     }
   d[now] = 1 + xgood;
   for (auto e : edge[now])
     if (e != parent) {
       d[now] =
           max(d[now], 1 + d[e] + xgood - (child[e] == good[e] ? child[e] : 0));
     }
   ans = max(ans, d[now]);
   int u = 1 + (n - child[now] == ngood - good[now] ? n - child[now] : 0);
   v.clear();
   for (auto e : edge[now])
     if (e != parent) {
       v.emplace_back(d[e], e);
     }
   sort((v).begin(), (v).end(), greater<pair<int, int> >());
   for (int i = 0; i < 8 && i < v.size(); i++) {
     for (int j = i + 1; j < 8 && j < v.size(); j++) {
       ans = max(
           ans, u + v[i].first + v[j].first + xgood -
                    (child[v[i].second] == good[v[i].second] ? child[v[i].second]
                                                             : 0) -
                    (child[v[j].second] == good[v[j].second] ? child[v[j].second]
                                                             : 0));
     }
   }
 }
 int param(int x) {
   int i, j, k;
   for (i = 0; i < n; i++) b[i] = a[i] >= x;
   ngood = count(b, b + n, 1);
   back(0, -1);
   ans = 0;
   back2(0, -1);
   return ans >= m;
 }
 int main() {
   int i, j, k;
   cin >> n >> m;
   for (i = 0; i < n; i++) scanf( %d , &a[i]);
   for (i = 0; i < n - 1; i++) {
     int x, y;
     scanf( %d%d , &x, &y), x--, y--;
     edge[x].push_back(y), edge[y].push_back(x);
   }
   int low = 0, high = 1000001, mid;
   while (low <= high) {
     mid = (low + high) / 2;
     if (param(mid))
       low = mid + 1;
     else
       high = mid - 1;
   }
   cout << high;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 5e4 + 10, max_node = N * 200;
 int n, m, q, C, c[N], a[N], size[N], heavy[N], dfn[N], dfn_t, top[N], father[N],
     tt, lo[max_node], ro[max_node], addv[max_node];
 long long sum[max_node], sum2[max_node];
 vector<int> graph[N], nodes[N];
 void dfs1(int u) {
   size[u] = 1;
   for (auto v : graph[u]) {
     dfs1(v);
     size[u] += size[v];
     if (size[v] > size[heavy[u]]) {
       heavy[u] = v;
     }
   }
 }
 void dfs2(int u, int t) {
   top[u] = t;
   dfn[u] = ++dfn_t;
   if (heavy[u]) {
     dfs2(heavy[u], t);
     for (auto v : graph[u]) {
       if (v != heavy[u]) {
         dfs2(v, v);
       }
     }
   }
 }
 void add(int l, int r, int o, int v) {
   addv[o] += v;
   sum2[o] += ((sum[o] << 1) + (r - l + 1) * v) * v;
   sum[o] += (r - l + 1) * v;
 }
 void push_down(int l, int r, int o) {
   if (addv[o]) {
     int mid = l + r >> 1;
     if (!lo[o]) {
       lo[o] = ++tt;
     }
     if (!ro[o]) {
       ro[o] = ++tt;
     }
     add(l, mid, lo[o], addv[o]);
     add(mid + 1, r, ro[o], addv[o]);
     addv[o] = 0;
   }
 }
 void push_up(int o) {
   sum[o] = sum[lo[o]] + sum[ro[o]];
   sum2[o] = sum2[lo[o]] + sum2[ro[o]];
 }
 void modify(int l, int r, int& o, int ql, int qr, int v) {
   if (!o) {
     o = ++tt;
   }
   if (ql <= l && r <= qr) {
     return add(l, r, o, v);
   } else {
     int mid = l + r >> 1;
     push_down(l, r, o);
     if (ql <= mid) {
       modify(l, mid, lo[o], ql, qr, v);
     }
     if (qr > mid) {
       modify(mid + 1, r, ro[o], ql, qr, v);
     }
     push_up(o);
   }
 }
 struct info_t {
   int root;
   void init(vector<int>& nodes) {
     for (auto v : nodes) {
       for (int x = v; x; x = father[top[x]]) {
         modify(1, n, root, dfn[top[x]], dfn[x], 1);
       }
     }
   }
   void update(int u, int type) {
     for (int x = u; x; x = father[top[x]]) {
       modify(1, n, root, dfn[top[x]], dfn[x], type);
     }
   }
   double get_answer(int c) {
     double result = (double)C * C;
     result -= 2.0 * C * c * sum[root] / n;
     result += 1.0 * c * c * sum2[root] / n;
     return result;
   }
 } info[N];
 int main() {
   scanf( %d%d%d%d , &n, &m, &q, &C);
   for (int i = 1; i <= n; ++i) {
     scanf( %d , &a[i]);
     nodes[a[i]].push_back(i);
   }
   for (int i = 2; i <= n; ++i) {
     scanf( %d , &father[i]);
     graph[father[i]].push_back(i);
   }
   for (int i = 1; i <= m; ++i) {
     scanf( %d , &c[i]);
   }
   dfs1(1);
   dfs2(1, 1);
   for (int i = 1; i <= m; ++i) {
     info[i].init(nodes[i]);
   }
   while (q--) {
     int type, x, w;
     scanf( %d , &type);
     if (type == 1) {
       scanf( %d%d , &x, &w);
       info[a[x]].update(x, -1);
       a[x] = w;
       info[a[x]].update(x, 1);
     } else {
       scanf( %d , &x);
       printf( %.12lf
 , info[x].get_answer(c[x]));
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 const long long int mod = 1000000007;
 const long long int AM = 2e6 + 5;
 using namespace std;
 long long int powm(long long int a, long long int b, long long int n) {
   long long int x = 1, y = a;
   while (b > 0) {
     if (b % 2 == 1) {
       x = (x * y) % n;
     }
     y = (y * y) % n;
     b /= 2;
   }
   return x % n;
 }
 long long int pow(long long int x, long long int y) {
   long long int res = 1;
   while (y > 0) {
     if (y & 1) res = ((res) * (x));
     y = y >> 1;
     x = ((x) * (x));
   }
   return res;
 }
 long long int fact(long long int n) {
   long long int ans = 1;
   for (long long int i = 1; i <= n; i++) ans *= i;
   return ans;
 }
 long long int nCr(long long int n, long long int r) {
   if (r > n - r) r = n - r;
   long long int ans = 1, i;
   for (i = 1; i <= r; i++) {
     ans *= n - r + i;
     ans /= i;
   }
   return ans;
 }
 bool isPrime(long long int n) {
   if (n <= 1) return false;
   for (long long int i = 2; i < n; i++)
     if (n % i == 0) return false;
   return true;
 }
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   ;
   long long int n;
   cin >> n;
   long long int s = sqrt(n), f1, f2;
   if (s * s >= n) {
     f1 = s;
     f2 = s;
   } else if (s * (s + 1) >= n) {
     f1 = s;
     f2 = s + 1;
   } else {
     f1 = s + 1;
     f2 = s + 1;
   }
   cout << f1 + f2;
 }


#include <bits/stdc++.h>
 using namespace std;
 int f[305][305], rf[305][305], g[305][305], rg[305][305];
 int a[305][305];
 int n, m, s, t1, t2, t3;
 int ans1, ans2, ans3, ans4, Min = 1999999999;
 set<pair<int, int> > S;
 int C(int first, int second) {
   if (first < second) return t2;
   if (first == second) return t1;
   return t3;
 }
 int G(int i, int j, int k, int l) {
   int ans = (f[i][l] - f[i][k]) + (rf[j][l] - rf[j][k]);
   ans += (g[l][j] - g[l][i]);
   return ans;
 }
 int main() {
   scanf( %d %d %d , &n, &m, &s);
   scanf( %d %d %d , &t1, &t2, &t3);
   for (int i = 1; i <= n; i++)
     for (int j = 1; j <= m; j++) scanf( %d , &a[i][j]);
   for (int i = 1; i <= n; i++) {
     f[i][1] = 0;
     for (int j = 2; j <= m; j++)
       f[i][j] = f[i][j - 1] + C(a[i][j - 1], a[i][j]);
     rf[i][1] = 0;
     for (int j = 2; j <= m; j++)
       rf[i][j] = rf[i][j - 1] + C(a[i][j], a[i][j - 1]);
   }
   for (int i = 1; i <= m; i++) {
     g[i][1] = 0;
     for (int j = 2; j <= n; j++)
       g[i][j] = g[i][j - 1] + C(a[j - 1][i], a[j][i]);
     rg[i][1] = 0;
     for (int j = 2; j <= n; j++)
       rg[i][j] = rg[i][j - 1] + C(a[j][i], a[j - 1][i]);
   }
   for (int i = 1; i <= n; i++)
     for (int j = i + 2; j <= n; j++) {
       S.clear();
       for (int k = m; k >= 1; k--) {
         if (k <= m - 2) S.insert(make_pair(G(i, j, 1, k + 2), k + 2));
         if (S.empty()) continue;
         int tmp = s + f[i][k] + rf[j][k] - rg[k][j] + rg[k][i];
         set<pair<int, int> >::iterator it = S.upper_bound(make_pair(tmp, 0));
         if (it != S.end()) {
           pair<int, int> tmpg = *it;
           if (abs(tmpg.first - tmp) < Min) {
             Min = abs(tmpg.first - tmp);
             ans1 = i, ans2 = j, ans3 = k, ans4 = tmpg.second;
           }
         }
         if (it == S.begin()) continue;
         it--;
         pair<int, int> tmpg = *it;
         if (abs(tmpg.first - tmp) < Min) {
           Min = abs(tmpg.first - tmp);
           ans1 = i, ans2 = j, ans3 = k, ans4 = tmpg.second;
         }
       }
     }
   printf( %d %d %d %d
 , ans1, ans3, ans2, ans4);
   return 0;
 }


#include <bits/stdc++.h>
 template <class T>
 T abs(T x) {
   return x > 0 ? x : -x;
 }
 const int MAXINT = 1111 * 1111 * 1111;
 const long long MAXLINT = MAXINT * 1ll * MAXINT;
 const long double EPS = 1e-10;
 using namespace std;
 int n, m, x, y, z, p;
 int a, b;
 void reverse_left() {
   int aa = a, bb = b;
   a = m - b + 1;
   b = aa;
   swap(n, m);
 }
 void reverse_middle() { b = m - b + 1; }
 void reverse_right() {
   int aa = a, bb = b;
   b = n - a + 1;
   a = bb;
   swap(n, m);
 }
 void solve() {
   cin >> n >> m >> x >> y >> z >> p;
   int N = n, M = m;
   x %= 4;
   z %= 4;
   y %= 2;
   while (p--) {
     n = N;
     m = M;
     cin >> a >> b;
     for (int i = 0; i < x; i++) reverse_right();
     for (int i = 0; i < y; i++) reverse_middle();
     for (int i = 0; i < z; i++) reverse_left();
     cout << a << ' ' << b << endl;
   }
 }
 int main() { solve(); }


#include <bits/stdc++.h>
 using namespace std;
 set<long long> s;
 map<long long, long long> m;
 vector<long long> v;
 long long n, a[100010], b[100010], dp[100010], mx = 0;
 bool mark[100010];
 int main() {
   ios_base::sync_with_stdio(false);
   cin >> n;
   for (long long i = 1; i <= n; i++) {
     cin >> a[i];
     if (i == 1)
       dp[i] = a[i];
     else
       dp[i] = dp[i - 1] + a[i];
   }
   for (long long i = 1; i <= n; i++) cin >> b[i];
   for (long long i = n; i > 0; i--) {
     v.push_back(mx);
     long long r, l;
     mark[b[i]] = 1;
     if (b[i] < n) {
       if (mark[b[i] + 1] == 1) {
         set<long long>::iterator it = s.lower_bound(b[i]);
         r = *it;
       } else
         r = b[i];
     } else
       r = b[i];
     s.insert(r);
     if (b[i] > 1) {
       if (mark[b[i] - 1] == 1) {
         s.erase(b[i] - 1);
       }
     }
     m[r] = m[b[i] - 1] + dp[r] - dp[b[i] - 1];
     if (m[r] > mx) mx = m[r];
   }
   for (long long i = v.size() - 1; i >= 0; i--) cout << v[i] << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 void _print(long long t) { cerr << t; }
 void _print(int t) { cerr << t; }
 void _print(string t) { cerr << t; }
 void _print(char t) { cerr << t; }
 void _print(long double t) { cerr << t; }
 void _print(double t) { cerr << t; }
 void _print(unsigned long long t) { cerr << t; }
 template <class T, class V>
 void _print(pair<T, V> p);
 template <class T>
 void _print(vector<T> v);
 template <class T>
 void _print(vector<vector<T>> v);
 template <class T>
 void _print(set<T> v);
 template <class T, class V>
 void _print(map<T, V> v);
 template <class T>
 void _print(multiset<T> v);
 template <class T, class V>
 void _print(pair<T, V> p) {
   cerr <<  { ;
   _print(p.first);
   cerr <<  , ;
   _print(p.second);
   cerr <<  } ;
 }
 template <class T>
 void _print(vector<T> v) {
   cerr <<  [  ;
   for (T i : v) {
     _print(i);
     cerr <<    ;
   }
   cerr <<  ] ;
 }
 template <class T>
 void _print(vector<vector<T>> v) {
   cerr <<  ==>  << endl;
   for (vector<T> vec : v) {
     for (T i : vec) {
       _print(i);
       cerr <<    ;
     }
     cerr << endl;
   }
 }
 template <class T>
 void _print(set<T> v) {
   cerr <<  [  ;
   for (T i : v) {
     _print(i);
     cerr <<    ;
   }
   cerr <<  ] ;
 }
 template <class T>
 void _print(multiset<T> v) {
   cerr <<  [  ;
   for (T i : v) {
     _print(i);
     cerr <<    ;
   }
   cerr <<  ] ;
 }
 template <class T, class V>
 void _print(map<T, V> v) {
   cerr <<  [  ;
   for (auto i : v) {
     _print(i);
     cerr <<    ;
   }
   cerr <<  ] ;
 }
 mt19937_64 rang(
     chrono::high_resolution_clock::now().time_since_epoch().count());
 int rng(int lim) {
   uniform_int_distribution<int> uid(0, lim - 1);
   return uid(rang);
 }
 const int INF = 0x3f3f3f3f;
 const int mod = 1e9 + 7;
 long long mod_exp(long long a, long long b) {
   a %= mod;
   if (a == 0) return 0LL;
   long long res = 1LL;
   while (b > 0) {
     if (b & 1) res = (res * a) % mod;
     a = (a * a) % mod;
     b >>= 1;
   }
   return res;
 }
 long long mod_inv(long long a) { return mod_exp(a, mod - 2); }
 long long GCD(long long a, long long b) { return (b == 0) ? a : GCD(b, a % b); }
 void solve() {
   long long a1, a2, a3, a4, a5, a6;
   cin >> a1 >> a2 >> a3 >> a4 >> a5 >> a6;
   cout << ((a1 + a2 + a3) * (a1 + a2 + a3)) - (a1 * a1) - (a3 * a3) - (a5 * a5);
 }
 int main() {
   ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
   srand(chrono::high_resolution_clock::now().time_since_epoch().count());
   int t = 1;
   while (t--) {
     solve();
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 char s[1005];
 char t[1000005];
 int main() {
   scanf( %s , s);
   scanf( %s , t);
   int len1 = strlen(s);
   int len2 = strlen(t);
   int le = 0x3f3f3f3f;
   int ri = 0x3f3f3f3f;
   for (int i = 0, j = 0; j < len2; j++) {
     if (s[i] == t[j]) i++;
     if (i == len1) {
       le = j;
       break;
     }
   }
   for (int i = len1 - 1, j = len2 - 1; j >= 0; j--) {
     if (s[i] == t[j]) i--;
     if (i == -1) {
       ri = j;
       break;
     }
   }
   if ((le - ri) == (len1 - 1))
     cout <<  0  << endl;
   else {
     int ans = ri - le;
     cout << ans << endl;
   }
 }


#include <bits/stdc++.h>
 int N, K;
 std::string S;
 int main() {
   std::cin >> N >> K;
   std::cin >> S;
   std::string t = S;
   std::string s =   ;
   for (int l = 1; l < N; ++l) {
     if (S.substr(0, l) == S.substr(N - l, l)) {
       t = S.substr(0, N - l);
       s = S.substr(N - l, l);
     }
   }
   while (K--) std::cout << t;
   std::cout << s <<
 ;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 void solve() {
   int n;
   cin >> n;
   vector<int> candies(n);
   for (int i = 0; i < n; i++) cin >> candies[i];
   int i = 0, j = n - 1;
   int store_a = 0, store_b = 0;
   int turn = 0, sum_a = 0, sum_b = 0;
   while (i <= j) {
     if (turn % 2 == 0) {
       int str_temp = 0;
       while ((str_temp <= store_b) && (i <= j)) {
         str_temp += candies[i];
         i++;
       }
       store_a = str_temp;
       sum_a += store_a;
     } else {
       int str_tem = 0;
       while ((str_tem <= store_a) && (i <= j)) {
         str_tem += candies[j];
         j--;
       }
       store_b = str_tem;
       sum_b += store_b;
     }
     turn++;
   }
   cout << turn <<     << sum_a <<     << sum_b <<
 ;
 }
 int main() {
   int t;
   cin >> t;
   while (t) {
     solve();
     t--;
   }
   return 0;
 }


#include <bits/stdc++.h>
 #pragma GCC optimize( Ofast ,  unroll-loops ,  no-stack-protector )
 using namespace std;
 using ll = long long;
 using ld = double;
 using pii = pair<int, int>;
 using uint = unsigned int;
 using ull = unsigned long long;
 mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
 default_random_engine generator;
 void solve();
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(0);
   cout.tie(0);
   cout << setprecision(16) << fixed;
   cerr << setprecision(16) << fixed;
   int tests = 1;
   for (int test = 1; test <= tests; ++test) {
     solve();
   }
 }
 const int INF = 1e9;
 struct Point {
   int x, y;
 };
 void solve() {
   int n;
   cin >> n;
   vector<Point> p(n);
   for (int i = 0; i < n; ++i) {
     string s;
     cin >> s;
     int a = 0, b = 0;
     for (char c : s) {
       a += c == 'B';
       b += c == 'N';
     }
     p[i] = {a, b};
   }
   int minx = INF, maxx = -INF;
   int miny = INF, maxy = -INF;
   int l = INF, r = -INF;
   for (auto [x, y] : p) {
     minx = min(minx, x);
     maxx = max(maxx, x);
     miny = min(miny, y);
     maxy = max(maxy, y);
     l = min(l, x - y);
     r = max(r, x - y);
   }
   int res = 0;
   res = max(res, (maxx - minx + 1) / 2);
   res = max(res, (maxy - miny + 1) / 2);
   res = max(res, (minx - miny) - l);
   res = max(res, r - (minx - miny));
   Point best = {minx + res, miny + res};
   for (int op = 0; op < 2; ++op) {
     int xx = miny + l;
     int yy = miny;
     int cur = 0;
     cur = max(cur, (maxx - xx + 2) / 3);
     cur = max(cur, (maxy - yy + 1) / 2);
     cur = max(cur, (r - l + 1) / 2);
     if (cur < res && cur <= minx - xx) {
       res = cur;
       best = {xx + 2 * res, yy + res};
       if (op) {
         swap(best.x, best.y);
       }
     }
     for (int i = 0; i < n; ++i) {
       swap(p[i].x, p[i].y);
     }
     swap(minx, miny);
     swap(maxx, maxy);
     r *= -1;
     l *= -1;
     swap(l, r);
   }
   cout << res << endl;
   string ans;
   for (int i = 0; i < best.x; ++i) {
     ans += 'B';
   }
   for (int i = 0; i < best.y; ++i) {
     ans += 'N';
   }
   cout << ans << endl;
 }