#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ull = unsigned long long; n using ld = long double; n const ll llinf = (1ll << 61) - 1; n const double eps = 1e-6, ldeps = 1e-9; n struct Dbg { n   static constexpr auto &os = cout; n   template <class C> n   static auto dud(C *x) -> decltype(os << *x, 0); n   template <class C> n   static char dud(...); n   template <class C> n   typename enable_if<sizeof dud<C>(0) != 1, Dbg &>::type operator<<( n       const C &x) { n     os << x; n     return *this; n   } n   template <class C> n   Dbg &dump(C b, C e) { n     *this <<   n[ ; n     int cur = 0; n     for (C i = b; i != e and cur < 26; i++, cur++) n       *this <<  ,   + 2 * (i == b) << *i; n     return *this <<  ] ; n   } n   template <class C> n   typename enable_if<sizeof dud<C>(0) == 1, Dbg &>::type operator<<( n       const C &x) { n     return dump(begin(x), end(x)); n   } n   template <class C, size_t X> n   typename enable_if<sizeof(C) != 1, Dbg &>::type operator<<(C (&x)[X]) { n     return dump(begin(x), end(x)); n   } n   template <class B, class C> n   Dbg &operator<<(const pair<B, C> &x) { n     return *this <<  {  << x.first <<  ,   << x.second <<  } ; n   } n   template <class C, size_t Y> n   struct TP { n     void operator()(Dbg &os, const C &t) { n       TP<C, Y - 1>()(os, t); n       os <<  ,   << get<Y - 1>(t); n     } n   }; n   template <class C> n   struct TP<C, 1> { n     void operator()(Dbg &os, const C &t) { os << get<0>(t); } n   }; n   template <class... C> n   Dbg &operator<<(const tuple<C...> &t) { n     os <<  { , TP<decltype(t), sizeof...(C)>()(*this, t); n     return *this <<  } ; n   } n   Dbg &operator<<(ostream &(*x)(std::ostream &)) { n     os << x; n     return *this; n   } n } dbg; n void err(istringstream *iss) { delete iss; } n template <class C, class... Args> n void err(istringstream *iss, const C &val, const Args &...args) { n   string name; n   *iss >> name; n   if (name.back() ==  , ) name.pop_back(); n   dbg << name <<   =   << val <<  ;  , err(iss, args...); n } n int n, m, K, T, Q, cn; n const int inf = 1000000007, mxnm = 1000005; n vector<vector<int> > a; n vector<pair<int, int> > pos[mxnm]; n int maxrow[mxnm], maxcol[mxnm]; n template <class C> n C compress(vector<C> &v) { n   int sz = int(v.size()); n   vector<pair<C, int> > tmp(sz); n   for (int i = 0; i < sz; i++) { n     tmp[i] = {v[i], i}; n   } n   sort(begin(tmp), end(tmp)); n   int cur = 1; n   for (int i = 0; i < sz; i++) { n     cur += i and tmp[i].first != tmp[i - 1].first; n     v[tmp[i].second] = cur; n   } n   return cur; n } n int os[mxnm], lastrow[mxnm], lastcol[mxnm]; n class DSU { n   vector<int> dsupar, siz; n  n  public: n   int cntDSU; n   void reset() { n     int sz = int(dsupar.size()); n     for (int i = 0; i < sz; i++) dsupar[i] = i, siz[i] = 1; n   } n   DSU(int N, int noOfSets = -1) { n     dsupar.resize(N + 5), siz.resize(N + 5); n     reset(); n     cntDSU = noOfSets == -1 ? N : noOfSets; n   } n   int find(int x) { return dsupar[x] == x ? x : dsupar[x] = find(dsupar[x]); } n   void union_(int u, int v) { n     if ((u = find(u)) == (v = find(v))) return; n     if (siz[u] < siz[v]) swap(u, v); n     dsupar[v] = u, siz[u] += siz[v], siz[v] = 0, os[u] = max(os[u], os[v]); n     cntDSU--; n   } n } dsu(mxnm); n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(NULL); n   cout.precision(11); n   cin >> n >> m; n   a.resize(n + 5, vector<int>(m + 5)); n   vector<int> a2(n * m); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> a2[i * m + j]; n     } n   } n   int o = compress(a2); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       pos[a2[i * m + j]].push_back({i, j}); n       a[i][j] = a2[i * m + j]; n     } n   } n   memset(lastrow, -1, sizeof(lastrow)), memset(lastcol, -1, sizeof(lastcol)); n   for (int i = 1; i <= o; i++) { n     for (auto &j : pos[i]) { n       os[j.first * m + j.second] = max(maxrow[j.first], maxcol[j.second]) + 1; n     } n     for (auto &j : pos[i]) { n       if (lastrow[j.first] == -1) n         lastrow[j.first] = j.second; n       else n         dsu.union_(j.first * m + j.second, j.first * m + lastrow[j.first]); n       if (lastcol[j.second] == -1) n         lastcol[j.second] = j.first; n       else n         dsu.union_(j.first * m + j.second, lastcol[j.second] * m + j.second); n     } n     for (auto &j : pos[i]) { n       a[j.first][j.second] = os[dsu.find(j.first * m + j.second)]; n       lastrow[j.first] = lastcol[j.second] = -1; n     } n     for (auto &j : pos[i]) { n       maxrow[j.first] = max(maxrow[j.first], a[j.first][j.second]), n       maxcol[j.second] = max(maxcol[j.second], a[j.first][j.second]); n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cout << a[i][j] <<    n [j == m - 1]; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n string s; n int main() { n   cin >> n >> s; n   int sol = n; n   for (int i = 1; i < n; i++) { n     if (s[i] != s[i - 1] and s[i - 1] !=  D ) { n       sol--; n       s[i] =  D ; n     } n   } n   cout << sol << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void FastIO() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   cout.precision(15); n } n string fun(string str) { n   int i, l = str.length(); n   for (i = 0; i < l; i++) { n     if (str[i] ==  + ) n       str[i] =  * ; n     else n       str[i] =  + ; n   } n   return str; n } n string s[10][1000]; n int main() { n   FastIO(); n   int i, n, j; n   cin >> n; n   if (n == 0) { n     cout <<  +  <<   n ; n     return 0; n   } n   s[0][0] =  ++ ; n   s[0][1] =  +* ; n   if (n == 1) { n     cout << s[0][0] <<   n ; n     cout << s[0][1] <<   n ; n     return 0; n   } n   for (i = 1; i < 9; i++) { n     for (j = 0; j < (1 << i); j++) s[i][j] = s[i - 1][j] + s[i - 1][j]; n     for (j = (1 << i); j < (1 << (i + 1)); j++) n       s[i][j] = s[i - 1][j - (1 << i)] + fun(s[i - 1][j - (1 << i)]); n     if (i == (n - 1)) { n       for (j = 0; j < (1 << n); j++) cout << s[i][j] <<   n ; n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[410][42]; n char a[42], t[42]; n int main() { n   int n; n   while (~scanf( %d , &n)) { n     int ans = n, g = 0; n     for (int k = 0; k < n; k++) { n       scanf( %s , t); n       while (true) { n         int k = 0; n         for (int i = 0; t[i] !=   0 ;) { n           if (t[i] ==  u ) n             a[k++] =  o , a[k++] =  o , i++; n           else if (t[i] ==  k ) { n             int num = -1; n             for (int j = i; t[j] !=   0 ; j++) { n               if (t[j] ==  k ) n                 continue; n               else if (t[j] ==  h ) { n                 num = j + 1; n                 break; n               } else n                 break; n             } n             if (num == -1) n               a[k++] =  k , i++; n             else n               a[k++] =  h , i = num; n           } else n             a[k++] = t[i], i++; n         } n         a[k] =   0 ; n         if (strcmp(a, t) == 0) n           break; n         else n           strcpy(t, a); n       } n       bool flag = false; n       for (int j = 0; j < g; j++) n         if (strcmp(s[j], t) == 0) { n           ans--; n           flag = true; n           break; n         } n       if (!flag) strcpy(s[g++], t); n     } n     printf( %d n , ans); 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(0); n   cout.tie(0); n   int n; n   cin >> n; n   vector<int> arr(n), lefts(n), rights(n); n   for (int &x : arr) cin >> x; n   stack<int> positions; n   for (int i = 0; i < n; ++i) { n     while (!positions.empty() && arr[positions.top()] <= arr[i]) n       positions.pop(); n     if (positions.empty()) { n       lefts[i] = -1; n     } else { n       lefts[i] = positions.top(); n     } n     positions.push(i); n   } n   while (!positions.empty()) positions.pop(); n   for (int i = n - 1; i >= 0; --i) { n     while (!positions.empty() && arr[positions.top()] <= arr[i]) n       positions.pop(); n     if (positions.empty()) { n       rights[i] = n; n     } else { n       rights[i] = positions.top(); n     } n     positions.push(i); n   } n   while (!positions.empty()) positions.pop(); n   vector<int> inv(n); n   for (int i = 0; i < n; ++i) { n     inv[arr[i]] = i; n   } n   long long res = 0; n   for (int i = 0; i < n; ++i) { n     int distL = i - lefts[i]; n     int distR = rights[i] - i; n     if (distL < distR) { n       for (int j = lefts[i] + 1; j < i; ++j) { n         if (arr[j] > arr[i]) continue; n         int Right = arr[i] - arr[j]; n         int pos = inv[Right]; n         if (pos > i && pos < rights[i]) { n           ++res; n         } n       } n     } else { n       for (int j = i + 1; j < rights[i]; ++j) { n         if (arr[j] > arr[i]) continue; n         int Left = arr[i] - arr[j]; n         int pos = inv[Left]; n         if (pos < i && pos > lefts[i]) { n           ++res; n         } n       } n     } n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 400000; n long long l[500000], r[500000], k[500000], b[500000], a[500000], pref1[500000], n     pref2[500000], s = MX; n long long f(long long b1, long long a1, long long b2, long long a2) { n   return (b1 - b2) / (a1 - a2); n } n void add(long long kx, long long bx) { n   if (s == MX) { n     l[s] = -2e18; n     r[s] = 2e18; n     b[s] = bx; n     k[s] = kx; n     s--; n     return; n   } n   if (f(bx, kx, b[s + 1], k[s + 1]) > r[s + 1]) { n     s++; n     add(kx, bx); n     return; n   } n   l[s] = -2e18; n   r[s] = f(bx, kx, b[s + 1], k[s + 1]); n   b[s] = bx; n   k[s] = kx; n   l[s + 1] = r[s] + 1; n   s--; n   return; n } n int main() { n   ios::sync_with_stdio(0); n   long long n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   pref1[0] = a[0]; n   pref2[0] = a[0]; n   for (int i = 1; i < n; i++) { n     pref1[i] = pref1[i - 1] + a[i]; n     pref2[i] = pref2[i - 1] + a[i] * (i + 1); n   } n   long long ans = 0; n   for (long long i = 0; i < n; i++) { n     ans = max(ans, pref2[i]); n     long long lt = s + 1, rt = MX; n     while (lt < rt) { n       long long mid = (lt + rt) / 2; n       if (lt == rt - 1) mid++; n       if (pref1[i] < l[mid]) n         rt = mid - 1; n       else n         lt = mid; n     } n     long long q = 0, t = 0; n     if (i > 0) { n       q = pref2[i - 1]; n       t = pref1[i - 1]; n     } n     if (lt <= MX) ans = max(ans, pref2[i] - b[lt] + pref1[i] * k[lt]); n     add(-i - 1, -(pref1[i] * (i + 1) - pref2[i])); n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n bool 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 unsigned int countBits(unsigned int n) { n   unsigned int count = 0; n   while (n) { n     count++; n     n >>= 1; n   } n   return count; n } n bool isPerfectSquare(long double x) { n   long double sr = sqrt(x); n   return ((sr - floor(sr)) == 0); n } n int subArraySum(int arr[], int n) { n   int curr_sum = arr[0], start = 0, i, count = 0, len; n   len = 0; n   for (i = 1; i <= n; i++) { n     len++; n     while (curr_sum > len && start < i - 1) { n       curr_sum = curr_sum - arr[start]; n       start++; n       len++; n     } n     if (curr_sum == len) { n       count++; n     } n     if (i < n && i > 0) { n       curr_sum = curr_sum + arr[i]; n     } n   } n   return count; n } n void printDigit(int N, vector<long long int>& arr) { n   int i = 0; n   int j, r; n   while (N != 0) { n     r = N % 10; n     arr.push_back(r); n     i++; n     N = N / 10; n   } n } n void solver(vector<long long int> arr, long long int start, n             long long int& count, long long int a1) { n   long long int sum = 0; n   for (long long int i = start + 1; i < arr.size() - 1; i++) { n     sum += arr[i]; n     if (sum == a1) count++; n   } n } n bool checkDivisibility(long long int n, int digit) { n   if (digit == 0) return true; n   return (digit != 0 && n % digit == 0); n } n bool allDigitsDivide(long long int n) { n   long long int temp = n; n   while (temp > 0) { n     int digit = temp % 10; n     if (!(checkDivisibility(n, digit))) return false; n     temp /= 10; n   } n   return true; n } n bool isPrime(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 (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(unsigned long long x) { return x && (!(x & (x - 1))); } n int nextPrime(int N) { n   if (N <= 1) return 2; n   int prime = N; n   bool found = false; n   while (!found) { n     prime++; n     if (isPrime(prime)) found = true; n   } n   return prime; n } n bool sortcol(const vector<long long int>& v1, const vector<long long int>& v2) { n   return v1[0] < v2[0]; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   long long int b = 0, q, c, x, y, start = 0, end = 0, count = 0, neg = 0, n                 pos = 0, count1 = 0, a1 = 0, b1 = 0, c1 = 0, x1, y1, n1, m, n                 ans = 0, sum = 0, k; n   long long int n; n   string s1, s2(  ), s3(  ); n   bool check2 = false, check3 = false; n   long long int x3, x2, y3, y2; n   int curr1 = 0, curr = 0; n   vector<int> arr(130, 0); n   vector<int> arr1; n   stack<char> s; n   long long int vb; n   bool check1 = false, check4 = false; n   vector<double> arrv; n   unordered_map<long long int, long long int> map1; n   unordered_map<long long int, long long int> map2; n   long long int a, l = 0, d = 0, u = 0, v, g, sub; n   cin >> s1; n   n = s1.length(); n   for (int i = 0; i < n; i++) { n     arr[s1[i]]++; n   } n   for (int i = 0; i < arr.size(); i++) { n     if (arr[i] % 2 != 0) count++; n   } n   if (count == 0 || count == 1) n     cout <<  First ; n   else if (count % 2 != 0) n     cout <<  First ; n   else n     cout <<  Second ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int x, int1, int2, int3, tempsum, lastsum = -1; n   string s, s1, s2, s3; n   cin >> s; n   x = s.size(); n   for (int d1 = 1; d1 < x - 1 && d1 < 8; d1++) { n     for (int d2 = 1; d2 < x - d1 && d2 < 8; d2++) { n       s1 = s.substr(0, d1); n       s2 = s.substr(d1, d2); n       s3 = s.substr(d1 + d2, x - d1 - d2); n       if ((s2.size() > 1 && s2[0] ==  0 ) || (s3.size() > 1 && s3[0] ==  0 ) || n           (s1.size() > 1 && s1[0] ==  0 ) || x - d1 - d2 > 7) n         continue; n       stringstream convert1, convert2, convert3; n       convert1 << s1; n       convert1 >> int1; n       convert2 << s2; n       convert2 >> int2; n       convert3 << s3; n       convert3 >> int3; n       if (int1 > 1000000 || int2 > 1000000 || int3 > 1000000) continue; n       tempsum = int1 + int2 + int3; n       lastsum = max(tempsum, lastsum); n     } n   } n   cout << lastsum << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n #pragma GCC optimization( unroll-loops ) n const long double pi = 2 * acos(0.0); n using namespace std; n int power(int a, int n) { n   if (n == 0) { n     return 1; n   } n   int p = power(a, n / 2) % 1000000007; n   p = (p * p) % 1000000007; n   if (n % 2 == 1) { n     p = (p * a) % 1000000007; n   } n   return p; n } n int gcd(int a, int b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n const int N = 1e6 + 5; n vector<int> sieve(N, 0); n void si() { n   sieve[1] = 1; n   for (int i = 2; i < N; i++) { n     if (sieve[i] == 0) { n       sieve[i] = i; n       for (int j = 2 * i; j < N; j += i) { n         sieve[j] = i; n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   int n; n   cin >> n; n   if (n == 1) { n     cout << 0 <<   n ; n     return 0; n   } n   vector<int> a(n + 1); n   ; n   for (int i = 0; i < n; i++) cin >> a[i]; n   ; n   a[n] = 0; n   int k = (n + 1) / 2; n   int dp[n + 1][k + 1][2]; n   for (int i = 0; i <= n; i++) { n     for (int j = 0; j <= k; j++) { n       dp[i][j][0] = 1e9; n       dp[i][j][1] = 1e9; n     } n   } n   dp[1][0][0] = 0; n   dp[1][1][1] = a[1] - min(a[0] - 1, a[1]); n   for (int i = 0; i <= n; i++) { n     dp[i][0][0] = 0; n   } n   vector<int> ans(k, 1e9); n   for (int i = 2; i <= n; i++) { n     for (int j = 1; j <= k; j++) { n       dp[i][j][0] = min(dp[i - 1][j][0], dp[i - 1][j][1]); n       int z = 1e9; n       if (i - 3 >= 0) { n         z = dp[i - 2][j - 1][1]; n         if (a[i - 3] > a[i - 1]) { n           z += a[i - 3] - a[i - 1]; n         } n         z += a[i] - min(a[i], a[i - 1] - 1); n       } n       dp[i][j][1] = min(dp[i - 1][j - 1][0] + a[i] - min(a[i], a[i - 1] - 1) + n                             a[i - 2] - min(a[i - 2], a[i - 1] - 1), n                         z); n       dp[i][j][1] = min(dp[i][j][1], (int)(1e9)); n       ans[j - 1] = min(ans[j - 1], dp[i][j][1]); n       ans[j - 1] = min(ans[j - 1], dp[i][j][0]); n     } n   }; n   for (int i = 0; i < k; i++) cout << ans[i] <<    ; n   ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T, class T2> n T2 fac(T n) { n   return n < 2 ? 1 : fac<T, T2>(n - 1) * n; n } n template <class T> n T nb_combinaisons(T k, T n) { n   if (n < k) return nb_combinaisons<T>(n, k); n   return fac<T, long long int>(n) / n          (fac<T, long long int>(n - k) * fac<T, long long int>(k)); n } n string itoa_2(long long int n) { n   string s; n   stringstream ss; n   ss << n; n   ss >> s; n   while (s.size() != 9) s =  0  + s; n   return s; n } n int pgcd(int a, int b) { n   if (a == 0) return b; n   if (b == 0) return a; n   return pgcd(b, a % b); n } n int main() { n   int A, B, n; n   cin >> A >> B >> n; n   if (A == 0) { n     if (B != 0) { n       cout <<  No solution ; n       return 0; n     } else { n       cout <<  0 ; n       return 0; n     } n   } n   if (B % A != 0) { n     cout <<  No solution ; n     return 0; n   } n   int z = B / A; n   for (int c = -1000; c <= 1000; c++) { n     int alpha = pow((double)c, n); n     if (alpha == z) { n       cout << c; n       return 0; n     } n   } n   cout <<  No solution ; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 110; n int N, M, U = 110, D = 0, L = 110, R = 0, cnt; n char s[maxn][maxn]; n inline int gi() { n   char ch; n   int ret = 0, f = 1; n   do ch = getchar(); n   while (!(ch >=  0  && ch <=  9 ) && ch !=  - ); n   if (ch ==  - ) f = -1, ch = getchar(); n   do ret = ret * 10 + ch -  0 , ch = getchar(); n   while (ch >=  0  && ch <=  9 ); n   return ret * f; n } n int main() { n   N = gi(); n   M = gi(); n   for (int i = 1; i <= N; ++i) scanf( %s , s[i] + 1); n   for (int i = 1; i <= N; ++i) n     for (int j = 1; j <= M; ++j) n       if (s[i][j] ==  B ) { n         ++cnt; n         U = min(i, U); n         D = max(i, D); n         L = min(j, L); n         R = max(j, R); n       } n   int W = max(R - L + 1, D - U + 1); n   if (!cnt) n     puts( 1 ); n   else if (W > N || W > M) n     puts( -1 ); n   else n     printf( %d n , W * W - cnt); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n int pre[N]; n void solve() { n   string s; n   int n, k, ans = 0; n   cin >> n >> k >> s; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  1 ) { n       if (i == 0) n         pre[i] = 1; n       else n         pre[i] = pre[i - 1] + 1; n     } else { n       if (i == 0) n         pre[i] = 0; n       else n         pre[i] = pre[i - 1]; n     } n   } n   for (int i = 0; i < n; i++) { n     if (s[i] ==  1 ) { n       i += k; n       continue; n     } n     int l = i - k - 1; n     int r = i + k; n     l = max(0, l); n     r = min(r, n - 1); n     if (pre[l] == pre[r]) { n       ans++; n       i = r; n     } n   } n   cout << ans <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int tc = 1; n   cin >> tc; n   while (tc--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const double pi = acos(-1.0); n const int N = 1e5 + 10; n const int mod = 100; n int main() { n   string str; n   int n, ans; n   cin >> n >> str; n   ans = n; n   for (int i = 0; i < n - 1; i++) { n     if (str[i] ==  U  && str[i + 1] ==  R ) { n       ans--; n       i++; n     } else if (str[i] ==  R  && str[i + 1] ==  U ) { n       i++; n       ans--; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 850; n int capacity[N][N], p[N], visit[N], n, source, sink, max_flow; n pair<int, int> a[N]; n vector<int> adj[N]; n double f[N], cost[N][N], res; n double dis(pair<int, int> a, pair<int, int> b) { n   return sqrt((a.first - b.first) * (a.first - b.first) + n               (a.second - b.second) * (a.second - b.second)); n } n void add(int u, int v, int cap, double c) { n   adj[u].push_back(v); n   adj[v].push_back(u); n   capacity[u][v] = cap; n   cost[u][v] = c; n   cost[v][u] = -c; n } n int bfs() { n   for (int i = 0; i <= 2 * n + 1; i++) { n     visit[i] = 2; n     f[i] = 1e9; n   } n   f[source] = 0; n   deque<int> q; n   q.push_back(source); n   int check = 0; n   while (!q.empty()) { n     int u = q.front(); n     q.pop_front(); n     visit[u] = 0; n     if (u == sink) check = 1; n     for (int v : adj[u]) n       if (capacity[u][v] > 0 && f[v] > f[u] + cost[u][v]) { n         f[v] = f[u] + cost[u][v]; n         p[v] = u; n         if (visit[v] == 1) continue; n         if (visit[v] == 2) n           q.push_back(v); n         else n           q.push_front(v); n       } n   } n   return check; n } n int main() { n   ios_base ::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   source = 0; n   sink = 2 * n + 1; n   for (int i = 1; i <= n; i++) cin >> a[i].first >> a[i].second; n   for (int i = 1; i <= n; i++) add(source, i, 2, 0); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n       if (a[i].second > a[j].second) add(i, j + n, 1, dis(a[i], a[j])); n   for (int i = n + 1; i <= 2 * n; i++) add(i, sink, 1, 0); n   while (bfs()) { n     int flow_path = 1e9, v = sink; n     while (v != source) { n       flow_path = min(flow_path, capacity[p[v]][v]); n       v = p[v]; n     } n     v = sink; n     max_flow += flow_path; n     res += flow_path * f[sink]; n     while (v != source) { n       capacity[p[v]][v] -= flow_path; n       capacity[v][p[v]] += flow_path; n       v = p[v]; n     } n   } n   cout << fixed << setprecision(9); n   if (max_flow != n - 1) n     cout << -1; n   else n     cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c; n   cin >> a >> b >> c; n   string ans =   ; n   int f; n   if (a > b) { n     ans +=  0 ; n     a--; n     f = 0; n   } else { n     ans +=  1 ; n     f = 1; n     b--; n   } n   for (int i = 1; i < c; i++) { n     if (f) { n       ans +=  0 ; n       a--; n       f = f ^ 1; n     } else { n       ans +=  1 ; n       b--; n       f = f ^ 1; n     } n   } n   if (!f) { n     while (a--) { n       ans +=  0 ; n     } n     f = f ^ 1; n     while (b--) { n       ans +=  1 ; n     } n   } else { n     while (b--) { n       ans +=  1 ; n     } n     while (a--) { n       ans +=  0 ; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200200; n int n; n int a[maxn], b[maxn], c[3][maxn]; n int sum[maxn]; n int lowbit(int x) { return x & (-x); } n void add(int p, int v = 1) { n   for (int i = p; i <= n; i += lowbit(i)) sum[i] += v; n } n int get(int p) { n   int ret = 0; n   for (int i = p; i; i -= lowbit(i)) ret += sum[i]; n   return ret; n } n bool on[maxn]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , a + i); n     a[i]++; n     int before = get(a[i]); n     c[0][i] = a[i] - 1 - before; n     add(a[i]); n   } n   memset(sum, 0, sizeof(sum)); n   for (int i = 0; i < n; i++) { n     scanf( %d , b + i); n     b[i]++; n     int before = get(b[i]); n     c[1][i] = b[i] - 1 - before; n     c[2][i] = c[0][i] + c[1][i]; n     add(b[i]); n   } n   for (int i = n - 1; i > 0; i--) { n     int j = n - 1 - i; n     if (c[2][i] > j) { n       c[2][i] -= j + 1; n       c[2][i - 1]++; n     } n   } n   c[2][0] %= n; n   memset(sum, 0, sizeof(sum)); n   memset(on, true, sizeof(on)); n   for (int i = 1; i <= n; i++) add(i); n   vector<int> vi; n   for (int i = 0; i < n; i++) { n     int goal = c[2][i]; n     int low = 0, high = n, ans = -1; n     while (low <= high) { n       int mid = (low + high) / 2; n       int s = get(mid); n       if (s == goal) { n         if (on[mid + 1] == true) { n           ans = mid + 1; n           break; n         } else n           low = mid + 1; n       } else if (s < goal) n         low = mid + 1; n       else if (s > goal) n         high = mid - 1; n     } n     on[ans] = false; n     add(ans, -1); n     vi.push_back(ans - 1); n   } n   for (int i = 0; i < n; i++) printf( %d  , vi[i]); n   putchar(10); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool vis[205][205]; n int cnt; n vector<int> ans; n void dfs(int x, int y) { n   if (x % 2 == 0 && y % 2 == 0) cnt++; n   vis[x][y] = 1; n   if (!vis[x + 1][y]) dfs(x + 1, y); n   if (!vis[x][y + 1]) dfs(x, y + 1); n   if (!vis[x - 1][y] && (x - 1 >= 0)) dfs(x - 1, y); n   if (!vis[x][y - 1] && (y - 1 >= 0)) dfs(x, y - 1); n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int w, h, n; n   cin >> w >> h >> n; n   for (int i = 0; i <= w * 2; i++) { n     vis[i][h * 2 - 1] = 1; n   } n   for (int i = 0; i <= h * 2; i++) { n     vis[w * 2 - 1][i] = 1; n   } n   for (int i = 0; i < n; i++) { n     int x0, y0, x1, y1; n     cin >> x0 >> y0 >> x1 >> y1; n     if (x0 == x1) { n       x0 *= 2; n       x0--; n       for (int j = max(y0 * 2 - 1, 0); j <= y1 * 2 - 1; j++) { n         vis[x0][j] = 1; n       } n     } else if (y0 == y1) { n       y0 *= 2; n       y0--; n       for (int j = max(x0 * 2 - 1, 0); j <= x1 * 2 - 1; j++) { n         vis[j][y0] = 1; n       } n     } n   } n   for (int i = 0; i <= w * 2 - 1; i++) { n     for (int j = 0; j <= h * 2 - 1; j++) { n       if (!vis[i][j]) { n         dfs(i, j); n         ans.push_back(cnt); n         cnt = 0; n       } n     } n   } n   sort(ans.begin(), ans.end()); n   for (auto i : ans) cout << i <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   bool remove = true; n   int ans = 0; n   while (remove) { n     remove = false; n     int idx; n     char c =   0 ; n     if (s.size() > 1) { n       for (int i = 0; i < s.size(); i++) { n         if (i > 0) { n           if (i == s.size() - 1) { n             if (s[i] - 1 == s[i - 1]) { n               remove = true; n               if (c < s[i]) { n                 idx = i; n                 c = s[i]; n               } n             } n           } else { n             if (s[i] - 1 == s[i - 1] || s[i] - 1 == s[i + 1]) { n               remove = true; n               if (c < s[i]) { n                 idx = i; n                 c = s[i]; n               } n             } n           } n         } else { n           if (s[i] - 1 == s[i + 1]) { n             remove = true; n             if (c < s[i]) { n               idx = i; n               c = s[i]; n             } n           } n         } n       } n     } n     if (remove) { n       s.erase(s.begin() + idx); n       ans++; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 50; n long long g[maxn]; n int ans, n; n inline int ctz(long long s) { n   return s ? __builtin_ctzll(s) : 8 * sizeof(long long); n } n void BronKerbosch(long long clique, long long allow, long long forbid) { n   if (!allow && !forbid) { n     ans = max(ans, __builtin_popcountll(clique)); n     return; n   } n   if (!allow) return; n   int pivot = ctz(allow | forbid); n   long long choose = allow & ~g[pivot]; n   for (int u = ctz(choose); u < n; u += ctz(choose >> (u + 1)) + 1) { n     BronKerbosch(clique | (1LL << u), allow & g[u], forbid & g[u]); n     allow ^= 1LL << u; n     forbid |= 1LL << u; n   } n } n int main(int argc, char const *argv[]) { n   ios_base::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   int k; n   long long x; n   cin >> n >> k; n   memset(g, 0, sizeof(g)); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       cin >> x; n       g[i] ^= (x << j); n     } n   } n   BronKerbosch(0LL, (1LL << n) - 1, 0LL); n   printf( %.9f n , 1.0 * k * k * (ans - 1) / (2 * ans)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 10; n int m, k, n, s, tot, del, a[N], b[N], c[N], d[N]; n int main() { n   scanf( %d%d%d%d , &m, &k, &n, &s); n   del = m - k * n; n   for (int i = 1; i <= m; i++) scanf( %d , &a[i]); n   for (int i = 1, x; i <= s; i++) { n     scanf( %d , &x); n     b[x]++; n     if (b[x] == 1) tot++; n   } n   for (int l = 1, r = 1; r <= m; r++) { n     ++c[a[r]]; n     tot -= c[a[r]] == b[a[r]]; n     while (!tot && r - l + 1 > k && l <= m && c[a[l]] > b[a[l]]) --c[a[l]], l++; n     if (!tot && r - l + 1 >= k) { n       int del2 = (l - 1) % k + r - l + 1 - k; n       if (del2 > del) continue; n       printf( %d n , del2); n       if (!del2) return 0; n       for (int i = 1; i <= (l - 1) % k && del2; i++) printf( %d  , i), del2--; n       for (int i = l; i <= r && del2; i++) n         if (d[a[i]] + 1 > b[a[i]]) n           printf( %d  , i), del2--; n         else n           d[a[i]]++; n       return 0; n     } n   } n   printf( -1 ); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)3e5 + 7; n int n, k; n int x[N], y[N]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     cin >> x[i]; n   } n   for (int i = 1; i <= n; i++) { n     cin >> y[i]; n   } n   long long f, s; n   f = s = 0; n   for (int i = 1; i <= n; i++) { n     f = max(0LL, x[i] + f - y[i] * 1LL * k); n     s = max(0LL, y[i] + s - x[i] * 1LL * k); n     if (f > k || s > k) { n       puts( NO ); n       return 0; n     } n   } n   puts( YES ); n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int> > a; n bool g[100][100]; n bool used[100]; n int n; n void dfs(int v) { n   used[v] = true; n   for (int i = 0; i < n; i++) n     if (g[v][i] && !used[i]) dfs(i); n } n int main() { n   int m; n   cin >> n >> m; n   bool now = false; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) g[i][j] = false; n   for (int i = 0; i < n; i++) used[i] = false; n   a.resize(n); n   for (int i = 0; i < n; i++) { n     int num; n     cin >> num; n     if (num != 0) now = true; n     a[i].resize(num); n     for (int j = 0; j < num; j++) { n       cin >> a[i][j]; n       a[i][j]--; n     } n   } n   for (int i = 0; i < n; i++) n     for (int j = i + 1; j < n; j++) n       for (int i1 = 0; i1 < a[i].size(); i1++) n         for (int j1 = 0; j1 < a[j].size(); j1++) n           if (a[i][i1] == a[j][j1]) { n             g[i][j] = true; n             g[j][i] = true; n           } n   int result = 0; n   for (int i = 0; i < n; i++) n     if (!used[i]) { n       dfs(i); n       result++; n     } n   if (!now) result++; n   cout << result - 1; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 512; n const int T = 101; n int n, m; n long long a[M][M]; n long long b[M][M]; n int t; n int x[T], y[T]; n void solve(int xl, int xr, int yl, int yr) { n   if (xl == xr && yl == yr) { n     b[xl][yl] = a[xl][yl]; n   } else if (xl != xr) { n     int tm = (xr - xl + 1); n     int um = (yr - yl + 1); n     for (int i = xl; i < xl + tm / 2; i++) { n       for (int j = yl; j <= yr; j++) a[i + tm / 2][j] ^= a[i][j]; n     } n     solve(xl + tm / 2, xr, yl, yr); n     for (int i = xl; i < xl + tm / 2; i++) { n       for (int j = yl; j <= yr; j++) a[i + tm / 2][j] ^= a[i][j]; n     } n     for (int i = xl + tm / 2; i <= xr; i++) { n       for (int j = yl; j <= yr; j++) { n         for (int k = 1; k <= t; k++) { n           if ((i + x[k]) % tm < tm / 2) n             a[xl + (i + x[k]) % tm][yl + (j + y[k]) % um] ^= b[i][j]; n         } n       } n     } n     solve(xl, xl + tm / 2 - 1, yl, yr); n     for (int i = xl + tm / 2; i <= xr; i++) { n       for (int j = yl; j <= yr; j++) { n         for (int k = 1; k <= t; k++) { n           if ((i + x[k]) % tm < tm / 2) n             a[xl + (i + x[k]) % tm][yl + (j + y[k]) % um] ^= b[i][j]; n         } n         b[i][j] ^= b[i - tm / 2][j]; n       } n     } n   } else { n     int tm = (xr - xl + 1); n     int um = (yr - yl + 1); n     for (int i = xl; i <= xr; i++) { n       for (int j = yl; j < yl + um / 2; j++) a[i][j + um / 2] ^= a[i][j]; n     } n     solve(xl, xr, yl + um / 2, yr); n     for (int i = xl; i <= xr; i++) { n       for (int j = yl; j < yl + um / 2; j++) a[i][j + um / 2] ^= a[i][j]; n     } n     for (int i = xl; i <= xr; i++) { n       for (int j = yl + um / 2; j <= yr; j++) { n         for (int k = 1; k <= t; k++) { n           if ((j + y[k]) % um < um / 2) n             a[xl + (i + x[k]) % tm][yl + (j + y[k]) % um] ^= b[i][j]; n         } n       } n     } n     solve(xl, xr, yl, yl + um / 2 - 1); n     for (int i = xl; i <= xr; i++) { n       for (int j = yl + um / 2; j <= yr; j++) { n         for (int k = 1; k <= t; k++) { n           if ((j + y[k]) % um < um / 2) n             a[xl + (i + x[k]) % tm][yl + (j + y[k]) % um] ^= b[i][j]; n         } n         b[i][j] ^= b[i][j - um / 2]; n       } n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   m = 1 << n; n   for (int i = 0; i < m; i++) { n     for (int j = 0; j < m; j++) { n       cin >> a[i][j]; n     } n   } n   cin >> t; n   for (int i = 1; i <= t; i++) { n     cin >> x[i] >> y[i]; n   } n   solve(0, m - 1, 0, m - 1); n   int ans = 0; n   for (int i = 0; i < m; i++) { n     for (int j = 0; j < m; j++) { n       ans += (b[i][j] != 0); n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e6 + 2; n int pos[MAX], a[MAX]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, c = 0, k; n   cin >> n; n   for (int i = 1; i < n + 1; i++) cin >> k, a[k] = i, pos[i] = k; n   for (int i = 1; i < n + 1; i++) { n     if (pos[i] != i) { n       c++; n       int j = pos[i]; n       int k = a[i]; n       pos[k] = j; n       a[j] = k; n       a[i] = i; n       pos[i] = i; n     } n   } n   if ((3 * n + c) & 1) n     cout <<  Um_nik ; n   else n     cout <<  Petr ; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000000], psum[1000000]; n map<long long, long long> mp, pp; n vector<long long> v; n int main() { n   std::ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   long long t; n   cin >> t; n   while (t--) { n     int n, k; n     cin >> n >> k; n     for (int i = 0; i < n; i++) cin >> a[i]; n     for (int i = 0; i < (2 * k + 2); i++) { n       psum[i] = 0; n     } n     for (int i = 0; i < n / 2; i++) { n       int mn = min(a[i], a[n - i - 1]); n       int mx = max(a[i], a[n - i - 1]); n       psum[1] += 2; n       psum[mn + 1] -= 1; n       psum[mn + mx] -= 1; n       psum[mx + mn + 1] += 1; n       psum[mx + k + 1] += 1; n     } n     int ans = INT_MAX, sum = 0; n     for (int i = 1; i <= (2 * k + 1); i++) psum[i] += psum[i - 1]; n     for (int i = 1; i < (2 * k + 1); i++) ans = min(ans, psum[i]); n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using vi = vector<int>; n using vvi = vector<vi>; n using ll = long long; n using vll = vector<ll>; n using vpi = vector<pair<int, int>>; n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   int speedlim = 301; n   bool overtake = true; n   int speed = -1; n   int noovertakes = 0; n   vi speedsigns(302, 0); n   int ignore = 0; n   for (int i = 0; i < n; ++i) { n     int t; n     cin >> t; n     switch (t) { n       case 1: n         int newspeed; n         cin >> newspeed; n         assert(newspeed <= 300 && newspeed >= 1); n         if (newspeed > speed && newspeed > speedlim) { n           ignore += speedsigns[newspeed]; n           for (int i = newspeed + 1; i < speedsigns.size(); ++i) { n             speedsigns[i] -= speedsigns[newspeed]; n           } n           for (int i = 0; i <= newspeed; ++i) { n             speedsigns[i] = 0; n           } n         } n         speed = newspeed; n         break; n       case 2: n         if (!overtake) { n           ignore += noovertakes; n           noovertakes = 0; n         } n         break; n       case 3: n         int newspeedlim; n         cin >> newspeedlim; n         assert(newspeedlim <= 300 && newspeedlim >= 1); n         if (newspeedlim < speed) n           ++ignore; n         else { n           for (int i = 0; i <= newspeedlim; ++i) speedsigns[i] = 0; n           for (int i = newspeedlim + 1; i < speedsigns.size(); ++i) n             ++speedsigns[i]; n         } n         speedlim = newspeedlim; n         break; n       case 4: n         overtake = true; n         noovertakes = 0; n         break; n       case 5: n         speedlim = 301; n         for (int& si : speedsigns) si = 0; n         break; n       case 6: n         overtake = false; n         ++noovertakes; n         break; n     } n   } n   cout << ignore <<   n ; n } n


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


