#include <bits/stdc++.h> n using namespace std; n const int Maxn = 40005; n int n, m, c, s, t, ct, cnt, ans, b[Maxn], dis[Maxn], head[Maxn], cur[Maxn], n     a[205][205], id[205][205]; n long long tot; n bool vis[Maxn]; n struct edg { n   int nxt, to, w; n } edge[10 * Maxn]; n void add(int x, int y, int w) { n   edge[++cnt] = (edg){head[x], y, w}; n   head[x] = cnt; n   edge[++cnt] = (edg){head[y], x, 0}; n   head[y] = cnt; n } n int dfs(int u, int T, int mini) { n   if (u == T || !mini) return mini; n   int w, used = 0; n   vis[u] = true; n   for (int i = head[u]; i; i = edge[i].nxt) { n     int to = edge[i].to; n     if (!vis[to] && edge[i].w) { n       w = dfs(to, T, min(mini - used, edge[i].w)); n       used += w, edge[i].w -= w, edge[((i - 1) ^ 1) + 1].w += w; n       if (mini == used) return used; n     } n   } n   return used; n } n void dinic(int S = s, int T = t, int W = 0x3f3f3f3f, bool type = false) { n   int used; n   while (memset(vis, 0, sizeof(bool[ct + 1])), used = dfs(S, T, W)) n     W -= used, ans += type ? -used : used; n } n int main() { n   scanf( %d , &n); n   s = ++ct, t = ++ct; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) { n       id[i][j] = ++ct; n       scanf( %d , &a[i][j]); n       if (a[i][j] > 0) b[++m] = a[i][j]; n       if (!a[i][j]) c++; n     } n   sort(b + 1, b + 1 + m); n   m = unique(b + 1, b + 1 + m) - b - 1; n   for (int j = 1; j <= n; j++) n     for (int k = 1; k <= n; k++) { n       if (a[j][k] == -1) continue; n       if (j != n && a[j + 1][k] != -1) n         add(id[j][k], id[j + 1][k], 1), add(id[j + 1][k], id[j][k], 1); n       if (k != n && a[j][k + 1] != -1) n         add(id[j][k], id[j][k + 1], 1), add(id[j][k + 1], id[j][k], 1); n       if (a[j][k] <= 0) continue; n       add(id[j][k], t, 0x3f3f3f3f); n     } n   for (int i = 1; i < m; i++) { n     for (int j = 1; j <= n; j++) n       for (int k = 1; k <= n; k++) n         if (a[j][k] == b[i]) { n           int w; n           for (int q = head[id[j][k]]; q; q = edge[q].nxt) n             if (edge[q].to == t) { n               w = edge[q].w; n               edge[q].w = edge[((q - 1) ^ 1) + 1].w = 0; n               break; n             } n           dinic(id[j][k], s, 0x3f3f3f3f - w, true); n           add(s, id[j][k], 0x3f3f3f3f); n         } n     dinic(); n     tot += ans * (long long)(b[i + 1] - b[i]); n   } n   printf( %lld , tot); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n vector<int> a[1000001]; n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) { n     int g; n     scanf(  %d , &g); n     while (g--) { n       int p; n       scanf(  %d , &p); n       a[p].push_back(i); n     } n   } n   sort(a, a + m + 1); n   int t = 1, ans = 1; n   for (int i = 2; i <= m; ++i) { n     if (a[i] == a[i - 1]) n       ans = (long long)ans * ++t % mod; n     else n       t = 1; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct debugger { n   template <typename T> n   debugger& operator,(const T& v) { n     cerr << v <<    ; n     return *this; n   } n } dbg; n const double PI = acos(-1); n template <class T> n T gcd(T a, T b) { n   return b ? gcd(b, a % b) : a; n } n int n, m, a, b; n int A[100001], B[100001], L[100001]; n int main() { n   int n, m, a, b; n   scanf( %d%d%d%d , &n, &m, &a, &b); n   int width = b - a; n   for (int i = 0; i < n; i++) scanf( %d , A + i); n   for (int i = 0; i < m; i++) scanf( %d , B + i); n   for (int i = 0; i < m; i++) scanf( %d , L + i); n   double ans = -1; n   int ans_a = -1, ans_b = -1; n   for (int i = 0; i < m; i++) { n     double ay = (double)((double)a / b) * B[i]; n     int aid = lower_bound(A, A + n, ay) - A; n     for (int j = max(0, aid - 1); j <= min(n - 1, aid + 1); j++) { n       double OA = sqrt((long long)pow(a, 2) + (long long)pow(A[j], 2)); n       double river_dist = n           sqrt((long long)pow(width, 2) + (long long)pow(A[j] - B[i], 2)); n       double B_to_village = L[i]; n       double total_dist = OA + river_dist + B_to_village; n       if (ans == -1 || total_dist < ans) { n         ans = total_dist; n         ans_a = j + 1; n         ans_b = i + 1; n       } n     } n   } n   printf( %d %d , ans_a, ans_b); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   scanf( %d , &t); n   cin.ignore(); n   while (t--) { n     int n, arr[1030]; n     map<int, int> maps; n     scanf( %d , &n); n     for (int i = 0; i < n; i++) { n       scanf( %d , &arr[i]); n       maps[arr[i]]++; n     } n     int ans = -1; n     for (int i = 1; i < 1025; i++) { n       map<int, int> cpmaps; n       cpmaps = maps; n       bool pass = true; n       for (int j = 0; j < n; j++) { n         int a = arr[j] ^ i; n         if (cpmaps[a]) n           cpmaps[a]--; n         else { n           pass = false; n           break; n         } n       } n       if (pass) { n         ans = i; n         break; n       } n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> fibonacci; n void createFibbo(int num); n int main() { n   int num; n   cin >> num; n   if (num == 0) n     cout <<  0 0 0 n ; n   else if (num == 1) n     cout <<  0 0 1 n ; n   else if (num == 2) n     cout <<  0 1 1 n ; n   else if (num == 3) n     cout <<  1 1 1 n ; n   else n     createFibbo(num); n   return 0; n } n void createFibbo(int num) { n   int nvo = 0; n   int pos = 2; n   fibonacci.push_back(0); n   fibonacci.push_back(1); n   while (nvo != num) { n     nvo = fibonacci[pos - 1] + fibonacci[pos - 2]; n     fibonacci.push_back(nvo); n     pos++; n   } n   pos--; n   cout << fibonacci[pos - 4] <<     << fibonacci[pos - 3] <<     n        << fibonacci[pos - 1] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n bool debug = 1; n int n, m, k; n int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; n long long ln, lk, lm; n int dp[1 << 20], s[105], t[105]; n string ac[105]; n int main() { n   scanf( %d , &n); n   for (int(i) = 0; (i) < (int)(n); (i)++) scanf( %d , s + i); n   sort(s, s + n); n   reverse(s, s + n); n   scanf( %d , &m); n   for (int(i) = 0; (i) < (int)(m); (i)++) cin >> ac[i] >> t[i]; n   for (int i = 1; i < (1 << m); i++) { n     int id = m - __builtin_popcount(i); n     dp[i] = t[id] == 1 ? -(1e8) : ((1e8)); n     for (int j = 0; j < m; j++) { n       if ((1 << j) & i) { n         if (t[id] == 1) { n           dp[i] = max(dp[i], (ac[id] ==  p  ? s[j] : 0) + dp[i - (1 << j)]); n         } else { n           dp[i] = min(dp[i], (ac[id] ==  p  ? -s[j] : 0) + dp[i - (1 << j)]); n         } n       } n     } n   } n   cout << dp[(1 << m) - 1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long n, m; n   cin >> n >> m; n   map<string, long> ma; n   vector<string> v; n   for (long i = 0; i < n; i++) { n     string a; n     cin >> a; n     v.push_back(a); n     ma[a] = 1; n   } n   long k = 0; n   long count = 0; n   for (long i = 0; i < n - 1; i++) { n     for (long j = i + 1; j < n; j++) { n       long ck = 0; n       string c =   ; n       for (long h = 0; h < m; h++) { n         if (v[i][h] == v[j][h]) { n           c += v[i][h]; n         } else { n           c += char( S  +  E  +  T  - v[i][h] - v[j][h]); n         } n       } n       if (ck == 0) { n         if (ma[c] == 1) count++; n       } n     } n   } n   cout << count / 3 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const long long INF = 1e9; n const long long LINF = INF * INF; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const int N = 1 << 19; n int n, k; n vector<pair<long long, long long>> g[N]; n long long dp[N][2]; n int cmp(pair<long long, long long> x, pair<long long, long long> y) { n   return x.second - x.first > y.second - y.first; n } n long long get(int treba, vector<pair<long long, long long>>& vec) { n   long long ans = 0; n   for (int i = (0); i < (treba); ++i) ans += max(vec[i].first, vec[i].second); n   for (int i = (treba); i < ((int)((vec).size())); ++i) ans += vec[i].first; n   return ans; n } n void dfs(int v, int p) { n   int sz = (int)((g[v]).size()); n   if (v) --sz; n   vector<pair<long long, long long>> vec(sz); n   int ptr = 0; n   for (auto i : g[v]) n     if (i.first != p) { n       dfs(i.first, v); n       vec[ptr++] = {dp[i.first][0], dp[i.first][1] + i.second}; n     } n   sort(vec.begin(), vec.end(), cmp); n   int zara = min((int)((vec).size()), k); n   dp[v][0] = get(zara, vec); n   zara = min((int)((vec).size()), k - 1); n   dp[v][1] = get(zara, vec); n } n void solve() { n   cin >> n >> k; n   for (int i = (0); i < (n); ++i) g[i].clear(); n   for (int i = (0); i < (n - 1); ++i) { n     int u, v, w; n     cin >> u >> v >> w; n     --u; n     --v; n     g[u].push_back({v, w}); n     g[v].push_back({u, w}); n   } n   dfs(0, -1); n   cout << dp[0][0] << endl; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int t; n   cin >> t; n   while (t--) solve(); n   cerr <<  Time elapsed:   << clock() / (double)CLOCKS_PER_SEC << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0; n   bool f = 0; n   char ch = getchar(); n   while (!isdigit(ch)) f |= ch ==  - , ch = getchar(); n   while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); n   return f ? -x : x; n } n inline void write(int x) { n   if (x < 0) putchar( - ), x = -x; n   if (x > 9) write(x / 10); n   putchar(48 + x % 10); n } n inline void writeln(int x) { n   write(x); n   putchar(  n ); n } n inline void writebl(int x) { n   write(x); n   putchar(   ); n } n const int maxn = 2e5 + 5; n struct edge { n   int v, w, next; n } e[maxn]; n int head[maxn], ecnt; n int color[maxn], out[maxn]; n int tot1, tot2, n, ans = -1, want; n queue<int> q; n void dfs(int x) { n   if (color[x]) n     ++tot1; n   else n     ++tot2; n   q.push(x); n   for (register int i = head[x]; i; i = e[i].next) { n     int v = e[i].v, w = color[x] ^ want ^ e[i].w; n     if (color[v] < 0) { n       color[v] = w; n       dfs(v); n       if (tot1 < 0) return; n     } else if (color[v] != w) { n       tot1 = -1; n       return; n     } n   } n } n inline void solve(int op) { n   want = op; n   int res = 0; n   memset(color, -1, sizeof(color)); n   for (register int i = 1; i <= n; ++i) n     if (color[i] < 0) { n       while (q.size()) q.pop(); n       tot1 = tot2 = 0; n       color[i] = 1; n       dfs(i); n       if (tot1 < 0) { n         res = -1; n         break; n       } n       res += tot1 < tot2 ? tot1 : tot2; n       register bool t = tot1 > tot2; n       while (q.size()) color[q.front()] ^= t, q.pop(); n     } n   if ((ans == -1 || ans > res) && res != -1) { n     for (register int i = 1; i <= n; ++i) out[i] = color[i]; n     ans = res; n   } n } n int main() { n   n = read(); n   int m = read(); n   for (register int i = 1; i <= m; ++i) { n     int u = read(), v = read(), w; n     char ch = getchar(); n     while (ch !=  R  && ch !=  B ) ch = getchar(); n     w = ch ==  R ; n     e[++ecnt] = (edge){v, w, head[u]}, head[u] = ecnt; n     e[++ecnt] = (edge){u, w, head[v]}, head[v] = ecnt; n   } n   solve(0); n   solve(1); n   writeln(ans); n   if (ans != -1) n     for (register int i = 1; i <= n; ++i) n       if (out[i]) writebl(i); n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3fffffff; n const int maxn = 100010; n int n, k; n int main() { n   cin >> n >> k; n   if (k > (n - 1) / 2) n     puts( -1 ); n   else { n     printf( %d n , n * k); n     for (int i = 0; i < n; ++i) n       for (int j = 1; j <= k; ++j) printf( %d %d n , i + 1, (i + j) % n + 1); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long C[5001][5001], fak[5000], a, b, c, ans1, ans2, ans3; n const long long MOD = 998244353; n void generate() { n   C[0][0] = 1; n   for (long long i = 1; i <= 5000; i++) { n     C[i][0] = 1; n     for (long long j = 1; j <= i; j++) { n       C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MOD; n     } n   } n   fak[0] = 1; n   for (long long i = 1; i <= 5000; i++) { n     fak[i] = (fak[i - 1] * i) % MOD; n   } n } n int main() { n   generate(); n   cin >> a >> b >> c; n   for (long long i = 0; i <= min(a, b); i++) { n     ans1 = (ans1 + ((C[a][i] * C[b][i]) % MOD * fak[i]) % MOD) % MOD; n   } n   for (long long i = 0; i <= min(b, c); i++) { n     ans2 = (ans2 + ((C[c][i] * C[b][i]) % MOD * fak[i]) % MOD) % MOD; n   } n   for (long long i = 0; i <= min(a, c); i++) { n     ans3 = (ans3 + ((C[a][i] * C[c][i]) % MOD * fak[i]) % MOD) % MOD; n   } n   cout << ((ans1 * ans2) % MOD * ans3) % MOD <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int long long a[200005], b[200005]; n vector<int> v; n int main() { n   int n, t; n   scanf( %d , &t); n   while (t--) { n     scanf( %d , &n); n     int long long neg = 0, ans = 0; n     for (int i = 0; i < n; i++) { n       scanf( %lld , &a[i]); n     } n     for (int i = n - 1; i >= 0; i--) { n       neg += a[i]; n       if (a[i] > 0) { n         if (neg > 0) { n           ans += neg; n           neg = 0; n         } n       } n     } n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n int main(int argc, char *argv[]) { n   std::string s; n   int n; n   std::cin >> n >> s; n   int con = 0; n   int ans = 0; n   for (int i = 0; i < s.length();) { n     while (s[i] ==  x ) { n       con++; n       i++; n     } n     if (s[i] !=  x ) { n       if (con >= 3) { n         ans += con - 2; n       } n       con = 0; n       i++; n     } n   } n   if (con >= 3) { n     ans += con - 2; n   } n   std::cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n struct FenwickTree { n   vector<int> tree; n   FenwickTree(int n = 0) { tree.resize(n); } n   void update(int i, int delta) { n     for (; i < (int)tree.size(); i |= i + 1) tree[i] += delta; n   } n   int sum(int ind) { n     int sum = 0; n     while (ind >= 0) { n       sum += tree[ind]; n       ind &= ind + 1; n       ind--; n     } n     return sum; n   } n   int sumRange(int hi, int lo) { return sum(hi) - sum(lo); } n   int val(int i) { return sum(i) - sum(i - 1); } n }; n int N, Q; n int main() { n   scanf( %d%d , &N, &Q); n   FenwickTree ft(100010); n   for (int i = 1; i <= N; i++) ft.update(i, 1); n   int L = 0, R = N; n   bool l = true; n   while (Q--) { n     int op, a, b; n     scanf( %d%d , &op, &a); n     if (op == 1) { n       int ra = R - L - a; n       if (l) { n         if (a <= ra) { n           L += a; n           for (int i = 0; i < a; i++) ft.update(L + i + 1, ft.val(L - i)); n         } else { n           R -= ra; n           for (int i = 0; i < ra; i++) ft.update(R - i, ft.val(R + i + 1)); n           l = false; n         } n       } else { n         if (a <= ra) { n           R -= a; n           for (int i = 0; i < a; i++) ft.update(R - i, ft.val(R + i + 1)); n         } else { n           L += ra; n           for (int i = 0; i < ra; i++) ft.update(L + i + 1, ft.val(L - i)); n           l = true; n         } n       } n     } else { n       scanf( %d , &b); n       int ans = l ? ft.sumRange(L + b, L + a) : ft.sumRange(R - a, R - b); n       printf( %d n , ans); n     } n   } 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(NULL); n   cout.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int n, k; n     cin >> n >> k; n     for (int i = 0; i < n; i++) { n       char c =  a  + i % k; n       cout << c; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool sortbysec(const array<long long int, 2> &a, n                const array<long long int, 2> &b) { n   return (a[0] > b[0]); n } n long long int gcd(long long int a, long long int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n bool inline isPrime(long long int n) { n   if (n <= 1) return false; n   if (n <= 3) return true; n   if (n % 2 == 0 || n % 3 == 0) return false; n   for (long long int i = 5; i * i <= n; i = i + 6) n     if (n % i == 0 || n % (i + 2) == 0) return false; n   return true; n } n bool isPowerOfTwo(long long int n) { n   if (n == 0) return false; n   return (ceil(log2(n)) == floor(log2(n))); n } n bool prtv(vector<long long int> e) { n   for (auto &i : e) cout << i <<    ; n   cout <<   n ; n } n void prt(int i) { n   if (i == 1) n     cout <<  YES n ; n   else n     cout <<  NO n ; n } n int getSum(int n) { n   int sum = 0; n   while (n != 0) { n     sum = sum + n % 10; n     n = n / 10; n   } n   return sum; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int test = 1; n   while (test--) { n     set<string> hai; n     queue<string> bachi; n     long long int count = 0, k, ans = 0, n; n     cin >> n >> k; n     string s; n     cin >> s; n     bachi.push(s); n     while (!bachi.empty() && count < k) { n       string temp = bachi.front(); n       bachi.pop(); n       long long int sz = temp.size(); n       if (hai.find(temp) != hai.end()) continue; n       hai.insert(temp); n       count++; n       ans += (n - sz); n       if (sz == 1) continue; n       string foo; n       for (long long int i = (0); i < (sz); i += 1) { n         foo = temp; n         foo = foo.erase(i, 1); n         bachi.push(foo); n       } n     } n     if (count == k - 1) ans += n; n     if (count < k - 1) ans = -1; n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n using db = double; n using str = string; n using pi = pair<int, int>; n using pl = pair<ll, ll>; n using pd = pair<db, db>; n using vi = vector<int>; n using vb = vector<bool>; n using vl = vector<ll>; n using vd = vector<db>; n using vs = vector<str>; n using vpi = vector<pi>; n using vpl = vector<pl>; n using vpd = vector<pd>; n template <class T> n using V = vector<T>; n template <class T, size_t SZ> n using AR = array<T, SZ>; n template <class T> n using PR = pair<T, T>; n const int MOD = 1e9 + 7; n const int MX = 2e5 + 5; n const ll INF = 1e18; n const ld PI = acos((ld)-1); n const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; n mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); n template <class T> n using pqg = priority_queue<T, vector<T>, greater<T>>; n constexpr int pct(int x) { return __builtin_popcount(x); } n constexpr int bits(int x) { return 31 - __builtin_clz(x); } n ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } n ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); } n template <class T> n bool ckmin(T& a, const T& b) { n   return b < a ? a = b, 1 : 0; n } n template <class T> n bool ckmax(T& a, const T& b) { n   return a < b ? a = b, 1 : 0; n } n template <class T, class U> n T fstTrue(T lo, T hi, U first) { n   hi++; n   assert(lo <= hi); n   while (lo < hi) { n     T mid = lo + (hi - lo) / 2; n     first(mid) ? hi = mid : lo = mid + 1; n   } n   return lo; n } n template <class T, class U> n T lstTrue(T lo, T hi, U first) { n   lo--; n   assert(lo <= hi); n   while (lo < hi) { n     T mid = lo + (hi - lo + 1) / 2; n     first(mid) ? lo = mid : hi = mid - 1; n   } n   return lo; n } n template <class T> n void remDup(vector<T>& v) { n   sort(begin(v), end(v)); n   v.erase(unique(begin(v), end(v)), end(v)); n } n template <class T, class U> n void erase(T& t, const U& u) { n   auto it = t.find(u); n   assert(it != end(t)); n   t.erase(u); n } n template <class T> n void re(complex<T>& c); n template <class T, class U> n void re(pair<T, U>& p); n template <class T> n void re(vector<T>& v); n template <class T, size_t SZ> n void re(AR<T, SZ>& a); n template <class T> n void re(T& x) { n   cin >> x; n } n void re(db& d) { n   str t; n   re(t); n   d = stod(t); n } n void re(ld& d) { n   str t; n   re(t); n   d = stold(t); n } n template <class T, class... U> n void re(T& t, U&... u) { n   re(t); n   re(u...); n } n template <class T> n void re(complex<T>& c) { n   T a, b; n   re(a, b); n   c = {a, b}; n } n template <class T, class U> n void re(pair<T, U>& p) { n   re(p.first, p.second); n } n template <class T> n void re(vector<T>& x) { n   for (auto& a : x) re(a); n } n template <class T, size_t SZ> n void re(AR<T, SZ>& x) { n   for (auto& a : x) re(a); n } n template <class T> n void rv(int& n, vector<T>& x) { n   re(n); n   x.resize(n); n   for (auto& a : x) re(a); n } n str to_string(char c) { return str(1, c); } n str to_string(const char* second) { return (str)second; } n str to_string(str second) { return second; } n str to_string(bool b) { return to_string((int)b); } n template <class T> n str to_string(complex<T> c) { n   stringstream ss; n   ss << c; n   return ss.str(); n } n str to_string(vector<bool> v) { n   str res =  { ; n   for (int i = (0); i < ((int)(v).size()); ++i) res += char( 0  + v[i]); n   res +=  } ; n   return res; n } n template <size_t SZ> n str to_string(bitset<SZ> b) { n   str res =   ; n   for (int i = (0); i < (SZ); ++i) res += char( 0  + b[i]); n   return res; n } n template <class T, class U> n str to_string(pair<T, U> p); n template <class T> n str to_string(T v) { n   bool fst = 1; n   str res =   ; n   for (const auto& x : v) { n     if (!fst) res +=    ; n     fst = 0; n     res += to_string(x); n   } n   return res; n } n template <class T, class U> n str to_string(pair<T, U> p) { n   return to_string(p.first) +     + to_string(p.second); n } n template <class T> n void pr(T x) { n   cout << to_string(x); n } n template <class T, class... U> n void pr(const T& t, const U&... u) { n   pr(t); n   pr(u...); n } n void ps() { pr(  n ); } n template <class T, class... U> n void ps(const T& t, const U&... u) { n   pr(t); n   if (sizeof...(u)) pr(   ); n   ps(u...); n } n void DBG() { cerr <<  ]  << endl; } n template <class T, class... U> n void DBG(const T& t, const U&... u) { n   cerr << to_string(t); n   if (sizeof...(u)) cerr <<  ,  ; n   DBG(u...); n } n void setIn(str second) { freopen(second.c_str(),  r , stdin); } n void setOut(str second) { freopen(second.c_str(),  w , stdout); } n void unsyncIO() { cin.tie(0)->sync_with_stdio(0); } n void setIO(str second =   ) { n   unsyncIO(); n   if ((int)(second).size()) { n     setIn(second +  .in ), setOut(second +  .out ); n   } n } n int N; n vl A; n void solve(int tc) { n   re(N); n   A.resize(N); n   re(A); n   vl fst(N), lst(N); n   for (int i = (0); i < (N - 1); ++i) { n     ll dif = A[i] - A[i + 1]; n     if (dif >= 0) { n       fst[i] += dif; n     } else { n       lst[i + 1] -= dif; n     } n   } n   ll sum = 0; n   for (int i = (0); i < (N); ++i) sum += fst[i]; n   0; n   if (sum > A[0]) n     ps( NO ); n   else n     ps( YES ); n } n int main() { n   setIO(); n   int TC; n   re(TC); n   for (int i = (1); i < (TC + 1); ++i) solve(i); n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1000000000; n const int MAXN = 1 << 20; n const long long MOD = 1e9 + 7; n template <typename T> n void chmin(T &a, T b) { n   if (a > b) a = b; n } n template <typename T> n void chmax(T &a, T b) { n   if (a < b) a = b; n } n template <typename T> n void add(T &a, T b) { n   a += b; n   if (a < 0) a += MOD; n   if (a >= MOD) a -= MOD; n } n int in() { n   int x; n   scanf( %d , &x); n   return x; n } n long long lin() { n   long long x; n   scanf( %lld , &x); n   return x; n } n double din() { n   double x; n   scanf( %lf , &x); n   return x; n } n long long p2[64]; n void initP2() { n   p2[0] = 1; n   for (int i = (1); i <= (63); ++i) { n     p2[i] = p2[i - 1] * 2; n   } n } n int countBitsBaseB(long long n, long long b) { n   if (n == 0) return 1; n   int ret = 0; n   while (n) { n     ret++; n     n /= b; n   } n   return ret; n } n int count1(long long n) { n   int ret = 0; n   while (n) { n     ret += n & 1; n     n >>= 1; n   } n   return ret; n } n long long c[1024][1024] = {0}; n void initC() { n   for (int i = 0; i < (1024); ++i) { n     for (int j = 0; j < (i + 1); ++j) { n       if (j == 0 || j == i) { n         c[i][j] = 1; n       } else { n         c[i][j] = c[i - 1][j - 1]; n         add(c[i][j], c[i - 1][j]); n       } n     } n   } n } n void init() { n   initP2(); n   initC(); n } n void mul(int a[(1 << 8)][(1 << 8)], int b[(1 << 8)][(1 << 8)], int n) { n   int c[(1 << 8)][(1 << 8)] = {0}; n   for (int i = 0; i < (n); ++i) { n     for (int k = 0; k < (n); ++k) { n       if (a[i][k]) n         for (int j = 0; j < (n); ++j) { n           if (b[k][j]) { n             long long tmp = (long long)a[i][k] * b[k][j]; n             if (tmp >= MOD) tmp %= MOD; n             add(c[i][j], (int)tmp); n           } n         } n     } n   } n   for (int i = 0; i < (n); ++i) { n     for (int j = 0; j < (n); ++j) { n       a[i][j] = c[i][j]; n     } n   } n } n int mat[(1 << 8)][(1 << 8)]; n int ret[(1 << 8)][(1 << 8)]; n int pos(int x, int y, int n) { return x * n + y; } n int main() { n   init(); n   int n = in(); n   int f = in(); n   int m = in(); n   memset((mat), (0), sizeof((mat))); n   memset((ret), (0), sizeof((ret))); n   const int matSize = (f + 1) * p2[m]; n   for (int i = 0; i < (matSize); ++i) { n     ret[i][i] = 1; n   } n   for (int i = 0; i < (f + 1); ++i) { n     for (int j = 0; j < (p2[m]); ++j) { n       const int to = pos(i, j, p2[m]); n       int from = pos(i, (j << 1) & (p2[m] - 1), p2[m]); n       mat[from][to]++; n       if (i + 1 <= f) { n         from = pos(i + 1, ((j << 1) | 1) & (p2[m] - 1), p2[m]); n         mat[from][to] += 1 + count1(j); n       } n     } n   } n   while (n) { n     if (n & 1) { n       mul(ret, mat, matSize); n     } n     mul(mat, mat, matSize); n     n >>= 1; n   } n   long long ans = 0; n   for (int i = 0; i < (p2[m]); ++i) { n     ans += ret[pos(f, i, p2[m])][pos(0, 0, p2[m])]; n   } n   cout << ans % MOD << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[2000010] = {0}, b[2000010] = {0}; n int main() { n   long long n, tmp, k, sign = 1, mx; n   cin >> n >> k; n   mx = -1; n   for (long long i = 0; i < n + 1; ++i) { n     cin >> b[i]; n   } n   for (long long i = 0; i < n + 1000; ++i) { n     a[i] += b[i]; n     tmp = abs(a[i]); n     for (long long j = 34; j >= 1; --j) { n       if (tmp >= (1ll << j)) { n         tmp -= (1ll << j); n         if (a[i] > 0) { n           ++a[i + j]; n         } else { n           --a[i + j]; n         } n       } n     } n     if (a[i] > 0) { n       a[i] = tmp; n     } else { n       a[i] = -tmp; n     } n   } n   long long c = 0; n   for (long long i = 0; i < n + 1000; ++i) { n     if (a[i] != 0) { n       mx = i; n       sign = (a[i] > 0); n     } n   } n   if (sign) { n     for (long long i = 0; i < mx + 2; ++i) { n       if (c) { n         if (a[i] == 1) { n           a[i] = 0; n           c = 0; n         } else if (a[i] == 0) { n           a[i] = 1; n           c = 1; n         } else { n           a[i] = 0; n           c = 1; n         } n       } else { n         if (a[i] == 1) { n           a[i] = 1; n         } else if (a[i] == 0) { n           a[i] = 0; n         } else { n           a[i] = 1; n           c = 1; n         } n       } n     } n   } else { n     for (long long i = 0; i < mx + 2; ++i) { n       if (c) { n         if (a[i] == -1) { n           a[i] = 0; n           c = 0; n         } else if (a[i] == 0) { n           a[i] = 1; n           c = 1; n         } else { n           a[i] = 0; n           c = 1; n         } n       } else { n         if (a[i] == -1) { n           a[i] = 1; n         } else if (a[i] == 0) { n           a[i] = 0; n         } else { n           a[i] = 1; n           c = 1; n         } n       } n     } n   } n   for (long long i = 0; i < n + 1000; ++i) { n     if (a[i] != 0) { n       mx = i; n     } n   } n   long long bound; n   if (mx <= 60) { n     bound = 0; n   } else { n     bound = mx - 60; n   } n   long long sum = 0, cnt = 0; n   for (long long i = mx + 2; i >= 0; --i) { n     if (a[i]) { n       sum += (1ll << (i - bound)); n     } n   } n   if (!sign) { n     sum *= -1ll; n   } n   for (long long i = 0; i < n + 1; ++i) { n     if (i > 0) { n       if (a[i - 1] > 0) break; n     } n     if (i >= bound) { n       if (-sum + b[i] <= k && -sum + b[i] >= -k && n           (-sum + b[i] != 0 || i != n)) { n         ++cnt; n       } n       if (sum % 2) break; n       sum /= 2ll; n     } n   } n   cout << cnt << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int r, c; n   cin >> r >> c; n   char a[r + 1][c + 1]; n   for (int i = 0; i < r; i++) { n     for (int j = 0; j < c; j++) { n       cin >> a[i][j]; n     } n   } n   for (int i = 0; i < r; i++) { n     for (int j = 0; j < c; j++) { n       if (a[i][j] ==  . ) { n         a[i][j] =  D ; n       } n     } n   } n   for (int i = 0; i < r; i++) { n     for (int j = 0; j < c; j++) { n       if (a[i][j] ==  W ) { n         if ((a[i - 1][j] ==  S  && (i - 1) != -1) || n             (a[i + 1][j] ==  S  && (i + 1) != r) || n             (a[i][j - 1] ==  S  && (j - 1) != -1) || n             (a[i][j + 1] ==  S  && (j + 1) != c)) { n           cout <<  No  << endl; n           return 0; n         } n       } n     } n   } n   cout <<  Yes  << endl; n   for (int i = 0; i < r; i++) { n     for (int j = 0; j < c; j++) { n       cout << a[i][j]; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[105], c, x, k; n int main() { n   cin >> n; n   for (int i = 0; i < 2; i++) { n     cin >> x; n     while (x--) cin >> k, c += 1 - a[k], a[k] = 1; n   } n   cout << (c == n ?  I become the guy.  :  Oh, my keyboard! ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long i, j, k, x, y, c, t, n, m, X, Y, bada, chota; n   cin >> n; n   long long a[n]; n   for (i = (0); i < (n); i++) cin >> a[i]; n   c = 0; n   for (i = (1); i < (n); i++) { n     if (a[i] < a[i - 1]) { n       c += a[i - 1] - a[i]; n     } n   } n   cout << c << endl; n   ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void cmn(T &a, const T &b) { n   if (b < a) a = b; n } n template <class T> n inline void cmx(T &a, const T &b) { n   if (b > a) a = b; n } n const double eps = 1e-9; n const long double leps = 1e-14; n string inp; n const int MN = 1000 + 100; n int cnt[MN]; n bool mp[MN]; n bool mark[MN]; n int n, cc; n int main(int argc, char *argv[]) { n   ios_base::sync_with_stdio(false); n   cin >> inp; n   n = ((int((inp).size()))); n   for (int i = 2; i <= n; i++) n     if (mp[i] == 0) n       for (int j = 2 * i; j <= n; j += i) mp[j] = 1; n   for (int i = 0; i < n; i++) cnt[inp[i] -  a ]++; n   for (int i = 2; i <= n; i++) n     if (i == 2 || mp[i] || (!mp[i] && 2 * i <= n)) cc++, mark[i] = 1; n   bool fnd = false; n   cerr << (( cc )) <<   =   << ((cc)) <<     <<   n ; n   for (int i = 0; i < 30; i++) n     if (cnt[i] >= cc) { n       fnd = true; n       for (int j = 0; j < n; j++) n         if (mark[j + 1]) inp[j] = (i +  a ), cnt[i]--; n       break; n     } n   if (!fnd) { n     cout <<  NO  <<   n ; n     return 0; n   } n   cout <<  YES  <<   n ; n   int p = 0; n   for (int i = 0; i < n; i++) n     if (!mark[i + 1]) { n       while (cnt[p] == 0) p++; n       inp[i] = (p +  a ), cnt[p]--; n     } n   cout << inp <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 7; n int memo[2002][10][10][10][10]; n int n; n int in[2002], out[2002]; n int calc(int i, int b1, int b2, int b3, int b4, int floor) { n   if (i == n && (b1 + b2 + b3 + b4) == 0) return 0; n   int a[4], load = 0; n   memset(a, 0, sizeof a); n   if (b1 != 0) a[load++] = b1; n   if (b2 != 0) a[load++] = b2; n   if (b3 != 0) a[load++] = b3; n   if (b4 != 0) a[load++] = b4; n   sort(a, a + 4); n   int mn = INF; n   if (load == 4) { n     mn = min(mn, calc(i, 0, a[0], a[1], a[2], a[3]) + abs(floor - a[3]) + 1); n     mn = min(mn, calc(i, 0, a[1], a[2], a[3], a[0]) + abs(floor - a[0]) + 1); n     mn = min(mn, calc(i, 0, a[0], a[2], a[3], a[1]) + abs(floor - a[1]) + 1); n     mn = min(mn, calc(i, 0, a[0], a[1], a[3], a[2]) + abs(floor - a[2]) + 1); n     return mn; n   } n   if (memo[i][a[1]][a[2]][a[3]][floor] != -1) n     return memo[i][a[1]][a[2]][a[3]][floor]; n   mn = INF; n   if (a[3] > 0) n     mn = min(mn, calc(i, 0, 0, a[1], a[2], a[3]) + abs(floor - a[3]) + 1); n   if (a[2] > 0) n     mn = min(mn, calc(i, 0, 0, a[1], a[3], a[2]) + abs(floor - a[2]) + 1); n   if (a[1] > 0) n     mn = min(mn, calc(i, 0, 0, a[2], a[3], a[1]) + abs(floor - a[1]) + 1); n   if (i < n) n     mn = min(mn, calc(i + 1, out[i], a[1], a[2], a[3], in[i]) + n                      abs(floor - in[i]) + 1); n   return memo[i][a[1]][a[2]][a[3]][floor] = mn; n } n int main(void) { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   memset(memo, -1, sizeof memo); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> in[i] >> out[i]; n   cout << calc(0, 0, 0, 0, 0, 1) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool check(int n, int k, int diff, int a[]) { n   vector<bool> dp(n); n   int last = -1; n   for (int i = k - 1; i < n; i++) { n     int low = 0, high = i - k + 1; n     while (low < high) { n       int pivot = (low + high) / 2; n       if (a[i] - a[pivot] > diff) n         low = pivot + 1; n       else n         high = pivot; n     } n     if (a[i] - a[low] > diff) { n       continue; n     } n     if (low == 0) n       dp[i] = 1; n     else if (dp[low - 1] || last > low - 1) n       dp[i] = 1; n     if (dp[i - k + 1]) last = i - k + 1; n   } n   return dp[n - 1]; n } n int main() { n   int n, k; n   cin >> n >> k; n   int a[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a, a + n); n   int low = 0, high = a[n - 1] - a[0]; n   while (low < high) { n     int pivot = (low + high) / 2; n     if (check(n, k, pivot, a)) n       high = pivot; n     else n       low = pivot + 1; n   } n   cout << low << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class opr { n  public: n   int q; n   int w; n   opr *pNext; n   opr *pLost; n }; n opr *pHead = 0; n opr *pTail = 0; n opr *pN = 0; n void addvalue(int e, int r) { n   opr *p = new opr; n   if (pHead == 0) { n     p->q = e; n     p->w = r; n     p->pNext = 0; n     p->pLost = 0; n     pHead = p; n     pTail = p; n     pN = p; n   } else { n     p->q = e; n     p->w = r; n     p->pLost = pN; n     p->pNext = 0; n     pN->pNext = p; n     pN = p; n     pTail = p; n   } n } n void resilt() { n   pN = pHead; n   while (pN != 0) { n     cout << pN->q <<     << pN->w << endl; n     pN = pN->pNext; n   } n } n int main() { n   int n; n   cin >> n; n   int *old = new int[n]; n   int *nw = new int[n]; n   for (int i = 0; i < n; i++) cin >> old[i]; n   for (int i = 0; i < n; i++) cin >> nw[i]; n   int sum = 0; n   for (int i = 0; i < n; i++) { n     if (old[i] != nw[i]) { n       int k = i + 1; n       while (old[i] != nw[k]) k++; n       addvalue(k - 1 + 1, k + 1); n       int temp = nw[k]; n       nw[k] = nw[k - 1]; n       nw[k - 1] = temp; n       sum++; n       if ((k - 1) != i) i--; n     } n   } n   cout << sum << endl; n   resilt(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, t, i, nn, p[2000010], tmp, x; n long long ans; n int main() { n   scanf( %d , &n); n   nn = n; n   while (n >= 0) { n     for (t = 0; n >> t; t++) n       ; n     t--; n     x = (1 << t + 1) - 1; n     for (i = 1 << t; i <= n; i++) p[i] = x - i, p[x - i] = i; n     n = x - n - 1; n   } n   for (i = 0; i <= nn; i++) ans += i ^ p[i]; n   printf( %I64d n , ans); n   for (i = 0; i <= nn; i++) printf( %d  , p[i]); n } n


