#include <bits/stdc++.h>   using namespace std;   const int maxn = 5e5 + 5, mod = 1e9 + 7, lg = 22;   int n, suf[maxn], lcp[maxn], Rank[lg][maxn], pw2[30], rmq[lg][maxn], lst,       seg[maxn * 4], klg[maxn], dp[maxn];   string s;   vector<pair<pair<int, int>, int>> V2;   void raDix_Sort() {     vector<vector<pair<pair<int, int>, int>>> V1(maxn), V3(maxn);     for (auto i : V2) V1[i.first.second + 1].push_back(i);     V2.clear();     for (int i = 0; i <= n; i++)       for (auto j : V1[i]) V3[j.first.first].push_back(j);     for (int i = 0; i < n; i++)       for (auto j : V3[i]) V2.push_back(j);   }   void build_suffix_array() {     vector<pair<int, int>> V;     V2.resize(n);     for (int i = 0; i < n; i++) V.push_back({s[i] -  a , i});     sort(V.begin(), V.end());     for (int i = 1; i < n; i++)       Rank[0][V[i].second] =           Rank[0][V[i - 1].second] + ((V[i].first) != (V[i - 1].first));     for (int i = 0; pw2[i] < n; i++) {       for (int j = 0; j < n; j++) {         int x = -1;         if (j + pw2[i] < n) x = Rank[i][j + pw2[i]];         V2[j] = {{Rank[i][j], x}, j};       }       raDix_Sort();       Rank[i + 1][V2[0].second] = 0;       for (int j = 1; j < n; j++)         Rank[i + 1][V2[j].second] =             Rank[i + 1][V2[j - 1].second] + ((V2[j].first) != (V2[j - 1].first));       lst = i + 1;       if (Rank[i + 1][V2[n - 1].second] == n - 1) break;     }     for (int i = 0; i < n; i++) suf[Rank[lst][i]] = i;     for (int i = 0; i < n - 1; i++) {       int p1 = suf[i], p2 = suf[i + 1];       for (int j = lst; j >= 0; j--) {         if (Rank[j][p1] != Rank[j][p2]) continue;         if (p1 >= n || p2 >= n) continue;         p1 += pw2[j];         p2 += pw2[j];       }       lcp[i] = p1 - suf[i];       rmq[0][i] = lcp[i];     }     n--;     for (int j = 1; j <= klg[n]; j++) {       for (int i = 0; i <= n - pw2[j]; i++)         rmq[j][i] = min(rmq[j - 1][i], rmq[j - 1][i + pw2[j - 1]]);     }     n++;   }   void fix(int p, int x, int v = 1, int s = 0, int e = n) {     if (e - s == 1) {       seg[v] = x;       return;     }     int mid = (e + s) >> 1, lc = v << 1, rc = lc | 1;     if (mid > p)       fix(p, x, lc, s, mid);     else       fix(p, x, rc, mid, e);     seg[v] = max(seg[lc], seg[rc]);   }   int get(int l, int r, int v = 1, int s = 0, int e = n) {     if ((l >= e) || (r <= s)) return 0;     if ((l <= s) && (e <= r)) return seg[v];     int mid = (e + s) >> 1, lc = v << 1, rc = lc | 1;     return max(get(l, r, lc, s, mid), get(l, r, rc, mid, e));   }   int get_mn(int l, int r) {     return min(rmq[klg[r - l]][l], rmq[klg[r - l]][r - pw2[klg[r - l]]]);   }   bool check(int l, int r) {     int len = r - l, p = Rank[lst][l], p1, p2, l1 = p, r1 = n;     while (l1 != r1 - 1) {       int mid = (l1 + r1) >> 1;       if (get_mn(l1, mid) >= len)         l1 = mid;       else         r1 = mid;     }     p2 = r1;     l1 = -1;     r1 = p;     while (l1 != r1 - 1) {       int mid = (l1 + r1) >> 1;       if (get_mn(mid, r1) >= len)         r1 = mid;       else         l1 = mid;     }     p1 = r1;     if (get(p1, p2) >= len)       return 1;     else       return 0;   }   int main() {     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);     pw2[0] = 1;     klg[1] = 0;     for (int i = 1; i < 25; i++) pw2[i] = (pw2[i - 1] * 2);     for (int i = 2; i < maxn; i++)       klg[i] = klg[i - 1] + (pw2[klg[i - 1]] * 2 == i);     cin >> n >> s;     build_suffix_array();     int ans = 1, l = n - 2, r = n - 1;     dp[n - 1] = 1;     fix(Rank[lst][n - 1], 1);     while (l >= 0) {       if (l == r - 1) {         dp[l] = 1;         l--;         continue;       }       if (!check(l, r - 1)) {         if (!check(l + 1, r)) {           r--;           fix(Rank[lst][r], dp[r]);           continue;         }       }       ans = max(ans, r - l);       dp[l] = r - l;       l--;     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 1e5 + 7;   const long long inf = 1e9 + 7;   const long long mod = 1e9 + 7;   int n;   int m;   int x;   int y;   vector<int> g[N];   int main() {     ios_base::sync_with_stdio(false);     cin >> n >> m;     for (int i = 1; i <= m; i++) {       cin >> x >> y;       g[x].push_back(y);       g[y].push_back(x);     }     int badbus = 0, cnt = 0;     for (int i = 1; i <= n; i++) {       badbus |= (g[i].empty());       if ((int)g[i].size() == 1) {         cnt++;         continue;       }       badbus |= ((int)g[i].size() != 2);     }     if (cnt == 2 && badbus == 0) {       cout <<  bus topology n ;       return 0;     }     int badring = 0;     for (int i = 1; i <= n; i++) {       badring |= ((int)g[i].size() != 2);     }     if (badring == 0) {       cout <<  ring topology n ;       return 0;     }     cnt = 0;     int badstar = 0;     for (int i = 1; i <= n; i++) {       if ((int)g[i].size() == n - 1) {         cnt++;         continue;       }       badstar |= ((int)g[i].size() != 1);     }     if (cnt == 1 && badstar == 0) {       cout <<  star topology n ;       return 0;     }     cout <<  unknown topology n ;   }


