#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n, m, x;     cin >> n >> m;     vector<long long> vPref(n), vSuf(n), vTot(n), vCen(n);     for (int i(0), ThkMk(n); i < ThkMk; ++i) {       cin >> x;       vector<long long> v(x);       vector<long long> prefix(x);       vector<long long> suffix(x);       vector<long long> dp(x);       long long curMx, curSuf, curPref;       for (int i(0), ThkMk(x); i < ThkMk; ++i) {         cin >> v[i];         dp[i] = prefix[i] = v[i];         if (i > 0) {           prefix[i] += prefix[i - 1];           dp[i] += max(0LL, dp[i - 1]);           curMx = max(curMx, dp[i]);         } else           curMx = v[0];       }       suffix[x - 1] = v[x - 1];       curSuf = suffix[0];       curPref = prefix[0];       for (int i = x - 2; i >= 0; --i) suffix[i] += suffix[i + 1] + v[i];       for (int i(0), ThkMk(x); i < ThkMk; ++i) {         curSuf = max(curSuf, suffix[i]);         curPref = max(curPref, prefix[i]);       }       vPref[i] = curPref;       vSuf[i] = curSuf;       vTot[i] = suffix[0];       vCen[i] = curMx;     }     long long ans = -1e18;     long long prev = 0;     for (int i(0), ThkMk(m); i < ThkMk; ++i) {       cin >> x;       --x;       ans = max(ans, vCen[x]);       ans = max(ans, prev + vPref[x]);       prev = max({0LL, prev + vTot[x], vSuf[x]});     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1000000007ll;   const double EPS = 1e-11;   int n;   char s[1252525];   int cntright[1252525];   int cntleft[1252525];   int revright[1252525];   int revleft[1252525];   long long sumright[1252525];   long long sumleft[1252525];   int main() {     scanf( %d , &n);     scanf( %s , s);     for (int i = 0; i < (int)(n); ++i) {       if (s[i] ==  U ) {         cntright[i + 1] = cntright[i] + 1;         cntleft[i + 1] = cntleft[i];         revright[cntright[i + 1]] = i + 1;       } else {         cntright[i + 1] = cntright[i];         cntleft[i + 1] = cntleft[i] + 1;         revleft[cntleft[i + 1]] = i + 1;       }     }     for (int i = 0; i < (int)(n); ++i) {       if (s[i] ==  U ) {         sumright[i + 1] = sumright[i] + (n - i);         sumleft[i + 1] = sumleft[i];       } else {         sumright[i + 1] = sumright[i];         sumleft[i + 1] = sumleft[i] + (i + 1);       }     }     for (int q = 0; q < (int)(n); ++q) {       int l = cntright[q];       int r = cntleft[n] - cntleft[q + 1];       if (s[q] ==  U ) {         if (r > l) {           int rightbnd = l + 1;           int leftbnd = l;           int rrr = revleft[cntleft[q + 1] + rightbnd];           int lll = 0;           long long ans = 0ll;           ans +=               (sumright[q] - sumright[lll] - (long long)leftbnd * (n - q)) * 2ll;           ans += (sumleft[rrr] - sumleft[q + 1] - (long long)rightbnd * (q + 1)) *                  2ll;           ans += q + 1;           printf( %I64d , ans);         } else {           int rightbnd = r;           int leftbnd = r;           int rrr = n;           int lll = revright[cntright[q] - leftbnd];           long long ans = 0ll;           ans +=               (sumright[q] - sumright[lll] - (long long)leftbnd * (n - q)) * 2ll;           ans += (sumleft[rrr] - sumleft[q + 1] - (long long)rightbnd * (q + 1)) *                  2ll;           ans += n - q;           printf( %I64d , ans);         }       } else {         if (l > r) {           int rightbnd = r;           int leftbnd = r + 1;           int rrr = n;           int lll = revright[cntright[q] - leftbnd];           long long ans = 0ll;           ans +=               (sumright[q] - sumright[lll] - (long long)leftbnd * (n - q)) * 2ll;           ans += (sumleft[rrr] - sumleft[q + 1] - (long long)rightbnd * (q + 1)) *                  2ll;           ans += n - q;           printf( %I64d , ans);         } else {           int rightbnd = l;           int leftbnd = l;           int rrr = revleft[cntleft[q + 1] + rightbnd];           int lll = 0;           long long ans = 0ll;           ans +=               (sumright[q] - sumright[lll] - (long long)leftbnd * (n - q)) * 2ll;           ans += (sumleft[rrr] - sumleft[q + 1] - (long long)rightbnd * (q + 1)) *                  2ll;           ans += q + 1;           printf( %I64d , ans);         }       }       if (q != n - 1) {         putchar(   );       } else {         putchar(  n );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     long long n, k, m;     cin >> n >> k >> m;     vector<long long> a(k);     for (long long& v : a) cin >> v;     sort(a.begin(), a.end());     a.push_back(0);     k++;     a.insert(a.begin(), 0);     for (int i = 1; i <= k; i++) a[i] += a[i - 1];     vector<long long> dp((k + 1) * n, 1e18);     dp[0] = 0;     for (int i = 0; i < n; i++) {       auto ndp = dp;       for (int j = 0; j <= k; j++) {         for (int l = dp.size() - 1 - j; l >= 0; l--)           ndp[l + j] = min(ndp[l + j], dp[l] + a[j]);       }       dp = ndp;     }     cout << upper_bound(dp.begin(), dp.end(), m) - dp.begin() - 1 << endl;   }


