#include <bits/stdc++.h> n using namespace std; n int a[500000 + 5], ans[500000 + 5]; n int main() { n   int n, k, d; n   cin >> n >> k >> d; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a, a + n); n   ans[n + 1] = 0; n   ans[n] = 1; n   int lptr = n - 1, dptr = n - 1; n   for (int i = n - 1; i >= 0; i--) { n     while (lptr > 0 && lptr - i > k - 1) lptr--; n     while (dptr > 0 && a[dptr] - a[i] > d) dptr--; n     if (ans[lptr + 1] - ans[dptr + 2] > 0 && lptr - i == k - 1) ans[i]++; n     ans[i] += ans[i + 1]; n   } n   if (ans[0] - ans[1] == 1) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   cerr.tie(nullptr); n   int n, p, k; n   cin >> n >> p >> k; n   map<int, int> mp; n   long long sol = 0; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     int y = x * 1ll * x % p; n     y = y * 1ll * y % p; n     y = ((y - k * 1ll * x) % p + p) % p; n     sol += mp[y]++; n   } n   cout << sol <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const long double pi = 3.1415926535897932384626433832795l; n template <typename T> n inline auto sqr(T x) -> decltype(x * x) { n   return x * x; n } n template <typename T1, typename T2> n inline bool umx(T1& a, T2 b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n template <typename T1, typename T2> n inline bool umn(T1& a, T2 b) { n   if (b < a) { n     a = b; n     return 1; n   } n   return 0; n } n const int K = 5; n const int INF = 1e9; n struct node { n   int t[K][K]; n   node() { n     for (int i = int(0); i < int(K); ++i) n       for (int j = int(0); j < int(K); ++j) t[i][j] = INF; n     for (int i = int(0); i < int(K); ++i) t[i][i] = 0; n   } n }; n const int N = 200100; n node t[4 * N]; n int sz; n node botva(node a, node b) { n   node c; n   for (int i = int(0); i < int(K); ++i) n     for (int j = int(i); j < int(K); ++j) { n       c.t[i][j] = INF; n       for (int k = int(i); k < int(j + 1); ++k) n         umn(c.t[i][j], a.t[i][k] + b.t[k][j]); n     } n   return c; n } n node fnd(int i, int x, int y, int l, int r) { n   if (x > r || y < l) return node(); n   if (x >= l && y <= r) { n     return t[i]; n   } n   return botva(fnd(2 * i, x, (x + y) >> 1, l, r), n                fnd(2 * i + 1, ((x + y) >> 1) + 1, y, l, r)); n } n node fnd(int l, int r) { n   l += sz, r += sz; n   if (l == r) { n     return t[l]; n   } n   node left = t[l], right = t[r]; n   while (l / 2 != r / 2) { n     if (!(l & 1)) { n       left = botva(left, t[l + 1]); n     } n     if (r & 1) { n       right = botva(t[r - 1], right); n     } n     l >>= 1, r >>= 1; n   } n   return botva(left, right); n } n char c[N]; n int main() { n   cout.setf(ios::showpoint | ios::fixed); n   cout.precision(20); n   int n, q; n   cin >> n >> q; n   string s; n   getline(cin, s); n   gets(c); n   s = c; n   string g =  2017 ; n   sz = 1; n   while (sz < n) sz <<= 1; n   for (int i = int(0); i < int(n); ++i) { n     node& v = t[i + sz]; n     for (int j = int(0); j < int(((int)(g).size())); ++j) { n       if (g[j] == s[i]) { n         v.t[j][j] = 1; n         v.t[j][j + 1] = 0; n       } n     } n     if (s[i] ==  6 ) { n       v.t[3][3] = v.t[4][4] = 1; n     } n   } n   for (int i = int(sz) - 1; i >= int(1); --i) { n     t[i] = botva(t[2 * i], t[2 * i + 1]); n   } n   for (int i = int(0); i < int(q); ++i) { n     int l, r; n     scanf( %d%d , &l, &r); n     l--, r--; n     node res = fnd(1, 0, sz - 1, l, r); n     if (res.t[0][4] >= INF) { n       printf( -1 n ); n     } else { n       printf( %d n , res.t[0][4]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n #pragma GCC optimize(3) n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( inline ) n #pragma GCC optimize( -fgcse ) n #pragma GCC optimize( -fgcse-lm ) n #pragma GCC optimize( -fipa-sra ) n #pragma GCC optimize( -ftree-pre ) n #pragma GCC optimize( -ftree-vrp ) n #pragma GCC optimize( -fpeephole2 ) n #pragma GCC optimize( -ffast-math ) n #pragma GCC optimize( -fsched-spec ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC optimize( -falign-jumps ) n #pragma GCC optimize( -falign-loops ) n #pragma GCC optimize( -falign-labels ) n #pragma GCC optimize( -fdevirtualize ) n #pragma GCC optimize( -fcaller-saves ) n #pragma GCC optimize( -fcrossjumping ) n #pragma GCC optimize( -fthread-jumps ) n #pragma GCC optimize( -funroll-loops ) n #pragma GCC optimize( -fwhole-program ) n #pragma GCC optimize( -freorder-blocks ) n #pragma GCC optimize( -fschedule-insns ) n #pragma GCC optimize( inline-functions ) n #pragma GCC optimize( -ftree-tail-merge ) n #pragma GCC optimize( -fschedule-insns2 ) n #pragma GCC optimize( -fstrict-aliasing ) n #pragma GCC optimize( -fstrict-overflow ) n #pragma GCC optimize( -falign-functions ) n #pragma GCC optimize( -fcse-skip-blocks ) n #pragma GCC optimize( -fcse-follow-jumps ) n #pragma GCC optimize( -fsched-interblock ) n #pragma GCC optimize( -fpartial-inlining ) n #pragma GCC optimize( no-stack-protector ) n #pragma GCC optimize( -freorder-functions ) n #pragma GCC optimize( -findirect-inlining ) n #pragma GCC optimize( -fhoist-adjacent-loads ) n #pragma GCC optimize( -frerun-cse-after-loop ) n #pragma GCC optimize( inline-small-functions ) n #pragma GCC optimize( -finline-small-functions ) n #pragma GCC optimize( -ftree-switch-conversion ) n #pragma GCC optimize( -foptimize-sibling-calls ) n #pragma GCC optimize( -fexpensive-optimizations ) n #pragma GCC optimize( -funsafe-loop-optimizations ) n #pragma GCC optimize( inline-functions-called-once ) n #pragma GCC optimize( -fdelete-null-pointer-checks ) n using namespace std; n const int N = 2e5 + 9; n int x[N], t, n, k; n char buf[1 << 21], *p1 = buf, *p2 = buf; n inline int getc() { n   return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) n              ? EOF n              : *p1++; n } n inline int read() { n   int ret = 0, f = 0; n   char ch = getc(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = 1; n     ch = getc(); n   } n   while (isdigit(ch)) { n     ret = ret * 10 + ch - 48; n     ch = getc(); n   } n   return f ? -ret : ret; n } n char Buf[1 << 21], out[20]; n int P, Size = -1; n inline void flush() { n   fwrite(Buf, 1, Size + 1, stdout); n   Size = -1; n } n inline void write(int x, char ch) { n   if (Size > 1 << 20) flush(); n   if (x < 0) Buf[++Size] = 45, x = -x; n   do { n     out[++P] = x % 10 + 48; n   } while (x /= 10); n   do { n     Buf[++Size] = out[P]; n   } while (--P); n   Buf[++Size] = ch; n } n inline int max(int a, int b) { return a > b ? a : b; } n int main() { n   t = read(); n   while (t--) { n     n = read(), k = read(); n     for (register int i = 0; i < n; i++) *(x + i) = read(); n     for (register int i = 0; i < n; i++) read(); n     sort(x, x + n); n     int maxi = 0, opt = 0, fans = 0; n     for (register int i = 0; i < n; i++) { n       int curr = upper_bound(x + i, x + n, *(x + i) + k) - x - i; n       if (i) opt = x + i - lower_bound(x, x + i - 1, *(x + i - 1) - k); n       maxi = max(maxi, opt); n       curr += maxi; n       fans = max(fans, curr); n     } n     write(fans,   n ); n   } n   flush(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> knapsack(long long lim, vector<int> allItems) { n   vector<bool> mark; n   int mx = 0; n   for (auto it : allItems) mx += it; n   mx = min((long long)mx, lim); n   mark.assign(mx + 1, 0); n   mark[0] = true; n   for (auto it : allItems) { n     for (int i = (int)mx; i >= (int)it; i--) { n       mark[i] = max(mark[i], mark[i - it]); n     } n   } n   vector<int> ret; n   for (int i = (int)0; i <= (int)mx; i++) n     if (mark[i]) ret.push_back(i); n   return ret; n } n long long W; n long long cnt[9]; n long long max840 = 0; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> W; n   for (int i = (int)1; i <= (int)8; i++) cin >> cnt[i]; n   for (int i = (int)1; i <= (int)8; i++) { n     int blockSize = 840 / i; n     if (cnt[i] < blockSize) continue; n     max840 += cnt[i] / blockSize; n     cnt[i] %= blockSize; n     if (cnt[i] < blockSize) max840 -= 1, cnt[i] += blockSize; n   } n   vector<int> allItems; n   for (int i = (int)1; i <= (int)8; i++) { n     int rem = cnt[i]; n     for (int k = (int)0; k <= (int)60; k++) { n       if (rem < (1LL << k)) { n         allItems.push_back(rem * i); n         break; n       } else { n         rem -= (1LL << k); n         allItems.push_back((1LL << k) * i); n       } n     } n   } n   vector<int> allCreatedValue = knapsack(W, allItems); n   long long ans = 0; n   for (auto it : allCreatedValue) { n     long long left = W - it; n     left -= 1LL * min(left / 840, max840) * 840; n     ans = max(ans, W - left); n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long power(long long a, long long b) { n   long long result = 1; n   while (b > 0) { n     if (b % 2 == 1) { n       result *= a; n     } n     a *= a; n     b /= 2; n   } n   return result; n } n long long gcd(long long x, long long y) { n   long long r; n   while (y != 0 && (r = x % y) != 0) { n     x = y; n     y = r; n   } n   return y == 0 ? x : y; n } n long long countSetBits(long long x) { n   long long Count = 0; n   while (x > 0) { n     if (x & 1) Count++; n     x = x >> 1; n   } n   return Count; n } n bool isPerfectSquare(long long n) { n   long long sr = sqrt(n); n   if (sr * sr == n) n     return true; n   else n     return false; n } n long long mod(long long x, long long M) { return ((x % M + M) % M); } n long long add(long long a, long long b, long long M) { n   return mod(mod(a, M) + mod(b, M), M); n } n long long mul(long long a, long long b, long long M) { n   return mod(mod(a, M) * mod(b, M), M); n } n long long powerM(long long a, long long b, long long M) { n   long long res = 1; n   while (b) { n     if (b % 2 == 1) { n       res = mul(a, res, M); n     } n     a = mul(a, a, M); n     b /= 2; n   } n   return res; n } n long long modInverse(long long n, long long M) { return powerM(n, M - 2, M); } n long long nCrM(long long n, long long r, long long M) { n   if (n < r) return 0; n   if (r == 0) return 1; n   vector<long long> fact(n + 1); n   fact[0] = 1; n   for (long long i = 1; i <= n; i++) { n     fact[i] = mul(fact[i - 1], i, M); n   } n   return mul(mul(fact[n], modInverse(fact[r], M), M), n              modInverse(fact[n - r], M), M); n } n long long n; n vector<pair<long long, long long>> vv; n map<pair<long long, long long>, long long> vis; n long long ans = 0; n void dfs(pair<long long, long long> p) { n   vis[p] = 1; n   for (long long i = 0; i < n; i++) { n     if (vis[vv[i]] == 0) { n       if (vv[i].first == p.first || vv[i].second == p.second) { n         dfs(vv[i]); n       } n     } n   } n } n void solve() { n   cin >> n; n   for (long long i = 0; i < n; i++) { n     long long x1, y1; n     cin >> x1 >> y1; n     vv.push_back({x1, y1}); n   } n   for (long long i = 0; i < n; i++) { n     if (vis[vv[i]] == 0) { n       dfs(vv[i]); n       ans++; n     } n   } n   cout << ans - 1 <<   n ; n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long long n, k; n   scanf( %lld %lld , &n, &k); n   long long total(0), count(0); n   for (long long p = 1; p <= n; p++) { n     long long x; n     scanf( %lld , &x); n     if (p > 1 && total - x * (n - p) * (p - count - 1) < k) { n       printf( %lld n , p); n       ++count; n     } else { n       total += x * (p - 1 - count); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int IN() { n   int x = 0, ch = getchar(), f = 1; n   while (!isdigit(ch) && (ch !=  - ) && (ch != EOF)) ch = getchar(); n   if (ch ==  - ) { n     f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = (x << 1) + (x << 3) + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n struct Flower { n   long long x, y, da, db; n   bool operator<(const Flower& w) const { return da < w.da; } n } a, b, c[2005]; n long long Dist(Flower a, Flower b) { n   return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); n } n int n; n long long mxb[2005]; n int main() { n   n = IN(); n   a.x = 1ll * IN(), a.y = 1ll * IN(); n   b.x = 1ll * IN(), b.y = 1ll * IN(); n   for (int i = 1; i <= n; i++) { n     c[i].x = 1ll * IN(), c[i].y = 1ll * IN(); n     c[i].da = Dist(c[i], a); n     c[i].db = Dist(c[i], b); n   } n   sort(c + 1, c + n + 1); n   mxb[n + 1] = 0; n   for (int i = n; i >= 1; i--) mxb[i] = max(mxb[i + 1], c[i].db); n   long long Ans = mxb[1], ND = 0; n   for (int i = 2; i <= n + 1; i++) Ans = min(Ans, c[i - 1].da + mxb[i]); n   printf( %I64d n , Ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   for (int i = 0; i < t; ++i) { n     int n; n     cin >> n; n     string s; n     cin >> s; n     int sum(0), pos(-1), pos1(-1), ot(-1), num(0); n     for (int i = 0; i < s.length(); ++i) sum += (s[i] -  0 ); n     for (int i = 0; i < s.length(); ++i) { n       if ((s[i] -  0 ) % 2) { n         pos = i; n         num = s[i] -  0 ; n       } n     } n     for (int i = 0; i < s.length(); ++i) { n       if ((s[i] -  0 ) % 2 == 0) n         pos1 = i; n       else if ((s[i] -  0 ) % 2 && i != pos) n         ot = s[i] -  0 ; n     } n     if (n == 1 || pos == -1) n       cout << -1 <<   n ; n     else if (pos1 == -1) { n       if (n % 2 == 0) n         cout << s <<   n ; n       else { n         s.erase(s.begin() + 0); n         cout << s <<   n ; n       } n     } else if (ot != -1) n       cout << ot * 10 + num <<   n ; n     else if (ot == -1) n       cout << -1 <<   n ; n   } n } n


