#include <bits/stdc++.h>   const int inf = 2000000000;   using namespace std;   vector<vector<int> > g;   vector<int> color;   vector<bool> on_cycle;   vector<int> parent;   int n;   bool isexit = false;   void dfs(int v, int p = -1) {     if (isexit) return;     color[v] = 1;     parent[v] = p;     for (int i = 0; i < (int)g[v].size(); i++) {       if (isexit) break;       if (color[g[v][i]] == 0)         dfs(g[v][i], v);       else if (color[g[v][i]] == 1 && g[v][i] != parent[v]) {         int j = v;         while (j != g[v][i]) {           on_cycle[j] = true;           j = parent[j];         }         on_cycle[g[v][i]] = true;         isexit = true;       }     }     color[v] = 3;   }   void dfs2(int v) {     for (int i = 0; i < (int)g[v].size(); i++)       if (color[g[v][i]] > color[v] + 1) {         color[g[v][i]] = color[v] + 1;         dfs2(g[v][i]);       }   }   int main() {     scanf( %d , &n);     g.resize(n + 1);     color.resize(n + 1);     on_cycle.resize(n + 1, false);     parent.resize(n + 1);     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);     }     for (int i = 1; i <= n; i++)       if (color[i] == 0) dfs(i);     for (int i = 1; i <= n; i++)       if (on_cycle[i])         color[i] = 0;       else         color[i] = inf;     for (int i = 1; i <= n; i++)       if (on_cycle[i]) dfs2(i);     for (int i = 1; i <= n; i++) printf( %d  , color[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3010;   int n;   int x[N], y[N], top;   pair<int, pair<int, int> > st[N * N];   bitset<N> adj[N];   int sqr(int u) { return u * u; }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d %d , &x[i], &y[i]);     }     for (int i = 1; i <= n; i++) {       for (int j = i + 1; j <= n; j++) {         int dis = sqr(abs(x[i] - x[j])) + sqr(abs(y[i] - y[j]));         st[top++] = make_pair(dis, make_pair(i, j));       }     }     sort(st, st + top);     for (int i = top - 1; i >= 0; i--) {       int dis = st[i].first;       int a = st[i].second.first;       int b = st[i].second.second;       if ((adj[a] & adj[b]).any()) {         printf( %0.17f , sqrt(dis) / 2);         return 0;       }       adj[a][b] = 1;       adj[b][a] = 1;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int Set(int n, int pos) { return n = n | (1LL << pos); }   int reset(int n, int pos) { return n = n & ~(1LL << pos); }   bool check(int n, int pos) { return (bool)(n & (1LL << pos)); }   int mul(int a, int b) {     int c;     c = (a % 1000000007 * b % 1000000007) % 1000000007;     return c;   }   int add(int a, int b) {     int c;     c = (a % 1000000007 + b % 1000000007) % 1000000007;     return c;   }   int sub(int a, int b) {     int c;     c = ((a % 1000000007 - b % 1000000007) % 1000000007 + 1000000007) %         1000000007;     return c;   }   int par[300005];   int dis[300005];   int dia[300005];   vector<int> graph[300005];   int n, m, q;   void cle_par() {     for (int i = 1; i <= n; i++) {       par[i] = i;       dis[i] = -1;     }   }   int find_pr(int r) {     if (par[r] == r) {       return r;     }     return par[r] = find_pr(par[r]);   }   void in_edges() {     int u, v;     for (int i = 1; i <= m; i++) {       scanf( %d , &u);       scanf( %d , &v);       graph[u].push_back(v);       graph[v].push_back(u);       u = find_pr(u);       v = find_pr(v);       if (u != v) {         par[u] = v;       }     }   }   void bfs(int main_src) {     deque<int> q;     vector<int> nod;     dis[main_src] = 0;     q.push_back(main_src);     nod.push_back(main_src);     while (!q.empty()) {       int src = q.front();       q.pop_front();       for (int i = 0; i < graph[src].size(); i++) {         int adj = graph[src][i];         if (dis[adj] == -1) {           dis[adj] = dis[src] + 1;           q.push_back(adj);           nod.push_back(adj);         }       }     }     int maxi_dis = -1;     int maxi_nod;     for (int i = 0; i < nod.size(); i++) {       if (dis[nod[i]] > maxi_dis) {         maxi_dis = dis[nod[i]];         maxi_nod = nod[i];       }       dis[nod[i]] = -1;     }     dis[maxi_nod] = 0;     q.push_back(maxi_nod);     nod.push_back(maxi_nod);     while (!q.empty()) {       int src = q.front();       q.pop_front();       for (int i = 0; i < graph[src].size(); i++) {         int adj = graph[src][i];         if (dis[adj] == -1) {           dis[adj] = dis[src] + 1;           q.push_back(adj);         }       }     }     maxi_dis = -1;     for (int i = 0; i < nod.size(); i++) {       if (dis[nod[i]] > maxi_dis) {         maxi_dis = dis[nod[i]];       }     }     dia[main_src] = maxi_dis;   }   void making_dia() {     for (int i = 1; i <= n; i++) {       if (par[i] == i) {         bfs(i);       }     }   }   int main() {     scanf( %d , &n);     scanf( %d , &m);     scanf( %d , &q);     cle_par();     in_edges();     making_dia();     for (int i = 1; i <= q; i++) {       int type;       scanf( %d , &type);       if (type == 1) {         int x;         scanf( %d , &x);         int u = find_pr(x);         printf( %d n , dia[u]);       } else if (type == 2) {         int x, y;         scanf( %d , &x);         scanf( %d , &y);         int u = find_pr(x);         int v = find_pr(y);         if (u == v) {           continue;         } else {           double first = (double)dia[u];           first /= 2.00;           first -= 0.0000000001;           first = ceil(first);           double second = (double)dia[v];           second /= 2.00;           second -= 0.0000000001;           second = ceil(second);           int uuu = (int)first;           int vvv = (int)second;           int tot_dia = uuu + vvv + 1;           dia[v] = max(dia[u], max(dia[v], tot_dia));           par[u] = v;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void maximize(int &a, int b) { a = max(a, b); }   int absolut(int x) {     if (x < 0) return -x;     return x;   }   const int tope = 1 << 19;   const int first = 1 << 18;   struct elem {     int ls, lH, lh, lb;     int rs, rH, rh, rb;     int sol;   };   elem v[tope];   void recompute(int pos) {     elem &e = v[pos];     elem &e0 = v[2 * pos];     elem &e1 = v[2 * pos + 1];     e.ls = e0.ls + e1.ls;     e.lH = max(e0.lH, e0.ls + e1.lH);     e.lh = min(e0.lh, e0.ls + e1.lh);     e.lb = max(e0.lb, -e0.ls + e1.lb);     if (e0.lh < e0.ls + e1.lH) maximize(e.lb, -2 * e0.lh + e0.ls + e1.lH);     e.rs = e0.rs + e1.rs;     e.rH = max(e1.rH, e0.rH + e1.rs);     e.rh = min(e1.rh, e0.rh + e1.rs);     e.rb = max(e1.rb, e0.rb - e1.rs);     if (e1.rh < e0.rH + e1.rs) maximize(e.rb, -2 * e1.rh + e1.rs + e0.rH);     e.sol = max(max(e0.sol, e1.sol), max(e0.rb + e1.lH, e0.rH + e1.lb));   }   void insert(int pos, int val) {     pos += first;     elem &e = v[pos];     e.ls = val;     e.lH = max(0, val);     e.lh = min(0, val);     e.lb = absolut(val);     val *= -1;     e.rs = val;     e.rH = max(0, val);     e.rh = min(0, val);     e.rb = absolut(val);     e.sol = absolut(val);     while (pos > 1) {       pos /= 2;       recompute(pos);     }   }   int n, cases;   string s;   void insert(int pos) { insert(pos, s[pos] ==  (  ? 1 : -1); }   int main() {     cin >> n >> cases >> s;     s =     + s;     for (int i = 1; i < int(s.size()); i++) insert(i);     cout << v[1].sol << endl;     for (int cas = 0; cas < cases; cas++) {       int pos0, pos1;       cin >> pos0 >> pos1;       swap(s[pos0], s[pos1]);       insert(pos0);       insert(pos1);       cout << v[1].sol << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int n, k, plen;   char p[200], ar[200];   char res[200];   int preone[200];   bool pos, found;   bool is_start(int ind) {     if (plen + ind > n) return false;     for (int l = 0; l < plen; l++)       if (p[l] != res[l + ind]) return false;     return true;   }   bool back(int ind) {     if (ind < 0) {       return true;     }     if (res[ind] != -1) {       if (ar[ind] ==  0  && is_start(ind)) return false;       return back(ind - 1);     }     for (int i = 0; i < k; i++) {       res[ind] =  a  + i;       if (!is_start(ind))         if (back(ind - 1)) return true;     }     return false;   }   int main() {     cin >> n >> k;     cin >> p;     cin >> ar;     plen = strlen(p);     for (int i = n - plen + 1; i < n; i++) {       ar[i] =  0 ;     }     memset(res, -1, sizeof(res));     pos = true;     for (int i = 0; i < n && pos; i++) {       if (ar[i] ==  1 ) {         for (int j = 0; j < plen && pos; j++) {           if (res[i + j] != -1 && res[i + j] != p[j])             pos = false;           else             res[i + j] = p[j];         }       }     }     if (pos && back(n)) {       for (int i = 0; i < n; i++) cout << res[i];     } else       cout <<  No solution ;     cout << endl;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:10000000000 )   using namespace std;   const int MOD = 1000000007;   const int INF = 1000000007LL;   const long long INF2 = 1000000007LL * 1000000007LL;   const long double EPS = 1e-9;   const int SIZE = 100100;   mt19937 rng(time(0));   uniform_int_distribution<int> uid(-1000000000, 1000000000);   int n;   int a[SIZE];   int d[210][SIZE][2];   const long long md = 998244353;   const int val = 200;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     if (n == 1) {       cout << 0;       return 0;     }     for (int i = 1; i <= n; ++i) cin >> a[i];     for (int lst = 1; lst <= val; ++lst) {       if (a[n] == -1) {         d[lst][n][0] = lst;         d[lst][n][1] = 1;       } else {         d[lst][n][0] = (a[n] <= lst);         d[lst][n][1] = (a[n] == lst);       }     }     for (int i = n - 1; i >= 1; --i) {       if (a[i] != -1) {         for (int lst = 1; lst <= val; ++lst) {           d[lst][i][0] = d[a[i]][i + 1][lst < a[i]];         }         for (int lst = 1; lst <= a[i]; ++lst) {           d[lst][i][1] = d[a[i]][i + 1][lst < a[i]];         }         continue;       }       long long pref = 0, suff = 0;       for (int b = 1; b <= val; ++b) {         suff += d[b][i + 1][1];         suff %= md;       }       for (int lst = 1; lst <= val; ++lst) {         pref += d[lst][i + 1][0];         pref %= md;         suff += md - d[lst][i + 1][1];         suff %= md;         d[lst][i][0] = (pref + suff) % md;       }       suff = 0;       for (int lst = val; lst >= 1; --lst) {         d[lst][i][1] = (suff + d[lst][i + 1][0]) % md;         suff += d[lst][i + 1][1];         suff %= md;       }     }     cout << d[1][1][0];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const long double pi = acos(-1);   const int MOD = 1e9 + 7;   const long long int LINF = 0x3f3f3f3f3f3f3f3f;   int n, m, k, s;   const int MAXN = 2005;   int a[MAXN][MAXN];   int maxma[15], maxme[15], minma[15], minme[15];   int seq[MAXN * MAXN];   int get(int v1, int v2) {     int ret = 0;     ret = max(ret, maxma[v1] - minma[v2]);     ret = max(ret, maxme[v1] - minme[v2]);     return ret;   }   int main() {     memset(minma, INF, sizeof(minma));     memset(minme, INF, sizeof(minme));     scanf( %d%d%d , &n, &m, &k);     scanf( %d , &s);     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         scanf( %d , &a[i][j]);         int v = a[i][j];         int ma = i + j;         int me = i - j;         maxma[v] = max(maxma[v], ma);         maxme[v] = max(maxme[v], me);         minma[v] = min(minma[v], ma);         minme[v] = min(minme[v], me);       }     }     int resp = 0;     scanf( %d , &seq[0]);     for (int i = 1; i < s; i++) {       scanf( %d , &seq[i]);       resp = max(resp, max(get(seq[i], seq[i - 1]), get(seq[i - 1], seq[i])));     }     printf( %d n , resp);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void make_uni(vector<int> &v) {     sort(v.begin(), v.end());     v.resize(unique(v.begin(), v.end()) - v.begin());   }   int get(vector<int> &v, int d) {     return lower_bound(v.begin(), v.end(), d) - v.begin();   }   int getu(vector<int> &v, int d) {     return upper_bound(v.begin(), v.end(), d) - v.begin();   }   int n, k;   vector<tuple<int, int, int> > que;   int main() {     scanf( %d %d , &n, &k);     vector<int> c1;     for (int i = 0, a, b, c; i < n; ++i) {       scanf( %d %d %d , &a, &b, &c);       c1.emplace_back(c);       que.emplace_back(a, b, c);     }     make_uni(c1);     vector<vector<int> > c2(c1.size());     for (int i = 0, a, b, c; i < n; ++i) {       tie(a, b, c) = que[i];       c2[get(c1, c)].emplace_back(a + b);     }     for (int i = 0; i < c2.size(); ++i) make_uni(c2[i]);     vector<vector<vector<int> > > c3(c2.size());     for (int i = 0; i < c3.size(); ++i) c3[i].resize(c2[i].size());     for (int i = 0, a, b, c; i < n; ++i) {       tie(a, b, c) = que[i];       int k1 = get(c1, c);       int k2 = get(c2[k1], a + b);       for (int i = k2 + 1; i; i -= i & -i) c3[k1][i - 1].emplace_back(a);     }     for (int i = 0; i < c3.size(); ++i)       for (int j = 0; j < c3[i].size(); ++j) make_uni(c3[i][j]);     vector<vector<vector<int> > > t(c3.size());     for (int i = 0; i < c3.size(); ++i) {       t[i].resize(c3[i].size());       for (int j = 0; j < t[i].size(); ++j) t[i][j].resize(c3[i][j].size());     }     sort(que.begin(), que.end(),          [&](const tuple<int, int, int> &a, const tuple<int, int, int> &b) {            return get<0>(a) < get<0>(b);          });     long long sum = 0;     for (auto &x : que) {       int a, b, c;       tie(a, b, c) = x;       for (int l = get(c1, c - k), r = getu(c1, c + k); l != r; ++l)         for (int i = get(c2[l], a) + 1; i <= c2[l].size(); i += i & -i)           for (int j = get(c3[l][i - 1], a - b) + 1; j <= c3[l][i - 1].size();                j += j & -j)             sum += t[l][i - 1][j - 1];       int p = get(c1, c);       for (int i = get(c2[p], a + b) + 1; i; i -= i & -i)         for (int j = get(c3[p][i - 1], a) + 1; j; j -= j & -j)           t[p][i - 1][j - 1]++;     }     printf( %lld n , sum);   }