#include <bits/stdc++.h>   using namespace std;   inline int in() {     int32_t x;     scanf( %d , &x);     return x;   }   inline string get() {     char ch[1000010];     scanf( %s , ch);     return ch;   }   const int MAX_LG = 21;   const int maxn = 2e5 + 100;   const int base = 29;   const int mod = 1e9 + 7;   const int INF = 2e9 + 100;   int nxt[maxn];   int sz[maxn];   int deg[maxn];   bool mark[maxn];   bool dp[maxn];   bool dp2[maxn];   inline int root(int x) { return (nxt[x] == 0 ? x : nxt[x] = root(nxt[x])); }   inline void dfs(int v) {     mark[v] = true;     sz[v] = 1;     if (nxt[v] == 0) return;     int u = nxt[v];     if (mark[u])       sz[v] += sz[u];     else {       dfs(u);       sz[v] += sz[u];     }   }   int32_t main() {     int n = in(), id = in();     for (int i = 1; i <= n; i++) nxt[i] = in(), deg[nxt[i]]++;     for (int i = 1; i <= n; i++) {       if (!mark[i]) dfs(i);     }     vector<int> vc;     for (int i = 1; i <= n; i++)       if (deg[i] == 0 && root(i) != root(id)) vc.push_back(sz[i]);     dp[0] = true;     for (int i = 0; i < vc.size(); i++) {       for (int j = 0; j + vc[i] <= 1000; j++) dp2[j + vc[i]] |= dp[j];       for (int j = 0; j <= 1000; j++) dp[j] |= dp2[j], dp2[j] = false;     }     for (int x = 0; x <= 1000; x++) {       if (dp[x]) {         cout << x + sz[id] <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int n, c0, c1, C[70][50010];   const int N = 60, M = 40000;   struct atom {     long long w;     int num;   } A[50];   int len;   int check(long long lim) {     int now = min(1ll * n, lim / c1);     for (int i = 1; i <= N; i++) {       int j = 0;       while (1ll * i * c0 + 1ll * j * c1 <= lim && now < n) {         now = min(n, now + C[i][j]);         j++;       }     }     return now;   }   int compare(atom k1, atom k2) { return k1.w < k2.w; }   long long calc(long long lim) {     long long ans = 1ll * n * (c0 + c1);     int len = 0;     int k = lim / c1;     A[++len] = (atom){1ll * k * c1, 1};     int rem = n;     k--;     if (k >= 0) ans += 1ll * (k + 1) * k / 2 * c1, rem -= k + 1;     for (int i = 1; i <= N; i++) {       int j = 0;       while (1ll * i * c0 + 1ll * j * c1 <= lim) j++;       j--;       if (j < 0) continue;       A[++len] = (atom){1ll * i * c0 + 1ll * j * c1, C[i][j]};       j--;       while (j >= 0) {         ans += (1ll * i * c0 + 1ll * j * c1) * C[i][j];         rem -= C[i][j];         j--;       }     }     sort(A + 1, A + len + 1, compare);     for (int i = 1; i <= len; i++) {       int k = min(rem, A[i].num);       ans += 1ll * k * A[i].w;       rem -= k;     }     return ans;   }   int main() {     scanf( %d%d%d , &n, &c0, &c1);     if (c0 < c1) swap(c0, c1);     n--;     if (n == 0) {       cout << 0 << endl;       return 0;     }     if (c1 == 0) {       cout << 1ll * n * c0 << endl;       return 0;     }     for (int i = 0; i <= M; i++) C[0][i] = 1;     for (int i = 1; i <= N; i++) {       C[i][0] = 1;       for (int j = 1; j <= M; j++) C[i][j] = min(C[i - 1][j] + C[i][j - 1], n);     }     long long l = 0, r = 1e17, ans = 0;     while (l < r) {       long long mid = l + r >> 1;       if (check(mid) >= n) {         ans = mid;         r = mid;       } else         l = mid + 1;     }     cout << calc(ans) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   class input {    private:     bool fail;     istream* stream;       public:     input(istream& stream_ = cin) { stream = &stream_; }     istream& getStream() { return *stream; }     template <typename T = int>     T read() {       T value;       if (*stream >> value) {         fail = false;       } else {         fail = true;       }       return value;     }     template <typename T = int>     vector<T> readArray() {       size_t size = read<size_t>();       return readArray<T>(size);     }     template <typename T = int>     vector<T> readArray(size_t size) {       vector<T> v(size);       for (size_t i = 0; i < size; i++) {         if (*stream >> v[i]) {           fail = false;         } else {           fail = true;         }       }       return v;     }     bool isFailed() { return fail; }     static input& getStdin() {       static input in(cin);       return in;     }   };   class output {    private:     ostream* stream;       public:     output(ostream& stream_ = cout) { stream = &stream_; }     ostream& getStream() { return *stream; }     template <typename T>     output& put(T& value) {       *stream << value;       return *this;     }     template <typename T>     output& put(T& value, int precision) {       *stream << setprecision(precision) << value;       return *this;     }     output& flush() {       stream->flush();       return *this;     }     template <typename T>     output& write(const T& value) {       *stream << value << endl;       return *this;     }     template <typename T>     output& write(T& value, int precision) {       *stream << setprecision(precision) << fixed << value << endl;       return *this;     }     template <typename T>     output& writeArray(const vector<T>& v, bool appendSize = true) {       if (appendSize) {         write(v.size());       }       for (auto& x : v) {         *stream << x <<    ;       }       *stream << endl;       return *this;     }     output& writeLine() {       *stream << endl;       return *this;     }     output getStdout() {       output out(cout);       return out;     }     output getStderr() {       output err(cerr);       return err;     }   };   namespace std {   template <>   struct hash<pair<string, int>> {     size_t operator()(const pair<string, int>& x) const {       return hash<string>()(x.first) ^ hash<int>()(x.second);     }   };   };  // namespace std   void solve(input in, output out, output err) {     pair<string, int> root{};     unordered_map<pair<string, int>, vector<pair<string, int>>> data;     int n = in.read();     for (int i = 0; i < n; i++) {       string name = in.read<string>();       int ver = in.read();       pair<string, int> x = make_pair(name, ver);       if (root.first.empty()) {         root = x;       }       int x_deps_count = in.read();       vector<pair<string, int>> x_deps(x_deps_count);       for (int j = 0; j < x_deps_count; j++) {         string depName = in.read<string>();         int depVer = in.read();         x_deps[j] = make_pair(depName, depVer);       }       data[x] = x_deps;     }     vector<pair<string, pair<int, int>>> answer;     unordered_map<string, pair<int, int>> info;     queue<string> q;     q.push(root.first);     info[root.first] = make_pair(root.second, 0);     while (!q.empty()) {       auto& cur = q.front();       auto& curInfo = info[cur];       if (cur != root.first) {         answer.emplace_back(cur, curInfo);       }       auto deps = data.find(make_pair(cur, curInfo.first));       if (deps != data.end()) {         for (const auto& next : deps->second) {           pair<int, int> nextInfo = make_pair(next.second, curInfo.second + 1);           auto addPtr = info.emplace(next.first, nextInfo);           if (!addPtr.second) {             if (addPtr.first->second.second == nextInfo.second &&                 addPtr.first->second.first < nextInfo.first) {               info[next.first] = nextInfo;             }           } else {             q.push(next.first);           }         }       }       q.pop();     }     sort(answer.begin(), answer.end());     out.write(answer.size());     for (const auto& x : answer) {       out.put(x.first).put(   ).write(x.second.first);     }   }   int main() {     input in(cin);     output out(cout);     output err(cerr);     while (true) {       auto startT = clock();       solve(in, out, err);       auto endT = clock();       cerr << endl;       cerr <<  Working time:   << (endT - startT) <<   ms  << endl;       cerr <<  -------------------------  << endl;       bool ok = false;       while (!in.isFailed() && !ok) {         ok = in.read<char>() ==  $ ;       }       if (!ok) {         break;       }     }   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:64000000 )   using namespace std;   const int INF = (int)1E9;   const long long INF64 = (long long)1E18;   const long double EPS = 1E-9;   const long double PI = 3.1415926535897932384626433832795;   const int MAXN = 100100;   map<int, vector<pair<int, int> > > m;   vector<pair<long long, int> > l[MAXN];   void process(int c, vector<pair<int, int> > &a) {     sort((a).rbegin(), (a).rend());     long long sum = 0;     for (int i = 0; i < (int)(a.size()); i++) {       sum += a[i].first;       l[i + 1].push_back(make_pair(sum, c));     }   }   int main() {     int n;     cin >> n;     for (int i = 0; i < (int)(n); i++) {       int c, s;       scanf( %d%d , &c, &s);       m[c].push_back(make_pair(s, i));     }     for (map<int, vector<pair<int, int> > >::iterator i = m.begin(); i != m.end();          i++)       process(i->first, i->second);     for (int i = 0; i < (int)(n + 1); i++) sort((l[i]).rbegin(), (l[i]).rend());     long long sum = -INF;     int c1, n1, c2, n2;     for (int i = 1; i <= n; i++)       for (int j = 0; j < (int)(min(2, (int)l[i].size())); j++) {         for (int k = 0; k < (int)(min(2, (int)l[i].size())); k++) {           long long cur = l[i][j].first + l[i][k].first;           if (l[i][j].second != l[i][k].second && cur > sum) {             sum = cur;             c1 = l[i][j].second;             n1 = i;             c2 = l[i][k].second;             n2 = i;           }         }         for (int k = 0; k < (int)(min(2, (int)l[i - 1].size())); k++) {           long long cur = l[i][j].first + l[i - 1][k].first;           if (l[i][j].second != l[i - 1][k].second && cur > sum) {             sum = cur;             c1 = l[i][j].second;             n1 = i;             c2 = l[i - 1][k].second;             n2 = i - 1;           }         }       }     cout << sum << endl << n1 + n2 << endl;     vector<pair<int, int> > a1(m[c1].begin(), m[c1].begin() + n1);     vector<pair<int, int> > a2(m[c2].begin(), m[c2].begin() + n2);     for (int i = 0; i < (int)(min(n1, n2)); i++) {       printf( %d  , a1[i].second + 1);       printf( %d  , a2[i].second + 1);     }     if (n1 != n2) printf( %d , a1.back().second + 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long l, r, u, d;   long long n, x, y, c;   long long sqr(long long x) { return x * x; }   long long tri(long long x) { return (x + 1) * x / 2; }   bool solve(long long t) {     long long sum = t * t + (t + 1) * (t + 1);     if (t > l) sum -= sqr(t - l);     if (t > r) sum -= sqr(t - r);     if (t > u) sum -= sqr(t - u);     if (t > d) sum -= sqr(t - d);     if (t > l + d) sum += tri(t - (l + d) - 1);     if (t > l + u) sum += tri(t - (l + u) - 1);     if (t > r + d) sum += tri(t - (r + d) - 1);     if (t > r + u) sum += tri(t - (r + u) - 1);     if (sum >= c) return true;     return false;   }   int main() {     scanf( %I64d%I64d%I64d%I64d , &n, &x, &y, &c);     l = x - 1;     r = n - x;     u = y - 1;     d = n - y;     long long low = 0, high = 2 * n, mid;     while (low <= high) {       mid = (low + high) / 2;       if (solve(mid))         high = mid - 1;       else         low = mid + 1;     }     printf( %I64d n , low);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool check(pair<int, int> a, pair<int, int> b) {     if (a.first > b.first)       return true;     else if (a.first == b.first)       return a.second < b.second;     else       return false;   }   int main() {     int n, k;     cin >> n >> k;     pair<int, int> a[n];     map<pair<int, int>, int> mp;     for (int i = 0; i < n; i++) {       int p, t;       cin >> p >> t;       a[i] = make_pair(p, t);       mp[a[i]]++;     }     sort(a, a + n, check);     cout << mp[a[k - 1]] <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 15;   const char inf =  z ;   bool g[N][N];   vector<string> gen_cand_strings(int n);   void rec_gen(vector<string>& ans, string& cur, char c, int n);   vector<string> get_good_strings(vector<string>& cand, int n);   vector<string> gen_cand2_strings(int n);   void rec_gen2(vector<string>& ans, string& cur, char c, int n);   bool is_ok(string s, vector<string>& good, int n);   bool is_ok_rec(string& s, vector<string>& good, vector<bool>& done, int c,                  int n);   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, m, k;     cin >> n >> m >> k;     for (int i = 0; i < m; ++i) {       int u, v;       cin >> u >> v;       --u;       --v;       g[u][v] = g[v][u] = 1;     }     vector<string> cand = gen_cand_strings(n);     vector<string> good = get_good_strings(cand, n);     sort(good.begin(), good.end());     vector<string> cand2 = gen_cand2_strings(n);     sort(cand2.begin(), cand2.end());     cand2.erase(unique(cand2.begin(), cand2.end()), cand2.end());     long long ans = 0;     for (auto& s : cand2) {       if (is_ok(s, good, n)) {         long long tmp = 1;         int cnt = *max_element(s.begin(), s.end()) + 1;         for (int i = 0; i < cnt; ++i) {           tmp *= (k - i);         }         ans += tmp;       } else {       }     }     cout << ans <<   n ;   }   vector<string> gen_cand_strings(int n) {     vector<string> ans;     string base(n, inf);     rec_gen(ans, base, 0, n);     return ans;   }   void rec_gen(vector<string>& ans, string& cur, char c, int n) {     for (int i = 0; i < n; ++i) {       if (cur[i] == inf) {         cur[i] = c;         for (int j = i + 1; j < n; ++j) {           if (cur[j] == inf) {             cur[j] = c;             rec_gen(ans, cur, c + 1, n);             cur[j] = inf;           }         }         cur[i] = inf;         return;       }     }     ans.push_back(cur);   }   vector<string> get_good_strings(vector<string>& cand, int n) {     vector<string> good;     for (auto& s : cand) {       vector<vector<int>> pos(n / 2);       for (int i = 0; i < n; ++i) {         pos[s[i]].push_back(i);       }       int tot = (1 << (n / 2));       vector<vector<int>> dp(tot, vector<int>(n / 2, 0));       for (int i = 0; i < n / 2; ++i) {         dp[(1 << i)][i] = g[pos[i][0]][pos[i][1]];       }       for (int mask = 0; mask < tot; ++mask) {         if (__builtin_popcount(mask) < 2) {           continue;         }         for (int i = 0; i < n / 2; ++i) {           if (!((mask >> i) & 1)) {             continue;           }           int u0 = pos[i][0], u1 = pos[i][1];           int submask = mask - (1 << i);           for (int j = 0; j < n / 2; ++j) {             if (dp[submask][j]) {               int v0 = pos[j][0], v1 = pos[j][1];               if ((g[u0][v0] and g[u1][v1]) or (g[u0][v1] and g[u1][v0])) {                 dp[mask][i] = true;                 break;               }             }           }         }       }       for (int i = 0; i < n / 2; ++i) {         if (dp[tot - 1][i]) {           good.push_back(s);           break;         }       }     }     return good;   }   vector<string> gen_cand2_strings(int n) {     vector<string> ans;     string base(n, inf);     rec_gen2(ans, base, 0, n);     return ans;   }   void rec_gen2(vector<string>& ans, string& cur, char c, int n) {     for (int i = 0; i < n; ++i) {       if (cur[i] == inf) {         for (int j = i + 1; j < n; ++j) {           if (cur[j] == inf) {             for (int nc = 0; nc <= c; ++nc) {               cur[i] = cur[j] = nc;               rec_gen2(ans, cur, max(int(c), nc + 1), n);               cur[i] = cur[j] = inf;             }           }         }         return;       }     }     ans.push_back(cur);   }   bool is_ok(string s, vector<string>& good, int n) {     vector<bool> done(n, 0);     return is_ok_rec(s, good, done, 0, n);   }   bool is_ok_rec(string& s, vector<string>& good, vector<bool>& done, int c,                  int n) {     if (c == n / 2) {       auto it = lower_bound(good.begin(), good.end(), s);       if (it != good.end() and *it == s) {         return true;       }     } else {       for (int i = 0; i < n; ++i) {         if (!done[i]) {           for (int j = i + 1; j < n; ++j) {             if (!done[j] and s[i] == s[j]) {               char tmp = s[i];               s[i] = s[j] = c;               done[i] = done[j] = true;               if (is_ok_rec(s, good, done, c + 1, n)) {                 return true;               }               s[i] = s[j] = tmp;               done[i] = done[j] = false;             }           }           break;         }       }     }     return false;   }


#include <bits/stdc++.h>   #pragma optimize( O3 )   using namespace std;   const int up = 1e5 + 5;   int la, ra, ta, lb, rb, tb;   int gcd(int x, int y) {     if (x == 0 || y == 0) {       return x + y;     }     int r = x % y;     while (r) {       x = y;       y = r;       r = x % y;     }     return y;   }   int main() {     ios_base::sync_with_stdio(false);     cin >> la >> ra >> ta;     cin >> lb >> rb >> tb;     if (lb < la) {       swap(la, lb);       swap(ra, rb);       swap(ta, tb);     }     int g = gcd(ta, tb);     int rem = (lb - la) % g;     int res = 0;     if (rem != 0) {       res = max(res, min(g - rem + ra - la, rb - lb) - (g - rem) + 1);       res = max(res, min(ra - la, rem + rb - lb) - rem + 1);     } else {       res = max(res, min(ra - la, rb - lb) + 1);     }     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 2e18;   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long n;     cin >> n;     vector<long long> a(n);     map<long long, long long> dp;     for (long long i = 0; i < n; ++i) {       cin >> a[i];       dp[a[i]] = dp[a[i] - 1] + 1;     }     long long x, mx = -INF;     for (auto &[last, len] : dp) {       if (len > mx) {         mx = len;         x = last;       }     }     cout << mx <<   n ;     long long l = x - mx + 1;     for (long long i = 0; i < n; ++i) {       if (a[i] == l) {         cout << i + 1 <<    ;         ++l;       }     }   }


#include <bits/stdc++.h>   using namespace std;   set<long long> sset;   void add(int n) {     set<long long>::iterator it = sset.find(n);     if (it == sset.end()) {       sset.insert(n);     } else {       sset.erase(it);       add(n + 1);     }   }   int main() {     cin.sync_with_stdio(false);     int n;     cin >> n;     while (n--) {       int t;       cin >> t;       add(t);     }     set<long long>::iterator it = sset.end();     it--;     cout << *it - sset.size() + 1 << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int mx = 1e3 + 10;   int a[mx], b[mx];   int main() {     int n, k;     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d%d , a + i, b + i);     scanf( %d , &k);     for (int i = 1; i <= n; i++)       if (k <= b[i]) return 0 * printf( %d n , n - i + 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b;     cin >> a >> b;     a = abs(a - b);     b = a / 2;     a = a / 2 + (a & 1);     cout << (a * a + a) / 2 + (b * b + b) / 2 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int ask(int l) {     cout <<  ?  ;     for (int i = 0; i < n - 1; i++) cout << 1 <<    ;     cout << l << endl;     cout.flush();     int k;     cin >> k;     if (k == 0)       return 1;     else       return 0;   }   int ask1(int l, int x) {     cout <<  ?  ;     for (int i = 0; i < n - 1; i++) {       cout << x <<    ;     }     cout << l <<   n ;     cout.flush();     int k;     cin >> k;     return k;   }   int main() {     cin >> n;     int t = 0;     int k = n;     for (int i = 0; i < n - 1; ++i) {       if (ask(i + 2)) {         break;       }       k--;     }     int ans[n];     ans[n - 1] = k;     for (int i = 1; i <= n; i++) {       if (i != k) {         int m = ask1(i, k);         ans[m - 1] = i;       }     }     cout <<  !  ;     for (int i = 0; i < n; ++i) cout << ans[i] <<    ;     cout <<   n ;     cout.flush();   }


