#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const ll INF = 4e18; n const int N = 5e5 + 1; n vector<int> vec; n vector<pair<int, ll> > g[N]; n int n, op, t, l, r, q, s, pos[N][2]; n void construct(int node = 1, int b = 1, int e = n) { n   pos[node][op] = ++t; n   if (b == e) { n     if (op == 0) n       g[pos[node][0]].push_back({b, 0}); n     else n       g[b].push_back({pos[node][1], 0}); n     return; n   } n   int m = (b + e) >> 1; n   construct(node << 1, b, m); n   construct(node << 1 | 1, m + 1, e); n   if (op == 0) { n     g[pos[node][0]].push_back({pos[node << 1][0], 0}); n     g[pos[node][0]].push_back({pos[node << 1 | 1][0], 0}); n   } else { n     g[pos[node << 1][1]].push_back({pos[node][1], 0}); n     g[pos[node << 1 | 1][1]].push_back({pos[node][1], 0}); n   } n } n void query(int node = 1, int b = 1, int e = n) { n   if (b > e || b > r || e < l) return; n   if (l <= b && e <= r) { n     vec.push_back(pos[node][op]); n     return; n   } n   int m = (b + e) >> 1; n   query(node << 1, b, m); n   query(node << 1 | 1, m + 1, e); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> q >> s; n   t = n; n   op = 0; n   construct(); n   op = 1; n   construct(); n   while (q--) { n     cin >> op; n     if (op == 1) { n       int v, u, w; n       cin >> v >> u >> w; n       g[v].push_back({u, w}); n     } else { n       op -= 2; n       int v, w; n       cin >> v >> l >> r >> w; n       query(); n       if (op == 0) { n         for (int x : vec) { n           g[v].push_back({x, w}); n         } n       } else { n         for (int x : vec) { n           g[x].push_back({v, w}); n         } n       } n       vec.clear(); n     } n   } n   priority_queue<pair<ll, int> > pq; n   pq.push({0, s}); n   vector<ll> dis(N, INF); n   dis[s] = 0; n   for (int i = 1; i <= n; i += 1) { n     pq.push({-dis[i], i}); n   } n   while (!pq.empty()) { n     int u = pq.top().second; n     pq.pop(); n     for (auto x : g[u]) { n       int v = x.first; n       int cost = x.second; n       if (dis[v] > dis[u] + cost) { n         dis[v] = dis[u] + cost; n         pq.push({-dis[v], v}); n       } n     } n   } n   for (int i = 1; i <= n; i += 1) { n     if (dis[i] >= INF) dis[i] = -1; n     cout << dis[i] <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> primes; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   int n, q; n   cin >> n >> q; n   vector<pair<int, int> > ans; n   int tmp(-1); n   vector<bool> state; n   for (int i = 0; i < n; ++i) { n     int x; n     cin >> x; n     if (tmp <= x) { n       if (!i) n         tmp = x; n       else { n         ans.push_back(make_pair(tmp, x)); n         tmp = x; n         state.push_back(true); n       } n     } else { n       ans.push_back(make_pair(x, tmp)); n       state.push_back(false); n     } n   } n   int nw = n - 1; n   while (q--) { n     long long y; n     cin >> y; n     if (!y) n       cout <<   n ; n     else { n       if (y < n) { n         if (state[y - 1]) n           cout << ans[y - 1].first <<     << ans[y - 1].second <<   n ; n         else n           cout << ans[y - 1].second <<     << ans[y - 1].first <<   n ; n       } else { n         y -= n; n         if (!y) n           cout << tmp <<     << ans[0].first <<   n ; n         else n           cout << tmp <<     << ans[y % nw].first <<   n ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int powP[2001] = {1}; n int invPowP[2001] = {1}; n int hashW[2001] = {0}; n vector<pair<int, int> > hashTable[100003]; n void findPos(string& w, string& subW, vector<int>& posSubW) { n   unsigned int j = 0, k = subW.size(); n   for (unsigned int i = 0; (i + k) <= w.size(); i++) { n     while (j < subW.size() && w[i] == subW[j]) { n       i++; n       j++; n     } n     if (j == subW.size()) posSubW.push_back(i - j + 1); n     if (j > 0) { n       i -= j; n       j = 0; n     } n   } n } n void fillHash(string& w) { n   for (unsigned int i = 0; i < w.size(); i++) n     hashW[i + 1] = n         (hashW[i] + (((long long)w[i] - 96) * (long long)powP[i]) % 100003) % n         100003; n } n int myMod(int a, int m) { n   if (a >= 0) return a % m; n   return m * ((-a / m) + 1) + a; n } n int getHash(int i, int j) { n   int hashVal = myMod(hashW[j] - hashW[i - 1], 100003), n       inverse = invPowP[i - 1]; n   return ((long long)hashVal * (long long)inverse) % (long long)100003; n   ; n } n inline int modPow(long long a, int b, int m) { n   long long ans = 1; n   while (b > 0) { n     if (b & 1) ans = (ans * a) % m; n     a = (a * a) % m; n     b >>= 1; n   } n   return ans; n } n int getInverse(int a, int m) { return modPow(a, m - 2, m); } n bool cmpHash(unsigned int wSize, pair<int, int>& a, pair<int, int> b) { n   unsigned int i = 0, j = wSize, mid; n   while (i < j) { n     mid = (i + j) / 2; n     if (getHash(a.first, a.first + mid) != getHash(b.first, b.first + mid)) n       return false; n     else n       i = mid + 1; n   } n   return true; n } n int main() { n   vector<int> posS, posE; n   string w, s, e; n   for (unsigned int i = 1; i < 2001; i++) n     powP[i] = (((long long)powP[i - 1] * (long long)37)) % (long long)100003; n   for (unsigned int i = 1; i < 2001; i++) n     invPowP[i] = getInverse(powP[i], 100003); n   cin >> w >> s >> e; n   findPos(w, s, posS); n   findPos(w, e, posE); n   fillHash(w); n   int cont = 0, hashVal; n   bool found = false; n   for (unsigned int i = 0; i < posS.size(); i++) n     for (unsigned int j = 0; j < posE.size(); j++) { n       if (posS[i] <= posE[j] && n           (posS[i] + s.size() - 1) <= (posE[j] + e.size() - 1)) { n         found = 0; n         hashVal = getHash(posS[i], posE[j] + e.size() - 1); n         for (unsigned int k = 0; k < hashTable[hashVal].size(); k++) { n           if ((hashTable[hashVal][k].second - hashTable[hashVal][k].first) == n               (posE[j] - posS[i])) { n             if (cmpHash(1 + posE[j] - posS[i], hashTable[hashVal][k], n                         make_pair(posS[i], posE[j]))) { n               found = true; n               break; n             } n           } n         } n         if (!found) { n           cont++; n           hashTable[hashVal].push_back(make_pair(posS[i], posE[j])); n         } n       } n     } n   cout << cont << endl; n   return 0; n } n


