#include <bits/stdc++.h>   using namespace std;   const int inf(1010101010);   struct uf {     int par[5005], rank[5005];     int ff(int x) { return par[x] == x ? x : par[x] = ff(par[x]); }     void uu(int x, int y) {       x = ff(x), y = ff(y);       if (x == y) return;       if (rank[x] > rank[y])         par[y] = x;       else if (rank[x] < rank[y])         par[x] = y;       else         rank[x]++, par[y] = x;     }     uf(int n) {       for (int i(0); i < (n); i++) par[i] = i, rank[i] = 0;     }   };   int n, m, k;   int wt[100005];   pair<int, int> e[100005];   int perm[100005];   struct mycmp {     bool operator()(int i, int j) const { return wt[i] < wt[j]; }   };   void kruskal_met_0(vector<int>& ret) {     uf G(n);     for (int i(0); i < (m); i++) {       int eid(perm[i]), x(e[eid].first), y(e[eid].second);       if (x == 0 || y == 0) {         ret.push_back(eid);         G.uu(x, y);       }     }     for (int i(0); i < (m); i++) {       int eid(perm[i]), x(e[eid].first), y(e[eid].second), xr(G.ff(x)),           yr(G.ff(y));       if (xr - yr) {         ret.push_back(eid);         G.uu(x, y);       }     }     for (int i(0); i < (n); i++)       if (G.ff(i) != G.ff(0)) {         puts( -1 );         exit(0);       }   }   void kruskal_wo_0(vector<int>& ret) {     uf G(n);     for (int i(0); i < (m); i++) {       int eid(perm[i]), x(e[eid].first), y(e[eid].second), xr(G.ff(x)),           yr(G.ff(y));       if (x == 0 || y == 0) continue;       if (xr - yr) {         ret.push_back(eid);         G.uu(x, y);       }     }   }   int koppel[5005];   int main() {     cin >> n >> m >> k;     int deg0(0);     for (int i(0); i < (m); i++) {       int x, y, w;       cin >> x >> y >> w;       x--, y--;       wt[i] = w;       e[i].first = x, e[i].second = y;       if (x == 0 || y == 0) deg0++;     }     if (deg0 < k) {       puts( -1 );       exit(0);     }     for (int i(0); i < (m); i++) perm[i] = i;     sort(perm, perm + m, mycmp());     vector<int> mst_met, mst_wo;     kruskal_met_0(mst_met);     kruskal_wo_0(mst_wo);     uf G(n);     for (int i(0); i < (n); i++) koppel[i] = inf;     vector<int> doen;     for (__typeof((mst_met).end()) it((mst_met).begin()); it != (mst_met).end();          ++it) {       int eid(*it), x(e[eid].first), y(e[eid].second), xr(G.ff(x)), yr(G.ff(y));       if (x == 0)         koppel[yr] = wt[eid];       else if (y == 0)         koppel[xr] = wt[eid];       else {         int mi(min(koppel[xr], koppel[yr]));         koppel[xr] = koppel[yr] = mi;         G.uu(x, y);         doen.push_back(eid);       }     }     for (int iteratie(0); iteratie < (deg0 - k); iteratie++) {       pair<int, int> opt(inf, 0);       for (__typeof((mst_wo).end()) it((mst_wo).begin()); it != (mst_wo).end();            ++it) {         int eid(*it), x(e[eid].first), y(e[eid].second), xr(G.ff(x)), yr(G.ff(y));         if (xr == yr) continue;         opt = min(opt, make_pair(wt[eid] - max(koppel[xr], koppel[yr]), eid));       }       if (opt.first == inf) {         puts( -1 );         return 0;       }       int eid(opt.second), x(e[eid].first), y(e[eid].second), xr(G.ff(x)),           yr(G.ff(y));       int mik(min(koppel[xr], koppel[yr]));       koppel[xr] = koppel[yr] = mik;       G.uu(x, y);       doen.push_back(eid);     }     uf T(n);     for (__typeof((doen).end()) it((doen).begin()); it != (doen).end(); ++it) {       int eid(*it), x(e[eid].first), y(e[eid].second);       assert(T.ff(x) != T.ff(y));       T.uu(x, y);     }     if (((int)(doen).size()) != n - 1 - k) {       puts( -1 );       return 0;     }     for (int i(0); i < (m); i++) {       int eid(perm[i]), x(e[eid].first), y(e[eid].second), xr(T.ff(x)),           yr(T.ff(y));       if (x == 0 || y == 0) {         if (xr - yr) {           doen.push_back(eid);           T.uu(x, y);         }       }     }     cout << ((int)(doen).size()) << endl;     int som(0);     for (__typeof((doen).end()) it((doen).begin()); it != (doen).end(); ++it) {       cout << (*it + 1) <<    ;       som += wt[*it];     }     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 5;   const int ZERO = 2e5 + 5000;   int n, w, h;   struct node {     int id, x, t;     node() {}     node(int _id, int _x, int _t) : id(_id), x(_x), t(_t) {}     bool operator<(const node &a) const { return x < a.x; }   } X[MAXN];   vector<node> Y[MAXN * 4];   int cnt[MAXN * 4];   pair<int, int> ans[MAXN];   int main() {     scanf( %d%d%d , &n, &w, &h);     int tot = 0;     for (int i = 1; i <= n; i++) {       int g, p, t;       scanf( %d%d%d , &g, &p, &t);       if (g == 1) {         X[++tot] = node(i, p, t);       } else {         Y[p - t + ZERO].push_back(node(i, p, t));       }     }     sort(X + 1, X + 1 + tot);     for (int i = 0; i < 4 * MAXN; i++) {       sort(Y[i].begin(), Y[i].end());     }     for (int i = 1; i <= tot; i++) {       int pos = X[i].x - X[i].t + ZERO;       int sz = Y[pos].size();       if (sz) {         int en = ((sz - cnt[pos] - 1) % sz + sz) % sz;         swap(Y[pos][en].id, X[i].id);         cnt[pos]++;       }       ans[X[i].id] = make_pair(X[i].x, h);     }     for (int i = 0; i < 4 * MAXN; i++) {       int sz = Y[i].size();       for (int j = 0; j < sz; j++) {         int pos = (((sz - cnt[i] - 1) % sz + sz) % sz + j + 1) % sz;         ans[Y[i][pos].id] = make_pair(w, Y[i][j].x);       }     }     for (int i = 1; i <= n; i++) {       printf( %d %d n , ans[i].first, ans[i].second);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char ans[1000005], tmp[1000005];   int wa;   int n, x;   void go(int x, int y) {     int len = 0;     while (x != y) {       if (x < y) {         y -= x;         tmp[len++] =  B ;       } else {         x -= y;         tmp[len++] =  T ;       }       if (len >= n) return;     }     tmp[len++] =  T ;     int w = 0;     if (x > 1 || len != n) return;     for (int i = 0; i + 1 < len; i++)       if (tmp[i] == tmp[i + 1]) w++;     if (wa > w) {       wa = w;       for (int i = 0; i < n; i++) ans[i] = tmp[i];     }   }   int main() {     while (scanf( %d%d , &n, &x) == 2) {       wa = 10000000;       for (int i = 1; i <= x; i++) {         go(i, x);         go(x, i);       }       if (wa == 10000000)         puts( IMPOSSIBLE );       else {         reverse(ans, ans + n);         printf( %d n , wa);         for (int i = 0; i < n; i++) putchar(ans[i]);         puts(  );       }     }     return 0;   }


#include <bits/stdc++.h>   const int N = 5e5 + 10, M = 1e7 + 10;   const long long mod = 1e9 + 7;   using namespace std;   map<pair<int, int>, set<int>> mp;   int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }   int x[N], y[N];   int main() {     int n;     cin >> n;     for (int i = 1; i <= n; i++) scanf( %d%d , &x[i], &y[i]);     long long cnt = 0, res = 0;     for (int i = 1; i < n; i++) {       for (int j = i + 1; j <= n; j++) {         int a = y[i] - y[j], b = x[i] - x[j];         int c = y[i] * x[j] - y[j] * x[i];         int g = gcd(a, b);         a /= g;         b /= g;         c /= g;         if (a < 0 || (a == 0 && b < 0)) a = -a, b = -b;         pair<int, int> p = make_pair(a, b);         if (mp[p].find(c) == mp[p].end()) {           cnt++;           mp[p].insert(c);           res += cnt - mp[p].size();         }       }     }     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int aa[105][105];   pair<int, int> pos[105][105];   int main() {     int n, m, q;     scanf( %d %d %d , &n, &m, &q);     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         pos[i][j] = {i, j};         aa[i][j] = INT_MAX;       }     }     for (int l = 1; l <= q; ++l) {       int a;       scanf( %d , &a);       if (a == 3) {         int r, c, x;         scanf( %d %d %d , &r, &c, &x);         aa[pos[r][c].first][pos[r][c].second] = x;       } else {         int x;         scanf( %d , &x);         if (a == 1) {           pair<int, int> temp = pos[x][1];           for (int i = 1; i <= m; ++i) {             pos[x][i] = pos[x][i + 1];           }           pos[x][m] = temp;         } else {           pair<int, int> temp = pos[1][x];           for (int i = 1; i <= n; ++i) {             pos[i][x] = pos[i + 1][x];           }           pos[n][x] = temp;         }       }     }     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         if (aa[i][j] == INT_MAX) {           printf( 1  );         } else {           printf( %d  , aa[i][j]);         }       }       printf(  n );     }   }