#include <bits/stdc++.h>   using namespace std;   long long int M = 1e9 + 7;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int t;     cin >> t;     vector<long long int> v;     for (long long int i = 1; i <= 100000; ++i) {       long long int x = (i * (3 * i + 1)) / 2;       if (x <= 2e9) {         v.push_back(x);       }     }     while (t--) {       long long int n;       cin >> n;       long long int ans = 0;       while (n >= 2) {         long long int x = lower_bound(v.begin(), v.end(), n) - v.begin();         if (v[x] != n) x--;         n -= v[x];         ans++;       }       cout << ans <<   n ;     }   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:256000000 )   using namespace std;   int n;   vector<long long> a(200013);   bool f(long long m) {     long long l = (long long)(a[0] + 2 * m), r = (long long)(a[n - 1] - 2 * m);     if (l >= r) {       return 1;     }     if ((r - l) <= 2 * m) {       return 1;     }     long long ll =         a[int(upper_bound(a.begin(), a.begin() + n, l) - a.begin())] + 2 * m;     long long rr = a[int(upper_bound(a.begin(), a.begin() + n, ll) - a.begin())];     if (rr == 0) {       return 1;     }     return (rr >= r);   }   int main() {     cin >> n;     for (long long i = 0; i < n; i++) {       cin >> a[i];       a[i] *= 2;     }     sort(a.begin(), a.begin() + n);     long long l = 0, r = 2000000000;     while (l + 1 < r) {       long long m = (l + r) / 2;       if (!f(m)) {         l = m;       } else {         r = m;       }     }     if (!f(l)) l++;     cout.setf(ios::fixed);     cout.precision(6);     cout << (double)l / 2.0 << endl          << (double)(a[0] + l) / 2.0 <<     << (double)(a[n - 1] - l) / 2.0          <<    ;     long long ll = (long long)(a[0] + 2 * l);     long long lll = a[int(upper_bound(a.begin(), a.begin() + n, ll) - a.begin())];     cout << (double)(lll + l) / 2.0;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1000000009;   long long modexp_recursion(long long a, long long b, long long n) {     long long t = 1;     if (b == 0) return 1;     if (b == 1) return a % n;     t = modexp_recursion(a, b >> 1, n);     t = t * t % n;     if (b & 0x1) {       t = t * a % n;     }     return t;   }   int main() {     long long n, m, k;     cin >> n >> m >> k;     long long a = m / (k - 1);     if (a <= n - m)       cout << m << endl;     else {       long long ret = 0;       a = n - (n - m) * k;       long long b = a / k;       if (b > 0) {         ret = 2 * k * modexp_recursion(2, b, MOD) - 2 * k;         ret = ret % MOD;       }       ret = (ret + (n - m) * (k - 1)) % MOD;       ret = (ret + (a - a / k * k)) % MOD;       cout << ret << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int read(int f = 1, int x = 0, char ch =    ) {     while (!isdigit(ch = getchar()))       if (ch ==  - ) f = -1;     while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar();     return f * x;   }   const int N = 5e5 + 5;   int n, m, f[N], g[N], fs[N], gs[N], ft[N], gt[N], cls[N];   vector<pair<int, long long> > s[N];   int find(int *p, int x) { return p[x] == x ? x : find(p, p[x]); }   void merge(int *p, int *s, int *t, int x, int y, int z) {     x = find(p, x), y = find(p, y);     if (s[x] > s[y]) swap(x, y);     p[x] = y, s[y] += s[x], t[x] = z;   }   long long ask(vector<pair<int, long long> > &s, int t) {     return s.back().second -            (--lower_bound(s.begin(), s.end(), pair<int, long long>(t, 0)))                ->second;   }   int main() {     n = read(), m = read();     for (int i = 1; i <= n; ++i)       f[i] = g[i] = i, fs[i] = gs[i] = 1, s[i].emplace_back(-1, 0);     for (int i = 1; i <= m; ++i) {       char op[2];       int x, y;       scanf( %s , op);       if (op[0] ==  U )         x = read(), y = read(), merge(f, fs, ft, x, y, i);       else if (op[0] ==  M )         x = read(), y = read(), merge(g, gs, gt, x, y, i);       else if (op[0] ==  A )         x = read(), x = find(f, x),         s[x].emplace_back(i, s[x].back().second + fs[x]);       else if (op[0] ==  Z )         x = read(), x = find(g, x), cls[x] = i;       else {         int x = read(), y, t = cls[x];         for (y = x; g[y] != y; y = g[y])           if (cls[g[y]] > gt[y]) t = max(t, cls[g[y]]);         long long ans = ask(s[x], t);         for (y = x; f[y] != y; y = f[y]) ans += ask(s[f[y]], max(t, ft[y]));         printf( %lld n , ans);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = (int)1e9 + 7;   const int MAXN = (int)1003;   const int LOG = (int)11;   const int infint = (int)1e9 + 3;   const long long inf = (long long)1e12;   string A, B[MAXN];   int cost[MAXN], dp[MAXN][MAXN], last[MAXN], n, mx, sza;   pair<int, int> radix[MAXN];   void build() {     memset(dp, -63, sizeof dp);     for (auto &ch : A) ch -=  0 ;     mx++;     reverse(A.begin(), A.end());     sza = A.size();     while ((int)A.size() < mx) A += (char)(0);     for (int i = 0; i < n; i++) {       last[i] = B[i].size();       reverse(B[i].begin(), B[i].end());       while ((int)B[i].size() < mx) B[i] +=  0 ;       radix[i] = {B[i][0], i};     }     sort(radix, radix + n);     reverse(radix, radix + n);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> A >> n;     mx = A.size();     for (int i = 0; i < n; i++) {       cin >> B[i];       mx = max(mx, (int)B[i].size());     }     for (int i = 0; i < 10; i++) cin >> cost[i];     build();     dp[0][0] = 0;     for (int i = 0; i < mx; i++) {       int l, r;       if (A[i] ==  ?  -  0 )         l = 0, r = 9;       else         l = r = A[i];       if (i == sza - 1 && A[i] ==  ?  -  0 ) l = 1;       for (int k = l; k <= r; k++) {         int pt = 0, add = 0;         for (int j = 0; j < n; j++) {           int ind = radix[j].second;           int nxt = B[ind][i] -  0  + k;           if (nxt >= 10) pt++;           if (max(last[ind], sza) > i || nxt > 0) add += cost[nxt % 10];         }         for (int j = 0; j <= n; j++) {           dp[i + 1][pt] = max(dp[i + 1][pt], dp[i][j] + add);           if (j == n) continue;           int ind = radix[j].second;           int nxt = B[ind][i] -  0  + k;           if (nxt == 9) pt++;           if (max(last[ind], sza) > i || nxt > 0) add -= cost[nxt % 10];           add += cost[(nxt + 1) % 10];         }       }       reverse(radix, radix + n);       for (int j = 0; j < n; j++) {         int ind = radix[j].second;         radix[j].first = n * B[ind][i] + j;       }       sort(radix, radix + n);       reverse(radix, radix + n);     }     cout << dp[mx][0];   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     int a, b, c, i, j, cnt = 0;     cin >> a >> b >> c;     for (i = 0; i <= c; i++) {       for (j = c; j >= 0; j--) {         if ((j * a + i * b) == c) {           cout <<  Yes  << endl;           cnt = 1;           break;         }       }       if (cnt == 1) break;     }     if (cnt == 0) cout <<  No  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = (long long)(1e6) + 322;   const long long MAXN = (long long)(1e7) + 100;   const long long inf = (long long)(3e18) + 322;   const long long mod = (long long)(1e9) + 7;   const double eps = 1e-6;   inline void upd(long long &mn, long long x, long long y, long long z) {     mn = min(mn, (x - y) * (x - y) + (y - z) * (y - z) + (z - x) * (z - x));   }   void solve() {     long long mn = inf;     long long n1, n2, n3;     vector<long long> v[4];     cin >> n1 >> n2 >> n3;     for (long long i = 1; i <= n1; ++i) {       long long x;       cin >> x;       v[0].push_back(x);     }     sort((v[0]).begin(), (v[0]).end());     for (long long i = 1; i <= n2; ++i) {       long long x;       cin >> x;       v[1].push_back(x);     }     sort((v[1]).begin(), (v[1]).end());     for (long long i = 1; i <= n3; ++i) {       long long x;       cin >> x;       v[2].push_back(x);     }     sort((v[2]).begin(), (v[2]).end());     vector<long long> ids;     for (long long i = 0; i < 3; ++i) {       ids.push_back(i);     }     do {       for (long long i = 0; i < (long long)(v[ids[0]].size()); ++i) {         long long x = v[ids[0]][i];         auto z = lower_bound((v[ids[2]]).begin(), (v[ids[2]]).end(), x);         if (z == v[ids[2]].end()) continue;         auto y =             lower_bound((v[ids[1]]).begin(), (v[ids[1]]).end(), (x + *z) / 2);         if (y != v[ids[1]].end()) upd(mn, x, *y, *z);         if (y != v[ids[1]].begin()) {           auto y2 =               --lower_bound((v[ids[1]]).begin(), (v[ids[1]]).end(), (x + *z) / 2);           upd(mn, x, *y2, *z);         }       }     } while (next_permutation((ids).begin(), (ids).end()));     cout << mn <<   n ;   }   signed main() {     ios_base ::sync_with_stdio(false);     cin.tie(0);     long long test;     cin >> test;     for (long long tt = 1; tt <= test; ++tt) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   int chkmax(T &a, T b) {     if (b > a) {       a = b;       return 1;     }     return 0;   }   template <class T>   int chkmin(T &a, T b) {     if (b < a) {       a = b;       return 1;     }     return 0;   }   template <class iterator>   void output(iterator begin, iterator end, ostream &out = cerr) {     while (begin != end) {       out << (*begin) <<    ;       begin++;     }     out << endl;   }   template <class T>   void output(T x, ostream &out = cerr) {     output(x.begin(), x.end(), out);   }   void fast_io() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   const int MOD = 998244353;   int mul(int a, int b) { return (1LL * a * b) % MOD; }   int power(int a, int deg) {     int res = 1;     for (; deg; a = mul(a, a), deg >>= 1) {       if (deg & 1) {         res = mul(res, a);       }     }     return res;   }   int inverse(int x) { return power(x, MOD - 2); }   void add(int &a, int b) {     a += b;     if (a >= MOD) {       a -= MOD;     }   }   const int mx = 2e5 + 228;   int n, fact[mx];   long long k;   void calc_fact() {     fact[0] = 1;     for (int i = 1; i < mx; ++i) {       fact[i] = mul(fact[i - 1], i);     }   }   int C(int n, int k) {     return (k > n ? 0 : mul(fact[n], inverse(mul(fact[k], fact[n - k]))));   }   signed main() {     fast_io();     calc_fact();     cin >> n >> k;     if (k >= n) {       cout << 0 << endl;       exit(0);     }     int x = n - k;     int ans = 0;     for (int i = 0; i <= x; ++i) {       int sign = (i % 2 ? MOD - 1 : 1);       add(ans, mul(sign, mul(C(x, i), power(x - i, n))));     }     ans = mul(ans, C(n, x));     if (k) {       ans = mul(ans, 2);     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 1e5 + 5;   const long long mod = 1e9 + 7;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     string str;     cin >> str;     long long n = str.length();     vector<pair<long long, long long> > arr;     arr.push_back(make_pair(1, str[0] -  a ));     for (long long i = 1; i < n; i++) {       if (str[i] == str[i - 1])         arr[arr.size() - 1].first++;       else         arr.push_back(make_pair(1, str[i] -  a ));     }     long long ans = 0;     while (arr.size() > 1) {       ans++;       vector<pair<long long, long long> > tmp;       for (long long i = 0; i < arr.size(); i++) {         if (i == 0 || i == arr.size() - 1)           arr[i].first -= 1;         else           arr[i].first -= 2;         if (tmp.size() == 0 || arr[i].second != tmp[tmp.size() - 1].second) {           if (arr[i].first > 0) tmp.push_back(arr[i]);         } else if (arr[i].first > 0)           tmp[tmp.size() - 1].first += arr[i].first;       }       arr = tmp;     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   class Graph {    public:     vector<vector<int> > adjList;     vector<int> indegree;     Graph(int v) {       adjList.resize(v);       indegree.resize(v, 0);     }     void add(int u, int v) {       adjList[u].push_back(v);       indegree[v]++;     }   };   struct subset {     int rank;     int parent;   };   int find(subset subsets[], int i) {     if (subsets[i].parent != i)       subsets[i].parent = find(subsets, subsets[i].parent);     return subsets[i].parent;   }   void Union(subset subsets[], int x, int y) {     int xroot = find(subsets, x);     int yroot = find(subsets, y);     if (subsets[xroot].rank > subsets[yroot].rank) {       subsets[yroot].parent = xroot;     } else if (subsets[xroot].rank < subsets[yroot].rank) {       subsets[xroot].parent = yroot;     } else {       subsets[yroot].parent = xroot;       subsets[xroot].rank++;     }   }   bool TopologicalSort(Graph const &graph, vector<int> &ans, int v,                        subset subsets[]) {     vector<int> indegree = graph.indegree;     queue<int> S;     for (int i = 0; i < v; i++) {       if (!indegree[i]) {         S.push(i);         ans[find(subsets, i)] = 1;       }     }     while (!S.empty()) {       int n = S.front();       S.pop();       for (int m : graph.adjList[n]) {         indegree[m] -= 1;         ans[find(subsets, m)] = ans[find(subsets, n)] + 1;         if (!indegree[m]) {           S.push(m);         }       }     }     for (int i = 0; i < v; i++) {       if (indegree[i]) {         return false;       }     }     return true;   }   int main() {     int n, m;     cin >> n >> m;     subset *subsets = new subset[n + m];     for (int v = 0; v < n + m; v++) {       subsets[v].parent = v;       subsets[v].rank = 0;     }     Graph graph(n + m);     char input[n][m];     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         cin >> input[i][j];         if (input[i][j] ==  = ) {           Union(subsets, i, j + n);         }       }     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         int x = find(subsets, i);         int y = find(subsets, j + n);         if (input[i][j] ==  < ) {           graph.add(x, y);         } else if (input[i][j] ==  > ) {           graph.add(y, x);         }       }     }     vector<int> ans(n + m);     if (TopologicalSort(graph, ans, n + m, subsets)) {       cout <<  YES  << endl;       for (int i = 0; i < n; i++) {         cout << ans[find(subsets, i)] <<    ;       }       cout << endl;       for (int i = n; i < m + n; i++) {         cout << ans[find(subsets, i)] <<    ;       }       cout << endl;     } else {       cout <<  NO  << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     char trash[100];     int n;     cin >> n;     cin.getline(trash, 10);     for (int i = 0; i < n; ++i) {       string s;       getline(cin, s);       bool fredas = (s.find( lala. , s.length() - 5) == s.length() - 5);       bool rainbows = (s.find( miao. ) == 0);       if (fredas ^ rainbows) {         cout << (fredas ?  Freda s  :  Rainbow s );       } else {         cout <<  OMG>.< I don t know! ;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int SET(int N, int pos) { return N = N | (1 << pos); }   int RESET(int N, int pos) { return N = N & ~(1 << pos); }   bool CHECK(int N, int pos) { return (bool)(N & (1 << pos)); }   template <typename T>   inline T __lcm(T a, T b) {     return (a * b) / __gcd(a, b);   }   struct node {     long long x, y, cnt;   };   int dx[] = {0, 1, 0, -1, -1, 1, -1, 1};   int dy[] = {1, 0, -1, 0, 1, 1, -1, -1};   long long a[1000000];   long long b[1000000];   long long c[1000000];   void solve() {     long long n;     cin >> n;     if (n % 2 == 0) {       cout << -1 << endl;       return;     }     for (long long i = 0; i < n; i++) {       c[i] = n - i - 1;     }     for (long long i = 0; i < n; i++) {       a[i] = i;     }     for (long long i = 0; i < n; i++) {       b[i] = c[i] - a[i];       if (b[i] < 0) {         b[i] += n;       }     }     for (long long i = 0; i < n; i++) {       cout << a[i] <<    ;     }     cout << endl;     for (long long i = 0; i < n; i++) {       cout << b[i] <<    ;     }     cout << endl;     for (long long i = 0; i < n; i++) {       cout << c[i] <<    ;     }     cout << endl;   }   int main() {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double long EPS = 1e-7;   vector<vector<long long>> precost(long long n, vector<long long> &p,                                     vector<long long> &id) {     vector<vector<long long>> res(n, vector<long long>(n));     for (int i = 0; i < n; i++) {       vector<long long> deg(p.size());       for (int j = 1; j < p.size(); j++) {         deg[p[j]]++;       }       long long val = 0;       for (int j = i; j < n; j++) {         long long cur = id[j];         while (cur != 0 && deg[cur] == 0) {           val++;           deg[p[cur]]--;           cur = p[cur];         }         res[i][j] = val;       }     }     return res;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n;     cin >> n;     vector<vector<long long>> adj(2);     vector<vector<long long>> id(2, vector<long long>(n));     for (int i = 0; i < 2; i++) {       long long a;       cin >> a;       adj[i].resize(a);       for (int j = 1; j <= a - 1; j++) {         cin >> adj[i][j];         adj[i][j]--;       }       for (int j = 0; j < n; j++) {         cin >> id[i][j];         id[i][j]--;       }     }     vector<vector<vector<long long>>> cost(2);     for (int i = 0; i < 2; i++) {       cost[i] = precost(n, adj[i], id[i]);     }     vector<long long> dp(n + 1);     for (int i = 0; i < n; i++) {       for (int j = i + 1; j <= n; j++) {         dp[j] = max(dp[j], dp[i] + max(cost[0][i][j - 1], cost[1][i][j - 1]));       }     }     cout << dp[n];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 5;   long long mpow(long long x, long long y) {     long long res = 1;     x = x % 1000000007;     while (y) {       if (y & 1) {         res *= x;         res = res % 1000000007;       }       x *= x;       x %= 1000000007;       y >>= 1;     }     return res;   }   bool isprime(long long n) {     for (long long i = 2; i * i <= n; i++) {       if (n % i == 0) return false;     }     return true;   }   long long gcd(long long a, long long b) {     if (b == 0)       return a;     else       return gcd(b, a % b);   }   void solves_1() {     long long n;     cin >> n;     vector<long long> v(n);     for (int i = 0; i < n; i++) cin >> v[i];     sort(v.rbegin(), v.rend());     set<long long> st;     long long tmp = 0;     for (int i = 0; i < n; i++) {       if (st.find(v[i]) != st.end()) continue;       st.insert(v[i]);       while (v[i] % 2 == 0) {         v[i] /= 2;         tmp++;         st.insert(v[i]);       }     }     cout << tmp << endl;     return;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     ;     long long t;     cin >> t;     while (t--) solves_1();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long mod(long long n, long long m) {     long long ret = n % m;     if (ret < 0) ret += m;     return ret;   }   long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); }   long long exp(long long a, long long b, long long m) {     if (b == 0) return 1;     if (b == 1) return mod(a, m);     long long k = mod(exp(a, b / 2, m), m);     if (b & 1) {       return mod(a * mod(k * k, m), m);     } else       return mod(k * k, m);   }   map<vector<long long>, long long> mp;   long long a[500100];   vector<long long> adj[500100];   void solve() {     mp.clear();     long long n, m;     cin >> n >> m;     for (long long i = 1; i <= n; i++) {       adj[i].clear();       cin >> a[i];     }     for (long long i = 0; i < m; i++) {       long long a, b;       cin >> a >> b;       adj[b].push_back(a);     }     for (long long i = 1; i <= n; i++) {       sort(adj[i].begin(), adj[i].end());       if (adj[i].size()) {         mp[adj[i]] += a[i];       }     }     long long g = -1;     for (map<vector<long long>, long long>::iterator it = mp.begin();          it != mp.end(); it++) {       if (g == -1)         g = it->second;       else         g = gcd(g, it->second);     }     if (g == -1) g = 0;     cout << g <<   n ;   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long t;     cin >> t;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   int n;   int main() {     cin >> n;     n -= 10;     if (n <= 0)       cout << 0 << endl;     else if (n <= 9)       cout << 4 << endl;     else if (n == 10)       cout << 15 << endl;     else if (n == 11)       cout << 4 << endl;     else       cout << 0 << endl;   }