#include <bits/stdc++.h> n using namespace std; n struct IO { n   IO() { n     ios_base::sync_with_stdio(0); n     cin.tie(0); n     cout.tie(0); n   } n } _; n long long n, m, len, k, t, a, b; n long long c[101]; n long long p[101][101]; n long long dp[101][101][101]; n long long rec(long long idx, long long col, long long cnt) { n   long long &ret = dp[idx][col][cnt]; n   if (ret != -1) return ret; n   if (idx + 1 >= n) { n     if (cnt + 1 == k) n       return 0; n     else n       return 10000000000007LL; n   } else if (c[idx + 1] == 0) { n     long long minz = 10000000000007LL; n     for (long long i = 1; i <= m; i++) { n       minz = min(minz, p[idx + 1][i - 1] + rec(idx + 1, i, cnt + (i != col))); n     } n     return ret = minz; n   } else { n     return ret = rec(idx + 1, c[idx + 1], cnt + (c[idx + 1] != col)); n   } n } n int main() { n   memset(dp, -1, sizeof(dp)); n   cin >> n >> m >> k; n   for (long long i = 0; i < n; i++) cin >> c[i]; n   for (long long i = 0; i < n; i++) n     for (long long j = 0; j < m; j++) cin >> p[i][j]; n   if (c[0] != 0) { n     long long ans = rec(0, c[0], 0); n     if (ans >= 10000000000007LL) n       cout << -1 <<   n ; n     else n       cout << ans <<   n ; n     return 0; n   } n   long long minz = 10000000000007LL; n   for (long long i = 1; i <= m; i++) n     minz = min(minz, p[0][i - 1] + rec(0, i, 0)); n   if (minz >= 10000000000007LL) n     cout << -1 <<   n ; n   else n     cout << minz <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   bool f = 1; n   for (int i = 0; i < s.length(); ++i) n     if (s[i] ==  H  || s[i] ==  Q  || s[i] ==  9 ) { n       cout <<  YES  << endl; n       f = 0; n       break; n     } n   if (f) cout <<  NO  << endl; 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 = 3005; n int n, m, p, k; n vector<int> pt[maxn]; n int cnt[maxn], prv[maxn], nxt[maxn], tmp[maxn]; n long long ans, cans[maxn]; n int main() { n   scanf( %d%d%d%d , &n, &m, &p, &k); n   for (int(i) = 1; (i) <= p; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     pt[y].push_back(x); n   } n   long long ans = 0; n   for (int(i) = 1; (i) <= m; i++) { n     for (int j = i; j <= m; j++) { n       for (int(p) = 0; (p) < pt[j].size(); p++) { n         int x = pt[j][p]; n         cnt[x]++; n       } n     } n     int l = 0; n     for (int(j) = 1; (j) <= n; j++) { n       prv[j] = l; n       if (cnt[j]) l = j; n     } n     int r = n + 1; n     for (int j = n; j >= 1; j--) { n       nxt[j] = r; n       if (cnt[j]) r = j; n     } n     int cc = 0; n     for (int j = m; j >= i; j--) { n       long long ccur = 0; n       for (int(p) = 0; (p) < pt[j].size(); p++) { n         int x = pt[j][p]; n         int cur = x; n         int c = 0; n         for (int(q) = 0; (q) < k; q++) { n           if (!cur) break; n           cur = prv[cur]; n           tmp[c++] = cur; n         } n         reverse(tmp, tmp + c); n         int sz = c; n         tmp[c++] = x; n         cur = x; n         for (int(q) = 0; (q) < k; q++) { n           if (cur > n) break; n           cur = nxt[cur]; n           tmp[c++] = cur; n         } n         int r = 0, sum = 0; n         long long ttmp = 0; n         for (int(l) = 0; (l) < sz + 1; l++) { n           while (r < c && sum < k) sum += cnt[tmp[r]], r++; n           if (l && sum == k && r > sz) n             ccur += 1LL * (tmp[l] - tmp[l - 1]) * (tmp[r] - tmp[r - 1]); n           sum -= cnt[tmp[l]]; n         } n         cnt[x]--; n         if (!cnt[x]) { n           int l = prv[x], r = nxt[x]; n           nxt[l] = r; n           prv[r] = l; n         } n       } n       cans[cc++] = ccur; n     } n     long long tot = 0; n     for (int(j) = 0; (j) < cc; j++) tot += cans[j]; n     for (int(j) = 0; (j) < cc; j++) { n       ans += tot; n       tot -= cans[j]; n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 1; n int n, h, w, g[N], p[N], t[N], ansx[N], ansy[N]; n vector<int> v[3 * N]; n int main() { n   scanf( %d%d%d , &n, &w, &h); n   for (int i = 0; i < n; i++) { n     scanf( %d%d%d , g + i, p + i, t + i); n     v[p[i] - t[i] + N].push_back(i); n   } n   fill(ansx, ansx + N, w); n   fill(ansy, ansy + N, h); n   for (auto i : v) n     if (!i.empty()) { n       vector<int> x, y; n       for (auto j : i) n         if (g[j] == 2) n           y.push_back(p[j]); n         else n           x.push_back(p[j]); n       sort(x.rbegin(), x.rend()); n       sort(y.begin(), y.end()); n       sort(i.begin(), i.end(), [](int &a, int &b) { n         if (g[a] != g[b]) return g[a] == 1; n         if (g[a] == 1) return p[a] > p[b]; n         return p[a] < p[b]; n       }); n       for (int j = 0; j < y.size(); j++) ansy[i[j]] = y[j]; n       for (int j = 0; j < x.size(); j++) ansx[i[j + y.size()]] = x[j]; n     } n   for (int i = 0; i < n; i++) printf( %d %d n , ansx[i], ansy[i]); 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 T; n   cin >> T; n   while (T--) { n     int n, k; n     cin >> n >> k; n     string s; n     cin >> s; n     sort(s.begin(), s.end()); n     if (s[k - 1] != s[0]) { n       cout << s[k - 1] << endl; n     } else { n       if (s[n - 1] != s[k]) { n         string tmp =   ; n         tmp += s[0]; n         for (int i = k; i < n; i++) { n           tmp += s[i]; n         } n         cout << tmp << endl; n       } else { n         int tmp = (n - k - 1) / k + 1; n         string tmp2 =   ; n         tmp2 += s[0]; n         for (int i = 0; i < tmp; i++) { n           tmp2 += s[k]; n         } n         cout << tmp2 << endl; n       } n     } n   } n } n