#include <bits/stdc++.h>   using namespace std;   const int mxN = 500, M = 1e9 + 7;   int n, a[mxN];   long long dp[mxN][mxN];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n;     for (int i = 0; i < n; ++i) cin >> a[i], --a[i];     dp[0][0] = 1;     for (int i = 1; i < n; ++i) dp[i][i] = dp[i - 1][i] = 1;     for (int i = n - 1; ~i; --i) {       for (int j = i + 2; j < n; ++j) {         dp[i][j] = dp[i + 1][j];         for (int k = i + 1; k < j; ++k)           if (a[k + 1] > a[i + 1])             dp[i][j] = (dp[i][j] + dp[i + 1][k] * dp[k][j]) % M;       }     }     cout << dp[0][n - 1];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   constexpr int inf = 1e9;   int main() {     int n, m;     cin >> n >> m;     vector<int> d(m), h(m);     for (int i = 0; i < m; ++i) {       cin >> d[i] >> h[i];     }     int ans = max(h[0] + d[0] - 1, h.back() + (n - d.back()));     for (int i = 0; i + 1 < m; ++i) {       if (abs(h[i] - h[i + 1]) > d[i + 1] - d[i]) {         ans = -1;         break;       }       ans = max(ans, (d[i + 1] - d[i] - abs(h[i] - h[i + 1])) / 2 +                          max(h[i], h[i + 1]));     }     if (ans == -1) {       cout <<  IMPOSSIBLE  << endl;     } else {       cout << ans << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   const int mod = 1e9 + 7;   void show2d(const vector<vector<int>>& adj) {     for (auto x : adj) {       for (auto y : x) {         cout << y <<    ;       }       cout <<   n ;     }   }   long long int modexp(long long int a, long long int b, long long int mod) {     long long int x = 1;     while (b) {       if (b & 1) x = ((x % mod) * (a % mod)) % mod;       a = ((a % mod) * (a % mod)) % mod;       b = b / 2;     }     return x;   }   void rf() {}   int32_t main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int n, m, k;     cin >> n >> m >> k;     int o = 0;     int tt = 0;     for (int i = 0; i < n; ++i) {       int ch;       cin >> ch;       if (ch == 1)         ++o;       else         ++tt;     }     tt = max(tt - k, 0);     cout << max((o + tt) - m, 0) <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   void print(T a) {     for (auto i = a.begin(); i != a.end(); i++) cout << *i <<    ;     cout << endl;   }   bool isVowel(char c) {     if (c ==  a  || c ==  e  || c ==  i  || c ==  o  || c ==  u ) return true;     return false;   }   void sync() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);   }   vector<long long int> G[1000000];   map<long long int, bool> ck;   bool dfs(long long int a, long long int b) {     if (a == b) return true;     ck[a] = true;     for (long long int j : G[a]) {       if (ck[j] == true) continue;       bool d = dfs(j, b);       if (d == true) return true;     }     return false;   }   int main() {     sync();     long long int t;     cin >> t;     long long int j = 0;     vector<pair<long long int, long long int> > edges;     for (long long int testcase = 1; testcase <= t; testcase++) {       long long int a, b, c;       cin >> a >> b >> c;       if (a == 2) {         b--, c--;         ck.clear();         bool d = dfs(b, c);         if (d == true)           cout <<  YES  << endl;         else           cout <<  NO  << endl;       } else {         edges.push_back({b, c});         if (j == 0) {         } else {           for (long long int i = 0; i < j; i++) {             long long int x1 = edges[i].first;             long long int y1 = edges[i].second;             if ((x1 > b && x1 < c) || (y1 > b && y1 < c)) {               G[i].push_back(j);             }             if ((b > x1 && b < y1) || (c > x1 && c < y1)) {               G[j].push_back(i);             }           }         }         j++;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 10000;   const int M = 1000000;   const int inf = 1e9;   struct Edge {     int v, w, cost, next;   } g[M];   int head[N], st, ed, flow, cost, cnt;   void init() {     memset(head, -1, sizeof(head));     flow = cost = cnt = 0;   }   void addEdge(int u, int v, int w, int cost) {     g[cnt].v = v, g[cnt].w = w, g[cnt].cost = cost, g[cnt].next = head[u],     head[u] = cnt++;     g[cnt].v = u, g[cnt].w = 0, g[cnt].cost = -cost, g[cnt].next = head[v],     head[v] = cnt++;   }   bool spfaFlow(int n) {     vector<int> dis(n + 1, inf);     vector<bool> vis(n + 1, false);     vector<int> path(n + 1, -1);     dis[st] = 0;     vis[st] = true;     queue<int> q;     q.push(st);     while (!q.empty()) {       int u = q.front();       q.pop();       vis[u] = false;       for (int i = head[u]; ~i; i = g[i].next) {         int v = g[i].v;         if (g[i].w && dis[v] > dis[u] + g[i].cost) {           dis[v] = dis[u] + g[i].cost;           path[v] = i;           if (!vis[v]) {             vis[v] = true;             q.push(v);           }         }       }     }     int mxflow = inf;     if (path[ed] != -1) {       for (int u = ed; u != st; u = g[path[u] ^ 1].v) {         mxflow = min(mxflow, g[path[u]].w);       }       flow += mxflow;       for (int u = ed; u != st; u = g[path[u] ^ 1].v) {         g[path[u]].w -= mxflow;         g[path[u] ^ 1].w += mxflow;         cost += mxflow * g[path[u]].cost;       }     }     return path[ed] != -1;   }   int minCost(int n) {     while (spfaFlow(n))       ;     return cost;   }   int main(int argc, char *argv[]) {     int n;     cin >> n;     string s;     cin >> s;     vector<int> b(n + 1);     for (int i = 1; i <= n; i++) {       cin >> b[i];     }     int N = 26 + n * 26 + (n / 2) * 26 + (n / 2);     st = N + 1, ed = N + 2;     init();     vector<int> cnt(255);     for (auto c : s) {       cnt[c]++;     }     int Z = n * 26 + (n / 2) * 27;     for (int i =  a ; i <=  z ; i++) {       addEdge(st, Z + i -  a  + 1, cnt[i], 0);       for (int j = 1; j <= n; j++) {         addEdge(Z + i -  a  + 1, (i -  a ) * n + j, 1, i == s[j - 1] ? -b[j] : 0);       }     }     for (int i = 0; i < 26; i++) {       for (int j = 1; j <= n / 2; j++) {         int ux = i * n + j, uy = i * n + n + 1 - j;         addEdge(ux, n * 26 + (n / 2) * i + j, 1, 0);         addEdge(uy, n * 26 + (n / 2) * i + j, 1, 0);         addEdge(n * 26 + (n / 2) * i + j, n * 26 + (n / 2) * 26 + j, 1, 0);       }     }     for (int j = 1; j <= n / 2; j++) {       addEdge(n * 26 + (n / 2) * 26 + j, ed, 2, 0);     }     cout << -minCost(N + 2) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long double pi = 3.141592653589793238;   set<long long>::iterator it;   multiset<long long>::iterator itr;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     long long t;     t = 1;     while (t--) {       long long n;       cin >> n;       long long m, a, b;       cin >> m >> a >> b;       long long r = n % m;       cout << min(b * (r), a * (m - r)) << endl;     }   }


#include <bits/stdc++.h>   using ll = long long;   using namespace std;   const int MOD = 1e9 + 7;   map<int, vector<int>> nodecolor;   map<int, vector<int>> tree;   void dfs(vector<int>& vis, int node, vector<int>& color) {     int blue = 0, c = 0, red = 0;     if (color[node - 1] == 0) {       c += 1;     }     if (color[node - 1] == 1) {       red += 1;     }     if (color[node - 1] == 2) {       blue += 1;     }     for (auto x : tree[node]) {       if (vis[x] == 0) {         vis[x] = 1;         dfs(vis, x, color);         blue += nodecolor[x][0];         red += nodecolor[x][1];         c += nodecolor[x][2];       }     }     nodecolor[node] = {blue, red, c};   }   int dfs2(vector<int>& vis, int node, int b, int r, int c, int ans) {     int b1 = 0, r1 = 0, b2 = 0, r2 = 0;     for (auto x : tree[node]) {       if (vis[x] == 0) {         vis[x] = 1;         b1 = b - (nodecolor[x][0]);         b2 = nodecolor[x][0];         r1 = r - (nodecolor[x][1]);         r2 = nodecolor[x][1];         if ((b1 == 0 || r1 == 0) && (b2 == 0 || r2 == 0)) {           ans += 1;         }         ans += dfs2(vis, x, b, r, c, 0);       }     }     return ans;   }   int main(int argc, const char* argv[]) {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, u, v;     cin >> n;     vector<int> color(n);     int b = 0, r = 0, c = 0;     for (int i = 0; i < n; i++) {       cin >> color[i];       b += (color[i] == 2);       c += (color[i] == 0);       r += (color[i] == 1);     }     for (int i = 0; i < n - 1; i++) {       cin >> u >> v;       tree[u].push_back(v);       tree[v].push_back(u);     }     int ans = 0;     vector<int> vis(n + 1, 0);     vis[1] = 1;     dfs(vis, 1, color);     for (int i = 0; i < n + 1; i++) {       vis[i] = 0;     }     vis[1] = 1;     ans = dfs2(vis, 1, b, r, c, 0);     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long in() {     int32_t x;     scanf( %d , &x);     return x;   }   const long long maxn = 1e6 + 10;   const long long mod = 1e9 + 7;   const long long inf = 1e9 + 10;   long long dp[maxn];   long long ans[maxn];   int32_t main() {     dp[1] = 1;     for (long long i = 2; i < maxn; i++) dp[i] = dp[i / 2] * 2;     long long n = in();     if (n & 1) {       cout <<  NO n ;     } else {       long long nn = n;       while (n) {         long long maxPower = dp[n];         for (long long x = 1; x <= n - maxPower + 1; x++)           ans[maxPower - x] = maxPower + x - 1,                          ans[maxPower + x - 1] = maxPower - x;         n = 2 * maxPower - n - 2;       }       cout <<  YES n ;       for (long long i = 1; i <= nn; i++) cout << ans[i] <<    ;       cout <<   n ;       n = nn;     }     if (n <= 7) {       bool ok = false;       long long arr[7];       for (long long i = 0; i < n; i++) arr[i] = i + 1;       do {         bool fl = true;         for (long long i = 0; i < n; i++) {           if ((arr[i] & (i + 1)) && arr[i] != i + 1) continue;           fl = false;         }         if (fl) {           ok = true;           break;         }       } while (next_permutation(arr, arr + n));       if (ok) {         cout <<  YES n ;         for (long long i = 0; i < n; i++) cout << arr[i] <<    ;       } else         cout <<  NO n ;     } else {       if (__builtin_popcountll(n) == 1)         cout <<  NO n ;       else {         cout <<  YES n ;         long long arr[7];         for (long long i = 0; i < 7; i++) arr[i] = i + 1;         do {           bool fl = true;           for (long long i = 0; i < 7; i++) {             if ((arr[i] & (i + 1)) && arr[i] != i + 1) continue;             fl = false;           }           if (fl) break;         } while (next_permutation(arr, arr + 7));         for (long long i = 0; i < 7; i++) cout << arr[i] <<    ;         for (long long i = 8; i <= n; i *= 2) {           long long lim = min(2 * i - 1, n);           for (long long x = i; x <= lim; x++) {             if (x == i)               cout << lim <<    ;             else               cout << x - 1 <<    ;           }         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, t, ans = 0, i, a, b, m, x, y;     cin >> t;     while (t > 0) {       cin >> n;       vector<int> res;       long long es = 0, os = 0;       if ((n / 2) % 2 == 0) {         cout <<  YES  << endl;         for (i = 0; i < n / 2; i++) {           res.push_back(2 * (i + 1));           es += 2 * (i + 1);         }         for (i = 0; i < n / 2; i++) {           res.push_back(2 * (i + 1) - 1);           os += 2 * (i + 1) - 1;         }         res[n - 1] += es - os;         for (i = 0; i < n; i++) cout << res[i] <<    ;         cout << endl;       } else         cout <<  NO  << endl;       t--;     }   }


