#include <bits/stdc++.h> n using namespace std; n vector<pair<long long, long long> > bl; n long long fact[200005]; n long long ifact[200005]; n long long invert_mod(long long a, long long p) { n   long long n = 1, old = 0, q = p, r, h; n   bool pos = false; n   while (a > 0) { n     r = q % a; n     q = q / a; n     h = q * n + old; n     old = n; n     n = h; n     q = a; n     a = r; n     pos = !pos; n   } n   return pos ? old : (p - old); n } n void calc_fact() { n   fact[0] = fact[1] = 1; n   ifact[0] = ifact[1] = 1; n   for (long long i = 2; i < 200005; i++) { n     fact[i] = (i * fact[i - 1]) % 1000000007; n     ifact[i] = invert_mod(fact[i], 1000000007) % 1000000007; n   } n } n int main(void) { n   ios_base::sync_with_stdio(false); n   long long h, w, n; n   cin >> h >> w >> n; n   calc_fact(); n   bl.resize(n); n   for (int i = 0; i < n; i++) { n     cin >> bl[i].first >> bl[i].second; n   } n   bl.push_back(make_pair(h, w)); n   vector<long long> dp(n + 1, 0); n   sort(bl.begin(), bl.end()); n   dp[0] = ((fact[bl[0].first + bl[0].second - 2] * ifact[bl[0].first - 1]) % n            1000000007 * ifact[bl[0].second - 1]) % n           1000000007; n   for (int i = 1; i <= n; i++) { n     dp[i] = ((fact[bl[i].first + bl[i].second - 2] * ifact[bl[i].first - 1]) % n              1000000007 * ifact[bl[i].second - 1]) % n             1000000007; n     long long ans = 0; n     for (int j = 0; j < i; j++) { n       long long val; n       if (bl[i].second >= bl[j].second && bl[i].first >= bl[j].first) n         val = ((fact[(bl[i].first + bl[i].second) - n                      (bl[j].first + bl[j].second)] * n                 ifact[bl[i].first - bl[j].first]) % n                1000000007 * ifact[bl[i].second - bl[j].second]) % n               1000000007; n       else n         val = 0; n       ans = (ans + (dp[j] * val) % 1000000007) % 1000000007; n     } n     dp[i] = (dp[i] + (1000000007 - ans)) % 1000000007; n   } n   cout << dp[n]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 300110; n const int mod = 1e9 + 9; n int n, m; n int sumv[maxn * 4]; n int f1[maxn * 4]; n int f2[maxn * 4]; n int num[maxn]; n int sum[maxn]; n int f[maxn]; n int addv(int a, int b, int n) { n   return ((((long long)f[n + 1] * (long long)b) % mod + n            ((long long)f[n] * (long long)a) % mod) % n               mod + n           (mod - b) % mod) % n          mod; n } n int get(int a, int b, int n) { n   return ((long long)f[n - 1] * (long long)b + n           ((long long)f[n - 2] * (long long)a) % mod) % n          mod; n } n void maintain(int o, int l, int r) { n   int lc = o * 2, rc = lc + 1; n   sumv[o] = 0; n   if (r > l) sumv[o] = (sumv[lc] + sumv[rc]) % mod; n   sumv[o] = n       ((sumv[o] + ((long long)f[r - l + 2] * (long long)(f2[o])) % mod) % mod + n        ((((long long)f[r - l + 1] * (long long)(f1[o])) % mod + n          (mod - f2[o]) % mod) % n         mod)) % n       mod; n } n void update(int o, int l, int r, int yl, int yr) { n   int lc = o * 2, rc = lc + 1; n   int m = l + (r - l) / 2; n   if (yl <= l && yr >= r) { n     f1[o] = (f1[o] + f[l - yl + 1]) % mod; n     f2[o] = (f2[o] + f[l - yl + 2]) % mod; n   } else { n     if (yl <= m) update(lc, l, m, yl, yr); n     if (yr > m) update(rc, m + 1, r, yl, yr); n   } n   maintain(o, l, r); n } n int _sum; n void query(int o, int l, int r, int ql, int qr, int add_f1, int add_f2) { n   int lc = o * 2, rc = lc + 1; n   int m = l + (r - l) / 2; n   if (ql <= l && qr >= r) { n     _sum = ((_sum + sumv[o]) % mod + addv(add_f1, add_f2, r - l + 1)) % mod; n   } else { n     if (ql <= m) n       query(lc, l, m, ql, qr, (add_f1 + f1[o]) % mod, (add_f2 + f2[o]) % mod); n     if (qr > m) n       query(rc, m + 1, r, ql, qr, n             get((add_f1 + f1[o]) % mod, (add_f2 + f2[o]) % mod, m + 2 - l), n             get((add_f1 + f1[o]) % mod, (add_f2 + f2[o]) % mod, m + 3 - l)); n   } n } n int main() { n   memset(sumv, 0, sizeof(sumv)); n   memset(f1, 0, sizeof(f1)); n   memset(f2, 0, sizeof(f2)); n   f[0] = 0; n   f[1] = 1; n   f[2] = 1; n   for (int i = 3; i < maxn; ++i) f[i] = (f[i - 1] + f[i - 2]) % mod; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) scanf( %d , &num[i]); n   sum[0] = 0; n   for (int i = 1; i <= n; ++i) sum[i] = (sum[i - 1] + num[i]) % mod; n   int x, y, z; n   for (int i = 0; i < m; ++i) { n     scanf( %d%d%d , &x, &y, &z); n     if (x == 1) n       update(1, 1, n, y, z); n     else { n       _sum = 0; n       query(1, 1, n, y, z, 0, 0); n       _sum = (_sum + (sum[z] - sum[y - 1] + mod) % mod) % mod; n       printf( %d n , _sum); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a; n int main() { n   int b; n   int k; n   int s; n   cin >> b; n   cin >> k; n   for (int i = 1; i <= k; i++) { n     cin >> a; n     s += a; n   } n   if (b % 2 == 0) n     if (a % 2 == 0) { n       cout <<  even ; n     } else { n       cout <<  odd ; n     } n   if (b % 2 == 1) n     if (s % 2 == 0) { n       cout <<  even ; n     } else { n       cout <<  odd ; n     } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fast() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n } n long long dotproduct(vector<long long>& a, vector<long long>& b) { n   long long sum = 0; n   for (long long i = 0; i < a.size(); i += 1) { n     sum += a[i] * b[i]; n   } n   return sum; n } n int main() { n   fast(); n   long long px, py, pz; n   long long x, y, z; n   cin >> px >> py >> pz >> x >> y >> z; n   px *= 2; n   py *= 2; n   pz *= 2; n   x *= 2; n   y *= 2; n   z *= 2; n   long long a1, a2, a3, a4, a5, a6; n   cin >> a1 >> a2 >> a3 >> a4 >> a5 >> a6; n   long long res = 0; n   vector<long long> zoxnv = {0, -1, 0}; n   vector<long long> zox2nv = {0, 1, 0}; n   vector<long long> xoynv = {0, 0, -1}; n   vector<long long> xoy2nv = {0, 0, 1}; n   vector<long long> yoznv = {-1, 0, 0}; n   vector<long long> yoz2nv = {1, 0, 0}; n   vector<long long> seezox = {px - (x / 2), py, pz - (z / 2)}; n   if (dotproduct(seezox, zoxnv) > 0) { n     res += a1; n   } n   vector<long long> seezox2 = {px - (x / 2), py - y, pz - (z / 2)}; n   if (dotproduct(seezox2, zox2nv) > 0) { n     res += a2; n   } n   vector<long long> seexoy = {px - (x / 2), py - (y / 2), pz}; n   if (dotproduct(seexoy, xoynv) > 0) { n     res += a3; n   } n   vector<long long> seexoy2 = {px - (x / 2), py - (y / 2), pz - z}; n   if (dotproduct(seexoy2, xoy2nv) > 0) { n     res += a4; n   } n   vector<long long> seeyoz = {px, py - (y / 2), pz - (z / 2)}; n   if (dotproduct(seeyoz, yoznv) > 0) { n     res += a5; n   } n   vector<long long> seeyoz2 = {px - x, py - (y / 2), pz - (z / 2)}; n   if (dotproduct(seeyoz2, yoz2nv) > 0) { n     res += a6; n   } n   cout << res; n } n


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


