#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int y, w; n   cin >> y >> w; n   int ans; n   ans; n   if (y >= w) { n     ans = 6 - y + 1; n   } else if (w >= y) { n     ans = 6 - w + 1; n   } n   if (ans == 6) { n     cout << 1 <<  /  << 1 << endl; n   } n   if (ans == 1) { n     cout << 1 <<  /  << 6 << endl; n   } n   if (ans == 2) { n     cout << 1 <<  /  << 3 << endl; n   } n   if (ans == 3) { n     cout << 1 <<  /  << 2 << endl; n   } n   if (ans == 4) { n     cout << 2 <<  /  << 3 << endl; n   } n   if (ans == 5) { n     cout << 5 <<  /  << 6 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a[111], ans = 1e9; n set<int> s[111]; n int main() { n   ios_base ::sync_with_stdio(false); n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n   } n   for (int i = 0, x, y; i < m; ++i) { n     cin >> x >> y; n     s[x].insert(y); n     s[y].insert(x); n   } n   for (int i = 1; i < n - 1; ++i) { n     for (int j = i + 1; j < n; ++j) { n       if (s[i].count(j)) { n         for (int k = j + 1; k <= n; ++k) { n           if (s[i].count(k) && s[j].count(k)) { n             ans = min(a[i] + a[j] + a[k], ans); n           } n         } n       } n     } n   } n   if (ans == 1e9) { n     cout << -1; n   } else { n     cout << ans; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int temp = 0; n   int n = 0; n   cin >> n; n   vector<int> city(n); n   for (int i = 0; i < n; ++i) { n     cin >> city[i]; n   } n   int max = 0; n   int min = 0; n   for (int j = 0; j < n; ++j) { n     if (j == n - 1) { n       min = abs(city[j] - city[j - 1]); n       max = abs(city[j] - city[0]); n     } else if (j == 0) { n       min = abs(city[0] - city[1]); n       max = abs(city[0] - city[n - 1]); n     } else { n       if (abs(city[j] - city[j + 1]) < abs(city[j] - city[j - 1])) n         min = abs(city[j] - city[j + 1]); n       else if (abs(city[j] - city[j + 1]) >= abs(city[j] - city[j - 1])) n         min = abs(city[j] - city[j - 1]); n       if (abs(city[j] - city[0]) < abs(city[j] - city[n - 1])) n         max = abs(city[j] - city[n - 1]); n       else if (abs(city[j] - city[0]) >= abs(city[j] - city[n - 1])) n         max = abs(city[j] - city[0]); n     } n     cout << min <<     << max << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T gcd(T a, T b) { n   return (b == 0) ? a : gcd(b, a % b); n } n template <typename T> n T lcm(T a, T b) { n   return a * (b / gcd(a, b)); n } n template <typename T> n T mod_exp(T b, T p, T m) { n   T x = 1; n   while (p) { n     if (p & 1) x = (x * b) % m; n     b = (b * b) % m; n     p = p >> 1; n   } n   return x; n } n template <typename T> n T invFermat(T a, T p) { n   return mod_exp(a, p - 2, p); n } n template <typename T> n T exp(T b, T p) { n   T x = 1; n   while (p) { n     if (p & 1) x = (x * b); n     b = (b * b); n     p = p >> 1; n   } n   return x; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   int a, b; n   cin >> a >> b; n   if (a == 0 && b == 0) { n     cout <<  NO  <<   n ; n   } else if (abs(a - b) <= 1) n     cout <<  YES  <<   n ; n   else n     cout <<  NO  <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int A[105][105]; n int main() { n   int n; n   int ans = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     string s; n     cin >> s; n     for (int j = 0; j < n; j++) { n       if (s[j] ==  C ) { n         A[i][j] = 1; n       } else { n         A[i][j] = 0; n       } n     } n   } n   for (int i = 0; i < n; i++) { n     int cnt = 0; n     for (int j = 0; j < n; j++) { n       if (A[i][j] == 1) { n         cnt++; n       } n     } n     ans += (cnt * (cnt - 1)) / 2; n   } n   for (int i = 0; i < n; i++) { n     int cnt = 0; n     for (int j = 0; j < n; j++) { n       if (A[j][i] == 1) { n         cnt++; n       } n     } n     ans += (cnt * (cnt - 1)) / 2; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n char name[110][3000], text[110][3000]; n int name_len[110], text_len[110]; n bool found[110][110], dp[110][110]; n int pre[110][110]; n int chosen[110]; n void variable_init() { n   memset(found, 0, sizeof found); n   memset(dp, 0, sizeof dp); n } n int main() { n   int tcase, n, m; n   scanf( %d , &tcase); n   while (tcase--) { n     variable_init(); n     scanf( %d , &n); n     for (int i = 1; i <= n; i++) { n       scanf( %s , name[i]); n       name_len[i] = strlen(name[i]); n     } n     scanf( %d , &m); n     gets(text[0]); n     for (int i = 1; i <= m; i++) { n       gets(text[i]); n       text_len[i] = strlen(text[i]); n       if (text[i][0] !=  ? ) { n         for (int k = 1; k <= n; k++) { n           bool ok = true; n           if (text_len[i] < name_len[k]) ok = false; n           for (int l = 0; l < name_len[k] && ok; l++) { n             if (text[i][l] != name[k][l]) ok = false; n           } n           ok &= n               (name_len[k] == text_len[i] || (!isalpha(text[i][name_len[k]]) && n                                               !isdigit(text[i][name_len[k]]))); n           if (ok) { n             bool found_others = false; n             int pre_choose; n             if (i == 1) { n               found_others = true; n               pre_choose = -1; n             } n             for (int j = 1; j <= n; j++) n               if (j != k && dp[i - 1][j]) { n                 found_others = true; n                 pre_choose = j; n                 break; n               } n             dp[i][k] = found_others; n             pre[i][k] = pre_choose; n             break; n           } n         } n         continue; n       } n       for (int j = 0; j < text_len[i]; j++) { n         if (!isalpha(text[i][j]) && !isdigit(text[i][j])) { n           for (int k = 1; k <= n; k++) { n             bool ok = true; n             if (text_len[i] - 1 - j < name_len[k]) ok = false; n             for (int l = j + 1; l <= j + name_len[k] && ok; l++) { n               if (text[i][l] != name[k][l - (j + 1)]) ok = false; n             } n             ok &= (j + name_len[k] + 1 == text_len[i] || n                    (!isalpha(text[i][j + name_len[k] + 1]) && n                     !isdigit(text[i][j + name_len[k] + 1]))); n             if (ok) found[i][k] = true; n           } n         } n       } n       for (int j = 1; j <= n; j++) { n         for (int k = 1; k <= n; k++) { n           if (i == 1) { n             if (!found[i][k]) { n               dp[i][k] = true; n               pre[i][k] = -1; n             } n           } else { n             if (j != k && dp[i - 1][j] && !found[i][k]) { n               dp[i][k] = true; n               pre[i][k] = j; n             } n           } n         } n       } n     } n     bool least_one = false; n     int last; n     for (int i = 1; i <= n; i++) { n       if (dp[m][i]) { n         least_one = true; n         last = i; n       } n     } n     if (!least_one) n       puts( Impossible ); n     else { n       for (int i = m; i >= 1; i--) { n         chosen[i] = last; n         last = pre[i][last]; n       } n       for (int i = 1; i <= m; i++) { n         if (text[i][0] ==  ? ) { n           printf( %s , name[chosen[i]]); n           int pos; n           for (int j = 1; j < text_len[i]; j++) n             if (text[i][j] ==  : ) { n               pos = j; n               break; n             } n           puts(text[i] + pos); n         } else { n           puts(text[i]); n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void readi(int &x) { n   int v = 0, f = 1; n   char c = getchar(); n   while (!isdigit(c) && c !=  - ) c = getchar(); n   if (c ==  - ) n     f = -1; n   else n     v = v * 10 + c -  0 ; n   while (isdigit(c = getchar())) v = v * 10 + c -  0 ; n   x = v * f; n } n void readll(long long &x) { n   long long v = 0ll, f = 1ll; n   char c = getchar(); n   while (!isdigit(c) && c !=  - ) c = getchar(); n   if (c ==  - ) n     f = -1; n   else n     v = v * 10 + c -  0 ; n   while (isdigit(c = getchar())) v = v * 10 + c -  0 ; n   x = v * f; n } n void readc(char &x) { n   char c; n   while ((c = getchar()) ==    ) n     ; n   x = c; n } n void writes(string s) { puts(s.c_str()); } n void writeln() { writes(  ); } n void writei(int x) { n   if (!x) putchar( 0 ); n   char a[25]; n   int top = 0; n   while (x) { n     a[++top] = (x % 10) +  0 ; n     x /= 10; n   } n   while (top) { n     putchar(a[top]); n     top--; n   } n } n void writell(long long x) { n   if (!x) putchar( 0 ); n   char a[25]; n   int top = 0; n   while (x) { n     a[++top] = (x % 10) +  0 ; n     x /= 10; n   } n   while (top) { n     putchar(a[top]); n     top--; n   } n } n inline long long inc(int &x) { return ++x; } n inline long long inc(long long &x) { return ++x; } n inline long long inc(int &x, long long y) { return x += y; } n inline long long inc(long long &x, long long y) { return x += y; } n inline double inc(double &x, double y) { return x += y; } n inline long long dec(int &x) { return --x; } n inline long long dec(long long &x) { return --x; } n inline long long dec(int &x, long long y) { return x -= y; } n inline long long dec(long long &x, long long y) { return x -= y; } n inline double dec(double &x, double y) { return x -= y; } n inline long long mul(int &x) { return x = ((long long)x) * x; } n inline long long mul(long long &x) { return x = x * x; } n inline long long mul(int &x, long long y) { return x *= y; } n inline long long mul(long long &x, long long y) { return x *= y; } n inline double mul(double &x, double y) { return x *= y; } n inline long long divi(int &x) { return x = sqrt(x); } n inline long long divi(long long &x) { return x = sqrt(x); } n inline long long divi(int &x, long long y) { return x /= y; } n inline long long divi(long long &x, long long y) { return x /= y; } n inline double divi(double &x, double y) { return x /= y; } n inline long long mod(int &x, long long y) { return x %= y; } n inline long long mod(long long &x, long long y) { return x %= y; } n long long n, m, i, j, cnt, cas; n long long ans; n vector<long long> all; n struct Scanning_line { n   long long l, r, h, ad; n } s[200005]; n struct seg { n   long long l, r, add; n   long long sum; n } tr[800005]; n void build(long long x, long long l, long long r) { n   if (l == r) { n     tr[x].l = l; n     tr[x].r = r; n     tr[x].add = tr[x].sum = 0; n     return; n   } n   tr[x].l = l; n   tr[x].r = r; n   tr[x].add = tr[x].sum = 0; n   build(x * 2, l, (l + r) / 2); n   build(x * 2 + 1, (l + r) / 2 + 1, r); n } n void pushup(long long x) { n   if (tr[x].add) n     tr[x].sum = all[tr[x].r] - all[tr[x].l - 1]; n   else if (tr[x].l == tr[x].r) n     tr[x].sum = 0; n   else n     tr[x].sum = tr[x * 2].sum + tr[x * 2 + 1].sum; n } n void update(long long x, long long l, long long r, long long c) { n   if (tr[x].l > r || tr[x].r < l) return; n   if (l <= tr[x].l && tr[x].r <= r) { n     tr[x].add += c; n     pushup(x); n     return; n   } n   update(x * 2, l, r, c); n   update(x * 2 + 1, l, r, c); n   pushup(x); n } n bool cmp(Scanning_line x, Scanning_line y) { return x.h < y.h; } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n; n   cnt = 0; n   for (i = 1; i <= n; i++) { n     long long w, x, y, z; n     cin >> w >> x >> y >> z; n     if (w > y) swap(w, y); n     if (x > z) swap(x, z); n     w--; n     x--; n     all.push_back(w); n     all.push_back(y); n     s[++cnt] = {w, y, x, 1}; n     s[++cnt] = {w, y, z, -1}; n   } n   stable_sort(all.begin(), all.end()); n   stable_sort(s + 1, s + cnt + 1, cmp); n   all.resize(unique(all.begin(), all.end()) - all.begin()); n   ans = 0; n   build(1, 0, all.size()); n   for (i = 1; i <= cnt; i++) { n     if (i != 1 && s[i].h != s[i - 1].h) { n       ans += abs(s[i].h - s[i - 1].h) * tr[1].sum; n     } n     long long l = upper_bound(all.begin(), all.end(), s[i].l) - all.begin(); n     long long r = upper_bound(all.begin(), all.end(), s[i].r) - all.begin(); n     update(1, l, r - 1, s[i].ad); n   } n   cout << ans << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int n, d; n string s, t; n map<pair<char, char>, vector<int> > M; n map<char, vector<int> > N; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> s >> t; n   d = 0; n   for (int i = 0; i < n; ++i) { n     if (s[i] != t[i]) { n       M[make_pair(s[i], t[i])].push_back(i + 1); n       N[s[i]].push_back(i + 1); n       ++d; n     } n   } n   for (int i = 0; i < n; ++i) { n     if (s[i] != t[i]) { n       map<pair<char, char>, vector<int> >::iterator it = n           M.find(make_pair(t[i], s[i])); n       if (it != M.end()) { n         cout << d - 2 << endl; n         cout << i + 1 <<     << it->second.back() << endl; n         return 0; n       } n     } n   } n   for (int i = 0; i < n; ++i) { n     if (s[i] != t[i]) { n       if (N.find(t[i]) != N.end()) { n         cout << d - 1 << endl; n         cout << i + 1 <<     << N[t[i]].back() << endl; n         return 0; n       } n     } n   } n   cout << d << endl; n   cout <<  -1 -1  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<int> s; n int ans[300005]; n int main() { n   int n, m, l, r, x; n   cin >> n >> m; n   for (int i = 1; i < n + 1; i++) { n     s.insert(i); n     ans[i] = 0; n   } n   for (int i = 0; i < m; i++) { n     cin >> l >> r >> x; n     set<int>::iterator lb = s.lower_bound(l), ub = s.upper_bound(r); n     for (set<int>::iterator it = lb; it != ub; it++) n       if (*it != x) ans[*it] = x; n     s.erase(lb, ub); n     s.insert(x); n   } n   for (int i = 1; i < n + 1; i++) cout << ans[i] <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e3 + 5; n char s[N]; n int n, f[N], mk[N], cnt[N]; n vector<int> p; n void init() { n   for (int i = 2; i < N; i++) n     if (!mk[i]) { n       p.push_back(i); n       for (int j = i + i; j < N; j += i) mk[j] = true; n     } n } n int main() { n   scanf( %s , s + 1); n   n = strlen(s + 1); n   init(); n   memset(mk, 0, sizeof(mk)); n   for (int i = 0; i < p.size(); i++) { n     if (p[i] * 2 > n) break; n     for (int j = 1; j * p[i] <= n; j++) mk[p[i] * j] = true; n   } n   int tot = 0, mx = 0, pos; n   for (int i = 1; i <= n; i++) { n     cnt[s[i] -  a ]++; n     if (mk[i]) tot++; n   } n   for (int i = 0; i < 26; i++) n     if (mx < cnt[i]) mx = cnt[i], pos = i; n   if (mx < tot) { n     cout <<  NO  <<   n ; n     return 0; n   } n   puts( YES ); n   for (int i = 1; i <= n; i++) { n     if (mk[i]) n       printf( %c ,  a  + pos), cnt[pos]--, tot--; n     else n       for (int j = 0; j < 26; j++) n         if (j == pos && cnt[j] > tot || (j != pos && cnt[j])) { n           printf( %c ,  a  + j); n           cnt[j]--; n           break; n         } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<long long int> > edge(100010); n long long int vis[100010]; n vector<long long int> color[2]; n bool dfs(long long int a, long long int c = 2) { n   vis[a] = c; n   color[c - 1].push_back(a); n   for (auto x : edge[a]) { n     if (!vis[x] && dfs(x, 3ll - c)) { n       return 1; n     } n     if (vis[x] != 3ll - c) { n       return 1; n     } n   } n   return 0; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int n, m, x, y; n   cin >> n >> m; n   for (long long int i = 1; i <= m; i++) { n     cin >> x >> y; n     edge[x].push_back(y); n     edge[y].push_back(x); n   } n   for (long long int i = 1; i <= n; i++) { n     if (!vis[i]) { n       if (edge[i].size() == 0) { n         continue; n       } n       if (dfs(i)) { n         cout << -1 << endl; n         return 0; n       } n     } n   } n   for (long long int i = 0; i < 2; i++) { n     cout << color[i].size() << endl; n     for (auto x : color[i]) { n       cout << x <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int p[110]; n int find(int v) { n   if (p[v] == v) return v; n   return p[v] = find(p[v]); n } n bool merge(int u, int v) { n   int x = find(u); n   int y = find(v); n   if (x != y) { n     p[x] = y; n     return true; n   } n   return false; n } n int main() { n   int n, m; n   cin >> n >> m; n   vector<pair<int, int> > edges; n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     edges.push_back({a, b}); n     p[a] = a; n     p[b] = b; n   } n   int flag = 0, count = 1; n   for (pair<int, int> e : edges) { n     int a = e.first, b = e.second; n     if (!merge(a, b)) { n       flag++; n       if (flag > 1) break; n     } else n       count++; n   } n   if (flag == 1 && count == n) n     cout <<  FHTAGN!  n          <<   n ; n   else n     cout <<  NO  n          <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char local =  O ; n template <typename T> n void read(T &p) { n   cin >> p; n } n template <typename T, typename T1> n void read(pair<T, T1> &p) { n   read(p.first); n   read(p.second); n } n template <typename T> n void read(T arr[], long long n) { n   for (long long i = 0; i < n; i++) { n     read(arr[i]); n   } n } n template <typename T> n void read(vector<T> &arr) { n   for (long long i = 0; i < arr.size(); i++) { n     read(arr[i]); n   } n } n template <typename T> n void out(T &n) { n   cout << n; n } n template <typename T, typename T1> n void out(pair<T, T1> &p) { n   cout <<  ( ; n   out(p.first); n   cout <<  , ; n   out(p.second); n   cout <<  ) ; n } n template <typename T> n void out(T arr[], long long n) { n   for (long long i = 0; i < n; i++) { n     out(arr[i]); n     cout <<    ; n   } n } n template <typename T> n void out(vector<T> &arr) { n   for (long long i = 0; i < arr.size(); i++) { n     out(arr[i]); n     cout <<    ; n   } n } n template <typename T> n void out(set<T> &arr) { n   for (auto i : arr) { n     out(i); n     cout <<    ; n   } n } n template <typename T> n void out(multiset<T> &arr) { n   for (auto i : arr) { n     out(i); n     cout <<    ; n   } n } n template <typename T, typename T1> n void out(map<T, T1> &m) { n   for (auto i : m) { n     out(i.first); n     cout <<   ->  ; n     out(i.second); n     cout <<  ,  ; n   } n } n template <typename Arg1> n void __f(const char *name, Arg1 &&arg1) { n   cout << name <<   :  ; n   out(arg1); n } n template <typename Arg1, typename... Args> n void __f(const char *names, Arg1 &&arg1, Args &&...args) { n   const char *comma = strchr(names + 1,  , ); n   cout.write(names, comma - names) <<   :  ; n   out(arg1); n   cout <<   |  ; n   __f(comma + 1, args...); n } n template <class T> n void debug(T n, string a) { n   cout << a <<  =  << n <<   n ; n } n void cal() {} n void solve() { n   int n, h; n   cin >> n >> h; n   for (int i = 1; i < n; i++) { n     cout << fixed << setprecision(15) << (sqrt(i / (n * 1.0)) * h) <<    ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t1; n   t1 = 1; n   cal(); n   while (t1--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 5; n bool match(string s1, string s2) { n   long long cnt = 0; n   for (long long i = 0; s1[i]; ++i) { n     if (s1[i] != s2[i]) ++cnt; n     if (cnt > 1) return 0; n   } n   return 1; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     long long n, m; n     cin >> n >> m; n     vector<string> v(n); n     for (long long i = 0; i < n; ++i) cin >> v[i]; n     bool ans = 0; n     string str; n     for (long long i = 0; i < m; ++i) { n       string s = v[0]; n       for (long long j = 1; j < n; ++j) { n         s[i] = v[j][i]; n         bool all_match = 1; n         for (long long k = 1; k < n; ++k) { n           if (!match(s, v[k])) { n             all_match = 0; n             break; n           } n         } n         if (all_match) { n           ans = 1; n           str = s; n           break; n         } n       } n       if (ans) break; n     } n     if (ans) n       cout << str <<   n ; n     else { n       bool all_match = 1; n       for (long long i = 1; i < n; ++i) { n         if (!match(v[0], v[i])) { n           all_match = 0; n           break; n         } n       } n       if (all_match) n         cout << v[0] <<   n ; n       else n         cout << -1 <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long min(long long a, long long b) { n   if (a < b) n     return a; n   else n     return b; n } n static long long gcd(long long a, long long b) { n   long long r; n   while (b != 0) { n     r = a % b; n     a = b; n     b = r; n   } n   return a; n } n long long lcm(long long a, long long b) { n   long long temp = gcd(a, b); n   return temp ? (a / temp * b) : 0; n } n unsigned long long PowMod(unsigned long long x, unsigned long long e, n                           unsigned long long mod) { n   unsigned long long res; n   if (e == 0) { n     res = 1; n   } else if (e == 1) { n     res = x; n   } else { n     res = PowMod(x, e / 2, mod); n     res = res * res % mod; n     if (e % 2) res = res * x % mod; n   } n   return res; n } n int solve() { n   int n, m; n   cin >> n >> m; n   long long a[n], b[m]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int j = 0; j < m; j++) { n     cin >> b[j]; n   } n   vector<pair<long long, int> > v; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       v.push_back(make_pair(a[i] * b[j], i)); n     } n   } n   sort((v).begin(), (v).end()); n   int I = v[(n * m) - 1].second; n   long long ans = (1000000000LL) * (1000000000LL) * (-1LL); n   ans--; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (i != I) { n         ans = max(ans, a[i] * b[j]); n       } n     } n   } n   cout << ans; n   return 0; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   for (int i = 1; i <= t; i++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int static_init = []() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout << fixed; n   return 0; n }(); n constexpr int N = 3e5; n int32_t n, x[2], c[N + 1], p[N + 1]; n int64_t t[N + 2]; n bool run(bool sw) { n   for (int k1 = 1; k1 <= n; k1++) { n     int i1 = n         lower_bound(p + 1, p + n + 2 - k1, x[sw], n                     [k1](int i, int x) { return (int64_t)c[i] * k1 < x; }) - n         p; n     int j = i1 + k1; n     if (j <= n && t[j] >= x[!sw]) { n       int i2; n       for (i2 = n; i2 >= j; i2--) { n         if (t[i2] == t[j]) break; n       } n       int k2 = n - i2 + 1; n       if (sw) swap(k1, k2), swap(i1, i2); n       cout <<  Yes n  << k1 <<     << k2 << endl; n       for (int i = i1; i < i1 + k1; i++) n         cout << p[i] << (i == i1 + k1 - 1 ?   n  :    ); n       for (int i = i2; i < i2 + k2; i++) n         cout << p[i] << (i == i2 + k2 - 1 ?   n  :    ); n       return true; n     } n   } n   return false; n } n int main() { n   cin >> n >> x[0] >> x[1]; n   for (int i = 1; i <= n; i++) cin >> c[i]; n   iota(p + 1, p + n + 1, 1); n   sort(p + 1, p + n + 1, [](int i, int j) { return c[i] < c[j]; }); n   for (int i = n; i >= 1; i--) { n     t[i] = max(t[i + 1], (int64_t)c[p[i]] * (n - i + 1)); n   } n   if (!run(false) && !run(true)) cout <<  No  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e9 + 7; n const int N = 1e3 + 10; n const int M = 1e4; n int a[N][N]; n int main() { n   int n, k, d; n   cin >> n >> k >> d; n   int flag = 1; n   memset((a), (0), sizeof(a)); n   if (n == 1) { n     while (d--) cout << 1 << endl; n     return 0; n   } n   for (int i = 1; i <= d; i++) a[1][i] = 1; n   for (int i = 2; i <= n; i++) { n     for (int j = 1; j <= d; j++) a[i][j] = a[i - 1][j]; n     a[i][d]++; n     for (int j = d; j > 0; j--) { n       if (a[i][j] > k) n         a[i][j] = 1, a[i][j - 1]++; n       else n         break; n     } n     if (a[i][0] == 1) { n       flag = 0; n       break; n     } n   } n   if (!flag) { n     cout << -1 << endl; n     return 0; n   } n   for (int j = 1; j <= d; j++) { n     for (int i = 1; i <= n; i++) { n       if (i > 1) cout <<    ; n       cout << a[i][j]; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = (int)3e3 + 5; n const long long INF = (long long)1e15; n pair<int, long long> dp[MAXN][MAXN]; n vector<int> adj[MAXN]; n int arr[MAXN]; n int sub[MAXN]; n int n, m; n void dfs(int v, int pr = -1) { n   fill(dp[v], dp[v] + n + 1, make_pair(-n, -INF)); n   dp[v][0] = make_pair(0, 0); n   sub[v] = 1; n   for (int to : adj[v]) { n     if (to == pr) { n       continue; n     } n     dfs(to, v); n   } n   vector<pair<int, long long> > ndp(n + 5, make_pair(-n, -INF)); n   for (int to : adj[v]) { n     if (to == pr) { n       continue; n     } n     for (int i = 0; i <= sub[v] + sub[to] + 3; ++i) { n       ndp[i] = make_pair(-n, -INF); n     } n     for (int i = sub[v]; i >= 0; --i) { n       for (int j = sub[to]; j >= 0; --j) { n         if (dp[v][i].second == -INF || dp[to][j].second == -INF || n             dp[v][i].first < 0 || dp[to][j].first < 0) { n           continue; n         } n         if (dp[to][j].second > 0) { n           ndp[i + j + 1] = max( n               ndp[i + j + 1], n               make_pair(dp[v][i].first + dp[to][j].first + 1, dp[v][i].second)); n         } else { n           ndp[i + j + 1] = n               max(ndp[i + j + 1], n                   make_pair(dp[v][i].first + dp[to][j].first, dp[v][i].second)); n         } n         ndp[i + j] = n             max(ndp[i + j], make_pair(dp[v][i].first + dp[to][j].first, n                                       dp[v][i].second + dp[to][j].second)); n       } n     } n     for (int j = 0; j < n; ++j) { n       dp[v][j] = ndp[j]; n     } n     sub[v] += sub[to]; n   } n   for (int i = 0; i < n; ++i) { n     if (dp[v][i].second != -INF) { n       dp[v][i].second += arr[v]; n     } n   } n } n void solve() { n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; ++i) { n     adj[i].clear(); n     arr[i] = 0; n   } n   for (int i = 1, x; i <= n; ++i) { n     scanf( %d , &x); n     arr[i] = -x; n   } n   for (int i = 1, x; i <= n; ++i) { n     scanf( %d , &x); n     arr[i] += x; n   } n   for (int i = 1; i < n; ++i) { n     int u, v; n     scanf( %d %d , &u, &v); n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   dfs(1); n   printf( %d n , dp[1][m - 1].first + (dp[1][m - 1].second > 0)); n } n int main() { n   int tt; n   scanf( %d , &tt); n   while (tt--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int main() { n   scanf( %d , &n); n   if (n & 1) n     printf( %d n , (n - 1) / 2); n   else { n     int w = 1; n     while (w <= n) w *= 2; n     w /= 2; n     printf( %d n , (n - w) / 2); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int solve(vector<int> a, int bit) { n   if (bit == 0) { n     return a.size(); n   } n   if (a.size() <= 1) { n     return a.size(); n   } n   vector<int> v0, v1; n   for (int x : a) { n     if (1 & (x >> bit)) { n       v0.push_back(x); n     } else { n       v1.push_back(x); n     } n   } n   return max(solve(v0, bit - 1) + (v1.size() ? 1 : 0), n              solve(v1, bit - 1) + (v0.size() ? 1 : 0)); n } n int main() { n   int n; n   cin >> n; n   vector<int> arr(n); n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   int ans = solve(arr, 30); n   cout << n - ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T gcd(T x, T y) { n   if (x < y) swap(x, y); n   while (y > 0) { n     T f = x % y; n     x = y; n     y = f; n   } n   return x; n } n const int maxn = 100007; n int BLOCKSIZE = 350; n set<int> s[maxn]; n int a[maxn]; n int nxt[maxn]; n int n, m; n long long b[maxn]; n int d[maxn]; n long long query(int block, int R) { n   int iBlockStart = block * BLOCKSIZE; n   int iBlockEnd = min((block + 1) * BLOCKSIZE, n); n   int idx = upper_bound(d + iBlockStart, d + iBlockEnd, R, n                         [](int i, int j) { return i < nxt[j]; }) - n             d; n   if (idx == iBlockStart) return 0; n   return b[idx - 1]; n } n void updateBlock(int block) { n   int iBlockStart = block * BLOCKSIZE; n   int iBlockEnd = min((block + 1) * BLOCKSIZE, n); n   sort(d + iBlockStart, d + iBlockEnd, n        [](int i, int j) { return nxt[i] < nxt[j]; }); n   b[iBlockStart] = nxt[d[iBlockStart]] - d[iBlockStart]; n   for (int i = iBlockStart + 1; i < iBlockEnd; i++) n     b[i] = b[i - 1] + nxt[d[i]] - d[i]; n } n int main(int argc, char *argv[]) { n   std::cin.sync_with_stdio(false); n   std::cin.tie(nullptr); n   cin >> n >> m; n   { n     BLOCKSIZE = 1; n     while (BLOCKSIZE * BLOCKSIZE < n) BLOCKSIZE++; n     BLOCKSIZE = max(BLOCKSIZE, 10); n     for (int i = 0; i < n; i++) nxt[i] = n + 1, d[i] = i; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n       if (!s[a[i]].empty()) nxt[*s[a[i]].rbegin()] = i; n       s[a[i]].insert(i); n     } n     for (int i = 0; i * BLOCKSIZE < n; i++) updateBlock(i); n     for (int i = 0; i < m; i++) { n       int op; n       cin >> op; n       if (op == 1) { n         int p, x; n         cin >> p >> x; n         p--; n         if (a[p] == x) continue; n         { n           auto it = s[a[p]].find(p); n           if (it != s[a[p]].begin()) { n             int pr = *prev(it); n             auto nxt_it = it == s[a[p]].end() ? it : next(it); n             nxt[pr] = nxt_it == s[a[p]].end() ? (n + 1) : *nxt_it; n             updateBlock(pr / BLOCKSIZE); n           } n         } n         s[a[p]].erase(p); n         a[p] = x; n         nxt[p] = n + 1; n         if (!s[x].empty()) { n           auto it = s[x].upper_bound(p); n           if (it != s[x].begin()) { n             int pr = *prev(it); n             nxt[pr] = p; n             updateBlock(pr / BLOCKSIZE); n           } n           if (it != s[x].end()) { n             nxt[p] = (it == s[x].end()) ? (n + 1) : *it; n           } n         } n         s[a[p]].insert(p); n         updateBlock(p / BLOCKSIZE); n       } else { n         int l, r, R; n         cin >> l >> r; n         l--; n         r--; n         R = r; n         long long res = 0; n         for (; l <= r && l % BLOCKSIZE; l++) n           if (nxt[l] <= R) res += nxt[l] - l; n         for (; l <= r && (r + 1) % BLOCKSIZE; r--) n           if (nxt[r] <= R) res += nxt[r] - r; n         if (l <= r) { n           for (int i = l / BLOCKSIZE, j = r / BLOCKSIZE; i <= j; i++) { n             res += query(i, R); n           } n         } n         cout << res << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool comp(pair<long long int, long long int> &a, n           pair<long long int, long long int> &b) { n   if (a.first == b.first) return a.second < b.second; n   return a.first > b.first; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int n, m, d; n   cin >> n >> m >> d; n   pair<long long int, long long int> py[n], in[m]; n   for (int i = 0; i < n; i++) cin >> py[i].first >> py[i].second; n   for (int i = 0; i < m; i++) cin >> in[i].first >> in[i].second; n   sort(py, py + n, comp); n   sort(in, in + m, comp); n   int p1 = 0, p2 = 1; n   long long int sm = py[0].first + in[0].first; n   long long int w = py[0].second + in[0].second; n   if (w > d) { n     cout <<  0 n ; n     return 0; n   } n   long long int ans = sm; n   while (p2 < m) { n     sm += in[p2].first; n     w += in[p2].second; n     if (w <= d) ans = sm; n     p2++; n   } n   p2 = m - 1; n   while (p1 != n - 1) { n     p1++; n     sm += py[p1].first; n     w += py[p1].second; n     while (w > d and p2 > 0) { n       sm -= in[p2].first; n       w -= in[p2].second; n       p2--; n     } n     if (w <= d) ans = max(ans, sm); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, h, n, i; n long long x, ans; n pair<pair<int, int>, int> p[100005]; n map<long long, long long> f; n void update(int index, long long value) { n   while (index < 1e9) { n     f[index] = max(f[index], value); n     index += (-index & index); n   } n } n long long read(int index) { n   long long ans = 0; n   while (index > 0) { n     ans = max(ans, f[index]); n     index -= (-index & index); n   } n   return ans; n } n int main() { n   scanf( %d , &n); n   for (i = 0; i < n; i++) n     scanf( %d%d%d , &p[i].first.second, &p[i].first.first, &p[i].second); n   sort(p, p + n); n   reverse(p, p + n); n   for (i = 0; i < n; i++) { n     x = read(p[i].first.first) + p[i].second; n     update(p[i].first.second + 1, x); n     ans = max(ans, x); n   } n   printf( %lld , ans); n } n


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


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n using ll = long long; n using ld = long double; n using pii = pair<int, int>; n using vi = vector<int>; n const ld EPS = 1E-9; n struct pt { n   ld x, y; n }; n struct seg { n   pt p, q; n   int id; n   ld get_y(ld x) const { n     if (abs(p.x - q.x) < EPS) return p.y; n     return p.y + (q.y - p.y) * (x - p.x) / (q.x - p.x); n   } n }; n inline bool intersect1d(ld l1, ld r1, ld l2, ld r2) { n   if (l1 > r1) swap(l1, r1); n   if (l2 > r2) swap(l2, r2); n   return max(l1, l2) <= min(r1, r2) + EPS; n } n inline int vec(const pt& a, const pt& b, const pt& c) { n   ld s = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); n   return abs(s) < EPS ? 0 : s > 0 ? +1 : -1; n } n bool intersect(const seg& a, const seg& b) { n   return intersect1d(a.p.x, a.q.x, b.p.x, b.q.x) && n          intersect1d(a.p.y, a.q.y, b.p.y, b.q.y) && n          vec(a.p, a.q, b.p) * vec(a.p, a.q, b.q) <= 0 && n          vec(b.p, b.q, a.p) * vec(b.p, b.q, a.q) <= 0; n } n bool operator<(const seg& a, const seg& b) { n   ld x = max(min(a.p.x, a.q.x), min(b.p.x, b.q.x)); n   return a.get_y(x) < b.get_y(x) - EPS; n } n struct event { n   ld x; n   int tp, id; n   event() {} n   event(ld x, int tp, int id) : x(x), tp(tp), id(id) {} n   bool operator<(const event& e) const { n     if (abs(x - e.x) > EPS) return x < e.x; n     return tp > e.tp; n   } n }; n set<seg> s; n vector<set<seg>::iterator> where; n inline set<seg>::iterator prev(set<seg>::iterator it) { n   return it == s.begin() ? s.end() : --it; n } n inline set<seg>::iterator next(set<seg>::iterator it) { return ++it; } n pair<int, int> solve(const vector<seg>& a) { n   int n = (int)a.size(); n   vector<event> e; n   for (int i = 0; i < n; ++i) { n     e.push_back(event(min(a[i].p.x, a[i].q.x), +1, i)); n     e.push_back(event(max(a[i].p.x, a[i].q.x), -1, i)); n   } n   sort(e.begin(), e.end()); n   s.clear(); n   where.resize(a.size()); n   for (size_t i = 0; i < e.size(); ++i) { n     int id = e[i].id; n     if (e[i].tp == +1) { n       set<seg>::iterator nxt = s.lower_bound(a[id]), prv = prev(nxt); n       if (nxt != s.end() && intersect(*nxt, a[id])) n         return make_pair(nxt->id, id); n       if (prv != s.end() && intersect(*prv, a[id])) n         return make_pair(prv->id, id); n       where[id] = s.insert(nxt, a[id]); n     } else { n       set<seg>::iterator nxt = next(where[id]), prv = prev(where[id]); n       if (nxt != s.end() && prv != s.end() && intersect(*nxt, *prv)) n         return make_pair(prv->id, nxt->id); n       s.erase(where[id]); n     } n   } n   return make_pair(-1, -1); n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0); n   int n; n   cin >> n; n   vector<pt> S(n), V(n); n   vi U(n); n   for (int i = 0; i < n; i++) { n     cin >> S[i].x >> S[i].y >> V[i].x >> V[i].y >> U[i]; n     ld len = sqrt(V[i].x * V[i].x + V[i].y * V[i].y); n     V[i].x /= len; n     V[i].y /= len; n     V[i].x *= U[i]; n     V[i].y *= U[i]; n   } n   ld lf = 0, rg = 1e12; n   { n     ld md = rg; n     vector<seg> a(n); n     for (int i = 0; i < n; i++) { n       a[i].id = i; n       a[i].p = S[i]; n       ld X = S[i].x + V[i].x * md; n       ld Y = S[i].y + V[i].y * md; n       a[i].q = {X, Y}; n     } n     if (solve(a) == make_pair(-1, -1)) { n       cout <<  No show :( ; n       return 0; n     } n   } n   const ld _eps = 1e-8; n   while ((rg - lf) > _eps) { n     ld md = (lf + rg) / 2; n     vector<seg> a(n); n     for (int i = 0; i < n; i++) { n       a[i].id = i; n       a[i].p = S[i]; n       ld X = S[i].x + V[i].x * md; n       ld Y = S[i].y + V[i].y * md; n       a[i].q = {X, Y}; n     } n     if (solve(a) != make_pair(-1, -1)) { n       rg = md; n     } else { n       lf = md; n     } n   } n   cout.precision(10); n   cout << fixed << (lf + rg) / 2; n } n