#include <bits/stdc++.h> n clock_t __t = clock(); n namespace my_std { n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n template <typename T> n inline T rnd(T l, T r) { n   return uniform_int_distribution<T>(l, r)(rng); n } n template <typename T> n inline bool chkmax(T &x, T y) { n   return x < y ? x = y, 1 : 0; n } n template <typename T> n inline bool chkmin(T &x, T y) { n   return x > y ? x = y, 1 : 0; n } n template <typename T> n inline void read(T &t) { n   t = 0; n   char f = 0, ch = getchar(); n   double d = 0.1; n   while (ch >  9  || ch <  0 ) f |= (ch ==  - ), ch = getchar(); n   while (ch <=  9  && ch >=  0 ) t = t * 10 + ch - 48, ch = getchar(); n   if (ch ==  . ) { n     ch = getchar(); n     while (ch <=  9  && ch >=  0 ) t += d * (ch ^ 48), d *= 0.1, ch = getchar(); n   } n   t = (f ? -t : t); n } n template <typename T, typename... Args> n inline void read(T &t, Args &...args) { n   read(t); n   read(args...); n } n char __sr[1 << 21], __z[20]; n int __C = -1, __zz = 0; n inline void Ot() { fwrite(__sr, 1, __C + 1, stdout), __C = -1; } n inline void print(int x) { n   if (__C > 1 << 20) Ot(); n   if (x < 0) __sr[++__C] =  - , x = -x; n   while (__z[++__zz] = x % 10 + 48, x /= 10) n     ; n   while (__sr[++__C] = __z[__zz], --__zz) n     ; n   __sr[++__C] =   n ; n } n void file() {} n inline void chktime() {} n long long ksm(long long x, int y) { n   long long ret = 1; n   for (; y; y >>= 1, x = x * x) n     if (y & 1) ret = ret * x; n   return ret; n } n }  // namespace my_std n using namespace my_std; n int n, m; n char a[20][1100000]; n long long c[1100000], w[1100000]; n void FWT(long long *a) { n   long long x, y; n   for (int i = (0); i <= (n - 1); i++) n     for (int j = (0); j <= ((1 << n) - 1); j++) n       if (j >> i & 1) n         x = a[1 << i ^ j], y = a[j], a[1 << i ^ j] = x + y, a[j] = x - y; n } n void iFWT(long long *a) { n   long long x, y; n   for (int i = (0); i <= (n - 1); i++) n     for (int j = (0); j <= ((1 << n) - 1); j++) n       if (j >> i & 1) n         x = a[1 << i ^ j], y = a[j], a[1 << i ^ j] = x + y, a[j] = x - y; n   for (int j = (0); j <= ((1 << n) - 1); j++) a[j] >>= n; n } n int main() { n   file(); n   read(n, m); n   for (int i = (0); i <= (n - 1); i++) cin >> (a[i] + 1); n   for (int j = (1); j <= (m); j++) { n     int x = 0; n     for (int i = (0); i <= (n - 1); i++) x += int(a[i][j] -  0 ) << i; n     ++c[x]; n   } n   for (int i = (0); i <= ((1 << n) - 1); i++) n     w[i] = min(__builtin_popcount(i), n - __builtin_popcount(i)); n   FWT(c), FWT(w); n   for (int i = (0); i <= ((1 << n) - 1); i++) w[i] = w[i] * c[i]; n   iFWT(w); n   long long ans = n * m; n   for (int i = (0); i <= ((1 << n) - 1); i++) chkmin(ans, w[i]); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, ch, maxx, minn = 10000, k, x, m; n long long mass[65][65], temp[8][8]; n long long d1[124], d2[124]; n long long st1, en1, st2, en2; n long long p1[124], p2[124]; n long long a[10000]; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a, a + n); n   int ans = floor(n / 2); n   cout << a[ans]; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int c[3][3]; n   int MAX = -1; n   int pos = -1; n   int area = 0; n   for (int i = 0; i < 3; i++) { n     int a, b; n     cin >> a >> b; n     c[i][0] = min(a, b); n     c[i][1] = max(a, b); n     if (a > MAX) { n       MAX = a; n       pos = i; n     } n     if (b > MAX) { n       MAX = b; n       pos = i; n     } n     area += a * b; n   } n   if (area != MAX * MAX) { n     printf( -1 n ); n     return 0; n   } else { n     printf( %d n , MAX); n     for (int i = 0; i < c[pos][0]; i++) { n       for (int i = 0; i < c[pos][1]; i++) { n         printf( %c ,  A  + pos); n       } n       printf(  n ); n     } n     bool ok = false; n     for (int i = 0; i < 3 && !ok; i++) { n       if (i == pos) n         continue; n       else if (c[i][1] == c[pos][1]) { n         for (int j = 0; j < c[i][0]; ++j) { n           for (int k = 0; k < c[i][1]; ++k) { n             printf( %c ,  A  + i); n           } n           printf(  n ); n         } n       } else { n         int j2, k2; n         if (c[i][0] + c[pos][0] == c[pos][1]) { n           j2 = c[i][0]; n           k2 = c[i][1]; n         } else { n           j2 = c[i][1]; n           k2 = c[i][0]; n         } n         for (int j = 0; j < j2; ++j) { n           int k; n           for (k = 0; k < k2; ++k) { n             printf( %c ,  A  + i); n           } n           for (k; k < MAX; k++) { n             printf( %c ,  A  + (3 - pos - i)); n           } n           printf(  n ); n           ok = true; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1 << 30; n typedef struct { n   long long x, y; n } point; n priority_queue<pair<int, int>, vector<pair<int, int> >, n                greater<pair<int, int> > > n     pq; n vector<pair<pair<int, int>, int> > vpp; n vector<pair<int, int> > vp; n const int N = 1e5 + 5; n const int seg = sqrt(N) + 1; n int main() { n   int n, k; n   cin >> n >> k; n   vector<pair<int, int> > a(n); n   vector<int> vi(n, 10); n   for (int i = 0; i < n; i++) { n     cin >> a[i].first; n     a[i].second = i; n   } n   sort(a.rbegin(), a.rend()); n   queue<int> q; n   for (int i = 0; i < n; i++) { n     q.push(a[i].second); n   } n   set<int> idx; n   for (int i = 0; i < n; ++i) { n     idx.insert(i); n   } n   string ans(n,  c ); n   int who = 0; n   while (!idx.empty()) { n     while (!idx.count(q.front())) { n       q.pop(); n     } n     int pos = q.front(); n     q.pop(); n     vector<int> add; n     auto it = idx.find(pos); n     for (int i = 0; i <= k; i++) { n       add.push_back(*it); n       if (it == idx.begin()) break; n       it--; n     } n     it = next(idx.find(pos)); n     for (int i = 0; i < k; i++) { n       if (it == idx.end()) break; n       add.push_back(*it); n       it++; n     } n     for (auto it : add) { n       idx.erase(it); n       ans[it] =  1  + who; n     } n     who ^= 1; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 200005; n long long n, has; n long long num[maxn], rk[maxn]; n long long a[maxn]; n signed main() { n   long long k, j, i; n   scanf( %lld , &n); n   for (k = 1; k <= n; k++) scanf( %lld , &a[k]); n   rk[0] = 1; n   num[1] = 0; n   has = 1; n   for (k = 1; k <= n; k++) { n     if (rk[a[k]]) n       rk[k] = rk[a[k]], rk[a[k]] = 0, num[rk[k]] = k; n     else { n       num[++has] = k, rk[k] = has; n     } n   } n   cout << has << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 4010; n const long long mod = 1e9 + 9; n long long f[maxn * 2][maxn], n, w, b; n long long num[maxn * 2], inv[maxn * 2]; n long long pp(long long a, long long b) { n   if (!b) return 1; n   long long cnt = pp(a, b / 2); n   cnt = 1ll * cnt * cnt % mod; n   if (b & 1) cnt = 1ll * cnt * a % mod; n   return cnt; n } n long long calc(long long n, long long m) { n   if (n < m) return 0; n   return 1ll * num[n] * num[n - 1] % mod * inv[m - 1] % mod * inv[n - m] % mod; n } n int main() { n   cin >> n >> w >> b; n   f[1][1] = 1; n   for (long long i = 2; i <= w + b; ++i) n     for (long long j = 1; j <= min(n, i); ++j) n       f[i][j] = (1ll * f[i - 1][j] * j + f[i - 1][j - 1]) % mod; n   num[0] = 1; n   for (long long i = 1; i <= w + b; ++i) num[i] = 1ll * num[i - 1] * i % mod; n   inv[w + b] = pp(num[w + b], mod - 2); n   for (long long i = w + b - 1; i >= 0; --i) n     inv[i] = 1ll * inv[i + 1] * (i + 1) % mod; n   long long ans = 0; n   for (long long i = 1; i < n - 1; ++i) { n     long long cur = n - i - 1; n     cur = 1ll * cur * calc(w, n - i) % mod * calc(b, i) % mod; n     (ans += cur) %= mod; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 123456; n int s[N]; n int main() { n   int n, k, ans = 0; n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n; ++i) { n     scanf( %d , s + i); n   } n   for (int i = 0; i < n - k; ++i) { n     ans = max(ans, s[i] + s[(n - k) * 2 - i - 1]); n   } n   printf( %d n , max(ans, s[n - 1])); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Edge { n   int from, to; n   long long capacity, cost; n   Edge(int from, int to, long long capacity, long long cost) { n     this->from = from; n     this->to = to; n     this->capacity = capacity; n     this->cost = cost; n   } n }; n vector<vector<int>> adj; n vector<vector<long long>> cost, capacity; n const long long INF = 1e9; n void shortest_paths(int n, int v0, vector<long long>& d, vector<int>& p) { n   d.assign(n, INF); n   d[v0] = 0; n   vector<int> m(n, 2); n   deque<int> q; n   q.push_back(v0); n   p.assign(n, -1); n   while (!q.empty()) { n     int u = q.front(); n     q.pop_front(); n     m[u] = 0; n     for (int v : adj[u]) { n       if (capacity[u][v] > 0 && d[v] > d[u] + cost[u][v]) { n         d[v] = d[u] + cost[u][v]; n         p[v] = u; n         if (m[v] == 2) { n           m[v] = 1; n           q.push_back(v); n         } else if (m[v] == 0) { n           m[v] = 1; n           q.push_front(v); n         } n       } n     } n   } n } n long long min_cost_flow(int N, vector<Edge> edges, long long K, int s, int t) { n   adj.assign(N, vector<int>()); n   cost.assign(N, vector<long long>(N, 0)); n   capacity.assign(N, vector<long long>(N, 0)); n   for (Edge e : edges) { n     adj[e.from].push_back(e.to); n     adj[e.to].push_back(e.from); n     cost[e.from][e.to] = e.cost; n     cost[e.to][e.from] = -e.cost; n     capacity[e.from][e.to] = e.capacity; n   } n   long long flow = 0; n   long long cost = 0; n   vector<int> p; n   vector<long long> d; n   while (flow < K) { n     shortest_paths(N, s, d, p); n     if (d[t] == INF) break; n     long long f = K - flow; n     int cur = t; n     while (cur != s) { n       f = min(f, capacity[p[cur]][cur]); n       cur = p[cur]; n     } n     flow += f; n     cost += f * d[t]; n     cur = t; n     while (cur != s) { n       capacity[p[cur]][cur] -= f; n       capacity[cur][p[cur]] += f; n       cur = p[cur]; n     } n   } n   if (flow < K) n     return -1; n   else n     return cost; n } n string s, x[105]; n int y, val[256], n, temp[256]; n vector<Edge> v; n int main() { n   cin >> s; n   for (int i = 0; i < s.size(); i++) { n     val[s[i] -  a ]++; n   } n   scanf( %d , &n); n   for (int i = 0; i < 26; i++) { n     v.push_back(Edge(n + 1 + i, n + 1 + 26, val[i], 0)); n   } n   for (int i = 1; i <= n; i++) { n     cin >> x[i] >> y; n     v.push_back(Edge(0, i, y, i)); n     for (int j = 0; j < x[i].size(); j++) { n       temp[x[i][j] -  a ]++; n     } n     for (int j = 0; j < 26; j++) { n       v.push_back(Edge(i, n + 1 + j, temp[j], 0)); n       temp[j] = 0; n     } n   } n   printf( %lld n , min_cost_flow(n + 26 + 2, v, s.size(), 0, n + 26 + 1)); n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:64000000 ) n using std::cerr; n using std::cin; n using std::cos; n using std::cout; n using std::endl; n using std::make_pair; n using std::max; n using std::min; n using std::next_permutation; n using std::pair; n using std::priority_queue; n using std::queue; n using std::random_shuffle; n using std::set; n using std::sin; n using std::sort; n using std::sqrt; n using std::stack; n using std::string; n using std::stringstream; n using std::swap; n using std::vector; n const long double PI = 3.14159265358979323846; n int main() { n   int n, d, s; n   long long sum = 0; n   int add = 0; n   cin >> n >> d >> s; n   vector<pair<int, int> > can, ne; n   for (int i = 0; i < n; ++i) { n     int c, l, f; n     cin >> c >> f >> l; n     sum += c; n     pair<int, int> p(f, c); n     if (l < d) { n       ne.push_back(p); n       if (c) ++add; n     } else n       can.push_back(p); n   } n   sort(can.begin(), can.end()); n   int mr = 0; n   long long fuel = 0; n   while ((mr < can.size()) && (fuel + can[mr].first <= s)) { n     fuel += can[mr].first; n     ++mr; n   } n   vector<pair<int, int> > cacan, necan; n   for (int i = 0; i < can.size(); ++i) { n     if (can[i].second) n       cacan.push_back(can[i]); n     else n       necan.push_back(can[i]); n   } n   vector<long long> cacans(cacan.size()); n   for (int i = 0; i < cacan.size(); ++i) { n     if (i) n       cacans[i] = cacans[i - 1] + cacan[i].first; n     else n       cacans[i] = cacan[i].first; n   } n   for (int k = 1; k <= cacan.size(); ++k) { n     long long free = sum - (int)(cacan.size()) + k - add; n     long long fu = cacans[k - 1]; n     if (fu > s) continue; n     int num = necan.size() + ne.size() - add; n     if (num <= free) { n       if (mr < n) { n         mr = n; n         fuel = fu; n       } else if (mr == n) { n         if (fuel > fu) fuel = fu; n       } n       continue; n     } n     num -= free; n     int mmr = n - num; n     for (int i = 0; i < min(num, (int)necan.size()); ++i) { n       if (fu + necan[i].first <= s) { n         ++mmr; n         fu += necan[i].first; n       } else n         break; n     } n     if (mmr > mr) { n       mr = mmr; n       fuel = fu; n     } else if (mmr == mr) n       if (fuel > fu) fuel = fu; n   } n   cout << mr <<     << fuel << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int maxN = 1000005, p = 998244353; n int n, ans, fac[maxN], inv[maxN]; n inline int power(int x, long long y) { n   int res = 1; n   for (; y; y >>= 1, x = (long long)x * x % p) { n     if (y & 1) { n       res = (long long)res * x % p; n     } n   } n   return res; n } n inline int binomial(int x, int y) { n   return (long long)fac[x] * inv[y] % p * inv[x - y] % p; n } n inline long long sign(int x) { return x & 1 ? -1 : 1; } n int main() { n   scanf( %d , &n); n   for (int i = fac[0] = 1; i <= n; i++) { n     fac[i] = (long long)fac[i - 1] * i % p; n   } n   inv[n] = power(fac[n], p - 2); n   for (int i = n; i; i--) { n     inv[i - 1] = (long long)inv[i] * i % p; n   } n   for (int i = 1; i <= n; i++) { n     ans = (ans + sign(i) * binomial(n, i) * n                      power(3, (long long)n * (n - i) + i) * 2) % n           p; n   } n   for (int i = 0; i < n; i++) { n     ans = (ans + sign(n - i) * binomial(n, i) * n                      (power(power(3, i) - 1, n) - power(3, (long long)n * i)) * n                      3) % n           p; n   } n   printf( %d n , (p - ans) % p); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s, t; n   cin >> s; n   cin >> t; n   int i = 0, j = 0; n   while (j < t.size()) { n     if (s[i] == t[j]) i++; n     j++; n   } n   cout << i + 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n multimap<int, string> MM; n long long int BinSearch(long long int a[], vector<long long int>& tVec, n                         long long int x, long long int y, long long int v) { n   while (y > x + 1) { n     long long int m = x + (y - x) / 2; n     if (a[tVec[m]] >= v) n       y = m; n     else n       x = m; n   } n   return y; n } n const int M = 1e9 + 7; n long long mod(long long x) { return ((x % M + M) % M); } n long long add(long long a, long long b) { return mod(mod(a) + mod(b)); } n long long mul(long long a, long long b) { return mod(mod(a) * mod(b)); } n long long int Mod = 1000000007; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int n, i; n   cin >> n; n   vector<long long int> res; n   while (n > 0) { n     long long int num = n, m = 0, p = 1; n     while (num > 0) { n       if (num % 10 > 0) m = m + p; n       p = p * 10; n       num = num / 10; n     } n     res.push_back(m); n     n = n - m; n   } n   cout << res.size() <<   n ; n   for (i = 0; i < res.size(); i++) cout << res[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string convertTostr(int x) { n   stringstream ss; n   ss << x; n   string st; n   st = ss.str(); n   return st; n } n unsigned long long convertToint(string y) { n   unsigned long long num; n   stringstream ss(y); n   ss >> num; n   return num; n } n unsigned long long fpow(unsigned long long b, unsigned long long p, n                         unsigned long long m) { n   if (p == 0) return 1; n   if (p == 1) return b % m; n   unsigned long long res = fpow(b, p / 2, m) % m; n   res = (res * res) % m; n   if (p % 2 != 0) res = (res * (b % m)) % m; n   return res; n } n int main() { n   std::cin.tie(0); n   std::ios::sync_with_stdio(false); n   int n; n   cin >> n; n   vector<pair<int, int> > vec; n   int x, y; n   for (int i = 0; i < n; i++) { n     cin >> x >> y; n     vec.push_back(make_pair(x, y)); n   } n   int cnt = 0; n   for (int i = 0; i < n - 2; i++) { n     int curX = vec[i].first, curY = vec[i].second; n     int nX = vec[i + 1].first, nY = vec[i + 1].second; n     int aX = vec[i + 2].first, aY = vec[i + 2].second; n     if ((nX > curX && aY > nY) || (nX < curX && aY < nY) || n         (nY > curY && aX < nX) || (nY < curY && aX > nX)) n       cnt++; n   } n   cout << cnt; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1 << 30; n const int N = 5e4 + 10; n int y[N]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , y + i); n   } n   int low = 1, high = 1e9; n   while (low != high) { n     int mid = (low + high) / 2; n     unordered_set<int> A; n     bool found = false; n     for (int i = 0; i < n; ++i) { n       int cur = y[i]; n       while (cur > mid) cur >>= 1; n       while (cur > 1 && A.find(cur) != A.end()) cur >>= 1; n       if (A.find(cur) == A.end()) { n         A.insert(cur); n       } else { n         found = true; n         break; n       } n     } n     if (found) { n       low = mid + 1; n     } else { n       high = mid; n     } n   } n   unordered_set<int> A; n   for (int i = 0; i < n; ++i) { n     int cur = y[i]; n     while (cur > high) cur >>= 1; n     while (cur > 1 && A.find(cur) != A.end()) cur >>= 1; n     if (A.find(cur) == A.end()) { n       A.insert(cur); n       printf( %d%c , cur,    n [i == n - 1]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, j, k = 0, l = 0, n; n   int a[1001], b[10001]; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (i = 0; i < n; i++) { n     cin >> b[i]; n   } n   for (i = 0; i < n; i++) { n     k = k | a[i]; n   } n   for (i = 0; i < n; i++) { n     l = l | b[i]; n   } n   cout << k + l <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void debug(int a[], int n) { n   cout <<  Array: [  ; n   for (auto i = 0; i != n; i++) cout << a[i] <<    ; n   cout <<  ] n ; n } n void debugv(vector<int>& v) { n   cout <<  Vector: [  ; n   for (auto i = 0; i != v.size(); i++) cout << v[i] <<    ; n   cout <<  ] n ; n } n bool comp(pair<long long int, long long int> a, n           pair<long long int, long long int> b) { n   if (a.first < b.first || (a.first == b.first && a.second >= b.second)) n     return true; n   return false; n } n long long int k[100000 + 2]; n pair<long long int, long long int> power[100000 + 2]; n void Solve() { n   long long int n; n   cin >> n; n   long long int ans = 0, temp; n   for (auto i = 0; i != n; i++) { n     cin >> k[i]; n     long long int ma = 0; n     for (auto j = 0; j != k[i]; j++) { n       long long int p; n       cin >> p; n       ma = max(ma, p + 1 - j); n     } n     power[i].first = ma, power[i].second = k[i]; n   } n   sort(power, power + n); n   temp = ans = power[0].first; n   for (auto i = 0; i != n; i++) { n     if (temp >= power[i].first) { n       temp += power[i].second; n     } else { n       ans += (power[i].first - temp); n       temp = power[i].first + power[i].second; n     } n   } n   cout << ans <<   n ; n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int T; n   cin >> T; n   while (T--) { n     Solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const unsigned long long inf = 0x3f3f3f3f3f3f3f3f; n const unsigned long long MAXN = 1e5 + 5; n unsigned long long cnt[MAXN]; n vector<unsigned long long> adj[MAXN]; n vector<unsigned long long> vis(MAXN, 0); n void dfs(unsigned long long v) { n   vis[v] = 1; n   if (adj[v].size() == 1 && v != 1) { n     cnt[v] = 1; n     return; n   } n   for (auto u : adj[v]) { n     if (!vis[u]) { n       dfs(u); n       cnt[v] += cnt[u]; n     } n   } n   ++cnt[v]; n } n unsigned long long nc2(unsigned long long n) { n   if (n < 2) return 0; n   return (n * (n - 1)) / 2; n } n long double nc3(unsigned long long n) { n   if (n < 3) return 0; n   return ((long double)n * (n - 1) * (n - 2)) / 6.0; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   unsigned long long n; n   cin >> n; n   pair<pair<unsigned long long, unsigned long long>, unsigned long long> cst[n]; n   for (unsigned long long i = 0; i < n - 1; i++) { n     unsigned long long u, v, l; n     cin >> u >> v >> l; n     adj[v].push_back(u); n     adj[u].push_back(v); n     cst[i + 1] = {{u, v}, l}; n   } n   dfs(1); n   long double ini = 0; n   long double wgt[n]; n   fill(wgt, wgt + n, 0); n   long double ttl = nc3(n); n   for (unsigned long long i = 1; i < n; i++) { n     unsigned long long u = cst[i].first.first, v = cst[i].first.second; n     unsigned long long lft = min(cnt[u], cnt[v]); n     unsigned long long rgt = n - lft; n     wgt[i] += (long double)lft * nc2(rgt) + rgt * nc2(lft); n     wgt[i] *= 2; n     wgt[i] /= ttl; n     ini += wgt[i] * cst[i].second; n   } n   unsigned long long q; n   cin >> q; n   cout << fixed << setprecision(12); n   while (q--) { n     unsigned long long idx, x; n     cin >> idx >> x; n     ini -= wgt[idx] * (cst[idx].second - x); n     cst[idx].second = x; n     long double d = ini; n     cout << d <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const int INF = 1000 * 1000 * 1000 + 7; n const long long LINF = INF * (long long)INF; n const int MOD = 1000 * 1000 * 1000 + 7; n const int MAX = 1000 * 1000 + 47; n long long getN(long long a) { n   long long res = a; n   while (a) { n     res = res * 10 + a % 10; n     a /= 10; n   } n   return res; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int k, p; n   cin >> k >> p; n   long long res = 0; n   for (int i = (1); i < (k + 1); i++) { n     res = (res + getN(i)) % p; n   } n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e5 + 10; n int n, u, v, dp[maxn], d[2]; n long long f[maxn], ans, sum; n vector<int> a[maxn]; n void dfs(int nod, int pa, int x) { n   d[x]++; n   for (int i : a[nod]) { n     if (i != pa) { n       dfs(i, nod, x ^ 1); n       dp[nod] += dp[i]; n       f[nod] += (f[i] + dp[i]); n     } n   } n   for (int i : a[nod]) { n     if (i != pa) { n       ans += (f[nod] - f[i] - dp[i]) * dp[i]; n     } n   } n   ans += f[nod]; n   dp[nod]++; n   return; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     scanf( %d%d , &u, &v); n     a[u].push_back(v); n     a[v].push_back(u); n   } n   dfs(1, 0, 0); n   printf( %lld n , (ans + (1ll * d[0] * d[1])) / 2); n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   cin >> n; n   int a[n]; n   int b[n]; n   string s; n   cin >> s; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  a ) { n       a[i] = 1; n       b[i] = 0; n     } else { n       a[i] = 0; n       b[i] = 1; n     } n     if (i) { n       a[i] += a[i - 1]; n       b[i] += b[i - 1]; n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       if ((i == 0 && b[j] == a[j]) || n           (i != 0 && ((b[j] - b[i - 1]) == (a[j] - a[i - 1])))) { n         cout << i + 1 <<     << j + 1 <<   n ; n         return; n       } n     } n   } n   cout <<  -1 -1 n ; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int N; n   cin >> N; n   deque<int> D; n   for (int i = 1; i <= N; ++i) D.push_back(i); n   for (int k = 2; k <= N; ++k) { n     int x = D.front(); n     D.pop_front(); n     for (int i = k - 1; i < N - 1; i += k) swap(x, D[i]); n     D.push_back(x); n   } n   for (int i = 0; i < N; ++i) cout << D[i] <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int i, cnt1 = 0, l, diff, a, c, fck; n   scanf( %d%d , &a, &c); n   l = a; n   diff = 0; n   while (l > 0) { n     if (l % 3) diff += (3 - (l % 3)) * pow(3, cnt1); n     l /= 3; n     cnt1++; n   } n   cnt1 = 0; n   l = 0; n   while (c || diff) { n     fck = c + diff; n     l += fck % 3 * pow(3, cnt1); n     cnt1++; n     c /= 3; n     diff /= 3; n   } n   printf( %d , l); n } n


