#include <bits/stdc++.h>   using namespace std;   deque<int> A, B;   long long p, i, j, k, n, m, aux;   long long fact(int a) {     long long f = 1;     int i;     for (int i = (int)(1); i <= (int)(a); ++i) f *= i;     return f;   }   int main(void) {     cin >> p;     cin >> n;     for (int i = (int)(1); i <= (int)(n); ++i) {       cin >> aux;       A.push_back(aux);     }     cin >> n;     for (int i = (int)(1); i <= (int)(n); ++i) {       cin >> aux;       B.push_back(aux);     }     long long step(0);     long long stari = fact(p + 1);     while (step <= stari) {       if (A.empty()) return cout << step <<     << 2, 0;       if (B.empty()) return cout << step <<     << 1, 0;       ++step;       int x = A.front();       A.pop_front();       int y = B.front();       B.pop_front();       if (x < y) {         B.push_back(x);         B.push_back(y);       } else {         A.push_back(y);         A.push_back(x);       }     }     cout <<  -1 ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int a[(int)(2 * 1e5 + 10)];   long long pa[(int)(2 * 1e5 + 10)];   long long sa[(int)(2 * 1e5 + 10)];   int main() {     cin >> n;     for (int i = 1; i <= n; ++i) {       cin >> a[i];       pa[i] = 1ll * a[i] + pa[i - 1];     }     for (int i = n; i >= 1; --i) {       sa[i] = 1ll * a[i] + sa[i + 1];     }     int x = n;     long long mx = 0;     int i = 1;     while (i <= n) {       if (i < x) {         if (pa[i] > sa[x])           --x;         else if (pa[i] < sa[x])           ++i;         else           mx = max(mx, pa[i]), ++i, --x;       } else         break;     }     cout << mx << endl;   }


#include <bits/stdc++.h>   using namespace std;   namespace io {   string separator =    ;   }   template <typename T>   inline void write(T x) {     cout << x << io::separator;   }   template <typename T, typename... Args>   inline void write(T x, Args... args) {     write(x);     write(args...);   }   inline void writeln() { cout <<   n ; }   template <typename... Args>   inline void writeln(Args... args) {     write(args...);     writeln();   }   template <typename T>   inline void read(T& x) {     cin >> x;   }   template <typename T, typename... Args>   inline void read(T& x, Args&... args) {     cin >> x;     read(args...);   }   template <typename T>   inline void writec(T x) {     for (auto i = x.begin(); i != x.end(); i++) write(*i);     writeln();   }   template <typename TL, typename TR>   inline TL GCD(TL a, TR b) {     TL c;     while (b) {       c = b;       b = a % b;       a = c;     }     return a;   }   template <typename TL, typename TR>   inline TL LCM(TL a, TR b) {     return a / GCD(a, b) * b;   }   template <typename TL, typename TR>   inline TL MAX(TL a, TR b) {     return a > b ? a : b;   }   template <typename T, typename... Args>   inline T MAX(T a, Args... args) {     return MAX(a, MAX(args...));   }   template <typename TL, typename TR>   inline TL MIN(TL a, TR b) {     return a < b ? a : b;   }   template <typename T, typename... Args>   inline T MIN(T a, Args... args) {     return MIN(a, MIN(args...));   }   inline int64_t fac(int16_t n) {     static vector<int64_t> f(1, 1);     for (auto i = f.size(); i <= n; i++) f.push_back(f.back() * i);     return f[n];   }   inline int64_t suma(int32_t a1, int32_t d, int32_t n) {     return n * (2 * a1 + (n - 1) * d) / 2;   }   inline int32_t factor(int32_t n) {     static vector<int32_t> d(2, 2), prime;     if (n + 1 > d.size()) {       int32_t np = d.size() - 1;       int32_t nn = d.size();       while (n > nn - 1) nn *= 2;       nn--;       d.resize(nn + 1);       for (auto i = 2; i <= nn; i++) {         if (!d[i]) {           d[i] = i;           prime.push_back(i);         }         int64_t x;         for (int32_t j = 0; prime[j] <= d[i] && j < prime.size() &&                             (x = (int64_t)i * prime[j]) <= nn;              j++)           d[x] = prime[j];       }     }     return d[n];   }   inline bool isprime(int32_t n) { return (factor(n) == n); }   inline int32_t prime(int32_t n) {     static vector<int32_t> p(1, 2);     if (n + 1 > p.size()) {       int32_t np = p.size() - 1;       int32_t nn = p.size();       while (n > nn - 1) nn *= 2;       factor(nn * (log(nn) + log(log(nn))));       nn--;       p.resize(nn + 1);       int32_t x = p[np] + 1;       for (auto i = np + 1; i <= nn; i++) {         while (!isprime(x)) x++;         p[i] = x++;       }     }     return p[n];   }   int32_t MOD = 1e9 + 7;   inline int32_t SUM(int32_t x, int32_t y) { return (x + y) % MOD; }   inline int32_t MUL(int32_t x, int32_t y) { return (int64_t)x * y % MOD; }   inline int32_t POW(int32_t x, int32_t n) {     int32_t y = 1;     while (n) {       if (n % 2) y = MUL(y, x);       x = MUL(x, x);       n /= 2;     }     return y;   }   inline int32_t INV(int32_t x) { return POW(x, MOD - 2); }   inline int32_t DIV(int32_t x, int32_t y) { return MUL(x, INV(y)); }   inline int32_t P(int32_t l, int32_t r) {     int32_t x = 1;     for (auto i = l; i <= r; i++) x = MUL(x, i);     return x;   }   inline int32_t FAC(int32_t n) { return P(2, n); }   inline int32_t C(int32_t n, int32_t k) { return DIV(P(k + 1, n), P(2, n - k)); }   void solve();   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     solve();   }   inline void solve() {     int16_t n;     read(n);     vector<int16_t> r(n);     for (auto i = r.begin(); i != r.end(); i++) read(*i);     int16_t mn = (*min_element(r.begin(), r.end()));     int16_t mx = (*max_element(r.begin(), r.end()));     int16_t s = 0;     for (auto i : r) s += i - mn;     s -= mx - mn;     while (s < mx - mn && mn != 0) {       s += n - 1;       mn--;     }     s += mx - mn;     if (!mn) {       s = 0;       for (auto i : r) s += i;     }     (s);     string ans0(n,  0 );     string ans = ans0;     writeln(mn);     if (mn) {       writeln(s / 2);       if (s % 2) {         int16_t i = 0;         for (auto i_ = 3; i_--;) {           while (r[i] == mn) i++;           ans[i] =  1 ;           r[i]--;           i++;         }         writeln(ans);         s -= 3;       }       for (auto i_ = s / 2; i_--;) {         int16_t mxi = distance(r.begin(), max_element(r.begin(), r.end()));         int16_t i = 0;         while (r[i] == mn || i == mxi) i++;         ans = ans0;         ans[i] = ans[mxi] =  1 ;         r[i]--;         r[mxi]--;         writeln(ans);       }     } else {       writeln(s);       for (auto i = 0; i <= n - 1; i++) {         ans = ans0;         ans[i] = ans[(i + 1) % n] =  1 ;         for (auto i_ = r[i]; i_--;) writeln(ans);       }     }   }


#include <bits/stdc++.h>   using namespace std;   template <typename a>   void SWAP(a &b, a &c) {     a d = b;     b = c;     c = d;   }   long long power(long long a, long long b) {     long long result = 1;     while (b) {       if (b & 1) result = a * result;       a *= a;       b >>= 1;     }     return result;   }   long long gcd(long long a, long long b) {     while (b) {       a = a % b;       SWAP(a, b);     }     return a;   }   long long lcm(long long a, long long b) { return (a * (b / gcd(a, b))); }   int main() {     long long n, k;     scanf( %lld %lld , &n, &k);     long long h[n];     long long sum = 0, ans = LLONG_MAX, index = 1;     for (long long i = 0; i < n; i++) {       scanf( %lld , &h[i]);       sum += h[i];       if (i + 1 >= k) {         if (ans > sum) {           index = i - k + 2;           ans = sum;         }         sum -= h[i - k + 1];       }     }     printf( %lld n , (index));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   template <class t, class u>   void chmax(t& first, u second) {     if (first < second) first = second;   }   template <class t, class u>   void chmin(t& first, u second) {     if (second < first) first = second;   }   template <class t>   using vc = vector<t>;   template <class t>   using vvc = vc<vc<t>>;   using pi = pair<int, int>;   using vi = vc<int>;   template <class t, class u>   ostream& operator<<(ostream& os, const pair<t, u>& p) {     return os <<  {  << p.first <<  ,  << p.second <<  } ;   }   template <class t>   ostream& operator<<(ostream& os, const vc<t>& v) {     os <<  { ;     for (auto e : v) os << e <<  , ;     return os <<  } ;   }   using uint = unsigned;   using ull = unsigned long long;   template <class t, size_t n>   ostream& operator<<(ostream& os, const array<t, n>& first) {     return os << vc<t>(first.begin(), first.end());   }   template <int i, class T>   void print_tuple(ostream&, const T&) {}   template <int i, class T, class H, class... Args>   void print_tuple(ostream& os, const T& t) {     if (i) os <<  , ;     os << get<i>(t);     print_tuple<i + 1, T, Args...>(os, t);   }   template <class... Args>   ostream& operator<<(ostream& os, const tuple<Args...>& t) {     os <<  { ;     print_tuple<0, tuple<Args...>, Args...>(os, t);     return os <<  } ;   }   template <class t>   void print(t x, int suc = 1) {     cout << x;     if (suc == 1) cout <<   n ;     if (suc == 2) cout <<    ;   }   ll read() {     ll i;     cin >> i;     return i;   }   vi readvi(int n, int off = 0) {     vi v(n);     for (int i = int(0); i < int(n); i++) v[i] = read() + off;     return v;   }   template <class T>   void print(const vector<T>& v, int suc = 1) {     for (int i = int(0); i < int(v.size()); i++)       print(v[i], i == int(v.size()) - 1 ? suc : 2);   }   string readString() {     string s;     cin >> s;     return s;   }   template <class T>   T sq(const T& t) {     return t * t;   }   void yes(bool ex = true) {     cout <<  Yes           <<   n ;     if (ex) exit(0);   }   void no(bool ex = true) {     cout <<  No           <<   n ;     if (ex) exit(0);   }   void possible(bool ex = true) {     cout <<  Possible           <<   n ;     if (ex) exit(0);   }   void impossible(bool ex = true) {     cout <<  Impossible           <<   n ;     if (ex) exit(0);   }   constexpr ll ten(int n) { return n == 0 ? 1 : ten(n - 1) * 10; }   const ll infLL = LLONG_MAX / 3;   const int inf = INT_MAX / 2 - 100;   int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }   int topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }   int botbit(signed first) { return first == 0 ? 32 : __builtin_ctz(first); }   int botbit(ll first) { return first == 0 ? 64 : __builtin_ctzll(first); }   int popcount(signed t) { return __builtin_popcount(t); }   int popcount(ll t) { return __builtin_popcountll(t); }   bool ispow2(int i) { return i && (i & -i) == i; }   ll mask(int i) { return (ll(1) << i) - 1; }   bool inc(int first, int second, int c) {     return first <= second && second <= c;   }   template <class t>   void mkuni(vc<t>& v) {     sort(v.begin(), v.end());     v.erase(unique(v.begin(), v.end()), v.end());   }   ll rand_int(ll l, ll r) {     static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());     return uniform_int_distribution<ll>(l, r)(gen);   }   template <class t>   void myshuffle(vc<t>& first) {     for (int i = int(0); i < int(int(first.size())); i++)       swap(first[i], first[rand_int(0, i)]);   }   template <class t>   int lwb(const vc<t>& v, const t& first) {     return lower_bound(v.begin(), v.end(), first) - v.begin();   }   using uint = unsigned;   using ull = unsigned long long;   struct modinfo {     uint mod, root;   };   template <modinfo const& ref>   struct modular {     static constexpr uint const& mod = ref.mod;     static modular root() { return modular(ref.root); }     uint v;     modular(ll vv = 0) { s(vv % mod + mod); }     modular& s(uint vv) {       v = vv < mod ? vv : vv - mod;       return *this;     }     modular operator-() const { return modular() - *this; }     modular& operator+=(const modular& rhs) { return s(v + rhs.v); }     modular& operator-=(const modular& rhs) { return s(v + mod - rhs.v); }     modular& operator*=(const modular& rhs) {       v = ull(v) * rhs.v % mod;       return *this;     }     modular& operator/=(const modular& rhs) { return *this *= rhs.inv(); }     modular operator+(const modular& rhs) const { return modular(*this) += rhs; }     modular operator-(const modular& rhs) const { return modular(*this) -= rhs; }     modular operator*(const modular& rhs) const { return modular(*this) *= rhs; }     modular operator/(const modular& rhs) const { return modular(*this) /= rhs; }     modular pow(int n) const {       modular res(1), x(*this);       while (n) {         if (n & 1) res *= x;         x *= x;         n >>= 1;       }       return res;     }     modular inv() const { return pow(mod - 2); }     friend modular operator+(int x, const modular& y) { return modular(x) + y; }     friend modular operator-(int x, const modular& y) { return modular(x) - y; }     friend modular operator*(int x, const modular& y) { return modular(x) * y; }     friend modular operator/(int x, const modular& y) { return modular(x) / y; }     friend ostream& operator<<(ostream& os, const modular& m) {       return os << m.v;     }     friend istream& operator>>(istream& is, modular& m) {       ll x;       is >> x;       m = modular(x);       return is;     }     bool operator<(const modular& r) const { return v < r.v; }     bool operator==(const modular& r) const { return v == r.v; }     bool operator!=(const modular& r) const { return v != r.v; }     explicit operator bool() const { return v; }   };   extern constexpr modinfo base{998244353, 3};   using mint = modular<base>;   const int vmax = (1 << 21) + 10;   mint fact[vmax], finv[vmax], invs[vmax];   void initfact() {     fact[0] = 1;     for (int i = int(1); i < int(vmax); i++) {       fact[i] = fact[i - 1] * i;     }     finv[vmax - 1] = fact[vmax - 1].inv();     for (int i = vmax - 2; i >= 0; i--) {       finv[i] = finv[i + 1] * (i + 1);     }     for (int i = vmax - 1; i >= 1; i--) {       invs[i] = finv[i] * fact[i - 1];     }   }   mint choose(int n, int k) { return fact[n] * finv[n - k] * finv[k]; }   mint binom(int first, int second) {     return fact[first + second] * finv[first] * finv[second];   }   mint catalan(int n) {     return binom(n, n) - (n - 1 >= 0 ? binom(n - 1, n + 1) : 0);   }   void swapbit(ll& first, int i, int j) {     if (i == j) return;     for (int _ = int(0); _ < int(3); _++) {       ll x = (first >> i) & 1;       first ^= x << j;       swap(i, j);     }   }   const int S = 17;   mint dp[S + 1][1 << S];   signed main() {     cin.tie(0);     ios::sync_with_stdio(0);     cout << fixed << setprecision(20);     int n, m;     cin >> n >> m;     vc<ll> vs;     mint w = 1;     for (int _ = int(0); _ < int(n); _++) {       ll first;       cin >> first;       for (auto v : vs) chmin(first, first ^ v);       if (first == 0)         w *= 2;       else         vs.push_back(first);     }     vc<mint> ans(m + 1);     int s = int(vs.size());     if (int(vs.size()) <= 18) {       for (int bit = int(0); bit < int(1 << s); bit++) {         ll x = 0;         for (int i = int(0); i < int(s); i++)           if (bit & 1 << i) x ^= vs[i];         ans[popcount(x)] += 1;       }     } else {       for (int i = int(0); i < int(s); i++) {         assert(vs[i]);         int j = botbit(vs[i]);         assert(j >= i);         for (int k = int(0); k < int(s); k++)           if (k != i && vs[k] & 1LL << j) vs[k] ^= vs[i];         for (int k = int(0); k < int(s); k++) swapbit(vs[k], i, j);       }       int len = m - s;       assert(len <= S);       dp[0][0] = 1;       for (int i = int(0); i < int(s); i++) {         int v = vs[i] >> s;         for (int j = int(i + 1) - 1; j >= int(0); j--) {           for (int k = int(0); k < int(1 << len); k++) {             dp[j + 1][k ^ v] += dp[j][k];           }         }       }       for (int i = int(0); i < int(s + 1); i++)         for (int j = int(0); j < int(1 << len); j++) {           ans[i + popcount(j)] += dp[i][j];         }     }     for (auto& v : ans) v *= w;     print(ans);   }


