#include <bits/stdc++.h>   using namespace std;   int md = 1e9 + 7;   int __gcd(int a, int b) {     if (b == 0) return a;     return __gcd(b, a % b);   }   int poww(int a, int b) {     int res = 1;     while (b) {       if (b & 1) {         res *= a;         res %= md;       }       a *= a;       a %= md;       b >>= 1;     }     return res;   }   void ainp(int arr[], int n) {     for (int i = 1; i <= n; i++) cin >> arr[i];   }   const int N = 1e5 + 5;   void fun() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);   }   int n;   int a[N], cache[N][10];   int dp(int idx, int cur) {     if (idx == n) return 1;     int &ans = cache[idx][cur];     if (ans != -1) return ans;     ans = 0;     if (a[idx + 1] > a[idx]) {       for (int i = cur + 1; i <= 5; i++) ans |= dp(idx + 1, i);     } else if (a[idx + 1] == a[idx]) {       for (int i = 1; i <= 5; i++) {         if (i == cur) continue;         ans |= dp(idx + 1, i);       }     } else {       for (int i = 1; i < cur; i++) ans |= dp(idx + 1, i);     }     return ans;   }   void path(int idx, int cur) {     cout << cur <<    ;     if (idx == n) return;     if (a[idx + 1] > a[idx]) {       for (int i = cur + 1; i <= 5; i++)         if (dp(idx + 1, i)) return path(idx + 1, i);     } else if (a[idx + 1] == a[idx]) {       for (int i = 1; i <= 5; i++) {         if (i == cur) continue;         if (dp(idx + 1, i)) return path(idx + 1, i);       }     } else {       for (int i = 1; i < cur; i++)         if (dp(idx + 1, i)) return path(idx + 1, i);     }   }   signed main() {     fun();     memset(cache, -1, sizeof(cache));     cin >> n;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 1; i <= 5; i++) {       if (dp(1, i)) {         path(1, i);         return 0;       }     }     cout <<  -1 ;     return 0;   }


#include <bits/stdc++.h>      using namespace std;      #define ar array   #define ll long long      const int MAX_N = 1e5 + 1;   const int m = 1e9 + 7;   const int INF = 1e9;   const ll LINF = 1e18;      int main()   {    int t=1;    cin>>t;    int n;    while(t--)    {     cin>>n;     int A[n];     for(int i=0;i<n;i++)cin>>A[i];     int x=0,y=n-1;     while(x<=y){      cout<<A[x]<<   ;      if(x!=y)cout<<A[y--]<<   ;      x++;     }     cout<<endl;    }      return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 3e3;   int r, c, n, k, tot, bound[maxn + 2], vis[maxn + 2], suc[maxn + 2],       pre[maxn + 2], rest[maxn + 2];   bool exist[maxn + 2][maxn + 2];   long long ans;   struct vertex {     int x, y;   } a[maxn + 1];   vector<int> t[maxn + 1];   int main() {     scanf( %d%d%d%d , &r, &c, &n, &k);     for (int i = 1; i <= n; ++i) {       scanf( %d%d , &a[i].x, &a[i].y);       exist[a[i].x][a[i].y] = true;     }     for (int i = 1; i <= r; ++i) {       memset(vis, 0, sizeof vis);       memset(suc, 0, sizeof suc);       memset(pre, 0, sizeof pre);       memset(rest, 0, sizeof rest);       tot = 0;       suc[c + 1] = c + 1;       for (int j = 1; j <= c + 1; ++j) bound[j] = c + 1;       for (int j = 1; j <= n; ++j)         if (a[j].x >= i) {           ++vis[a[j].y];           t[a[j].x].push_back(a[j].y);         }       for (int j = 1, l = 0, sum = 0; j <= c; ++j) {         while (l <= c && sum < k) sum += vis[++l];         if (l > c) break;         bound[j] = l;         rest[j] = sum;         sum -= vis[j];       }       for (int j = 1, last = 0; j <= c; ++j) {         if (vis[j]) {           suc[last] = j;           pre[j] = last;           last = j;         }         if (j == c) {           suc[last] = c + 1;           pre[c + 1] = last;         }       }       long long sum = 0;       for (int j = c + 1; j; j = pre[j])         sum += (long long)(c - bound[j] + 1) * (j - pre[j]);       for (int j = r; j >= i; --j) {         ans += sum;         for (int l = 0; l < t[j].size(); ++l) {           int lt = t[j][l];           --vis[lt];           for (int cnt = vis[lt], now = lt; now && bound[now] >= lt;                now = pre[now]) {             --rest[now];             if (rest[now] < k) {               sum -= (long long)(c - bound[now] + 1) * (now - pre[now]);               bound[now] = suc[bound[now]];               rest[now] += vis[bound[now]];               sum += (long long)(c - bound[now] + 1) * (now - pre[now]);             }           }           if (!vis[lt]) {             sum += (long long)(c - bound[suc[lt]] + 1) * (suc[lt] - pre[lt]) -                    (long long)(c - bound[lt] + 1) * (lt - pre[lt]) -                    (long long)(c - bound[suc[lt]] + 1) * (suc[lt] - lt);             pre[suc[lt]] = pre[lt];             suc[pre[lt]] = suc[lt];           }         }         t[j].clear();       }     }     printf( %lld , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n, a, b;       string s =   ;       cin >> n >> a >> b;       s +=  a ;       for (int i = 1; i < b; i++) {         s += s[i - 1] + 1;       }       string s2 = s;       for (int i = 0; i < n / b - 1; i++) {         s += s2;       }       for (int i = 0; i < n % b; i++) {         s += s[i];       }       cout << s << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   set<int> cn[10000];   int nx[10000];   string xm[10000];   int main() {     int m;     cin >> m;     ;     map<string, int> mx;     int n = 0;     for (int i = (0); i < m; i++) {       string a, b;       cin >> a >> b;       if (!mx.count(a)) mx[a] = n, xm[n++] = a;       if (!mx.count(b)) mx[b] = n, xm[n++] = b;       cn[mx[a]].insert(mx[b]);       cn[mx[b]].insert(mx[a]);     }     cout << n << endl;     for (int i = (0); i < n; i++) {       memset(nx, 0, sizeof(nx));       int my = -1, cmy = 0;       for (auto f : cn[i]) {         for (auto fx : cn[f]) {           if (fx == i || cn[fx].count(i)) continue;           nx[fx]++;           if (nx[fx] > my)             my = nx[fx], cmy = 1;           else if (nx[fx] == my)             cmy++;         }       }       if (cmy == 0) {         cmy = n - 1 - cn[i].size();       }       cout << xm[i] <<     << cmy << endl;     }     return 0;   }