#include <bits/stdc++.h> n using namespace std; n typedef long long ll; n  n  n pair<int, int> intersect(pair<int, int> a, pair<int, int> b) n { n     vector<pair<int, int>> v; n     v.push_back(a); n     v.push_back(b); n     sort(v.begin(), v.end()); n     a = v[0]; n     b = v[1]; n  n     if(a.first >= b.first && a.second <= b.second) return a; n     if(b.first >= a.first && b.second <= a.second) return b; n  n     if(a.second >= b.first) n     { n         return {b.first, a.second}; n     } n  n     return {1, 0}; n } n  n int main() { n     cin.tie(0); n     cin.sync_with_stdio(0); n  n  n     int t; cin>>t; n     while(t--) n     { n         int n, m; cin>>n>>m; n         string s; cin>>s; n  n         pair<int, int> L[n], R[n]; n         int X[n]; n         int mn = 0, mx = 0; n         int x = 0; n         for(int i = 0; i < n; i++) n         { n             if(s[i] ==  + ) n             { n                 x++; n                 mx = max(mx, x); n             } n             else n             { n                 x--; n                 mn = min(mn, x); n             } n  n             L[i] = {mn, mx}; n             X[i] = x; n         } n  n         mn = 0, mx = 0; n         for(int i = n - 1; i >= 0; i--) n         { n             if(s[i] ==  + ) n             { n                 mx++; n                 mn++; n             } n             else n             { n                 mx--; n                 mn--; n             } n  n             mx = max(mx, 0); n             mn = min(0, mn); n             R[i] = {mn, mx}; n         } n  n         while(m--) n         { n             int l, r; n             cin>>l>>r; n  n             --l; n             --r; n  n  n             if(!l && r == n - 1) n             { n                 cout<<1<<  n ; n                 continue; n             } n             if(!l) n             { n                 cout<<R[r + 1].second - R[r + 1].first + 1<<  n ; n             } n             else if(r == n - 1) n             { n                 cout<<L[l - 1].second - L[l - 1].first + 1<<  n ; n             } n             else n             { n                 int x = X[l - 1]; n                 pair<int, int> before = L[l - 1]; n                 pair<int, int> after  = R[r + 1]; n  n                 after.first  += x; n                 after.second += x; n  n                 int out = before.second - before.first + 1 + after.second - after.first + 1; n                 pair<int, int> sec = intersect(before, after); n                 out -= sec.second - sec.first + 1; n  n                 cout<<out<<  n ; n             } n         } n     } n } n


