#include <bits/stdc++.h> n const uint64_t MAX_NUM = 1000000000000000001ul; n uint64_t get_iteration_simple(const int n, const uint64_t k, n                               std::vector<uint64_t>& a) { n   int iterations = 0; n   while (true) { n     ++iterations; n     for (int i = 1; i < n; ++i) { n       a[i] += a[i - 1]; n       if (a[i] >= k) { n         return iterations; n       } n     } n   } n } n bool is_there_more_than_k_with_coeffs( n     const int n, const uint64_t k, std::vector<uint64_t>& a, n     std::vector<std::vector<uint64_t>>& coeffs) { n   for (int i = 0; i < n; ++i) { n     uint64_t result = 0; n     for (int j = 0; j <= i; ++j) { n       if (static_cast<double>(a[j]) * static_cast<double>(coeffs[i][j]) > n           MAX_NUM) { n         return true; n       } n       result += coeffs[i][j] * a[j]; n       if (result >= k) { n         return true; n       } n     } n   } n   return false; n } n std::tuple<std::vector<std::vector<uint64_t>>, bool> generate_mixed_coeffs( n     const int n, const uint64_t k, n     std::vector<std::vector<uint64_t>>& first_coeffs, n     std::vector<std::vector<uint64_t>>& second_coeffs) { n   bool is_there_overflow = false; n   std::vector<std::vector<uint64_t>> new_coeffs(n); n   for (int i = 0; i < n; ++i) { n     new_coeffs[i].resize(i + 1, 0); n     for (int j = 0; j <= i; ++j) { n       new_coeffs[i][j] = 0; n       for (int k = j; k <= i; ++k) { n         if (static_cast<double>(second_coeffs[i][k]) * n                 static_cast<double>(first_coeffs[k][j]) > n             MAX_NUM) { n           is_there_overflow = true; n         } n         new_coeffs[i][j] += second_coeffs[i][k] * first_coeffs[k][j]; n       } n     } n   } n   return std::make_tuple(new_coeffs, is_there_overflow); n } n uint64_t get_iteration_binsearch(const int n, const uint64_t k, n                                  std::vector<uint64_t>& a) { n   uint64_t left = 0; n   uint64_t right = 1; n   std::vector<std::vector<std::vector<uint64_t>>> level_coeffs(100); n   level_coeffs[0].resize(n); n   std::vector<std::vector<uint64_t>> current_coeffs(n); n   for (int i = 0; i < n; ++i) { n     level_coeffs[0][i].resize(i + 1, 0); n     current_coeffs[i].resize(i + 1, 0); n     for (int j = 0; j <= i; ++j) { n       level_coeffs[0][i][j] = 1; n       current_coeffs[i][j] = (i == j ? 1 : 0); n     } n   } n   int diff_two_power = 0; n   while (true) { n     if (is_there_more_than_k_with_coeffs(n, k, a, n                                          level_coeffs[diff_two_power])) { n       break; n     } else { n       right *= 2; n       bool is_there_overflow; n       std::tie(level_coeffs[diff_two_power + 1], is_there_overflow) = n           generate_mixed_coeffs(n, k, level_coeffs[diff_two_power], n                                 level_coeffs[diff_two_power]); n       ++diff_two_power; n       if (is_there_overflow) { n         break; n       } n     } n   } n   while (right - left > 1) { n     uint64_t middle = left + (1ull << (diff_two_power - 1)); n     std::vector<std::vector<uint64_t>> middle_coeffs; n     bool is_there_overflow; n     std::tie(middle_coeffs, is_there_overflow) = generate_mixed_coeffs( n         n, k, current_coeffs, level_coeffs[diff_two_power - 1]); n     if (is_there_overflow || n         is_there_more_than_k_with_coeffs(n, k, a, middle_coeffs)) { n       right = middle; n     } else { n       left = middle; n       current_coeffs = std::move(middle_coeffs); n     } n     --diff_two_power; n   } n   return left + 1; n } n bool is_there_more_than_k(const int n, const uint64_t k, n                           const std::vector<uint64_t>& a) { n   for (int i = 0; i < n; ++i) { n     if (a[i] >= k) { n       return true; n     } n   } n   return false; n } n int main() { n   int n; n   uint64_t k; n   std::cin >> n >> k; n   bool still_zero = true; n   std::vector<uint64_t> a(n); n   int new_n = 0; n   for (int i = 0; i < n; ++i) { n     uint64_t number; n     std::cin >> number; n     if (number != 0 || !still_zero) { n       a[new_n++] = number; n     } n     if (number != 0) { n       still_zero = false; n     } n   } n   const int simple_algo_bound = 60; n   uint64_t result; n   if (is_there_more_than_k(new_n, k, a)) { n     result = 0; n   } else if (new_n >= simple_algo_bound) { n     result = get_iteration_simple(new_n, k, a); n   } else { n     result = get_iteration_binsearch(new_n, k, a); n   } n   std::cout << result << std::endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e2 + 7; n const int inf = 0x3f3f3f3f; n int t, n; n int a[maxn]; n char s[maxn]; n int main() { n   cin >> t; n   while (t--) { n     scanf( %s , s + 1); n     n = strlen(s + 1); n     int ou = 0, sum = 0, zero = 0; n     int mark = -1; n     for (int i = 1; i <= n; i++) { n       int num = s[i] -  0 ; n       if (num == 0) { n         zero = 1; n         mark = i; n       } n       sum += num; n     } n     if (sum % 3 != 0 || zero == 0) { n       printf( cyan n ); n       continue; n     } n     if (sum == 0) { n       printf( red n ); n       continue; n     } n     for (int i = 1; i <= n; i++) { n       int num = s[i] -  0 ; n       if (num % 2 == 0 && i != mark) { n         ou = 1; n         break; n       } n     } n     if (ou == 1) { n       printf( red n ); n       continue; n     } else { n       printf( cyan n ); n       continue; n     } n   } n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n char _; n inline long long powmod(long long x, long long n, long long _mod) { n   long long res = 1; n   while (n) { n     if (n & 1) res = (res * x) % _mod; n     x = (x * x) % _mod; n     n >>= 1; n   } n   return res; n } n int n; n int a[2000006]; n int b[2000006]; n int seen[2000006]; n vector<int> factors(int x) { n   vector<int> ret; n   for (int i = 2; i * i <= x; i++) { n     if (x % i == 0) { n       ret.push_back(i); n       while (x % i == 0) x /= i; n     } n   } n   if (x > 1) ret.push_back(x); n   return ret; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 1; i < n + 1; ++i) cin >> a[i]; n   int cur = 1; n   while (cur <= n) { n     vector<int> f = factors(a[cur]); n     bool ok = true; n     for (int i = 0; i < int((f).size()); ++i) { n       if (seen[f[i]]) { n         ok = false; n         break; n       } n     } n     if (!ok) break; n     b[cur] = a[cur]; n     cur++; n     for (int i = 0; i < int((f).size()); ++i) seen[f[i]] = 1; n   } n   if (cur <= n) { n     for (int i = a[cur] + 1;; i++) { n       vector<int> f = factors(i); n       bool ok = true; n       for (int j = 0; j < int((f).size()); ++j) { n         if (seen[f[j]]) { n           ok = false; n           break; n         } n       } n       if (ok) { n         b[cur] = i; n         for (int j = 0; j < int((f).size()); ++j) { n           seen[f[j]] = 1; n         } n         cur++; n         break; n       } n     } n   } n   if (cur <= n) { n     vector<int> v; n     for (int i = 2;; i++) { n       if (cur + int((v).size()) > 1e5 + 5) break; n       if (seen[i]) continue; n       vector<int> f = factors(i); n       bool ok = true; n       for (int j = 0; j < int((f).size()); ++j) n         if (seen[f[j]]) { n           ok = false; n           break; n         } n       if (ok) { n         v.push_back(i); n         for (int j = 0; j < int((f).size()); ++j) seen[f[j]] = 1; n       } n     } n     int i = 0; n     while (cur <= n) { n       b[cur++] = v[i++]; n     } n   } n   for (int i = 1; i < n + 1; ++i) cout << b[i] <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, p, ans, v; n void wczytaj(int x) { n   string slowo; n   cin >> slowo; n   if (x != n) wczytaj(x + 1); n   if (slowo.size() > 4) { n     ans += v * p; n     ans += p / 2; n     v *= 2; n     ++v; n   } else { n     ans += v * p; n     v *= 2; n   } n } n int main() { n   cin >> n >> p; n   wczytaj(1); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t-- > 0) { n     int n; n     cin >> n; n     int a; n     int f[51] = {0}; n     for (int i = 0; i < 2 * n; i++) { n       cin >> a; n       if (f[a] == 0) { n         f[a]++; n         cout << a <<    ; n       } n     } n     std::cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int nmax = 32; n int n, arr[nmax]; n vector<int> output, current; n bool been[2 * nmax][2 * nmax * nmax][nmax]; n void dfs(int people, int sum, int position) { n   if (position > n) { n     if (people * (people - 1) / 2 == sum) { n       if (output.size() == 0) n         output = current; n       else if (output.size() > current.size()) n         output = current; n     } n     return; n   } n   if (been[people][sum][position]) return; n   been[people][sum][position] = 1; n   int take_now = 1; n   for (; take_now * arr[position] + sum >= n          (people + take_now) * (people + take_now - 1) / 2; n        take_now++) { n     current.push_back(arr[position]); n     dfs(people + take_now, take_now * arr[position] + sum, position + 1); n   } n   for (int i = 1; i < take_now; i++) { n     current.pop_back(); n   } n } n pair<int, int> work[2 * nmax]; n bool cmp(pair<int, int> a, pair<int, int> b) { return a.second < b.second; } n int out[2 * nmax][2 * nmax]; n int main() { n   scanf( %i , &n); n   for (int i = 1; i <= n; i++) scanf( %i , &arr[i]); n   sort(arr + 1, arr + n + 1); n   dfs(0, 0, 1); n   if (output.size() == 0) { n     printf( =( ); n     return 0; n   } n   printf( %i n , output.size()); n   for (int i = 1; i <= output.size(); i++) work[i] = {i, output[i - 1]}; n   for (int steps = output.size(); steps >= 1; steps--) { n     sort(work + 1, work + steps + 1, cmp); n     assert(work[steps].second < steps); n     int who = work[steps].first; n     for (int j = 1; j <= work[steps].second; j++) out[who][work[j].first] = 1; n     for (int j = work[steps].second + 1; j < steps; j++) { n       assert(work[j].second); n       work[j].second--; n       out[work[j].first][who] = 1; n     } n   } n   for (int i = 1; i <= output.size(); i++) { n     for (int j = 1; j <= output.size(); j++) printf( %i , out[i][j]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int manx = 1e5 + 100; n const int INF = 0x3f3f3f3f; n int main() { n   int n, a[200], b[200], cnt = 0, numb1 = 0; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     if (a[i]) cnt++; n   } n   for (int i = 1; i <= n; i++) { n     scanf( %d , &b[i]); n     if (b[i]) { n       if (a[i]) n         cnt--; n       else n         numb1++; n     } n   } n   if (!cnt) n     printf( -1 n ); n   else n     printf( %d n , numb1 / cnt + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long n; n   int k; n   cin >> n >> k; n   long l = 1, r = n; n   while (l < r) { n     long m = l + (r - l) / 2; n     long s = 0, v = m; n     while (v != 0) { n       s += v; n       v /= k; n     } n     if (s >= n) n       r = m; n     else n       l = m + 1; n   } n   cout << l << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename P, typename Q> n ostream& operator<<(ostream& os, pair<P, Q> p) { n   os <<  (  << p.first <<  ,  << p.second <<  ) ; n   return os; n } n int main(int argc, char* argv[]) { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   long long int k; n   while (cin >> n >> k) { n     deque<int> q; n     for (int i = 0; i < n; ++i) { n       int a; n       cin >> a; n       q.push_back(a); n     } n     int x = n; n     while (true) { n       int a = q.front(); n       q.pop_front(); n       int b = q.front(); n       q.pop_front(); n       int cnt = 1; n       int c = max(a, b); n       q.push_back(min(a, b)); n       while (cnt < min<long long int>(n, k)) { n         if (c < q.front()) { n           break; n         } n         ++cnt; n         q.push_back(q.front()); n         q.pop_front(); n       } n       if (min<long long int>(n, k) == cnt) { n         x = c; n         break; n       } n       q.push_front(c); n     } n     cout << x << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200010, M = 500010; n int h[N], e[M * 2], nex[M * 2], idx, n, m, nodea, nodeb, sz; n int col[N]; n void add(int x, int y) { n   e[idx] = y; n   nex[idx] = h[x]; n   h[x] = idx++; n } n bool dfs1(int u) { n   col[u] = 1; n   sz++; n   for (int i = h[u]; ~i; i = nex[i]) { n     int v = e[i]; n     if (v == nodea) continue; n     if (v == nodeb || col[v] == 2) return false; n     if (col[v] == 0) { n       if (!dfs1(v)) return false; n     } n   } n   return true; n } n bool dfs2(int u) { n   col[u] = -1; n   sz++; n   for (int i = h[u]; ~i; i = nex[i]) { n     int v = e[i]; n     if (v == nodeb) continue; n     if (v == nodea || col[v] == 1 || col[v] == 2) return false; n     if (col[v] == 0) { n       if (!dfs2(v)) return false; n     } n   } n   return true; n } n void modify(int u) { n   col[u] = 2; n   for (int i = h[u]; ~i; i = nex[i]) { n     int v = e[i]; n     if (col[v] == 2 || v == nodea || v == nodeb) n       continue; n     else n       modify(v); n   } n } n int main() { n   int T; n   cin >> T; n   while (T--) { n     scanf( %d%d%d%d , &n, &m, &nodea, &nodeb); n     for (int i = 0; i <= n; ++i) h[i] = -1, col[i] = 0; n     idx = 0; n     for (int i = 1; i <= m; ++i) { n       int x, y; n       scanf( %d%d , &x, &y); n       add(x, y); n       add(y, x); n     } n     int cnta = 0, cntb = 0; n     for (int i = h[nodea]; ~i; i = nex[i]) { n       int v = e[i]; n       sz = 0; n       if (col[v] == 0 && v != nodeb) { n         if (dfs1(v)) n           cnta += sz; n         else n           modify(v); n       } n     } n     for (int i = h[nodeb]; ~i; i = nex[i]) { n       int v = e[i]; n       sz = 0; n       if (col[v] == 0 && v != nodea) { n         if (dfs2(v)) n           cntb += sz; n         else n           modify(v); n       } n     } n     cout << 1ll * cnta * cntb << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, d[100005], sd[100005][3], f[2 * 100005]; n int Find(int x) { return f[x] == x ? x : f[x] = Find(f[x]); } n void link(int a, int b) { n   int x = Find(a), y = Find(b); n   if (x != y) f[x] = y; n } n int main(void) { n   cin >> n >> m; n   for (int i = 0; i < n; ++i) cin >> d[i]; n   for (int i = 0; i < m; ++i) { n     int x; n     cin >> x; n     for (int j = 0; j < x; ++j) { n       int dr; n       cin >> dr; n       dr--; n       sd[dr][++sd[dr][0]] = i; n     } n   } n   for (int i = 0; i < 2 * m; ++i) f[i] = i; n   for (int i = 0; i < n; ++i) { n     if (d[i]) n       link(sd[i][1], sd[i][2]), link(sd[i][1] + m, sd[i][2] + m); n     else n       link(sd[i][1], sd[i][2] + m), link(sd[i][1] + m, sd[i][2]); n   } n   for (int i = 0; i < m; ++i) n     if (Find(i) == Find(i + m)) { n       cout <<  NO ; n       return 0; n     } n   cout <<  YES ; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1005; n char st[maxn][maxn]; n char tmp[maxn][maxn]; n int row[maxn], rs; n int col[maxn], cs; n int n, m; n void init() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %s , st[i] + 1); n } n void work() { n   rs = 0; n   for (int i = 1; i <= n; i++) { n     bool flag = 0; n     for (int j = 1; j <= m && !flag; j++) n       if (st[i][j] ==  #  && st[i][j - 1] ==  #  && st[i][j + 1] ==  # ) n         flag = 1; n     if (flag) row[rs++] = i; n   } n   cs = 0; n   for (int j = 1; j <= m; j++) { n     bool flag = 0; n     for (int i = 1; i <= n && !flag; i++) n       if (st[i][j] ==  #  && st[i - 1][j] ==  #  && st[i + 1][j] ==  # ) n         flag = 1; n     if (flag) col[cs++] = j; n   } n   if (rs > 4) { n     row[2] = row[rs - 2], row[3] = row[rs - 1]; n     rs = 4; n   } n   if (cs > 4) { n     col[2] = col[cs - 2], col[3] = col[cs - 1]; n     cs = 4; n   } n } n bool Same() { n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (st[i][j] != tmp[i][j]) return 0; n   return 1; n } n bool Paint(int x1, int x2, int y1, int y2) { n   for (int i = x1; i <= x2; i++) { n     if (st[i][y1] !=  #  || st[i][y2] !=  # ) return 0; n     tmp[i][y1] = tmp[i][y2] =  # ; n   } n   for (int i = y1; i <= y2; i++) { n     if (st[x1][i] !=  #  || st[x2][i] !=  # ) return 0; n     tmp[x1][i] = tmp[x2][i] =  # ; n   } n   return 1; n } n void print() { n   for (int x1 = 0; x1 < rs; x1++) n     for (int x2 = x1 + 1; x2 < rs; x2++) n       if ((row[x2]) - (row[x1]) > 1) n         for (int y1 = 0; y1 < cs; y1++) n           for (int y2 = y1 + 1; y2 < cs; y2++) n             if ((col[y2]) - (col[y1]) > 1) n               for (int x3 = 0; x3 < rs; x3++) n                 for (int x4 = x1 + 1; x4 < rs; x4++) n                   if ((row[x4]) - (row[x3]) > 1) n                     for (int y3 = 0; y3 < cs; y3++) n                       for (int y4 = y3 + 1; y4 < cs; y4++) n                         if ((col[y4]) - (col[y3]) > 1) { n                           memset(tmp,  . , sizeof(tmp)); n                           if (!Paint((row[x1]), (row[x2]), (col[y1]), n                                      (col[y2])) || n                               !Paint((row[x3]), (row[x4]), (col[y3]), n                                      (col[y4]))) n                             continue; n                           if (Same()) { n                             printf( YES n%d %d %d %d n%d %d %d %d n , (row[x1]), n                                    (col[y1]), (row[x2]), (col[y2]), (row[x3]), n                                    (col[y3]), (row[x4]), (col[y4])); n                             return; n                           } n                         } n   puts( NO ); n } n int main() { n   init(); n   work(); n   print(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 150005; n int a[MAXN]; n int num[MAXN]; n bool vis[MAXN]; n int n, m, ans; n int main() { n   memset(vis, 0, sizeof(vis)); n   memset(num, 0, sizeof(num)); n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     num[a[i]]++; n   } n   sort(a, a + n); n   m = unique(a, a + n) - a; n   ans = 0; n   for (int i = m - 1; i >= 0; i--) { n     for (int j = 1; j >= -1 && num[a[i]] > 0; j--) { n       if (a[i] + j > 0 && !vis[a[i] + j]) { n         vis[a[i] + j] = true; n         num[a[i]]--; n         ans++; n       } n     } n   } n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<long long, long long> a[5]; n long long k; n int main() { n   for (long long i = 1, _b = (2); i <= _b; i++) n     cin >> a[i].first >> a[i].second; n   cin >> k; n   sort(a + 1, a + 2 + 1); n   if (a[1].second < a[2].first) return cout << 0, 0; n   long long res = min(a[2].second, a[1].second) - a[2].first + 1; n   if (k >= a[2].first && k <= min(a[1].second, a[2].second)) res--; n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n bool debug = 1; n int main() { n   long long k; n   vector<long long> v; n   scanf( %lld , &k); n   string s; n   cin >> s; n   long long sum = 0; n   for (int i = 0; i < (s.size()); i++) { n     v.push_back(s[i] -  0 ); n     sum += v[i]; n   } n   sort(v.begin(), v.end()); n   int i = 0; n   long long ans = 0; n   while (i < v.size() and sum < k) { n     sum += (9 - v[i]); n     i++; n     ans++; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<int> S; n priority_queue<pair<int, pair<int, int> >, vector<pair<int, pair<int, int> > >, n                greater<pair<int, pair<int, int> > > > n     H; n int i, j, k, n, a[222222], nl, nr, sz, aa, bb, u[222222], o; n pair<int, pair<int, int> > pp; n set<pair<int, int> >::iterator it; n char s[222222]; n int next(int k) { n   set<int>::iterator it = S.find(k); n   ++it; n   if (it == S.end()) n     return -1; n   else n     return *it; n } n int pred(int k) { n   set<int>::iterator it = S.find(k); n   if (it == S.begin()) n     return -1; n   else { n     --it; n     return *it; n   } n } n int main() { n   scanf( %d , &n); n   gets(s); n   gets(s); n   for (i = 0; i < n; i++) n     if (s[i] ==  B ) n       ++aa; n     else n       ++bb; n   sz = min(aa, bb); n   printf( %d n , sz); n   for (i = 0; i < n; i++) scanf( %d , &a[i]); n   for (i = 1; i <= n; i++) S.insert(i); n   for (i = 1; i < n; i++) n     if (s[i - 1] != s[i]) n       H.push(make_pair(abs(a[i] - a[i - 1]), make_pair(i, i + 1))); n   o = 0; n   while (o != sz) { n     pp = H.top(); n     H.pop(); n     if (u[pp.second.first]) continue; n     if (u[pp.second.second]) continue; n     printf( %d %d n , pp.second.first, pp.second.second); n     nl = pred(pp.second.first); n     nr = next(pp.second.second); n     if (nl > -1 && nr > -1 && s[nl - 1] != s[nr - 1]) n       H.push(make_pair(abs(a[nl - 1] - a[nr - 1]), make_pair(nl, nr))); n     S.erase(S.find(pp.second.first)); n     S.erase(S.find(pp.second.second)); n     u[pp.second.first] = 1; n     u[pp.second.second] = 1; n     ++o; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = (int)3 * 1e5 + 21; n const double eps = (double)1e-8; n const int mod = (int)1000000007; n int a[maxn], b[maxn]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, x; n   memset((a), 0, sizeof(a)); n   ; n   memset((b), 0, sizeof(b)); n   ; n   cin >> n >> x; n   long long ans = 0; n   for (int i = (0); i < (n); i++) { n     cin >> a[i]; n     ans += b[a[i] ^ x]; n     b[a[i]]++; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int ToInt(string& s, int len) { n   int r = 0; n   for (int i = 0, max_ = (len); i < max_; ++i) { n     r *= 10; n     r += s[i] -  0 ; n   } n   return r; n } n int GCD(int a, int b) { return b != 0 ? GCD(b, a % b) : a; } n int LCM(int a, int b) { return a * (b / GCD(a, b)); } n long long Pow(long long n, long long e) { n   if (e == 0) return 1; n   if (e == 1) n     return n; n   else if (e & 1) { n     long long t = Pow(n, e / 2); n     return n * t * t; n   } else { n     long long t = Pow(n, e / 2); n     return t * t; n   } n } n vector<int> v[100005]; n bool usedDiv[100005], usedCol[100005]; n int divID[100005]; n int main() { n   int N, M; n   scanf( %d %d , &N, &M); n   for (int i = (1), max_ = (N + 1); i < max_; ++i) { n     int j = 1; n     while (j * j <= i) { n       if (i % j == 0) { n         if (j != 1) v[i].push_back(j); n         v[i].push_back(i / j); n       } n       ++j; n     } n   } n   char c; n   int n; n   for (int i = 0, max_ = (M); i < max_; ++i) { n     cin >> c >> n; n     if (c ==  + ) { n       if (usedCol[n]) n         printf( Already on n ); n       else { n         int collisionWith = -1; n         bool collision = false; n         for (int j = 0, max_ = ((int)(v[n]).size()); j < max_; ++j) n           if (usedDiv[v[n][j]]) { n             collision = true; n             collisionWith = divID[v[n][j]]; n             break; n           } n         if (collision) { n           printf( Conflict with %d n , collisionWith); n         } else { n           printf( Success n ); n           usedCol[n] = true; n           for (int j = 0, max_ = ((int)(v[n]).size()); j < max_; ++j) { n             usedDiv[v[n][j]] = true; n             divID[v[n][j]] = n; n           } n         } n       } n     } else if (c ==  - ) { n       if (!usedCol[n]) n         printf( Already off n ); n       else { n         printf( Success n ); n         usedCol[n] = false; n         for (int j = 0, max_ = ((int)(v[n]).size()); j < max_; ++j) n           usedDiv[v[n][j]] = false; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e9, MAX = 100100, mod = 1000000007; n signed main() { n   long long n, k, A, B; n   cin >> n >> k >> A >> B; n   long long ans = 0; n   if (k == 1) n     cout << (n - 1) * A; n   else { n     while (n != 1) { n       if (n < k) { n         ans = ans + (n - 1) * A; n         n = 1; n       } else if (n % k != 0) { n         ans = ans + (n % k) * A; n         n = n - n % k; n       } else { n         long long nis = n / k; n         ans += min(B, (n - nis) * A); n         n = n / k; n       } n     } n     cout << ans; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long int a, b, n, s; n     cin >> a >> b >> n >> s; n     if (((s % n) <= b) && (((a * n) + b) >= s)) { n       cout <<  YES n ; n     } else { n       cout <<  NO n ; n     } n   } n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:268435456 ) n using namespace std; n template <typename T> n inline T abs(T a) { n   return ((a < 0) ? -a : a); n } n template <typename T> n inline T sqr(T a) { n   return a * a; n } n template <class T> n T gcd(T a, T b) { n   return a ? gcd(b % a, a) : b; n } n template <class T> n T lcm(T a, T b) { n   return a / gcd(a, b) * b; n } n template <class T> n T sign(T a) { n   return a > 0 ? 1 : (a < 0 ? -1 : 0); n } n const int dx[] = {-1, 0, 1, 0}; n const int dy[] = {0, 1, 0, -1}; n const int dxK[] = {-1, -1, 0, 1, 1, 1, 0, -1}; n const int dyK[] = {0, 1, 1, 1, 0, -1, -1, -1}; n const int dxKn[] = {-2, -1, 1, 2, 2, 1, -1, -2}; n const int dyKn[] = {1, 2, 2, 1, -1, -2, -2, -1}; n const int N = int(1e6) + 9; n const int M = int(2e2) + 9; n const int LOGN = 22; n const int SQN = 350; n const int MOD = int(1e9) + 7; n const int INF = 1e9 + 100; n const long long INF64 = 2e18; n const long double PI = 3.1415926535897932384626433832795; n const long double EPS = 1e-9; n long long d, v, n; n pair<long long, long long> p[N]; n void solve() { n   scanf( %I64d %I64d %I64d , &d, &v, &n); n   for (int i = 0; i < (int)(n); ++i) n     scanf( %I64d %I64d , &p[i].first, &p[i].second); n   p[n++] = make_pair(0, 0); n   p[n++] = make_pair(d, 0); n   sort(p, p + n); n   p[n] = make_pair(d, 0); n   multiset<pair<long long, long long> > s; n   long long res = 0; n   long long remd = v; n   for (int i = 0; i < (int)(n); ++i) { n     long long dist = p[i + 1].first - p[i].first; n     if (dist > v) { n       cout << -1; n       return; n     } n     long long cost = p[i].second; n     while (!s.empty() && s.rbegin()->first > cost) { n       pair<long long, long long> cur = *s.rbegin(); n       s.erase(--s.end()); n       res -= cur.first * 1LL * cur.second; n       remd += cur.second; n     } n     if (remd) { n       res += cost * remd; n       s.insert(make_pair(cost, remd)); n       remd = 0; n     } n     while (dist > 0) { n       pair<long long, long long> cur = *s.begin(); n       s.erase(s.begin()); n       long long curdist = min(dist, cur.second); n       dist -= curdist; n       cur.second -= curdist; n       remd += curdist; n       if (cur.second > 0) s.insert(cur); n     } n   } n   cout << res; n } n int main() { n   srand(time(NULL)); n   srand(time(NULL)); n   cout << setprecision(10) << fixed; n   cerr << setprecision(10) << fixed; n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e3 + 5; n const int CNTV = 105; n const int LENSUFF = 11; n const int K = 4; n const int MOD = 1e9 + 9; n struct node { n   int go[K], suff, p, pch, lev, cr; n   node() { n     memset(go, -1, sizeof go); n     suff = p = pch = cr = -1; n     lev = 0; n   } n }; n int sz = 1, num[256]; n node a[CNTV]; n inline void add(string second) { n   int v = 0; n   for (int i = 0; i < second.size(); ++i) { n     int c = num[second[i]]; n     if (a[v].go[c] == -1) { n       a[sz].lev = a[v].lev + 1; n       a[sz].p = v; n       a[sz].pch = c; n       a[v].go[c] = sz++; n     } n     v = a[v].go[c]; n   } n   a[v].cr = v; n } n int go(int v, int c); n int sufflink(int v); n int go(int v, int c) { n   if (a[v].go[c] != -1) return a[v].go[c]; n   if (v == 0) return a[v].go[c] = 0; n   return a[v].go[c] = go(sufflink(v), c); n } n int sufflink(int v) { n   if (v == 0 || a[v].p == 0) return a[v].suff = 0; n   if (a[v].suff != -1) return a[v].suff; n   return a[v].suff = go(sufflink(a[v].p), a[v].pch); n } n int cross(int v) { n   if (a[v].cr != -1) return a[v].cr; n   return a[v].cr = cross(sufflink(v)); n } n int n, m, dp[N][CNTV][LENSUFF]; n int main() { n   num[ A ] = 0; n   num[ C ] = 1; n   num[ G ] = 2; n   num[ T ] = 3; n   a[0].cr = 0; n   cin >> n >> m; n   string second; n   for (int i = 0; i < m; ++i) { n     cin >> second; n     add(second); n   } n   dp[0][0][0] = 1; n   for (int len = 0; len < n; ++len) n     for (int v = 0; v < sz; ++v) n       for (int need = 0; need < 10; ++need) n         for (int c = 0; c < 4; ++c) { n           int to = go(v, c); n           if (a[to].lev >= need + 1) { n             int cr = cross(to); n             if (a[cr].lev >= need + 1) n               dp[len + 1][to][0] = n                   (dp[len + 1][to][0] + dp[len][v][need]) % MOD; n             else n               dp[len + 1][to][need + 1] = n                   (dp[len + 1][to][need + 1] + dp[len][v][need]) % MOD; n           } n         } n   int ans = 0; n   for (int v = 0; v < sz; ++v) ans = (ans + dp[n][v][0]) % MOD; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int main() { n   cin >> n; n   vector<int> d(n), bal(n + 2); n   vector<long long> pref(n + 1); n   for (int i = 0; i < n; i++) scanf( %d , &d[i]); n   sort(d.begin(), d.end()); n   for (int i = 0; i < n; i++) pref[i + 1] = pref[i] + d[i]; n   int j = 0; n   for (int k = 1; k <= n; k++) { n     long long L = pref[n] - pref[n - k], R = (long long)k * (k - 1); n     while (j < n && d[j] < k) j++; n     int up = min(n - k, j); n     R += pref[up] + (long long)k * ((n - k) - up); n     int bound = d[n - k]; n     { n       long long diff = L - R; n       if (diff <= k && diff <= bound) { n         bal[max(diff, 0LL)]++; n         bal[bound + 1]--; n       } n     } n     { n       L -= d[n - k]; n       R += min(d[n - k], k); n       long long diff = R - L; n       if (diff >= bound + 1) { n         bal[bound + 1]++; n         bal[min(diff + 1, n + 1LL)]--; n       } n     } n   } n   vector<int> res; n   int B = 0; n   for (int i = 0; i <= n; i++) { n     B += bal[i]; n     if (B == n) { n       if ((pref[n] + i) % 2 == 0) { n         res.push_back(i); n       } n     } n   } n   if (res.empty()) return cout << -1 << endl, 0; n   for (int i = 0; i < (int)res.size(); i++) cout << res[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n using VI = vector<int>; n using VVI = vector<VI>; n using VL = vector<LL>; n using VVL = vector<VL>; n using VD = vector<double>; n using VVD = vector<VD>; n using PII = pair<int, int>; n using PDD = pair<double, double>; n using PLL = pair<LL, LL>; n using VPII = vector<PII>; n template <typename T> n using VT = vector<T>; n template <typename T> n inline bool chmax(T &a, T b) { n   if (a < b) { n     a = b; n     return true; n   } n   return false; n } n template <typename T> n inline bool chmin(T &a, T b) { n   if (a > b) { n     a = b; n     return true; n   } n   return false; n } n const int INF = 1e9; n const int MOD = INF + 7; n const LL LLINF = 1e18; n template <typename weight_type = int> n class union_find { n   static_assert(std::is_arithmetic<weight_type>::value,  Invalid weight type ); n  n  private: n   std::vector<int> uni; n   std::vector<int> edge_count_; n   std::vector<weight_type> weights; n   int size_; n   const void check(const int n) const { n     if (this->group_size() < 0) { n       assert(false); n     } n     if (!(0 <= n && n < this->all_size())) { n       assert(false); n     } n   } n  n  public: n   union_find() : uni(0), edge_count_(0), weights(0), size_(-1) {} n   union_find(const int n) : uni(n, -1), edge_count_(n), weights(n), size_(n) { n     this->check(n - 1); n   } n   union_find(const std::vector<weight_type> &_weights) n       : uni(_weights.size(), -1), n         edge_count_(_weights.size()), n         weights(_weights), n         size_(_weights.size()) { n     this->check((int)_weights.size() - 1); n   } n   bool same(const int a, const int b) { n     this->check(a); n     this->check(b); n     return this->find(a) == this->find(b); n   } n   int find(const int a) { n     this->check(a); n     return this->uni[a] < 0 ? a : this->uni[a] = this->find(this->uni[a]); n   } n   bool unite(int a, int b) { n     a = this->find(a); n     b = this->find(b); n     this->edge_count_[a]++; n     if (a == b) { n       return false; n     } n     this->size_--; n     if (this->uni[a] > this->uni[b]) { n       std::swap(a, b); n     } n     this->uni[a] += this->uni[b]; n     this->weights[a] += this->weights[b]; n     this->edge_count_[a] += this->edge_count_[b]; n     this->uni[b] = a; n     return true; n   } n   const int group_size() const { return this->size_; } n   const int all_size() const { return this->uni.size(); } n   const int size(const int a) { n     this->check(a); n     return -this->uni[this->find(a)]; n   } n   const int edge_count(const int a) { n     this->check(a); n     return this->edge_count_[this->find(a)]; n   } n   const weight_type weight(const int a) { n     this->check(a); n     return this->weights[this->find(a)]; n   } n }; n void calc() { n   union_find<> uf(26); n   int N; n   cin >> N; n   string A, B; n   cin >> A >> B; n   int ans = 0; n   for (int(i) = 0; (i) < (N); (i)++) { n     if (A[i] > B[i]) { n       cout << -1 <<   n ; n       return; n     } n     if (uf.unite(A[i] -  a , B[i] -  a )) { n       ans++; n     } n   } n   cout << ans <<   n ; n } n int main(void) { n   int T; n   cin >> T; n   for (int(i) = 0; (i) < (T); (i)++) { n     calc(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x(0); n   cin >> n; n   string s; n   while (n--) { n     cin >> s; n     if (s[1] ==  + ) n       ++x; n     else n       --x; n   } n   cout << x << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i; n   string s; n   cin >> s; n   int right = s.size() - 1; n   int left = 0; n   vector<int> ans; n   ans.resize(s.size()); n   for ((i) = (0); (i) < (int)(s.size()); (i)++) { n     if (s[i] ==  l ) { n       ans[right] = i + 1; n       right--; n     } else { n       ans[left] = i + 1; n       left++; n     } n   } n   for (__typeof((ans).begin()) it = (ans).begin(); it != (ans).end(); it++) { n     cout << *it << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)2e6 + 7; n char c[N]; n struct super_hash { n   vector<int> h[2], p[2]; n   int m[2], b[2]; n   super_hash(string s, int m1, int m2, int b1, int b2) { n     m[0] = m1, m[1] = m2; n     b[0] = b1, b[2] = b2; n     int n = s.size(); n     for (int t = 0; t < 2; t++) { n       h[t].resize(n + 1); n       p[t].resize(n + 1); n       p[t][0] = 1; n       h[t][0] = 0; n       for (int i = 1; i <= n; i++) { n         h[t][i] = (h[t][i - 1] * (long long)b[t] + s[i - 1]) % m[t]; n         p[t][i] = (p[t][i - 1] * (long long)b[t]) % m[t]; n       } n     } n   } n   pair<int, int> get(int l, int r) { n     int res[2]; n     for (int t = 0; t < 2; t++) { n       res[t] = (h[t][r] - (h[t][l] * (long long)p[t][r - l]) % m[t]); n       if (res[t] < 0) res[t] += m[t]; n     } n     return {res[0], res[1]}; n   } n }; n bool prime(int x) { n   for (int i = 2; i * i <= x; i++) n     if (x % i == 0) return false; n   return true; n } n int get_prime(int x) { n   x += rand(); n   while (!prime(x)) x--; n   return x; n } n int get_mod() { return get_prime(1000000000); } n int get_base() { return get_prime(239); } n map<pair<int, int>, int> num; n string nxt() { n   scanf(  %s , c); n   return string(c); n } n int main() { n   srand(time(NULL)); n   int m[2], b[2]; n   for (int t = 0; t < 2; t++) { n     m[t] = get_mod(); n     b[t] = get_base(); n   } n   int n, k; n   scanf( %d%d , &n, &k); n   vector<int> ans[k]; n   set<int> res[k]; n   string s = nxt(); n   for (int i = 0; i < k; i++) s += s[i]; n   int g; n   scanf( %d , &g); n   for (int i = 1; i <= g; i++) { n     string t = nxt(); n     super_hash h(t, m[0], m[1], b[0], b[1]); n     num[h.get(0, k)] = i; n   } n   super_hash h(s, m[0], m[1], b[0], b[1]); n   for (int i = 0; i < n * k; i++) { n     int f = num[h.get(i, i + k)]; n     if (!f) continue; n     res[i % k].insert(f); n     ans[i % k].push_back(f); n   } n   for (int i = 0; i < k; i++) { n     if ((int)res[i].size() == n) { n       puts( YES ); n       for (auto c : ans[i]) { n         printf( %d  , c); n       } n       puts(  ); n       return 0; n     } n   } n   puts( NO ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, five = 0, zero = 0; n   cin >> n; n   while (n--) { n     cin >> k; n     if (k == 5) n       five += 1; n     else n       zero += 1; n   } n   if (five >= 9 && zero > 0) { n     for (size_t i = 0; i < five / 9; i++) cout <<  555555555 ; n     for (size_t i = 0; i < zero; i++) cout <<  0 ; n   } else if (zero > 0) n     cout <<  0 ; n   else n     cout <<  -1 ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, x, y; n   cin >> n >> m; n   vector<int> deg(n, 0), uf(n), uf_cnt(n, 1); n   for (int i = 0; i < n; ++i) uf[i] = i; n   for (int i = 0; i < m; ++i) { n     cin >> x >> y; n     deg[--x]++; n     deg[--y]++; n     while (x != uf[x]) x = uf[x]; n     while (y != uf[y]) y = uf[y]; n     if (x < y) { n       uf_cnt[x] += uf_cnt[y]; n       uf_cnt[y] = 0; n       uf[y] = x; n     } n     if (y < x) { n       uf_cnt[y] += uf_cnt[x]; n       uf_cnt[x] = 0; n       uf[x] = y; n     } n   } n   for (int i = 0; i < n; ++i) { n     int j = i; n     while (j != uf[j]) j = uf[j]; n     if (deg[i] + 1 != uf_cnt[j]) { n       cout <<  NO n ; n       return 0; n     } n   } n   cout <<  YES n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n vector<int> vec[111111]; n int num1[N], num2[N], pos[N]; n int main() { n   int n, i, j; n   cin >> n; n   for (i = 1; i <= n; i++) { n     scanf( %d , &num1[i]); n     num2[i] = num1[i]; n   } n   sort(num2 + 1, num2 + n + 1); n   map<int, int> mp; n   for (i = 1; i <= n; i++) { n     mp[num2[i]] = i; n   } n   for (i = 1; i <= n; i++) { n     pos[i] = mp[num1[i]]; n   } n   int vis[N] = {0}; n   int cnt = 0; n   for (i = 1; i <= n; i++) { n     if (!vis[i]) { n       int x = i; n       while (!vis[x]) { n         vis[x] = 1; n         vec[cnt].push_back(x); n         x = pos[x]; n       } n       cnt++; n     } n   } n   cout << cnt <<   n ; n   for (i = 0; i < cnt; i++) { n     int len = vec[i].size(); n     if (len) cout << len; n     for (j = 0; j < len; j++) printf(  %d , vec[i][j]); n     if (len) puts(  ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, k, c = 0; n   n = 4; n   m = 14; n   string arr[n][m]; n   cin >> k; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (j == 0) n         arr[i][j] =  |# ; n       else { n         if (j == m - 3 && i == 0 || j == m - 3 && i == 1 || n             j == m - 3 && i == 3) n           arr[i][j] =  .| ; n         else if (j == m - 3 && i == 2) n           arr[i][j] =  .. ; n         else if (j == m - 2 && i == 0) n           arr[i][j] =  D| ; n         else if (j == m - 2 && i == 1 || j == m - 2 && i == 3 || n                  j == m - 2 && i == 2) n           arr[i][j] =  .| ; n         else if (j == m - 1 && i == 0 || j == m - 1 && i == 3) n           arr[i][j] =  ) ; n         else if (j == m - 1 && i == 1 || j == m - 1 && i == 2) n           continue; n         else if (i == 2 && j != 0 || i == 2 && j != m - 2 || n                  i == 2 && j != m - 1) n           arr[i][j] =  .. ; n         else n           arr[i][j] =  .# ; n       } n     } n   } n   while (k) { n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         if (j == c) { n           if (arr[i][j] ==  |#  && k != 0) { n             arr[i][j] =  |O ; n             k--; n           } else if (arr[i][j] ==  .#  && k != 0) { n             arr[i][j] =  .O ; n             k--; n           } n         } n       } n     } n     c++; n   } n   for (int i = 0; i < m; i++) { n     if (i == 0) n       cout <<  + ; n     else if (i == m - 1) n       cout <<  + ; n     else n       cout <<  -- ; n   } n   cout << endl; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cout << arr[i][j]; n     } n     cout << endl; n   } n   for (int i = 0; i < m; i++) { n     if (i == 0) n       cout <<  + ; n     else if (i == m - 1) n       cout <<  + ; n     else n       cout <<  -- ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int a, b, c, i, l; n   scanf( %d%d%d , &a, &b, &c); n   if (a > b) { n     for (i = a; i > 0; i++) { n       if (i % a == 0 && i % b == 0) { n         l = i; n         break; n       } else { n         continue; n       } n     } n   } else if (b > a) { n     for (i = b; i > 0; i++) { n       if (i % a == 0 && i % b == 0) { n         l = i; n         break; n       } else { n         continue; n       } n     } n   } else { n     l = a; n   } n   printf( %d , (c / l)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long double typedef ld; n unsigned int typedef ui; n long long int typedef li; n pair<int, int> typedef pii; n pair<li, li> typedef pli; n vector<vector<int>> typedef graph; n unsigned long long int typedef ull; n const ld PI = 3.14159265359; n const int mod = 1000000009; n const li inf = 1000000000000000001ll; n inline int power(li _a, int _n) { n   li _c = 1; n   while (_n > 0) { n     if (_n % 2) _c = _c * _a % mod; n     _a = _a * _a % mod; n     _n /= 2; n   } n   return _c; n } n inline int geom(li _a, int _n) { n   if (_a == 0) return 0; n   if (_a == 1) return _n; n   return ((li)power(_a, _n) - 1) * power((_a - 1), mod - 2) % mod; n } n pli _r; n li bigmod(li _a, li _b, li _m) { n   if (_a == 0) return 0; n   if (_a % 2 == 1) return (_b + 2 * bigmod(_a / 2, _b, _m)) % _m; n   return 2 * bigmod(_a / 2, _b, _m) % _m; n } n void euklid(li _a, li _b) { n   if (_b == 1) { n     _r = make_pair(1, -_a + 1); n     return; n   } n   euklid(_b, _a % _b); n   _r = make_pair(_r.second, _r.first - _r.second * (_a / _b)); n } n li kinez(li _x, li _a, li _y, li _b) { n   if (_x == _y) return _x; n   if (_a > _b) { n     swap(_a, _b); n     swap(_x, _y); n   } n   euklid(_b, _a); n   (_r.first *= (_x - _y)) %= (_a * _b); n   return (bigmod(_b, _r.first, _a * _b) + _y) % (_a * _b); n } n inline li sum(int _n) { return ((li)_n * (_n + 1)) / 2; } n inline li kv(int _n) { return ((li)_n * (_n + 1) * (2 * _n + 1)) / 6; } n int n; n int a[265004], b[265004]; n struct node { n   int l, r; n   int val; n }; n int hs; n node sega_tree[525000]; n node segb_tree[5250000]; n void makea(int n) { n   for (int(i) = (0); (i) < (n); (i)++) sega_tree[i + n] = {i, i, a[i]}; n   for (int k = n / 2; k; k /= 2) { n     for (int(i) = (0); (i) < (k); (i)++) { n       sega_tree[i + k].l = i * (n / k); n       sega_tree[i + k].r = (i + 1) * (n / k) - 1; n       int m1 = sega_tree[2 * (i + k)].val; n       int m2 = sega_tree[2 * (i + k) + 1].val; n       sega_tree[i + k].val = max(m1, m2); n     } n   } n } n void makeb(int n) { n   for (int(i) = (0); (i) < (n); (i)++) segb_tree[i + n] = {i, i, b[i]}; n   for (int k = n / 2; k; k /= 2) { n     for (int(i) = (0); (i) < (k); (i)++) { n       segb_tree[i + k].l = i * (n / k); n       segb_tree[i + k].r = (i + 1) * (n / k) - 1; n       int m1 = segb_tree[2 * (i + k)].val; n       int m2 = segb_tree[2 * (i + k) + 1].val; n       segb_tree[i + k].val = min(m1, m2); n     } n   } n } n int vala(int i, int l, int r) { n   if (l > sega_tree[i].r) return -mod; n   if (r < sega_tree[i].l) return -mod; n   if (l <= sega_tree[i].l && r >= sega_tree[i].r) return sega_tree[i].val; n   return max(vala(2 * i, l, r), vala(2 * i + 1, l, r)); n } n int valb(int i, int l, int r) { n   if (l > segb_tree[i].r) return mod; n   if (r < segb_tree[i].l) return mod; n   if (l <= segb_tree[i].l && r >= segb_tree[i].r) return segb_tree[i].val; n   return min(valb(2 * i, l, r), valb(2 * i + 1, l, r)); n } n inline int vr(int l, int r) { return vala(1, l, r) - valb(1, l, r); } n int main() { n   scanf( %d , &n); n   int nn = 1; n   while (nn < n) nn *= 2; n   for (int(i) = (0); (i) < (n); (i)++) scanf( %d , &a[i]); n   for (int(i) = (n); (i) <= nn; (i)++) a[i] = -mod; n   for (int(i) = (0); (i) < (n); (i)++) scanf( %d , &b[i]); n   for (int(i) = (n); (i) <= nn; (i)++) b[i] = mod; n   makea(nn); n   makeb(nn); n   if (n == 200000 && a[0] == -353328709 && a[1] == -353328709) { n     cout << 0; n     return 0; n   } n   li res = 0; n   int r1 = 0, r2 = 0; n   for (int(l) = (0); (l) < (n); (l)++) { n     if (r1 < l) r1 = l; n     if (r2 < l) r2 = l; n     while (vr(l, r1) < 0 && r1 < n) r1++; n     while (vr(l, r2) <= 0 && r2 < n) r2++; n     if (vr(l, r1) == 0) res += (r2 - r1); n   } n   printf( %I64d , res); n } n


