#include <bits/stdc++.h>   const int N = 55;   const int M = 2e4 + 5;   const long long inf = 9223372036854775807ll;   int n, m, i, j, k;   long long a[N][M], sum[N][M], dp[M];   long long mx[3][M << 2];   inline void push_up(int c, int u) {     mx[c][u] = std::max(mx[c][u << 1], mx[c][u << 1 | 1]);   }   void build(int l, int r, int u) {     if (l == r) {       mx[0][u] = mx[1][u] = mx[2][u] = -inf;       return;     }     int mid = (l + r) >> 1;     build(l, mid, u << 1);     build(mid + 1, r, u << 1 | 1);     push_up(0, u);     push_up(1, u);     push_up(2, u);   }   void modify(int c, int p, int l, int r, int u, long long v) {     if (l == r) {       mx[c][u] = v;       return;     }     int mid = (l + r) >> 1;     if (p <= mid) {       modify(c, p, l, mid, u << 1, v);     } else {       modify(c, p, mid + 1, r, u << 1 | 1, v);     }     push_up(c, u);   }   long long range_max(int c, int ql, int qr, int l, int r, int u) {     if (ql <= l && r <= qr) {       return mx[c][u];     }     long long res = -inf;     int mid = (l + r) >> 1;     if (ql <= mid) {       res = std::max(res, range_max(c, ql, qr, l, mid, u << 1));     }     if (mid < qr) {       res = std::max(res, range_max(c, ql, qr, mid + 1, r, u << 1 | 1));     }     return res;   }   int main() {     scanf( %d %d %d , &n, &m, &k);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         scanf( %lld , a[i] + j);         sum[i][j] = sum[i][j - 1] + a[i][j];       }     }     build(1, m, 1);     for (int i = 1; i <= n; i++) {       auto conb = [&](int x, int y) {         long long res = 0;         res += sum[x][y + k - 1] - sum[x][y - 1];         if (x < n) {           res += sum[x + 1][y + k - 1] - sum[x + 1][y - 1];         }         return res;       };       for (int j = 1; j <= m - k + 1; j++) {         dp[j] = 0;         if (i == 1) {           dp[j] = conb(i, j);           continue;         }         if (j - k >= 1) {           dp[j] = std::max(dp[j], range_max(0, 1, j - k, 1, m, 1));         }         if (j + k <= m) {           dp[j] = std::max(dp[j], range_max(0, j + k, m, 1, m, 1));         }         dp[j] = std::max(dp[j], range_max(1, std::max(j - k + 1, 1), j, 1, m, 1) +                                     sum[i][j - 1]);         dp[j] = std::max(dp[j],                          range_max(2, j, j + k - 1, 1, m, 1) - sum[i][j + k - 1]);         dp[j] += conb(i, j);       }       for (int j = 1; j <= m - k + 1; j++) {         modify(0, j, 1, m, 1, dp[j]);         modify(1, j, 1, m, 1, dp[j] - sum[i + 1][j + k - 1]);         modify(2, j, 1, m, 1, dp[j] + sum[i + 1][j - 1]);       }     }     long long ans = 0;     for (int i = 1; i <= m; i++) {       ans = std::max(ans, dp[i]);     }     printf( %lld n , ans);     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   unsigned long long int ans = 1;   int main() {     int i, j, k, l, c = 0, fuck = 0;     string s;     cin >> s;     l = s.size();     s +=  9 ;     for (i = 0; i < l; i++) {       if (fuck == 1) {         if (int(s[i] - 48) + int(s[i + 1] - 48) == 9)           c += 1;         else {           if (c % 2 != 0) ans *= (c / 2 + 1);           fuck = 0;         }       }       if (fuck == 0) {         if (int(s[i] - 48) + int(s[i + 1] - 48) == 9) {           fuck = 1;           c = 2;         }       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T1, typename T2>   struct lazysegtree {     vector<T1> v1;     vector<T2> v2;     int n;     const T1 init_value1 = 0;     const T2 init_value2 = 0;     lazysegtree(int sz) {       n = 1;       while (true) {         if (n >= sz) break;         n *= 2;       }       v1.resize(2 * n, init_value1);       v2.resize(2 * n, init_value2);       for (int i = n - 1; i >= 0; i--) {         v1[i] = func1(v1[i * 2], v1[i * 2 + 1]);       }     }     lazysegtree(vector<T1> &x) {       n = 1;       while (true) {         if (n >= x.size()) break;         n *= 2;       }       v1.resize(2 * n, init_value1);       v2.resize(2 * n, init_value2);       for (int i = 0; i < x.size(); i++) {         v1[n + i] = x[i];       }       for (int i = n - 1; i >= 0; i--) {         v1[i] = func1(v1[i * 2], v1[i * 2 + 1]);       }     }     void propagate(int ind) {       update(ind * 2, v2[ind]);       update(ind * 2 + 1, v2[ind]);       v2[ind] = init_value2;     }     void reflect(int ind) {       if (ind == 0) return;       reflect(ind / 2);       if (ind < n) propagate(ind);     }     void mergeChildren(int ind) {       v1[ind] =           func2(func1(v1[ind * 2], v1[ind * 2 + 1]), v2[ind], get_size(ind));     }     void update(int ind, T2 x) {       v1[ind] = func2(v1[ind], x, get_size(ind));       v2[ind] = func2(v2[ind], x, 1);     }     void update(int l, int r, T2 x) {       if (l >= r) return;       int L = l, R = r;       l += n;       r += n;       reflect(l);       reflect(r - 1);       while (true) {         if (l % 2 == 1) {           update(l, x);           l++;         }         if (r % 2 == 1) {           update(r - 1, x);           r--;         }         if (l >= r) break;         l /= 2;         r /= 2;       }       l = L + n;       r = R + n;       while (true) {         l /= 2;         r = (r + 1) / 2;         if (l <= 0) break;         mergeChildren(l);         mergeChildren(r - 1);       }     }     T1 query(int l, int r) {       T1 res1 = init_value1;       T1 res2 = init_value1;       if (l >= r) return res1;       l += n;       r += n;       reflect(l);       reflect(r - 1);       while (true) {         if (l % 2 == 1) {           res1 = func1(res1, v1[l]);           l++;         }         if (r % 2 == 1) {           res2 = func1(v1[r - 1], res2);           r--;         }         if (l >= r) break;         l /= 2;         r /= 2;       }       return func1(res1, res2);     }     T1 func1(T1 a, T1 b) { return a + b; }     T1 func2(T1 a, T2 b, long long sz) { return a + b * sz; }     long long get_size(int ind) {       long long ret = 1;       while (true) {         if (ret > ind) {           ret /= 2;           return n / ret;         }         ret *= 2;       }     }     void show() {       int n = 1;       for (int i = 1; i < v1.size(); i++) {         for (int j = 0; j < n; j++) {           if (j != 0) cout <<    ;           cout << v1[i + j];         }         cout << endl;         i += n - 1;         n *= 2;       }     }   };   int main() {     int n;     cin >> n;     vector<long long> a(n);     for (int i = 0; i < n; i++) {       scanf( %lld , &a[i]);     }     vector<long long> k(n - 1);     for (int i = 0; i < n - 1; i++) {       scanf( %lld , &k[i]);     }     map<int, int> mp;     mp[0] = 0;     for (int i = 1; i < n; i++) {       if (a[i] - k[i - 1] == a[i - 1]) {         (*mp.rbegin()).second = i;       } else {         mp[i] = i;       }     }     lazysegtree<long long, long long> seg(a);     int q;     cin >> q;     for (int i = 0; i < q; i++) {       char c;       int x, y;       getchar();       scanf( %c %d %d , &c, &x, &y);       if (c ==  + ) {         x--;         auto it = mp.upper_bound(x);         it--;         if ((*it).first != x) {           int k = (*it).second;           (*it).second = x - 1;           mp[x] = k;           it = mp.lower_bound(x);         }         seg.update((*it).first, (*it).second + 1, y);         while (true) {           it = mp.lower_bound(x);           int A = (*it).second;           if (A == n - 1) break;           long long V1 = seg.query(A, A + 1), V2 = seg.query(A + 1, A + 2);           long long dif = V1 + k[A] - V2;           if (dif <= 0) break;           it++;           seg.update((*it).first, (*it).second + 1, dif);           int k = (*it).second;           mp.erase(it);           it = mp.lower_bound(x);           (*it).second = k;         }       } else {         printf( %lld n , seg.query(x - 1, y));       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct E {     int to, nxt, d;   } b[100005 << 1];   int fst[100005], tot;   void insert(int f, int t, int d) {     b[++tot] = (E){t, fst[f], d};     fst[f] = tot;     b[++tot] = (E){f, fst[t], d};     fst[t] = tot;   }   int in[100005];   int dfn[100005];   int deep[100005];   long long dis[100005];   int fa[100005][20];   int dfs_c;   void dfs(int x) {     for (int i = 1; i <= 17; i++) fa[x][i] = fa[fa[x][i - 1]][i - 1];     dfn[++dfs_c] = x;     in[x] = dfs_c;     for (int i = fst[x]; i; i = b[i].nxt) {       int v = b[i].to;       if (!in[v]) {         fa[v][0] = x;         deep[v] = deep[x] + 1;         dis[v] = dis[x] + b[i].d;         dfs(v);       }     }   }   int lca(int u, int v) {     if (deep[u] > deep[v]) swap(u, v);     int t = deep[v] - deep[u];     for (int i = 0; i <= 17; i++)       if ((t >> i) & 1) v = fa[v][i];     if (u == v) return u;     for (int i = 17; i >= 0; i--)       if (fa[u][i] != fa[v][i]) {         u = fa[u][i];         v = fa[v][i];       }     return fa[u][0];   }   long long query(int u, int v) {     int l = lca(u, v);     return dis[u] + dis[v] - 2 * dis[l];   }   char s[5];   set<int> S;   set<int>::iterator it;   int main() {     int n;     scanf( %d , &n);     int x, y, c;     for (int i = 1; i < n; i++) {       scanf( %d%d%d , &x, &y, &c);       insert(x, y, c);     }     deep[1] = 1;     fa[1][0] = 1;     dfs(1);     long long ans = 0;     int m;     scanf( %d , &m);     for (int i = 1; i <= m; i++) {       scanf( %s , s);       if (s[0] ==  + ) {         int y = 0, z = 0;         scanf( %d , &x);         S.insert(in[x]);         it = S.find(in[x]);         it++;         if (it != S.end()) {           y = dfn[*it];           ans += query(x, y);         }         it--;         if (it != S.begin()) {           it--;           z = dfn[*it];           if (y) ans -= query(y, z);           ans += query(x, z);         }       }       if (s[0] ==  - ) {         int y = 0, z = 0;         scanf( %d , &x);         it = S.find(in[x]);         it++;         if (it != S.end()) {           y = dfn[*it];           ans -= query(x, y);         }         it--;         if (it != S.begin()) {           it--;           z = dfn[*it];           if (y) ans += query(y, z);           ans -= query(x, z);         }         S.erase(in[x]);       }       if (s[0] ==  ? ) {         long long p = ans;         if (S.size() > 1) {           set<int>::iterator it2;           it = S.begin();           it2 = S.end();           it2--;           p += query(dfn[*it], dfn[*it2]);         }         printf( %I64d n , p / 2);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3000 * 100 + 5;   vector<pair<int, int> > adj[N];   pair<int, int> edge[N];   short d[N];   bool mark[N], edg[N], c[N];   int bad[N], par[N], m, n;   void findd(int a, int e, int b) {     while (a != b) {       edg[e] = 1;       if (a == edge[e].first)         a = edge[e].second;       else         a = edge[e].first;       e = par[a];     }     return;   }   void DFS2(int v, int parr) {     mark[v] = 1;     par[v] = parr;     int s = adj[v].size();     int temp = -1, temp2 = 0;     if (c[v]) temp = v;     for (int i = 0; i < s; i++) {       int u = adj[v][i].first;       int w = adj[v][i].second;       if (!mark[u]) {         DFS2(u, w);         if (bad[u] != -1) {           if (temp == -1) {             temp = bad[u];             temp2 = par[bad[u]];           } else {             findd(temp, temp2, v);             findd(bad[u], par[bad[u]], v);             temp = -1;           }         }       }     }     bad[v] = temp;     return;   }   void DFS(int v) {     mark[v] = 1;     int s = adj[v].size();     for (int i = 0; i < s; i++) {       int u = adj[v][i].first;       int w = adj[v][i].second;       if (!mark[u]) {         DFS(u);         if (c[u]) {           c[v] = 1 - c[v];           edg[w] = 1;         }       }     }     return;   }   void pp() {     int t = 0;     for (int i = 0; i < m; i++) {       if (!edg[i]) t++;     }     printf( %d n , t);     for (int i = 0; i < m; i++) {       if (!edg[i]) printf( %d n , i + 1);     }     return;   }   int main() {     int t = -1, x, y, k = 0;     scanf( %d %d , &n, &m);     for (int i = 0; i < n; i++) {       scanf( %d , &d[i]);       if (d[i] == -1) t = i;     }     for (int i = 0; i < m; i++) {       scanf( %d %d , &x, &y);       adj[--x].push_back(make_pair(--y, i));       adj[y].push_back(make_pair(x, i));       edge[i] = make_pair(x, y);     }     for (int i = 0; i < n; i++) {       int s = adj[i].size();       if (d[i] == 0 && s % 2) {         k++;         c[i] = 1;       }       if (d[i] == 1 && s % 2 == 0) {         k++;         c[i] = 1;       }     }     if (t != -1) {       DFS(t);       pp();     } else {       t = 0;       if (k % 2 == 1) {         cout <<  -1 ;         return 0;       }       DFS2(0, m);       pp();     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     multiset<string> p;     cin >> s;     for (int i = 0; i < s.size(); i++) {       if ((s[i] ==  < ) || (s[i] ==  > )) s[i] =    ;     }     istringstream sin(s);     multiset<string>::iterator it;     string f;     while (sin >> f) {       if (f[0] !=  / ) {         p.insert(f);         cout << string(2 * (p.size() - 1),    ) <<  <  << f <<  >  << endl;       } else {         cout << string(2 * (p.size() - 1),    ) <<  <  << f <<  >  << endl;         f = f.substr(1);         it = p.find(f);         if (it != p.end()) p.erase(it);       }     }   }