#include <bits/stdc++.h>   enum { WHITE, GREY, BLACK };   using namespace std;   long long int inf = 1e18;   template <class T>   T gcd(T a, T b) {     return b == 0 ? a : gcd(b, a % b);   }   template <typename T>   T lcm(T a, T b) {     return a / gcd(a, b) * b;   }   template <typename T>   T last_bit(T n) {     return n & 1;   }   template <class T>   T big_mod(T n, T p, T m) {     if (p == 0) return (T)1;     T x = big_mod(n, p / 2, m);     x = (x * x) % m;     if (p & 1) x = (x * n) % m;     return x;   }   template <class T>   T modInv(T a, T m) {     T x, y;     extgcd(a, m, x, y);     x %= m;     while (x < 0) {       x += m;     }     return x;   }   template <class T>   T extgcd(T a, T b, T& x, T& y) {     if (b == 0) {       x = 1;       y = 0;       return a;     } else {       int g = extgcd(b, a % b, y, x);       y -= a / b * x;       return g;     }   }   template <class T>   T multiplication(T n, T p, T m) {     if (p == 0) return (T)0;     T x = multiplication(n, p / 2, m);     x = (x + x) % m;     if (p & 1) x = (x + n) % m;     return x;   }   template <class T>   T my_pow(T n, T p) {     if (p == 0) return 1;     T x = my_pow(n, p / 2);     x = (x * x);     if (p & 1) x = (x * n);     return x;   }   template <class T>   double getdist(T a, T b) {     return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));   }   template <class T>   T extract(string s, T ret) {     stringstream ss(s);     ss >> ret;     return ret;   }   template <class T>   string tostring(T n) {     stringstream ss;     ss << n;     return ss.str();   }   template <class T>   T Mod(T n, T m) {     return (n % m + m) % m;   }   template <class T>   T MIN3(T a, T b, T c) {     return min(a, min(b, c));   }   template <class T>   T MAX3(T a, T b, T c) {     return max(a, max(b, c));   }   template <class T>   void print_vector(T& v) {     int sz = v.size();     if (sz) cout << v[0];     for (int i = 1; i < sz; i++) cout <<     << v[i];     cout <<   n ;   }   bool isVowel(char ch) {     ch = toupper(ch);     if (ch ==  A  || ch ==  U  || ch ==  I  || ch ==  O  || ch ==  E )       return true;     return false;   }   bool isConsonant(char ch) {     if (isalpha(ch) && !isVowel(ch)) return true;     return false;   }   template <class R>   R Josephus(R n, R k) {     R ans = 1;     for (R i = 2; i <= n; i++) ans = (ans + k - 1) % i + 1;     return ans;   }   template <class R>   R toitent_Phi2(R a) {     R result = a;     for (R i = 2; i * i <= a; i++) {       if (a % i == 0) result = result - result / i;       while (a % i == 0) a = a / i;     }     if (a > 1) result = result - result / a;     return result;   }   template <typename T>   T Angle(T x1, T y1, T x2, T y2) {     return atan(double(y1 - y2) / double(x1 - x2));   }   long long int a[102];   int main() {     cin.sync_with_stdio(false);     cin.tie();     ;     long long int T, t;     long long int i, j, k;     long long int n, m, q;     scanf( %lld , &n);     for (i = 1; i <= 2 * n; i++) {       scanf( %lld , &a[i]);     }     sort(a + 1, a + 1 + 2 * n);     long long int f = 1;     j = n;     for (i = 2 * n; i > n; i--) {       for (j = n; j >= 1; j--) {         if (a[i] <= a[j]) f = 0;       }     }     if (f)       printf( YES n );     else       printf( NO n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, k, ans, s, e, in[100020], pre[100020], vis[100020], ff[100020];   long long len[100020];   struct node {     int v, f;     long long w;   };   vector<node> adj[100020];   bool operator<(const node &x, const node &y) {     if (x.w > y.w) return true;     return false;   }   int dijk(int s) {     priority_queue<node> q;     node now, newnode;     now.w = 0;     now.v = s;     q.push(now);     int i, u, v;     memset(ff, 0, sizeof(ff));     memset(vis, 0, sizeof(vis));     memset(len, 123, sizeof(len));     len[s] = 0;     int cnt = 0;     while (!q.empty()) {       if (cnt == n) break;       u = q.top().v;       q.pop();       if (vis[u]) continue;       vis[u] = 1;       for (i = 0; i < adj[u].size(); i++) {         now = adj[u][i];         v = now.v;         if (now.w + len[u] < len[v] || (now.w + len[u] == len[v] && !now.f)) {           len[v] = now.w + len[u];           newnode.w = len[v];           newnode.v = v;           if (now.f)             ff[v] = 1;           else             ff[v] = 0;           pre[v] = u;           q.push(newnode);         }       }       cnt++;     }     return 0;   }   void addnode(int u, int v, int w, int f) {     node nn;     nn.v = v;     nn.w = w;     nn.f = f;     adj[u].push_back(nn);   }   int main() {     ans = 0;     int t, i, u, v, w, cnt = 0;     node now, nn;     scanf( %d%d%d , &n, &m, &k);     for (i = 0; i < m; i++) {       scanf( %d%d%d , &u, &v, &w);       addnode(u, v, w, 0);       addnode(v, u, w, 0);     }     for (i = 0; i < k; i++) {       scanf( %d%d , &v, &w);       addnode(1, v, w, 1);       addnode(v, 1, w, 1);     }     dijk(1);     ans = 0;     for (int i = 2; i <= n; i++) {       if (ff[i]) ans++;     }     printf( %d n , k - ans);     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 n;     cin >> n;     long long int mc[6];     mc[0] = mc[5] = 0;     for (long long int i = 0, k = 0; i < n; i++) {       cin >> k;       mc[k] += 1;     }     if (mc[0] < 1)       cout << -1;     else if (mc[5] < 9)       cout << 0;     else {       for (long long int i = 0; i < mc[5] / 9 * 9; i++) cout << 5;       for (long long int i = 0; i < mc[0]; i++) cout << 0;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 32768, N = 109;   char ch;   string s[N], t;   int ans, v, f[MOD + 9], num, p, pos;   int ex();   int gt() {     int ret = 0;     if (t[pos] ==  n ) return pos++, v;     if (t[pos] ==  f ) return pos += 2, ret = f[ex()], pos++, ret;     while (t[pos] >=  0  && t[pos] <=  9 ) ret = ret * 10 + (t[pos] -  0 ), pos++;     return ret;   }   int nex() {     int ret = gt();     while (t[pos] ==  *  || t[pos] ==  / ) {       char tp = t[pos];       pos++;       int t1 = gt();       if (tp ==  * )         ret = (int)((1ll * ret * t1) % MOD);       else         ret = (ret / t1) % MOD;     }     return ret;   }   int ex() {     int ret = nex();     while (t[pos] ==  +  || t[pos] ==  - ) {       char tp = t[pos];       pos++;       int t1 = nex();       if (tp ==  + )         ret = (ret + t1) % MOD;       else         ret = (ret + MOD - t1) % MOD;     }     return ret;   }   bool iex() {     int p1, p2, tp;     p1 = ex();     if (t[pos] ==  = )       tp = 0, pos += 2;     else if (t[pos] ==  < )       tp = -1, pos++;     else       tp = 1, pos++;     p2 = ex();     if (tp == 0) return p1 == p2;     if (tp == -1) return p1 < p2;     if (tp == 1) return p1 > p2;     return 0;   }   int getans() {     for (int i = 0; i < num; i++) {       t = s[i];       if (t[0] ==  i ) {         pos = 3;         if (iex()) return pos += 7, ex();       } else         return pos = 6, ex();     }     return -1;   }   int main() {     cin >> ans >> ch;     while (ch !=  { ) cin >> ch;     num = 0;     while (true) {       cin >> ch;       if (ch ==  } ) break;       if (ch ==  ; )         num++;       else         s[num] += ch;     }     int anst = -1;     for (int i = 0; i < MOD; i++) {       v = i, f[i] = getans();       if (f[i] == ans) anst = i;     }     cout << anst;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, l;   vector<vector<int>> adj;   int timer;   vector<int> tin, tout;   vector<vector<int>> up;   vector<int> dist;   vector<int> down;   void dfs(int v, int p) {     dist[v] = dist[p] + 1;     down[v] = 1;     tin[v] = ++timer;     up[v][0] = p;     for (int i = 1; i <= l; ++i) up[v][i] = up[up[v][i - 1]][i - 1];     for (int u : adj[v]) {       if (u != p) {         dfs(u, v);         down[v] += down[u];       }     }     tout[v] = ++timer;   }   bool is_ancestor(int u, int v) {     return tin[u] <= tin[v] && tout[u] >= tout[v];   }   int lca(int u, int v) {     if (is_ancestor(u, v)) return u;     if (is_ancestor(v, u)) return v;     for (int i = l; i >= 0; --i) {       if (!is_ancestor(up[u][i], v)) u = up[u][i];     }     return up[u][0];   }   int pre(int u, int v) {     for (int i = l; i >= 0; --i) {       if (!is_ancestor(up[u][i], v)) u = up[u][i];     }     return u;   }   void preprocess(int root) {     tin.resize(n + 1);     tout.resize(n + 1);     timer = 0;     l = ceil(log2(n + 1));     up.assign(n + 1, vector<int>(l + 2));     dfs(root, root);   }   void trav(int x, int d) {     int to = x;     for (int i = l; i >= 0; i--) {       if (dist[x] - dist[up[to][i]] < d) {         to = up[to][i];       }     }     cout << down[up[to][0]] - down[to] <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n;     adj.resize(n + 1);     dist.resize(n + 1);     down.resize(n + 1);     for (int i = 0; i < n - 1; i++) {       int x, y;       cin >> x >> y;       adj[x].push_back(y);       adj[y].push_back(x);     }     preprocess(1);     cin >> m;     for (int i = 0; i < m; i++) {       int x, y;       cin >> x >> y;       int L = lca(x, y);       if (x == y) {         cout << n <<   n ;         continue;       }       int d = (dist[x] - dist[L] + dist[y] - dist[L] + 1);       if (d % 2 == 0) {         cout << 0 <<   n ;       } else {         if (dist[x] - dist[L] == dist[y] - dist[L]) {           cout << n - down[pre(x, L)] - down[pre(y, L)] <<   n ;         } else {           d = (dist[x] - dist[L] + dist[y] - dist[L]);           if (dist[x] < dist[y]) swap(x, y);           int z = d / 2;           trav(x, z);         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int fp(int abc, int parent[]) {     if (parent[abc] == abc) return abc;     int def = fp(parent[abc], parent);     parent[abc] = def;     return def;   }   void cycledmerge(vector<int> hub[], int n, int parent[]) {     stack<int> urutan;     for (int i = 0; i < n; i++) parent[i] = i;     stack<pair<int, int> > ngurut;     vector<int> sudah(n, 0);     vector<int> adadiurutan(n, 0);     vector<int> dari(n, -1);     for (int i = 0; i < n; i++) {       if (sudah[i]) continue;       urutan.push(i);       ngurut.push(make_pair(i, 0));       adadiurutan[i] = 1;       sudah[i] = 1;       while (!ngurut.empty()) {         int id = ngurut.top().first;         int val = ngurut.top().second;         ngurut.pop();         if (val == (int)hub[id].size()) {           while (!urutan.empty() && urutan.top() == id) urutan.pop();           adadiurutan[id] = 0;           continue;         }         ngurut.push(make_pair(id, val + 1));         int next = hub[id][val];         if (dari[id] == next) continue;         if (sudah[next] && !adadiurutan[fp(next, parent)]) continue;         if (!sudah[next]) {           sudah[next] = 1;           urutan.push(next);           ngurut.push(make_pair(next, 0));           adadiurutan[next] = 1;           dari[next] = id;           continue;         }         if (parent[fp(id, parent)] == fp(next, parent)) continue;         parent[fp(id, parent)] = fp(next, parent);         while (!urutan.empty() && urutan.top() != fp(next, parent)) {           parent[fp(urutan.top(), parent)] = fp(next, parent);           urutan.pop();         }       }     }     return;   }   int main() {     int p[200];     vector<int> hub[200];     int n, m;     cin >> n >> m;     for (int(i) = 0; (i) < (m); ++(i)) {       int a, b;       cin >> a >> b;       --a;       --b;       hub[a].push_back(b);       hub[b].push_back(a);     }     cycledmerge(hub, n, p);     vector<int> ukuran(n, 0);     for (int(i) = 0; (i) < (n); ++(i)) ukuran[p[i]]++;     int numcyc = 0;     for (int(i) = 0; (i) < (n); ++(i))       if (ukuran[i] >= 3) ++numcyc;     if (numcyc != 1) {       cout <<  NO  << endl;       return 0;     }     int besar = 0;     for (int(i) = 0; (i) < (n); ++(i))       if (ukuran[i] >= 3) besar = ukuran[i];     int yesadj = 0;     int noadj = 0;     for (int(i) = 0; (i) < (n); ++(i))       for (int(j) = 0; (j) < (((int)hub[i].size())); ++(j)) {         int k = hub[i][j];         if (k < i) continue;         if (p[i] != p[k])           ++noadj;         else           ++yesadj;       }     if (yesadj != besar || noadj != (n - besar + 1) - 1) {       cout <<  NO  << endl;     } else {       cout <<  FHTAGN!  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, resmax = -round(1e9), resmin = round(1e9);   int main() {     cin >> n;     for (int i = 1; i <= 7; i++) {       int j = i, dem1 = 0;       for (int dem = 1; dem <= n; dem++) {         if ((j % 7 == 0) || (j % 7 == 6)) dem1++;         j++;         if (j > 7) j = 1;       }       resmax = max(resmax, dem1);       resmin = min(resmin, dem1);     }     cout << resmin <<     << resmax;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string st;     cin >> st;     for (int i = 0; i < st.length(); i++) {       if (st[i] ==  H  || st[i] ==  Q  || st[i] ==  9 ) {         cout <<  YES  << endl;         return 0;       }     }     cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int solve() {     int n;     cin >> n;     if (n == 1) return cout << -1 << endl, 0;     string s =   ;     for (int i = 0; i < n - 1; i++) s +=  3 ;     cout <<  2  + s << endl;     return 0;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int gi() {     int res = 0, w = 1;     char ch = getchar();     while (ch !=  -  && !isdigit(ch)) ch = getchar();     if (ch ==  - ) w = -1, ch = getchar();     while (isdigit(ch)) res = res * 10 + ch -  0 , ch = getchar();     return res * w;   }   const int Mod = 1e9 + 7, K = 1e9;   int upd(int x) { return x + (x >> 31 & Mod); }   const int MAX_N = 3e5 + 5;   int N, M, a[MAX_N], b[MAX_N], c[MAX_N];   char s[MAX_N];   int d[MAX_N], m;   void Div(int x, int &v) {     long long now = 0;     for (int i = m; i; i--) {       now = now * K + d[i];       d[i] = now / x, now %= x;     }     v = now;     for (; m > 1 && !d[m]; m--)       ;   }   int f[2][MAX_N * 20], mx[2];   void add(int o, int x, int v) {     f[o][x] = upd(f[o][x] + v - Mod);     mx[o] = max(mx[o], x);   }   int main() {     N = gi();     a[1] = 1;     for (int i = 2; i <= N; i++) a[i] = gi();     for (int i = 1; i <= N; i++) b[i] = gi();     scanf( %s , s + 1), M = strlen(s + 1);     reverse(&s[1], &s[M + 1]);     for (int i = 1; i <= M; i += 9) {       ++m;       for (int j = min(M, i + 8); j >= i; j--) d[m] = d[m] * 10 + s[j] -  0 ;     }     a[N + 1] = K;     for (int i = 1; i <= N; i++)       if (a[i + 1] > 1) Div(a[i + 1], c[i]);     if (m != 1 || d[1] != 0) return puts( 0 ) & 0;     f[0][0] = 1, f[0][1] = Mod - 1, mx[0] = 1;     for (int i = 1, o = 0; i <= N; i++, o ^= 1) {       for (int j = 1; j <= mx[o]; j++) f[o][j] = upd(f[o][j] + f[o][j - 1] - Mod);       for (int j = 0; j <= mx[o]; j++)         if (f[o][j]) {           int k = (c[i] - j % a[i + 1] + a[i + 1]) % a[i + 1];           int l = (j + k - c[i]) / a[i + 1],               r = b[i] / a[i + 1] + 1 - (b[i] % a[i + 1] < k);           add(o ^ 1, l, f[o][j]), add(o ^ 1, l + r, Mod - f[o][j]);           f[o][j] = 0;         }       mx[o] = 0;     }     printf( %d n , f[N & 1][0]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<int, int> cnt, b;   vector<int> ans;   int a[200010], x;   int n, m, p;   int main() {     cin >> n >> m >> p;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < m; i++) {       cin >> x;       b[x]++;     }     for (int i = 0; i < p; i++) {       if (i + (long long)(m - 1) * p >= n) break;       long long j;       cnt = b;       for (j = i; j <= i + (long long)(m - 1) * p; j += p)         if (--cnt[a[j]] == 0) cnt.erase(a[j]);       if (cnt.size() == 0) ans.push_back(i + 1);       for (long long k = i; j < n; j += p, k += p) {         if (--cnt[a[j]] == 0) cnt.erase(a[j]);         if (++cnt[a[k]] == 0) cnt.erase(a[k]);         if (cnt.size() == 0) ans.push_back(k + p + 1);       }     }     sort(ans.begin(), ans.end());     cout << ans.size() << endl;     for (int i = 0; i < ans.size(); i++) cout << ans[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int k[5] = {100, 20, 10, 5, 1};   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n;     while (cin >> n) {       long long ans = 0;       for (int i = 0; i < 5; i++) {         ans += n / k[i];         n %= k[i];       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   #pragma optimize( Ofast )   using namespace std;   using ll = long long int;   using ull = unsigned long long int;   using dd = double;   using ldd = long double;   using si = short int;   using pii = pair<int, int>;   using pll = pair<ll, ll>;   int _size = 1 << 20;   vector<ll> tree(_size << 1), cnt(_size << 1);   vector<ll> pos;   vector<ll> x;   vector<ll> price;   vector<ll> if_now;   vector<ll> to_pay;   vector<vector<pll>> gr;   vector<ll> ans;   int n;   void upd(int v, ll cn) {     ll c = cn;     cn *= price[v];     v = pos[v];     v += _size;     cnt[v] += c;     tree[v] += cn;     v >>= 1;     while (v) {       tree[v] += cn;       cnt[v] += c;       v >>= 1;     }   }   int find_f(ll t, int v = 1) {     if (v >= _size) return v - _size;     if (tree[v << 1] > t)       return find_f(t, v << 1);     else       return find_f(t - tree[v << 1], v << 1 | 1);   }   ll get_cnt(ll rr, int v = 1, int l = 0, int r = _size) {     if (r <= rr) return cnt[v];     if (l >= rr) return 0;     return get_cnt(rr, v << 1, l, (r + l) >> 1) +            get_cnt(rr, v << 1 | 1, (r + l) >> 1, r);   }   ll get_pay(ll rr, int v = 1, int l = 0, int r = _size) {     if (r <= rr) return tree[v];     if (l >= rr || r <= l) return 0;     return get_pay(rr, v << 1, l, (r + l) >> 1) +            get_pay(rr, v << 1 | 1, (r + l) >> 1, r);   }   ll get_now(ll t) {     0;     if (t >= tree[1]) {       return get_cnt(_size);     }     0;     int ind = find_f(t);     0;     ll ans = get_cnt(ind);     0;     t -= get_pay(ind);     0;     if (ind >= to_pay.size() || to_pay[ind] == 0) {       return ans;     }     return ans + t / to_pay[ind];   }   void dfs(int v, ll t) {     0;     upd(v, x[v]);     0;     if_now[v] = get_now(t);     0;     for (pll i : gr[v]) {       t -= i.second * 2;       if (t >= 0) dfs(i.first, t);       t += i.second * 2;     }     upd(v, -x[v]);   }   void afs(int v) {     ans[v] = if_now[v];     vector<ll> ch;     for (pii i : gr[v]) {       afs(i.first);       ch.push_back(ans[i.first]);     }     sort((ch).begin(), (ch).end());     reverse((ch).begin(), (ch).end());     ans[v] = max(ans[v], (ch.size() >= 2 ? ch[1] : 0));   }   int main() {     ios_base::sync_with_stdio(0);     cout.tie(0);     cin.tie(0);     ll t;     cin >> n >> t;     x.resize(n);     pos.resize(n);     price.resize(n);     if_now.resize(n);     gr.resize(n);     to_pay.resize(n);     ans.resize(n);     for (int i = 0; i < n; ++i) cin >> x[i];     map<ll, ll> ttt;     int s = 0;     for (int i = 0; i < n; ++i) {       cin >> price[i];       ttt[price[i]] = 0;     }     for (auto& i : ttt) {       i.second = s;       to_pay[s] = i.first;       ++s;     }     0;     for (int i = 0; i < n; ++i) pos[i] = ttt[price[i]];     0;     vector<pair<ll, ll>> tt;     for (int i = 0; i < n - 1; ++i) {       ll a, b;       cin >> a >> b;       tt.push_back({a, b});       gr[a - 1].push_back({i + 1, b});     }     0;     dfs(0, t);     0;     0;     afs(0);     0;     for (pll i : gr[0]) {       ans[0] = max(ans[0], ans[i.first]);     }     cout << ans[0];   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int TESTS = 1;     cin >> TESTS;     while (TESTS--) {       long long n, x, y, d;       cin >> n >> x >> y >> d;       if (abs(y - x) % d == 0) {         cout << abs(y - x) / d <<   n ;         continue;       }       long long l = 1000000007, r = 1000000007;       if ((n - y) % d == 0) {         l = (n - y) / d + (n - x + d - 1) / d;       }       if ((y - 1) % d == 0) {         r = (y - 1) / d + (x - 1 + d - 1) / d;       }       long long ans = min(l, r);       if (ans != 1000000007)         cout << ans <<   n ;       else         cout <<  -1 n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long nch(long long n) {     long long ans = 0, tmp = 1, k = 1;     while (n) {       ans += min(n, tmp * 9) * k;       n -= min(n, tmp * 9);       tmp *= 10;       k++;     }     return ans;   }   char get(long long n, long long d) {     string s;     stringstream ss;     ss << n;     ss >> s;     return s[d - 1];   }   char kth(long long n) {     long long s = 0, nine = 9, d = 0, len;     for (len = 1;; len++) {       s += nine * len;       d += nine;       if (s >= n) {         s -= nine * len;         d -= nine;         n -= s;         break;       }       nine *= 10;     }     long long diff = ceil((double)n / len);     long long dd = n % len;     if (dd == 0) dd = len;     return get(d + diff, dd);   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long q;     cin >> q;     while (q--) {       long long k, n = 1;       cin >> k;       for (n = 1; nch(n) < k; n++) k -= nch(n);       cout << kth(k) << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 5e5 + 7;   vector<int> g[N];   int used[N];   int pos[N];   vector<vector<int>> cycles;   vector<int> st;   void dfs(int u, int p = -1) {     st.push_back(u);     pos[st.back()] = (int)st.size() - 1;     used[u] = 1;     for (auto v : g[u]) {       if (!used[v]) {         dfs(v, u);       } else if (v != p && used[v] == 1) {         int x = pos[v];         cycles.push_back({});         for (int i = x; i < (int)st.size(); ++i) {           cycles.back().push_back(st[i]);         }       }     }     st.pop_back();     used[u] = 2;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n, m;     cin >> n >> m;     vector<pair<int, int>> edges;     map<pair<int, int>, int> id;     for (int i = 0; i < m; ++i) {       int u, v;       cin >> u >> v;       --u, --v;       g[u].push_back(v);       g[v].push_back(u);       edges.push_back({u, v});       id[{u, v}] = id[{v, u}] = i;     }     dfs(0);     vector<int> res(m);     vector<int> who(m, -1);     vector<int> cmp(n, -1);     for (int i = 0; i < (int)cycles.size(); ++i) {       vector<int> ce;       for (int j = 0; j < (int)cycles[i].size(); ++j) {         cmp[cycles[i][j]] = i;         ce.push_back(             id[{cycles[i][j], cycles[i][(j + 1) % (int)cycles[i].size()]}]);       }       rotate(ce.begin(), min_element(ce.begin(), ce.end()), ce.end());       int mx = max_element(ce.begin(), ce.end()) - ce.begin();       bool ok = true;       for (int i = 1; i <= mx; ++i) ok &= ce[i] > ce[i - 1];       for (int i = mx + 1; i < ce.size(); ++i) ok &= ce[i] < ce[i - 1];       if (ok) who[ce[0]] = ce[mx];     }     vector<int> ans(n, 1);     for (int i = m - 1; i >= 0; --i) {       int a = edges[i].first;       int b = edges[i].second;       int sum = ans[a] + ans[b];       if (who[i] != -1) sum -= res[who[i]];       res[i] = sum;       ans[a] = ans[b] = sum;     }     for (int i = 0; i < n; ++i) cout << ans[i] - 1 <<    ;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 200015;   const int MOD = 1000000007;   int a[MAXN];   bool F(int len, int num) {     bool flag;     int pres;     for (int i = 0; i <= num - 1; i++) {       flag = true;       pres = i;       for (int j = 0; j <= len - 1; j++) {         if (a[pres] == 0) flag = false;         pres += num;       }       if (flag == true) return flag;     }     return false;   }   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i <= n - 1; i++) scanf( %d , &a[i]);     bool ok = false;     for (int i = 1; i <= sqrt(n); i++) {       if (n % i == 0) {         if (i >= 3) ok = ok | F(i, n / i);         if (i != sqrt(n) && n / i >= 3) ok = ok | F(n / i, i);       }     }     if (ok)       cout <<  YES n ;     else       cout <<  NO n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     int s;     cin >> n;     s = n;     int k = 2;     while (k <= n) {       if (n % k == 0) {         s += n / k;         n = n / k;       } else         k++;     }     cout << s;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int signum(long long a) { return (a == 0 ? 0 : (a > 0 ? 1 : -1)); }   int dir(pair<long long, long long>& a, pair<long long, long long>& b,           pair<long long, long long>& c) {     return signum((b.first - a.first) * (c.second - a.second) -                   (b.second - a.second) * (c.first - a.first));   }   bool inside(vector<pair<long long, long long> >& hull,               pair<long long, long long>& p) {     if (dir(hull[0], hull[1], p) != 1) return false;     if (dir(hull[0], hull[hull.size() - 1], p) != -1) return false;     int l = 1, r = hull.size() - 2;     while (l < r) {       int m = (l + r) / 2 + 1;       if (dir(hull[0], hull[m], p) == -1)         r = m - 1;       else         l = m;     }     return dir(hull[l], hull[l + 1], p) == 1;   }   int main() {     cin.sync_with_stdio(false);     cin.tie(0);     int n, m;     cin >> n;     vector<pair<long long, long long> > A(n);     for (pair<long long, long long>& a : A) cin >> a.first >> a.second;     reverse(A.begin(), A.end());     cin >> m;     vector<pair<long long, long long> > B(m);     bool sol = true;     for (pair<long long, long long>& a : B) {       cin >> a.first >> a.second;       sol &= inside(A, a);     }     cout << (sol ?  YES  :  NO ) << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, y = 0;     cin >> a >> b;     while (a <= b) {       a *= 3;       b *= 2;       y++;     }     cout << y;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 2e9;   const long long Inf = 1e10;   const int P = 1e9 + 7;   const int N = 100005;   const int M = 1000005;   inline long long IN() {     long long x = 0;     int ch = 0, f = 0;     for (ch = getchar(); ch != -1 && (ch < 48 || ch > 57); ch = getchar())       f = (ch ==  - );     for (; ch >= 48 && ch <= 57; ch = getchar())       x = (x << 1) + (x << 3) + ch -  0 ;     return f ? (-x) : x;   }   template <typename T>   inline int chkmin(T &a, const T &b) {     if (b < a) return a = b, 1;     return 0;   }   template <typename T>   inline int chkmax(T &a, const T &b) {     if (b > a) return a = b, 1;     return 0;   }   void renew(int &x, const int &y) {     x += y;     if (x >= P) x -= P;     if (x < 0) x += P;   }   int Pow(int x, int y, int p) {     int a = 1;     for (; y; y >>= 1, x = (long long)x * x % p)       if (y & 1) a = (long long)a * x % p;     return a;   }   vector<pair<int, int> > in[M], ou[M];   int n, m, K;   int cov[M << 2], sum[M << 2], len[M << 2];   int ql, qr, qv, qans;   void Cov(int x, int y) {     cov[x] = y;     sum[x] = 1LL * len[x] * y % P;   }   void sep(int x) {     if (cov[x] == -1) return;     Cov(x << 1, cov[x]);     Cov(x + x + 1, cov[x]);     cov[x] = -1;   }   void A(int x, int L, int R) {     if (ql <= L && R <= qr) {       Cov(x, qv);       return;     }     sep(x);     int md = (L + R) >> 1;     if (ql <= md) A(x << 1, L, md);     if (md < qr) A(x + x + 1, md + 1, R);     sum[x] = sum[x + x + 1];     renew(sum[x], sum[x << 1]);   }   void Add(int l, int r, int v) {     if (l > r) return;     ql = l;     qr = r;     qv = v;     A(1, 1, m);   }   void Q(int x, int L, int R) {     if (ql <= L && R <= qr) {       renew(qans, sum[x]);       return;     }     sep(x);     int md = (L + R) >> 1;     if (ql <= md) Q(x << 1, L, md);     if (md < qr) Q(x + x + 1, md + 1, R);   }   int Ask(int l, int r) {     if (l > r) return 0;     ql = l;     qr = r;     qans = 0;     Q(1, 1, m);     return qans;   }   void B(int x, int L, int R) {     len[x] = R - L + 1;     if (L == R) return;     int md = (L + R) / 2;     B(x << 1, L, md);     B(x + x + 1, md + 1, R);   }   set<pair<int, int> > st;   int main() {     scanf( %d%d%d , &n, &m, &K);     for (int i = (int)(1); i <= (int)(K); i++) {       int x1, y1, x2, y2;       scanf( %d%d%d%d , &x1, &y1, &x2, &y2);       in[x1].push_back(make_pair(y1, y2));       ou[x2 + 1].push_back(make_pair(y1, y2));     }     B(1, 1, m);     Add(1, 1, 1);     for (auto v : in[1]) {       st.insert(v);       Add(v.first, v.second, 0);     }     for (int i = (int)(2); i <= (int)(n); i++) {       static int tot, vx[N], vc[N];       tot = 0;       for (auto v : in[i]) {         if (v.second + 1 > m) continue;         auto w = st.lower_bound(make_pair(v.second + 2, 0));         int Low = 0;         if (w != st.begin()) {           --w;           Low = w->second;         }         ++tot;         vc[tot] = Ask(Low + 1, v.second + 1);         vx[tot] = v.second + 1;       }       for (int j = (int)(1); j <= (int)(tot); j++) Add(vx[j], vx[j], vc[j]);       for (auto v : ou[i]) st.erase(v);       for (auto v : in[i]) {         st.insert(v);         Add(v.first, v.second, 0);       }     }     int Low = (st.empty()) ? (0) : ((--st.end())->second);     cout << Ask(Low + 1, m) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int Maxn = 500010;   const int Maxk = 110;   const int Maxp = 110;   const int inf = 2147483647;   int read() {     int x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 )       x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();     return x * f;   }   int n, k, p, s[Maxn], f[Maxn][Maxk];   int mn[2][Maxp], v[Maxp];   void modify(int o, int x, int y) {     for (; x <= p; x += (x & -x)) mn[o][x] = min(mn[o][x], y);   }   int query(int o, int x) {     int re = inf;     for (; x; x -= (x & -x)) re = min(re, mn[o][x]);     return re;   }   int main() {     memset(f, 63, sizeof(f));     n = read(), k = read(), p = read();     s[0] = 0;     for (int i = 1; i <= n; i++)       s[i] = (read() % p + s[i - 1]) % p, f[i][1] = s[i];     for (int j = 2; j <= k; j++) {       memset(v, 63, sizeof(v));       memset(mn, 63, sizeof(mn));       v[s[j - 1]] = f[j - 1][j - 1] - s[j - 1];       modify(0, s[j - 1] + 1, v[s[j - 1]]);       modify(1, p - s[j - 1], v[s[j - 1]]);       for (int i = j; i <= n; i++) {         f[i][j] = min(s[i] + query(0, s[i] + 1), s[i] + p + query(1, p - s[i]));         if (f[i][j - 1] - s[i] < v[s[i]]) {           v[s[i]] = f[i][j - 1] - s[i];           modify(0, s[i] + 1, v[s[i]]);           modify(1, p - s[i], v[s[i]]);         }       }     }     printf( %d , f[n][k]);   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) {     while (b) {       a %= b;       swap(a, b);     }     return a;   }   int main() {     int t;     cin >> t;     while (t--) {       long long x, y;       cin >> x >> y;       if (gcd(x, y) == 1) {         cout <<  FINITE  << endl;       } else {         cout <<  INFINITE  << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200005;   int cycl[N], set_val[N], cycl_size[N];   int main() {     int h, m, n;     cin >> h >> m >> n;     vector<int> strt;     bool vis[h];     memset(vis, false, sizeof vis);     int kk = 0;     set<int> dmm[h + 1];     for (long long i = 0; i < m; i++) {       if (vis[i]) {         continue;       }       int ii = i;       int jp = 0;       while (1) {         if (vis[ii]) {           break;         }         set_val[ii] = jp;         cycl[ii] = kk;         dmm[kk].insert(jp);         vis[ii] = true;         ii += m;         ii %= h;         jp++;       }       cycl_size[kk] = jp;       kk++;     }     map<int, int> hash;     map<int, int> final;     long long cnt = 0;     while (n--) {       char ch;       scanf(  %c , &ch);       if (ch ==  + ) {         int id, t;         scanf( %d%d , &id, &t);         hash[id] = t;         if (dmm[cycl[t]].find(set_val[t]) != dmm[cycl[t]].end()) {           final[id] = set_val[t];           dmm[cycl[t]].erase(set_val[t]);           continue;         }         set<int>::iterator it, it1;         it = dmm[cycl[t]].lower_bound(set_val[t]);         int xx1;         if (it == dmm[cycl[t]].end()) {           it = dmm[cycl[t]].begin();           xx1 = cycl_size[cycl[t]] + *it - set_val[t];           int jj = *it;           final[id] = jj;           dmm[cycl[t]].erase(jj);         } else {           xx1 = *it - set_val[t];           int jj = *it;           final[id] = jj;           dmm[cycl[t]].erase(jj);         }         cnt += (long long)xx1;       } else {         int id;         scanf( %d , &id);         dmm[cycl[hash[id]]].insert(final[id]);       }     }     cout << cnt << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     long long int a[n], p[n], s[n];     for (int i = 0; i < n; i++) {       cin >> a[i];     }     int blocks = 0;     p[0] = a[0];     for (int i = 1; i < n; i++) {       p[i] = max(p[i - 1], a[i]);     }     s[n - 1] = a[n - 1];     for (int i = n - 2; i >= 0; i--) {       s[i] = min(s[i + 1], a[i]);     }     for (int i = 0; i < n - 1; i++) {       if (p[i] <= s[i + 1]) {         blocks++;       }     }     cout << blocks + 1 << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, k, i, j;     cin >> n >> m >> k;     int a[n][m];     for (i = 0; i < n; i++) {       for (j = 0; j < m; j++) {         cin >> a[i][j];       }     }     int x;     for (i = 0; i < k; i++) {       cin >> x;       x--;       int v = 0;       while (v < n) {         if (a[v][x] == 2) {           v++;         } else if (a[v][x] == 1) {           a[v][x] = 2;           x++;         } else {           a[v][x] = 2;           x--;         }       }       cout << x + 1 <<    ;     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, i = 0;     cin >> n;     long long a;     vector<long long> vc;     while (n--) {       cin >> a;       vc.push_back(a);     }     sort(vc.begin(), vc.end());     i = vc.size();     long long ans = vc[i - 1];     for (int j = i - 2; j >= 0; j--) {       if (vc[j] >= vc[j + 1]) {         vc[j] = vc[j + 1] - 1;       }       if (vc[j] > 0) ans += vc[j];     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int q, n;   long long s, cur;   char str[100100];   int main() {     scanf( %lld%d , &s, &q);     while (q--) {       scanf( %lld , &cur);       scanf( %s , str);       n = strlen(str);       for (int i = 0; i < n; i++) {         long long t = 1;         while (cur % (t << 1) == 0) t <<= 1;         if (str[i] ==  U ) {           if ((t << 1) > s) continue;           cur ^= t;           cur |= (t << 1);         } else if (str[i] ==  L ) {           if (t == 1) continue;           cur ^= t;           cur |= (t >> 1);         } else {           if (t == 1) continue;           cur |= (t >> 1);         }       }       printf( %lld n , cur);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool iss(int d) {     for (int i = 2; i * i <= d; ++i)       if (d % i == 0) return false;     return true;   }   int main() {     int a, b, c;     scanf( %d%d%d , &a, &b, &c);     vector<int> simple;     for (int i = 2; i < 100; ++i)       if (iss(i)) simple.push_back(i);     vector<vector<int> > d(101);     for (int i = 1; i <= 100; ++i) {       d[i].resize(simple.size());       for (int j = 0, tmp = i; j < simple.size(); ++j)         while (tmp % simple[j] == 0) {           tmp /= simple[j];           d[i][j]++;         }     }     int ans = 0;     int mod = 1 << 30;     for (int i = 1; i <= a; ++i)       for (int j = 1; j <= b; ++j)         for (int k = 1; k <= c; ++k) {           int prod = 1;           for (int l = 0; l < simple.size(); ++l)             prod *= d[i][l] + d[j][l] + d[k][l] + 1;           ans = (ans + prod) % mod;         }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     int l, r, res = 0;     cin >> l >> r;     vector<unsigned long long> two, three, temp;     for (int i = 0; i < 31; ++i) {       two.push_back(1LL << i);       if (i > 19) continue;       three.push_back(pow(3LL, i));     }     for (int i = 0; i < two.size(); ++i) {       for (int j = 0; j < three.size(); ++j) {         if (two[i] * three[j] <= 2000000000LL) temp.push_back(two[i] * three[j]);       }     }     sort(temp.begin(), temp.end());     res = upper_bound(temp.begin(), temp.end(), r) -           lower_bound(temp.begin(), temp.end(), l);     cout << res;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T, typename U>   std::istream& operator>>(std::istream& i, pair<T, U>& p) {     i >> p.first >> p.second;     return i;   }   template <typename T>   std::istream& operator>>(std::istream& i, vector<T>& t) {     for (auto& v : t) {       i >> v;     }     return i;   }   template <typename T, typename U>   std::ostream& operator<<(std::ostream& o, const pair<T, U>& p) {     o << p.first <<     << p.second;     return o;   }   template <typename T>   std::ostream& operator<<(std::ostream& o, const vector<T>& t) {     if (t.empty()) o <<   n ;     for (size_t i = 0; i < t.size(); ++i) {       o << t[i] <<    n [i == t.size() - 1];     }     return o;   }   template <typename T>   using minheap = priority_queue<T, vector<T>, greater<T>>;   template <typename T>   using maxheap = priority_queue<T, vector<T>, less<T>>;   template <typename T>   bool in(T a, T b, T c) {     return a <= b && b < c;   }   unsigned int logceil(long long first) {     return first ? 8 * sizeof(long long) - __builtin_clzll(first) : 0;   }   namespace std {   template <typename T, typename U>   struct hash<pair<T, U>> {     hash<T> t;     hash<U> u;     size_t operator()(const pair<T, U>& p) const {       return t(p.first) ^ (u(p.second) << 7);     }   };   }  // namespace std   template <typename T, typename F>   T bsh(T l, T h, const F& f) {     T r = -1, m;     while (l <= h) {       m = (l + h) / 2;       if (f(m)) {         l = m + 1;         r = m;       } else {         h = m - 1;       }     }     return r;   }   template <typename F>   double bshd(double l, double h, const F& f, double p = 1e-9) {     unsigned int r = 3 + (unsigned int)log2((h - l) / p);     while (r--) {       double m = (l + h) / 2;       if (f(m)) {         l = m;       } else {         h = m;       }     }     return (l + h) / 2;   }   template <typename T, typename F>   T bsl(T l, T h, const F& f) {     T r = -1, m;     while (l <= h) {       m = (l + h) / 2;       if (f(m)) {         h = m - 1;         r = m;       } else {         l = m + 1;       }     }     return r;   }   template <typename F>   double bsld(double l, double h, const F& f, double p = 1e-9) {     unsigned int r = 3 + (unsigned int)log2((h - l) / p);     while (r--) {       double m = (l + h) / 2;       if (f(m)) {         h = m;       } else {         l = m;       }     }     return (l + h) / 2;   }   template <typename T>   T gcd(T a, T b) {     if (a < b) swap(a, b);     return b ? gcd(b, a % b) : a;   }   template <typename T>   class vector2 : public vector<vector<T>> {    public:     vector2() {}     vector2(size_t a, size_t b, T t = T())         : vector<vector<T>>(a, vector<T>(b, t)) {}   };   template <typename T>   class vector3 : public vector<vector2<T>> {    public:     vector3() {}     vector3(size_t a, size_t b, size_t c, T t = T())         : vector<vector2<T>>(a, vector2<T>(b, c, t)) {}   };   template <typename T>   class vector4 : public vector<vector3<T>> {    public:     vector4() {}     vector4(size_t a, size_t b, size_t c, size_t d, T t = T())         : vector<vector3<T>>(a, vector3<T>(b, c, d, t)) {}   };   template <typename T>   class vector5 : public vector<vector4<T>> {    public:     vector5() {}     vector5(size_t a, size_t b, size_t c, size_t d, size_t e, T t = T())         : vector<vector4<T>>(a, vector4<T>(b, c, d, e, t)) {}   };   class ENeatness {    public:     int N, A, B, X, Y;     string Sol;     vector<vector<bool>> L, V;     vector<int> LOX, HIX, LOY, HIY;     constexpr char op(char c) {       switch (c) {         case  L :           return  R ;         case  R :           return  L ;         case  D :           return  U ;         case  U :           return  D ;         case  1 :           return  2 ;         case  2 :           return  1 ;         default:           return  x ;       }     }     void goUp() {       if (A == 0) return;       while (B > 0 && L[A][B - 1]) {         add( L );         --B;       }       while (B < N - 1 && !L[A - 1][B] && L[A][B + 1]) {         add( R );         ++B;       }       if (L[A - 1][B]) {         --A;         add( U );         goUp();       }     }     void goDown() {       if (A == N - 1) return;       while (B > 0 && L[A][B - 1]) {         add( L );         --B;       }       while (B < N - 1 && !L[A + 1][B] && L[A][B + 1]) {         add( R );         ++B;       }       if (L[A + 1][B]) {         ++A;         add( D );         goDown();       }     }     void turnOffAndDown() {       if (A == X) return;       int b = -1;       for (int i = 0; i < N; ++i) {         if (L[A][i] && L[A + 1][i]) {           b = i;         }       }       while (B > 0 && L[A][B - 1]) {         add( L );         --B;       }       while (B < N - 1 && L[A][B + 1]) {         if (B != b) {           add( 2 );           L[A][B] = false;         }         add( R );         ++B;       }       if (B != b) {         add( 2 );         L[A][B] = false;       }       while (B != b) {         add( L );         --B;       }       add( 2 );       L[A][B] = false;       add( D );       ++A;       turnOffAndDown();     }     void turnOffAndUp() {       int b = Y;       if (A != X) {         for (int i = 0; i < N; ++i) {           if (L[A][i] && L[A - 1][i]) {             b = i;           }         }       }       while (B > 0 && L[A][B - 1]) {         add( L );         --B;       }       while (B < N - 1 && L[A][B + 1]) {         if (B != b) {           add( 2 );           L[A][B] = false;         }         add( R );         ++B;       }       if (B != b) {         add( 2 );         L[A][B] = false;       }       while (B != b) {         add( L );         --B;       }       add( 2 );       L[A][B] = false;       if (A == X) return;       add( U );       --A;       turnOffAndUp();     }     void add(char c) {       if (!Sol.empty() && Sol.back() == op(c))         Sol.pop_back();       else         Sol.push_back(c);     }     void bfs(int i, int j) {       if (V[i][j]) return;       V[i][j] = true;       if (!L[i][j]) {         add( 1 );         L[i][j] = true;         LOX[i] = min(LOX[i], j);         HIX[i] = max(HIX[i], j);         LOY[j] = min(LOY[j], i);         HIY[j] = max(HIY[j], i);       }       if (LOX[i] < j) {         add( L );         bfs(i, j - 1);         add( R );       }       if (HIX[i] > j) {         add( R );         bfs(i, j + 1);         add( L );       }       if (LOY[j] < i) {         add( U );         bfs(i - 1, j);         add( D );       }       if (HIY[j] > i) {         add( D );         bfs(i + 1, j);         add( U );       }     }     void solve(istream& cin, ostream& cout) {       cin >> N >> A >> B;       L = vector2<bool>(N, N);       V = vector2<bool>(N, N, false);       for (auto& l : L) {         for (int i = 0; i < N; ++i) {           bool c;           cin >> c;           l[i] = c;         }       }       LOX.assign(N, N);       LOY.assign(N, N);       HIX.assign(N, -1);       HIY.assign(N, -1);       for (int i = 0; i < N; ++i) {         for (int j = 0; j < N; ++j) {           if (L[i][j]) {             LOX[i] = min(LOX[i], j);             HIX[i] = max(HIX[i], j);             LOY[j] = min(LOY[j], i);             HIY[j] = max(HIY[j], i);           }         }       }       --A;       --B;       X = A;       Y = B;       bfs(A, B);       goUp();       turnOffAndDown();       goDown();       turnOffAndUp();       for (int i = 0; i < N; ++i) {         for (int j = 0; j < N; ++j) {           if (L[i][j]) {             cout <<  NO n ;             return;           }         }       }       cout <<  YES n  << Sol <<   n ;     }   };   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     ENeatness solver;     std::istream& in(std::cin);     std::ostream& out(std::cout);     solver.solve(in, out);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int T, arr[100005], a, b, ans;   int cal(int v) {     int vv = v * (v - 1);     return (vv >> 1);   }   int main() {     cin >> T;     for (int i = 1; i < T; i++) {       cin >> a >> b;       arr[a]++;       arr[b]++;     }     for (int i = 1; i <= T; i++) {       if (arr[i] >= 2) ans += cal(arr[i]);     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 200010;   struct node {     int x, y;   } a[maxn];   int ans[maxn];   int f[maxn];   bool cmp(int c, int d) { return c < d; }   int main() {     int n, m;     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) {       scanf( %d , &f[i]);     }     stack<int> s;     node ff;     ff.x = 0;     ff.y = 0;     s.push(0);     a[0].y = 0;     a[0].x = 0;     for (int i = 1; i <= m; i++) {       scanf( %d%d , &a[i].x, &a[i].y);     }     for (int i = m; i >= 1; i--) {       if (a[i].y > a[s.top()].y) {         s.push(i);       }     }     int flag = n;     int kk = 0;     int gg = a[s.top()].y;     for (int i = gg + 1; i <= n; i++) ans[i] = f[i];     sort(f + 1, f + 1 + gg);     int l = 1;     int r = gg;     int per = s.top();     s.pop();     while (!s.empty()) {       int now = s.top();       s.pop();       if (a[per].x == 1) {         for (int i = a[per].y; i > a[now].y; i--) ans[i] = f[r--];       } else {         for (int i = a[per].y; i > a[now].y; i--) {           ans[i] = f[l++];         }       }       per = now;     }     for (int i = 1; i < n; i++) {       printf( %d  , ans[i]);     }     printf( %d n , ans[n]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000000;   long long powmod(long long a, long long b) {     long long res = 1;     a %= mod;     for (; b; b >>= 1) {       if (b & 1) res = res * a % mod;       a = a * a % mod;     }     return res;   }   const int N = 201000;   struct node {     long long fib[4];     int fg;   } nd[N * 4];   int fib[N], a[N];   int n, ty, l, r, v, m;   long long ret[2], ans;   void build(int p, int l, int r) {     if (l == r) {       nd[p].fib[0] = fib[l];       nd[p].fib[1] = fib[l + 1];       nd[p].fib[2] = nd[p].fib[0] * a[l] % mod;       nd[p].fib[3] = nd[p].fib[1] * a[l] % mod;     } else {       int md = (l + r) >> 1;       build(p + p, l, md);       build(p + p + 1, md + 1, r);       for (int i = 0; i < 4; i++)         nd[p].fib[i] = (nd[p + p].fib[i] + nd[p + p + 1].fib[i]) % mod;     }   }   void setp(int p, int w) {     nd[p].fg += w;     nd[p].fib[2] = (nd[p].fib[2] + nd[p].fib[0] * w) % mod;     nd[p].fib[3] = (nd[p].fib[3] + nd[p].fib[1] * w) % mod;   }   void push(int p) {     if (nd[p].fg) {       setp(p + p, nd[p].fg);       setp(p + p + 1, nd[p].fg);       nd[p].fg = 0;     }   }   void modify(int p, int l, int r, int x, long long w) {     if (l == r) {       nd[p].fib[2] = fib[l] * w % mod;       nd[p].fib[3] = fib[l + 1] * w % mod;     } else {       push(p);       int md = (l + r) >> 1;       if (x <= md)         modify(p + p, l, md, x, w);       else         modify(p + p + 1, md + 1, r, x, w);       for (int i = 2; i < 4; i++)         nd[p].fib[i] = (nd[p + p].fib[i] + nd[p + p + 1].fib[i]) % mod;     }   }   void add(int p, int l, int r, int tl, int tr, int w) {     if (l == tl && r == tr)       setp(p, w);     else {       push(p);       int md = (l + r) >> 1;       if (tr <= md)         add(p + p, l, md, tl, tr, w);       else if (tl > md)         add(p + p + 1, md + 1, r, tl, tr, w);       else         add(p + p, l, md, tl, md, w), add(p + p + 1, md + 1, r, md + 1, tr, w);       for (int i = 2; i < 4; i++)         nd[p].fib[i] = (nd[p + p].fib[i] + nd[p + p + 1].fib[i]) % mod;     }   }   void query(int p, int l, int r, int tl, int tr) {     if (l == tl && r == tr) {       for (int i = 2; i < 4; i++) ret[i - 2] = (ret[i - 2] + nd[p].fib[i]) % mod;     } else {       push(p);       int md = (l + r) >> 1;       if (tr <= md)         query(p + p, l, md, tl, tr);       else if (tl > md)         query(p + p + 1, md + 1, r, tl, tr);       else         query(p + p, l, md, tl, md), query(p + p + 1, md + 1, r, md + 1, tr);     }   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i < n + 1; i++) scanf( %d , a + i);     fib[0] = fib[1] = 1;     for (int i = 2; i < n + 10; i++) fib[i] = (fib[i - 1] + fib[i - 2]) % mod;     build(1, 1, n);     for (int i = 0; i < m; i++) {       scanf( %d , &ty);       if (ty == 1) {         scanf( %d%d , &l, &v);         modify(1, 1, n, l, v);       } else if (ty == 2) {         scanf( %d%d , &l, &r);         ret[0] = ret[1] = 0;         query(1, 1, n, l, r);         ans = (fib[l] * ret[0] - fib[l - 1] * ret[1]) % mod;         if (l % 2 == 1) ans *= -1;         if (ans < 0) ans += mod;         printf( %I64d n , ans);       } else {         scanf( %d%d%d , &l, &r, &v);         add(1, 1, n, l, r, v);       }     }   }