#include <bits/stdc++.h>   using namespace std;   int main(int argc, char const *argv[]) {     int n;     cin >> n;     vector<int> a;     for (int i = 0; i < n; ++i) {       int x;       cin >> x;       a.push_back(x);     }     int b = 0, c = n - 1;     while (a[b + 1] > a[b] && b < n - 1) {       b++;     }     while (a[c - 1] > a[c] && c > 0) {       c--;     }     int j = b;     while (a[j] == a[j + 1]) {       j++;     }     if (j == c) {       cout <<  YES  << endl;     } else {       cout <<  NO  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int a[100001];   vector<long long int> adj[100001];   long long int ans[21] = {};   long long int dp[100001][21][2];   void dfs(long long int i, long long int par) {     long long int u = 0;     for (long long int j = 0; j < adj[i].size(); j++) {       long long int x = adj[i][j];       if (x != par) {         dfs(x, i);         if (u == 0) {           u = 1;           long long int z = a[i];           for (long long int k = 0; k <= 20; k++) {             if (z % 2) {               dp[i][k][1] = dp[x][k][0] + 1;               dp[i][k][0] = dp[x][k][1];             } else {               dp[i][k][1] = dp[x][k][1];               dp[i][k][0] = dp[x][k][0] + 1;             }             z = z / 2;             ans[k] += dp[i][k][1];           }         } else {           long long int z = a[i];           for (long long int k = 0; k <= 20; k++) {             ans[k] += dp[i][k][1] * dp[x][k][0] + dp[i][k][0] * dp[x][k][1];             if (z % 2) {               dp[i][k][1] += dp[x][k][0];               dp[i][k][0] += dp[x][k][1];             } else {               dp[i][k][1] += dp[x][k][1];               dp[i][k][0] += dp[x][k][0];             }             z = z / 2;           }         }       }     }     if (u == 0) {       long long int z = a[i];       for (long long int k = 0; k <= 20; k++) {         if (z % 2) {           ans[k]++;           dp[i][k][1]++;         } else {           dp[i][k][0]++;         }         z = z / 2;       }     }   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int n;     cin >> n;     for (long long int i = 1; i <= n; i++) cin >> a[i];     for (long long int i = 1; i < n; i++) {       long long int x, y;       cin >> x >> y;       adj[x].push_back(y);       adj[y].push_back(x);     }     dfs(1, 0);     long long int f = 1, pt = 0;     for (long long int i = 0; i <= 20; i++) {       pt += f * ans[i];       f = f * 2;     }     cout << pt;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int T;     cin >> T;     while (T--) {       int n;       cin >> n;       n *= 2;       vector<int> a, b, x;       for (int i = 0; i < n; i++) {         int tn;         cin >> tn;         x.push_back(tn);       }       sort(x.begin(), x.end());       cout << x[n / 2] - x[n / 2 - 1] << endl;     }   }


#include<bits/stdc++.h>   #define rep(i, a, n) for(int i = a; i <= n; i++)   #define _rep(i, n, a) for(int i = n; i >= a; i--)   typedef long long LL;   using namespace std;   const int maxn = 2e5 + 10;   const int mod = 1004535809;   inline LL read(){    LL x = 0, fl = 1; char c = getchar();    while (c <  0  || c >  9 ) { if(c ==  - ) fl = -1; c = getchar(); }    while (c >=  0  && c <=  9 ) { x = x * 10 + c -  0 ; c = getchar(); }    return x * fl;   }   int n, m, t;   LL cnt[maxn];   int main(){    cin >> t;    while(t--){     cin >> n;     memset(cnt, 0, (n + 5) * sizeof(LL));     int x;     rep(i, 1, n) cin >> x, cnt[x]++;     LL ans = 0;     rep(i, 2, n - 1) ans += cnt[i - 1] * cnt[i] * cnt[i + 1];     rep(i, 1, n - 1) ans += cnt[i] * (cnt[i] - 1) / 2 * cnt[i + 1];     rep(i, 1, n - 1) ans += cnt[i] * cnt[i + 1] * (cnt[i + 1] - 1) / 2;     rep(i, 1, n - 2) ans += cnt[i] * (cnt[i] - 1) / 2 * cnt[i + 2];     rep(i, 1, n - 2) ans += cnt[i] * cnt[i + 2] * (cnt[i + 2] - 1) / 2;     rep(i, 1, n) ans += cnt[i] * (cnt[i] - 1) * (cnt[i] - 2) / 6;     cout << ans << endl;    }   }