// Cutiepie is a hoe!   #pragma comment(linker,  /stack:200000000 )   #pragma GCC optimize ( Ofast )   #pragma GCC target ( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx )   #include <bits/stdc++.h>   using namespace std;      #define TRACE      #ifdef TRACE   #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)   template <typename Arg1>   void __f(const char* name, Arg1&& arg1){    cerr << name <<   :   << arg1 << std::endl;   }   template <typename Arg1, typename... Args>   void __f(const char* names, Arg1&& arg1, Args&&... args){    const char* comma = strchr(names + 1,  , );cerr.write(names, comma - names) <<   :   << arg1<<  |  ;__f(comma+1, args...);   }   #else   #define trace(...)   #endif      #define rep(i, a) for(int i = 0; i < (a); ++i)   #define repp(i, a, b) for(int i = a; i < (b); ++i)   #define trav(a, x) for(auto& a : x)   #define all(x) x.begin(), x.end()   #define sz(x) (int)(x).size()   #define fst first   #define snd second   #define mp make_pair   #define pb push_back   typedef long long ll;   typedef long double ld;   typedef pair<int, int> pii;   typedef vector<int> vi;      int a[1005][1005];   int ee[3][1005];   ll s[1005];   ll s2[1005];      void solve()   {     int n, k;     cin>>n>>k;     rep(i, k){       s[i] = 0;       rep(j, n)       {         cin>>a[i][j];         s[i] += 1LL*a[i][j];         s2[i] += 1LL*a[i][j]*a[i][j];       }     }     fflush(stdout);     int kk = 1;     ll change, vdif;     if(2*s[1] == s[0]+s[2]){       vdif = s[1]-s[0];     }else{       vdif = s[k-1]-s[k-2];     }     repp(i, 1, k)     {       if(vdif != s[i]-s[i-1]){         change = s[i]-s[i-1]-vdif;         kk = i;         break;       }     }     ll v2;     if(kk<=2){       v2 = s2[k-1]+s2[k-3]-2LL*s2[k-2];     }else{       v2 = s2[2]+s2[0]-2LL*s2[1];     }     ll v2_ = s2[kk+1]+s2[kk-1]-2LL*s2[kk];     rep(i, n)     {       ll x = a[kk][i];       ll y = x - change;       ll v2__ = v2_ + 2LL*x*x - 2LL*y*y;       if(v2 == v2__){         cout<<kk<<   <<y<<endl;         fflush(stdout);         return;       }     }   }      int main() {     cin.sync_with_stdio(0); cin.tie(0);     cin.exceptions(cin.failbit);     solve();     return 0;   }


#include <bits/stdc++.h>   int main() {     std::ios_base::sync_with_stdio(false);     std::cin.tie(nullptr);     std::cout.tie(nullptr);     std::string str;     std::cin >> str;     std::uint64_t l = 0, r = str.size() - 1;     while (l < str.size() && std::isupper(str[l])) l++;     while (r && std::islower(str[r])) r--;     std::uint64_t lower_total = 0, upper_total = 0;     for (int i = l; i <= r; ++i)       if (std::isupper(str[i]))         upper_total++;       else         lower_total++;     std::uint64_t ans = std::min(lower_total, upper_total), lower_count = 0,                   upper_count = 0;     for (int i = l; i <= r; ++i) {       if (std::islower(str[i])) {         ans = std::min(ans, lower_count + upper_total - upper_count);         lower_count++;       } else         upper_count++;     }     std::cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     int ans = 0;     char c = getchar();     while (!isdigit(c)) c = getchar();     while (isdigit(c)) ans = (ans << 3) + (ans << 1) + (c ^ 48), c = getchar();     return ans;   }   int fa[5005], cnt[5005], sum[5005], dep[5005], bad[5005];   int main() {     sum[1] = 1;     for (int i = 2; i < 5005; i++) sum[i] = i + sum[i / 2] + sum[i - i / 2 - 1];     for (int i = 1; i < 5005; i++) sum[i] -= i;     for (int T = read(); T; T--) {       int n, d;       n = read(), d = read();       int cur = n * (n - 1) / 2;       if (d < sum[n] || d > cur) {         puts( NO );         continue;       }       fa[1] = 0;       for (int i = 2; i <= n; i++) fa[i] = i - 1;       for (int i = 1; i < n; i++) cnt[i] = 1;       cnt[n] = 0;       for (int i = 1; i <= n; i++) bad[i] = 0, dep[i] = i - 1;       while (cur > d) {         int u;         for (u = 1; u <= n; u++)           if (!cnt[u] && !bad[u]) break;         bad[u] = 1;         for (int p = 1; p <= n; p++)           if (cnt[p] < 2 && dep[u] - dep[p] == 2) {             --cnt[fa[u]], fa[u] = p;             dep[u] = dep[p] + 1, ++cnt[p];             bad[u] = 0, --cur;             break;           }       }       puts( YES );       for (int i = 2; i <= n; i++) printf( %d%c , fa[i],    n [i == n]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T, typename L>   bool smax(T &x, L y) {     return x < y ? (x = y, true) : false;   }   template <typename T, typename L>   bool smin(T &x, L y) {     return y < x ? (x = y, true) : false;   }   void pre_dfs(int, int);   void sack(int, int, bool);   void update(int, int, int, int, int);   const int MAXN = 1e5 + 15;   int cnt[MAXN], col[MAXN];   int st[MAXN], ft[MAXN], add[MAXN];   vector<int> adj[MAXN];   int n;   long long ans[MAXN];   pair<int, long long> tree[MAXN << 2];   int main() {     ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);     cerr <<  HELLO WORLD :) n ;     cin >> n;     for (int i = 0; i < n; i++) cin >> col[i];     for (int i = 1; i < n; i++) {       int u, v;       cin >> u >> v;       u--, v--;       adj[u].push_back(v);       adj[v].push_back(u);     }     pre_dfs(0, -1);     sack(0, -1, 0);     for (int i = 0; i < n; i++) cout << ans[i] <<    ;     return cout <<   n , false;   }   void pre_dfs(int v, int p) {     static int t = 0;     st[v] = t++;     add[st[v]] = v;     for (auto u : adj[v])       if (u != p) pre_dfs(u, v);     ft[v] = t;   }   void sack(int v, int p, bool keep) {     int id = -1, mx = -1061109567;     for (auto u : adj[v])       if (u != p && smax(mx, ft[u] - st[u])) id = u;     for (auto u : adj[v])       if (u != p && u != id) sack(u, v, 0);     if (id != -1) sack(id, v, 1);     update(1, 1, n + 1, col[v], 1);     for (auto u : adj[v])       if (u != p && u != id)         for (int i = st[u]; i < ft[u]; i++) update(1, 1, n + 1, col[add[i]], 1);     ans[v] = tree[1].second;     if (!keep)       for (int i = st[v]; i < ft[v]; i++) update(1, 1, n + 1, col[add[i]], -1);   }   void update(int id, int b, int e, int p, int val) {     if (b == e - 1) {       tree[id].first += val;       smax(tree[id].second, b);       return;     }     int mid = (b + e) >> 1;     if (p < mid)       update(id << 1, b, mid, p, val);     else       update(id << 1 | 1, mid, e, p, val);     tree[id].first = max(tree[id << 1].first, tree[id << 1 | 1].first);     tree[id].second = (tree[id << 1].first == tree[id << 1 | 1].first                            ? tree[id << 1].second + tree[id << 1 | 1].second                            : (tree[id << 1].first > tree[id << 1 | 1].first                                   ? tree[id << 1].second                                   : tree[id << 1 | 1].second));   }


