#include <bits/stdc++.h> n using namespace std; n const long long M = 100 + 19; n long long d[M][M][M], n, m, k; n char c; n struct tuplee { n   long long x, y, z; n }; n vector<tuplee> v1, v2; n long long go(long long x1, long long y1, long long z1, long long x2, n              long long y2, long long z2) { n   if (x1 > x2 || y1 > y2 || z1 > z2 || d[x1][y1][z1] == 0) return 0; n   if (x1 == x2 && y1 == y2 && z1 == z2) return 1; n   return go(x1 + 1, y1, z1, x2, y2, z2) + go(x1, y1 + 1, z1, x2, y2, z2) + n          go(x1, y1, z1 + 1, x2, y2, z2); n } n inline bool is_bad(long long x, long long y, long long z) { n   tuplee T; n   v1.clear(); n   v2.clear(); n   if (d[x - 1][y][z]) T.x = x - 1, T.y = y, T.z = z, v1.push_back(T); n   if (d[x][y - 1][z]) T.x = x, T.y = y - 1, T.z = z, v1.push_back(T); n   if (d[x][y][z - 1]) T.x = x, T.y = y, T.z = z - 1, v1.push_back(T); n   if (d[x + 1][y][z]) T.x = x + 1, T.y = y, T.z = z, v2.push_back(T); n   if (d[x][y + 1][z]) T.x = x, T.y = y + 1, T.z = z, v2.push_back(T); n   if (d[x][y][z + 1]) T.x = x, T.y = y, T.z = z + 1, v2.push_back(T); n   if (v1.empty() || v2.empty()) return false; n   for (long long i = (0); i <= (v1.size() - 1); i++) { n     for (long long j = (0); j <= (v2.size() - 1); j++) { n       if (go(v1[i].x, v1[i].y, v1[i].z, v2[j].x, v2[j].y, v2[j].z) == 1) n         return true; n     } n   } n   return false; n } n int main() { n   ios::sync_with_stdio(false); n   scanf( %lld%lld%lld , &n, &m, &k); n   long long ct = 0; n   for (long long i1 = (1); i1 <= (n); i1++) n     for (long long i2 = (1); i2 <= (m); i2++) n       for (long long i3 = (1); i3 <= (k); i3++) n         scanf(  %c , &c), d[i1][i2][i3] = (c -  0 ); n   for (long long i1 = (1); i1 <= (n); i1++) n     for (long long i2 = (1); i2 <= (m); i2++) n       for (long long i3 = (1); i3 <= (k); i3++) { n         if (d[i1][i2][i3] && is_bad(i1, i2, i3)) ct++; n       } n   printf( %lld , ct); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 2000005; n const int inf = 1000000009; n int c, Len, Ans, id; n char String[N], s[N]; n int D[N], num[N], n, Q[N], h, t, x; n vector<int> e[N]; n vector<char> v[N]; n void getstr(char *s, int &l) { n   l = 0; n   while (String[c] !=  ,  && c < Len) s[l++] = String[c], c++; n   c++; n   s[l] = 0; n   return; n } n void getdig(int &x) { n   x = 0; n   while (String[c] !=  ,  && c < Len) x = x * 10 + String[c] - 48, c++; n   c++; n   return; n } n void Dfs(int x, int y, int dep) { n   Ans = max(Ans, dep); n   D[x] = dep; n   for (int i = ((int)1); i <= ((int)y); i++) { n     getstr(s, n); n     id++; n     for (int i = ((int)0); i <= ((int)n - 1); i++) v[id].push_back(s[i]); n     e[x].push_back(id); n     getdig(num[id]); n     Dfs(id, num[id], dep + 1); n   } n } n void write(int x) { n   for (int i = ((int)0); i <= ((int)v[x].size() - 1); i++) putchar(v[x][i]); n   putchar(32); n   return; n } n int main() { n   scanf( %s , String); n   Len = strlen(String) - 1; n   while (c < Len) { n     getstr(s, n); n     id++; n     e[0].push_back(id); n     for (int i = ((int)0); i <= ((int)n - 1); i++) v[id].push_back(s[i]); n     getdig(num[id]); n     Dfs(id, num[id], 1); n   } n   printf( %d , Ans); n   Q[t = 1] = 0; n   h = 0; n   while (h < t) { n     x = Q[++h]; n     if (D[x] != D[Q[h - 1]]) puts(  ); n     if (x != 0) write(x); n     for (int j = ((int)0); j <= ((int)e[x].size() - 1); j++) Q[++t] = e[x][j]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, x, y, z; n   cin >> t; n   while (t--) { n     cin >> x >> y >> z; n     if ((x != y && y != z && x != z) || n         ((x == y && x < z) || (y == z && y < x) || (z == x && z < y))) { n       cout <<  NO n ; n       continue; n     } else { n       cout <<  YES n ; n       if (x == y && y == z) { n         cout << x <<     << x <<     << x <<   n ; n       } else { n         if (x == y) { n           cout << x <<     << z <<     << z <<   n ; n         } else if (y == z) { n           cout << y <<     << x <<     << x <<   n ; n         } else { n           cout << z <<     << y <<     << y <<   n ; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n const int mod = 998244353, g = 3, _g = 332748118, maxn = 2e5 + 9; n inline int Pow(int base, int b) { n   int ret(1); n   while (b) { n     if (b & 1) ret = 1ll * ret * base % mod; n     base = 1ll * base * base % mod; n     b >>= 1; n   } n   return ret; n } n int r[maxn], W[maxn]; n inline int Fir(int n) { n   int limit(1), len(0), up(n << 1); n   while (limit < up) { n     limit <<= 1; n     ++len; n   } n   for (int i = 0; i < limit; ++i) n     r[i] = (r[i >> 1] >> 1) | ((i & 1) << len - 1); n   return limit; n } n inline void NTT(int *a, int n, int type) { n   for (int i = 0; i < n; ++i) n     if (i < r[i]) std::swap(a[i], a[r[i]]); n   for (int mid = 1; mid < n; mid <<= 1) { n     int wn(Pow(type ? g : _g, (mod - 1) / (mid << 1))); n     W[0] = 1; n     for (int i = 1; i < mid; ++i) W[i] = 1ll * W[i - 1] * wn % mod; n     for (int R = mid << 1, j = 0; j < n; j += R) n       for (int k = 0; k < mid; ++k) { n         int x(a[j + k]), y(1ll * W[k] * a[j + mid + k] % mod); n         a[j + k] = 1ll * (x + y) % mod; n         a[j + mid + k] = 1ll * (x - y + mod) % mod; n       } n   } n } n int T[maxn], F[maxn], H[maxn], G[maxn], fac[maxn], fav[maxn], tmp[maxn], n     sum[maxn], B[maxn]; n inline int Mul(int n, int *a, int *b, int *ans) { n   int limit(Fir(n)); n   NTT(a, limit, 1); n   NTT(b, limit, 1); n   for (int i = 0; i < limit; ++i) ans[i] = 1ll * a[i] * b[i] % mod; n   NTT(ans, limit, 0); n   for (int i = ((n - 1) << 1) + 1; i < limit; ++i) a[i] = b[i] = 0; n   return Pow(limit, mod - 2); n } n inline void Solve(int n, int *a) { n   if (!n) { n     a[0] = 1; n     return; n   } n   if (n == 1) { n     a[1] = 1; n     return; n   } n   int len(n / 2); n   Solve(len, a); n   for (int i = 0; i <= len; ++i) { n     F[i] = 1ll * Pow(len, i) * fav[i] % mod; n     H[i] = 1ll * fac[i] * a[i] % mod; n   } n   for (int i = 0; i <= (len >> 1); ++i) std::swap(H[i], H[len - i]); n   int limit(Fir(len + 1)); n   for (int i = len + 1; i < limit; ++i) F[i] = H[i] = 0; n   NTT(F, limit, 1); n   NTT(H, limit, 1); n   for (int i = 0; i < limit; ++i) G[i] = 1ll * F[i] * H[i] % mod; n   NTT(G, limit, 0); n   int ty(Pow(limit, mod - 2)); n   for (int i = 0; i <= len; ++i) n     tmp[i] = 1ll * G[len - i] * ty % mod * Pow(fac[i], mod - 2) % mod; n   int val(Mul(len + 1, a, tmp, B)); n   for (int i = 0; i <= (len << 1); ++i) a[i] = 1ll * B[i] * val % mod; n   if (n & 1) { n     for (int i = 0; i < n; ++i) T[i] = a[i]; n     for (int i = 1; i <= n; ++i) n       a[i] = 1ll * (T[i - 1] + 1ll * (n - 1) * T[i] % mod) % mod; n   } n } n inline int Get_c(int n, int m) { n   return 1ll * fac[n] * fav[m] % mod * fav[n - m] % mod; n } n int n, a, b; n int ans[maxn]; n int main() { n   scanf( %d%d%d , &n, &a, &b); n   fac[0] = fac[1] = 1; n   for (int i = 2; i <= n; ++i) fac[i] = 1ll * fac[i - 1] * i % mod; n   fav[n] = Pow(fac[n], mod - 2); n   for (int i = n; i >= 1; --i) fav[i - 1] = 1ll * fav[i] * i % mod; n   Solve(n - 1, ans); n   printf( %d n , 1ll * ans[a + b - 2] * Get_c(a + b - 2, a - 1) % mod); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int d, k, a, b, t; n   cin >> d >> k >> a >> b >> t; n   long long int res = 0; n   if (d > k) { n     if (t + a * k > b * k) { n       res = k * a + (d - k) * b; n     } else { n       long long int x = n           ((d % k) * b < (d % k) * a + t ? (d % k) * b : ((d % k) * a + t)); n       res = k * a + (d / k - 1) * (t + k * a) + x; n     } n   } else { n     res = a * d; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool vis[100005]; n int main() { n   int n; n   while (cin >> n) { n     int i, tag = 0, num = 0; n     for (i = 5;; i += 5) { n       int x = i; n       while (x % 5 == 0 && x) { n         num++; n         x /= 5; n       } n       if (num == n) { n         tag = 1; n         break; n       } else if (num > n) n         break; n     } n     if (tag) { n       cout <<  5  << endl; n       for (int p = 0; p < 5; p++) cout << i + p <<    ; n       cout << endl; n     } else n       cout <<  0  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n void qread(int &x) { n   int neg = 1; n   x = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) neg = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) x = 10 * x + c -  0 , c = getchar(); n   x *= neg; n } n const int maxn = 500005; n int n; n char s[maxn]; n int dp[maxn]; n namespace Sgt { n int sz; n int t[maxn * 4]; n void init() { n   for (sz = 1; sz <= n; sz <<= 1) n     ; n   for (int(i) = 0; (i) < sz << 1; i++) t[i] = 0; n } n void upd(int v, int val) { n   v += sz; n   t[v] = val; n   for (v >>= 1; v; v >>= 1) t[v] = max(t[v << 1], t[v << 1 | 1]); n } n int qry(int l, int r) { n   int res = 0; n   for (l += sz, r += sz; l <= r; l >>= 1, r >>= 1) { n     if (l & 1) res = max(res, t[l]), l++; n     if ((r + 1) & 1) res = max(res, t[r]), r--; n   } n   return res; n } n }  // namespace Sgt n namespace St { n const int mlog = 20; n int mn[maxn][mlog]; n void init(int *a) { n   for (int(i) = 0; (i) < n; i++) mn[i][0] = a[i]; n   for (int(j) = 1; (j) <= mlog - 1; j++) n     for (int(i) = 0; (i) < n; i++) { n       if (i + (1 << j - 1) >= n) n         mn[i][j] = 1e9; n       else n         mn[i][j] = min(mn[i][j - 1], mn[i + (1 << j - 1)][j - 1]); n     } n } n pair<int, int> getval(int p, int len) { n   int l = p, r = p; n   for (int i = mlog - 1; i >= 0; i--) { n     if (l - (1 << i) >= 0 && mn[l - (1 << i) + 1][i] >= len) l -= (1 << i); n     if (r + (1 << i) < n && mn[r + 1][i] >= len) r += (1 << i); n   } n   return make_pair(l, r); n } n }  // namespace St n namespace Sa { n pair<int, int> cur[maxn]; n int tmp[maxn], sum[maxn], sa[maxn], rk[maxn], lcp[maxn]; n void rsort() { n   int mx = 0; n   for (int(i) = 0; (i) < n; i++) n     sum[cur[i].second]++, mx = max(mx, cur[i].second); n   for (int(i) = 1; (i) <= mx; i++) sum[i] += sum[i - 1]; n   for (int i = n - 1; i >= 0; i--) tmp[--sum[cur[i].second]] = i; n   for (int(i) = 0; (i) < mx + 1; i++) sum[i] = 0; n   mx = 0; n   for (int(i) = 0; (i) < n; i++) n     sum[cur[tmp[i]].first]++, mx = max(mx, cur[tmp[i]].first); n   for (int(i) = 1; (i) <= mx; i++) sum[i] += sum[i - 1]; n   for (int i = n - 1; i >= 0; i--) sa[--sum[cur[tmp[i]].first]] = tmp[i]; n   for (int(i) = 0; (i) < mx + 1; i++) sum[i] = 0; n   rk[sa[0]] = 0; n   for (int(i) = 1; (i) <= n - 1; i++) n     rk[sa[i]] = rk[sa[i - 1]] + (cur[sa[i]] != cur[sa[i - 1]]); n } n void init() { n   for (int(i) = 0; (i) < n; i++) cur[i] = make_pair(s[i], s[i]); n   rsort(); n   for (int m = 1; m <= n; m <<= 1) { n     for (int(i) = 0; (i) < n; i++) n       cur[i] = make_pair(rk[i] + 1, i + m < n ? (rk[i + m] + 1) : 0); n     rsort(); n   } n   int h = 0; n   for (int(i) = 0; (i) < n; i++) { n     if (rk[i] == 0) continue; n     int j = sa[rk[i] - 1]; n     if (h) h--; n     while (i + h < n && j + h < n && s[i + h] == s[j + h]) h++; n     lcp[rk[i]] = h; n   } n   St::init(lcp); n } n }  // namespace Sa n int main() { n   scanf( %d%s , &n, s); n   Sgt::init(); n   Sa::init(); n   int ans = 0; n   for (int i = n - 1; i >= 0; i--) { n     dp[i] = dp[i + 1] + 1; n     for (;;) { n       bool ok = false; n       pair<int, int> cur = St::getval(Sa::rk[i], dp[i] - 1); n       ok |= (Sgt::qry(cur.first, cur.second) >= dp[i] - 1); n       if (i + 1 < n) { n         cur = St::getval(Sa::rk[i + 1], dp[i] - 1); n         ok |= (Sgt::qry(cur.first, cur.second) >= dp[i] - 1); n       } n       if (ok) break; n       if (i + dp[i] - 1 < n) Sgt::upd(Sa::rk[i + dp[i] - 1], dp[i + dp[i] - 1]); n       dp[i]--; n     } n     ans = max(ans, dp[i]); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n int k, t; n int a[11]; n long long b[16], c[11], ele[11], hash_val; n map<pair<int, long long>, long long> vis; n void add(int i) { n   c[b[i]]--; n   hash_val -= ele[b[i]]; n   b[i]++; n   c[b[i]]++; n   hash_val += ele[b[i]]; n } n void remove(int i) { n   c[b[i]]--; n   hash_val -= ele[b[i]]; n   b[i]--; n   c[b[i]]++; n   hash_val += ele[b[i]]; n } n long long find(int ind) { n   if (ind == 11) return 1; n   pair<int, long long> mp = make_pair(ind, hash_val); n   if (vis.find(mp) != vis.end()) return vis[mp]; n   long long ans = 0; n   for (int i = 0; i < 16; i++) { n     if (b[i] >= t) continue; n     add(i); n     ans += find(ind + 1); n     remove(i); n   } n   vis[mp] = ans; n   return ans; n } n int main() { n   scanf( %d%d , &k, &t); n   ele[0] = 1; n   for (int i = 1; i < 11; i++) ele[i] = 17 * ele[i - 1]; n   if (t == 10) { n     printf( %x n , k); n     return 0; n   } n   c[0] = 16; n   hash_val = 16; n   int fir = 10; n   add(1); n   for (int i = 10; i > 0; i--) { n     long long x = 15ll * find(i + 1); n     if (k <= x) { n       fir = i; n       break; n     } else n       k -= x; n   } n   remove(1); n   for (int i = fir; i <= 10; i++) { n     for (int j = 0; j < 16; j++) { n       if (j == 0 and i == fir) continue; n       if (b[j] == t) continue; n       add(j); n       long long x = find(i + 1); n       if (k > x) { n         k -= x; n         remove(j); n       } else { n         a[i] = j; n         break; n       } n     } n   } n   int i = fir; n   while (i <= 10) { n     if (a[i] < 10) n       printf( %d , a[i]); n     else n       printf( %c ,  a  + a[i] - 10); n     i++; n   } n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (b == 0) { n     return a; n   } n   return gcd(b, a % b); n } n long long lcm(long long a, long long b) { return ((a * b) / gcd(a, b)); } n const long long maxn = 1e5; n long long n, k; n vector<long long> freq(maxn, 0); n vector<long long> arr(maxn); n vector<long long> ans(maxn, 0); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   cin >> n >> k; n   for (long long i = 0; i < n; i++) { n     cin >> arr[i]; n     freq[arr[i]]++; n   } n   long long counter = 0; n   for (long long i = 0; i < 1e4 + 1; i++) { n     if (freq[i] > 0) { n       for (long long j = i; j < 1e4 + 1; j++) { n         if (freq[j] == 0) { n           continue; n         } n         long long now = i ^ j; n         if (__builtin_popcount(now) == k) { n           if (i == j) { n             counter += ((freq[i] * (freq[i] - 1)) / 2); n             continue; n           } n           counter += ((freq[i] * freq[j])); n         } n       } n     } n   } n   cout << counter; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[100010], dp[100010][2], cum[100010]; n int calc(int at, int flag) { n   if (at == n + 1) return 0; n   int &ret = dp[at][flag]; n   if (~ret) return ret; n   ret = 0; n   if (flag) { n     ret = max(ret, calc(at + 1, 1) - a[at]); n     ret = max(ret, calc(at + 1, 0) - a[at]); n   } else { n     ret = max(ret, calc(at + 1, 0) + a[at]); n     ret = max(ret, -cum[n] + cum[at - 1]); n   } n   return ret; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d , a + i); n   for (int i = 1; i <= n; ++i) cum[i] = cum[i - 1] + a[i]; n   memset(dp, -1, sizeof dp); n   printf( %d , max(calc(1, 0), calc(1, 1))); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } n string toString(long long n) { n   stringstream second; n   second << n; n   return second.str(); n } n long long toNumber(string s) { n   stringstream second; n   long long n; n   second << s; n   second >> n; n   return n; n } n map<int, int> mapi; n int n, m, a, b, c; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     cin >> a >> b >> c; n     if (mapi[a] > 0) { n       mapi[b] = 1 ^ mapi[a]; n       if (mapi[b] == 0) mapi[b] = 2; n       mapi[c] = mapi[a] ^ mapi[b]; n     } else if (mapi[b] > 0) { n       mapi[c] = 1 ^ mapi[b]; n       if (mapi[c] == 0) mapi[c] = 2; n       mapi[a] = mapi[b] ^ mapi[c]; n     } else if (mapi[c] > 0) { n       mapi[a] = 1 ^ mapi[c]; n       if (mapi[a] == 0) mapi[a] = 2; n       mapi[b] = mapi[a] ^ mapi[c]; n     } else { n       mapi[a] = 1; n       mapi[b] = 2; n       mapi[c] = 3; n     } n   } n   for (int i = 1; i < n + 1; i++) { n     cout << mapi[i] <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:200000000 ) n using namespace std; n const double PI = acos(-1.0); n const double eps = 1e-9; n const unsigned long long MOD = 1e9 + 7; n const unsigned long long INF = 1e18 + 7; n const unsigned long long MAXN = 2e5 + 7; n unsigned long long n, m, A, B, C, ans; n unsigned long long d[MAXN], out[MAXN], link[MAXN], pos[MAXN], vi[MAXN]; n map<unsigned long long, unsigned long long> mp[MAXN]; n unordered_set<unsigned long long> st; n vector<pair<unsigned long long, unsigned long long> > f, G[MAXN]; n vector<unsigned long long> l[MAXN], r[MAXN], g[MAXN], low, high; n char vis[MAXN]; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   unsigned long long i, j, k, mid, u, v, w, res; n   cin >> n >> m >> A >> B >> C; n   for (i = 1; i <= m; i++) { n     cin >> u >> v; n     d[u]++; n     d[v]++; n     f.push_back({min(u, v), max(u, v)}); n   } n   sort(f.begin(), f.end(), n        [](pair<unsigned long long, unsigned long long> a, n           pair<unsigned long long, unsigned long long> b) { n          return a.first < b.first || n                 (a.first == b.first && a.second < b.second); n        }); n   for (auto x : f) { n     l[x.second].push_back(x.first); n     r[x.first].push_back(x.second); n   } n   res = 0; n   for (i = 1; i < n - 1; i++) { n     u = (i + 1) * i / 2 - i; n     v = (n - 1) * n / 2 - u - i; n     res += A * u * (n - 1 - i) + B * i * (i) * (n - 1 - i) + C * v * (i); n   } n   ans += res; n   res = 0; n   for (i = 0; i < m; i++) { n     pair<unsigned long long, unsigned long long> x = f[i]; n     u = x.first * (x.first + 1) / 2 - x.first; n     res += A * u + B * x.first * (x.first >= 1 ? x.first : 0ull) + n            C * x.second * (x.first >= 1 ? x.first : 0ull); n     u = (x.first + x.second) * (x.second - x.first + 1) / 2 - x.first - n         x.second; n     res += A * x.first * (x.second - x.first - 1) + B * u + n            C * x.second * (x.second - x.first - 1); n     u = (n - 1 + x.second) * (n - 1 - x.second + 1) / 2 - x.second; n     res += A * x.first * (n - 1 - x.second) + n            B * x.second * (n - 1 - x.second) + C * u; n   } n   ans -= res; n   res = 0; n   unsigned long long suml = 0, sumr = 0, cntl = 0, cntr = 0, len; n   for (i = 0; i < n; i++) { n     suml = sumr = cntl = cntr = 0; n     len = l[i].size(); n     if (len) { n       suml = l[i][0]; n       cntl = 1; n       for (j = 1; j < len; j++) { n         res += A * suml + B * l[i][j] * cntl + C * i * cntl; n         suml += l[i][j]; n         cntl++; n       } n     } n     len = r[i].size(); n     if (len) { n       sumr = r[i][0]; n       cntr = 1; n       for (j = 1; j < len; j++) { n         res += A * i * cntr + B * sumr + C * r[i][j] * cntr; n         sumr += r[i][j]; n         cntr++; n       } n     } n     res += (A * suml * cntr + B * i * cntl * cntr + C * sumr * cntl) * n            (cntr > 0 && cntl > 0); n   } n   ans += res; n   res = 0; n   for (auto x : f) { n     if (d[x.second] < d[x.first]) n       G[x.second].push_back({x.first, i}); n     else n       G[x.first].push_back({x.second, i}); n   } n   for (i = 0; i < m; i++) { n     u = f[i].first; n     v = f[i].second; n     for (auto vp : G[u]) pos[vp.first] = vp.second, vi[vp.first] = i + 1; n     for (auto vp : G[v]) { n       w = vp.first; n       if (vi[w] == i + 1) { n         unsigned long long x, y, z; n         x = min(u, min(v, w)); n         z = max(u, max(v, w)); n         y = u + v + w - x - z; n         res += A * x + B * y + C * z; n       } n     } n   } n   ans -= res; n   cout << ans << endl; n   return 0; n } n