#include <bits/stdc++.h>   using namespace std;   int prime(int n) {     int val = sqrt(n), counter = 0;     ;     if (n == 1) return false;     for (int i = 2; i <= val; i++) {       if (!(n % i)) counter++;     }     if (counter) return -1;     return n;   }   int root(int n) {     int val = sqrt(n);     int x = n, flag = 0;     ;     for (int i = 2; i <= val; i++) {       if (!(n % i)) {         while (n > 1) {           if (n % i != 0) {             flag = 1;             break;           }           n = n / i;         }         if (flag) return -1;         return x;       }     }     return -1;   }   int main() {     int n, counter = 0, val, val1;     vector<int> vec;     ;     cin >> n;     for (int i = 2; i <= n; i++) {       val = prime(i);       val1 = root(i);       if (val != -1) {         counter++;         vec.push_back(val);       } else if (val1 != -1) {         counter++;         vec.push_back(val1);       }     }     cout << counter << endl;     for (int i = 0; i < counter; i++) cout << vec[i] <<    ;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long int a1, a2, a3, b1, b2, b3, n, total = 0;     cin >> a1 >> a2 >> a3 >> b1 >> b2 >> b3 >> n;     if ((a1 + a2 + a3) % 5 == 0) {       total = total + (a1 + a2 + a3) / 5;     } else {       total = total + 1 + (a1 + a2 + a3) / 5;     }     if ((b1 + b2 + b3) % 10 == 0) {       total = total + (b1 + b2 + b3) / 10;     } else {       total = total + 1 + (b1 + b2 + b3) / 10;     }     if (total <= n) {       cout <<  YES ;     } else {       cout <<  NO ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int R = 300005;   const int inf = 2000000001;   const int mod = 998244353;   map<char, int> m;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, k;     cin >> n >> k;     string second;     cin >> second;     if (k == 1) {       for (int i = 0; i < n; i++) {         m[second[i]]++;       }       int ma = 0;       for (int i = 0; i < n; i++) {         if (m[second[i]] > ma) ma = m[second[i]];       }       cout << ma;       return 0;     }     char last = second[0];     int l = 1;     for (int i = 1; i < n; i++) {       if (second[i] == second[i - 1] && l > 0) {         l++;         if (l == k) {           l = 0;           m[second[i]]++;         }       } else {         l = 1;         last = second[i];       }     }     int ma = 0;     for (int i = 0; i < n; i++) {       if (m[second[i]] > ma) ma = m[second[i]];     }     cout << ma;   }


#include <bits/stdc++.h>   long long int maxx(long long int a, long long int b) { return a > b ? a : b; }   long long int minn(long long int a, long long int b) { return a > b ? b : a; }   using namespace std;   int dx[] = {1, 1, 1, 0, 0, -1, -1, -1, 0};   int dy[] = {-1, 0, 1, 1, -1, 0, 1, -1, 0};   void boostIO() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   long long int gcd(long long int a, long long int b) {     return b == 0 ? a : gcd(b, a % b);   }   long long int lcm(long long int a, long long int b) {     return (a * b) / gcd(a, b);   }   long long int poww(long long int base, long long int exp) {     base %= 1000000007;     long long int result = 1;     while (exp > 0) {       if (exp & 1) result = (result * base) % 1000000007;       base = (base * base) % 1000000007;       exp >>= 1;     }     return result;   }   const int Max_N = 100100;   int dist[101][Max_N];   vector<int> a[Max_N], type[101];   void bfs(int t) {     for (int i = 0; i <= 100000; i++) dist[t][i] = 1e9;     queue<int> q;     for (int i = 0; i < type[t].size(); i++)       q.push(type[t][i]), dist[t][type[t][i]] = 0;     while (!q.empty()) {       int node = q.front();       q.pop();       for (int i = 0; i < a[node].size(); i++) {         if (dist[t][a[node][i]] > dist[t][node] + 1) {           dist[t][a[node][i]] = dist[t][node] + 1;           q.push(a[node][i]);         }       }     }     return;   }   int main() {     boostIO();     int n, m, k, seco3nd;     cin >> n >> m >> k >> seco3nd;     for (int i = 1; i <= n; i++) {       int x;       cin >> x;       type[x].push_back(i);     }     for (int i = 0; i < m; i++) {       int u, v;       cin >> u >> v;       a[u].push_back(v), a[v].push_back(u);     }     for (int i = 1; i <= k; i++) {       bfs(i);     }     for (int i = 1; i <= n; i++) {       priority_queue<int> q;       int ans = 0;       for (int j = 1; j <= k; j++) {         q.push(-dist[j][i]);       }       for (int j = 0; j < seco3nd; j++) {         ans = ans + -q.top();         q.pop();       }       cout << ans <<    ;     }   }