#include <bits/stdc++.h>   #pragma warning(disable : 4786)   using namespace std;   int a[200000];   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);     }     a[n + 1] = a[1];     int y = 0;     for (int i = 2; i <= n + 1; i++) {       if (a[i] < a[i - 1]) {         if (y != 0) {           printf( -1 n );           return 0;         } else {           y = i;         }       }     }     if (y == 0) y = n + 1;     printf( %d , n - y + 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxN = 11111;   pair<int, int> a[maxN];   short dp[maxN][maxN];   int s[maxN];   bool mycmp(pair<int, int> a, pair<int, int> b) {     if (a.second != b.second) return (a.second < b.second);     return (a.first > b.first);   }   int main() {     ios_base::sync_with_stdio(false);     int n, l, r;     cin >> n >> l >> r;     for (int i = 1; i <= n; ++i) {       cin >> a[i].first;     }     for (int i = 1; i <= n; ++i) {       cin >> a[i].second;     }     sort(a + 1, a + n + 1, mycmp);     int sum = 0;     for (int i = 1; i <= n; ++i) {       sum += a[i].first;     }     s[0] = 0;     for (int i = 1; i <= n; ++i) {       s[i] = s[i - 1] + a[i].first;     }     for (int i = 0; i <= n; ++i) {       for (int j = 0; j <= sum; ++j) {         dp[i][j] = -1;       }     }     dp[0][0] = 0;     for (int i = 0; i < n; ++i) {       for (int j = 0; j <= s[i]; ++j) {         if (dp[i][j] < 0) continue;         int type = a[i + 1].second, w = a[i + 1].first;         if (type == 0) {           dp[i + 1][j + w] = max(dp[i + 1][j + w], dp[i][j]);           dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);         } else {           int x = j, y = sum - (s[i] - j);           short f1 = (x >= l && x <= r);           if (dp[i][j] + f1 > dp[i + 1][j + w]) {             dp[i + 1][j + w] = dp[i][j] + f1;           }           short f2 = (y - a[i + 1].first >= l && y - a[i + 1].first <= r);           if (dp[i][j] + f2 > dp[i + 1][j]) {             dp[i + 1][j] = dp[i][j] + f2;           }         }       }     }     short ans = -1;     for (int i = 0; i <= sum; ++i) {       ans = max(ans, dp[n][i]);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   pair<int, pair<int, int> > a[200020];   int f[200020];   int v[200020];   int n, m;   int F(int x) {     if (f[x] == x) {       return x;     }     f[x] = F(f[x]);     v[f[x]] |= v[x];     return f[x];   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) {       f[i] = i;       v[i] = 0;     }     for (int i = 0; i < m; i++) {       scanf( %d%d%d , &a[i].second.first, &a[i].second.second, &a[i].first);     }     sort(a, a + m);     reverse(a, a + m);     long long ans = 0;     for (int i = 0; i < m; i++) {       int x = a[i].second.first;       int y = a[i].second.second;       x = F(x);       y = F(y);       if (x == y && v[x] == 0) {         v[x] = 1;         ans += a[i].first;       } else if (x != y && (v[x] == 0 || v[y] == 0)) {         v[y] |= v[x];         f[x] = y;         ans += a[i].first;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> g1[30000 + 100], g2[30000 + 100];   set<int> st;   int Next[30000 + 100], Prev[30000 + 100];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, m, start;     cin >> n >> m;     for (int i = 1; i <= m; i++) {       int u, v;       cin >> u >> v;       g1[u].push_back(v);       g2[v].push_back(u);     }     for (int i = 1; i <= n; i++) {       sort(g1[i].begin(), g1[i].end());       sort(g2[i].begin(), g2[i].end());     }     start = 0;     st.insert(0);     st.insert(1);     Next[start] = 1;     Prev[1] = 0;     for (int i = 2; i <= n; i++) {       vector<int> d;       bool flag = false;       for (auto it : g1[i]) {         if (it > i) continue;         int t1 = Next[it];         if (!binary_search(g1[i].begin(), g1[i].end(), t1)) {           flag = true;           Next[it] = i;           Prev[t1] = i;           Next[i] = t1;           Prev[i] = it;           break;         } else {           st.erase(it);           d.push_back(it);         }       }       if (!flag) {         for (auto it : g2[i]) {           if (it > i) continue;           int t1 = Prev[it];           if (!binary_search(g1[t1].begin(), g1[t1].end(), i)) {             flag = true;             Next[t1] = i;             Prev[it] = i;             Next[i] = it;             Prev[i] = t1;             break;           } else {             st.erase(t1);             d.push_back(t1);           }         }       }       if (!flag) {         auto it = st.begin();         int t1 = Next[*it];         Next[*it] = i;         Prev[t1] = i;         Next[i] = t1;         Prev[i] = *it;       }       for (auto it : d) {         st.insert(it);       }       st.insert(i);     }     int now = Next[0];     while (now) {       cout << now <<    ;       now = Next[now];     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = (int)1e6 + 7;   int n;   string s;   vector<int> p;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> s;     n = (int)s.size();     for (int i = 0; i < n; i++) {       if (s[i] != s[(i + 1) % n]) {         p.push_back(i);       }     }     int ant = -1;     int yes = 0;     int no = 0;     for (int i = 1; i < (int)p.size(); i++) {       if (ant == -1 || p[i] != ant + 1) {         ant = p[i];         yes++;       }     }     if (p.back() == n - 1 && p[0] == 0) {       p.pop_back();     }     ant = -1;     for (int i = 0; i < (int)p.size(); i++) {       if (ant == -1 || p[i] != ant + 1) {         ant = p[i];         no++;       }     }     cout << max(yes, no) <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int n;   bool isp(int a) {     for (int i = 2; i * i <= a; i++)       if (a % i == 0) return false;     return true;   }   int power[100001];   long long pow(long long a, long long b, long long MOD = n) {     long long ret = 1, c = a;     while (b) {       if (b & 1) ret = (ret * c) % MOD;       b >>= 1;       c = (c * c) % MOD;     }     return ret;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n;     if (n < 4) {       cout <<  YES n ;       for (int i = 1; i <= n; i++) cout << i <<    ;     } else if (n == 4)       cout <<  YES n1 3 2 4  <<   n ;     else if (isp(n) == false)       cout <<  NO  <<   n ;     else {       cout <<  YES  <<   n ;       int g;       for (g = 2; g < n; g++) {         int i, j = 1;         for (i = 1;; i++) {           j = (g * j) % n;           if (j == 1) break;         }         if (i == n - 1) break;       }       power[0] = 1;       for (int i = 1; i < n; i++) power[i] = (power[i - 1] * 1ll * g) % n;       for (int i = 0; i < n - 1; i++)         cout << power[i % 2 ? i : n - i - 1] <<   n ;       cout << n <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = (int)1e5 + 17;   const int MX = (int)1e6 + 17;   const int MOD = (int)1e9 + 7;   const long long oo = LLONG_MAX;   const int INF = INT_MAX;   const long double Pi = 3.14159265358979323846264338327950288419716939937510;   const int di[4] = {-1, 0, 1, 0};   const int dj[4] = {0, 1, 0, -1};   inline long long IN() {     long long x = 0, ch = getchar(), f = 1;     while (!isdigit(ch) && (ch !=  - ) && (ch != EOF)) ch = getchar();     if (ch ==  - ) {       f = -1;       ch = getchar();     }     while (isdigit(ch)) {       x = (x << 1) + (x << 3) + ch -  0 ;       ch = getchar();     }     return x * f;   }   inline void OUT(long long x) {     if (x < 0) putchar( - ), x = -x;     if (x >= 10)       OUT(x / 10), putchar(x % 10 +  0 );     else       putchar(x +  0 );   }   void IOI2017() {     freopen(          partition           .in ,          r , stdin);     freopen(          partition           .out ,          w , stdout);   }   int n, cur = 4;   struct Week {     int w[10];   };   Week a[2 * N];   int Year(int x) {     if (x % 400 == 0 || x % 4 == 0 && x % 100) return 366;     return 365;   }   bool check(Week a, Week b) {     for (int i = 1; i <= 7; i++) {       if (a.w[i] != b.w[i]) return 0;     }     return 1;   }   int main() {     for (int i = 1000; i <= N * 2; i++) {       for (int j = 1; j <= Year(i); j++) {         a[i].w[cur]++;         cur++;         if (cur == 8) cur = 1;       }     }     ios_base::sync_with_stdio(0);     cin >> n;     for (int j = n + 1;; j++) {       if (check(a[n], a[j])) {         cout << j;         return 0;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INFL = 1LL << 60;   const int INF = 1 << 30;   const long long MOD = 998244353;   double EPS = 1e-10;   vector<long long> dy = {0, 1, 0, -1, 1, 1, -1, -1, 0};   vector<long long> dx = {1, 0, -1, 0, 1, -1, 1, -1, 0};   void pres(double A) { printf( %.12lf n , A); }   void BinarySay(long long x, long long y = 60) {     for (int i = 0; i < (y); i++) cout << (x >> (y - 1 - i) & 1);     cout << endl;   }   long long cnt_bit(long long x) { return __builtin_popcountll(x); }   long long pow_long(long long x, long long k) {     long long res = 1;     while (k > 0) {       if (k % 2) res *= x;       x *= x;       k /= 2;     }     return res;   }   long long pow_mod(long long x, long long k) {     x %= MOD;     long long res = 1;     while (k > 0) {       if (k % 2) {         res *= x;         res %= MOD;       }       x *= x;       x %= MOD;       k /= 2;     }     return res;   }   long long inverse(long long x) { return pow_mod(x, MOD - 2); };   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long lcm(long long x, long long y) {     long long res = x / gcd(x, y);     res *= y;     return res;   };   const int MAXcomb = 200010;   long long fac[MAXcomb], finv[MAXcomb], inv[MAXcomb];   void COMinit() {     fac[0] = fac[1] = 1;     finv[0] = finv[1] = 1;     inv[1] = 1;     for (int i = 2; i < MAXcomb; i++) {       fac[i] = fac[i - 1] * i % MOD;       inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;       finv[i] = finv[i - 1] * inv[i] % MOD;     }   }   long long comb(int n, int k) {     if (n < k) return 0;     if (n < 0 || k < 0) return 0;     return fac[n] * finv[k] % MOD * finv[n - k] % MOD;   }   const int MAXkai = 200010;   long long kai_memo[MAXkai];   long long kai(long long N) {     if (kai_memo[N] != 0) return kai_memo[N];     if (N <= 1) return 1;     return kai_memo[N] = N * kai(N - 1) % MOD;   }   long long disit(long long second, long long base = 10) {     long long res = 0;     while (second) {       res++;       second /= base;     }     return res;   }   vector<long long> divisor(long long n) {     vector<long long> res(0);     for (long long i = 1; i * i <= n; i++) {       if (n % i == 0) {         res.push_back(i);         if (i != n / i) res.push_back(n / i);       }     }     sort(res.begin(), res.end());     return res;   }   void solve() {     int n;     cin >> n;     vector<int> a(n);     for (int i = 0; i < (n); i++) cin >> a[i];     sort(a.begin(), a.end());     long long sum = 0;     for (int i = 0; i < (n); i++) sum += a[i];     int m;     cin >> m;     for (int i = 0; i < (m); i++) {       int q;       cin >> q;       cout << sum - a[n - q] << endl;     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0), cout.tie(0);     solve();   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 305;   struct sss {     int a, b, c;     bool operator<(const sss& a) const { return c < a.c; }   };   vector<sss> v[MAXN * MAXN];   int cost(int a, int b, int c, int d) { return abs(a - c) + abs(b - d); }   int main() {     int m, n, i, j, k, p;     cin >> m >> n >> p;     for (i = 1; i <= m; i++) {       for (j = 1; j <= n; j++) {         int num;         cin >> num;         v[num].push_back({i, j, 999999999});       }     }     i = 1;     for (j = 0; j < v[i].size(); j++) {       v[i][j].c = cost(v[i][j].a, v[i][j].b, 1, 1);     }     for (i = 2; i <= p; i++) {       sort((v[i - 1]).begin(), (v[i - 1]).end());       for (j = 0; j < v[i].size(); j++) {         for (k = 0; k < min((int)v[i - 1].size(), m + n + 2); k++) {           v[i][j].c =               min(v[i][j].c, v[i - 1][k].c + cost(v[i - 1][k].a, v[i - 1][k].b,                                                   v[i][j].a, v[i][j].b));         }       }     }     int ans = 999999999;     i = p;     for (j = 0; j < v[i].size(); j++) ans = min(ans, v[i][j].c);     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 11;   int s[N];   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &s[i]);     sort(s + 1, s + 1 + n);     for (int i = 1, j = 1; i <= n; i++) {       if (s[i] % s[j] != 0) {         printf( %d , -1);         return 0;       }     }     printf( %d n , s[1]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T1, typename T2, typename T3>   struct pair3 {     T1 first;     T2 second;     T3 third;   };   template <typename T1, typename T2, typename T3, typename T4>   struct pair4 {     T1 first;     T2 second;     T3 third;     T4 fourth;   };   const long long MOD = 1000000007;   const long double PI = acos(-1.0);   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long n, m;     cin >> n;     vector<long long> ar(n);     for (long long i = 0; i < n; ++i) {       cin >> ar[i];     }     vector<vector<long long>> ans(n + 1);     for (long long len = 1; len <= n; ++len) {       vector<long long> temp;       long long mx = -1, mn = INT_MAX;       multiset<long long> st;       auto del = [&](long long val) {         auto it = temp.end();         --it;         while (1) {           if (*it == val) {             temp.erase(it);             break;           }           it -= 1;         }       };       for (long long start = 0; start < n; ++start) {         if (temp.size() < len) {           temp.push_back(ar[start]);           st.insert(ar[start]);         } else {           if (ar[start] > *st.begin()) {             del(*st.begin());             st.erase(st.begin());             st.insert(ar[start]);             temp.push_back(ar[start]);           }         }         ans[len] = temp;       }     }     cin >> m;     long long l, idx;     while (m--) {       cin >> l >> idx;       cout << ans[l][idx - 1] << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   char s[1008][1008], last[1008][1008];   int a[58][58];   int flag[58], x[58], y[58];   int ans, tot;   bool in[8][8];   struct Tprogram {     void open() {       freopen( 1.in ,  r , stdin);       freopen( 1.out ,  w , stdout);     }     void close() {}     void init() {       scanf( %d%d , &n, &m);       for (int i = 1; i <= n; i++) scanf( %s , &s[i][1]);       for (int i = 1; i <= n; i++)         for (int j = 1; j <= m; j++) {           if (s[i][j] !=  .  && !a[i][j])             a[i][j] = a[i + 1][j] = a[i][j + 1] = a[i + 1][j + 1] = ++tot;           if (s[i][j] !=  . ) {             int t = s[i][j];             t = t <=  B  ? t - 64 : t - 94;             if (!x[t])               x[t] = a[i][j];             else               y[t] = a[i][j];           }         }     }     void dfs(int c, int d) {       if (d > 14) {         memset(in, 0, sizeof(in));         for (int i = 1; i <= 28; i++)           if (!in[flag[x[i]]][flag[y[i]]])             in[flag[x[i]]][flag[y[i]]] = in[flag[y[i]]][flag[x[i]]] = 1;           else             return;         ++ans;         if (ans == 1) {           for (int i = 1; i <= n; i++)             for (int j = 1; j <= m; j++) {               if (a[i][j])                 last[i][j] = flag[a[i][j]] - 1 +  0 ;               else                 last[i][j] =  . ;             }         }         return;       }       if (flag[d])         dfs(c, d + 1);       else {         flag[d] = c;         for (int i = d + 1; i <= 14; i++)           if (!flag[i]) {             flag[i] = c;             dfs(c + 1, d + 1);             flag[i] = 0;           }         flag[d] = 0;       }     }     void work() {       dfs(1, 1);       printf( %d n , ans * 5040);       for (int i = 1; i <= n; i++) {         for (int j = 1; j <= m; j++) printf( %c , last[i][j]);         printf(  n );       }     }   } Program;   int main() {     Program.init();     Program.work();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a;     cin >> a;     double x, y, s = 0;     for (int i = 0; i < a; i++) {       cin >> x >> y;       s += y;     }     s /= a;     printf( %.3lf n , s + 5);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t = 1;     while (t--) {       int sum, limit;       cin >> sum >> limit;       vector<pair<int, int>> v;       for (int i = 1; i <= limit; i++) {         v.push_back({1 << __builtin_ctz(i), i});       }       sort(v.rbegin(), v.rend());       vector<int> a;       int i = 0;       while (sum) {         if (i == v.size()) break;         if (sum >= v[i].first) {           a.push_back(v[i].second);           sum -= v[i].first;         }         i++;       }       if (sum != 0)         cout <<  -1 n ;       else {         cout << a.size() <<   n ;         for (auto i : a) {           cout << i <<    ;         }         cout <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int GEN_SIZE = 6;   vector<int> v, adj[50005];   int match[50005], match2[50005], vis[50005], ans[50005];   int V, Vleft;   int conv(vector<int> v, int s) {     int ans = 0;     for (int i = 0; i < v.size(); i++) {       ans *= s;       ans += v[i];     }     return ans;   }   vector<int> unconv(int p, int s) {     vector<int> v1;     for (int i = 0; i < s; i++) {       v1.push_back(p % s);       p /= s;     }     reverse(v1.begin(), v1.end());     return v1;   }   void recur(int x) {     if (x == GEN_SIZE) {       int rval = conv(v, GEN_SIZE);       for (int rem = 0; rem < GEN_SIZE; rem++) {         vector<int> tmp_v;         tmp_v.push_back(rem);         for (int i = 0; i < GEN_SIZE; i++)           if (rem != i) tmp_v.push_back(v[i]);         int lval = conv(tmp_v, GEN_SIZE);         adj[rval].push_back(lval);       }       return;     }     for (int i = 0; i < GEN_SIZE; i++) {       v.push_back(i);       recur(x + 1);       v.pop_back();     }   }   int aug(int v) {     if (vis[v]) return 0;     vis[v] = 1;     for (int j = 0; j < (int)adj[v].size(); j++) {       int r = adj[v][j];       if (match[r] == -1) {         match[r] = v;         return 1;       }     }     for (int j = 0; j < (int)adj[v].size(); j++) {       int r = adj[v][j];       if (aug(match[r])) {         match[r] = v;         return 1;       }     }     return 0;   }   int T;   vector<pair<pair<int, int>, int> > q;   int main() {     scanf( %d , &T);     for (int i = 0; i < T; i++) {       int N, R;       scanf( %d%d , &N, &R);       vector<int> v1;       v1.push_back(R - 1);       for (int j = 0; j < N - 1; j++) {         int x;         scanf( %d , &x);         v1.push_back(x - 1);       }       int val = conv(v1, N);       q.push_back(make_pair(make_pair(N, val), i));     }     sort(q.begin(), q.end());     int curN = 0;     for (int i = 0; i < q.size(); i++) {       if (curN != q[i].first.first) {         curN = q[i].first.first;         GEN_SIZE = curN;         memset(match, -1, sizeof(match));         int v = 1;         for (int i = 0; i < GEN_SIZE; i++) {           v *= GEN_SIZE;         }         for (int i = 0; i < v; i++) adj[i].clear();         recur(0);         for (int i = 0; i < v; i++) {           memset(vis, 0, sizeof(vis));           aug(i);         }         for (int i = 0; i < v; i++) match2[match[i]] = i;       }       vector<int> qv = unconv(q[i].first.second, curN);       vector<int> v1 = unconv(match[q[i].first.second], curN);       ans[q[i].second] = v1[qv[0]] + 1;     }     for (int i = 0; i < T; i++) printf( %d n , ans[i]);   }