#include <bits/stdc++.h>   const int INF = 0x3f3f3f3f;   const int kN = 100 + 5;   const int MOD = (int)1e9 + 7;   int n, limit;   int graph[kN][kN];   std::vector<int> edges[kN];   int dp[kN][kN];   void dfs(int u, int fa) {     for (int w = 0; w < n; ++w)       if (graph[u][w] <= limit) dp[u][w] = 1;     for (int v : edges[u]) {       if (fa == v) continue;       dfs(v, u);     }     for (int w = 0; w < n; ++w) {       if (dp[u][w] == 0) continue;       for (int v : edges[u]) {         int slot = 0;         if (v == fa) continue;         for (int r = 0; r < n; ++r) {           if (dp[v][r] == 0) continue;           if (graph[u][r] < graph[u][w] || graph[u][r] == graph[u][w] && r < w)             continue;           if (graph[v][w] < graph[v][r] || graph[v][w] == graph[v][r] && w < r)             continue;           slot += dp[v][r];           if (slot >= MOD) slot -= MOD;         }         dp[u][w] = dp[u][w] * 1ll * slot % MOD;       }     }   }   int main() {     scanf( %d%d , &n, &limit);     for (int i = 0; i < n; ++i)       for (int j = 0; j < n; ++j) graph[i][j] = i == j ? 0 : INF;     for (int i = 0; i < n - 1; ++i) {       int a, b;       scanf( %d%d , &a, &b);       a--;       b--;       edges[a].emplace_back(b);       edges[b].emplace_back(a);       graph[a][b] = graph[b][a] = 1;     }     for (int k = 0; k < n; ++k)       for (int i = 0; i < n; ++i)         for (int j = 0; j < n; ++j)           graph[i][j] = std::min(graph[i][j], graph[i][k] + graph[k][j]);     dfs(0, -1);     int result = 0;     for (int v = 0; v < n; ++v) {       result = (result + dp[0][v]) % MOD;     }     printf( %d n , result);   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1001;   int a[N][N], LR[N][N], RR[N][N], LC[N][N], RC[N][N];   int main() {     int n, m;     cin >> n >> m;     for (int i = 1; i <= n; ++i) {       vector<int> v;       for (int j = 1; j <= m; ++j) {         cin >> a[i][j];         v.push_back(a[i][j]);       }       sort(v.begin(), v.end());       auto last = unique(v.begin(), v.end());       v.erase(last, v.end());       for (int j = 1; j <= m; ++j) {         int l = 0, r = (int)v.size() - 1;         while (l <= r) {           int m = (l + r) / 2;           if (v[m] >= a[i][j]) {             if (v[m] == a[i][j]) {               LR[i][j] = m;               RR[i][j] = (int)v.size() - 1 - m;             }             r = m - 1;           } else {             l = m + 1;           }         }       }     }     for (int i = 1; i <= m; ++i) {       set<int> s;       vector<int> v;       for (int j = 1; j <= n; ++j) {         v.push_back(a[j][i]);       }       sort(v.begin(), v.end());       auto last = unique(v.begin(), v.end());       v.erase(last, v.end());       for (int j = 1; j <= n; ++j) {         int l = 0, r = (int)v.size() - 1;         while (l <= r) {           int m = (l + r) / 2;           if (v[m] >= a[j][i]) {             if (v[m] == a[j][i]) {               LC[j][i] = m;               RC[j][i] = (int)v.size() - 1 - m;             }             r = m - 1;           } else {             l = m + 1;           }         }       }     }     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         cout << max(LC[i][j], LR[i][j]) + 1 + max(RC[i][j], RR[i][j]) <<    ;       }       cout << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   namespace fast_IO {   inline int read_int() {     register int ret = 0, f = 1;     register char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) f = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       ret = (ret << 1) + (ret << 3) + int(c - 48);       c = getchar();     }     return ret * f;   }   }  // namespace fast_IO   using namespace fast_IO;   int N, Q;   int a[200005];   char s[200005];   struct BIT {     int c[200005];     inline int lowbit(int x) { return x & (-x); }     inline void add(int pos, int w) {       while (pos <= N) c[pos] += w, pos += lowbit(pos);     }     inline int query(int pos) {       int res = 0;       while (pos) res += c[pos], pos -= lowbit(pos);       return res;     }   } BIT[3];   set<int> S[3];   inline int decode(char c) {     if (c ==  R ) return 0;     if (c ==  P ) return 1;     if (c ==  S ) return 2;   }   inline void init() {     N = read_int(), Q = read_int();     scanf( %s , s + 1);     for (register int i = 0; i < 3; i++)       S[i].insert(-0x3f3f3f3f), S[i].insert(0x3f3f3f3f);     for (register int i = 1; i <= N; i++) {       a[i] = decode(s[i]);       BIT[a[i]].add(i, 1);       S[a[i]].insert(i);     }   }   inline int calc() {     if (S[0].size() == N + 2 || S[1].size() == N + 2 || S[2].size() == N + 2)       return N;     if (S[0].size() == 2) return S[2].size() - 2;     if (S[1].size() == 2) return S[0].size() - 2;     if (S[2].size() == 2) return S[1].size() - 2;     set<int>::iterator it_min[3], it_max[3];     for (register int i = 0; i < 3; i++) it_min[i] = S[i].begin(), it_min[i]++;     for (register int i = 0; i < 3; i++)       it_max[i] = S[i].end(), it_max[i]--, it_max[i]--;     int ans = N;     for (register int i = 0; i < 3; i++) {       int l1 = (i + 1) % 3, r1 = (i + 2) % 3;       int L = *it_min[l1], R = *it_min[r1] - 1;       ans -= max(0, BIT[i].query(R) - BIT[i].query(L - 1));       L = *it_max[r1], R = *it_max[l1];       ans -= max(0, BIT[i].query(R) - BIT[i].query(L - 1));     }     return ans;   }   inline void ans() {     printf( %d n , calc());     register int pos, nw;     static char opt[20];     while (Q--) {       pos = read_int();       scanf( %s , opt);       nw = decode(opt[0]);       S[a[pos]].erase(pos);       BIT[a[pos]].add(pos, -1);       a[pos] = nw, S[a[pos]].insert(pos), BIT[a[pos]].add(pos, 1);       printf( %d n , calc());     }   }   int main() {     init();     ans();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxx = 500 + 7;   const int maxn = 100 + 7;   const long long mod = 1E9 + 7;   const int maxs = 2 * (1E7) + 5;   const int maxm = 100 + 3;   const long long maxk = 1e13;   long double dp[maxn][maxn];   int last[1000];   char seq[maxn];   long long n, k;   void init() {     cin >> n >> k;     cin >> seq;     memset(last, -1, sizeof(last));   }   void solve() {     dp[0][0] = 1;     for (int i = 1; i <= n; i++) {       for (int j = 0; j <= i; j++) {         if (!j) {           dp[i][j] = 1;           continue;         }         dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1];         if (last[seq[i - 1]] != -1) dp[i][j] -= dp[last[seq[i - 1]]][j - 1];       }       last[seq[i - 1]] = i - 1;     }     long long cost = 0;     long double kk = k;     for (long long i = n; i >= 0; i--) {       long double tt = min(kk, dp[n][i]);       cost += tt * (n - i);       kk -= min(tt, kk);     }     if (kk > 0)       cout << -1 << endl;     else       cout << (long long)cost << endl;   }   int main(void) {     init();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   void read(T& x) {     static char c;     static int f;     for (c = getchar(), f = 1; c <  0  || c >  9 ; c = getchar())       if (c ==  - ) f = -f;     for (x = 0; c >=  0  && c <=  9 ; c = getchar()) x = x * 10 + (c & 15);     x *= f;   }   template <typename T>   void write(T x) {     static char q[65];     int cnt = 0;     if (x < 0) putchar( - ), x = -x;     q[++cnt] = x % 10, x /= 10;     while (x) q[++cnt] = x % 10, x /= 10;     while (cnt) putchar(q[cnt--] +  0 );   }   const int mod = 1000000007, maxn = 1000005;   int mo(const int x) { return x >= mod ? x - mod : x; }   int iac[maxn], fac[maxn], _n[maxn], _m[maxn];   int main() {     int n, m, a, b;     read(n), read(m), read(a), read(b);     int mx = max(n, m);     iac[0] = iac[1] = fac[0] = fac[1] = _n[1] = _m[0] = 1;     _m[1] = m;     for (int i = 2; i <= mx; ++i)       iac[i] = 1ll * (mod - mod / i) * iac[mod % i] % mod;     _n[0] = iac[n];     for (int i = 2; i <= mx; ++i)       iac[i] = 1ll * iac[i - 1] * iac[i] % mod,       fac[i] = 1ll * fac[i - 1] * i % mod, _m[i] = 1ll * _m[i - 1] * m % mod,       _n[i] = 1ll * _n[i - 1] * n % mod;     int ans = 0;     for (int i = 1; i <= n - 1 && i <= m; ++i)       ans = mo(ans + 1ll * (i + 1) * _n[n - 1 - i] % mod * _m[n - 1 - i] % mod *                          iac[n - 1 - i] % mod * iac[m - i] % mod * iac[i - 1] %                          mod);     ans = 1ll * ans * fac[n - 2] % mod * fac[m - 1] % mod;     write(ans), putchar(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int Set(int N, int pos) { return N = N | (1 << pos); }   int reset(int N, int pos) { return N = N & ~(1 << pos); }   bool check(int N, int pos) { return (bool)(N & (1 << pos)); }   string st, st2, st3, st4, ans;   int main() {     ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);     int i, j, a, b, n, m, x, y, ck, t, r, rem, cnt, lo, hi, sum, cs = 1;     cin >> n >> m;     cin >> st;     a = b = m / 2;     for (i = 0; i < n; i++) {       if (st[i] ==  (  && a > 0) ans +=  ( , a--;       if (st[i] ==  )  && b > 0) ans +=  ) , b--;     }     cout << ans;   }


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


#include <bits/stdc++.h>   using namespace std;   const int maxk = 15;   const int maxn = 1e5 + 10;   long long D[maxk][maxn];   long long queryBIT(int k, int x) {     long long ret = 0;     while (x > 0) {       ret += D[k][x];       x -= x & -x;     }     return ret;   }   void addBIT(int k, int x, long long d) {     while (x < maxn) {       D[k][x] += d;       x += x & -x;     }   }   int main() {     int n, k;     cin >> n >> k;     while (n--) {       int a;       cin >> a;       for (int j = 11; j > 0; j--) {         addBIT(j, a, queryBIT(j - 1, a));       }       addBIT(0, a, 1);     }     cout << queryBIT(k, maxn - 1) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100010;   vector<int> g[maxn];   int src[maxn], dest[maxn];   bool vis[maxn];   int main() {     int n, m;     int a, b;     scanf( %d %d , &n, &m);     for (int i = 0; i < m; i++) {       scanf( %d %d , &a, &b);       a--, b--;       g[a].push_back(b);       g[b].push_back(a);     }     queue<int> Q;     for (int i = 0; i < n; i++) {       scanf( %d , &dest[i]);       if (dest[i] == 0) {         Q.push(i);       }     }     int ans = 0;     while (!Q.empty()) {       int i = Q.front();       Q.pop();       if (dest[i] == src[i]) {         vis[i] = true;         ans++;         src[i]++;         int len = g[i].size();         for (int j = 0; j < len; j++) {           src[g[i][j]]++;           if (src[g[i][j]] == dest[g[i][j]]) {             Q.push(g[i][j]);           }         }       }     }     printf( %d n , ans);     for (int i = 0; i < n; i++) {       if (vis[i]) {         ans--;         printf( %d%c , i + 1, ans ?     :   n );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int ls, cc;   char s[1111111], a[1111111];   int main() {     gets(s), ls = strlen(s), cc = 0, a[0] =  # ;     for (int i = 0; i < ls; ++i) {       a[++cc] = s[i];       while (cc > 1 && a[cc - 1] != a[cc]) cc -= 2;     }     for (int i = 1; i <= cc; ++i) putchar(a[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int sz = 100005;   int a[sz], b[sz], sum[sz];   int main() {     int n, m, c;     cin >> n >> m >> c;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 1; i <= m; i++) {       cin >> b[i];       sum[i] = (sum[i - 1] + b[i]) % c;     }     for (int i = 1; i <= n; i++) {       int l, r;       r = min(m, i);       l = max(1, m - (n - i + 1) + 1);       if (l <= r) a[i] += (sum[r] - sum[l - 1] + 3 * c);       a[i] %= c;       cout << a[i] <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 105;   int n, res = 1e9, a[5][N][N];   void calc(int a[N][N], int b[N][N], int c[N][N], int d[N][N]) {     int tmp = 0, tmp2 = 0;     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= n; ++j) {         if (a[i][j] != (i + j) % 2)           ++tmp;         else           ++tmp2;         if (c[i][j] != (i + j) % 2)           ++tmp;         else           ++tmp2;         if (b[i][j] != (i + j) % 2)           ++tmp2;         else           ++tmp;         if (d[i][j] != (i + j) % 2)           ++tmp2;         else           ++tmp;       }     res = min(res, min(tmp, tmp2));   }   int main() {     cin >> n;     for (int i = 1; i <= 4; ++i)       for (int j = 1; j <= n; ++j)         for (int k = 1; k <= n; ++k) {           char z;           cin >> z;           a[i][j][k] = (int)(z -  0 );         }     vector<int> v;     v.push_back(1);     v.push_back(2);     v.push_back(3);     v.push_back(4);     do {       calc(a[v[0]], a[v[1]], a[v[2]], a[v[3]]);     } while (next_permutation(v.begin(), v.end()));     cout << res <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   string name[101];   vector<string> seen;   bool see = false;   int m, n, i, j;   int main() {     cin >> n;     m = n;     i = 1;     int k = 0;     while (n > 0) {       cin >> name[i];       i++;       n--;     }     for (i = 1; i <= m; i++) {       see = false;       for (vector<string>::iterator it = seen.begin(); it != seen.end(); ++it) {         if (name[i] == *it) {           cout <<  YES  << endl;           see = true;           break;         }       }       if (see == false) {         cout <<  NO  << endl;         see = false;         seen.push_back(name[i]);       }     }     return 0;   }


//Jai Shree Ram   #include<bits/stdc++.h>   using namespace std;   #define   f(a,b,n)        for(ll a=b;a<n;a++)   #define   readarr(arr,n)  f(i,0,n) cin>>arr[i];   #define   ll              long long   #define   pb              push_back   #define   fastio            ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)   #define   all(x)          x.begin(), x.end()   #define   mod             1000000007    #define   w(t)            int t; cin>>t; while(t--)    #define   fi              first   #define   sd              second                                                                                #define   sp(x,y)         fixed<<setprecision(y)<<x                 #define   trace(x)     cout<<#x<< :  <<x<<endl;    #define   trav(a, x)      for(auto& a : x)    #define   setbits(x)      __builtin_popcountll(x)     #define   zrobits(x)      __builtin_ctzll(x)     #define   pqmax           priority_queue<ll>   #define   pqmin           priority_queue<ll,vi,greater<ll>>                            #define   vi              vector<ll>   #define   mpi              map<ll,ll>    #define   pi              pair<ll,ll>     #define   N               1e5      #define   inf             1e18    #define   nline             n    void c_a_c()   {    #ifndef ONLINE_JUDGE    freopen( input.txt , r ,stdin);    freopen( output.txt , w ,stdout);    #endif   }   int main()   {    c_a_c();    ll t;    cin>>t;    while(t--)    {     ll n;     cin>>n;        vector<long long> v[n+1];        long long array[n];        for(long long i = 0; i < n; i++) {         cin >> array[i];        }        long long x;        for(long long i = 0; i < n; i++) {         cin >> x;         v[array[i]].push_back(x);        }        for(long long i = 1; i <= n; i++) {         sort(all(v[i]));         reverse(all(v[i]));        }        long long ans[n+1];        for(long long i = 1; i <= n; i++) {         ans[i] = 0;        }        for(long long i  = 1; i <= n; i++) {         for(long long j  = 1; j < v[i].size(); j++) {          v[i][j] += v[i][j-1];         }         for(long long j = 1; j <= v[i].size(); j++) {          long long temp = v[i].size() % j;          ans[j] += v[i][v[i].size() - temp-1];         }        }        for(long long i = 1; i <= n; i++) {         cout << ans[i] <<    ;        }        cout <<   n ;       }   }