#include <stdio.h>   #include <stdlib.h>   #include <sys/time.h>      #define N 300000      int min(int a, int b) { return a < b ? a : b; }      unsigned int X;      void srand_() {    struct timeval tv;       gettimeofday(&tv, NULL);    X = tv.tv_sec ^ tv.tv_usec;    if (X % 2 == 0)     X++;   }      int rand_() {    return (X *= 3) >> 1;   }      int *ej[N], eo[N];      void append(int i, int j) {    int o = eo[i]++;       if (o >= 2 && (o & o - 1) == 0)     ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);    ej[i][o] = j;   }      int aa[N], pp[N], dd[N], mn[N];      void dfs1(int i, int d) {    int o;       dd[i] = d;    for (o = eo[i]; o--; ) {     int j = ej[i][o];        dfs1(j, d + 1);    }   }      int bb[N], a_, a, b;      void sort(int *ii, int l, int r) {    while (l < r) {     int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;        while (j < k)      if (mn[ii[j]] == mn[i_])       j++;      else if (mn[ii[j]] < mn[i_]) {       tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;       i++, j++;      } else {       k--;       tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;      }     sort(ii, l, i);     l = k;    }   }      void dfs2(int i) {    int o;       mn[i] = aa[i];    for (o = eo[i]; o--; ) {     int j = ej[i][o];        dfs2(j);     mn[i] = min(mn[i], mn[j]);    }    sort(ej[i], 0, eo[i]);   }      int dfs3(int i) {    int o;       bb[i] = ++b;    for (o = 0; o < eo[i]; o++) {     int j = ej[i][o];        if (!dfs3(j))      return 0;    }    if (aa[i] < a_ && aa[i] != a++)     return 0;    return 1;   }      int dfs4(int i) {    int o;       if (aa[i] < a_)     return 1;    if (aa[i] != a++)     return 0;    for (o = 0; o < eo[i]; o++) {     int j = ej[i][o];        if (!dfs4(j))      return 0;    }    return 1;   }      int main() {    static int ii[N];    int n, h, i, j, tmp;    long long ans;       srand_();    scanf( %d , &n);    for (i = 0; i < n; i++) {     scanf( %d , &aa[i]), aa[i]--;     ii[aa[i]] = i;    }    for (i = 0; i < n; i++)     ej[i] = (int *) malloc(2 * sizeof *ej[i]);    for (h = 0; h < n - 1; h++) {     scanf( %d%d , &i, &j), i--, j--;     append(i, j);     pp[j] = i;    }    dfs1(0, 0);    a_ = 0, ans = 0;    while (ii[a_] != 0)     ans += dd[ii[a_++]];    if (a_ > 0) {     i = ii[--a_];     while (i != 0) {      if (aa[pp[i]] != a_ + dd[i]) {       printf( NO n );       return 0;      }      tmp = aa[i], aa[i] = aa[pp[i]], aa[pp[i]] = tmp;      i = pp[i];     }    }    dfs2(0);    if (!dfs3(0) || !dfs4(0)) {     printf( NO n );     return 0;    }    printf( YES n );    printf( %lld n , ans);    for (i = 0; i < n; i++)     printf( %d  , bb[i]);    printf(  n );    return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int maxn = 2005;   long long int cnt[26];   long long int n, k;   bool ok(long long int l, long long int k) {     long long int k2 = k % l;     if (k2 == 0) return 1;     if (l % k2 != 0) return cnt[25] >= l;     long long int need = l / k2;     for (long long int i = 25; i >= 0; i--) {       l -= (cnt[i] / need) * need;       if (l <= 0) return 1;     }     return 0;   }   int32_t main() {     long long int t;     cin >> t;     while (t--) {       for (long long int i = 0; i < 26; i++) cnt[i] = 0;       string s;       cin >> n >> k >> s;       for (long long int i = 0; i < n; i++) cnt[s[i] -  a ]++;       sort(cnt, cnt + 26);       long long int ans;       long long int flag = 1;       for (ans = n; ans >= 1; ans--) {         for (long long int i = 1; i * i <= k and flag; i++) {           if (k % i) continue;           if (ok(ans, i)) flag = 0;           if (flag and k / i != i and ok(ans, k / i)) flag = 0;         }         if (flag == 0) break;       }       cout << ans <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int oo = 1e9;   const int MOD = 1000000007;   const int N = 100010;   vector<int> g[N];   multiset<int> seg[4 * N], stk;   int n, u, v, sz[N], st[N], ed[N], bk[N], cnt = 1, res[N];   void dfs(int u, int p) {     sz[u] = 1, bk[cnt] = u, st[u] = cnt++;     for (int i = 0; i < g[u].size(); ++i) {       int v = g[u][i];       dfs(v, u);       sz[u] += sz[v];     }     ed[u] = cnt - 1;   }   void update(int n, int s, int e, int at, int val, int type) {     if (s > at || e < at) return;     if (type == 0) {       seg[n].insert(val);     } else {       seg[n].erase(seg[n].find(val));     }     if (s == e) return;     update(2 * n, s, (s + e) / 2, at, val, type);     update(2 * n + 1, (s + e) / 2 + 1, e, at, val, type);   }   inline int check(multiset<int> &mst, int mn, int mx, int OFFSET) {     int ans = 1e9;     int S = (mx - mn) / 2 + OFFSET;     multiset<int>::iterator it = mst.lower_bound(S);     for (int f = 0; f < 2; ++f) {       if (it != mst.end() && (*it != n))         ans = min(ans, max(mx - (*it - OFFSET), mn + (*it - OFFSET)));       if (it == mst.begin()) break;       it--;     }     return ans;   }   int l, r, mn, mx;   int get(int n, int s, int e) {     if (s > r || e < l) return oo;     if (s >= l && e <= r) return check(seg[n], mn, mx, 0);     return min(get(2 * n, s, (s + e) / 2), get(2 * n + 1, (s + e) / 2 + 1, e));   }   int get(int _l, int _r, int _mn, int _mx) {     l = _l;     r = _r;     mn = _mn;     mx = _mx;     if (l > r) return oo;     return get(1, 1, n);   }   void solve(int u, int p) {     pair<int, int> mx(-1, -1), mn(1e9, -1);     for (int i = 0; i < g[u].size(); ++i) {       int v = g[u][i];       mx = max(mx, make_pair(sz[v], v));       mn = min(mn, make_pair(sz[v], v));     }     if (p != -1) {       mx = max(mx, make_pair(n - sz[u], p));       mn = min(mn, make_pair(n - sz[u], p));     }     update(1, 1, n, st[u], sz[u], 1);     int ans = max(0, mx.first);     if (mx.first != mn.first) {       if (mx.second == p) {         ans = min(ans, get(1, st[u] - 1, mn.first, mx.first));         ans = min(ans, get(ed[u] + 1, n, mn.first, mx.first));         ans = min(ans, check(stk, mn.first, mx.first, sz[u]));       } else {         ans = min(ans, get(st[mx.second], ed[mx.second], mn.first, mx.first));       }       for (int i = 0; i < g[u].size(); ++i) {         int v = g[u][i];         if (v == mx.second || v == mn.second) continue;         ans = max(ans, sz[v]);       }       if (p != -1 && mx.second != p && mn.second != p) {         ans = max(ans, n - sz[u]);       }     }     res[u] = ans;     stk.insert(sz[u]);     for (int i = 0; i < g[u].size(); ++i) {       solve(g[u][i], u);     }     stk.erase(stk.find(sz[u]));     update(1, 1, n, st[u], sz[u], 0);   }   int root = -1;   int main() {     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       scanf( %d%d , &u, &v);       if (!u) {         root = v;         continue;       }       g[u].push_back(v);     }     dfs(root, -1);     for (int i = 1; i <= n; ++i) update(1, 1, n, st[i], sz[i], 0);     solve(root, -1);     for (int i = 1; i <= n; ++i) {       printf( %d n , res[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> myvec;   long long int dp[100003];   int main() {     int t1, t2, x, index1, index2, time, n;     vector<int>::iterator it, it2;     cin >> n;     myvec.push_back(0);     dp[0] = 0;     for (int i = 0; i < n; i++) cin >> x, myvec.push_back(x);     for (int i = 1; i <= n; i++) {       time = myvec[i];       t1 = max(0, myvec[i] - 90);       t2 = max(0, myvec[i] - 1440);       it = upper_bound(myvec.begin(), myvec.end(), t1);       it2 = upper_bound(myvec.begin(), myvec.end(), t2);       index1 = it - myvec.begin();       index2 = it2 - myvec.begin();       if (myvec[i] - 90 < 0) {         dp[i] = min(dp[0] + 50, dp[0] + 120);         dp[i] = min(dp[i], dp[i - 1] + 20);       } else {         if (myvec[i] - 1440 < 0)           dp[i] = min(dp[index1 - 1] + 50, dp[0] + 120);         else           dp[i] = min(dp[index1 - 1] + 50, dp[index2 - 1] + 120);         dp[i] = min(dp[i], dp[i - 1] + 20);       }     }     for (int i = 1; i <= n; i++) cout << dp[i] - dp[i - 1] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   mt19937_64 random_num(       chrono::high_resolution_clock::now().time_since_epoch().count());   const int MOD = 1000000007;   const int inf = 2000000000;   const int mod = 1007050321;   const double e = 0.000001;   const int N = 200005;   const bool aut = 788480;   const int base = 521;   int n, m, p, b[N], a[N];   int poww(int n, int a) {     if (n == 1) return a;     if (n == 0) return 0;     int t = poww(n / 2, a);     t = 1LL * t * t % MOD;     if (n & 1) t = 1LL * t * a % MOD;     return t;   }   int main() {     ios_base ::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> n >> m >> p;     vector<int> pack;     for (int i = 1; i <= n; ++i) cin >> a[i], pack.push_back(a[i]);     for (int i = 1; i <= m; ++i) cin >> b[i], pack.push_back(b[i]);     sort((pack).begin(), (pack).end());     for (int i = 1; i <= n; ++i)       a[i] = lower_bound((pack).begin(), (pack).end(), a[i]) - pack.begin() + 1;     for (int i = 1; i <= m; ++i)       b[i] = lower_bound((pack).begin(), (pack).end(), b[i]) - pack.begin() + 1;     vector<int> ans;     long long S = 0, T = 1;     for (int i = 1; i <= m; ++i) S += b[i], T = 1LL * T * b[i] % MOD;     for (int i = 1; i <= p; ++i) {       long long sum = 0;       int mul = 1;       if ((n - i) / p < m - 1) break;       for (int j = 0; j <= m - 1; ++j) {         sum += a[i + j * p];         mul = 1LL * mul * a[i + j * p] % MOD;       }       if (sum == S && mul == T) ans.push_back(i);       for (int j = m; i + j * p <= n; ++j) {         sum += a[i + j * p] - a[i + (j - m) * p];         mul = 1LL * mul * poww(MOD - 2, a[i + (j - m) * p]) % MOD * a[i + j * p] %               MOD;         if (mul == T && sum == S) ans.push_back(i + (j - m + 1) * p);       }     }     sort((ans).begin(), (ans).end());     cout << ans.size() <<   n ;     for (int v : ans) cout << v <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline bool chmin(T& a, T b) {     if (a > b) {       a = b;       return true;     }     return false;   }   template <class T>   inline bool chmax(T& a, T b) {     if (a < b) {       a = b;       return true;     }     return false;   }   long long select(vector<long long>& a, long long k) {     for (long long l = 0, r = a.size() - 1; l <= r;) {       long long p = a[(l + r) / 2];       long long i = l - 1, j = r + 1;       while (1) {         while (a[++i] < p)           ;         while (a[--j] > p)           ;         if (i >= j) break;         swap(a[i], a[j]);       }       if (k == i - l)         return a[i];       else if (k < i - l) {         r = i - 1;       } else if (k > i - l) {         k -= (i - l + 1);         l = j + 1;       }     }     return -99999999;   }   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long modinv(long long a, long long m) {     long long b = m, u = 1, v = 0;     while (b) {       long long t = a / b;       a -= t * b;       swap(a, b);       u -= t * v;       swap(u, v);     }     u %= m;     if (u < 0) u += m;     return u;   }   vector<pair<long long, long long>> prim;   void pf(long long n) {     long long s = sqrt(n);     long long r = 0;     for (long long i = 2; i <= s; i++) {       if ((n % i) == 0) {         r = 0;         do {           r++;           n = n / i;         } while ((n % i) == 0);         prim.push_back({i, r});       }     }     if (n > s) {       prim.push_back({n, 1});     }   }   long long powmod(int a, int n) {     long long x = 1;     while (n > 0) {       if (n & 1) {         x = (x * a) % (long long)(1e9 + 7);       }       a = (a * a) % (long long)(1e9 + 7);       n >>= 1;     }     return x;   }   void solve() {     long long N;     cin >> N;     string s;     cin >> s;     vector<string> cs(30);     vector<string> csr(30);     long long ans = -1;     for (long long j = 0; j < (26); j++) {       char ch = j +  a ;       for (long long i = 0; i < (N); i++) {         if (s[i] != ch) {           cs[j] += s[i];         }       }       csr[j] = cs[j];       reverse(csr[j].begin(), csr[j].end());       if (csr[j] != cs[j]) continue;       long long cnt = 0;       long long x1 = 0;       long long x2 = N - 1;       while (x2 - x1 >= 1) {         if (s[x1] == s[x2]) {           x1++;           x2--;         } else if (s[x1] == ch) {           x1++;           cnt++;         } else if (s[x2] == ch) {           x2--;           cnt++;         }       }       if (ans < 0 || ans > cnt) ans = cnt;     }     cout << ans << endl;   }   int main(void) {     long long t;     cin >> t;     for (long long i = 0; i < (t); i++) solve();   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 6;   int ans[N];   int main() {     int n, k;     scanf( %d %d , &n, &k);     if (k == 1 or k * 3 > n) return !printf( -1 n );     for (int x = 1; x <= k; x += 2) {       for (int j = 0; j < 6; ++j) {         if (j == 0 or j == 1 or j == 3)           ans[(x - 1) * 3 + j + 1] = x;         else           ans[(x - 1) * 3 + j + 1] = x + 1;       }     }     for (int i = 1; i <= n; ++i)       if (ans[i] > k or ans[i] == 0) ans[i] = 1;     if (n % 3 == 0 and k % 2 == 1) ans[1] = k, ans[n] = 1;     for (int i = 1; i <= n; ++i) printf( %d  , ans[i]);   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 3E4 + 5;   const int maxm = 1E5 + 5;   int n, m, cnt;   int ans[maxn];   bool vis[maxn];   vector<int> to[maxn];   void DFS(int u, int fa) {     vis[u] = 1;     for (int i = 0; i < to[u].size(); ++i) {       int v = to[u][i];       if (!vis[v])         DFS(v, u);       else if (v == fa) {         puts( -1 );         exit(0);       }     }     ans[++cnt] = u;   }   int main() {     scanf( %d%d , &n, &m);     while (m--) {       int x, y;       scanf( %d%d , &x, &y);       to[x].push_back(y);     }     for (int i = 1; i <= n; ++i)       if (!vis[i]) DFS(i, 0);     for (int i = 1; i <= n; ++i) printf( %d  , ans[i]);   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2016;   vector<int> g[2][maxn];   vector<int> g2[2][maxn];   vector<int> gr[2][maxn];   int n;   int neg(int first) {     if (first < n) {       return first + n;     } else {       return first - n;     }   }   void add(int f, int first, int second) {     g[f][first].push_back(second);     gr[f][second].push_back(first);   }   vector<int> was;   vector<int> ls[2];   vector<int> col[2];   vector<int> who[2][maxn];   int C[2];   void dfs1(int f, int v) {     was[v] = 1;     for (int e : g[f][v])       if (!was[e]) {         dfs1(f, e);       }     ls[f].push_back(v);   }   void dfs2(int f, int v, int c) {     col[f][v] = c;     who[f][c].push_back(v);     for (int e : gr[f][v])       if (col[f][e] == -1) {         dfs2(f, e, c);       }   }   void compress(int f) {     was = vector<int>(2 * n, 0);     for (int i = 0; i < 2 * n; i++) {       if (was[i] == 0) {         dfs1(f, i);       }     }     col[f] = vector<int>(2 * n, -1);     reverse((ls[f]).begin(), (ls[f]).end());     C[f] = 0;     for (int v : ls[f]) {       if (col[f][v] == -1) {         dfs2(f, v, ++C[f]);       }     }     for (int v = 0; v < 2 * n; v++) {       for (int e : g[f][v]) {         g2[f][col[f][v]].push_back(col[f][e]);       }     }     for (int i = 1; i <= C[f]; i++) {       {         sort((g2[f][i]).begin(), (g2[f][i]).end()),             g2[f][i].resize(unique((g2[f][i]).begin(), (g2[f][i]).end()) -                             g2[f][i].begin());       };     }   }   vector<unsigned long long> path[2][maxn];   int K;   void SET(vector<unsigned long long>& a, int first) {     a[first >> 6] |= ((unsigned long long)1) << (first & 63);   }   bool ISONE(vector<unsigned long long>& a, int first) {     return (a[first >> 6] >> (first & 63)) & 1;   }   void OR(vector<unsigned long long>& a, vector<unsigned long long>& b,           int first) {     for (int i = first >> 6; i < K; i++) {       a[i] |= b[i];     }   }   void calcpath(int f) {     K = (C[f] + 1) / 64 + 2;     for (int i = 1; i <= C[f]; i++) {       path[f][i] = vector<unsigned long long>(K, 0);     }     for (int v = C[f]; v >= 1; v--) {       SET(path[f][v], v);       for (int e : g2[f][v]) {         OR(path[f][v], path[f][e], e);       }     }   }   void printsol(int f) {     for (int i = 0; i < n; i++) {       if (col[f][i] < col[f][i + n]) {         printf( 0  );       } else {         printf( 1  );       }     }   }   vector<pair<int, int>> lse[2];   bool can1(int f, int first) {     return !ISONE(path[f][col[f][first]], col[f][neg(first)]);   }   bool can2(int f, int x1, int x2) {     for (int i = 0; i < n; i++) {       int val1 = -1;       if (ISONE(path[f][col[f][x1]], col[f][i])) {         val1 = 1;       }       if (ISONE(path[f][col[f][x1]], col[f][i + n])) {         val1 = 0;       }       int val2 = -1;       if (ISONE(path[f][col[f][x2]], col[f][i])) {         val2 = 1;       }       if (ISONE(path[f][col[f][x2]], col[f][i + n])) {         val2 = 0;       }       if (val1 != -1 && val2 != -1) {         if (val1 != val2) {           return false;         }       }     }     return true;   }   bool canprint(int f, int x1, int x2) {     for (int i = 0; i < n; i++) {       int val1 = -1;       if (ISONE(path[f][col[f][x1]], col[f][i])) {         val1 = 1;       }       if (ISONE(path[f][col[f][x1]], col[f][i + n])) {         val1 = 0;       }       int val2 = -1;       if (ISONE(path[f][col[f][x2]], col[f][i])) {         val2 = 1;       }       if (ISONE(path[f][col[f][x2]], col[f][i + n])) {         val2 = 0;       }       if (val1 == -1) {         if (val2 == -1) {           if (col[f][i] < col[f][i + n]) {             printf( 0  );           } else {             printf( 1  );           }         } else {           printf( %d  , val2);         }         continue;       }       if (val2 == -1) {         if (val1 == -1) {           if (col[f][i] < col[f][i + n]) {             printf( 0  );           } else {             printf( 1  );           }         } else {           printf( %d  , val1);         }         continue;       }       printf( %d  , val1);     }     return false;   }   void can(int f1, int f2) {     for (auto p : lse[f1]) {       int v1 = p.first;       int v2 = p.second;       if (can1(f2, neg(v1)) && can1(f2, neg(v1))) {         if (can2(f2, neg(v1), neg(v2))) {           canprint(f2, neg(v1), neg(v2));           exit(0);         }       }     }   }   int main() {     vector<int> m(2, 0);     scanf( %d %d %d , &n, &m[0], &m[1]);     for (int f = 0; f < 2; f++) {       for (int i = 0; i < m[f]; i++) {         int first, second;         scanf( %d %d , &first, &second);         if (first > 0) {           first = first - 1;         } else {           first = -first - 1 + n;         }         if (second > 0) {           second = second - 1;         } else {           second = -second - 1 + n;         }         lse[f].push_back(make_pair(first, second));         add(f, neg(first), second);         add(f, neg(second), first);       }       compress(f);       calcpath(f);     }     vector<bool> nosol(2, false);     for (int f = 0; f < 2; f++) {       for (int i = 0; i < n; i++) {         if (ISONE(path[f][col[f][i]], col[f][i + n]) &&             ISONE(path[f][col[f][i + n]], col[f][i])) {           nosol[f] = true;         }       }     }     if (nosol[0] && nosol[1]) {       printf( SIMILAR n );       return 0;     }     if (nosol[0] && !nosol[1]) {       printsol(1);       return 0;     }     if (!nosol[0] && nosol[1]) {       printsol(0);       return 0;     }     can(0, 1);     can(1, 0);     printf( SIMILAR n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool h[1000009];   int main() {     int n, m, k, tmp, tmp1, pos = 1;     cin >> n >> m >> k;     for (int i = 0; i < m; ++i) {       cin >> tmp;       h[tmp] = true;     }     if (h[1]) {       cout << pos << endl;       return 0;     }     for (int i = 0; i < k; ++i) {       cin >> tmp >> tmp1;       if (pos == tmp || tmp1 == pos) {         pos = (tmp == pos) ? tmp1 : tmp;         if (h[pos]) break;       }     }     cout << pos << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int a, b, c, d, i, j, k, n, m, t;     cin >> t;     while (t--) {       cin >> n;       long long int ar[n + 5];       memset(ar, -1, sizeof(ar));       int f = 0;       for (i = 0; i < n; i++) {         cin >> a;         if (ar[a] == -1)           ar[a] = i;         else {           if ((abs(i - ar[a]) - 1) >= 1) f = 1;         }       }       if (f == 0)         cout <<  NO n ;       else         cout <<  YES n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<string> tmp;   long long num(string str) {     int len = str.size();     long long res = 0;     for (int i = 0; i < len; i++) {       res = res * 10 + str[i] -  0 ;     }     return res;   }   void solve(int N) {     queue<string> q;     q.push( 11 );     q.push( 22 );     q.push( 33 );     q.push( 44 );     q.push( 55 );     q.push( 66 );     q.push( 77 );     q.push( 88 );     q.push( 99 );     tmp.push_back( 11 );     tmp.push_back( 22 );     tmp.push_back( 33 );     tmp.push_back( 44 );     tmp.push_back( 55 );     tmp.push_back( 66 );     tmp.push_back( 77 );     tmp.push_back( 88 );     tmp.push_back( 99 );     while (!q.empty()) {       string str = q.front();       q.pop();       if (tmp.size() >= N) return;       for (int i = 0; i < 10; i++) {         char ch = i + 48;         int l = str.size() / 2;         string kp = str.substr(0, l) + ch + ch + str.substr(l, l);         tmp.push_back(kp);         q.push(kp);       }     }   }   int main() {     int k, p;     scanf( %d%d , &k, &p);     solve(k);     long long res = 0;     for (int i = 0; i < k; i++) {       res = (res + num(tmp[i])) % p;     }     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 50;   int n, cnt;   pair<int, int> poi[maxn], A[maxn];   void solve(int l, int r) {     if (l > r) return;     int mid = (l + r) >> 1;     for (int i = l; i <= r; i++)       poi[++cnt] = (pair<int, int>){A[mid].first, A[i].second};     solve(l, mid - 1);     solve(mid + 1, r);   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d%d , &A[i].first, &A[i].second);     sort(A + 1, A + 1 + n);     solve(1, n);     sort(poi + 1, poi + 1 + cnt);     cnt = unique(poi + 1, poi + 1 + cnt) - poi - 1;     printf( %d n , cnt);     for (int i = 1; i <= cnt; i++) printf( %d %d n , poi[i].first, poi[i].second);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100100, INF = 0x7fffffff, _INF = 0x80000000;   class Splay_Tree {    public:     int tot, root;     struct Splay_Node {       int c[2], fa, key, cnt, size;       Splay_Node() {         c[0] = c[1] = fa = key = cnt = size = 0;         return;       }     } p[maxn + 10];     void Init() {       p[1].c[0] = p[1].size = tot = 2;       p[1].key = INF;       p[2].key = _INF;       p[1].cnt = p[2].cnt = p[2].size = p[2].fa = root = 1;       return;     }     inline bool Gr(int k) { return p[p[k].fa].c[0] != k; }     inline void Update(int k) {       p[k].size = p[k].cnt + p[p[k].c[0]].size + p[p[k].c[1]].size;       return;     }     void Rotate(int a, bool t) {       int b = p[a].c[t ^ 1];       if (p[b].fa = p[a].fa)         p[p[a].fa].c[Gr(a)] = b;       else         root = b;       if (p[a].c[t ^ 1] = p[b].c[t]) p[p[b].c[t]].fa = a;       p[b].c[t] = a;       p[a].fa = b;       Update(a);       Update(b);       return;     }     void Splay(int k, int t = 0) {       while (p[k].fa != t) {         int F = p[k].fa, G = p[F].fa;         if (G != t) {           if (Gr(k) != Gr(F)) {             Rotate(F, Gr(k) ^ 1);             Rotate(G, Gr(k) ^ 1);           } else {             Rotate(G, Gr(F) ^ 1);             Rotate(F, Gr(k) ^ 1);           }         } else           Rotate(F, Gr(k) ^ 1);       }       return;     }     int Insert(int k, int key) {       if (key != p[k].key) {         bool t = key > p[k].key;         while (p[k].c[t] && key != p[p[k].c[t]].key)           t = key > p[k = p[k].c[t]].key;         if (!p[k].c[t]) {           p[p[k].c[t] = ++tot].key = key;           p[tot].fa = k;           k = tot;         } else           k = p[k].c[t];       }       ++p[k].cnt;       ++p[k].size;       Splay(k);       return k;     }     int Rank(int k, int key) {       int rank = -1;       while (k && key != p[k].key) {         bool t = key > p[k].key;         if (t) rank += p[p[k].c[0]].size + p[k].cnt;         k = p[k].c[t];       }       if (k && key == p[k].key) rank += p[k].cnt;       return rank + p[p[k].c[0]].size;     }   } ST;   int n;   struct Point {     int x, y;   } P[maxn];   bool cmp1(Point a, Point b) { return a.x < b.x; }   bool cmp2(Point a, Point b) { return a.y < b.y; }   int v1[maxn], v2[maxn];   bool vis1[maxn], vis2[maxn];   int p[10], c[10];   int k = 0;   struct node1 {     int lx1, lx2, ly1, ly2, t1, t2, t3, t4;     bool flag;   } mes[maxn << 2];   int tot = -1, head[maxn];   struct node2 {     int p, next, idx1, idx2;   } edge[maxn << 4];   inline void Add(int u, int v, int idx1, int idx2) {     edge[++tot] = (node2){v, head[u], idx1, idx2};     head[u] = tot;     return;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) scanf( %d%d , &P[i].x, &P[i].y);     for (int i = 1; i <= 9; ++i) {       p[i] = i;       scanf( %d , &c[i]);     }     sort(P + 1, P + n + 1, cmp1);     for (int i = 1, j, sum = 0; i <= n; i = j + 1) {       for (j = i; j < n && P[j].x == P[j + 1].x; ++j)         ;       v1[sum += j - i + 1] = P[i].x;       vis1[sum] = 1;     }     sort(P + 1, P + n + 1, cmp2);     for (int i = 1, j, sum = 0; i <= n; i = j + 1) {       for (j = i; j < n && P[j].y == P[j + 1].y; ++j)         ;       v2[sum += j - i + 1] = i;       vis2[sum] = 1;     }     memset(head, -1, sizeof(head));     do {       int t1 = c[p[1]] + c[p[4]] + c[p[7]], t2 = t1 + c[p[2]] + c[p[5]] + c[p[8]],           t3 = c[p[1]] + c[p[2]] + c[p[3]], t4 = t3 + c[p[4]] + c[p[5]] + c[p[6]];       if (vis1[t1] && vis1[t2] && vis2[t3] && vis2[t4]) {         mes[++k] = (node1){v1[t1],                            v1[t2],                            P[v2[t3]].y,                            P[v2[t4]].y,                            c[p[1]],                            c[p[1]] + c[p[2]],                            c[p[1]] + c[p[4]],                            c[p[1]] + c[p[2]] + c[p[4]] + c[p[5]],                            1};         Add(v2[t3], v1[t1], k, 1);         Add(v2[t3], v1[t2], k, 2);         Add(v2[t4], v1[t1], k, 3);         Add(v2[t4], v1[t2], k, 4);       }     } while (next_permutation(p + 1, p + 10));     ST.Init();     for (int i = 1, j; i <= n; i = j + 1) {       for (ST.Insert(ST.root, P[j = i].x); j < n && P[j].y == P[j + 1].y;            ST.Insert(ST.root, P[++j].x))         ;       for (int k = head[i]; ~k; k = edge[k].next) {         int p = edge[k].p, idx1 = edge[k].idx1, idx2 = edge[k].idx2;         int cnt = ST.Rank(ST.root, p);         if (idx2 == 1 && mes[idx1].t1 != cnt) mes[idx1].flag = 0;         if (idx2 == 2 && mes[idx1].t2 != cnt) mes[idx1].flag = 0;         if (idx2 == 3 && mes[idx1].t3 != cnt) mes[idx1].flag = 0;         if (idx2 == 4 && mes[idx1].t4 != cnt) mes[idx1].flag = 0;       }     }     for (int i = 1; i <= k; ++i)       if (mes[i].flag) {         printf( %.10lf %.10lf n%.10lf %.10lf n , double(mes[i].lx1) + 0.5,                double(mes[i].lx2) + 0.5, double(mes[i].ly1) + 0.5,                double(mes[i].ly2) + 0.5);         return 0;       }     puts( -1 );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   using vc = vector<T>;   template <class T>   using vvc = vc<vc<T>>;   template <class T>   void mkuni(vector<T>& v) {     sort(v.begin(), v.end());     v.erase(unique(v.begin(), v.end()), v.end());   }   long long rand_int(long long l, long long r) {     static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());     return uniform_int_distribution<long long>(l, r)(gen);   }   template <class T>   void print(T x, int suc = 1) {     cout << x;     if (suc == 1)       cout <<   n ;     else       cout <<    ;   }   template <class T>   void print(const vector<T>& v, int suc = 1) {     for (int i = 0; i < v.size(); i++)       print(v[i], i == (int)(v.size()) - 1 ? suc : 2);   }   const int INF = 0x3f3f3f3f;   const int MAXN = 2e5 + 7;   const int maxn = 2e6 + 7;   int a[MAXN], b[MAXN];   int mx[maxn];   struct e {     int u, v, w;   } Edge[maxn];   bool cmp(e a, e b) { return a.w > b.w; }   int fa[maxn];   int find(int x) { return (x == fa[x] ? x : fa[x] = find(fa[x])); }   int main() {     int n, m, cnt = 0;     cin >> n >> m;     for (int i = 1; i <= n; ++i) cin >> a[i];     for (int i = 1; i <= m; ++i) cin >> b[i];     for (int i = 1; i <= n + m + 1; ++i) fa[i] = i;     long long s = 0;     for (int i = 1; i <= n; ++i) {       int sz;       cin >> sz;       for (int j = 1; j <= sz; ++j) {         int u;         cin >> u;         Edge[++cnt] = {u, i + m, a[i] + b[u]};         s += (a[i] + b[u]);       }     }     long long sum = 0;     sort(Edge + 1, Edge + cnt + 1, cmp);     for (int i = 1; i <= cnt; ++i) {       int u = Edge[i].u, v = Edge[i].v, w = Edge[i].w;       int fu = find(u), fv = find(v);       if (fu == fv) continue;       fa[fu] = fv;       sum += w;     }     cout << s - sum << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int a, t, b, c, m, n, ck = 0, ans = 0, sum = 0;     cin >> t;     cin.ignore();     while (t--) {       ck = 0;       cin >> a;       cin.ignore();       string s;       cin >> s;       b = a - 10;       for (int i = 0; i < b; i++) {         if (s[i] ==  8 ) {           ck = 1;         }       }       if (ck == 1) {         cout <<  YES  << endl;       } else {         cout <<  NO  << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, x;     cin >> n >> x;     if (n == 5 && x == 5) {       printf( >...v nv.<.. n..^.. n>.... n..^.< n1 1 );       return 0;     }     if (n == 3 && x == 2) {       printf( >vv n^<. n^.< n1 3 );       return 0;     }     for (int i = 0; i < 50; i++) {       for (int j = 0; j < 33; j++) printf( > );       for (int j = 0; j < 33; j++) printf( .> );       printf( v n^ );       for (int j = 0; j < 33; j++) printf( <. );       printf( . );       for (int j = 0; j < 32; j++) printf( < );       printf(  n );     }     printf( 1 1 );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   using ll = long long;   template <typename T>   void DEBUG(string label, T value) {     cerr <<  [  << label <<   =   << value <<  ] n ;   }   const int oo = 1 << 30;   void solve() {     int n;     cin >> n;     int bsize;     cin >> bsize;     vector<int> arr(n + 2);     for (int i = 1; i <= n; ++i) {       cin >> arr[i];       arr[i] -= i;     }     arr[0] = -oo;     arr[n + 1] = oo;     vector<int> b(bsize + 2);     for (int i = 1; i <= bsize; ++i) {       cin >> b[i];     }     b[0] = 0;     b[bsize + 1] = n + 1;     bsize += 2;     n += 2;     int ans = 0;     for (int i = 0; i < bsize - 1; ++i) {       if (arr[b[i]] > arr[b[i + 1]]) {         cout << -1 <<   n ;         return;       }       vector<int> dp;       for (int j = b[i] + 1; j < b[i + 1]; ++j) {         auto u = upper_bound(dp.begin(), dp.end(), arr[j]);         if (arr[j] >= arr[b[i]] && arr[j] <= arr[b[i + 1]]) {           if (u == dp.end()) {             dp.push_back(arr[j]);           } else {             *u = arr[j];           }         }       }       ans += b[i + 1] - b[i] - 1 - (int)dp.size();     }     cout << ans <<   n ;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     int t = 1;     while (t--) solve();   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 1e5 + 5;   int n, k, sz, trie[MAX][26];   bool win[MAX], lose[MAX];   char s[MAX];   void dfs(int u) {     int ady = 0;     for (int i = 0; i < 26; i++) {       if (trie[u][i] == 0) continue;       ady++;       int v = trie[u][i];       dfs(v);       win[u] |= !win[v];       lose[u] |= !lose[v];     }     if (ady == 0) {       win[u] = 0;       lose[u] = 1;     }   }   int main() {     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; i++) {       scanf( %s , s);       int l = strlen(s);       int r = 0;       for (int i = 0; i < l; i++) {         int c = s[i] -  a ;         if (trie[r][c] == 0) trie[r][c] = ++sz;         r = trie[r][c];       }     }     dfs(0);     if (win[0] && lose[0])       printf( First n );     else if (win[0] && !lose[0]) {       if (k & 1)         printf( First n );       else         printf( Second n );     } else if (!win[0] && lose[0])       printf( Second n );     else       printf( Second n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100004;   int v[N];   int main(void) {     priority_queue<int> q;     int x, n;     cin >> n;     int res = 0;     set<pair<int, int> > S;     for (int i = 0; i < n; ++i) {       int x;       cin >> x;       S.insert(make_pair(x, i + 1));     }     int k = 1;     for (auto it : S) {       v[it.second] = k++;     }     for (int i = 1; i <= n; ++i) {       x = v[i];       q.push(x);       if (q.top() == i) {         res++;         while (q.size()) q.pop();       }     }     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, k;   vector<int> ini[100005];   vector<int> mat[100005];   int maior[100005];   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cin >> n >> m;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) {         int a;         cin >> a;         mat[i].push_back(a);         ini[i].push_back(0);         maior[i] = 0x3f3f3f3f;       }     for (int j = 0; j < m; j++)       for (int i = 1; i < n; i++) {         if (mat[i][j] >= mat[i - 1][j]) {           ini[i][j] = ini[i - 1][j];           if (maior[i] > ini[i][j]) maior[i] = ini[i][j];         } else {           ini[i][j] = i;         }       }     cin >> k;     for (int i = 0; i < k; i++) {       int l, r;       cin >> l >> r;       bool flag = false;       if (maior[r - 1] <= l - 1 or l == r)         cout <<  Yes  << endl;       else         cout <<  No  << endl;     }     return 0;   }


