#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   unsigned seed = chrono::system_clock::now().time_since_epoch().count();   mt19937 eng(seed);   long long random2() { return (1ll << 31ll) * eng() + eng(); }   long long n, m, k, q, T;   const long long big = 1000000007;   const long long big2 = 1000000009;   const long long mod = 998244353;   const int MAXN = 300001;   long long PATH[401][401] = {0};   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     long long a, b, c, d, e;     cin >> k;     for (int c1 = 0; c1 < (401); ++c1) {       for (int c2 = 0; c2 < (401); ++c2) {         if (c1 == 0 && c2 == 1) PATH[c1][c2] = 1;         if (c2 == 0) PATH[c1][c2] = 1;       }     }     for (int i = 1; i < (k); ++i) {       for (long long r = 1; r < k + 1; r++) {         for (int x = 0; x < (r + 2); ++x) {           if (x < r)             PATH[i][r] += (PATH[i - 1][x] * PATH[i - 1][r - 1 - x]) % big;           if (x < r + 1)             PATH[i][r] +=                 (1 + 2 * r) * ((PATH[i - 1][x] * PATH[i - 1][r - x]) % big);           PATH[i][r] %= big;           PATH[i][r] +=               r * (r + 1) * ((PATH[i - 1][x] * PATH[i - 1][r - x + 1]) % big);           PATH[i][r] %= big;         }       }     }     cout << PATH[k - 1][1] <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = (int)1e9, maxn = (int)1e5 + 1;   const double eps = (double)1e-8;   const int mod = (int)1000000007;   int c[200009], a[200009], t, k, f, n;   vector<int> b[200009], o;   int main() {     cin >> n;     memset((c), 0, sizeof(c));     ;     for (int i = (1); i <= (n); i++) {       scanf( %d , &(a[i]));       b[a[i]].push_back(i);     }     k = 0;     t = 1;     for (int i = 1; i <= n && t; i++) {       if (b[k].size() == c[k]) {         t = 0;         break;       }       o.push_back(b[k][c[k]]);       c[k]++;       if (k - 2 < 0 || b[k - 2].size() == c[k - 2] ||           b[k - 1].size() == c[k - 1] || b[k].size() == c[k]) {         k++;         continue;       }       k -= 2;     }     if (t) {       cout <<  Possible  << endl;       for (int i = 0; i < o.size(); i++) {         cout << o[i] <<    ;       }       return 0;     }     k--;     for (int i = n; i >= 0; i--) {       if ((b[i].size() != c[i] && i >= k) || (b[i].size() > c[i] + 1)) {         cout <<  Impossible ;         return 0;       }       if (b[i].size() == c[i] + 1 && (i == 0 || b[i - 1].size() == c[i - 1])) {         if ((k - i + 1) % 3 != 0) {           cout <<  Impossible ;           return 0;         }         k = i;         o.push_back(b[i][c[i]]);         if (b[i + 1].size() != c[i + 1]) {           o.push_back(b[i + 1][c[i + 1]]);           k = i + 1;         }       }     }     cout <<  Possible  << endl;     for (int i = 0; i < o.size(); i++) {       cout << o[i] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     cin.tie(NULL);     ios::sync_with_stdio(false);     long long int n, m, i, j, k, ans = 0;     cin >> n >> m;     long long int A[n], B[m];     string str(500,  0 );     for (i = 0; i < n; i++) cin >> A[i];     for (i = 0; i < m; i++) {       cin >> k;       k--;       B[i] = k;     }     for (i = 0; i < m; i++) {       j = i - 1;       str = string(500,  0 );       while (j >= 0 && B[j] != B[i]) {         if (str[B[j]] !=  1 ) {           ans += A[B[j]];           str[B[j]] =  1 ;         }         j--;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   static const int MAXN = 2e5 + 4;   static const int MAXK = 2e5 + 3;   static int n, k;   static std::pair<int, int> p[MAXN];   static int idx[MAXN];   static std::vector<int> q[MAXN];   static int ans[MAXN];   int main() {     scanf( %d%d , &n, &k);     for (int i = 0; i < n; ++i) {       scanf( %d , &p[i].first);       p[i].second = i;     }     for (int i = 0, u, v; i < k; ++i) {       scanf( %d%d , &u, &v);       --u, --v;       q[u].push_back(v);       q[v].push_back(u);     }     std::sort(p, p + n);     for (int i = 0; i < n; ++i) idx[p[i].second] = i;     for (int i = 0, j; i < n;) {       for (j = i + 1; j < n && p[j].first == p[i].first; ++j)         ;       for (int count = i; i < j; ++i) {         int r = count;         for (int t : q[p[i].second])           if (p[idx[t]].first < p[i].first) --r;         ans[p[i].second] = r;       }     }     for (int i = 0; i < n; ++i) printf( %d%c , ans[i], i == n - 1 ?   n  :    );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   mt19937 rng_32(chrono::steady_clock::now().time_since_epoch().count());   const long long maxn = 3e5 + 10;   struct E {     long long to, nxt;   } e[maxn * 2];   long long tot, head[maxn];   long long Add[maxn << 2], lz[maxn << 2];   void adde(long long u, long long v) {     tot++;     e[tot] = {v, head[u]};     head[u] = tot;   }   vector<pair<long long, long long> > v[maxn];   long long n;   long long ans[maxn];   void dfs(int u, int fa, int dep) {     int len = v[u].size();     for (int i = 0; i < len; i++) {       int d = min(n + 1, dep + v[u][i].first + 1);       Add[dep] += v[u][i].second;       Add[d] -= v[u][i].second;     }     Add[dep] += Add[dep - 1];     ans[u] = Add[dep];     for (int i = head[u]; i; i = e[i].nxt) {       int to = e[i].to;       if (to == fa) continue;       dfs(to, u, dep + 1);     }     for (int i = 0; i < len; i++) {       int d = min(n + 1, dep + v[u][i].first + 1);       Add[dep] -= v[u][i].second;       Add[d] += v[u][i].second;     }     Add[dep] -= Add[dep - 1];   }   int main() {     cin >> n;     long long t1, t2;     for (long long i = 1; i < n; i++) {       scanf( %lld%lld , &t1, &t2);       adde(t1, t2);       adde(t2, t1);     }     long long m, t3;     cin >> m;     for (long long i = 1; i <= m; i++) {       scanf( %lld%lld%lld , &t1, &t2, &t3);       v[t1].push_back(make_pair(t2, t3));     }     dfs(1, 1, 1);     for (int i = 1; i <= n; i++) printf( %lld  , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   complex<double> a[524288], b[524288], r[524288], o[524289];   void fft(complex<double> *x, int n, int base) {     int size = 0, mask = (n >> 1) - 1;     for (int i = base - 1; i >= 0; --i, size = (size << 1) | 1, mask >>= 1) {       for (int j = 0; j < n; ++j) {         int front = j >> i, back = j & mask;         int a = (front & size) << 1, b = a | 1;         r[j] = x[(a << i) | back] +                o[(front << i) * (524288 / n)] * x[(b << i) | back];       }       for (int j = 0; j < n; ++j) x[j] = r[j];     }   }   void ifft(complex<double> *x, int n, int base) {     fft(x, n, base);     reverse(x + 1, x + n);     for (int i = 0; i < n; ++i) x[i] /= n;   }   void pin(vector<int> *v) {     cout << v->size() <<   : ;     for (int i : *v) cout << i <<    ;     cout << endl;   }   vector<int> *mg(vector<int> *pa, vector<int> *pb) {     int nn = pa->size() + pb->size() + 2, base = 0;     while ((1 << base) < nn) ++base;     nn = (1 << base);     for (int i = 0; i < pa->size(); ++i) a[i] = complex<double>((*pa)[i], 0);     for (int i = pa->size(); i <= nn; ++i) a[i] = complex<double>(0, 0);     for (int i = 0; i < pb->size(); ++i) b[i] = complex<double>((*pb)[i], 0);     for (int i = pb->size(); i <= nn; ++i) b[i] = complex<double>(0, 0);     fft(a, nn, base);     fft(b, nn, base);     for (int i = 0; i < nn; ++i) a[i] *= b[i];     ifft(a, nn, base);     vector<int> *rt = new vector<int>();     for (int i = 0; i < nn; ++i)       rt->push_back(((long long)(real(a[i]) + 0.5)) % 1009);     while (rt->size() && rt->back() == 0) rt->pop_back();     delete pa;     delete pb;     return rt;   }   int cnt[524288];   const double PI = acos(-1);   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     for (int i = 0; i <= 524288; ++i)       o[i] = exp(2. * i * PI / 524288 * complex<double>(0, 1));     int n, m, k;     cin >> n >> m >> k;     while (n--) {       int t;       cin >> t;       ++cnt[t];     }     auto cmp = [&](const vector<int> *a, const vector<int> *b) {       return a->size() > b->size();     };     priority_queue<vector<int> *, vector<vector<int> *>, decltype(cmp)> pq(cmp);     for (int i = 1; i <= m; ++i) {       if (cnt[i] == 0) continue;       vector<int> *v = new vector<int>();       for (int j = 0; j <= cnt[i]; ++j) v->push_back(1);       pq.push(v);     }     while (pq.size() > 1u) {       vector<int> *p1, *p2;       p1 = pq.top();       pq.pop();       p2 = pq.top();       pq.pop();       pq.push(mg(p1, p2));     }     vector<int> *ans = pq.top();     cout << (ans->size() > k ? (*ans)[k] : 0) << endl;   }


#include <bits/stdc++.h>   using namespace std;   int n, k, v;   map<int, int> mp;   vector<int> a;   bool can(int mid) {     int rem = 0;     for (int i = 0; i < a.size(); i++) {       if (mp[a[i]] >= mid) {         rem += mp[a[i]] / mid;       }     }     return rem >= k;   }   bool cmp(int s, int f) { return mp[s] > mp[f]; }   int main() {     ios_base ::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> k;     for (int i = 0; i < n; i++) {       cin >> v;       mp[v]++;     }     for (auto i : mp) {       a.push_back(i.first);     }     sort(a.begin(), a.end(), cmp);     int l, r, ans = 1;     l = 1, r = n;     while (l < r) {       int mid = (l + r) / 2;       if (can(mid)) {         l = mid + 1;         ans = mid;       } else {         r = mid;       }     }     n = a.size();     for (int i = 0; i < n; i++) {       if (k - mp[a[i]] / ans >= 0) {         for (int j = 0; j < mp[a[i]] / ans; j++) cout << a[i] <<    ;         k -= mp[a[i]] / ans;       } else {         for (int j = 0; j < k; j++) cout << a[i] <<    ;         break;       }     }   }