#include <bits/stdc++.h> n double a, b, c, d, x, y; n int main() { n   scanf( %lf%lf%lf%lf , &a, &b, &c, &d); n   x = a / b; n   y = c / d; n   printf( %lf , x / (1.0 - (1.0 - x) * (1.0 - y))); n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const ll OO = 1e18; n int di[8] = {0, 0, 1, -1, -1, 1, -1, 1}; n int dj[8] = {1, -1, 0, 0, 1, 1, -1, -1}; n string ys =  YES , no =  NO ; n void fast() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n } n string s1, s2; n int n, m, idx_shift; n const int N = 1001; n int mem[N][N]; n int dp(int i, int j) { n   if (i == s1.size()) return s2.size() - j; n   if (j == s2.size()) return s1.size() - i; n   int &rt = mem[i][j]; n   if (~rt) return rt; n   if (s1[i] == s2[j]) return rt = dp(i + 1, j + 1); n   int choice1 = 1 + dp(i, j + 1); n   int choice2 = 1 + dp(i + 1, j); n   int choice3 = 1 + dp(i + 1, j + 1); n   return rt = min({choice1, choice2, choice3}); n } n void build(int i, int j) { n   if (i == s1.size()) { n     int to_add = s2.size() - j; n     for (int k = 0; k < to_add; k++) n       cout <<  INSERT   << i + k + 1 + idx_shift <<     << s2[k + j] <<   n ; n     idx_shift += to_add; n     return; n   } n   if (j == s2.size()) { n     int to_delete = s1.size() - i; n     for (int k = 0; k < to_delete; k++) { n       cout <<  DELETE   << i + k + 1 + idx_shift <<   n ; n       --idx_shift; n     } n     return; n   } n   if (s1[i] == s2[j]) { n     build(i + 1, j + 1); n     return; n   } n   int choice1 = 1 + dp(i, j + 1); n   int choice2 = 1 + dp(i + 1, j); n   int choice3 = 1 + dp(i + 1, j + 1); n   int optimal = dp(i, j); n   if (choice1 == optimal) { n     cout <<  INSERT   << i + 1 + idx_shift <<     << s2[j] <<   n ; n     ++idx_shift; n     build(i, j + 1); n   } else if (choice2 == optimal) { n     cout <<  DELETE   << i + 1 + idx_shift <<   n ; n     --idx_shift; n     build(i + 1, j); n   } else { n     cout <<  REPLACE   << i + 1 + idx_shift <<     << s2[j] <<   n ; n     build(i + 1, j + 1); n   } n } n int main() { n   fast(); n   cin >> s1 >> s2; n   memset(mem, -1, sizeof mem); n   cout << dp(0, 0) <<   n ; n   idx_shift = 0; n   build(0, 0); n } n


#include <bits/stdc++.h> n using namespace std; n void solutionA(); n int main() { n   ios_base::sync_with_stdio(false); n   solutionA(); n   return EXIT_SUCCESS; n } n long long n, a, b, u[1024]; n double d[1024]; n struct point { n   double x, y; n   point(double x = 0, double y = 0) : x(x), y(y) {} n } s, f; n struct segment { n   point p1, p2; n } t[1024]; n point operator+(const point& p1, const point& p2) { n   return point(p1.x + p2.x, p1.y + p2.y); n } n point operator-(const point& p1, const point& p2) { n   return point(p1.x - p2.x, p1.y - p2.y); n } n istream& operator>>(istream& in, point& p) { n   in >> p.x >> p.y; n   return in; n } n ostream& operator<<(ostream& out, point& p) { n   out <<  (  << p.x <<  ,   << p.y <<  ) ; n   return out; n } n double dot(const point& p1, const point& p2) { n   return p1.x * p2.x + p1.y * p2.y; n } n double cross(const point& p1, const point& p2) { n   return p1.x * p2.y - p1.y * p2.x; n } n double dist(const point& p1, const point& p2) { n   double dx = p1.x - p2.x, dy = p1.y - p2.y; n   return sqrt(dx * dx + dy * dy); n } n double dist(const point& p, const segment& s) { n   double res = min(dist(p, s.p1), dist(p, s.p2)); n   double cos1 = dot(p - s.p1, s.p2 - s.p1); n   double cos2 = dot(p - s.p2, s.p1 - s.p2); n   if (fabs(cos1) > 1e-9 && cos1 < 0) return res; n   if (fabs(cos2) > 1e-9 && cos2 < 0) return res; n   double h = abs(cross(p - s.p1, s.p2 - s.p1)) / dist(s.p1, s.p2); n   res = min(res, h); n   return res; n } n double dist(const segment& s1, const segment& s2) { n   double res = min(dist(s1.p1, s2), dist(s1.p2, s2)); n   res = min(res, min(dist(s2.p1, s1), dist(s2.p2, s1))); n   return res; n } n void solve() { n   memset(u, 0, sizeof(long long) * 1024); n   for (long long i = 0; i < 1024; ++i) d[i] = 1e12; n   d[0] = 0; n   for (;;) { n     long long v = -1; n     for (long long i = 0; i < n + 2; ++i) { n       if (u[i]) continue; n       if (fabs(d[i] - 1e12) < 1e-9) continue; n       if (v == -1 || d[v] > d[i]) v = i; n     } n     if (v == -1 || v == 1) break; n     u[v] = 1; n     if (v == 0) { n       double l = dist(s, f); n       if (fabs(l - a) < 1e-9 || l < a) { n         d[1] = l; n       } n       for (long long i = 0; i < n; ++i) { n         l = dist(s, t[i]); n         if (fabs(l - a) > 1e-9 && l > a) continue; n         if (fabs(a - d[i + 2]) > 1e-9 && a < d[i + 2]) { n           d[i + 2] = a; n         } n       } n     } else { n       double l = dist(f, t[v - 2]); n       if (fabs(l - a) < 1e-9 || l < a) { n         double l2 = d[v] + b + l; n         if (fabs(l2 - d[1]) > 1e-9 && l2 < d[1]) { n           d[1] = l2; n         } n       } n       for (long long i = 0; i < n; ++i) { n         if (i + 2 == v) continue; n         l = dist(t[v - 2], t[i]); n         if (fabs(l - a) > 1e-9 && l > a) continue; n         double l2 = d[v] + a + b; n         if (fabs(l2 - d[i + 2]) > 1e-9 && l2 < d[i + 2]) { n           d[i + 2] = l2; n         } n       } n     } n   } n   if (fabs(d[1] - 1e12) < 1e-9) n     cout << -1 << endl; n   else n     cout << fixed << d[1] << endl; n } n void solutionA() { n   cout.precision(6); n   for (; cin >> a >> b;) { n     cin >> s >> f >> n; n     for (long long i = 0; i < n; ++i) cin >> t[i].p1 >> t[i].p2; n     solve(); n     cerr << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long sol(long long n, long long t) { n   long long ret = n * (n + 1) * (2 * n + 3 * t + 1); n   ret /= 6; n   return ret; n } n int main() { n   ios_base::sync_with_stdio(0); n   long long x; n   cin >> x; n   long long ans = 0; n   vector<pair<long long, long long> > aa; n   for (long long n = 1;; n++) { n     long long z = sol(n, 0); n     if (z > x) { n       break; n     } n     long long r = 6 * x - 2 * n * n * n - 3 * n * n - n; n     long long d = 3 * n * n + 3 * n; n     if (r % d == 0) { n       ans++; n       aa.push_back(make_pair(n, n + r / d)); n     } n   } n   long long xx = aa.size(); n   for (int i = 0; i < xx; i++) { n     if (aa[i].first != aa[i].second) n       aa.push_back(make_pair(aa[i].second, aa[i].first)); n   } n   cout << aa.size() <<   n ; n   sort(aa.begin(), aa.end()); n   for (int i = 0; i < aa.size(); i++) { n     cout << aa[i].first <<     << aa[i].second <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class c> n struct rge { n   c b, e; n }; n template <class c> n rge<c> range(c i, c j) { n   return rge<c>{i, j}; n } n template <class c> n auto dud(c* x) -> decltype(cerr << *x, 0); n template <class c> n char dud(...); n struct debug { n   template <class c> n   debug& operator<<(const c&) { n     return *this; n   } n }; n using ll = long long; n void solve() { n   string s; n   int a; n   cin >> a >> s; n   int sum = 0; n   vector<int> v; n   v.push_back(0); n   for (int i = s.size() - 1; i >= 0; i--) { n     if (s[i] ==  P ) n       sum++; n     else { n       v.push_back(sum); n       sum = 0; n     } n   } n   cout << *max_element(v.begin(), v.end()) << endl; n } n int main() { n   int t; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v[1000005]; n int ok[2][1000005]; n int main() { n   int q; n   cin >> q; n   while (q--) { n     int n; n     cin >> n; n     for (int i = 0; i < n; i++) ok[0][i] = 0; n     set<int> s; n     for (int i = 0; i < n; i++) s.insert(i); n     int cnt = 0; n     for (int i = 0; i < n; i++) { n       int x; n       cin >> x; n       v[i].clear(); n       for (int j = 0; j < x; j++) { n         int a; n         cin >> a; n         v[i].push_back(a - 1); n       } n       for (int j = 0; j < v[i].size(); j++) { n         if (s.count(v[i][j])) { n           ok[0][i] = ok[1][v[i][j]] = 1; n           s.erase(v[i][j]); n           cnt++; n           break; n         } n       } n     } n     if (cnt == n) n       cout <<  OPTIMAL  << endl; n     else { n       cout <<  IMPROVE  << endl; n       for (int i = n - 1; i >= 0; i--) { n         if (ok[0][i] == 0) { n           cout << (i + 1) <<     << (*s.rbegin() + 1) << endl; n           ; n           break; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, a[55], b[55], c, mx; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   cin >> m; n   for (int j = 1; j <= m; j++) { n     cin >> b[j]; n   } n   mx = 0; n   c = 1; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (b[j] / a[i] >= mx && b[j] % a[i] == 0) { n         if (b[j] / a[i] == mx) { n           c++; n         } n         if (b[j] / a[i] > mx) { n           c = 1; n         } n         mx = b[j] / a[i]; n       } n     } n   } n   cout << c << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC target( avx2 ) n #pragma GCC optimization( O3 ) n #pragma GCC optimization( unroll-loops ) n int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}; n int dy[] = {1, -1, 0, 0, -1, 1, 1, -1}; n const double PI = acos(-1), EPS = 1e-7; n const int OO = 0x3f3f3f3f, N = 1e7 + 5, mod = 1e9 + 7; n using namespace std; n long long gcd(long long x, long long y) { return (!y) ? x : gcd(y, x % y); } n long long lcm(long long x, long long y) { return ((x / gcd(x, y)) * y); } n void file() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n vector<long long> v; n int main() { n   std::ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   file(); n   long long n, d; n   long long x, y; n   cin >> n >> d; n   while (n--) { n     cin >> x >> y; n     if (x == d || y == d || x == 7 - d || y == 7 - d) { n       cout <<  NO  <<   n ; n       return 0; n     } n   } n   cout <<  YES  <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int months[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; n string itos(long long i) { n   string s =   ; n   while (i) { n     s += char(i % 10 +  0 ); n     i /= 10; n   } n   reverse(s.begin(), s.end()); n   return s; n } n long long stoi(string &s) { n   long long tot = 0; n   for (int i = (int)s.length() - 1, j = 1; i >= 0; i--, j *= 10) { n     tot += j * (s[i] +  0 ); n   } n   return tot; n } n bool cmp(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.first > b.first); n } n long long pwr(long long base, long long p, long long mod = 1000000007) { n   long long ans = 1; n   while (p) { n     if (p & 1) ans = (ans * base) % 1000000007; n     base = (base * base) % 1000000007; n     p /= 2; 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 lcm(long long a, long long b) { return (a * b * 1LL) / gcd(a, b); } n const int N = 1e6 + 5; n int n, m, k, cnt = 0; n long long dist[N]; n vector<pair<pair<int, long long>, int> > adj[N]; n priority_queue<pair<pair<long long, int>, int>, n                vector<pair<pair<long long, int>, int> >, n                greater<pair<pair<long long, int>, int> > > n     q; n void dijkstra(int src) { n   for (int i = 1; i < N; i++) dist[i] = 1e18; n   q.push(make_pair(make_pair(0, 0), src)); n   while (!q.empty()) { n     pair<pair<long long, int>, int> p = q.top(); n     q.pop(); n     int u = p.second; n     long long d = p.first.first; n     int t = p.first.second; n     if (dist[u] != 1e18) continue; n     dist[u] = d; n     if (t == 1) cnt++; n     for (int i = 0; i < adj[u].size(); i++) { n       int v = adj[u][i].first.first; n       long long w = adj[u][i].first.second; n       int t = adj[u][i].second; n       if (dist[v] > w + d) { n         q.push(make_pair(make_pair(w + d, t), v)); n       } n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cout << fixed << setprecision(10); n   clock_t clk; n   clk = clock(); n   cin >> n >> m >> k; n   for (int i = (1); i <= (m); ++i) { n     int x, y, w; n     cin >> x >> y >> w; n     adj[x].push_back(make_pair(make_pair(y, w), 0)); n     adj[y].push_back(make_pair(make_pair(x, w), 0)); n   } n   for (int i = (1); i <= (k); ++i) { n     int x, w; n     cin >> x >> w; n     adj[1].push_back(make_pair(make_pair(x, w), 1)); n     adj[x].push_back(make_pair(make_pair(1, w), 1)); n   } n   dijkstra(1); n   cnt = k - cnt; n   cout << cnt; n   clk = clock() - clk; n   cerr << fixed << setprecision(6) <<  Time:   << ((double)clk) / CLOCKS_PER_SEC n        <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<int> se; n struct zzz { n   int v, id; n } zz[1000005]; n bool cmp(const zzz &i, const zzz &j) { return i.v > j.v; } n int N, M, K; n char S[1000005], T[1000005], s[1000005]; n int z[1000005], p[2][1000005]; n void w(int td) { n   for (int i = 0; i < M; i++) s[i] = T[i]; n   s[M] =    ; n   for (int i = 0; i < N; i++) s[M + 1 + i] = S[i]; n   int L = 0, R = 0, n = N + M + 1; n   for (int i = 1; i < n; i++) { n     if (i > R) { n       L = R = i; n       while (R < n && s[R - L] == s[R]) R++; n       z[i] = R - L; n       R--; n     } else { n       int k = i - L; n       if (z[k] < R - i + 1) n         z[i] = z[k]; n       else { n         L = i; n         while (R < n && s[R - L] == s[R]) R++; n         z[i] = R - L; n         R--; n       } n     } n   } n   for (int i = 0; i < N; i++) zz[i].v = z[M + 1 + i], zz[i].id = i; n   sort(zz, zz + N, cmp); n   int i = -1; n   se.clear(); n   for (int x = M; x >= 0; x--) { n     while (i + 1 < N && zz[i + 1].v >= x) { n       i++; n       se.insert(zz[i].id); n     } n     if (se.lower_bound(K - x) == se.end()) n       p[td][x] = 1000000000; n     else n       p[td][x] = *se.lower_bound(K - x); n   } n } n int main() { n   scanf( %d%d%d , &N, &M, &K); n   scanf( %s%s , S, T); n   w(0); n   for (int i = 1; i <= N; i++) n     if (z[M + i] == M) { n       puts( Yes ); n       if (i + K + K - 1 <= N) n         printf( %d %d , i, i + K); n       else n         printf( %d %d , N - K - K + 1, N - K + 1); n       return 0; n     } n   reverse(S, S + N); n   reverse(T, T + M); n   w(1); n   for (int i = 1; i < M; i++) n     if (i <= K && M - i <= K && p[0][i] + i <= N - p[1][M - i] - (M - i)) { n       puts( Yes ); n       printf( %d %d , p[0][i] + i - K + 1, N - p[1][M - i] - (M - i) + 1); n       return 0; n     } n   puts( No ); n } n