#include <bits/stdc++.h>   const int maxnm = 9 + 2;   int n, m;   char map[maxnm][maxnm], best[maxnm][maxnm];   bool shape[4][3][3] = {{{1, 1, 1}, {0, 1, 0}, {0, 1, 0}},                          {{0, 1, 0}, {0, 1, 0}, {1, 1, 1}},                          {{1, 0, 0}, {1, 1, 1}, {1, 0, 0}},                          {{0, 0, 1}, {1, 1, 1}, {0, 0, 1}}};   inline bool valid(int x, int y, int k) {     if (x < 1 || x + 2 > n || y < 1 || y + 2 > m) return false;     for (int i = 0; i <= 2; ++i)       for (int j = 0; j <= 2; ++j) {         if (shape[k][i][j] && map[x + i][y + j] !=  . ) return false;       }     return true;   }   inline void set(int x, int y, int k, char c) {     for (int i = 0; i <= 2; ++i)       for (int j = 0; j <= 2; ++j) {         if (shape[k][i][j]) map[x + i][y + j] = c;       }   }   int ans;   void dfs(int x, int y, int now) {     if (now + ((n - x - 1) * m + m - y + 1) / 5 <= ans) return;     if (y > m) return dfs(x + 1, 1, now);     if (x <= n) {       for (int i = x; i <= n; ++i)         for (int j = (i == x ? y : 1); j <= m; ++j) {           for (int k = 0; k < 4; ++k) {             if (!valid(i, j, k)) continue;             set(i, j, k,  A  + now);             dfs(i, j + 1, now + 1);             set(i, j, k,  . );           }         }     }     if (now > ans) {       ans = now;       memcpy(best, map, sizeof(map));     }   }   int main() {     int a;     a = 1;     a = 0;     a = (a == 1);     scanf( %d%d , &n, &m);     memset(map,  . , sizeof(map));     memset(best,  . , sizeof(best));     dfs(1, 1, 0);     printf( %d n , ans);     for (int i = 1; i <= n; ++i, putchar(  n ))       for (int j = 1; j <= m; ++j) {         putchar(best[i][j]);       }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   void _R(T &x) {     cin >> x;   }   void _R(int &x) { scanf( %d , &x); }   void _R(long long &x) { scanf( %lld , &x); }   void _R(double &x) { scanf( %lf , &x); }   void _R(char &x) { scanf(  %c , &x); }   void _R(char *x) { scanf( %s , x); }   void R() {}   template <class T, class... U>   void R(T &head, U &...tail) {     _R(head);     R(tail...);   }   template <class T>   void _W(const T &x) {     cout << x;   }   void _W(const int &x) { printf( %d , x); }   void _W(const long long &x) { printf( %lld , x); }   void _W(const double &x) { printf( %.16f , x); }   void _W(const char &x) { putchar(x); }   void _W(const char *x) { printf( %s , x); }   template <class T, class U>   void _W(const pair<T, U> &x) {     _W(x.first);     putchar(   );     _W(x.second);   }   template <class T>   void _W(const vector<T> &x) {     for (auto i = x.begin(); i != x.end(); _W(*i++))       if (i != x.cbegin()) putchar(   );   }   void W() {}   template <class T, class... U>   void W(const T &head, const U &...tail) {     _W(head);     putchar(sizeof...(tail) ?     :   n );     W(tail...);   }   int MOD = 1e9 + 7;   void ADD(long long &x, long long v) {     x = (x + v) % MOD;     if (x < 0) x += MOD;   }   const int SIZE = 210;   int AA[SIZE][SIZE], BB[SIZE][SIZE];   int n, m;   int col[SIZE], row[SIZE];   void check(int x) {     for (int i = 0; i < (m); ++i) {       col[i] = (i >= x) ^ AA[0][i];       BB[0][i] = col[i] ^ AA[0][i];     }     for (int i = (1); i < (n); ++i) {       bool suc = 0;       for (int j = 0; j < (2); ++j) {         row[i] = AA[i][0] ^ j ^ col[0];         for (int k = 0; k < (m); ++k) {           BB[i][k] = AA[i][k] ^ row[i] ^ col[k];         }         if (BB[i][0] < BB[i - 1][m - 1]) continue;         bool fail = 0;         for (int k = (1); k < (m); ++k) {           if (BB[i][k] < BB[i][k - 1]) {             fail = 1;           }         }         if (fail) continue;         suc = 1;         break;       }       if (!suc) return;     }     W( YES );     for (int i = 0; i < (n); ++i) _W(row[i]);     W(  );     for (int i = 0; i < (m); ++i) _W(col[i]);     W(  );     exit(0);   }   int main() {     R(n, m);     for (int i = 0; i < (n); ++i)       for (int j = 0; j < (m); ++j) {         R(AA[i][j]);       }     for (int i = (1); i <= (m); ++i) {       check(i);     }     W( NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2e5 + 10;   long long spf[MAXN];   long long min1[MAXN];   long long min2[MAXN];   void sieve() {     spf[1] = 1;     for (int i = 2; i < MAXN; i++) spf[i] = i;     for (int i = 4; i < MAXN; i += 2) spf[i] = 2;     for (int i = 3; i * i < MAXN; i++) {       if (spf[i] == i) {         for (int j = i * i; j < MAXN; j += i)           if (spf[j] == j) spf[j] = i;       }     }   }   vector<pair<int, int>> getFactorization(int x) {     vector<pair<int, int>> ret;     while (x != 1) {       int cur = spf[x];       int cnt = 0;       while (x && spf[x] == cur) {         ++cnt;         x = x / spf[x];       }       ret.push_back({cur, cnt});     }     return ret;   }   void solve() {     int n;     cin >> n;     vector<int> arr(n);     for (auto &a : arr) {       cin >> a;     }     sieve();     memset(min1, -1, sizeof(min1));     memset(min2, -1, sizeof(min2));     vector<int> many(MAXN, 0);     for (int i = 0; i < n; ++i) {       int x = arr[i];       vector<pair<int, int>> fact = getFactorization(x);       for (auto ff : fact) {         int p = ff.first;         int c = ff.second;         if (min1[p] == -1) {           min1[p] = c;         } else if (min2[p] == -1) {           int m = min1[p];           if (c <= m) {             min1[p] = c;             min2[p] = m;           } else {             min1[p] = m;             min2[p] = c;           }         } else {           int m1 = min1[p];           int m2 = min2[p];           if (c <= m1) {             min1[p] = c;             min2[p] = m1;           } else if (c <= m2) {             min2[p] = m2;           }         }         ++many[p];       }     }     long long res = 1;     for (int i = 2; i < MAXN; ++i) {       if (many[i] == n) {         res *= pow(1ll * i, min2[i]);       } else if (many[i] == n - 1) {         res *= pow(1ll * i, min1[i]);       }     }     cout << res <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     int t;     t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long> v;   bool vis[10005];   long long n, m, k;   int main() {     cin >> n >> m >> k;     if ((n * m * 2) % k != 0)       puts( NO );     else {       puts( YES );       puts( 0 0 );       if ((n * 2) % k == 0) {         printf( %lld 0 n , n * 2 / k);         printf( %lld %lld n , n / 2, m);       } else if ((m * 2) % k == 0) {         printf( %lld 0 n , n);         printf( %lld %lld n , n / 2, m * 2 / k);       } else {         for (int i = 2; i * i <= k; i++) {           if (k % i == 0) {             while (k % i == 0) {               v.push_back(i);               k /= i;             }           }         }         if (k != 1) v.push_back(k);         long long len = v.size(), N = n, M = m;         for (int i = 0; i < len; i++) {           if (N % v[i] == 0) {             N /= v[i];             vis[i] = 1;           }         }         for (int i = 0; i < len; i++) {           if (vis[i]) continue;           if (M % v[i] == 0) {             M /= v[i];             vis[i] = 1;           }         }         long long cnt = 2;         for (int i = 0; i < len; i++) {           if (!vis[i]) cnt = 1;         }         if (N < n) {           printf( %lld 0 n , N * cnt);           printf( %lld %lld n , N, M);         } else {           printf( %lld 0 n , N);           printf( %lld %lld n , N, M * cnt);         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long double EPS = 1e-11;   int n, d;   const int M = 30001;   int f[M][250 * 2];   int v[M];   int solve(int i, int j) {     if (i >= M) return 0;     int jj = j - (d - 250);     if (f[i][jj] != -1) return f[i][jj];     f[i][jj] = 0;     int res = 0;     if (j == 1)       res += max(solve(i + j, j), solve(i + j + 1, j + 1)) + v[i];     else       res += max(solve(i + j - 1, j - 1),                  max(solve(i + j, j), solve(i + j + 1, j + 1))) +              v[i];     return f[i][jj] = res;   }   int main() {     ios_base::sync_with_stdio(false);     for (int i = 0; i < (int)M; i++)       for (int j = 0; j < (int)250 * 2; j++) f[i][j] = -1;     cin >> n >> d;     for (int i = 0; i < (int)n; i++) {       int j;       cin >> j;       v[j]++;     }     cout << solve(d, d) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000100, M = 100100, K = 1000100, mod = 1000000007;   int n, m, k, l = N, r = 0, tot = 0, p[K] = {1};   int main() {     scanf( %d%d%d , &n, &m, &k);     for (int i = 1, u, v; i <= m; ++i) {       scanf( %d%d , &u, &v);       if (v - u > 1) {         if (v - u != k + 1) {           puts( 0 );           return 0;         }         l = min(l, u);         r = max(r, u);         ++tot;       }     }     for (int i = 1; i <= k; ++i) p[i] = p[i - 1] * 2 % mod;     if (l == N && r == 0) {       int ans = 1;       for (int i = 1; i <= n - k + 1; ++i)         ans = (ans + p[min(k, n - i - k - 1)]) % mod;       printf( %d n , ans);     } else {       int ans = 0;       for (int i = 1; i <= n - k + 1; ++i)         if (i <= l && i + k >= r)           ans = (ans + p[min(k, n - i - k - 1) - tot + (i == l)]) % mod;       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long UNDEF = -1;   const int INF = 1 << 25;   template <typename T>   inline bool chkmax(T& aa, T bb) {     return aa < bb ? aa = bb, true : false;   }   template <typename T>   inline bool chkmin(T& aa, T bb) {     return aa > bb ? aa = bb, true : false;   }   static char stdinBuffer[1024];   static char* stdinDataEnd = stdinBuffer + sizeof(stdinBuffer);   static const char* stdinPos = stdinDataEnd;   void readAhead(size_t amount) {     size_t remaining = stdinDataEnd - stdinPos;     if (remaining < amount) {       memmove(stdinBuffer, stdinPos, remaining);       size_t sz = fread(stdinBuffer + remaining, 1,                         sizeof(stdinBuffer) - remaining, stdin);       stdinPos = stdinBuffer;       stdinDataEnd = stdinBuffer + remaining + sz;       if (stdinDataEnd != stdinBuffer + sizeof(stdinBuffer)) *stdinDataEnd = 0;     }   }   int readInt() {     readAhead(16);     int x = 0;     bool neg = false;     while (*stdinPos ==     || *stdinPos ==   n ) ++stdinPos;     if (*stdinPos ==  - ) {       ++stdinPos;       neg = true;     }     while (*stdinPos >=  0  && *stdinPos <=  9 ) {       x *= 10;       x += *stdinPos -  0 ;       ++stdinPos;     }     return neg ? -x : x;   }   char readCh() {     readAhead(16);     while (*stdinPos ==     || *stdinPos ==   n ) ++stdinPos;     char ans = *stdinPos;     ++stdinPos;     return ans;   }   const int mn = 1000000;   pair<int, int> vp[mn];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n = readInt();     for (int i = 0; i < n; i++) {       int val = readInt();       vp[i] = make_pair(val, i);     }     sort(vp, vp + n);     long long final = 0;     long long sign = -1;     for (int k = 0; k < 2; k++) {       set<int> s;       s.insert(-1);       s.insert(n);       for (int i = 0; i < n; i++) {         int x = vp[i].second;         auto got = s.insert(x);         auto it = got.first;         auto lit = it;         --lit;         auto rit = it;         ++rit;         long long l = x - *lit;         long long r = *rit - x;         long long prod = l * r;         long long val = (long long)vp[i].first * (long long)prod * sign;         final += val;       }       reverse(vp, vp + n);       sign = -sign;     }     printf( %lld n , final);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     for (int i = 0; i < t; i++) {       int n;       cin >> n;       int maxi = 0, mini = 1000000000;       vector<long long int> a(n, 0);       long long int xor1 = 0;       long long int sum1 = 0;       for (int i = 0; i < n; i++) {         cin >> a[i];         xor1 ^= a[i];         sum1 += a[i];       }       sum1 += xor1;       cout << 2 << endl << xor1 <<     << sum1 << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   char S[111111];   long long cnt1[26], cnt2[26 * 26];   int main() {     scanf( %s , S);     int len = strlen(S);     for (int i = 0; i < len; i++) {       for (int j = 0; j < 26; j++) cnt2[26 * j + S[i] -  a ] += cnt1[j];       cnt1[S[i] -  a ]++;     }     long long ans = 0;     for (int i = 0; i < 26; i++) ans = max(ans, cnt1[i]);     for (int i = 0; i < 26 * 26; i++) ans = max(ans, cnt2[i]);     printf( %lld , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, d = 2;   bool P[100100];   int A[100100];   vector<pair<int, int> > R;   void criba() {     for (int i = 0; i < 100100; i++) P[i] = 1;     P[0] = 0;     P[1] = 0;     for (int i = 2; i < 100100; i++) {       if (P[i]) {         for (int j = 2 * i; j < 100100; j += i) P[j] = 0;       }     }   }   void solve(int a, int b) {     if (a == b) return;     if (a > b) swap(a, b);     for (int i = b; i > a; i--)       if (P[i - a + 1]) {         swap(A[a], A[i]);         R.push_back(make_pair(a, i));         solve(i, b);         break;       }   }   int main() {     cin >> n;     for (int i = 1; i <= n; i++) cin >> A[i];     criba();     for (int i = 1; i <= n; i++) {       while (A[i] != i) solve(i, A[i]);     }     cout << R.size() << endl;     for (int i = 0; i < R.size(); i++) {       cout << R[i].first <<     << R[i].second << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100100;   inline long long int gcd(long long int ga, long long int gb) {     if (!gb) return ga;     return gcd(gb, ga % gb);   }   inline long long int lcm(long long int a, long long int b, long long int c) {     long long int ans = a * b / gcd(a, b);     return ans * c / gcd(ans, c);   }   int main() {     long long int n;     scanf( %lld , &n);     if (n == 1)       printf( 1 );     else if (n == 2)       printf( 2 );     else if (n & 1)       printf( %lld , n * (n - 1) * (n - 2));     else {       long long int ans(1);       for (int i = 1; i <= n; ++i) ans = max(ans, lcm(n, n - 1, i));       printf( %lld , max(ans, max((n - 1) * (n - 2) * (n - 3),                                   n * (n - 1) * (n - 2) / 2)));     }     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   int main() {     double n, m;     scanf( %lf %lf , &n, &m);     printf( %.6lf , pow(1.000000011, m) * n);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int b, x;     double a, c;     cin >> a >> b >> c;     a = ceil(a * (c / 100));     x = a;     x -= b;     if (x < 1) x = 0;     cout << x << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     int a[1200];     while (~scanf( %d , &n)) {       for (int i = 1; i <= 2 * n; i++) scanf( %d , &a[i]);       sort(a + 1, a + 2 * n + 1);       if (a[n] == a[n + 1])         printf( NO n );       else         printf( YES n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 30;   class Solution;   class LinearBasis {    public:     friend class Solution;     LinearBasis() : size(0) { fill(begin(bs), end(bs), 0); }     LinearBasis& operator=(const LinearBasis& other) {       copy(begin(other.bs), end(other.bs), begin(bs));       size = other.size;       return *this;     }     void add(int num) {       for (int i = 0; i < N; ++i) {         if (num & (1 << i)) {           if (bs[i] == 0) {             bs[i] = num;             ++size;             break;           } else {             num ^= bs[i];           }         }       }     }     bool isValid(int num) {       if (num == 0) {         return true;       }       for (int i = 0; i < N; ++i) {         if (num & (1 << i)) {           num ^= bs[i];           if (num == 0) {             return true;           }         }       }       return false;     }       protected:     int bs[N];     int size;   };   class Solution {    public:     Solution(vector<int>& _A, int n) : nums(_A), size(n) { init(); }     int query(int l, int x) {       if (l <= 0) {         return 0;       }       l = min(l, size);       if (lbs[l].isValid(x)) {         return powM(2, l - lbs[l].size);       } else {         return 0;       }     }       private:     int size;     vector<int> nums;     vector<LinearBasis> lbs;     const int M = 1e9 + 7;     void init() {       lbs.clear();       lbs.resize(size + 1);       for (int i = 0; i < size; ++i) {         int a = nums[i];         lbs[i + 1] = lbs[i];         lbs[i + 1].add(a);       }     }     long long normalize(long long a) {       a %= M;       if (a < 0) {         a += M;       }       return a;     }     long long addM(long long a, long long b) { return normalize(a + b); }     long long mulM(long long a, long long b) { return normalize(a * b); }     long long powM(long long x, long long e) {       long long res = 1;       while (e > 0) {         if (e & 0x1) {           res = mulM(res, x);         }         x = mulM(x, x);         e >>= 1;       }       return res;     }   };   int main(int argc, char** argv) {     ios::sync_with_stdio(false);     cin.tie(0);     int n, q;     cin >> n >> q;     vector<int> nums(n);     for (int i = 0; i < n; ++i) {       cin >> nums[i];     }     Solution sol(nums, n);     for (int i = 0, l = 0, x = 0; i < q; ++i) {       cin >> l >> x;       cout << sol.query(l, x) << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main(void) {     int n;     scanf( %d , &n);     for (int i = 30; i >= 0; i--) {       if (n >> i & 1) printf( %d  , i + 1);     }     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int a[100], b[100];   int res[100][3001][101] = {0};   bool us[100][3001][101] = {0};   const int mod = 1000000007;   int work(int ll, int pr = -1, int lpred = -1) {     if (ll < 0) return 0;     if (ll == 0) return 1;     if (pr != -1 && us[pr][ll][lpred] != 0) return res[pr][ll][lpred];     int r = 0;     for (int i = 0; i < n; i++)       if (i != pr) {         if ((lpred == -1) || (a[i] == lpred))           r = (r + work(ll - a[i], i, b[i])) % mod;         if ((a[i] != b[i]) && ((lpred == -1) || (b[i] == lpred)))           r = (r + work(ll - b[i], i, a[i])) % mod;       }     if (pr != -1) {       res[pr][ll][lpred] = r;       us[pr][ll][lpred] = 1;     }     return r;   }   int main() {     ios_base::sync_with_stdio(false);     int l;     cin >> n >> l;     for (int i = 0; i < n; i++) cin >> a[i] >> b[i];     cout << work(l);   }