#include <bits/stdc++.h>   using namespace std;   int p[100005], cnt, h[100005], cnth, l[100005], cntl, cntquery;   int query(int x, int y) {     printf( 0 %d %d n , x, y);     fflush(stdout);     int ret;     scanf( %d , &ret);     cntquery++;     if (cntquery > 300000)       while (1)         ;     return ret;   }   int main() {     int t = 0, q, X;     for (int x = -100000000; x <= 100000000;) {       q = query(x, x);       if (q == 0) {         p[++cnt] = x;         x++;       } else if (q < 100 && x + 100 * 2 <= 100000000 &&                  query(x + 100, x + 100) >= 100) {         X = x;         x += 100 * 2;       } else {         X = x;         x += q;       }     }     for (int i = 1; i <= cnt; i++) {       q = query(p[i], X);       if (q == 0) l[++cntl] = p[i];       q = query(X, p[i]);       if (q == 0) h[++cnth] = p[i];     }     printf( 1 %d %d n , cntl, cnth);     for (int i = 1; i <= cntl; i++) printf( %d  , l[i]);     puts(  );     for (int i = 1; i <= cnth; i++) printf( %d  , h[i]);     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 150001;   struct BIT {     int bit[maxn];     BIT() { memset(bit, 0, sizeof(bit)); }     void add(int x, int v) {       for (x++; x < maxn; x += x & -x) bit[x] += v;     }     void add(int a, int b, int v) { add(a, 1), add(b + 1, -1); }     int qry(int x) {       int ret = 0;       for (x++; x; x -= x & -x) ret += bit[x];       return ret;     }   };   struct segTree {     int l, r;     segTree *left = 0, *right = 0;     int val = 0;     segTree(int a, int b) : l(a), r(b){};     void add(int x, int v) {       if (l == r) {         val += v;         return;       }       int mid = (l + r) / 2;       if (x <= mid) {         if (!left) left = new segTree(l, mid);         left->add(x, v);       } else {         if (!right) right = new segTree(mid + 1, r);         right->add(x, v);       }       val = (left ? left->val : 0) + (right ? right->val : 0);     }     void mrg(segTree *tre) {       if (!tre) return;       if (!left)         left = tre->left;       else if (tre->left)         left->mrg(tre->left);       if (!right)         right = tre->right;       else if (tre->right)         right->mrg(tre->right);       val += tre->val;       delete tre;     }     int qry(int a, int b) {       if (a <= l && r <= b) return val;       int ret = 0, mid = (l + r) / 2;       if (a <= mid && b >= l && left) ret += left->qry(a, b);       if (b > mid && a <= r && right) ret += right->qry(a, b);       return ret;     }     void clear() {       if (left) left->clear();       if (right) right->clear();       delete this;     }   };   const int w = 18;   int n, m, k;   int q[maxn][2];   int p[w][maxn];   int d[maxn], sz[maxn], h[maxn], l[maxn], r[maxn];   vector<int> graph[maxn], v[maxn], v2[maxn];   BIT bit;   segTree *tre[maxn];   int dfsh(int c) {     sz[c] = 1, h[c] = -1;     for (int i = 1; i < w; i++) {       p[i][c] = ~p[i - 1][c] ? p[i - 1][p[i - 1][c]] : -1;     }     for (int i : graph[c]) {       if (i == p[0][c]) continue;       p[0][i] = c;       d[i] = d[c] + 1;       sz[c] += dfsh(i);       if (!~h[c] || sz[i] > sz[h[c]]) h[c] = i;     }     return sz[c];   }   int dfsh2(int c) {     r[c] = l[c];     for (int i : graph[c]) {       if (i == p[0][c] || i == h[c]) continue;       l[i] = r[c] + 1;       r[c] = dfsh2(i);     }     if (~h[c]) {       l[h[c]] = r[c] + 1;       r[c] = dfsh2(h[c]);     }     return r[c];   }   int lft(int c, int x) {     for (int i = 0; i < w; i++) {       if (((x >> i) & 1) && ~c) c = p[i][c];     }     return c;   }   int lca(int x, int y) {     if (d[x] < d[y]) swap(x, y);     x = lft(x, d[x] - d[y]);     for (int i = w - 1; ~i; i--) {       if (p[i][x] != p[i][y]) x = p[i][x], y = p[i][y];     }     return x == y ? x : p[0][x];   }   long long dfs2(int c, int rt) {     long long ret = 0;     tre[c] = new segTree(0, n - 1);     for (int i : v2[c]) {       int dd = max(0, k + d[rt] - d[q[i][0]]);       int j = q[i][1];       if (d[j] - d[rt] >= dd) {         j = lft(j, d[j] - d[rt] - dd);         ret += tre[c]->qry(l[j], r[j]);       }       tre[c]->add(l[q[i][1]], 1);     }     for (int i : graph[c]) {       if (i == p[0][c] || (c == rt && i == h[c])) continue;       ret += dfs2(i, rt);       if (v2[c].size() < v2[i].size()) {         swap(v2[c], v2[i]);         swap(tre[c], tre[i]);       }       for (int it : v2[i]) {         int dd = max(0, k + d[rt] - d[c]);         int j = q[it][1];         if (d[j] - d[rt] >= dd) {           j = lft(j, d[j] - d[rt] - dd);           ret += tre[c]->qry(l[j], r[j]);         }         v2[c].push_back(it);       }       v2[i].clear();       tre[c]->mrg(tre[i]);     }     return ret;   }   long long dfs(int c) {     long long ret = 0;     for (int i : graph[c])       if (i != p[0][c]) ret += dfs(i);     for (int t = 0; t < 2; t++) {       for (int i : v[c]) ret += bit.qry(l[q[i][t]]);       for (int i : v[c]) {         int j = q[i][t];         if (d[j] - d[c] >= k) {           j = lft(j, d[j] - d[c] - k);           bit.add(l[j], r[j], 1);         }         if (!t) v2[q[i][t]].push_back(i);       }     }     ret += dfs2(c, c);     v2[c].clear();     tre[c]->clear();     return ret;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n >> m >> k;     for (int i = 0; i < n - 1; i++) {       int u, v;       cin >> u >> v;       u--, v--;       graph[u].push_back(v);       graph[v].push_back(u);     }     p[0][0] = -1;     dfsh(0), dfsh2(0);     for (int i = 0; i < m; i++) {       cin >> q[i][0] >> q[i][1];       q[i][0]--, q[i][1]--;       if (l[q[i][0]] > l[q[i][1]]) swap(q[i][0], q[i][1]);       v[lca(q[i][0], q[i][1])].push_back(i);     }     cout << dfs(0) <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int mod(int val) { return val > 0 ? val : -val; }   int main() {     int g[2], b[2];     cin >> g[0] >> g[1] >> b[0] >> b[1];     bool pass = false;     if (((g[1] + 1) * 2 >= b[0]) && (b[0] - g[1]) > -2) {       pass = true;     }     if ((g[0] + 1) * 2 >= b[1] && (b[1] - g[0]) > -2) {       pass = true;     }     if (pass) {       cout <<  YES  << endl;     } else {       cout <<  NO  << endl;     }   }


#include <bits/stdc++.h>   int main() {     int i, n, k;     scanf( %d %d , &n, &k);     int a[n];     int count = 0, j = 0;     for (i = 0; i < n; i++) {       scanf( %d , &a[i]);     }     for (i = 0; i < n; i++) {       if (a[i] <= k)         count++;       else {         j = 1;         break;       }     }     if (j != 0) {       for (i = n - 1; i >= 0; i--) {         if (a[i] <= k)           count++;         else           break;       }     }     printf( %d n , count);     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize(3)   using namespace std;   int n, m, a[1005][1005];   vector<int> row[1005];   vector<int> col[1005];   int ans[1005][1005];   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) {         scanf( %d , &a[i][j]);         row[i].push_back(a[i][j]);         col[j].push_back(a[i][j]);       }     for (int i = 1; i <= n; i++) {       sort(row[i].begin(), row[i].end());       row[i].erase(unique(row[i].begin(), row[i].end()), row[i].end());     }     for (int i = 1; i <= m; i++) {       sort(col[i].begin(), col[i].end());       col[i].erase(unique(col[i].begin(), col[i].end()), col[i].end());     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         int l1 =             lower_bound(row[i].begin(), row[i].end(), a[i][j]) - row[i].begin();         int l2 =             lower_bound(col[j].begin(), col[j].end(), a[i][j]) - col[j].begin();         int r1 =             row[i].end() - upper_bound(row[i].begin(), row[i].end(), a[i][j]);         int r2 =             col[j].end() - upper_bound(col[j].begin(), col[j].end(), a[i][j]);         printf( %d  , max(l1, l2) + 1 + max(r1, r2));       }       puts(  );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   #define INF 200000000000000000LL   typedef long long int ll;   typedef pair<ll,ll> ii;   typedef pair<ii,ll> iii;   typedef vector<ll> vi;   typedef vector<ii> vii;   #define getbit(n,i) (((n)&(1LL<<(i)))!=0)   #define setbit0(n,i) ((n)&(~(1LL<<(i))))   #define setbit1(n,i) ((n)|(1LL<<(i)))   #define lastone(n) ((n)&(-(n)))   #define read freopen( debug//in.txt , r ,stdin)   #define write freopen( debug//out.txt , w ,stdout)   #define DBG(a) cerr<<#a<<  ->->->->  <<a<<  n    #define fi first   #define se second   #define PI (acos(-1))   #define fastread ios::sync_with_stdio(false);cin.tie(NULL)   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   #define mod (1000000007)   #define asz 500005   template<class T,class V> ostream& operator<<(ostream &s,pair<T,V> a)   {       s<<a.fi<<   <<a.se;       return s;   }   const int N = 1e3+5;   int main()   {       fastread;   #ifdef FILE_IO       read;       write;   #endif          int T=1;       cin>>T;       for(int qq=1; qq<=T; qq++)       {           int n;           string s;           cin>>n>>s;           vi l(n+2),r(n+2);           r[n] = 1;           r[n-1] = 1 + (s.back() ==  R );           l[0] = 1;           l[1] = 1 + (s[0] ==  L );           for(int i=2;i<=n;i++){               if(s[i-1] ==  L ){                   if(s[i-2] ==  R )l[i] = 2+l[i-2];                   else l[i] = 2;               }               else l[i] = 1;                  if(s[n-i] ==  R ){                   if(s[n-i+1] ==  L )r[n-i] = 2+r[n-i+2];                   else r[n-i] = 2;               }               else r[n-i] = 1;           }              for(int i=0;i<=n;i++)cout<<l[i]+r[i]-1<<   ;           cout<<endl;       }   }


#include <bits/stdc++.h>   using namespace std;   long long mod = 1000000007;   long long qpow(long long x, long long tms) {     long long tmp = 1;     while (tms) {       if (tms & 1) tmp = tmp * x % mod;       tms >>= 1;       x = x * x % mod;     }     return tmp;   }   int n, m, T;   bool chk(int k) {     if (n == 1) return 1;     long long sum = 1, t = (long long)k * m;     double C = 1;     long long s = 1;     for (int i = 1; i <= k; ++i) {       long long x = t / i, y = mod;       if (x == 0) return 0;       C = C * (k - i + 1) / i;       s = s * (k - i + 1) % mod * qpow(i, mod - 2) % mod;       if (C < mod) y = s;       x = min(x, y);       sum += x;       if (sum >= n) return 1;       t -= x * i;     }     return 0;   }   int main() {     scanf( %d , &T);     while (T--) {       scanf( %d%d , &n, &m);       int l = 0, r = n;       while (l < r) {         int mid = (l + r) >> 1;         if (chk(mid))           r = mid;         else           l = mid + 1;       }       printf( %d n , l);     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     scanf( %d , &t);     for (int i = 0; i < t; i++) {       int cont = 0;       int tamano;       scanf( %d , &tamano);       string x;       cin >> x;       for (int j = 0; j < tamano / 2; j++) {         if (x[j] == x[tamano - j - 1]) {           cont++;         } else if ((x[j] + 1 == x[tamano - j - 1] + 1) ||                    (x[j] + 1 == x[tamano - j - 1] - 1) ||                    (x[j] - 1 == x[tamano - j - 1] - 1) ||                    (x[j] - 1 == x[tamano - j - 1] + 1)) {           cont++;         } else {           printf( NO n );           break;         }       }       if (cont == tamano / 2) {         printf( YES n );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, a[109], d;   void nhap() {     scanf( %d n , &n);     for (int i = 1; i <= n; i++) scanf( %d  , &a[i]);   }   void qs(int l, int r) {     int x = a[(l + r) / 2];     int i = l;     int j = r;     while (i <= j) {       while (a[i] < x) i++;       while (a[j] > x) j--;       if (i <= j) {         int tmp = a[i];         a[i] = a[j];         a[j] = tmp;         i++;         j--;       }     }     if (i < r) qs(i, r);     if (j > l) qs(l, j);   }   void xuli() {     qs(1, n);     for (int i = 2; i <= n; i += 2) {       int s = a[i] - a[i - 1];       if (s != 0) d = d + s;     }     printf( %d , d);   }   int main() {     nhap();     xuli();   }