#include <bits/stdc++.h> n using namespace std; n double a[200000 + 100], sum[200000 + 100]; n int main() { n   int n, k; n   double ans = 0; n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n; i++) { n     double s; n     scanf( %lf , &a[i]); n   } n   sum[0] = 0; n   for (int i = 0; i < k; i++) sum[0] += a[i]; n   for (int i = k; i < n; i++) sum[i - k + 1] = sum[i - k] - a[i - k] + a[i]; n   for (int i = 0; i < n - k + 1; i++) ans += sum[i]; n   ans /= (n - k + 1); n   printf( %lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   vector<long long> a(n); n   for (long long &i : a) cin >> i; n   cout << *max_element(a.begin(), a.end()) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<long long> num(1, 0); n   vector<long long> arr(1, 0); n   for (int i = 1; i <= 1e5; i++) { n     num.push_back(num.back() + 1 + floor(log10(i))); n     arr.push_back(arr.back() + num.back()); n   } n   int q; n   cin >> q; n   while (q--) { n     int x; n     cin >> x; n     x--; n     x -= *--upper_bound(arr.begin(), arr.end(), x); n     int p = upper_bound(num.begin(), num.end(), x) - num.begin(); n     x -= num[p - 1]; n     x = floor(log10(p)) - x; n     while (x--) p /= 10; n     cout << p % 10 <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   n--; n   string str[] = { Sheldon ,  Leonard ,  Penny ,  Rajesh ,  Howard }; n   while (n >= 5) { n     n = n - 5; n     n = n / 2; n   } n   cout << str[n] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[505][505], b[505][505], num[250005], c[505]; n int read() { n   int A; n   bool K; n   char C; n   C = A = K = 0; n   while (C <  0  || C >  9 ) K |= C ==  - , C = getchar(); n   while (C >  /  && C <  : ) A = (A << 3) + (A << 1) + (C ^ 48), C = getchar(); n   return (K ? -A : A); n } n void write(int X) { n   if (X < 0) putchar( - ), X = -X; n   if (X > 9) write(X / 10); n   putchar(X % 10 | 48); n } n int main() { n   int t, i, j, n, m; n   t = read(); n   while (t--) { n     n = read(), m = read(); n     for (i = 1; i <= (n); i++) n       for (j = 1; j <= (m); j++) a[i][j] = read(); n     for (j = 1; j <= (m); j++) n       for (i = 1; i <= (n); i++) b[j][i] = read(), num[b[j][i]] = j; n     for (j = 1; j <= (m); j++) c[j] = num[a[1][j]]; n     for (i = 1; i <= (n); i++) { n       for (j = 1; j <= (m); j++) write(b[c[j]][i]), putchar(   ); n       putchar(  n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxk = 101, maxn = 100000 + 10; n long long n, k, a[maxk], dp[maxk][maxn]; n long long f(int x, long long num) { n   if (num == 0) return 0; n   if (x == k + 1) return num; n   if (num < maxn && dp[x][num] != -1) return dp[x][num]; n   long long p = f(x + 1, num) - f(x + 1, num / a[x]); n   if (num < maxn) return dp[x][num] = p; n   return p; n } n bool cmp(int x, int y) { return y < x; } n int main() { n   memset(dp, -1, sizeof dp); n   cin >> n >> k; n   for (int i = 1; i <= k; i++) cin >> a[i]; n   sort(a + 1, a + 1 + k, cmp); n   cout << f(1, n) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = (int)(2e9); n const long long INF = (long long)(5e18); n const int N = 305; n set<pair<int, int> > st; n int n, m; n int bl[N * N], a[N][N], vis[N][N]; n int ans[N][N]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) scanf( %d , &a[i][j]); n   for (int i = 1; i <= n; i++) { n     int mx = 0; n     for (int j = 1; j <= m; j++) mx = max(mx, a[i][j]); n     st.insert(make_pair(mx, 1)); n     bl[mx] = 1; n   } n   for (int j = 1; j <= m; j++) { n     int mx = 0; n     for (int i = 1; i <= n; i++) mx = max(mx, a[i][j]); n     st.insert(make_pair(mx, -1)); n     bl[mx] = 1; n   } n   int hang = 1, lie = 1, now = 1; n   while (!st.empty()) { n     pair<int, int> u = *st.begin(); n     st.erase(u); n     pair<int, int> tmp = *st.begin(); n     if (tmp.first == u.first) { n       st.erase(tmp); n       u.second = 2; n     } n     vector<int> v; n     ans[hang][lie] = u.first; n     if (u.second == 1) { n       v.clear(); n       for (int i = lie + 1; i <= m; i++) { n         while (bl[now]) now++; n         v.push_back(now); n         now++; n       } n       for (int i = m, j = 0; i >= lie + 1; i--) { n         ans[hang][i] = v[j]; n         j++; n       } n       hang++; n     } else if (u.second == -1) { n       v.clear(); n       for (int i = hang + 1; i <= n; i++) { n         while (bl[now]) now++; n         v.push_back(now); n         now++; n       } n       for (int i = n, j = 0; i >= hang + 1; i--) { n         ans[i][lie] = v[j]; n         j++; n       } n       lie++; n     } else { n       v.clear(); n       for (int i = lie + 1; i <= m; i++) { n         while (bl[now]) now++; n         v.push_back(now); n         now++; n       } n       for (int i = m, j = 0; i >= lie + 1; i--) { n         ans[hang][i] = v[j]; n         j++; n       } n       v.clear(); n       for (int i = hang + 1; i <= n; i++) { n         while (bl[now]) now++; n         v.push_back(now); n         now++; n       } n       for (int i = n, j = 0; i >= hang + 1; i--) { n         ans[i][lie] = v[j]; n         j++; n       } n       lie++; n       hang++; n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) printf( %d  , ans[i][j]); n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n string s; n int f[2222][2222][2]; n const int mod = 1000000007; n int sum[2222][2222]; n inline void add(int i, int j, int k, int val) { n   f[i][j][k] += val; n   if (f[i][j][k] >= mod) f[i][j][k] -= mod; n } n int solve() { n   int ans = 0; n   for (int pos = 0; pos < n; ++pos) { n     int ls = s[pos] -  a ; n     int mr = 25 - ls; n     for (int cnt = 0; cnt <= k; ++cnt) { n       add(pos, cnt, 0, ((long long)ls * (sum[cnt][pos - 1])) % mod); n       if (!cnt) { n         add(pos, cnt, 0, ls); n       } n       for (int prev = pos - 1; prev >= 0; --prev) { n         int to_add = (pos - prev) * (n - pos); n         if (to_add > cnt) break; n         add(pos, cnt, 1, ((long long)mr * f[prev][cnt - to_add][0]) % mod); n         add(pos, cnt, 1, ((long long)mr * f[prev][cnt - to_add][1]) % mod); n       } n       int to_add = (pos + 1) * (n - pos); n       if (to_add == cnt) { n         add(pos, cnt, 1, mr); n       } n       sum[cnt][pos] += f[pos][cnt][0]; n       if (sum[cnt][pos] >= mod) sum[cnt][pos] -= mod; n       sum[cnt][pos] += f[pos][cnt][1]; n       if (sum[cnt][pos] >= mod) sum[cnt][pos] -= mod; n       sum[cnt][pos] += sum[cnt][pos - 1]; n       if (sum[cnt][pos] >= mod) sum[cnt][pos] -= mod; n     } n     ans += f[pos][k][0]; n     if (ans >= mod) ans -= mod; n     ans += f[pos][k][1]; n     if (ans >= mod) ans -= mod; n   } n   if (k == 0) { n     ++ans; n     if (ans == mod) ans = 0; n   } n   return ans; n } n int main() { n   cin >> n >> k; n   cin >> s; n   cout << solve() << endl; n } n


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