#include <bits/stdc++.h>   using namespace std;   #define pi acos(-1)   #define mod 1000000007   #define lli long long int   #define li long int   #define pb push_back   #define mp make_pair   #define ff first   #define ss second   #define nl   n          int main()   {   ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);          lli t,n;       cin>>t;       while(t--)       {           cin>>n;          int count=0;           for(lli i=1; i<=n; i=i*10 +1)           {               for(int j=1; j<=9; j++)               {                   if(i*j<=n)                   {                       count++;                   }               }           }           cout<<count<<  n ;       }       return 0;   }


#include <bits/stdc++.h>   using LL = long long;   using ULL = unsigned long long;   using namespace std;   long long n, m;   vector<long long> a;   vector<pair<long long, long long> > vec;   signed main() {     ios::sync_with_stdio(false), cin.tie(NULL);     cin >> n;     a.resize(n, 0);     vec.resize(n, {0, 0});     for (long long i = (0); i < (n); i++) cin >> a[i];     for (long long i = (0); i < (n); i++)       vec[i].first = a[i], vec[i].second = n - i;     sort(vec.rbegin(), vec.rend());     cin >> m;     for (long long i = (0); i < (m); i++) {       long long k, pos;       cin >> k >> pos;       vector<pair<long long, long long> > tmp;       tmp.resize(k, {0, 0});       for (long long i = (0); i < (k); i++)         tmp[i].first = n - vec[i].second, tmp[i].second = vec[i].first;       sort(tmp.begin(), tmp.end());       cout << tmp[pos - 1].second <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s, t;     cin >> s >> t;     reverse(s.begin(), s.end());     if (s == t)       cout <<  YES ;     else       cout <<  NO ;   }


#include <bits/stdc++.h>   using namespace std;   const long long int Maxn = 20 + 5, Mod = 1e9 + 7, Inf = 1e9;   int n, p[Maxn], ansss, m, fac[Maxn];   bool mrk[Maxn], mark[Maxn];   int ans[] = {1,         0, 18,        0, 1800,      0, 670320,    0,                734832000, 0, 890786230, 0, 695720788, 0, 150347555, 0};   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> n;     cout << ans[n - 1] <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 10;   const int pN = 1e6;   const int INF = 0x3f3f3f3f;   const int MOD = 1e9 + 7;   void add(long long &x, long long y) {     x += y;     x %= MOD;   }   bool vis[MAXN];   int a[MAXN];   int main() {     int n, k;     cin >> n >> k;     for (int i = 0; i < n; i++) cin >> a[i];     int ans = INF;     int id;     for (int i = 0; i < n; i++) {       if (vis[i]) continue;       int sum = 0;       for (int j = i;; j = (j + k) % n) {         if (vis[j]) break;         vis[j] = true;         sum += a[j];       }       if (sum < ans) {         id = i;         ans = sum;       }     }     cout << id + 1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   long long data[100010], diff[100010], change[100010];   vector<vector<long long> > adj;   vector<pair<long long, int> > vp;   int main(void) {     scanf( %d%d , &n, &m);     adj.resize(n + 1, vector<long long>());     for (int i = 0; i < m; i++) scanf( %I64d , &data[i]);     if (m == 1) {       puts( 0 );       return 0;     }     for (int i = 0; i < m; i++) {       if (i == 0 && data[i] != data[i + 1])         adj[data[i]].push_back(data[i + 1]);       else if (i == m - 1 && data[i] != data[i - 1])         adj[data[i]].push_back(data[i - 1]);       else if (i > 0 && i < m - 1) {         if (data[i] != data[i - 1]) adj[data[i]].push_back(data[i - 1]);         if (data[i] != data[i + 1]) adj[data[i]].push_back(data[i + 1]);       }     }     for (int i = 1; i <= n; i++) {       if (adj[i].empty()) continue;       long long sum = 0;       sort(adj[i].begin(), adj[i].end());       long long avg1 = adj[i][adj[i].size() / 2];       long long newMid = adj[i].size() / 2 - 1;       if (newMid < 0) newMid = 0;       long long avg2 = adj[i][newMid];       long long sum1 = 0, sum2 = 0;       for (int j = 0; j < adj[i].size(); j++) sum += abs(adj[i][j] - i);       for (int j = 0; j < adj[i].size(); j++) sum1 += abs(adj[i][j] - avg1);       for (int j = 0; j < adj[i].size(); j++) sum2 += abs(adj[i][j] - avg2);       if (sum1 < sum2) {         vp.push_back(make_pair(sum - sum1, i));         change[i] = avg1;       } else {         vp.push_back(make_pair(sum - sum2, i));         change[i] = avg2;       }     }     sort(vp.begin(), vp.end());     if (vp.empty()) {       puts( 0 );       return 0;     }     int target = vp[vp.size() - 1].second;     for (int i = 0; i < m; i++)       if (data[i] == target) data[i] = change[target];     long long sol = 0;     for (int i = 0; i < m - 1; i++) sol += abs(data[i] - data[i + 1]);     printf( %I64d n , sol);   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   #pragma GCC target( sse4 )   const char nl =   n ;   using namespace std;   using ll = long long;   using vi = vector<ll>;   using vl = vector<ll>;   using pii = pair<ll, ll>;   using pll = pair<ll, ll>;   using str = string;   str to_string(char c) { return str(1, c); }   str to_string(bool b) { return b ?  true  :  false ; }   str to_string(const char* second) { return (str)second; }   str to_string(str second) { return second; }   template <class A>   str to_string(complex<A> c) {     stringstream ss;     ss << c;     return ss.str();   }   str to_string(vector<bool> v) {     str res =  { ;     for (ll i = 0; i < (ll)v.size(); i++) res += char( 0  + v[i]);     res +=  } ;     return res;   }   template <size_t SZ>   str to_string(bitset<SZ> b) {     str res =   ;     for (ll i = 0; i < b.size(); i++) res += char( 0  + b[i]);     return res;   }   template <class A, class B>   str to_string(pair<A, B> p);   template <class T>   str to_string(T v) {     bool fst = 1;     str res =  { ;     for (const auto& x : v) {       if (!fst) res +=  ,  ;       fst = 0;       res += to_string(x);     }     res +=  } ;     return res;   }   template <class A, class B>   str to_string(pair<A, B> p) {     return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ;   }   void DBG() { cerr <<  ]  << endl; }   template <class H, class... T>   void DBG(H h, T... t) {     cerr << to_string(h);     if (sizeof...(t)) cerr <<  ,  ;     DBG(t...);   }   ll n;   string c;   vector<vi> pref;   set<ll> vis;   bool notone = 0;   ll bruh(ll idx, char tr, ll len) {     vis.insert(idx);     ll amt =         pref[idx + len - 1][tr -  a ] - ((idx > 0) ? pref[idx - 1][tr -  a ] : 0);     0;     ll add = len - amt;     if (len == n / 2) {       return add;     } else {       if (len == 1 && notone) {         if ((idx + 1) / 2 == idx / 2) {           ;           notone = 0;           add += bruh(idx + 1, tr - 1, len);         } else {           notone = 0;           add += bruh(idx - 1, tr - 1, len);         }       } else {         bool minus = 1;         if ((idx + len * 2) / (len * 2) * (len * 2) / (len * 4) ==             idx / (len * 4)) {           minus = 0;         }         if (!minus) {           add += bruh((idx + len * 2) / (len * 2) * (len * 2), tr - 1, len * 2);         } else {           add += bruh((idx - len * 2) / (len * 2) * (len * 2), tr - 1, len * 2);         }       }     }     return add;   }   void solve() {     cin >> n >> c;     if (n == 1) {       if (c[0] ==  a )         cout << 0 << nl;       else {         cout << 1 << nl;       }       return;     } else if (n == 2) {       sort(c.begin(), c.end());       ll ans = 0;       if (c[0] ==  a  && c[1] ==  b ) {         cout << 0 << nl;       } else {         if (c[0] ==  a ) {           cout << 1 << nl;         } else if (c[0] ==  b ) {           cout << 1 << nl;         } else {           cout << 2 << nl;         }       }       return;     }     ll x = 1;     ll atharv = 0;     for (; x != n; atharv++) {       x *= 2;     }     ll amt = atharv + 1;     pref.resize(n);     for (ll i = 0; i < n; i++) {       pref[i].resize(26);     }     for (ll i = 0; i < n; i++) {       for (ll j = 0; j < 26; j++) {         if (i) pref[i][j] = pref[i - 1][j];       }       pref[i][c[i] -  a ]++;     }     0;     ll ans = 1e9;     for (ll i = 0; i < n; i++) {       notone = 1;       vis.clear();       ans = min(bruh(i,  a  + amt - 1, 1), ans);       0;       0;     }     cout << ans << nl;     pref.clear();   }   signed main() {     cin.tie(nullptr);     ios_base::sync_with_stdio(false);     ll t;     cin >> t;     while (t--) solve();   }