#include <bits/stdc++.h>   int max(int x, int y) { return (x > y ? x : y); }   int geGcd(int a, int b) {     if (a == 0 || b == 0) return 0;     for (int i = max(a, b); i >= 1; i--)       if (a % i == 0 && b % i == 0) return i;   }   using namespace std;   int main() {     int a, b, n;     cin >> a >> b >> n;     int cnt = 0;     while (n > 0) {       if (geGcd(a, n)) {         n -= geGcd(a, n);         cnt++;       }       if (geGcd(b, n)) {         n -= geGcd(b, n);         cnt++;       }     }     if (cnt % 2 == 0)       cout <<  1 ;     else       cout <<  0 ;   }


#include <bits/stdc++.h>   using namespace std;   long long lv[100001], lf[100001], par[100001], n;   vector<long long> con[100001], cld[100001];   long long get_leaf(long long id) {     if (cld[id].size() == 0) {       lf[1]++;       return 1;     }     long long x = 0, i;     for (i = 0; i <= cld[id].size() - 1; i++) {       x += get_leaf(cld[id][i]);     }     lf[x]++;     return x;   }   int main() {     ios_base::sync_with_stdio(0);     long long int i, j, x, y, m, cnt = 0, k, g, flg, t, l, r, md;     cin >> n;     for (i = 2; i <= n; i++) {       cin >> x;       con[x].push_back(i);       con[i].push_back(x);     }     queue<long long> q;     q.push(1);     while (!q.empty()) {       x = q.front();       q.pop();       if (con[x].size() == 0) continue;       for (i = 0; i <= con[x].size() - 1; i++) {         y = con[x][i];         if (par[y] || y == 1) continue;         par[y] = x;         cld[x].push_back(y);         q.push(y);       }     }     x = get_leaf(1);     for (i = 2; i <= n; i++) {       lf[i] += lf[i - 1];     }     for (i = 1; i <= n; i++) {       l = 0;       r = n;       while (r - l > 1) {         md = (l + r) / 2;         if (lf[md] < i)           l = md;         else           r = md;       }       cout << r <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     string s;     cin >> s;     int c = 0, f = 0;     for (int i = 0; i < n; i++) {       if (s[i] ==  x )         c++;       else if (s[i] !=  x  && c < 3)         c = 0;       if (c > 2) {         while (1) {           f++;           c--;           if (c < 3) break;         }       }     }     cout << f;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int k, d;     cin >> k >> d;     if (d == 9) {       cout << 9;       for (int i = 1; i < k; i++) cout << 0;     } else if (d == 0 && k > 1) {       cout <<  No solution ;     } else {       cout << d;       for (int i = 1; i < k; i++) cout << 9;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) {     if (!b) return a;     return gcd(b, a % b);   }   long long lcm(long long a, long long b) { return b / gcd(a, b) * a; }   int n, g[5001];   int s;   int l[5000][5001];   int best = 1 << 30, freq;   int SWAP(int a, int b) {     int B = g[b];     int A = g[a];     int res = s;     res -= (b - a - 2) - (l[B][b] - l[B][a]);     res += (l[B][b] - l[B][a]);     res -= (l[A][b] - l[A][a]);     res += (b - a - 2) - (l[A][b] - l[A][a]);     return res;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) {       scanf( %d , g + i);       for (int j = i - 1; j; --j) s += g[i] < g[j];     }     for (int(i) = 0; i < int(n); ++i) {       l[i][1] = 0;       for (int j = 2; j <= n; ++j) l[i][j] = l[i][j - 1] + (i > g[j - 1]);     }     for (int i = 1; i <= n; ++i) {       for (int j = i + 1; j <= n; ++j) {         int cost = SWAP(i, j);         if (g[i] < g[j])           ++cost;         else           --cost;         if (cost == best)           ++freq;         else if (cost < best) {           best = cost;           freq = 1;         }       }     }     cout << best <<     << freq << endl;   }


#include <bits/stdc++.h>   using namespace std;   int ucln(int a, int b) {     int r;     while (b > 0) {       r = a % b;       a = b;       b = r;     }     return a;   }   int main() {     int x, y, a, b, bc;     cin >> x >> y >> a >> b;     bc = x * y / ucln(x, y);     int l = a, r = b;     while (l % bc != 0) l++;     while (r % bc != 0) r--;     cout << (r - l) / bc + 1;   }


#include <bits/stdc++.h>   using namespace std;   struct point {     long long x, y, index;     point(long long x, long long y, long long index) : x(x), y(y), index(index) {}     long long operator*(const point p) { return x * p.x + y * p.y; }     long long operator^(const point p) { return x * p.y - y * p.x; }     bool operator<(const point p) const {       return make_pair(x, y) < make_pair(p.x, p.y);     }     bool operator==(const point p) const { return x == p.x && y == p.y; }     bool operator!=(const point p) const { return !(*this == p); }     point operator*(long long scale) {       return point(scale * x, scale * y, index);     }     point operator+(const point p) { return point(x + p.x, y + p.y, index); }     point operator-(const point p) { return point(x - p.x, y - p.y, index); }     long long norm() { return (*this) * (*this); }   };   bool half(point p) {     assert(p.x != 0 || p.y != 0);     return p.x > 0 || (p.x == 0 && p.y > 0);   }   void polarSort(point o, vector<point> &v, int L, int R) {     sort(v.begin() + L, v.begin() + R, [&](point v, point w) {       return make_tuple(half(v - o), 0, (v - o).norm()) <              make_tuple(half(w - o), ((v - o) ^ (w - o)), (w - o).norm());     });   }   const int N = 1515;   int n;   int Ans[N];   int Sub[N];   vector<point> pts;   vector<int> Tree[N];   int DFS(int u, int p) {     Sub[u] = 1;     for (auto v : Tree[u])       if (v != p) Sub[u] += DFS(v, u);     return Sub[u];   }   void PaintTree(int u, int p, int L, int R) {     int idx = L;     for (int i = L; i <= R; i++)       if (pts[idx].y < pts[i].y) idx = i;     swap(pts[L], pts[idx]);     Ans[pts[L].index] = u;     polarSort(pts[L], pts, L + 1, R + 1);     int pre = 0;     for (auto v : Tree[u])       if (v != p) {         PaintTree(v, u, L + pre + 1, L + pre + Sub[v]);         pre += Sub[v];       }   }   int main() {     scanf( %d , &n);     for (int i = 1; i < n; i++) {       int u, v;       scanf( %d%d , &u, &v);       Tree[u].push_back(v);       Tree[v].push_back(u);     }     for (int i = 0; i < n; i++) {       int x, y;       scanf( %d%d , &x, &y);       pts.push_back(point(x, y, i + 1));     }     DFS(1, 0);     PaintTree(1, 0, 0, n - 1);     for (int i = 1; i <= n; i++) printf( %d  , Ans[i]);     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using u64 = unsigned long long;   const int mod = 1e9 + 7, N = 2e3 + 10;   char mp[N][N];   int f[N][N];   bool st[N][N];   unordered_map<char, pair<int, int>> mv = {       { L , {0, -1}}, { R , {0, 1}}, { U , {-1, 0}}, { D , {1, 0}}};   int main(void) {     int t;     cin >> t;     while (t--) {       int n, m, tx, ty;       cin >> n >> m;       for (int i = 1; i <= n; i++) scanf( %s , &mp[i][1]);       stack<pair<int, int>> q;       for (int i = 0; i <= n + 1; i++)         for (int j = 0; j <= m + 1; j++) f[i][j] = 1;       for (int i = 1; i <= n; i++) {         for (int j = 1; j <= m; j++) {           int x = i, y = j;           bool ring = false;           if (f[x][y] != 1) continue;           do {             q.push({x, y});             st[x][y] = true;             tx = x + mv[mp[x][y]].first, ty = y + mv[mp[x][y]].second;             x = tx, y = ty;             if (x < 1 || x > n || y < 1 || y > m) break;             if (st[x][y]) {               ring = true;               break;             }             if (f[x][y] != 1) {               q.push({x, y});               break;             }           } while (true);           pair<int, int> xy;           int step;           if (ring) {             stack<pair<int, int>> rq;             do {               xy = q.top();               q.pop();               st[xy.first][xy.second] = false;               rq.push(xy);               if (xy.first == x && xy.second == y) break;             } while (true);             step = rq.size();             while (rq.size()) {               xy = rq.top();               rq.pop();               f[xy.first][xy.second] = step;             }             step++;             while (q.size()) {               xy = q.top();               q.pop();               st[xy.first][xy.second] = false;               f[xy.first][xy.second] = step++;             }             continue;           }           step = f[q.top().first][q.top().second];           while (q.size()) {             xy = q.top();             q.pop();             st[xy.first][xy.second] = false;             f[xy.first][xy.second] = step++;           }         }       }       int res = -1, x, y;       for (int i = 1; i <= n; i++) {         for (int j = 1; j <= m; j++) {           if (res < f[i][j]) {             x = i, y = j;             res = f[i][j];           }         }       }       cout << x <<     << y <<     << res << endl;     }     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:36777216 )   using namespace std;   const int INF = ~(1 << 31);   const double EPS = 1;   const double PI = 3.141592653589793;   int main() {     int n, m;     cin >> n >> m;     if (m == 3 && n >= 5) {       cout <<  -1 n ;       return 0;     }     for (int i = 0; i < m; i++) cout << i <<     << i * i + 10000000 <<   n ;     for (int i = 0; i < n - m; i++) cout << i <<     << -i * i - 10000000 <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int m, x, p[100005], h[100005], cnt;   long long n;   int ksm(int a, long long k) {     if (!k) return 1;     int p = ksm(a, k / 2);     if (k & 1) return (long long)p * p % 1000000007 * a % 1000000007;     return (long long)p * p % 1000000007;   }   int Getval(long long n, int x) {     int ret = 1;     while (n) {       n /= x;       ret = (long long)ret * ksm(x, n) % 1000000007;     }     return ret;   }   int main() {     scanf( %d%I64d , &x, &n);     int m = int(sqrt(x)) + 1, ans = 1;     for (int i = 2; i <= m; i++) {       if (x % i == 0) {         while (x % i == 0) x /= i;         ans = (long long)ans * Getval(n, i) % 1000000007;       }     }     if (x > 1) ans = (long long)ans * Getval(n, x) % 1000000007;     printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x0f0f0f0f;   template <class S, class T>   inline pair<S, T> MP(S a, T b) {     return make_pair(a, b);   }   template <class T>   inline auto LX(T &X) -> decltype((X.first)) {     return X.first;   }   template <class T>   inline auto LX(const T &X) -> const decltype((X.first)) {     return X.first;   }   template <class T>   inline auto RX(T &X) -> decltype((X.second)) {     return X.second;   }   template <class T>   inline auto RX(const T &X) -> const decltype((X.second)) {     return X.second;   }   template <class T>   inline void RST(T &a) {     memset(a, 0, sizeof(a));   }   template <class T>   inline void RST1(T &a) {     memset(a, -1, sizeof(a));   }   template <class T>   inline void SINF(T &a) {     memset(a, 0x0f, sizeof(a));   }   template <class S, class T>   inline void RST(S &a, T b) {     fill((T *)a, (T *)a + (sizeof(a) / sizeof(b)), b);   }   template <class T>   inline void CLR(T &a) {     a.clear();   }   template <class S, class T>   inline void CPY(S &a, T &b) {     memcpy(a, b, sizeof(a));   }   template <class S, class T>   inline bool chkmin(S &a, T b) {     return b < a ? a = b, 1 : 0;   }   template <class S, class T>   inline bool chkmax(S &a, T b) {     return a < b ? a = b, 1 : 0;   }   template <class T>   inline T sqr(T x) {     return x * x;   }   template <class T>   inline int SZ(T &a) {     return (int)(a.size());   }   template <class T>   inline bool EPT(T &a) {     return a.empty();   }   template <class T>   inline auto FRT(T &a) -> decltype((a.front())) {     return a.front();   }   template <class T>   inline auto FRT(const T &a) -> const decltype((a.front())) {     return a.front();   }   template <class T>   inline auto BK(T &a) -> decltype(a.back()) & {     return a.back();   }   template <class T>   inline auto BK(const T &a) -> const decltype((a.back())) {     return a.back();   }   template <class T>   inline auto TOP(T &a) -> decltype((a.top())) {     return a.top();   }   template <class T>   inline auto TOP(const T &a) -> const decltype((a.top())) {     return a.top();   }   int n, m, nn, k;   int a[310][310], b[310][310];   int r[200010], cr[200010], sz[200010];   vector<pair<int, int> > l[200010];   unordered_map<int, int> c;   int find(int x) { return x == r[x] ? x : r[x] = find(r[x]); }   int d[1010][1010];   deque<pair<int, int> > Q;   int ox, oy;   bool cmp(const pair<int, int> &a, const pair<int, int> &b) {     return d[LX(a) - ox + k][RX(a) - oy + k] < d[LX(b) - ox + k][RX(b) - oy + k];   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) {         int &x = a[i][j];         scanf( %d , &x);         if (x)           if (!c.count(x))             x = c[x] = ++nn;           else             x = c[x];         sz[x]++;       }     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) {         int &x = b[i][j];         scanf( %d , &x);         if (x != -1) {           if (x)             if (!c.count(x))               x = c[x] = ++nn;             else               x = c[x];           l[a[i][j]].push_back(MP(i, j));         }       }     for (int i = 0; i <= nn; i++) r[i] = cr[i] = i;     k = max(n, m);     int x = k, y = k, t = 0;     for (int i = 1; i < k; i++) {       d[--x][y] = ++t;       for (int j = 1; j < 2 * i; j++) d[x][++y] = ++t;       for (int j = 0; j < 2 * i; j++) d[++x][y] = ++t;       for (int j = 0; j < 2 * i; j++) d[x][--y] = ++t;       for (int j = 0; j < 2 * i; j++) d[--x][y] = ++t;     }     scanf( %d%d , &x, &y);     Q.push_back(MP(x, y));     long long ans = 0;     while (!EPT(Q)) {       int x = LX(FRT(Q)), y = RX(FRT(Q));       Q.pop_front();       int c1 = find(a[x][y]), c2 = find(b[x][y]);       if (!cr[c1] || cr[c1] == b[x][y]) continue;       ox = x, oy = y;       sort(l[c1].begin(), l[c1].end(), cmp);       for (pair<int, int> t : l[c1]) Q.push_back(t);       CLR(l[c1]);       ans += sz[c1];       if (c1 != c2) sz[c2] += sz[c1], r[c1] = c2;       cr[c2] = b[x][y];     }     printf( %I64d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int it = 0;   int find(int l, int r, int k) {     if (l == r) return l;     int ras = (r - l + 1) / 2;     int ll, rr;     if (k == 0) {       ll = l + ras - 1;       rr = l + ras;     } else {       ll = r - ras;       rr = ll + 1;     }     if (k == 0) {       assert(it != 60);       cout << 1 <<     << ll <<     << rr << endl;       ++it;     } else {       assert(it != 60);       cout << 1 <<     << rr <<     << ll << endl;       ++it;     }     string s;     cin >> s;     if (s[0] ==  T ) {       if (k == 0) {         return find(l, rr - 1, k);       } else {         return find(ll + 1, r, k);       }     } else {       if (k == 0) {         return find(ll + 1, r, k);       } else {         return find(l, rr - 1, k);       }     }   }   signed main() {     int n, k;     cin >> n >> k;     int cur = find(1, n, 0);     set<int> sss;     sss.insert(cur);     sss.insert(find(1, cur, 0));     sss.insert(find(cur, n, 1));     cout << 2 <<     << *sss.begin() <<     << *(++sss.begin()) << endl;     return 0;   }