#include <bits/stdc++.h> n const long long mod = 1e8; n using namespace std; n bool in(int x, int l, int r) { return l <= x && x <= r; } n void solve() { n   long long l, r; n   cin >> l >> r; n   vector<long long> v; n   long long p = -1; n   for (int i = 61; i >= 0; i--) { n     unsigned long long mask = 1ull << i; n     if (mask & r) { n       v.push_back(i); n       if (mask & l) { n         continue; n       } else { n         p = i; n         break; n       } n     } n   } n   long long ans = 0; n   if (v.size() == 0) { n     cout << l <<   n ; n     return; n   } n   int q = 0; n   for (int i = p - 1; i >= 0; i--) { n     unsigned long long mak = 1ull << i; n     if (mak & r) q++; n   } n   if (q == p) { n     cout << r <<   n ; n     return; n   } n   for (int i = 0; i < v.size(); i++) { n     if (v[i] != p) ans += pow(2, v[i]); n   } n   for (int i = p - 1; i >= 0; i--) ans += pow(2, i); n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T &x) { n   x = 0; n   bool f = 0; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = 1; n   for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); n   if (f) x = -x; n } n template <typename T> n inline void Mx(T &x, T y) { n   x < y && (x = y); n } n template <typename T> n inline void Mn(T &x, T y) { n   x > y && (x = y); n } n long long T, n, l, r; n int main() { n   read(T); n   while (T--) { n     read(n), read(l), read(r); n     if (n == 2) { n     } n     long long sum = 0, pre = 0; n     for (int i = 1; i < n; i++) { n       sum += (n - i) * 2; n       if (l < sum) { n         long long t = l - pre; n         long long x = (t & 1) ? i : (t / 2) + i, y = (t / 2) + i; n         while (l < sum && l <= r) { n           printf( %lld  , x); n           x == i ? y++, x = y : x = i; n           l++; n         } n       } n       if (l == sum && l <= r) printf( %lld  , n), l++; n       pre = sum; n       if (l > r) break; n     } n     if (l <= r) printf( 1 ); n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const int maxn = 100; n int b, d, lenA, lenC; n string a, c; n int dp[maxn + 10], nex[maxn + 10]; n void cope(int x) { n   dp[x] = 0; n   nex[x] = x; n   int use = 0; n   while (use < lenA) { n     if (a[use] == c[nex[x]]) { n       if (++nex[x] == lenC) { n         dp[x]++; n         nex[x] = 0; n       } n     } n     use++; n   } n } n int main() { n   std::ios::sync_with_stdio(false); n   while (cin >> b >> d) { n     cin >> a >> c; n     lenA = a.length(), lenC = c.length(); n     for (int(i) = 0; (i) < (lenC); (i)++) cope(i); n     int x = 0, num = 0; n     for (int(i) = 0; (i) < (b); (i)++) { n       num += dp[x]; n       x = nex[x]; n     } n     num /= d; n     cout << num << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, m, i, j, k, ct; n   bool swapped = false; n   cin >> n >> m; n   if (n < m) { n     swap(n, m); n     swapped = true; n   } n   vector<vector<int>> ans(n, vector<int>(m, -1)); n   ct = 0; n   if (m == 1) { n     for (i = 1; i < n; i += 2) ans[i][0] = ++ct; n     for (i = 0; i < n; i += 2) ans[i][0] = ++ct; n   } else { n     for (i = 0; i < n; i++) { n       for (j = i % 2; j < m; j += 2) ans[i][j] = ++ct; n     } n     for (i = 0; i < n; i++) { n       for (j = 1 - (i % 2); j < m; j += 2) ans[i][j] = ++ct; n     } n   } n   bool solved = true; n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) { n       if ((i < n - 1) && (abs(ans[i][j] - ans[i + 1][j]) == 1)) solved = false; n       if ((j < m - 1) && (abs(ans[i][j] - ans[i][j + 1]) == 1)) solved = false; n     } n   } n   if (!solved) n     cout <<  -1 n ; n   else { n     if (!swapped) { n       for (i = 0; i < n; i++) { n         for (j = 0; j < m; j++) cout << ans[i][j] <<    ; n         cout <<   n ; n       } n     } else { n       for (j = 0; j < m; j++) { n         for (i = 0; i < n; i++) cout << ans[i][j] <<    ; n         cout <<   n ; n       } n     } 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(0); n   cout.tie(0); n   int t = 1; n   while (t--) { n     unsigned long long int a, b, c, d; n     cin >> a >> b >> c >> d; n     unordered_map<unsigned long long int, unsigned long long int> mp, mp1; n     for (int i = 0; i <= 100000; i++) { n       ++mp[b + (i * a)]; n       ++mp1[d + (i * c)]; n     } n     for (int i = 0; i <= 100000; i++) { n       if (mp[i] && mp1[i]) { n         cout << i; n         return 0; n       } n     } n     cout << -1; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x, y; n   cin >> n >> x >> y; n   int people = n - x; n   int wizards = x; n   people *= 100; n   wizards *= 100; n   int needed = max(0, n * y - wizards); n   cout << (needed + 99) / 100 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[400500], b[400500], n, tot; n long long read() { n   long long x = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n int main() { n   n = read(); n   tot = 0; n   for (int i = 1; i <= n; i++) { n     int x = read(); n     if (x) a[++tot] = x; n   } n   tot = 0; n   for (int i = 1; i <= n; i++) { n     int x = read(); n     if (x) b[++tot] = x; n   } n   for (int i = n; i <= 2 * n - 2; i++) b[i] = b[i - (n - 1)]; n   n--; n   int loc = 0; n   for (int i = 1; i <= n; i++) n     if (a[1] == b[i]) loc = i; n   int flag = 0; n   for (int i = loc, j = 1; j <= n; i++, j++) n     if (b[i] != a[j]) { n       flag = 1; n       break; n     } n   if (!flag) n     puts( YES ); n   else n     puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<string> v; n   vector<int> vn; n   for (int i = 0; i < n; i++) { n     string a; n     cin >> a; n     bool cn = false; n     int y = 0; n     for (int j = 0; j < v.size(); j++) { n       if (a == v[j]) { n         y = j; n         cn = true; n         break; n       } n     } n     if (cn) { n       vn[y]++; n     } else { n       v.push_back(a); n       vn.push_back(1); n     } n   } n   int rpta = vn[0]; n   for (int i = 1; i < vn.size(); i++) { n     rpta = max(rpta, vn[i]); n   } n   cout << rpta <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool vowel(char c) { n   return (c ==  a  || c ==  e  || c ==  o  || c ==  i  || c ==  u ); n } n int cvowel(string& s) { n   int n = 0; n   for (char& c : s) { n     n += vowel(c); n   } n   return n; n } n int getlast(string& s) { n   for (int i = s.size() - 1;; i--) { n     if (vowel(s[i])) { n       return s[i]; n     } n   } n } n const int KEK = 1000001; n vector<string> ss[KEK]; n vector<pair<char, int>> vv[KEK]; n int main() { n   ios::sync_with_stdio(0); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     string s; n     cin >> s; n     int cnt = cvowel(s); n     ss[cnt].push_back(s); n     vv[cnt].push_back(make_pair(getlast(s), ss[cnt].size() - 1)); n   } n   vector<pair<string, string>> q1, q2; n   for (int i = 1; i < KEK; i++) { n     sort(vv[i].begin(), vv[i].end()); n     int m = vv[i].size(); n     vector<bool> inpair(m, false); n     for (int j = 1; j < m; j++) { n       if (vv[i][j - 1].first == vv[i][j].first) { n         inpair[j - 1] = inpair[j] = true; n         q2.push_back( n             make_pair(ss[i][vv[i][j - 1].second], ss[i][vv[i][j].second])); n         j++; n       } n     } n     int last = -1; n     for (int j = 0; j < m; j++) { n       if (inpair[j] == false) { n         if (last == -1) { n           last = j; n         } else { n           q1.push_back( n               make_pair(ss[i][vv[i][last].second], ss[i][vv[i][j].second])); n           last = -1; n         } n       } n     } n   } n   while (q2.size() > q1.size()) { n     q1.push_back(q2.back()); n     q2.pop_back(); n   } n   while (q1.size() > q2.size()) { n     q1.pop_back(); n   } n   cout << q1.size() << endl; n   for (int i = 0; i < q1.size(); i++) { n     cout << q1[i].first <<     << q2[i].first <<   n ; n     cout << q1[i].second <<     << q2[i].second <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, c, a[2], r = 0; n int main() { n   cin >> n >> c >> a[0]; n   for (int i = 1; i < n; i++) n     cin >> a[i % 2], r = max(r, a[i % 2 ^ 1] - a[i % 2] - c); n   cout << r; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> tab(n); n   for (int i = 0; i < n; i++) cin >> tab[i]; n   for (int i = 0; i < n; i++) { n     if (tab[i] % 2 == 0) n       continue; n     else { n       if (i == (n - 1) || tab[i + 1] == 0) { n         cout <<  NO ; n         return 0; n       } else n         tab[i + 1] -= 1; n     } n   } n   cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, a, b, x, cnt, ans; n deque<long long> V; n map<long long, long long> mp; n long long arr[1000001]; n int main() { n   cin >> n >> a >> b; n   for (int i = 1; i <= n; i++) { n     cin >> arr[i]; n   } n   sort(arr + 1, arr + n + 1); n   ans = (n - a) + 1; n   x = arr[ans] - arr[b]; n   cout << x << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000000; n const int MAXN = 200001; n int c[MAXN * 3 + 10]; n int n; n char s[MAXN + 10]; n int sum[MAXN + 10]; n int lowbit(int x) { return x & (-x); } n void update(int pos, int delta) { n   while (pos <= n) { n     c[pos] = min(c[pos], delta); n     pos += lowbit(pos); n   } n } n int query(int end) { n   int res = INF; n   while (end > 0) { n     res = min(res, c[end]); n     end -= lowbit(end); n   } n   return res; n } n bool isvowel(char x) { n   if (x ==  a  || x ==  e  || x ==  i  || x ==  o  || x ==  u ) return true; n   if (x ==  A  || x ==  E  || x ==  I  || x ==  O  || x ==  U ) return true; n   return false; n } n int main() { n   scanf( %s , s); n   int len = strlen(s); n   n = 3 * len + 1; n   for (int i = 0; i <= n; i++) { n     c[i] = INF; n   } n   memset(sum, 0, sizeof(sum)); n   for (int i = 0; i < len; i++) { n     sum[i + 1] = sum[i] + (isvowel(s[i]) ? -1 : 2); n   } n   int ret = -1; n   update(sum[0] + len + 1, 0); n   for (int i = 1; i <= len; i++) { n     int p = query(sum[i] + len + 1); n     if (p != INF && i - p > ret) { n       ret = i - p; n     } n     update(sum[i] + len + 1, i); n   } n   if (ret == -1) { n     printf( No solution n ); n   } else { n     int count = 0; n     for (int i = 0; i <= len; i++) { n       if (i + ret > len) break; n       if (sum[i + ret] - sum[i] >= 0) { n         count++; n       } n     } n     printf( %d %d n , ret, count); 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(0); n   cout.tie(0); n   int n, n1, n2, limiter = 1, fight = 0, temp; n   cin >> n >> n1; n   queue<int> p1, p2; n   for (int i = 1; i <= n1; i++) { n     cin >> temp; n     p1.push(temp); n     limiter *= i; n   } n   cin >> n2; n   for (int i = 1; i <= n2; i++) { n     cin >> temp; n     p2.push(temp); n     limiter *= (n1 + i); n   } n   limiter *= (n + 1); n   while (1) { n     if (fight > limiter) { n       cout << -1; n       break; n     } else if (p1.empty()) { n       cout << fight <<     << 2; n       break; n     } else if (p2.empty()) { n       cout << fight <<     << 1; n       break; n     } else { n       if (p1.front() > p2.front()) { n         p1.push(p2.front()); n         p1.push(p1.front()); n         p1.pop(); n         p2.pop(); n         fight++; n       } else { n         p2.push(p1.front()); n         p2.push(p2.front()); n         p2.pop(); n         p1.pop(); n         fight++; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int max_n = 200002, inf = 1111111111; n const int max_p = 500005; n int n, q, a[max_n], z[max_p], cnt[max_p], used[max_n]; n long long ans; n int get_cnt(int x) { n   int res = 0; n   for (int i = 1; i * i <= x; ++i) { n     if (x % i == 0) { n       int d = i; n       res += cnt[d] * z[d]; n       d = x / i; n       if (i < d) { n         res += cnt[d] * z[d]; n       } n     } n   } n   return res; n } n void add(int x, int f) { n   for (int i = 1; i * i <= x; ++i) { n     if (x % i == 0) { n       int d = i; n       cnt[d] += f; n       d = x / i; n       if (i < d) { n         cnt[d] += f; n       } n     } n   } n } n int main() { n   for (int i = 2; i < max_p; ++i) { n     int x = i; n     for (int j = 2; j * j <= x; ++j) { n       if (x % j == 0) { n         x /= j; n         ++z[i]; n         if (x % j == 0) { n           z[i] = 0; n           x = 0; n           break; n         } n       } n     } n     if (x == 0) { n       continue; n     } n     if (x > 1) { n       ++z[i]; n     } n     if (z[i] % 2 == 1) { n       z[i] = 1; n     } else { n       z[i] = -1; n     } n   } n   scanf( %d%d , &n, &q); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &a[i]); n   } n   int sz = 0; n   while (q--) { n     int x; n     scanf( %d , &x); n     --x; n     int res; n     if (used[x]) { n       add(a[x], -1); n       used[x] = 0; n       --sz; n       ans -= (sz - get_cnt(a[x])); n     } else { n       used[x] = 1; n       ans += sz - get_cnt(a[x]); n       add(a[x], 1); n       ++sz; n     } n     printf( %I64d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void aent(long long int *a, long long int n) { n   for (long long int i = 0; i < n; i++) cin >> a[i]; n } n string arrtostr(long long int *a, long long int n) { n   ostringstream os; n   for (long long int i = 0; i < n; i++) os << a[i]; n   string str(os.str()); n   return str; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   long long int n; n   cin >> n; n   vector<long long int> v; n   for (long long int i = 0; i < n; i++) { n     long long int a; n     cin >> a; n     v.push_back(a); n   } n   long long int s = 0, e = n - 1; n   string ss =   ; n   long long int mi = LLONG_MIN; n   while (s <= e) { n     if (s == e) { n       if (mi < v[s]) ss +=  L ; n       break; n     } n     long long int st = v[s]; n     long long int ed = v[e]; n     if (st < mi && ed < mi) break; n     if (st == ed) { n       string s1 =  L , s2 =  R ; n       int rs = s; n       if (st < mi) break; n       int chk = st; n       while (s + 1 < e) { n         if (v[s + 1] > chk) { n           s1 +=  L ; n           chk = v[s + 1]; n           s++; n         } else n           break; n       } n       chk = ed; n       while (e - 1 > rs) { n         if (v[e - 1] > chk) { n           s2 +=  R ; n           chk = v[e - 1]; n           e--; n         } else n           break; n       } n       if (s1.size() >= s2.size()) { n         ss += s1; n       } else n         ss += s2; n       cout << ss.size() <<   n ; n       cout << ss; n       return 0; n     } n     if (ed < st) { n       if (ed > mi) { n         ss +=  R ; n         e--; n         mi = ed; n       } else { n         if (st > mi) { n           ss +=  L ; n           s++; n           mi = st; n         } else n           break; n       } n     } else { n       if (st > mi) { n         ss +=  L ; n         s++; n         mi = st; n       } else { n         if (ed > mi) { n           ss +=  R ; n           e--; n           mi = ed; n         } else n           break; n       } n     } n   } n   cout << ss.size() <<   n ; n   cout << ss; n } n