#include <bits/stdc++.h>   using namespace std;   bool flag = 0, flag1 = 0, flag2 = 0;   string t =  abacaba ;   int check(string s) {     long long int x = 0;     for (long long int i = 0; i < s.size(); i++) {       if (s.substr(i, 7) == t) {         x++;       }     }     return x;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long int tt = 1;     cin >> tt;     while (tt--) {       long long int n;       cin >> n;       string s;       cin >> s;       long long int ans = 0;       ans = check(s);       if (ans == 0) {         for (long long int i = 0; i < n - 6; i++) {           if (s[i] == t[0] || s[i] ==  ? ) {             flag = 0;             for (long long int j = 0; j < 7; j++) {               if (s[j + i] != t[j] && s[i + j] !=  ? ) {                 flag = 1;                 break;               }             }             if (flag == 0) {               string temp = s;               for (long long int j = 0; j < 7; j++) temp[i + j] = t[j];               if (check(temp) == 1) {                 ans++;                 s = temp;                 break;               }             }           }         }       }       if (ans == 1) {         cout <<  Yes n ;         for (long long int i = 0; i < s.size(); i++) {           if (s[i] ==  ? )             cout <<  d ;           else             cout << s[i];         }         cout <<   n ;       } else         cout <<  No n ;     }   }


#include <bits/stdc++.h>   using namespace std;   struct rua {     long double x, y;   } m;   set<pair<long double, long double> > p;   vector<pair<long double, long double> > del;   vector<long double> d;   set<long double> mm;   int n, ans;   long double x, y;   bool tt;   bool check(rua T) {     d.clear();     rua M = {(T.x + x) / 2, (T.y + y) / 2};     rua mM = {M.x - m.x, M.y - m.y};     rua r0 = {mM.y, -mM.x};     if (!r0.x)       if (tt)         return false;       else         tt = true;     long double k = r0.y / r0.x;     if (mm.count(k))       return false;     else       mm.insert(k);     long double R = pow(r0.x * r0.x + r0.y * r0.y, 0.5);     for (auto i = p.begin(); i != p.end(); i++) {       rua mP = {(*i).first - m.x, (*i).second - m.y};       long double t = r0.x * mP.x + r0.y * mP.y;       t /= r0.x * r0.x + r0.y * r0.y;       if (t) d.push_back(t);     }     long double _ = (r0.x * mM.x + r0.y * mM.y) / R;     int l = 0, r = d.size() - 1;     sort(d.begin(), d.end());     while (l <= r && d[l] + d[r] == 0) l++, r--;     return l > r;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       cin >> x >> y;       p.insert(make_pair(x, y));       m.x += x, m.y += y;     }     m.x = m.x * 1.0 / n, m.y = m.y * 1.0 / n;     for (auto i = p.begin(); i != p.end(); i++) {       x = (*i).first, y = (*i).second;       if (p.count(make_pair(2 * m.x - x, 2 * m.y - y)))         del.push_back(make_pair(2 * m.x - x, 2 * m.y - y)), del.push_back(*i);     }     for (int i = 0; i < del.size(); i++)       if (p.count(del[i])) p.erase(del[i]);     if (!p.size()) return printf( -1 n ), 0;     x = (*p.begin()).first, y = (*p.begin()).second;     for (auto i = p.begin(); i != p.end(); i++)       if (check({(*i).first, (*i).second})) ans++;     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     cin.tie(0);     ios::sync_with_stdio(0);     long long n;     cin >> n;     long long num = 1e5 + 3;     long long pos[num];     for (int i = 0; i < n; ++i) {       long long a;       cin >> a;       pos[a] = i + 1;     }     long long m;     cin >> m;     long long counta = 0, countb = 0;     for (int j = 0; j < m; ++j) {       long long b;       cin >> b;       counta += pos[b];       countb += n - pos[b] + 1;     }     cout << counta <<     << countb;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 10;   const long long inf = 1e9 + 7;   long long mod = 998244353;   vector<int> a;   int solve(vector<int> &b, int bit) {     vector<int> l, r;     if (b.size() == 0 || bit < 0) return 0;     for (int i = 0; i < b.size(); i++) {       if ((b[i] >> bit) & 1 == 1)         l.push_back(b[i]);       else         r.push_back(b[i]);     }     if (l.size() == 0) return solve(r, bit - 1);     if (r.size() == 0) return solve(l, bit - 1);     return min(solve(l, bit - 1), solve(r, bit - 1)) + (1 << bit);   }   int main(void) {     int n;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       int x;       scanf( %d , &x);       a.push_back(x);     }     int ans = solve(a, 30);     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 10;   int n, m, u, v, w, answer, capital, intree, l, dis[N];   vector<pair<int, int> > nei[N];   vector<pair<int, pair<int, int> > > edge;   bool done[N];   priority_queue<pair<int, int> > pq;   int main() {     cin >> n >> m >> capital;     capital--;     for (int i = 0; i < m; i++) {       cin >> u >> v >> w;       u--, v--;       nei[v].push_back(make_pair(u, w));       nei[u].push_back(make_pair(v, w));       edge.push_back(make_pair(w, make_pair(u, v)));     }     cin >> l;     done[capital] = true;     for (int i = 0; i < nei[capital].size(); i++) {       pq.push(make_pair(-nei[capital][i].second, nei[capital][i].first));     }     while (intree < n - 1) {       while (done[(pq.top()).second]) pq.pop();       pair<int, int> p = pq.top();       v = p.second;       dis[v] = -p.first;       done[v] = true;       for (int i = 0; i < nei[v].size(); i++) {         if (!done[nei[v][i].first])           pq.push(make_pair(-dis[v] - nei[v][i].second, nei[v][i].first));       }       intree++;     }     for (int i = 0; i < n; i++) {       answer += (dis[i] == l);     }     for (int i = 0; i < m; i++) {       pair<int, pair<int, int> > pa = edge[i];       for (int j = 1; j < pa.first; j++) {         if (min(dis[pa.second.first] + j, dis[pa.second.second] + pa.first - j) ==             l) {           answer++;         }       }     }     cout << answer;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long n;     cin >> n;     vector<long long> v(n + 1);     long long xr = 0;     for (long long i = 1; i <= n; i++) {       cin >> v[i];       xr ^= v[i];     }     if (xr == 0) {       cout <<  DRAW             <<   n ;       return;     }     long long msb;     for (long long i = 32; ~i; i--) {       if (xr & (1ll << i)) {         msb = i;         break;       }     }     long long count = 0;     for (long long i = 1; i <= n; i++) {       count += ((v[i] & (1ll << msb)) > 0);     }     bool win = (count % 4 == 1 or ((n - count) & 1));     cout << (win ?  WIN  :  LOSE ) <<   n ;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long t;     cin >> t;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ul = unsigned long long;   using ld = long double;   const int N = 101;   int a[N][N];   vector<vector<int>> ans;   void q(int ax, int ay, int bx, int by, int cx, int cy) {     a[ax][ay] ^= 1;     a[bx][by] ^= 1;     a[cx][cy] ^= 1;     ans.push_back({ax, ay, bx, by, cx, cy});   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int t, n, m;     cin >> t;     for (int z = 0; z < (t); z++) {       cin >> n >> m;       string s;       for (int i = 0; i < (n); i++) {         cin >> s;         for (int j = 0; j < (m); j++) a[i][j] = s[j] -  0 ;       }       ans.clear();       for (int i = 0; i < (n - 2); i++)         for (int j = 0; j < (m); j++) {           if (a[i][j]) q(i, j, i + 1, j, i + 1, j + (j == 0 ? 1 : -1));         }       for (int i = 0; i < (m - 2); i++) {         if (a[n - 2][i] && a[n - 1][i])           q(n - 2, i, n - 1, i, n - 2, i + 1);         else if (a[n - 2][i])           q(n - 2, i, n - 2, i + 1, n - 1, i + 1);         else if (a[n - 1][i])           q(n - 1, i, n - 1, i + 1, n - 2, i + 1);       }       int ss = 0;       for (int i = 0; i < (2); i++)         for (int j = 0; j < (2); j++) ss ^= a[n - 2 + i][m - 2 + j];       for (int i = 0; i < (2); i++) {         for (int j = 0; j < (2); j++) {           if (a[n - 2 + i][m - 2 + j] == ss) continue;           vector<int> cv;           for (int i1 = 0; i1 < (2); i1++)             for (int j1 = 0; j1 < (2); j1++)               if (i1 != i || j1 != j) {                 cv.push_back(n - 2 + i1);                 cv.push_back(m - 2 + j1);               }           ans.push_back(cv);         }       }       cout << ans.size() <<   n ;       for (auto &pp : ans) {         for (int i : pp) cout << i + 1 <<    ;         cout <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   template <typename A, typename B>   string to_string(pair<A, B> p);   template <typename A, typename B, typename C>   string to_string(tuple<A, B, C> p);   template <typename A, typename B, typename C, typename D>   string to_string(tuple<A, B, C, D> p);   string to_string(const string& s) { return     + s +    ; }   string to_string(char ch) {     string s;     s += ch;     return s;   }   string to_string(const char* s) { return to_string((string)s); }   string to_string(bool b) { return (b ?  true  :  false ); }   string to_string(vector<bool> v) {     bool first = true;     string res =  { ;     for (int i = 0; i < static_cast<int>(v.size()); i++) {       if (!first) {         res +=  ,  ;       }       first = false;       res += to_string(v[i]);     }     res +=  } ;     return res;   }   template <size_t N>   string to_string(bitset<N> v) {     string res =   ;     for (size_t i = 0; i < N; i++) {       res += static_cast<char>( 0  + v[i]);     }     return res;   }   template <typename A>   string to_string(A v) {     bool first = true;     string res =  { ;     for (const auto& x : v) {       if (!first) {         res +=  ,  ;       }       first = false;       res += to_string(x);     }     res +=  } ;     return res;   }   template <typename A, typename B>   string to_string(pair<A, B> p) {     return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ;   }   template <typename A, typename B, typename C>   string to_string(tuple<A, B, C> p) {     return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   +            to_string(get<2>(p)) +  ) ;   }   template <typename A, typename B, typename C, typename D>   string to_string(tuple<A, B, C, D> p) {     return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   +            to_string(get<2>(p)) +  ,   + to_string(get<3>(p)) +  ) ;   }   void debug_out() { cerr << endl; }   template <typename Head, typename... Tail>   void debug_out(Head H, Tail... T) {     cerr <<     << to_string(H);     debug_out(T...);   }   long long expmod(long long x, long long n, long long mod) {     if (n == 0)       return 1;     else if (n % 2 == 0) {       long long temp = expmod(x, n / 2, mod);       return (temp * temp) % mod;     } else {       return (expmod(x, n - 1, mod) * x) % mod;     }   }   long long modinv(long long x, long long mod) { return expmod(x, mod - 2, mod); }   void solve() {     int n;     cin >> n;     long long mod = 1e9 + 7;     long long p = 0, q = 0;     int even = 0;     long long power = 2;     for (int i = 0; i < n; i++) {       long long x;       cin >> x;       even |= (x % 2 == 0);       power = expmod(power, x, mod);     }     42;     power = power * modinv(2, mod) % mod;     p = power + (even ? 1 : -1);     p = (p + mod) % mod;     p = p * modinv(3, mod) % mod;     q = power;     42;     cout << p <<  /  << q <<   n ;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t = 1;     for (int tc = 1; tc <= t; tc++) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   int Read() {     char c;     while (c = getchar(), (c !=  - ) && (c <  0  || c >  9 ))       ;     bool neg = (c ==  - );     int ret = (neg ? 0 : c - 48);     while (c = getchar(), c >=  0  && c <=  9 ) ret = ret * 10 + c - 48;     return neg ? -ret : ret;   }   const int MAXN = 1005;   int N, n, a[MAXN];   pair<int, int> ans[MAXN * MAXN];   void init() {     scanf( %d , &N);     for (int i = 1; i <= N; i++) a[i] = Read(), n += (a[i] > 0);   }   void work() {     if (n < 2) {       puts( -1 );       return;     } else       n = 0;     int B = 1, C = 2;     for (int i = 3; i <= N; i++)       for (int A = i;;) {         if (a[B] < a[A]) swap(A, B);         if (a[C] < a[B]) swap(C, B);         if (a[B] < a[A]) swap(A, B);         if (!a[A]) break;         for (int j = a[B] / a[A]; j; j >>= 1)           if (j & 1)             ans[++n] = make_pair(A, B), a[B] -= a[A], a[A] <<= 1;           else             ans[++n] = make_pair(A, C), a[C] -= a[A], a[A] <<= 1;       }     printf( %d n , n);     for (int i = 1; i <= n; i++) printf( %d %d n , ans[i].first, ans[i].second);   }   int main() {     init();     work();     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   inline long long read() {     long long ret = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) ret = ret * 10 + ch -  0 , ch = getchar();     return ret * f;   }   const long long maxn = 100005;   long long n, A, cj, cm, m;   pair<long long, long long> a[maxn];   long long sum[maxn];   long long ans = 0, ansx = -1, ansy = -1;   inline bool cmp_value(pair<long long, long long> aa,                         pair<long long, long long> bb) {     return aa.first < bb.first;   }   inline bool cmp_id(pair<long long, long long> aa,                      pair<long long, long long> bb) {     return aa.second < bb.second;   }   inline long long get_all_sum(long long x, long long up) {     long long L = 1, R = up;     long long ret = 0;     while (L <= R) {       long long mid = ((R - L) >> 1) + L;       if (a[mid].first > x)         R = mid - 1;       else         ret = x * mid - sum[mid], L = mid + 1;     }     return ret;   }   signed main() {     n = read();     A = read();     cj = read();     cm = read();     m = read();     for (long long i = 1; i <= n; i++) a[i].first = read(), a[i].second = i;     sort(a + 1, a + 1 + n, cmp_value);     for (long long i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i].first;     long long need = 0;     for (long long i = n + 1; i >= 1; i--) {       if (i <= n) need += A - a[i].first;       if (need > m) break;       long long L = 0, R = A;       while (L <= R) {         long long mid = ((R - L) >> 1) + L;         long long all_sum = get_all_sum(mid, i - 1);         if (all_sum > m - need)           R = mid - 1;         else {           if ((n - i + 1) * cj + mid * cm > ans) {             ans = (n - i + 1) * cj + mid * cm;             ansx = mid;             ansy = i;           }           L = mid + 1;         }       }     }     printf( %lld n , ans);     if (ansx != -1 && ansy != -1) {       for (long long i = 1; i < ansy; i++) a[i].first = max(ansx, a[i].first);       for (long long i = ansy; i <= n; i++) a[i].first = A;     }     sort(a + 1, a + 1 + n, cmp_id);     for (long long i = 1; i <= n; i++) printf( %lld  , a[i].first);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline void read(int &x) {     char c;     int flag = 1;     while ((c = getchar()) <  0  || c >  9 )       if (c ==  - ) flag *= -1;     x = c -  0 ;     while ((c = getchar()) >=  0  && c <=  9 ) x = x * 10 + c -  0 ;     x *= flag;     return;   }   inline void read(long long &x) {     char c;     int flag = 1;     while ((c = getchar()) <  0  || c >  9 )       if (c ==  - ) flag *= -1;     x = c -  0 ;     while ((c = getchar()) >=  0  && c <=  9 ) x = x * 10 + c -  0 ;     x *= flag;     return;   }   int dp[5005][5005];   int arr[5005];   int main() {     int n;     read(n);     for (int i = 1; i <= n; ++i) {       read(arr[i]);       dp[i][1] = arr[i];     }     for (int i = 2; i <= n; ++i) {       for (int j = 1; j <= n; ++j) {         dp[j][i] = dp[j + 1][i - 1] ^ dp[j][i - 1];       }     }     for (int i = 2; i <= n; ++i) {       for (int j = 1; j <= n; ++j) {         dp[j][i] = max(dp[j][i], max(dp[j + 1][i - 1], dp[j][i - 1]));       }     }     int q;     cin >> q;     while (q--) {       int l, r;       read(l);       read(r);       printf( %d n , dp[l][r - l + 1]);     }   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   int add(int a, int b) { return (a + b) % MOD; }   int sub(int a, int b) { return add(a, MOD - b); }   int mul(int a, int b) { return ((long long)a * b) % MOD; }   int inv(int a) {     int b = 1;     for (int i = 0; (1 << i) <= MOD - 2; i++) {       if ((1 << i) & (MOD - 2)) b = mul(b, a);       a = mul(a, a);     }     return b;   }   const int MAXN = 1e5 + 10;   vector<int> e[MAXN];   int val[MAXN], sz[MAXN], parent[MAXN];   map<int, int> dist[MAXN];   int A[MAXN], B[MAXN], total;   bool active[MAXN];   vector<int> in[MAXN], out[MAXN];   int calc_sz(int i, int p) {     sz[i] = 1;     for (int j : e[i]) {       if (!active[j] || j == p) continue;       sz[i] += calc_sz(j, i);     }     return sz[i];   }   int find_centroid(int i, int p, int n) {     for (int j : e[i]) {       if (!active[j] || j == p) continue;       if (sz[j] > n / 2) return find_centroid(j, i, n);     }     return i;   }   void calc_dist(int i, int p, int r, int d) {     dist[i][r] = d;     for (int j : e[i]) {       if (!active[j] || j == p) continue;       calc_dist(j, i, r, d + 1);     }   }   int build_centroid(int i) {     i = find_centroid(i, -1, calc_sz(i, -1));     calc_dist(i, -1, i, 0);     active[i] = false;     for (int j : e[i]) {       if (!active[j]) continue;       parent[build_centroid(j)] = i;     }     return i;   }   void update(int i, int k) {     int v = mul(val[i], MOD + k);     for (int p = i; p != -1; p = parent[p]) {       total = sub(total, mul(A[p], B[p]));       A[p] = add(A[p], mul(v, dist[i][p]));       if (parent[p] != -1) A[p] = sub(A[p], mul(v, dist[i][parent[p]]));       B[p] = add(B[p], v);       total = add(total, mul(A[p], B[p]));     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     int t = 1;     for (int i = 0; i < n; i++) {       int l, r;       cin >> l >> r;       in[l].push_back(i);       out[r].push_back(i);       val[i] = inv(r - l + 1);       t = mul(t, r - l + 1);       active[i] = true;     }     for (int i = 0; i < n - 1; i++) {       int x, y;       cin >> x >> y;       x--;       y--;       e[x].push_back(y);       e[y].push_back(x);     }     parent[build_centroid(0)] = -1;     int sol = 0;     for (int color = 0; color < MAXN; color++) {       for (int i : in[color]) update(i, 1);       sol = add(sol, total);       for (int i : out[color]) update(i, -1);     }     cout << mul(sol, t);     return 0;   }