#include <bits/stdc++.h>   unsigned long long int p[64][2][2], m, pi[64];   void mul_matrix(int size, unsigned long long int (*a)[2],                   unsigned long long int (*b)[2],                   unsigned long long int (*dest)[2]) {     int i, j, k;     for (i = 0; i < size; i++)       for (j = 0; j < 2; j++) {         dest[i][j] = 0;         for (k = 0; k < 2; k++)           dest[i][j] = (dest[i][j] + (a[i][k] * b[k][j]) % m) % m;       }   }   void init_mat() {     int i;     p[0][0][0] = p[0][0][1] = p[0][1][0] = 1;     p[0][1][1] = 0;     pi[0] = 2;     for (i = 1; i < 64; i++) {       mul_matrix(2, p[i - 1], p[i - 1], p[i]);       pi[i] = (pi[i - 1] * pi[i - 1]) % m;     }   }   int main() {     unsigned long long int n, k, t, zero = 0, one = 1, ans = 1, l, i;     unsigned long long int base1[1][2] = {0, 1}, base2[1][2] = {1, 0}, dest[1][2];     scanf( %lld %lld %lld %lld , &n, &k, &l, &m);     init_mat();     for (i = 0, t = n - 1; t; t >>= 1, i++) {       if (t & 1) {         mul_matrix(1, base1, p[i], dest);         memcpy(base1, dest, sizeof(dest));         mul_matrix(1, base2, p[i], dest);         memcpy(base2, dest, sizeof(dest));       }     }     for (i = 0, t = n; t; t >>= 1, i++)       if (t & 1) one = (one * pi[i]) % m;     zero =         ((base1[0][0] + base1[0][1]) % m + (base2[0][0] + base2[0][1]) % m) % m;     one = ((one + m) - zero) % m;     for (t = k, i = 0; i < l; t >>= 1, i++) {       if (t & 1)         ans = (ans * one) % m;       else         ans = (ans * zero) % m;     }     if (t) {       printf( 0 );       return 0;     }     printf( %u , ans % m);     return 0;   }


#include <bits/stdc++.h>   int inp() {     char c = getchar();     while (c <  0  || c >  9 ) c = getchar();     int sum = 0;     while (c >=  0  && c <=  9 ) {       sum = sum * 10 + c -  0 ;       c = getchar();     }     return sum;   }   char s[1000010], l[1000010], r[1000010];   long long f[1000010], sum[1000010];   int n, sl, sr, pl[1000010], pr[1000010], hshs[1000010], hshl[1000010],       hshr[1000010], fpow[1000010];   bool ok1(int x) {     if (x + sl - 1 > n) return false;     if (sl == pl[x]) return true;     return l[pl[x] + 1] < s[x + pl[x]];   }   bool ok2(int x) {     if (x + sr - 1 > n) return false;     if (pr[x] == sr) return true;     return r[pr[x] + 1] > s[x + pr[x]];   }   int get_hsh(int *hsh, int l, int r) {     return (hsh[r] -             ((long long)(hsh[l - 1]) * (long long)(fpow[r - l + 1]) % 998244353) +             998244353) %            998244353;   }   void solve(int *s, int *hsh, int len) {     for (int i = 1; i <= n - len + 1; i++) {       int l = 0;       int r = len;       while (l < r) {         int mid = (l + r + 1) >> 1;         if (get_hsh(hshs, i, i + mid - 1) == get_hsh(hsh, 1, mid))           l = mid;         else           r = mid - 1;       }       s[i] = l;     }   }   void gethash(char *str, int *hsh, int len) {     for (int i = 1; i <= len; i++)       hsh[i] = ((long long)(hsh[i - 1]) * (long long)(11) + str[i] -  0  + 1) %                998244353;   }   int main() {     fpow[0] = 1;     for (int i = 1; i <= 1000000; i++)       fpow[i] = ((long long)(fpow[i - 1]) * (long long)(11) % 998244353);     scanf( %s , s + 1);     scanf( %s , l + 1);     scanf( %s , r + 1);     n = strlen(s + 1);     sl = strlen(l + 1);     sr = strlen(r + 1);     gethash(l, hshl, sl);     gethash(r, hshr, sr);     gethash(s, hshs, n);     f[0] = 1;     solve(pl, hshl, sl);     solve(pr, hshr, sr);     for (int i = 0; i <= n; i++) {       if (i) {         sum[i] += sum[i - 1];         f[i] += sum[i];         f[i] %= 998244353;       }       if (s[i + 1] ==  0 ) {         if (sl == 1 && l[1] ==  0 ) {           f[i + 1] += f[i];           f[i + 1] %= 998244353;         }         continue;       }       if (sl < sr) {         sum[sl + i + 1] += f[i];         sum[sl + i + 1] %= 998244353;         sum[sr + i] += 998244353 - f[i];         sum[sr + i] %= 998244353;       }       if (sl == sr) {         if (ok1(i + 1) && ok2(i + 1)) {           f[i + sl] += f[i];           f[i + sl] %= 998244353;         }       } else {         if (ok1(i + 1)) {           f[i + sl] += f[i];           f[i + sl] %= 998244353;         }         if (ok2(i + 1)) {           f[i + sr] += f[i];           f[i + sr] %= 998244353;         }       }     }     std::cout << f[n] << std::endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int t;     t = 1;     while (t--) {       int n;       cin >> n;       long long arr[n], c4 = 0, c2 = 0;       long long i;       map<long long, long long> make_pair;       for (i = 0; i < n; i++) {         cin >> arr[i];         c4 -= make_pair[arr[i]] / 4;         c2 -= make_pair[arr[i]] / 2;         make_pair[arr[i]]++;         c4 += make_pair[arr[i]] / 4;         c2 += make_pair[arr[i]] / 2;       }       long long q;       cin >> q;       for (i = 0; i < q; i++) {         char c;         long long x;         cin >> c >> x;         c4 -= make_pair[x] / 4;         c2 -= make_pair[x] / 2;         if (c ==  + ) {           make_pair[x]++;         } else {           make_pair[x]--;         }         c4 += make_pair[x] / 4;         c2 += make_pair[x] / 2;         if (c4 >= 1 && c2 >= 4) {           cout <<  YES  << endl;         } else {           cout <<  NO  << endl;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 505;   int l[MAXN], r[MAXN], s[MAXN], w[MAXN], v[MAXN];   int dp[2 * MAXN][MAXN];   vector<int> wr[2 * MAXN];   int calc(int str, int i) {     if (dp[str][i] != -1) return dp[str][i];     int L = l[i], R = r[i];     vector<int> prf(2 * MAXN);     prf[L] = 0;     for (int x = L + 1; x <= R; x++) {       prf[x] = prf[x - 1];       for (auto j : wr[x]) {         if (l[j] == L && r[j] == R) continue;         if (l[j] < L || w[j] > str) continue;         prf[x] = max(prf[x], calc(min(str - w[j], s[j]), j) + prf[l[j]]);       }     }     return dp[str][i] = v[i] + prf[R];   }   int main() {     int n, S;     cin >> n >> S;     l[0] = 0;     r[0] = 2 * n;     s[0] = S;     w[0] = 0;     v[0] = 0;     for (int i = 1; i <= n; i++) {       cin >> l[i] >> r[i] >> w[i] >> s[i] >> v[i];       wr[r[i]].push_back(i);     }     for (int i = 0; i < 2 * MAXN; i++)       for (int j = 0; j < MAXN; j++) dp[i][j] = -1;     cout << calc(S, 0) << endl;   }