#include <bits/stdc++.h>   using namespace std;   void FastIO() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);   }   vector<long long int> fib;   void calc(int n) {     fib.assign(n + 1, 0);     fib[0] = 1;     fib[1] = 1;     for (int i = (int)2; i <= (int)n; i++) {       fib[i] = fib[i - 1] + fib[i - 2];     }   }   int main() {     long long int n, k;     cin >> n >> k;     calc(n);     vector<long long int> ans;     for (int i = 0; i < (int)n; i++) {       if (k <= fib[n - 1 - i])         ans.push_back(i + 1);       else {         ans.push_back(i + 2);         ans.push_back(i + 1);         k -= fib[n - 1 - i];         i++;       }     }     for (int i = 0; i < (int)n; i++) {       cout << ans[i] <<    ;     }     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e18;   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n, m, cl, ce, v;     cin >> n >> m >> cl >> ce >> v;     vector<long long> data_l(cl), data_e(ce);     for (long long i = 0; i < cl; i++) {       long long k;       cin >> k;       data_l[i] = k - 1;     }     for (long long i = 0; i < ce; i++) {       long long k;       cin >> k;       data_e[i] = k - 1;     }     sort((data_l).begin(), (data_l).end());     sort((data_e).begin(), (data_e).end());     vector<long long> r_data_l = data_l;     vector<long long> r_data_e = data_e;     for (long long i = 0; i < r_data_l.size(); i++) r_data_l[i] *= -1;     for (long long i = 0; i < r_data_e.size(); i++) r_data_e[i] *= -1;     sort((r_data_l).begin(), (r_data_l).end());     sort((r_data_e).begin(), (r_data_e).end());     long long q;     cin >> q;     vector<long long> ans(q);     for (long long i = 0; i < q; i++) {       long long x1, y1, x2, y2;       cin >> x1 >> y1 >> x2 >> y2;       x1--;       y1--;       x2--;       y2--;       swap(x1, y1);       swap(x2, y2);       if (y1 == y2) {         ans[i] = abs(x2 - x1);         continue;       }       long long lift_time = (abs(y2 - y1) - 1) / v + 1;       long long left_l = -1, right_l = -1, left_e = -1, right_e = -1;       vector<long long>::iterator it =           lower_bound((r_data_l).begin(), (r_data_l).end(), -x1);       if (it != r_data_l.end()) left_l = -(*it);       it = lower_bound((data_l).begin(), (data_l).end(), x1);       if (it != data_l.end()) right_l = *it;       it = lower_bound((r_data_e).begin(), (r_data_e).end(), -x1);       if (it != r_data_e.end()) left_e = -(*it);       it = lower_bound((data_e).begin(), (data_e).end(), x1);       if (it != data_e.end()) right_e = *it;       long long ans1 =           (left_l == -1 ? INF                         : abs(left_l - x1) + abs(left_l - x2) + abs(y1 - y2));       long long ans2 =           (right_l == -1 ? INF                          : abs(right_l - x1) + abs(right_l - x2) + abs(y1 - y2));       long long ans3 =           (left_e == -1 ? INF : abs(left_e - x1) + abs(left_e - x2) + lift_time);       long long ans4 =           (right_e == -1 ? INF                          : abs(right_e - x1) + abs(right_e - x2) + lift_time);       ans[i] = min(min(ans1, ans2), min(ans3, ans4));     }     for (long long i = 0; i < q; i++) cout << ans[i] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, c = 0;     string s;     cin >> n >> s;     for (int i = 0; i < s.length(); i++) {       if (s[i] ==  1 )         c++;       else {         c++;         break;       }     }     cout << c;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, m, ai;     cin >> n;     map<pair<long long int, long long int>, int> mp;     vector<long long int> c;     for (long long int i = 0; i < n; i++) {       cin >> ai;       c.push_back(ai);       mp[{ai, i}]++;     }     for (long long int i = 0; i < n; i++) {       cin >> ai;       c.push_back(ai);       mp[{ai, i}]++;     }     cin >> m;     long long int d2 = 0;     for (auto c : mp) {       if (c.second == 2) d2++;     }     sort(c.begin(), c.end());     long long int ans = 1;     for (long long int i = 0; i < 2 * n;) {       long long int j = i + 1;       while (j < 2 * n && c[j] == c[i]) j++;       for (long long int k = 1; k <= j - i; k++) {         long long int p = k;         while (p % 2 == 0 && d2) {           d2--;           p /= 2;         }         (ans *= p) %= m;       }       i = j;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 105;   map<string, int> H1[maxn], H2[maxn];   vector<char> ans;   char getA(string a, string b) {     int c[30];     for (int i = 0; i < 26; i++) c[i] = 0;     for (int i = 0; i < a.size(); i++) c[a[i] -  a ]++;     for (int i = 0; i < b.size(); i++) c[b[i] -  a ]--;     for (int i = 0; i < 26; i++) {       if (c[i] > 0) return i +  a ;     }   }   int main() {     int n;     cin >> n;     if (n == 1) {       cout <<  ? 1 1  << endl;       string sss;       cin >> sss;       cout <<  !   << sss << endl;     } else {       cout <<  ? 1   << n << endl;       for (int i = 0; i < n * (n + 1) / 2; i++) {         string s;         cin >> s;         sort(s.begin(), s.end());         H1[s.size()][s]++;       }       cout <<  ? 2   << n << endl;       for (int i = 0; i < n * (n - 1) / 2; i++) {         string s;         cin >> s;         sort(s.begin(), s.end());         H2[s.size()][s]++;       }       for (int i = 1; i <= n; i++) {         for (auto p : H2[i]) {           H1[i][p.first] -= H2[i][p.first];         }         for (auto p : H1[i]) {           if (p.second > 0) {             string new1 =   ;             for (int j = 0; j < ans.size(); j++) new1 += ans[j];             ans.push_back(getA(p.first, new1));           }         }       }       string new1 =   ;       for (int j = 0; j < ans.size(); j++) new1 += ans[j];       cout <<  !   << new1 << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   void __print(unsigned x) { cerr << x; }   void __print(int x) { cerr << x; }   void __print(long x) { cerr << x; }   void __print(unsigned long x) { cerr << x; }   void __print(float x) { cerr << x; }   void __print(double x) { cerr << x; }   void __print(long long x) { cerr << x; }   void __print(unsigned long long x) { cerr << x; }   void __print(long double x) { cerr << x; }   void __print(const char *x) { cerr <<     << x <<    ; }   void __print(const string &x) { cerr <<     << x <<    ; }   void __print(bool x) { cerr << (x ?  true  :  false ); }   void __print(char x) { cerr <<      << x <<     ; }   template <typename T, typename V>   void __print(const pair<T, V> &x) {     cerr <<  { ;     __print(x.first);     cerr <<  , ;     __print(x.second);     cerr <<  } ;   }   template <typename T>   void __print(const T &x) {     int f = 0;     cerr <<  { ;     for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i);     cerr <<  } ;   }   void _print() { cerr <<  ] n ; }   template <typename T, typename... V>   void _print(T t, V... v) {     __print(t);     if (sizeof...(v)) cerr <<  ,  ;     _print(v...);   }   int n, x;   vector<int> visited;   vector<int> g[20000];   void dfs(int u, int deep) {     visited[u] = deep;     for (int v : g[u]) {       if (!visited[v]) {         dfs(v, deep + 1);       }     }   }   void solve() {     cin >> n >> x;     x--;     for (int i = 0; i < n; i += 1) g[i].clear();     visited.clear();     visited.resize(n);     int e = n - 1;     while (e--) {       int u, v;       cin >> u >> v;       u--;       v--;       g[u].emplace_back(v);       g[v].emplace_back(u);     };     if (g[x].size() <= 1) {       cout <<  Ayush n ;       return;     }     int ans = 0;     dfs(x, 1);     for (int &x : visited) x--;     vector<int> dis = visited;     sort(dis.begin(), dis.end());     ans = dis[n - 1] + dis[n - 2];     int t = n - ans - 1;     if (ans & 1) {       if (t & 1) {         cout <<  Ashish n ;       } else         cout <<  Ayush n ;     } else {       if (t & 1)         cout <<  Ayush n ;       else         cout <<  Ashish n ;     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int tc;     cin >> tc;     while (tc--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T, typename S>   inline T smax(T &a, const S &b) {     return a < b ? a = b : a;   }   int n, s, m, k, a[int(1500 + 20)], l[int(1500 + 20)], r[int(1500 + 20)],       id[int(1500 + 20)];   int psum[int(1500 + 20)], dp[int(1500 + 20)][int(1500 + 20)],       pos[int(1500 + 20)];   inline bool check(int val) {     for (int i = 1; i <= n; i++) {       psum[i] = psum[i - 1] + ((a[i] <= val) ? 1 : 0);       for (int j = 1; j <= m; j++) {         dp[i][j] = dp[i - 1][j];         if (!~pos[i]) continue;         int L = l[pos[i]];         smax(dp[i][j], dp[L - 1][j - 1] + psum[i] - psum[L - 1]);         L = r[id[pos[i]]];         smax(dp[i][j], dp[L][j - 1] + psum[i] - psum[L]);       }     }     return dp[n][m] >= k;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cout.tie(0);     cin >> n >> s >> m >> k;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 0; i < s; i++) cin >> l[i] >> r[i];     memset(id, -1, sizeof(id));     memset(pos, -1, sizeof(pos));     for (int i = 0; i < s; i++) {       if (!~pos[r[i]] || l[pos[r[i]]] > l[i]) pos[r[i]] = i;       for (int j = 0; j < s; j++)         if (r[j] <= r[i] && r[j] >= l[i])           if (!~id[i] || l[id[i]] > l[j]) id[i] = j;     }     int lo = 0, hi = 1e9 + 2;     while (lo != hi - 1) {       int mid = (long long)lo + hi >> 1;       if (check(mid))         hi = mid;       else         lo = mid;     }     if (!check(hi)) hi = -1;     cout << hi << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char ch[60][60];   int x[4] = {-1, 1, 0, 0};   int y[4] = {0, 0, -1, 1};   bool mark[60][60];   void del(int a, int b) {     for (int i = 0; i < 4; i++)       if (mark[a + x[i]][b + y[i]]) {         mark[a + x[i]][b + y[i]] = 0;         del(a + x[i], b + y[i]);       }   }   bool dfs(int a, int b, int n, int m) {     for (int i = 0; i <= n + 1; i++)       for (int j = 0; j <= m + 1; j++)         if (ch[i][j] ==  # )           mark[i][j] = 1;         else           mark[i][j] = 0;     mark[a][b] = 0;     int s = 0;     for (int i = 0; i < 4; i++)       if (mark[a + x[i]][b + y[i]]) {         s++;         if (s > 1) return 1;         mark[a + x[i]][b + y[i]] = 0;         del(a + x[i], b + y[i]);       }     return 0;   }   int main() {     int n, m;     while (scanf( %d %d , &n, &m) != EOF) {       gets(ch[0]);       for (int i = 0; i <= n + 1; i++)         for (int j = 0; j <= m + 1; j++) ch[i][j] =  . ;       for (int i = 1; i <= n; i++) gets(ch[i] + 1);       int s = 0;       for (int i = 1; i <= n; i++)         for (int j = 1; j <= m; j++)           if (ch[i][j] ==  # ) s++;       if (s < 3)         printf( -1 n );       else {         s = 2;         for (int i = 1; i <= n; i++) {           for (int j = 1; j <= m; j++)             if (ch[i][j] ==  # ) {               if (dfs(i, j, n, m)) {                 s = 1;                 break;               }             }           if (s < 2) break;         }         printf( %d n , s);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 400005;   const long long MOD = 998244353;   const long long g = 3;   int n, N;   int rev[MAXN];   long long m, Inv;   long long f[MAXN];   long long A[MAXN];   long long B[MAXN];   long long fac[MAXN];   long long inv[MAXN];   long long wn[2][MAXN];   long long power(long long a, long long b) {     long long res = 1;     while (b) {       if (b & 1) (res *= a) %= MOD;       (a *= a) %= MOD;       b >>= 1;     }     return res;   }   void init(int l) {     N = 1;     while (N < l) N <<= 1;     int half = N >> 1;     Inv = power(N, MOD - 2);     for (int i = 1; i < N; i++) rev[i] = (rev[i >> 1] >> 1) + (i & 1) * half;     for (int i = 0; i < half; i++) {       wn[0][i + half] = power(g, (MOD - 1) * i / N);       wn[1][i + half] = power((MOD + 1) / g, (MOD - 1) * i / N);     }     for (int i = half - 1; i >= 0; i--) {       wn[0][i] = wn[0][i << 1];       wn[1][i] = wn[1][i << 1];     }     fac[0] = 1;     for (int i = 1; i <= l; i++) fac[i] = fac[i - 1] * i % MOD;     inv[l] = power(fac[l], MOD - 2);     for (int i = l; i >= 1; i--) inv[i - 1] = inv[i] * i % MOD;   }   void NTT(long long *v, bool inv) {     for (int i = 0; i < N; i++)       if (i < rev[i]) swap(v[i], v[rev[i]]);     for (int i = 1; i < N; i <<= 1)       for (int j = 0; j < N; j += i << 1) {         long long *l = v + j, *r = v + j + i, *w = wn[inv] + i, A;         for (int k = 0; k < i; k++, l++, r++, w++) {           A = *r * *w;           *r = (*l - A) % MOD;           *l = (*l + A) % MOD;         }       }     if (inv) {       for (int i = 0; i < N; i++) (v[i] *= Inv) %= MOD;     }   }   int main() {     scanf( %d%lld , &n, &m);     init(n << 1 | 1);     for (int i = 0; i <= n; i++) scanf( %lld , &f[i]);     for (int i = 0; i <= n; i++) A[i] = inv[i];     for (int i = 0; i <= n; i++) B[i] = fac[n - i] * f[n - i] % MOD;     NTT(A, 0);     NTT(B, 0);     for (int i = 0; i < N; i++) f[i] = A[i] * B[i] % MOD;     NTT(f, 1);     reverse(f, f + n + 1);     for (int i = 0; i <= n; i++)       f[i] = f[i] * inv[i] % MOD * power(fac[i] * inv[i + 1] % MOD, m) % MOD;     memset(A, 0, sizeof(A));     memset(B, 0, sizeof(B));     for (int i = 0; i <= n; i++) A[i] = (i % 2 ? -inv[i] : inv[i]);     for (int i = 0; i <= n; i++) B[i] = fac[n - i] * f[n - i] % MOD;     NTT(A, 0);     NTT(B, 0);     for (int i = 0; i < N; i++) f[i] = A[i] * B[i] % MOD;     NTT(f, 1);     reverse(f, f + n + 1);     for (int i = 0; i <= n; i++) f[i] = f[i] * inv[i] % MOD;     for (int i = 0; i <= n; i++) printf( %lld  , (f[i] + MOD) % MOD);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   inline int Get() {     char ch;     while ((ch = getchar()) <  0  || ch >  9 )       ;     int Num = ch -  0 ;     while ((ch = getchar()) >=  0  && ch <=  9 )       Num = (Num << 3) + (Num << 1) + ch -  0 ;     return Num;   }   const int N = 1e3 + 5;   const int M = 1600;   int n, m, gA[N], gB[N], tA[N], tB[N], id[N];   long double prob[N];   std ::pair<long double, long double> dp[N][M];   inline bool cmp(const int &a, const int &b) {     return (1 - prob[b]) * prob[a] * tB[a] < (1 - prob[a]) * prob[b] * tB[b];   }   int main() {     n = Get(), m = Get();     for (int i = 1; i <= n; ++i) {       gA[i] = Get(), gB[i] = Get(), tA[i] = Get(), tB[i] = Get();       double res;       scanf( %lf , &res);       prob[i] = res;     }     for (int i = 1; i <= n; ++i) id[i] = i;     std ::sort(id + 1, id + n + 1, cmp);     for (int i = 1, k; k = id[i], i <= n; ++i)       for (int j = m; j >= 0; --j) {         std ::pair<long double, long double> nxt;         dp[i][j] = dp[i - 1][j];         if (j >= tA[k]) {           nxt = std ::make_pair(dp[i - 1][j - tA[k]].first + gA[k],                                 dp[i - 1][j - tA[k]].second - tA[k]);           dp[i][j] = std ::max(dp[i][j], nxt);         }         if (j >= tA[k] + tB[k]) {           nxt.first =               dp[i - 1][j - tA[k] - tB[k]].first + gA[k] + (1 - prob[k]) * gB[k];           nxt.second = prob[k] * dp[i - 1][j - tA[k] - tB[k]].second -                        (1 - prob[k]) * (j - tA[k]) - tA[k];           dp[i][j] = std ::max(dp[i][j], nxt);         }       }     std ::pair<long double, long double> ans = std ::make_pair(0, 0);     for (int j = 0; j <= m; ++j) ans = std ::max(ans, dp[n][j]);     printf( %.10lf %.10lf n , (double)ans.first, -(double)ans.second);   }