#include <algorithm>   n #include <iostream>   n #include <sstream>   n #include <string>   n #include <vector>   n #include <queue>   n #include <set>   n #include <map>   n #include <cstdio>   n #include <cstdlib>   n #include <cctype>   n #include <cmath>   n #include <cstring> n #include <list>   n #include <cassert> n #include <climits> n #include <bitset> n #include <chrono> n #include <random> n using namespace std; n  n #define PB push_back   n #define MP make_pair   n #define SZ(v) ((int)(v).size())   n #define FOR(i,a,b) for(int i=(a);i<(b);++i)   n #define REP(i,n) FOR(i,0,n)   n #define FORE(i,a,b) for(int i=(a);i<=(b);++i)   n #define REPE(i,n) FORE(i,0,n)   n #define FORSZ(i,a,v) FOR(i,a,SZ(v))   n #define REPSZ(i,v) REP(i,SZ(v))   n std::mt19937 rnd((int)std::chrono::steady_clock::now().time_since_epoch().count()); n typedef long long ll; n ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); } n  n const int MAXN = 50; n const int MOD = 998244353; n void inc(int& a, int b) { if ((a += b) >= MOD) a -= MOD; } n  n int n; n int firstval; n int deltaval[MAXN]; n ll anslen; int answays; n  n ll segfrom[MAXN], segto[MAXN]; n vector<ll> special; n  n vector<int> path; n vector<vector<int>> C; n vector<int> inv; n  n int choose(ll n, int k) { n  int ret = 1; n  REP(i, k) ret = (ll)ret * ((n - i) % MOD) % MOD * inv[i + 1] % MOD; n  return ret; n } n  n int calctrans(int s, int t) { n  int npos = 0; n  FOR(i, s, t) if (path[i] == path[s] && path[i + 1] == path[t]) ++npos; n  int nneg = npos - 1; n  ll between = special[path[t]] - special[path[s]] - 1; n  int nbar = npos + nneg - 1; n  //printf(  tnpos=%d nneg=%d between=%lld n , npos, nneg, between); n  int ret = 0; n  REPE(i, nneg) { // #times neg more than 1 n   ll rem = between - 2 * i; n   if (rem < 0) continue; n   int ways = (ll)choose(rem + nbar, nbar) * C[nneg][i] % MOD; n   //printf(  t%d: %d (rem=%lld) n , i, ways, rem); n   if (i % 2 == 0) inc(ret, ways); else inc(ret, MOD - ways); n  } n  //printf(  t->%d n , ret); n  return ret; n } n  n  n void solve() { n  REP(i, n) { n   segfrom[i] = i == 0 ? firstval : segto[i - 1]; n   segto[i] = segfrom[i] + deltaval[i]; n  } n  special.clear(); n  REP(i, n) special.PB(segfrom[i]), special.PB(segto[i]); n  sort(special.begin(), special.end()); n  special.erase(unique(special.begin(), special.end()), special.end()); n  //printf( special: ); REPSZ(i, special) printf(  %lld , special[i]); puts(  ); n  n  path.clear(); n  REP(i, n) { n   int fromidx = lower_bound(special.begin(), special.end(), segfrom[i]) - special.begin(); n   int toidx = lower_bound(special.begin(), special.end(), segto[i]) - special.begin(); n   if (i == 0) path.PB(fromidx); n   if (fromidx < toidx) FORE(j, fromidx + 1, toidx) path.PB(j); n   if (fromidx > toidx) for (int j = fromidx - 1; j >= toidx; --j) path.PB(j); n  } n  //printf( path: ); REPSZ(i, path) printf(  %d , path[i]); puts(  ); n  //printf( path: ); REPSZ(i, path) printf(  %lld , special[path[i]]); puts(  ); n  n  bool desc = true; n  FORSZ(i, 1, path) if (path[i] > path[i - 1]) desc = false; n  if (desc) { anslen = 1; answays = (special[path[0]] - special[path.back()] + 1) % MOD; return; } n  n  anslen = 0; n  REPSZ(i, path) FORSZ(j, i + 1, path) if (path[i] < path[j]) anslen = max(anslen, special[path[j]] - special[path[i]] + 1); n  assert(anslen > 0); n  n  C = vector<vector<int>>(SZ(path) + 1, vector<int>(SZ(path) + 1)); n  REPSZ(i, C) { C[i][0] = C[i][i] = 1; FOR(j, 1, i) C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % MOD; } n  inv = vector<int>(SZ(path) + 1); n  inv[1] = 1; FORSZ(i, 2, inv) inv[i] = (ll)(MOD - MOD / i) * inv[MOD % i] % MOD; n  n  answays = 0; n  vector<vector<int>> trans(SZ(path), vector<int>(SZ(path), 0)); n  REPSZ(i, path) FORSZ(j, i + 1, path) if (path[j] == path[i] + 1) trans[i][j] = calctrans(i, j); n  REPSZ(i, special) { n   vector<int> dp(SZ(path), 0); n   REPSZ(j, path) if (path[j] == i) inc(dp[j], 1); n   REPSZ(j, path) FORSZ(k, j + 1, path) dp[k] = (dp[k] + (ll)dp[j] * trans[j][k]) % MOD; n   REPSZ(j, path) if (special[path[j]] - special[i] + 1 == anslen) inc(answays, dp[j]); n  } n } n  n void run() { n  scanf( %d , &n); n  scanf( %d , &firstval); n  REP(i, n) scanf( %d , &deltaval[i]); n  solve(); n  printf( %lld %d n , anslen, answays); n } n  n int main() { n  run(); n  return 0; n } n


