#include <bits/stdc++.h>   using namespace std;   vector<long long> primes;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     int n, q;     cin >> n >> q;     vector<pair<int, int> > ans;     int tmp(-1);     vector<bool> state;     for (int i = 0; i < n; ++i) {       int x;       cin >> x;       if (tmp <= x) {         if (!i)           tmp = x;         else {           ans.push_back(make_pair(tmp, x));           tmp = x;           state.push_back(true);         }       } else {         ans.push_back(make_pair(x, tmp));         state.push_back(false);       }     }     int nw = n - 1;     while (q--) {       long long y;       cin >> y;       if (!y)         cout <<   n ;       else {         if (y < n) {           if (state[y - 1])             cout << ans[y - 1].first <<     << ans[y - 1].second <<   n ;           else             cout << ans[y - 1].second <<     << ans[y - 1].first <<   n ;         } else {           y -= n;           if (!y)             cout << tmp <<     << ans[0].first <<   n ;           else             cout << tmp <<     << ans[y % nw].first <<   n ;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int powP[2001] = {1};   int invPowP[2001] = {1};   int hashW[2001] = {0};   vector<pair<int, int> > hashTable[100003];   void findPos(string& w, string& subW, vector<int>& posSubW) {     unsigned int j = 0, k = subW.size();     for (unsigned int i = 0; (i + k) <= w.size(); i++) {       while (j < subW.size() && w[i] == subW[j]) {         i++;         j++;       }       if (j == subW.size()) posSubW.push_back(i - j + 1);       if (j > 0) {         i -= j;         j = 0;       }     }   }   void fillHash(string& w) {     for (unsigned int i = 0; i < w.size(); i++)       hashW[i + 1] =           (hashW[i] + (((long long)w[i] - 96) * (long long)powP[i]) % 100003) %           100003;   }   int myMod(int a, int m) {     if (a >= 0) return a % m;     return m * ((-a / m) + 1) + a;   }   int getHash(int i, int j) {     int hashVal = myMod(hashW[j] - hashW[i - 1], 100003),         inverse = invPowP[i - 1];     return ((long long)hashVal * (long long)inverse) % (long long)100003;     ;   }   inline int modPow(long long a, int b, int m) {     long long ans = 1;     while (b > 0) {       if (b & 1) ans = (ans * a) % m;       a = (a * a) % m;       b >>= 1;     }     return ans;   }   int getInverse(int a, int m) { return modPow(a, m - 2, m); }   bool cmpHash(unsigned int wSize, pair<int, int>& a, pair<int, int> b) {     unsigned int i = 0, j = wSize, mid;     while (i < j) {       mid = (i + j) / 2;       if (getHash(a.first, a.first + mid) != getHash(b.first, b.first + mid))         return false;       else         i = mid + 1;     }     return true;   }   int main() {     vector<int> posS, posE;     string w, s, e;     for (unsigned int i = 1; i < 2001; i++)       powP[i] = (((long long)powP[i - 1] * (long long)37)) % (long long)100003;     for (unsigned int i = 1; i < 2001; i++)       invPowP[i] = getInverse(powP[i], 100003);     cin >> w >> s >> e;     findPos(w, s, posS);     findPos(w, e, posE);     fillHash(w);     int cont = 0, hashVal;     bool found = false;     for (unsigned int i = 0; i < posS.size(); i++)       for (unsigned int j = 0; j < posE.size(); j++) {         if (posS[i] <= posE[j] &&             (posS[i] + s.size() - 1) <= (posE[j] + e.size() - 1)) {           found = 0;           hashVal = getHash(posS[i], posE[j] + e.size() - 1);           for (unsigned int k = 0; k < hashTable[hashVal].size(); k++) {             if ((hashTable[hashVal][k].second - hashTable[hashVal][k].first) ==                 (posE[j] - posS[i])) {               if (cmpHash(1 + posE[j] - posS[i], hashTable[hashVal][k],                           make_pair(posS[i], posE[j]))) {                 found = true;                 break;               }             }           }           if (!found) {             cont++;             hashTable[hashVal].push_back(make_pair(posS[i], posE[j]));           }         }       }     cout << cont << endl;     return 0;   }


#include <bits/stdc++.h>   const long long mod = 1e8;   using namespace std;   bool in(int x, int l, int r) { return l <= x && x <= r; }   void solve() {     long long l, r;     cin >> l >> r;     vector<long long> v;     long long p = -1;     for (int i = 61; i >= 0; i--) {       unsigned long long mask = 1ull << i;       if (mask & r) {         v.push_back(i);         if (mask & l) {           continue;         } else {           p = i;           break;         }       }     }     long long ans = 0;     if (v.size() == 0) {       cout << l <<   n ;       return;     }     int q = 0;     for (int i = p - 1; i >= 0; i--) {       unsigned long long mak = 1ull << i;       if (mak & r) q++;     }     if (q == p) {       cout << r <<   n ;       return;     }     for (int i = 0; i < v.size(); i++) {       if (v[i] != p) ans += pow(2, v[i]);     }     for (int i = p - 1; i >= 0; i--) ans += pow(2, i);     cout << ans <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   void read(T &x) {     x = 0;     bool f = 0;     char c = getchar();     for (; !isdigit(c); c = getchar())       if (c ==  - ) f = 1;     for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);     if (f) x = -x;   }   template <typename T>   inline void Mx(T &x, T y) {     x < y && (x = y);   }   template <typename T>   inline void Mn(T &x, T y) {     x > y && (x = y);   }   long long T, n, l, r;   int main() {     read(T);     while (T--) {       read(n), read(l), read(r);       if (n == 2) {       }       long long sum = 0, pre = 0;       for (int i = 1; i < n; i++) {         sum += (n - i) * 2;         if (l < sum) {           long long t = l - pre;           long long x = (t & 1) ? i : (t / 2) + i, y = (t / 2) + i;           while (l < sum && l <= r) {             printf( %lld  , x);             x == i ? y++, x = y : x = i;             l++;           }         }         if (l == sum && l <= r) printf( %lld  , n), l++;         pre = sum;         if (l > r) break;       }       if (l <= r) printf( 1 );       puts(  );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const int maxn = 100;   int b, d, lenA, lenC;   string a, c;   int dp[maxn + 10], nex[maxn + 10];   void cope(int x) {     dp[x] = 0;     nex[x] = x;     int use = 0;     while (use < lenA) {       if (a[use] == c[nex[x]]) {         if (++nex[x] == lenC) {           dp[x]++;           nex[x] = 0;         }       }       use++;     }   }   int main() {     std::ios::sync_with_stdio(false);     while (cin >> b >> d) {       cin >> a >> c;       lenA = a.length(), lenC = c.length();       for (int(i) = 0; (i) < (lenC); (i)++) cope(i);       int x = 0, num = 0;       for (int(i) = 0; (i) < (b); (i)++) {         num += dp[x];         x = nex[x];       }       num /= d;       cout << num << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n, m, i, j, k, ct;     bool swapped = false;     cin >> n >> m;     if (n < m) {       swap(n, m);       swapped = true;     }     vector<vector<int>> ans(n, vector<int>(m, -1));     ct = 0;     if (m == 1) {       for (i = 1; i < n; i += 2) ans[i][0] = ++ct;       for (i = 0; i < n; i += 2) ans[i][0] = ++ct;     } else {       for (i = 0; i < n; i++) {         for (j = i % 2; j < m; j += 2) ans[i][j] = ++ct;       }       for (i = 0; i < n; i++) {         for (j = 1 - (i % 2); j < m; j += 2) ans[i][j] = ++ct;       }     }     bool solved = true;     for (i = 0; i < n; i++) {       for (j = 0; j < m; j++) {         if ((i < n - 1) && (abs(ans[i][j] - ans[i + 1][j]) == 1)) solved = false;         if ((j < m - 1) && (abs(ans[i][j] - ans[i][j + 1]) == 1)) solved = false;       }     }     if (!solved)       cout <<  -1 n ;     else {       if (!swapped) {         for (i = 0; i < n; i++) {           for (j = 0; j < m; j++) cout << ans[i][j] <<    ;           cout <<   n ;         }       } else {         for (j = 0; j < m; j++) {           for (i = 0; i < n; i++) cout << ans[i][j] <<    ;           cout <<   n ;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t = 1;     while (t--) {       unsigned long long int a, b, c, d;       cin >> a >> b >> c >> d;       unordered_map<unsigned long long int, unsigned long long int> mp, mp1;       for (int i = 0; i <= 100000; i++) {         ++mp[b + (i * a)];         ++mp1[d + (i * c)];       }       for (int i = 0; i <= 100000; i++) {         if (mp[i] && mp1[i]) {           cout << i;           return 0;         }       }       cout << -1;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, x, y;     cin >> n >> x >> y;     int people = n - x;     int wizards = x;     people *= 100;     wizards *= 100;     int needed = max(0, n * y - wizards);     cout << (needed + 99) / 100 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[400500], b[400500], n, tot;   long long read() {     long long x = 0, f = 1;     char ch = getchar();     while (!isdigit(ch)) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (isdigit(ch)) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * f;   }   int main() {     n = read();     tot = 0;     for (int i = 1; i <= n; i++) {       int x = read();       if (x) a[++tot] = x;     }     tot = 0;     for (int i = 1; i <= n; i++) {       int x = read();       if (x) b[++tot] = x;     }     for (int i = n; i <= 2 * n - 2; i++) b[i] = b[i - (n - 1)];     n--;     int loc = 0;     for (int i = 1; i <= n; i++)       if (a[1] == b[i]) loc = i;     int flag = 0;     for (int i = loc, j = 1; j <= n; i++, j++)       if (b[i] != a[j]) {         flag = 1;         break;       }     if (!flag)       puts( YES );     else       puts( NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<string> v;     vector<int> vn;     for (int i = 0; i < n; i++) {       string a;       cin >> a;       bool cn = false;       int y = 0;       for (int j = 0; j < v.size(); j++) {         if (a == v[j]) {           y = j;           cn = true;           break;         }       }       if (cn) {         vn[y]++;       } else {         v.push_back(a);         vn.push_back(1);       }     }     int rpta = vn[0];     for (int i = 1; i < vn.size(); i++) {       rpta = max(rpta, vn[i]);     }     cout << rpta <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool vowel(char c) {     return (c ==  a  || c ==  e  || c ==  o  || c ==  i  || c ==  u );   }   int cvowel(string& s) {     int n = 0;     for (char& c : s) {       n += vowel(c);     }     return n;   }   int getlast(string& s) {     for (int i = s.size() - 1;; i--) {       if (vowel(s[i])) {         return s[i];       }     }   }   const int KEK = 1000001;   vector<string> ss[KEK];   vector<pair<char, int>> vv[KEK];   int main() {     ios::sync_with_stdio(0);     int n;     cin >> n;     for (int i = 0; i < n; i++) {       string s;       cin >> s;       int cnt = cvowel(s);       ss[cnt].push_back(s);       vv[cnt].push_back(make_pair(getlast(s), ss[cnt].size() - 1));     }     vector<pair<string, string>> q1, q2;     for (int i = 1; i < KEK; i++) {       sort(vv[i].begin(), vv[i].end());       int m = vv[i].size();       vector<bool> inpair(m, false);       for (int j = 1; j < m; j++) {         if (vv[i][j - 1].first == vv[i][j].first) {           inpair[j - 1] = inpair[j] = true;           q2.push_back(               make_pair(ss[i][vv[i][j - 1].second], ss[i][vv[i][j].second]));           j++;         }       }       int last = -1;       for (int j = 0; j < m; j++) {         if (inpair[j] == false) {           if (last == -1) {             last = j;           } else {             q1.push_back(                 make_pair(ss[i][vv[i][last].second], ss[i][vv[i][j].second]));             last = -1;           }         }       }     }     while (q2.size() > q1.size()) {       q1.push_back(q2.back());       q2.pop_back();     }     while (q1.size() > q2.size()) {       q1.pop_back();     }     cout << q1.size() << endl;     for (int i = 0; i < q1.size(); i++) {       cout << q1[i].first <<     << q2[i].first <<   n ;       cout << q1[i].second <<     << q2[i].second <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int n, c, a[2], r = 0;   int main() {     cin >> n >> c >> a[0];     for (int i = 1; i < n; i++)       cin >> a[i % 2], r = max(r, a[i % 2 ^ 1] - a[i % 2] - c);     cout << r;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<int> tab(n);     for (int i = 0; i < n; i++) cin >> tab[i];     for (int i = 0; i < n; i++) {       if (tab[i] % 2 == 0)         continue;       else {         if (i == (n - 1) || tab[i + 1] == 0) {           cout <<  NO ;           return 0;         } else           tab[i + 1] -= 1;       }     }     cout <<  YES ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, a, b, x, cnt, ans;   deque<long long> V;   map<long long, long long> mp;   long long arr[1000001];   int main() {     cin >> n >> a >> b;     for (int i = 1; i <= n; i++) {       cin >> arr[i];     }     sort(arr + 1, arr + n + 1);     ans = (n - a) + 1;     x = arr[ans] - arr[b];     cout << x << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1000000000;   const int MAXN = 200001;   int c[MAXN * 3 + 10];   int n;   char s[MAXN + 10];   int sum[MAXN + 10];   int lowbit(int x) { return x & (-x); }   void update(int pos, int delta) {     while (pos <= n) {       c[pos] = min(c[pos], delta);       pos += lowbit(pos);     }   }   int query(int end) {     int res = INF;     while (end > 0) {       res = min(res, c[end]);       end -= lowbit(end);     }     return res;   }   bool isvowel(char x) {     if (x ==  a  || x ==  e  || x ==  i  || x ==  o  || x ==  u ) return true;     if (x ==  A  || x ==  E  || x ==  I  || x ==  O  || x ==  U ) return true;     return false;   }   int main() {     scanf( %s , s);     int len = strlen(s);     n = 3 * len + 1;     for (int i = 0; i <= n; i++) {       c[i] = INF;     }     memset(sum, 0, sizeof(sum));     for (int i = 0; i < len; i++) {       sum[i + 1] = sum[i] + (isvowel(s[i]) ? -1 : 2);     }     int ret = -1;     update(sum[0] + len + 1, 0);     for (int i = 1; i <= len; i++) {       int p = query(sum[i] + len + 1);       if (p != INF && i - p > ret) {         ret = i - p;       }       update(sum[i] + len + 1, i);     }     if (ret == -1) {       printf( No solution n );     } else {       int count = 0;       for (int i = 0; i <= len; i++) {         if (i + ret > len) break;         if (sum[i + ret] - sum[i] >= 0) {           count++;         }       }       printf( %d %d n , ret, count);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, n1, n2, limiter = 1, fight = 0, temp;     cin >> n >> n1;     queue<int> p1, p2;     for (int i = 1; i <= n1; i++) {       cin >> temp;       p1.push(temp);       limiter *= i;     }     cin >> n2;     for (int i = 1; i <= n2; i++) {       cin >> temp;       p2.push(temp);       limiter *= (n1 + i);     }     limiter *= (n + 1);     while (1) {       if (fight > limiter) {         cout << -1;         break;       } else if (p1.empty()) {         cout << fight <<     << 2;         break;       } else if (p2.empty()) {         cout << fight <<     << 1;         break;       } else {         if (p1.front() > p2.front()) {           p1.push(p2.front());           p1.push(p1.front());           p1.pop();           p2.pop();           fight++;         } else {           p2.push(p1.front());           p2.push(p2.front());           p2.pop();           p1.pop();           fight++;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int max_n = 200002, inf = 1111111111;   const int max_p = 500005;   int n, q, a[max_n], z[max_p], cnt[max_p], used[max_n];   long long ans;   int get_cnt(int x) {     int res = 0;     for (int i = 1; i * i <= x; ++i) {       if (x % i == 0) {         int d = i;         res += cnt[d] * z[d];         d = x / i;         if (i < d) {           res += cnt[d] * z[d];         }       }     }     return res;   }   void add(int x, int f) {     for (int i = 1; i * i <= x; ++i) {       if (x % i == 0) {         int d = i;         cnt[d] += f;         d = x / i;         if (i < d) {           cnt[d] += f;         }       }     }   }   int main() {     for (int i = 2; i < max_p; ++i) {       int x = i;       for (int j = 2; j * j <= x; ++j) {         if (x % j == 0) {           x /= j;           ++z[i];           if (x % j == 0) {             z[i] = 0;             x = 0;             break;           }         }       }       if (x == 0) {         continue;       }       if (x > 1) {         ++z[i];       }       if (z[i] % 2 == 1) {         z[i] = 1;       } else {         z[i] = -1;       }     }     scanf( %d%d , &n, &q);     for (int i = 0; i < n; ++i) {       scanf( %d , &a[i]);     }     int sz = 0;     while (q--) {       int x;       scanf( %d , &x);       --x;       int res;       if (used[x]) {         add(a[x], -1);         used[x] = 0;         --sz;         ans -= (sz - get_cnt(a[x]));       } else {         used[x] = 1;         ans += sz - get_cnt(a[x]);         add(a[x], 1);         ++sz;       }       printf( %I64d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void aent(long long int *a, long long int n) {     for (long long int i = 0; i < n; i++) cin >> a[i];   }   string arrtostr(long long int *a, long long int n) {     ostringstream os;     for (long long int i = 0; i < n; i++) os << a[i];     string str(os.str());     return str;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     long long int n;     cin >> n;     vector<long long int> v;     for (long long int i = 0; i < n; i++) {       long long int a;       cin >> a;       v.push_back(a);     }     long long int s = 0, e = n - 1;     string ss =   ;     long long int mi = LLONG_MIN;     while (s <= e) {       if (s == e) {         if (mi < v[s]) ss +=  L ;         break;       }       long long int st = v[s];       long long int ed = v[e];       if (st < mi && ed < mi) break;       if (st == ed) {         string s1 =  L , s2 =  R ;         int rs = s;         if (st < mi) break;         int chk = st;         while (s + 1 < e) {           if (v[s + 1] > chk) {             s1 +=  L ;             chk = v[s + 1];             s++;           } else             break;         }         chk = ed;         while (e - 1 > rs) {           if (v[e - 1] > chk) {             s2 +=  R ;             chk = v[e - 1];             e--;           } else             break;         }         if (s1.size() >= s2.size()) {           ss += s1;         } else           ss += s2;         cout << ss.size() <<   n ;         cout << ss;         return 0;       }       if (ed < st) {         if (ed > mi) {           ss +=  R ;           e--;           mi = ed;         } else {           if (st > mi) {             ss +=  L ;             s++;             mi = st;           } else             break;         }       } else {         if (st > mi) {           ss +=  L ;           s++;           mi = st;         } else {           if (ed > mi) {             ss +=  R ;             e--;             mi = ed;           } else             break;         }       }     }     cout << ss.size() <<   n ;     cout << ss;   }