#include <bits/stdc++.h>   using namespace std;   string m[10];   bool win(int i, int j, int x, int y) {     int X = 0;     int O = 0;     int D = 0;     for (int c = 0; c < 5 && i >= 0 && i < 10 && j >= 0 && j < 10;          i += x, j += y, c++) {       if (m[i][j] ==  X ) {         X++;       } else if (m[i][j] ==  O ) {         O++;       } else {         D++;       }     }     return X == 4 && D == 1;   }   int main() {     ios_base::sync_with_stdio(0);     for (int i = 0; i < 10; i++) {       cin >> m[i];     }     for (int i = 0; i < 10; i++) {       for (int j = 0; j < 10; j++) {         if (m[i][j] ==  X ) {           for (int kx = -1; kx < 2; kx++) {             for (int ky = -1; ky < 2; ky++) {               if (kx == 0 && ky == 0) continue;               if (win(i, j, kx, ky)) {                 cout <<  YES ;                 return 0;               }             }           }         }       }     }     cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   bool chkmax(T &x, T y) {     return x < y ? x = y, true : false;   }   template <typename T>   bool chkmin(T &x, T y) {     return x > y ? x = y, true : false;   }   int readint() {     int x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * f;   }   long long n, p;   int main() {     n = readint();     p = readint();     for (long long i = 1;; i++) {       n -= p;       if (n <= 0) return printf( -1 n ), 0;       if (n < i) continue;       if (__builtin_popcountll(n) <= i) return printf( %lld n , i), 0;     }     printf( -1 n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int dx[4] = {1, 0};   const int dy[4] = {0, 1};   pair<int, vector<vector<int> > > MoveBotsDirs(       const vector<vector<int> >& bots_dirs) {     map<int, int> xs;     map<int, int> ys;     for (int i = 0; i < bots_dirs.size(); ++i) {       if (bots_dirs[i][2] == 0) {         xs[bots_dirs[i][0]]++;       } else {         ys[bots_dirs[i][1]]++;       }     }     if (xs.size() > 2 || ys.size() > 2) {       return make_pair(-1, vector<vector<int> >());     }     for (map<int, int>::const_iterator it = xs.begin(); it != xs.end(); ++it) {       if (it->second > 2) {         return make_pair(-1, vector<vector<int> >());       }     }     for (map<int, int>::const_iterator it = ys.begin(); it != ys.end(); ++it) {       if (it->second > 2) {         return make_pair(-1, vector<vector<int> >());       }     }     if (xs.size() < 2 && ys.size() < 2) {       return make_pair(-1, vector<vector<int> >());     }     if (ys.size() < 2) {       vector<vector<int> > bots_dirs2;       for (int i = 0; i < bots_dirs.size(); ++i) {         vector<int> bot;         bot.push_back(bots_dirs[i][1]);         bot.push_back(bots_dirs[i][0]);         bot.push_back(bots_dirs[i][3]);         bot.push_back(bots_dirs[i][2]);         bots_dirs2.push_back(bot);       }       pair<int, vector<vector<int> > > sol = MoveBotsDirs(bots_dirs2);       for (int i = 0; i < sol.second.size(); ++i) {         swap(sol.second[i][0], sol.second[i][1]);       }       return sol;     }     int ymin = ys.begin()->first;     int ymax = ys.rbegin()->first;     int side = ymax - ymin;     set<int> xcand;     vector<int> Xs;     for (map<int, int>::const_iterator it = xs.begin(); it != xs.end(); ++it) {       xcand.insert(it->first);     }     for (int i = 0; i < bots_dirs.size(); ++i) {       xcand.insert(bots_dirs[i][0]);       xcand.insert(bots_dirs[i][0] - side);       for (int j = 0; j < bots_dirs.size(); ++j) {         xcand.insert((bots_dirs[i][0] + bots_dirs[j][0] - side) / 2);         xcand.insert((bots_dirs[i][0] + bots_dirs[j][0] - side) / 2 + 1);         xcand.insert((bots_dirs[i][0] + bots_dirs[j][0] - side) / 2 - 1);         xcand.insert((bots_dirs[i][0] + bots_dirs[j][0] - side) / 2 - side);         xcand.insert((bots_dirs[i][0] + bots_dirs[j][0] - side) / 2 + 1 - side);         xcand.insert((bots_dirs[i][0] + bots_dirs[j][0] - side) / 2 - 1 - side);         xcand.insert((bots_dirs[i][0] + bots_dirs[j][0]) / 2);         xcand.insert((bots_dirs[i][0] + bots_dirs[j][0]) / 2 + 1);         xcand.insert((bots_dirs[i][0] + bots_dirs[j][0]) / 2 - 1);         xcand.insert((bots_dirs[i][0] + bots_dirs[j][0]) / 2 - side);         xcand.insert((bots_dirs[i][0] + bots_dirs[j][0]) / 2 + 1 - side);         xcand.insert((bots_dirs[i][0] + bots_dirs[j][0]) / 2 - 1 - side);       }     }     pair<int, vector<vector<int> > > res;     res.first = -1;     for (set<int>::const_iterator it = xcand.begin(); it != xcand.end(); ++it) {       int xmin = *it;       int xmax = xmin + side;       vector<vector<int> > nodes(4);       nodes[0].push_back(xmin);       nodes[0].push_back(ymin);       nodes[1].push_back(xmin);       nodes[1].push_back(ymax);       nodes[2].push_back(xmax);       nodes[2].push_back(ymax);       nodes[3].push_back(xmax);       nodes[3].push_back(ymin);       vector<int> perm;       for (int i = 0; i < 4; ++i) {         perm.push_back(i);       }       do {         vector<vector<int> > new_pos;         for (int i = 0; i < 4; ++i) {           new_pos.push_back(nodes[perm[i]]);         }         bool can_go = true;         int max_steps = 0;         for (int i = 0; i < 4; ++i) {           if (bots_dirs[i][2] == 0) {             if (new_pos[i][0] != bots_dirs[i][0]) {               can_go = false;               break;             }             int steps = abs((new_pos[i][1] - bots_dirs[i][1]) / bots_dirs[i][3]);             max_steps = max(max_steps, steps);           } else {             if (new_pos[i][1] != bots_dirs[i][1]) {               can_go = false;               break;             }             int steps = abs((new_pos[i][0] - bots_dirs[i][0]) / bots_dirs[i][2]);             max_steps = max(max_steps, steps);           }         }         if (can_go) {           if (res.first == -1) {             res.first = max_steps;             res.second = new_pos;           } else if (max_steps < res.first) {             res.first = max_steps;             res.second = new_pos;           }         }       } while (next_permutation(perm.begin(), perm.end()));     }     return res;   }   pair<int, vector<vector<int> > > MoveBots(const vector<vector<int> >& bots) {     pair<int, vector<vector<int> > > res;     res.first = -1;     int dir[4];     for (dir[0] = 0; dir[0] < 2; ++dir[0]) {       for (dir[1] = 0; dir[1] < 2; ++dir[1]) {         for (dir[2] = 0; dir[2] < 2; ++dir[2]) {           for (dir[3] = 0; dir[3] < 2; ++dir[3]) {             vector<vector<int> > bots_dirs;             for (int i = 0; i < 4; ++i) {               bots_dirs.push_back(bots[i]);               bots_dirs[i].push_back(dx[dir[i]]);               bots_dirs[i].push_back(dy[dir[i]]);             }             pair<int, vector<vector<int> > > curres = MoveBotsDirs(bots_dirs);             if (res.first == -1 && curres.first != -1) {               res = curres;             } else if (curres.first != -1 && curres.first < res.first) {               res = curres;             }           }         }       }     }     return res;   }   pair<int, vector<vector<int> > > MoveBotsSlow(       const vector<vector<int> >& bots) {     const int CHECK = 10;     pair<int, vector<vector<int> > > res;     res.first = -1;     for (int x = -CHECK; x <= CHECK; ++x) {       for (int y = -CHECK; y <= CHECK; ++y) {         for (int side = 1; side <= CHECK; ++side) {           int xmin = x;           int ymin = y;           int xmax = x + side;           int ymax = y + side;           vector<vector<int> > pos(4);           pos[0].push_back(xmin);           pos[0].push_back(ymin);           pos[1].push_back(xmin);           pos[1].push_back(ymax);           pos[2].push_back(xmax);           pos[2].push_back(ymax);           pos[3].push_back(xmax);           pos[3].push_back(ymin);           vector<int> perm;           for (int i = 0; i < 4; ++i) {             perm.push_back(i);           }           do {             vector<vector<int> > new_pos;             for (int i = 0; i < 4; ++i) {               new_pos.push_back(pos[perm[i]]);             }             int max_steps = 0;             bool can_go = true;             for (int i = 0; i < 4; ++i) {               if (new_pos[i][0] == bots[i][0]) {                 max_steps = max(max_steps, abs(new_pos[i][1] - bots[i][1]));               } else if (new_pos[i][1] == bots[i][1]) {                 max_steps = max(max_steps, abs(new_pos[i][0] - bots[i][0]));               } else {                 can_go = false;               }             }             if (can_go) {               if (res.first == -1) {                 res.first = max_steps;                 res.second = new_pos;               } else if (max_steps < res.first) {                 res.first = max_steps;                 res.second = new_pos;               }             }           } while (next_permutation(perm.begin(), perm.end()));         }       }     }     return res;   }   int main() {     int tests;     cin >> tests;     for (int test_index = 0; test_index < tests; ++test_index) {       vector<vector<int> > bots(4, vector<int>(2));       for (int i = 0; i < 4; ++i) {         for (int j = 0; j < 2; ++j) {           scanf( %d , &bots[i][j]);         }       }       pair<int, vector<vector<int> > > new_pos = MoveBots(bots);       if (new_pos.first == -1) {         printf( -1 n );         continue;       } else {         printf( %d n , new_pos.first);         for (int i = 0; i < 4; ++i) {           printf( %d %d n , new_pos.second[i][0], new_pos.second[i][1]);         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using lint = long long;   const int inf = 1000000007;   const int MOD = 1000000007;   void solve() {     int n, k;     cin >> n >> k;     vector<set<int>> edge(n);     for (int i = 0; i < n - 1; ++i) {       int a, b;       cin >> a >> b;       --a;       --b;       edge[a].insert(b);       edge[b].insert(a);     }     if (k == 1) {       cout << n - 1 <<   n ;       return;     }     int res = 0;     vector<queue<int>> child(n);     for (int i = 0; i < n; ++i) {       if (edge[i].size() == 1) {         child[*edge[i].begin()].push(i);       }     }     queue<int> que;     for (int i = 0; i < n; ++i) {       if (child[i].size() >= k) que.push(i);     }     while (!que.empty()) {       int x = que.front();       que.pop();       if (child[x].size() < k) continue;       res += 1;       for (int i = 0; i < k; ++i) {         int buf = child[x].front();         child[x].pop();         edge[x].erase(buf);       }       if (child[x].size() >= k) que.push(x);       if (edge[x].size() == 1) {         int par = *edge[x].begin();         child[par].push(x);         if (child[par].size() >= k) que.push(par);       }     }     cout << res <<   n ;   }   signed main() {     cin.tie(0);     ios::sync_with_stdio(false);     int t;     cin >> t;     for (int i = 0; i < t; ++i) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, N, Left, Right;   long long seg[2000001], vs[2000001];   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     char grid[n + 1][m + 1];     vs[0] = 0;     set<pair<long long, long long>> d;     for (long long i = 1; i <= n; i++) {       for (long long j = 1; j <= m; j++) {         cin >> grid[i][j];       }     }     for (long long i = 1; i <= n; i++) {       for (long long j = 1; j <= m; j++) {         if (i > 1 && j > 1) {           if (grid[i - 1][j] ==  X  && grid[i][j - 1] ==  X ) {             vs[j]++;           }         }       }     }     for (long long i = 1; i <= m; i++) vs[i] += vs[i - 1];     long long q;     cin >> q;     while (q--) {       cin >> Left >> Right;       if (vs[Right] - vs[Left])         cout <<  NO ;       else         cout <<  YES ;       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long int n, m, k, kk, x, y;     cin >> n >> m >> k;     kk = k;     long long int a[m];     for (long long int i = 0; i < m; i++) a[i] = 9999999999;     for (long long int i = 0; i < n; i++) {       cin >> x >> y;       a[x - 1] = min(a[x - 1], y);     }     for (long long int i = 0; i < m; i++) {       if (a[i] >= kk)         kk = 0;       else         kk -= a[i];       if (kk == 0) break;     }     cout << k - kk;     return;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long int t = 1;     while (t--) solve();     cerr <<   n           <<  Time elapsed :  << clock() * 1000.0 / CLOCKS_PER_SEC <<   ms n ;   }