#include <bits/stdc++.h>   using namespace std;   void read(long long &x) {     x = 0;     long long f = 1;     char ch = getchar();     for (; !isdigit(ch); ch = getchar())       if (ch ==  - ) f = -f;     for (; isdigit(ch); ch = getchar()) x = x * 10 + ch -  0 ;     x *= f;   }   void print(long long x) {     if (x < 0) putchar( - ), x = -x;     if (!x) return;     print(x / 10), putchar(x % 10 + 48);   }   void write(long long x) {     if (!x)       putchar( 0 );     else       print(x);     putchar(  n );   }   const long long maxn = 2e5 + 10;   const long long inf = 1e9;   const long double eps = 1e-12;   const long long mod = 1e9 + 7;   long long add(long long x, long long y) {     return x + y >= mod ? x + y - mod : x + y;   }   long long del(long long x, long long y) {     return x - y < 0 ? x - y + mod : x - y;   }   long long mul(long long x, long long y) {     return 1ll * x * y - 1ll * x * y / mod * mod;   }   void inc(long long &x, long long y) {     x += y;     x %= mod;   }   long long n, k, a[maxn], res, c;   long long qpow(long long a, long long x) {     long long res = 1;     for (; x; x >>= 1, a = 1ll * a * a % mod)       if (x & 1) res = 1ll * res * a % mod;     return res;   }   struct Matrix {     long long a, b, r[110][110];     Matrix() { a = b = 0, memset(r, 0, sizeof r); }     Matrix operator*(const Matrix &t) const {       Matrix res;       res.a = a, res.b = t.b;       for (long long i = 0; i <= a; i++)         for (long long j = 0; j <= t.b; j++)           for (long long k = 0; k <= b; k++)             res.r[i][j] = add(res.r[i][j], mul(r[i][k], t.r[k][j]));       return res;     }   } tr, ans;   signed main() {     read(n), read(k);     for (long long i = 1; i <= n; i++) read(a[i]), res += !(a[i] & 1);     for (long long i = 1; i <= res; i++) c += !a[i];     ans.a = 0, ans.b = res;     ans.r[0][c] = 1;     tr.a = tr.b = res;     for (long long i = 0; i <= res; i++) {       long long lw = i, lb = res - i, rw = res - lw, rb = n - res - lb;       if (i != res) inc(tr.r[i][i + 1], lb * rw % mod);       if (i != 0) inc(tr.r[i][i - 1], lw * rb % mod);       inc(tr.r[i][i], (res * (res - 1) + (n - res) * (n - res - 1)) % mod *                           qpow(2, mod - 2) % mod);       inc(tr.r[i][i], (lw * lb + rw * rb) % mod);     }     long long x = k, f = 0;     for (; x; x >>= 1, tr = tr * tr)       if (x & 1) ans = ans * tr;     for (long long i = 0, i_r = res; i <= i_r; i++) f = (f + ans.r[0][i]) % mod;     write(ans.r[0][res] * qpow(f, mod - 2) % mod);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 998244353;   const long long INF = 1e17;   vector<vector<long long>> g;   vector<long long> used;   void dfs(long long v) {     used[v] = 1;     for (auto u : g[v]) {       if (!used[u]) {         dfs(u);       }     }   }   void solve() {     long long n, m;     cin >> n >> m;     long long q;     cin >> q;     long long N = n + m;     g.resize(N);     used.resize(N);     while (q--) {       long long x, y;       cin >> x >> y;       --x, --y;       x += m;       g[x].push_back(y);       g[y].push_back(x);     }     long long ans = -1;     for (long long i = 0; i < N; ++i) {       if (!used[i]) {         dfs(i);         ans++;       }     }     cout << ans;   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long tt = 1;     while (tt--) {       solve();       cout <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main(void) {     int t;     cin >> t;     for (int z = 0; z < t; ++z) {       int n;       string s;       cin >> n >> s;       string ans =   ;       for (int i = 0; i < n; ++i) {         if ((s[i] -  0 ) % 2 == 1) ans += s[i];         if (ans.size() == 2) break;       }       if (ans.size() < 2) {         cout <<  -1 n ;       } else {         cout << ans <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = (int)1e5 + 100;   vector<int> g[maxn], G[maxn];   int type[maxn];   int cntIn[maxn];   void no() {     printf( No );     exit(0);   }   void yes() {     printf( Yes );     exit(0);   }   void dfsG(int v, int par = -1) {     if ((int)G[v].size() > 2) no();     type[v] = 3;     for (int i = 0; i < (int)G[v].size(); i++) {       int nv = G[v][i];       if (nv == par) continue;       dfsG(nv, v);       break;     }   }   void solve() {     int n;     scanf( %d , &n);     if (n <= 2) yes();     for (int i = 0; i < n - 1; i++) {       int a, b;       scanf( %d%d , &a, &b);       a--;       b--;       g[a].push_back(b);       g[b].push_back(a);     }     bool fnd = false;     for (int i = 0; i < n; i++)       if ((int)g[i].size() >= 3) fnd = true;     if (!fnd) yes();     for (int i = 0; i < n; i++)       if ((int)g[i].size() == 1 && type[i] == 0) {         type[i] = 1;         int pv = i;         int v = g[i][0];         while ((int)g[v].size() == 2) {           type[v] = 1;           int nv = g[v][0] + g[v][1] - pv;           pv = v;           v = nv;         }         cntIn[v]++;       }     for (int i = 0; i < n; i++)       if ((int)g[i].size() == 3 && cntIn[i] == 2) {         type[i] = 2;       }     for (int i = 0; i < n; i++)       for (int j = 0; j < (int)g[i].size(); j++) {         int ni = g[i][j];         if (type[i] == 0 && type[ni] == 0) G[i].push_back(ni);       }     int st = -1;     for (int i = 0; i < n; i++)       if (type[i] == 0 && (int)G[i].size() <= 2) st = i;     if (st != -1) {       type[st] = 3;       for (int i = 0; i < (int)G[st].size(); i++) dfsG(G[st][i], st);     }     for (int i = 0; i < n; i++)       if (type[i] == 0) no();     yes();   }   int main(int, char **) {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 77;   int main() {     int a, b;     cin >> a >> b;     int ans = 0;     while (1) {       if (a <= 0 || b <= 0) break;       if (a < b) {         a++;         b -= 2;       } else {         b++;         a -= 2;       }       if (a < 0 || b < 0) break;       ++ans;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int arr[1000005];   int main() {     int n, q, a, b, x = 0, xPar = 0;     scanf( %d %d , &n, &q);     while (q--) {       scanf( %d , &a);       if (a == 1) {         scanf( %d , &b);         x += b;         if (x >= n) {           x = x % n;         } else if (x < 0) {           x = n - ((x * -1) % n);         }       } else {         if ((x + xPar) % 2 == 1) {           xPar = ((xPar + 1) % n);         } else {           if (--xPar < 0) {             xPar += n;           }         }       }     }     for (int i = 1; i <= n; i++) {       int nTemp = i + x;       if (i % 2) {         nTemp -= xPar;       } else {         nTemp += xPar;       }       while (nTemp <= 0) {         nTemp += n;       }       while (nTemp > n) {         nTemp -= n;       }       arr[nTemp] = i;     }     for (int i = 1; i <= n; i++) {       cout << arr[i];       if (i != n) {         cout <<    ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e18;   struct query {     long long k, pos, ind;   };   struct fenwick {     long long n;     vector<long long> f;     fenwick(long long _n) {       n = _n;       f.resize(n, 0);     }     void add(long long val, long long i) {       for (; i < n; i |= (i + 1)) f[i] += val;     }     long long sum(long long i) {       long long ans = 0;       for (; i >= 0; i &= (i + 1), --i) ans += f[i];       return ans;     }   };   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n;     cin >> n;     vector<pair<long long, long long> > a(n);     vector<long long> start(n);     for (long long i = 0; i < n; ++i) {       cin >> a[i].first;       start[i] = a[i].first;       a[i].second = i;     }     sort(a.begin(), a.end(),          [](pair<long long, long long> a, pair<long long, long long> b) {            if (a.first == b.first) return a.second > b.second;            return a.first < b.first;          });     reverse(a.begin(), a.end());     long long q;     cin >> q;     vector<query> qs(q);     for (long long t = 0; t < q; ++t) {       cin >> qs[t].k >> qs[t].pos;       qs[t].k--;       qs[t].pos--;       qs[t].ind = t;     }     sort(qs.begin(), qs.end(), [](query a, query b) { return a.k < b.k; });     fenwick f(n);     long long l = -1;     vector<long long> ans(q);     for (long long i = 0; i < q; ++i) {       while (l < qs[i].k) {         ++l;         f.add(1, a[l].second);       }       long long L = 0, R = n - 1;       while (R - L > 1) {         long long m = (R + L) / 2;         if (f.sum(m) <= qs[i].pos) {           L = m;         } else           R = m;       }       if (f.sum(L) < qs[i].pos + 1) swap(L, R);       ans[qs[i].ind] = start[L];     }     for (auto x : ans) cout << x <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     string x, y;     cin >> x >> y;     int n, m;     n = x.size(), m = y.size();     if (n != m) {       cout <<  NO ;       return 0;     }     if (n == 1) {       if (x == y)         cout <<  YES ;       else         cout <<  NO ;       return 0;     }     if (x == y) {       cout <<  YES ;       return 0;     }     int cnt = count(x.begin(), x.end(),  0 );     int cnt1 = count(y.begin(), y.end(),  0 );     if (cnt == n || cnt1 == n)       cout <<  NO ;     else       cout <<  YES ;   }


#include <bits/stdc++.h>   using namespace std;   int dp[2][316 + 1][316 + 2];   int addmod(int x, int y) {     int sum = x + y;     if (sum >= 1000000007) sum -= 1000000007;     return sum;   }   int mulmod(int x, int y, int p) {     long long prod = x;     prod = (prod * y) % p;     return (int)prod;   }   void add_to_mod(int &x, int y) { x = addmod(x, y); }   int run_dp(int n, int m, int x) {     int result = 0;     if (n > m) return result;     int next = 0;     dp[next][0][0] = 1;     for (int i = 1; i <= m; i++) {       int prev = next;       next ^= 1;       for (int j = n; j > 0; j--)         for (int k = j; k >= 0; k--) {           dp[next][j][k] = addmod(dp[prev][j - 1][k], dp[next][j][k + 1]);           if (m - i != x - k) {             int s = dp[prev][j][k];             if (k > 0) add_to_mod(s, dp[next][j - 1][k - 1]);             add_to_mod(dp[next][j][k], s);           }         }       dp[next][0][0] = (m - i) > x;     }     result = dp[next][n][0];     for (int i = 2; i <= n; i++) result = mulmod(result, i, 1000000007);     return result;   }   int solve_problem() {     int n, m, x;     if (scanf( %d %d %d , &n, &m, &x) != 3) return 1;     --x;     int result = run_dp(n, m, x);     printf( %d n , result);     return 0;   }   int main() {     solve_problem();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     int n;     cin >> n;     vector<int> data(n + 1);     int ans = 1e9;     for (int i = 1; i <= n; i++) {       cin >> data[i];       ans = min(ans, data[i] / max(i - 1, n - i));     }     cout << ans << endl;   }   int main() {     cin.sync_with_stdio(0);     cin.tie(0);     solve();   }


#include <bits/stdc++.h>   int n, k, m, t, l;   int a, b;   int main() {     scanf( %d %d %d %d , &n, &k, &m, &t);     l = n;     for (int i = 1; i <= t; i++) {       scanf( %d %d , &a, &b);       if (a == 1) {         if (b <= k)           l++, k++;         else           l++;       } else {         if (b < k)           l -= b, k -= b;         else           l = b;       }       printf( %d %d n , l, k);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int gl, gr, bl, br;   bool check(int g, int b) {     if (g - 1 <= b && b <= 2 * (g + 1))       return true;     else       return false;   }   int main() {     scanf( %d %d %d %d , &gl, &gr, &bl, &br);     if (check(gl, br) || check(gr, bl))       cout <<  YES  << endl;     else       cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int dx[4] = {-1, 1, 0, 0};   int dy[4] = {0, 0, -1, 1};   const int inf = 0x7f7f7f7f;   const int mod = 1000000007;   const double eps = 1e-8;   int n, m;   set<int> v;   vector<pair<int, int> > ans;   vector<int> vc[100007];   bool isok(int u, int v) {     for (size_t i = 0; i < vc[u].size(); ++i) {       if (vc[u][i] == v) return false;     }     return true;   }   bool dfs(int u) {     if ((int)ans.size() == min(n - 1, m)) return true;     v.erase(u);     for (set<int>::iterator it = v.begin(); it != v.end(); ++it) {       if (!isok(u, *it)) continue;       ans.push_back(make_pair(u, *it));       if (dfs(*it))         return true;       else {         ans.pop_back();         it = v.find(*it);       }     }     v.insert(u);     return false;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i <= n; ++i) vc[i].clear();     int x, y;     for (int i = 0; i < m; ++i) {       scanf( %d%d , &x, &y);       vc[x].push_back(y);       vc[y].push_back(x);     }     for (int i = 1; i <= n; ++i) v.insert(i);     ans.clear();     for (int i = 1; i <= n; ++i) {       if (dfs(i)) {         if (m == n) ans.push_back(make_pair(ans[0].first, ans.back().second));         for (int i = 0; i < m; ++i)           printf( %d %d n , ans[i].first, ans[i].second);         return 0;       }     }     printf( -1 n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long b, c, d, e, f, g, h, i, j = 0, k = 2, l, o = 0, m = 0, n = 0, q = 1,                                       r = 1, s, t, u, v, x = 1, y = 1,                                       z = 1111111;     string a;     cin >> a;     b = a.size();     for (i = 0; i < b; i++) {       if (a[i] ==  4 ) n++;       if (a[i] ==  7 ) m++;     }     if (n == 0 && m == 0) {       cout << -1;       return 0;     }     if (m > n)       cout << 7;     else       cout << 4;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   vector<vector<int> > pre;   vector<int> starts;   vector<int> coproc;   vector<int> cnts;   vector<int> nexts;   void dfs(int v, int co) {     for (int p : pre[v]) {       cnts[p]--;       if (cnts[p] == 0) {         if (coproc[p] == co) {           nexts.push_back(p);         } else {           dfs(p, co);         }       }     }   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int n, m;     cin >> n >> m;     coproc.resize(n);     for (int i = 0; i < n; i++) {       cin >> coproc[i];     }     pre.resize(n);     cnts.assign(n, 0);     for (int i = 0; i < m; i++) {       int u, v;       cin >> u >> v;       pre[v].push_back(u);       cnts[u]++;     }     for (int i = 0; i < n; i++) {       if (cnts[i] == 0) {         if (coproc[i])           nexts.push_back(i);         else           starts.push_back(i);       }     }     int cnt = 0;     while (starts.size() + nexts.size()) {       for (auto v : starts) {         dfs(v, 1);       }       starts.swap(nexts);       nexts.clear();       if (starts.size()) {         cnt++;       }       for (auto v : starts) {         dfs(v, 0);       }       starts.swap(nexts);       nexts.clear();     }     cout << cnt <<   n ;     return false;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long> z_function(string s) {     long long n = (long long)s.length();     vector<long long> z(n);     for (long long i = 1, l = 0, r = 0; i < n; ++i) {       if (i <= r) z[i] = min(r - i + 1, z[i - l]);       while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i];       if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1;     }     return z;   }   signed main() {     ios_base::sync_with_stdio(false);     string s, t;     cin >> s >> t;     string str = t +  #  + s;     auto z = z_function(str);     long long n = s.size(), m = t.size();     long long dp[n], pre1[n], pre2[n];     for (long long i = 0; i < n; i++) dp[i] = 0, pre1[i] = 0, pre2[i] = 0;     for (long long i = m - 1; i < n; i++) {       if (z[i + 2] != m) {         if (i) dp[i] = dp[i - 1] % 1000000007;       } else {         long long pre;         if (i == m - 1)           pre = 0;         else           pre = pre2[i - m];         dp[i] = pre + i - m + 2;         dp[i] %= 1000000007;       }       if (i == 0)         pre1[i] = dp[i];       else         pre1[i] = pre1[i - 1] + dp[i];       if (i == 0)         pre2[i] = pre1[i];       else         pre2[i] = pre2[i - 1] + pre1[i];       pre1[i] %= 1000000007;       pre2[i] %= 1000000007;     }     long long ans = 0;     for (long long i = 0; i < n; i++) {       ans += dp[i];       ans %= 1000000007;     }     cout << ans % 1000000007;   }