#include <bits/stdc++.h>   using namespace std;   int mp[100][100];   int cnt[100];   int main() {     int n, x, y;     cin >> n;     while (cin >> x) {       cin >> y;       mp[x][y] = 1;       mp[y][x] = 1;       cnt[x]++;     }     for (int i = 1; i <= n; i++) {       for (int j = i + 1; j <= n; j++) {         if (!mp[i][j]) {           if (cnt[i] > cnt[j])             cout << i <<     << j;           else             cout << j <<     << i;           return 0;         }       }     }   }


#include <bits/stdc++.h>   int n, m;   std::vector<std::vector<int> > gr;   std::vector<int> par;   std::vector<bool> vis;   std::vector<int> lev;   void dfsgr(int v) {     vis[v] = true;     for (auto& ch : (gr[v])) {       if (vis[ch]) continue;       lev[ch] = lev[v] + 1;       par[ch] = v;       dfsgr(ch);     }   }   int lca(int a, int b) {     if (lev[a] > lev[b]) std::swap(a, b);     while (lev[b] != lev[a]) b = par[b];     while (a != b) {       a = par[a];       b = par[b];     }     return a;   }   int main() {     std::ios_base::sync_with_stdio(false);     std::cin.tie(0);     std::cin >> n >> m;     gr.resize(n);     vis.resize(n);     par.resize(n, -1);     lev.resize(n);     for (int i = 0; i < (m); ++i) {       int a, b;       std::cin >> a >> b;       a--;       b--;       gr[a].push_back(b);       gr[b].push_back(a);     }     dfsgr(0);     int q;     std::cin >> q;     std::vector<int> dd(n);     std::vector<std::vector<int> > paths;     for (int _ = 0; _ < (q); ++_) {       int a, b;       std::cin >> a >> b;       a--;       b--;       int lc = lca(a, b);       int pos = a;       std::vector<int> pth;       while (pos != lc) {         dd[pos] ^= 1;         pth.push_back(pos);         pos = par[pos];       }       pth.push_back(lc);       std::vector<int> nd;       pos = b;       while (pos != lc) {         dd[pos] ^= 1;         nd.push_back(pos);         pos = par[pos];       }       while (((int)(nd).size())) pth.push_back(nd.back()), nd.pop_back();       paths.push_back(std::move(pth));     }     bool good = true;     for (int i = 0; i < (n); ++i) {       if (dd[i]) good = false;     }     if (good) {       std::cout <<  YES n ;       for (auto& p : (paths)) {         std::cout << ((int)(p).size()) <<   n ;         for (auto& i : (p)) std::cout << i + 1 <<    ;         std::cout <<   n ;       }     } else {       std::cout <<  NO n ;       std::vector<std::vector<int> > ed(n);       for (int i = 0; i < (n); ++i)         if (dd[i]) {           ed[i].push_back(par[i]);           ed[par[i]].push_back(i);         }       std::fill(vis.begin(), vis.end(), false);       int xd = 0;       std::function<int(int)> dfs = [&](int v) {         vis[v] = true;         int mam = 0;         for (auto& ch : (ed[v]))           if (!vis[ch]) {             dfs(ch);             mam++;           }         xd += mam / 2;         return mam % 2;       };       int ans = 0;       for (int i = 0; i < (n); ++i)         if (!vis[i]) {           xd = 0;           if (dfs(i)) xd++;           ans += xd;         }       std::cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   inline void rd(int &x) {     int k;     int m = 0;     x = 0;     for (;;) {       k = getchar();       if (k ==  - ) {         m = 1;         break;       }       if ( 0  <= k && k <=  9 ) {         x = k -  0 ;         break;       }     }     for (;;) {       k = getchar();       if (k <  0  || k >  9 ) {         break;       }       x = x * 10 + k -  0 ;     }     if (m) {       x = -x;     }   }   inline void rd(char &c) {     int i;     for (;;) {       i = getchar();       if (i !=     && i !=   n  && i !=   r  && i !=   t  && i != EOF) {         break;       }     }     c = i;   }   inline int rd(char c[]) {     int i;     int sz = 0;     for (;;) {       i = getchar();       if (i !=     && i !=   n  && i !=   r  && i !=   t  && i != EOF) {         break;       }     }     c[sz++] = i;     for (;;) {       i = getchar();       if (i ==     || i ==   n  || i ==   r  || i ==   t  || i == EOF) {         break;       }       c[sz++] = i;     }     c[sz] =   0 ;     return sz;   }   inline void wt_L(char a) { putchar(a); }   inline void wt_L(int x) {     int s = 0;     int m = 0;     char f[10];     if (x < 0) {       m = 1;       x = -x;     }     while (x) {       f[s++] = x % 10;       x /= 10;     }     if (!s) {       f[s++] = 0;     }     if (m) {       putchar( - );     }     while (s--) {       putchar(f[s] +  0 );     }   }   template <class S, class T>   inline S chmin(S &a, T b) {     if (a > b) {       a = b;     }     return a;   }   int N;   char S[300002];   int sz;   int lis[300002];   int main() {     int i, m;     int k;     int mn;     int y;     int f;     int cnt;     int res;     int res1;     int res2;     int ini;     rd(N);     rd(S);     k = mn = 0;     for (i = (0); i < (N); i++) {       if (S[i] ==  ( ) {         k++;       } else {         k--;       }       chmin(mn, k);     }     if (k) {       wt_L(0);       wt_L(  n );       wt_L(1);       wt_L(   );       wt_L(1);       wt_L(  n );       return 0;     }     k = 0;     res = res1 = res2 = 0;     for (i = (0); i < (N); i++) {       if (S[i] ==  ( ) {         k++;       } else {         k--;       }       if (k == mn) {         res++;         lis[sz++] = i;       }     }     ini = res;     for (m = (0); m < (sz); m++) {       k = 0;       if (m == sz - 1) {         y = lis[0] + N;       } else {         y = lis[m + 1];       }       f = -1;       for (i = (lis[m] + 1); i < (y); i++) {         if (S[i % N] ==  ( ) {           k++;         } else {           k--;         }         if (k < 2) {           f = -1;           cnt = 0;         }         if (k == 2 && f == -1) {           f = i;         }         if (k == 2) {           cnt++;         }         if (k == 2 && ini + cnt > res) {           res = ini + cnt;           res1 = f % N;           res2 = (i + 1) % N;         }       }     }     for (m = (0); m < (sz); m++) {       k = 0;       if (m == sz - 1) {         y = lis[0] + N;       } else {         y = lis[m + 1];       }       cnt = 0;       for (i = (lis[m] + 1); i < (y); i++) {         if (S[i % N] ==  ( ) {           k++;         } else {           k--;         }         if (k == 1) {           cnt++;         }       }       if (cnt > res) {         res = cnt;         res1 = (lis[m] + 1) % N;         res2 = y % N;       }     }     wt_L(res);     wt_L(  n );     wt_L(res1 + 1);     wt_L(   );     wt_L(res2 + 1);     wt_L(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long mod = 1000000007;   long long n, k, t, m, q, flag = 0;   long long po(long long k, long long n, long long ans, long long temp,                long long ans1) {     if (n == 0) return ans;     while (temp <= n) {       ans *= ans1;       ans %= mod;       ans1 = ans1 * ans1;       ans1 %= mod;       n = n - temp;       temp *= 2;     }     return po(k, n, ans, 1, k) % mod;   }   long long min(long long a, long long b) {     if (a > b)       return b;     else       return a;   }   long long max(long long a, long long b) {     if (a > b)       return a;     else       return b;   }   long long md[100 + 1][100 + 1];   vector<long long> v[100 + 1];   long long bfs(long long i, long long j, queue<long long> q, long long z,                 map<long long, long long> m1) {     long long o = q.size();     if (o == 0) return -1;     for (int l = 0; l < o; l++) {       long long b = q.front();       if (b == j)         return z;       else {         if (m1[b] != 1)           for (int i = 0; i < v[b].size(); i++) {             q.push(v[b][i]);           }         m1[b] = 1;         q.pop();       }     }     return bfs(i, j, q, z + 1, m1);   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     t = 1;     while (t--) {       cin >> n;       char c[n][n];       for (int i = 0; i < n; i++)         for (int j = 0; j < n; j++) cin >> c[i][j];       for (int i = 0; i < n; i++) {         for (int j = 0; j < n; j++) {           if (c[i][j] ==  1 ) v[i + 1].push_back(j + 1);         }       }       for (int i = 0; i < n; i++) {         for (int j = 0; j < n; j++) {           queue<long long> q;           map<long long, long long> m1;           q.push(i + 1);           md[i + 1][j + 1] = bfs(i + 1, j + 1, q, 0, m1);         }       }       long long m;       cin >> m;       long long a[m];       for (int i = 0; i < m; i++) cin >> a[i];       vector<long long> ans;       long long pi = a[0], pii = 0;       ans.push_back(a[0]);       for (int i = 1; i < m; i++) {         if (md[pi][a[i]] == i - pii)           ;         else {           ans.push_back(a[i - 1]);           pi = a[i - 1];           pii = i - 1;         }       }       cout << ans.size() + 1;       cout <<   n ;       for (int i = 0; i < ans.size(); i++) {         cout << ans[i] <<    ;       }       cout << a[m - 1];     }     return 0;   }


#include<bits/stdc++.h>   #include<unordered_map>   #include<unordered_set>   #define f(i,a,b) for( int i=a;i<=b;++i)   #define ff(i,a,b) for( int i=a;i>=b;--i)   #define debug(x) cerr << #x <<   :   << x <<     << endl   using namespace std;   typedef long long ll;   typedef unsigned long long ull;   typedef long double ld;   typedef pair<int, int> pii;   typedef pair<string, string> pss;   const ll mod = 1e9 + 7;   const ll mod2 = 998244353;   const ll inf = 2e18;   const double tiaohe = 0.57721566490153286060651209;   ll oula(ll x) { ll res = x;f(i, 2, x / i) { if (x % i == 0) { res = res / i * (i - 1);while (x % i == 0) x /= i; } }if (x > 1) res = res / x * (x - 1);return res; }   ll quickmod(ll a, ll n, ll m) { ll s = 1;while (n) { if (n & 1) { s = s * a % m; }a = (a*a) % m;n = n / 2; }return s; }   ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }   void ex_gcd(ll a, ll b, ll &x, ll &y, ll &d) { if (!b) { d = a, x = 1, y = 0; } else { ex_gcd(b, a % b, y, x, d);y -= x * (a / b); } }   ll inv(ll t, ll p) { ll d, x, y;ex_gcd(t, p, x, y, d);return d == 1 ? (x % p + p) % p : -1; }   bool isPrime(ll x) { if (x == 2)return true;if (x % 2 == 0)return false;for (ll i = 2;i*i <= x;i++) if (x % i == 0)return false; return true; }   inline ll in() { char ch = getchar();ll x = 0, f = 1;while (ch< 0  || ch> 9 ) { if (ch ==  - )f = -1;ch = getchar(); }while (ch >=  0 &&ch <=  9 ) { x = x * 10 + ch -  0 ;ch = getchar(); }return x * f; }   void print(double x) { printf( %.6lf n , x); }   //double a = log(n) +tiaohe + 1.0 / (2 * n);   double eqa = (1 + sqrt(5.0)) / 2.0;   double E = 2.7182818284;   const double eps = 1e-8;   double pi = acos(-1);   const int N = 5e5 + 100;   int n, m;   ll k;   int a[N], b[N],id;   ll M[2], LCM;   pair<ll,ll> st[N];//等价类起点   pair<ll,ll> linear( ll B[], ll M[], int n)   {    //求解 A[i]x = B[i] (mod M[i]), 总共 n 个线性 方程组, →     ll x = 0, m = 1;    for (int i = 0; i < n; i++)    {     ll a =  m, b = B[i] -  x, d = gcd(M[i], a);     if (b % d != 0) return pair<ll,ll>(0, -1);//答案不存在，返回-1      ll t = b / d * inv(a / d, M[i] / d) % (M[i] / d);     x = x + m * t;     m *= M[i] / d;    }    x = (x % m + m) % m;    return pair<ll, ll>{x,m};//返回的 x 就是答案，m 是最后的 lcm 值   }   ll pos[N][2];   bool check(ll mid) {    ll res = mid;//减去命中的    f(i, 1, id) {     if (st[i].second == -1) {      continue;     }     if (st[i].first > mid)continue;     ll num = (ll)ceil((mid-1-st[i].first)*1.0/LCM);     res -= num;    }    //cout << mid <<     << res << endl;    return res <= k;   }   void work(ll k) {    ll l = 0, r = 2e18,res=0;    while (l <= r) {     ll mid = l + r >> 1;     if (check(mid)) {      res = mid;      l = mid + 1;     }     else {      r = mid - 1;     }    }    cout << res << endl;   }   int main()   {   #ifndef ONLINE_JUDGE    freopen( in.txt ,  r , stdin);   #endif    cin >> n >> m >> k;    unordered_map<int, int> mp;    f(i, 1, n)a[i] = in(), mp[a[i]] = i-1;    f(i, 1, m)b[i] = in();    id = 0;    f(i, 1, m) {     if (mp.count(b[i])) {      pos[++id][0] = mp[b[i]];      pos[id][1] = i-1;     }    }    LCM= 1ll*n * m / gcd(n, m);    M[0] = n, M[1] = m;    f(i, 1, id) {     st[i] = linear(pos[i], M, 2);     //cout << st[i].first <<     << st[i].second << endl;    }    work(k);    return 0;   }