#include <bits/stdc++.h> n using namespace std; n int first(int a) { n   int x = 1; n   while (a) { n     if (a % 10) x *= a % 10; n     a /= 10; n   } n   return x; n } n int main() { n   int q; n   cin >> q; n   vector<vector<int> > m(int(1e6 + 1)); n   for (int i = 0; i < int(1e6 + 1); ++i) m[i].resize(10); n   vector<int> par(int(1e6 + 1)); n   for (int i = 1; i < int(1e6 + 1); ++i) { n     int z = first(i); n     while (z >= 10) { n       z = par[z]; n     } n     par[i] = z; n     m[i][z]++; n     for (int j = 0; j < 10; ++j) { n       m[i][j] += m[i - 1][j]; n     } n   } n   while (q--) { n     int l, r, k; n     cin >> l >> r >> k; n     cout << m[r][k] - m[l - 1][k] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int Max(int a, int b) { return a > b ? a : b; } n int Min(int a, int b) { return a < b ? a : b; } n char s[10][10]; n int n, k; n int a[10], mi, ma, ans; n int flag[10]; n void dfs(int t) { n   int i, j; n   if (t == k) { n     mi = ma = a[0]; n     for (i = 1; i < n; ++i) { n       mi = Min(mi, a[i]); n       ma = Max(ma, a[i]); n     } n     ans = Min(ans, ma - mi); n     return; n   } n   for (i = 0; i < k; ++i) { n     if (flag[i]) { n       flag[i] = 0; n       for (j = 0; j < n; ++j) { n         a[j] *= 10; n         a[j] += s[j][i] -  0 ; n       } n       dfs(t + 1); n       for (j = 0; j < n; ++j) { n         a[j] /= 10; n       } n       flag[i] = 1; n     } n   } n } n int main() { n   int i; n   scanf( %d%d , &n, &k); n   for (i = 0; i < n; ++i) scanf( %s , s[i]); n   ans = 1000000000; n   memset(flag, 1, sizeof(flag)); n   dfs(0); n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n const int MOD = 998244353; n int n; n char s[N]; n int solve() { n   scanf( %d , &n); n   scanf( %s , s); n   bool ok = true; n   for (int i = 0; i < n;) { n     int j = i; n     while (j < n && s[j] == s[i]) ++j; n     if (s[i] ==  0  && i == 0) { n       for (int k = i; k < j; ++k) printf( 0 ); n     } else if (s[i] ==  1  && j == n) { n       for (int k = i; k < j; ++k) printf( 1 ); n     } else if (s[i] ==  1  && ok) { n       printf( 0 ); n       ok = false; n     } n     i = j; n   } n   printf(  n ); n   return 0; n } n int main() { n   int t = 1; n   scanf( %d , &t); n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18 + 10; n const int maxn = 1e6 + 10; n struct node { n   int d, l, r; n   long long c; n } a[maxn]; n long long L[maxn], R[maxn], vis[maxn]; n bool cmp(node x, node y) { return x.d < y.d; } n int main() { n   int n, m, k; n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= m; ++i) { n     scanf( %d%d%d%I64d , &a[i].d, &a[i].l, &a[i].r, &a[i].c); n   } n   sort(a + 1, a + 1 + m, cmp); n   memset(L, -1, sizeof L); n   memset(R, -1, sizeof R); n   memset(vis, 0, sizeof vis); n   long long cnt = 0; n   long long tot = 0; n   for (int i = 1; i <= m; ++i) { n     if (a[i].r != 0) continue; n     if (!vis[a[i].l]) { n       vis[a[i].l] = a[i].c; n       cnt++; n       tot += a[i].c; n     } else { n       if (vis[a[i].l] > a[i].c) { n         tot = tot - vis[a[i].l] + a[i].c; n         vis[a[i].l] = a[i].c; n       } n     } n     if (cnt == n) { n       L[i] = tot; n     } n   } n   memset(vis, 0, sizeof vis); n   cnt = 0; n   tot = 0; n   for (int i = m; i >= 1; --i) { n     if (a[i].l != 0) continue; n     if (!vis[a[i].r]) { n       vis[a[i].r] = a[i].c; n       cnt++; n       tot += a[i].c; n     } else { n       if (vis[a[i].r] > a[i].c) { n         tot = tot - vis[a[i].r] + a[i].c; n         vis[a[i].r] = a[i].c; n       } n     } n     if (cnt == n) { n       R[i] = tot; n     } n   } n   long long ans = INF; n   for (int i = m; i >= 1; --i) { n     if (R[i] == -1) continue; n     if (R[i + 1] != -1) R[i] = min(R[i], R[i + 1]); n   } n   for (int i = 1; i <= m; ++i) { n     if (L[i] == -1) continue; n     int day = a[i].d + k + 1; n     int l = i + 1, r = m, len = 50; n     while (len--) { n       int mid = (l + r) / 2; n       if (a[mid].d >= day) n         r = mid - 1; n       else n         l = mid + 1; n     } n     if (l == -1) continue; n     if (R[l] != -1) { n       ans = min(ans, L[i] + R[l]); n     } n   } n   if (ans == INF) { n     puts( -1 ); n   } else { n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline int in() { n   int N = 0; n   register char c = getchar_unlocked(); n   while (c < 48 || c > 57) { n     c = getchar_unlocked(); n   } n   while (c > 47 && c < 58) { n     N = (N << 3) + (N << 1) + (c - 48); n     c = getchar_unlocked(); n   } n   return N; n } n inline long long int inl() { n   long long int N = 0; n   register char c = getchar_unlocked(); n   while (c < 48 || c > 57) { n     c = getchar_unlocked(); n   } n   while (c > 47 && c < 58) { n     N = (N << 3) + (N << 1) + (c - 48); n     c = getchar_unlocked(); n   } n   return N; n } n inline int inp() { n   int N = 0, sign = 1; n   register char c = getchar_unlocked(); n   while (c < 48 || c > 57) { n     if (c ==  - ) sign = 0; n     c = getchar_unlocked(); n   } n   while (c > 47 && c < 58) { n     N = (N << 3) + (N << 1) + (c - 48); n     c = getchar_unlocked(); n   } n   return (sign ? N : (-N)); n } n inline long long int inpl() { n   long long int N = 0, sign = 1; n   register char c = getchar_unlocked(); n   while (c < 48 || c > 57) { n     if (c ==  - ) sign = 0; n     c = getchar_unlocked(); n   } n   while (c > 47 && c < 58) { n     N = (N << 3) + (N << 1) + (c - 48); n     c = getchar_unlocked(); n   } n   return (sign ? N : (-N)); n } n inline bool inb() { n   char c = getchar_unlocked(); n   while (c < 48 || c > 57) { n     c = getchar_unlocked(); n   } n   return (c ==  0  ? 0 : 1); n } n inline long long int ModP(long long int b, long long int e) { n   b %= 1000000007; n   long long int r = 1; n   while (e > 0) { n     if (e & 1) r = (r * b) % 1000000007; n     b = (b * b) % 1000000007; n     e >>= 1; n   } n   return r; n } n bool pri[10] = {1, 1, 0, 0}; n vector<int> prm; n inline void prime_init() { n   int i, j; n   for (i = 3; i * i < 10; i += 2) { n     if (!pri[i]) { n       for (j = i * i; j <= 10; j += 2 * i) { n         pri[j] = 1; n       } n     } n   } n } n inline void prime_asgn() { n   int i; n   prm.push_back(2); n   for (i = 3; i < 10; i += 2) n     if (!pri[i]) prm.push_back(i); n } n inline bool prime_check(long long int i) { n   if (i == 1) return 0; n   if (i < 4) return 1; n   if (i & 1) { n     if (i < 10) { n       if (!pri[i]) n         return 1; n       else n         return 0; n     } else { n       int j, x; n       for (j = 1; ((x = prm[j]) * x) <= i; j++) { n         if (!(i % x)) return 0; n       } n       return 1; n     } n   } else n     return 0; n } n inline bool rngprm_check(int i) { n   if (i == 1) return 0; n   if (i < 4) return 1; n   if (i & 1) { n     if (!pri[i]) n       return 1; n     else n       return 0; n   } else n     return 0; n } n inline int npf(long long int s) { n   int j, x; n   int c; n   for (c = j = 0; ((x = prm[j]) * prm[j]) <= s; j++) { n     if (!(s % x)) { n       c++; n       while (!(s % x)) { n         s /= x; n       } n     } n   } n   if (s > 1) c++; n   return (c); n } n inline int nf(long long int s) { n   if (s == 1) return 1; n   int j, x, f = 1; n   int c = 1; n   for (j = 0; ((x = prm[j]) * prm[j]) <= s; c = 1, j++) { n     if (!(s % x)) { n       while (!(s % x)) { n         s /= x; n         c++; n       } n       f = f * c; n     } n   } n   if (s > 1) { n     f = f << 1; n   } n   return (f); n } n vector<pair<int, int> > nfa; n inline void nfx(long long int s) { n   nfa.clear(); n   if (s == 1) return; n   int j, x, c = 0; n   for (j = 0; ((x = prm[j]) * prm[j]) <= s; c = 0, j++) { n     if (!(s % x)) { n       while (!(s % x)) { n         s /= x; n         c++; n       } n       nfa.push_back(make_pair((x), (c))); n     } n   } n   if (s > 1) { n     nfa.push_back(make_pair((s), (1))); n   } n } n vector<long long int> factors; n inline void factr() { n   factors.clear(); n   factors.push_back(1); n   long long int i, y, p, q, j; n   for (pair<int, int> x : nfa) { n     y = factors.size(); n     p = x.first; n     q = x.second; n     for (j = 0; j < q; j++) { n       for (i = 0; i < y; i++) { n         factors.push_back(factors[i] * p); n       } n       p *= x.first; n     } n   } n } n long long int fc[10]; n inline long long int InverseEuler(long long int n) { n   return ModP(n, 1000000007 - 2); n } n inline long long int nCr(int n, int r) { n   if (r > n) return 0; n   return (fc[n] * n           ((InverseEuler(fc[r]) * InverseEuler(fc[n - r])) % 1000000007)) % n          1000000007; n } n inline void facs() { n   fc[0] = fc[1] = 1; n   for (long long int i = 2; i < 10; i++) { n     fc[i] = (i * fc[i - 1]) % 1000000007; n   } n } n int main() { n   int h1 = in(), a1 = in(), c1 = in(), h2 = in(), a2 = in(), s = 0, h = 0, i; n   vector<int> a; n   while (h2 > 0) { n     if ((h2 - a1) > 0 && h1 <= a2) { n       h++; n       a.push_back(1); n       h1 += c1; n     } else { n       h2 -= a1; n       s++; n     } n     h1 -= a2; n   } n   cout << h + s <<   n ; n   for (i = 0; i < (h); ++i) cout <<  HEAL n ; n   for (i = 0; i < (s); ++i) cout <<  STRIKE n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxm = 20005, maxn = 1005; n double f[maxm][maxn]; n int K, q, p, l, r, mid; n int main() { n   scanf( %d%d , &K, &q); n   f[0][0] = 1; n   for (int i = 1; i <= maxm - 1; i++) n     for (int j = 1; j <= K; j++) n       f[i][j] = f[i - 1][j - 1] * (K - j + 1) / K + f[i - 1][j] * j / K; n   while (q--) { n     scanf( %d , &p); n     l = 1; n     r = maxm - 1; n     while (l < r) { n       mid = (l + r) >> 1; n       if (f[mid][K] >= 1.0 * p / 2000 - 1e-10) n         r = mid; n       else n         l = mid + 1; n     } n     printf( %d n , r); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n int len(const T& a) { n   return a.size(); n } n using ll = long long; n constexpr long double EPS = 1e-7; n long double MaxSubarray(const vector<long double>& a) { n   long double best = -1e8; n   long double cur = 0.0; n   for (auto& i : a) { n     cur = max((long double)0, cur + i); n     best = max(best, cur); n   } n   return best; n } n long double MinSubarray(const vector<long double>& a) { n   long double best = 1e8; n   long double cur = 0.0; n   for (auto& i : a) { n     cur = min((long double)0, cur + i); n     best = min(best, cur); n   } n   return best; n } n long double Check(const vector<long double>& a, long double x) { n   vector<long double> new_a(a.size()); n   for (int i = 0; i < len(a); ++i) { n     new_a[i] = (long double)a[i] - x; n   } n   return max(abs(MaxSubarray(new_a)), abs(MinSubarray(new_a))); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout << fixed << setprecision(6); n   int n; n   cin >> n; n   vector<long double> a(n); n   for (auto& i : a) cin >> i; n   long double l = -1e4 - 10, r = 1e4 + 10; n   for (int step = 0; step < 200; ++step) { n     long double m1 = l + (r - l) / 3.0; n     long double m2 = r - (r - l) / 3.0; n     if (Check(a, m1) - Check(a, m2) > EPS) { n       l = m1; n     } else { n       r = m2; n     } n   } n   cout << Check(a, l); n   return 0; n } n


#include <bits/stdc++.h> n enum QueryType { kArea = 1, kCrossProduct = 2 }; n int64_t ask(QueryType query_type, int64_t first_index, int64_t second_index, n             int64_t third_index) { n   std::cout << query_type <<     << first_index + 1 <<     << second_index + 1 n             <<     << third_index + 1 << std::endl n             << std::flush; n   std::cout.flush(); n   int64_t answer; n   std::cin >> answer; n   return answer; n } n void answer(const std::vector<int64_t>& permutation) { n   std::cout <<  0 ; n   for (auto index : permutation) { n     std::cout <<     << index + 1; n   } n   std::cout << std::endl << std::flush; n   std::cout.flush(); n } n int main() { n   int64_t vertex_count; n   std::cin >> vertex_count; n   std::vector<int64_t> left, right; n   for (int64_t third_index = 2; third_index < vertex_count; ++third_index) { n     if (ask(kCrossProduct, 0, 1, third_index) == 1) { n       left.push_back(third_index); n     } else { n       right.push_back(third_index); n     } n   } n   std::vector<int64_t> area(vertex_count); n   for (int64_t third_index = 2; third_index < vertex_count; ++third_index) { n     area[third_index] = ask(kArea, 0, 1, third_index); n   } n   std::vector<int64_t> permutation = {0}; n   if (!right.empty()) { n     int64_t rightmost = *std::max_element( n         right.begin(), right.end(), [&](int64_t first, int64_t second) -> bool { n           return area[first] < area[second]; n         }); n     std::vector<int64_t> right_bottom, right_top; n     for (auto vertex : right) { n       if (vertex != rightmost) { n         if (ask(kCrossProduct, 0, rightmost, vertex) == -1) { n           right_bottom.push_back(vertex); n         } else { n           right_top.push_back(vertex); n         } n       } n     } n     std::sort(right_bottom.begin(), right_bottom.end(), n               [&](int64_t first, int64_t second) -> bool { n                 return area[first] < area[second]; n               }); n     std::copy(right_bottom.begin(), right_bottom.end(), n               std::back_inserter(permutation)); n     permutation.push_back(rightmost); n     std::sort(right_top.begin(), right_top.end(), n               [&](int64_t first, int64_t second) -> bool { n                 return area[first] > area[second]; n               }); n     std::copy(right_top.begin(), right_top.end(), n               std::back_inserter(permutation)); n   } n   permutation.push_back(1); n   if (!left.empty()) { n     int64_t leftmost = *std::max_element( n         left.begin(), left.end(), [&](int64_t first, int64_t second) -> bool { n           return area[first] < area[second]; n         }); n     std::vector<int64_t> left_bottom, left_top; n     for (auto vertex : left) { n       if (vertex != leftmost) { n         if (ask(kCrossProduct, 1, leftmost, vertex) == -1) { n           left_bottom.push_back(vertex); n         } else { n           left_top.push_back(vertex); n         } n       } n     } n     std::sort(left_bottom.begin(), left_bottom.end(), n               [&](int64_t first, int64_t second) -> bool { n                 return area[first] < area[second]; n               }); n     std::copy(left_bottom.begin(), left_bottom.end(), n               std::back_inserter(permutation)); n     permutation.push_back(leftmost); n     std::sort(left_top.begin(), left_top.end(), n               [&](int64_t first, int64_t second) -> bool { n                 return area[first] > area[second]; n               }); n     std::copy(left_top.begin(), left_top.end(), n               std::back_inserter(permutation)); n   } n   answer(permutation); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2000001; n vector<long long> d1(N), d2(N); n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long n; n   cin >> n; n   long long x, y; n   for (long long i = 0; i < n; i++) { n     cin >> x >> y; n     d1[x + y]++; n     d2[x - y + 1000]++; n   } n   long long sums = 0; n   for (long long i = 0; i <= 2001; i++) { n     sums += 1LL * (d1[i]) * (d1[i] - 1) / 2; n     sums += 1LL * (d2[i]) * (d2[i] - 1) / 2; n   } n   cout << sums <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int k, n; n long long ans, maxim, suma; n map<string, int> f; n deque<pair<string, int>> box[200001], b1, b2, b; n vector<pair<int, int>> trouble; n string s; n int val; n bool comp(pair<string, int> x, pair<string, int> y) { n   return x.second > y.second; n } n int main() { n   cin >> k >> n; n   int nr = 0; n   for (int i = 1; i <= k; i++) { n     cin >> s >> val; n     string s1 = s; n     reverse(s1.begin(), s1.end()); n     s1 = min(s1, s); n     if (f[s1] == 0) { n       nr++; n       f[s1] = nr; n     } n     box[f[s1]].push_back({s, val}); n   } n   for (int i = 1; i <= nr; i++) { n     b = box[i]; n     b1.clear(); n     b2.clear(); n     b1.push_back(b[0]); n     for (int j = 1; j < b.size(); j++) { n       if (b[j].first == b1[0].first) n         b1.push_back(b[j]); n       else n         b2.push_back(b[j]); n     } n     sort(b1.begin(), b1.end(), comp); n     sort(b2.begin(), b2.end(), comp); n     if (!b2.empty()) { n       while (b1.size() != b2.size()) { n         if (b1.size() > b2.size()) n           b1.pop_back(); n         else n           b2.pop_back(); n       } n       for (int j = 0; j < b1.size(); j++) n         if (b1[j].second + b2[j].second > 0) ans += b1[j].second + b2[j].second; n     } else { n       s = b1.front().first; n       string s1 = s; n       reverse(s1.begin(), s1.end()); n       if (s == s1) { n         while (b1.size() >= 2) { n           if (b1[0].second < 0 || b1[1].second < 0) break; n           ans += b1[0].second + b1[1].second; n           b1.pop_front(); n           b1.pop_front(); n         } n         if (b1.size() == 1 && b1[0].second > 0) n           trouble.push_back({b1[0].second, -1e8}); n         if (b1.size() >= 2 && b1[0].second > 0) n           trouble.push_back({b1[0].second, b1[1].second}); n       } n     } n   } n   for (auto i : trouble) { n     suma += max(i.first + i.second, 0); n     if (suma > maxim) maxim = suma; n   } n   for (auto i : trouble) { n     long long sum1 = suma - max(i.first + i.second, 0) + i.first; n     if (sum1 > maxim) maxim = sum1; n   } n   cout << ans + maxim; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cin.tie(nullptr)->sync_with_stdio(false); n   if (fopen(   n              .in , n              r )) { n     freopen( n            n          .in , n          r , stdin); n     freopen( n            n          .out , n          w , stdout); n   } n   int n; n   cin >> n; n   vector<int> v(n); n   for (auto &x : v) cin >> x; n   sort(v.begin(), v.end()); n   int64_t sum = 0; n   for (int i = 0; i < n; ++i) { n     sum += abs(v[i] - i - 1); n   } n   cout << sum; n } n