n  n //================code===================// n  n //#define TLE n  n #ifdef TLE n #pragma GCC optimize( O3 ) n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n #endif n  n #include <bits/stdc++.h> n #include <unordered_map> n #include <unordered_set> n #include <random> n #include <ctime> n  n #define ci(t) cin>>t n #define co(t) cout<<t n #define LL long long n #define ld long double n #define fa(i,a,b) for(LL i=(a);i<(LL)(b);++i) n #define fd(i,a,b) for(LL i=(a);i>(LL)(b);--i) n #define setp pair<pair<int,int>,int> n #define setl pair<LL,LL> n  n #define M_PI 3.14159265358979323846 n #define micro 0.000001 n  n using namespace std; n  n #ifdef OHSOLUTION n #define ce(t) cerr<<t n #define AT cerr <<   n=================ANS================= n  n #define AE cerr <<   n===================================== n  n LL gcd(LL a, LL b) { return a % b ? gcd(b, a % b) : b; } n LL lcm(LL a, LL b) { return (a * b) / gcd(a, b); } n #else n #define AT n #define AE n #define ce(t) n #define __popcnt __builtin_popcount n #define gcd __gcd n #define lcm __lcm n #endif n  n  n pair <int, int> vu[9] = { {0,1},{1,0},{0,-1} ,{-1,0},{0,0},{1,1}, {-1,1} , {1,-1},{-1,-1} }; //RDLU EWSN n  n template<typename T, typename U> void ckmax(T& a, U b) { a = a < b ? b : a; } n template<typename T, typename U> void ckmin(T& a, U b) { a = a > b ? b : a; } n struct gcmp { bool operator()(LL a, LL b) { return a < b; } bool operator()(setl a, setl b) { return a.second < b.second; } }; n struct lcmp { bool operator()(LL a, LL b) { return a > b; } bool operator()(setl a, setl b) { return a.second > b.second; } }; n  n const int max_v = 2e5 + 7; n const int INF = 1e9 + 7; n const LL LNF = (LL)1e18 + 7ll; n const LL mod = 1e9 + 7; n  n int v, e; n vector <setl> adj[max_v]; n LL dist[max_v][57]; n bool vist[max_v][57]; n  n struct node n { n  LL first, second; n  LL cost; n }; n  n struct cmp n { n  bool operator()(const node& x, const node& b) n  { n   return x.second > b.second; n  } n }; n  n int main() n { n #ifdef OHSOLUTION n  freopen( input.txt ,  r , stdin); n #endif  n  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); n  ci(v >> e); n  n  fa(i, 0, e) n  { n   int x, y, cost; ci(x >> y >> cost), --x, --y; n   adj[x].push_back({ y,cost }); n   adj[y].push_back({ x,cost }); n  }  n  n  priority_queue<node, vector<node>, cmp> pq; n  fa(i, 0, v) fa(j, 0, 51) dist[i][j] = INF; n  dist[0][0] = 0; n  pq.push({ 0,0 ,0}); n  n  while (!pq.empty()) n  { n   node cur; n   do n   { n    cur = pq.top(); pq.pop(); n   } while (!pq.empty() && vist[cur.first][cur.cost]); n  n   if (vist[cur.first][cur.cost]) break; n   vist[cur.first][cur.cost] = 1; n  n   if (cur.cost) n   { n    for (auto x : adj[cur.first]) n    { n     if (dist[x.first][0] > dist[cur.first][cur.cost] + (cur.cost + x.second) * (cur.cost + x.second)) n     { n      dist[x.first][0] = dist[cur.first][cur.cost] + (cur.cost + x.second) * (cur.cost + x.second); n      pq.push({ x.first,dist[x.first][0],0 }); n     } n    } n   } n   else n   { n    for (auto x : adj[cur.first]) n    { n     if (dist[x.first][x.second] > dist[cur.first][cur.cost]) n     { n      dist[x.first][x.second] = dist[cur.first][cur.cost]; n      pq.push({ x.first,dist[x.first][x.second],x.second }); n     } n    } n   } n  } n  n  fa(i, 0, v) dist[i][0] == INF ? co(-1 <<    ) : co(dist[i][0] <<    ); n  n  n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n vector<string> v; n string s; n int n; n int main() { n   cin >> n >> s; n   v.push_back(s); n   for (int i = 0; i < n; i++) { n     string s1 = s; n     if (s[i] != 0) { n       int kl = 10 - (s[i] - 48); n       for (int j = 0; j < n; j++) { n         int zn = s[j] - 48 + kl; n         zn %= 10; n         s1[j] = zn + 48; n       } n     } n     string s2 =   ; n     for (int j = i; j < n; j++) s2 += s1[j]; n     for (int j = 0; j < i; j++) s2 += s1[j]; n     v.push_back(s2); n   } n   sort(v.begin(), v.end()); n   cout << v[0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct _IO { n   _IO() { n     ios::sync_with_stdio(0); n     cin.tie(0); n     cout.tie(0); n   } n } _io; n const long long maxn = 3e5 + 10, mod = 1e9 + 7; n template <typename... T> n long long madd(T... args) { n   long long res = 0; n   for (auto i : {args...}) { n     res = (res + ((i % mod + mod) % mod)) % mod; n   } n   return res % mod; n } n template <typename... T> n long long mmul(T... args) { n   long long res = 1; n   for (auto i : {args...}) { n     res = (res * ((i % mod + mod) % mod)) % mod; n   } n   return res % mod; n } n long long qpow(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b & 1) res *= a; n     a *= a; n     b >>= 1; n   } n   return res; n } n long long mpow(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b & 1) res = (res * a) % mod; n     a = (a * a) % mod; n     b >>= 1; n   } n   return res % mod; n } n long long _inv(long long x) { return mpow(x, mod - 2); } n long long dsufa[maxn], fa[maxn], cnt[maxn]; n vector<pair<long long, long long> > query; n vector<long long> edge[maxn]; n deque<long long> res; n long long findfa(long long x) { n   return x == dsufa[x] ? x : (dsufa[x] = findfa(dsufa[x])); n } n bool mer(long long x, long long y) { n   x = findfa(x); n   y = findfa(y); n   if (x == y) return 0; n   dsufa[x] = y; n   return 1; n } n void dfs(long long u, long long p) { n   for (auto v : edge[u]) { n     if (v == p) continue; n     fa[v] = u; n     dfs(v, u); n   } n } n void path(long long x, long long y) { n   deque<long long> a(1, x), b(1, y); n   res.clear(); n   while (fa[x] != x) { n     x = fa[x]; n     a.push_front(x); n   } n   while (fa[y] != y) { n     y = fa[y]; n     b.push_front(y); n   } n   long long len = min(a.size(), b.size()); n   long long idx = 0; n   for (; idx < len && a[idx] == b[idx]; ++idx) n     ; n   res.push_back(a[idx - 1]); n   for (long long i = idx; i < b.size(); ++i) res.push_back(b[i]); n   for (long long i = idx; i < a.size(); ++i) res.push_front(a[i]); n } n void solve() { n   long long n, m; n   cin >> n >> m; n   for (long long i = 1; i <= n; ++i) dsufa[i] = i; n   for (long long i = 1; i <= m; ++i) { n     long long x, y; n     cin >> x >> y; n     if (mer(x, y)) { n       edge[x].push_back(y); n       edge[y].push_back(x); n     } n   } n   dfs(1, 1); n   long long q; n   cin >> q; n   for (long long i = 1; i <= q; ++i) { n     long long x, y; n     cin >> x >> y; n     query.push_back({x, y}); n     ++cnt[x]; n     ++cnt[y]; n   } n   bool ok = 1; n   long long oddcnt = 0; n   for (long long i = 1; i <= n; ++i) { n     if (cnt[i] & 1) { n       ok = 0; n       ++oddcnt; n     } n   } n   if (!ok) { n     cout <<  NO n ; n     cout << oddcnt / 2 <<   n ; n   } else { n     cout <<  YES n ; n     for (auto vec : query) { n       long long x = vec.first, y = vec.second; n       path(x, y); n       cout << res.size() <<   n ; n       for (long long i = 0; i < res.size(); ++i) n         cout << res[i] <<    n [i == (long long)res.size() - 1]; n     } n   } n } n int main() { n   long long T = 1; n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n int a[2010]; n bool vis[2010]; n long long sum; n vector<int> edge[2010]; n long long DFS(int u, int d, int fa, int root) { n   long long ret = 1; n   for (int i = 0; i < edge[u].size(); i++) { n     int v = edge[u][i]; n     if (v != fa && a[root] >= a[v] && a[root] - a[v] <= d && n         !(a[root] == a[v] && v > root)) { n       ret = ret * (DFS(v, d, u, root) + 1) % mod; n     } n   } n   return ret; n } n int main() { n   int n, d; n   scanf( %d %d , &d, &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i < n; i++) { n     int a, b; n     scanf( %d %d , &a, &b); n     edge[a].push_back(b); n     edge[b].push_back(a); n   } n   for (int i = 1; i <= n; i++) { n     sum = (sum + DFS(i, d, -1, i)) % mod; n     vis[i] = true; n   } n   printf( %lld n , sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXK = 21; n long long dp[(1 << MAXK)]; n int n, m; n string s; n long long d[MAXK][MAXK]; n long long solve(int mask) { n   if (mask == ((1 << m) - 1)) return 0ll; n   long long &ret = dp[mask]; n   if (ret != -1) return ret; n   ret = 1e10; n   long long cost = __builtin_popcount(mask); n   for (int i = 0; i < m; ++i) { n     if ((mask & (1 << i))) continue; n     long long now = solve(mask | (1 << i)); n     for (int j = 0; j < m; ++j) { n       if (i == j) continue; n       if ((mask & (1 << j))) n         now += cost * d[i][j]; n       else n         now -= cost * d[i][j]; n     } n     ret = min(ret, now); n   } n   return ret; n } n int main() { n   memset(dp, -1, sizeof(dp)); n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n >> m; n   cin >> s; n   for (int i = 0; i < n - 1; ++i) { n     int a = s[i] -  a ; n     int b = s[i + 1] -  a ; n     d[a][b]++; n     if (a != b) d[b][a]++; n   } n   return cout << solve(0) <<   n , 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char *argv[]) { n   long long n, k; n   cin >> n >> k; n   long long len = 2 * n; n   long long temp = k * 2 / len; n   long long ans = k * 2 % len == 0 ? temp : temp + 1; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T min(T a, T b, T c) { n   return min(a, min(b, c)); n } n template <class T> n T max(T a, T b, T c) { n   return max(a, max(b, c)); n } n template <class T> n void read(vector<T> &v) { n   T x; n   cin >> x; n   v.push_back(x); n } n int n, a, b, x[1000005]; n long long ans = (long long)1000000007 * 1000000007, f[1000005][3]; n bool isPrime(int n) { n   if (n < 2) return false; n   int lim = sqrt(n); n   for (int i = (2); i <= (lim); ++i) { n     if (n % i == 0) return false; n   } n   return true; n } n void Solve(int p) { n   if (!isPrime(p)) return; n   for (int i = (0); i <= (n); ++i) { n     for (int k = (0); k <= (2); ++k) n       f[i][k] = (long long)1000000007 * 1000000007; n   } n   f[0][0] = 0; n   for (int i = (1); i <= (n); ++i) { n     if (x[i] % p == 0) { n       f[i][0] = f[i - 1][0]; n       f[i][2] = min(f[i - 1][1], f[i - 1][2]); n     } else if ((x[i] - 1) % p == 0 || (x[i] + 1) % p == 0) { n       f[i][0] = f[i - 1][0] + b; n       f[i][2] = min(f[i - 1][1], f[i - 1][2]) + b; n     } n     f[i][1] = min(f[i - 1][0], f[i - 1][1]) + a; n   } n   for (int k = (0); k <= (2); ++k) ans = min(ans, f[n][k]); n } n void Try(int p) { n   int lim = sqrt(p); n   for (int i = (1); i <= (lim); ++i) { n     if (p % i == 0) { n       Solve(i); n       Solve(p / i); n     } n   } n } n int main() { n   scanf( %d%d%d , &n, &a, &b); n   for (int i = (1); i <= (n); ++i) scanf( %d , &x[i]); n   for (int d = (-1); d <= (1); ++d) { n     Try(x[1] + d); n     Try(x[n] + d); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n long long a[maxn]; n pair<long long, long long> st[maxn]; n long long top, n, m, k, l, r, rec, rec2; n long long ans; n int main() { n   cin >> n >> k >> m; n   for (long long i = 1; i <= n; i++) cin >> a[i]; n   for (long long i = 1; i <= n; i++) { n     if (!top || st[top].first != a[i]) n       st[++top] = make_pair(a[i], 1); n     else n       st[top].second = (st[top].second + 1) % k; n     if (st[top].second == 0) top--; n   } n   for (long long i = 1; i <= top; i++) { n     rec += st[i].second; n   } n   l = 1, r = top; n   while (l < r && st[l].first == st[r].first && n          (st[l].second + st[r].second) % k == 0) { n     rec2 += st[l].second + st[r].second; n     l++; n     r--; n   } n   if (l == r) { n     if (st[l].second * m % k == 0) n       ans += m * rec - rec2 * (m) - (st[l].second * m / k * k); n     else n       ans += m * rec - rec2 * (m - 1) - (st[l].second * m / k * k); n   } else { n     if (st[l].first == st[r].first) n       rec2 += (st[l].second + st[r].second) / k * k; n     ans = m * rec - rec2 * (m - 1); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> t; n void set_val(int p, int x) { n   t[p + int(t.size()) / 2] = x; n   for (p = (p + int(t.size()) / 2) / 2; p > 0; p /= 2) { n     t[p] = max(t[2 * p], t[2 * p + 1]); n   } n } n int leftmost(int left, int y) { n   if (left >= int(t.size()) / 2) return -1; n   int c = left + int(t.size()) / 2; n   if (t[c] > y) return left; n   for (; c > 1 && !(c % 2 == 0 && t[c + 1] > y);) c /= 2; n   if (c == 1) return -1; n   ++c; n   for (; c < int(t.size()) / 2;) { n     if (t[2 * c] > y) n       c *= 2; n     else n       c = 2 * c + 1; n   } n   return c - int(t.size()) / 2; n } n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   vector<string> req(n); n   vector<int> x(n), y(n); n   for (int i = 0; i < n; ++i) { n     cin >> req[i] >> x[i] >> y[i]; n   } n   vector<int> di = x; n   sort(di.begin(), di.end()); n   di.resize(unique(di.begin(), di.end()) - di.begin()); n   map<int, int> id; n   vector<set<int> > c(di.size()); n   for (int i = 0; i < int(di.size()); ++i) { n     id[di[i]] = i; n     c[i].insert(-1); n   } n   int k = 1; n   for (; k < int(di.size());) k *= 2; n   t = vector<int>(2 * k, -1); n   for (int i = 0; i < n; ++i) { n     if (req[i] ==  add ) { n       c[id[x[i]]].insert(y[i]); n       set<int>::iterator last = c[id[x[i]]].end(); n       --last; n       set_val(id[x[i]], *last); n     } else if (req[i] ==  remove ) { n       c[id[x[i]]].erase(y[i]); n       set<int>::iterator last = c[id[x[i]]].end(); n       --last; n       set_val(id[x[i]], *last); n     } else if (req[i] ==  find ) { n       int a = leftmost(id[x[i]] + 1, y[i]); n       if (a == -1) n         cout << -1 << endl; n       else n         cout << di[a] <<     << *c[a].upper_bound(y[i]) << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n const int INF = 1000000001; n const long long LINF = 1000000000000000001LL; n int len[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; n string days[] = { monday ,  tuesday ,   wednesday ,  thursday , n                   friday ,  saturday ,  sunday }; n int dayId(string s) { return find(days, days + 7, s) - days; } n int main() { n   ios_base::sync_with_stdio(0); n   string a, b; n   cin >> a >> b; n   int aid = dayId(a), bid = dayId(b); n   for (int i = (0); i <= (10); ++i) { n     if ((aid + len[i]) % 7 == bid) { n       cout <<  YES ; n       return 0; n     } n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, a[200], m; n int main() { n   cin >> n >> m; n   k = n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   sort(a + 1, a + 1 + n); n   while (m > 0) m -= a[n--]; n   if (n < 0) n = 0; n   cout << k - n << 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   vector<string> words(4 * n); n   string S; n   int aabb = 0, abab = 0, abba = 0, aaaa = 0, NO = 0; n   for (int i = (0); i < (4 * n); ++i) { n     cin >> S; n     int size = S.size(); n     int Kth = 0; n     string newS =   ; n     bool canRhyme = false; n     for (int j = size - 1; j >= 0; --j) { n       if (S[j] ==  a  || S[j] ==  e  || S[j] ==  i  || S[j] ==  u  || n           S[j] ==  o ) { n         ++Kth; n       } n       newS = S[j] + newS; n       if (Kth == k) { n         canRhyme = true; n         break; n       } n     } n     if (canRhyme) n       words[i] = newS; n     else { n       stringstream out; n       out << i; n       words[i] = out.str(); n     } n   } n   for (int i = 0; i < n; ++i) { n     if (words[4 * i].compare(words[4 * i + 1]) == 0 && n         words[4 * i + 2].compare(words[4 * i + 3]) == 0) { n       if (words[4 * i].compare(words[4 * i + 2]) == 0) { n         ++aaaa; n         ++aabb; n         ++abab; n         ++abba; n       } else n         ++aabb; n     } else if (words[4 * i].compare(words[4 * i + 2]) == 0 && n                words[4 * i + 1].compare(words[4 * i + 3]) == 0) n       ++abab; n     else if (words[4 * i].compare(words[4 * i + 3]) == 0 && n              words[4 * i + 1].compare(words[4 * i + 2]) == 0) n       ++abba; n     else n       ++NO; n   } n   if (NO) n     cout <<  NO  << endl; n   else { n     if (aaaa == n) n       cout <<  aaaa  << endl; n     else if (aabb == n) n       cout <<  aabb  << endl; n     else if (abab == n) n       cout <<  abab  << endl; n     else if (abba == n) n       cout <<  abba  << endl; n     else n       cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MN = 1e6 + 44; n const int mod = 1e9 + 9, b = 8888; n long long rcshnt[MN]; n int n; n char a[MN]; n bool check(int len) { n   ; n   long long curr = 0; n   for (int i = 0; i < len; ++i) curr = (curr * b + a[i]) % mod; n   long long pre = curr; n   ; n   for (int i = len; i < n - 1; ++i) { n     ; n     ; n     curr = ((curr - a[i - len] * rcshnt[len - 1]) % mod * b + a[i]) % mod; n     ; n     if ((pre - curr) % mod == 0) { n       ; n       return true; n     } n   }; n   return false; n } n int main() { n   scanf( %s , a); n   n = strlen(a); n   rcshnt[0] = 1; n   for (int i = 1; i < MN; ++i) { n     rcshnt[i] = rcshnt[i - 1] * b % mod; n   } n   for (int i = 0; i < 10; ++i) n     ; n   vector<int> pre; n   long long ph = 0, sh = 0; n   for (int i = 0; i < n - 1; ++i) { n     ph = (ph * b + a[i]) % mod; n     sh = (sh + a[n - i - 1] * rcshnt[i]) % mod; n     if (ph == sh) { n       pre.push_back(i + 1); n       ; n     } n   } n   int low = 1, high = n - 1, ans = 0; n   while (low <= high) { n     int med = (low + high) / 2; n     if (check(med)) { n       ans = med; n       low = med + 1; n     } else n       high = med - 1; n   } n   int res = -1; n   for (auto c : pre) { n     if (c <= ans) res = max(res, c); n   } n   if (res <= 0) { n     printf( Just a legend n ); n   } else { n     a[res] = 0; n     printf( %s n , a); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long MOD = 1e9 + 7; n long long add(long long a, long long b) { n   a += b; n   if (a >= MOD) a -= MOD; n   if (a < 0) a += MOD; n   return a; n } n long long mul(long long a, long long b) { return a * 1ll * b % MOD; } n void solve() { n   long long n; n   string s; n   cin >> n >> s; n   string ans = string(n,  0 ); n   long long bal = 0; n   for (long long i = 0; i < n; i++) { n     if (s[i] ==  ) ) bal--; n     ans[i] = char( 0  + (bal & 1)); n     if (s[i] ==  ( ) bal++; n   } n   cout << ans << endl; n } n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve(); n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   solve(); n   return 0; n } n void sd() { n   int n; n   cin >> n; n   n = 2 * n; n   vector<vector<pair<int, long long>>> adj(n + 1); n   vector<int> cnt(n + 1, 1); n   int i; n   for (i = 0; i < n - 1; i++) { n     int u, v, t; n     cin >> u >> v >> t; n     adj[u].emplace_back(v, t); n     adj[v].emplace_back(u, t); n   } n   vector<int> parent(n + 1, -1), topo(1, 1); n   vector<long long> cost(n + 1, 0); n   parent[1] = 1; n   stack<int> st; n   st.push(1); n   while (!st.empty()) { n     int u = st.top(); n     st.pop(); n     for (auto [v, c] : adj[u]) { n       if (parent[v] == -1) { n         parent[v] = u; n         cost[v] = c; n         topo.push_back(v); n         st.push(v); n       } n     } n   } n   reverse(topo.begin(), topo.end()); n   long long G, B; n   G = B = 0; n   for (int u : topo) { n     cnt[parent[u]] += cnt[u]; n     B += cost[u] * min(cnt[u], n - cnt[u]); n     G += cost[u] * (cnt[u] % 2 == 1); n   } n   cout << G <<     << B << endl; n } n void solve() { n   int t; n   cin >> t; n   while (t--) { n     sd(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int n = 0; n   char c; n   for (c = getchar(); c <  0  || c >  9 ; c = getchar()) n     ; n   for (; c >=  0  && c <=  9 ; c = getchar()) n = n * 10 + c - 48; n   return n; n } n const int maxn = 1e5 + 5, N = 1 << 18, mo = 998244353; n int i, j, n, fa[maxn], size[maxn], ni[maxn], son[maxn]; n long long x; n vector<int> g[maxn], F[maxn], s1[maxn << 2], s2[maxn << 2], ans[maxn]; n int t1[N], t2[N], len, f[N], w[N]; n int qs(int x, int y) { n   int s = 1; n   for (; y; y >>= 1, x = (long long)x * x % mo) n     if (y & 1) s = (long long)s * x % mo; n   return s; n } n void mod(int &x) { x -= x >= mo ? mo : 0; } n namespace Poly { n void pre(int len) { n   int i, m = len >> 1; n   for (i = 1; i <= len - 1; i++) f[i] = f[i >> 1] >> 1 | (i & 1 ? m : 0); n } n void dft(int *a, bool sig) { n   int i; n   for (i = 0; i <= len - 1; i++) n     if (f[i] < i) swap(a[f[i]], a[i]); n   for (int i = 1, d = N >> 1; i < len; i <<= 1, d >>= 1) n     for (int j = 0; j < len; j += i << 1) n       for (int k = 0, o = 0; k < i; k++, o += d) { n         int u = a[j + k], v = (long long)a[i + j + k] * w[o] % mo; n         a[j + k] = u + v >= mo ? u + v - mo : u + v; n         a[i + j + k] = u - v < 0 ? u - v + mo : u - v; n       } n   if (sig) { n     reverse(a + 1, a + len); n     int re = qs(len, mo - 2); n     for (i = 0; i <= len - 1; i++) a[i] = (long long)a[i] * re % mo; n   } n } n void mul(vector<int> &a, vector<int> &b, vector<int> &c) { n   len = 1; n   for (; len < a.size() + b.size();) len <<= 1; n   for (int i = 0; i < a.size(); i++) t1[i] = a[i]; n   for (int i = a.size(); i < len; i++) t1[i] = 0; n   for (int i = 0; i < b.size(); i++) t2[i] = b[i]; n   for (int i = b.size(); i < len; i++) t2[i] = 0; n   pre(len); n   dft(t1, 0), dft(t2, 0); n   for (int i = 0; i < len; i++) t1[i] = (long long)t1[i] * t2[i] % mo; n   dft(t1, 1); n   c.resize(a.size() + b.size() - 1); n   for (int i = 0; i < c.size(); i++) c[i] = t1[i]; n } n void add(vector<int> &a, vector<int> &b, vector<int> &c) { n   c.resize((a.size() > b.size() ? a.size() : b.size())); n   for (int i = 0; i < c.size(); i++) n     mod(c[i] = (i < a.size() ? a[i] : 0) + (i < b.size() ? b[i] : 0)); n } n void right(vector<int> &a, vector<int> &b) { n   b.resize(a.size() + 1); n   for (int i = 1; i < a.size(); i++) b[i + 1] = a[i]; n   b[0] = 0, b[1] = 1; n } n }  // namespace Poly n void dfs(int x, int y) { n   size[x] = 1, fa[x] = y; n   for (int i = 0; i < g[x].size(); i++) { n     int go = g[x][i]; n     if (go == y) continue; n     dfs(go, x); n     size[x] += size[go]; n     if (size[go] > size[son[x]]) son[x] = go; n   } n } n void solve(int k, int l, int r) { n   static vector<int> t; n   if (l == r) { n     s1[k] = s2[k] = F[l]; n     return; n   } n   int m = ((l + r) >> 1), lk = k << 1, rk = lk + 1; n   solve(lk, l, m); n   solve(rk, m + 1, r); n   Poly::mul(s1[lk], s1[rk], s1[k]); n   Poly::mul(s1[lk], s2[rk], t); n   Poly::add(s2[lk], t, s2[k]); n } n void calc(int x) { n   int tot = 0; n   for (int i = x; i; i = son[i]) F[++tot] = ans[i]; n   solve(1, 1, tot); n   ans[x] = s2[1]; n } n void Dfs(int x, int y) { n   if (son[x]) Dfs(son[x], y); n   for (int i = 0; i < g[x].size(); i++) { n     int go = g[x][i]; n     if (go == fa[x] || go == son[x]) continue; n     Dfs(go, go); n     Poly::right(ans[go], ans[x]); n   } n   if (!ans[x].size()) ans[x].resize(2), ans[x][0] = 0, ans[x][1] = 1; n   if (x == y) calc(x); n } n int main() { n   w[0] = 1, w[1] = qs(3, (mo - 1) / N); n   for (i = 2; i <= N - 1; i++) w[i] = (long long)w[i - 1] * w[1] % mo; n   n = read(), scanf( %lld , &x); n   for (i = 1; i <= n - 1; i++) { n     int a = read(), b = read(); n     g[a].push_back(b); n     g[b].push_back(a); n   } n   dfs(1, 0); n   Dfs(1, 1); n   long long C = 1, Ans = 0; n   x %= mo; n   ni[1] = ni[0] = 1; n   for (i = 2; i <= n; i++) ni[i] = (long long)(mo - mo / i) * ni[mo % i] % mo; n   for (i = 1; i <= n; i++) { n     Ans += C * ans[1][i] % mo; n     C = C * (i + x) % mo * ni[i] % mo; n   } n   printf( %I64d n , Ans % mo); n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000009]; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int n; n     scanf( %d , &n); n     int v = 0; n     int min1 = 0x3f3f3f3f; n     for (int i = 1; i <= n; ++i) { n       scanf( %d , &a[i]); n     } n     for (int i = n; i >= 1; --i) { n       min1 = min(a[i], min1); n       if (min1 < a[i]) { n         v++; n       } n     } n     printf( %d n , v); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[100005], ans[100005]; n struct node { n   int x, y, t; n } a[100005]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   int r, n; n   cin >> r >> n; n   a[0].x = a[0].y = 1; n   a[0].t = 0; n   for (int i = 1; i <= n; i++) { n     cin >> a[i].t >> a[i].x >> a[i].y; n     dp[i] = -n; n     for (int j = i - 1; j >= 0; j--) { n       if (a[i].t - a[j].t >= 2 * (r - 1)) { n         dp[i] = max(dp[i], ans[j] + 1); n         break; n       } n       if (a[i].t - a[j].t >= abs(a[i].x - a[j].x) + abs(a[i].y - a[j].y)) { n         dp[i] = max(dp[i], dp[j] + 1); n       } n     } n     ans[i] = max(dp[i], ans[i - 1]); n   } n   cout << ans[n] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int a; n   cin >> a; n   int n, s, t; n   while (a--) { n     cin >> n >> s >> t; n     cout << max(n - s + 1, n - t + 1) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int query(long long a, long long b) { n   cout << a <<     << b << endl << flush; n   int ans; n   cin >> ans; n   return ans; n } n int main() { n   long long n; n   cin >> n; n   long long a = 0, b = 0, sa = 1, sb = 1; n   int ans = query(a + sa, b + sb); n   while (ans) { n     switch (ans) { n       case 1: n         a += sa; n         sa = min(sa * 2, n - a); n         break; n       case 2: n         b += sb; n         sb = min(sb * 2, n - b); n         break; n       case 3: n         sa = max(sa / 2, 1ll); n         sb = max(sb / 2, 1ll); n         break; n       default: n         return 0; n     } n     ans = query(a + sa, b + sb); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 5; n int a[maxn], cnt[maxn]; n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   int l = 1, r = 1; n   int ans = 0; n   cnt[a[1]]++; n   while (l <= n) { n     if (((r - l + 1) - cnt[a[l]]) <= k) { n       ans = max(ans, cnt[a[l]]); n       if (r < n) { n         r++; n         cnt[a[r]]++; n       } else { n         cnt[a[l]]--; n         l++; n       } n     } else { n       cnt[a[l]]--; n       l++; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #define ll long long n #define MOD (ll)1000000007 n #define pii pair<int,int> n #define pll pair<ll,ll> n #define UNIQUE(v) sort(v.begin(),v.end());v.erase(unique(v.begin(),v.end()),v.end()); n ll GCD(ll a,ll b){return b?GCD(b,a%b):a;} n ll power(ll a,ll b){ll ret=1;while(b){if(b%2)ret=ret*a%MOD;a=a*a%MOD;b/=2;}return ret;} n  n ll a[200001]; n  n int main() n { n     int tc; n     scanf( %d ,&tc); n     while(tc--) n     { n         int n; n         scanf( %d ,&n); n         for(int i=1;i<=n;i++) n             scanf( %lld ,&a[i]); n         vector<ll> v; n         ll temp = 0; n         a[0] = a[n+1] = 1e18; n         for(int i=2;i<n;i++) n         { n             temp = max(temp, abs(a[i-1]-a[i]) + abs(a[i]-a[i+1])-abs(a[i+1]-a[i-1])); n         } n         temp = max(temp, abs(a[1]-a[2])); n         temp = max(temp, abs(a[n]-a[n-1])); n         ll ans = 0; n         for(int i=2;i<=n;i++) n             ans+=abs(a[i]-a[i-1]); n         printf( %lld n ,ans-temp); n     } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n bool has[20]; n int dp[1010][12][12]; n int use[1010][12][12]; n int main() { n   for (int i = 1; i <= 10; i++) { n     char c; n     cin >> c; n     if (c ==  1 ) has[i] = true; n   } n   int m; n   cin >> m; n   if (m == 1) { n     for (int i = 1; i <= 10; i++) n       if (has[i]) { n         cout <<  YES  << endl << i << endl; n         return 0; n       } n     cout <<  NO  << endl; n     return 0; n   } n   for (int i = 1; i <= 10; i++) n     for (int j = 1; j <= 10; j++) { n       use[m][i][j] = -1; n       dp[m][i][j] = true; n     } n   for (int i = m - 1; i >= 1; i--) n     for (int dif = 1; dif <= 10; dif++) n       for (int ls = 1; ls <= 10; ls++) n         for (int j = dif + 1; j <= 10; j++) n           if (j != ls && has[j]) { n             if (dp[i + 1][j - dif][j]) { n               dp[i][dif][ls] = true; n               use[i][dif][ls] = j; n               break; n             } n           } n   for (int i = 1; i <= 10; i++) n     if (has[i]) n       if (dp[1][i][i]) { n         cout <<  YES  << endl; n         cout << i <<    ; n         int k = use[1][i][i]; n         int diff = i; n         int lev = 1; n         while (k != -1) { n           cout << k <<    ; n           lev++; n           diff = k - diff; n           k = use[lev][diff][k]; n         } n         return 0; n       } n   cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < int(n); i++) cin >> a[i]; n   long long diff = 0; n   int m = a[0]; n   int ans = 0; n   for (int i = 0; i < n; i++) { n     if (a[i] >= m) { n       m = a[i]; n     } else { n       diff = (m - a[i]); n       int d = floor(log2(diff)) + 1; n       ans = max(d, ans); n     } n   } n   cout << ans << endl; n } n int main() { n   cin.tie(0); n   ios_base::sync_with_stdio(false); n   int t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long fa[400005], iv[400005]; n pair<long long, long long> p[2005]; n long long dp[2005]; n long long pw(long long bs, long long x) { n   long long ans = 1; n   while (x) { n     if (x & 1) ans = ans * bs % 1000000007; n     bs = bs * bs % 1000000007; n     x /= 2; n   } n   return ans % 1000000007; n } n void init() { n   fa[0] = iv[0] = 1; n   for (int i = 1; i <= 400000; i++) { n     fa[i] = fa[i - 1] * i % 1000000007; n     iv[i] = pw(fa[i], 1000000007 - 2) % 1000000007; n   } n } n long long cal(long long n, long long m) { n   if (m > n) return 0; n   long long ans = fa[n] % 1000000007; n   ans = (ans * iv[m] % 1000000007) * iv[n - m] % 1000000007; n   return ans; n } n long long lu(long long n, long long m) { n   if (m == 0) return 1; n   return cal(n % 1000000007, m % 1000000007) * n          lu(n / 1000000007, m / 1000000007) % 1000000007; n } n int main() { n   init(); n   long long n, m, k; n   cin >> n >> m >> k; n   n--; n   m--; n   for (int i = 0; i < k; i++) { n     cin >> p[i].first >> p[i].second; n     p[i].first--; n     p[i].second--; n   } n   sort(p, p + k); n   p[k].first = n; n   p[k].second = m; n   for (int i = 0; i <= k; i++) { n     long long sum = 0; n     for (int j = 0; j < i; j++) { n       long long x = p[i].first - p[j].first, y = p[i].second - p[j].second; n       if (x < 0 || y < 0) continue; n       sum = (sum + lu(x + y, x) * dp[j] % 1000000007) % 1000000007; n     } n     dp[i] = (cal(p[i].first + p[i].second, p[i].first) - sum + 1000000007) % n             1000000007; n   } n   cout << dp[k] % 1000000007 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXPOW = 20; n const int MAXA = 1048577; n const int MAXN = 100009; n int n; n int k; n vector<int> a; n unordered_set<int> visited[MAXN]; n bool used[MAXA]; n void gen() { n   freopen( input.txt ,  w , stdout); n   n = 100000; n   printf( %d n , n); n   for (int i = 0; i < n; ++i) printf( %d  , rand() % 1000001); n   exit(0); n } n int stupid() { n   used[0] = true; n   for (int i = 0; i < n; ++i) { n     int x = 0; n     for (int j = i; j < n; ++j) { n       x |= a[j]; n       if (!used[x]) { n         cerr <<  i  n              <<  :   << i << endl; n         ; n         cerr <<  j  n              <<  :   << j << endl; n         ; n         cerr <<  x  n              <<  :   << x << endl; n         ; n       } n       used[x] = true; n     } n   } n   int cnt = 0; n   for (int i = 0; i < MAXA; ++i) n     if (used[i]) cnt++; n   return cnt; n } n int main() { n   scanf( %d , &n); n   a.resize(n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &a[i]); n   } n   for (int i = 0; i < n; ++i) { n     int x = 0; n     for (int j = i; j < n; ++j) { n       x |= a[j]; n       if (visited[j].find(x) != visited[j].end()) break; n       visited[j].insert(x); n       used[x] = true; n     } n   } n   int cnt = 0; n   for (int i = 0; i < MAXA; ++i) n     if (used[i]) cnt++; n   printf( %d n , cnt); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n int Mo[N], a[N]; n inline int gi() { n   int x = 0, o = 1; n   char ch = getchar(); n   while (ch !=  -  && (ch <  0  || ch >  9 )) ch = getchar(); n   if (ch ==  - ) ch = getchar(), o = -1; n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   return x * o; n } n inline int phi(int x) { n   int ret = x, y = x; n   for (int i = 2; i * i <= y; i++) n     if (x % i == 0) { n       while (x % i == 0) x /= i; n       ret = ret / i * (i - 1); n     } n   if (x > 1) ret = ret / x * (x - 1); n   return ret; n } n inline void calc(int &x, int y, int M) { n   long long c = 1LL * x * y; n   x = (c < M ? c : c % M + M); n } n inline int Pow(int x, int y, int M) { n   int ret = 1; n   while (y) { n     if (y & 1) calc(ret, x, M); n     calc(x, x, M), y >>= 1; n   } n   return ret; n } n inline int solve(int l, int r, int t) { n   if (Mo[t] == 1) return 1; n   if (l == r) { n     if (a[l] < Mo[t]) return a[l]; n     return a[l] % Mo[t] + Mo[t]; n   } n   return Pow(a[l], solve(l + 1, r, t + 1), Mo[t]); n } n int main() { n   int n, m, q; n   cin >> n >> m; n   Mo[1] = m; n   for (int i = 1; Mo[i] ^ 1;) Mo[++i] = m = phi(m); n   for (int i = 1; i <= n; i++) a[i] = gi(); n   cin >> q; n   while (q--) { n     int l = gi(), r = gi(); n     printf( %d n , solve(l, r, 1) % Mo[1]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     string s; n     cin >> s; n     long long i; n     map<char, long long> mp; n     for (i = 0; i < n; i++) { n       mp[s[i]]++; n     } n     if (n == 2) { n       if (s[0] >= s[1]) { n         cout <<  NO  << endl; n       } else { n         cout <<  YES  << endl; n         cout << 2 << endl; n         cout << s[0] <<     << s.substr(1, n) << endl; n       } n     } else { n       cout <<  YES  << endl; n       cout << 2 << endl; n       cout << s[0] <<     << s.substr(1, n) << endl; n     } n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 200005; n const int inf = 1000000007; n int main() { n   int a, b, c; n   int x, y, z; n   cin >> a >> b >> c; n   cin >> x >> y >> z; n   int da = a - x; n   int db = b - y; n   int dc = c - z; n   int can = max(0, da / 2) + max(0, db / 2) + max(0, dc / 2); n   int needed = min(0, da) + min(0, db) + min(0, dc); n   cout << (can + needed >= 0 ?  Yes  :  No ) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n long long s[211111], mx[211111]; n int a[211111], bg[211111]; n long long hf, ans; n int bhf, aa, bb; n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n     s[i] = s[i - 1] + a[i]; n   } n   for (int i = k; i <= n; ++i) { n     if (s[i] - s[i - k] > mx[i - 1]) { n       mx[i] = s[i] - s[i - k]; n       bg[i] = i - k + 1; n     } else n       mx[i] = mx[i - 1], bg[i] = bg[i - 1]; n   } n   hf = 0; n   for (int i = n - k + 1; i >= 1; --i) { n     if (mx[i] + hf >= ans) { n       long long tmp = ans; n       ans = mx[i] + hf; n       if (tmp < ans) aa = bg[i], bb = bhf; n       if (bg[i] < aa || (bg[i] == aa && bhf < bb)) { n         aa = bg[i], bb = bhf; n       } n     } n     if (s[i + k - 1] - s[i - 1] >= hf) { n       hf = s[i + k - 1] - s[i - 1]; n       bhf = i; n     } n   } n   cout << aa <<     << bb << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, a[1000003]; n pair<long long, long long> b[1000003]; n long long calc1() { n   for (int i = 0; i < n; i++) b[i] = make_pair(-a[i], i); n   sort(b, b + n); n   set<signed> s1; n   s1.insert(-1); n   s1.insert(n); n   long long ret = 0; n   for (int i = 0; i < n; i++) { n     set<int>::iterator tmpr = s1.lower_bound(b[i].second), tmpl = prev(tmpr); n     ret -= (b[i].second - *tmpl) * (*tmpr - b[i].second) * b[i].first; n     s1.insert(b[i].second); n   } n   return ret; n } n long long calc2() { n   for (int i = 0; i < n; i++) b[i] = make_pair(-b[i].first, b[i].second); n   for (int i = 0; i < n / 2; i++) swap(b[i], b[n - i - 1]); n   set<signed> s1; n   s1.insert(-1); n   s1.insert(n); n   long long ret = 0; n   for (int i = 0; i < n; i++) { n     set<int>::iterator tmpr = s1.lower_bound(b[i].second), tmpl = prev(tmpr); n     ret += (b[i].second - *tmpl) * (*tmpr - b[i].second) * b[i].first; n     s1.insert(b[i].second); n   } n   return ret; n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) scanf( %lld , a + i); n   cout << calc1() - calc2(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n int i, j, k, n, m; n pair<int, int> p[N]; n int a[N]; n int c[N]; n int main() { n   cin >> n; n   for (i = 0; i < n; ++i) { n     cin >> p[i].first; n     p[i].second = i; n   } n   sort(p, p + n); n   k = 0; n   for (i = 0; i < n; ++i) { n     c[p[i].first]++; n     k = max(k, p[i].first); n   } n   for (i = 1; i <= k; ++i) n     if (c[i] == 0 || (i > 1 && c[i] > c[i - 1])) { n       cout << -1 << endl; n       return 0; n     } n   m = 0; n   k = 0; n   for (i = 0; i < n; ++i) { n     if (!i || p[i].first != p[i - 1].first) { n       k = 0; n     } n     ++k; n     m = max(m, k); n     a[p[i].second] = k; n   } n   cout << m << endl; n   for (i = 0; i < n; ++i) { n     cout << a[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct _ { n   ios_base::Init i; n   _() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); } n } _; n const int lgn = 32; n int n; n vector<vector<int>> gr; n vector<vector<int>> P; n vector<int> lvl, ans, dp; n map<pair<int, int>, int> ind; n void pre(int curr, int par) { n   lvl[curr] = lvl[par] + 1; n   P[curr][0] = par; n   for (int j = 1; j <= lgn; j++) P[curr][j] = P[P[curr][j - 1]][j - 1]; n   for (auto child : gr[curr]) { n     if (child == par) continue; n     pre(child, curr); n   } n   return; n } n int lca(int u, int v) { n   if (lvl[u] > lvl[v]) swap(u, v); n   for (int j = lgn; j >= 0; j--) { n     if (lvl[P[v][j]] >= lvl[u]) v = P[v][j]; n   } n   if (u == v) return u; n   for (int j = lgn; j >= 0; j--) { n     if (P[u][j] != P[v][j]) u = P[u][j], v = P[v][j]; n   } n   return P[u][0]; n } n void pull(int curr, int par) { n   for (auto child : gr[curr]) { n     if (child == par) continue; n     pull(child, curr); n     dp[curr] += dp[child]; n   } n   if (curr != 1) ans[ind[{curr, par}]] = dp[curr]; n   return; n } n int main() { n   cin >> n; n   gr.resize(n + 1); n   lvl.resize(n + 1, 0); n   dp.resize(n + 1, 0); n   ans.resize(n - 1, 0); n   P.resize(n + 1, vector<int>(lgn + 1, 0)); n   for (int i = 0; i <= n - 2; i++) { n     int u, v; n     cin >> u >> v; n     gr[u].push_back(v); n     gr[v].push_back(u); n     ind[{u, v}] = ind[{v, u}] = i; n   } n   pre(1, 1); n   int q; n   cin >> q; n   while (q--) { n     int a, b; n     cin >> a >> b; n     if (lvl[a] > lvl[b]) swap(a, b); n     int lc = lca(a, b); n     if (lc == a) { n       dp[b]++; n       dp[a]--; n     } else { n       dp[a]++; n       dp[b]++; n       dp[lc] -= 2; n     } n   } n   pull(1, 1); n   for (int i = 0; i <= n - 2; i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool rsort1(const pair<long long int, long long int> &a, n             const pair<long long int, long long int> &b) { n   return (a.first > b.first); n } n bool rsort2(const pair<long long int, long long int> &a, n             const pair<long long int, long long int> &b) { n   return (a.second > b.second); n } n bool sort2(const pair<long long int, long long int> &a, n            const pair<long long int, long long int> &b) { n   return (a.second < b.second); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t; n   cin >> t; n   while (t--) { n     long long int h, m; n     cin >> h >> m; n     if (m == 0) n       cout << (24 - h) * 60 <<   n ; n     else n       cout << (23 - h) * 60 + (60 - m) <<   n ; n   } 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     int a, b; n     cin >> a; n     set<int> s; n     while (a--) { n       int b; n       cin >> b; n       s.insert(b); n     } n     cout << s.size() << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   if (n == 1) { n     cout << 1; n     return 0; n   } n   long long a[100005]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   long long ans = 0, kol = 1; n   for (int i = 0; i < n - 1; i++) n     if (a[i] == a[i + 1]) n       kol++; n     else { n       ans += kol * (kol + 1) / 2; n       kol = 1; n     } n   ans += kol * (kol + 1) / 2; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, p, q, a; n   cin >> p >> q >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a; n     if (q == 0 || a > p / q) return cout <<  NO , 0; n     p -= q * a; n     swap(p, q); n   } n   cout << ((q) ?  NO  :  YES ); n } n


