#include <bits/stdc++.h> n using namespace std; n enum Type { Normal, HBox, VBox }; n struct Widget { n   char name[11]; n   long long width, height; n   int border, spacing; n   vector<int> sons; n   Type type; n }; n int wCnt; n Widget widgets[101]; n map<string, int> name2id; n int str2int(char* start, char* end) { n   int res = 0; n   for (char* p = start; p != end; ++p) { n     res = res * 10 + (*p -  0 ); n   } n   return res; n } n void addWidget(char name[11], int width, int height, Type type) { n   strcpy(widgets[wCnt].name, name); n   widgets[wCnt].width = width; n   widgets[wCnt].height = height; n   widgets[wCnt].border = 0; n   widgets[wCnt].spacing = 0; n   widgets[wCnt].type = type; n   name2id[name] = wCnt; n   wCnt++; n } n bool isWidget(char buf[50]) { n   if (strncmp(buf,  Widget , 6) == 0) { n     char name[11]; n     int width, height; n     int p1, p2, p3; n     for (int i = 7; buf[i] !=   0 ; ++i) { n       if (buf[i] ==  ( ) { n         p1 = i; n       } else if (buf[i] ==  , ) { n         p2 = i; n       } else if (buf[i] ==  ) ) { n         p3 = i; n       } n     } n     strncpy(name, buf + 7, p1 - 7); n     name[p1 - 7] =   0 ; n     width = str2int(buf + p1 + 1, buf + p2); n     height = str2int(buf + p2 + 1, buf + p3); n     addWidget(name, width, height, Normal); n     return true; n   } else { n     return false; n   } n } n bool isBox(char buf[50]) { n   char name[11]; n   if (strncmp(buf,  HBox , 4) == 0) { n     strcpy(name, buf + 5); n     addWidget(name, 0, 0, HBox); n     return true; n   } else if (strncmp(buf,  VBox , 4) == 0) { n     strcpy(name, buf + 5); n     addWidget(name, 0, 0, VBox); n     return true; n   } else { n     return false; n   } n } n void operate(char buf[50]) { n   char name[11]; n   int p1, p2, p3; n   for (int i = 0; buf[i] !=   0 ; ++i) { n     if (buf[i] ==  . ) { n       p1 = i; n     } else if (buf[i] ==  ( ) { n       p2 = i; n     } else if (buf[i] ==  ) ) { n       p3 = i; n     } n   } n   strncpy(name, buf, p1); n   name[p1] =   0 ; n   int idx = name2id[name]; n   if (strncmp(buf + p1 + 1,  pack , p2 - p1 - 1) == 0) { n     char packName[11]; n     strncpy(packName, buf + p2 + 1, p3 - p2 - 1); n     packName[p3 - p2 - 1] =   0 ; n     int packIdx = name2id[packName]; n     widgets[idx].sons.push_back(packIdx); n   } else if (strncmp(buf + p1 + 1,  set_border , p2 - p1 - 1) == 0) { n     int border = str2int(buf + p2 + 1, buf + p3); n     widgets[idx].border = border; n   } else if (strncmp(buf + p1 + 1,  set_spacing , p2 - p1 - 1) == 0) { n     int spacing = str2int(buf + p2 + 1, buf + p3); n     widgets[idx].spacing = spacing; n   } n } n void compBox(int idx) { n   if (widgets[idx].sons.size() == 0 || widgets[idx].width != 0) { n     return; n   } n   int sonSize = widgets[idx].sons.size(); n   for (int i = 0; i < sonSize; ++i) { n     int sonIdx = widgets[idx].sons[i]; n     compBox(sonIdx); n     if (widgets[idx].type == HBox) { n       widgets[idx].width += widgets[sonIdx].width; n       widgets[idx].height = max(widgets[idx].height, widgets[sonIdx].height); n     } else if (widgets[idx].type == VBox) { n       widgets[idx].width = max(widgets[idx].width, widgets[sonIdx].width); n       widgets[idx].height += widgets[sonIdx].height; n     } n   } n   widgets[idx].width += 2 * widgets[idx].border; n   widgets[idx].height += 2 * widgets[idx].border; n   if (widgets[idx].type == HBox) { n     widgets[idx].width += (sonSize - 1) * widgets[idx].spacing; n   } else if (widgets[idx].type == VBox) { n     widgets[idx].height += (sonSize - 1) * widgets[idx].spacing; n   } n } n void compSize() { n   for (int i = 0; i < wCnt; ++i) { n     if (widgets[i].type != Normal && widgets[i].width == 0 && n         widgets[i].sons.size() != 0) { n       compBox(i); n     } n   } n } n bool cmp(Widget a, Widget b) { n   return (strcmp(a.name, b.name) <= 0) ? true : false; n } n void clear() { n   name2id.clear(); n   for (int i = 0; i < wCnt; ++i) { n     widgets[i].sons.clear(); n   } n } n int main() { n   int n; n   while (scanf( %d , &n) != EOF) { n     getchar(); n     wCnt = 0; n     while (n--) { n       char buf[50]; n       fgets(buf, 50, stdin); n       int bufLen = strlen(buf); n       buf[bufLen - 1] =   0 ; n       if (isWidget(buf)) { n         continue; n       } n       if (isBox(buf)) { n         continue; n       } n       operate(buf); n     } n     compSize(); n     sort(widgets, widgets + wCnt, cmp); n     for (int i = 0; i < wCnt; ++i) { n       printf( %s %I64d %I64d n , widgets[i].name, widgets[i].width, n              widgets[i].height); n     } n     clear(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n const int inf = (int)1e9; n const double inf1 = 1e-9; n const int ss = (int)1e6 + 3; n int main() { n   string s; n   getline(cin, s); n   int sz = s.find( . ); n   int z1 = 0; n   if (sz == -1) { n     sz = s.size(); n     z1 = -1; n   } n   string res; n   int z = 0; n   if (s[0] ==  - ) { n     res =  ( ; n     z = 1; n   } n   res +=  $ ; n   for (int i = z; i < (sz - z) % 3 + z; ++i) res += s[i]; n   if ((sz - z) % 3 != 0) res +=  , ; n   string tmp; n   for (int i = (sz - z) % 3 + z; i < sz; i += 3) { n     res += s[i]; n     res += s[i + 1]; n     res += s[i + 2]; n     res +=  , ; n   } n   res.erase(res.size() - 1, 1); n   res +=  . ; n   s +=  00 ; n   res += s[sz + 1 + z1]; n   res += s[sz + 2 + z1]; n   if (s[0] ==  - ) { n     res +=  ) ; n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long ans1, ans2; n     long long n, m; n     cin >> n >> m; n     ans1 = n * m / 2; n     ans2 = ans1; n     if (n * m % 2) ans1++; n     long long x1, y1, x2, y2; n     cin >> x1 >> y1 >> x2 >> y2; n     long long white1 = (x2 - x1 + 1) * (y2 - y1 + 1) / 2; n     long long black1 = white1; n     if ((x2 - x1 + 1) * (y2 - y1 + 1) % 2) { n       if (abs(x2 - y2) % 2) n         black1++; n       else n         white1++; n     } n     long long x3, y3, x4, y4; n     cin >> x3 >> y3 >> x4 >> y4; n     long long white2 = (x4 - x3 + 1) * (y4 - y3 + 1) / 2; n     long long black2 = white2; n     if ((x4 - x3 + 1) * (y4 - y3 + 1) % 2) { n       if (abs(x4 - y3) % 2) n         black2++; n       else n         white2++; n     } n     long long x5 = max(x1, x3), y5 = max(y1, y3), x6 = min(x2, x4), n               y6 = min(y2, y4); n     long long white = 0, black = 0; n     if (x5 <= x6 && y5 <= y6) { n       long long gapx = x6 - x5 + 1; n       long long gapy = y6 - y5 + 1; n       white = gapx * gapy / 2; n       black = white; n       if (gapx * gapy % 2) { n         if (abs(x5 - y5) % 2) n           black++; n         else n           white++; n       } n     } n     ans1 -= white; n     ans2 -= black; n     white1 -= white; n     black1 -= black; n     white2 -= white; n     black2 -= black; n     ans1 += black1; n     ans2 -= black1; n     ans2 += white2; n     ans1 -= white2; n     ans2 += white + black; n     cout << ans1 <<     << ans2 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n constexpr int inf = numeric_limits<int>::max() >> 1; n constexpr int N = 100001; n constexpr int mod = 1000000007; n int h[N], tin[N], tout[N], ap[N], n, root, a[N], T; n int mx[N * 4], mn[N * 4], ml[N * 4]; n vector<int> g[N]; n void dfs(int u, int p) { n   tin[u] = ++T; n   ap[T] = u; n   h[u] = h[p] + 1; n   for (int v : g[u]) n     if (v != p) dfs(v, u); n   tout[u] = T; n } n void build(int id, int l, int r) { n   if (l == r) { n     mx[id] = h[ap[l]]; n     mn[id] = a[ap[l]]; n     ml[id] = h[ap[l]]; n     return; n   } n   build((id << 1), l, (l + r) / 2); n   build(((id << 1) + 1), (l + r) / 2 + 1, r); n   mx[id] = max(mx[(id << 1)], mx[((id << 1) + 1)]); n   mn[id] = min(mn[(id << 1)], mn[((id << 1) + 1)]); n   ml[id] = min(ml[(id << 1)], ml[((id << 1) + 1)]); n   return; n } n int query(int id, int l, int r, int L, int R, int dep) { n   if (R < l || r < L) return inf; n   if (ml[id] > dep) return inf; n   if (L <= l && r <= R && mx[id] <= dep) return mn[id]; n   return min(query((id << 1), l, (l + r) / 2, L, R, dep), n              query(((id << 1) + 1), (l + r) / 2 + 1, r, L, R, dep)); n } n void solution() { n   cin >> n >> root; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i < n; i++) { n     int u, v; n     cin >> u >> v; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   dfs(root, 0); n   build(1, 1, n); n   int m; n   cin >> m; n   int last = 0; n   while (m--) { n     int p, q; n     cin >> p >> q; n     p = 1 + ((p + last) % n); n     q = (q + last) % n; n     last = query(1, 1, n, tin[p], tout[p], h[p] + q); n     cout << last <<   n ; n   } n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int tc = 1; n   while (tc--) { n     solution(); n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 2e5 + 1; n int vis[M], vis2[M]; n int main() { n   string t, s; n   cin >> s >> t; n   int a = 0, b = 0; n   for (char c : t) vis[c]++; n   int i = 0; n   for (char c : s) { n     if (vis[c]) { n       vis[c]--; n       a++; n       vis2[i] = 1; n     } n     i++; n   } n   for (i = 0; i < s.size(); i++) { n     if (vis2[i]) continue; n     char c = s[i]; n     if (islower(c)) n       c = toupper(c); n     else n       c = tolower(c); n     if (vis[c]) b++, vis[c]--; n   } n   cout << a <<     << b; n } n


