#include <bits/stdc++.h>   using namespace std;   template <class T>   T Mul(T x, T y, T P) {     T F1 = 0;     while (y) {       if (y & 1) {         F1 += x;         if (F1 < 0 || F1 >= P) F1 -= P;       }       x <<= 1;       if (x < 0 || x >= P) x -= P;       y >>= 1;     }     return F1;   }   template <class T>   T Pow(T x, T y, T P) {     T F1 = 1;     x %= P;     while (y) {       if (y & 1) {         F1 = Mul(F1, x, P);       }       x = Mul(x, x, P);       y >>= 1;     }     return F1;   }   template <class T>   T Gcd(T x, T y) {     if (y == 0) return x;     T z;     while (z = x % y) {       x = y, y = z;     }     return y;   }   template <class T>   void UpdateMin(T &x, T y) {     if (y < x) {       x = y;     }   }   template <class T>   void UpdateMax(T &x, T y) {     if (x < y) {       x = y;     }   }   template <class T>   T Sqr(const T x) {     return x * x;   }   template <class T>   T Abs(const T x) {     return x < 0 ? -x : x;   }   class ReadBuffer {    private:     char buff[20000000];     char *buf;       public:     void init(int size = 20000000) {       fread(buff, 1, size, stdin);       buf = buff;     }     template <class T>     bool readInteger(T &x) {       x = 0;       while (*buf && isspace(*buf)) ++buf;       if (*buf == 0) return false;       static bool flag;       flag = 0;       if (*buf ==  - )         flag = true;       else         x = *buf -  0 ;       while (isdigit(*++buf)) x = x * 10 + *buf -  0 ;       if (flag) x = -x;       return true;     }     template <class T>     bool readFloat(T &x) {       long double nowpos = 0.1;       x = 0;       while (*buf && isspace(*buf)) ++buf;       if (*buf == 0) return false;       static bool flag, decimal;       decimal = flag = 0;       if (*buf ==  - )         flag = true, ++buf;       else if (*buf ==  . )         decimal = true;       while (isdigit(*buf) || *buf ==  . ) {         if (*buf ==  . )           decimal = true;         else {           if (decimal) {             x += nowpos * (*buf -  0 );             nowpos *= 0.1;           } else {             x = x * 10 + *buf -  0 ;           }         }         ++buf;       }       return true;     }     bool readChar(char c) {       if (*buf == 0) return 0;       return c = *buf++, 1;     }     bool readString(char *s) {       while (*buf && isspace(*buf)) ++buf;       if (!*buf) return false;       while (!isspace(*buf)) *s++ = *buf++;       *s++ = 0;       return true;     }     int countSpacetonext() {       int total = 0;       while (*buf && *buf ==    ) ++total, ++buf;       return total;     }     bool splitBycharactor(char *s, char Split =   n ) {       while (*buf && *buf != Split) *s++ = *buf++;       *s++ = 0;       return *buf != 0;     }   };   struct EDGE {     int T;     EDGE *Nxt;   };   long long F[31][31];   long long n;   int m;   struct matrix {     long long M[31][31];     long long *operator[](const int dn) { return M[dn]; }     void Rot(int x) {       for (int i = 0; i < m; ++i)         for (int j = 0; j <= m; ++j) F[i][j] = M[i][j];       int f = x;       for (int i = 0; i < m; ++i) {         M[f][m] = F[i][m];         int g = x;         for (int j = 0; j < m; ++j) {           M[f][g] = F[i][j];           if (++g == m) g = 0;         }         if (++f == m) f = 0;       }     }   } M[64][30], C, E;   long long now[33];   long long D[33];   vector<long long> Dec;   const int mod = 1000000007;   void Mul(matrix &A, matrix &B) {     for (int i = 0; i <= m; ++i)       for (int j = 0; j <= m; ++j)         for (int k = 0; k <= m; ++k) {           C[i][j] += B[i][k] * A[k][j];           if (C[i][j] >= mod) C[i][j] %= mod;         }     for (int i = 0; i <= m; ++i)       for (int j = 0; j <= m; ++j) A[i][j] = C[i][j], C[i][j] = 0;   }   void Mul(long long A[], matrix &B) {     for (int i = 0; i <= m; ++i)       for (int j = 0; j <= m; ++j) {         D[i] += B[i][j] * A[j];         if (D[i] >= mod) D[i] %= mod;       }     for (int i = 0; i <= m; ++i) A[i] = D[i], D[i] = 0;   }   void solve() {     cin >> n >> m;     for (int i = 0; i <= m; ++i) {       E[i][i] = 1;     }     for (int j = 0; j <= m; ++j) M[0][0][j][j] = M[0][0][0][j] = 1;     for (int i = 1; i < m; ++i) {       M[0][i] = M[0][0];       M[0][i].Rot(i);     }     while (n) {       Dec.push_back(n % m);       n /= m;     }     for (int i = 1; i < int(Dec.size()); ++i) {       M[i][0] = E;       for (int j = 0; j < m; ++j) {         Mul(M[i][0], M[i - 1][j]);       }       for (int j = 1; j < m; ++j) {         M[i][j] = M[i][0];         M[i][j].Rot(j);       }     }     now[m] = 1;     int presum = 0;     for (int i = int(Dec.size()) - 1; i >= 0; --i) {       for (int j = 0; j < Dec[i]; ++j) {         Mul(now, M[i][(j + presum) % m]);       }       presum += Dec[i];       if (presum >= m) presum %= m;     }     long long ans = 0;     for (int i = 0; i <= m; ++i) {       ans += now[i];       if (ans >= mod) ans -= mod;     }     cout << ans << endl;   }   int main() {     solve();     cin >> n;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int oo = 0x7fffffff;   const long long OO = 1000000000000000000LL;   const int base = 1000000007;   const int dx[] = {1, 0, 0, -1};   const int dy[] = {0, 1, -1, 0};   int x;   int main() {     ios_base::sync_with_stdio(false);     cin >> x;     if (x == 2) {       cout << 3 << endl;       return 0;     }     if (x == 3) {       cout << 5;       return 0;     }     for (int n = 1;; n += 2) {       int count = (n / 2 + 1) * (n / 2 + 1) + (n / 2) * (n / 2);       if (count >= x) {         cout << n;         return 0;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     vector<pair<unsigned long long, unsigned long long> > v;     unsigned long long target;     cin >> target;     unsigned long long base_l = 1, base_r = target + 1;     for (unsigned long long n = 1; n * (n + 1) * (2 * n + 1) <= 6 * target; n++) {       unsigned long long l = base_l, r = base_r;       unsigned long long base_n1 = n * (n + 1) * (2 * n + 1),                          base_n2 = 3 * n * (n + 1);       while (r - l > 1) {         unsigned long long mid = (r + l) / 2;         if (base_n1 + (mid - n) * base_n2 <= 6 * target)           l = mid;         else           r = mid;       }       base_r = r;       if (base_n1 + (l - n) * base_n2 == 6 * target) v.push_back({n, l});     }     cout << 2 * v.size() - (v.back().first != v.back().second ? 0 : 1) <<   n ;     for (auto p : v) cout << p.first <<     << p.second <<   n ;     for (int i = v.size() - 1; i >= 0; i--) {       auto p = v[i];       unsigned long long a = p.first, b = p.second;       if (a != b) cout << b <<     << a <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool f(string s1, string s2) {     if (s1 == s2) return true;     bool ans = false;     string t1(s1.begin(), s1.begin() + s1.size() / 2);     string t2(s1.begin() + s1.size() / 2, s1.end());     string t3(s2.begin(), s2.begin() + s2.size() / 2);     string t4(s2.begin() + s2.size() / 2, s2.end());     if (s1.size() % 2 == 0 && s2.size() % 2 == 0) {       ans = ans | (f(t1, t3) && f(t2, t4));     }     if (!ans)       if (s1.size() % 2 == 0 && s2.size() % 2 == 0) {         ans = ans | (f(t1, t4) && f(t2, t3));       }     return ans;   }   string cf(string s) {     if (s.size() & 1) return s;     string s1 = cf(s.substr(0, s.size() / 2));     string s2 = cf(s.substr(s.size() / 2, s.size()));     if (s1 < s2)       return s1 + s2;     else       return s2 + s1;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     string s1, s2;     cin >> s1 >> s2;     if (cf(s1) == cf(s2))       cout <<  YES  << endl;     else       cout <<  NO  << endl;   }


#include <bits/stdc++.h>   using namespace std;   bool visit[204];   vector<int> dependency[204];   int col[203];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> col[i];     }     for (int i = 1; i <= n; i++) {       int k;       cin >> k;       for (int j = 1; j <= k; j++) {         int m;         cin >> m;         dependency[i].push_back(m);       }     }     int ans = INT_MAX;     for (int l = 1; l <= 3; l++) {       int hours = 0, cur = l, vis = 0;       for (int i = 1; i <= n; i++) {         visit[i] = false;       }       while (vis != n) {         bool cvis = false;         for (int i = 1; i <= n; i++) {           if (!visit[i] && col[i] == cur) {             bool cr = true;             for (int j = 0; j < dependency[i].size(); j++) {               if (!visit[dependency[i][j]]) {                 cr = false;                 break;               }             }             if (cr) {               visit[i] = true;               vis++;               cvis = true;               hours++;               break;             }           }         }         if (!cvis) {           cur = 1 + (cur) % 3;           hours++;         }       }       ans = min(ans, hours);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int hcf(long long a, long long b) {     if (b == 0) return a;     return hcf(b, a % b);   }   void solve() {     long long n;     cin >> n;     long long x = hcf(n + 1, 4 * n);     long long lcm = ((4 * n) * (n + 1)) / x;     cout << lcm / (n + 1) + 1;   }   int main() {     int t = 1;     cin >> t;     while (t--) {       solve();       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a, n, i, k, l, r, d, kol1, kol2, ans;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n >> k;     for (i = 1; i <= n; i++) {       cin >> a;       if (a < k)         kol1++;       else if (a > k)         kol2++;     }     l = n;     r = n * 3;     while (l <= r) {       d = (l + r) / 2;       if (((d + 1) / 2 - 1 >= kol1) && (d - (d + 1) / 2 >= kol2)) {         r = d - 1;         ans = d - n;       } else         l = d + 1;     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long maxn = 1e5 + 10;   int to[maxn << 1], nex[maxn << 1], head[maxn << 1], e, cnt;   int u[maxn << 1], v[maxn << 1];   int dep[maxn << 1], siz[maxn << 1], son[maxn << 1], top[maxn << 1],       fa[maxn << 1], id[maxn << 1];   int sum[maxn << 2];   int n;   void add(int x, int y) {     to[++e] = y;     nex[e] = head[x];     head[x] = e;   }   int ls(int p) { return p << 1; }   int rs(int p) { return p << 1 | 1; }   void push_up(int p) { sum[p] = sum[ls(p)] + sum[rs(p)]; }   int query(int p, int l, int r, int qx, int qy) {     if (qx <= l && qy >= r) {       return sum[p];     }     int res = 0;     int mid = (l + r) >> 1;     if (qx <= mid) res = res + query(ls(p), l, mid, qx, qy);     if (qy > mid) {       res = res + query(rs(p), mid + 1, r, qx, qy);     }     return res;   }   void update(int p, int l, int r, int pos, int val) {     if (l == r) {       sum[p] = val;       return;     }     int mid = (l + r) >> 1;     if (pos <= mid) {       update(ls(p), l, mid, pos, val);     }     if (pos > mid) update(rs(p), mid + 1, r, pos, val);     push_up(p);   }   int getrange(int x, int y) {     int ans = 0;     while (top[x] != top[y]) {       if (dep[top[x]] < dep[top[y]]) swap(x, y);       ans = ans + query(1, 1, n, id[top[x]], id[x]);       x = fa[top[x]];     }     if (dep[x] > dep[y]) swap(x, y);     ans = ans + query(1, 1, n, id[x] + 1, id[y]);     return ans;   }   void dfs1(int x, int f, int deep) {     fa[x] = f;     siz[x] = 1;     dep[x] = deep;     int maxson = -1;     for (int i = head[x]; i; i = nex[i]) {       int y = to[i];       if (y == f) continue;       dfs1(y, x, deep + 1);       siz[x] += siz[y];       if (maxson < siz[y]) {         maxson = siz[y];         son[x] = y;       }     }   }   void dfs2(int x, int topf) {     id[x] = ++cnt;     top[x] = topf;     if (!son[x]) return;     dfs2(son[x], topf);     for (int i = head[x]; i; i = nex[i]) {       int y = to[i];       if (y == fa[x] || y == son[x]) continue;       dfs2(y, y);     }   }   int lca(int x, int y) {     while (top[x] != top[y]) {       if (dep[top[x]] < dep[top[y]]) {         swap(x, y);       }       x = fa[top[x]];     }     if (dep[x] > dep[y]) swap(x, y);     return x;   }   int main() {     memset(sum, 0, sizeof(sum));     scanf( %d , &n);     for (int i = 1; i < n; i++) {       scanf( %d%d , &u[i], &v[i]);       add(u[i], v[i]);       add(v[i], u[i]);     }     dfs1(1, 0, 1);     dfs2(1, 0);     int m;     scanf( %d , &m);     while (m--) {       int op;       scanf( %d , &op);       if (op == 1) {         int x;         scanf( %d , &x);         if (dep[u[x]] > dep[v[x]])           update(1, 1, n, id[u[x]], 0);         else           update(1, 1, n, id[v[x]], 0);       }       if (op == 2) {         int x;         scanf( %d , &x);         if (dep[u[x]] > dep[v[x]]) {           update(1, 1, n, id[u[x]], 1);         } else {           update(1, 1, n, id[v[x]], 1);         }       }       if (op == 3) {         int x, y;         scanf( %d%d , &x, &y);         if (getrange(x, y)) {           printf( -1 n );         } else {           printf( %d n , dep[x] + dep[y] - 2 * dep[lca(x, y)]);         }       }     }   }


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