#include <bits/stdc++.h> n using namespace std; n char ch[100010], temp[100010]; n bool ok[100010]; n int main() { n   int n; n   cin >> n; n   cin >> ch; n   int m = strlen(ch); n   for (int i = 1; i < n; i++) { n     cin >> temp; n     for (int j = 0; j < m; j++) { n       if (ch[j] >=  a  && ch[j] <=  z  && temp[j] >=  a  && temp[j] <=  z  && n           ch[j] != temp[j]) n         ok[j] = 1, ch[j] =  ? ; n       if (ch[j] ==  ?  && !ok[j]) ch[j] = temp[j]; n     } n   } n   for (int i = 0; i < m; i++) { n     if (ch[i] ==  ?  && !ok[i]) n       cout <<  x ; n     else n       cout << ch[i]; 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(0); n   cout.tie(0); n   long long int n; n   cin >> n; n   long long int s = 0; n   for (long long int i = 0; i < n; i++) { n     long long int x; n     cin >> x; n     s += x; n   } n   if (s % n == 0) n     cout << n; n   else n     cout << n - 1; n } n


#include <bits/stdc++.h> n using namespace std; n long long choose(long long n, long long k); n bool isprime(long long n); n long long power(long long x, long long y, long long p); n long long power(long long x, long long y); n long long gcd(long long a, long long b); n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, k; n   cin >> n >> k; n   queue<long long> q; n   set<long long> st; n   for (int i = 0; i < n; i++) { n     long long x; n     cin >> x; n     if (st.find(x) == st.end()) { n       if (q.size() == k) { n         st.erase(st.find(q.front())); n         q.pop(); n         q.push(x); n       } else { n         q.push(x); n       } n       st.insert(x); n     } n   } n   cout << q.size() << endl; n   vector<long long> v; n   while (!q.empty()) v.push_back(q.front()), q.pop(); n   for (int i = v.size() - 1; i > -1; i--) cout << v[i] <<    ; n   return 0; n } n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long choose(long long n, long long k) { n   if (k == 0) return 1; n   return (n * choose(n - 1, k - 1)) / k; n } n bool isprime(long long n) { n   for (long long i = 2; i * i <= n; i++) { n     if (n % i == 0) return false; n   } n   return true; n } n long long power(long long x, long long y, long long p) { n   long long res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n long long power(long long x, long long y) { n   long long res = 1; n   while (y > 0) { n     if (y & 1) res = res * x; n     y = y >> 1; n     x = x * x; n   } n   return res; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 102; n int w, h, n, qs, qt; n int area[maxn], areath; n pair<int, int> q[1000006]; n int dx[4] = {-1, 0, 1, 0}; n int dy[4] = {0, -1, 0, 1}; n bool vis[maxn][maxn]; n bool cut[maxn][maxn][4]; n int bfs(int stx, int sty) { n   qs = 0; n   qt = 1; n   q[0] = make_pair(stx, sty); n   while (qs < qt) { n     int nowx = q[qs].first; n     int nowy = q[qs++].second; n     for (int k = 0; k < 4; k++) n       if (!cut[nowx][nowy][k]) { n         int x = nowx + dx[k], y = nowy + dy[k]; n         if (x < 0 || x >= w || y < 0 || y >= h || vis[x][y]) continue; n         vis[x][y] = true; n         q[qt++] = make_pair(x, y); n       } n   } n   return qt; n } n int main() { n   cin >> w >> h >> n; n   for (int i = 1; i <= n; i++) { n     int xa, ya, xb, yb; n     cin >> xa >> ya >> xb >> yb; n     if (xa == xb) n       for (int j = ya; j < yb; j++) cut[xa][j][0] = cut[xa - 1][j][2] = true; n     else n       for (int j = xa; j < xb; j++) cut[j][ya][1] = cut[j][ya - 1][3] = true; n   } n   for (int i = 0; i < w; i++) n     for (int j = 0; j < h; j++) n       if (!vis[i][j]) { n         vis[i][j] = true; n         area[++areath] = bfs(i, j); n       } n   sort(area + 1, area + areath + 1); n   for (int i = 1; i < areath; i++) cout << area[i] <<    ; n   cout << area[areath] << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(3) n using namespace std; n inline char gc() { n   static const int L = 1000; n   static char sxd[L], *sss = sxd, *ttt = sxd; n   if (sss == ttt) { n     ttt = (sss = sxd) + fread(sxd, 1, L, stdin); n     if (sss == ttt) return EOF; n   } n   return *sss++; n } n inline int read(int& x) { n   x = 0; n   char c = getchar(); n   bool f = false; n   for (; !isdigit(c); c = getchar()) { n     if (c == EOF) return -1; n     if (c ==  - ) f = true; n   } n   for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); n   if (f) x = -x; n   return 1; n } n int i, j, a[1000], n, m, k, s, sum, z; n int main() { n   read(n); n   for (i = 1; i <= n; i++) read(a[i]); n   while (!a[++s]) n     ; n   for (; s <= n; s++) { n     if (a[s]) { n       if (sum < 2) z += sum; n       sum = 0; n       z++; n     } else n       sum++; n   } n   cout << z; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long a[5010]; n int pw2[5010]; n int ans = 1e9; n bool check(int x, int y) { n   if (a[x] % a[y] != 0LL) return false; n   if (pw2[y] - (y - x - 1) <= 0) return true; n   return (pw2[x] + 1 == pw2[y] - (y - x - 1)); n } n int dp[5010]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %I64d , a + i); n     while (a[i] % 2LL == 0LL) { n       a[i] /= 2LL; n       pw2[i]++; n     } n   } n   for (int i = 1; i <= n; i++) { n     dp[i] = i - 1; n     for (int j = 1; j < i; j++) { n       if (!check(j, i)) continue; n       dp[i] = min(dp[i], dp[j] + (i - j - 1)); n     } n     ans = min(ans, dp[i] + n - i); n   } n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, x, l, r, i, s, a[10179]; n int main() { n   cin >> n >> m; n   for (i = 1; i <= n; i++) cin >> a[i]; n   while (m--) { n     cin >> l >> r >> x; n     for (s = 0, i = l; i <= r; i++) n       if (a[i] < a[x]) s++; n     if (s + l == x) n       cout <<  Yes n ; n     else n       cout <<  No n ; n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n template <class T> n void na(vector<T>& a, int n) { n   a = vector<T>(n); n   for (T& i : a) cin >> i; n } n template <class T> n void printVector(vector<T>& a) { n   for (T& i : a) cout << i <<    ; n   cout <<   n ; n } n template <class T> n vector<T> shrink(vector<T>& x) { n   vector<T> vals = x; n   sort((vals).begin(), (vals).end()); n   (vals).resize(unique((vals).begin(), (vals).end()) - (vals).begin()); n   for (T& i : x) n     i = upper_bound((vals).begin(), (vals).end(), i) - vals.begin(); n   return vals; n } n const int N = 5555; n int n; n bool gmat[N][N]; n vector<int> g[N]; n bool found = false; n bool onstack[N], seen[N]; n stack<int> stk; n vector<int> cycle; n void dfs(int u) { n   onstack[u] = 1; n   seen[u] = 1; n   stk.push(u); n   for (int v : g[u]) { n     if (found) return; n     if (onstack[v]) { n       found = true; n       int cur = u; n       cycle.push_back(cur); n       do { n         stk.pop(); n         cur = stk.top(); n         cycle.push_back(cur); n       } while (cur != v); n       return; n     } n     if (!seen[v]) dfs(v); n   } n   onstack[u] = false; n   stk.pop(); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0), cout.tie(); n   cin >> n; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= n; ++j) { n       char c; n       cin >> c; n       if (c ==  1 ) g[i].push_back(j), gmat[i][j] = 1; n     } n   for (int i = 1; i <= n && !found; ++i) n     if (!seen[i]) dfs(i); n   reverse((cycle).begin(), (cycle).end()); n   if (!found) n     cout <<  -1 n ; n   else { n     int j = 1, k = 2; n     while (k < ((int)(cycle).size())) { n       if (gmat[cycle[k]][cycle[0]]) { n         cout << cycle[0] <<     << cycle[j] <<     << cycle[k] <<   n ; n         break; n       } n       ++k, ++j; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b, c, d, e, f; n bool ju2(long long x, long long y) { n   x -= c, y -= d; n   long long div = e * e + f * f; n   if (!div) return x == 0 && y == 0; n   return (e * x + f * y) % div == 0 && (e * y - f * x) % div == 0; n } n bool ju1() { n   if (ju2(a, b)) return 1; n   if (ju2(-a, -b)) return 1; n   if (ju2(-b, a)) return 1; n   if (ju2(b, -a)) return 1; n   return 0; n } n signed main() { n   scanf( %lld%lld%lld%lld%lld%lld , &a, &b, &c, &d, &e, &f); n   if (ju1()) n     puts( YES ); n   else n     puts( NO ); n   return 0; n } n