#include <bits/stdc++.h> n using namespace std; n inline char read() { n   static const int IN_LEN = 1000000; n   static char buf[IN_LEN], *s, *t; n   return (s == t               ? t = (s = buf) + fread(buf, 1, IN_LEN, stdin), n           (s == t ? -1 : *s++) : *s++); n } n template <class T> n inline void read(T &x) { n   static bool iosig; n   static char c; n   for (iosig = false, c = read(); !isdigit(c); c = read()) { n     if (c ==  - ) iosig = true; n     if (c == -1) return; n   } n   for (x = 0; isdigit(c); c = read()) x = ((x + (x << 2)) << 1) + (c ^  0 ); n   if (iosig) x = -x; n } n const int OUT_LEN = 10000000; n char obuf[OUT_LEN], *ooh = obuf; n inline void print(char c) { n   if (ooh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), ooh = obuf; n   *ooh++ = c; n } n template <class T> n inline void print(T x) { n   static int buf[30], cnt; n   if (x == 0) n     print( 0 ); n   else { n     if (x < 0) print( - ), x = -x; n     for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48; n     while (cnt) print((char)buf[cnt--]); n   } n } n inline void print(pair<int, int> x) { n   print(x.first), print(   ), print(x.second), print(  n ); n } n inline void flush() { fwrite(obuf, 1, ooh - obuf, stdout); } n const int N = 100005, M = 52; n const long long inf = 1e16; n int n, m, q, x[N], y[N], xx[N], yy[N], X[N], Y[N], fa[N], fa1[N], f1[N], n     s[N << 2], f[N][M]; n long long T[N], ga[N], g[N][M]; n char opt[N], op[N]; n vector<int> a[N], b[N], c[N]; n void build(int l, int r, int t) { n   s[t] = 0; n   if (l == r) return; n   int mid = (l + r) >> 1, k = t << 1; n   build(l, mid, k), build(mid + 1, r, k | 1); n } n void change(int l, int r, int t, int L, int R, int x) { n   if (L <= l && r <= R) return (void)(s[t] = x); n   int mid = (l + r) >> 1, k = t << 1; n   if (s[t]) s[k] = s[k | 1] = s[t], s[t] = 0; n   if (L <= mid) change(l, mid, k, L, R, x); n   if (R > mid) change(mid + 1, r, k | 1, L, R, x); n } n int query(int l, int r, int t, int x) { n   if (l == r || s[t]) return s[t]; n   int mid = (l + r) >> 1, k = t << 1; n   return (x <= mid ? query(l, mid, k, x) : query(mid + 1, r, k | 1, x)); n } n inline pair<int, int> solve(pair<int, int> x, char o, long long t) { n   int a = x.first, b = x.second; n   if (o ==  L ) a = max(0ll, a - t); n   if (o ==  R ) a = min((long long)m, a + t); n   if (o ==  D ) b = max(0ll, b - t); n   if (o ==  U ) b = min((long long)m, b + t); n   return make_pair(a, b); n } n signed main() { n   read(n), read(m); n   for (int i = 1; i <= n; ++i) { n     read(x[i]), read(y[i]), read(xx[i]), read(yy[i]); n     if (x[i] == xx[i]) n       a[x[i]].push_back(i), op[i] = (y[i] > yy[i] ?  D  :  U ); n     else n       b[x[i]].push_back(i), b[xx[i]].push_back(i), n           op[i] = (x[i] > xx[i] ?  L  :  R ); n   } n   read(q); n   for (int i = 1; i <= q; ++i) { n     read(X[i]), read(Y[i]); n     while (isspace(opt[i] = read())) n       ; n     read(T[i]); n   } n   for (int i = 1; i <= q; ++i) n     if (opt[i] ==  R ) c[X[i]].push_back(i); n   build(0, m, 1); n   for (int i = m; ~i; --i) { n     for (int j : a[i]) change(0, m, 1, min(y[j], yy[j]), max(y[j], yy[j]), j); n     for (int j : b[i]) n       if (x[j] > xx[j]) n         change(0, m, 1, y[j], y[j], j); n       else n         f[j][0] = query(0, m, 1, y[j]), n         g[j][0] = (f1[j] = abs(xx[f[j][0]] - xx[j])) + abs(yy[f[j][0]] - y[j]); n     for (int j : c[i]) { n       fa[j] = query(0, m, 1, Y[j]), n       ga[j] = abs(xx[fa[j]] - X[j]) + abs(yy[fa[j]] - Y[j]); n       fa1[j] = (x[fa[j]] == xx[fa[j]] ? abs(xx[fa[j]] - X[j]) n                                       : max(xx[fa[j]] - X[j], 0)); n     } n   } n   for (int i = 0; i <= m; ++i) c[i].clear(); n   for (int i = 1; i <= q; ++i) n     if (opt[i] ==  L ) c[X[i]].push_back(i); n   build(0, m, 1); n   for (int i = 0; i <= m; ++i) { n     for (int j : a[i]) change(0, m, 1, min(y[j], yy[j]), max(y[j], yy[j]), j); n     for (int j : b[i]) n       if (x[j] < xx[j]) n         change(0, m, 1, y[j], y[j], j); n       else n         f[j][0] = query(0, m, 1, y[j]), n         g[j][0] = (f1[j] = abs(xx[j] - xx[f[j][0]])) + abs(yy[f[j][0]] - y[j]); n     for (int j : c[i]) { n       fa[j] = query(0, m, 1, Y[j]), n       ga[j] = abs(X[j] - xx[fa[j]]) + abs(yy[fa[j]] - Y[j]); n       fa1[j] = (x[fa[j]] == xx[fa[j]] ? abs(xx[fa[j]] - X[j]) n                                       : max(X[j] - xx[fa[j]], 0)); n     } n   } n   for (int i = 0; i <= m; ++i) c[i].clear(), a[i].clear(), b[i].clear(); n   for (int i = 1; i <= n; ++i) n     if (y[i] == yy[i]) n       a[y[i]].push_back(i); n     else n       b[y[i]].push_back(i), b[yy[i]].push_back(i); n   for (int i = 1; i <= q; ++i) n     if (opt[i] ==  U ) c[Y[i]].push_back(i); n   build(0, m, 1); n   for (int i = m; ~i; --i) { n     for (int j : a[i]) change(0, m, 1, min(x[j], xx[j]), max(x[j], xx[j]), j); n     for (int j : b[i]) n       if (y[j] > yy[j]) n         change(0, m, 1, x[j], x[j], j); n       else n         f[j][0] = query(0, m, 1, x[j]), n         g[j][0] = (f1[j] = abs(yy[f[j][0]] - yy[j])) + abs(xx[f[j][0]] - x[j]); n     for (int j : c[i]) { n       fa[j] = query(0, m, 1, X[j]), n       ga[j] = (fa1[j] = abs(yy[fa[j]] - Y[j])) + abs(xx[fa[j]] - X[j]); n       fa1[j] = (x[fa[j]] == xx[fa[j]] ? max(yy[fa[j]] - Y[j], 0) n                                       : abs(yy[fa[j]] - Y[j])); n     } n   } n   for (int i = 0; i <= m; ++i) c[i].clear(); n   for (int i = 1; i <= q; ++i) n     if (opt[i] ==  D ) c[Y[i]].push_back(i); n   build(0, m, 1); n   for (int i = 0; i <= m; ++i) { n     for (int j : a[i]) change(0, m, 1, min(x[j], xx[j]), max(x[j], xx[j]), j); n     for (int j : b[i]) n       if (y[j] < yy[j]) n         change(0, m, 1, x[j], x[j], j); n       else n         f[j][0] = query(0, m, 1, x[j]), n         g[j][0] = (f1[j] = abs(yy[j] - yy[f[j][0]])) + abs(xx[f[j][0]] - x[j]); n     for (int j : c[i]) { n       fa[j] = query(0, m, 1, X[j]), n       ga[j] = (fa1[j] = abs(Y[j] - yy[fa[j]])) + abs(xx[fa[j]] - X[j]); n       fa1[j] = (x[fa[j]] == xx[fa[j]] ? max(Y[j] - yy[fa[j]], 0) n                                       : abs(yy[fa[j]] - Y[j])); n     } n   } n   for (int j = 1; j <= 50; ++j) n     for (int i = 1; i <= n; ++i) n       f[i][j] = f[f[i][j - 1]][j - 1], n       g[i][j] = min(inf, g[i][j - 1] + g[f[i][j - 1]][j - 1]); n   for (int i = 1; i <= q; ++i) { n     if (!fa[i] || T[i] < fa1[i]) n       print(solve(make_pair(X[i], Y[i]), opt[i], T[i])); n     else if (T[i] < ga[i]) n       print(solve(solve(make_pair(X[i], Y[i]), opt[i], fa1[i]), op[fa[i]], n                   T[i] - fa1[i])); n     else { n       T[i] -= ga[i]; n       int now = fa[i]; n       for (int j = 50; ~j; --j) n         if (f[now][j] && g[now][j] <= T[i]) T[i] -= g[now][j], now = f[now][j]; n       if (!f[now][0] || T[i] < f1[now]) n         print(solve(make_pair(xx[now], yy[now]), op[now], T[i])); n       else n         print(solve(solve(make_pair(xx[now], yy[now]), op[now], f1[now]), n                     op[f[now][0]], T[i] - f1[now])); n     } n   } n   return flush(), 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, i; n     cin >> n; n     int a[n]; n     map<long int, int> mp; n     for (i = 0; i < n; i++) { n       cin >> a[i]; n       mp[a[i]]++; n     } n     cout << mp.size() << endl; n   } n } n