#include <bits/stdc++.h> n #define maxn 1086 n  n using namespace std; n  n typedef long long ll; n  n int n, m; n ll a[maxn][maxn]; n ll sum[maxn], sum2[maxn]; n map<ll, int> mp; n  n int main(){ n  scanf( %d%d , &m, &n); n  for(int i = 1;i <= n;i++) for(int j = 1;j <= m;j++) scanf( %lld , &a[i][j]); n  for(int i = 1;i <= n;i++) for(int j = 1;j <= m;j++) sum[i] += a[i][j], sum2[i] += a[i][j] * a[i][j]; n  for(int i = 2;i <= n;i++) mp[sum[i] - sum[i - 1]]++; n  int mx = 0; n  ll d, val; n  for(map<ll, int>::iterator it = mp.begin();it != mp.end();++it){ n   if(it->second > mx) mx = it->second, d = it->first; n  } n  int x; n  for(int i = 2;i <= n;i++){ n   if(sum[i] - sum[i - 1] != d){ n    if(i == n) x = n, val = d - (sum[i] - sum[i - 1]); n    else if(sum[i + 1] - sum[i] != d) x = i, val = d - (sum[i] - sum[i - 1]); n    else x = 1, val = sum[i] - sum[i - 1] - d; n    break; n   } n  } n  ll d2; n  if(x > 3) d2 = (sum2[1] + sum2[3] - 2 * sum2[2]) / 2; n  else d2 = (sum2[n - 2] + sum2[n] - 2 * sum2[n - 1]) / 2; n  for(int i = 1;i <= m;i++){ n   if(x == 1){ n    if(sum2[x] - a[x][i] * a[x][i] + (a[x][i] + val) * (a[x][i] + val) + sum2[x + 2] - 2 * sum2[x + 1] == 2 * d2) return printf( %d %d , x - 1, a[x][i] + val), 0; n   }else if(x == n){ n    if(sum2[x - 2]+ sum2[x] - a[x][i] * a[x][i] + (a[x][i] + val) * (a[x][i] + val) - 2 * sum2[x - 1] == 2 * d2) return printf( %d %d , x - 1, a[x][i] + val), 0; n   }else{ n    if(sum2[x - 1] + sum2[x + 1] - 2 * (sum2[x] - a[x][i] * a[x][i] + (a[x][i] + val) * (a[x][i] + val)) == 2 * d2) return printf( %d %d , x - 1, a[x][i] + val), 0; n   } n  } n   n }