#include <bits/stdc++.h>   using namespace std;   int a[1000][1000];   int w, h;   int fastMax(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y; }   int fastMin(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ x; }   int main() {     scanf( %d%d , &h, &w);     for (int y = 0; y < h; y++)       for (int x = 0; x < w; x++) scanf( %d , &a[y][x]);     int ans = -1;     for (int y1 = 0; y1 < h; y1++)       for (int y2 = y1 + 1; y2 < h; y2++) {         int m1 = -1, m2 = -1;         for (int x = 0; x < w; x++) {           const int v = fastMin(a[y1][x], a[y2][x]);           if (m1 < v) {             m2 = m1;             m1 = v;           } else if (m2 < v) {             m2 = v;           }         }         ans = fastMax(ans, m2);       }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, sum = 0, inp;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> inp;       sum += inp;     }     cout << (float)sum / (float)n;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, d, ff = 1, bb = 1;   long long mx;   pair<long long, long long> f[100005];   int main() {     scanf( %d%d , &n, &d);     for (int i = 1; i <= n; i++) scanf( %d%d , &f[i].first, &f[i].second);     sort(f + 1, f + n + 1);     for (int i = 2; i <= n; i++) f[i].second += f[i - 1].second;     while (true) {       while (f[bb].first - f[ff].first < d && bb != n + 1) bb++;       mx = max(mx, f[bb - 1].second - f[ff - 1].second);       ff++;       if (ff == n + 1) break;     }     printf( %I64d , mx);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, a, b, sum = 0;     cin >> n >> m;     for (int i = 0; i < n * m; i++) {       cin >> a >> b;       if (a == 1 || b == 1) sum++;     }     cout << sum;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 7;   const int SS = 4 * N;   long long n, q, k, a, b;   long long seg[SS][2];   void modif(int ind, int val, int s = 0, int e = n, int v = 1) {     if (ind < s || ind >= e) return;     if (e - s == 1) {       seg[v][0] += val, seg[v][0] = min(seg[v][0], a);       seg[v][1] += val, seg[v][1] = min(seg[v][1], b);       return;     }     int mid = (s + e) / 2;     modif(ind, val, s, mid, 2 * v);     modif(ind, val, mid, e, 2 * v + 1);     for (int i = 0; i < 2; i++) seg[v][i] = seg[2 * v][i] + seg[2 * v + 1][i];   }   long long get(int l, int r, int ind, int s = 0, int e = n, int v = 1) {     if (l >= e || r <= s) return 0;     if (l <= s && e <= r) return seg[v][ind];     int mid = (s + e) / 2;     long long res = 0;     res += get(l, r, ind, s, mid, 2 * v);     res += get(l, r, ind, mid, e, 2 * v + 1);     return res;   }   int main() {     ios::sync_with_stdio(false);     cin >> n >> k >> a >> b >> q;     while (q--) {       int t;       cin >> t;       if (t == 1) {         int d, x;         cin >> d >> x;         d--;         modif(d, x);       } else {         int d;         cin >> d;         d--;         cout << get(0, d, 1) + get(d + k, n, 0) <<   n ;       }     }     return 0;   }