#include <bits/stdc++.h>   #pragma hdrstop   using namespace std;   void solve();   int main() {     int t = 1;     while (t--) solve();     return 0;   }   long long n, k;   long long dp[5000][5000];   vector<pair<long long, long long> > a;   long long b[5000];   void solve() {     cin >> n >> k;     a.push_back(make_pair(1, 0));     b[0] = 1;     for (long long i = 0; i < n; i++) {       long long q, w;       cin >> q >> w;       a.push_back(make_pair(q, w));       b[i + 1] = q;     }     a.push_back(make_pair(86401, 0));     b[n + 1] = 86401;     long long border = 0;     for (long long i = 0; i <= n + 5; i++)       for (long long j = 0; j <= i; j++) dp[i][j] = (1LL << 50);     dp[0][0] = 1;     for (long long i = 0; i <= n; i++)       for (long long j = 0; j <= i; j++) {         pair<long long, long long> cur = a[i + 1];         dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j]);         long long now = max(b[i + 1], dp[i][j]) + cur.second;         dp[i + 1][j] = min(dp[i + 1][j], now);       }     long long ans = 0;     for (long long i = 1; i <= n + 1; i++) {       for (long long j = 0; j <= k; j++) {         long long cur = dp[i - 1][j];         long long now = b[i];         ans = max(ans, now - cur);       }     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   long long arr[500005];   map<long long, int> mm;   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %lld , &arr[i]);     for (int i = 1; i <= n - 1; i++) arr[i] += arr[i - 1];     for (int i = 0; i < n; i++) mm[arr[i]]++;     int mx = -1;     map<long long, int>::iterator it;     for (it = mm.begin(); it != mm.end(); it++) {       mx = max(mx, it->second);     }     printf( %d n , n - mx);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100007;   long long i, j, k, n, m, t, x, y, ans;   long long pw2[32];   long long a[MAXN];   void calPowerTwo() {     long long i, j, k;     pw2[0] = 1;     for (i = 1; i < 32; i++) {       pw2[i] = pw2[i - 1] * 2;     }   }   int main() {     calPowerTwo();     while (cin >> n) {       for (i = 0; i < n; i++) {         cin >> a[i];       }       ans = 0;       for (i = 0; i < n - 1; i++) {         ans += a[i];         k = 0;         while (i + pw2[k] < n) {           k++;         }         k--;         a[i + pw2[k]] += a[i];         a[i] = 0;         cout << ans << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int dadsadasda;   const int INF = 0x3f3f3f3f;   const long long LLINF = 1e18;   const int MAXN = 3e5;   int n;   int a[MAXN];   int ans[MAXN];   priority_queue<pair<int, int> > pq;   int father[MAXN], siz[MAXN], cnt[MAXN];   void makeSet(int nn, int m) {     for (int i = nn; i <= m; ++i) {       father[i] = -1;       siz[i] = 0;       cnt[i] = 1;     }   }   int find(int x) {     if (father[x] == -1) return x;     return father[x] = find(father[x]);   }   void unio(int x, int y) {     if (siz[x] > siz[y]) {       father[y] = x;       cnt[x] += cnt[y];     } else {       father[x] = y;       cnt[y] += cnt[x];       if (siz[x] == siz[y]) siz[y]++;     }   }   bool active[MAXN];   int main() {     dadsadasda = scanf( %d , &n);     for (int i = 0; i < n; i++) {       dadsadasda = scanf( %d , &a[i]);       pq.push({a[i], i});     }     makeSet(0, n - 1);     while (!pq.empty()) {       auto p = pq.top();       pq.pop();       active[p.second] = true;       if (p.second > 0 && find(p.second) != find(p.second - 1) &&           active[p.second - 1])         unio(find(p.second), find(p.second - 1));       if (p.second < n - 1 && find(p.second) != find(p.second + 1) &&           active[p.second + 1])         unio(find(p.second), find(p.second + 1));       int sz = cnt[find(p.second)];       ans[sz] = max(ans[sz], p.first);     }     for (int i = n - 1; i >= 1; i--) {       ans[i] = max(ans[i], ans[i + 1]);     }     for (int i = 1; i < n + 1; i++) {       printf( %d  , ans[i]);     }     printf(  n );     return 0;   }