#include <bits/stdc++.h>   using namespace std;   void prepare() {}   const int maxn = 100005;   int n;   int a[maxn], b[maxn];   vector<int> res;   int q[maxn];   void go(int x) {     int l = 0, r = 0;     long long cur = a[0];     while (true) {       cur -= b[r];       (++r) %= n;       while (cur < 0) {         ((--l) += n) %= n;         cur -= b[l];         cur += a[l];         if (l == r) break;       }       if (l != r)         cur += a[r];       else         break;     }     assert(cur == 0);     int s = l;     do {       ((--l) += n) %= n;       cur -= b[l];       cur += a[l];       if (cur >= 0) res.push_back(x ? q[l] : l);     } while (l != s);   }   bool solve() {     int i;     scanf( %d , &n);     for (i = (0); i < (n); i++) scanf( %d , &a[i]);     for (i = (0); i < (n); i++) scanf( %d , &b[i]);     for (i = (0); i < (n); i++) q[i] = n - i - 1;     go(0);     reverse(a, a + n);     reverse(b, b + n - 1);     go(1);     sort(res.begin(), res.end());     res.resize(unique(res.begin(), res.end()) - res.begin());     printf( %d n , (int)(res).size());     for (i = (0); i < ((int)(res).size()); i++) printf( %d  , res[i] + 1);     return false;   }   int main() {     prepare();     while (solve())       ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double pi = acos(-1.0);   const int imax = 2147483647;   const int imin = -2147483647;   const long long lmax = 9223372036854775807;   const long long lmin = -9223372036854775807;   long long power(long long a, long long b, long long m) {     long long p = 1;     while (b) {       if (b & 1) p = (p * a) % m;       b >>= 1;       a = (a * a) % m;     }     return (p % m);   }   int main() {     int n;     cin >> n;     int arr[100005];     int distinct[100005] = {0}, mp[100005] = {0};     for (int i = 0; i < n; i++) cin >> arr[i];     for (int i = n - 1; i >= 0; i--) {       if (i == n - 1) {         mp[arr[i]] = 1;         distinct[i] = 1;       } else {         if (mp[arr[i]] == 0) {           mp[arr[i]] = 1;           distinct[i] = 1 + distinct[i + 1];         } else           distinct[i] = distinct[i + 1];       }     }     memset(mp, 0, sizeof(mp));     long long ans = 0;     for (int i = 0; i < n - 1; i++) {       if (mp[arr[i]] == 0) {         mp[arr[i]] = 1;         ans += (long long)distinct[i + 1];       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int socks, day;   int main() {     cin.sync_with_stdio(0);     cin >> socks >> day;     int i = 0;     while (socks) {       i++;       if (!(i % day)) socks++;       socks--;     }     cout << i;   }


#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) { return a == 0 ? b : gcd(b % a, a); }   void solve(int test) {     int n, k;     cin >> n >> k;     vector<pair<pair<int, int>, int>> v(n);     int i, j, l;     for (i = (0); i < (n); ++i) cin >> v[i].first.first >> v[i].first.second;     for (i = (0); i < (n); ++i)       if (v[i].first.first == 0 && v[i].first.second == 0) break;     if (i == n) throw 0;     if (i != 0) swap(v[0], v[i]);     for (i = (1); i < (n); ++i) {       v[i].second = gcd(abs(v[i].first.first), abs(v[i].first.second));       v[i].first.first /= v[i].second;       v[i].first.second /= v[i].second;     }     sort(v.begin() + 1, v.end());     int prev = 1;     vector<double> all(1, 0.0), neg;     for (i = (2); i < (n + 1); ++i)       if (i == n || v[prev].first != v[i].first) {         long long x = v[prev].first.first;         long long y = v[prev].first.second;         double len = sqrt(x * x + y * y + 0.0);         vector<double> w;         for (j = (prev); j < (i); ++j) w.push_back(len * v[j].second);         reverse((w).begin(), (w).end());         int mid = min((k + 1) / 2, int((w).size()));         for (j = (0); j < (mid); ++j) all.push_back(w[j] * (k - 1 - 2 * j));         for (j = (mid); j < (int((w).size())); ++j) neg.push_back(w[j]);         prev = i;       }     sort((all).begin(), (all).end());     sort((neg).begin(), (neg).end());     if (int((all).size()) < k) {       int cnt = k - int((all).size());       int mid = (k + 1) / 2;       for (i = (0); i < (cnt); ++i)         all.push_back(neg[i] * (k - 1 - 2 * (mid + cnt - 1 - i)));       sort((all).begin(), (all).end());     }     reverse((all).begin(), (all).end());     double res = 0;     for (i = (0); i < (k); ++i) res += all[i];     cout.precision(11);     cout << res << endl;   }   int main() {     int T = 1, t;     for (t = (0); t < (T); ++t) solve(t);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 50, M = 55;   int B, bl[N];   vector<int> bcc[N];   inline void update(int &a, int b) { a = max(a, b); }   int n, m, d;   int dp[N * M];   char op[N][M];   vector<int> G[N], G2[N], G3[N];   void add(int u, int v, vector<int> G[]) { G[u].push_back(v); }   bool vis[N][M];   bool tv[N];   int val[N * M];   int c[N][M], tot;   vector<int> path;   void dfs(int u, int j, vector<int> G[], int col) {     vis[u][j] = 1;     c[u][j] = col;     if (op[u][j] ==  1  && !tv[u]) {       tv[u] = 1;       ++val[col];       path.push_back(u);     }     for (int v : G[u]) {       if (!vis[v][(j + 1) % d]) dfs(v, (j + 1) % d, G, col);     }   }   void doit(int bid) {     for (int bx : bcc[bid])       for (int j = 0; j < d; ++j)         if (!vis[bx][j]) {           path.clear();           dfs(bx, j, G2, ++tot);           for (int x : path) tv[x] = 0;         }   }   int topo(vector<int> G[]) {     for (int i = 0; i < B; ++i) {       for (int bx : bcc[i])         for (int j = 0; j < d; ++j) {           int uc = c[bx][j], u = bx;           for (int v : G[u]) {             int vc = c[v][(j + d - 1) % d];             update(dp[vc], dp[uc] + val[uc]);           }         }     }     int cc = c[0][0];     return dp[cc] + val[cc];   }   pair<int, int> e[N];   namespace tarjan {   int low[N], dfn[N], clk;   void init(int n) {     B = clk = 0;     memset(dfn, 0, sizeof(int) * (n + 1));     for (int i = 0; i < n; ++i) bcc[i].clear();   }   void tarjan(int u) {     static int st[N], p;     static bool in[N];     dfn[u] = low[u] = ++clk;     st[p++] = u;     in[u] = true;     for (int &v : G[u]) {       if (!dfn[v]) {         tarjan(v);         low[u] = min(low[u], low[v]);       } else if (in[v])         low[u] = min(low[u], dfn[v]);     }     if (dfn[u] == low[u]) {       while (1) {         int x = st[--p];         in[x] = false;         bl[x] = B;         bcc[B].push_back(x);         if (x == u) break;       }       ++B;     }   }   void go(int n) {     init(n);     for (int i = 0; i < n; ++i) {       if (!dfn[i]) tarjan(i);     }   }   }  // namespace tarjan   int main(int argc, char const *argv[]) {     scanf( %d%d%d , &n, &m, &d);     for (int i = 0; i < m; ++i) {       scanf( %d%d , &e[i].first, &e[i].second);       --e[i].first;       --e[i].second;       add(e[i].first, e[i].second, G);     }     for (int i = 0; i < n; ++i) scanf( %s , op[i]);     tarjan::go(n);     for (int i = 0; i < m; ++i) {       int u = e[i].first, v = e[i].second;       if (bl[u] == bl[v])         add(u, v, G2);       else         add(v, u, G3);     }     for (int i = 0; i < B; ++i) doit(i);     int ans = topo(G3);     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int power(long long int x, unsigned long long int y) {     long long int res = 1;     while (y > 0) {       if (y & 1) {         res = res * x;       }       y = y >> 1;       x = x * x;     }     return res;   }   long long int powermod(long long int x, unsigned long long int y,                          long long int p) {     long long int res = 1;     x = x % p;     while (y > 0) {       if (y & 1) {         res = (res * x) % p;       }       y = y >> 1;       x = (x * x) % p;     }     return res;   }   int32_t main() {     long long int n, m;     cin >> n >> m;     vector<long long int> f(m + n);     f[0] = 1, f[1] = 1;     for (long long int i = (long long int)(2); i < (long long int)(m + n); i++) {       f[i] = f[i - 1] + f[i - 2];       f[i] %= 1000000007;     }     long long int ans = 2 * f[n];     ans %= 1000000007;     ans += 2 * f[m];     ans %= 1000000007;     ans -= 2;     if (ans < 0) ans += 1000000007;     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   int B = 400;   using namespace std;   const long long inf = 1e8;   int MOD = 1e9 + 7;   const int mxn = 300010;   const int N = 1000005;   const double pi = 3.14159;   int dx[] = {0, 1, 0, -1, 1, -1, 1, -1};   int dy[] = {1, 0, -1, 0, -1, 1, 1, -1};   int n, d, k;   vector<int> g[mxn];   vector<pair<int, int> > q[mxn];   long long add[mxn];   long long ans[mxn];   void dfs(int v, int p = -1, int d = 1, long long cur = 0) {     for (auto i : q[v]) {       int d_ = i.first, val = i.second;       cur += (long long)val;       int tot = min(d + d_ + 1, mxn);       add[tot] -= (long long)val;     }     cur += add[d];     ans[v] = cur;     for (auto i : g[v]) {       if (i == p) continue;       dfs(i, v, d + 1, cur);     }     for (auto i : q[v]) {       int d_ = i.first, val = i.second;       int tot = min(d + d_ + 1, mxn);       add[tot] += (long long)val;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n;     for (int i = 1; i < n; i++) {       int u, v;       cin >> u >> v;       g[u].push_back(v), g[v].push_back(u);     }     int qn;     cin >> qn;     while (qn--) {       int n, d, val;       cin >> n >> d >> val;       q[n].push_back({d, val});     }     dfs(1);     for (int i = 1; i <= n; i++) cout << ans[i] <<    ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     t = 1;     for (int r = 0; r < t; r++) {       long long n, m, i, x, y, z, sum = 0;       cin >> n >> m;       vector<string> v(51);       char a =  a , b =  A ;       for (i = 0; i <= 50; i++) {         if (b >  Z ) b =  A ;         v[i].push_back(b);         if (i >= 26) {           v[i].push_back(a);           a++;         }         b++;       }       x = 0, y = 0;       vector<string> ans;       string temp;       while (y < m - 1) {         ans.push_back(v[y]);         y++;       }       x = y;       for (i = 0; i < (n - m + 1); i++, x++) {         cin >> temp;         if (temp ==  NO ) {           ans.push_back(ans[x - (m - 1)]);         } else {           ans.push_back(v[y]);           y++;         }       }       for (i = 0; i < n; i++) cout << ans[i] <<    ;       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);     int i, j, k;     int n, m, idx;     string s, s2;     cin >> s;     n = s.length();     int cnt = 0;     for (i = 0; i < n; i++) {       if (s[i] ==  a ) cnt++;     }     k = 0;     idx = 0;     for (i = 0; i < n; i++) {       if (k == cnt) break;       if (s[i] ==  a )         k++, idx = i + 1;       else         s2.push_back(s[i]);     }     k = n - idx;     k -= s2.length();     if (k % 2 == 0) {       k /= 2;       j = idx;       idx += k;       while (k > 0) {         s2.push_back(s[j]);         j++;         k--;       }       k = s.length() - 1;       for (i = s2.length() - 1; i >= 0; i--) {         if (s2[i] != s[k]) {           cout <<  :( ;           return 0;         }         k--;       }       for (i = 0; i < idx; i++) cout << s[i];     } else       cout <<  :( ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int p1[400010], p2[400010], p3[400010], p0[400010];   int cnt1, cnt2, cnt3, cnt0;   inline bool cmp(const int a, const int b) { return a > b; }   int main() {     int n;     cin >> n;     for (int i = 1, a, b, va; i <= n; i++) {       scanf( %1d%1d %d , &a, &b, &va);       if (a + b == 0)         p0[++cnt0] = va;       else if (a + b == 2)         p3[++cnt3] = va;       else if (a == 1)         p1[++cnt1] = va;       else         p2[++cnt2] = va;     }     sort(p1 + 1, p1 + 1 + cnt1, cmp);     sort(p2 + 1, p2 + 1 + cnt2, cmp);     long long ans = 0;     int fl = 0;     if (cnt2 < cnt1) swap(cnt2, cnt1), fl = 1;     for (int i = 1; i <= cnt1; i++) {       ans += p2[i] + p1[i];     }     if (fl)       for (int i = cnt1 + 1; i <= cnt2; i++) p0[++cnt0] = p1[i];     else       for (int i = cnt1 + 1; i <= cnt2; i++) p0[++cnt0] = p2[i];     sort(p0 + 1, p0 + 1 + cnt0, cmp);     for (int i = 1; i <= cnt3; i++) {       ans += p0[i] + p3[i];     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, y, flag = 0, count, z, x;     cin >> y;     if (y % 400 == 0 || y % 4 == 0 && y % 100 != 0) {       x = 1;     } else {       x = 2;     }     z = y + 1;     if (x == 1)       count = 3;     else       count = 2;     while (flag != 1) {       if (count == 1) {         if (z % 400 == 0 || z % 4 == 0 && z % 100 != 0) {           a = 1;         } else           a = 2;         if (a == x) {           flag = 1;           break;         }       }       if (z % 400 == 0 || z % 4 == 0 && z % 100 != 0) {         if (count == 7) {           count = 2;         } else if (count == 6)           count = 1;         else           count += 2;       } else {         if (count == 7)           count = 1;         else           count += 1;       }       z++;     }     cout << z;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n = 0, m = 0, k;   vector<string> a;   int CountSpiders(int j) {     int sum = 0;     int jR = j, jL = j;     for (int i = 1; i < n; ++i) {       if (!(i % 2) && a[i][j] ==  U ) ++sum;       if (jR > 0) {         --jR;         if (a[i][jR] ==  R ) ++sum;       }       if (jL < m - 1) {         ++jL;         if (a[i][jL] ==  L ) ++sum;       }     }     return sum;   }   int main() {     cin >> n >> m >> k;     cin.ignore(100,   n );     a.resize(n);     for (int i = 0; i < n; ++i) getline(cin, a[i]);     for (int j = 0; j < m; ++j) {       cout << CountSpiders(j) <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   int xx1, xx2, yy1, yy2;   int x, y, r, ans;   char v[30000000];   int zhuan(int x, int y) { return (x + 1000) * 10000 + y + 1000; }   void swap(int &a, int &b) {     int h = a;     a = b;     b = h;   }   bool dis(int xx, int yy) {     return (xx - x) * (xx - x) + (yy - y) * (yy - y) <= r * r;   }   void fun(int x1, int y1, int x2, int y2) {     if (x1 > x2) swap(x1, x2);     if (y1 > y2) swap(y1, y2);     for (int i = x1; i <= x2; i++)       for (int j = y1; j <= y2; j++) {         int k = zhuan(i, j);         if (v[k] == 0 && dis(i, j)) {           v[k] = 1;           ans++;         }       }   }   int main() {     while (scanf( %d %d %d %d , &xx1, &yy2, &xx2, &yy1) != EOF) {       if (xx1 > xx2) swap(xx1, xx2);       if (yy1 > yy2) swap(yy1, yy2);       int n, i, j, k, w;       memset(v, 0, sizeof(v));       scanf( %d , &n);       ans = 0;       for (i = 1; i <= n; i++) {         scanf( %d %d %d , &x, &y, &r);         fun(xx1, yy1, xx1, yy2);         fun(xx1, yy2, xx2, yy2);         fun(xx2, yy2, xx2, yy1);         fun(xx2, yy1, xx1, yy1);       }       w = (xx2 - xx1 + 1) * 2 + (yy2 - yy1 + 1) * 2 - 4;       if (xx2 == xx1 && yy1 == yy2) w = 1;       printf( %d n , w - ans);     }     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   long long t, n, i, b[1010], ta, tb, k1, a[1010], tem, x[1010], y[1010], k2, tom;   vector<long long> v[1010];   void dfs(long long aa, long long bb) {     b[aa] = bb;     long long ii;     for (ii = 0; ii < v[aa].size(); ii++)       if (!b[v[aa][ii]]) dfs(v[aa][ii], bb);   }   int main() {     cin >> t;     while (t--) {       cin >> n;       for (i = 1; i <= n; i++) v[i].clear();       for (i = 1; i <= n; i++) b[i] = 0;       for (i = 1; i < n; i++) {         cin >> ta >> tb;         v[ta].push_back(tb);         v[tb].push_back(ta);       }       for (i = 1; i <= n; i++) b[i] = 0;       for (i = 1; i <= n; i++) x[i] = 0;       for (i = 1; i <= n; i++) y[i] = 0;       cin >> k1;       for (i = 1; i <= k1; i++) {         cin >> a[i];         b[a[i]] = a[i];         x[a[i]] = 1;       }       for (i = 1; i <= k1; i++) dfs(a[i], a[i]);       cin >> k2;       for (i = 1; i <= k2; i++) {         cin >> ta;         y[ta] = 1;       }       cout <<  B   << ta << endl;       cin >> tem;       if (x[tem])         cout <<  C   << tem << endl;       else {         cout <<  A   << b[tem] << endl;         cin >> tom;         if (y[tom])           cout <<  C   << b[tem] << endl;         else           cout <<  C   << -1 << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s =  qwertyuiopasdfghjkl;zxcvbnm,./ ;     char lr;     cin >> lr;     string message;     cin >> message;     for (int i = 0; i < message.length(); i++) {       int it = s.find(message[i]);       if (lr ==  R ) {         cout << s[it - 1];       } else {         cout << s[it + 1];       }     }   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e6 + 3;   long long n, k, x;   pair<long long, long long> ans;   long long powmod(long long a, long long b, long long c) {     long long ans = 1;     while (b > 0) {       if (b & 1) ans = ans * a % c;       a = a * a % c;       b /= 2;     }     return ans;   }   pair<long long, long long> work(long long n, long long k) {     if (k == 0) {       return make_pair(0, 1);     }     long long dq = powmod(2, n, mod), now = 1;     for (int i = 1; i < 2 * mod; i += 2) {       now = now * (dq - i + 2 * mod) % mod;     }     long long ans;     ans = powmod(now, k / (2 * mod), mod);     for (int i = 1; i <= (k % (2 * mod)); i += 2)       ans = ans * (dq - i + 2 * mod) % mod;     pair<long long, long long> cur = work(n - 1, k / 2);     ans = ans * cur.second % mod;     return make_pair((k / 2 + cur.first) % (mod - 1), ans);   }   int main() {     cin >> n >> k;     x = 0;     while ((1LL << x) < k) x++;     if (n < x) {       cout <<  1 1  << endl;       return 0;     }     pair<long long, long long> ans = work(n, k - 1);     long long fz, fm;     fz = ans.second;     fm = (n % (mod - 1)) * ((k - 1) % (mod - 1)) % (mod - 1);     fm = (fm - ans.first + (mod - 1)) % (mod - 1);     fm = powmod(2, fm, mod);     fz = (fm - fz + mod) % mod;     cout << fz <<     << fm << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   typedef pair<int, int> pii;   template <typename T>   inline void read(T &x) {     int ch = getchar();     x = 0;     bool f = false;     double d = 1;     for (; ch !=  -  && (ch <  0  || ch >  9 ); ch = getchar())       ;     if (ch ==  - ) {       f = true;       ch = getchar();     }     for (; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch -  0 ;     if (ch ==  . )       for (ch = getchar(); ch >=  0  && ch <=  9 ; ch = getchar())         x += (ch -  0 ) * (d *= 0.1);     if (f) x = -x;   }   template <typename T>   inline bool chkmax(T &a, const T &b) {     if (a < b) return a = b, 1;     return 0;   }   template <typename T>   inline bool chkmin(T &a, const T &b) {     if (a > b) return a = b, 1;     return 0;   }   const int K = 81, N = 170, M = N << 4, INF = 0x3f3f3f3f;   int n, k, S, T, a[K], c[K], head[N], to[M], nxt[M], cap[M], w[M], tmp[K];   inline void add(int a, int b, int c, int d) {     static int cnt = 1;     to[++cnt] = b;     nxt[cnt] = head[a];     head[a] = cnt;     cap[cnt] = c;     w[cnt] = d;     to[++cnt] = a;     nxt[cnt] = head[b];     head[b] = cnt;     cap[cnt] = 0;     w[cnt] = -d;   }   int dis[N], inc[N], q[N], f, r, pre[N], ans;   bool inq[N];   inline void AMOD(int &x) {     ++x;     if (x >= N) x = 0;   }   inline bool spfa() {     memset(inq, 0, sizeof inq);     memset(dis, 0x3f, sizeof dis);     f = r = 0;     q[r++] = S;     inc[S] = INF;     dis[S] = 0;     inq[S] = true;     while (f != r) {       int u = q[f];       AMOD(f);       inq[u] = false;       for (register int i = head[u]; i; i = nxt[i])         if (cap[i] && chkmin(dis[to[i]], dis[u] + w[i])) {           inc[to[i]] = min(inc[u], cap[i]);           pre[to[i]] = i;           if (!inq[to[i]]) {             inq[to[i]] = true;             q[r] = to[i];             AMOD(r);           }         }     }     return dis[T] < INF;   }   int main() {     read(n);     read(k);     for (register int i = 1; i <= n; ++i) read(a[i]);     for (register int i = 1; i <= n; ++i) read(c[i]);     T = 1 + (S = n << 1 | 1);     for (register int i = 1; i <= n; ++i) {       add(S, i, 1, c[a[i]]);       add(i, i + n, 1, 0);       add(i + n, T, 1, 0);       if (i < n) add(i, i + 1, k - 1, 0);       if (tmp[a[i]]) add(i - 1, tmp[a[i]] + n, 1, -c[a[i]]);       tmp[a[i]] = i;     }     while (spfa()) {       ans += inc[T] * dis[T];       for (register int u = T; u; u = to[pre[u] ^ 1]) {         cap[pre[u]] -= inc[T];         cap[pre[u] ^ 1] += inc[T];       }     }     printf( %d , ans);   }