#include <bits/stdc++.h> n const int N = 100005; n int n, fa[N], rt, cnt = 0, sz[N], son[N] = {0}, top[N], dep[N], idx = 0, Q, n                   dfn[N], head[N], hq[N], nodes = 0, year[N]; n int ret, L, R; n inline int readint() { n   int c = getchar(), d = 0; n   for (; !isdigit(c); c = getchar()) n     ; n   for (; isdigit(c); c = getchar()) d = (d << 3) + (d << 1) + (c ^  0 ); n   return d; n } n struct edge { n   int to, nxt; n } e[N << 1]; n struct SegmentTreeNode { n   int v, ls, rs, l, r, sz; n } d[N << 5]; n void BigMagician(int now) { n   sz[now] = 1; n   for (int i = head[now]; i; i = e[i].nxt) n     if (fa[e[i].to] == now) { n       dep[e[i].to] = dep[now] + 1; n       BigMagician(e[i].to); n       sz[now] += sz[e[i].to]; n       if (!son[now] || sz[e[i].to] > sz[son[now]]) son[now] = e[i].to; n     } n } n void dfs(int now) { n   dfn[now] = ++idx; n   hq[idx] = now; n   if (son[now]) top[son[now]] = top[now], dfs(son[now]); n   for (int i = head[now]; i; i = e[i].nxt) n     if (dep[now] < dep[e[i].to] && e[i].to != son[now]) n       dfs(top[e[i].to] = e[i].to); n } n void build(int l, int r, int& o) { n   o = ++nodes; n   d[o].l = l, d[o].r = r, d[o].sz = r - l + 1; n   if (l == r) n     d[o].v = 1; n   else { n     int mid = l + r >> 1; n     build(l, mid, d[o].ls); n     build(mid + 1, r, d[o].rs); n     d[o].v = d[d[o].ls].v + d[d[o].rs].v; n   } n } n void modify(int o, int& nw, int& p) { n   d[nw = ++nodes] = d[o]; n   --d[nw].v; n   if (d[o].l != d[o].r) { n     int mid = d[o].l + d[o].r >> 1; n     if (p <= mid) n       modify(d[o].ls, d[nw].ls, p); n     else n       modify(d[o].rs, d[nw].rs, p); n   } n } n void query(int& o, int& nw) { n   if (L <= d[o].l && d[o].r <= R) n     ret += d[o].v - d[nw].v; n   else { n     int mid = d[d[o].ls].r; n     if (L <= mid) query(d[o].ls, d[nw].ls); n     if (mid < R) query(d[o].rs, d[nw].rs); n   } n } n void query2(int& o, int& nw, int k) { n   if (d[o].l == d[o].r) n     ret = hq[d[o].l]; n   else { n     if (k <= d[d[o].rs].sz - d[d[o].rs].v + d[d[nw].rs].v) n       query2(d[o].rs, d[nw].rs, k); n     else n       query2(d[o].ls, d[nw].ls, n              k - (d[d[o].rs].sz - d[d[o].rs].v + d[d[nw].rs].v)); n   } n } n inline int theLCA(int x, int y) { n   for (; top[x] != top[y];) n     if (dep[top[x]] >= dep[top[y]]) n       x = fa[top[x]]; n     else n       y = fa[top[y]]; n   return (dep[x] < dep[y]) ? x : y; n } n inline int search_to_lca(int u, int v, int& k, int& y, int& nw) { n   while (top[u] != top[v]) { n     ret = 0; n     L = dfn[top[u]], R = dfn[u]; n     query(y, nw); n     ret = R - L + 1 - ret; n     if (ret >= k) { n       ret = 0; n       L = dfn[u] + 1, R = n; n       if (L <= n) query(y, nw); n       ret = R - L + 1 - ret; n       k += ret; n       ret = 0; n       query2(y, nw, k); n       return ret; n     } n     k -= ret; n     u = fa[top[u]]; n   } n   ret = 0; n   L = dfn[v], R = dfn[u]; n   query(y, nw); n   ret = R - L + 1 - ret; n   if (ret >= k) { n     ret = 0; n     L = dfn[u] + 1, R = n; n     if (L <= n) query(y, nw); n     ret = R - L + 1 - ret; n     k += ret; n     ret = 0; n     query2(y, nw, k); n     return ret; n   } n   k -= ret; n   return -1; n } n inline int search_on_a_list(int u, int v, int& y, int& nw) { n   int ans = 0; n   while (top[u] != top[v]) { n     ret = 0; n     L = dfn[top[u]], R = dfn[u]; n     query(y, nw); n     ret = R - L + 1 - ret; n     ans += ret; n     u = fa[top[u]]; n   } n   ret = 0; n   L = dfn[v], R = dfn[u]; n   query(y, nw); n   ret = R - L + 1 - ret; n   return ans + ret; n } n inline int search_from_lca(int u, int v, int& k, int& y, int& nw) { n   int all = search_on_a_list(u, v, y, nw); n   if (all < k) return -1; n   k = all - k + 1; n   while (top[u] != top[v]) { n     ret = 0; n     L = dfn[top[u]], R = dfn[u]; n     query(y, nw); n     ret = R - L + 1 - ret; n     if (ret >= k) { n       ret = 0; n       L = dfn[u] + 1, R = n; n       if (L <= n) query(y, nw); n       ret = R - L + 1 - ret; n       k += ret; n       ret = 0; n       query2(y, nw, k); n       return ret; n     } n     k -= ret; n     u = fa[top[u]]; n   } n   ret = 0; n   L = dfn[v], R = dfn[u]; n   query(y, nw); n   ret = R - L + 1 - ret; n   if (ret >= k) { n     ret = 0; n     L = dfn[u] + 1, R = n; n     if (L <= n) query(y, nw); n     ret = R - L + 1 - ret; n     k += ret; n     ret = 0; n     query2(y, nw, k); n     return ret; n   } n   k -= ret; n   return -1; n } n int main() { n   memset(head, 0, sizeof head); n   n = readint(); n   for (int i = 1; i <= n; ++i) { n     fa[i] = readint(); n     if (!fa[i]) n       rt = i; n     else { n       e[++cnt] = (edge){i, head[fa[i]]}; n       head[fa[i]] = cnt; n       e[++cnt] = (edge){fa[i], head[i]}; n       head[i] = cnt; n     } n   } n   dep[rt] = 1; n   top[rt] = rt; n   BigMagician(rt); n   dfs(rt); n   build(1, n, year[0]); n   Q = readint(); n   for (int i = 1; i <= Q; ++i) { n     int opt = readint(); n     if (opt == 1) { n       int p = readint(); n       modify(year[i - 1], year[i], dfn[p]); n     } else { n       year[i] = year[i - 1]; n       int u = readint(), v = readint(), k = readint(), y = readint(); n       ret = 0; n       L = R = dfn[u]; n       query(year[y], year[i]); n       if (!ret) ++k; n       int lca = theLCA(u, v); n       int ans = search_to_lca(u, lca, k, year[y], year[i]); n       if (!~ans) { n         ret = 0; n         L = R = dfn[lca]; n         query(year[y], year[i]); n         if (!ret) ++k; n         ans = search_from_lca(v, lca, k, year[y], year[i]); n       } n       if (ans == v || ans == u) ans = -1; n       printf( %d n , ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1005; n const int INF = 0x3f3f3f3f; n int n, a[MAXN]; n vector<int> ans; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     if (i != 1 && a[i] == 1) ans.push_back(a[i - 1]); n   } n   ans.push_back(a[n]); n   printf( %d n , ans.size()); n   for (auto i : ans) printf( %d  , i); n   return 0; n } n


#include <bits/stdc++.h> n int n, i, a[10001], p = 0; n int main() { n   scanf( %d , &n); n   for (i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (i = 1; i <= n; i++) { n     if (a[i] == 1) p++; n   } n   if (n > 1) { n     if (p == (n - 1)) n       printf( YES ); n     else n       printf( NO ); n   } else { n     if (p == 1) n       printf( YES ); n     else n       printf( NO ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int main() { n   cin >> n; n   if (n % 2 == 0) n     cout <<  white n  n          <<  1 2 n ; n   else n     cout <<  black n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 505; n const int dr[4] = {0, 0, -1, 1}; n const int dc[4] = {-1, 1, 0, 0}; n int n, K, mien, ans, res, t; n int sz[N * N], vis[N * N], c[N][N]; n char a[N][N]; n bool ok(int x, int y) { n   return x >= 1 && x <= n && y >= 1 && y <= n && a[x][y] ==  . ; n } n void dfs(int x, int y) { n   if (c[x][y]) return; n   c[x][y] = mien; n   sz[mien]++; n   for (int i = 0; i < 4; i++) n     if (ok(x + dr[i], y + dc[i])) dfs(x + dr[i], y + dc[i]); n } n void add(int x, int y) { n   if (ok(x, y) && vis[c[x][y]] != t) res += sz[c[x][y]], vis[c[x][y]] = t; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> K; n   for (int i = 1; i <= n; i++) cin >> a[i] + 1; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n       if (a[i][j] ==  .  && !c[i][j]) mien++, dfs(i, j); n   for (int x = 1; x + K - 1 <= n; x++) { n     for (int i = 0; i < K; i++) n       for (int j = 0; j < K; j++) sz[c[x + i][j + 1]]--; n     for (int y = 1; y + K - 1 <= n; y++) { n       res = 0; n       t++; n       for (int k = 0; k < K; k++) n         add(x - 1, y + k), add(x + K, y + k), add(x + k, y - 1), n             add(x + k, y + K); n       ans = max(ans, res); n       if (y + K - 1 != n) n         for (int i = 0; i < K; i++) sz[c[x + i][y]]++, sz[c[x + i][y + K]]--; n       else n         for (int i = 0; i < K; i++) n           for (int j = 0; j < K; j++) sz[c[x + i][n - j]]++; n     } n   } n   cout << ans + K * K; n   return 0; n } n