#include <bits/stdc++.h> n const int N = (int)1e3 + 5; n int n; n int a[N]; n int b[N]; n int u[N]; n int gcd(int a, int b) { return !b ? a : gcd(b, a % b); } n int main() { n   int ts; n   scanf( %d , &ts); n   while (ts--) { n     for (int i = 0; i < N; ++i) { n       u[i] = 0; n     } n     scanf( %d , &n); n     for (int i = 0; i < n; ++i) { n       scanf( %d , &a[i]); n     } n     int g = 0; n     for (int p = 1; p <= n; ++p) { n       int mx = -1; n       int ind = -1; n       for (int i = 0; i < n; ++i) { n         if (!u[i]) { n           if (mx < gcd(g, a[i])) { n             mx = gcd(g, a[i]); n             ind = i; n           } n         } n       } n       assert(ind != -1); n       u[ind] = 1; n       g = gcd(g, a[ind]); n       b[p] = a[ind]; n     } n     for (int i = 1; i <= n; ++i) { n       printf( %d  , b[i]); n     } n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n  n void ng() { cout << -1 <<   n ; } n  n void solve() { n     int N; cin >> N; n     map<int, int> M; n     vector<int> A(N); n     for (auto &x: A) { n         cin >> x; M[x]++; n     } n     for (auto &[key, val]: M) { n         if (val > (N + 1) / 2) return ng(); n     } n     int l = 0; n     vector<pair<int, int>> V; n     for (int i = 0; i < N-1; i++) { n         if (A[i] == A[i+1]) { n             V.emplace_back(A[l], A[i]); n             l = i+1; n         } n     } n     V.emplace_back(A[l], A[N-1]); n     M.clear(); n     int sum = V.size(); n     int sum2 = 0; n     map<int, int> P; n     for (auto &[c, d]: V) { n         if (c == d) P[c]++, sum2++; n         else M[c]++, M[d]++; n     } n     sum -= sum2; n     for (auto &[key, val]: P) { n         if (sum - M[key] + 1 + (sum2 - val) < val) { n             int a = val - (sum - M[key] + 1 + (sum2 - val)); n             cout << V.size() - 1 + a <<   n ; n             return; n         } n     } n     cout << V.size() - 1 <<   n ; n } n  n int main() { n     cin.tie(nullptr); n     ios::sync_with_stdio(false); n     int T; cin >> T; n     while (T--) solve(); n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n int n, m; n int a[100]; n int sol[1000000]; n void prueba(int ind) { n   sol[0] = ind; n   priority_queue<pair<int, int> > q; n   for (int i = 1; i <= m; i++) { n     if (ind == i) { n       if (a[i] > 1) q.push(pair<int, int>(a[i] - 1, i)); n     } else { n       q.push(pair<int, int>(a[i], i)); n     } n   } n   for (int i = 1; i < n; i++) { n     pair<int, int> p = q.top(); n     q.pop(); n     if (i < n - 1) { n       if (p.second != sol[i - 1]) { n         sol[i] = p.second; n         p.first--; n         if (p.first) q.push(p); n       } else if (int(q.size()) == 0) { n         return; n       } else { n         pair<int, int> pbis = q.top(); n         q.pop(); n         sol[i] = pbis.second; n         pbis.first--; n         if (pbis.first) q.push(pbis); n         q.push(p); n       } n     } else { n       if (p.second != sol[i - 1] and p.second != sol[0]) { n         sol[i] = p.second; n         p.first--; n         if (p.first) q.push(p); n       } else if (int(q.size()) == 0) { n         return; n       } else { n         pair<int, int> pbis = q.top(); n         q.pop(); n         if (pbis.second != sol[i - 1] and pbis.second != sol[0]) { n           sol[i] = pbis.second; n           pbis.first--; n           if (pbis.first) q.push(pbis); n           q.push(p); n         } else if (int(q.size()) == 0) { n           return; n         } else { n           pair<int, int> pbisbis = q.top(); n           q.pop(); n           sol[i] = pbisbis.second; n           pbisbis.first--; n           if (pbisbis.first) q.push(pbisbis); n           q.push(p); n           q.push(pbis); n         } n       } n     } n   } n   for (int i = 0; i < n; i++) { n     if (i > 0) cout <<    ; n     cout << sol[i]; n   } n   cout << endl; n   exit(0); n } n int main() { n   cin >> n >> m; n   int suma = 0; n   for (int i = 1; i <= m; i++) { n     cin >> a[i]; n     suma += a[i]; n   } n   if (suma < n) { n     cout << -1 << endl; n     exit(0); n   } n   for (int i = 1; i <= m; i++) prueba(i); n   cout << -1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int nmax = 5e4 + 5; n int l[nmax], r[nmax]; n int main() { n   int n, x; n   scanf( %d %d , &n, &x); n   for (int i = 0; i < n; ++i) { n     scanf( %d %d , l + i, r + i); n   } n   long long res = 0; n   for (int i = 0; i < n;) { n     if (!(l[i] <= x && r[i] >= x)) { n       int lc = 0, rc = 0; n       int lc2 = 0, rc2 = 0; n       int j = i; n       while ((lc < 1 || rc < 1) && lc2 < 2 && rc2 < 2) { n         if (r[j] <= x) ++lc; n         if (l[j] >= x) ++rc; n         if (r[j] < x) ++lc2; n         if (l[j] > x) ++rc2; n         ++j; n       } n       if (lc && rc) { n         if (l[i] > x) { n           res += l[i] - x; n         } n         if (r[i] < x) { n           res += x - r[i]; n         } n         ++i; n       } else if (lc2 == 2) { n         int nxt = 0; n         for (int k = i; k < j; ++k) { n           nxt = max(nxt, l[k]); n           if (r[k] < x) { n             nxt = max(nxt, r[k]); n           } n         } n         res += x - nxt; n         x = nxt; n       } else if (rc2 == 2) { n         int nxt = mod; n         for (int k = i; k < j; ++k) { n           nxt = min(nxt, r[k]); n           if (l[k] > x) { n             nxt = min(nxt, l[k]); n           } n         } n         res += nxt - x; n         x = nxt; n       } n     } else { n       ++i; n     } n   } n   printf( %lld n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void read(T &x) { n   x = 0; n   register char c = getchar(); n   bool f = 0; n   while (c !=  -  && (c <  0  || c >  9 )) c = getchar(); n   if (c ==  - ) c = getchar(), f = 1; n   while (c >=  0  && c <=  9 ) x = x * 10 + c - 48, c = getchar(); n   x = f ? -x : x; n   return; n } n template <class T> n inline void write(T x) { n   if (!x) { n     putchar(48); n     return; n   } n   if (x < 0) x = -x, putchar( - ); n   int len = -1, z[20]; n   while (x > 0) z[++len] = x % 10, x /= 10; n   for (register int i = len; i >= 0; i--) putchar(z[i] + 48); n   return; n } n const int N = 10010; n struct node { n   int to, nxt, w; n } g[N * 10]; n int lst[N], gl; n void add(int x, int y, int z) { n   g[++gl] = (node){y, lst[x], z}; n   lst[x] = gl; n } n vector<pair<int, int> > E[N]; n vector<int> E2[N]; n bool vis1[N], visn[N]; n void dfs(int x) { n   vis1[x] = 1; n   for (int i = 0; i < E[x].size(); i++) { n     int v = E[x][i].first; n     if (!vis1[v]) dfs(v); n   } n } n void dfs2(int x) { n   visn[x] = 1; n   for (int i = 0; i < E2[x].size(); i++) { n     int v = E2[x][i]; n     if (!visn[v]) dfs2(v); n   } n } n int cnt[N], dis[N], n, m; n queue<int> q; n bool vis[N]; n bool spfa() { n   q.push(1); n   memset(dis, 127 / 3, sizeof(dis)); n   dis[1] = 0; n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     vis[u] = 0; n     for (int i = lst[u]; i; i = g[i].nxt) { n       int v = g[i].to; n       if (dis[v] > dis[u] + g[i].w) { n         dis[v] = dis[u] + g[i].w; n         if ((++cnt[v]) == n) return 0; n         if (!vis[v]) { n           vis[v] = 1; n           q.push(v); n         } n       } n     } n   } n   return 1; n } n int ans[N]; n int main() { n   read(n), read(m); n   for (int i = 1, x, y; i <= m; i++) { n     read(x), read(y); n     E[x].push_back(make_pair(y, i)); n     E2[y].push_back(x); n   } n   dfs(1); n   dfs2(n); n   for (int u = 1; u <= n; u++) n     for (auto v : E[u]) n       if (vis1[u] && visn[v.first]) { n         add(v.first, u, 2); n         add(u, v.first, -1); n       } n   if (!spfa()) n     puts( No ); n   else { n     puts( Yes ); n     int flg = 0; n     for (int u = 1; u <= n; u++) n       for (auto v : E[u]) n         if (vis1[u] && visn[v.first]) n           ans[v.second] = dis[u] - dis[v.first]; n         else n           ans[v.second] = 1; n     for (int i = 1; i <= m; i++) printf( %d n , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n template <typename T> n inline T abs(T a) { n   return a < 0 ? -a : a; n } n template <typename T> n inline T min(T a, T b) { n   return a < b ? a : b; n } n template <typename T> n inline T max(T a, T b) { n   return a > b ? a : b; n } n template <typename T> n inline void swap(T& a, T& b) { n   T t = a; n   a = b, b = t; n } n template <typename T> n inline bool umin(T& mn, T u) { n   return u < mn ? mn = u, true : false; n } n template <typename T> n inline bool umax(T& mx, T u) { n   return u > mx ? mx = u, true : false; n } n template <typename T> n inline void read(T& r) { n   char c; n   r = 0; n   bool flag = false; n   do c = getchar(); n   while (c !=  -  && (c <  0  || c >  9 )); n   if (c ==  - ) flag = true, c = getchar(); n   do r = r * 10 + c -  0 , c = getchar(); n   while (c >=  0  && c <=  9 ); n   if (flag) r *= -1; n } n template <typename T1, typename T2> n inline void read(T1& r, T2& s) { n   read(r), read(s); n } n template <typename T1, typename T2, typename T3> n inline void read(T1& r, T2& s, T3& t) { n   read(r), read(s), read(t); n } n template <typename T1, typename T2, typename T3, typename T4> n inline void read(T1& r, T2& s, T3& t, T4& u) { n   read(r), read(s), read(t), read(u); n } n template <typename T> n inline void write(T w, const char* c =   ) { n   if (w == 0) n     putchar( 0 ); n   else { n     if (w < 0) putchar( - ), w *= -1; n     static int s[21], top; n     top = 0; n     while (w) s[top++] = w % 10, w /= 10; n     while (top--) putchar(s[top] +  0 ); n   } n   for (int i = 0; c[i]; i++) putchar(c[i]); n } n int n, k, qn, en; n long long p[10000]; n const long long P = 1e7 + 19, mod = 1e9 + 7; n void init() { n   for (int i = p[0] = 1; i < k; i++) p[i] = p[i - 1] * P % mod; n } n struct knapsack { n   int dp[10001]; n   void add(std::pair<int, int> o) { n     int v = o.first, w = o.second; n     for (int i = k - w; i >= 0; i--) umax(dp[i + w], dp[i] + v); n   } n   int getans() { n     long long ans = 0; n     for (int i = 1; i <= k; i++) (ans += dp[i] * p[i - 1]) %= mod; n     return (int)ans; n   } n } st[160]; n struct perfect_binary_tree { n   struct ac { n     std::pair<int, int> to; n     int nx; n   } e[4500000]; n   int L, first[655360]; n   perfect_binary_tree() : L(1) {} n   inline void addedge(int u, std::pair<int, int> v) { n     e[L].to = v, e[L].nx = first[u], first[u] = L++; n   } n   void add(int L, int R, std::pair<int, int> c, int now = 1, int l = 1, n            int r = qn) { n     if (L <= l && r <= R) { n       addedge(now, c); n       return; n     } n     int m = (l + r) >> 1; n     if (L <= m) add(L, R, c, now << 1, l, m); n     if (m < R) add(L, R, c, now << 1 | 1, m + 1, r); n   } n   void getans(int depth = 1, int now = 1, int l = 1, int r = qn) { n     st[depth] = st[depth - 1]; n     for (int j = first[now]; j; j = e[j].nx) st[depth].add(e[j].to); n     if (l == r) { n       write(st[depth].getans(),   n ); n       return; n     } n     int m = (l + r) >> 1; n     getans(depth + 1, now << 1, l, m); n     getans(depth + 1, now << 1 | 1, m + 1, r); n   } n } T; n std::pair<int, int> exhibit[150000], show[150000]; n void solve() { n   init(); n   for (int i = 0; i < n; i++) n     if (show[i].first <= qn && show[i].second >= 1) n       T.add(show[i].first, show[i].second, exhibit[i]); n   T.getans(); n } n void input() { n   qn = 0, n = 0; n   int m, o; n   read(m, k); n   while (m--) n     read(exhibit[n].first, exhibit[n].second), n         show[n++] = std::pair<int, int>(0, -1); n   read(m); n   while (m--) { n     read(o); n     switch (o) { n       case 1: n         read(exhibit[n].first, exhibit[n].second), n             show[n++] = std::pair<int, int>(qn + 1, -1); n         break; n       case 2: n         read(o), show[--o].second = qn; n         break; n       case 3: n         ++qn; n         break; n     } n   } n   for (int i = 0; i < n; i++) n     if (show[i].second == -1) show[i].second = qn; n } n int main() { n   input(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Upgrade { n   int id, i, val; n   Upgrade(int _id = 0, int _i = 0, int _val = 0) : id(_id), i(_i), val(_val) {} n   friend bool operator<(const Upgrade &a, const Upgrade &b) { n     return a.val < b.val; n   } n }; n struct Data { n   Upgrade upgrade; n   long long nume, deno; n   int id; n   Data(int _id = 0, long long _nume = 0, long long _deno = 0, n        Upgrade _upgrade = Upgrade()) n       : id(_id), nume(_nume), deno(_deno), upgrade(_upgrade) {} n   friend bool operator<(const Data &a, const Data &b) { n     return double(a.nume) / a.deno < double(b.nume) / b.deno; n   } n }; n int n, m, k; n long long a[100010]; n bool is_assg[100010]; n Upgrade assg[100010]; n vector<Upgrade> mult; n vector<Upgrade> add; n vector<Upgrade> add_list[100010]; n priority_queue<Data> q; n double sum_mult[100010], sum; n bool cmp(const Upgrade &a, const Upgrade &b) { return b < a; } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> m >> k; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   memset(assg, -1, sizeof assg); n   for (int i = 1; i <= m; i++) { n     Upgrade upgrade(i); n     int type; n     cin >> type >> upgrade.i >> upgrade.val; n     if (type == 1) { n       is_assg[i] = 1; n       if (assg[upgrade.i] < upgrade) assg[upgrade.i] = upgrade; n     } else if (type == 2) n       add_list[upgrade.i].push_back(upgrade); n     else n       mult.push_back(upgrade); n   } n   for (int i = 1; i <= n; i++) { n     if (assg[i].id != -1) { n       assg[i].val -= a[i]; n       add_list[i].push_back(assg[i]); n     } n     sort(add_list[i].begin(), add_list[i].end()); n     if (!add_list[i].empty()) { n       q.push(Data(i, a[i] + add_list[i].back().val, a[i], add_list[i].back())); n       add_list[i].pop_back(); n     } n   } n   while (!q.empty()) { n     Data data = q.top(); n     q.pop(); n     add.push_back(data.upgrade); n     int i = data.id; n     if (!add_list[i].empty()) { n       q.push(Data(i, data.nume + add_list[i].back().val, n                   data.deno + data.upgrade.val, add_list[i].back())); n       add_list[i].pop_back(); n     } n   } n   sort(mult.begin(), mult.end(), cmp); n   sum_mult[0] = 0; n   for (int i = 1; i <= mult.size(); i++) n     sum_mult[i] = sum_mult[i - 1] + log(mult[i - 1].val); n   sum = 0; n   for (int i = 1; i <= n; i++) sum += log(a[i]); n   double max_sum = sum + sum_mult[min((int)mult.size(), k)]; n   int optimal = 0; n   for (int i = 0; i < add.size() && i < k; i++) { n     sum -= log(a[add[i].i]); n     a[add[i].i] += add[i].val; n     sum += log(a[add[i].i]); n     double total = sum + sum_mult[min((int)mult.size(), k - i - 1)]; n     if (total > max_sum) { n       max_sum = total; n       optimal = i + 1; n     } n   } n   vector<int> trace; n   for (int i = 0; i < optimal; i++) n     if (is_assg[add[i].id]) trace.push_back(add[i].id); n   for (int i = 0; i < optimal; i++) n     if (!is_assg[add[i].id]) trace.push_back(add[i].id); n   for (int i = 0; i < min((int)mult.size(), k - optimal); i++) n     trace.push_back(mult[i].id); n   cout << trace.size() <<   n ; n   for (int i = 0; i < trace.size(); i++) cout << trace[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 10; n bool prime[N]; n int phi[N], deg[N]; n void sieve(); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   sieve(); n   int n, k; n   cin >> n >> k; n   priority_queue<pair<int, int>, vector<pair<int, int>>, n                  greater<pair<int, int>>> n       q; n   for (int i = 3; i <= n; ++i) { n     if (deg[i] == 0) { n       q.push({phi[i], i}); n     } n     for (int j = 2 * i; j <= n; j += i) { n       ++deg[j]; n     } n   } n   long long ans = (k == 1 ? 1 : 2); n   while (k--) { n     auto cur = q.top(); n     q.pop(); n     int i = cur.second; n     ans += phi[i]; n     for (int j = 2 * i; j <= n; j += i) { n       --deg[j]; n       if (deg[j] == 0) { n         q.push({phi[j], j}); n       } n     } n   } n   cout << ans <<   n ; n } n void sieve() { n   for (int i = 2; i < N; ++i) { n     prime[i] = true; n     phi[i] = i; n   } n   for (int i = 2; i < N; ++i) { n     if (prime[i]) { n       for (int j = i; j < N; j += i) { n         prime[j] = false; n         phi[j] /= i; n         phi[j] *= (i - 1); n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 100010; n int n, m, k, q; n vector<int> G[maxN]; n int pre[maxN], s[maxN], e[maxN], cnt[maxN], t; n bool nroot[maxN]; n void dfs(int cur) { n   s[cur] = ++t; n   cnt[cur] = 1; n   for (int i = 0; i < (int)G[cur].size(); i++) { n     int nxt = G[cur][i]; n     dfs(nxt); n     cnt[cur] += cnt[nxt]; n   } n   e[cur] = t; n } n int main(int argc, char** argv) { n   cin >> n >> m >> k >> q; n   int a, b; n   for (int i = 0; i < k; i++) { n     cin >> a >> b; n     if (pre[b]) { n       G[pre[b]].push_back(a); n       nroot[a] = true; n     } n     pre[b] = a; n   } n   for (int i = 1; i <= n; i++) { n     if (!nroot[i]) dfs(i); n   } n   for (int i = 0; i < q; i++) { n     cin >> a >> b; n     cout << ((s[a] < s[pre[b]] && e[a] >= e[pre[b]]) ? cnt[a] : 0) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   vector<string> tt(n); n   for (int i = 0; i < n; ++i) { n     cin >> tt[i]; n   } n   int const INF = 1000000000; n   vector<int> dp(k + 1, INF), ndp(k + 1, INF); n   dp[0] = 0; n   for (int day = 0; day < n; ++day) { n     vector<int> prefix(m + 1); n     for (int i = 0; i < m; ++i) { n       prefix[i + 1] = prefix[i] + (tt[day][i] ==  1 ); n     } n     vector<int> update(m + 1, INF); n     for (int arr = 0; arr <= m; ++arr) { n       for (int dep = arr; dep <= m; ++dep) { n         int plus_time = dep - arr; n         int plus_sk = prefix[m] - (prefix[dep] - prefix[arr]); n         update[plus_sk] = min(update[plus_sk], plus_time); n       } n     } n     for (int sk = 0; sk <= k; ++sk) { n       for (int plus_sk = 0; plus_sk <= m && sk + plus_sk <= k; ++plus_sk) { n         ndp[sk + plus_sk] = min(ndp[sk + plus_sk], dp[sk] + update[plus_sk]); n       } n     } n     dp.assign(k + 1, INF); n     dp.swap(ndp); n   } n   int result = INF; n   for (int sk = 0; sk <= k; ++sk) { n     result = min(result, dp[sk]); n   } n   cout << result << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, id = 0; n int Hash[333], L[333]; n map<string, int> M; n vector<int> v; n int kmp(int Len) { n   int len = v.size(); n   int lps[len], I = 0, num = 0; n   lps[0] = 0; n   for (int i = 1; i < len; i++) { n     if (v[I] != v[i]) I = -1; n     lps[i] = ++I; n   } n   int i = 0, j = 0; n   while (i < n) { n     if (Hash[i] == v[j]) { n       i++; n       j++; n     } else { n       if (j == 0) n         i++; n       else { n         if (j == lps[j]) n           j = 0; n         else n           j = lps[j - 1]; n       } n     } n     if (j == len) { n       num++; n       j = 0; n     } n   } n   if (num == 1) num = 0; n   return (Len - 1) * num; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   string s; n   int Len = 0, res = 0, len; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> s; n     if (!M[s]) { n       M[s] = ++id; n     } n     Hash[i] = M[s]; n     L[i] = s.length(); n     Len += L[i]; n   } n   Len += (n - 1); n   for (int i = 0; i < n; i++) { n     v.clear(); n     len = 0; n     for (int j = i; j < n; j++) { n       v.push_back(Hash[j]); n       len += L[j]; n       res = max(res, kmp(len)); n     } n   } n   cout << Len - res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1010; n int N; n pair<int, int> point[MAXN]; n int cntx[MAXN], cnty[MAXN], cnt1, cnt2; n map<int, vector<pair<int, int> > > samex, samey; n int fa[MAXN]; n int find(int x) { return (x == fa[x]) ? x : fa[x] = find(fa[x]); } n void merge(int u, int v) { n   u = find(u); n   v = find(v); n   if (u != v) fa[u] = v; n } n bool check(long long dis) { n   for (int i = 1; i <= N; i++) fa[i] = i; n   for (int i = 1; i <= cnt1; i++) { n     for (int j = 1; j < samex[cntx[i]].size(); j++) { n       int id1 = samex[cntx[i]][j - 1].second; n       int id2 = samex[cntx[i]][j].second; n       if (point[id2].second - point[id1].second <= dis) merge(id1, id2); n     } n   } n   for (int i = 1; i <= cnt2; i++) { n     for (int j = 1; j < samey[cnty[i]].size(); j++) { n       int id1 = samey[cnty[i]][j - 1].second; n       int id2 = samey[cnty[i]][j].second; n       if (point[id2].first - point[id1].first <= dis) merge(id1, id2); n     } n   } n   int num = 0; n   for (int i = 1; i <= N; i++) num += (i == find(i)); n   if (num > 4) return false; n   if (num == 1) return true; n   if (num == 2) { n     for (int i = 1; i <= N; i++) { n       for (int j = i + 1; j <= N; j++) n         if (fa[i] != fa[j]) { n           if (point[i].first == point[j].first && n               abs(point[i].second - point[j].second) <= dis * 2) n             return true; n           if (point[i].second == point[j].second && n               abs(point[i].first - point[j].first) <= dis * 2) n             return true; n           if (abs(point[i].first - point[j].first) <= dis && n               abs(point[i].second - point[j].second) <= dis) n             return true; n         } n     } n   } else if (num == 3) { n     vector<pair<int, int> > seg; n     for (int i = 1; i <= cnt1; i++) { n       for (int j = 1; j < (int)samex[cntx[i]].size(); j++) { n         int id1 = samex[cntx[i]][j - 1].second; n         int id2 = samex[cntx[i]][j].second; n         if (fa[id1] != fa[id2]) { n           seg.push_back(make_pair(id1, id2)); n         } n       } n     } n     for (int i = 1; i <= cnt2; i++) { n       for (int j = 1; j < (int)samey[cnty[i]].size(); j++) { n         int id1 = samey[cnty[i]][j - 1].second; n         int id2 = samey[cnty[i]][j].second; n         if (fa[id1] != fa[id2]) { n           seg.push_back(make_pair(id1, id2)); n         } n       } n     } n     for (int i = 0; i < (int)seg.size(); i++) { n       int id1 = seg[i].first; n       int id2 = seg[i].second; n       for (int j = 1; j <= N; j++) n         if (fa[id1] != fa[j] && fa[id2] != fa[j]) { n           if (point[id1].first == point[id2].first) { n             if (min(point[id1].second, point[id2].second) >= point[j].second || n                 max(point[id1].second, point[id2].second) <= point[j].second) n               continue; n             if (abs(point[j].first - point[id1].first) <= dis && n                 abs(point[id1].second - point[j].second) <= dis && n                 abs(point[id2].second - point[j].second) <= dis) n               return true; n           } else { n             if (min(point[id1].first, point[id2].first) >= point[j].first || n                 max(point[id1].first, point[id2].first) <= point[j].first) n               continue; n             if (abs(point[j].first - point[id1].first) <= dis && n                 abs(point[id1].second - point[j].second) <= dis && n                 abs(point[j].first - point[id2].first) <= dis) n               return true; n           } n         } n     } n   } else { n     vector<pair<int, int> > segx, segy; n     for (int i = 1; i <= cnt1; i++) { n       for (int j = 1; j < (int)samex[cntx[i]].size(); j++) { n         int id1 = samex[cntx[i]][j - 1].second; n         int id2 = samex[cntx[i]][j].second; n         if (fa[id1] != fa[id2]) segx.push_back(make_pair(id1, id2)); n       } n     } n     for (int i = 1; i <= cnt2; i++) { n       for (int j = 1; j < (int)samey[cnty[i]].size(); j++) { n         int id1 = samey[cnty[i]][j - 1].second; n         int id2 = samey[cnty[i]][j].second; n         if (fa[id1] != fa[id2]) segy.push_back(make_pair(id1, id2)); n       } n     } n     for (int i = 0; i < (int)segx.size(); i++) { n       for (int j = 0; j < (int)segy.size(); j++) { n         int x1 = segx[i].first; n         int y1 = segx[i].second; n         int x2 = segy[j].first; n         int y2 = segy[j].second; n         if (fa[x1] == fa[x2] || fa[x1] == fa[y2] || fa[y1] == fa[x2] || n             fa[y1] == fa[y2]) n           continue; n         if (min(point[x2].first, point[y2].first) >= point[x1].first || n             max(point[x2].first, point[y2].first) <= point[x1].first) n           continue; n         if (min(point[x1].second, point[y1].second) >= point[x2].second || n             max(point[x1].second, point[y1].second) <= point[x2].second) n           continue; n         if (abs(point[x2].first - point[x1].first) <= dis && n             abs(point[y2].first - point[x1].first) <= dis && n             abs(point[x1].second - point[x2].second) <= dis && n             abs(point[y1].second - point[x2].second) <= dis) n           return true; n       } n     } n   } n   return false; n } n int main() { n   scanf( %d , &N); n   cnt1 = cnt2 = 0; n   for (int i = 1, x, y; i <= N; i++) { n     scanf( %d%d , &x, &y); n     point[i] = make_pair(x, y); n     cntx[++cnt1] = x; n     cnty[++cnt2] = y; n     samex[x].push_back(make_pair(y, i)); n     samey[y].push_back(make_pair(x, i)); n   } n   sort(cntx + 1, cntx + 1 + cnt1); n   sort(cnty + 1, cnty + 1 + cnt2); n   cnt1 = unique(cntx + 1, cntx + 1 + cnt1) - (cntx + 1); n   cnt2 = unique(cnty + 1, cnty + 1 + cnt2) - (cnty + 1); n   for (int i = 1; i <= cnt1; i++) n     sort(samex[cntx[i]].begin(), samex[cntx[i]].end()); n   for (int i = 1; i <= cnt2; i++) n     sort(samey[cnty[i]].begin(), samey[cnty[i]].end()); n   long long l = 0; n   long long r = 2e9; n   long long ans = -1; n   while (l <= r) { n     long long mid = (l + r) >> 1; n     if (check(mid)) { n       ans = mid; n       r = mid - 1; n     } else n       l = mid + 1; n   } n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int E = 1e18 + 7; n const long long int MOD = 1000000007; n int main() { n   long long int n; n   cin >> n; n   while (n--) { n     string s; n     cin >> s; n     bool P = true; n     bool same = true; n     for (int i = 0; i < s.size(); i++) { n       if (s[i] != s[0]) { n         same = false; n       } n       if (s[i] != s[s.size() - 1 - i]) { n         P = false; n       } n     } n     if (!P) { n       cout << s <<   n ; n     } else if (same) { n       cout << -1 <<   n ; n     } else { n       for (int i = 1; i < s.size(); i++) { n         if (s[i] != s[i - 1]) { n           swap(s[i], s[i - 1]); n         } n       } n       cout << s <<   n ; n     } n   } n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n #define fo(a,b,c) for(int a=b;a<=c;++a) n #define fr(a,b,c) for(int a=b;a>=c;--a) n #define ll long long n #define pq priority_queue n int t,n,a[30010]; n int cnt[3]; n bool check() n { n  if(cnt[1]==cnt[2]&&cnt[1]==cnt[0]) n   return false; n  return true;  n } n int main() n { n  scanf( %d ,&t);  n  while(t--) n  { n   memset(cnt,0,sizeof(cnt)); n   int ans=0; n   scanf( %d ,&n); n   fo(i,1,n) n    scanf( %d ,&a[i]); n   fo(i,1,n) n   { n    if(a[i]%3==0)cnt[0]++; n    else if(a[i]%3==1)cnt[1]++; n    else cnt[2]++;  n   } n   int sum=n/3,i=0; n   while(check()) n   { n    int pre=(i+2)%3; n    if(cnt[i]<sum) n    { n     cnt[pre]-=abs(sum-cnt[i]); n     ans+=abs(sum-cnt[i]); n     cnt[i]=sum; n    } n    i=(i+1)%3; n   }  n   printf( %d n ,ans); n  } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int xa, ya, xb, yb, n, count = 0; n   cin >> xa >> ya >> xb >> yb; n   if (xa > xb) { n     int t = xa; n     xa = xb; n     xb = t; n   } n   if (ya > yb) { n     int t = ya; n     ya = yb; n     yb = t; n   } n   cin >> n; n   int x[n], y[n], r[n]; n   for (int i = 0; i < n; i++) { n     cin >> x[i] >> y[i] >> r[i]; n   } n   for (int i = xa; i <= xb; i++) { n     for (int h = 0; h < n; h++) { n       double diff = sqrt((x[h] - i) * (x[h] - i) + (y[h] - ya) * (y[h] - ya)); n       if (diff <= r[h]) { n         count++; n         break; n       } n     } n     for (int h = 0; h < n; h++) { n       double diff = sqrt((x[h] - i) * (x[h] - i) + (y[h] - yb) * (y[h] - yb)); n       if (diff <= r[h]) { n         count++; n         break; n       } n     } n   } n   for (int j = (ya + 1); j < yb; j++) { n     for (int h = 0; h < n; h++) { n       double diff = sqrt((x[h] - xa) * (x[h] - xa) + (y[h] - j) * (y[h] - j)); n       if (diff <= r[h]) { n         count++; n         break; n       } n     } n     for (int h = 0; h < n; h++) { n       double diff = sqrt((x[h] - xb) * (x[h] - xb) + (y[h] - j) * (y[h] - j)); n       if (diff <= r[h]) { n         count++; n         break; n       } n     } n   } n   int ans = (2 * (xb - xa + yb - ya)) - count; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, a; n   cin >> n >> a; n   int arr[n + 1]; n   int cnt = 0; n   for (int i = 1; i <= n; i++) { n     cin >> arr[i]; n   } n   for (int i = 1; i <= n; i++) { n     if (arr[i]) { n       int temp = i - a; n       int d = a - temp; n       if (d > n || d < 1 || arr[d] == arr[i]) cnt++; n     } n   } n   cout << cnt; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   long long x, y; n }; n long long cross(point a, point b, point c) { n   return (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y); n } n int main() { n   point a, b, c; n   cin >> a.x >> a.y >> b.x >> b.y >> c.x >> c.y; n   long long ans = cross(a, b, c); n   if (ans > 0) n     cout <<  LEFT n ; n   else if (ans < 0) n     cout <<  RIGHT n ; n   else n     cout <<  TOWARDS n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1e9 + 7; n long long po(long long x, long long y, long long p) { n   long long res = 1; n   x = x % p; n   if (x == 0) return 0; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n void dfs(vector<vector<long long> > &adj, long long s, long long e, n          vector<long long> &v, vector<bool> &vis) { n   if (s == e) return; n   vis[s] = true; n   for (auto vx : adj[s]) { n     if (!vis[vx]) { n       dfs(adj, vx, e, v, vis); n     } n   } n   v.push_back(s); n } n void solve() { n   long long n, m, a, b; n   cin >> n >> m >> a >> b; n   vector<vector<long long> > adj(n + 1); n   while (m--) { n     long long x, y; n     cin >> x >> y; n     adj[x].push_back(y); n     adj[y].push_back(x); n   } n   vector<long long> v1; n   vector<long long> v2; n   vector<bool> vis(n + 1, false); n   dfs(adj, a, b, v1, vis); n   for (int i = 0; i < n + 1; i++) vis[i] = false; n   dfs(adj, b, a, v2, vis); n   sort((v1).begin(), (v1).end()); n   sort((v2).begin(), (v2).end()); n   long long n1 = 0, n2 = 0; n   for (long long i = 0; i < v1.size(); i++) { n     if (!binary_search(v2.begin(), v2.end(), v1[i]) and v1[i] != a) { n       n1++; n     } n   } n   for (long long i = 0; i < v2.size(); i++) { n     if (!binary_search(v1.begin(), v1.end(), v2[i]) and v2[i] != b) n2++; n   } n   cout << n1 * n2 <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), 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 using namespace std; n const int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1}; n const int dy[8] = {-1, 0, 1, -1, 1, 0, 1, -1}; n const int mod[2] = {1000000007, 1000000009}; n const int p[2] = {29, 31}; n const int maxn = 3000; n int add(int a, int b, int k) { n   return a + b >= mod[k] ? a + b - mod[k] : a + b; n } n int mul(int a, int b, int k) { return 1LL * a * b % mod[k]; } n int sub(int a, int b, int k) { return a < b ? a - b + mod[k] : a - b; } n int pwr(int a, int k, int i) { n   int res = 1; n   while (k) { n     if (k % 2) res = mul(res, a, i); n     a = mul(a, a, i); n     k /= 2; n   } n   return res; n } n int invmod(int a, int k) { return pwr(a, mod[k] - 2, k); } n int H[2][maxn], pinv[2][maxn]; n pair<int, int> hsb, hse; n string t, sb, se; n pair<int, int> h(string a) { n   for (int k = 0; k < 2; ++k) { n     int pp = 1; n     for (int i = 0; i < a.size(); ++i) { n       H[k][i] = mul(a[i] -  a  + 1, pp, k), pp = mul(pp, p[k], k); n       if (i) H[k][i] = add(H[k][i - 1], H[k][i], k); n     } n   } n   return {H[0][a.size() - 1], H[1][a.size() - 1]}; n } n pair<int, int> f(int i, int j) { n   return {mul(pinv[0][i], i ? sub(H[0][j], H[0][i - 1], 0) : H[0][j], 0), n           mul(pinv[1][i], i ? sub(H[1][j], H[1][i - 1], 1) : H[1][j], 1)}; n } n vector<int> x, y; n vector<pair<int, int> > M; n int main() { n   for (int k = 0; k < 2; ++k) { n     pinv[k][0] = 1; n     pinv[k][1] = invmod(p[k], k); n     for (int i = 2; i < maxn; ++i) n       pinv[k][i] = mul(pinv[k][i - 1], pinv[k][1], k); n   } n   cin >> t >> sb >> se; n   hsb = h(sb); n   hse = h(se); n   h(t); n   int cnt = 0, a = sb.size(), b = se.size(), c = t.size(); n   for (int i = 0; i + a - 1 < c; ++i) n     if (hsb == f(i, i + a - 1)) x.push_back(i); n   for (int i = 0; i + b - 1 < c; ++i) n     if (hse == f(i, i + b - 1)) y.push_back(i); n   for (int i : x) n     for (int j : y) n       if (i <= j && i + a <= j + b) M.push_back(f(i, j + b - 1)); n   sort(M.begin(), M.end()); n   int res = M.size() != 0; n   for (int i = 1; i < M.size(); ++i) res += (M[i] != M[i - 1]); n   cout << res <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n int n; n long long int dn[6010], dinamik2[6010]; n int main() { n   string ar; n   string ar2; n   cin >> ar; n   cin >> ar2; n   n = ((int)(ar2.size())); n   long long res = 0; n   for (int k = 0; k < ((int)(ar.size())); k++) { n     for (int j = 0; j < ((int)(ar2.size())); j++) { n       if (ar2[j] == ar[k]) dinamik2[j] = (dinamik2[j] + dn[j - 1] + 1) % MOD; n       dinamik2[j + 1] = (dinamik2[j + 1] + dinamik2[j]) % MOD; n     } n     res = (res + dinamik2[n]) % MOD; n     for (int j = 0; j < ((int)(ar2.size())); j++) dn[j] = dinamik2[j]; n     memset(dinamik2, 0, sizeof dinamik2); n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   set<string> l; n   int ans = 0; n   while (getline(cin, s)) { n     if (s ==   ) break; n     if (s[0] ==  + ) { n       l.insert(s.substr(1)); n     } else if (s[0] ==  - ) { n       l.erase(s.substr(1)); n     } else { n       int i = 0; n       while (s[i] !=  : ) i++; n       ans += ((int)s.size() - i - 1) * l.size(); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int pb, mb, bb, m; n   cin >> pb >> mb >> bb >> m; n   int s1 = 2 * pb; n   int temp = 2 * m; n   if (2 * m >= s1) { n     cout << -1 << endl; n     return 0; n   } n   int s2 = 2 * mb; n   if (2 * m >= s2) { n     cout << -1 << endl; n     return 0; n   } n   int s3 = 2 * bb, c = 0; n   for (int i = bb; i <= s3; i++) { n     if (m <= i && temp >= i) { n       s3 = i; n       c = 1; n       break; n     } n   } n   if (c == 0) { n     cout << -1 << endl; n     return 0; n   } n   cout << s1 << endl; n   cout << s2 << endl; n   cout << s3 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string a, b; n struct node { n   int x, y; n }; n vector<node> ans; n void rua() { n   ans.clear(); n   int n; n   scanf( %d , &n); n   cin >> a >> b; n   for (int i = 0; i < n; i++) n     if (a[i] != b[i]) n       for (int j = i + 1; j < n; j++) { n         if (a[i] == a[j]) { n           swap(a[j], b[i]); n           ans.push_back({j, i}); n           break; n         } n         if (a[i] == b[j]) { n           swap(a[j], b[j]); n           ans.push_back({j, j}); n           swap(a[j], b[i]); n           ans.push_back({j, i}); n           break; n         } n       } n   if (a != b) { n     printf( NO n ); n     return; n   } n   printf( YES n%d n , (int)ans.size()); n   for (auto i : ans) printf( %d %d n , i.x + 1, i.y + 1); n } n int main() { n   int t; n   scanf( %d , &t); n   while (t--) rua(); n   return 0; n } n