#include <bits/stdc++.h>   const int MAXN = 3e5 + 5;   const int SQ = 600;   int n, p, m, w[MAXN];   long long sum[MAXN], ans[MAXN];   std::vector<int> vec[SQ];   struct Ques {     int a, b;   } ques[MAXN];   void ss(int x) {     int step = ques[x].b;     if (step > m) {       long long res = 0;       for (int i = ques[x].a; i <= n; i += step) res += w[i];       ans[x] = res;     } else {       vec[step].push_back(x);     }   }   void solve() {     scanf( %d , &n);     m = sqrt(n);     for (int i = 1; i <= n; i++) scanf( %d , w + i);     scanf( %d , &p);     for (int i = 1; i <= p; i++) scanf( %d%d , &ques[i].a, &ques[i].b);     for (int i = 1; i <= p; i++) ss(i);     for (int i = 1; i <= m; i++) {       memset(sum, 0, sizeof(sum));       for (int j = n; j >= n - i + 1; j--) sum[j] = w[j];       for (int j = n - i; j >= 1; j--) sum[j] = w[j] + sum[j + i];       for (int x : vec[i]) ans[x] = sum[ques[x].a];     }     for (int i = 1; i <= p; i++) printf( %I64d n , ans[i]);   }   int main() {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   const int N = 1e6, inf = 1e9, mod = 1000000007;   vector<pair<int, int> > ad[N + 2], tree[N + 2];   int vis[N + 2], lev[N + 2];   void dfs(int node, int par, int lv) {     vis[node] = 1, lev[node] = lv;     for (auto x : ad[node]) {       if (x.first == par) continue;       if (vis[x.first]) {       } else {         tree[node].push_back(x);         tree[x.first].push_back({node, x.second});         dfs(x.first, node, lv + 1);       }     }   }   int a[N + 2], cnt[N + 2];   void dds(int nd, int pr, int id) {     for (auto x : tree[nd]) {       if (x.first == pr) continue;       dds(x.first, nd, x.second);     }     if (pr == -1) return;     if (a[nd] == 1) {       cnt[id] ^= 1;       a[nd] = 0;       if (a[pr] != -1) a[pr] ^= 1;     }   }   int f;   int vs[N + 2];   void dfs(int nd) {     if (a[nd] == 1) {       a[nd] = 0;       f = 1;       return;     } else if (a[nd] == -1) {       f = 1;       return;     }     vs[nd] = 1;     for (int i = 0; i < (int)tree[nd].size(); i++) {       if (vs[tree[nd][i].first]) continue;       cnt[tree[nd][i].second] ^= 1;       dfs(tree[nd][i].first);       if (f) break;       cnt[tree[nd][i].second] ^= 1;     }     vs[nd] = 0;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n, m;     cin >> n >> m;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 1; i <= m; i++) {       int u, v;       cin >> u >> v;       ad[u].push_back({v, i});       ad[v].push_back({u, i});     }     dfs(1, -1, 0);     dds(1, -1, 0);     int ans = 0;     f = 1;     if (a[1] == 1) {       f = 0;       a[1] = 0;       dfs(1);     }     if (f == 0) cout << -1 << endl, exit(0);     for (int i = 1; i <= m; i++) ans += cnt[i];     cout << ans << endl;     for (int i = 1; i <= m; i++)       if (cnt[i]) cout << i <<    ;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     int l = s.length();     vector<char> s1;     for (int i = 0; i < l; i++) {       s1.push_back(s[i]);       int f = 0;       while (s[i] ==  /  && i < l) {         i++;         f = 1;       }       if (f == 1) i--;     }     if (s1[s1.size() - 1] ==  /  && s1.size() != 1) s1.pop_back();     for (int i = 0; i < s1.size(); i++) cout << s1[i];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, k, x[100039], y[100039], z, now, cur, flag[100039];   long long d[100039], ans = 1e15;   struct yyy {     int to;     long long w;     int z;   } tmps;   struct ljb {     int head, h[100039];     yyy f[600039];     inline void add(int x, int y, long long z) {       f[++head] = (yyy){y, z, h[x]};       h[x] = head;     }   } s;   struct tree {     int x, y, num;   } f[100039];   inline bool cmp1(tree x, tree y) { return x.x < y.x; }   inline bool cmp2(tree x, tree y) { return x.y < y.y; }   inline bool cmp3(tree x, tree y) { return x.num < y.num; }   inline int calc(int a, int b) {     return ((abs(x[a] - x[b])) < (abs(y[a] - y[b])) ? (abs(x[a] - x[b]))                                                     : (abs(y[a] - y[b])));   }   inline int calc1(int a, int b) { return abs(x[a] - x[b]) + abs(y[a] - y[b]); }   struct ques {     int to;     long long w;     bool operator<(const ques &x) const { return w > x.w; }   } tmp;   priority_queue<ques> q;   int main() {     memset(d, 0x3f, sizeof(d));     memset(s.h, -1, sizeof(s.h));     register int i;     scanf( %d%d , &n, &n);     scanf( %d%d%d%d , &x[0], &y[0], &x[n + 1], &y[n + 1]);     ans = abs(x[0] - x[n + 1]) + abs(y[0] - y[n + 1]);     for (i = 1; i <= n; i++)       scanf( %d%d , &f[i].x, &f[i].y), x[i] = f[i].x, y[i] = f[i].y, f[i].num = i;     sort(f + 1, f + n + 1, cmp1);     for (i = 2; i <= n; i++)       s.add(f[i - 1].num, f[i].num, calc(f[i - 1].num, f[i].num)),           s.add(f[i].num, f[i - 1].num, calc(f[i - 1].num, f[i].num));     sort(f + 1, f + n + 1, cmp2);     for (i = 2; i <= n; i++)       s.add(f[i - 1].num, f[i].num, calc(f[i - 1].num, f[i].num)),           s.add(f[i].num, f[i - 1].num, calc(f[i - 1].num, f[i].num));     for (i = 1; i <= n; i++) s.add(0, i, calc(0, i));     for (i = 1; i <= n; i++) s.add(i, n + 1, calc1(i, n + 1));     q.push((ques){0, 0});     d[0] = 0;     while (!q.empty()) {       tmp = q.top();       q.pop();       cur = s.h[tmp.to];       while (~cur) {         tmps = s.f[cur];         if (d[tmps.to] > d[tmp.to] + tmps.w)           d[tmps.to] = d[tmp.to] + tmps.w, q.push((ques){tmps.to, d[tmps.to]});         cur = tmps.z;       }     }     printf( %lld n , ((d[n + 1]) < (ans) ? (d[n + 1]) : (ans)));   }