#include <bits/stdc++.h> n  n #define all(x) (x).begin(),(x).end() n #define sz(x) int(x.size()) n  n using namespace std; n using ll = long long; n using pii = pair<int, int>; n template<typename... T> void rd(T&... args) {((cin>>args), ...);} n template<typename... T> void wr(T... args) {((cout<<args<<   ), ...); cout<<  n ;} n  n int main() { n     ios::sync_with_stdio(false); n     cin.tie(nullptr); n     int tt; n     cin>>tt; n next: n     while (tt--) { n         int n, u, v; n         cin>>n>>u>>v; n         vector<int> a(n); n         for (auto& i : a) cin>>i; n         int ans=2e9; n         for (int i=1; i<n; i++) { n             if (abs(a[i]-a[i-1])>1) { n                 cout<< 0 n ; n                 goto next; n             } n             if (abs(a[i]-a[i-1])==1) ans=min(ans, min(u, v)); n         } n         ans=min(ans, v+min(u, v)); n         cout<<ans<<  n ; n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void kaboom() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n int main() { n   kaboom(); n   int t; n   cin >> t; n   while (t--) { n     long long int n; n     cin >> n; n     cout << n << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int name[100], i, j = 0; n   string codename; n   cin >> codename; n   for (i = 0; i < codename.length(); i++) { n     name[j] = (int)codename[i]; n     j++; n   } n   sort(name, name + j); n   int count = 0; n   for (int k = 1; k < j; k++) { n     if (name[k] != name[k - 1]) { n       count++; n     } n   } n   if (count % 2 == 0) { n     cout <<  IGNORE HIM! n ; n   } else { n     cout <<  CHAT WITH HER! n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, d; n   cin >> a >> b >> c >> d; n   if (max((3 * a / 10), a - a / 250 * c) == max((3 * b / 10), b - b / 250 * d)) n     cout <<  Tie ; n   else if (max((3 * a / 10), a - a / 250 * c) > n            max((3 * b / 10), b - b / 250 * d)) n     cout <<  Misha ; n   else if (max((3 * a / 10), a - a / 250 * c) < n            max((3 * b / 10), b - b / 250 * d)) n     cout <<  Vasya ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int a[100005]; n int main() { n   long long int n, b, k; n   cin >> k >> b >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   long long int ans = 0; n   if (b == 0) { n     for (int i = 0; i < n;) { n       long long int now = a[i]; n       long long int cnt = 0; n       while (i < n && a[i] == now) { n         i++; n         cnt++; n       } n       if (now == 0) { n         ans += (cnt * (cnt + 1) / 2); n       } n     } n     cout << ans << endl; n     return 0; n   } n   if (b == k - 1) b = 0; n   long long int ans2 = 0; n   if (b == 0) { n     for (int i = 0; i < n;) { n       long long int now = a[i]; n       long long int cnt = 0; n       while (i < n && a[i] == now) { n         i++; n         cnt++; n       } n       if (now == 0) { n         ans2 += (cnt * (cnt + 1) / 2); n       } n     } n   } n   map<long long int, long long int> mp; n   mp[0]++; n   for (int i = 0; i < n; i++) { n     if (i > 0) a[i] += a[i - 1]; n     long long int mod = a[i] % (k - 1); n     long long int need = (mod - b + k + k - 2) % (k - 1); n     if (mp.find(need) != mp.end()) { n       ans += mp[need]; n     } n     mp[mod]++; n   } n   cout << ans - ans2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int solve() { n   int n; n   cin >> n; n   int e = 0, o = 0; n   int x; n   for (int i = 0; i < n; i++) { n     cin >> x; n     if (i % 2 == 0 && x % 2 == 1) o++; n     if (i % 2 == 1 && x % 2 == 0) e++; n   } n   if (o == e) return o; n   return -1; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   for (int i = 0; i < t; i++) { n     cout << solve() <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using ll = long long; n using namespace std; n using pii = pair<ll, ll>; n ll mod = 998244353; n int get_p2(int n) { n   int i = 0; n   while ((1 << i) <= n) i++; n   return i - 1; n } n int main() { n   ios_base::sync_with_stdio(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     string a, b; n     cin >> a >> b; n     int need0 = 0, need1 = 0, nneed0 = 0, nneed1 = 0; n     for (int i = 0; i < n; i++) { n       if (a[i] != b[i]) { n         if (a[i] ==  0 ) n           need0++; n         else n           need1++; n       } else { n         if (a[i] ==  0 ) n           nneed0++; n         else n           nneed1++; n       } n     } n     int ans = -1; n     if (nneed1 - nneed0 == 1) ans = nneed1 + nneed0; n     if (need0 == need1) n       ans = (ans < 0 ? need0 + need1 : min(ans, need0 + need1)); n     cout << ans <<   n ; n   } n   cout.flush(); n } n


#include <bits/stdc++.h> n using namespace std; n const string sf[6] = { lios ,  liala ,  etr ,  etra ,  initis ,  inites }; n char a[1100000], s[1100000]; n int n, sd, p, num[3]; n int gender; n int suffix(int x) { n   int len = strlen(s); n   if (len < (int)sf[x].length()) return 0; n   for (int i = len - sf[x].length(); i < len; i++) n     if (s[i] != sf[x][i - len + sf[x].length()]) return 0; n   return 1; n } n int kind() { n   for (int k = 0; k < 6; k++) n     if (suffix(k)) { n       if (gender != -1) { n         if (gender != (k & 1)) return -1; n       } else n         gender = (k & 1); n       return k >> 1; n     } n   return -1; n } n void work() { n   memset(num, 0, sizeof(num)); n   n = strlen(a); n   s[0] = 0; n   sd = p = 0; n   gender = -1; n   while (scanf( %s , s) != EOF) { n     if (!strlen(s)) continue; n     if (sd) { n       printf( NO n ); n       return; n     } n     int k = kind(); n     if (k < 0) { n       printf( NO n ); n       return; n     } n     if (k == 0) { n       if (num[k + 1] || num[k + 2]) { n         printf( NO n ); n         return; n       } else n         num[k]++; n     } n     if (k == 1) { n       if (num[k] || num[k + 1]) { n         printf( NO n ); n         return; n       } else n         num[k]++; n     } n     if (k == 2) { n       if (!num[k - 1]) { n         if (!num[k - 2] && !num[k]) { n           sd = 1; n           num[k]++; n         } else { n           printf( NO n ); n           return; n         } n       } else n         num[k]++; n     } n   } n   if (num[0] + num[1] + num[2] > 1 && num[1] == 0) { n     printf( NO n ); n     return; n   } n   printf( YES n ); n } n int main() { n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int q, n, i, a[201], start, tmp, yn; n   cin >> q; n   while (q--) { n     cin >> n; n     for (i = 0; i < n; i++) { n       cin >> a[i]; n       if (a[i] == 1) start = i; n     } n     yn = 0; n     tmp = 1; n     for (i = 0; i < n - 1; i++) n       if (a[(start + i + 1) % n] != a[(start + i) % n] + 1) tmp = 0; n     yn += tmp; n     tmp = 1; n     for (i = 0; i < n - 1; i++) n       if (a[(1 + start + i + 1) % n] != a[(1 + start + i) % n] - 1) tmp = 0; n     yn += tmp; n     cout << (yn == 0 ?  NO n  :  YES n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n ifstream Cin( input.txt ); n ofstream Cout( output.txt ); n pair<bool, long long> Try(long long A, long long B, long long N) { n   long long err = 0, C; n   while (A > 0 && B > 0 && N > 0) { n     if (A == 1 && B != 1) { n       C = B - 1; n       A = B = 1; n     } else if (A >= B) { n       C = A / B; n       A %= B; n     } else { n       C = B / A; n       B %= A; n     } n     err += C - 1; n     N -= C; n   } n   return make_pair(N == 0 && A == 0 && B == 1, err); n } n string get(long long A, long long B, long long N) { n   string TMP; n   while (N) { n     N--; n     if (A >= B) { n       A -= B; n       TMP.push_back( T ); n     } else { n       B -= A; n       TMP.push_back( B ); n     } n   } n   reverse(TMP.begin(), TMP.end()); n   return TMP; n } n int main() { n   long long n, r, i, A, B, ER = (long long)1e15; n   pair<bool, long long> Cur; n   cin >> n >> r; n   for (i = 1; i <= r; i++) { n     Cur = Try(i, r, n); n     if (Cur.first) { n       if (Cur.second < ER) { n         ER = Cur.second; n         A = i; n         B = r; n       } n     } n     Cur = Try(r, i, n); n     if (Cur.first) { n       if (Cur.second < ER) { n         ER = Cur.second; n         A = r; n         B = i; n       } n     } n   } n   if (ER == (long long)1e15) n     cout <<  IMPOSSIBLE ; n   else n     cout << ER << endl << get(A, B, n); n } n