#include <bits/stdc++.h> n const int N = 100100; n struct Edge { n   int weight, type; n   int x, y; n   int num; n   bool operator<(const Edge& other) const { n     return weight == other.weight ? type > other.type : weight < other.weight; n   } n } edges[N]; n int n, m; n int x[N], y[N]; n bool b[N]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; ++i) { n     scanf( %d%d , &edges[i].weight, &edges[i].type); n     edges[i].num = i; n   } n   std::sort(edges, edges + m); n   int k = 0; n   int ptrX = 0, ptrY = 2; n   for (int i = 0; i < m; ++i) { n     if (edges[i].type == 0) { n       if (b[ptrX] && b[ptrY]) { n         edges[i].x = ptrX; n         edges[i].y = ptrY; n         if (ptrX + 2 < ptrY) n           ++ptrX; n         else { n           ptrX = 0; n           ++ptrY; n         } n       } else { n         puts( -1 ); n         return 0; n       } n     } else { n       edges[i].x = k; n       edges[i].y = k + 1; n       b[k] = true; n       b[k + 1] = true; n       ++k; n     } n   } n   for (int i = 0; i < m; ++i) { n     int num = edges[i].num; n     x[num] = edges[i].x; n     y[num] = edges[i].y; n   } n   for (int i = 0; i < m; ++i) printf( %d %d n , x[i] + 1, y[i] + 1); n   return 0; n } n


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