#include <bits/stdc++.h> n const int N = 200100; n using namespace std; n vector<long long int> v[N]; n long long int n, down[N], up[N], k, s[N], a[N]; n long long int ans; n void f(long long int x, long long int p) { n   s[x] = a[x]; n   for (int i = 0; i < v[x].size(); i++) { n     long long int y = v[x][i]; n     if (y == p) continue; n     f(y, x); n     s[x] += s[y]; n     down[x] += down[y] + s[y]; n   } n } n void dfs(long long int x, long long int p) { n   long long int mx = k + k - s[x]; n   for (int i = 0; i < v[x].size(); i++) { n     long long int y = v[x][i]; n     if (y == p) continue; n     up[y] = up[x] + down[x] - down[y] - s[y] + k + k - s[y]; n     dfs(y, x); n     mx = max(mx, s[y]); n   } n   if (mx <= k) ans = max(ans, up[x] + down[x]); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> k; n   for (int i = 1; i <= k + k; i++) { n     long long int x; n     cin >> x; n     a[x] = 1; n   } n   for (int i = 1; i < n; i++) { n     long long int x, y; n     cin >> x >> y; n     v[x].push_back(y); n     v[y].push_back(x); n   } n   f(1, -1); n   dfs(1, -1); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n  n typedef long long ll; n  n using namespace std; n  n int main(){ n     ios::sync_with_stdio(0); n     cin.tie(0); n  n     ll t; n     cin >> t; n     while(t--){ n         ll n; n         cin >> n; n         ll x[n]; n         for(ll i=0; i<n; i++){ n             cin >> x[i]; n         } n         vector<ll> v; n         for(ll i=0; i<n; i++){ n             for(ll j=0; j<n; j++){ n                 if(i != j && find(v.begin(), v.end(), abs(x[i]-x[j])) == v.end()){ n                     v.push_back(abs(x[i]-x[j])); n                 } n             } n         } n         cout << v.size() <<   n ; n     } n } n  n  n