#include <bits/stdc++.h>   const int N = 600000 + 7;   using namespace std;   char o[] = { I ,  E ,  A ,  O ,  U ,  Y };   bool mark[N];   long long sum[N];   long double ans;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     string s;     cin >> s;     for (int i = 0; i < (((int)s.size())); i++)       for (int j = 0; j < (6); j++)         if (s[i] == o[j]) mark[i + 1] = true;     int n = ((int)s.size());     for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + mark[i];     long long cnt = sum[n];     ans = cnt;     for (int i = 2; i <= n; i++) {       cnt += (sum[n - i + 1] - sum[i - 1]);       ans += (long double)cnt / (long double)i;     }     cout << setprecision(7) << fixed << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     long long x;     bool s;     node(long long a, bool b) { x = a, s = b; }   };   bool operator<(node a, node b) { return a.x > b.x; }   priority_queue<node> pq;   int main() {     int n;     long long temp;     cin >> n;     long long ans = 0;     for (int i = 1; i <= n; i++) {       cin >> temp;       if (pq.empty() || pq.top().x > temp) {         pq.push(node(temp, 0));       } else {         long long now = pq.top().x;         bool se = pq.top().s;         pq.pop();         if (se) pq.push(node(now, 0));         ans += temp - now, pq.push(node(temp, 1));       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     int n;     cin >> n;     long long int a[100005] = {0};     for (int i = 1; i <= n; i++) cin >> a[i];     a[n + 1] = 0;     for (int i = 1; i <= n; i++) {       cout << a[i] + a[i + 1];       cout <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   const long long SZ = 107;   const long long N = 1e6 + 7;   const long long M = 1e4 + 7;   long long n, m, c;   void solve() {     long long n;     cin >> n;     long long a[n];     vector<long long> b, c;     for (long long i = 0; i < n; i++) {       cin >> a[i];     }     sort(a, a + n);     long long sm1 = 0, sm2 = 0;     if (n % 2) {       for (long long i = 0; i < n / 2; i++) {         b.push_back(a[i]);         sm1 += a[i];       }       for (long long i = n / 2; i < n; i++) {         c.push_back(a[i]);         sm2 += a[i];       }     } else {       for (long long i = 0; i < n / 2; i++) {         b.push_back(a[i]);         sm1 += a[i];       }       for (long long i = n / 2; i < n; i++) {         c.push_back(a[i]);         sm2 += a[i];       }     }     cout << sm1 * sm1 + sm2 * sm2 <<   n ;     cout <<   n ;   }   signed main() {     long long t = 1;     for (long long i = 1; i <= t; i++) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   string tos(T a) {     stringstream ss;     string ret;     ss << a;     ss >> ret;     return ret;   }   void read(int &_a) { int temp = scanf( %d , &_a); }   void read(int &_a, int &_b) { int temp = scanf( %d %d , &_a, &_b); }   void read(int &_a, int &_b, int &_c) {     int temp = scanf( %d %d %d , &_a, &_b, &_c);   }   const int SZ = 3e5 + 2;   int N, M, AR[SZ], FR[2 * SZ], MN[SZ], MX[SZ], LOC[2 * SZ];   void addc(int idx, int v) {     if (idx < 1) return;     int mx = 2 * SZ;     while (idx < mx) {       FR[idx] += v;       idx += (idx & -idx);     }   }   void addr(int L, int R, int v) {     addc(L, v);     addc(R + 1, -v);   }   int get(int idx) {     if (idx < 1) return 0;     int sum = 0;     while (idx > 0) {       sum += FR[idx];       idx -= (idx & -idx);     }     return sum;   }   int main() {     while (cin >> N >> M) {       memset(FR, 0, sizeof FR);       ;       int head = SZ;       for (int(p) = (1); (p) < (N + 1); (p)++) {         MN[p] = MX[p] = p;         LOC[p] = SZ + p - 1;         addc(SZ + p, 1);       }       for (int(x) = (0); (x) < (M); (x)++) {         int p;         read(p);         MN[p] = 1;         int loc = LOC[p];         int pos = get(loc);         MX[p] = max(MX[p], pos + 1);         LOC[p] = --head;         addr(head + 1, loc, 1);       }       for (int(p) = (1); (p) < (N + 1); (p)++) {         int loc = LOC[p];         int pos = get(loc);         MX[p] = max(MX[p], pos + 1);         printf( %d %d n , MN[p], MX[p]);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int arr[1000], n, k, s = 0, c = 0, v = 0;     cin >> n >> k;     for (int i = 1; i <= n; i++) {       cin >> arr[i];     }     for (int i = 1; i <= n; i++) {       if (arr[i] <= 8) {         s = s + arr[i];         c++;         if (s >= k) break;       } else if (arr[i] > 8) {         v = arr[i] - 8;         arr[i + 1] += v;         s = s + 8;         c++;         if (s >= k) break;       }     }     if (s >= k)       cout << c;     else       cout <<  -1 ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long double pi = acos(-1);   long long a[200002] = {};   long long b[200002] = {};   long long c[200002] = {};   string s =   , ss =   ;   long long n, m, k, w;   long long ans, ans1;   long long mod = 1e9 + 7;   int main() {     cin.tie(0);     cin.sync_with_stdio(0);     int t = 1;     while (t--) {       cin >> n >> m;       for (int i = 0; i < n; i++) cin >> a[i];       ;       b[n - 1] = a[n - 1];       for (int i = n - 2; i >= 0; i--) b[i] = max(b[i + 1], a[i]);       c[0] = a[0];       for (long long i = 1; i < n; i++) c[i] = min(a[i], c[i - 1]);       ans = m;       for (long long i = 0; i < n - 1; i++) {         k = m / c[i];         k *= b[i + 1];         k += m % c[i];         ans = max(ans, k);       }       cout << ans;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = (int)1e9 + 7;   const int MOD2 = MOD + 2;   const int INF = (int)1e9;   const long long LINF = (long long)1e18;   const long double PI = acos((long double)-1);   const long double EPS = 1e-9;   inline long long gcd(long long a, long long b) {     long long r;     while (b) {       r = a % b;       a = b;       b = r;     }     return a;   }   inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }   inline long long fpow(long long n, long long k, int p = MOD) {     long long r = 1;     for (; k; k >>= 1) {       if (k & 1) r = r * n % p;       n = n * n % p;     }     return r;   }   template <class T>   inline int chkmin(T& a, const T& val) {     return val < a ? a = val, 1 : 0;   }   template <class T>   inline int chkmax(T& a, const T& val) {     return a < val ? a = val, 1 : 0;   }   inline long long isqrt(long long k) {     long long r = sqrt(k) + 1;     while (r * r > k) r--;     return r;   }   inline long long icbrt(long long k) {     long long r = cbrt(k) + 1;     while (r * r * r > k) r--;     return r;   }   inline void addmod(int& a, int val, int p = MOD) {     if ((a = (a + val)) >= p) a -= p;   }   inline void submod(int& a, int val, int p = MOD) {     if ((a = (a - val)) < 0) a += p;   }   inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; }   inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); }   inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; }   inline int sign(long double x, long double y) { return sign(x - y); }   int modulo(int a, int b) { return (a % b + b) % b; }   long long v1, v2, t, d;   long long dp[110][2000];   long long solve(long long i, long long cur) {     if (i == t) {       if (cur == v2) {         return dp[i][cur] = cur;       } else {         return dp[i][cur] = -LINF;       }     }     if (dp[i][cur] != -1) {       return dp[i][cur];     }     long long ans = -LINF;     for (int j = -d; j <= d; j++) {       if (cur + j > 0) {         ans = max(ans, solve(i + 1, cur + j));       }     }     return dp[i][cur] = ans + cur;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     cin >> v1 >> v2 >> t >> d;     memset(dp, -1, sizeof(dp));     cout << solve(1, v1) <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   list<pair<int, long long> > l;   long long get(int x) {     long long ans = 0;     for (list<pair<int, long long> >::iterator it = l.begin(), e = l.end();          it != e; ++it) {       ans = max(ans, it->second);       if (x <= it->first) {         break;       }       x -= it->first;     }     return ans;   }   void update(int ox, long long v) {     int x = ox;     list<pair<int, long long> >::iterator now = l.begin(), e = l.end();     for (; now != e; ++now) {       if (x < now->first) {         break;       }       x -= now->first;     }     while (l.begin() != now) {       l.pop_front();     }     l.begin()->first = l.begin()->first - x;     l.push_front(make_pair(ox, v));   }   int main() {     int n, m;     cin >> n;     l.clear();     for (int i = 1; i <= n; i++) {       long long t;       cin >> t;       l.push_back(make_pair(1, t));     }     cin >> m;     for (int i = 0; i < m; i++) {       int w;       long long h;       cin >> w >> h;       long long ans = get(w);       cout << ans <<   n ;       update(w, ans + h);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int Map[30][30];   int N, M, T;   void Swap(int x, int y) {     for (int i = 1; i <= N; i++) {       swap(Map[i][x], Map[i][y]);     }   }   bool Find() {     int ans = 0;     for (int i = 1; i <= N; i++) {       int s = 0;       for (int j = 1; j <= M; j++) {         if (Map[i][j] != j) s++;       }       if (s > 2) return 0;     }     return 1;   }   int main() {     cin >> N;     cin >> M;     for (int i = 1; i <= N; i++)       for (int j = 1; j <= M; j++) scanf( %d , &Map[i][j]);     int flag = 0;     if (Find()) flag = 1;     for (int i = 1; i <= M; i++) {       for (int j = i + 1; j <= M; j++) {         if (i == j) continue;         Swap(i, j);         if (Find()) {           flag = 1;           break;         }         Swap(i, j);       }       if (flag) break;     }     if (flag)       cout <<  YES n ;     else       cout <<  NO n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string func(pair<int, int> a, pair<int, int> b) {     return to_string(a.first) +     + to_string(a.second) +     +            to_string(b.first) +     + to_string(b.second);   }   int nSmallFactor(int n, vector<int>& primes) {     for (int prime : primes) {       if (n % prime == 0) {         return prime;       }     }     return -1;   }   void DFS(vector<int>& ve, vector<int>& visited, int n, int start) {     int ans = 0;     for (int i = start + start; i <= n; i += start) {       if (visited[i] == -1) {         DFS(ve, visited, n, i);       }       if (ve[i] > ve[start]) {         ans = max(ans, visited[i]);       }     }     visited[start] = ans + 1;   }   long long sum(long long a) {     if (a % 2 == 0) {       return (a / 2) * (a + 1);     }     return ((a + 1) / 2) * a;   }   long long sum(long long a, long long b) { return sum(b) - sum(a - 1); }   vector<int> getPrimes(int n) {     vector<int> primes;     primes.push_back(2);     bool isPrime;     for (int i = 3; i * i <= n; i++) {       isPrime = true;       for (int p = 0; p < primes.size() && primes[p] * primes[p] <= i; p++) {         if (i % primes[p] == 0) {           isPrime = false;           break;         }       }       if (isPrime) {         primes.push_back(i);       }     }     return primes;   }   int findMaxPrime(vector<int> primes, int n, int k) {     int ans = INT_MAX;     bool findAns = true;     int ncpy = n;     for (int i = 0; primes[i] <= k && primes[i] * primes[i] <= n; i++) {       if (findAns && n % primes[i] == 0) {         ans = primes[i];       }       while (n % primes[i] == 0) {         n /= primes[i];       }     }     if (n <= k) {       ans = min(ans, ncpy / n);     }     ans = min(ans, ncpy);     return ans;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int ttt = 1;     cin >> ttt;     string s;     int temp;     int n, k;     int p, q, a, b, c, d;     bool ans;     vector<int> primes = getPrimes(1000000005);     int row, col;     while (ttt--) {       cin >> n;       bool a[n][n];       string s;       for (int i = (int)0; i < (int)n; i++) {         cin >> s;         for (int j = (int)0; j < (int)n; j++) {           if (s[j] ==  1 ) {             a[i][j] = true;           } else {             a[i][j] = false;           }         }       }       bool possible = true;       for (int i = 1; possible && i < n; i++) {         row = n - i - 1;         p = row;         for (p; p >= 0; p--) {           if (a[row][p]) {             if (!a[row][p + 1] && !a[row + 1][p]) {               possible = false;               break;             }           }         }         col = row;         p = col;         for (p; p >= 0; p--) {           if (a[p][col]) {             if (!a[p][col + 1] && !a[p + 1][col]) {               possible = false;               break;             }           }         }       }       if (possible) {         cout <<  YES n ;       } else {         cout <<  NO n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int NMax = 110000;   struct edge {     int num;     edge *next;   } * G[NMax], pool[NMax * 2];   int N, M, K, cant[NMax], L, deg[NMax], cnt[NMax], del[NMax], cur[NMax];   void Build(int x, int y) {     edge *p = &pool[L++], *q = &pool[L++];     p->num = y;     p->next = G[x];     G[x] = p;     q->num = x;     q->next = G[y];     G[y] = q;   }   void DFS(int a, long double k) {     for (edge *p = G[a]; p; p = p->next)       if (!del[p->num]) cur[p->num]--;     for (edge *p = G[a]; p; p = p->next)       if (!del[p->num]) {         if ((long double)cur[p->num] / (long double)deg[p->num] < k) {           del[p->num] = 1;           DFS(p->num, k);         }       }   }   int main() {     scanf( %d%d%d , &N, &M, &K);     for (int i = 1; i <= K; i++) {       int x;       scanf( %d , &x);       cant[x] = 1;     }     for (int i = 1; i <= M; i++) {       int x, y;       scanf( %d%d , &x, &y);       Build(x, y);       deg[x]++;       deg[y]++;     }     for (int i = 1; i <= N; i++)       if (!cant[i])         for (edge *p = G[i]; p; p = p->next)           if (!cant[p->num]) cnt[i]++;     long double l = -1e-4f, r = 1.0;     while (l + 1e-6f < r) {       long double mid = (l + r) / 2;       for (int i = 1; i <= N; i++) del[i] = cant[i], cur[i] = cnt[i];       for (int i = 1; i <= N; i++)         if (!del[i]) {           if ((long double)cur[i] / (long double)deg[i] < mid) {             del[i] = 1;             DFS(i, mid);           }         }       int flag = 0;       for (int i = 1; i <= N; i++)         if (!del[i]) flag = 1;       if (flag)         l = mid;       else         r = mid;     }     for (int i = 1; i <= N; i++) del[i] = cant[i], cur[i] = cnt[i];     for (int i = 1; i <= N; i++)       if (!del[i]) {         if ((long double)cur[i] / (long double)deg[i] < l) {           del[i] = 1;           DFS(i, l);         }       }     int ans = 0;     for (int i = 1; i <= N; i++)       if (!del[i]) ans++;     printf( %d n , ans);     for (int i = 1; i <= N; i++)       if (!del[i]) printf( %d  , i);     puts(  );     getchar();     getchar();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long cumu[200005];   int main() {     int n, k, x;     while (scanf( %d %d , &n, &k) == 2) {       memset(cumu, 0ll, sizeof cumu);       int mn = INT_MAX;       for (int i = 0; i < n; i++)         scanf( %d , &x), cumu[x + 1] -= 1, mn = min(mn, x);       cumu[1] = n;       for (int i = 1; i <= 200001; i++) cumu[i] += cumu[i - 1];       for (int i = 1; i <= 200001; i++) cumu[i] += cumu[i - 1];       int last = 200001;       int cut = 0;       int ans = 0;       for (int i = 200000; i >= mn; i--) {         cut = cumu[last] - cumu[i];         if (cut > k) ans++, last = i + 1, cut = cumu[last] - cumu[i];       }       ans += (cut != 0);       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   long long x, y;   long long gcd(long long a, long long b) {     if (!b) {       x = 1;       y = 0;       return a;     }     long long t1 = gcd(b, a % b);     long long t2 = x;     x = y;     y = t2 - a / b * y;     return t1;   }   int main() {     long long a1, b1, a2, b2, l, r, g, t;     double dl, dr;     scanf( %I64d%I64d%I64d%I64d%I64d%I64d , &a1, &b1, &a2, &b2, &l, &r);     g = gcd(a1, a2);     if ((b2 - b1) % g != 0) {       printf( 0 n );       return 0;     }     x *= (b2 - b1) / g;     y *= (b2 - b1) / g;     t = ceil((double)g * (-x) / a2);     x += a2 / g * t;     y -= a1 / g * t;     if (y > 0) {       t = floor((double)g * (-y) / a1);       x -= a2 / g * t;       y += a1 / g * t;     }     dl = ((double)(l - b1) / a1 - x) * (double)g / a2;     dr = ((double)(r - b1) / a1 - x) * (double)g / a2;     dl = (dl - 0 > 1e-20 ? dl : 0);     long long ans = floor(dr) - ceil(dl) + 1;     if (dr < 0) {       printf( 0 n );       return 0;     }     printf( %I64d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   string s[105];   int n, m;   int main() {     long long l, r;     cin >> l >> r;     int digit1, digit2;     digit1 = digit2 = 0;     long long l1, r1;     l1 = l;     r1 = r;     while (l1 != 0) {       l1 /= 10;       digit1++;     }     while (r1 != 0) {       r1 /= 10;       digit2++;     }     int digit = max(digit1, digit2);     long long min_val = 1;     long long max_val = 9;     digit--;     while (digit != 0) {       min_val *= 10;       digit--;       max_val *= 10;       max_val += 9;     }     if ((max_val + 1) / 2 >= l && (max_val + 1) / 2 <= r) {       cout << (max_val - (max_val + 1) / 2) * (max_val + 1) / 2 << endl;     } else if (r < (max_val + 1) / 2) {       cout << (max_val - r) * r << endl;     } else {       if (l < min_val)         cout << max((max_val - l) * l,                     (max_val - (max_val + 1) / 2) * (max_val + 1) / 2)              << endl;       else         cout << (max_val - l) * l << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<int> s(n, 0);     vector<int> m(n, 0);     map<int, int> freq, freq2;     string sher, mori;     cin >> sher;     cin >> mori;     for (int i = 0; i < n; i++) {       s[i] = sher[i] -  0 ;     }     for (int i = 0; i < n; i++) {       m[i] = mori[i] -  0 ;       freq[m[i]] += 1;       freq2[m[i]] += 1;     }     int sflick = 0;     for (int i = 0; i < n; i++) {       for (int j = (s[i] + 1) % 10;; j = (j + 1) % 10) {         if (freq.find(j) != freq.end() && freq[j] > 0) {           freq[j]--;           if (j > s[i]) sflick++;           break;         }       }     }     int mflick = 0;     for (int i = 0; i < n; i++) {       for (int j = s[i];; j = (j + 1) % 10) {         if (freq2.find(j) != freq2.end() && freq2[j] > 0) {           freq2[j]--;           if (j < s[i]) mflick++;           break;         }       }     }     cout << mflick <<     << sflick << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 5100;   const int oo = 1e9;   const int MX = 100000000;   const int FF = 33000;   int n, m, a[N], gc[N];   int dp[5010], pre[5010];   map<int, bool> bad;   map<int, int> cache;   vector<int> prime;   bool mark[FF + 200];   int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }   int fact(int num) {     if (cache.find(num) != cache.end()) return cache[num];     int &ret = cache[num];     long long idx = 0, pf = prime[0];     while (pf * pf <= num) {       int cnt = 0;       while (num % pf == 0) num /= pf, cnt++;       ret += cnt * (bad[pf] ? -1 : 1);       if (++idx < prime.size()) pf = prime[idx];     }     if (num != 1) ret += bad[num] ? -1 : 1;     return ret;   }   int eval() {     int res = 0;     for (int i = 0; i < n; ++i) res += fact(a[i]);     return res;   }   int rn(int l, int r, int gc) {     return pre[r] - (!l ? 0 : pre[l - 1]) - (r - l + 1) * fact(gc);   }   int calc(int idx) {     if (idx == n) return 0;     int &ret = dp[idx];     if (ret != -oo) return ret;     ret = rn(idx, n - 1, 1);     for (int j = idx; j < n; ++j) {       ret = max(ret, rn(idx, j, gc[j]) + calc(j + 1));     }     return ret;   }   int main() {     cin >> n >> m;     for (int i = 0; i < n; ++i) {       scanf( %d , &a[i]);     }     for (int first, i = 0; i < m; ++i) {       scanf( %d , &first);       bad[first] = 1;     }     for (long long i = 2; i <= FF; ++i) {       if (mark[i]) continue;       for (int j = i; j <= FF; j += i) mark[j] = 1;       prime.push_back(i);     }     for (int i = 0; i < n; ++i) gc[i] = !i ? a[i] : gcd(a[i], gc[i - 1]);     for (int i = 0; i <= n; ++i) dp[i] = -oo;     for (int i = 0; i < n; ++i) {       pre[i] = fact(a[i]);       if (i) pre[i] += pre[i - 1];     }     cout << calc(0) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long n, a, b, dp[200005][2];     cin >> n >> a >> b;     string t;     cin >> t;     memset(dp, 0x3f, sizeof(dp));     dp[0][0] = b;     for (int i = 0; i < n; i++) {       if (t[i] ==  1 )         dp[i + 1][1] = dp[i][1] + a + 2 * b;       else {         dp[i + 1][0] = min(dp[i][0] + a + b, dp[i][1] + 2 * a + b);         dp[i + 1][1] = min(dp[i][0] + 2 * (a + b), dp[i][1] + a + 2 * b);       }     }     cout << dp[n][0] << endl;   }   int main() {     int t;     scanf( %d , &t);     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long n, m, ans;     cin >> n >> m;     vector<long long> ar(n);     for (long long i = 0; i < n; i++) cin >> ar[i];     sort(ar.begin(), ar.end());     ans = 1;     if (n > m) {       cout << 0;       return;     }     for (long long i = 0; i < n; i++) {       for (long long j = i + 1; j < n; j++) {         ans = (ans % m * abs(ar[i] - ar[j]) % m) % m;         if (ans == 0) {           cout << 0;           return;         }       }     }     cout << ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long q = 1;     while (q--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 2e9;   const int mod = 1e9 + 7;   const int N = 1234;   int n, m;   long long k;   int a[N][N], h[N * N], p[N * N];   long long b[N][N];   bool used[N][N];   vector<pair<int, int> > go = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};   bool check(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; }   int get(int x) { return x == p[x] ? x : p[x] = get(p[x]); }   void unite(int x, int y) {     x = get(x);     y = get(y);     if (x == y) return;     p[x] = y;     h[y] += h[x];     h[x] = 0;   }   long long qwe;   void dfs(int i, int j) {     k -= qwe;     used[i][j] = true;     b[i][j] = qwe;     for (auto w : go) {       int x = i + w.first, y = j + w.second;       if (check(x, y) && !used[x][y] && k > 0ll && a[x][y] >= qwe) dfs(x, y);     }   }   int main() {     ios::sync_with_stdio(0);     cin >> n >> m >> k;     vector<pair<int, pair<int, int> > > v;     v.reserve(n * m);     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         cin >> a[i][j];         p[i * m + j] = i * m + j;         h[i * m + j] = 1;         v.push_back({a[i][j], {i, j}});       }     }     sort(v.rbegin(), v.rend());     for (int i = 0; i < n * m; i++) {       int x = v[i].second.first, y = v[i].second.second, w = v[i].first;       for (auto z : go) {         int nx = x + z.first, ny = y + z.second;         if (check(nx, ny) && a[nx][ny] >= w) {           unite(x * m + y, nx * m + ny);         }       }       int sz = h[get(x * m + y)];       if (k % (long long)w == 0ll && k / (long long)w <= (long long)sz) {         qwe = w;         dfs(x, y);         cout <<  YES n ;         for (int j = 0; j < n; j++) {           for (int z = 0; z < m; z++) {             cout << b[j][z] <<    ;           }           cout <<   n ;         }         return 0;       }     }     cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline void read(T &f) {     f = 0;     T fu = 1;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) fu = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       f = (f << 3) + (f << 1) + (c & 15);       c = getchar();     }     f *= fu;   }   template <typename T>   void print(T x) {     if (x < 0) putchar( - ), x = -x;     if (x < 10)       putchar(x + 48);     else       print(x / 10), putchar(x % 10 + 48);   }   template <typename T>   void print(T x, char t) {     print(x);     putchar(t);   }   const int N = 55, M = 7777;   char c[N];   int isp[M + 5], val[N];   int T, n;   bool check(int x) {     for (int i = 2; i * i <= x; i++) {       if (x % i == 0) return 0;     }     return 1;   }   int main() {     for (int i = 2; i <= M; i++) isp[i] = check(i);     read(T);     while (T--) {       read(n);       scanf( %s , c + 1);       for (int i = 1; i <= M; i++) {         if (isp[i]) continue;         int len = 0, tmp = i;         while (tmp) {           val[++len] = tmp % 10;           tmp /= 10;         }         reverse(val + 1, val + len + 1);         int pos = 1;         for (int j = 1; j <= n; j++) {           if (c[j] -  0  == val[pos]) ++pos;           if (pos == len + 1) break;         }         if (pos == len + 1) {           print(len,   n );           print(i,   n );           break;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct item_t {     int cost;     int gain;     int when;     bool operator<(const item_t& other) const { return when < other.when; }     bool operator<(const int t) const { return when < t; }   } item[4000];   bool operator<(int v, const item_t& item) { return v < item.when; }   struct dp_t {     int maxgain[4000 + 1];     void update(int c, int g) {       for (int b = 4000; b >= 0; b--)         if (b + c <= 4000) maxgain[b + c] = max(maxgain[b + c], maxgain[b] + g);     }   } toleft[4000], toright[4000];   int main() {     int n, window;     scanf( %d %d , &n, &window);     for (int i = 0; i < n; i++) {       scanf( %d %d %d , &item[i].cost, &item[i].gain, &item[i].when);     }     sort(item, item + n);     memset(toleft, 0, sizeof(toleft));     memset(toright, 0, sizeof(toright));     for (int i = 0; i < n; i++) {       if (i - 1 >= 0 && item[i - 1].when / window == item[i].when / window)         toleft[i] = toleft[i - 1];       toleft[i].update(item[i].cost, item[i].gain);     }     for (int i = n - 1; i >= 0; i--) {       if (i + 1 < n && item[i + 1].when / window == item[i].when / window)         toright[i] = toright[i + 1];       toright[i].update(item[i].cost, item[i].gain);     }     int nq;     scanf( %d , &nq);     for (int q = 0; q < nq; q++) {       int vis_when, budget;       scanf( %d %d , &vis_when, &budget);       int b = vis_when;       int a = b - window + 1;       int lb = lower_bound(item, item + n, a) - item;       int _ub = upper_bound(item, item + n, b) - item;       int gain;       if (lb == _ub)         gain = 0;       else {         int ub1 = _ub - 1;         if (item[lb].when / window != item[ub1].when / window) {           gain = 0;           for (int lbud = 0; lbud <= budget; lbud++)             gain = max(gain, toright[lb].maxgain[lbud] +                                  toleft[ub1].maxgain[budget - lbud]);         } else {           if (item[ub1].when / window == b / window)             gain = toleft[ub1].maxgain[budget];           else             gain = toright[lb].maxgain[budget];         }       }       printf( %d n , gain);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void debug_out() { cerr << endl; }   template <class T>   ostream& prnt(ostream& out, T v) {     out << v.size() <<   n ;     for (auto e : v) out << e <<    ;     return out;   }   template <class T>   ostream& operator<<(ostream& out, vector<T> v) {     return prnt(out, v);   }   template <class T>   ostream& operator<<(ostream& out, set<T> v) {     return prnt(out, v);   }   template <class T1, class T2>   ostream& operator<<(ostream& out, map<T1, T2> v) {     return prnt(out, v);   }   template <class T1, class T2>   ostream& operator<<(ostream& out, pair<T1, T2> p) {     return out <<  (  << p.first <<     << p.second <<  ) ;   }   template <typename Head, typename... Tail>   void debug_out(Head H, Tail... T) {     cerr <<     << H;     debug_out(T...);   }   struct Flow {     const static int MAX_EDGES = 2510005;     const static int MAX_NODES = 6105;     const int INF = 1e9 + 50;     int S = MAX_NODES - 2, D = MAX_NODES - 1;     int G[MAX_NODES];     struct Edge {       int vec, flow, cap, cost, rev, nxt;     };     Edge E[MAX_EDGES];     int edges = 0;     int ParentNode[MAX_NODES], ParentEdge[MAX_NODES], Dist[MAX_NODES];     bool InQ[MAX_NODES];     queue<int> Q;     Flow() { Reset(); }     void setSource(int s) { S = s; }     void setSink(int d) { D = d; }     void _addEdge(int a, int b, int cap, int cost, int rev) {       ++edges;       E[edges] = (Edge){b, 0, cap, cost, rev, G[a]};       G[a] = edges;     }     void AddEdge(int a, int b, int cap, int cost) {       _addEdge(a, b, cap, cost, edges + 2);       _addEdge(b, a, 0, -cost, edges);     }     bool Bellman() {       memset(ParentNode, 0, sizeof(ParentNode));       Dist[S] = 0;       Q.push(S);       while (!Q.empty()) {         int top = Q.front();         InQ[top] = 0;         Q.pop();         for (int i = G[top]; i; i = E[i].nxt) {           int vec = E[i].vec;           if (E[i].flow < E[i].cap &&               (ParentNode[vec] == 0 || Dist[vec] > Dist[top] + E[i].cost)) {             Dist[vec] = Dist[top] + E[i].cost;             ParentNode[vec] = top;             ParentEdge[vec] = i;             if (!InQ[vec]) {               Q.push(vec);               InQ[vec] = 1;             }           }         }       }       return ParentNode[D] != 0;     }     void Reset() {       edges = 0;       memset(G, 0, sizeof(G));     }     int MFMC() {       int cost = 0, flow = 0;       while (Bellman()) {         int M = INF;         for (int node = D; node != S; node = ParentNode[node]) {           int ei = ParentEdge[node];           M = min(M, E[ei].cap - E[ei].flow);         }         for (int node = D; node != S; node = ParentNode[node]) {           int ei = ParentEdge[node], ri = E[ei].rev;           E[ei].flow += M;           E[ri].flow -= M;           cost += E[ei].cost * M;         }         flow += M;       }       cerr <<  flow             <<   -> ,           debug_out(flow);       return cost;     }   };   Flow F;   const int MAX_DAY = 101;   int n, m, k, c, d, a[55], x, y;   int vert(int nr, int day) { return 51 * day + nr; }   int main() {     ios_base::sync_with_stdio(false);     cin >> n >> m >> k >> c >> d;     for (int i = 1; i <= k; i++) {       cin >> a[i];       F.AddEdge(F.S, vert(a[i], 0), 1, 0);     }     for (int i = 1; i <= m; i++) {       cin >> x >> y;       for (int day = 0; day < MAX_DAY; day++) {         for (int cost = 0; cost < 50; cost++) {           F.AddEdge(vert(x, day), vert(y, day + 1), 1, (2 * cost + 1) * d);           F.AddEdge(vert(y, day), vert(x, day + 1), 1, (2 * cost + 1) * d);         }       }     }     for (int day = 0; day <= MAX_DAY; day++) {       for (int i = 1; i <= n; i++)         F.AddEdge(vert(i, day), vert(i, day + 1), 51, 0);       for (int i = 0; i < k; i++) F.AddEdge(vert(1, day), F.D, 1, c * day);     }     cout << F.MFMC() <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100000 + 1;   int a[N];   int main() {     float n, x, y;     cin >> x >> y;     printf( %.12f , (y * y - x * x) / (2 * x));   }