#include <bits/stdc++.h>   double const MOD = 1000000007;   double const N = 1000005;   double const LN = 20;   double const inf = 1e18 + 5;   using namespace std;   double a, b;   void mirror(double &x, double &z) {     if (x < 0) {       x = -x;       return;     }     if (z < 0) {       z = -z;       return;     }     if (x > a) {       x = 2 * a - x;       return;     }     if (z > b) {       z = 2 * b - z;       return;     }   }   int main() {     double m;     double vx, vy, vz;     cin >> a >> b >> m;     cin >> vx >> vy >> vz;     double ok = abs(m / vy);     double x = a / 2 + ok * vx;     double z = 0 + vz * ok;     while (1) {       if (x >= 0 && x <= a && z >= 0 && z <= b) break;       mirror(x, z);     }     printf( %.10f %.10f n , x, z);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long ara[100010];   int segtree[20][100010 * 4];   int lazy[20][100010 * 4];   void build(int node, int l, int r, int po) {     if (l == r) {       segtree[po][node] = 0;       if ((ara[l] & (1 << po))) {         segtree[po][node] = 1;       }       return;     }     int mid = (l + r) >> 1;     int left = node << 1;     int right = left | 1;     build(left, l, mid, po);     build(right, mid + 1, r, po);     segtree[po][node] = segtree[po][left] + segtree[po][right];     return;   }   int query(int node, int l, int r, int ql, int qr, int po) {     if (lazy[po][node]) {       segtree[po][node] = (r - l + 1) - segtree[po][node];       if (l != r) {         lazy[po][(node << 1)] ^= 1;         lazy[po][1 + (node << 1)] ^= 1;       }       lazy[po][node] = 0;     }     if (l >= ql && qr >= r) {       return segtree[po][node];     }     if (l > qr || ql > r) return 0;     int mid = (l + r) >> 1;     int left = node << 1;     int right = left | 1;     int p = query(left, l, mid, ql, qr, po);     int q = query(right, mid + 1, r, ql, qr, po);     return p + q;   }   void update(int node, int l, int r, int ql, int qr, int po) {     if (lazy[po][node]) {       segtree[po][node] = (r - l + 1) - segtree[po][node];       if (l != r) {         lazy[po][node << 1] ^= 1;         lazy[po][1 + (node << 1)] ^= 1;       }       lazy[po][node] = 0;     }     if (l >= ql && qr >= r) {       segtree[po][node] = (r - l + 1) - segtree[po][node];       if (l != r) {         lazy[po][node << 1] ^= 1;         lazy[po][1 + (node << 1)] ^= 1;       }       lazy[po][node] = 0;       return;     }     if (l > qr || r < ql) return;     int mid = (l + r) >> 1;     int left = node << 1;     int right = left | 1;     update(left, l, mid, ql, qr, po);     update(right, mid + 1, r, ql, qr, po);     segtree[po][node] = segtree[po][left] + segtree[po][right];     return;   }   int main() {     long long n;     scanf( %lld , &n);     for (int i = 1; i <= n; i++) {       scanf( %lld , &ara[i]);     }     for (int i = 0; i < 20; i++) {       build(1, 1, n, i);     }     long long m;     scanf( %lld , &m);     while (m--) {       long long x;       scanf( %lld , &x);       if (x == 1) {         long long a, b;         scanf( %lld%lld , &a, &b);         long long sum = 0;         for (int i = 0; i < 20; i++) {           int y = query(1, 1, n, a, b, i);           sum += y * (1LL << i);         }         printf( %lld n , sum);       } else {         long long a, b, c;         scanf( %lld%lld , &a, &b);         scanf( %lld , &c);         for (int i = 0; i < 20; i++) {           if (c & (1 << i)) {             update(1, 1, n, a, b, i);           }         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t, a;     cin >> t;     while (t--) {       cin >> a;       cout << a / 2 << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   struct road {     int x, next;     double dis;   } r[305 * 305 * 2];   int N, M;   int st[305], w;   int vis[305];   double P[305];   double l1 = -1, r1 = 0, mid;   double dis[305];   void add(int x, int y, double dis) {     r[++w].x = y, r[w].next = st[x];     r[w].dis = dis;     st[x] = w;   }   bool work(int x) {     int i, j, tmp;     if (vis[x]) return true;     vis[x] = 1;     for (i = st[x]; i; i = r[i].next) {       tmp = r[i].x;       if (dis[tmp] > dis[x] + r[i].dis - mid) {         dis[tmp] = dis[x] + r[i].dis - mid;         if (work(tmp)) return true;       }     }     vis[x] = 0;     return false;   }   int main() {     int i, j;     int fr, to;     scanf( %d , &N);     for (i = 0; i <= N; i++) scanf( %lf , &P[i]);     M = N * 3;     for (i = 0; i <= M; i++)       for (j = N, fr = i; j >= -N; j -= 2) {         to = fr + j;         if (to > M || to < 0) continue;         add(fr, to, -P[(N - j) / 2]);       }     for (i = 1; i <= 21; i++) {       mid = (l1 + r1) / 2;       for (j = 1; j <= 20; j++) {         memset(dis, 63, sizeof(dis));         memset(vis, 0, sizeof(vis));         fr = rand() % M + 1, dis[fr] = 0;         if (work(fr)) break;       }       if (j <= 9)         r1 = mid;       else         l1 = mid;     }     printf( %.8lf n , -mid);     return 0;   }


#include <bits/stdc++.h>   char map[1005][1005];   int main() {     int n, m;     scanf( %d %d , &n, &m);     for (int r = 0; r < n; r++) scanf( %s , map[r]);     int row[1005] = {0};     int col[1005] = {0};     for (int r = 0; r < n; r++) {       int count = 0;       for (int c = 0; c < m; c++)         if (map[r][c] ==  * ) count++;       row[r] = count;     }     for (int c = 0; c < m; c++) {       int count = 0;       for (int r = 0; r < n; r++)         if (map[r][c] ==  * ) count++;       col[c] = count;     }     long long answer = 0;     for (int r = 0; r < n; r++)       for (int c = 0; c < m; c++)         if (map[r][c] ==  * ) answer += (row[r] - 1) * (col[c] - 1);     printf( %I64d n , answer);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 20;   const int mod = 1e9 + 7;   long double a[N];   int n, k;   long double dp[1 << N], out[N];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> n >> k;     int zeros = 0;     for (int i = 0; i < n; i++) cin >> a[i], zeros += fabs(a[i] < (1e-7));     k = min(k, n - zeros);     dp[0] = 1.0;     for (int i = 0; i < 1 << n; i++) {       double tmp = 1.0;       for (int j = 0; j < n; j++)         if (i >> j & 1) tmp -= a[j];       if (__builtin_popcount(i) == k)         for (int j = 0; j < n; j++)           if (i >> j & 1) out[j] += dp[i];       if (tmp == 0.0) continue;       cout << fixed << setprecision(6);       for (int j = 0; j < n; j++)         if (!(i >> j & 1)) dp[i | 1 << j] += a[j] * dp[i] / tmp;     }     for (int i = 0; i < n; i++) cout << out[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   const long long N = 1000000;   using namespace std;   long long n, k, l, i = 0, j, mn, mx = N, ans = 0, als, r1, r2, r3, t, q, v, m,                      ii, r4;   string d1, d3, d2, s;   char kl, km, mk, lk;   double fg;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     cin >> n;     bool a[N];     for (i = 0; i < n * 2; i++) {       cin >> t;       a[t - 1] = !a[t - 1];       m += a[t - 1] * 2 - 1;       ans = max(ans, m);     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main(void) {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n;     long long r1, r2, r3, d;     cin >> n >> r1 >> r2 >> r3 >> d;     vector<int> a(n);     for (int i = 0; i < n; i++) cin >> a[i];     vector<vector<long long>> dp(n, vector<long long>(2, LLONG_MAX));     dp[0][0] = r1 * a[0] + r3;     dp[0][1] = min(r1 * (a[0] + 2), r2 + r1);     for (int i = 1; i < n; i++) {       long long one = r1 * a[i] + r3;       long long two = min(r1 * (a[i] + 2), r2 + r1);       dp[i][0] = min(dp[i][0], dp[i - 1][0] + one);       dp[i][0] = min(dp[i][0], dp[i - 1][1] + min(one, two) + 2 * d);       if (i == n - 1) dp[i][0] = min(dp[i][0], dp[i - 1][1] + one + d);       dp[i][1] = min(dp[i][1], dp[i - 1][0] + two);     }     printf( %lld n , min(dp[n - 1][0], dp[n - 1][1] + 2 * d) + (n - 1) * d);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using namespace std;   const long long MOD = 1000000007;   vector<vector<long long> > matrix_mul(vector<vector<long long> > a,                                         vector<vector<long long> > b) {     vector<vector<long long> > c(a.size(), vector<long long>(b[0].size()));     for (int i = 0; i < a.size(); i++)       for (int j = 0; j < b[0].size(); j++)         for (int k = 0; k < b.size(); k++) {           c[i][j] += (a[i][k] * b[k][j]) % MOD;           c[i][j] %= MOD;         }     return c;   }   vector<vector<long long> > matrix_pow(vector<vector<long long> > a,                                         long long p) {     if (p == 1) return a;     if (p % 2 == 1) return matrix_mul(a, matrix_pow(a, p - 1));     vector<vector<long long> > c = matrix_pow(a, p / 2);     return matrix_mul(c, c);   }   int N, B, K, X;   long long occ[10];   int main() {     cin >> N >> B >> K >> X;     for (int i = 0; i < N; i++) {       int d;       cin >> d;       occ[d]++;     }     vector<long long> f(X);     f[0] = 1;     vector<vector<long long> > T(X);     for (int i = 0; i < X; i++) {       T[i] = vector<long long>(X);     }     for (int i = 0; i < X; i++) {       for (int d = 1; d <= 9; d++)         if (occ[d]) {           int a = (i * 10 + d) % X;           T[a][i] += occ[d];         }     }     T = matrix_pow(T, B);     long long res = 0;     res = (T[K][0] * f[0] % MOD) % MOD;     cout << res << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e3 + 5;   const int inf = 0x3f3f3f3f;   const long long mod = 1e9 + 7;   const double eps = 1e-6;   vector<int> g[maxn];   vector<int> tree[maxn];   int deep[maxn], son[maxn];   int cnt, mx;   void dfs(int x, int y) {     int sz = g[x].size();     deep[x] = 0;     son[x] = -1;     for (int i = 0; i < sz; ++i) {       int to = g[x][i];       if (to == y) {         continue;       }       tree[x].push_back(to);       dfs(to, x);       if (son[x] == -1 || deep[son[x]] < deep[to]) {         son[x] = son[to];       }     }     if (son[x] == -1) {       son[x] = x;     } else {       deep[x] = deep[son[x]] + 1;     }   }   struct node {     int id, w;     bool operator<(const node& rhs) const { return w > rhs.w; }   } nd[maxn];   int solve(int x) {     int sz = tree[x].size();     for (int i = 0; i < sz; ++i) {       nd[i] = {tree[x][i], deep[tree[x][i]]};     }     sort(nd, nd + sz);     int hf = sz / 2;     for (int i = 0; i < hf; ++i) {       int a = nd[i * 2].id, b = nd[i * 2 + 1].id;       ++cnt;       printf( ? %d %d n , son[a], son[b]);       fflush(stdout);       int c;       scanf( %d , &c);       if (c != x) {         return solve(c);       }       if (cnt >= mx) break;     }     if ((sz & 1) == 1 && cnt < mx) {       ++cnt;       printf( ? %d %d n , x, son[nd[sz - 1].id]);       fflush(stdout);       int c;       scanf( %d , &c);       if (c != x) {         return solve(c);       }     }     return x;   }   int main() {     int n;     scanf( %d , &n);     cnt = 0, mx = n / 2;     for (int i = 1; i < n; ++i) {       int u, v;       scanf( %d%d , &u, &v);       g[u].push_back(v);       g[v].push_back(u);     }     dfs(1, -1);     printf( ! %d n , solve(1));     fflush(stdout);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int N;   int main() {     cin >> N;     int dec[2 * N];     int all_round_down = 0;     int z = 0;     for (int i = 0; i < 2 * N; i++) {       double x;       cin >> x;       dec[i] = int((x + 0.0005 - int(x + 0.0005)) * 1000);       all_round_down += dec[i];       if (dec[i] == 0) z++;     }     int mi = max(0, N - z);     int ma = min(N, 2 * N - z);     int best = (all_round_down + 500) / 1000;     int ans;     if (best >= mi && best <= ma)       ans = abs(all_round_down - 1000 * best);     else       ans = min(abs(all_round_down - 1000 * mi), abs(all_round_down - 1000 * ma));     cout << ans / 1000 <<  .  << (ans % 1000) / 100 << (ans % 100) / 10          << ans % 10;   }


#include <bits/stdc++.h>   using namespace std;   int n;   vector<int> edge[(1 << 18)];   int degree[(1 << 18)];   int DelNode;   int DFS(int iDeep, int iNode, int iParent) {     int iSons = edge[iNode].size();     if (iParent) iSons--;     if (iDeep > n) return -1;     if (iSons > 3) return -1;     if (iSons == 0) return 1;     if (iSons & 1) {       if (DelNode) return -1;       DelNode = iNode;       vector<int> RetList;       for (auto Iter : edge[iNode]) {         if (Iter == iParent) continue;         int iRet = DFS(iDeep + 1, Iter, iNode);         if (-1 == iRet) return iRet;         RetList.push_back(iRet);       }       sort(RetList.begin(), RetList.end());       if (iSons == 1) {         if (RetList[0] != 1) return -1;         return RetList[0] + 1;       } else {         if (RetList[0] != RetList[1] || RetList[0] + 1 != RetList[2]) return -1;         return RetList[2] + 1;       }     } else {       assert(iSons == 2);       int iDeepToButtom = -1;       for (auto Iter : edge[iNode]) {         if (Iter == iParent) continue;         int iRet = DFS(iDeep + 1, Iter, iNode);         if (-1 == iRet) return iRet;         if (iDeepToButtom == -1)           iDeepToButtom = iRet;         else if (iDeepToButtom != iRet)           return -1;       }       return iDeepToButtom + 1;     }   }   int main() {     scanf( %d , &n);     int k = (1 << n) - 2;     for (int i = 0; i < k - 1; ++i) {       int u, v;       scanf( %d %d , &u, &v);       edge[u].push_back(v);       edge[v].push_back(u);       degree[u]++;       degree[v]++;     }     vector<int> list;     for (int i = 1; i <= k; ++i)       if (degree[i] == 1) {         list.push_back(i);       }     for (int i = 0; i < list.size(); ++i) {       int iCur = list[i];       for (auto Iter : edge[iCur]) {         if (--degree[Iter] == 1) list.push_back(Iter);       }     }     assert(list.size() == k);     int i = 0;     vector<int> result;     for (auto Iter = list.rbegin(); i < 5 && Iter != list.rend(); Iter++, i++) {       DelNode = 0;       if (DFS(1, *Iter, 0) != -1) {         assert(DelNode);         result.push_back(DelNode);       }     }     sort(result.begin(), result.end());     result.resize(unique(result.begin(), result.end()) - result.begin());     printf( %d n , result.size());     for (int i = 0; i < result.size(); ++i) {       if (i) putchar(   );       printf( %d , result[i]);     }     putchar(  n );   }