#include<bits/stdc++.h>   using namespace std;   #define ll long long   #define error(args...) { string _s = #args; replace(_s.begin(), _s.end(),  , ,    ); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }      void err(istream_iterator<string> it) {}   template<typename T, typename... Args>   void err(istream_iterator<string> it, T a, Args... args)   {       cerr << *it <<   =   << a << endl;       err(++it, args...);   }      int main()   {       ll t;       cin>>t;       while(t--)       {           ll i,j,n,m,x;           cin>>n>>m>>x;           ll c=((x-1)/n)+1;           ll r=x-(c-1)*n;           ll ans=(r-1)*m+c;           cout<<ans<<endl;       }   }


#include <bits/stdc++.h>   using namespace std;   const int INF = (int)1e9;   const int maxN = 1.1e4;   vector<int> g[maxN];   map<string, int> m[maxN];   int len = 1;   void add(int v, string s) {     if (m[v][s] == 0) {       m[v][s] = len;       g[v].push_back(len);       len++;     }   }   void parse(string s) {     s = s +    ;     string cur;     int v = 0;     for (int i = 0; i < s.size(); i++) {       if (s[i] ==      && cur.size()) {         add(v, cur);         v = m[v][cur];         cur =   ;       } else {         cur.push_back(s[i]);       }     }     cout << endl;   }   int mx[2];   pair<int, int> dfs(int v, int d) {     pair<int, int> ans = {0, g[v].size() == 0};     for (auto u : g[v]) {       pair<int, int> p = dfs(u, d + 1);       ans.first += p.first;       ans.second += p.second;     }     if (d >= 2) {       mx[0] = max(mx[0], ans.first);       mx[1] = max(mx[1], ans.second);     }     if (g[v].size()) ans.first++;     return ans;   }   int main() {     string s;     while (cin >> s) {       parse(s);     }     dfs(0, 0);     cout << mx[0] <<     << mx[1] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;      #define pb push_back   #define f first   #define s second   #define ll long long         ll MOD = 1000000007;      int main(){    ll t;    cin>>t;    while(t--){     ll n;     cin>>n;     int c =1;     if(n==1)cout<<1<<endl;     if(n==2)cout<<-1<<endl;     if(n>=3){      for(int i=0;i<n;i++){       for(int j=0;j<n;j++){        cout<<c<<   ;        c+=2;        if(c>n*n)c=2;       }       cout<<endl;      }     }    }   }