#include <bits/stdc++.h>   using namespace std;   inline char read() {     static const int IN_LEN = 1000000;     static char buf[IN_LEN], *s, *t;     return (s == t               ? t = (s = buf) + fread(buf, 1, IN_LEN, stdin),             (s == t ? -1 : *s++) : *s++);   }   template <class T>   inline void read(T &x) {     static bool iosig;     static char c;     for (iosig = false, c = read(); !isdigit(c); c = read()) {       if (c ==  - ) iosig = true;       if (c == -1) return;     }     for (x = 0; isdigit(c); c = read()) x = ((x + (x << 2)) << 1) + (c ^  0 );     if (iosig) x = -x;   }   const int OUT_LEN = 10000000;   char obuf[OUT_LEN], *ooh = obuf;   inline void print(char c) {     if (ooh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), ooh = obuf;     *ooh++ = c;   }   template <class T>   inline void print(T x) {     static int buf[30], cnt;     if (x == 0)       print( 0 );     else {       if (x < 0) print( - ), x = -x;       for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;       while (cnt) print((char)buf[cnt--]);     }   }   inline void print(pair<int, int> x) {     print(x.first), print(   ), print(x.second), print(  n );   }   inline void flush() { fwrite(obuf, 1, ooh - obuf, stdout); }   const int N = 100005, M = 52;   const long long inf = 1e16;   int n, m, q, x[N], y[N], xx[N], yy[N], X[N], Y[N], fa[N], fa1[N], f1[N],       s[N << 2], f[N][M];   long long T[N], ga[N], g[N][M];   char opt[N], op[N];   vector<int> a[N], b[N], c[N];   void build(int l, int r, int t) {     s[t] = 0;     if (l == r) return;     int mid = (l + r) >> 1, k = t << 1;     build(l, mid, k), build(mid + 1, r, k | 1);   }   void change(int l, int r, int t, int L, int R, int x) {     if (L <= l && r <= R) return (void)(s[t] = x);     int mid = (l + r) >> 1, k = t << 1;     if (s[t]) s[k] = s[k | 1] = s[t], s[t] = 0;     if (L <= mid) change(l, mid, k, L, R, x);     if (R > mid) change(mid + 1, r, k | 1, L, R, x);   }   int query(int l, int r, int t, int x) {     if (l == r || s[t]) return s[t];     int mid = (l + r) >> 1, k = t << 1;     return (x <= mid ? query(l, mid, k, x) : query(mid + 1, r, k | 1, x));   }   inline pair<int, int> solve(pair<int, int> x, char o, long long t) {     int a = x.first, b = x.second;     if (o ==  L ) a = max(0ll, a - t);     if (o ==  R ) a = min((long long)m, a + t);     if (o ==  D ) b = max(0ll, b - t);     if (o ==  U ) b = min((long long)m, b + t);     return make_pair(a, b);   }   signed main() {     read(n), read(m);     for (int i = 1; i <= n; ++i) {       read(x[i]), read(y[i]), read(xx[i]), read(yy[i]);       if (x[i] == xx[i])         a[x[i]].push_back(i), op[i] = (y[i] > yy[i] ?  D  :  U );       else         b[x[i]].push_back(i), b[xx[i]].push_back(i),             op[i] = (x[i] > xx[i] ?  L  :  R );     }     read(q);     for (int i = 1; i <= q; ++i) {       read(X[i]), read(Y[i]);       while (isspace(opt[i] = read()))         ;       read(T[i]);     }     for (int i = 1; i <= q; ++i)       if (opt[i] ==  R ) c[X[i]].push_back(i);     build(0, m, 1);     for (int i = m; ~i; --i) {       for (int j : a[i]) change(0, m, 1, min(y[j], yy[j]), max(y[j], yy[j]), j);       for (int j : b[i])         if (x[j] > xx[j])           change(0, m, 1, y[j], y[j], j);         else           f[j][0] = query(0, m, 1, y[j]),           g[j][0] = (f1[j] = abs(xx[f[j][0]] - xx[j])) + abs(yy[f[j][0]] - y[j]);       for (int j : c[i]) {         fa[j] = query(0, m, 1, Y[j]),         ga[j] = abs(xx[fa[j]] - X[j]) + abs(yy[fa[j]] - Y[j]);         fa1[j] = (x[fa[j]] == xx[fa[j]] ? abs(xx[fa[j]] - X[j])                                         : max(xx[fa[j]] - X[j], 0));       }     }     for (int i = 0; i <= m; ++i) c[i].clear();     for (int i = 1; i <= q; ++i)       if (opt[i] ==  L ) c[X[i]].push_back(i);     build(0, m, 1);     for (int i = 0; i <= m; ++i) {       for (int j : a[i]) change(0, m, 1, min(y[j], yy[j]), max(y[j], yy[j]), j);       for (int j : b[i])         if (x[j] < xx[j])           change(0, m, 1, y[j], y[j], j);         else           f[j][0] = query(0, m, 1, y[j]),           g[j][0] = (f1[j] = abs(xx[j] - xx[f[j][0]])) + abs(yy[f[j][0]] - y[j]);       for (int j : c[i]) {         fa[j] = query(0, m, 1, Y[j]),         ga[j] = abs(X[j] - xx[fa[j]]) + abs(yy[fa[j]] - Y[j]);         fa1[j] = (x[fa[j]] == xx[fa[j]] ? abs(xx[fa[j]] - X[j])                                         : max(X[j] - xx[fa[j]], 0));       }     }     for (int i = 0; i <= m; ++i) c[i].clear(), a[i].clear(), b[i].clear();     for (int i = 1; i <= n; ++i)       if (y[i] == yy[i])         a[y[i]].push_back(i);       else         b[y[i]].push_back(i), b[yy[i]].push_back(i);     for (int i = 1; i <= q; ++i)       if (opt[i] ==  U ) c[Y[i]].push_back(i);     build(0, m, 1);     for (int i = m; ~i; --i) {       for (int j : a[i]) change(0, m, 1, min(x[j], xx[j]), max(x[j], xx[j]), j);       for (int j : b[i])         if (y[j] > yy[j])           change(0, m, 1, x[j], x[j], j);         else           f[j][0] = query(0, m, 1, x[j]),           g[j][0] = (f1[j] = abs(yy[f[j][0]] - yy[j])) + abs(xx[f[j][0]] - x[j]);       for (int j : c[i]) {         fa[j] = query(0, m, 1, X[j]),         ga[j] = (fa1[j] = abs(yy[fa[j]] - Y[j])) + abs(xx[fa[j]] - X[j]);         fa1[j] = (x[fa[j]] == xx[fa[j]] ? max(yy[fa[j]] - Y[j], 0)                                         : abs(yy[fa[j]] - Y[j]));       }     }     for (int i = 0; i <= m; ++i) c[i].clear();     for (int i = 1; i <= q; ++i)       if (opt[i] ==  D ) c[Y[i]].push_back(i);     build(0, m, 1);     for (int i = 0; i <= m; ++i) {       for (int j : a[i]) change(0, m, 1, min(x[j], xx[j]), max(x[j], xx[j]), j);       for (int j : b[i])         if (y[j] < yy[j])           change(0, m, 1, x[j], x[j], j);         else           f[j][0] = query(0, m, 1, x[j]),           g[j][0] = (f1[j] = abs(yy[j] - yy[f[j][0]])) + abs(xx[f[j][0]] - x[j]);       for (int j : c[i]) {         fa[j] = query(0, m, 1, X[j]),         ga[j] = (fa1[j] = abs(Y[j] - yy[fa[j]])) + abs(xx[fa[j]] - X[j]);         fa1[j] = (x[fa[j]] == xx[fa[j]] ? max(Y[j] - yy[fa[j]], 0)                                         : abs(yy[fa[j]] - Y[j]));       }     }     for (int j = 1; j <= 50; ++j)       for (int i = 1; i <= n; ++i)         f[i][j] = f[f[i][j - 1]][j - 1],         g[i][j] = min(inf, g[i][j - 1] + g[f[i][j - 1]][j - 1]);     for (int i = 1; i <= q; ++i) {       if (!fa[i] || T[i] < fa1[i])         print(solve(make_pair(X[i], Y[i]), opt[i], T[i]));       else if (T[i] < ga[i])         print(solve(solve(make_pair(X[i], Y[i]), opt[i], fa1[i]), op[fa[i]],                     T[i] - fa1[i]));       else {         T[i] -= ga[i];         int now = fa[i];         for (int j = 50; ~j; --j)           if (f[now][j] && g[now][j] <= T[i]) T[i] -= g[now][j], now = f[now][j];         if (!f[now][0] || T[i] < f1[now])           print(solve(make_pair(xx[now], yy[now]), op[now], T[i]));         else           print(solve(solve(make_pair(xx[now], yy[now]), op[now], f1[now]),                       op[f[now][0]], T[i] - f1[now]));       }     }     return flush(), 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n, i;       cin >> n;       int a[n];       map<long int, int> mp;       for (i = 0; i < n; i++) {         cin >> a[i];         mp[a[i]]++;       }       cout << mp.size() << endl;     }   }