#include <bits/stdc++.h> n using namespace std; n const int P = 1e9 + 7; n int add(int a, int b) { n   if ((a += b) >= P) a -= P; n   return a; n } n int sub(int a, int b) { n   if ((a -= b) < 0) a += P; n   return a; n } n int mul(int a, int b) { return 1ll * a * b % P; } n int kpow(int a, int b) { n   int r = 1; n   for (; b; b >>= 1, a = mul(a, a)) { n     if (b & 1) r = mul(r, a); n   } n   return r; n } n const int N = 205; n int a, b, c, w, n, m, p1, p2; n char ans[N][N]; n bool vis[200]; n bool solve(int n, int m) { n   if (n * m % 2 == 1) return 0; n   if (n % 2 == 1) { n     if (m / 2 > a) return 0; n     a -= m / 2; n     for (int i = 1; i <= m; i += 2) n       ans[n][i] = ans[n][i + 1] = (i / 2 % 2 == 0 ?  a  :  b ); n     n--; n   } n   if (m % 2 == 1) { n     if (n / 2 > b) return 0; n     b -= n / 2; n     for (int i = 1; i <= n; i += 2) n       ans[i][m] = ans[i + 1][m] = (i / 2 % 2 == 0 ?  a  :  b ); n     m--; n   } n   if (a & 1) a--; n   if (b & 1) b--; n   if (n * m > a * 2 + b * 2 + c * 4) return 0; n   for (int i = 1; i <= n; i += 2) n     for (int j = 1; j <= m; j += 2) { n       for (int k = ( a ); k < ( z  + 1); k++) vis[k] = 0; n       vis[ans[i - 1][j]] = 1; n       vis[ans[i - 1][j + 1]] = 1; n       vis[ans[i][j - 1]] = 1; n       vis[ans[i + 1][j - 1]] = 1; n       for (int k = ( c ); k < ( z  + 1); k++) n         if (!vis[k]) { n           p1 = k; n           vis[p1] = 1; n           break; n         } n       for (int k = ( c ); k < ( z  + 1); k++) n         if (!vis[k]) { n           p2 = k; n           vis[p2] = 1; n           break; n         } n       if (a > 0) { n         a -= 2; n         ans[i][j] = ans[i][j + 1] = p1; n         ans[i + 1][j] = ans[i + 1][j + 1] = p2; n       } else if (b > 0) { n         b -= 2; n         ans[i][j] = ans[i + 1][j] = p1; n         ans[i][j + 1] = ans[i + 1][j + 1] = p2; n       } else { n         c--; n         ans[i][j] = ans[i][j + 1] = ans[i + 1][j] = ans[i + 1][j + 1] = p1; n       } n     } n   return 1; n } n int main() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(0); n   cin >> n >> m >> a >> b >> c; n   bool ok = solve(n, m); n   if (!ok) n     cout <<  IMPOSSIBLE  n          <<   n ; n   else { n     for (int i = (1); i < (n + 1); i++) { n       for (int j = (1); j < (m + 1); j++) cout << ans[i][j]; n       cout <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e9 + 7; n const long long MOD = 998244353; n vector<vector<pair<long long, long long> > > adj; n int c[2]; n int colour[700005]; n bool ok; n int n; n void dfs(int idx) { n   c[colour[idx]] += (idx <= n); n   for (auto x : adj[idx]) { n     if (colour[x.first] == -1) { n       colour[x.first] = colour[idx] ^ x.second; n       dfs(x.first); n     } else if (colour[x.first] != (colour[idx] ^ x.second)) { n       ok = false; n     } n   } n } n int main(void) { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int m; n     cin >> n >> m; n     adj.assign(n + m + 5, vector<pair<long long, long long> >()); n     for (int i = 0; i < n + m + 5; i++) { n       colour[i] = -1; n     } n     int fake = n + 1; n     for (int i = 0; i < m; i++) { n       int a, b; n       string c; n       cin >> a >> b >> c; n       if (c ==  crewmate ) { n         adj[a].push_back(pair<long long, long long>(fake, 1)); n         adj[fake].push_back(pair<long long, long long>(a, 1)); n         adj[fake].push_back(pair<long long, long long>(b, 1)); n         adj[b].push_back(pair<long long, long long>(fake, 1)); n         fake++; n       } else { n         adj[a].push_back(pair<long long, long long>(b, 1)); n         adj[b].push_back(pair<long long, long long>(a, 1)); n       } n     } n     int ans = 0; n     ok = true; n     for (int i = 1; i < n + 1; i++) { n       if (colour[i] == -1) { n         colour[i] = 0; n         c[0] = c[1] = 0; n         dfs(i); n         ans += max(c[0], c[1]); n       } n     } n     if (!ok) { n       ans = -1; n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n int a[1005]; n void Init() { n   int i; n   a[0] = 1; n   a[1] = 1; n   for (i = 2; i <= 1000; i++) { n     a[i] = a[i - 1] * 3 % 1000003; n   } n } n int main() { n   int i, n; n   Init(); n   scanf( %d , &n); n   printf( %d n , a[n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b[100], c, xd = 0, lol; n   cin >> a; n   for (int i = 0; i < a; i++) { n     cin >> b[i]; n   } n   sort(b, b + a); n   for (int i = 0; i < a; i++) { n     if (i == a - 1) { n       c = b[i]; n     } n   } n   for (int i = 0; i < a; i++) { n     if (b[i] <= c) { n       xd = xd + (c - b[i]); n     } n   } n   cout << xd; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T inline sqr(T x) { n   return x * x; n } n const long double pi = 3.1415926535897932384626433832795; n const long double eps = 1e-8; n const int MAXN = 1000500; n string s; n int n; n int fin[3]; n struct node { n   int l, r, par, link; n   map<char, int> next; n   node(int l = 0, int r = 0, int par = -1) : l(l), r(r), par(par), link(-1) {} n   int len() { return r - l; } n   int &get(char c) { n     if (!next.count(c)) next[c] = -1; n     return next[c]; n   } n }; n node t[MAXN]; n int sz; n struct state { n   int v, pos; n   state(int v, int pos) : v(v), pos(pos) {} n }; n state ptr(0, 0); n state go(state st, int l, int r) { n   while (l < r) n     if (st.pos == t[st.v].len()) { n       st = state(t[st.v].get(s[l]), 0); n       if (st.v == -1) return st; n     } else { n       if (s[t[st.v].l + st.pos] != s[l]) return state(-1, -1); n       if (r - l < t[st.v].len() - st.pos) return state(st.v, st.pos + r - l); n       l += t[st.v].len() - st.pos; n       st.pos = t[st.v].len(); n     } n   return st; n } n int split(state st) { n   if (st.pos == t[st.v].len()) return st.v; n   if (st.pos == 0) return t[st.v].par; n   node v = t[st.v]; n   int id = sz++; n   t[id] = node(v.l, v.l + st.pos, v.par); n   t[v.par].get(s[v.l]) = id; n   t[id].get(s[v.l + st.pos]) = st.v; n   t[st.v].par = id; n   t[st.v].l += st.pos; n   return id; n } n int get_link(int v) { n   if (t[v].link != -1) return t[v].link; n   if (t[v].par == -1) return 0; n   int to = get_link(t[v].par); n   return t[v].link = split( n              go(state(to, t[to].len()), t[v].l + (t[v].par == 0), t[v].r)); n } n void tree_extend(int pos) { n   for (;;) { n     state nptr = go(ptr, pos, pos + 1); n     if (nptr.v != -1) { n       ptr = nptr; n       return; n     } n     int mid = split(ptr); n     int leaf = sz++; n     t[leaf] = node(pos, n, mid); n     t[mid].get(s[pos]) = leaf; n     ptr.v = get_link(mid); n     ptr.pos = t[ptr.v].len(); n     if (!mid) break; n   } n } n void build_tree() { n   sz = 1; n   for (int i = 0; i < n; ++i) tree_extend(i); n } n long long d[MAXN][3]; n long long ans[MAXN]; n const int MOD = 1e9 + 7; n long long bal[MAXN]; n void add(int l, int r, int val) { n   bal[l] += val; n   bal[r + 1] -= val; n } n void dfs(int v, int len) { n   for (auto w : ::t[v].next) { n     int nlen = len + ::t[w.second].len(); n     dfs(w.second, nlen); n   } n   int tt = -1; n   for (int i = 0; i < (int)(3); ++i) n     if (fin[i] >= t[v].l && fin[i] < t[v].r) { n       tt = i; n       break; n     } n   if (tt != -1) n     d[v][tt] = 1; n   else { n     for (auto w : ::t[v].next) { n       for (int i = 0; i < (int)(3); ++i) d[v][i] += d[w.second][i]; n     } n     long long c = 1; n     for (int i = 0; i < (int)(3); ++i) c = (c * d[v][i]) % MOD; n     add(len - t[v].len() + 1, len, c); n   } n } n int main() { n   vector<string> in(3); n   for (int i = 0; i < (int)(3); ++i) { n     cin >> in[i]; n     s += in[i]; n     fin[i] = s.length(); n     s += char( 0  + i); n   } n   n = s.length(); n   build_tree(); n   dfs(0, 0); n   int l = 1e9; n   for (int i = 0; i < (int)(3); ++i) l = min(l, (int)in[i].length()); n   long long z = 0; n   for (int i = 0; i < (int)(MAXN); ++i) { n     z += bal[i]; n     z %= MOD; n     z = (z + MOD) % MOD; n     ans[i] = z; n   } n   for (int i = 1; i <= (int)(l); ++i) printf( %d  , (int)ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using UL = unsigned long int; n using UI = unsigned int; n using ULL = unsigned long long int; n using LI = long int; n using LL = long long; n using LF = double; n using LLF = long double; n const int mod1 = 1000000007; n const int mod2 = 998244353; n const double PI = acos(-1); n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   int ntc = 1, tc, i, j; n   int n, k; n   for (tc = 1; tc <= ntc; tc++) { n     cin >> n >> k; n     string s; n     cin >> s; n     int cnt1[26] = {0}, cnt2[26] = {0}; n     string ans =   ; n     for (auto ch : s) { n       cnt1[ch -  a ]++; n     } n     for (i = 0; i < 26 && k > 0; i++) { n       cnt2[i] += min(k, cnt1[i]); n       k -= min(k, cnt1[i]); n     } n     for (auto ch : s) { n       if (cnt2[ch -  a ] > 0) { n         cnt2[ch -  a ]--; n         continue; n       } n       cout << ch; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1004; n int n, p, q, f[N][54][54], vc = 1000000000, ns[N][30], nt[N][30]; n char c[N], s[N], t[N]; n bool check1(int i, char x, int j) { n   if (i + 1 < j) return false; n   for (int k = 1; k < j; k++) { n     if (s[i + k - j + 1] != s[k]) return false; n   } n   if (x != s[j]) return false; n   return true; n } n bool check2(int i, char x, int j) { n   if (i + 1 < j) return false; n   for (int k = 1; k < j; k++) { n     if (t[i + k - j + 1] != t[k]) return false; n   } n   if (x != t[j]) return false; n   return true; n } n void build() { n   for (int i = 0; i <= p; i++) { n     for (char x =  a ; x <=  z ; x++) { n       for (int j = p; j >= 1; j--) { n         if (check1(i, x, j)) { n           ns[i][x -  a  + 1] = j; n           break; n         } n       } n     } n   } n   for (int i = 0; i <= q; i++) { n     for (char x =  a ; x <=  z ; x++) { n       for (int j = q; j >= 1; j--) { n         if (check2(i, x, j)) { n           nt[i][x -  a  + 1] = j; n           break; n         } n       } n     } n   } n } n int cal(int i, int x, int y) { n   if (i == n) return 0; n   if (f[i][x][y] != -vc) return f[i][x][y]; n   f[i][x][y] = -vc + 1; n   if (c[i + 1] ==  * ) { n     for (char b =  a ; b <=  z ; b++) { n       int cost = 0; n       int nx1 = ns[x][b -  a  + 1]; n       if (nx1 == p) cost++; n       int nx2 = nt[y][b -  a  + 1]; n       if (nx2 == q) cost--; n       f[i][x][y] = max(f[i][x][y], cal(i + 1, nx1, nx2) + cost); n     } n   } else { n     int cost = 0; n     char b = c[i + 1]; n     int nx1 = ns[x][b -  a  + 1]; n     if (nx1 == p) cost++; n     int nx2 = nt[y][b -  a  + 1]; n     if (nx2 == q) cost--; n     f[i][x][y] = max(f[i][x][y], cal(i + 1, nx1, nx2) + cost); n   } n   return f[i][x][y]; n } n int main() { n   scanf( %s%s%s , c + 1, s + 1, t + 1); n   p = strlen(s + 1); n   q = strlen(t + 1); n   n = strlen(c + 1); n   build(); n   for (int i = 0; i <= n; i++) { n     for (int j = 0; j <= p; j++) { n       for (int z = 0; z <= q; z++) f[i][j][z] = -vc; n     } n   } n   cout << cal(0, 0, 0); n } n


#include <bits/stdc++.h> n using namespace std; n string S; n set<string> answer; n void izomorphism1(string &s) { n   swap(s[0], s[3]); n   swap(s[1], s[3]); n   swap(s[2], s[3]); n } n void izomorphism2(string &s) { n   swap(s[1], s[4]); n   swap(s[3], s[5]); n   swap(s[4], s[5]); n } n void izomorphism3(string &s) { n   swap(s[4], s[2]); n   swap(s[0], s[5]); n   swap(s[4], s[5]); n } n int main() { n   cin >> S; n   sort(S.begin(), S.end()); n   do { n     string cur_S, min_string; n     cur_S = S; n     min_string = S; n     for (int i = 1; i <= 4; i++, izomorphism1(cur_S)) n       for (int j = 1; j <= 4; j++, izomorphism2(cur_S)) n         for (int z = 1; z <= 4; z++, izomorphism3(cur_S)) { n           min_string = min(min_string, cur_S); n         } n     answer.insert(min_string); n   } while (next_permutation(S.begin(), S.end())); n   cout << answer.size() << endl; n   return 0; n } n