#include<bits/stdc++.h> n #define fo(i,a,b) for(int i=a;i<=b;i++) n #define gc (p1==p2&&(p2=(p1=fr)+fread(fr,1,1<<20,stdin))==p1?EOF:*p1++) n #define fl fwrite(fw,1,p3-fw,stdout),p3=fw n char ch,fr[1<<21],*p1=fr,*p2=fr,fw[1<<21],*p3=fw; n void rd(int&x) n { n  while(!isdigit(ch=gc));x=ch-48; n  while( isdigit(ch=gc)) x=ch-48+x*10; n } n void pc(char c) {*p3++=c; if(p3-fw==1<<20)fl;} n void pt(int x) {if(x>9)pt(x/10); pc(x%10+48);} n using namespace std; n typedef pair<int,int>P; n const int N=5e5+9; n int T,n,m,X[N],Y[N],x,y,n1,c[N],sz[N],deg[N],t,d[N]; n set<int>r,e[N],E[N]; n struct Comp{bool operator()(const int&x,const int&y){return deg[x]>deg[y];}}; n multiset<int,Comp>_; n multiset<int,Comp>::iterator it; n #define fi first n #define se second n P a[N],w[N]; n void dg(int x) n { n  int y; n  for(set<int>::iterator it=r.begin();it!=r.end();) n   if(e[x].find(y=*it)==e[x].end()) n   { n    E[x].insert(y); n    E[y].insert(x); n    r.erase(it); dg(y); n    it=r.lower_bound(y); n   } n   else it++; n } n int main() n { n  for(rd(T);T--;) n  { n   rd(n),rd(m); _.clear(); n   fo(i,1,n) e[i].clear(), E[i].clear(), deg[i]=c[i]=sz[i]=0; n   fo(i,1,m) n   { n    rd(x),rd(y); deg[x]++,deg[y]++; X[i]=x,Y[i]=y; n    e[x].insert(y); n    e[y].insert(x); n   } n   fo(i,1,n) _.insert(i); n   n1=n; n //  n1=_.size(); n   while(!_.empty()) n   { n    if(deg[x=*_.begin()]<n1-1) break; n    _.erase(_.find(x)), n1--; n    for(int y:e[x]) n    { n     _.erase(_.find(y)); n     e[y].erase(x); deg[y]--; _.insert(y); n    } n   } n //  n1=_.size(); n   if(!n1) goto DW; n   r.clear(); n   r.insert(_.begin(),_.end()); n   for(int x:_) if(r.find(x)!=r.end()) r.erase(x),dg(x); n   if(!r.empty()) n   { n    puts( cyka blyat! ); n    printf( %d %d n ,n,m); n    fo(i,1,m) printf( %d %d n ,X[i],Y[i]); n    return 0; n   } n   for(int x:_)  n   if(!c[x]) n   { n    UP:for(int y:E[x]) if(!c[y]) d[++t]=y; n    if(t) n    { n     for(sz[c[x]=x]=t+1;t;t--) c[d[t]]=x; n     continue; n    } n    y=*E[x].begin(); n    if(sz[c[y]]==2) {c[c[y]]=sz[c[y]]=0; x=y; goto UP;} n    else --sz[c[y]], sz[c[y]=c[x]=x]=2; n   } n   DW:int nw=0; n   for(int x:_) if(sz[x]) w[x]=a[x]=P(nw+sz[x],nw+1), nw+=sz[x]; n   for(int x:_) if((y=c[x])^x) w[y]=a[x]=P(w[y].fi-1,w[y].se==a[y].se?a[y].fi:w[y].se-1); n //  n1=_.size(); n   fo(i,1,n) if(_.find(i)==_.end()) ++nw,a[i]=P(nw,nw); n   fo(i,1,n) pt(a[i].fi),pc(   ); n   pc(  n ); n   fo(i,1,n) pt(a[i].se),pc(   ); n   pc(  n ); n  } n  fl; n }


#include <bits/stdc++.h> n const int Max_N = 100005, Mod = 1000000007; n int Fact[Max_N], Inv_Fact[Max_N]; n long long Inv(const int x) { n   return x == 1 ? 1 : (Mod - Mod / x) * Inv(Mod % x) % Mod; n } n long long Binomial(const int x1, const int x2) { n   return x1 == -1 && x2 == -1 n              ? 1 n              : static_cast<long long>(Fact[x1]) * Inv_Fact[x2] % Mod * n                    Inv_Fact[x1 - x2] % Mod; n } n int calc(const int x, const int a, const int b, const int c, const int d) { n   if (a < 0 || b < 0 || c < 0 || d < 0 || n       (b != c && (x == 0 ? b != c + 1 : b + 1 != c))) n     return 0; n   return Binomial((x == 0) + a + c - 1, (x == 0) + c - 1) * n          Binomial((x == 1) + b + d - 1, (x == 1) + b - 1) % Mod; n } n int calc(const char A[], int a, int b, int c, int d) { n   if (b != c && b != c + 1 && b + 1 != c) return 0; n   int len = strlen(A); n   if (a + b + c + d == 0) return len != 1; n   if (len - 1 < a + b + c + d) return 0; n   Fact[0] = 1; n   for (int i = 1; i <= len; ++i) n     Fact[i] = static_cast<long long>(Fact[i - 1]) * i % Mod; n   Inv_Fact[len] = Inv(Fact[len]); n   for (int i = len; i; --i) n     Inv_Fact[i - 1] = static_cast<long long>(Inv_Fact[i]) * i % Mod; n   if (len - 1 > a + b + c + d) return calc(1, a, b, c, d); n   int res = 0; n   for (int i = 0; i != len - 1 && a >= 0 && b >= 0 && c >= 0 && d >= 0; ++i) { n     int x = (A[i] -  0 ) << 1 | (A[i + 1] -  0 ); n     if (x == 0) n       --a; n     else if (x == 1) n       res = (res + calc(0, a - 1, b, c, d)) % Mod, --b; n     else if (x == 2) n       --c; n     else n       res = (res + calc(0, a, b, c - 1, d)) % Mod, --d; n   } n   return res; n } n char A[Max_N], B[Max_N]; n int a, b, c, d; n int main(int argc, char **argv) { n   scanf( %s , A); n   scanf( %s , B); n   scanf( %d %d %d %d , &a, &b, &c, &d); n   for (int i = strlen(B) - 1; i >= 0; --i) n     if (B[i] ==  0 ) { n       B[i] =  1 ; n       break; n     } else n       B[i] =  0 ; n   if (B[0] ==  0 ) B[0] =  1 , B[strlen(B)] =  0 ; n   printf( %d , (calc(B, a, b, c, d) + Mod - calc(A, a, b, c, d)) % Mod); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> adj[300007]; n bool vis[300007]; n int Time[300007], timeMin[300007], cnt = 0, res = 0; n int dfs(int u, int p) { n   vis[u] = true; n   Time[u] = timeMin[u] = cnt++; n   int ret = 0; n   vector<int> childList; n   for (auto child : adj[u]) { n     if (child == p) { n       continue; n     } n     if (vis[child]) { n       timeMin[u] = min(timeMin[u], Time[child]); n     } else { n       int temp = dfs(child, u); n       timeMin[u] = min(timeMin[u], timeMin[child]); n       if (timeMin[child] > Time[u]) { n         temp++; n       } n       ret = max(ret, temp); n       childList.push_back(temp); n     } n   } n   sort(childList.rbegin(), childList.rend()); n   if (childList.size() == 1) { n     res = max(res, childList[0]); n   } else if (childList.size() > 1) { n     res = max(res, childList[0] + childList[1]); n   } n   return ret; n } n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   for (int i = 0; i < m; i++) { n     int u, v; n     scanf( %d %d , &u, &v); n     u--; n     v--; n     adj[u].emplace_back(v); n     adj[v].emplace_back(u); n   } n   dfs(0, -1); n   printf( %d n , res); n } n


#include <bits/stdc++.h> n const int M = 100010; n int n, k; n int l[M], r[M], a[M]; n long long dp[M], ans = 1e18; n template <typename T> n inline void read(T &x) { n   x = 0; n   char ch = getchar(); n   int f = 1; n   for (; ch <  0  || ch >  9 ; ch = getchar()) n     if (ch ==  - ) f = -1; n   for (; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch -  0 ; n   x *= f; n } n int main() { n   read(n); n   read(k); n   for (int i = 1; i <= n; i++) read(l[i]), read(r[i]), read(a[i]); n   memset(dp, 0x3f, sizeof(dp)); n   dp[1] = 0; n   for (int i = 1; i <= n; i++) { n     long long sum = dp[i], res = k; n     for (int j = i; j <= n; j++) { n       long long reload; n       if (a[j] <= res) n         reload = 0; n       else n         reload = (a[j] - res + k - 1) / k; n       if (reload > r[j] - l[j]) break; n       long long rest = res + reload * k - a[j]; n       sum += a[j]; n       if (j == n) n         ans = std::min(ans, sum); n       else if (l[j] + reload < l[j + 1]) n         dp[j + 1] = std::min(dp[j + 1], sum + rest); n       res = rest; n     } n   } n   if (ans >= 1e16) n     printf( -1 n ); n   else n     printf( %lld n , ans); 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(0); n   int t, s, q; n   cin >> t >> s >> q; n   int ans = 0; n   int tot = s; n   while (tot < t) { n     tot *= q; n     ++ans; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   vector<set<int> > v(26); n   for (int i = 0; i < s.size(); i++) v[s[i] -  a ].insert(i); n   int q = 0, op = 0; n   cin >> q; n   while (q--) { n     cin >> op; n     if (op == 1) { n       int pos = 0; n       char x; n       cin >> pos >> x; n       v[s[pos - 1] -  a ].erase(pos - 1); n       s[pos - 1] = x; n       v[s[pos - 1] -  a ].insert(pos - 1); n     } else { n       int l = 0, r = 0, ans = 0; n       cin >> l >> r; n       for (int i = 0; i < 26; i++) { n         auto it = v[i].lower_bound(l - 1); n         if (it != v[i].end() && *it <= r - 1) ans++; n       } n       cout << ans << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int two(int n) { return 1 << n; } n inline int test(int n, int b) { return (n >> b) & 1; } n inline void set_bit(int& n, int b) { n |= two(b); } n inline void unset_bit(int& n, int b) { n &= ~two(b); } n inline int last_bit(int n) { return n & (-n); } n int dist[403][403]; n void print(int a[][403], int n) { n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) cout << a[i][j] <<    ; n     cout <<   n ; n   } n } n int floyd_warshall(int a[][403], int n) { n   for (int via = 1; via <= n; via++) { n     for (int from = 1; from <= n; from++) { n       for (int to = 1; to <= n; to++) { n         a[from][to] = min(a[from][to], a[from][via] + a[via][to]); n       } n     } n   } n   return a[1][n]; n } n int dist_dij[403]; n bool visited[403]; n int vertex_min_path_len(int n) { n   int min_dist = INT_MAX, min_vertex = 0; n   for (int i = 1; i <= n; i++) { n     if (!visited[i] && dist_dij[i] <= min_dist) n       min_dist = dist_dij[i], min_vertex = i; n   } n   return min_vertex; n } n int dijkstra(int src, int n) { n   memset(visited, 0, sizeof visited); n   for (int i = 1; i <= n; i++) dist_dij[i] = 403; n   dist_dij[src] = 0; n   int cnt = 1; n   while (cnt <= n) { n     int u = vertex_min_path_len(n); n     visited[u] = true; n     for (int v = 1; v <= n; v++) { n       if (!visited[v] && dist[u][v] != 403 && n           dist_dij[u] + dist[u][v] <= dist_dij[v]) n         dist_dij[v] = dist_dij[u] + dist[u][v]; n     } n     cnt++; n   } n   return dist_dij[n]; n } n int main() { n   ios::sync_with_stdio(false); n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) dist[i][j] = 403; n   for (int i = 0; i < m; i++) { n     int u, v; n     cin >> u >> v; n     dist[u][v] = dist[v][u] = 1; n   } n   if (dist[1][n] == 1) { n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= n; j++) { n         if (dist[i][j] == 403) n           dist[i][j] = 1; n         else n           dist[i][j] = 403; n         if (i == j) dist[i][j] = 403; n       } n     } n   } n   int ans = -2; n   ans = dijkstra(1, n); n   if (ans == 403) n     cout <<  -1 n ; n   else n     cout << ans <<   n ; n   return 0; n } n