#include <bits/stdc++.h> n using namespace std; n int l; n char s[205]; n int exe(int a, int b) { n   int u = 0, r = 0; n   for (int i = a; i < b; i++) { n     switch (s[i]) { n       case  U : n         u++; n         break; n       case  D : n         u--; n         break; n       case  R : n         r++; n         break; n       case  L : n         r--; n         break; n     } n   } n   return u == 0 and r == 0; n } n int main() { n   scanf( %d n , &l); n   gets(s); n   int r = 0; n   for (int i = 0; i < l; i++) n     for (int j = i + 1; j <= l; j++) r += exe(i, j); n   printf( %d , r); n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000007; n void solve() { n   long long n; n   cin >> n; n   vector<long long> v(n); n   for (long long i = 0; i < n; ++i) { n     cin >> v[i]; n   } n   long long ans = 0; n   map<long long, long long> mp; n   for (long long i = 0; i < n; i++) { n     map<long long, long long> tp; n     long long cur = 0; n     for (long long j = i + 1; j < n; j++) { n       if (v[j] == v[i]) ans += cur; n       tp[v[j]]++; n       if (mp.count(v[j])) cur += mp[v[j]]; n     } n     mp[v[i]]++; n   } n   cout << ans <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   long long t = 1; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 201; n int n, a[N], cum[N][N], k, freq[N]; n vector<int> hs; n int calc(int l, int r) { n   priority_queue<int> qu; n   int res = 0; n   for (int i = l; i <= r; ++i) { n     qu.push(-hs[a[i]]); n     res += hs[a[i]]; n   } n   int sz = hs.size(), op = k; n   bool E = false; n   for (int i = sz - 1; i >= 0 && !E; --i) { n     int can = min(freq[i] - (cum[r + 1][i] - cum[l][i]), op); n     while (!qu.empty() && can > 0) { n       int v = -qu.top(); n       qu.pop(); n       if (v > hs[i]) { n         E = true; n         break; n       } n       res -= v; n       res += hs[i]; n       --op; n       --can; n     } n   } n   return res; n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &a[i]); n     hs.push_back(a[i]); n   } n   sort(hs.begin(), hs.end()); n   hs.resize(unique(hs.begin(), hs.end()) - hs.begin()); n   for (int i = 0; i < n; ++i) { n     a[i] = lower_bound(hs.begin(), hs.end(), a[i]) - hs.begin(); n     for (size_t j = 0; j < hs.size(); ++j) cum[i + 1][j] = cum[i][j]; n     ++cum[i + 1][a[i]]; n     ++freq[a[i]]; n   } n   int best = -2e9; n   for (int i = 0; i < n; ++i) n     for (int j = i; j < n; ++j) best = max(best, calc(i, j)); n   printf( %d n , best); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 5; n int n, x[2], need[N][2]; n pair<int, int> p[N]; n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> x[0] >> x[1]; n   for (int i = 0; i < n; i++) { n     int c; n     cin >> c; n     p[i] = {c, i}; n   } n   sort(p, p + n); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < 2; j++) { n       int c = p[i].first; n       need[i][j] = (x[j] + c - 1) / c; n     } n   for (int i = n - 1; i >= 0; i--) { n     if (i + need[i][0] < n && need[i][0] + need[i + need[i][0]][1] <= n - i) { n       vector<int> a, b; n       for (int j = i; j < i + need[i][0]; j++) a.push_back(p[j].second); n       for (int j = i + need[i][0]; j < i + need[i][0] + need[i + need[i][0]][1]; n            j++) n         b.push_back(p[j].second); n       cout <<  Yes n ; n       cout << a.size() <<     << b.size() <<   n ; n       for (int x : a) cout << x + 1 <<    ; n       cout <<   n ; n       for (int x : b) cout << x + 1 <<    ; n       cout <<   n ; n       return 0; n     } n     if (i + need[i][1] < n && need[i][1] + need[i + need[i][1]][0] <= n - i) { n       vector<int> a, b; n       for (int j = i; j < i + need[i][1]; j++) b.push_back(p[j].second); n       for (int j = i + need[i][1]; j < i + need[i][1] + need[i + need[i][1]][0]; n            j++) n         a.push_back(p[j].second); n       cout <<  Yes n ; n       cout << a.size() <<     << b.size() <<   n ; n       for (int x : a) cout << x + 1 <<    ; n       cout <<   n ; n       for (int x : b) cout << x + 1 <<    ; n       cout <<   n ; n       return 0; n     } n   } n   cout <<  No n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double Eps = 1e-9; n int Mod = 1000000000 + 7; n int n, m, q, k; n int Arr[100010]; n bool prime[10010]; n bool ok = true; n void sieve() { n   for (int i = 2; i * i <= 1e4; i++) { n     if (prime[i] == true) n       for (int j = i * 2; j <= 1e4; j++) { n         prime[j] = false; n       } n   } n } n int dp[100010]; n int cnt; n int main() { n   ios::sync_with_stdio(0); n   ; n   memset(prime, true, sizeof(prime)); n   sieve(); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cnt = 0; n     cin >> Arr[i]; n     if (Arr[i] & 1 && Arr[i] >= 9) { n       Arr[i] -= 9; n       cnt++; n     } n     if (Arr[i] % 4 == 0 && Arr[i] >= 0) { n       cout << cnt + Arr[i] / 4 << endl; n       continue; n     } else if ((Arr[i] - 6) % 4 == 0 && (Arr[i] - 6) >= 0) { n       cout << cnt + 1 + (Arr[i] - 6) / 4 << endl; n       continue; n     } n     cout << -1 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int N; n vector<int> fen, v; n void _update(int i) { n   while (i <= N) { n     fen[i]++; n     i += i & -i; n   } n } n int _sum(int i) { n   int ret = 0; n   while (i > 0) { n     ret += fen[i]; n     i -= i & -i; n   } n   return ret; n } n int _bin(int i) { n   int sum = _sum(i); n   int imin = 1, imax = i; n   while (imin <= imax) { n     int imid = (imin + imax) / 2; n     if (sum - _sum(imid - 1) >= v[i]) n       imin = imid + 1; n     else n       imax = imid - 1; n   } n   return imax; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int Q; n   cin >> N >> Q; n   v = vector<int>(N + 1); n   for (int n = 1; n <= N; ++n) { n     cin >> v[n]; n   } n   for (int n = 1; n <= N; ++n) { n     v[n] = n - v[n]; n   } n   vector<int> answer(Q); n   vector<vector<pair<int, int>>> _query(N + 1); n   for (int q = 0; q < Q; ++q) { n     int x, y; n     cin >> x >> y; n     _query[N - y].push_back({x + 1, q}); n   } n   fen = vector<int>(N + 1); n   for (int r = 1; r <= N; ++r) { n     if (v[r] == 0) { n       _update(r); n     } else if (v[r] > 0) { n       int tmp = _bin(r); n       if (tmp) _update(tmp); n     } n     for (auto x : _query[r]) { n       answer[x.second] = _sum(r) - _sum(x.first - 1); n     } n   } n   for (auto x : answer) cout << x <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[1010][105], po[1010], po2[1010]; n long long qui(long long a, long long n, long long m) { n   long long ans = 1; n   while (n) { n     if (n & 1) ans = (ans * a) % m; n     n >>= 1; n     a = (a * a) % m; n   } n   return ans; n }; n int main() { n   long long n, k, m; n   while (cin >> n >> k >> m) { n     po[0] = 1; n     for (int i = 1; i <= n; i++) po[i] = (po[i - 1] * 10) % m; n     po2[0] = 1; n     for (int i = 1; i <= n; i++) po2[i] = (po2[i - 1] * 10) % k; n     long long sum = 0; n     memset(dp, 0, sizeof(dp)); n     for (int i = 1; i < 10; i++) dp[1][i % k]++; n     if (n >= 2) sum = (sum + dp[1][0] * 9 * po[n - 2]) % m; n     for (int i = 1; i < n - 1; i++) { n       for (int j = 1; j < k; j++) n         for (int x = 0; x < 10; x++) n           dp[i + 1][(j + x * po2[i]) % k] = n               (dp[i + 1][(j + x * po2[i]) % k] + dp[i][j]) % m; n       for (int x = 1; x < 10; x++) n         dp[i + 1][x * po2[i] % k] = (dp[i + 1][x * po2[i] % k] + 1) % m; n       sum = (sum + ((dp[i + 1][0] * 9) % m) * po[n - i - 2]) % m; n     } n     if (n >= 2) { n       for (int j = 1; j < k; j++) n         for (int x = 1; x < 10; x++) n           dp[n][(j + x * po2[n - 1]) % k] = n               (dp[n][(j + x * po2[n - 1]) % k] + dp[n - 1][j]) % m; n       for (int x = 1; x < 10; x++) n         dp[n][x * po2[n - 1] % k] = (dp[n][x * po2[n - 1] % k] + 1) % m; n     } n     sum = (sum + dp[n][0] % m) % m; n     cout << sum << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   char c = getchar(); n   int t = 0, f = 1; n   while (c >  9  || c <  0 ) f = (c ==  - ) ? -1 : 1, c = getchar(); n   while (c >=  0  && c <=  9 ) t = t * 10 + c -  0 , c = getchar(); n   return t * f; n } n struct node { n   int L, R, po, id; n   bool operator<(const node &b) const { n     if (po == b.po) return R < b.R; n     return po < b.po; n   } n } p[200005]; n int n, Q, K, sq, a[200005], op[200005], id[200005], uid[200005], did[200005], n     cnt[200005]; n long long sum[200005], st[200005], ans[200005], now; n int main() { n   n = read(), K = read(), sq = sqrt(n); n   for (int i = 1; i <= n; i++) op[i] = (read() == 1) ? 1 : -1; n   for (int i = 1; i <= n; i++) n     a[i] = read(), st[i] = sum[i] = sum[i - 1] + a[i] * op[i]; n   st[n + 1] = 0, sort(st + 1, st + n + 2); n   int tp = unique(st + 1, st + n + 2) - st - 1; n   for (int i = 0; i <= n; i++) { n     id[i] = lower_bound(st + 1, st + tp + 1, sum[i]) - st; n     int up = lower_bound(st + 1, st + tp + 1, sum[i] + K) - st; n     if (st[up] == sum[i] + K) uid[i] = up; n     int down = lower_bound(st + 1, st + tp + 1, sum[i] - K) - st; n     if (st[down] == sum[i] - K) did[i] = down; n   } n   Q = read(); n   for (int i = 1, x, y, z; i <= Q; i++) n     x = read() - 1, y = read(), z = x / sq, p[i] = (node){x, y, z, i}; n   sort(p + 1, p + Q + 1); n   for (int i = p[1].L; i <= p[1].R; i++) now += cnt[did[i]], cnt[id[i]]++; n   ans[p[1].id] = now; n   for (int i = 2; i <= n; i++) { n     int L = p[i - 1].L, R = p[i - 1].R; n     while (R < p[i].R) R++, now += cnt[did[R]], cnt[id[R]]++; n     while (R > p[i].R) cnt[id[R]]--, now -= cnt[did[R]], R--; n     while (L > p[i].L) L--, now += cnt[uid[L]], cnt[id[L]]++; n     while (L < p[i].L) cnt[id[L]]--, now -= cnt[uid[L]], L++; n     ans[p[i].id] = now; n   } n   for (int i = 1; i <= Q; i++) printf( %lld n , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n long long int pow_mod(long long int a, long long int b) { n   long long int res = 1; n   while (b != 0) { n     if (b & 1) { n       res = (res * a) % 1000000007; n     } n     a = (a * a) % 1000000007; n     b /= 2; n   } n   return res; n } n void solve() { n   long long int h; n   cin >> h; n   long long int a[h + 1]; n   for (long long int i = 0; i < h + 1; i++) cin >> a[i]; n   bool f = true; n   for (long long int i = 1; i < h + 1; i++) { n     if (a[i] > 1 && a[i - 1] > 1) { n       f = false; n       break; n     } n   } n   if (f) n     cout <<  perfect n ; n   else { n     cout <<  ambiguous n ; n     long long int p1[h + 1]; n     long long int p2[h + 1]; n     long long int sz = 0; n     bool f = true; n     long long int capture = -1; n     for (long long int i = 0; i < h + 1; i++) { n       for (long long int j = 0; j < a[i]; j++) { n         p1[i] = sz; n         cout << sz <<    ; n       } n       if (i > 0 && a[i] > 1 && a[i - 1] > 1) capture = i; n       sz += a[i]; n     } n     cout << endl; n     for (long long int i = 0; i < h + 1; i++) { n       if (i == capture) cout << (p1[i] - 1) <<    ; n       for (long long int j = 0; j < a[i]; j++) { n         if (j == 0 && capture == i) continue; n         cout << p1[i] <<    ; n       } n     } n     cout << endl; n   } n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t; n   t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int y, a, b, c, d, temp; n   while (cin >> y) { n     temp = y + 1; n     while (temp != 0) { n       a = temp % 10; n       b = (temp % 100) / 10; n       c = (temp % 1000) / 100; n       d = temp / 1000; n       if (a == b || b == c || c == d || d == a || d == b || a == c) n         temp++; n       else n         break; n     } n     cout << temp << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using ll = long long int; n using namespace std; n template <class c> n struct rge { n   c b, e; n }; n template <class c> n rge<c> range(c i, c j) { n   return rge<c>{i, j}; n } n template <class c> n auto dud(c *x) -> decltype(cerr << *x, 0); n template <class c> n char dud(...); n struct debug { n   template <class c> n   debug &operator<<(const c &) { n     return *this; n   } n }; n template <typename T> n void ara_read(T &v, ll n) { n   ll temp; n   for (ll i = 0; i < n; i++) { n     scanf( %lld , &temp); n     v.emplace_back(temp); n   } n } n template <typename T> n void ara_print(T &v) { n   for (ll x : v) printf( %lld  , x); n   puts(  ); n } n const int INF = 1e9 + 99; n using Pair = pair<int, int>; n using vec = vector<ll>; n void bruteforce(int l, int r, vec &v) { n   return; n   set<ll> myset; n   for (ll x : v) { n     for (ll i = l; i <= r; i++) myset.insert(i + x); n   } n   printf( BruteForce : %d n , (int)myset.size()); n } n void improved(vec &v, vec &pref, int n) { n   ll l, r; n   scanf( %lld%lld , &l, &r); n   ll w = r - l + 1; n   ll ind = lower_bound(v.begin(), --v.end(), w) - v.begin(); n   ll ans = (n - ind) * w + pref[ind]; n   printf( %lld  , ans); n } n int main() { n   int n; n   scanf( %d , &n); n   vec v(n); n   for (ll &x : v) scanf( %lld , &x); n   sort(v.begin(), v.end()); n   for (int i = 0; i < n - 1; i++) v[i] = v[i + 1] - v[i]; n   sort(v.begin(), --v.end()); n   int q; n   scanf( %d , &q); n   vec pref(n); n   for (int i = 1; i < n; i++) pref[i] = pref[i - 1] + v[i - 1]; n   for (int i = 0; i < q; i++) improved(v, pref, n); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = (int)1e9 + 7; n const double EPS = 1e-9; n const int INF = INT_MAX; n const long long INFLL = (long long)1e18; n const double PI = acos(-1.0); n template <class T> n T gcd(T a, T b) { n   return b ? gcd(b, a % b) : a; n } n template <class T> n T lcm(T a, T b) { n   return a / gcd(a, b) * b; n } n const int N = (int)1e5 + 5; n const int P = 1 << 18; n int n, m; n int a[N]; n pair<int, int> g[N]; n int use[N]; n int main() { n   ios::sync_with_stdio(false); n   cout.setf(ios::fixed | ios::showpoint); n   cout.precision(8); n   cin >> n >> m; n   if (n == 1) return !printf( NO n ); n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     a[x]++; n     a[y]++; n     g[i] = make_pair(x, y); n   } n   int flag = 0; n   for (int i = 1; i <= n; i++) { n     if (a[i] < n - 1) { n       flag = 1; n       for (int j = 0; j < m; j++) { n         if (g[j].first == i) { n           use[g[j].second] = 1; n         } n         if (g[j].second == i) { n           use[g[j].first] = 1; n         } n       } n       int cur; n       for (int j = 1; j <= n; j++) { n         if (!use[j] && j != i) { n           cur = j; n           break; n         } n       } n       cout <<  YES n ; n       int st = 3; n       for (int j = 1; j <= n; j++) { n         if (j == i) { n           cout <<  1  ; n           continue; n         } n         if (j == cur) { n           cout <<  2  ; n           continue; n         } n         cout << st <<    ; n         st++; n       } n       cout <<   n ; n       st = 3; n       for (int j = 1; j <= n; j++) { n         if (j == i || j == cur) { n           cout <<  1  ; n           continue; n         } n         cout << st <<    ; n         st++; n       } n       return 0; n     } n   } n   if (!flag) return !printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> mp[105]; n int vis[105]; n struct node { n   int fa; n   int kid; n   node(int fa, int b) { n     this->fa = fa; n     kid = b; n   } n }; n bool bfs(int rt, int n) { n   queue<node> q; n   q.push(node(-1, rt)); n   vis[rt] = 1; n   while (!q.empty()) { n     int now = q.front().kid; n     int fa = q.front().fa; n     q.pop(); n     for (int i = 0; i < mp[now].size(); i++) { n       int v = mp[now][i]; n       if (vis[v] && v != fa) { n         vis[v]++; n         continue; n       } n       if (vis[v] || v == fa) continue; n       vis[v] = 1; n       q.push(node(now, v)); n     } n   } n   int f = 0; n   for (int i = 1; i <= n; i++) { n     if (vis[i] > 2) return false; n     if (vis[i] == 2) f = 1; n   } n   if (f) return true; n   return false; n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     mp[u].push_back(v); n     mp[v].push_back(u); n   } n   int yes = 0; n   int no = 0; n   for (int i = 1; i <= n; i++) { n     if (vis[i] == 0) { n       if (bfs(i, n)) { n         yes++; n       } else { n         no = 1; n       } n     } n   } n   if (yes == 1 && no == 0) n     puts( FHTAGN! ); n   else n     puts( NO ); n } n