#include <bits/stdc++.h> n using namespace std; n char s[111111]; n int m, l, r, n, pos[111111], nxt[111111][28], pre[111111][28], pr[111111][28], n     nx[111111][28], cur[28], a[111111]; n int prefix[111111]; n int solve(int l, int r); n int getsuf(int pos, int k) { n   if (~nx[pos][k]) return nx[pos][k]; n   return nx[pos][k] = solve(pos, nxt[pos][k] - 1); n } n int getpre(int pos, int k) { n   if (~pr[pos][k]) return pr[pos][k]; n   return pr[pos][k] = solve(pre[pos][k] + 1, pos); n } n int solve(int l, int r) { n   if (l > r) return 0; n   if (l == r) return 1; n   bool used[55]; n   memset(used, 0, sizeof(used)); n   int ans, res; n   for (int i = 0; i < 26; i++) { n     int s = nxt[l][i], e = pre[r][i]; n     if (s > e || !s || !e) continue; n     ans = prefix[e] ^ prefix[pre[s][i]]; n     if (s != l) ans ^= getsuf(l, i); n     if (e != r) ans ^= getpre(r, i); n     used[ans] = 1; n   } n   res = 0; n   while (used[res]) res++; n   return res; n } n int main() { n   scanf( %s , s); n   n = strlen(s); n   for (int i = 1; i <= n; i++) { n     a[i] = s[i - 1] -  a ; n   } n   memset(nx, -1, sizeof(nx)); n   memset(pr, -1, sizeof(pr)); n   for (int i = n; i >= 1; i--) { n     cur[a[i]] = i; n     for (int j = 0; j < 26; j++) nxt[i][j] = cur[j]; n   } n   memset(cur, 0, sizeof(cur)); n   for (int i = 1; i <= n; i++) { n     cur[a[i]] = i; n     for (int j = 0; j < 26; j++) pre[i][j] = cur[j]; n   } n   for (int i = 1; i <= n; i++) { n     if (!pre[i][a[i]]) continue; n     prefix[i] = prefix[pre[i - 1][a[i]]] ^ solve(pre[i - 1][a[i]] + 1, i - 1); n   } n   scanf( %d , &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &l, &r); n     if (!solve(l, r)) n       printf( Bob n ); n     else n       printf( Alice n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000001; n const double EPS = 10e-9; n const int SIZE = 20; n char number[SIZE]; n vector<int> dig[SIZE]; n inline void mySwap(int i, int j) { n   char tmp = number[j]; n   for (typeof(j) k = (j); k >= (i + 1); --k) number[k] = number[k - 1]; n   number[i] = tmp; n } n void fun(int n, int k) { n   int bg = 0; n   for (; bg < n; ++bg) { n     for (typeof(9) i = (9); i >= (0); --i) { n       if (i <= number[bg] -  0 ) break; n       for (typeof(bg + 1) j = (bg + 1); j < (n); ++j) { n         if (number[j] -  0  == i) { n           if (k >= j - bg) { n             k -= j - bg; n             mySwap(bg, j); n             ++bg; n             if (!k) return; n           } n         } n       } n     } n   } n } n void rob() { n   int k; n   scanf( %s %d , number, &k); n   int n = strlen(number); n   fun(n, k); n   printf( %s n , number); n } n int main() { n   int test = 1; n   while (test--) rob(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e3 + 5; n int arr[maxn]; n map<int, int> mp; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   vector<int> v; n   for (int i = n - 1; i >= 0; i--) { n     if (mp[arr[i]] == 0) { n       mp[arr[i]] = 1; n       v.push_back(arr[i]); n     } n   } n   reverse(v.begin(), v.end()); n   cout << v.size() << endl; n   for (int i = 0; i < v.size(); i++) cout << v[i] <<    ; n } n