#include <bits/stdc++.h>   const int N = (int)1e3 + 5;   int n;   int a[N];   int b[N];   int u[N];   int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }   int main() {     int ts;     scanf( %d , &ts);     while (ts--) {       for (int i = 0; i < N; ++i) {         u[i] = 0;       }       scanf( %d , &n);       for (int i = 0; i < n; ++i) {         scanf( %d , &a[i]);       }       int g = 0;       for (int p = 1; p <= n; ++p) {         int mx = -1;         int ind = -1;         for (int i = 0; i < n; ++i) {           if (!u[i]) {             if (mx < gcd(g, a[i])) {               mx = gcd(g, a[i]);               ind = i;             }           }         }         assert(ind != -1);         u[ind] = 1;         g = gcd(g, a[ind]);         b[p] = a[ind];       }       for (int i = 1; i <= n; ++i) {         printf( %d  , b[i]);       }       printf(  n );     }   }


#include <bits/stdc++.h>   using namespace std;      void ng() { cout << -1 <<   n ; }      void solve() {       int N; cin >> N;       map<int, int> M;       vector<int> A(N);       for (auto &x: A) {           cin >> x; M[x]++;       }       for (auto &[key, val]: M) {           if (val > (N + 1) / 2) return ng();       }       int l = 0;       vector<pair<int, int>> V;       for (int i = 0; i < N-1; i++) {           if (A[i] == A[i+1]) {               V.emplace_back(A[l], A[i]);               l = i+1;           }       }       V.emplace_back(A[l], A[N-1]);       M.clear();       int sum = V.size();       int sum2 = 0;       map<int, int> P;       for (auto &[c, d]: V) {           if (c == d) P[c]++, sum2++;           else M[c]++, M[d]++;       }       sum -= sum2;       for (auto &[key, val]: P) {           if (sum - M[key] + 1 + (sum2 - val) < val) {               int a = val - (sum - M[key] + 1 + (sum2 - val));               cout << V.size() - 1 + a <<   n ;               return;           }       }       cout << V.size() - 1 <<   n ;   }      int main() {       cin.tie(nullptr);       ios::sync_with_stdio(false);       int T; cin >> T;       while (T--) solve();       return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   int a[100];   int sol[1000000];   void prueba(int ind) {     sol[0] = ind;     priority_queue<pair<int, int> > q;     for (int i = 1; i <= m; i++) {       if (ind == i) {         if (a[i] > 1) q.push(pair<int, int>(a[i] - 1, i));       } else {         q.push(pair<int, int>(a[i], i));       }     }     for (int i = 1; i < n; i++) {       pair<int, int> p = q.top();       q.pop();       if (i < n - 1) {         if (p.second != sol[i - 1]) {           sol[i] = p.second;           p.first--;           if (p.first) q.push(p);         } else if (int(q.size()) == 0) {           return;         } else {           pair<int, int> pbis = q.top();           q.pop();           sol[i] = pbis.second;           pbis.first--;           if (pbis.first) q.push(pbis);           q.push(p);         }       } else {         if (p.second != sol[i - 1] and p.second != sol[0]) {           sol[i] = p.second;           p.first--;           if (p.first) q.push(p);         } else if (int(q.size()) == 0) {           return;         } else {           pair<int, int> pbis = q.top();           q.pop();           if (pbis.second != sol[i - 1] and pbis.second != sol[0]) {             sol[i] = pbis.second;             pbis.first--;             if (pbis.first) q.push(pbis);             q.push(p);           } else if (int(q.size()) == 0) {             return;           } else {             pair<int, int> pbisbis = q.top();             q.pop();             sol[i] = pbisbis.second;             pbisbis.first--;             if (pbisbis.first) q.push(pbisbis);             q.push(p);             q.push(pbis);           }         }       }     }     for (int i = 0; i < n; i++) {       if (i > 0) cout <<    ;       cout << sol[i];     }     cout << endl;     exit(0);   }   int main() {     cin >> n >> m;     int suma = 0;     for (int i = 1; i <= m; i++) {       cin >> a[i];       suma += a[i];     }     if (suma < n) {       cout << -1 << endl;       exit(0);     }     for (int i = 1; i <= m; i++) prueba(i);     cout << -1 << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const int nmax = 5e4 + 5;   int l[nmax], r[nmax];   int main() {     int n, x;     scanf( %d %d , &n, &x);     for (int i = 0; i < n; ++i) {       scanf( %d %d , l + i, r + i);     }     long long res = 0;     for (int i = 0; i < n;) {       if (!(l[i] <= x && r[i] >= x)) {         int lc = 0, rc = 0;         int lc2 = 0, rc2 = 0;         int j = i;         while ((lc < 1 || rc < 1) && lc2 < 2 && rc2 < 2) {           if (r[j] <= x) ++lc;           if (l[j] >= x) ++rc;           if (r[j] < x) ++lc2;           if (l[j] > x) ++rc2;           ++j;         }         if (lc && rc) {           if (l[i] > x) {             res += l[i] - x;           }           if (r[i] < x) {             res += x - r[i];           }           ++i;         } else if (lc2 == 2) {           int nxt = 0;           for (int k = i; k < j; ++k) {             nxt = max(nxt, l[k]);             if (r[k] < x) {               nxt = max(nxt, r[k]);             }           }           res += x - nxt;           x = nxt;         } else if (rc2 == 2) {           int nxt = mod;           for (int k = i; k < j; ++k) {             nxt = min(nxt, r[k]);             if (l[k] > x) {               nxt = min(nxt, l[k]);             }           }           res += nxt - x;           x = nxt;         }       } else {         ++i;       }     }     printf( %lld n , res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline void read(T &x) {     x = 0;     register char c = getchar();     bool f = 0;     while (c !=  -  && (c <  0  || c >  9 )) c = getchar();     if (c ==  - ) c = getchar(), f = 1;     while (c >=  0  && c <=  9 ) x = x * 10 + c - 48, c = getchar();     x = f ? -x : x;     return;   }   template <class T>   inline void write(T x) {     if (!x) {       putchar(48);       return;     }     if (x < 0) x = -x, putchar( - );     int len = -1, z[20];     while (x > 0) z[++len] = x % 10, x /= 10;     for (register int i = len; i >= 0; i--) putchar(z[i] + 48);     return;   }   const int N = 10010;   struct node {     int to, nxt, w;   } g[N * 10];   int lst[N], gl;   void add(int x, int y, int z) {     g[++gl] = (node){y, lst[x], z};     lst[x] = gl;   }   vector<pair<int, int> > E[N];   vector<int> E2[N];   bool vis1[N], visn[N];   void dfs(int x) {     vis1[x] = 1;     for (int i = 0; i < E[x].size(); i++) {       int v = E[x][i].first;       if (!vis1[v]) dfs(v);     }   }   void dfs2(int x) {     visn[x] = 1;     for (int i = 0; i < E2[x].size(); i++) {       int v = E2[x][i];       if (!visn[v]) dfs2(v);     }   }   int cnt[N], dis[N], n, m;   queue<int> q;   bool vis[N];   bool spfa() {     q.push(1);     memset(dis, 127 / 3, sizeof(dis));     dis[1] = 0;     while (!q.empty()) {       int u = q.front();       q.pop();       vis[u] = 0;       for (int i = lst[u]; i; i = g[i].nxt) {         int v = g[i].to;         if (dis[v] > dis[u] + g[i].w) {           dis[v] = dis[u] + g[i].w;           if ((++cnt[v]) == n) return 0;           if (!vis[v]) {             vis[v] = 1;             q.push(v);           }         }       }     }     return 1;   }   int ans[N];   int main() {     read(n), read(m);     for (int i = 1, x, y; i <= m; i++) {       read(x), read(y);       E[x].push_back(make_pair(y, i));       E2[y].push_back(x);     }     dfs(1);     dfs2(n);     for (int u = 1; u <= n; u++)       for (auto v : E[u])         if (vis1[u] && visn[v.first]) {           add(v.first, u, 2);           add(u, v.first, -1);         }     if (!spfa())       puts( No );     else {       puts( Yes );       int flg = 0;       for (int u = 1; u <= n; u++)         for (auto v : E[u])           if (vis1[u] && visn[v.first])             ans[v.second] = dis[u] - dis[v.first];           else             ans[v.second] = 1;       for (int i = 1; i <= m; i++) printf( %d n , ans[i]);     }     return 0;   }