#include <bits/stdc++.h> n using namespace std; n map<pair<string, string>, pair<int, int> > mp; n set<pair<string, string> > st; n int main() { n   int n, d; n   cin >> n >> d; n   for (int i = 0; i < n; ++i) { n     string s, q; n     int x; n     cin >> s >> q; n     scanf( %d , &x); n     if (mp.count({q, s})) { n       pair<int, int> a = mp[{q, s}]; n       int y = x - a.first; n       if (y <= d && y > 0) st.insert({min(s, q), max(s, q)}); n       y = x - a.second; n       if (y <= d && y > 0) st.insert({min(s, q), max(s, q)}); n     } n     if (!mp.count({s, q})) n       mp[{s, q}] = {x, x}; n     else { n       pair<int, int> a = mp[{s, q}]; n       if (x != a.first) a.second = a.first, a.first = x; n       mp[{s, q}] = a; n     } n   } n   cout << st.size() << endl; n   for (set<pair<string, string> >::iterator it = st.begin(); it != st.end(); n        it++) n     printf( %s %s n , it->first.c_str(), it->second.c_str()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int len[500020]; n vector<int> s[500020]; n vector<int> g[500020]; n int col[500020]; n set<int> FK; n int c; n int rt; n void dfs(int u, int p) { n   if (p) { n     vector<int> t; n     int k = c; n     for (auto v : s[u]) { n       if (!col[v]) continue; n       FK.erase(col[v]); n       t.push_back(col[v]); n     } n     k = 0; n     set<int>::iterator it = FK.begin(); n     for (auto v : s[u]) { n       if (col[v]) continue; n       col[v] = *it; n       ++it; n     } n     for (auto v : t) { n       FK.insert(v); n     } n     t.clear(); n   } n   for (auto v : g[u]) { n     if (v == p) continue; n     dfs(v, u); n   } n } n int main() { n   scanf( %d%d , &n, &m); n   rt = 0; n   int tmp = 0; n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &len[i]); n     for (int j = 1; j <= len[i]; ++j) { n       int v; n       scanf( %d , &v); n       s[i].push_back(v); n     } n     if (len[i] > tmp) { n       tmp = len[i], rt = i; n     } n   } n   for (int i = 1; i < n; ++i) { n     int u, v; n     scanf( %d%d , &u, &v); n     g[u].push_back(v); n     g[v].push_back(u); n   } n   int t = 0; n   for (auto v : s[rt]) { n     col[v] = ++t; n   } n   c = max(tmp, 1); n   for (int i = 1; i <= c; ++i) FK.insert(i); n   dfs(rt, 0); n   for (int i = 1; i <= m; ++i) n     if (!col[i]) col[i] = 1; n   printf( %d n , c); n   for (int i = 1; i <= m; ++i) printf( %d%c , col[i], i == m ?   n  :    ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long rate[2000002]; n int n, k; n int main() { n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> rate[i]; n   long long p = 0, c; n   int del = 0; n   vector<int> vec; n   for (int i = 1; i <= n; i++) { n     if (i - del > 1) n       c = (-rate[i - 1] * ((n - del) - (i - del))) * (i - del - 1) + p; n     else n       c = 0; n     if (c < k) n       del++, vec.push_back(i); n     else n       p += rate[i - 1] * (i - del - 1); n   } n   for (int i = 0; i < vec.size(); i++) cout << vec[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct wektor { n   int roz; n   int *tab; n   int roztab; n   wektor() { n     tab = new int[1]; n     roz = 0; n     roztab = 1; n   } n   ~wektor() { delete[] tab; } n   int size() { return roz; } n   bool empty() { return roz == 0; } n   void pop_back() { roz--; } n   int &operator[](int ind) { return tab[ind]; } n   int operator[](int ind) const { return tab[ind]; } n   int back() { return tab[roz - 1]; } n   void resize(int cos) { n     if (cos > roztab) { n       int *tmp; n       tmp = new int[2 * cos]; n       for (int i = 0; i < roz; i++) { n         tmp[i] = tab[i]; n       } n       delete[] tab; n       tab = tmp; n       roztab = 2 * cos; n     } else { n       if (cos < roz) { n         roz = cos; n       } n     } n   } n   void push_back(int elem) { n     resize(roz + 1); n     tab[roz] = elem; n     roz = roz + 1; n     ; n   } n }; n wektor slo[100005]; n int vis[100005]; n int gleb[100005]; n double wynik; n void dfs(int v, int g) { n   wynik += double(1) / g; n   vis[v] = 1; n   for (int i = 0; i < slo[v].size(); i++) { n     int aktv = slo[v][i]; n     if (vis[aktv] == 0) { n       dfs(aktv, g + 1); n     } n   } n } n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n - 1; i++) { n     int a, b; n     cin >> a >> b; n     slo[a].push_back(b); n     slo[b].push_back(a); n   } n   dfs(1, 1); n   printf( %.7lf n , wynik); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double kEps(1e-8); n #pragma gcc optimize(2) n int gcd(int x, int y) { n   while (y ^= x ^= y ^= x %= y) n     ; n   return x; n } n void write(int x) { n   if (x < 0) putchar( - ), x = -x; n   if (x > 9) write(x / 10); n   putchar(x % 10 +  0 ); n } n int qpow(int m, int n) { n   int res = 1; n   while (n) { n     if (n & 1) { n       res *= m; n     } n     n >>= 1; n     m *= m; n   } n   return res; n } n int max(int a, int b) { n   if (a > b) { n     return a; n   } else n     return b; n } n int read() { n   int x = 0, f = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) x = x * 10 + c -  0 , c = getchar(); n   return x * f; n } n signed main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     long long ans = 1; n     map<long long, long long> mp; n     for (long long i = 2; i < n; ++i) { n       if (i * i > n) { n         break; n       } else { n         if (i * i <= n) { n           ans++; n         } n         if (i * i * i <= n) { n           ans++; n         } n         mp[i * i]++; n         mp[i * i * i]++; n       } n     } n     long long temp = 0; n     for (auto it : mp) { n       if (it.second > 1) { n         temp++; n       } n     } n     mp.clear(); n     cout << ans - temp <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int SIZE = 100005; n const int MSIZE = 10000; n const int INF = 1 << 30; n set<int>::iterator it; n int main() { n   int n, a, b; n   int p[SIZE]; n   set<int> fi, se; n   map<int, int> mp; n   bool flag[SIZE]; n   while (cin >> n >> a >> b) { n     fi.clear(), se.clear(), mp.clear(); n     memset(flag, 0, sizeof(flag)); n     for (int i = 0; i < n; i++) { n       scanf( %d , &p[i]); n       fi.insert(p[i]); n       mp[p[i]] = i; n     } n     vector<int> v; n     for (it = fi.begin(); it != fi.end(); it++) { n       if (fi.find(a - *it) == fi.end()) { n         se.insert(*it); n         v.push_back(*it); n       } n     } n     for (int i = 0; i < v.size(); i++) { n       fi.erase(v[i]); n       se.insert(v[i]); n     } n     bool mark = 0; n     while (!se.empty()) { n       it = se.begin(); n       int x = *it; n       if (se.find(b - x) != se.end()) { n         flag[mp[x]] = flag[mp[b - x]] = 1; n         se.erase(x); n         if ((x)*2 != b) se.erase(b - x); n       } else if (fi.find(b - x) != fi.end()) { n         flag[mp[x]] = flag[mp[b - x]] = 1; n         fi.erase(b - x); n         se.erase(x); n         if ((b - x) * 2 != a) { n           fi.erase(a - (b - x)); n           se.insert(a - (b - x)); n         } n       } else { n         puts( NO ); n         mark = 1; n         break; n       } n     } n     if (mark) continue; n     puts( YES ); n     for (int i = 0; i < n; i++) printf( %d  , flag[i]); n     puts(  ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char *argv[]) { n   int n, a, d; n   cin >> n >> a >> d; n   int t, v; n   double T, P = 0, D; n   while (n--) { n     cin >> t >> v; n     T = 1.0 * v / a; n     if (T * v <= 2 * d) { n       D = (1.0 * d - T * v * 0.5) / v; n       T = T + D; n     } else { n       T = sqrt(2.0 * d / a); n     } n     T += t; n     if (T < P) T = P; n     printf( %.7f n , T); n     P = T; n   } n   return EXIT_SUCCESS; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n long long fac[2000001]; n long long PowMod(long long a, long long b, long long mod) { n   long long ret = 1LL; n   while (b) { n     if (b & 1) ret = (ret * a) % mod; n     a = (a * a) % mod; n     b >>= 1; n   } n   return ret; n } n void cfac() { n   fac[0] = 1; n   for (long long i = 1; i <= 200000; i++) fac[i] = i * fac[i - 1] % mod; n } n long long Lucas(long long n, long long m, long long p = mod) { n   long long ret = 1; n   if (n == 0) return 1; n   while (n && m) { n     long long a = n % p, b = m % p; n     if (a < b) return 0; n     ret = (ret * fac[a] * PowMod(fac[b] * fac[a - b] % p, p - 2, p)) % p; n     n /= p; n     m /= p; n   } n   return ret; n } n long long pre[2002][2002]; n long long dp[2002]; n struct data { n   long long x, y; n   bool operator<(const data& b) const { return y == b.y ? x < b.x : y < b.y; } n } Q[2002]; n int main() { n   ios::sync_with_stdio(0); n   cfac(); n   int h, w, n, l; n   cin >> h >> w >> n; n   for (int i = 0; i < n; i++) { n     cin >> Q[i].x >> Q[i].y; n   } n   sort(Q, Q + n); n   long long ans = 0LL; n   for (int i = 0; i < n; i++) { n     dp[i] += Lucas(Q[i].x - 1 + Q[i].y - 1, Q[i].x - 1); n     dp[i] %= mod; n     if (dp[i] < 0) dp[i] += mod; n     for (int j = i + 1; j < n; j++) { n       if (Q[j].x < Q[i].x) continue; n       long long temp = n           Lucas(Q[j].x + Q[j].y - Q[i].y - Q[i].x, Q[j].x - Q[i].x); n       pre[i][j] = temp; n       dp[j] -= temp * dp[i]; n       dp[j] %= mod; n       if (dp[j] < 0) dp[j] += mod; n     } n   } n   long long temp = Lucas(h + w - 2, h - 1); n   for (int i = n - 1; i >= 0; i--) { n     temp -= dp[i] * Lucas(h + w - Q[i].x - Q[i].y, h - Q[i].x); n     temp %= mod; n     if (temp < 0) temp += mod; n     temp %= mod; n     if (temp < 0) temp += mod; n   } n   cout << temp << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1002][102]; n int main() { n   int n, m, k, x; n   cin >> n >> m >> k; n   cout << m * (m - 1) / 2 << endl; n   for (int i = 0; i < m; i++) { n     for (int j = i + 1; j < m; j++) { n       if (!k) n         cout << i + 1 <<     << j + 1 << endl; n       else n         cout << j + 1 <<     << i + 1 << endl; 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.precision(10); n   int n; n   cin >> n; n   int m; n   cin >> m; n   std::vector<std::vector<long long> > re(n, std::vector<long long>(m, 0)); n   long long ta = 0; n   for (int i = 0; i < n; ++i) { n     cin >> re[i][0]; n     ta ^= re[i][0]; n   } n   long long tb = 0; n   long long a; n   for (int i = 0; i < m; ++i) { n     cin >> a; n     re[0][i] ^= a; n     tb ^= a; n   } n   if (tb != ta) { n     cout <<  NO ; n     exit(0); n   } n   re[0][0] ^= ta; n   cout <<  YES n ; n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) cout << re[i][j] <<   n  [j != m - 1]; n } n