#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)); }   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) {     normal(a);     return modPow(a, 1000000007 - 2);   }   inline long long modDiv(long long a, long long b) {     return modMul(a, modInverse(b));   }   int dx[] = {0, 0, +1, -1};   int dy[] = {+1, -1, 0, 0};   int n, a[15][15], num[15][15], cur[105], dp[2][5][5][105], lagbe[2][5][105];   vector<int> vt[5][105];   int knightx[] = {-2, -2, +2, +2, -1, -1, +1, +1};   int knighty[] = {-1, +1, -1, +1, +2, -2, +2, -2};   void valid(int ro, int col) {     for (int i = 0; i < 8; i++) {       int tmp = ro + knightx[i];       int tpp = col + knighty[i];       if (tmp > 0 && tmp <= n && tpp > 0 && tpp <= n)         vt[1][num[ro][col]].push_back(num[tmp][tpp]);     }     for (int i = 1; i <= 10; i++) {       int tmp = ro + i;       int tpp = col + i;       if (tmp > 0 && tmp <= n && tpp > 0 && tpp <= n)         vt[2][num[ro][col]].push_back(num[tmp][tpp]);       tmp = ro - i;       tpp = col - i;       if (tmp > 0 && tmp <= n && tpp > 0 && tpp <= n)         vt[2][num[ro][col]].push_back(num[tmp][tpp]);       tmp = ro + i;       tpp = col - i;       if (tmp > 0 && tmp <= n && tpp > 0 && tpp <= n)         vt[2][num[ro][col]].push_back(num[tmp][tpp]);       tmp = ro - i;       tpp = col + i;       if (tmp > 0 && tmp <= n && tpp > 0 && tpp <= n)         vt[2][num[ro][col]].push_back(num[tmp][tpp]);     }     for (int i = 1; i <= 10; i++) {       int tmp = ro + i;       int tpp = col;       if (tmp > 0 && tmp <= n && tpp > 0 && tpp <= n)         vt[3][num[ro][col]].push_back(num[tmp][tpp]);       tmp = ro - i;       tpp = col;       if (tmp > 0 && tmp <= n && tpp > 0 && tpp <= n)         vt[3][num[ro][col]].push_back(num[tmp][tpp]);       tmp = ro;       tpp = col + i;       if (tmp > 0 && tmp <= n && tpp > 0 && tpp <= n)         vt[3][num[ro][col]].push_back(num[tmp][tpp]);       tmp = ro;       tpp = col - i;       if (tmp > 0 && tmp <= n && tpp > 0 && tpp <= n)         vt[3][num[ro][col]].push_back(num[tmp][tpp]);     }   }   bool one(int beg, int fin, int ro, int col, int &mov, int &change) {     if (ro == col) {       change++;       return true;     }     if (beg != fin) return false;     for (int i = 0; i < vt[beg][ro].size(); i++)       if (vt[beg][ro][i] == col) {         mov++;         return true;       }     return false;   }   bool berkoro(int num, int beg, int fin, int ro, int col, int &mov,                int &change) {     int res = 0, val1 = 0, val2 = 0;     if (num == 1) return one(beg, fin, ro, col, mov, change);     for (int i = 0, tmp; !res && i < vt[beg][ro].size(); i++) {       tmp = vt[beg][ro][i];       val1 = 1, val2 = 0;       res |= berkoro(num - 1, beg, fin, tmp, col, val1, val2);     }     for (int i = 1; !res && i <= 3; i++) {       if (beg == i) continue;       val2 = 1, val1 = 0;       res |= berkoro(num - 1, i, fin, ro, col, val1, val2);     }     if (res) mov += val1, change += val2;     return res;   }   void process(int beg, int fin, int ro, int col, int &mov, int &change) {     for (int i = 1; i <= 4; i++) {       mov = 0, change = 0;       if (berkoro(i, beg, fin, ro, col, mov, change)) return;     }   }   void process(int mark, int pos, int &mov, int &change) {     int &ret1 = lagbe[0][mark][pos];     int &ret2 = lagbe[1][mark][pos];     if (ret1 != -1) {       mov += ret1;       change += ret2;       return;     }     if (pos == n * n) return;     int val1 = 0, val2 = 0, res1 = 1e9, res2 = 1e9;     if (pos == 1) {       for (int i = 1; i <= 3; i++)         for (int j = 1; j <= 3; j++) {           val1 = dp[0][i][j][pos], val2 = dp[1][i][j][pos];           process(j, pos + 1, val1, val2);           if (val1 + val2 < res1 + res2)             res1 = val1, res2 = val2;           else if (val1 + val2 == res1 + res2 && val2 < res2)             res1 = val1, res2 = val2;         }     } else {       for (int i = 1; i <= 3; i++) {         val1 = dp[0][mark][i][pos], val2 = dp[1][mark][i][pos];         process(i, pos + 1, val1, val2);         if (val1 + val2 < res1 + res2)           res1 = val1, res2 = val2;         else if (val1 + val2 == res1 + res2 && val2 < res2)           res1 = val1, res2 = val2;       }     }     mov += res1, change += res2;     ret1 = res1, ret2 = res2;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     cin >> n;     for (int i = 1, cnt = 1; i <= n; i++)       for (int j = 1; j <= n; j++) {         cin >> a[i][j];         num[i][j] = cnt;         cur[a[i][j]] = cnt;         cnt++;       }     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) valid(i, j);     for (int i = 1; i < n * n; i++) {       for (int k = 1; k <= 3; k++)         for (int l = 1; l <= 3; l++)           process(k, l, cur[i], cur[i + 1], dp[0][k][l][i], dp[1][k][l][i]);     }     memset(lagbe, -1, sizeof(lagbe));     int mov = 0, change = 0;     process(0, 1, mov, change);     cout << mov + change <<     << change <<   n ;     return 0;   }


#include<bits/stdc++.h>   #define pb push_back   #define all(v) v.begin(),v.end()   #define allr(v) v.rbegin(),v.rend()   #define SORT(v) sort(all(v))   #define int long long   #define ff first   #define ss second   #define mod 1000000007      using namespace std;      void oj()   {     ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);     #ifndef ONLINE_JUDGE       freopen( input.txt ,  r , stdin);     freopen( output.txt ,  w , stdout);     #endif   }      void solve()   {     int n, k; cin>>n>>k;     vector<int>h(n);     vector<pair<int,int>>val;        for(int i = 0; i<n; i++)       cin>>h[i];        int mini = h[0];     int maxi = h[0] + k;        for(int i = 1; i<n - 1; i++){          if(h[i] + 2*k - 1 <= mini || h[i] >= maxi)       {         cout<< NO <<endl;          return;       }             maxi = min(maxi + k - 1LL, h[i] + 2 * k - 1LL);       mini = max(mini - k + 1LL, h[i] );        }        if(h[n-1] + k <= mini || h[n-1] >= maxi)     {       cout<< NO <<endl;         return;     }     cout<< YES <<endl;            }   signed main()   {     oj();     int t; cin>>t;     while(t--)       solve();        return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int NMAX = 2020;   vector<int> Gph[2][NMAX];   int N, A, B, K;   int st[2][NMAX], dr[2][NMAX], edges[2][NMAX];   int val[2][NMAX][NMAX];   int dp[NMAX / 2];   void dfs(int node) {     if (node != 1) edges[K][node] = 1;     for (int i = 0; i < Gph[K][node].size(); i++) {       int v = Gph[K][node][i];       dfs(v);       st[K][node] = min(st[K][node], st[K][v]);       dr[K][node] = max(dr[K][node], dr[K][v]);       edges[K][node] += edges[K][v];     }     val[K][st[K][node]][dr[K][node]] =         max(val[K][st[K][node]][dr[K][node]], edges[K][node]);   }   int main() {     scanf( %d%d , &N, &A);     int x;     K = 0;     st[K][1] = A + 1;     dr[K][1] = 0;     for (int i = 2; i <= A; i++) {       st[K][i] = A + 1;       dr[K][i] = 0;       scanf( %d , &x);       Gph[K][x].push_back(i);     }     for (int i = 1; i <= N; i++) {       scanf( %d , &x);       st[K][x] = dr[K][x] = i;     }     dfs(1);     scanf( %d , &B);     K = 1;     st[K][1] = B + 1;     dr[K][1] = 0;     for (int i = 2; i <= B; i++) {       st[K][i] = B + 1;       dr[K][i] = 0;       scanf( %d , &x);       Gph[K][x].push_back(i);     }     for (int i = 1; i <= N; i++) {       scanf( %d , &x);       st[K][x] = dr[K][x] = i;     }     dfs(1);     for (int i = 1; i <= N; i++)       for (int j = i; j <= N; j++)         dp[j] = max(dp[j], dp[i - 1] + max(val[0][i][j], val[1][i][j]));     printf( %d , dp[N]);     return 0;   }


#include <bits/stdc++.h>   struct Mock {     Mock(int n, const std::string &s) : n(n), s(s) {}     std::vector<std::string> ask(int l, int r) {       if (l > r) {         return {};       }       std::vector<std::string> substrings;       for (int i = l - 1; i < r; ++i) {         for (int j = 1; i + j <= r; ++j) {           auto ss = s.substr(i, j);           std::shuffle(ss.begin(), ss.end(), gen);           substrings.push_back(ss);         }       }       std::shuffle(substrings.begin(), substrings.end(), gen);       return substrings;     }     void out(const std::string &t) { assert(s == t); }     int n;       private:     std::string s;     std::mt19937 gen;   };   struct IO {     IO() { scanf( %d , &n); }     std::vector<std::string> ask(int l, int r) {       if (l > r) {         return {};       }       printf( ? %d %d n , l, r);       fflush(stdout);       std::vector<char> buf(r - l + 2);       std::vector<std::string> substrings;       int size = (r - l + 2) * (r - l + 1) / 2;       for (int i = 0; i < size; ++i) {         scanf( %s , buf.data());         substrings.push_back(buf.data());       }       return substrings;     }     void out(const std::string &t) {       printf( ! %s n , t.c_str());       fflush(stdout);     }     int n;   };   static const int C = 26;   using Count = std::array<int, C>;   Count toCount(const std::string &s) {     Count result;     memset(result.data(), 0, sizeof(result));     for (char c : s) {       result[c -  a ]++;     }     return result;   }   template <typename IO>   std::string solve(IO &io) {     int n = io.n;     if (n == 1) {       return io.ask(1, 1)[0];     }     int n2 = n >> 1;     std::vector<int> s(n, 0);     {       std::map<Count, int> halfs;       for (auto &&s : io.ask(1, n2)) {         halfs[toCount(s)]++;       }       for (auto &&s : io.ask(2, n2)) {         halfs[toCount(s)]--;       }       std::vector<std::pair<int, Count>> counts;       for (auto &&kv : halfs) {         if (kv.second) {           int size = 0;           for (int c : kv.first) {             size += c;           }           counts.emplace_back(size, kv.first);         }       }       std::sort(counts.begin(), counts.end());       assert(static_cast<int>(counts.size()) == n2);       for (int i = 0; i < n2; ++i) {         auto count = counts[i].second;         for (int j = 0; j < i; ++j) {           count[s[j]]--;         }         while (!count[s[i]]) {           s[i]++;         }       }     }     {       std::vector<Count> counts(n + 1);       for (auto &&s : io.ask(1, n)) {         auto count = toCount(s);         for (int j = 0; j < C; ++j) {           counts[s.length()][j] += count[j];         }       }       for (int i = n + 1 >> 1; i >= 1; --i) {         auto count = counts[i];         for (int j = 0; j < C; ++j) {           count[j] -= counts[i - 1][j];         }         int left = i - 1, right = n - i;         for (int j = left; j < right; ++j) {           count[s[j]]--;         }         while (!count[s[right]]) {           s[right]++;         }       }     }     std::string buf(n,    );     for (int i = 0; i < n; ++i) {       buf[i] =  a  + s[i];     }     return buf;   }   int main() {     IO io;     io.out(solve(io));   }


