#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int n = 0, f = 1, ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     n = n * 10 + ch -  0 ; n     ch = getchar(); n   } n   return n * f; n } n char a[2000005], b[2000005]; n int n; n int c[2000005]; n int get(int x) { n   int cur = 0, res = 0; n   for (int i = 1; i <= n; i++) { n     cur += x * c[i]; n     res = max(res, cur); n     if (cur < 0) cur = 0; n   } n   return res; n } n int main() { n   int s1 = 0, s2 = 0; n   n = read(); n   scanf( %s%s , a + 1, b + 1); n   for (int i = 1; i <= n; i++) { n     if (a[i] != b[i] && a[i] ==  0 ) n       c[i] = -1, s1++; n     else if (a[i] != b[i] && a[i] ==  1 ) n       c[i] = 1, s2++; n   } n   if (s1 != s2) { n     printf( -1 n ); n     return 0; n   } n   printf( %d n , max(get(1), get(-1))); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long double sq(long double a) { return a * a; } n long double dist(pair<long double, long double> a, n                  pair<long double, long double> b) { n   return sqrt((long double)(sq(a.second - b.second) + sq(a.first - b.first))); n } n long double mandist(pair<long long, long long> a, n                     pair<long long, long long> b) { n   return abs(a.second - b.second) + abs(a.first - b.first); n } n long long a, b, c; n pair<long long, long long> p1, p2; n int main() { n   scanf(  %lld%lld%lld , &a, &b, &c); n   scanf(  %lld%lld%lld%lld , &p1.first, &p1.second, &p2.first, &p2.second); n   if (a == 0 || b == 0) n     printf( %.20Lf n , mandist(p1, p2)); n   else { n     long double res = mandist(p1, p2); n     long double interhoriz1 = n         ((long double)(-c - a * p1.first)) / ((long double)b); n     long double interhoriz2 = n         ((long double)(-c - a * p2.first)) / ((long double)b); n     long double intervert1 = n         ((long double)(-c - b * p1.second)) / ((long double)a); n     long double intervert2 = n         ((long double)(-c - b * p2.second)) / ((long double)a); n     res = min(res, abs((long double)p1.second - interhoriz1) + n                        abs((long double)p2.second - interhoriz2) + n                        dist(make_pair((long double)p1.first, interhoriz1), n                             make_pair((long double)p2.first, interhoriz2))); n     res = min(res, abs((long double)p1.second - interhoriz1) + n                        abs((long double)p2.first - intervert2) + n                        dist(make_pair((long double)p1.first, interhoriz1), n                             make_pair(intervert2, (long double)p2.second))); n     res = min(res, abs((long double)p1.first - intervert1) + n                        abs((long double)p2.first - intervert2) + n                        dist(make_pair(intervert1, (long double)p1.second), n                             make_pair(intervert2, (long double)p2.second))); n     res = min(res, abs((long double)p1.first - intervert1) + n                        abs((long double)p2.second - interhoriz2) + n                        dist(make_pair(intervert1, (long double)p1.second), n                             make_pair((long double)p2.first, interhoriz2))); n     printf( %.20Lf n , res); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[2005], b[2005]; n int main() { n   int n, i, j, x, y, k; n   long long s; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> x >> y; n     a[x + y]++; n     b[1001 + x - y]++; n   } n   s = 0; n   for (i = 0; i < 2003; i++) { n     k = a[i]; n     s += (k - 1) * k / 2; n     k = b[i]; n     s += (k - 1) * k / 2; n   } n   cout << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve(); n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout << fixed; n   cout.precision(12); n   solve(); n   return 0; n } n template <typename T> n void prv(vector<T> v) { n   for (int __ii = 0; __ii < ((int)v.size()); __ii++) { n     if (__ii) cout <<    ; n     cout << v[__ii]; n   } n   cout <<   n ; n } n template <typename T> n void prm(vector<vector<T>> v) { n   for (int __ii = 0; __ii < ((int)v.size()); __ii++) { n     for (int __jj = 0; __jj < v[__ii].size(); __jj++) { n       if (__jj) cout <<    ; n       cout << v[__ii][__jj]; n     } n     cout <<   n ; n   } n } n template <typename T> n void sc(T& x) { n   cin >> x; n } n template <typename Head, typename... Tail> n void sc(Head& head, Tail&... tail) { n   cin >> head; n   sc(tail...); n } n template <typename T> n void pr(const T& x) { n   cout << x <<   n ; n } n template <typename Head, typename... Tail> n void pr(const Head& head, const Tail&... tail) { n   cout << head <<    ; n   pr(tail...); n } n template <typename... T> n void err(const T&... cod) { n   pr(cod...); n   exit(0); n } n void solve() { n   int n, m, y, x; n   sc(n, m, y, x); n   string s; n   sc(s); n   int dx = (s[1] ==  L  ? -1 : 1); n   int dy = (s[0] ==  U  ? -1 : 1); n   const int MAX_ITER = 1e6 + 10; n   set<pair<int, int>> st; n   long long ans = 1; n   for (int i = 0; i < MAX_ITER; i++) { n     st.emplace(y, x); n     if (((int)st.size()) == n + m - 2) err(ans); n     int u = (dy == -1 ? y - 1 : n - y); n     int v = (dx == -1 ? x - 1 : m - x); n     int w = min(u, v); n     x += dx * w; n     y += dy * w; n     ans += w; n     if (v == w) dx *= -1; n     if (u == w) dy *= -1; n   } n   pr(-1); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(0); n   string s; n   cin >> s; n   string v =  aeiou ; n   string c =  bcdfghjklmnpqrstvwxyz ; n   map<char, int> cnt; n   map<char, int> mp; n   for (auto i : v) mp[i] = 1; n   for (auto i : c) mp[i] = 0; n   string temp =   ; n   int vc = 0; n   int cc = 0; n   int dc = 0; n   vector<string> ans; n   for (int i = 0; i < s.size(); i++) { n     if (mp[s[i]] == 1) { n       cc = 0; n       dc = 0; n       for (auto j : c) cnt[j] = 0; n     } else { n       if (cnt[s[i]] == 0) { n         cnt[s[i]]++; n         dc++; n         cc++; n       } else { n         cnt[s[i]]++; n         cc++; n       } n     } n     if (cc > 2 && dc > 1) { n       ans.push_back(temp); n       for (auto j : c) cnt[j] = 0; n       cnt[s[i]] = 1; n       cc = 1; n       dc = 1; n       temp = s[i]; n     } else n       temp += s[i]; n   } n   ans.push_back(temp); n   for (auto i : ans) cout << i <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 5; n string s[10005]; n int vis[10005]; n int main() { n   ios::sync_with_stdio(0); n   int n, sum(0); n   cin >> n; n   for (long long i = 0; i < n; ++i) cin >> s[i], sum += s[i].size(); n   char c; n   cin >> c; n   for (long long i = 0; i < n; ++i) s[i] += c; n   sort(s, s + n); n   sum = sum * 2 / n + 1; n   for (long long i = 0; i < n; ++i) n     if (!vis[i]) { n       cout << s[i]; n       vis[i]++; n       for (long long j = i + 1; j < n; ++j) n         if (!vis[j] && sum - s[i].size() == s[j].size() - 1) { n           cout << s[j].substr(0, s[j].size() - 1) <<   n ; n           vis[j]++; n           break; n         } n     } n } n


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


