#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const double eps = 1e-9; n vector<long long> prnum; n void sieve() { n   bool prime[1000001]; n   memset(prime, true, sizeof(prime)); n   for (int i = 2; i * i <= 1000000; i++) { n     if (prime[i] == true) { n       for (int j = 2 * i; j <= 1000000; j += i) { n         prime[j] = false; n       } n     } n   } n   for (int i = 2; i <= 1000000; i++) { n     if (prime[i]) prnum.push_back(i); n   } n } n int main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     long long n, k; n     cin >> n >> k; n     if (k >= n) { n       cout << 1 <<   n ; n     } else { n       long long ans = 1000000000; n       for (long long i = 1; i * i <= n; i++) { n         if (i > k) break; n         if (n % i == 0) { n           if ((n / i) <= k) { n             ans = min(ans, i); n           } else { n             ans = min(ans, n / i); n           } n         } n       } n       cout << ans <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18; n signed main() { n   long long n, sum = 0, temp; n   multiset<long long> a; n   cin >> n; n   for (long long i = 0; i < n; ++i) { n     cin >> temp; n     a.insert(temp); n   } n   while (a.size() > 1) { n     long long temp_sum = 0, sz = a.size(); n     for (long long j = 0; j < ((sz % 2 == 0) ? 2 : min(3ll, sz)); ++j) { n       temp_sum += *a.begin(); n       a.erase(a.begin()); n     } n     sum += temp_sum; n     a.insert(temp_sum); n   } n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   int x = 0, f = 0; n   char ch = getchar(); n   while (!isdigit(ch)) f |= ch ==  - , ch = getchar(); n   while (isdigit(ch)) x = 10 * x + ch -  0 , ch = getchar(); n   return f ? -x : x; n } n template <typename T> n void print(T x) { n   if (x < 0) putchar( - ), x = -x; n   if (x >= 10) print(x / 10); n   putchar(x % 10 +  0 ); n } n template <typename T> n void print(T x, char let) { n   print(x), putchar(let); n } n const int maxn = 2e5 + 5; n const int mn = (1 << 18) - 1; n int n, a[maxn], b[mn], f[mn], sz[mn]; n int find(int k) { n   if (f[k] == k) return k; n   return f[k] = find(f[k]); n } n void merge(int a, int b) { n   int fa = find(a); n   int fb = find(b); n   if (sz[fa] <= sz[fb]) { n     f[fa] = fb; n     sz[fb] += sz[fa]; n   } else { n     f[fb] = fa; n     sz[fa] += sz[fb]; n   } n } n int main() { n   n = read(); n   long long sum1 = 0; n   for (int i = 1; i <= n; i++) { n     a[i] = read(); n     sum1 += a[i]; n     b[a[i]]++; n   } n   b[0]++; n   for (int i = 0; i <= mn; i++) f[i] = i; n   long long sum = 0; n   for (int i = mn; i >= 1; i--) { n     if (b[0] > 0 && b[i] > 0 && find(0) != find(i)) { n       merge(0, i); n       sum += (long long)(b[0] + b[i] - 1) * i; n       b[0] = b[i] = 1; n     } n     for (int j = i; j; j = (j - 1) & i) { n       int k = i ^ j; n       if (b[j] > 0 && b[k] > 0 && find(j) != find(k)) { n         merge(j, k); n         sum += (long long)(b[j] + b[k] - 1) * i; n         b[j] = b[k] = 1; n       } n     } n   } n   cout << sum - sum1 << 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 x, y; n   cin >> x >> y; n   int u = 0, v = 0, turns = 0, flag = 1, c = 1; n   while (1) { n     if (u == x and v == y) break; n     if (flag == 1) { n       int d = 0; n       while (d != c) { n         u++; n         if (u == x and v == y) break; n         d++; n       } n       flag = 2; n       if (u != x or v != y) turns++; n     } else if (flag == 2) { n       int d = 0; n       while (d != c) { n         v++; n         if (u == x and v == y) break; n         d++; n       } n       if (u != x or v != y) turns++; n       flag = 3; n       c++; n     } else if (flag == 3) { n       int d = 0; n       while (d != c) { n         u--; n         if (u == x and v == y) break; n         d++; n       } n       flag = 4; n       if (u != x or v != y) turns++; n     } else if (flag == 4) { n       int d = 0; n       while (d != c) { n         v--; n         if (u == x and v == y) break; n         d++; n       } n       c++; n       flag = 1; n       if (u != x or v != y) turns++; n     } n   } n   cout << turns; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string line; n   int persons = 0; n   int data = 0; n   while (getline(cin, line)) { n     if (line[0] ==  + ) n       ++persons; n     else if (line[0] ==  - ) n       --persons; n     else n       data += (line.size() - line.find( : ) - 1) * persons; n   } n   cout << data << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n string s; n int l = 0, u = 0, d = 0; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     int l = -1, u = -1, d = -1; n     cin >> s; n     for (int j = 0; j < s.length(); j++) { n       if (isdigit(s[j])) d = j; n       if (islower(s[j])) l = j; n       if (isupper(s[j])) u = j; n     } n     if (d == -1) n       for (int j = 0; j < s.length(); j++) n         if (j != l && j != u) { n           s[j] =  4 ; n           d = j; n           break; n         } n     if (l == -1) n       for (int j = 0; j < s.length(); j++) n         if (j != d && j != u) { n           s[j] =  a ; n           l = j; n           break; n         } n     if (u == -1) n       for (int j = 0; j < s.length(); j++) n         if (j != l && j != d) { n           s[j] =  A ; n           u = j; n           break; n         } n     cout << s << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   int count = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     string a; n     cin >> a; n     if (a ==  ABSINTH  || a ==  BEER  || a ==  BRANDY  || a ==  CHAMPAGNE  || n         a ==  GIN  || a ==  RUM  || a ==  SAKE  || a ==  TEQUILA  || n         a ==  VODKA  || a ==  WHISKEY  || a ==  WINE  || a ==  0  || a ==  1  || n         a ==  2  || a ==  3  || a ==  4  || a ==  5  || a ==  6  || a ==  7  || n         a ==  8  || a ==  9  || a ==  10  || a ==  11  || a ==  12  || n         a ==  13  || a ==  14  || a ==  15  || a ==  16  || a ==  17 ) n       count++; n   } n   cout << count; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int a, b; n   cin >> a >> b; n   int c = min(a, b); n   a = max(a, b) - c; n   cout << c <<     << a / 2; n   ; n } n int main() { n   int count = 1; n   while (count--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long modpower(long long n, long long k) { n   long long res = 1; n   while (k) { n     if (k & 1) res = (res * n) % 1000000009; n     k >>= 1; n     n = (n * n) % 1000000009; n   } n   return res; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n; n   cin >> n; n   long long a[n]; n   bool f = 0; n   for (long long i = 0; i < n; ++i) { n     cin >> a[i]; n     if (a[i] != 1) f = 1; n   } n   sort(a, a + n); n   if (!f) { n     for (long long i = 0; i < n - 1; ++i) { n       cout << a[i] <<    ; n     } n     cout << 2; n     return 0; n   } n   cout << 1 <<    ; n   for (long long i = 0; i < n - 1; ++i) { n     cout << a[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n vector<vector<int> > g; n vector<int> depth; n void dfs(int v, int d = 0) { n   depth[v] = d; n   for (int to : g[v]) { n     if (depth[to] == -1) { n       dfs(to, d + 1); n     } n   } n } n int main() { n   int n, m; n   cin >> n >> m; n   g.resize(n); n   for (int i = 0; i < m; ++i) { n     int u, v; n     cin >> u >> v; n     --u, --v; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   depth.assign(n, -1); n   dfs(0); n   int v = 0; n   for (int i = 1; i < n; ++i) { n     if (depth[i] > depth[v]) { n       v = i; n     } n   } n   depth.assign(n, -1); n   dfs(v); n   int ans = 0; n   for (int i = 0; i < n; ++i) { n     ans = max(ans, depth[i]); n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n class strocks { n  public: n   string strocksguy() {} n }; n int main() { n   strocks strocks; n   iostream::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, m; n   cin >> n >> m; n   string s, d; n   unordered_map<string, string> master; n   for (long long i = 0; i < m; i++) { n     cin >> s >> d; n     if (s.size() > d.size()) { n       master.insert(make_pair(s, d)); n     } else n       master.insert(make_pair(d, s)); n   } n   for (long long i = 0; i < n; i++) { n     cin >> s; n     if (master.find(s) != master.end()) n       cout << master.at(s) <<    ; n     else n       cout << s <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ii = pair<int, int>; n const int N = 100005; n int n, m, k; n vector<int> adj[N]; n int cnt[N]; n int mark[N]; n int pass; n int aux[N]; n bool ta[N]; n bool clique(int x) { n   pass++; n   mark[x] = pass; n   int id = 0; n   for (int i : adj[x]) n     if (!ta[i]) { n       mark[i] = pass; n       aux[id++] = i; n     } n   for (int i = 0; i < id; i++) { n     for (int j = i + 1; j < id; j++) { n       if (!binary_search(adj[aux[i]].begin(), adj[aux[i]].end(), aux[j])) n         return 0; n     } n   } n   return 1; n } n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     scanf( %d %d %d , &n, &m, &k); n     for (int i = 1; i <= n; i++) { n       adj[i].clear(); n       cnt[i] = 0; n       ta[i] = 0; n     } n     for (int i = 0; i < m; i++) { n       int a, b; n       scanf( %d %d , &a, &b); n       adj[a].push_back(b); n       adj[b].push_back(a); n       cnt[b]++; n       cnt[a]++; n     } n     for (int i = 1; i <= n; i++) { n       sort(adj[i].begin(), adj[i].end()); n     } n     priority_queue<ii> pq; n     for (int i = 1; i <= n; i++) { n       pq.push({-cnt[i], i}); n     } n     bool ok = 0; n     while (1) { n       if (pq.empty()) break; n       int c, x; n       tie(c, x) = pq.top(); n       c = -c; n       if (c != cnt[x]) { n         pq.pop(); n         continue; n       } n       if (c >= k) { n         vector<int> ans; n         while (!pq.empty()) { n           int c, x; n           tie(c, x) = pq.top(); n           c = -c; n           pq.pop(); n           if (c != cnt[x]) { n             continue; n           } n           ans.push_back(x); n         } n         printf( 1 %d n , (int)ans.size()); n         for (int i : ans) { n           printf( %d  , i); n         } n         printf(  n ); n         ok = 1; n         break; n       } n       pq.pop(); n       if (c == k - 1) { n         if (clique(x)) { n           printf( 2 n ); n           printf( %d , x); n           for (int i : adj[x]) n             if (!ta[i]) { n               printf(  %d , i); n             } n           printf(  n ); n           ok = 1; n           break; n         } n       } n       cnt[x] = 0; n       ta[x] = 1; n       for (int i : adj[x]) n         if (cnt[i]) { n           cnt[i]--; n           pq.push({-cnt[i], i}); n         } n     } n     if (!ok) { n       printf( -1 n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ara[1000]; n int rf[1000]; n int main() { n   int n, i, f, w, cnt = 0, m, r; n   char str[1000001]; n   scanf( %d %d , &n, &m); n   scanf( %s , str); n   for (i = 0; i < n; i++) { n     r = str[i]; n     ara[r] = ara[r] + 1; n   } n   for (i = 0; i < n; i++) { n     r = str[i]; n     if (rf[r] == 0) { n       cnt++; n       rf[r] = 1; n       if (cnt > m) { n         printf( YES ); n         return 0; n       } n     } n     ara[r] = ara[r] - 1; n     if (ara[r] == 0) { n       cnt--; n     } n   } n   printf( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const long long mint = 0x7fffffff; n const long long linf = 1000000000000000000LL; n const long long mod = 1000000007LL; n const double eps = 1e-3; n const int N = 1000020; n inline long long read() { n   long long x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n long long qpow(long long a, long long b, long long m) { n   long long ans = 1; n   while (b > 0) { n     if (b & 1) { n       ans = ans * a % m; n     } n     a = a * a % m; n     b >>= 1; n   } n   return ans; n } n int main(void) { n   for (int _ = read(); _ > 0; _--) { n     int n = read(); n     long long p = read(); n     vector<long long> k(n); n     long long b = 0; n     for (int i = 0; i < n; i++) k[i] = read(); n     sort(k.begin(), k.end(), greater<long long>()); n     long long q = 1; n     bool f = false; n     if (p == 1) { n       printf( %d n , n % 2 == 1 ? 1 : 0); n       continue; n     } n     long long now = k[0]; n     b = 1; n     int i = 1; n     for (; i < n; i++) { n       if (b == 0) n         now = k[i]; n       else n         while (now > k[i] && !f) { n           if (b >= n) { n             f = true; n             break; n           } n           b = b * p; n           now--; n         } n       if (f) { n         break; n       } else { n         if (b > 0) n           b--; n         else n           b++; n       } n     } n     long long ans = b % mod * qpow(p, now, mod) % mod; n     for (; i < n; i++) ans = (ans + (mod - 1) * qpow(p, k[i], mod) % mod) % mod; n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 5; n long long n; n long long a[N], b[N], c[N]; n bool check() { n   bool check = 0; n   for (long long i = 1; i <= n; i++) n     if (a[i] != b[i]) check = 1; n   bool check2 = 0; n   for (long long i = 1; i <= n; i++) n     if (a[i] != c[i]) check2 = 1; n   return check & check2; n } n void work() { n   for (long long i = 1; i <= n - 1; i++) { n     if (a[i] != a[i + 1]) { n       swap(a[i], a[i + 1]); n       if (check()) { n         cout << i <<     << i + 1; n         exit(0); n       } n       swap(a[i], a[i + 1]); n     } n   } n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n; n   for (long long i = 1; i <= n; i++) { n     cin >> a[i]; n     b[i] = a[i]; n     c[i] = b[i]; n   } n   sort(b + 1, b + n + 1); n   sort(c + 1, c + n + 1, greater<long long>()); n   work(); n   cout <<  -1 ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, dem; n string s; n void setup() {} n void solve() { n   cin >> n; n   cin >> s; n   if (n == 1) n     cout << s; n   else { n     for (int i = 0; i <= n - 1; ++i) dem += s[i] ==  0 ; n     cout << 1; n     for (int i = 1; i <= dem; ++i) cout << 0; n   } n } n int main() { n   setup(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long f[100], f0[100]; n int main() { n   int n, k, m; n   scanf( %d %d %d , &n, &k, &m); n   for (int i = 1; i < k; i++) f0[i] = 0, f[i] = 0; n   int tmp = 1; n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     f0[0] = 1; n     for (int j = 0; j < k; j++) n       if (f0[j] > 0) { n         for (int p = 0; p < 10; p++) { n           f[(p * tmp + j) % k] = (f[(p * tmp + j) % k] + f0[j]) % m; n         } n       } n     tmp = tmp * 10 % k; n     long long asd = 1; n     for (int j = i + 1; j < n - 1; j++) asd = asd * 10 % m; n     if (i + 1 < n) asd = (asd * 9) % m; n     ans = (ans + asd * (f[0] - 1) + m) % m; n     f[0] = 0; n     memcpy(f0, f, sizeof f0); n     memset(f, 0, sizeof f); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int n; n   cin >> n; n   long long int a[n]; n   for (long long int i = 0; i < n; i++) cin >> a[i]; n   long long int m; n   cin >> m; n   long long int num1[m], num2[m]; n   for (long long int i = 0; i < m; i++) { n     cin >> num1[i] >> num2[i]; n   } n   for (long long int i = 0; i < m; i++) { n     long long int x = num1[i] - 1; n     long long int y = num2[i]; n     if (x - 1 >= 0) { n       a[x - 1] += y - 1; n     } n     if (x + 1 < n) { n       a[x + 1] += a[x] - y; n     } n     a[x] = 0; n   } n   for (long long int i = 0; i < n; i++) cout << a[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e16; n const long long maxn = 128; n const long long maxlog = 60; n long long a, b; n string s, t; n long long dp[maxlog][maxn]; n long long ans; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> a >> b; n   cin >> s >> t; n   string S =   ; n   for (long long i = 100; i--; S += s) n     ; n   for (long long i = 0; i < s.size(); i++) { n     long long p1 = 0, p2 = i; n     for (; p1 < t.size(); p1++, p2++) { n       for (; p2 < S.size() and S[p2] != t[p1]; p2++) n         ; n       if (p2 == S.size()) break; n     } n     if (p1 == t.size()) n       dp[0][i] = p2 - i; n     else n       dp[0][i] = inf; n   } n   for (long long j = 1; j < maxlog; j++) n     for (long long i = 0; i < s.size(); i++) n       dp[j][i] = n           min(dp[j - 1][i] + dp[j - 1][(i + dp[j - 1][i]) % s.size()], inf); n   long long len = 0; n   for (long long j = maxlog; j--;) n     if (dp[j][len % s.size()] + len <= a * s.size()) n       ans |= 1ll << j, len = len + dp[j][len % s.size()]; n   cout << ans / b << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using vi = vector<int>; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n template <typename T> n using vec = vector<T>; n template <typename T> n using Prior = priority_queue<T>; n template <typename T> n using prior = priority_queue<T, vector<T>, greater<T>>; n const int INF = 1e9; n const int nINF = -1e9; n const ll llINF = 4 * 1e18; n const int MOD = 1e9 + 7; n ll& pmod(ll& a, ll b) { n   a = (a + b) % MOD; n   return a; n } n ll& pmod(ll& a, ll b, ll c) { n   a = (a + b) % c; n   return a; n } n ll& mmod(ll& a, ll b) { n   a = (a - b + MOD) % MOD; n   return a; n } n ll& mmod(ll& a, ll b, ll c) { n   a = (a - b + c) % c; n   return a; n } n ll& tmod(ll& a, ll b) { n   a = (a * b) % MOD; n   return a; n } n ll mul(ll a, ll b) { return (a * b) % MOD; } n ll mul(ll a, ll b, ll c) { return (a * b) % c; } n ll POW(ll a, ll b) { n   ll res = 1; n   do { n     if (b % 2) tmod(res, a); n     tmod(a, a); n   } while (b >>= 1); n   return res; n } n ll FAC(ll a) { n   ll res = 1; n   for (int i = 1; i <= a; i++) tmod(res, i); n   return res; n } n template <typename T1, typename T2> n ostream& operator<<(ostream& out, pair<T1, T2> a) { n   cout << a.first <<     << a.second; n   return out; n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0); n   int n; n   cin >> n; n   vec<int> _list(n); n   for (auto& i : _list) cin >> i; n   if (n % 2) { n     cout <<  YES n ; n     cout << n - 2 <<   n ; n     for (int i = 0; i < n - 2; i += 2) n       cout << i + 1 <<     << i + 2 <<     << i + 3 <<   n ; n     for (int i = 0; i < n - 3; i += 2) n       cout << i + 1 <<     << i + 2 <<     << n <<   n ; n   } else { n     int tmp = 0, idx = -1; n     for (int i = 0; i < n; i++) { n       tmp ^= _list[i]; n       if (_list[i]) idx = i; n     } n     if (tmp != 0) n       cout <<  NO n ; n     else { n       cout <<  YES n ; n       cout << n - 2 <<   n ; n       for (int i = 0; i < n - 2; i += 2) n         cout << i + 1 <<     << i + 2 <<     << i + 3 <<   n ; n       for (int i = 0; i < n - 3; i += 2) n         cout << i + 1 <<     << i + 2 <<     << n <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long int M = 100 + 5, SM = 600; n const long long int inf = 1e18; n const long long int lg = 18; n const long long int z = 26; n long long int dp[M][M][M]; n string dps[M][M][M]; n long long int x; n long long int nxt[M][z]; n long long int tt = 1; n long long int f[M]; n long long int insert(string s) { n   long long int p = 0; n   for (char c : s) { n     if (nxt[p][c -  A ] == 0) { n       nxt[p][c -  A ] = tt++; n     } n     p = nxt[p][c -  A ]; n   } n   return p; n } n int main() { n   string s, t, virus; n   cin >> s >> t; n   long long int n = ((long long int)s.size()); n   long long int m = ((long long int)t.size()); n   cin >> virus; n   long long int vi = insert(virus); n   { n     queue<long long int> q; n     for (long long int i = 0; i < 26; i++) { n       if (nxt[0][i] != 0) q.push(nxt[0][i]); n     } n     while (((long long int)q.size()) > 0) { n       long long int x = q.front(); n       q.pop(); n       for (long long int i = 0; i < 26; i++) { n         if (nxt[x][i] == 0) n           nxt[x][i] = nxt[f[x]][i]; n         else { n           f[nxt[x][i]] = nxt[f[x]][i]; n           q.push(nxt[x][i]); n         } n       } n     } n   } n   for (long long int i = 1; i < n + 1; i++) { n     for (long long int j = 1; j < m + 1; j++) { n       if (s[i - 1] == t[j - 1]) { n         for (long long int k = 0; k < tt; k++) { n           long long int d = nxt[k][s[i - 1] -  A ]; n           if (k == vi || d == vi) continue; n           if (dp[i][j][d] < dp[i - 1][j - 1][k] + 1) { n             dp[i][j][d] = dp[i - 1][j - 1][k] + 1; n             dps[i][j][d] = dps[i - 1][j - 1][k] + s[i - 1]; n           } n         } n       } n       for (long long int k = 0; k < tt; k++) { n         if (dp[i][j][k] < dp[i][j - 1][k]) { n           dp[i][j][k] = dp[i][j - 1][k]; n           dps[i][j][k] = dps[i][j - 1][k]; n         } n         if (dp[i][j][k] < dp[i - 1][j][k]) { n           dp[i][j][k] = dp[i - 1][j][k]; n           dps[i][j][k] = dps[i - 1][j][k]; n         } n       } n     } n   } n   long long int res = 0; n   string pes =   ; n   for (long long int k = 0; k < tt; k++) { n     if (k != vi) { n       if (res < dp[n][m][k]) { n         res = dp[n][m][k]; n         pes = dps[n][m][k]; n       } n     } n   } n   cout << (pes ==    ?  0  : pes); n } n