#include <bits/stdc++.h>   using namespace std;   char s[110], ss[110];   int a[210], ans[210], n, k, namlen;   bool boo;   bool check() {     int i, j;     for (i = 1; i <= n; i++)       if (ss[i] ==  0 ) {         bool bk = true;         for (j = 1; j <= namlen; j++)           if (a[i + j - 1] != s[j]) {             bk = false;             break;           }         if (bk) return false;       }     return true;   }   void search(int x) {     if (x == n + 1) {       if (check()) {         for (int i = 1; i <= n; i++) ans[i] = a[i];         boo = true;       }       return;     }     if (ss[x] ==  1 ) {       search(x + 1);       return;     }     if (a[x] != 0) {       search(x + 1);       return;     }     for (int i = 0; i < k; i++) {       a[x] = i +  a ;       search(x + 1);       if (boo) return;     }   }   int main() {     int len, i, j;     scanf( %d%d , &n, &k);     getchar();     gets(s + 1);     gets(ss + 1);     namlen = strlen(s + 1);     len = strlen(ss + 1);     for (i = 1; i <= namlen; i++)       if (s[i] -  a  + 1 > k) {         printf( No solution n );         return 0;       }     bool bk = true;     for (i = 1; i <= n; i++) a[0] = 0;     for (i = 1; i <= len; i++) {       if (ss[i] ==  1 ) {         if (i + namlen - 1 > n) {           bk = false;           break;         }         for (j = 1; j <= namlen; j++) {           if (a[i + j - 1] != 0 && a[i + j - 1] != s[j]) {             bk = false;             break;           }           a[i + j - 1] = s[j];         }         if (!bk) break;       }     }     if (!bk) {       printf( No solution n );       return 0;     }     boo = false;     search(1);     if (!boo)       printf( No solution n );     else {       for (i = 1; i <= n; i++) printf( %c , char(ans[i]));       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     string a;     cin >> a;     string res =   ;     int kolvo = k / 2;     int i = 0;     int s = 0;     int bla = 0;     while (i < kolvo) {       res += a[s];       if (a[s] ==  ( ) {         ++i;       } else {         ++bla;       }       ++s;     }     while (bla < kolvo) {       res +=  ) ;       ++bla;     }     cout << res;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 2e5 + 5, M = 1e18 + 5, OO = 0x3f3f3f3f;   const double pi = 3.141592653589793238462643383279502884;   double n, r;   double calc() {     double ang = 180 / n;     ang = (ang / 180) * pi;     double a = (sin(ang / 2) * r) / sin(pi - (1.5 * ang));     double ans = n * a * r * sin(ang);     return ans;   }   int main() {     cin >> n >> r;     printf( %.12lf  n , calc());     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3228;   const int MOD = 1000 * 1000 * 1000 + 7;   long long pw(long long a, int first) {     if (!first) return 1;     if (first % 2) return a * pw(a * a, first / 2);     return pw(a * a, first / 2);   }   void solve() {     int pl[3] = {0};     int n, p1, p2;     cin >> n >> p1 >> p2;     pl[1] -= p1;     pl[2] -= p2;     for (int i = 0; i < n; ++i) {       int nxt;       cin >> nxt;       if (nxt == 2 && pl[2] >= 0)         ++pl[1];       else         ++pl[nxt];     }     cout << max(0, pl[1]) + max(0, pl[2]);   }   int main() {     double tt = clock();     cout.tie(nullptr);     cin.tie(nullptr);     ios_base::sync_with_stdio(false);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1000000007;   const int N = 300005;   const double PI = 4 * atan(1);   const long long MAX = 9223372036854775807;   int dx[] = {1, -1, 0, 0, 1, -1, 1, -1};   int dy[] = {0, 0, 1, -1, 1, 1, -1, -1};   long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); }   long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); }   bool issquare(long long w) { return trunc(sqrt(w)) * trunc(sqrt(w)) == w; }   bool isprime(long long u) {     for (long long i = 2; i <= (int)sqrt(u); i++) {       if (u % i == 0) return 0;     }     return 1;   }   long long mod(long long to_mod, long long MOD) {     to_mod %= MOD;     while (to_mod < 0) to_mod += MOD;     return to_mod % MOD;   }   long long moduloMultiplication(long long a, long long b, long long mod) {     long long res = 0;     a %= mod;     while (b) {       if (b & 1) res = (res + a) % mod;       a = (2 * a) % mod;       b >>= 1;     }     return res;   }   long long power(long long x, long long y) {     long long res = 1;     x = x;     while (y > 0) {       if (y & 1) res = (res * x);       y = y >> 1;       x = (x * x);     }     return res;   }   long long n, m, x, y, res, t, k, ans = 0, q, z, w, l, a, b;   vector<pair<long long, pair<long long, long long> > > v;   long long dp[N];   vector<long long> vv;   long long curr[N];   int main() {     ios::sync_with_stdio(0);     memset(dp, 0, sizeof(dp));     memset(curr, 0, sizeof(curr));     cin >> n >> m;     for (int i = 0; i < m; i++) {       cin >> x >> y >> z;       x--;       y--;       v.push_back(make_pair(z, make_pair(x, y)));     }     sort(v.begin(), v.end());     ans = 1;     for (int i = 0; i < m;) {       long long j = i;       vv.clear();       while (j < m && v[j].first == v[i].first) {         curr[v[j].second.second] =             max(curr[v[j].second.second], 1 + dp[v[j].second.first]);         vv.push_back(v[j].second.second);         j++;       }       i = j;       for (auto t : vv) {         dp[t] = max(dp[t], curr[t]);       }     }     for (int i = 0; i < n; i++) {       ans = max(ans, dp[i]);     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     int res = 0, f = 1;     char ch = getchar();     while (!isdigit(ch)) {       if (ch ==  - ) f = -f;       ch = getchar();     }     while (isdigit(ch)) {       res = (res << 3) + (res << 1) + ch -  0 ;       ch = getchar();     }     return res * f;   }   namespace qiqi {   const int N = 6e5 + 5;   int n, m, siz[N], si[N], fa[N], ch[N][2], rev[N], cnt, mx[N];   bool del[N];   struct Edge {     int u, v, w, id;     inline bool friend operator<(Edge a, Edge b) { return a.w < b.w; }   } e[N];   priority_queue<Edge> q;   inline bool isroot(int x) { return ch[fa[x]][0] != x && ch[fa[x]][1] != x; }   inline void push_up(int x) {     siz[x] = (x <= n) + si[x] + siz[ch[x][0]] + siz[ch[x][1]];     mx[x] = x > n ? x - n : 0;     for (int i = (0); i <= (1); ++i)       if (e[mx[ch[x][i]]].w > e[mx[x]].w) mx[x] = mx[ch[x][i]];   }   inline void push_down(int x, bool k) {     if (k && !isroot(x)) push_down(fa[x], k);     if (rev[x]) {       swap(ch[x][0], ch[x][1]);       rev[ch[x][0]] ^= 1;       rev[ch[x][1]] ^= 1;       rev[x] = 0;     }   }   inline void rotate(int x) {     int y = fa[x], z = fa[y], l = x == ch[y][1];     if (!isroot(y)) ch[z][y == ch[z][1]] = x;     fa[x] = z;     push_up(fa[ch[y][l] = ch[x][l ^ 1]] = y);     push_up(fa[ch[x][l ^ 1] = y] = x);   }   inline void splay(int x) {     push_down(x, 1);     while (!isroot(x)) {       int y = fa[x];       if (!isroot(y)) rotate(x ^ y ^ ch[y][0] ^ ch[fa[y]][0] ? x : y);       rotate(x);     }     push_up(x);   }   inline void access(int x) {     for (int i = 0; x; x = fa[i = x]) {       splay(x);       si[x] += siz[ch[x][1]];       si[x] -= siz[ch[x][1] = i];       push_up(x);     }   }   inline void makeroot(int x) {     access(x);     splay(x);     rev[x] ^= 1;   }   inline void split(int x, int y) {     makeroot(x);     access(y);     splay(y);   }   inline int find(int x) {     access(x);     splay(x);     while (ch[x][rev[x]]) x = ch[x][rev[x]];     splay(x);     return x;   }   inline int qry(int x, int y) {     split(x, y);     return mx[y];   }   inline void cut(int x, int y) {     split(x, y);     cnt -= siz[y] & 1;     ch[y][0] = fa[x] = 0;     push_up(y);     cnt += (siz[x] & 1) + (siz[y] & 1);   }   inline void link(int x, int y) {     split(x, y);     cnt -= (siz[x] & 1) + (siz[y] & 1);     si[fa[x] = y] += siz[x];     push_up(y);     cnt += siz[y] & 1;   }   inline int upd(int x, int y, int id) {     bool flg = 1;     if (find(x) == find(y)) {       int k = qry(x, y);       if (e[k].w > e[id].w) {         del[k] = 1;         cut(e[k].u, k + n);         cut(e[k].v, k + n);       } else         flg = 0;     }     if (flg) {       link(x, id + n);       link(y, id + n);     }     if (cnt) return -1;     while (1) {       Edge e = q.top();       q.pop();       if (del[e.id]) continue;       cut(e.u, e.id + n);       cut(e.v, e.id + n);       if (cnt) {         q.push(e);         link(e.u, e.id + n);         link(e.v, e.id + n);         return q.top().w;       }     }   }   void main() {     e[0].w = 0;     cnt = n = read();     m = read();     for (int i = (1); i <= (m); ++i) {       q.push(e[i] = (Edge){read(), read(), read(), i});       printf( %d n , upd(e[i].u, e[i].v, i));     }   }   }  // namespace qiqi   int main() {     qiqi::main();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int max(int a, int b) {     if (a > b)       return a;     else       return b;   }   long long min(long long a, long long b) {     if (a < b)       return a;     else       return b;   }   int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }   int lcm(int a, int b) { return a * (b / gcd(a, b)); }   vector<bool> isPrime(1000005 + 1, true);   string tos(int n) {     stringstream ss;     ss << n;     return ss.str();   }   void seive() {     isPrime[0] = 0, isPrime[1] = 0;     for (long long i = 2; i * i <= 1000005; i++)       if (isPrime[i])         for (long long j = i * i; j <= 1000005; j += i) isPrime[j] = 0;   }   int dx[] = {1, -1, 0, 0};   int dy[] = {0, 0, 1, -1};   const int val = 1 << 20;   long long n, m, st, e, ans = 1, a[1111111], b[1111111];   int main() {     string s1, s2;     cin >> s1 >> s2;     long long i = 0, j = 0, beg = -1;     while (j < s2.size()) {       if (s1[i] == s2[j]) {         if (beg == -1) {           beg = j;           a[0] = j;         }         i++;       }       if (i == s1.size()) {         b[0] = j;         break;       }       j++;     }     i = s1.size();     j = s2.size();     beg = -1;     while (j >= 0) {       if (s1[i] == s2[j]) {         if (beg == -1) {           beg = j;           b[1] = j;         }         i--;       }       if (i < 0) {         a[1] = j;         break;       }       j--;     }     ans = max(0, a[1] - b[0]);     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   T abs(T x) {     return x < 0 ? -x : x;   }   const double pi = asin(1.0) * 2;   int n;   int m;   long long pl, pr, vl, vr;   int k;   vector<long long> h;   double prob(long long l, long long r, long long L, long long R) {     L = max(l, L);     R = min(R, r);     if (L > R) return 0.0;     return (R - L + 1) / (double)(r - l + 1);   }   int main() {     cin >> pl >> pr >> vl >> vr >> k;     h.push_back(0);     int cur = 0;     for (int i = cur; h[i] * 10 + 4 <= 1e10; i++) {       h.push_back(h[i] * 10 + 4);       h.push_back(h[i] * 10 + 7);     }     sort((h).begin(), (h).end());     double ans = 0;     for (int i = 1; i < (int)((h).size()); i++) {       if (i + k >= (int)((h).size())) break;       long long l1 = h[i - 1] + 1, r1 = h[i];       long long l2 = h[i + k - 1], r2 = h[i + k] - 1;       ans += prob(pl, pr, l1, r1) * prob(vl, vr, l2, r2);       ans += prob(pl, pr, l2, r2) * prob(vl, vr, l1, r1);       ans -= prob(pl, pr, max(l2, l1), min(r1, r2)) *              prob(vl, vr, max(l2, l1), min(r1, r2));     }     printf( %.10lf n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long calc2(long long a, long long b, long long c, long long ab,                   long long ac, long long bc, long long abc) {     long long parts[] = {a - ab - ac + abc,                          b - ab - bc + abc,                          c - ac - bc + abc,                          ab - abc,                          ac - abc,                          bc - abc,                          abc};     int masks[3] = {1 + 8 + 16 + 64, 2 + 8 + 32 + 64, 4 + 16 + 32 + 64};     static int mark[64 * 8] = {0};     static int id = 0;     ++id;     long long res = 0;     for (int i = 0; i < 7; ++i)       if ((masks[0] >> i) & 1)         for (int j = 0; j < 7; ++j)           if ((masks[1] >> j) & 1)             for (int k = 0; k < 7; ++k)               if ((masks[2] >> k) & 1) {                 int a1 = min(i, min(j, k));                 int a3 = max(i, max(j, k));                 int a2 = i + j + k - a1 - a3;                 if (mark[64 * a1 + 8 * a2 + a3] == id) continue;                 mark[64 * a1 + 8 * a2 + a3] = id;                 if (i == j && j == k) {                   long long x = parts[i];                   res += +x * (x - 1) * (x - 2) / 6 + x * (x - 1) + x;                 } else if (i == j || j == k || i == k) {                   long long x = parts[i];                   long long y = parts[j];                   if (i == j)                     y = parts[k];                   else if (j == k)                     swap(x, y);                   res += x * (x + 1) / 2 * y;                 } else {                   res += parts[i] * parts[j] * parts[k];                 }               }     return res;   }   int get_common_num(vector<int> &a, vector<int> &b) {     int ia = 0, ib = 0;     int res = 0;     while (ia < a.size() && ib < b.size()) {       if (a[ia] == b[ib]) {         ++res;         ++ia, ++ib;       } else if (a[ia] < b[ib])         ++ia;       else         ++ib;     }     return res;   }   int get_common_num(vector<int> &a, vector<int> &b, vector<int> &c) {     int ia = 0, ib = 0, ic = 0;     int res = 0;     while (ia < a.size() && ib < b.size() && ic < c.size()) {       if (a[ia] == b[ib] && b[ib] == c[ic]) {         ++res;         ++ia, ++ib, ++ic;       } else if (a[ia] <= min(b[ib], c[ic]))         ++ia;       else if (b[ib] <= min(a[ia], c[ic]))         ++ib;       else         ++ic;     }     return res;   }   vector<int> dividers[100111];   int main() {     ios::sync_with_stdio(false);     dividers[1].push_back(1);     vector<int> smallest_fact(100111 + 1, 0);     for (int i = 2; i < 100111; ++i) {       if (!smallest_fact[i]) {         smallest_fact[i] = i;         for (long long j = i * (long long)i; j < 100111; j += i) {           if (!smallest_fact[j]) smallest_fact[j] = i;         }       }       int p = smallest_fact[i];       int x = i;       while (x % p == 0) x /= p;       int y = i;       int pp = p;       dividers[i] = dividers[x];       while (y % p == 0) {         for (int d : dividers[x]) dividers[i].push_back(pp * d);         pp *= p;         y /= p;       }       sort(dividers[i].begin(), dividers[i].end());     }     int t;     cin >> t;     while (t--) {       int a, b, c;       cin >> a >> b >> c;       int x = dividers[a].size();       int y = dividers[b].size();       int z = dividers[c].size();       int xy = get_common_num(dividers[a], dividers[b]);       int xz = get_common_num(dividers[a], dividers[c]);       int yz = get_common_num(dividers[b], dividers[c]);       int xyz = get_common_num(dividers[a], dividers[b], dividers[c]);       long long ans = calc2(x, y, z, xy, xz, yz, xyz);       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void inout() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;   }   int main() {     inout();     long long n, i, u, v;     cin >> n;     map<long long, long long> prv, nxt, cnt1, cnt2;     vector<long long> ans(n + 5);     for (i = 0; i < n; i++) {       cin >> u >> v;       prv[v] = u;       nxt[u] = v;       cnt1[u]++;       cnt2[v]++;     }     for (auto k : cnt1)       if (cnt1[k.first] > cnt2[k.first]) ans[1] = k.first;     for (auto k : cnt2)       if (cnt1[k.first] < cnt2[k.first]) ans[n] = k.first;     i = 3;     while (i <= n) {       ans[i] = nxt[ans[i - 2]];       i += 2;     }     i = n + n % 2 - 2;     while (i > 0) {       ans[i] = prv[ans[i + 2]];       i -= 2;     }     for (i = 1; i <= n; i++) cout << ans[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 105000;   const int M = 105;   int n, m, p;   long long hill[N];   long long a[N], s[N];   long long dp[N];   int top, tail;   long long duix[N], duiy[N];   int getint() {     char e = getchar();     int res = 0;     while (e <  0  || e >  9 ) e = getchar();     while ( 0  <= e && e <=  9 ) {       res = (res << 3) + (res << 1) + e -  0 ;       e = getchar();     }     return res;   }   int main() {     int i, j;     n = getint();     m = getint();     p = getint();     for (i = 2; i <= n; i++) hill[i] = getint();     for (i = 2; i <= n; i++) hill[i] += hill[i - 1];     for (i = 1; i <= m; i++) {       a[i] = -hill[getint()];       a[i] += getint();     }     sort(a + 1, a + m + 1);     for (i = 1; i <= m; i++) s[i] = a[i] + s[i - 1];     for (i = 1; i <= m; i++) dp[i] = i * a[i];     for (j = 2; j <= p; j++) {       top = 1;       tail = 0;       for (i = 1; i <= m; i++) {         long long nowx = i;         long long nowy = dp[i];         while (tail >= 2) {           double xla = duiy[tail] - duiy[tail - 1];           xla /= duix[tail] - duix[tail - 1];           double xlb = nowy - duiy[tail - 1];           xlb /= nowx - duix[tail - 1];           if (xla - xlb > -1e-10)             tail--;           else             break;         }         tail++;         duix[tail] = nowx;         duiy[tail] = nowy;       }       for (i = 1; i <= m; i++) {         dp[i] = duiy[top] - a[i] * duix[top];         while (top < tail && duix[top + 1] <= i) {           long long now = duiy[top + 1] - a[i] * duix[top + 1];           if (now <= dp[i]) {             top++;             dp[i] = now;           } else             break;         }         dp[i] += i * a[i];       }     }     printf( %I64d n , dp[m] - s[m]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, h;   int main() {     scanf( %d %d , &n, &h);     for (int i = 1; i < n; i++) {       printf( %.10lf n , sqrt(1. * h * h * i / n));     }   }