#include <bits/stdc++.h>   using namespace std;   template <typename T, typename U>   inline void smin(T &a, U b) {     if (a > b) a = b;   }   template <typename T, typename U>   inline void smax(T &a, U b) {     if (a < b) a = b;   }   template <class T>   inline void gn(T &first) {     char c, sg = 0;     while (c = getchar(), (c >  9  || c <  0 ) && c !=  - )       ;     for ((c ==  -  ? sg = 1, c = getchar() : 0), first = 0; c >=  0  && c <=  9 ;          c = getchar())       first = (first << 1) + (first << 3) + c -  0 ;     if (sg) first = -first;   }   template <class T, class T1>   inline void gn(T &first, T1 &second) {     gn(first);     gn(second);   }   template <class T, class T1, class T2>   inline void gn(T &first, T1 &second, T2 &z) {     gn(first);     gn(second);     gn(z);   }   template <class T>   inline void print(T first) {     if (first < 0) {       putchar( - );       return print(-first);     }     if (first < 10) {       putchar( 0  + first);       return;     }     print(first / 10);     putchar(first % 10 +  0 );   }   template <class T>   inline void printsp(T first) {     print(first);     putchar(   );   }   template <class T>   inline void println(T first) {     print(first);     putchar(  n );   }   template <class T, class U>   inline void print(T first, U second) {     printsp(first);     println(second);   }   template <class T, class U, class V>   inline void print(T first, U second, V z) {     printsp(first);     printsp(second);     println(z);   }   int power(int a, int b, int m, int ans = 1) {     for (; b; b >>= 1, a = 1LL * a * a % m)       if (b & 1) ans = 1LL * ans * a % m;     return ans;   }   int V, E, src, tar;   int head[60000], work[60000], dis[60000];   int to[2010000], cap[2010000], nxt[2010000];   int q[60000], qf, qb;   int n, m, males, females;   void init(int n, int s, int t) {     V = n;     E = 0;     src = s;     tar = t;     memset(head, -1, sizeof(int) * V);   }   void add_edge(int u, int v, int c) {     to[E] = v;     cap[E] = c;     nxt[E] = head[u];     head[u] = E++;     to[E] = u;     cap[E] = 0;     nxt[E] = head[v];     head[v] = E++;   }   bool bfs() {     memset(dis, -1, sizeof(int) * V);     qf = qb = 0;     q[qb++] = src;     dis[src] = 0;     while (qf < qb && dis[tar] == -1) {       int u = q[qf++];       for (int i = head[u]; i >= 0; i = nxt[i]) {         int v = to[i];         if (dis[v] == -1 && cap[i] > 0) {           dis[v] = dis[u] + 1;           q[qb++] = v;         }       }     }     return dis[tar] >= 0;   }   int dfs(int u, int &bot) {     int v, bot1, delta;     if (u == tar) return bot;     for (int &i = work[u]; i >= 0; i = nxt[i]) {       int v = to[i];       if (dis[v] != dis[u] + 1 || cap[i] == 0) continue;       bot1 = min(bot, cap[i]);       if (delta = dfs(v, bot1)) {         cap[i] -= delta;         cap[i ^ 1] += delta;         bot = bot1;         return delta;       }     }     return 0;   }   int dinic() {     int ans = 0, delta, bot;     while (bfs()) {       memcpy(work, head, sizeof(int) * V);       delta = 0x3f3f3f3f;       while (delta = dfs(src, bot = 0x3f3f3f3f)) ans += delta;     }     return ans;   }   void NoSolution() {     puts( -1 );     exit(0);   }   char grid[33][33];   int dst[33][33][33][33];   int dx[] = {0, 0, -1, 1};   int dy[] = {-1, 1, 0, 0};   int mx[33 * 33], my[33 * 33], mt[33 * 33];   int fx[33 * 33], fy[33 * 33], ft[33 * 33];   int can(long long mid) {     init(males + females + n * m * 2 + 10, 0, males + females + n * m * 2 + 1);     for (int i = 0; i < males; i++) {       add_edge(src, i + 1, 1);       for (int j = 1; j <= n; j++)         for (int k = 1; k <= m; k++) {           if (dst[mx[i]][my[i]][j][k] == 0x3f3f3f3f) continue;           if ((long long)dst[mx[i]][my[i]][j][k] * mt[i] > mid) continue;           add_edge(i + 1, males + (j - 1) * m + k, 1);         }     }     for (int i = 0; i < females; i++) {       add_edge(males + n * m * 2 + i + 1, tar, 1);       for (int j = 1; j <= n; j++)         for (int k = 1; k <= m; k++) {           if (dst[fx[i]][fy[i]][j][k] == 0x3f3f3f3f) continue;           if ((long long)dst[fx[i]][fy[i]][j][k] * ft[i] > mid) continue;           add_edge(males + (j - 1) * m + k + n * m, males + n * m * 2 + i + 1, 1);         }     }     for (int j = 1; j <= n; j++)       for (int k = 1; k <= m; k++)         add_edge(males + (j - 1) * m + k, males + (j - 1) * m + k + n * m, 1);     return dinic() == males;   }   int main() {     gn(n, m);     gn(males, females);     if (abs(males - females) != 1) NoSolution();     for (int i = 1; i <= n; i++) scanf( %s , grid[i] + 1);     int r, c, t;     gn(r, c, t);     memset(dst, 0x3f, sizeof dst);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++)         if (grid[i][j] ==  . ) {           dst[i][j][i][j] = 0;           qf = qb = 0;           q[qb++] = i;           q[qb++] = j;           while (qf < qb) {             int u = q[qf++], v = q[qf++];             for (int k = 0; k < 4; k++) {               int uu = u + dx[k], vv = v + dy[k];               if ((uu < 1 || vv < 1 || uu > n || vv > m) || grid[uu][vv] ==  # )                 continue;               if (dst[i][j][uu][vv] > dst[i][j][u][v] + 1) {                 dst[i][j][uu][vv] = dst[i][j][u][v] + 1;                 q[qb++] = uu;                 q[qb++] = vv;               }             }           }         }     }     for (int i = 0; i < males; i++) gn(mx[i], my[i], mt[i]);     for (int i = 0; i < females; i++) gn(fx[i], fy[i], ft[i]);     if (males == females + 1) {       fx[females] = r;       fy[females] = c;       ft[females] = t;       females++;     } else if (males + 1 == females) {       mx[males] = r;       my[males] = c;       mt[males] = t;       males++;     }     assert(males == females);     long long st = -1, ed = (long long)1e16;     while (ed - st > 1) {       long long mid = st + ed >> 1;       if (can(mid))         ed = mid;       else         st = mid;     }     if (ed == (long long)1e16) NoSolution();     println(ed);     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   #pragma GCC optimize( Ofast )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )   using namespace std;   string a, b;   signed main() {     ios_base::sync_with_stdio(0);     cin.tie();     cout.tie();     cin >> a;     b += a;     reverse(a.begin(), a.end());     b += a;     cout << b;   }