#include <bits/stdc++.h>   using namespace std;   vector<vector<int> > g;   vector<char> used;   vector<long long> distArr, fArr, sArr;   long long A, B, diameter;   long long answer = 0;   struct step {     int a, b, d;     friend ostream& operator<<(ostream& s, const step& v);   };   ostream& operator<<(ostream& s, const step& v) {     s << v.a <<     << v.b <<     << v.d;     return s;   }   vector<step> arr;   void dfs(int v, long long dist, vector<long long>& dArr, long long& cur) {     dArr[v] = dist;     if (dArr[cur] < dist) cur = v;     for (int i = 0; i < (int)g[v].size(); i++) {       int to = g[v][i];       if (!used[to]) {         used[to] = true;         dfs(to, dist + 1, dArr, cur);       }     }   }   void dfs2(int v, long long dist, bool flag) {     if (!flag) {       sArr[v] = dist;     }     for (int i = 0; i < (int)g[v].size(); i++) {       int to = g[v][i];       if (!used[to]) {         used[to] = true;         dfs2(to, dist + 1, flag);       }     }     if (fArr[v] + sArr[v] > diameter) {       step temp;       if (fArr[v] > sArr[v]) {         temp.a = A + 1;         answer += fArr[v];       } else {         temp.a = B + 1;         answer += sArr[v];       }       temp.b = v + 1;       temp.d = v + 1;       arr.push_back(temp);     }     if (fArr[v] + sArr[v] == diameter && flag && v != A) {       step temp;       temp.a = A + 1;       temp.d = v + 1;       temp.b = v + 1;       answer += fArr[v];       arr.push_back(temp);     } else if (fArr[v] + sArr[v] == diameter)       used[v] = false;   }   int main() {     int n;     cin >> n;     g.assign(n, vector<int>());     for (int i = 0; i < n - 1; i++) {       int from, to;       cin >> from >> to;       g[from - 1].push_back(to - 1);       g[to - 1].push_back(from - 1);     }     used.assign(n, false);     used[0] = true;     distArr.assign(n, 0);     A = 0;     B = 0;     dfs(0, 0, distArr, A);     used.assign(n, false);     used[A] = true;     fArr.assign(n, 0);     sArr.assign(n, 0);     dfs(A, 0, fArr, B);     diameter = fArr[B];     used.assign(n, false);     used[B] = true;     dfs2(B, 0, false);     dfs2(A, 0, true);     cout << answer << endl;     for (int i = 0; i < (int)arr.size(); i++) cout << arr[i] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long> v;   long long a, b, f, k;   int cnt() {     int c = 0;     int n = v.size();     long long now = 0;     for (int i = 0; i < n; i++) {       if (now + b < v[i]) return -1;       while (i < n && now + b >= v[i]) i++;       i--;       c++;       now = v[i];     }     return c - 1;   }   int main() {     scanf( %I64d%I64d%I64d%I64d , &a, &b, &f, &k);     v.push_back(f);     for (long long i = 1; i < k; i++) {       if (i & 1)         v.push_back(v[i - 1] + 2 * (a - f));       else         v.push_back(v[i - 1] + 2 * f);     }     v.push_back(a * k);     printf( %d , cnt());   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     long long q;     cin >> q;     while (q--) {       long long l1, r1, l2, r2;       cin >> l1 >> r1 >> l2 >> r2;       if (l1 == l2) {         cout << l1 <<     << r2 <<   n ;         continue;       }       if (l1 == r2) {         cout << l1 <<     << l2 <<   n ;         continue;       }       if (r1 == l2) {         cout << r1 <<     << r2 <<   n ;         continue;       }       if (r2 == r1) {         cout << r1 <<     << l2 <<   n ;         continue;       }       cout << l1 <<     << l2 <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const long long M = 1e9 + 9;   int n, m, a, b, k;   char s[100005];   long long ans, q;   inline long long f(char a) { return a ==  -  ? -1 : 1; }   inline long long fpow(long long a, long long b) {     long long r = 1;     for (; b; b >>= 1, a = (a * a) % M)       if (b & 1) r = (r * a) % M;     return r;   }   inline long long sum(long long a1, long long q, long long n) {     if (q == 1) return a1 * n % M;     long long p = (fpow(q, n) - 1 + M) % M;     long long qq = (q - 1 + M) % M;     qq = fpow(qq, M - 2);     return a1 * p % M * qq % M;   }   int main() {     scanf( %d %d %d %d , &n, &a, &b, &k);     q = b * fpow(a, M - 2) % M;     q = fpow(q, k);     m = (n + 1) / k;     scanf( %s , s);     if (n < k) {       for (int i = 0; i <= n; i++) {         long long t = fpow(a, n - i) * fpow(b, i) % M;         if (s[i] ==  - ) t = M - t;         (ans += t) %= M;       }       return printf( %lld n , ans), 0;     }     for (int i = 0; i < k; i++) {       long long x = sum(fpow(a, n - i) * fpow(b, i) % M, q, m);       if (s[i] ==  - ) x = M - x;       (ans += x) %= M;     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   int n, a[105], ans;   int comp(const void* elem1, const void* elem2) {     int f = *((int*)elem1);     int s = *((int*)elem2);     if (f > s) return 1;     if (f < s) return -1;     return 0;   }   int main(int argc, char* argv[]) {     scanf( %d , &n);     int i;     for (i = 0; i < n; ++i) {       int HH, MM;       scanf( %d:%d , &HH, &MM);       a[i] = HH * 60 + MM;     }     qsort(a, n, sizeof(a[0]), comp);     ans = 0;     for (i = 0; i < n - 1; ++i) {       if (a[i + 1] - a[i] > ans) {         ans = a[i + 1] - a[i];       }     }     if (a[0] + 24 * 60 - a[n - 1] > ans) {       ans = a[0] + 24 * 60 - a[n - 1];     }     ans--;     int d1, d2;     d1 = ans / 60;     d2 = ans % 60;     if (d1 < 10) {       if (d2 < 10) {         printf( 0%d:0%d n , d1, d2);       } else {         printf( 0%d:%d n , d1, d2);       }     } else {       if (d2 < 10) {         printf( %d:0%d n , d1, d2);       } else {         printf( %d:%d n , d1, d2);       }     }     return 0;   }


#include <bits/stdc++.h>   const int maxn = 100100;   const int bl = 512;   static const int N = 600;   int n, k, q;   int s[maxn];   struct block {     int pop[N], c0;     int push[N], c1;     int ok;   } o[maxn / bl + 10];   inline int eql(const int *a, const int *b, const int len) {     for (int i = 0; i < len; ++i) {       if (a[i] != b[i]) return 0;     }     return 1;   }   inline bool ask(int l, int r) {     static int st[maxn];     int top = 0;     for (; l % bl && l <= r; ++l)       if (s[l] > 0) {         st[++top] = s[l];       } else {         if (!top || st[top] != -s[l]) return 0;         --top;       };     for (; l + bl - 1 <= r; l += bl) {       block &o = ::o[l / bl];       if (!o.ok) return 0;       if (top < o.c0) return 0;       if (!eql(st + top - o.c0 + 1, o.pop + N - o.c0, o.c0)) return 0;       top -= o.c0;       memcpy(st + top + 1, o.push + 1, o.c1 << 2);       top += o.c1;     }     for (; l <= r; ++l)       if (s[l] > 0) {         st[++top] = s[l];       } else {         if (!top || st[top] != -s[l]) return 0;         --top;       };     return top == 0;   }   inline void init(int id) {     block &o = ::o[id];     o.c0 = o.c1 = 0;     o.ok = 1;     int top = 0;     const int L = id * bl, R = std::min(n - 1, id * bl + bl - 1);     for (int i = L; i <= R; ++i) {       if (s[i] > 0) {         o.push[++top] = s[i];       } else {         if (top && o.push[top] != -s[i]) {           o.ok = 0;           return;         } else {           if (top) {             --top;           } else {             o.pop[N - ++o.c0] = -s[i];           }         }       }     }     o.c1 = top;   }   int main() {     std::ios::sync_with_stdio(false), std::cin.tie(0);     std::cin >> n >> k;     for (int i = 0; i < n; ++i) {       std::cin >> s[i];     }     for (int i = 0; i <= (n - 1) / bl; ++i) init(i);     std::cin >> q;     for (int i = 0; i < q; ++i) {       int opt, pos, v, l, r;       std::cin >> opt;       if (opt == 1) {         std::cin >> pos;         --pos;         std::cin >> s[pos];         init(pos / bl);       } else {         std::cin >> l >> r;         std::cout << (ask(l - 1, r - 1) ?  Yes  :  No ) <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long int;   int main() {     int n, k;     cin >> n >> k;     set<char> st;     string s;     cin >> s;     for (int i = 0; i < n; ++i) st.insert(s[i]);     int res = 0;     char lastc = 0;     int i;     for (i = 0; i < k && !st.empty(); ++i) {       auto it = st.begin();       char c = *it;       st.erase(it);       if (*it - lastc < 2)         --i;       else {         res += int(c -  a  + 1);         lastc = c;       }     }     if (i == k)       cout << res <<   n ;     else       cout << -1 <<   n ;     return 0;   }