#include <bits/stdc++.h> n using namespace std; n char str[200005]; n int main() { n   int ts; n   scanf( %d , &ts); n   while (ts--) { n     int n; n     scanf( %d %s , &n, str); n     vector<int> vec; n     for (int i = 0; i < n; i++) { n       int j = i, c = 0; n       while (i < n && str[i] == str[j]) i++, c++; n       i--; n       vec.push_back(c); n     } n     if (vec.size() == 1 && vec[0] > 2) vec[0] += 2; n     if (vec.size() != 1 && str[0] == str[n - 1]) n       vec[0] += vec.back(), vec.pop_back(); n     int res = 0; n     for (int i = 0; i < vec.size(); i++) res += vec[i] / 3; n     printf( %d n , res); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); } n long long lcm(long long a, long long b) { return a * b / gcd(a, b); } n int main() { n   int n; n   long long k; n   scanf( %d%I64d , &n, &k); n   long long ans = 1; n   bool ok = false; n   for (int i = 0; i < n; ++i) { n     long long x; n     scanf( %I64d , &x); n     if (ans) ans = lcm(ans, x) % k; n     if (ans == 0) ok = true; n   } n   if (ok) n     printf( Yes n ); n   else n     printf( No n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mr[] = {0, 1, 0, -1}; n int mc[] = {1, 0, -1, 0}; n char a[52][52]; n int id[52][52]; n int ar[2500], br[2500], ac[2500], bc[2500]; n void flood(int r, int c, char color, int tag) { n   ar[tag] = min(ar[tag], r); n   br[tag] = max(br[tag], r); n   ac[tag] = min(ac[tag], c); n   bc[tag] = max(bc[tag], c); n   id[r][c] = tag; n   for (int o = 0; o < 4; o++) { n     int dr = r + mr[o]; n     int dc = c + mc[o]; n     if (a[dr][dc] == color && id[dr][dc] < 0) flood(dr, dc, color, tag); n   } n } n int main() { n   int n, m, s, cnt = 0; n   scanf( %d%d , &n, &s); n   for (int i = 1; i <= n; i++) scanf( %s , a[i] + 1); n   m = strlen(a[1] + 1); n   memset(id, 255, sizeof(id)); n   memset(ar, 127, sizeof(ar)); n   memset(br, 128, sizeof(br)); n   memset(ac, 127, sizeof(ac)); n   memset(bc, 128, sizeof(bc)); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (id[i][j] < 0) flood(i, j, a[i][j], cnt++); n   int r = 1, c = 1, dp = 0, cp = 0; n   while (s--) { n     int tr = r, tc = c, tag = id[r][c]; n     if (dp == 0 && cp == 0) tr = ar[tag], tc = bc[tag] + 1; n     if (dp == 0 && cp == 1) tr = br[tag], tc = bc[tag] + 1; n     if (dp == 1 && cp == 0) tr = br[tag] + 1, tc = bc[tag]; n     if (dp == 1 && cp == 1) tr = br[tag] + 1, tc = ac[tag]; n     if (dp == 2 && cp == 0) tr = br[tag], tc = ac[tag] - 1; n     if (dp == 2 && cp == 1) tr = ar[tag], tc = ac[tag] - 1; n     if (dp == 3 && cp == 0) tr = ar[tag] - 1, tc = ac[tag]; n     if (dp == 3 && cp == 1) tr = ar[tag] - 1, tc = bc[tag]; n     if (!a[tr][tc] || a[tr][tc] ==  0 ) { n       cp ^= 1; n       if (!cp) dp = (dp + 1) & 3; n     } else { n       r = tr; n       c = tc; n     } n   } n   printf( %c n , a[r][c]); n } n