#include <bits/stdc++.h>   using namespace std;   const long long LIM = 1e18;   long long root(long long x) {     long long L = 0LL, R = 1e9 + 7;     while (L < R - 1) {       long long mid = (L + R) >> 1LL;       if (mid * mid <= x)         L = mid;       else         R = mid;     }     return L;   }   int main() {     set<long long> s;     for (long long i = 2; i <= 1e6; i++) {       for (long long j = i * i * i; j <= LIM; j *= i) {         long long val = sqrt(j);         if (val * val != j) s.insert(j);         if (j > LIM / i) break;       }     }     vector<long long> v(s.begin(), s.end());     int t;     scanf( %d , &t);     while (t--) {       long long L, R;       scanf( %lld %lld , &L, &R);       long long ans =           upper_bound(v.begin(), v.end(), R) - lower_bound(v.begin(), v.end(), L);       ans += root(R) - root(L - 1);       printf( %lld n , ans);     }     return 0;   }


#include <iostream>   #include <bits/stdc++.h>      using namespace std;      int main () {    int t;    cin >> t;    for (int k=0; k<t;k++){     int n;     cin >> n;     int a[n];     for (int i=0; i<n; i++){      cin >> a[i];     }     for (int j=n-1; j>=0; j--){      if(a[j]+j < n){       a[j] = a[j]+a[a[j]+j];      }     }     int max=0;     for (int i=0; i<n; i++){      if(a[i]>max){       max=a[i];      }     }     cout << max << endl;    }    return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long ncr_2(long long n, long long k) {     if (k > n) return 0;     if (k * 2 > n) k = n - k;     if (k == 0) return 1;     long long result = n;     for (long long i = 2; i <= k; ++i) {       result = result * (n - i + 1);       result /= i;     }     return result;   }   long long pow(int x, unsigned int y, int p) {     int res = 1;     x = x % p;     if (x == 0) return 0;     while (y > 0) {       if (y & 1) res = (res * x) % p;       y = y >> 1;       x = (x * x) % p;     }     return res;   }   long long _pow(int a, int b) {     if (!b) return 1;     int temp = _pow(a, b / 2);     temp = (temp * temp);     if (b % 2) return (a * temp);     return temp;   }   int invmod(int a, int m) { return pow(a, m - 2, m); }   long long feelthepower(long long x, long long y) {     long long res = 1LL;     while (y) {       if (y & 1) {         res = (res * 1LL * x) % 1000000007;       }       x = (x * 1LL * x) % 1000000007;       y /= 2;     }     return res;   }   vector<int> Adj[100001], A;   int cnt = 0;   vector<bool> vis(100001, false);   void dfs(int s) {     if (vis[s]) return;     vis[s] = true;     A.push_back(s);     cnt++;     for (auto v : Adj[s]) {       if (!vis[v]) {         dfs(v);       }     }   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     ;     int tc = 1;     for (long long ti = 0; ti < tc; ti++) {       int n;       cin >> n;       vector<int> V(n);       map<int, int> mp;       for (long long i = 0; i < n; i++) {         cin >> V[i];         mp[V[i]] = i + 1;       }       vector<int> vec(V);       sort(vec.begin(), vec.end());       for (long long i = 0; i < n; i++) {         Adj[mp[vec[i]]].push_back(i + 1);         Adj[i + 1].push_back(mp[vec[i]]);       }       vector<int> Store[100001];       int j = 0, cnt1 = 0;       for (int i = 1; i <= n; i++) {         if (!vis[i]) {           cnt = 0;           dfs(i);           Store[j].push_back(cnt);           for (auto x : A) {             Store[j].push_back(x);           }           A.clear();           j++;         }       }       cout << j << endl;       for (long long i = 0; i < j; i++) {         for (long long k = 0; k < Store[i].size(); k++) {           cout << Store[i][k] <<    ;         }         cout << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const long long inf = 1e16;   const int maxn = 1e5;   pair<long long, int> tree[4 * maxn + 11];   long long ans[maxn + 11];   long long minn[4 * maxn + 11], lazy[4 * maxn + 11];   set<pair<long long, int> > t;   void push_up(int rt) { tree[rt] = min(tree[rt << 1], tree[rt << 1 | 1]); }   void build(int rt, int l, int r) {     if (l == r) {       tree[rt] = make_pair(inf, l);       return;     }     int mid = (l + r) >> 1;     build(rt << 1, l, mid);     build(rt << 1 | 1, mid + 1, r);     push_up(rt);   }   void update(int rt, int l, int r, int pos, long long val) {     if (l > pos || r < pos) return;     if (l == r) {       tree[rt] = make_pair(val, l);       return;     }     int mid = (l + r) >> 1;     update(rt << 1, l, mid, pos, val);     update(rt << 1 | 1, mid + 1, r, pos, val);     push_up(rt);   }   void Push_up(int rt) { minn[rt] = min(minn[rt << 1], minn[rt << 1 | 1]); }   void push_down(int rt) {     if (minn[rt << 1] < inf) {       minn[rt << 1] -= lazy[rt];       lazy[rt << 1] += lazy[rt];     }     if (minn[rt << 1 | 1] < inf) {       minn[rt << 1 | 1] -= lazy[rt];       lazy[rt << 1 | 1] += lazy[rt];     }     lazy[rt] = 0;     minn[rt << 1] = max(minn[rt << 1], 0ll);     minn[rt << 1 | 1] = max(minn[rt << 1 | 1], 0ll);   }   void update(int rt, int l, int r, int al, int ar, long long val) {     if (l > ar || r < al) return;     if (l >= al && r <= ar) {       minn[rt] -= val;       minn[rt] = max(minn[rt], 0ll);       lazy[rt] += val;       return;     }     int mid = (l + r) >> 1;     if (lazy[rt]) push_down(rt);     update(rt << 1, l, mid, al, ar, val);     update(rt << 1 | 1, mid + 1, r, al, ar, val);     Push_up(rt);   }   pair<long long, int> query(int rt, int l, int r, int al, int ar) {     if (l > ar || r < al) return make_pair(inf, maxn + 1);     if (l >= al && r <= ar) return tree[rt];     int mid = (l + r) >> 1;     return min(query(rt << 1, l, mid, al, ar),                query(rt << 1 | 1, mid + 1, r, al, ar));   }   pair<long long, int> query(int rt, int l, int r) {     int mid = (l + r) >> 1;     if (l == r) return make_pair(minn[rt], l);     if (lazy[rt]) push_down(rt);     if (minn[rt << 1] <= minn[rt << 1 | 1]) return query(rt << 1, l, mid);     return query(rt << 1 | 1, mid + 1, r);   }   void upd(int rt, int l, int r, int pos, long long val) {     if (l > pos || r < pos) return;     if (l == r) {       minn[rt] = val;       return;     }     int mid = (l + r) >> 1;     if (lazy[rt]) push_down(rt);     upd(rt << 1, l, mid, pos, val);     upd(rt << 1 | 1, mid + 1, r, pos, val);     Push_up(rt);   }   int main() {     int n;     long long p;     cin >> n >> p;     build(1, 1, n);     for (int i = 1; i <= 4 * n; i++) minn[i] = inf, lazy[i] = 0;     for (int i = 1; i <= n; i++) {       long long x;       cin >> x;       update(1, 1, n, i, x);       upd(1, 1, n, i, x);     }     int out = 0;     queue<int> q;     set<int> s;     long long Pas = 0;     long long res = p;     while (out < n) {       if (q.empty()) {         pair<long long, int> pi = query(1, 1, n);         Pas += pi.first;         update(1, 1, n, 1, n, pi.first);         s.insert(pi.second);         q.push(pi.second);         upd(1, 1, n, pi.second, inf);       } else {         int lef = *s.begin();         if (lef == 1) {           Pas += p;           res = p;           update(1, 1, n, 1, n, p);           ans[q.front()] = Pas;           update(1, 1, n, q.front(), inf);           s.erase(q.front());           q.pop();           out++;         } else {           pair<long long, int> pi = query(1, 1, n, 1, lef - 1);           if (Pas + res > pi.first) {             res -= pi.first - Pas;             q.push(pi.second);             upd(1, 1, n, pi.second, inf);             s.insert(pi.second);           } else {             Pas += p;             res = p;             update(1, 1, n, 1, n, p);             ans[q.front()] = Pas;             update(1, 1, n, q.front(), inf);             s.erase(q.front());             q.pop();             out++;           }         }       }     }     for (int i = 1; i <= n; i++) cout << ans[i] <<    ;   }


#include <bits/stdc++.h>   using namespace std;      typedef long long ll;   typedef pair<ll,ll> pi;   typedef vector <ll> vi;   typedef vector <pi> vpi;   typedef pair<pi,ll> pii;   typedef set <ll> si;   typedef long double ld;   #define f first   #define s second   #define FOR(i,s,e) for(ll i=s;i<=ll(e);++i)   #define DEC(i,s,e) for(ll i=s;i>=ll(e);--i)   #define pb push_back   #define all(x) (x).begin(), (x).end()   #define lbd(x, y) lower_bound(all(x), y)   #define ubd(x, y) upper_bound(all(x), y)   #define aFOR(i,x) for (auto i: x)   #define mem(x,i) memset(x,i,sizeof x)   #define fast ios_base::sync_with_stdio(false),cin.tie(0)      int N,Q,a,b;   string S,F;   struct node{    int s,e,m, val, lazy = 0, sz;    node *l,*r;    node (int ss,int ee){     s = ss; e = ee; m = (s+e) / 2; sz = e - s + 1;     if (s != e){      l = new node(s,m); r = new node(m+1,e);     }    }        void prop(){     if (lazy == 0 || s == e) return;     l->lazy = r->lazy = lazy;     l->val = lazy * l->sz; r->val = lazy * r->sz;     lazy = 0;    }        void upd(int a,int b,int c){     prop();     if (a <= s && e <= b){      lazy = c;      val = c * sz;     }else if (a > e || s > b) return;     else{      l->upd(a,b,c); r->upd(a,b,c);      val = l->val + r->val;     }    }        int qry(int a,int b){     prop();     if (a <= s && e <= b) return val;     else if (a > e || s > b) return 0;     else return l->qry(a,b) + r->qry(a,b);    }   }*root;      void solve(){    cin >> N >> Q >> S >> F;    swap(S,F);    root = new node(1,N);       FOR(i,0,N-1) root->upd(i + 1,i + 1, (S[i] ==  0  ? -1 : 1));        vpi v;    FOR(i,1,Q){     cin >> a >> b;     v.pb(pi(a,b));    }    reverse(all(v));    aFOR(i,v){     a = i.f, b = i.s;          int x = root->qry(a,b);     if (x == 0){      cout <<  NO n ; return;     }else if (x < 0) root->upd(a,b,-1);     else root->upd(a,b,1);    }    FOR(i,0,N-1){     if (root->qry(i+1,i+1) != (F[i] ==  0  ? -1 : 1)){      cout <<  NO n ; return;     }    }    cout <<  YES n ;   }   int main(){    fast;    int TC;    cin >> TC;    while (TC--) solve();   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int N;     cin >> N;     int black = 0, white = 1000000001;     cout << 0 <<     << 0 << endl;     cout << flush;     string S;     cin >> S;     if (S ==  white ) swap(white, black);     for (int i = 1; i < N; i++) {       int mid = (white + black) / 2;       cout << mid <<     << 0 << endl;       cout << flush;       cin >> S;       if (S ==  black ) {         black = mid;       } else {         white = mid;       }     }     if (black + 1 < white || white + 1 < black)       cout << (black + white) / 2 <<     << 1 <<     << (black + white) / 2 <<                << 2 << endl;     else {       if (black < white) {         if (black > 0)           cout << black <<     << 1 <<     << black - 1 <<     << 3 << endl;         else           cout << black + 1 <<     << 1 <<     << black + 2 <<     << 3 << endl;       } else {         if (white > 0)           cout << white <<     << 1 <<     << white - 1 <<     << 3 << endl;         else           cout << white + 1 <<     << 1 <<     << white + 2 <<     << 3 << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long modPow(long long a, long long b);   long long modInv(long long a);   void solve() {     long long n;     cin >> n;     long long a[n];     for (long long i = 0; i < n; i++) cin >> a[i];     long long dp[n + 1][n + 1];     memset(dp, 0, sizeof(dp));     long long ans = 0;     for (long long i = 0; i < n; i++) {       for (long long j = 0; j < i - 1; j++) {         dp[a[j]][a[i - 1]]++;       }       for (long long j = i + 1; j < n; j++) {         ans += dp[a[i]][a[j]];       }     }     cout << ans <<   n ;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long test = 1;     cin >> test;     while (test--) solve();   }   long long modPow(long long a, long long b) {     if (b == 0) return 1;     if (b % 2 == 0) {       long long x = a * a;       x %= 1000000007;       return modPow(x, b / 2);     }     return (a * modPow(a, b - 1)) % 1000000007;   }   long long modInv(long long a) { return modPow(a, 1000000007 - 2); }