#include <bits/stdc++.h> n using namespace std; n void candy(int x, int y) { n   int d = 1; n   bool flag = true; n   while (x >= 0 && y >= 0) { n     if (flag) { n       x = x - d; n       d++; n       flag = false; n     } else { n       y = y - d; n       d++; n       flag = true; n     } n     if (flag == true && x < d) { n       cout <<  Vladik  << endl; n       break; n     } else if (flag == false && y < d) { n       cout <<  Valera  << endl; n       break; n     } n   } n } n int main(int argc, char** argv) { n   int a, b; n   cin >> a >> b; n   candy(a, b); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, r1, c1, r2, c2; n char a[120][120]; n int main() { n   cin >> n >> m; n   bool sign = true; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) cin >> a[i][j]; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (a[i][j] ==  B ) { n         if (sign) { n           r1 = i; n           c1 = j; n           sign = false; n         } n         r2 = i; n         c2 = j; n       } n   cout << (r1 + r2) / 2 <<     << (c1 + c2) / 2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n const long long MOD = 1e9 + 7; n const int INF = 1e9; n template <class T> n inline T gcd(T a, T b) { n   if (a < 0) return gcd(-a, b); n   if (b < 0) return gcd(a, -b); n   return (b == 0) ? a : gcd(b, a % b); n } n template <class T> n inline T lcm(T a, T b) { n   if (a < 0) return lcm(-a, b); n   if (b < 0) return lcm(a, -b); n   return a * (b / gcd(a, b)); n } n int a, b; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> a >> b; n   int t = gcd(a, b); n   a /= t; n   b /= t; n   if (abs(a - b) <= 1) return puts( Equal ); n   puts(a < b ?  Dasha  :  Masha ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {-1, 0, 1, 0}; n int dy[] = {0, 1, 0, -1}; n vector<int> f, col, wlk; n int Find(int a) { n   if (f[a] == a) return a; n   int fa = Find(f[a]); n   f[a] = fa; n   return fa; n } n void Union(int a, int b) { n   int fa = Find(a); n   int fb = Find(b); n   if (fa == fb) return; n   wlk[fa] += wlk[fb]; n   f[fb] = f[fa]; n } n int main() { n   ios_base::sync_with_stdio(0); n   int n, m; n   cin >> n >> m; n   vector<vector<int> > pan(3 * n, vector<int>(3 * m, 0)); n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) cin >> pan[n + i][m + j]; n   vector<vector<int> > sym(3 * n, vector<int>(3 * m, -1)); n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) cin >> sym[n + i][m + j]; n   int x, y; n   cin >> x >> y; n   x = x - 1 + n; n   y = y - 1 + m; n   vector<vector<int> > spi(3 * n, vector<int>(3 * m)); n   spi[x][y] = 0; n   int nr = 1, ileMam = 1, d = 1; n   int akt_x = x, akt_y = y; n   while (ileMam < 9 * n * m) { n     for (int kier = 0; kier < 4; ++kier) { n       for (int i = 0; i < d; ++i) { n         akt_x += dx[kier]; n         akt_y += dy[kier]; n         if (akt_x >= 0 && akt_x < 3 * n && akt_y >= 0 && akt_y < 3 * m) { n           spi[akt_x][akt_y] = nr; n           ++ileMam; n         } n         ++nr; n       } n       if (kier == 1 || kier == 3) ++d; n     } n   } n   map<int, vector<pair<int, pair<int, int> > > > M; n   map<int, pair<int, int> > gdzieCol; n   for (int i = 0; i < 3 * n; ++i) { n     for (int j = 0; j < 3 * m; ++j) { n       gdzieCol[pan[i][j]] = make_pair(i, j); n       M[pan[i][j]].push_back(make_pair(-1, make_pair(i, j))); n       int H = i * 3 * m + j; n       f.push_back(H); n       col.push_back(pan[i][j]); n       wlk.push_back(1); n       if (M.find(pan[i][j]) != M.end()) { n         int Hroot = (M[pan[i][j]][0]).second.first * 3 * m + n                     (M[pan[i][j]][0]).second.second; n         Union(Hroot, H); n       } n     } n   } n   long long odp = 0; n   queue<pair<int, int> > Q; n   Q.push(make_pair(x, y)); n   while (!Q.empty()) { n     pair<int, int> akt = Q.front(); n     Q.pop(); n     int colPan = col[Find(akt.first * 3 * m + akt.second)]; n     int colSym = sym[akt.first][akt.second]; n     if (colPan == 0 || colPan == colSym) continue; n     if (M[colPan].size() != 0) { n       for (int i = 0; i < M[colPan].size(); ++i) { n         int wx = akt.first - x; n         int wy = akt.second - y; n         wx *= -1; n         wy *= -1; n         M[colPan][i].first = spi[M[colPan][i].second.first + wx] n                                 [M[colPan][i].second.second + wy]; n       } n       sort(M[colPan].begin(), M[colPan].end()); n       for (int i = 0; i < M[colPan].size(); ++i) n         if ((M[colPan][i].second.first != x || n              M[colPan][i].second.second != y) && n             sym[M[colPan][i].second.first][M[colPan][i].second.second] != -1) n           Q.push(M[colPan][i].second); n       M[colPan].clear(); n     } n     odp += wlk[Find(akt.first * 3 * m + akt.second)]; n     if (gdzieCol.find(colSym) != gdzieCol.end() && gdzieCol[colSym].first != -1) n       Union(gdzieCol[colSym].first * 3 * m + gdzieCol[colSym].second, n             akt.first * 3 * m + akt.second); n     else { n       col[Find(akt.first * 3 * m + akt.second)] = colSym; n       gdzieCol[colSym] = akt; n     } n     gdzieCol[colPan] = make_pair(-1, -1); n   } n   cout << odp; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long A[200010], sz[200010], v[200010][11]; n vector<long long> l[11]; n class Timer { n  private: n   using clock_t = std::chrono::high_resolution_clock; n   using second_t = std::chrono::duration<double, std::ratio<1> >; n   std::chrono::time_point<clock_t> m_beg; n  n  public: n   Timer() : m_beg(clock_t::now()) {} n   void reset() { m_beg = clock_t::now(); } n   double elapsed() const { n     return std::chrono::duration_cast<second_t>(clock_t::now() - m_beg).count(); n   } n } T; n long long cnt(long long x) { n   long long r = 0; n   while (x) r++, x /= 10; n   return r; n } n void precompute(long long n, long long k) { n   for (long long i = 0; i < n; i++) { n     sz[i] = cnt(A[i]); n     long long x = A[i]; n     v[i][0] = x; n     for (long long j = 1; j < 11; j++) x = (x * 10) % k, v[i][j] = x; n     A[i] %= k; n   } n } n void put(long long i) { n   l[sz[i]].push_back(A[i]); n   return; n } n long long binary(long long T, long long mod) { n   long long L = 0, R = (long long)l[T].size() - 1, ans1 = -1; n   while (L <= R) { n     long long mid = (L + R) >> 1; n     if (l[T][mid] == mod) n       ans1 = mid, R = mid - 1; n     else if (l[T][mid] > mod) n       R = mid - 1; n     else n       L = mid + 1; n   } n   if (ans1 == -1) return 0; n   L = 0, R = (long long)l[T].size() - 1; n   long long ans2 = -1; n   while (L <= R) { n     long long mid = (L + R) >> 1; n     if (l[T][mid] == mod) n       ans2 = mid, L = mid + 1; n     else if (l[T][mid] > mod) n       R = mid - 1; n     else n       L = mid + 1; n   } n   return (ans2 - ans1 + 1); n } n long long get(long long idx, long long k) { n   long long ans = 0LL; n   for (long long i = 1; i < 11; i++) { n     long long x = v[idx][i]; n     ans += binary(i, (k - x) % k); n     if (i == sz[idx] && (v[idx][i] + A[idx]) % k == 0) ans--; n   } n   return ans; n } n signed main() { n   long long n, k; n   scanf( %I64d %I64d , &n, &k); n   for (long long i = 0; i < n; i++) scanf( %I64d , &A[i]); n   precompute(n, k); n   for (long long i = 0; i < n; i++) put(i); n   for (long long i = 1; i < 11; i++) sort(l[i].begin(), l[i].end()); n   long long ans = 0; n   for (long long i = 0; i < n; i++) ans += get(i, k); n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   char c = getchar(); n   int x = 0, f = 1; n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = -1; n   for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ; n   return x * f; n } n const int MAXN = 3e2 + 5; n const int MAXQ = 2e6 + 5; n struct Query { n   int x, y, nc, oc; n } q[MAXQ]; n int fa[MAXQ << 1], ans[MAXQ], a[MAXN][MAXN], id[MAXN][MAXN]; n int N, M, Q, Num, cnt; n inline int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } n inline int mrg(int x, int y) { n   if (find(x) != find(y)) { n     fa[fa[x]] = fa[y]; n     Num--; n   } n } n inline void check(int x, int y) { n   if (a[x][y] == a[x - 1][y]) mrg(id[x][y], id[x - 1][y]); n   if (a[x][y] == a[x + 1][y]) mrg(id[x][y], id[x + 1][y]); n   if (a[x][y] == a[x][y - 1]) mrg(id[x][y], id[x][y - 1]); n   if (a[x][y] == a[x][y + 1]) mrg(id[x][y], id[x][y + 1]); n } n inline void solveTo() { n   for (int i = 1; i <= Q; i++) { n     if (q[i].oc == q[i].nc) continue; n     int x = q[i].x, y = q[i].y; n     Num = 1; n     a[x][y] = q[i].nc; n     id[x][y] = ++cnt; n     fa[cnt] = cnt; n     check(x, y); n     ans[i] += Num; n   } n } n inline void solveBack() { n   for (int i = Q; i; i--) { n     if (q[i].oc == q[i].nc) continue; n     int x = q[i].x, y = q[i].y; n     Num = 1; n     a[x][y] = q[i].oc; n     id[x][y] = ++cnt; n     fa[cnt] = cnt; n     check(x, y); n     ans[i] -= Num; n   } n } n int main() { n   N = read(); n   M = read(); n   Q = read(); n   for (int i = 1; i <= Q; i++) { n     int x = read(), y = read(), z = read(); n     q[i] = {x, y, z, a[x][y]}; n     a[x][y] = z; n   } n   ans[0] = 1; n   cnt = 0; n   memset(a, -1, sizeof(a)); n   for (int i = 1; i <= N; i++) n     for (int j = 1; j <= M; j++) a[i][j] = 0; n   solveTo(); n   memset(fa, cnt = 0, sizeof(fa)); n   for (int i = 1; i <= N; i++) n     for (int j = 1; j <= M; j++) { n       id[i][j] = ++cnt; n       fa[cnt] = cnt; n     } n   for (int i = 1; i <= N; i++) n     for (int j = 1; j <= M; j++) check(i, j); n   solveBack(); n   for (int i = 1; i <= Q; i++) { n     ans[i] += ans[i - 1]; n     printf( %d n , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); n long long int myRand(long long int B) { return (unsigned long long)rng() % B; } n struct UnionFind { n   vector<int> par, num; n   vector<bool> done; n   UnionFind(int n) : par(n), num(n, 1), done(n, false) { n     iota(par.begin(), par.end(), 0); n   } n   int find(int v) { return (par[v] == v) ? v : (par[v] = find(par[v])); } n   void unite(int u, int v) { n     u = find(u), v = find(v); n     if (u == v) return; n     if (num[u] < num[v]) swap(u, v); n     num[u] += num[v]; n     par[v] = u; n     done[u] = done[u] | done[v]; n   } n   bool same(int u, int v) { return find(u) == find(v); } n   bool ispar(int v) { return v = find(v); } n   int size(int v) { return num[find(v)]; } n }; n const int BS = 524288; n int CNT, rc; n char inb[BS]; n inline char gchar(void) { n   if (CNT % BS == 0) { n     CNT = 0; n     rc = fread(inb, 1, BS, stdin); n   } n   return CNT < rc ? inb[CNT++] : 0; n } n inline int _readint(int *ptr) { n   int n = 0, c = 0; n   *ptr = 0; n   while (1) { n     char read = gchar(); n     if (read >=  0  && read <=  9 ) n       read -=  0 , n *= 10, n += read; n     else { n       *ptr = n; n       return n == 0 ? 0 : !c; n     } n     c++; n   } n   *ptr = n; n   return 0; n } n inline int readint(void) { n   int ptr; n   while (_readint(&ptr)) n     ; n   return ptr; n } n int main() { n   cin.tie(nullptr); n   ios::sync_with_stdio(false); n   int n, m, q; n   cin >> n >> m >> q; n   vector<int> x(m), y(m), w(m); n   vector<pair<int, int>> v(m); n   for (int i = 0; i < m; i++) { n     cin >> x[i] >> y[i] >> w[i]; n     x[i]--; n     y[i]--; n     v[i] = make_pair(w[i], i); n   } n   sort(v.rbegin(), v.rend()); n   while (q--) { n     int l, r; n     cin >> l >> r; n     l--; n     int res = -1; n     UnionFind uf(2 * n); n     for (int i = 0; i < v.size(); i++) { n       if (v[i].second < l || r <= v[i].second) continue; n       int id = v[i].second; n       if (uf.same(x[id], y[id])) { n         res = v[i].first; n         break; n       } else { n         uf.unite(x[id], y[id] + n); n         uf.unite(x[id] + n, y[id]); n       } n     } n     printf( %d n , res); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n string S; n long long dp[222][222]; n long long GetAnswer(int pos, int numdigs) { n   if (pos == S.size()) { n     if (numdigs == 0) n       return 0; n     else n       return -1; n   } n   if (numdigs == 0) return -1; n   if (dp[pos][numdigs] != -2) return dp[pos][numdigs]; n   long long ret = -1; n   long long x = 0; n   if ((numdigs - 1.0) * log(n) > log(1400000000000000000ll)) n     return dp[pos][numdigs] = -1; n   long long npow = 1; n   for (int j = 0; j < numdigs - 1; j++) npow *= n; n   if (npow > 1000000000000000000ll) return dp[pos][numdigs] = -1; n   for (int j = pos; j < S.size(); j++) { n     if (S[pos] ==  0  && j > pos) break; n     x = x * 10 + S[j] -  0 ; n     if (x >= n) break; n     long long curr = GetAnswer(j + 1, numdigs - 1); n     if (curr == -1 || curr > 1000000000000000000ll || n         x > 1000000000000000000ll / npow) n       continue; n     if (log(x) + (numdigs - 1.0) * log(n) > log(1400000000000000000ll - curr)) n       continue; n     curr += x * npow; n     if (curr <= 1000000000000000000ll && (ret == -1 || curr < ret)) ret = curr; n   } n   return dp[pos][numdigs] = ret; n } n int main() { n   cin >> n >> S; n   long long answer = -1; n   for (int j = 0; j < 222; j++) n     for (int k = 0; k < 222; k++) dp[j][k] = -2; n   for (int numdigs = 1; numdigs <= 61; numdigs++) { n     long long curr = GetAnswer(0, numdigs); n     if (curr == -1) continue; n     if (answer == -1 || (curr < answer)) answer = curr; n   } n   cout << answer << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   int n, h; n   cin >> n >> h; n   vector<int> v(n); n   for (int i = 0; i < n; i++) cin >> v[i]; n   int lo = 0; n   int hi = n - 1; n   int ans, mid; n   while (lo <= hi) { n     mid = lo + (hi - lo) / 2; n     vector<int> a(mid + 1); n     for (int i = 0; i < mid + 1; i++) a[i] = v[i]; n     sort(a.begin(), a.end()); n     int c = 0; n     int j = mid; n     int leftheight = h; n     while (j >= 0 && leftheight > 0) { n       if (a[j] <= leftheight) { n         leftheight -= a[j]; n         if (mid % 2 == 0 && j == 0) { n           c++; n           break; n         } else { n           j -= 2; n           c += 2; n         } n       } else n         break; n     } n     if (c >= (mid + 1)) { n       lo = mid + 1; n       ans = mid; n     } else n       hi = mid - 1; n   } n   cout << ans + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, flag, ln, prv = -1; n char in[100010][20], pr[100010][20]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %s , in[i]); n     ln = strlen(in[i]); n     flag = 0; n     if (ln < prv) { n       printf( NO n ); n       return 0; n     } else if (i == 0 || ln > prv) { n       for (int j = 0; j < ln; j++) { n         if (j == 0 && in[i][j] ==  ? ) n           pr[i][j] =  1 ; n         else if (in[i][j] ==  ? ) n           pr[i][j] =  0 ; n         else n           pr[i][j] = in[i][j]; n       } n     } else { n       for (int j = 0; j < ln; j++) { n         if (in[i][j] ==  ? ) { n           if (flag == 0) n             pr[i][j] = pr[i - 1][j]; n           else n             pr[i][j] =  0 ; n         } else if (in[i][j] > pr[i - 1][j]) { n           pr[i][j] = in[i][j]; n           flag = 1; n         } else if (in[i][j] < pr[i - 1][j] && flag == 0) { n           for (int x = j - 1; x >= 0; x--) { n             if (in[i][x] ==  ?  && pr[i][x] <  9 ) { n               pr[i][x]++; n               for (int z = x + 1; z < j; z++) n                 if (in[i][z] ==  ? ) pr[i][z] =  0 ; n               flag = 1; n               break; n             } n           } n           if (flag == 0) { n             printf( NO n ); n             return 0; n           } n           pr[i][j] = in[i][j]; n         } else n           pr[i][j] = in[i][j]; n       } n       if (flag == 0) { n         for (int j = ln - 1; j >= 0; j--) { n           if (in[i][j] ==  ?  && pr[i][j] <  9 ) { n             pr[i][j]++; n             for (int z = j + 1; z < ln; z++) n               if (in[i][z] ==  ? ) pr[i][z] =  0 ; n             flag = 1; n             break; n           } n         } n         if (flag == 0) { n           printf( NO n ); n           return 0; n         } n       } n     } n     prv = ln; n   } n   printf( YES n ); n   for (int i = 0; i < n; i++) printf( %s n , pr[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pi = pair<int, int>; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout << setprecision(15) << fixed; n   int n; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a.begin(), a.end()); n   int x = a[0]; n   for (int i = 1; i < n; i++) { n     if (a[i] != x) { n       if (a[i] < 30) { n         ll y = (1ll << x) - 1; n         if (y * y > (1ll << a[i]) - 1) { n           cout <<  YES n ; n           return 0; n         } n       } else if (2 * x > a[i]) { n         cout <<  YES n ; n         return 0; n       } n       x = a[i]; n     } n   } n   cout <<  NO n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXL = 5e5 + 1e3; n int n, t; n char dat[MAXL + 1]; n int main() { n   int n; n   while (scanf( %d%d%s , &n, &t, dat) >= 1) { n     int hl = n, hr = -1, hcnt = 0; n     vector<int> shops; n     for (int i = 0; i < n; i++) n       if (dat[i] ==  H ) { n         hl = min(hl, i); n         hr = max(hr, i); n         hcnt++; n       } else if (dat[i] ==  S ) { n         shops.push_back(i); n       } n     t--; n     if (hr > t) n       printf( -1 n ); n     else { n       int L = -1, R = hcnt; n       while (L + 1 < R) { n         int M = (L + R) / 2; n         assert(M < hcnt); n         bool good = false; n         int rem = hcnt - M; n         if (rem <= ((int)(shops).size())) { n           int maxpos = max(hr, shops[rem - 1]); n           int ctim = maxpos; n           int btim = 2e9; n           int bal = M; n           for (int i = 0; i <= maxpos; i++) { n             if (dat[i] ==  H ) n               bal--; n             else if (dat[i] ==  S ) n               bal++; n             if (bal < 0) { n               btim = min(btim, ctim + maxpos - i); n               ctim += 2; n             } n           } n           btim = min(btim, ctim); n           if (btim <= t) good = true; n         } n         if (good) n           R = M; n         else n           L = M; n       } n       printf( %d n , R); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 3e5 + 5; n const int INF = 1e9; n int N, p[MAXN], tag[MAXN << 2]; n long long ans; n struct node { n   int mn, cnt; n   node(int mn = 0, int cnt = 0) : mn(mn), cnt(cnt) {} n   friend node operator+(const node& l, const node& r) { n     if (l.mn < r.mn) n       return l; n     else if (l.mn > r.mn) n       return r; n     else n       return node(l.mn, l.cnt + r.cnt); n   } n } t[MAXN << 2]; n void pushup(int o) { t[o] = t[(o << 1)] + t[(o << 1 | 1)]; } n void add(int o, int k) { t[o].mn += k, tag[o] += k; } n void pushdown(int o) { n   if (tag[o]) add((o << 1), tag[o]), add((o << 1 | 1), tag[o]), tag[o] = 0; n } n void build(int o, int l, int r) { n   if (l == r) n     t[o] = node(0, 1); n   else n     build((o << 1), l, ((l + r) >> 1)), n         build((o << 1 | 1), ((l + r) >> 1) + 1, r), pushup(o); n } n void ins(int o, int l, int r, int L, int R, int K) { n   if (r < L || l > R) return; n   if (L <= l && r <= R) return add(o, K); n   pushdown(o); n   ins((o << 1), l, ((l + r) >> 1), L, R, K), n       ins((o << 1 | 1), ((l + r) >> 1) + 1, r, L, R, K); n   pushup(o); n } n node qry(int o, int l, int r, int L, int R) { n   if (r < L || l > R) return node(INF, 0); n   if (L <= l && r <= R) return t[o]; n   pushdown(o); n   return qry((o << 1), l, ((l + r) >> 1), L, R) + n          qry((o << 1 | 1), ((l + r) >> 1) + 1, r, L, R); n } n int s1[MAXN], l1, s2[MAXN], l2; n int main() { n   scanf( %d , &N); n   int x, y; n   for (int i = 1; i <= N; ++i) scanf( %d%d , &x, &y), p[x] = y; n   build(1, 1, N); n   s1[0] = s2[0] = N + 1; n   for (int i = N; i >= 1; --i) { n     while (l1 && p[s1[l1]] < p[i]) { n       ins(1, 1, N, s1[l1], s1[l1 - 1] - 1, p[i] - p[s1[l1]]), --l1; n     } n     s1[++l1] = i, ins(1, 1, N, i, i, p[i]); n     while (l2 && p[s2[l2]] > p[i]) { n       ins(1, 1, N, s2[l2], s2[l2 - 1] - 1, p[s2[l2]] - p[i]), --l2; n     } n     s2[++l2] = i, ins(1, 1, N, i, i, -p[i]); n     if (i < N) ins(1, 1, N, i + 1, N, -1); n     ans += qry(1, 1, N, i, N).cnt; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, q, a[300010], f[300010], ans[300010], g[300010]; n struct edge { n   long long v, next; n } e[300010 << 1]; n long long eid, p[300010]; n void insert(long long u, long long v) { n   e[++eid] = {v, p[u]}; n   p[u] = eid; n } n long long dep[300010], cnt, c[300010], d[300010], siz[300010]; n pair<long long, long long> b[300010]; n void dfs(long long u, long long f) { n   siz[u] = 1; n   c[u] = ++cnt, d[cnt] = u; n   dep[u] = dep[f] + 1; n   b[u] = make_pair(dep[u], c[u]); n   for (int i = p[u]; i; i = e[i].next) { n     long long v = e[i].v; n     if (v == f) continue; n     dfs(v, u); n     siz[u] += siz[v]; n   } n } n void dfs1(long long u, long long f) { n   g[u] += g[f]; n   for (int i = p[u]; i; i = e[i].next) { n     long long v = e[i].v; n     if (v == f) continue; n     dfs1(v, u); n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i < n; ++i) { n     long long u, v; n     cin >> u >> v; n     insert(u, v); n     insert(v, u); n   } n   dfs(1, 0); n   sort(b + 1, b + n + 1); n   cin >> q; n   while (q--) { n     long long v, d, x; n     cin >> v >> d >> x; n     a[c[v]] += x, a[c[v] + siz[v]] -= x; n     long long l = n         lower_bound(b + 1, b + n + 1, make_pair(dep[v] + d + 1, c[v])) - b; n     long long r = lower_bound(b + 1, b + n + 1, n                               make_pair(dep[v] + d + 1, c[v] + siz[v])) - n                   b; n     f[l] -= x, f[r] += x; n   } n   for (int i = 1; i <= n; ++i) a[i] += a[i - 1], f[i] += f[i - 1]; n   for (int i = 1; i <= n; ++i) g[d[b[i].second]] = f[i]; n   dfs1(1, 0); n   for (int i = 1; i <= n; ++i) ans[d[i]] += a[i]; n   for (int i = 1; i <= n; ++i) cout << ans[i] + g[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s1, s2; n   cin >> s1; n   cin >> s2; n   int arr[27] = {}; n   int a[27] = {}; n   string s =   ; n   for (int i = 0; i < s1.length(); i++) { n     arr[int(s1[i]) - 97]++; n   } n   int count1 = 0; n   for (int i = 0; i < s2.length(); i++) { n     if (arr[int(s2[i]) - 97] == 0) { n       cout << -1; n       count1++; n       break; n     } else { n       if (a[int(s2[i]) - 97] == 0) { n         s += s2[i]; n       } n       a[int(s2[i]) - 97]++; n     } n   } n   long long int sum = 0; n   if (count1 == 0) { n     for (int i = 0; i < s.length(); i++) { n       if (a[int(s[i]) - 97] <= arr[int(s[i]) - 97]) { n         sum += a[int(s[i]) - 97]; n       } else { n         sum += arr[int(s[i]) - 97]; n       } n     } n     cout << sum << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve(int a[], int n) { n   int cnt = 0, pos = 0; n   while (pos < n) { n     cnt++; n     int mx = pos; n     while (pos < n && pos <= mx) { n       mx = max(mx, a[pos]); n       pos++; n     } n   } n   cout << cnt << endl; n } n int main() { n   int n; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     a[i]--; n   } n   solve(a, n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T BigMod(T b, T p, T m) { n   if (p == 0) return 1; n   if (p % 2 == 0) { n     T s = BigMod(b, p / 2, m); n     return ((s % m) * (s % m)) % m; n   } n   return ((b % m) * (BigMod(b, p - 1, m) % m)) % m; n } n template <typename T> n T ModInv(T b, T m) { n   return BigMod(b, m - 2, m); n } n template <typename T> n T in() { n   char ch; n   T n = 0; n   bool ng = false; n   while (1) { n     ch = getchar(); n     if (ch ==  - ) { n       ng = true; n       ch = getchar(); n       break; n     } n     if (ch >=  0  && ch <=  9 ) break; n   } n   while (1) { n     n = n * 10 + (ch -  0 ); n     ch = getchar(); n     if (ch <  0  || ch >  9 ) break; n   } n   return (ng ? -n : n); n } n template <typename T> n T POW(T B, T printf) { n   if (printf == 0) return 1; n   if (printf & 1) n     return B * POW(B, printf - 1); n   else n     return (POW(B, printf / 2) * POW(B, printf / 2)); n } n template <typename T> n T Bigmod(T b, T p, T m) { n   if (p == 0) n     return 1; n   else if (!(p & 1)) n     return (Bigmod(b, p / 2, m) * Bigmod(b, p / 2, m)) % m; n   else n     return ((b % m) * Bigmod(b, p - 1, m)) % m; n } n template <typename T> n T Dis(T x1, T y1, T x2, T y2) { n   return sqrt((x1 - x2 * x1 - x2) + (y1 - y2 * y1 - y2)); n } n template <typename T> n T Angle(T x1, T y1, T x2, T y2) { n   return atan(double(y1 - y2) / double(x1 - x2)); n } n template <typename T> n T DIFF(T a, T b) { n   T d = a - b; n   if (d < 0) n     return -d; n   else n     return d; n } n template <typename T> n T ABS(T a) { n   if (a < 0) n     return -a; n   else n     return a; n } n template <typename T> n T gcd(T a, T b) { n   if (a < 0) return gcd(-a, b); n   if (b < 0) return gcd(a, -b); n   return (b == 0) ? a : gcd(b, a % b); n } n template <typename T> n T lcm(T a, T b) { n   if (a < 0) return lcm(-a, b); n   if (b < 0) return lcm(a, -b); n   return a * (b / gcd(a, b)); n } n template <typename T> n T euclide(T a, T b, T &x, T &y) { n   if (a < 0) { n     T d = euclide(-a, b, x, y); n     x = -x; n     return d; n   } n   if (b < 0) { n     T d = euclide(a, -b, x, y); n     y = -y; n     return d; n   } n   if (b == 0) { n     x = 1; n     y = 0; n     return a; n   } else { n     T d = euclide(b, a % b, x, y); n     T t = x; n     x = y; n     y = t - (a / b) * y; n     return d; n   } n } n template <typename T> n void ia(T a[], int n) { n   for (int i = 0; i < n; i++) cin >> a[i]; n } n template <typename T> n void pa(T a[], int n) { n   for (int i = 0; i < n - 1; i++) cout << a[i] <<    ; n   cout << a[n - 1] << endl; n } n template <typename T> n long long int isLeft(T a, T b, T c) { n   return (a.x - b.x) * (b.y - c.y) - (b.x - c.x) * (a.y - b.y); n } n int Set(int N, int pos) { return N = N | (1 << pos); } n int Reset(int N, int pos) { return N = N & ~(1 << pos); } n bool Check(int N, int pos) { return (bool)(N & (1 << pos)); } n template <class T, class first> n inline T togglebit(T a, first i) { n   T t = 1; n   return (a ^ (t << i)); n } n int toInt(string s) { n   int sm; n   stringstream ss(s); n   ss >> sm; n   return sm; n } n int toLlint(string s) { n   long long int sm; n   stringstream ss(s); n   ss >> sm; n   return sm; n } n int cdigittoint(char ch) { return ch -  0 ; } n bool isVowel(char ch) { n   ch = toupper(ch); n   if (ch ==  A  || ch ==  U  || ch ==  I  || ch ==  O  || ch ==  E ) n     return true; n   return false; n } n bool isConst(char ch) { n   if (isalpha(ch) && !isVowel(ch)) return true; n   return false; n } n double DEG(double x) { return (180.0 * x) / (2.0 * acos(0.0)); } n double RAD(double x) { return (x * (double)2.0 * acos(0.0)) / (180.0); } n struct Node { n   int cnt; n   Node *l, *r; n   Node() { n     cnt = 0; n     l = r = NULL; n   } n }; n vector<int> adj[100007]; n vector<int> ppl[100007]; n Node Tree[18 * 100007]; n int avail; n Node *Roots[100007]; n int L[100007], T[100007]; n int PP[100007][20]; n Node *Update(Node *idx, int l, int r, int pos) { n   Node *u = &Tree[++avail]; n   if (idx != NULL) *u = *idx; n   if (l == r) { n     u->cnt++; n     return u; n   } n   u->cnt++; n   if (pos <= ((l + r) >> 1)) n     u->l = Update(u->l, l, ((l + r) >> 1), pos); n   else n     u->r = Update(u->r, ((l + r) >> 1) + 1, r, pos); n   return u; n } n int Query(Node *u, Node *v, Node *p, Node *fp, int l, int r, int k) { n   if (l == r) return l; n   int sum = ((u ? u->l : NULL) ? (u ? u->l : NULL)->cnt : 0) + n             ((v ? v->l : NULL) ? (v ? v->l : NULL)->cnt : 0) - n             ((p ? p->l : NULL) ? (p ? p->l : NULL)->cnt : 0) - n             ((fp ? fp->l : NULL) ? (fp ? fp->l : NULL)->cnt : 0); n   if (sum >= k) { n     return Query((u ? u->l : NULL), (v ? v->l : NULL), (p ? p->l : NULL), n                  (fp ? fp->l : NULL), l, ((l + r) >> 1), k); n   } else { n     return Query((u ? u->r : NULL), (v ? v->r : NULL), (p ? p->r : NULL), n                  (fp ? fp->r : NULL), ((l + r) >> 1) + 1, r, k - sum); n   } n } n void Bfs(int n, int m) { n   queue<int> q; n   q.push(1); n   q.push(0); n   q.push(0); n   Roots[0] = NULL; n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     int p = q.front(); n     q.pop(); n     int l = q.front(); n     q.pop(); n     L[u] = l; n     T[u] = p; n     Roots[u] = Roots[p]; n     for (int i = 0; i < (int)ppl[u].size(); i++) { n       int x = ppl[u][i]; n       Roots[u] = Update(Roots[u], 1, m, x); n     } n     for (int i = 0; i < (int)adj[u].size(); i++) { n       int v = adj[u][i]; n       if (v != p) { n         q.push(v); n         q.push(u); n         q.push(l + 1); n       } n     } n   } n } n void Sparse(int n) { n   for (int i = 1; i <= n; i++) PP[i][0] = T[i]; n   for (int j = 1; (1 << j) < n; j++) { n     for (int i = 1; i <= n; i++) { n       if (PP[i][j - 1]) PP[i][j] = PP[PP[i][j - 1]][j - 1]; n     } n   } n } n int LCA(int p, int q) { n   if (L[p] < L[q]) swap(p, q); n   int b; n   for (b = 1; (1 << b) <= L[p]; b++) n     ; n   b--; n   for (int i = b; i >= 0; i--) { n     if (L[p] - (1 << i) >= L[q]) p = PP[p][i]; n   } n   if (p == q) return p; n   for (int i = b; i >= 0; i--) { n     if (PP[p][i] && PP[p][i] != PP[q][i]) { n       p = PP[p][i]; n       q = PP[q][i]; n     } n   } n   return T[p]; n } n int main() { n   int n, m, q; n   scanf( %d%d%d , &n, &m, &q); 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   for (int i = 1; i <= m; i++) { n     int(x); n     scanf( %d , &x); n     ppl[x].push_back(i); n   } n   Roots[0] = NULL; n   Bfs(n, m); n   Sparse(n); n   while (q--) { n     int u, v, a; n     scanf( %d%d%d , &u, &v, &a); n     int lca = LCA(u, v); n     int cnt = (Roots[u] ? Roots[u]->cnt : 0) + (Roots[v] ? Roots[v]->cnt : 0) - n               (Roots[lca] ? Roots[lca]->cnt : 0) - n               (Roots[T[lca]] ? Roots[T[lca]]->cnt : 0); n     int k = min(cnt, a); n     vector<int> xx; n     for (int i = 1; i <= k; i++) { n       int x = Query(Roots[u], Roots[v], Roots[lca], Roots[T[lca]], 1, m, i); n       xx.push_back(x); n     } n     printf( %d , (int)xx.size()); n     for (int i = 0; i < (int)xx.size(); i++) printf(  %d , xx[i]); n     printf(  n ); n     ; n   } n   return 0; n } n


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