#include <bits/stdc++.h> n const unsigned long long maxn = 70; n unsigned long long n, flg, KKK, ans; n unsigned long long f[maxn], mul10[maxn], cycle[maxn], a[maxn]; n struct matrix { n   unsigned long long len, wid; n   unsigned long long mt[3][3]; n } pre[14][45]; n inline unsigned long long min(unsigned long long a, unsigned long long b) { n   return a < b ? a : b; n } n unsigned long long modmul(unsigned long long a, unsigned long long b, n                           unsigned long long mod) { n   unsigned long long res = 0; n   while (b) { n     if (b & 1) { n       res += a; n       if (res > mod) res -= mod; n     } n     a += a; n     if (a > mod) a -= mod; n     b >>= 1; n   } n   return res; n } n matrix mul(matrix a, matrix b, unsigned long long mod) { n   matrix res; n   res.len = a.len, res.wid = b.wid; n   for (unsigned long long u = 1; u <= a.len; u++) n     for (unsigned long long v = 1; v <= b.wid; v++) res.mt[u][v] = 0; n   for (unsigned long long u = 1; u <= a.len; u++) n     for (unsigned long long v = 1; v <= b.wid; v++) n       for (unsigned long long w = 1; w <= a.wid; w++) n         res.mt[u][v] = n             (res.mt[u][v] + modmul(a.mt[u][w], b.mt[w][v], mod)) % mod; n   return res; n } n unsigned long long calc(unsigned long long b, unsigned long long kk, n                         unsigned long long mod) { n   matrix a, res; n   res.len = 1, res.wid = 2, res.mt[1][1] = 0, res.mt[1][2] = 1; n   for (unsigned long long i = 0; i <= 44; i++) n     if ((b >> i) & 1) res = mul(res, pre[kk][i], mod); n   return res.mt[1][1]; n } n void dfs(unsigned long long len, unsigned long long now) { n   if (now > ans) return; n   if (len == n + 1) { n     flg = 1, ans = min(ans, now); n     return; n   } n   for (unsigned long long i = 0; i < 10; i++) { n     unsigned long long k = n         (modmul(i, cycle[len], cycle[len + 1]) + now) % cycle[len + 1]; n     if (calc(k, len, mul10[len]) == a[len]) dfs(len + 1, k); n   } n } n signed main() { n   f[0] = 0, f[1] = 1; n   for (unsigned long long i = 2; i <= 60; i++) f[i] = f[i - 1] + f[i - 2]; n   cycle[0] = 6, mul10[0] = 1; n   for (unsigned long long i = 1; i <= 19; i++) n     cycle[i] = 10ll * cycle[i - 1], mul10[i] = 10ll * mul10[i - 1]; n   for (unsigned long long i = 1; i <= 13; i++) { n     pre[i][0].len = pre[i][0].wid = 2, pre[i][0].mt[1][1] = 0, n     pre[i][0].mt[1][2] = pre[i][0].mt[2][1] = pre[i][0].mt[2][2] = 1; n     for (unsigned long long j = 1; j <= 44; j++) n       pre[i][j] = mul(pre[i][j - 1], pre[i][j - 1], mul10[i]); n   } n   scanf( %llu , &KKK); n   n = 13, ans = 10000000000000000; n   for (unsigned long long i = 1; i <= n; i++) n     a[i] = a[i - 1] + KKK % 10 * mul10[i - 1], KKK /= 10; n   for (unsigned long long i = 0; i < 60; i++) n     if (f[i] % 10 == a[1]) dfs(1, i); n   if (flg == 0) n     puts( -1 ); n   else n     printf( %llu n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> mp; n int main() { n   int n, m; n   cin >> n >> m; n   int now1 = 0; n   for (int i = 0; i < n; i++) { n     int c, t; n     cin >> c >> t; n     now1 += c * t; n     mp[now1] = 1; n   } n   map<int, int>::iterator it = mp.begin(); n   int now = 0; n   for (int i = 0; i < m; i++) { n     int v; n     cin >> v; n     while (v > it->first) { n       it++; n       now++; n     } n     cout << now + 1 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n inline void smin(T &a, U b) { n   if (a > b) a = b; n } n template <typename T, typename U> n inline void smax(T &a, U b) { n   if (a < b) a = b; n } n template <class T> n inline void gn(T &first) { n   char c, sg = 0; n   while (c = getchar(), (c >  9  || c <  0 ) && c !=  - ) n     ; n   for ((c ==  -  ? sg = 1, c = getchar() : 0), first = 0; c >=  0  && c <=  9 ; n        c = getchar()) n     first = (first << 1) + (first << 3) + c -  0 ; n   if (sg) first = -first; n } n template <class T, class T1> n inline void gn(T &first, T1 &second) { n   gn(first); n   gn(second); n } n template <class T, class T1, class T2> n inline void gn(T &first, T1 &second, T2 &z) { n   gn(first); n   gn(second); n   gn(z); n } n template <class T> n inline void print(T first) { n   if (first < 0) { n     putchar( - ); n     return print(-first); n   } n   if (first < 10) { n     putchar( 0  + first); n     return; n   } n   print(first / 10); n   putchar(first % 10 +  0 ); n } n template <class T> n inline void printsp(T first) { n   print(first); n   putchar(   ); n } n template <class T> n inline void println(T first) { n   print(first); n   putchar(  n ); n } n template <class T, class U> n inline void print(T first, U second) { n   printsp(first); n   println(second); n } n template <class T, class U, class V> n inline void print(T first, U second, V z) { n   printsp(first); n   printsp(second); n   println(z); n } n int power(int a, int b, int m, int ans = 1) { n   for (; b; b >>= 1, a = 1LL * a * a % m) n     if (b & 1) ans = 1LL * ans * a % m; n   return ans; n } n int ch[300010], re[300010], a[300010]; n int C(int n, int m) { n   return ((long long)ch[n] * re[m] % 1000000007) * (long long)re[n - m] % n          1000000007; n } n void pre() { n   ch[0] = 1; n   for (int i = 1; i < 300010; i++) n     ch[i] = (long long)ch[i - 1] * i % 1000000007; n   re[300010 - 1] = power(ch[300010 - 1], 1000000007 - 2, 1000000007); n   for (int i = 300010 - 1; i > 0; i--) n     re[i - 1] = (long long)re[i] * i % 1000000007; n   return; n } n int main() { n   pre(); n   int n; n   gn(n); n   for (int i = 1; i <= n; i++) gn(a[i]); n   if (n == 1) { n     printf( %d n , a[1]); n     return 0; n   } n   if (n == 2) { n     printf( %d n , ((a[1] + a[2]) % 1000000007 + 1000000007) % 1000000007); n     return 0; n   } n   if (n & 1) { n     for (int i = 1; i < n; i++) { n       if (i & 1) n         a[i] = ((a[i] + a[i + 1]) % 1000000007 + 1000000007) % 1000000007; n       else n         a[i] = ((a[i] - a[i + 1]) % 1000000007 + 1000000007) % 1000000007; n     } n     n--; n   } n   if (n == 2) { n     printf( %d n , (a[1] + a[2]) % 1000000007); n     return 0; n   } n   long long ans1 = 0, ans2 = 0; n   int m = n / 2; n   for (int i = 1; i <= n; i++) { n     if (i & 1) { n       ans1 = n           (ans1 + (long long)C(m - 1, i / 2) * a[i] % 1000000007) % 1000000007; n     } else { n       ans2 = (ans2 + (long long)C(m - 1, i / 2 - 1) * a[i] % 1000000007) % n              1000000007; n     } n   } n   if (m & 1 == 1) n     printf( %I64d n , (ans1 + ans2) % 1000000007); n   else n     printf( %I64d n , ((ans1 - ans2) % 1000000007 + 1000000007) % 1000000007); n   return 0; n } n