#include <bits/stdc++.h>   using namespace std;   void sprint(int x) { cout << x; }   void sprint(long x) { cout << x; }   void sprint(long long x) { cout << x; }   void sprint(unsigned x) { cout << x; }   void sprint(unsigned long x) { cout << x; }   void sprint(unsigned long long x) { cout << x; }   void sprint(float x) { cout << x; }   void sprint(double x) { cout << x; }   void sprint(long double x) { cout << x; }   void sprint(char x) { cout <<      << x <<     ; }   void sprint(const char *x) { cout <<     << x <<    ; }   void sprint(const string &x) { cout <<     << x <<    ; }   void sprint(bool x) { cout << (x ?  true  :  false ); }   template <typename T, typename V>   void sprint(const pair<T, V> &x);   template <typename T>   void sprint(const vector<T> &x);   template <typename T, typename V>   void sprint(const pair<T, V> &x) {     cout <<  { ;     sprint(x.first);     cout <<  , ;     sprint(x.second);     cout <<  } ;   }   template <typename T>   void sprint(const vector<T> &x) {     cout <<  { ;     for (int u69 = 0; u69 < x.size(); u69++) {       sprint(x[u69]);       cout << (u69 + 1 == x.size() ?    :  , );     };     cout <<  } ;   }   template <typename T>   void sprint(const T &x) {     int f = 0;     cout <<  { ;     for (auto &i : x) cout << (f++ ?  ,  :   ), sprint(i);     cout <<  } ;   }   void sprint() { cout <<  ] n ; }   template <typename T, typename... V>   void sprint(T t, V... v) {     sprint(t);     if (sizeof...(v)) cout <<  ,  ;     sprint(v...);   }   inline long long ceil2(long long a, long long b) { return (a + b - 1) / b; }   inline bool isPowerOfTwo(long long x) { return x && (!(x & (x - 1))); }   bool isPerfectSquare(long long x) {     long long sr = sqrt(x);     return ((sr * sr) == x);   }   bool isIn(string &s2, string &s1) {     if (s1.find(s2) != string::npos) return true;     return false;   }   bool isSorted(vector<int> &a) {     for (int i = 0; i < (int)a.size() - 1; i++)       if (a[i] > a[i + 1]) return false;     return true;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, m;     cin >> n >> m;     char a[n][m];     for (long long i = 0; i < n; i++) {       for (long long j = 0; j < m; j++) {         cin >> a[i][j];       }     }     int count = 0;     for (long long i = 0; i < n; i++) {       for (long long j = 0; j < m; j++) {         if (a[i][j] ==  W  && ((a[i + 1][j] ==  P  && i + 1 < n) ||                                (a[i - 1][j] ==  P  && i - 1 >= 0) ||                                (a[i][j + 1] ==  P  && j + 1 < m) ||                                (a[i][j - 1] ==  P  && j - 1 >= 0))) {           count++;         }       }     }     cout << count <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int l[100005], r[100005];   int main(int argc, char* argv[]) {     std::ios::sync_with_stdio(false);     int n, k;     scanf( %d%d , &n, &k);     int sum = 0;     for (int i = 0; i < n; i++) {       scanf( %d%d , &l[i], &r[i]);       sum += r[i] - l[i] + 1;     }     int res = 0;     while (sum % k != 0) {       sum++;       res++;     }     printf( %d n , res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline T bigmod(T p, T e, T M) {     long long ret = 1;     for (; e > 0; e >>= 1) {       if (e & 1) ret = (ret * p) % M;       p = (p * p) % M;     }     return (T)ret;   }   template <class T>   inline T gcd(T a, T b) {     if (b == 0) return a;     return gcd(b, a % b);   }   template <class T>   inline T modinverse(T a, T M) {     return bigmod(a, M - 2, M);   }   template <class T>   inline T lcm(T a, T b) {     a = abs(a);     b = abs(b);     return (a / _gcd(a, b)) * b;   }   template <class T, class X>   inline bool getbit(T a, X i) {     T t = 1;     return ((a & (t << i)) > 0);   }   template <class T, class X>   inline T setbit(T a, X i) {     T t = 1;     return (a | (t << i));   }   template <class T, class X>   inline T resetbit(T a, X i) {     T t = 1;     return (a & (~(t << i)));   }   inline long long power(long long a, long long b) {     long long multiply = 1;     for (int i = (0); i < (b); i++) {       multiply *= a;     }     return multiply;   }   char given[505][505];   long long dp[505][505];   int n, m, modby;   long long calc(int x, int y) {     long long &ret = dp[x][y];     if (x == 2 && y == 0) return 1;     if (x == 0 && y == 1) return 0;     if (ret != -1) return ret;     ret = 0;     long long a;     if (x > 1) {       a = calc(x - 2, y) % modby;       a = (a * ((x * (x - 1)) / 2) % modby) % modby;       ret += a % modby;     }     if (y > 1) {       a = calc(x + 2, y - 2) % modby;       a = (a * ((y * (y - 1)) / 2) % modby) % modby;       ret += a % modby;     }     if (x != 0 && y != 0) {       a = calc(x, y - 1) % modby;       a = (a * (x * y) % modby) % modby;       ret += a % modby;     }     return ret;   }   int main() {     ios_base::sync_with_stdio(0);     cin >> n >> m >> modby;     memset(dp, -1, sizeof(dp));     for (int i = (0); i < (m); i++) {       for (int j = (0); j < (n); j++) cin >> given[i][j];     }     int zeros = 0, ones = 0;     for (int i = (0); i < (n); i++) {       int x = 0;       for (int j = (0); j < (m); j++) {         if (given[j][i] ==  1 ) x++;       }       if (x == 0)         zeros++;       else if (x == 1)         ones++;     }     if (n == m)       cout << 1 << endl;     else       cout << calc(ones, zeros) % modby << endl;     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:64000000 )   using namespace std;   const int MAXN = -1;   const int MOD = 1;   const long double EPS = 1e-11;   long double sqr(long double x) { return x * x; }   bool ok(long double x) { return x >= -EPS && x <= 1 + EPS; }   void norm(long double &x) {     if (x < 0) x = 0;     if (x > 1) x = 1;   }   int main() {     int n;     while (scanf( %d , &n) == 1) {       vector<long double> mn(n), mx(n);       for (int i = 0; i < n; i++) {         double o;         scanf( %lf , &o);         mx[i] = o;       }       for (int i = 0; i < n; i++) {         double o;         scanf( %lf , &o);         mn[i] = o;       }       vector<double> a(n), b(n);       long double sa = 0.0, sb = 0.0;       for (int i = 0; i < n; i++) {         long double g = mn[i] + mx[i];         long double A = -1;         long double B = (sb + g - sa);         long double C = g * sa - mx[i];         long double D = B * B - 4 * A * C;         assert(D >= -EPS);         D = fabs(D);         long double na = (-B - sqrt(D)) / (2 * A);         long double nb = g - na;         assert(ok(na));         assert(ok(nb));         a[i] = na;         b[i] = nb;         sa += na;         sb += nb;       }       for (int i = 0; i < n; i++) printf( %.15f%c , a[i],    n [i + 1 == n]);       for (int i = 0; i < n; i++) printf( %.15f%c , b[i],    n [i + 1 == n]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 65;   char g[maxn][maxn];   int r, c, row[maxn], col[maxn];   void solve() {     memset(row, 0, sizeof row);     memset(col, 0, sizeof col);     int sum = 0;     for (int i = 1; i <= r; i++) {       for (int j = 1; j <= c; j++) {         if (g[i][j] ==  A ) col[i]++, row[j]++, sum++;       }     }     if (sum == 0)       printf( MORTAL n );     else if (sum == r * c)       printf( 0 n );     else if (row[1] == r || row[c] == r || col[1] == c || col[r] == c)       printf( 1 n );     else {       int flag = 0;       for (int i = 2; i < r; i++)         if (col[i] == c) flag = 1;       for (int i = 2; i < c; i++)         if (row[i] == r) flag = 1;       if (flag == 1 || g[1][1] ==  A  || g[r][1] ==  A  || g[1][c] ==  A  ||           g[r][c] ==  A )         printf( 2 n );       else if (row[1] > 0 || row[c] > 0 || col[1] > 0 || col[r] > 0)         printf( 3 n );       else         printf( 4 n );     }   }   int main() {     int T;     scanf( %d , &T);     while (T--) {       scanf( %d%d , &r, &c);       for (int i = 1; i <= r; i++) scanf( %s , g[i] + 1);       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int c[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};   char a[60][60], color[2510];   int to[60][60], minx[2510], maxx[2510], miny[2510], maxy[2510], f[2510][4][2];   void dfs(int x0, int y0, int value) {     to[x0][y0] = value;     minx[value] = min(minx[value], x0);     miny[value] = min(miny[value], y0);     maxx[value] = max(maxx[value], x0);     maxy[value] = max(maxy[value], y0);     for (int i = 0; i < 4; i++) {       int x = x0 + c[i][0], y = y0 + c[i][1];       if (to[x][y] == 0 && a[x][y] == a[x0][y0]) dfs(x, y, value);     }   }   int calc(int x, int y, int z) {     int x1 = minx[x], x2 = maxx[x], y1 = miny[x], y2 = maxy[x];     if (y == 0)       return (z == 0 ? to[x1][y2 + 1] : to[x2][y2 + 1]);     else if (y == 1)       return (z == 0 ? to[x2 + 1][y2] : to[x2 + 1][y1]);     else if (y == 2)       return (z == 0 ? to[x2][y1 - 1] : to[x1][y1 - 1]);     else       return (z == 0 ? to[x1 - 1][y1] : to[x1 - 1][y2]);   }   int main() {     int n, K;     scanf( %d%d , &n, &K);     for (int i = 1; i <= n; i++) scanf( %s , a[i] + 1);     int m = strlen(a[1] + 1), tot = 0;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         if (a[i][j] !=  0  && to[i][j] == 0) {           tot++;           color[tot] = a[i][j];           minx[tot] = miny[tot] = 1 << 30;           maxx[tot] = maxy[tot] = 0;           dfs(i, j, tot);         }     for (int i = 1; i <= tot; i++)       for (int j = 0; j < 4; j++)         for (int k = 0; k < 2; k++) f[i][j][k] = calc(i, j, k);     int bp = 1, dp = 0, cp = 0;     for (int i = 1; i <= K; i++) {       int p = f[bp][dp][cp];       if (p == 0)         if (cp == 0)           cp = 1;         else {           cp = 0;           dp = dp + 1 & 3;         }       else         bp = p;     }     printf( %c n , color[bp]);     return (0);   }


#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1.0);   const unsigned long long MAX = LONG_MAX;   void fasterIO() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   int sumDig(int a) {     int sum = 0;     while (a != 0) {       sum += a % 10;       a /= 10;     }     return sum;   }   int main() {     int test;     cin >> test;     while (test--) {       long long p, k;       long long arr[150];       cin >> p >> k;       for (unsigned long long i = 0; i < (p); i++) cin >> arr[i];       sort(arr, arr + p);       if (abs((arr[0] + k) - arr[p - 1]) > k)         cout <<  -1  << endl;       else         cout << arr[0] + k << endl;     }     return 0;   }