#include <bits/stdc++.h> n int main() { n   int a; n   while (scanf( %d , &a) != EOF) printf( %d %d %d n , 0, 0, a); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int maxrob(int p, int f, int cnt_s, int cnt_w, int s, int w) { n   if (p > f) { n     swap(p, f); n   } n   if (s > w) { n     swap(cnt_s, cnt_w); n     swap(s, w); n   } n   long long s_total = (long long)cnt_s * s; n   if ((p + f) <= s_total) { n     return (p / s) + (f / s); n   } n   int w_cnt_max = min(((p + f) - s_total) / w, (long long)cnt_w); n   int answer = 0; n   for (int k = 0; k <= w_cnt_max; ++k) { n     if ((long long)w * k > p) break; n     int res = 0; n     int s_cnt_p = min((p - w * k) / s, cnt_s); n     int s_cnt_f = cnt_s - s_cnt_p; n     res += s_cnt_p; n     if ((long long)s * s_cnt_f > f) { n       s_cnt_f = (f / s); n     } n     res += s_cnt_f; n     res += min((p - s * s_cnt_p) / w + (f - s * s_cnt_f) / w, cnt_w); n     if (answer < res) answer = res; n   } n   return answer; n } n int main() { n   int T; n   cin >> T; n   while (T-- != 0) { n     int p, f; n     cin >> p >> f; n     int cnt_s, cnt_w; n     cin >> cnt_s >> cnt_w; n     int s, w; n     cin >> s >> w; n     cout << maxrob(p, f, cnt_s, cnt_w, s, w) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t = 1, n, k, fact, a, b; n   cin >> t; n   while (t--) { n     cin >> a >> b; n     if (a == 1) n       cout <<  0 n ; n     else if (a == 2) n       cout << b <<   n ; n     else n       cout << 2 * b <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int res[100010], k = 0; n struct edge { n   int id, v, w, next; n } e[100010]; n int head[100010], cnt = 0; n void adde(int u, int v, int w, int id) { n   e[cnt].id = id; n   e[cnt].v = v; n   e[cnt].w = w; n   e[cnt].next = head[u]; n   head[u] = cnt++; n } n int ind[100010]; n queue<int> q; n int lev[100010]; n int tl; n bool check(int lim) { n   memset(ind, 0, sizeof ind); n   memset(lev, 0, sizeof lev); n   tl = 0; n   while (!q.empty()) q.pop(); n   for (int i = 0; i < cnt; ++i) n     if (e[i].w > lim) ++ind[e[i].v]; n   for (int i = 1; i <= n; ++i) n     if (ind[i] == 0) q.push(i); n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     lev[u] = tl++; n     for (int i = head[u], v, w; ~i; i = e[i].next) { n       v = e[i].v; n       w = e[i].w; n       if (w <= lim) continue; n       --ind[v]; n       if (ind[v] == 0) { n         q.push(v); n       } n     } n   } n   for (int i = 1; i <= n; ++i) n     if (ind[i]) return false; n   return true; n } n int main() { n   memset(head, -1, sizeof head); n   scanf( %d%d , &n, &m); n   int L = 0, R = 0, mid, ans; n   for (int i = 1, u, v, w; i <= m; ++i) { n     scanf( %d%d%d , &u, &v, &w); n     adde(u, v, w, i); n     R = max(R, w); n   } n   ++R; n   while (L < R) { n     mid = (L + R) >> 1; n     if (check(mid)) { n       ans = R = mid; n     } else { n       L = mid + 1; n     } n   } n   check(ans); n   for (int u = 1; u <= n; ++u) { n     for (int i = head[u], v, w; ~i; i = e[i].next) { n       v = e[i].v; n       w = e[i].w; n       if (w > ans) continue; n       if (lev[u] > lev[v]) res[++k] = e[i].id; n     } n   } n   printf( %d %d n , ans, k); n   for (int i = 1; i <= k; ++i) printf( %d%c , res[i], (i == k ?   n  :    )); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); n   long long int t; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     if (s.size() == 1) { n       cout << 0 << endl; n       continue; n     } n     map<char, int> m; n     int i; n     int count = 0; n     for (i = 0; i < s.size(); i++) { n       m[s[i]]++; n     } n     for (auto it : m) { n       if (it.second > 1) { n         count += 2; n       } else if (it.second == 1) { n         count += 1; n       } n     } n     cout << count / 2 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n uniform_int_distribution<long long> d(156, 99657); n mt19937 rnd; n int n, s, x, mn = -100, q, w; n void solve() { n   scanf( %d%d%d , &n, &s, &x); n   if (n >= 2000) { n     for (int i = 1; i <= 1000; i++) { n       printf( ? %d n , (d(rnd) * d(rnd)) % n + 1); n       fflush(stdout); n       scanf( %d%d , &q, &w); n       if (q < x && q > mn) { n         mn = q; n         s = w; n       } n     } n   } n   while (1) { n     printf( ? %d n , s); n     fflush(stdout); n     scanf( %d%d , &q, &w); n     if (q >= x) { n       printf( ! %d n , q); n       fflush(stdout); n       return; n     } n     if (w == -1) { n       printf( ! -1 n ); n       fflush(stdout); n       return; n     } n     s = w; n   } n } n int main() { n   rnd.seed(time(NULL)); n   int tt = 1; n   for (int i = 1; i <= tt; i++) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1 << 30; n const int N = 1003, mod = 1000000007; n int n, q; n char a[N]; n int k; n int p[N]; n int calc(int x) { n   if (x == 1) return 0; n   if (p[x] != -1) return p[x]; n   int cnt = 0, ret = 0; n   while (x) { n     cnt += (x % 2); n     x /= 2; n   } n   return ret = calc(cnt) + 1; n } n void pre() { n   memset(p, -1, sizeof p); n   for (int i = 1; i <= 1000; i++) { n     p[i] = calc(i); n   } n } n int mem[N][N][2]; n int dp(int i, int kk, int f) { n   if (i == n) { n     return (k == p[kk] + 1 && kk != 0); n   } n   int &ret = mem[i][kk][f]; n   if (ret != -1) return ret; n   ret = 0; n   if (f) { n     ret += dp(i + 1, kk + 1, f) % mod; n     ret %= mod; n     ret += dp(i + 1, kk, f) % mod; n     ret %= mod; n   } else { n     if (a[i] ==  0 ) { n       ret += dp(i + 1, kk, f) % mod; n       ret %= mod; n     } else { n       ret += dp(i + 1, kk + 1, f) % mod; n       ret %= mod; n       ret += dp(i + 1, kk, 1) % mod; n       ret %= mod; n     } n   } n   return ret; n } n int main() { n   pre(); n   scanf( %s , a); n   scanf( %d , &k); n   n = strlen(a); n   if (k == 0) { n     printf( 1 ); n     return 0; n   } n   memset(mem, -1, sizeof mem); n   printf( %d , (dp(0, 0, 0) % mod + (k == 1 ? -1 : 0))); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   string s; n   int i, n; n   cin >> s; n   n = s.length(); n   cout <<  Mike  <<   n ; n   int mi = s[0] -  a ; n   for (i = 1; i < n; i++) { n     int x; n     x = s[i] -  a ; n     if (mi < x) { n       cout <<  Ann  <<   n ; n     } else { n       cout <<  Mike  <<   n ; n       mi = x; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, r; n int v[100], cnt, diff; n const int inf = 0x3fffffff; n int calc(int x, int y) { n   cnt = 0; n   int sum = 0; n   while (x) { n     if (y % x == 0) { n       if (y == 1) n         v[++cnt] = y / x; n       else if (x == 1) n         v[++cnt] = y / x - 1; n       else n         return inf; n     } else n       v[++cnt] = y / x; n     sum += v[cnt]; n     int t = y - v[cnt] * x; n     y = x; n     x = t; n   } n   if (sum != n) return inf; n   diff = 0; n   for (int i = 1; i <= cnt; i++) n     if (v[i] != 1) diff += v[i] - 1; n   return diff; n } n int ans[100], ansc; n int main() { n   while (scanf( %d %d , &n, &r) != EOF) { n     if (r == 0) { n       if (n == 1) n         printf( 0 nT n ); n       else n         puts( IMPOSSIBLE ); n       continue; n     } n     int ret = inf; n     for (int i = 1; i <= r; i++) { n       int now = calc(i, r); n       if (ret > now) { n         ret = now; n         ansc = cnt; n         for (int j = 1; j <= cnt; j++) ans[j] = v[j]; n       } n     } n     if (ret == inf) n       puts( IMPOSSIBLE ); n     else { n       printf( %d n , ret); n       char c =  T ; n       for (int i = ansc; i >= 1; i--) { n         while (ans[i]--) putchar(c); n         if (c ==  T ) n           c =  B ; n         else n           c =  T ; n       } n       puts(  ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-8; n const int inf = 0x3f3f3f3f; n const int maxn = 2000 + 10; n long long MOD = 1e9 + 7; n long long fac[200010]; n void getFac() { n   fac[0] = 1; n   for (int i = 1; i < 200010; i++) { n     fac[i] = fac[i - 1] * i % MOD; n   } n } n long long powerMod(long long x, long long n) { n   long long ans = 1; n   x %= MOD; n   while (n > 0) { n     if (n & 1) { n       ans = (ans * x) % MOD; n     } n     x = (x * x) % MOD; n     n >>= 1; n   } n   return ans; n } n long long C(long long n, long long m) { n   if (m > n) { n     return 0; n   } else { n     return fac[n] * powerMod(fac[m] * fac[n - m], MOD - 2) % MOD; n   } n } n long long Lucas(long long n, long long m) { n   if (m == 0) { n     return 1; n   } else { n     return C(n % MOD, m % MOD) * Lucas(n / MOD, m / MOD) % MOD; n   } n } n struct Point { n   int r, c; n   Point(int a = 0, int b = 0) { n     r = a; n     c = b; n   } n }; n bool operator<(const Point& a, const Point& b) { n   return a.r < b.r || (a.r == b.r && a.c < b.c); n } n int h, w, n; n Point point[maxn]; n vector<int> G[maxn]; n long long dp[maxn]; n long long dfs(int u) { n   if (dp[u] >= 0) { n     return dp[u]; n   } n   long long ret = Lucas(h - point[u].r + w - point[u].c, h - point[u].r); n   int glen = G[u].size(); n   for (int i = 0; i < glen; i++) { n     int v = G[u][i]; n     ret = (ret - Lucas(point[v].r - point[u].r + point[v].c - point[u].c, n                        point[v].r - point[u].r) * n                      dfs(v)) % n           MOD; n   } n   return dp[u] = (ret + MOD) % MOD; n } n int main() { n   getFac(); n   scanf( %d%d%d , &h, &w, &n); n   for (int i = 0; i < n; i++) { n     scanf( %d%d , &point[i].r, &point[i].c); n   } n   point[n++] = Point(1, 1); n   point[n++] = Point(h, w); n   sort(point, point + n); n   for (int i = 0; i < n - 1; i++) { n     for (int j = i + 1; j < n; j++) { n       { G[i].push_back(j); } n     } n   } n   for (int i = 0; i < n; i++) { n     dp[i] = -1; n   } n   dp[n - 1] = 0; n   long long ans = dfs(0); n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Edge { n   int v, next; n } edge[100010]; n int head[10]; n int pos; n void insert(int x, int y) { n   edge[pos].v = y; n   edge[pos].next = head[x]; n   head[x] = pos++; n } n int du[10]; n int pp[100010]; n int qq; n int fa[10]; n int find(int x) { n   if (fa[x] != x) fa[x] = find(fa[x]); n   return fa[x]; n } n void uunion(int x, int y) { n   int fx = find(x); n   int fy = find(y); n   fa[fx] = fy; n } n bool vis[100010]; n void dfs(int now) { n   for (int i = head[now]; i != -1; i = edge[i].next) { n     if (vis[i / 2]) continue; n     vis[i / 2] = true; n     dfs(edge[i].v); n     pp[qq++] = i ^ 1; n   } n } n int main() { n   int n; n   scanf( %d , &n); n   memset(head, -1, sizeof(head)); n   pos = 0; n   for (int i = 0; i <= 6; i++) fa[i] = i; n   for (int i = 0; i < n; i++) { n     int x, y; n     scanf( %d %d , &x, &y); n     insert(x, y); n     insert(y, x); n     du[x]++; n     du[y]++; n     uunion(x, y); n   } n   bool flag = true; n   for (int i = 0; i <= 6; i++) n     for (int j = 0; j <= 6; j++) n       if (du[i] > 0 && du[j] > 0 && find(i) != find(j)) flag = false; n   if (!flag) { n     printf( No solution n ); n     return 0; n   } n   qq = 0; n   for (int i = 0; i <= 6; i++) n     if (du[i] & 1) pp[qq++] = i; n   if (qq > 2) { n     printf( No solution n ); n     return 0; n   } n   for (int i = 0; i <= 6; i++) n     if (du[i] != 0) pp[qq++] = i; n   int begin = pp[0]; n   qq = 0; n   memset(vis, false, sizeof(vis)); n   dfs(begin); n   for (int i = 0; i < qq; i++) { n     printf( %d  , pp[i] / 2 + 1); n     if (pp[i] & 1) n       printf( - ); n     else n       printf( + ); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int x[100010], y[100010], vis[100010]; n int main() { n   int n, ans, i; n   scanf( %d , &n); n   memset(vis, 0, sizeof(vis)); n   for (i = 1; i <= n; i++) { n     scanf( %d%d , &x[i], &y[i]); n     vis[x[i]]++; n   } n   for (i = 1; i <= n; i++) { n     ans = n - 1; n     ans = ans + vis[y[i]]; n     printf( %d %d n , ans, 2 * (n - 1) - ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, t, i, j, k; n   cin >> n >> t; n   string a; n   cin >> a; n   for (i = 1; i <= t; ++i) { n     for (j = 0; j < n - 1; ++j) { n       if (a[j] ==  B  && a[j + 1] ==  G ) { n         a[j + 1] =  B ; n         a[j] =  G ; n         ++j; n       } n     } n   } n   cout << a << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, x = -1, y, xx, yy, k; n bool a[510][510]; n char h; n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> h; n       if (h ==  X ) { n         a[i][j] = true; n         if (x == -1) { n           x = i; n           y = j; n         } n         xx = i; n         yy = j; n         k++; n       } n     } n   } n   for (int i = x; i <= xx; i++) { n     for (int j = y; j <= yy; j++) { n       if (!a[i][j]) { n         cout <<  NO ; n         return 0; n       } n       k--; n     } n   } n   if (k > 0) n     cout <<  NO ; n   else n     cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> vec; n int main() { n   int k; n   cin >> k; n   string arr; n   while (k--) { n     cin >> arr; n     int n = arr.length(); n     int maxLen = 0; n     for (int i = 0; i < n; i++) { n       int len = 0; n       while (arr[i] ==  L ) { n         len++; n         i++; n         if (i == n) break; n       } n       maxLen = max(maxLen, len); n     } n     cout << maxLen + 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long max(long long a, long long b) { return a > b ? a : b; } n long long a[100005]; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     for (int i = 1; i <= n; i++) { n       cin >> a[i]; n     } n     sort(a + 1, a + 1 + n); n     long long ans = a[n] * a[n - 1] * a[n - 2] * a[n - 3] * a[n - 4]; n     long long ans1 = a[n] * a[n - 1] * a[n - 2] * a[1] * a[2]; n     long long ans2 = a[n] * a[1] * a[2] * a[3] * a[4]; n     cout << max(max(ans, ans1), ans2) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   vector<long long> bs(n); n   for (auto &x : bs) cin >> x; n   if (count(bs.begin(), bs.end(), 0) == n) { n     cout <<  YES  << endl; n     for (int _ = 0; _ < (n); ++_) cout << 1 <<    ; n     cout << endl; n     return 0; n   } n   auto prv = [&](int i) { return (i + n - 1) % n; }; n   bs.push_back(bs[0]); n   for (int i = 0; i < (n); ++i) n     if (bs[i] > bs[prv(i)]) { n       vector<long long> a(n); n       a[i] = bs[i]; n       for (int k = prv(i); k != i; k = prv(k)) { n         int j = prv(k), l = (k + 1) % n; n         long long x; n         long long dif = bs[j] - bs[k]; n         if (dif < 0) n           x = 0; n         else { n           x = 1 + dif / a[l]; n         } n         a[k] = bs[k] + x * a[l]; n       } n       cout <<  YES  << endl; n       for (auto &x : a) cout << x <<    ; n       cout << endl; n       return 0; n     } n   cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a[1111]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = (0); i < (n); i++) scanf( %d , &a[i]); n   int ans = 0; n   for (int i = (0); i < (m); i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     x--; n     y--; n     ans += min(a[x], a[y]); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[10010]; n int main() { n   int n, m; n   while (scanf( %d%d , &n, &m) == 2) { n     for (int i = 1; i < n; i++) a[i] = 1; n     a[n] = 0; n     int maxx = 1; n     for (int ii = 0; ii < m; ii++) { n       for (int i = 1; i <= n; i++) { n         if (a[i] + maxx <= n - i) { n           printf( %d  , n - maxx); n           a[i] += maxx; n         } else { n           printf( %d  , i + a[i]); n           a[i] = n - i; n         } n       } n       puts(  ); n       maxx *= 2; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void read(T& x) { n   bool fu = 0; n   char c; n   for (c = getchar(); c <= 32; c = getchar()) n     ; n   if (c ==  - ) fu = 1, c = getchar(); n   for (x = 0; c > 32; c = getchar()) x = x * 10 + c -  0 ; n   if (fu) x = -x; n }; n template <class T> n inline void read(T& x, T& y) { n   read(x); n   read(y); n } n template <class T> n inline void read(T& x, T& y, T& z) { n   read(x); n   read(y); n   read(z); n } n template <class T> n inline void read(T& x, T& y, T& z, T& q) { n   read(x); n   read(y); n   read(z); n   read(q); n } n inline char getc() { n   char c; n   for (c = getchar(); c <= 32; c = getchar()) n     ; n   return c; n } n int n, T, i, j, k, l, p; n long double ans1, ans2; n double xx; n long double f[1110][5000], g[1110][5000]; n struct node { n   long long t0, t1, s0, s1; n   long double f; n } a[1010]; n long double get(node a, node b) { n   return (a.t1 + b.t1) * (1 - b.f) + a.t1 * b.f * (1 - a.f); n } n bool cmp(node a, node b) { return get(a, b) < get(b, a); } n bool upmaxs(long double& x, long double y) { return y > x ? x = y, 1 : 0; } n void upd(long double& x, long double& x0, long double y, long double y0) { n   if (make_pair(y, -y0) > make_pair(x, -x0)) x = y, x0 = y0; n } n int main() { n   read(n, T); n   for (i = 1; i <= n; i++) n     read(a[i].s0, a[i].s1, a[i].t0, a[i].t1), scanf( %lf , &xx), a[i].f = xx; n   sort(a + 1, a + 1 + n, cmp); n   for (i = 1; i <= n; i++) n     for (j = 0; j <= T; j++) { n       upd(f[i + 1][j], g[i + 1][j], f[i][j], g[i][j]); n       upd(f[i + 1][j + a[i].t0], g[i + 1][j + a[i].t0], f[i][j] + a[i].s0, n           g[i][j] + a[i].t0); n       upd(f[i + 1][j + a[i].t0 + a[i].t1], g[i + 1][j + a[i].t0 + a[i].t1], n           f[i][j] + a[i].s0 + a[i].s1 * (1 - a[i].f), n           a[i].t0 + g[i][j] * a[i].f + (j + a[i].t1) * (1 - a[i].f)); n     } n   for (j = 0; j <= T; j++) upd(ans1, ans2, f[n + 1][j], g[n + 1][j]); n   cout << fixed << setprecision(20) << ans1 <<     << ans2 << endl; n   scanf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:268435456 ) n using namespace std; n template <typename T1, typename T2> n istream& operator>>(istream& in, pair<T1, T2>& t) { n   return in >> t.first >> t.second; n } n template <typename T1, typename T2> n ostream& operator<<(ostream& out, pair<T1, T2>& t) { n   return out << t.first <<     << t.second << endl; n } n template <typename T> n istream& operator>>(istream& in, vector<T>& t) { n   for (long long i = 0; i < t.size(); i++) in >> t[i]; n   return in; n } n template <typename T> n ostream& operator<<(ostream& out, vector<T>& t) { n   for (long long i = 0; i < t.size(); i++) out << t[i] <<    ; n   return out; n } n long long n, d, k; n vector<vector<long long> > g; n long long in = 1; n void printg(long long x, long long p = -1) { n   for (long long to : g[x]) { n     cout << x + 1 <<     << to + 1 << endl; n     printg(to, x); n   } n } n void con(long long x) { n   g[x].push_back(in++); n   if (in >= n) { n     puts( YES ); n     printg(0); n     exit(0); n   } n } n void add(long long h) { n   long long t = 0; n   for (long long i = 1; i <= h; ++i) { n     con(t); n     t = in - 1; n   } n } n void build(long long x, long long p, long long h, long long curh = 1) { n   if (curh >= h) return; n   for (long long i : g[x]) { n     build(i, x, h, curh + 1); n   } n   while (g[x].size() < k - 1) { n     con(x); n     build(in - 1, x, h, curh + 1); n   } n } n signed main() { n   long long rrrr = 1e5; n   cin >> n >> d >> k; n   d++; n   if (n < d) return puts( NO ), 0; n   if (k == 1) { n     if (n == d && n == 2) { n       cout <<  YES  << endl; n       for (long long i = 0; i < n - 1; ++i) { n         cout << i + 1 <<     << i + 2 << endl; n       } n       return 0; n     } else n       return puts( NO ), 0; n   } n   --d; n   g.resize(n); n   add(d / 2); n   add((d + 1) / 2); n   build(g[0][0], 0, d / 2); n   build(g[0][1], 0, (d + 1) / 2); n   for (long long i = 2; i < k; ++i) { n     if (d / 2 == 0) continue; n     con(0); n     build(in - 1, 0, d / 2); n   } n   cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast,unroll-loops ) n using namespace std; n const int maxn = 15e4 + 7, blo = 255, mo = 998244353; n int n, q, cl = 0, p[maxn], sz[maxn], lb[maxn], rb[maxn]; n int_fast64_t inv, one = 0, rec[maxn], dat[maxn]; n vector<int> vt, g[maxn]; n vector<vector<int>> hsz; n inline int qp(int_fast64_t x, int t = mo - 2) { n   int_fast64_t re = 1; n   while (t) { n     if (t & 1) re = re * x % mo; n     x = x * x % mo, t >>= 1; n   } n   return re; n } n inline void add(int i, int x) { n   for (; i <= cl + 1; i += i & -i) { n     dat[i] += x; n     if (dat[i] >= mo) dat[i] -= mo; n   } n } n inline int sum(int i) { n   int_fast64_t re = 0; n   for (; i > 0; i -= i & -i) { n     re += dat[i]; n   } n   re += one; n   return re % mo; n } n void dfs(int u, int f) { n   lb[u] = cl++; n   sz[u] = 1; n   p[u] = f; n   for (register int i = (0); i < (g[u].size()); ++i) { n     int& v = g[u][i]; n     if (v == f) continue; n     dfs(v, u); n     sz[u] += sz[v]; n   } n   rb[u] = cl; n } n void gsz(int u, int f, int k) { n   hsz.back()[u] = k; n   for (register int i = (0); i < (g[u].size()); ++i) { n     int& v = g[u][i]; n     if (v == f) continue; n     gsz(v, u, k); n   } n } n int main() { n   cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(false); n   cin >> n >> q; n   inv = qp(n); n   for (register int i = (1); i < (n); ++i) { n     int u, v; n     cin >> u >> v, --u, --v; n     g[u].emplace_back(v), g[v].emplace_back(u); n   } n   dfs(0, -1); n   for (register int i = (0); i < (n); ++i) { n     if (g[i].size() > blo) { n       vt.emplace_back(i); n       hsz.emplace_back(*new vector<int>); n       hsz.back().reserve(n); n       int s = 0; n       for (register int j = (0); j < (g[i].size()); ++j) { n         int& v = g[i][j]; n         if (v == p[i]) continue; n         gsz(v, i, sz[v]); n         s += sz[v]; n       } n       if (p[i] >= 0) gsz(p[i], i, n - s - 1); n       hsz.back()[i] = 0; n     } n   } n   while (q--) { n     int_fast64_t t, v, d, di; n     cin >> t >> v, --v; n     if (t == 1) { n       cin >> d; n       if (g[v].size() > blo) { n         rec[v] += d; n         if (rec[v] >= mo) rec[v] -= mo; n       } else { n         di = d * inv % mo; n         for (register int i = (0); i < (g[v].size()); ++i) { n           int& u = g[v][i]; n           int x = di * (n - sz[u]) % mo; n           if (u == p[v]) continue; n           add(lb[u] + 1, x); n           add(rb[u] + 1, mo - x); n         } n         int x = di * sz[v] % mo; n         one += x; n         add(lb[v] + 1, d < x ? mo + d - x : d - x); n         add(rb[v] + 1, x); n         add(lb[v] + 2, mo - d); n       } n     } else { n       int_fast64_t res = 0; n       for (register int i = (0); i < (vt.size()); ++i) { n         res += rec[vt[i]] * (n - hsz[i][v]) % mo; n       } n       cout << (res % mo * inv + sum(lb[v] + 1)) % mo <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e5 + 10; n const int base = 31337; n const int mod = 1e9 + 7; n const int inf = 0x3f3f3f3f; n const int logo = 20; n const int off = 1 << logo; n const int treesiz = off << 1; n int n; n char niz[maxn]; n int his[maxn]; n int main() { n   scanf( %d%s , &n, niz); n   long long sol = 0; n   long long tren = 0; n   memset(his, -1, sizeof his); n   for (int i = 0; i < n; i++) { n     if (niz[i] ==  0 ) n       sol += tren; n     else { n       int ptr = i; n       while (ptr + 1 < n && niz[ptr + 1] ==  1 ) ptr++; n       int cnt = 1; n       for (int j = i; j <= ptr; j++) { n         tren += j - his[cnt]; n         his[cnt] = ptr - cnt + 1; n         sol += tren; n         cnt++; n       } n       i = ptr; n     } n   } n   printf( %lld n , sol); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x, y, z; n   cin >> x >> y >> z; n   if (x > y + z) n     cout <<  +  << endl; n   else if (y > x + z) n     cout <<  -  << endl; n   else if (x == y && z == 0) n     cout <<  0  << endl; n   else n     cout <<  ?  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int ampow(int k, int p) { n   long long int ans = 1; n   for (int i = 0; i < p; i++) { n     ans *= k; n   } n   return ans; n } n int main() { n   std::ios::sync_with_stdio(false); n   long long int k, b, n, t, z, i; n   cin >> k >> b >> n >> t; n   if (k == 1) { n     z = 1 + (n * b); n     if (t >= z) n       i = 0; n     else n       i = ceil(((double)z - t) / b); n   } else { n     double c = (double)b / (k - 1); n     if (log(t + c) - (n * log(k) + log(c + 1)) >= 0) n       i = 0; n     else if (abs(log(t + c) - (n * log(k) + log(c + 1))) <= 0.0000000001) n       i = 0; n     else { n       double num = n * log(k) + log(c + 1) - log(t + c); n       double den = log((double)k); n       i = ceil(num / den); n       if (i - (num / den) >= 0.9999999999) i--; n     } n   } n   cout << i << endl; n } n