#include <bits/stdc++.h>   using namespace std;   string lines[10000];   int scheme[4];   int quart = -1;   bool is_vowel(char c) {     return c ==  a  || c ==  e  || c ==  i  || c ==  o  || c ==  u ;   }   bool rhyme(int s, int a, int b) {     int A = lines[s + a].length() - scheme[a];     int B = lines[s + b].length() - scheme[b];     if (A != B) {       return false;     }     return lines[s + a].substr(scheme[a]) == lines[s + b].substr(scheme[b]);   }   int main() {     int n, k;     cin >> n >> k;     for (int i = 0; i < 4 * n; i++) {       cin >> lines[i];     }     for (int i = 0; i < n; i++) {       int s = i * 4;       for (int x = 0; x < 4; x++) {         scheme[x] = -1;         int v = 0;         for (int j = lines[s + x].length() - 1; j >= 0; j--) {           if (is_vowel(lines[s + x][j])) {             v++;             if (v == k) {               scheme[x] = j;               break;             }           }         }         if (scheme[x] == -1) {           cout <<  NO  << endl;           return 0;         }       }       bool ab = rhyme(s, 0, 1);       bool ac = rhyme(s, 0, 2);       bool ad = rhyme(s, 0, 3);       bool bc = rhyme(s, 1, 2);       bool bd = rhyme(s, 1, 3);       bool cd = rhyme(s, 2, 3);       int nq;       if (ab && bc && cd) {         nq = 0;       } else if (ab && cd) {         nq = 1;       } else if (ac && bd) {         nq = 2;       } else if (ad && bc) {         nq = 3;       } else {         cout <<  NO  << endl;         return 0;       }       if (quart == -1 || quart == 0) {         quart = nq;       } else if (nq == 0 || nq == quart) {         continue;       } else {         cout <<  NO  << endl;         return 0;       }     }     if (quart == 0) {       cout <<  aaaa  << endl;     } else if (quart == 1) {       cout <<  aabb  << endl;     } else if (quart == 2) {       cout <<  abab  << endl;     } else if (quart == 3) {       cout <<  abba  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1000000007;   const long long INF = 1e15;   using Graph = vector<vector<long long>>;   signed main() {     long long T;     cin >> T;     for (long long t = 0; t < T; t++) {       long long N;       cin >> N;       vector<long long> A(N);       for (long long i = 0; i < N; i++) cin >> A[i];       sort(A.begin(), A.end());       long long ans = 1000000;       for (long long i = 1; i < N; i++) {         ans = min(ans, A[i] - A[i - 1]);       }       cout << ans << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-8;   struct node {     int v, next;   } p[200005 << 1];   int head[200005], n, k;   long long dp[200005][10], size[200005], ans;   void dfs(int x, int fa, int depth) {     size[x] = dp[x][depth % k] = 1;     for (int i = head[x]; ~i; i = p[i].next) {       if (p[i].v == fa) continue;       dfs(p[i].v, x, depth + 1);       for (int a = 0; a < k; a++) {         for (int b = 0; b < k; b++) {           int dis = ((a + b) % k - (depth * 2) % k + k) % k;           ans += ((k - dis) % k) * dp[x][a] * dp[p[i].v][b];         }       }       for (int a = 0; a < k; a++) {         dp[x][a] += dp[p[i].v][a];       }       ans += size[p[i].v] * (n - size[p[i].v]);       size[x] += size[p[i].v];     }   }   int main() {     scanf( %d%d , &n, &k);     memset(head, -1, sizeof(head));     int len = 0;     for (int i = 0; i < n - 1; i++) {       int u, v;       scanf( %d%d , &u, &v);       p[len].v = v;       p[len].next = head[u];       head[u] = len++;       p[len].v = u;       p[len].next = head[v];       head[v] = len++;     }     dfs(1, 0, 0);     printf( %lld n , ans / k);   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 10, M = 1 << 20 | 10;   const int mod = 1e9 + 7;   long long n, m, sum;   int a[M];   int main() {     scanf( %lld%lld , &n, &m);     for (int i = 1; i <= n; i++) a[i] = i, sum += i;     if (sum > m) return puts( -1 ), 0;     for (int i = 1, j = n; i <= j;) {       if (sum + j - i <= m)         swap(a[i], a[j]), sum += j - i, i++, j--;       else         j--;     }     printf( %lld n , sum);     for (int i = 1; i <= n; i++) printf( %d  , i);     puts(  );     for (int i = 1; i <= n; i++) printf( %d  , a[i]);     puts(  );   }