#include <bits/stdc++.h> n using namespace std; n int n, m; n vector<int> A, B; n int d[10000010]; n int a[10000010]; n int pr[10010], pn; n bool isp[100010]; n void GetPrime(int n) { n   memset(isp + 2, 1, sizeof(isp) - 2); n   for (int i = 2; i < n; i++) { n     if (isp[i]) pr[pn++] = i; n     for (int j = 0; j < pn && pr[j] * i < n; j++) { n       isp[pr[j] * i] = 0; n       if (i % pr[j] == 0) break; n     } n   } n } n int main() { n   std::ios::sync_with_stdio(false); n   GetPrime(10000); n   cin >> n >> m; n   A.resize(n), B.resize(m); n   for (int i = 0; i < n; i++) cin >> A[i]; n   for (int i = 0; i < m; i++) cin >> B[i]; n   for (int i = 0; i < n; i++) { n     int t = A[i]; n     for (int j = 0; pr[j] * pr[j] <= t; j++) { n       while (t % pr[j] == 0) d[pr[j]]++, t /= pr[j]; n     } n     if (t > 1) d[t]++; n   } n   for (int i = 0; i < m; i++) { n     int t = B[i]; n     for (int j = 0; pr[j] * pr[j] <= t; j++) { n       while (t % pr[j] == 0) d[pr[j]]--, t /= pr[j]; n     } n     if (t > 1) d[t]--; n   } n   cout << n <<     << m << endl; n   for (int i = 0; i < n; i++) { n     int r = 1; n     int t = A[i]; n     for (int j = 0; pr[j] * pr[j] <= t; j++) { n       while (t % pr[j] == 0) { n         t /= pr[j]; n         if (d[pr[j]] > 0) { n           d[pr[j]]--; n           r *= pr[j]; n         } n       } n     } n     if (t > 1 && d[t] > 0) d[t]--, r *= t; n     cout << r <<    ; n   } n   cout << endl; n   for (int i = 0; i < m; i++) { n     int r = 1; n     int t = B[i]; n     for (int j = 0; pr[j] * pr[j] <= t; j++) { n       while (t % pr[j] == 0) { n         t /= pr[j]; n         if (d[pr[j]] < 0) { n           d[pr[j]]++; n           r *= pr[j]; n         } n       } n     } n     if (t > 1 && d[t] < 0) d[t]++, r *= t; n     cout << r <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   long long x, y; n   long long a = 0, d = 0; n   for (long long i = 0; i < n; i++) { n     cin >> x >> y; n     if (x < 0) { n       a++; n     } else n       d++; n   } n   if (a <= 1) { n     cout <<  Yes  << endl; n   } else if (d <= 1) { n     cout <<  Yes  << endl; n   } else { n     cout <<  No  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, ans, mx; n vector<vector<int>> g; n vector<bool> vis; n bool a[100001]; n void bfs(int src) { n   vis[src] = true; n   queue<int> q; n   q.push(src); n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     for (int i = 0; i < g[u].size(); i++) { n       int v = g[u][i]; n       if (!vis[v]) { n         vis[v] = true; n         q.push(v); n         ans++; n       } n     } n   } n } n int main() { n   scanf( %d%d , &n, &k); n   g.resize(n); n   vis.resize(n); n   for (int a, b, i = 0; i < k; i++) { n     scanf( %d%d , &a, &b); n     --a; n     --b; n     g[a].push_back(b); n     g[b].push_back(a); n   } n   for (int i = 0; i < n; i++) { n     if (!vis[i]) bfs(i); n   } n   cout << k - ans; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1e9 + 7; n long long N = 100107; n long long binpow(long long x, long long y) { n   if (y == 0) return 1; n   if (y == 1) n     return x % mod; n   else if (y % 2 == 0) { n     long long w = binpow(x, y / 2) % mod; n     return (w * w) % mod; n   } else n     return ((x % mod) * (binpow(x, y - 1) % mod)) % mod; n } n int main() { n   long long t = 1; n   cin >> t; n   while (t--) { n     long long n, m, k, kol = 0, sum = 0; n     set<long long> se1; n     set<long long> se2; n     vector<long long> v; n     string s; n     cin >> n >> m; n     long long a[n + 10][m + 10]; n     for (long long i = 1; i <= n; i++) n       for (long long j = 1; j <= m; j++) { n         cin >> a[i][j]; n         if (a[i][j] == 1) { n           se1.insert(i); n           se2.insert(j); n         } n       } n     long long mn = min(n - se1.size(), m - se2.size()); n     if (mn % 2 == 0) n       cout <<  Vivek ; n     else n       cout <<  Ashish ; n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a, b, fr, k1, k2; n int dist[3005][3005]; n bool flag[3005][3005]; n vector<int> v[3005]; n queue<int> q; n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d %d , &a, &b); n     v[a].push_back(b); n     v[b].push_back(a); n   } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) dist[i][j] = 1e9; n   memset(flag, 1, sizeof(flag)); n   for (int k = 1; k <= n; k++) { n     while (!q.empty()) q.pop(); n     q.push(k); n     dist[k][k] = 0; n     while (!q.empty()) { n       fr = q.front(); n       q.pop(); n       flag[fr][fr] = false; n       for (int i = 0; i < v[fr].size(); i++) { n         if (flag[k][v[fr][i]]) { n           flag[k][v[fr][i]] = false; n           dist[k][v[fr][i]] = dist[k][fr] + 1; n           q.push(v[fr][i]); n         } n       } n     } n   } n   int s1, t1, l1, s2, t2, l2, ans = -1; n   scanf( %d %d %d , &s1, &t1, &l1); n   scanf( %d %d %d , &s2, &t2, &l2); n   for (int i = 1; i < n; i++) { n     for (int j = i + 1; j <= n; j++) { n       k1 = min(dist[s1][i] + dist[i][j] + dist[j][t1], n                dist[s1][j] + dist[i][j] + dist[i][t1]); n       k2 = min(dist[s2][i] + dist[i][j] + dist[j][t2], n                dist[s2][j] + dist[i][j] + dist[i][t2]); n       if (k1 <= l1 && k2 <= l2) { n         ans = max(ans, m - k1 - k2 + dist[i][j]); n       } n     } n   } n   if (dist[s1][t1] <= l1 && dist[s2][t2] <= l2) { n     ans = max(ans, m - dist[s1][t1] - dist[s2][t2]); n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int cnt[200001], n, par; n vector<int> adj[200001]; n vector<pair<int, int> > v; n void count(int h, int p) { n   cnt[h]++; n   for (auto it : adj[h]) n     if (it ^ p) { n       count(it, h); n       cnt[h] += cnt[it]; n     } n } n int ct(int h, int p) { n   for (auto it : adj[h]) n     if (it ^ p) { n       if (cnt[it] > n / 2) return ct(it, h); n     } n   par = p; n   return h; n } n vector<pair<int, pair<int, int> > > res; n void dfs(int h, int p) { n   for (auto it : adj[h]) n     if (it ^ p) { n       dfs(it, h); n     } n   v.push_back({h, p}); n } n void f(int h, int p) { n   v.clear(); n   dfs(h, p); n   v.pop_back(); n   int t = h; n   for (auto it : v) { n     res.push_back({p, {t, it.first}}); n     res.push_back({it.first, {it.second, h}}); n     t = it.first; n   } n   res.push_back({p, {t, h}}); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     adj[x].push_back(y); n     adj[y].push_back(x); n   } n   count(1, 0); n   int cen = ct(1, 0), cen2 = 0; n   for (auto it : adj[cen]) n     if (par ^ it && 2 * cnt[it] == n) cen2 = it; n   for (auto it : adj[cen]) n     if (it ^ cen2) f(it, cen); n   for (auto it : adj[cen2]) n     if (it ^ cen) f(it, cen2); n   printf( %d n , res.size()); n   for (auto it : res) n     printf( %d %d %d n , it.first, it.second.first, it.second.second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long t; n   cin >> t; n   while (t--) { n     long long n, m; n     cin >> n >> m; n     string s; n     cin >> s; n     vector<long long> p(m), arr(26, 0); n     vector<long long> ans(n, 0); n     for (int i = 0; i < int(m); i++) { n       cin >> p[i]; n       ++ans[p[i] - 1]; n     } n     for (long long i = n - 1; i > 0; --i) ans[i - 1] += ans[i]; n     for (int i = 0; i < int(n); i++) { n       arr[s[i] -  a ] += ans[i] + 1; n     } n     for (int i = 0; i < int(26); i++) cout << arr[i] <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, bal; n string s, t; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> s; n   n = s.length(); n   bal = m = 0; n   for (int i = n - 1; i >= 0; --i) { n     if (s[i] ==  0 ) n       --bal; n     else { n       if (bal + m >= 0) { n         int j = i; n         while (j >= 0 && s[j] ==  1 ) { n           s[j] =  0 ; n           --j; n           ++m; n         } n         --bal; n       } else n         ++bal; n     } n   } n   cout << s; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int l[200000], r[200000], p[200001]; n vector<int> beg[200001], ed[200001]; n map<unsigned long long, unsigned long long> cnt, sum; n unsigned long long h[200000], all; n mt19937_64 mt(time(0)); n int main() { n   scanf( %d%d , &n, &m); n   all = 0; n   for (int i = 0; i < (int)(n); ++i) { n     scanf( %d%d , l + i, r + i); n     --l[i], --r[i]; n     ++p[l[i]]; n     --p[r[i] + 1]; n     beg[l[i]].push_back(i); n     ed[r[i] + 1].push_back(i); n     h[i] = mt(); n     all ^= h[i]; n   } n   unsigned long long mask = 0, bmask = all, emask = 0, change = 0, ans = 0; n   for (int i = 0; i < (int)(m + 1); ++i) { n     for (int x : ed[i]) { n       emask ^= h[x]; n     } n     mask ^= change; n     ans += cnt[mask ^ all ^ bmask] * i - sum[mask ^ all ^ bmask]; n     ++cnt[mask ^ emask]; n     sum[mask ^ emask] += i; n     for (int x : beg[i]) { n       change ^= h[x]; n       bmask ^= h[x]; n     } n     for (int x : ed[i]) { n       change ^= h[x]; n     } n   } n   int t = 0, c = 0; n   for (int i = 0; i < (int)(m); ++i) { n     t += p[i]; n     if (t > 0) { n       c = 0; n     } else { n       ++c; n       ans -= (unsigned long long)c * (c + 1) / 2; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string str; n   cin >> str; n   for (int i = 0;; i += 2) { n     if (i >= str.length() - 1) break; n     if (str[i] == str[i + 1]) { n       str.erase(str.begin() + i); n       i -= 2; n     } n   } n   if (str.length() % 2) str.erase(str.end() - 1); n   cout << n - str.length() << endl; n   cout << str << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 60; n long long fib[MAXN]; n int main() { n   int n; n   long long k; n   cin >> n >> k; n   fib[0] = 1; n   fib[1] = 1; n   for (int i = 2; i <= n; i++) fib[i] = fib[i - 1] + fib[i - 2]; n   int i = 0; n   while (i < n) { n     if (fib[n - i - 1] < k) { n       k -= fib[n - i - 1]; n       cout << i + 2 <<     << i + 1 <<    ; n       i += 2; n     } else { n       cout << i + 1 <<    ; n       i++; n     } n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   int cnt; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> k; n     cnt = 0; n     if (k == 1 || k == 2 || k == 3 || k == 5 || k == 7 || k == 11) { n       cout << -1 << endl; n     } else { n       if (k % 4 == 3) { n         k = k - 15; n         cnt = 2; n       } else if (k % 4 == 2) { n         k = k - 6; n         cnt = 1; n       } else if (k % 4 == 1) { n         k = k - 9; n         cnt = 1; n       } n       cnt = cnt + (k / 4); n       cout << cnt << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string S; n int MOD; n int len; n long long dp[1 << 18][100]; n int cnt[1 << 18]; n int bits[1 << 18]; n int DMASKS[1 << 18]; n int powers[20]; n int orig; n long long solve(int mask, int mod) { n   if (mask == 0) { n     return mod == 0; n   } n   long long &ret = dp[mask][mod]; n   if (ret != -1) return ret; n   ret = 0; n   int pos = len - cnt[mask]; n   int dMask = pos == 0; n   int m = mask; n   while (m > 0) { n     int b = m & -m; n     if (!(dMask & (1 << (S[bits[b]] -  0 )))) { n       ret += solve(mask ^ b, n                    (mod + powers[len - pos - 1] * (S[bits[b]] -  0 )) % MOD); n       dMask |= 1 << (S[bits[b]] -  0 ); n     } n     m -= b; n   } n   return ret; n } n int main() { n   memset(dp, -1, sizeof dp); n   cin >> S >> MOD; n   len = S.size(); n   powers[0] = 1; n   powers[0] %= MOD; n   for (int i = 1; i < 20; i++) { n     powers[i] = powers[i - 1] * 10; n     powers[i] %= MOD; n   } n   for (int i = 0; i < 18; i++) { n     bits[1 << i] = i; n   } n   for (int i = 0, to = 1 << 18; i < to; i++) { n     cnt[i] = __builtin_popcount(i); n   } n   orig = (1 << len) - 1; n   cout << solve(orig, 0) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 220000; n vector<int> e1[M], e2[M]; n int p[M], dis[M]; n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int u, v; n     cin >> u >> v; n     e1[u].push_back(v); n     e2[v].push_back(u); n   } n   int k; n   cin >> k; n   for (int i = 0; i < k; i++) cin >> p[i]; n   queue<int> q; n   q.push(p[k - 1]); n   dis[p[k - 1]] = 1; n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     for (int v : e2[u]) { n       if (dis[v] == 0) { n         dis[v] = dis[u] + 1; n         q.push(v); n       } n     } n   } n   int mi = 0, mx = 0; n   for (int i = 0; i < k - 1; i++) { n     if (dis[p[i]] != dis[p[i + 1]] + 1) ++mi; n     for (int v : e1[p[i]]) { n       if (v != p[i + 1] && dis[v] == dis[p[i]] - 1) { n         ++mx; n         break; n       } n     } n   } n   cout << mi <<     << mx << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int n; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   if (n == 1 || n == 2) { n     cout << n <<   n ; n     return 0; n   } n   int ans = 2, temp = 2; n   for (int i = 2; i < n; i++) { n     if (a[i] == a[i - 1] + a[i - 2]) { n       temp++; n       ans = max(ans, temp); n     } else { n       temp = 2; n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double Pi = 3.14159265358979323; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(); n   double x0, y0, v, T, n, x, y, r; n   vector<tuple<double, double> > c; n   cin >> x0 >> y0 >> v >> T; n   T = v * T * 1.0; n   cin >> n; n   while (n--) { n     cin >> x >> y >> r; n     double angulo, d, adiff; n     d = sqrt((x - x0) * (x - x0) + (y - y0) * (y - y0)); n     if (d * d <= r * r || (x == x0 && y == y0)) { n       cout <<  1.000 n ; n       return 0; n     } n     if (r == 0) continue; n     if (d * d >= ((r + T) * (r + T))) continue; n     angulo = atan2(y - y0, x - x0); n     if (angulo < 0) { n       angulo += 2 * Pi; n     } n     if (sqrt(d * d - r * r) <= T) { n       adiff = asin(r / d); n     } else { n       adiff = acos((-r * r + T * T + d * d) / (2 * T * d)); n     } n     double x1 = angulo - adiff; n     if (x1 < 0) x1 = 2 * Pi + x1; n     c.push_back(make_tuple(x1, 1)); n     x1 = angulo + adiff; n     if (x1 > 2 * Pi) x1 -= 2 * Pi; n     c.push_back(make_tuple(x1, -1)); n   } n   sort(c.begin(), c.end()); n   double s = 0.0; n   int in = 0, fin = -1; n   vector<int> sweep(c.size(), 0); n   while (in != fin && c.size() != 0) { n     double angulo, con; n     if (fin == -1 && in == 0) fin = 0; n     tie(angulo, con) = c[in]; n     if (con == -1 && s == 0) fin = in; n     if (con == -1 && s > 0) n       s--; n     else if (con == 1) n       s++; n     sweep[in] += s; n     in++; n     in %= c.size(); n   } n   in = -1; n   for (int i = 0; i < c.size(); i++) { n     if (sweep[i] == 0) { n       in = i; n       in++; n       in %= c.size(); n       break; n     } n   } n   if (in == -1 && c.size() != 0) { n     cout <<  1.00 ; n     return 0; n   } n   fin = -1; n   double sum = 0.0, an = -1; n   while (c.size() != 0 && in != fin) { n     if (fin == -1) fin = in; n     if (sweep[in] == 1 && an == -1) an = get<0>(c[in]); n     if (sweep[in] == 0) { n       if (get<0>(c[in]) < an) n         sum += get<0>(c[in]) - an + 2 * Pi; n       else n         sum += get<0>(c[in]) - an; n       an = -1; n     } n     in++; n     in %= c.size(); n   } n   cout << setprecision(20) << (sum / (2 * Pi)) <<   n ; n   return 0; n } n