#include <bits/stdc++.h> n using namespace std; n int hx[] = {-2, -2, -1, -1, 1, 1, 2, 2}; n int hy[] = {-1, 1, -2, 2, -2, 2, -1, 1}; n int dx[] = {1, -1, 0, 0}; n int dy[] = {0, 0, 1, -1}; n int nx[] = {1, -1, 0, 0, -1, 1, 1, -1}; n int ny[] = {0, 0, 1, -1, 1, -1, 1, -1}; n class pt { n  public: n   int x, y; n }; n int main() { n   int t = 1; n   while (t--) { n     long long n, m; n     cin >> n >> m; n     long long ans = 1, tmp = 3; n     for (int i = 0; i < 32; i++) { n       if (n & 1 << i) ans = (ans * tmp) % m; n       tmp = (tmp * tmp) % m; n     } n     if (ans == 0) ans = m; n     cout << ans - 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, K, T, to[25]; n long double prob[25], dp[2000100], val[25], ans[25]; n bool v[2000100]; n long double calc(int mask) { n   if (v[mask]) return dp[mask]; n   v[mask] = 1; n   int i; n   long double tot = 0; n   for (i = 0; i < N; i++) n     if (mask & (1 << i)) tot += prob[i]; n   for (i = 0; i < N; i++) n     if (mask & (1 << i)) { n       tot -= prob[i]; n       dp[mask] = dp[mask] + (prob[i] * calc(mask - (1 << i))) / (1 - tot); n       tot += prob[i]; n     } n   return dp[mask]; n } n void get_masks(int mask, int cnt, int p) { n   if (p == N || cnt == K) { n     if (cnt != K) return; n     calc(mask); n     int i; n     for (i = 0; i < N; i++) n       if (mask & (1 << i)) val[i] += dp[mask]; n     return; n   } n   get_masks(mask, cnt, p + 1); n   get_masks(mask + (1 << p), cnt + 1, p + 1); n } n int main() { n   cin >> N >> K; n   int i; n   for (i = 0; i < N; i++) { n     cin >> prob[i]; n     if (prob[i] != 0) { n       T++; n       prob[T - 1] = prob[i]; n       to[T - 1] = i; n     } n   } n   swap(T, N); n   if (K > N) K = N; n   dp[0] = 1, v[0] = 1; n   get_masks(0, 0, 0); n   for (i = 0; i < N; i++) ans[to[i]] = val[i]; n   for (i = 0; i < T; i++) cout << fixed << setprecision(9) << ans[i] <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> graph[200010]; n long long v[200010], new_visit[200010], MAX_node, mx, cnt; n void dfs(long long node) { n   v[node] = 1; n   if ((long long)graph[node].size() > mx) { n     mx = (long long)graph[node].size(); n     MAX_node = node; n   } n   for (auto &x : graph[node]) n     if (!v[x]) dfs(x); n } n void bfs(long long node) { n   queue<long long> q; n   q.push(node); n   while (!q.empty()) { n     long long frt = q.front(); n     new_visit[frt] = 1; n     q.pop(); n     for (auto &x : graph[frt]) { n       if (!new_visit[x]) { n         new_visit[x] = 1; n         cout << frt <<     << x << endl; n         q.push(x); n         cnt--; n         if (cnt == 0) return; n       } n     } n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, m; n   cin >> n >> m; n   cnt = n - 1; n   for (long long i = 0; i < m; i++) { n     long long x, y; n     cin >> x >> y; n     graph[x].push_back(y); n     graph[y].push_back(x); n   } n   dfs(1); n   bfs(MAX_node); n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> p; n vector<pair<int, int> > v, ans; n bool cmp(pair<int, int> a, pair<int, int> b) { n   if (a.first == b.first) n     return a.second > b.second; n   else n     return a.first < b.first; n } n int main() { n   int n; n   cin >> n; n   int a, b; n   for (int i = 0; i < n; i++) { n     cin >> a; n     p.push_back(a); n   } n   for (int i = 0; i < n; i++) { n     cin >> b; n     v.push_back({b, i}); n   } n   sort(p.begin(), p.end()); n   sort(v.begin(), v.end(), cmp); n   for (int i = 0; i < n; i++) { n     ans.push_back({v[i].second, p[n - i - 1]}); n   } n   sort(ans.begin(), ans.end()); n   for (int i = 0; i < n; i++) cout << ans[i].second <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000007; n const long long INF64 = 1000000000000000007; n int n; n int p[17]{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59}; n int a[105]; n int number[60]; n int dp[105][1 << 17]; n int pred[105][1 << 17]; n void find_pred(int x, int p) { n   if (x == n) return; n   cout << pred[x][p] <<    ; n   find_pred(x + 1, p | number[pred[x][p]]); n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 2; i < 60; i++) { n     for (int j = 0; j < 17; j++) { n       if (i % p[j] == 0) { n         number[i] |= (1 << j); n       } n     } n   } n   for (int i = n - 1; i >= 0; i--) { n     for (int j = 0; j < (1 << 17); j++) { n       dp[i][j] = INF; n       for (int k = 1; k < 60; k++) { n         if (!(j & number[k])) { n           int w = dp[i + 1][j | number[k]] + abs(k - a[i]); n           if (dp[i][j] > w) { n             dp[i][j] = w; n             pred[i][j] = k; n           } n         } n       } n     } n   } n   find_pred(0, 0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, budget; n long long price[100005], perMoney[100005]; n bool can(int k) { n   long long curBudget = 0; n   int j = 0; n   for (int i = k - 1; i >= 0; i--, j++) { n     curBudget += max(0ll, price[j] - perMoney[i]); n   } n   return curBudget <= budget; n } n int main() { n   cin >> n >> m >> budget; n   for (int i = 0; i < n; i++) cin >> perMoney[i]; n   for (int i = 0; i < m; i++) cin >> price[i]; n   sort(price, price + m); n   sort(perMoney, perMoney + n); n   reverse(perMoney, perMoney + n); n   long long lo = 0, hi = min(m, n); n   while (lo < hi) { n     long long mid = (lo + hi + 1) / 2; n     if (can(mid)) n       lo = mid; n     else n       hi = mid - 1; n   } n   long long spent = 0; n   for (int i = 0; i < lo; i++) { n     spent += price[i]; n   } n   cout << lo <<     << max(0ll, spent - budget) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n struct Node { n   int loc; n   int s; n   int v; n   Node(int ll, int ss, int vv) : loc(ll), s(ss), v(vv) {} n   bool operator<(const Node& other) const { return v > other.v; } n }; n vector<pair<int, int> > adj[32]; n int main() { n   priority_queue<Node> que; n   int N, K, T; n   scanf( %d%d%d , &N, &K, &T); n   while (K--) { n     int h, w, r; n     scanf( %d%d%d , &h, &w, &r); n     h--; n     w--; n     adj[h].push_back(pair<int, int>(w, r)); n   } n   que.push(Node(0, 0, 0)); n   int now = 0; n   while (now + 1 < T) { n     Node p = que.top(); n     que.pop(); n     now++; n     for (int h = p.loc; h < N; h++) { n       for (int i = 0; i < adj[h].size(); i++) { n         pair<int, int> w = adj[h][i]; n         if (!(p.s & (1 << w.first))) n           que.push(Node(h + 1, p.s | (1 << w.first), p.v + w.second)); n       } n     } n   } n   printf( %d n , que.top().v); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long t, n, i, j, k, b[200005], a[200005], x, m, l, r, mid, ans, s; n vector<long long> u, v; n int main() { n   ios::sync_with_stdio(NULL); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> x; n     if (x % 2) n       v.push_back(x); n     else n       u.push_back(x); n     s += x; n   } n   sort(u.begin(), u.end(), greater<long long>()); n   sort(v.begin(), v.end(), greater<long long>()); n   n = v.size(); n   m = u.size(); n   n = min(n, m); n   for (i = 0; i < n; i++) s -= u[i] + v[i]; n   if (u.size() > v.size()) s -= u[i]; n   if (u.size() < v.size()) s -= v[i]; n   cout << s; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int visit[111111]; n vector<int> v[111111], mark[111111]; n int dfs(int t) { n   vector<int> v1; n   int i; n   for (i = 0; i < v[t].size(); i++) { n     int u = v[t][i]; n     if (visit[mark[t][i]] == 0) { n       visit[mark[t][i]] = 1; n       int w = dfs(u); n       if (w == 0) n         v1.push_back(u); n       else n         printf( %d %d %d n , t, u, w); n     } n   } n   while (v1.size() > 1) { n     printf( %d %d %d n , v1[v1.size() - 1], t, v1[v1.size() - 2]); n     v1.pop_back(); n     v1.pop_back(); n   } n   if (v1.size() == 1) n     return v1[0]; n   else n     return 0; n } n int main() { n   memset(visit, 0, sizeof(visit)); n   scanf( %d%d , &n, &m); n   int i; n   for (i = 0; i < m; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     v[a].push_back(b); n     v[b].push_back(a); n     mark[a].push_back(i); n     mark[b].push_back(i); n   } n   if (m % 2) n     printf( No solution n ); n   else { n     for (i = 1; i <= n; i++) dfs(i); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   vector<int> vi1, vi2; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     vi1.push_back(x); n   } n   sort(vi1.begin(), vi1.end()); n   vi1.push_back(1e9); n   for (int i = 0; i < m; i++) { n     int a, b, c; n     cin >> a >> b >> c; n     if (a == 1) vi2.push_back(b); n   } n   sort(vi2.begin(), vi2.end()); n   int j = 0, mini = vi2.size(); n   for (int i = 0; i <= n; i++) { n     int hi = vi1[i]; n     while (j < vi2.size() && vi2[j] < hi) { n       j++; n     } n     int asdf = vi2.size(); n     mini = min(mini, i + asdf - j); n   } n   cout << mini; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int main() { n   int t, h, g; n   cin >> t; n   while (t--) { n     scanf( %d %d , &h, &g); n     int n = (1 << h) - 1, m = (1 << g) - 1; n     std::vector<pair<int, int> > a(1 << h); n     for (int i = 0; i < n; i++) { n       scanf( %d , &a[i].first); n       a[i].second = i + 1; n     } n     sort(a.begin(), a.end()); n     std::vector<int> notFree(n + 1, 0), taken(n + 1, 0); n     ll ans = 0LL; n     for (int i = 0; i < n; i++) { n       int where = a[i].second; n       while (where > m or notFree[where]) where = where >> 1; n       if (!where) continue; n       if (notFree[where << 1] == notFree[where << 1 | 1]) { n         notFree[where] = 1; n         ans += a[i].first; n         taken[a[i].second] = 1; n       } n     } n     printf( %lld n , ans); n     for (int i = n; i >= 1; i--) { n       if (!taken[i]) printf( %d  , i); n       ; n     } n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n, cnt = 0; n   cin >> n; n   vector<vector<int>> adj(n); n   vector<int> deg(n); n   for (int i = 0; i < n - 1; i++) { n     int x, y; n     cin >> x >> y; n     x--, y--; n     adj[x].push_back(y); n     adj[y].push_back(x); n     deg[x]++, deg[y]++; n   } n   for (int i = 0; i < n; i++) n     if (deg[i] == 1) n       cnt++; n     else if (deg[i] == 2) { n       cnt = 0; n       break; n     } n   if ((long long)cnt * (cnt - 1) >= 2 * (n - 1)) n     cout <<  YES  <<   n ; n   else n     cout <<  NO  <<   n ; n } n int main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n struct node { n   int x, l; n } a[200005]; n int skip[200005][25]; n int mx[200005]; n long long f[200005][25]; n inline int read() { n   register int x = 0, f = 1; n   register char s = getchar(); n   while (s >  9  || s <  0 ) { n     if (s ==  - ) f = -1; n     s = getchar(); n   } n   while (s >=  0  && s <=  9 ) { n     x = x * 10 + s -  0 ; n     s = getchar(); n   } n   return x * f; n } n inline int max(const int &x, const int &y) { return x > y ? x : y; } n int main() { n   int n = read(); n   for (register int i = 1; i <= n; ++i) { n     a[i].x = read(); n     a[i].l = read(); n   } n   for (register int i = n - 1; i >= 1; --i) { n     int cur = i + 1; n     mx[i] = a[i].x + a[i].l; n     while (cur && a[i].x + a[i].l >= a[cur].x) { n       mx[i] = max(mx[i], mx[cur]); n       cur = skip[cur][0]; n     } n     skip[i][0] = cur; n     f[i][0] = a[cur].x - mx[i]; n   } n   for (register int i = 1; i <= 20; ++i) { n     for (register int x = 1; x <= n; ++x) { n       f[x][i] = f[x][i - 1] + f[skip[x][i - 1]][i - 1]; n       skip[x][i] = skip[skip[x][i - 1]][i - 1]; n     } n   } n   int Q = read(); n   while (Q--) { n     int l = read(), r = read(); n     if (skip[l][0] > r) { n       printf( 0 n ); n     } else { n       long long ans = 0; n       for (register int i = 20; i >= 0; --i) { n         if (skip[l][i] && skip[l][i] <= r) { n           ans += f[l][i]; n           l = skip[l][i]; n         } n       } n       printf( %lld n , ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int q; n   cin >> q; n   while (q--) { n     int n; n     cin >> n; n     vector<int> v(n); n     set<int> st; n     for (int i = 0; i < n; i++) { n       cin >> v[i]; n       st.insert(v[i]); n     } n     sort(v.begin(), v.end()); n     int mx = v.back(); n     int ans = mx; n     if (mx % 2 == 0 && mx % 3 == 0 && mx % 5 == 0) { n       if (st.find(mx / 2) != st.end() && st.find(mx / 3) != st.end() && n           st.find(mx / 5) != st.end()) { n         ans = max(ans, mx / 2 + mx / 3 + mx / 5); n       } n     } n     vector<int> tmp; n     while (!st.empty() && tmp.size() < 3) { n       int x = *st.rbegin(); n       st.erase(prev(st.end())); n       bool f = 1; n       for (auto it : tmp) { n         if (it % x == 0 || x % it == 0) f = 0; n       } n       if (f) tmp.push_back(x); n     } n     int sum = 0; n     for (auto it : tmp) sum += it; n     ans = max(ans, sum); n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, q, i, j, u, v, k; n int a[300003]; n int last[19]; n int go[300003][19]; n int main() { n   cin >> n >> q; n   for (i = 0; i < n; i++) cin >> a[i]; n   for (i = 0; i < 19; i++) { n     go[n][i] = n; n     last[i] = n; n   } n   for (i = n - 1; i >= 0; i--) { n     for (j = 0; j < 19; j++) go[i][j] = n; n     for (j = 0; j < 19; j++) { n       if ((a[i] >> j) & 1) { n         for (k = 0; k < 19; k++) go[i][k] = min(go[i][k], go[last[j]][k]); n         last[j] = i; n         go[i][j] = i; n       } n     } n   } n   for (i = 0; i < q; i++) { n     cin >> u >> v; n     u--, v--; n     bool good = false; n     for (j = 0; j < 19; j++) good |= (((a[v] >> j) & 1) && go[u][j] <= v); n     if (good) n       cout <<  Shi n ; n     else n       cout <<  Fou n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> arr(78498); n vector<vector<int>> in(505, vector<int>(505)); n vector<vector<int>> out(505, vector<int>(505)); n void SieveOfEratosthenes(int n) { n   vector<bool> prime(n + 1, true); n   for (int p = 2; p * p <= n; p++) { n     if (prime[p] == true) { n       for (int i = p * p; i <= n; i += p) prime[i] = false; n     } n   } n   int i = 0; n   for (int p = 2; p <= n; p++) n     if (prime[p]) arr[i++] = p; n } n int f(int val) { n   int l = 0, u = arr.size() - 1, res = -1; n   while (l <= u) { n     int mid = (l + u) / 2; n     if (val == arr[mid]) n       return -2; n     else if (val < arr[mid]) { n       res = mid; n       u = mid - 1; n     } else n       l = mid + 1; n   } n   return res; n } n int main() { n   SieveOfEratosthenes(1000000); n   sort(arr.begin(), arr.end()); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) cin >> in[i][j]; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       int res = f(in[i][j]); n       if (res == -2) n         out[i][j] = 0; n       else if (res == -1) n         cout <<  error  << endl; n       else { n         out[i][j] = arr[res] - in[i][j]; n       } n     } n   } n   long long mini = pow(10, 18); n   for (int i = 0; i < n; i++) { n     long long tot = 0; n     for (int j = 0; j < m; j++) tot += (long long)out[i][j]; n     mini = min(mini, tot); n   } n   for (int j = 0; j < m; j++) { n     long long tot = 0; n     for (int i = 0; i < n; i++) tot += (long long)out[i][j]; n     mini = min(mini, tot); n   } n   cout << mini << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char a[103], c; n   int length, i, m; n   gets(a); n   length = strlen(a); n   for (i = length - 2; i >= 0; --i) { n     if (a[i] !=    ) { n       c = a[i]; n       break; n     } n   } n   if (c ==  a  || c ==  e  || c ==  i  || c ==  o  || c ==  u  || c ==  y  || n       c ==  A  || c ==  E  || c ==  I  || c ==  O  || c ==  U  || c ==  Y ) { n     printf( YES n ); n   } else { n     printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int vp, vd, t, f, c, ans; n   double cc, d; n   scanf( %d%d%d%d%d , &vp, &vd, &t, &f, &c); n   if (vd <= vp) { n     printf( 0 n ); n     return 0; n   } n   d = vp * t; n   cc = c; n   ans = 0; n   while (1) { n     if (d + d / (vd - vp) * vp >= cc - 1e-9) break; n     ans++; n     d = d + (2 * d / (vd - vp) + f) * vp; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v; n int n, x, now; n long long ans; n char a[3]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a; n     if (a[0] ==  a ) { n       cin >> x; n       v.insert(lower_bound(v.begin(), v.end(), x), x); n     } n     if (a[0] ==  d ) { n       cin >> x; n       v.erase(lower_bound(v.begin(), v.end(), x)); n     } n     if (a[0] ==  s ) { n       ans = 0; n       for (int i = 2; i < v.size(); i += 5) ans += v[i]; n       cout << ans << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void read(int &x) { n   int v = 0, f = 1; n   char c = getchar(); n   while (!isdigit(c) && c !=  - ) c = getchar(); n   if (c ==  - ) n     f = -1; n   else n     v = v * 10 + c -  0 ; n   while (isdigit(c = getchar())) v = v * 10 + c -  0 ; n   x = v * f; n } n inline void read(long long &x) { n   long long v = 0ll, f = 1ll; n   char c = getchar(); n   while (!isdigit(c) && c !=  - ) c = getchar(); n   if (c ==  - ) n     f = -1; n   else n     v = v * 10 + c -  0 ; n   while (isdigit(c = getchar())) v = v * 10 + c -  0 ; n   x = v * f; n } n inline void readc(char &x) { n   char c; n   while ((c = getchar()) ==    ) n     ; n   x = c; n } n inline void writes(string s) { puts(s.c_str()); } n inline void writeln() { writes(  ); } n inline void writei(int x) { n   if (x < 0) { n     putchar( - ); n     x = abs(x); n   } n   if (!x) putchar( 0 ); n   char a[25]; n   int top = 0; n   while (x) { n     a[++top] = (x % 10) +  0 ; n     x /= 10; n   } n   while (top) { n     putchar(a[top]); n     top--; n   } n } n inline void writell(long long x) { n   if (x < 0) { n     putchar( - ); n     x = abs(x); n   } n   if (!x) putchar( 0 ); n   char a[25]; n   int top = 0; n   while (x) { n     a[++top] = (x % 10) +  0 ; n     x /= 10; n   } n   while (top) { n     putchar(a[top]); n     top--; n   } n } n int n, h, m, i, j, dis[3005], cnt, ans, s = 0, t = 3001; n vector<int> e[3005]; n struct edg { n   int y, z; n } a[1111111]; n void add(int x, int y, int z) { n   cnt++; n   e[x].push_back(cnt * 2 - 2); n   e[y].push_back(cnt * 2 - 1); n   a[cnt * 2 - 2] = (edg){y, z}; n   a[cnt * 2 - 1] = (edg){x, 0}; n } n int id(int x, int y) { return (x - 1) * h + y; } n bool bfs(int s, int t) { n   queue<int> q; n   memset((dis), (0x16), (sizeof((dis)))); n   dis[s] = 0; n   q.push(s); n   while (!q.empty()) { n     int x = q.front(); n     q.pop(); n     for (__typeof((e[x]).begin()) it = (e[x]).begin(); it != (e[x]).end(); it++) n       if (a[*it].z) { n         if (dis[a[*it].y] > dis[x] + 1) { n           dis[a[*it].y] = dis[x] + 1; n           q.push(a[*it].y); n         } n       } n   } n   return dis[t] < dis[t + 1]; n } n int dfs(int x, int tt, int flw) { n   if (x == tt || !flw) return flw; n   int ans = 0; n   for (__typeof((e[x]).begin()) it = (e[x]).begin(); it != (e[x]).end(); it++) { n     if (a[*it].z && dis[x] + 1 == dis[a[*it].y]) { n       int t = dfs(a[*it].y, tt, min(flw, a[*it].z)); n       ans += t; n       flw -= t; n       a[*it].z -= t; n       a[(*it) ^ 1].z += t; n     } n     if (!flw) break; n   } n   if (flw) dis[x] = -1; n   return ans; n } n int dinic(int s, int t) { n   int ans = 0; n   while (bfs(s, t)) { n     ans += dfs(s, t, 0x3bbbbbbb); n   } n   return ans; n } n int main() { n   read(n); n   read(h); n   read(m); n   for (((i)) = (1); ((i)) <= ((n)); ((i))++) { n     for (((j)) = (1); ((j)) <= ((h)); ((j))++) { n       if (j < h) add(id(i, j + 1), id(i, j), 0x3bbbbbbb); n       add(s, id(i, j), j * 2 - 1); n       ans += j * 2 - 1; n     } n   } n   for (((i)) = (1); ((i)) <= ((m)); ((i))++) { n     int l, r, ys, c; n     read(l); n     read(r); n     read(ys); n     read(c); n     if (ys < h) n       for ((j) = (l); (j) <= (r); (j)++) { n         add(id(j, ys + 1), i + 2500, 0x3bbbbbbb); n       } n     add(i + 2500, t, c); n   } n   cout << ans - dinic(s, t) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class Jora { n  public: n   int a; n   int b; n }; n int myfunc(Jora A, Jora B) { n   if (A.a < B.a) return 1; n   if (A.a > B.a) return 0; n   return A.b < B.b; n } n Jora jrr[200200]; n int n; n vector<int> ser; n map<int, int> taken; n map<int, vector<int> > mp; n int sss; n int cal() { n   map<int, int> val, ans; n   for (int i = 0; i < ser.size(); i++) { n     val[ser[i]] = 2000000000; n     ans[ser[i]] = 0; n   } n   int sum = 0; n   int cur = 0; n   int tim = 1; n   for (int i = 0; i < ser.size(); i++) { n     int to = ser[i]; n     int uu = ((to - cur) + sum) % 1000000007; n     if (val[to] > uu) { n       val[to] = uu; n       ans[to] = tim; n     } else if (val[to] == uu) { n       ans[to] = (ans[to] + tim) % 1000000007; n     } n     int aa = val[to]; n     int bb = ans[to]; n     vector<int> &vvv = mp[to]; n     for (int j : vvv) { n       if (val[j] > aa) { n         val[j] = aa; n         ans[j] = bb; n       } else if (val[j] == aa) { n         ans[j] = (ans[j] + bb) % 1000000007; n       } n     } n     sum = aa; n     cur = to; n     tim = bb; n   } n   int tot = 0; n   int value = 2000000000; n   map<int, int> mrk; n   for (int i = 1; i <= n; i++) { n     int u = jrr[i].a; n     if (mrk[u] != 1 && u > sss) { n       if (val[u] < value) { n         value = val[u]; n         tot = ans[u]; n       } else if (val[u] == value) n         tot = (tot + ans[u]) % 1000000007; n     } n     mrk[u] = 1; n   } n   return tot; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &jrr[i].a, &jrr[i].b); n     mp[jrr[i].b].push_back(jrr[i].a); n     if (taken[jrr[i].a] != 1) ser.push_back(jrr[i].a); n     if (taken[jrr[i].b] != 1) ser.push_back(jrr[i].b); n     taken[jrr[i].a] = 1; n     taken[jrr[i].b] = 1; n     sss = max(sss, jrr[i].b); n   } n   sort(ser.begin(), ser.end()); n   printf( %d n , cal()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long f(long long l) { n   if (l < 10ll) return l; n   long long L = l, t = 1ll, ans = 0ll; n   int x = 0, last; n   while (L) { n     x++; n     last = L % 10; n     L /= 10; n   } n   for (int i = 1; i < x; i++) { n     ans += 9ll * t; n     if (i >= 2) t *= 10ll; n   } n   ans += (last - 1ll) * t; n   if (x >= 2) t *= 10ll; n   l -= t * 1ll * last; n   ans += l / 10ll; n   if (l % 10 >= last) ans++; n   return ans; n } n int main() { n   long long l, r; n   cin >> l >> r; n   cout << f(r) - f(l - 1); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long long k, l, c = 0; n   scanf( %I64d , &k); n   scanf( %I64d , &l); n   while (l % k == 0) { n     l /= k; n     c++; n   } n   if (c >= 0 && l == 1) n     printf( YES n%I64d , c - 1); n   else n     printf( NO ); n } n