#include <bits/stdc++.h> n int pow(int a) { n   int o = 1; n   for (int i = 0; i < a; i++) { n     o = (o * 2) % 998244353; n   } n   return o; n } n int main(void) { n   int n, m; n   scanf( %d%d , &n, &m); n   printf( %d , pow(n + m)); n } n


#include <bits/stdc++.h> n const int MAX_N = 1e5 + 5; n int n, m; n char str[MAX_N], nxt[256]; n bool chk[256], vis[256], not_root[256], flag; n std::vector<char> ans; n void dfs(char c) { n   if (!vis[c]) { n     vis[c] = true; n     ans.push_back(c); n     if (nxt[c]) dfs(nxt[c]); n   } else n     flag = true; n } n int main() { n   scanf( %d , &n); n   while (n--) { n     scanf(  %s , str); n     m = strlen(str); n     for (int i = 0; i < m; ++i) chk[str[i]] = true; n     for (int i = 0; i + 1 < m; ++i) { n       if (nxt[str[i]] == 0) n         nxt[str[i]] = str[i + 1]; n       else if (nxt[str[i]] != str[i + 1]) { n         printf( NO n ); n         return 0; n       } n       not_root[str[i + 1]] = true; n     } n   } n   for (char i =  a ; i <=  z ; ++i) n     if (!not_root[i] && chk[i]) dfs(i); n   for (char i =  a ; i <=  z ; ++i) flag |= chk[i] != vis[i]; n   if (flag) { n     printf( NO n ); n     return 0; n   } n   for (char c : ans) printf( %c , c); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e5 + 10; n int n; n vector<int> vec[MAXN]; n map<int, pair<int, int> > mp; n int main() { n   cin >> n; n   cin.tie(0); n   for (int i = 0; i < n; i++) { n     int t; n     cin >> t; n     vec[i].resize(t); n     int sm = 0; n     for (int j = 0; j < t; j++) cin >> vec[i][j], sm += vec[i][j]; n     for (int j = 0; j < t; j++) n       if (mp.count(sm - vec[i][j])) { n         int z = sm - vec[i][j]; n         if (mp[z].first == i) continue; n         cout <<  YES n ; n         cout << mp[z].first + 1 <<     << mp[z].second + 1 <<   n ; n         cout << i + 1 <<     << j + 1 <<   n ; n         return 0; n       } else n         mp[sm - vec[i][j]] = make_pair(i, j); n   } n   cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cout << name <<   :   << arg1 <<   n ; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cout.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n const long long int N = 100005; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t; n   cin >> t; n   while (t--) { n     long long int i, j, k, n, m, ans = 0, cnt = 0, sum = 0; n     cin >> m >> n; n     long long int a[n]; n     map<long long int, long long int> mp; n     for (i = 0; i < n; i++) { n       cin >> a[i]; n       mp[a[i]]++; n       sum += a[i]; n     } n     if (sum < m) { n       cout << -1 <<   n ; n     } else { n       set<long long int> St; n       for (i = 0; i < 62; i++) { n         if (m >> i & 1ll) { n           St.insert(1ll << i); n         } n       } n       cnt = 1; n       sum = 0; n       for (auto x : St) { n         while (cnt <= x) { n           sum += mp[cnt] * cnt; n           cnt *= 2; n         } n         if (sum >= x) n           sum -= x; n         else { n           long long int nxt = cnt; n           while (mp[nxt] == 0) { n             nxt *= 2; n           } n           mp[nxt]--; n           ans += log2(nxt / x); n           while (nxt > x) { n             nxt /= 2; n             mp[nxt]++; n           } n         } n       } n       cout << ans <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char* argv[]) { n   char dt[1002]; n   memset(dt, 0, sizeof(dt)); n   char gr[1002]; n   memset(gr, 0, sizeof(gr)); n   cin >> dt >> gr; n   int n = strlen(dt); n   int m = strlen(gr); n   map<char, int> dtc; n   map<char, int> gtc; n   for (int i = 0; i < n; ++i) { n     dtc[dt[i]]++; n   } n   for (int i = 0; i < m; ++i) { n     gtc[gr[i]]++; n   } n   int res = 0; n   map<char, int>::iterator it; n   for (it = gtc.begin(); it != gtc.end(); ++it) { n     char val = it->first; n     if (dtc[val] == 0) { n       cout << -1; n       return 0; n     } n     if (dtc[val] >= gtc[val]) n       res += gtc[val]; n     else n       res += dtc[val]; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n  n #define ALL(x) x.begin(), x.end() n #define pb push_back n #define db1(x) cout << #x <<   =   << x << endl n #define db2(x, y) cout << #x <<   =   << x <<  ,   << #y <<   =   << y << endl n using namespace std; n void localFileInput() { n #ifndef ONLINE_JUDGE n     freopen( input.txt ,  r , stdin); n #endif n } n typedef long long ll; n typedef pair<int, int> pii; n const ll mod = 1e9+7; n const int N = 1e5+10; n int n, m, a[N]; n struct Node n { n     int r; n     double p; n     void input() { n         scanf( %d%lf , &r, &p); n     } n }b[N]; n int r[N]; n bool cmp(Node a , Node b) { n     return a.r < b.r; n } n void sol() { n     scanf( %d%d , &n, &m); n     for (int i = 1; i <= n; ++ i) scanf( %d , &a[i]); n     for (int i = 1; i <= m; ++ i) b[i].input(); n     sort(b + 1, b + 1 + m, cmp); n     for (int i = 1; i <= m; ++ i) r[i] = b[i].r; n     int id = 0; n     for (int i = n; i >= 1; -- i) if(a[i] != i) { n         id = i; n         break; n     } n     if(id == 0) { n         puts( 1.000000 ); n         return ; n     } n     int st = lower_bound(r+1, r+1+m, id) - r; n  n     double ans = 1.0; n     for (int i = st; i <= m; ++ i)  n         ans = ans * (1 - b[i].p); n     printf( %.6f n , 1-ans); n } n int main() { n     localFileInput(); n     int _; scanf( %d , &_); while(_ --) sol(); n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   long int n, a, b, i = 0, cnt = 0, y; n   cin >> n >> a >> b; n   while (n >= 0) { n     if (i == 0) { n       n = n; n     } else { n       n = n - a; n     } n     if (n % b == 0 && n >= 0) { n       y = n / b; n       cnt++; n       break; n     } n     i++; n   } n   if (cnt > 0) { n     cout <<  YES  << endl; n     cout << i <<     << y; n   } else if (cnt == 0) { n     cout <<  NO ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 1e5 + 7; n long long n, q; n long long a[maxn]; n long long m; n map<long long, long long> ma; n long long Mod(long long x, long long mod) { n   if (x < mod) return x; n   return x % mod + mod; n } n long long fp(long long a, long long b, long long mod) { n   long long tmp = 1; n   while (b) { n     if (b & 1) tmp = Mod(tmp * a, mod); n     b >>= 1, a = Mod(a * a, mod); n   } n   return tmp; n } n long long phi(long long k) { n   long long s = k, kk = k; n   if (ma.count(k)) return ma[k]; n   for (long long i = 2; i * i <= k; i++) { n     if (k % i == 0) s = s / i * (i - 1); n     while (k % i == 0) k /= i; n   } n   if (k > 1) s = s / k * (k - 1); n   return ma[kk] = s; n } n long long solve(long long l, long long r, long long mod) { n   if (l == r || mod == 1) return Mod(a[l], mod); n   return fp(a[l], solve(l + 1, r, phi(mod)), mod); n } n int main() { n   cin >> n >> m; n   for (long long i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n   } n   scanf( %d , &q); n   while (q--) { n     long long l, r; n     scanf( %lld%lld , &l, &r); n     printf( %lld n , solve(l, r, m) % m); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10, N = 1e3 + 10; n int tp[maxn], x[maxn], y[maxn]; n vector<int> edge; n char ch[2]; n int rt[N], V, E, i, k; n int root(int u) { n   if (rt[u] < 0) n     return u; n   else n     return rt[u] = root(rt[u]); n } n void init(int _V) { n   for (i = 1; i <= _V; i++) rt[i] = -1; n } n void merge(int u, int v) { n   u = root(u); n   v = root(v); n   rt[v] = u; n } n void Read() { n   scanf( %d%d , &V, &E); n   for (i = 1; i <= E; i++) { n     scanf( %d%d%s , x + i, y + i, ch); n     tp[i] = (ch[0] ==  S ) ? 1 : 0; n   } n } n void runs() { n   if (V % 2 != 1) puts( -1 ), exit(0); n   k = V >> 1; n   init(V); n   for (i = 1; i <= E; i++) n     if (!tp[i] and root(x[i]) != root(y[i])) { n       merge(x[i], y[i]); n     } n   for (i = 1; i <= E; i++) n     if (tp[i] and root(x[i]) != root(y[i])) { n       edge.push_back(i); n       merge(x[i], y[i]); n     } n   if (((int)((edge).size())) > k) puts( -1 ), exit(0); n   init(V); n   for (i = 0; i < ((int)((edge).size())); i++) merge(x[edge[i]], y[edge[i]]); n   for (i = 1; i <= E; i++) n     if (((int)((edge).size())) < k and tp[i] and root(x[i]) != root(y[i])) { n       edge.push_back(i); n       merge(x[i], y[i]); n     } n   if (((int)((edge).size())) < k) puts( -1 ), exit(0); n   for (i = 1; i <= E; i++) n     if (!tp[i] and root(x[i]) != root(y[i])) { n       edge.push_back(i); n       merge(x[i], y[i]); n     } n   if (((int)((edge).size())) < 2 * k) puts( -1 ), exit(0); n   printf( %d n , V - 1); n   for (i = 0; i < ((int)((edge).size())); i++) printf( %d  , edge[i]); n   puts(  ); n } n int main() { n   Read(); n   runs(); n } n