#include <bits/stdc++.h> n using namespace std; n template <class htpe, class cmp> n using heap = priority_queue<htpe, vector<htpe>, cmp>; n template <class htpe> n using min_heap = heap<htpe, greater<htpe> >; n template <class htpe> n using max_heap = heap<htpe, less<htpe> >; n const int INF = 1791791791; n const long long INFLL = 1791791791791791791ll; n bool query(int x, int y) { n   cout << 1 <<     << x <<     << y << endl; n   cout.flush(); n   string s; n   cin >> s; n   return s ==  TAK ; n } n bool is_two_answers(int a, int b) { return query(a, b) && query(b, a); } n int find_on_segment(int l, int r, int tr) { n   l--; n   while (l < r - 1) { n     int mid = (l + r + tr) >> 1; n     if (!query(mid, mid + 1)) n       l = mid; n     else n       r = mid; n   } n   return r; n } n int main() { n   int n, k; n   cin >> n >> k; n   int a = find_on_segment(1, n, 0); n   int b = find_on_segment(a + 1, n, 1); n   if (a + 1 > n || !is_two_answers(a, b)) b = find_on_segment(1, a - 1, 0); n   cout << 2 <<     << a <<     << b << endl; n   cout.flush(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string to_string(const string& s) { return     + s +    ; } n string to_string(const char* s) { return to_string((string)s); } n string to_string(bool b) { return (b ?  true  :  false ); } n string to_string(vector<bool> v) { n   bool first = true; n   string res =  { ; n   for (long long i = 0; i < static_cast<long long>(v.size()); i++) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(v[i]); n   } n   res +=  } ; n   return res; n } n template <size_t N> n string to_string(bitset<N> v) { n   string res =   ; n   for (size_t i = 0; i < N; i++) { n     res += static_cast<char>( 0  + v[i]); n   } n   return res; n } n template <class A, class B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <class A, class B, class C> n string to_string(tuple<A, B, C> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ) ; n } n template <class A, class B, class C, class D> n string to_string(tuple<A, B, C, D> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ,   + to_string(get<3>(p)) +  ) ; n } n template <class A> n string to_string(A v) { n   bool first = true; n   string res =  { ; n   for (const auto& x : v) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n template <class Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<   :   << to_string(arg1) <<   n ; n } n template <class Arg1, class... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<   :   << to_string(arg1) <<   |  ; n   __f(comma + 1, args...); n } n const long long inf = (long long)4e18; n long long sum(long long x) { n   long long s = 0; n   for (; x; x /= 10) { n     s += (x % 10); n   } n   return s; n } n long long to_int(string s) { n   long long res = 0; n   for (long long i = 0; i < (long long)s.size(); i++) { n     res *= 10; n     res += s[i] -  0 ; n   } n   return res; n } n void solve() { n   long long n, k; n   cin >> n >> k; n   long long ans = inf; n   for (long long d = 0; d < 10; d++) { n     for (long long cnt = 0; cnt < 18; cnt++) { n       long long cur = cnt * 9 + d; n       if (cur > n) break; n       long long x = 0; n       for (long long i = 0; i < cnt; i++) { n         x += 9; n         x *= 10; n       } n       x += d; n       long long su = 0; n       for (long long i = 0; i <= k; i++) { n         su += sum(x + i); n       } n       69; n       su -= max(0LL, k + 1 - (10 - d)); n       if (su > n) continue; n       for (long long d1 = 0; d1 < 9; d1++) { n         long long p = min(k + 1, 10 - d); n         long long new_su = su + d1 * p + (d1 + 1) * (k + 1 - p); n         if (new_su > n) continue; n         if (new_su == n) { n           if (d1 > 0) { n             string tmp = to_string(d1) + to_string(x); n             ans = min(ans, to_int(tmp)); n           } else { n             ans = min(ans, x); n           } n           continue; n         } n         long long rem = n - new_su; n         if (rem % (k + 1) != 0) continue; n         rem /= (k + 1); n         long long f = rem % 9; n         rem -= f; n         for (long long it = 0; it < rem / 9; it++) { n           f *= 10; n           f += 9; n         } n         if (f > 0) { n           f *= 10; n           f += d1; n         } n         69; n         if (f == 0) { n           ans = min(ans, x); n         } else { n           string q = to_string(f) + to_string(x); n           ans = min(ans, to_int(q)); n         } n       } n     } n   } n   if (ans == inf) { n     cout <<  -1 n ; n   } else { n     cout << ans <<   n ; n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   long long tt; n   cin >> tt; n   while (tt--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 13e9; n const long long dd = 1e6 + 7; n int main() { n   int n, s, r = -1; n   cin >> n >> s; n   s *= 100; n   for (int i = 0; i < (long long)n; i++) { n     int x, y; n     cin >> x >> y; n     x = x * 100 + y; n     if (x <= s) r = max((100 - y) % 100, r); n   } n   cout << r; n } n