#include <bits/stdc++.h> n struct DSU { n   long long M[400005]; n   DSU(long long n) { n     for (long long i = 0; i < n; i++) { n       M[i] = i; n     } n   } n   long long find(long long x) { n     while (M[x] != x) { n       long long p = M[x]; n       M[x] = M[p]; n       x = p; n     } n     return x; n   } n   bool join(long long x, long long y) { n     long long xr = find(x); n     long long yr = find(y); n     if (xr == yr) { n       return false; n     } else { n       M[xr] = yr; n       return true; n     } n   } n }; n using namespace std; n int main() { n   long long n, m, q; n   cin >> n >> m >> q; n   DSU dsu(n + m); n   long long z = n + m - 1; n   for (long long i = 0; i < q; i++) { n     long long r, c; n     cin >> r >> c; n     r--; n     c--; n     if (dsu.join(r, c + n)) z--; n   } n   cout << z; n } n


#include <bits/stdc++.h> n const int N = 2e5; n using namespace std; n const int SIZE = 26; n int tot = 0, sub = 0; n struct node { n   node* next[SIZE]; n   int strings; n   int tin, tout; n   node() { n     for (int i = 0; i < SIZE; i++) { n       next[i] = nullptr; n     } n     tin = 0; n     tout = 0; n     strings = 0; n   } n }; n void add(struct node* root, string key) { n   struct node* cur_v = root; n   for (size_t i = 0; i < key.length(); i++) { n     char c = key[i]; n     if (cur_v->next[c -  a ] == nullptr) { n       cur_v->next[c -  a ] = new node(); n     } n     cur_v = cur_v->next[c -  a ]; n   } n   cur_v->strings++; n } n int t[4 * N + 1], mx_id[4 * N + 1]; n void pull(int v) { n   if (t[2 * v] > t[2 * v + 1]) { n     t[v] = t[2 * v]; n     mx_id[v] = mx_id[2 * v]; n   } else { n     t[v] = t[2 * v + 1]; n     mx_id[v] = mx_id[2 * v + 1]; n   } n } n void upd(int v, int tl, int tr, int id, int x) { n   if (tl == tr) { n     t[v] = x; n     mx_id[v] = id; n     return; n   } n   int tm = (tl + tr) >> 1; n   if (id <= tm) { n     upd(2 * v, tl, tm, id, x); n   } else { n     upd(2 * v + 1, tm + 1, tr, id, x); n   } n   pull(v); n } n int pos = -1; n int mx = 0; n void calc(int v, int tl, int tr, int L, int R) { n   if (tl == L && tr == R) { n     if (mx < t[v]) { n       mx = t[v]; n       pos = mx_id[v]; n     } n     return; n   } n   int tm = (tl + tr) >> 1; n   if (R <= tm) { n     calc(2 * v, tl, tm, L, R); n   } else if (L > tm) { n     calc(2 * v + 1, tm + 1, tr, L, R); n   } else { n     calc(2 * v, tl, tm, L, tm); n     calc(2 * v + 1, tm + 1, tr, tm + 1, R); n   } n } n int t1, t2; n void dfs(struct node* v, int d = 0) { n   t1++; n   v->tin = t1; n   if (v->strings > 0) { n     upd(1, 1, N, t1, d); n   } n   for (int i = 0; i < SIZE; i++) { n     if (v->next[i] == nullptr) { n       continue; n     } n     dfs(v->next[i], d + 1); n   } n   if (d < 1) { n     return; n   } n   v->tout = t1; n   if (v->strings == 0) { n     mx = 0; n     pos = 0; n     calc(1, 1, N, v->tin, v->tout); n     upd(1, 1, N, pos, 0); n     upd(1, 1, N, v->tin, d); n     sub += mx - d; n   } n } n char buf[N]; n int main() { n   int n; n   scanf( %d , &n); n   string s; n   node* t = new node(); n   for (int i = 1; i <= n; i++) { n     scanf( %s , buf); n     s = buf; n     int sz = s.size(); n     tot += sz; n     add(t, s); n   } n   dfs(t); n   printf( %d n , tot - sub); n } n