#include <bits/stdc++.h>   using namespace std;   void update(int *arr, int pos, int diff) {     while (pos <= 100001) {       arr[pos] = max(arr[pos], diff);       pos += pos & -pos;     }   }   int sum(int *arr, int pos) {     int ret = 0;     while (pos > 0) {       ret = max(ret, arr[pos]);       pos -= pos & -pos;     }     return ret;   }   int n, c, d;   int coin[100001], dia[100001];   int main() {     scanf( %d%d%d , &n, &c, &d);     int ans = 0;     for (int i = 0; i < n; i++) {       int b, p;       char k;       int coin_max, dia_max, m;       coin_max = 0;       dia_max = 0;       scanf( %d%d %c , &b, &p, &k);       if (k ==  C ) {         coin_max = sum(dia, d);         if (p > c) continue;         coin_max = max(coin_max, sum(coin, c - p));         update(coin, p, b);       } else {         dia_max = sum(coin, c);         if (p > d) continue;         dia_max = max(dia_max, sum(dia, d - p));         update(dia, p, b);       }       if (coin_max == 0 && dia_max == 0) continue;       m = max(b + coin_max, b + dia_max);       ans = max(ans, m);     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   signed main() {     std::string s;     std::cin >> s;     long long n = s.size();     long long p = 0;     long long ttl = 0;     while (1) {       if (p == n) break;       while (p < n && std::isalpha(s[p])) ++p;       long long a = 0;       std::string tmp;       while (p < n && (s[p] ==  .  || std::isdigit(s[p]))) {         tmp.push_back(s[p]);         if (std::isdigit(s[p])) a = a * 10 + (s[p] -  0 );         ++p;       }       if (tmp.size() < 3 || tmp[tmp.size() - 3] !=  . ) a *= 100;       ttl += a;     }     long long integral = ttl / 100;     std::deque<char> output;     if (integral == 0) {       output.push_front( 0 );     } else {       long long dgt = 0;       while (integral) {         if (dgt == 3) {           dgt = 0;           output.push_front( . );         }         output.push_front(integral % 10 +  0 );         integral /= 10;         ++dgt;       }     }     long long fractional = ttl % 100;     if (fractional) {       output.push_back( . );       output.push_back( 0  + fractional / 10);       output.push_back( 0  + fractional % 10);     }     for (auto ch : output) std::cout << ch;     std::cout << std::endl;     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:256000000 )   using namespace std;   vector<char> cc;   void solve() {     int n, m, k, d = 0;     cin >> n >> m >> k;     vector<string> arr(n), ans(n);     for (int i = 0; i < n; i++) {       cin >> arr[i];       ans[i].resize(m);       for (int j = 0; j < m; j++) d += (arr[i][j] ==  R );     }     vector<int> cnt;     for (int i = 0; i < k - d % k; i++) cnt.push_back(d / k);     for (int i = 0; i < d % k; i++) cnt.push_back(d / k + 1);     vector<int> have(k);     int ptr = 0;     for (int i = 0; i < n; i++) {       if (i % 2 == 0) {         for (int j = 0; j < m; j++) {           char cur = cc[ptr];           ans[i][j] = cur;           have[ptr] += (arr[i][j] ==  R );           if (have[ptr] == cnt[ptr] && ptr < k - 1) ptr++;         }       } else {         for (int j = m - 1; j >= 0; j--) {           char cur = cc[ptr];           ans[i][j] = cur;           have[ptr] += (arr[i][j] ==  R );           if (have[ptr] == cnt[ptr] && ptr < k - 1) ptr++;         }       }     }     for (int i = 0; i < n; i++) cout << ans[i] <<   n ;   }   int main() {     srand(1373737);     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     for (char c =  0 ; c <=  9 ; c++) cc.push_back(c);     for (char c =  a ; c <=  z ; c++) cc.push_back(c);     for (char c =  A ; c <=  Z ; c++) cc.push_back(c);     int t;     cin >> t;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = (int)1e9;   const double eps = (double)1e-8;   const int mod = (int)1000000007;   const int maxn = (int)1e6 + 5;   int n, l, r, z[2 * maxn];   string a, b, c, d;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     cin >> a;     cin >> b;     c =   ;     for (int i = (n - 1); i >= (0); i--) {       if (a[i] ==  W ) c +=  E ;       if (a[i] ==  E ) c +=  W ;       if (a[i] ==  N ) c +=  S ;       if (a[i] ==  S ) c +=  N ;     }     d = c +  #  + b;     l = 0;     r = 0;     z[0] = 0;     for (int i = (1); i < (d.size()); i++) {       z[i] = 0;       if (i <= r) z[i] = min(r - i + 1, z[i - l]);       while (i + z[i] < d.size() && d[i + z[i]] == d[z[i]]) {         z[i]++;       }       if (i + z[i] - 1 > r) {         l = i;         r = i + z[i] - 1;       }       if (i + z[i] - 1 == (int)d.size() - 1) {         cout <<  NO ;         return 0;       }     }     cout <<  YES ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int mod = 1e9 + 7;   const int K = 3;   int main() {     long long int t;     cin >> t;     while (t--) {       long long int n;       cin >> n;       long long int ans = -1;       for (int i = 1; i <= n; ++i) {         long long int x;         cin >> x;         ans = max(ans, x - i);       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main(void) {     int n;     cin >> n;     long long ans = n;     for (int i = 2; i * i <= n; ++i)       if (n % i == 0) {         while (n % i == 0) n /= i, ans += n;       }     if (n > 1) ++ans;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int a[200200];   void solve() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);     }     int pref = 0;     for (int i = 0; i < n; i++) {       if (a[i])         pref++;       else         break;     }     if (pref == n) {       printf( NO n );       return;     }     vector<int> rj;     if (pref % 2 == 1) {       for (int i = pref - 1; i < n - 2; i += 1) {         int xr = a[i] ^ a[i + 1] ^ a[i + 2];         int sm = a[i] + a[i + 1] + a[i + 2];         if (xr == 0) {           rj.push_back(i + 1);           a[i] = 0;           a[i + 1] = 0;           a[i + 2] = 0;           pref -= 1;           break;         } else {           if (sm == 1) {             rj.push_back(i + 1);             a[i + 1] = 1;             a[i + 2] = 1;             i += 1;             pref += 2;           } else if (sm == 3) {             i += 1;             pref += 2;           }         }       }     }     if (pref >= n) {       printf( NO n );       return;     }     if (pref % 2 == 1) {       printf( NO n );       return;     }     for (int i = pref - 1; i >= 1; i -= 2) {       rj.push_back(i);     }     for (int i = 0; i < pref; i++) a[i] = 0;     for (int i = pref; i < n - 2; i++) {       int xr = a[i] ^ a[i + 1] ^ a[i + 2];       int sm = a[i] + a[i + 1] + a[i + 2];       if (sm == 0) continue;       if (a[i]) {         if (xr == 1) {           rj.push_back(i + 1);           rj.push_back(i);           a[i] = 0;           i++;           a[i + 1] = 1;           a[i] = 0;           i--;         } else {           rj.push_back(i + 1);           a[i] = 0;           a[i + 1] = 0;           a[i + 2] = 0;         }       } else {         if (xr == 0) {           rj.push_back(i + 1);           a[i] = 0;           a[i + 1] = 0;           a[i + 2] = 0;         }       }     }     for (int i = 0; i < n; i++) {       if (a[i]) {         printf( NO n );         return;       }     }     printf( YES n%d n , rj.size());     for (int i : rj) {       printf( %d  , i);     }     printf(  n );   }   int main() {     int t;     scanf( %d , &t);     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 4e5 + 5;   struct Edge {     int to, nxt;   } e[N * 2];   int n, head[N], cntE, siz[N], f[N][2];   int ans[N], g[N], mxpt[N], pos[N];   inline void add(int u, int v) {     e[++cntE] = (Edge){v, head[u]};     head[u] = cntE;   }   void dfs1(int u, int fa) {     siz[u] = 1;     for (int i = head[u], v; i; i = e[i].nxt)       if ((v = e[i].to) != fa) {         dfs1(v, u);         siz[u] += siz[v];         if (siz[v] > siz[mxpt[u]]) mxpt[u] = v;         int w = siz[v] <= n / 2 ? siz[v] : f[v][0];         if (f[u][0] < w)           f[u][1] = f[u][0], f[u][0] = w, pos[u] = v;         else           f[u][1] = max(f[u][1], w);       }   }   void dfs2(int u, int fa) {     if (siz[mxpt[u]] > n / 2)       ans[u] = siz[mxpt[u]] - f[mxpt[u]][0] <= n / 2;     else       ans[u] = n - siz[u] - g[u] <= n / 2;     for (int i = head[u], v; i; i = e[i].nxt)       if ((v = e[i].to) != fa) {         int w = (n - siz[u] <= n / 2) ? n - siz[u] : g[u];         g[v] = max(w, f[u][pos[u] == v]);         dfs2(v, u);       }   }   int main() {     scanf( %d , &n);     for (int i = 1, a, b; i < n; ++i) {       scanf( %d%d , &a, &b);       add(a, b);       add(b, a);     }     dfs1(1, 0);     dfs2(1, 0);     for (int i = 1; i <= n; ++i) printf( %d  , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   pair<int, int> a[200];   pair<int, int> b[200];   int pp[200];   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) {       int aa, bb;       scanf( %d%d , &aa, &bb);       a[i].first = min(aa, bb), a[i].second = max(aa, bb);     }     for (int i = 1; i <= m; i++) {       int aa, bb;       scanf( %d%d , &aa, &bb);       b[i].first = min(aa, bb), b[i].second = max(aa, bb);     }     for (int i = 1; i <= n; i++) {       bool flag1 = false, flag2 = false;       for (int j = 1; j <= m; j++) {         if (a[i].first != b[j].first && a[i].second == b[j].second)           pp[b[j].second] = 1, flag1 = true;         if (a[i].second != b[j].second && a[i].first == b[j].first)           pp[b[j].first] = 1, flag2 = true;         if (a[i].second != b[j].first && a[i].first == b[j].second)           pp[b[j].second] = 1, flag2 = true;         if (a[i].first != b[j].second && a[i].second == b[j].first)           pp[b[j].first] = 1, flag1 = true;         if (flag1 && flag2 && a[i].first != a[i].second) {           cout << -1 << endl;           return 0;         }       }     }     for (int i = 1; i <= m; i++) {       bool flag1 = false, flag2 = false;       for (int j = 1; j <= n; j++) {         if (a[j].first != b[i].first && a[j].second == b[i].second) flag2 = true;         if (a[j].second != b[i].second && a[j].first == b[i].first) flag1 = true;         if (a[j].second != b[i].first && a[j].first == b[i].second) flag2 = true;         if (a[j].first != b[i].second && a[j].second == b[i].first) flag1 = true;         if (flag1 && flag2 && b[i].first != b[i].second) {           cout << -1 << endl;           return 0;         }       }     }     int sum = 0;     int ans;     for (int i = 1; i <= 9; i++) {       if (pp[i]) ans = i;       sum += pp[i];     }     if (sum == 1) {       cout << ans << endl;       return 0;     } else {       cout << 0 << endl;       return 0;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = (int)2e5 + 10;   vector<int> res[maxn];   int main() {     string s;     cin >> s;     stack<int> st, st2;     int ma = 0;     for (int i = 0; i < s.size(); i++) {       if (s[i] ==  1 ) {         if (st.empty()) {           cout << -1 << endl;           return 0;         }         res[st.top()].push_back(i + 1);         st2.push(st.top());         st.pop();       } else {         if (!st2.empty()) {           res[st2.top()].push_back(i + 1);           st.push(st2.top());           st2.pop();         } else {           res[ma].push_back(i + 1);           st.push(ma);           ma++;         }       }     }     if (!st2.empty()) {       cout << -1 << endl;       return 0;     }     int cnt = 0;     for (int i = 0; i < maxn; i++)       if (!res[i].empty()) cnt++;     cout << cnt << endl;     for (int i = 0; i < maxn; i++)       if (!res[i].empty()) {         printf( %d , res[i].size());         for (int j = 0; j < res[i].size(); j++) {           printf(  %d , res[i][j]);         }         printf(  n );       }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MX = 255;   int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};   char s[MX][MX];   long long tot[MX][MX];   int was[MX][MX];   int cur[MX][MX];   int main() {     int n, m, p, w;     scanf( %d %d %d %d , &n, &m, &p, &w);     for (int i = 1; i <= n; i++) {       scanf( %s , s[i]);       for (int j = m; j >= 1; j--) {         s[i][j] = s[i][j - 1];       }       s[i][0] =   0 ;     }     int cnt = 0;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         if (s[i][j] >=  A  && s[i][j] <=  Z ) {           cnt++;           queue<pair<int, int> > q;           q.push(make_pair(i, j));           cur[i][j] = p * (s[i][j] -  A  + 1);           was[i][j] = cnt;           while (!q.empty()) {             int xx = q.front().first, yy = q.front().second;             q.pop();             tot[xx][yy] += cur[xx][yy];             for (int dir = 0; dir < 4; dir++) {               int nx = xx + dx[dir], ny = yy + dy[dir];               if (was[nx][ny] != cnt && cur[xx][yy] / 2 > 0 && s[nx][ny] !=  *  &&                   nx >= 1 && nx <= n && ny >= 1 && ny <= m) {                 was[nx][ny] = cnt;                 cur[nx][ny] = cur[xx][yy] / 2;                 q.push(make_pair(nx, ny));               }             }           }         }       }     }     int res = 0;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         if (tot[i][j] > w) res++;       }     }     printf( %d n , res);   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<int, int> > ans;   int p[2000], damage[2000], u[2000];   int main() {     int n, Max, r, used = 0, TT;     cin >> n >> Max >> r;     for (int i = 1; i <= n; i++) cin >> p[i] >> damage[i];     int cap = Max;     memset(u, 0, sizeof(u));     for (int s = 0;; s++) {       int cur = -1;       for (int i = 1; i <= n; i++)         if (!u[i] && p[i] / 100.00 >= Max / double(cap))           if (cur == -1 || damage[i] > damage[cur]) cur = i;       if (cur == -1 && r >= 0) {         cout <<  NO  << endl;         return 0;       }       if (cur != -1) {         r -= damage[cur];         u[cur] = 1;         used++;         ans.push_back(make_pair(s, cur));       }       Max += r;       if (Max > cap) Max = cap;       if (Max <= 0) {         TT = s + 1;         break;       }     }     cout <<  YES  << endl;     cout << TT <<     << ans.size() << endl;     for (int i = 0; i < ans.size(); i++)       cout << ans[i].first <<     << ans[i].second << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int N = 100001;   bool ok;   long long int n, q;   long long int trie[6 * N][5], done[6 * N];   long long int nxt = 1;   string s;   void insert(string s) {     long long int node = 0;     for (char c : s) {       if (trie[node][c -  a ] == 0) {         node = trie[node][c -  a ] = nxt++;       } else {         node = trie[node][c -  a ];       }     }     done[nxt - 1] = 1;   }   void dfs(long long int u, long long int idx, long long int f) {     if (f >= 2) return;     if (idx == s.length() - 1 && f == 1 && done[u]) {       ok = 1;       return;     }     if (idx == s.length() - 1 || done[u]) return;     for (long long int i = 0; i < 3; i++) {       if (trie[u][i] == 0) continue;       if (s[idx + 1] == (char)(i +  a ))         dfs(trie[u][i], idx + 1, f);       else         dfs(trie[u][i], idx + 1, f + 1);     }   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> q;     for (long long int i = 0; i < n; i++) {       cin >> s;       insert(s);     }     for (long long int i = 0; i < q; i++) {       cin >> s;       ok = 0;       dfs(0, -1, 0);       cout << (ok ?  YES n  :  NO n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int ar[310];   bool mp[310][310];   int main() {     int n;     cin >> n;     for (int i = 1; i <= n; i++) cin >> ar[i];     string str;     for (int i = 1; i <= n; i++) {       cin >> str;       for (int j = 0; j < n; j++)         if (str[j] ==  1 ) mp[i][j + 1] = true;     }     for (int k = 1; k <= n; k++)       for (int i = 1; i <= n; i++)         for (int j = 1; j <= n; j++)           if (mp[i][k] && mp[k][j]) mp[i][j] = true;     for (int i = 1; i < n; i++)       for (int j = i + 1; j <= n; j++)         if (ar[i] > ar[j] && mp[i][j]) swap(ar[i], ar[j]);     cout << ar[1];     for (int i = 2; i <= n; i++) cout <<     << ar[i];     cout <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     long long int n;     cin >> n;     string s;     cin >> s;     vector<long long int> v(n + 1, 0);     for (long long int i = 1; i <= n; i++) {       if (s[i - 1] ==  ( )         v[i] = v[i - 1] + 1;       else         v[i] = v[i - 1] - 1;     }     if (v[n] != 0) {       cout <<  -1  <<   n ;       return 0;     }     long long int ans = 0;     long long int temp = 0;     for (long long int i = 1; i <= n; i++) {       if (v[i] < 0) {         temp++;       } else if (v[i] == 0 && v[i - 1] < 0) {         temp++;         ans += temp;         temp = 0;       }     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ld = long double;   using vi = vector<int>;   using vll = vector<ll, ll>;   using pii = pair<int, int>;   using pll = pair<ll, ll>;   ll modpow(ll a, ll b, ll mod = (ll)(1e9 + 7)) {     if (!b) return 1;     a %= mod;     return modpow(a * a % mod, b / 2, mod) * (b & 1 ? a : 1) % mod;   }   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   const int DEBUG = 1;   const int mxk = 13, mxm = 4, dsz = (1 << mxm) * mxk, mod = 1e9 + 7;   int n, m, k;   void add(ll &a, ll b) {     a += b;     if (a >= mod) a -= mod;   }   struct Matrix {     ll a[dsz][dsz];     Matrix() {       for (int i = 0; i < dsz; i++)         for (int j = 0; j < dsz; j++) a[i][j] = 0;     }   } id;   void init() {     for (int i = 0; i < dsz; i++) id.a[i][i] = 1;   }   Matrix operator*(Matrix a, Matrix b) {     Matrix ret;     for (int i = 0; i < dsz; i++)       for (int j = 0; j < dsz; j++)         for (int kk = 0; kk < dsz; kk++)           add(ret.a[i][kk], a.a[i][j] * b.a[j][kk] % mod);     return ret;   }   int cnv(int mask, int kk) { return (kk << mxm) + mask; }   int rot(int mask) { return (mask << 1) % (1 << m); }   Matrix modpow(Matrix a, ll b) {     Matrix ans = id;     while (b) {       if (b & 1) ans = ans * a;       a = a * a;       b /= 2;     }     return ans;   }   void solve() {     init();     Matrix op;     for (int mask = 0; mask < 1 << m; mask++)       for (int kk = 0; kk <= k; kk++) {         add(op.a[cnv(rot(mask), kk)][cnv(mask, kk)], 1);         if (kk < k)           add(op.a[cnv(rot(mask) + 1, kk + 1)][cnv(mask, kk)],               1 + __builtin_popcount(mask));       }     Matrix b;     b.a[0][0] = 1;     Matrix res = modpow(op, n) * b;     ll ans = 0;     for (int mask = 0; mask < 1 << m; mask++) add(ans, res.a[cnv(mask, k)][0]);     cout << ans << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     while (cin >> n >> k >> m) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   bool chmax(T& a, const T& b) {     return a < b ? (a = b, true) : false;   }   template <typename T>   bool chmin(T& a, const T& b) {     return b < a ? (a = b, true) : false;   }   int N;   string S;   int main() {     cin >> S;     N = S.size();     for (int i = (1); i < (N); i++) {       if (S[i] == S[i - 1]) {         cout <<  Impossible  << endl;         return 0;       }     }     char twice;     for (char c =  A ; c <=  Z ; c++) {       if (count((S).begin(), (S).end(), c) == 2) {         twice = c;         break;       }     }     int x = S.find(twice), y = S.find(twice, x + 1);     string a = S.substr(0, x), b = S.substr(x + 1, y - x - 1),            c = S.substr(y + 1);     string t = b + c + a;     vector<string> ans(2, string(13,  . ));     ans[0][12 - b.size() / 2] = twice;     int idx = 0;     bool flag = false;     while (true) {       for (int i = 0; i < 13; i++) {         if (ans[0][i] == twice && flag == false) {           flag = true;         } else if (ans[0][i] == twice && flag == true) {           cout << ans[0] << endl;           cout << ans[1] << endl;           return 0;         } else if (flag && idx < t.size()) {           ans[0][i] = t[idx++];         }       }       for (int i = 12; i >= 0; i--) {         if (flag && idx < t.size()) {           ans[1][i] = t[idx++];         }       }     }     return 0;   }