#include <bits/stdc++.h> n using namespace std; n struct Node { n   int id, fail, next[26]; n   void init() { n     id = -1; n     fail = -1; n     memset(next, 0, sizeof(int) * 26); n   } n } node[2000050]; n int e; n void insert(char *str, int id) { n   int len = strlen(str); n   int pos = 0; n   for (int i = 0; i < len; i++) { n     int tmp = str[i] -  a ; n     if (node[pos].next[tmp] == 0) { n       node[++e].init(); n       node[pos].next[tmp] = e; n     } n     pos = node[pos].next[tmp]; n   } n   node[pos].id = id; n } n int head, tail, q[2000050]; n void bfs() { n   head = tail = 0; n   q[tail++] = 0; n   while (head < tail) { n     int u = q[head++]; n     for (int i = 0; i < 26; i++) { n       if (node[u].next[i]) { n         int tmp = node[u].fail; n         while (tmp != -1 && node[tmp].next[i] == 0) tmp = node[tmp].fail; n         if (tmp == -1) n           tmp = 0; n         else n           tmp = node[tmp].next[i]; n         node[node[u].next[i]].fail = tmp; n         q[tail++] = node[u].next[i]; n       } n     } n   } n } n int n, k, g; n char record[2000050]; n char word[2000050]; n int dp[2000050]; n int ans[2000050]; n int cnt[2000050]; n void solve() { n   memcpy(record + k * n, record, k * n); n   memset(dp, -1, sizeof(int) * (n * k)); n   int pos = 0; n   for (int i = 0; i < k * n * 2; i++) { n     while (pos != 0 && node[pos].next[record[i] -  a ] == 0) n       pos = node[pos].fail; n     pos = node[pos].next[record[i] -  a ]; n     if (node[pos].id != -1) dp[i % (n * k)] = node[pos].id; n   } n   memset(cnt, 0, sizeof(int) * g); n   for (int st = 0; st < k; st++) { n     bool flag = true; n     pos = st; n     for (int i = 0; i < n; i++) { n       if (dp[pos] == -1 || cnt[dp[pos]] > 0) { n         flag = false; n         while (pos >= 0) { n           cnt[dp[pos]] = 0; n           pos -= k; n         } n         break; n       } n       cnt[dp[pos]] = 1; n       ans[i] = dp[pos] + 1; n       pos += k; n     } n     if (flag) { n       printf( YES n ); n       for (int i = 0; i < n; i++) printf( %d  , ans[i]); n       printf(  n ); n       return; n     } n   } n   printf( NO n ); n } n int main() { n   while (scanf( %d%d , &n, &k) == 2) { n     scanf( %s , record); n     scanf( %d , &g); n     e = 0; n     node[0].init(); n     for (int i = 0; i < g; i++) { n       scanf( %s , word); n       insert(word, i); n     } n     bfs(); n     solve(); n   } n } n