#include <bits/stdc++.h>   #pragma GCC optimize(2)   using namespace std;   long long n, m, a[200010], dp[200010][2];   vector<pair<long long, long long> > allseg, okseg;   vector<long long> hvseg[200010];   void solve() {     long long i, j, l, r, mn, dx, dy;     scanf( %lld%lld , &n, &m);     a[0] = -1000000000000;     a[n + 1] = 1000000000000;     hvseg[0].clear();     for (i = 1; i <= n; i++) {       hvseg[i].clear();       scanf( %lld , &a[i]);     }     hvseg[n + 1].clear();     sort(a, a + n + 2);     allseg.clear();     for (i = 0; i < m; i++) {       scanf( %lld%lld , &l, &r);       allseg.push_back(make_pair(l, r));     }     sort(allseg.begin(), allseg.end());     okseg.clear();     mn = 1000000000000;     for (i = allseg.size() - 1; i >= 0; i--) {       if (allseg[i].second >= mn) {         continue;       }       mn = min(mn, allseg[i].second);       l = lower_bound(a, a + n + 2, allseg[i].first) - a;       r = upper_bound(a, a + n + 2, allseg[i].second) - a - 1;       if (l <= r) {         continue;       }       hvseg[r].push_back(okseg.size());       okseg.push_back(allseg[i]);     }     reverse(okseg.begin(), okseg.end());     for (i = 0; i <= n + 1; i++) {       for (j = 0; j < hvseg[i].size(); j++) {         hvseg[i][j] = okseg.size() - hvseg[i][j] - 1;       }       reverse(hvseg[i].begin(), hvseg[i].end());     }     for (i = 0; i <= n + 1; i++) {       dp[i][0] = dp[i][1] = 1000000000000;     }     dp[0][0] = dp[0][1] = 0;     for (i = 1; i <= n + 1; i++) {       for (dx = 0; dx < 2; dx++) {         if (dp[i - 1][dx] >= 1000000000000) {           continue;         }         for (dy = 0; dy < 2; dy++) {           if (hvseg[i - 1].size() == 0) {             dp[i][dy] = min(dp[i][dy], dp[i - 1][dx]);             continue;           }           dp[i][dy] = min(               dp[i][dy],               dp[i - 1][dx] +                   (dx + 1) * (okseg[hvseg[i - 1][hvseg[i - 1].size() - 1]].first -                               a[i - 1]));           dp[i][dy] = min(               dp[i][dy],               dp[i - 1][dx] + (2 - dy) * (a[i] - okseg[hvseg[i - 1][0]].second));           for (j = 0; j + 1 < hvseg[i - 1].size(); j++) {             dp[i][dy] =                 min(dp[i][dy],                     dp[i - 1][dx] +                         (dx + 1) * (okseg[hvseg[i - 1][j]].first - a[i - 1]) +                         (2 - dy) * (a[i] - okseg[hvseg[i - 1][j + 1]].second));           }         }       }     }     printf( %lld n , min(dp[n + 1][0], dp[n + 1][1]));     return;   }   int main() {     long long T;     scanf( %lld , &T);     while (T--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 53;   const int maxm = 23;   int n, m;   char s[maxn][maxm];   double dp[1 << 20];   long long f[1 << 20];   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %s , s[i]);     m = strlen(s[1]);     memset(f, 0, sizeof(f));     for (int i = 0; i < n; i++) {       for (int j = i + 1; j < n; j++) {         int same = 0;         for (int k = 0; k < m; k++) {           if (s[i][k] == s[j][k]) same |= (1 << k);         }         f[same] |= (1LL << j) | (1LL << i);       }     }     for (int same = (1 << m) - 1; same >= 0; same--) {       for (int i = 0; i < m; i++) {         if (same & (1 << i)) f[same ^ (1 << i)] |= f[same];       }     }     double ans = 0;     memset(dp, 0, sizeof(dp));     dp[0] = 1;     for (int sta = 0; sta < (1 << m); sta++) {       int cnt = 0;       for (int i = 0; i < m; i++)         if (sta & (1 << i)) cnt++;       for (int i = 0; i < m; i++) {         if (sta & (1 << i)) continue;         dp[sta | (1 << i)] += dp[sta] / (m - cnt);       }       for (int i = 0; i < n; i++) {         if (f[sta] & (1LL << i)) ans += dp[sta];       }     }     printf( %.10f n , ans / n);   }