#include <bits/stdc++.h> n using namespace std; n long double PI = acosl(-1); n bool compare_int(int a, int b) { return (a > b); } n bool compare_string(string a, string b) { return a.size() < b.size(); } n bool compare_pair(const pair<int, int> &a, const pair<int, int> &b) { n   if (a.second == b.second) n     return a.first < b.first; n   else n     return (a.second > b.second); n } n bool cmp(pair<string, int> x, pair<string, int> y) { n   return (x.second < y.second); n } n void NA() { n   printf( NO n ); n   exit(0); n } n void YA() { n   printf( YES n ); n   exit(0); n } n const int N = 300005; n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, x, y, q; n   cin >> n; n   int sz = sqrt(n); n   long long int a[n + 1], tmp[n + 1]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   cin >> q; n   vector<long long int> res(q); n   vector<pair<int, pair<int, int> > > v; n   for (int i = 0; i < q; i++) { n     cin >> x >> y; n     v.push_back({y, {x, i}}); n   } n   sort(v.begin(), v.end()); n   int pre = -1; n   for (int i = 0; i < q; i++) { n     x = v[i].second.first - 1; n     y = v[i].first; n     int id = v[i].second.second; n     if (y >= sz) { n       for (int i = x; i < n; i += y) res[id] += a[i]; n     } else { n       if (pre != y) { n         memset(tmp, 0, sizeof tmp); n         for (int i = n - 1; i >= 0; i--) { n           tmp[i] += a[i]; n           if (i + y < n) tmp[i] += tmp[i + y]; n         } n         pre = y; n       } n       res[id] = tmp[x]; n     } n   } n   for (int i = 0; i < q; i++) { n     cout << res[i] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n set<int> st[2]; n map<int, int> mp; n int ar[110]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> ar[i]; n     mp[ar[i]]++; n   } n   int idx = 0; n   map<int, int>::iterator it1; n   for (it1 = mp.begin(); it1 != mp.end(); it1++) { n     if (it1->second == 1) { n       st[idx].insert(it1->first); n       idx = !idx; n     } n   } n   if (st[0].size() == st[1].size()) { n     string ans(n,  A ); n     for (int i = 0; i < n; i++) { n       if (st[1].count(ar[i])) ans[i] =  B ; n     } n     cout <<  YES n ; n     for (int i = 0; i < n; i++) cout << ans[i]; n     return 0; n   } else { n     string res(n,  A ); n     for (int i = 0; i < n; i++) { n       if (st[1].count(ar[i])) { n         res[i] =  B ; n       } n     } n     int id = -1; n     map<int, int>::iterator it; n     for (it = mp.begin(); it != mp.end(); it++) { n       if (it->second >= 3) { n         id = it->first; n         break; n       } n     } n     if (id == -1) { n       cout <<  NO ; n       return 0; n     } n     bool flag = false; n     for (int i = 0; i < n; i++) { n       if (ar[i] == id) { n         if (!flag) { n           flag = true; n           res[i] =  B ; n         } n       } n     } n     cout <<  YES n ; n     for (int i = 0; i < n; i++) { n       cout << res[i]; n     } n     return 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int len; n set<string> v; n bool check[111111][5]; n void fuck(string father, int now) { n   if (now - 2 > 4) { n     string now1 = s.substr(now - 2, 2); n     if (now1 != father) { n       v.insert(now1); n       if (check[now - 2][2] == true) n         check[now - 2][2] = false, fuck(now1, now - 2); n     } n   } n   if (now - 3 > 4) { n     string now2 = s.substr(now - 3, 3); n     if (now2 != father) { n       v.insert(now2); n       if (check[now - 3][3] == true) n         check[now - 3][3] = false, fuck(now2, now - 3); n     } n   } n } n int main() { n   cin >> s; n   memset(check, true, sizeof(check)); n   len = s.length(); n   fuck(  , len); n   cout << v.size() << endl; n   for (set<string>::iterator it = v.begin(); it != v.end(); it++) n     cout << *it << endl; n } n