#pragma GCC optimize( O3 ) n #include <bits/stdc++.h> n // #include <ext/pb_ds/assoc_container.hpp> n // using namespace __gnu_pbds; n // gp_hash_table<int, int> mapka; n  n using namespace std; n #define PB push_back n #define MP make_pair n #define LL long long n #define int LL n #define FOR(i,a,b) for(int i = (a); i <= (b); i++) n #define RE(i,n) FOR(i,1,n) n #define REP(i,n) FOR(i,0,(int)(n)-1) n #define R(i,n) REP(i,n) n #define VI vector<int> n #define PII pair<int,int> n #define LD long double n #define FI first n #define SE second n #define st FI n #define nd SE n #define ALL(x) (x).begin(), (x).end() n #define SZ(x) ((int)(x).size()) n  n template<class C> void mini(C &a4, C b4) { a4 = min(a4, b4); } n template<class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); } n  n template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<< = <<h<<endl; } n template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { n   while(*sdbg!= , )cerr<<*sdbg++; n   cerr<< = <<h<< , ; _dbg(sdbg+1, a...); n } n  n template<class T> ostream &operator<<(ostream& os, vector<T> V) { n   os <<  [ ; for (auto vv : V) os << vv <<  , ; return os <<  ] ; n } n template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) { n   return os <<  (  << P.st <<  ,  << P.nd <<  ) ; n } n  n #ifdef LOCAL n #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) n #else n #define debug(...) (__VA_ARGS__) n #define cerr if(0)cout n #endif n  n vector<vector<int>> res; n int n,m; n vector<vector<PII>> d; n void licz(int s){ n   vector<vector<int>> kt(n); n   vector<int> odl(n,2e9); n   int naj = 2e9; n   auto dod = [&](int v,int od){ n     if(odl[v] > od){ n       odl[v] = od; n       kt[od % n].PB(v); n     } n   }; n   for(PII el:d[s]){ n     mini(naj,el.SE); n     dod(el.FI, el.SE); n   } n   int gd = naj % n; n   R(_,n){ n     debug(gd, SZ(kt)); n     R(i,SZ(kt[gd])){ n       int el = kt[gd][i]; n //     for(int el:kt[gd]){ n       if(odl[el] == naj){ n         for(PII ed:d[el]){ n           dod((ed.FI + gd) % n, naj + ed.SE); n         } n         el++; n         if(el == n)el -= n; n         dod(el, naj + 1); n       } n     } n     naj++; n     gd++; n     if(gd == n)gd = 0; n   } n   R(i,n)if(s != i){ n     res[s][i] = odl[i]; n   } n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout << fixed << setprecision(11); n   cerr << fixed << setprecision(6); n   cin >> n >> m; n   res.resize(n,vector<int>(n)); n   d.resize(n); n   R(i,m){ n     int a,b,c; n     cin >> a >> b >> c; n     d[a].PB({b,c}); n   } n   R(i,n){ n     licz(i); n   } n   R(i,n){ n     R(j,n){ n       cout << res[i][j] <<    ; n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long modinverse(long long a, long long m) { n   long long m0 = m; n   long long y = 0, x = 1; n   if (m == 1) return 0; n   while (a > 1) { n     long long q = a / m, t = m; n     m = a % m, a = t; n     t = y; n     y = x - q * y; n     x = t; n   } n   if (x < 0) x += m0; n   return x; n } n long long mod_pow(long long a, long long n, long long p) { n   long long ans = 1; n   while (n) { n     if (n & 1) ans = (ans * a) % p; n     a = (a * a) % p; n     n >>= 1; n   } n   return ans; n } n long long prime_mod_pow(long long a, long long n, long long p) { n   long long ans = 1; n   n %= p - 1; n   while (n) { n     if (n & 1) ans = (ans * a) % p; n     a = (a * a) % p; n     n >>= 1; n   } n   return ans; n } n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long ext_gcd(long long l, long long r, long long &x, long long &y) { n   if (l == 0) { n     x = 0; n     y = 1; n     return r; n   } n   long long x1, y1, g; n   g = ext_gcd(r % l, l, x1, y1); n   x = y1 - (r / l) * x1; n   y = x1; n   return g; n } n const long long dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; n long long ok(long long x, long long y, long long n, long long m) { n   return (x >= 0 and y >= 0 and x < n and y < m); n } n long long num_div; n map<long long, long long> pr_factor; n void factorise(long long n) { n   long long count = 0; n   while (!(n % 2)) { n     n >>= 1; n     count++; n   } n   if (count) num_div *= (count + 1); n   pr_factor[2] = count; n   for (long long i = 3; i <= sqrt(n); i += 2) { n     count = 0; n     while (n % i == 0) { n       count++; n       n /= i; n     } n     if (count) num_div *= (count + 1); n     pr_factor[i] = count; n   } n   if (n > 2) num_div *= 2; n   pr_factor[n] = 1; n } n bool type1(long long x1, long long x2, long long x3, long long y1, long long y2, n            long long y3, char c1, char c2, char c3) { n   if (x1 == x2 + x3 and y2 == y3 and x1 == y1 + y2) { n     cout << x1 <<   n ; n     for (long long i = 0; i < y1; i++) { n       for (long long j = 0; j < x1; j++) cout << c1; n       cout <<   n ; n     } n     for (long long i = 0; i < y2; i++) { n       for (long long j = 0; j < x2; j++) cout << c2; n       for (long long j = 0; j < x3; j++) cout << c3; n       cout <<   n ; n     } n     return true; n   } n   return false; n } n bool type2(long long x1, long long x2, long long x3, long long y1, long long y2, n            long long y3, char c1, char c2, char c3) { n   if (x1 == x2 and x2 == x3 and x1 == y1 + y2 + y3) { n     cout << x1 <<   n ; n     for (long long i = 0; i < y1; i++) { n       for (long long j = 0; j < x1; j++) cout << c1; n       cout <<   n ; n     } n     for (long long i = 0; i < y2; i++) { n       for (long long j = 0; j < x1; j++) cout << c2; n       cout <<   n ; n     } n     for (long long i = 0; i < y3; i++) { n       for (long long j = 0; j < x3; j++) cout << c3; n       cout <<   n ; n     } n     return true; n   } n   return false; n } n void solve() { n   long long x1, x2, x3, y1, y2, y3; n   cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3; n   vector<pair<long long, long long> > a; n   a.push_back(make_pair(x1, y1)); n   a.push_back(make_pair(y1, x1)); n   vector<pair<long long, long long> > b; n   b.push_back(make_pair(x2, y2)); n   b.push_back(make_pair(y2, x2)); n   vector<pair<long long, long long> > c; n   c.push_back(make_pair(x3, y3)); n   c.push_back(make_pair(y3, x3)); n   for (long long i = 0; i < a.size(); i++) { n     long long X1 = a[i].first, Y1 = a[i].second; n     for (long long j = 0; j < b.size(); j++) { n       long long X2 = b[j].first, Y2 = b[j].second; n       for (long long k = 0; k < c.size(); k++) { n         long long X3 = c[k].first, Y3 = c[k].second; n         if (type1(X1, X2, X3, Y1, Y2, Y3,  A ,  B ,  C )) return; n         if (type1(X1, X3, X2, Y1, Y3, Y2,  A ,  C ,  B )) return; n         if (type1(X2, X1, X3, Y2, Y1, Y3,  B ,  A ,  C )) return; n         if (type1(X2, X3, X1, Y2, Y3, Y1,  B ,  C ,  A )) return; n         if (type1(X3, X1, X2, Y3, Y1, Y2,  C ,  A ,  B )) return; n         if (type1(X3, X2, X1, Y3, Y2, Y1,  C ,  B ,  A )) return; n         if (type2(X1, X2, X3, Y1, Y2, Y3,  A ,  B ,  C )) return; n         if (type2(X1, X3, X2, Y1, Y3, Y2,  A ,  C ,  B )) return; n         if (type2(X2, X1, X3, Y2, Y1, Y3,  B ,  A ,  C )) return; n         if (type2(X2, X3, X1, Y2, Y3, Y1,  B ,  C ,  A )) return; n         if (type2(X3, X1, X2, Y3, Y1, Y2,  C ,  A ,  B )) return; n         if (type2(X3, X2, X1, Y3, Y2, Y1,  C ,  B ,  A )) return; n       } n     } n   } n   cout << -1 <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t = 1; n   long long tc = 1; n   while (t--) { n     solve(); n     tc++; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char a[505][505]; n   int i, n, j, c = 0; n   cin >> n; n   for (i = 0; i < n; i++) { n     for (j = 0; j < n; j++) { n       cin >> a[i][j]; n     } n   } n   for (i = 0; i < n; i++) { n     for (j = 0; j < n; j++) { n       if (a[i][j] ==  X ) { n         int m = i - 1; n         int n = i + 1; n         int o = j - 1; n         int p = j + 1; n         if (a[m][o] ==  X  && a[m][p] ==  X  && a[n][o] ==  X  && n             a[n][p] ==  X ) { n           c++; n         } n       } n     } n   } n   cout << c << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[1000]; n bool ok = false; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int sum = 0; n   for (int i = 0; i < n; i++) sum += a[i]; n   int m; n   cin >> m; n   int l, r; n   for (int i = 0; i < m; i++) { n     cin >> l >> r; n     if (sum >= l && sum <= r) { n       cout << sum << endl; n       exit(0); n     } n     if (sum < l) cout << l << endl, exit(0); n   } n   cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int main() { n   int a, b, c; n   cin >> a >> b >> c; n   if (!c) return cout << (a == b ?  YES  :  NO ), 0; n   if (c > 0 && b < a || c < 0 && b > a) return cout <<  NO , 0; n   cout << ((a % c + c) % c == (b % c + c) % c ?  YES  :  NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 512345, lgN = 20, rootN = 1123; n const double eps = 1e-18; n int r[N], w[N]; n vector<double> positive, negative; n double p[N]; n int main() { n   int n, c, d; n   cin >> n >> c >> d; n   for (int i = 1; i <= n; ++i) { n     cin >> r[i] >> w[i]; n     r[i] -= c, w[i] -= d; n     p[i] = (((double)r[i]) / (double)w[i]); n   } n   for (int i = 1; i <= n; ++i) { n     double x = (((double)r[i]) / (double)w[i]); n     if (w[i] > 0) { n       positive.push_back(x); n     } else if (w[i] < 0) { n       negative.push_back(x); n     } n   } n   sort(positive.begin(), positive.end()); n   sort(negative.begin(), negative.end()); n   long long first = 0, second = 0; n   for (int i = 0; i < positive.size(); ++i) { n     first += (lower_bound(negative.begin(), negative.end(), positive[i]) - n               negative.begin()); n   } n   for (int i = 0; i < negative.size(); ++i) { n     second += (lower_bound(positive.begin(), positive.end(), negative[i]) - n                positive.begin()); n   } n   long long ans = 0; n   for (int i = 1; i <= n; ++i) { n     if (w[i] > 0) { n       ans += ((long long)(negative.end() - upper_bound(negative.begin(), n                                                        negative.end(), p[i]))) * n              (lower_bound(negative.begin(), negative.end(), p[i]) - n               negative.begin()); n     } else if (w[i] < 0) { n       ans += ((long long)(positive.end() - upper_bound(positive.begin(), n                                                        positive.end(), p[i]))) * n              (lower_bound(positive.begin(), positive.end(), p[i]) - n               positive.begin()); n     } else if (w[i] == 0) { n       if (r[i] < 0) n         ans += first; n       else n         ans += second; 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 n, la, lb, x; n   cin >> n >> la >> lb; n   int apple[100]; n   for (int i = 1; i <= n; i++) apple[i] = 2; n   for (int i = 1; i <= la; i++) { n     cin >> x; n     apple[x] = 1; n   } n   for (int i = 1; i <= lb; i++) { n     cin >> x; n     if (apple[x] != 1) apple[x] = 2; n   } n   for (int i = 1; i <= n; i++) cout << apple[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n bool vis[400]; n set<int> sub[400]; n vector<int> ga[400]; n vector<int> path; n vector<int> ord; n int curr; n void DFS(int a) { n   sub[a].insert(a); n   for (int i = 0; i < ga[a].size(); i++) { n     int b = ga[a][i]; n     if (vis[b]) continue; n     vis[b] = true; n     DFS(b); n     for (__typeof(sub[b].begin()) it = sub[b].begin(); it != sub[b].end(); it++) n       sub[a].insert(*it); n   } n } n void SFD(int a) { n   if (ord[curr] == a) { n     curr++; n     return; n   } n   while (1) { n     if (curr == ord.size()) return; n     bool upd = false; n     for (int i = 0; i < ga[a].size(); i++) { n       int b = ga[a][i]; n       if (vis[b] || sub[b].find(ord[curr]) == sub[b].end()) continue; n       vis[b] = true; n       path.push_back(b); n       SFD(b); n       path.push_back(a); n       upd = true; n       break; n     } n     if (!upd) return; n   } n } n int main() { n   cin >> n; n   for (int i = 1; i < n; i++) { n     int a, b; n     cin >> a >> b; n     ga[a].push_back(b); n     ga[b].push_back(a); n   } n   int x; n   while (cin >> x) ord.push_back(x); n   vis[1] = true; n   DFS(1); n   memset(vis, false, sizeof(vis)); n   path.push_back(1); n   vis[1] = true; n   SFD(1); n   if (path.size() != n * 2 - 1) n     cout << -1; n   else { n     for (int i = 0; i < path.size(); i++) cout << path[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 300100; n int read() { n   int x = 0, w = 1; n   char ch = 0; n   while (!isdigit(ch)) { n     ch = getchar(); n     if (ch ==  - ) w = -1; n   } n   while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar(); n   return x * w; n } n vector<int> G[maxn]; n int vis[maxn]; n int fa[maxn], ans[maxn]; n int findfa(int rt) { return rt == fa[rt] ? rt : fa[rt] = findfa(fa[rt]); } n void merge(int a, int b) { n   if (findfa(a) != findfa(b)) fa[fa[a]] = fa[b]; n } n int tmp = 0, tmp2 = 0; n void dfs(int rt, int col, int res) { n   if (vis[rt] == col) return; n   if (res > tmp) tmp = res, tmp2 = rt; n   vis[rt] = col; n   for (int to : G[rt]) merge(rt, to), dfs(to, col, res + 1); n } n int main() { n   int n = read(), m = read(), q = read(); n   for (int i = 1; i <= n; i++) fa[i] = i; n   while (m--) { n     int u = read(), v = read(); n     G[u].push_back(v); n     G[v].push_back(u); n   } n   for (int i = 1; i <= n; i++) n     if (!vis[i]) { n       tmp = 0, tmp2 = i; n       dfs(i, i, 0); n       tmp = 0; n       dfs(tmp2, tmp2, 0); n       ans[findfa(i)] = tmp; n     } n   while (q--) { n     int opt = read(); n     if (opt == 1) n       printf( %d n , ans[findfa(read())]); n     else { n       int u = read(), v = read(); n       u = findfa(u), v = findfa(v); n       if (u == v) continue; n       fa[u] = v; n       ans[v] = n           max(max(ans[v], ans[u]), (ans[u] + 1) / 2 + (ans[v] + 1) / 2 + 1); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, I; n   cin >> n >> I; n   map<long long, long long> mp; n   for (long long i = 0; i < n; i++) { n     long long x; n     cin >> x; n     mp[x]++; n   } n   if (ceil(log2(mp.size())) * n <= I * 8) { n     cout << 0; n     return 0; n   } n   vector<long long> v(mp.size() + 1); n   long long j = 1; n   v[0] = 0; n   for (auto i = mp.begin(); i != mp.end(); i++) { n     v[j] = v[j - 1] + i->second; n     j++; n   } n   long long x = pow(2, (I * 8) / n); n   long long max_int = 0; n   for (long long i = 0; i < v.size() - x; i++) { n     max_int = max(max_int, v[i + x] - v[i]); n   } n   cout << n - max_int; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long cc = 0; n unordered_map<long long, long long> c, rc; n vector<vector<long long> > graph(1e6); n vector<bool> visited(1e6, false); n vector<vector<long long> > answers(1e6); n void dfs(long long node) { n   visited[node] = true; n   answers[cc].push_back(node); n   for (auto i : graph[node]) n     if (!visited[i]) dfs(i); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n; n   cin >> n; n   vector<long long> nums(n); n   for (int i = 0; i < (int)(n); i++) cin >> (nums)[i]; n   ; n   vector<long long> nums_copy((nums).begin(), (nums).end()); n   sort((nums_copy).begin(), (nums_copy).end()); n   for (int i = 0; i < n; i++) c[i] = nums_copy[i], rc[nums_copy[i]] = i; n   vector<long long> modified(n); n   for (int i = 0; i < n; i++) modified[i] = rc[nums[i]]; n   for (int i = 0; i < n; i++) { n     graph[i].push_back(modified[i]); n   } n   for (int i = 0; i < n; i++) { n     if (!visited[i]) dfs(i), cc++; n   } n   cout << cc << endl; n   for (int i = 0; i < cc; i++) { n     long long k = answers[i].size(); n     cout << k <<    ; n     for (int j = 0; j < k; j++) cout << answers[i][j] + 1 <<    ; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> addr; n void psh(string s) { n   long long res = 0, cur = 0; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  . ) { n       res *= 256; n       res += cur; n       cur = 0; n     } else { n       cur *= 10; n       cur += (s[i] -  0 ); n     } n   } n   res *= 256; n   res += cur; n   addr.push_back(res); n } n void outp(long long mask) { n   int d = mask % 256; n   mask /= 256; n   int c = mask % 256; n   mask /= 256; n   int b = mask % 256; n   mask /= 256; n   int a = mask; n   printf( %d.%d.%d.%d n , a, b, c, d); n } n set<long long> s; n int main() { n   int n, k; n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     string s; n     cin >> s; n     psh(s); n   } n   long long mask = 0; n   for (int i = 31; i >= 0; i--) { n     mask += (1ll << i); n     s.clear(); n     for (int i = 0; i < n; i++) { n       s.insert(addr[i] & mask); n     } n     if (s.size() == k) { n       outp(mask); n       return 0; n     } n   } n   cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 51; n vector<int> g[N]; n int n, sz[N], dp[N][N][N][N], s, m, ans = 1e9, cost[N][N], temp[N]; n void dfs(int u, int p) { n   for (auto i : g[u]) n     if (i != p) dfs(i, u), sz[u] += sz[i]; n } n int calc(int from, int to, int behind, int front) { n   if (!front) return 1e9; n   if (g[to].size() == 1) n     return behind ? calc(to, from, 0, behind) + cost[to][from] : cost[to][from]; n   int &ref = dp[to][from][behind][front], id = 0, x; n   if (ref != -1) return ref; n   for (auto i : g[to]) n     if (i != from) n       for (int j = 1; j <= front; j++) calc(to, i, behind + front - j, j); n   for (int i = 1; i <= front; i++) temp[i] = -1e9; n   for (auto x : g[to]) { n     if (x == from) continue; n     for (int i = front; i; i--) n       for (int j = 1; i - j >= 0; j++) n         temp[i] = n             max(temp[i], min(temp[i - j], calc(to, x, behind + front - j, j))); n   } n   return ref = temp[front] + cost[from][to]; n } n int main() { n   scanf( %d , &n); n   memset(dp, -1, sizeof dp); n   for (int i = 1, a, b, c; i < n; i++) { n     scanf( %d%d%d , &a, &b, &c); n     g[--a].push_back(--b); n     g[b].push_back(a); n     cost[a][b] = cost[b][a] = c; n   } n   temp[0] = 1e9; n   scanf( %d%d , &s, &m); n   for (int i = 0, a; i < m; i++) { n     scanf( %d , &a); n     sz[a - 1]++; n   } n   dfs(--s, -1); n   for (auto i : g[s]) ans = min(ans, calc(s, i, m - sz[i], sz[i])); n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, p; n   cin >> n >> p; n   int a[n], maxi = 0; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a, a + n); n   for (int i = 0; i < n; i++) { n     maxi = max(maxi, a[i] - i); n   } n   int b = 0; n   for (int i = 0; i < n; i++) { n     if (i >= p - 1) { n       b = max(b, min(maxi - (a[i] - i), i)); n     } n   } n   b = max(0, p - b - 1); n   cout << b <<   n ; n   for (int i = 0; i < b; i++) { n     cout << maxi + i << (i == b - 1 ?   n  :    ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1005; n typedef struct { n   int pre[maxn]; n   void init(int n) { n     for (int i = 0; i <= n + 1; ++i) pre[i] = i; n   } n   int find(int x) { n     if (x == pre[x]) return x; n     return pre[x] = find(pre[x]); n   } n   void merge(int x, int y) { pre[find(x)] = find(y); } n } DS; n DS lds, rds; n int n, m, q; n int M[maxn][maxn]; n int D[maxn][maxn]; n int U[maxn][maxn]; n int L[maxn][maxn]; n int R[maxn][maxn]; n int a[maxn]; n int id[maxn]; n bool comp(int x, int y) { return a[x] < a[y]; } n int getMax(int n, int x) { n   int ret = 0; n   int l, r; n   int i; n   for (i = 1; i <= n; ++i) id[i] = i; n   lds.init(n); n   rds.init(n); n   sort(id + 1, id + 1 + n, comp); n   for (i = n; i > 0; --i) { n     l = lds.find(id[i] - 1) + 1; n     r = rds.find(id[i] + 1) - 1; n     if (l <= x && x <= r) ret = max(ret, (r - l + 1) * a[id[i]]); n     lds.merge(id[i], id[i] - 1); n     rds.merge(id[i], id[i] + 1); n   } n   return ret; n } n void printMat(int x[][maxn]) { n   int i, j; n   for (i = 1; i <= n; ++i) { n     for (j = 1; j <= m; ++j) printf( %d  , x[i][j]); n     putchar(  n ); n   } n } n int main() { n   int i, j, k; n   int ans; n   int op, x, y; n   scanf( %d %d %d , &n, &m, &q); n   for (i = 1; i <= n; ++i) n     for (j = 1; j <= m; ++j) scanf( %d , &M[i][j]); n   memset(D, 0, sizeof(D)); n   memset(U, 0, sizeof(U)); n   memset(L, 0, sizeof(L)); n   memset(R, 0, sizeof(R)); n   for (i = 1; i <= n; ++i) { n     for (j = 1; j <= m; ++j) { n       if (M[i][j]) { n         U[i][j] = U[i - 1][j] + 1; n         L[i][j] = L[i][j - 1] + 1; n       } n     } n   } n   for (i = n; i > 0; --i) { n     for (j = m; j > 0; --j) { n       if (M[i][j]) { n         D[i][j] = D[i + 1][j] + 1; n         R[i][j] = R[i][j + 1] + 1; n       } n     } n   } n   while (q--) { n     scanf( %d %d %d , &op, &x, &y); n     if (op == 1) { n       M[x][y] = !M[x][y]; n       for (j = y; j <= m; ++j) L[x][j] = M[x][j] ? L[x][j - 1] + 1 : 0; n       for (i = x; i <= n; ++i) U[i][y] = M[i][y] ? U[i - 1][y] + 1 : 0; n       for (j = y; j > 0; --j) R[x][j] = M[x][j] ? R[x][j + 1] + 1 : 0; n       for (i = x; i > 0; --i) D[i][y] = M[i][y] ? D[i + 1][y] + 1 : 0; n     } else { n       ans = 0; n       for (i = 1; i <= n; ++i) a[i] = L[i][y]; n       ans = max(ans, getMax(n, x)); n       for (i = 1; i <= n; ++i) a[i] = R[i][y]; n       ans = max(ans, getMax(n, x)); n       for (i = 1; i <= m; ++i) a[i] = U[x][i]; n       ans = max(ans, getMax(m, y)); n       for (i = 1; i <= m; ++i) a[i] = D[x][i]; n       ans = max(ans, getMax(m, y)); n       printf( %d n , ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n using ull = unsigned long long; n using pii = pair<int, int>; n using vi = vector<int>; n struct res { n   int x, t; n   bool operator<(const res &z) const { return x < z.x; } n   bool operator==(const res &z) const { return x == z.x && t == z.t; } n }; n const res E = {-1, 0}; n const int M = 1e9; n void q(int cnt, int x, int y) { n   if (cnt) cout << cnt <<     << x + 1 <<     << y + 1 <<   n ; n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout << setprecision(15); n   int n, k, v; n   cin >> n >> k >> v; n   vi a(n); n   for (int &x : a) cin >> x; n   vector<vector<res>> dp(n + 1, vector<res>(k, E)); n   dp[0][0] = {0, 0}; n   for (int i = 1; i <= n; ++i) { n     dp[i] = dp[i - 1]; n     for (res &r : dp[i]) r.t = 0; n     for (int j = 0; j < k; ++j) n       if (!(dp[i - 1][j] == E)) { n         auto &l = dp[i][(j + a[i - 1]) % k]; n         l = max(l, {dp[i - 1][j].x + a[i - 1], 1}); n         dp[i][j] = max(dp[i][j], {dp[i - 1][j].x + a[i - 1] / k * k, 0}); n       } n   } n   if (dp[n][v % k] == E) return cout <<  NO n , 0; n   vi b, g; n   for (int i = n, c = v % k; i >= 1; --i) { n     if (dp[i][c].t) n       b.push_back(i - 1), c = (c - a[i - 1] % k + k) % k; n     else n       g.push_back(i - 1); n   } n   int cap = 0; n   for (int x : b) cap += a[x]; n   int ad = 0; n   for (int x : g) ad += a[x]; n   cap += ad / k * k; n   if (cap < v) return cout <<  NO n , 0; n   cout <<  YES n ; n   int r, cur = 0, ot; n   if (((int)(b).size())) { n     r = b[0], cur = a[r]; n     for (int i = 1; i < ((int)(b).size()); ++i) { n       q(M, b[i], r), cur += a[b[i]]; n     } n     ot = r == 0 ? 1 : 0; n   } else { n     r = 0, ot = 1; n     q(M, r, ot); n   } n   for (int x : g) n     if (x != ot) q(M, x, ot); n   q(cur / k, r, ot), cur %= k; n   q((v - cur) / k, ot, r); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1050; n int n, m, g[maxn][maxn], ans[maxn][maxn]; n int p[4] = {4, 3, 2, 1}; n bool chk1(int x, int a, int b) { n   int i, st, c[2] = {a, b}; n   for (st = 0; st < m && !g[x][st]; st++) n     ; n   if (g[x][st] != c[0]) swap(c[0], c[1]); n   for (i = 0; st + i < m; i++) n     if (g[x][st + i] && g[x][st + i] != c[i & 1]) n       return 0; n     else n       ans[x][st + i] = c[i & 1]; n   for (i = 1; i <= st; i++) ans[x][st - i] = c[i & 1]; n   return 1; n } n bool chk2(int x, int a, int b) { n   int i, st, c[2] = {a, b}; n   for (st = 0; st < n && !g[st][x]; st++) n     ; n   if (g[st][x] != c[0]) swap(c[0], c[1]); n   for (i = 0; st + i < n; i++) n     if (g[st + i][x] && g[st + i][x] != c[i & 1]) n       return 0; n     else n       ans[st + i][x] = c[i & 1]; n   for (i = 1; i <= st; i++) ans[st - i][x] = c[i & 1]; n   return 1; n } n void print() { n   int i, j; n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) printf( %d , ans[i][j]); n     printf(  n ); n   } n } n bool check() { n   int i, j; n   bool z = 1; n   for (i = 0; i < n; i++) z &= chk1(i, p[(i & 1) * 2], p[(i & 1) * 2 + 1]); n   if (z) { n     print(); n     return 1; n   } n   z = 1; n   for (i = 0; i < m; i++) z &= chk2(i, p[(i & 1) * 2], p[(i & 1) * 2 + 1]); n   if (z) { n     print(); n     return 1; n   } n   return 0; n } n int main() { n   int i, j; n   scanf( %d%d n , &n, &m); n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) g[i][j] = getchar() -  0 ; n     scanf(  n ); n   } n   for (i = 0; i < 4 * 3 * 2; i++) { n     next_permutation(p, p + 4); n     if (check()) return 0; n   } n   printf( 0 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool chmax(T &a, const T &b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n bool chmin(T &a, const T &b) { n   if (a > b) { n     a = b; n     return 1; n   } n   return 0; n } n template <typename T> n istream &operator>>(istream &is, vector<T> &vec) { n   for (auto &v : vec) is >> v; n   return is; n } n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &vec) { n   for (int i = 0; i < vec.size(); i++) { n     os << vec[i]; n     if (i + 1 != vec.size()) os <<    ; n   } n   return os; n } n template <typename T> n ostream &operator<<(ostream &os, const set<T> &st) { n   for (auto itr = st.begin(); itr != st.end(); ++itr) { n     os << *itr; n     auto titr = itr; n     if (++titr != st.end()) os <<    ; n   } n   return os; n } n template <typename T> n ostream &operator<<(ostream &os, const unordered_set<T> &st) { n   for (auto itr = st.begin(); itr != st.end(); ++itr) { n     os << *itr; n     auto titr = itr; n     if (++titr != st.end()) os <<    ; n   } n   return os; n } n template <typename T> n ostream &operator<<(ostream &os, const multiset<T> &st) { n   for (auto itr = st.begin(); itr != st.end(); ++itr) { n     os << *itr; n     auto titr = itr; n     if (++titr != st.end()) os <<    ; n   } n   return os; n } n template <typename T> n ostream &operator<<(ostream &os, const unordered_multiset<T> &st) { n   for (auto itr = st.begin(); itr != st.end(); ++itr) { n     os << *itr; n     auto titr = itr; n     if (++titr != st.end()) os <<    ; n   } n   return os; n } n template <typename T1, typename T2> n ostream &operator<<(ostream &os, const pair<T1, T2> &p) { n   os <<  (  << p.first <<  ,   << p.second <<  ) ; n   return os; n } n template <typename T1, typename T2> n ostream &operator<<(ostream &os, const map<T1, T2> &mp) { n   for (auto itr = mp.begin(); itr != mp.end(); ++itr) { n     os <<  (  << itr->first <<  ,   << itr->second <<  ) ; n     auto titr = itr; n     if (++titr != mp.end()) os <<    ; n   } n   return os; n } n template <typename T1, typename T2> n ostream &operator<<(ostream &os, const unordered_map<T1, T2> &mp) { n   for (auto itr = mp.begin(); itr != mp.end(); ++itr) { n     os <<  (  << itr->first <<  ,   << itr->second <<  ) ; n     auto titr = itr; n     if (++titr != mp.end()) os <<    ; n   } n   return os; n } n using ll = long long int; n using P = pair<int, int>; n const int inf = 1e9; n const ll linf = 1LL << 50; n const double EPS = 1e-10; n const int mod = 1000000007; n const int dx[4] = {-1, 0, 1, 0}; n const int dy[4] = {0, -1, 0, 1}; n struct fast_io { n   fast_io() { n     ios_base::sync_with_stdio(false); n     cin.tie(nullptr); n     cout << fixed << setprecision(20); n   } n } fast_io_; n int main(int argc, char const *argv[]) { n   ll n, m; n   cin >> n >> m; n   m *= 2; n   ll t; n   cin >> t; n   t *= 2; n   vector<ll> x(n); n   vector<ll> d(n); n   for (int i = (0); i < (n); ++i) { n     cin >> x[i]; n     x[i] *= 2; n     x[i] %= m; n     string c; n     cin >> c; n     d[i] = (c ==  L  ? -1 : 1); n   } n   vector<int> e(n); n   for (int i = (0); i < (n); ++i) e[i] = i; n   sort((e).begin(), (e).end(), [&](int i, int j) { return x[i] < x[j]; }); n   vector<int> tr(n); n   for (int i = (0); i < (n); ++i) tr[e[i]] = i; n   vector<ll> res(n); n   for (int i_ = (0); i_ < (n); ++i_) { n     int i = e[i_]; n     res[i_] = (x[i] + d[i] * t) % m; n     if (res[i_] < 0) res[i_] += m; n   } n   ll curr = 0; n   if (d[e[0]] == 1) { n     for (int k = (1); k < (n); ++k) { n       if (d[e[k]] == -1 && (x[e[k]] - x[e[0]]) / 2 <= t) { n         curr += (t - (x[e[k]] - x[e[0]]) / 2) / (m / 2) + 1; n       } n     } n   } else { n     x[e[0]] += m; n     for (int k = (1); k < (n); ++k) { n       if (d[e[k]] == 1 && (x[e[0]] - x[e[k]]) / 2 <= t) { n         curr += (t - (x[e[0]] - x[e[k]]) / 2) / (m / 2) + 1; n       } n     } n     curr = (n - curr) % n; n     if (curr < 0) curr += n; n   } n   curr %= n; n   vector<pair<ll, int>> v(n); n   for (int i = (0); i < (n); ++i) v[i] = make_pair(res[i], i); n   sort((v).begin(), (v).end(), [&](pair<ll, int> x, pair<ll, int> y) { n     if (x.first != y.first) return x.first < y.first; n     if (d[e[x.second]] == -1) return true; n     return false; n   }); n   int at = 0; n   for (int i = (0); i < (n); ++i) n     if (v[i].second == 0) at = i; n   for (int i = (0); i < (n); ++i) { n     res[i] = v[(at + i) % n].first; n   } n   vector<ll> tres = res; n   for (int i = (0); i < (n); ++i) { n     res[i] = tres[(i - curr + n) % n]; n   } n   tres = res; n   for (int i = (0); i < (n); ++i) res[i] = tres[tr[i]]; n   for (int i = (0); i < (n); ++i) { n     if (res[i] < 0) res[i] += m; n   } n   for (int i = (0); i < (n); ++i) { n     res[i] /= 2; n     if (res[i] == 0) res[i] = (m / 2); n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[100000]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> arr[i]; n   for (int i = 1; n / i >= 3; ++i) { n     if (n % i != 0) continue; n     for (int j = 0; j < i; ++j) { n       int Ok = 0; n       for (int k = j; k < n; k += i) { n         if (arr[k] == 1) ++Ok; n       } n       if (Ok == n / i) { n         cout <<  YES  << endl; n         return 0; n       } n     } n   } n   cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k = 0; n   string s; n   cin >> s >> n; n   vector<char> v, vv; n   int len = s.size(); n   if (len % n != 0) { n     cout <<  NO  << endl; n     return 0; n   } n   int cnt = 0, c = 0, j; n   int m = len / n; n   for (int i = 0; i < n; i++) { n     v.clear(); n     vv.clear(); n     for (j = c; j < len; j++) { n       v.push_back(s[j]); n       vv.push_back(s[j]); n       cnt++; n       c++; n       if (cnt == m) break; n     } n     cnt = 0; n     int pnt = 0; n     reverse(vv.begin(), vv.end()); n     int ln = v.size(); n     for (int i = 0; i < ln; i++) { n       if (v[i] == vv[i]) pnt++; n       if (pnt == ln) { n         k++; n       } n     } n     v.clear(); n     vv.clear(); n   } n   if (k == n) { n     k = 0; n     cout <<  YES  << endl; n   } else { n     k = 0; n     cout <<  NO  << endl; n   } n   return 0; n } n