#include <bits/stdc++.h> n struct node { n   int x, y; n   int dir, len; n } arr[1100 * 1100], st[1100]; n int n, m, tot, vis[1100][1100], cnt; n int srow[1100][1100], scol[1100][1100]; n int dir[4] = {1, -1, -1, 1}; n char mmap[1100][1100]; n void Initial() { n   int i, j, k; n   for (i = 1; i <= n; ++i) n     for (j = 1; j <= m; ++j) srow[i][j] = srow[i][j - 1] + vis[i][j]; n   for (j = 1; j <= m; ++j) n     for (i = 1; i <= n; ++i) scol[i][j] = scol[i - 1][j] + vis[i][j]; n } n int GetDir(char c) { n   if (c ==  N ) return 0; n   if (c ==  S ) return 1; n   if (c ==  W ) return 2; n   if (c ==  E ) return 3; n } n int Isok(int in) { n   int i, j, k, x, y; n   x = st[in].x, y = st[in].y; n   for (i = 1; i <= tot; ++i) { n     int td = arr[i].dir, tl = arr[i].len; n     if (td == 0) { n       if (x - tl <= 0) return 0; n       if (scol[x][y] - scol[x - tl - 1][y] > 0) return 0; n       x -= tl; n     } else if (td == 1) { n       if (x + tl >= n + 1) return 0; n       if (scol[x + tl][y] - scol[x - 1][y] > 0) return 0; n       x += tl; n     } else if (td == 2) { n       if (y - tl <= 0) return 0; n       if (srow[x][y] - srow[x][y - tl - 1] > 0) return 0; n       y -= tl; n     } else { n       if (y + tl >= m + 1) return 0; n       if (srow[x][y + tl] - srow[x][y - 1] > 0) return 0; n       y += tl; n     } n   } n   cnt++; n   return 1; n } n int main() { n   int i, j, k; n   while (scanf( %d%d , &n, &m) != EOF) { n     for (i = 0; i < 26; ++i) st[i].x = -1; n     for (i = 1; i <= n; ++i) { n       scanf( %s , mmap[i] + 1); n       for (j = 1; j <= m; ++j) { n         if (mmap[i][j] >=  A  && mmap[i][j] <=  Z ) n           st[mmap[i][j] -  A ].x = i, st[mmap[i][j] -  A ].y = j; n         vis[i][j] = mmap[i][j] ==  #  ? 1 : 0; n       } n     } n     Initial(); n     scanf( %d , &tot); n     for (i = 1; i <= tot; ++i) { n       char str[20]; n       scanf( %s %d , str, &arr[i].len); n       arr[i].dir = GetDir(str[0]); n     } n     cnt = 0; n     for (i = 0; i < 26; ++i) n       if (st[i].x != -1 && Isok(i)) printf( %c , i +  A ); n     if (cnt == 0) printf( no solution n ); n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2000 + 5; n int a[N], b[N]; n bool mark[N]; n vector<int> ch; n int main() { n   int n, m, mn, ans = 0; n   cin >> n >> m; n   mn = n / m; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (a[i] <= m && !mark[a[i]]) { n       b[a[i]]++; n       if (b[a[i]] == mn) mark[a[i]] = true; n     } else n       ch.push_back(i); n   } n   for (int i = 1; i <= m; i++) n     while (!mark[i]) { n       ans++; n       a[ch.back()] = i; n       ch.pop_back(); n       b[i]++; n       if (b[i] == mn) mark[i] = true; n     } n   cout << mn <<     << ans <<   n ; n   for (int i = 0; i < n; i++) cout << a[i] <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n const unsigned long long inf = 1LL << 61; n const double eps = 1e-5; n using namespace std; n bool cmp(int a, int b) { return a > b; } n string itoa(long long a) { n   string t =   ; n   while (a > 0) { n     t += (char)(a % 10 +  0 ); n     a /= 10; n   } n   string s =   ; n   for (int i = t.size() - 1; i >= 0; i--) s += t[i]; n   return s; n } n long long atoi(string s) { n   long long t = 1; n   long long ans = 0; n   for (int i = s.size() - 1; i >= 0; i--) { n     long long b = (long long)(s[i] -  0 ); n     ans += b * t; n     t *= 10; n   } n   return ans; n } n int main() { n   int n, m; n   while (cin >> n >> m) { n     long long c = m + n; n     string c1 = itoa(c); n     string c2 =   ; n     for (int i = 0; i < c1.size(); i++) { n       if (c1[i] !=  0 ) c2 += c1[i]; n     } n     long long cc = atoi(c2); n     string a = itoa(n); n     string b = itoa(m); n     string a1 =   ; n     string b1 =   ; n     for (int i = 0; i < a.size(); i++) { n       if (a[i] !=  0 ) a1 += a[i]; n     } n     for (int j = 0; j < b.size(); j++) { n       if (b[j] !=  0 ) b1 += b[j]; n     } n     long long aa = atoi(a1); n     long long bb = atoi(b1); n     if (aa + bb == cc) n       puts( YES ); n     else n       puts( NO ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100005]; n int ans[100005]; n string s[100005]; n map<string, int> h; n vector<pair<int, int> > v; n string res[6] = { S ,  M ,  L ,  XL ,  XXL ,  XXXL }; n int main() { n   memset(ans, -1, sizeof(ans)); n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   h[ S ] = 0; n   h[ M ] = 1; n   h[ L ] = 2; n   h[ XL ] = 3; n   h[ XXL ] = 4; n   h[ XXXL ] = 5; n   for (int i = 0; i < 6; i++) cin >> a[i]; n   int n; n   cin >> n; n   bool ok = 1; n   for (int i = 1; i <= n; i++) { n     cin >> s[i]; n     string t[2]; n     t[0] =   ; n     t[1] =   ; n     int k = 0; n     int m = s[i].length(); n     for (int j = 0; j < m; j++) { n       if (s[i][j] ==  , ) n         k++; n       else n         t[k] += s[i][j]; n     } n     if (t[1] ==   ) { n       ans[i] = h[t[0]]; n       a[h[t[0]]]--; n       if (a[h[t[0]]] < 0) ok = 0; n     } else n       v.push_back(pair<int, int>(h[t[0]], i)); n   } n   sort(v.begin(), v.end()); n   for (int i = 0; i < v.size(); i++) { n     int j = v[i].second, x = v[i].first; n     if (a[x] > 0) n       ans[j] = x, a[x]--; n     else n       ans[j] = x + 1, a[x + 1]--; n   } n   for (int i = 0; i < 6; i++) n     if (a[i] < 0) ok = 0; n   if (!ok) n     printf( NO n ); n   else { n     cout <<  YES  n          <<   n ; n     for (int i = 1; i <= n; i++) cout << res[ans[i]] <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[103], n, x, y, m, ans; n int main() { n   cin >> x >> y >> n; n   m = x * y; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < m; i++) { n     int x, j; n     cin >> x; n     for (j = 0; j < n; j++) { n       if (a[j] == x) break; n     } n     ans = ans + j + 1; n     for (int k = j - 1; k >= 0; k--) a[k + 1] = a[k]; n     a[0] = x; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n void runcase() { n   int n, k; n   cin >> n >> k; n   vector<int> a(n); n   for (int i = 0; i < n; i++) cin >> a[i]; n   int changes = 0; n   k--; n   for (int i = 1; i < n; i++) n     if (a[i] != a[i - 1]) changes++; n   int ans = 0; n   if (k == 0) { n     if (changes == 0) n       ans = 1; n     else n       ans = -1; n   } else { n     ans = max(1, (changes + k - 1) / k); n   } n   cout << ans << endl; n } n int main() { n   int t; n   cin >> t; n   while (t--) runcase(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long f[10]; n void ge() { n   f[0] = 1; n   for (int i = 1; i <= 9; i++) { n     f[i] = f[i - 1] * 10; n   } n } n long long a[100005], b[100005]; n int main() { n   ge(); n   int n, k; n   long long ans = 1; n   int i, j; n   scanf( %d%d , &n, &k); n   for (i = 0; i < n / k; i++) { n     scanf( %I64d , &a[i]); n   } n   for (i = 0; i < n / k; i++) { n     scanf( %I64d , &b[i]); n   } n   for (i = 0; i < n / k; i++) { n     long long n1 = (f[k] - 1) / a[i] + 1; n     long long n2 = (f[k - 1] - 1) / a[i] + 1; n     long long n3 = (f[k - 1] * (b[i] + 1) - 1) / a[i] + 1 - n                    (f[k - 1] * b[i] - 1) / a[i] - 1; n     if (b[i] == 0) n       ans *= n1 - n2; n     else n       ans *= n1 - n3; n     ans %= 1000000007; n   } n   printf( %I64d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   cin >> n; n   vector<long long int> a(n); n   vector<long long int> mp(n + 1, 0); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     mp[a[i]]++; n   } n   vector<long long int> ans(n + 1); n   priority_queue<pair<long long, long long>> pq; n   long long cost = 0; n   for (int i = 0; i < n + 1; i++) { n     ans[i] = mp[i] + cost; n     if (mp[i] == 0) { n       if (pq.empty()) { n         for (int j = i + 1; j < n + 1; j++) ans[j] = -1; n         break; n       } n       auto tp = pq.top(); n       pq.pop(); n       cost += (long long)(i - tp.first); n       tp.second--; n       if (tp.second > 1) pq.push({tp.first, tp.second}); n     } n     if (mp[i] > 1) pq.push({i, mp[i]}); n   } n   for (int i = 0; i < n + 1; i++) cout << ans[i] <<    ; n   cout << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> in[101000]; n int n, m, k, curt; n int up[101000], t[101000], tout[101000], ans[101000]; n int jmp[101000][20]; n char used[101000]; n void dfs(int v, int p) { n   if (used[v]) return; n   used[v] = 1; n   t[v] = curt++; n   up[v] = t[v]; n   for (unsigned int i = 0; i < in[v].size(); i++) n     if (used[in[v][i]]) { n       if (p != in[v][i]) up[v] = min(up[v], t[in[v][i]]); n     } else { n       dfs(in[v][i], v); n       up[v] = min(up[v], up[in[v][i]]); n     } n } n void dfs2(int v, int cur) { n   if (used[v]) return; n   used[v] = 1; n   ans[v] = cur; n   for (unsigned int i = 0; i < in[v].size(); i++) n     if (!used[in[v][i]] && !((t[v] < t[in[v][i]] && up[in[v][i]] > t[v]) || n                              (t[v] > t[in[v][i]] && up[v] > t[in[v][i]]))) n       dfs2(in[v][i], cur); n   for (unsigned int i = 0; i < in[v].size(); i++) n     if (!used[in[v][i]] && ((t[v] < t[in[v][i]] && up[in[v][i]] > t[v]) || n                             (t[v] > t[in[v][i]] && up[v] > t[in[v][i]]))) n       dfs2(in[v][i], cur + 1); n } n void getLCA(int v, int p) { n   if (used[v]) return; n   used[v] = 1; n   t[v] = curt++; n   jmp[v][0] = p; n   for (int i = 1; i <= 18; ++i) jmp[v][i] = jmp[jmp[v][i - 1]][i - 1]; n   for (unsigned int i = 0; i < in[v].size(); i++) n     if (in[v][i] != p) getLCA(in[v][i], v); n   tout[v] = curt++; n } n char upp(int a, int b) { return t[a] <= t[b] && tout[a] >= tout[b]; } n int lca(int a, int b) { n   if (upp(a, b)) return a; n   if (upp(b, a)) return b; n   for (int i = 18; i >= 0; --i) n     if (!upp(jmp[a][i], b)) a = jmp[a][i]; n   return jmp[a][0]; n } n int main(void) { n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int q, w; n     cin >> q >> w; n     in[q].push_back(w); n     in[w].push_back(q); n   } n   dfs(1, -1); n   memset(used, 0, sizeof(used)); n   curt = 0; n   dfs2(1, 0); n   curt = 0; n   memset(used, 0, sizeof(used)); n   getLCA(1, 1); n   cin >> k; n   for (int i = 0; i < k; i++) { n     int q, w; n     cin >> q >> w; n     int l = lca(q, w); n     cout << ans[q] + ans[w] - 2 * ans[l] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double x, y, r, x1, y1; n   cin >> r >> x >> y >> x1 >> y1; n   double diam = 2 * r; n   double dist = sqrt((x1 - x) * (x1 - x) + (y1 - y) * (y1 - y)); n   int q = 0; n   if (dist == 0) { n     cout << 0 << endl; n   } else { n     while (dist >= diam) { n       dist -= diam; n       q++; n     } n     if (dist == 0) { n       cout << q << endl; n     } else { n       cout << q + 1 << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n const int mod = 1e9 + 7; n const int MAX = 1e5 + 7; n using namespace std; n string st =  fedabc ; n void solve(unsigned long long n, char c) { n   n--; n   unsigned long long res = 16LL * (n / 4LL); n   if (n % 2) res += 7; n   res += st.find(c) + 1; n   cout << res; n } n void mysolve(unsigned long long n, char c) { n   unsigned long long l = 0, r = n / 2; n   unsigned long long res = 0; n   while (l <= r) { n     unsigned long long mid = (l + r) / 2; n     if ((1 + (mid + (mid * 3LL))) <= n) { n       res = mid; n       l = mid + 1; n     } else { n       r = mid - 1; n     } n   } n   unsigned long long currF = (1 + (res + (res * 3))); n   unsigned long long served = 2LL * res; n   unsigned long long sec = 6; n   if ((currF == n) || (currF + 2 == n)) { n     sec += ((served - 1) * 6) + (currF - 1); n   } else if (currF + 1 == n || currF + 3 == n) { n     sec += (served * 6) + (currF); n   } else if (currF + 4 == n || currF + 6 == n) { n     sec += ((served + 1) * 6) + (currF + 1); n   } n   for (auto i : st) { n     sec++; n     if (i == c) break; n   } n   cout << sec; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   string s; n   cin >> s; n   unsigned long long n = stoll(s.substr(0, s.size() - 1)); n   char seat = s[s.size() - 1]; n   solve(n, seat); n } n


#include <bits/stdc++.h> n using namespace std; n int ar[1002][1002]; n char buf[20]; n int main() { n   int n, m, k, w; n   int res = 0; n   vector<pair<int, int> > r; n   scanf( %d %d %d %d n , &n, &m, &k, &w); n   vector<vector<string> > f(k); n   for (int i = 0; i < k; i++) { n     for (int j = 0; j < n; j++) { n       fgets(buf, 20, stdin); n       string s = buf; n       s = s.substr(0, s.size() - 1); n       f[i].push_back(s); n     } n   } n   for (int i = 0; i < f.size(); i++) { n     for (int j = i + 1; j < f.size(); j++) { n       int d = 0; n       for (int e = 0; e < n; e++) { n         for (int l = 0; l < m; l++) { n           if (f[i][e][l] != f[j][e][l]) d++; n         } n       } n       if (d * w < n * m) { n         ar[i + 1][j + 1] = d * w; n         ar[j + 1][i + 1] = d * w; n       } else { n         ar[i + 1][j + 1] = 1000; n         ar[j + 1][i + 1] = 1000; n       } n     } n   } n   for (int i = 0; i < k + 1; i++) { n     ar[0][i] = n * m; n     ar[i][0] = n * m; n   } n   vector<char> used(k + 1, false); n   vector<int> min_edge(k + 1, 1000), end_edge(k + 1, -1); n   min_edge[0] = 0; n   for (int i = 0; i < k + 1; i++) { n     int v = -1; n     for (int j = 0; j < k + 1; j++) { n       if (!used[j] && (v == -1 || min_edge[j] < min_edge[v])) v = j; n     } n     used[v] = true; n     if (end_edge[v] != -1) { n       res += ar[v][end_edge[v]]; n       r.push_back(pair<int, int>(v, end_edge[v])); n     } n     for (int j = 0; j < k + 1; j++) { n       if (ar[v][j] < min_edge[j]) { n         min_edge[j] = ar[v][j]; n         end_edge[j] = v; n       } n     } n   } n   printf( %d n , res); n   for (int i = 0; i < r.size(); i++) { n     printf( %d %d n , r[i].first, r[i].second); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n size_t ham_dist(const string &a, const string &b) { n   size_t ct = 0; n   for (size_t i = 0; i < a.size(); ++i) n     if (a[i] != b[i]) ++ct; n   return ct; n } n string opp(const string &a) { n   string r =   ; n   for (size_t i = 0; i < a.size(); ++i) r += (a[i] ==  0  ?  1  :  0 ); n   return r; n } n string read_line(size_t m) { n   string r; n   for (size_t j = 0; j < m; ++j) { n     char c[2]; n     cin >> c; n     r += string(c); n   } n   return r; n } n string string_from_int(size_t len, size_t z) { n   string o; n   for (size_t i = 0; i < len; ++i) { n     if (z & (1 << i)) n       o +=  1 ; n     else n       o +=  0 ; n   } n   return o; n } n int main() { n   size_t n, m, k; n   cin >> n >> m >> k; n   map<string, size_t> counts; n   vector<string> mat; n   mat.reserve(n); n   for (size_t i = 0; i < n; ++i) { n     string l = read_line(m); n     mat.push_back(l); n   } n   if (n < m) { n     vector<string> newmat = mat; n     swap(n, m); n     mat.clear(); n     mat.reserve(n); n     for (size_t i = 0; i < n; ++i) { n       string p; n       for (size_t j = 0; j < m; ++j) { n         p += newmat[j][i]; n       } n       mat.push_back(p); n     } n   } n   for (size_t i = 0; i < n; ++i) { n     string &l = mat[i]; n     if (counts.find(l) == counts.end()) counts[l] = 0; n     ++counts[l]; n   } n   bool feasible = false; n   if (counts.size() <= k + 2) { n     if (n <= k) { n       for (size_t z = 0; z < ((size_t)1 << m); ++z) { n         string l = string_from_int(m, z); n         if (counts.find(l) == counts.end()) counts[l] = 0; n       } n     } n     for (map<string, size_t>::iterator it = counts.begin(); it != counts.end(); n          ++it) { n       size_t cost = 0; n       for (map<string, size_t>::iterator jt = counts.begin(); n            jt != counts.end(); ++jt) { n         if (jt->second == 0) continue; n         size_t cost_per = ham_dist(it->first, jt->first); n         cost_per = min(cost_per, ham_dist(opp(it->first), jt->first)); n         cost += cost_per * jt->second; n       } n       if (cost <= k) { n         feasible = true; n         k = cost; n       } n     } n   } n   if (!feasible) { n     cout <<  -1  << endl; n   } else { n     cout << k << endl; n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   cin.sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   vector<int> data(n + 1); n   for (int i = 1; i <= n; i++) cin >> data[i]; n   int i = 1, j = n; n   while (j > i && data[j] == data[1]) j--; n   while (i < j && data[i + 1] == data[1]) i++; n   cout << max(j - 1, max(j - i, n - i - 1)) << endl; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n #pragma GCC optimization( unroll-loops ) n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n = 0, k = 0, s = 0; n   cin >> n >> k >> s; n   int sz = min(n * (n - 1), k * (k - 1) / 2 + s); n   vector<vector<int> > dp(k + 1, vector<int>(sz + 1, 1e9 + 42)); n   dp[0][0] = 0; n   for (int pos = 0; pos < n; pos++) { n     int q; n     cin >> q; n     for (int i = k - 1; i >= 0; i--) n       for (int j = sz - pos; j >= 0; j--) n         dp[i + 1][j + pos] = min(dp[i + 1][j + pos], dp[i][j] + q); n   } n   int ans = 1e9 + 42; n   for (int i = k * (k - 1) / 2; i <= sz; i++) ans = min(ans, dp[k][i]); n   cout << ans; n   return 0; n } n