#include <bits/stdc++.h> n using namespace std; n inline char nc() { n   return getchar(); n   static char buf[100000], *l = buf, *r = buf; n   return l == r && (r = (l = buf) + fread(buf, 1, 100000, stdin), l == r) n              ? EOF n              : *l++; n } n template <class T> n void read(T &x) { n   x = 0; n   int f = 1, ch = nc(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = nc(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 -  0  + ch; n     ch = nc(); n   } n   x *= f; n } n int N, M, D, A[100050][50]; n int cnt, id[100050][50]; n pair<int, int> bel[5000050]; n int head[5000050]; n int head1[5000050]; n struct edge { n   int to, nex; n   edge(int to = 0, int nex = 0) : to(to), nex(nex) {} n }; n vector<edge> G; n vector<edge> G1; n inline void addedge(int u, int v) { n   G.push_back(edge(v, head[u])), head[u] = G.size() - 1; n } n inline void addedge1(int u, int v) { n   G1.push_back(edge(v, head1[u])), head1[u] = G1.size() - 1; n } n int dfn[5000050], low[5000050], dfc; n int sccno[5000050], val[5000050], scnt; n int vis[100050]; n int stk[5000050], top; n void tarjan(int u) { n   low[u] = dfn[u] = ++dfc; n   stk[++top] = u; n   for (int i = head[u]; ~i; i = G[i].nex) { n     int v = G[i].to; n     if (!dfn[v]) { n       tarjan(v); n       low[u] = min(low[u], low[v]); n     } else if (!sccno[v]) { n       low[u] = min(low[u], dfn[v]); n     } n   } n   if (low[u] == dfn[u]) { n     ++scnt; n     while (true) { n       int x = stk[top--]; n       sccno[x] = scnt; n       int a = bel[x].first, b = bel[x].second; n       if (A[a][b] && vis[a] != scnt) { n         vis[a] = scnt; n         ++val[scnt]; n       } n       if (x == u) break; n     } n   } n } n void find_scc() { n   for (int i = 1; i <= cnt; ++i) n     if (!dfn[i]) { n       tarjan(i); n     } n   memset(head1, -1, sizeof(head1)); n   for (int u = 1; u <= cnt; ++u) { n     for (int i = head[u]; ~i; i = G[i].nex) { n       int v = G[i].to; n       if (sccno[u] != sccno[v]) { n         addedge1(sccno[u], sccno[v]); n       } n     } n   } n } n int f[5000050]; n void dp(int u) { n   if (f[u]) return; n   for (int i = head1[u]; ~i; i = G1[i].nex) { n     int v = G1[i].to; n     dp(v); n     f[u] = max(f[u], f[v]); n   } n   f[u] += val[u]; n } n int solve() { n   find_scc(); n   int u = sccno[id[1][0]]; n   dp(u); n   return f[u]; n } n char str[60]; n int main() { n   read(N), read(M), read(D); n   for (int i = 1; i <= N; ++i) { n     for (int j = 0; j < D; ++j) { n       id[i][j] = ++cnt; n       bel[cnt] = pair<int, int>(i, j); n     } n   } n   memset(head, -1, sizeof(head)); n   for (int i = 1; i <= M; ++i) { n     int u, v; n     read(u), read(v); n     for (int j = 1; j < D; ++j) { n       addedge(id[u][j - 1], id[v][j]); n     } n     addedge(id[u][D - 1], id[v][0]); n   } n   for (int i = 1; i <= N; ++i) { n     scanf( %s , str); n     for (int j = 0; j < D; ++j) { n       A[i][j] = str[j] ==  1 ; n     } n   } n   printf( %d n , solve()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mn = 1e6 + 10; n int a[mn * 3]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) a[i] = i; n   for (int i = 2; i <= n; i++) { n     int v = a[i - 1]; n     for (int j = i - 1; j < n + i - 1; j += i) { n       swap(v, a[min(j + i, n + i - 1)]); n     } n   } n   for (int i = n; i <= n + n - 1; i++) printf( %d  , a[i]); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 106, INF = 0x3f3f3f3f; n int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n int n, a[N << 1]; n int couple[N][2]; n int main() { n   n = read(); n   for (int i = 1; i <= n << 1; i++) { n     a[i] = read(); n     if (!couple[a[i]][0]) n       couple[a[i]][0] = i; n     else n       couple[a[i]][1] = i; n   } n   int ans = 0; n   for (int i = 1; i <= (n << 1); i += 2) { n     if (a[i] == a[i + 1]) n       continue; n     else { n       ans += couple[a[i]][1] - couple[a[i]][0] - 1; n       for (int j = couple[a[i]][1] - 1; j >= i; j--) n         if (couple[a[j]][0] == j) n           a[++couple[a[j]][0]] = a[j]; n         else n           a[++couple[a[j]][1]] = a[j]; n       a[couple[a[i]][1] = i + 1] = a[i]; n     } n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n long long fact[1000001]; n long long pow(long long a, long long b, long long m) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans = (ans * a) % m; n     b /= 2; n     a = (a * a) % m; n   } n   return ans; n } n long long modinv(long long k) { return pow(k, 998244353 - 2, 998244353); } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n; n   cin >> n; n   fact[0] = 1; n   for (long long i = 1; i < 1000001; i++) n     fact[i] = (fact[i - 1] * i) % 998244353; n   long long ans = 0; n   ans += fact[n]; n   if (n - 2 > 0) ans = (ans + (n - 2) * fact[n]) % 998244353; n   for (long long i = 1; i < n - 1; i++) { n     ans = (ans - fact[n] * modinv(fact[n - i])) % 998244353; n   } n   cout << (ans + 998244353) % 998244353 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[100010], cewa, now; 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   now = 0; n   for (int i = 0; i < n; i++) { n     if (now > a[i]) { n       cewa++; n       now -= a[i]; n     } n     now += a[i]; n   } n   cout << n - cewa <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int destroy(list<int> row) { n   int totalDestroyed = 0, lastDestroyed = -1; n   int combo = 1; n   while (lastDestroyed != 0) { n     lastDestroyed = 0; n     combo = 1; n     list<int>::iterator it = row.begin(); n     list<int>::iterator prev = it++; n     for (; it != row.end(); ++it) { n       if (*prev == *it) { n         combo++; n       } else { n         if (combo >= 3) { n           it = row.erase(prev, it); n           lastDestroyed += combo; n         } n         prev = it; n         combo = 1; n       } n     } n     totalDestroyed += lastDestroyed; n   } n   totalDestroyed += (combo >= 3 ? combo : 0); n   return totalDestroyed > 0 ? totalDestroyed - 1 : 0; n } n int main() { n   int N, K, X; n   cin >> N >> K >> X; n   list<int> row; n   int ball; n   for (int i = 0; i < N; ++i) { n     cin >> ball; n     row.push_back(ball); n   } n   int maxDestroyed = 0; n   for (list<int>::iterator it = row.begin(); it != row.end(); ++it) { n     it = row.insert(it, X); n     int destroyed = destroy(row); n     maxDestroyed = max(maxDestroyed, destroyed); n     it = row.erase(it); n   } n   cout << maxDestroyed; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 112; n const long long mod = 1e9 + 7; n int R; n long long p1, p2, p3, p4, s; n int lim[N]; n long long F[N][N]; n long long ksm(long long a, long long b = mod - 2) { n   long long ret = 1; n   for (; b; b >>= 1, a = a * a % mod) n     if (b & 1) ret = ret * a % mod; n   return ret; n } n struct node { n   long long a[N]; n   node() { memset(a, 0, sizeof a); } n } a[N][N]; n node operator*(long long x, node a) { n   node c; n   for (int i = 0; i <= 2 * R + 1; i++) c.a[i] = a.a[i] * x % mod; n   return c; n } n node operator+(node a, node b) { n   node c; n   for (int i = 0; i <= 2 * R + 1; i++) c.a[i] = (a.a[i] + b.a[i]) % mod; n   return c; n } n node operator-(node a, node b) { n   node c; n   for (int i = 0; i <= 2 * R + 1; i++) c.a[i] = (a.a[i] - b.a[i] + mod) % mod; n   return c; n } n void solve() { n   int n = 2 * R; n   for (int i = 0; i <= n; i++) { n     int maxx = i; n     for (int j = i + 1; j <= n; j++) n       if (abs(F[j][i]) > abs(F[maxx][i])) maxx = j; n     swap(F[maxx], F[i]); n     for (int j = 0; j <= n; j++) { n       if (j != i) { n         long long x = F[j][i] * ksm(F[i][i]) % mod; n         for (int k = i; k <= n + 1; k++) n           F[j][k] = (F[j][k] - F[i][k] * x) % mod; n       } n     } n   } n   for (int i = 0; i <= n; i++) F[i][n + 1] = F[i][n + 1] * ksm(F[i][i]) % mod; n } n int main() { n   scanf( %d%lld%lld%lld%lld , &R, &p1, &p2, &p3, &p4); n   s = p1 + p2 + p3 + p4; n   s = ksm(s); n   p1 = p1 * s % mod; n   p2 = p2 * s % mod; n   p3 = p3 * s % mod; n   p4 = p4 * s % mod; n   int T = R + 3; n   p4 = ksm(p4); n   for (int i = 0; i <= R; i++) n     for (int j = 0; j * j + i * i <= R * R; j++) lim[i] = j; n   for (int j = -R; j <= R + 1; j++) n     for (int i = -R; i <= R; i++) { n       int lt = lim[abs(i)]; n       if (j == -lt) n         a[i + T][-lt + T].a[i + R] = 1; n       else if (j > -lt && j <= lt + 1) { n         a[i + T][j + T] = n             (a[i + T][j - 1 + T] - p1 * a[i - 1 + T][j - 1 + T] - n              p2 * a[i + T][j - 2 + T] - p3 * a[i + 1 + T][j - 1 + T]); n         (a[i + T][j + T].a[2 * R + 1])--; n         (a[i + T][j + T].a[2 * R + 1]) = (a[i + T][j + T].a[2 * R + 1]) % mod; n         a[i + T][j + T] = p4 * a[i + T][j + T]; n       } n     } n   for (int i = -R; i <= R; i++) { n     for (int j = 0; j <= 2 * R + 1; j++) n       F[i + R][j] = a[i + T][lim[abs(i)] + 1 + T].a[j]; n     F[i + R][2 * R + 1] = -F[i + R][2 * R + 1]; n   } n   solve(); n   long long ans = 0; n   for (int i = 0; i <= 2 * R; i++) n     ans = (ans + a[T][T].a[i] * F[i][2 * R + 1]) % mod; n   ans = (ans + a[T][T].a[2 * R + 1]) % mod; n   ans = (ans + mod) % mod; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n template <class T, class U> n ostream& operator<<(ostream& o, const pair<T, U>& p) { n   o <<  (  << p.first <<  ,  << p.second <<  ) ; n   return o; n } n template <class T> n ostream& operator<<(ostream& o, const vector<T>& v) { n   o <<  [ ; n   for (T t : v) { n     o << t <<  , ; n   } n   o <<  ] ; n   return o; n } n struct LazySegTree { n   int n; n   vector<ll> dat, lazy; n   LazySegTree(int _n) { n     n = 1; n     while (n < _n) n *= 2; n     dat = vector<ll>(2 * n - 1, LLONG_MAX / 2); n     lazy = vector<ll>(2 * n - 1, 0); n   } n   void show() { n     for (int(j) = 0; (j) < (int)(dat.size()); ++(j)) n       printf(  %d: dat= %lld, lazy= %lld n , j, dat[j], lazy[j]); n   } n   void setLazy(int k, ll v) { n     lazy[k] = v; n     dat[k] = v; n   } n   void push(int k, int l, int r) { n     if (lazy[k] != 0) { n       setLazy(2 * k + 1, lazy[k]); n       setLazy(2 * k + 2, lazy[k]); n     } n     lazy[k] = 0; n   } n   void fix(int k, int l, int r) { n     dat[k] = min(dat[2 * k + 1], dat[2 * k + 2]); n   } n   ll merge(ll x, ll y) { return min(x, y); } n   void _update(int a, int b, ll x, int k, int l, int r) { n     if (r <= a || b <= l) return; n     if (a <= l && r <= b) { n       setLazy(k, x); n       return; n     } n     push(k, l, r); n     _update(a, b, x, 2 * k + 1, l, (l + r) / 2); n     _update(a, b, x, 2 * k + 2, (l + r) / 2, r); n     fix(k, l, r); n   } n   void update(int a, int b, ll x) { return _update(a, b, x, 0, 0, n); } n   ll _query(int a, int b, int k, int l, int r) { n     if (r <= a || b <= l) return LLONG_MAX / 2; n     if (a <= l && r <= b) return dat[k]; n     push(k, l, r); n     ll vl = _query(a, b, 2 * k + 1, l, (l + r) / 2); n     ll vr = _query(a, b, 2 * k + 2, (l + r) / 2, r); n     return merge(vl, vr); n   } n   ll query(int a, int b) { return _query(a, b, 0, 0, n); } n }; n int main() { n   int n, k; n   cin >> n >> k; n   vector<int> b(n); n   for (int(i) = 0; (i) < (int)(n); ++(i)) cin >> b[i]; n   int q; n   cin >> q; n   vector<int> t(q), l(q), r(q), x(q, -1); n   for (int(i) = 0; (i) < (int)(q); ++(i)) { n     cin >> t[i] >> l[i] >> r[i]; n     --l[i]; n     --r[i]; n     if (t[i] == 1) cin >> x[i]; n   } n   vector<int> idx; n   for (int(i) = 0; (i) < (int)(q); ++(i)) { n     idx.push_back(l[i]); n     idx.push_back(r[i]); n   } n   idx.push_back(0); n   idx.push_back(n * k - 1); n   sort((idx).begin(), (idx).end()); n   idx.erase(unique((idx).begin(), (idx).end()), idx.end()); n   LazySegTree one(n); n   for (int(i) = 0; (i) < (int)(n); ++(i)) one.update(i, i + 1, b[i]); n   map<int, int> m; n   vector<int> comp; n   comp.push_back(b[0]); n   m[0] = 0; n   for (int i = 1; i < idx.size(); ++i) { n     if (idx[i] - idx[i - 1] > 1) { n       int L = (idx[i - 1] + 1) % n, R = (idx[i] + n - 1) % n; n       int v; n       if (L <= R) n         v = one.query(L, R + 1); n       else n         v = min(one.query(0, R + 1), one.query(L, n)); n       comp.push_back(v); n     } n     m[idx[i]] = comp.size(); n     comp.push_back(b[idx[i] % n]); n   } n   LazySegTree st(comp.size()); n   for (int(i) = 0; (i) < (int)(comp.size()); ++(i)) n     st.update(i, i + 1, comp[i]); n   for (int(i) = 0; (i) < (int)(q); ++(i)) { n     int L = m[l[i]], R = m[r[i]]; n     if (t[i] == 1) n       st.update(L, R + 1, x[i]); n     else n       cout << st.query(L, R + 1) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000001]; n int main() { n   int n, k; n   cin >> n >> k; n   if (k == 1) return puts( 3 ), 0; n   for (int i = 1; i <= n; i++) a[i] = i; n   for (int i = 2; i <= n; i++) n     if (a[i] == i) { n       for (int j = i; j <= n; j += i) a[j] = (a[j] / i) * (i - 1); n     } n   sort(a + 3, a + n + 1); n   cout << accumulate(a + 3, a + k + 3, 0ll) + 2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:134217728 ) n using namespace std; n const long long MOD = 1000000000 + 7; n const long long MAGIC = 123123123; n const double PI = 4 * atan(1.); n const double EPS = 1E-7; n void time_elapsed() { n   cout <<   nTIME ELAPSED:   << (double)clock() / CLOCKS_PER_SEC <<   sec n ; n } n struct frac; n template <typename T> n T gcd(T a, T b) { n   return ((b == 0) ? a : gcd(b, a % b)); n } n template <class T> n T gcd(T a, T b, T& x, T& y) { n   if (!a) { n     x = 0, y = 1; n     return b; n   } n   T x1, y1; n   T d = gcd(b % a, a, x1, y1); n   x = y1 - (b / a) * x1; n   y = x1; n   return d; n } n template <typename T> n T lcm(T a, T b) { n   return (a / gcd(a, b)) * b; n } n template <typename T, typename M> n T neg_mod(T a, M mod) { n   return ((a % mod) + mod) % mod; n } n long long binpow(long long x, long long p) { n   long long res = 1; n   while (p) { n     if (p & 1) res *= x; n     x *= x; n     p >>= 1; n   } n   return res; n } n long long binpow_mod(long long x, long long p, long long m) { n   long long res = 1; n   while (p) { n     if (p & 1) res = (res * x) % m; n     x = (x * x) % m; n     p >>= 1; n   } n   return res; n } n struct cmp_for_set { n   bool operator()(const pair<long long, long long>& a, n                   const pair<long long, long long>& b) const { n     return a > b; n   } n }; n string a, b; n map<char, int> amap; n bool check(int len, map<char, int>& need, string& res) { n   map<char, int> aa = amap; n   for (auto it : need) { n     if (aa[it.first] < it.second) { n       return false; n     } n     aa[it.first] -= it.second; n     if (aa[it.first] == 0) { n       aa.erase(it.first); n     } n   } n   res.resize(a.size()); n   for (int i = 0; i < len; ++i) { n     res[i] = b[i]; n   } n   if (len == b.size()) { n     return true; n   } n   char nxt =  0  - 1; n   for (auto it : aa) { n     if (it.first < b[len] && it.second > 0) { n       nxt = it.first; n     } n   } n   if (nxt ==  0  - 1) { n     return false; n   } n   res[len] = nxt; n   aa[nxt]--; n   vector<char> toadd; n   for (auto it : aa) { n     for (int j = 0; j < it.second; ++j) { n       toadd.push_back(it.first); n     } n   } n   for (int i = 0; i < toadd.size(); ++i) { n     res[len + 1 + i] = toadd[(int)toadd.size() - 1 - i]; n   } n   return true; n } n int main() { n   cin >> a >> b; n   for (int i = 0; i < a.size(); ++i) { n     amap[a[i]]++; n   } n   string res = a; n   sort(res.begin(), res.end()); n   if (a.size() < b.size()) { n     sort(res.rbegin(), res.rend()); n     cout << res; n     return 0; n   } n   map<char, int> need; n   for (int i = 0; i <= b.size(); ++i) { n     string cur; n     if (check(i, need, cur)) { n       res = cur; n     } n     need[b[i]]++; n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n const int MOD = (int)1e9 + 7; n using namespace std; n stack<long long> st; n int main() { n   long long n, num; n   cin >> n; n   long long sum = 0; n   int over = 0; n   st.push(1); n   for (int i = 1; i <= n; i++) { n     string s; n     cin >> s; n     if (over == 1) continue; n     if (s[0] ==  a ) { n       sum += st.top(); n       if (sum > 4294967295) { n         over = 1; n       } n     } else if (s[0] ==  f ) { n       cin >> num; n       long long f = st.top(); n       if (st.top() > 4294967295) n         st.push(st.top()); n       else n         st.push(f * num); n     } else { n       st.pop(); n     } n   } n   if (over == 1) n     cout <<  OVERFLOW!!!  << endl; n   else n     cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int k, n; n   cin >> k >> n; n   int ans = 0; n   unordered_set<int> pref_sums; n   int curr_sum = 0; n   while (k--) { n     int x; n     cin >> x; n     curr_sum += x; n     pref_sums.insert(curr_sum); n   } n   unordered_set<int> r; n   while (n--) { n     int x; n     cin >> x; n     r.insert(x); n   } n   for (int p : pref_sums) { n     int i = *begin(r) - p; n     bool check = true; n     for (int e : r) { n       if (!count(pref_sums.begin(), pref_sums.end(), e - i)) { n         check = false; n         break; n       } n     } n     if (check) ans++; n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn(2e5 + 5); n int n, m, s, a[maxn], bl[maxn], br[maxn], b[maxn], o[maxn], len, id[maxn], blk; n int mn[505][505], st[505][505], tag[505], w[maxn], pmn[maxn], smn[maxn]; n int curmn[505], curst[505], curtag[505]; n inline void Modify(int l, int r, int id) { n   int i; n   pmn[id] = smn[id] = 1 << 30; n   if (b[l] == b[r]) n     for (i = l; i <= r; ++i) ++w[i]; n   else { n     for (i = l; i <= br[l]; ++i) ++w[i]; n     for (i = bl[r]; i <= r; ++i) ++w[i]; n     for (i = b[l] + 1; i < b[r]; ++i) ++tag[i]; n   } n   for (i = bl[l]; i <= br[l]; ++i) pmn[id] = min(pmn[id], w[i]); n   for (i = bl[r]; i <= br[r]; ++i) smn[id] = min(smn[id], w[i]); n } n inline int Binary(int v) { n   int l = 1, r = n, mid, ret = -1; n   while (l <= r) { n     mid = (l + r) >> 1; n     if (o[a[id[mid]]] <= v) n       ret = mid, l = mid + 1; n     else n       r = mid - 1; n   } n   return ret; n } n inline int Query(int l, int r, int x) { n   int val = curst[b[l]], i, ret = 1 << 30; n   for (i = bl[l]; i < l; ++i) val += (a[i + m] <= x) - (a[i] <= x); n   for (i = l; i <= r; ++i) n     ret = min(ret, val), val += (a[i + m] <= x) - (a[i] <= x); n   return ret; n } n int main() { n   int i, j, ans = 0, l, r, x, ql, qr; n   scanf( %d%d , &n, &m), len = n; n   for (i = 1; i <= n; ++i) scanf( %d , &a[i]), o[i] = a[i], id[i] = i; n   sort(o + 1, o + len + 1), len = unique(o + 1, o + len + 1) - o - 1; n   for (i = 1; i <= n; ++i) a[i] = lower_bound(o + 1, o + len + 1, a[i]) - o; n   sort(id + 1, id + n + 1, [](int x, int y) { return a[x] < a[y]; }); n   for (blk = sqrt(n), i = 1; i <= n; ++i) n     b[i] = (i - 1) / blk + 1, bl[i] = b[i] == b[i - 1] ? bl[i - 1] : i; n   for (i = n; i; --i) br[i] = b[i] == b[i + 1] ? br[i + 1] : i; n   memset(mn, 63, sizeof(mn)), memset(mn[0], 0, sizeof(mn[0])); n   for (i = 1; i <= n; ++i) { n     Modify(max(1, id[i] - m + 1), id[i], i); n     if (b[i] != b[i + 1]) { n       for (j = 1; j <= n; ++j) w[j] += tag[b[j]]; n       for (j = 1; j <= b[n]; ++j) tag[j] = 0; n       for (j = 1; j <= n; ++j) { n         mn[b[i]][b[j]] = min(mn[b[i]][b[j]], w[j]); n         if (b[j] != b[j - 1]) st[b[i]][b[j]] = w[j]; n       } n     } n   } n   scanf( %d , &s); n   while (s--) { n     scanf( %d%d%d , &l, &r, &x), x ^= ans, x = Binary(x - 1); n     if (x < 0) { n       printf( %d n , ans = 0); n       continue; n     } n     for (i = 1; i <= b[n]; ++i) n       curmn[i] = mn[b[x] - 1][i], curst[i] = st[b[x] - 1][i], curtag[i] = 0; n     for (i = bl[x]; i <= x; ++i) { n       ql = max(1, id[i] - m + 1), qr = id[i]; n       curmn[b[ql]] = pmn[i], curmn[b[qr]] = smn[i]; n       if (b[ql] < b[qr]) ++curtag[b[ql] + 1], --curtag[b[qr]]; n       if (bl[ql] >= ql && (b[ql] ^ b[qr])) ++curst[b[ql]]; n       if (bl[qr] >= ql) ++curst[b[qr]]; n     } n     for (i = 1; i <= b[n]; ++i) n       curtag[i] += curtag[i - 1], curmn[i] += curtag[i], curst[i] += curtag[i]; n     if (b[l] == b[r]) n       printf( %d n , ans = Query(l, r, a[id[x]])); n     else { n       ans = min(Query(l, br[l], a[id[x]]), Query(bl[r], r, a[id[x]])); n       for (i = b[l] + 1; i < b[r]; ++i) ans = min(ans, curmn[i]); n       printf( %d n , ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long T; n void Read(long long &x) { n   char c; n   while (c = getchar(), c != EOF) n     if (c >=  0  && c <=  9 ) { n       x = c -  0 ; n       while (c = getchar(), c >=  0  && c <=  9 ) x = x * 10 + c -  0 ; n       ungetc(c, stdin); n       return; n     } n } n struct wall { n   int x1, y1, x2, y2; n   inline wall() {} n   inline wall(int x1, int y1, int x2, int y2) n       : x1(x1), y1(y1), x2(x2), y2(y2) {} n } ans[300 + 10]; n int cnt; n int main() { n   Read(T); n   long long a = 1; n   int t, now = 2, i, tot = 2; n   while (a <= T) a *= 6, tot += 2; n   printf( %d %d n , tot, tot); n   ans[++cnt] = wall(1, 2, 2, 2); n   ans[++cnt] = wall(2, 1, 2, 2); n   while (a != 1) { n     a /= 6; n     t = T / a; n     T -= t * a; n     now += 2; n     ans[++cnt] = wall(now - 3, now - 1, now - 2, now - 1); n     if (now != tot) { n       if (now != 4) { n         ans[++cnt] = wall(now - 4, now + 1, now - 3, now + 1); n         ans[++cnt] = wall(now + 1, now - 4, now + 1, now - 3); n       } n       ans[++cnt] = wall(now - 2, now, now - 2, now + 1); n       ans[++cnt] = wall(now - 1, now, now - 1, now + 1); n       ans[++cnt] = wall(now - 2, now + 2, now - 1, now + 2); n       ans[++cnt] = wall(now, now - 1, now + 1, now - 1); n       ans[++cnt] = wall(now, now - 2, now + 1, now - 2); n       ans[++cnt] = wall(now + 2, now - 2, now + 2, now - 1); n     } n     if (t % 3 == 0) ans[++cnt] = wall(now - 3, now, now - 2, now); n     if (t % 3 <= 1) ans[++cnt] = wall(now, now - 3, now, now - 2); n     if (t < 3) ans[++cnt] = wall(now - 1, now - 3, now - 1, now - 2); n   } n   printf( %d n , cnt); n   for (i = 1; i <= cnt; i++) n     printf( %d %d %d %d n , ans[i].x1, ans[i].y1, ans[i].x2, ans[i].y2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int N, M; n   cin >> N >> M; n   int a[N][M]; n   for (int i = 0; i < N; i++) { n     for (int j = 0; j < M; j++) { n       cin >> a[i][j]; n     } n   } n   long long l = 0, r = 1e9 + 1; n   int i1 = 1, i2 = 1; n   int vis[1 << M]; n   while (r - l > 1) { n     long long m = (l + r) >> 1; n     bool check = false; n     memset(vis, 0, sizeof(vis)); n     for (int i = 0; i < N; i++) { n       int bit = 0; n       for (int j = 0; j < M; j++) { n         if (a[i][j] >= m) { n           bit ^= (1 << j); n         } n       } n       vis[bit] = i + 1; n     } n     for (int i = 0; i < (1 << M); i++) { n       for (int j = 0; j < (1 << M); j++) { n         if (vis[i] && vis[j] && ((i | j) + 1) == (1 << M)) { n           i1 = vis[i], i2 = vis[j]; n           check = true; n           break; n         } n       } n     } n     if (check) n       l = m; n     else n       r = m; n   } n   cout << i1 <<     << i2; n } n