#include <cmath> n #include <cstdio> n #include <string> n #include <cstring> n #include <iostream> n #include <algorithm> n using namespace std; n typedef long long ll; n const int N = 55; n const int mod = 1e9 + 7; n  n  n int t , n , m ,ans; n char s[N]; n int a[N]; n  n void solve(){ n     cin>>s + 1; n     int len = strlen( s + 1); n     int flag = 1; // 1 : smaller n     int cnt = 0; n     while( cnt < len ){ n         if( cnt < len){ n             for(int i = 1;i <= len; ++ i){ n                 if( !a[i] ) { n                     a[i] = 1 , ++cnt ; n                     if( s[i] !=  a  ) s[i] =  a  ; n                     else s[i] =  b  ; n                     break; n                 } n             } n         } n         if( cnt < len){ n             for(int i = 1;i <= len; ++ i){ n                 if( !a[i] ) { n                     a[i] = 1 , ++cnt; n                     if( s[i] !=  z  ) s[i] =  z  ; n                     else s[i] =  y ; n                     break; n                 } n             } n         } n     } n     for(int i = 1;i <= len; ++ i) cout<<s[i]; n     memset(a ,0 , sizeof(a) ); n     cout<<endl; n } n  n int main(){ n     ios::sync_with_stdio(false); n     cin.tie(0); n     cin>>t; n     while( t -- ) solve(); n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int findGreater(int compare, int startIndex, int *arr, int sizeofArray) { n   for (int i = startIndex; i < sizeofArray; i++) { n     if (compare < arr[i]) return i; n   } n   return -1; n } n int findLesser(int compare, int startIndex, int *arr, int sizeofArray) { n   for (int i = startIndex; i < sizeofArray; i++) { n     if (compare > arr[i]) return i; n   } n   return -1; n } n void initArray(int *arr, int Size) { n   for (int j = 0; j < Size; j++) { n     cin >> arr[j]; n   } n } n int main() { n   int TestCases; n   int Size; n   int *arr; n   int i; n   int j; n   int k; n   cin >> TestCases; n   for (int t = 0; t < TestCases; t++) { n     cin >> Size; n     arr = new int[Size]; n     initArray(arr, Size); n     for (int i = 0; i < Size; i++) { n       if (i + 1 == Size) { n         cout <<  NO  << endl; n       } n       j = findGreater(arr[i], i + 1, arr, Size); n       if (j < 0) { n         continue; n       } n       k = findLesser(arr[j], j + 1, arr, Size); n       if (k < 0) { n         continue; n       } n       cout <<  YES  << endl; n       cout << i + 1 <<     << j + 1 <<     << k + 1 << endl; n       break; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int next_x[8] = {0, 1, -1, 0, 1, 1, -1, -1}; n const int next_y[8] = {1, 0, 0, -1, 1, -1, -1, 1}; n const int inf = 1e9 + 5; n const long long linf = 1e18 + 5; n const double PI = acos(-1.0); n const int MAXN = 1e6 + 5; n const int N = 4e6 + 5; n const long double eps = 1e-8; n const long double fix = 1e-2; n int n; n int a[MAXN], b[MAXN]; n int l[MAXN], r[MAXN]; n int s[MAXN], h = 0; n vector<int> st[MAXN], ed[MAXN]; n int cnt[MAXN]; n bool flag = 1; n struct node { n   int i; n   bool operator<(const node &t) const { return r[i] < r[t.i]; } n }; n int work() { n   scanf( %d , &n); n   for (int i = (1), I = (2 * n) + 1; i < I; ++i) { n     char t[5]; n     scanf( %s , t); n     if (t[0] ==  + ) continue; n     scanf( %d , &a[i]); n   } n   for (int i = (1), I = (2 * n) + 1; i < I; ++i) n     if (a[i]) { n       while (h && a[i] > a[s[h]]) --h; n       l[a[i]] = s[h] + 1, r[a[i]] = i - 1, s[++h] = i; n     } n   for (int x = (1), I = (n) + 1; x < I; ++x) n     st[l[x]].push_back(x), ed[r[x]].push_back(x), flag &= (r[x] >= l[x]); n   set<node> s; n   for (int i = (1), I = (2 * n) + 1; i < I; ++i) { n     for (auto x : st[i]) s.insert({x}); n     if (!a[i]) { n       if (s.empty()) n         flag = 0; n       else n         b[i] = s.begin()->i, s.erase(s.begin()); n     } n     for (auto x : ed[i]) n       if (s.find({x}) != s.end()) flag = 0; n   } n   for (int i = (1), I = (2 * n) + 1; i < I; ++i) n     if (b[i]) cnt[b[i]]++; n   for (int i = (1), I = (n) + 1; i < I; ++i) flag &= (cnt[i] == 1); n   if (!flag) return puts( NO ); n   puts( YES ); n   for (int i = (1), I = (2 * n) + 1; i < I; ++i) n     if (b[i]) printf( %d  , b[i]); n   return 0; n } n int main() { n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = INT_MAX; n const long double PI = acos(-1); n const long double E = 2.71828183; n const int MOD = 1e9 + 7; n vector<int> bfs(vector<vector<int> > adj, int src) { n   queue<int> q; n   q.push(src); n   vector<int> dst(adj.size(), INF); n   dst[src] = 0; n   while (!q.empty()) { n     int c = q.front(); n     q.pop(); n     for (int nbr : adj[c]) { n       if (dst[nbr] == INF) { n         dst[nbr] = dst[c] + 1; n         q.push(nbr); n       } n     } n   } n   return dst; n } n vector<int> bfsParents(vector<vector<int> > adj, int src) { n   vector<int> parents = vector<int>(adj.size(), INF); n   parents[src] = -1; n   queue<int> q; n   q.push(src); n   vector<int> dst(adj.size(), INF); n   dst[src] = 0; n   while (!q.empty()) { n     int c = q.front(); n     q.pop(); n     for (int nbr : adj[c]) { n       if (dst[nbr] == INF) { n         parents[nbr] = c; n         dst[nbr] = dst[c] + 1; n         q.push(nbr); n       } n     } n   } n   return parents; n } n list<int> shortestPath(vector<vector<int> > adj, int src, int dst) { n   vector<int> bfsParetns = bfsParents(adj, src); n   list<int> ans; n   ans.push_back(dst); n   while (ans.front() != src) ans.push_front(bfsParetns[ans.front()]); n   return ans; n } n long long powMod(long long base, long long exp, long long mod) { n   long long ans = 1; n   for (long long i = 0; i < exp; i++) ans = ((ans % mod) * (base % mod)) % mod; n   return ans; n } n vector<int> divisorsOf(int n) { n   vector<int> ans; n   for (int i = 1; i * i <= n; i++) { n     if (n % i == 0) ans.push_back(i); n     if (i * i != n && i != 1) ans.push_back(n / i); n   } n   return ans; n } n long long gcd(long long a, long long b) { n   return (a % b == 0 ? b : gcd(b, a % b)); n } n long double log(long double a, long double b) { return log(b) / log(a); } n long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } n long long power(long long base, long long exp) { n   long long res = 1; n   while (exp) { n     exp--; n     res *= base; n   } n   return res; n } n void solve(); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t = 1; n   while (t--) solve(); n   return 0; n } n void solve() { n   long long n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   vector<char> v(k); n   for (int i = 0; i < k; i++) cin >> v[i]; n   long long curr = 0; n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     bool in = false; n     for (char c : v) n       if (c == s[i]) in = true; n     if (in) curr++; n     if (!in || (in && i == n - 1)) { n       ans += curr * (curr + 1) / 2; n       curr = 0; n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n void err(istream_iterator<string> it) {} n template <typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) { n   cerr << *it <<   =   << a << endl; n   err(++it, args...); n } n long long mod = 1e9 + 7; n long long powm(long long a, long long b, long long mod) { n   long long res = 1; n   while (b) { n     if (b & 1) res = (res * a) % mod; n     a = (a * a) % mod; n     b >>= 1; n   } n   return res; n } n bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.first > b.first); n } n void solve() { n   int n, m; n   cin >> n >> m; n   string s[n]; n   for (long long i = 0; i < (n); i++) cin >> s[i]; n   int mat1[n][m]; n   int mat2[n][m]; n   int mat3[n][m]; n   int mat4[n][m]; n   memset(mat1, 0, sizeof(mat1)); n   memset(mat2, 0, sizeof(mat2)); n   memset(mat3, 0, sizeof(mat3)); n   memset(mat4, 0, sizeof(mat4)); n   for (long long i = 0; i < (n); i++) { n     for (long long j = 0; j < (m); j++) { n       if (s[i][j] ==  * ) { n         if (j - 1 >= 0) n           mat1[i][j] += 1 + mat1[i][j - 1]; n         else n           mat1[i][j] = 1; n       } n     } n   } n   for (long long i = 0; i < (n); i++) { n     for (long long j = (m)-1; j >= 0; j--) { n       if (s[i][j] ==  * ) { n         if (j + 1 < m) n           mat2[i][j] += 1 + mat2[i][j + 1]; n         else n           mat2[i][j] = 1; n       } n     } n   } n   for (long long j = 0; j < (m); j++) { n     for (long long i = 0; i < (n); i++) { n       if (s[i][j] ==  * ) { n         if (i - 1 >= 0) n           mat3[i][j] += 1 + mat3[i - 1][j]; n         else n           mat3[i][j] = 1; n       } n     } n   } n   for (long long j = 0; j < (m); j++) { n     for (long long i = (n)-1; i >= 0; i--) { n       if (s[i][j] ==  * ) { n         if (i + 1 < n) n           mat4[i][j] += 1 + mat4[i + 1][j]; n         else n           mat4[i][j] = 1; n       } n     } n   } n   int temp_mat2[n + 1][m + 1]; n   int temp_mat1[n + 1][m + 1]; n   memset(temp_mat1, 0, sizeof(temp_mat1)); n   memset(temp_mat2, 0, sizeof(temp_mat2)); n   vector<tuple<int, int, int> > v; n   for (long long i = 0; i < (n); i++) { n     for (long long j = 0; j < (m); j++) { n       if (mat1[i][j] > 1 and mat2[i][j] > 1 and mat3[i][j] > 1 and n           mat4[i][j] > 1) { n         temp_mat1[i][j - min({mat1[i][j], mat2[i][j], mat3[i][j], mat4[i][j]}) + n                      1]++; n         temp_mat1[i] n                  [j + min({mat1[i][j], mat2[i][j], mat3[i][j], mat4[i][j]})]--; n         temp_mat2[i - min({mat1[i][j], mat2[i][j], mat3[i][j], mat4[i][j]}) + 1] n                  [j]++; n         temp_mat2[i + min({mat1[i][j], mat2[i][j], mat3[i][j], mat4[i][j]})] n                  [j]--; n         v.push_back(make_tuple( n             i + 1, j + 1, n             min({mat1[i][j], mat2[i][j], mat3[i][j], mat4[i][j]}) - 1)); n       } n     } n   } n   for (long long i = 0; i < (n); i++) { n     for (long long j = 0; j < (m); j++) { n       if (j > 0) temp_mat1[i][j] += temp_mat1[i][j - 1]; n     } n   } n   for (long long j = 0; j < (m); j++) { n     for (long long i = 0; i < (n); i++) { n       if (i > 0) temp_mat2[i][j] += temp_mat2[i - 1][j]; n     } n   } n   for (long long i = 0; i < (n); i++) { n     for (long long j = 0; j < (m); j++) { n       if (s[i][j] ==  * ) { n         if (temp_mat1[i][j] == 0 and temp_mat2[i][j] == 0) { n           cout << -1 << endl; n           return; n         } n       } n     } n   } n   cout << v.size() << endl; n   for (long long i = 0; i < (v.size()); i++) { n     cout << (get<0>(v[i])) <<     << (get<1>(v[i])) <<     << (get<2>(v[i])) n          << endl; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int n, a, sum, high, w, low; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   cout << setprecision(10) << fixed; n   cin >> n >> w; n   for (int i = 1; i <= n; i++) { n     cin >> a; n     sum += a; n     if (sum > high) { n       high = sum; n     } n     if (sum < low) { n       low = sum; n     } n   } n   cout << max(w - high + 1 + low, 0) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 10; n const double INF = 1e18; n int n, m, c; n int a[N], p[N], f[N]; n double E[N]; n struct Node { n   double L, M, R, S; n   Node() {} n   Node(double L, double M, double R, double S) : L(L), M(M), R(R), S(S) {} n }; n struct SegmentTree { n   Node st[4 * N]; n   Node Merge(Node A, Node B) { n     Node C; n     C.L = max(A.L, A.S + B.L); n     C.R = max(B.R, B.S + A.R); n     C.M = max(A.M, max(B.M, A.R + B.L)); n     C.S = A.S + B.S; n     return C; n   } n   void Init(int k, int l, int r) { n     if (l == r) { n       double val = E[l]; n       st[k] = Node(val, val, val, val); n       return; n     } n     Init(k << 1, l, ((l + r) >> 1)); n     Init(k << 1 | 1, ((l + r) >> 1) + 1, r); n     st[k] = Merge(st[k << 1], st[k << 1 | 1]); n   } n   Node Query(int k, int l, int r, int i, int j) { n     if (l > j || r < i) return Node(-INF, -INF, -INF, 0); n     if (i <= l && r <= j) return st[k]; n     return Merge(Query(k << 1, l, ((l + r) >> 1), i, j), n                  Query(k << 1 | 1, ((l + r) >> 1) + 1, r, i, j)); n   } n } ST; n double Query(int l, int r) { n   Node ans = ST.Query(1, 1, n - 1, l, r); n   return max(ans.L, max(ans.R, ans.M)); n } n int main() { n   cin >> n >> m >> c; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   for (int i = 1; i < n; ++i) cin >> p[i]; n   for (int i = 1; i < n; ++i) { n     double prob = (double)p[i] / 100; n     double val = (double)(a[i + 1] - a[i]) / 2 - prob * c; n     E[i] = val; n   } n   ST.Init(1, 1, n - 1); n   double ans = 0; n   while (m--) { n     int u, v; n     cin >> u >> v; n     ans += max(0.00, Query(u, v - 1)); n   } n   cout << fixed << setprecision(10) << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int memo[500][26][26]; n int son[500][26][26]; n int cambios[500][26][26]; n vector<string> A; n int m, n; n void solve(int x, int y, int z) { n   for (int row = 0; row < x; row++) n     for (int a = 0; a < y; a++) n       for (int b = 0; b < z; b++) { n         int &res = memo[row][a][b]; n         if (row == 0) { n           res = cambios[0][a][b]; n           continue; n         } n         int mn = 1 << 29; n         for (int i = 0; i < 26; i++) n           if (i != a) n             for (int j = 0; j < 26; j++) n               if (j != b) { n                 int tmp = memo[row - 1][i][j] + cambios[row][a][b]; n                 if (mn > tmp) mn = tmp, son[row][a][b] = 1000 * i + j; n               } n         res = mn; n       } n } n int main() { n   while (cin >> m >> n) { n     memset(memo, -1, sizeof(memo)); n     A.clear(); n     string cad; n     for (int i = 0; i < m; i++) cin >> cad, A.push_back(cad); n     reverse((A).begin(), (A).end()); n     for (int row = 0; row < m; row++) { n       for (int a = 0; a < 26; a++) n         for (int b = 0; b < 26; b++) n           if (b != a) { n             int s = 0; n             for (int k = 0; k < n; k++) { n               if ((k & 1) == 0 && A[row][k] -  a  != a) s++; n               if ((k & 1) == 1 && A[row][k] -  a  != b) s++; n             } n             cambios[row][a][b] = s; n           } else n             cambios[row][a][b] = 1 << 29; n     } n     solve(m, 26, 26); n     int mn = 1 << 29; n     int a, b; n     for (int i = 0; i < 26; i++) n       for (int j = 0; j < 26; j++) n         if (j != i) { n           if (mn > memo[m - 1][i][j]) mn = memo[m - 1][i][j], a = i, b = j; n         } n     printf( %d n , mn); n     for (int row = m - 1; row >= 0; row--) { n       for (int i = 0; i < n; i++) { n         if ((i & 1) == 0) n           cout << char(a +  a ); n         else n           cout << char(b +  a ); n       } n       cout << endl; n       int tmp = son[row][a][b]; n       a = tmp / 1000; n       b = tmp % 1000; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct edge { n   int to, next; n } e[200005 * 2]; n int d[200005], dl[200005], dr[200005], dd[200005]; n int head[200005], q[200005], id[200005], a[200005]; n long long ans; n int n, tot; n bool cmp(int x, int y) { n   if (dd[x] != dd[y]) return dd[x] > dd[y]; n   return dl[x] > dl[y]; n } n void add(int x, int y) { n   e[++tot] = (edge){y, head[x]}; n   head[x] = tot; n } n void bfs(int S, int *d) { n   for (int i = 1; i <= n; i++) d[i] = -1; n   d[S] = 0; n   q[1] = S; n   for (int h = 0, t = 1; h != t;) n     for (int x = q[++h], i = head[x]; i; i = e[i].next) n       if (d[e[i].to] == -1) d[e[i].to] = d[x] + a[e[i].to], q[++t] = e[i].to; n } n int findmax(int *d) { n   int mx = 1; n   for (int i = 2; i <= n; i++) n     if (d[mx] < d[i]) mx = i; n   return mx; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     add(x, y); n     add(y, x); n   } n   for (int i = 1; i <= n; i++) a[i] = 1; n   bfs(1, d); n   int l = findmax(d); n   bfs(l, dl); n   int r = findmax(dl); n   bfs(r, dr); n   for (int i = 1; i <= n; i++) n     if (dl[i] + dr[i] == dl[r]) n       a[i] = 0; n     else n       ans += max(dl[i], dr[i]); n   ans += 1ll * dl[r] * (dl[r] + 1) / 2; n   printf( %I64d n , ans); n   bfs(l, dd); n   for (int i = 1; i <= n; i++) id[i] = i; n   sort(id + 1, id + n + 1, cmp); n   for (int i = 1; i < n; i++) n     if (dd[id[i]]) { n       int tmp = dl[id[i]] > dr[id[i]] ? l : r; n       printf( %d %d %d n , tmp, id[i], id[i]); n     } else n       printf( %d %d %d n , l, id[i], id[i]); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 300010, INF = 0x3f3f3f3f; n template <typename Tp> n inline int getmin(Tp &x, Tp y) { n   return y < x ? x = y, 1 : 0; n } n template <typename Tp> n inline int getmax(Tp &x, Tp y) { n   return y > x ? x = y, 1 : 0; n } n template <typename Tp> n inline void read(Tp &x) { n   x = 0; n   int f = 0; n   char ch = getchar(); n   while (ch !=  -  && (ch <  0  || ch >  9 )) ch = getchar(); n   if (ch ==  - ) f = 1, ch = getchar(); n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   if (f) x = -x; n } n struct data { n   int v, nxt; n } edge[maxn << 1]; n int n, p, k, tot, head[maxn], w[maxn], mark[maxn]; n long long mx = -INF, f[maxn]; n void insert(int u, int v) { n   edge[++p] = (data){v, head[u]}; n   head[u] = p; n   edge[++p] = (data){u, head[v]}; n   head[v] = p; n } n void input() { n   int u, v; n   read(n); n   for (int i = 1; i <= n; i++) read(w[i]); n   for (int i = 1; i < n; i++) { n     read(u); n     read(v); n     insert(u, v); n   } n } n void dp(int x, int pre) { n   f[x] = w[x]; n   for (int i = head[x]; i; i = edge[i].nxt) n     if (edge[i].v ^ pre) { n       dp(edge[i].v, x); n       if (f[edge[i].v] > 0) f[x] += f[edge[i].v], mark[edge[i].v] = 1; n     } n   getmax(mx, f[x]); n } n void getans(int x, int pre) { n   f[x] = w[x]; n   for (int i = head[x]; i; i = edge[i].nxt) n     if (edge[i].v ^ pre) { n       getans(edge[i].v, x); n       if (f[edge[i].v] > 0) f[x] += f[edge[i].v], mark[edge[i].v] = 1; n     } n   if (f[x] == mx) ++k, f[x] = 0; n } n int main() { n   input(); n   dp(1, 1); n   getans(1, 1); n   printf( %lld %d n , mx * k, k); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int maxn = 100000 + 5; n const long double eps = 1e-6; n long double PI = acos((long double)(-1)); n long long n, m, par[maxn][20], cnt[maxn], st[maxn], fi[maxn], ti; n long long h[maxn]; n long double qd[maxn], qu[maxn]; n vector<int> e[maxn]; n void dfs(int u, int p = -1) { n   st[u] = ti++; n   h[u] = ~p ? h[p] + 1 : 0; n   par[u][0] = p; n   bool leaf = 1; n   for (int i = 1; i < 20; i++) { n     if (~par[u][i - 1]) par[u][i] = par[par[u][i - 1]][i - 1]; n   } n   int sz = e[u].size(); n   for (int i = 0; i < sz; i++) { n     int v = e[u][i]; n     if (v != p) { n       leaf = 0; n       dfs(v, u); n       cnt[u] += cnt[v] + 1; n     } n   } n   for (int i = 0; i < sz; i++) { n     int v = e[u][i]; n     if (v != p) qd[u] += 1.0 * cnt[v] / cnt[u] * qd[v]; n   } n   if (~leaf) qd[u] += 1; n   fi[u] = ti++; n } n void dfs_d(int u, int p = -1) { n   if (~p) { n     long double cntd = cnt[p] - cnt[u] - 1, nou, kcntd, kasr; n     if (cntd == 0) { n       kcntd = 1.0 / (n - cnt[u] - 1); n       qu[u] = qu[p] * kcntd * (n - cnt[p] - 1) + (n - cnt[p]) * kcntd; n     } else { n       kcntd = 1.0 / cntd; n       nou = qd[p] * kcntd * cnt[p] - qd[u] * kcntd * cnt[u] - cnt[u] * kcntd - n             kcntd; n       kasr = 1.0 / (n - cnt[u] - 1); n       qu[u] = cntd * kasr * nou + qu[p] * kasr * (n - cnt[p] - 1) + 1; n     } n   } n   int sz = e[u].size(); n   for (int i = 0; i < sz; i++) { n     int v = e[u][i]; n     if (v != p) dfs_d(v, u); n   } n } n int ispar(int u, int v) { return st[u] <= st[v] && fi[v] <= fi[u]; } n int up(int u, int d) { n   for (int i = 20 - 1; i >= 0; i--) n     if ((d >> i) & 1) u = par[u][i]; n   return u; n } n int lca(int u, int v) { n   if (h[u] < h[v]) swap(u, v); n   u = up(u, h[u] - h[v]); n   if (u == v) return v; n   for (int i = 20 - 1; i >= 0; i--) { n     if (par[u][i] != par[v][i]) u = par[u][i], v = par[v][i]; n   } n   return par[u][0]; n } n long double query(int u, int v) { n   if (h[u] > h[v]) swap(u, v); n   if (ispar(u, v)) { n     int w = up(v, h[v] - h[u] - 1); n     long double cn = 1.0 / (cnt[v] + 1) / (n - cnt[w] - 1); n     long double ans = cnt[v] * (n - cnt[w] - 1) * cn * qd[v] + n                       (n - cnt[w] - 1) * (cnt[v] + 1) * cn * qu[w]; n     return ans + h[v] - h[u]; n   } n   int w = lca(u, v); n   long double cn = 1.0 / (cnt[u] + 1) / (cnt[v] + 1); n   long double ans = n       cnt[u] * (cnt[v] + 1) * cn * qd[u] + cnt[v] * (cnt[u] + 1) * cn * qd[v]; n   return h[u] - h[w] + h[v] - h[w] + ans + 1; n } n int main() { n   std::ios_base::sync_with_stdio(false); n   cin >> n >> m; n   int u, v; n   for (int i = 0; i < n - 1; i++) { n     cin >> u >> v; n     u--; n     v--; n     e[u].push_back(v); n     e[v].push_back(u); n   } n   dfs(0); n   dfs_d(0); n   cout << fixed << setprecision(8); n   while (m--) { n     cin >> u >> v; n     cout << query(--u, --v) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = (int)(1e5) + 5; n int l[maxn], r[maxn]; n int main() { n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> l[i] >> r[i]; n   sort(l + 1, l + n + 1); n   sort(r + 1, r + n + 1); n   long long ret = n; n   for (int i = 1; i <= n; ++i) ret += max(l[i], r[i]); n   cout << ret << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n ostream &operator<<(ostream &out, string str) { n   for (char c : str) out << c; n   return out; n } n template <class L, class R> n ostream &operator<<(ostream &out, pair<L, R> p) { n   return out <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <class T> n auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { n   out <<  { ; n   for (auto &e : x) out << e << (&e == &*--x.end() ?    :  ,  ); n   return out <<  } ; n } n template <class... Args> n void dump(Args &&...args) { n   ((cerr << args <<  ;   ), ...); n } n template <class T> n int size(T &&a) { n   return (int)a.size(); n } n using LL = long long; n using PII = pair<int, int>; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   for (int _t = 0; _t < t; _t++) { n     int l, r; n     cin >> l >> r; n     if (2 * l > r) n       cout <<  -1 -1 n ; n     else n       cout << l <<     << 2 * l <<   n ; n   } n } n