#include <bits/stdc++.h>   using namespace std;   char grid[2010][2010];   bool vis[2010][2010];   int dx[] = {0, 0, 1, -1};   int dy[] = {-1, 1, 0, 0};   bool valid(int x, int y, int n, int m) {     if (x == n || y == m || x == -1 || y == -1 || vis[x][y] ||         grid[x][y] ==  * ) {       return 0;     }     return 1;   }   int main() {     int n, m;     int sr, sc;     int x, y;     scanf( %d%d%d%d%d%d , &n, &m, &sr, &sc, &x, &y);     sr--, sc--;     for (int i = 0; i < n; i++) {       scanf( %s , grid[i]);     }     int ans = 0;     queue<pair<pair<int, int>, pair<int, int> > > q[2];     int curQ = 0, nxtQ = 1;     q[curQ].push(make_pair(make_pair(sr, sc), make_pair(x, y)));     while (q[curQ].size()) {       while (q[curQ].size()) {         pair<pair<int, int>, pair<int, int> > cur = q[curQ].front();         q[curQ].pop();         int curX = cur.first.first;         int curY = cur.first.second;         int rem[] = {cur.second.first, cur.second.second};         if (valid(curX, curY, n, m) == 0) {           continue;         }         ans++;         vis[curX][curY] = 1;         for (int i = 0; i < 4; i++) {           if (i < 2) {             if (rem[i] == 0) continue;             rem[i]--;             q[nxtQ].push(make_pair(make_pair(curX + dx[i], curY + dy[i]),                                    make_pair(rem[0], rem[1])));             rem[i]++;           } else {             q[curQ].push(make_pair(make_pair(curX + dx[i], curY + dy[i]),                                    make_pair(rem[0], rem[1])));           }         }       }       curQ = !curQ;       nxtQ = !nxtQ;     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   const int MOD = 1e9 + 7;   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int n, i;     cin >> n;     while (n--) {       long long int k, t;       cin >> k >> t;       cout << t + (k - 1) * 9 <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int mxn = 5e5 + 123;   const int inf = 1e9;   const int mod = 1e9 + 7;   int a[mxn], n;   vector<int> v;   int main() {     long long s;     cin >> s;     for (long long i = 1; i <= s; i++) {       long long n;       cin >> n;       long long sum = 0;       while (n > 0) {         if (n % 2 == 1) sum++;         n /= 2;       }       cout << (1ll << sum) << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 300000 + 10;   long long num[maxn], dp[maxn][15];   void Solve() {     int n, m, k;     scanf( %d%d%d , &n, &m, &k);     for (int i = 1; i <= n; i++) scanf( %lld , &num[i]);     long long ans = 0;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         if (j == 1)           dp[i][j] = max(dp[i - 1][m] + num[i] - k, num[i] - k);         else {           if (i < j) break;           dp[i][j] = dp[i - 1][j - 1] + num[i];         }         ans = max(ans, dp[i][j]);       }     }     printf( %lld n , ans);   }   int main() {     Solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   long long c;   int last, l, r;   int q[200010];   long long a[400010], ans[200010];   int main() {     scanf( %d%lld , &n, &c);     for (int i = 1; i < 2 * n; i++) scanf( %lld , &a[i]), a[i] += a[i - 1];     for (int i = 1; i <= n; i++) {       while (last < i && a[2 * i - 1] - a[2 * last] >= c) last++;       while (l <= r && a[2 * i - 1] - a[2 * q[l]] >= c) l++;       if (last) ans[i] = ans[last - 1] + c;       if (l <= r) ans[i] = max(ans[i], a[2 * i - 1] + ans[q[l]] - a[2 * q[l]]);       while (l <= r && ans[i] - a[2 * i] >= ans[q[r]] - a[2 * q[r]]) r--;       q[++r] = i;     }     printf( %lld n , ans[n]);   }


#include <bits/stdc++.h>   using namespace std;   long long H[200005];   int a[200005];   long long c[200005 << 2];   long long s[200005 << 2];   long long lz[200005 << 2];   vector<int> v[200005];   int u;   int yl, yr;   void build(int o, int l, int r) {     if (l == r) {       lz[o] = l;       c[o] = l;       s[o] = l;       return;     }     int mid = (l + r) >> 1;     build(o * 2, l, mid);     build(o * 2 + 1, mid + 1, r);     lz[o] = -1;     c[o] = c[o * 2] + c[o * 2 + 1];     s[o] = min(s[o * 2], s[o * 2 + 1]);   }   void pushdown(int o, int l, int r) {     if (l == r) return;     if (lz[o] == -1) return;     lz[o * 2] = lz[o];     lz[o * 2 + 1] = lz[o];     s[o * 2] = lz[o];     s[o * 2 + 1] = lz[o];     int mid = (l + r) >> 1;     c[o * 2] = lz[o] * (mid - l + 1);     c[o * 2 + 1] = lz[o] * (r - mid);     lz[o] = -1;   }   void update(int o, int l, int r) {     if (yl <= l && yr >= r) {       c[o] = 1LL * u * (r - l + 1);       s[o] = u;       lz[o] = u;       return;     }     pushdown(o, l, r);     int mid = (l + r) >> 1;     if (yl <= mid) update(o * 2, l, mid);     if (yr > mid) update(o * 2 + 1, mid + 1, r);     c[o] = c[o * 2] + c[o * 2 + 1];     s[o] = min(s[o * 2], s[o * 2 + 1]);     if (lz[o * 2] == lz[o * 2 + 1] && lz[o * 2] >= 0) lz[o] = lz[o * 2];   }   int query(int o, int l, int r) {     if (l == r && c[o] < u) return l;     if (l == r && c[o] >= u) return 0;     if (lz[o] != -1 && lz[o] < u) return r;     if (lz[o] != -1 && lz[o] >= u) return 0;     int mid = (l + r) >> 1;     if (s[o * 2 + 1] < u)       return query(o * 2 + 1, mid + 1, r);     else       return query(o * 2, l, mid);   }   int main() {     int n;     scanf( %d , &n);     int mx = -1;     memset(a, 0, sizeof(a));     for (int i = 1; i < n + 1; ++i) {       int tmp;       scanf( %d , &tmp);       a[tmp] = i;       mx = max(mx, tmp);     }     for (int i = 1; i <= mx; ++i) {       for (int j = 1; j * i <= mx; ++j) {         if (a[j * i]) v[i].push_back(a[j * i]);       }     }     for (int i = 0; i <= mx; ++i) sort(v[i].begin(), v[i].end());     long long sum = 1LL * n * (n + 1);     build(1, 1, n);     for (int i = mx; i >= 0; --i) {       H[i] = sum - c[1];       int k = v[i].size();       if (k < 2) {         continue;       }       u = n + 1;       yl = v[i][1] + 1;       yr = n;       if (yl <= yr) update(1, 1, n);       u = v[i][k - 1];       int pos = query(1, 1, n);       pos = min(pos, v[i][1]);       if (pos > v[i][0] && pos <= v[i][1]) {         yl = v[i][0] + 1;         yr = pos;         update(1, 1, n);       }       u = v[i][k - 2];       pos = query(1, 1, n);       pos = min(pos, v[i][0]);       if (pos >= 1 && pos <= v[i][0]) {         yl = 1;         yr = pos;         update(1, 1, n);       }     }     long long ans = 0;     for (int i = 1; i <= mx; ++i) {       ans += 1LL * i * (H[i] - H[i - 1]);     }     printf( %I64d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   const int MM = 1e9 + 7;   const double eps = 1e-8;   const int MAXN = 2e6 + 10;   int n, m;   int a[MAXN], Q[MAXN], cnt[MAXN], ans[MAXN];   int f[MAXN];   void prework() {}   void read() {}   int gao(int k) {     if (f[k] != -1) return f[k];     int ret = 0;     int now = 0;     for (int i = 1; i <= n; i++) {       if (cnt[a[i]] == 0) {         if (now == k) {           ret++;           while (now) {             cnt[Q[now--]] = 0;           }           now = 1;           Q[now] = a[i];           cnt[a[i]] = 1;         } else {           Q[++now] = a[i];           cnt[a[i]] = 1;         }       } else {         ;       }     }     ret += (now > 0);     while (now) {       cnt[Q[now--]] = 0;     }     return f[k] = ret;   }   void work(int l, int r) {     if (l >= r || gao(l) == gao(r)) {       for (int i = l; i <= r; i++) ans[i] = gao(l);       return;     }     m = (l + r) >> 1;     work(l, m);     work(m + 1, r);   }   void solve(int casi) {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);     }     for (int i = 0; i <= n; i++) f[i] = -1;     work(1, n);     for (int i = 1; i <= n; i++) printf( %d%c , ans[i],    n [i == n]);   }   void printans() {}   int main() {     prework();     int T = 1;     for (int i = 1; i <= T; i++) {       read();       solve(i);       printans();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, l, m, sol;   int a[10];   bool r[333][333];   bool ok;   int main() {     cin >> n;     for (int(i) = (0); (i) < (n); ++(i)) cin >> a[i];     if (n == 1) {       if (a[0] == 0)         cout <<  BitAryo  << endl;       else         cout <<  BitLGM  << endl;       return 0;     }     if (n == 2) {       r[0][0] = false;       for (int(i) = (0); (i) < (a[0] + 1); ++(i))         for (int(j) = (0); (j) < (a[1] + 1); ++(j)) {           if (i + j == 0) continue;           ok = false;           m = min(i, j);           while (m > 0) {             ok |= (r[i - m][j - m] == 0);             m--;           }           m = i;           while (m > 0) {             ok |= (r[i - m][j] == 0);             m--;           }           m = j;           while (m > 0) {             ok |= (r[i][j - m] == 0);             m--;           }           r[i][j] = ok;         }       if (r[a[0]][a[1]])         cout <<  BitLGM  << endl;       else         cout <<  BitAryo  << endl;       return 0;     }     if (a[0] ^ a[1] ^ a[2])       cout <<  BitLGM  << endl;     else       cout <<  BitAryo  << endl;     return 0;   }