#include <bits/stdc++.h> n using namespace std; n bool f(int a, string s) { n   vector<char> v; n   int tmp; n   int ss = (s.size() - 1); n   while (a) { n     tmp = a % 10; n     if (tmp == 7) v.push_back( 7 ); n     if (tmp == 4) v.push_back( 4 ); n     a /= 10; n   } n   if (v.size() != s.size()) return false; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] != v[s.size() - i - 1]) return false; n   } n   return true; n } n int main() { n   int a; n   string s; n   cin >> a >> s; n   for (int i = (a + 1); i <= 100000000; i++) { n     if (f(i, s)) { n       cout << i << endl; n       return 0; n     } n   } n } n


#include <bits/stdc++.h> n int main() { n   int n, a, b; n   scanf( %d%d%d , &n, &a, &b); n   for (int i = 1; i <= n; i++) { n     long long x; n     scanf( %I64d , &x); n     x = ((x * a) % b) / a; n     printf( %I64d  , x); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b; n   long long p(1); n   cin >> a >> b; n   while (b--) p = (p << 1ll) % 1000000009ll; n   long long ans(1); n   while (a--) { n     p--; n     if (p < 0) p += 1000000009ll; n     ans *= p; n     ans %= 1000000009ll; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n const int MAXN = 500000; n int A[MAXN + 10]; n int main() { n   scanf( %d %d , &n, &k); n   for (int i = 1; i <= n; i++) scanf( %d , &A[i]); n   A[n + 1] = INT_MAX; n   A[0] = INT_MIN; n   sort(A + 1, A + n + 1); n   int l = 1, r = n; n   for (l = 2; l <= n && A[l] == A[l - 1]; l++) n     ; n   l--; n   int days = k; n   while (l <= n) { n     long long d = l * (long long)(A[l + 1] - A[l]); n     if (d <= days) { n       days -= d; n       A[l] = A[l + 1]; n       while (l <= n && A[l] == A[l + 1]) l++; n     } else { n       long long m = days / l, M = days % l, v = A[l] + m; n       for (int i = 1; i <= l; i++) A[i] = v; n       for (int i = 0; i < M; i++) A[l - i] = v + 1LL; n       break; n     } n   } n   for (r = n - 1; r > 0 && A[r] == A[r + 1]; r--) n     ; n   r++; n   days = k; n   while (r > 0) { n     long long d = (n - r + 1) * ((long long)A[r] - (long long)A[r - 1]); n     if (d <= days) { n       days -= d; n       A[r] = A[r - 1]; n       while (r > 0 && A[r] == A[r - 1]) r--; n     } else { n       long long m = days / (n - r + 1), M = days % (n - r + 1), v = A[r] - m; n       for (int i = r; i <= n; i++) A[i] = v; n       for (int i = 0; i < M; i++) A[r + i] = v - 1LL; n       break; n     } n   } n   printf( %d n , A[n] - A[1]); n   return 0; n } n