#include <bits/stdc++.h>   using namespace std;   int n;   vector<pair<int, int> > A;   int F(int i, int j) {     return (A[i].first - A[j].first) * (A[i].first - A[j].first) +            (A[i].second - A[j].second) * (A[i].second - A[j].second);   }   bool F1(vector<int> I) {     for (int(i) = (0); (i) < (4); ++(i))       for (int(j) = (0); (j) < (i); ++(j)) {         int d = F(I[i], I[j]);         vector<int> t;         for (int(k) = (0); (k) < (4); ++(k))           if (k != i && k != j) t.push_back(k);         int dd = F(I[t[0]], I[t[1]]);         if (d != dd) continue;         vector<int> v;         vector<int> q;         v.push_back(F(I[i], I[t[0]]));         q.push_back(F(I[t[0]], I[j]));         v.push_back(F(I[j], I[t[1]]));         q.push_back(F(I[t[1]], I[i]));         if (q[0] == q[1] && v[0] == v[1]) return true;       }     return false;   }   bool F2(vector<int> I) {     for (int(i) = (0); (i) < (4); ++(i))       for (int(j) = (0); (j) < (i); ++(j)) {         int d = F(I[i], I[j]);         vector<int> t;         for (int(k) = (0); (k) < (4); ++(k))           if (k != i && k != j) t.push_back(k);         int dd = F(I[t[0]], I[t[1]]);         if (d != dd) continue;         vector<int> v;         v.push_back(F(I[i], I[t[0]]));         v.push_back(F(I[t[0]], I[j]));         v.push_back(F(I[j], I[t[1]]));         v.push_back(F(I[t[1]], I[i]));         bool ok = true;         for (int(k) = (1); (k) < (4); ++(k))           if (v[k] != v[k - 1]) ok = false;         if (ok) return true;       }     return false;   }   int main() {     for (int(i) = (0); (i) < (8); ++(i)) {       int x, y;       cin >> x >> y;       A.push_back(make_pair(x, y));     }     int res = -1;     for (int(i) = (0); (i) < ((1 << 8)); ++(i)) {       vector<int> I;       vector<int> J;       for (int(j) = (0); (j) < (8); ++(j))         if ((i & (1 << j)) != 0)           I.push_back(j);         else           J.push_back(j);       if (I.size() != 4) continue;       if (F1(I) && F2(J)) res = i;     }     if (res == -1)       cout <<  NO  << endl;     else {       cout <<  YES  << endl;       for (int(i) = (0); (i) < (8); ++(i))         if ((res & (1 << i)) == 0) cout << i + 1 <<    ;       cout << endl;       for (int(i) = (0); (i) < (8); ++(i))         if ((res & (1 << i)) != 0) cout << i + 1 <<    ;       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100010;   int n, a[N] = {}, tot = 0;   bool v[N] = {};   void dfs(int s) {     v[s] = true;     if (!v[s * 2 % n]) dfs(s * 2 % n);     if (!v[s * 2 % n + 1]) dfs(s * 2 % n + 1);     a[++tot] = s;   }   int main() {     cin >> n;     if (n % 2)       puts( -1 );     else {       dfs(0);       for (int i = tot; i >= 0; --i) printf( %d%c , a[i],    n [i == 0]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   void chkmin(T &x, T y) {     if (y < x) x = y;   }   template <class T>   void chkmax(T &x, T y) {     if (y > x) x = y;   }   const int N = 105;   int a[N], n, x, ans1, ans2[N][N], len;   int main() {     scanf( %d , &n);     for (int i = 1; i <= (n); i++) scanf( %d , a + i);     for (int i = 1; i <= (n - (n & 1)); i++) {       for (int j = 1; j <= (n); j++)         if (a[j] == i) {           x = j;           break;         }       ans1++;       len = 0;       for (int j = 1; j <= (i - 1); j++) ans2[ans1][++len] = 1;       ans2[ans1][++len] = x - i + 1;       reverse(a + i, a + x + 1);       if (x < n) {         ans2[ans1][++len] = n - x;         reverse(a + x + 1, a + n + 1);       }       if (i % 2 == 0) reverse(ans2[ans1] + 1, ans2[ans1] + len + 1);       if (len == 1) ans1--;       ans2[ans1][0] = len;     }     printf( %d n , ans1);     for (int i = 1; i <= (ans1); i++) {       printf( %d  , ans2[i][0]);       for (int j = 1; j <= (ans2[i][0]); j++) printf( %d  , ans2[i][j]);       putchar(  n );     }   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3fffffff;   const int maxn = 100000 + 5, maxh = 800;   int n, k;   int f[maxh], g[maxh];   vector<int> P[maxn];   int main() {     scanf( %d%d , &n, &k);     for (int i = 1, iend = k; i <= iend; ++i) {       int x, y;       scanf( %d%d , &x, &y);       P[y].push_back(n - x + 1);     }     for (int i = 1, iend = n; i <= iend; ++i) sort(P[i].begin(), P[i].end());     int hgt = sqrt(6 * k) + 5;     memset(f, 0x3f, sizeof f);     f[0] = 0;     for (int i = 1, iend = n; i <= iend; ++i) {       memcpy(g, f, sizeof f);       memset(f, 0x3f, sizeof f);       int minf = 0, pt = 0;       for (int j = 0, jend = hgt; j <= jend; ++j) {         if (pt < P[i].size() && P[i][pt] == j) pt++;         if (g[j] < g[minf]) minf = j;         f[j] = min(g[j + 1], g[minf] + (j ? j * (j + 1) / 2 + 2 : 0)) +                (P[i].size() - pt) * 3;       }     }     int ans = inf;     for (int i = 0, iend = hgt; i <= iend; ++i) ans = min(ans, f[i]);     printf( %d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[27];   bool h[27];   bool valid[1005][1005];   long long dp[1005];   int main() {     for (int i = 0; i < 1005; i++) {       for (int j = 0; j < 1005; j++) valid[i][j] = false;     }     for (int i = 0; i < 26; i++) h[i] = false;     int n, lsub = 1;     cin >> n;     string s;     cin >> s;     for (int i = 0; i < 26; i++) cin >> a[i];     for (int i = 0; i < n; i++) {       int f = 0;       for (int j = i; j < n; j++) {         h[s[j] -  a ] = true;         for (int k = 0; k < 26; k++)           if (h[k] && a[k] < j - i + 1) {             f = 1;             break;           }         if (f == 1) break;         valid[i][j] = true;         lsub = max(lsub, j - i + 1);       }       for (int k = 0; k < 26; k++) h[k] = false;     }     dp[0] = 1;     dp[-1] = 1;     for (int i = 1; i < n; i++) {       dp[i] = dp[i - 1];       for (int j = 0; j < i; j++) {         if (valid[j][i]) dp[i] = (dp[i] + dp[j - 1]) % 1000000007;       }     }     int i = 0, j = 0, k = 0;     while (i < n) {       if (!valid[j][i]) {         j = i;         k++;       }       i++;     }     cout << dp[n - 1] <<   n ;     cout << lsub <<   n ;     cout << k + 1 <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   char ans[1010][1010];   int len[1010];   void upd(int n, int c) { ans[n][len[n]++] = c; }   char all[1 << 21];   int main() {     int n, k, l;     scanf( %d%d%d , &n, &l, &k);     scanf( %s , all);     sort(all, all + strlen(all));     int cur = 0;     int beg = 1;     while (beg < k) {       int beg_ = beg;       while (beg <= k) {         upd(beg, all[cur]);         beg++;         cur++;       }       beg = beg_;       for (int i = k; i >= beg_; i--)         if (ans[i][len[i] - 1] != ans[k][len[k] - 1]) {           beg = i + 1;           break;         }       if (len[k] == l) break;     }     while (len[k] < l) upd(k, all[cur++]);     for (int i = 1; i <= n; i++) {       while (len[i] < l) upd(i, all[cur++]);     }     for (int i = 1; i <= n; i++) puts(ans[i]);   }