#include <bits/stdc++.h> n char str[100005], ans[100005]; n int out[100005]; n struct Edge { n   int link[400005], next[400005], son[400005], l; n   void clear() { n     l = 0; n     memset(son, 0, sizeof(son)); n   } n   void addedge(int x, int y) { n     link[++l] = y; n     next[l] = son[x]; n     son[x] = l; n   } n }; n Edge e; n int top = 0; n int main() { n   scanf( %s , str); n   int l = strlen(str); n   for (int i = l - 1; i >= 0; --i) e.addedge(str[i], i); n   for (char c =  z ; c >=  a ; --c) n     for (int p = e.son[c]; p; p = e.next[p]) n       if (top == 0 || e.link[p] > out[top]) out[++top] = e.link[p]; n   for (int i = 1; i <= top; ++i) ans[i - 1] = str[out[i]]; n   printf( %s n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0), cin.tie(0); n   int arr[4]; n   for (int i = 0; i < (int)4; i++) cin >> arr[i]; n   sort(arr, arr + 4); n   bool ok = 0; n   for (int mask = 0; mask < (1 << 4); mask++) { n     int sum1 = 0; n     int sum2 = 0; n     for (int bit = 0; bit < 4; bit++) { n       if (mask & (1 << bit)) { n         sum1 += arr[bit]; n       } else { n         sum2 += arr[bit]; n       } n     } n     if (sum1 == sum2) { n       ok = 1; n       break; n     } n   } n   if (ok) n     cout <<  YES  <<   n ; n   else n     cout <<  NO  <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n inline int read() { n   int s = 0, w = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) w = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) s = s * 10 + ch -  0 , ch = getchar(); n   return s * w; n } n inline long long readll() { n   long long s = 0, w = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) w = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) s = s * 10 + ch -  0 , ch = getchar(); n   return s * w; n } n using namespace std; n int a[1000003]; n long long w[1000003], ans[1000003]; n long long qp(long long x, long long y) { n   if (y == 1) return x; n   if (y & 1) { n     long long t = qp(x, y >> 1); n     return t * t % 998244353LL * x % 998244353LL; n   } n   long long t = qp(x, y >> 1); n   return t * t % 998244353LL; n } n int main() { n   int n = read(); n   for (int i = 1; i <= n; i++) a[i] = read(); n   sort(a + 1, a + n + 1); n   for (int i = 1; i <= n; i++) w[i] = w[i - 1] + a[i]; n   for (int i = 1; i < n; i++) n     for (int j = i; j > 0; j -= (n - i)) ans[i] += w[j], ans[i] %= 998244353LL; n   for (int i = n - 1; i >= 0; i--) n     printf( %lld  , ans[i] * qp(n, 998244351LL) % 998244353LL); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[40005]; n int main() { n   int i, j; n   int n, m, x; n   while (scanf( %d %d , &n, &m) != EOF) { n     if ((n == 1 || n == 2) && m == 1) { n       printf( -1 n ); n       continue; n     } n     if (n < (2 * m + 1)) { n       if (m == 1) { n         printf( %d n , n * m); n         for (i = 1; i <= n; i++) { n           for (j = 1; j <= m; j++) { n             x = i + j; n             if (x > n) { n               x = x - n; n             } n             printf( %d %d n , i, x); n           } n         } n       } else n         printf( -1 n ); n       continue; n     } n     printf( %d n , n * m); n     for (i = 1; i <= n; i++) { n       for (j = 1; j <= m; j++) { n         x = i + j; n         if (x > n) { n           x = x - n; n         } n         printf( %d %d n , i, x); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool collinear(int x1, int y1, int x2, int y2, int x3, int y3) { n   return (y1 - y2) * (x1 - x3) == (y1 - y3) * (x1 - x2); n } n int main() { n   int n, x, y; n   cin >> n; n   vector<pair<int, int> > points; n   for (int i = 0; i < n; i++) { n     cin >> x >> y; n     points.push_back(make_pair(x, y)); n   } n   long long ans = 0; n   for (int i = 0; i < n - 2; i++) n     for (int j = i + 1; j < n - 1; j++) n       for (int k = j + 1; k < n; k++) { n         if (!collinear(points[i].first, points[i].second, points[j].first, n                        points[j].second, points[k].first, points[k].second)) n           ans++; n       } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int Get() { n   char c; n   while (c = getchar(), c <  0  || c >  9 ) n     ; n   int X = c - 48; n   while (c = getchar(), c >=  0  && c <=  9 ) X = X * 10 + c - 48; n   return X; n } n int Pow(int A, int B, int Mod) { n   if (!B) return 1; n   int T = Pow((long long)A * A % Mod, B / 2, Mod); n   if (B & 1) T = (long long)T * A % Mod; n   return T; n } n int main() { n   int N = Get(), M = Get(), Mod = Get(); n   static int G[600][600], In[600], Out[600]; n   memset(G, 0, sizeof(G)); n   memset(In, 0, sizeof(In)); n   memset(Out, 0, sizeof(Out)); n   for (int i = 0; i < M; i++) { n     int X = Get() - 1, Y = Get() - 1; n     G[X][Y]++; n     In[Y]++; n     Out[X]++; n   } n   static vector<int> List[600]; n   for (int i = 0; i < N; i++) n     for (int j = 0; j < N; j++) n       if (G[i][j]) List[i].push_back(j); n   static int Temp[600], Stack[600]; n   int Top = 0; n   for (int i = 0; i < N; i++) { n     Temp[i] = In[i]; n     if (!Temp[i]) Stack[Top++] = i; n   } n   static int Queue[600]; n   int Count = 0; n   while (Top--) { n     int V = Stack[Top]; n     Queue[Count++] = V; n     for (int i = 0; i < N; i++) n       if (G[V][i]) { n         Temp[i] -= G[V][i]; n         if (!Temp[i]) Stack[Top++] = i; n       } n   } n   static int P[600][600]; n   memset(P, 0, sizeof(P)); n   for (int i = 0; i < N; i++) { n     int V = Queue[i]; n     P[V][V] = 1; n     for (int j = i; j < N; j++) { n       int U = Queue[j]; n       if (!P[V][U]) continue; n       for (int k = 0; k < List[U].size(); k++) { n         int _U = List[U][k]; n         P[V][_U] = ((long long)P[V][U] * G[U][_U] + P[V][_U]) % Mod; n       } n     } n   } n   int K0 = 0, K1 = 0; n   static int X[600], Y[600]; n   for (int i = 0; i < N; i++) { n     if (!In[i] && Out[i]) X[K0++] = i; n     if (!Out[i] && In[i]) Y[K1++] = i; n   } n   int K = K0; n   static long long A[600][600]; n   for (int i = 0; i < K; i++) n     for (int j = 0; j < K; j++) A[i][j] = P[X[i]][Y[j]]; n   int Ans = 1; n   for (int i = 0; i < N; i++) n     if (!In[i] && !Out[i]) X[K0++] = Y[K1++] = i; n   for (int i = 0; i < K0; i++) n     for (int j = 0; j < K0; j++) n       if (X[i] < X[j] && Y[i] > Y[j]) Ans = -Ans; n   for (int i = 0; i < K; i++) { n     int P = i; n     while (P < K && !A[P][i]) P++; n     if (P == K) { n       printf( 0 n ); n       return 0; n     } n     if (P != i) { n       Ans = -Ans; n       for (int j = i; j < K; j++) swap(A[i][j], A[P][j]); n     } n     int Q = Pow(A[i][i], Mod - 2, Mod); n     for (int j = i + 1; j < K; j++) { n       int T = (long long)A[j][i] * Q % Mod; n       if (!T) continue; n       for (int k = i; k < K; k++) { n         A[j][k] = (A[j][k] - (long long)T * A[i][k]) % Mod; n         if (A[j][k] < 0) A[j][k] += Mod; n       } n     } n   } n   for (int i = 0; i < K; i++) Ans = (long long)Ans * A[i][i] % Mod; n   if (Ans < 0) Ans += Mod; n   printf( %d n , Ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int par[20][100001], dep[100001], lg[100001], sizes[100001]; n bool isParent[100001]; n vector<int> adj[100001]; n void dfs(int u) { n   for (int i = 0; i < adj[u].size(); i++) { n     int v = adj[u][i]; n     if (v != par[0][u]) { n       isParent[u] = true; n       dep[v] = dep[u] + 1; n       par[0][v] = u; n       dfs(v); n     } n   } n } n int subtree(int u) { n   if (sizes[u] == -1) { n     sizes[u] = 1; n     if (isParent[u]) { n       for (int i = 0; i < adj[u].size(); i++) { n         int v = adj[u][i]; n         if (v != par[0][u]) sizes[u] += subtree(v); n       } n     } n   } n   return sizes[u]; n } n void buildLCA(int root, int n) { n   memset(par, -1, sizeof par); n   memset(sizes, -1, sizeof sizes); n   memset(isParent, false, sizeof isParent); n   dep[root] = 0; n   dfs(root); n   for (int i = 2; i < n; i++) lg[i] = lg[i >> 1] + 1; n   for (int i = 1; (1 << i) < n; i++) n     for (int u = 0; u < n; u++) n       if ((1 << i) <= dep[u]) par[i][u] = par[i - 1][par[i - 1][u]]; n   subtree(root); n } n int climb(int u, int k) { n   if (dep[u] < k) return -1; n   for (; k > 0; k -= (1 << lg[k])) u = par[lg[k]][u]; n   return u; n } n int lca(int a, int b) { n   if (dep[a] < dep[b]) n     b = climb(b, dep[b] - dep[a]); n   else n     a = climb(a, dep[a] - dep[b]); n   for (int k = lg[dep[a]]; k >= 0 && a != b; k--) n     if (par[k][a] != par[k][b]) { n       a = par[k][a]; n       b = par[k][b]; n     } n   if (a != b && dep[a] > 0) { n     a = par[0][a]; n     b = par[0][b]; n   } n   return a == b ? a : -1; n } n int dist(int u, int v) { n   int L = lca(u, v); n   return L == -1 ? -1 : dep[u] + dep[v] - 2 * dep[L]; n } n int main() { n   int n, m; n   cin >> n; n   for (int i = 1; i < n; i++) { n     int a, b; n     cin >> a >> b; n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   buildLCA(1, n + 1); n   cin >> m; n   while (m--) { n     int a, b; n     cin >> a >> b; n     if (a != b) { n       int l = lca(a, b), distance = dist(a, b); n       if (distance % 2 == 0) { n         if (dep[a] != dep[b]) { n           int deepernode; n           if (dep[a] > dep[b]) n             deepernode = a; n           else n             deepernode = b; n           int mid1 = climb(deepernode, distance / 2), n               mid2 = climb(deepernode, distance / 2 - 1); n           cout << sizes[mid1] - sizes[mid2] << endl; n         } else { n           int aa = climb(a, dist(a, l) - 1), bb = climb(b, dist(a, l) - 1); n           cout << n - sizes[aa] - sizes[bb] << endl; n         } n       } else n         cout << 0 << endl; n     } else n       cout << n << endl; n   } n } n