#include <bits/stdc++.h>   using namespace std;   const int Dian = 200005;   const int Bian = Dian * 2;   const int M = Dian;   const int Segment_Size = Dian * 4;   int fir[Dian], ne[Bian], to[Bian], cnt, n, m;   int dp[Dian], depth[Dian], fa[Dian], Bigson[Dian], top[Dian], S[Dian],       dep[Dian], info[Dian];   struct One {     int from, to, C, LCA;   } Road[M];   vector<One> G[Dian];   void add(int x, int y) {     ne[++cnt] = fir[x];     fir[x] = cnt;     to[cnt] = y;   }   void link(int x, int y) {     add(x, y);     add(y, x);   }   void getinfo(int x, int f) {     S[x] = 1;     dep[x] = dep[f] + 1;     int mx = -1;     for (int i = fir[x]; i; i = ne[i]) {       int V = to[i];       if (V == f) continue;       getinfo(V, x);       if (S[V] > mx) Bigson[x] = V, mx = S[V];       S[x] += S[V];     }   }   void getidx(int x, int T) {     top[x] = T;     if (Bigson[x]) getidx(Bigson[x], T);     for (int i = fir[x]; i; i = ne[i]) {       int V = to[i];       if (V == fa[x] || V == Bigson[x]) continue;       getidx(V, V);     }   }   int LCA(int x, int y) {     while (top[x] != top[y]) {       if (dep[top[x]] < dep[top[y]]) swap(x, y);       x = fa[top[x]];     }     if (dep[y] < dep[x]) return y;     return x;   }   void read(void) {     scanf( %d%d , &n, &m);     for (int i = 2; i <= n; i++) {       scanf( %d , &fa[i]);       link(i, fa[i]);     }     getinfo(1, 0);     getidx(1, 1);     for (int i = 1; i <= m; i++) {       scanf( %d%d%d , &Road[i].from, &Road[i].to, &Road[i].C);       Road[i].LCA = LCA(Road[i].from, Road[i].to);       G[Road[i].LCA].push_back(Road[i]);     }   }   int getans(One Query) {     int x = Query.from, y = Query.to, now = Query.C;     while (top[x] != top[y]) {       if (dep[top[x]] < dep[top[y]]) swap(x, y);       now = now + info[top[x]] - info[Bigson[x]];       x = fa[top[x]];     }     if (dep[x] > dep[y]) swap(x, y);     now = now + info[x] - info[Bigson[y]];     return now;   }   int dfs(int x, int f) {     int sum = 0;     for (int i = fir[x]; i; i = ne[i]) {       int V = to[i];       if (V == f) continue;       sum += dfs(V, x);     }     info[x] = info[Bigson[x]] + sum;     dp[x] = sum;     for (int i = 0; i < (int)G[x].size(); i++)       dp[x] = max(dp[x], getans(G[x][i]));     info[x] -= dp[x];     return dp[x];   }   void doit(void) { printf( %d n , dfs(1, 0)); }   int main(void) {     read();     doit();   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     long long t;     cin >> t;     while (t--) {       long long n, i, j;       string s;       cin >> s;       long long x;       cin >> x;       n = s.length();       string w = s;       for (i = 0; i < n; i++) {         w[i] =  1 ;       }       for (i = 0; i < n; i++) {         if (s[i] ==  0 ) {           if (i - x >= 0) {             w[i - x] =  0 ;           }           if (i + x < n) {             w[i + x] =  0 ;           }         }       }       bool con = true;       for (i = 0; i < n; i++) {         if (s[i] ==  1 ) {           long long c1 = 0;           long long c2 = 0;           if (i - x >= 0) {             c1 = w[i - x] -  0 ;           }           if (i + x < n) {             c2 = w[i + x] -  0 ;           }           long long num = c1 | c2;           if (num != 1) {             con = false;             break;           }         }       }       if (con)         cout << w <<   n ;       else         cout <<  -1  n ;     }   }