#include<bits/stdc++.h>   using namespace std;   #define  FIO        ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);   #define  all(v)     v.begin(),v.end()   #define  rall(v)    v.rbegin(),v.rend()   #define MEM(a,x)    memset(a,x,sizeof(a))   #define pi          acos(-1.0)   #define  ll     long long   #define  pb     push_back   #define  mp     make_pair   #define  pii    pair< int,int >   #define  pll    pair< ll,ll >   #define  vii    vector< int >   #define  vll    vector< ll >   #define  vpi    vector< pii >   #define  MX     200005   #define  EPS    1e-12   #define  ss     second   #define  ff     first   int cases=1;   #ifdef ppqq        #define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)       template < typename Arg1 >       void __f(const char* name, Arg1&& arg1){           cerr << name <<   is   << arg1 << std::endl;       }       template < typename Arg1, typename... Args>       void __f(const char* names, Arg1&& arg1, Args&&... args){           const char* comma = strchr(names+1,  , );           cerr.write(names, comma - names) <<   is   << arg1 <<    ;           __f(comma+1, args...);       }   #else       #define debug(...)   #endif   ///******************************************START******************************************   struct info   {       ll u,v;       ll w;      };   vector<info> graph;   ll par[MX],cnt,rnk[MX];   bool cmp(const info&a,const info&b)   {       if(a.w<b.w) return true;       return false;   }   ll find_set(ll v)   {       if(v==par[v]) return v;       return par[v]=find_set(par[v]);   }   void mrg(ll a,ll b)   {       if(rnk[a]<=rnk[b])       {           par[a]=b;           rnk[b]++;       }       else       {           par[b]=a;           rnk[a]++;       }   }   ll MST(ll n)   {       ll sum=0;       for(ll i=0;i<graph.size();i++)       {           ll a=find_set(graph[i].u);           ll b=find_set(graph[i].v);           if(a!=b)           {               mrg(a,b);               cnt++;               sum+=graph[i].w;           }           if(cnt==n-1) break;       }       return sum;   }   bool tree(ll n)   {       for(ll i=1;i<n;i++)       {           if(find_set(i)!=find_set(i+1)) return false;       }       return true;   }   void CLR(ll n)   {       graph.clear();       cnt=0;       for(ll i=1;i<=n;i++) par[i]=0,rnk[i]=0;   }   int main()   {       FIO;       int t;       cin>>t;       while(t--) {           ll n,m;           ll k,mini=LLONG_MAX;           cin>>n>>m>>k;           vector<pair<ll,pll>> kom,shb;           for(ll i=0;i<m;i++)           {               ll u,v;               ll w;               cin>>u>>v>>w;               info temp;               temp.u=u;               temp.v=v;               temp.w=w;               if(w<=k) graph.pb(temp);               shb.pb({w,{u,v}});           }           sort(all(shb));           sort(all(graph),cmp);           for (ll i = 1; i <= n; i++) par[i] = i;           MST(n);              if(tree(n))           {               ll ans=LLONG_MAX;               for(auto it: shb) ans=min(ans,abs(it.ff-k));               cout<<ans<<  n ;           }           else           {               for (ll i = 1; i <= n; i++) par[i] = i;               for(ll i=0;i<graph.size();i++)                   graph[i].w=0;               cnt=0;               for(auto it: shb)               {                   if(it.ff>k){                       info temp;                       temp.u=it.ss.ff;temp.v=it.ss.ss;                       temp.w=it.ff-k;                       graph.pb(temp);                   }               }               sort(all(graph),cmp);               cout<<MST(n)<<  n ;           }              CLR(n+2);       }   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   int n, m, chk[100100], a[100100], b[100100];   vector<int> v[100100];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n >> m;     for (int i = 1; i <= m; i++) {       int a, b;       cin >> a >> b;       v[a].push_back(b), v[b].push_back(a);     }     for (int i = 1; i <= n; i++)       if (v[i].size() != n - 1) {         chk[i] = 1;         for (int x : v[i]) chk[x] = 1;         int j = 1, c = 0;         while (chk[j]) j++;         for (int k = 1; k <= n; k++)           if (k != i && k != j) a[k] = b[k] = ++c;         a[i] = b[i] = b[j] = ++c;         a[j] = ++c;         cout <<  YES n ;         for (int i = 1; i <= n; i++) cout << a[i] <<    ;         cout <<   n ;         for (int i = 1; i <= n; i++) cout << b[i] <<    ;         cout <<   n ;         return 0;       }     cout <<  NO ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string org;     cin >> org;     int n;     cin >> n;     map<string, int> mp;     for (int i = 0; i < n; i++) {       string s, t;       cin >> s >> t;       string name;       string trash;       if (t[0] ==  p  || t[0] ==  c ) {         string trash;         cin >> trash;         cin >> name >> trash;         name.pop_back();         name.pop_back();         if (t[0] ==  p ) {           if (s == org)             mp[name] += 15;           else if (name == org)             mp[s] += 15;         } else {           if (s == org)             mp[name] += 10;           else if (name == org)             mp[s] += 10;         }       } else {         cin >> name >> trash;         name.pop_back();         name.pop_back();         if (s == org)           mp[name] += 5;         else if (name == org)           mp[s] += 5;       }       mp[s];       mp[name];     }     vector<pair<string, int>> v;     for (auto x : mp) {       if (x.first == org) continue;       v.emplace_back(x.first, x.second);     }     sort(v.begin(), v.end(), [](pair<string, int> p1, pair<string, int> p2) {       if (p1.second != p2.second) return p1.second > p2.second;       return p1.first < p2.first;     });     for (auto x : v) {       cout << x.first <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, mid;     bool flag;     string str;     char ans[3000];     cin >> n;     cin >> str;     if (n % 2 == 0) {       mid = (n / 2) - 1;       flag = true;     } else {       mid = (n - 1) / 2;       flag = false;     }     long long k = 1, cnt = 0;     ans[mid] = str[0];     for (long long i = 1; i < str.size(); i++) {       cnt++;       if (flag) {         ans[mid + k] = str[i];       } else {         ans[mid - k] = str[i];       }       if (cnt == 2) {         k++;         cnt = 0;       }       flag = !flag;     }     for (long long i = 0; i < n; i++) {       cout << ans[i];     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1000000007;   int N, dp[5000][5001], cumlcp[5000][5000];   vector<int> sa, ra, rsa, lcp;   string s;   void saInit() {     int l = (int)s.size();     sa.resize(l);     iota(sa.begin(), sa.end(), 0);     ra.assign(s.begin(), s.end());     for (int k = 1; k < l; k *= 2) {       sort(sa.begin(), sa.end(), [&](int a, int b) {         if (ra[a] != ra[b]) return ra[a] < ra[b];         int ak = a + k < l ? ra[a + k] : -1;         int bk = b + k < l ? ra[b + k] : -1;         return ak < bk;       });       vector<int> ra2(l);       int x = 0;       for (int i = 1; i < l; i++) {         if (ra[sa[i]] != ra[sa[i - 1]] || sa[i - 1] + k >= l ||             ra[sa[i] + k] != ra[sa[i - 1] + k])           x++;         ra2[sa[i]] = x;       }       ra = ra2;     }     rsa.resize(l);     for (int i = 0; i < int(l); i++) rsa[sa[i]] = i;   }   void saLCP() {     int l = (int)s.size();     lcp.resize(l);     vector<int> p(l);     for (int i = 0; i < l; i++) {       p[sa[i]] = (i) ? sa[i - 1] : -1;     }     int x = 0;     for (int i = 0; i < l; i++) {       while (p[i] != -1 && p[i] + x < l && s[i + x] == s[p[i] + x] &&              s[i + x] !=  $ )         x++;       lcp[rsa[i]] = x;       if (x) x--;     }     for (int i = 0; i < int(l); i++) {       cumlcp[i][i] = INT_MAX;       for (int j = i + 1; j < int(l); j++)         cumlcp[i][j] = min(cumlcp[i][j - 1], lcp[j]);     }   }   bool ism(int a, int b, int n) {     int as = a - n + 1, bs = b - n + 1;     if (as < 0) return false;     if (rsa[bs] <= rsa[as]) return false;     return cumlcp[rsa[as]][rsa[bs]] < n;   }   int main() {     cin >> N >> s;     saInit();     saLCP();     for (int i = 0; i < int(N); i++)       for (int j = 1; j < int(5001); j++) {         int& ans = dp[i][j];         ans = dp[i][j - 1];         if (j > i + 1) {           continue;         } else if (s[i - j + 1] ==  0 ) {         } else if (j == i + 1) {           ans = (ans + 1) % MOD;         } else if (ism(i - j, i, j)) {           ans = (ans + dp[i - j][j]) % MOD;         } else {           ans = (ans + dp[i - j][j - 1]) % MOD;         }       }     cout << dp[N - 1][N] << endl;   }