#include <bits/stdc++.h>   using namespace std;   const long long maxn = 2e6 + 10;   long long n, a[maxn], Begin[maxn], Next[maxn], To[maxn], e, sum;   bool vis[maxn], use[maxn];   inline void add(long long u, long long v) {     To[++e] = v;     Next[e] = Begin[u];     Begin[u] = e;   }   inline void DFS(long long u, long long op) {     if (vis[u]) return;     vis[u] = true;     if (op == 0) {       use[u] = true;       sum += u;     }     for (long long i = Begin[u]; i; i = Next[i]) DFS(To[i], op ^ 1);   }   signed main() {     scanf( %lld , &n);     if (n % 2 == 0) {       puts( First );       for (long long i = (1), _end_ = (n); i <= _end_; ++i) printf( %lld  , i);       for (long long i = (1), _end_ = (n); i <= _end_; ++i) printf( %lld  , i);       fflush(stdout);       return 0;     }     puts( Second );     fflush(stdout);     for (long long i = (1), _end_ = (n + n); i <= _end_; ++i) {       long long x;       scanf( %lld , &x);       if (a[x]) {         add(a[x], i);         add(i, a[x]);       } else         a[x] = i;     }     for (long long i = (1), _end_ = (n); i <= _end_; ++i) {       add(i, i + n);       add(i + n, i);     }     for (long long i = (1), _end_ = (n + n); i <= _end_; ++i)       if (!vis[i]) DFS(i, 0);     if (sum % (n + n) == 0) {       for (long long i = (1), _end_ = (n + n); i <= _end_; ++i)         if (use[i]) printf( %lld  , i);     } else {       for (long long i = (1), _end_ = (n + n); i <= _end_; ++i)         if (!use[i]) printf( %lld  , i);     }     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   mt19937 gen(chrono::system_clock::now().time_since_epoch().count());   const int M = 3e5 + 7;   const int N = 1e6 + 7;   int n, q, l, r, t[N * 4], ans[N], k, x, tn = 1;   map<int, vector<int> > p;   map<int, vector<pair<int, int> > > otr;   map<int, vector<pair<int, int> > > R;   void update(int v, int tl, int tr, int pos, int x) {     if (tr == tl + 1) {       t[v] = min(t[v], x);       return;     }     int mid = (tl + tr) / 2;     if (pos < mid)       update(v << 1, tl, mid, pos, x);     else       update((v << 1) + 1, mid, tr, pos, x);     t[v] = min(t[v << 1], t[(v << 1) + 1]);     return;   }   long long query(int v, int tl, int tr, int l, int r) {     if (r <= tl || tr <= l) return 1e9;     if (l <= tl && tr <= r) return t[v];     int mid = (tl + tr) / 2;     return min(query(v << 1, tl, mid, l, r), query((v << 1) + 1, mid, tr, l, r));   }   int query(int l, int r) { return query(1, 0, tn, l, r); }   void update(int pos, int x) {     update(1, 0, tn, pos, x);     return;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> q;     while (tn < N) tn <<= 1;     for (int i = 0; i < n; i++) {       cin >> x;       if (p[x].size()) otr[i].push_back({p[x].back(), i - p[x].back()});       p[x].push_back(i);     }     for (int i = 0; i < q; i++) {       cin >> l >> r;       l--;       r--;       R[r].push_back({l, i});     }     for (int i = 1; i < tn * 2; i++) t[i] = 1e9 + 7;     for (int i = 0; i < q; i++) ans[i] = 1e9 + 7;     for (int i = 1; i < N; i++) {       for (int j = 0; j < otr[i].size(); j++)         update(otr[i][j].first, otr[i][j].second);       for (int j = 0; j < R[i].size(); j++)         ans[R[i][j].second] = query(R[i][j].first, i + 1);     }     for (int i = 0; i < q; i++) cout << (ans[i] >= n * 2 ? -1 : ans[i]) <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   std::vector<int> res;   std::vector<int> adj[200005];   std::vector<int> DEPTH(200005);   std::vector<int> NODENUM(200005);   std::vector<int> funct(200005);   vector<bool> taken(200005);   vector<bool> visited(200005);   int dfs(int v, int depth) {     int nodeNum = 0;     visited[v] = 1;     DEPTH[v] = depth;     for (int u : adj[v]) {       if (!visited[u]) {         nodeNum += dfs(u, depth + 1);       }     }     NODENUM[v] = nodeNum;     return NODENUM[v] + 1;   }   long long dfsV2(int v) {     visited[v] = 1;     long long ans = 0;     for (int u : adj[v]) {       if (!visited[u]) {         if (taken[u]) {           ans += (long long)(NODENUM[u] + 1) * DEPTH[u];         } else           ans += (long long)dfsV2(u);       }     }     return ans;   }   bool func(int x, int y) {     if (funct[x] > funct[y]) return true;     return false;   }   void solve() {     int n, k, x, y, i;     cin >> n >> k;     for (i = 0; i < n - 1; i++) {       cin >> x >> y;       adj[x].push_back(y);       adj[y].push_back(x);     }     dfs(1, 0);     for (i = 1; i <= n; i++) {       funct[i] = DEPTH[i] - NODENUM[i];       res.push_back(i);     }     sort(res.begin(), res.end(), func);     for (i = 0; i < k; i++) {       taken[res[i]] = 1;     }     for (i = 0; i < n + 2; i++) {       visited[i] = 0;     }     cout << dfsV2(1) <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int t;     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, dig = 0, rem;     cin >> n;     while (n != 0) {       rem = n % 10;       if (rem == 4 || rem == 7) {         dig++;       }       n = n / 10;     }     if (dig == 4 || dig == 7)       cout <<  YES ;     else       cout <<  NO ;   }


#include <bits/stdc++.h>   using namespace std;   int place_to_score[] = {25, 18, 15, 12, 10, 8, 6, 4, 2, 1};   int t, n;   map<string, int> ids;   int next_id = 1;   char names[50][64];   char tmp[64];   int score[50];   int places[50][50];   int main() {     scanf( %d , &t);     for (int i = 0; i < t; i++) {       scanf( %d , &n);       int place = 0;       for (int j = 0; j < n; j++) {         scanf( %s , tmp);         int id = ids[tmp];         if (!id) {           ids[tmp] = id = next_id++;           strcpy(names[id - 1], tmp);         }         id--;         if (place < 10) score[id] += place_to_score[place];         places[id][place]++;         place++;       }     }     vector<int> best;     int points = -1;     for (int i = 0; i < next_id - 1; i++) {       if (score[i] > points) {         best.clear();         points = score[i];       }       if (score[i] == points) best.push_back(i);     }     for (int place = 0; best.size() > 1 && place < next_id - 1; place++) {       vector<int> new_best;       points = -1;       for (int i = 0; i < best.size(); i++) {         if (places[best[i]][place] > points) {           new_best.clear();           points = places[best[i]][place];         }         if (places[best[i]][place] == points) new_best.push_back(best[i]);       }       best = new_best;     }     printf( %s n , names[best[0]]);     best.clear();     points = -1;     for (int i = 0; i < next_id - 1; i++) {       if (places[i][0] > points) {         best.clear();         points = places[i][0];       }       if (places[i][0] == points) best.push_back(i);     }     if (best.size() > 1) {       vector<int> new_best;       points = -1;       for (int i = 0; i < best.size(); i++) {         if (score[best[i]] > points) {           new_best.clear();           points = score[best[i]];         }         if (score[best[i]] == points) new_best.push_back(best[i]);       }       best = new_best;     }     for (int place = 1; best.size() > 1 && place < next_id - 1; place++) {       vector<int> new_best;       points = -1;       for (int i = 0; i < best.size(); i++) {         if (places[best[i]][place] > points) {           new_best.clear();           points = places[best[i]][place];         }         if (places[best[i]][place] == points) new_best.push_back(best[i]);       }       best = new_best;     }     printf( %s n , names[best[0]]);   }


#include <bits/stdc++.h>   using namespace std;   string dst__;   stringstream dss__;   void debug(stringstream& ds) {     cerr << endl;     return;   }   template <typename F, typename... R>   void debug(stringstream& ds, const F& f, const R&... r) {     string n;     ds >> n;     cerr << n <<  =  << f <<    ;     debug(ds, r...);   }   constexpr long long LINF = 334ll << 53;   constexpr int INF = 15 << 26;   constexpr long long MOD = 1E9 + 7;   void sieve(long long n, vector<long long>& p) {     vector<char> isprime(n + 1, 1);     isprime[0] = isprime[1] = 0;     for (long long i = 2; i <= n; i++) {       if (isprime[i]) {         p.push_back(i);         for (long long j = i * i; j <= n; j += i) isprime[j] = 0;       }     }   }   vector<long long> primes;   vector<vector<long long>> dp(22, vector<long long>(1000010));   long long r, c;   long long primeFactor(long long k) {     vector<pair<long long, int>> ret;     for (int i = 0;; ++i) {       if (primes[i] * primes[i] > k) {         if (k != 1ll) ret.push_back(make_pair(k, 1));         break;       }       if (k % primes[i] == 0) {         ret.push_back(make_pair(primes[i], 0));         while (k % primes[i] == 0) {           k /= primes[i];           ret.back().second++;         }       }     }     long long ans = 1;     for (auto& y : ret) {       ans = ans * dp[y.second][r] % MOD;     }     return ans;   }   int main() {     sieve(1000003, primes);     for (int i = 1; i < 22; ++i) {       dp[i][0] = 2;       if (i == 1) {         for (int j = 1; j < 1000010; ++j) {           dp[i][j] = j + 2;         }       } else {         for (int j = 1; j < 1000010; ++j) {           dp[i][j] = dp[i - 1][j] + dp[i][j - 1] % MOD;         }       }     }     int n;     cin >> n;     for (int i = 0; i < n; ++i) {       scanf( %lld%lld , &r, &c);       auto ans = primeFactor(c);       printf( %lld n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     int cnt[26] = {0};     for (int i = 0; i < s.size(); i++) {       cnt[s[i] -  a ]++;     }     int ans[26] = {0};     vector<int> v;     for (int i = 0; i < 26; i++) {       ans[i] = cnt[i] / 2;       cnt[i] = cnt[i] % 2;       if (cnt[i] == 1) v.push_back(i);     }     for (int i = 0; i < v.size() / 2; i++) {       ans[v[i]]++;     }     int tmp = -1;     if (v.size() % 2 == 1) {       tmp = v[v.size() / 2];     }     for (int i = 0; i < 26; i++) {       for (int j = 0; j < ans[i]; j++) {         cout << (char)( a  + i);       }     }     if (tmp != -1) {       cout << (char)( a  + tmp);     }     for (int i = 25; i >= 0; i--) {       for (int j = 0; j < ans[i]; j++) {         cout << (char)( a  + i);       }     }     return 0;   }


#include <bits/stdc++.h>   const double pi = acos(-1);   const int MOD = 1e9 + 7;   const int INF = 1e9 + 7;   const int MAXN = 1e6 + 5;   const double eps = 1e-9;   using namespace std;   double xp, yp, vp, x, y, v, r, R;   double P, len, L;   pair<double, double> pos[2];   double f(double dphi) {     if (dphi > pi) dphi = 2 * pi - dphi;     if (dphi > P) return ((dphi - P) * r + L) / v;     return sqrt(len * len + R * R - 2 * len * R * cos(dphi)) / v;   }   int main() {     scanf( %lf %lf %lf %lf %lf %lf %lf , &xp, &yp, &vp, &x, &y, &v, &r);     double lo = 0, hi = 1e18;     R = hypot(xp, yp);     double omega = vp / R;     double phi0 = 0;     if (x * yp == xp * y) {       if (x * xp < 0 || y * yp < 0) phi0 = pi;     } else {       phi0 = acos((x * xp + y * yp) / (hypot(xp, yp) * hypot(x, y)));       if (x * yp < xp * y) phi0 = 2 * pi - phi0;     }     len = hypot(x, y);     L = sqrt(len * len - r * r) + sqrt(R * R - r * r);     P = acos(r / len) + acos(r / R);     hi = fabs(R - len) / v + 2 * pi * R / vp;     while (abs(hi - lo) > eps) {       double T = (lo + hi) / 2.;       double dphi = fmod(omega * T + phi0, 2 * pi);       if (dphi < 0) dphi += 2 * pi;       if (f(dphi) < T)         hi = T;       else         lo = T;     }     printf( %.20f n , lo);     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:667772160 )   using namespace std;   struct __isoff {     __isoff() {       if (0) freopen( input.txt ,  r , stdin), freopen( output.txt ,  w , stdout);       srand( C  +  T  +  A  +  C  +  Y  +  M  +  B  +  A );     }     ~__isoff() {}   } __osafwf;   const unsigned long long p1 = 31;   const unsigned long long p2 = 29;   const double eps = 1e-8;   const double pi = acos(-1.0);   const int infi = 1e9 + 7;   const long long inf = 1e18 + 7;   const long long dd = 2e5 + 7;   const long long mod = 1e9 + 7;   const int logc = 20;   int up[dd][logc], H[dd], in[dd], out[dd], ti = 0;   vector<int> P[dd];   void dfs(int v, int p = 0, int h = 0) {     H[v] = h;     in[v] = ti++;     up[v][0] = p;     for (int i = 1; i < logc; i++) {       up[v][i] = up[up[v][i - 1]][i - 1];     }     for (auto it : P[v]) {       if (it != p) {         dfs(it, v, h + 1);       }     }     out[v] = ti++;   }   void build() { dfs(0); }   bool is(int a, int b) { return in[a] <= in[b] && out[b] <= out[a]; }   int lca(int a, int b) {     if (is(a, b)) {       return a;     }     if (is(b, a)) {       return b;     }     for (int i = logc - 1; i >= 0; i--) {       int t = up[a][i];       if (!is(t, b)) {         a = t;       }     }     return up[a][0];   }   int dis(int a, int b) { return H[a] + H[b] - 2 * H[lca(a, b)]; }   int main() {     int n;     cin >> n;     vector<int> E;     for (long long i = 0; i < (long long)n - 1; i++) {       int t;       cin >> t;       t--;       P[t].push_back(i + 1);       E.push_back(i + 1);     }     build();     int a = 0, b = 0, l = 0;     for (long long i = 0; i < (long long)n - 1; i++) {       int t = E[i];       if (l < dis(a, t)) {         l = dis(a, t);         b = t;       }       if (l < dis(b, t)) {         l = dis(b, t);         a = t;       }       cout << l <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int modulo = 1e9 + 7;   template <class __Ty>   bool mini(__Ty& a, __Ty b) {     return a > b ? ((a = b) | 1) : (0);   }   template <class __Ty>   bool maxi(__Ty& a, __Ty b) {     return a < b ? ((a = b) | 1) : (0);   }   template <class __Ty>   __Ty add(__Ty a, __Ty b) {     return (a + b) % (__Ty(modulo));   }   template <class __Ty>   __Ty sub(__Ty a, __Ty b) {     __Ty m(modulo);     return ((a - b) % m + m) % m;   }   int a[int(1e6) + 3] = {0};   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n, m;     cin >> n >> m;     if ((n <= m + 1) && (m <= 2 * (n + 1))) {       m -= n - 1;       for (int i = 1; i < n; ++i) a[i] = 1;       for (int i = 0; i <= n; ++i)         while (m && a[i] < 2) {           m--;           a[i]++;         }       for (int i = 0; i <= n; ++i) {         if (i > 0) cout << 0;         while (a[i]--) cout << 1;       }     } else       cout << -1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, c, d, e, f;     cin >> a >> b >> c >> d >> e >> f;     cout << (a + b + c) * (a + b + c) - (a * a + c * c + e * e);   }