#include<bits/stdc++.h>   using namespace std;   #define ll long long   #define pb push_back   #define f first   #define s second   #define go ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)   /*      ll dx[8]={-1,-1,-1,0,0,1,1,1};   ll dy[8]={-1,0,1,-1,1,-1,0,1};   bool sortbysec(const pair<int,int> &a,                 const pair<int,int> &b)   {       return (a.second < b.second);   }   */   int main()   {        ll t;        cin>>t;        while(t--)        {            ll n;            cin>>n;            for(int i=2;i<=n;i++)               cout<<i<<   ;            cout<< 1 <<endl;        }      }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 200010;   map<int, int> mp;   int n;   struct pnt {     int x, w;     bool operator<(const pnt &p) const { return x < p.x; }   } p[maxn];   int ask(int x) {     int res = 0;     for (int i = x; i > 0; i -= (i & -i))       if (mp.count(i)) res = max(res, mp[i]);     return res;   }   void update(int x, int v) {     for (int i = x; i < 0x3f3f3f3f; i += (i & -i)) mp[i] = max(mp[i], v);   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %d%d , &p[i].x, &p[i].w);     sort(p, p + n);     int ans = 0;     for (int i = 0; i < n; i++) {       int tmp = ask(p[i].x - p[i].w);       ans = max(ans, tmp + 1);       update(p[i].x + p[i].w, tmp + 1);     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 2;   long long n, m;   long long a[N], b[N];   long long f(long long x) {     long long res = 0;     for (long long(i) = (0); (i) < (n); (i)++) {       if (a[i] < x) res += llabs(a[i] - x);     }     for (long long(i) = (0); (i) < (m); (i)++) {       if (b[i] > x) res += llabs(b[i] - x);     }     return res;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     ;     cin >> n >> m;     for (long long(i) = (0); (i) < (n); (i)++) cin >> a[i];     for (long long(i) = (0); (i) < (m); (i)++) cin >> b[i];     long long le = 1, ri = 1e9;     while (llabs(ri - le) > 3) {       long long m1 = le + (ri - le) / 3;       long long m2 = ri - (ri - le) / 3;       if (f(m1) < f(m2))         ri = m2;       else         le = m1;     }     long long ans = 1e18;     for (long long(x) = (le); (x) < (ri + 1); (x)++) {       ans = min(ans, f(x));     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       long long ma = -1e15, need = 0;       for (int i = 0; i < n; i++) {         long long a;         cin >> a;         ma = max(a, ma);         need = max(need, ma - a);       }       long long ans = 0;       while ((1ll << ans) - 1 < need) ans++;       cout << ans << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     double eps = 1e-12;     double y1, y2, yw, xb, yb, r;     cin >> y1 >> y2 >> yw >> xb >> yb >> r;     double yk = yw - r;     double yt = y1 + r;     double d1 = yk - yt;     double d2 = yk - yb;     double xk = xb * d1 / (d2 + d1);     double A = yt - yk;     double B = xk;     double C = -B * yt;     double h = fabs(B * y2 + C) / sqrt(A * A + B * B);     if (r > h - eps) {       cout << -1 << endl;     } else {       printf( %.12lf n , xk - eps);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f;   const int mod = 1e9 + 7;   int n, a[500010], b[500010], c[500010], bit[500010];   void init() {     for (int i = 1; i <= n; i++) {       bit[i] = 0;     }   }   int lowbit(int x) { return x & (-x); }   void add(int u, int x) {     for (; u <= n; u += lowbit(u)) {       bit[u] = (bit[u] + x) % mod;     }   }   int sum(int u) {     int res = 0;     for (; u >= 1; u -= lowbit(u)) {       res = (res + bit[u]) % mod;     }     return res;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       b[i] = a[i];     }     sort(b + 1, b + n + 1);     for (int i = 1; i <= n; i++) {       a[i] = lower_bound(b + 1, b + n + 1, a[i]) - b;     }     int ans = 0;     init();     for (int i = 1; i <= n; i++) {       ans = (ans + 1LL * b[a[i]] * sum(a[i]) % mod * (n - i + 1) % mod) % mod;       c[a[i]] += sum(a[i]) * (n - i + 1);       add(a[i], i);     }     init();     for (int i = n; i >= 1; i--) {       ans = (ans + 1LL * b[a[i]] * sum(a[i]) % mod * i % mod) % mod;       c[a[i]] += sum(a[i]) * i;       add(a[i], n - i + 1);     }     for (int i = 1; i <= n; i++) {       ans = (ans + 1LL * b[a[i]] * i % mod * (n - i + 1) % mod) % mod;       c[a[i]] += i * (n - i + 1);     }     printf( %d n , ans);     return 0;   }