#include<bits/stdc++.h> n // #include <ext/pb_ds/assoc_container.hpp> n // #include <ext/pb_ds/tree_policy.hpp> n // using namespace __gnu_pbds; n using namespace std; n // typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; n #define ll long long  n #define scn(n) scanf( %d ,&n) n #define lscn(n) scanf( %lld ,&n) n #define lpri(n) printf( %lld ,n) n #define pri(n) printf( %d ,n) n #define pln() printf(  n ) n #define priln(n) printf( %d n ,n) n #define lpriln(n) printf( %lld n ,n) n #define rep(i,init,n) for(int i=init;i<n;i++) n #define pb push_back      n #define mp make_pair n #define F first n #define S second n #define gcd __gcd n #define inf INT_MAX n #define ninf INT_MIN    n #define inf INT_MAX n #define linf LLONG_MAX n #define lninf LLONG_MIN    n const int mod = 1e9 + 7; n const int N = 2e5 + 4; n  n int solve() n { n  int n; scn(n); n  n  string s; cin >> s; n  n  vector<int> pre(n + 1, 0), suf(n + 1, 0); n  n  rep(i, 1, n + 1) n  { n   pre[i] = 1; n  n   if(i - 2 >= 0 and s[i - 1] != s[i - 2]) n   pre[i] += pre[i - 1]; n  } n  n  for(int i = n; i >= 1; i--) n  { n   suf[i] = 1; n  n   if(i < n and s[i - 1] != s[i]) n   suf[i] += suf[i + 1]; n  } n  n  rep(i, 0, n + 1) n  { n   if(i == 0) n   { n    if(s[i] ==  R ) n    printf( %d  , suf[i + 1] + 1); n    else n    printf( 1  ); n   } n   else if(i == n) n   { n    if(s[i - 1] ==  L ) n    printf( %d  , pre[i] + 1); n    else n    printf( 1  ); n   } n   else n   { n    int ans = 1; n  n    if(s[i] ==  R ) n    ans += suf[i + 1]; n  n    if(s[i - 1] ==  L ) n    ans += pre[i]; n  n    printf( %d  , ans); n   } n  } n  n  return 0; n } n  n int main()                   n { n  int t = 1; scn(t); n  n  while(t--) n  { n   solve(); n  } n  n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n int n, k; n int a[200005]; n int cnt[400005]; n int cv[400005]; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     memset(cnt, 0, sizeof(cnt)); n     memset(cv, 0, sizeof(cv)); n     scanf( %d%d , &n, &k); n     for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n     for (int i = 1; i * 2 <= n; i++) { n       cnt[a[i] + a[n - i + 1]]++; n       cv[min(a[i], a[n - i + 1]) + 1]++, cv[max(a[i], a[n - i + 1]) + k + 1]--; n     } n     for (int i = 3; i <= 2 * k; i++) cv[i] += cv[i - 1]; n     int ans = 200005; n     for (int x = 2; x <= 2 * k; x++) { n       ans = min(ans, n - cnt[x] - cv[x]); n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100000]; n vector<int> adjNos[100001]; n vector<long long int> diff; n int main() { n   int n; n   int m; n   long long int sum = 0; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     cin >> a[i]; n   } n   for (int i = 0; i < m; i++) { n     if (i < m - 1) { n       if (a[i] != a[i + 1]) { n         adjNos[a[i]].push_back(a[i + 1]); n         adjNos[a[i + 1]].push_back(a[i]); n       } n     } n   } n   long long int beforeSum; n   long long int afterSum; n   int vlen; n   int median; n   for (int i = 1; i <= n; i++) { n     vlen = adjNos[i].size(); n     if (vlen == 0) { n       continue; n     } n     sort(adjNos[i].begin(), adjNos[i].end()); n     median = adjNos[i][vlen / 2]; n     beforeSum = 0; n     afterSum = 0; n     for (int j = 0; j < vlen; j++) { n       beforeSum += abs(i - adjNos[i][j]); n       afterSum += abs(median - adjNos[i][j]); n     } n     diff.push_back(beforeSum - afterSum); n     sum += beforeSum; n   } n   sum /= 2; n   if (diff.size() != 0) { n     sum -= *max_element(diff.begin(), diff.end()); n   } n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n typedef struct node_t { n   int l, r, k, id; n   friend bool operator<(const node_t& a, const node_t& b) { n     if (a.r == b.r) return a.l > b.l; n     return a.r < b.r; n   } n   void print() { printf( l = %d, r = %d, k = %d, id = %d n , l, r, k, id); } n } node_t; n const int maxn = 1e5 + 5; n node_t Q[maxn], P[maxn]; n int ans[maxn]; n int main() { n   ios::sync_with_stdio(false); n   int n, m; n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %d %d , &Q[i].l, &Q[i].r); n     Q[i].id = i; n   } n   scanf( %d , &m); n   for (int i = 0; i < m; ++i) { n     scanf( %d %d %d , &P[i].l, &P[i].r, &P[i].k); n     P[i].id = i + 1; n   } n   sort(Q, Q + n); n   sort(P, P + m); n   set<pair<int, int> > st; n   set<pair<int, int> >::iterator iter, titer; n   int i, j, k; n   i = 0; n   for (j = 0; j < m; ++j) { n     while (i < n && Q[i].r <= P[j].r) { n       st.insert(make_pair(Q[i].l, Q[i].id)); n       ++i; n     } n     iter = st.lower_bound(make_pair(P[j].l, -1)); n     while (iter != st.end() && P[j].k-- > 0) { n       ans[iter->second] = P[j].id; n       titer = iter; n       iter++; n       st.erase(titer); n     } n   } n   if (i < n || st.size() > 0) { n     puts( NO ); n   } else { n     puts( YES ); n     for (int i = 0; i < n; ++i) { n       printf( %d  , ans[i]); n     } n     putchar(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, size_t N> n long long SIZE(const T (&t)[N]) { n   return N; n } n template <typename T> n long long SIZE(const T &t) { n   return t.size(); n } n string to_string(const string s, long long x1 = 0, long long x2 = 1e9) { n   return     + ((x1 < s.size()) ? s.substr(x1, x2 - x1 + 1) :   ) +    ; n } n string to_string(const char *s) { return to_string((string)s); } n string to_string(const bool b) { return (b ?  true  :  false ); } n string to_string(const char c) { return string({c}); } n template <size_t N> n string to_string(const bitset<N> &b, long long x1 = 0, long long x2 = 1e9) { n   string t =   ; n   for (long long __iii__ = min(x1, SIZE(b)), __jjj__ = min(x2, SIZE(b) - 1); n        __iii__ <= __jjj__; ++__iii__) { n     t += b[__iii__] +  0 ; n   } n   return     + t +    ; n } n template <typename A, typename... C> n string to_string(const A(&v), long long x1 = 0, long long x2 = 1e9, n                  C... coords); n long long l_v_l_v_l = 0, t_a_b_s = 0; n template <typename A, typename B> n string to_string(const pair<A, B> &p) { n   l_v_l_v_l++; n   string res =  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n   l_v_l_v_l--; n   return res; n } n template <typename A, typename... C> n string to_string(const A(&v), long long x1, long long x2, C... coords) { n   long long rnk = rank<A>::value; n   string tab(t_a_b_s,    ); n   string res =   ; n   bool first = true; n   if (l_v_l_v_l == 0) res +=   n ; n   res += tab +  [ ; n   x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); n   auto l = begin(v); n   advance(l, x1); n   auto r = l; n   advance(r, (x2 - x1) + (x2 < SIZE(v))); n   for (auto e = l; e != r; e = next(e)) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     l_v_l_v_l++; n     if (e != l) { n       if (rnk > 1) { n         res +=   n ; n         t_a_b_s = l_v_l_v_l; n       }; n     } else { n       t_a_b_s = 0; n     } n     res += to_string(*e, coords...); n     l_v_l_v_l--; n   } n   res +=  ] ; n   if (l_v_l_v_l == 0) res +=   n ; n   return res; n } n void dbgm() { ; } n template <typename Heads, typename... Tails> n void dbgm(Heads H, Tails... T) { n   cerr << to_string(H) <<   |  ; n   dbgm(T...); n } n void FIO() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n const long long N = 2e5 + 6; n int32_t main() { n   FIO(); n   long long tc; n   cin >> tc; n   while (tc--) { n     long long n, m; n     cin >> m >> n; n     vector<vector<long long> > a(m, vector<long long>(n)); n     vector<long long> v(n, -1); n     for (long long i = 0; i < m; i++) { n       for (long long j = 0; j < n; j++) { n         cin >> a[i][j]; n         v[j] = max(v[j], a[i][j]); n       } n     } n     long long ans = 1e18; n     for (long long i = 0; i < n; i++) ans = min(ans, v[i]); n     if (n <= m) { n       long long val = -1; n       for (long long i = 0; i < m; i++) { n         sort(a[i].begin(), a[i].end()); n         val = max(val, a[i][n - 2]); n       } n       ans = min(ans, val); n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mx = 2e5 + 7; n const int base = 19260817; n int a[mx]; n int n; n unsigned long long hs[mx * 2]; n void init() { n   hs[0] = 1; n   for (int i = 1; i <= 2 * n; i++) hs[i] = hs[i - 1] * base; n } n int main() { n   int m, x; n   scanf( %d , &n); n   init(); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   scanf( %d , &m); n   unsigned long long ans = 0, cur = 0; n   long long sum = 0; n   int p = 1, go = 1; n   for (int i = 1; i <= m; i++) { n     scanf( %d , &x); n     ans += hs[x - 1]; n   } n   if (m == 1) { n     printf( 0 n ); n     return 0; n   } n   int pre = 0; n   for (int i = 1; i <= m; i++) { n     sum += (i == 1 ? 0 : abs(a[p] - a[pre])); n     cur += hs[p - 1]; n     pre = p; n     p += go; n     if (p == n) n       go = -1; n     else if (p == 1) n       go = 1; n   } n   set<long long> s; n   if (cur == ans) s.insert(sum); n   for (int i = 2; i <= 2 * n - 2; i++) { n     cur -= i > n ? hs[2 * n - i] : hs[i - 2]; n     int end = i + (m - 1) % (2 * n - 2); n     while (end > 2 * n - 2) end -= 2 * n - 2; n     cur += end > n ? hs[2 * n - end - 1] : hs[end - 1]; n     sum -= abs(i <= n ? a[i] - a[i - 1] : a[2 * n - i] - a[2 * n - i + 1]); n     sum += abs(end <= n ? (end == 1 ? a[2] - a[1] : a[end] - a[end - 1]) n                         : a[2 * n - end] - a[2 * n - end + 1]); n     if (cur == ans) { n       s.insert(sum); n     } n   } n   if (s.size() != 1) n     printf( -1 n ); n   else n     printf( %lld n , *s.begin()); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:16777216 ) n using namespace std; n void rot(int n, int *x, int *y, int rx, int ry); n int xy2d(int n, int x, int y) { n   int rx, ry, s, d = 0; n   for (s = n / 2; s > 0; s /= 2) { n     rx = (x & s) > 0; n     ry = (y & s) > 0; n     d += s * s * ((3 * rx) ^ ry); n     rot(s, &x, &y, rx, ry); n   } n   return d; n } n void d2xy(int n, int d, int *x, int *y) { n   int rx, ry, s, t = d; n   *x = *y = 0; n   for (s = 1; s < n; s *= 2) { n     rx = 1 & (t / 2); n     ry = 1 & (t ^ rx); n     rot(s, x, y, rx, ry); n     *x += s * rx; n     *y += s * ry; n     t /= 4; n   } n } n void rot(int n, int *x, int *y, int rx, int ry) { n   int t; n   if (ry == 0) { n     if (rx == 1) { n       *x = n - 1 - *x; n       *y = n - 1 - *y; n     } n     t = *x; n     *x = *y; n     *y = t; n   } n } n int main() { n   int a, b; n   cin >> a >> b; n   int x, y; n   d2xy(1 << a, b, &x, &y); n   cout << x <<     << y << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   puts( Even ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char s[60]; n   while (scanf( %s , s) == 1) { n     int k = strlen(s); n     int sum = 0; n     for (int i = 0; i < k / 2; i++) { n       if (s[i] == s[k - i - 1]) sum++; n     } n     int ans = k; n     if (sum == k / 2) ans -= 1; n     int t = 0; n     if (ans == k - 1) { n       for (int i = 1; i <= (k - 1) / 2; i++) { n         if (s[i] == s[k - i]) t++; n       } n       if (t == (k - 1) / 2) ans = 0; n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n unsigned seed = chrono::system_clock::now().time_since_epoch().count(); n mt19937 eng(seed); n long long random2() { return (1ll << 31ll) * eng() + eng(); } n long long n, m, k, q, T; n const long long big = 1000000007; n const long long big2 = 1000000009; n const long long mod = 998244353; n const int MAXN = 300001; n long long PATH[401][401] = {0}; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long a, b, c, d, e; n   cin >> k; n   for (int c1 = 0; c1 < (401); ++c1) { n     for (int c2 = 0; c2 < (401); ++c2) { n       if (c1 == 0 && c2 == 1) PATH[c1][c2] = 1; n       if (c2 == 0) PATH[c1][c2] = 1; n     } n   } n   for (int i = 1; i < (k); ++i) { n     for (long long r = 1; r < k + 1; r++) { n       for (int x = 0; x < (r + 2); ++x) { n         if (x < r) n           PATH[i][r] += (PATH[i - 1][x] * PATH[i - 1][r - 1 - x]) % big; n         if (x < r + 1) n           PATH[i][r] += n               (1 + 2 * r) * ((PATH[i - 1][x] * PATH[i - 1][r - x]) % big); n         PATH[i][r] %= big; n         PATH[i][r] += n             r * (r + 1) * ((PATH[i - 1][x] * PATH[i - 1][r - x + 1]) % big); n         PATH[i][r] %= big; n       } n     } n   } n   cout << PATH[k - 1][1] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = (int)1e9, maxn = (int)1e5 + 1; n const double eps = (double)1e-8; n const int mod = (int)1000000007; n int c[200009], a[200009], t, k, f, n; n vector<int> b[200009], o; n int main() { n   cin >> n; n   memset((c), 0, sizeof(c)); n   ; n   for (int i = (1); i <= (n); i++) { n     scanf( %d , &(a[i])); n     b[a[i]].push_back(i); n   } n   k = 0; n   t = 1; n   for (int i = 1; i <= n && t; i++) { n     if (b[k].size() == c[k]) { n       t = 0; n       break; n     } n     o.push_back(b[k][c[k]]); n     c[k]++; n     if (k - 2 < 0 || b[k - 2].size() == c[k - 2] || n         b[k - 1].size() == c[k - 1] || b[k].size() == c[k]) { n       k++; n       continue; n     } n     k -= 2; n   } n   if (t) { n     cout <<  Possible  << endl; n     for (int i = 0; i < o.size(); i++) { n       cout << o[i] <<    ; n     } n     return 0; n   } n   k--; n   for (int i = n; i >= 0; i--) { n     if ((b[i].size() != c[i] && i >= k) || (b[i].size() > c[i] + 1)) { n       cout <<  Impossible ; n       return 0; n     } n     if (b[i].size() == c[i] + 1 && (i == 0 || b[i - 1].size() == c[i - 1])) { n       if ((k - i + 1) % 3 != 0) { n         cout <<  Impossible ; n         return 0; n       } n       k = i; n       o.push_back(b[i][c[i]]); n       if (b[i + 1].size() != c[i + 1]) { n         o.push_back(b[i + 1][c[i + 1]]); n         k = i + 1; n       } n     } n   } n   cout <<  Possible  << endl; n   for (int i = 0; i < o.size(); i++) { n     cout << o[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cin.tie(NULL); n   ios::sync_with_stdio(false); n   long long int n, m, i, j, k, ans = 0; n   cin >> n >> m; n   long long int A[n], B[m]; n   string str(500,  0 ); n   for (i = 0; i < n; i++) cin >> A[i]; n   for (i = 0; i < m; i++) { n     cin >> k; n     k--; n     B[i] = k; n   } n   for (i = 0; i < m; i++) { n     j = i - 1; n     str = string(500,  0 ); n     while (j >= 0 && B[j] != B[i]) { n       if (str[B[j]] !=  1 ) { n         ans += A[B[j]]; n         str[B[j]] =  1 ; n       } n       j--; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n static const int MAXN = 2e5 + 4; n static const int MAXK = 2e5 + 3; n static int n, k; n static std::pair<int, int> p[MAXN]; n static int idx[MAXN]; n static std::vector<int> q[MAXN]; n static int ans[MAXN]; n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &p[i].first); n     p[i].second = i; n   } n   for (int i = 0, u, v; i < k; ++i) { n     scanf( %d%d , &u, &v); n     --u, --v; n     q[u].push_back(v); n     q[v].push_back(u); n   } n   std::sort(p, p + n); n   for (int i = 0; i < n; ++i) idx[p[i].second] = i; n   for (int i = 0, j; i < n;) { n     for (j = i + 1; j < n && p[j].first == p[i].first; ++j) n       ; n     for (int count = i; i < j; ++i) { n       int r = count; n       for (int t : q[p[i].second]) n         if (p[idx[t]].first < p[i].first) --r; n       ans[p[i].second] = r; n     } n   } n   for (int i = 0; i < n; ++i) printf( %d%c , ans[i], i == n - 1 ?   n  :    ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng_32(chrono::steady_clock::now().time_since_epoch().count()); n const long long maxn = 3e5 + 10; n struct E { n   long long to, nxt; n } e[maxn * 2]; n long long tot, head[maxn]; n long long Add[maxn << 2], lz[maxn << 2]; n void adde(long long u, long long v) { n   tot++; n   e[tot] = {v, head[u]}; n   head[u] = tot; n } n vector<pair<long long, long long> > v[maxn]; n long long n; n long long ans[maxn]; n void dfs(int u, int fa, int dep) { n   int len = v[u].size(); n   for (int i = 0; i < len; i++) { n     int d = min(n + 1, dep + v[u][i].first + 1); n     Add[dep] += v[u][i].second; n     Add[d] -= v[u][i].second; n   } n   Add[dep] += Add[dep - 1]; n   ans[u] = Add[dep]; n   for (int i = head[u]; i; i = e[i].nxt) { n     int to = e[i].to; n     if (to == fa) continue; n     dfs(to, u, dep + 1); n   } n   for (int i = 0; i < len; i++) { n     int d = min(n + 1, dep + v[u][i].first + 1); n     Add[dep] -= v[u][i].second; n     Add[d] += v[u][i].second; n   } n   Add[dep] -= Add[dep - 1]; n } n int main() { n   cin >> n; n   long long t1, t2; n   for (long long i = 1; i < n; i++) { n     scanf( %lld%lld , &t1, &t2); n     adde(t1, t2); n     adde(t2, t1); n   } n   long long m, t3; n   cin >> m; n   for (long long i = 1; i <= m; i++) { n     scanf( %lld%lld%lld , &t1, &t2, &t3); n     v[t1].push_back(make_pair(t2, t3)); n   } n   dfs(1, 1, 1); n   for (int i = 1; i <= n; i++) printf( %lld  , ans[i]); n   return 0; n } n