#include <bits/stdc++.h> n const int MAXN = 55; n const int MAXS = 10105; n const int MOD = 1000000007; n int n; n bool g[MAXN][MAXN]; n std::vector<int> e[MAXN][MAXN]; n std::vector<int> tr[MAXS]; n int f[MAXS]; n void input() { n   int m; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; ++i) { n     int x, y, nv; n     scanf( %d%d%d , &x, &y, &nv); n     --x; n     --y; n     std::vector<int> ev(nv); n     for (int j = 0; j < nv; ++j) { n       int v; n       scanf( %d , &v); n       --v; n       ev[j] = v; n     } n     g[x][y] = true; n     e[x][y] = ev; n   } n } n int check_edge(const std::vector<int> &a, int x, int y) { n   if (a.empty()) return -1; n   for (std::vector<int>::const_iterator i = a.begin(); i + 1 != a.end(); ++i) n     if (*i == x && *(i + 1) == y) return i - a.begin(); n   return -1; n } n int walk(std::vector<int> &a, int now, bool reverse) { n   for (int k = 0; (int)a.size() < 2 * n && k < (int)a.size(); ++k) { n     if ((!reverse && !g[now][a[k]]) || (reverse && !g[a[k]][now])) return -1; n     if (!reverse) n       a.insert(a.end(), e[now][a[k]].begin(), e[now][a[k]].end()); n     else { n       a.insert(a.end(), e[a[k]][now].rbegin(), e[a[k]][now].rend()); n     } n     now = a[k]; n   } n   if ((int)a.size() >= 2 * n) return -1; n   return now; n } n void add_trans(int x, int y, int len) { n   for (int i = 0; i + len <= 2 * n; ++i) n     tr[i * (2 * n) + x].push_back((i + len) * (2 * n) + y); n } n void init() { n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < n; ++j) n       if (g[i][j]) { n         int pos; n         if ((pos = check_edge(e[i][j], i, j)) != -1) { n           std::vector<int> tmpl(e[i][j].begin(), e[i][j].begin() + pos); n           std::reverse(tmpl.begin(), tmpl.end()); n           int nowl = walk(tmpl, i, true); n           std::vector<int> tmpr(e[i][j].begin() + (pos + 2), e[i][j].end()); n           int nowr = walk(tmpr, j, false); n           if (nowl != -1 && nowr != -1) n             add_trans(nowl, nowr + n, tmpl.size() + tmpr.size() + 1); n         } n         if (e[i][j].empty()) { n           add_trans(i + n, j, 1); n         } n         if (!e[i][j].empty() && e[i][j].front() == j) { n           std::vector<int> tmp(e[i][j].begin() + 1, e[i][j].end()); n           int now = walk(tmp, j, false); n           if (now != -1) add_trans(i + n, now + n, tmp.size() + 1); n         } n         if (!e[i][j].empty() && e[i][j].back() == i) { n           std::vector<int> tmp(e[i][j].begin(), e[i][j].end() - 1); n           std::reverse(tmp.begin(), tmp.end()); n           int now = walk(tmp, i, true); n           if (now != -1) add_trans(now, j, tmp.size() + 1); n         } n       } n } n void solve() { n   int totstate = (2 * n + 1) * (2 * n); n   for (int i = 0; i < n; ++i) f[i] = 1; n   for (int i = 0; i < totstate; ++i) n     for (std::vector<int>::const_iterator j = tr[i].begin(); j != tr[i].end(); n          ++j) n       (f[*j] += f[i]) %= MOD; n   for (int i = 1; i <= 2 * n; ++i) { n     int sum = 0; n     for (int j = 0; j < n; ++j) (sum += f[i * (2 * n) + j + n]) %= MOD; n     printf( %d n , sum); n   } n } n int main() { n   input(); n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool jolmetsz(long long a, long long b, long long c, long long x1, long long y1, n               long long x2, long long y2) { n   return -c - a * x1 < b * y1 && -c - a * x2 > b * y2; n } n int main() { n   long long x1, x2, y1, y2, n, db = 0; n   cin >> x1 >> y1 >> x2 >> y2 >> n; n   if (y2 < y1) { n     swap(y1, y2); n     swap(x1, x2); n   } n   int lep = 0; n   for (int i = 0; i < n; i++) { n     long long a, b, c; n     cin >> a >> b >> c; n     if (jolmetsz(a, b, c, x1, y1, x2, y2) || jolmetsz(a, b, c, x2, y2, x1, y1)) n       db++; n   } n   cout << db << endl; n } n


#include <bits/stdc++.h> n using namespace std; 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   } n   string str; n   cin >> str; n   bool flag = 0; n   int start = 0; n   set<int> s; n   for (int i = 0; i < str.length(); i++) { n     if (!flag && str[i] ==  0  && a[i] != i + 1) { n       cout <<  NO  << endl; n       return 0; n     } n     if (flag && str[i] ==  0 ) { n       s.insert(a[i]); n       for (int j = start; j <= i + 1; j++) { n         if (s.find(j) == s.end()) { n           cout <<  NO  << endl; n           return 0; n         } n       } n       s.clear(); n       flag = 0; n     } n     if (str[i] ==  1 ) { n       s.insert(a[i]); n       if (!flag) { n         start = i + 1; n       } n       flag = 1; n     } n   } n   cout <<  YES  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<int> s; n int main() { n   int x; n   cin >> x; n   s.insert(x); n   for (;;) { n     x++; n     while (x % 10 == 0) x /= 10; n     int sz = s.size(); n     s.insert(x); n     if (sz == s.size()) { n       printf( %d , s.size()); n       return 0; n     } n   } n } n