#include<bits/stdc++.h>   // #define int long long   #define ld long double   #define fi first   #define se second   #define vll vector<int>   #define pii pair<int,int>   #define pb push_back   #define sz(v) (int)(v).size()   #define inf (int)(1e8)   #define md (int)(1e9+7)   #define all(v) (v).begin(),(v).end()   #define rep(i,a,b) for(int i=a;i<b;++i)   using namespace std;      const int M = 50 + 11;   const int K = 251;      int n;   string s;   int dp[2][M][K][K];   /*   int f(int i,int car,int pos,int neg) {    if(pos<0 or neg<0 or car+30>=M or car+30<0) return inf;    if(i>=n) return car?inf:0;    if(dp[i][car+30][pos][neg]!=-1) return dp[i][car+30][pos][neg];    int ans=min(f(i,car,pos-1,neg),f(i,car,pos,neg-1));    int now=s[i]- 0 ,tot=pos-neg+car;    if((tot%10+10)%10==now) {     ans=min(ans,f(i+1,tot/10+(tot>0?0:-1)*(bool)(tot%10),pos,neg)+pos+neg);    }    return dp[i][car+30][pos][neg]=ans;   }   */   int32_t main(){       ios_base::sync_with_stdio(false);       cin.tie(0);              cin>>s;       reverse(all(s));       s+= 0 ;       n=sz(s);       rep(i,0,2)rep(car,0,M)rep(pos,0,K)rep(neg,0,K) dp[i][car][pos][neg]=inf;      rep(pos,0,K)rep(neg,0,K) dp[0][0+M/2][pos][neg]=0;      for(int i=n-1;i>=0;--i) {       rep(car,-M/2,M/2+1)rep(pos,0,K)rep(neg,0,K) {        int &ans=dp[1][car+M/2][pos][neg];        if(pos) ans=min(ans,dp[1][car+M/2][pos-1][neg]);        if(neg) ans=min(ans,dp[1][car+M/2][pos][neg-1]);        int now=s[i]- 0 ,tot=pos-neg+car;        if((tot%10+10)%10==now) {         int ncar=tot/10+(tot>0?0:-1)*(bool)(tot%10)+M/2;         if(ncar<0 or ncar>=M) continue;         ans=min(ans,dp[0][ncar][pos][neg]+pos+neg);        }       }       rep(car,-M/2,M/2+1)rep(pos,0,K)rep(neg,0,K) {        dp[0][car+M/2][pos][neg]=dp[1][car+M/2][pos][neg];        dp[1][car+M/2][pos][neg]=inf;       }      }      cout<<dp[0][0+M/2][K-1][K-1];      /*       memset(dp,-1,sizeof(dp));       cout<<f(0,0,K-1,K-1);       */              return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long mx, mx2;   void solve(long long m, long long blocks, long long sum) {     if (m == 0) {       if (blocks > mx) {         mx = blocks;         mx2 = sum;       } else if (blocks == mx && sum > mx2) {         mx2 = sum;       }       return;     }     long long x = 1;     while ((x + 1) * (x + 1) * (x + 1) <= m) x++;     solve(m - x * x * x, blocks + 1, sum + x * x * x);     solve(x * x * x - 1 - (x - 1) * (x - 1) * (x - 1), blocks + 1,           sum + (x - 1) * (x - 1) * (x - 1));   }   int main() {     long long m;     cin >> m;     solve(m, 0, 0);     cout << mx <<     << mx2 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int num[300];   int main() {     int n, a, minn = 99999, sum = 0, sum2 = 0;     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &num[i]);     for (int i = 1; i <= n; i++) sum += abs(num[i]);     printf( %d n , sum);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     long long t, n, m;     cin >> t;     while (t--) {       cin >> n >> m;       if (n == 1 || m == 1 || (n == 2 && m == 2)) {         cout <<  YES  << endl;       } else {         cout <<  NO  << endl;       }     }   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma )   #pragma GCC optimize( unroll-loops )   using namespace std;   const double eps = 1e-12;   const long long MOD = 1e9 + 7;   const double PI = acos(-1.0);   const long long INF = 1e18;   void solve() {     long long n, m, k;     cin >> n >> m >> k;     vector<long long> a(n);     for (long long i = 0; i < n; i++) {       cin >> a[i];     }     vector<long long> pref(n + 1, 0);     pref[1] = a[0];     for (long long i = 2; i <= n; i++) {       pref[i] = pref[i - 1] + a[i - 1];     }     vector<long long> sums(n + 1, 0);     for (long long i = 0; i + m <= n; i++) {       sums[i] = pref[i + m] - pref[i];     }     long long dp[n + 1][k + 1];     memset(dp, 0, sizeof(dp));     for (long long i = 0; i <= n; i++) {       for (long long j = 1; j <= k; j++) {         if (i < j - 1) continue;         dp[i][j] = max(((i - m >= 0) ? dp[i - m][j - 1] : 0) + sums[i],                        ((i - 1 >= 0) ? dp[i - 1][j] : 0));       }     }     long long ans = 0;     for (long long i = 0; i <= n; i++) {       ans = max(dp[i][k], ans);     }     cout << ans <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     long long t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, c, d;     cin >> a >> b >> c >> d;     int k = a + b + c + d;     if (k % 2 == 0) {       if (a + b == k / 2 || a + c == k / 2 || a + d == k / 2 || b + c == k / 2 ||           b + d == k / 2 || c + d == k / 2 || a + b + c == k / 2 ||           a + b + d == k / 2 || a + c + d == k / 2 || b + c + d == k / 2)         cout <<  YES n ;       else         cout <<  NO n ;     } else       cout <<  NO n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 510;   const int MAXQ = 100010;   const double EPS = 1e-10;   const double DINF = 1e30;   const int IINF = ~0U >> 3;   const long long LINF = 1LL << 61;   void debug() { cout <<  error_here  << endl; }   char str[MAXN][MAXN];   int a[MAXN][MAXN], b[MAXN][MAXN];   int n, m, k;   void init() {     scanf( %d%d%d , &n, &m, &k);     for (int i = 0; i < n; ++i) scanf( %s , str[i]);     memset(a, 0, sizeof(a));     memset(b, 0, sizeof(b));     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j) a[i][j] = (str[i - 1][j - 1] ==  1 );     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j)         if (a[i][j] && a[i - 1][j] && a[i + 1][j] && a[i][j - 1] && a[i][j + 1])           b[i][j] = 1;     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j)         b[i][j] += b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1];   }   int get_sum(int x, int y, int xx, int yy) {     return b[xx][yy] - b[xx][y - 1] - b[x - 1][yy] + b[x - 1][y - 1];   }   void solve() {     long long ans = 0;     for (int u = 1; u <= n; ++u)       for (int v = u + 2; v <= n; ++v) {         for (int i = 1, j = 3; i <= m && j <= m;) {           if (get_sum(u + 1, i + 1, v - 1, j - 1) >= k)             ans += m - j + 1, i++;           else             j++;         }       }     cout << ans << endl;   }   int main() {     init();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int comp(pair<long long int, long long int> a,                      pair<long long int, long long int> b) {     if (a.first != b.first) return a < b;     return a.second < b.second;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int n;     cin >> n;     vector<pair<long long int, long long int>> v;     for (long long int i = 0; i < n; i++) {       long long int x, y;       cin >> x >> y;       x += 1000;       y += 1000;       v.push_back({x, y});     }     sort(v.begin(), v.end(), comp);     long long int cnt = 0;     for (long long int i = 0; i < n; i++) {       for (long long int j = i + 1; j < n; j++) {         if (((((v[i].first + v[j].first)) % 2 == 0) &&              (((v[i].second + v[j].second)) % 2 == 0))) {           long long int x1, y1;           x1 = (v[i].first + v[j].first) / 2;           y1 = (v[i].second + v[j].second) / 2;           long long int lo = i + 1, hi = j - 1, id = -1;           while (lo <= hi) {             long long int mid = (lo + hi) / 2;             if (v[mid].first >= x1) {               id = mid;               hi = mid - 1;             } else if (v[mid].first < x1)               lo = mid + 1;           }           if (id != -1) {             while (v[id].first == x1 && id < n) {               if (v[id].second == y1) {                 cnt++;                 break;               }               id++;             }           }         }       }     }     cout << cnt;   }


#include <bits/stdc++.h>   using namespace std;   const int mx = 1e5 + 10;   int p[mx];   int main() {     int n;     cin >> n;     if (n % 4 > 1) {       puts( -1 );       return 0;     }     int l = 1, r = n;     while (l < r) {       p[l] = l + 1;       p[l + 1] = r;       p[r] = r - 1;       p[r - 1] = l;       l += 2;       r -= 2;     }     if (l == r) p[l] = l;     for (int i = 1; i <= n; i++) printf( %d  , p[i]);     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int power(long long int a, long long int b) {     long long int res = 1;     a %= 1000000007;     assert(b >= 0);     for (; b; b >>= 1) {       if (b & 1) res = res * a % 1000000007;       a = a * a % 1000000007;     }     return res;   }   long long int modInverse(long long int a) { return power(a, 1000000007 - 2); }   long long int gcd(long long int a, long long int b) {     if (b == 0) return a;     a %= b;     return gcd(b, a);   }   const int N = 500023;   bool vis[N];   vector<int> adj[N];   void solve() {     int n;     cin >> n;     vector<long long int> a(n + 1);     for (long long int i = 0; i < n; i++) cin >> a[i + 1];     int moves = 1;     long long int by_ana = 0;     long long int by_bob = 0;     int prev_a = 1;     int prev_b = n + 1;     by_ana += a[1];     int to_comp_by_bob = a[1];     int to_comp_by_ana = 0;     int turn = 2;     int f = 0;     int eaten[n + 1];     for (int i = 1; i <= n; i++) eaten[i] = 0;     eaten[1] = 1;     while (1) {       moves++;       int temp = 0;       if (prev_a == prev_b - 1 || prev_a > n || prev_b < 1) {         moves--;         break;       }       if (turn == 2) {         while (temp <= to_comp_by_bob) {           if (prev_b == prev_a || prev_b < 1) {             f = 1;             break;           }           prev_b--;           if (eaten[prev_b] == 1) {             f = 1;             break;           }           temp += a[prev_b];           by_bob += a[prev_b];           eaten[prev_b] = 1;         }         to_comp_by_ana = temp;         turn = 1;         if (f == 1) break;       } else if (turn == 1) {         while (temp <= to_comp_by_ana) {           if (prev_b == prev_a || prev_a > n) {             f = 1;             break;           }           prev_a++;           if (eaten[prev_a] == 1) {             f = 1;             break;           }           temp += a[prev_a];           by_ana += a[prev_a];           eaten[prev_a] = 1;         }         if (f == 1) break;         to_comp_by_bob = temp;         turn = 2;       }       if (f == 1) {         if (temp == 0) moves--;         break;       }     }     if (n == 1) moves = 1;     cout << moves <<     << by_ana <<     << by_bob << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int T = 1;     cin >> T;     int t = 0;     while (t++ < T) {       solve();     }     cerr <<  Time :   << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC          <<  ms n ;   }