#include <bits/stdc++.h> n const int INF_INT = 0x3f3f3f3f; n const long long INF_LL = 0x7f7f7f7f; n const int MOD = 1e9 + 7; n const double eps = 1e-10; n const double pi = acos(-1); n using namespace std; n const int N = 200010; n long long n, k; n long long fac[N], inv[N]; n long long ksm(long long a, long long x, long long mod) { n   if (x == 0) return 1; n   long long ret = ksm(a, x / 2, mod); n   ret = ret * ret % mod; n   if (x & 1) ret = ret * a % mod; n   return ret; n } n void init() { n   fac[0] = 1; n   inv[0] = 1; n   for (long long i = 1; i < N; i++) { n     fac[i] = fac[i - 1] * i % MOD; n     inv[i] = ksm(fac[i], MOD - 2, MOD); n   } n } n map<int, int> mp; n long long C(long long n, long long k) { n   long long ret = fac[n] * inv[k] % MOD; n   ret = ret * inv[n - k] % MOD; n   return ret; n } n int main(int argc, char const *argv[]) { n   init(); n   while (cin >> n >> k) { n     mp.clear(); n     for (int i = 1; i <= n; i++) { n       int l, r; n       scanf( %d%d , &l, &r); n       mp[l]++; n       mp[r + 1]--; n     } n     int last = mp.begin()->first; n     int tmp = 0; n     long long ans = 0; n     for (auto it = mp.begin(); it != mp.end(); it++) { n       long long num = it->first - last; n       if (tmp >= k) ans += C(tmp, k) * num % MOD; n       ans %= MOD; n       tmp += it->second; n       last = it->first; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> v[3005]; n long long f[20][3005], ans; n int n, k; n void solve(int l, int r, int d) { n   if (l == r) { n     for (int i = (int)(0); i <= (int)(v[l].size() - 1); i++) n       if (i + 1 <= k) ans = max(ans, v[l][i] + f[d][k - i - 1]); n     return; n   } n   int mid = (l + r) / 2; n   memcpy(f[d + 1], f[d], sizeof(f[d])); n   for (int i = (int)(mid + 1); i <= (int)(r); i++) { n     long long v1 = v[i].size(), v2 = v[i][v1 - 1]; n     for (int j = (int)(k - v1); j >= (int)(0); j--) n       f[d + 1][j + v1] = max(f[d + 1][j + v1], f[d + 1][j] + v2); n   } n   solve(l, mid, d + 1); n   memcpy(f[d + 1], f[d], sizeof(f[d])); n   for (int i = (int)(l); i <= (int)(mid); i++) { n     long long v1 = v[i].size(), v2 = v[i][v1 - 1]; n     for (int j = (int)(k - v1); j >= (int)(0); j--) n       f[d + 1][j + v1] = max(f[d + 1][j + v1], f[d + 1][j] + v2); n   } n   solve(mid + 1, r, d + 1); n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = (int)(1); i <= (int)(n); i++) { n     int t; n     scanf( %d , &t); n     v[i].resize(t); n     for (int j = (int)(0); j <= (int)(t - 1); j++) scanf( %lld , &v[i][j]); n     for (int j = (int)(1); j <= (int)(t - 1); j++) v[i][j] += v[i][j - 1]; n   } n   solve(1, n, 0); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n string s, t, k; n string str(string a) { n   if (a[1] ==  X ) n     swap(a[1], a[3]); n   else if (a[2] ==  X ) n     swap(a[2], a[3]); n   else if (a[0] ==  X ) n     swap(a[0], a[2]), swap(a[2], a[3]); n   string b = a.substr(0, 3); n   return b; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> s >> k; n   s += k; n   s = str(s); n   cin >> t >> k; n   t += k; n   t = str(t); n   if (s == t) { n     cout <<  YES n ; n     return 0; n   } n   swap(s[0], s[2]); n   swap(s[1], s[2]); n   if (s == t) { n     cout <<  YES n ; n     return 0; n   } n   swap(s[0], s[2]); n   swap(s[1], s[2]); n   if (s == t) { n     cout <<  YES n ; n     return 0; n   } n   cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 15; n const int K = 500; n int a[N][N]; n const int INF = 1e9 + 7; n int calc(int x, int y, int h, int w) { n   int cnt = 0; n   for (int i = x; i < x + h; i++) { n     for (int j = y; j < y + w; j++) { n       cnt += a[i][j]; n     } n   } n   return cnt; n } n int main() { n   int r, c; n   int n, k; n   cin >> r >> c >> n >> k; n   for (int i = 0; i < n; i++) { n     int x, y; n     cin >> x >> y; n     x--; n     y--; n     a[x][y] = 1; n   } n   int ans = 0; n   for (int d = 1; d <= r; d++) { n     for (int w = 1; w <= c; w++) { n       for (int i = 0; i + d - 1 < r; i++) { n         for (int j = 0; j + w - 1 < c; j++) { n           if (calc(i, j, d, w) >= k) ans++; n         } n       } n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n long long pow2[61]; n vector<long long> ans; n int main() { n   std::ios_base::sync_with_stdio(false); n   pow2[0] = 1; n   pow2[1] = 2; n   for (int i = 2; i <= 60; i++) pow2[i] = pow2[i - 1] * 2; n   long long X, d; n   scanf( %lld %lld , &X, &d); n   while (X > 1) { n     long long k = 0; n     while (pow2[k] - 1 < X) k++; n     k--; n     long long j = 1; n     if (ans.size()) { n       j = ans.back() + d; n     } n     for (int i = 1; i <= k; i++) ans.push_back(j); n     X -= pow2[k] - 1; n   } n   if (X == 1) { n     if (ans.size()) n       ans.push_back(ans.back() + d); n     else n       ans.push_back(1); n   } n   if (ans.size() > 10000) n     printf( -1 n ); n   else { n     printf( %lu n , ans.size()); n     for (int i = 0; i < ans.size(); i++) n       if (ans[i] >= 1e18) return 0 * printf( -1 n ); n     for (int i = 0; i < ans.size(); i++) printf( %lld  , ans[i]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n typedef long long int ll; n  n  n ll sum(ll n) n { n     ll s=0; n     while(n>0) n     { n         s+= n%10; n         n=n/10; n  n     } n     return s; n } n  n  n ll gcd(ll n, ll m ) n { n     ll k = n%m; n     while(k!=0) n     { n         n=m; n         m=k; n         k= n%m; n     } n  n     return m; n } n  n ll gcdSum(ll n) n { n     ll x = sum(n); n     ll num =gcd(n,x); n     while(num ==1) n     { n         n++; n         if (n%10 !=0) n         { n             x++; n         } n         else n         { n             x-= 8; n         } n         num = gcd(n,x); n  n         //cout<<n<<   <<x<<   <<num<<endl; n  n     } n  n     return n; n } n  n  n  n int main() { n     #ifndef ONLINE_JUDGE n     // for getting input from input.txt n     freopen( input.txt ,  r , stdin); n     // for writing output to output.txt n     freopen( output.txt ,  w , stdout); n #endif n  //code n  int t; n     cin >> t; n     while(t>0) n     { n         ll n,w; n         cin >> n>>w; n  n         map<ll,int> mp; n         for(ll i=0;i<n;i++) n         { n             ll wi ; n             cin >> wi; n             mp[wi]++; n         } n         // for(auto x : mp) n         // { n         //     cout<< x.first<<   <<x.second<<endl; n         // } n  n         ll h=0; n          n         map<ll,int>  :: iterator it; n         while(!mp.empty()) n         { n             h++; n             it = mp.end(); n             it--; n             ll wirem =w; n             //cout<< hi <<endl; n              n             while(!mp.empty() && wirem>= mp.begin()->first  ) n             { n                 if(wirem - it->first >=0) n                 { n                     wirem -= it->first; n                     it->second--; n                     if(it->second==0) n                     { n                         mp.erase(it->first); n                         if(mp.empty()) n                         { n                             break; n                         } n                         it= mp.end(); n                         it--; n                          n                          n                          n                     } n                      n  n                 } n                 else n                 { n                     it--; n                 }    n  n             } n             // for(auto x : mp) n             // { n             //     cout<< x.first<<   <<x.second<<endl; n             // } n                  n              n             // cout<<         <<h<<endl; n         } n  n         cout<<h<<endl; n         t--; n     } n         n      n  return 0; n }