#include <bits/stdc++.h>   using namespace std;   inline long long getnum() {     char c = getchar();     long long num, sign = 1;     for (; c <  0  || c >  9 ; c = getchar())       if (c ==  - ) sign = -1;     for (num = 0; c >=  0  && c <=  9 ;) {       c -=  0 ;       num = num * 10 + c;       c = getchar();     }     return num * sign;   }   int nxt[400005], A[400005], store[400005], at[400005];   set<int> Set;   set<pair<long long, long long> > Cur;   int main() {     int n = getnum(), k = getnum(), ans = 0;     for (int i = 1; i <= n; i++) A[i] = getnum();     for (int i = n; i >= 1; i--) {       if (store[A[i]] != 0)         nxt[i] = store[A[i]];       else         nxt[i] = n + 1;       store[A[i]] = i;     }     for (int i = 1; i <= n; i++) {       if (Set.find(A[i]) == Set.end()) {         ans++;         if (Set.size() == k) {           auto it = Cur.end();           it--;           Set.erase(it->second);           Cur.erase(it);         }         Set.insert(A[i]);         Cur.insert({nxt[i], A[i]});         at[A[i]] = i;       } else {         Cur.erase({nxt[at[A[i]]], A[i]});         Cur.insert({nxt[i], A[i]});         at[A[i]] = i;       }     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   int n, ar[N];   char mark[N] = {};   int sz[N] = {};   priority_queue<pair<int, int>, vector<pair<int, int> > > pq;   int main(void) {     cin.tie(0);     ios::sync_with_stdio(0);     cin >> n;     for (int i = 0; i < n; ++i) cin >> ar[i];     for (int i = 0; i < n; ++i) {       if (ar[i] != 0) {         pq.push(make_pair(ar[i], i));       } else {         sz[i] = min(3, (int)pq.size());         int tmp = sz[i];         while (tmp--) {           pair<int, int> x = pq.top();           pq.pop();           mark[x.second] = 1;         }         while (pq.size()) pq.pop();       }     }     int flag = 0;     for (int i = 0; i < n; ++i) {       if (ar[i] != 0) {         if (mark[i]) {           switch (flag) {             case 0:               cout <<  pushStack n ;               break;             case 1:               cout <<  pushQueue n ;               break;             case 2:               cout <<  pushFront n ;               break;           }           flag = (flag + 1) % 3;         } else           cout <<  pushBack n ;       } else {         int k = sz[i];         cout << k;         while (k--) {           flag = (flag - 1 + 3) % 3;           switch (flag) {             case 0:               cout <<   popStack ;               break;             case 1:               cout <<   popQueue ;               break;             case 2:               cout <<   popFront ;               break;           }         }         cout <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 5e2 + 10;   int ans, mac, n;   int main() {     ios_base::sync_with_stdio(false);     cin >> n;     while (ans < n) {       mac += 5;       int x = mac;       while (x % 5 == 0) {         x /= 5;         ans++;       }     }     if (ans == n) {       cout << 5 <<   n ;       for (int i = mac; i < mac + 5; ++i) {         cout << i <<    ;       }       cout <<   n ;       return 0;     }     cout << 0 <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 1000000007;   long long q;   long long md[255][255] = {0};   long long a[500005] = {0};   int main() {     long long op, x, y;     scanf( %lld , &q);     while (q--) {       scanf( %lld %lld %lld , &op, &x, &y);       if (op == 1) {         for (long long i = 1; i <= 250; i++) md[i][x % i] += y;         a[x] += y;       }       if (op == 2) {         long long ans = 0;         if (x > 250) {           for (long long i = y; i <= 500000; i += x) ans += a[i];         } else           ans = md[x][y];         printf( %lld n , ans);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, d[22];   int f[22], g[22], L[22], memF[22];   void moveG(int n, int a, int b, int c) {     if (n == 0) return;     moveG(n - L[n], a, c, b);     for (int i = 0; i < L[n]; i++) printf( %d %d n , a, c);     moveG(n - L[n], b, a, c);   }   void moveF(int n, int a, int b, int c) {     if (n == 0) return;     if (memF[n] == 0) {       moveG(n - 1, a, c, b);       printf( %d %d n , a, c);       moveG(n - 1, b, a, c);     } else {       int k = memF[n];       moveG(n - k, a, b, c);       for (int i = 0; i < k; i++) printf( %d %d n , a, b);       moveG(n - k, c, b, a);       for (int i = 0; i < k; i++) printf( %d %d n , b, c);       moveF(n - k, a, b, c);     }   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &d[i]);     reverse(d + 1, d + 1 + n);     for (int i = 1; i <= n; i++) {       L[i] = 1;       while (i - L[i] >= 1 && d[i - L[i]] == d[i]) L[i]++;     }     for (int i = 1; i <= n; i++) {       f[i] = 2 * g[i - 1] + 1;       memF[i] = 0;       for (int k = 1; k <= L[i]; k++) {         int tmp = 2 * g[i - k] + f[i - k] + 2 * k;         if (tmp < f[i]) f[i] = tmp, memF[i] = k;       }       g[i] = 2 * g[i - L[i]] + L[i];     }     printf( %d n , f[n]);     moveF(n, 1, 2, 3);   }