#include <bits/stdc++.h>   const long long LL_INF = (long long)2e18 + 5;   using namespace std;   const long long N = 2e5 + 10;   void solve() {     long long n, a0, a, b0, b, k;     cin >> n;     vector<long long> v(n), pref(n + 1);     ;     {       for (auto &i : v) cin >> i;     }     sort(v.begin(), v.end()), reverse(v.begin(), v.end());     for (long long i = (long long)(1); i < (long long)(n + 1); i++)       pref[i] = pref[i - 1] + v[i - 1];     cin >> a0 >> a >> b0 >> b >> k;     if (a0 < b0) swap(a0, b0), swap(a, b);     auto sum = [&](long long l, long long r) { return pref[r] - pref[l]; };     long long ctall = 0, cta = 0, ctb = 0;     for (long long i = (long long)(1); i < (long long)(n + 1); i++) {       if (i % a == 0 and i % b == 0)         ctall++;       else if (i % a == 0)         cta++;       else if (i % b == 0)         ctb++;       if (sum(0, ctall) * (a0 + b0) / 100 + sum(ctall, ctall + cta) * a0 / 100 +               sum(ctall + cta, ctall + cta + ctb) * b0 / 100 >=           k) {         cout << i <<   n ;         return;       }     }     cout <<  -1 n ;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long tt;     cin >> tt;     while (tt--) solve();   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 5;   struct cate {     long long num, t;     bool operator<(const cate& other) const { return t > other.t; }   } a[maxn];   int n;   set<long long> has;   map<long long, long long> fa;   long long find(long long x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n;     for (int i = 1; i <= n; ++i) {       cin >> a[i].num;       fa[a[i].num] = a[i].num;     }     for (int i = 1; i <= n; ++i) cin >> a[i].t;     sort(a + 1, a + n + 1);     long long ans = 0;     for (int i = 1; i <= n; ++i) {       long long cur = a[i].num;       if (has.find(cur) != has.end()) {         cur = find(cur);         cur += 1;         fa[cur] = cur;         ans += (cur - a[i].num) * a[i].t;       }       has.insert(cur);       if (has.find(cur - 1) != has.end()) fa[cur - 1] = cur;       if (has.find(cur + 1) != has.end()) fa[cur] = cur + 1;     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long LL_MIN = std::numeric_limits<long long>::min();   const long long LL_MAX = std::numeric_limits<long long>::max();   template <class T, class R>   ostream& operator<<(ostream& o, const pair<T, R>& p) {     o <<  (  << p.first <<  ,   << p.second <<  ) ;     return o;   }   template <class T, class R>   istream& operator>>(istream& o, pair<T, R>& p) {     o >> p.first >> p.second;     return o;   }   template <class T>   string to_string(const T& x) {     stringstream o;     o <<  [ ;     for (__typeof((x).begin()) i = ((x).begin()); i != (x).end(); ++i) {       o << ((i != x.begin()) ?  ,   :   ) << *i;     }     o <<  ] ;     string r;     getline(o, r);     return r;   }   template <class T>   T get_next(string& s) {     stringstream ss(s);     T r;     ss >> r;     getline(ss, s);     return r;   }   template <class T>   void split_string(const string& s, vector<T>& o) {     stringstream ss(s);     o.clear();     T x;     while (ss >> x) {       o.push_back(x);     }   }   void trim(string& s) {     stringstream ss;     ss << s;     s.clear();     ss >> s;   }   template <class T, class R>   ostream& operator<<(ostream& o, const map<T, R>& x) {     o << to_string(x);     return o;   }   template <class T>   ostream& operator<<(ostream& o, const vector<T>& x) {     o << to_string(x);     return o;   };   template <class T>   ostream& operator<<(ostream& o, const set<T>& x) {     o << to_string(x);     return o;   };   template <class T>   ostream& operator<<(ostream& o, const deque<T>& x) {     o << to_string(x);     return o;   };   const long long INFL = 1000000000LL * 1000000000LL + 1000LL;   const int INF = 1000000000 + 1000;   const long long MOD = 1000000009;   struct Tri {     int a, b, c;     Tri(int a, int b, int c) : a(a), b(b), c(c) {}   };   queue<Tri> q;   int n, m, a, b;   vector<int> graf[510];   bool sol[510][510][2];   int dist[510][510][2], f[510][510][2];   int main() {     scanf( %d %d , &n, &m);     for (int i = 1; i <= m; i++) {       scanf( %d %d , &a, &b);       graf[a].push_back(b);       graf[b].push_back(a);     }     dist[1][n][0] = 0;     q.push(Tri(1, n, 0));     sol[1][n][0] = true;     while (q.size()) {       Tri t = q.front();       q.pop();       if (t.c == 0) {         for (__typeof((graf[t.a]).begin()) a = ((graf[t.a]).begin());              a != (graf[t.a]).end(); ++a) {           if (!sol[*a][t.b][1]) {             sol[*a][t.b][1] = true;             dist[*a][t.b][1] = dist[t.a][t.b][0] + 1;             q.push(Tri(*a, t.b, 1));             f[*a][t.b][1] = t.a;           }         }       } else {         for (__typeof((graf[t.b]).begin()) b = ((graf[t.b]).begin());              b != (graf[t.b]).end(); ++b) {           if (!sol[t.a][*b][0] && t.a != *b) {             sol[t.a][*b][0] = true;             dist[t.a][*b][0] = dist[t.a][t.b][1];             q.push(Tri(t.a, *b, 0));             f[t.a][*b][0] = t.b;           }         }       }     }     if (!sol[n][1][0]) {       printf( -1 n );     } else {       printf( %d n , dist[n][1][0]);       a = n;       b = 1;       vector<int> ra, rb;       ra.push_back(n);       rb.push_back(1);       while (make_pair(a, b) != make_pair(1, n)) {         rb.push_back(f[a][b][0]);         b = f[a][b][0];         ra.push_back(f[a][b][1]);         a = f[a][b][1];       }       for (__typeof((rb).begin()) it = ((rb).begin()); it != (rb).end(); ++it) {         printf( %d  , *it);       }       printf(  n );       for (__typeof((ra).begin()) it = ((ra).begin()); it != (ra).end(); ++it) {         printf( %d  , *it);       }       printf(  n );     }   }


#include <bits/stdc++.h>   using namespace std;   int present(int *lib, int k, int x) {     int i;     for (i = 0; i < k; i++) {       if (lib[i] == x) return 1;     }     return 0;   }   int findMax(int *lib, int k, queue<int> *q, int n) {     int i, max = -1, maxi = 0;     for (i = 0; i < k; i++) {       if (q[lib[i]].empty()) return i;       if (max < q[lib[i]].front()) {         max = q[lib[i]].front();         maxi = i;       }     }     return maxi;   }   int main() {     int n, k, i, ans = 0, size = 0, z;     scanf( %d%d , &n, &k);     int *lib = (int *)calloc(k, sizeof(int));     int *arr = (int *)calloc(n, sizeof(int));     queue<int> q[n + 1];     for (i = 0; i < n; i++) {       scanf( %d , &arr[i]);       q[arr[i]].push(i);     }     for (i = 0; i < n; i++) {       if (present(lib, k, arr[i]) == 0) {         if (size < k) {           q[arr[i]].pop();           lib[size++] = arr[i];           ans++;         } else {           q[arr[i]].pop();           z = findMax(lib, k, q, n);           lib[z] = arr[i];           ans++;         }       } else {         q[arr[i]].pop();       }     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   const int maxn = 3e5 + 5;   using namespace std;   long long n, m;   int main() {     scanf( %d%d , &n, &m);     if (min(n, m) == 1) {       long long t = max(n, m);       long long ti = t / 6, res = t % 6;       printf( %lld n , ti * 6 + max(0ll, res - 3) * 2);       return 0;     }     if ((n % 2) && (m % 2)) {       printf( %lld n , n * m - 1);     } else if (n % 2 == 0 && m % 2 == 0) {       if (max(n, m) <= 2)         printf( 0 n );       else         printf( %lld n , n * m);     } else {       if (min(n, m) == 2) {         long long t = max(n, m);         if (t == 3 || t == 7)           printf( %lld n , n * m - 2);         else           printf( %lld n , n * m);       } else         printf( %lld n , n * m);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   double p[600][600], pp[600][600];   double f[600][600];   bool checkInRound(int I, int K, int Round) {     int i = I - 1;     int k = K - 1;     if (i / (1 << Round) != k / (1 << Round)) return false;     if (i / (1 << (Round - 1)) == k / (1 << (Round - 1))) return false;     return true;   }   int main() {     memset(f, 0, sizeof(f));     int N;     cin >> N;     int SL = 1 << N;     for (int i = 1; i <= SL; i++) {       int temp;       for (int j = 1; j <= SL; j++) {         scanf( %d , &temp);         p[i][j] = temp * 0.01;       }     }     int Round = N;     for (int i = 1; i <= SL; i++) {       if (i % 2) {         f[i][1] = p[i][i + 1];       } else {         f[i][1] = p[i][i - 1];       }     }     for (int i = 1; i <= SL; i++) pp[i][1] = f[i][1];     for (int R = 2; R <= Round; R++) {       for (int i = 1; i <= SL; i++) {         for (int k = 1; k <= SL; k++) {           if (checkInRound(i, k, R)) {             pp[i][R] += pp[i][R - 1] * pp[k][R - 1] * p[i][k];           }         }       }     }     for (int R = 2; R <= Round; R++) {       for (int i = 1; i <= SL; i++) {         double mem = 0;         for (int k = 1; k <= SL; k++) {           if (checkInRound(i, k, R)) {             f[i][R] += pp[i][R - 1] * pp[k][R - 1] * p[i][k] * (1 << (R - 1));             mem = max(mem, f[k][R - 1]);           }         }         f[i][R] += mem + f[i][R - 1];       }     }     double ans = 0;     for (int i = 1; i <= SL; i++) ans = max(ans, f[i][Round]);     printf( %.12f , ans);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, x, p, m = 0;     stack<int> s;     cin >> n;     p = n % 2;     for (int i = 0; i < n; i++) {       cin >> x;       m = max(x, m);       if (!s.empty() && s.top() < x) cout <<  NO , exit(0);       if (s.empty() || (s.top() != x))         s.push(x);       else         s.pop();     }     if (p) {       if (s.size() == 0) {         cout <<  YES ;       } else if (s.size() == 1 && s.top() == m) {         cout <<  YES ;       } else {         cout <<  NO ;       }     } else {       if (s.size())         cout <<  NO ;       else         cout <<  YES ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool check(char s) {     if (s ==  A  || s ==  H  || s ==  I  || s ==  M  || s ==  O  || s ==  T  ||         s ==  U  || s ==  V  || s ==  W  || s ==  X  || s ==  Y )       return true;     else       return false;   }   int main() {     int n, i, x, p;     bool ans = true;     string st;     cin >> st;     n = st.size();     if (n == 1 && check(st[0]) == true) {       cout <<  YES n ;       ;     } else if (n == 2 && st[0] == st[1] && check(st[0]) == true) {       cout <<  YES n ;       ;     } else if (n >= 3) {       ans = true;       x = 0, p = n - 1;       while (x <= p) {         if (st[x] == st[p] && check(st[x]) == true) {           x += 1, p -= 1;         } else {           ans = false;           break;         }       }       if (ans == false) {         cout <<  NO n ;         ;       } else {         cout <<  YES n ;         ;       }     } else {       cout <<  NO n ;       ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int count[26] = {0}, c = 0, i;     string str;     getline(cin, str);     for (i = 1; i <= str.length() - 1; i++) {       if (str[i] ==  }  || str[i] ==  , ) count[str[i - 1] -  a ]++;     }     for (i = 0; i <= 25; i++)       if (count[i] > 0) c++;     printf( %d n , c);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     cin.tie(0);     ios_base::sync_with_stdio(0);     long long t;     cin >> t;     while (t--) {       vector<long long> v(7);       for (auto& e : v) cin >> e;       sort(v.begin(), v.end());       cout << v[0] <<     << v[1] <<    ;       if (v[2] != v[1] + v[0])         cout << v[2] << endl;       else         cout << v[3] << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[1000100], n, m;   int sum[1000100], now;   long long ans;   int idx, cnt;   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]), ans += a[i];     if (ans % m) {       puts( No );       return 0;     }     ans /= m;     for (int i = 1; i <= n; i++) {       cnt += a[i], now++;       if (cnt == ans) {         sum[++idx] = now;         now = 0, cnt = 0;       } else if (cnt > ans) {         puts( No );         return 0;       }     }     puts( Yes );     for (int i = 1; i <= idx; i++) printf( %d  , sum[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int read(int f = 1, int x = 0, char ch =    ) {     while (!isdigit(ch = getchar()))       if (ch ==  - ) f = -1;     while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar();     return f * x;   }   const int N = 2e5 + 5;   int m, n, T, s[2][N], c[2], f[2], ans, q[N], t[N], type[N], tot;   long long a, b;   int main() {     m = read();     while (m--) {       n = read(), T = read(), a = read(), b = read();       c[0] = c[1] = f[0] = f[1] = ans = 0;       for (int i = 1; i <= n; ++i) type[i] = read(), s[0][i] = s[1][i] = 0;       for (int i = 1; i <= n; ++i) q[i] = t[i] = read();       sort(q + 1, q + 1 + n), tot = unique(q + 1, q + 1 + n) - q - 1;       for (int i = 1; i <= n; ++i) {         int j = lower_bound(q + 1, q + 1 + tot, t[i]) - q;         ++s[type[i]][j], ++c[type[i]];       }       q[tot + 1] = max(q[tot] + 1, T + 1);       for (int i = 0; i <= tot; ++i) {         f[0] += s[0][i], f[1] += s[1][i];         int t = min(q[i + 1] - 1, T), resa = c[0] - f[0], resb = c[1] - f[1];         ;         if (f[0] * a + f[1] * b > t) continue;         int rest = t - (f[0] * a + f[1] * b);         if (resa * a <= rest)           ans = max(f[0] + f[1] + resa + min((rest - resa * a) / b, 1ll * resb),                     1ll * ans);         else           ans = max(f[0] + f[1] + rest / a, 1ll * ans);       }       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:66777216 )   using namespace std;   const double eps = 1e-7;   const double pi = acos(-1.0);   const long long INF = (long long)2e9 + 1;   const long long LINF = (long long)8e18;   const long long inf = (long long)2e9 + 1;   const long long linf = (long long)8e18;   const long long MM = (long long)1e9 + 7;   int solve();   void gen();   int main() {     solve();     return 0;   }   const int dd = 3e5 + 7;   int get(int t) {     while (t < 0) t += 360;     while (t > 360) t -= 360;     return min(t, 360 - t);   }   int solve() {     long long x;     cin >> x;     x *= -1;     x %= 360;     vector<pair<int, int> > M;     M.push_back(make_pair(get(x), 0));     M.push_back(make_pair(get(x + 90), 1));     M.push_back(make_pair(get(x + 180), 2));     M.push_back(make_pair(get(x + 270), 3));     sort(M.begin(), M.end());     cout << M[0].second;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 10 + 1e6;   const int INF = 1e9 + 10;   long long a, b;   map<long long, int> f;   map<long long, long long> dad;   long long ans[100];   void dfs(long long x, int s) {     if (x > b) return;     if (f[2 * x] == 0 || f[2 * x] > s + 1) {       dad[2 * x] = x;       f[2 * x] = s + 1;       dfs(2 * x, s + 1);     }     if (f[10 * x + 1] == 0 || f[10 * x + 1] > s + 1) {       dad[10 * x + 1] = x;       f[10 * x + 1] = s + 1;       dfs(10 * x + 1, s + 1);     }   }   int main() {     cin >> a >> b;     dad[a] = a;     dfs(a, 1);     if (f[b] == 0)       cout <<  NO ;     else {       cout <<  YES  << endl;       int l = 0;       while (dad[b] != b) {         ans[++l] = b;         b = dad[b];       }       l++;       ans[l] = a;       cout << l << endl;       for (int i = (l), _b = (1); i >= _b; i--) cout << ans[i] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s, t;     cin >> s >> t;     for (int i = 0; i < s.size(); i++) {       if (s[i] == t[s.size() - i - 1]) {         continue;       } else {         cout <<  NO ;         return 0;       }     }     cout <<  YES ;   }


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