#include <bits/stdc++.h>   using namespace std;   long long n, sz, i, ans, j, a[200001], t[531172], op;   void upd(int x) {     for (j = x / 2; j >= 1; j /= 2) t[j] = max(t[j * 2], t[j * 2 + 1]);   }   long long finds(int l, int r) {     long long sum = -1;     while (l <= r) {       if (l % 2 == 1) sum = max(sum, t[l]);       if (r % 2 == 0) sum = max(sum, t[r]);       l++, r--, l /= 2, r /= 2;     }     return sum;   }   int main() {     cin >> n;     sz = 1;     while (sz < n) sz *= 2;     for (i = 1; i <= n; i++) cin >> a[i];     for (i = 1; i <= n; i++) {       t[a[i] + sz - 1] = finds(sz, a[i] + sz - 1) + 1;       upd(a[i] + sz - 1);     }     cout << max(t[1], 1ll);   }


#include <bits/stdc++.h>   using namespace std;   int read() {     register 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 = 10 * x + ch -  0 ;       ch = getchar();     }     return x * f;   }   const int Size = 100005;   int n, top;   struct vec {     double x, y;   } w[Size], a[Size], O, stk[Size];   inline vec operator-(vec a, vec b) { return (vec){a.x - b.x, a.y - b.y}; }   inline vec operator+(vec a, vec b) { return (vec){a.x + b.x, a.y + b.y}; }   inline bool operator<(vec a, vec b) {     if (a.x != b.x) return a.x < b.x;     return a.y < b.y;   }   inline double Xji(vec p, vec q) { return p.x * q.y - p.y * q.x; }   inline double dist(vec mhy) { return mhy.x * mhy.x + mhy.y * mhy.y; }   inline bool comp(vec jzm, vec xjp) {     double tmp = Xji(jzm, xjp);     if (fabs(tmp) > 1e-6) return tmp > 1e-6;     return dist(jzm) < dist(xjp);   }   inline double dist(vec mhy, vec zwt) {     return sqrt((mhy.x - zwt.x) * (mhy.x - zwt.x) +                 (mhy.y - zwt.y) * (mhy.y - zwt.y));   }   inline vec rotate(vec a, double rad) {     return (vec){a.x * cos(rad) - a.y * sin(rad),                  a.x * sin(rad) + a.y * cos(rad)};   }   int main() {     n = read();     for (register int i = 1; i <= n; i++) {       double x, y;       scanf( %lf %lf , &x, &y);       w[i].x = x;       w[i].y = y - x * x;     }     vec O = w[1];     int id = 1;     for (register int i = 2; i <= n; i++) {       if (w[i] < O) {         id = i;         O = w[i];       }     }     swap(w[id], w[1]);     for (register int i = 1; i <= n; i++) {       a[i] = w[i] - O;     }     sort(a + 2, a + 1 + n, comp);     for (register int i = 1; i <= n; i++) {       while (top > 1 && Xji(stk[top] - stk[top - 1], a[i] - stk[top - 1]) <= 0) {         top--;       }       stk[++top] = a[i];     }     for (register int i = 1; i <= top; i++) {       stk[i] = stk[i] + O;     }     int ans = 0;     for (register int i = 1; i <= top; i++) {       int pos = i % top + 1;       if (stk[i].x > stk[pos].x) {         ans++;       }     }     printf( %d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long c[55];   struct cmp {     bool operator()(long long a, long long b) { return c[a] < c[b]; }   };   int main() {     long long n, m, k;     long long a[55], b[55], ind[55];     long long dp[55][55][105][3];     cin >> n >> m >> k;     for (long long i = 0; i < m; ++i) {       cin >> a[i] >> b[i] >> c[i];       ind[i] = i;     }     sort(ind, ind + m, cmp());     if (0)       for (long long i = 0; i < m; i++) cout << ind[i] <<    ;     if (0) cout << endl;     memset(dp, -1, sizeof(dp));     for (long long i = 0; i < m; i++) {       for (long long p = 0; p <= (long long)(b[ind[i]] - a[ind[i]]); p++) {         dp[i][1][p][0] = a[ind[i]] + p;         dp[i][1][p][1] = i;         dp[i][1][p][2] = p;       }     }     long long maxVal, maxInd, maxH;     for (long long i = 1; i < m; i++) {       for (long long j = 2; j <= min(n, i + 1); j++) {         for (long long p = 0; p <= (long long)(b[ind[i]] - a[ind[i]]); p++) {           maxVal = -1;           maxInd = -1;           maxH = -1;           long long cur = a[ind[i]] + p, req1 = cur - k,                     req2 = ((cur % k) ? -1 : (cur / k));           for (long long s = i - 1; s >= 0; s--) {             if (c[ind[s]] == c[ind[i]]) continue;             if (0)               cout <<  hello   << s <<     << req1 <<     << req2 <<                        << a[ind[s]] <<     << b[ind[s]] <<                        << dp[s][j - 1][(long long)(req1 - a[ind[s]])][0] << endl;             if (0) cout <<  MAXVAL   << maxVal << endl;             if (req1 > 0 && req1 <= b[ind[s]] && req1 >= a[ind[s]]) {               if (dp[s][j - 1][(long long)(req1 - a[ind[s]])][0] > maxVal) {                 maxInd = s;                 maxVal = dp[s][j - 1][(long long)(req1 - a[ind[s]])][0];                 maxH = (req1 - a[ind[s]]);               }             }             if (req2 > 0 && req2 <= b[ind[s]] && req2 >= a[ind[s]]) {               if (dp[s][j - 1][(long long)(req2 - a[ind[s]])][0] > maxVal) {                 maxInd = s;                 maxVal = dp[s][j - 1][(long long)(req2 - a[ind[s]])][0];                 maxH = (req2 - a[ind[s]]);               }             }           }           if (0) cout <<  FUCK   << maxVal <<     << maxH << endl;           dp[i][j][p][0] = (maxVal != -1) ? (maxVal + cur) : maxVal;           dp[i][j][p][1] = maxInd;           dp[i][j][p][2] = maxH;           if (0)             cout << i <<     << j <<     << a[ind[i]] + p <<     << dp[i][j][p][0]                  << endl;         }       }     }     maxVal = -1;     maxInd = -1;     maxH = -1;     for (long long i = 0; i < m; i++) {       for (long long j = 0; j <= (b[ind[i]] - a[ind[i]]); j++)         if (maxVal < dp[i][n][j][0]) {           maxVal = dp[i][n][j][0];           maxInd = i;           maxH = j;         }     }     if (maxVal != -1) {       long long j = n;       vector<pair<long long, long long> > v;       vector<pair<long long, long long> >::iterator it;       cout <<  YES  << endl;       while (true) {         if (0) cout <<  mind mh   << maxInd <<     << maxH << endl;         v.push_back(make_pair(ind[maxInd] + 1, a[ind[maxInd]] + maxH));         long long temp = maxInd;         maxInd = dp[temp][j][maxH][1];         maxH = dp[temp][j][maxH][2];         j--;         if (j == 0) break;       }       for (it = --v.end(), j = n; j > 0; it--, j--) {         cout << (*it).first <<     << (*it).second << endl;       }     } else {       cout <<  NO  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[1010];   long long b[1010];   long long c[2020];   long long d[1010];   long long mex(long long m) {     sort(b, b + m);     long long j = 0;     long long k = 0;     long long ans = m;     while (j < m) {       if (b[k] > j) {         ans = j;         break;       } else if (b[k] == j) {         k++;         j++;       } else {         k++;       }     }     return ans;   }   long long check(long long m) {     long long ct3 = 0;     for (long long i = 1; i < m; i++) {       if (a[i] < a[i - 1]) {         ct3 = 1;         break;       }     }     if (ct3 == 1)       return 0;     else       return 1;   }   int main() {     long long t;     cin >> t;     while (t--) {       long long n;       cin >> n;       long long ct = 1;       long long ct1 = 0;       for (long long i = 0; i <= n; i++) {         a[i] = 0;         b[i] = 0;         c[i] = 0;         d[i] = 0;       }       for (long long i = n; i <= (n + n); i++) {         c[i] = 0;       }       for (long long i = 0; i < n; i++) {         cin >> a[i];         b[i] = a[i];         if (a[i] == i) d[i] = 1;       }       while (ct <= (2 * n)) {         if (check(n) == 1) break;         ct1 = mex(n);         if (ct1 == n) {           for (long long i = 0; i < n; i++) {             if (d[i] != 1) {               a[i] = n;               c[ct] = i;               break;             }           }         } else {           c[ct] = ct1;           a[ct1] = ct1;           d[ct1] = 1;         }         for (long long i = 0; i < n; i++) b[i] = a[i];         ct++;       }       cout << ct - 1 <<   n ;       for (long long i = 1; i < ct; i++) {         cout << c[i] + 1 <<    ;       }       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class A>   ostream& operator<<(ostream& out, const vector<A>& v) {     for (long long i = 0; i < v.size(); i++) {       if (i) out <<    ;       out << v[i];     }     return out <<   n ;   }   const long long mod = 1000000007;   inline long long add(long long a, long long b) {     a += b;     if (a >= mod) a -= mod;     return a;   }   inline long long sub(long long a, long long b) {     a -= b;     if (a < 0) a += mod;     return a;   }   inline long long mul(long long a, long long b) { return (a * 1ll * b) % mod; }   inline long long powmod(long long a, long long b) {     long long rt = 1;     while (b > 0) {       if (b & 1) rt = mul(rt, a);       a = mul(a, a);       b >>= 1;     }     return rt;   }   inline long long inv(long long a) { return powmod(a, mod - 2); }   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   long long n, k;   long long a[100005];   bool check(long long mid) {     long long sum = 0;     for (long long i = 0; i < n; i++) {       sum += min(mid, a[i]);     }     return sum <= k;   }   void solve() {     cin >> n >> k;     long long rt = 0;     for (long long i = 0; i < n; i++) {       cin >> a[i];       rt += a[i];     }     if (rt < k) {       cout << -1 <<   n ;       return;     }     long long lo = 1;     long long hi = *max_element(a, a + n);     long long ans = 0;     while (hi - lo >= 0) {       long long mid = (hi + lo) >> 1;       if (check(mid)) {         lo = mid + 1;         ans = mid;       } else {         hi = mid - 1;       }     }     if (false) cerr << ans <<   n ;     for (long long i = 0; i < n; i++) {       k -= min(a[i], ans);       a[i] -= min(a[i], ans);     }     if (false) cerr << k <<   n ;     long long i = 0;     for (i = 0; i < n && k > 0; i++) {       if (a[i] <= 0)         continue;       else {         a[i]--;         k--;       }       if (false) cerr << a[i] <<    ;     }     if (false) cerr <<   n ;     long long y = 0;     for (long long j = i; j < i + n; j++) {       long long tmp = j % n;       if (false) cerr << a[tmp] <<    ;       if (a[tmp] <= 0) continue;       y = 1;       cout << tmp + 1 <<    ;     }     cout <<   n ;   }   int32_t main() {     ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL),         cout << setprecision(14);     ;     long long t = 1;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   string in_name =  sample.in ;   string out_name =  sample.out ;   ifstream in;   ofstream out;   vector<int> v[1001];   int N;   int main() {     cin >> N;     int k, x;     for (int i = 0; i < N; i++) {       cin >> k;       for (int j = 0; j < k; j++) {         cin >> x;         v[i].push_back(x);       }       sort(v[i].begin(), v[i].end());     }     int res = 0;     for (int i = 0; i < N; i++) {       for (int j = 0; j < v[i].size() - 1; j++) {         std::vector<int>::iterator low, up;         int before = (i - 1 + N) % N;         int after = (i + 1) % N;         int left = 0, right = 0;         up = upper_bound(v[before].begin(), v[before].end(), v[i][j]);         low = lower_bound(v[before].begin(), v[before].end(), v[i][j + 1]);         int u = (int)(up - v[before].begin());         int l = (int)(low - v[before].begin());         if (u < v[before].size() && v[before][u] < v[i][j + 1])           left += (l - u) + 1;         up = upper_bound(v[after].begin(), v[after].end(), v[i][j]);         low = lower_bound(v[after].begin(), v[after].end(), v[i][j + 1]);         u = (int)(up - v[after].begin());         l = (int)(low - v[after].begin());         if (u < v[after].size() && v[after][u] < v[i][j + 1])           right += (l - u) + 1;         if (left != right) {           res++;         }       }     }     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int firstt(int* a, int n) {     long long l, r, s;     for (int i = 0; i <= n - 1; ++i) {       l = 0;       r = 10000000000;       while (l < r - 1) {         s = (-l + r) / 2 + l;         if (s * n >= a[i] - i) {           r = s;         } else {           l = s;         }       }       if (l * n >= a[i] - i) {         a[i] = l;       } else {         a[i] = l + 1;       }     }     l = 0;     int minn = a[0];     for (int i = 1; i <= n - 1; ++i) {       if (a[i] < minn) {         minn = a[i];         l = i;       }     }     return (l + 1);   }   int main() {     int n, i;     cin >> n;     int* a = new int[n];     for (i = 0; i <= n - 1; ++i) {       cin >> a[i];     }     long long res = firstt(a, n);     cout << res;   }