#include <bits/stdc++.h> n const int xn = 200001, xe = 399999; n int head[xn], next[xe], to[xe], N, E, fa[xn], q[xn], ue[xn], O[xn * 5], L, n     col[xn], D; n int main() { n   scanf( %d , &N); n   bool allblack = 1; n   for (int i = 1; i <= N; i++) { n     scanf( %d , col + i); n     if (col[i] == -1) allblack = 0; n   } n   if (allblack) { n     puts( 1 ); n     return 0; n   } n   for (int i = 1, u, v; i < N; i++) { n     scanf( %d%d , &u, &v); n     next[++E] = head[u], to[E] = v, head[u] = E; n     next[++E] = head[v], to[E] = u, head[v] = E; n   } n   q[1] = 1; n   ue[1] = head[1]; n   D = 1; n   O[L = 1] = 1; n   while (D) { n     if (to[ue[D]] == fa[q[D]]) ue[D] = next[ue[D]]; n     if (ue[D]) { n       int To = to[ue[D]]; n       ue[D] = next[ue[D]]; n       O[++L] = To; n       fa[To] = q[D]; n       q[++D] = To; n       ue[D] = head[To]; n     } else { n       if (D > 1) { n         O[++L] = q[D - 1]; n         col[q[D - 1]] *= col[q[D]]; n       } n       D--; n     } n   } n   printf( 1 ); n   for (int i = 1; i < L - 1; i++) n     if (O[i] == fa[O[i + 1]] && col[O[i + 1]] == 1) n       printf(  %d %d %d , O[i + 1], O[i], O[i + 1]); n     else n       printf(  %d , O[i + 1]); n   if (col[1] == 1) printf(  1 ); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, cntu[1000006], cntd[1000006], preu[1000006], pred[1000006]; n string s; n vector<long long> lisu, lisd; n int main() { n   ios::sync_with_stdio(0); n   cin >> n >> s; n   lisd.push_back(0); n   lisu.push_back(0); n   for (int i = 1; i <= n; i++) { n     if (s[i - 1] ==  D ) n       cntd[i]++; n     else n       cntu[i]++; n     cntd[i] += cntd[i - 1]; n     cntu[i] += cntu[i - 1]; n     if (s[i - 1] ==  D ) n       pred[i] += i; n     else n       preu[i] += i; n     pred[i] += pred[i - 1]; n     preu[i] += preu[i - 1]; n     if (s[i - 1] ==  D ) { n       lisd.push_back(lisd.back() + i); n     } else { n       lisu.push_back(lisu.back() + i); n     } n   } n   for (int i = 1; i <= n; i++) { n     if (s[i - 1] ==  U ) { n       long long down_after = cntd[n] - cntd[i]; n       long long up_before = cntu[i - 1]; n       if (down_after <= up_before) n         cout << 2 * (pred[n] - pred[i]) + n                     2 * (-lisu[cntu[i - 1]] + lisu[cntu[i - 1] - down_after]) + n                     n - i + 1 n              <<    ; n       else n         cout << 2 * (lisd[cntd[i] + up_before + 1] - i - lisd[cntd[i]] - n                      lisu[cntu[i - 1]] + lisu[cntu[i - 1] - up_before]) + n                     i n              <<    ; n     } else { n       long long down_after = cntd[n] - cntd[i]; n       long long up_before = cntu[i - 1]; n       if (up_before <= down_after) n         cout << 2 * (lisd[cntd[i] + up_before] - lisd[cntd[i]] - n                      lisu[cntu[i - 1]] + lisu[cntu[i - 1] - up_before]) + n                     i n              <<    ; n       else n         cout << 2 * (lisd[cntd[i] + down_after] - lisd[cntd[i]] - n                      lisu[cntu[i - 1]] + lisu[cntu[i - 1] - down_after - 1] + n                      i) + n                     n - i + 1 n              <<    ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   long long t; n   cin >> t; n   while (t--) { n     long long a; n     cin >> a; n     long long b; n     cin >> b; n     if (b >= a) { n       if (b % a == 0 && pow(2, (long long)log2(b / a)) == b / a) { n         if ((long long)(log2(b / a)) % 3 == 0) { n           cout << (long long)log2(b / a) / 3 << endl; n         } else { n           cout << (long long)(1 + (log2(b / a) / 3)) << endl; n         } n       } else { n         cout << -1 << endl; n       } n     } else { n       if (a % b == 0 && pow(2, (long long)log2(a / b)) == a / b) { n         if ((long long)(log2(a / b)) % 3 == 0) { n           cout << (long long)log2(a / b) / 3 << endl; n         } else { n           cout << (long long)(1 + (log2(a / b) / 3)) << endl; n         } n       } else { n         cout << -1 << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, count = 0; n   for (int i = 0; i < 5; i++) { n     for (int j = 0; j < 5; j++) { n       cin >> n; n       if (n == 1) { n         count = abs(2 - i) + abs(2 - j); n         cout << count << endl; n         return 0; n       } n     } n   } n   cout << count << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, v; n   int sum = 0; n   int cnt[10]; n   memset(cnt, 0, sizeof cnt); n   cin >> n; n   while (n--) { n     cin >> v; n     cnt[v]++; n     sum += v; n   } n   if (cnt[0] == 0) { n     cout <<  -1 ; n     return 0; n   } n   int mod = sum % 3; n   if (mod != 0) { n     bool deleted = 0; n     for (int i = mod; i < 9; i += 3) { n       if (cnt[i] > 0) { n         deleted = 1; n         cnt[i]--; n         break; n       } n     } n     if (!deleted) { n       int left = 2; n       for (int i = mod == 1 ? 2 : 1; i < 9 && left; i += 3) { n         int take = min(left, cnt[i]); n         left -= take; n         cnt[i] -= take; n       } n     } n   } n   string res; n   for (int i = 9; i >= 0; i--) { n     if (i == 0 && res.empty()) cnt[0] = 1; n     res += string(cnt[i],  0  + i); n   } n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<long long, string> > v[10020]; n int main() { n   long long n, m; n   pair<long long, string> p; n   long long temp; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> p.second; n     cin >> temp; n     cin >> p.first; n     v[temp].push_back(p); n   } n   for (int i = 1; i <= m; i++) { n     sort(v[i].begin(), v[i].end()); n   } n   for (int i = 1; i <= m; i++) { n     int k = v[i].size(); n     if (k < 2 || (k > 2 && v[i][k - 2].first == v[i][k - 3].first)) { n       cout <<  ?  << endl; n     } else { n       cout << v[i][k - 1].second <<     << v[i][k - 2].second << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T gcd(T a, T b) { n   return b ? gcd(b, a % b) : a; n } n const double EPS = 1e-9; n const double PI = acos(-1.); n const int INF = 1e8; n const int MAXN = 505; n const long long MOD = 1e9 + 7; n int v[MAXN]; n int dp[MAXN][MAXN]; n void solve() { n   int n, i, j, k, lim, fl; n   scanf( %d , &n); n   for ((i) = (0); (i) < (int)(n); (i)++) { n     scanf( %d , &v[i]); n   } n   for ((i) = (0); (i) < (int)(n); (i)++) { n     for ((j) = (0); (j) < (int)(n); (j)++) { n       dp[i][j] = INF; n     } n   } n   for (i = n - 1; i >= 0; i--) { n     dp[i][i] = 1; n     if (i < n - 1) { n       dp[i][i + 1] = 2; n       if (v[i] == v[i + 1]) { n         dp[i][i + 1] = 1; n       } n     } n     for (j = i + 2; j < n; j++) { n       for (k = i; k < j; k++) { n         dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j]); n       } n       if (v[i] == v[j]) { n         dp[i][j] = min(dp[i][j], dp[i + 1][j - 1]); n       } n     } n   } n   printf( %d n , dp[0][n - 1]); n } n int main() { n   int t = 1; n   while (t--) { n     solve(); n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   long long int n; n   cin >> n; n   vector<vector<long double> > dist(n + 1, vector<long double>(n + 1)); n   vector<long double> x(n + 1), y(n + 1), z(n + 1); n   vector<long long int> vis(n + 1, 0); n   for (long long int i = 1; i <= n; i++) cin >> x[i] >> y[i] >> z[i]; n   for (long long int i = 1; i <= n; i++) { n     for (long long int j = 1; j <= n; j++) { n       dist[i][j] = (x[i] - x[j]) * (x[i] - x[j]) + n                    (y[i] - y[j]) * (y[i] - y[j]) + n                    (z[i] - z[j]) * (z[i] - z[j]); n       dist[i][j] = sqrt(dist[i][j]); n     } n   } n   for (long long int i = 1; i <= n; i++) { n     if (vis[i] == 0) { n       long double big = 1e18 * 1.0; n       long long int ans; n       for (long long int j = i + 1; j <= n; j++) { n         if (vis[j] == 0) { n           if (dist[i][j] < big) { n             big = dist[i][j]; n             ans = j; n           } n         } n       } n       vis[ans] = 1; n       vis[i] = 1; n       cout << i <<     << ans <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int getint() { n   static char c; n   while ((c = getchar()) <  0  || c >  9 ) n     ; n   int res = c -  0 ; n   while ((c = getchar()) >=  0  && c <=  9 ) res = res * 10 + c -  0 ; n   return res; n } n const int MaxN = 200005; n const int M = 1000000007; n inline int add(int a, const int &b) { n   a += b; n   return a >= M ? a - M : a; n } n inline int dec(int a, const int &b) { n   a -= b; n   return a < 0 ? a + M : a; n } n int n; n int key[MaxN]; n vector<int> adj[MaxN]; n int inv[MaxN]; n int phi[MaxN]; n vector<int> divs[MaxN]; n vector<int> coe[MaxN]; n int prod[MaxN]; n inline void init_math() { n   inv[1] = phi[1] = 1; n   for (int i = 2; i <= n; ++i) { n     phi[i] = i; n     inv[i] = (long long)(M - M / i) * inv[M % i] % M; n   } n   for (int i = 1; i <= n; ++i) { n     divs[i].push_back(i); n     for (int j = i << 1; j <= n; j += i) { n       divs[j].push_back(i); n       phi[j] -= phi[i]; n     } n   } n   for (int i = 1; i <= n; ++i) coe[i].resize(divs[i].size()); n   coe[1][0] = 1; n   for (int i = 2; i <= n; ++i) { n     int p = divs[i][1]; n     if (p == i) { n       coe[i][0] = 1; n       coe[i][1] = inv[p - 1]; n       continue; n     } n     int q = i; n     while (q % p == 0) q /= p; n     for (int d : divs[i]) prod[d] = 0; n     for (int i = 0; i < divs[p].size(); ++i) n       for (int j = 0; j < divs[q].size(); ++j) { n         int u = divs[p][i], a = coe[p][i]; n         int v = divs[q][j], b = coe[q][j]; n         int x = u * v, y = (long long)a * b % M; n         prod[x] = add(prod[x], y); n       } n     for (int j = 0; j < divs[i].size(); ++j) coe[i][j] = prod[divs[i][j]]; n   } n } n bool book[MaxN]; n int q_n, q[MaxN]; n int fa[MaxN]; n int dep[MaxN]; n int size[MaxN]; n inline void bfs(const int &sv, const int &prev) { n   q[q_n = 1] = sv; n   fa[sv] = prev, dep[sv] = 1; n   for (int i = 1; i <= q_n; ++i) { n     int u = q[i]; n     for (int v : adj[u]) n       if (!book[v] && v != fa[u]) { n         fa[v] = u; n         dep[v] = dep[u] + 1; n         q[++q_n] = v; n       } n   } n } n inline int centroid(int root) { n   bfs(root, 0); n   int vC = 0, wC = q_n; n   for (int i = 1; i <= q_n; ++i) size[q[i]] = 1; n   for (int i = q_n; i >= 1; --i) { n     int u = q[i]; n     size[fa[u]] += size[u]; n     int vW = q_n - size[u]; n     for (int v : adj[u]) n       if (!book[v] && v != fa[u]) vW = max(vW, size[v]); n     if (vW < wC) wC = vW, vC = u; n   } n   return vC; n } n int nVer = 0; n int ver[MaxN]; n int sum = 0; n int sum_phi[MaxN]; n inline int calc(int v) { n   int sum = 0; n   for (int i = 0; i < divs[v].size(); ++i) { n     int d = divs[v][i]; n     sum = add(sum, (long long)coe[v][i] * sum_phi[d] % M); n   } n   return (long long)sum * phi[v] % M; n } n inline void work(int vC) { n   nVer = q_n; n   for (int i = 1; i <= q_n; ++i) ver[i] = q[i]; n   for (int i = 1; i <= nVer; ++i) { n     int v = key[ver[i]]; n     for (int d : divs[v]) sum_phi[d] = add(sum_phi[d], phi[v]); n   } n   for (int u : adj[vC]) n     if (!book[u]) { n       bfs(u, vC); n       for (int i = 1; i <= q_n; ++i) { n         int v = key[q[i]]; n         for (int d : divs[v]) sum_phi[d] = dec(sum_phi[d], phi[v]); n       } n       for (int i = 1; i <= q_n; ++i) { n         int v = q[i]; n         int w = key[v]; n         sum = add(sum, (long long)dep[v] * calc(w) % M); n       } n       for (int i = 1; i <= q_n; ++i) { n         int v = key[q[i]]; n         for (int d : divs[v]) sum_phi[d] = add(sum_phi[d], phi[v]); n       } n     } n   for (int i = 1; i <= nVer; ++i) { n     int v = key[ver[i]]; n     for (int d : divs[v]) sum_phi[d] = 0; n   } n } n void solve(int root) { n   int vC = centroid(root); n   book[vC] = true; n   work(vC); n   for (int v : adj[vC]) n     if (!book[v]) solve(v); n } n int main() { n   n = getint(); n   for (int u = 1; u <= n; ++u) key[u] = getint(); n   for (int i = 1; i < n; ++i) { n     int u = getint(), v = getint(); n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   init_math(); n   solve(1); n   sum = (long long)sum * inv[n] % M; n   sum = (long long)sum * inv[n - 1] % M; n   sum = (long long)sum * 2 % M; n   printf( %d n , sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int xx[4] = {0, 0, 1, -1}; n int yy[4] = {1, -1, 0, 0}; n int n, m, b, s; n int x[int(1e5)], y[int(1e5)]; n int main() { n   scanf( %d%d , &n, &m); n   scanf( %d , &b); n   for (int i = (1), _b = (b); i <= _b; i++) { n     scanf( %d , &s); n     x[s] = true; n   } n   scanf( %d , &b); n   for (int i = (1), _b = (b); i <= _b; i++) { n     scanf( %d , &s); n     y[s] = true; n   } n   for (int i = (0), _b = (2 * n * m); i <= _b; i++) { n     int u = i % n; n     int v = i % m; n     if (x[u] || y[v]) { n       x[u] = true; n       y[v] = true; n     } n   } n   for (int i = (0), _b = (n - 1); i <= _b; i++) n     if (!x[i]) { n       cout <<  No  << endl; n       return 0; n     } n   for (int i = (0), _b = (m - 1); i <= _b; i++) n     if (!y[i]) { n       cout <<  No  << endl; n       return 0; n     } n   cout <<  Yes  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e3 + 10; n int ad[N][N]; n long long n; n pair<int, int> t[N]; n bool ok; n int c[N]; n void connect(int k) { n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       if (abs(t[i].first - t[j].first) + abs(t[i].second - t[j].second) > k) n         ad[i][j] = ad[j][i] = 1; n       else n         ad[i][j] = ad[j][i] = 0; n     } n   } n } n void dfs(int v, int col) { n   c[v] = col; n   for (int i = 0; i < n; i++) { n     if (ad[v][i] && c[i] == 0) n       dfs(i, 3 - col); n     else if (ad[v][i] && c[i] == col) { n       ok = 0; n       return; n     } n   } n } n int tt = 10000; n int bs() { n   long long top = 10100, low = 0; n   while (top > low + 1) { n     int md = (low + top) / 2; n     connect(md); n     ok = true; n     for (int i = 0; i < n; i++) c[i] = 0; n     for (int i = 0; i < n; i++) { n       if (!c[i]) { n         dfs(i, 1); n       } n     } n     if (ok) n       top = md; n     else n       low = md; n     tt = top; n   } n   return top; n } n bool mark[N] = {0}; n void dfsp(int v) { n   mark[v] = 1; n   for (int i = 0; i < n; i++) { n     if (ad[v][i] && mark[i] == 0) dfsp(i); n   } n } n const int M = 1e9 + 7; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   if (n == 2) { n     cout << 0 << endl << 2; n     return 0; n   } n   for (int i = 0; i < n; i++) cin >> t[i].first >> t[i].second; n   cout << bs() << endl; n   connect(tt); n   long long x = 1; n   for (int i = 0; i < n; i++) { n     if (mark[i] == 0) { n       x *= 2; n       x %= M; n       dfsp(i); n     } n   } n   cout << x << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long ans = 0; n int n, u, r; n vector<int> b, k; n vector<int> p; n void exec(vector<int> a, int type, int count) { n   if (count < u) { n     if (type == 0) { n       for (int i = 0; i < n; ++i) a[i] = a[i] ^ b[i]; n       exec(a, 1, count + 1); n     } else { n       vector<int> aa(n); n       for (int i = 0; i < n; ++i) aa[i] = a[p[i]] + r; n       int rest = u - count - 1; n       long long cur_cand = 0; n       if (rest & 1) { n         vector<int> aaa(n); n         for (int i = 0; i < n; ++i) aaa[i] = aa[i] ^ b[i]; n         cur_cand = inner_product(aaa.begin(), aaa.end(), k.begin(), 0LL); n       } else { n         cur_cand = inner_product(aa.begin(), aa.end(), k.begin(), 0LL); n       } n       ans = max(cur_cand, ans); n       exec(aa, 0, count + 1); n       exec(aa, 1, count + 1); n     } n   } else { n     long long cand = inner_product(a.begin(), a.end(), k.begin(), 0LL); n     ans = max(cand, ans); n   } n } n int main() { n   cin >> n >> u >> r; n   vector<int> a(n); n   b.resize(n); n   k.resize(n); n   p.resize(n); n   for (int i = 0; i < n; ++i) cin >> a[i]; n   for (int i = 0; i < n; ++i) cin >> b[i]; n   for (int i = 0; i < n; ++i) cin >> k[i]; n   for (int i = 0; i < n; ++i) { n     cin >> p[i]; n     --p[i]; n   } n   for (int i = 0; i < n; ++i) { n     ans += k[i] * (u & 1 ? a[i] ^ b[i] : a[i]); n   } n   exec(a, 1, 0); n   exec(a, 0, 0); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int book[100010]; n vector<pair<int, int>> v; n deque<int> high, low; n int main() { n   int n, k, ans = 0, num = 0, r, l; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) cin >> book[i]; n   for (r = 1, l = 1; r <= n; r++) { n     while (!high.empty() && high.back() < book[r]) high.pop_back(); n     high.push_back(book[r]); n     while (!low.empty() && low.back() > book[r]) low.pop_back(); n     low.push_back(book[r]); n     while (!high.empty() && !low.empty() && high.front() - low.front() > k) { n       if (v.empty()) { n         num = 1; n         ans = r - l; n         v.push_back(make_pair(l, r - 1)); n       } else if (r - l > ans) { n         v.clear(); n         num = 1; n         ans = r - l; n         v.push_back(make_pair(l, r - 1)); n       } else if (!v.empty() && r - l == ans) { n         num++; n         v.push_back(make_pair(l, r - 1)); n       } n       if (high.front() == book[l]) high.pop_front(); n       if (low.front() == book[l]) low.pop_front(); n       l++; n     } n   } n   while (l <= n) { n     if (v.empty()) { n       num = 1; n       ans = r - l; n       v.push_back(make_pair(l, r - 1)); n     } else if (r - l > ans) { n       v.clear(); n       num = 1; n       ans = r - l; n       v.push_back(make_pair(l, r - 1)); n     } else if (!v.empty() && r - l == ans) { n       num++; n       v.push_back(make_pair(l, r - 1)); n     } n     if (high.front() == book[l]) high.pop_front(); n     if (low.front() == book[l]) low.pop_front(); n     l++; n   } n   cout << ans <<     << num << endl; n   for (int i = 0; i < v.size(); i++) n     cout << v[i].first <<     << v[i].second << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > Dates; n int n, x, y, ans, cur; n int main() { n   std::ios_base::sync_with_stdio(false); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> x >> y; n     Dates.push_back(make_pair(x, y)); n   } n   sort(Dates.begin(), Dates.end()); n   for (int i = 0; i < Dates.size(); i++) { n     if (Dates[i].second > cur) n       cur = Dates[i].second; n     else if (Dates[i].second < cur) { n       ans++; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n string a[] = { zero ,  one ,  two ,    three ,  four , n                five ,  six ,  seven ,  eight ,  nine }; n string b[] = { ten ,    twenty ,   thirty ,  forty ,  fifty , n                sixty ,  seventy ,  eighty ,  ninety }; n string c[] = { eleven ,   twelve ,     thirteen ,  fourteen ,  fifteen , n                sixteen ,  seventeen ,  eighteen ,  nineteen }; n int main() { n   int n; n   cin >> n; n   if (n < 10) { n     cout << a[n] << endl; n     return 0; n   } else { n     if (n % 10 == 0) { n       cout << b[(n / 10) - 1] << endl; n       return 0; n     } else if (n > 10 && n < 20) { n       cout << c[(n % 10) - 1] << endl; n       return 0; n     } else { n       cout << b[(n / 10) - 1] <<  -  << a[n % 10] << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const long long int INF = 3e16; n const int N = 200010; n int n; n int ask(vector<int> v) { n   printf( ? ); n   for (int i = 0; i < n; i++) printf(  %d , v[i]); n   printf(  n ); n   fflush(stdout); n   int ans; n   scanf( %d , &ans); n   return ans; n } n int main() { n   scanf( %d , &n); n   int pivo = -1; n   for (int i = 1; i < n; i++) { n     vector<int> tmp(n); n     tmp[n - 1] = 1; n     for (int j = 0; j < n - 1; j++) { n       tmp[j] = 1 + i; n     } n     int k = ask(tmp); n     if (k == 0) { n       pivo = i; n       break; n     } n   } n   if (pivo == -1) pivo = n; n   vector<int> ans(n + 1); n   ans[n] = pivo; n   for (int i = 1; i <= n; i++) { n     if (i == pivo) continue; n     vector<int> tmp(n); n     tmp[n - 1] = (n + 1) - pivo; n     for (int j = 0; j < n - 1; j++) { n       tmp[j] = (n + 1) - i; n     } n     int k = ask(tmp); n     ans[k] = i; n   } n   printf( ! ); n   for (int i = 1; i <= n; i++) printf(  %d , ans[i]); n   printf(  n ); n   fflush(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> a[300000 + 13]; n int n; n void erf() { n   for (int i = 1; i < n; i++) { n     if (a[i - 1].first + 1 >= a[i].first) a[i].first = a[i - 1].first + 1; n   } n } n int main() { n   int cur, ebte; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i].first; n     a[i].second = i; n   } n   sort(a, a + n); n   erf(); n   pair<int, int> b; n   for (int i = 0; i < n; i++) { n     b = make_pair(a[i].second, a[i].first); n     swap(a[i], b); n   } n   sort(a, a + n); n   for (int i = 0; i < n; i++) { n     cout << a[i].second <<    ; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e6; n long long a[100000 + 5]; n bool vis[100000 + 5]; n char c[] = { 6 ,  7 ,  8 ,  9 ,  T ,  J ,  Q ,  K ,  A }; n int main() { n   long long n; n   string s, s1, s2; n   cin >> s >> s1 >> s2; n   if (s1[1] == s[0]) { n     if (s2[1] != s[0]) n       cout <<  YES  << endl; n     else { n       int i = 0; n       for (i = 0; i < 9; i++) { n         if (s1[0] == c[i]) break; n       } n       int j = 0; n       for (j = 0; j < 9; j++) { n         if (s2[0] == c[j]) break; n       } n       if (i > j) n         cout <<  YES  << endl; n       else n         cout <<  NO  << endl; n     } n   } else { n     if (s2[1] == s[0]) n       cout <<  NO  << endl; n     else if (s1[1] == s2[1]) { n       int i = 0; n       for (i = 0; i < 9; i++) { n         if (s1[0] == c[i]) break; n       } n       int j = 0; n       for (j = 0; j < 9; j++) { n         if (s2[0] == c[j]) break; n       } n       if (i > j) n         cout <<  YES  << endl; n       else n         cout <<  NO  << endl; n     } else n       cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int getID(char nms[202][30], char s[30], int &len) { n   for (int i = 0; i < 30; ++i) { n     if (s[i] >=  a  && s[i] <=  z ) s[i] +=  A  -  a ; n   } n   for (int i = 0; i < len; ++i) { n     if (strcmp(nms[i], s) == 0) return i; n   } n   strcpy(nms[len], s); n   len++; n   return len - 1; n } n int search(int mailFrom[202], int mailTo[202], int p, int n, int h) { n   int max = 0; n   for (int i = 0; i < n; ++i) { n     if (mailFrom[i] == p) { n       int ans = search(mailFrom, mailTo, mailTo[i], n, h + 1); n       if (max < ans) max = ans; n     } n   } n   if (max == 0) return h; n   return max; n } n int main() { n   int n; n   while (cin >> n) { n     char nms[202][30] = {0}; n     int len = 0; n     int mailFrom[202] = {0}; n     int mailTo[202] = {0}; n     bool notHead[202] = {0}; n     for (int i = 0; i < n; ++i) { n       char n1[30], n2[30], t[30]; n       cin >> n1; n       cin >> t; n       cin >> n2; n       mailFrom[i] = getID(nms, n2, len); n       mailTo[i] = getID(nms, n1, len); n       notHead[mailTo[i]] = 1; n     } n     int max = 0; n     for (int i = 0; i < n; ++i) { n       if (!notHead[i]) { n         int ans = search(mailFrom, mailTo, i, n, 1); n         if (ans > max) max = ans; n       } n     } n     cout << max << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n ; n const long long N = 5e3 + 10; n vector<bitset<N> > v; n vector<pair<long long, pair<long long, long long> > > hor, ver; n long long n, cnt, y11, y2, x1, x2; n long long ans; n bool check(long long x, pair<long long, long long> a) { n   return a.first <= x && x <= a.second; n } n void solve() { n   cin >> n; n   for (long long i = 0; i < n; i++) { n     cin >> x1 >> y11 >> x2 >> y2; n     if (x1 > x2) swap(x1, x2); n     if (y11 > y2) swap(y11, y2); n     if (x1 == x2) n       ver.push_back({x1, {y11, y2}}); n     else n       hor.push_back({y11, {x1, x2}}); n   } n   if ((long long)((hor).size()) > (long long)((ver).size())) swap(hor, ver); n   v.resize((long long)((hor).size())); n   for (long long i = 0; i < (long long)((hor).size()); i++) { n     for (long long j = 0; j < (long long)((ver).size()); j++) { n       v[i][j] = check(hor[i].first, ver[j].second) && n                 check(ver[j].first, hor[i].second); n     } n   } n   for (long long i = 0; i < (long long)((v).size()); i++) { n     for (long long j = i + 1; j < (long long)((v).size()); j++) { n       cnt = (v[i] & v[j]).count(); n       ans += cnt * (cnt - 1) / 2; n     } n   } n   cout << ans <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   clock_t start, end; n   start = clock(); n   cout << fixed << setprecision(15); n   while (t--) { n     solve(); n   } n   end = clock(); n   double time_taken = double(end - start) / double(CLOCKS_PER_SEC); n } n