#include <bits/stdc++.h>   using namespace std;   long long p;   long long base;   char s[20];   long long a;   long long d;   int main(int argc, const char* argv[]) {     int T;     cin >> T;     while (T--) {       base = 1989;       d = 10;       scanf( %s , s);       int i;       for (i = 0, a = 0; s[i + 4] >=  0  && s[i + 4] <=  9 ; i++)         a = a * 10 + (long long)(s[i + 4] -  0 );       for (int j = 2; j <= i; j++) {         base += d;         d *= 10;       }       for (i = 0; i * d + a < base; i++)         ;       cout << i * d + a << endl;     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   vector<vector<pair<int, int>>> g;   vector<pair<int, pair<int, int>>> edg;   int cp[100010];   int find(int x) { return cp[x] = cp[x] == x ? x : find(cp[x]); }   void union_(int x, int y) { cp[find(x)] = find(y); }   int h[100010];   int anc[100010][18];   int val[100010][18];   void dfs_preprocess(int u, int p = -1) {     anc[u][0] = p;     if (~p) {       h[u] = h[p] + 1;     }     for (int i = 1; i < 18; ++i) {       if (~anc[u][i - 1]) {         anc[u][i] = anc[anc[u][i - 1]][i - 1];         val[u][i] = max(val[anc[u][i - 1]][i - 1], val[u][i - 1]);       }     }     for (auto v : g[u]) {       if (v.second != p) {         val[v.second][0] = v.first;         dfs_preprocess(v.second, u);       }     }   }   int LCA(int v, int u) {     if (h[v] < h[u]) swap(u, v);     int val_ans = 0;     for (int i = 18 - 1; i >= 0; --i) {       if (anc[v][i] + 1 && h[anc[v][i]] >= h[u]) {         val_ans = max(val_ans, val[v][i]);         v = anc[v][i];       }     }     if (v == u) return val_ans;     for (int i = 18 - 1; i >= 0; --i) {       if (anc[v][i] != anc[u][i]) {         val_ans = max({val_ans, val[v][i], val[u][i]});         v = anc[v][i];         u = anc[u][i];       }     }     val_ans = max({val_ans, val[v][0], val[u][0]});     return val_ans;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n, m;     cin >> n >> m;     for (int i = 0; i < n; ++i) cp[i] = i;     g.assign(n, vector<pair<int, int>>());     map<pair<int, int>, int> mp;     int id = 0;     for (int i = 0; i < m; ++i) {       int u, v, w;       cin >> u >> v >> w;       u--, v--;       mp[{u, v}] = id++;       edg.push_back({w, {u, v}});     }     sort((edg).begin(), (edg).end());     vector<int> mst(m, 0);     memset(anc, -1, sizeof(anc));     for (int i = 0; i < m; ++i) {       int u = edg[i].second.first, v = edg[i].second.second, w = edg[i].first;       if (find(u) != find(v)) {         union_(u, v);         g[u].push_back({w, v});         g[v].push_back({w, u});         mst[i] = 1;       }     }     dfs_preprocess(0);     vector<int> ans(m, -1);     for (int i = 0; i < m; ++i) {       if (!mst[i]) {         ans[mp[edg[i].second]] = LCA(edg[i].second.first, edg[i].second.second);       }     }     for (int u : ans) {       if (u + 1) cout << u <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   struct NODE {     char w[4];   };   NODE tmp;   vector<NODE> st, stt;   bool use[10];   int bull, cow;   char ss[6];   void back(int index) {     if (index == 4) {       int i, j, b, c;       for (i = 0, b = 0, c = 0; i < 4; ++i)         for (j = 0; j < 4; ++j)           if (tmp.w[i] == ss[j]) {             if (i == j)               b++;             else               c++;           }       if (b == bull && c == cow) {         st.push_back(tmp);       }     } else {       int i;       for (i = 0; i < 10; ++i) {         if (use[i]) continue;         tmp.w[index] =  0  + i;         use[i] = true;         back(index + 1);         use[i] = false;       }     }   }   int main() {     int i, k, j, num, m, n, b, c;     while (scanf( %d , &num) == 1) {       st.clear();       scanf( %s %d %d , ss, &bull, &cow);       memset(use, 0, sizeof(use));       back(0);       for (i = 2; i <= num; ++i) {         scanf( %s %d %d , ss, &bull, &cow);         stt.clear();         for (j = 0, k = st.size(); j < k; ++j) {           for (m = 0, b = 0, c = 0; m < 4; ++m) {             for (n = 0; n < 4; ++n) {               if (st[j].w[m] == ss[n]) {                 if (m == n)                   ++b;                 else                   ++c;               }             }           }           if (b == bull && c == cow) stt.push_back(st[j]);         }         st.clear();         for (j = 0, k = stt.size(); j < k; ++j) st.push_back(stt[j]);       }       if (st.size() > 1)         puts( Need more data );       else if (st.size() < 1)         puts( Incorrect data );       else {         for (i = 0; i < 4; ++i) printf( %c , st[0].w[i]);         putchar(  n );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long t, a, b;     cin >> t;     while (t--) {       cin >> a >> b;       if (a == b) {         cout << 0 <<   n ;       } else if (abs(a - b) <= 10) {         cout << 1 <<   n ;       } else if (abs(a - b) % 10 != 0) {         cout << (abs(b - a) / 10) + 1 <<   n ;       } else {         cout << (abs(a - b) / 10) <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int M = 100010;   long long MAX = 0;   vector<int> maxs;   vector<int> A[M];   map<pair<int, int>, int> mep;   map<pair<int, int>, int> tmpmep;   map<int, vector<pair<int, int> > > edges;   int weight[M];   int parent[M];   int compsize[M];   int visited[M];   int dp[M];   int f(int first) {     return parent[first] = parent[first] == first ? first : f(parent[first]);   }   void add(int a, int b) {     int first = f(a), second = f(b);     if (first != second) {       parent[first] = second;       compsize[second] += compsize[first];     }   }   int dfs1(int first) {     visited[first] = true;     int sum = 0;     for (int second : A[first])       if (!visited[second]) sum += dfs1(second);     return sum + compsize[first];   }   int dfs2(int first, int p, int total) {     visited[first] = true;     int sum = compsize[first];     for (int second : A[first])       if (!visited[second]) sum += dfs2(second, first, total);     long long trees = (long long)sum * (long long)(total - sum);     if (trees > MAX)       MAX = trees, maxs.clear(),       maxs.push_back(tmpmep[make_pair(min(p, first), max(p, first))]);     else if (trees == MAX)       maxs.push_back(tmpmep[make_pair(min(p, first), max(p, first))]);     return sum;   }   int main() {     ios_base::sync_with_stdio(false);     int i, j, k, n;     scanf( %d , &n);     for (int _ = 1; _ < n; _++) {       scanf( %d%d%d , &i, &j, &k);       mep[make_pair(min(i, j), max(i, j))] = _;       edges[k].push_back(make_pair(min(i, j), max(i, j)));       weight[_] = k;     }     for (i = 0; i <= n; i++) parent[i] = i, compsize[i] = 1;     for (auto elm : edges) {       set<int> nodes;       tmpmep.clear();       for (auto e : elm.second) {         int first = f(e.first), second = f(e.second);         nodes.insert(first);         nodes.insert(second);         A[first].push_back(second);         A[second].push_back(first);         tmpmep[make_pair(min(first, second), max(first, second))] = mep[e];       }       vector<pair<int, int> > roots;       for (auto first : nodes)         if (!visited[first]) roots.push_back(make_pair(first, dfs1(first)));       for (auto first : nodes) visited[first] = false;       for (auto p : roots) dfs2(p.first, -1, p.second);       for (auto e : elm.second) add(e.first, e.second);       for (auto first : nodes) {         A[first].clear();         visited[first] = false;       }     }     sort((maxs).begin(), (maxs).end());     printf( %lld %d n , 2 * MAX, (int)maxs.size());     for (auto m : maxs) printf( %d  , m);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> k;     vector<vector<int>> g(n);     for (int i = 0; i < n - 1; i++) {       int a, b;       cin >> a >> b;       a--;       b--;       g[a].push_back(b);       g[b].push_back(a);     }     vector<int> color(n);     vector<int> d(n);     vector<int> sub(n);     int iter = 0;     function<void(int, int, int, int)> dfs_to_color = [&](int v, int p, int c,                                                           int pr) {       color[v] = c;       for (int i : g[v]) {         if (i == p || color[i] != pr) continue;         dfs_to_color(i, v, c, pr);       }     };     function<void(int, int, int)> dfs_on_color = [&](int v, int p, int c) {       if (~p)         d[v] = d[p] ^ 1;       else         d[v] = 0;       sub[v] = 1;       for (int i : g[v]) {         if (color[i] != c || i == p) continue;         dfs_on_color(i, v, c);         sub[v] += sub[i];       }     };     vector<pair<int, int>> ret;     function<void(vector<int>)> solve = [&](vector<int> nodes) {       int sz = nodes.size();       if (sz <= 4) return;       int root = nodes[0];       int c = color[root];       dfs_on_color(root, -1, c);       int pr = -1;       while (true) {         int nxt = -1;         for (int i : g[root]) {           if (color[i] != c || i == pr) continue;           if (sub[i] > sz / 2) nxt = i;         }         if (nxt == -1) break;         pr = root;         root = nxt;       }       for (int i : g[root]) {         if (color[i] != c) continue;         iter++;         dfs_to_color(i, root, iter, c);       }       map<int, vector<int>> to_color;       for (int i : nodes) {         if (i == root) continue;         to_color[color[i]].push_back(i);       }       int pos = -1;       int cd = 0;       for (auto it : to_color) {         int id = it.first;         vector<int> nxt = it.second;         solve(nxt);         for (int i : nxt) color[i] = id;         dfs_on_color(nxt[0], -1, id);         if ((int)nxt.size() > cd) {           cd = nxt.size();           pos = id;         }       }       for (auto it : to_color) {         int id = it.first;         vector<int> vertices = it.second;         vector<int> to_root;         if (id != pos)           to_root = vertices;         else {           vector<int> cnt(2);           for (int i : vertices) cnt[d[i]]++;           int chosen = cnt[1] < cnt[0];           for (int i : vertices) {             if (d[i] == chosen) to_root.push_back(i);           }         }         for (int i : to_root) {           ret.emplace_back(i, root);         }       }     };     vector<int> foo(n);     iota(foo.begin(), foo.end(), 0);     solve(foo);     assert((int)ret.size() <= 10 * n);     cout << ret.size() << endl;     for (auto i : ret) {       cout << i.first + 1 <<     << i.second + 1 <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double Pi = acos(-1.0);   const long long MAXN = 1e6 + 5;   long long n, m, t;   struct NODE {     long long a, b;   } v[MAXN];   long long cmp1(NODE a, NODE b) { return a.a < b.a; }   long long a[MAXN], b[MAXN];   long long q[MAXN];   int main() {     cin >> t;     while (t--) {       cin >> m >> n;       for (long long i = 1; i <= n; i++) cin >> v[i].a >> v[i].b;       sort(v + 1, v + 1 + n, cmp1);       for (long long i = 1; i <= n; i++)         a[i] = v[i].a, b[i] = v[i].b, q[i] = q[i - 1] + a[i];       long long ans = 0;       for (long long i = 1; i <= n; i++) {         long long nowb = b[i];         long long index = lower_bound(a + 1, a + 1 + n, nowb) - a;         if (index == n + 1) {           ans = max(ans, a[i] + (m - 1) * b[i]);           continue;         }         if (index <= i) {           long long can = n - index + 1;           if (can >= m) {             ans = max(ans, q[n] - q[n - m]);             continue;           }           ans = max(ans, q[n] - q[index - 1] + b[i] * (m - can));         } else {           long long can = n - index + 1;           if (can >= m - 1) {             ans = max(ans, q[n] - q[n - m + 1] + a[i]);             continue;           }           ans = max(ans, q[n] - q[index] + a[i] + b[i] * (m - can - 1) +                              max(b[i], a[index]));         }       }       cout << ans << endl;     }   }


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


#include <bits/stdc++.h>   using namespace std;   const int inf = (int)1e9 + 100;   const long double eps = 1e-11;   const long double pi = acos(-1.0L);   int myrand() { return rand(); }   unsigned rdtsc() {     unsigned ans;     asm( rdtsc  :  =a (ans));     return ans;   }   int rnd(int x) { return myrand() % x; }   void precalc() {}   const int maxn = 1e5;   int n;   string str;   char s[maxn];   int dp[5][maxn];   bool read() {     if (scanf( %s , s) < 1) {       return false;     }     str = string(s);     n = strlen(s);     return true;   }   void solve() {     memset(dp, 0, sizeof(dp));     dp[2][n - 2] = 1;     dp[3][n - 3] = 1;     for (int i = n - 4; i >= 5; i--) {       for (int j = 2; j <= 3; j++) {         if (dp[j ^ 1][i + j]) {           dp[j][i] = 1;         }         if (i + j * 2 <= n) {           string s1 = str.substr(i, j);           string s2 = str.substr(i + j, j);           if (s1 != s2 && dp[j][i + j]) {             dp[j][i] = 1;           }         }       }     }     set<string> ans;     for (int i = 5; i < n; i++) {       for (int j = 2; j <= 3; j++) {         if (i + j > n) {           continue;         }         if (i + j == n - 1) {           continue;         }         string cur = str.substr(i, j);         if (dp[2][i + j] || dp[3][i + j] || n == i + j) {           ans.insert(cur);         }       }     }     printf( %d n , ((int)((ans).size())));     for (string s : ans) {       printf( %s n , s.c_str());     }   }   int main() {     srand(rdtsc());     precalc();     while (true) {       if (!read()) {         break;       }       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   class mat {    public:     long long a, b, c, d;   };   mat a;   long long n, k, l, m, step, res;   long long power(long long a, long long b);   mat mult(mat a, mat b);   mat power1(mat a, long long n);   int main() {     cin >> n >> k >> l >> m;     step = power(2, n);     a.a = a.b = a.c = 1;     a.d = 0;     a = power1(a, n + 1);     res = 1;     while (l > 0) {       if (k % 2)         res = (res * ((step + m - a.a) % m)) % m;       else         res = (res * a.a) % m;       k /= 2;       l--;     }     if (k > 0)       cout << 0;     else       cout << res % m;     return 0;   }   long long power(long long a, long long b) {     long long res;     if (b == 0) return 1;     res = power(a, b / 2) % m;     res = (res * res) % m;     if (b % 2) res = (res * a) % m;     return res;   }   mat mult(mat a, mat b) {     mat res;     res.a = (a.a * b.a + a.b * b.c) % m;     res.b = (a.a * b.b + a.b * b.d) % m;     res.c = (a.c * b.a + a.d * b.c) % m;     res.d = (a.c * b.b + a.d * b.d) % m;     return res;   }   mat power1(mat a, long long n) {     mat res, a1;     if (n == 0) {       res.a = res.d = 1;       res.b = res.c = 0;       return res;     }     res = power1(a, n / 2);     res = mult(res, res);     if (n % 2) {       a1.a = a1.b = a1.c = 1;       a1.d = 0;       res = mult(res, a1);     }     return res;   }


#include <bits/stdc++.h>   using namespace std;   const long long int MOD = 1000000007;   const long long int ll_MAX = 1e15;   long long int fast_expo(long long int x, long long int p) {     if (p == 0)       return 1;     else if (p % 2 == 0) {       long long int t = fast_expo(x, p / 2) % MOD;       return (t * t) % MOD;     } else       return (x * (fast_expo(x, p - 1)) % MOD) % MOD;   }   long long int invMod(long long int num) {     return fast_expo(num, MOD - 2) % MOD;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     long long int t_c;     t_c = 1;     for (long long int t_i = 0; t_i < t_c; ++t_i) {       string str;       cin >> str;       long long int n = str.size();       long long int mini[27][n], maxi[27][n];       memset(mini, -1, sizeof(mini));       memset(maxi, -1, sizeof(maxi));       for (long long int k = 1; k < 27; ++k) {         long long int cnt[26]{}, num = 0, j = 0;         for (long long int i = 0; i < n; ++i) {           while (j < n && num != k) {             long long int pos = str[j] -  a ;             if (cnt[pos] == 0) {               num++;             }             cnt[pos]++;             j++;           }           if (num == k) mini[k][i] = j - 1;           long long int pos = str[i] -  a ;           if (cnt[pos] == 1) {             num--;           }           cnt[pos]--;         }       }       for (long long int k = 1; k < 27; ++k) {         long long int cnt[26]{}, num = 0, j = 0;         for (long long int i = 0; i < n; ++i) {           while (j < n && num != k + 1) {             long long int pos = str[j] -  a ;             if (cnt[pos] == 0) {               num++;             }             cnt[pos]++;             j++;           }           if (num == k + 1)             maxi[k][i] = j - 2;           else if (num == k)             maxi[k][i] = n - 1;           long long int pos = str[i] -  a ;           if (cnt[pos] == 1) {             num--;           }           cnt[pos]--;         }       }       long long int ans[27]{}, num = 0;       for (long long int k = 1; k < 27; ++k) {         for (long long int i = 0; i < n; ++i) {           if (mini[k][i] != -1) ans[k] += maxi[k][i] - mini[k][i] + 1;         }         if (ans[k] > 0) {           num = k;         }       }       cout << num << endl;       for (long long int i = 1; i <= num; ++i) cout << ans[i] << endl;     }     return 0;   }