#include <bits/stdc++.h> n using namespace std; n constexpr int MAXN = 1 << 30; n struct tournament { n   struct Node { n     Node* parent; n     set<int, greater<int>> blocked; n     Node* left = nullptr; n     Node* right = nullptr; n     Node(Node* parent) : parent(parent) {} n   }* root = new Node(nullptr); n   set<int, greater<int>>::iterator it; n   void update(int x, int start, int end, int lo = 0, int hi = MAXN, n               Node* node = nullptr) { n     if (node == nullptr) node = root; n     if (lo >= end || hi <= start) return; n     if (lo >= start && hi <= end) { n       node->blocked.insert(x); n       return; n     } n     int mid = (lo + hi) / 2; n     if (mid > start) { n       if (node->left == nullptr) node->left = new Node(node); n       update(x, start, end, lo, mid, node->left); n     } n     if (mid < end) { n       if (node->right == nullptr) node->right = new Node(node); n       update(x, start, end, mid, hi, node->right); n     } n   } n   int query(int x, int pos) { n     Node* node = root; n     int lo = 0, hi = MAXN; n     int sol = -1; n     while (node != nullptr) { n       if ((it = node->blocked.lower_bound(x)) != node->blocked.end() && n           *it > sol) n         sol = *it; n       int mid = (lo + hi) / 2; n       if (pos < mid) n         hi = mid, node = node->left; n       else n         lo = mid, node = node->right; n     } n     return sol; n   } n } rows, cols; n set<int> bio; n int unzipA[200100], unzipB[200100]; n pair<int, int> a[200100], b[200100]; n char dirs[200100]; n int main() { n   ios_base::sync_with_stdio(false); n   int n, q, i, j, x; n   char dir; n   cin >> n >> q; n   rows.update(-1, 0, MAXN), cols.update(-1, 0, MAXN); n   for (i = 0; i < q; ++i) { n     cin >> b[i].first >> a[i].first >> dirs[i]; n     --a[i].first, --b[i].first; n     a[i].second = b[i].second = i; n   } n   sort(a, a + q); n   sort(b, b + q); n   new (unzipA - 1) int; n   new (unzipB - 1) int; n   unzipA[-1] = unzipB[-1] = -1; n   for (i = 0; i < q; ++i) { n     unzipA[i] = a[i].first; n     unzipB[i] = b[i].first; n     a[i].first = b[i].first = i; n   } n   sort(a, a + q, [](const pair<int, int>& a, const pair<int, int>& b) { n     return a.second < b.second; n   }); n   sort(b, b + q, [](const pair<int, int>& a, const pair<int, int>& b) { n     return a.second < b.second; n   }); n   for (int k = 0; k < q; ++k) { n     i = a[k].first; n     j = b[k].first; n     dir = dirs[k]; n     if (bio.count(MAXN + i - j)) { n       cout <<  0 n ; n       continue; n     } n     bio.insert(MAXN + i - j); n     switch (dir) { n       case  U : n         x = rows.query(i, j); n         cout << unzipA[i] - unzipA[x] << endl; n         cols.update(j, x, i); n         break; n       case  L : n         x = cols.query(j, i); n         cout << unzipB[j] - unzipB[x] << endl; n         rows.update(i, x + 1, j + 1); n         break; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   int a, b, p, pos; n   int i, cost, j, k, l; n   string s; n   while (t--) { n     cin >> a >> b >> p; n     cost = 0; n     cin >> s; n     pos = s.length() - 2; n     i = s.length() - 2; n     if (s[i] ==  A ) { n       cost += a; n       if (cost > p) { n         cout << s.length() << endl; n         continue; n       } n     } n     if (s[i] ==  B ) { n       cost += b; n       if (cost > p) { n         cout << s.length() << endl; n         continue; n       } n     } n     while (i > 0) { n       if (s[i] == s[i - 1]) { n         i--; n         pos = i; n       } n       if (s[i] != s[i - 1] && i > 0) { n         if (s[i - 1] ==  A ) { n           cost += a; n         } n         if (s[i - 1] ==  B ) { n           cost += b; n         } n         if (cost <= p) { n           pos = i - 1; n           i--; n         } n         if (cost > p) { n           break; n         } n       } n     } n     cout << pos + 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const MOD = 1e9 + 7; n double const PI = acos(-1); n long long const oo = 8e18; n int const N = 3e5 + 100; n int const M = 1e6 + 100; n int n; n pair<int, int> a[N], p1[N], p2[N]; n pair<int, int> intersection(pair<int, int> x, pair<int, int> y) { n   if (x.second < y.first || y.second < x.first) n     return {-1, -1}; n   else n     return {max(x.first, y.first), min(x.second, y.second)}; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) scanf( %d%d , &a[i].first, &a[i].second); n   sort(a, a + n); n   for (int i = 0; i < n; ++i) { n     if (i == 0) n       p1[i] = a[i]; n     else { n       p1[i] = intersection(a[i], p1[i - 1]); n     } n   } n   for (int i = n - 1; i >= 0; --i) { n     if (i == n - 1) n       p2[i] = a[i]; n     else { n       p2[i] = intersection(a[i], p2[i + 1]); n     } n   } n   int mx = 0; n   for (int i = 0; i < n; ++i) { n     pair<int, int> temp; n     if (i == 0) { n       temp = p2[i + 1]; n     } else if (i == n - 1) { n       temp = p1[i - 1]; n     } else { n       temp = intersection(p1[i - 1], p2[i + 1]); n     } n     mx = max(mx, temp.second - temp.first); n   } n   cout << mx << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, x; n   cin >> n >> x; n   int ar[n], i, cnt = 0; n   for (i = 0; i < n; i++) { n     cin >> ar[i]; n     cnt += ar[i]; n   } n   cnt += n - 1; n   if (cnt == x) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a1, a2, s1, s2; n   cin >> a1 >> a2 >> s1 >> s2; n   if (a1 <= a2) n     cout <<  Second ; n   else n     cout <<  First ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5; n bool isprime[32000]; n vector<int> prime; n int a[maxn + 50], num[maxn + 50], factor[maxn + 50], now[maxn + 50]; n int k, n, len = 0; n long long ans = 0; n bool check() { n   for (int i = 1; i <= len; ++i) n     if (now[i] < num[i]) return false; n   return true; n } n void make(int x, int type) { n   for (int i = 1; i <= len; ++i) { n     if (x % factor[i] != 0) continue; n     if (factor[i] == 1) { n       now[i] += type; n       continue; n     } n     while (x % factor[i] == 0) { n       x /= factor[i]; n       now[i] += type; n     } n     if (x == 1) break; n   } n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); n   isprime[1] = 1; n   for (int i = 2; i <= sqrt(k); ++i) n     if (!isprime[i]) { n       prime.push_back(i); n       for (int j = i + i; j <= sqrt(k); j += i) isprime[j] = 1; n     } n   ++len; n   factor[len] = 1; n   num[len] = 1; n   for (int i = 0; i < prime.size(); ++i) { n     if (k % prime[i] != 0) continue; n     factor[++len] = prime[i]; n     while (k % prime[i] == 0) { n       k /= prime[i]; n       ++num[len]; n     } n     if (k == 1) break; n   } n   if (k != 1) factor[++len] = k, num[len] = 1; n   int r = 0; n   for (int l = 1; l <= n; ++l) { n     while (!check() && r < n) { n       ++r; n       make(a[r], 1); n     } n     if (check()) ans += n - r + 1; n     make(a[l], -1); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000500; n const int INF = 1000000000, mod = 1000000007; n const long long LLINF = 1000000000000000000ll; n char s[N]; n int z[N]; n int cnt[N]; n int n; n void calcZ() { n   int l = 0, r = 0; n   for (int i = 1; i < n; ++i) { n     if (i <= r) { n       z[i] = min(z[i - l], r - i + 1); n     } n     while (i + z[i] < n && s[z[i]] == s[i + z[i]]) { n       z[i]++; n     } n     if (i + z[i] >= r) { n       l = i; n       r = i + z[i] - 1; n     } n     cnt[z[i]]++; n   } n } n int main() { n   scanf( %s , &s); n   n = strlen(s); n   calcZ(); n   int cur = 0; n   for (int i = n; i >= 1; --i) { n     cur += cnt[i]; n     if (cur >= 2 && z[n - i] == i) { n       for (int j = 0; j < i; ++j) { n         putchar(s[j]); n       } n       exit(0); n     } n   } n   puts( Just a legend ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool vis[10010][51]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int m, n; n   cin >> m >> n; n   for (int i = 0; i < m; i++) { n     int k; n     cin >> k; n     for (int j = 0; j < k; j++) { n       int temp; n       cin >> temp; n       vis[temp][i] = true; n     } n   } n   for (int i = 0; i < m; i++) { n     for (int j = i + 1; j < m; j++) { n       int flag = 0; n       for (int k = 1; k < n + 1; k++) { n         if (vis[k][i] && vis[k][j]) { n           flag = 1; n           break; n         } n       } n       if (flag == 0) { n         cout <<  impossible  << endl; n         return 0; n       } n     } n   } n   cout <<  possible  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n int dp[4][N]; n string str2[N], str3[N]; n set<string> ans; n int n; n int cal(int len, int idx) { n   if (dp[len][idx] != -1) return dp[len][idx]; n   if (idx == n - 1) return dp[len][idx] = 1; n   ; n   int x = 0; n   if (idx + 2 < n) { n     if (len == 2 && str2[idx] == str2[idx + 2]) { n     } else n       x |= cal(2, idx + 2); n   } n   if (idx + 3 < n) { n     if (len == 3 && str3[idx] == str3[idx + 3]) { n     } else n       x |= cal(3, idx + 3); n   } n   x = x ? 1 : 0; n   return dp[len][idx] = x; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   string s; n   cin >> s; n   n = s.length(); n   for (int i = 0; i < n - 1; ++i) { n     str2[i + 1] = s.substr(i, 2); n     if (i + 2 < n) str3[i + 2] = s.substr(i, 3); n   } n   for (int i = 0; i < 4; ++i) { n     for (int j = 0; j <= n; ++j) dp[i][j] = -1; n   } n   for (int i = 6; i < n; ++i) { n     if (dp[2][i] == -1) cal(2, i); n     if (i > 6 && dp[3][i] == -1) cal(3, i); n   } n   for (int i = 5; i < n; ++i) { n     if (dp[2][i] > 0) ans.insert(str2[i]); n     if (dp[3][i] > 0) ans.insert(str3[i]); n   } n   cout << ans.size() << endl; n   for (auto I : ans) cout << I << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n const long long INF = 4e18 + 42; n long long l[N]; n char type[N]; n void get_stam(long long need, long long &stam, long long &have, long long &ans, n               long long cost); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> l[i]; n   } n   for (int i = 1; i <= n; ++i) { n     cin >> type[i]; n   } n   long long swim_res = 0, walk_res = INF; n   long long stam = 0, s_fly = 0, w_fly = 0, ans = 0; n   for (int i = 1; i <= n; ++i) { n     if (type[i] ==  L ) { n       get_stam(l[i] - stam, stam, s_fly, ans, 1); n       get_stam(l[i] - stam, stam, w_fly, ans, 2); n       get_stam(l[i] - stam, stam, swim_res, ans, 3); n       get_stam(l[i] - stam, stam, walk_res, ans, 5); n       stam -= l[i]; n       ans += l[i]; n     } else if (type[i] ==  W ) { n       long long fly = min(2 * l[i], stam + l[i]), swim = 2 * l[i] - fly; n       stam += (swim - fly) / 2; n       ans += (3 * swim + fly) / 2; n       s_fly += fly; n       swim_res = INF; n     } else { n       get_stam(l[i] - stam, stam, s_fly, ans, 1); n       long long fly = min(2 * l[i], stam + l[i]), walk = 2 * l[i] - fly; n       stam += (walk - fly) / 2; n       ans += (5 * walk + fly) / 2; n       w_fly += fly; n     } n   } n   cout << ans << endl; n } n void get_stam(long long need, long long &stam, long long &have, long long &ans, n               long long cost) { n   if (need <= 0) { n     return; n   } n   long long take = min(have, need); n   ans += cost * take; n   have -= take; n   stam += take; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:102400000,102400000 ) n using namespace std; n int a[1005][1005]; n int main() { n   int N, K, D, i, j; n loop: n   while (~scanf( %d%d%d , &N, &K, &D)) { n     memset(a, 0, sizeof(a)); n     for (j = 0; j < N; ++j) { n       a[D - 1][j] = j; n     } n     for (i = D - 1; i > 0; --i) { n       for (j = 0; j < N; ++j) { n         a[i - 1][j] += a[i][j] / K; n         a[i][j] %= K; n       } n     } n     for (i = 0; i < D; ++i) { n       for (j = 0; j < N; ++j) { n         if (a[i][j] >= K) { n           puts( -1 ); n           goto loop; n         } n       } n     } n     for (i = 0; i < D; ++i) { n       for (j = 0; j < N; ++j) { n         printf( %d%s , a[i][j] + 1, j == N - 1 ?   n  :    ); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void setIO() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n const long double PI = 4 * atan((long double)1); n const int INF = 1e9 + 7; n const long long _INF = 1e18; n int main() { n   setIO(); n   int t; n   cin >> t; n   while (t--) { n     int x, k = 0; n     cin >> x; n     int y = x; n     while (y) { n       y /= 10; n       k++; n     } n     cout << 10 * (x % 10 - 1) + k * (k + 1) / 2 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n using dd = long double; n struct Tree { n   struct Node { n     dd mul; n     dd add; n     dd sum; n     Node() : mul(1), add(0), sum(0) {} n   }; n   int S; n   vector<Node> drz; n   void update(int v) { drz[v].sum = drz[2 * v].sum + drz[2 * v + 1].sum; } n   Tree(const vector<int>& tab) { n     int n = tab.size(); n     S = 1; n     while (S < n) { n       S *= 2; n     } n     drz.resize(2 * S); n     for (int i = 0; i < n; i++) { n       drz[i + S].sum = tab[i]; n     } n     for (int v = S - 1; v; v--) { n       update(v); n     } n   } n   void receive(int v, int len, dd mul, dd add) { n     drz[v].sum = drz[v].sum * mul + add * len; n     drz[v].mul *= mul; n     drz[v].add = drz[v].add * mul + add; n   } n   void push(int v, int len) { n     dd mul = drz[v].mul; n     dd add = drz[v].add; n     receive(2 * v, len / 2, mul, add); n     receive(2 * v + 1, len / 2, mul, add); n     drz[v].mul = 1; n     drz[v].add = 0; n   } n   void mul_add_rec(int v, int vl, int vr, int l, int r, dd mul, dd add) { n     if (vl > r || l > vr) { n       return; n     } n     if (l <= vl && vr <= r) { n       receive(v, vr - vl + 1, mul, add); n       return; n     } n     push(v, vr - vl + 1); n     int vc = (vl + vr) / 2; n     mul_add_rec(2 * v, vl, vc, l, r, mul, add); n     mul_add_rec(2 * v + 1, vc + 1, vr, l, r, mul, add); n     update(v); n   } n   void mul_add(int l, int r, dd mul, dd add) { n     mul_add_rec(1, 0, S - 1, l, r, mul, add); n   } n   dd sum_rec(int v, int vl, int vr, int l, int r) { n     if (vl > r || l > vr) { n       return 0; n     } n     if (l <= vl && vr <= r) { n       return drz[v].sum; n     } n     push(v, vr - vl + 1); n     int vc = (vl + vr) / 2; n     return sum_rec(2 * v, vl, vc, l, r) + sum_rec(2 * v + 1, vc + 1, vr, l, r); n   } n   dd sum(int l, int r) { return sum_rec(1, 0, S - 1, l, r); } n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int n, q; n   cin >> n >> q; n   vector<int> tab(n + 1); n   for (int i = 1; i <= n; i++) { n     cin >> tab[i]; n   } n   Tree tree(tab); n   while (q--) { n     int t; n     cin >> t; n     if (t == 1) { n       int l1, r1, l2, r2; n       cin >> l1 >> r1 >> l2 >> r2; n       auto sum1 = tree.sum(l1, r1); n       auto sum2 = tree.sum(l2, r2); n       int len1 = r1 - l1 + 1; n       int len2 = r2 - l2 + 1; n       tree.mul_add(l1, r1, 1.0 * (len1 - 1) / len1, 1.0 * sum2 / len1 / len2); n       tree.mul_add(l2, r2, 1.0 * (len2 - 1) / len2, 1.0 * sum1 / len1 / len2); n     } else { n       int l, r; n       cin >> l >> r; n       cout << setprecision(10) << fixed << tree.sum(l, r) <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int day[100005]; n int cnt[100005]; n int vis[100005]; n int n, m; n int jud(int x) { n   memset(vis, 0, sizeof(vis)); n   int i, j; n   int sum = 0, cntt = 0; n   ; n   for (i = x; i >= 1; i--) { n     if (day[i] && vis[day[i]] == 0) { n       cntt++; n       vis[day[i]] = 1; n       sum += cnt[day[i]]; n     } else if (sum) n       sum--; n   } n   if (cntt == m && sum == 0) n     return 1; n   else n     return 0; n } n int main() { n   int i, j; n   scanf( %d %d , &n, &m); n   for (i = 1; i <= n; i++) scanf( %d , &day[i]); n   for (i = 1; i <= m; i++) scanf( %d , &cnt[i]); n   int left, right, mid; n   int ans = -1; n   left = 1, right = n; n   while (left <= right) { n     mid = (left + right) >> 1; n     if (jud(mid)) { n       right = mid - 1; n       ans = mid; n     } else { n       left = mid + 1; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, j, t, k, x, y; n   scanf( %d , &t); n   int a[10] = {0}, b[10] = {0}; n   for (j = 0; j < t; j++) { n     scanf( %d%d%d , &k, &x, &y); n     if (k == 1) { n       a[0] = a[0] + x; n       a[1] = a[1] + y; n     } else { n       b[0] = b[0] + x; n       b[1] = b[1] + y; n     } n   } n   if (a[0] >= a[1]) n     printf( LIVE n ); n   else n     printf( DEAD n ); n   if (b[0] >= b[1]) n     printf( LIVE n ); n   else n     printf( DEAD n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   vector<string> a; n   vector<string> ra; n   cin >> n >> m; n   for (int i = 0; i < n; ++i) { n     string s; n     cin >> s; n     a.push_back(s); n     reverse(s.begin(), s.end()); n     ra.push_back(s); n   } n   vector<int> selected; n   vector<int> arr(n, -1); n   vector<int> pallindrome; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       if (ra[i] == a[j]) { n         arr[i] = j; n         if (arr[j] != i) { n           selected.push_back(i); n         } else if (i == j) { n           pallindrome.push_back(i); n         } n       } n     } n   } n   cout << (selected.size() * 2 * m) + (pallindrome.size() > 0 ? 1 : 0) * m n        << endl; n   for (int i = 0; i < selected.size(); ++i) { n     cout << a[selected[i]]; n   } n   if (pallindrome.size() > 0) { n     cout << a[pallindrome[0]]; n   } n   for (int i = selected.size() - 1; i >= 0; --i) { n     cout << ra[selected[i]]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct comp { n   bool operator()(long long int a, long long int b) { return a > b; } n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int n, k; n   cin >> n >> k; n   vector<pair<long long int, long long int> > pow(n); n   vector<long long int> mon(n); n   vector<long long int> res(n); n   priority_queue<long long int, vector<long long int>, comp> least; n   for (int(i) = (0); (i) < (n); (i)++) { n     long long int power; n     cin >> power; n     pow[i] = make_pair(power, i); n   } n   sort(pow.begin(), pow.end()); n   for (int(i) = (0); (i) < (n); (i)++) { n     cin >> mon[i]; n   } n   if (k == 0) { n     for (int(i) = (0); (i) < (n); (i)++) { n       if (i) n         cout <<     << mon[i]; n       else n         cout << mon[i]; n     } n     return 0; n   } n   least.push(mon[pow[0].second]); n   res[pow[0].second] = mon[pow[0].second]; n   for (int(i) = (1); (i) < (min(n, k + 1)); (i)++) { n     res[pow[i].second] = res[pow[i - 1].second] + mon[pow[i].second]; n     least.push(mon[pow[i].second]); n   } n   for (int(i) = (k + 1); (i) < (n); (i)++) { n     long long int toremove = least.top(); n     least.pop(); n     res[pow[i].second] = res[pow[i - 1].second] + mon[pow[i].second] - toremove; n     least.push(mon[pow[i].second]); n   } n   for (int(i) = (0); (i) < (n); (i)++) { n     if (i) n       cout <<     << res[i]; n     else n       cout << res[i]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n; n char str[1000010]; n long long cntu[1000010]; n long long cntd[1000010]; n long long sumu[1000010]; n long long sumd[1000010]; n long long CNTU; n long long CNTD; n int main() { n   scanf( %I64d%s , &n, str + 1); n   for (long long i = 1; i <= n; i++) { n     cntu[i] = cntu[i - 1]; n     cntd[i] = cntd[i - 1]; n     sumu[i] = sumu[i - 1]; n     sumd[i] = sumd[i - 1]; n     if (str[i] ==  U ) { n       CNTU++; n       cntu[i]++; n       sumu[i] += i; n     } else { n       CNTD++; n       cntd[i]++; n       sumd[i] += i; n     } n   } n   for (long long i = 1; i <= n; i++) n     if (str[i] ==  U ) { n       if (cntu[i - 1] >= CNTD - cntd[i]) { n         long long k = n             upper_bound(cntu + 1, cntu + n + 1, cntu[i - 1] - CNTD + cntd[i]) - n             cntu; n         printf( %I64d  , n                (-(sumu[i - 1] - sumu[k - 1]) + (sumd[n] - sumd[i])) * 2 + n - n                    i + 1); n       } else { n         long long k = n             lower_bound(cntd + 1, cntd + n + 1, cntd[i] + cntu[i - 1] + 1) - n             cntd; n         printf( %I64d  , (-sumu[i - 1] + (sumd[k] - sumd[i]) - i) * 2 + i); n       } n     } else { n       if (cntu[i - 1] <= CNTD - cntd[i]) { n         long long k = n             lower_bound(cntd + i, cntd + n + 1, cntd[i] + cntu[i - 1]) - cntd; n         printf( %I64d  , (-sumu[i - 1] + (sumd[k] - sumd[i])) * 2 + i); n       } else { n         long long k = upper_bound(cntu + 1, cntu + n + 1, n                                   cntu[i - 1] - CNTD + cntd[i] - 1) - n                       cntu; n         printf( %I64d  , n                (i - (sumu[i - 1] - sumu[k - 1]) + (sumd[n] - sumd[i])) * 2 + n - n                    i + 1); n       } n     } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 7002; n const int N = 100010; n int n, q; n bitset<M> num[M]; n bitset<M> sq[M]; n bitset<M> a[N]; n vector<int> squareFree; n int main() { n   cin >> n >> q; n   for (int i = 1; i < M; ++i) { n     int good = 1; n     for (int j = 2; j * j <= i; ++j) { n       if (i % (j * j) == 0) { n         good = 0; n         break; n       } n     } n     if (good) squareFree.push_back(i); n   } n   for (int i = 1; i < M; ++i) { n     for (int j = 1; j * j <= i; ++j) { n       if (i % j == 0) { n         num[i][j] = num[i][i / j] = 1; n       } n     } n   } n   for (int i = 1; i < M; ++i) { n     for (int j : squareFree) { n       if (i * j > 7000) break; n       sq[i][i * j] = 1; n     } n   } n   while (q--) { n     int cmd, x, y, z, v; n     scanf( %d %d , &cmd, &x); n     if (cmd == 1) { n       scanf( %d , &v); n       a[x] = num[v]; n     } else if (cmd == 2) { n       scanf( %d %d , &y, &z); n       a[x] = a[y] ^ a[z]; n     } else if (cmd == 3) { n       scanf( %d %d , &y, &z); n       a[x] = a[y] & a[z]; n     } else { n       scanf( %d , &v); n       int ans = (sq[v] & a[x]).count() % 2; n       printf( %d , ans); n     } n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long double EPS = 1e-9; n long long cel(long long a, long long b) { return ((a - 1) / b + 1); } n long long gcd(long long a, long long b) { n   if (a < b) swap(a, b); n   return (b == 0) ? a : gcd(b, a % b); n } n long long MIN(long long a, int b) { n   long long ans; n   (a >= b) ? ans = b : ans = a; n   return ans; n } n long long MAX(long long a, int b) { n   long long ans; n   (a >= b) ? ans = a : ans = b; n   return ans; n } n long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } n long long po(long long x, long long y) { n   long long ans = 1; n   while (y) { n     if (y & 1) { n       ans = (ans * x) % 1000000007; n     } n     y >>= 1; n     x = (x * x) % 1000000007; n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n, t, x, y, m, k; n   cin >> n >> m; n   vector<int> v(1000001, 0); n   vector<int> A(n), B(n), C(m), D(m); n   for (int i = 0; i < n; i++) { n     cin >> A[i] >> B[i]; n   } n   for (int i = 0; i < m; i++) { n     cin >> C[i] >> D[i]; n   } n   for (int i = 0; i < m; i++) { n     for (int j = 0; j < n; j++) { n       if (C[i] - A[j] >= 0) { n         v[C[i] - A[j]] = max(v[C[i] - A[j]], D[i] - B[j] + 1); n       } n     } n   } n   for (int i = 1000000 - 1; i >= 0; i--) { n     v[i] = max(v[i + 1], v[i]); n   } n   int ans = 1000000007; n   for (int i = 0; i < 1000001; i++) { n     y = ans; n     ans = min(ans, i + v[i]); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int count_on(int mask) { n   int ans = 0; n   while (mask) { n     if (mask & 1) { n       ans++; n     } n     mask >>= 1; n   } n   return ans; n } n int dp[1 << 20]; n string bin(int mask) { n   string str =   ; n   while (mask) { n     if (mask & 1) { n       str += ( 1 ); n     } else { n       str += ( 0 ); n     } n     mask >>= 1; n   } n   reverse(str.begin(), str.end()); n   if (str ==   ) { n     str =  0 ; n   } n   return str; n } n int main() { n   string str; n   cin >> str; n   vector<int> masks; n   int N = str.size(); n   for (int i = 0; i < N; i++) { n     int mask = 0; n     for (int j = 0; i + j < N && j < 20; j++) { n       int dig = str[i + j] -  a ; n       if (mask & (1 << dig)) { n         break; n       } n       mask |= (1 << dig); n       masks.push_back(mask); n       dp[mask] = count_on(mask); n     } n   } n   for (int i = 0; i < (1 << 20); i++) { n     for (int j = 0; j < 20; j++) { n       if (i & (1 << j)) { n         dp[i] = max(dp[i], dp[i ^ (1 << j)]); n       } n     } n   } n   int best = 0; n   for (auto mask : masks) { n     int ans = dp[mask] + dp[((1 << 20) - 1) ^ mask]; n     best = max(best, ans); n   } n   cout << best << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int M; n int next_losing_index(int A, int losing_index) { n   return A == 0 || losing_index < M ? min(losing_index + 1, M) : 0; n } n struct segment_change { n   bool flip; n   segment_change(bool _flip = false) : flip(_flip) {} n   void reset() { flip = false; } n   segment_change combine(const segment_change &other) const { n     return segment_change(flip ^ other.flip); n   } n }; n struct segment { n   vector<int> through, flipped; n   segment() { n     through.resize(M + 1); n     flipped.resize(M + 1); n     for (int index = 0; index <= M; index++) n       through[index] = flipped[index] = index; n   } n   void init(int start, int end, int A) { n     for (int index = 0; index <= M; index++) { n       through[index] = next_losing_index(A, index); n       flipped[index] = next_losing_index(A ^ 1, index); n     } n   } n   void apply(int start, int end, const segment_change &change) { n     if (change.flip) swap(through, flipped); n   } n   void join(const segment &other) { n     vector<int> next_through(M + 1), next_flipped(M + 1); n     for (int index = 0; index <= M; index++) { n       next_through[index] = through[other.through[index]]; n       next_flipped[index] = flipped[other.flipped[index]]; n     } n     through = next_through; n     flipped = next_flipped; n   } n   void join(const segment &a, const segment &b) { n     *this = a; n     join(b); n   } n }; n struct seg_tree { n   int tree_n; n   vector<segment> tree; n   vector<segment_change> changes; n   seg_tree(int n = 0) { n     tree_n = 0; n     if (n > 0) init(n); n   } n   void init(int n) { n     tree_n = 1; n     while (tree_n < n) tree_n *= 2; n     tree.assign(2 * tree_n, segment()); n     changes.assign(2 * tree_n, segment_change()); n   } n   template <typename T> n   void build(const vector<T> &initial) { n     int n = initial.size(); n     assert(n <= tree_n); n     for (int i = 0; i < n; i++) tree[tree_n + i].init(i, i + 1, initial[i]); n     for (int position = tree_n - 1; position > 0; position--) n       tree[position].join(tree[2 * position], tree[2 * position + 1]); n   } n   void push_down(int position, int start, int mid, int end) { n     tree[2 * position].apply(start, mid, changes[position]); n     tree[2 * position + 1].apply(mid, end, changes[position]); n     changes[2 * position] = changes[2 * position].combine(changes[position]); n     changes[2 * position + 1] = n         changes[2 * position + 1].combine(changes[position]); n     changes[position].reset(); n   } n   template <typename T_range_op, typename T_join_op> n   void process_range(int position, int start, int end, int a, int b, n                      T_range_op &&range_op, T_join_op &&join_op) { n     if (a <= start && end <= b) { n       range_op(position, start, end); n       return; n     } n     if (position >= tree_n) return; n     int mid = (start + end) / 2; n     push_down(position, start, mid, end); n     if (a < mid) n       process_range(2 * position, start, mid, a, b, range_op, join_op); n     if (b > mid) n       process_range(2 * position + 1, mid, end, a, b, range_op, join_op); n     join_op(position); n   } n   segment query(int a, int b) { n     segment answer; n     process_range( n         1, 0, tree_n, a, b, n         [&](int position, int, int) { answer.join(tree[position]); }, n         [&](int) {}); n     return answer; n   } n   void update(int a, int b, const segment_change &change) { n     process_range( n         1, 0, tree_n, a, b, n         [&](int position, int start, int end) { n           tree[position].apply(start, end, change); n           changes[position] = changes[position].combine(change); n         }, n         [&](int position) { n           tree[position].join(tree[2 * position], tree[2 * position + 1]); n         }); n   } n   template <typename T_bool> n   int find_last_prefix(int n, T_bool &&should_join) { n     segment current; n     if (!should_join(current, current)) return -1; n     if (should_join(current, tree[1])) return n; n     int position = 1; n     int start = 0, end = tree_n; n     while (position < tree_n) { n       int mid = (start + end) / 2; n       push_down(position, start, mid, end); n       if (should_join(current, tree[2 * position])) { n         current.join(tree[2 * position]); n         position = 2 * position + 1; n         start = mid; n       } else { n         position = 2 * position; n         end = mid; n       } n     } n     assert(position == tree_n + start); n     assert(0 <= start && start < n); n     assert(end - start == 1); n     return start; n   } n }; n int N, Q; n vector<int> A; n int main() { n   scanf( %d %d %d , &N, &M, &Q); n   A.resize(N); n   for (int i = 0; i < N; i++) { n     long long x; n     scanf( %lld , &x); n     A[i] = x % 2; n   } n   seg_tree tree(N); n   tree.build(A); n   for (int q = 0; q < Q; q++) { n     int type, L, R; n     scanf( %d %d %d , &type, &L, &R); n     L--; n     if (type == 1) { n       long long d; n       scanf( %lld , &d); n       if (d % 2 == 1) tree.update(L, R, segment_change(true)); n     } else if (type == 2) { n       puts(tree.query(L, R).through[M] != 0 ?  1  :  2 ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> tree[100005]; n int cat[100005]; n bool vis[100005]; n int n, m, ans = 0; n void dfs(int root, int num) { n   vis[root] = true; n   if (cat[root]) n     num++; n   else n     num = 0; n   if (num > m) return; n   if (tree[root].size() == 1 && vis[tree[root][0]]) { n     ans++; n     return; n   } n   for (int i = 0; i < tree[root].size(); i++) { n     int c = tree[root][i]; n     if (!vis[c]) dfs(c, num); n   } n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , &cat[i]); n   for (int i = 0; i < n - 1; i++) { n     int f, s; n     scanf( %d %d , &f, &s); n     tree[f].push_back(s); n     tree[s].push_back(f); n   } n   dfs(1, 0); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   string t; n   cin >> s >> t; n   long j = 0; n   long ans = 0; n   for (long i = 0; i < t.size(); i++) { n     if (t[i] == s[j]) ++j; n     if (j == s.size()) { n       long rj = s.size() - 1; n       for (long k = t.size() - 1; k >= 0; k--) { n         if (t[k] == s[rj]) --rj; n         if (rj == -1) { n           cout << std::max(k - i, long(0)); n           return 0; n         } n       } n       break; n     } n   } n   cout << 0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   char ch[n]; n   ch[n] =   0 ; n   int a[n][2]; n   for (int i = 0; i < n; i++) { n     cin >> a[i][0] >> a[i][1]; n   } n   int suma = 0; n   int sumg = 0; n   for (int i = 0; i < n; i++) { n     if (((a[i][0] + suma) - sumg) <= 500) { n       suma += a[i][0]; n       ch[i] =  A ; n     } else if (((a[i][1] + sumg) - suma) <= 500) { n       sumg += a[i][1]; n       ch[i] =  G ; n     } n   } n   if ((abs(suma - sumg)) > 500) { n     cout <<  -1 ; n   } else { n     cout << ch; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void rread(T& num) { n   num = 0; n   T f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) num = num * 10 + ch -  0 , ch = getchar(); n   num *= f; n } n const int maxn = 1e5 + 5, maxpow = 9; n const long long inf = 1e18; n int n, m; n long long a[maxn]; n vector<long long> bin; n inline void Prework() { n   long long now = 1; n   for (int(i) = (0); (i) < (maxpow); (i)++) bin.push_back(now), now *= 42; n } n inline long long GetToNxt(long long x) { n   int tmp = lower_bound(bin.begin(), bin.end(), x) - bin.begin(); n   if (tmp == maxpow) return inf; n   return bin[tmp] - x; n } n int root; n namespace Treap { n int ndtot; n struct TreapNode { n   int l, r, len, sz; n   long long val, tonxt, mini, tag; n } T[maxn]; n vector<int> pool; n inline int NewNode(long long val, int len) { n   int x; n   if (((pool).size())) n     x = pool.back(), pool.pop_back(); n   else n     x = ++ndtot; n   (T[(x)].l) = (T[(x)].r) = (T[(x)].tag) = 0; n   (T[(x)].val) = val; n   (T[(x)].sz) = (T[(x)].len) = len; n   (T[(x)].tonxt) = (T[(x)].mini) = GetToNxt(val); n   return x; n } n inline void Update(int x) { n   (T[(x)].sz) = (T[(x)].len) + (T[((T[(x)].l))].sz) + (T[((T[(x)].r))].sz); n   (T[(x)].mini) = n       min((T[(x)].tonxt), min((T[((T[(x)].l))].mini), (T[((T[(x)].r))].mini))); n } n inline int Build(int l, int r) { n   if (l > r) return 0; n   int x = NewNode(a[((l + r) >> 1)], 1); n   (T[(x)].l) = Build(l, ((l + r) >> 1) - 1); n   (T[(x)].r) = Build(((l + r) >> 1) + 1, r); n   Update(x); n   return x; n } n inline void AddVal(int x, long long num) { n   if (!x) return; n   (T[(x)].tag) += num; n   (T[(x)].val) += num; n   (T[(x)].tonxt) -= num; n   (T[(x)].mini) -= num; n } n inline void PushDown(int x) { n   if (!x || !(T[(x)].tag)) return; n   AddVal((T[(x)].l), (T[(x)].tag)); n   AddVal((T[(x)].r), (T[(x)].tag)); n   (T[(x)].tag) = 0; n } n inline int Rnd(int x, int y) { n   return rand() % ((T[(x)].sz) + (T[(y)].sz)) < (T[(x)].sz); n } n inline int Merge(int x, int y) { n   if (!x || !y) return x + y; n   PushDown(x); n   PushDown(y); n   if (Rnd(x, y)) { n     (T[(x)].r) = Merge((T[(x)].r), y); n     Update(x); n     return x; n   } else { n     (T[(y)].l) = Merge(x, (T[(y)].l)); n     Update(y); n     return y; n   } n } n inline pair<int, int> Split(int x, int len) { n   if (!x) return make_pair(0, 0); n   PushDown(x); n   if ((T[((T[(x)].l))].sz) >= len) { n     pair<int, int> y = Split((T[(x)].l), len); n     (T[(x)].l) = y.second; n     Update(x); n     return make_pair(y.first, x); n   } n   len -= (T[((T[(x)].l))].sz); n   if (len < (T[(x)].len)) { n     int x1 = NewNode((T[(x)].val), len), n         x2 = NewNode((T[(x)].val), (T[(x)].len) - len); n     (T[(x1)].l) = (T[(x)].l); n     (T[(x2)].r) = (T[(x)].r); n     Update(x1); n     Update(x2); n     pool.push_back(x); n     return make_pair(x1, x2); n   } n   len -= (T[(x)].len); n   pair<int, int> z = Split((T[(x)].r), len); n   (T[(x)].r) = z.first; n   Update(x); n   return make_pair(x, z.second); n } n inline long long QueryVal(int x) { n   long long res; n   pair<int, int> b = Split(root, x), a = Split(b.first, x - 1); n   res = (T[(a.second)].val); n   b.first = Merge(a.first, a.second); n   root = Merge(b.first, b.second); n   return res; n } n inline void Recycle(int x) { n   if (!x) return; n   Recycle((T[(x)].l)); n   pool.push_back(x); n   Recycle((T[(x)].r)); n } n inline void SetVal(int l, int r, long long val) { n   pair<int, int> b = Split(root, r), a = Split(b.first, l - 1); n   Recycle(a.second); n   int x = NewNode(val, r - l + 1); n   b.first = Merge(a.first, x); n   root = Merge(b.first, b.second); n } n inline void FixMini(int x) { n   if (!x) return; n   PushDown(x); n   if ((T[(x)].tonxt) == (T[(x)].mini)) { n     (T[(x)].tonxt) = GetToNxt((T[(x)].val)); n     Update(x); n     return; n   } n   if ((T[((T[(x)].l))].mini) < (T[((T[(x)].r))].mini)) n     FixMini((T[(x)].l)); n   else n     FixMini((T[(x)].r)); n   Update(x); n } n inline void Print(int x) { n   if (!x) return; n   Print((T[(x)].l)); n   cerr << (T[(x)].val) <<  (  << (T[(x)].tonxt) <<  )  ; n   Print((T[(x)].r)); n } n inline void AddVal(int l, int r, long long x) { n   pair<int, int> b = Split(root, r), a = Split(b.first, l - 1); n   AddVal(a.second, x); n   while (1) { n     while ((T[(a.second)].mini) < 0) FixMini(a.second); n     if ((T[(a.second)].mini) > 0) break; n     AddVal(a.second, x); n   } n   b.first = Merge(a.first, a.second); n   root = Merge(b.first, b.second); n } n inline void Init() { (T[(0)].mini) = inf; } n }  // namespace Treap n inline void judge() { n   freopen( input.txt ,  r , stdin); n   freopen( output.txt ,  w , stdout); n } n int main() { n   Prework(); n   Treap::Init(); n   rread(n); n   rread(m); n   for (int(i) = (1); (i) <= (n); (i)++) rread(a[i]); n   root = Treap::Build(1, n); n   while (m--) { n     int opt; n     rread(opt); n     if (opt == 1) { n       int x; n       rread(x); n       printf( %lld n , Treap::QueryVal(x)); n     } else if (opt == 2) { n       int l, r; n       long long x; n       rread(l); n       rread(r); n       rread(x); n       Treap::SetVal(l, r, x); n     } else if (opt == 3) { n       int l, r; n       long long x; n       rread(l); n       rread(r); n       rread(x); n       Treap::AddVal(l, r, x); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 10; n map<int, int> mx, my; n vector<int> v; n long long cal() { n   long long ans = 0; n   int sz = v.size(); n   sort(v.begin(), v.end()); n   int l = 0, r; n   while (l < sz) { n     r = l; n     while (r < sz - 1 && v[r + 1] == v[l]) r++; n     ans += 1ll * (r - l + 1) * (r - l) / 2; n     l = r + 1; n   } n   return ans; n } n struct point { n   int x; n   int y; n } px[N], py[N]; n bool cmpx(point &u, point &v) { return u.x < v.x; } n bool cmpy(point &u, point &v) { return u.y < v.y; } n int read() { n   char c = getchar(); n   int ans = 0; n   while (c ==     || c ==   n ) c = getchar(); n   while (c >=  0  && c <=  9 ) ans = ans * 10 + c -  0 , c = getchar(); n   return ans; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     int cx = 0, cy = 0; n     long long ans = 0; n     mx.clear(); n     my.clear(); n     int n, m, k; n     cin >> n >> m >> k; n     for (int i = 1, x; i <= n; i++) { n       x = read(); n       mx[x] = 1; n     } n     for (int i = 1, y; i <= m; i++) { n       y = read(); n       my[y] = 1; n     } n     for (int i = 1, x, y; i <= k; i++) { n       x = read(); n       y = read(); n       if (mx.count(x) && my.count(y)) continue; n       if (mx.count(x)) n         py[++cy].x = x, py[cy].y = y; n       else n         px[++cx].x = x, px[cx].y = y; n     } n     sort(px + 1, px + cx + 1, cmpx); n     sort(py + 1, py + cy + 1, cmpy); n     map<int, int>::iterator P = mx.begin(); n     int L = 1, R; n     while (P != mx.end()) { n       v.clear(); n       v.push_back(px[L].y); n       int l = P->first; n       P++; n       int r = P->first; n       if (P == mx.end()) break; n       if (r < px[L].x) continue; n       R = L; n       while (R < cx && px[R + 1].x < r) R++, v.push_back(px[R].y); n       ans += 1LL * (R - L + 1) * (R - L) / 2 - cal(); n       L = R + 1; n     } n     L = 1; n     P = my.begin(); n     while (P != my.end()) { n       v.clear(); n       v.push_back(py[L].x); n       int l = P->first; n       P++; n       int r = P->first; n       if (P == my.end()) break; n       if (r < py[L].y) continue; n       R = L; n       while (R < cy && py[R + 1].y < r) R++, v.push_back(py[R].x); n       ans += 1LL * (R - L + 1) * (R - L) / 2 - cal(); n       L = R + 1; n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, excluidos = 0; n vector<priority_queue<int, vector<int>, greater<int>>> Vpq; n bool umVazio() { n   for (auto u : Vpq) { n     if (u.size() == 0) return true; n   } n   return false; n } n int procura(int indAnterior, int i) { n   int num = -1; n   while (Vpq[i].size() != 0) { n     if (Vpq[i].top() > indAnterior) { n       num = Vpq[i].top(); n       Vpq[i].pop(); n       return num; n     } else { n       excluidos++; n       Vpq[i].pop(); n     } n   } n   return num; n } n int main() { n   cin.tie(nullptr); n   ios_base::sync_with_stdio(0); n   cin >> n; n   int a; n   Vpq.resize(6); n   for (int i = 0; i < n; i++) { n     cin >> a; n     if (a == 4) { n       Vpq[0].push(i); n     } else if (a == 8) { n       Vpq[1].push(i); n     } else if (a == 15) { n       Vpq[2].push(i); n     } else if (a == 16) { n       Vpq[3].push(i); n     } else if (a == 23) { n       Vpq[4].push(i); n     } else if (a == 42) { n       Vpq[5].push(i); n     } n   } n   bool flag = 1; n   while (flag) { n     int indAnterior = -2; n     for (int i = 0; i < 6; i++) { n       indAnterior = procura(indAnterior, i); n       if (indAnterior == -1) { n         excluidos += i; n         flag = 0; n         break; n       } n     } n   } n   for (int i = 0; i < 6; i++) { n     excluidos += Vpq[i].size(); n   } n   cout << excluidos << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T abs(T x) { n   return x > 0 ? x : -x; n } n const double eps = 1e-10; n int n; n int m; n double ax, ay, az, vx, vy, vz, R, r, ox, oy, oz, px, py, pz; n double scal(double x1, double y8687969, double z1, double x2, double y2, n             double z2) { n   return x1 * x2 + y8687969 * y2 + z1 * z2; n } n double vect(double x1, double y8687969, double z1, double x2, double y2, n             double z2) { n   double x3 = y8687969 * z2 - y2 * z1; n   double y3 = z1 * x2 - z2 * x1; n   double z3 = x1 * y2 - x2 * y8687969; n   return sqrt(abs(((x3) * (x3)) + ((y3) * (y3)) + ((z3) * (z3)))); n } n double dist1(double x1, double y8687969, double z1, double x2, double y2, n              double z2) { n   return sqrt(abs(((x2 - x1) * (x2 - x1)) + n                   ((y2 - y8687969) * (y2 - y8687969)) + n                   ((z2 - z1) * (z2 - z1)))); n } n double dist2(double x1, double y8687969, double z1, double x2, double y2, n              double z2, double x3, double y3, double z3) { n   double tmp = min(dist1(x1, y8687969, z1, x2, y2, z2), n                    dist1(x1, y8687969, z1, x3, y3, z3)); n   double s1 = scal(x1 - x2, y8687969 - y2, z1 - z2, x3 - x2, y3 - y2, z3 - z2); n   double s2 = scal(x1 - x3, y8687969 - y3, z1 - z3, x2 - x3, y2 - y3, z2 - z3); n   if (s1 > 0 && s2 > 0) { n     double d = dist1(x2, y2, z2, x3, y3, z3); n     double s = n         vect(x2 - x1, y2 - y8687969, z2 - z1, x3 - x1, y3 - y8687969, z3 - z1); n     tmp = s / d; n   } n   return tmp; n } n double get1(double x, double y, double z, double r) { n   double p = 0, q = 1e13; n   for (int it = 0; it < 300; it++) { n     double s1 = (2 * p + q) / 3; n     double s2 = (p + 2 * q) / 3; n     double d1 = dist1(ax + vx * s1, ay + vy * s1, az + vz * s1, x, y, z); n     double d2 = dist1(ax + vx * s2, ay + vy * s2, az + vz * s2, x, y, z); n     if (d1 < d2) n       q = s2; n     else n       p = s1; n   } n   double s = (p + q) / 2; n   double d = dist1(ax + vx * s, ay + vy * s, az + vz * s, x, y, z); n   if (d > r + R + eps) return 1e15; n   p = 0; n   q = s; n   for (int it = 0; it < 300; it++) { n     s = (p + q) / 2; n     d = dist1(ax + vx * s, ay + vy * s, az + vz * s, x, y, z); n     if (d > r + R) n       p = s; n     else n       q = s; n   } n   return q; n } n double get2(double x1, double y8687969, double z1, double x2, double y2, n             double z2) { n   double p = 0, q = 1e13; n   for (int it = 0; it < 300; it++) { n     double s1 = (2 * p + q) / 3; n     double s2 = (p + 2 * q) / 3; n     double d1 = dist2(ax + vx * s1, ay + vy * s1, az + vz * s1, x1, y8687969, n                       z1, x2, y2, z2); n     double d2 = dist2(ax + vx * s2, ay + vy * s2, az + vz * s2, x1, y8687969, n                       z1, x2, y2, z2); n     if (d1 < d2) n       q = s2; n     else n       p = s1; n   } n   double s = (p + q) / 2; n   double d = dist2(ax + vx * s, ay + vy * s, az + vz * s, x1, y8687969, z1, x2, n                    y2, z2); n   if (d > R + eps) return 1e15; n   p = 0; n   q = s; n   for (int it = 0; it < 300; it++) { n     s = (p + q) / 2; n     d = dist2(ax + vx * s, ay + vy * s, az + vz * s, x1, y8687969, z1, x2, y2, n               z2); n     if (d > R) n       p = s; n     else n       q = s; n   } n   return q; n } n int main() { n   cin >> ax >> ay >> az >> vx >> vy >> vz >> R >> n; n   double ans = 1e15; n   for (int i = 0; i < n; i++) { n     cin >> ox >> oy >> oz >> r >> m; n     ans = min(ans, get1(ox, oy, oz, r)); n     for (int j = 0; j < m; j++) { n       cin >> px >> py >> pz; n       ans = min(ans, get2(ox, oy, oz, ox + px, oy + py, oz + pz)); n     } n   } n   if (ans > 5e14) n     printf( -1 n ); n   else n     printf( %.10f n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class trio { n  public: n   long long first, second, third; n }; n class quad { n  public: n   long long first, second, third, fourth; n }; n class DSU { n  public: n   long long *par, *size; n   DSU(long long n) { n     par = new long long[n + 1]; n     size = new long long[n + 1]; n     for (long long i = 0; i < n + 1; ++i) { n       par[i] = i; n       size[i] = 0; n     } n   } n   long long find(long long a) { n     long long p = a, third; n     while (par[p] != p) p = par[p]; n     while (p != a) { n       third = par[a]; n       par[a] = p; n       a = third; n     } n     return a; n   } n   void union_(long long a, long long b) { n     long long u1 = find(a); n     long long u2 = find(b); n     if (size[u1] < size[u2]) { n       par[u1] = u2; n     } else { n       if (size[u2] < size[u1]) { n         par[u2] = u1; n       } else { n         par[u1] = u2; n         size[u1]++; n       } n     } n   } n }; n bool mycom(quad a, quad b) { return a.first < b.first; } n long long *intime, *outime, timt = 1; n bool inacestor(long long a, long long b) { n   return (intime[a] <= intime[b] && outime[a] >= outime[b]); n } n long long tree[100010][20]; n long long dp[100010][20]; n void dfs(long long i, list<pair<long long, long long> > *adj) { n   intime[i] = timt; n   timt++; n   for (auto it : adj[i]) { n     if (it.first != tree[i][0]) { n       tree[it.first][0] = i; n       dp[it.first][0] = it.second; n       dfs(it.first, adj); n     } n   } n   outime[i] = timt; n   timt++; n } n long long func(long long a, long long b) { n   long long ans = 0; n   for (long long j = 19; j >= 0; --j) { n     if (inacestor(tree[a][j], b)) continue; n     ans = max(ans, dp[a][j]); n     a = tree[a][j]; n   } n   for (long long j = 19; j >= 0; --j) { n     if (inacestor(tree[b][j], a)) continue; n     ans = max(ans, dp[b][j]); n     b = tree[b][j]; n   } n   if (!inacestor(a, b) && !inacestor(b, a)) { n     ans = max(ans, dp[b][0]); n     ans = max(ans, dp[a][0]); n   } else { n     if (inacestor(a, b)) { n       ans = max(ans, dp[b][0]); n     } else n       ans = max(ans, dp[a][0]); n   } n   return ans; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long n, m; n   cin >> n >> m; n   quad edges[m + 1]; n   bool select[m + 1]; n   memset(select, 0, sizeof(select)); n   for (long long i = 0; i < m; ++i) { n     cin >> edges[i].second >> edges[i].third >> edges[i].first; n     edges[i].fourth = i; n   } n   sort(edges, edges + m, mycom); n   DSU dsu1(n); n   long long ans[m + 1]; n   for (long long i = 0; i < m; ++i) { n     ans[i] = -1; n   } n   list<pair<long long, long long> > adj[n + 1]; n   for (long long i = 0; i < m; ++i) { n     if (dsu1.find(edges[i].second) != dsu1.find(edges[i].third)) { n       dsu1.union_(edges[i].second, edges[i].third); n       select[i] = 1; n       adj[edges[i].second].push_back({edges[i].third, edges[i].first}); n       adj[edges[i].third].push_back({edges[i].second, edges[i].first}); n     } n   } n   intime = new long long[n + 1]; n   outime = new long long[n + 1]; n   tree[1][0] = -1; n   dfs(1, adj); n   tree[1][0] = 1; n   dp[1][0] = 0; n   for (long long j = 1; j < 20; ++j) { n     for (long long i = 1; i < n + 1; ++i) { n       tree[i][j] = tree[tree[i][j - 1]][j - 1]; n       dp[i][j] = max(dp[tree[i][j - 1]][j - 1], dp[i][j - 1]); n     } n   } n   for (long long j = 0; j < m; ++j) { n     if (select[j] == 0) { n       ans[edges[j].fourth] = func(edges[j].second, edges[j].third); n     } n   } n   for (long long i = 0; i < m; ++i) { n     if (ans[i] != -1) { n       cout << ans[i] << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T& x) { n   x = 0; n   char c; n   int sign = 1; n   do { n     c = getchar(); n     if (c ==  - ) sign = -1; n   } while (c <  0  || c >  9 ); n   do { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } while (c <=  9  && c >=  0 ); n   x *= sign; n } n const int N = 101, inf = 1e6; n int t, n, cnt, a[N], k[N]; n bool vis[inf << 1]; n bool check(int x) { n   for (register int i = 1; i <= n; ++i) n     if (vis[a[i] + x]) return 0; n   for (register int i = 1; i <= n; ++i) vis[a[i] + x] = 1; n   return 1; n } n inline int solve() { n   cnt = 0; n   memset(vis, 0, sizeof vis); n   read(n); n   for (register int i = 1; i <= n; ++i) read(a[i]); n   sort(a + 1, a + n + 1); n   for (register int i = 2; i <= n; ++i) n     if (a[i] == a[i - 1]) return puts( NO ), 0; n   for (register int i = 1; i <= inf; ++i) n     if (cnt == n) n       break; n     else if (check(i)) n       k[++cnt] = i; n   if (cnt == n) { n     puts( YES ); n     for (register int i = 1; i <= n; ++i) printf( %d  , k[i]); n     printf(  n ); n   } else n     puts( NO ); n   return 0; n } n int main() { n   read(t); n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long binPow(long long a, long long b) { n   a = a % 1000000007; n   if (a == 0) return 0; n   long long res = 1; n   while (b > 0) { n     if (b & 1) res = (res * a) % 1000000007; n     a = (a * a) % 1000000007; n     b >>= 1; n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   cin >> n >> m; n   string p; n   cin >> p; n   int size = p.size(); n   int PI[size]; n   PI[0] = 0; n   for (int i = 1; i < size; i++) { n     int j = PI[i - 1]; n     while (j > 0 && p[i] != p[j]) j = PI[j - 1]; n     if (p[i] == p[j]) j++; n     PI[i] = j; n   } n   unordered_map<int, int> make_pair; n   int j = PI[size - 1]; n   while (j > 0) { n     make_pair[j]++; n     j = PI[j - 1]; n   } n   int y; n   int last = 1; n   long long count = 0; n   for (int i = 0; i < m; i++) { n     cin >> y; n     if (y < last) { n       if (make_pair[last - y] == 0) { n         cout << 0 <<   n ; n         return 0; n       } n     } else { n       count += y - last; n     } n     last = min(n + 1, y + size); n   } n   count += max(0, n + 1 - last); n   cout << binPow(26, count) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int arr[n], brr[n]; n   for (int i = 0; i < n; ++i) { n     cin >> arr[i] >> brr[i]; n   } n   for (int i = 0; i < n; ++i) { n     if (arr[i] != brr[i]) { n       cout <<  rated ; n       return 0; n     } n   } n   for (int i = 0; i < n - 1; ++i) { n     if (arr[i] < arr[i + 1] && brr[i] < brr[i + 1]) { n       cout <<  unrated ; n       return 0; n     } n   } n   cout <<  maybe ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long int t, n, i, x, mx, mn, f, j, s, r, y, c, m, k, c1; n   cin >> t; n   while (t--) { n     cin >> n; n     vector<long long int> v; n     f = 0; n     for (i = 0; i < n; i++) { n       cin >> x; n       v.push_back(x); n       if (x == 0) f = 1; n     } n     s = 2; n     r = pow(3, n); n     while (s < r) { n       x = s; n       s++; n       c = c1 = 0; n       set<long long int> S; n       for (i = 0; i < n; i++) { n         if (x % 3 == 1) n           c += v[i]; n         else if (x % 3 == 2) n           c -= v[i]; n         else n           S.insert(v[i]); n         x /= 3; n       } n       if (S.count(c)) f = 1; n     } n     if (f) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T &x) { n   cin >> x; n } n template <typename T, typename T0> n void read(T &x, T0 &y) { n   cin >> x >> y; n } n template <typename T, typename T0, typename T1> n void read(T &x, T0 &y, T1 &z) { n   cin >> x >> y >> z; n } n bool isPowerOfTwo(int x) { return x && (!(x & (x - 1))); } n int digits(int n) { n   int x = floor(log10(n)) + 1; n   return x; n } n long long int fact(long long int n) { n   if (n == 0) return 1; n   return n * fact(n - 1); n } n bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.second < b.second); n } n vector<bool> sieve(1000000, true); n void sieve_er() { n   sieve[0] = false; n   sieve[1] = false; n   for (long long int i = 2; i * i <= 1000000; i++) { n     if (sieve[i] == true) { n       for (long long int j = i * i; j < 1000000; j += i) sieve[j] = false; n     } n   } n } n vector<long long int> vec; n void divisors(long long int n) { n   for (long long int i = 1; i <= sqrt(n); i++) { n     if (n % i == 0) { n       if (n / i == i) n         vec.push_back(i); n       else { n         vec.push_back(i); n         vec.push_back(n / i); n       } n     } n   } n } n long long int total_divisors(long long int n) { n   long long int c = 0; n   for (long long int i = 1; i <= sqrt(n); i++) { n     if (n % i == 0) { n       if (n / i == i) n         c = c + 1; n       else n         c = c + 2; n     } n   } n   return c; n } n long long int nCr(long long int n, long long int r) { n   long long int ans = 1; n   for (int i = 0; i < r; i++) { n     ans *= n - i; n     ans /= i + 1; n   } n   return ans; n } n long long int power_m(long long int a, long long int b, n                       long long int m = 1000000007) { n   a %= m; n   long long int res = 1; n   while (b > 0) { n     if (b & 1) res = res * a % m; n     a = a * a % m; n     b >>= 1; n   } n   return res; n } n long long int power(long long int a, long long int b) { n   long long int res = 1; n   while (b > 0) { n     if (b & 1) res = res * a; n     a = a * a; n     b >>= 1; n   } n   return res; n } n long long int strTo(string s) { n   stringstream tmp(s); n   long long int x = 0; n   tmp >> x; n   return x; n } n string NoTo(long long int n) { n   ostringstream str1; n   str1 << n; n   string ans = str1.str(); n   return ans; n } n void start() {} n long long int tree[4000001]; n long long int a[10000001]; n void build(int i, int s, int e) { n   if (s > e) return; n   if (s == e) { n     tree[i] = a[s]; n     return; n   } n   int mid = (s + e) / 2; n   build(2 * i, s, mid); n   build(2 * i + 1, mid + 1, e); n   tree[i] = (tree[2 * i] + tree[2 * i + 1]); n   return; n } n void un(int in, int s, int e, int i, int val) { n   if (i < s || i > e) return; n   if (s == e) { n     tree[in] += val; n     return; n   } n   int mid = (s + e) / 2; n   un(2 * in, s, mid, i, val); n   un(2 * in + 1, mid + 1, e, i, val); n   tree[in] = (tree[2 * in] + tree[2 * in + 1]); n   return; n } n long long int query(int in, int s, int e, int qs, int qe) { n   if (qs > e || qe < s) return 0; n   if (s >= qs && e <= qe) return tree[in]; n   int mid = (s + e) / 2; n   long long int left = query(2 * in, s, mid, qs, qe); n   long long int right = query(2 * in + 1, mid + 1, e, qs, qe); n   return left + right; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int a, b, c; n   read(a, b, c); n   int ans = 0; n   int z = min(a / 3, min(b / 2, c / 2)); n   ans += (7 * z); n   a -= (z * 3); n   b -= (z * 2); n   c -= (z * 2); n   char ar[7] = { a ,  a ,  b ,  c ,  a ,  c ,  b }; n   int ma = -1; n   for (int i = 0; i < 7; i++) { n     int j = i; n     int a1 = a, b1 = b, c1 = c; n     int x = 0; n     while (true) { n       if (ar[j] ==  a ) { n         if (a1 > 0) { n           a1--; n           x++; n         } else n           break; n       } n       if (ar[j] ==  b ) { n         if (b1 > 0) { n           b1--; n           x++; n         } else n           break; n       } n       if (ar[j] ==  c ) { n         if (c1 > 0) { n           c1--; n           x++; n         } else n           break; n       } n       if (j == 6) n         j = 0; n       else n         j++; n     } n     ma = max(ma, x); n   } n   ans += ma; n   cout << ans <<   n ; n   cerr <<  Time :   << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC n        <<  ms n ; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   std::ios::sync_with_stdio(false); n   int n; n   std::cin >> n; n   std::vector<int> data(n); n   for (int i = 0; i < n; i++) { n     std::cin >> data[i]; n   } n   int s; n   int t; n   std::cin >> s; n   std::cin >> t; n   --s; n   --t; n   if (s > t) { n     std::swap(s, t); n   } n   int sum1 = 0; n   for (int i = s; i < t; i++) { n     sum1 += data[i]; n   } n   int sum2 = std::accumulate(data.begin(), data.end(), 0) - sum1; n   int solution = std::min(sum1, sum2); n   std::cout << solution << std::endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n int f[N], ff[N]; n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     f[x]++; n   } n   multiset<int> ms; n   for (int i = 1; i < N; i++) { n     ms.insert(f[i]); n     ff[f[i]]++; n   } n   int q; n   cin >> q; n   while (q--) { n     char c; n     int x; n     cin >> c >> x; n     ff[f[x]]--; n     ms.erase(ms.find(f[x])); n     if (c ==  + ) { n       f[x]++; n     } else { n       f[x]--; n     } n     ff[f[x]]++; n     ms.insert(f[x]); n     if (ms.empty()) { n       cout <<  NO n ; n       continue; n     } n     if (*ms.rbegin() >= 8) { n       cout <<  YES n ; n       continue; n     } n     vector<int> t(8); n     for (int i = 1; i < 8; i++) t[i] = ff[i]; n     bool f = 0; n     int twos = 0; n     for (int i = 7; i >= 4; i--) { n       if (t[i]) { n         f = 1; n         t[i]--; n         t[i - 4]++; n         break; n       } n     } n     for (int i = 7; i >= 2; i--) { n       twos += (i / 2) * t[i]; n     } n     if (f && twos >= 2) { n       cout <<  YES n ; n     } else { n       cout <<  NO n ; n     } n   } n } n