#include <bits/stdc++.h> n const double PI = 3.141592653589793238460; n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   cin >> t; n   while (t--) { n     long long int n; n     cin >> n; n     long long int a[n], i, sum = 0, maxi = -1000000000, mini = 1000000000; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n       if (a[i] > maxi) n         maxi = a[i]; n       else if (a[i] < mini) n         mini = a[i]; n     } n     for (i = 0; i < n - 1; i++) { n       long long int pro = a[i] * a[i + 1]; n       if (pro > sum) { n         sum = pro; n       } n     } n     cout << sum <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:1760777216 ) n using namespace std; n const double Pi = 3.14159265358979323846; n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     double R, r, k; n     scanf( %lf%lf%lf , &R, &r, &k); n     double x1 = 0.5 / R, x2 = 0.5 / r, rr = fabs(x1 - x2) / 2; n     double x = (x1 + x2) / 2, y = 2 * k * rr, a = x / sqrt(x * x + y * y); n     double dx = rr * a, dy = rr * sqrt(1 - a * a); n     double res = sqrt(x * x + y * y); n     printf( %.9lf n , (1 / (res - rr) - 1 / (res + rr)) / 2); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool arr[100100]; n int dp[100100]; n int pref[100100]; n int pref2[100100]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int n; n   cin >> n; n   char a; n   for (int i = 1; i <= n; i++) { n     cin >> a; n     if (a ==  G ) arr[i] = true; n   } n   for (int i = 1; i <= n; i++) { n     if (arr[i]) pref[i] = pref[i - 1] + 1; n   } n   for (int i = 1; i <= n; i++) { n     pref2[i] = pref2[i - 1] + arr[i]; n   } n   if (pref2[n] == n) { n     cout << n << endl; n     return 0; n   } n   if (pref2[n] == 0) { n     cout << 0 << endl; n     return 0; n   } n   for (int i = n; i >= 1; i--) { n     if (pref[i - 1] > 0 && pref[i] > 0) pref[i - 1] = pref[i]; n   } n   int ma = -1e9; n   for (int i = 1; i <= n; i++) { n     if (!arr[i]) { n       if (pref[i - 1] + pref[i + 1] < pref2[n]) n         ma = max(ma, pref[i - 1] + pref[i + 1] + 1); n       else n         ma = max(ma, pref[i - 1] + pref[i + 1]); n     } n   } n   cout << ma << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n ifstream f( test.in ); n ofstream g( test.out ); n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n, ans = 0; n     cin >> n; n     stack<int> s; n     int x; n     cin >> x; n     s.push(x); n     for (int i = 2; i <= n; i++) { n       cin >> x; n       while (!s.empty() and s.top() > x) { n         ans++; n         s.pop(); n       } n       s.push(x); n     } n     while (!s.empty()) s.pop(); n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string a, b; n int main() { n   cin >> a >> b; n   if (a == b) n     cout << a; n   else n     cout << 1; n } n