#include <bits/stdc++.h>   const int N = 1000001;   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int n;     cin >> n;     vector<pair<int, int>> a[n + 1];     int ans = 0;     for (int i = 0; i < n - 1; i++) {       int u, v;       cin >> u >> v;       a[u].push_back(make_pair(v, i + 1));       a[v].push_back(make_pair(u, i + 1));       ans = max({ans, (int)a[u].size(), (int)a[v].size()});     }     cout << ans <<   n ;     int final[n], vis[n + 1];     memset(final, -1, sizeof final);     memset(vis, 0, sizeof vis);     queue<int> q;     q.push(1);     vis[1] = 1;     while (!q.empty()) {       int u = q.front();       q.pop();       map<int, int> d;       for (auto [v, ind] : a[u]) {         d[final[ind]] = 1;       }       int day = 1;       for (auto [v, ind] : a[u]) {         if (final[ind] == -1) {           while (d[day]) {             ++day;           }           final[ind] = day++;         }         if (vis[v] == 0) {           vis[v] = 1;           q.push(v);         }       }     }     vector<int> f1[ans + 1];     for (int i = 1; i < n; i++) {       f1[final[i]].push_back(i);     }     for (int i = 1; i < ans + 1; i++) {       cout << f1[i].size() <<    ;       for (int e : f1[i]) {         cout << e <<    ;       }       cout <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   struct column {     int a[13];     int x;   };   column c[2001];   bool cmp(column p, column q) { return p.x > q.x; }   int s[13][4096], f[13][4096];   int mian() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) scanf( %d , &c[j].a[i]);     for (int j = 1; j <= m; j++) {       c[j].x = 0;       for (int i = 1; i <= n; i++) c[j].x = max(c[j].x, c[j].a[i]);     }     sort(c + 1, c + 1 + m, cmp);     m = min(m, n);     for (int i = 1; i <= m; i++)       for (int j = 0; j < (1 << n); j++) {         s[i][j] = 0;         for (int p = 0; p < n; p++) {           int sum = 0;           for (int l = 1, k = p + 1; l <= n; l++, k = (k == n) ? 1 : (k + 1))             if (j & (1 << l - 1)) sum += c[i].a[k];           s[i][j] = max(s[i][j], sum);         }       }     for (int i = 1; i <= m; i++)       for (int j = 0; j < (1 << n); j++) {         f[i][j] = f[i - 1][j];         for (int k = j; k; k = (k - 1) & j)           f[i][j] = max(f[i][j], f[i - 1][j ^ k] + s[i][k]);       }     printf( %d n , f[m][(1 << n) - 1]);     return 0;   }   int t;   int main() {     for (scanf( %d , &t); t; t--) mian();     return 0;   }


#include <bits/stdc++.h>   #pragma GCC target( avx2 )   #pragma GCC optimization( O3 )   #pragma GCC optimization( unroll-loops )   using namespace std;   template <class T1, class T2>   ostream &operator<<(ostream &os, const pair<T1, T2> &p) {     os <<  {  << p.first <<  ,  << p.second <<  } ;     return os;   }   const int N = 1e6 + 5;   const long long oo = 998244353;   long long fact[N];   long long fact_inv[N];   long long extended_euclidean(long long a, long long b, long long &x,                                long long &y) {     if (a == 0) {       x = 0;       y = 1;       return b;     }     long long x1, y1;     long long d = extended_euclidean(b % a, a, x1, y1);     x = y1 - (b / a) * x1;     y = x1;     return d;   }   long long mod_inverse(long long a, long long m) {     long long x, y;     long long g = extended_euclidean(a, m, x, y);     if (g != 1) {       cout <<  No solution! ;       return -1;     } else {       x = (x % m + m) % m;       return x;     }   }   long long ncr(int n, int r) {     return n < r ? 0                  : ((1LL * fact[n] % oo * fact_inv[r] % oo) % oo *                     fact_inv[n - r]) %                        oo;   }   void pre(int N) {     fact[0] = 1;     for (long long i = 1; i < N; i++) {       fact[i] = (i * fact[i - 1]) % oo;     }     fact_inv[N - 1] = mod_inverse(fact[N - 1], oo);     for (long long i = N - 2; i >= 0; i--) {       fact_inv[i] = (i + 1LL) * fact_inv[i + 1] % oo;     }   }   long long bigmod(long long a, long long p, long long m) {     long long res = 1;     long long x = a;     while (p) {       if (p & 1) {         res = (res * x) % m;       }       x = (x * x) % m;       p = p >> 1;     }     return res;   }   int32_t main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     pre(N);     int n;     cin >> n;     int a = 0, b = 0;     int totww = 0, totwb = 0;     int totbb = 0, totbw = 0;     int tot__ = 0;     while (n--) {       string s;       cin >> s;       if (s ==  WW )         a++;       else if (s ==  W?  or s ==  ?W )         b++;       else if (s ==  B?  or s ==  ?B )         a--, b++;       else if (s ==  BB )         a--;       else if (s ==  ?? )         b += 2, a--;       totww += s ==  WW ;       totwb += s ==  W? ;       totwb += s ==  WB ;       totwb += s ==  ?B ;       totbb += s ==  BB ;       totbw += s ==  B? ;       totbw += s ==  BW ;       totbw += s ==  ?W ;       tot__ += s ==  ?? ;     }     a = -a;     long long ans = 0;     if (a <= b and a >= 0) {       ans = ncr(b, a);       if (totww == 0 and totbb == 0) {         long long x = bigmod(2, tot__, oo);         ans = (ans - x + oo) % oo;         ans += totbw == 0;         ans += totwb == 0;       }     }     ans = (ans + oo) % oo;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-9;   const int INF = 1 << 30;   const long long LINF = 1ll << 60;   const int BUFFER_SZ = 1 << 15;   const int MOD = 1000 * 1000 * 1000 + 7;   char BUFFER[BUFFER_SZ];   int gi() {     int x;     scanf( %d , &x);     return x;   }   double gd() {     double x;     scanf( %lf , &x);     return x;   }   long long gll() {     long long x;     cin >> x;     return x;   }   vector<int> gvi(int n) {     vector<int> a;     while (n-- > 0) a.push_back(gi());     return a;   }   string gs() {     scanf( %s , BUFFER);     return string(BUFFER);   }   template <class T>   void print(vector<T> &x, string format) {     format +=  %c ;     for (int i = 0; i < x.size(); ++i)       printf(format.c_str(), x[i], i + 1 == x.size() ?   n  :    );   }   int c_ins, c_del, c_rep, c_swp;   int dp[4444][4444];   int main() {     int c_ins = gi();     int c_del = gi();     int c_rep = gi();     int c_swp = gi();     string a = gs();     string b = gs();     int prev[256];     memset((prev), (0), sizeof(prev));     for (int i = 0; i <= a.size(); ++i) {       int last = 0;       for (int j = 0; j <= b.size(); ++j) {         if (!i)           dp[i][j] = j * c_ins;         else if (!j)           dp[i][j] = i * c_del;         else {           dp[i][j] = 1 << 30;           int ii = j > 0 ? prev[b[j - 1]] : 0;           int jj = last;           if (a[i - 1] == b[j - 1]) {             dp[i][j] = dp[i - 1][j - 1];             last = j;           }           int v = min(min(dp[i - 1][j] + c_del, dp[i][j - 1] + c_ins),                       dp[i - 1][j - 1] + c_rep);           if (i && j && ii && jj) {             v = min(v, dp[ii - 1][jj - 1] + c_swp + c_del * (i - ii - 1) +                            c_ins * (j - jj - 1));           }           dp[i][j] = min(dp[i][j], v);         }       }       if (i > 0) {         prev[a[i - 1]] = i;       }     }     printf( %d n , dp[a.size()][b.size()]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long int;   using P = pair<ll, ll>;   const int INF = 1001001001;   const ll mod = 998244353;   const ll INFL = 1000000001000000000;   int M = 100005;   int main() {     int n, q;     cin >> n >> q;     vector<ll> v(n);     for (int i = 0; i < (n); i++) cin >> v[i];     vector<int> c(n);     for (int i = 0; i < (n); i++) cin >> c[i];     for (int i = 0; i < (n); i++) c[i]--;     while (q--) {       ll a, b;       cin >> a >> b;       vector<ll> dp(M, -INFL);       ll bigM1 = 0, bigM2 = 0;       int bigC1 = -1, bigC2 = -1;       for (int i = 0; i < (n); i++) {         if (c[i] == bigC1) {           ll nM = max(dp[c[i]] + v[i] * a, bigM2 + v[i] * b);           dp[c[i]] = max(dp[c[i]], nM);           if (bigM1 < nM) {             bigM1 = nM;             bigC1 = c[i];           }         } else {           ll nM = max(dp[c[i]] + v[i] * a, bigM1 + v[i] * b);           dp[c[i]] = max(dp[c[i]], nM);           if (bigM2 < nM) {             bigM2 = nM;             bigC2 = c[i];           }           if (bigM1 < bigM2) {             swap(bigM1, bigM2);             swap(bigC1, bigC2);           }         }       }       ll ans = 0;       for (int i = 0; i < (M); i++) ans = max(ans, dp[i]);       printf( %lld n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) {     if (a == 0)       return b;     else if (b == 0)       return a;     else {       a = a % b;       return gcd(b, a);     }   }   int main() {     int n;     int sum = 0, odd = 0;     scanf( %d , &n);     int t;     for (int i = 0; i < n; i++) {       scanf( %d , &t);       sum += t;       if (t % 2) odd++;     }     if (sum % 2)       cout << odd;     else       cout << n - odd;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     char arr1[n][m];     char arr2[m][n];     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         char a;         cin >> a;         arr1[i][j] = a;         arr2[j][i] = a;       }     }     string ans =   ;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (count(arr1[i], arr1[i] + m, arr1[i][j]) == 1 &&             count(arr2[j], arr2[j] + n, arr1[i][j]) == 1) {           ans += string(1, arr1[i][j]);         }       }     }     cout << ans;   }


#include <bits/stdc++.h>   static const int INF = std::numeric_limits<int>::max();   int main() {     int n;     std::cin >> n;     std::vector<int> a(n + 1), b(n + 1);     for (int i = 1; i <= n; ++i) {       std::cin >> a[i];     }     for (int i = 1; i <= n; ++i) {       std::cin >> b[i];     }     std::vector<int> res(n + 1);     for (int i = 1; i <= n; ++i) {       res[a[i]] = b[i];     }     for (int i = 1; i <= n; ++i) {       std::cout << res[i] <<    ;     }     std::cout << std::endl;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   char c[109][109];   bool ja = 1;   bool useless = 1;   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         cin >> c[i][j];         if (c[i][j] ==  . ) {           if (i % 2 == 0 && j % 2 == 0 || i % 2 == 1 && j % 2 == 1) {             c[i][j] =  B ;           } else {             c[i][j] =  W ;           }         }       }     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         cout << c[i][j];       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e2 + 10;   int main() {     mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n, i, in, last, ans = 0, prelast = -1;     cin >> n;     cin >> last;     for (i = 1; i < n; i++) {       cin >> in;       if (last == 1 && in == 2) {         ans += 3;         if (prelast == 3) ans--;       } else if (last == 1 && in == 3)         ans += 4;       else if (last == 2 && in == 1)         ans += 3;       else if (last == 2 && in == 3) {         cout <<  Infinite n ;         return 0;       } else if (last == 3 && in == 2) {         cout <<  Infinite n ;         return 0;       } else if (last == 3 && in == 1)         ans += 4;       prelast = last;       last = in;     }     cout <<  Finite n ;     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   const long long e = 1e5 + 110;   bool zs[e];   long long num[e];   void sai() {     for (long long i = 2; i <= e - 99; i++) {       if (zs[i]) continue;       for (long long j = i + i; j <= e - 99; j += i) {         zs[j] = true;       }     }     for (long long tot = 0, i = 2; i <= e - 99; i++) {       if (!zs[i]) tot++, num[tot] = i;     }   }   struct fengjie {     long long v, t;   } f[e];   long long cnf = 0;   void fj(long long x) {     long long tmp = x;     for (long long i = 2; i <= x; i++) {       long long cnt = 0;       while (tmp % i == 0) {         tmp /= i;         cnt++;       }       if (cnt == 0) continue;       cnf++;       f[cnf].v = i, f[cnf].t = cnt;     }     return;   }   long long ksm(long long a, long long z) {     long long ret = 1;     if (z == 0) return 1;     while (z != 1) {       if (z % 2 == 1) {         ret *= a;         a *= a;       } else {         a *= a;       }       z /= 2;     }     return a * ret;   }   fengjie an[e];   signed main() {     long long n;     cin >> n;     sai();     fj(n);     long long ans = 1, g = 0, cna = 0, minn;     for (long long i = cnf; i > 0; i--) {       for (long long j = 1; j <= f[i].t; j++) {         if (cna == 0) {           g++;           ans *= ksm(num[g], f[i].v - 1);           an[++cna].v = num[g];           an[cna].t = f[i].v - 1;           continue;         }         long long pl = -1;         minn = ans * ksm(num[g + 1], f[i].v - 1);         for (long long l = 1; l <= cna; l++) {           long long ct = f[i].v * (an[l].t + 1) - 1;           long long tmp = ans / ksm(an[l].v, an[l].t);           tmp *= ksm(an[l].v, ct);           if (tmp < ans or ksm(an[l].v, ct) < an[l].v) continue;           if (tmp < minn) {             pl = l;             minn = tmp;           }         }         if (pl == -1) {           g++;           ans *= ksm(num[g], f[i].v - 1);           an[++cna].v = num[g];           an[cna].t = f[i].v - 1;         }         if (pl != -1) {           long long tmp = ans / ksm(an[pl].v, an[pl].t);           long long ct = f[i].v * (an[pl].t + 1) - 1;           ans = tmp * ksm(an[pl].v, ct);         }       }     }     cout << ans;     return 0;   }