#include <bits/stdc++.h>   using namespace std;   int n;   int x[20005], y[20005], c[20005], ans[20005], LOC;   bool cmp(const int &u, const int &v) {     return (x[u] - x[LOC]) * (y[v] - y[LOC]) - (y[u] - y[LOC]) * (x[v] - x[LOC]) <            0;   }   void solve(int l, int r) {     if (l > r) return;     int loc = l;     for (int i = (l), LIM = (r); i <= LIM; i++)       if (x[c[i]] < x[c[loc]]) loc = i;     swap(c[loc], c[l]);     LOC = c[l];     sort(c + l + 1, c + r + 1, cmp);     int s0 = 0, s1 = 0, k = -100000;     for (int i = (l + 1), LIM = (r); i <= LIM; i++) {       if (s0 == s1 && ((c[i] <= n) ^ (c[l] <= n))) {         if (k == -100000 || max(k - l - 1, r - k) > max(i - l - 1, r - i)) k = i;       }       if (c[i] <= n)         s0++;       else         s1++;     }     ans[c[l]] = c[k], ans[c[k]] = c[l];     solve(l + 1, k - 1), solve(k + 1, r);   }   int main() {     scanf( %d , &n);     for (int i = (1), LIM = (2 * n); i <= LIM; i++)       scanf( %d%d , &x[i], &y[i]), c[i] = i;     solve(1, 2 * n);     for (int i = (1), LIM = (n); i <= LIM; i++) printf( %d n , ans[i] - n);   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 10;   const int MOD = 1e9 + 7;   int m, d, L;   int bit[2001];   long long dp[2001][2001];   void add(long long &x, long long y) { x = (x + y) % MOD; }   long long DFS(int pos, int preyu, bool yes) {     if (pos == L) return preyu == 0;     if (!yes && dp[pos][preyu] != -1) return dp[pos][preyu];     long long ans = 0;     int End = yes ? bit[pos] : 9;     for (int i = 0; i <= End; i++) {       if (pos % 2 == 0 && i == d) continue;       if (pos % 2 == 1 && i != d) continue;       add(ans, DFS(pos + 1, (preyu * 10 + i) % m, yes && i == End));     }     if (!yes) dp[pos][preyu] = ans;     return ans;   }   long long Count(string str) {     int len = str.size();     int cnt = 0;     for (int i = 0; i < len; i++) bit[i] = str[i] -  0 ;     return DFS(0, 0, 1);   }   bool judge(string str) {     int len = str.size();     int yu = 0;     for (int i = 0; i < len; i++) {       int v = str[i] -  0 ;       yu = yu * 10 + v;       yu %= m;       if ((i + 1) & 1 && v == d) return false;       if (i & 1 && v != d) return false;     }     return yu == 0;   }   int main() {     memset(dp, (-1), sizeof(dp));     cin >> m >> d;     string a, b;     cin >> a >> b;     L = a.size();     cout << (Count(b) - Count(a) + judge(a) + MOD) % MOD << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using vi = vector<int>;   using si = unordered_set<int>;   using vsi = vector<si>;   using vvi = vector<vi>;   using qi = queue<int>;   int n, k, r, s = -1;   vi d, depth;   vsi h;   vvi g;   bool dfs(int u, int p) {     if (p == -1 && (d[u] != 2 && d[u] != 3)) return 0;     if (p == -1 && d[u] == 3) {       if (r) return 0;       r = u + 1;       int c = 0;       for (int v : g[u]) {         if (depth[v] == depth[u] - 1)           s = v + 1;         else if (depth[v] == depth[u] - 2)           c++;       }       if (c != 2 || s == -1) return 0;     }     if (p >= 0 && d[u] == 4 && depth[u] > 1) {       if (r) return 0;       r = u + 1;     }     if (p >= 0 && d[u] == 2 && depth[u] > 1) return 0;     if (p >= 0 && d[u] == 2 && depth[u] == 1) {       if (r) return 0;       r = u + 1;     }     bool ok = 1;     for (int v : g[u])       if (v != p) {         if (r != u + 1 && depth[v] + 1 != depth[u]) ok = 0;         if (!dfs(v, u)) ok = 0;       }     return ok;   }   int main() {     int n, u, v;     cin >> n;     if (n == 2) {       cout <<  2 n1 2 n ;       return 0;     }     int k = 1;     for (int i = 0; i < n; i++) k *= 2;     k -= 2;     g = vvi(k), h = vsi(k);     for (int i = 0; i < k - 1; i++) {       cin >> u >> v;       u--, v--;       g[u].push_back(v);       g[v].push_back(u);       h[u].insert(v);       h[v].insert(u);     }     vi c(5);     d = vi(k), depth = vi(k);     qi q;     for (int i = 0; i < k; i++) {       d[i] = g[i].size();       if (d[i] >= 1 && d[i] <= 4)         c[d[i]]++;       else         c[0]++;       if (d[i] == 1) {         q.push(i);         depth[i] = 0;       }     }     int l = (k + 2) / 2;     if (c[0] || c[4] > 1 || (c[1] != l && c[1] != l - 1)) {       cout <<  0 n ;       return 0;     }     vi p(k, -1);     while (!q.empty()) {       int u = q.front();       q.pop();       if (h[u].empty()) continue;       int v = *h[u].begin();       p[u] = v;       depth[v] = max(depth[u], depth[u] + 1);       h[v].erase(u);       if (h[v].size() == 1) q.push(v);     }     int root = -1;     for (int i = 0; i < k; i++)       if (p[i] == -1) {         if (root == -1)           root = i;         else {           cout <<  0 n ;           return 0;         }       }     if (root == -1) {       cout <<  0 n ;       return 0;     }     if (dfs(root, -1)) {       if (s >= 0)         cout <<  2 n  << min(r, s) <<     << max(r, s) <<   n ;       else         cout <<  1 n  << r <<   n ;     } else       cout <<  0 n ;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 998244353;   const long long N = 4e5;   long long fac[N];   long long qpow(long long x, long long y) {     long long res = 1;     for (; y; y >>= 1, x = x * x % mod)       if (y & 1) res = res * x % mod;     return res;   }   long long inv(long long x) { return qpow(x, mod - 2); }   long long C(long long x, long long y) {     return ((fac[x] * inv(fac[y]) % mod) * inv(fac[x - y])) % mod;   }   void build() {     fac[0] = 1;     for (long long i = 1; i < N; i++) fac[i] = fac[i - 1] * i % mod;   }   long long a[N];   signed main() {     ios::sync_with_stdio(0);     cin.tie(0);     build();     long long n;     cin >> n;     for (long long i = 0; i < 2 * n; i++) cin >> a[i];     sort(a, a + 2 * n);     for (long long i = 0; i < 2 * n; i++) a[i] %= mod;     long long ans = 0;     for (long long i = 0; i < n; i++) ans = (ans + a[i + n] - a[i] + mod) % mod;     cout << ans * C(2 * n, n) % mod <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int used[1123], used1[1123];   char a[1123][1123];   int main() {     int n, m, i, j;     bool flag = 0;     cin >> n >> m;     for (i = 1; i <= n; i++) {       for (j = 1; j <= m; j++) {         cin >> a[i][j];       }     }     int ans = 0;     for (j = 1; j <= m; j++) {       flag = 0;       for (i = 2; i <= n; i++) {         if (used[i] == 0) {           if (a[i][j] < a[i - 1][j]) {             ans++;             flag = 1;             break;           } else if (a[i][j] > a[i - 1][j]) {             used1[i] = 1;           }         } else {           used1[i] = 1;         }       }       if (flag == 0) {         for (i = 2; i <= n; i++) {           used[i] = used1[i];         }       }       for (i = 2; i <= n; i++) {         used1[i] = 0;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long> bitr;   void update(long long ind, long long val, long long n) {     long long i = ind + 1;     while (i <= n) {       bitr[i] += val;       i += i & (-i);     }   }   long long sum(long long ind) {     long long i = ind + 1;     long long ans = 0;     while (i > 0) {       ans += bitr[i];       i -= i & (-i);     }     return ans;   }   int main() {     long long n, q, i;     cin >> n >> q;     long long arr[n];     bitr.push_back(0);     for (i = 0; i < n; i++) {       cin >> arr[i];       bitr.push_back(0);     }     sort(arr, arr + n);     for (i = 0; i < q; i++) {       long long l, r;       cin >> l >> r;       l--;       r--;       update(l, 1, n);       update(r + 1, -1, n);     }     long long fact[n];     for (i = 0; i < n; i++) fact[i] = sum(i);     sort(fact, fact + n);     long long res = 0;     for (i = 0; i < n; i++) {       res += (fact[i] * arr[i]);     }     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 1000000007;   const long long linf = 1ll * inf * inf;   const int N = 100000 + 7;   const int multipleTest = 0;   vector<pair<int, int> > adj[N];   int pa[N][20];   int depth[N];   int g[N];   int root[N];   int k = 0;   int n;   int low[N];   int high[N];   int rr = 0;   int curRoot;   void dfs(int u, int r) {     pa[u][0] = r;     low[u] = ++rr;     for (int j = 1; j < 20; ++j) {       if (pa[u][j - 1] == -1)         pa[u][j] = -1;       else         pa[u][j] = pa[pa[u][j - 1]][j - 1];     }     root[u] = curRoot;     for (pair<int, int> x : adj[u]) {       int v = x.first;       int c = x.second;       if (v != r) {         depth[v] = depth[u] + 1;         g[v] = g[u] + c;         dfs(v, u);       }     }     high[u] = rr;   }   bool isPa(int r, int u) { return low[r] <= low[u] && high[r] >= high[u]; }   int LCA(int u, int v) {     if (depth[u] < depth[v]) swap(u, v);     while (depth[u] > depth[v]) {       int k = log2(depth[u] - depth[v]);       u = pa[u][k];     }     if (u == v) return u;     for (int j = 19; j >= 0; --j) {       if (pa[u][j] != -1 && pa[v][j] != pa[u][j]) {         u = pa[u][j];         v = pa[v][j];       }     }     return pa[u][0];   }   void solve() {     cin >> n;     for (int i = (1), _b = (n + 1); i < _b; ++i) {       int u, c;       scanf( %d%d , &u, &c);       if (u != -1)         adj[u].push_back(make_pair(i, c));       else         pa[i][0] = -1;     }     for (int i = (1), _b = (n + 1); i < _b; ++i)       if (pa[i][0] == -1) {         curRoot = i;         dfs(i, -1);       }     int q;     cin >> q;     while (q-- > 0) {       int op, u, v;       scanf( %d%d%d , &op, &u, &v);       if (root[u] != root[v] || u == v) {         puts( NO );         continue;       }       if (op == 1) {         if (isPa(u, v) && g[v] - g[u] == 0)           puts( YES );         else           puts( NO );       } else {         int r = LCA(u, v);         if (r == u) {           if (g[v] - g[u] == depth[v] - depth[u])             puts( YES );           else             puts( NO );         } else if (r == v) {           puts( NO );         } else {           if (g[u] - g[r] == 0 && g[v] - g[r] == depth[v] - depth[r])             puts( YES );           else             puts( NO );         }       }     }   }   int main() {     int Test = 1;     if (multipleTest) {       cin >> Test;     }     for (int i = 0; i < Test; ++i) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   int main(void) {     int n, m;     cin >> n >> m;     vector<vector<int> > file(n);     for (int i = (0); i < ((m)); ++i) {       int tmp;       cin >> tmp;       vector<int> f(tmp);       for (int j = (0); j < ((tmp)); ++j) cin >> f[j];       file.push_back(f);     }     vector<int> v;     for (int i = (0); i < ((file.size())); ++i)       for (int j = (0); j < ((file[i].size())); ++j) v.push_back(file[i][j]);     sort((v).begin(), (v).end());     int emp = v.size() + 1;     for (int i = (0); i < ((v.size())); ++i) {       if (i + 1 != v[i]) {         emp = i + 1;         break;       }     }     int index = 1;     vector<pair<int, int> > res;     for (int i = (0); i < ((file.size())); ++i) {       for (int j = (0); j < ((file[i].size())); ++j) {         for (int k = (0); k < ((file.size())); ++k) {           for (int l = (0); l < ((file[k].size())); ++l) {             if (file[k][l] == index) {               if (i != k || j != l) {                 int tmp;                 res.push_back(make_pair(file[k][l], emp));                 res.push_back(make_pair(file[i][j], index));                 tmp = file[k][l];                 file[k][l] = emp;                 emp = file[i][j];                 file[i][j] = tmp;               }               ++index;               goto NEXT;             }           }         }         res.push_back(make_pair(file[i][j], index));         emp = file[i][j];         file[i][j] = index;         ++index;       NEXT:;       }     }     cout << res.size() << endl;     for (int i = (0); i < ((res.size())); ++i)       cout << res[i].first <<     << res[i].second << endl;   }


#include <bits/stdc++.h>   using namespace std;      int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int t;     cin >> t;     while (t--) {       int n, m;       cin >> n >> m;       vector<int> x(n + m - 1);       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) {           int a;           cin >> a;           x[i + j] ^= a;         }       }       bool flag = false;       for (int i = 0; i < n + m - 1; i++) {         if (x[i] != 0) {           flag = true;           break;         }       }       cout << (flag ?  Ashish  :  Jeel ) <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char getOtherValue(char val) {     if (val ==  . ) return  0 ;     if (val ==  1 ) return  0 ;     return  1 ;   }   int main() {     int n, p;     cin >> n >> p;     string str;     cin >> str;     bool ok = false;     for (int i = 0; i < n - p; ++i) {       if (str[i] ==  .  || str[i] != str[i + p]) {         ok = true;         if (str[i] ==  . ) {           str[i] = getOtherValue(str[i + p]);         }         if (str[i + p] ==  . ) {           str[i + p] = getOtherValue(str[i]);         }         break;       }     }     if (ok) {       for (int i = 0; i < n; ++i) {         if (str[i] ==  . ) {           str[i] =  0 ;         }       }       cout << str;       return 0;     }     cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 2000000009;   const int Max = 1000007;   const double PI = acos(-1.0);   double binSearch(double p, double b) {     int low = 0, high = INF, k;     double x;     while (low <= high) {       k = (low + high) / 2;       x = p / (2.0 * k);       if (x < b)         high = k - 1;       else if (x == b)         return x;       else         low = k + 1;     }     x = p / (2 * high);     if (x < b) x = p / (2 * low);     return x;   }   int main() {     int i, j, k, l, temp, t, n, m, caseno = 0, ans;     string str;     double a, b, x;     while (cin >> a >> b) {       if (b > a) {         cout << -1 << endl;         continue;       }       x = binSearch((a + b), b);       printf( %.10lf n , x);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> v[10005];   long long a[100005], dis[10005];   int b[100005], vis[10005];   bool cmp(const int &first, const int &second) { return a[first] < a[second]; }   priority_queue<pair<long long, int> > T, Q;   int main() {     long long h;     int n, m, p;     scanf( %I64d%d%d%d , &h, &n, &m, &p);     for (int i = 0; i < p; i++) dis[i] = h + 1;     dis[1] = 1;     for (int i = 1; i <= n; i++) {       scanf( %I64d%d , &a[i], &b[i]);       if (a[i] % p == 1 % p)         T.push(pair<long long, int>(b[i], -i));       else         v[a[i] % p].push_back(i);     }     for (int i = 0; i < p; i++) sort(v[i].begin(), v[i].end(), cmp);     for (int i = 1, ch; i <= m; i++) {       scanf( %d , &ch);       if (ch == 1) {         long long first;         scanf( %I64d , &first);         for (int j = 0; j < p; j++) Q.push(pair<long long, int>(-dis[j], j));         for (; !Q.empty();) {           pair<long long, int> t = Q.top();           Q.pop();           if (vis[t.second] == i) continue;           vis[t.second] = i;           int q = (t.second + first) % p;           if (first - t.first < dis[q])             dis[q] = first - t.first,             Q.push(pair<long long, int>(t.first - first, q));         }         for (int j = 0; j < p; j++)           while (!v[j].empty() && a[v[j][v[j].size() - 1]] >= dis[j])             T.push(pair<long long, int>(b[v[j][v[j].size() - 1]],                                         -v[j][v[j].size() - 1])),                 v[j].resize(v[j].size() - 1);       }       if (ch == 2) {         int first, second;         scanf( %d%d , &first, &second);         b[first] -= second;       }       if (ch == 3) {         pair<long long, int> t;       retry:         if (!T.empty())           t = T.top(), T.pop();         else           t = pair<long long, int>(0, -n - 1);         if (t.first != b[-t.second]) {           T.push(pair<long long, int>(b[-t.second], t.second));           goto retry;         }         b[-t.second] = 0;         printf( %d n , t.first);       }     }   }