#include <bits/stdc++.h>   using namespace std;   long long int pp(long long int a, long long int b) {     if (b == 0) return 1;     long long int t = pp(a, b / 2);     if (b % 2 == 0)       return t * t;     else       return t * t * a;   }   string ss(string s, long long int j) {     for (long long int i = 0; i < j; i++) {       if (s[i] ==  0 )         s[i] =  1 ;       else         s[i] =  0 ;     }     long long int l = 0, r = j - 1;     while (l < r) {       swap(s[l], s[r]);       l++;       r--;     }     return s;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     long long int q;     cin >> q;     while (q--) {       long long int i, j, k, n, m, x, y, l, r, c = 0;       string s, s1, s2 =   ;       cin >> n;       cin >> s >> s1;       vector<long long int> v;       for (i = n - 1; i > 0; i--) {         if (s[i] == s1[i])           continue;         else {           if ((s1[i] ==  0  && s[i] ==  1  && s[0] ==  1 ) ||               (s1[i] ==  1  && s[i] ==  0  && s[0] ==  0 )) {             v.push_back(i + 1);             s = ss(s, i + 1);           } else if (s1[i] ==  0  && s[i] ==  1  && s[0] ==  0 ) {             v.push_back(1);             s[0] =  1 ;             v.push_back(i + 1);             s = ss(s, i + 1);           } else {             v.push_back(1);             s[0] =  0 ;             v.push_back(i + 1);             s = ss(s, i + 1);           }         }       }       if (s[0] != s1[0]) v.push_back(1);       cout << v.size() <<    ;       for (i = 0; i < v.size(); i++) cout << v[i] <<    ;       cout << endl;     }   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:66777216 )   using namespace std;   static unsigned rnd() {     static int second = 124612741;     second ^= second << 13;     second ^= second >> 17;     return second ^= second << 5;   }   inline int fpow(int a, int st, int mod) {     int ans = 1;     while (st) {       if (st % 2) ans = (long long)ans * a % mod;       a = (long long)a * a % mod;       st /= 2;     }     return ans;   }   int invmod(int first, int second) {     int a = 1, b = 0, k;     for (; second; swap(first, second), swap(a, b)) {       k = first / second;       first -= second * k;       a -= b * k;     }     return a + max(b, 0);   }   inline int calcBits(int a) { return __builtin_popcount(a); }   struct Hash {     Hash() : x1(0), x2(0) {}     Hash(const int& x1, const int& x2) : x1(x1), x2(x2) {}     int x1, x2;     static int mod1, mod2;     static int p1, p2;     static int invp1, invp2;     static bool initCalled;     static const Hash ONE, ZERO, INVALID;     static Hash UNIT, INV_UNIT;     static bool prime(int a) {       if (a == 1) return 0;       for (int i = 2; i * i <= a; ++i) {         if (a % i == 0) return false;       }       return true;     }     static int pickModHelper(int start, int bad) {       int mn = 1000000000, best = -1;       for (int _n(start + 10000), i(start); i <= _n; i++) {         if (i == bad || !prime(i)) continue;         int cur = calcBits(i);         if (cur < mn) {           mn = cur;           best = i;         }       }       return best;     }     static void init() {       if (initCalled) return;       initCalled = true;       mod1 = pickModHelper(1000000000 + rnd() % 10000000, -1);       while (!prime(mod1)) ++mod1;       mod2 = pickModHelper(1000000000 + rnd() % 10000000, mod1);       invp1 = fpow(p1, mod1 - 2, mod1);       invp2 = fpow(p2, mod2 - 2, mod2);       INV_UNIT = Hash(invp1, invp2);     }     inline Hash& operator+=(const Hash& other) {       x1 += other.x1;       if (x1 >= Hash::mod1) x1 -= Hash::mod1;       x2 += other.x2;       if (x2 >= Hash::mod2) x2 -= Hash::mod2;       return *this;     }     inline Hash& operator-=(const Hash& other) {       x1 -= other.x1;       if (x1 < 0) x1 += Hash::mod1;       x2 -= other.x2;       if (x2 < 0) x2 += Hash::mod2;       return *this;     }     inline Hash& operator*=(const Hash& other) {       x1 = (long long)x1 * other.x1 % Hash::mod1;       x2 = (long long)x2 * other.x2 % Hash::mod2;       return *this;     }     inline Hash& operator*=(const int& other) {       x1 = (long long)x1 * other % Hash::mod1;       x2 = (long long)x2 * other % Hash::mod2;       return *this;     }     inline Hash& operator^=(const int& power) {       int st = power;       Hash ans(1, 1);       while (st) {         if (st % 2) ans *= *this;         x1 = (long long)x1 * x1 % Hash::mod1;         x2 = (long long)x2 * x2 % Hash::mod2;         st /= 2;       }       return *this;     }     inline bool operator==(const Hash& other) const {       return x1 == other.x1 && x2 == other.x2;     }   };   bool Hash::initCalled = false;   int Hash::mod1 = 0;   int Hash::mod2 = 0;   int Hash::p1 = 23561;   int Hash::p2 = 10000019;   int Hash::invp1 = -1;   int Hash::invp2 = -1;   const Hash Hash::ONE(1, 1);   const Hash Hash::ZERO(0, 0);   const Hash Hash::INVALID(-1, -1);   Hash Hash::UNIT(Hash::p1, Hash::p2);   Hash Hash::INV_UNIT(-1, -1);   inline Hash operator+(const Hash& a, const Hash& b) {     Hash ans(a.x1 + b.x1, a.x2 + b.x2);     if (ans.x1 >= Hash::mod1) ans.x1 -= Hash::mod1;     if (ans.x2 >= Hash::mod2) ans.x2 -= Hash::mod2;     return ans;   }   inline Hash operator-(const Hash& a, const Hash& b) {     Hash ans(a.x1 - b.x1, a.x2 - b.x2);     if (ans.x1 < 0) ans.x1 += Hash::mod1;     if (ans.x2 < 0) ans.x2 += Hash::mod2;     return ans;   }   inline Hash operator*(const Hash& a, const Hash& b) {     return Hash((long long)a.x1 * b.x1 % Hash::mod1,                 (long long)a.x2 * b.x2 % Hash::mod2);   }   inline Hash operator*(const Hash& a, const int& b) {     return Hash((long long)a.x1 * b % Hash::mod1,                 (long long)a.x2 * b % Hash::mod2);   }   inline Hash operator^(const Hash& a, const int& power) {     int st = power;     Hash ans(1, 1), cur(a);     while (st) {       if (st % 2) ans *= cur;       cur *= cur;       st /= 2;     }     return ans;   }   inline Hash fpow(const Hash& a, const int& power) { return a ^ power; }   inline Hash invmod(const Hash& a) {     return Hash(invmod(a.x1, Hash::mod1), invmod(a.x2, Hash::mod2));   }   template <class H>   struct HashString : public string {     HashString() : string() { precalc(0); }     HashString(const HashString& other) : string(other) { precalc(size()); }     HashString(HashString&& other)         : hh(std::move(other.hh)), string(std::move(other)) {       precalc(size());     }     HashString(const string& other) : string(other) {       precalc(size());       build();     }     HashString(string&& other) : string(std::move(other)) {       precalc(size());       build();     }     HashString(const char* other) : string(other) {       precalc(size());       build();     }     inline void clear() {       hh.clear();       string::clear();     }     inline static H getPow(int p) {       if (p < haveN) {         return pp[p];       } else {         return fpow(H::UNIT, p);       }     }     inline static H getInvPow(size_t p) {       if (p < invpp.size()) {         return invpp[p];       } else {         return fpow(H::INV_UNIT, p);       }     }     inline H getInvRegister(const H& p) const {       if (p == register_) {         return registerValue_;       } else {         register_ = p;         return registerValue_ = invmod(p);       }     }     static void precalc(int N = 0) {       Hash::init();       if (haveN >= N) return;       pp.resize(N);       invpp.resize(N);       if (!haveN) {         pp[0] = H::ONE;         invpp[0] = H::ONE;         haveN = 1;       }       for (int i = haveN; i < N; ++i) {         pp[i] = pp[i - 1] * H::UNIT;         invpp[i] = invpp[i - 1] * H::INV_UNIT;       }       haveN = N;     }     inline void build() {       int curSize = size();       if (haveN <= curSize) {         precalc(curSize);       }       for (int i = hh.size(); i < curSize; ++i) {         H pr = i ? hh.back() : H::ZERO;         hh.push_back(pr + getPow(i) * (*this)[i]);       }     }     inline H getHash(int pos, int len) const {       if (len <= 0) return H::ZERO;       const int S = size();       if (pos + len - 1 < S) {         H ans = hh[pos + len - 1];         if (pos) {           ans -= hh[pos - 1];           ans *= getInvPow(pos);         }         return ans;       } else {         H head = hh.back();         if (pos) {           head -= hh[pos - 1];           head *= getInvPow(pos);         }         int full = -1, en = -1;         H mid = H::ZERO;         int midlen = 0;         if (len <= S) {           en = pos + len - 1 - S;           H tail = hh[en];           full = 0;           return head + tail * getPow(S - pos);         } else {           en = (pos + len - 1) % S;           H tail = hh[en];           full = (len - (S - pos) - (en + 1)) / S;           mid = hh.back() * (H::ONE - getInvPow(full * S)) *                 getInvRegister(H::ONE - getInvPow(S));           return head + mid * getPow(S - pos) + tail * getPow(len - (en + 1));         }       }     }     inline HashString& operator+=(const char& ch) {       string::operator+=(ch);       build();       return *this;     }     inline HashString& operator+=(const HashString& other) {       string::operator+=(other);       build();       return *this;     }     inline bool operator==(const HashString& other) const {       return hh.back() == other.hh.back();     }     inline bool operator<(const HashString& other) const {       int s1 = size();       int s2 = other.size();       const int S = min(s1, s2);       int l = 0, r = S + 1;       while (r - l > 1) {         int xx = (r + l) / 2;         if (getHash(0, xx) == other.getHash(0, xx))           l = xx;         else           r = xx;       }       char c1 = 0, c2 = 0;       if (l < s1) c1 = (*this)[l];       if (l < s2) c2 = other[l];       return c1 < c2;     }     inline bool operator<=(const HashString& other) const {       return (*this == other) || (*this < other);     }     inline bool operator>(const HashString& other) const {       return !(*this == other) && !(*this < other);     }     inline bool operator>=(const HashString& other) const {       return !(*this < other);     }     inline bool operator!=(const HashString& other) const {       return !(*this == other);     }     inline void reserve(int N) {       hh.reserve(N);       string::reserve(N);     }     vector<H> hh;     static vector<H> pp, invpp;       private:     mutable H register_, registerValue_;     static int haveN;   };   template <>   int HashString<Hash>::haveN = 0;   template <>   vector<Hash> HashString<Hash>::pp = vector<Hash>();   template <>   vector<Hash> HashString<Hash>::invpp = vector<Hash>();   template <class H, bool cyclic = true>   struct HashStringPiece {     explicit HashStringPiece(const HashString<H>& s, int pos, int len)         : s(s), pos(pos), len(len), hash(H::INVALID) {}     inline H getHash() const {       if (hash == H::INVALID) hash = s.getHash(pos, len);       return hash;     }     inline H getPartialHash(int needLen) const { return s.getHash(pos, needLen); }     inline char getChar(int i) const {       i += pos;       const int S = s.size();       if (i < S) return s[i];       if (!cyclic) return 0;       i -= S;       if (i < S) return s[i];       return s[i % S];     }     const HashString<H>& s;     const int pos, len;     mutable H hash;   };   template <class H, class T>   char getHSPRangeChar(T beg, T en, int pos) {     for (T it = beg; it != en; ++it) {       if (pos < it->len) {         return it->getChar(pos);       }       pos -= it->len;     }     return 0;   }   template <class H, class T>   H getHSPRangeHash(T beg, T en, int len) {     H ans = H::ZERO;     int cur = 0;     for (T it = beg; it != en; ++it) {       if (len >= it->len) {         ans += it->getHash() * it->s.getPow(cur);         cur += it->len;         len -= it->len;       } else {         ans += it->getPartialHash(len) * it->s.getPow(cur);         break;       }     }     return ans;   }   int TB, IT;   template <class H, class T>   inline int compareHSPRanges(T beg1, T en1, int len1, T beg2, T en2, int len2) {     if (beg1 == en1 && beg2 == en2) return 0;     if (beg1 == en1) return -1;     if (beg2 == en2) return 1;     int l = 0, r = min(len1, len2) + 1;     while (r - l > 1) {       int xx = (r + l) / 2;       auto h1 = getHSPRangeHash<H, T>(beg1, en1, xx);       auto h2 = getHSPRangeHash<H, T>(beg2, en2, xx);       if (h1 == h2)         l = xx;       else         r = xx;     }     char c1 = getHSPRangeChar<H, T>(beg1, en1, l);     char c2 = getHSPRangeChar<H, T>(beg2, en2, l);     if (c1 < c2)       return -1;     else if (c1 == c2)       return 0;     else       return 1;   }   template <class H, class T>   inline int compareHSPRanges(T beg1, T en1, T beg2, T en2) {     int len1 = 0;     for (T it = beg1; it != en1; ++it) {       len1 += it->len;     }     int len2 = 0;     for (T it = beg2; it != en2; ++it) {       len2 += it->len;     }     return compareHSPRanges<H, T>(beg1, en1, len1, beg2, en2, len2);   }   int a, b, c, d, n, m, k;   char str[1000002];   int bal[2000002];   HashString<Hash> s;   inline string build(pair<pair<int, int>, int> a) {     string ans =   ;     for (int _n((a.first.first) - 1), i(0); i <= _n; i++) ans +=  ( ;     for (int _n((n)-1), i(0); i <= _n; i++) {       int cur = a.second + i;       if (cur >= n) cur -= n;       ans += str[cur];     }     for (int _n((a.first.second) - 1), i(0); i <= _n; i++) ans +=  ) ;     return ans;   }   HashString<Hash> op, cl;   inline bool better(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b,                      const HashString<Hash>& s) {     HashStringPiece<Hash> A[3] = {HashStringPiece<Hash>(op, 0, a.first.first),                                   HashStringPiece<Hash>(s, a.second, n),                                   HashStringPiece<Hash>(cl, 0, a.first.second)};     HashStringPiece<Hash> B[3] = {HashStringPiece<Hash>(op, 0, b.first.first),                                   HashStringPiece<Hash>(s, b.second, n),                                   HashStringPiece<Hash>(cl, 0, b.first.second)};     int t = compareHSPRanges<Hash>(A, A + 3, B, B + 3);     if (t == -1)       return 1;     else       return 0;   }   int main() {     gets(str);     n = (int)strlen(str);     HashString<Hash> s(str);     HashString<Hash>::precalc(2 * n + 1);     op.reserve(n + 1);     cl.reserve(n + 1);     for (int _n((n)-1), i(0); i <= _n; i++) {       op +=  ( ;       cl +=  ) ;     }     c = 0;     for (int _n((n)-1), i(0); i <= _n; i++) {       if (str[i] ==  ( )         ++c;       else         --c;     }     bal[0] = 0;     for (int _n((2 * n) - 1), i(0); i <= _n; i++) {       if (i) bal[i] = bal[i - 1];       int cur = i;       if (i >= n) cur = i - n;       if (str[cur] ==  ( )         ++bal[i];       else         --bal[i];     }     int blen = 1000000000;     pair<pair<int, int>, int> best = make_pair(make_pair(0, 0), 0);     stack<pair<int, int> > s1, s2;     for (int _n((n)-1), i(0); i <= _n; i++) {       int new_element = bal[i];       int minima = s1.empty() ? new_element : min(new_element, s1.top().second);       s1.push(make_pair(new_element, minima));     }     for (int _n((n)-1), cyc(0); cyc <= _n; cyc++) {       int current_minimum = -1000000000;       if (s1.empty() || s2.empty())         current_minimum = s1.empty() ? s2.top().second : s1.top().second;       else         current_minimum = min(s1.top().second, s2.top().second);       int p = 0;       if (cyc) p = bal[cyc - 1];       int mn = current_minimum - p;       int beg = max(0, -mn);       int en = c + beg;       int len = beg + en + n;       if (len < blen ||           (len == blen && better(make_pair(make_pair(beg, en), cyc), best, s))) {         blen = len;         best = make_pair(make_pair(beg, en), cyc);       }       if (s2.empty()) {         while (!s1.empty()) {           int element = s1.top().first;           s1.pop();           int minima = s2.empty() ? element : min(element, s2.top().second);           s2.push(make_pair(element, minima));         }       }       s2.pop();       int new_element = bal[cyc + n];       int minima = s1.empty() ? new_element : min(new_element, s1.top().second);       s1.push(make_pair(new_element, minima));     }     string ans = build(best);     printf( %s n , ans.c_str());   }