#include <bits/stdc++.h>   using namespace std;   vector<vector<int> > V(7);   vector<vector<string> > S(7);   set<string> SS;   int n, m;   void DFS(int node, string T) {     if (T.length() == n) {       SS.insert(T);       return;     }     for (int i = 0; i < S[node].size(); i++) {       string R = S[node][i];       string E;       E += R[1];       if (T.length() + 2 == n) {         DFS(0, T + R);       } else         DFS(R[0] -  a , T + E);     }   }   int main() {     cin >> n >> m;     string y;     char C;     char A[m];     for (int i = 0; i < m; i++) {       cin >> y >> C;       A[i] = C;       S[C -  a ].push_back(y);     }     for (int i = 0; i < m; i++) {       if (A[i] ==  a ) {         DFS(A[i] -  a ,   );       }     }     cout << SS.size() << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MAXN = 5e5 + 5;   struct Node {     long long t, h, m;   } a[MAXN];   bool cmp(Node x, Node y) { return x.m > y.m; }   long long n, x;   long long vis[MAXN];   long long solve(long long now) {     memset(vis, 0, sizeof(vis));     long long ans = 0;     long long h = x;     while (1) {       long long f = 0;       for (long long i = 1; i <= n; i++) {         if (vis[i] == 0 && now != a[i].t && h >= a[i].h) {           f = 1;           now = 1 - now;           vis[i] = 1;           ans++;           h += a[i].m;           break;         }       }       if (f == 0) {         break;       }     }     return ans;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     cin >> n >> x;     for (long long i = 1; i <= n; i++) {       cin >> a[i].t >> a[i].h >> a[i].m;     }     sort(a + 1, a + n + 1, cmp);     long long ans = max(solve(1), solve(0));     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   #define ll long long int   #define pb push_back   #define mp make_pair   #define deb(x) cout<< #x <<     << x <<   n ;   #define MAX 9223372036854775807   #define MIN -9223372036854775807   #define PI 3.141592653589   #define setbits(n) __builtin_popcountll(n)   #define mkunique(a) a.resize(unique(a.begin(),a.end())-a.begin());   const ll mod=998244353;            int main() {              ios_base::sync_with_stdio(false);       cin.tie(NULL);       cout.tie(NULL);       ll T=clock();          ll t;       cin>>t;       while(t--){           ll n,m;           cin>>n>>m;           vector<vector<ll>> a(n,vector<ll>(m));           vector<ll> p(n+m);           for(ll i=0;i<n;i++){               for(ll j=0;j<m;j++){                   cin>>a[i][j];                   p[i+j]^=a[i][j];               }           }           bool ok=false;           for(ll i=0;i<n+m;i++)               if(p[i]!=0)                   ok=true;           if(ok)               cout<< Ashish n ;           else               cout<< Jeel n ;       }                cerr<<  n nTIME:  <<(double)(clock()-T)/CLOCKS_PER_SEC<<  sec n ;       T = clock();       return 0;   }


#include <bits/stdc++.h>   using namespace std;   int q;   int n;   int nav[200005], pos[200005];   int main() {     scanf( %d , &q);     while (q--) {       int men = 999999, lar = 999999;       scanf( %d , &n);       int a;       for (int i = 0; i < n; i++) {         scanf( %d , &a);         if (nav[a] == 0) nav[a] = 1, pos[a] = i;         if (nav[a] == 1) lar = i - pos[a], pos[a] = i;         if (lar < men && lar != 0) men = lar;       }       printf( %d n , men == 999999 ? -1 : men + 1);       for (int i = 0; i < 200001; i++) nav[i] = pos[i] = 0;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e3;   int main() {     int n;     cin >> n;     for (int i = 1; i < N; i++)       if (i * (i + 1) <= 2 * n && (i + 2) * (i + 1) > 2 * n) {         cout << i << endl;         for (int j = 1; j < i; j++) cout << j <<    ;         cout << n - (i - 1) * i / 2;         return 0;       }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, tf = 0, f = 0, hun = 0;     cin >> n;     int a[n];     for (int i = 0; i < n; i++) {       cin >> a[i];       if (a[i] == 25)         tf++;       else if (a[i] == 50) {         f++;         tf--;       } else {         hun++;         if (f >= 1) {           f--;           tf--;         } else           tf = tf - 3;       }       if (tf < 0) {         cout <<  NO ;         break;       }     }     if (tf >= 0) cout <<  YES ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, y;   int main() {     cin >> n;     n--;     for (int i = 1; i <= sqrt(n); i++)       if (n % i == 0) {         y = n / i - 1 - i;         if ((y % 2 == 0) && (y > 0)) {           cout << i <<     << y / 2;           return 0;         }       }     cout <<  NO ;     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }   int power(int a, int b, int p) {     int ans = 1 % p;     for (; b; b >>= 1) {       if (b & 1) ans = (long long)ans * a % p;       a = (long long)a * a % p;     }     return ans;   }   int a, b, c, d;   int main() {     scanf( %d%d%d%d , &a, &b, &c, &d);     if (a == d && (a != 0 || c == 0))       printf( 1 n );     else       printf( 0 n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXINT = 1000001;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       vector<int> a(n), b(n);       vector<bool> used(MAXINT);       set<int> dif;       for (int i = 0; i < n; i++) {         cin >> a[i];       }       sort(a.begin(), a.end());       for (int i = 0; i < n; i++) {         for (int j = 0; j < i; j++) {           dif.insert(a[i] - a[j]);         }       }       int p = 1;       bool can = true;       for (int i = 0; i < n; i++) {         while (p < MAXINT && used[p]) {           ++p;         }         if (p == MAXINT) {           can = false;           break;         }         b[i] = p;         used[p] = true;         for (int d : dif) {           if (b[i] + d < MAXINT) {             used[b[i] + d] = true;           }         }       }       if (can) {         cout <<  YES n ;         for (int i : b) {           cout << i <<    ;         }         cout <<   n ;       } else {         cout <<  NO n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int dp[100001];   int tmp, n, b[100001];   vector<pair<int, int> > a;   int main() {     int tt, ss;     cin >> n;     for (int i = (0); i <= (n - 1); i++) {       cin >> tt >> ss;       a.push_back(make_pair(tt, ss));     }     sort(a.begin(), a.end());     tmp = 0;     int mn = INT_MIN;     for (int i = (0); i <= (n - 1); i++) {       int lb = lower_bound(a.begin(), a.end(),                            make_pair(a[i].first - a[i].second, 0)) -                a.begin();       dp[i] = dp[lb - 1] + 1;       mn = max(mn, dp[i]);     }     cout << n - mn << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long b, deocoai, g, n, p, q, i, a[300], t, tong, z, a1, a2, k2, k1;     cin >> b >> g >> n;     if (min(b, g) >= n) {       cout << n + 1;       return 0;     }     if (max(b, g) >= n && min(b, g) <= n) {       cout << min(b, g) + 1;       return 0;     }     cout << b + g - n + 1;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     scanf( %d , &n);     long long ans = 0;     vector<pair<int, int> > v;     vector<int> all;     for (int i = 1; i <= n; ++i) {       int a, b, c, d;       scanf( %d%d%d%d , &a, &b, &c, &d);       if (a + b < c + d) {         if (a > d) v.push_back(make_pair(a - d, d - a));         if (b > c) v.push_back(make_pair(c - b, b - c));       } else {         v.push_back(make_pair(a, b));         v.push_back(make_pair(c, d));       }     }     for (auto p : v) {       int a = p.first, b = p.second;       all.push_back(a + b);       ans += a - b;     }     sort(all.begin(), all.end(), greater<int>());     for (int i = 0; i < (int)all.size(); ++i) {       if (i & 1)         ans -= all[i];       else         ans += all[i];     }     printf( %lld n , ans / 2);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long max_sz = 1e5 + 1;   const long long mod = 1e9 + 7;   const long long MAXN = 4e18 + 1;   char flip(char k) {     if (k ==  0 ) return  1 ;     return  0 ;   }   long long sum_digit(long long n) {     long long a = 0, i;     for (i = n; i > 0; i = i / 10) {       a += (i % 10);     }     return a;   }   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   int main() {     long long t;     t = 1;     while (t--) {       long long n;       cin >> n;       vector<long long> a(n);       for (int i = 0; i < n; ++i) cin >> a[i];       sort(a.begin(), a.end());       long long f = 0;       for (long long i = 0; i < n - 2; i++) {         if (a[i] == a[i + 1] && a[i] == a[i + 2]) {           f = 1;           break;         }       }       if (f == 1)         cout <<  NO               <<   n ;       else {         cout <<  YES               <<   n ;         vector<long long> inc, dec;         for (long long i = 0; i < n;) {           if (a[i] == a[i + 1]) {             inc.push_back(a[i]);             dec.push_back(a[i + 1]);             i += 2;           } else {             inc.push_back(a[i]);             i++;           }         }         cout << inc.size() << endl;         for (auto it : inc) cout << it <<    ;         cout << endl;         cout << dec.size() << endl;         sort(dec.begin(), dec.end(), greater<long long>());         for (auto it : dec) cout << it <<    ;         cout << endl;       }     }     cerr << endl          <<  Time :   << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC          <<  ms n ;     return 0;   }


#include <bits/stdc++.h>   const int INF = 0x3f3f3f3f;   const double PI = acos(-1.0);   const double EPS = 1e-8;   using namespace std;   int main() {     int n;     while (cin >> n) {       if (n <= 2)         puts( -1 );       else {         if (n % 2)           cout << 2 * ((long long)(n / 2) * (n / 2) + n / 2) <<                    << 2 * ((long long)(n / 2) * (n / 2) + n / 2) + 1 << endl;         else           cout << (long long)(n / 2) * (n / 2) - 1 <<                    << (long long)(n / 2) * (n / 2) + 1 << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   double across(int n, int num_sides) {     double ang = M_PI * ((double)num_sides / n) / 2;     return 0.5 / sin(M_PI / (2 * n)) * sin(ang) * 2;   }   int main() {     cin.tie(0);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       double arg = (M_PI * (n - 1)) / ((double)2 * n);       if (n % 2 == 0) {         cout << setprecision(50) << tan(arg) <<   n ;       } else {         double r = 0.5 / sin(M_PI / (2 * n));         double side = r * 2 * sin((M_PI / n * (n - 1)) / 2);         cout << setprecision(50)              << sqrt(pow(across(n, n / 2), 2) / (double)2) +                     sqrt(pow(across(n, n / 2 + 1), 2) / (double)2)              <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t, a, i, j, ans, k, n, m;     scanf( %d , &t);     while (t--) {       ans = 0;       set<int> a;       a.clear();       scanf( %d , &n);       for (i = 1; i <= n; i++) {         scanf( %d , &k);         if (k % 2 == 0) a.insert(k);       }       while (a.size() != 0) {         m = *a.rbegin();         ans++;         a.erase(m);         m /= 2;         if (m % 2 == 0) a.insert(m);       }       printf( %d n , ans);     }     return 0;   }