#include<bits/stdc++.h> n #define llong long long n #define mkpr make_pair n #define x first n #define y second n #define iter iterator n #define riter reverse_iterator n #define y1 Lorem_ipsum_ n #define tm dolor_sit_amet_ n using namespace std; n  n inline int read() n { n  int x = 0,f = 1; char ch = getchar(); n  for(;!isdigit(ch);ch=getchar()) {if(ch== - ) f = -1;} n  for(; isdigit(ch);ch=getchar()) {x = x*10+ch-48;} n  return x*f; n } n  n const int P = 1e9+7; n const int mxN = 2e5; n int n,m; llong fact[mxN+3],facti[mxN+3],inv[mxN+3]; n  n llong quickpow(llong x,llong y) n { n  llong cur = x,ret = 1ll; n  for(int i=0; y; i++) n  { n   if(y&(1ll<<i)) {y-=(1ll<<i); ret = ret*cur%P;} n   cur = cur*cur%P; n  } n  return ret; n } n llong comb(llong x,llong y) {return x<0ll||y<0ll||x<y?0ll:fact[x]*facti[y]%P*facti[x-y]%P;} n llong combi(llong x,llong y) {return x<0ll||y<0ll||x<y?0ll:facti[x]*fact[y]%P*fact[x-y]%P;} n  n void initfact(int n) n { n  fact[0] = 1ll; for(int i=1; i<=n; i++) fact[i] = fact[i-1]*i%P; n  facti[n] = quickpow(fact[n],P-2); for(int i=n-1; i>=0; i--) facti[i] = facti[i+1]*(i+1ll)%P; n } n  n int main() n { n  initfact(mxN); n  int T = read(); while(T--) n  { n   n = read(),m = read(); llong ans = 0ll; n   for(int i=0; i<=n; i++) n   { n    llong cur = comb(n-1ll*(i-1)*(m-1),i)*combi(n,i)%P; n    ans = (ans+cur)%P; n   } n   printf( %lld n ,ans); n  } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n long long a[10], nd[10], dad[10], root, ans; n int nopd(long long n) { n   long long now = 2, ans = 0; n   for (; now * now <= n; now++) n     while (n && n % now == 0) { n       n /= now; n       ans++; n     } n   if (n > 1) ans++; n   return ans; n } n bool prime(long long n) { n   if (n == 1) return false; n   for (long long i = 2; i * i <= n; i++) n     if (n % i == 0) return false; n   return true; n } n int nopd_pooyan(long long a) { n   int ans = 0; n   if (prime(a)) n     ans++; n   else { n     long long j = 2; n     while (a > 1) { n       if (a % j == 0) { n         while (a % j == 0) { n           a /= j; n           ans++; n         } n         if (prime(a)) { n           ans++; n           break; n         } n       } n       j++; n     } n   } n   return ans; n } n int main() { n   int n, best, ibest, flag; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   reverse(a, a + n); n   for (int i = 0; i < n; i++) { n     nd[i] = nopd_pooyan(a[i]); n     ans += nd[i]; n     if (nd[i] == 1) ans--; n   } n   for (int i = 0; i < n; i++) { n     if (!dad[i]) root++; n     flag = 1; n     while (flag) { n       best = ibest = -1; n       for (int j = i + 1; j < n; j++) n         if (a[i] % a[j] == 0 && !dad[j] && nd[j] > best) { n           best = nd[j]; n           ibest = j; n         } n       if (ibest == -1) n         flag = 0; n       else { n         ans -= (best - 1); n         dad[ibest] = i + 1; n         a[i] /= a[ibest]; n       } n     } n   } n   ans += root; n   if (root > 1) ans++; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, n, b, x, y, s; n   int q; n   cin >> q; n   while (q--) { n     cin >> a >> b >> n >> s; n     long long ts = a * n + b; n     if (ts < s) { n       cout <<  NO n ; n       continue; n     } n     if (s % n > b) { n       cout <<  NO n ; n       continue; n     } n     cout <<  YES n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int uf[55]; n int buc[55]; n bool chk[55]; n int ans[5000][2]; n int fh(int idx) { n   if (uf[idx] != idx) uf[idx] = fh(uf[idx]); n   return uf[idx]; n } n vector<int> edge[55]; n void dfs(int idx) { n   if (chk[idx]) return; n   chk[idx] = true; n   for (int i = 0; i < edge[idx].size(); i++) { n     dfs(edge[idx][i]); n   } n } n int main() { n   int n, m, g1, g2; n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; i++) { n     uf[i] = i; n   } n   bool b = true; n   for (int i = 0; i < m; i++) { n     scanf( %d %d , &g1, &g2); n     edge[g1].push_back(g2); n     edge[g2].push_back(g1); n     buc[g1]++; n     buc[g2]++; n     if (uf[g1] == uf[g2]) { n       b = false; n     } n     uf[fh(g1)] = fh(g2); n   } n   if (n == 1) { n     if (m == 0) { n       printf( YES n1 n1 1 ); n     } else if (m == 1) { n       printf( YES n0 ); n     } else { n       printf( NO ); n     } n     return 0; n   } n   for (int i = 1; i <= n; i++) { n     if (buc[i] > 2) { n       printf( NO ); n       return 0; n     } n   } n   if (!b) { n     dfs(1); n     for (int i = 1; i <= n; i++) { n       if (!chk[i]) { n         printf( NO ); n         return 0; n       } n     } n     printf( YES n0 ); n     return 0; n   } n   int cnt = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = i + 1; j <= n; j++) { n       if (fh(i) != fh(j) && buc[i] != 2 && buc[j] != 2) { n         ans[cnt][0] = i; n         ans[cnt][1] = j; n         uf[fh(i)] = fh(j); n         buc[i]++; n         buc[j]++; n         cnt++; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     if (buc[i] != 2) { n       for (int j = i + 1; j <= n; j++) { n         if (buc[j] != 2) { n           ans[cnt][0] = i; n           ans[cnt][1] = j; n           cnt++; n           break; n         } n       } n       break; n     } n   } n   printf( YES n ); n   printf( %d n , cnt); n   for (int i = 0; i < cnt; i++) { n     printf( %d %d n , ans[i][0], ans[i][1]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n int fst[N], v[2 * N], w[2 * N], nxt[2 * N], cnt = 1; n bool vis[2 * N]; n int c[N], ans[N], n; n queue<int> q; n void add(int x, int y, int z) { n   v[++cnt] = y, w[cnt] = z; n   c[y] += z; n   nxt[cnt] = fst[x], fst[x] = cnt; n } n void bfs() { n   q.push(1); n   while (!q.empty()) { n     int x = q.front(); n     q.pop(); n     for (int i = fst[x]; i; i = nxt[i]) { n       int y = v[i]; n       if (vis[i]) continue; n       vis[i] = vis[i ^ 1] = 1, ans[i / 2] = i; n       c[y] -= w[i]; n       if (!c[y] && y != n) q.push(y); n     } n   } n } n int main() { n   int m, x, y, z; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d%d , &x, &y, &z); n     add(x, y, z), add(y, x, z); n   } n   for (int i = 1; i <= n; i++) c[i] /= 2; n   bfs(); n   for (int i = 1; i <= m; i++) printf( %d n , ans[i] - i * 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s, t; n double ans; n double solve(string s) { n   int a = 0, b = 0, c = 0, p = 0; n   for (int i = 0; i < s.size(); i++) { n     char e = (b % 2) ?  R  :  L ; n     if (s[i] ==  X ) n       b++; n     else if (s[i] == e) n       a++, b++, p = 0; n     else n       a++, b += 2, c += p, p = !p; n   } n   if (b % 2) b++, c += p; n   if (a * 2 > b) a -= c, b -= 2 * c; n   return 100.0 * a / b; n } n int main() { n   cin >> t; n   for (int i = 0; i + 1 < t.size(); i++) { n     s += t[i]; n     if (t[i] == t[i + 1] && t[i] !=  X ) s +=  X ; n   } n   s += t.back(); n   if (s[0] == s.back() && s.back() !=  X ) n     ans = max(solve(s +  X ), solve( X  + s)); n   else n     ans = solve(s); n   int f = ans * 1000000; n   ans = f / 1000000.0; n   printf( %.6lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n int N; n int arr[300005]; n int segf[4 * 300005]; n int segb[4 * 300005]; n int pow29[300005]; n void inv(long long a, long long b, long long &x, long long &y) { n   if (a == 0) { n     x = 0; n     y = 1; n     return; n   } n   inv((b % a), a, x, y); n   long long z = x; n   long long zz = y; n   x = zz - (b / a) * z; n   y = z; n } n void updatef(int start, int end, int i, int node) { n   if (start > i || end < i) return; n   if (start == end) { n     segf[node] += pow29[start]; n     if (segf[node] >= mod) segf[node] -= mod; n     return; n   } n   updatef(start, (start + end) / 2, i, 2 * node); n   updatef((start + end) / 2 + 1, end, i, 2 * node + 1); n   segf[node] = segf[2 * node] + segf[2 * node + 1]; n   if (segf[node] >= mod) segf[node] -= mod; n } n void updateb(int start, int end, int i, int node) { n   if (start > i || end < i) return; n   if (start == end) { n     segb[node] += pow29[N - start]; n     if (segb[node] >= mod) segb[node] -= mod; n     return; n   } n   updateb(start, (start + end) / 2, i, 2 * node); n   updateb((start + end) / 2 + 1, end, i, 2 * node + 1); n   segb[node] = segb[2 * node] + segb[2 * node + 1]; n   if (segb[node] >= mod) segb[node] -= mod; n } n int queryf(int start, int end, int i, int j, int node = 1) { n   if (start > j || end < i) return 0; n   if (start >= i && end <= j) return segf[node]; n   return (queryf(start, (start + end) / 2, i, j, 2 * node) + n           queryf((start + end) / 2 + 1, end, i, j, 2 * node + 1)) % n          mod; n } n int queryb(int start, int end, int i, int j, int node = 1) { n   if (start > j || end < i) return 0; n   if (start >= i && end <= j) return segb[node]; n   return (queryb(start, (start + end) / 2, i, j, 2 * node) + n           queryb((start + end) / 2 + 1, end, i, j, 2 * node + 1)) % n          mod; n } n int main() { n   pow29[0] = 1; n   for (int g = 1; g <= 300005; g++) pow29[g] = (1LL * 29 * pow29[g - 1]) % mod; n   scanf( %d , &N); n   for (int g = 1; g <= N; g++) { n     scanf( %d , &arr[g]); n     arr[g]--; n   } n   for (int g = 1; g <= N; g++) { n     int t = min(arr[g], N - 1 - arr[g]); n     int first = queryf(0, N - 1, arr[g], arr[g] + t); n     int second = queryb(0, N - 1, arr[g] - t, arr[g]); n     long long R = pow29[arr[g]]; n     long long x = 0, y = 0; n     inv(R, mod, x, y); n     x %= mod; n     x += mod; n     x %= mod; n     first = (1LL * first * x) % mod; n     R = pow29[N - arr[g]], x = 0, y = 0; n     inv(R, mod, x, y); n     x %= mod; n     x += mod; n     x %= mod; n     second = (1LL * second * x) % mod; n     if (first != second) { n       if (arr[g] > 0 && arr[g] < N - 1) { n         puts( YES ); n         return 0; n       } n     } n     updatef(0, N - 1, arr[g], 1); n     updateb(0, N - 1, arr[g], 1); n   } n   puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int T; n   cin >> T; n   for (int test_case = 0; test_case < T; test_case++) { n     int num, round = 1; n     vector<int> result; n     cin >> num; n     while (num > 0) { n       if (num % 10 != 0) { n         result.push_back((num % 10) * round); n       } n       num /= 10; n       round *= 10; n     } n     cout << result.size() << endl; n     for (int i = 0; i < result.size(); i++) { n       cout << result[i] <<    ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 314000000; n int n, m, id[111111], dis[111111], dp[111111]; n int val[111111], sum[111111], deg[111111]; n int vis[111111]; n vector<int> str[111111]; n vector<int> to[111111]; n vector<int> fr[111111]; n int add(int x, int y) { n   int s = x + y; n   return min(s, inf); n } n void dijkstra() { n   priority_queue<pair<int, int>, vector<pair<int, int> >, n                  greater<pair<int, int> > > n       q; n   for (int i = 1; i <= n; i++) { n     dis[i] = 0x3f3f3f3f; n   } n   for (int i = 1; i <= m; i++) { n     if (to[i].empty()) { n       if (val[i] < dis[id[i]]) { n         dis[id[i]] = val[i]; n         q.push(make_pair(dis[id[i]], id[i])); n       } n     } n   } n   while (!q.empty()) { n     int x = q.top().second, d = q.top().first; n     q.pop(); n     if (dis[x] != d) continue; n     for (int i = 0; i < fr[x].size(); i++) { n       deg[fr[x][i]]--; n       sum[fr[x][i]] = add(sum[fr[x][i]], dis[x]); n       if (!deg[fr[x][i]] && dis[id[fr[x][i]]] > sum[fr[x][i]]) { n         dis[id[fr[x][i]]] = sum[fr[x][i]]; n         q.push(make_pair(dis[id[fr[x][i]]], id[fr[x][i]])); n       } n     } n   } n } n int dfs(int x) { n   if (vis[x]) return -2; n   if (dp[x] != -1) return dp[x]; n   vis[x] = 1; n   for (int i = 0; i < str[x].size(); i++) { n     int y = str[x][i], sum = 0, t = 0; n     for (int j = 0; j < to[y].size(); t = ++j) { n       if (dis[to[y][j]] == 0x3f3f3f3f) { n         break; n       } n     } n     if (t < to[y].size()) { n       continue; n     } n     for (int j = 0; j < to[y].size(); j++) { n       int t = dfs(to[y][j]); n       if (t == -2) { n         vis[x] = 0; n         return dp[x] = -2; n       } n       sum = add(sum, t); n     } n     sum = add(sum, val[y]); n     dp[x] = max(dp[x], sum); n   } n   vis[x] = 0; n   return dp[x]; n } n int main() { n   scanf( %d%d , &m, &n); n   for (int i = 1; i <= m; i++) { n     int cnt; n     scanf( %d%d , &id[i], &cnt); n     str[id[i]].push_back(i); n     while (cnt--) { n       int x; n       scanf( %d , &x); n       if (x == -1) { n         val[i]++; n         sum[i]++; n         continue; n       } n       to[i].push_back(x); n       fr[x].push_back(i); n     } n     deg[i] = to[i].size(); n   } n   dijkstra(); n   memset(dp, -1, sizeof(dp)); n   for (int i = 1; i <= n; i++) { n     if (dis[i] == 0x3f3f3f3f) { n       puts( -1 -1 ); n     } else { n       printf( %d %d n , dis[i], dfs(i)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, t; n   cin >> n; n   while (n--) { n     cin >> t; n     if (t <= 2) { n       cout << 0 << endl; n       continue; n     } n     if (t % 2 != 0) { n       cout << t / 2 << endl; n     } else { n       cout << t / 2 - 1 << endl; n     } n   } n   return 0; n } n