#include <bits/stdc++.h>   template <typename T>   inline T abs(T a) {     return a < 0 ? -a : a;   }   template <typename T>   inline T min(T a, T b) {     return a < b ? a : b;   }   template <typename T>   inline T max(T a, T b) {     return a > b ? a : b;   }   template <typename T>   inline void swap(T& a, T& b) {     T t = a;     a = b, b = t;   }   template <typename T>   inline bool umin(T& mn, T u) {     return u < mn ? mn = u, true : false;   }   template <typename T>   inline bool umax(T& mx, T u) {     return u > mx ? mx = u, true : false;   }   template <typename T>   inline void read(T& r) {     char c;     r = 0;     bool flag = false;     do c = getchar();     while (c !=  -  && (c <  0  || c >  9 ));     if (c ==  - ) flag = true, c = getchar();     do r = r * 10 + c -  0 , c = getchar();     while (c >=  0  && c <=  9 );     if (flag) r *= -1;   }   template <typename T1, typename T2>   inline void read(T1& r, T2& s) {     read(r), read(s);   }   template <typename T1, typename T2, typename T3>   inline void read(T1& r, T2& s, T3& t) {     read(r), read(s), read(t);   }   template <typename T1, typename T2, typename T3, typename T4>   inline void read(T1& r, T2& s, T3& t, T4& u) {     read(r), read(s), read(t), read(u);   }   template <typename T>   inline void write(T w, const char* c =   ) {     if (w == 0)       putchar( 0 );     else {       if (w < 0) putchar( - ), w *= -1;       static int s[21], top;       top = 0;       while (w) s[top++] = w % 10, w /= 10;       while (top--) putchar(s[top] +  0 );     }     for (int i = 0; c[i]; i++) putchar(c[i]);   }   int n, k, qn, en;   long long p[10000];   const long long P = 1e7 + 19, mod = 1e9 + 7;   void init() {     for (int i = p[0] = 1; i < k; i++) p[i] = p[i - 1] * P % mod;   }   struct knapsack {     int dp[10001];     void add(std::pair<int, int> o) {       int v = o.first, w = o.second;       for (int i = k - w; i >= 0; i--) umax(dp[i + w], dp[i] + v);     }     int getans() {       long long ans = 0;       for (int i = 1; i <= k; i++) (ans += dp[i] * p[i - 1]) %= mod;       return (int)ans;     }   } st[160];   struct perfect_binary_tree {     struct ac {       std::pair<int, int> to;       int nx;     } e[4500000];     int L, first[655360];     perfect_binary_tree() : L(1) {}     inline void addedge(int u, std::pair<int, int> v) {       e[L].to = v, e[L].nx = first[u], first[u] = L++;     }     void add(int L, int R, std::pair<int, int> c, int now = 1, int l = 1,              int r = qn) {       if (L <= l && r <= R) {         addedge(now, c);         return;       }       int m = (l + r) >> 1;       if (L <= m) add(L, R, c, now << 1, l, m);       if (m < R) add(L, R, c, now << 1 | 1, m + 1, r);     }     void getans(int depth = 1, int now = 1, int l = 1, int r = qn) {       st[depth] = st[depth - 1];       for (int j = first[now]; j; j = e[j].nx) st[depth].add(e[j].to);       if (l == r) {         write(st[depth].getans(),   n );         return;       }       int m = (l + r) >> 1;       getans(depth + 1, now << 1, l, m);       getans(depth + 1, now << 1 | 1, m + 1, r);     }   } T;   std::pair<int, int> exhibit[150000], show[150000];   void solve() {     init();     for (int i = 0; i < n; i++)       if (show[i].first <= qn && show[i].second >= 1)         T.add(show[i].first, show[i].second, exhibit[i]);     T.getans();   }   void input() {     qn = 0, n = 0;     int m, o;     read(m, k);     while (m--)       read(exhibit[n].first, exhibit[n].second),           show[n++] = std::pair<int, int>(0, -1);     read(m);     while (m--) {       read(o);       switch (o) {         case 1:           read(exhibit[n].first, exhibit[n].second),               show[n++] = std::pair<int, int>(qn + 1, -1);           break;         case 2:           read(o), show[--o].second = qn;           break;         case 3:           ++qn;           break;       }     }     for (int i = 0; i < n; i++)       if (show[i].second == -1) show[i].second = qn;   }   int main() {     input();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct Upgrade {     int id, i, val;     Upgrade(int _id = 0, int _i = 0, int _val = 0) : id(_id), i(_i), val(_val) {}     friend bool operator<(const Upgrade &a, const Upgrade &b) {       return a.val < b.val;     }   };   struct Data {     Upgrade upgrade;     long long nume, deno;     int id;     Data(int _id = 0, long long _nume = 0, long long _deno = 0,          Upgrade _upgrade = Upgrade())         : id(_id), nume(_nume), deno(_deno), upgrade(_upgrade) {}     friend bool operator<(const Data &a, const Data &b) {       return double(a.nume) / a.deno < double(b.nume) / b.deno;     }   };   int n, m, k;   long long a[100010];   bool is_assg[100010];   Upgrade assg[100010];   vector<Upgrade> mult;   vector<Upgrade> add;   vector<Upgrade> add_list[100010];   priority_queue<Data> q;   double sum_mult[100010], sum;   bool cmp(const Upgrade &a, const Upgrade &b) { return b < a; }   int main() {     ios_base::sync_with_stdio(false);     cin >> n >> m >> k;     for (int i = 1; i <= n; i++) cin >> a[i];     memset(assg, -1, sizeof assg);     for (int i = 1; i <= m; i++) {       Upgrade upgrade(i);       int type;       cin >> type >> upgrade.i >> upgrade.val;       if (type == 1) {         is_assg[i] = 1;         if (assg[upgrade.i] < upgrade) assg[upgrade.i] = upgrade;       } else if (type == 2)         add_list[upgrade.i].push_back(upgrade);       else         mult.push_back(upgrade);     }     for (int i = 1; i <= n; i++) {       if (assg[i].id != -1) {         assg[i].val -= a[i];         add_list[i].push_back(assg[i]);       }       sort(add_list[i].begin(), add_list[i].end());       if (!add_list[i].empty()) {         q.push(Data(i, a[i] + add_list[i].back().val, a[i], add_list[i].back()));         add_list[i].pop_back();       }     }     while (!q.empty()) {       Data data = q.top();       q.pop();       add.push_back(data.upgrade);       int i = data.id;       if (!add_list[i].empty()) {         q.push(Data(i, data.nume + add_list[i].back().val,                     data.deno + data.upgrade.val, add_list[i].back()));         add_list[i].pop_back();       }     }     sort(mult.begin(), mult.end(), cmp);     sum_mult[0] = 0;     for (int i = 1; i <= mult.size(); i++)       sum_mult[i] = sum_mult[i - 1] + log(mult[i - 1].val);     sum = 0;     for (int i = 1; i <= n; i++) sum += log(a[i]);     double max_sum = sum + sum_mult[min((int)mult.size(), k)];     int optimal = 0;     for (int i = 0; i < add.size() && i < k; i++) {       sum -= log(a[add[i].i]);       a[add[i].i] += add[i].val;       sum += log(a[add[i].i]);       double total = sum + sum_mult[min((int)mult.size(), k - i - 1)];       if (total > max_sum) {         max_sum = total;         optimal = i + 1;       }     }     vector<int> trace;     for (int i = 0; i < optimal; i++)       if (is_assg[add[i].id]) trace.push_back(add[i].id);     for (int i = 0; i < optimal; i++)       if (!is_assg[add[i].id]) trace.push_back(add[i].id);     for (int i = 0; i < min((int)mult.size(), k - optimal); i++)       trace.push_back(mult[i].id);     cout << trace.size() <<   n ;     for (int i = 0; i < trace.size(); i++) cout << trace[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 10;   bool prime[N];   int phi[N], deg[N];   void sieve();   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     sieve();     int n, k;     cin >> n >> k;     priority_queue<pair<int, int>, vector<pair<int, int>>,                    greater<pair<int, int>>>         q;     for (int i = 3; i <= n; ++i) {       if (deg[i] == 0) {         q.push({phi[i], i});       }       for (int j = 2 * i; j <= n; j += i) {         ++deg[j];       }     }     long long ans = (k == 1 ? 1 : 2);     while (k--) {       auto cur = q.top();       q.pop();       int i = cur.second;       ans += phi[i];       for (int j = 2 * i; j <= n; j += i) {         --deg[j];         if (deg[j] == 0) {           q.push({phi[j], j});         }       }     }     cout << ans <<   n ;   }   void sieve() {     for (int i = 2; i < N; ++i) {       prime[i] = true;       phi[i] = i;     }     for (int i = 2; i < N; ++i) {       if (prime[i]) {         for (int j = i; j < N; j += i) {           prime[j] = false;           phi[j] /= i;           phi[j] *= (i - 1);         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxN = 100010;   int n, m, k, q;   vector<int> G[maxN];   int pre[maxN], s[maxN], e[maxN], cnt[maxN], t;   bool nroot[maxN];   void dfs(int cur) {     s[cur] = ++t;     cnt[cur] = 1;     for (int i = 0; i < (int)G[cur].size(); i++) {       int nxt = G[cur][i];       dfs(nxt);       cnt[cur] += cnt[nxt];     }     e[cur] = t;   }   int main(int argc, char** argv) {     cin >> n >> m >> k >> q;     int a, b;     for (int i = 0; i < k; i++) {       cin >> a >> b;       if (pre[b]) {         G[pre[b]].push_back(a);         nroot[a] = true;       }       pre[b] = a;     }     for (int i = 1; i <= n; i++) {       if (!nroot[i]) dfs(i);     }     for (int i = 0; i < q; i++) {       cin >> a >> b;       cout << ((s[a] < s[pre[b]] && e[a] >= e[pre[b]]) ? cnt[a] : 0) <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, k;     cin >> n >> m >> k;     vector<string> tt(n);     for (int i = 0; i < n; ++i) {       cin >> tt[i];     }     int const INF = 1000000000;     vector<int> dp(k + 1, INF), ndp(k + 1, INF);     dp[0] = 0;     for (int day = 0; day < n; ++day) {       vector<int> prefix(m + 1);       for (int i = 0; i < m; ++i) {         prefix[i + 1] = prefix[i] + (tt[day][i] ==  1 );       }       vector<int> update(m + 1, INF);       for (int arr = 0; arr <= m; ++arr) {         for (int dep = arr; dep <= m; ++dep) {           int plus_time = dep - arr;           int plus_sk = prefix[m] - (prefix[dep] - prefix[arr]);           update[plus_sk] = min(update[plus_sk], plus_time);         }       }       for (int sk = 0; sk <= k; ++sk) {         for (int plus_sk = 0; plus_sk <= m && sk + plus_sk <= k; ++plus_sk) {           ndp[sk + plus_sk] = min(ndp[sk + plus_sk], dp[sk] + update[plus_sk]);         }       }       dp.assign(k + 1, INF);       dp.swap(ndp);     }     int result = INF;     for (int sk = 0; sk <= k; ++sk) {       result = min(result, dp[sk]);     }     cout << result << endl;   }