#include <bits/stdc++.h>   using namespace std;   const long long MXN = 2e5 + 10;   long long n, m, sum, maxi;   long long A[MXN], Ans[MXN], Max[MXN];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     for (int i = 1; i <= m; i++) cin >> A[i], sum += A[i];     if (sum < n) return cout << -1, 0;     for (int i = 1; i <= m; i++) {       if (i > n - A[i] + 1) return cout << -1, 0;       Ans[i] = i;       Max[i] = max(Max[i - 1], i + A[i] - 1);     }     long long l = Max[m] + 1, r = n, p = m;     while (p >= 1 && l <= r) {       Ans[p] = r - A[p] + 1;       r = r - A[p];       l = Max[p - 1] + 1;       p--;     }     for (int i = 1; i <= m; i++) cout << Ans[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100010;   const int MOD = 1e9 + 7;   vector<pair<int, int> > G[maxn];   int vis[maxn];   int dp[maxn];   int dfs(int x, int v) {     vis[x] = 1;     dp[x] = v;     for (pair<int, int> e : G[x]) {       int a = e.first;       int b = 1 - e.second;       if (vis[a]) {         if (dp[a] != (dp[x] ^ b)) return 0;       } else {         if (!dfs(a, v ^ b)) return 0;       }     }     return 1;   }   int main() {     int n, m;     cin >> n >> m;     for (int i = 1; i <= m; i++) {       int x, y, v;       scanf( %d%d%d , &x, &y, &v);       G[x].push_back(make_pair(y, v));       G[y].push_back(make_pair(x, v));     }     long long ans = 0;     for (int i = 1; i <= n; i++) {       if (!vis[i]) {         if (!dfs(i, 0)) {           puts( 0 );           return 0;         } else {           if (!ans)             ans = 1;           else             ans = ans * 2 % MOD;         }       }     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   string t;   int ct, z;   vector<int> vi;   int gcd(int a, int b) {     if (!b) return a;     if (a % b == 0) return b;     return gcd(b, a % b);   }   int main() {     int a, b, k;     cin >> a >> b >> k;     if (a + k < b) {       printf( 0.0000000 n );       exit(0);     }     if (!b) {       printf( 1.000000 n );       exit(0);     }     double p = 1;     for (int i = 0; i <= k; ++i) {       p *= (1.0 * (b - i)) / (a + i + 1);     }     printf( %.6lf n , 1 - p);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 505;   int a[N];   int dp[N][N];   int deal(int L, int R) {     if (L >= R) return 1;     if (dp[L][R] != N) return dp[L][R];     if (a[L] == a[R]) dp[L][R] = min(dp[L][R], deal(L + 1, R - 1));     for (int i = L; i < R; ++i) {       dp[L][R] = min(dp[L][R], deal(L, i) + deal(i + 1, R));     }     return dp[L][R];   }   void gao(int n) {     for (int i = 0; i < N; ++i)       for (int j = 0; j < N; ++j) dp[i][j] = N;     int ans = deal(0, n - 1);     cout << ans << endl;   }   int main() {     int n;     while (cin >> n) {       for (int i = 0; i < n; ++i) {         cin >> a[i];       }       gao(n);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ld = long double;   const int MAX = (int)(2e5) + 7;   const int MOD = (int)(1e9) + 7;   const int INF_I = (int)(1e9) + 7;   const ll INF_LL = (ll)(1e18);   template <typename T>   void info(T& v) {     for (const auto& i : v) cout << i <<    ;     cout << endl;   }   template <typename T>   bool chkmin(T& a, T b) {     if (a > b) {       a = b;       return true;     }     return false;   }   template <typename T>   bool chkmax(T& a, T b) {     if (a < b) {       a = b;       return true;     }     return false;   }   void boot_up() {     cin.tie(nullptr);     cout.tie(nullptr);     ios_base::sync_with_stdio(false);   }   int q;   int n, k;   void solve();   signed main() {     boot_up();     cin >> q;     while (cin >> n >> k) solve();   }   void solve() {     string s;     cin >> s;     int curr_pos = 0;     string a =   , b =   , c =   ;     vector<string> mask = { R ,  G ,  B };     for (int i = 0; i < n; ++i) {       a += mask[curr_pos % 3];       b += mask[(curr_pos + 1) % 3];       c += mask[(curr_pos + 2) % 3];       curr_pos = (curr_pos + 1) % 3;     }     vector<int> pref_a(n);     vector<int> pref_b(n);     vector<int> pref_c(n);     pref_a[0] = (s[0] ==  R  ? 0 : 1);     pref_b[0] = (s[0] ==  G  ? 0 : 1);     pref_c[0] = (s[0] ==  B  ? 0 : 1);     for (int i = 1; i < n; ++i) {       pref_a[i] = pref_a[i - 1] + 1 - (int)(s[i] == a[i]);       pref_b[i] = pref_b[i - 1] + 1 - (int)(s[i] == b[i]);       pref_c[i] = pref_c[i - 1] + 1 - (int)(s[i] == c[i]);     }     int ans = INF_I;     for (int i = 0; i < n - k + 1; ++i) {       chkmin(ans, pref_a[i + k - 1] - (i == 0 ? 0 : pref_a[i - 1]));       chkmin(ans, pref_b[i + k - 1] - (i == 0 ? 0 : pref_b[i - 1]));       chkmin(ans, pref_c[i + k - 1] - (i == 0 ? 0 : pref_c[i - 1]));     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     long long n;     long long t1(0), t2(0);     long long mn = 0, mx = 0;     cin >> n;     vector<long> vec(n, 0);     for (long i = 0; i < n; i++) {       cin >> vec[i];     }     sort(vec.begin(), vec.end());     mn = vec[0];     mx = vec[n - 1];     for (long i = 0; i < vec.size(); i++) {       t1 += (vec[i] == mn);       t2 += (vec[i] == mx);     }     cout << mx - mn <<    ;     if (mx - mn == 0)       cout << n * (n - 1) / 2 << endl;     else       cout << t1 * t2 << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int NMAX = 1000000 + 5;   int N;   int p[2 * NMAX];   int main() {     cin >> N;     iota(p + 1, p + N + 1, 1);     int st = 1, dr = N;     for (int phase = 2; phase <= N; ++phase, ++st) {       int old = 0;       for (int i = st; i <= dr; i += phase) {         int aux = p[i];         p[i] = old;         old = aux;       }       p[++dr] = old;     }     for (int i = st; i <= dr; ++i) cout << p[i] <<    n [i == dr];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 3e5 + 7;   int n;   long long a[maxn];   long long work(long long m) {     long long res = 0;     while (m) {       if (m & 1) res++;       m >>= 1;     }     return res;   }   long long dp[maxn][2];   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) {       scanf( %lld , &a[i]);       a[i] = work(a[i]);     }     dp[0][0] = 0;     dp[0][1] = 0;     dp[1][0] = 0;     dp[1][1] = 0;     for (int i = 2; i <= n; ++i) {       if (a[i] & 1) {         dp[i][0] = dp[i - 1][1] + (a[i - 1] & 1);         dp[i][1] = dp[i - 1][0] + !(a[i - 1] & 1);       } else {         dp[i][0] = dp[i - 1][0] + !(a[i - 1] & 1);         dp[i][1] = dp[i - 1][1] + (a[i - 1] & 1);       }     }     long long ans = 0;     for (int i = 2; i <= n; ++i) {       int l_ = max(1, i - 119);       long long max_ = a[i], cnt = 0, sum = a[i];       for (int j = i - 1; j >= l_; --j) {         sum += a[j];         max_ = max(max_, a[j]);         if (sum & 1) continue;         if (max_ > sum / 2) cnt++;       }       ans += (dp[i][0] - cnt);     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     unsigned long int val;     long int index;     unsigned long int other;   };   bool cmp(node& a, node& b) {     if (a.val != b.val)       return a.val < b.val;     else       return a.other > b.other;   }   struct node2 {     bool operator()(const node& a, const node& b) const {       if (a.other != b.other)         return a.other < b.other;       else if (a.val != b.val)         return a.val < b.val;       else         return a.index < b.index;     }   };   int main() {     unsigned long int n;     scanf( %lu , &n);     vector<node> all(n);     for (unsigned long int i = 0; i < n; i++) {       scanf( %lu , &all[i].other);       scanf( %lu , &all[i].val);       if (all[i].other > all[i].val) swap(all[i].other, all[i].val);       all[i].index = i + 1;     }     sort(all.begin(), all.end(), cmp);     unsigned long int outer = 0, id = 0;     set<node, node2> ended;     for (unsigned long int i = 0; i < n; i++) {       auto it = ended.insert(all[i]).first;       auto it1 = it, it2 = it;       it1++;       if (it1 != ended.end()) {         outer = (*it).index;         id = (*it1).index;         break;       } else if (it2 != ended.begin()) {         it2--;         if ((*it2).other >= (*it).other) {           outer = (*it).index;           id = (*it2).index;         }       }     }     if (outer == 0)       printf( -1 -1 n );     else       printf( %lu %lu n , id, outer);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   void printvec(vector<T>& x, int a, int b) {     for (int i = (a); i <= (b); i++) cout << x[i] <<    ;     cout <<   n ;   }   const double PI = acos((double)-1);   const double EPSILON = 1e-7;   const int N = 1e6 + 1;   const int NN = 1e3 + 1;   const long long MOD = 998244353;   const long long oo = LLONG_MAX;   const int BASE = 10000;   void query() {     int n, x;     cin >> n >> x;     vector<vector<int>> g(n + 1);     for (int i = 1; i < n; i++) {       int u, v;       cin >> u >> v;       g[u].push_back(v);       g[v].push_back(u);     }     if ((int)(g[x]).size() <= 1) {       cout <<  Ayush             <<   n ;       return;     }     if (n % 2) {       cout <<  Ashish             <<   n ;     } else {       cout <<  Ayush             <<   n ;     }   }   void solve() {     int q;     cin >> q;     while (q--) {       query();     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(nullptr);     cout.tie(nullptr);     cout << fixed << setprecision(10);     solve();     cerr <<  Time elapsed:   << 1.0 * clock() / CLOCKS_PER_SEC <<   s. n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k;   string s;   int cnt;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> k;     cin >> s;     for (int i = 0; i < n; i++) {       if (s[i] ==  # ) {         cnt++;       } else {         if (cnt >= k) {           cout <<  NO  << endl;           return 0;         }         cnt = 0;       }     }     if (cnt >= k) {       cout <<  NO  << endl;       return 0;     } else {       cout <<  YES  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int N, A[100], i, j;   bool w;   string S;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> N;     for (i = 0; i < N; i++) {       cin >> A[i];     }     getline(cin, S);     for (j = 0; j < N; j++) {       getline(cin, S);       if (!w) {         for (i = S.length() - 1; i >= 0; i--) {           if (S[i] ==  a  || S[i] ==  i  || S[i] ==  u  || S[i] ==  e  ||               S[i] ==  o  || S[i] ==  y ) {             A[j]--;           }         }         if (A[j] != 0) {           w = 1;         }       }     }     if (w) {       cout <<  NO  << endl;     } else       cout <<  YES  << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 10;   int n, k;   vector<int> g[N];   int fa[N];   int dep[N];   bool vis[N];   int anc[N][35];   void dfs(int u, int f) {     fa[u] = f;     anc[u][0] = fa[u];     for (int i = 0; i < g[u].size(); i++) {       int v = g[u][i];       if (v != f) {         dep[v] = dep[u] + 1;         dfs(v, u);       }     }   }   void rdfs(int u, int f) {     vis[u] = 1;     if (u == f) return;     rdfs(fa[u], f);   }   void preprocess() {     for (int j = 1; (1 << j) < n; j++) {       for (int i = 0; i < n; i++) {         if (anc[i][j - 1] != -1) {           int a = anc[i][j - 1];           anc[i][j] = anc[a][j - 1];         }       }     }   }   int main() {     memset(anc, -1, sizeof(anc));     scanf( %d%d , &n, &k);     k = n - k;     for (int i = 1; i < n; i++) {       int x, y;       scanf( %d%d , &x, &y);       x--, y--;       g[x].push_back(y);       g[y].push_back(x);     }     dfs(n - 1, -1);     preprocess();     int num = 1;     vis[n - 1] = 1;     for (int i = n - 2; i >= 0; i--) {       if (vis[i]) continue;       int t = i;       for (int j = 20; j >= 0; j--) {         if (anc[t][j] == -1)           continue;         else {           if (!vis[anc[t][j]]) {             t = anc[t][j];           }         }       }       if (dep[i] - dep[t] + 1 + num <= k) {         num += dep[i] - dep[t] + 1;         rdfs(i, t);       }     }     for (int i = 0; i < n; i++) {       if (!vis[i]) printf( %d  , i + 1);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long read() {     char ch = getchar();     long long x = 0;     int op = 1;     for (; !isdigit(ch); ch = getchar())       if (ch ==  - ) op = -1;     for (; isdigit(ch); ch = getchar()) x = x * 10 + ch -  0 ;     return x * op;   }   inline void write(long long a) {     if (a < 0) putchar( - ), a = -a;     if (a >= 10) write(a / 10);     putchar( 0  + a % 10);   }   const int N = 1010;   int n, m, k;   long long a[N][N], s1[N][N], s2[N][N];   int main() {     n = read(), m = read(), k = read();     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         a[i][j] = a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1] + read();     for (int i = 1; i <= n; i++)       for (int j = 0; j <= m; j++) {         s1[i][j] = s1[i - 1][j + 1] + a[i][j] -                    ((i - k >= 0 && j + k <= m) ? a[i - k][j + k] : 0);         if (j)           s2[i][j] = s2[i - 1][j - 1] + a[i][j] -                      ((i - k >= 0 && j - k >= 0) ? a[i - k][j - k] : 0);       }     long long mx = -1, x, y;     for (int i = k; i <= n - k + 1; i++)       for (int j = k; j <= m - k + 1; j++) {         long long tmp = s1[i + k - 1][j] + s1[i - 1][j - k] -                         s2[i - 1][j + k - 1] - s2[i + k - 1][j - 1];         if (tmp > mx) {           mx = tmp;           x = i;           y = j;         }       }     write(x);     putchar(   );     write(y);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int oo = (int)1e9;   const double PI = 2 * acos(0);   const double eps = 1e-9;   inline int comp(const double &a, const double &b) {     if (fabs(a - b) < eps) return 0;     return a > b ? 1 : -1;   }   int di[] = {1, -1, 0, 0, 1, -1, 1, -1};   int dj[] = {0, 0, 1, -1, 1, -1, -1, 1};   int diK[] = {-2, -2, -1, 1, 2, 2, 1, -1};   int djK[] = {-1, 1, 2, 2, 1, -1, -2, -2};   int I, J;   inline bool val(const int &i, const int &j) {     if (i < 0 || j < 0 || i >= I || j >= J) return false;     return true;   }   int N;   int n;   int ret[109][109];   bool val() {     for (int i = 0; i < (int)(I); i++)       for (int j = 0; j < (int)(J); j++)         for (int k = 0; k < (int)(4); k++) {           int ni = i + di[k];           int nj = j + dj[k];           if (val(ni, nj) && abs(ret[i][j] - ret[ni][nj] == 1)) return 0;         }     return 1;   }   bool vis[10009];   bool dfs(int i, int j) {     if (i == I) return 1;     if (j == J) return dfs(i + 1, 0);     for (int a = 1; a <= I * J; a++)       if (!vis[a]) {         if (i && abs(a - ret[i - 1][j]) == 1) continue;         if (j && abs(a - ret[i][j - 1]) == 1) continue;         vis[a] = 1;         ret[i][j] = a;         if (dfs(i, j + 1)) return 1;         vis[a] = 0;       }     return 0;   }   int main() {     cin >> I >> J;     if (I * J <= 1000000) {       if (dfs(0, 0)) {         for (int i = 0; i < (int)(I); i++) {           for (int j = 0; j < (int)(J); j++) {             if (j) printf(   );             printf( %d , ret[i][j]);           }           printf(  n );         }       } else         cout << -1 << endl;       return 0;     }     vector<pair<int, int> > v;     for (int i = 0; i < (int)(I); i++)       for (int j = 0; j < (int)(J); j++) v.push_back(make_pair(i, j));     int cc = 1;     for (int i = 1; i < I * J; i += 2) ret[v[i].first][v[i].second] = cc++;     for (int i = 0; i < I * J; i += 2) ret[v[i].first][v[i].second] = cc++;     if (!val()) {       cc = 1;       for (int i = 0; i < I * J; i += 2) ret[v[i].first][v[i].second] = cc++;       for (int i = 1; i < I * J; i += 2) ret[v[i].first][v[i].second] = cc++;       if (!val()) {         cout << -1 << endl;         return 0;       }     }     for (int i = 0; i < (int)(I); i++) {       for (int j = 0; j < (int)(J); j++) {         if (j) printf(   );         printf( %d , ret[i][j]);       }       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string a;   int n;   int main() {     cin >> a;     scanf( %d , &n);     int i;     for (i = 0; i < a.size(); i++)       if (a[i] >=  A  && a[i] <=  Z ) a[i] =  a  + a[i] -  A ;     for (i = 0; i < a.size(); i++)       if (a[i] -  a  + 1 <= n) a[i] =  A  + a[i] -  a ;     cout << a;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int dx[4] = {1, -1, 0, 0};   int dy[4] = {0, 0, 1, -1};   void fast() {     ios_base::sync_with_stdio(NULL);     cin.tie(0);     cout.tie(0);   }   void online_judge() {     freopen( input.txt ,  r , stdin);     freopen( output.txt ,  w , stdout);   }   const int flag_max = 0x3f3f3f3f;   const long long OO = 2e9 + 10;   const double EPS = (1e-8);   int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }   long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }   long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; }   const long long MOD = 1e14 + 7;   long long Power(long long n, long long deg) {     if (!deg)       return 1;     else if (deg & 1)       return (Power(n, deg - 1) * n);     else {       long long half = Power(n, deg / 2);       return ((half * half));     }   }   int k, ans = 0;   string s;   vector<pair<char, char> > pr;   int main() {     fast();     cin >> s;     cin >> k;     for (int i = 0; i < k; i++) {       string q;       cin >> q;       int F = 0, S = 0;       for (int i = 0; i < s.size(); i++) {         if (s[i] == q[0] || s[i] == q[1])           if (s[i] == q[0])             F++;           else             S++;         else           ans += min(F, S), S = F = 0;       }       ans += min(F, S);     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long mod = 1000000007;   long long BASE = 101;   bool is_enough(int h, int n, int m) {     int n1 = h / 2;     int m1 = h / 3;     int mn1 = h / 6;     if (n1 - mn1 >= n && m1 - mn1 >= m) {       return true;     }     int need_n = max(0, n - (n1 - mn1));     int need_m = max(0, m - (m1 - mn1));     if (need_n + need_m <= mn1) {       return true;     }     return false;   }   int main() {     std::cout.precision(15);     std::ios_base::sync_with_stdio(false);     int n, m;     cin >> n >> m;     int l = 2;     int r = 6000000;     while (r - l > 1000) {       int mid = l + (r - l) / 2;       if (is_enough(mid, n, m)) {         r = mid;       } else {         l = mid;       }     }     for (int i = l; i <= r; ++i) {       if ((i % 2 == 0 || i % 3 == 0) && is_enough(i, n, m)) {         cout << i;         break;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 200500;   const int inf = 2e9;   const double eps = 1e-8;   const int base = 1073676287;   long long a[maxn];   int b[maxn];   int c[maxn];   int bit[64];   int bits2[64];   void check(int n) {     long long sum = 0LL;     for (int j = 0; j < n; j++) {       sum += a[j];       int x = a[j];       int pos = 0;       while (x) {         bit[pos++] += x % 2;         x /= 2;       }     }     for (int j = 0; j < n; j++) {       int x = a[j];       int pos = 0;       for (int i = 0; i < 64; i++) bits2[i] = 0;       while (x) {         bits2[pos++] = x % 2;         x /= 2;       }       long long cnt = 0LL;       long long deg = 1LL;       for (int i = 0; i < 60; i++) {         if (bits2[i]) cnt += 1LL * bit[i] * deg;         deg *= 2LL;       }       if (cnt != b[j]) {         puts( -1 );         exit(0);       }       if (1LL * a[j] * n + sum != 1LL * b[j] + 1LL * c[j]) {         puts( -1 );         exit(0);       }     }   }   int main() {     srand(time(0));     int n;     long long sum = 0LL;     scanf( %d , &n);     for (int j = 0; j < n; j++) {       scanf( %d , &b[j]);       sum += 1LL * b[j];     }     for (int j = 0; j < n; j++) {       scanf( %d , &c[j]);       sum += 1LL * c[j];     }     if (sum % (2LL * n) != 0LL) {       puts( -1 );       return 0;     }     sum /= 2LL * n;     for (int j = 0; j < n; j++) {       int cur = b[j] + c[j];       a[j] = cur - sum;       if (a[j] < 0LL) {         puts( -1 );         return 0;       }       if (1LL * a[j] % n != 0LL) {         puts( -1 );         return 0;       }       a[j] /= 1LL * n;     }     check(n);     for (int j = 0; j < n; j++) cout << a[j] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct wor {     int x, y;     bool start;   };   struct node {     int maxN;     int plus;   };   vector<node> t(16 * 1000000 + 1000, {0, 0});   int m = 4 * 1000000 + 5;   bool operator<(wor a, wor b) { return a.y < b.y or a.y == b.y and a.x < b.x; }   void update(int v, int l, int r, int a, int b, int val) {     if (l >= b or r <= a) {       return;     } else if (a <= l and r <= b) {       t[v].plus += val;     } else {       int m = (l + r) / 2;       update(2 * v + 1, l, m, a, b, val);       update(2 * v + 2, m, r, a, b, val);       t[v].maxN = max(t[2 * v + 1].maxN + t[2 * v + 1].plus,                       t[2 * v + 2].maxN + t[2 * v + 2].plus);     }   }   int query(int v, int l, int r, int a, int b) {     if (l >= b or r <= a) {       return -1;     } else if (a <= l and r <= b) {       return t[v].maxN + t[v].plus;     } else {       int m = (l + r) / 2;       return max(query(2 * v + 1, l, m, a, b), query(2 * v + 2, m, r, a, b)) +              t[v].plus;     }   }   int main() {     int add = 1000000;     ios_base ::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     int r;     cin >> r;     r *= 2;     vector<wor> a(2 * n);     for (int i = 0; i < n; i++) {       int x, y;       cin >> x >> y;       int newX = x + y;       int newY = x - y;       newX += add;       newY += add;       a[2 * i] = {newX, newY, true};       a[2 * i + 1] = {newX, newY + r + 1, false};     }     int best = 0;     sort(a.begin(), a.end());     for (auto e : a) {       if (e.start) {         update(0, 0, m, e.x, e.x + r + 1, 1);         int q = query(0, 0, m, e.x, e.x + r + 1);         best = max(best, q);       } else {         update(0, 0, m, e.x, e.x + r + 1, -1);       }     }     cout << best;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int t;   typedef long long LL;   set<LL>a;   int main(){    for(LL i = 1;i <= 10000;i++)a.insert(i*i*i);    scanf( %d ,&t);    while(t--){     LL x;     cin >> x;     int flag = 0;     for(LL i = 1;i <= 10000;i++)      if(a.count(x-i*i*i)){       puts( YES );       flag = 1;       break;      }     if(!flag)puts( NO );    }    return 0;   }