#include<bits/stdc++.h> n using namespace std; n int main() n { n     int t,n,k=0,c; n     cin >> t; n     while(t--) n     { n         cin >> n; n         c=1; n         for(int i=2; i<=n; i++) n         { n                 cout << i <<    ; n         } n         cout << 1; n         cout << endl; n     } n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n int main() { n   int n; n   cin >> n; n   int maxn = 0; n   int x, y; n   while (n--) { n     scanf( %d %d , &x, &y); n     maxn = max(maxn, x + y); n   } n   cout << maxn << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, n, w; n   string s; n   cin >> t; n   while (t--) { n     cin >> n >> s; n     bool flag = 0; n     if (n % 2) { n       for (int i = 0; i < n; i += 2) { n         if ((s[i] -  0 ) % 2) { n           flag = 1; n           break; n         } n       } n       w = flag ? 1 : 2; n       cout << w << endl; n     } else { n       for (int i = 1; i < n; i += 2) { n         if ((s[i] -  0 ) % 2 == 0) { n           flag = 1; n           break; n         } n       } n       w = flag ? 2 : 1; n       cout << w << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[1111], x, n, s, y, o, oo; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> x; n     if (x == 0 || x == 100) n       a[s++] = x; n     else if (x < 10 && o == 0) n       a[s++] = x, o = 1; n     else if (x % 10 == 0 && oo == 0) n       a[s++] = x, oo = 1; n     else n       y = x; n   } n   if (o == 0 && oo == 0 && y != 0) a[s++] = y; n   cout << s << endl; n   for (int i = 0; i < s; i++) cout << a[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n double dist(long long x, long long y) { return sqrt(x * x + y * y); } n int main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(nullptr); n   int test; n   std::cin >> test; n   double inf = 1000.0 * 1000.0 * 1000.0; n   while (test--) { n     long long w, h; n     std::cin >> w >> h; n     long long x1, y1, x2, y2; n     std::cin >> x1 >> y1 >> x2 >> y2; n     long long ww, hh; n     std::cin >> ww >> hh; n     double min_ans = inf; n     int x_intersect = std::max(0ll, ww - x1); n     int y_intersect = std::max(0ll, hh - y1); n     if (x2 + x_intersect <= w) { n       min_ans = std::min(min_ans, dist(x_intersect, 0ll)); n     } n     if (y2 + y_intersect <= h) { n       min_ans = std::min(min_ans, dist(y_intersect, 0ll)); n     } n     x_intersect = std::max(0ll, ww - x1); n     y_intersect = std::max(0ll, y2 - (h - hh)); n     if (x2 + x_intersect <= w) { n       min_ans = std::min(min_ans, dist(x_intersect, 0ll)); n     } n     if (y1 - y_intersect >= 0) { n       min_ans = std::min(min_ans, dist(y_intersect, 0ll)); n     } n     x_intersect = std::max(0ll, x2 - (w - ww)); n     y_intersect = std::max(0ll, hh - y1); n     if (x1 - x_intersect >= 0) { n       min_ans = std::min(min_ans, dist(x_intersect, 0ll)); n     } n     if (y2 + y_intersect <= h) { n       min_ans = std::min(min_ans, dist(y_intersect, 0ll)); n     } n     x_intersect = std::max(0ll, x2 - (w - ww)); n     y_intersect = std::max(0ll, y2 - (h - hh)); n     if (x1 - x_intersect >= 0) { n       min_ans = std::min(min_ans, dist(x_intersect, 0ll)); n     } n     if (y1 - y_intersect >= 0) { n       min_ans = std::min(min_ans, dist(y_intersect, 0ll)); n     } n     std::cout.precision(20); n     if (min_ans == inf) { n       std::cout <<  -1 n ; n     } else { n       std::cout << min_ans <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937_64 rnd(time(0)); n ifstream in; n ofstream out; n const long long INF = LLONG_MAX; n const long double EPS = 1e-9; n const long double pi = 3.141592653589793238462643383279502884; n long long n; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout.precision(20); n   srand(time(0)); n   cin >> n; n   long long l = 0; n   long long r = 1e9; n   long long k = 2; n   cout <<  0 2  << endl; n   cout.flush(); n   string x; n   cin >> x; n   if (x[0] ==  b ) swap(l, r); n   for (long long i = 0; i < n - 1; i++) { n     long long m = (l + r) >> 1; n     cout << m <<     << k << endl; n     cout.flush(); n     string x; n     cin >> x; n     if (x[0] ==  w ) n       l = m; n     else n       r = m; n   } n   cout << l <<     << k - 1 <<     << r <<     << k + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int c[2][501][501]; n int main() { n   int n, m, b, mod; n   scanf( %d%d%d%d , &n, &m, &b, &mod); n   int a[n + 1]; n   for (long i = 1; i <= n; i++) scanf( %d , &a[i]); n   memset(c, 0, sizeof(c)); n   c[0][0][0] = 1; n   for (long i = 1; i <= n; i++) n     for (long j = 0; j <= m; j++) n       for (long k = 0; k <= b; k++) { n         c[(i & 1)][j][k] = c[(i & 1) ^ 1][j][k]; n         if (k >= a[i] && j > 0) c[(i & 1)][j][k] += c[(i & 1)][j - 1][k - a[i]]; n         c[(i & 1)][j][k] = c[(i & 1)][j][k] % mod; n       } n   int ans = 0; n   for (int i = 0; i <= b; i++) { n     ans += c[n & 1][m][i]; n     ans = ans % mod; n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { 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   map<long long int, long long int> M; n   for (long long int i = 0; i < N; i++) { n     if (A[i] != 0) M[A[i]]++; n   } n   cout << M.size() << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e3 + 10; n struct pos { n   double x, y; n } p[maxn]; n int n; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %lf%lf , &p[i].x, &p[i].y); n   double ans = 0x3f3f3f3f; n   for (int i = 0; i < n; i++) { n     pos a = p[i]; n     pos b = p[(i + 1) % n]; n     pos c = p[(i + 2) % n]; n     double dis = (double)(c.x - a.x) * (b.y - c.y) - (c.y - a.y) * (b.x - c.x); n     if (dis < 0) dis = -dis; n     dis = dis / (sqrt((c.x - a.x) * (c.x - a.x) + (c.y - a.y) * (c.y - a.y))); n     dis = dis / 2; n     ans = min(ans, dis); n   } n   printf( %f , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long n, i, j, k, x, MOD = 1000000007, ans, a1, a2, b1, b2, a, b; n   string s1, s2; n   cin >> n >> s1 >> s2; n   vector<vector<long long>> dp(n + 1, vector<long long>(4, 0)); n   dp[0][0] = 1; n   for (i = 0; i < n; i++) { n     for (j = 0; j < 4; j++) { n       if (s1[i] ==  ? ) n         a1 = 0, a2 = 9; n       else n         a1 = s1[i] -  0 , a2 = a1; n       if (s2[i] ==  ? ) n         b1 = 0, b2 = 9; n       else n         b1 = s2[i] -  0 , b2 = b1; n       for (a = a1; a <= a2; a++) { n         for (b = b1; b <= b2; b++) { n           x = j; n           if (a < b) n             x |= 1; n           else if (a > b) n             x |= 2; n           dp[i + 1][x] = (dp[i + 1][x] + dp[i][j]) % MOD; n         } n       } n     } n   } n   ans = dp[n][3]; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1); n const double eps = 1e-9; n const int inf = 2000000000; n const long long infLL = 9000000000000000000; n int dx[] = {0, 0, +1, -1}; n int dy[] = {+1, -1, 0, 0}; n template <typename first, typename second> n ostream &operator<<(ostream &os, const pair<first, second> &p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   os <<  { ; n   for (auto it = v.begin(); it != v.end(); ++it) { n     if (it != v.begin()) os <<  ,  ; n     os << *it; n   } n   return os <<  } ; n } n template <typename T> n ostream &operator<<(ostream &os, const set<T> &v) { n   os <<  [ ; n   for (auto it = v.begin(); it != v.end(); ++it) { n     if (it != v.begin()) os <<  ,  ; n     os << *it; n   } n   return os <<  ] ; n } n template <typename T> n ostream &operator<<(ostream &os, const multiset<T> &v) { n   os <<  [ ; n   for (auto it = v.begin(); it != v.end(); ++it) { n     if (it != v.begin()) os <<  ,  ; n     os << *it; n   } n   return os <<  ] ; n } n template <typename first, typename second> n ostream &operator<<(ostream &os, const map<first, second> &v) { n   os <<  [ ; n   for (auto it = v.begin(); it != v.end(); ++it) { n     if (it != v.begin()) os <<  ,  ; n     os << it->first <<   =   << it->second; n   } n   return os <<  ] ; n } n void faltu() { cerr <<   n ; } n template <typename T> n void faltu(T a[], int n) { n   for (int i = 0; i < n; ++i) cerr << a[i] <<    ; n   cerr <<   n ; n } n template <typename T, typename... hello> n void faltu(T arg, const hello &...rest) { n   cerr << arg <<    ; n   faltu(rest...); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int n; n   cin >> n; n   while (n--) { n     int m; n     cin >> m; n     vector<int> v(m); n     for (int i = 0; i < m; i++) cin >> v[i]; n     while (!v.empty() && v.back() == 0) v.pop_back(); n     reverse((v).begin(), (v).end()); n     while (!v.empty() && v.back() == 0) v.pop_back(); n     int y = (int)v.size(); n     int ans = 0; n     if (y == 1) n       cout << 0 <<   n ; n     else { n       for (int i = 0; i < y; i++) { n         if (v[i] == 0) ans++; n       } n       cout << ans <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b, c, d, e, i, j, ii, jj, zx, xc, qw[200009], we[200009], pas, pi, n     k[200009]; n pair<pair<long long, long long>, long long> p[600009]; n bool bo[200009], rko[200009]; n vector<long long> v[200009]; n deque<long long> de; n void dfsst(long long q, long long w, long long rr) { n   if (rr > xc) { n     xc = rr; n     zx = q; n   } n   for (vector<long long>::iterator it = v[q].begin(); it != v[q].end(); it++) { n     if ((*it) == w) continue; n     dfsst((*it), q, rr + 1); n   } n } n void dfsst2(long long q, long long w, long long rr) { n   if (rr > xc) { n     xc = rr; n     zx = q; n   } n   for (vector<long long>::iterator it = v[q].begin(); it != v[q].end(); it++) { n     if ((*it) == w) continue; n     dfsst2((*it), q, rr + 1); n   } n } n void dfs(long long q, long long w, long long rr) { n   qw[q] = rr - 1; n   for (vector<long long>::iterator it = v[q].begin(); it != v[q].end(); it++) { n     if ((*it) == w) continue; n     dfs((*it), q, rr + 1); n   } n } n void dfs2(long long q, long long w, long long rr) { n   if (d == 0) bo[q] = 1; n   if (q == e) d = 1; n   we[q] = rr - 1; n   for (vector<long long>::iterator it = v[q].begin(); it != v[q].end(); it++) { n     if ((*it) == w) continue; n     dfs2((*it), q, rr + 1); n   } n   if (d == 0) bo[q] = 0; n } n void dfs3(long long q, long long w) { n   if (q == zx) return; n   pi++; n   p[pi].first.first = q; n   p[pi].first.second = zx; n   p[pi].second = q; n   for (vector<long long>::iterator it = v[q].begin(); it != v[q].end(); it++) { n     if ((*it) == w) continue; n     if (bo[(*it)] == 1) { n       dfs3((*it), q); n       return; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> a; n   for (i = 1; i < a; i++) { n     cin >> c >> d; n     v[c].push_back(d); n     v[d].push_back(c); n     k[c]++; n     k[d]++; n   } n   zx = 0; n   xc = 0; n   dfsst(1, 0, 1); n   e = zx; n   zx = 0; n   xc = 0; n   dfsst2(e, 0, 1); n   dfs(e, 0, 1); n   d = 0; n   dfs2(zx, 0, 1); n   for (i = 1; i <= a; i++) { n     if (bo[i] == 0 && v[i].size() == 1) { n       de.push_back(i); n       rko[i] = 1; n     } n   } n   while (de.size() != 0) { n     c = de.front(); n     de.pop_front(); n     pas += max(qw[c], we[c]); n     pi++; n     if (qw[c] >= we[c]) { n       p[pi].first.first = e; n       p[pi].first.second = c; n       p[pi].second = c; n     } else { n       p[pi].first.first = zx; n       p[pi].first.second = c; n       p[pi].second = c; n     } n     d = 0; n     for (vector<long long>::iterator it = v[c].begin(); it != v[c].end(); n          it++) { n       if (rko[(*it)] == 1) continue; n       d = (*it); n       break; n     } n     k[d]--; n     if (d != 0 && bo[d] == 0 && k[d] == 1) { n       de.push_back(d); n       rko[d] = 1; n     } n   } n   pas += qw[zx] * (qw[zx] + 1) / 2; n   dfs3(e, 0); n   cout << pas << endl; n   for (i = 1; i <= pi; i++) n     cout << p[i].first.first <<     << p[i].first.second <<     << p[i].second n          << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool mysort(const pair<double, int> &a, const pair<double, int> &b) { n   if (a.first == b.first) n     return a.second < b.second; n   else n     return a.first > b.first; n } n int main() { n   int n, t1, t2, k, a, b, i; n   double h1, h2; n   vector<pair<double, int> > vec; n   cin >> n >> t1 >> t2 >> k; n   for (i = 1; i <= n; i++) { n     cin >> a >> b; n     h1 = (1.0 * a * t1 * (100 - k) / 100) + b * t2; n     h2 = (1.0 * b * t1 * (100 - k) / 100) + a * t1; n     h1 = a * t1 * (1 - (k * 1.0) / 100) + b * t2; n     h2 = b * t1 * (1 - (k * 1.0) / 100) + a * t2; n     vec.push_back(make_pair(max(h1, h2), i)); n   } n   sort(vec.begin(), vec.end(), mysort); n   for (i = 0; i < vec.size(); i++) n     cout << vec[i].second <<     << fixed << setprecision(2) << vec[i].first n          << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int c[1000001]; n int b[1000001]; n int main() { n   int n, i, l, r, m, kq = 1, a; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     cin >> a; n     c[a] = i; n     b[i] = 0; n   } n   cin >> a; n   b[1] = c[a]; n   for (i = 2; i <= n; i++) { n     cin >> a; n     a = c[a]; n     l = 1; n     r = i; n     while (l < r) { n       m = (l + r) / 2; n       if (b[m] > a) n         l = m + 1; n       else n         r = m; n     } n     b[r] = a; n     if (kq < r) kq = r; n   } n   cout << kq; n   return 0; n } n