#include <bits/stdc++.h>   using namespace std;   char str[1010000];   int b[1010000];   int a[1010000];   int bb[1010000];   int main() {     int n;     int flag = 1;     scanf( %d , &n);     scanf( %s , str);     for (int i = 1; i <= n; i++) {       scanf( %d , &b[i]);     }     int coutt1 = 0;     for (int i = 0; i <= n - 1; i++) {       if (str[i] ==  R ) {         coutt1++;         a[coutt1] = b[i + 1];       }     }     int coutt2 = 0;     for (int i = 0; i <= n - 1; i++) {       if (str[i] ==  L ) {         coutt2++;         bb[coutt2] = b[i + 1];       }     }     int ans = 1000000005;     int i, j;     for (i = 1, j = 1; i <= coutt1 && j <= coutt2;) {       if (a[i] < bb[j]) {         flag = 0;         ans = min((bb[j] - a[i]) / 2, ans);         i++;       } else {         j++;       }     }     if (flag)       printf( -1 );     else       printf( %d , ans);   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<double, double> > vec;   double x[10], y[10];   int cross(pair<double, double> a, pair<double, double> b,             pair<double, double> c) {     double re = (a.first - c.first) * (b.second - c.second);     re -= (a.second - c.second) * (b.first - c.first);     if (fabs(re) < 1e-8) return 0;     return re > 0 ? 1 : -1;   }   bool work() {     double x0, y0, d, xb, yb, xc, yc, xa, ya, xd, yd;     int i;     x0 = x[1] * 2 - x[2];     y0 = y[1] * 2 - y[2];     d = x0 * (y[1] - y[3]) + x[1] * (y[3] - y0) + x[3] * (y0 - y[1]);     d *= 2;     xb = (x0 * x0 + y0 * y0) * (y[1] - y[3]);     xb += (x[1] * x[1] + y[1] * y[1]) * (y[3] - y0);     xb += (x[3] * x[3] + y[3] * y[3]) * (y0 - y[1]);     xb /= d;     yb = (x0 * x0 + y0 * y0) * (x[3] - x[1]);     yb += (x[1] * x[1] + y[1] * y[1]) * (x0 - x[3]);     yb += (x[3] * x[3] + y[3] * y[3]) * (x[1] - x0);     yb /= d;     xc = 2 * x[1] - xb;     yc = 2 * y[1] - yb;     xa = 2 * x[3] - xb;     ya = 2 * y[3] - yb;     xd = 2 * x[2] - xc;     yd = 2 * y[2] - yc;     vec.clear();     vec.push_back(make_pair(xa, ya));     vec.push_back(make_pair(xd, yd));     vec.push_back(make_pair(xc, yc));     vec.push_back(make_pair(xb, yb));     for (i = 0; i < 4; i++)       if (cross(vec[i], vec[(i + 1) % 4], vec[(i + 2) % 4]) *               cross(vec[i], vec[(i + 1) % 4], vec[(i + 3) % 4]) <=           0)         return false;     printf( YES n );     for (i = 0; i < 4; i++) printf( %.10f %.10f  , vec[i].first, vec[i].second);     printf(  n );     return true;   }   int main() {     int T, i;     scanf( %d , &T);     while (T--) {       for (i = 1; i <= 3; i++) scanf( %lf%lf , &x[i], &y[i]);       if ((x[1] - x[3]) * (y[2] - y[3]) == (y[1] - y[3]) * (x[2] - x[3])) {         printf( NO n n );         continue;       }       if (work()) continue;       swap(x[1], x[2]);       swap(y[1], y[2]);       if (work()) continue;       swap(x[1], x[3]);       swap(y[1], y[3]);       if (work()) continue;       printf( NO n n );     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<int> v(n);     for (int i = 0; i < n; i++) {       cin >> v[i];     }     v.insert(v.begin(), 0);     for (int i = 1; i <= n; i++) {       v[i] += v[i - 1];     }     int ans = 0;     int l, r;     cin >> l >> r;     if (l > r) swap(l, r);     ans = v[r - 1] - v[l - 1];     ans = min(ans, v[n] - ans);     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int MAX = 1e6 + 5;   const int N = 2e5 + 5;   const long long int mod = 1e9 + 7;   const long long int mx = 1e16;   bool comp(const int a, const int b) { return a > b; }   const double PI = 2 * acos(0.0);   long long int fac[N];   void pre() {     fac[0] = 1LL;     for (long long int i = 1; i < N; i++) {       fac[i] = 1LL * i * fac[i - 1];       if (fac[i] >= mod) fac[i] %= mod;     }   }   long long int power(long long int a, long long int b) {     long long int x = 1;     while (b) {       if (b & 1) {         x *= a;         if (x >= mod) x %= mod;       }       a *= a;       if (a >= mod) a %= mod;       b >>= 1;     }     return x;   }   long long int inv(long long int k) { return power(k, mod - 2); }   int main() {     vector<int> v1, v2;     int x, t, a, b, da, db;     cin >> x >> t >> a >> b >> da >> db;     int t1 = t;     int t2 = t;     int x1 = x;     int x2 = x;     if (x == 0) {       printf( YES n );       return 0;     }     for (long long int i = 0; i < t; i++) {       if (a - i * da == x || b - i * db == x) {         printf( YES n );         return 0;       }     }     for (long long int i = 0; i < t; i++) {       for (long long int j = 0; j < t; j++) {         int res = a - i * da + b - j * db;         if (res == x) {           printf( YES n );           return 0;         }       }     }     printf( NO n );   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2 * (1e5) + 10, inf = 2147483647;   long long ans;   int n, lastR, lastB, lastP, mxR, mxB;   int main() {     int x;     char c;     scanf( %d , &n);     lastR = lastB = lastP = -inf;     mxR = mxB = 0;     for (int i = 1; i <= n; ++i) {       scanf( %d %c n , &x, &c);       if (c ==  R  || c ==  P ) {         if (lastR != -inf) ans += x - lastR, mxR = max(mxR, x - lastR);         lastR = x;       }       if (c ==  B  || c ==  P ) {         if (lastB != -inf) ans += x - lastB, mxB = max(mxB, x - lastB);         lastB = x;       }       if (c ==  P ) {         if (lastP != -inf) ans += min(0, x - lastP - mxR - mxB);         lastP = x;         mxR = mxB = 0;       }     }     printf( %I64d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   int a[41][1001][1001];   int bir(int n) {     int s = 0;     while (n > 0) {       s += n % 2;       n /= 2;     }     return s;   }   bool fun(int x, int i) {     if (i < 0) return 0;     return ((x >> i) & 1);   }   bool funk(int a, int b, int c, int m) {     for (int i = 0; i < m; i++) {       if (!(fun(a, i) || fun(b, i) || fun(c, i) || fun(b, i - 1) ||             fun(b, i + 1)))         return 1;     }     return 0;   }   int main() {     int n, m, i, j, msk, pmsk, tmsk, ans = 1e9;     cin >> n >> m;     if (n < m) swap(n, m);     for (i = 0; i <= n; i++) {       for (pmsk = 0; pmsk < (1 << m); pmsk++) {         for (msk = 0; msk < (1 << m); msk++) {           a[i][pmsk][msk] = 1e9;         }       }     }     for (i = 0; i < (1 << m); i++) a[0][0][i] = bir(i);     for (i = 1; i <= n; i++) {       for (pmsk = 0; pmsk < (1 << m); pmsk++) {         for (msk = 0; msk < (1 << m); msk++) {           for (tmsk = 0; tmsk < (1 << m); tmsk++) {             if (funk(tmsk, pmsk, msk, m)) continue;             a[i][pmsk][msk] =                 min(a[i][pmsk][msk], a[i - 1][tmsk][pmsk] + bir(msk));           }         }       }     }     for (i = 0; i < (1 << m); i++) {       ans = min(ans, a[n][i][0]);     }     cout << n * m - ans;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     long long k, left;     cin >> k;     long long base = k + 1;     vector<long long> v;     v.push_back(-1);     left = base;     for (long long i = 1; i < 2001; i++) {       left++;       if (left > 1000000) {         v.push_back(1000000);         left -= 1000000;       } else {         v.push_back(left);         break;       }     }     cout << v.size() << endl;     for (long long i = 0; i < v.size(); i++) cout << v[i] <<    ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int i, n, t, p = 0, hPxl, hPxl1, wPxl, Pxl, sum = 0;     scanf( %d , &n);     int h[n + 5], w[n + 5];     for (i = 0; i < n; ++i) {       scanf( %d %d , &w[i], &h[i]);       sum += w[i];     }     hPxl = *max_element(h, h + n);     for (i = 0; i < n; ++i) {       wPxl = sum - w[i];       if (hPxl == h[i] && p == 0) {         t = h[i];         h[i] = 0;         hPxl1 = *max_element(h, h + n);         Pxl = wPxl * hPxl1;         ++p;       } else {         Pxl = wPxl * hPxl;       }       printf( %d  , Pxl);     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9 + 10;   const long long int LINF = 1LL * INF * INF;   const int MAXN = 2e5 + 10;   const int MAXM = 5e3 + 10;   priority_queue<int> pq;   vector<vector<int> > graph;   queue<int> que;   long long int mygcd(long long int a, long long int b) {     return a ? mygcd(b % a, a) : b;   }   int main() {     long long int n, m, k, a, b, x, y, q;     int sum = 0;     int cnt = 0;     int mx = 0;     int mn = INF;     int cur = 0, idx = -1;     int tc;     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n;     long long int res = n;     for (long long int i = 1; i * i <= n; i++) {       if (n % i) continue;       a = mygcd(i, n / i);       if (a == 1) {         res = min(n / i, res);       }     }     cout << n / res <<     << res <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   signed main() {     long long i, j, t, n, m;     cin >> n;     long long a[n], b[n], c[n];     if (n & 1) {       for (long long i = 0; i < n; ++i) {         a[i] = i;         b[i] = i;         c[i] = i + i;         c[i] %= n;       }       for (long long i = 0; i < n; ++i) {         cout << a[i] <<    ;       }       cout << endl;       for (long long i = 0; i < n; ++i) {         cout << b[i] <<    ;       }       cout << endl;       for (long long i = 0; i < n; ++i) {         cout << c[i] <<    ;       }       cout << endl;     } else {       cout <<  -1 ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int SIZE = 100005;   int gap, tmp[SIZE * 2];   class SuffixArray {    public:     int sa[SIZE], rk[SIZE], ht[SIZE];     static bool cmp(int x, int y) {       if (tmp[x] != tmp[y]) return tmp[x] < tmp[y];       return tmp[x + gap] < tmp[y + gap];     }     void gao(const char* s, int n) {       copy(s, s + n + 1, rk);       for (gap = 1; gap <= n; gap <<= 1) {         for (int i = 0; i <= n; i++) sa[i] = i;         copy(rk, rk + n + 1, tmp);         sort(sa, sa + n + 1, cmp);         for (int i = rk[*sa] = 0; i < n; i++)           rk[sa[i + 1]] = rk[sa[i]] + cmp(sa[i], sa[i + 1]);       }       for (int i = gap = ht[*sa] = 0; i < n; i++) {         if (gap) --gap;         while (s[i + gap] == s[sa[rk[i] - 1] + gap]) gap++;         ht[rk[i]] = gap;       }     }   };   char str[SIZE];   SuffixArray SA;   int main() {     int n = strlen(gets(str));     long long ans = n * (n + 1ll) / 2, cnt = 0;     SA.gao(str, n);     vector<int> u = {0};     for (int i = 1; i <= n; i++) {       while (SA.ht[i] < SA.ht[u.back()]) {         cnt -= SA.ht[u.back()] * (u.back() - u[u.size() - 2]);         u.pop_back();       }       u.push_back(i);       cnt += SA.ht[u.back()] * (u.back() - u[u.size() - 2]);       ans += cnt;     }     printf( %I64d n , ans);   }