#include <bits/stdc++.h>   using namespace std;   template <typename A>   ostream &operator<<(ostream &cout, vector<A> const &v);   template <typename A, typename B>   ostream &operator<<(ostream &cout, pair<A, B> const &p) {     return cout <<  (  << p.first <<  ,   << p.second <<  ) ;   }   template <typename A>   ostream &operator<<(ostream &cout, vector<A> const &v) {     cout <<  [ ;     for (int i = 0; i < v.size(); i++) {       if (i) cout <<  ,  ;       cout << v[i];     }     return cout <<  ] ;   }   template <typename A, typename B>   istream &operator>>(istream &cin, pair<A, B> &p) {     cin >> p.first;     return cin >> p.second;   }   mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());   void usaco(string filename) {     freopen((filename +  .in ).c_str(),  r , stdin);     freopen((filename +  .out ).c_str(),  w , stdout);   }   const long double pi = 3.14159265358979323846;   const long long mod = 1000000007;   struct dsu {     int n;     vector<int> sizes, marks;     vector<vector<int>> graph;     void init(int rn) {       n = rn;       sizes = vector<int>(n);       fill(sizes.begin(), sizes.end(), 1);       marks = vector<int>(n);       for (int i = 0; i < n; i++) marks[i] = i;       graph = vector<vector<int>>(n);     }     void mark(int m, int node) {       if (marks[node] == m) return;       marks[node] = m;       for (int i : graph[node]) mark(m, i);     }     bool merge(int a, int b) {       if (marks[a] == marks[b]) return 0;       graph[a].push_back(b);       graph[b].push_back(a);       if (sizes[marks[a]] > sizes[marks[b]]) swap(a, b);       sizes[marks[b]] += sizes[marks[a]];       sizes[marks[a]] = 0;       mark(marks[b], a);       return 1;     }     int find(int n) { return marks[n]; }   };   long long n, m, k, q, l, r, x, y, z;   const long long template_array_size = 1e6 + 6884;   long long a[template_array_size];   long long b[template_array_size];   long long c[template_array_size];   string second, t;   long long ans = 0;   pair<long long, pair<long long, long long>> vals[200005];   long long root[100005];   bool cnt[100005];   dsu d;   void solve(int tc = 0) {     cin >> m >> n;     for (long long i = 0; i < m; i++) cin >> a[i];     for (long long i = 0; i < n; i++) cin >> b[i];     long long ans = 0, second = 0;     for (long long i = 0; i < m; i++) {       cin >> x;       for (long long j = 0; j < x; j++) {         cin >> y;         --y;         ans += a[i] + b[y];         vals[second++] = make_pair(-a[i] - b[y], make_pair(i, y));       }     }     sort(vals, vals + second);     memset(cnt, 0, sizeof(cnt));     d.init(n);     for (long long i = 0; i < second; i++) {       long long a = vals[i].second.first, b = vals[i].second.second,                 val = vals[i].first;       if (!cnt[a]) {         cnt[a] = 1;         root[a] = b;         ans += val;       } else {         if (d.merge(root[a], b)) ans += val;       }     }     cout << ans <<   n ;   }   int main() {     { ios_base::sync_with_stdio(false); }     { cin.tie(NULL); }     cout << setprecision(12) << fixed;     int tc = 1;     for (int t = 0; t < tc; t++) solve(t);   }


#include <bits/stdc++.h>   using namespace std;   FILE *in;   FILE *out;   const int MAX = 1048576;   const int TREE = 2097152;   const int MOD = 1000000007;   int n;   int a[MAX];   int tree[TREE];   bool seen[MAX];   int query(int idx1, int idx2) {     idx1 += (TREE >> 1);     idx2 += (TREE >> 1);     if (idx1 == idx2) return tree[idx1];     int ret = 0;     ret += tree[idx1];     if (ret >= MOD) ret -= MOD;     int flag1 = !(idx1 & 1);     idx1 >>= 1;     ret += tree[idx2];     if (ret >= MOD) ret -= MOD;     int flag2 = (idx2 & 1);     idx2 >>= 1;     while (idx1 != idx2) {       if (flag1) {         ret += tree[(idx1 << 1) + 1];         if (ret >= MOD) ret -= MOD;       }       if (flag2) {         ret += tree[(idx2 << 1) + 0];         if (ret >= MOD) ret -= MOD;       }       flag1 = !(idx1 & 1);       idx1 >>= 1;       flag2 = (idx2 & 1);       idx2 >>= 1;     }     return ret;   }   void update(int idx, int val) {     idx += (TREE >> 1);     while (idx) {       tree[idx] += val;       if (tree[idx] >= MOD) tree[idx] -= MOD;       idx >>= 1;     }   }   int main(void) {     in = stdin;     out = stdout;     fscanf(in,  %d , &n);     for (int i = 0; i < n; i++) fscanf(in,  %d , &a[i]);     int ans = 0;     memset(tree, 0, sizeof(tree));     for (int i = n - 1; i >= 0; i--) {       int cur = ((long long)a[i] * (query(a[i], MAX - 1) + 1)) % MOD;       ans += cur;       if (ans >= MOD) ans -= MOD;       int sub = query(a[i], a[i]);       ans -= sub;       if (ans < 0) ans += MOD;       update(a[i], (cur - sub + MOD) % MOD);     }     fprintf(out,  %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class A, class B>   ostream& operator<<(ostream& o, pair<A, B>& p) {     return o <<  (  << p.first <<  ,   << p.second <<  ) ;   }   template <class T>   ostream& operator<<(ostream& o, const vector<T>& v) {     o <<  { ;     for (typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)       o << *it <<  ,  ;     return o <<  } ;   }   struct debugger {     ostream& out;     bool first;     void start() { first = true; }     debugger(ostream& out) : out(out) { start(); }     void nl() {       out << endl;       start();     }     template <class T>     debugger& operator,(T& v) {       if (first)         first = false;       else         out <<  ,  ;       out << v;       return *this;     }     template <class T>     debugger& operator,(pair<T*, int> arr) {       out <<  { ;       for (typeof((arr.second) - 1) i = (0); i <= ((arr.second) - 1); i++) {         out << arr.first[i] <<  ,  ;       }       out <<  } ;       return *this;     }   } dbg(cerr);   long long pyra(long long n) { return (n + 1) * (n + 1); }   long long incr(long long n) { return (n * (n + 1)) / 2; }   long long f(long long x, long long y, long long step, long long n) {     long long ret = 2 * pyra(step) - (2 * step + 1);     if (x + step > n) {       ret -= pyra(x + step - n - 1);     }     if (y + step > n) {       ret -= pyra(y + step - n - 1);     }     if (x - step < 1) {       ret -= pyra(1 - (x - step) - 1);     }     if (y - step < 1) {       ret -= pyra(1 - (y - step) - 1);     }     if (step - 1 - (n - x) - (n - y) > 0) {       ret += incr(step - 1 - (n - x) - (n - y));     }     if (step - 1 - (n - x) - (y - 1) > 0) {       ret += incr(step - 1 - (n - x) - (y - 1));     }     if (step - 1 - (x - 1) - (y - 1) > 0) {       ret += incr(step - 1 - (x - 1) - (y - 1));     }     if (step - 1 - (x - 1) - (n - y) > 0) {       ret += incr(step - 1 - (x - 1) - (n - y));     }     return ret;   }   int main() {     long long n, x, y, c, lo, hi;     cin >> n >> x >> y >> c;     lo = 0;     hi = 1e9 + 10;     {}     while (lo < hi) {       long long mid = (lo + hi) / 2;       if (f(x, y, mid, n) < c)         lo = mid + 1;       else         hi = mid;     }     cout << lo << endl;     return 0;   }


#include <bits/stdc++.h>   int minDig(uint64_t n) {     int k = n % 10, min = k;     n /= 10;     while (n != 0) {       k = n % 10;       if (k <= min) min = k;       n /= 10;     }     return min;   }   int maxDig(uint64_t n) {     int k = n % 10, max = k;     n /= 10;     while (n != 0) {       k = n % 10;       if (k >= max) max = k;       n /= 10;     }     return max;   }   int searchzero(uint64_t n) {     while (n != 0) {       if (n % 10 == 0) return 1;       n /= 10;     }     return 0;   }   int main(void) {     int t;     uint64_t a, k;     scanf( %d , &t);     while (t--) {       scanf( %llu %llu , &a, &k);       for (uint64_t i = 1; i < k; i++) {         a += (minDig(a) * maxDig(a));         if (searchzero(a) == 1) break;       }       printf( %llu n , a);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename Arg1>   void __f(const char* name, Arg1&& arg1) {     std::cerr << name <<   :   << arg1 << endl;   }   template <typename Arg1, typename... Args>   void __f(const char* names, Arg1&& arg1, Args&&... args) {     const char* comma = strchr(names + 1,  , );     std::cerr.write(names, comma - names) <<   :   << arg1 <<   |  ;     __f(comma + 1, args...);   }   bool findfxn(string a, string b) {     size_t found = a.find(b);     if (found != string::npos)       return 1;     else       return 0;   }   long long int power(long long int a, long long int b) {     if (b == 0) {       return 1;     } else if (b % 2 == 0) {       return power(a * a, b / 2);     } else {       return a * power(a * a, (b - 1) / 2);     }   }   void solve() {     long long int n, p, k, z = 0;     cin >> n >> p >> k;     int a[n];     int sum[n + 10];     for (int i = 0; i < (n); i++) {       cin >> a[i];     }     sort(a, a + n);     int i = 0;     for (i = 0; i < n; i++) {       if (i > 1) {         sum[i] = sum[i - 2] + a[i];       } else         sum[i] = a[i];       if (sum[i] > p) {         break;       }     }     cout << i << endl;     return;   }   signed main() {     std::ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long int t = 1;     cin >> t;     while (t--) solve();     return 0;     return 0;   }