#include <bits/stdc++.h>   using namespace std;   unsigned long long int n, m;   int main() {     cin >> n;     m = (((n - 3) * 3) + ((n - 3) * (n - 4))) + 1;     cout << m << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 8e5 + 5;   struct trie {     int a[26];     vector<int> val[2];   } a[N];   int vis[N][2];   char ch[800005];   int cnt, n;   void insert(char ch[], int op, int k) {     int u = 0;     int len = strlen(ch + 1);     a[u].val[op].push_back(k);     for (int i = 1; i <= len; i++) {       if (!a[u].a[ch[i] -  a ]) a[u].a[ch[i] -  a ] = ++cnt;       u = a[u].a[ch[i] -  a ];       a[u].val[op].push_back(k);     }   }   int dfs(int k, int dep) {     int ans = 0;     for (int i = 0; i < 26; i++)       if (a[k].a[i]) ans += dfs(a[k].a[i], dep + 1);     vector<int> t[2];     for (int i = 0; i <= 1; i++)       for (int j = 0; j < a[k].val[i].size(); j++)         if (!vis[a[k].val[i][j]][i]) t[i].push_back(a[k].val[i][j]);     int tmp = min(t[0].size(), t[1].size());     ans += tmp * dep;     for (int i = 0; i < tmp; i++) {       vis[t[0][i]][0] = t[1][i];       vis[t[1][i]][1] = t[0][i];     }     return ans;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %s , ch + 1);       insert(ch, 0, i);     }     for (int i = 1; i <= n; i++) {       scanf( %s , ch + 1);       insert(ch, 1, i);     }     printf( %d n , dfs(0, 0));     for (int i = 1; i <= n; i++) printf( %d %d n , i, vis[i][0]);     return 0;   }