#include <bits/stdc++.h> n using namespace std; n int N; n int x, y, root = -1; n vector<int> g[200100]; n int len[200100], viz[3][200100]; n int dmax = 0, ind; n int stacky[200100], l = 0; n void dfs(int x) { n   viz[0][x] = 1; n   if (g[x].size() == 1) { n     len[x] = 1; n   } else { n     int okk = 1; n     int kiddo = -1, kiddo2 = -1; n     for (auto y : g[x]) { n       if (viz[0][y]) { n         continue; n       } n       dfs(y); n       if (x == root) { n         if (len[y] == 0) { n           okk = 0; n         } else if (kiddo == -1) { n           kiddo = len[y]; n         } else if (kiddo != len[y] && kiddo2 == -1) { n           kiddo2 = len[y]; n         } else if (kiddo2 != len[y] && kiddo != len[y]) { n           okk = 0; n         } n       } else { n         if (len[y] == 0) { n           okk = 0; n           continue; n         } n         if (kiddo == -1) { n           kiddo = len[y]; n         } else if (kiddo != len[y]) { n           okk = 0; n         } n       } n     } n     if (okk) { n       if (x != root || kiddo2 == -1) { n         len[x] = kiddo + 1; n       } else { n         len[x] = kiddo + kiddo2 + 1; n       } n     } n   } n } n void dfss(int x, int t, int d) { n   stacky[++l] = x; n   viz[t][x] = 1; n   if (d > dmax) { n     ind = x; n     dmax = d; n     root = stacky[(l + 1) / 2]; n   } n   for (auto y : g[x]) n     if (!viz[t][y]) dfss(y, t, d + 1); n   --l; n } n int main() { n   cin.sync_with_stdio(false); n   cin >> N; n   if (N == 2) { n     cout << 1; n     return 0; n   } n   for (int i = 1; i < N; ++i) { n     cin >> x >> y; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   dfss(1, 1, 0); n   dmax = 0; n   dfss(ind, 2, 0); n   dfs(root); n   if (len[root]) { n     int ret = len[root] - 1; n     while (ret % 2 == 0) ret /= 2; n     cout << ret; n     return 0; n   } else { n     cout << -1; n     return 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<int> a; n bool check(vector<int> &b) { n   if (b.size() <= 1) return !b.empty(); n   int d = a[b[1]] - a[b[0]]; n   for (int i = 1; i < b.size(); ++i) { n     if (d != a[b[i]] - a[b[i - 1]]) return false; n   } n   return true; n } n bool solve(int o, int p) { n   vector<int> fir, sec; n   fir.push_back(o); n   fir.push_back(p); n   if (p == 2) { n     sec.push_back(1 - o); n   } n   for (int i = p + 1; i < n; ++i) { n     if (a[i] == a[fir[1]] - a[fir[0]] + a[*fir.rbegin()]) { n       fir.push_back(i); n     } else { n       sec.push_back(i); n     } n   } n   if (check(fir) && check(sec)) { n     for (auto &i : fir) { n       cout << a[i] <<    ; n     } n     cout << endl; n     for (auto &i : sec) { n       cout << a[i] <<    ; n     } n     cout << endl; n     return true; n   } n   sec.push_back(*fir.rbegin()); n   fir.pop_back(); n   sort(sec.begin(), sec.end()); n   if (check(fir) && check(sec)) { n     for (auto &i : fir) { n       cout << a[i] <<    ; n     } n     cout << endl; n     for (auto &i : sec) { n       cout << a[i] <<    ; n     } n     cout << endl; n     return true; n   } n   return false; n } n int main() { n   scanf( %d , &n); n   a.resize(n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &a[i]); n   } n   if (n == 2) { n     cout << a[0] << endl << a[1] << endl; n   } else { n     if (solve(0, 1)) return 0; n     if (solve(0, 2)) return 0; n     if (solve(1, 2)) return 0; n     puts( No solution n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void in(T &x) { n   x = 0; n   bool flag = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) flag = 1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = (x << 3) + (x << 1) + (c ^ 48); n     c = getchar(); n   } n   if (flag) x = -x; n } n priority_queue<long long, vector<long long>, greater<long long> > q; n long long n, x, ans; n signed main() { n   in(n); n   for (register long long i = 1; i <= n; ++i) { n     in(x); n     q.push(x); n     long long tmp = q.top(); n     if (x > tmp) ans += x - tmp, q.pop(), q.push(x); n   } n   printf( %lld , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int A; n int gcd(int a, int b) { n   if (a < b) swap(a, b); n   if (a % b == 0) return b; n   return gcd(b, a % b); n } n pair<int, int> find(vector<pair<int, int> > &sets, int a) { n   if (sets[a].first != a) sets[a].first = find(sets, sets[a].first).first; n   return sets[a]; n } n void unions(vector<pair<int, int> > &sets, int a, int b) { n   pair<int, int> p = find(sets, a); n   pair<int, int> q = find(sets, b); n   if (p.second < q.second) swap(p, q); n   sets[q.first].first = p.first; n   if (p.second == q.second) sets[p.first].second++; n } n void solve() { n   long long int n, i, a, b, x, y, a1, b1; n   cin >> n; n   vector<pair<int, int> > sets(n, {0, 1}); n   pair<int, int> p; n   for (i = 0; i < n; i++) sets[i].first = i; n   vector<pair<int, pair<int, int> > > v; n   for (i = 0; i < n - 1; i++) { n     cin >> a >> b; n     a--; n     b--; n     p = find(sets, a); n     a1 = p.first; n     x = p.second; n     p = find(sets, b); n     b1 = p.first; n     y = p.second; n     if (a1 != b1) n       unions(sets, a1, b1); n     else { n       v.push_back({a1, {a + 1, b + 1}}); n     } n   } n   set<int> s; n   for (i = 0; i < n; i++) { n     s.insert(find(sets, sets[i].first).first); n   } n   auto itr = s.begin(); n   x = *itr + 1; n   itr++; n   cout << v.size() <<   n ; n   for (i = 0, itr; i < v.size(); i++, itr++) n     cout << v[i].second.first <<     << v[i].second.second <<     << x <<     n          << *itr + 1 <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int T = 1; n   while (T--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:102400000,102400000 ) n const int N = 200005; n int len, fa[26], sz[N]; n char s1[N], s2[N]; n int Find(int x) { return (fa[x] == x) ? x : fa[x] = Find(fa[x]); } n void Unite(int x, int y) { n   int fax = Find(x), fay = Find(y); n   if (fax != fay) fa[fax] = fay, sz[fay] += sz[fax]; n } n vector<int> ans[26]; n int main() { n   for (int i = 0; i <= 25; ++i) fa[i] = i, sz[i] = 1; n   scanf( %d , &len); n   scanf( %s%s , s1 + 1, s2 + 1); n   for (int i = 1; i <= len; ++i) { n     if (s1[i] == s2[i]) continue; n     if (s1[i] > s2[i]) swap(s1[i], s2[i]); n     Unite(s1[i] -  a , s2[i] -  a ); n   } n   int ans1 = 0; n   for (int i = 0; i <= 25; ++i) n     if (fa[i] != i) ans[fa[i]].push_back(i), ++ans1; n   printf( %d n , ans1); n   for (int i = 0; i <= 25; ++i) n     if (ans[i].size() > 0) n       for (int u : ans[i]) printf( %c %c n ,  a  + u,  a  + i); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int x, y; n   node(int x1 = 0, int y1 = 0) : x(x1), y(y1){}; n   bool operator=(const node &b) const { return (x == b.x) && (y == b.y); } n }; n const int dx[5] = {0, 0, 0, 1, -1}; n const int dy[5] = {0, 1, -1, 0, 0}; n int N, M, Q; n char maze[1007][1007]; n int ans = 0; n int vis[1007][1007]; n int memo[1000007]; n void dfs(int x, int y, int id) { n   if (maze[x][y] ==  * ) return; n   for (int i = 1; i <= 4; i++) { n     int x1 = x + dx[i]; n     int y1 = y + dy[i]; n     if (x1 > 0 && x1 <= N && y1 > 0 && y1 <= M && maze[x1][y1] ==  * ) ans++; n   } n   for (int i = 1; i <= 4; i++) { n     int x1 = x + dx[i]; n     int y1 = y + dy[i]; n     if (maze[x1][y1] ==  .  && vis[x1][y1] == 0 && x1 > 0 && x1 <= N && n         y1 > 0 && y1 <= M) { n       vis[x1][y1] = id; n       dfs(x1, y1, id); n     } n   } n   return; n } n int main() { n   scanf( %d%d%d , &N, &M, &Q); n   for (int i = 1; i <= N; i++) { n     for (int j = 1; j <= M; j++) { n       cin >> maze[i][j]; n     } n   } n   int k = 1; n   while (Q--) { n     int x, y; n     scanf( %d%d , &x, &y); n     if (vis[x][y] == 0) { n       ans = 0; n       vis[x][y] = k; n       dfs(x, y, k); n       memo[k] = ans; n       k++; n     } n     printf( %d n , memo[vis[x][y]]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAX = 1e6 + 1; n void solve() { n   long long n, m; n   cin >> n >> m; n   if (m == 0) { n     cout << 0; n     return; n   } n   long long total[n + 1]; n   memset(total, 0, sizeof(total)); n   long long ai, bi, ci; n   for (long long i = 0; i < m; i++) { n     cin >> ai >> bi >> ci; n     total[ai] -= ci; n     total[bi] += ci; n   } n   long long ans = 0; n   for (long long i = 0; i < n + 1; i++) { n     if (total[i] > 0) { n       ans += total[i]; n     } n   } n   cout << ans; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t; n   t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline namespace Infinity { n inline namespace Constant { n constexpr const int eof = EOF; n constexpr const int maxint = INT_MAX; n constexpr const long long maxll = LLONG_MAX; n }  // namespace Constant n inline namespace TypeDefine { n using uint = unsigned int; n using ull = unsigned long long int; n using ld = long double; n template <typename T = vector<int>> n using iter = typename T::iterator; n template <typename T = int> n using vector_vector = vector<vector<T>>; n template <typename T = int, typename U = int> n using vector_pair = vector<pair<T, U>>; n }  // namespace TypeDefine n inline namespace IO { n const char CR =   n ; n const char SP =    ; n inline void write(const int n) { printf( %d , n); } n inline void write(const unsigned n) { printf( %u , n); } n inline void write(const long long n) { cout << n; } n inline void write(const unsigned long long n) { cout << n; } n inline void writef(const double a, const int n = 15) { printf( %.*f , n, a); } n inline void writef(const long double a, const int n = 18) { n   cout << setprecision(n) << fixed << a; n } n inline void write(const char c) { printf( %c , c); } n inline void write(const char s[]) { printf( %s , s); } n inline void write(const string &s) { printf( %s , s.data()); } n inline void write(const pair<int, int> &p) { n   printf( %d %d , p.first, p.second); n } n template <class T> n inline void write(const T a) { n   for (auto i = a.begin(); i != a.end(); i++) { n     if (i != a.begin()) write(SP); n     write(*i); n   } n } n template <typename T> n inline void writer(T begin, T end) { n   for (write(*begin++); begin != end; ++begin) write(SP), write(*begin); n } n inline void writeln() { write(CR); } n template <typename T> n inline void writeln(const T &a) { n   write(a); n   write(CR); n } n inline void writefln(const double a, int n) { n   printf( %.*f , n, a); n   write(CR); n } n inline void writefln(const long double a, int n = 18) { n   cout << setprecision(n) << fixed << a << endl; n } n inline void writesln(const int *a, const int l, const int r) { n   for (int i = l; i <= r; i++) printf( %d  , a[i]); n   writeln(CR); n } n template <typename T> n inline void writerln(T begin, T end) { n   for (write(*begin++); begin != end; ++begin) write(SP), write(*begin); n   write(CR); n } n template <class T> n inline void writelns(const T &a) { n   for (auto n : a) writeln(n); n } n template <typename T, typename... types> n inline void write(const T &a, const types &...args) { n   write(a); n   write(args...); n } n template <typename... types> n inline void writeln(const types &...args) { n   write(args...); n   write(CR); n } n template <typename... types> n inline void writeSP(const types &...args) { n   write(args...); n   write(SP); n } n inline void writelnYN(bool b) { writeln(b ?  YES  :  NO ); } n inline void writelnyn(bool b) { writeln(b ?  Yes  :  No ); } n string caseSharpSpace(int n) { return  Case #  + to_string(n) +  :  ; } n string caseNoSharpSpace(int n) { return  Case   + to_string(n) +  :  ; } n string caseSharpNoSpace(int n) { return  Case #  + to_string(n) +  : ; } n string caseNoSharpNoSpace(int n) { return  Case   + to_string(n) +  : ; } n inline int read(int &n) { return scanf( %d , &n); } n inline int read(long long &n) { return cin >> n ? 1 : -1; } n template <typename T, typename... types> n inline int read(T &n, types &...args) { n   return read(n) == -1 ? -1 : read(args...) + 1; n } n inline char getcc() { n   char c; n   do c = getchar(); n   while (c ==     || c ==   n ); n   return c; n } n inline int getint() { n   int n; n   read(n); n   return n; n } n inline long long getll() { n   long long n; n   read(n); n   return n; n } n inline double getdouble() { n   double n; n   scanf( %lf , &n); n   return n; n } n inline vector<int> getints(int n) { n   vector<int> v(n); n   for (int i = 0; i < n; i++) v[i] = getint(); n   return v; n } n inline vector<pair<int, int>> getpairs(int n) { n   vector<pair<int, int>> v(n); n   for (int i = 0; i < n; i++) { n     int a = getint(), b = getint(); n     v[i] = pair<int, int>(a, b); n   } n   return v; n } n inline void read(string &str, const unsigned size) { n   char s[size]; n   scanf( %s , s); n   str.assign(s); n } n inline string getstr(const unsigned size = 1048576) { n   string s; n   read(s, size + 1); n   return s; n } n inline string getln(const unsigned size = 1048576) { n   char s[size + 1]; n   scanf( %[^ n] , s); n   getchar(); n   return string(s); n } n }  // namespace IO n inline namespace Function { n inline constexpr int ctoi(const char c) { return c -  0 ; } n inline constexpr char itoc(const int n) { return n +  0 ; } n inline int dtoi(const double d) { return round(d); } n template <typename T> n inline constexpr bool in(T x, T l, T r) { n   return l <= x && x <= r; n } n template <class T> n inline int size(const T &a) { n   return a.size(); n } n template <class T> n inline void sort(T &a) { n   std::sort(a.begin(), a.end()); n } n template <class T1, class T2> n inline void sort(T1 &a, T2 comp) { n   std::sort(a.begin(), a.end(), comp); n } n template <class T1, typename T2> n inline int lbound(const T1 &a, const T2 k) { n   return lower_bound(a.begin(), a.end(), k) - a.begin(); n } n template <class T1, typename T2> n inline int ubound(const T1 &a, const T2 k) { n   return upper_bound(a.begin(), a.end(), k) - a.begin(); n } n template <class T1, class T2> n int count(T1 &a, T2 k) { n   return ubound(a, k) - lbound(a, k); n } n template <class T1, class T2> n int find(T1 &a, T2 k) { n   return count(a, k) ? lbound(a, k) : -1; n } n template <typename T> n inline void clear(T &a) { n   memset(a, 0, sizeof a); n } n template <typename T> n T gcd(T a, T b) { n   while (b) { n     T t = a % b; n     a = b; n     b = t; n   } n   return a; n } n template <typename T> n T lcm(T a, T b) { n   return a / gcd(a, b) * b; n } n long long qpow(long long a, long long b, long long c) { n   return b ? qpow(a * a % c, b >> 1, c) * (b & 1 ? a : 1) % c : 1; n } n template <typename T> n T exGcd(T a, T b, T &x, T &y) { n   T d = a; n   if (b) { n     d = exGcd(b, a % b, y, x); n     y -= a / b * x; n   } else { n     x = 1; n     y = 0; n   } n   return d; n } n template <typename T> n T constexpr inline mps(T l, T r, T k) { n   return ((r - (r % k + k) % k) - (l + (k - l % k) % k)) / k + 1; n } n template <typename T> n T constexpr inline sgn(T a) { n   return a == 0 ? 0 : a > 0 ? 1 : -1; n } n template <typename T> n constexpr inline T sq(T a) { n   return a * a; n } n template <typename T> n constexpr inline bool odd(T a) { n   return bool(a % 2); n } n template <typename T> n constexpr inline bool even(T a) { n   return !bool(a % 2); n } n template <typename T1, typename T2> n constexpr inline T1 smod(T1 x, T2 m) { n   return x > m ? x - m : x + m < m ? x + m : x; n } n template <typename T, typename F> n T dichotomy(T l, T r, F check, T prec = 1) { n   while (r - l > prec) { n     T m = l + (r - l) / 2; n     check(m) ? l = m : r = m; n   } n   return l; n } n bool contains(const string &s, const string &t) { n   return s.find(t) != string::npos; n } n template <typename T> n bool contains(const T &s, typename T::value_type t) { n   for (typename T::value_type c : s) n     if (c == t) return true; n   return false; n } n }  // namespace Function n }  // namespace Infinity n int main(int, char *[]) { n   int n = getint(); n   vector<int> a = getints(n); n   vector<string> s(n), r(n); n   for (int i = 0; i < n; i++) { n     cin >> s[i]; n     r[i].resize(s[i].size()); n     reverse_copy(s[i].begin(), s[i].end(), r[i].begin()); n   } n   vector<vector<unsigned long long>> d(n, vector<unsigned long long>(2, -1)); n   d[0][0] = 0; n   d[0][1] = a[0]; n   for (int i = 1; i < n; i++) { n     if (s[i] >= s[i - 1]) { n       d[i][0] = min(d[i][0], d[i - 1][0]); n     } n     if (s[i] >= r[i - 1]) { n       d[i][0] = min(d[i][0], d[i - 1][1]); n     } n     if (r[i] >= s[i - 1] && d[i - 1][0] != -1) { n       d[i][1] = min(d[i][1], d[i - 1][0] + a[i]); n     } n     if (r[i] >= r[i - 1] && d[i - 1][1] != -1) { n       d[i][1] = min(d[i][1], d[i - 1][1] + a[i]); n     } n   } n   writeln((long long)(min(d.back().front(), d.back().back()))); n   return 0; n } n