//  “Alhamdulillah for Everything” n #include<bits/stdc++.h> n using namespace std; n  n #define      pai              acos(-1) n #define      ff               first n #define      ss               second n #define      ll               long long n #define      pb               push_back n #define      mp               make_pair n #define      pll              pair<ll,ll> n #define      sz(a)            (ll)a.size() n #define      endl               n  n #define      gcd(a, b)        __gcd(a, b) n #define      lcm(a, b)        ((a*b)/gcd(a,b)) n #define      all(vec)         vec.begin(),vec.end() n #define      ms(a, b)         memset(a,b,sizeof(a)) n #define      rep(i,n)         for(int i= 0; i < int(n); i++) n #define      rep1(i,n)        for(int i= 1; i <= n; i++) n #define      TEST_CASE(t)     for(int zz=1;zz<=t;zz++) n #define      PRINT_CASE       printf( Case %d:  ,zz) n #define      fii              freopen( input.txt , r ,stdin); n #define      foo              freopen( output.txt , w ,stdout); n #define      boom             ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL); n  n const ll M   = 1e9+7; n const ll mxn = 1e5+7; n const ll inf = 1e18; n  n int  main() n { n     //boom; n     ll i,j,k,a,b,c,d,n,m,t,x,y,z,u,v; n     cin>>t; n     TEST_CASE(t) n     { n         cin>>n>>k; n         vector<ll>vec(n); n         for(i=0;i<n;i++) n         { n             cin>>vec[i]; n         } n         ll ans=0,sum=vec[0]; n         for(i=1;i<n;i++) n         { n             a=vec[i]*100; n             b=sum*k; n             if(a<b) n             { n                 sum+=vec[i]; n             } n             else n             { n                 ll tem=a-b+k-1; n                 tem/=k; n                 sum+=(vec[i]+tem); n                 ans+=tem; n             } n             //cout<<i<< === <<sum<< ==== <<ans<<endl; n         } n         cout<<ans<<endl; n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T POW(T B, T P) { n   if (P == 0) return 1; n   if (P & 1) n     return B * POW(B, P - 1); n   else n     return SQR(POW(B, P / 2)); n } n template <typename T> n inline T ModInv(T b, T m) { n   return BigMod(b, m - 2, m); n } n template <typename T> n inline T ABS(T a) { n   if (a < 0) n     return -a; n   else n     return a; n } n template <typename T> n inline T gcd(T a, T b) { n   if (a < 0) return gcd(-a, b); n   if (b < 0) return gcd(a, -b); n   return (b == 0) ? a : gcd(b, a % b); n } n template <typename T> n inline T lcm(T a, T b) { n   if (a < 0) return lcm(-a, b); n   if (b < 0) return lcm(a, -b); n   return a * (b / gcd(a, b)); n } n template <class T> n inline T BMOD(T p, T e, T m) { n   T ret = 1; n   while (e) { n     if (e & 1) ret = (ret * p) % m; n     p = (p * p) % m; n     e >>= 1; n   } n   return (T)ret; n } n int n, T; n int wt[200005], bit[200005]; n set<pair<int, int> > st; n void update(int pos) { n   while (pos < 200001) { n     bit[pos] += 1; n     pos += (pos & -pos); n   } n   return; n } n int query(int pos) { n   int sum = 0; n   while (pos > 0) { n     sum += bit[pos]; n     pos -= (pos & -pos); n   } n   return sum; n } n int main() { n   scanf( %d%d , &n, &T); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &wt[i]); n     st.insert(make_pair(wt[i] - i, i)); n   } n   int ans = 0; n   for (auto &i : st) { n     int xx = i.first; n     int yy = i.second; n     update(yy); n     int rng = T - 1 - xx; n     rng = min(n, rng); n     rng = min(rng, T - 1); n     if (rng <= 0) continue; n     int id = query(rng); n     ans = max(ans, id); n   } n   printf( %d , ans), puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 1025; n const int maxm = 10; n using namespace std; n int main() { n   int n; n   string s; n   cin >> n; n   cin >> s; n   int ans1 = 0, ans2 = 0; n   int cnt1 = 0; n   int f = 0; n   for (int i = 0; i < s.length(); i++) { n     if (s[i] ==  ( ) { n       f = 1; n       ans1 = max(ans1, cnt1); n       cnt1 = 0; n       continue; n     } n     if (s[i] ==  ) ) { n       f = 0; n       continue; n     } n     if (!f) { n       if (s[i] !=  _ ) { n         cnt1++; n       } else { n         ans1 = max(ans1, cnt1); n         cnt1 = 0; n       } n     } n     if (f) { n       if ((s[i - 1] ==  (  || s[i - 1] ==  _ ) && s[i] !=  _ ) { n         ans2++; n       } n     } n   } n   ans1 = max(ans1, cnt1); n   cnt1 = 0; n   cout << ans1 <<     << ans2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   if (abs(n - k) == 1 || ((n - k) == 0 && n > 0 && k > 0)) { n     cout <<  YES ; n   } else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n int main() { n   long long n, k; n   std::cin >> n >> k; n   long long c = std::max(n - 2 * k, 0LL); n   std::cout << n * (n - 1) / 2 - c * (c - 1) / 2 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, q; n int arr[110][110], r[10010], c[10010], v[10010], t[10010]; n int main() { n   scanf( %d , &n), scanf( %d , &m), scanf( %d , &q); n   for (int z = 0; z < q; ++z) { n     scanf( %d , &t[z]); n     if (t[z] == 1) scanf( %d , &r[z]); n     if (t[z] == 2) scanf( %d , &c[z]); n     if (t[z] == 3) scanf( %d , &r[z]), scanf( %d , &c[z]), scanf( %d , &v[z]); n   } n   reverse(t, t + q); n   reverse(c, c + q); n   reverse(r, r + q); n   reverse(v, v + q); n   for (int z = 0; z < q; ++z) { n     if (t[z] == 1) { n       --r[z]; n       v[z] = arr[r[z]][m - 1]; n       for (int j = m - 1; j > 0; --j) arr[r[z]][j] = arr[r[z]][j - 1]; n       arr[r[z]][0] = v[z]; n     } else if (t[z] == 2) { n       --c[z]; n       v[z] = arr[n - 1][c[z]]; n       for (int i = n - 1; i > 0; --i) arr[i][c[z]] = arr[i - 1][c[z]]; n       arr[0][c[z]] = v[z]; n     } else { n       arr[r[z] - 1][c[z] - 1] = v[z]; n     } n   } n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) printf( %d%c , arr[i][j],    n [j == m - 1]); n } n


#include <bits/stdc++.h> n using namespace std; n const int p = 1000000007; n int K; n struct Ma { n   int g[31][31]; n   Ma() { memset(g, 0, sizeof(g)); } n   int* operator[](int x) { return g[x]; } n   Ma operator*(const Ma& b) const { n     Ma res; n     for (int i = 0; i <= K; i++) n       for (int k = 0; k <= K; k++) n         if (g[i][k]) n           for (int j = 0; j <= K; j++) n             res.g[i][j] = (res.g[i][j] + (long long)g[i][k] * b.g[k][j]) % p; n     return res; n   } n   void w() { n     for (int i = 0; i <= K; i++) g[i][i] = 1; n   } n } A[105][30], res, pre[105][30], sub[105][30]; n int num[105], tot; n int main() { n   ios::sync_with_stdio(0); n   long long n; n   cin >> n >> K; n   while (n) num[tot++] = n % K, n /= K; n   for (int i = 0; i < K; i++) { n     for (int j = 0; j <= K; j++) A[0][i][j][j] = 1; n     for (int j = 0; j <= K; j++) A[0][i][j][i] = 1; n   } n   for (int i = 1; i <= tot; i++) { n     pre[i - 1][0] = A[i - 1][0]; n     for (int j = 1; j < K; j++) pre[i - 1][j] = pre[i - 1][j - 1] * A[i - 1][j]; n     sub[i - 1][K - 1] = A[i - 1][K - 1]; n     for (int j = K - 2; ~j; j--) n       sub[i - 1][j] = A[i - 1][j] * sub[i - 1][j + 1]; n     for (int j = 0; j < K; j++) { n       A[i][j].w(); n       A[i][j] = sub[i - 1][j]; n       if (j) A[i][j] = A[i][j] * pre[i - 1][j - 1]; n     } n   } n   res.g[K][K] = 1; n   for (int i = tot - 1, j = 0; ~i; i--) n     while (num[i]--) res = res * A[i][j], j = (j + 1) % K; n   int ans = 0; n   for (int i = 0; i <= K; i++) ans = (ans + res[K][i]) % p; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int l[6][6], h[6][6], a[6][6]; n int in[6]; n int cost; n int rec(int inNode, int outNode) { n   if (inNode == n - 1) return 0; n   int bestCost = -1; n   for (int f = l[inNode][outNode]; f <= h[inNode][outNode]; f++) { n     if (in[inNode] >= f && (outNode != n - 1 || in[inNode] == f)) { n       int nextIn = inNode, nextOut = outNode + 1; n       if (outNode == n) { n         nextIn = inNode + 1; n         nextOut = 0; n       }; n       if (nextIn == nextOut) nextOut++; n       in[inNode] -= f; n       in[outNode] += f; n       int val = rec(nextIn, nextOut); n       if (val != -1) { n         val += f * f; n         if (f > 0) val += a[inNode][outNode]; n         bestCost = max(bestCost, val); n       } n       in[inNode] += f; n       in[outNode] -= f; n     } n   } n   return bestCost; n } n int s, f; n int main() { n   cin >> n; n   for (int i = 0; i < n * (n - 1) / 2; i++) { n     cin >> s >> f; n     s--, f--; n     cin >> l[s][f] >> h[s][f] >> a[s][f]; n   } n   int maxCost = -1; n   int f; n   for (f = 0; f <= 25; f++) { n     memset(in, 0, sizeof in); n     in[0] = f; n     maxCost = rec(0, 1); n     if (maxCost != -1) break; n   } n   if (maxCost != -1) n     cout << f <<     << maxCost << endl; n   else n     cout << -1 <<     << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m; n long long side[300001]; n vector<long long> edges[300001]; n bool vis[300001]; n bool res = 1; n map<long long, long long> mp; n long long dfs(long long u, long long s) { n   vis[u] = 1; n   side[u] = s; n   ++mp[s]; n   long long ret = 1; n   for (long long i = 0; i < edges[u].size(); ++i) { n     long long nxt = edges[u][i]; n     if (vis[nxt] && side[u] == side[nxt]) { n       res = 0; n     } else if (!vis[nxt]) { n       ret += dfs(nxt, -s); n     } n   } n   return ret; n } n int32_t main() { n   cin >> n >> m; n   for (long long i = 0; i < m; ++i) { n     long long a, b; n     cin >> a >> b; n     edges[a].emplace_back(b); n     edges[b].emplace_back(a); n   } n   long long big = 0; n   long long cnt = 0; n   for (long long i = 1; i <= n; ++i) { n     if (vis[i]) continue; n     long long x = dfs(i, i); n     if (x == 2) ++cnt; n     big = max(big, x); n   } n   if (big == 1) { n     cout <<  3   << n * (n - 1) * (n - 2) / 6 <<   n ; n   } else if (big == 2) { n     cout <<  2   << cnt * (n - 2) <<   n ; n   } else if (res == 1) { n     long long ans = 0; n     for (auto it = mp.begin(); it != mp.end(); ++it) { n       long long x = (*it).second; n       ans += x * (x - 1) / 2; n     } n     cout <<  1   << ans <<   n ; n   } else { n     cout <<  0 1 n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 112345; n long long l[MAXN], r[MAXN]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) scanf( %lld %lld , l + i, r + i); n   sort(l, l + n); n   sort(r, r + n); n   long long ans = n; n   for (int i = 0; i < n; ++i) ans += max(l[i], r[i]); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int NMax = 5e3 + 5; n const int MMax = 1e3 + 5; n int N, M; n char input[NMax][MMax + 101]; n short bit[NMax][MMax][2]; n char solMin[MMax], solMax[MMax]; n map<string, int> ID; n struct elem { n   bool known; n   int op1, op2; n   char tor; n } v[NMax]; n int compute(int, int, int); n int main() { n   cin.sync_with_stdio(false); n   cin.tie(0); n   cin >> N >> M; n   cin.getline(input[0], MMax + 100); n   for (int i = 1; i <= N; ++i) { n     cin.getline(input[i] + 1, MMax + 100); n     string name =   ; n     int j = 1; n     while ( a  <= input[i][j] && input[i][j] <=  z ) { n       name += input[i][j++]; n     } n     ID[name] = i; n   } n   for (int i = 1; i <= N; ++i) { n     int j = 1; n     while (input[i][j] !=  = ) { n       ++j; n     } n     j += 2; n     if (input[i][j] ==  0  || input[i][j] ==  1 ) { n       v[i].known = true; n       for (int k = 1; k <= M; ++k) { n         bit[i][k][0] = bit[i][k][1] = input[i][j] -  0  + 1; n         ++j; n       } n     } else { n       string str; n       if (input[i][j] ==  ? ) { n         v[i].op1 = -1; n         ++j; n       } else { n         str =   ; n         while ( a  <= input[i][j] && input[i][j] <=  z ) { n           str += input[i][j]; n           ++j; n         } n         v[i].op1 = ID[str]; n       } n       ++j; n       if (input[i][j] ==  A ) { n         v[i].tor =  & ; n         j += 3; n       } else if (input[i][j] ==  O ) { n         v[i].tor =  | ; n         j += 2; n       } else { n         v[i].tor =  ^ ; n         j += 3; n       } n       ++j; n       if (input[i][j] ==  ? ) { n         v[i].op2 = -1; n       } else { n         str =   ; n         while ( a  <= input[i][j] && input[i][j] <=  z ) { n           str += input[i][j]; n           ++j; n         } n         v[i].op2 = ID[str]; n       } n     } n   } n   for (int b = 1; b <= M; ++b) { n     int nr1By1 = 0, nr1By0 = 0; n     for (int i = 1; i <= N; ++i) { n       nr1By1 += compute(i, b, 1); n       nr1By0 += compute(i, b, 0); n     } n     if (nr1By1 > nr1By0) { n       solMax[b] =  1 ; n     } else { n       solMax[b] =  0 ; n     } n     if (nr1By1 < nr1By0) { n       solMin[b] =  1 ; n     } else { n       solMin[b] =  0 ; n     } n   } n   cout << (solMin + 1) <<   n  << (solMax + 1); n   return 0; n } n int compute(int id, int b, int val) { n   if (bit[id][b][val] != 0) { n     return bit[id][b][val] - 1; n   } n   int f1, f2; n   if (v[id].op1 == -1) { n     f1 = val; n   } else { n     f1 = compute(v[id].op1, b, val); n   } n   if (v[id].op2 == -1) { n     f2 = val; n   } else { n     f2 = compute(v[id].op2, b, val); n   } n   int res; n   if (v[id].tor ==  & ) { n     res = f1 & f2; n   } else if (v[id].tor ==  | ) { n     res = f1 | f2; n   } else { n     res = f1 ^ f2; n   } n   bit[id][b][val] = res + 1; n   return res; n } n