#include <bits/stdc++.h> n using namespace std; n inline void setmin(int &x, int y) { n   if (y < x) x = y; n } n inline void setmax(int &x, int y) { n   if (y > x) x = y; n } n inline void setmin(long long &x, long long y) { n   if (y < x) x = y; n } n inline void setmax(long long &x, long long y) { n   if (y > x) x = y; n } n const int N = 100000; n const int inf = (int)1e9 + 1; n const long long big = (long long)1e18 + 1; n const int P = 239; n const int MOD = (int)1e9 + 7; n const int MOD1 = (int)1e9 + 9; n const double eps = 1e-9; n const double pi = atan2(0, -1); n const int ABC = 26; n vector<int> pos[N]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.precision(20); n   cout << fixed; n   int n, m, s, e; n   cin >> n >> m >> s >> e; n   int a[n], b[m]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     a[i]--; n   } n   for (int i = 0; i < m; i++) { n     cin >> b[i]; n     b[i]--; n     pos[b[i]].push_back(i); n   } n   int cnt = s / e; n   int dp[n][cnt + 1]; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < cnt + 1; j++) dp[i][j] = inf; n   for (int i = 0; i < n; i++) { n     int p = 0; n     dp[i][0] = -1; n     for (int j = 1; j < min(i + 1, cnt) + 1; j++) { n       if (i > 0) setmin(dp[i][j], dp[i - 1][j]); n       int last_pos = (i == 0 ? -1 : dp[i - 1][j - 1]); n       while (p < (int)pos[a[i]].size() && pos[a[i]][p] <= last_pos) p++; n       if (p < (int)pos[a[i]].size()) setmin(dp[i][j], pos[a[i]][p]); n     } n   } n   int ans = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < cnt + 1; j++) n       if ((i + 1) + (dp[i][j] + 1) + j * e <= s) setmax(ans, j); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long modPow(long long a, long long b, long long m) { n   if (b == 0) return 1; n   if (b % 2 == 0) { n     long long x = a * a; n     x %= m; n     return modPow(x, b / 2, m); n   } n   return (a * modPow(a, b - 1, m)) % m; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     long long n, i, j; n     cin >> n; n     string s =   ; n     for (i = 0; i < (53); i++) s +=  a ; n     cout << s; n     cout <<   n ; n     for (i = 0; i < (n); i++) { n       long long a; n       cin >> a; n       if (s[a] ==  a ) { n         for (auto j = (a); j <= (52); j++) { n           s[j] =  b ; n         } n       } else { n         for (auto j = (a); j <= (52); j++) { n           s[j] =  a ; n         } n       } n       cout << s; n       cout <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long g, t; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n int main() { n   int n; n   long long Max = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> t; n     if (i) n       g = gcd(g, t); n     else n       g = t; n     Max = max(Max, t); n   } n   long long cnt = Max / g - n; n   if (cnt % 2 == 0) n     printf( Bob n ); n   else n     printf( Alice n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, T, a[200010], x, y, d[200010], q[200010], l, r; n vector<int> v[200010], Q; n int main() { n   scanf( %d , &T); n   while (T--) { n     scanf( %d , &n); n     for (int i = 1; i <= n; i++) n       scanf( %d , &a[i]), a[i] = 2 - a[i], v[i].clear(); n     for (int i = 1; i < n; i++) { n       scanf( %d%d , &x, &y); n       v[x].push_back(y), v[y].push_back(x); n     } n     if (n == 1) { n       puts( 1 ); n       continue; n     } n     int Ans = n; n     for (int col = 0; col < 2; col++) { n       int ans = 0; n       for (int i = 1; i <= n; i++) d[i] = v[i].size(); n       l = 1, r = 0, Q.clear(); n       for (int i = 1; i <= n; i++) n         if (d[i] == 1) { n           if (a[i] != col) n             q[++r] = i; n           else n             Q.push_back(i); n         } n       while (1) { n         while (l <= r) { n           x = q[l++]; n           for (int j = 0, sz = v[x].size(); j < sz; j++) n             if ((--d[v[x][j]]) == 1) { n               if (a[v[x][j]] == (col ^ (ans & 1))) n                 Q.push_back(v[x][j]); n               else n                 q[++r] = v[x][j]; n             } n         } n         ans++; n         if (r == n) { n           Ans = min(Ans, ans); n           break; n         } n         for (int j = 0, sz = Q.size(); j < sz; j++) q[++r] = Q[j]; n         Q.clear(); n       } n     } n     printf( %d n , Ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   ; n   int t, n; n   string s, p; n   cin >> t; n   while (t--) { n     cin >> n; n     cin >> s; n     for (int j = 0; j < (2 * n - 1); j += 2) { n       cout << s[j]; n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0}; n void solve() { n   int n; n   cin >> n; n   int a[n + 1]; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     a[x] = i; n   } n   int l = 0, r = n - 1; n   for (int i = n; i > 0; i--) { n     if (a[i] == l) { n       l++; n       continue; n     } n     if (a[i] == r) { n       r--; n       continue; n     } n     cout <<  YES  << endl n          << a[i] - 1 + 1 <<     << a[i] + 1 <<     << a[i] + 1 + 1 << endl; n     return; n   } n   cout <<  NO  << endl; n } 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     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int MAXN = 2e5 + 10; n int n; n int a[MAXN], b[MAXN]; n namespace bit { n int a[MAXN]; n void init() { std::fill(a + 1, a + n + 1, 0); } n void add(int x, int y) { n   for (; x <= n; x += x & (-x)) { n     a[x] += y; n   } n } n int query(int x) { n   int ans = 0; n   for (; x > 0; x -= x & (-x)) { n     ans += a[x]; n   } n   return ans; n } n }  // namespace bit n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , a + i); n     a[i]++; n   } n   for (int i = 1; i <= n; i++) { n     scanf( %d , b + i); n     b[i]++; n   } n   static int p[MAXN], q[MAXN]; n   for (int i = 1; i <= n; i++) { n     p[i] = a[i] - bit::query(a[i]) - 1; n     bit::add(a[i], 1); n   } n   bit::init(); n   for (int i = 1; i <= n; i++) { n     q[i] = b[i] - bit::query(b[i]) - 1; n     bit::add(b[i], 1); n   } n   static int sum[MAXN]; n   for (int i = n; i >= 1; i--) { n     sum[i] += p[i] + q[i]; n     int k = n - i + 1; n     sum[i - 1] = sum[i] / k; n     sum[i] %= k; n   } n   bit::init(); n   for (int i = 1; i <= n; i++) { n     int l = 1, r = n, ans = 0; n     while (l <= r) { n       int mid = (l + r) >> 1; n       if (mid - bit::query(mid) - 1 >= sum[i]) { n         ans = mid; n         r = mid - 1; n       } else { n         l = mid + 1; n       } n     } n     bit::add(ans, 1); n     printf( %d  , ans - 1); n   } n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = int(1e4) + 4; n int a[MAXN], h[MAXN], c[MAXN]; n int n; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n     h[i] = a[i]; n     int j = i - 1; n     while (h[j] > h[i] && j > 0) { n       h[i] += a[j]; n       c[i]++; n       j--; n     } n     c[i] += c[j]; n   } n   cout << c[n] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n const int N = 2e5 + 3; n const int MOD = 998244353; n int max1 = 0, max2 = 0; n void solve() { n   long long int a[3]; n   cin >> a[0] >> a[1] >> a[2]; n   sort(a, a + 3); n   cout << (a[0] + a[1] + min(a[2], 2 * (a[1] + a[0]))) / 3 << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> cc[100000]; n int num[100000]; n int main() { n   int n, k, m; n   cin >> n >> k >> m; n   for (int j = 0; j < n; cin >> num[j++]) n     ; n   sort(num, num + n); n   for (int j = 0; j < m; cc[j++].clear()) n     ; n   cc[0].push_back(0); n   for (int j = 1; j < n; j++) cc[(num[j] - num[0]) % m].push_back(j); n   for (int j = 0; j < m; j++) { n     if (cc[j].size() >= k) { n       cout <<  Yes  << endl; n       for (int i = 0; i < k; i++) { n         cout << num[cc[j][i]] <<    ; n       } n       return 0; n     } n   } n   cout <<  No  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 31; n const int mod = 1000000007; n struct ma { n   long long a[maxn][maxn]; n   int n; n   ma(int m = 0) { n     n = m; n     memset(a, 0, sizeof(a)); n     for (int i = 0; i < n; i++) a[i][i] = 1; n   } n   ma(const ma& nn) { n     n = nn.n; n     memcpy(a, nn.a, sizeof(a)); n   } n   ma operator*(const ma& nn) { n     ma ret(n); n     memset(ret.a, 0, sizeof(ret.a)); n     for (int i = 0; i < n; i++) n       for (int j = 0; j < n; j++) { n         for (int k = 0; k < n; k++) ret.a[i][j] += a[i][k] * nn.a[k][j] % mod; n         ret.a[i][j] %= mod; n       } n     return ret; n   } n }; n ma mypow(ma a, long long n) { n   ma ret(a.n); n   while (n) { n     if (n & 1) ret = ret * a; n     n >>= 1; n     a = a * a; n   } n   return ret; n } n ma m1[100], m2[100]; n int main() { n   long long n; n   int k, i, j; n   cin >> n >> k; n   n--; n   m1[1].n = k + 1; n   for (i = 0; i <= k; i++) m1[1].a[i][i] = 1; n   for (i = 1; i < 99; i++) { n     int tmp = ((-i - 1) % k + k) % k + 1; n     tmp = k - tmp; n     memcpy(m2[i].a[0], m1[i].a[k], sizeof(m1[i].a[k])); n     for (j = 1; j <= k; j++) { n       for (int c = 0; c <= k; c++) n         m2[i].a[j][c] = n             ((m2[i].a[j - 1][c] * 2 - m1[i].a[tmp][c]) % mod + mod) % mod; n       tmp++; n       if (tmp >= k) tmp = 0; n     } n     m2[i].n = k + 1; n     m1[i + 1] = m1[i] * mypow(m2[i], k - 1); n   } n   ma ans(k + 1); n   int ind = 1, sheng = n % k; n   n /= k; n   while (n) { n     ans = ans * mypow(m2[ind], n % k); n     ind++, n /= k; n   } n   long long ret = 0; n   for (i = 0; i <= k; i++) ret = (ret + ans.a[sheng + 1][i] * (1ll << i)) % mod; n   cout << ret << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = (int)1e9; n const long long inf = (long long)3e18; n const int base = 1000 * 1000 * 1000; n const int maxn = 20; n const long double pi = acosl(-1.0); n const long double eps = 1e-5; n void error() { exit(1); } n int n; n bool flag = true; n long long A, cf, cm, m; n vector<long long> d; n long long ans = 0, minans; n long long predmin, curmin, people; n unordered_map<long long, long long> st; n set<long long> val; n set<pair<long long, int> > powerans; n set<pair<long long, int> > power, nopower; n void solve() { n   scanf( %d %I64d %I64d %I64d %I64d , &n, &A, &cf, &cm, &m); n   d.resize(n); n   for (int i = 0; i < n; i++) { n     scanf( %I64d , &d[i]); n     nopower.insert(make_pair((-1) * d[i], i)); n     st[d[i]]++; n     val.insert(d[i]); n   } n   val.insert(A); n   val.insert(A + 1); n   while ((int)nopower.size() && (A + (*nopower.begin()).first) <= m) { n     pair<long long, int> v = (*nopower.begin()); n     nopower.erase(nopower.begin()); n     st[(-1) * v.first]--; n     st[A]++; n     m -= (A + v.first); n     power.insert(make_pair((-1) * v.first, v.second)); n     ans += cf; n   } n   predmin = (*val.begin()); n   val.erase(val.begin()); n   curmin = (*val.begin()); n   val.erase(*val.begin()); n   people = st[predmin]; n   ans += predmin * cm; n   minans = predmin; n   powerans = power; n   while ((int)power.size() || flag) { n     if (predmin == A || (int)power.size() == n) { n       ans = A * cm + n * cf; n       break; n     } n     if ((int)power.size() && (m <= 0 || !flag)) { n       pair<long long, int> v = (*power.begin()); n       power.erase(power.begin()); n       if (predmin >= v.first) n         m += (A - predmin); n       else n         m += (A - v.first); n       if (v.first <= predmin) n         people++; n       else n         st[v.first]++; n     } n     flag = false; n     long long del = m / people; n     if (m > 0 && del > 0) { n       flag = true; n       if (curmin <= predmin + del) { n         m -= (curmin - predmin) * people; n         people += st[curmin]; n         predmin = curmin; n         curmin = (*val.begin()); n         val.erase(*val.begin()); n       } else { n         predmin += del; n         m -= del * people; n       } n       long long curans = predmin * cm + (int)power.size() * cf; n       if (predmin == A) curans += (people - (int)power.size()) * cf; n       if (ans < curans) { n         ans = curans; n         minans = predmin; n         while ((int)powerans.size() != (int)power.size()) { n           powerans.erase(*powerans.begin()); n         } n       } n     } n   } n   printf( %I64d n , ans); n   while ((int)powerans.size()) { n     pair<long long, int> v = (*powerans.begin()); n     powerans.erase(powerans.begin()); n     d[v.second] = A; n   } n   for (int i = 0; i < n; i++) n     if (d[i] < minans) d[i] = minans; n   for (int i = 0; i < n; i++) printf( %I64d  , d[i]); n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n const int MAXN = 1000001; n int n, m, a[MAXN], b[MAXN]; n int box[MAXN], axi[MAXN]; n int n1, n2; n int main() { n   std::cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     std::scanf( %d , a + i); n     if (a[i] <= m) { n       b[++n1] = a[i]; n       box[a[i]]++; n     } n   } n   if (!n1) { n     std::puts( 1 0 ); n     return 0; n   } n   std::sort(b + 1, b + 1 + n1); n   int n2 = std::unique(b + 1, b + 1 + n1) - b - 1; n   for (int i = 1; i <= n2; i++) n     for (int j = 1; j <= m / b[i]; j++) axi[b[i] * j] += box[b[i]]; n   int sum = 0, lcm; n   for (int i = 1; i <= m; i++) { n     if (axi[i] > sum) sum = axi[i], lcm = i; n   } n   std::printf( %d %d n , lcm, sum); n   for (int i = 1; i <= n; i++) n     if (!(lcm % a[i])) std::printf( %d  , i); n   std::putchar(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 100; n const int mod = 1e9 + 7; n mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); n int n, trie[maxn][26], en[maxn], nxt; n string s; n void ins(int cur, int i) { n   if (i == (int)s.size()) n     en[cur] = 1; n   else { n     int le = (s[i] -  a ); n     if (!trie[cur][le]) trie[cur][le] = ++nxt; n     ins(trie[cur][le], i + 1); n   } n } n multiset<int> merge(multiset<int> a, multiset<int> b) { n   if ((int)a.size() < (int)b.size()) swap(a, b); n   for (auto aa : b) a.insert(aa); n   return a; n } n multiset<int> dfs(int cur, int dep) { n   multiset<int> ret; n   for (int i = 0; i < 26; i++) n     if (trie[cur][i]) ret = merge(ret, dfs(trie[cur][i], dep + 1)); n   if (en[cur]) n     ret.insert(dep); n   else if (cur != 0) { n     int a = *prev(ret.end()); n     ret.erase(prev(ret.end())); n     ret.insert(dep); n   } n   return ret; n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> s; n     ins(0, 0); n   } n   multiset<int> res = dfs(0, 0); n   long long ans = 0; n   for (auto a : res) ans += a; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 2e5 + 10; n long long arr[MAX], arr2[MAX]; n int n; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> arr[i]; n   for (int i = 0; i < n; ++i) cin >> arr2[i]; n   vector<pair<long long, long long> > vp; n   for (int i = 0; i < n; ++i) vp.push_back({arr[i], arr2[i]}); n   sort(vp.begin(), vp.end()); n   for (int i = 0; i < n; ++i) arr[i] = vp[i].first, arr2[i] = vp[i].second; n   int idx = 0; n   int cur = arr[0]; n   long long ans = 0, sum = 0; n   priority_queue<long long> q; n   while (1) { n     ans += sum; n     while (idx < n && cur == arr[idx]) { n       sum += arr2[idx]; n       q.push(arr2[idx]); n       idx++; n     } n     if (q.size() == 0) { n       if (idx == n) break; n       cur = arr[idx]; n     } else { n       long long x = q.top(); n       q.pop(); n       sum -= x; n       cur++; n     } n   } n   return cout << ans <<   n , 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ld = long double; n ld sq(ld x) { return x * x; } n int main() { n   size_t n; n   cin >> n; n   vector<ld> mn(n), mx(n), mnk(n), mxk(n); n   for (size_t i = 0; i != n; ++i) { n     cin >> mx[i]; n   } n   for (size_t i = 0; i != n; ++i) { n     cin >> mn[i]; n   } n   mnk[0] = mn[0]; n   mxk[0] = mx[0]; n   for (size_t i = 1; i != n; ++i) { n     mnk[i] = mnk[i - 1] + mn[i]; n     mxk[i] = mxk[i - 1] + mx[i]; n   } n   vector<ld> a(n), b(n), ak(n), bk(n); n   ak[n - 1] = 1; n   bk[n - 1] = 1; n   for (size_t i = 0; i < n - 1; ++i) { n     ld u = mxk[i]; n     ld v = mnk[i]; n     ld D = sqrt(sq(u + v) - 4 * u); n     ld x1 = (u + v + D) / 2; n     ld x2 = (u + v - D) / 2; n     ak[i] = x1; n     bk[i] = x2; n   } n   a[0] = ak[0]; n   b[0] = bk[0]; n   for (size_t i = 1; i != n; ++i) { n     a[i] = ak[i] - ak[i - 1]; n     b[i] = bk[i] - bk[i - 1]; n   } n   for (size_t i = 0; i != n; ++i) cout << a[i] <<    ; n   cout << endl; n   for (size_t i = 0; i != n; ++i) cout << b[i] <<    ; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <int M> n struct static_mint { n   static_assert(0 < M,  Module must be positive ); n   int val; n   static_mint() : val() {} n   static_mint(long long x) : val(x % M) { n     if (val < 0) val += M; n   } n   static_mint pow(long long n) const { n     static_mint ans = 1, x(*this); n     while (n) { n       if (n & 1) ans *= x; n       x *= x; n       n /= 2; n     } n     return ans; n   } n   static_mint inv() const { return pow(M - 2); } n   friend static_mint pow(const static_mint &m, long long n) { return m.pow(n); } n   friend static_mint inv(const static_mint &m) { return m.inv(); } n   static_mint operator+() const { n     static_mint m; n     m.val = val; n     return m; n   } n   static_mint operator-() const { n     static_mint m; n     m.val = M - val; n     return m; n   } n   static_mint &operator+=(const static_mint &m) { n     if ((val += m.val) >= M) val -= M; n     return *this; n   } n   static_mint &operator-=(const static_mint &m) { n     if ((val -= m.val) < 0) val += M; n     return *this; n   } n   static_mint &operator*=(const static_mint &m) { n     val = (long long)val * m.val % M; n     return *this; n   } n   static_mint &operator/=(const static_mint &m) { n     val = (long long)val * m.inv().val % M; n     return *this; n   } n   friend static_mint operator+(const static_mint &lhs, const static_mint &rhs) { n     return static_mint(lhs) += rhs; n   } n   friend static_mint operator-(const static_mint &lhs, const static_mint &rhs) { n     return static_mint(lhs) -= rhs; n   } n   friend static_mint operator*(const static_mint &lhs, const static_mint &rhs) { n     return static_mint(lhs) *= rhs; n   } n   friend static_mint operator/(const static_mint &lhs, const static_mint &rhs) { n     return static_mint(lhs) /= rhs; n   } n   friend bool operator==(const static_mint &lhs, const static_mint &rhs) { n     return lhs.val == rhs.val; n   } n   friend bool operator!=(const static_mint &lhs, const static_mint &rhs) { n     return lhs.val != rhs.val; n   } n   static_mint &operator++() { return *this += 1; } n   static_mint &operator--() { return *this -= 1; } n   static_mint operator++(int) { n     static_mint result(*this); n     *this += 1; n     return result; n   } n   static_mint operator--(int) { n     static_mint result(*this); n     *this -= 1; n     return result; n   } n   template <typename T> n   explicit operator T() const { n     return T(val); n   } n   friend std::ostream &operator<<(std::ostream &os, const static_mint &m) { n     return os << m.val; n   } n   friend std::istream &operator>>(std::istream &is, static_mint &m) { n     long long x; n     is >> x; n     m = x; n     return is; n   } n }; n template <typename> n struct is_mint : public std::false_type {}; n template <int M> n struct is_mint<static_mint<M>> : public std::true_type {}; n template <typename T> n struct matrix : std::vector<std::vector<T>> { n   int n, m; n   matrix() : n(), m() {} n   matrix(int n, int m, const T val = T()) n       : n(n), m(m), std::vector<std::vector<T>>(n, std::vector<T>(m, val)) {} n   matrix(std::initializer_list<std::vector<T>> l) n       : std::vector<std::vector<T>>(l) { n     n = l.size(); n     if (l.size()) { n       m = l.begin()->size(); n     } n   } n   matrix &operator+=(const matrix &mat) { n     assert(n == mat.n && m == mat.m); n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         (*this)[i][j] += mat[i][j]; n       } n     } n     return *this; n   } n   matrix &operator-=(const matrix &mat) { n     assert(n == mat.n && m == mat.m); n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         (*this)[i][j] -= mat[i][j]; n       } n     } n     return *this; n   } n   matrix &operator*=(const matrix &mat) { n     assert(m == mat.n); n     matrix res(n, mat.m); n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < mat.m; j++) { n         for (int k = 0; k < m; k++) { n           res[i][j] += (*this)[i][k] * mat[k][j]; n         } n       } n     } n     this->swap(res); n     return *this; n   } n   matrix &operator*=(const T &val) { n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         (*this)[i][j] *= val; n       } n     } n     return *this; n   } n   friend matrix operator+(matrix a, const matrix &b) { n     a += b; n     return a; n   } n   friend matrix operator-(matrix a, const matrix &b) { n     a -= b; n     return a; n   } n   friend matrix operator*(matrix a, const matrix &b) { n     a *= b; n     return a; n   } n   friend matrix operator*(matrix a, const T &val) { n     a *= val; n     return a; n   } n   friend matrix operator*(const T &val, matrix a) { n     a *= val; n     return a; n   } n }; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   using mint = static_mint<1000000007>; n   int n, x; n   cin >> n >> x; n   vector<int> cnt(101); n   for (int i = 0, d; i < n; i++) { n     cin >> d, cnt[d] += 1; n   } n   matrix<mint> m(101, 101); n   m[0][0] = 1 + cnt[1]; n   for (int i = 1; i < 100; i++) { n     m[i][0] = cnt[i + 1] - cnt[i]; n   } n   m[100][0] = -cnt[100]; n   for (int j = 1; j < 101; j++) { n     m[j - 1][j] = 1; n   } n   matrix<mint> ans(101, 101); n   for (int i = 0; i < 101; i++) ans[0][0] = 1; n   for (; x; x /= 2) { n     if (x & 1) ans *= m; n     m *= m; n   } n   cout << ans[0][0] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename A, typename B> n string to_string(pair<A, B> p); n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p); n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p); n string to_string(const string& s) { return     + s +    ; } n string to_string(const char* s) { return to_string((string)s); } n string to_string(bool b) { return (b ?  true  :  false ); } n string to_string(vector<bool> v) { n   bool first = true; n   string res =  { ; n   for (int i = 0; i < static_cast<int>(v.size()); i++) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(v[i]); n   } n   res +=  } ; n   return res; n } n template <size_t N> n string to_string(bitset<N> v) { n   string res =   ; n   for (size_t i = 0; i < N; i++) { n     res += static_cast<char>( 0  + v[i]); n   } n   return res; n } n template <typename A> n string to_string(A v) { n   bool first = true; n   string res =  { ; n   for (const auto& x : v) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ) ; n } n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ,   + to_string(get<3>(p)) +  ) ; n } n void debug_out() { cerr << endl; } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cerr <<     << to_string(H); n   debug_out(T...); n } n template <typename T> n T inverse(T a, T m) { n   T u = 0, v = 1; n   while (a != 0) { n     T t = m / a; n     m -= t * a; n     swap(a, m); n     u -= t * v; n     swap(u, v); n   } n   assert(m == 1); n   return u; n } n template <typename T> n class Modular { n  public: n   using Type = typename decay<decltype(T::value)>::type; n   constexpr Modular() : value() {} n   template <typename U> n   Modular(const U& x) { n     value = normalize(x); n   } n   template <typename U> n   static Type normalize(const U& x) { n     Type v; n     if (-mod() <= x && x < mod()) n       v = static_cast<Type>(x); n     else n       v = static_cast<Type>(x % mod()); n     if (v < 0) v += mod(); n     return v; n   } n   const Type& operator()() const { return value; } n   template <typename U> n   explicit operator U() const { n     return static_cast<U>(value); n   } n   constexpr static Type mod() { return T::value; } n   Modular& operator+=(const Modular& other) { n     if ((value += other.value) >= mod()) value -= mod(); n     return *this; n   } n   Modular& operator-=(const Modular& other) { n     if ((value -= other.value) < 0) value += mod(); n     return *this; n   } n   template <typename U> n   Modular& operator+=(const U& other) { n     return *this += Modular(other); n   } n   template <typename U> n   Modular& operator-=(const U& other) { n     return *this -= Modular(other); n   } n   Modular& operator++() { return *this += 1; } n   Modular& operator--() { return *this -= 1; } n   Modular operator++(int) { n     Modular result(*this); n     *this += 1; n     return result; n   } n   Modular operator--(int) { n     Modular result(*this); n     *this -= 1; n     return result; n   } n   Modular operator-() const { return Modular(-value); } n   template <typename U = T> n   typename enable_if<is_same<typename Modular<U>::Type, int>::value, n                      Modular>::type& n   operator*=(const Modular& rhs) { n     value = normalize(static_cast<int64_t>(value) * n                       static_cast<int64_t>(rhs.value)); n     return *this; n   } n   template <typename U = T> n   typename enable_if<is_same<typename Modular<U>::Type, int64_t>::value, n                      Modular>::type& n   operator*=(const Modular& rhs) { n     int64_t q = static_cast<int64_t>(static_cast<long double>(value) * n                                      rhs.value / mod()); n     value = normalize(value * rhs.value - q * mod()); n     return *this; n   } n   template <typename U = T> n   typename enable_if<!is_integral<typename Modular<U>::Type>::value, n                      Modular>::type& n   operator*=(const Modular& rhs) { n     value = normalize(value * rhs.value); n     return *this; n   } n   Modular& operator/=(const Modular& other) { n     return *this *= Modular(inverse(other.value, mod())); n   } n   template <typename U> n   friend const Modular<U>& abs(const Modular<U>& v) { n     return v; n   } n   template <typename U> n   friend bool operator==(const Modular<U>& lhs, const Modular<U>& rhs); n   template <typename U> n   friend bool operator<(const Modular<U>& lhs, const Modular<U>& rhs); n   template <typename U> n   friend std::istream& operator>>(std::istream& stream, Modular<U>& number); n  n  private: n   Type value; n }; n template <typename T> n bool operator==(const Modular<T>& lhs, const Modular<T>& rhs) { n   return lhs.value == rhs.value; n } n template <typename T, typename U> n bool operator==(const Modular<T>& lhs, U rhs) { n   return lhs == Modular<T>(rhs); n } n template <typename T, typename U> n bool operator==(U lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) == rhs; n } n template <typename T> n bool operator!=(const Modular<T>& lhs, const Modular<T>& rhs) { n   return !(lhs == rhs); n } n template <typename T, typename U> n bool operator!=(const Modular<T>& lhs, U rhs) { n   return !(lhs == rhs); n } n template <typename T, typename U> n bool operator!=(U lhs, const Modular<T>& rhs) { n   return !(lhs == rhs); n } n template <typename T> n bool operator<(const Modular<T>& lhs, const Modular<T>& rhs) { n   return lhs.value < rhs.value; n } n template <typename T> n Modular<T> operator+(const Modular<T>& lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) += rhs; n } n template <typename T, typename U> n Modular<T> operator+(const Modular<T>& lhs, U rhs) { n   return Modular<T>(lhs) += rhs; n } n template <typename T, typename U> n Modular<T> operator+(U lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) += rhs; n } n template <typename T> n Modular<T> operator-(const Modular<T>& lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) -= rhs; n } n template <typename T, typename U> n Modular<T> operator-(const Modular<T>& lhs, U rhs) { n   return Modular<T>(lhs) -= rhs; n } n template <typename T, typename U> n Modular<T> operator-(U lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) -= rhs; n } n template <typename T> n Modular<T> operator*(const Modular<T>& lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) *= rhs; n } n template <typename T, typename U> n Modular<T> operator*(const Modular<T>& lhs, U rhs) { n   return Modular<T>(lhs) *= rhs; n } n template <typename T, typename U> n Modular<T> operator*(U lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) *= rhs; n } n template <typename T> n Modular<T> operator/(const Modular<T>& lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) /= rhs; n } n template <typename T, typename U> n Modular<T> operator/(const Modular<T>& lhs, U rhs) { n   return Modular<T>(lhs) /= rhs; n } n template <typename T, typename U> n Modular<T> operator/(U lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) /= rhs; n } n template <typename T, typename U> n Modular<T> power(const Modular<T>& a, const U& b) { n   assert(b >= 0); n   Modular<T> x = a, res = 1; n   U p = b; n   while (p > 0) { n     if (p & 1) res *= x; n     x *= x; n     p >>= 1; n   } n   return res; n } n template <typename T> n bool IsZero(const Modular<T>& number) { n   return number() == 0; n } n template <typename T> n string to_string(const Modular<T>& number) { n   return to_string(number()); n } n template <typename T> n std::ostream& operator<<(std::ostream& stream, const Modular<T>& number) { n   return stream << number(); n } n template <typename T> n std::istream& operator>>(std::istream& stream, Modular<T>& number) { n   typename common_type<typename Modular<T>::Type, int64_t>::type x; n   stream >> x; n   number.value = Modular<T>::normalize(x); n   return stream; n } n constexpr int md = (int)1e9 + 7; n using Mint = Modular<std::integral_constant<decay<decltype(md)>::type, md>>; n template <typename T> n class graph { n  public: n   struct edge { n     int from; n     int to; n     T cost; n   }; n   vector<edge> edges; n   vector<vector<int>> g; n   int n; n   graph(int _n) : n(_n) { g.resize(n); } n   virtual int add(int from, int to, T cost) = 0; n }; n template <typename T> n class forest : public graph<T> { n  public: n   using graph<T>::edges; n   using graph<T>::g; n   using graph<T>::n; n   forest(int _n) : graph<T>(_n) {} n   int add(int from, int to, T cost = 1) { n     assert(0 <= from && from < n && 0 <= to && to < n); n     int id = (int)edges.size(); n     assert(id < n - 1); n     g[from].push_back(id); n     g[to].push_back(id); n     edges.push_back({from, to, cost}); n     return id; n   } n }; n template <typename T> n class dfs_forest : public forest<T> { n  public: n   using forest<T>::edges; n   using forest<T>::g; n   using forest<T>::n; n   vector<int> pv; n   vector<int> pe; n   vector<int> order; n   vector<int> pos; n   vector<int> end; n   vector<int> sz; n   vector<int> root; n   vector<int> depth; n   vector<T> dist; n   dfs_forest(int _n) : forest<T>(_n) {} n   void init() { n     pv = vector<int>(n, -1); n     pe = vector<int>(n, -1); n     order.clear(); n     pos = vector<int>(n, -1); n     end = vector<int>(n, -1); n     sz = vector<int>(n, 0); n     root = vector<int>(n, -1); n     depth = vector<int>(n, -1); n     dist = vector<T>(n); n   } n   void clear() { n     pv.clear(); n     pe.clear(); n     order.clear(); n     pos.clear(); n     end.clear(); n     sz.clear(); n     root.clear(); n     depth.clear(); n     dist.clear(); n   } n  n  private: n   void do_dfs(int v) { n     pos[v] = (int)order.size(); n     order.push_back(v); n     sz[v] = 1; n     for (int id : g[v]) { n       if (id == pe[v]) { n         continue; n       } n       auto& e = edges[id]; n       int to = e.from ^ e.to ^ v; n       depth[to] = depth[v] + 1; n       dist[to] = dist[v] + e.cost; n       pv[to] = v; n       pe[to] = id; n       root[to] = (root[v] != -1 ? root[v] : to); n       do_dfs(to); n       sz[v] += sz[to]; n     } n     end[v] = (int)order.size() - 1; n   } n   void do_dfs_from(int v) { n     depth[v] = 0; n     dist[v] = T{}; n     root[v] = v; n     pv[v] = pe[v] = -1; n     do_dfs(v); n   } n  n  public: n   void dfs(int v, bool clear_order = true) { n     if (pv.empty()) { n       init(); n     } else { n       if (clear_order) { n         order.clear(); n       } n     } n     do_dfs_from(v); n   } n   void dfs_all() { n     init(); n     for (int v = 0; v < n; v++) { n       if (depth[v] == -1) { n         do_dfs_from(v); n       } n     } n     assert((int)order.size() == n); n   } n }; n template <typename T> n class lca_forest : public dfs_forest<T> { n  public: n   using dfs_forest<T>::edges; n   using dfs_forest<T>::g; n   using dfs_forest<T>::n; n   using dfs_forest<T>::pv; n   using dfs_forest<T>::pos; n   using dfs_forest<T>::end; n   using dfs_forest<T>::depth; n   int h; n   vector<vector<int>> pr; n   lca_forest(int _n) : dfs_forest<T>(_n) {} n   inline void build_lca() { n     assert(!pv.empty()); n     int max_depth = 0; n     for (int i = 0; i < n; i++) { n       max_depth = max(max_depth, depth[i]); n     } n     h = 1; n     while ((1 << h) <= max_depth) { n       h++; n     } n     pr.resize(n); n     for (int i = 0; i < n; i++) { n       pr[i].resize(h); n       pr[i][0] = pv[i]; n     } n     for (int j = 1; j < h; j++) { n       for (int i = 0; i < n; i++) { n         pr[i][j] = (pr[i][j - 1] == -1 ? -1 : pr[pr[i][j - 1]][j - 1]); n       } n     } n   } n   inline bool anc(int x, int y) { n     return (pos[x] <= pos[y] && end[y] <= end[x]); n   } n   inline int go_up(int x, int up) { n     assert(!pr.empty()); n     up = min(up, (1 << h) - 1); n     for (int j = h - 1; j >= 0; j--) { n       if (up & (1 << j)) { n         x = pr[x][j]; n         if (x == -1) { n           break; n         } n       } n     } n     return x; n   } n   inline int lca(int x, int y) { n     assert(!pr.empty()); n     if (anc(x, y)) { n       return x; n     } n     if (anc(y, x)) { n       return y; n     } n     for (int j = h - 1; j >= 0; j--) { n       if (pr[x][j] != -1 && !anc(pr[x][j], y)) { n         x = pr[x][j]; n       } n     } n     return pr[x][0]; n   } n }; n template <typename T> n class fenwick { n  public: n   vector<T> fenw; n   int n; n   fenwick(int _n) : n(_n) { fenw.resize(n); } n   void modify(int x, T v) { n     while (x < n) { n       fenw[x] += v; n       x |= (x + 1); n     } n   } n   T get(int x) { n     T v{}; n     while (x >= 0) { n       v += fenw[x]; n       x = (x & (x + 1)) - 1; n     } n     return v; n   } n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   dfs_forest<int> g(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; n     --y; n     g.add(x, y); n     ++deg[x]; n     ++deg[y]; n   } n   42; n   g.dfs(0); n   vector<vector<int>> children(n); n   for (int i = 1; i < n; i++) { n     children[g.pv[i]].push_back(i); n   } n   42; n   vector<int> num(n, -1); n   vector<int> low(n, -1); n   for (int i : g.order) { n     int sz = (int)children[i].size(); n     int from = (i == 0 ? -1 : max(0, num[i] - sz)); n     low[i] = from; n     int x = from; n     for (int j : children[i]) { n       if (x == num[i]) { n         ++x; n       } n       num[j] = x++; n     } n   } n   42; n   vector<vector<int>> go(n); n   for (int i = 0; i < n; i++) { n     go[i].resize(deg[i]); n   } n   42; n   go[0].resize(deg[0] + 1); n   42; n   for (int i = 1; i < n; i++) { n     int j = g.pv[i]; n     int t = num[i]; n     go[i][t - low[i]] = j; n     go[j][t - low[j]] = i; n   } n   42; n   vector<pair<int, int>> res; n   res.emplace_back(0, 0); n   42; n   while (res.back().second >= 0) { n     int i = res.back().first; n     int t = res.back().second; n     int pos = t - low[i]; n     if (pos >= (int)go[i].size()) { n       res.emplace_back(i, low[i]); n       continue; n     } n     res.emplace_back(go[i][pos], t + 1); n   } n   42; n   res.pop_back(); n   42; n   cout << res.size() <<   n ; n   for (auto& p : res) { n     cout << p.first + 1 <<     << p.second <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long MOD = 1000000007; n long long binary_exp(long long a, long long b) { n   long long ans = 1; n   while (b > 0) { n     if (b & 1) { n       ans = (ans * a); n     } n     a = (a * a); n     b = b >> 1; n   } n   return ans; n } n int fn(int low, vector<int> &a, int p) { n   int k = low; n   for (int i = 0; i < a.size(); i++) { n     k = low + i; n     int j = upper_bound(a.begin(), a.end(), k) - a.begin(); n     if ((j - i) % p == 0) { n       return 0; n     } n   } n   return 1; n } n map<long long, long long> mp; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long T; n   T = 1; n   while (T--) { n     int n, p; n     cin >> n >> p; n     vector<int> a(n); n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n     } n     sort(a.begin(), a.end()); n     int low = max(a[0], a[n - 1] - n + 1); n     for (int i = 0; i < n; i++) { n       low = max(low, a[i] - i); n     } n     int high = a[n - 1]; n     if (!fn(low, a, p)) { n       cout << 0 << endl; n       cout << endl; n       continue; n     } n     int z = low; n     while (low < high) { n       int mid = (low + high) / 2; n       mid++; n       if (fn(mid, a, p)) { n         low = mid; n       } else { n         high = mid - 1; n       } n     } n     cout << low - z + 1 << endl; n     for (int i = z; i <= low; i++) { n       cout << i <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e18; n const long long M = 1e9 + 9; n long long axm(long long a, long long x, long long m) { n   a %= m; n   long long r = 1; n   while (x > 0) { n     if (x & 1) r = r * a % m; n     a = a * a % m; n     x /= 2; n   } n   return r; n } n long long inv(long long a, long long m) { return axm(a, m - 2, m); } n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n struct P { n   long long x, y; n }; n long long dist(P a, P b) { n   return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); n } n long long test(P p1, P p2, P p3, P p4) { n   long long d2 = dist(p1, p2); n   long long d3 = dist(p1, p3); n   long long d4 = dist(p1, p4); n   if (d2 == 0 || d3 == 0 || d4 == 0) return 0; n   if (d2 == d3 && 2 * d2 == d4 && 2 * dist(p2, p4) == dist(p2, p3)) { n     return 1; n   } n   if (d3 == d4 && 2 * d3 == d2 && 2 * dist(p3, p2) == dist(p3, p4)) { n     return 1; n   } n   if (d2 == d4 && 2 * d2 == d3 && 2 * dist(p2, p3) == dist(p2, p4)) { n     return 1; n   } n   return 0; n } n P p[4][4]; n int main(int argc, char **argv) { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   ; n   long long n; n   cin >> n; n   for (long long tt = 0; tt < n; tt++) { n     for (long long i = 0; i < 4; i++) { n       long long xx, yy, aa, bb; n       cin >> xx >> yy >> aa >> bb; n       p[i][0].x = xx; n       p[i][0].y = yy; n       for (long long j = 1; j < 4; j++) { n         p[i][j].x = aa - (p[i][j - 1].y - bb); n         p[i][j].y = bb + (p[i][j - 1].x - aa); n       } n     } n     long long ans = inf; n     for (long long i = 0; i < 4; i++) { n       for (long long j = 0; j < 4; j++) { n         for (long long k = 0; k < 4; k++) { n           for (long long l = 0; l < 4; l++) { n             long long s = i + j + k + l; n             if (s < ans && test(p[0][i], p[1][j], p[2][k], p[3][l])) { n               ans = s; n             } n           } n         } n       } n     } n     if (ans == inf) ans = -1; n     cout << ans <<   n ; n   } n   return 0; n } n