#include <bits/stdc++.h>   using namespace std;   int n, id = 0;   int Hash[333], L[333];   map<string, int> M;   vector<int> v;   int kmp(int Len) {     int len = v.size();     int lps[len], I = 0, num = 0;     lps[0] = 0;     for (int i = 1; i < len; i++) {       if (v[I] != v[i]) I = -1;       lps[i] = ++I;     }     int i = 0, j = 0;     while (i < n) {       if (Hash[i] == v[j]) {         i++;         j++;       } else {         if (j == 0)           i++;         else {           if (j == lps[j])             j = 0;           else             j = lps[j - 1];         }       }       if (j == len) {         num++;         j = 0;       }     }     if (num == 1) num = 0;     return (Len - 1) * num;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     string s;     int Len = 0, res = 0, len;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> s;       if (!M[s]) {         M[s] = ++id;       }       Hash[i] = M[s];       L[i] = s.length();       Len += L[i];     }     Len += (n - 1);     for (int i = 0; i < n; i++) {       v.clear();       len = 0;       for (int j = i; j < n; j++) {         v.push_back(Hash[j]);         len += L[j];         res = max(res, kmp(len));       }     }     cout << Len - res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1010;   int N;   pair<int, int> point[MAXN];   int cntx[MAXN], cnty[MAXN], cnt1, cnt2;   map<int, vector<pair<int, int> > > samex, samey;   int fa[MAXN];   int find(int x) { return (x == fa[x]) ? x : fa[x] = find(fa[x]); }   void merge(int u, int v) {     u = find(u);     v = find(v);     if (u != v) fa[u] = v;   }   bool check(long long dis) {     for (int i = 1; i <= N; i++) fa[i] = i;     for (int i = 1; i <= cnt1; i++) {       for (int j = 1; j < samex[cntx[i]].size(); j++) {         int id1 = samex[cntx[i]][j - 1].second;         int id2 = samex[cntx[i]][j].second;         if (point[id2].second - point[id1].second <= dis) merge(id1, id2);       }     }     for (int i = 1; i <= cnt2; i++) {       for (int j = 1; j < samey[cnty[i]].size(); j++) {         int id1 = samey[cnty[i]][j - 1].second;         int id2 = samey[cnty[i]][j].second;         if (point[id2].first - point[id1].first <= dis) merge(id1, id2);       }     }     int num = 0;     for (int i = 1; i <= N; i++) num += (i == find(i));     if (num > 4) return false;     if (num == 1) return true;     if (num == 2) {       for (int i = 1; i <= N; i++) {         for (int j = i + 1; j <= N; j++)           if (fa[i] != fa[j]) {             if (point[i].first == point[j].first &&                 abs(point[i].second - point[j].second) <= dis * 2)               return true;             if (point[i].second == point[j].second &&                 abs(point[i].first - point[j].first) <= dis * 2)               return true;             if (abs(point[i].first - point[j].first) <= dis &&                 abs(point[i].second - point[j].second) <= dis)               return true;           }       }     } else if (num == 3) {       vector<pair<int, int> > seg;       for (int i = 1; i <= cnt1; i++) {         for (int j = 1; j < (int)samex[cntx[i]].size(); j++) {           int id1 = samex[cntx[i]][j - 1].second;           int id2 = samex[cntx[i]][j].second;           if (fa[id1] != fa[id2]) {             seg.push_back(make_pair(id1, id2));           }         }       }       for (int i = 1; i <= cnt2; i++) {         for (int j = 1; j < (int)samey[cnty[i]].size(); j++) {           int id1 = samey[cnty[i]][j - 1].second;           int id2 = samey[cnty[i]][j].second;           if (fa[id1] != fa[id2]) {             seg.push_back(make_pair(id1, id2));           }         }       }       for (int i = 0; i < (int)seg.size(); i++) {         int id1 = seg[i].first;         int id2 = seg[i].second;         for (int j = 1; j <= N; j++)           if (fa[id1] != fa[j] && fa[id2] != fa[j]) {             if (point[id1].first == point[id2].first) {               if (min(point[id1].second, point[id2].second) >= point[j].second ||                   max(point[id1].second, point[id2].second) <= point[j].second)                 continue;               if (abs(point[j].first - point[id1].first) <= dis &&                   abs(point[id1].second - point[j].second) <= dis &&                   abs(point[id2].second - point[j].second) <= dis)                 return true;             } else {               if (min(point[id1].first, point[id2].first) >= point[j].first ||                   max(point[id1].first, point[id2].first) <= point[j].first)                 continue;               if (abs(point[j].first - point[id1].first) <= dis &&                   abs(point[id1].second - point[j].second) <= dis &&                   abs(point[j].first - point[id2].first) <= dis)                 return true;             }           }       }     } else {       vector<pair<int, int> > segx, segy;       for (int i = 1; i <= cnt1; i++) {         for (int j = 1; j < (int)samex[cntx[i]].size(); j++) {           int id1 = samex[cntx[i]][j - 1].second;           int id2 = samex[cntx[i]][j].second;           if (fa[id1] != fa[id2]) segx.push_back(make_pair(id1, id2));         }       }       for (int i = 1; i <= cnt2; i++) {         for (int j = 1; j < (int)samey[cnty[i]].size(); j++) {           int id1 = samey[cnty[i]][j - 1].second;           int id2 = samey[cnty[i]][j].second;           if (fa[id1] != fa[id2]) segy.push_back(make_pair(id1, id2));         }       }       for (int i = 0; i < (int)segx.size(); i++) {         for (int j = 0; j < (int)segy.size(); j++) {           int x1 = segx[i].first;           int y1 = segx[i].second;           int x2 = segy[j].first;           int y2 = segy[j].second;           if (fa[x1] == fa[x2] || fa[x1] == fa[y2] || fa[y1] == fa[x2] ||               fa[y1] == fa[y2])             continue;           if (min(point[x2].first, point[y2].first) >= point[x1].first ||               max(point[x2].first, point[y2].first) <= point[x1].first)             continue;           if (min(point[x1].second, point[y1].second) >= point[x2].second ||               max(point[x1].second, point[y1].second) <= point[x2].second)             continue;           if (abs(point[x2].first - point[x1].first) <= dis &&               abs(point[y2].first - point[x1].first) <= dis &&               abs(point[x1].second - point[x2].second) <= dis &&               abs(point[y1].second - point[x2].second) <= dis)             return true;         }       }     }     return false;   }   int main() {     scanf( %d , &N);     cnt1 = cnt2 = 0;     for (int i = 1, x, y; i <= N; i++) {       scanf( %d%d , &x, &y);       point[i] = make_pair(x, y);       cntx[++cnt1] = x;       cnty[++cnt2] = y;       samex[x].push_back(make_pair(y, i));       samey[y].push_back(make_pair(x, i));     }     sort(cntx + 1, cntx + 1 + cnt1);     sort(cnty + 1, cnty + 1 + cnt2);     cnt1 = unique(cntx + 1, cntx + 1 + cnt1) - (cntx + 1);     cnt2 = unique(cnty + 1, cnty + 1 + cnt2) - (cnty + 1);     for (int i = 1; i <= cnt1; i++)       sort(samex[cntx[i]].begin(), samex[cntx[i]].end());     for (int i = 1; i <= cnt2; i++)       sort(samey[cnty[i]].begin(), samey[cnty[i]].end());     long long l = 0;     long long r = 2e9;     long long ans = -1;     while (l <= r) {       long long mid = (l + r) >> 1;       if (check(mid)) {         ans = mid;         r = mid - 1;       } else         l = mid + 1;     }     printf( %lld , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int E = 1e18 + 7;   const long long int MOD = 1000000007;   int main() {     long long int n;     cin >> n;     while (n--) {       string s;       cin >> s;       bool P = true;       bool same = true;       for (int i = 0; i < s.size(); i++) {         if (s[i] != s[0]) {           same = false;         }         if (s[i] != s[s.size() - 1 - i]) {           P = false;         }       }       if (!P) {         cout << s <<   n ;       } else if (same) {         cout << -1 <<   n ;       } else {         for (int i = 1; i < s.size(); i++) {           if (s[i] != s[i - 1]) {             swap(s[i], s[i - 1]);           }         }         cout << s <<   n ;       }     }     return 0;   }


#include<bits/stdc++.h>   using namespace std;   #define fo(a,b,c) for(int a=b;a<=c;++a)   #define fr(a,b,c) for(int a=b;a>=c;--a)   #define ll long long   #define pq priority_queue   int t,n,a[30010];   int cnt[3];   bool check()   {    if(cnt[1]==cnt[2]&&cnt[1]==cnt[0])     return false;    return true;    }   int main()   {    scanf( %d ,&t);     while(t--)    {     memset(cnt,0,sizeof(cnt));     int ans=0;     scanf( %d ,&n);     fo(i,1,n)      scanf( %d ,&a[i]);     fo(i,1,n)     {      if(a[i]%3==0)cnt[0]++;      else if(a[i]%3==1)cnt[1]++;      else cnt[2]++;      }     int sum=n/3,i=0;     while(check())     {      int pre=(i+2)%3;      if(cnt[i]<sum)      {       cnt[pre]-=abs(sum-cnt[i]);       ans+=abs(sum-cnt[i]);       cnt[i]=sum;      }      i=(i+1)%3;     }      printf( %d n ,ans);    }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int xa, ya, xb, yb, n, count = 0;     cin >> xa >> ya >> xb >> yb;     if (xa > xb) {       int t = xa;       xa = xb;       xb = t;     }     if (ya > yb) {       int t = ya;       ya = yb;       yb = t;     }     cin >> n;     int x[n], y[n], r[n];     for (int i = 0; i < n; i++) {       cin >> x[i] >> y[i] >> r[i];     }     for (int i = xa; i <= xb; i++) {       for (int h = 0; h < n; h++) {         double diff = sqrt((x[h] - i) * (x[h] - i) + (y[h] - ya) * (y[h] - ya));         if (diff <= r[h]) {           count++;           break;         }       }       for (int h = 0; h < n; h++) {         double diff = sqrt((x[h] - i) * (x[h] - i) + (y[h] - yb) * (y[h] - yb));         if (diff <= r[h]) {           count++;           break;         }       }     }     for (int j = (ya + 1); j < yb; j++) {       for (int h = 0; h < n; h++) {         double diff = sqrt((x[h] - xa) * (x[h] - xa) + (y[h] - j) * (y[h] - j));         if (diff <= r[h]) {           count++;           break;         }       }       for (int h = 0; h < n; h++) {         double diff = sqrt((x[h] - xb) * (x[h] - xb) + (y[h] - j) * (y[h] - j));         if (diff <= r[h]) {           count++;           break;         }       }     }     int ans = (2 * (xb - xa + yb - ya)) - count;     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n, a;     cin >> n >> a;     int arr[n + 1];     int cnt = 0;     for (int i = 1; i <= n; i++) {       cin >> arr[i];     }     for (int i = 1; i <= n; i++) {       if (arr[i]) {         int temp = i - a;         int d = a - temp;         if (d > n || d < 1 || arr[d] == arr[i]) cnt++;       }     }     cout << cnt;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct point {     long long x, y;   };   long long cross(point a, point b, point c) {     return (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y);   }   int main() {     point a, b, c;     cin >> a.x >> a.y >> b.x >> b.y >> c.x >> c.y;     long long ans = cross(a, b, c);     if (ans > 0)       cout <<  LEFT n ;     else if (ans < 0)       cout <<  RIGHT n ;     else       cout <<  TOWARDS n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long mod = 1e9 + 7;   long long po(long long x, long long y, long long p) {     long long 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;   }   void dfs(vector<vector<long long> > &adj, long long s, long long e,            vector<long long> &v, vector<bool> &vis) {     if (s == e) return;     vis[s] = true;     for (auto vx : adj[s]) {       if (!vis[vx]) {         dfs(adj, vx, e, v, vis);       }     }     v.push_back(s);   }   void solve() {     long long n, m, a, b;     cin >> n >> m >> a >> b;     vector<vector<long long> > adj(n + 1);     while (m--) {       long long x, y;       cin >> x >> y;       adj[x].push_back(y);       adj[y].push_back(x);     }     vector<long long> v1;     vector<long long> v2;     vector<bool> vis(n + 1, false);     dfs(adj, a, b, v1, vis);     for (int i = 0; i < n + 1; i++) vis[i] = false;     dfs(adj, b, a, v2, vis);     sort((v1).begin(), (v1).end());     sort((v2).begin(), (v2).end());     long long n1 = 0, n2 = 0;     for (long long i = 0; i < v1.size(); i++) {       if (!binary_search(v2.begin(), v2.end(), v1[i]) and v1[i] != a) {         n1++;       }     }     for (long long i = 0; i < v2.size(); i++) {       if (!binary_search(v1.begin(), v1.end(), v2[i]) and v2[i] != b) n2++;     }     cout << n1 * n2 <<   n ;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int t = 1;     cin >> t;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};   const int dy[8] = {-1, 0, 1, -1, 1, 0, 1, -1};   const int mod[2] = {1000000007, 1000000009};   const int p[2] = {29, 31};   const int maxn = 3000;   int add(int a, int b, int k) {     return a + b >= mod[k] ? a + b - mod[k] : a + b;   }   int mul(int a, int b, int k) { return 1LL * a * b % mod[k]; }   int sub(int a, int b, int k) { return a < b ? a - b + mod[k] : a - b; }   int pwr(int a, int k, int i) {     int res = 1;     while (k) {       if (k % 2) res = mul(res, a, i);       a = mul(a, a, i);       k /= 2;     }     return res;   }   int invmod(int a, int k) { return pwr(a, mod[k] - 2, k); }   int H[2][maxn], pinv[2][maxn];   pair<int, int> hsb, hse;   string t, sb, se;   pair<int, int> h(string a) {     for (int k = 0; k < 2; ++k) {       int pp = 1;       for (int i = 0; i < a.size(); ++i) {         H[k][i] = mul(a[i] -  a  + 1, pp, k), pp = mul(pp, p[k], k);         if (i) H[k][i] = add(H[k][i - 1], H[k][i], k);       }     }     return {H[0][a.size() - 1], H[1][a.size() - 1]};   }   pair<int, int> f(int i, int j) {     return {mul(pinv[0][i], i ? sub(H[0][j], H[0][i - 1], 0) : H[0][j], 0),             mul(pinv[1][i], i ? sub(H[1][j], H[1][i - 1], 1) : H[1][j], 1)};   }   vector<int> x, y;   vector<pair<int, int> > M;   int main() {     for (int k = 0; k < 2; ++k) {       pinv[k][0] = 1;       pinv[k][1] = invmod(p[k], k);       for (int i = 2; i < maxn; ++i)         pinv[k][i] = mul(pinv[k][i - 1], pinv[k][1], k);     }     cin >> t >> sb >> se;     hsb = h(sb);     hse = h(se);     h(t);     int cnt = 0, a = sb.size(), b = se.size(), c = t.size();     for (int i = 0; i + a - 1 < c; ++i)       if (hsb == f(i, i + a - 1)) x.push_back(i);     for (int i = 0; i + b - 1 < c; ++i)       if (hse == f(i, i + b - 1)) y.push_back(i);     for (int i : x)       for (int j : y)         if (i <= j && i + a <= j + b) M.push_back(f(i, j + b - 1));     sort(M.begin(), M.end());     int res = M.size() != 0;     for (int i = 1; i < M.size(); ++i) res += (M[i] != M[i - 1]);     cout << res <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1000000007;   int n;   long long int dn[6010], dinamik2[6010];   int main() {     string ar;     string ar2;     cin >> ar;     cin >> ar2;     n = ((int)(ar2.size()));     long long res = 0;     for (int k = 0; k < ((int)(ar.size())); k++) {       for (int j = 0; j < ((int)(ar2.size())); j++) {         if (ar2[j] == ar[k]) dinamik2[j] = (dinamik2[j] + dn[j - 1] + 1) % MOD;         dinamik2[j + 1] = (dinamik2[j + 1] + dinamik2[j]) % MOD;       }       res = (res + dinamik2[n]) % MOD;       for (int j = 0; j < ((int)(ar2.size())); j++) dn[j] = dinamik2[j];       memset(dinamik2, 0, sizeof dinamik2);     }     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     set<string> l;     int ans = 0;     while (getline(cin, s)) {       if (s ==   ) break;       if (s[0] ==  + ) {         l.insert(s.substr(1));       } else if (s[0] ==  - ) {         l.erase(s.substr(1));       } else {         int i = 0;         while (s[i] !=  : ) i++;         ans += ((int)s.size() - i - 1) * l.size();       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int pb, mb, bb, m;     cin >> pb >> mb >> bb >> m;     int s1 = 2 * pb;     int temp = 2 * m;     if (2 * m >= s1) {       cout << -1 << endl;       return 0;     }     int s2 = 2 * mb;     if (2 * m >= s2) {       cout << -1 << endl;       return 0;     }     int s3 = 2 * bb, c = 0;     for (int i = bb; i <= s3; i++) {       if (m <= i && temp >= i) {         s3 = i;         c = 1;         break;       }     }     if (c == 0) {       cout << -1 << endl;       return 0;     }     cout << s1 << endl;     cout << s2 << endl;     cout << s3 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string a, b;   struct node {     int x, y;   };   vector<node> ans;   void rua() {     ans.clear();     int n;     scanf( %d , &n);     cin >> a >> b;     for (int i = 0; i < n; i++)       if (a[i] != b[i])         for (int j = i + 1; j < n; j++) {           if (a[i] == a[j]) {             swap(a[j], b[i]);             ans.push_back({j, i});             break;           }           if (a[i] == b[j]) {             swap(a[j], b[j]);             ans.push_back({j, j});             swap(a[j], b[i]);             ans.push_back({j, i});             break;           }         }     if (a != b) {       printf( NO n );       return;     }     printf( YES n%d n , (int)ans.size());     for (auto i : ans) printf( %d %d n , i.x + 1, i.y + 1);   }   int main() {     int t;     scanf( %d , &t);     while (t--) rua();     return 0;   }