#include <iostream> n #include <bits/stdc++.h> n #include<algorithm> n #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) n #define ll long long n #define MOD 1000000007 n #define f first n #define s second n #define pb push_back n #define nl   n  n #define mem(arr, value) memset(arr, value, sizeof(arr)) n  n #define vll vector<ll> n #define vb vector<bool> n #define vpll vector<pair<ll, ll>> n #define vvll vector<vector<ll>> n  n using namespace std; n  n // DEBUG n void __print(int x) { cerr << x; } n void __print(ll x) { cerr << x; } n void __print(double x) { cerr << x; } n void __print(char x) { cerr <<      << x <<     ; } n void __print(const char* x) { cerr <<     << x <<    ; } n void __print(const string& x) { cerr <<     << x <<    ; } n void __print(bool x) { cerr << (x ?  true  :  false ); } n  n template<typename T, typename V> n void __print(const pair<T, V>& x) { cerr <<  { ; __print(x.first); cerr <<  , ; __print(x.second); cerr <<  } ; } n template<typename T> n void __print(const T& x) { int _ = 0; cerr <<  { ; for (auto& i : x) cerr << (_++ ?  ,  :   ), __print(i); cerr <<  } ; } n void _print() { cerr <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr <<  ,  ; _print(v...); } n #ifndef ONLINE_JUDGE n #define dbg(x...) cerr <<  [  << #x <<  ] = [ ; _print(x) n #else n #define dbg(x...) n #endif n  n bool flag = false; n  n bool comp1(pair<pair<ll, ll>, pair<ll,ll> > a, pair<pair<ll, ll>, pair<ll, ll>> b) n { n     return a.s.s < b.s.s; n } n  n bool comp(pair<ll,ll> a, pair<ll, ll> b) n { n     return a.s < b.s; n } n  n struct myComp { n     constexpr bool operator()( n             pair< pair<ll,ll>, pair<ll,ll> > const& a, n             pair< pair<ll,ll>, pair<ll,ll> > const& b) n     const noexcept n     { n         return a.s.s < b.s.s; n     } n }; n ll digitSum(ll n){ll sum=0; while(n>0){sum += n%10;n=n/10;}return sum;} n  n  n void dfs(vvll &adj, vb &visited, ll v, ll e) n { n  n //    visited[v] = true; n  n     for (ll i=0; i<adj[v].size(); i++) n     { n         ll n = adj[v][i]; n         if (n==e) n         { n             flag = true; n             return; n         } n         dfs(adj, visited, n, e); n     } n  n //    return false; n  n } n  n bool isSubsetSum(int set[], int n, int sum) n { n     bool subset[n + 1][sum + 1]; n  n     for (int i = 0; i <= n; i++) n         subset[i][0] = true; n  n     for (int i = 1; i <= sum; i++) n         subset[0][i] = false; n  n     for (int i = 1; i <= n; i++) { n         for (int j = 1; j <= sum; j++) { n             if (j < set[i - 1]) n                 subset[i][j] = subset[i - 1][j]; n             if (j >= set[i - 1]) n                 subset[i][j] = subset[i - 1][j] n                                || subset[i - 1][j - set[i - 1]]; n         } n     } n  n  n     return subset[n][sum]; n } n  n void solve() n { n     ll n; n     cin>>n; n  n     ll b[n+2]; n  n     ll sum = 0; n     for (ll i=0; i<n+2; i++) n     { n         cin>>b[i]; n         sum+=b[i]; n     } n  n     sort(b, b+n+2); n  n     ll aa = b[n+1]; n     ll bb = b[n]; n     ll x=-1; n  n     ll temp1 = sum - b[n+1]; n     for (ll i=0; i<n+1; i++) n     { n         temp1 = temp1 - b[i]; n         if (temp1 == aa) n         { n             x = i; n             break; n         } n         temp1 += b[i]; n     } n  n     if (x == -1) n     { n         ll temp2 = sum - b[n] - b[n+1]; n  n         if (temp2 == b[n]) n         { n             x = n; n         } n     } n  n     if (x==-1) n         cout<<-1<<endl; n     else n     { n         for (ll i=0; i<n+1; i++) n         { n             if (i==x) n                 continue; n             else n                 cout<<b[i]<<   ; n         } n         cout<<endl; n     } n  n  n  n  n } n  n int main() n { n     fastio; n     ll t=1; n     cin>>t; n     while (t-->0) n     { n         solve(); n     } n  n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n int n, a[100005] = {0}, b[100005] = {0}, t, j, i, rn = 0, r[100005] = {0}, n        dis[100005] = {0}; n vector<int> v; n vector<int> ht; n int main() { n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     if (a[i] == 1) ht.push_back(i); n   } n   for (i = 1; i <= n; i++) { n     scanf( %d , &b[i]); n     dis[b[i]]++; n   } n   for (i = 0; i < ht.size(); i++) { n     v.clear(); n     v.push_back(ht[i]); n     while (1) { n       t = v.size(); n       if (b[v[t - 1]] == 0) { n         break; n       } n       if (dis[b[v[t - 1]]] == 1) n         v.push_back(b[v[t - 1]]); n       else n         break; n     } n     if (rn < v.size()) { n       rn = v.size(); n       for (j = 0; j < rn; j++) r[j] = v[j]; n     } n   } n   printf( %d n , rn); n   for (i = rn - 1; i >= 0; i--) printf( %d%c , r[i], i ?     :   n ); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n; n   scanf( %d , &n); n   int *mas = new int[n], m = 0; n   for (int i = 0; i < n; i++) { n     scanf( %d , &mas[m++]); n     while (m > 1 && mas[m - 1] == mas[m - 2]) { n       mas[m - 2]++; n       m--; n     } n   } n   printf( %d n , m); n   for (int i = 0; i < m; i++) printf( %d  , mas[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 101000; n const double eps = 1e-9, g = 9.8; n double V, angle, ca, sa, Y, K, x, y; n int i, j, k, l, m, n, o, p; n inline int sn(double x) { return x < -eps ? -1 : x > eps ? 1 : 0; } n inline double Sqr(double x) { return x * x; } n struct Point { n   double x, y; n   inline void read() { scanf( %lf%lf , &x, &y); } n   bool operator<(const Point &t) const { return x < t.x; } n } wall[N], ans[N]; n struct fire { n   double a, b; n   int i; n   fire() {} n   fire(double x, double y, int id) { n     a = x; n     b = y; n     i = id; n   } n   inline void read(int id) { n     scanf( %lf , &angle); n     ca = cos(angle); n     sa = sin(angle); n     a = g / (2 * Sqr(V * ca)); n     b = sa / ca; n     i = id; n     ans[i].x = (b / a); n     ans[i].y = 0; n   } n   bool operator<(const fire &t) const { n     double b1 = b - K * a, b2 = t.b - K * t.a; n     return (b1 < b2) || (!sn(b1 - b2) && (i < t.i)); n   } n } Tfire, P; n set<fire> F; n set<fire>::iterator pos; n int main() { n   scanf( %d%lf , &n, &V); n   K = (double)0.455555; n   for (i = 1; i <= n; i++) { n     Tfire.read(i); n     F.insert(Tfire); n   } n   scanf( %d , &m); n   for (i = 1; i <= m; i++) wall[i].read(); n   sort(wall + 1, wall + m + 1); n   for (i = 1; i <= m; i++) { n     K = wall[i].x; n     Y = wall[i].y / wall[i].x; n     while (1) { n       pos = F.lower_bound(fire(0, Y + eps, N)); n       if (pos == F.begin()) break; n       pos--; n       P = (*pos); n       j = P.i; n       if (ans[j].x > wall[i].x) { n         ans[j].x = wall[i].x; n         ans[j].y = (-P.a) * Sqr(wall[i].x) + wall[i].x * P.b; n       } n       F.erase(pos); n     } n   } n   for (i = 1; i <= n; i++) printf( %.9lf %.9lf n , ans[i].x, ans[i].y); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n string s[500001]; n char line[10]; n int dp[30][30]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     memset(line, 0, sizeof(line)); n     scanf( %s , line); n     s[i] = line; n   } n   memset(dp, 0, sizeof(dp)); n   for (int k = 0; k < n; ++k) { n     int len = (int)s[k].size(); n     int start = s[k][0] -  a , end = s[k][len - 1] -  a ; n     for (int i = 0; i < 26; ++i) n       if (dp[i][start]) { n         dp[i][end] = max(dp[i][end], dp[i][start] + len); n       } n     dp[start][end] = max(dp[start][end], len); n   } n   int ret = 0; n   for (int i = 0; i < 26; ++i) ret = max(ret, dp[i][i]); n   cout << ret << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     long long int a[n], b[n]; n     long long int sum = 0, suma = 0; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n       sum += a[i]; n     } n     for (int i = 0; i < n; i++) { n       cin >> b[i]; n       suma += b[i]; n     } n     long long int mina = *min_element(a, a + n); n     long long int mine = *min_element(b, b + n); n     long long int ans = 0; n     for (int i = 0; i < n; i++) { n       ans += min(abs(a[i] - mina), abs(b[i] - mine)); n       int aoeu = a[i]; n       a[i] -= min(abs(a[i] - mina), abs(b[i] - mine)); n       b[i] -= min(abs(aoeu - mina), abs(b[i] - mine)); n       ans += abs(a[i] - mina); n       ans += abs(b[i] - mine); n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int q[200010]; n int main() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     int l = s.length(); n     if (l == 1) { n       if (s[0] ==  L ) { n         cout << 2 << endl; n       } else { n         cout << 1 << endl; n       } n       continue; n     } n     int flag = 0; n     for (int i = 0; i < l; i++) { n       if (s[i] ==  R ) { n         flag = 1; n         break; n       } n     } n     if (flag == 0) { n       cout << l + 1 << endl; n       continue; n     } n     int k = 1; n     q[0] = 0; n     for (int i = 0; i < l; i++) { n       if (s[i] ==  R ) { n         q[k++] = i + 1; n       } n     } n     q[k] = l + 1; n     int maxn = 0; n     for (int i = 1; i <= k; i++) { n       int x = q[i] - q[i - 1]; n       maxn = max(maxn, x); n     } n     cout << maxn << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, j, i, l, t, k, x, y; n string s, a[10000001]; n int main() { n   cin >> n >> s; n   for (i = 1; i < n; i++) n     for (j = 0; j < n - i; j++) { n       t++; n       for (l = 0; l <= i; l++) { n         a[t] += s[j + l]; n       } n     } n   for (i = 1; i <= t; i++) { n     x = 0; n     y = 0; n     for (j = 0; j < a[i].size(); j++) n       if (a[i][j] ==  U ) n         y++; n       else if (a[i][j] ==  D ) n         y--; n       else if (a[i][j] ==  L ) n         x--; n       else if (a[i][j] ==  R ) n         x++; n     if (x == 0 && y == 0) k++; n   } n   cout << k; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5005; n int gcd[N]; n int GCD(int a, int b) { return b ? GCD(b, a % b) : a; } n vector<int> primes; n void Calc() { n   for (int i = 2; i * i <= 1000000000; i++) { n     bool is_prime = true; n     for (int p : primes) { n       if (p * p > i) { n         break; n       } n       is_prime &= i % p != 0; n     } n     if (is_prime) { n       primes.push_back(i); n     } n   } n } n vector<int> Factorize(int a) { n   vector<int> res; n   for (int p : primes) { n     if (p * p > a) { n       break; n     } n     while (a % p == 0) { n       res.push_back(p); n       a /= p; n     } n   } n   if (a != 1) { n     res.push_back(a); n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   Calc(); n   int n; n   int m; n   cin >> n >> m; n   vector<int> a(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   set<int> b; n   for (int i = 0; i < m; i++) { n     int p; n     cin >> p; n     b.insert(p); n   } n   gcd[0] = a[0]; n   for (int i = 1; i < n; i++) { n     gcd[i] = GCD(gcd[i - 1], a[i]); n   } n   int res = 0; n   int t = 1; n   for (int i = n - 1; i >= 0; i--) { n     auto factors = Factorize(gcd[i] / t); n     int d = 0; n     for (auto p : factors) { n       d += b.count(p) ? 1 : -1; n     } n     if (d > 0) { n       t = gcd[i]; n       res += d * (i + 1); n     } n   } n   for (auto num : a) { n     auto factors = Factorize(num); n     int d = 0; n     for (auto p : factors) { n       d += b.count(p) ? -1 : 1; n     } n     res += d; n   } n   cout << res <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n void expand(int); n int main() { n   int num; n   cin >> num; n   expand(num); n   cout <<   n ; n } n void expand(int value) { n   const char* const ones[20] = { zero ,     one ,        two ,       three , n                                  four ,     five ,       six ,       seven , n                                  eight ,    nine ,       ten ,       eleven , n                                  twelve ,   thirteen ,   fourteen ,  fifteen , n                                  sixteen ,  seventeen ,  eighteen ,  nineteen }; n   const char* const tens[10] = {  ,        ten ,    twenty ,  thirty , n                                  forty ,   fifty ,  sixty ,   seventy , n                                  eighty ,  ninety }; n   if (value < 0) { n     cout <<  minus  ; n     expand(-value); n   } else if (value >= 20) { n     cout << tens[value / 10]; n     if (value % 10) { n       cout <<  - ; n       expand(value % 10); n     } n   } else { n     cout << ones[value]; n   } n   return; n } n