#include <bits/stdc++.h>   using namespace std;   int main() {     long long t;     cin >> t;     while (t--) {       double n, x;       cin >> n >> x;       vector<double> a(n);       for (__typeof(n) i = 0; i < n; i++) {         cin >> a[i];       }       sort(a.rbegin(), a.rend());       a.push_back(0);       double no = 1, am = a[0];       while ((am / no >= x) && (no <= n)) {         am += a[no];         no++;       }       cout << no - 1 <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int fromStrtoInt(string s, string c) {     int x = 0;     for (int i = 0; i < c.size(); i++) {       x *= 10;       x += s[c[i] -  0 ] -  0 ;     }     return x;   }   int main() {     int n, k;     cin >> n >> k;     string s, arr[n];     for (int i = 0; i < n; i++) cin >> arr[i];     for (int i = 0; i < k; i++) s += i +  0 ;     int a[n];     int ans = 1e9;     do {       for (int i = 0; i < n; i++) a[i] = fromStrtoInt(arr[i], s);       sort(a, a + n);       ans = min(ans, a[n - 1] - a[0]);     } while (next_permutation(s.begin(), s.end()));     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, x, y, ans = 0;     scanf( %d , &n);     while (n--) scanf( %d%d , &x, &y), ans = max(ans, x + y);     printf( %d , ans);   }


#include <bits/stdc++.h>   using namespace std;   long long int full = 0;   long long int arr[51][51];   long long int n, t;   void solve(long long int i, long long int j, long long int level) {     if (level > n - 1) {       return;     } else {       if (arr[i][j] < (1ll << level)) {         arr[i][j]++;         if (arr[i][j] == (1ll << level)) {           full++;         }       } else {         solve(i + 1, j + 1, level + 1);         solve(i + 1, j, level + 1);       }     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> n >> t;     for (long long int i = 0; i < 10ll; i++) {       for (long long int j = 0; j < 10ll; j++) {         arr[i][j] = 0;       }     }     for (long long int i = 1; i <= t; i++) {       solve(0, 0, 0);     }     cout << full << endl;   }


#include <bits/stdc++.h>   using namespace std;   int V[102][102], M2[2][102];   int main() {     int n, a;     scanf( %d , &n);     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++) {         scanf( %d , &a);         M2[0][i] += a;         M2[1][j] += a;       }     int cnt = 0;     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++)         if (M2[0][i] < M2[1][j]) cnt++;     cout << cnt << endl;   }


#include <bits/stdc++.h>   unsigned long long c;   int n, i, j, k, ks, ke, pd, pp, pa;   char s[1000100];   bool f, ff, fff;   int main() {     scanf( %s , s);     n = strlen(s);     for (i = 0; i < n; i++) {       if (s[i] >=  a  && s[i] <=  z ) {         k = 0, f = 1, ff = 0, fff = 0, ks = 1, ke = 0, pa = 0, pd = 0, pp = 0;         for (j = i + 1; f && j < n; j++) {           if (!k) {             if (s[j] ==  @ )               ++k, pa = j;             else if (!((s[j] >=  a  && s[j] <=  z ) ||                        (s[j] >=  0  && s[j] <=  9 ) || s[j] ==  _ ))               f = 0;             else if (s[j] >=  a  && s[j] <=  z )               ++ks;           } else if (k == 1) {             if (s[j] ==  . )               ++k, pd = j;             else if ((s[j] >=  a  && s[j] <=  z ) || (s[j] >=  0  && s[j] <=  9 ))               ff = 1;             else               f = 0;           } else {             if (s[j] >=  a  && s[j] <=  z )               fff = 1, ++ke, pp = j;             else               f = 0;           }         }         if (ff && fff) c += (unsigned long long)ks * ke;         if (pa)           i = pa;         else if (pd)           i = pd;         else if (pp)           i = pp - 1;         else           i = j - 1;       }     }     printf( %I64u n , c);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2000;   int n, i, j;   char s[maxn];   long long F[maxn][maxn];   long long P[maxn];   int main() {     scanf( %d , &n);     scanf( %s , s);     P[1] = 1;     for (i = 2; i <= 18; ++i) P[i] = 10LL * P[i - 1];     memset(F, 0, sizeof F);     for (i = 0; i <= n; ++i)       for (j = 0; j <= n; ++j) {         if (i)           F[i][j] =               max(F[i][j], F[i - 1][j] + P[i - 1] * (s[n + n - i - j] -  0 ));         if (j)           F[i][j] =               max(F[i][j], F[i][j - 1] + P[j - 1] * (s[n + n - i - j] -  0 ));       }     i = n, j = n;     while (i || j) {       if (i && F[i][j] == (F[i - 1][j] + P[i - 1] * (s[n + n - i - j] -  0 )))         putchar( H ), --i;       else         putchar( M ), --j;     }     printf(  n );     return 0;   }


#include <bits/stdc++.h>   int n, from[300005], p[300005], dis[300005], a[300005], b[300005];   std::queue<int> que;   std::set<int, std::greater<int>> l;   void work(int now) {     if (now != n) work(from[now]), std::cout << p[now] <<    ;   }   void print(int now) {     std::cout << dis[now] + 1 << std::endl;     work(now);     std::cout << 0 << std::endl;     exit(0);   }   signed main() {     std::ios::sync_with_stdio(false);     std::cin >> n;     for (int i = 1; i <= n; ++i) std::cin >> a[i];     for (int i = 1; i <= n; ++i) std::cin >> b[i];     for (int i = 0; i <= n; ++i) l.insert(i);     std::fill(dis, dis + n, INT_MAX);     que.push(n);     while (!que.empty()) {       int now = que.front();       que.pop();       if (now - a[now] <= 0) print(now);       for (std::set<int>::iterator i = l.lower_bound(now);            i != l.end() && now - *i <= a[now]; l.erase(i++)) {         int tmp = *i + b[*i];         if (dis[tmp] > dis[now] + 1)           dis[tmp] = dis[now] + 1, p[tmp] = *i, from[tmp] = now, que.push(tmp);       }     }     std::cout << -1 << std::endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MAX = -1000000000000000000;   const long long MIN = 1000000000000000000;   const long long inf = 1000000000;   const long long KOK = 100000;   const long long LOG = 30;   const long long li = 500005;   const long long mod = 1000000007;   int n, m, b[li], a[li], k, flag, t, yes, say, ind, cev1, mn, cev2;   int cev;   string s;   vector<int> v;   int main(void) {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> s;     for (int i = 0; i < (int)s.size(); i++) {       if (s[i] ==  # ) {         say++;         ind = i;       }     }     for (int i = 0; i < (int)s.size(); i++) {       if (ind == i) {         if (cev <= 0) {           printf( -1 n );           return 0;         }         a[1] = cev;         cev = 0;         continue;       }       if (i > ind) {         if (s[i] ==  ( )           cev++;         else if (s[i] ==  )  && cev > 0) {           cev--;         } else if (s[i] ==  )  && a[1] > 1) {           a[1]--;         } else {           printf( -1 n );           return 0;         }       } else {         if (s[i] ==  ( )           cev++;         else           cev--;         if (cev < 0) {           printf( -1 n );           return 0;         }       }     }     if (cev > 0) {       printf( -1 n );       return 0;     }     for (int i = 1; i < say; i++) printf( 1 n );     printf( %d n , a[1]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     int n;     string s;     cin >> n;     cin >> s;     sort(s.begin(), s.end());     cout << s;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n, k;     cin >> n >> k;     long long a;     int g = 0;     for (int i = 0; i < n; i++) {       cin >> a;       set<int> d;       while (a) {         if (a % 10 <= k) {           d.insert(a % 10);         }         a /= 10;       }       if (d.size() == k + 1) {         g++;       }     }     cout << g;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     set<int> st;     for (int i = 2; i <= n; i++) {       int a = i;       for (int j = 2; j * j <= a; j++) {         int num = 1;         while (a % j == 0 and a > 1) {           a /= j;           num *= j;         }         if (num != 1) st.insert(num);       }       if (a > 1) st.insert(a);     }     cout << st.size() << endl;     for (auto item : st) cout << item <<    ;   }


#define _CRT_SECURE_NO_WARNINGS   #include <iostream>   #include <vector>   #include <algorithm>   #include <string>   #include <fstream>   #include <set>   #include <deque>   #include <math.h>   #include <iomanip>   #define ll long long   #define ld long double      using namespace std;      int all = 0;   int price = 0;      void dfs(int u, vector<int>& used, vector<int> &col, vector<vector<int>>& graph) {       all++;       if (used[u] != col[u]) {           price++;       }       for (int v : graph[u]) {           if (used[v] == -1) {               used[v] = 1 - used[u];               dfs(v, used, col, graph);           }       }   }      int main() {       ios_base::sync_with_stdio(0);       cin.tie(0);       //freopen( input.txt ,  r , stdin);       //freopen( output.txt ,  w , stdout);       int n;       cin >> n;       vector<int> col(n);       vector<pair<int, int>> arr(n);       vector<int> L(2 * n);       for (int i = 0; i < 2 * n; ++i) {           L[i] = -1;       }       for (int i = 0; i < n; ++i) {           cin >> arr[i].first >> arr[i].second;           if (arr[i].first > arr[i].second) {               col[i] = 1;               swap(arr[i].first, arr[i].second);           }           arr[i].first--;           arr[i].second--;           L[arr[i].first] = i;       }       for (int i = 0; i < n; ++i) {           if (L[i] == -1) {               cout <<  -1 n ;               return 0;           }       }       vector<int> r(n);       for (int i = 0; i < n; ++i) {           r[i] = arr[L[i]].second;       }       vector<int> mn(n);       vector<int> mni(n);       vector<int> mx(n);       vector<int> mxi(n);       mn[0] = r[0];       mni[0] = 0;       mx[n - 1] = r[n - 1];       mxi[n - 1] = n - 1;       for (int i = 1; i < n; ++i) {           if (r[i] < mn[i - 1])               mni[i] = i;           else               mni[i] = mni[i - 1];           mn[i] = min(mn[i - 1], r[i]);       }       for (int i = n - 2; i >= 0; --i) {           if (r[i] > mx[i + 1])               mxi[i] = i;           else               mxi[i] = mxi[i + 1];           mx[i] = max(mx[i + 1], r[i]);       }       for (int i = 1; i < n - 1; ++i) {           if (mn[i - 1] < r[i] && r[i] < mx[i + 1]) {               cout <<  -1 n ;               return 0;           }       }       //cerr <<  here n ;       vector<vector<int>> graph(n);       vector<int> st;       for (int i = 0; i < n; ++i) {           if (i < n - 1 && r[i] < mx[i + 1]) {               st.push_back(i);               continue;           }           if (i > 0 && r[i] > mn[i - 1]) {               int useful = st.back();               while (!st.empty()){                   if (r[st.back()] < r[i]) {                       graph[L[st.back()]].push_back(L[i]);                       graph[L[i]].push_back(L[st.back()]);                       st.pop_back();                       continue;                   }                   break;               }               st.push_back(useful);              }       }       vector<int> used(n);       int ans = 0;       for (int i = 0; i < n; ++i) {           used[i] = -1;       }       for (int i = 0; i < n; ++i) {           if (used[i] == -1) {               price = 0;               all = 0;               used[i] = 0;               dfs(i, used, col, graph);               ans += min(price, all - price);           }       }       cout << ans <<   n ;   }   //*/