#include <bits/stdc++.h>   using namespace std;   int pp(int x, int y) {     int i = 0;     int min = 1;     for (i = 0; i < y; i++) {       min = min * x;     }     return min;   }   int main() {     int n;     int a[100001];     int i, k, t, flag;     long long int min;     while (scanf( %d , &n) != EOF) {       for (i = 1; i <= n; i++) {         cin >> a[i];       }       min = 0;       for (k = 1; k < n; k++) {         for (t = 0; k + pp(2, t) <= n; t++) {           flag = k + pp(2, t);         }         min = min + a[k];         a[flag] += a[k];         a[k] = 0;         cout << min << endl;       }     }     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, i, j;     cin >> t;     while (t--) {       long long n;       cin >> n;       set<long long> s;       s.insert(0);       s.insert(1);       for (i = sqrt(n); i > 0; i--) {         s.insert(n / i);         s.insert(i);       }       cout << s.size() <<   n ;       for (auto i = s.begin(); i != s.end(); i++) cout << *i <<    ;       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1000000007;   const int inf = 50000000;   const int maxn = 100010;   int check(vector<int> &v) {     int i, first, cur, j;     for (i = 0; i < v.size(); i++) {       first = j = 0;       cur = v[i];       while (j < 8) {         if (cur == 1) {           first = 1;           break;         }         cur = v[cur];         j++;       }       if (!first) return 0;     }     return 1;   }   int generator(vector<int> &v, int left, int k) {     if (left == 0) return check(v);     int i, ret = 0;     for (i = 1; i <= k; i++) {       v.push_back(i);       ret += generator(v, left - 1, k);       v.pop_back();     }     return ret;   }   long long modpow(long long a, long long b) {     long long x = 1, y = a;     while (b) {       if (b % 2) x = (x * y) % mod;       y = (y * y) % mod;       b /= 2;     }     return x;   }   long long temp[] = {0, 1, 2, 9, 64, 625, 7776, 117649, 2097152};   int main() {     int n, k;     scanf( %d , &n), scanf( %d , &k);     vector<int> v;     long long ans = temp[k];     ans = (ans * modpow(n - k, n - k)) % mod;     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int nBox, testC, iBox, nBird, A[109];   int main() {     scanf( %d , &nBox);     for (int i = 0; i < nBox; ++i) scanf( %d , &A[i]);     scanf( %d , &testC);     for (int i = 0; i < testC; ++i) {       scanf( %d%d , &iBox, &nBird);       --iBox;       if (iBox != 0) A[iBox - 1] += nBird - 1;       if (iBox != nBox - 1) A[iBox + 1] += A[iBox] - nBird;       A[iBox] = 0;     }     for (int i = 0; i < nBox; ++i) printf( %d n , A[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[100005];   int calc(int x, int k) {     int SG;     if (k % 2 == 0) {       if (x >= 3)         SG = !(x % 2);       else         SG = x;     } else {       if (x >= 6) {         if (x % 2)           SG = 0;         else {           if (calc(x / 2, k) == 1)             SG = 2;           else             SG = 1;         }       } else {         if (x == 4)           SG = 2;         else if (x == 5)           SG = 0;         else           SG = x % 2;       }     }     return SG;   }   int main() {     int n, k;     cin >> n >> k;     for (int i = 0; i < n; i++) scanf( %d , &a[i]);     int res = 0;     for (int i = 0; i < n; i++) {       res ^= calc(a[i], k);     }     puts(res > 0 ?  Kevin  :  Nicky );   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     ;     cout.tie(0);     int n, i;     cin >> n;     int a[n], f[105] = {0};     int m = 0;     for (i = 1; i <= n; i++) {       cin >> a[i];       f[a[i]]++;       if (f[a[i]] > m) m = f[a[i]];     }     cout << m << endl;   }