#include <bits/stdc++.h> n #ifdef LOCAL n #include  debuger.hpp  n #else n #define DUMP(...) 1145141919810 n #define CHECK(...) (__VA_ARGS__) n #endif n  n const int kN = 200000 + 5; n const int INF = 0x3f3f3f3f; n std::vector<int> edges[kN]; n int n, q; n int header[kN],dfn[kN],tim,parent[kN],sz[kN]; n int depth[kN]; n int last[kN]; n  n void get_sz(int u, int fa) { n   sz[u] = 1; n   for (int v : edges[u]) { n     if (v == fa) continue; n     depth[v] = depth[u] + 1; n     get_sz(v, u); n     sz[u] += sz[v]; n   } n } n  n void rebuild(int u, int fa, int color) { n   dfn[u] = tim ++; n   parent[u] = fa; n   header[u] = color; n   last[color] = u; n   int p = -1; n   for (int v : edges[u]) { n     if (v == fa) continue; n     if (p == -1 || sz[v] > sz[p]) { n       p = v; n     } n   } n   if (p != -1) { n     rebuild(p, u, color); n   } n   for (int v : edges[u]) { n     if (v == fa || v == p) continue; n     rebuild(v, u, v); n   } n } n  n int get_lca(int a, int b) { n   while (header[a] != header[b]) { n     if (dfn[header[a]] < dfn[header[b]]) { n       std::swap(a,b); n     } n     a = parent[header[a]]; n   } n   return dfn[a] < dfn[b] ? a : b; n } n  n struct SegmentTree { n   int mx[kN << 1]; n  n   int get_id(int l, int r) const { return l + r | l != r; } n   void up(int l, int r) { n     if (l != r) { n       int mid = l + r >> 1; n       mx[get_id(l, r)] = std::max(mx[get_id(l, mid)], mx[get_id(mid + 1, r)]); n     } n   } n   void build(int l, int r) { n     mx[get_id(l, r)] = -INF; n     if (l != r) { n       int mid = l + r >> 1; n       build(l, mid); n       build(mid + 1, r); n     } n   } n   int query(int L, int R, int l, int r) const { n     if (r < L || R < l) return -INF; n     if (L <= l && r <= R) return mx[get_id(l, r)]; n     int mid = l + r >> 1; n     return std::max(query(L, R, l, mid), query(L, R, mid + 1, r)); n   } n   void modify(int p, int v, int l, int r) { n     if (l == r) { n       mx[get_id(l, r)] = v; n     } else { n       int mid = l + r >> 1; n       if (p <= mid) modify(p, v, l, mid); n       else modify(p, v, mid + 1, r); n       up(l, r); n     } n   } n  n } tree[2];  // 0: val-dfn[v], 1: val+dfn[v] n  n struct Fenwick { n   int arr[kN]; n  n   void update(int p, int dt) { for (int i = p; i >= 0; i -= ~i & i + 1) arr[i] += dt; } n   int query(int p) const { int ret = 0; for (int i = p; i < kN; i += ~i & i + 1) ret += arr[i]; return ret; } n  n   void hld_update(int u, int dt) { n     while (u != -1) { n       update(dfn[u], dt); n       if (dfn[header[u]] > 0) { n         update(dfn[header[u]] - 1, -dt); n       } n       u = parent[header[u]]; n     } n   } n } bit; n  n const int kLog = 19; n int pjump[kLog][kN]; n  n void init_jump() { n   for (int i = 0; i < n; ++i) { n     pjump[0][i] = parent[i]; n   } n   for (int i = 0; i + 1 < kLog; ++i) { n     for (int u = 0; u < n; ++u) { n       pjump[i + 1][u] = pjump[i][u] == -1 ? -1 : pjump[i][pjump[i][u]]; n     } n   } n } n  n int jump(int u, int d) { n   for (int i = 0; i < kLog; ++i) { n     if (d >> i & 1) { n       u = pjump[i][u]; n       if (u == -1) return 0; n     } n   } n   return u; n } n  n std::multiset<std::pair<int, int>> multiset; n  n std::multiset<int> virtual_sets[kN]; n  n int hld_query(int u, int guard) { n   int ret = 0; n   int v = u; n   int prev_head = -1; n   while (v != -1 && dfn[v] >= dfn[guard]) { n     int w = header[v]; n     if (dfn[w] < dfn[guard]) w = guard; n     int e = last[header[v]]; n  n     if (!virtual_sets[v].empty()) { n       int homologous_val = -INF; n       if (prev_head != -1) { n         int val = tree[1].query(dfn[prev_head], dfn[last[prev_head]], 0, n - 1); n         if (val != -INF) { n           homologous_val = val - dfn[prev_head] + 1; n         } n       } n       if (homologous_val != *virtual_sets[v].rbegin()) { n         ret = std::max(ret, depth[u] - depth[v] + *virtual_sets[v].rbegin()); n       } else if (virtual_sets[v].size() >= 2) { n         ret = std::max(ret, depth[u] - depth[v] + *std::prev(std::prev(virtual_sets[v].end()))); n       } n     } n     if (dfn[v] < dfn[e]) { n       int val = tree[1].query(dfn[v] + 1, dfn[e], 0, n - 1); n       if (val != -INF) { n         ret = std::max(ret, val - dfn[v] + depth[u] - depth[v]); n       } n     } n     if (dfn[w] < dfn[v]) { n       int val = tree[0].query(dfn[w], dfn[v] - 1, 0, n - 1); n       if (val != -INF) { n         ret = std::max(ret, val + dfn[v] + depth[u] - depth[v]); n       } n     } n     DUMP(v, ret); n     prev_head = header[v]; n     v = parent[header[v]]; n   } n   return ret; n } n  n #define ERASE_ONE(set, elem) (set).erase((set).find(elem)) n  n void hld_insert(int u) { n   std::vector<int> stack; n   for (int v = u; v != -1; v = parent[header[v]]) { n     stack.emplace_back(v); n   } n   for (int i = (int)stack.size() - 1; i >= 0; --i) { n     int x = stack[i]; n     int h = header[x]; n     if (parent[h] != -1) { n       int val = tree[1].query(dfn[h], dfn[last[h]], 0, n - 1); n       if (val != -INF) { n         ERASE_ONE(virtual_sets[parent[h]], val - dfn[h] + 1); n       } n     } n   } n   virtual_sets[u].emplace(0); n   for (int x : stack) { n     int val = *virtual_sets[x].rbegin(); n     tree[0].modify(dfn[x], val - dfn[x], 0, n - 1); n     tree[1].modify(dfn[x], val + dfn[x], 0, n - 1); n     int h = header[x]; n     if (parent[h] != -1) { n       int val = tree[1].query(dfn[h], dfn[last[h]], 0, n - 1); n       if (val != -INF) { n         virtual_sets[parent[h]].emplace(val - dfn[h] + 1); n       } n     } n   } n } n  n void hld_remove(int u) { n   std::vector<int> stack; n   for (int v = u; v != -1; v = parent[header[v]]) { n     stack.emplace_back(v); n   } n   for (int i = (int)stack.size() - 1; i >= 0; --i) { n     int x = stack[i]; n     int h = header[x]; n     if (parent[h] != -1) { n       int val = tree[1].query(dfn[h], dfn[last[h]], 0, n - 1); n       if (val != -INF) { n         ERASE_ONE(virtual_sets[parent[h]], val - dfn[h] + 1); n       } n     } n   } n   ERASE_ONE(virtual_sets[u], 0); n   for (int x : stack) { n     if (!virtual_sets[x].empty()) { n       int val = *virtual_sets[x].rbegin(); n       tree[0].modify(dfn[x], val - dfn[x], 0, n - 1); n       tree[1].modify(dfn[x], val + dfn[x], 0, n - 1); n     } else { n       tree[0].modify(dfn[x], -INF, 0, n - 1); n       tree[1].modify(dfn[x], -INF, 0, n - 1); n     } n     int h = header[x]; n     if (parent[h] != -1) { n       int val = tree[1].query(dfn[h], dfn[last[h]], 0, n - 1); n       if (val != -INF) { n         virtual_sets[parent[h]].emplace(val - dfn[h] + 1); n       } n     } n   } n } n  n int main() { n   scanf( %d%d , &n, &q); n   for (int i = 0; i < n - 1; ++i) { n     int a, b; n     scanf( %d%d , &a, &b); --a; --b; n     edges[a].emplace_back(b); n     edges[b].emplace_back(a); n   } n   get_sz(0, -1); n   rebuild(0, -1, 0); n   tree[0].build(0, n - 1); n   tree[1].build(0, n - 1); n   init_jump(); n   while (q--) { n     int op; n     scanf( %d , &op); n     if (op == 1) { n       int u, v; n       scanf( %d%d , &u, &v); --u; --v; n       int lca = get_lca(u, v); n       bit.hld_update(u, 1); n       bit.hld_update(v, 1); n       bit.hld_update(lca, -1); n       multiset.emplace(depth[lca], lca); n       hld_insert(lca); n     } else if (op == 2) { n       int u, v; n       scanf( %d%d , &u, &v); --u; --v; n       int lca = get_lca(u, v); n       bit.hld_update(u, -1); n       bit.hld_update(v, -1); n       bit.hld_update(lca, 1); n       ERASE_ONE(multiset, std::make_pair(depth[lca], lca)); n       hld_remove(lca); n     } else { n       int d; n       scanf( %d , &d); n       CHECK(!multiset.empty()); n       int v = multiset.rbegin()->second; n       int guard = jump(v, d + d); n       if (bit.query(dfn[guard]) != bit.query(dfn[0])) { n         puts( No ); n         continue; n       } n       puts(hld_query(v, guard) <= d + d ?  Yes  :  No ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n int n, a[N], ans[N], cnt, x, y; n bool Ok = 0; n int main() { n   cin >> n; n   for (int i = 0; i <= n; i++) { n     cin >> a[i]; n     if (i) Ok |= (a[i] > 1 && a[i - 1] > 1); n   } n   if (!Ok) { n     cout <<  perfect ; n   } else { n     cout <<  ambiguous  << endl; n     cnt = 1; n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= a[i]; j++) { n         ans[cnt + j] = cnt; n       } n       if (a[i] > 1 && a[i - 1] > 1) { n         x = cnt + a[i]; n         y = cnt - 1; n       } n       cnt += a[i]; n     } n     for (int i = 1; i <= cnt; i++) { n       cout << ans[i] <<    ; n     } n     cout << endl; n     ans[x] = y; n     for (int j = 1; j <= cnt; j++) { n       cout << ans[j] <<    ; n     } n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int a[250]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     int N = 250; n     for (int j = 0; j < N; j++) scanf( %d , a + j); n     double M = 0; n     for (int j = 0; j < N; j++) M += a[j]; n     M /= N; n     double v = 0; n     for (int j = 0; j < N; j++) v += (a[j] - M) * (a[j] - M); n     v /= N; n     if (v < 3 * M) n       puts( poisson ); n     else n       puts( uniform ); n   } n } n