#include <bits/stdc++.h>   const int N = 100100;   struct Edge {     int weight, type;     int x, y;     int num;     bool operator<(const Edge& other) const {       return weight == other.weight ? type > other.type : weight < other.weight;     }   } edges[N];   int n, m;   int x[N], y[N];   bool b[N];   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < m; ++i) {       scanf( %d%d , &edges[i].weight, &edges[i].type);       edges[i].num = i;     }     std::sort(edges, edges + m);     int k = 0;     int ptrX = 0, ptrY = 2;     for (int i = 0; i < m; ++i) {       if (edges[i].type == 0) {         if (b[ptrX] && b[ptrY]) {           edges[i].x = ptrX;           edges[i].y = ptrY;           if (ptrX + 2 < ptrY)             ++ptrX;           else {             ptrX = 0;             ++ptrY;           }         } else {           puts( -1 );           return 0;         }       } else {         edges[i].x = k;         edges[i].y = k + 1;         b[k] = true;         b[k + 1] = true;         ++k;       }     }     for (int i = 0; i < m; ++i) {       int num = edges[i].num;       x[num] = edges[i].x;       y[num] = edges[i].y;     }     for (int i = 0; i < m; ++i) printf( %d %d n , x[i] + 1, y[i] + 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool fun(int n) {     if (n == 1) return false;     if (n == 2) return true;     if (n % 2 == 0) return false;     for (int i = 2; i * i <= n; i++) {       if (n % i == 0) return false;     }     return true;   }   int main() {     int n;     scanf( %d , &n);     if (fun(n))       printf( 1 n );     else if (n & 1) {       if (fun(n - 2)) {         printf( 2 n );       } else         printf( 3 n );     } else       printf( 2 n );   }