#include <bits/stdc++.h> n int a[201][3]; n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, k; n   cin >> n >> k; n   vector<string> a; n   for (int i = 0; i < n; i++) { n     string r; n     cin >> r; n     a.push_back(r); n   } n   int count = 0, ans = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < a[i].size(); j++) { n       if (a[i][j] ==  4  || a[i][j] ==  7 ) count++; n       if (count > k) break; n     } n     if (count <= k) ans++; n     count = 0; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k, i = 1; n   cin >> n >> k; n   while (~k & 1) k >>= 1, i++; n   cout << i; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct edge { n   int to, cost; n }; n vector<edge> G[500005]; n int q, n, k; n long long f[500005], g[500005]; n vector<long long> weights[500005]; n void add_edge(int u, int v, int cost) { n   G[u].push_back((edge){v, cost}); n   G[v].push_back((edge){u, cost}); n } n void dfs(int v, int p) { n   f[v] = g[v] = 0; n   for (auto e : G[v]) { n     if (e.to == p) continue; n     dfs(e.to, v); n     f[v] += f[e.to]; n     g[v] += f[e.to]; n     weights[v].push_back(e.cost + g[e.to] - f[e.to]); n   } n   sort(weights[v].begin(), weights[v].end(), greater<long long>()); n   long long sum1 = 0, sum2 = 0; n   for (int i = 0; i < (int)weights[v].size(); i++) { n     if (weights[v][i] <= 0) break; n     if (i < k - 1) sum1 += weights[v][i]; n     if (i < k) sum2 += weights[v][i]; n   } n   f[v] += sum2; n   g[v] += sum1; n } n int main() { n   scanf( %d , &q); n   while (q--) { n     scanf( %d%d , &n, &k); n     for (int i = 1; i <= n; i++) G[i].clear(), weights[i].clear(); n     for (int i = 0; i < n - 1; i++) { n       int u, v, cost; n       scanf( %d%d%d , &u, &v, &cost); n       add_edge(u, v, cost); n     } n     dfs(1, 0); n     printf( %lld n , f[1]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 ran(time(0)); n long long mod = 1e9 + 7; n long long gcd(long long a, long long b) { n   if (!b) return a; n   return gcd(b, a % b); n } n long long Bpow(long long a, long long b) { n   long long ans = 1; n   while (b) { n     if (b & 1) { n       ans *= a; n       ans %= mod; n     } n     a = a * a % mod; n     b >>= 1; n   } n   return ans; n } n const long long MX = 1 << 19; n long long t1[2 * MX]; n long long t2[2 * MX]; n void upd(long long x) { n   x += MX; n   for (; x > 0; x >>= 1) { n     t1[x]++; n   } n } n void upd2(long long l, long long r, long long x) { n   for (l += MX, r += MX; l <= r; l >>= 1, r >>= 1) { n     if (l & 1) { n       t2[l++] -= x; n     } n     if (~r & 1) t2[r--] -= x; n   } n } n long long get1(long long l, long long r) { n   long long ans = 0; n   for (l += MX, r += MX; l <= r; l >>= 1, r >>= 1) { n     if (l & 1) ans += t1[l++]; n     if (~r & 1) ans += t1[r--]; n   } n   return ans; n } n long long get2(long long x) { n   x += MX; n   long long ans = 0; n   while (x > 0) { n     ans += t2[x]; n     x >>= 1; n   } n   return ans; n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   vector<long long> a(n); n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   long long ans = 0; n   vector<long long> h(MX + 1); n   long long sum = 0; n   for (long long i = 0; i < n; i++) { n     ans += sum + i * a[i]; n     long long now = a[i]; n     while (now < MX) { n       long long v = get1(now, MX - 1); n       upd2(now + (a[i] == now), MX - 1, a[i]); n       ans -= a[i] * v; n       now += a[i]; n     } n     ans += get2(a[i]); n     upd(a[i]); n     h[a[i]]++; n     sum += a[i]; n     cout << ans <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100 * 1000 + 23, D = 1000 * 1000 * 1000 + 7, MXLN = 17 + 3; n int mul() { return 1; } n template <typename... T> n int mul(int x, T... a) { n   return 1ll * x * mul(a...) % D; n } n int add() { return 0; } n template <typename... T> n int add(int x, T... a) { n   return (x + add(a...)) % D; n } n int n, e[MAXN], w[MAXN], sum[MAXN], sum2[MAXN], up[MAXN], up2[MAXN], sz[MAXN], n     h[MAXN], par[MXLN][MAXN], dis[MAXN], q; n vector<int> g[MAXN]; n void dfs(int v = 0, int p = -1) { n   for (auto i : g[v]) { n     int u = v ^ e[i]; n     if (u == p) continue; n     h[u] = h[v] + 1; n     dis[u] = add(dis[v], w[i]); n     par[0][u] = v; n     dfs(u, v); n     sz[v] += sz[u]; n     sum[v] = add(sum[v], sum[u], mul(sz[u], w[i])); n     sum2[v] = n         add(sum2[v], mul(w[i], w[i], sz[u]), sum2[u], mul(2, w[i], sum[u])); n   } n   sz[v]++; n } n void ufs(int v = 0, int p = -1) { n   int tmp = up[v], tmp2 = up2[v]; n   for (auto i : g[v]) { n     int u = v ^ e[i]; n     if (u == p) continue; n     tmp = add(tmp, sum[u], mul(sz[u], w[i])); n     tmp2 = add(tmp2, mul(w[i], w[i], sz[u]), sum2[u], mul(2, w[i], sum[u])); n   } n   for (auto i : g[v]) { n     int u = v ^ e[i]; n     if (u == p) continue; n     up[u] = add(tmp, D - sum[u], D - mul(sz[u], w[i]), mul(w[i], (n - sz[u]))); n     up2[u] = add(tmp2, D - mul(w[i], w[i], sz[u]), D - sum2[u], n                  D - mul(2, w[i], sum[u]), mul(w[i], w[i], n - sz[u]), n                  mul(2, w[i], add(tmp, D - sum[u], D - mul(sz[u], w[i])))); n     ufs(u, v); n   } n } n int lca(int u, int v) { n   if (h[u] < h[v]) swap(u, v); n   for (int i = MXLN - 1; ~i; i--) n     if (h[par[i][u]] >= h[v]) u = par[i][u]; n   if (u == v) return v; n   for (int i = MXLN - 1; ~i; i--) n     if (par[i][u] != par[i][v]) { n       u = par[i][u]; n       v = par[i][v]; n     } n   return par[0][v]; n } n int main() { n   cin >> n; n   for (int i = 1; i < n; i++) { n     int a, b, c; n     cin >> a >> b >> c; n     g[--a].push_back(i), g[--b].push_back(i); n     e[i] = a ^ b; n     w[i] = c; n   } n   dfs(), ufs(); n   for (int i = 1; i < MXLN; i++) n     for (int j = 0; j < n; j++) par[i][j] = par[i - 1][par[i - 1][j]]; n   cin >> q; n   while (q--) { n     int u, v; n     cin >> u >> v; n     int x = lca(--u, --v); n     if (x == v) { n       int fas = add(dis[u], D - dis[v]), n           tmp = D - add(up2[v], mul(fas, fas, (n - sz[v])), mul(2, fas, up[v])); n       cout << add(sum2[u], up2[u], tmp, tmp) <<   n ; n     } else { n       int fas = ((x == u) ? add(dis[v], D - dis[u]) n                           : add(dis[u], dis[v], D - dis[x], D - dis[x])), n           tmp = add(mul(fas, fas, sz[v]), sum2[v], mul(2, fas, sum[v])); n       cout << add(tmp, D - sum2[u], D - up2[u], tmp) <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int z[201010]; n int cost[201010], acc[201010]; n string s, aux, text, text2, patt; n pair<int, int> dp[201010]; n void build() { n   text = aux; n   text2 = aux; n   int n = (int)aux.size(); n   for (int i = 0; i < n; i++) { n     cost[i] = -1; n     acc[i] = 0; n     if (aux[i] ==  ? ) { n       text[i] =  a ; n       text2[i] =  b ; n       if (i % 2 == 0) swap(text[i], text2[i]); n       acc[i] = 1; n     } n     if (i) acc[i] += acc[i - 1]; n   } n } n int getCost(int l, int r) { return acc[r] - (l ? acc[l - 1] : 0); } n void Z() { n   int l = 0, r = 0, n = (int)s.size(); n   z[0] = 0; n   for (int i = 1; i < n; i++) { n     z[i] = 0; n     if (i <= r) z[i] = min(z[i - l], r - i + 1); n     while (i + z[i] < n && s[z[i]] == s[z[i] + i]) z[i]++; n     if (i + z[i] - 1 >= r) { n       l = i; n       r = i + z[i] - 1; n     } n     if (z[i] == (int)patt.size()) n       cost[i - 2] = getCost(i - (int)patt.size() - 1, i - 2); n   } n } n pair<int, int> operator+(pair<int, int> a, pair<int, int> b) { n   return make_pair(a.first + b.first, a.second + b.second); n } n pair<int, int> maxx(pair<int, int> a, pair<int, int> b) { n   if (a.first > b.first) return a; n   if (a.first < b.first) return b; n   return make_pair(a.first, min(a.second, b.second)); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int m, n; n   cin >> n; n   cin >> aux; n   cin >> m; n   patt =   ; n   for (int i = 0; i < m; i++) { n     if (i % 2 == 0) n       patt +=  a ; n     else n       patt +=  b ; n   } n   build(); n   s = patt +  $  + text; n   Z(); n   s = patt +  $  + text2; n   Z(); n   for (int i = 0; i < n; i++) { n     dp[i] = i ? dp[i - 1] : make_pair(0, 0); n     if (cost[i] != -1) { n       dp[i] = maxx(dp[i], dp[i - m] + make_pair(1, cost[i])); n     } n   } n   cout << dp[n - 1].second <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mxn = 5005; n int dp[mxn][mxn], ans, n, a[mxn]; n int md7[8], pos[114514]; n inline void ckmax(int& x, int y) { n   if (x < y) x = y; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   for (int i = 0; i <= n; ++i) { n     for (int j = 0; j <= n; ++j) { n       if (i < j) n         dp[i][j] = n             max({md7[a[j] % 7], pos[a[j] - 1], pos[a[j] + 1], dp[0][i]}) + 1; n       else n         dp[i][j] = dp[j][i]; n       ckmax(ans, dp[i][j]); n       ckmax(md7[a[j] % 7], dp[i][j]); n       ckmax(pos[a[j]], dp[i][j]); n     } n     memset(md7, 0, sizeof(md7)); n     memset(pos, 0, sizeof(pos)); n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long value(const vector<long long>& a, long long b) { n   long long result = 0; n   for (int i = 0; i < a.size(); ++i) { n     result += min(b, a[i]); n   } n   return result; n } n int main() { n   long long n, k; n   cin >> n >> k; n   vector<long long> a(n); n   long long sum = 0; n   long long max_a = 0; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     sum += a[i]; n     max_a = max(max_a, a[i]); n   } n   if (sum < k) { n     cout << -1; n     return 0; n   } n   int l = 0; n   int r = max_a; n   while (r - l > 1) { n     if (value(a, (l + r) / 2) > k) { n       r = (l + r) / 2; n     } else { n       l = (l + r) / 2; n     } n   } n   if (k >= value(a, r)) { n     l = r; n   } n   k -= value(a, l); n   queue<pair<int, int> > q; n   for (int i = 0; i < n; ++i) { n     if (a[i] > l) { n       q.push(make_pair(a[i] - l, i)); n     } n   } n   while (k > 0) { n     --k; n     pair<int, int> t = q.front(); n     q.pop(); n     --t.first; n     if (t.first > 0) { n       q.push(t); n     } n   } n   while (!q.empty()) { n     cout << q.front().second + 1 <<    ; n     q.pop(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e9 + 7; n const int N = 2e2 + 10; n vector<string> v[N]; n int nex[N]; n int flag[N]; n int main() { n   int n; n   cin >> n; n   map<char, int> mc; n   memset((nex), (-1), sizeof(nex)); n   while (n--) { n     string str; n     cin >> str; n     int o = 0; n     for (auto &c : str) { n       mc[c] = 1; n       if (flag[c] == 0) flag[c] = 1; n       if (o >> (c -  0 ) & 1) { n         puts( NO ); n         return 0; n       } n       o |= 1 << (c -  0 ); n     } n     for (int i = 0; i + 1 < str.length(); i++) { n       if (nex[str[i]] == -1) { n         nex[str[i]] = str[i + 1]; n         flag[str[i + 1]] = 2; n       } n       if (nex[str[i]] != str[i + 1]) { n         puts( NO ); n         return 0; n       } n     } n   } n   string ans; n   for (int i =  a ; i <=  z ; i++) { n     if (flag[i] != 1) continue; n     int c = i; n     while (c != -1 && ans.length() <= mc.size()) { n       ans.push_back(c); n       c = nex[c]; n     } n   } n   if (ans.length() != mc.size()) { n     puts( NO ); n   } else n     puts(ans.c_str()); n   return 0; n } n