#include <bits/stdc++.h> n const int inf = 1e7 + 7; n long long mod = 1e9 + 7; n const double eps = 1e-9; n using namespace std; n int num[1000005]; n char s[1000005]; n int main() { n   cin >> s; n   int len = strlen(s); n   num[len] = 0; n   for (int i = len - 1; i >= 0; i--) { n     if (s[i] ==  0 ) n       num[i] = num[i + 1] - 1; n     else { n       num[i] = num[i + 1] + 1; n       if (num[i] > 0) { n         num[i] = 0; n         s[i] =  0 ; n       } n     } n   } n   cout << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a, b, x, r; n int main() { n   for (cin >> n >> a >> b, r = n; x * a <= n; x++) n     r = min(r, (n - x * a) % (5 * b)); n   cout << r; n } n


#include <bits/stdc++.h> n using namespace std; n typedef long double ld; n const long double PI = n     3.141592653589793238462643383279502884197169399375105820974944; n long long mod = 1e9 + 7; n const long long inf = 1e18; n const long long maxn = 3e5 + 5; n void solve() { n   long long n, k; n   cin >> n >> k; n   vector<pair<long long, pair<long long, long long>>> v(n); n   for (long long i = 0; i < n; i++) { n     cin >> v[i].first >> v[i].second.first; n     v[i].second.second = i + 1; n   } n   sort((v).begin(), (v).end()); n   multiset<pair<long long, long long>> s; n   vector<long long> ans; n   for (auto i : v) { n     while (!s.empty() && (*s.begin()).first < i.first) s.erase(s.begin()); n     while ((long long)((s).size()) > k) { n       auto it = --s.end(); n       ans.push_back((*it).second); n       s.erase(it); n     } n     if ((long long)((s).size()) == k) { n       auto it = --s.end(); n       if ((*it).first >= i.second.first) { n         ans.push_back((*it).second); n         s.erase(it); n         s.insert({i.second.first, i.second.second}); n       } else n         ans.push_back(i.second.second); n     } else n       s.insert({i.second.first, i.second.second}); n   } n   cout << (long long)((ans).size()) <<   n ; n   for (auto i : ans) cout << i <<    ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long tc = 1; n   while (tc--) { n     solve(); n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = ~0U >> 1; n const int N = 1005; n int a[N][N]; n int dp[N][N][2]; n int d[] = {2, 5}; n void print(int x, int y, int z) { n   if (x == 0 && y == 0) return; n   if (x == 0 || y && dp[x - 1][y][z] > dp[x][y - 1][z]) { n     print(x, y - 1, z); n     cout <<  R ; n   } else { n     print(x - 1, y, z); n     cout <<  D ; n   } n } n int main() { n   int n; n   cin >> n; n   int zx = -1, zy = -1; n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < n; ++j) { n       cin >> a[i][j]; n       if (a[i][j] == 0) { n         zx = i; n         zy = j; n       } n     } n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < n; ++j) n       for (int k = 0; k < 2; ++k) { n         if (i == 0 && j == 0) n           dp[i][j][k] = 0; n         else if (i == 0) n           dp[i][j][k] = dp[i][j - 1][k]; n         else if (j == 0) n           dp[i][j][k] = dp[i - 1][j][k]; n         else n           dp[i][j][k] = min(dp[i][j - 1][k], dp[i - 1][j][k]); n         if (a[i][j] == 0) dp[i][j][k] = 1; n         for (int x = a[i][j]; x && x % d[k] == 0; x /= d[k]) dp[i][j][k]++; n       } n   int z = dp[n - 1][n - 1][0] > dp[n - 1][n - 1][1]; n   if (zx != -1 && dp[n - 1][n - 1][z] != 0) { n     cout << 1 << endl; n     for (int i = 0; i < zx; i++) cout <<  D ; n     for (int i = 0; i < zy; i++) cout <<  R ; n     for (int i = zx + 1; i < n; i++) cout <<  D ; n     for (int i = zy + 1; i < n; i++) cout <<  R ; n     return 0; n   } n   cout << dp[n - 1][n - 1][z] << endl; n   print(n - 1, n - 1, z); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> a(n + 1); n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   for (int i = 1; i <= n; i++) { n     if (i == a[a[a[i]]]) { n       return cout <<  YES , 0; n     } n   } n   cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n int T, N, P, ap[109][109]; n int main() { n   scanf( %d , &T); n   while (T--) { n     scanf( %d %d , &N, &P), P += 2 * N; n     for (int df = 1; df < N && P; df++) n       for (int i = 0; i < N && P; i++) ap[i][(i + df) % N] = 1, P--; n     for (int i = 0; i < N; i++) n       for (int j = 0; j < N; j++) n         if (ap[i][j]) printf( %d %d n , i + 1, j + 1); n     for (int i = 0; i < N; i++) n       for (int j = 0; j < N; j++) ap[i][j] = 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int main() { n   int s = 0, i; n   cin >> n; n   while (n != 0) { n     s = 0; n     for (i = 0;; ++i) { n       s += (1 << i); n       if (s >= n) break; n     } n     if (s == n) { n       cout << i + 1 <<    ; n       n -= (1 << i); n     } else { n       cout << i + 1 <<    ; n       n -= (1 << i); n     } n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int a1, a2; n   cin >> a1 >> a2; n   int res = 0; n   while (a1 && a2 && (a1 > 1 || a2 > 1)) { n     if (a1 > a2) swap(a1, a2); n     ++a1; n     a2 -= 2; n     ++res; n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1e9 + 7; n long long fact[1100000]; n void find_fact() { n   fact[0] = fact[1] = 1; n   for (int i = 2; i <= 1e6; i++) fact[i] = (i * fact[i - 1]) % mod; n } n bool isvalid(long long num, int num1, int num2) { n   while (num > 0) { n     int cval = (num % 10); n     if ((cval != num1) && (cval != num2)) return 0; n     num = (num / 10); n   } n   return 1; n } n long long my_exp(int base, int exp) { n   if (exp == 0) return 1; n   if (exp == 1) return base; n   long long ans = my_exp(base, exp / 2); n   ans = (ans * ans) % mod; n   if (exp % 2) ans = (ans * base) % mod; n   return ans; n } n long long my_comb(int n, int i) { n   return (fact[n] * my_exp((fact[i] * fact[n - i]) % mod, mod - 2)) % mod; n } n int main() { n   find_fact(); n   int num1, num2, n; n   cin >> num1 >> num2 >> n; n   long long csm, ans = 0; n   for (int i = 0; i <= n; i++) { n     csm = (i * num1 + (n - i) * num2); n     if (isvalid(csm, num1, num2)) ans = (ans + my_comb(n, i)) % mod; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class A, class B> n inline bool mina(A &first, B second) { n   return (first > second) ? (first = second, 1) : 0; n } n template <class A, class B> n inline bool maxa(A &first, B second) { n   return (first < second) ? (first = second, 1) : 0; n } n const int MAXN = 2e5 + 5; n int N, K, A, B, Q; n int total[MAXN]; n class SegmentTree { n  public: n   typedef struct segment_st { n     int numa, numb; n     int l, r; n   } TreeNode; n   vector<TreeNode> tree; n   int N; n   int tree_size; n   vector<int> leaves; n   SegmentTree(int n) { build_tree(n); } n   int query(int ll, int rr, int limit) { return query(1, ll, rr, limit); } n   void update(int at, int val) { internal_update(at, val); } n  n  private: n   void initialize_node(int at) { n     tree[at].numa = 0; n     tree[at].numb = 0; n   } n   void update_up(int at) { n     int left = at + at; n     int right = at + at + 1; n     tree[at].numa = tree[left].numa + tree[right].numa; n     tree[at].numb = tree[left].numb + tree[right].numb; n   } n   void update_result(int &res, int val) { res += val; } n   void update_leaf_value(int at, int &val, int d) { n     tree[at].numa = min(A, total[d]); n     tree[at].numb = min(B, total[d]); n   } n   int query(int at, int ll, int rr, int limit) { n     if (ll > tree[at].r || rr < tree[at].l) return 0; n     if (ll <= tree[at].l && tree[at].r <= rr) { n       if (limit == 0) { n         return tree[at].numb; n       } else { n         return tree[at].numa; n       } n     } else { n       int res = 0; n       update_result(res, query(at + at, ll, rr, limit)); n       update_result(res, query(at + at + 1, ll, rr, limit)); n       return res; n     } n   } n   void internal_update(int at, int &val) { n     int tmp = at; n     at = leaves[at]; n     update_leaf_value(at, val, tmp); n     at >>= 1; n     while (at) { n       update_up(at); n       at >>= 1; n     } n   } n   void build_tree(int n) { n     N = n; n     int base = 1; n     while (base < N) base <<= 1; n     tree_size = base << 1; n     leaves.resize(N + 1); n     tree = vector<TreeNode>(tree_size); n     init(1, 1, n); n   } n   void init(int at, int ll, int rr) { n     tree[at].l = ll; n     tree[at].r = rr; n     initialize_node(at); n     if (ll == rr) { n       leaves[ll] = at; n     } else { n       int mid = (tree[at].l + tree[at].r) >> 1; n       init(at + at, tree[at].l, mid); n       init(at + at + 1, mid + 1, tree[at].r); n       update_up(at); n     } n   } n }; n int main() { n   scanf( %d %d %d %d %d , &N, &K, &A, &B, &Q); n   SegmentTree tree(N); n   int c; n   for (int(i) = 0; (i) < (Q); ++(i)) { n     scanf( %d , &c); n     if (c == 1) { n       int d, a; n       scanf( %d %d , &d, &a); n       total[d] += a; n       tree.update(d, a); n     } else { n       int p; n       scanf( %d , &p); n       int res = 0; n       if (p - 1 >= 1) res += tree.query(1, p - 1, 0); n       p += K; n       if (p <= N) res += tree.query(p, N, 1); n       printf( %d n , res); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b; n   int ans = 0; n   cin >> a >> b; n   ans += a; n   while (a >= b) { n     ans += (a / b); n     a = (a / b) + (a % b); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n long long mx, my, w; n map<double, int> a, b; n int rev(long long x) { n   long long ret = 0; n   while (x) { n     ret *= 10; n     ret += x % 10; n     x /= 10; n   } n   return ret; n } n long long get(long long x, long long y) { n   a.clear(); n   for (long long i = 1; i <= y; ++i) { n     double tmp = 1.0 * rev(i) / i; n     a[tmp]++; n   } n   long long ret = 0; n   for (long long i = 1; i <= x; ++i) { n     double tmp = 1.0 * i / rev(i); n     ret += a[tmp]; n   } n   return ret; n } n int main() { n   cin >> mx >> my >> w; n   if (get(mx, my) < w) { n     puts( -1 ); n     return 0; n   } n   int nx = mx, ny = 1, ax = mx, ay = my; n   long long now = get(nx, ny); n   a.clear(); n   for (long long i = 1; i <= nx; ++i) { n     a[1.0 * i / rev(i)]++; n   } n   b.clear(); n   for (long long i = 1; i <= ny; ++i) { n     b[1.0 * rev(i) / i]++; n   } n   while (nx >= 1 && ny <= my) { n     if (now < w) { n       ny++; n       double tmp = 1.0 * rev(ny) / ny; n       now += a[tmp]; n       b[tmp]++; n     } else { n       if (1ll * nx * ny < 1ll * ax * ay) { n         ax = nx; n         ay = ny; n       } n       double tmp = 1.0 * nx / rev(nx); n       nx--; n       now -= b[tmp]; n       a[tmp]--; n     } n   } n   cout << ax <<     << ay << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   cin >> n; n   int a, b, c; n   cin >> a >> b >> c; n   string s; n   cin >> s; n   vector<int> brr[26]; n   int p1 =  R  -  A ; n   int p2 =  P  -  A ; n   int p3 =  S  -  A ; n   for (int i = 0; i < s.size(); ++i) { n     brr[s[i] -  A ].emplace_back(i); n   } n   string ans; n   for (int i = 0; i < n; ++i) { n     ans +=  * ; n   } n   int cnt = 0; n   while (brr[p1].size()) { n     if (b == 0) break; n     ans[brr[p1].back()] =  P ; n     brr[p1].pop_back(); n     --b; n     ++cnt; n   } n   while (brr[p2].size()) { n     if (c == 0) break; n     ans[brr[p2].back()] =  S ; n     brr[p2].pop_back(); n     --c; n     ++cnt; n   } n   while (brr[p3].size()) { n     if (a == 0) break; n     ans[brr[p3].back()] =  R ; n     brr[p3].pop_back(); n     --a; n     ++cnt; n   } n   int half = ceil(n / 2.0); n   if (cnt >= half) { n     for (int i = 0; i < ans.size(); ++i) { n       if (ans[i] ==  * ) { n         if (a > 0) n           ans[i] =  R , --a; n         else if (b > 0) n           ans[i] =  P , --b; n         else if (c > 0) n           ans[i] =  S , --c; n       } n     } n     assert(a == 0 && b == 0 && c == 0); n     cout <<  YES n  << ans <<   n ; n   } else { n     cout <<  NO n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int T; n   cin >> T; n   while (T--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n int a[30]; n bool mark[N]; n bool dp[30][1000]; n int n; n void update() { n   dp[0][0] = 1; n   for (int i = 1; i <= n; i++) n     for (int j = 0; j < 1000; j++) { n       dp[i][j] = dp[i - 1][j]; n       if (j >= a[i] && !mark[i]) dp[i][j] |= dp[i - 1][j - a[i]]; n     } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   sort(a + 1, a + n + 1); n   update(); n   int k = n; n   for (int i = 1; i <= n; i++) { n     int t = a[i] - 1; n     for (int j = i - 1; j >= 1; j--) { n       if (a[j] != a[i] - 1 && a[j] <= t && !mark[j] && dp[j - 1][t - a[j]]) { n         update(); n         mark[j] = 1; n         t -= a[j]; n         k--; n       } n     } n     if (t) k = 200; n   } n   if (k == 1) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n const int MAXN = 100 * 1000; n int a[MAXN + 10]; n int dp[MAXN + 10]; n int mx = 0; n using namespace std; n int main() { n   long long n, m, k, s = 0; n   cin >> k >> n >> m; n   long long shif = (k - (n % k)) % k; n   n = n + shif; n   if (m < n) n     cout << 0 << endl; n   else n     cout << (m - n) / k + 1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int a[(1 << 17) + 1]; n int tree[4 * (1 << 17)]; n void build(int nd, int s, int e, bool ex) { n   if (s == e) { n     tree[nd] = a[s]; n     return; n   } n   int mid = (s + e) / 2; n   int left = 2 * nd; n   int right = 2 * nd + 1; n   build(left, s, mid, !ex); n   build(right, mid + 1, e, !ex); n   if (ex) { n     tree[nd] = tree[left] ^ tree[right]; n   } else { n     tree[nd] = tree[left] | tree[right]; n   } n } n void update(int nd, int s, int e, int p, int v, bool ex) { n   if (s == e && s == p) { n     tree[nd] = v; n     return; n   } n   if (e < p || s > p) return; n   int mid = (s + e) / 2; n   int left = 2 * nd; n   int right = 2 * nd + 1; n   update(left, s, mid, p, v, !ex); n   update(right, mid + 1, e, p, v, !ex); n   if (ex) { n     tree[nd] = tree[left] ^ tree[right]; n   } else { n     tree[nd] = tree[left] | tree[right]; n   } n } n int main() { n   int n, m, total; n   cin >> n >> m; n   total = (1 << n); n   for (int i = 1; i <= total; i++) { n     scanf( %d , a + i); n   } n   build(1, 1, total, !(n & 1)); n   int p, v; n   while (m--) { n     scanf( %d %d , &p, &v); n     update(1, 1, total, p, v, !(n & 1)); n     cout << tree[1] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n string s; n queue<int> q[2]; n int main() { n   ios_base::sync_with_stdio(); n   cin.tie(); n   cin >> n >> s; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  D ) { n       q[0].push(i); n     } n     if (s[i] ==  R ) { n       q[1].push(i); n     } n   } n   while (q[1].size() > 0 && q[0].size() > 0) { n     int x = q[0].front(); n     int y = q[1].front(); n     if (x < y) { n       q[1].pop(); n       q[0].pop(); n       q[0].push(x + n); n     } else { n       q[0].pop(); n       q[1].pop(); n       q[1].push(y + n); n     } n   } n   if (q[0].size()) { n     cout <<  D n ; n   } else { n     cout <<  R n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, q; n pair<int, int> t[300100]; n pair<int, int> up[300100]; n pair<int, int> down[300100]; n pair<int, int> inter(pair<int, int> a, pair<int, int> b) { n   int f = max(a.first, b.first); n   int s = min(a.second, b.second); n   pair<int, int> res = {f, s}; n   return res; n } n long long int len(pair<int, int> a) { n   int res = a.second - a.first; n   if (res < 0) res = 0; n   return res; n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> t[i].first >> t[i].second; n   down[0] = t[0]; n   up[n - 1] = t[n - 1]; n   for (int i = 0; i < n - 1; i++) down[i + 1] = inter(down[i], t[i + 1]); n   for (int i = n - 1; i > 0; i--) up[i - 1] = inter(up[i], t[i - 1]); n   long long int ma = -1; n   ma = max(ma, len(up[1])); n   ma = max(ma, len(down[n - 2])); n   for (int i = 1; i < n - 1; i++) { n     long long int a = len(inter(down[i - 1], up[i + 1])); n     ma = max(ma, a); n   } n   cout << ma; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int min(int x, int y) { return x < y ? x : y; } n int max(int x, int y) { return x > y ? x : y; } n void swap(int &x, int &y) { n   int t; n   t = x, x = y, y = t; n } n char s[200000]; n int main() { n   int x, y, i, j, n, k, t; n   while (~scanf( %d%d , &n, &k)) { n     if ((k + n - 1) / n == 2) { n       printf( %d n , n); n       continue; n     } n     for (i = 0;; i++) { n       t = k - i * 2; n       if (t == 0) break; n       if (n - i <= 0) { n         i--; n         break; n       } n       if ((t) / (n - i) > 2) { n         break; n       } n     } n     printf( %d n , i); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const ll inf = INT_MAX - 1; n bool check(ll r, ll g, ll b, ll w, ll total) { n   if (r < 0 || g < 0 || b < 0 || w < 0) { n     return false; n   } n   ll numberIsOne = 0; n   if (r & 1) { n     numberIsOne++; n   } n   if (g & 1) { n     numberIsOne++; n   } n   if (b & 1) { n     numberIsOne++; n   } n   if (w & 1) { n     numberIsOne++; n   } n   if (numberIsOne == 0 && total % 2 == 0) { n     return true; n   } n   if (total % 2 == 1 && numberIsOne == 1) { n     return true; n   } n } n void solve() { n   ll r, g, b, w; n   cin >> r >> g >> b >> w; n   ll total = r + g + b + w; n   ll minNum = min(r, min(g, b)); n   if (check(r, g, b, w, total)) { n     cout <<  Yes n ; n     return; n   } n   if (check(r - 1, g - 1, b - 1, w + 3, total)) { n     cout <<  Yes n ; n     return; n   } n   if (check(r - 2, g - 2, b - 2, w + 6, total)) { n     cout <<  Yes n ; n     return; n   } n   if (check(r - minNum, g - minNum, b - minNum, w + minNum * 3, total)) { n     cout <<  Yes n ; n     return; n   } n   cout <<  No n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   ll t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-7; n const long long INFF = 1e18; n const int INF = 1e9; n const int mod = 1e9 + 7; n const int MAX = 2e7 + 7; n vector<long long> st1, st2; n int main(void) { n   long long a, b; n   scanf( %lld %lld , &a, &b); n   ; n   long long c = a + b; n   for (long long i = 1; i <= MAX; ++i) { n     if (i * i > a) break; n     if (a % i == 0) st1.push_back(i); n   } n   int tp = ((int)st1.size()) - 1; n   for (int i = tp; i >= 0; --i) st1.push_back(a / st1[i]); n   for (long long i = 1; i <= MAX; ++i) { n     if (i * i > b) break; n     if (b % i == 0) st2.push_back(i); n   } n   tp = ((int)st2.size()) - 1; n   for (int i = tp; i >= 0; --i) st2.push_back(b / st2[i]); n   long long ans = INFF; n   for (long long i = 1; i <= MAX; ++i) { n     if (i * i > c) break; n     if (c % i == 0) { n       bool ok = 0; n       long long n = i, m = c / i; n       long long n1 = *(--upper_bound((st1).begin(), (st1).end(), n)); n       if (m >= a / n1) ok = 1; n       n1 = *(--upper_bound((st1).begin(), (st1).end(), m)); n       if (n >= a / n1) ok = 1; n       n1 = *(--upper_bound((st2).begin(), (st2).end(), n)); n       if (m >= b / n1) ok = 1; n       n1 = *(--upper_bound((st2).begin(), (st2).end(), m)); n       if (n >= b / n1) ok = 1; n       if (ok) ans = min(ans, 2 * (n + m)); n     } n   } n   printf( %lld n , 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--) { n     int n; n     cin >> n; n     string second; n     cin >> second; n     int mi = 0, gr = 0, le = 0, ans = 0; n     for (int i = 0; i < second.length(); i++) { n       if (second[i] ==  - ) n         mi++; n       else if (second[i] ==  > ) n         gr++; n       else n         le++; n     } n     if (mi == n || gr == n || le == n) { n       cout << n << endl; n       continue; n     } n     if (mi == 0 && gr && le) { n       cout << 0 << endl; n       continue; n     } n     if ((mi && gr && !le) || (mi && le && !gr)) { n       cout << n << endl; n       continue; n     } n     for (int i = 0; i < second.length(); i++) { n       if (second[i] ==  -  || second[(i - 1 + n) % n] ==  - ) ans++; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e5 + 7; n const int MOD = 1e9 + 7; n int a[MAX], h[MAX], c[MAX] = {0}; n int main() { n   int n, m, i, j, k, q; n   scanf( %d %d %d , &n, &k, &m); n   for (i = 0; i < n; i++) scanf( %d , &a[i]); n   for (i = 0; i < n; i++) { n     h[i] = a[i] % m; n     c[h[i]]++; n   } n   for (i = 0; i < m; i++) n     if (c[i] >= k) break; n   if (i == m) { n     printf( No n ); n     return 0; n   } n   printf( Yes n ); n   j = i, q = 0; n   for (i = 0; i < n && q < k; i++) { n     if (h[i] == j) printf( %d  , a[i]), q++; n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long logN = 20; n const long long M = 1000000007; n const long long INF = 1e12; n const long long N = 100005; n const long int maxi = 1000000; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, m, h1, i, j; n   cin >> n >> m >> h1; n   long long a[m]; n   for (i = 0; i < m; i++) { n     cin >> a[i]; n   } n   long long b[n]; n   for (i = 0; i < n; i++) { n     cin >> b[i]; n   } n   long long h[n][m], ans[n][m]; n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) { n       cin >> h[i][j]; n       ans[i][j] = 0; n     } n   } n   for (i = 0; i < m; i++) { n     if (a[i] > 0) { n       for (j = 0; j < n; j++) { n         if (h[j][i] == 1 && a[i] <= b[j]) { n           ans[j][i] = a[i]; n           break; n         } n       } n     } n   } n   for (i = 0; i < n; i++) { n     if (b[i] > 0) { n       for (j = 0; j < m; j++) { n         if (h[i][j] == 1 && ans[i][j] == 0 && b[i] <= a[j]) { n           ans[i][j] = b[i]; n           break; n         } n       } n     } n   } n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) { n       cout << max(ans[i][j], h[i][j]) <<    ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline int solve(int w, int h, int S) { n   if (S == (2 * w + 1) * (2 * h + 1)) return 2 * (w + 1) * (h + 1) - 1; n   if (S > (2 * w + 1) * (2 * h + 1)) return 0; n   S = (2 * w + 1) * (2 * h + 1) - S; n   if (S & 3) return 0; n   S /= 4; n   int res = 0; n   for (int w2 = 0; w2 < w; w2++) { n     if (S % (w - w2)) continue; n     int h2 = h - S / (w - w2); n     if (h2 < 0 || h2 >= h) continue; n     res++; n   } n   return 2 * res; n } n int main() { n   int W, H, s; n   while (scanf( %d%d%d , &W, &H, &s) >= 1) { n     long long ans = 0; n     for (int w = 0; 2 * w + 1 <= W; w++) { n       for (int h = 0; 2 * h + 1 <= H; h++) { n         ans += solve(w, h, s) * (long long)(W - (2 * w + 1) + 1) * n                (H - (2 * h + 1) + 1); n       } n     } n     printf( n          %lld  n           n , n         ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n; n   cin >> n; n   long long int max1 = 0, max2 = 0, min1 = (1000000000 + 9), n                 min2 = (1000000000 + 9); n   for (int i = 0; i < n; i++) { n     long long int x, y; n     cin >> x >> y; n     if (y < min1) { n       min1 = y; n     } n     if (x > max1) { n       max1 = x; n     } n   } n   long long int m; n   cin >> m; n   for (int i = 0; i < m; i++) { n     long long int x, y; n     cin >> x >> y; n     if (y < min2) { n       min2 = y; n     } n     if (x > max2) { n       max2 = x; n     } n   } n   long long int x1 = (max1 - min2); n   long long int x2 = (max2 - min1); n   long long int x3 = max(x1, x2); n   if (x3 < 0) { n     cout << 0 << endl; n   } else { n     cout << x3 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool debug = true; n const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1, 0}; n const int dy[] = {1, 0, -1, 0, -1, 1, 1, -1, 0}; n template <typename X> n inline X square(const X& a) { n   return a * a; n } n int scan_d() { n   bool minus = false; n   int result = 0; n   char ch; n   ch = getchar(); n   while (true) { n     if (ch ==  - ) break; n     if (ch >=  0  && ch <=  9 ) break; n     ch = getchar(); n   } n   if (ch ==  - ) n     minus = true; n   else n     result = ch -  0 ; n   while (true) { n     ch = getchar(); n     if (ch <  0  || ch >  9 ) break; n     result = (result << 3) + (result << 1) + (ch -  0 ); n   } n   if (minus) n     return -result; n   else n     return result; n } n long scan_ld() { n   bool minus = false; n   long result = 0; n   char ch; n   ch = getchar(); n   while (true) { n     if (ch ==  - ) break; n     if (ch >=  0  && ch <=  9 ) break; n     ch = getchar(); n   } n   if (ch ==  - ) n     minus = true; n   else n     result = ch -  0 ; n   while (true) { n     ch = getchar(); n     if (ch <  0  || ch >  9 ) break; n     result = (result << 3) + (result << 1) + (ch -  0 ); n   } n   if (minus) n     return -result; n   else n     return result; n } n long long scan_lld() { n   bool minus = false; n   long long result = 0; n   char ch; n   ch = getchar(); n   while (true) { n     if (ch ==  - ) break; n     if (ch >=  0  && ch <=  9 ) break; n     ch = getchar(); n   } n   if (ch ==  - ) n     minus = true; n   else n     result = ch -  0 ; n   while (true) { n     ch = getchar(); n     if (ch <  0  || ch >  9 ) break; n     result = (result << 3) + (result << 1) + (ch -  0 ); n   } n   if (minus) n     return -result; n   else n     return result; n } n long long unsigned scan_llu() { n   long long unsigned result = 0; n   char ch; n   ch = getchar(); n   while (true) { n     if (ch ==  - ) break; n     if (ch >=  0  && ch <=  9 ) break; n     ch = getchar(); n   } n   result = ch -  0 ; n   while (true) { n     ch = getchar(); n     if (ch <  0  || ch >  9 ) break; n     result = (result << 3) + (result << 1) + (ch -  0 ); n   } n   return result; n } n void print_d(int n) { n   if (n < 0) { n     n = -n; n     putchar( - ); n   } n   int i = 10; n   char output_buffer[10]; n   do { n     output_buffer[--i] = (n % 10) +  0 ; n     n /= 10; n   } while (n); n   do { n     putchar(output_buffer[i]); n   } while (++i < 10); n } n void print_ld(long n) { n   if (n < 0) { n     n = -n; n     putchar( - ); n   } n   int i = 11; n   char output_buffer[11]; n   do { n     output_buffer[--i] = (n % 10) +  0 ; n     n /= 10; n   } while (n); n   do { n     putchar(output_buffer[i]); n   } while (++i < 11); n } n void print_lld(long long n) { n   if (n < 0) { n     n = -n; n     putchar( - ); n   } n   int i = 21; n   char output_buffer[21]; n   do { n     output_buffer[--i] = (n % 10) +  0 ; n     n /= 10; n   } while (n); n   do { n     putchar(output_buffer[i]); n   } while (++i < 21); n } n void print_llu(long long unsigned n) { n   int i = 21; n   char output_buffer[21]; n   do { n     output_buffer[--i] = (n % 10) +  0 ; n     n /= 10; n   } while (n); n   do { n     putchar(output_buffer[i]); n   } while (++i < 21); n } n void println_d(int n) { n   if (n < 0) { n     n = -n; n     putchar( - ); n   } n   int i = 10; n   char output_buffer[11]; n   output_buffer[10] =   n ; n   do { n     output_buffer[--i] = (n % 10) +  0 ; n     n /= 10; n   } while (n); n   do { n     putchar(output_buffer[i]); n   } while (++i < 11); n } n void println_ld(long n) { n   if (n < 0) { n     n = -n; n     putchar( - ); n   } n   int i = 11; n   char output_buffer[12]; n   output_buffer[11] =   n ; n   do { n     output_buffer[--i] = (n % 10) +  0 ; n     n /= 10; n   } while (n); n   do { n     putchar(output_buffer[i]); n   } while (++i < 12); n } n void println_lld(long long n) { n   if (n < 0) { n     n = -n; n     putchar( - ); n   } n   int i = 21; n   char output_buffer[22]; n   output_buffer[21] =   n ; n   do { n     output_buffer[--i] = (n % 10) +  0 ; n     n /= 10; n   } while (n); n   do { n     putchar(output_buffer[i]); n   } while (++i < 22); n } n void println_llu(long long unsigned n) { n   int i = 21; n   char output_buffer[22]; n   output_buffer[21] =   n ; n   do { n     output_buffer[--i] = (n % 10) +  0 ; n     n /= 10; n   } while (n); n   do { n     putchar(output_buffer[i]); n   } while (++i < 22); n } n char sp; n void printsp_d(int n) { n   if (n < 0) { n     n = -n; n     putchar( - ); n   } n   int i = 10; n   char output_buffer[11]; n   output_buffer[10] = sp; n   do { n     output_buffer[--i] = (n % 10) +  0 ; n     n /= 10; n   } while (n); n   do { n     putchar(output_buffer[i]); n   } while (++i < 11); n } n void printsp_ld(long n) { n   if (n < 0) { n     n = -n; n     putchar( - ); n   } n   int i = 11; n   char output_buffer[12]; n   output_buffer[11] = sp; n   do { n     output_buffer[--i] = (n % 10) +  0 ; n     n /= 10; n   } while (n); n   do { n     putchar(output_buffer[i]); n   } while (++i < 12); n } n void printsp_lld(long long n) { n   if (n < 0) { n     n = -n; n     putchar( - ); n   } n   int i = 21; n   char output_buffer[22]; n   output_buffer[21] = sp; n   do { n     output_buffer[--i] = (n % 10) +  0 ; n     n /= 10; n   } while (n); n   do { n     putchar(output_buffer[i]); n   } while (++i < 22); n } n void printsp_llu(long long unsigned n) { n   int i = 21; n   char output_buffer[22]; n   output_buffer[21] = sp; n   do { n     output_buffer[--i] = (n % 10) +  0 ; n     n /= 10; n   } while (n); n   do { n     putchar(output_buffer[i]); n   } while (++i < 22); n } n long long cal[100005][15], dp[105][105][15]; n vector<pair<int, int> > ind; n int main() { n   int n, q, c, j, i, x, y, x1, x2, y1, y2, t; n   long long ans = 0; n   n = scan_d(); n   q = scan_d(); n   c = scan_d(); n   for (i = 0; i < n; i++) { n     x = scan_d(); n     y = scan_d(); n     t = scan_d(); n     cal[i][0] = t; n     dp[x][y][0] += t; n     ind.push_back(make_pair(x, y)); n   } n   for (i = 1; i <= c + 3; i++) { n     for (j = 0; j < n; j++) { n       cal[j][i] = (cal[j][i - 1] + 1) % (c + 1); n       dp[ind[j].first][ind[j].second][i] += (cal[j][i]); n     } n   } n   for (t = 0; t <= c + 3; t++) { n     for (i = 1; i <= 100; i++) { n       for (j = 1; j <= 100; j++) { n         dp[i][j][t] += dp[i][j - 1][t]; n       } n     } n   } n   while (q--) { n     ans = 0; n     t = scan_d(); n     x1 = scan_d(); n     y1 = scan_d(); n     x2 = scan_d(); n     y2 = scan_d(); n     if (y1 > y2) swap(y1, y2); n     if (x1 > x2) swap(x1, x2); n     t %= (c + 1); n     for (i = x1; i <= x2; i++) { n       ans += (dp[i][y2][t] - dp[i][y1 - 1][t]); n     } n     println_lld(ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma warning(disable : 4996) n #pragma comment(linker,  /STACK:20000007 ) n #pragma GCC optimize( unroll-loops ) n const int N = (int)1e6 + 100; n const int maxn = (int)1e3 + 100; n const int base = (int)1e9; n const int mod = (int)1e9 + 7; n const int inf = INT_MAX; n const long long ll_inf = LLONG_MAX; n const long double PI = acos((long double)-1.0); n const long double eps = 1e-8; n void solve() { n   int n, s; n   scanf( %d%d , &n, &s); n   vector<int> P(n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &P[i]); n   } n   multiset<int> S; n   for (int i = 0; i < n; ++i) { n     S.insert(P[i]); n   } n   map<int, int> C; n   for (int i = 0; i < n; ++i) { n     C[P[i]]++; n   } n   int ans = 0; n   --s; n   if (P[s]) { n     C[P[s]]--; n     S.erase(S.find(P[s])); n     C[0]++; n     ++ans; n     P[s] = 0; n   } n   ans += C[0] - 1; n   int have = C[0] - 1; n   S.erase(0); n   int now = 1; n   while (!S.empty() && now <= *(--S.end())) { n     if (S.find(now) != S.end()) n       ; n     else if (have) n       --have; n     else { n       ++ans; n       S.erase(--S.end()); n     } n     ++now; n   } n   printf( %d n , ans); n } n int main() { n   int T = 1; n   for (; T; --T) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int det(long long int a, long long int b, int bit) { n   if (!bit) { n     if (a & 1) return a; n     if (b & 1) return b; n     return a; n   } n   if (a & (1ll << bit)) return det(a, b, bit - 1); n   if (!(b & (1ll << bit))) return det(a, b, bit - 1); n   if ((b & ((1ll << (bit + 1)) - 1)) == ((1ll << (bit + 1)) - 1)) return b; n   return ((a & (~((1ll << bit) - 1))) | ((1ll << bit) - 1)); n } n int main() { n   int n = 0; n   cin >> n; n   long long int a = 0, b = 0; n   while (n--) { n     cin >> a >> b; n     cout << det(a, b, 60) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1005; n int n, m, b[N][N], a[N][N]; n void solve() { n   if (n % 2) { n     printf( %d n , n); n     return; n   } n   bool ok = 1; n   while (n > 1 && ok && n % 2 == 0) { n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         if (b[i][j] != b[n - i - 1][j]) { n           ok = 0; n           break; n         } n       } n     } n     if (ok) n /= 2; n   } n   printf( %d n , n); n } n int main() { n   ios::sync_with_stdio(false); n   scanf( %d %d , &n, &m); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) scanf( %d , &b[i][j]); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = (int)1e9; n const int mod = inf + 7; n const double eps = 1e-9; n const double pi = acos(-1.0); n int n, t; n int a[100100], cur = 0, all; n int binpow(int a, int k) { n   if (k == 0) return 1; n   int ans = binpow(a, k / 2); n   ans = 1ll * ans * ans % mod; n   if (k % 2) ans = 1ll * ans * a % mod; n   return ans; n } n int main() { n   cin >> n; n   int bad = 0; n   for (int i = 1; i < 4; i++) { n     for (int j = 1; j < 4; j++) { n       for (int k = 1; k < 4; k++) { n         if (i + j + k == 6) bad++; n       } n     } n   } n   bad = binpow(bad, n); n   all = binpow(3, 3 * n); n   all -= bad; n   all %= mod; n   all += mod; n   all %= mod; n   cout << all << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long ceilVal(long long a, long long b) { n   long long sum = a / b; n   if (a % b) return ++sum; n   return sum; n } n int main() { n   long long a, n, m; n   scanf( %lld%lld%lld , &n, &m, &a); n   long long sum = 0; n   sum = ceilVal(n, a) * ceilVal(m, a); n   printf( %lld , sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void p(vector<int> a) { n   int x = 0; n   for (int y : a) { n     if (x) printf(   ); n     printf( %d , y); n     x = 1; n   } n   puts(  ); n } n int main() { n   int n; n   cin >> n; n   if (n % 2 == 0) return !printf( -1 n ); n   vector<int> a, b, c; n   for (int i = 0; i < n; ++i) a.emplace_back(i), b.emplace_back(i); n   for (int i = 0; i < n; ++i) c.emplace_back((a[i] + b[i]) % n); n   p(a); n   p(b); n   p(c); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x, y, sum = 0; n   cin >> n >> x >> y; n   sum = (n * y) / 100; n   if ((n * y) % 100 != 0) { n     sum += 1; n   } n   if (sum <= x) n     cout << 0; n   else n     cout << abs(sum - x); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int n; n   cin >> n; n   if (n & 1) n     cout << n / 2 <<   n ; n   else { n     long long int p = 1; n     for (long long int i = 1; 2 * p <= n; i++) p = 1 << i; n     cout << (n - p) / 2 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T gcd(T a, T b) { n   while (b > 0) { n     a %= b; n     swap(a, b); n   } n   return a; n } n template <class _T> n inline _T sqr(const _T& first) { n   return first * first; n } n template <class _T> n inline string tostr(const _T& a) { n   ostringstream os(  ); n   os << a; n   return os.str(); n } n const long double PI = 3.1415926535897932384626433832795L; n const double EPS = 1e-9; n char TEMPORARY_CHAR; n const int INF = 1e9; n inline void input(int& a) { n   a = 0; n   while (((TEMPORARY_CHAR = getchar()) >  9  || TEMPORARY_CHAR <  0 ) && n          (TEMPORARY_CHAR !=  - )) { n   } n   char neg = 0; n   if (TEMPORARY_CHAR ==  - ) { n     neg = 1; n     TEMPORARY_CHAR = getchar(); n   } n   while (TEMPORARY_CHAR <=  9  && TEMPORARY_CHAR >=  0 ) { n     a = 10 * a + TEMPORARY_CHAR -  0 ; n     TEMPORARY_CHAR = getchar(); n   } n   if (neg) a = -a; n } n inline void out(long long a) { n   if (!a) putchar( 0 ); n   if (a < 0) { n     putchar( - ); n     a = -a; n   } n   char s[20]; n   int i; n   for (i = 0; a; ++i) { n     s[i] =  0  + a % 10; n     a /= 10; n   } n   for (int j = (i)-1; j >= 0; j--) putchar(s[j]); n } n inline int nxt() { n   int(ret); n   input((ret)); n   ; n   return ret; n } n const int MAXN = 300000; n long long powers[MAXN + 1]; n struct data { n   int len; n   long long data1; n   long long data2; n }; n data merge(const data& l, const data& r) { n   data res; n   res.len = l.len + r.len; n   res.data1 = r.data1 + l.data1 * powers[r.len]; n   res.data2 = l.data2 + r.data2 * powers[l.len]; n   return res; n } n int main() { n   int(n); n   input((n)); n   ; n   int first[n]; n   powers[0] = 1; n   for (int i = 1; i <= n; ++i) { n     powers[i] = powers[i - 1] * 3; n   } n   for (int i = 0; i < n; ++i) { n     first[i] = nxt() - 1; n   } n   data t[n + n]; n   for (int i = 0; i < n; ++i) { n     t[i + n].len = 1; n     t[i + n].data1 = t[i + n].data2 = 0; n   } n   for (int i = n - 1; i > 0; --i) { n     t[i] = merge(t[i + i], t[i + i + 1]); n   } n   for (int i = 0; i < n; ++i) { n     t[first[i] + n].data1 = t[first[i] + n].data2 = 1; n     int pos = first[i] + n; n     for (pos >>= 1; pos; pos >>= 1) { n       t[pos] = merge(t[pos + pos], t[pos + pos + 1]); n     } n     int r = min(first[i], n - first[i] - 1); n     data L{0, 0, 0}; n     data R{0, 0, 0}; n     int a = first[i] - r, b = first[i] + r; n     for (a += n, b += n; a <= b; a = (a + 1) >> 1, b = (b - 1) >> 1) { n       if (a & 1) { n         L = merge(L, t[a]); n       } n       if (!(b & 1)) { n         R = merge(t[b], R); n       } n     } n     data res = merge(L, R); n     if (res.data1 != res.data2) { n       puts( YES ); n       return 0; n     } n   } n   puts( NO ); n   return 0; n } n