#include <bits/stdc++.h> n using namespace std; n vector<set<int> > newadj(212345); n int visit[212345]; n vector<vector<int> > adj(212345); n vector<int> vec, vecans; n set<int> seti; n int cnt = 0; n int pos[212345]; n int dfs(int cur) { n   cnt++; n   visit[cur] = 1; n   set<int>::iterator it; n   for (it = newadj[cur].begin(); it != newadj[cur].end(); it++) { n     if (!visit[*it]) { n       dfs(*it); n     } n   } n   return 0; n } n int main() { n   std::ios::sync_with_stdio(false); n   int n, m; n   int i, j, u, v; n   cin >> n >> m; n   for (i = 0; i < m; i++) { n     cin >> u >> v; n     u--; n     v--; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   int mini = (1000 * 1000 * 1000 + 5), vert; n   for (i = 0; i < n; i++) { n     if (mini > adj[i].size()) { n       mini = adj[i].size(); n       vert = i; n     } n   } n   int sumi = 0; n   for (i = 0; i < n; i++) { n     sumi++; n     pos[i] = vert; n   } n   vec.push_back(vert); n   for (i = 0; i < adj[vert].size(); i++) { n     sumi--; n     pos[adj[vert][i]] = adj[vert][i]; n     vec.push_back(adj[vert][i]); n     seti.insert(adj[vert][i]); n   } n   seti.insert(vert); n   set<int>::iterator it; n   for (i = 1; i < vec.size(); i++) { n     for (it = seti.begin(); it != seti.end(); it++) { n       newadj[vec[i]].insert(*it); n     } n     cnt = 0; n     for (j = 0; j < adj[vec[i]].size(); j++) { n       if (pos[adj[vec[i]][j]] == vert) { n         cnt++; n         continue; n       } n       newadj[vec[i]].erase(pos[adj[vec[i]][j]]); n     } n     if (cnt == sumi) { n       newadj[vec[i]].erase(vert); n     } else { n       newadj[vert].insert(vec[i]); n     } n   } n   for (i = 0; i < vec.size(); i++) { n     if (!visit[vec[i]]) { n       cnt = 0; n       dfs(vec[i]); n       if (i == 0) { n         cnt += sumi - 1; n       } n       vecans.push_back(cnt); n     } n   } n   sort(vecans.begin(), vecans.end()); n   cout << vecans.size() << endl; n   for (i = 0; i < vecans.size(); i++) { n     cout << vecans[i] <<    ; n   } n   return 0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int b = -1, n = 0, limit = 10; n   string pass, box =  0000000000 , zero, one, two, three, four, five, six, n                seven, eight, nine; n   cin >> pass; n   cin >> zero; n   cin >> one; n   cin >> two; n   cin >> three; n   cin >> four; n   cin >> five; n   cin >> six; n   cin >> seven; n   cin >> eight; n   cin >> nine; n   while (n < 8) { n     for (int i = b + 1, i2 = 0; i < limit; i++, i2++) { n       box[i2] = pass[i]; n       b = i; n     } n     if (box == zero) n       cout <<  0 ; n     else if (box == one) n       cout <<  1 ; n     else if (box == two) n       cout <<  2 ; n     else if (box == three) n       cout <<  3 ; n     else if (box == four) n       cout <<  4 ; n     else if (box == five) n       cout <<  5 ; n     else if (box == six) n       cout <<  6 ; n     else if (box == seven) n       cout <<  7 ; n     else if (box == eight) n       cout <<  8 ; n     else if (box == nine) n       cout <<  9 ; n     n++; n     limit += 10; n   } n   return 0; n } n