#include <bits/stdc++.h> n #define lo long long n #define KKH  1546C  n #define fi first n #define se second n #define BIT(x,k) (((x)>>(k-1))&1) n #define rs(x,a) memset(x,(a),sizeof x) n #define off(x,k) ((x)^(1<<(k-1))) n using namespace std; n void START() n { n     #ifdef toilagun2004 n     freopen(KKH .inp , r ,stdin); n     freopen(KKH .out , w ,stdout); n     #endif // toilagun2004 n     ios::sync_with_stdio(0); n     cin.tie(0); n     cout.tie(0); n } n const int gh=1e5+6; n int n,a[gh],g[gh],d[gh]; n int main() n { n  START(); n     int T; n     cin>>T; n     while(T--){ n         cin>>n; n         rs(d,0); n         rs(g,0); n         for (int i=1;i<=n;i++) n         { n             cin>>a[i]; n             if (i&1) d[a[i]]++; n             else g[a[i]]++; n         } n         sort(a+1,a+n+1); n         for (int i=1;i<=n;i++) n         { n             if (i&1) d[a[i]]--; n             else g[a[i]]--; n         } n         bool hung=true; n         for(int e=1;e<=100000 && hung;e++) n             if (d[e] || g[e]) hung=false; n         if (hung) cout<< YES n ; n         else cout<< NO n ; n     } n }


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1000000000000000000LL; n long long ff(long long a, long long b, long long c) { n   if ((a + b) % 2 != 0) return INF; n   long long x = (a + b) / 2; n   if (a < b) swap(a, b); n   long long y = 0, z = (a - b) / 2; n   return x + y + z; n } n int main() { n   long long a, b, c; n   scanf( %I64d%I64d%I64d , &a, &b, &c); n   printf( %I64d n , min(min(ff(a, b, c), ff(b, c, a)), ff(c, a, b))); n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n typedef long long int ll; n  n  n void solve() n { n     ll n; cin>>n; n     vector<ll> v; n     for(int i=0;i<n;i++) n     { n         ll x; cin>>x; n         v.push_back(x); n     } n     sort(v.begin(),v.end()); n     ll cost=0; n     for(ll i=2; i<n; ++i) n     { n         ll j=lower_bound(v.begin(),v.end(),v[i]-2)-v.begin(); n         ll x=(i-j); n         cost+=(x*(x-1))/2; n     }cout<<cost<<  n ; n } n  n  n  n     int main() n     { n int t; n cin>>t; n while(t--) n { n  n solve(); n     } n  n   }


#include <bits/stdc++.h> n using namespace std; n long long mod = 1000000007; n long long poww(long long num, long long p) { n   long long ret = 1; n   while (p != 0) { n     if (p & 1) { n       ret = (ret * num) % mod; n     } n     num = (num * num) % mod; n     p = p >> 1; n   } n   return ret; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     long long n, p; n     cin >> n >> p; n     long long a[n]; n     for (int i = 0; i < n; i++) cin >> a[i]; n     if (p == 1) { n       if (n % 2 == 1) n         cout <<  1 n ; n       else n         cout <<  0 n ; n       continue; n     } n     sort(a, a + n); n     stack<pair<long long, long long> > st; n     for (int i = n - 1; i >= 0; i--) { n       if (st.size() == 0) { n         st.push(make_pair(a[i], 1)); n       } else { n         long long t1 = a[i]; n         long long cnt = 1; n         while (true) { n           pair<long long, long long> t2 = st.top(); n           if (t2.first > t1) { n             st.push(make_pair(t1, cnt)); n             break; n           } else if (t2.first == t1) { n             st.pop(); n             if (st.size() == 0) n               break; n             else { n               cnt = t2.second + 1; n               if (cnt == p) { n                 cnt = 1; n                 t1++; n               } else { n                 st.push(make_pair(t1, cnt)); n                 break; n               } n             } n           } n         } n       } n     } n     if (st.size() == 0) { n       cout << 0 <<   n ; n     } else { n       long long ans = 0; n       while (st.size() != 1) { n         pair<long long, long long> temp = st.top(); n         st.pop(); n         ans = (ans + (temp.second * poww(p, temp.first)) % mod) % mod; n       } n       pair<long long, long long> temp = st.top(); n       st.pop(); n       long long res = poww(p, temp.first); n       res = (res - ans + mod) % mod; n       cout << res <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   cin >> n; n   long long a[1010000]; n   long long s[1010000]; n   memset(a, 0, sizeof(a)); n   for (int i = 0; i < n; i++) { n     scanf( %lld , a + i); n   } n   vector<pair<long long, long long>> pll; n   for (int i = 0; i < n; i++) { n     long long sum = a[i]; n     long long sz = 1; n     while (!pll.empty() && n            pll.back().first * 1.0 / pll.back().second > sum * 1.0 / sz) { n       sum += pll.back().first; n       sz += pll.back().second; n       pll.pop_back(); n     } n     pll.push_back(make_pair(sum, sz)); n   } n   for (int i = 0; i < pll.size(); i++) { n     for (int j = 0; j < pll[i].second; j++) { n       if (!i && !j) n         printf( %.9lf , pll[i].first * 1.0 / pll[i].second); n       else n         printf(  %.9lf , pll[i].first * 1.0 / pll[i].second); n     } n   } n   return; n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a1, t1, a2, t2, n1 = 0, n2 = 0; n   cin >> n >> a1 >> a2 >> t1 >> t2; n   n1 += t1 * 2; n   n2 += t2 * 2; n   n1 += a1 * n; n   n2 += a2 * n; n   if (n1 > n2) cout <<  Second ; n   if (n2 > n1) cout <<  First ; n   if (n1 == n2) cout <<  Friendship ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n long long DP[N]; n int n; n int a[N]; n int freq[N]; n long long solve(int num) { n   if (num < 1) return 0; n   if (DP[num] != -1) return DP[num]; n   return DP[num] = n              max((long long)num * freq[num] + solve(num - 2), solve(num - 1)); n } n int main() { n   memset(DP, -1, sizeof DP); n   scanf( %d , &n); n   int mx = 0; n   for (int i = 0; i < n; ++i) { n     scanf( %d , a + i); n     ++freq[a[i]]; n     if (a[i] > mx) mx = a[i]; n   } n   printf( %lld n , solve(mx)); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n bool reachable(vector<set<int>> g, int u, int v) { n   if (g[u].find(v) == g[u].end()) { n     for (auto x : g[u]) { n       if (g[x].find(v) != g[x].end()) return true; n     } n   } else { n     return true; n   } n   return false; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   set<pair<int, int>> edges; n   set<int> badnodes; n   while (m--) { n     int x, y; n     cin >> x >> y; n     --x; n     --y; n     edges.insert({x, y}); n     edges.insert({y, x}); n     badnodes.insert(x); n     badnodes.insert(y); n   } n   int c = 0; n   for (int u = 0; u < n; ++u) { n     if (badnodes.find(u) == badnodes.end()) c = u; n   } n   cout << n - 1 << endl; n   vector<set<int>> g(n); n   for (int u = 0; u < n; ++u) { n     if (u == c) continue; n     if (reachable(g, u, c)) continue; n     if (edges.find({u, c}) == edges.end()) { n       g[c].insert(u); n       g[u].insert(c); n       cout << c + 1 <<     << u + 1 << endl; n     } else { n       for (int v = 0; v < n; ++v) { n         if (edges.find({u, v}) == edges.end()) { n           g[u].insert(v); n           g[v].insert(u); n           cout << u + 1 <<     << v + 1 << endl; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long parent[200001], Size[200001]; n long long find_set(long long x) { n   if (x == parent[x]) return parent[x]; n   return parent[x] = find_set(parent[x]); n } n void Union(long long x, long long y) { n   x = find_set(x); n   y = find_set(y); n   if (Size[x] > Size[y]) { n     parent[y] = parent[x]; n     Size[x] += Size[y]; n   } else { n     parent[x] = parent[y]; n     Size[y] += Size[x]; n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long t; n   cin >> t; n   while (t--) { n     long long n, k; n     cin >> n >> k; n     string s; n     cin >> s; n     for (long long i = 0; i <= n; i++) { n       parent[i] = i; n       Size[i] = 1; n     } n     for (long long i = 0; i < n - k; i++) { n       Union(i, i + k); n       if (i < n / 2) Union(i, n - i - 1); n     } n     vector<long long> v[n + 1]; n     for (long long i = 0; i < n; i++) { n       long long p = find_set(i); n       v[p].push_back(s[i] -  a ); n     } n     long long ans = 0; n     for (long long i = 0; i < n; i++) { n       if (v[i].size() == 0) continue; n       map<long long, long long> mm; n       for (auto j : v[i]) { n         mm[j]++; n       } n       long long Max = INT_MIN; n       for (auto i : mm) Max = max(Max, i.second); n       ans += v[i].size() - Max; n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n const int MNN = 2e3 + 1; n using namespace std; n int n, b[MNN][MNN], mx; n bool used[MNN]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       cin >> b[i][j]; n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       mx = max(mx, b[i][j]); n     } n     if (used[mx] == 1) { n       cout << mx + 1 <<    ; n       used[mx + 1] = 1; n     } else { n       cout << mx <<    ; n       used[mx] = 1; n     } n     mx = 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename G> n struct triple { n   G first, second, T; n }; n struct BIT { n   vector<int> v; n   BIT(int n) : v(n + 10) {} n   void upd(int p) { n     for (; p < v.size(); p += (p & (-p))) ++v[p]; n   } n   int query(int p) { n     int x = 0; n     for (; p > 0; p -= (p & (-p))) x += v[p]; n     return x; n   } n }; n struct fraction { n   long long x, y; n   bool operator<(fraction o) { return x * o.y < y * o.x; } n   bool operator>(fraction o) { return x * o.y > y * o.x; } n   bool operator!=(fraction o) { return x * o.y != y * o.x; } n }; n struct info { n   fraction f1, f2; n   int ind; n   info(long long x, long long v, long long _ind, long long w) : ind(_ind) { n     f1 = {abs(x), abs(v - w)}; n     f2 = {abs(x), abs(v + w)}; n   } n }; n bool cmp1(info x, info y) { n   if (x.f1 != y.f1) return x.f1 < y.f1; n   if (x.f2 != y.f2) return x.f2 > y.f2; n   return x.ind < y.ind; n } n bool cmp2(info x, info y) { n   if (x.f2 != y.f2) return x.f2 < y.f2; n   if (x.f1 != y.f1) return x.f1 > y.f1; n   return x.ind > y.ind; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, w; n   cin >> n >> w; n   vector<info> a; n   for (int i = 0; i < n; i++) { n     int x, v; n     cin >> x >> v; n     a.push_back({x, v, i, w}); n   } n   vector<int> t1(n), t2(n); n   sort(a.begin(), a.end(), cmp1); n   for (int i = 0; i < n; i++) t1[i] = a[i].ind; n   sort(a.begin(), a.end(), cmp2); n   for (int i = 0; i < n; i++) t2[i] = a[i].ind; n   vector<int> p(n); n   for (int i = 0; i < n; i++) p[t1[i]] = i + 1; n   long long ans = 0; n   BIT abi(n); n   for (int i = 0; i < n; i++) { n     ans += i - abi.query(p[t2[i]]); n     abi.upd(p[t2[i]]); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n bool prime[N + 5]; n int vis[11111]; n int main() { n   int tc = 1; n   cin >> tc; n   while (tc--) { n     int n; n     int m; n     cin >> n >> m; n     int ara[n + 1]; n     for (int i = 0; i < n; i++) { n       cin >> ara[i]; n     } n     if (m < n || n == 2) { n       cout <<  -1  << endl; n       continue; n     } else { n       int sum = 0; n       for (int i = 0; i < n - 1; i++) { n         sum += ara[i]; n         sum += ara[i + 1]; n       } n       sum += ara[n - 1]; n       sum += ara[0]; n       cout << sum << endl; n       for (int i = 1; i < n; i++) { n         cout << i <<     << i + 1 << endl; n       } n       cout << 1 <<     << n << endl; n     } n   } n } n