#include <bits/stdc++.h> n using namespace std; n const long double E = 1e-7; n const int MOD = 1e9 + 7; n const long long MAX = 1e6 + 1; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int n, k; n   cin >> n >> k; n   int ar[n]; n   for (int i = 0; i < n; i++) cin >> ar[i]; n   int ans = INT_MIN; n   int a[n], b[n]; n   for (int i = 0; i < n; i++) n     for (int j = i; j < n; j++) { n       int A = j - i + 1; n       int B = n - A; n       copy(ar + i, ar + j + 1, a); n       copy(ar, ar + i, b); n       copy(ar + j + 1, ar + n, b + i); n       sort(a, a + A); n       sort(b, b + B); n       for (int l = 0, r = B - 1, q = k; l < A && r >= 0 && a[l] < b[r] && q; n            l++, r--, --q) n         swap(a[l], b[r]); n       int sum = 0; n       for (int i = 0; i < A; i++) sum += a[i]; n       ans = max(ans, sum); n     } n   cout << ans; n } n


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


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, p = 0, ans = 0; n   string s, dvg, answ, r_dvg; n   cin >> n >> s; n   for (int i = 0; i < n - 1; i++) { n     dvg = s.substr(i, 2); n     for (int j = i; j < n - 1; j++) { n       r_dvg = s.substr(j, 2); n       if (dvg == r_dvg) { n         p++; n       } n     } n     if (p > ans) { n       ans = p; n       answ = dvg; n     } n     p = 0; n   } n   cout << answ << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Point { n   long long x, y; n   void read() { scanf( %lld%lld , &x, &y); } n }; n long long product(const Point &o, const Point &a, const Point &b) { n   return (a.x - o.x) * (b.y - o.y) - (b.x - o.x) * (a.y - o.y); n } n bool intersect(const Point &a, const Point &b, const Point &c, const Point &d) { n   bool insect1 = product(a, c, d) * product(b, c, d) < 0; n   bool insect2 = product(c, a, b) * product(d, a, b) < 0; n   return insect1 && insect2; n } n int main() { n   ios ::sync_with_stdio(false); n   cout << fixed << setprecision(16); n   int R, B; n   scanf( %d%d , &R, &B); n   vector<Point> bases(B); n   vector<Point> ships(R); n   vector<int> b(R); n   for (int i = 0; i < R; i++) { n     ships[i].read(); n   } n   for (int i = 0; i < B; i++) { n     bases[i].read(); n   } n   if (B != R) { n     puts( No ); n     return 0; n   } n   for (int i = 0; i < R; i++) { n     b[i] = i; n   } n   bool yes = false; n   do { n     bool ok = true; n     for (int i = 0; i < B; i++) { n       for (int j = i + 1; j < B; j++) { n         if (intersect(ships[b[i]], bases[i], ships[b[j]], bases[j])) { n           ok = false; n         } n       } n     } n     if (ok) { n       yes = true; n       break; n     } n   } while (next_permutation(b.begin(), b.end())); n   puts(yes ?  Yes  :  No ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 305000; n long long mod = 1e9 + 7; n long long h, w, n; n long long dp[maxn], f[maxn], inv[maxn]; n struct stu { n   long long x, y; n } a[maxn]; n long long cmp(stu a, stu b) { n   if (a.x != b.x) n     return a.x < b.x; n   else n     return a.y < b.y; n } n long long ksm(long long base, long long b, long long mod) { n   long long ans = 1; n   while (b) { n     if (b & 1) { n       ans *= base; n       ans %= mod; n     } n     base *= base; n     base %= mod; n     b >>= 1; n   } n   return ans; n } n void init() { n   f[0] = 1, inv[0] = 1; n   for (long long i = 1; i <= 200000; i++) { n     f[i] = f[i - 1] * i % mod; n     inv[i] = ksm(f[i], mod - 2, mod); n   } n } n long long c(long long x, long long y) { n   if (x < y) n     return 0; n   else n     return f[x] * inv[y] % mod * inv[x - y] % mod; n } n signed main() { n   cin >> h >> w >> n; n   init(); n   for (long long i = 1; i <= n; i++) cin >> a[i].x >> a[i].y; n   sort(a + 1, a + n + 1, cmp); n   a[n + 1].x = h, a[n + 1].y = w; n   for (long long i = 1; i <= n + 1; i++) { n     dp[i] = c(a[i].x + a[i].y - 2, a[i].x - 1); n     for (long long j = 1; j < i; j++) { n       if (a[j].x > a[i].x || a[j].y > a[i].y) continue; n       dp[i] = (dp[i] - n                dp[j] * c(a[i].x + a[i].y - a[j].x - a[j].y, a[i].x - a[j].x)) % n               mod; n     } n   } n   cout << (dp[n + 1] + mod) % mod << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int v[3005], inv = 0, n; n double sol; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> v[i]; n   for (int i = 1; i <= n; i++) n     for (int j = i + 1; j <= n; j++) n       if (v[i] > v[j]) inv++; n   sol = double(inv) - inv / 2 + (inv / 2) * 3.0; n   cout << fixed; n   cout << setprecision(6); n   cout << sol <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000 * 1000 * 1000; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   pair<int, int> minp1(INF, INF), minp2(INF, INF); n   for (int i = 0; i < n; i++) { n     int x, y; n     cin >> x >> y; n     minp1 = min(minp1, pair<int, int>(x, y)); n   } n   for (int i = 0; i < n; i++) { n     int x, y; n     cin >> x >> y; n     x *= -1; n     y *= -1; n     minp2 = min(minp2, pair<int, int>(x, y)); n   } n   cout << minp1.first - minp2.first <<     << minp1.second - minp2.second; n } n


#include <bits/stdc++.h> n const int MAXN = 100100; n struct Gao { n   int p[MAXN], q[MAXN]; n   bool r[MAXN]; n   void init(int n) { n     for (int i = 0; i < n; ++i) { n       q[i] = 1; n       r[i] = true; n     } n   } n   void next(int i) { n     if (!r[p[i]] && p[i] != i) { n       next(p[i]); n       q[i] += q[p[i]]; n       p[i] = p[p[i]]; n     } n   } n   void kill(int i) { n     if (r[i]) { n       next(i); n       r[i] = false; n     } n   } n } gao; n int main() { n   int n, m, x, y, z; n   static int a[MAXN], b[MAXN]; n   scanf( %d%d , &n, &m); n   gao.init(n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &gao.p[i]); n     --gao.p[i]; n   } n   for (int i = 0; i < m; ++i) { n     scanf( %d , &a[i]); n     --a[i]; n   } n   for (int i = 0; i < m; ++i) { n     scanf( %d , &b[i]); n   } n   z = 0; n   for (int i = 0; i < m; ++i) { n     x = (a[i] + z) % n; n     y = b[i]; n     z = 0; n     while (true) { n       if (!gao.r[x]) { n         gao.next(x); n         y -= gao.q[x]; n         x = gao.p[x]; n       } n       if (gao.r[x] && y > 0) { n         ++z; n         gao.kill(x); n       } else { n         break; n       } n     } n     printf( %d n , z); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 4e4 + 5; n int pr[N], ps = 0; n bool np[N]; n bool check(int n) { n   if (n == 1 || n == 0) return false; n   for (int i = 0; pr[i] * pr[i] <= n; ++i) n     if (n % pr[i] == 0) return false; n   return true; n } n int main() { n   for (int i = 2; i < N; ++i) n     if (!np[i]) { n       pr[ps++] = i; n       for (int j = i + i; j < N; j += i) np[j] = 1; n     } n   int n; n   cin >> n; n   if (check(n)) { n     cout <<  1 n  << n << endl; n     return 0; n   } n   for (int i = 0; i < ps && pr[i] <= n; ++i) { n     if (check(n - pr[i])) { n       cout <<  2 n  << pr[i] <<     << n - pr[i] << endl; n       return 0; n     } n     if (pr[i] % 2 == 1 && check((n - pr[i]) / 2)) { n       cout <<  3 n  n            << pr[i] <<     << (n - pr[i]) / 2 <<     << (n - pr[i]) / 2 << endl; n       return 0; n     } n   } n   cout <<  lol nope n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mk = 100005; n long long n; n vector<long long> g[mk]; n long long find(long long A) { n   if (A == 0) return 0; n   long long tmp = A & 3; n   long long R = (find(A / 4) << 2); n   if (tmp == 0) return R; n   if (tmp == 1) return (R | 2); n   if (tmp == 2) return (R | 3); n   if (tmp == 3) return (R | 1); n } n void init() { n   long long test; n   cin >> test; n   for (long long i = 1; i <= test; i++) { n     long long n; n     cin >> n; n     long long root = 1; n     while (root * 4 <= n) root *= 4; n     n -= (root - 1); n     long long A = (root | ((n - 1) / 3)); n     long long B = find(A); n     long long C = A ^ B; n     if (n % 3 == 1) cout << A; n     if (n % 3 == 2) cout << B; n     if (n % 3 == 0) cout << C; n     cout <<   n ; n   } n } n signed main() { init(); } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e4 + 5; n pair<int, int> p[maxn]; n set<pair<int, int> > s; n void solve(int l, int r) { n   if (r - l < 2) return; n   int mid = (l + r) / 2; n   solve(l, mid), solve(mid, r); n   for (int i = l; i < r; i++) s.insert(make_pair(p[mid].first, p[i].second)); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) n     cin >> p[i].first >> p[i].second, n         s.insert(make_pair(p[i].first, p[i].second)); n   sort(p, p + n); n   solve(0, n); n   cout << s.size() << endl; n   for (pair<int, int> now : s) cout << now.first <<     << now.second << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n; n   long long int s; n   scanf( %d , &n); n   s = 6 * n * (n - 1) + 1; n   printf( %I64d , s); n   return 0; n } n


#include <bits/stdc++.h> n template <class C, class E> n inline bool contains(const C& container, const E& element) { n   return container.find(element) != container.end(); n } n template <class T> n inline void checkmin(T& a, T b) { n   if (b < a) a = b; n } n template <class T> n inline void checkmax(T& a, T b) { n   if (b > a) a = b; n } n using namespace std; n class UnionFind { n  public: n   UnionFind(int elements) : _subsets(elements), _setCount(elements) { n     for (int i = 0; i < elements; ++i) { n       _subsets[i].parent = i; n       _subsets[i].rank = 0; n     } n   } n   int Find(int i) { n     if (_subsets[i].parent != i) _subsets[i].parent = Find(_subsets[i].parent); n     return _subsets[i].parent; n   } n   void Union(int x, int y) { n     int xroot = Find(x); n     int yroot = Find(y); n     if (xroot == yroot) return; n     --_setCount; n     if (_subsets[xroot].rank < _subsets[yroot].rank) n       _subsets[xroot].parent = yroot; n     else if (_subsets[xroot].rank > _subsets[yroot].rank) n       _subsets[yroot].parent = xroot; n     else { n       _subsets[yroot].parent = xroot; n       _subsets[xroot].rank++; n     } n   } n   struct subset { n     int parent; n     int rank; n   }; n   void Trace() { n     for (int i = (0), _b = ((_subsets.size()) - 1); i <= _b; i++) n       cout << i <<  :  << Find(i) <<   | ; n     cout << endl; n   } n   vector<subset> _subsets; n   int _setCount; n }; n struct SetInfo { n   set<int> _yc; n   set<int> _xc; n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cout << std::setprecision(15); n   cout << std::fixed; n   long long xs, ys, q; n   cin >> xs >> ys >> q; n   UnionFind uf(xs + ys); n   vector<pair<int, int> > p(q); n   for (int i = (0), _b = ((q)-1); i <= _b; i++) { n     int x, y; n     cin >> p[i].first >> p[i].second; n     --p[i].first; n     --p[i].second; n     uf.Union(p[i].first, xs + p[i].second); n   } n   map<int, SetInfo> ss; n   for (int i = (0), _b = ((q)-1); i <= _b; i++) { n     int id = uf.Find(p[i].first); n     SetInfo& setInfo = ss[id]; n     setInfo._xc.insert(p[i].first); n     setInfo._yc.insert(p[i].second); n   } n   long long got = 0; n   long long totX = 0, totY = 0; n   for (auto& p : ss) { n     totX += (long long)p.second._xc.size(); n     totY += (long long)p.second._yc.size(); n   } n   long long remX = xs - totX; n   long long remY = ys - totY; n   long long res = remX + remY - 1 + ss.size(); n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T sqr(T x) { n   return x * x; n } n template <typename T> n T abs(T x) { n   return x < 0 ? -x : x; n } n template <typename T> n T gcd(T a, T b) { n   return b ? gcd(b, a % b) : a; n } n template <typename T> n bool chmin(T& x, const T& y) { n   if (x > y) { n     x = y; n     return true; n   } n   return false; n } n template <typename T> n bool chmax(T& x, const T& y) { n   if (x < y) { n     x = y; n     return true; n   } n   return false; n } n string to_bin(int x) { n   string ret; n   while (x) { n     ret = ret + (char)((x % 2) +  0 ); n     x /= 2; n   } n   return ret; n } n int main(int, char**) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     long long m = (n - 1) / 3; n     int k = 0; n     long long p = 1; n     while (m >= p) { n       m -= p; n       k += 1; n       p *= 4; n     } n     vector<long long> a; n     for (int i = 0; i < k; ++i) { n       a.push_back(m % 4); n       m /= 4; n     } n     long long A, B; n     A = 1LL << (2 * k); n     B = 1LL << (2 * k + 1); n     vector<long long> b = {0, 2, 3, 1}; n     for (int i = 0; i < k; ++i) { n       A ^= a[i] << (2 * i); n       B ^= b[a[i]] << (2 * i); n     } n     long long C = A ^ B; n     long long ans; n     if (n % 3 == 1) { n       ans = A; n     } else if (n % 3 == 2) { n       ans = B; n     } else { n       ans = C; n     } n     cout << ans <<   n ; n   } n   cerr <<  Time execute:   << clock() / (double)CLOCKS_PER_SEC <<   sec  n        << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double n, m, a, b; n   cin >> n >> m >> a >> b; n   double s = n / m; n   long long int s1 = floor(s) * m; n   long long int s2 = ceil(s) * m; n   long long int a1 = (n - s1) * b; n   long long int a2 = (s2 - n) * a; n   cout << (min(a1, a2)); n } n


#include <bits/stdc++.h> n using namespace std; n int res; n int tox[] = {0, 0, 1, -1}; n int toy[] = {1, -1, 0, 0}; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr), cout.tie(nullptr); n   string s; n   cin >> s; n   while (s.back() ==  0 ) s.erase(s.size() - 1, 1), res++; n   while (s !=  1 ) { n     while (s.size() && s.back() ==  1 ) s.erase(s.size() - 1, 1), res++; n     res++; n     if (!s.size()) break; n     s.back() =  1 ; n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   deque<long long int> b, g; n   long long int n; n   cin >> n; n   for (long long int i = 0; i < n; i++) { n     long long int temp; n     cin >> temp; n     b.push_back(temp); n   } n   cin >> n; n   for (long long int i = 0; i < n; i++) { n     long long int temp; n     cin >> temp; n     g.push_back(temp); n   } n   sort(b.begin(), b.end()); n   sort(g.begin(), g.end()); n   long long int ans = 0; n   while (b.size() && g.size()) { n     if (abs(b.front() - g.front()) <= 1) { n       ans++; n       b.pop_front(); n       g.pop_front(); n     } else { n       if (b.front() < g.front()) n         b.pop_front(); n       else n         g.pop_front(); n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1e9 + 7; n bool comp(pair<long long, long long> p1, pair<long long, long long> p2) { n   if (p1.first != p2.first) { n     return p1.first < p2.first; n   } else n     return p2.second < p1.second; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n; n   cin >> n; n   map<long long, vector<pair<long long, long long>>> m; n   for (long long i = 0; i < n; i++) { n     long long x, y; n     cin >> x >> y; n     long long level = max(x, y); n     m[level].push_back({x, y}); n   } n   vector<pair<long long, long long>> v; n   long long dp[200006][2]; n   memset(dp, INT_MAX, sizeof dp); n   dp[0][0] = 0; n   dp[0][1] = 0; n   pair<long long, long long> p_start, p_end; n   p_start = {0, 0}; n   p_start = {0, 0}; n   long long i = 1; n   for (auto it : m) { n     for (auto it1 : it.second) { n       v.push_back(it1); n     } n     sort(v.begin(), v.end(), comp); n     pair<long long, long long> start = v[0]; n     pair<long long, long long> end = v[v.size() - 1]; n     long long di = n         abs(start.first - end.first) + abs(start.second - end.second); n     long long a1 = dp[i - 1][0] + abs(p_start.first - end.first) + n                    abs(p_start.second - end.second) + di; n     long long a2 = dp[i - 1][1] + abs(p_end.first - end.first) + n                    abs(p_end.second - end.second) + di; n     dp[i][0] = min(a1, a2); n     a1 = dp[i - 1][0] + abs(p_start.first - start.first) + n          abs(p_start.second - start.second) + di; n     a2 = dp[i - 1][1] + abs(p_end.first - start.first) + n          abs(p_end.second - start.second) + di; n     dp[i][1] = min(a1, a2); n     p_start = start; n     p_end = end; n     v.clear(); n     i++; n   } n   cout << min(dp[i - 1][0], dp[i - 1][1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int INF = 9223372036854775807; n const long long int mod = 998244353; n long long int MOD(long long int a, long long int b) { n   if (a > b) n     return a - b; n   else n     return b - a; n } n long long int max3(long long int a, long long int b, long long int c) { n   return max(c, max(a, b)); n } n long long int min3(long long int a, long long int b, long long int c) { n   return min(a, min(b, c)); n } n long long int power(long long int x, long long int y) { n   long long int res = 1; n   while (y > 0) { n     if (y & 1) res = (res * x); n     y = y >> 1; n     x = (x * x); n   } n   return res; n } n long long int power1(long long int x, long long int y) { n   long long int res = 1; n   while (y > 0) { n     if (y & 1) res = (res * x) % mod; n     y = y >> 1; n     x = (x * x) % mod; n   } n   return res % mod; n } n long long int logg(long long int a) { n   long long int x = 0; n   while (a > 1) { n     x++; n     a /= 2; n   } n   return x; n } n long long int gcd(long long int a, long long int b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n long long int abso(long long int x) { n   if (x < 0) { n     return -x; n   } n   return x; n } n long long int ceiling(long long int a, long long int b) { n   if (a % b == 0) { n     return a / b; n   } else { n     return a / b + 1; n   } n } n long long int modinv(long long int x) { return power1(x, mod - 2); } n long long int par[20][400000]; n long long int sum[20][400000]; n long long int weight[400000]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int t; n   cin >> t; n   long long int cnt = 1; n   long long int last = 0; n   par[0][1] = 1; n   weight[1] = 0; n   weight[0] = INF; n   for (int i = 0; i < 20; i++) { n     for (int j = 0; j < 400000; j++) { n       par[i][j] = 0; n     } n   } n   for (int i = 0; i < 20; i++) { n     sum[i][1] = INF; n   } n   for (int i = 0; i < 20; i++) { n     for (int j = 2; j < 400000; j++) { n       sum[i][j] = 0; n     } n   } n   while (t--) { n     int ch; n     cin >> ch; n     long long int p, q; n     cin >> p >> q; n     p = p ^ last; n     q = q ^ last; n     if (ch == 1) { n       cnt++; n       weight[cnt] = q; n       long long int curr = p; n       if (weight[p] >= q) { n         curr = p; n         par[0][cnt] = curr; n       } else { n         for (int j = 19; j >= 0; j--) { n           if (weight[par[j][curr]] < weight[cnt]) { n             curr = par[j][curr]; n           } n         } n         par[0][cnt] = par[0][curr]; n       } n       if (par[0][cnt] != 0) { n         sum[0][cnt] = weight[par[0][cnt]]; n       } else { n         sum[0][cnt] = INF; n       } n       for (int i = 1; i < 20; i++) { n         par[i][cnt] = par[i - 1][par[i - 1][cnt]]; n       } n       for (int i = 1; i < 20; i++) { n         if (par[i][cnt] != 0 && sum[i - 1][cnt] != INF) { n           sum[i][cnt] = sum[i - 1][cnt] + sum[i - 1][par[i - 1][cnt]]; n         } else { n           sum[i][cnt] = INF; n         } n       } n     } else { n       long long int W = weight[p]; n       if (W > q) { n         cout << 0 << endl; n         last = 0; n       } else { n         long long int curr = p; n         long long int ans = 1; n         for (int j = 19; j >= 0; j--) { n           if (sum[j][curr] != INF && sum[j][curr] + W <= q) { n             ans += power(2, j); n             W += sum[j][curr]; n             curr = par[j][curr]; n           } n         } n         cout << ans << endl; n         last = ans; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1005; n const int Q = 1e9 + 7; n struct Point { n   int x, y; n   Point(int _x = 0, int _y = 0) : x(_x), y(_y) {} n   const int sign() const { return y < 0 || y == 0 && x < 0; } n   Point operator-(const Point &R) { return Point(x - R.x, y - R.y); } n   long long operator^(const Point &R) const { n     return (long long)x * R.y - (long long)y * R.x; n   } n   bool operator<(const Point &R) const { n     if (sign() != R.sign()) { n       return sign() < R.sign(); n     } n     return (*this ^ R) > 0; n   } n }; n inline void add(long long &A, long long B) { A += B; } n Point p[N]; n int n; n long long f[6][N][N]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %d%d , &p[i].x, &p[i].y); n     f[0][i][i] = 1; n   } n   vector<tuple<Point, int, int>> v; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < i; ++j) { n       v.emplace_back(p[j] - p[i], i, j); n       v.emplace_back(p[i] - p[j], j, i); n     } n   } n   sort(v.begin(), v.end()); n   for (auto &it : v) { n     int x = get<1>(it); n     int y = get<2>(it); n     for (int i = 5; i > 0; --i) { n       for (int j = 0; j < n; ++j) { n         add(f[i][j][y], f[i - 1][j][x]); n       } n     } n   } n   long long res = 0; n   for (int i = 0; i < n; ++i) { n     add(res, f[5][i][i]); n   } n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, l, r, sall, sk; n   cin >> n >> k >> l >> r >> sall >> sk; n   int el[n]; n   int div, mod, m, sm; n   m = n - k; n   sm = sall - sk; n   if (k > 0) { n     div = sk / k; n     mod = sk % k; n     for (int i = 0; i < k; i++) { n       el[i] = div; n       if (i < mod) { n         el[i] += 1; n       } n     } n   } n   if (m > 0) { n     div = sm / m; n     mod = sm % m; n     for (int i = 0; i < m; i++) { n       el[i + k] = div; n       if (i < mod) { n         el[i + k] += 1; n       } n     } n   } n   for (int i = 0; i < n; i++) { n     cout << el[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long ar[100005]; n long long N; n int main() { n   cin >> N; n   for (long long i = 0; i < N; i++) cin >> ar[i]; n   long long ans = 0ll; n   for (long long i = 0; i < N; i++) { n     long long lft = (i == 0 ? 0 : ar[i - 1]); n     if (ar[i] < lft) ans += (ar[i]) * (lft - ar[i]); n     if (ar[i] > lft) ans += (ar[i] - lft) * (N - ar[i] + 1); n   } n   cout << ans << endl; n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int n, m; n   cin >> n >> m; n   for (long long int i = 0; i < n; i += 1) { n     long long int s, f, t; n     cin >> s >> f >> t; n     long long int q = t / (m - 1); n     long long int r = t % (m - 1); n     if (s == f) { n       cout << t <<   n ; n       continue; n     } n     if (q % 2 == 0) { n       long long int floor = r + 1; n       if (s >= floor) { n         t += (s - floor); n         if (f >= s) { n           t += (f - s); n         } else { n           t += (m - s) + (m - f); n         } n       } else { n         t += (m - floor) + (m - s); n         if (f > s) { n           t += (s - 1) + (f - 1); n         } else { n           t += (s - f); n         } n       } n     } else { n       long long int floor = m - r; n       if (s <= floor) { n         t += (floor - s); n         if (f <= s) { n           t += (s - f); n         } else { n           t += (s - 1) + (f - 1); n         } n       } else { n         t += (floor - 1) + (s - 1); n         if (f >= s) { n           t += (f - s); n         } else { n           t += (m - s) + (m - f); n         } n       } n     } n     cout << t <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)1e5 + 5; n long long add[N << 2], sum[N << 2], color[N << 2]; n int n, m; n void down(int rt, int len) { n   if (add[rt]) { n     add[(rt << 1)] += add[rt]; n     add[(rt << 1 | 1)] += add[rt]; n     sum[(rt << 1)] += add[rt] * (len - (len >> 1)); n     sum[(rt << 1 | 1)] += add[rt] * (len >> 1); n     add[rt] = 0; n     color[(rt << 1)] = color[(rt << 1 | 1)] = color[rt]; n     color[rt] = 0; n   } n } n void build(int rt, int l, int r) { n   if (l == r) { n     color[rt] = l; n     return; n   } n   int mid = (l + r) >> 1; n   build((rt << 1), l, mid); n   build((rt << 1 | 1), mid + 1, r); n } n void modify(int rt, int l, int r, int L, int R, int x) { n   if (L <= l && r <= R && color[rt]) { n     add[rt] += abs(color[rt] - x); n     sum[rt] += (r - l + 1) * abs(color[rt] - x); n     color[rt] = x; n     return; n   } n   down(rt, r - l + 1); n   int mid = (l + r) >> 1; n   if (L <= mid) modify((rt << 1), l, mid, L, R, x); n   if (mid < R) modify((rt << 1 | 1), mid + 1, r, L, R, x); n   color[rt] = (color[(rt << 1)] == color[(rt << 1 | 1)]) ? color[(rt << 1)] : 0; n   sum[rt] = sum[(rt << 1)] + sum[(rt << 1 | 1)]; n } n long long query(int rt, int l, int r, int L, int R) { n   if (L <= l && r <= R) { n     return sum[rt]; n   } n   down(rt, r - l + 1); n   long long t = 0ll; n   int mid = (l + r) >> 1; n   if (L <= mid) t += query((rt << 1), l, mid, L, R); n   if (mid < R) t += query((rt << 1 | 1), mid + 1, r, L, R); n   return t; n } n int main() { n   scanf( %d%d , &n, &m); n   build(1, 1, n); n   while (m--) { n     int op, l, r, x; n     scanf( %d , &op); n     if (op == 1) { n       scanf( %d%d%d , &l, &r, &x); n       modify(1, 1, n, l, r, x); n     } else { n       scanf( %d%d , &l, &r); n       printf( %I64d n , query(1, 1, n, l, r)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool f[9000000]; n int a[30]; n bool check(int sta, int i) { n   if (sta & (1 << (i - 1))) n     ; n   else n     return false; n   for (int j = 0; j < i; j++) n     if (sta & (1 << j)) n       for (int k = j; k < i; k++) n         if (sta & (1 << k)) n           if (a[j] + a[k] == a[i]) return true; n   return false; n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   memset(f, false, sizeof(f)); n   f[1] = true; n   for (int i = 1; i < n; i++) { n     int l = 1 << i, r = 1 << (i + 1); n     for (int j = l; j < r; j++) n       if (f[j - l] && check(j, i)) { n         f[j] = true; n         for (int k = 0; k < i; k++) f[j & (~(1 << k))] = true; n       } n   } n   int ans = 30; n   for (int i = (1 << (n - 1)); i < (1 << n); i++) n     if (f[i]) { n       int total = 0; n       for (int j = 0; j < n; j++) n         if (i & (1 << j)) total++; n       ans = ((ans) < (total) ? (ans) : (total)); n     } n   printf( %d n , ans == 30 ? -1 : ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 500 + 1; n int a[maxn], n; n bool check(int r) { n   for (int i = 1; i <= n; i++) n     if (a[i] % r > 0 && r - 1 - a[i] % r > a[i] / r) return 0; n   return 1; n } n long long calc(int r) { n   long long res = 0; n   for (int i = 1; i <= n; i++) res += a[i] / r + (a[i] % r != 0); n   return res; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   int res = 1; n   for (int i = 1, j, m = a[1]; i <= m; i = j + 1) { n     int t = m / i; n     j = m / t; n     if (check(t + 1)) { n       res = t + 1; n       break; n     } n     if (check(t)) { n       res = t; n       break; n     } n   } n   cout << calc(res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 1005; n int n, m, k; n long long s[Maxn][Maxn], sl[Maxn][Maxn], sr[Maxn][Maxn]; n inline int get() { n   char ch; n   while (!isdigit(ch = getchar())) n     ; n   int v = ch - 48; n   while (isdigit(ch = getchar())) v = v * 10 + ch - 48; n   return v; n } n int main() { n   n = get(); n   m = get(); n   k = get(); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       s[i][j] = get() + s[i][j - 1] + s[i - 1][j] - s[i - 1][j - 1]; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       sl[i][j] = n           sl[i - 1][j - 1] + s[i][j] - (i > k && j > k ? s[i - k][j - k] : 0); n       sr[i][j] = sr[i - 1][j + 1] + s[i][j] - n                  (i > k && j + k <= m ? s[i - k][j + k] : 0); n     } n     sr[i][0] = sr[i - 1][1] + s[i][0] - (i > k ? s[i - k][k] : 0); n   } n   int wx = 0, wy = 0; n   long long maxv = -1; n   for (int i = k; i <= n - k + 1; i++) n     for (int j = k; j <= m - k + 1; j++) { n       long long v = sr[i + k - 1][j] + sr[i - 1][j - k] - sl[i - 1][j + k - 1] - n                     sl[i + k - 1][j - 1]; n       if (maxv < v) maxv = v, wx = i, wy = j; n     } n   cout << wx <<     << wy << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, sum[3] = {0}; n   cin >> n; n   int *x = new int[n], *y = new int[n], *z = new int[n]; n   for (int i = 0; i < n; i++) { n     cin >> x[i] >> y[i] >> z[i]; n     sum[0] += x[i]; n     sum[1] += y[i]; n     sum[2] += z[i]; n   } n   if (sum[0] == 0 && sum[1] == 0 && sum[2] == 0) n     cout <<  YES ; n   else n     cout <<  NO ; n   delete[] x; n   delete[] y; n   delete[] z; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct man { n   long long int m, s, I; n }; n man a[131072]; n bool cmp(man a, man b) { n   if (a.m < b.m) return true; n   if (a.m > b.m) return false; n   if (a.I > b.I) return true; n   if (a.I < b.I) return false; n } n int main() { n   queue<man> x; n   long long int n, d, i, tbr = 0, maxbr = 0; n   cin >> n >> d; n   for (i = 0; i < n; i++) { n     cin >> a[i].m >> a[i].s; n     a[i].I = i; n   } n   sort(a, a + n, cmp); n   for (i = 0; i < n; i++) { n     x.push(a[i]); n     struct man m = x.front(), m2 = x.back(); n     while (m2.m - m.m >= d) { n       tbr -= m.s; n       x.pop(); n       m = x.front(); n       m2 = x.back(); n     } n     tbr += m2.s; n     if (tbr > maxbr) maxbr = tbr; n   } n   cout << maxbr << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000007; n const long long INF = 1000000000; n struct wie { n   int il_50, il_100, str; n }; n int main() { n   ios_base::sync_with_stdio(0); n   int n, k; n   cin >> n >> k; n   int p = 0, s = 0; n   for (int i = 0; i < n; ++i) { n     int x; n     cin >> x; n     if (x == 50) n       ++p; n     else n       ++s; n   } n   vector<vector<long long> > C(51, vector<long long>(51)); n   for (int i = 0; i <= 50; ++i) { n     for (int j = 0; j <= i; ++j) { n       if (j == 0 || j == i) n         C[i][j] = 1; n       else n         C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % MOD; n     } n   } n   long long il[51][51][2]; n   long long odl[51][51][2]; n   bool odw[51][51][2]; n   for (int i = 0; i < 51; ++i) { n     for (int j = 0; j < 51; ++j) { n       for (int l = 0; l < 2; ++l) { n         odw[i][j][l] = false; n         odl[i][j][l] = -1; n       } n     } n   } n   queue<wie> Q; n   Q.push({p, s, 1}); n   odw[p][s][1] = true; n   while (!Q.empty()) { n     wie akt = Q.front(); n     Q.pop(); n     if (akt.il_100 == s && akt.il_50 == p && akt.str == 1) { n       odl[akt.il_50][akt.il_100][akt.str] = 0; n       il[akt.il_50][akt.il_100][akt.str] = 1; n       for (int i = 0; i <= akt.il_50; ++i) { n         for (int j = 0; j <= akt.il_100; ++j) { n           wie nowy = {p - akt.il_50 + i, s - akt.il_100 + j, (akt.str ^ 1)}; n           if (i + j != 0 && 50 * i + 100 * j <= k) { n             Q.push(nowy); n             odw[nowy.il_50][nowy.il_100][nowy.str] = true; n           } n         } n       } n     } else { n       odl[akt.il_50][akt.il_100][akt.str] = INF; n       il[akt.il_50][akt.il_100][akt.str] = 0; n       for (int i = 0; i <= akt.il_50; ++i) { n         for (int j = 0; j <= akt.il_100; ++j) { n           wie nowy = {p - akt.il_50 + i, s - akt.il_100 + j, (akt.str ^ 1)}; n           if (i + j != 0 && 50 * i + 100 * j <= k) { n             if (odw[nowy.il_50][nowy.il_100][nowy.str]) { n               if (odl[nowy.il_50][nowy.il_100][nowy.str] != -1) { n                 odl[akt.il_50][akt.il_100][akt.str] = n                     min(odl[akt.il_50][akt.il_100][akt.str], n                         odl[nowy.il_50][nowy.il_100][nowy.str] + 1); n                 il[akt.il_50][akt.il_100][akt.str] = n                     (il[akt.il_50][akt.il_100][akt.str] + n                      (C[akt.il_50][i] * C[akt.il_100][j]) % MOD * n                          il[nowy.il_50][nowy.il_100][nowy.str]) % n                     MOD; n               } n             } else { n               Q.push(nowy); n               odw[nowy.il_50][nowy.il_100][nowy.str] = true; n             } n           } n         } n       } n     } n   } n   if (odw[p][s][0]) n     cout << odl[p][s][0] <<     << il[p][s][0]; n   else n     cout <<  -1 0 ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1000 + 10, M = 998244353; n int t; n int n, m, q; n bool locked[N][N]; n long long num[N][N][2]; n long long ans = 0; n int calc(int i, int j, int t) { n   if (i > n || j > m) return 0; n   if (num[i][j][t] != 0) return num[i][j][t]; n   num[i][j][t] = 1; n   if (t == 0) n     num[i][j][t] += calc(i + 1, j, t ^ 1); n   else n     num[i][j][t] += calc(i, j + 1, t ^ 1); n   return num[i][j][t]; n } n void go(int i, int j, int t, int val) { n   if (i <= 0 || j <= 0 || locked[i][j]) return; n   if (t == 0) { n     num[i][j][t] -= val; n     ans -= val; n     return go(i, j - 1, t ^ 1, val); n   } else { n     num[i][j][t] -= val; n     ans -= val; n     return go(i - 1, j, t ^ 1, val); n   } n } n void go2(int i, int j, int t) { n   num[i][j][t] = 1; n   ans++; n   if (t == 0) { n     if (i < n) { n       num[i][j][t] += num[i + 1][j][t ^ 1]; n       ans += num[i + 1][j][t ^ 1]; n     } n   } else { n     if (j < m) { n       num[i][j][t] += num[i][j + 1][t ^ 1]; n       ans += num[i][j + 1][t ^ 1]; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> m >> q; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       for (int k = 0; k < 2; k++) ans += calc(i, j, k); n     } n   } n   long long free = n * m; n   while (q--) { n     int x, y; n     cin >> x >> y; n     if (!locked[x][y]) { n       go(x, y, 0, num[x][y][0]); n       go(x, y, 1, num[x][y][1]); n       locked[x][y] = 1; n       free--; n     } else { n       go2(x, y, 0); n       go2(x, y, 1); n       locked[x][y] = 0; n       go(x, y, 0, -num[x][y][0]); n       go(x, y, 1, -num[x][y][1]); n       num[x][y][0] /= 2; n       num[x][y][1] /= 2; n       ans -= num[x][y][0] + num[x][y][1]; n       free++; n     } n     cout << ans - free <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int values[400][400]; n int mem[400][400][2]; n void read() { n   cin >> n; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       cin >> values[i][j]; n       mem[i][j][0] = 0; n       mem[i][j][1] = 0; n     } n   } n } n void add(int x1, int x2, int c, int v) { n   if (x1 > x2) return; n   int lo = max(0, c - n + 1), hi = min(n - 1, c); n   if (x1 < lo || x2 > hi) return; n   int y1 = c - x1, y2 = c - x2; n   v += values[x1][y1] + values[x2][y2]; n   if (x1 == x2) v -= values[x1][y1]; n   if (v > mem[x1][x2][c & 1]) mem[x1][x2][c & 1] = v; n } n void solve() { n   mem[0][0][0] = values[0][0]; n   for (int c = 0; c <= 2 * n - 3; ++c) { n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j < n; ++j) { n         mem[i][j][(c + 1) & 1] = -1000 * 400 * 2; n       } n     } n     int lo = max(0, c - n + 1), hi = min(n - 1, c); n     for (int x1 = lo; x1 <= hi; ++x1) { n       for (int x2 = x1; x2 <= hi; ++x2) { n         add(x1, x2, c + 1, mem[x1][x2][c & 1]); n         add(x1 + 1, x2, c + 1, mem[x1][x2][c & 1]); n         add(x1, x2 + 1, c + 1, mem[x1][x2][c & 1]); n         add(x1 + 1, x2 + 1, c + 1, mem[x1][x2][c & 1]); n       } n     } n   } n } n int main() { n   read(); n   solve(); n   cout << mem[n - 1][n - 1][0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int modulo = 1e9 + 7; n int n; n const int limite = 100001; n long long int cuantoshasta[limite], sumahasta[limite], valor[limite], n     prodhasta[limite]; n long long int cuatros[limite][10], sietes[limite][10], diezala[limite], n     dosala[limite], sumahasta7[limite][10], prodhasta7[limite][10]; n long long int d[limite]; n long long int computa(string &s) { n   for (int i = 0; i < n; i++) d[i] = s[n - 1 - i] -  0 ; n   valor[0] = d[0]; n   if (d[0] == 4) { n     cuantoshasta[0] = 1; n     sumahasta[0] = 4; n     prodhasta[0] = 0; n   } else { n     cuantoshasta[0] = 2; n     sumahasta[0] = 11; n     prodhasta[0] = 28; n   } n   for (int i = 1; i < n; i++) { n     valor[i] = (valor[i - 1] + diezala[i] * d[i]) % modulo; n     if (d[i] == 4) { n       cuantoshasta[i] = cuantoshasta[i - 1]; n       sumahasta[i] = n           (4 * diezala[i] % modulo * cuantoshasta[i - 1] + sumahasta[i - 1]) % n           modulo; n       prodhasta[i] = n           (4 * diezala[i] % modulo * n                (2 * sumahasta[i - 1] - cuatros[i - 1][4] - valor[i - 1] + n                 4 * diezala[i] % modulo * (cuantoshasta[i - 1] - 1) % modulo) % n                modulo + n            prodhasta[i - 1]) % n           modulo; n     } else { n       cuantoshasta[i] = (cuantoshasta[i - 1] + dosala[i]) % modulo; n       sumahasta[i] = n           (sumahasta7[i][4] + 7 * diezala[i] % modulo * cuantoshasta[i - 1] + n            sumahasta[i - 1]) % n           modulo; n       prodhasta[i] = n           (prodhasta7[i][4] + sietes[i][4] * cuatros[i][7] % modulo + n            7 * diezala[i] % modulo * n                (2 * sumahasta[i - 1] - cuatros[i - 1][4] - valor[i - 1] + n                 7 * diezala[i] % modulo * (cuantoshasta[i - 1] - 1) % modulo) % n                modulo + n            prodhasta[i - 1]) % n           modulo; n     } n   } n   return prodhasta[n - 1]; n } n int main() { n   ios::sync_with_stdio(false); n   string sl, sr; n   cin >> sl >> sr; n   n = int(sl.size()); n   cuatros[0][4] = 4; n   cuatros[0][7] = 7; n   sietes[0][4] = 4; n   sietes[0][7] = 7; n   diezala[0] = 1; n   dosala[0] = 1; n   sumahasta7[0][4] = 4; n   sumahasta7[0][7] = 11; n   prodhasta7[0][4] = 0; n   prodhasta7[0][7] = 28; n   for (int i = 1; i < n; i++) { n     diezala[i] = (diezala[i - 1] * 10) % modulo; n     dosala[i] = (dosala[i - 1] * 2) % modulo; n     cuatros[i][4] = (cuatros[i - 1][4] + 4 * diezala[i]) % modulo; n     cuatros[i][7] = (cuatros[i - 1][4] + 7 * diezala[i]) % modulo; n     sietes[i][4] = (sietes[i - 1][7] + 4 * diezala[i]) % modulo; n     sietes[i][7] = (sietes[i - 1][7] + 7 * diezala[i]) % modulo; n     sumahasta7[i][4] = n         (4 * diezala[i] % modulo * dosala[i] + sumahasta7[i - 1][7]) % modulo; n     sumahasta7[i][7] = (sumahasta7[i][4] + 7 * diezala[i] % modulo * dosala[i] + n                         sumahasta7[i - 1][7]) % n                        modulo; n     prodhasta7[i][4] = n         (4 * diezala[i] % modulo * n              (2 * sumahasta7[i - 1][7] - cuatros[i - 1][4] - sietes[i - 1][7] + n               4 * diezala[i] % modulo * (dosala[i] - 1) % modulo) % n              modulo + n          prodhasta7[i - 1][7]) % n         modulo; n     prodhasta7[i][7] = n         (prodhasta7[i][4] + sietes[i][4] * cuatros[i][7] % modulo + n          7 * diezala[i] % modulo * n              (2 * sumahasta7[i - 1][7] - cuatros[i - 1][4] - sietes[i - 1][7] + n               7 * diezala[i] % modulo * (dosala[i] - 1) % modulo) % n              modulo + n          prodhasta7[i - 1][7]) % n         modulo; n   } n   cout << ((computa(sr) - computa(sl)) % modulo + modulo) % modulo << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long x, y, z; n   long long x1, y1, z1; n   vector<long long> a(6); n   long long sum = 0; n   cin >> x >> y >> z; n   cin >> x1 >> y1 >> z1; n   for (int i = 0; i < 6; ++i) { n     long long b; n     cin >> b; n     a[i] = b; n   } n   if (x < 0) { n     sum += a[4]; n   } else if (x > x1) { n     sum += a[5]; n   } n   if (y < 0) { n     sum += a[0]; n   } else if (y > y1) { n     sum += a[1]; n   } n   if (z < 0) { n     sum += a[2]; n   } else if (z > z1) { n     sum += a[3]; n   } n   cout << sum; n } n


#include <bits/stdc++.h> n struct node { n   double x, y, z; n } p[5005]; n double d[5005]; n double getlen(node a, node b) { n   double len; n   len = (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) + n         (a.z - b.z) * (a.z - b.z); n   return sqrt(len); n } n int main() { n   int n, i, j, a, b, c; n   while (scanf( %d , &n) != EOF) { n     for (i = 0; i < n; i++) { n       scanf( %d %d %d , &a, &b, &c); n       p[i].x = a * 1.0; n       p[i].y = b * 1.0; n       p[i].z = c * 1.0; n     } n     for (i = 1; i < n; i++) { n       d[i] = getlen(p[0], p[i]); n     } n     double lenn; n     double max = 9999999.0; n     for (i = 1; i < n; i++) { n       for (j = 1; j <= i; j++) { n         if (j == i) continue; n         lenn = d[i] + d[j] + getlen(p[i], p[j]); n         if (max - lenn > 1e-6) max = lenn; n       } n     } n     printf( %.10f n , max * 1.0 / 2.0); n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n; n   scanf( %d , &n); n   if (n == 2) { n     printf( -1 n ); n     return 0; n   } n   printf( 6 n10 n15 n ); n   for (int i = 4; i <= n; i++) { n     printf( 6 ); n     for (int zero = 1; zero <= i - 4 + 1; zero++) printf( 0 ); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     int x; n     cin >> x; n     int a[s.length()]; n     for (int i = 0; i < s.length(); i++) { n       a[i] = 1; n     } n     for (int i = 0; i < s.length(); i++) { n       if (s[i] ==  0 ) { n         if (i - x >= 0) { n           a[i - x] = 0; n         } n         if (i + x < s.length()) { n           a[i + x] = 0; n         } n       } n     } n     int tag = 0; n     for (int i = 0; i < s.length(); i++) { n       if (s[i] ==  1 ) { n         int tag1 = 0; n         if (i - x >= 0) { n           if (a[i - x] == 1) { n             a[i - x] = 1; n             tag1++; n           } n         } n         if (i + x < s.length()) { n           if (a[i + x] == 1) { n             tag1++; n           } n         } n         if (tag1 == 0) { n           tag = 1; n           break; n         } n       } n     } n     if (tag == 1) { n       cout <<  -1  << endl; n     } else { n       for (int i = 0; i < s.length(); i++) { n         cout << a[i]; n       } n       cout << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, a, b, c, d, e, R, B, P, ans; n char s; n inline long long read() { n   long long sum = 0, x = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) x = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     sum = sum * 10 + ch -  0 ; n     ch = getchar(); n   } n   return sum * x; n } n inline void write(long long x) { n   if (x < 0) { n     putchar( - ); n     x = -x; n   } n   if (x > 9) write(x / 10); n   putchar(x % 10 +  0 ); n   return; n } n signed main() { n   n = read(); n   for (register long long i = 1; i <= n; ++i) { n     m = read(); n     cin >> s; n     if (s ==  P  || s ==  R ) { n       if (R) { n         ans += m - b; n         a = max(a, m - b); n       } n       R = 1; n       b = m; n     } n     if (s ==  P  || s ==  B ) { n       if (B) { n         ans += m - c; n         d = max(d, m - c); n       } n       B = 1; n       c = m; n     } n     if (s ==  P ) { n       if (P) ans += min(0ll, m - e - d - a); n       P = 1; n       a = d = 0; n       e = m; n     } n   } n   write(ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long LINF = 0x3f3f3f3f3f3f3f3fll; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long n, x, y; n   cin >> n >> x >> y; n   long long a1 = y - n + 1; n   if (a1 <= 0 or a1 * a1 + n - 1 < x) return cout << -1 <<   n , 0; n   cout << a1 <<   n ; n   for (int i = 0; i < n - 1; i++) cout << 1 <<   n ; n   exit(0); n } n


#include <bits/stdc++.h> n using namespace std; n int T; n priority_queue<int, vector<int>, greater<int> > q; n vector<int> g[1000005]; n int main() { n   scanf( %d , &T); n   int n; n   long long int ans; n   while (T--) { n     ans = 0; n     scanf( %d , &n); n     int x, y; n     for (int i = 1; i <= n; i++) { n       scanf( %d %d , &x, &y); n       g[x].push_back(y); n     } n     for (int i = n - 1; i >= 0; i--) { n       int sz = g[i].size(); n       for (int j = 0; j < sz; j++) q.push(g[i][j]); n       while (q.size() > n - i) { n         ans += q.top(), q.pop(); n       } n     } n     printf( %lld n , ans); n     for (int i = 0; i < n; ++i) { n       g[i].clear(); n       while (!q.empty()) q.pop(); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n const double EPS = 1e-24; n const long long int MOD = 1000000007ll; n const long long int MOD1 = 1000000009ll; n const long long int MOD2 = 1100000009ll; n const double PI = 3.14159265359; n int INF = 2147483645; n long long int INFINF = 9223372036854775807; n template <class T> n T Max2(T a, T b) { n   return a < b ? b : a; n } n template <class T> n T Min2(T a, T b) { n   return a < b ? a : b; n } n template <class T> n T Max3(T a, T b, T c) { n   return Max2(Max2(a, b), c); n } n template <class T> n T Min3(T a, T b, T c) { n   return Min2(Min2(a, b), c); n } n template <class T> n T Max4(T a, T b, T c, T d) { n   return Max2(Max2(a, b), Max2(c, d)); n } n template <class T> n T Min4(T a, T b, T c, T d) { n   return Min2(Min2(a, b), Min2(c, d)); n } n using namespace std; n int bit_count(long long int _x) { n   int _ret = 0; n   while (_x) { n     if (_x % 2 == 1) _ret++; n     _x /= 2; n   } n   return _ret; n } n int bit(long long int _mask, long long int _i) { n   return (_mask & (1 << _i)) == 0 ? 0 : 1; n } n long long int powermod(long long int _a, long long int _b, long long int _m) { n   long long int _r = 1; n   while (_b) { n     if (_b % 2 == 1) _r = (_r * _a) % _m; n     _b /= 2; n     _a = (_a * _a) % _m; n   } n   return _r; n } n int x[5], y[5]; n bool issquare[30010]; n int sz, sz2; n char s[110][110]; n char c[5]; n void type2(int a1, int a2, int b1, int b2, int c1, int c2, int i1, int i2, n            int i3) { n   for (int i = 1; i <= a1; i++) n     for (int j = 1; j <= a2; j++) s[i][j] = c[i1]; n   for (int i = a1 + 1; i <= a1 + b1; i++) n     for (int j = 1; j <= b2; j++) s[i][j] = c[i2]; n   for (int i = 1; i <= c1; i++) n     for (int j = a2 + 1; j <= a2 + c2; j++) s[i][j] = c[i3]; n } n void print() { n   cout << sz <<   n ; n   for (int i = 1; i <= sz; i++) { n     for (int j = 1; j <= sz; j++) cout << s[i][j]; n     cout <<   n ; n   } n } n int main() { n   std::ios::sync_with_stdio(false); n   srand(time(NULL)); n   for (int i = 1; i <= 3; i++) cin >> x[i] >> y[i]; n   c[1] =  A ; n   c[2] =  B ; n   c[3] =  C ; n   for (int i = 2; i * i <= 30010; i++) issquare[i * i] = true; n   sz2 = 0; n   for (int i = 1; i <= 3; i++) sz2 += x[i] * y[i]; n   if (!issquare[sz2]) { n     cout <<  -1 ; n     return 0; n   } n   for (int i = 2; i * i <= 30010; i++) { n     if (i * i == sz2) { n       sz = i; n       break; n     } n   } n   for (int i = 1; i <= 3; i++) n     if (x[i] > y[i]) swap(x[i], y[i]); n   if (y[1] == sz && y[2] == sz && y[3] == sz && x[1] + x[2] + x[3] == sz) { n     cout << sz <<   n ; n     for (int i = 1; i <= x[1]; i++) { n       for (int j = 1; j <= y[1]; j++) cout <<  A ; n       cout <<   n ; n     } n     for (int i = 1; i <= x[2]; i++) { n       for (int j = 1; j <= y[2]; j++) cout <<  B ; n       cout <<   n ; n     } n     for (int i = 1; i <= x[3]; i++) { n       for (int j = 1; j <= y[3]; j++) cout <<  C ; n       cout <<   n ; n     } n     return 0; n   } n   for (int i = 1; i <= 3; i++) n     for (int j = 1; j <= 3; j++) n       for (int k = 1; k <= 3; k++) { n         if (i == j || j == k || i == k) continue; n         if (x[j] + x[k] == sz && y[j] == y[k]) { n           if (x[i] == sz && y[i] == sz - y[j]) { n             type2(x[j], y[j], x[k], y[k], x[i], y[i], j, k, i); n             print(); n             return 0; n           } n           if (y[i] == sz && x[i] == sz - y[j]) { n             type2(x[j], y[j], x[k], y[k], y[i], x[i], j, k, i); n             print(); n             return 0; n           } n         } else if (x[j] + y[k] == sz && y[j] == x[k]) { n           if (x[i] == sz && y[i] == sz - y[j]) { n             type2(x[j], y[j], y[k], x[k], x[i], y[i], j, k, i); n             print(); n             return 0; n           } n           if (y[i] == sz && x[i] == sz - y[j]) { n             type2(x[j], y[j], y[k], x[k], y[i], x[i], j, k, i); n             print(); n             return 0; n           } n         } else if (y[j] + x[k] == sz && x[j] == y[k]) { n           if (x[i] == sz && y[i] == sz - x[j]) { n             type2(y[j], x[j], x[k], y[k], x[i], y[i], j, k, i); n             print(); n             return 0; n           } n           if (y[i] == sz && x[i] == sz - x[j]) { n             type2(y[j], x[j], x[k], y[k], y[i], x[i], j, k, i); n             print(); n             return 0; n           } n         } else if (y[j] + y[k] == sz && x[j] == x[k]) { n           if (x[i] == sz && y[i] == sz - x[j]) { n             type2(y[j], x[j], y[k], x[k], x[i], y[i], j, k, i); n             print(); n             return 0; n           } n           if (y[i] == sz && x[i] == sz - x[j]) { n             type2(y[j], x[j], y[k], x[k], y[i], x[i], j, k, i); n             print(); n             return 0; n           } n         } n       } n   cout <<  -1 ; 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   string s; n   int l1, l, r, count = 0; n   cin >> s; n   l1 = s.size(); n   int arr[l1]; n   memset(arr, 0, sizeof(arr)); n   int last = -1; n   for (int i = 0; i < l1;) { n     if (s[i] == s[i + 1]) { n       l = i; n       r = i + 1; n       while (s[l] == s[r] && l >= 0 && r < l1) { n         count++; n         s.erase(l, 1); n         s.erase(l, 1); n         l--; n         r--; n         l1 -= 2; n       } n       i = r - 1; n       last = r - 1; n     } n     i++; n   } n   if (count % 2) n     cout <<  Yes n ; n   else n     cout <<  No ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<long long int, long long int>> a; n vector<pair<long long int, long long int>> b; n vector<long long int> c; n long long int n, m, T; n bool solve(long long int d) { n   priority_queue<long long int, vector<long long int>, greater<long long int>> n       s; n   long long int j = 0; n   long long int sum = 0; n   for (long long int i = 0; i < m; i = i + d) { n     while (j < n && a[j].first >= b[i].first) { n       s.push(c[a[j].second]); n       j++; n     } n     if (s.empty()) return false; n     long long int temp = s.top(); n     s.pop(); n     sum += temp; n   } n   return sum <= T; n } n signed main(void) { n   std::ios::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t = 1; n   while (t--) { n     cin >> n >> m >> T; n     a.resize(0); n     b.resize(0); n     c.resize(n); n     for (long long int i = 0; i < m; i++) { n       long long int temp; n       cin >> temp; n       b.push_back({temp, i}); n     } n     for (long long int i = 0; i < n; i++) { n       long long int temp; n       cin >> temp; n       a.push_back({temp, i}); n     } n     for (long long int i = 0; i < n; i++) cin >> c[i]; n     sort(b.begin(), b.end(), greater<pair<long long int, long long int>>()); n     sort(a.begin(), a.end(), greater<pair<long long int, long long int>>()); n     long long int l = 1; n     long long int r = m; n     long long int ans = -1; n     while (l <= r) { n       long long int mid = (l + r) / 2; n       if (solve(mid)) { n         r = mid - 1; n         ans = mid; n       } else n         l = mid + 1; n     } n     if (ans == -1) { n       cout <<  NO  << endl; n       continue; n     } n     cout <<  YES  << endl; n     priority_queue<pair<long long int, long long int>, n                    vector<pair<long long int, long long int>>, n                    greater<pair<long long int, long long int>>> n         s; n     long long int j = 0; n     long long int sum = 0; n     vector<long long int> p(m); n     for (long long int i = 0; i < m; i = i + ans) { n       while (j < n && a[j].first >= b[i].first) { n         s.push({c[a[j].second], a[j].second}); n         j++; n       } n       long long int index = s.top().second; n       s.pop(); n       for (long long int k = i; k < min(i + ans, m); k++) { n         p[b[k].second] = index; n       } n     } n     for (long long int i = 0; i < m; i++) cout << p[i] + 1 <<    ; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 6e1; n map<string, int> mp; n int n; n int answer = 1; n string To(string a) { n   for (int i = 0; i < a.size(); i++) n     if (a[i] >  Z ) a[i] = a[i] - 32; n   return a; n } n int main() { n   mp[ POLYCARP ] = 1; n   cin >> n; n   for (int i = 0; i < n; i++) { n     string n1, r, n2; n     cin >> n1 >> r >> n2; n     n1 = To(n1); n     n2 = To(n2); n     mp[n1] = mp[n2] + 1; n     answer = max(answer, mp[n1]); n   } n   cout << answer; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10; n struct Node { n   long long l, r; n } dict[maxn]; n long long N, s; n inline bool check(long long mid) { n   long long cnt = 0, cost = 0; n   priority_queue<long long, vector<long long>, greater<long long>> q; n   for (int i = 1; i <= N; ++i) { n     cost += dict[i].l; n     if (dict[i].l < mid && dict[i].r >= mid) { n       q.push(mid - dict[i].l); n     } else if (dict[i].l >= mid) { n       ++cnt; n     } n   } n   while (!q.empty() && cnt < (N + 1) / 2) { n     cost += q.top(); n     q.pop(); n     cnt++; n   } n   return cost <= s && cnt >= (N + 1) / 2; n } n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     scanf( %lld%lld , &N, &s); n     for (int i = 1; i <= N; ++i) { n       scanf( %lld%lld , &dict[i].l, &dict[i].r); n     } n     long long l = 1, r = 1e14, ans; n     while (l <= r) { n       long long mid = (l + r) >> 1; n       if (check(mid)) n         ans = mid, l = mid + 1; n       else n         r = mid - 1; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t = 1, n, k = 0, flag = -1, b = 0; n   while (t--) { n     scanf( %lld , &n); n     k = 0; n     while (n != flag) { n       if (n / 10 == 0 && b == 0) { n         flag = n; n         b = 1; n       } n       k++; n       n++; n       while (n % 10 == 0) { n         n = n / 10; n       } n     } n     printf( %lld n , k); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t[40008]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n, m, k, l, p; n   cin >> n; n   string s; n   map<string, string> ex; n   map<string, int> kol; n   map<string, int> wh; n   for (int u = 1; u <= n; u++) { n     cin >> s; n     for (int i = 0; i < s.length(); i++) { n       string q; n       for (int j = i; j < s.length(); j++) { n         q += s[j]; n         if (wh[q] == u) continue; n         if (kol[q] == 0) { n           ex[q] = s; n         } n         wh[q] = u; n         kol[q]++; n       } n     } n   } n   int kk; n   cin >> kk; n   for (int i = 0; i < kk; i++) { n     string s; n     cin >> s; n     int kko = kol[s]; n     cout << kko <<    ; n     if (kko == 0) { n       cout <<  - n ; n     } else { n       cout << ex[s] <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   int mid = (m + 1) / 2; n   int cnt = 0; n   int ans[m + 1]; n   if (m % 2) { n     int i = 1; n     int cnt = 1; n     int k = 1; n     ans[k++] = mid; n     while (true) { n       if (cnt > m) break; n       ans[k++] = mid - i; n       cnt++; n       if (cnt > m) break; n       ans[k++] = mid + i; n       cnt++; n       i++; n     } n   } else { n     int i = 1; n     int cnt = 0; n     int k = 1; n     while (true) { n       if (cnt > m) break; n       ans[k++] = mid - i + 1; n       cnt++; n       if (cnt > m) break; n       ans[k++] = mid + i; n       cnt++; n       i++; n     } n   } n   int k = 1; n   for (int i = (1); i <= (n); i++) { n     cout << ans[k++] << endl; n     if (k > m) k = 1; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, d[105]; n char x[205]; n int main() { n   scanf( %d%d%s , &n, &m, x); n   d[0] = 1; n   for (int i = 0; i < n; i++) n     if (x[i] ==  1  && d[i]) n       for (int j = 1; j <= m; j++) n         if (x[i + j] ==  1  && (!d[i + j] || d[i + j] > d[i] + 1)) n           d[i + j] = d[i] + 1; n   printf( %d n , d[n - 1] - 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n  n int main(){ n  int t,n,d; n  int a[110],b[110]; n   n   n  scanf( %d ,&t); n  while(t--){ n   scanf( %d%d ,&n,&d); n   int fir=200,sec=200; n   int fid,sid; n   int x[110]; n   int xid[110]; n   int c=0; n    n   for(int i=1;i<=n;i++){ n    scanf( %d ,&a[i]); n    if(a[i]<fir){ n     sec=fir;fir=a[i]; n     sid=fid;fid=i; n    } n    else if(a[i]<sec){sec=a[i];sid=i;} n    if(a[i]>d){ n     x[++c]=a[i]; n     xid[c]=i; n    } n   } n   if(fir>d||sec>d||(c>=1&&fir+sec>d))printf( NO n ); n   else printf( YES n ); n  } n  return 0; n } n    n    n


#include <bits/stdc++.h> n using namespace std; n template <typename T1, typename T2> n inline void chkmin(T1 &first, T2 second) { n   if (first > second) first = second; n } n template <typename T1, typename T2> n inline void chkmax(T1 &first, T2 second) { n   if (first < second) first = second; n } n const string FileName =  input ; n const int MAXN = 3e5 + 100; n int n, k, b, c; n vector<int> arr; n vector<int> event[5]; n long long sum, mod, ans = 1e18; n multiset<long long> que; n long long f(long long s, long long t) { n   return ((t - s) / 5) * b + ((t - s) % 5) * c; n } n int main(int argc, char const *argv[]) { n   srand(time(0)); n   ios::sync_with_stdio(0); n   cin >> n >> k >> b >> c; n   arr.resize(n); n   b = min(b, c * 5); n   for (int i = 0; i < n; ++i) { n     cin >> arr[i]; n   } n   sort(arr.begin(), arr.end()); n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < 5; ++j) { n       event[(((arr[i] + j) % 5) + 5) % 5].push_back(arr[i] + j); n     } n   } n   for (int j = 0; j < 5; ++j) { n     int L = 0; n     sum = 0, mod = 0; n     que.clear(); n     for (int i = 0; i < n; ++i) { n       while (L < n && arr[L] <= event[j][i]) { n         que.insert(-f(arr[L], event[j][i]) - mod); n         sum += f(arr[L], event[j][i]); n         ++L; n       } n       while (que.size() > k) { n         sum += *que.begin() + mod; n         que.erase(que.begin()); n       } n       if (que.size() == k) { n         ans = min(ans, sum); n       } n       if (i != n - 1) { n         assert(event[j][i] <= event[j][i + 1]); n         mod -= f(event[j][i], event[j][i + 1]); n         sum += f(event[j][i], event[j][i + 1]) * que.size(); n       } n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, a[400010]; n map<int, int> m; n set<pair<int, int> > s; n long long ans = 0; n int main() { n   cin >> n >> k; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   a[0] = 0; n   for (int l = 1, r = 0; l <= n; ++l) { n     s.erase(make_pair(-m[a[l - 1]], a[l - 1])); n     m[a[l - 1]]--; n     s.insert(make_pair(-m[a[l - 1]], a[l - 1])); n     while (-s.begin()->first < k) { n       ++r; n       if (r >= n + 1) break; n       s.erase(make_pair(-m[a[r]], a[r])); n       m[a[r]]++; n       s.insert(make_pair(-m[a[r]], a[r])); n     } n     ans += max(n + 1 - r, 0); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     long long a[n]; n     vector<long long> v; n     for (long long i = 0; i < n; i++) { n       cin >> a[i]; n       v.push_back(a[i]); n     } n     bool ans = true; n     sort(v.begin(), v.end()); n     for (long long i = 0; i < n; i++) { n       if (a[i] != v[i] && a[i] % v[0] > 0) { n         ans = false; n         break; n       } n     } n     if (ans) n       cout <<  YES ; n     else n       cout <<  NO ; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 10; n int fa[maxn], cnt[maxn], vis[maxn], arr[maxn]; n int fi(int x) { return x == fa[x] ? x : fa[x] = fi(fa[x]); } n int main(int argc, char const *argv[]) { n   int n, m; n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; ++i) fa[i] = i, cnt[i] = 0, vis[i] = 0, arr[i] = 0; n   for (int i = 1, a, b; i <= m; ++i) { n     scanf( %d %d , &a, &b); n     cnt[a]++; n     cnt[b]++; n     vis[a] = 1; n     vis[b] = 1; n     int fx = fi(a), fy = fi(b); n     if (fx != fy) fa[fx] = fy; n   } n   int ans = 0, tot = 0; n   for (int i = 1; i <= n; ++i) { n     if (cnt[i] % 2 == 1) { n       arr[fi(i)]++; n     } n   } n   int ff = 0; n   int cnt2 = 0, cnt1 = 0; n   for (int i = 2; i <= n; ++i) n     if (vis[i] == 1) ff = 1; n   int cnt = 0; n   for (int i = 1; i <= n; ++i) { n     if (vis[i] && fa[i] == i && arr[i] >= 2) { n       tot = tot + (arr[i] - 2) / 2; n       ++cnt2; n     } n     if (vis[i] && fa[i] == i) tot += 1, cnt++; n   } n   cnt1 = cnt - cnt2; n   if (cnt == 1) { n     if (cnt1 == 1) { n       if (vis[1] == 0) n         printf( 2 n ); n       else n         printf( 0 n ); n     } else if (cnt2 == 1) { n       if (vis[1] == 0) tot++; n       printf( %d n , tot); n     } n   } else if (cnt == 0) { n     printf( 0 n ); n   } else if (cnt >= 2) { n     if (vis[1] == 0) ++tot; n     printf( %d n , tot); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n void input() { cin >> n; } n void solve() { n   int cnt = 0; n   for (int i = 1; i <= n; ++i) { n     if (cnt == n) { n       break; n     } n     printf( %d 1 n , i); n     ++cnt; n     if (cnt == n) { n       break; n     } n     if ((i % 2) == 0) { n       printf( %d 4 n , i); n       ++cnt; n     } n   } n } n int main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   t = 1; n   while (t--) { n     input(); n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int l = s.length(); n   cout << 4 << endl; n   cout <<  R   << l - 1 << endl; n   cout <<  L   << 2 << endl; n   cout <<  L   << l + 1 << endl; n   cout <<  L   << 2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n const int dx[] = {0, 1, 0, -1}; n const int dy[] = {1, 0, -1, 0}; n int n, k; n string s; n int main() { n   cin >> n >> k; n   cin >> s; n   int sum = 0; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  # ) sum++; n     if (sum >= k) { n       puts( NO ); n       return 0; n     } n     if (s[i] ==  . ) sum = 0; n   } n   puts( YES ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int b[65], p = 1e9 + 7; n long long pow_(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b & 1) res = res * a % p; n     b >>= 1, a = a * a % p; n   } n   return res % p; n } n void solve(int n, int x) { n   int t = 0; n   long long ans = 0; n   while (x) { n     if (x & 1) ans = (ans + pow_(n, t)) % p; n     x >>= 1, t++; n   } n   cout << ans % p << endl; n } n int main() { n   ios::sync_with_stdio(!true); n   int t, n, k; n   cin >> t; n   while (t--) cin >> n >> k, solve(n, k); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5000 + 10; n const int BASE = 1e9 + 7; n int n; n int memo[N][N], _memo[N][N]; n string s; n int _solve(int pos_1, int pos_2) { n   if (pos_1 > n || pos_2 > n) return 0; n   if (_memo[pos_1][pos_2] != -1) return _memo[pos_1][pos_2]; n   int cur = 0; n   if (s[pos_1] == s[pos_2]) cur += _solve(pos_1 + 1, pos_2 + 1) + 1; n   return _memo[pos_1][pos_2] = cur; n } n int solve(int pos, int len) { n   if (s[pos] ==  0 ) return 0; n   if (pos + len - 1 > n) return 0; n   if (pos + len - 1 == n) return 1; n   if (memo[pos][len] != -1) return memo[pos][len]; n   int cur = 0; n   cur = (cur + solve(pos, len + 1)) % BASE; n   if (pos + 2 * len - 1 <= n) { n     int tmp = _solve(pos, pos + len); n     if (tmp < len && s[pos + tmp] < s[pos + len + tmp]) { n       cur = (cur + solve(pos + len, len)) % BASE; n     } else n       cur = (cur + solve(pos + len, len + 1)) % BASE; n   } n   return memo[pos][len] = cur; n } n int main() { n   ios ::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> s; n   s =  @  + s; n   memset(memo, -1, sizeof(memo)); n   memset(_memo, -1, sizeof(_memo)); n   cout << solve(1, 1); n } n


#include <bits/stdc++.h> n using namespace std; n int parent[100005]; n int coun[100005]; n bool isLucky(int a) { n   while (a) { n     int x = a % 10; n     if (x == 4 || x == 7) { n     } else n       return false; n     a = a / 10; n   } n   return true; n } n int findp(int a) { n   if (parent[a] == a) { n     return a; n   } n   parent[a] = findp(parent[a]); n   return parent[a]; n } n void uni(int x, int y) { n   int parx = findp(x); n   int pary = findp(y); n   if (parx != pary) { n     parent[parx] = pary; n     coun[pary] += coun[parx]; n   } n } n vector<int> vec[100005]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(); n   cout.tie(); n   long long int n, i, j, k, x, y, c = 0, ans = 0, z, z1; n   cin >> n; n   for (int i = 1; i < n + 1; i++) { n     coun[i] = 1; n     parent[i] = i; n   } n   for (int i = 0; i < n - 1; i++) { n     cin >> x >> y >> z; n     if (!isLucky(z)) { n       vec[x].push_back(y); n       vec[y].push_back(x); n       uni(x, y); n     } n   } n   for (i = 1; i <= n; i++) { n     int x = findp(i); n     int y = coun[x]; n     ans += (n - y) * (n - y - 1); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fscpp() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n int main() { n   fscpp(); n   long long n; n   cin >> n; n   long long ct = 0; n   long long xm, ym; n   long long x, y; n   cin >> xm >> ym; n   vector<long long> vx(n), vy(n); n   vx[0] = xm; n   vy[0] = ym; n   for (long long int i = 1; i < n; i++) { n     cin >> x >> y; n     vx[i] = x; n     vy[i] = y; n   } n   vector<long long> mvx(vx), mvy(vy); n   sort(mvx.begin(), mvx.end()); n   sort(mvy.begin(), mvy.end()); n   long long max1 = mvx[n - 1], max2 = mvx[n - 2]; n   long long min1 = mvy[0], min2 = mvy[1]; n   long long len = min1 - max1; n   if (len < 0) len = 0; n   long long rx, ry; n   for (long long int i = 0; i < n; i++) { n     if (max1 == vx[i]) n       rx = max2; n     else n       rx = max1; n     if (min1 == vy[i]) n       ry = min2; n     else n       ry = min1; n     if (ry - rx > len) len = ry - rx; n   } n   len--; n   cout << len + 1; n   int p = 23; n   string s = std::bitset<63>(p).to_string(); n   string h; n   string g; n   g = s; n   for (long long int i = s.size() - 2; i < s.size(); i++) h += s[i]; n   y = h.size(); n   s.clear(); n   for (long long int i = 0; i < 31 - y; i++) { n     s +=  1 ; n   } n   sort(s.begin(), s.end()); n   s.erase(unique(s.begin(), s.end()), s.end()); n   for (long long int i = 0; i < y; i++) { n     s += h[i]; n   } n   y = std::bitset<63>(g).to_ulong(); n   g.clear(); n   if (g.size() != 0) cout <<  yedhukku n t n ; n   reverse(s.begin(), s.end()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxint = -1u >> 1; n const double pi = 3.14159265358979323; n const double eps = 1e-8; n int n, k, a[100100]; n set<long long> vis; n int main() { n   scanf( %d%d , &n, &k); n   for (int i = (0); i < (n); i++) scanf( %d , &a[i]); n   sort(a, a + n); n   int ans = 0; n   for (int i = (0); i < (n); i++) { n     if (vis.find(a[i]) == vis.end()) { n       ans++; n       vis.insert(1LL * a[i] * k); n     } else n       continue; n   } n   printf( %d n , ans); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 100 + 10; n const int MAX_V = 200 + 10; n const int MAX_S = 1 << 7; n const int INF = 1000000; n const int dx[] = {-1, 0, 1, 0}; n const int dy[] = {0, 1, 0, -1}; n int N, M, K, S; n int w[MAX_N][MAX_N]; n int f[MAX_N][MAX_N][MAX_S]; n int pre[MAX_N][MAX_N][MAX_S]; n int qx[MAX_V]; n int qy[MAX_V]; n int in[MAX_N][MAX_N]; n int cov[MAX_N][MAX_N]; n void Init() { n   int x, y; n   scanf( %d%d%d , &N, &M, &K); n   S = 1 << K; n   for (int i = 1; i <= N; ++i) n     for (int j = 1; j <= M; ++j) scanf( %d , &w[i][j]); n   for (int i = 1; i <= N; ++i) n     for (int j = 1; j <= M; ++j) n       for (int k = 1; k < S; ++k) { n         f[i][j][k] = INF; n         pre[i][j][k] = INF; n       } n   for (int i = 0; i < K; ++i) { n     scanf( %d%d , &x, &y); n     f[x][y][1 << i] = w[x][y]; n   } n } n void SPFA(int s) { n   int qh = 0, qt = 0; n   for (int i = 1; i <= N; ++i) n     for (int j = 1; j <= M; ++j) { n       qx[qt] = i; n       qy[qt++] = j; n       in[i][j] = true; n     } n   int nx, ny, px, py; n   for (; qh < qt;) { n     px = qx[qh]; n     py = qy[qh++]; n     for (int i = 0; i < 4; ++i) { n       nx = px + dx[i]; n       ny = py + dy[i]; n       if (nx > 0 && nx <= N && ny > 0 && ny <= M && n           f[nx][ny][s] > f[px][py][s] + w[nx][ny]) { n         f[nx][ny][s] = f[px][py][s] + w[nx][ny]; n         pre[nx][ny][s] = -i; n         if (!in[nx][ny]) { n           in[nx][ny] = true; n           qx[qt] = nx; n           qy[qt++] = ny; n         } n       } n     } n     in[px][py] = false; n   } n } n void DFS(int x, int y, int s) { n   cov[x][y] = true; n   int tmp = pre[x][y][s]; n   if (tmp == INF) return; n   if (tmp <= 0) n     DFS(x - dx[-tmp], y - dy[-tmp], s); n   else n     DFS(x, y, tmp), DFS(x, y, s - tmp); n } n void Solve() { n   for (int s = 1; s < S; ++s) { n     for (int i = 1; i <= N; ++i) n       for (int j = 1; j <= M; ++j) n         for (int ns = (s - 1) & s; ns; ns = (ns - 1) & s) n           if (f[i][j][s] > f[i][j][ns] + f[i][j][s - ns] - w[i][j]) { n             f[i][j][s] = f[i][j][ns] + f[i][j][s - ns] - w[i][j]; n             pre[i][j][s] = ns; n           } n     SPFA(s); n     if (0) { n       printf( Now is state %d n , s); n       for (int i = 1; i <= N; ++i) { n         for (int j = 1; j <= M; ++j) printf( %d  , f[i][j][s]); n         printf(  n ); n       } n     } n   } n   int ax, ay, ans = INF; n   for (int i = 1; i <= N; ++i) n     for (int j = 1; j <= M; ++j) n       if (f[i][j][S - 1] < ans) { n         ans = f[i][j][S - 1]; n         ax = i, ay = j; n       } n   printf( %d n , ans); n   DFS(ax, ay, S - 1); n   for (int i = 1; i <= N; ++i) { n     for (int j = 1; j <= M; ++j) putchar(cov[i][j] ?  X  :  . ); n     putchar(  n ); n   } n } n int main() { n   Init(); n   Solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool isprime(long long n) { n   if (n < 2) return false; n   if (n == 2) return true; n   if (n % 2 == 0) return false; n   for (int i = 3; i * i <= n; i = i + 2) { n     if (n % i == 0) return false; n   } n   return true; n } n int main() { n   int w, h, k; n   cin >> w >> h >> k; n   int sum = 0; n   while (k--) { n     sum += (2 * w + (2 * (h - 2))); n     w -= 4; n     h -= 4; n   } n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, r, s; n   cin >> n; n   if (n % 2 == 1) { n     if (n == 1) { n       cout <<  -1 ; n     } else { n       r = (n * n - 1) / 2; n       s = (n * n + 1) / 2; n       cout << r <<     << s; n     } n   } else { n     if (n == 2) { n       cout <<  -1 ; n     } else { n       r = (n / 2) * (n / 2) - 1; n       s = (n / 2) * (n / 2) + 1; n       cout << r <<     << s; n     } n   } n   cout << endl; n } n


#include <bits/stdc++.h> n const double eps = 1e-11; n int dx[] = {-1, 0, 1, 0}; n int dy[] = {0, 1, 0, -1}; n using namespace std; n const int maxn = 1005; n int n; n typedef struct node_ { n   int x, y; n } N; n N node[maxn]; n pair<int, pair<int, int> > ans[100005]; n int cnt; n void deal(int t, int a, int b) { n   ans[cnt].first = t, ans[cnt].second.first = a, ans[cnt++].second.second = b; n } n int main() { n   while (scanf( %d , &n) == 1) { n     cnt = 0; n     for (int i = (1); i < (n); i++) { n       scanf( %d%d , &node[i].x, &node[i].y); n     } n     for (int i = 1; i < n; i++) { n       int tmp; n       if (node[i].x != i + 1) { n         tmp = node[i].x; n         for (int j = (1); j < (n); j++) { n           if (node[j].x == tmp) { n             node[j].x = i + 1; n           } else if (node[j].x == i + 1) { n             node[j].x = tmp; n           } n         } n         deal(1, tmp, i + 1); n       } n       if (node[i].y > i) { n         tmp = node[i].y; n         for (int j = (1); j < (n); j++) { n           if (node[j].y == tmp) { n             node[j].y = i; n           } else if (node[j].y == i) { n             node[j].y = tmp; n           } n         } n         deal(2, tmp, i); n       } n     } n     printf( %d n , cnt); n     for (int i = 0; i < cnt; i++) { n       printf( %d %d %d n , ans[i].first, ans[i].second.first, n              ans[i].second.second); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c; n   cin >> a >> b >> c; n   cout << min(a + a + b + b, min(a + b + c, min(a + a + c + c, b + b + c + c))) n        << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 6; n namespace AC { n int tot, tr[N][26]; n int fail[N]; n int cnt[N]; n int sid[N]; n int next[N]; n bool vis[N]; n int isend[N]; n int in[N]; n int val[N]; n int victim[N]; n queue<int> q; n void init() { n   memset(fail, 0, sizeof(fail)); n   memset(tr, 0, sizeof(tr)); n   memset(cnt, 0, sizeof(cnt)); n   memset(vis, false, sizeof vis); n   memset(sid, 0, sizeof sid); n   memset(val, 0, sizeof val); n   memset(isend, 0, sizeof isend); n   memset(victim, 0, sizeof victim); n   memset(next, 0, sizeof next); n   tot = 0; n } n void insert(char *s, int id) { n   int u = 0; n   for (int i = 1; s[i]; i++) { n     if (!tr[u][s[i] -  a ]) tr[u][s[i] -  a ] = ++tot; n     u = tr[u][s[i] -  a ]; n   } n   sid[id] = u; n   isend[u] = true; n } n void build() { n   next[0] = 0; n   for (int i = 0; i < 26; i++) n     if (tr[0][i]) q.push(tr[0][i]); n   while (q.size()) { n     int u = q.front(); n     q.pop(); n     for (int i = 0; i < 26; i++) { n       if (tr[u][i]) { n         fail[tr[u][i]] = tr[fail[u]][i], q.push(tr[u][i]); n         in[fail[tr[u][i]]] += 1; n       } else n         tr[u][i] = tr[fail[u]][i]; n       next[tr[u][i]] = n           isend[fail[tr[u][i]]] ? fail[tr[u][i]] : next[fail[tr[u][i]]]; n     } n   } n } n int query(char *t) { n   int u = 0, res = -1; n   for (int i = 1; t[i]; i++) { n     u = tr[u][t[i] -  a ]; n     for (int x = u; x; x = next[x]) { n       if (isend[x]) { n         res = max(res, victim[x]); n       } n     } n   } n   return res; n } n void query(char *t, int) { n   int u = 0; n   for (int i = 1; t[i]; i++) { n     u = tr[u][t[i] -  a ]; n     val[u] += 1; n   } n } n void topoSort() { n   for (int i = 0; i < N; ++i) n     if (in[i] == 0) q.push(i); n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     int v = fail[u]; n     in[v]--; n     val[v] += val[u]; n     if (in[v] == 0) q.push(v); n   } n } n }  // namespace AC n char s[N]; n int val[N]; n map<int, int> mp[N]; n int main() { n   int ans = 0; n   int n, m; n   cin >> n >> m; n   AC::init(); n   for (int i = 0; i < n; i++) { n     scanf( %s , s + 1); n     AC::insert(s, i); n     mp[AC::sid[i]][0] += 1; n   } n   AC::build(); n   while (m--) { n     int x, c, v; n     scanf( %d , &x); n     if (x == 1) { n       scanf( %d %d , &c, &v); n       mp[AC::sid[c - 1]][val[c - 1]] -= 1; n       if (mp[AC::sid[c - 1]][val[c - 1]] == 0) { n         mp[AC::sid[c - 1]].erase(val[c - 1]); n       } n       val[c - 1] = v; n       mp[AC::sid[c - 1]][v] += 1; n       AC::victim[AC::sid[c - 1]] = (*--mp[AC::sid[c - 1]].end()).first; n       continue; n     } n     scanf( %s , s + 1); n     cout << AC::query(s) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> a; n vector<pair<int, int> > s; n int main() { n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     a.push_back(x); n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n - 1; j++) { n       if (a[j] > a[j + 1]) { n         swap(a[j], a[j + 1]); n         s.push_back({j + 1, j + 2}); n       } n     } n   } n   for (auto a : s) { n     cout << a.first <<     << a.second <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   short n, before, after; n   cin >> n; n   string a; n   bool flag = false; n   for (short i = 0; i < n; i++) { n     cin >> a; n     cin >> before; n     cin >> after; n     if (after >= 2400 && before >= 2400 && after - before > 0) flag = true; n   } n   if (flag == true) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include<bits/stdc++.h> n #define pb push_back n #define int long long n using namespace std; n int p=998244353,tt,n,m,fac[25]; n void prepare(){ n  fac[0]=1; n  for(int i=1;i<25;i++){ n   fac[i]=(fac[i-1]*i)%p; n  } n } n int inv(int z){ n  int s=p-2,t=z,ans=1; n     while(s>0){ n      if(s%2)ans=(ans*t)%p; n      t=(t*t)%p; n      s/=2; n     } n     return ans; n } n void solve(){ n  prepare(); n  cin>>n>>m; n  int a[n+1][m+1],x=0; n  for(int i=1;i<=n;i++){ n   for(int j=1;j<=m;j++){ n    cin>>a[i][j]; n   } n  } n  for(int i=1;i<=m;i++){ n   vector<int> v; n   for(int j=1;j<=n;j++)v.pb(a[j][i]); n      sort(v.begin(),v.end()); n      int c=1; n      for(int i=0;i<n;i++){ n       c=(c*(v[i]-i-1))%p; n      } n      x=(x+fac[n]-c+p)%p; n  } n     cout<<((x*inv(fac[n]))%p)<<  n ; n  } n  n signed main(){ n     #ifndef ONLINE_JUDGE n     freopen( input.txt , r ,stdin); n     freopen( output.txt , w ,stdout); n     #endif n     ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); n     //cin>>tt; n     //while(tt--) n     solve(); n }


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e4 + 10, mod = 1e9 + 9, maxa = 1e6 + 100; n long long sta = 0; n const long long inf = 2e18 + 13; n long long max(long long x, long long y) { return (x > y ? x : y); } n long long min(long long x, long long y) { return (x < y ? x : y); } n long long pw(long long x, long long y) { n   if (!y) { n     return 1; n   } n   long long res = pw(x, y / 2); n   res *= res; n   res %= mod; n   if (y & 1) { n     res *= x; n   } n   return res % mod; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, m; n   cin >> n >> m; n   long long ans = 1; n   for (int i = 1; i <= n; i++) { n     ans = ans * (pw(2, m) - i); n     ans += mod; n     ans %= mod; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, tx, ty; n vector<pair<int, int> > ar; n int vis[100010]; n bool check(int first, int second, int x1, int y1, int x2, int y2) { n   if ((1LL * x2 - 1LL * x1) * 1LL * (1LL * second - 1LL * y1) == n       (1LL * y2 - 1LL * y1) * 1LL * (1LL * first - 1LL * x1)) n     return true; n   return false; n } n int main(int argc, char const *argv[]) { n   scanf( %d , &n); n   ar.clear(); n   for (int i = 0; i < n; i++) { n     scanf( %d%d , &tx, &ty); n     ar.push_back({tx, ty}); n   } n   if (n == 1) { n     cout <<  YES n ; n   } else if (n == 2) { n     cout <<  YES n ; n   } else { n     memset(vis, 0, sizeof(vis)); n     for (int i = 0; i < n; i++) { n       if (check(ar[i].first, ar[i].second, ar[0].first, ar[0].second, n                 ar[1].first, ar[1].second)) { n         vis[i] = 1; n       } n     } n     int x1, y1, x2, y2, flag = 0, hold1, hold2; n     for (int j = 0; j < n; j++) { n       if (!vis[j]) { n         flag++; n         x1 = ar[j].first; n         y1 = ar[j].second; n         vis[j] = 1; n         hold1 = j; n         break; n       } n     } n     for (int j = 0; j < n; j++) { n       if (!vis[j]) { n         flag++; n         x2 = ar[j].first; n         y2 = ar[j].second; n         int fgh = 1; n         if (check(ar[0].first, ar[0].second, x1, y1, x2, y2) || n             check(ar[1].first, ar[1].second, x1, y1, x2, y2)) { n           fgh = 0; n           hold2 = j; n           continue; n         } n         if (fgh) { n           vis[j] = 1; n           hold2 = j; n         } n         break; n       } n     } n     vis[hold2] = 1; n     if (flag == 0 || flag == 1) { n       cout <<  YES n ; n     } else { n       for (int i = 0; i < n; i++) { n         if (check(ar[i].first, ar[i].second, x1, y1, x2, y2)) { n           vis[i] = 1; n         } n       } n       flag = 0; n       for (int i = 0; i < n; i++) { n         if (!vis[i]) { n           flag = 1; n         } n       } n       if (!flag) n         cout <<  YES n ; n       else { n         memset(vis, 0, sizeof(vis)); n         for (int i = 0; i < n; i++) { n           if (check(ar[i].first, ar[i].second, ar[0].first, ar[0].second, x1, n                     y1)) n             vis[i] = 1; n           else if (check(ar[i].first, ar[i].second, ar[1].first, ar[1].second, n                          x2, y2)) n             vis[i] = 1; n         } n         flag = 0; n         for (int i = 0; i < n; i++) { n           if (!vis[i]) { n             flag = 1; n           } n         } n         if (!flag) { n           cout <<  YES n ; n         } else { n           memset(vis, 0, sizeof(vis)); n           for (int i = 0; i < n; i++) { n             if (check(ar[i].first, ar[i].second, ar[0].first, ar[0].second, x2, n                       y2)) n               vis[i] = 1; n             else if (check(ar[i].first, ar[i].second, ar[1].first, ar[1].second, n                            x1, y1)) n               vis[i] = 1; n           } n           flag = 0; n           for (int i = 0; i < n; i++) { n             if (!vis[i]) { n               flag = 1; n             } n           } n           if (flag) n             cout <<  NO n ; n           else n             cout <<  YES n ; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long K, B, N, T; n int main() { n   cin >> K >> B >> N >> T; n   if (K == 1 && 1 + N * B >= T) { n     cout << (int)ceil((1 + N * B - T) / (1.0 * B)) << endl; n     return 0; n   } n   if (K == 1) { n     cout << 0 << endl; n     return 0; n   } n   long long C = 1, counter = N + 1; n   double thres = 1.0 * (T * (K - 1) + B) / (K - 1 + B); n   for (int i = 1; i <= N; i++) { n     C *= K; n     if (C > thres) { n       counter = i; n       break; n     } n   } n   counter--; n   cout << N - counter << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char buff[100000], *pb; n int readInt() { n   int num = 0; n   while (*pb && isdigit(*pb)) { n     num = num * 10 + (*pb -  0 ); n     ++pb; n   } n   return num; n } n int nextChar() { n   int c =   0 ; n   if (*pb) { n     c = *pb; n     pb++; n   } n   return c; n } n int hasNext() { return *pb ? 1 : 0; } n int main(void) { n   int n; n   scanf( %i n , &n); n   int nextId = 1; n   map<vector<int>, int> SId; n   vector<int> deg(n + 1); n   vector<vector<vector<int>>> S(n + 1); n   vector<vector<int>> H(n + 1); n   for (int i = 1; i <= n; i++) { n     scanf( %s , buff); n     pb = buff; n     while (hasNext()) { n       int k = readInt(); n       assert(nextChar() ==  : ); n       vector<int> s; n       while (hasNext()) { n         s.push_back(readInt()); n         if (nextChar() ==  - ) { n           break; n         } n       } n       assert(s.size() == k); n       sort(begin(s), end(s)); n       S[i].push_back(s); n       deg[i]++; n       if (!SId.count(s)) { n         SId[s] = nextId++; n       } n       H[i].push_back(SId[s]); n     } n   } n   vector<vector<int>> sh = H; n   for (vector<int> &s : sh) { n     sort(s.begin(), s.end()); n   } n   vector<int> from, to; n   vector<int> proc(n + 1); n   queue<int> Q; n   for (int i = 1; i <= n; i++) { n     if (deg[i] == 1) { n       proc[i] = 1; n       Q.push(i); n     } n   } n   vector<int> tree_deg(n + 1); n   map<int, int> root; n   set<int> seen; n   while (!Q.empty()) { n     int u = Q.front(); n     Q.pop(); n     vector<int> sub = {u}; n     for (int j = 0; j < H[u].size(); j++) { n       int id = H[u][j]; n       if (seen.count(id)) { n         from.push_back(u); n         to.push_back(root[id]); n         tree_deg[root[id]]++; n         for (auto e : S[u][j]) { n           sub.push_back(e); n         } n       } n     } n     sort(begin(sub), end(sub)); n     int id = SId[sub]; n     seen.insert(id); n     root[id] = u; n     for (int i = 1; i <= n; i++) n       if (!proc[i]) { n         if (binary_search(begin(sh[i]), end(sh[i]), id)) { n           deg[i]--; n           if (deg[i] <= 1) { n             proc[i] = 1; n             Q.push(i); n           } n         } n       } n   } n   bool ok = true; n   for (int d : tree_deg) { n     if (d > 1) { n       ok = false; n     } n   } n   if (ok && int(from.size()) == n - 1) { n     printf( %i n , n - 1); n     for (int i = 0; i < n - 1; i++) { n       printf( %i %i n , from[i], to[i]); n     } n   } else { n     puts( -1 ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int a, b, c, d; n     cin >> a >> b >> c >> d; n     if (min(a, b) + min(c, d) == max(a, b) && max(a, b) == max(c, d)) { n       cout <<  YES n ; n     } else { n       cout <<  NO n ; n     } 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   ; n   long long int t; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     long long int a[26] = {0}; n     long long int cnt = 0; n     char c; n     int i = 0; n     while (i < s.length()) { n       cnt = 0; n       c = s[i]; n       while (s[i] == c && i < s.length()) { n         cnt++; n         i++; n       } n       if (cnt % 2 != 0) { n         a[c -  a ] = 1; n       } n     } n     string ans =   ; n     for (i = 0; i < 26; i++) { n       if (a[i] == 1) { n         ans += (i +  a ); n       } n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int check(string st) { n   int s1 = 0, s2 = 0; n   for (int i = 1; i < st.length(); i++) { n     if (st[i] ==  1  && i % 2 == 1) s1++; n     if (st[i] ==  1  && i % 2 == 0) s2++; n     if (s1 - s2 > 5 - i / 2 || (i % 2 == 1 && s2 - s1 >= 5 - i / 2) || n         (i % 2 == 0 && s2 - s1 > 5 - i / 2)) n       return i; n   } n   return 10; n }; n int main() { n   int t; n   cin >> t; n   while (t--) { n     string st; n     cin >> st; n     st =     + st; n     string sts = st; n     for (int i = 1; i < st.length(); i++) n       if (st[i] ==  ? ) n         if (i % 2 == 1) n           st[i] =  1 ; n         else n           st[i] =  0 ; n     for (int i = 1; i < sts.length(); i++) n       if (sts[i] ==  ? ) n         if (i % 2 == 0) n           sts[i] =  1 ; n         else n           sts[i] =  0 ; n     cout << min(check(st), check(sts)) << endl; n   } n   return 0; n } n