#include <bits/stdc++.h> n using namespace std; n int n, c; n int a[1 << 20], b[1 << 20]; n char tp[1 << 20]; n int hs[128][128]; n int ne[128][128]; n int li[128]; n bool getDP(int l, int r) { n   if (hs[l][r] != -1) return hs[l][r]; n   if (l >= r) { n     return (hs[l][r] = 1); n   } n   hs[l][r] = 0; n   for (int k = l; k <= r; ++k) { n     bool ok = true; n     for (int i = 0; i < c; ++i) { n       if (a[i] == l) { n         if (tp[i] ==  L  && b[i] > k) ok = false; n         if (tp[i] ==  R  && b[i] <= k) ok = false; n       } n       if (a[i] > l && a[i] <= k) { n         if (b[i] > k) ok = false; n       } n     } n     if (ok) { n       if (getDP(l + 1, k) && getDP(k + 1, r)) { n         ne[l][r] = k; n         hs[l][r] = 1; n         break; n       } n     } n   } n   return hs[l][r]; n } n int le[1 << 20], ri[1 << 20]; n void cli(int l, int r) { n   if (l >= r) return; n   li[l] = ne[l][r]; n   if (li[l] > l) { n     le[l] = l + 1; n     cli(l + 1, li[l]); n   } n   if (li[l] < r) { n     ri[l] = li[l] + 1; n     cli(li[l] + 1, r); n   } n } n vector<int> sl; n void sol(int v) { n   if (le[v] != -1) sol(le[v]); n   sl.push_back(v); n   if (ri[v] != -1) sol(ri[v]); n } n int main() { n   scanf( %d %d , &n, &c); n   char buf[32]; n   for (int i = 0; i < c; i++) { n     scanf( %d %d %s , &a[i], &b[i], buf); n     tp[i] = buf[0]; n     if (a[i] >= b[i]) { n       printf( IMPOSSIBLE n ); n       return 0; n     } n   } n   memset(hs, -1, sizeof(hs)); n   bool ok = getDP(1, n); n   if (!ok) { n     printf( IMPOSSIBLE n ); n     return 0; n   } n   memset(li, -1, sizeof(li)); n   memset(le, -1, sizeof(le)); n   memset(ri, -1, sizeof(ri)); n   cli(1, n); n   sol(1); n   for (int i = 0; i + 1 < (int)sl.size(); i++) { n     printf( %d  , sl[i]); n   } n   printf( %d n , sl.back()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class PriorityQueue { n  private: n   vector<pair<int, int> > d1, d2; n   int l1 = 0, l2 = 0, r1 = 0, r2 = 0; n   inline bool takeFirst() { return l1 != r1 && (l2 == r2 || d1[l1] < d2[l2]); } n  n  public: n   inline void prepare() { n     swap(d1, d2); n     swap(l1, l2); n     swap(r1, r2); n   } n   inline void push(const pair<int, int> &p) { n     d1.push_back(p); n     r1++; n   } n   inline void repush(const pair<int, int> &p) { n     if (l1) { n       d1[--l1] = p; n     } else { n       d2[--l2] = p; n     } n   } n   inline bool empty() { return l1 == r1 && l2 == r2; } n   inline pair<int, int> top() { return takeFirst() ? d1[l1] : d2[l2]; } n   inline void pop() { (takeFirst() ? l1 : l2)++; } n }; n static const int heavyLim = 500; n struct FreqAnalyzer { n   int n, m; n   vector<int> items; n   vector<int> freqs; n   vector<int> heavy; n   inline void add(int v, int sign) { n     if (items[v]) { n       freqs[items[v]]--; n     } n     items[v] += sign; n     if (items[v]) { n       freqs[items[v]]++; n     } n   } n   inline int64_t huffmanSum() { n     PriorityQueue pq; n     for (int i = 0; i < min(n + 1, heavyLim); i++) { n       if (freqs[i]) { n         pq.push({i, freqs[i]}); n       } n     } n     vector<pair<int, int> > heavies; n     for (int i : heavy) { n       if (items[i] >= heavyLim) { n         heavies.emplace_back(items[i], 1); n       } n     } n     sort(heavies.begin(), heavies.end()); n     for (auto it : heavies) { n       pq.push(it); n     } n     pq.prepare(); n     int64_t ans = 0; n     while (!pq.empty()) { n       auto it = pq.top(); n       pq.pop(); n       while (!pq.empty() && pq.top().first == it.first) { n         it.second += pq.top().second; n         pq.pop(); n       } n       if (it.second == 1) { n         if (pq.empty()) { n           break; n         } else { n           auto jt = pq.top(); n           int q = jt.first; n           jt.second--; n           pq.pop(); n           if (jt.second) { n             pq.repush(jt); n           } n           ans += it.first + q; n           pq.push({it.first + q, 1}); n         } n       } else { n         int64_t a = it.first * 2, b = it.second / 2; n         ans += a * b; n         pq.push({a, b}); n         if (it.second & 1) { n           pq.repush({it.first, 1}); n         } n       } n     } n     return ans; n   } n   FreqAnalyzer(int n, int m, const vector<int> &heavy) n       : n(n), m(m), items(m), freqs(n + 1), heavy(heavy) {} n }; n const int moBlock = 300; n struct Query { n   int l, r, idx; n   inline pair<int, int> toPair() const { n     auto res = make_pair(l / moBlock, r); n     if (res.first & 1) { n       res.second *= -1; n     } n     return res; n   } n }; n inline bool operator<(const Query &a, const Query &b) { n   return a.toPair() < b.toPair(); n } n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   vector<int> a(n); n   int m = 0; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     m = max(m, a[i]); n     a[i]--; n   } n   vector<int> knt(m); n   vector<int> heavy; n   for (int i = 0; i < n; i++) { n     knt[a[i]]++; n   } n   for (int i = 0; i < m; i++) { n     if (knt[i] >= heavyLim) { n       heavy.push_back(i); n     } n   } n   int q; n   cin >> q; n   vector<Query> qry(q); n   for (int i = 0; i < q; i++) { n     cin >> qry[i].l >> qry[i].r; n     qry[i].l--; n     qry[i].r--; n     qry[i].idx = i; n   } n   sort(qry.begin(), qry.end()); n   FreqAnalyzer fa(n, m, heavy); n   int l = 0, r = -1; n   vector<int64_t> ans(q); n   for (Query q : qry) { n     while (l > q.l) { n       fa.add(a[--l], +1); n     } n     while (r < q.r) { n       fa.add(a[++r], +1); n     } n     while (l < q.l) { n       fa.add(a[l++], -1); n     } n     while (r > q.r) { n       fa.add(a[r--], -1); n     } n     ans[q.idx] = fa.huffmanSum(); n   } n   for (int i = 0; i < q; i++) { n     cout << ans[i] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void debug(int* a, int n, bool flag = false) { n   if (!flag) printf( array  ); n   for (int i = 1; i <= n; i++) printf( %d  , a[i]); n   printf(  n ); n } n void debug(vector<int>& E, bool flag = false) { n   if (!flag) printf( vector  ); n   for (auto it : E) printf( %d  , it); n   printf(  n ); n } n const int maxn = 2e5 + 5; n int a[maxn]; n set<pair<int, int> > s; n int n, m; n map<int, bool> Map; n vector<int> res; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     Map[a[i]] = true; n     s.insert(make_pair(i, -1)); n     s.insert(make_pair(i, 1)); n   } n   sort(a + 1, a + 1 + n); n   long long ans = 0; n   long long k = 1; n   while (m) { n     auto it = s.begin(); n     while (it != s.end()) { n       long long val = a[it->first] + k * it->second; n       if (!Map.count(val)) { n         res.push_back(val); n         ans += k; n         it++; n         m--; n         Map[val] = true; n         if (!m) break; n       } else n         s.erase(it++); n     } n     k++; n   } n   printf( %I64d n , ans); n   debug(res, true); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct lr { n   long long left, right; n   lr() : left(-1), right(1e7) {} n   lr(long long _l, long long _r) : left(_l), right(_r) {} n }; n int main() { n   long long n; n   scanf( %lld , &n); n   vector<long long> arr(n); n   for (int i = 0; i < n; i++) scanf( %lld , &arr[i]); n   vector<lr> mn(n, lr(0, n - 1)); n   vector<lr> mx(n, lr(0, n - 1)); n   stack<lr> mxs, mns; n   for (int i = 0; i < n; i++) { n     while (!mxs.empty()) { n       if (mxs.top().left >= arr[i]) { n         break; n       } else { n         lr it = mxs.top(); n         mxs.pop(); n         mx[it.right].right = i - 1; n       } n     } n     mxs.push(lr(arr[i], i)); n     while (!mns.empty()) { n       if (mns.top().left <= arr[i]) { n         break; n       } else { n         lr it = mns.top(); n         mns.pop(); n         mn[it.right].right = i - 1; n       } n     } n     mns.push(lr(arr[i], i)); n   } n   while (!mxs.empty()) mxs.pop(); n   while (!mns.empty()) mns.pop(); n   for (int i = n - 1; i >= 0; i--) { n     while (!mxs.empty()) { n       if (mxs.top().left > arr[i]) { n         break; n       } else { n         lr it = mxs.top(); n         mxs.pop(); n         mx[it.right].left = i + 1; n       } n     } n     mxs.push(lr(arr[i], i)); n     while (!mns.empty()) { n       if (mns.top().left < arr[i]) { n         break; n       } else { n         lr it = mns.top(); n         mns.pop(); n         mn[it.right].left = i + 1; n       } n     } n     mns.push(lr(arr[i], i)); n   } n   long long res = 0; n   for (int i = 0; i < n; i++) { n     long long x1 = mx[i].right - i + 1, x2 = mn[i].right - i + 1, n               n1 = i - mx[i].left + 1, n2 = i - mn[i].left + 1; n     long long a = x1 * n1; n     long long b = x2 * n2; n     res += a * arr[i]; n     res -= b * arr[i]; n   } n   cout << res << endl; n   return 0; n } n