#include <bits/stdc++.h>   using namespace std;   template <typename T>   void chkmax(T &x, T y) {     x = x > y ? x : y;   }   template <typename T>   void chkmin(T &x, T y) {     x = x > y ? y : x;   }   const int INF = (1ll << 30);   template <typename T>   void read(T &x) {     x = 0;     bool f = 1;     char ch;     do {       ch = getchar();       if (ch ==  - ) f = 0;     } while (ch >  9  || ch <  0 );     do {       x = x * 10 + ch -  0 ;       ch = getchar();     } while (ch >=  0  && ch <=  9 );     x = f ? x : -x;   }   template <typename T>   void write(T x) {     if (x < 0) x = ~x + 1, putchar( - );     if (x > 9) write(x / 10);     putchar(x % 10 +  0 );   }   const int N = 5000 + 5;   const int M = 1e5 + 7;   map<int, int> mp;   int n, m, ans, cnt, a[N], b[N], g[N], f[N], prime[M];   bool vis[M];   inline int gcd(int a, int b) {     if (a < b) swap(a, b);     return b == 0 ? a : gcd(b, a % b);   }   inline void init(int NN) {     for (int i = 2; i <= NN; i++) {       if (!vis[i]) {         prime[++cnt] = i;       }       for (int j = 1; j <= cnt && prime[j] * i <= NN; j++) {         vis[prime[j] * i] = true;         if (i % prime[j] == 0) break;       }     }   }   inline int divide(int x) {     if (mp.count(x)) return mp[x];     int p = 0, tp = x;     for (int i = 1; i <= m && b[i] <= x; i++) {       while (x % b[i] == 0) x /= b[i], p--;     }     for (int i = 1; i <= cnt && 1ll * prime[i] * prime[i] <= x; i++) {       while (x % prime[i] == 0) x /= prime[i], p++;     }     if (x != 1) p++;     return mp[tp] = p;   }   int main() {     read(n);     read(m);     for (int i = 1; i <= n; i++) read(a[i]), g[i] = gcd(a[i], g[i - 1]);     for (int i = 1; i <= m; i++) read(b[i]);     init(1e5);     ans = 0;     for (int i = 1; i <= n; i++) ans += divide(a[i]);     for (int i = 1; i <= n; i++) f[i] = ans - i * divide(g[i]);     for (int i = n; i >= 1; i--) {       ans = max(ans, f[i]);       for (int j = i - 1; j >= 1; j--) {         int gg = g[j] / g[i];         f[j] = max(f[j], f[i] - j * divide(gg));         chkmax(ans, f[j]);       }     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ull = unsigned long long;   using pii = pair<int, int>;   template <class T>   inline void read(T &x) {     x = 0;     char c = getchar();     bool flag = 0;     while (!isdigit(c)) {       if (c ==  - ) flag = 1;       c = getchar();     }     while (isdigit(c)) {       x = (x << 3) + (x << 1) + c -  0 ;       c = getchar();     }     x = flag ? -x : x;   }   template <class T>   inline void write(T x) {     if (x < 0) {       putchar( - );       x = -x;     }     if (x >= 10) write(x / 10);     putchar(x % 10 +  0 );   }   const int maxn = 5e5 + 10;   const int mod = 998244353;   struct Edge {     int next, ed;   };   Edge e[maxn * 2];   int head[maxn], cnt;   void addEdge(int st, int ed) {     e[cnt].ed = ed;     e[cnt].next = head[st];     head[st] = cnt++;   }   bool vis[maxn];   int sum[maxn], d[maxn];   int ansl[maxn], ansr[maxn];   void get(int k) {     sum[k] = 1;     for (int i = head[k]; i != -1; i = e[i].next) {       int ed = e[i].ed;       if (sum[ed] == 0) {         get(ed);         sum[k] += sum[ed];         ++d[k];       }     }   }   void dfs(int k, int l, int r) {     vis[k] = 1;     ansr[k] = l + d[k];     int c = l, temp = r;     for (int i = head[k]; i != -1; i = e[i].next) {       int ed = e[i].ed;       if (!vis[ed]) {         ansl[ed] = c;         ++c;         dfs(ed, temp - sum[ed] * 2 + 2, temp);         temp -= sum[ed] * 2 - 1;       }     }   }   int main() {     int n;     read(n);     memset(head, -1, sizeof(head));     for (int i = 1; i < n; ++i) {       int st, ed;       scanf( %d %d , &st, &ed);       addEdge(st, ed);       addEdge(ed, st);     }     get(1);     ansl[1] = 1;     dfs(1, 2, n * 2);     for (int i = 1; i <= n; ++i) {       printf( %d %d n , ansl[i], ansr[i]);     }   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9 + 7;   const long long LL_INF = 1ll * INF * INF;   const int MAX_N = 1000000 + 7;   template <typename T>   inline void addmod(T& a, const long long& b, const int& MOD = INF) {     a = (a + b) % MOD;     if (a < 0) a += MOD;   }   const int mod = INF;   long long f[MAX_N];   int n;   vector<int> adj[MAX_N];   vector<int> rev[MAX_N];   long long gcd(long long a, long long b) { return a == 0 ? b : gcd(b % a, a); }   vector<int> divisors[MAX_N];   void init() {     for (int i = 1; i < MAX_N; ++i)       if (!rev[i].empty()) {         for (int j = i; j < MAX_N; j += i) divisors[j].push_back(i);       }   }   class FenWickTree {    public:     void update(int x, int val) {       for (int t = x; t < MAX_N; t += t & -t) {         tree[t] += val;       }     }     int get(int x) {       int res = 0;       for (int t = x; t > 0; t -= t & -t) {         res += tree[t];       }       return res;     }       private:     static const int MAX_N = ::MAX_N + 7;     int tree[MAX_N];   } fenwickTree;   int level[MAX_N];   int low[MAX_N], high[MAX_N];   void dfs(int u) {     static int cnt = 0;     low[u] = ++cnt;     for (int v : adj[u]) {       level[v] = level[u] + 1;       dfs(v);       f[u] += f[v];     }     high[u] = cnt;   }   bool check(int d) {     if (d == 1) return true;     if (f[0] % d) return false;     long long sum = f[0] / d;     int cnt = 0;     for (int c : divisors[d])       if (!rev[c].empty()) {         cnt += rev[c].size();       }     return d == cnt;   }   int ans[MAX_N];   void solve() {     cin >> n;     for (int i = (0), _b = (n); i < _b; ++i) cin >> f[i];     for (int i = (1), _b = (n); i < _b; ++i) {       int p;       cin >> p;       adj[p - 1].push_back(i);     }     dfs(0);     for (int i = 0; i < n; ++i) {       long long d = gcd(f[i], f[0]);       if (f[0] / d < MAX_N) {         rev[f[0] / d].push_back(i);       }     }     init();     for (int i = n; i > 0; --i)       if (check(i)) {         ans[i] += 1;         if (ans[i] >= mod) ans[i] -= mod;         for (int c : divisors[i])           if (c != i) {             ans[c] += ans[i];             if (ans[c] >= mod) ans[c] -= mod;           }       }     cout << ans[1] <<   n ;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     const bool multiple_test = false;     int test = 1;     if (multiple_test) cin >> test;     for (int i = 0; i < test; ++i) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   struct lichao {     int l, r, A;     long long B;     lichao() : l(0), r(0), A(0), B(0x7fffffffffffffffLL) {}   };   const int SZ = 1 << 17;   vector<int> adj[100000];   vector<lichao> tree(2 * SZ);   int node_cnt, num[100000], A[100000], B[100000];   long long ans[100000];   int get_sign(long long a) { return a < 0 ? -1 : a > 0; }   void add_tree2(int A, long long B, int p, int s = -SZ, int e = SZ - 1) {     int m = (s + e) >> 1, &pA = tree[p].A;     long long &pB = tree[p].B;     long long ys = 1LL * A * s + B, ym = 1LL * A * m + B, ye = 1LL * A * e + B,               pys = 1LL * pA * s + pB, pym = 1LL * pA * m + pB,               pye = 1LL * pA * e + pB;     if (ym < pym) {       swap(A, pA);       swap(B, pB);       swap(ys, pys);       swap(ym, pym);       swap(ye, pye);     }     if (ys >= pys && ye >= pye) return;     if (get_sign(ys - pys) * get_sign(ym - pym) < 0 || ym == pym && ys < pys) {       if (tree[p].l == 0) tree[p].l = tree.size(), tree.push_back(lichao());       add_tree2(A, B, tree[p].l, s, m);     } else {       if (tree[p].r == 0) tree[p].r = tree.size(), tree.push_back(lichao());       add_tree2(A, B, tree[p].r, m + 1, e);     }   }   void add_tree(int n, int A, long long B) {     for (add_tree2(A, B, n += SZ); n >>= 1;) add_tree2(A, B, n);   }   long long get_min2(int x, int p, int s = -SZ, int e = SZ - 1) {     int m = (s + e) >> 1;     if (p == 0 || s == e) return 1LL * tree[p].A * x + tree[p].B;     return min(         1LL * tree[p].A * x + tree[p].B,         x <= m ? get_min2(x, tree[p].l, s, m) : get_min2(x, tree[p].r, m + 1, e));   }   long long get_min(int s, int e, int x) {     long long ret = 0x7fffffffffffffffLL;     for (s |= SZ, e |= SZ; s <= e; s >>= 1, e >>= 1) {       if (s & 1) ret = min(ret, get_min2(x, s++));       if (~e & 1) ret = min(ret, get_min2(x, e--));     }     return ret;   }   void dfs(int c) {     bool leaf = true;     num[c] = ++node_cnt;     for (auto n : adj[c])       if (num[n] == 0) {         leaf = false;         dfs(n);       }     ans[c] = (leaf ? 0 : get_min(num[c], node_cnt, A[c]));     add_tree(num[c], B[c], ans[c]);   }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ((void)0);     ((void)0);     ((void)0);     int N;     cin >> N;     for (int i = 0; i < N; i++) cin >> A[i];     for (int i = 0; i < N; i++) cin >> B[i];     for (int i = 1; i < N; i++) {       int a, b;       cin >> a >> b;       adj[--a].push_back(--b);       adj[b].push_back(a);     }     dfs(0);     for (int i = 0; i < N; i++) cout << ans[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 1e5 + 9, lg = 30;   int a[inf * 3], n, table[lg][inf << 2];   int ans[inf], nxt[inf], sml[inf << 2];   void build() {     int i, j;     for (i = 1; i <= n + n; i++) table[0][i] = a[i];     for (j = 1; j < lg - 1; j++)       for (i = 1; i + (1 << (j - 1)) - 1 <= n + n; i++)         table[j][i] = min(table[j - 1][i], table[j - 1][i + (1 << (j - 1))]);   }   int query(int x, int y) {     int len, k;     len = y - x + 1;     k = log2(len);     return min(table[k][x], table[k][y + 1 - (1 << k)]);   }   int main() {     cin >> n;     for (int i = 1; i <= n; i++)       cin >> a[i], a[i + n] = a[i], a[i + n + n] = a[i];     int mx = 0;     for (int i = n;; i++) {       if (mx > a[i]) break;       if (i == n + n + n) {         ans[n] = -1;         break;       }       ans[n]++;       mx = max(mx, (a[i] % 2 == 0 ? a[i] / 2 : a[i] / 2 + 1));     }     ans[n + n] = ans[n];     stack<int> st;     for (int i = n + n; i >= 1; i--) {       while (!st.empty() && a[st.top()] <= a[i]) st.pop();       if (st.empty())         nxt[i] = -1;       else         nxt[i] = st.top();       st.push(i);     }     build();     for (int i = 1; i <= n; i++) {       sml[i] = 1e9;       int val = (a[i] % 2 == 0 ? a[i] / 2 : a[i] / 2 + 1);       int l = i, r = min(n + n + 1, n + i + 1);       while (r - l > 1) {         int mid = (l + r) / 2;         if (query(i, mid) < val)           r = mid;         else           l = mid;       }       if (a[r] < val) sml[i] = r;     }     vector<pair<int, int> > v;     for (int i = 1; i <= n; i++) {       v.push_back(make_pair(a[i], i));     }     sort(v.begin(), v.end());     reverse(v.begin(), v.end());     for (auto o : v) {       int i = o.second;       if (sml[i] == 1e9) {         if (nxt[i] == -1)           ans[i] = -1;         else           ans[i] = (ans[nxt[i]] != -1 ? ans[nxt[i]] + nxt[i] - i : -1);       } else {         if (nxt[i] == -1)           ans[i] = sml[i] - i;         else {           if (sml[i] < nxt[i])             ans[i] = sml[i] - i;           else             ans[i] = (ans[nxt[i]] != -1 ? ans[nxt[i]] + nxt[i] - i : -1);         }       }       ans[i + n] = ans[i];     }     for (int i = 1; i <= n; i++) cout << ans[i] <<    ;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   T abs(T a) {     return a < 0 ? -a : a;   }   template <typename T>   T sqr(T a) {     return a * a;   }   const int INF = (int)1e9;   const long double EPS = 1e-9;   const long double PI = 3.1415926535897932384626433832795;   int main() {     int n;     cin >> n;     cout << n <<    ;     for (int i = 1; i < n; i++) cout << i <<    ;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int p;   int pm(int x, int e) {     int res = 1;     int tmp = x;     while (e > 0) {       if (e & 1) res = ((long long)res * tmp) % p;       tmp = ((long long)tmp * tmp) % p;       e >>= 1;     }     return res;   }   bool prim() {     for (int i = 2; i * i <= p; ++i)       if (p % i == 0) return false;     return true;   }   vector<int> d;   void roz() {     int M = p - 1;     for (int i = 2; i * i < p; ++i) {       if (M % i == 0) d.push_back(M / i);     }   }   bool ok(int x) {     for (int i = 0; i < d.size(); ++i)       if (pm(x, d[i]) == 1) return false;     return true;   }   int gen() {     roz();     for (int i = 2; i < p; ++i) {       if (ok(i)) return i;     }   }   int main() {     scanf( %d , &p);     if (p == 1) {       printf( YES n1 n );     } else if (p == 2) {       printf( YES n1 n2 n );     } else if (p == 4) {       printf( YES n1 n3 n2 n4 n );     } else if (!prim()) {       printf( NO n );     } else {       int g = gen();       printf( YES n );       for (int i = 0; i < (p - 1) / 2; ++i) {         printf( %d n%d n , pm(g, p - 1 - 2 * i), pm(g, 1 + 2 * i));       }       printf( %d n , p);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100010;   long long gcd(long long x, long long y) {     if (y == 0) return x;     return gcd(y, x % y);   }   int main() {     long long n;     cin >> n;     string s;     cin >> s;     vector<int> v(26);     for (long long i = 0; i < n; i++) {       v[s[i] -  a ]++;     }     for (long long i = 0; i < 26; i++) {       while (v[i]--) {         char c = i +  a ;         cout << c;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 5;   int k, n, m;   long long a[MAXN], g[MAXN], ii[MAXN], ty[MAXN];   vector<pair<long long, long long> > V[MAXN];   vector<pair<long long, pair<long long, long long> > > P;   bool Compare(pair<long long, pair<long long, long long> > A,                pair<long long, pair<long long, long long> > B) {     assert(A.second.first >= A.second.second);     assert(B.second.first >= B.second.second);     A.second.first -= A.second.second;     B.second.first -= B.second.second;     return A.second.first * B.second.second > A.second.second * B.second.first;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> k >> n >> m;     for (int i = 1; i <= k; i++) {       cin >> a[i];     }     for (int i = 1; i <= n; i++) {       int t, id, b;       cin >> t >> id >> b;       ty[i] = t;       if (t == 1) {         if (g[id] < b) {           g[id] = b;           ii[id] = i;         }       } else if (t == 2) {         V[id].emplace_back(b, i);       } else {         P.emplace_back(i, make_pair(b, 1));       }     }     for (int i = 1; i <= k; i++) {       if (a[i] < g[i]) {         V[i].emplace_back(g[i] - a[i], ii[i]);       }       sort(V[i].begin(), V[i].end(), greater<pair<long long, long long> >());       for (auto x : V[i]) {         P.emplace_back(x.second, make_pair(a[i] + x.first, a[i]));         a[i] += x.first;       }     }     sort(P.begin(), P.end(), Compare);     vector<pair<long long, long long> > L;     for (int i = 0; i < min(m, (int)P.size()); i++) {       L.emplace_back(ty[P[i].first], P[i].first);     }     sort(L.begin(), L.end());     cout << L.size() <<   n ;     for (auto x : L) {       cout << x.second <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<long long, long long> > g[100005];   long long n, ans, ans2;   void dfs(long long u, long long p, long long cur) {     ans2 = max(ans2, cur);     for (auto e : g[u])       if (e.first != p) dfs(e.first, u, cur + e.second);   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> n;     for (long long i = 1; i <= n - 1; i++) {       long long a, b, w;       cin >> a >> b >> w;       g[a].push_back({b, w});       g[b].push_back({a, w});       ans += w * 2;     }     dfs(1, 0, 0);     cout << ans - ans2;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline void rd(T &x) {     char ch;     x = 0;     bool fl = false;     while (!isdigit(ch = getchar())) (ch ==  - ) && (fl = true);     for (x = (ch ^  0 ); isdigit(ch = getchar()); x = x * 10 + (ch ^  0 ))       ;     (fl == true) && (x = -x);   }   template <class T>   inline void output(T x) {     if (x / 10) output(x / 10);     putchar(x % 10 +  0 );   }   template <class T>   inline void ot(T x) {     if (x < 0) putchar( - ), x = -x;     output(x);     putchar(   );   }   template <class T>   inline void prt(T a[], int st, int nd) {     for (register int i = st; i <= nd; ++i) ot(a[i]);     putchar(  n );   }   namespace Modulo {   const int mod = 998244353;   int ad(int x, int y) { return (x + y) >= mod ? x + y - mod : x + y; }   void inc(int &x, int y) { x = ad(x, y); }   int mul(int x, int y) { return (long long)x * y % mod; }   void inc2(int &x, int y) { x = mul(x, y); }   int qm(int x, int y = mod - 2) {     int ret = 1;     while (y) {       if (y & 1) ret = mul(x, ret);       x = mul(x, x);       y >>= 1;     }     return ret;   }   }  // namespace Modulo   namespace Miracle {   const int N = 300000 + 5;   const int inf = 0x3f3f3f3f;   int n, m;   int du[N];   int b[N][2];   vector<int> to[N];   int go[N];   int q[N], l, r, df;   void topo() {     l = 1, r = 0;     df = 0;     for (register int i = 1; i <= n; ++i) {       if (du[i] == 0) q[++r] = i;     }     while (l <= r) {       int x = q[l];       ++df;       int sz = r - l + 1;       ++l;       if (sz == 1) {         go[x] += n - df;       } else if (sz == 2) {         int b = q[l];         int lp = 0;         for (const auto &y : to[b]) {           if (du[y] == 1) {             lp = 1;             break;           }         }         if (!lp) {           go[x] += n - df - 1;         } else {           go[x] = -inf;         }       } else {         go[x] = -inf;       }       for (const auto &y : to[x]) {         --du[y];         if (du[y] == 0) {           q[++r] = y;         }       }     }   }   int main() {     rd(n);     rd(m);     for (register int i = 1; i <= m; ++i) {       rd(b[i][0]);       rd(b[i][1]);       to[b[i][0]].push_back(b[i][1]);       ++du[b[i][1]];     }     topo();     for (register int i = 1; i <= n; ++i) to[i].clear(), du[i] = 0;     for (register int i = 1; i <= m; ++i) {       to[b[i][1]].push_back(b[i][0]);       ++du[b[i][0]];     }     topo();     int ans = 0;     for (register int i = 1; i <= n; ++i) {       if (go[i] >= n - 2) ++ans;     }     ot(ans);     return 0;   }   }  // namespace Miracle   signed main() {     Miracle::main();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int M = 2000 + 4, Inf = 1e9 + 10;   int n, m, k;   char f[M][M];   int main() {     ios::sync_with_stdio(false);     cin >> n >> m >> k;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) cin >> f[i][j];     for (int i = 0; i < m; i++) {       int ans = 0;       for (int y = 2; y < n; y += 2)         if (f[y][i] ==  U ) ans++;       for (int x = i, y = 0; x >= 0 && y < n; y++, x--)         if (f[y][x] ==  R ) ans++;       for (int x = i, y = 0; x < m && y < n; y++, x++)         if (f[y][x] ==  L ) ans++;       cout << ans <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   void read(T &n) {     n = 0;     T f = 1;     char c = getchar();     while (!isdigit(c) && c !=  - ) c = getchar();     if (c ==  - ) f = -1, c = getchar();     while (isdigit(c)) n = n * 10 + c -  0 , c = getchar();     n *= f;   }   template <typename T>   void write(T n) {     if (n < 0) putchar( - ), n = -n;     if (n > 9) write(n / 10);     putchar(n % 10 +  0 );   }   int n, m, vis[105];   long long f[105][105], p[105][105];   struct node {     long long a, b;     int id;     friend bool operator<(node a, node b) { return a.b < b.b; }   } v[105];   int main() {     int T;     cin >> T;     while (T--) {       read(n), read(m);       for (int i = 1; i <= n; i++) {         read(v[i].a), read(v[i].b);         v[i].id = i;       }       sort(v + 1, v + n + 1);       memset(f, -0x3f, sizeof(f));       memset(p, 0, sizeof(p));       memset(vis, 0, sizeof(vis));       f[0][0] = 0;       for (int i = 1; i <= n; i++) {         for (int j = 0; j <= min(i, m); j++) {           f[i][j] = f[i - 1][j] + v[i].b * (m - 1);           if (j && f[i - 1][j - 1] + v[i].a + v[i].b * (j - 1) > f[i][j]) {             f[i][j] = f[i - 1][j - 1] + v[i].a + v[i].b * (j - 1);             p[i][j] = 1;           }         }       }       int t = m;       for (int i = n; i >= 1; i--) {         if (p[i][t]) {           vis[i] = 1;           t--;         }       }       printf( %d n , m + (n - m) * 2);       int cnt = 0, x;       for (int i = 1; i <= n; i++) {         if (!vis[i]) continue;         cnt++;         if (cnt == m)           x = v[i].id;         else           printf( %d  , v[i].id);       }       for (int i = 1; i <= n; i++) {         if (!vis[i]) printf( %d %d  , v[i].id, -v[i].id);       }       printf( %d n , x);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100050;   const int inf = 0x3f3f3f3f;   struct Edge {     int v, w;   };   vector<Edge> G[maxn];   bool vis[maxn];   vector<int> st;   int dep[maxn], f[maxn][20];   long long dis[maxn];   void dfs(int x, int par, long long sum) {     f[x][0] = par;     dep[x] = dep[par] + 1;     dis[x] = sum;     vis[x] = true;     for (int i = 1; f[x][i - 1]; ++i) f[x][i] = f[f[x][i - 1]][i - 1];     for (auto e : G[x]) {       int v = e.v;       if (v != par && vis[v]) {         st.push_back(v), st.push_back(x);       }       if (v != par && !vis[v]) dfs(v, x, sum + e.w);     }   }   int lca(int x, int y) {     if (dep[x] < dep[y]) swap(x, y);     int h = dep[x] - dep[y];     for (int i = 0; h; ++i) {       if (h & (1 << i)) {         x = f[x][i];         h ^= (1 << i);       }     }     if (x == y) return x;     for (int i = 17; i >= 0; --i) {       if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];     }     return f[x][0];   }   long long Dis(int x, int y) {     int z = lca(x, y);     return dis[x] + dis[y] - dis[z] * 2;   }   struct Dijkstra {     long long d[maxn];     struct node {       int x;       long long d;       node(int x = 0, long long d = 0) : x(x), d(d) {}       bool operator<(const node& t) const { return d > t.d; }     };     void run(int s) {       memset(d, 0x3f, sizeof d);       memset(vis, false, sizeof vis);       priority_queue<node> q;       q.push(node(s, 0)), d[s] = 0;       while (!q.empty()) {         node u = q.top();         q.pop();         if (vis[u.x]) continue;         vis[u.x] = true;         for (auto e : G[u.x]) {           int v = e.v;           if (!vis[v] && d[v] > u.d + e.w) {             d[v] = u.d + e.w;             q.push(node(v, d[v]));           }         }       }     }   } dij[50];   int main() {     int n, m;     scanf( %d%d , &n, &m);     for (int i = 0; i < m; ++i) {       int x, y, w;       scanf( %d%d%d , &x, &y, &w);       G[x].push_back({y, w});       G[y].push_back({x, w});     }     dfs(1, 0, 0);     sort(st.begin(), st.end());     int sz = unique(st.begin(), st.end()) - st.begin();     while (st.size() > sz) st.pop_back();     for (int i = 0; i < sz; ++i) {       dij[i].run(st[i]);     }     int q;     scanf( %d , &q);     while (q--) {       int u, v;       scanf( %d%d , &u, &v);       long long ans = Dis(u, v);       for (int i = 0; i < sz; ++i) ans = min(ans, dij[i].d[u] + dij[i].d[v]);       printf( %lld n , ans);     }     return 0;   }