#include <bits/stdc++.h>   using namespace std;   signed main() {     int q;     cin >> q;     while (q--) {       int n;       cin >> n;       string a, b;       cin >> a;       cin >> b;       if (n == 1) {         if (a[0] ==  1  || a[0] ==  2 )           cout <<  NO n ;         else if (b[0] ==  1  || b[0] ==  2 )           cout <<  NO n ;         else           cout <<  YES n ;         continue;       }       a = a + b;       int i = 0;       bool dir = false;       while (i != 2 * n - 1) {         if (!dir) {           if (a[i] ==  1  || a[i] ==  2 ) {             i++;             if (i == n) break;             dir = false;           } else {             if (i < n)               i = i + n;             else               i = i - n;             dir = true;           }         } else {           if (a[i] ==  1  || a[i] ==  2 ) {             break;           } else {             i++;             dir = false;           }         }       }       if (i != 2 * n - 1)         cout <<  NO n ;       else {         if (!dir) {           if (a[i] ==  2  || a[i] ==  1 )             cout <<  YES n ;           else             cout <<  NO n ;         } else {           if (a[i] ==  2  || a[i] ==  1 )             cout <<  NO n ;           else             cout <<  YES n ;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   const long long int MOD = 1000000007;   const long double PI = 2 * acos(0.0);   const vector<long long int> dx = {1, -1, 0, 0};   const vector<long long int> dy = {0, 0, 1, -1};   vector<long long int> ga(long long int n, bool oneIndexed = false) {     vector<long long int> a;     if (oneIndexed) a.push_back(0ll);     for (long long int i = 0; i < n; i++) {       long long int p;       cin >> p;       a.push_back(p);     }     return move(a);   }   vector<unsigned long long int> gau(unsigned long long int n,                                      bool oneIndexed = false) {     vector<unsigned long long int> a;     if (oneIndexed) a.push_back(0llu);     for (unsigned long long int i = 0; i < n; i++) {       unsigned long long int p;       cin >> p;       a.push_back(p);     }     return move(a);   }   vector<string> gas(unsigned long long int n, bool oneIndexed = false) {     vector<string> a;     if (oneIndexed) a.push_back(  );     for (unsigned long long int i = 0; i < n; i++) {       string p;       cin >> p;       a.push_back(p);     }     return move(a);   }   template <typename T, typename A>   void pa(vector<T, A> const &a, long long int begin = 0,           long long int end = -1) {     if (end == -1) end = a.size() - 1;     for (long long int i = begin; i <= end; i++) {       cout << a[i] <<    ;     }     cout <<   n ;   }   template <typename T, typename A>   void papair(vector<T, A> const &a, long long int begin = 0,               long long int end = -1) {     if (end == -1) end = (long long int)a.size() - 1;     for (long long int i = begin; i <= end; i++) {       cout << a[i].first <<     << a[i].second <<   n ;     }     cout <<   n ;   }   void yes() { cout <<  YES n ; }   void no() { cout <<  NO n ; }   void yesno(int f) {     if (f)       yes();     else       no();   }   void solve() {     long long int n;     cin >> n;     vector<pair<long long int, long long int>> edges;     long long int p1 = n / 2, p2 = n / 2;     if (p1 + p2 < n) p2++;     for (long long int i = 1; i <= p1; i++) {       for (long long int j = p1 + 1; j <= n; j++) {         edges.push_back({i, j});       }     }     cout << (long long int)edges.size();     cout <<   n ;     papair(edges);   }   int main(void) {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n;   vector<long long> G[500005];   long long t1, d1, t2, d2;   long long dis1[500005], dis2[500005];   long long inp[500005], in[500005];   stringstream ss;   void dfs1(long long u, long long fa, long long d) {     if (d > d1) {       d1 = d;       t1 = u;     }     for (long long i = 0; i < G[u].size(); i++) {       if (G[u][i] == fa) continue;       dfs1(G[u][i], u, d + 1);     }   }   void dfs2(long long u, long long fa, long long d) {     if (d > d2) {       d2 = d;       t2 = u;     }     dis1[u] = d;     for (long long i = 0; i < G[u].size(); i++) {       if (G[u][i] == fa) continue;       dfs2(G[u][i], u, d + 1);     }   }   void dfs3(long long u, long long fa, long long d) {     dis2[u] = d;     for (long long i = 0; i < G[u].size(); i++) {       if (G[u][i] == fa) continue;       dfs3(G[u][i], u, d + 1);     }   }   void findpath(long long u, long long fa, long long t) {     inp[u] = true;     if (u == t) {       memcpy(in, inp, sizeof inp);       return;     }     for (long long i = 0; i < G[u].size(); i++) {       if (G[u][i] == fa) continue;       findpath(G[u][i], u, t);     }     inp[u] = false;   }   long long ans;   bool del[500005];   long long deg[500005];   void dfs4(long long u, long long fa) {     del[u] = true;     if ((dis1[u] > dis2[u] || del[t2]) && !del[t1]) {       ans += dis1[u];       ss << t1 <<     << u <<     << u << endl;     } else if (!del[t2]) {       ans += dis2[u];       ss << t2 <<     << u <<     << u << endl;     }     for (long long i = 0; i < G[u].size(); i++) {       if (G[u][i] == fa) continue;       if (in[G[u][i]]) dfs4(G[u][i], u);     }   }   signed main() {     cin >> n;     for (long long i = 1; i <= n - 1; i++) {       long long u, v;       cin >> u >> v;       G[u].push_back(v);       G[v].push_back(u);       deg[u]++;       deg[v]++;     }     dfs1(1, 0, 0);     dfs2(t1, 0, 0);     dfs3(t2, 0, 0);     findpath(t1, 0, t2);     queue<long long> Q;     for (long long i = 1; i <= n; i++) {       if (deg[i] == 1) Q.push(i);     }     while (Q.size()) {       long long i = Q.front();       Q.pop();       if (in[i]) continue;       if (del[i]) continue;       del[i] = true;       if (dis1[i] > dis2[i]) {         ans += dis1[i];         ss << t1 <<     << i <<     << i << endl;       } else {         ans += dis2[i];         ss << t2 <<     << i <<     << i << endl;       }       for (long long j = 0; j < G[i].size(); j++) {         deg[G[i][j]]--;         if (deg[G[i][j]] == 1) Q.push(G[i][j]);       }     }     dfs4(t1, 0);     cout << ans << endl;     cout << ss.str() << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct segtree {     int size = 1;     vector<pair<int, int>> v;     vector<pair<int, int>> lazy;     void init(int n) {       while (n > size) {         size *= 2;       }       v.assign(size * 2, {0, -1});       lazy.assign(size * 2, {0, -1});     }     void prop(int x, int lx, int rx) {       if (rx - lx == 1) return;       if (lazy[x] == make_pair(0, -1)) return;       v[x * 2 + 1] = lazy[x];       v[x * 2 + 2] = lazy[x];       lazy[x * 2 + 1] = lazy[x];       lazy[x * 2 + 2] = lazy[x];       lazy[x] = {0, -1};     }     void update(int l, int r, pair<int, int> val, int x, int lx, int rx) {       prop(x, lx, rx);       if (rx <= l || lx >= r) {         return;       }       if (lx >= l && rx <= r) {         v[x] = val;         lazy[x] = val;         return;       }       int m = (lx + rx) / 2;       update(l, r, val, x * 2 + 1, lx, m);       update(l, r, val, x * 2 + 2, m, rx);       v[x] = max(v[x * 2 + 1], v[x * 2 + 2]);     }     void update(int l, int r, pair<int, int> val) {       update(l, r + 1, val, 0, 0, size);     }     pair<int, int> get(int l, int r, int x, int lx, int rx) {       prop(x, lx, rx);       if (rx <= l || lx >= r) {         return {0, -1};       }       if (lx >= l && rx <= r) {         return v[x];       }       int m = (lx + rx) / 2;       pair<int, int> m1 = get(l, r, x * 2 + 1, lx, m);       pair<int, int> m2 = get(l, r, x * 2 + 2, m, rx);       return max(m1, m2);     }     pair<int, int> get(int l, int r) { return get(l, r + 1, 0, 0, size); }   };   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, m;     cin >> n >> m;     set<int> s;     vector<vector<pair<int, int>>> v(n);     vector<tuple<int, int, int>> actions;     for (int i = 0; i < m; i++) {       int j, l, r;       cin >> j >> l >> r;       j--;       actions.push_back({j, l, r});       s.insert(l);       s.insert(r);       v[j].push_back(make_pair(l, r));     }     auto s1 = s.begin();     auto s2 = s.begin();     if (s2 != s.end()) s2++;     map<int, int> mapa;     int ctr = 0;     mapa[*s1] = ctr;     ctr++;     while (s2 != s.end()) {       if ((*s1) != (*s2)) {         mapa[*s2] = ctr;         ctr++;       }       s1++;       s2++;     }     segtree prev;     prev.init(2 * m + 2);     vector<int> before(n, -1);     for (int i = 0; i < n; i++) {       pair<int, int> mx = {0, -1};       for (auto x : v[i]) {         int a = mapa[x.first];         int b = mapa[x.second];         mx = max(mx, prev.get(a, b));       }       mx.first++;       before[i] = mx.second;       mx.second = i;       for (auto x : v[i]) {         int a = mapa[x.first];         int b = mapa[x.second];         prev.update(a, b, mx);       }     }     vector<int> ok(n, false);     int lastmx = prev.get(0, 2 * m + 1).second;     while (lastmx >= 0) {       ok[lastmx] = true;       lastmx = before[lastmx];     }     vector<int> res;     int resnum = 0;     for (int i = 0; i < n; i++) {       if (!ok[i]) {         res.push_back(i);         resnum++;       }     }     cout << resnum <<   n ;     for (auto w : res) cout << w + 1 <<    ;   }