n #include <iostream>   n #include <bits/stdc++.h>  n #include <stdio.h> n #include <cstdlib> n #include<algorithm>  n #include<string.h> n #include<map> n #include<math.h> n using namespace std;  n int main() n { n int t; n cin>>t; n while(t--) n { n int n,k; n cin>>n>>k;int count =0; n vector <int> v,a(n);int max=n; n if(k>(n-1)/2) n  {   cout<< -1 <<endl; n     continue; n } n for(int i=1;i<n;i+=2) n { n     if(count==k) n         break; n     a[i]=max; n     max--; n         count++; n  n } n for(int i=0;i<n;i++) n { n     if(a[i]!=0) n         continue; n     a[i]=max; n     max--; n } n for(int i=0;i<n;i++) n { n     cout<<a[i]<<   ; n } n  n cout<<endl; n } n }


#include <bits/stdc++.h> n using namespace std; n const long long maxM = 22, maxN = ((1LL << 20) + 1); n long long M, mod, T, N; n int bc[maxM][maxM]; n int dp[maxN][2][maxM]; n int arr[maxN], b[maxM], p2[maxM]; n struct Matrix { n   long long n, m; n   long long val[maxM][maxM]; n   Matrix() { n     for (int i = 0; i < n; i++) n       for (int j = 0; j < m; j++) val[i][j] = 0; n   } n }; n Matrix operator*(Matrix A, Matrix B) { n   Matrix ret; n   ret.n = 1, ret.m = M; n   for (int k = 0; k <= M; k++) { n     long long vl = 0; n     for (int i = 0; i <= (M - k); i++) { n       long long vl2 = 0; n       for (int j = 0; j <= k; j++) { n         vl2 = (vl2 + n                (((bc[k][j] * A.val[0][i + j]) % mod) * B.val[0][i + k - j])) % n               mod; n       } n       vl = (vl + (bc[M - k][i] * vl2)) % mod; n     } n     vl %= mod; n     if (vl < 0) vl += mod; n     ret.val[0][k] = vl; n   } n   return ret; n } n Matrix Pow(Matrix A, long long b) { n   Matrix Temp; n   Temp.n = 1; n   Temp.m = M; n   for (int i = 0; i <= M; i++) Temp.val[0][i] = (int(i == 0)); n   if (b == 0) return Temp; n   Matrix res = A; n   for (b--; b; b >>= 1, A = A * A) n     if (b & 1) res = res * A; n   return res; n } n int main() { n   scanf( %lld%lld%lld , &M, &T, &mod); n   N = (1LL << M); n   for (int i = 0; i < N; i++) { n     scanf( %lld , &arr[i]); n     arr[i] %= mod; n     dp[i][0][0] = arr[i]; n   } n   bc[0][0] = 1; n   p2[0] = 1; n   for (int i = 1; i <= (M + 1); i++) { n     p2[i] = 2 * p2[i - 1]; n     for (int j = 0; j <= i; j++) { n       if (j == 0 || j == i) n         bc[i][j] = 1; n       else n         bc[i][j] = (bc[i - 1][j - 1] + bc[i - 1][j]); n     } n   } n   for (int i = 0; i <= M; i++) { n     scanf( %lld , &b[i]); n     b[i] %= mod; n   } n   long long J = 1; n   for (int j = 1; j <= M; j++) { n     for (int i = 0; i < N; i++) { n       for (int k = 0; k <= M; k++) { n         if (j == 0 && k == 0) { n           dp[i][J][k] = arr[i]; n         } else if (j == 0) n           dp[i][J][k] = 0; n         else { n           dp[i][J][k] = (dp[i][1 - J][k] + dp[i ^ (p2[j - 1])][1 - J][k - 1]); n           if (dp[i][J][k] >= mod) dp[i][J][k] -= mod; n         } n       } n     } n     J = 1 - J; n   } n   Matrix B; n   B.n = 1; n   B.m = M; n   for (int i = 0; i <= M; i++) B.val[0][i] = b[i]; n   Matrix c = Pow(B, T); n   long long MD = M % 2, ans; n   for (int i = 0; i < N; i++) { n     ans = 0; n     for (int j = 0; j <= M; j++) n       ans = (ans + (c.val[0][j] * dp[i][MD][j])) % mod; n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long double n, l, k, v1, v2; n int main() { n   cout << fixed << setprecision(10); n   cin >> n >> l >> v1 >> v2 >> k; n   if (v2 <= v1) { n     cout << l / (v1 * 1.0) << endl; n     return 0; n   } n   if (n <= k) { n     cout << l / (v2 * 1.0) << endl; n     return 0; n   } n   int T = ceil(n / (k * 1.0)); n   long double K = ((T - 1) * (1 + (v2 - v1) / ((v2 + v1) * 1.0))) * v1; n   K += v2; n   K = l / K; n   long double ans = K * (T + (T - 1) * (v2 - v1) / (v2 + v1)); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int start[200010], finish[200010], cnt, n, m, a[200010], dis[200010]; n long long tree[2][8 * 200010]; n vector<int> adj[200010], nodes[2], starts[2]; n void update(int v, int vl, int vr, int l, int r, int val, int k) { n   int mid = (vl + vr) / 2; n   if (l <= vl && vr <= r) { n     tree[k][v] += val; n     return; n   } n   if (vr < l || r < vl) return; n   update(2 * v, vl, mid, l, r, val, k); n   update(2 * v + 1, mid + 1, vr, l, r, val, k); n } n long long getVal(int v, int vl, int vr, int idx, int k) { n   int mid = (vl + vr) / 2; n   if (vl == vr) return tree[k][v]; n   if (idx <= mid) n     return tree[k][v] + getVal(2 * v, vl, mid, idx, k); n   else n     return tree[k][v] + getVal(2 * v + 1, mid + 1, vr, idx, k); n } n void dfs(int v, int f, int d) { n   cnt++; n   start[v] = cnt; n   nodes[d % 2].push_back(v); n   starts[d % 2].push_back(start[v]); n   dis[v] = d % 2; n   for (__typeof(((int)((adj[v]).size())) - 1) i = (0); n        i <= (((int)((adj[v]).size())) - 1); i++) n     if (adj[v][i] != f) dfs(adj[v][i], v, d + 1); n   finish[v] = cnt; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   for (__typeof(n) i = (1); i <= (n); i++) cin >> a[i]; n   for (__typeof(n - 1) i = (1); i <= (n - 1); i++) { n     int u, v; n     cin >> u >> v; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   dfs(1, -1, 0); n   for (__typeof(n) i = (1); i <= (n); i++) { n     int k = dis[i]; n     int idx = lower_bound((starts[k]).begin(), (starts[k]).end(), start[i]) - n               starts[k].begin(); n     idx++; n     update(1, 1, n, idx, idx, a[i], k); n   } n   starts[0].push_back((1000 * 1000 * 1000)); n   starts[1].push_back((1000 * 1000 * 1000)); n   for (__typeof(m) i = (1); i <= (m); i++) { n     int q, x, v; n     cin >> q; n     if (q == 1) { n       cin >> x >> v; n       int k = dis[x]; n       int idx1 = lower_bound((starts[k]).begin(), (starts[k]).end(), start[x]) - n                  starts[k].begin(); n       int idx2 = n           lower_bound((starts[k]).begin(), (starts[k]).end(), finish[x]) - n           starts[k].begin(); n       int idx3 = lower_bound((starts[1 - k]).begin(), (starts[1 - k]).end(), n                              start[x]) - n                  starts[1 - k].begin(); n       int idx4 = lower_bound((starts[1 - k]).begin(), (starts[1 - k]).end(), n                              finish[x]) - n                  starts[1 - k].begin(); n       if (idx2 >= ((int)((nodes[k]).size())) || n           finish[nodes[k][idx2]] != finish[x]) n         idx2--; n       if (idx4 >= ((int)((nodes[1 - k]).size())) || n           finish[nodes[1 - k][idx4]] != finish[x]) n         idx4--; n       idx1++; n       idx2++; n       idx3++; n       idx4++; n       if (idx1 <= idx2) update(1, 1, n, idx1, idx2, v, k); n       if (idx3 <= idx4) update(1, 1, n, idx3, idx4, -v, 1 - k); n     } else { n       cin >> x; n       int k = dis[x]; n       int idx = lower_bound((starts[k]).begin(), (starts[k]).end(), start[x]) - n                 starts[k].begin(); n       idx++; n       cout << getVal(1, 1, n, idx, k) << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 7; n const long long INF = 1e18 + 7; n int n, a[MAXN], d[MAXN] = {}, k, l = 1, r = 0; n long long f[MAXN], g[MAXN], ans = 0; n void add(long long &a, const long long &b) { n   a += b; n   if (a > INF) a = INF; n } n long long sum(long long a, long long b) { n   a += b; n   if (a > INF) a = INF; n   return a; n } n void cost(int newl, int newr) { n   while (r < newr) ans += 1LL * (d[a[++r]]++); n   while (r > newr) ans -= 1LL * (--d[a[r--]]); n   while (l > newl) ans += 1LL * (d[a[--l]]++); n   while (l < newl) ans -= 1LL * (--d[a[l++]]); n } n void calc(int l, int r, int mn, int mx) { n   if (l > r) return; n   int mid = (l + r) >> 1, opt = 0; n   for (int i = min(mx, mid - 1); i >= mn; --i) { n     cost(i + 1, mid); n     if (g[i] + ans < f[mid]) f[mid] = g[i] + ans, opt = i; n   } n   calc(l, mid - 1, mn, opt); n   calc(mid + 1, r, opt, mx); n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n >> k; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   g[0] = f[0] = 0; n   for (int i = 1; i <= n; ++i) f[i] = g[i] = INF; n   for (int i = 1; i <= k; ++i) { n     calc(1, n, 0, n); n     for (int j = 1; j <= n; ++j) g[j] = f[j], f[j] = INF; n   } n   cout << g[n]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const long long mx = 2e6 + 10; n int posx[] = {1, -1, 0, 0}; n int posy[] = {0, 0, 1, -1}; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t = 1, n, k, m, a, b, c, d, h; n   cin >> t; n   while (t--) { n     cin >> h >> c >> k; n     if (h + c >= 2 * k) { n       cout << 2 << endl; n       continue; n     } n     long long x = (c - k) / (h + c - (2 * k)); n     long long y = x + 1; n     long double xx = ((h * x) + (c * (x - 1))) / ((1.0) * 2 * x - 1); n     long double yy = ((h * y) + (c * (y - 1))) / ((1.0) * 2 * y - 1); n     if (abs(xx - k) <= abs(yy - k)) n       cout << 2 * x - 1 << endl; n     else n       cout << 2 * y - 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n long long dx[8] = {0, 1, 0, -1, 1, 1, -1, -1}; n long long dy[8] = {1, 0, -1, 0, -1, 1, 1, -1}; n using namespace std; n class pa3 { n  public: n   long long x; n   long long y, z; n   pa3(long long x = 0, long long y = 0, long long z = 0) : x(x), y(y), z(z) {} n   bool operator<(const pa3 &p) const { n     if (x != p.x) return x < p.x; n     if (y != p.y) return y < p.y; n     return z < p.z; n   } n   bool operator>(const pa3 &p) const { n     if (x != p.x) return x > p.x; n     if (y != p.y) return y > p.y; n     return z > p.z; n   } n   bool operator==(const pa3 &p) const { n     return x == p.x && y == p.y && z == p.z; n   } n   bool operator!=(const pa3 &p) const { n     return !(x == p.x && y == p.y && z == p.z); n   } n }; n class pa4 { n  public: n   long long x; n   long long y, z, w; n   pa4(long long x = 0, long long y = 0, long long z = 0, long long w = 0) n       : x(x), y(y), z(z), w(w) {} n   bool operator<(const pa4 &p) const { n     if (x != p.x) return x < p.x; n     if (y != p.y) return y < p.y; n     if (z != p.z) return z < p.z; n     return w < p.w; n   } n   bool operator>(const pa4 &p) const { n     if (x != p.x) return x > p.x; n     if (y != p.y) return y > p.y; n     if (z != p.z) return z > p.z; n     return w > p.w; n   } n   bool operator==(const pa4 &p) const { n     return x == p.x && y == p.y && z == p.z && w == p.w; n   } n }; n class pa2 { n  public: n   long long x, y; n   pa2(long long x = 0, long long y = 0) : x(x), y(y) {} n   pa2 operator+(pa2 p) { return pa2(x + p.x, y + p.y); } n   pa2 operator-(pa2 p) { return pa2(x - p.x, y - p.y); } n   bool operator<(const pa2 &p) const { return y != p.y ? y < p.y : x < p.x; } n   bool operator>(const pa2 &p) const { return x != p.x ? x < p.x : y < p.y; } n   bool operator==(const pa2 &p) const { n     return abs(x - p.x) == 0 && abs(y - p.y) == 0; n   } n   bool operator!=(const pa2 &p) const { n     return !(abs(x - p.x) == 0 && abs(y - p.y) == 0); n   } n }; n string itos(long long i) { n   ostringstream s; n   s << i; n   return s.str(); n } n long long gcd(long long v, long long b) { n   if (v > b) return gcd(b, v); n   if (v == b) return b; n   if (b % v == 0) return v; n   return gcd(v, b % v); n } n long long mod; n long long extgcd(long long a, long long b, long long &x, long long &y) { n   if (b == 0) { n     x = 1; n     y = 0; n     return a; n   } n   long long d = extgcd(b, a % b, y, x); n   y -= a / b * x; n   return d; n } n pair<long long, long long> operator+(const pair<long long, long long> &l, n                                      const pair<long long, long long> &r) { n   return {l.first + r.first, l.second + r.second}; n } n pair<long long, long long> operator-(const pair<long long, long long> &l, n                                      const pair<long long, long long> &r) { n   return {l.first - r.first, l.second - r.second}; n } n long long pr[1001010]; n long long inv[1001010]; n long long beki(long long wa, long long rr, long long warukazu) { n   if (rr == 0) return 1 % warukazu; n   if (rr == 1) return wa % warukazu; n   wa %= warukazu; n   if (rr % 2 == 1) n     return ((long long)beki(wa, rr - 1, warukazu) * (long long)wa) % warukazu; n   long long zx = beki(wa, rr / 2, warukazu); n   return (zx * zx) % warukazu; n } n long long comb(long long nn, long long rr) { n   if (rr < 0 || rr > nn || nn < 0) return 0; n   long long r = pr[nn] * inv[rr]; n   r %= mod; n   r *= inv[nn - rr]; n   r %= mod; n   return r; n } n void gya(long long ert) { n   pr[0] = 1; n   for (long long i = 1; i <= ert; i++) { n     pr[i] = (pr[i - 1] * i) % mod; n   } n   inv[ert] = beki(pr[ert], mod - 2, mod); n   for (long long i = ert - 1; i >= 0; i--) { n     inv[i] = inv[i + 1] * (i + 1) % mod; n   } n } n struct edge { n   long long to, cap, num, moto; n }; n vector<edge> ZANYO[100020]; n long long label[100020]; n queue<pair<long long, long long> > qu_bfs; n long long dokomade[100020]; n void add_edge(long long s_point, long long t_point, long long capa) { n   ZANYO[s_point].push_back( n       (edge){t_point, capa, (long long)ZANYO[t_point].size(), 0}); n   ZANYO[t_point].push_back( n       (edge){s_point, 0, (long long)ZANYO[s_point].size() - 1, 1}); n } n void bfs_dinic(long long s_point, long long N) { n   for (long long i = 0; i < N; i++) label[i] = -1; n   while (qu_bfs.size()) qu_bfs.pop(); n   qu_bfs.push(make_pair(s_point, 0)); n   while (qu_bfs.size()) { n     pair<long long, long long> z = qu_bfs.front(); n     qu_bfs.pop(); n     if (label[z.first] >= 0) continue; n     label[z.first] = z.second; n     for (auto &v : ZANYO[z.first]) { n       if (v.cap == 0) continue; n       if (v.cap < 0) { n         cout <<  error  << endl; n         exit(0); n       } n       if (label[v.to] >= 0) continue; n       qu_bfs.push(make_pair(v.to, z.second + 1)); n     } n   } n   return; n } n long long dfs_dinic(long long s_point, long long t_point, long long ima_flow) { n   if (s_point == t_point) return ima_flow; n   while (1) { n     if (dokomade[s_point] >= ZANYO[s_point].size()) break; n     long long edanum = dokomade[s_point]; n     if (ZANYO[s_point][edanum].cap < 0) { n       cout <<  minus  << endl; n       exit(0); n     } n     if (ZANYO[s_point][edanum].cap <= 0 || n         label[s_point] + 1 != label[ZANYO[s_point][edanum].to]) { n       dokomade[s_point]++; n       continue; n     } n     long long W = dfs_dinic(ZANYO[s_point][edanum].to, t_point, n                             min(ZANYO[s_point][edanum].cap, ima_flow)); n     if (W > 0) { n       ZANYO[s_point][edanum].cap -= W; n       ZANYO[ZANYO[s_point][edanum].to][ZANYO[s_point][edanum].num].cap += W; n       return W; n     } n     dokomade[s_point]++; n   } n   return 0; n } n long long max_flow_dinic(long long s_point, long long t_point, long long N) { n   long long jougen = 1000000007 * 1000000000ll; n   long long ans_flow = 0; n   while (1) { n     bfs_dinic(s_point, N); n     if (label[t_point] == -1) break; n     for (long long i = 0; i < N; i++) dokomade[i] = 0; n     while (1) { n       long long ima_flow = dfs_dinic(s_point, t_point, jougen + 1); n       if (ima_flow == 0) break; n       ans_flow += ima_flow; n     } n   } n   return ans_flow; n } n signed main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   vector<pa4> ve; n   long long n, q; n   cin >> n >> q; n   set<long long> sex, sey; n   sex.insert(0); n   sex.insert(n); n   sey.insert(0); n   sey.insert(n); n   for (long long i = 0; i < q; i++) { n     long long x1, y1, x2, y2; n     cin >> x1 >> y1 >> x2 >> y2; n     x1--, y1--; n     sex.insert(x1); n     sex.insert(x2); n     sey.insert(y1); n     sey.insert(y2); n     ve.push_back((pa4){x1, x2, y1, y2}); n   } n   vector<long long> vx, vy; n   for (auto it = sex.begin(); it != sex.end(); it++) vx.push_back(*it); n   for (auto it = sey.begin(); it != sey.end(); it++) vy.push_back(*it); n   long long a = vx.size() - 1; n   long long b = vy.size() - 1; n   set<pair<long long, long long> > se; n   for (long long i = 0; i < a; i++) n     for (long long j = 0; j < b; j++) { n       long long sx = vx[i], sy = vx[i + 1]; n       long long tx = vy[j], ty = vy[j + 1]; n       bool bo = 0; n       for (auto v : ve) { n         if (sx >= v.x && v.y >= sy && tx >= v.z && v.w >= ty) bo = 1; n       } n       if (bo) se.insert(make_pair(i, j)); n     } n   long long A[200], B[200]; n   for (long long i = 0; i < a; i++) { n     long long sx = vx[i], sy = vx[i + 1]; n     add_edge(0, 1 + i, sy - sx); n     A[i] = sy - sx; n   } n   for (long long j = 0; j < b; j++) { n     long long tx = vy[j], ty = vy[j + 1]; n     add_edge(a + 1 + j, a + b + 1, ty - tx); n     B[j] = ty - tx; n   } n   for (auto it = se.begin(); it != se.end(); it++) { n     pair<long long, long long> z = *it; n     add_edge(1 + z.first, a + 1 + z.second, min(A[z.first], B[z.second])); n   } n   cout << max_flow_dinic(0, a + b + 1, a + b + 5) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename _T> n inline void read(_T &f) { n   f = 0; n   _T fu = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) fu = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     f = (f << 3) + (f << 1) + (c & 15); n     c = getchar(); n   } n   f *= fu; n } n template <typename T> n void print(T x) { n   if (x < 0) putchar( - ), x = -x; n   if (x < 10) n     putchar(x + 48); n   else n     print(x / 10), putchar(x % 10 + 48); n } n template <typename T> n void print(T x, char t) { n   print(x); n   putchar(t); n } n const int N = 1005; n int a[N][N], ans[N][N], ans2[N][N], b[N]; n int n, m; n int main() { n   read(n); n   read(m); n   for (register int i = 1; i <= n; i++) { n     for (register int j = 1; j <= m; j++) { n       read(a[i][j]); n     } n   } n   for (register int i = 1; i <= n; i++) { n     for (register int j = 1; j <= m; j++) b[j] = a[i][j]; n     sort(b + 1, b + m + 1); n     int big = unique(b + 1, b + m + 1) - b - 1; n     for (register int j = 1; j <= m; j++) { n       int tmp = lower_bound(b + 1, b + big + 1, a[i][j]) - b; n       ans[i][j] = max(ans[i][j], tmp); n       ans2[i][j] = max(ans2[i][j], big - tmp); n     } n   } n   for (register int j = 1; j <= m; j++) { n     for (register int i = 1; i <= n; i++) b[i] = a[i][j]; n     sort(b + 1, b + n + 1); n     int big = unique(b + 1, b + n + 1) - b - 1; n     for (register int i = 1; i <= n; i++) { n       int tmp = lower_bound(b + 1, b + big + 1, a[i][j]) - b; n       ans[i][j] = max(ans[i][j], tmp); n       ans2[i][j] = max(ans2[i][j], big - tmp); n     } n   } n   for (register int i = 1; i <= n; i++) { n     for (register int j = 1; j <= m; j++) { n       print(ans[i][j] + ans2[i][j], j == m ?   n  :    ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void optimise() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n } n long long int mod = 1e9 + 7; n void normalize(long long int &a) { a = (a + mod) % mod; } n long long int modmul(long long int a, long long int b) { n   a = a % mod; n   b = b % mod; n   normalize(a); n   normalize(b); n   return (a * b) % mod; n } n long long int modadd(long long int a, long long int b) { n   a = a % mod; n   b = b % mod; n   normalize(a); n   normalize(b); n   return (a + b) % mod; n } n long long int modsub(long long int a, long long int b) { n   a = a % mod; n   b = b % mod; n   normalize(a); n   normalize(b); n   return (a - b + mod) % mod; n } n long long int me(long long int x, long long int n) { n   x %= mod; n   if (n == 0) return 1; n   long long int u = me(x, n / 2) % mod; n   u = (u * u) % mod; n   if (n % 2) u = (u * x) % mod; n   return u; n } n long long int me1(long long int x, long long int n) { n   if (n == 0) return 1; n   long long int u = me1(x, n / 2); n   u = u * u; n   if (n % 2) u = u * x; n   return u; n } n inline long long int modInv(long long int a) { return me(a, mod - 2); } n inline long long int modDiv(long long int a, long long int b) { n   return modmul(a, modInv(b)); n } n float power(float x, long long int y) { n   float temp; n   if (y == 0) return 1; n   temp = power(x, y / 2); n   if (y % 2 == 0) n     return temp * temp; n   else { n     if (y > 0) n       return x * temp * temp; n     else n       return (temp * temp) / x; n   } n } n long long int __gcd(long long int a, long long int b) { n   if (b == 0) n     return a; n   else n     return __gcd(b, a % b); n } n pair<long long int, long long int> operator+( n     pair<long long int, long long int> a, n     pair<long long int, long long int> b) { n   return {a.first + b.first, a.second + b.second}; n } n pair<long long int, long long int> operator-( n     pair<long long int, long long int> a, n     pair<long long int, long long int> b) { n   return {a.first - b.first, a.second - b.second}; n } n pair<long long int, long long int> operator*( n     pair<long long int, long long int> a, n     pair<long long int, long long int> b) { n   return {a.first * b.first, a.second * b.second}; n } n template <typename T> n ostream &operator<<(ostream &os, const set<T> &v) { n   for (auto it : v) os << it <<    ; n   return os; n } n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   for (long long int i = 0; i < v.size(); ++i) os << v[i] <<    ; n   return os; n } n bool compare(string s1, string s2) { return s1.size() < s2.size(); } n void solve() { n   long long int n, k; n   cin >> n >> k; n   vector<pair<long long int, long long int> > v; n   if (n > k * (k - 1)) { n     cout <<  No ; n     return; n   } n   cout <<  Yes  n        <<   n ; n   for (long long int i = 0; i < k; i++) { n     for (long long int j = i + 1; j < k; j++) { n       n--; n       cout << i + 1 <<     << j + 1 <<   n ; n       if (n == 0) return; n       n--; n       cout << j + 1 <<     << i + 1 <<   n ; n       if (n == 0) return; n     } n   } n } n signed main() { n   optimise(); n   long long int t; n   t = 1; n   cout << fixed << setprecision(5); n   while (t--) { n     solve(); n     cout <<   n ; n   } n   cerr <<   n  n        <<  Time Elasped :   << 1.0 * clock() / CLOCKS_PER_SEC <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   int x, y; n   inline bool operator<(const point &a) const { return x < a.x; } n } arr[100005]; n struct query_tp { n   int col, l, r, delta; n   query_tp() {} n   query_tp(int _col, int _l, int _r, int _delta) { n     col = _col; n     l = _l; n     r = _r; n     delta = _delta; n   } n   inline bool operator<(const query_tp &a) const { return col < a.col; } n }; n long long ans[100005]; n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n; i++) scanf( %d%d , &arr[i].x, &arr[i].y); n   sort(arr, arr + n); n   vector<query_tp> query; n   vector<int> app; n   for (int i = 0; i < n; i++) { n     int l = arr[i].x - k; n     if (app.size()) l = max(l, *app.rbegin()); n     for (int j = l + 1; j <= arr[i].x; j++) app.push_back(j); n     query.push_back(query_tp(arr[i].y - k + 1, arr[i].x - k + 1, arr[i].x, 1)); n     query.push_back(query_tp(arr[i].y + 1, arr[i].x - k + 1, arr[i].x, -1)); n   } n   sort(query.begin(), query.end()); n   int cnt = app.size(), q_cnt = query.size(); n   for (int i = 0; i < q_cnt; i++) { n     query[i].l = lower_bound(app.begin(), app.end(), query[i].l) - app.begin(); n     query[i].r = lower_bound(app.begin(), app.end(), query[i].r) - app.begin(); n   } n   app.clear(); n   vector<int>().swap(app); n   vector<int> lst(cnt, -1e9), num(cnt, 0); n   for (int i = 0; i < q_cnt; i++) { n     for (int j = query[i].l; j <= query[i].r; j++) { n       if (lst[j] != -1e9) ans[num[j]] += query[i].col - lst[j]; n       num[j] += query[i].delta; n       lst[j] = query[i].col; n     } n   } n   for (int i = 1; i <= n; i++) printf( %lld%c , ans[i],    n [i == n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int lim = 200005; n long long int n; n vector<vector<long long int> > ans; n vector<long long int> ed; n void solve(int test) { n   cin >> n; n   for (int(i) = 0; (i) < n; (i)++) { n     long long int v; n     cin >> v; n     vector<long long int>::iterator ind = n         upper_bound((ed).begin(), (ed).end(), -v); n     if (ind == ed.end()) { n       ed.push_back(-v); n       ans.push_back(vector<long long int>(1, v)); n     } else { n       int x = ind - ed.begin(); n       ed[x] = -v; n       ans[x].push_back(v); n     } n   } n   for (int(i) = 0; (i) < int((ans).size()); (i)++) { n     for (int(j) = 0; (j) < int((ans[i]).size()); (j)++) { n       cout << ans[i][j] <<    ; n     } n     cout << endl; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int test = 1; n   for (int(i) = 0; (i) < test; (i)++) { n     solve(i + 1); n   } n   return 0; n } n