#include<bits/stdc++.h>   #define L(i, j, k) for(int i = (j), i##E = k; i <= i##E; i++)    #define R(i, j, k) for(int i = (j), i##E = k; i >= i##E; i--)   #define mkp make_pair   #define pii pair<int, int>   #define ll long long   #define db double   #define sz(a) a.size()   #define x first   #define y second   #define vi vector<int>   using namespace std;   const int N = 1e4, M = 100, mod = 998244353;   int m;   struct Matrix {    int f[M][M];    Matrix (int x = 1) {     L(i, 1, m) L(j, 1, m) f[i][j] = (i == j ? x : 0);    }   } ;   Matrix operator * (Matrix aa, Matrix bb) {    Matrix res(0);    L(i, 1, m) L(j, 1, m) L(k, 1, m) (res.f[i][j] += (ll) aa.f[i][k] * bb.f[k][j] % mod) %= mod;    return res;   }   Matrix operator ^ (Matrix x, ll y) {    Matrix res(1);    for(; y; x = x * x, y >>= 1) if(y & 1) res = res * x;    return res;   }      int n, nousex, otp, s[N], tot, v[N], sv[N], ns;   ll a[N], p[N], mx, val, arr[N], sl[N], sr[N];   void calc(int l, int r) {    ll mx = 0;    p[l] = 0, tot = 0;    L(i, l + 1, r) {     sl[i] = (p[i - 1] + (a[i] < 0 ? -1 : 1)), p[i] = p[i - 1] + a[i], sr[i] = p[i];     if(sl[i] > sr[i]) swap(sl[i], sr[i]);     mx = max(mx, p[i]);     }    if(val != mx) return;    sl[l + 1] = 0;    L(i, l + 1, r)      arr[++tot] = sl[i] - 1, arr[++tot] = sl[i], arr[++tot] = sl[i] + 1,      arr[++tot] = sr[i] - 1, arr[++tot] = sr[i], arr[++tot] = sr[i] + 1;    sort(arr + 1, arr + tot + 1), tot = unique(arr + 1, arr + tot + 1) - arr - 1;    m = r - l;    memset(v, 0, sizeof(v));    v[1] = 1, arr[0] = -1;    L(k, 1, tot) if(arr[k] <= mx) {     Matrix G(0);     L(i, l + 1, r) if(sl[i] <= arr[k - 1] + 1 && arr[k] <= sr[i]) {      L(j, l + 1, i - 1) G.f[j - l][i - l] = 1;      if(a[i] > 0) G.f[i - l][i - l] = 1;     }     G = G ^ (arr[k] - arr[k - 1]);     L(i, 1, m) L(j, 1, m) (sv[j] += (ll) v[i] * G.f[i][j] % mod) %= mod;     L(i, 1, m) v[i] = sv[i], sv[i] = 0;    }    L(i, 1, m) (ns += v[i]) %= mod;   }   void Main() {    cin >> n >> nousex;    L(i, 1, n) {     cin >> a[i];     if(a[i]) a[++otp] = a[i];    }    n = otp, otp = 0;    L(i, 1, n)      if((a[i] > 0 && a[i - 1] > 0) || (a[i] < 0 && a[i - 1] < 0)) a[otp] += a[i];     else a[++otp] = a[i];    n = otp;    L(i, 1, n) mx += a[i], val = max(val, mx), mx = max(mx, 0ll);    cout << val + 1 <<    ;    if(val == 0) {     int sum = 1;     L(i, 1, n) (sum += abs(a[i]) % mod) %= mod;     return cout << sum <<   n , void();    }    int top = 0;    mx = 0;    L(i, 1, n) {     mx += a[i];     if(mx < 0) s[++top] = i, mx = 0;    }    s[++top] = n + 1;    L(i, 1, top) calc(s[i - 1], s[i] - 1);    cout << ns <<   n ;   }   int main() {    ios::sync_with_stdio(false);    cin.tie(0); cout.tie(0);    Main();    return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct Point {     double x, y;     Point() {}     Point(double a, double b) {       x = a;       y = b;     }     Point operator+(Point who) { return {x + who.x, y + who.y}; }     Point operator-(Point who) { return {x - who.x, y - who.y}; }     Point operator/(double val) { return {x / val, y / val}; }     Point operator*(double val) { return {x * val, y * val}; }     bool operator==(const Point who) const {       return max(abs(x - who.x), abs(y - who.y)) <= 1e-7;     }     bool operator<(const Point who) const {       if (*this == who) return false;       return make_pair(x, y) < make_pair(who.x, who.y);     }   };   struct Line {     double a, b, c;     Line(){};     Line(double _a, double _b, double _c) {       a = _a;       b = _b;       c = _c;     }     Line(Point A, Point B) {       a = B.y - A.y;       b = A.x - B.x;       c = -(A.x * a + B.x * b);     }     Line operator||(Point A) { return {a, b, -(a * A.x + b * A.y)}; }     Line perp() {       Line ans = {a, b, c};       swap(ans.a, ans.b);       ans.a *= -1;       return ans;     }     Point intersect(Line who) {       double up_x = who.c * b - c * who.b;       double up_y = c * who.a - a * who.c;       double down = a * who.b - b * who.a;       return {up_x / down, up_y / down};     }     bool is_parallel(Line who) { return abs(a * who.b - b * who.a) <= 1e-7; }   };   int n, i, j, ans;   Point P[2017], mid, Origin(0.00, 0.00);   bool used[2017];   Line aux;   Point _P[2017], _mid;   map<Point, int> M;   vector<Line> ant;   void remove_sym() {     int i, j;     for (i = 1; i <= n; i++) {       for (j = i; j <= n; j++) {         if (used[j]) continue;         if (P[i] + P[j] == mid * 2) {           used[i] = used[j] = true;           break;         }       }     }   }   Point get_proj(Point A, Line L) {     Line aux = (L.perp() || A);     return aux.intersect(L);   }   bool check(Line act) {     int i;     int rm = 0;     Point sym;     M.clear();     _mid = get_proj(mid, act);     for (i = 1; i <= n; i++) {       _P[i] = get_proj(P[i], act);       if (_P[i] == _mid) {         rm++;         continue;       }       sym = _mid * 2 - _P[i];       if (M[sym] > 0) {         rm += 2;         M[sym]--;         continue;       }       M[_P[i]]++;     }     if (rm != n) return false;     return true;   }   int main() {     scanf( %d , &n);     mid = {0, 0};     for (i = 1; i <= n; i++) {       scanf( %lf%lf , &P[i].x, &P[i].y);       mid = mid + P[i];     }     mid = mid / n;     remove_sym();     for (i = 1; used[i] && i <= n; i++)       ;     if (i == n + 1) {       printf( -1 );       return 0;     }     for (j = i; j <= n; j++) {       if (used[j]) continue;       aux = Line(mid, (P[i] + P[j]) / 2);       aux = (aux.perp() || Origin);       bool ok = true;       for (auto e : ant) {         if (e.is_parallel(aux)) {           ok = false;           break;         }       }       if (!ok) continue;       ant.push_back(aux);       if (check(aux)) ans++;     }     printf( %d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     if (((n + 1) % 4) > 1)       cout << 1 << endl;     else       cout << 0 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long nInf = -1000000000;   const long long pInf = 1000000000;   const long long mod = 1000000007;   const int MAXN = 3010;   void getdata();   void preproc();   void solve();   vector<int> g[MAXN];   bool gr[MAXN][MAXN];   int n, m;   int main() {     int t = 1;     for (int i = 0; i < t; i++) {       getdata();       preproc();       solve();     }     return 0;   }   void getdata() {     scanf( %d %d , &n, &m);     for (int i = 0; i < m; i++) {       int x, y;       scanf( %d %d , &x, &y);       x--;       y--;       g[x].push_back(y);       gr[x][y] = true;     }   }   void preproc() {}   void solve() {     long long res = 0;     for (int a = 0; a < n; a++) {       for (int c = 0; c < n; c++) {         if (a == c) continue;         int count = 0;         for (int i = 0; i < (int)g[a].size(); i++) {           int b = g[a][i];           if (gr[b][c]) {             count++;           }         }         res += count * (count - 1) / 2;       }     }     printf( %I64d n , res);   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline T mod_v(T num) {     if (num >= 0)       return num % 1000000007;     else       return (num % 1000000007 + 1000000007) % 1000000007;   }   template <class T>   inline T gcd(T a, T b) {     a = abs(a);     b = abs(b);     while (b) b ^= a ^= b ^= a %= b;     return a;   }   template <class T>   T fast_pow(T n, T p) {     if (p == 0) return 1;     if (p % 2) {       T g = mod_v(mod_v(n) * mod_v(fast_pow(n, p - 1)));       return g;     } else {       T g = fast_pow(n, p / 2);       g = mod_v(mod_v(g) * mod_v(g));       return g;     }   }   template <class T>   inline T modInverse(T n) {     return fast_pow(n, 1000000007 - 2);   }   bool equalTo(double a, double b) {     if (fabs(a - b) <= 1e-9)       return true;     else       return false;   }   bool notEqual(double a, double b) {     if (fabs(a - b) > 1e-9)       return true;     else       return false;   }   bool lessThan(double a, double b) {     if (a + 1e-9 < b)       return true;     else       return false;   }   bool lessThanEqual(double a, double b) {     if (a < b + 1e-9)       return true;     else       return false;   }   bool greaterThan(double a, double b) {     if (a > b + 1e-9)       return true;     else       return false;   }   bool greaterThanEqual(double a, double b) {     if (a + 1e-9 > b)       return true;     else       return false;   }   struct debugger {     template <typename T>     debugger& operator,(const T& v) {       cerr << v <<    ;       return *this;     }   } dbg;   int nextInt() {     int n;     scanf( %d , &n);     return n;   }   long long nextLong() {     long long n;     scanf( %lld , &n);     return n;   }   void print(int n) { printf( %d , n); }   void println(int n) { printf( %d n , n); }   void println(long long n) { printf( %lld n , n); }   template <class T>   inline int in(register T& num) {     register char c = 0;     num = 0;     bool n = false;     while (c < 33) c = getchar();     while (c > 33) {       if (c ==  - )         n = true;       else         num = num * 10 + c -  0 ;       c = getchar();     }     num = n ? -num : num;     return 1;   }   string tshirt[] = { S ,  M ,  L ,  XL ,  XXL ,  XXXL };   int cnt[10];   string res[1000004];   int main() {     int var;     int n;     map<string, int> val;     for (int i = 0; i < 6; i++) {       in(var);       val[tshirt[i]] = i;       cnt[i] = var;     }     in(n);     bool ok = true;     vector<pair<int, pair<int, int> > > pos;     for (int i = 1; i <= n; i++) {       char s[10];       char* tmp;       char v[] =  , ;       scanf( %s , s);       if (!ok) continue;       tmp = strtok(s, v);       int l = 100, r = -1;       while (tmp != NULL) {         l = min(l, val[tmp]);         r = max(r, val[tmp]);         tmp = strtok(NULL, v);       }       if (l == r) {         if (cnt[l] == 0)           ok = false;         else {           cnt[l]--;           res[i] = tshirt[l];         }       } else         pos.push_back({l, {r, i}});     }     sort((pos).begin(), (pos).end());     for (auto i : pos) {       if (!ok) break;       if (cnt[i.first]) {         cnt[i.first]--;         res[i.second.second] = tshirt[i.first];       } else if (cnt[i.second.first]) {         cnt[i.second.first]--;         res[i.second.second] = tshirt[i.second.first];       } else         ok = false;     }     if (!ok)       printf( NO n );     else {       printf( YES n );       for (int i = 1; i <= n; i++) cout << res[i] <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   char matr[1010][1010];   int dx[] = {-1, 0, 1, 0};   int dy[] = {0, -1, 0, 1};   bool Field(int x, int y) {     if (x < 1 || x > n) return false;     if (y < 1 || y > m) return false;     return true;   }   int color[1010][1010];   int ans[1010][1010];   int f[10];   int H;   void Solve() {     int i, j, o;     int x2, y2;     for (j = 1; j <= m; j++) {       for (i = 1; i <= n; i++) {         if (color[j][i]) continue;         if (matr[j][i] ==  . ) {           for (o = 0; o <= 3; o++) {             x2 = i + dx[o];             y2 = j + dy[o];             if (!Field(x2, y2)) continue;             if (color[y2][x2]) continue;             if (matr[y2][x2] !=  . ) continue;             color[j][i] = 1;             color[y2][x2] = 1;             memset(f, 0, sizeof(f));             ;             ans[j][i] = H;             ans[y2][x2] = H;             H++;             color[j][i] = 1;             color[y2][x2] = 1;             break;           }         }       }     }   }   int C;   int dfs(int x, int y) {     int o;     int x2, y2;     int res;     res = 1;     color[y][x] = C;     for (o = 0; o <= 3; o++) {       x2 = x + dx[o];       y2 = y + dy[o];       if (!Field(x2, y2)) continue;       if (color[y2][x2] == C) continue;       if (ans[y2][x2] != ans[y][x]) continue;       if (matr[y2][x2] !=  . ) continue;       res += dfs(x2, y2);     }     return res;   }   void Solve2() {     int i, j, o;     int x2, y2;     memset(color, 0, sizeof(color));     ;     int flag;     for (j = 1; j <= m; j++) {       for (i = 1; i <= n; i++) {         if (matr[j][i] ==  .  && ans[j][i] == -1) {           flag = 0;           for (o = 0; o <= 3; o++) {             x2 = i + dx[o];             y2 = j + dy[o];             if (!Field(x2, y2)) continue;             if (ans[y2][x2] == -1) continue;             C++;             if (dfs(x2, y2) < 5) {               ans[j][i] = ans[y2][x2];               flag = 1;               break;             }           }           if (!flag) {             printf( -1 n );             exit(0);           }         }       }     }   }   vector<pair<int, int> > t;   void dfs2(int x, int y) {     int o;     int x2, y2;     color[y][x] = C;     t.push_back(make_pair(x, y));     for (o = 0; o <= 3; o++) {       x2 = x + dx[o];       y2 = y + dy[o];       if (!Field(x2, y2)) continue;       if (color[y2][x2] == C) continue;       if (matr[y2][x2] !=  . ) continue;       if (ans[y2][x2] >= 0 && ans[y2][x2] <= 9) {         f[ans[y2][x2]] = 1;       }       if (ans[y2][x2] != ans[y][x]) continue;       dfs2(x2, y2);     }   }   void Solve3() {     int i, j, o;     int k;     for (j = 1; j <= m; j++) {       for (i = 1; i <= n; i++) {         if (ans[j][i] >= 10) {           memset(f, 0, sizeof(f));           ;           C++;           t.clear();           dfs2(i, j);           k = 0;           for (o = 0; o <= 9; o++) {             if (!f[o]) {               k = o;               break;             }           }           for (o = 0; o <= (int)t.size() - 1; o++) {             ans[t[o].second][t[o].first] = k;           }         }       }     }   }   int main() {     int i, j;     scanf( %d %d , &m, &n);     for (j = 1; j <= m; j++) {       scanf( %s , matr[j] + 1);     }     memset(ans, -1, sizeof(ans));     Solve();     Solve2();     Solve3();     for (j = 1; j <= m; j++) {       for (i = 1; i <= n; i++) {         if (matr[j][i] ==  . ) {           printf( %d , ans[j][i]);         } else {           printf( %c , matr[j][i]);         }       }       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100, mod = 1e9 + 7;   long long a[N], k, n;   struct mat {     int d[N][N];     mat() { memset(d, 0, sizeof d); }     mat operator*(const mat& o) {       mat p;       for (int i = 0; i < n; i++) {         for (int j = 0; j < n; j++) {           for (int k = 0; k < n; k++) {             p.d[i][j] = (p.d[i][j] + 1ll * d[i][k] * o.d[k][j]) % mod;           }         }       }       return p;     }     mat pw(long long k) {       mat result;       for (int i = 0; i < n; i++) result.d[i][i] = 1;       mat s = *this;       while (k) {         if (k & 1) {           result = result * s;         }         s = s * s;         k >>= 1;       }       return result;     }   };   int main() {     cin >> n >> k;     for (int i = 0; i < n; i++) cin >> a[i];     mat adj;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         long long x = a[i] ^ a[j];         int cnt = 0;         while (x) {           if (x & 1) cnt++;           x >>= 1;         }         if (cnt % 3 == 0) adj.d[i][j] = 1;       }     }     mat r = adj.pw(k - 1);     int ans = 0;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         ans = (ans + r.d[i][j]) % mod;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int op(int y) {     if (y < 0) y = -y;     return y;   }   int n, pos, l, r;   int main() {     cin >> n >> pos >> l >> r;     if (l == 1 && r == n) {       cout << 0;     }     if (l == 1 && r != n) {       cout << op(r - pos) + 1;     }     if (l != 1 && r == n) {       cout << op(l - pos) + 1;     }     if (l != 1 && r != n) {       if (op(r - pos) < op(l - pos))         cout << r - l + op(r - pos) + 2;       else         cout << r - l + op(l - pos) + 2;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int mn[1111];   int main() {     int n, m, k;     scanf( %d%d%d , &n, &m, &k);     for (int i = 0; i < m; i++) {       mn[i] = 1e9;     }     for (int i = 0; i < n; i++) {       int x, y;       scanf( %d%d , &x, &y);       --x;       mn[x] = min(mn[x], y);     }     int s = 0;     for (int i = 0; i < m; i++) {       s += mn[i];     }     cout << min(s, k);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9;   const long long int MOD = 1e9 + 7;   const int N = 1e6 + 6;   int sum[N][3], prv[N][3];   int main() {     int n;     string s;     cin >> n >> s;     s =  *  + s;     prv[0][0] = prv[0][1] = -1;     for (int i = 1; i <= n; i++) {       sum[i][0] = sum[i - 1][0];       sum[i][1] = sum[i - 1][1];       prv[i][0] = prv[i - 1][0];       prv[i][1] = prv[i - 1][1];       if (s[i] ==  1 ) {         sum[i][1]++;         prv[i][1] = i;       }       if (s[i] ==  0 ) {         sum[i][0]++;         prv[i][0] = i;       }     }     for (int len = 1; len <= n; len++) {       int now = 1;       int cnt = 0;       while (now + len - 1 <= n) {         if (sum[now + len - 1][1] - sum[now - 1][1] == 0 ||             sum[now + len - 1][0] - sum[now - 1][0] == 0) {           cnt++;           now += len;           continue;         }         now = min(prv[now + len - 1][0], prv[now + len - 1][1]) + 1;       }       cout << cnt <<    ;     }     cout <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<int, int> > anom;   long long int fact[200005];   long long int invfact[200005];   long long int MOD = 1000000007;   long long int dp[2005][25];   long long int invmod(long long int i) {     long long int ans = 1;     long long int mult = i;     long long int exp = MOD - 2;     while (exp > 0) {       if (exp % 2 == 1) {         ans = (ans * mult) % MOD;       }       mult = (mult * mult) % MOD;       exp /= 2;     }     return ans;   }   void precomp() {     fact[0] = 1;     invfact[0] = 1;     for (long long int i = 1; i < 200005; i++) {       fact[i] = (fact[i - 1] * i) % MOD;       invfact[i] = (invfact[i - 1] * invmod(i)) % MOD;     }   }   long long int comb(int a, int b) {     long long int ans = (fact[a] * invfact[b]) % MOD;     ans = (ans * invfact[a - b]) % MOD;     return ans;   }   long long int paths(int a, int b, int c, int d) {     if (a <= c && b <= d) {       return comb(c - a + d - b, c - a);     }     return 0;   }   int main() {     precomp();     int n, m, k, s;     cin >> n >> m >> k >> s;     if (k == 0) {       cout << s << endl;       return 0;     }     int limred = ceil(log2(s)) + 1;     int a, b;     for (int i = 0; i < k; i++) {       cin >> a >> b;       anom.push_back({a, b});     }     sort(anom.begin(), anom.end());     bool beginanom = true;     if (k > 0 && anom[k] != make_pair(1, 1)) {       anom.push_back({1, 1});       beginanom = false;       k++;     }     sort(anom.begin(), anom.end());     for (int i = k - 1; i >= 0; i--) {       dp[i][0] = paths(anom[i].first, anom[i].second, n, m);       for (int j = i + 1; j < k; j++) {         dp[i][0] = (dp[i][0] -                     (dp[j][0] * paths(anom[i].first, anom[i].second,                                       anom[j].first, anom[j].second)) %                         MOD +                     MOD) %                    MOD;       }     }     for (int i = 1; i <= limred; i++) {       for (int j = k - 1; j >= 0; j--) {         dp[j][i] = paths(anom[j].first, anom[j].second, n, m);         for (int p = j + 1; p < k; p++) {           dp[j][i] = (dp[j][i] -                       (dp[p][i] * paths(anom[j].first, anom[j].second,                                         anom[p].first, anom[p].second)) %                           MOD +                       MOD) %                      MOD;         }         for (int p = 0; p < i; p++) {           dp[j][i] = (dp[j][i] - dp[j][p] + MOD) % MOD;         }       }     }     int prevs;     long long int expsum = 0;     long long int numways = 0;     for (int i = 0; i <= limred; i++) {       prevs = s;       s = ceil(s / 2.0);       if (beginanom) {         expsum = (expsum + (s * dp[0][i]) % MOD) % MOD;       } else {         expsum = (expsum + (prevs * dp[0][i]) % MOD) % MOD;       }       numways = (numways + dp[0][i]) % MOD;     }     long long int leftways = paths(1, 1, n, m);     leftways = (leftways - numways + MOD) % MOD;     expsum = (expsum + leftways) % MOD;     long long int ans = (expsum * invmod(comb(n + m - 2, n - 1))) % MOD;     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 150;   const int M = 6e4 + 5;   int n, m;   struct Node {     int a, b;   } s[N];   bool cmp(Node a, Node b) {     if (a.b > 0 && b.b < 0) return 1;     if (a.b < 0 && b.b > 0) return 0;     if (a.b > 0 && b.b > 0) return a.a < b.a;     return a.a + a.b > b.a + b.b;   }   int dp[N][M];   int main() {     cin >> n >> m;     memset(dp, 0, sizeof dp);     for (int i = 1; i <= n; i++) cin >> s[i].a >> s[i].b;     sort(s + 1, s + 1 + n, cmp);     int ans = 0;     dp[0][m] = 1;     for (int i = 1; i <= n; i++) {       for (int j = 0; j < M; j++) {         dp[i][j] = max(dp[i][j], dp[i - 1][j]);         if (j - s[i].b >= 0 && j - s[i].b < M && dp[i - 1][j - s[i].b] &&             j - s[i].b >= s[i].a)           dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - s[i].b] + 1);         ans = max(ans, dp[i][j]);       }     }     cout << ans - 1 << endl;   }