#include <bits/stdc++.h>   const int MAXN = 55;   const int MAXS = 10105;   const int MOD = 1000000007;   int n;   bool g[MAXN][MAXN];   std::vector<int> e[MAXN][MAXN];   std::vector<int> tr[MAXS];   int f[MAXS];   void input() {     int m;     scanf( %d%d , &n, &m);     for (int i = 0; i < m; ++i) {       int x, y, nv;       scanf( %d%d%d , &x, &y, &nv);       --x;       --y;       std::vector<int> ev(nv);       for (int j = 0; j < nv; ++j) {         int v;         scanf( %d , &v);         --v;         ev[j] = v;       }       g[x][y] = true;       e[x][y] = ev;     }   }   int check_edge(const std::vector<int> &a, int x, int y) {     if (a.empty()) return -1;     for (std::vector<int>::const_iterator i = a.begin(); i + 1 != a.end(); ++i)       if (*i == x && *(i + 1) == y) return i - a.begin();     return -1;   }   int walk(std::vector<int> &a, int now, bool reverse) {     for (int k = 0; (int)a.size() < 2 * n && k < (int)a.size(); ++k) {       if ((!reverse && !g[now][a[k]]) || (reverse && !g[a[k]][now])) return -1;       if (!reverse)         a.insert(a.end(), e[now][a[k]].begin(), e[now][a[k]].end());       else {         a.insert(a.end(), e[a[k]][now].rbegin(), e[a[k]][now].rend());       }       now = a[k];     }     if ((int)a.size() >= 2 * n) return -1;     return now;   }   void add_trans(int x, int y, int len) {     for (int i = 0; i + len <= 2 * n; ++i)       tr[i * (2 * n) + x].push_back((i + len) * (2 * n) + y);   }   void init() {     for (int i = 0; i < n; ++i)       for (int j = 0; j < n; ++j)         if (g[i][j]) {           int pos;           if ((pos = check_edge(e[i][j], i, j)) != -1) {             std::vector<int> tmpl(e[i][j].begin(), e[i][j].begin() + pos);             std::reverse(tmpl.begin(), tmpl.end());             int nowl = walk(tmpl, i, true);             std::vector<int> tmpr(e[i][j].begin() + (pos + 2), e[i][j].end());             int nowr = walk(tmpr, j, false);             if (nowl != -1 && nowr != -1)               add_trans(nowl, nowr + n, tmpl.size() + tmpr.size() + 1);           }           if (e[i][j].empty()) {             add_trans(i + n, j, 1);           }           if (!e[i][j].empty() && e[i][j].front() == j) {             std::vector<int> tmp(e[i][j].begin() + 1, e[i][j].end());             int now = walk(tmp, j, false);             if (now != -1) add_trans(i + n, now + n, tmp.size() + 1);           }           if (!e[i][j].empty() && e[i][j].back() == i) {             std::vector<int> tmp(e[i][j].begin(), e[i][j].end() - 1);             std::reverse(tmp.begin(), tmp.end());             int now = walk(tmp, i, true);             if (now != -1) add_trans(now, j, tmp.size() + 1);           }         }   }   void solve() {     int totstate = (2 * n + 1) * (2 * n);     for (int i = 0; i < n; ++i) f[i] = 1;     for (int i = 0; i < totstate; ++i)       for (std::vector<int>::const_iterator j = tr[i].begin(); j != tr[i].end();            ++j)         (f[*j] += f[i]) %= MOD;     for (int i = 1; i <= 2 * n; ++i) {       int sum = 0;       for (int j = 0; j < n; ++j) (sum += f[i * (2 * n) + j + n]) %= MOD;       printf( %d n , sum);     }   }   int main() {     input();     init();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool jolmetsz(long long a, long long b, long long c, long long x1, long long y1,                 long long x2, long long y2) {     return -c - a * x1 < b * y1 && -c - a * x2 > b * y2;   }   int main() {     long long x1, x2, y1, y2, n, db = 0;     cin >> x1 >> y1 >> x2 >> y2 >> n;     if (y2 < y1) {       swap(y1, y2);       swap(x1, x2);     }     int lep = 0;     for (int i = 0; i < n; i++) {       long long a, b, c;       cin >> a >> b >> c;       if (jolmetsz(a, b, c, x1, y1, x2, y2) || jolmetsz(a, b, c, x2, y2, x1, y1))         db++;     }     cout << db << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a[n];     for (int i = 0; i < n; i++) {       cin >> a[i];     }     string str;     cin >> str;     bool flag = 0;     int start = 0;     set<int> s;     for (int i = 0; i < str.length(); i++) {       if (!flag && str[i] ==  0  && a[i] != i + 1) {         cout <<  NO  << endl;         return 0;       }       if (flag && str[i] ==  0 ) {         s.insert(a[i]);         for (int j = start; j <= i + 1; j++) {           if (s.find(j) == s.end()) {             cout <<  NO  << endl;             return 0;           }         }         s.clear();         flag = 0;       }       if (str[i] ==  1 ) {         s.insert(a[i]);         if (!flag) {           start = i + 1;         }         flag = 1;       }     }     cout <<  YES  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   set<int> s;   int main() {     int x;     cin >> x;     s.insert(x);     for (;;) {       x++;       while (x % 10 == 0) x /= 10;       int sz = s.size();       s.insert(x);       if (sz == s.size()) {         printf( %d , s.size());         return 0;       }     }   }


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


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   int main() {     int n;     cin >> n;     int maxn = 0;     int x, y;     while (n--) {       scanf( %d %d , &x, &y);       maxn = max(maxn, x + y);     }     cout << maxn << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t, n, w;     string s;     cin >> t;     while (t--) {       cin >> n >> s;       bool flag = 0;       if (n % 2) {         for (int i = 0; i < n; i += 2) {           if ((s[i] -  0 ) % 2) {             flag = 1;             break;           }         }         w = flag ? 1 : 2;         cout << w << endl;       } else {         for (int i = 1; i < n; i += 2) {           if ((s[i] -  0 ) % 2 == 0) {             flag = 1;             break;           }         }         w = flag ? 2 : 1;         cout << w << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int a[1111], x, n, s, y, o, oo;   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       cin >> x;       if (x == 0 || x == 100)         a[s++] = x;       else if (x < 10 && o == 0)         a[s++] = x, o = 1;       else if (x % 10 == 0 && oo == 0)         a[s++] = x, oo = 1;       else         y = x;     }     if (o == 0 && oo == 0 && y != 0) a[s++] = y;     cout << s << endl;     for (int i = 0; i < s; i++) cout << a[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   double dist(long long x, long long y) { return sqrt(x * x + y * y); }   int main() {     std::ios_base::sync_with_stdio(false);     std::cin.tie(nullptr);     int test;     std::cin >> test;     double inf = 1000.0 * 1000.0 * 1000.0;     while (test--) {       long long w, h;       std::cin >> w >> h;       long long x1, y1, x2, y2;       std::cin >> x1 >> y1 >> x2 >> y2;       long long ww, hh;       std::cin >> ww >> hh;       double min_ans = inf;       int x_intersect = std::max(0ll, ww - x1);       int y_intersect = std::max(0ll, hh - y1);       if (x2 + x_intersect <= w) {         min_ans = std::min(min_ans, dist(x_intersect, 0ll));       }       if (y2 + y_intersect <= h) {         min_ans = std::min(min_ans, dist(y_intersect, 0ll));       }       x_intersect = std::max(0ll, ww - x1);       y_intersect = std::max(0ll, y2 - (h - hh));       if (x2 + x_intersect <= w) {         min_ans = std::min(min_ans, dist(x_intersect, 0ll));       }       if (y1 - y_intersect >= 0) {         min_ans = std::min(min_ans, dist(y_intersect, 0ll));       }       x_intersect = std::max(0ll, x2 - (w - ww));       y_intersect = std::max(0ll, hh - y1);       if (x1 - x_intersect >= 0) {         min_ans = std::min(min_ans, dist(x_intersect, 0ll));       }       if (y2 + y_intersect <= h) {         min_ans = std::min(min_ans, dist(y_intersect, 0ll));       }       x_intersect = std::max(0ll, x2 - (w - ww));       y_intersect = std::max(0ll, y2 - (h - hh));       if (x1 - x_intersect >= 0) {         min_ans = std::min(min_ans, dist(x_intersect, 0ll));       }       if (y1 - y_intersect >= 0) {         min_ans = std::min(min_ans, dist(y_intersect, 0ll));       }       std::cout.precision(20);       if (min_ans == inf) {         std::cout <<  -1 n ;       } else {         std::cout << min_ans <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   mt19937_64 rnd(time(0));   ifstream in;   ofstream out;   const long long INF = LLONG_MAX;   const long double EPS = 1e-9;   const long double pi = 3.141592653589793238462643383279502884;   long long n;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cout.precision(20);     srand(time(0));     cin >> n;     long long l = 0;     long long r = 1e9;     long long k = 2;     cout <<  0 2  << endl;     cout.flush();     string x;     cin >> x;     if (x[0] ==  b ) swap(l, r);     for (long long i = 0; i < n - 1; i++) {       long long m = (l + r) >> 1;       cout << m <<     << k << endl;       cout.flush();       string x;       cin >> x;       if (x[0] ==  w )         l = m;       else         r = m;     }     cout << l <<     << k - 1 <<     << r <<     << k + 1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long int N;     cin >> N;     long long int A[N];     for (long long int i = 0; i < N; i++) cin >> A[i];     map<long long int, long long int> M;     for (long long int i = 0; i < N; i++) {       if (A[i] != 0) M[A[i]]++;     }     cout << M.size() << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e3 + 10;   struct pos {     double x, y;   } p[maxn];   int n;   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %lf%lf , &p[i].x, &p[i].y);     double ans = 0x3f3f3f3f;     for (int i = 0; i < n; i++) {       pos a = p[i];       pos b = p[(i + 1) % n];       pos c = p[(i + 2) % n];       double dis = (double)(c.x - a.x) * (b.y - c.y) - (c.y - a.y) * (b.x - c.x);       if (dis < 0) dis = -dis;       dis = dis / (sqrt((c.x - a.x) * (c.x - a.x) + (c.y - a.y) * (c.y - a.y)));       dis = dis / 2;       ans = min(ans, dis);     }     printf( %f , ans);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     long long n, i, j, k, x, MOD = 1000000007, ans, a1, a2, b1, b2, a, b;     string s1, s2;     cin >> n >> s1 >> s2;     vector<vector<long long>> dp(n + 1, vector<long long>(4, 0));     dp[0][0] = 1;     for (i = 0; i < n; i++) {       for (j = 0; j < 4; j++) {         if (s1[i] ==  ? )           a1 = 0, a2 = 9;         else           a1 = s1[i] -  0 , a2 = a1;         if (s2[i] ==  ? )           b1 = 0, b2 = 9;         else           b1 = s2[i] -  0 , b2 = b1;         for (a = a1; a <= a2; a++) {           for (b = b1; b <= b2; b++) {             x = j;             if (a < b)               x |= 1;             else if (a > b)               x |= 2;             dp[i + 1][x] = (dp[i + 1][x] + dp[i][j]) % MOD;           }         }       }     }     ans = dp[n][3];     cout << ans <<   n ;     return 0;   }


#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;   int dx[] = {0, 0, +1, -1};   int dy[] = {+1, -1, 0, 0};   template <typename first, typename second>   ostream &operator<<(ostream &os, const pair<first, second> &p) {     return os <<  (  << p.first <<  ,   << p.second <<  ) ;   }   template <typename T>   ostream &operator<<(ostream &os, const vector<T> &v) {     os <<  { ;     for (auto it = v.begin(); it != v.end(); ++it) {       if (it != v.begin()) os <<  ,  ;       os << *it;     }     return os <<  } ;   }   template <typename T>   ostream &operator<<(ostream &os, const set<T> &v) {     os <<  [ ;     for (auto it = v.begin(); it != v.end(); ++it) {       if (it != v.begin()) os <<  ,  ;       os << *it;     }     return os <<  ] ;   }   template <typename T>   ostream &operator<<(ostream &os, const multiset<T> &v) {     os <<  [ ;     for (auto it = v.begin(); it != v.end(); ++it) {       if (it != v.begin()) os <<  ,  ;       os << *it;     }     return os <<  ] ;   }   template <typename first, typename second>   ostream &operator<<(ostream &os, const map<first, second> &v) {     os <<  [ ;     for (auto it = v.begin(); it != v.end(); ++it) {       if (it != v.begin()) os <<  ,  ;       os << it->first <<   =   << it->second;     }     return os <<  ] ;   }   void faltu() { cerr <<   n ; }   template <typename T>   void faltu(T a[], int n) {     for (int i = 0; i < n; ++i) cerr << a[i] <<    ;     cerr <<   n ;   }   template <typename T, typename... hello>   void faltu(T arg, const hello &...rest) {     cerr << arg <<    ;     faltu(rest...);   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     int n;     cin >> n;     while (n--) {       int m;       cin >> m;       vector<int> v(m);       for (int i = 0; i < m; i++) cin >> v[i];       while (!v.empty() && v.back() == 0) v.pop_back();       reverse((v).begin(), (v).end());       while (!v.empty() && v.back() == 0) v.pop_back();       int y = (int)v.size();       int ans = 0;       if (y == 1)         cout << 0 <<   n ;       else {         for (int i = 0; i < y; i++) {           if (v[i] == 0) ans++;         }         cout << ans <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a, b, c, d, e, i, j, ii, jj, zx, xc, qw[200009], we[200009], pas, pi,       k[200009];   pair<pair<long long, long long>, long long> p[600009];   bool bo[200009], rko[200009];   vector<long long> v[200009];   deque<long long> de;   void dfsst(long long q, long long w, long long rr) {     if (rr > xc) {       xc = rr;       zx = q;     }     for (vector<long long>::iterator it = v[q].begin(); it != v[q].end(); it++) {       if ((*it) == w) continue;       dfsst((*it), q, rr + 1);     }   }   void dfsst2(long long q, long long w, long long rr) {     if (rr > xc) {       xc = rr;       zx = q;     }     for (vector<long long>::iterator it = v[q].begin(); it != v[q].end(); it++) {       if ((*it) == w) continue;       dfsst2((*it), q, rr + 1);     }   }   void dfs(long long q, long long w, long long rr) {     qw[q] = rr - 1;     for (vector<long long>::iterator it = v[q].begin(); it != v[q].end(); it++) {       if ((*it) == w) continue;       dfs((*it), q, rr + 1);     }   }   void dfs2(long long q, long long w, long long rr) {     if (d == 0) bo[q] = 1;     if (q == e) d = 1;     we[q] = rr - 1;     for (vector<long long>::iterator it = v[q].begin(); it != v[q].end(); it++) {       if ((*it) == w) continue;       dfs2((*it), q, rr + 1);     }     if (d == 0) bo[q] = 0;   }   void dfs3(long long q, long long w) {     if (q == zx) return;     pi++;     p[pi].first.first = q;     p[pi].first.second = zx;     p[pi].second = q;     for (vector<long long>::iterator it = v[q].begin(); it != v[q].end(); it++) {       if ((*it) == w) continue;       if (bo[(*it)] == 1) {         dfs3((*it), q);         return;       }     }   }   int main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     cin >> a;     for (i = 1; i < a; i++) {       cin >> c >> d;       v[c].push_back(d);       v[d].push_back(c);       k[c]++;       k[d]++;     }     zx = 0;     xc = 0;     dfsst(1, 0, 1);     e = zx;     zx = 0;     xc = 0;     dfsst2(e, 0, 1);     dfs(e, 0, 1);     d = 0;     dfs2(zx, 0, 1);     for (i = 1; i <= a; i++) {       if (bo[i] == 0 && v[i].size() == 1) {         de.push_back(i);         rko[i] = 1;       }     }     while (de.size() != 0) {       c = de.front();       de.pop_front();       pas += max(qw[c], we[c]);       pi++;       if (qw[c] >= we[c]) {         p[pi].first.first = e;         p[pi].first.second = c;         p[pi].second = c;       } else {         p[pi].first.first = zx;         p[pi].first.second = c;         p[pi].second = c;       }       d = 0;       for (vector<long long>::iterator it = v[c].begin(); it != v[c].end();            it++) {         if (rko[(*it)] == 1) continue;         d = (*it);         break;       }       k[d]--;       if (d != 0 && bo[d] == 0 && k[d] == 1) {         de.push_back(d);         rko[d] = 1;       }     }     pas += qw[zx] * (qw[zx] + 1) / 2;     dfs3(e, 0);     cout << pas << endl;     for (i = 1; i <= pi; i++)       cout << p[i].first.first <<     << p[i].first.second <<     << p[i].second            << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool mysort(const pair<double, int> &a, const pair<double, int> &b) {     if (a.first == b.first)       return a.second < b.second;     else       return a.first > b.first;   }   int main() {     int n, t1, t2, k, a, b, i;     double h1, h2;     vector<pair<double, int> > vec;     cin >> n >> t1 >> t2 >> k;     for (i = 1; i <= n; i++) {       cin >> a >> b;       h1 = (1.0 * a * t1 * (100 - k) / 100) + b * t2;       h2 = (1.0 * b * t1 * (100 - k) / 100) + a * t1;       h1 = a * t1 * (1 - (k * 1.0) / 100) + b * t2;       h2 = b * t1 * (1 - (k * 1.0) / 100) + a * t2;       vec.push_back(make_pair(max(h1, h2), i));     }     sort(vec.begin(), vec.end(), mysort);     for (i = 0; i < vec.size(); i++)       cout << vec[i].second <<     << fixed << setprecision(2) << vec[i].first            << endl;   }


#include <bits/stdc++.h>   using namespace std;   int c[1000001];   int b[1000001];   int main() {     int n, i, l, r, m, kq = 1, a;     scanf( %d , &n);     for (i = 1; i <= n; i++) {       cin >> a;       c[a] = i;       b[i] = 0;     }     cin >> a;     b[1] = c[a];     for (i = 2; i <= n; i++) {       cin >> a;       a = c[a];       l = 1;       r = i;       while (l < r) {         m = (l + r) / 2;         if (b[m] > a)           l = m + 1;         else           r = m;       }       b[r] = a;       if (kq < r) kq = r;     }     cout << kq;     return 0;   }


#include <bits/stdc++.h>   int a[201][3];   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, k;     cin >> n >> k;     vector<string> a;     for (int i = 0; i < n; i++) {       string r;       cin >> r;       a.push_back(r);     }     int count = 0, ans = 0;     for (int i = 0; i < n; i++) {       for (int j = 0; j < a[i].size(); j++) {         if (a[i][j] ==  4  || a[i][j] ==  7 ) count++;         if (count > k) break;       }       if (count <= k) ans++;       count = 0;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, k, i = 1;     cin >> n >> k;     while (~k & 1) k >>= 1, i++;     cout << i;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct edge {     int to, cost;   };   vector<edge> G[500005];   int q, n, k;   long long f[500005], g[500005];   vector<long long> weights[500005];   void add_edge(int u, int v, int cost) {     G[u].push_back((edge){v, cost});     G[v].push_back((edge){u, cost});   }   void dfs(int v, int p) {     f[v] = g[v] = 0;     for (auto e : G[v]) {       if (e.to == p) continue;       dfs(e.to, v);       f[v] += f[e.to];       g[v] += f[e.to];       weights[v].push_back(e.cost + g[e.to] - f[e.to]);     }     sort(weights[v].begin(), weights[v].end(), greater<long long>());     long long sum1 = 0, sum2 = 0;     for (int i = 0; i < (int)weights[v].size(); i++) {       if (weights[v][i] <= 0) break;       if (i < k - 1) sum1 += weights[v][i];       if (i < k) sum2 += weights[v][i];     }     f[v] += sum2;     g[v] += sum1;   }   int main() {     scanf( %d , &q);     while (q--) {       scanf( %d%d , &n, &k);       for (int i = 1; i <= n; i++) G[i].clear(), weights[i].clear();       for (int i = 0; i < n - 1; i++) {         int u, v, cost;         scanf( %d%d%d , &u, &v, &cost);         add_edge(u, v, cost);       }       dfs(1, 0);       printf( %lld n , f[1]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   mt19937 ran(time(0));   long long mod = 1e9 + 7;   long long gcd(long long a, long long b) {     if (!b) return a;     return gcd(b, a % b);   }   long long Bpow(long long a, long long b) {     long long ans = 1;     while (b) {       if (b & 1) {         ans *= a;         ans %= mod;       }       a = a * a % mod;       b >>= 1;     }     return ans;   }   const long long MX = 1 << 19;   long long t1[2 * MX];   long long t2[2 * MX];   void upd(long long x) {     x += MX;     for (; x > 0; x >>= 1) {       t1[x]++;     }   }   void upd2(long long l, long long r, long long x) {     for (l += MX, r += MX; l <= r; l >>= 1, r >>= 1) {       if (l & 1) {         t2[l++] -= x;       }       if (~r & 1) t2[r--] -= x;     }   }   long long get1(long long l, long long r) {     long long ans = 0;     for (l += MX, r += MX; l <= r; l >>= 1, r >>= 1) {       if (l & 1) ans += t1[l++];       if (~r & 1) ans += t1[r--];     }     return ans;   }   long long get2(long long x) {     x += MX;     long long ans = 0;     while (x > 0) {       ans += t2[x];       x >>= 1;     }     return ans;   }   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long n;     cin >> n;     vector<long long> a(n);     for (long long i = 0; i < n; i++) {       cin >> a[i];     }     long long ans = 0;     vector<long long> h(MX + 1);     long long sum = 0;     for (long long i = 0; i < n; i++) {       ans += sum + i * a[i];       long long now = a[i];       while (now < MX) {         long long v = get1(now, MX - 1);         upd2(now + (a[i] == now), MX - 1, a[i]);         ans -= a[i] * v;         now += a[i];       }       ans += get2(a[i]);       upd(a[i]);       h[a[i]]++;       sum += a[i];       cout << ans <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100 * 1000 + 23, D = 1000 * 1000 * 1000 + 7, MXLN = 17 + 3;   int mul() { return 1; }   template <typename... T>   int mul(int x, T... a) {     return 1ll * x * mul(a...) % D;   }   int add() { return 0; }   template <typename... T>   int add(int x, T... a) {     return (x + add(a...)) % D;   }   int n, e[MAXN], w[MAXN], sum[MAXN], sum2[MAXN], up[MAXN], up2[MAXN], sz[MAXN],       h[MAXN], par[MXLN][MAXN], dis[MAXN], q;   vector<int> g[MAXN];   void dfs(int v = 0, int p = -1) {     for (auto i : g[v]) {       int u = v ^ e[i];       if (u == p) continue;       h[u] = h[v] + 1;       dis[u] = add(dis[v], w[i]);       par[0][u] = v;       dfs(u, v);       sz[v] += sz[u];       sum[v] = add(sum[v], sum[u], mul(sz[u], w[i]));       sum2[v] =           add(sum2[v], mul(w[i], w[i], sz[u]), sum2[u], mul(2, w[i], sum[u]));     }     sz[v]++;   }   void ufs(int v = 0, int p = -1) {     int tmp = up[v], tmp2 = up2[v];     for (auto i : g[v]) {       int u = v ^ e[i];       if (u == p) continue;       tmp = add(tmp, sum[u], mul(sz[u], w[i]));       tmp2 = add(tmp2, mul(w[i], w[i], sz[u]), sum2[u], mul(2, w[i], sum[u]));     }     for (auto i : g[v]) {       int u = v ^ e[i];       if (u == p) continue;       up[u] = add(tmp, D - sum[u], D - mul(sz[u], w[i]), mul(w[i], (n - sz[u])));       up2[u] = add(tmp2, D - mul(w[i], w[i], sz[u]), D - sum2[u],                    D - mul(2, w[i], sum[u]), mul(w[i], w[i], n - sz[u]),                    mul(2, w[i], add(tmp, D - sum[u], D - mul(sz[u], w[i]))));       ufs(u, v);     }   }   int lca(int u, int v) {     if (h[u] < h[v]) swap(u, v);     for (int i = MXLN - 1; ~i; i--)       if (h[par[i][u]] >= h[v]) u = par[i][u];     if (u == v) return v;     for (int i = MXLN - 1; ~i; i--)       if (par[i][u] != par[i][v]) {         u = par[i][u];         v = par[i][v];       }     return par[0][v];   }   int main() {     cin >> n;     for (int i = 1; i < n; i++) {       int a, b, c;       cin >> a >> b >> c;       g[--a].push_back(i), g[--b].push_back(i);       e[i] = a ^ b;       w[i] = c;     }     dfs(), ufs();     for (int i = 1; i < MXLN; i++)       for (int j = 0; j < n; j++) par[i][j] = par[i - 1][par[i - 1][j]];     cin >> q;     while (q--) {       int u, v;       cin >> u >> v;       int x = lca(--u, --v);       if (x == v) {         int fas = add(dis[u], D - dis[v]),             tmp = D - add(up2[v], mul(fas, fas, (n - sz[v])), mul(2, fas, up[v]));         cout << add(sum2[u], up2[u], tmp, tmp) <<   n ;       } else {         int fas = ((x == u) ? add(dis[v], D - dis[u])                             : add(dis[u], dis[v], D - dis[x], D - dis[x])),             tmp = add(mul(fas, fas, sz[v]), sum2[v], mul(2, fas, sum[v]));         cout << add(tmp, D - sum2[u], D - up2[u], tmp) <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int z[201010];   int cost[201010], acc[201010];   string s, aux, text, text2, patt;   pair<int, int> dp[201010];   void build() {     text = aux;     text2 = aux;     int n = (int)aux.size();     for (int i = 0; i < n; i++) {       cost[i] = -1;       acc[i] = 0;       if (aux[i] ==  ? ) {         text[i] =  a ;         text2[i] =  b ;         if (i % 2 == 0) swap(text[i], text2[i]);         acc[i] = 1;       }       if (i) acc[i] += acc[i - 1];     }   }   int getCost(int l, int r) { return acc[r] - (l ? acc[l - 1] : 0); }   void Z() {     int l = 0, r = 0, n = (int)s.size();     z[0] = 0;     for (int i = 1; i < n; i++) {       z[i] = 0;       if (i <= r) z[i] = min(z[i - l], r - i + 1);       while (i + z[i] < n && s[z[i]] == s[z[i] + i]) z[i]++;       if (i + z[i] - 1 >= r) {         l = i;         r = i + z[i] - 1;       }       if (z[i] == (int)patt.size())         cost[i - 2] = getCost(i - (int)patt.size() - 1, i - 2);     }   }   pair<int, int> operator+(pair<int, int> a, pair<int, int> b) {     return make_pair(a.first + b.first, a.second + b.second);   }   pair<int, int> maxx(pair<int, int> a, pair<int, int> b) {     if (a.first > b.first) return a;     if (a.first < b.first) return b;     return make_pair(a.first, min(a.second, b.second));   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int m, n;     cin >> n;     cin >> aux;     cin >> m;     patt =   ;     for (int i = 0; i < m; i++) {       if (i % 2 == 0)         patt +=  a ;       else         patt +=  b ;     }     build();     s = patt +  $  + text;     Z();     s = patt +  $  + text2;     Z();     for (int i = 0; i < n; i++) {       dp[i] = i ? dp[i - 1] : make_pair(0, 0);       if (cost[i] != -1) {         dp[i] = maxx(dp[i], dp[i - m] + make_pair(1, cost[i]));       }     }     cout << dp[n - 1].second <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mxn = 5005;   int dp[mxn][mxn], ans, n, a[mxn];   int md7[8], pos[114514];   inline void ckmax(int& x, int y) {     if (x < y) x = y;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0), cout.tie(0);     cin >> n;     for (int i = 1; i <= n; ++i) cin >> a[i];     for (int i = 0; i <= n; ++i) {       for (int j = 0; j <= n; ++j) {         if (i < j)           dp[i][j] =               max({md7[a[j] % 7], pos[a[j] - 1], pos[a[j] + 1], dp[0][i]}) + 1;         else           dp[i][j] = dp[j][i];         ckmax(ans, dp[i][j]);         ckmax(md7[a[j] % 7], dp[i][j]);         ckmax(pos[a[j]], dp[i][j]);       }       memset(md7, 0, sizeof(md7));       memset(pos, 0, sizeof(pos));     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   long long value(const vector<long long>& a, long long b) {     long long result = 0;     for (int i = 0; i < a.size(); ++i) {       result += min(b, a[i]);     }     return result;   }   int main() {     long long n, k;     cin >> n >> k;     vector<long long> a(n);     long long sum = 0;     long long max_a = 0;     for (int i = 0; i < n; ++i) {       cin >> a[i];       sum += a[i];       max_a = max(max_a, a[i]);     }     if (sum < k) {       cout << -1;       return 0;     }     int l = 0;     int r = max_a;     while (r - l > 1) {       if (value(a, (l + r) / 2) > k) {         r = (l + r) / 2;       } else {         l = (l + r) / 2;       }     }     if (k >= value(a, r)) {       l = r;     }     k -= value(a, l);     queue<pair<int, int> > q;     for (int i = 0; i < n; ++i) {       if (a[i] > l) {         q.push(make_pair(a[i] - l, i));       }     }     while (k > 0) {       --k;       pair<int, int> t = q.front();       q.pop();       --t.first;       if (t.first > 0) {         q.push(t);       }     }     while (!q.empty()) {       cout << q.front().second + 1 <<    ;       q.pop();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e9 + 7;   const int N = 2e2 + 10;   vector<string> v[N];   int nex[N];   int flag[N];   int main() {     int n;     cin >> n;     map<char, int> mc;     memset((nex), (-1), sizeof(nex));     while (n--) {       string str;       cin >> str;       int o = 0;       for (auto &c : str) {         mc[c] = 1;         if (flag[c] == 0) flag[c] = 1;         if (o >> (c -  0 ) & 1) {           puts( NO );           return 0;         }         o |= 1 << (c -  0 );       }       for (int i = 0; i + 1 < str.length(); i++) {         if (nex[str[i]] == -1) {           nex[str[i]] = str[i + 1];           flag[str[i + 1]] = 2;         }         if (nex[str[i]] != str[i + 1]) {           puts( NO );           return 0;         }       }     }     string ans;     for (int i =  a ; i <=  z ; i++) {       if (flag[i] != 1) continue;       int c = i;       while (c != -1 && ans.length() <= mc.size()) {         ans.push_back(c);         c = nex[c];       }     }     if (ans.length() != mc.size()) {       puts( NO );     } else       puts(ans.c_str());     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   using v2d = vector<vector<T> >;   template <class T>   bool uin(T &a, T b) {     return a > b ? (a = b, true) : false;   }   template <class T>   bool uax(T &a, T b) {     return a < b ? (a = b, true) : false;   }   mt19937 rng(chrono::system_clock::now().time_since_epoch().count());   const int maxN = 2e5 + 10;   int n, q;   vector<int> adj[maxN];   int t[maxN], in[maxN], out[maxN], f[maxN * 4], lazy[maxN * 4], mp[maxN];   int L, R;   void dfs(int u) {     in[u] = ++in[0];     mp[in[u]] = u;     for (auto &v : adj[u]) {       dfs(v);     }     out[u] = in[0];   }   void build(int x, int lo, int hi) {     if (lo == hi) {       f[x] = t[mp[lo]];       return;     }     int mid = (lo + hi) / 2;     build(x * 2, lo, mid);     build(x * 2 + 1, mid + 1, hi);     f[x] = f[x * 2] + f[x * 2 + 1];   }   void down(int x, int lo, int hi) {     int t = lazy[x];     lazy[x] = 0;     if (t & 1) {       int mid = (lo + hi) / 2;       ++lazy[x * 2];       f[x * 2] = mid - lo + 1 - f[x * 2];       ++lazy[x * 2 + 1];       f[x * 2 + 1] = hi - mid - f[x * 2 + 1];     }   }   void update(int x, int lo, int hi) {     if (lo > R || hi < L) {       return;     }     if (lo >= L && hi <= R) {       f[x] = hi - lo + 1 - f[x];       ++lazy[x];       return;     }     down(x, lo, hi);     int mid = (lo + hi) / 2;     update(x * 2, lo, mid);     update(x * 2 + 1, mid + 1, hi);     f[x] = f[x * 2] + f[x * 2 + 1];   }   int query(int x, int lo, int hi) {     if (lo > R || hi < L) {       return 0;     }     if (lo >= L && hi <= R) {       return f[x];     }     int mid = (lo + hi) / 2;     down(x, lo, hi);     return query(x * 2, lo, mid) + query(x * 2 + 1, mid + 1, hi);   }   void solve() {     cin >> n;     for (int i = (int)(2); i <= (int)(n); ++i) {       int p;       cin >> p;       adj[p].emplace_back(i);     }     for (int i = 1; i <= (int)(n); ++i) {       cin >> t[i];     }     dfs(1);     build(1, 1, n);     cin >> q;     while (q--) {       string task;       int v;       cin >> task >> v;       if (task ==  get ) {         L = in[v], R = out[v];         cout << query(1, 1, n) <<   n ;       } else {         L = in[v], R = out[v];         update(1, 1, n);       }     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int T = 1;     while (T--) {       solve();     }     return 0;   }