#include <bits/stdc++.h>   using namespace std;   long long n, ans, k, cnt1[200010], cnt2[200010][10];   vector<long long> adj[200010];   long long sub(long long a, long long b) { return (a - b + k) % k; }   void dfs(long long st, long long par, long long depth) {     cnt2[st][depth % k] = 1;     cnt1[st] = 1;     for (int l = 0; l < adj[st].size(); l++) {       long long to = adj[st][l];       if (to != par) {         dfs(to, st, depth + 1);         for (int i = 0; i < k; i++) {           for (int j = 0; j < k; j++) {             long long rem = sub(i + j, 2 * depth);             long long need = sub(k, rem);             ans += need * cnt2[st][i] * cnt2[to][j];           }         }         for (int i = 0; i < k; i++) {           cnt2[st][i] += cnt2[to][i];         }         cnt1[st] += cnt1[to];       }     }     long long sv = cnt1[st];     ans += sv * (n - sv);   }   int main() {     ans = 0;     memset(cnt1, 0, sizeof(cnt1));     ;     memset(cnt2, 0, sizeof(cnt2));     ;     cin >> n >> k;     for (int i = 1; i < n; i++) {       long long a, b;       cin >> a >> b;       adj[a].push_back(b);       adj[b].push_back(a);     }     dfs(1, -1, 0);     cout << ans / k << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 233;   struct item {     int ini, num;   } t[MAXN];   int vis1[MAXN] = {0}, vis2[MAXN] = {0};   int cg[MAXN] = {0};   int n;   bool cmp1(const item &a, const item &b) { return a.num < b.num; }   bool cmp2(const item &a, const item &b) { return a.ini < b.ini; }   int main() {     int cnt = 1;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &t[i].num);       t[i].ini = i;       if (vis1[t[i].num] == 0 && t[i].num <= n) {         vis2[i] = 1;       }       vis1[t[i].num] = 1;     }     sort(t + 1, t + 1 + n, cmp1);     for (int i = 1; i <= n; i++) {       while (vis1[cnt] != 0) cnt++;       if (vis2[t[i].ini] == 0) {         t[i].num = cnt;         cnt++;       }     }     sort(t + 1, t + 1 + n, cmp2);     for (int i = 1; i <= n; i++) {       printf( %d  , t[i].num);     }   }


#include <bits/stdc++.h>   using namespace std;   int n, arr[3], ans, a, b, c, ma, t;   multiset<int> s;   int main() {     scanf( %d , &n);     for (int i = 0; i < 3; i++) scanf( %d , arr + i);     for (int i = 0; i < n; i++) {       scanf( %d , &t);       s.insert(t);       ma = max(t, ma);     }     sort(arr, arr + 3);     a = arr[0];     b = arr[1];     c = arr[2];     if (a + b + c < ma) return 0 * printf( -1 n );     multiset<int>::iterator now, it = s.upper_bound(b + c);     while (it != s.end()) s.erase(it++), ans++;     it = s.upper_bound(a + c);     while (it != s.end()) {       ans++;       s.erase(it++);       now = s.upper_bound(a);       now--;       if (now != s.end()) s.erase(now);     }     it = s.upper_bound(max(a + b, c));     while (it != s.end()) {       ans++;       s.erase(it++);       now = s.upper_bound(b);       now--;       if (now != s.end()) s.erase(now);     }     if (a + b > c) {       now = it = s.upper_bound(c);       now--;       while (it != s.end()) {         ans++;         s.erase(it++);         if (now != s.end()) s.erase(now--);       }     }     while (!s.empty()) {       ans++;       it = s.end();       it--;       s.erase(it);       if (s.empty()) break;       now = s.upper_bound(b);       now--;       if (now != s.end() && b >= *now) {         s.erase(now);         if (s.empty()) break;         now = s.upper_bound(a);         now--;         if (now != s.end() && a >= *now) s.erase(now);       } else {         now = s.upper_bound(a + b);         now--;         if (now != s.end() && a + b >= *now) s.erase(now);       }     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       string s;       cin >> s;       set<char> res;       vector<char> ans;       for (int i = 0; i < s.length(); i++) {         if (s[i] == s[i + 1]) {           i++;         } else {           char c = s[i];           res.insert(c);         }       }       for (auto i : res) cout << i;       cout <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int x[1010], y[1010], cnt, _cnt;   int main() {     int n;     cin >> n;     for (int i = 1; i <= n; i++) scanf( %d %d , &x[i], &y[i]);     while (true) {       int cnt = 0;       for (int i = 1; i <= n; i++)         if ((x[i] + y[i]) & 1 == 1) cnt++;       if (cnt >= 1 && cnt <= n - 1) {         cout << cnt << endl;         for (int i = 1; i <= n; i++)           if ((x[i] + y[i]) & 1 == 1) printf( %d  , i);         return 0;       }       if (cnt == n) {         for (int i = 1; i <= n; i++) x[i]--;       }       for (int i = 1; i <= n; i++) {         cnt = (x[i] - y[i]) / 2;         _cnt = (x[i] + y[i]) / 2;         x[i] = cnt;         y[i] = _cnt;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e18 + 123;   const long double EPS = 1e-9;   const int MX = 1e9 + 1;   const int inf = 1e9 + 123;   const int MOD = 1e9 + 7;   const int N = 1e5 + 123;   const int dx[] = {0, 0, 1, -1};   const int dy[] = {1, -1, 0, 0};   int n, m;   long long dp[1001], a[13], b[13], c[13], d[13];   int main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     cin >> n >> m >> c[0] >> d[0];     for (int i = 1; i <= m; ++i) {       cin >> a[i] >> b[i] >> c[i] >> d[i];     }     for (int i = 1; i <= m; ++i) {       for (int l = n; l >= 0; --l) {         for (int j = 1; j <= a[i] / b[i]; ++j) {           if (l + j * c[i] > n) {             continue;           }           dp[l + j * c[i]] = max(dp[l + j * c[i]], dp[l] + j * d[i]);         }       }     }     for (int l = n; l >= 0; --l) {       for (int j = 1; j <= n / c[0]; ++j) {         if (l + j * c[0] > n) {           continue;         }         dp[l + j * c[0]] = max(dp[l + j * c[0]], dp[l] + d[0] * j);       }     }     long long ans = 0;     for (int i = 1; i <= n; ++i) {       ans = max(ans, dp[i]);     }     cout << ans;     return 0;   }